Merge changes from merged-arm-thumb-backend-branch onto trunk.
[official-gcc.git] / gcc / config / arm / arm.c
blob3a4acd798468f6480a5038bfcc36d9b58d9a59a3
1 /* Output routines for GCC for ARM.
2 Copyright (C) 1991, 93, 94, 95, 96, 97, 98, 99, 2000 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 "tree.h"
28 #include "tm_p.h"
29 #include "regs.h"
30 #include "hard-reg-set.h"
31 #include "real.h"
32 #include "insn-config.h"
33 #include "conditions.h"
34 #include "insn-flags.h"
35 #include "output.h"
36 #include "insn-attr.h"
37 #include "flags.h"
38 #include "reload.h"
39 #include "function.h"
40 #include "expr.h"
41 #include "toplev.h"
42 #include "recog.h"
43 #include "ggc.h"
44 #include "except.h"
45 #include "tm_p.h"
47 /* Forward definitions of types. */
48 typedef struct minipool_node Mnode;
49 typedef struct minipool_fixup Mfix;
51 /* In order to improve the layout of the prototypes below
52 some short type abbreviations are defined here. */
53 #define Hint HOST_WIDE_INT
54 #define Mmode enum machine_mode
55 #define Ulong unsigned long
57 /* Forward function declarations. */
58 static void arm_add_gc_roots PARAMS ((void));
59 static int arm_gen_constant PARAMS ((enum rtx_code, Mmode, Hint, rtx, rtx, int, int));
60 static int arm_naked_function_p PARAMS ((tree));
61 static Ulong bit_count PARAMS ((signed int));
62 static int const_ok_for_op PARAMS ((Hint, enum rtx_code));
63 static int eliminate_lr2ip PARAMS ((rtx *));
64 static rtx emit_multi_reg_push PARAMS ((int));
65 static rtx emit_sfm PARAMS ((int, int));
66 static char * fp_const_from_val PARAMS ((REAL_VALUE_TYPE *));
67 static int function_really_clobbers_lr PARAMS ((rtx));
68 static arm_cc get_arm_condition_code PARAMS ((rtx));
69 static void init_fpa_table PARAMS ((void));
70 static Hint int_log2 PARAMS ((Hint));
71 static rtx is_jump_table PARAMS ((rtx));
72 static char * output_multi_immediate PARAMS ((rtx *, char *, char *, int, Hint));
73 static int pattern_really_clobbers_lr PARAMS ((rtx));
74 static void print_multi_reg PARAMS ((FILE *, char *, int, int, int));
75 static Mmode select_dominance_cc_mode PARAMS ((rtx, rtx, Hint));
76 static char * shift_op PARAMS ((rtx, Hint *));
77 static void arm_init_machine_status PARAMS ((struct function *));
78 static void arm_mark_machine_status PARAMS ((struct function *));
79 static int number_of_first_bit_set PARAMS ((int));
80 static void replace_symbols_in_block PARAMS ((tree, rtx, rtx));
81 static void thumb_exit PARAMS ((FILE *, int, rtx));
82 static void thumb_pushpop PARAMS ((FILE *, int, int));
83 static char * thumb_condition_code PARAMS ((rtx, int));
84 static rtx is_jump_table PARAMS ((rtx));
85 static Hint get_jump_table_size PARAMS ((rtx));
86 static Mnode * move_minipool_fix_forward_ref PARAMS ((Mnode *, Mnode *, Hint));
87 static Mnode * add_minipool_forward_ref PARAMS ((Mfix *));
88 static Mnode * move_minipool_fix_backward_ref PARAMS ((Mnode *, Mnode *, Hint));
89 static Mnode * add_minipool_backward_ref PARAMS ((Mfix *));
90 static void assign_minipool_offsets PARAMS ((Mfix *));
91 static void arm_print_value PARAMS ((FILE *, rtx));
92 static void dump_minipool PARAMS ((rtx));
93 static int arm_barrier_cost PARAMS ((rtx));
94 static Mfix * create_fix_barrier PARAMS ((Mfix *, Hint));
95 static void push_minipool_barrier PARAMS ((rtx, Hint));
96 static void push_minipool_fix PARAMS ((rtx, Hint, rtx *, Mmode, rtx));
97 static void note_invalid_constants PARAMS ((rtx, Hint));
99 #undef Hint
100 #undef Mmode
101 #undef Ulong
103 /* The maximum number of insns skipped which will be conditionalised if
104 possible. */
105 static int max_insns_skipped = 5;
107 extern FILE * asm_out_file;
109 /* True if we are currently building a constant table. */
110 int making_const_table;
112 /* Define the information needed to generate branch insns. This is
113 stored from the compare operation. */
114 rtx arm_compare_op0, arm_compare_op1;
116 /* What type of floating point are we tuning for? */
117 enum floating_point_type arm_fpu;
119 /* What type of floating point instructions are available? */
120 enum floating_point_type arm_fpu_arch;
122 /* What program mode is the cpu running in? 26-bit mode or 32-bit mode. */
123 enum prog_mode_type arm_prgmode;
125 /* Set by the -mfp=... option. */
126 const char * target_fp_name = NULL;
128 /* Used to parse -mstructure_size_boundary command line option. */
129 const char * structure_size_string = NULL;
130 int arm_structure_size_boundary = DEFAULT_STRUCTURE_SIZE_BOUNDARY;
132 /* Bit values used to identify processor capabilities. */
133 #define FL_CO_PROC (1 << 0) /* Has external co-processor bus */
134 #define FL_FAST_MULT (1 << 1) /* Fast multiply */
135 #define FL_MODE26 (1 << 2) /* 26-bit mode support */
136 #define FL_MODE32 (1 << 3) /* 32-bit mode support */
137 #define FL_ARCH4 (1 << 4) /* Architecture rel 4 */
138 #define FL_ARCH5 (1 << 5) /* Architecture rel 5 */
139 #define FL_THUMB (1 << 6) /* Thumb aware */
140 #define FL_LDSCHED (1 << 7) /* Load scheduling necessary */
141 #define FL_STRONG (1 << 8) /* StrongARM */
143 /* The bits in this mask specify which instructions we are
144 allowed to generate. */
145 static int insn_flags = 0;
147 /* The bits in this mask specify which instruction scheduling options should
148 be used. Note - there is an overlap with the FL_FAST_MULT. For some
149 hardware we want to be able to generate the multiply instructions, but to
150 tune as if they were not present in the architecture. */
151 static int tune_flags = 0;
153 /* The following are used in the arm.md file as equivalents to bits
154 in the above two flag variables. */
156 /* Nonzero if this is an "M" variant of the processor. */
157 int arm_fast_multiply = 0;
159 /* Nonzero if this chip supports the ARM Architecture 4 extensions. */
160 int arm_arch4 = 0;
162 /* Nonzero if this chip supports the ARM Architecture 5 extensions. */
163 int arm_arch5 = 0;
165 /* Nonzero if this chip can benefit from load scheduling. */
166 int arm_ld_sched = 0;
168 /* Nonzero if this chip is a StrongARM. */
169 int arm_is_strong = 0;
171 /* Nonzero if this chip is a an ARM6 or an ARM7. */
172 int arm_is_6_or_7 = 0;
174 /* In case of a PRE_INC, POST_INC, PRE_DEC, POST_DEC memory reference, we
175 must report the mode of the memory reference from PRINT_OPERAND to
176 PRINT_OPERAND_ADDRESS. */
177 enum machine_mode output_memory_reference_mode;
179 /* Nonzero if the prologue must setup `fp'. */
180 int current_function_anonymous_args;
182 /* The register number to be used for the PIC offset register. */
183 const char * arm_pic_register_string = NULL;
184 int arm_pic_register = 9;
186 /* Set to one if we think that lr is only saved because of subroutine calls,
187 but all of these can be `put after' return insns. */
188 int lr_save_eliminated;
190 /* Set to 1 when a return insn is output, this means that the epilogue
191 is not needed. */
192 int return_used_this_function;
194 /* Set to 1 after arm_reorg has started. Reset to start at the start of
195 the next function. */
196 static int after_arm_reorg = 0;
198 /* The maximum number of insns to be used when loading a constant. */
199 static int arm_constant_limit = 3;
201 /* For an explanation of these variables, see final_prescan_insn below. */
202 int arm_ccfsm_state;
203 enum arm_cond_code arm_current_cc;
204 rtx arm_target_insn;
205 int arm_target_label;
207 /* The condition codes of the ARM, and the inverse function. */
208 char * arm_condition_codes[] =
210 "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
211 "hi", "ls", "ge", "lt", "gt", "le", "al", "nv"
214 #define streq(string1, string2) (strcmp (string1, string2) == 0)
216 /* Initialization code. */
218 struct processors
220 char * name;
221 unsigned int flags;
224 /* Not all of these give usefully different compilation alternatives,
225 but there is no simple way of generalizing them. */
226 static struct processors all_cores[] =
228 /* ARM Cores */
230 {"arm2", FL_CO_PROC | FL_MODE26 },
231 {"arm250", FL_CO_PROC | FL_MODE26 },
232 {"arm3", FL_CO_PROC | FL_MODE26 },
233 {"arm6", FL_CO_PROC | FL_MODE26 | FL_MODE32 },
234 {"arm60", FL_CO_PROC | FL_MODE26 | FL_MODE32 },
235 {"arm600", FL_CO_PROC | FL_MODE26 | FL_MODE32 },
236 {"arm610", FL_MODE26 | FL_MODE32 },
237 {"arm620", FL_CO_PROC | FL_MODE26 | FL_MODE32 },
238 {"arm7", FL_CO_PROC | FL_MODE26 | FL_MODE32 },
239 /* arm7m doesn't exist on its own, but only with D, (and I), but
240 those don't alter the code, so arm7m is sometimes used. */
241 {"arm7m", FL_CO_PROC | FL_MODE26 | FL_MODE32 | FL_FAST_MULT },
242 {"arm7d", FL_CO_PROC | FL_MODE26 | FL_MODE32 },
243 {"arm7dm", FL_CO_PROC | FL_MODE26 | FL_MODE32 | FL_FAST_MULT },
244 {"arm7di", FL_CO_PROC | FL_MODE26 | FL_MODE32 },
245 {"arm7dmi", FL_CO_PROC | FL_MODE26 | FL_MODE32 | FL_FAST_MULT },
246 {"arm70", FL_CO_PROC | FL_MODE26 | FL_MODE32 },
247 {"arm700", FL_CO_PROC | FL_MODE26 | FL_MODE32 },
248 {"arm700i", FL_CO_PROC | FL_MODE26 | FL_MODE32 },
249 {"arm710", FL_MODE26 | FL_MODE32 },
250 {"arm720", FL_MODE26 | FL_MODE32 },
251 {"arm710c", FL_MODE26 | FL_MODE32 },
252 {"arm7100", FL_MODE26 | FL_MODE32 },
253 {"arm7500", FL_MODE26 | FL_MODE32 },
254 /* Doesn't have an external co-proc, but does have embedded fpu. */
255 {"arm7500fe", FL_CO_PROC | FL_MODE26 | FL_MODE32 },
256 {"arm7tdmi", FL_CO_PROC | FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_THUMB },
257 {"arm8", FL_MODE26 | FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_LDSCHED },
258 {"arm810", FL_MODE26 | FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_LDSCHED },
259 {"arm9", FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_THUMB | FL_LDSCHED },
260 {"arm920", FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_LDSCHED },
261 {"arm920t", FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_THUMB | FL_LDSCHED },
262 {"arm9tdmi", FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_THUMB | FL_LDSCHED },
263 {"strongarm", FL_MODE26 | FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_LDSCHED | FL_STRONG },
264 {"strongarm110", FL_MODE26 | FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_LDSCHED | FL_STRONG },
265 {"strongarm1100", FL_MODE26 | FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_LDSCHED | FL_STRONG },
267 {NULL, 0}
270 static struct processors all_architectures[] =
272 /* ARM Architectures */
274 { "armv2", FL_CO_PROC | FL_MODE26 },
275 { "armv2a", FL_CO_PROC | FL_MODE26 },
276 { "armv3", FL_CO_PROC | FL_MODE26 | FL_MODE32 },
277 { "armv3m", FL_CO_PROC | FL_MODE26 | FL_MODE32 | FL_FAST_MULT },
278 { "armv4", FL_CO_PROC | FL_MODE26 | FL_MODE32 | FL_FAST_MULT | FL_ARCH4 },
279 /* Strictly, FL_MODE26 is a permitted option for v4t, but there are no
280 implementations that support it, so we will leave it out for now. */
281 { "armv4t", FL_CO_PROC | FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_THUMB },
282 { "armv5", FL_CO_PROC | FL_MODE32 | FL_FAST_MULT | FL_ARCH4 | FL_THUMB | FL_ARCH5 },
283 { NULL, 0 }
286 /* This is a magic stucture. The 'string' field is magically filled in
287 with a pointer to the value specified by the user on the command line
288 assuming that the user has specified such a value. */
290 struct arm_cpu_select arm_select[] =
292 /* string name processors */
293 { NULL, "-mcpu=", all_cores },
294 { NULL, "-march=", all_architectures },
295 { NULL, "-mtune=", all_cores }
298 /* Return the number of bits set in value' */
299 static unsigned long
300 bit_count (value)
301 signed int value;
303 unsigned long count = 0;
305 while (value)
307 value &= ~(value & - value);
308 ++ count;
311 return count;
314 /* Fix up any incompatible options that the user has specified.
315 This has now turned into a maze. */
316 void
317 arm_override_options ()
319 unsigned i;
321 /* Set up the flags based on the cpu/architecture selected by the user. */
322 for (i = sizeof (arm_select) / sizeof (arm_select[0]); i--;)
324 struct arm_cpu_select * ptr = arm_select + i;
326 if (ptr->string != NULL && ptr->string[0] != '\0')
328 const struct processors * sel;
330 for (sel = ptr->processors; sel->name != NULL; sel ++)
331 if (streq (ptr->string, sel->name))
333 if (i == 2)
334 tune_flags = sel->flags;
335 else
337 /* If we have been given an architecture and a processor
338 make sure that they are compatible. We only generate
339 a warning though, and we prefer the CPU over the
340 architecture. */
341 if (insn_flags != 0 && (insn_flags ^ sel->flags))
342 warning ("switch -mcpu=%s conflicts with -march= switch",
343 ptr->string);
345 insn_flags = sel->flags;
348 break;
351 if (sel->name == NULL)
352 error ("bad value (%s) for %s switch", ptr->string, ptr->name);
356 /* If the user did not specify a processor, choose one for them. */
357 if (insn_flags == 0)
359 struct processors * sel;
360 unsigned int sought;
361 static struct cpu_default
363 int cpu;
364 char * name;
366 cpu_defaults[] =
368 { TARGET_CPU_arm2, "arm2" },
369 { TARGET_CPU_arm6, "arm6" },
370 { TARGET_CPU_arm610, "arm610" },
371 { TARGET_CPU_arm710, "arm710" },
372 { TARGET_CPU_arm7m, "arm7m" },
373 { TARGET_CPU_arm7500fe, "arm7500fe" },
374 { TARGET_CPU_arm7tdmi, "arm7tdmi" },
375 { TARGET_CPU_arm8, "arm8" },
376 { TARGET_CPU_arm810, "arm810" },
377 { TARGET_CPU_arm9, "arm9" },
378 { TARGET_CPU_strongarm, "strongarm" },
379 { TARGET_CPU_generic, "arm" },
380 { 0, 0 }
382 struct cpu_default * def;
384 /* Find the default. */
385 for (def = cpu_defaults; def->name; def ++)
386 if (def->cpu == TARGET_CPU_DEFAULT)
387 break;
389 /* Make sure we found the default CPU. */
390 if (def->name == NULL)
391 abort ();
393 /* Find the default CPU's flags. */
394 for (sel = all_cores; sel->name != NULL; sel ++)
395 if (streq (def->name, sel->name))
396 break;
398 if (sel->name == NULL)
399 abort ();
401 insn_flags = sel->flags;
403 /* Now check to see if the user has specified some command line
404 switch that require certain abilities from the cpu. */
405 sought = 0;
407 if (TARGET_INTERWORK || TARGET_THUMB)
409 sought |= (FL_THUMB | FL_MODE32);
411 /* Force apcs-32 to be used for interworking. */
412 target_flags |= ARM_FLAG_APCS_32;
414 /* There are no ARM processors that support both APCS-26 and
415 interworking. Therefore we force FL_MODE26 to be removed
416 from insn_flags here (if it was set), so that the search
417 below will always be able to find a compatible processor. */
418 insn_flags &= ~ FL_MODE26;
420 else if (! TARGET_APCS_32)
421 sought |= FL_MODE26;
423 if (sought != 0 && ((sought & insn_flags) != sought))
425 /* Try to locate a CPU type that supports all of the abilities
426 of the default CPU, plus the extra abilities requested by
427 the user. */
428 for (sel = all_cores; sel->name != NULL; sel ++)
429 if ((sel->flags & sought) == (sought | insn_flags))
430 break;
432 if (sel->name == NULL)
434 unsigned int current_bit_count = 0;
435 struct processors * best_fit = NULL;
437 /* Ideally we would like to issue an error message here
438 saying that it was not possible to find a CPU compatible
439 with the default CPU, but which also supports the command
440 line options specified by the programmer, and so they
441 ought to use the -mcpu=<name> command line option to
442 override the default CPU type.
444 Unfortunately this does not work with multilibing. We
445 need to be able to support multilibs for -mapcs-26 and for
446 -mthumb-interwork and there is no CPU that can support both
447 options. Instead if we cannot find a cpu that has both the
448 characteristics of the default cpu and the given command line
449 options we scan the array again looking for a best match. */
450 for (sel = all_cores; sel->name != NULL; sel ++)
451 if ((sel->flags & sought) == sought)
453 unsigned int count;
455 count = bit_count (sel->flags & insn_flags);
457 if (count >= current_bit_count)
459 best_fit = sel;
460 current_bit_count = count;
464 if (best_fit == NULL)
465 abort ();
466 else
467 sel = best_fit;
470 insn_flags = sel->flags;
474 /* If tuning has not been specified, tune for whichever processor or
475 architecture has been selected. */
476 if (tune_flags == 0)
477 tune_flags = insn_flags;
479 /* Make sure that the processor choice does not conflict with any of the
480 other command line choices. */
481 if (TARGET_APCS_32 && !(insn_flags & FL_MODE32))
483 /* If APCS-32 was not the default then it must have been set by the
484 user, so issue a warning message. If the user has specified
485 "-mapcs-32 -mcpu=arm2" then we loose here. */
486 if ((TARGET_DEFAULT & ARM_FLAG_APCS_32) == 0)
487 warning ("target CPU does not support APCS-32" );
488 target_flags &= ~ ARM_FLAG_APCS_32;
490 else if (! TARGET_APCS_32 && !(insn_flags & FL_MODE26))
492 warning ("target CPU does not support APCS-26" );
493 target_flags |= ARM_FLAG_APCS_32;
496 if (TARGET_INTERWORK && !(insn_flags & FL_THUMB))
498 warning ("target CPU does not support interworking" );
499 target_flags &= ~ARM_FLAG_INTERWORK;
502 if (TARGET_THUMB && !(insn_flags & FL_THUMB))
504 warning ("target CPU does not supoport THUMB instructions.");
505 target_flags &= ~ARM_FLAG_THUMB;
508 if (TARGET_APCS_FRAME && TARGET_THUMB)
510 /* warning ("ignoring -mapcs-frame because -mthumb was used."); */
511 target_flags &= ~ARM_FLAG_APCS_FRAME;
514 /* TARGET_BACKTRACE calls leaf_function_p, which causes a crash if done
515 from here where no function is being compiled currently. */
516 if ((target_flags & (THUMB_FLAG_LEAF_BACKTRACE | THUMB_FLAG_BACKTRACE))
517 && TARGET_ARM)
518 warning ("enabling backtrace support is only meaningful when compiling for the Thumb.");
520 if (TARGET_ARM && TARGET_CALLEE_INTERWORKING)
521 warning ("enabling callee interworking support is only meaningful when compiling for the Thumb.");
523 if (TARGET_ARM && TARGET_CALLER_INTERWORKING)
524 warning ("enabling caller interworking support is only meaningful when compiling for the Thumb.");
526 /* If interworking is enabled then APCS-32 must be selected as well. */
527 if (TARGET_INTERWORK)
529 if (! TARGET_APCS_32)
530 warning ("interworking forces APCS-32 to be used" );
531 target_flags |= ARM_FLAG_APCS_32;
534 if (TARGET_APCS_STACK && ! TARGET_APCS_FRAME)
536 warning ("-mapcs-stack-check incompatible with -mno-apcs-frame");
537 target_flags |= ARM_FLAG_APCS_FRAME;
540 if (TARGET_POKE_FUNCTION_NAME)
541 target_flags |= ARM_FLAG_APCS_FRAME;
543 if (TARGET_APCS_REENT && flag_pic)
544 fatal ("-fpic and -mapcs-reent are incompatible");
546 if (TARGET_APCS_REENT)
547 warning ("APCS reentrant code not supported. Ignored");
549 /* If this target is normally configured to use APCS frames, warn if they
550 are turned off and debugging is turned on. */
551 if (TARGET_ARM
552 && write_symbols != NO_DEBUG
553 && ! TARGET_APCS_FRAME
554 && (TARGET_DEFAULT & ARM_FLAG_APCS_FRAME))
555 warning ("-g with -mno-apcs-frame may not give sensible debugging");
557 /* If stack checking is disabled, we can use r10 as the PIC register,
558 which keeps r9 available. */
559 if (flag_pic && ! TARGET_APCS_STACK)
560 arm_pic_register = 10;
562 if (TARGET_APCS_FLOAT)
563 warning ("Passing floating point arguments in fp regs not yet supported");
565 /* Initialise boolean versions of the flags, for use in the arm.md file. */
566 arm_fast_multiply = (insn_flags & FL_FAST_MULT) != 0;
567 arm_arch4 = (insn_flags & FL_ARCH4) != 0;
568 arm_arch5 = (insn_flags & FL_ARCH5) != 0;
570 arm_ld_sched = (tune_flags & FL_LDSCHED) != 0;
571 arm_is_strong = (tune_flags & FL_STRONG) != 0;
572 arm_is_6_or_7 = (((tune_flags & (FL_MODE26 | FL_MODE32))
573 && !(tune_flags & FL_ARCH4))) != 0;
575 /* Default value for floating point code... if no co-processor
576 bus, then schedule for emulated floating point. Otherwise,
577 assume the user has an FPA.
578 Note: this does not prevent use of floating point instructions,
579 -msoft-float does that. */
580 arm_fpu = (tune_flags & FL_CO_PROC) ? FP_HARD : FP_SOFT3;
582 if (target_fp_name)
584 if (streq (target_fp_name, "2"))
585 arm_fpu_arch = FP_SOFT2;
586 else if (streq (target_fp_name, "3"))
587 arm_fpu_arch = FP_SOFT3;
588 else
589 fatal ("Invalid floating point emulation option: -mfpe-%s",
590 target_fp_name);
592 else
593 arm_fpu_arch = FP_DEFAULT;
595 if (TARGET_FPE && arm_fpu != FP_HARD)
596 arm_fpu = FP_SOFT2;
598 /* For arm2/3 there is no need to do any scheduling if there is only
599 a floating point emulator, or we are doing software floating-point. */
600 if ((TARGET_SOFT_FLOAT || arm_fpu != FP_HARD)
601 && (tune_flags & FL_MODE32) == 0)
602 flag_schedule_insns = flag_schedule_insns_after_reload = 0;
604 arm_prog_mode = TARGET_APCS_32 ? PROG_MODE_PROG32 : PROG_MODE_PROG26;
606 if (structure_size_string != NULL)
608 int size = strtol (structure_size_string, NULL, 0);
610 if (size == 8 || size == 32)
611 arm_structure_size_boundary = size;
612 else
613 warning ("Structure size boundary can only be set to 8 or 32");
616 if (arm_pic_register_string != NULL)
618 int pic_register;
620 if (! flag_pic)
621 warning ("-mpic-register= is useless without -fpic");
623 pic_register = decode_reg_name (arm_pic_register_string);
625 /* Prevent the user from choosing an obviously stupid PIC register. */
626 if (pic_register < 0 || call_used_regs[pic_register]
627 || pic_register == HARD_FRAME_POINTER_REGNUM
628 || pic_register == STACK_POINTER_REGNUM
629 || pic_register >= PC_REGNUM)
630 error ("Unable to use '%s' for PIC register", arm_pic_register_string);
631 else
632 arm_pic_register = pic_register;
635 if (TARGET_THUMB && flag_schedule_insns)
637 /* Don't warn since it's on by default in -O2. */
638 flag_schedule_insns = 0;
641 /* If optimizing for space, don't synthesize constants.
642 For processors with load scheduling, it never costs more than 2 cycles
643 to load a constant, and the load scheduler may well reduce that to 1. */
644 if (optimize_size || (tune_flags & FL_LDSCHED))
645 arm_constant_limit = 1;
647 /* If optimizing for size, bump the number of instructions that we
648 are prepared to conditionally execute (even on a StrongARM).
649 Otherwise for the StrongARM, which has early execution of branches,
650 a sequence that is worth skipping is shorter. */
651 if (optimize_size)
652 max_insns_skipped = 6;
653 else if (arm_is_strong)
654 max_insns_skipped = 3;
656 /* Register global variables with the garbage collector. */
657 arm_add_gc_roots ();
660 static void
661 arm_add_gc_roots ()
663 ggc_add_rtx_root (&arm_compare_op0, 1);
664 ggc_add_rtx_root (&arm_compare_op1, 1);
665 ggc_add_rtx_root (&arm_target_insn, 1); /* Not sure this is really a root */
666 /* XXX: What about the minipool tables? */
669 /* Return 1 if it is possible to return using a single instruction. */
671 use_return_insn (iscond)
672 int iscond;
674 int regno;
676 /* Never use a return instruction before reload has run. */
677 if (! reload_completed
678 /* Or if the function is variadic. */
679 || current_function_pretend_args_size
680 || current_function_anonymous_args
681 /* Of if the function calls __builtin_eh_return () */
682 || cfun->machine->eh_epilogue_sp_ofs != NULL
683 /* Or if there is no frame pointer and there is a stack adjustment. */
684 || ((get_frame_size () + current_function_outgoing_args_size != 0)
685 && ! frame_pointer_needed))
686 return 0;
688 /* Can't be done if interworking with Thumb, and any registers have been
689 stacked. Similarly, on StrongARM, conditional returns are expensive
690 if they aren't taken and registers have been stacked. */
691 if (iscond && arm_is_strong && frame_pointer_needed)
692 return 0;
694 if ((iscond && arm_is_strong)
695 || TARGET_INTERWORK)
697 for (regno = 0; regno <= LAST_ARM_REGNUM; regno++)
698 if (regs_ever_live[regno] && ! call_used_regs[regno])
699 return 0;
701 if (flag_pic && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
702 return 0;
705 /* Can't be done if any of the FPU regs are pushed, since this also
706 requires an insn. */
707 if (TARGET_HARD_FLOAT)
708 for (regno = FIRST_ARM_FP_REGNUM; regno <= LAST_ARM_FP_REGNUM; regno++)
709 if (regs_ever_live[regno] && ! call_used_regs[regno])
710 return 0;
712 /* If a function is naked, don't use the "return" insn. */
713 if (arm_naked_function_p (current_function_decl))
714 return 0;
716 return 1;
719 /* Return TRUE if int I is a valid immediate ARM constant. */
722 const_ok_for_arm (i)
723 HOST_WIDE_INT i;
725 unsigned HOST_WIDE_INT mask = ~(unsigned HOST_WIDE_INT)0xFF;
727 /* For machines with >32 bit HOST_WIDE_INT, the bits above bit 31 must
728 be all zero, or all one. */
729 if ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff) != 0
730 && ((i & ~(unsigned HOST_WIDE_INT) 0xffffffff)
731 != ((~(unsigned HOST_WIDE_INT) 0)
732 & ~(unsigned HOST_WIDE_INT) 0xffffffff)))
733 return FALSE;
735 /* Fast return for 0 and powers of 2 */
736 if ((i & (i - 1)) == 0)
737 return TRUE;
741 if ((i & mask & (unsigned HOST_WIDE_INT) 0xffffffff) == 0)
742 return TRUE;
743 mask =
744 (mask << 2) | ((mask & (unsigned HOST_WIDE_INT) 0xffffffff)
745 >> (32 - 2)) | ~((unsigned HOST_WIDE_INT) 0xffffffff);
746 } while (mask != ~(unsigned HOST_WIDE_INT) 0xFF);
748 return FALSE;
751 /* Return true if I is a valid constant for the operation CODE. */
752 static int
753 const_ok_for_op (i, code)
754 HOST_WIDE_INT i;
755 enum rtx_code code;
757 if (const_ok_for_arm (i))
758 return 1;
760 switch (code)
762 case PLUS:
763 return const_ok_for_arm (ARM_SIGN_EXTEND (-i));
765 case MINUS: /* Should only occur with (MINUS I reg) => rsb */
766 case XOR:
767 case IOR:
768 return 0;
770 case AND:
771 return const_ok_for_arm (ARM_SIGN_EXTEND (~i));
773 default:
774 abort ();
778 /* Emit a sequence of insns to handle a large constant.
779 CODE is the code of the operation required, it can be any of SET, PLUS,
780 IOR, AND, XOR, MINUS;
781 MODE is the mode in which the operation is being performed;
782 VAL is the integer to operate on;
783 SOURCE is the other operand (a register, or a null-pointer for SET);
784 SUBTARGETS means it is safe to create scratch registers if that will
785 either produce a simpler sequence, or we will want to cse the values.
786 Return value is the number of insns emitted. */
789 arm_split_constant (code, mode, val, target, source, subtargets)
790 enum rtx_code code;
791 enum machine_mode mode;
792 HOST_WIDE_INT val;
793 rtx target;
794 rtx source;
795 int subtargets;
797 if (subtargets || code == SET
798 || (GET_CODE (target) == REG && GET_CODE (source) == REG
799 && REGNO (target) != REGNO (source)))
801 /* After arm_reorg has been called, we can't fix up expensive
802 constants by pushing them into memory so we must synthesise
803 them in-line, regardless of the cost. This is only likely to
804 be more costly on chips that have load delay slots and we are
805 compiling without running the scheduler (so no splitting
806 occurred before the final instruction emission).
808 Ref: gcc -O1 -mcpu=strongarm gcc.c-torture/compile/980506-2.c
810 if (! after_arm_reorg
811 && (arm_gen_constant (code, mode, val, target, source, 1, 0)
812 > arm_constant_limit + (code != SET)))
814 if (code == SET)
816 /* Currently SET is the only monadic value for CODE, all
817 the rest are diadic. */
818 emit_insn (gen_rtx_SET (VOIDmode, target, GEN_INT (val)));
819 return 1;
821 else
823 rtx temp = subtargets ? gen_reg_rtx (mode) : target;
825 emit_insn (gen_rtx_SET (VOIDmode, temp, GEN_INT (val)));
826 /* For MINUS, the value is subtracted from, since we never
827 have subtraction of a constant. */
828 if (code == MINUS)
829 emit_insn (gen_rtx_SET (VOIDmode, target,
830 gen_rtx_MINUS (mode, temp, source)));
831 else
832 emit_insn (gen_rtx_SET (VOIDmode, target,
833 gen_rtx (code, mode, source, temp)));
834 return 2;
839 return arm_gen_constant (code, mode, val, target, source, subtargets, 1);
842 /* As above, but extra parameter GENERATE which, if clear, suppresses
843 RTL generation. */
844 static int
845 arm_gen_constant (code, mode, val, target, source, subtargets, generate)
846 enum rtx_code code;
847 enum machine_mode mode;
848 HOST_WIDE_INT val;
849 rtx target;
850 rtx source;
851 int subtargets;
852 int generate;
854 int can_invert = 0;
855 int can_negate = 0;
856 int can_negate_initial = 0;
857 int can_shift = 0;
858 int i;
859 int num_bits_set = 0;
860 int set_sign_bit_copies = 0;
861 int clear_sign_bit_copies = 0;
862 int clear_zero_bit_copies = 0;
863 int set_zero_bit_copies = 0;
864 int insns = 0;
865 unsigned HOST_WIDE_INT temp1, temp2;
866 unsigned HOST_WIDE_INT remainder = val & (unsigned HOST_WIDE_INT)0xffffffff;
868 /* Find out which operations are safe for a given CODE. Also do a quick
869 check for degenerate cases; these can occur when DImode operations
870 are split. */
871 switch (code)
873 case SET:
874 can_invert = 1;
875 can_shift = 1;
876 can_negate = 1;
877 break;
879 case PLUS:
880 can_negate = 1;
881 can_negate_initial = 1;
882 break;
884 case IOR:
885 if (remainder == (unsigned HOST_WIDE_INT)0xffffffff)
887 if (generate)
888 emit_insn (gen_rtx_SET (VOIDmode, target,
889 GEN_INT (ARM_SIGN_EXTEND (val))));
890 return 1;
892 if (remainder == 0)
894 if (reload_completed && rtx_equal_p (target, source))
895 return 0;
896 if (generate)
897 emit_insn (gen_rtx_SET (VOIDmode, target, source));
898 return 1;
900 break;
902 case AND:
903 if (remainder == 0)
905 if (generate)
906 emit_insn (gen_rtx_SET (VOIDmode, target, const0_rtx));
907 return 1;
909 if (remainder == (unsigned HOST_WIDE_INT)0xffffffff)
911 if (reload_completed && rtx_equal_p (target, source))
912 return 0;
913 if (generate)
914 emit_insn (gen_rtx_SET (VOIDmode, target, source));
915 return 1;
917 can_invert = 1;
918 break;
920 case XOR:
921 if (remainder == 0)
923 if (reload_completed && rtx_equal_p (target, source))
924 return 0;
925 if (generate)
926 emit_insn (gen_rtx_SET (VOIDmode, target, source));
927 return 1;
929 if (remainder == (unsigned HOST_WIDE_INT)0xffffffff)
931 if (generate)
932 emit_insn (gen_rtx_SET (VOIDmode, target,
933 gen_rtx_NOT (mode, source)));
934 return 1;
937 /* We don't know how to handle this yet below. */
938 abort ();
940 case MINUS:
941 /* We treat MINUS as (val - source), since (source - val) is always
942 passed as (source + (-val)). */
943 if (remainder == 0)
945 if (generate)
946 emit_insn (gen_rtx_SET (VOIDmode, target,
947 gen_rtx_NEG (mode, source)));
948 return 1;
950 if (const_ok_for_arm (val))
952 if (generate)
953 emit_insn (gen_rtx_SET (VOIDmode, target,
954 gen_rtx_MINUS (mode, GEN_INT (val),
955 source)));
956 return 1;
958 can_negate = 1;
960 break;
962 default:
963 abort ();
966 /* If we can do it in one insn get out quickly. */
967 if (const_ok_for_arm (val)
968 || (can_negate_initial && const_ok_for_arm (-val))
969 || (can_invert && const_ok_for_arm (~val)))
971 if (generate)
972 emit_insn (gen_rtx_SET (VOIDmode, target,
973 (source ? gen_rtx (code, mode, source,
974 GEN_INT (val))
975 : GEN_INT (val))));
976 return 1;
979 /* Calculate a few attributes that may be useful for specific
980 optimizations. */
981 for (i = 31; i >= 0; i--)
983 if ((remainder & (1 << i)) == 0)
984 clear_sign_bit_copies++;
985 else
986 break;
989 for (i = 31; i >= 0; i--)
991 if ((remainder & (1 << i)) != 0)
992 set_sign_bit_copies++;
993 else
994 break;
997 for (i = 0; i <= 31; i++)
999 if ((remainder & (1 << i)) == 0)
1000 clear_zero_bit_copies++;
1001 else
1002 break;
1005 for (i = 0; i <= 31; i++)
1007 if ((remainder & (1 << i)) != 0)
1008 set_zero_bit_copies++;
1009 else
1010 break;
1013 switch (code)
1015 case SET:
1016 /* See if we can do this by sign_extending a constant that is known
1017 to be negative. This is a good, way of doing it, since the shift
1018 may well merge into a subsequent insn. */
1019 if (set_sign_bit_copies > 1)
1021 if (const_ok_for_arm
1022 (temp1 = ARM_SIGN_EXTEND (remainder
1023 << (set_sign_bit_copies - 1))))
1025 if (generate)
1027 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1028 emit_insn (gen_rtx_SET (VOIDmode, new_src,
1029 GEN_INT (temp1)));
1030 emit_insn (gen_ashrsi3 (target, new_src,
1031 GEN_INT (set_sign_bit_copies - 1)));
1033 return 2;
1035 /* For an inverted constant, we will need to set the low bits,
1036 these will be shifted out of harm's way. */
1037 temp1 |= (1 << (set_sign_bit_copies - 1)) - 1;
1038 if (const_ok_for_arm (~temp1))
1040 if (generate)
1042 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1043 emit_insn (gen_rtx_SET (VOIDmode, new_src,
1044 GEN_INT (temp1)));
1045 emit_insn (gen_ashrsi3 (target, new_src,
1046 GEN_INT (set_sign_bit_copies - 1)));
1048 return 2;
1052 /* See if we can generate this by setting the bottom (or the top)
1053 16 bits, and then shifting these into the other half of the
1054 word. We only look for the simplest cases, to do more would cost
1055 too much. Be careful, however, not to generate this when the
1056 alternative would take fewer insns. */
1057 if (val & (unsigned HOST_WIDE_INT)0xffff0000)
1059 temp1 = remainder & (unsigned HOST_WIDE_INT)0xffff0000;
1060 temp2 = remainder & 0x0000ffff;
1062 /* Overlaps outside this range are best done using other methods. */
1063 for (i = 9; i < 24; i++)
1065 if ((((temp2 | (temp2 << i))
1066 & (unsigned HOST_WIDE_INT)0xffffffff) == remainder)
1067 && ! const_ok_for_arm (temp2))
1069 rtx new_src = (subtargets
1070 ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
1071 : target);
1072 insns = arm_gen_constant (code, mode, temp2, new_src,
1073 source, subtargets, generate);
1074 source = new_src;
1075 if (generate)
1076 emit_insn (gen_rtx_SET
1077 (VOIDmode, target,
1078 gen_rtx_IOR (mode,
1079 gen_rtx_ASHIFT (mode, source,
1080 GEN_INT (i)),
1081 source)));
1082 return insns + 1;
1086 /* Don't duplicate cases already considered. */
1087 for (i = 17; i < 24; i++)
1089 if (((temp1 | (temp1 >> i)) == remainder)
1090 && ! const_ok_for_arm (temp1))
1092 rtx new_src = (subtargets
1093 ? (generate ? gen_reg_rtx (mode) : NULL_RTX)
1094 : target);
1095 insns = arm_gen_constant (code, mode, temp1, new_src,
1096 source, subtargets, generate);
1097 source = new_src;
1098 if (generate)
1099 emit_insn
1100 (gen_rtx_SET (VOIDmode, target,
1101 gen_rtx_IOR
1102 (mode,
1103 gen_rtx_LSHIFTRT (mode, source,
1104 GEN_INT (i)),
1105 source)));
1106 return insns + 1;
1110 break;
1112 case IOR:
1113 case XOR:
1114 /* If we have IOR or XOR, and the constant can be loaded in a
1115 single instruction, and we can find a temporary to put it in,
1116 then this can be done in two instructions instead of 3-4. */
1117 if (subtargets
1118 /* TARGET can't be NULL if SUBTARGETS is 0 */
1119 || (reload_completed && ! reg_mentioned_p (target, source)))
1121 if (const_ok_for_arm (ARM_SIGN_EXTEND (~ val)))
1123 if (generate)
1125 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1127 emit_insn (gen_rtx_SET (VOIDmode, sub, GEN_INT (val)));
1128 emit_insn (gen_rtx_SET (VOIDmode, target,
1129 gen_rtx (code, mode, source, sub)));
1131 return 2;
1135 if (code == XOR)
1136 break;
1138 if (set_sign_bit_copies > 8
1139 && (val & (-1 << (32 - set_sign_bit_copies))) == val)
1141 if (generate)
1143 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1144 rtx shift = GEN_INT (set_sign_bit_copies);
1146 emit_insn (gen_rtx_SET (VOIDmode, sub,
1147 gen_rtx_NOT (mode,
1148 gen_rtx_ASHIFT (mode,
1149 source,
1150 shift))));
1151 emit_insn (gen_rtx_SET (VOIDmode, target,
1152 gen_rtx_NOT (mode,
1153 gen_rtx_LSHIFTRT (mode, sub,
1154 shift))));
1156 return 2;
1159 if (set_zero_bit_copies > 8
1160 && (remainder & ((1 << set_zero_bit_copies) - 1)) == remainder)
1162 if (generate)
1164 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1165 rtx shift = GEN_INT (set_zero_bit_copies);
1167 emit_insn (gen_rtx_SET (VOIDmode, sub,
1168 gen_rtx_NOT (mode,
1169 gen_rtx_LSHIFTRT (mode,
1170 source,
1171 shift))));
1172 emit_insn (gen_rtx_SET (VOIDmode, target,
1173 gen_rtx_NOT (mode,
1174 gen_rtx_ASHIFT (mode, sub,
1175 shift))));
1177 return 2;
1180 if (const_ok_for_arm (temp1 = ARM_SIGN_EXTEND (~ val)))
1182 if (generate)
1184 rtx sub = subtargets ? gen_reg_rtx (mode) : target;
1185 emit_insn (gen_rtx_SET (VOIDmode, sub,
1186 gen_rtx_NOT (mode, source)));
1187 source = sub;
1188 if (subtargets)
1189 sub = gen_reg_rtx (mode);
1190 emit_insn (gen_rtx_SET (VOIDmode, sub,
1191 gen_rtx_AND (mode, source,
1192 GEN_INT (temp1))));
1193 emit_insn (gen_rtx_SET (VOIDmode, target,
1194 gen_rtx_NOT (mode, sub)));
1196 return 3;
1198 break;
1200 case AND:
1201 /* See if two shifts will do 2 or more insn's worth of work. */
1202 if (clear_sign_bit_copies >= 16 && clear_sign_bit_copies < 24)
1204 HOST_WIDE_INT shift_mask = ((((unsigned HOST_WIDE_INT)0xffffffff)
1205 << (32 - clear_sign_bit_copies))
1206 & (unsigned HOST_WIDE_INT)0xffffffff);
1208 if ((remainder | shift_mask) != (unsigned HOST_WIDE_INT)0xffffffff)
1210 if (generate)
1212 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1213 insns = arm_gen_constant (AND, mode, remainder | shift_mask,
1214 new_src, source, subtargets, 1);
1215 source = new_src;
1217 else
1219 rtx targ = subtargets ? NULL_RTX : target;
1220 insns = arm_gen_constant (AND, mode, remainder | shift_mask,
1221 targ, source, subtargets, 0);
1225 if (generate)
1227 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1228 rtx shift = GEN_INT (clear_sign_bit_copies);
1230 emit_insn (gen_ashlsi3 (new_src, source, shift));
1231 emit_insn (gen_lshrsi3 (target, new_src, shift));
1234 return insns + 2;
1237 if (clear_zero_bit_copies >= 16 && clear_zero_bit_copies < 24)
1239 HOST_WIDE_INT shift_mask = (1 << clear_zero_bit_copies) - 1;
1241 if ((remainder | shift_mask) != (unsigned HOST_WIDE_INT)0xffffffff)
1243 if (generate)
1245 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1247 insns = arm_gen_constant (AND, mode, remainder | shift_mask,
1248 new_src, source, subtargets, 1);
1249 source = new_src;
1251 else
1253 rtx targ = subtargets ? NULL_RTX : target;
1255 insns = arm_gen_constant (AND, mode, remainder | shift_mask,
1256 targ, source, subtargets, 0);
1260 if (generate)
1262 rtx new_src = subtargets ? gen_reg_rtx (mode) : target;
1263 rtx shift = GEN_INT (clear_zero_bit_copies);
1265 emit_insn (gen_lshrsi3 (new_src, source, shift));
1266 emit_insn (gen_ashlsi3 (target, new_src, shift));
1269 return insns + 2;
1272 break;
1274 default:
1275 break;
1278 for (i = 0; i < 32; i++)
1279 if (remainder & (1 << i))
1280 num_bits_set++;
1282 if (code == AND || (can_invert && num_bits_set > 16))
1283 remainder = (~remainder) & (unsigned HOST_WIDE_INT)0xffffffff;
1284 else if (code == PLUS && num_bits_set > 16)
1285 remainder = (-remainder) & (unsigned HOST_WIDE_INT)0xffffffff;
1286 else
1288 can_invert = 0;
1289 can_negate = 0;
1292 /* Now try and find a way of doing the job in either two or three
1293 instructions.
1294 We start by looking for the largest block of zeros that are aligned on
1295 a 2-bit boundary, we then fill up the temps, wrapping around to the
1296 top of the word when we drop off the bottom.
1297 In the worst case this code should produce no more than four insns. */
1299 int best_start = 0;
1300 int best_consecutive_zeros = 0;
1302 for (i = 0; i < 32; i += 2)
1304 int consecutive_zeros = 0;
1306 if (! (remainder & (3 << i)))
1308 while ((i < 32) && ! (remainder & (3 << i)))
1310 consecutive_zeros += 2;
1311 i += 2;
1313 if (consecutive_zeros > best_consecutive_zeros)
1315 best_consecutive_zeros = consecutive_zeros;
1316 best_start = i - consecutive_zeros;
1318 i -= 2;
1322 /* Now start emitting the insns, starting with the one with the highest
1323 bit set: we do this so that the smallest number will be emitted last;
1324 this is more likely to be combinable with addressing insns. */
1325 i = best_start;
1328 int end;
1330 if (i <= 0)
1331 i += 32;
1332 if (remainder & (3 << (i - 2)))
1334 end = i - 8;
1335 if (end < 0)
1336 end += 32;
1337 temp1 = remainder & ((0x0ff << end)
1338 | ((i < end) ? (0xff >> (32 - end)) : 0));
1339 remainder &= ~temp1;
1341 if (generate)
1343 rtx new_src;
1345 if (code == SET)
1346 emit_insn (gen_rtx_SET (VOIDmode,
1347 new_src = (subtargets
1348 ? gen_reg_rtx (mode)
1349 : target),
1350 GEN_INT (can_invert
1351 ? ~temp1 : temp1)));
1352 else if (code == MINUS)
1353 emit_insn (gen_rtx_SET (VOIDmode,
1354 new_src = (subtargets
1355 ? gen_reg_rtx (mode)
1356 : target),
1357 gen_rtx (code, mode, GEN_INT (temp1),
1358 source)));
1359 else
1360 emit_insn (gen_rtx_SET (VOIDmode,
1361 new_src = (remainder
1362 ? (subtargets
1363 ? gen_reg_rtx (mode)
1364 : target)
1365 : target),
1366 gen_rtx (code, mode, source,
1367 GEN_INT (can_invert ? ~temp1
1368 : (can_negate
1369 ? -temp1
1370 : temp1)))));
1371 source = new_src;
1374 if (code == SET)
1376 can_invert = 0;
1377 code = PLUS;
1379 else if (code == MINUS)
1380 code = PLUS;
1382 insns++;
1383 i -= 6;
1385 i -= 2;
1386 } while (remainder);
1388 return insns;
1391 /* Canonicalize a comparison so that we are more likely to recognize it.
1392 This can be done for a few constant compares, where we can make the
1393 immediate value easier to load. */
1394 enum rtx_code
1395 arm_canonicalize_comparison (code, op1)
1396 enum rtx_code code;
1397 rtx * op1;
1399 unsigned HOST_WIDE_INT i = INTVAL (*op1);
1401 switch (code)
1403 case EQ:
1404 case NE:
1405 return code;
1407 case GT:
1408 case LE:
1409 if (i != ((((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1))
1410 - 1)
1411 && (const_ok_for_arm (i+1) || const_ok_for_arm (- (i+1))))
1413 *op1 = GEN_INT (i+1);
1414 return code == GT ? GE : LT;
1416 break;
1418 case GE:
1419 case LT:
1420 if (i != (((unsigned HOST_WIDE_INT) 1) << (HOST_BITS_PER_WIDE_INT - 1))
1421 && (const_ok_for_arm (i-1) || const_ok_for_arm (- (i-1))))
1423 *op1 = GEN_INT (i-1);
1424 return code == GE ? GT : LE;
1426 break;
1428 case GTU:
1429 case LEU:
1430 if (i != ~((unsigned HOST_WIDE_INT) 0)
1431 && (const_ok_for_arm (i+1) || const_ok_for_arm (- (i+1))))
1433 *op1 = GEN_INT (i + 1);
1434 return code == GTU ? GEU : LTU;
1436 break;
1438 case GEU:
1439 case LTU:
1440 if (i != 0
1441 && (const_ok_for_arm (i - 1) || const_ok_for_arm (- (i - 1))))
1443 *op1 = GEN_INT (i - 1);
1444 return code == GEU ? GTU : LEU;
1446 break;
1448 default:
1449 abort ();
1452 return code;
1455 /* Decide whether a type should be returned in memory (true)
1456 or in a register (false). This is called by the macro
1457 RETURN_IN_MEMORY. */
1459 arm_return_in_memory (type)
1460 tree type;
1462 if (! AGGREGATE_TYPE_P (type))
1463 /* All simple types are returned in registers. */
1464 return 0;
1466 /* For the arm-wince targets we choose to be compitable with Microsoft's
1467 ARM and Thumb compilers, which always return aggregates in memory. */
1468 #ifndef ARM_WINCE
1470 if (int_size_in_bytes (type) > 4)
1471 /* All structures/unions bigger than one word are returned in memory. */
1472 return 1;
1474 if (TREE_CODE (type) == RECORD_TYPE)
1476 tree field;
1478 /* For a struct the APCS says that we only return in a register
1479 if the type is 'integer like' and every addressable element
1480 has an offset of zero. For practical purposes this means
1481 that the structure can have at most one non bit-field element
1482 and that this element must be the first one in the structure. */
1484 /* Find the first field, ignoring non FIELD_DECL things which will
1485 have been created by C++. */
1486 for (field = TYPE_FIELDS (type);
1487 field && TREE_CODE (field) != FIELD_DECL;
1488 field = TREE_CHAIN (field))
1489 continue;
1491 if (field == NULL)
1492 return 0; /* An empty structure. Allowed by an extension to ANSI C. */
1494 /* Check that the first field is valid for returning in a register. */
1496 /* ... Floats are not allowed */
1497 if (FLOAT_TYPE_P (TREE_TYPE (field)))
1498 return 1;
1500 /* ... Aggregates that are not themselves valid for returning in
1501 a register are not allowed. */
1502 if (RETURN_IN_MEMORY (TREE_TYPE (field)))
1503 return 1;
1505 /* Now check the remaining fields, if any. Only bitfields are allowed,
1506 since they are not addressable. */
1507 for (field = TREE_CHAIN (field);
1508 field;
1509 field = TREE_CHAIN (field))
1511 if (TREE_CODE (field) != FIELD_DECL)
1512 continue;
1514 if (! DECL_BIT_FIELD_TYPE (field))
1515 return 1;
1518 return 0;
1521 if (TREE_CODE (type) == UNION_TYPE)
1523 tree field;
1525 /* Unions can be returned in registers if every element is
1526 integral, or can be returned in an integer register. */
1527 for (field = TYPE_FIELDS (type);
1528 field;
1529 field = TREE_CHAIN (field))
1531 if (TREE_CODE (field) != FIELD_DECL)
1532 continue;
1534 if (FLOAT_TYPE_P (TREE_TYPE (field)))
1535 return 1;
1537 if (RETURN_IN_MEMORY (TREE_TYPE (field)))
1538 return 1;
1541 return 0;
1543 #endif /* not ARM_WINCE */
1545 /* Return all other types in memory. */
1546 return 1;
1549 /* Initialize a variable CUM of type CUMULATIVE_ARGS
1550 for a call to a function whose data type is FNTYPE.
1551 For a library call, FNTYPE is NULL. */
1552 void
1553 arm_init_cumulative_args (pcum, fntype, libname, indirect)
1554 CUMULATIVE_ARGS * pcum;
1555 tree fntype;
1556 rtx libname ATTRIBUTE_UNUSED;
1557 int indirect ATTRIBUTE_UNUSED;
1559 /* On the ARM, the offset starts at 0. */
1560 pcum->nregs = ((fntype && aggregate_value_p (TREE_TYPE (fntype))) ? 1 : 0);
1562 pcum->call_cookie = CALL_NORMAL;
1564 if (TARGET_LONG_CALLS)
1565 pcum->call_cookie = CALL_LONG;
1567 /* Check for long call/short call attributes. The attributes
1568 override any command line option. */
1569 if (fntype)
1571 if (lookup_attribute ("short_call", TYPE_ATTRIBUTES (fntype)))
1572 pcum->call_cookie = CALL_SHORT;
1573 else if (lookup_attribute ("long_call", TYPE_ATTRIBUTES (fntype)))
1574 pcum->call_cookie = CALL_LONG;
1578 /* Determine where to put an argument to a function.
1579 Value is zero to push the argument on the stack,
1580 or a hard register in which to store the argument.
1582 MODE is the argument's machine mode.
1583 TYPE is the data type of the argument (as a tree).
1584 This is null for libcalls where that information may
1585 not be available.
1586 CUM is a variable of type CUMULATIVE_ARGS which gives info about
1587 the preceding args and about the function being called.
1588 NAMED is nonzero if this argument is a named parameter
1589 (otherwise it is an extra parameter matching an ellipsis). */
1591 arm_function_arg (pcum, mode, type, named)
1592 CUMULATIVE_ARGS * pcum;
1593 enum machine_mode mode;
1594 tree type ATTRIBUTE_UNUSED;
1595 int named;
1597 if (mode == VOIDmode)
1598 /* Compute operand 2 of the call insn. */
1599 return GEN_INT (pcum->call_cookie);
1601 if (! named || pcum->nregs >= NUM_ARG_REGS)
1602 return NULL_RTX;
1604 return gen_rtx_REG (mode, pcum->nregs);
1607 /* Encode the current state of the #pragma [no_]long_calls. */
1608 typedef enum
1610 OFF, /* No #pramgma [no_]long_calls is in effect. */
1611 LONG, /* #pragma long_calls is in effect. */
1612 SHORT /* #pragma no_long_calls is in effect. */
1613 } arm_pragma_enum;
1615 static arm_pragma_enum arm_pragma_long_calls = OFF;
1617 /* Handle pragmas for compatibility with Intel's compilers.
1618 FIXME: This is incomplete, since it does not handle all
1619 the pragmas that the Intel compilers understand. */
1621 arm_process_pragma (p_getc, p_ungetc, pname)
1622 int (* p_getc) PARAMS ((void)) ATTRIBUTE_UNUSED;
1623 void (* p_ungetc) PARAMS ((int)) ATTRIBUTE_UNUSED;
1624 char * pname;
1626 /* Should be pragma 'far' or equivalent for callx/balx here. */
1627 if (strcmp (pname, "long_calls") == 0)
1628 arm_pragma_long_calls = LONG;
1629 else if (strcmp (pname, "no_long_calls") == 0)
1630 arm_pragma_long_calls = SHORT;
1631 else if (strcmp (pname, "long_calls_off") == 0)
1632 arm_pragma_long_calls = OFF;
1633 else
1634 return 0;
1636 return 1;
1639 /* Return nonzero if IDENTIFIER with arguments ARGS is a valid machine specific
1640 attribute for TYPE. The attributes in ATTRIBUTES have previously been
1641 assigned to TYPE. */
1643 arm_valid_type_attribute_p (type, attributes, identifier, args)
1644 tree type;
1645 tree attributes ATTRIBUTE_UNUSED;
1646 tree identifier;
1647 tree args;
1649 if ( TREE_CODE (type) != FUNCTION_TYPE
1650 && TREE_CODE (type) != METHOD_TYPE
1651 && TREE_CODE (type) != FIELD_DECL
1652 && TREE_CODE (type) != TYPE_DECL)
1653 return 0;
1655 /* Function calls made to this symbol must be done indirectly, because
1656 it may lie outside of the 26 bit addressing range of a normal function
1657 call. */
1658 if (is_attribute_p ("long_call", identifier))
1659 return (args == NULL_TREE);
1661 /* Whereas these functions are always known to reside within the 26 bit
1662 addressing range. */
1663 if (is_attribute_p ("short_call", identifier))
1664 return (args == NULL_TREE);
1666 return 0;
1669 /* Return 0 if the attributes for two types are incompatible, 1 if they
1670 are compatible, and 2 if they are nearly compatible (which causes a
1671 warning to be generated). */
1673 arm_comp_type_attributes (type1, type2)
1674 tree type1;
1675 tree type2;
1677 int l1, l2, s1, s2;
1679 /* Check for mismatch of non-default calling convention. */
1680 if (TREE_CODE (type1) != FUNCTION_TYPE)
1681 return 1;
1683 /* Check for mismatched call attributes. */
1684 l1 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type1)) != NULL;
1685 l2 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type2)) != NULL;
1686 s1 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type1)) != NULL;
1687 s2 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type2)) != NULL;
1689 /* Only bother to check if an attribute is defined. */
1690 if (l1 | l2 | s1 | s2)
1692 /* If one type has an attribute, the other must have the same attribute. */
1693 if ((l1 != l2) || (s1 != s2))
1694 return 0;
1696 /* Disallow mixed attributes. */
1697 if ((l1 & s2) || (l2 & s1))
1698 return 0;
1701 return 1;
1704 /* Encode long_call or short_call attribute by prefixing
1705 symbol name in DECL with a special character FLAG. */
1706 void
1707 arm_encode_call_attribute (decl, flag)
1708 tree decl;
1709 char flag;
1711 const char * str = XSTR (XEXP (DECL_RTL (decl), 0), 0);
1712 int len = strlen (str);
1713 char * newstr;
1715 if (TREE_CODE (decl) != FUNCTION_DECL)
1716 return;
1718 /* Do not allow weak functions to be treated as short call. */
1719 if (DECL_WEAK (decl) && flag == SHORT_CALL_FLAG_CHAR)
1720 return;
1722 if (ggc_p)
1723 newstr = ggc_alloc_string (NULL, len + 2);
1724 else
1725 newstr = permalloc (len + 2);
1727 sprintf (newstr, "%c%s", flag, str);
1729 XSTR (XEXP (DECL_RTL (decl), 0), 0) = newstr;
1732 /* Assigns default attributes to newly defined type. This is used to
1733 set short_call/long_call attributes for function types of
1734 functions defined inside corresponding #pragma scopes. */
1735 void
1736 arm_set_default_type_attributes (type)
1737 tree type;
1739 /* Add __attribute__ ((long_call)) to all functions, when
1740 inside #pragma long_calls or __attribute__ ((short_call)),
1741 when inside #pragma no_long_calls. */
1742 if (TREE_CODE (type) == FUNCTION_TYPE || TREE_CODE (type) == METHOD_TYPE)
1744 tree type_attr_list, attr_name;
1745 type_attr_list = TYPE_ATTRIBUTES (type);
1747 if (arm_pragma_long_calls == LONG)
1748 attr_name = get_identifier ("long_call");
1749 else if (arm_pragma_long_calls == SHORT)
1750 attr_name = get_identifier ("short_call");
1751 else
1752 return;
1754 type_attr_list = tree_cons (attr_name, NULL_TREE, type_attr_list);
1755 TYPE_ATTRIBUTES (type) = type_attr_list;
1759 /* Return 1 if the operand is a SYMBOL_REF for a function known to be
1760 defined within the current compilation unit. If this caanot be
1761 determined, then 0 is returned. */
1762 static int
1763 current_file_function_operand (sym_ref)
1764 rtx sym_ref;
1766 /* This is a bit of a fib. A function will have a short call flag
1767 applied to its name if it has the short call attribute, or it has
1768 already been defined within the current compilation unit. */
1769 if (ENCODED_SHORT_CALL_ATTR_P (XSTR (sym_ref, 0)))
1770 return 1;
1772 /* The current funciton is always defined within the current compilation
1773 unit. if it s a weak defintion however, then this may not be the real
1774 defintion of the function, and so we have to say no. */
1775 if (sym_ref == XEXP (DECL_RTL (current_function_decl), 0)
1776 && ! DECL_WEAK (current_function_decl))
1777 return 1;
1779 /* We cannot make the determination - default to returning 0. */
1780 return 0;
1783 /* Return non-zero if a 32 bit "long_call" should be generated for
1784 this call. We generate a long_call if the function:
1786 a. has an __attribute__((long call))
1787 or b. is within the scope of a #pragma long_calls
1788 or c. the -mlong-calls command line switch has been specified
1790 However we do not generate a long call if the function:
1792 d. has an __attribute__ ((short_call))
1793 or e. is inside the scope of a #pragma no_long_calls
1794 or f. has an __attribute__ ((section))
1795 or g. is defined within the current compilation unit.
1797 This function will be called by C fragments contained in the machine
1798 description file. CALL_REF and CALL_COOKIE correspond to the matched
1799 rtl operands. CALL_SYMBOL is used to distinguish between
1800 two different callers of the function. It is set to 1 in the
1801 "call_symbol" and "call_symbol_value" patterns and to 0 in the "call"
1802 and "call_value" patterns. This is because of the difference in the
1803 SYM_REFs passed by these patterns. */
1805 arm_is_longcall_p (sym_ref, call_cookie, call_symbol)
1806 rtx sym_ref;
1807 int call_cookie;
1808 int call_symbol;
1810 if (! call_symbol)
1812 if (GET_CODE (sym_ref) != MEM)
1813 return 0;
1815 sym_ref = XEXP (sym_ref, 0);
1818 if (GET_CODE (sym_ref) != SYMBOL_REF)
1819 return 0;
1821 if (call_cookie & CALL_SHORT)
1822 return 0;
1824 if (TARGET_LONG_CALLS && flag_function_sections)
1825 return 1;
1827 if (current_file_function_operand (sym_ref, VOIDmode))
1828 return 0;
1830 return (call_cookie & CALL_LONG)
1831 || ENCODED_LONG_CALL_ATTR_P (XSTR (sym_ref, 0))
1832 || TARGET_LONG_CALLS;
1836 legitimate_pic_operand_p (x)
1837 rtx x;
1839 if (CONSTANT_P (x)
1840 && flag_pic
1841 && (GET_CODE (x) == SYMBOL_REF
1842 || (GET_CODE (x) == CONST
1843 && GET_CODE (XEXP (x, 0)) == PLUS
1844 && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF)))
1845 return 0;
1847 return 1;
1851 legitimize_pic_address (orig, mode, reg)
1852 rtx orig;
1853 enum machine_mode mode;
1854 rtx reg;
1856 if (GET_CODE (orig) == SYMBOL_REF)
1858 rtx pic_ref, address;
1859 rtx insn;
1860 int subregs = 0;
1862 if (reg == 0)
1864 if (reload_in_progress || reload_completed)
1865 abort ();
1866 else
1867 reg = gen_reg_rtx (Pmode);
1869 subregs = 1;
1872 #ifdef AOF_ASSEMBLER
1873 /* The AOF assembler can generate relocations for these directly, and
1874 understands that the PIC register has to be added into the offset. */
1875 insn = emit_insn (gen_pic_load_addr_based (reg, orig));
1876 #else
1877 if (subregs)
1878 address = gen_reg_rtx (Pmode);
1879 else
1880 address = reg;
1882 emit_insn (gen_pic_load_addr (address, orig));
1884 pic_ref = gen_rtx_MEM (Pmode,
1885 gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
1886 address));
1887 RTX_UNCHANGING_P (pic_ref) = 1;
1888 insn = emit_move_insn (reg, pic_ref);
1889 #endif
1890 current_function_uses_pic_offset_table = 1;
1891 /* Put a REG_EQUAL note on this insn, so that it can be optimized
1892 by loop. */
1893 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUAL, orig,
1894 REG_NOTES (insn));
1895 return reg;
1897 else if (GET_CODE (orig) == CONST)
1899 rtx base, offset;
1901 if (GET_CODE (XEXP (orig, 0)) == PLUS
1902 && XEXP (XEXP (orig, 0), 0) == pic_offset_table_rtx)
1903 return orig;
1905 if (reg == 0)
1907 if (reload_in_progress || reload_completed)
1908 abort ();
1909 else
1910 reg = gen_reg_rtx (Pmode);
1913 if (GET_CODE (XEXP (orig, 0)) == PLUS)
1915 base = legitimize_pic_address (XEXP (XEXP (orig, 0), 0), Pmode, reg);
1916 offset = legitimize_pic_address (XEXP (XEXP (orig, 0), 1), Pmode,
1917 base == reg ? 0 : reg);
1919 else
1920 abort ();
1922 if (GET_CODE (offset) == CONST_INT)
1924 /* The base register doesn't really matter, we only want to
1925 test the index for the appropriate mode. */
1926 GO_IF_LEGITIMATE_INDEX (mode, 0, offset, win);
1928 if (! reload_in_progress && ! reload_completed)
1929 offset = force_reg (Pmode, offset);
1930 else
1931 abort ();
1933 win:
1934 if (GET_CODE (offset) == CONST_INT)
1935 return plus_constant_for_output (base, INTVAL (offset));
1938 if (GET_MODE_SIZE (mode) > 4
1939 && (GET_MODE_CLASS (mode) == MODE_INT
1940 || TARGET_SOFT_FLOAT))
1942 emit_insn (gen_addsi3 (reg, base, offset));
1943 return reg;
1946 return gen_rtx_PLUS (Pmode, base, offset);
1948 else if (GET_CODE (orig) == LABEL_REF)
1950 current_function_uses_pic_offset_table = 1;
1952 if (NEED_GOT_RELOC)
1954 rtx pic_ref, address = gen_reg_rtx (Pmode);
1956 emit_insn (gen_pic_load_addr (address, orig));
1957 pic_ref = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, address);
1959 emit_move_insn (address, pic_ref);
1960 return address;
1964 return orig;
1967 static rtx pic_rtx;
1970 is_pic (x)
1971 rtx x;
1973 if (x == pic_rtx)
1974 return 1;
1975 return 0;
1978 void
1979 arm_finalize_pic ()
1981 #ifndef AOF_ASSEMBLER
1982 rtx l1, pic_tmp, pic_tmp2, seq;
1983 rtx global_offset_table;
1985 if (current_function_uses_pic_offset_table == 0 || TARGET_SINGLE_PIC_BASE)
1986 return;
1988 if (! flag_pic)
1989 abort ();
1991 start_sequence ();
1992 l1 = gen_label_rtx ();
1994 global_offset_table = gen_rtx_SYMBOL_REF (Pmode, "_GLOBAL_OFFSET_TABLE_");
1995 /* On the ARM the PC register contains 'dot + 8' at the time of the
1996 addition, on the Thumb it is 'dot + 4'. */
1997 pic_tmp = plus_constant (gen_rtx_LABEL_REF (Pmode, l1), TARGET_ARM ? 8 : 4);
1998 if (GOT_PCREL)
1999 pic_tmp2 = gen_rtx_CONST (VOIDmode,
2000 gen_rtx_PLUS (Pmode, global_offset_table, pc_rtx));
2001 else
2002 pic_tmp2 = gen_rtx_CONST (VOIDmode, global_offset_table);
2004 pic_rtx = gen_rtx_CONST (Pmode, gen_rtx_MINUS (Pmode, pic_tmp2, pic_tmp));
2006 emit_insn (gen_pic_load_addr (pic_offset_table_rtx, pic_rtx));
2007 if (TARGET_ARM)
2008 emit_insn (gen_pic_add_dot_plus_eight (pic_offset_table_rtx, l1));
2009 else
2010 emit_insn (gen_pic_add_dot_plus_four (pic_offset_table_rtx, l1));
2012 seq = gen_sequence ();
2013 end_sequence ();
2014 emit_insn_after (seq, get_insns ());
2016 /* Need to emit this whether or not we obey regdecls,
2017 since setjmp/longjmp can cause life info to screw up. */
2018 emit_insn (gen_rtx_USE (VOIDmode, pic_offset_table_rtx));
2019 #endif /* AOF_ASSEMBLER */
2022 #define REG_OR_SUBREG_REG(X) \
2023 (GET_CODE (X) == REG \
2024 || (GET_CODE (X) == SUBREG && GET_CODE (SUBREG_REG (X)) == REG))
2026 #define REG_OR_SUBREG_RTX(X) \
2027 (GET_CODE (X) == REG ? (X) : SUBREG_REG (X))
2029 #ifndef COSTS_N_INSNS
2030 #define COSTS_N_INSNS(N) ((N) * 4 - 2)
2031 #endif
2034 arm_rtx_costs (x, code, outer)
2035 rtx x;
2036 enum rtx_code code;
2037 enum rtx_code outer;
2039 enum machine_mode mode = GET_MODE (x);
2040 enum rtx_code subcode;
2041 int extra_cost;
2043 if (TARGET_THUMB)
2045 switch (code)
2047 case ASHIFT:
2048 case ASHIFTRT:
2049 case LSHIFTRT:
2050 case ROTATERT:
2051 case PLUS:
2052 case MINUS:
2053 case COMPARE:
2054 case NEG:
2055 case NOT:
2056 return COSTS_N_INSNS (1);
2058 case MULT:
2059 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
2061 int cycles = 0;
2062 unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
2064 while (i)
2066 i >>= 2;
2067 cycles ++;
2069 return COSTS_N_INSNS (2) + cycles;
2071 return COSTS_N_INSNS (1) + 16;
2073 case SET:
2074 return (COSTS_N_INSNS (1)
2075 + 4 * ((GET_CODE (SET_SRC (x)) == MEM)
2076 + GET_CODE (SET_DEST (x)) == MEM));
2078 case CONST_INT:
2079 if (outer == SET)
2081 if ((unsigned HOST_WIDE_INT) INTVAL (x) < 256)
2082 return 0;
2083 if (thumb_shiftable_const (INTVAL (x)))
2084 return COSTS_N_INSNS (2);
2085 return COSTS_N_INSNS (3);
2087 else if (outer == PLUS
2088 && INTVAL (x) < 256 && INTVAL (x) > -256)
2089 return 0;
2090 else if (outer == COMPARE
2091 && (unsigned HOST_WIDE_INT) INTVAL (x) < 256)
2092 return 0;
2093 else if (outer == ASHIFT || outer == ASHIFTRT
2094 || outer == LSHIFTRT)
2095 return 0;
2096 return COSTS_N_INSNS (2);
2098 case CONST:
2099 case CONST_DOUBLE:
2100 case LABEL_REF:
2101 case SYMBOL_REF:
2102 return COSTS_N_INSNS (3);
2104 case UDIV:
2105 case UMOD:
2106 case DIV:
2107 case MOD:
2108 return 100;
2110 case TRUNCATE:
2111 return 99;
2113 case AND:
2114 case XOR:
2115 case IOR:
2116 /* XXX guess. */
2117 return 8;
2119 case ADDRESSOF:
2120 case MEM:
2121 /* XXX another guess. */
2122 /* Memory costs quite a lot for the first word, but subsequent words
2123 load at the equivalent of a single insn each. */
2124 return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
2125 + (CONSTANT_POOL_ADDRESS_P (x) ? 4 : 0));
2127 case IF_THEN_ELSE:
2128 /* XXX a guess. */
2129 if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
2130 return 14;
2131 return 2;
2133 case ZERO_EXTEND:
2134 /* XXX still guessing. */
2135 switch (GET_MODE (XEXP (x, 0)))
2137 case QImode:
2138 return (1 + (mode == DImode ? 4 : 0)
2139 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
2141 case HImode:
2142 return (4 + (mode == DImode ? 4 : 0)
2143 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
2145 case SImode:
2146 return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
2148 default:
2149 return 99;
2152 default:
2153 return 99;
2154 #if 0
2155 case FFS:
2156 case FLOAT:
2157 case FIX:
2158 case UNSIGNED_FIX:
2159 /* XXX guess */
2160 fprintf (stderr, "unexpected code for thumb in rtx_costs: %s\n",
2161 rtx_name[code]);
2162 abort ();
2163 #endif
2167 switch (code)
2169 case MEM:
2170 /* Memory costs quite a lot for the first word, but subsequent words
2171 load at the equivalent of a single insn each. */
2172 return (10 + 4 * ((GET_MODE_SIZE (mode) - 1) / UNITS_PER_WORD)
2173 + (CONSTANT_POOL_ADDRESS_P (x) ? 4 : 0));
2175 case DIV:
2176 case MOD:
2177 return 100;
2179 case ROTATE:
2180 if (mode == SImode && GET_CODE (XEXP (x, 1)) == REG)
2181 return 4;
2182 /* Fall through */
2183 case ROTATERT:
2184 if (mode != SImode)
2185 return 8;
2186 /* Fall through */
2187 case ASHIFT: case LSHIFTRT: case ASHIFTRT:
2188 if (mode == DImode)
2189 return (8 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : 8)
2190 + ((GET_CODE (XEXP (x, 0)) == REG
2191 || (GET_CODE (XEXP (x, 0)) == SUBREG
2192 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
2193 ? 0 : 8));
2194 return (1 + ((GET_CODE (XEXP (x, 0)) == REG
2195 || (GET_CODE (XEXP (x, 0)) == SUBREG
2196 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG))
2197 ? 0 : 4)
2198 + ((GET_CODE (XEXP (x, 1)) == REG
2199 || (GET_CODE (XEXP (x, 1)) == SUBREG
2200 && GET_CODE (SUBREG_REG (XEXP (x, 1))) == REG)
2201 || (GET_CODE (XEXP (x, 1)) == CONST_INT))
2202 ? 0 : 4));
2204 case MINUS:
2205 if (mode == DImode)
2206 return (4 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 8)
2207 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
2208 || (GET_CODE (XEXP (x, 0)) == CONST_INT
2209 && const_ok_for_arm (INTVAL (XEXP (x, 0)))))
2210 ? 0 : 8));
2212 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
2213 return (2 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
2214 || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
2215 && const_double_rtx_ok_for_fpu (XEXP (x, 1))))
2216 ? 0 : 8)
2217 + ((REG_OR_SUBREG_REG (XEXP (x, 0))
2218 || (GET_CODE (XEXP (x, 0)) == CONST_DOUBLE
2219 && const_double_rtx_ok_for_fpu (XEXP (x, 0))))
2220 ? 0 : 8));
2222 if (((GET_CODE (XEXP (x, 0)) == CONST_INT
2223 && const_ok_for_arm (INTVAL (XEXP (x, 0)))
2224 && REG_OR_SUBREG_REG (XEXP (x, 1))))
2225 || (((subcode = GET_CODE (XEXP (x, 1))) == ASHIFT
2226 || subcode == ASHIFTRT || subcode == LSHIFTRT
2227 || subcode == ROTATE || subcode == ROTATERT
2228 || (subcode == MULT
2229 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
2230 && ((INTVAL (XEXP (XEXP (x, 1), 1)) &
2231 (INTVAL (XEXP (XEXP (x, 1), 1)) - 1)) == 0)))
2232 && REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 0))
2233 && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 1), 1))
2234 || GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT)
2235 && REG_OR_SUBREG_REG (XEXP (x, 0))))
2236 return 1;
2237 /* Fall through */
2239 case PLUS:
2240 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
2241 return (2 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
2242 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
2243 || (GET_CODE (XEXP (x, 1)) == CONST_DOUBLE
2244 && const_double_rtx_ok_for_fpu (XEXP (x, 1))))
2245 ? 0 : 8));
2247 /* Fall through */
2248 case AND: case XOR: case IOR:
2249 extra_cost = 0;
2251 /* Normally the frame registers will be spilt into reg+const during
2252 reload, so it is a bad idea to combine them with other instructions,
2253 since then they might not be moved outside of loops. As a compromise
2254 we allow integration with ops that have a constant as their second
2255 operand. */
2256 if ((REG_OR_SUBREG_REG (XEXP (x, 0))
2257 && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))
2258 && GET_CODE (XEXP (x, 1)) != CONST_INT)
2259 || (REG_OR_SUBREG_REG (XEXP (x, 0))
2260 && ARM_FRAME_RTX (REG_OR_SUBREG_RTX (XEXP (x, 0)))))
2261 extra_cost = 4;
2263 if (mode == DImode)
2264 return (4 + extra_cost + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 8)
2265 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
2266 || (GET_CODE (XEXP (x, 1)) == CONST_INT
2267 && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
2268 ? 0 : 8));
2270 if (REG_OR_SUBREG_REG (XEXP (x, 0)))
2271 return (1 + (GET_CODE (XEXP (x, 1)) == CONST_INT ? 0 : extra_cost)
2272 + ((REG_OR_SUBREG_REG (XEXP (x, 1))
2273 || (GET_CODE (XEXP (x, 1)) == CONST_INT
2274 && const_ok_for_op (INTVAL (XEXP (x, 1)), code)))
2275 ? 0 : 4));
2277 else if (REG_OR_SUBREG_REG (XEXP (x, 1)))
2278 return (1 + extra_cost
2279 + ((((subcode = GET_CODE (XEXP (x, 0))) == ASHIFT
2280 || subcode == LSHIFTRT || subcode == ASHIFTRT
2281 || subcode == ROTATE || subcode == ROTATERT
2282 || (subcode == MULT
2283 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
2284 && ((INTVAL (XEXP (XEXP (x, 0), 1)) &
2285 (INTVAL (XEXP (XEXP (x, 0), 1)) - 1)) == 0)))
2286 && (REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 0)))
2287 && ((REG_OR_SUBREG_REG (XEXP (XEXP (x, 0), 1)))
2288 || GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT))
2289 ? 0 : 4));
2291 return 8;
2293 case MULT:
2294 /* There is no point basing this on the tuning, since it is always the
2295 fast variant if it exists at all. */
2296 if (arm_fast_multiply && mode == DImode
2297 && (GET_CODE (XEXP (x, 0)) == GET_CODE (XEXP (x, 1)))
2298 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
2299 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND))
2300 return 8;
2302 if (GET_MODE_CLASS (mode) == MODE_FLOAT
2303 || mode == DImode)
2304 return 30;
2306 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
2308 unsigned HOST_WIDE_INT i = (INTVAL (XEXP (x, 1))
2309 & (unsigned HOST_WIDE_INT) 0xffffffff);
2310 int add_cost = const_ok_for_arm (i) ? 4 : 8;
2311 int j;
2313 /* Tune as appropriate. */
2314 int booth_unit_size = ((tune_flags & FL_FAST_MULT) ? 8 : 2);
2316 for (j = 0; i && j < 32; j += booth_unit_size)
2318 i >>= booth_unit_size;
2319 add_cost += 2;
2322 return add_cost;
2325 return (((tune_flags & FL_FAST_MULT) ? 8 : 30)
2326 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4)
2327 + (REG_OR_SUBREG_REG (XEXP (x, 1)) ? 0 : 4));
2329 case TRUNCATE:
2330 if (arm_fast_multiply && mode == SImode
2331 && GET_CODE (XEXP (x, 0)) == LSHIFTRT
2332 && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT
2333 && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0))
2334 == GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)))
2335 && (GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ZERO_EXTEND
2336 || GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == SIGN_EXTEND))
2337 return 8;
2338 return 99;
2340 case NEG:
2341 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
2342 return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 6);
2343 /* Fall through */
2344 case NOT:
2345 if (mode == DImode)
2346 return 4 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
2348 return 1 + (REG_OR_SUBREG_REG (XEXP (x, 0)) ? 0 : 4);
2350 case IF_THEN_ELSE:
2351 if (GET_CODE (XEXP (x, 1)) == PC || GET_CODE (XEXP (x, 2)) == PC)
2352 return 14;
2353 return 2;
2355 case COMPARE:
2356 return 1;
2358 case ABS:
2359 return 4 + (mode == DImode ? 4 : 0);
2361 case SIGN_EXTEND:
2362 if (GET_MODE (XEXP (x, 0)) == QImode)
2363 return (4 + (mode == DImode ? 4 : 0)
2364 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
2365 /* Fall through */
2366 case ZERO_EXTEND:
2367 switch (GET_MODE (XEXP (x, 0)))
2369 case QImode:
2370 return (1 + (mode == DImode ? 4 : 0)
2371 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
2373 case HImode:
2374 return (4 + (mode == DImode ? 4 : 0)
2375 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
2377 case SImode:
2378 return (1 + (GET_CODE (XEXP (x, 0)) == MEM ? 10 : 0));
2380 default:
2381 break;
2383 abort ();
2385 case CONST_INT:
2386 if (const_ok_for_arm (INTVAL (x)))
2387 return outer == SET ? 2 : -1;
2388 else if (outer == AND
2389 && const_ok_for_arm (~ INTVAL (x)))
2390 return -1;
2391 else if ((outer == COMPARE
2392 || outer == PLUS || outer == MINUS)
2393 && const_ok_for_arm (- INTVAL (x)))
2394 return -1;
2395 else
2396 return 5;
2398 case CONST:
2399 case LABEL_REF:
2400 case SYMBOL_REF:
2401 return 6;
2403 case CONST_DOUBLE:
2404 if (const_double_rtx_ok_for_fpu (x))
2405 return outer == SET ? 2 : -1;
2406 else if ((outer == COMPARE || outer == PLUS)
2407 && neg_const_double_rtx_ok_for_fpu (x))
2408 return -1;
2409 return 7;
2411 default:
2412 return 99;
2417 arm_adjust_cost (insn, link, dep, cost)
2418 rtx insn;
2419 rtx link;
2420 rtx dep;
2421 int cost;
2423 rtx i_pat, d_pat;
2425 /* XXX This is not strictly true for the FPA. */
2426 if (REG_NOTE_KIND (link) == REG_DEP_ANTI
2427 || REG_NOTE_KIND (link) == REG_DEP_OUTPUT)
2428 return 0;
2430 /* Call insns don't incur a stall, even if they follow a load. */
2431 if (REG_NOTE_KIND (link) == 0
2432 && GET_CODE (insn) == CALL_INSN)
2433 return 1;
2435 if ((i_pat = single_set (insn)) != NULL
2436 && GET_CODE (SET_SRC (i_pat)) == MEM
2437 && (d_pat = single_set (dep)) != NULL
2438 && GET_CODE (SET_DEST (d_pat)) == MEM)
2440 /* This is a load after a store, there is no conflict if the load reads
2441 from a cached area. Assume that loads from the stack, and from the
2442 constant pool are cached, and that others will miss. This is a
2443 hack. */
2445 if (CONSTANT_POOL_ADDRESS_P (XEXP (SET_SRC (i_pat), 0))
2446 || reg_mentioned_p (stack_pointer_rtx, XEXP (SET_SRC (i_pat), 0))
2447 || reg_mentioned_p (frame_pointer_rtx, XEXP (SET_SRC (i_pat), 0))
2448 || reg_mentioned_p (hard_frame_pointer_rtx,
2449 XEXP (SET_SRC (i_pat), 0)))
2450 return 1;
2453 return cost;
2456 /* This code has been fixed for cross compilation. */
2458 static int fpa_consts_inited = 0;
2460 char * strings_fpa[8] =
2462 "0", "1", "2", "3",
2463 "4", "5", "0.5", "10"
2466 static REAL_VALUE_TYPE values_fpa[8];
2468 static void
2469 init_fpa_table ()
2471 int i;
2472 REAL_VALUE_TYPE r;
2474 for (i = 0; i < 8; i++)
2476 r = REAL_VALUE_ATOF (strings_fpa[i], DFmode);
2477 values_fpa[i] = r;
2480 fpa_consts_inited = 1;
2483 /* Return TRUE if rtx X is a valid immediate FPU constant. */
2486 const_double_rtx_ok_for_fpu (x)
2487 rtx x;
2489 REAL_VALUE_TYPE r;
2490 int i;
2492 if (!fpa_consts_inited)
2493 init_fpa_table ();
2495 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
2496 if (REAL_VALUE_MINUS_ZERO (r))
2497 return 0;
2499 for (i = 0; i < 8; i++)
2500 if (REAL_VALUES_EQUAL (r, values_fpa[i]))
2501 return 1;
2503 return 0;
2506 /* Return TRUE if rtx X is a valid immediate FPU constant. */
2509 neg_const_double_rtx_ok_for_fpu (x)
2510 rtx x;
2512 REAL_VALUE_TYPE r;
2513 int i;
2515 if (!fpa_consts_inited)
2516 init_fpa_table ();
2518 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
2519 r = REAL_VALUE_NEGATE (r);
2520 if (REAL_VALUE_MINUS_ZERO (r))
2521 return 0;
2523 for (i = 0; i < 8; i++)
2524 if (REAL_VALUES_EQUAL (r, values_fpa[i]))
2525 return 1;
2527 return 0;
2530 /* Predicates for `match_operand' and `match_operator'. */
2532 /* s_register_operand is the same as register_operand, but it doesn't accept
2533 (SUBREG (MEM)...).
2535 This function exists because at the time it was put in it led to better
2536 code. SUBREG(MEM) always needs a reload in the places where
2537 s_register_operand is used, and this seemed to lead to excessive
2538 reloading. */
2541 s_register_operand (op, mode)
2542 register rtx op;
2543 enum machine_mode mode;
2545 if (GET_MODE (op) != mode && mode != VOIDmode)
2546 return 0;
2548 if (GET_CODE (op) == SUBREG)
2549 op = SUBREG_REG (op);
2551 /* We don't consider registers whose class is NO_REGS
2552 to be a register operand. */
2553 /* XXX might have to check for lo regs only for thumb ??? */
2554 return (GET_CODE (op) == REG
2555 && (REGNO (op) >= FIRST_PSEUDO_REGISTER
2556 || REGNO_REG_CLASS (REGNO (op)) != NO_REGS));
2559 /* Only accept reg, subreg(reg), const_int. */
2562 reg_or_int_operand (op, mode)
2563 register rtx op;
2564 enum machine_mode mode;
2566 if (GET_CODE (op) == CONST_INT)
2567 return 1;
2569 if (GET_MODE (op) != mode && mode != VOIDmode)
2570 return 0;
2572 if (GET_CODE (op) == SUBREG)
2573 op = SUBREG_REG (op);
2575 /* We don't consider registers whose class is NO_REGS
2576 to be a register operand. */
2577 return (GET_CODE (op) == REG
2578 && (REGNO (op) >= FIRST_PSEUDO_REGISTER
2579 || REGNO_REG_CLASS (REGNO (op)) != NO_REGS));
2582 /* Return 1 if OP is an item in memory, given that we are in reload. */
2585 arm_reload_memory_operand (op, mode)
2586 rtx op;
2587 enum machine_mode mode ATTRIBUTE_UNUSED;
2589 int regno = true_regnum (op);
2591 return (! CONSTANT_P (op)
2592 && (regno == -1
2593 || (GET_CODE (op) == REG
2594 && REGNO (op) >= FIRST_PSEUDO_REGISTER)));
2597 /* Return 1 if OP is a valid memory address, but not valid for a signed byte
2598 memory access (architecture V4).
2599 MODE is QImode if called when computing contraints, or VOIDmode when
2600 emitting patterns. In this latter case we cannot use memory_operand()
2601 because it will fail on badly formed MEMs, which is precisly what we are
2602 trying to catch. */
2604 bad_signed_byte_operand (op, mode)
2605 rtx op;
2606 enum machine_mode mode ATTRIBUTE_UNUSED;
2608 #if 0
2609 if ((mode == QImode && ! memory_operand (op, mode)) || GET_CODE (op) != MEM)
2610 return 0;
2611 #endif
2612 if (GET_CODE (op) != MEM)
2613 return 0;
2615 op = XEXP (op, 0);
2617 /* A sum of anything more complex than reg + reg or reg + const is bad. */
2618 if ((GET_CODE (op) == PLUS || GET_CODE (op) == MINUS)
2619 && (! s_register_operand (XEXP (op, 0), VOIDmode)
2620 || (! s_register_operand (XEXP (op, 1), VOIDmode)
2621 && GET_CODE (XEXP (op, 1)) != CONST_INT)))
2622 return 1;
2624 /* Big constants are also bad. */
2625 if (GET_CODE (op) == PLUS && GET_CODE (XEXP (op, 1)) == CONST_INT
2626 && (INTVAL (XEXP (op, 1)) > 0xff
2627 || -INTVAL (XEXP (op, 1)) > 0xff))
2628 return 1;
2630 /* Everything else is good, or can will automatically be made so. */
2631 return 0;
2634 /* Return TRUE for valid operands for the rhs of an ARM instruction. */
2637 arm_rhs_operand (op, mode)
2638 rtx op;
2639 enum machine_mode mode;
2641 return (s_register_operand (op, mode)
2642 || (GET_CODE (op) == CONST_INT && const_ok_for_arm (INTVAL (op))));
2645 /* Return TRUE for valid operands for the rhs of an ARM instruction, or a load.
2649 arm_rhsm_operand (op, mode)
2650 rtx op;
2651 enum machine_mode mode;
2653 return (s_register_operand (op, mode)
2654 || (GET_CODE (op) == CONST_INT && const_ok_for_arm (INTVAL (op)))
2655 || memory_operand (op, mode));
2658 /* Return TRUE for valid operands for the rhs of an ARM instruction, or if a
2659 constant that is valid when negated. */
2662 arm_add_operand (op, mode)
2663 rtx op;
2664 enum machine_mode mode;
2666 if (TARGET_THUMB)
2667 return thumb_cmp_operand (op, mode);
2669 return (s_register_operand (op, mode)
2670 || (GET_CODE (op) == CONST_INT
2671 && (const_ok_for_arm (INTVAL (op))
2672 || const_ok_for_arm (-INTVAL (op)))));
2676 arm_not_operand (op, mode)
2677 rtx op;
2678 enum machine_mode mode;
2680 return (s_register_operand (op, mode)
2681 || (GET_CODE (op) == CONST_INT
2682 && (const_ok_for_arm (INTVAL (op))
2683 || const_ok_for_arm (~INTVAL (op)))));
2686 /* Return TRUE if the operand is a memory reference which contains an
2687 offsettable address. */
2689 offsettable_memory_operand (op, mode)
2690 register rtx op;
2691 enum machine_mode mode;
2693 if (mode == VOIDmode)
2694 mode = GET_MODE (op);
2696 return (mode == GET_MODE (op)
2697 && GET_CODE (op) == MEM
2698 && offsettable_address_p (reload_completed | reload_in_progress,
2699 mode, XEXP (op, 0)));
2702 /* Return TRUE if the operand is a memory reference which is, or can be
2703 made word aligned by adjusting the offset. */
2705 alignable_memory_operand (op, mode)
2706 register rtx op;
2707 enum machine_mode mode;
2709 rtx reg;
2711 if (mode == VOIDmode)
2712 mode = GET_MODE (op);
2714 if (mode != GET_MODE (op) || GET_CODE (op) != MEM)
2715 return 0;
2717 op = XEXP (op, 0);
2719 return ((GET_CODE (reg = op) == REG
2720 || (GET_CODE (op) == SUBREG
2721 && GET_CODE (reg = SUBREG_REG (op)) == REG)
2722 || (GET_CODE (op) == PLUS
2723 && GET_CODE (XEXP (op, 1)) == CONST_INT
2724 && (GET_CODE (reg = XEXP (op, 0)) == REG
2725 || (GET_CODE (XEXP (op, 0)) == SUBREG
2726 && GET_CODE (reg = SUBREG_REG (XEXP (op, 0))) == REG))))
2727 && REGNO_POINTER_ALIGN (REGNO (reg)) >= 32);
2730 /* Similar to s_register_operand, but does not allow hard integer
2731 registers. */
2733 f_register_operand (op, mode)
2734 register rtx op;
2735 enum machine_mode mode;
2737 if (GET_MODE (op) != mode && mode != VOIDmode)
2738 return 0;
2740 if (GET_CODE (op) == SUBREG)
2741 op = SUBREG_REG (op);
2743 /* We don't consider registers whose class is NO_REGS
2744 to be a register operand. */
2745 return (GET_CODE (op) == REG
2746 && (REGNO (op) >= FIRST_PSEUDO_REGISTER
2747 || REGNO_REG_CLASS (REGNO (op)) == FPU_REGS));
2750 /* Return TRUE for valid operands for the rhs of an FPU instruction. */
2753 fpu_rhs_operand (op, mode)
2754 rtx op;
2755 enum machine_mode mode;
2757 if (s_register_operand (op, mode))
2758 return TRUE;
2760 if (GET_MODE (op) != mode && mode != VOIDmode)
2761 return FALSE;
2763 if (GET_CODE (op) == CONST_DOUBLE)
2764 return const_double_rtx_ok_for_fpu (op);
2766 return FALSE;
2770 fpu_add_operand (op, mode)
2771 rtx op;
2772 enum machine_mode mode;
2774 if (s_register_operand (op, mode))
2775 return TRUE;
2777 if (GET_MODE (op) != mode && mode != VOIDmode)
2778 return FALSE;
2780 if (GET_CODE (op) == CONST_DOUBLE)
2781 return (const_double_rtx_ok_for_fpu (op)
2782 || neg_const_double_rtx_ok_for_fpu (op));
2784 return FALSE;
2787 /* Return nonzero if OP is a constant power of two. */
2790 power_of_two_operand (op, mode)
2791 rtx op;
2792 enum machine_mode mode ATTRIBUTE_UNUSED;
2794 if (GET_CODE (op) == CONST_INT)
2796 HOST_WIDE_INT value = INTVAL (op);
2797 return value != 0 && (value & (value - 1)) == 0;
2799 return FALSE;
2802 /* Return TRUE for a valid operand of a DImode operation.
2803 Either: REG, SUBREG, CONST_DOUBLE or MEM(DImode_address).
2804 Note that this disallows MEM(REG+REG), but allows
2805 MEM(PRE/POST_INC/DEC(REG)). */
2808 di_operand (op, mode)
2809 rtx op;
2810 enum machine_mode mode;
2812 if (s_register_operand (op, mode))
2813 return TRUE;
2815 if (mode != VOIDmode && GET_MODE (op) != VOIDmode && GET_MODE (op) != DImode)
2816 return FALSE;
2818 if (GET_CODE (op) == SUBREG)
2819 op = SUBREG_REG (op);
2821 switch (GET_CODE (op))
2823 case CONST_DOUBLE:
2824 case CONST_INT:
2825 return TRUE;
2827 case MEM:
2828 return memory_address_p (DImode, XEXP (op, 0));
2830 default:
2831 return FALSE;
2835 /* Like di_operand, but don't accept constants. */
2837 nonimmediate_di_operand (op, mode)
2838 rtx op;
2839 enum machine_mode mode;
2841 if (s_register_operand (op, mode))
2842 return TRUE;
2844 if (mode != VOIDmode && GET_MODE (op) != VOIDmode && GET_MODE (op) != DImode)
2845 return FALSE;
2847 if (GET_CODE (op) == SUBREG)
2848 op = SUBREG_REG (op);
2850 if (GET_CODE (op) == MEM)
2851 return memory_address_p (DImode, XEXP (op, 0));
2853 return FALSE;
2856 /* Return TRUE for a valid operand of a DFmode operation when -msoft-float.
2857 Either: REG, SUBREG, CONST_DOUBLE or MEM(DImode_address).
2858 Note that this disallows MEM(REG+REG), but allows
2859 MEM(PRE/POST_INC/DEC(REG)). */
2862 soft_df_operand (op, mode)
2863 rtx op;
2864 enum machine_mode mode;
2866 if (s_register_operand (op, mode))
2867 return TRUE;
2869 if (mode != VOIDmode && GET_MODE (op) != mode)
2870 return FALSE;
2872 if (GET_CODE (op) == SUBREG && CONSTANT_P (SUBREG_REG (op)))
2873 return FALSE;
2875 if (GET_CODE (op) == SUBREG)
2876 op = SUBREG_REG (op);
2878 switch (GET_CODE (op))
2880 case CONST_DOUBLE:
2881 return TRUE;
2883 case MEM:
2884 return memory_address_p (DFmode, XEXP (op, 0));
2886 default:
2887 return FALSE;
2891 /* Like soft_df_operand, but don't accept constants. */
2893 nonimmediate_soft_df_operand (op, mode)
2894 rtx op;
2895 enum machine_mode mode;
2897 if (s_register_operand (op, mode))
2898 return TRUE;
2900 if (mode != VOIDmode && GET_MODE (op) != mode)
2901 return FALSE;
2903 if (GET_CODE (op) == SUBREG)
2904 op = SUBREG_REG (op);
2906 if (GET_CODE (op) == MEM)
2907 return memory_address_p (DFmode, XEXP (op, 0));
2908 return FALSE;
2911 /* Return TRUE for valid index operands. */
2913 index_operand (op, mode)
2914 rtx op;
2915 enum machine_mode mode;
2917 return (s_register_operand (op, mode)
2918 || (immediate_operand (op, mode)
2919 && (GET_CODE (op) != CONST_INT
2920 || (INTVAL (op) < 4096 && INTVAL (op) > -4096))));
2923 /* Return TRUE for valid shifts by a constant. This also accepts any
2924 power of two on the (somewhat overly relaxed) assumption that the
2925 shift operator in this case was a mult. */
2928 const_shift_operand (op, mode)
2929 rtx op;
2930 enum machine_mode mode;
2932 return (power_of_two_operand (op, mode)
2933 || (immediate_operand (op, mode)
2934 && (GET_CODE (op) != CONST_INT
2935 || (INTVAL (op) < 32 && INTVAL (op) > 0))));
2938 /* Return TRUE for arithmetic operators which can be combined with a multiply
2939 (shift). */
2942 shiftable_operator (x, mode)
2943 rtx x;
2944 enum machine_mode mode;
2946 if (GET_MODE (x) != mode)
2947 return FALSE;
2948 else
2950 enum rtx_code code = GET_CODE (x);
2952 return (code == PLUS || code == MINUS
2953 || code == IOR || code == XOR || code == AND);
2957 /* Return TRUE for binary logical operators. */
2960 logical_binary_operator (x, mode)
2961 rtx x;
2962 enum machine_mode mode;
2964 if (GET_MODE (x) != mode)
2965 return FALSE;
2966 else
2968 enum rtx_code code = GET_CODE (x);
2970 return (code == IOR || code == XOR || code == AND);
2974 /* Return TRUE for shift operators. */
2977 shift_operator (x, mode)
2978 rtx x;
2979 enum machine_mode mode;
2981 if (GET_MODE (x) != mode)
2982 return FALSE;
2983 else
2985 enum rtx_code code = GET_CODE (x);
2987 if (code == MULT)
2988 return power_of_two_operand (XEXP (x, 1), mode);
2990 return (code == ASHIFT || code == ASHIFTRT || code == LSHIFTRT
2991 || code == ROTATERT);
2995 /* Return TRUE if x is EQ or NE. */
2997 equality_operator (x, mode)
2998 rtx x;
2999 enum machine_mode mode ATTRIBUTE_UNUSED;
3001 return GET_CODE (x) == EQ || GET_CODE (x) == NE;
3004 /* Return TRUE for SMIN SMAX UMIN UMAX operators. */
3006 minmax_operator (x, mode)
3007 rtx x;
3008 enum machine_mode mode;
3010 enum rtx_code code = GET_CODE (x);
3012 if (GET_MODE (x) != mode)
3013 return FALSE;
3015 return code == SMIN || code == SMAX || code == UMIN || code == UMAX;
3018 /* Return TRUE if this is the condition code register, if we aren't given
3019 a mode, accept any class CCmode register. */
3021 cc_register (x, mode)
3022 rtx x;
3023 enum machine_mode mode;
3025 if (mode == VOIDmode)
3027 mode = GET_MODE (x);
3029 if (GET_MODE_CLASS (mode) != MODE_CC)
3030 return FALSE;
3033 if ( GET_MODE (x) == mode
3034 && GET_CODE (x) == REG
3035 && REGNO (x) == CC_REGNUM)
3036 return TRUE;
3038 return FALSE;
3041 /* Return TRUE if this is the condition code register, if we aren't given
3042 a mode, accept any class CCmode register which indicates a dominance
3043 expression. */
3045 dominant_cc_register (x, mode)
3046 rtx x;
3047 enum machine_mode mode;
3049 if (mode == VOIDmode)
3051 mode = GET_MODE (x);
3053 if (GET_MODE_CLASS (mode) != MODE_CC)
3054 return FALSE;
3057 if ( mode != CC_DNEmode && mode != CC_DEQmode
3058 && mode != CC_DLEmode && mode != CC_DLTmode
3059 && mode != CC_DGEmode && mode != CC_DGTmode
3060 && mode != CC_DLEUmode && mode != CC_DLTUmode
3061 && mode != CC_DGEUmode && mode != CC_DGTUmode)
3062 return FALSE;
3064 return cc_register (x, mode);
3067 /* Return TRUE if X references a SYMBOL_REF. */
3069 symbol_mentioned_p (x)
3070 rtx x;
3072 register const char * fmt;
3073 register int i;
3075 if (GET_CODE (x) == SYMBOL_REF)
3076 return 1;
3078 fmt = GET_RTX_FORMAT (GET_CODE (x));
3080 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
3082 if (fmt[i] == 'E')
3084 register int j;
3086 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3087 if (symbol_mentioned_p (XVECEXP (x, i, j)))
3088 return 1;
3090 else if (fmt[i] == 'e' && symbol_mentioned_p (XEXP (x, i)))
3091 return 1;
3094 return 0;
3097 /* Return TRUE if X references a LABEL_REF. */
3099 label_mentioned_p (x)
3100 rtx x;
3102 register const char * fmt;
3103 register int i;
3105 if (GET_CODE (x) == LABEL_REF)
3106 return 1;
3108 fmt = GET_RTX_FORMAT (GET_CODE (x));
3109 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
3111 if (fmt[i] == 'E')
3113 register int j;
3115 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3116 if (label_mentioned_p (XVECEXP (x, i, j)))
3117 return 1;
3119 else if (fmt[i] == 'e' && label_mentioned_p (XEXP (x, i)))
3120 return 1;
3123 return 0;
3126 enum rtx_code
3127 minmax_code (x)
3128 rtx x;
3130 enum rtx_code code = GET_CODE (x);
3132 if (code == SMAX)
3133 return GE;
3134 else if (code == SMIN)
3135 return LE;
3136 else if (code == UMIN)
3137 return LEU;
3138 else if (code == UMAX)
3139 return GEU;
3141 abort ();
3144 /* Return 1 if memory locations are adjacent. */
3146 adjacent_mem_locations (a, b)
3147 rtx a, b;
3149 int val0 = 0, val1 = 0;
3150 int reg0, reg1;
3152 if ((GET_CODE (XEXP (a, 0)) == REG
3153 || (GET_CODE (XEXP (a, 0)) == PLUS
3154 && GET_CODE (XEXP (XEXP (a, 0), 1)) == CONST_INT))
3155 && (GET_CODE (XEXP (b, 0)) == REG
3156 || (GET_CODE (XEXP (b, 0)) == PLUS
3157 && GET_CODE (XEXP (XEXP (b, 0), 1)) == CONST_INT)))
3159 if (GET_CODE (XEXP (a, 0)) == PLUS)
3161 reg0 = REGNO (XEXP (XEXP (a, 0), 0));
3162 val0 = INTVAL (XEXP (XEXP (a, 0), 1));
3164 else
3165 reg0 = REGNO (XEXP (a, 0));
3166 if (GET_CODE (XEXP (b, 0)) == PLUS)
3168 reg1 = REGNO (XEXP (XEXP (b, 0), 0));
3169 val1 = INTVAL (XEXP (XEXP (b, 0), 1));
3171 else
3172 reg1 = REGNO (XEXP (b, 0));
3173 return (reg0 == reg1) && ((val1 - val0) == 4 || (val0 - val1) == 4);
3175 return 0;
3178 /* Return 1 if OP is a load multiple operation. It is known to be
3179 parallel and the first section will be tested. */
3181 load_multiple_operation (op, mode)
3182 rtx op;
3183 enum machine_mode mode ATTRIBUTE_UNUSED;
3185 HOST_WIDE_INT count = XVECLEN (op, 0);
3186 int dest_regno;
3187 rtx src_addr;
3188 HOST_WIDE_INT i = 1, base = 0;
3189 rtx elt;
3191 if (count <= 1
3192 || GET_CODE (XVECEXP (op, 0, 0)) != SET)
3193 return 0;
3195 /* Check to see if this might be a write-back. */
3196 if (GET_CODE (SET_SRC (elt = XVECEXP (op, 0, 0))) == PLUS)
3198 i++;
3199 base = 1;
3201 /* Now check it more carefully. */
3202 if (GET_CODE (SET_DEST (elt)) != REG
3203 || GET_CODE (XEXP (SET_SRC (elt), 0)) != REG
3204 || REGNO (XEXP (SET_SRC (elt), 0)) != REGNO (SET_DEST (elt))
3205 || GET_CODE (XEXP (SET_SRC (elt), 1)) != CONST_INT
3206 || INTVAL (XEXP (SET_SRC (elt), 1)) != (count - 2) * 4
3207 || GET_CODE (XVECEXP (op, 0, count - 1)) != CLOBBER
3208 || GET_CODE (XEXP (XVECEXP (op, 0, count - 1), 0)) != REG
3209 || REGNO (XEXP (XVECEXP (op, 0, count - 1), 0))
3210 != REGNO (SET_DEST (elt)))
3211 return 0;
3213 count--;
3216 /* Perform a quick check so we don't blow up below. */
3217 if (count <= i
3218 || GET_CODE (XVECEXP (op, 0, i - 1)) != SET
3219 || GET_CODE (SET_DEST (XVECEXP (op, 0, i - 1))) != REG
3220 || GET_CODE (SET_SRC (XVECEXP (op, 0, i - 1))) != MEM)
3221 return 0;
3223 dest_regno = REGNO (SET_DEST (XVECEXP (op, 0, i - 1)));
3224 src_addr = XEXP (SET_SRC (XVECEXP (op, 0, i - 1)), 0);
3226 for (; i < count; i++)
3228 elt = XVECEXP (op, 0, i);
3230 if (GET_CODE (elt) != SET
3231 || GET_CODE (SET_DEST (elt)) != REG
3232 || GET_MODE (SET_DEST (elt)) != SImode
3233 || REGNO (SET_DEST (elt)) != (unsigned int)(dest_regno + i - base)
3234 || GET_CODE (SET_SRC (elt)) != MEM
3235 || GET_MODE (SET_SRC (elt)) != SImode
3236 || GET_CODE (XEXP (SET_SRC (elt), 0)) != PLUS
3237 || ! rtx_equal_p (XEXP (XEXP (SET_SRC (elt), 0), 0), src_addr)
3238 || GET_CODE (XEXP (XEXP (SET_SRC (elt), 0), 1)) != CONST_INT
3239 || INTVAL (XEXP (XEXP (SET_SRC (elt), 0), 1)) != (i - base) * 4)
3240 return 0;
3243 return 1;
3246 /* Return 1 if OP is a store multiple operation. It is known to be
3247 parallel and the first section will be tested. */
3249 store_multiple_operation (op, mode)
3250 rtx op;
3251 enum machine_mode mode ATTRIBUTE_UNUSED;
3253 HOST_WIDE_INT count = XVECLEN (op, 0);
3254 int src_regno;
3255 rtx dest_addr;
3256 HOST_WIDE_INT i = 1, base = 0;
3257 rtx elt;
3259 if (count <= 1
3260 || GET_CODE (XVECEXP (op, 0, 0)) != SET)
3261 return 0;
3263 /* Check to see if this might be a write-back. */
3264 if (GET_CODE (SET_SRC (elt = XVECEXP (op, 0, 0))) == PLUS)
3266 i++;
3267 base = 1;
3269 /* Now check it more carefully. */
3270 if (GET_CODE (SET_DEST (elt)) != REG
3271 || GET_CODE (XEXP (SET_SRC (elt), 0)) != REG
3272 || REGNO (XEXP (SET_SRC (elt), 0)) != REGNO (SET_DEST (elt))
3273 || GET_CODE (XEXP (SET_SRC (elt), 1)) != CONST_INT
3274 || INTVAL (XEXP (SET_SRC (elt), 1)) != (count - 2) * 4
3275 || GET_CODE (XVECEXP (op, 0, count - 1)) != CLOBBER
3276 || GET_CODE (XEXP (XVECEXP (op, 0, count - 1), 0)) != REG
3277 || REGNO (XEXP (XVECEXP (op, 0, count - 1), 0))
3278 != REGNO (SET_DEST (elt)))
3279 return 0;
3281 count--;
3284 /* Perform a quick check so we don't blow up below. */
3285 if (count <= i
3286 || GET_CODE (XVECEXP (op, 0, i - 1)) != SET
3287 || GET_CODE (SET_DEST (XVECEXP (op, 0, i - 1))) != MEM
3288 || GET_CODE (SET_SRC (XVECEXP (op, 0, i - 1))) != REG)
3289 return 0;
3291 src_regno = REGNO (SET_SRC (XVECEXP (op, 0, i - 1)));
3292 dest_addr = XEXP (SET_DEST (XVECEXP (op, 0, i - 1)), 0);
3294 for (; i < count; i++)
3296 elt = XVECEXP (op, 0, i);
3298 if (GET_CODE (elt) != SET
3299 || GET_CODE (SET_SRC (elt)) != REG
3300 || GET_MODE (SET_SRC (elt)) != SImode
3301 || REGNO (SET_SRC (elt)) != (unsigned int)(src_regno + i - base)
3302 || GET_CODE (SET_DEST (elt)) != MEM
3303 || GET_MODE (SET_DEST (elt)) != SImode
3304 || GET_CODE (XEXP (SET_DEST (elt), 0)) != PLUS
3305 || ! rtx_equal_p (XEXP (XEXP (SET_DEST (elt), 0), 0), dest_addr)
3306 || GET_CODE (XEXP (XEXP (SET_DEST (elt), 0), 1)) != CONST_INT
3307 || INTVAL (XEXP (XEXP (SET_DEST (elt), 0), 1)) != (i - base) * 4)
3308 return 0;
3311 return 1;
3315 load_multiple_sequence (operands, nops, regs, base, load_offset)
3316 rtx * operands;
3317 int nops;
3318 int * regs;
3319 int * base;
3320 HOST_WIDE_INT * load_offset;
3322 int unsorted_regs[4];
3323 HOST_WIDE_INT unsorted_offsets[4];
3324 int order[4];
3325 int base_reg = -1;
3326 int i;
3328 /* Can only handle 2, 3, or 4 insns at present, though could be easily
3329 extended if required. */
3330 if (nops < 2 || nops > 4)
3331 abort ();
3333 /* Loop over the operands and check that the memory references are
3334 suitable (ie immediate offsets from the same base register). At
3335 the same time, extract the target register, and the memory
3336 offsets. */
3337 for (i = 0; i < nops; i++)
3339 rtx reg;
3340 rtx offset;
3342 /* Convert a subreg of a mem into the mem itself. */
3343 if (GET_CODE (operands[nops + i]) == SUBREG)
3344 operands[nops + i] = alter_subreg (operands[nops + i]);
3346 if (GET_CODE (operands[nops + i]) != MEM)
3347 abort ();
3349 /* Don't reorder volatile memory references; it doesn't seem worth
3350 looking for the case where the order is ok anyway. */
3351 if (MEM_VOLATILE_P (operands[nops + i]))
3352 return 0;
3354 offset = const0_rtx;
3356 if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
3357 || (GET_CODE (reg) == SUBREG
3358 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
3359 || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
3360 && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
3361 == REG)
3362 || (GET_CODE (reg) == SUBREG
3363 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
3364 && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
3365 == CONST_INT)))
3367 if (i == 0)
3369 base_reg = REGNO (reg);
3370 unsorted_regs[0] = (GET_CODE (operands[i]) == REG
3371 ? REGNO (operands[i])
3372 : REGNO (SUBREG_REG (operands[i])));
3373 order[0] = 0;
3375 else
3377 if (base_reg != (int) REGNO (reg))
3378 /* Not addressed from the same base register. */
3379 return 0;
3381 unsorted_regs[i] = (GET_CODE (operands[i]) == REG
3382 ? REGNO (operands[i])
3383 : REGNO (SUBREG_REG (operands[i])));
3384 if (unsorted_regs[i] < unsorted_regs[order[0]])
3385 order[0] = i;
3388 /* If it isn't an integer register, or if it overwrites the
3389 base register but isn't the last insn in the list, then
3390 we can't do this. */
3391 if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14
3392 || (i != nops - 1 && unsorted_regs[i] == base_reg))
3393 return 0;
3395 unsorted_offsets[i] = INTVAL (offset);
3397 else
3398 /* Not a suitable memory address. */
3399 return 0;
3402 /* All the useful information has now been extracted from the
3403 operands into unsorted_regs and unsorted_offsets; additionally,
3404 order[0] has been set to the lowest numbered register in the
3405 list. Sort the registers into order, and check that the memory
3406 offsets are ascending and adjacent. */
3408 for (i = 1; i < nops; i++)
3410 int j;
3412 order[i] = order[i - 1];
3413 for (j = 0; j < nops; j++)
3414 if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
3415 && (order[i] == order[i - 1]
3416 || unsorted_regs[j] < unsorted_regs[order[i]]))
3417 order[i] = j;
3419 /* Have we found a suitable register? if not, one must be used more
3420 than once. */
3421 if (order[i] == order[i - 1])
3422 return 0;
3424 /* Is the memory address adjacent and ascending? */
3425 if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
3426 return 0;
3429 if (base)
3431 *base = base_reg;
3433 for (i = 0; i < nops; i++)
3434 regs[i] = unsorted_regs[order[i]];
3436 *load_offset = unsorted_offsets[order[0]];
3439 if (unsorted_offsets[order[0]] == 0)
3440 return 1; /* ldmia */
3442 if (unsorted_offsets[order[0]] == 4)
3443 return 2; /* ldmib */
3445 if (unsorted_offsets[order[nops - 1]] == 0)
3446 return 3; /* ldmda */
3448 if (unsorted_offsets[order[nops - 1]] == -4)
3449 return 4; /* ldmdb */
3451 /* For ARM8,9 & StrongARM, 2 ldr instructions are faster than an ldm
3452 if the offset isn't small enough. The reason 2 ldrs are faster
3453 is because these ARMs are able to do more than one cache access
3454 in a single cycle. The ARM9 and StrongARM have Harvard caches,
3455 whilst the ARM8 has a double bandwidth cache. This means that
3456 these cores can do both an instruction fetch and a data fetch in
3457 a single cycle, so the trick of calculating the address into a
3458 scratch register (one of the result regs) and then doing a load
3459 multiple actually becomes slower (and no smaller in code size).
3460 That is the transformation
3462 ldr rd1, [rbase + offset]
3463 ldr rd2, [rbase + offset + 4]
3467 add rd1, rbase, offset
3468 ldmia rd1, {rd1, rd2}
3470 produces worse code -- '3 cycles + any stalls on rd2' instead of
3471 '2 cycles + any stalls on rd2'. On ARMs with only one cache
3472 access per cycle, the first sequence could never complete in less
3473 than 6 cycles, whereas the ldm sequence would only take 5 and
3474 would make better use of sequential accesses if not hitting the
3475 cache.
3477 We cheat here and test 'arm_ld_sched' which we currently know to
3478 only be true for the ARM8, ARM9 and StrongARM. If this ever
3479 changes, then the test below needs to be reworked. */
3480 if (nops == 2 && arm_ld_sched)
3481 return 0;
3483 /* Can't do it without setting up the offset, only do this if it takes
3484 no more than one insn. */
3485 return (const_ok_for_arm (unsorted_offsets[order[0]])
3486 || const_ok_for_arm (-unsorted_offsets[order[0]])) ? 5 : 0;
3489 char *
3490 emit_ldm_seq (operands, nops)
3491 rtx * operands;
3492 int nops;
3494 int regs[4];
3495 int base_reg;
3496 HOST_WIDE_INT offset;
3497 char buf[100];
3498 int i;
3500 switch (load_multiple_sequence (operands, nops, regs, &base_reg, &offset))
3502 case 1:
3503 strcpy (buf, "ldm%?ia\t");
3504 break;
3506 case 2:
3507 strcpy (buf, "ldm%?ib\t");
3508 break;
3510 case 3:
3511 strcpy (buf, "ldm%?da\t");
3512 break;
3514 case 4:
3515 strcpy (buf, "ldm%?db\t");
3516 break;
3518 case 5:
3519 if (offset >= 0)
3520 sprintf (buf, "add%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
3521 reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
3522 (long) offset);
3523 else
3524 sprintf (buf, "sub%%?\t%s%s, %s%s, #%ld", REGISTER_PREFIX,
3525 reg_names[regs[0]], REGISTER_PREFIX, reg_names[base_reg],
3526 (long) -offset);
3527 output_asm_insn (buf, operands);
3528 base_reg = regs[0];
3529 strcpy (buf, "ldm%?ia\t");
3530 break;
3532 default:
3533 abort ();
3536 sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
3537 reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
3539 for (i = 1; i < nops; i++)
3540 sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
3541 reg_names[regs[i]]);
3543 strcat (buf, "}\t%@ phole ldm");
3545 output_asm_insn (buf, operands);
3546 return "";
3550 store_multiple_sequence (operands, nops, regs, base, load_offset)
3551 rtx * operands;
3552 int nops;
3553 int * regs;
3554 int * base;
3555 HOST_WIDE_INT * load_offset;
3557 int unsorted_regs[4];
3558 HOST_WIDE_INT unsorted_offsets[4];
3559 int order[4];
3560 int base_reg = -1;
3561 int i;
3563 /* Can only handle 2, 3, or 4 insns at present, though could be easily
3564 extended if required. */
3565 if (nops < 2 || nops > 4)
3566 abort ();
3568 /* Loop over the operands and check that the memory references are
3569 suitable (ie immediate offsets from the same base register). At
3570 the same time, extract the target register, and the memory
3571 offsets. */
3572 for (i = 0; i < nops; i++)
3574 rtx reg;
3575 rtx offset;
3577 /* Convert a subreg of a mem into the mem itself. */
3578 if (GET_CODE (operands[nops + i]) == SUBREG)
3579 operands[nops + i] = alter_subreg (operands[nops + i]);
3581 if (GET_CODE (operands[nops + i]) != MEM)
3582 abort ();
3584 /* Don't reorder volatile memory references; it doesn't seem worth
3585 looking for the case where the order is ok anyway. */
3586 if (MEM_VOLATILE_P (operands[nops + i]))
3587 return 0;
3589 offset = const0_rtx;
3591 if ((GET_CODE (reg = XEXP (operands[nops + i], 0)) == REG
3592 || (GET_CODE (reg) == SUBREG
3593 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
3594 || (GET_CODE (XEXP (operands[nops + i], 0)) == PLUS
3595 && ((GET_CODE (reg = XEXP (XEXP (operands[nops + i], 0), 0))
3596 == REG)
3597 || (GET_CODE (reg) == SUBREG
3598 && GET_CODE (reg = SUBREG_REG (reg)) == REG))
3599 && (GET_CODE (offset = XEXP (XEXP (operands[nops + i], 0), 1))
3600 == CONST_INT)))
3602 if (i == 0)
3604 base_reg = REGNO (reg);
3605 unsorted_regs[0] = (GET_CODE (operands[i]) == REG
3606 ? REGNO (operands[i])
3607 : REGNO (SUBREG_REG (operands[i])));
3608 order[0] = 0;
3610 else
3612 if (base_reg != (int) REGNO (reg))
3613 /* Not addressed from the same base register. */
3614 return 0;
3616 unsorted_regs[i] = (GET_CODE (operands[i]) == REG
3617 ? REGNO (operands[i])
3618 : REGNO (SUBREG_REG (operands[i])));
3619 if (unsorted_regs[i] < unsorted_regs[order[0]])
3620 order[0] = i;
3623 /* If it isn't an integer register, then we can't do this. */
3624 if (unsorted_regs[i] < 0 || unsorted_regs[i] > 14)
3625 return 0;
3627 unsorted_offsets[i] = INTVAL (offset);
3629 else
3630 /* Not a suitable memory address. */
3631 return 0;
3634 /* All the useful information has now been extracted from the
3635 operands into unsorted_regs and unsorted_offsets; additionally,
3636 order[0] has been set to the lowest numbered register in the
3637 list. Sort the registers into order, and check that the memory
3638 offsets are ascending and adjacent. */
3640 for (i = 1; i < nops; i++)
3642 int j;
3644 order[i] = order[i - 1];
3645 for (j = 0; j < nops; j++)
3646 if (unsorted_regs[j] > unsorted_regs[order[i - 1]]
3647 && (order[i] == order[i - 1]
3648 || unsorted_regs[j] < unsorted_regs[order[i]]))
3649 order[i] = j;
3651 /* Have we found a suitable register? if not, one must be used more
3652 than once. */
3653 if (order[i] == order[i - 1])
3654 return 0;
3656 /* Is the memory address adjacent and ascending? */
3657 if (unsorted_offsets[order[i]] != unsorted_offsets[order[i - 1]] + 4)
3658 return 0;
3661 if (base)
3663 *base = base_reg;
3665 for (i = 0; i < nops; i++)
3666 regs[i] = unsorted_regs[order[i]];
3668 *load_offset = unsorted_offsets[order[0]];
3671 if (unsorted_offsets[order[0]] == 0)
3672 return 1; /* stmia */
3674 if (unsorted_offsets[order[0]] == 4)
3675 return 2; /* stmib */
3677 if (unsorted_offsets[order[nops - 1]] == 0)
3678 return 3; /* stmda */
3680 if (unsorted_offsets[order[nops - 1]] == -4)
3681 return 4; /* stmdb */
3683 return 0;
3686 char *
3687 emit_stm_seq (operands, nops)
3688 rtx * operands;
3689 int nops;
3691 int regs[4];
3692 int base_reg;
3693 HOST_WIDE_INT offset;
3694 char buf[100];
3695 int i;
3697 switch (store_multiple_sequence (operands, nops, regs, &base_reg, &offset))
3699 case 1:
3700 strcpy (buf, "stm%?ia\t");
3701 break;
3703 case 2:
3704 strcpy (buf, "stm%?ib\t");
3705 break;
3707 case 3:
3708 strcpy (buf, "stm%?da\t");
3709 break;
3711 case 4:
3712 strcpy (buf, "stm%?db\t");
3713 break;
3715 default:
3716 abort ();
3719 sprintf (buf + strlen (buf), "%s%s, {%s%s", REGISTER_PREFIX,
3720 reg_names[base_reg], REGISTER_PREFIX, reg_names[regs[0]]);
3722 for (i = 1; i < nops; i++)
3723 sprintf (buf + strlen (buf), ", %s%s", REGISTER_PREFIX,
3724 reg_names[regs[i]]);
3726 strcat (buf, "}\t%@ phole stm");
3728 output_asm_insn (buf, operands);
3729 return "";
3733 multi_register_push (op, mode)
3734 rtx op;
3735 enum machine_mode mode ATTRIBUTE_UNUSED;
3737 if (GET_CODE (op) != PARALLEL
3738 || (GET_CODE (XVECEXP (op, 0, 0)) != SET)
3739 || (GET_CODE (SET_SRC (XVECEXP (op, 0, 0))) != UNSPEC)
3740 || (XINT (SET_SRC (XVECEXP (op, 0, 0)), 1) != 2))
3741 return 0;
3743 return 1;
3746 /* Routines for use with attributes. */
3748 /* Return nonzero if ATTR is a valid attribute for DECL.
3749 ATTRIBUTES are any existing attributes and ARGS are
3750 the arguments supplied with ATTR.
3752 Supported attributes:
3754 naked:
3755 don't output any prologue or epilogue code, the user is assumed
3756 to do the right thing.
3758 interfacearm:
3759 Always assume that this function will be entered in ARM mode,
3760 not Thumb mode, and that the caller wishes to be returned to in
3761 ARM mode. */
3763 arm_valid_machine_decl_attribute (decl, attr, args)
3764 tree decl;
3765 tree attr;
3766 tree args;
3768 if (args != NULL_TREE)
3769 return 0;
3771 if (is_attribute_p ("naked", attr))
3772 return TREE_CODE (decl) == FUNCTION_DECL;
3774 #ifdef ARM_PE
3775 if (is_attribute_p ("interfacearm", attr))
3776 return TREE_CODE (decl) == FUNCTION_DECL;
3777 #endif /* ARM_PE */
3779 return 0;
3782 /* Return non-zero if FUNC is a naked function. */
3783 static int
3784 arm_naked_function_p (func)
3785 tree func;
3787 tree a;
3789 if (TREE_CODE (func) != FUNCTION_DECL)
3790 abort ();
3792 a = lookup_attribute ("naked", DECL_MACHINE_ATTRIBUTES (func));
3793 return a != NULL_TREE;
3796 /* Routines for use in generating RTL. */
3798 arm_gen_load_multiple (base_regno, count, from, up, write_back, unchanging_p,
3799 in_struct_p, scalar_p)
3800 int base_regno;
3801 int count;
3802 rtx from;
3803 int up;
3804 int write_back;
3805 int unchanging_p;
3806 int in_struct_p;
3807 int scalar_p;
3809 int i = 0, j;
3810 rtx result;
3811 int sign = up ? 1 : -1;
3812 rtx mem;
3814 result = gen_rtx_PARALLEL (VOIDmode,
3815 rtvec_alloc (count + (write_back ? 2 : 0)));
3816 if (write_back)
3818 XVECEXP (result, 0, 0)
3819 = gen_rtx_SET (GET_MODE (from), from,
3820 plus_constant (from, count * 4 * sign));
3821 i = 1;
3822 count++;
3825 for (j = 0; i < count; i++, j++)
3827 mem = gen_rtx_MEM (SImode, plus_constant (from, j * 4 * sign));
3828 RTX_UNCHANGING_P (mem) = unchanging_p;
3829 MEM_IN_STRUCT_P (mem) = in_struct_p;
3830 MEM_SCALAR_P (mem) = scalar_p;
3831 XVECEXP (result, 0, i)
3832 = gen_rtx_SET (VOIDmode, gen_rtx_REG (SImode, base_regno + j), mem);
3835 if (write_back)
3836 XVECEXP (result, 0, i) = gen_rtx_CLOBBER (SImode, from);
3838 return result;
3842 arm_gen_store_multiple (base_regno, count, to, up, write_back, unchanging_p,
3843 in_struct_p, scalar_p)
3844 int base_regno;
3845 int count;
3846 rtx to;
3847 int up;
3848 int write_back;
3849 int unchanging_p;
3850 int in_struct_p;
3851 int scalar_p;
3853 int i = 0, j;
3854 rtx result;
3855 int sign = up ? 1 : -1;
3856 rtx mem;
3858 result = gen_rtx_PARALLEL (VOIDmode,
3859 rtvec_alloc (count + (write_back ? 2 : 0)));
3860 if (write_back)
3862 XVECEXP (result, 0, 0)
3863 = gen_rtx_SET (GET_MODE (to), to,
3864 plus_constant (to, count * 4 * sign));
3865 i = 1;
3866 count++;
3869 for (j = 0; i < count; i++, j++)
3871 mem = gen_rtx_MEM (SImode, plus_constant (to, j * 4 * sign));
3872 RTX_UNCHANGING_P (mem) = unchanging_p;
3873 MEM_IN_STRUCT_P (mem) = in_struct_p;
3874 MEM_SCALAR_P (mem) = scalar_p;
3876 XVECEXP (result, 0, i)
3877 = gen_rtx_SET (VOIDmode, mem, gen_rtx_REG (SImode, base_regno + j));
3880 if (write_back)
3881 XVECEXP (result, 0, i) = gen_rtx_CLOBBER (SImode, to);
3883 return result;
3887 arm_gen_movstrqi (operands)
3888 rtx * operands;
3890 HOST_WIDE_INT in_words_to_go, out_words_to_go, last_bytes;
3891 int i;
3892 rtx src, dst;
3893 rtx st_src, st_dst, fin_src, fin_dst;
3894 rtx part_bytes_reg = NULL;
3895 rtx mem;
3896 int dst_unchanging_p, dst_in_struct_p, src_unchanging_p, src_in_struct_p;
3897 int dst_scalar_p, src_scalar_p;
3899 if (GET_CODE (operands[2]) != CONST_INT
3900 || GET_CODE (operands[3]) != CONST_INT
3901 || INTVAL (operands[2]) > 64
3902 || INTVAL (operands[3]) & 3)
3903 return 0;
3905 st_dst = XEXP (operands[0], 0);
3906 st_src = XEXP (operands[1], 0);
3908 dst_unchanging_p = RTX_UNCHANGING_P (operands[0]);
3909 dst_in_struct_p = MEM_IN_STRUCT_P (operands[0]);
3910 dst_scalar_p = MEM_SCALAR_P (operands[0]);
3911 src_unchanging_p = RTX_UNCHANGING_P (operands[1]);
3912 src_in_struct_p = MEM_IN_STRUCT_P (operands[1]);
3913 src_scalar_p = MEM_SCALAR_P (operands[1]);
3915 fin_dst = dst = copy_to_mode_reg (SImode, st_dst);
3916 fin_src = src = copy_to_mode_reg (SImode, st_src);
3918 in_words_to_go = NUM_INTS (INTVAL (operands[2]));
3919 out_words_to_go = INTVAL (operands[2]) / 4;
3920 last_bytes = INTVAL (operands[2]) & 3;
3922 if (out_words_to_go != in_words_to_go && ((in_words_to_go - 1) & 3) != 0)
3923 part_bytes_reg = gen_rtx_REG (SImode, (in_words_to_go - 1) & 3);
3925 for (i = 0; in_words_to_go >= 2; i+=4)
3927 if (in_words_to_go > 4)
3928 emit_insn (arm_gen_load_multiple (0, 4, src, TRUE, TRUE,
3929 src_unchanging_p,
3930 src_in_struct_p,
3931 src_scalar_p));
3932 else
3933 emit_insn (arm_gen_load_multiple (0, in_words_to_go, src, TRUE,
3934 FALSE, src_unchanging_p,
3935 src_in_struct_p, src_scalar_p));
3937 if (out_words_to_go)
3939 if (out_words_to_go > 4)
3940 emit_insn (arm_gen_store_multiple (0, 4, dst, TRUE, TRUE,
3941 dst_unchanging_p,
3942 dst_in_struct_p,
3943 dst_scalar_p));
3944 else if (out_words_to_go != 1)
3945 emit_insn (arm_gen_store_multiple (0, out_words_to_go,
3946 dst, TRUE,
3947 (last_bytes == 0
3948 ? FALSE : TRUE),
3949 dst_unchanging_p,
3950 dst_in_struct_p,
3951 dst_scalar_p));
3952 else
3954 mem = gen_rtx_MEM (SImode, dst);
3955 RTX_UNCHANGING_P (mem) = dst_unchanging_p;
3956 MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
3957 MEM_SCALAR_P (mem) = dst_scalar_p;
3958 emit_move_insn (mem, gen_rtx_REG (SImode, 0));
3959 if (last_bytes != 0)
3960 emit_insn (gen_addsi3 (dst, dst, GEN_INT (4)));
3964 in_words_to_go -= in_words_to_go < 4 ? in_words_to_go : 4;
3965 out_words_to_go -= out_words_to_go < 4 ? out_words_to_go : 4;
3968 /* OUT_WORDS_TO_GO will be zero here if there are byte stores to do. */
3969 if (out_words_to_go)
3971 rtx sreg;
3973 mem = gen_rtx_MEM (SImode, src);
3974 RTX_UNCHANGING_P (mem) = src_unchanging_p;
3975 MEM_IN_STRUCT_P (mem) = src_in_struct_p;
3976 MEM_SCALAR_P (mem) = src_scalar_p;
3977 emit_move_insn (sreg = gen_reg_rtx (SImode), mem);
3978 emit_move_insn (fin_src = gen_reg_rtx (SImode), plus_constant (src, 4));
3980 mem = gen_rtx_MEM (SImode, dst);
3981 RTX_UNCHANGING_P (mem) = dst_unchanging_p;
3982 MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
3983 MEM_SCALAR_P (mem) = dst_scalar_p;
3984 emit_move_insn (mem, sreg);
3985 emit_move_insn (fin_dst = gen_reg_rtx (SImode), plus_constant (dst, 4));
3986 in_words_to_go--;
3988 if (in_words_to_go) /* Sanity check */
3989 abort ();
3992 if (in_words_to_go)
3994 if (in_words_to_go < 0)
3995 abort ();
3997 mem = gen_rtx_MEM (SImode, src);
3998 RTX_UNCHANGING_P (mem) = src_unchanging_p;
3999 MEM_IN_STRUCT_P (mem) = src_in_struct_p;
4000 MEM_SCALAR_P (mem) = src_scalar_p;
4001 part_bytes_reg = copy_to_mode_reg (SImode, mem);
4004 if (last_bytes && part_bytes_reg == NULL)
4005 abort ();
4007 if (BYTES_BIG_ENDIAN && last_bytes)
4009 rtx tmp = gen_reg_rtx (SImode);
4011 /* The bytes we want are in the top end of the word. */
4012 emit_insn (gen_lshrsi3 (tmp, part_bytes_reg,
4013 GEN_INT (8 * (4 - last_bytes))));
4014 part_bytes_reg = tmp;
4016 while (last_bytes)
4018 mem = gen_rtx_MEM (QImode, plus_constant (dst, last_bytes - 1));
4019 RTX_UNCHANGING_P (mem) = dst_unchanging_p;
4020 MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
4021 MEM_SCALAR_P (mem) = dst_scalar_p;
4022 emit_move_insn (mem, gen_rtx_SUBREG (QImode, part_bytes_reg, 0));
4024 if (--last_bytes)
4026 tmp = gen_reg_rtx (SImode);
4027 emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (8)));
4028 part_bytes_reg = tmp;
4033 else
4035 if (last_bytes > 1)
4037 mem = gen_rtx_MEM (HImode, dst);
4038 RTX_UNCHANGING_P (mem) = dst_unchanging_p;
4039 MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
4040 MEM_SCALAR_P (mem) = dst_scalar_p;
4041 emit_move_insn (mem, gen_rtx_SUBREG (HImode, part_bytes_reg, 0));
4042 last_bytes -= 2;
4043 if (last_bytes)
4045 rtx tmp = gen_reg_rtx (SImode);
4047 emit_insn (gen_addsi3 (dst, dst, GEN_INT (2)));
4048 emit_insn (gen_lshrsi3 (tmp, part_bytes_reg, GEN_INT (16)));
4049 part_bytes_reg = tmp;
4053 if (last_bytes)
4055 mem = gen_rtx_MEM (QImode, dst);
4056 RTX_UNCHANGING_P (mem) = dst_unchanging_p;
4057 MEM_IN_STRUCT_P (mem) = dst_in_struct_p;
4058 MEM_SCALAR_P (mem) = dst_scalar_p;
4059 emit_move_insn (mem, gen_rtx_SUBREG (QImode, part_bytes_reg, 0));
4063 return 1;
4066 /* Generate a memory reference for a half word, such that it will be loaded
4067 into the top 16 bits of the word. We can assume that the address is
4068 known to be alignable and of the form reg, or plus (reg, const). */
4070 arm_gen_rotated_half_load (memref)
4071 rtx memref;
4073 HOST_WIDE_INT offset = 0;
4074 rtx base = XEXP (memref, 0);
4076 if (GET_CODE (base) == PLUS)
4078 offset = INTVAL (XEXP (base, 1));
4079 base = XEXP (base, 0);
4082 /* If we aren't allowed to generate unaligned addresses, then fail. */
4083 if (TARGET_MMU_TRAPS
4084 && ((BYTES_BIG_ENDIAN ? 1 : 0) ^ ((offset & 2) == 0)))
4085 return NULL;
4087 base = gen_rtx_MEM (SImode, plus_constant (base, offset & ~2));
4089 if ((BYTES_BIG_ENDIAN ? 1 : 0) ^ ((offset & 2) == 2))
4090 return base;
4092 return gen_rtx_ROTATE (SImode, base, GEN_INT (16));
4095 static enum machine_mode
4096 select_dominance_cc_mode (x, y, cond_or)
4097 rtx x;
4098 rtx y;
4099 HOST_WIDE_INT cond_or;
4101 enum rtx_code cond1, cond2;
4102 int swapped = 0;
4104 /* Currently we will probably get the wrong result if the individual
4105 comparisons are not simple. This also ensures that it is safe to
4106 reverse a comparison if necessary. */
4107 if ((arm_select_cc_mode (cond1 = GET_CODE (x), XEXP (x, 0), XEXP (x, 1))
4108 != CCmode)
4109 || (arm_select_cc_mode (cond2 = GET_CODE (y), XEXP (y, 0), XEXP (y, 1))
4110 != CCmode))
4111 return CCmode;
4113 if (cond_or)
4114 cond1 = reverse_condition (cond1);
4116 /* If the comparisons are not equal, and one doesn't dominate the other,
4117 then we can't do this. */
4118 if (cond1 != cond2
4119 && ! comparison_dominates_p (cond1, cond2)
4120 && (swapped = 1, ! comparison_dominates_p (cond2, cond1)))
4121 return CCmode;
4123 if (swapped)
4125 enum rtx_code temp = cond1;
4126 cond1 = cond2;
4127 cond2 = temp;
4130 switch (cond1)
4132 case EQ:
4133 if (cond2 == EQ || ! cond_or)
4134 return CC_DEQmode;
4136 switch (cond2)
4138 case LE: return CC_DLEmode;
4139 case LEU: return CC_DLEUmode;
4140 case GE: return CC_DGEmode;
4141 case GEU: return CC_DGEUmode;
4142 default: break;
4145 break;
4147 case LT:
4148 if (cond2 == LT || ! cond_or)
4149 return CC_DLTmode;
4150 if (cond2 == LE)
4151 return CC_DLEmode;
4152 if (cond2 == NE)
4153 return CC_DNEmode;
4154 break;
4156 case GT:
4157 if (cond2 == GT || ! cond_or)
4158 return CC_DGTmode;
4159 if (cond2 == GE)
4160 return CC_DGEmode;
4161 if (cond2 == NE)
4162 return CC_DNEmode;
4163 break;
4165 case LTU:
4166 if (cond2 == LTU || ! cond_or)
4167 return CC_DLTUmode;
4168 if (cond2 == LEU)
4169 return CC_DLEUmode;
4170 if (cond2 == NE)
4171 return CC_DNEmode;
4172 break;
4174 case GTU:
4175 if (cond2 == GTU || ! cond_or)
4176 return CC_DGTUmode;
4177 if (cond2 == GEU)
4178 return CC_DGEUmode;
4179 if (cond2 == NE)
4180 return CC_DNEmode;
4181 break;
4183 /* The remaining cases only occur when both comparisons are the
4184 same. */
4185 case NE:
4186 return CC_DNEmode;
4188 case LE:
4189 return CC_DLEmode;
4191 case GE:
4192 return CC_DGEmode;
4194 case LEU:
4195 return CC_DLEUmode;
4197 case GEU:
4198 return CC_DGEUmode;
4200 default:
4201 break;
4204 abort ();
4207 enum machine_mode
4208 arm_select_cc_mode (op, x, y)
4209 enum rtx_code op;
4210 rtx x;
4211 rtx y;
4213 /* All floating point compares return CCFP if it is an equality
4214 comparison, and CCFPE otherwise. */
4215 if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
4216 return (op == EQ || op == NE) ? CCFPmode : CCFPEmode;
4218 /* A compare with a shifted operand. Because of canonicalization, the
4219 comparison will have to be swapped when we emit the assembler. */
4220 if (GET_MODE (y) == SImode && GET_CODE (y) == REG
4221 && (GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
4222 || GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ROTATE
4223 || GET_CODE (x) == ROTATERT))
4224 return CC_SWPmode;
4226 /* This is a special case that is used by combine to allow a
4227 comparison of a shifted byte load to be split into a zero-extend
4228 followed by a comparison of the shifted integer (only valid for
4229 equalities and unsigned inequalities). */
4230 if (GET_MODE (x) == SImode
4231 && GET_CODE (x) == ASHIFT
4232 && GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 24
4233 && GET_CODE (XEXP (x, 0)) == SUBREG
4234 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == MEM
4235 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == QImode
4236 && (op == EQ || op == NE
4237 || op == GEU || op == GTU || op == LTU || op == LEU)
4238 && GET_CODE (y) == CONST_INT)
4239 return CC_Zmode;
4241 /* An operation that sets the condition codes as a side-effect, the
4242 V flag is not set correctly, so we can only use comparisons where
4243 this doesn't matter. (For LT and GE we can use "mi" and "pl"
4244 instead. */
4245 if (GET_MODE (x) == SImode
4246 && y == const0_rtx
4247 && (op == EQ || op == NE || op == LT || op == GE)
4248 && (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
4249 || GET_CODE (x) == AND || GET_CODE (x) == IOR
4250 || GET_CODE (x) == XOR || GET_CODE (x) == MULT
4251 || GET_CODE (x) == NOT || GET_CODE (x) == NEG
4252 || GET_CODE (x) == LSHIFTRT
4253 || GET_CODE (x) == ASHIFT || GET_CODE (x) == ASHIFTRT
4254 || GET_CODE (x) == ROTATERT || GET_CODE (x) == ZERO_EXTRACT))
4255 return CC_NOOVmode;
4257 /* A construct for a conditional compare, if the false arm contains
4258 0, then both conditions must be true, otherwise either condition
4259 must be true. Not all conditions are possible, so CCmode is
4260 returned if it can't be done. */
4261 if (GET_CODE (x) == IF_THEN_ELSE
4262 && (XEXP (x, 2) == const0_rtx
4263 || XEXP (x, 2) == const1_rtx)
4264 && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
4265 && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) == '<')
4266 return select_dominance_cc_mode (XEXP (x, 0), XEXP (x, 1),
4267 INTVAL (XEXP (x, 2)));
4269 if (GET_MODE (x) == QImode && (op == EQ || op == NE))
4270 return CC_Zmode;
4272 if (GET_MODE (x) == SImode && (op == LTU || op == GEU)
4273 && GET_CODE (x) == PLUS
4274 && (rtx_equal_p (XEXP (x, 0), y) || rtx_equal_p (XEXP (x, 1), y)))
4275 return CC_Cmode;
4277 return CCmode;
4280 /* X and Y are two things to compare using CODE. Emit the compare insn and
4281 return the rtx for register 0 in the proper mode. FP means this is a
4282 floating point compare: I don't think that it is needed on the arm. */
4285 arm_gen_compare_reg (code, x, y)
4286 enum rtx_code code;
4287 rtx x, y;
4289 enum machine_mode mode = SELECT_CC_MODE (code, x, y);
4290 rtx cc_reg = gen_rtx_REG (mode, CC_REGNUM);
4292 emit_insn (gen_rtx_SET (VOIDmode, cc_reg,
4293 gen_rtx_COMPARE (mode, x, y)));
4295 return cc_reg;
4298 void
4299 arm_reload_in_hi (operands)
4300 rtx * operands;
4302 rtx ref = operands[1];
4303 rtx base, scratch;
4304 HOST_WIDE_INT offset = 0;
4306 if (GET_CODE (ref) == SUBREG)
4308 offset = SUBREG_WORD (ref) * UNITS_PER_WORD;
4309 if (BYTES_BIG_ENDIAN)
4310 offset -= (MIN (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (ref)))
4311 - MIN (UNITS_PER_WORD,
4312 GET_MODE_SIZE (GET_MODE (SUBREG_REG (ref)))));
4313 ref = SUBREG_REG (ref);
4316 if (GET_CODE (ref) == REG)
4318 /* We have a pseudo which has been spilt onto the stack; there
4319 are two cases here: the first where there is a simple
4320 stack-slot replacement and a second where the stack-slot is
4321 out of range, or is used as a subreg. */
4322 if (reg_equiv_mem[REGNO (ref)])
4324 ref = reg_equiv_mem[REGNO (ref)];
4325 base = find_replacement (&XEXP (ref, 0));
4327 else
4328 /* The slot is out of range, or was dressed up in a SUBREG. */
4329 base = reg_equiv_address[REGNO (ref)];
4331 else
4332 base = find_replacement (&XEXP (ref, 0));
4334 /* Handle the case where the address is too complex to be offset by 1. */
4335 if (GET_CODE (base) == MINUS
4336 || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
4338 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
4340 emit_insn (gen_rtx_SET (VOIDmode, base_plus, base));
4341 base = base_plus;
4343 else if (GET_CODE (base) == PLUS)
4345 /* The addend must be CONST_INT, or we would have dealt with it above. */
4346 HOST_WIDE_INT hi, lo;
4348 offset += INTVAL (XEXP (base, 1));
4349 base = XEXP (base, 0);
4351 /* Rework the address into a legal sequence of insns. */
4352 /* Valid range for lo is -4095 -> 4095 */
4353 lo = (offset >= 0
4354 ? (offset & 0xfff)
4355 : -((-offset) & 0xfff));
4357 /* Corner case, if lo is the max offset then we would be out of range
4358 once we have added the additional 1 below, so bump the msb into the
4359 pre-loading insn(s). */
4360 if (lo == 4095)
4361 lo &= 0x7ff;
4363 hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
4364 ^ (HOST_WIDE_INT) 0x80000000)
4365 - (HOST_WIDE_INT) 0x80000000);
4367 if (hi + lo != offset)
4368 abort ();
4370 if (hi != 0)
4372 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
4374 /* Get the base address; addsi3 knows how to handle constants
4375 that require more than one insn. */
4376 emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
4377 base = base_plus;
4378 offset = lo;
4382 scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
4383 emit_insn (gen_zero_extendqisi2 (scratch,
4384 gen_rtx_MEM (QImode,
4385 plus_constant (base,
4386 offset))));
4387 emit_insn (gen_zero_extendqisi2 (gen_rtx_SUBREG (SImode, operands[0], 0),
4388 gen_rtx_MEM (QImode,
4389 plus_constant (base,
4390 offset + 1))));
4391 if (! BYTES_BIG_ENDIAN)
4392 emit_insn (gen_rtx_SET (VOIDmode, gen_rtx_SUBREG (SImode, operands[0], 0),
4393 gen_rtx_IOR (SImode,
4394 gen_rtx_ASHIFT
4395 (SImode,
4396 gen_rtx_SUBREG (SImode, operands[0], 0),
4397 GEN_INT (8)),
4398 scratch)));
4399 else
4400 emit_insn (gen_rtx_SET (VOIDmode, gen_rtx_SUBREG (SImode, operands[0], 0),
4401 gen_rtx_IOR (SImode,
4402 gen_rtx_ASHIFT (SImode, scratch,
4403 GEN_INT (8)),
4404 gen_rtx_SUBREG (SImode, operands[0],
4405 0))));
4408 /* Handle storing a half-word to memory during reload by synthesising as two
4409 byte stores. Take care not to clobber the input values until after we
4410 have moved them somewhere safe. This code assumes that if the DImode
4411 scratch in operands[2] overlaps either the input value or output address
4412 in some way, then that value must die in this insn (we absolutely need
4413 two scratch registers for some corner cases). */
4414 void
4415 arm_reload_out_hi (operands)
4416 rtx * operands;
4418 rtx ref = operands[0];
4419 rtx outval = operands[1];
4420 rtx base, scratch;
4421 HOST_WIDE_INT offset = 0;
4423 if (GET_CODE (ref) == SUBREG)
4425 offset = SUBREG_WORD (ref) * UNITS_PER_WORD;
4426 if (BYTES_BIG_ENDIAN)
4427 offset -= (MIN (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (ref)))
4428 - MIN (UNITS_PER_WORD,
4429 GET_MODE_SIZE (GET_MODE (SUBREG_REG (ref)))));
4430 ref = SUBREG_REG (ref);
4434 if (GET_CODE (ref) == REG)
4436 /* We have a pseudo which has been spilt onto the stack; there
4437 are two cases here: the first where there is a simple
4438 stack-slot replacement and a second where the stack-slot is
4439 out of range, or is used as a subreg. */
4440 if (reg_equiv_mem[REGNO (ref)])
4442 ref = reg_equiv_mem[REGNO (ref)];
4443 base = find_replacement (&XEXP (ref, 0));
4445 else
4446 /* The slot is out of range, or was dressed up in a SUBREG. */
4447 base = reg_equiv_address[REGNO (ref)];
4449 else
4450 base = find_replacement (&XEXP (ref, 0));
4452 scratch = gen_rtx_REG (SImode, REGNO (operands[2]));
4454 /* Handle the case where the address is too complex to be offset by 1. */
4455 if (GET_CODE (base) == MINUS
4456 || (GET_CODE (base) == PLUS && GET_CODE (XEXP (base, 1)) != CONST_INT))
4458 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
4460 /* Be careful not to destroy OUTVAL. */
4461 if (reg_overlap_mentioned_p (base_plus, outval))
4463 /* Updating base_plus might destroy outval, see if we can
4464 swap the scratch and base_plus. */
4465 if (! reg_overlap_mentioned_p (scratch, outval))
4467 rtx tmp = scratch;
4468 scratch = base_plus;
4469 base_plus = tmp;
4471 else
4473 rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
4475 /* Be conservative and copy OUTVAL into the scratch now,
4476 this should only be necessary if outval is a subreg
4477 of something larger than a word. */
4478 /* XXX Might this clobber base? I can't see how it can,
4479 since scratch is known to overlap with OUTVAL, and
4480 must be wider than a word. */
4481 emit_insn (gen_movhi (scratch_hi, outval));
4482 outval = scratch_hi;
4486 emit_insn (gen_rtx_SET (VOIDmode, base_plus, base));
4487 base = base_plus;
4489 else if (GET_CODE (base) == PLUS)
4491 /* The addend must be CONST_INT, or we would have dealt with it above. */
4492 HOST_WIDE_INT hi, lo;
4494 offset += INTVAL (XEXP (base, 1));
4495 base = XEXP (base, 0);
4497 /* Rework the address into a legal sequence of insns. */
4498 /* Valid range for lo is -4095 -> 4095 */
4499 lo = (offset >= 0
4500 ? (offset & 0xfff)
4501 : -((-offset) & 0xfff));
4503 /* Corner case, if lo is the max offset then we would be out of range
4504 once we have added the additional 1 below, so bump the msb into the
4505 pre-loading insn(s). */
4506 if (lo == 4095)
4507 lo &= 0x7ff;
4509 hi = ((((offset - lo) & (HOST_WIDE_INT) 0xffffffff)
4510 ^ (HOST_WIDE_INT) 0x80000000)
4511 - (HOST_WIDE_INT) 0x80000000);
4513 if (hi + lo != offset)
4514 abort ();
4516 if (hi != 0)
4518 rtx base_plus = gen_rtx_REG (SImode, REGNO (operands[2]) + 1);
4520 /* Be careful not to destroy OUTVAL. */
4521 if (reg_overlap_mentioned_p (base_plus, outval))
4523 /* Updating base_plus might destroy outval, see if we
4524 can swap the scratch and base_plus. */
4525 if (! reg_overlap_mentioned_p (scratch, outval))
4527 rtx tmp = scratch;
4528 scratch = base_plus;
4529 base_plus = tmp;
4531 else
4533 rtx scratch_hi = gen_rtx_REG (HImode, REGNO (operands[2]));
4535 /* Be conservative and copy outval into scratch now,
4536 this should only be necessary if outval is a
4537 subreg of something larger than a word. */
4538 /* XXX Might this clobber base? I can't see how it
4539 can, since scratch is known to overlap with
4540 outval. */
4541 emit_insn (gen_movhi (scratch_hi, outval));
4542 outval = scratch_hi;
4546 /* Get the base address; addsi3 knows how to handle constants
4547 that require more than one insn. */
4548 emit_insn (gen_addsi3 (base_plus, base, GEN_INT (hi)));
4549 base = base_plus;
4550 offset = lo;
4554 if (BYTES_BIG_ENDIAN)
4556 emit_insn (gen_movqi (gen_rtx_MEM (QImode,
4557 plus_constant (base, offset + 1)),
4558 gen_rtx_SUBREG (QImode, outval, 0)));
4559 emit_insn (gen_lshrsi3 (scratch,
4560 gen_rtx_SUBREG (SImode, outval, 0),
4561 GEN_INT (8)));
4562 emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
4563 gen_rtx_SUBREG (QImode, scratch, 0)));
4565 else
4567 emit_insn (gen_movqi (gen_rtx_MEM (QImode, plus_constant (base, offset)),
4568 gen_rtx_SUBREG (QImode, outval, 0)));
4569 emit_insn (gen_lshrsi3 (scratch,
4570 gen_rtx_SUBREG (SImode, outval, 0),
4571 GEN_INT (8)));
4572 emit_insn (gen_movqi (gen_rtx_MEM (QImode,
4573 plus_constant (base, offset + 1)),
4574 gen_rtx_SUBREG (QImode, scratch, 0)));
4578 /* Print a symbolic form of X to the debug file, F. */
4579 static void
4580 arm_print_value (f, x)
4581 FILE * f;
4582 rtx x;
4584 switch (GET_CODE (x))
4586 case CONST_INT:
4587 fprintf (f, HOST_WIDE_INT_PRINT_HEX, INTVAL (x));
4588 return;
4590 case CONST_DOUBLE:
4591 fprintf (f, "<0x%lx,0x%lx>", (long)XWINT (x, 2), (long)XWINT (x, 3));
4592 return;
4594 case CONST_STRING:
4595 fprintf (f, "\"%s\"", XSTR (x, 0));
4596 return;
4598 case SYMBOL_REF:
4599 fprintf (f, "`%s'", XSTR (x, 0));
4600 return;
4602 case LABEL_REF:
4603 fprintf (f, "L%d", INSN_UID (XEXP (x, 0)));
4604 return;
4606 case CONST:
4607 arm_print_value (f, XEXP (x, 0));
4608 return;
4610 case PLUS:
4611 arm_print_value (f, XEXP (x, 0));
4612 fprintf (f, "+");
4613 arm_print_value (f, XEXP (x, 1));
4614 return;
4616 case PC:
4617 fprintf (f, "pc");
4618 return;
4620 default:
4621 fprintf (f, "????");
4622 return;
4626 /* Routines for manipulation of the constant pool. */
4628 /* Arm instructions cannot load a large constant directly into a
4629 register; they have to come from a pc relative load. The constant
4630 must therefore be placed in the addressable range of the pc
4631 relative load. Depending on the precise pc relative load
4632 instruction the range is somewhere between 256 bytes and 4k. This
4633 means that we often have to dump a constant inside a function, and
4634 generate code to branch around it.
4636 It is important to minimize this, since the branches will slow
4637 things down and make the code larger.
4639 Normally we can hide the table after an existing unconditional
4640 branch so that there is no interruption of the flow, but in the
4641 worst case the code looks like this:
4643 ldr rn, L1
4645 b L2
4646 align
4647 L1: .long value
4651 ldr rn, L3
4653 b L4
4654 align
4655 L3: .long value
4659 We fix this by performing a scan after scheduling, which notices
4660 which instructions need to have their operands fetched from the
4661 constant table and builds the table.
4663 The algorithm starts by building a table of all the constants that
4664 need fixing up and all the natural barriers in the function (places
4665 where a constant table can be dropped without breaking the flow).
4666 For each fixup we note how far the pc-relative replacement will be
4667 able to reach and the offset of the instruction into the function.
4669 Having built the table we then group the fixes together to form
4670 tables that are as large as possible (subject to addressing
4671 constraints) and emit each table of constants after the last
4672 barrier that is within range of all the instructions in the group.
4673 If a group does not contain a barrier, then we forcibly create one
4674 by inserting a jump instruction into the flow. Once the table has
4675 been inserted, the insns are then modified to reference the
4676 relevant entry in the pool.
4678 Possible enhancements to the algorithm (not implemented) are:
4680 1) For some processors and object formats, there may be benefit in
4681 aligning the pools to the start of cache lines; this alignment
4682 would need to be taken into account when calculating addressability
4683 of a pool. */
4685 /* These typedefs are located at the start of this file, so that
4686 they can be used in the prototypes there. This comment is to
4687 remind readers of that fact so that the following structures
4688 can be understood more easily.
4690 typedef struct minipool_node Mnode;
4691 typedef struct minipool_fixup Mfix; */
4693 struct minipool_node
4695 /* Doubly linked chain of entries. */
4696 Mnode * next;
4697 Mnode * prev;
4698 /* The maximum offset into the code that this entry can be placed. While
4699 pushing fixes for forward references, all entries are sorted in order
4700 of increasing max_address. */
4701 HOST_WIDE_INT max_address;
4702 /* Similarly for a entry inserted for a backwards ref. */
4703 HOST_WIDE_INT min_address;
4704 /* The number of fixes referencing this entry. This can become zero
4705 if we "unpush" an entry. In this case we ignore the entry when we
4706 come to emit the code. */
4707 int refcount;
4708 /* The offset from the start of the minipool. */
4709 HOST_WIDE_INT offset;
4710 /* The value in table. */
4711 rtx value;
4712 /* The mode of value. */
4713 enum machine_mode mode;
4714 int fix_size;
4717 struct minipool_fixup
4719 Mfix * next;
4720 rtx insn;
4721 HOST_WIDE_INT address;
4722 rtx * loc;
4723 enum machine_mode mode;
4724 int fix_size;
4725 rtx value;
4726 Mnode * minipool;
4727 HOST_WIDE_INT forwards;
4728 HOST_WIDE_INT backwards;
4731 /* Fixes less than a word need padding out to a word boundary. */
4732 #define MINIPOOL_FIX_SIZE(mode) \
4733 (GET_MODE_SIZE ((mode)) >= 4 ? GET_MODE_SIZE ((mode)) : 4)
4735 static Mnode * minipool_vector_head;
4736 static Mnode * minipool_vector_tail;
4737 static rtx minipool_vector_label;
4739 /* The linked list of all minipool fixes required for this function. */
4740 Mfix * minipool_fix_head;
4741 Mfix * minipool_fix_tail;
4742 /* The fix entry for the current minipool, once it has been placed. */
4743 Mfix * minipool_barrier;
4745 /* Determines if INSN is the start of a jump table. Returns the end
4746 of the TABLE or NULL_RTX. */
4747 static rtx
4748 is_jump_table (insn)
4749 rtx insn;
4751 rtx table;
4753 if (GET_CODE (insn) == JUMP_INSN
4754 && JUMP_LABEL (insn) != NULL
4755 && ((table = next_real_insn (JUMP_LABEL (insn)))
4756 == next_real_insn (insn))
4757 && table != NULL
4758 && GET_CODE (table) == JUMP_INSN
4759 && (GET_CODE (PATTERN (table)) == ADDR_VEC
4760 || GET_CODE (PATTERN (table)) == ADDR_DIFF_VEC))
4761 return table;
4763 return NULL_RTX;
4766 static HOST_WIDE_INT
4767 get_jump_table_size (insn)
4768 rtx insn;
4770 rtx body = PATTERN (insn);
4771 int elt = GET_CODE (body) == ADDR_DIFF_VEC ? 1 : 0;
4773 return GET_MODE_SIZE (GET_MODE (body)) * XVECLEN (body, elt);
4776 /* Move a minipool fix MP from its current location to before MAX_MP.
4777 If MAX_MP is NULL, then MP doesn't need moving, but the addressing
4778 contrains may need updating. */
4779 static Mnode *
4780 move_minipool_fix_forward_ref (mp, max_mp, max_address)
4781 Mnode * mp;
4782 Mnode * max_mp;
4783 HOST_WIDE_INT max_address;
4785 /* This should never be true and the code below assumes these are
4786 different. */
4787 if (mp == max_mp)
4788 abort ();
4790 if (max_mp == NULL)
4792 if (max_address < mp->max_address)
4793 mp->max_address = max_address;
4795 else
4797 if (max_address > max_mp->max_address - mp->fix_size)
4798 mp->max_address = max_mp->max_address - mp->fix_size;
4799 else
4800 mp->max_address = max_address;
4802 /* Unlink MP from its current position. Since max_mp is non-null,
4803 mp->prev must be non-null. */
4804 mp->prev->next = mp->next;
4805 if (mp->next != NULL)
4806 mp->next->prev = mp->prev;
4807 else
4808 minipool_vector_tail = mp->prev;
4810 /* Re-insert it before MAX_MP. */
4811 mp->next = max_mp;
4812 mp->prev = max_mp->prev;
4813 max_mp->prev = mp;
4815 if (mp->prev != NULL)
4816 mp->prev->next = mp;
4817 else
4818 minipool_vector_head = mp;
4821 /* Save the new entry. */
4822 max_mp = mp;
4824 /* Scan over the preceeding entries and adjust their addresses as
4825 required. */
4826 while (mp->prev != NULL
4827 && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
4829 mp->prev->max_address = mp->max_address - mp->prev->fix_size;
4830 mp = mp->prev;
4833 return max_mp;
4836 /* Add a constant to the minipool for a forward reference. Returns the
4837 node added or NULL if the constant will not fit in this pool. */
4838 static Mnode *
4839 add_minipool_forward_ref (fix)
4840 Mfix * fix;
4842 /* If set, max_mp is the first pool_entry that has a lower
4843 constraint than the one we are trying to add. */
4844 Mnode * max_mp = NULL;
4845 HOST_WIDE_INT max_address = fix->address + fix->forwards;
4846 Mnode * mp;
4848 /* If this fix's address is greater than the address of the first
4849 entry, then we can't put the fix in this pool. We subtract the
4850 size of the current fix to ensure that if the table is fully
4851 packed we still have enough room to insert this value by suffling
4852 the other fixes forwards. */
4853 if (minipool_vector_head &&
4854 fix->address >= minipool_vector_head->max_address - fix->fix_size)
4855 return NULL;
4857 /* Scan the pool to see if a constant with the same value has
4858 already been added. While we are doing this, also note the
4859 location where we must insert the constant if it doesn't already
4860 exist. */
4861 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
4863 if (GET_CODE (fix->value) == GET_CODE (mp->value)
4864 && fix->mode == mp->mode
4865 && (GET_CODE (fix->value) != CODE_LABEL
4866 || (CODE_LABEL_NUMBER (fix->value)
4867 == CODE_LABEL_NUMBER (mp->value)))
4868 && rtx_equal_p (fix->value, mp->value))
4870 /* More than one fix references this entry. */
4871 mp->refcount++;
4872 return move_minipool_fix_forward_ref (mp, max_mp, max_address);
4875 /* Note the insertion point if necessary. */
4876 if (max_mp == NULL
4877 && mp->max_address > max_address)
4878 max_mp = mp;
4881 /* The value is not currently in the minipool, so we need to create
4882 a new entry for it. If MAX_MP is NULL, the entry will be put on
4883 the end of the list since the placement is less constrained than
4884 any existing entry. Otherwise, we insert the new fix before
4885 MAX_MP and, if neceesary, adjust the constraints on the other
4886 entries. */
4887 mp = xmalloc (sizeof (* mp));
4888 mp->fix_size = fix->fix_size;
4889 mp->mode = fix->mode;
4890 mp->value = fix->value;
4891 mp->refcount = 1;
4892 /* Not yet required for a backwards ref. */
4893 mp->min_address = -65536;
4895 if (max_mp == NULL)
4897 mp->max_address = max_address;
4898 mp->next = NULL;
4899 mp->prev = minipool_vector_tail;
4901 if (mp->prev == NULL)
4903 minipool_vector_head = mp;
4904 minipool_vector_label = gen_label_rtx ();
4906 else
4907 mp->prev->next = mp;
4909 minipool_vector_tail = mp;
4911 else
4913 if (max_address > max_mp->max_address - mp->fix_size)
4914 mp->max_address = max_mp->max_address - mp->fix_size;
4915 else
4916 mp->max_address = max_address;
4918 mp->next = max_mp;
4919 mp->prev = max_mp->prev;
4920 max_mp->prev = mp;
4921 if (mp->prev != NULL)
4922 mp->prev->next = mp;
4923 else
4924 minipool_vector_head = mp;
4927 /* Save the new entry. */
4928 max_mp = mp;
4930 /* Scan over the preceeding entries and adjust their addresses as
4931 required. */
4932 while (mp->prev != NULL
4933 && mp->prev->max_address > mp->max_address - mp->prev->fix_size)
4935 mp->prev->max_address = mp->max_address - mp->prev->fix_size;
4936 mp = mp->prev;
4939 return max_mp;
4942 static Mnode *
4943 move_minipool_fix_backward_ref (mp, min_mp, min_address)
4944 Mnode * mp;
4945 Mnode * min_mp;
4946 HOST_WIDE_INT min_address;
4948 HOST_WIDE_INT offset;
4950 /* This should never be true, and the code below assumes these are
4951 different. */
4952 if (mp == min_mp)
4953 abort ();
4955 if (min_mp == NULL)
4957 if (min_address > mp->min_address)
4958 mp->min_address = min_address;
4960 else
4962 /* We will adjust this below if it is too loose. */
4963 mp->min_address = min_address;
4965 /* Unlink MP from its current position. Since min_mp is non-null,
4966 mp->next must be non-null. */
4967 mp->next->prev = mp->prev;
4968 if (mp->prev != NULL)
4969 mp->prev->next = mp->next;
4970 else
4971 minipool_vector_head = mp->next;
4973 /* Reinsert it after MIN_MP. */
4974 mp->prev = min_mp;
4975 mp->next = min_mp->next;
4976 min_mp->next = mp;
4977 if (mp->next != NULL)
4978 mp->next->prev = mp;
4979 else
4980 minipool_vector_tail = mp;
4983 min_mp = mp;
4985 offset = 0;
4986 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
4988 mp->offset = offset;
4989 if (mp->refcount > 0)
4990 offset += mp->fix_size;
4992 if (mp->next && mp->next->min_address < mp->min_address + mp->fix_size)
4993 mp->next->min_address = mp->min_address + mp->fix_size;
4996 return min_mp;
4999 /* Add a constant to the minipool for a backward reference. Returns the
5000 node added or NULL if the constant will not fit in this pool.
5002 Note that the code for insertion for a backwards reference can be
5003 somewhat confusing because the calculated offsets for each fix do
5004 not take into account the size of the pool (which is still under
5005 construction. */
5006 static Mnode *
5007 add_minipool_backward_ref (fix)
5008 Mfix * fix;
5010 /* If set, min_mp is the last pool_entry that has a lower constraint
5011 than the one we are trying to add. */
5012 Mnode * min_mp = NULL;
5013 /* This can be negative, since it is only a constraint. */
5014 HOST_WIDE_INT min_address = fix->address - fix->backwards;
5015 Mnode * mp;
5017 /* If we can't reach the current pool from this insn, or if we can't
5018 insert this entry at the end of the pool without pushing other
5019 fixes out of range, then we don't try. This ensures that we
5020 can't fail later on. */
5021 if (min_address >= minipool_barrier->address
5022 || (minipool_vector_tail->min_address + fix->fix_size
5023 >= minipool_barrier->address))
5024 return NULL;
5026 /* Scan the pool to see if a constant with the same value has
5027 already been added. While we are doing this, also note the
5028 location where we must insert the constant if it doesn't already
5029 exist. */
5030 for (mp = minipool_vector_tail; mp != NULL; mp = mp->prev)
5032 if (GET_CODE (fix->value) == GET_CODE (mp->value)
5033 && fix->mode == mp->mode
5034 && (GET_CODE (fix->value) != CODE_LABEL
5035 || (CODE_LABEL_NUMBER (fix->value)
5036 == CODE_LABEL_NUMBER (mp->value)))
5037 && rtx_equal_p (fix->value, mp->value)
5038 /* Check that there is enough slack to move this entry to the
5039 end of the table (this is conservative). */
5040 && (mp->max_address
5041 > (minipool_barrier->address
5042 + minipool_vector_tail->offset
5043 + minipool_vector_tail->fix_size)))
5045 mp->refcount++;
5046 return move_minipool_fix_backward_ref (mp, min_mp, min_address);
5049 if (min_mp != NULL)
5050 mp->min_address += fix->fix_size;
5051 else
5053 /* Note the insertion point if necessary. */
5054 if (mp->min_address < min_address)
5055 min_mp = mp;
5056 else if (mp->max_address
5057 < minipool_barrier->address + mp->offset + fix->fix_size)
5059 /* Inserting before this entry would push the fix beyond
5060 its maximum address (which can happen if we have
5061 re-located a forwards fix); force the new fix to come
5062 after it. */
5063 min_mp = mp;
5064 min_address = mp->min_address + fix->fix_size;
5069 /* We need to create a new entry. */
5070 mp = xmalloc (sizeof (* mp));
5071 mp->fix_size = fix->fix_size;
5072 mp->mode = fix->mode;
5073 mp->value = fix->value;
5074 mp->refcount = 1;
5075 mp->max_address = minipool_barrier->address + 65536;
5077 mp->min_address = min_address;
5079 if (min_mp == NULL)
5081 mp->prev = NULL;
5082 mp->next = minipool_vector_head;
5084 if (mp->next == NULL)
5086 minipool_vector_tail = mp;
5087 minipool_vector_label = gen_label_rtx ();
5089 else
5090 mp->next->prev = mp;
5092 minipool_vector_head = mp;
5094 else
5096 mp->next = min_mp->next;
5097 mp->prev = min_mp;
5098 min_mp->next = mp;
5100 if (mp->next != NULL)
5101 mp->next->prev = mp;
5102 else
5103 minipool_vector_tail = mp;
5106 /* Save the new entry. */
5107 min_mp = mp;
5109 if (mp->prev)
5110 mp = mp->prev;
5111 else
5112 mp->offset = 0;
5114 /* Scan over the following entries and adjust their offsets. */
5115 while (mp->next != NULL)
5117 if (mp->next->min_address < mp->min_address + mp->fix_size)
5118 mp->next->min_address = mp->min_address + mp->fix_size;
5120 if (mp->refcount)
5121 mp->next->offset = mp->offset + mp->fix_size;
5122 else
5123 mp->next->offset = mp->offset;
5125 mp = mp->next;
5128 return min_mp;
5131 static void
5132 assign_minipool_offsets (barrier)
5133 Mfix * barrier;
5135 HOST_WIDE_INT offset = 0;
5136 Mnode * mp;
5138 minipool_barrier = barrier;
5140 for (mp = minipool_vector_head; mp != NULL; mp = mp->next)
5142 mp->offset = offset;
5144 if (mp->refcount > 0)
5145 offset += mp->fix_size;
5149 /* Output the literal table */
5150 static void
5151 dump_minipool (scan)
5152 rtx scan;
5154 Mnode * mp;
5155 Mnode * nmp;
5157 if (rtl_dump_file)
5158 fprintf (rtl_dump_file,
5159 ";; Emitting minipool after insn %u; address %ld\n",
5160 INSN_UID (scan), (unsigned long) minipool_barrier->address);
5162 scan = emit_label_after (gen_label_rtx (), scan);
5163 scan = emit_insn_after (gen_align_4 (), scan);
5164 scan = emit_label_after (minipool_vector_label, scan);
5166 for (mp = minipool_vector_head; mp != NULL; mp = nmp)
5168 if (mp->refcount > 0)
5170 if (rtl_dump_file)
5172 fprintf (rtl_dump_file,
5173 ";; Offset %u, min %ld, max %ld ",
5174 (unsigned) mp->offset, (unsigned long) mp->min_address,
5175 (unsigned long) mp->max_address);
5176 arm_print_value (rtl_dump_file, mp->value);
5177 fputc ('\n', rtl_dump_file);
5180 switch (mp->fix_size)
5182 #ifdef HAVE_consttable_1
5183 case 1:
5184 scan = emit_insn_after (gen_consttable_1 (mp->value), scan);
5185 break;
5187 #endif
5188 #ifdef HAVE_consttable_2
5189 case 2:
5190 scan = emit_insn_after (gen_consttable_2 (mp->value), scan);
5191 break;
5193 #endif
5194 #ifdef HAVE_consttable_4
5195 case 4:
5196 scan = emit_insn_after (gen_consttable_4 (mp->value), scan);
5197 break;
5199 #endif
5200 #ifdef HAVE_consttable_8
5201 case 8:
5202 scan = emit_insn_after (gen_consttable_8 (mp->value), scan);
5203 break;
5205 #endif
5206 default:
5207 abort ();
5208 break;
5212 nmp = mp->next;
5213 free (mp);
5216 minipool_vector_head = minipool_vector_tail = NULL;
5217 scan = emit_insn_after (gen_consttable_end (), scan);
5218 scan = emit_barrier_after (scan);
5221 /* Return the cost of forcibly inserting a barrier after INSN. */
5222 static int
5223 arm_barrier_cost (insn)
5224 rtx insn;
5226 /* Basing the location of the pool on the loop depth is preferable,
5227 but at the moment, the basic block information seems to be
5228 corrupt by this stage of the compilation. */
5229 int base_cost = 50;
5230 rtx next = next_nonnote_insn (insn);
5232 if (next != NULL && GET_CODE (next) == CODE_LABEL)
5233 base_cost -= 20;
5235 switch (GET_CODE (insn))
5237 case CODE_LABEL:
5238 /* It will always be better to place the table before the label, rather
5239 than after it. */
5240 return 50;
5242 case INSN:
5243 case CALL_INSN:
5244 return base_cost;
5246 case JUMP_INSN:
5247 return base_cost - 10;
5249 default:
5250 return base_cost + 10;
5254 /* Find the best place in the insn stream in the range
5255 (FIX->address,MAX_ADDRESS) to forcibly insert a minipool barrier.
5256 Create the barrier by inserting a jump and add a new fix entry for
5257 it. */
5258 static Mfix *
5259 create_fix_barrier (fix, max_address)
5260 Mfix * fix;
5261 HOST_WIDE_INT max_address;
5263 HOST_WIDE_INT count = 0;
5264 rtx barrier;
5265 rtx from = fix->insn;
5266 rtx selected = from;
5267 int selected_cost;
5268 HOST_WIDE_INT selected_address;
5269 Mfix * new_fix;
5270 HOST_WIDE_INT max_count = max_address - fix->address;
5271 rtx label = gen_label_rtx ();
5273 selected_cost = arm_barrier_cost (from);
5274 selected_address = fix->address;
5276 while (from && count < max_count)
5278 rtx tmp;
5279 int new_cost;
5281 /* This code shouldn't have been called if there was a natural barrier
5282 within range. */
5283 if (GET_CODE (from) == BARRIER)
5284 abort ();
5286 /* Count the length of this insn. */
5287 count += get_attr_length (from);
5289 /* If there is a jump table, add its length. */
5290 tmp = is_jump_table (from);
5291 if (tmp != NULL)
5293 count += get_jump_table_size (tmp);
5295 /* Jump tables aren't in a basic block, so base the cost on
5296 the dispatch insn. If we select this location, we will
5297 still put the pool after the table. */
5298 new_cost = arm_barrier_cost (from);
5300 if (count < max_count && new_cost <= selected_cost)
5302 selected = tmp;
5303 selected_cost = new_cost;
5304 selected_address = fix->address + count;
5307 /* Continue after the dispatch table. */
5308 from = NEXT_INSN (tmp);
5309 continue;
5312 new_cost = arm_barrier_cost (from);
5314 if (count < max_count && new_cost <= selected_cost)
5316 selected = from;
5317 selected_cost = new_cost;
5318 selected_address = fix->address + count;
5321 from = NEXT_INSN (from);
5324 /* Create a new JUMP_INSN that branches around a barrier. */
5325 from = emit_jump_insn_after (gen_jump (label), selected);
5326 JUMP_LABEL (from) = label;
5327 barrier = emit_barrier_after (from);
5328 emit_label_after (label, barrier);
5330 /* Create a minipool barrier entry for the new barrier. */
5331 new_fix = (Mfix *) oballoc (sizeof (* new_fix));
5332 new_fix->insn = barrier;
5333 new_fix->address = selected_address;
5334 new_fix->next = fix->next;
5335 fix->next = new_fix;
5337 return new_fix;
5340 /* Record that there is a natural barrier in the insn stream at
5341 ADDRESS. */
5342 static void
5343 push_minipool_barrier (insn, address)
5344 rtx insn;
5345 HOST_WIDE_INT address;
5347 Mfix * fix = (Mfix *) oballoc (sizeof (* fix));
5349 fix->insn = insn;
5350 fix->address = address;
5352 fix->next = NULL;
5353 if (minipool_fix_head != NULL)
5354 minipool_fix_tail->next = fix;
5355 else
5356 minipool_fix_head = fix;
5358 minipool_fix_tail = fix;
5361 /* Record INSN, which will need fixing up to load a value from the
5362 minipool. ADDRESS is the offset of the insn since the start of the
5363 function; LOC is a pointer to the part of the insn which requires
5364 fixing; VALUE is the constant that must be loaded, which is of type
5365 MODE. */
5366 static void
5367 push_minipool_fix (insn, address, loc, mode, value)
5368 rtx insn;
5369 HOST_WIDE_INT address;
5370 rtx * loc;
5371 enum machine_mode mode;
5372 rtx value;
5374 Mfix * fix = (Mfix *) oballoc (sizeof (* fix));
5376 #ifdef AOF_ASSEMBLER
5377 /* PIC symbol refereneces need to be converted into offsets into the
5378 based area. */
5379 /* XXX This shouldn't be done here. */
5380 if (flag_pic && GET_CODE (value) == SYMBOL_REF)
5381 value = aof_pic_entry (value);
5382 #endif /* AOF_ASSEMBLER */
5384 fix->insn = insn;
5385 fix->address = address;
5386 fix->loc = loc;
5387 fix->mode = mode;
5388 fix->fix_size = MINIPOOL_FIX_SIZE (mode);
5389 fix->value = value;
5390 fix->forwards = get_attr_pool_range (insn);
5391 fix->backwards = get_attr_neg_pool_range (insn);
5392 fix->minipool = NULL;
5394 /* If an insn doesn't have a range defined for it, then it isn't
5395 expecting to be reworked by this code. Better to abort now than
5396 to generate duff assembly code. */
5397 if (fix->forwards == 0 && fix->backwards == 0)
5398 abort ();
5400 if (rtl_dump_file)
5402 fprintf (rtl_dump_file,
5403 ";; %smode fixup for i%d; addr %lu, range (%ld,%ld): ",
5404 GET_MODE_NAME (mode),
5405 INSN_UID (insn), (unsigned long) address,
5406 -1 * (long)fix->backwards, (long)fix->forwards);
5407 arm_print_value (rtl_dump_file, fix->value);
5408 fprintf (rtl_dump_file, "\n");
5411 /* Add it to the chain of fixes. */
5412 fix->next = NULL;
5414 if (minipool_fix_head != NULL)
5415 minipool_fix_tail->next = fix;
5416 else
5417 minipool_fix_head = fix;
5419 minipool_fix_tail = fix;
5422 /* Scan INSN and note any of its operands that need fixing. */
5423 static void
5424 note_invalid_constants (insn, address)
5425 rtx insn;
5426 HOST_WIDE_INT address;
5428 int opno;
5430 extract_insn (insn);
5432 if (! constrain_operands (1))
5433 fatal_insn_not_found (insn);
5435 /* Fill in recog_op_alt with information about the constraints of this
5436 insn. */
5437 preprocess_constraints ();
5439 for (opno = 0; opno < recog_data.n_operands; opno++)
5441 /* Things we need to fix can only occur in inputs. */
5442 if (recog_data.operand_type[opno] != OP_IN)
5443 continue;
5445 /* If this alternative is a memory reference, then any mention
5446 of constants in this alternative is really to fool reload
5447 into allowing us to accept one there. We need to fix them up
5448 now so that we output the right code. */
5449 if (recog_op_alt[opno][which_alternative].memory_ok)
5451 rtx op = recog_data.operand[opno];
5453 if (CONSTANT_P (op))
5454 push_minipool_fix (insn, address, recog_data.operand_loc[opno],
5455 recog_data.operand_mode[opno], op);
5456 #if 0
5457 /* RWE: Now we look correctly at the operands for the insn,
5458 this shouldn't be needed any more. */
5459 #ifndef AOF_ASSEMBLER
5460 /* XXX Is this still needed? */
5461 else if (GET_CODE (op) == UNSPEC && XINT (op, 1) == 3)
5462 push_minipool_fix (insn, address, recog_data.operand_loc[opno],
5463 recog_data.operand_mode[opno],
5464 XVECEXP (op, 0, 0));
5465 #endif
5466 #endif
5467 else if (GET_CODE (op) == MEM
5468 && GET_CODE (XEXP (op, 0)) == SYMBOL_REF
5469 && CONSTANT_POOL_ADDRESS_P (XEXP (op, 0)))
5470 push_minipool_fix (insn, address, recog_data.operand_loc[opno],
5471 recog_data.operand_mode[opno],
5472 get_pool_constant (XEXP (op, 0)));
5477 void
5478 arm_reorg (first)
5479 rtx first;
5481 rtx insn;
5482 HOST_WIDE_INT address = 0;
5483 Mfix * fix;
5485 minipool_fix_head = minipool_fix_tail = NULL;
5487 /* The first insn must always be a note, or the code below won't
5488 scan it properly. */
5489 if (GET_CODE (first) != NOTE)
5490 abort ();
5492 /* Scan all the insns and record the operands that will need fixing. */
5493 for (insn = next_nonnote_insn (first); insn; insn = next_nonnote_insn (insn))
5496 if (GET_CODE (insn) == BARRIER)
5497 push_minipool_barrier (insn, address);
5498 else if (GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN
5499 || GET_CODE (insn) == JUMP_INSN)
5501 rtx table;
5503 note_invalid_constants (insn, address);
5504 address += get_attr_length (insn);
5506 /* If the insn is a vector jump, add the size of the table
5507 and skip the table. */
5508 if ((table = is_jump_table (insn)) != NULL)
5510 address += get_jump_table_size (table);
5511 insn = table;
5516 fix = minipool_fix_head;
5518 /* Now scan the fixups and perform the required changes. */
5519 while (fix)
5521 Mfix * ftmp;
5522 Mfix * fdel;
5523 Mfix * last_added_fix;
5524 Mfix * last_barrier = NULL;
5525 Mfix * this_fix;
5527 /* Skip any further barriers before the next fix. */
5528 while (fix && GET_CODE (fix->insn) == BARRIER)
5529 fix = fix->next;
5531 /* No more fixes. */
5532 if (fix == NULL)
5533 break;
5535 last_added_fix = NULL;
5537 for (ftmp = fix; ftmp; ftmp = ftmp->next)
5539 if (GET_CODE (ftmp->insn) == BARRIER)
5541 if (ftmp->address >= minipool_vector_head->max_address)
5542 break;
5544 last_barrier = ftmp;
5546 else if ((ftmp->minipool = add_minipool_forward_ref (ftmp)) == NULL)
5547 break;
5549 last_added_fix = ftmp; /* Keep track of the last fix added. */
5552 /* If we found a barrier, drop back to that; any fixes that we
5553 could have reached but come after the barrier will now go in
5554 the next mini-pool. */
5555 if (last_barrier != NULL)
5557 /* Reduce the refcount for those fixes that won't go into this
5558 pool after all. */
5559 for (fdel = last_barrier->next;
5560 fdel && fdel != ftmp;
5561 fdel = fdel->next)
5563 fdel->minipool->refcount--;
5564 fdel->minipool = NULL;
5567 ftmp = last_barrier;
5569 else
5571 /* ftmp is first fix that we can't fit into this pool and
5572 there no natural barriers that we could use. Insert a
5573 new barrier in the code somewhere between the previous
5574 fix and this one, and arrange to jump around it. */
5575 HOST_WIDE_INT max_address;
5577 /* The last item on the list of fixes must be a barrier, so
5578 we can never run off the end of the list of fixes without
5579 last_barrier being set. */
5580 if (ftmp == NULL)
5581 abort ();
5583 max_address = minipool_vector_head->max_address;
5584 /* Check that there isn't another fix that is in range that
5585 we couldn't fit into this pool because the pool was
5586 already too large: we need to put the pool before such an
5587 instruction. */
5588 if (ftmp->address < max_address)
5589 max_address = ftmp->address;
5591 last_barrier = create_fix_barrier (last_added_fix, max_address);
5594 assign_minipool_offsets (last_barrier);
5596 while (ftmp)
5598 if (GET_CODE (ftmp->insn) != BARRIER
5599 && ((ftmp->minipool = add_minipool_backward_ref (ftmp))
5600 == NULL))
5601 break;
5603 ftmp = ftmp->next;
5606 /* Scan over the fixes we have identified for this pool, fixing them
5607 up and adding the constants to the pool itself. */
5608 for (this_fix = fix; this_fix && ftmp != this_fix;
5609 this_fix = this_fix->next)
5610 if (GET_CODE (this_fix->insn) != BARRIER)
5612 rtx addr
5613 = plus_constant (gen_rtx_LABEL_REF (VOIDmode,
5614 minipool_vector_label),
5615 this_fix->minipool->offset);
5616 *this_fix->loc = gen_rtx_MEM (this_fix->mode, addr);
5619 dump_minipool (last_barrier->insn);
5620 fix = ftmp;
5623 /* From now on we must synthesize any constants that we can't handle
5624 directly. This can happen if the RTL gets split during final
5625 instruction generation. */
5626 after_arm_reorg = 1;
5629 /* Routines to output assembly language. */
5631 /* If the rtx is the correct value then return the string of the number.
5632 In this way we can ensure that valid double constants are generated even
5633 when cross compiling. */
5634 char *
5635 fp_immediate_constant (x)
5636 rtx x;
5638 REAL_VALUE_TYPE r;
5639 int i;
5641 if (!fpa_consts_inited)
5642 init_fpa_table ();
5644 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
5645 for (i = 0; i < 8; i++)
5646 if (REAL_VALUES_EQUAL (r, values_fpa[i]))
5647 return strings_fpa[i];
5649 abort ();
5652 /* As for fp_immediate_constant, but value is passed directly, not in rtx. */
5653 static char *
5654 fp_const_from_val (r)
5655 REAL_VALUE_TYPE * r;
5657 int i;
5659 if (! fpa_consts_inited)
5660 init_fpa_table ();
5662 for (i = 0; i < 8; i++)
5663 if (REAL_VALUES_EQUAL (*r, values_fpa[i]))
5664 return strings_fpa[i];
5666 abort ();
5669 /* Output the operands of a LDM/STM instruction to STREAM.
5670 MASK is the ARM register set mask of which only bits 0-15 are important.
5671 INSTR is the possibly suffixed base register. HAT unequals zero if a hat
5672 must follow the register list. */
5674 static void
5675 print_multi_reg (stream, instr, reg, mask, hat)
5676 FILE * stream;
5677 char * instr;
5678 int reg;
5679 int mask;
5680 int hat;
5682 int i;
5683 int not_first = FALSE;
5685 fputc ('\t', stream);
5686 asm_fprintf (stream, instr, reg);
5687 fputs (", {", stream);
5689 for (i = 0; i <= LAST_ARM_REGNUM; i++)
5690 if (mask & (1 << i))
5692 if (not_first)
5693 fprintf (stream, ", ");
5695 asm_fprintf (stream, "%r", i);
5696 not_first = TRUE;
5699 fprintf (stream, "}%s\n", hat ? "^" : "");
5702 /* Output a 'call' insn. */
5704 char *
5705 output_call (operands)
5706 rtx * operands;
5708 /* Handle calls to lr using ip (which may be clobbered in subr anyway). */
5710 if (REGNO (operands[0]) == LR_REGNUM)
5712 operands[0] = gen_rtx_REG (SImode, IP_REGNUM);
5713 output_asm_insn ("mov%?\t%0, %|lr", operands);
5716 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
5718 if (TARGET_INTERWORK)
5719 output_asm_insn ("bx%?\t%0", operands);
5720 else
5721 output_asm_insn ("mov%?\t%|pc, %0", operands);
5723 return "";
5726 static int
5727 eliminate_lr2ip (x)
5728 rtx * x;
5730 int something_changed = 0;
5731 rtx x0 = * x;
5732 int code = GET_CODE (x0);
5733 register int i, j;
5734 register const char * fmt;
5736 switch (code)
5738 case REG:
5739 if (REGNO (x0) == LR_REGNUM)
5741 *x = gen_rtx_REG (SImode, IP_REGNUM);
5742 return 1;
5744 return 0;
5745 default:
5746 /* Scan through the sub-elements and change any references there. */
5747 fmt = GET_RTX_FORMAT (code);
5749 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
5750 if (fmt[i] == 'e')
5751 something_changed |= eliminate_lr2ip (&XEXP (x0, i));
5752 else if (fmt[i] == 'E')
5753 for (j = 0; j < XVECLEN (x0, i); j++)
5754 something_changed |= eliminate_lr2ip (&XVECEXP (x0, i, j));
5756 return something_changed;
5760 /* Output a 'call' insn that is a reference in memory. */
5762 char *
5763 output_call_mem (operands)
5764 rtx * operands;
5766 operands[0] = copy_rtx (operands[0]); /* Be ultra careful. */
5767 /* Handle calls using lr by using ip (which may be clobbered in subr anyway). */
5768 if (eliminate_lr2ip (&operands[0]))
5769 output_asm_insn ("mov%?\t%|ip, %|lr", operands);
5771 if (TARGET_INTERWORK)
5773 output_asm_insn ("ldr%?\t%|ip, %0", operands);
5774 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
5775 output_asm_insn ("bx%?\t%|ip", operands);
5777 else
5779 output_asm_insn ("mov%?\t%|lr, %|pc", operands);
5780 output_asm_insn ("ldr%?\t%|pc, %0", operands);
5783 return "";
5787 /* Output a move from arm registers to an fpu registers.
5788 OPERANDS[0] is an fpu register.
5789 OPERANDS[1] is the first registers of an arm register pair. */
5791 char *
5792 output_mov_long_double_fpu_from_arm (operands)
5793 rtx * operands;
5795 int arm_reg0 = REGNO (operands[1]);
5796 rtx ops[3];
5798 if (arm_reg0 == IP_REGNUM)
5799 abort ();
5801 ops[0] = gen_rtx_REG (SImode, arm_reg0);
5802 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
5803 ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
5805 output_asm_insn ("stm%?fd\t%|sp!, {%0, %1, %2}", ops);
5806 output_asm_insn ("ldf%?e\t%0, [%|sp], #12", operands);
5808 return "";
5811 /* Output a move from an fpu register to arm registers.
5812 OPERANDS[0] is the first registers of an arm register pair.
5813 OPERANDS[1] is an fpu register. */
5815 char *
5816 output_mov_long_double_arm_from_fpu (operands)
5817 rtx * operands;
5819 int arm_reg0 = REGNO (operands[0]);
5820 rtx ops[3];
5822 if (arm_reg0 == IP_REGNUM)
5823 abort ();
5825 ops[0] = gen_rtx_REG (SImode, arm_reg0);
5826 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
5827 ops[2] = gen_rtx_REG (SImode, 2 + arm_reg0);
5829 output_asm_insn ("stf%?e\t%1, [%|sp, #-12]!", operands);
5830 output_asm_insn ("ldm%?fd\t%|sp!, {%0, %1, %2}", ops);
5831 return "";
5834 /* Output a move from arm registers to arm registers of a long double
5835 OPERANDS[0] is the destination.
5836 OPERANDS[1] is the source. */
5837 char *
5838 output_mov_long_double_arm_from_arm (operands)
5839 rtx * operands;
5841 /* We have to be careful here because the two might overlap. */
5842 int dest_start = REGNO (operands[0]);
5843 int src_start = REGNO (operands[1]);
5844 rtx ops[2];
5845 int i;
5847 if (dest_start < src_start)
5849 for (i = 0; i < 3; i++)
5851 ops[0] = gen_rtx_REG (SImode, dest_start + i);
5852 ops[1] = gen_rtx_REG (SImode, src_start + i);
5853 output_asm_insn ("mov%?\t%0, %1", ops);
5856 else
5858 for (i = 2; i >= 0; i--)
5860 ops[0] = gen_rtx_REG (SImode, dest_start + i);
5861 ops[1] = gen_rtx_REG (SImode, src_start + i);
5862 output_asm_insn ("mov%?\t%0, %1", ops);
5866 return "";
5870 /* Output a move from arm registers to an fpu registers.
5871 OPERANDS[0] is an fpu register.
5872 OPERANDS[1] is the first registers of an arm register pair. */
5874 char *
5875 output_mov_double_fpu_from_arm (operands)
5876 rtx * operands;
5878 int arm_reg0 = REGNO (operands[1]);
5879 rtx ops[2];
5881 if (arm_reg0 == IP_REGNUM)
5882 abort ();
5884 ops[0] = gen_rtx_REG (SImode, arm_reg0);
5885 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
5886 output_asm_insn ("stm%?fd\t%|sp!, {%0, %1}", ops);
5887 output_asm_insn ("ldf%?d\t%0, [%|sp], #8", operands);
5888 return "";
5891 /* Output a move from an fpu register to arm registers.
5892 OPERANDS[0] is the first registers of an arm register pair.
5893 OPERANDS[1] is an fpu register. */
5895 char *
5896 output_mov_double_arm_from_fpu (operands)
5897 rtx * operands;
5899 int arm_reg0 = REGNO (operands[0]);
5900 rtx ops[2];
5902 if (arm_reg0 == IP_REGNUM)
5903 abort ();
5905 ops[0] = gen_rtx_REG (SImode, arm_reg0);
5906 ops[1] = gen_rtx_REG (SImode, 1 + arm_reg0);
5907 output_asm_insn ("stf%?d\t%1, [%|sp, #-8]!", operands);
5908 output_asm_insn ("ldm%?fd\t%|sp!, {%0, %1}", ops);
5909 return "";
5912 /* Output a move between double words.
5913 It must be REG<-REG, REG<-CONST_DOUBLE, REG<-CONST_INT, REG<-MEM
5914 or MEM<-REG and all MEMs must be offsettable addresses. */
5916 char *
5917 output_move_double (operands)
5918 rtx * operands;
5920 enum rtx_code code0 = GET_CODE (operands[0]);
5921 enum rtx_code code1 = GET_CODE (operands[1]);
5922 rtx otherops[3];
5924 if (code0 == REG)
5926 int reg0 = REGNO (operands[0]);
5928 otherops[0] = gen_rtx_REG (SImode, 1 + reg0);
5930 if (code1 == REG)
5932 int reg1 = REGNO (operands[1]);
5933 if (reg1 == IP_REGNUM)
5934 abort ();
5936 /* Ensure the second source is not overwritten. */
5937 if (reg1 == reg0 + (WORDS_BIG_ENDIAN ? -1 : 1))
5938 output_asm_insn ("mov%?\t%Q0, %Q1\n\tmov%?\t%R0, %R1", operands);
5939 else
5940 output_asm_insn ("mov%?\t%R0, %R1\n\tmov%?\t%Q0, %Q1", operands);
5942 else if (code1 == CONST_DOUBLE)
5944 if (GET_MODE (operands[1]) == DFmode)
5946 long l[2];
5947 union real_extract u;
5949 bcopy ((char *) &CONST_DOUBLE_LOW (operands[1]), (char *) &u,
5950 sizeof (u));
5951 REAL_VALUE_TO_TARGET_DOUBLE (u.d, l);
5952 otherops[1] = GEN_INT (l[1]);
5953 operands[1] = GEN_INT (l[0]);
5955 else if (GET_MODE (operands[1]) != VOIDmode)
5956 abort ();
5957 else if (WORDS_BIG_ENDIAN)
5960 otherops[1] = GEN_INT (CONST_DOUBLE_LOW (operands[1]));
5961 operands[1] = GEN_INT (CONST_DOUBLE_HIGH (operands[1]));
5963 else
5966 otherops[1] = GEN_INT (CONST_DOUBLE_HIGH (operands[1]));
5967 operands[1] = GEN_INT (CONST_DOUBLE_LOW (operands[1]));
5970 output_mov_immediate (operands);
5971 output_mov_immediate (otherops);
5973 else if (code1 == CONST_INT)
5975 #if HOST_BITS_PER_WIDE_INT > 32
5976 /* If HOST_WIDE_INT is more than 32 bits, the intval tells us
5977 what the upper word is. */
5978 if (WORDS_BIG_ENDIAN)
5980 otherops[1] = GEN_INT (ARM_SIGN_EXTEND (INTVAL (operands[1])));
5981 operands[1] = GEN_INT (INTVAL (operands[1]) >> 32);
5983 else
5985 otherops[1] = GEN_INT (INTVAL (operands[1]) >> 32);
5986 operands[1] = GEN_INT (ARM_SIGN_EXTEND (INTVAL (operands[1])));
5988 #else
5989 /* Sign extend the intval into the high-order word. */
5990 if (WORDS_BIG_ENDIAN)
5992 otherops[1] = operands[1];
5993 operands[1] = (INTVAL (operands[1]) < 0
5994 ? constm1_rtx : const0_rtx);
5996 else
5997 otherops[1] = INTVAL (operands[1]) < 0 ? constm1_rtx : const0_rtx;
5998 #endif
5999 output_mov_immediate (otherops);
6000 output_mov_immediate (operands);
6002 else if (code1 == MEM)
6004 switch (GET_CODE (XEXP (operands[1], 0)))
6006 case REG:
6007 output_asm_insn ("ldm%?ia\t%m1, %M0", operands);
6008 break;
6010 case PRE_INC:
6011 abort (); /* Should never happen now. */
6012 break;
6014 case PRE_DEC:
6015 output_asm_insn ("ldm%?db\t%m1!, %M0", operands);
6016 break;
6018 case POST_INC:
6019 output_asm_insn ("ldm%?ia\t%m1!, %M0", operands);
6020 break;
6022 case POST_DEC:
6023 abort (); /* Should never happen now. */
6024 break;
6026 case LABEL_REF:
6027 case CONST:
6028 output_asm_insn ("adr%?\t%0, %1", operands);
6029 output_asm_insn ("ldm%?ia\t%0, %M0", operands);
6030 break;
6032 default:
6033 if (arm_add_operand (XEXP (XEXP (operands[1], 0), 1),
6034 GET_MODE (XEXP (XEXP (operands[1], 0), 1))))
6036 otherops[0] = operands[0];
6037 otherops[1] = XEXP (XEXP (operands[1], 0), 0);
6038 otherops[2] = XEXP (XEXP (operands[1], 0), 1);
6039 if (GET_CODE (XEXP (operands[1], 0)) == PLUS)
6041 if (GET_CODE (otherops[2]) == CONST_INT)
6043 switch (INTVAL (otherops[2]))
6045 case -8:
6046 output_asm_insn ("ldm%?db\t%1, %M0", otherops);
6047 return "";
6048 case -4:
6049 output_asm_insn ("ldm%?da\t%1, %M0", otherops);
6050 return "";
6051 case 4:
6052 output_asm_insn ("ldm%?ib\t%1, %M0", otherops);
6053 return "";
6055 if (!(const_ok_for_arm (INTVAL (otherops[2]))))
6056 output_asm_insn ("sub%?\t%0, %1, #%n2", otherops);
6057 else
6058 output_asm_insn ("add%?\t%0, %1, %2", otherops);
6060 else
6061 output_asm_insn ("add%?\t%0, %1, %2", otherops);
6063 else
6064 output_asm_insn ("sub%?\t%0, %1, %2", otherops);
6066 return "ldm%?ia\t%0, %M0";
6068 else
6070 otherops[1] = adj_offsettable_operand (operands[1], 4);
6071 /* Take care of overlapping base/data reg. */
6072 if (reg_mentioned_p (operands[0], operands[1]))
6074 output_asm_insn ("ldr%?\t%0, %1", otherops);
6075 output_asm_insn ("ldr%?\t%0, %1", operands);
6077 else
6079 output_asm_insn ("ldr%?\t%0, %1", operands);
6080 output_asm_insn ("ldr%?\t%0, %1", otherops);
6085 else
6086 abort (); /* Constraints should prevent this. */
6088 else if (code0 == MEM && code1 == REG)
6090 if (REGNO (operands[1]) == IP_REGNUM)
6091 abort ();
6093 switch (GET_CODE (XEXP (operands[0], 0)))
6095 case REG:
6096 output_asm_insn ("stm%?ia\t%m0, %M1", operands);
6097 break;
6099 case PRE_INC:
6100 abort (); /* Should never happen now. */
6101 break;
6103 case PRE_DEC:
6104 output_asm_insn ("stm%?db\t%m0!, %M1", operands);
6105 break;
6107 case POST_INC:
6108 output_asm_insn ("stm%?ia\t%m0!, %M1", operands);
6109 break;
6111 case POST_DEC:
6112 abort (); /* Should never happen now. */
6113 break;
6115 case PLUS:
6116 if (GET_CODE (XEXP (XEXP (operands[0], 0), 1)) == CONST_INT)
6118 switch (INTVAL (XEXP (XEXP (operands[0], 0), 1)))
6120 case -8:
6121 output_asm_insn ("stm%?db\t%m0, %M1", operands);
6122 return "";
6124 case -4:
6125 output_asm_insn ("stm%?da\t%m0, %M1", operands);
6126 return "";
6128 case 4:
6129 output_asm_insn ("stm%?ib\t%m0, %M1", operands);
6130 return "";
6133 /* Fall through */
6135 default:
6136 otherops[0] = adj_offsettable_operand (operands[0], 4);
6137 otherops[1] = gen_rtx_REG (SImode, 1 + REGNO (operands[1]));
6138 output_asm_insn ("str%?\t%1, %0", operands);
6139 output_asm_insn ("str%?\t%1, %0", otherops);
6142 else
6143 abort (); /* Constraints should prevent this */
6145 return "";
6149 /* Output an arbitrary MOV reg, #n.
6150 OPERANDS[0] is a register. OPERANDS[1] is a const_int. */
6152 char *
6153 output_mov_immediate (operands)
6154 rtx * operands;
6156 HOST_WIDE_INT n = INTVAL (operands[1]);
6157 int n_ones = 0;
6158 int i;
6160 /* Try to use one MOV */
6161 if (const_ok_for_arm (n))
6163 output_asm_insn ("mov%?\t%0, %1", operands);
6164 return "";
6167 /* Try to use one MVN */
6168 if (const_ok_for_arm (~n))
6170 operands[1] = GEN_INT (~n);
6171 output_asm_insn ("mvn%?\t%0, %1", operands);
6172 return "";
6175 /* If all else fails, make it out of ORRs or BICs as appropriate. */
6177 for (i=0; i < 32; i++)
6178 if (n & 1 << i)
6179 n_ones++;
6181 if (n_ones > 16) /* Shorter to use MVN with BIC in this case. */
6182 output_multi_immediate (operands, "mvn%?\t%0, %1", "bic%?\t%0, %0, %1", 1,
6183 ~n);
6184 else
6185 output_multi_immediate (operands, "mov%?\t%0, %1", "orr%?\t%0, %0, %1", 1, n);
6187 return "";
6191 /* Output an ADD r, s, #n where n may be too big for one instruction. If
6192 adding zero to one register, output nothing. */
6194 char *
6195 output_add_immediate (operands)
6196 rtx * operands;
6198 HOST_WIDE_INT n = INTVAL (operands[2]);
6200 if (n != 0 || REGNO (operands[0]) != REGNO (operands[1]))
6202 if (n < 0)
6203 output_multi_immediate (operands,
6204 "sub%?\t%0, %1, %2", "sub%?\t%0, %0, %2", 2,
6205 -n);
6206 else
6207 output_multi_immediate (operands,
6208 "add%?\t%0, %1, %2", "add%?\t%0, %0, %2", 2,
6212 return "";
6215 /* Output a multiple immediate operation.
6216 OPERANDS is the vector of operands referred to in the output patterns.
6217 INSTR1 is the output pattern to use for the first constant.
6218 INSTR2 is the output pattern to use for subsequent constants.
6219 IMMED_OP is the index of the constant slot in OPERANDS.
6220 N is the constant value. */
6222 static char *
6223 output_multi_immediate (operands, instr1, instr2, immed_op, n)
6224 rtx * operands;
6225 char * instr1, * instr2;
6226 int immed_op;
6227 HOST_WIDE_INT n;
6229 #if HOST_BITS_PER_WIDE_INT > 32
6230 n &= (unsigned HOST_WIDE_INT)0xffffffff;
6231 #endif
6233 if (n == 0)
6235 operands[immed_op] = const0_rtx;
6236 output_asm_insn (instr1, operands); /* Quick and easy output. */
6238 else
6240 int i;
6241 char *instr = instr1;
6243 /* Note that n is never zero here (which would give no output). */
6244 for (i = 0; i < 32; i += 2)
6246 if (n & (3 << i))
6248 operands[immed_op] = GEN_INT (n & (255 << i));
6249 output_asm_insn (instr, operands);
6250 instr = instr2;
6251 i += 6;
6255 return "";
6259 /* Return the appropriate ARM instruction for the operation code.
6260 The returned result should not be overwritten. OP is the rtx of the
6261 operation. SHIFT_FIRST_ARG is TRUE if the first argument of the operator
6262 was shifted. */
6264 char *
6265 arithmetic_instr (op, shift_first_arg)
6266 rtx op;
6267 int shift_first_arg;
6269 switch (GET_CODE (op))
6271 case PLUS:
6272 return "add";
6274 case MINUS:
6275 return shift_first_arg ? "rsb" : "sub";
6277 case IOR:
6278 return "orr";
6280 case XOR:
6281 return "eor";
6283 case AND:
6284 return "and";
6286 default:
6287 abort ();
6292 /* Ensure valid constant shifts and return the appropriate shift mnemonic
6293 for the operation code. The returned result should not be overwritten.
6294 OP is the rtx code of the shift.
6295 On exit, *AMOUNTP will be -1 if the shift is by a register, or a constant
6296 shift. */
6298 static char *
6299 shift_op (op, amountp)
6300 rtx op;
6301 HOST_WIDE_INT *amountp;
6303 char * mnem;
6304 enum rtx_code code = GET_CODE (op);
6306 if (GET_CODE (XEXP (op, 1)) == REG || GET_CODE (XEXP (op, 1)) == SUBREG)
6307 *amountp = -1;
6308 else if (GET_CODE (XEXP (op, 1)) == CONST_INT)
6309 *amountp = INTVAL (XEXP (op, 1));
6310 else
6311 abort ();
6313 switch (code)
6315 case ASHIFT:
6316 mnem = "asl";
6317 break;
6319 case ASHIFTRT:
6320 mnem = "asr";
6321 break;
6323 case LSHIFTRT:
6324 mnem = "lsr";
6325 break;
6327 case ROTATERT:
6328 mnem = "ror";
6329 break;
6331 case MULT:
6332 /* We never have to worry about the amount being other than a
6333 power of 2, since this case can never be reloaded from a reg. */
6334 if (*amountp != -1)
6335 *amountp = int_log2 (*amountp);
6336 else
6337 abort ();
6338 return "asl";
6340 default:
6341 abort ();
6344 if (*amountp != -1)
6346 /* This is not 100% correct, but follows from the desire to merge
6347 multiplication by a power of 2 with the recognizer for a
6348 shift. >=32 is not a valid shift for "asl", so we must try and
6349 output a shift that produces the correct arithmetical result.
6350 Using lsr #32 is identical except for the fact that the carry bit
6351 is not set correctly if we set the flags; but we never use the
6352 carry bit from such an operation, so we can ignore that. */
6353 if (code == ROTATERT)
6354 *amountp &= 31; /* Rotate is just modulo 32 */
6355 else if (*amountp != (*amountp & 31))
6357 if (code == ASHIFT)
6358 mnem = "lsr";
6359 *amountp = 32;
6362 /* Shifts of 0 are no-ops. */
6363 if (*amountp == 0)
6364 return NULL;
6367 return mnem;
6371 /* Obtain the shift from the POWER of two. */
6372 static HOST_WIDE_INT
6373 int_log2 (power)
6374 HOST_WIDE_INT power;
6376 HOST_WIDE_INT shift = 0;
6378 while (((((HOST_WIDE_INT) 1) << shift) & power) == 0)
6380 if (shift > 31)
6381 abort ();
6382 shift++;
6385 return shift;
6388 /* Output a .ascii pseudo-op, keeping track of lengths. This is because
6389 /bin/as is horribly restrictive. */
6390 #define MAX_ASCII_LEN 51
6392 void
6393 output_ascii_pseudo_op (stream, p, len)
6394 FILE * stream;
6395 const unsigned char * p;
6396 int len;
6398 int i;
6399 int len_so_far = 0;
6401 fputs ("\t.ascii\t\"", stream);
6403 for (i = 0; i < len; i++)
6405 register int c = p[i];
6407 if (len_so_far >= MAX_ASCII_LEN)
6409 fputs ("\"\n\t.ascii\t\"", stream);
6410 len_so_far = 0;
6413 switch (c)
6415 case TARGET_TAB:
6416 fputs ("\\t", stream);
6417 len_so_far += 2;
6418 break;
6420 case TARGET_FF:
6421 fputs ("\\f", stream);
6422 len_so_far += 2;
6423 break;
6425 case TARGET_BS:
6426 fputs ("\\b", stream);
6427 len_so_far += 2;
6428 break;
6430 case TARGET_CR:
6431 fputs ("\\r", stream);
6432 len_so_far += 2;
6433 break;
6435 case TARGET_NEWLINE:
6436 fputs ("\\n", stream);
6437 c = p [i + 1];
6438 if ((c >= ' ' && c <= '~')
6439 || c == TARGET_TAB)
6440 /* This is a good place for a line break. */
6441 len_so_far = MAX_ASCII_LEN;
6442 else
6443 len_so_far += 2;
6444 break;
6446 case '\"':
6447 case '\\':
6448 putc ('\\', stream);
6449 len_so_far ++;
6450 /* drop through. */
6452 default:
6453 if (c >= ' ' && c <= '~')
6455 putc (c, stream);
6456 len_so_far ++;
6458 else
6460 fprintf (stream, "\\%03o", c);
6461 len_so_far += 4;
6463 break;
6467 fputs ("\"\n", stream);
6471 /* Try to determine whether a pattern really clobbers the link register.
6472 This information is useful when peepholing, so that lr need not be pushed
6473 if we combine a call followed by a return.
6474 NOTE: This code does not check for side-effect expressions in a SET_SRC:
6475 such a check should not be needed because these only update an existing
6476 value within a register; the register must still be set elsewhere within
6477 the function. */
6478 static int
6479 pattern_really_clobbers_lr (x)
6480 rtx x;
6482 int i;
6484 switch (GET_CODE (x))
6486 case SET:
6487 switch (GET_CODE (SET_DEST (x)))
6489 case REG:
6490 return REGNO (SET_DEST (x)) == LR_REGNUM;
6492 case SUBREG:
6493 if (GET_CODE (XEXP (SET_DEST (x), 0)) == REG)
6494 return REGNO (XEXP (SET_DEST (x), 0)) == LR_REGNUM;
6496 if (GET_CODE (XEXP (SET_DEST (x), 0)) == MEM)
6497 return 0;
6498 abort ();
6500 default:
6501 return 0;
6504 case PARALLEL:
6505 for (i = 0; i < XVECLEN (x, 0); i++)
6506 if (pattern_really_clobbers_lr (XVECEXP (x, 0, i)))
6507 return 1;
6508 return 0;
6510 case CLOBBER:
6511 switch (GET_CODE (XEXP (x, 0)))
6513 case REG:
6514 return REGNO (XEXP (x, 0)) == LR_REGNUM;
6516 case SUBREG:
6517 if (GET_CODE (XEXP (XEXP (x, 0), 0)) == REG)
6518 return REGNO (XEXP (XEXP (x, 0), 0)) == LR_REGNUM;
6519 abort ();
6521 default:
6522 return 0;
6525 case UNSPEC:
6526 return 1;
6528 default:
6529 return 0;
6533 static int
6534 function_really_clobbers_lr (first)
6535 rtx first;
6537 rtx insn, next;
6539 for (insn = first; insn; insn = next_nonnote_insn (insn))
6541 switch (GET_CODE (insn))
6543 case BARRIER:
6544 case NOTE:
6545 case CODE_LABEL:
6546 case JUMP_INSN: /* Jump insns only change the PC (and conds) */
6547 break;
6549 case INSN:
6550 if (pattern_really_clobbers_lr (PATTERN (insn)))
6551 return 1;
6552 break;
6554 case CALL_INSN:
6555 /* Don't yet know how to handle those calls that are not to a
6556 SYMBOL_REF. */
6557 if (GET_CODE (PATTERN (insn)) != PARALLEL)
6558 abort ();
6560 switch (GET_CODE (XVECEXP (PATTERN (insn), 0, 0)))
6562 case CALL:
6563 if (GET_CODE (XEXP (XEXP (XVECEXP (PATTERN (insn), 0, 0), 0), 0))
6564 != SYMBOL_REF)
6565 return 1;
6566 break;
6568 case SET:
6569 if (GET_CODE (XEXP (XEXP (SET_SRC (XVECEXP (PATTERN (insn),
6570 0, 0)), 0), 0))
6571 != SYMBOL_REF)
6572 return 1;
6573 break;
6575 default: /* Don't recognize it, be safe. */
6576 return 1;
6579 /* A call can be made (by peepholing) not to clobber lr iff it is
6580 followed by a return. There may, however, be a use insn iff
6581 we are returning the result of the call.
6582 If we run off the end of the insn chain, then that means the
6583 call was at the end of the function. Unfortunately we don't
6584 have a return insn for the peephole to recognize, so we
6585 must reject this. (Can this be fixed by adding our own insn?) */
6586 if ((next = next_nonnote_insn (insn)) == NULL)
6587 return 1;
6589 /* No need to worry about lr if the call never returns. */
6590 if (GET_CODE (next) == BARRIER)
6591 break;
6593 if (GET_CODE (next) == INSN
6594 && GET_CODE (PATTERN (next)) == USE
6595 && (GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
6596 && (GET_CODE (XEXP (PATTERN (next), 0)) == REG)
6597 && (REGNO (SET_DEST (XVECEXP (PATTERN (insn), 0, 0)))
6598 == REGNO (XEXP (PATTERN (next), 0))))
6599 if ((next = next_nonnote_insn (next)) == NULL)
6600 return 1;
6602 if (GET_CODE (next) == JUMP_INSN
6603 && GET_CODE (PATTERN (next)) == RETURN)
6604 break;
6605 return 1;
6607 default:
6608 abort ();
6612 /* We have reached the end of the chain so lr was _not_ clobbered. */
6613 return 0;
6616 char *
6617 output_return_instruction (operand, really_return, reverse)
6618 rtx operand;
6619 int really_return;
6620 int reverse;
6622 char instr[100];
6623 int reg, live_regs = 0;
6624 int volatile_func = arm_volatile_func ();
6626 /* If a function is naked, don't use the "return" insn. */
6627 if (arm_naked_function_p (current_function_decl))
6628 return "";
6630 return_used_this_function = 1;
6632 if (TARGET_ABORT_NORETURN && volatile_func)
6634 /* If this function was declared non-returning, and we have found a tail
6635 call, then we have to trust that the called function won't return. */
6636 if (really_return)
6638 rtx ops[2];
6640 /* Otherwise, trap an attempted return by aborting. */
6641 ops[0] = operand;
6642 ops[1] = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)"
6643 : "abort");
6644 assemble_external_libcall (ops[1]);
6645 output_asm_insn (reverse ? "bl%D0\t%a1" : "bl%d0\t%a1", ops);
6648 return "";
6651 if (current_function_calls_alloca && ! really_return)
6652 abort ();
6654 for (reg = 0; reg <= 10; reg++)
6655 if (regs_ever_live[reg] && ! call_used_regs[reg])
6656 live_regs++;
6658 if (! TARGET_APCS_FRAME
6659 && ! frame_pointer_needed
6660 && regs_ever_live[HARD_FRAME_POINTER_REGNUM]
6661 && ! call_used_regs[HARD_FRAME_POINTER_REGNUM])
6662 live_regs++;
6664 if (flag_pic && ! TARGET_SINGLE_PIC_BASE
6665 && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
6666 live_regs++;
6668 if (live_regs || (regs_ever_live[LR_REGNUM] && ! lr_save_eliminated))
6669 live_regs++;
6671 if (frame_pointer_needed)
6672 live_regs += 4;
6674 /* On some ARM architectures it is faster to use LDR rather than LDM to
6675 load a single register. On other architectures, the cost is the same. */
6676 if (live_regs == 1
6677 && regs_ever_live[LR_REGNUM]
6678 && ! lr_save_eliminated
6679 /* FIXME: We ought to handle the case TARGET_APCS_32 is true,
6680 really_return is true, and only the PC needs restoring. */
6681 && ! really_return)
6682 output_asm_insn (reverse ? "ldr%?%D0\t%|lr, [%|sp], #4"
6683 : "ldr%?%d0\t%|lr, [%|sp], #4", &operand);
6684 else if (live_regs == 1
6685 && regs_ever_live[LR_REGNUM]
6686 && ! lr_save_eliminated
6687 && TARGET_APCS_32)
6688 output_asm_insn (reverse ? "ldr%?%D0\t%|pc, [%|sp], #4"
6689 : "ldr%?%d0\t%|pc, [%|sp], #4", &operand);
6690 else if (live_regs)
6692 if (lr_save_eliminated || ! regs_ever_live[LR_REGNUM])
6693 live_regs++;
6695 if (frame_pointer_needed)
6696 strcpy (instr,
6697 reverse ? "ldm%?%D0ea\t%|fp, {" : "ldm%?%d0ea\t%|fp, {");
6698 else
6699 strcpy (instr,
6700 reverse ? "ldm%?%D0fd\t%|sp!, {" : "ldm%?%d0fd\t%|sp!, {");
6702 for (reg = 0; reg <= 10; reg++)
6703 if (regs_ever_live[reg]
6704 && (! call_used_regs[reg]
6705 || (flag_pic && ! TARGET_SINGLE_PIC_BASE
6706 && reg == PIC_OFFSET_TABLE_REGNUM)))
6708 strcat (instr, "%|");
6709 strcat (instr, reg_names[reg]);
6710 if (--live_regs)
6711 strcat (instr, ", ");
6714 if (frame_pointer_needed)
6716 strcat (instr, "%|");
6717 strcat (instr, reg_names[11]);
6718 strcat (instr, ", ");
6719 strcat (instr, "%|");
6720 strcat (instr, reg_names[13]);
6721 strcat (instr, ", ");
6722 strcat (instr, "%|");
6723 strcat (instr, TARGET_INTERWORK || (! really_return)
6724 ? reg_names[LR_REGNUM] : reg_names[PC_REGNUM] );
6726 else
6728 if (! TARGET_APCS_FRAME
6729 && regs_ever_live[HARD_FRAME_POINTER_REGNUM]
6730 && ! call_used_regs[HARD_FRAME_POINTER_REGNUM])
6732 strcat (instr, "%|");
6733 strcat (instr, reg_names[HARD_FRAME_POINTER_REGNUM]);
6734 strcat (instr, ", ");
6737 strcat (instr, "%|");
6739 if (TARGET_INTERWORK && really_return)
6740 strcat (instr, reg_names[IP_REGNUM]);
6741 else
6742 strcat (instr, really_return ? reg_names[PC_REGNUM] : reg_names[LR_REGNUM]);
6745 strcat (instr, (TARGET_APCS_32 || !really_return) ? "}" : "}^");
6746 output_asm_insn (instr, &operand);
6748 if (TARGET_INTERWORK && really_return)
6750 strcpy (instr, "bx%?");
6751 strcat (instr, reverse ? "%D0" : "%d0");
6752 strcat (instr, "\t%|");
6753 strcat (instr, frame_pointer_needed ? "lr" : "ip");
6755 output_asm_insn (instr, & operand);
6758 else if (really_return)
6760 if (TARGET_INTERWORK)
6761 sprintf (instr, "bx%%?%%%s0\t%%|lr", reverse ? "D" : "d");
6762 else
6763 sprintf (instr, "mov%%?%%%s0%s\t%%|pc, %%|lr",
6764 reverse ? "D" : "d", TARGET_APCS_32 ? "" : "s");
6766 output_asm_insn (instr, & operand);
6769 return "";
6772 /* Return nonzero if optimizing and the current function is volatile.
6773 Such functions never return, and many memory cycles can be saved
6774 by not storing register values that will never be needed again.
6775 This optimization was added to speed up context switching in a
6776 kernel application. */
6778 arm_volatile_func ()
6780 return (optimize > 0
6781 && current_function_nothrow
6782 && TREE_THIS_VOLATILE (current_function_decl));
6785 /* Write the function name into the code section, directly preceding
6786 the function prologue.
6788 Code will be output similar to this:
6790 .ascii "arm_poke_function_name", 0
6791 .align
6793 .word 0xff000000 + (t1 - t0)
6794 arm_poke_function_name
6795 mov ip, sp
6796 stmfd sp!, {fp, ip, lr, pc}
6797 sub fp, ip, #4
6799 When performing a stack backtrace, code can inspect the value
6800 of 'pc' stored at 'fp' + 0. If the trace function then looks
6801 at location pc - 12 and the top 8 bits are set, then we know
6802 that there is a function name embedded immediately preceding this
6803 location and has length ((pc[-3]) & 0xff000000).
6805 We assume that pc is declared as a pointer to an unsigned long.
6807 It is of no benefit to output the function name if we are assembling
6808 a leaf function. These function types will not contain a stack
6809 backtrace structure, therefore it is not possible to determine the
6810 function name. */
6812 void
6813 arm_poke_function_name (stream, name)
6814 FILE * stream;
6815 char * name;
6817 unsigned long alignlength;
6818 unsigned long length;
6819 rtx x;
6821 length = strlen (name) + 1;
6822 alignlength = ROUND_UP (length);
6824 ASM_OUTPUT_ASCII (stream, name, length);
6825 ASM_OUTPUT_ALIGN (stream, 2);
6826 x = GEN_INT (((unsigned HOST_WIDE_INT)0xff000000) + alignlength);
6827 ASM_OUTPUT_INT (stream, x);
6830 /* The amount of stack adjustment that happens here, in output_return and in
6831 output_epilogue must be exactly the same as was calculated during reload,
6832 or things will point to the wrong place. The only time we can safely
6833 ignore this constraint is when a function has no arguments on the stack,
6834 no stack frame requirement and no live registers execpt for `lr'. If we
6835 can guarantee that by making all function calls into tail calls and that
6836 lr is not clobbered in any other way, then there is no need to push lr
6837 onto the stack. */
6838 void
6839 output_arm_prologue (f, frame_size)
6840 FILE * f;
6841 int frame_size;
6843 int reg, live_regs_mask = 0;
6844 int volatile_func = arm_volatile_func ();
6846 /* Nonzero if we must stuff some register arguments onto the stack as if
6847 they were passed there. */
6848 int store_arg_regs = 0;
6850 if (arm_ccfsm_state || arm_target_insn)
6851 abort (); /* Sanity check. */
6853 if (arm_naked_function_p (current_function_decl))
6854 return;
6856 return_used_this_function = 0;
6857 lr_save_eliminated = 0;
6859 asm_fprintf (f, "\t%@ args = %d, pretend = %d, frame = %d\n",
6860 current_function_args_size,
6861 current_function_pretend_args_size, frame_size);
6862 asm_fprintf (f, "\t%@ frame_needed = %d, current_function_anonymous_args = %d\n",
6863 frame_pointer_needed,
6864 current_function_anonymous_args);
6866 if (volatile_func)
6867 asm_fprintf (f, "\t%@ Volatile function.\n");
6869 if (current_function_anonymous_args && current_function_pretend_args_size)
6870 store_arg_regs = 1;
6872 for (reg = 0; reg <= 10; reg++)
6873 if (regs_ever_live[reg] && ! call_used_regs[reg])
6874 live_regs_mask |= (1 << reg);
6876 if (! TARGET_APCS_FRAME
6877 && ! frame_pointer_needed
6878 && regs_ever_live[HARD_FRAME_POINTER_REGNUM]
6879 && ! call_used_regs[HARD_FRAME_POINTER_REGNUM])
6880 live_regs_mask |= (1 << HARD_FRAME_POINTER_REGNUM);
6882 if (flag_pic && ! TARGET_SINGLE_PIC_BASE
6883 && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
6884 live_regs_mask |= (1 << PIC_OFFSET_TABLE_REGNUM);
6886 if (frame_pointer_needed)
6887 live_regs_mask |= 0xD800;
6888 else if (regs_ever_live[LR_REGNUM])
6890 if (! current_function_args_size
6891 && ! function_really_clobbers_lr (get_insns ()))
6892 lr_save_eliminated = 1;
6893 else
6894 live_regs_mask |= 1 << LR_REGNUM;
6897 if (live_regs_mask)
6899 /* If a di mode load/store multiple is used, and the base register
6900 is r3, then r4 can become an ever live register without lr
6901 doing so, in this case we need to push lr as well, or we
6902 will fail to get a proper return. */
6903 live_regs_mask |= 1 << LR_REGNUM;
6904 lr_save_eliminated = 0;
6908 if (lr_save_eliminated)
6909 asm_fprintf (f,"\t%@ I don't think this function clobbers lr\n");
6911 #ifdef AOF_ASSEMBLER
6912 if (flag_pic)
6913 asm_fprintf (f, "\tmov\t%r, %r\n", IP_REGNUM, PIC_OFFSET_TABLE_REGNUM);
6914 #endif
6917 char *
6918 arm_output_epilogue ()
6920 int reg;
6921 int live_regs_mask = 0;
6922 /* If we need this, then it will always be at least this much. */
6923 int floats_offset = 12;
6924 rtx operands[3];
6925 int frame_size = get_frame_size ();
6926 rtx eh_ofs = cfun->machine->eh_epilogue_sp_ofs;
6927 FILE * f = asm_out_file;
6928 int volatile_func = (optimize > 0
6929 && TREE_THIS_VOLATILE (current_function_decl));
6930 int return_regnum;
6932 if (use_return_insn (FALSE) && return_used_this_function)
6933 return "";
6935 /* Naked functions don't have epilogues. */
6936 if (arm_naked_function_p (current_function_decl))
6937 return "";
6939 /* If we are throwing an exception, the address we want to jump to is in
6940 R1; otherwise, it's in LR. */
6941 return_regnum = eh_ofs ? 2 : LR_REGNUM;
6943 /* A volatile function should never return. Call abort. */
6944 if (TARGET_ABORT_NORETURN && volatile_func)
6946 rtx op;
6947 op = gen_rtx_SYMBOL_REF (Pmode, NEED_PLT_RELOC ? "abort(PLT)" : "abort");
6948 assemble_external_libcall (op);
6949 output_asm_insn ("bl\t%a0", &op);
6950 return "";
6953 for (reg = 0; reg <= 10; reg++)
6954 if (regs_ever_live[reg] && ! call_used_regs[reg])
6956 live_regs_mask |= (1 << reg);
6957 floats_offset += 4;
6960 /* Handle the frame pointer as a special case. */
6961 if (! TARGET_APCS_FRAME
6962 && ! frame_pointer_needed
6963 && regs_ever_live[HARD_FRAME_POINTER_REGNUM]
6964 && ! call_used_regs[HARD_FRAME_POINTER_REGNUM])
6966 live_regs_mask |= (1 << HARD_FRAME_POINTER_REGNUM);
6967 floats_offset += 4;
6970 /* If we aren't loading the PIC register, don't stack it even though it may
6971 be live. */
6972 if (flag_pic && ! TARGET_SINGLE_PIC_BASE
6973 && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
6975 live_regs_mask |= (1 << PIC_OFFSET_TABLE_REGNUM);
6976 floats_offset += 4;
6979 if (frame_pointer_needed)
6981 if (arm_fpu_arch == FP_SOFT2)
6983 for (reg = LAST_ARM_FP_REGNUM; reg >= FIRST_ARM_FP_REGNUM; reg--)
6984 if (regs_ever_live[reg] && ! call_used_regs[reg])
6986 floats_offset += 12;
6987 asm_fprintf (f, "\tldfe\t%r, [%r, #-%d]\n",
6988 reg, FP_REGNUM, floats_offset);
6991 else
6993 int start_reg = LAST_ARM_FP_REGNUM;
6995 for (reg = LAST_ARM_FP_REGNUM; reg >= FIRST_ARM_FP_REGNUM; reg--)
6997 if (regs_ever_live[reg] && ! call_used_regs[reg])
6999 floats_offset += 12;
7001 /* We can't unstack more than four registers at once. */
7002 if (start_reg - reg == 3)
7004 asm_fprintf (f, "\tlfm\t%r, 4, [%r, #-%d]\n",
7005 reg, FP_REGNUM, floats_offset);
7006 start_reg = reg - 1;
7009 else
7011 if (reg != start_reg)
7012 asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
7013 reg + 1, start_reg - reg,
7014 FP_REGNUM, floats_offset);
7015 start_reg = reg - 1;
7019 /* Just in case the last register checked also needs unstacking. */
7020 if (reg != start_reg)
7021 asm_fprintf (f, "\tlfm\t%r, %d, [%r, #-%d]\n",
7022 reg + 1, start_reg - reg,
7023 FP_REGNUM, floats_offset);
7026 if (TARGET_INTERWORK)
7028 live_regs_mask |= 0x6800;
7029 print_multi_reg (f, "ldmea\t%r", FP_REGNUM, live_regs_mask, FALSE);
7030 if (eh_ofs)
7031 asm_fprintf (f, "\tadd\t%r, %r, %r\n", SP_REGNUM, SP_REGNUM,
7032 REGNO (eh_ofs));
7033 asm_fprintf (f, "\tbx\t%r\n", return_regnum);
7035 else if (eh_ofs)
7037 live_regs_mask |= 0x6800;
7038 print_multi_reg (f, "ldmea\t%r", FP_REGNUM, live_regs_mask, FALSE);
7039 asm_fprintf (f, "\tadd\t%r, %r, %r\n", SP_REGNUM, SP_REGNUM,
7040 REGNO (eh_ofs));
7041 /* Even in 26-bit mode we do a mov (rather than a movs) because
7042 we don't have the PSR bits set in the address. */
7043 asm_fprintf (f, "\tmov\t%r, %r\n", PC_REGNUM, return_regnum);
7045 else
7047 live_regs_mask |= 0xA800;
7048 print_multi_reg (f, "ldmea\t%r", FP_REGNUM, live_regs_mask,
7049 TARGET_APCS_32 ? FALSE : TRUE);
7052 else
7054 /* Restore stack pointer if necessary. */
7055 if (frame_size + current_function_outgoing_args_size != 0)
7057 operands[0] = operands[1] = stack_pointer_rtx;
7058 operands[2] = GEN_INT (frame_size
7059 + current_function_outgoing_args_size);
7060 output_add_immediate (operands);
7063 if (arm_fpu_arch == FP_SOFT2)
7065 for (reg = FIRST_ARM_FP_REGNUM; reg <= LAST_ARM_FP_REGNUM; reg++)
7066 if (regs_ever_live[reg] && ! call_used_regs[reg])
7067 asm_fprintf (f, "\tldfe\t%r, [%r], #12\n",
7068 reg, SP_REGNUM);
7070 else
7072 int start_reg = FIRST_ARM_FP_REGNUM;
7074 for (reg = FIRST_ARM_FP_REGNUM; reg <= LAST_ARM_FP_REGNUM; reg++)
7076 if (regs_ever_live[reg] && ! call_used_regs[reg])
7078 if (reg - start_reg == 3)
7080 asm_fprintf (f, "\tlfmfd\t%r, 4, [%r]!\n",
7081 start_reg, SP_REGNUM);
7082 start_reg = reg + 1;
7085 else
7087 if (reg != start_reg)
7088 asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
7089 start_reg, reg - start_reg,
7090 SP_REGNUM);
7092 start_reg = reg + 1;
7096 /* Just in case the last register checked also needs unstacking. */
7097 if (reg != start_reg)
7098 asm_fprintf (f, "\tlfmfd\t%r, %d, [%r]!\n",
7099 start_reg, reg - start_reg, SP_REGNUM);
7102 if (current_function_pretend_args_size == 0 && regs_ever_live[LR_REGNUM])
7104 if (TARGET_INTERWORK)
7106 if (! lr_save_eliminated)
7107 live_regs_mask |= 1 << LR_REGNUM;
7109 /* Handle LR on its own. */
7110 if (live_regs_mask == (1 << LR_REGNUM))
7112 if (eh_ofs)
7113 asm_fprintf (f, "\tadd\t%r, %r, #4\n", SP_REGNUM,
7114 SP_REGNUM);
7115 else
7116 asm_fprintf (f, "\tldr\t%r, [%r], #4\n", LR_REGNUM,
7117 SP_REGNUM);
7119 else if (live_regs_mask != 0)
7120 print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM, live_regs_mask,
7121 FALSE);
7123 if (eh_ofs)
7124 asm_fprintf (f, "\tadd\t%r, %r, %r\n", SP_REGNUM, SP_REGNUM,
7125 REGNO (eh_ofs));
7127 asm_fprintf (f, "\tbx\t%r\n", return_regnum);
7129 else if (lr_save_eliminated)
7130 asm_fprintf (f,
7131 TARGET_APCS_32 ? "\tmov\t%r, %r\n" : "\tmovs\t%r, %r\n",
7132 PC_REGNUM, LR_REGNUM);
7133 else if (eh_ofs)
7135 if (live_regs_mask == 0)
7136 asm_fprintf (f, "\tadd\t%r, %r, #4\n", SP_REGNUM, SP_REGNUM);
7137 else
7138 print_multi_reg (f, "\tldmfd\t%r!", SP_REGNUM,
7139 live_regs_mask | (1 << LR_REGNUM), FALSE);
7141 asm_fprintf (f, "\tadd\t%r, %r, %r\n", SP_REGNUM, SP_REGNUM,
7142 REGNO (eh_ofs));
7143 /* Jump to the target; even in 26-bit mode. */
7144 asm_fprintf (f, "\tmov\t%r, %r\n", PC_REGNUM, return_regnum);
7146 else if (TARGET_APCS_32 && live_regs_mask == 0)
7147 asm_fprintf (f, "\tldr\t%r, [%r], #4\n", PC_REGNUM, SP_REGNUM);
7148 else
7149 print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM,
7150 live_regs_mask | (1 << PC_REGNUM),
7151 TARGET_APCS_32 ? FALSE : TRUE);
7153 else
7155 if (live_regs_mask || regs_ever_live[LR_REGNUM])
7157 /* Restore the integer regs, and the return address into lr. */
7158 if (! lr_save_eliminated)
7159 live_regs_mask |= 1 << LR_REGNUM;
7161 if (live_regs_mask == (1 << LR_REGNUM))
7163 if (eh_ofs)
7164 asm_fprintf (f, "\tadd\t%r, %r, #4\n", SP_REGNUM,
7165 SP_REGNUM);
7166 else
7167 asm_fprintf (f, "\tldr\t%r, [%r], #4\n", LR_REGNUM,
7168 SP_REGNUM);
7170 else if (live_regs_mask != 0)
7171 print_multi_reg (f, "ldmfd\t%r!", SP_REGNUM, live_regs_mask,
7172 FALSE);
7175 if (current_function_pretend_args_size)
7177 /* Unwind the pre-pushed regs. */
7178 operands[0] = operands[1] = stack_pointer_rtx;
7179 operands[2] = GEN_INT (current_function_pretend_args_size);
7180 output_add_immediate (operands);
7183 if (eh_ofs)
7184 asm_fprintf (f, "\tadd\t%r, %r, %r\n", SP_REGNUM, SP_REGNUM,
7185 REGNO (eh_ofs));
7187 /* And finally, go home. */
7188 if (TARGET_INTERWORK)
7189 asm_fprintf (f, "\tbx\t%r\n", return_regnum);
7190 else if (TARGET_APCS_32 || eh_ofs)
7191 asm_fprintf (f, "\tmov\t%r, %r\n", PC_REGNUM, return_regnum);
7192 else
7193 asm_fprintf (f, "\tmovs\t%r, %r\n", PC_REGNUM, return_regnum);
7197 return "";
7200 void
7201 output_func_epilogue (frame_size)
7202 int frame_size;
7204 if (TARGET_THUMB)
7206 /* ??? Probably not safe to set this here, since it assumes that a
7207 function will be emitted as assembly immediately after we generate
7208 RTL for it. This does not happen for inline functions. */
7209 return_used_this_function = 0;
7211 else
7213 if (use_return_insn (FALSE)
7214 && return_used_this_function
7215 && (frame_size + current_function_outgoing_args_size) != 0
7216 && ! frame_pointer_needed)
7217 abort ();
7219 /* Reset the ARM-specific per-function variables. */
7220 current_function_anonymous_args = 0;
7221 after_arm_reorg = 0;
7225 /* Generate and emit an insn that we will recognize as a push_multi.
7226 Unfortunately, since this insn does not reflect very well the actual
7227 semantics of the operation, we need to annotate the insn for the benefit
7228 of DWARF2 frame unwind information. */
7229 static rtx
7230 emit_multi_reg_push (mask)
7231 int mask;
7233 int num_regs = 0;
7234 int i, j;
7235 rtx par;
7236 rtx dwarf;
7237 rtx tmp, reg;
7239 for (i = 0; i <= LAST_ARM_REGNUM; i++)
7240 if (mask & (1 << i))
7241 num_regs ++;
7243 if (num_regs == 0 || num_regs > 16)
7244 abort ();
7246 par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_regs));
7247 dwarf = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (num_regs));
7248 RTX_FRAME_RELATED_P (dwarf) = 1;
7250 for (i = 0; i <= LAST_ARM_REGNUM; i++)
7252 if (mask & (1 << i))
7254 reg = gen_rtx_REG (SImode, i);
7256 XVECEXP (par, 0, 0)
7257 = gen_rtx_SET (VOIDmode,
7258 gen_rtx_MEM (BLKmode,
7259 gen_rtx_PRE_DEC (BLKmode,
7260 stack_pointer_rtx)),
7261 gen_rtx_UNSPEC (BLKmode,
7262 gen_rtvec (1, reg),
7263 2));
7265 tmp = gen_rtx_SET (VOIDmode,
7266 gen_rtx_MEM (SImode,
7267 gen_rtx_PRE_DEC (BLKmode,
7268 stack_pointer_rtx)),
7269 reg);
7270 RTX_FRAME_RELATED_P (tmp) = 1;
7271 XVECEXP (dwarf, 0, num_regs - 1) = tmp;
7273 break;
7277 for (j = 1, i++; j < num_regs; i++)
7279 if (mask & (1 << i))
7281 reg = gen_rtx_REG (SImode, i);
7283 XVECEXP (par, 0, j) = gen_rtx_USE (VOIDmode, reg);
7285 tmp = gen_rtx_SET (VOIDmode,
7286 gen_rtx_MEM (SImode,
7287 gen_rtx_PRE_DEC (BLKmode,
7288 stack_pointer_rtx)),
7289 reg);
7290 RTX_FRAME_RELATED_P (tmp) = 1;
7291 XVECEXP (dwarf, 0, num_regs - j - 1) = tmp;
7293 j++;
7297 par = emit_insn (par);
7298 REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
7299 REG_NOTES (par));
7300 return par;
7303 static rtx
7304 emit_sfm (base_reg, count)
7305 int base_reg;
7306 int count;
7308 rtx par;
7309 rtx dwarf;
7310 rtx tmp, reg;
7311 int i;
7313 par = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
7314 dwarf = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (count));
7315 RTX_FRAME_RELATED_P (dwarf) = 1;
7317 reg = gen_rtx_REG (XFmode, base_reg++);
7319 XVECEXP (par, 0, 0)
7320 = gen_rtx_SET (VOIDmode,
7321 gen_rtx_MEM (BLKmode,
7322 gen_rtx_PRE_DEC (BLKmode, stack_pointer_rtx)),
7323 gen_rtx_UNSPEC (BLKmode,
7324 gen_rtvec (1, reg),
7325 2));
7327 = gen_rtx_SET (VOIDmode,
7328 gen_rtx_MEM (XFmode,
7329 gen_rtx_PRE_DEC (BLKmode, stack_pointer_rtx)),
7330 reg);
7331 RTX_FRAME_RELATED_P (tmp) = 1;
7332 XVECEXP (dwarf, 0, count - 1) = tmp;
7334 for (i = 1; i < count; i++)
7336 reg = gen_rtx_REG (XFmode, base_reg++);
7337 XVECEXP (par, 0, i) = gen_rtx_USE (VOIDmode, reg);
7339 tmp = gen_rtx_SET (VOIDmode,
7340 gen_rtx_MEM (XFmode,
7341 gen_rtx_PRE_DEC (BLKmode,
7342 stack_pointer_rtx)),
7343 reg);
7344 RTX_FRAME_RELATED_P (tmp) = 1;
7345 XVECEXP (dwarf, 0, count - i - 1) = tmp;
7348 par = emit_insn (par);
7349 REG_NOTES (par) = gen_rtx_EXPR_LIST (REG_FRAME_RELATED_EXPR, dwarf,
7350 REG_NOTES (par));
7351 return par;
7354 void
7355 arm_expand_prologue ()
7357 int reg;
7358 rtx amount = GEN_INT (-(get_frame_size ()
7359 + current_function_outgoing_args_size));
7360 int live_regs_mask = 0;
7361 int store_arg_regs = 0;
7362 /* If this function doesn't return, then there is no need to push
7363 the call-saved regs. */
7364 int volatile_func = arm_volatile_func ();
7365 rtx insn;
7367 /* Naked functions don't have prologues. */
7368 if (arm_naked_function_p (current_function_decl))
7369 return;
7371 if (current_function_anonymous_args && current_function_pretend_args_size)
7372 store_arg_regs = 1;
7374 if (! volatile_func)
7376 for (reg = 0; reg <= 10; reg++)
7377 if (regs_ever_live[reg] && ! call_used_regs[reg])
7378 live_regs_mask |= 1 << reg;
7380 if (! TARGET_APCS_FRAME
7381 && ! frame_pointer_needed
7382 && regs_ever_live[HARD_FRAME_POINTER_REGNUM]
7383 && ! call_used_regs[HARD_FRAME_POINTER_REGNUM])
7384 live_regs_mask |= 1 << HARD_FRAME_POINTER_REGNUM;
7386 if (flag_pic && regs_ever_live[PIC_OFFSET_TABLE_REGNUM])
7387 live_regs_mask |= 1 << PIC_OFFSET_TABLE_REGNUM;
7389 if (regs_ever_live[LR_REGNUM])
7390 live_regs_mask |= 1 << LR_REGNUM;
7393 if (frame_pointer_needed)
7395 live_regs_mask |= 0xD800;
7396 insn = emit_insn (gen_movsi (gen_rtx_REG (SImode, IP_REGNUM),
7397 stack_pointer_rtx));
7398 RTX_FRAME_RELATED_P (insn) = 1;
7401 if (current_function_pretend_args_size)
7403 if (store_arg_regs)
7404 insn = emit_multi_reg_push
7405 ((0xf0 >> (current_function_pretend_args_size / 4)) & 0xf);
7406 else
7407 insn = emit_insn
7408 (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
7409 GEN_INT (-current_function_pretend_args_size)));
7410 RTX_FRAME_RELATED_P (insn) = 1;
7413 if (live_regs_mask)
7415 /* If we have to push any regs, then we must push lr as well, or
7416 we won't get a proper return. */
7417 live_regs_mask |= 1 << LR_REGNUM;
7418 insn = emit_multi_reg_push (live_regs_mask);
7419 RTX_FRAME_RELATED_P (insn) = 1;
7422 /* For now the integer regs are still pushed in output_arm_epilogue (). */
7424 if (! volatile_func)
7426 if (arm_fpu_arch == FP_SOFT2)
7428 for (reg = LAST_ARM_FP_REGNUM; reg >= FIRST_ARM_FP_REGNUM; reg --)
7429 if (regs_ever_live[reg] && ! call_used_regs[reg])
7431 insn = gen_rtx_PRE_DEC (XFmode, stack_pointer_rtx);
7432 insn = gen_rtx_MEM (XFmode, insn);
7433 insn = emit_insn (gen_rtx_SET (VOIDmode, insn,
7434 gen_rtx_REG (XFmode, reg)));
7435 RTX_FRAME_RELATED_P (insn) = 1;
7438 else
7440 int start_reg = LAST_ARM_FP_REGNUM;
7442 for (reg = LAST_ARM_FP_REGNUM; reg >= FIRST_ARM_FP_REGNUM; reg --)
7444 if (regs_ever_live[reg] && ! call_used_regs[reg])
7446 if (start_reg - reg == 3)
7448 insn = emit_sfm (reg, 4);
7449 RTX_FRAME_RELATED_P (insn) = 1;
7450 start_reg = reg - 1;
7453 else
7455 if (start_reg != reg)
7457 insn = emit_sfm (reg + 1, start_reg - reg);
7458 RTX_FRAME_RELATED_P (insn) = 1;
7460 start_reg = reg - 1;
7464 if (start_reg != reg)
7466 insn = emit_sfm (reg + 1, start_reg - reg);
7467 RTX_FRAME_RELATED_P (insn) = 1;
7472 if (frame_pointer_needed)
7474 insn = GEN_INT (-(4 + current_function_pretend_args_size));
7475 insn = emit_insn (gen_addsi3 (hard_frame_pointer_rtx,
7476 gen_rtx_REG (SImode, IP_REGNUM),
7477 insn));
7478 RTX_FRAME_RELATED_P (insn) = 1;
7481 if (amount != const0_rtx)
7483 insn = emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
7484 amount));
7485 RTX_FRAME_RELATED_P (insn) = 1;
7486 emit_insn (gen_rtx_CLOBBER (VOIDmode,
7487 gen_rtx_MEM (BLKmode, stack_pointer_rtx)));
7490 /* If we are profiling, make sure no instructions are scheduled before
7491 the call to mcount. Similarly if the user has requested no
7492 scheduling in the prolog. */
7493 if (profile_flag || profile_block_flag || TARGET_NO_SCHED_PRO)
7494 emit_insn (gen_blockage ());
7498 /* If CODE is 'd', then the X is a condition operand and the instruction
7499 should only be executed if the condition is true.
7500 if CODE is 'D', then the X is a condition operand and the instruction
7501 should only be executed if the condition is false: however, if the mode
7502 of the comparison is CCFPEmode, then always execute the instruction -- we
7503 do this because in these circumstances !GE does not necessarily imply LT;
7504 in these cases the instruction pattern will take care to make sure that
7505 an instruction containing %d will follow, thereby undoing the effects of
7506 doing this instruction unconditionally.
7507 If CODE is 'N' then X is a floating point operand that must be negated
7508 before output.
7509 If CODE is 'B' then output a bitwise inverted value of X (a const int).
7510 If X is a REG and CODE is `M', output a ldm/stm style multi-reg. */
7512 void
7513 arm_print_operand (stream, x, code)
7514 FILE * stream;
7515 rtx x;
7516 int code;
7518 switch (code)
7520 case '@':
7521 fputs (ASM_COMMENT_START, stream);
7522 return;
7524 case '_':
7525 fputs (user_label_prefix, stream);
7526 return;
7528 case '|':
7529 fputs (REGISTER_PREFIX, stream);
7530 return;
7532 case '?':
7533 if (arm_ccfsm_state == 3 || arm_ccfsm_state == 4)
7534 fputs (arm_condition_codes[arm_current_cc], stream);
7535 return;
7537 case 'N':
7539 REAL_VALUE_TYPE r;
7540 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
7541 r = REAL_VALUE_NEGATE (r);
7542 fprintf (stream, "%s", fp_const_from_val (&r));
7544 return;
7546 case 'B':
7547 if (GET_CODE (x) == CONST_INT)
7549 HOST_WIDE_INT val;
7550 val = ARM_SIGN_EXTEND (~ INTVAL (x));
7551 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, val);
7553 else
7555 putc ('~', stream);
7556 output_addr_const (stream, x);
7558 return;
7560 case 'i':
7561 fprintf (stream, "%s", arithmetic_instr (x, 1));
7562 return;
7564 case 'I':
7565 fprintf (stream, "%s", arithmetic_instr (x, 0));
7566 return;
7568 case 'S':
7570 HOST_WIDE_INT val;
7571 char * shift = shift_op (x, & val);
7573 if (shift)
7575 fprintf (stream, ", %s ", shift_op (x, & val));
7576 if (val == -1)
7577 arm_print_operand (stream, XEXP (x, 1), 0);
7578 else
7580 fputc ('#', stream);
7581 fprintf (stream, HOST_WIDE_INT_PRINT_DEC, val);
7585 return;
7587 /* An explanation of the 'Q', 'R' and 'H' register operands:
7589 In a pair of registers containing a DI or DF value the 'Q'
7590 operand returns the register number of the register containing
7591 the least signficant part of the value. The 'R' operand returns
7592 the register number of the register containing the most
7593 significant part of the value.
7595 The 'H' operand returns the higher of the two register numbers.
7596 On a run where WORDS_BIG_ENDIAN is true the 'H' operand is the
7597 same as the 'Q' operand, since the most signficant part of the
7598 value is held in the lower number register. The reverse is true
7599 on systems where WORDS_BIG_ENDIAN is false.
7601 The purpose of these operands is to distinguish between cases
7602 where the endian-ness of the values is important (for example
7603 when they are added together), and cases where the endian-ness
7604 is irrelevant, but the order of register operations is important.
7605 For example when loading a value from memory into a register
7606 pair, the endian-ness does not matter. Provided that the value
7607 from the lower memory address is put into the lower numbered
7608 register, and the value from the higher address is put into the
7609 higher numbered register, the load will work regardless of whether
7610 the value being loaded is big-wordian or little-wordian. The
7611 order of the two register loads can matter however, if the address
7612 of the memory location is actually held in one of the registers
7613 being overwritten by the load. */
7614 case 'Q':
7615 if (REGNO (x) > LAST_ARM_REGNUM)
7616 abort ();
7617 asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 1 : 0));
7618 return;
7620 case 'R':
7621 if (REGNO (x) > LAST_ARM_REGNUM)
7622 abort ();
7623 asm_fprintf (stream, "%r", REGNO (x) + (WORDS_BIG_ENDIAN ? 0 : 1));
7624 return;
7626 case 'H':
7627 if (REGNO (x) > LAST_ARM_REGNUM)
7628 abort ();
7629 asm_fprintf (stream, "%r", REGNO (x) + 1);
7630 return;
7632 case 'm':
7633 asm_fprintf (stream, "%r",
7634 GET_CODE (XEXP (x, 0)) == REG
7635 ? REGNO (XEXP (x, 0)) : REGNO (XEXP (XEXP (x, 0), 0)));
7636 return;
7638 case 'M':
7639 asm_fprintf (stream, "{%r-%r}",
7640 REGNO (x),
7641 REGNO (x) + NUM_REGS (GET_MODE (x)) - 1);
7642 return;
7644 case 'd':
7645 if (! x)
7646 return;
7648 if (TARGET_ARM)
7649 fputs (arm_condition_codes[get_arm_condition_code (x)],
7650 stream);
7651 else
7652 fputs (thumb_condition_code (x, 0), stream);
7653 return;
7655 case 'D':
7656 if (! x)
7657 return;
7659 if (TARGET_ARM)
7660 fputs (arm_condition_codes[ARM_INVERSE_CONDITION_CODE
7661 (get_arm_condition_code (x))],
7662 stream);
7663 else
7664 fputs (thumb_condition_code (x, 1), stream);
7665 return;
7667 default:
7668 if (x == 0)
7669 abort ();
7671 if (GET_CODE (x) == REG)
7672 asm_fprintf (stream, "%r", REGNO (x));
7673 else if (GET_CODE (x) == MEM)
7675 output_memory_reference_mode = GET_MODE (x);
7676 output_address (XEXP (x, 0));
7678 else if (GET_CODE (x) == CONST_DOUBLE)
7679 fprintf (stream, "#%s", fp_immediate_constant (x));
7680 else if (GET_CODE (x) == NEG)
7681 abort (); /* This should never happen now. */
7682 else
7684 fputc ('#', stream);
7685 output_addr_const (stream, x);
7690 /* A finite state machine takes care of noticing whether or not instructions
7691 can be conditionally executed, and thus decrease execution time and code
7692 size by deleting branch instructions. The fsm is controlled by
7693 final_prescan_insn, and controls the actions of ASM_OUTPUT_OPCODE. */
7695 /* The state of the fsm controlling condition codes are:
7696 0: normal, do nothing special
7697 1: make ASM_OUTPUT_OPCODE not output this instruction
7698 2: make ASM_OUTPUT_OPCODE not output this instruction
7699 3: make instructions conditional
7700 4: make instructions conditional
7702 State transitions (state->state by whom under condition):
7703 0 -> 1 final_prescan_insn if the `target' is a label
7704 0 -> 2 final_prescan_insn if the `target' is an unconditional branch
7705 1 -> 3 ASM_OUTPUT_OPCODE after not having output the conditional branch
7706 2 -> 4 ASM_OUTPUT_OPCODE after not having output the conditional branch
7707 3 -> 0 ASM_OUTPUT_INTERNAL_LABEL if the `target' label is reached
7708 (the target label has CODE_LABEL_NUMBER equal to arm_target_label).
7709 4 -> 0 final_prescan_insn if the `target' unconditional branch is reached
7710 (the target insn is arm_target_insn).
7712 If the jump clobbers the conditions then we use states 2 and 4.
7714 A similar thing can be done with conditional return insns.
7716 XXX In case the `target' is an unconditional branch, this conditionalising
7717 of the instructions always reduces code size, but not always execution
7718 time. But then, I want to reduce the code size to somewhere near what
7719 /bin/cc produces. */
7721 /* Returns the index of the ARM condition code string in
7722 `arm_condition_codes'. COMPARISON should be an rtx like
7723 `(eq (...) (...))'. */
7725 static enum arm_cond_code
7726 get_arm_condition_code (comparison)
7727 rtx comparison;
7729 enum machine_mode mode = GET_MODE (XEXP (comparison, 0));
7730 register int code;
7731 register enum rtx_code comp_code = GET_CODE (comparison);
7733 if (GET_MODE_CLASS (mode) != MODE_CC)
7734 mode = SELECT_CC_MODE (comp_code, XEXP (comparison, 0),
7735 XEXP (comparison, 1));
7737 switch (mode)
7739 case CC_DNEmode: code = ARM_NE; goto dominance;
7740 case CC_DEQmode: code = ARM_EQ; goto dominance;
7741 case CC_DGEmode: code = ARM_GE; goto dominance;
7742 case CC_DGTmode: code = ARM_GT; goto dominance;
7743 case CC_DLEmode: code = ARM_LE; goto dominance;
7744 case CC_DLTmode: code = ARM_LT; goto dominance;
7745 case CC_DGEUmode: code = ARM_CS; goto dominance;
7746 case CC_DGTUmode: code = ARM_HI; goto dominance;
7747 case CC_DLEUmode: code = ARM_LS; goto dominance;
7748 case CC_DLTUmode: code = ARM_CC;
7750 dominance:
7751 if (comp_code != EQ && comp_code != NE)
7752 abort ();
7754 if (comp_code == EQ)
7755 return ARM_INVERSE_CONDITION_CODE (code);
7756 return code;
7758 case CC_NOOVmode:
7759 switch (comp_code)
7761 case NE: return ARM_NE;
7762 case EQ: return ARM_EQ;
7763 case GE: return ARM_PL;
7764 case LT: return ARM_MI;
7765 default: abort ();
7768 case CC_Zmode:
7769 case CCFPmode:
7770 switch (comp_code)
7772 case NE: return ARM_NE;
7773 case EQ: return ARM_EQ;
7774 default: abort ();
7777 case CCFPEmode:
7778 switch (comp_code)
7780 case GE: return ARM_GE;
7781 case GT: return ARM_GT;
7782 case LE: return ARM_LS;
7783 case LT: return ARM_MI;
7784 default: abort ();
7787 case CC_SWPmode:
7788 switch (comp_code)
7790 case NE: return ARM_NE;
7791 case EQ: return ARM_EQ;
7792 case GE: return ARM_LE;
7793 case GT: return ARM_LT;
7794 case LE: return ARM_GE;
7795 case LT: return ARM_GT;
7796 case GEU: return ARM_LS;
7797 case GTU: return ARM_CC;
7798 case LEU: return ARM_CS;
7799 case LTU: return ARM_HI;
7800 default: abort ();
7803 case CC_Cmode:
7804 switch (comp_code)
7806 case LTU: return ARM_CS;
7807 case GEU: return ARM_CC;
7808 default: abort ();
7811 case CCmode:
7812 switch (comp_code)
7814 case NE: return ARM_NE;
7815 case EQ: return ARM_EQ;
7816 case GE: return ARM_GE;
7817 case GT: return ARM_GT;
7818 case LE: return ARM_LE;
7819 case LT: return ARM_LT;
7820 case GEU: return ARM_CS;
7821 case GTU: return ARM_HI;
7822 case LEU: return ARM_LS;
7823 case LTU: return ARM_CC;
7824 default: abort ();
7827 default: abort ();
7830 abort ();
7834 void
7835 arm_final_prescan_insn (insn)
7836 rtx insn;
7838 /* BODY will hold the body of INSN. */
7839 register rtx body = PATTERN (insn);
7841 /* This will be 1 if trying to repeat the trick, and things need to be
7842 reversed if it appears to fail. */
7843 int reverse = 0;
7845 /* JUMP_CLOBBERS will be one implies that the conditions if a branch is
7846 taken are clobbered, even if the rtl suggests otherwise. It also
7847 means that we have to grub around within the jump expression to find
7848 out what the conditions are when the jump isn't taken. */
7849 int jump_clobbers = 0;
7851 /* If we start with a return insn, we only succeed if we find another one. */
7852 int seeking_return = 0;
7854 /* START_INSN will hold the insn from where we start looking. This is the
7855 first insn after the following code_label if REVERSE is true. */
7856 rtx start_insn = insn;
7858 /* If in state 4, check if the target branch is reached, in order to
7859 change back to state 0. */
7860 if (arm_ccfsm_state == 4)
7862 if (insn == arm_target_insn)
7864 arm_target_insn = NULL;
7865 arm_ccfsm_state = 0;
7867 return;
7870 /* If in state 3, it is possible to repeat the trick, if this insn is an
7871 unconditional branch to a label, and immediately following this branch
7872 is the previous target label which is only used once, and the label this
7873 branch jumps to is not too far off. */
7874 if (arm_ccfsm_state == 3)
7876 if (simplejump_p (insn))
7878 start_insn = next_nonnote_insn (start_insn);
7879 if (GET_CODE (start_insn) == BARRIER)
7881 /* XXX Isn't this always a barrier? */
7882 start_insn = next_nonnote_insn (start_insn);
7884 if (GET_CODE (start_insn) == CODE_LABEL
7885 && CODE_LABEL_NUMBER (start_insn) == arm_target_label
7886 && LABEL_NUSES (start_insn) == 1)
7887 reverse = TRUE;
7888 else
7889 return;
7891 else if (GET_CODE (body) == RETURN)
7893 start_insn = next_nonnote_insn (start_insn);
7894 if (GET_CODE (start_insn) == BARRIER)
7895 start_insn = next_nonnote_insn (start_insn);
7896 if (GET_CODE (start_insn) == CODE_LABEL
7897 && CODE_LABEL_NUMBER (start_insn) == arm_target_label
7898 && LABEL_NUSES (start_insn) == 1)
7900 reverse = TRUE;
7901 seeking_return = 1;
7903 else
7904 return;
7906 else
7907 return;
7910 if (arm_ccfsm_state != 0 && !reverse)
7911 abort ();
7912 if (GET_CODE (insn) != JUMP_INSN)
7913 return;
7915 /* This jump might be paralleled with a clobber of the condition codes
7916 the jump should always come first */
7917 if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
7918 body = XVECEXP (body, 0, 0);
7920 #if 0
7921 /* If this is a conditional return then we don't want to know */
7922 if (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == PC
7923 && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE
7924 && (GET_CODE (XEXP (SET_SRC (body), 1)) == RETURN
7925 || GET_CODE (XEXP (SET_SRC (body), 2)) == RETURN))
7926 return;
7927 #endif
7929 if (reverse
7930 || (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == PC
7931 && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE))
7933 int insns_skipped;
7934 int fail = FALSE, succeed = FALSE;
7935 /* Flag which part of the IF_THEN_ELSE is the LABEL_REF. */
7936 int then_not_else = TRUE;
7937 rtx this_insn = start_insn, label = 0;
7939 if (get_attr_conds (insn) == CONDS_JUMP_CLOB)
7941 /* The code below is wrong for these, and I haven't time to
7942 fix it now. So we just do the safe thing and return. This
7943 whole function needs re-writing anyway. */
7944 jump_clobbers = 1;
7945 return;
7948 /* Register the insn jumped to. */
7949 if (reverse)
7951 if (!seeking_return)
7952 label = XEXP (SET_SRC (body), 0);
7954 else if (GET_CODE (XEXP (SET_SRC (body), 1)) == LABEL_REF)
7955 label = XEXP (XEXP (SET_SRC (body), 1), 0);
7956 else if (GET_CODE (XEXP (SET_SRC (body), 2)) == LABEL_REF)
7958 label = XEXP (XEXP (SET_SRC (body), 2), 0);
7959 then_not_else = FALSE;
7961 else if (GET_CODE (XEXP (SET_SRC (body), 1)) == RETURN)
7962 seeking_return = 1;
7963 else if (GET_CODE (XEXP (SET_SRC (body), 2)) == RETURN)
7965 seeking_return = 1;
7966 then_not_else = FALSE;
7968 else
7969 abort ();
7971 /* See how many insns this branch skips, and what kind of insns. If all
7972 insns are okay, and the label or unconditional branch to the same
7973 label is not too far away, succeed. */
7974 for (insns_skipped = 0;
7975 !fail && !succeed && insns_skipped++ < max_insns_skipped;)
7977 rtx scanbody;
7979 this_insn = next_nonnote_insn (this_insn);
7980 if (!this_insn)
7981 break;
7983 switch (GET_CODE (this_insn))
7985 case CODE_LABEL:
7986 /* Succeed if it is the target label, otherwise fail since
7987 control falls in from somewhere else. */
7988 if (this_insn == label)
7990 if (jump_clobbers)
7992 arm_ccfsm_state = 2;
7993 this_insn = next_nonnote_insn (this_insn);
7995 else
7996 arm_ccfsm_state = 1;
7997 succeed = TRUE;
7999 else
8000 fail = TRUE;
8001 break;
8003 case BARRIER:
8004 /* Succeed if the following insn is the target label.
8005 Otherwise fail.
8006 If return insns are used then the last insn in a function
8007 will be a barrier. */
8008 this_insn = next_nonnote_insn (this_insn);
8009 if (this_insn && this_insn == label)
8011 if (jump_clobbers)
8013 arm_ccfsm_state = 2;
8014 this_insn = next_nonnote_insn (this_insn);
8016 else
8017 arm_ccfsm_state = 1;
8018 succeed = TRUE;
8020 else
8021 fail = TRUE;
8022 break;
8024 case CALL_INSN:
8025 /* If using 32-bit addresses the cc is not preserved over
8026 calls. */
8027 if (TARGET_APCS_32)
8029 /* Succeed if the following insn is the target label,
8030 or if the following two insns are a barrier and
8031 the target label. */
8032 this_insn = next_nonnote_insn (this_insn);
8033 if (this_insn && GET_CODE (this_insn) == BARRIER)
8034 this_insn = next_nonnote_insn (this_insn);
8036 if (this_insn && this_insn == label
8037 && insns_skipped < max_insns_skipped)
8039 if (jump_clobbers)
8041 arm_ccfsm_state = 2;
8042 this_insn = next_nonnote_insn (this_insn);
8044 else
8045 arm_ccfsm_state = 1;
8046 succeed = TRUE;
8048 else
8049 fail = TRUE;
8051 break;
8053 case JUMP_INSN:
8054 /* If this is an unconditional branch to the same label, succeed.
8055 If it is to another label, do nothing. If it is conditional,
8056 fail. */
8057 /* XXX Probably, the tests for SET and the PC are unnecessary. */
8059 scanbody = PATTERN (this_insn);
8060 if (GET_CODE (scanbody) == SET
8061 && GET_CODE (SET_DEST (scanbody)) == PC)
8063 if (GET_CODE (SET_SRC (scanbody)) == LABEL_REF
8064 && XEXP (SET_SRC (scanbody), 0) == label && !reverse)
8066 arm_ccfsm_state = 2;
8067 succeed = TRUE;
8069 else if (GET_CODE (SET_SRC (scanbody)) == IF_THEN_ELSE)
8070 fail = TRUE;
8072 /* Fail if a conditional return is undesirable (eg on a
8073 StrongARM), but still allow this if optimizing for size. */
8074 else if (GET_CODE (scanbody) == RETURN
8075 && ! use_return_insn (TRUE)
8076 && ! optimize_size)
8077 fail = TRUE;
8078 else if (GET_CODE (scanbody) == RETURN
8079 && seeking_return)
8081 arm_ccfsm_state = 2;
8082 succeed = TRUE;
8084 else if (GET_CODE (scanbody) == PARALLEL)
8086 switch (get_attr_conds (this_insn))
8088 case CONDS_NOCOND:
8089 break;
8090 default:
8091 fail = TRUE;
8092 break;
8095 break;
8097 case INSN:
8098 /* Instructions using or affecting the condition codes make it
8099 fail. */
8100 scanbody = PATTERN (this_insn);
8101 if (! (GET_CODE (scanbody) == SET
8102 || GET_CODE (scanbody) == PARALLEL)
8103 || get_attr_conds (this_insn) != CONDS_NOCOND)
8104 fail = TRUE;
8105 break;
8107 default:
8108 break;
8111 if (succeed)
8113 if ((!seeking_return) && (arm_ccfsm_state == 1 || reverse))
8114 arm_target_label = CODE_LABEL_NUMBER (label);
8115 else if (seeking_return || arm_ccfsm_state == 2)
8117 while (this_insn && GET_CODE (PATTERN (this_insn)) == USE)
8119 this_insn = next_nonnote_insn (this_insn);
8120 if (this_insn && (GET_CODE (this_insn) == BARRIER
8121 || GET_CODE (this_insn) == CODE_LABEL))
8122 abort ();
8124 if (!this_insn)
8126 /* Oh, dear! we ran off the end.. give up */
8127 recog (PATTERN (insn), insn, NULL_PTR);
8128 arm_ccfsm_state = 0;
8129 arm_target_insn = NULL;
8130 return;
8132 arm_target_insn = this_insn;
8134 else
8135 abort ();
8136 if (jump_clobbers)
8138 if (reverse)
8139 abort ();
8140 arm_current_cc =
8141 get_arm_condition_code (XEXP (XEXP (XEXP (SET_SRC (body),
8142 0), 0), 1));
8143 if (GET_CODE (XEXP (XEXP (SET_SRC (body), 0), 0)) == AND)
8144 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
8145 if (GET_CODE (XEXP (SET_SRC (body), 0)) == NE)
8146 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
8148 else
8150 /* If REVERSE is true, ARM_CURRENT_CC needs to be inverted from
8151 what it was. */
8152 if (!reverse)
8153 arm_current_cc = get_arm_condition_code (XEXP (SET_SRC (body),
8154 0));
8157 if (reverse || then_not_else)
8158 arm_current_cc = ARM_INVERSE_CONDITION_CODE (arm_current_cc);
8161 /* Restore recog_data (getting the attributes of other insns can
8162 destroy this array, but final.c assumes that it remains intact
8163 across this call; since the insn has been recognized already we
8164 call recog direct). */
8165 recog (PATTERN (insn), insn, NULL_PTR);
8170 arm_regno_class (regno)
8171 int regno;
8173 if (TARGET_THUMB)
8175 if (regno == STACK_POINTER_REGNUM)
8176 return STACK_REG;
8177 if (regno == CC_REGNUM)
8178 return CC_REG;
8179 if (regno < 8)
8180 return LO_REGS;
8181 return HI_REGS;
8184 if ( regno <= LAST_ARM_REGNUM
8185 || regno == FRAME_POINTER_REGNUM
8186 || regno == ARG_POINTER_REGNUM)
8187 return GENERAL_REGS;
8189 if (regno == CC_REGNUM)
8190 return NO_REGS;
8192 return FPU_REGS;
8195 /* Handle a special case when computing the offset
8196 of an argument from the frame pointer. */
8198 arm_debugger_arg_offset (value, addr)
8199 int value;
8200 rtx addr;
8202 rtx insn;
8204 /* We are only interested if dbxout_parms() failed to compute the offset. */
8205 if (value != 0)
8206 return 0;
8208 /* We can only cope with the case where the address is held in a register. */
8209 if (GET_CODE (addr) != REG)
8210 return 0;
8212 /* If we are using the frame pointer to point at the argument, then
8213 an offset of 0 is correct. */
8214 if (REGNO (addr) == HARD_FRAME_POINTER_REGNUM)
8215 return 0;
8217 /* If we are using the stack pointer to point at the
8218 argument, then an offset of 0 is correct. */
8219 if ((TARGET_THUMB || ! frame_pointer_needed)
8220 && REGNO (addr) == SP_REGNUM)
8221 return 0;
8223 /* Oh dear. The argument is pointed to by a register rather
8224 than being held in a register, or being stored at a known
8225 offset from the frame pointer. Since GDB only understands
8226 those two kinds of argument we must translate the address
8227 held in the register into an offset from the frame pointer.
8228 We do this by searching through the insns for the function
8229 looking to see where this register gets its value. If the
8230 register is initialised from the frame pointer plus an offset
8231 then we are in luck and we can continue, otherwise we give up.
8233 This code is exercised by producing debugging information
8234 for a function with arguments like this:
8236 double func (double a, double b, int c, double d) {return d;}
8238 Without this code the stab for parameter 'd' will be set to
8239 an offset of 0 from the frame pointer, rather than 8. */
8241 /* The if() statement says:
8243 If the insn is a normal instruction
8244 and if the insn is setting the value in a register
8245 and if the register being set is the register holding the address of the argument
8246 and if the address is computing by an addition
8247 that involves adding to a register
8248 which is the frame pointer
8249 a constant integer
8251 then... */
8253 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
8255 if ( GET_CODE (insn) == INSN
8256 && GET_CODE (PATTERN (insn)) == SET
8257 && REGNO (XEXP (PATTERN (insn), 0)) == REGNO (addr)
8258 && GET_CODE (XEXP (PATTERN (insn), 1)) == PLUS
8259 && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 0)) == REG
8260 && REGNO (XEXP (XEXP (PATTERN (insn), 1), 0)) == HARD_FRAME_POINTER_REGNUM
8261 && GET_CODE (XEXP (XEXP (PATTERN (insn), 1), 1)) == CONST_INT
8264 value = INTVAL (XEXP (XEXP (PATTERN (insn), 1), 1));
8266 break;
8270 if (value == 0)
8272 debug_rtx (addr);
8273 warning ("Unable to compute real location of stacked parameter");
8274 value = 8; /* XXX magic hack */
8277 return value;
8281 /* Recursively search through all of the blocks in a function
8282 checking to see if any of the variables created in that
8283 function match the RTX called 'orig'. If they do then
8284 replace them with the RTX called 'new'. */
8286 static void
8287 replace_symbols_in_block (block, orig, new)
8288 tree block;
8289 rtx orig;
8290 rtx new;
8292 for (; block; block = BLOCK_CHAIN (block))
8294 tree sym;
8296 if (! TREE_USED (block))
8297 continue;
8299 for (sym = BLOCK_VARS (block); sym; sym = TREE_CHAIN (sym))
8301 if ( (DECL_NAME (sym) == 0 && TREE_CODE (sym) != TYPE_DECL)
8302 || DECL_IGNORED_P (sym)
8303 || TREE_CODE (sym) != VAR_DECL
8304 || DECL_EXTERNAL (sym)
8305 || ! rtx_equal_p (DECL_RTL (sym), orig)
8307 continue;
8309 DECL_RTL (sym) = new;
8312 replace_symbols_in_block (BLOCK_SUBBLOCKS (block), orig, new);
8316 /* Return the number (counting from 0) of the least significant set
8317 bit in MASK. */
8318 #ifdef __GNUC__
8319 inline
8320 #endif
8321 static int
8322 number_of_first_bit_set (mask)
8323 int mask;
8325 int bit;
8327 for (bit = 0;
8328 (mask & (1 << bit)) == 0;
8329 ++ bit)
8330 continue;
8332 return bit;
8335 /* Generate code to return from a thumb function.
8336 If 'reg_containing_return_addr' is -1, then the return address is
8337 actually on the stack, at the stack pointer. */
8338 static void
8339 thumb_exit (f, reg_containing_return_addr, eh_ofs)
8340 FILE * f;
8341 int reg_containing_return_addr;
8342 rtx eh_ofs;
8344 unsigned regs_available_for_popping;
8345 unsigned regs_to_pop;
8346 int pops_needed;
8347 unsigned available;
8348 unsigned required;
8349 int mode;
8350 int size;
8351 int restore_a4 = FALSE;
8353 /* Compute the registers we need to pop. */
8354 regs_to_pop = 0;
8355 pops_needed = 0;
8357 /* There is an assumption here, that if eh_ofs is not NULL, the
8358 normal return address will have been pushed. */
8359 if (reg_containing_return_addr == -1 || eh_ofs)
8361 /* When we are generating a return for __builtin_eh_return,
8362 reg_containing_return_addr must specify the return regno. */
8363 if (eh_ofs && reg_containing_return_addr == -1)
8364 abort ();
8366 regs_to_pop |= 1 << LR_REGNUM;
8367 ++ pops_needed;
8370 if (TARGET_BACKTRACE)
8372 /* Restore the (ARM) frame pointer and stack pointer. */
8373 regs_to_pop |= (1 << ARM_HARD_FRAME_POINTER_REGNUM) | (1 << SP_REGNUM);
8374 pops_needed += 2;
8377 /* If there is nothing to pop then just emit the BX instruction and
8378 return. */
8379 if (pops_needed == 0)
8381 if (eh_ofs)
8382 asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, REGNO (eh_ofs));
8384 asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
8385 return;
8387 /* Otherwise if we are not supporting interworking and we have not created
8388 a backtrace structure and the function was not entered in ARM mode then
8389 just pop the return address straight into the PC. */
8390 else if ( ! TARGET_INTERWORK
8391 && ! TARGET_BACKTRACE
8392 && ! is_called_in_ARM_mode (current_function_decl))
8394 if (eh_ofs)
8396 asm_fprintf (f, "\tadd\t%r, #4\n", SP_REGNUM);
8397 asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, REGNO (eh_ofs));
8398 asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
8400 else
8401 asm_fprintf (f, "\tpop\t{%r}\n", PC_REGNUM);
8403 return;
8406 /* Find out how many of the (return) argument registers we can corrupt. */
8407 regs_available_for_popping = 0;
8409 /* If returning via __builtin_eh_return, the bottom three registers
8410 all contain information needed for the return. */
8411 if (eh_ofs)
8412 size = 12;
8413 else
8415 #ifdef RTX_CODE
8416 /* If we can deduce the registers used from the function's
8417 return value. This is more reliable that examining
8418 regs_ever_live[] because that will be set if the register is
8419 ever used in the function, not just if the register is used
8420 to hold a return value. */
8422 if (current_function_return_rtx != 0)
8423 mode = GET_MODE (current_function_return_rtx);
8424 else
8425 #endif
8426 mode = DECL_MODE (DECL_RESULT (current_function_decl));
8428 size = GET_MODE_SIZE (mode);
8430 if (size == 0)
8432 /* In a void function we can use any argument register.
8433 In a function that returns a structure on the stack
8434 we can use the second and third argument registers. */
8435 if (mode == VOIDmode)
8436 regs_available_for_popping =
8437 (1 << ARG_REGISTER (1))
8438 | (1 << ARG_REGISTER (2))
8439 | (1 << ARG_REGISTER (3));
8440 else
8441 regs_available_for_popping =
8442 (1 << ARG_REGISTER (2))
8443 | (1 << ARG_REGISTER (3));
8445 else if (size <= 4)
8446 regs_available_for_popping =
8447 (1 << ARG_REGISTER (2))
8448 | (1 << ARG_REGISTER (3));
8449 else if (size <= 8)
8450 regs_available_for_popping =
8451 (1 << ARG_REGISTER (3));
8454 /* Match registers to be popped with registers into which we pop them. */
8455 for (available = regs_available_for_popping,
8456 required = regs_to_pop;
8457 required != 0 && available != 0;
8458 available &= ~(available & - available),
8459 required &= ~(required & - required))
8460 -- pops_needed;
8462 /* If we have any popping registers left over, remove them. */
8463 if (available > 0)
8464 regs_available_for_popping &= ~ available;
8466 /* Otherwise if we need another popping register we can use
8467 the fourth argument register. */
8468 else if (pops_needed)
8470 /* If we have not found any free argument registers and
8471 reg a4 contains the return address, we must move it. */
8472 if (regs_available_for_popping == 0
8473 && reg_containing_return_addr == LAST_ARG_REGNUM)
8475 asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
8476 reg_containing_return_addr = LR_REGNUM;
8478 else if (size > 12)
8480 /* Register a4 is being used to hold part of the return value,
8481 but we have dire need of a free, low register. */
8482 restore_a4 = TRUE;
8484 asm_fprintf (f, "\tmov\t%r, %r\n",IP_REGNUM, LAST_ARG_REGNUM);
8487 if (reg_containing_return_addr != LAST_ARG_REGNUM)
8489 /* The fourth argument register is available. */
8490 regs_available_for_popping |= 1 << LAST_ARG_REGNUM;
8492 -- pops_needed;
8496 /* Pop as many registers as we can. */
8497 thumb_pushpop (f, regs_available_for_popping, FALSE);
8499 /* Process the registers we popped. */
8500 if (reg_containing_return_addr == -1)
8502 /* The return address was popped into the lowest numbered register. */
8503 regs_to_pop &= ~ (1 << LR_REGNUM);
8505 reg_containing_return_addr =
8506 number_of_first_bit_set (regs_available_for_popping);
8508 /* Remove this register for the mask of available registers, so that
8509 the return address will not be corrupted by futher pops. */
8510 regs_available_for_popping &= ~ (1 << reg_containing_return_addr);
8513 /* If we popped other registers then handle them here. */
8514 if (regs_available_for_popping)
8516 int frame_pointer;
8518 /* Work out which register currently contains the frame pointer. */
8519 frame_pointer = number_of_first_bit_set (regs_available_for_popping);
8521 /* Move it into the correct place. */
8522 asm_fprintf (f, "\tmov\t%r, %r\n",
8523 ARM_HARD_FRAME_POINTER_REGNUM, frame_pointer);
8525 /* (Temporarily) remove it from the mask of popped registers. */
8526 regs_available_for_popping &= ~ (1 << frame_pointer);
8527 regs_to_pop &= ~ (1 << ARM_HARD_FRAME_POINTER_REGNUM);
8529 if (regs_available_for_popping)
8531 int stack_pointer;
8533 /* We popped the stack pointer as well,
8534 find the register that contains it. */
8535 stack_pointer = number_of_first_bit_set (regs_available_for_popping);
8537 /* Move it into the stack register. */
8538 asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, stack_pointer);
8540 /* At this point we have popped all necessary registers, so
8541 do not worry about restoring regs_available_for_popping
8542 to its correct value:
8544 assert (pops_needed == 0)
8545 assert (regs_available_for_popping == (1 << frame_pointer))
8546 assert (regs_to_pop == (1 << STACK_POINTER)) */
8548 else
8550 /* Since we have just move the popped value into the frame
8551 pointer, the popping register is available for reuse, and
8552 we know that we still have the stack pointer left to pop. */
8553 regs_available_for_popping |= (1 << frame_pointer);
8557 /* If we still have registers left on the stack, but we no longer have
8558 any registers into which we can pop them, then we must move the return
8559 address into the link register and make available the register that
8560 contained it. */
8561 if (regs_available_for_popping == 0 && pops_needed > 0)
8563 regs_available_for_popping |= 1 << reg_containing_return_addr;
8565 asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM,
8566 reg_containing_return_addr);
8568 reg_containing_return_addr = LR_REGNUM;
8571 /* If we have registers left on the stack then pop some more.
8572 We know that at most we will want to pop FP and SP. */
8573 if (pops_needed > 0)
8575 int popped_into;
8576 int move_to;
8578 thumb_pushpop (f, regs_available_for_popping, FALSE);
8580 /* We have popped either FP or SP.
8581 Move whichever one it is into the correct register. */
8582 popped_into = number_of_first_bit_set (regs_available_for_popping);
8583 move_to = number_of_first_bit_set (regs_to_pop);
8585 asm_fprintf (f, "\tmov\t%r, %r\n", move_to, popped_into);
8587 regs_to_pop &= ~ (1 << move_to);
8589 -- pops_needed;
8592 /* If we still have not popped everything then we must have only
8593 had one register available to us and we are now popping the SP. */
8594 if (pops_needed > 0)
8596 int popped_into;
8598 thumb_pushpop (f, regs_available_for_popping, FALSE);
8600 popped_into = number_of_first_bit_set (regs_available_for_popping);
8602 asm_fprintf (f, "\tmov\t%r, %r\n", SP_REGNUM, popped_into);
8604 assert (regs_to_pop == (1 << STACK_POINTER))
8605 assert (pops_needed == 1)
8609 /* If necessary restore the a4 register. */
8610 if (restore_a4)
8612 if (reg_containing_return_addr != LR_REGNUM)
8614 asm_fprintf (f, "\tmov\t%r, %r\n", LR_REGNUM, LAST_ARG_REGNUM);
8615 reg_containing_return_addr = LR_REGNUM;
8618 asm_fprintf (f, "\tmov\t%r, %r\n", LAST_ARG_REGNUM, IP_REGNUM);
8621 if (eh_ofs)
8622 asm_fprintf (f, "\tadd\t%r, %r\n", SP_REGNUM, REGNO (eh_ofs));
8624 /* Return to caller. */
8625 asm_fprintf (f, "\tbx\t%r\n", reg_containing_return_addr);
8628 /* Emit code to push or pop registers to or from the stack. */
8629 static void
8630 thumb_pushpop (f, mask, push)
8631 FILE * f;
8632 int mask;
8633 int push;
8635 int regno;
8636 int lo_mask = mask & 0xFF;
8638 if (lo_mask == 0 && ! push && (mask & (1 << 15)))
8640 /* Special case. Do not generate a POP PC statement here, do it in
8641 thumb_exit() */
8642 thumb_exit (f, -1, NULL_RTX);
8643 return;
8646 fprintf (f, "\t%s\t{", push ? "push" : "pop");
8648 /* Look at the low registers first. */
8649 for (regno = 0; regno <= LAST_LO_REGNUM; regno ++, lo_mask >>= 1)
8651 if (lo_mask & 1)
8653 asm_fprintf (f, "%r", regno);
8655 if ((lo_mask & ~1) != 0)
8656 fprintf (f, ", ");
8660 if (push && (mask & (1 << LR_REGNUM)))
8662 /* Catch pushing the LR. */
8663 if (mask & 0xFF)
8664 fprintf (f, ", ");
8666 asm_fprintf (f, "%r", LR_REGNUM);
8668 else if (!push && (mask & (1 << PC_REGNUM)))
8670 /* Catch popping the PC. */
8671 if (TARGET_INTERWORK || TARGET_BACKTRACE)
8673 /* The PC is never poped directly, instead
8674 it is popped into r3 and then BX is used. */
8675 fprintf (f, "}\n");
8677 thumb_exit (f, -1, NULL_RTX);
8679 return;
8681 else
8683 if (mask & 0xFF)
8684 fprintf (f, ", ");
8686 asm_fprintf (f, "%r", PC_REGNUM);
8690 fprintf (f, "}\n");
8693 void
8694 thumb_final_prescan_insn (insn)
8695 rtx insn;
8697 extern int * insn_addresses;
8699 if (flag_print_asm_name)
8700 asm_fprintf (asm_out_file, "%@ 0x%04x\n", insn_addresses[INSN_UID (insn)]);
8704 thumb_shiftable_const (val)
8705 unsigned HOST_WIDE_INT val;
8707 unsigned HOST_WIDE_INT mask = 0xff;
8708 int i;
8710 if (val == 0) /* XXX */
8711 return 0;
8713 for (i = 0; i < 25; i++)
8714 if ((val & (mask << i)) == val)
8715 return 1;
8717 return 0;
8720 /* Returns non-zero if the current function contains,
8721 or might contain a far jump. */
8723 thumb_far_jump_used_p (int in_prologue)
8725 rtx insn;
8727 /* This test is only important for leaf functions. */
8728 /* assert (! leaf_function_p ()); */
8730 /* If we have already decided that far jumps may be used,
8731 do not bother checking again, and always return true even if
8732 it turns out that they are not being used. Once we have made
8733 the decision that far jumps are present (and that hence the link
8734 register will be pushed onto the stack) we cannot go back on it. */
8735 if (cfun->machine->far_jump_used)
8736 return 1;
8738 /* If this function is not being called from the prologue/epilogue
8739 generation code then it must be being called from the
8740 INITIAL_ELIMINATION_OFFSET macro. */
8741 if (! in_prologue)
8743 /* In this case we know that we are being asked about the elimination
8744 of the arg pointer register. If that register is not being used,
8745 then there are no arguments on the stack, and we do not have to
8746 worry that a far jump might force the prologue to push the link
8747 register, changing the stack offsets. In this case we can just
8748 return false, since the presence of far jumps in the function will
8749 not affect stack offsets.
8751 If the arg pointer is live (or if it was live, but has now been
8752 eliminated and so set to dead) then we do have to test to see if
8753 the function might contain a far jump. This test can lead to some
8754 false negatives, since before reload is completed, then length of
8755 branch instructions is not known, so gcc defaults to returning their
8756 longest length, which in turn sets the far jump attribute to true.
8758 A false negative will not result in bad code being generated, but it
8759 will result in a needless push and pop of the link register. We
8760 hope that this does not occur too often. */
8761 if (regs_ever_live [ARG_POINTER_REGNUM])
8762 cfun->machine->arg_pointer_live = 1;
8763 else if (! cfun->machine->arg_pointer_live)
8764 return 0;
8767 /* Check to see if the function contains a branch
8768 insn with the far jump attribute set. */
8769 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
8771 if (GET_CODE (insn) == JUMP_INSN
8772 /* Ignore tablejump patterns. */
8773 && GET_CODE (PATTERN (insn)) != ADDR_VEC
8774 && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC
8775 && get_attr_far_jump (insn) == FAR_JUMP_YES
8778 /* Record the fact that we have decied that
8779 the function does use far jumps. */
8780 cfun->machine->far_jump_used = 1;
8781 return 1;
8785 return 0;
8788 /* Return non-zero if FUNC must be entered in ARM mode. */
8790 is_called_in_ARM_mode (func)
8791 tree func;
8793 if (TREE_CODE (func) != FUNCTION_DECL)
8794 abort ();
8796 /* Ignore the problem about functions whoes address is taken. */
8797 if (TARGET_CALLEE_INTERWORKING && TREE_PUBLIC (func))
8798 return TRUE;
8800 #ifdef ARM_PE
8801 return lookup_attribute ("interfacearm", DECL_MACHINE_ATTRIBUTES (func)) != NULL_TREE;
8802 #else
8803 return FALSE;
8804 #endif
8807 /* The bits which aren't usefully expanded as rtl. */
8808 char *
8809 thumb_unexpanded_epilogue ()
8811 int regno;
8812 int live_regs_mask = 0;
8813 int high_regs_pushed = 0;
8814 int leaf_function = leaf_function_p ();
8815 int had_to_push_lr;
8816 rtx eh_ofs = cfun->machine->eh_epilogue_sp_ofs;
8818 if (return_used_this_function)
8819 return "";
8821 for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
8822 if (regs_ever_live[regno] && ! call_used_regs[regno]
8823 && ! (TARGET_SINGLE_PIC_BASE && (regno == arm_pic_register)))
8824 live_regs_mask |= 1 << regno;
8826 for (regno = 8; regno < 13; regno++)
8828 if (regs_ever_live[regno] && ! call_used_regs[regno]
8829 && ! (TARGET_SINGLE_PIC_BASE && (regno == arm_pic_register)))
8830 high_regs_pushed ++;
8833 /* The prolog may have pushed some high registers to use as
8834 work registers. eg the testuite file:
8835 gcc/testsuite/gcc/gcc.c-torture/execute/complex-2.c
8836 compiles to produce:
8837 push {r4, r5, r6, r7, lr}
8838 mov r7, r9
8839 mov r6, r8
8840 push {r6, r7}
8841 as part of the prolog. We have to undo that pushing here. */
8843 if (high_regs_pushed)
8845 int mask = live_regs_mask;
8846 int next_hi_reg;
8847 int size;
8848 int mode;
8850 #ifdef RTX_CODE
8851 /* If we can deduce the registers used from the function's return value.
8852 This is more reliable that examining regs_ever_live[] because that
8853 will be set if the register is ever used in the function, not just if
8854 the register is used to hold a return value. */
8856 if (current_function_return_rtx != 0)
8857 mode = GET_MODE (current_function_return_rtx);
8858 else
8859 #endif
8860 mode = DECL_MODE (DECL_RESULT (current_function_decl));
8862 size = GET_MODE_SIZE (mode);
8864 /* Unless we are returning a type of size > 12 register r3 is
8865 available. */
8866 if (size < 13)
8867 mask |= 1 << 3;
8869 if (mask == 0)
8870 /* Oh dear! We have no low registers into which we can pop
8871 high registers! */
8872 fatal ("No low registers available for popping high registers");
8874 for (next_hi_reg = 8; next_hi_reg < 13; next_hi_reg++)
8875 if (regs_ever_live[next_hi_reg] && ! call_used_regs[next_hi_reg]
8876 && ! (TARGET_SINGLE_PIC_BASE && (next_hi_reg == arm_pic_register)))
8877 break;
8879 while (high_regs_pushed)
8881 /* Find lo register(s) into which the high register(s) can
8882 be popped. */
8883 for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
8885 if (mask & (1 << regno))
8886 high_regs_pushed--;
8887 if (high_regs_pushed == 0)
8888 break;
8891 mask &= (2 << regno) - 1; /* A noop if regno == 8 */
8893 /* Pop the values into the low register(s). */
8894 thumb_pushpop (asm_out_file, mask, 0);
8896 /* Move the value(s) into the high registers. */
8897 for (regno = 0; regno <= LAST_LO_REGNUM; regno++)
8899 if (mask & (1 << regno))
8901 asm_fprintf (asm_out_file, "\tmov\t%r, %r\n", next_hi_reg,
8902 regno);
8904 for (next_hi_reg++; next_hi_reg < 13; next_hi_reg++)
8905 if (regs_ever_live[next_hi_reg] &&
8906 ! call_used_regs[next_hi_reg]
8907 && ! (TARGET_SINGLE_PIC_BASE
8908 && (next_hi_reg == arm_pic_register)))
8909 break;
8915 had_to_push_lr = (live_regs_mask || ! leaf_function
8916 || thumb_far_jump_used_p (1));
8918 if (TARGET_BACKTRACE
8919 && ((live_regs_mask & 0xFF) == 0)
8920 && regs_ever_live [LAST_ARG_REGNUM] != 0)
8922 /* The stack backtrace structure creation code had to
8923 push R7 in order to get a work register, so we pop
8924 it now. */
8925 live_regs_mask |= (1 << LAST_LO_REGNUM);
8928 if (current_function_pretend_args_size == 0 || TARGET_BACKTRACE)
8930 if (had_to_push_lr
8931 && ! is_called_in_ARM_mode (current_function_decl)
8932 && ! eh_ofs)
8933 live_regs_mask |= 1 << PC_REGNUM;
8935 /* Either no argument registers were pushed or a backtrace
8936 structure was created which includes an adjusted stack
8937 pointer, so just pop everything. */
8938 if (live_regs_mask)
8939 thumb_pushpop (asm_out_file, live_regs_mask, FALSE);
8941 if (eh_ofs)
8942 thumb_exit (asm_out_file, 2, eh_ofs);
8943 /* We have either just popped the return address into the
8944 PC or it is was kept in LR for the entire function or
8945 it is still on the stack because we do not want to
8946 return by doing a pop {pc}. */
8947 else if ((live_regs_mask & (1 << PC_REGNUM)) == 0)
8948 thumb_exit (asm_out_file,
8949 (had_to_push_lr
8950 && is_called_in_ARM_mode (current_function_decl)) ?
8951 -1 : LR_REGNUM, NULL_RTX);
8953 else
8955 /* Pop everything but the return address. */
8956 live_regs_mask &= ~ (1 << PC_REGNUM);
8958 if (live_regs_mask)
8959 thumb_pushpop (asm_out_file, live_regs_mask, FALSE);
8961 if (had_to_push_lr)
8962 /* Get the return address into a temporary register. */
8963 thumb_pushpop (asm_out_file, 1 << LAST_ARG_REGNUM, 0);
8965 /* Remove the argument registers that were pushed onto the stack. */
8966 asm_fprintf (asm_out_file, "\tadd\t%r, %r, #%d\n",
8967 SP_REGNUM, SP_REGNUM,
8968 current_function_pretend_args_size);
8970 if (eh_ofs)
8971 thumb_exit (asm_out_file, 2, eh_ofs);
8972 else
8973 thumb_exit (asm_out_file,
8974 had_to_push_lr ? LAST_ARG_REGNUM : LR_REGNUM, NULL_RTX);
8977 return "";
8980 /* Functions to save and restore machine-specific function data. */
8982 static void
8983 arm_mark_machine_status (p)
8984 struct function * p;
8986 struct machine_function *machine = p->machine;
8988 ggc_mark_rtx (machine->ra_rtx);
8989 ggc_mark_rtx (machine->eh_epilogue_sp_ofs);
8992 static void
8993 arm_init_machine_status (p)
8994 struct function * p;
8996 p->machine =
8997 (struct machine_function *) xcalloc (1, sizeof (struct machine_function));
9000 /* Return an RTX indicating where the return address to the
9001 calling function can be found. */
9003 arm_return_addr (count, frame)
9004 int count;
9005 rtx frame ATTRIBUTE_UNUSED;
9007 rtx reg;
9009 if (count != 0)
9010 return NULL_RTX;
9012 reg = cfun->machine->ra_rtx;
9014 if (reg == NULL)
9016 rtx init;
9018 /* No rtx yet. Invent one, and initialize it for r14 (lr) in
9019 the prologue. */
9020 reg = gen_reg_rtx (Pmode);
9021 cfun->machine->ra_rtx = reg;
9023 if (! TARGET_APCS_32)
9024 init = gen_rtx_AND (Pmode, gen_rtx_REG (Pmode, LR_REGNUM),
9025 GEN_INT (RETURN_ADDR_MASK26));
9026 else
9027 init = gen_rtx_REG (Pmode, LR_REGNUM);
9029 init = gen_rtx_SET (VOIDmode, reg, init);
9031 /* Emit the insn to the prologue with the other argument copies. */
9032 push_topmost_sequence ();
9033 emit_insn_after (init, get_insns ());
9034 pop_topmost_sequence ();
9037 return reg;
9040 /* Do anything needed before RTL is emitted for each function. */
9041 void
9042 arm_init_expanders ()
9044 /* Arrange to initialize and mark the machine per-function status. */
9045 init_machine_status = arm_init_machine_status;
9046 mark_machine_status = arm_mark_machine_status;
9049 /* Generate the rest of a function's prologue. */
9050 void
9051 thumb_expand_prologue ()
9053 HOST_WIDE_INT amount = (get_frame_size ()
9054 + current_function_outgoing_args_size);
9056 /* Naked functions don't have prologues. */
9057 if (arm_naked_function_p (current_function_decl))
9058 return;
9060 if (frame_pointer_needed)
9061 emit_insn (gen_movsi (hard_frame_pointer_rtx, stack_pointer_rtx));
9063 if (amount)
9065 amount = ROUND_UP (amount);
9067 if (amount < 512)
9068 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
9069 GEN_INT (- amount)));
9070 else
9072 int regno;
9073 rtx reg;
9075 /* The stack decrement is too big for an immediate value in a single
9076 insn. In theory we could issue multiple subtracts, but after
9077 three of them it becomes more space efficient to place the full
9078 value in the constant pool and load into a register. (Also the
9079 ARM debugger really likes to see only one stack decrement per
9080 function). So instead we look for a scratch register into which
9081 we can load the decrement, and then we subtract this from the
9082 stack pointer. Unfortunately on the thumb the only available
9083 scratch registers are the argument registers, and we cannot use
9084 these as they may hold arguments to the function. Instead we
9085 attempt to locate a call preserved register which is used by this
9086 function. If we can find one, then we know that it will have
9087 been pushed at the start of the prologue and so we can corrupt
9088 it now. */
9089 for (regno = LAST_ARG_REGNUM + 1; regno <= LAST_LO_REGNUM; regno++)
9090 if (regs_ever_live[regno]
9091 && ! call_used_regs[regno] /* Paranoia */
9092 && ! (TARGET_SINGLE_PIC_BASE && (regno == arm_pic_register))
9093 && ! (frame_pointer_needed
9094 && (regno == THUMB_HARD_FRAME_POINTER_REGNUM)))
9095 break;
9097 if (regno > LAST_LO_REGNUM) /* Very unlikely */
9099 rtx spare = gen_rtx (REG, SImode, IP_REGNUM);
9101 /* Choose an arbitary, non-argument low register. */
9102 reg = gen_rtx (REG, SImode, LAST_LO_REGNUM);
9104 /* Save it by copying it into a high, scratch register. */
9105 emit_insn (gen_movsi (spare, reg));
9107 /* Decrement the stack. */
9108 emit_insn (gen_movsi (reg, GEN_INT (- amount)));
9109 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
9110 reg));
9112 /* Restore the low register's original value. */
9113 emit_insn (gen_movsi (reg, spare));
9115 /* Emit a USE of the restored scratch register, so that flow
9116 analysis will not consider the restore redundant. The
9117 register won't be used again in this function and isn't
9118 restored by the epilogue. */
9119 emit_insn (gen_rtx_USE (VOIDmode, reg));
9121 else
9123 reg = gen_rtx (REG, SImode, regno);
9125 emit_insn (gen_movsi (reg, GEN_INT (- amount)));
9126 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
9127 reg));
9132 if (profile_flag || profile_block_flag || TARGET_NO_SCHED_PRO)
9133 emit_insn (gen_blockage ());
9136 void
9137 thumb_expand_epilogue ()
9139 HOST_WIDE_INT amount = (get_frame_size ()
9140 + current_function_outgoing_args_size);
9142 /* Naked functions don't have epilogues. */
9143 if (arm_naked_function_p (current_function_decl))
9144 return;
9146 if (frame_pointer_needed)
9147 emit_insn (gen_movsi (stack_pointer_rtx, hard_frame_pointer_rtx));
9148 else if (amount)
9150 amount = ROUND_UP (amount);
9152 if (amount < 512)
9153 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx,
9154 GEN_INT (amount)));
9155 else
9157 /* r3 is always free in the epilogue. */
9158 rtx reg = gen_rtx (REG, SImode, LAST_ARG_REGNUM);
9160 emit_insn (gen_movsi (reg, GEN_INT (amount)));
9161 emit_insn (gen_addsi3 (stack_pointer_rtx, stack_pointer_rtx, reg));
9165 /* Emit a USE (stack_pointer_rtx), so that
9166 the stack adjustment will not be deleted. */
9167 emit_insn (gen_rtx_USE (VOIDmode, stack_pointer_rtx));
9169 if (profile_flag || profile_block_flag || TARGET_NO_SCHED_PRO)
9170 emit_insn (gen_blockage ());
9173 void
9174 output_thumb_prologue (f)
9175 FILE * f;
9177 int live_regs_mask = 0;
9178 int high_regs_pushed = 0;
9179 int store_arg_regs = 0;
9180 int regno;
9182 if (arm_naked_function_p (current_function_decl))
9183 return;
9185 if (is_called_in_ARM_mode (current_function_decl))
9187 const char * name;
9189 if (GET_CODE (DECL_RTL (current_function_decl)) != MEM)
9190 abort ();
9191 if (GET_CODE (XEXP (DECL_RTL (current_function_decl), 0)) != SYMBOL_REF)
9192 abort ();
9193 name = XSTR (XEXP (DECL_RTL (current_function_decl), 0), 0);
9195 /* Generate code sequence to switch us into Thumb mode. */
9196 /* The .code 32 directive has already been emitted by
9197 ASM_DECLARE_FUNCITON_NAME */
9198 asm_fprintf (f, "\torr\t%r, %r, #1\n", IP_REGNUM, PC_REGNUM);
9199 asm_fprintf (f, "\tbx\t%r\n", IP_REGNUM);
9201 /* Generate a label, so that the debugger will notice the
9202 change in instruction sets. This label is also used by
9203 the assembler to bypass the ARM code when this function
9204 is called from a Thumb encoded function elsewhere in the
9205 same file. Hence the definition of STUB_NAME here must
9206 agree with the definition in gas/config/tc-arm.c */
9208 #define STUB_NAME ".real_start_of"
9210 asm_fprintf (f, "\t.code\t16\n");
9211 #ifdef ARM_PE
9212 if (arm_dllexport_name_p (name))
9213 name = ARM_STRIP_NAME_ENCODING (name);
9214 #endif
9215 asm_fprintf (f, "\t.globl %s%U%s\n", STUB_NAME, name);
9216 asm_fprintf (f, "\t.thumb_func\n");
9217 asm_fprintf (f, "%s%U%s:\n", STUB_NAME, name);
9220 if (current_function_anonymous_args && current_function_pretend_args_size)
9221 store_arg_regs = 1;
9223 if (current_function_pretend_args_size)
9225 if (store_arg_regs)
9227 int num_pushes;
9229 asm_fprintf (f, "\tpush\t{");
9231 num_pushes = NUM_INTS (current_function_pretend_args_size);
9233 for (regno = LAST_ARG_REGNUM + 1 - num_pushes;
9234 regno <= LAST_ARG_REGNUM;
9235 regno ++)
9236 asm_fprintf (f, "%r%s", regno,
9237 regno == LAST_ARG_REGNUM ? "" : ", ");
9239 asm_fprintf (f, "}\n");
9241 else
9242 asm_fprintf (f, "\tsub\t%r, %r, #%d\n",
9243 SP_REGNUM, SP_REGNUM,
9244 current_function_pretend_args_size);
9247 for (regno = 0; regno <= LAST_LO_REGNUM; regno ++)
9248 if (regs_ever_live[regno] && ! call_used_regs[regno]
9249 && ! (TARGET_SINGLE_PIC_BASE && (regno == arm_pic_register)))
9250 live_regs_mask |= 1 << regno;
9252 if (live_regs_mask || ! leaf_function_p () || thumb_far_jump_used_p (1))
9253 live_regs_mask |= 1 << LR_REGNUM;
9255 if (TARGET_BACKTRACE)
9257 int offset;
9258 int work_register = 0;
9259 int wr;
9261 /* We have been asked to create a stack backtrace structure.
9262 The code looks like this:
9264 0 .align 2
9265 0 func:
9266 0 sub SP, #16 Reserve space for 4 registers.
9267 2 push {R7} Get a work register.
9268 4 add R7, SP, #20 Get the stack pointer before the push.
9269 6 str R7, [SP, #8] Store the stack pointer (before reserving the space).
9270 8 mov R7, PC Get hold of the start of this code plus 12.
9271 10 str R7, [SP, #16] Store it.
9272 12 mov R7, FP Get hold of the current frame pointer.
9273 14 str R7, [SP, #4] Store it.
9274 16 mov R7, LR Get hold of the current return address.
9275 18 str R7, [SP, #12] Store it.
9276 20 add R7, SP, #16 Point at the start of the backtrace structure.
9277 22 mov FP, R7 Put this value into the frame pointer. */
9279 if ((live_regs_mask & 0xFF) == 0)
9281 /* See if the a4 register is free. */
9283 if (regs_ever_live [LAST_ARG_REGNUM] == 0)
9284 work_register = LAST_ARG_REGNUM;
9285 else /* We must push a register of our own */
9286 live_regs_mask |= (1 << LAST_LO_REGNUM);
9289 if (work_register == 0)
9291 /* Select a register from the list that will be pushed to
9292 use as our work register. */
9293 for (work_register = (LAST_LO_REGNUM + 1); work_register--;)
9294 if ((1 << work_register) & live_regs_mask)
9295 break;
9298 asm_fprintf
9299 (f, "\tsub\t%r, %r, #16\t%@ Create stack backtrace structure\n",
9300 SP_REGNUM, SP_REGNUM);
9302 if (live_regs_mask)
9303 thumb_pushpop (f, live_regs_mask, 1);
9305 for (offset = 0, wr = 1 << 15; wr != 0; wr >>= 1)
9306 if (wr & live_regs_mask)
9307 offset += 4;
9309 asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
9310 offset + 16 + current_function_pretend_args_size);
9312 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
9313 offset + 4);
9315 /* Make sure that the instruction fetching the PC is in the right place
9316 to calculate "start of backtrace creation code + 12". */
9317 if (live_regs_mask)
9319 asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
9320 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
9321 offset + 12);
9322 asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
9323 ARM_HARD_FRAME_POINTER_REGNUM);
9324 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
9325 offset);
9327 else
9329 asm_fprintf (f, "\tmov\t%r, %r\n", work_register,
9330 ARM_HARD_FRAME_POINTER_REGNUM);
9331 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
9332 offset);
9333 asm_fprintf (f, "\tmov\t%r, %r\n", work_register, PC_REGNUM);
9334 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
9335 offset + 12);
9338 asm_fprintf (f, "\tmov\t%r, %r\n", work_register, LR_REGNUM);
9339 asm_fprintf (f, "\tstr\t%r, [%r, #%d]\n", work_register, SP_REGNUM,
9340 offset + 8);
9341 asm_fprintf (f, "\tadd\t%r, %r, #%d\n", work_register, SP_REGNUM,
9342 offset + 12);
9343 asm_fprintf (f, "\tmov\t%r, %r\t\t%@ Backtrace structure created\n",
9344 ARM_HARD_FRAME_POINTER_REGNUM, work_register);
9346 else if (live_regs_mask)
9347 thumb_pushpop (f, live_regs_mask, 1);
9349 for (regno = 8; regno < 13; regno++)
9351 if (regs_ever_live[regno] && ! call_used_regs[regno]
9352 && ! (TARGET_SINGLE_PIC_BASE && (regno == arm_pic_register)))
9353 high_regs_pushed ++;
9356 if (high_regs_pushed)
9358 int pushable_regs = 0;
9359 int mask = live_regs_mask & 0xff;
9360 int next_hi_reg;
9362 for (next_hi_reg = 12; next_hi_reg > LAST_LO_REGNUM; next_hi_reg--)
9364 if (regs_ever_live[next_hi_reg] && ! call_used_regs[next_hi_reg]
9365 && ! (TARGET_SINGLE_PIC_BASE
9366 && (next_hi_reg == arm_pic_register)))
9367 break;
9370 pushable_regs = mask;
9372 if (pushable_regs == 0)
9374 /* Desperation time -- this probably will never happen. */
9375 if (regs_ever_live[LAST_ARG_REGNUM]
9376 || ! call_used_regs[LAST_ARG_REGNUM])
9377 asm_fprintf (f, "\tmov\t%r, %r\n", IP_REGNUM, LAST_ARG_REGNUM);
9378 mask = 1 << LAST_ARG_REGNUM;
9381 while (high_regs_pushed > 0)
9383 for (regno = LAST_LO_REGNUM; regno >= 0; regno--)
9385 if (mask & (1 << regno))
9387 asm_fprintf (f, "\tmov\t%r, %r\n", regno, next_hi_reg);
9389 high_regs_pushed --;
9391 if (high_regs_pushed)
9392 for (next_hi_reg--; next_hi_reg > LAST_LO_REGNUM;
9393 next_hi_reg--)
9395 if (regs_ever_live[next_hi_reg]
9396 && ! call_used_regs[next_hi_reg]
9397 && ! (TARGET_SINGLE_PIC_BASE
9398 && (next_hi_reg == arm_pic_register)))
9399 break;
9401 else
9403 mask &= ~ ((1 << regno) - 1);
9404 break;
9409 thumb_pushpop (f, mask, 1);
9412 if (pushable_regs == 0
9413 && (regs_ever_live[LAST_ARG_REGNUM]
9414 || ! call_used_regs[LAST_ARG_REGNUM]))
9415 asm_fprintf (f, "\tmov\t%r, %r\n", LAST_ARG_REGNUM, IP_REGNUM);
9419 /* Handle the case of a double word load into a low register from
9420 a computed memory address. The computed address may involve a
9421 register which is overwritten by the load. */
9423 char *
9424 thumb_load_double_from_address (operands)
9425 rtx * operands;
9427 rtx addr;
9428 rtx base;
9429 rtx offset;
9430 rtx arg1;
9431 rtx arg2;
9433 if (GET_CODE (operands[0]) != REG)
9434 fatal ("thumb_load_double_from_address: destination is not a register");
9436 if (GET_CODE (operands[1]) != MEM)
9438 debug_rtx (operands[1]);
9439 fatal ("thumb_load_double_from_address: source is not a computed memory address");
9442 /* Get the memory address. */
9443 addr = XEXP (operands[1], 0);
9445 /* Work out how the memory address is computed. */
9446 switch (GET_CODE (addr))
9448 case REG:
9449 operands[2] = gen_rtx (MEM, SImode,
9450 plus_constant (XEXP (operands[1], 0), 4));
9452 if (REGNO (operands[0]) == REGNO (addr))
9454 output_asm_insn ("ldr\t%H0, %2", operands);
9455 output_asm_insn ("ldr\t%0, %1", operands);
9457 else
9459 output_asm_insn ("ldr\t%0, %1", operands);
9460 output_asm_insn ("ldr\t%H0, %2", operands);
9462 break;
9464 case CONST:
9465 /* Compute <address> + 4 for the high order load. */
9466 operands[2] = gen_rtx (MEM, SImode,
9467 plus_constant (XEXP (operands[1], 0), 4));
9469 output_asm_insn ("ldr\t%0, %1", operands);
9470 output_asm_insn ("ldr\t%H0, %2", operands);
9471 break;
9473 case PLUS:
9474 arg1 = XEXP (addr, 0);
9475 arg2 = XEXP (addr, 1);
9477 if (CONSTANT_P (arg1))
9478 base = arg2, offset = arg1;
9479 else
9480 base = arg1, offset = arg2;
9482 if (GET_CODE (base) != REG)
9483 fatal ("thumb_load_double_from_address: base is not a register");
9485 /* Catch the case of <address> = <reg> + <reg> */
9486 if (GET_CODE (offset) == REG)
9488 int reg_offset = REGNO (offset);
9489 int reg_base = REGNO (base);
9490 int reg_dest = REGNO (operands[0]);
9492 /* Add the base and offset registers together into the
9493 higher destination register. */
9494 asm_fprintf (asm_out_file, "\tadd\t%r, %r, %r",
9495 reg_dest + 1, reg_base, reg_offset);
9497 /* Load the lower destination register from the address in
9498 the higher destination register. */
9499 asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #0]",
9500 reg_dest, reg_dest + 1);
9502 /* Load the higher destination register from its own address
9503 plus 4. */
9504 asm_fprintf (asm_out_file, "\tldr\t%r, [%r, #4]",
9505 reg_dest + 1, reg_dest + 1);
9507 else
9509 /* Compute <address> + 4 for the high order load. */
9510 operands[2] = gen_rtx (MEM, SImode,
9511 plus_constant (XEXP (operands[1], 0), 4));
9513 /* If the computed address is held in the low order register
9514 then load the high order register first, otherwise always
9515 load the low order register first. */
9516 if (REGNO (operands[0]) == REGNO (base))
9518 output_asm_insn ("ldr\t%H0, %2", operands);
9519 output_asm_insn ("ldr\t%0, %1", operands);
9521 else
9523 output_asm_insn ("ldr\t%0, %1", operands);
9524 output_asm_insn ("ldr\t%H0, %2", operands);
9527 break;
9529 case LABEL_REF:
9530 /* With no registers to worry about we can just load the value
9531 directly. */
9532 operands[2] = gen_rtx (MEM, SImode,
9533 plus_constant (XEXP (operands[1], 0), 4));
9535 output_asm_insn ("ldr\t%H0, %2", operands);
9536 output_asm_insn ("ldr\t%0, %1", operands);
9537 break;
9539 default:
9540 debug_rtx (operands[1]);
9541 fatal ("thumb_load_double_from_address: Unhandled address calculation");
9542 break;
9545 return "";
9549 char *
9550 thumb_output_move_mem_multiple (n, operands)
9551 int n;
9552 rtx * operands;
9554 rtx tmp;
9556 switch (n)
9558 case 2:
9559 if (REGNO (operands[2]) > REGNO (operands[3]))
9561 tmp = operands[2];
9562 operands[2] = operands[3];
9563 operands[3] = tmp;
9565 output_asm_insn ("ldmia\t%1!, {%2, %3}", operands);
9566 output_asm_insn ("stmia\t%0!, {%2, %3}", operands);
9567 break;
9569 case 3:
9570 if (REGNO (operands[2]) > REGNO (operands[3]))
9572 tmp = operands[2];
9573 operands[2] = operands[3];
9574 operands[3] = tmp;
9576 if (REGNO (operands[3]) > REGNO (operands[4]))
9578 tmp = operands[3];
9579 operands[3] = operands[4];
9580 operands[4] = tmp;
9582 if (REGNO (operands[2]) > REGNO (operands[3]))
9584 tmp = operands[2];
9585 operands[2] = operands[3];
9586 operands[3] = tmp;
9589 output_asm_insn ("ldmia\t%1!, {%2, %3, %4}", operands);
9590 output_asm_insn ("stmia\t%0!, {%2, %3, %4}", operands);
9591 break;
9593 default:
9594 abort ();
9597 return "";
9600 /* Routines for generating rtl */
9602 void
9603 thumb_expand_movstrqi (operands)
9604 rtx * operands;
9606 rtx out = copy_to_mode_reg (SImode, XEXP (operands[0], 0));
9607 rtx in = copy_to_mode_reg (SImode, XEXP (operands[1], 0));
9608 HOST_WIDE_INT len = INTVAL (operands[2]);
9609 HOST_WIDE_INT offset = 0;
9611 while (len >= 12)
9613 emit_insn (gen_movmem12b (out, in));
9614 len -= 12;
9617 if (len >= 8)
9619 emit_insn (gen_movmem8b (out, in));
9620 len -= 8;
9623 if (len >= 4)
9625 rtx reg = gen_reg_rtx (SImode);
9626 emit_insn (gen_movsi (reg, gen_rtx (MEM, SImode, in)));
9627 emit_insn (gen_movsi (gen_rtx (MEM, SImode, out), reg));
9628 len -= 4;
9629 offset += 4;
9632 if (len >= 2)
9634 rtx reg = gen_reg_rtx (HImode);
9635 emit_insn (gen_movhi (reg, gen_rtx (MEM, HImode,
9636 plus_constant (in, offset))));
9637 emit_insn (gen_movhi (gen_rtx (MEM, HImode, plus_constant (out, offset)),
9638 reg));
9639 len -= 2;
9640 offset += 2;
9643 if (len)
9645 rtx reg = gen_reg_rtx (QImode);
9646 emit_insn (gen_movqi (reg, gen_rtx (MEM, QImode,
9647 plus_constant (in, offset))));
9648 emit_insn (gen_movqi (gen_rtx (MEM, QImode, plus_constant (out, offset)),
9649 reg));
9654 thumb_cmp_operand (op, mode)
9655 rtx op;
9656 enum machine_mode mode;
9658 return ((GET_CODE (op) == CONST_INT
9659 && (unsigned HOST_WIDE_INT) (INTVAL (op)) < 256)
9660 || register_operand (op, mode));
9663 static char *
9664 thumb_condition_code (x, invert)
9665 rtx x;
9666 int invert;
9668 static char * conds[] =
9670 "eq", "ne", "cs", "cc", "mi", "pl", "vs", "vc",
9671 "hi", "ls", "ge", "lt", "gt", "le"
9673 int val;
9675 switch (GET_CODE (x))
9677 case EQ: val = 0; break;
9678 case NE: val = 1; break;
9679 case GEU: val = 2; break;
9680 case LTU: val = 3; break;
9681 case GTU: val = 8; break;
9682 case LEU: val = 9; break;
9683 case GE: val = 10; break;
9684 case LT: val = 11; break;
9685 case GT: val = 12; break;
9686 case LE: val = 13; break;
9687 default:
9688 abort ();
9691 return conds[val ^ invert];
9694 /* Handle storing a half-word to memory during reload. */
9695 void
9696 thumb_reload_out_hi (operands)
9697 rtx * operands;
9699 emit_insn (gen_thumb_movhi_clobber (operands[0], operands[1], operands[2]));
9702 /* Handle storing a half-word to memory during reload. */
9703 void
9704 thumb_reload_in_hi (operands)
9705 rtx * operands ATTRIBUTE_UNUSED;
9707 abort ();
9710 /* Return the length of a function name prefix
9711 that starts with the character 'c'. */
9712 static int
9713 arm_get_strip_length (char c)
9715 switch (c)
9717 ARM_NAME_ENCODING_LENGTHS
9718 default: return 0;
9722 /* Return a pointer to a function's name with any
9723 and all prefix encodings stripped from it. */
9724 const char *
9725 arm_strip_name_encoding (const char * name)
9727 int skip;
9729 while ((skip = arm_get_strip_length (* name)))
9730 name += skip;
9732 return name;
9735 #ifdef AOF_ASSEMBLER
9736 /* Special functions only needed when producing AOF syntax assembler. */
9738 rtx aof_pic_label = NULL_RTX;
9739 struct pic_chain
9741 struct pic_chain * next;
9742 char * symname;
9745 static struct pic_chain * aof_pic_chain = NULL;
9748 aof_pic_entry (x)
9749 rtx x;
9751 struct pic_chain ** chainp;
9752 int offset;
9754 if (aof_pic_label == NULL_RTX)
9756 /* We mark this here and not in arm_add_gc_roots() to avoid
9757 polluting even more code with ifdefs, and because it never
9758 contains anything useful until we assign to it here. */
9759 ggc_add_rtx_root (& aof_pic_label, 1);
9760 /* This needs to persist throughout the compilation. */
9761 end_temporary_allocation ();
9762 aof_pic_label = gen_rtx_SYMBOL_REF (Pmode, "x$adcons");
9763 resume_temporary_allocation ();
9766 for (offset = 0, chainp = &aof_pic_chain; *chainp;
9767 offset += 4, chainp = &(*chainp)->next)
9768 if ((*chainp)->symname == XSTR (x, 0))
9769 return plus_constant (aof_pic_label, offset);
9771 *chainp = (struct pic_chain *) xmalloc (sizeof (struct pic_chain));
9772 (*chainp)->next = NULL;
9773 (*chainp)->symname = XSTR (x, 0);
9774 return plus_constant (aof_pic_label, offset);
9777 void
9778 aof_dump_pic_table (f)
9779 FILE * f;
9781 struct pic_chain * chain;
9783 if (aof_pic_chain == NULL)
9784 return;
9786 asm_fprintf (f, "\tAREA |%r$$adcons|, BASED %r\n",
9787 PIC_OFFSET_TABLE_REGNUM,
9788 PIC_OFFSET_TABLE_REGNUM);
9789 fputs ("|x$adcons|\n", f);
9791 for (chain = aof_pic_chain; chain; chain = chain->next)
9793 fputs ("\tDCD\t", f);
9794 assemble_name (f, chain->symname);
9795 fputs ("\n", f);
9799 int arm_text_section_count = 1;
9801 char *
9802 aof_text_section ()
9804 static char buf[100];
9805 sprintf (buf, "\tAREA |C$$code%d|, CODE, READONLY",
9806 arm_text_section_count++);
9807 if (flag_pic)
9808 strcat (buf, ", PIC, REENTRANT");
9809 return buf;
9812 static int arm_data_section_count = 1;
9814 char *
9815 aof_data_section ()
9817 static char buf[100];
9818 sprintf (buf, "\tAREA |C$$data%d|, DATA", arm_data_section_count++);
9819 return buf;
9822 /* The AOF assembler is religiously strict about declarations of
9823 imported and exported symbols, so that it is impossible to declare
9824 a function as imported near the beginning of the file, and then to
9825 export it later on. It is, however, possible to delay the decision
9826 until all the functions in the file have been compiled. To get
9827 around this, we maintain a list of the imports and exports, and
9828 delete from it any that are subsequently defined. At the end of
9829 compilation we spit the remainder of the list out before the END
9830 directive. */
9832 struct import
9834 struct import * next;
9835 char * name;
9838 static struct import * imports_list = NULL;
9840 void
9841 aof_add_import (name)
9842 char * name;
9844 struct import * new;
9846 for (new = imports_list; new; new = new->next)
9847 if (new->name == name)
9848 return;
9850 new = (struct import *) xmalloc (sizeof (struct import));
9851 new->next = imports_list;
9852 imports_list = new;
9853 new->name = name;
9856 void
9857 aof_delete_import (name)
9858 char * name;
9860 struct import ** old;
9862 for (old = &imports_list; *old; old = & (*old)->next)
9864 if ((*old)->name == name)
9866 *old = (*old)->next;
9867 return;
9872 int arm_main_function = 0;
9874 void
9875 aof_dump_imports (f)
9876 FILE * f;
9878 /* The AOF assembler needs this to cause the startup code to be extracted
9879 from the library. Brining in __main causes the whole thing to work
9880 automagically. */
9881 if (arm_main_function)
9883 text_section ();
9884 fputs ("\tIMPORT __main\n", f);
9885 fputs ("\tDCD __main\n", f);
9888 /* Now dump the remaining imports. */
9889 while (imports_list)
9891 fprintf (f, "\tIMPORT\t");
9892 assemble_name (f, imports_list->name);
9893 fputc ('\n', f);
9894 imports_list = imports_list->next;
9897 #endif /* AOF_ASSEMBLER */