[ARC] Fix errors in arc_ifcvt.
[gcc.git] / gcc / config / arc / arc.c
1 /* Subroutines used for code generation on the Synopsys DesignWare ARC cpu.
2 Copyright (C) 1994-2017 Free Software Foundation, Inc.
3
4 Sources derived from work done by Sankhya Technologies (www.sankhya.com) on
5 behalf of Synopsys Inc.
6
7 Position Independent Code support added,Code cleaned up,
8 Comments and Support For ARC700 instructions added by
9 Saurabh Verma (saurabh.verma@codito.com)
10 Ramana Radhakrishnan(ramana.radhakrishnan@codito.com)
11
12 Fixing ABI inconsistencies, optimizations for ARC600 / ARC700 pipelines,
13 profiling support added by Joern Rennecke <joern.rennecke@embecosm.com>
14
15 This file is part of GCC.
16
17 GCC is free software; you can redistribute it and/or modify
18 it under the terms of the GNU General Public License as published by
19 the Free Software Foundation; either version 3, or (at your option)
20 any later version.
21
22 GCC is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 GNU General Public License for more details.
26
27 You should have received a copy of the GNU General Public License
28 along with GCC; see the file COPYING3. If not see
29 <http://www.gnu.org/licenses/>. */
30
31 #include "config.h"
32 #include "system.h"
33 #include "coretypes.h"
34 #include "memmodel.h"
35 #include "backend.h"
36 #include "target.h"
37 #include "rtl.h"
38 #include "tree.h"
39 #include "cfghooks.h"
40 #include "df.h"
41 #include "tm_p.h"
42 #include "stringpool.h"
43 #include "attribs.h"
44 #include "optabs.h"
45 #include "regs.h"
46 #include "emit-rtl.h"
47 #include "recog.h"
48 #include "diagnostic.h"
49 #include "fold-const.h"
50 #include "varasm.h"
51 #include "stor-layout.h"
52 #include "calls.h"
53 #include "output.h"
54 #include "insn-attr.h"
55 #include "flags.h"
56 #include "explow.h"
57 #include "expr.h"
58 #include "langhooks.h"
59 #include "tm-constrs.h"
60 #include "reload.h" /* For operands_match_p */
61 #include "cfgrtl.h"
62 #include "tree-pass.h"
63 #include "context.h"
64 #include "builtins.h"
65 #include "rtl-iter.h"
66 #include "alias.h"
67 #include "opts.h"
68
69 /* Which cpu we're compiling for (ARC600, ARC601, ARC700). */
70 static char arc_cpu_name[10] = "";
71 static const char *arc_cpu_string = arc_cpu_name;
72
73 /* ??? Loads can handle any constant, stores can only handle small ones. */
74 /* OTOH, LIMMs cost extra, so their usefulness is limited. */
75 #define RTX_OK_FOR_OFFSET_P(MODE, X) \
76 (GET_CODE (X) == CONST_INT \
77 && SMALL_INT_RANGE (INTVAL (X), (GET_MODE_SIZE (MODE) - 1) & -4, \
78 (INTVAL (X) & (GET_MODE_SIZE (MODE) - 1) & 3 \
79 ? 0 \
80 : -(-GET_MODE_SIZE (MODE) | -4) >> 1)))
81
82 #define LEGITIMATE_SMALL_DATA_OFFSET_P(X) \
83 (GET_CODE (X) == CONST \
84 && GET_CODE (XEXP ((X), 0)) == PLUS \
85 && GET_CODE (XEXP (XEXP ((X), 0), 0)) == SYMBOL_REF \
86 && SYMBOL_REF_SMALL_P (XEXP (XEXP ((X), 0), 0)) \
87 && GET_CODE (XEXP(XEXP ((X), 0), 1)) == CONST_INT \
88 && INTVAL (XEXP (XEXP ((X), 0), 1)) <= g_switch_value)
89
90 #define LEGITIMATE_SMALL_DATA_ADDRESS_P(X) \
91 (GET_CODE (X) == PLUS \
92 && REG_P (XEXP ((X), 0)) \
93 && REGNO (XEXP ((X), 0)) == SDATA_BASE_REGNUM \
94 && ((GET_CODE (XEXP ((X), 1)) == SYMBOL_REF \
95 && SYMBOL_REF_SMALL_P (XEXP ((X), 1))) \
96 || LEGITIMATE_SMALL_DATA_OFFSET_P (XEXP ((X), 1))))
97
98 /* Array of valid operand punctuation characters. */
99 char arc_punct_chars[256];
100
101 /* State used by arc_ccfsm_advance to implement conditional execution. */
102 struct GTY (()) arc_ccfsm
103 {
104 int state;
105 int cc;
106 rtx cond;
107 rtx_insn *target_insn;
108 int target_label;
109 };
110
111 /* Status of the IRQ_CTRL_AUX register. */
112 typedef struct irq_ctrl_saved_t
113 {
114 /* Last register number used by IRQ_CTRL_SAVED aux_reg. */
115 short irq_save_last_reg;
116 /* True if BLINK is automatically saved. */
117 bool irq_save_blink;
118 /* True if LPCOUNT is automatically saved. */
119 bool irq_save_lpcount;
120 } irq_ctrl_saved_t;
121 static irq_ctrl_saved_t irq_ctrl_saved;
122
123 #define ARC_AUTOBLINK_IRQ_P(FNTYPE) \
124 ((ARC_INTERRUPT_P (FNTYPE) \
125 && irq_ctrl_saved.irq_save_blink) \
126 || (ARC_FAST_INTERRUPT_P (FNTYPE) \
127 && rgf_banked_register_count > 8))
128
129 #define ARC_AUTOFP_IRQ_P(FNTYPE) \
130 ((ARC_INTERRUPT_P (FNTYPE) \
131 && (irq_ctrl_saved.irq_save_last_reg > 26)) \
132 || (ARC_FAST_INTERRUPT_P (FNTYPE) \
133 && rgf_banked_register_count > 8))
134
135 #define ARC_AUTO_IRQ_P(FNTYPE) \
136 (ARC_INTERRUPT_P (FNTYPE) && !ARC_FAST_INTERRUPT_P (FNTYPE) \
137 && (irq_ctrl_saved.irq_save_blink \
138 || (irq_ctrl_saved.irq_save_last_reg >= 0)))
139
140 /* Number of registers in second bank for FIRQ support. */
141 static int rgf_banked_register_count;
142
143 #define arc_ccfsm_current cfun->machine->ccfsm_current
144
145 #define ARC_CCFSM_BRANCH_DELETED_P(STATE) \
146 ((STATE)->state == 1 || (STATE)->state == 2)
147
148 /* Indicate we're conditionalizing insns now. */
149 #define ARC_CCFSM_RECORD_BRANCH_DELETED(STATE) \
150 ((STATE)->state += 2)
151
152 #define ARC_CCFSM_COND_EXEC_P(STATE) \
153 ((STATE)->state == 3 || (STATE)->state == 4 || (STATE)->state == 5 \
154 || current_insn_predicate)
155
156 /* Check if INSN has a 16 bit opcode considering struct arc_ccfsm *STATE. */
157 #define CCFSM_ISCOMPACT(INSN,STATE) \
158 (ARC_CCFSM_COND_EXEC_P (STATE) \
159 ? (get_attr_iscompact (INSN) == ISCOMPACT_TRUE \
160 || get_attr_iscompact (INSN) == ISCOMPACT_TRUE_LIMM) \
161 : get_attr_iscompact (INSN) != ISCOMPACT_FALSE)
162
163 /* Likewise, but also consider that INSN might be in a delay slot of JUMP. */
164 #define CCFSM_DBR_ISCOMPACT(INSN,JUMP,STATE) \
165 ((ARC_CCFSM_COND_EXEC_P (STATE) \
166 || (JUMP_P (JUMP) \
167 && INSN_ANNULLED_BRANCH_P (JUMP) \
168 && (TARGET_AT_DBR_CONDEXEC || INSN_FROM_TARGET_P (INSN)))) \
169 ? (get_attr_iscompact (INSN) == ISCOMPACT_TRUE \
170 || get_attr_iscompact (INSN) == ISCOMPACT_TRUE_LIMM) \
171 : get_attr_iscompact (INSN) != ISCOMPACT_FALSE)
172
173 /* The maximum number of insns skipped which will be conditionalised if
174 possible. */
175 /* When optimizing for speed:
176 Let p be the probability that the potentially skipped insns need to
177 be executed, pn the cost of a correctly predicted non-taken branch,
178 mt the cost of a mis/non-predicted taken branch,
179 mn mispredicted non-taken, pt correctly predicted taken ;
180 costs expressed in numbers of instructions like the ones considered
181 skipping.
182 Unfortunately we don't have a measure of predictability - this
183 is linked to probability only in that in the no-eviction-scenario
184 there is a lower bound 1 - 2 * min (p, 1-p), and a somewhat larger
185 value that can be assumed *if* the distribution is perfectly random.
186 A predictability of 1 is perfectly plausible not matter what p is,
187 because the decision could be dependent on an invocation parameter
188 of the program.
189 For large p, we want MAX_INSNS_SKIPPED == pn/(1-p) + mt - pn
190 For small p, we want MAX_INSNS_SKIPPED == pt
191
192 When optimizing for size:
193 We want to skip insn unless we could use 16 opcodes for the
194 non-conditionalized insn to balance the branch length or more.
195 Performance can be tie-breaker. */
196 /* If the potentially-skipped insns are likely to be executed, we'll
197 generally save one non-taken branch
198 o
199 this to be no less than the 1/p */
200 #define MAX_INSNS_SKIPPED 3
201
202 /* A nop is needed between a 4 byte insn that sets the condition codes and
203 a branch that uses them (the same isn't true for an 8 byte insn that sets
204 the condition codes). Set by arc_ccfsm_advance. Used by
205 arc_print_operand. */
206
207 static int get_arc_condition_code (rtx);
208
209 static tree arc_handle_interrupt_attribute (tree *, tree, tree, int, bool *);
210 static tree arc_handle_fndecl_attribute (tree *, tree, tree, int, bool *);
211
212 /* Initialized arc_attribute_table to NULL since arc doesnot have any
213 machine specific supported attributes. */
214 const struct attribute_spec arc_attribute_table[] =
215 {
216 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
217 affects_type_identity } */
218 { "interrupt", 1, 1, true, false, false, arc_handle_interrupt_attribute, true },
219 /* Function calls made to this symbol must be done indirectly, because
220 it may lie outside of the 21/25 bit addressing range of a normal function
221 call. */
222 { "long_call", 0, 0, false, true, true, NULL, false },
223 /* Whereas these functions are always known to reside within the 25 bit
224 addressing range of unconditionalized bl. */
225 { "medium_call", 0, 0, false, true, true, NULL, false },
226 /* And these functions are always known to reside within the 21 bit
227 addressing range of blcc. */
228 { "short_call", 0, 0, false, true, true, NULL, false },
229 /* Function which are not having the prologue and epilogue generated
230 by the compiler. */
231 { "naked", 0, 0, true, false, false, arc_handle_fndecl_attribute, false },
232 { NULL, 0, 0, false, false, false, NULL, false }
233 };
234 static int arc_comp_type_attributes (const_tree, const_tree);
235 static void arc_file_start (void);
236 static void arc_internal_label (FILE *, const char *, unsigned long);
237 static void arc_output_mi_thunk (FILE *, tree, HOST_WIDE_INT, HOST_WIDE_INT,
238 tree);
239 static int arc_address_cost (rtx, machine_mode, addr_space_t, bool);
240 static void arc_encode_section_info (tree decl, rtx rtl, int first);
241
242 static void arc_init_builtins (void);
243 static rtx arc_expand_builtin (tree, rtx, rtx, machine_mode, int);
244
245 static int branch_dest (rtx);
246
247 static void arc_output_pic_addr_const (FILE *, rtx, int);
248 static bool arc_function_ok_for_sibcall (tree, tree);
249 static rtx arc_function_value (const_tree, const_tree, bool);
250 const char * output_shift (rtx *);
251 static void arc_reorg (void);
252 static bool arc_in_small_data_p (const_tree);
253
254 static void arc_init_reg_tables (void);
255 static bool arc_return_in_memory (const_tree, const_tree);
256 static bool arc_vector_mode_supported_p (machine_mode);
257
258 static bool arc_can_use_doloop_p (const widest_int &, const widest_int &,
259 unsigned int, bool);
260 static const char *arc_invalid_within_doloop (const rtx_insn *);
261
262 static void output_short_suffix (FILE *file);
263
264 static bool arc_frame_pointer_required (void);
265
266 static bool arc_use_by_pieces_infrastructure_p (unsigned HOST_WIDE_INT,
267 unsigned int,
268 enum by_pieces_operation op,
269 bool);
270
271 /* Globally visible information about currently selected cpu. */
272 const arc_cpu_t *arc_selected_cpu;
273
274 static bool
275 legitimate_scaled_address_p (machine_mode mode, rtx op, bool strict)
276 {
277 if (GET_CODE (op) != PLUS)
278 return false;
279
280 if (GET_CODE (XEXP (op, 0)) != MULT)
281 return false;
282
283 /* Check multiplication operands. */
284 if (!RTX_OK_FOR_INDEX_P (XEXP (XEXP (op, 0), 0), strict))
285 return false;
286
287 if (!CONST_INT_P (XEXP (XEXP (op, 0), 1)))
288 return false;
289
290 switch (GET_MODE_SIZE (mode))
291 {
292 case 2:
293 if (INTVAL (XEXP (XEXP (op, 0), 1)) != 2)
294 return false;
295 break;
296 case 8:
297 if (!TARGET_LL64)
298 return false;
299 /* Fall through. */
300 case 4:
301 if (INTVAL (XEXP (XEXP (op, 0), 1)) != 4)
302 return false;
303 default:
304 return false;
305 }
306
307 /* Check the base. */
308 if (RTX_OK_FOR_BASE_P (XEXP (op, 1), (strict)))
309 return true;
310
311 if (flag_pic)
312 {
313 if (CONST_INT_P (XEXP (op, 1)))
314 return true;
315 return false;
316 }
317 if (CONSTANT_P (XEXP (op, 1)))
318 {
319 /* Scalled addresses for sdata is done other places. */
320 if (GET_CODE (XEXP (op, 1)) == SYMBOL_REF
321 && SYMBOL_REF_SMALL_P (XEXP (op, 1)))
322 return false;
323 return true;
324 }
325
326 return false;
327 }
328
329 /* Check for constructions like REG + OFFS, where OFFS can be a
330 register, an immediate or an long immediate. */
331
332 static bool
333 legitimate_offset_address_p (machine_mode mode, rtx x, bool index, bool strict)
334 {
335 if (GET_CODE (x) != PLUS)
336 return false;
337
338 if (!RTX_OK_FOR_BASE_P (XEXP (x, 0), (strict)))
339 return false;
340
341 /* Check for: [Rx + small offset] or [Rx + Ry]. */
342 if (((index && RTX_OK_FOR_INDEX_P (XEXP (x, 1), (strict))
343 && GET_MODE_SIZE ((mode)) <= 4)
344 || RTX_OK_FOR_OFFSET_P (mode, XEXP (x, 1))))
345 return true;
346
347 /* Check for [Rx + symbol]. */
348 if (!flag_pic
349 && (GET_CODE (XEXP (x, 1)) == SYMBOL_REF)
350 /* Avoid this type of address for double or larger modes. */
351 && (GET_MODE_SIZE (mode) <= 4)
352 /* Avoid small data which ends in something like GP +
353 symb@sda. */
354 && (!SYMBOL_REF_SMALL_P (XEXP (x, 1))))
355 return true;
356
357 return false;
358 }
359
360 /* Implements target hook vector_mode_supported_p. */
361
362 static bool
363 arc_vector_mode_supported_p (machine_mode mode)
364 {
365 switch (mode)
366 {
367 case E_V2HImode:
368 return TARGET_PLUS_DMPY;
369 case E_V4HImode:
370 case E_V2SImode:
371 return TARGET_PLUS_QMACW;
372 case E_V4SImode:
373 case E_V8HImode:
374 return TARGET_SIMD_SET;
375
376 default:
377 return false;
378 }
379 }
380
381 /* Implements target hook TARGET_VECTORIZE_PREFERRED_SIMD_MODE. */
382
383 static machine_mode
384 arc_preferred_simd_mode (scalar_mode mode)
385 {
386 switch (mode)
387 {
388 case E_HImode:
389 return TARGET_PLUS_QMACW ? V4HImode : V2HImode;
390 case E_SImode:
391 return V2SImode;
392
393 default:
394 return word_mode;
395 }
396 }
397
398 /* Implements target hook
399 TARGET_VECTORIZE_AUTOVECTORIZE_VECTOR_SIZES. */
400
401 static unsigned int
402 arc_autovectorize_vector_sizes (void)
403 {
404 return TARGET_PLUS_QMACW ? (8 | 4) : 0;
405 }
406
407 /* TARGET_PRESERVE_RELOAD_P is still awaiting patch re-evaluation / review. */
408 static bool arc_preserve_reload_p (rtx in) ATTRIBUTE_UNUSED;
409 static rtx arc_delegitimize_address (rtx);
410 static bool arc_can_follow_jump (const rtx_insn *follower,
411 const rtx_insn *followee);
412
413 static rtx frame_insn (rtx);
414 static void arc_function_arg_advance (cumulative_args_t, machine_mode,
415 const_tree, bool);
416 static rtx arc_legitimize_address_0 (rtx, rtx, machine_mode mode);
417
418 static void arc_finalize_pic (void);
419
420 /* initialize the GCC target structure. */
421 #undef TARGET_COMP_TYPE_ATTRIBUTES
422 #define TARGET_COMP_TYPE_ATTRIBUTES arc_comp_type_attributes
423 #undef TARGET_ASM_FILE_START
424 #define TARGET_ASM_FILE_START arc_file_start
425 #undef TARGET_ATTRIBUTE_TABLE
426 #define TARGET_ATTRIBUTE_TABLE arc_attribute_table
427 #undef TARGET_ASM_INTERNAL_LABEL
428 #define TARGET_ASM_INTERNAL_LABEL arc_internal_label
429 #undef TARGET_RTX_COSTS
430 #define TARGET_RTX_COSTS arc_rtx_costs
431 #undef TARGET_ADDRESS_COST
432 #define TARGET_ADDRESS_COST arc_address_cost
433
434 #undef TARGET_ENCODE_SECTION_INFO
435 #define TARGET_ENCODE_SECTION_INFO arc_encode_section_info
436
437 #undef TARGET_CANNOT_FORCE_CONST_MEM
438 #define TARGET_CANNOT_FORCE_CONST_MEM arc_cannot_force_const_mem
439
440 #undef TARGET_INIT_BUILTINS
441 #define TARGET_INIT_BUILTINS arc_init_builtins
442
443 #undef TARGET_EXPAND_BUILTIN
444 #define TARGET_EXPAND_BUILTIN arc_expand_builtin
445
446 #undef TARGET_BUILTIN_DECL
447 #define TARGET_BUILTIN_DECL arc_builtin_decl
448
449 #undef TARGET_ASM_OUTPUT_MI_THUNK
450 #define TARGET_ASM_OUTPUT_MI_THUNK arc_output_mi_thunk
451
452 #undef TARGET_ASM_CAN_OUTPUT_MI_THUNK
453 #define TARGET_ASM_CAN_OUTPUT_MI_THUNK hook_bool_const_tree_hwi_hwi_const_tree_true
454
455 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
456 #define TARGET_FUNCTION_OK_FOR_SIBCALL arc_function_ok_for_sibcall
457
458 #undef TARGET_MACHINE_DEPENDENT_REORG
459 #define TARGET_MACHINE_DEPENDENT_REORG arc_reorg
460
461 #undef TARGET_IN_SMALL_DATA_P
462 #define TARGET_IN_SMALL_DATA_P arc_in_small_data_p
463
464 #undef TARGET_PROMOTE_FUNCTION_MODE
465 #define TARGET_PROMOTE_FUNCTION_MODE \
466 default_promote_function_mode_always_promote
467
468 #undef TARGET_PROMOTE_PROTOTYPES
469 #define TARGET_PROMOTE_PROTOTYPES hook_bool_const_tree_true
470
471 #undef TARGET_RETURN_IN_MEMORY
472 #define TARGET_RETURN_IN_MEMORY arc_return_in_memory
473 #undef TARGET_PASS_BY_REFERENCE
474 #define TARGET_PASS_BY_REFERENCE arc_pass_by_reference
475
476 #undef TARGET_SETUP_INCOMING_VARARGS
477 #define TARGET_SETUP_INCOMING_VARARGS arc_setup_incoming_varargs
478
479 #undef TARGET_ARG_PARTIAL_BYTES
480 #define TARGET_ARG_PARTIAL_BYTES arc_arg_partial_bytes
481
482 #undef TARGET_MUST_PASS_IN_STACK
483 #define TARGET_MUST_PASS_IN_STACK must_pass_in_stack_var_size
484
485 #undef TARGET_FUNCTION_VALUE
486 #define TARGET_FUNCTION_VALUE arc_function_value
487
488 #undef TARGET_SCHED_ADJUST_PRIORITY
489 #define TARGET_SCHED_ADJUST_PRIORITY arc_sched_adjust_priority
490
491 #undef TARGET_VECTOR_MODE_SUPPORTED_P
492 #define TARGET_VECTOR_MODE_SUPPORTED_P arc_vector_mode_supported_p
493
494 #undef TARGET_VECTORIZE_PREFERRED_SIMD_MODE
495 #define TARGET_VECTORIZE_PREFERRED_SIMD_MODE arc_preferred_simd_mode
496
497 #undef TARGET_VECTORIZE_AUTOVECTORIZE_VECTOR_SIZES
498 #define TARGET_VECTORIZE_AUTOVECTORIZE_VECTOR_SIZES arc_autovectorize_vector_sizes
499
500 #undef TARGET_CAN_USE_DOLOOP_P
501 #define TARGET_CAN_USE_DOLOOP_P arc_can_use_doloop_p
502
503 #undef TARGET_INVALID_WITHIN_DOLOOP
504 #define TARGET_INVALID_WITHIN_DOLOOP arc_invalid_within_doloop
505
506 #undef TARGET_PRESERVE_RELOAD_P
507 #define TARGET_PRESERVE_RELOAD_P arc_preserve_reload_p
508
509 #undef TARGET_CAN_FOLLOW_JUMP
510 #define TARGET_CAN_FOLLOW_JUMP arc_can_follow_jump
511
512 #undef TARGET_DELEGITIMIZE_ADDRESS
513 #define TARGET_DELEGITIMIZE_ADDRESS arc_delegitimize_address
514
515 #undef TARGET_USE_BY_PIECES_INFRASTRUCTURE_P
516 #define TARGET_USE_BY_PIECES_INFRASTRUCTURE_P \
517 arc_use_by_pieces_infrastructure_p
518
519 /* Usually, we will be able to scale anchor offsets.
520 When this fails, we want LEGITIMIZE_ADDRESS to kick in. */
521 #undef TARGET_MIN_ANCHOR_OFFSET
522 #define TARGET_MIN_ANCHOR_OFFSET (-1024)
523 #undef TARGET_MAX_ANCHOR_OFFSET
524 #define TARGET_MAX_ANCHOR_OFFSET (1020)
525
526 #undef TARGET_SECONDARY_RELOAD
527 #define TARGET_SECONDARY_RELOAD arc_secondary_reload
528
529 #define TARGET_OPTION_OVERRIDE arc_override_options
530
531 #define TARGET_CONDITIONAL_REGISTER_USAGE arc_conditional_register_usage
532
533 #define TARGET_TRAMPOLINE_INIT arc_initialize_trampoline
534
535 #define TARGET_TRAMPOLINE_ADJUST_ADDRESS arc_trampoline_adjust_address
536
537 #define TARGET_CAN_ELIMINATE arc_can_eliminate
538
539 #define TARGET_FRAME_POINTER_REQUIRED arc_frame_pointer_required
540
541 #define TARGET_FUNCTION_ARG arc_function_arg
542
543 #define TARGET_FUNCTION_ARG_ADVANCE arc_function_arg_advance
544
545 #define TARGET_LEGITIMATE_CONSTANT_P arc_legitimate_constant_p
546
547 #define TARGET_LEGITIMATE_ADDRESS_P arc_legitimate_address_p
548
549 #define TARGET_MODE_DEPENDENT_ADDRESS_P arc_mode_dependent_address_p
550
551 #define TARGET_LEGITIMIZE_ADDRESS arc_legitimize_address
552
553 #define TARGET_ADJUST_INSN_LENGTH arc_adjust_insn_length
554
555 #define TARGET_INSN_LENGTH_PARAMETERS arc_insn_length_parameters
556
557 #undef TARGET_NO_SPECULATION_IN_DELAY_SLOTS_P
558 #define TARGET_NO_SPECULATION_IN_DELAY_SLOTS_P \
559 arc_no_speculation_in_delay_slots_p
560
561 #undef TARGET_LRA_P
562 #define TARGET_LRA_P arc_lra_p
563 #define TARGET_REGISTER_PRIORITY arc_register_priority
564 /* Stores with scaled offsets have different displacement ranges. */
565 #define TARGET_DIFFERENT_ADDR_DISPLACEMENT_P hook_bool_void_true
566 #define TARGET_SPILL_CLASS arc_spill_class
567
568 #undef TARGET_ALLOCATE_STACK_SLOTS_FOR_ARGS
569 #define TARGET_ALLOCATE_STACK_SLOTS_FOR_ARGS arc_allocate_stack_slots_for_args
570
571 #undef TARGET_WARN_FUNC_RETURN
572 #define TARGET_WARN_FUNC_RETURN arc_warn_func_return
573
574 #include "target-def.h"
575
576 #undef TARGET_ASM_ALIGNED_HI_OP
577 #define TARGET_ASM_ALIGNED_HI_OP "\t.hword\t"
578 #undef TARGET_ASM_ALIGNED_SI_OP
579 #define TARGET_ASM_ALIGNED_SI_OP "\t.word\t"
580
581 #ifdef HAVE_AS_TLS
582 #undef TARGET_HAVE_TLS
583 #define TARGET_HAVE_TLS HAVE_AS_TLS
584 #endif
585
586 #undef TARGET_DWARF_REGISTER_SPAN
587 #define TARGET_DWARF_REGISTER_SPAN arc_dwarf_register_span
588
589 /* Try to keep the (mov:DF _, reg) as early as possible so
590 that the d<add/sub/mul>h-lr insns appear together and can
591 use the peephole2 pattern. */
592
593 static int
594 arc_sched_adjust_priority (rtx_insn *insn, int priority)
595 {
596 rtx set = single_set (insn);
597 if (set
598 && GET_MODE (SET_SRC(set)) == DFmode
599 && GET_CODE (SET_SRC(set)) == REG)
600 {
601 /* Incrementing priority by 20 (empirically derived). */
602 return priority + 20;
603 }
604
605 return priority;
606 }
607
608 /* For ARC base register + offset addressing, the validity of the
609 address is mode-dependent for most of the offset range, as the
610 offset can be scaled by the access size.
611 We don't expose these as mode-dependent addresses in the
612 mode_dependent_address_p target hook, because that would disable
613 lots of optimizations, and most uses of these addresses are for 32
614 or 64 bit accesses anyways, which are fine.
615 However, that leaves some addresses for 8 / 16 bit values not
616 properly reloaded by the generic code, which is why we have to
617 schedule secondary reloads for these. */
618
619 static reg_class_t
620 arc_secondary_reload (bool in_p,
621 rtx x,
622 reg_class_t cl,
623 machine_mode mode,
624 secondary_reload_info *sri)
625 {
626 enum rtx_code code = GET_CODE (x);
627
628 if (cl == DOUBLE_REGS)
629 return GENERAL_REGS;
630
631 /* The loop counter register can be stored, but not loaded directly. */
632 if ((cl == LPCOUNT_REG || cl == WRITABLE_CORE_REGS)
633 && in_p && MEM_P (x))
634 return GENERAL_REGS;
635
636 /* If we have a subreg (reg), where reg is a pseudo (that will end in
637 a memory location), then we may need a scratch register to handle
638 the fp/sp+largeoffset address. */
639 if (code == SUBREG)
640 {
641 rtx addr = NULL_RTX;
642 x = SUBREG_REG (x);
643
644 if (REG_P (x))
645 {
646 int regno = REGNO (x);
647 if (regno >= FIRST_PSEUDO_REGISTER)
648 regno = reg_renumber[regno];
649
650 if (regno != -1)
651 return NO_REGS;
652
653 /* It is a pseudo that ends in a stack location. */
654 if (reg_equiv_mem (REGNO (x)))
655 {
656 /* Get the equivalent address and check the range of the
657 offset. */
658 rtx mem = reg_equiv_mem (REGNO (x));
659 addr = find_replacement (&XEXP (mem, 0));
660 }
661 }
662 else
663 {
664 gcc_assert (MEM_P (x));
665 addr = XEXP (x, 0);
666 addr = simplify_rtx (addr);
667 }
668 if (addr && GET_CODE (addr) == PLUS
669 && CONST_INT_P (XEXP (addr, 1))
670 && (!RTX_OK_FOR_OFFSET_P (mode, XEXP (addr, 1))))
671 {
672 switch (mode)
673 {
674 case E_QImode:
675 sri->icode =
676 in_p ? CODE_FOR_reload_qi_load : CODE_FOR_reload_qi_store;
677 break;
678 case E_HImode:
679 sri->icode =
680 in_p ? CODE_FOR_reload_hi_load : CODE_FOR_reload_hi_store;
681 break;
682 default:
683 break;
684 }
685 }
686 }
687 return NO_REGS;
688 }
689
690 /* Convert reloads using offsets that are too large to use indirect
691 addressing. */
692
693 void
694 arc_secondary_reload_conv (rtx reg, rtx mem, rtx scratch, bool store_p)
695 {
696 rtx addr;
697
698 gcc_assert (GET_CODE (mem) == MEM);
699 addr = XEXP (mem, 0);
700
701 /* Large offset: use a move. FIXME: ld ops accepts limms as
702 offsets. Hence, the following move insn is not required. */
703 emit_move_insn (scratch, addr);
704 mem = replace_equiv_address_nv (mem, scratch);
705
706 /* Now create the move. */
707 if (store_p)
708 emit_insn (gen_rtx_SET (mem, reg));
709 else
710 emit_insn (gen_rtx_SET (reg, mem));
711
712 return;
713 }
714
715 static unsigned arc_ifcvt (void);
716
717 namespace {
718
719 const pass_data pass_data_arc_ifcvt =
720 {
721 RTL_PASS,
722 "arc_ifcvt", /* name */
723 OPTGROUP_NONE, /* optinfo_flags */
724 TV_IFCVT2, /* tv_id */
725 0, /* properties_required */
726 0, /* properties_provided */
727 0, /* properties_destroyed */
728 0, /* todo_flags_start */
729 TODO_df_finish /* todo_flags_finish */
730 };
731
732 class pass_arc_ifcvt : public rtl_opt_pass
733 {
734 public:
735 pass_arc_ifcvt(gcc::context *ctxt)
736 : rtl_opt_pass(pass_data_arc_ifcvt, ctxt)
737 {}
738
739 /* opt_pass methods: */
740 opt_pass * clone () { return new pass_arc_ifcvt (m_ctxt); }
741 virtual unsigned int execute (function *) { return arc_ifcvt (); }
742 };
743
744 } // anon namespace
745
746 rtl_opt_pass *
747 make_pass_arc_ifcvt (gcc::context *ctxt)
748 {
749 return new pass_arc_ifcvt (ctxt);
750 }
751
752 static unsigned arc_predicate_delay_insns (void);
753
754 namespace {
755
756 const pass_data pass_data_arc_predicate_delay_insns =
757 {
758 RTL_PASS,
759 "arc_predicate_delay_insns", /* name */
760 OPTGROUP_NONE, /* optinfo_flags */
761 TV_IFCVT2, /* tv_id */
762 0, /* properties_required */
763 0, /* properties_provided */
764 0, /* properties_destroyed */
765 0, /* todo_flags_start */
766 TODO_df_finish /* todo_flags_finish */
767 };
768
769 class pass_arc_predicate_delay_insns : public rtl_opt_pass
770 {
771 public:
772 pass_arc_predicate_delay_insns(gcc::context *ctxt)
773 : rtl_opt_pass(pass_data_arc_predicate_delay_insns, ctxt)
774 {}
775
776 /* opt_pass methods: */
777 virtual unsigned int execute (function *)
778 {
779 return arc_predicate_delay_insns ();
780 }
781 };
782
783 } // anon namespace
784
785 rtl_opt_pass *
786 make_pass_arc_predicate_delay_insns (gcc::context *ctxt)
787 {
788 return new pass_arc_predicate_delay_insns (ctxt);
789 }
790
791 /* Called by OVERRIDE_OPTIONS to initialize various things. */
792
793 static void
794 arc_init (void)
795 {
796 if (TARGET_V2)
797 {
798 /* I have the multiplier, then use it*/
799 if (TARGET_MPYW || TARGET_MULTI)
800 arc_multcost = COSTS_N_INSNS (1);
801 }
802 /* Note: arc_multcost is only used in rtx_cost if speed is true. */
803 if (arc_multcost < 0)
804 switch (arc_tune)
805 {
806 case TUNE_ARC700_4_2_STD:
807 /* latency 7;
808 max throughput (1 multiply + 4 other insns) / 5 cycles. */
809 arc_multcost = COSTS_N_INSNS (4);
810 if (TARGET_NOMPY_SET)
811 arc_multcost = COSTS_N_INSNS (30);
812 break;
813 case TUNE_ARC700_4_2_XMAC:
814 /* latency 5;
815 max throughput (1 multiply + 2 other insns) / 3 cycles. */
816 arc_multcost = COSTS_N_INSNS (3);
817 if (TARGET_NOMPY_SET)
818 arc_multcost = COSTS_N_INSNS (30);
819 break;
820 case TUNE_ARC600:
821 if (TARGET_MUL64_SET)
822 {
823 arc_multcost = COSTS_N_INSNS (4);
824 break;
825 }
826 /* Fall through. */
827 default:
828 arc_multcost = COSTS_N_INSNS (30);
829 break;
830 }
831
832 /* MPY instructions valid only for ARC700 or ARCv2. */
833 if (TARGET_NOMPY_SET && TARGET_ARC600_FAMILY)
834 error ("-mno-mpy supported only for ARC700 or ARCv2");
835
836 if (!TARGET_DPFP && TARGET_DPFP_DISABLE_LRSR)
837 error ("-mno-dpfp-lrsr supported only with -mdpfp");
838
839 /* FPX-1. No fast and compact together. */
840 if ((TARGET_DPFP_FAST_SET && TARGET_DPFP_COMPACT_SET)
841 || (TARGET_SPFP_FAST_SET && TARGET_SPFP_COMPACT_SET))
842 error ("FPX fast and compact options cannot be specified together");
843
844 /* FPX-2. No fast-spfp for arc600 or arc601. */
845 if (TARGET_SPFP_FAST_SET && TARGET_ARC600_FAMILY)
846 error ("-mspfp_fast not available on ARC600 or ARC601");
847
848 /* FPX-4. No FPX extensions mixed with FPU extensions. */
849 if ((TARGET_DPFP_FAST_SET || TARGET_DPFP_COMPACT_SET || TARGET_SPFP)
850 && TARGET_HARD_FLOAT)
851 error ("No FPX/FPU mixing allowed");
852
853 /* Warn for unimplemented PIC in pre-ARC700 cores, and disable flag_pic. */
854 if (flag_pic && TARGET_ARC600_FAMILY)
855 {
856 warning (DK_WARNING,
857 "PIC is not supported for %s. Generating non-PIC code only..",
858 arc_cpu_string);
859 flag_pic = 0;
860 }
861
862 arc_init_reg_tables ();
863
864 /* Initialize array for PRINT_OPERAND_PUNCT_VALID_P. */
865 memset (arc_punct_chars, 0, sizeof (arc_punct_chars));
866 arc_punct_chars['#'] = 1;
867 arc_punct_chars['*'] = 1;
868 arc_punct_chars['?'] = 1;
869 arc_punct_chars['!'] = 1;
870 arc_punct_chars['^'] = 1;
871 arc_punct_chars['&'] = 1;
872 arc_punct_chars['+'] = 1;
873 arc_punct_chars['_'] = 1;
874
875 if (optimize > 1 && !TARGET_NO_COND_EXEC)
876 {
877 /* There are two target-independent ifcvt passes, and arc_reorg may do
878 one or more arc_ifcvt calls. */
879 opt_pass *pass_arc_ifcvt_4 = make_pass_arc_ifcvt (g);
880 struct register_pass_info arc_ifcvt4_info
881 = { pass_arc_ifcvt_4, "dbr", 1, PASS_POS_INSERT_AFTER };
882 struct register_pass_info arc_ifcvt5_info
883 = { pass_arc_ifcvt_4->clone (), "shorten", 1, PASS_POS_INSERT_BEFORE };
884
885 register_pass (&arc_ifcvt4_info);
886 register_pass (&arc_ifcvt5_info);
887 }
888
889 if (flag_delayed_branch)
890 {
891 opt_pass *pass_arc_predicate_delay_insns
892 = make_pass_arc_predicate_delay_insns (g);
893 struct register_pass_info arc_predicate_delay_info
894 = { pass_arc_predicate_delay_insns, "dbr", 1, PASS_POS_INSERT_AFTER };
895
896 register_pass (&arc_predicate_delay_info);
897 }
898 }
899
900 /* Parse -mirq-ctrl-saved=RegisterRange, blink, lp_copunt. The
901 register range is specified as two registers separated by a dash.
902 It always starts with r0, and its upper limit is fp register.
903 blink and lp_count registers are optional. */
904
905 static void
906 irq_range (const char *cstr)
907 {
908 int i, first, last, blink, lpcount, xreg;
909 char *str, *dash, *comma;
910
911 i = strlen (cstr);
912 str = (char *) alloca (i + 1);
913 memcpy (str, cstr, i + 1);
914 blink = -1;
915 lpcount = -1;
916
917 dash = strchr (str, '-');
918 if (!dash)
919 {
920 warning (0, "value of -mirq-ctrl-saved must have form R0-REGx");
921 return;
922 }
923 *dash = '\0';
924
925 comma = strchr (dash + 1, ',');
926 if (comma)
927 *comma = '\0';
928
929 first = decode_reg_name (str);
930 if (first != 0)
931 {
932 warning (0, "first register must be R0");
933 return;
934 }
935
936 /* At this moment we do not have the register names initialized
937 accordingly. */
938 if (!strcmp (dash + 1, "ilink"))
939 last = 29;
940 else
941 last = decode_reg_name (dash + 1);
942
943 if (last < 0)
944 {
945 warning (0, "unknown register name: %s", dash + 1);
946 return;
947 }
948
949 if (!(last & 0x01))
950 {
951 warning (0, "last register name %s must be an odd register", dash + 1);
952 return;
953 }
954
955 *dash = '-';
956
957 if (first > last)
958 {
959 warning (0, "%s-%s is an empty range", str, dash + 1);
960 return;
961 }
962
963 while (comma)
964 {
965 *comma = ',';
966 str = comma + 1;
967
968 comma = strchr (str, ',');
969 if (comma)
970 *comma = '\0';
971
972 xreg = decode_reg_name (str);
973 switch (xreg)
974 {
975 case 31:
976 blink = 31;
977 break;
978
979 case 60:
980 lpcount = 60;
981 break;
982
983 default:
984 warning (0, "unknown register name: %s", str);
985 return;
986 }
987 }
988
989 irq_ctrl_saved.irq_save_last_reg = last;
990 irq_ctrl_saved.irq_save_blink = (blink == 31) || (last == 31);
991 irq_ctrl_saved.irq_save_lpcount = (lpcount == 60);
992 }
993
994 /* Parse -mrgf-banked-regs=NUM option string. Valid values for NUM are 4,
995 8, 16, or 32. */
996
997 static void
998 parse_mrgf_banked_regs_option (const char *arg)
999 {
1000 long int val;
1001 char *end_ptr;
1002
1003 errno = 0;
1004 val = strtol (arg, &end_ptr, 10);
1005 if (errno != 0 || *arg == '\0' || *end_ptr != '\0'
1006 || (val != 0 && val != 4 && val != 8 && val != 16 && val != 32))
1007 {
1008 error ("invalid number in -mrgf-banked-regs=%s "
1009 "valid values are 0, 4, 8, 16, or 32", arg);
1010 return;
1011 }
1012 rgf_banked_register_count = (int) val;
1013 }
1014
1015 /* Check ARC options, generate derived target attributes. */
1016
1017 static void
1018 arc_override_options (void)
1019 {
1020 unsigned int i;
1021 cl_deferred_option *opt;
1022 vec<cl_deferred_option> *vopt
1023 = (vec<cl_deferred_option> *) arc_deferred_options;
1024
1025 if (arc_cpu == PROCESSOR_NONE)
1026 arc_cpu = TARGET_CPU_DEFAULT;
1027
1028 /* Set the default cpu options. */
1029 arc_selected_cpu = &arc_cpu_types[(int) arc_cpu];
1030
1031 /* Set the architectures. */
1032 switch (arc_selected_cpu->arch_info->arch_id)
1033 {
1034 case BASE_ARCH_em:
1035 arc_cpu_string = "EM";
1036 break;
1037 case BASE_ARCH_hs:
1038 arc_cpu_string = "HS";
1039 break;
1040 case BASE_ARCH_700:
1041 if (arc_selected_cpu->processor == PROCESSOR_nps400)
1042 arc_cpu_string = "NPS400";
1043 else
1044 arc_cpu_string = "ARC700";
1045 break;
1046 case BASE_ARCH_6xx:
1047 arc_cpu_string = "ARC600";
1048 break;
1049 default:
1050 gcc_unreachable ();
1051 }
1052
1053 irq_ctrl_saved.irq_save_last_reg = -1;
1054 irq_ctrl_saved.irq_save_blink = false;
1055 irq_ctrl_saved.irq_save_lpcount = false;
1056
1057 rgf_banked_register_count = 0;
1058
1059 /* Handle the deferred options. */
1060 if (vopt)
1061 FOR_EACH_VEC_ELT (*vopt, i, opt)
1062 {
1063 switch (opt->opt_index)
1064 {
1065 case OPT_mirq_ctrl_saved_:
1066 if (TARGET_V2)
1067 irq_range (opt->arg);
1068 else
1069 warning (0, "option -mirq-ctrl-saved valid only for ARC v2 processors");
1070 break;
1071
1072 case OPT_mrgf_banked_regs_:
1073 if (TARGET_V2)
1074 parse_mrgf_banked_regs_option (opt->arg);
1075 else
1076 warning (0, "option -mrgf-banked-regs valid only for ARC v2 processors");
1077 break;
1078
1079 default:
1080 gcc_unreachable();
1081 }
1082 }
1083
1084 /* Set cpu flags accordingly to architecture/selected cpu. The cpu
1085 specific flags are set in arc-common.c. The architecture forces
1086 the default hardware configurations in, regardless what command
1087 line options are saying. The CPU optional hw options can be
1088 turned on or off. */
1089 #define ARC_OPT(NAME, CODE, MASK, DOC) \
1090 do { \
1091 if ((arc_selected_cpu->flags & CODE) \
1092 && ((target_flags_explicit & MASK) == 0)) \
1093 target_flags |= MASK; \
1094 if (arc_selected_cpu->arch_info->dflags & CODE) \
1095 target_flags |= MASK; \
1096 } while (0);
1097 #define ARC_OPTX(NAME, CODE, VAR, VAL, DOC) \
1098 do { \
1099 if ((arc_selected_cpu->flags & CODE) \
1100 && (VAR == DEFAULT_##VAR)) \
1101 VAR = VAL; \
1102 if (arc_selected_cpu->arch_info->dflags & CODE) \
1103 VAR = VAL; \
1104 } while (0);
1105
1106 #include "arc-options.def"
1107
1108 #undef ARC_OPTX
1109 #undef ARC_OPT
1110
1111 /* Check options against architecture options. Throw an error if
1112 option is not allowed. */
1113 #define ARC_OPTX(NAME, CODE, VAR, VAL, DOC) \
1114 do { \
1115 if ((VAR == VAL) \
1116 && (!(arc_selected_cpu->arch_info->flags & CODE))) \
1117 { \
1118 error ("%s is not available for %s architecture", \
1119 DOC, arc_selected_cpu->arch_info->name); \
1120 } \
1121 } while (0);
1122 #define ARC_OPT(NAME, CODE, MASK, DOC) \
1123 do { \
1124 if ((target_flags & MASK) \
1125 && (!(arc_selected_cpu->arch_info->flags & CODE))) \
1126 error ("%s is not available for %s architecture", \
1127 DOC, arc_selected_cpu->arch_info->name); \
1128 } while (0);
1129
1130 #include "arc-options.def"
1131
1132 #undef ARC_OPTX
1133 #undef ARC_OPT
1134
1135 /* Set Tune option. */
1136 if (arc_tune == TUNE_NONE)
1137 arc_tune = (enum attr_tune) arc_selected_cpu->tune;
1138
1139 if (arc_size_opt_level == 3)
1140 optimize_size = 1;
1141
1142 /* Compact casesi is not a valid option for ARCv2 family. */
1143 if (TARGET_V2)
1144 {
1145 if (TARGET_COMPACT_CASESI)
1146 {
1147 warning (0, "compact-casesi is not applicable to ARCv2");
1148 TARGET_COMPACT_CASESI = 0;
1149 }
1150 }
1151 else if (optimize_size == 1
1152 && !global_options_set.x_TARGET_COMPACT_CASESI)
1153 TARGET_COMPACT_CASESI = 1;
1154
1155 if (flag_pic)
1156 target_flags |= MASK_NO_SDATA_SET;
1157
1158 if (flag_no_common == 255)
1159 flag_no_common = !TARGET_NO_SDATA_SET;
1160
1161 /* TARGET_COMPACT_CASESI needs the "q" register class. */
1162 if (TARGET_MIXED_CODE)
1163 TARGET_Q_CLASS = 1;
1164 if (!TARGET_Q_CLASS)
1165 TARGET_COMPACT_CASESI = 0;
1166 if (TARGET_COMPACT_CASESI)
1167 TARGET_CASE_VECTOR_PC_RELATIVE = 1;
1168
1169 /* Check for small data option */
1170 if (!global_options_set.x_g_switch_value && !TARGET_NO_SDATA_SET)
1171 g_switch_value = TARGET_LL64 ? 8 : 4;
1172
1173 /* These need to be done at start up. It's convenient to do them here. */
1174 arc_init ();
1175 }
1176
1177 /* The condition codes of the ARC, and the inverse function. */
1178 /* For short branches, the "c" / "nc" names are not defined in the ARC
1179 Programmers manual, so we have to use "lo" / "hs"" instead. */
1180 static const char *arc_condition_codes[] =
1181 {
1182 "al", 0, "eq", "ne", "p", "n", "lo", "hs", "v", "nv",
1183 "gt", "le", "ge", "lt", "hi", "ls", "pnz", 0
1184 };
1185
1186 enum arc_cc_code_index
1187 {
1188 ARC_CC_AL, ARC_CC_EQ = ARC_CC_AL+2, ARC_CC_NE, ARC_CC_P, ARC_CC_N,
1189 ARC_CC_C, ARC_CC_NC, ARC_CC_V, ARC_CC_NV,
1190 ARC_CC_GT, ARC_CC_LE, ARC_CC_GE, ARC_CC_LT, ARC_CC_HI, ARC_CC_LS, ARC_CC_PNZ,
1191 ARC_CC_LO = ARC_CC_C, ARC_CC_HS = ARC_CC_NC
1192 };
1193
1194 #define ARC_INVERSE_CONDITION_CODE(X) ((X) ^ 1)
1195
1196 /* Returns the index of the ARC condition code string in
1197 `arc_condition_codes'. COMPARISON should be an rtx like
1198 `(eq (...) (...))'. */
1199
1200 static int
1201 get_arc_condition_code (rtx comparison)
1202 {
1203 switch (GET_MODE (XEXP (comparison, 0)))
1204 {
1205 case E_CCmode:
1206 case E_SImode: /* For BRcc. */
1207 switch (GET_CODE (comparison))
1208 {
1209 case EQ : return ARC_CC_EQ;
1210 case NE : return ARC_CC_NE;
1211 case GT : return ARC_CC_GT;
1212 case LE : return ARC_CC_LE;
1213 case GE : return ARC_CC_GE;
1214 case LT : return ARC_CC_LT;
1215 case GTU : return ARC_CC_HI;
1216 case LEU : return ARC_CC_LS;
1217 case LTU : return ARC_CC_LO;
1218 case GEU : return ARC_CC_HS;
1219 default : gcc_unreachable ();
1220 }
1221 case E_CC_ZNmode:
1222 switch (GET_CODE (comparison))
1223 {
1224 case EQ : return ARC_CC_EQ;
1225 case NE : return ARC_CC_NE;
1226 case GE: return ARC_CC_P;
1227 case LT: return ARC_CC_N;
1228 case GT : return ARC_CC_PNZ;
1229 default : gcc_unreachable ();
1230 }
1231 case E_CC_Zmode:
1232 switch (GET_CODE (comparison))
1233 {
1234 case EQ : return ARC_CC_EQ;
1235 case NE : return ARC_CC_NE;
1236 default : gcc_unreachable ();
1237 }
1238 case E_CC_Cmode:
1239 switch (GET_CODE (comparison))
1240 {
1241 case LTU : return ARC_CC_C;
1242 case GEU : return ARC_CC_NC;
1243 default : gcc_unreachable ();
1244 }
1245 case E_CC_FP_GTmode:
1246 if (TARGET_ARGONAUT_SET && TARGET_SPFP)
1247 switch (GET_CODE (comparison))
1248 {
1249 case GT : return ARC_CC_N;
1250 case UNLE: return ARC_CC_P;
1251 default : gcc_unreachable ();
1252 }
1253 else
1254 switch (GET_CODE (comparison))
1255 {
1256 case GT : return ARC_CC_HI;
1257 case UNLE : return ARC_CC_LS;
1258 default : gcc_unreachable ();
1259 }
1260 case E_CC_FP_GEmode:
1261 /* Same for FPX and non-FPX. */
1262 switch (GET_CODE (comparison))
1263 {
1264 case GE : return ARC_CC_HS;
1265 case UNLT : return ARC_CC_LO;
1266 default : gcc_unreachable ();
1267 }
1268 case E_CC_FP_UNEQmode:
1269 switch (GET_CODE (comparison))
1270 {
1271 case UNEQ : return ARC_CC_EQ;
1272 case LTGT : return ARC_CC_NE;
1273 default : gcc_unreachable ();
1274 }
1275 case E_CC_FP_ORDmode:
1276 switch (GET_CODE (comparison))
1277 {
1278 case UNORDERED : return ARC_CC_C;
1279 case ORDERED : return ARC_CC_NC;
1280 default : gcc_unreachable ();
1281 }
1282 case E_CC_FPXmode:
1283 switch (GET_CODE (comparison))
1284 {
1285 case EQ : return ARC_CC_EQ;
1286 case NE : return ARC_CC_NE;
1287 case UNORDERED : return ARC_CC_C;
1288 case ORDERED : return ARC_CC_NC;
1289 case LTGT : return ARC_CC_HI;
1290 case UNEQ : return ARC_CC_LS;
1291 default : gcc_unreachable ();
1292 }
1293 case E_CC_FPUmode:
1294 switch (GET_CODE (comparison))
1295 {
1296 case EQ : return ARC_CC_EQ;
1297 case NE : return ARC_CC_NE;
1298 case GT : return ARC_CC_GT;
1299 case GE : return ARC_CC_GE;
1300 case LT : return ARC_CC_C;
1301 case LE : return ARC_CC_LS;
1302 case UNORDERED : return ARC_CC_V;
1303 case ORDERED : return ARC_CC_NV;
1304 case UNGT : return ARC_CC_HI;
1305 case UNGE : return ARC_CC_HS;
1306 case UNLT : return ARC_CC_LT;
1307 case UNLE : return ARC_CC_LE;
1308 /* UNEQ and LTGT do not have representation. */
1309 case LTGT : /* Fall through. */
1310 case UNEQ : /* Fall through. */
1311 default : gcc_unreachable ();
1312 }
1313 case E_CC_FPU_UNEQmode:
1314 switch (GET_CODE (comparison))
1315 {
1316 case LTGT : return ARC_CC_NE;
1317 case UNEQ : return ARC_CC_EQ;
1318 default : gcc_unreachable ();
1319 }
1320 default : gcc_unreachable ();
1321 }
1322 /*NOTREACHED*/
1323 return (42);
1324 }
1325
1326 /* Return true if COMPARISON has a short form that can accomodate OFFSET. */
1327
1328 bool
1329 arc_short_comparison_p (rtx comparison, int offset)
1330 {
1331 gcc_assert (ARC_CC_NC == ARC_CC_HS);
1332 gcc_assert (ARC_CC_C == ARC_CC_LO);
1333 switch (get_arc_condition_code (comparison))
1334 {
1335 case ARC_CC_EQ: case ARC_CC_NE:
1336 return offset >= -512 && offset <= 506;
1337 case ARC_CC_GT: case ARC_CC_LE: case ARC_CC_GE: case ARC_CC_LT:
1338 case ARC_CC_HI: case ARC_CC_LS: case ARC_CC_LO: case ARC_CC_HS:
1339 return offset >= -64 && offset <= 58;
1340 default:
1341 return false;
1342 }
1343 }
1344
1345 /* Given a comparison code (EQ, NE, etc.) and the first operand of a COMPARE,
1346 return the mode to be used for the comparison. */
1347
1348 machine_mode
1349 arc_select_cc_mode (enum rtx_code op, rtx x, rtx y)
1350 {
1351 machine_mode mode = GET_MODE (x);
1352 rtx x1;
1353
1354 /* For an operation that sets the condition codes as a side-effect, the
1355 C and V flags is not set as for cmp, so we can only use comparisons where
1356 this doesn't matter. (For LT and GE we can use "mi" and "pl"
1357 instead.) */
1358 /* ??? We could use "pnz" for greater than zero, however, we could then
1359 get into trouble because the comparison could not be reversed. */
1360 if (GET_MODE_CLASS (mode) == MODE_INT
1361 && y == const0_rtx
1362 && (op == EQ || op == NE
1363 || ((op == LT || op == GE) && GET_MODE_SIZE (GET_MODE (x)) <= 4)))
1364 return CC_ZNmode;
1365
1366 /* add.f for if (a+b) */
1367 if (mode == SImode
1368 && GET_CODE (y) == NEG
1369 && (op == EQ || op == NE))
1370 return CC_ZNmode;
1371
1372 /* Check if this is a test suitable for bxor.f . */
1373 if (mode == SImode && (op == EQ || op == NE) && CONST_INT_P (y)
1374 && ((INTVAL (y) - 1) & INTVAL (y)) == 0
1375 && INTVAL (y))
1376 return CC_Zmode;
1377
1378 /* Check if this is a test suitable for add / bmsk.f . */
1379 if (mode == SImode && (op == EQ || op == NE) && CONST_INT_P (y)
1380 && GET_CODE (x) == AND && CONST_INT_P ((x1 = XEXP (x, 1)))
1381 && ((INTVAL (x1) + 1) & INTVAL (x1)) == 0
1382 && (~INTVAL (x1) | INTVAL (y)) < 0
1383 && (~INTVAL (x1) | INTVAL (y)) > -0x800)
1384 return CC_Zmode;
1385
1386 if (GET_MODE (x) == SImode && (op == LTU || op == GEU)
1387 && GET_CODE (x) == PLUS
1388 && (rtx_equal_p (XEXP (x, 0), y) || rtx_equal_p (XEXP (x, 1), y)))
1389 return CC_Cmode;
1390
1391 if (TARGET_ARGONAUT_SET
1392 && ((mode == SFmode && TARGET_SPFP) || (mode == DFmode && TARGET_DPFP)))
1393 switch (op)
1394 {
1395 case EQ: case NE: case UNEQ: case LTGT: case ORDERED: case UNORDERED:
1396 return CC_FPXmode;
1397 case LT: case UNGE: case GT: case UNLE:
1398 return CC_FP_GTmode;
1399 case LE: case UNGT: case GE: case UNLT:
1400 return CC_FP_GEmode;
1401 default: gcc_unreachable ();
1402 }
1403 else if (TARGET_HARD_FLOAT
1404 && ((mode == SFmode && TARGET_FP_SP_BASE)
1405 || (mode == DFmode && TARGET_FP_DP_BASE)))
1406 switch (op)
1407 {
1408 case EQ:
1409 case NE:
1410 case UNORDERED:
1411 case ORDERED:
1412 case UNLT:
1413 case UNLE:
1414 case UNGT:
1415 case UNGE:
1416 case LT:
1417 case LE:
1418 case GT:
1419 case GE:
1420 return CC_FPUmode;
1421
1422 case LTGT:
1423 case UNEQ:
1424 return CC_FPU_UNEQmode;
1425
1426 default:
1427 gcc_unreachable ();
1428 }
1429 else if (GET_MODE_CLASS (mode) == MODE_FLOAT && TARGET_OPTFPE)
1430 {
1431 switch (op)
1432 {
1433 case EQ: case NE: return CC_Zmode;
1434 case LT: case UNGE:
1435 case GT: case UNLE: return CC_FP_GTmode;
1436 case LE: case UNGT:
1437 case GE: case UNLT: return CC_FP_GEmode;
1438 case UNEQ: case LTGT: return CC_FP_UNEQmode;
1439 case ORDERED: case UNORDERED: return CC_FP_ORDmode;
1440 default: gcc_unreachable ();
1441 }
1442 }
1443 return CCmode;
1444 }
1445
1446 /* Vectors to keep interesting information about registers where it can easily
1447 be got. We use to use the actual mode value as the bit number, but there
1448 is (or may be) more than 32 modes now. Instead we use two tables: one
1449 indexed by hard register number, and one indexed by mode. */
1450
1451 /* The purpose of arc_mode_class is to shrink the range of modes so that
1452 they all fit (as bit numbers) in a 32-bit word (again). Each real mode is
1453 mapped into one arc_mode_class mode. */
1454
1455 enum arc_mode_class {
1456 C_MODE,
1457 S_MODE, D_MODE, T_MODE, O_MODE,
1458 SF_MODE, DF_MODE, TF_MODE, OF_MODE,
1459 V_MODE
1460 };
1461
1462 /* Modes for condition codes. */
1463 #define C_MODES (1 << (int) C_MODE)
1464
1465 /* Modes for single-word and smaller quantities. */
1466 #define S_MODES ((1 << (int) S_MODE) | (1 << (int) SF_MODE))
1467
1468 /* Modes for double-word and smaller quantities. */
1469 #define D_MODES (S_MODES | (1 << (int) D_MODE) | (1 << DF_MODE))
1470
1471 /* Mode for 8-byte DF values only. */
1472 #define DF_MODES (1 << DF_MODE)
1473
1474 /* Modes for quad-word and smaller quantities. */
1475 #define T_MODES (D_MODES | (1 << (int) T_MODE) | (1 << (int) TF_MODE))
1476
1477 /* Modes for 128-bit vectors. */
1478 #define V_MODES (1 << (int) V_MODE)
1479
1480 /* Value is 1 if register/mode pair is acceptable on arc. */
1481
1482 unsigned int arc_hard_regno_mode_ok[] = {
1483 T_MODES, T_MODES, T_MODES, T_MODES, T_MODES, T_MODES, T_MODES, T_MODES,
1484 T_MODES, T_MODES, T_MODES, T_MODES, T_MODES, T_MODES, T_MODES, T_MODES,
1485 T_MODES, T_MODES, T_MODES, T_MODES, T_MODES, T_MODES, T_MODES, D_MODES,
1486 D_MODES, S_MODES, S_MODES, S_MODES, S_MODES, S_MODES, S_MODES, S_MODES,
1487
1488 /* ??? Leave these as S_MODES for now. */
1489 S_MODES, S_MODES, S_MODES, S_MODES, S_MODES, S_MODES, S_MODES, S_MODES,
1490 DF_MODES, 0, DF_MODES, 0, S_MODES, S_MODES, S_MODES, S_MODES,
1491 S_MODES, S_MODES, S_MODES, S_MODES, S_MODES, S_MODES, S_MODES, S_MODES,
1492 S_MODES, S_MODES, S_MODES, S_MODES, S_MODES, C_MODES, S_MODES,
1493
1494 V_MODES, V_MODES, V_MODES, V_MODES, V_MODES, V_MODES, V_MODES, V_MODES,
1495 V_MODES, V_MODES, V_MODES, V_MODES, V_MODES, V_MODES, V_MODES, V_MODES,
1496 V_MODES, V_MODES, V_MODES, V_MODES, V_MODES, V_MODES, V_MODES, V_MODES,
1497 V_MODES, V_MODES, V_MODES, V_MODES, V_MODES, V_MODES, V_MODES, V_MODES,
1498
1499 V_MODES, V_MODES, V_MODES, V_MODES, V_MODES, V_MODES, V_MODES, V_MODES,
1500 V_MODES, V_MODES, V_MODES, V_MODES, V_MODES, V_MODES, V_MODES, V_MODES,
1501 V_MODES, V_MODES, V_MODES, V_MODES, V_MODES, V_MODES, V_MODES, V_MODES,
1502 V_MODES, V_MODES, V_MODES, V_MODES, V_MODES, V_MODES, V_MODES, V_MODES,
1503
1504 S_MODES, S_MODES, S_MODES, S_MODES, S_MODES, S_MODES, S_MODES, S_MODES,
1505 S_MODES, S_MODES, S_MODES, S_MODES, S_MODES, S_MODES, S_MODES, S_MODES
1506 };
1507
1508 unsigned int arc_mode_class [NUM_MACHINE_MODES];
1509
1510 enum reg_class arc_regno_reg_class[FIRST_PSEUDO_REGISTER];
1511
1512 enum reg_class
1513 arc_preferred_reload_class (rtx, enum reg_class cl)
1514 {
1515 if ((cl) == CHEAP_CORE_REGS || (cl) == WRITABLE_CORE_REGS)
1516 return GENERAL_REGS;
1517 return cl;
1518 }
1519
1520 /* Initialize the arc_mode_class array. */
1521
1522 static void
1523 arc_init_reg_tables (void)
1524 {
1525 int i;
1526
1527 for (i = 0; i < NUM_MACHINE_MODES; i++)
1528 {
1529 machine_mode m = (machine_mode) i;
1530
1531 switch (GET_MODE_CLASS (m))
1532 {
1533 case MODE_INT:
1534 case MODE_PARTIAL_INT:
1535 case MODE_COMPLEX_INT:
1536 if (GET_MODE_SIZE (m) <= 4)
1537 arc_mode_class[i] = 1 << (int) S_MODE;
1538 else if (GET_MODE_SIZE (m) == 8)
1539 arc_mode_class[i] = 1 << (int) D_MODE;
1540 else if (GET_MODE_SIZE (m) == 16)
1541 arc_mode_class[i] = 1 << (int) T_MODE;
1542 else if (GET_MODE_SIZE (m) == 32)
1543 arc_mode_class[i] = 1 << (int) O_MODE;
1544 else
1545 arc_mode_class[i] = 0;
1546 break;
1547 case MODE_FLOAT:
1548 case MODE_COMPLEX_FLOAT:
1549 if (GET_MODE_SIZE (m) <= 4)
1550 arc_mode_class[i] = 1 << (int) SF_MODE;
1551 else if (GET_MODE_SIZE (m) == 8)
1552 arc_mode_class[i] = 1 << (int) DF_MODE;
1553 else if (GET_MODE_SIZE (m) == 16)
1554 arc_mode_class[i] = 1 << (int) TF_MODE;
1555 else if (GET_MODE_SIZE (m) == 32)
1556 arc_mode_class[i] = 1 << (int) OF_MODE;
1557 else
1558 arc_mode_class[i] = 0;
1559 break;
1560 case MODE_VECTOR_INT:
1561 if (GET_MODE_SIZE (m) == 4)
1562 arc_mode_class[i] = (1 << (int) S_MODE);
1563 else if (GET_MODE_SIZE (m) == 8)
1564 arc_mode_class[i] = (1 << (int) D_MODE);
1565 else
1566 arc_mode_class[i] = (1 << (int) V_MODE);
1567 break;
1568 case MODE_CC:
1569 default:
1570 /* mode_class hasn't been initialized yet for EXTRA_CC_MODES, so
1571 we must explicitly check for them here. */
1572 if (i == (int) CCmode || i == (int) CC_ZNmode || i == (int) CC_Zmode
1573 || i == (int) CC_Cmode
1574 || i == CC_FP_GTmode || i == CC_FP_GEmode || i == CC_FP_ORDmode
1575 || i == CC_FPUmode || i == CC_FPU_UNEQmode)
1576 arc_mode_class[i] = 1 << (int) C_MODE;
1577 else
1578 arc_mode_class[i] = 0;
1579 break;
1580 }
1581 }
1582 }
1583
1584 /* Core registers 56..59 are used for multiply extension options.
1585 The dsp option uses r56 and r57, these are then named acc1 and acc2.
1586 acc1 is the highpart, and acc2 the lowpart, so which register gets which
1587 number depends on endianness.
1588 The mul64 multiplier options use r57 for mlo, r58 for mmid and r59 for mhi.
1589 Because mlo / mhi form a 64 bit value, we use different gcc internal
1590 register numbers to make them form a register pair as the gcc internals
1591 know it. mmid gets number 57, if still available, and mlo / mhi get
1592 number 58 and 59, depending on endianness. We use DBX_REGISTER_NUMBER
1593 to map this back. */
1594 char rname56[5] = "r56";
1595 char rname57[5] = "r57";
1596 char rname58[5] = "r58";
1597 char rname59[5] = "r59";
1598 char rname29[7] = "ilink1";
1599 char rname30[7] = "ilink2";
1600
1601 static void
1602 arc_conditional_register_usage (void)
1603 {
1604 int regno;
1605 int i;
1606 int fix_start = 60, fix_end = 55;
1607
1608 if (TARGET_V2)
1609 {
1610 /* For ARCv2 the core register set is changed. */
1611 strcpy (rname29, "ilink");
1612 strcpy (rname30, "r30");
1613 call_used_regs[30] = 1;
1614 fixed_regs[30] = 0;
1615
1616 arc_regno_reg_class[30] = WRITABLE_CORE_REGS;
1617 SET_HARD_REG_BIT (reg_class_contents[WRITABLE_CORE_REGS], 30);
1618 SET_HARD_REG_BIT (reg_class_contents[CHEAP_CORE_REGS], 30);
1619 SET_HARD_REG_BIT (reg_class_contents[GENERAL_REGS], 30);
1620 SET_HARD_REG_BIT (reg_class_contents[MPY_WRITABLE_CORE_REGS], 30);
1621 }
1622
1623 if (TARGET_MUL64_SET)
1624 {
1625 fix_start = 57;
1626 fix_end = 59;
1627
1628 /* We don't provide a name for mmed. In rtl / assembly resource lists,
1629 you are supposed to refer to it as mlo & mhi, e.g
1630 (zero_extract:SI (reg:DI 58) (const_int 32) (16)) .
1631 In an actual asm instruction, you are of course use mmed.
1632 The point of avoiding having a separate register for mmed is that
1633 this way, we don't have to carry clobbers of that reg around in every
1634 isntruction that modifies mlo and/or mhi. */
1635 strcpy (rname57, "");
1636 strcpy (rname58, TARGET_BIG_ENDIAN ? "mhi" : "mlo");
1637 strcpy (rname59, TARGET_BIG_ENDIAN ? "mlo" : "mhi");
1638 }
1639
1640 /* The nature of arc_tp_regno is actually something more like a global
1641 register, however globalize_reg requires a declaration.
1642 We use EPILOGUE_USES to compensate so that sets from
1643 __builtin_set_frame_pointer are not deleted. */
1644 if (arc_tp_regno != -1)
1645 fixed_regs[arc_tp_regno] = call_used_regs[arc_tp_regno] = 1;
1646
1647 if (TARGET_MULMAC_32BY16_SET)
1648 {
1649 fix_start = 56;
1650 fix_end = fix_end > 57 ? fix_end : 57;
1651 strcpy (rname56, TARGET_BIG_ENDIAN ? "acc1" : "acc2");
1652 strcpy (rname57, TARGET_BIG_ENDIAN ? "acc2" : "acc1");
1653 }
1654 for (regno = fix_start; regno <= fix_end; regno++)
1655 {
1656 if (!fixed_regs[regno])
1657 warning (0, "multiply option implies r%d is fixed", regno);
1658 fixed_regs [regno] = call_used_regs[regno] = 1;
1659 }
1660 if (TARGET_Q_CLASS)
1661 {
1662 if (optimize_size)
1663 {
1664 reg_alloc_order[0] = 0;
1665 reg_alloc_order[1] = 1;
1666 reg_alloc_order[2] = 2;
1667 reg_alloc_order[3] = 3;
1668 reg_alloc_order[4] = 12;
1669 reg_alloc_order[5] = 13;
1670 reg_alloc_order[6] = 14;
1671 reg_alloc_order[7] = 15;
1672 reg_alloc_order[8] = 4;
1673 reg_alloc_order[9] = 5;
1674 reg_alloc_order[10] = 6;
1675 reg_alloc_order[11] = 7;
1676 reg_alloc_order[12] = 8;
1677 reg_alloc_order[13] = 9;
1678 reg_alloc_order[14] = 10;
1679 reg_alloc_order[15] = 11;
1680 }
1681 else
1682 {
1683 reg_alloc_order[2] = 12;
1684 reg_alloc_order[3] = 13;
1685 reg_alloc_order[4] = 14;
1686 reg_alloc_order[5] = 15;
1687 reg_alloc_order[6] = 1;
1688 reg_alloc_order[7] = 0;
1689 reg_alloc_order[8] = 4;
1690 reg_alloc_order[9] = 5;
1691 reg_alloc_order[10] = 6;
1692 reg_alloc_order[11] = 7;
1693 reg_alloc_order[12] = 8;
1694 reg_alloc_order[13] = 9;
1695 reg_alloc_order[14] = 10;
1696 reg_alloc_order[15] = 11;
1697 }
1698 }
1699 if (TARGET_SIMD_SET)
1700 {
1701 int i;
1702 for (i = ARC_FIRST_SIMD_VR_REG; i <= ARC_LAST_SIMD_VR_REG; i++)
1703 reg_alloc_order [i] = i;
1704 for (i = ARC_FIRST_SIMD_DMA_CONFIG_REG;
1705 i <= ARC_LAST_SIMD_DMA_CONFIG_REG; i++)
1706 reg_alloc_order [i] = i;
1707 }
1708 /* For ARC600, lp_count may not be read in an instruction
1709 following immediately after another one setting it to a new value.
1710 There was some discussion on how to enforce scheduling constraints for
1711 processors with missing interlocks on the gcc mailing list:
1712 http://gcc.gnu.org/ml/gcc/2008-05/msg00021.html .
1713 However, we can't actually use this approach, because for ARC the
1714 delay slot scheduling pass is active, which runs after
1715 machine_dependent_reorg. */
1716 if (TARGET_ARC600)
1717 CLEAR_HARD_REG_BIT (reg_class_contents[SIBCALL_REGS], LP_COUNT);
1718 else if (!TARGET_LP_WR_INTERLOCK)
1719 fixed_regs[LP_COUNT] = 1;
1720 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1721 if (!call_used_regs[regno])
1722 CLEAR_HARD_REG_BIT (reg_class_contents[SIBCALL_REGS], regno);
1723 for (regno = 32; regno < 60; regno++)
1724 if (!fixed_regs[regno])
1725 SET_HARD_REG_BIT (reg_class_contents[WRITABLE_CORE_REGS], regno);
1726 if (!TARGET_ARC600_FAMILY)
1727 {
1728 for (regno = 32; regno <= 60; regno++)
1729 CLEAR_HARD_REG_BIT (reg_class_contents[CHEAP_CORE_REGS], regno);
1730
1731 /* If they have used -ffixed-lp_count, make sure it takes
1732 effect. */
1733 if (fixed_regs[LP_COUNT])
1734 {
1735 CLEAR_HARD_REG_BIT (reg_class_contents[LPCOUNT_REG], LP_COUNT);
1736 CLEAR_HARD_REG_BIT (reg_class_contents[SIBCALL_REGS], LP_COUNT);
1737 CLEAR_HARD_REG_BIT (reg_class_contents[WRITABLE_CORE_REGS], LP_COUNT);
1738
1739 /* Instead of taking out SF_MODE like below, forbid it outright. */
1740 arc_hard_regno_mode_ok[60] = 0;
1741 }
1742 else
1743 arc_hard_regno_mode_ok[60] = 1 << (int) S_MODE;
1744 }
1745
1746 /* ARCHS has 64-bit data-path which makes use of the even-odd paired
1747 registers. */
1748 if (TARGET_HS)
1749 {
1750 for (regno = 1; regno < 32; regno +=2)
1751 {
1752 arc_hard_regno_mode_ok[regno] = S_MODES;
1753 }
1754 }
1755
1756 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1757 {
1758 if (i < 29)
1759 {
1760 if ((TARGET_Q_CLASS || TARGET_RRQ_CLASS)
1761 && ((i <= 3) || ((i >= 12) && (i <= 15))))
1762 arc_regno_reg_class[i] = ARCOMPACT16_REGS;
1763 else
1764 arc_regno_reg_class[i] = GENERAL_REGS;
1765 }
1766 else if (i < 60)
1767 arc_regno_reg_class[i]
1768 = (fixed_regs[i]
1769 ? (TEST_HARD_REG_BIT (reg_class_contents[CHEAP_CORE_REGS], i)
1770 ? CHEAP_CORE_REGS : ALL_CORE_REGS)
1771 : (((!TARGET_ARC600_FAMILY)
1772 && TEST_HARD_REG_BIT (reg_class_contents[CHEAP_CORE_REGS], i))
1773 ? CHEAP_CORE_REGS : WRITABLE_CORE_REGS));
1774 else
1775 arc_regno_reg_class[i] = NO_REGS;
1776 }
1777
1778 /* ARCOMPACT16_REGS is empty, if TARGET_Q_CLASS / TARGET_RRQ_CLASS
1779 has not been activated. */
1780 if (!TARGET_Q_CLASS && !TARGET_RRQ_CLASS)
1781 CLEAR_HARD_REG_SET(reg_class_contents [ARCOMPACT16_REGS]);
1782 if (!TARGET_Q_CLASS)
1783 CLEAR_HARD_REG_SET(reg_class_contents [AC16_BASE_REGS]);
1784
1785 gcc_assert (FIRST_PSEUDO_REGISTER >= 144);
1786
1787 /* Handle Special Registers. */
1788 arc_regno_reg_class[29] = LINK_REGS; /* ilink1 register. */
1789 if (!TARGET_V2)
1790 arc_regno_reg_class[30] = LINK_REGS; /* ilink2 register. */
1791 arc_regno_reg_class[31] = LINK_REGS; /* blink register. */
1792 arc_regno_reg_class[60] = LPCOUNT_REG;
1793 arc_regno_reg_class[61] = NO_REGS; /* CC_REG: must be NO_REGS. */
1794 arc_regno_reg_class[62] = GENERAL_REGS;
1795
1796 if (TARGET_DPFP)
1797 {
1798 for (i = 40; i < 44; ++i)
1799 {
1800 arc_regno_reg_class[i] = DOUBLE_REGS;
1801
1802 /* Unless they want us to do 'mov d1, 0x00000000' make sure
1803 no attempt is made to use such a register as a destination
1804 operand in *movdf_insn. */
1805 if (!TARGET_ARGONAUT_SET)
1806 {
1807 /* Make sure no 'c', 'w', 'W', or 'Rac' constraint is
1808 interpreted to mean they can use D1 or D2 in their insn. */
1809 CLEAR_HARD_REG_BIT(reg_class_contents[CHEAP_CORE_REGS ], i);
1810 CLEAR_HARD_REG_BIT(reg_class_contents[ALL_CORE_REGS ], i);
1811 CLEAR_HARD_REG_BIT(reg_class_contents[WRITABLE_CORE_REGS ], i);
1812 CLEAR_HARD_REG_BIT(reg_class_contents[MPY_WRITABLE_CORE_REGS], i);
1813 }
1814 }
1815 }
1816 else
1817 {
1818 /* Disable all DOUBLE_REGISTER settings,
1819 if not generating DPFP code. */
1820 arc_regno_reg_class[40] = ALL_REGS;
1821 arc_regno_reg_class[41] = ALL_REGS;
1822 arc_regno_reg_class[42] = ALL_REGS;
1823 arc_regno_reg_class[43] = ALL_REGS;
1824
1825 fixed_regs[40] = 1;
1826 fixed_regs[41] = 1;
1827 fixed_regs[42] = 1;
1828 fixed_regs[43] = 1;
1829
1830 arc_hard_regno_mode_ok[40] = 0;
1831 arc_hard_regno_mode_ok[42] = 0;
1832
1833 CLEAR_HARD_REG_SET(reg_class_contents [DOUBLE_REGS]);
1834 }
1835
1836 if (TARGET_SIMD_SET)
1837 {
1838 gcc_assert (ARC_FIRST_SIMD_VR_REG == 64);
1839 gcc_assert (ARC_LAST_SIMD_VR_REG == 127);
1840
1841 for (i = ARC_FIRST_SIMD_VR_REG; i <= ARC_LAST_SIMD_VR_REG; i++)
1842 arc_regno_reg_class [i] = SIMD_VR_REGS;
1843
1844 gcc_assert (ARC_FIRST_SIMD_DMA_CONFIG_REG == 128);
1845 gcc_assert (ARC_FIRST_SIMD_DMA_CONFIG_IN_REG == 128);
1846 gcc_assert (ARC_FIRST_SIMD_DMA_CONFIG_OUT_REG == 136);
1847 gcc_assert (ARC_LAST_SIMD_DMA_CONFIG_REG == 143);
1848
1849 for (i = ARC_FIRST_SIMD_DMA_CONFIG_REG;
1850 i <= ARC_LAST_SIMD_DMA_CONFIG_REG; i++)
1851 arc_regno_reg_class [i] = SIMD_DMA_CONFIG_REGS;
1852 }
1853
1854 /* pc : r63 */
1855 arc_regno_reg_class[PROGRAM_COUNTER_REGNO] = GENERAL_REGS;
1856
1857 /*ARCV2 Accumulator. */
1858 if ((TARGET_V2
1859 && (TARGET_FP_DP_FUSED || TARGET_FP_SP_FUSED))
1860 || TARGET_PLUS_DMPY)
1861 {
1862 arc_regno_reg_class[ACCL_REGNO] = WRITABLE_CORE_REGS;
1863 arc_regno_reg_class[ACCH_REGNO] = WRITABLE_CORE_REGS;
1864 SET_HARD_REG_BIT (reg_class_contents[WRITABLE_CORE_REGS], ACCL_REGNO);
1865 SET_HARD_REG_BIT (reg_class_contents[WRITABLE_CORE_REGS], ACCH_REGNO);
1866 SET_HARD_REG_BIT (reg_class_contents[CHEAP_CORE_REGS], ACCL_REGNO);
1867 SET_HARD_REG_BIT (reg_class_contents[CHEAP_CORE_REGS], ACCH_REGNO);
1868 SET_HARD_REG_BIT (reg_class_contents[GENERAL_REGS], ACCL_REGNO);
1869 SET_HARD_REG_BIT (reg_class_contents[GENERAL_REGS], ACCH_REGNO);
1870 SET_HARD_REG_BIT (reg_class_contents[MPY_WRITABLE_CORE_REGS], ACCL_REGNO);
1871 SET_HARD_REG_BIT (reg_class_contents[MPY_WRITABLE_CORE_REGS], ACCH_REGNO);
1872
1873 /* Allow the compiler to freely use them. */
1874 fixed_regs[ACCL_REGNO] = 0;
1875 fixed_regs[ACCH_REGNO] = 0;
1876
1877 arc_hard_regno_mode_ok[ACC_REG_FIRST] = D_MODES;
1878 }
1879 }
1880
1881 /* Handle an "interrupt" attribute; arguments as in
1882 struct attribute_spec.handler. */
1883
1884 static tree
1885 arc_handle_interrupt_attribute (tree *, tree name, tree args, int,
1886 bool *no_add_attrs)
1887 {
1888 gcc_assert (args);
1889
1890 tree value = TREE_VALUE (args);
1891
1892 if (TREE_CODE (value) != STRING_CST)
1893 {
1894 warning (OPT_Wattributes,
1895 "argument of %qE attribute is not a string constant",
1896 name);
1897 *no_add_attrs = true;
1898 }
1899 else if (!TARGET_V2
1900 && strcmp (TREE_STRING_POINTER (value), "ilink1")
1901 && strcmp (TREE_STRING_POINTER (value), "ilink2"))
1902 {
1903 warning (OPT_Wattributes,
1904 "argument of %qE attribute is not \"ilink1\" or \"ilink2\"",
1905 name);
1906 *no_add_attrs = true;
1907 }
1908 else if (TARGET_V2
1909 && strcmp (TREE_STRING_POINTER (value), "ilink")
1910 && strcmp (TREE_STRING_POINTER (value), "firq"))
1911 {
1912 warning (OPT_Wattributes,
1913 "argument of %qE attribute is not \"ilink\" or \"firq\"",
1914 name);
1915 *no_add_attrs = true;
1916 }
1917
1918 return NULL_TREE;
1919 }
1920
1921 static tree
1922 arc_handle_fndecl_attribute (tree *node, tree name, tree args ATTRIBUTE_UNUSED,
1923 int flags ATTRIBUTE_UNUSED, bool *no_add_attrs)
1924 {
1925 if (TREE_CODE (*node) != FUNCTION_DECL)
1926 {
1927 warning (OPT_Wattributes, "%qE attribute only applies to functions",
1928 name);
1929 *no_add_attrs = true;
1930 }
1931
1932 return NULL_TREE;
1933 }
1934
1935 /* Implement `TARGET_ALLOCATE_STACK_SLOTS_FOR_ARGS' */
1936
1937 static bool
1938 arc_allocate_stack_slots_for_args (void)
1939 {
1940 /* Naked functions should not allocate stack slots for arguments. */
1941 unsigned int fn_type = arc_compute_function_type (cfun);
1942
1943 return !ARC_NAKED_P(fn_type);
1944 }
1945
1946 /* Implement `TARGET_WARN_FUNC_RETURN'. */
1947
1948 static bool
1949 arc_warn_func_return (tree decl)
1950 {
1951 struct function *func = DECL_STRUCT_FUNCTION (decl);
1952 unsigned int fn_type = arc_compute_function_type (func);
1953
1954 return !ARC_NAKED_P (fn_type);
1955 }
1956
1957 /* Return zero if TYPE1 and TYPE are incompatible, one if they are compatible,
1958 and two if they are nearly compatible (which causes a warning to be
1959 generated). */
1960
1961 static int
1962 arc_comp_type_attributes (const_tree type1,
1963 const_tree type2)
1964 {
1965 int l1, l2, m1, m2, s1, s2;
1966
1967 /* Check for mismatch of non-default calling convention. */
1968 if (TREE_CODE (type1) != FUNCTION_TYPE)
1969 return 1;
1970
1971 /* Check for mismatched call attributes. */
1972 l1 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type1)) != NULL;
1973 l2 = lookup_attribute ("long_call", TYPE_ATTRIBUTES (type2)) != NULL;
1974 m1 = lookup_attribute ("medium_call", TYPE_ATTRIBUTES (type1)) != NULL;
1975 m2 = lookup_attribute ("medium_call", TYPE_ATTRIBUTES (type2)) != NULL;
1976 s1 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type1)) != NULL;
1977 s2 = lookup_attribute ("short_call", TYPE_ATTRIBUTES (type2)) != NULL;
1978
1979 /* Only bother to check if an attribute is defined. */
1980 if (l1 | l2 | m1 | m2 | s1 | s2)
1981 {
1982 /* If one type has an attribute, the other must have the same attribute. */
1983 if ((l1 != l2) || (m1 != m2) || (s1 != s2))
1984 return 0;
1985
1986 /* Disallow mixed attributes. */
1987 if (l1 + m1 + s1 > 1)
1988 return 0;
1989 }
1990
1991
1992 return 1;
1993 }
1994
1995 /* Set the default attributes for TYPE. */
1996
1997 void
1998 arc_set_default_type_attributes (tree type ATTRIBUTE_UNUSED)
1999 {
2000 gcc_unreachable();
2001 }
2002
2003 /* Misc. utilities. */
2004
2005 /* X and Y are two things to compare using CODE. Emit the compare insn and
2006 return the rtx for the cc reg in the proper mode. */
2007
2008 rtx
2009 gen_compare_reg (rtx comparison, machine_mode omode)
2010 {
2011 enum rtx_code code = GET_CODE (comparison);
2012 rtx x = XEXP (comparison, 0);
2013 rtx y = XEXP (comparison, 1);
2014 rtx tmp, cc_reg;
2015 machine_mode mode, cmode;
2016
2017
2018 cmode = GET_MODE (x);
2019 if (cmode == VOIDmode)
2020 cmode = GET_MODE (y);
2021 gcc_assert (cmode == SImode || cmode == SFmode || cmode == DFmode);
2022 if (cmode == SImode)
2023 {
2024 if (!register_operand (x, SImode))
2025 {
2026 if (register_operand (y, SImode))
2027 {
2028 tmp = x;
2029 x = y;
2030 y = tmp;
2031 code = swap_condition (code);
2032 }
2033 else
2034 x = copy_to_mode_reg (SImode, x);
2035 }
2036 if (GET_CODE (y) == SYMBOL_REF && flag_pic)
2037 y = copy_to_mode_reg (SImode, y);
2038 }
2039 else
2040 {
2041 x = force_reg (cmode, x);
2042 y = force_reg (cmode, y);
2043 }
2044 mode = SELECT_CC_MODE (code, x, y);
2045
2046 cc_reg = gen_rtx_REG (mode, CC_REG);
2047
2048 /* ??? FIXME (x-y)==0, as done by both cmpsfpx_raw and
2049 cmpdfpx_raw, is not a correct comparison for floats:
2050 http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
2051 */
2052 if (TARGET_ARGONAUT_SET
2053 && ((cmode == SFmode && TARGET_SPFP) || (cmode == DFmode && TARGET_DPFP)))
2054 {
2055 switch (code)
2056 {
2057 case NE: case EQ: case LT: case UNGE: case LE: case UNGT:
2058 case UNEQ: case LTGT: case ORDERED: case UNORDERED:
2059 break;
2060 case GT: case UNLE: case GE: case UNLT:
2061 code = swap_condition (code);
2062 tmp = x;
2063 x = y;
2064 y = tmp;
2065 break;
2066 default:
2067 gcc_unreachable ();
2068 }
2069 if (cmode == SFmode)
2070 {
2071 emit_insn (gen_cmpsfpx_raw (x, y));
2072 }
2073 else /* DFmode */
2074 {
2075 /* Accepts Dx regs directly by insns. */
2076 emit_insn (gen_cmpdfpx_raw (x, y));
2077 }
2078
2079 if (mode != CC_FPXmode)
2080 emit_insn (gen_rtx_SET (cc_reg,
2081 gen_rtx_COMPARE (mode,
2082 gen_rtx_REG (CC_FPXmode, 61),
2083 const0_rtx)));
2084 }
2085 else if (TARGET_FPX_QUARK && (cmode == SFmode))
2086 {
2087 switch (code)
2088 {
2089 case NE: case EQ: case GT: case UNLE: case GE: case UNLT:
2090 case UNEQ: case LTGT: case ORDERED: case UNORDERED:
2091 break;
2092 case LT: case UNGE: case LE: case UNGT:
2093 code = swap_condition (code);
2094 tmp = x;
2095 x = y;
2096 y = tmp;
2097 break;
2098 default:
2099 gcc_unreachable ();
2100 }
2101
2102 emit_insn (gen_cmp_quark (cc_reg,
2103 gen_rtx_COMPARE (mode, x, y)));
2104 }
2105 else if (TARGET_HARD_FLOAT
2106 && ((cmode == SFmode && TARGET_FP_SP_BASE)
2107 || (cmode == DFmode && TARGET_FP_DP_BASE)))
2108 emit_insn (gen_rtx_SET (cc_reg, gen_rtx_COMPARE (mode, x, y)));
2109 else if (GET_MODE_CLASS (cmode) == MODE_FLOAT && TARGET_OPTFPE)
2110 {
2111 rtx op0 = gen_rtx_REG (cmode, 0);
2112 rtx op1 = gen_rtx_REG (cmode, GET_MODE_SIZE (cmode) / UNITS_PER_WORD);
2113 bool swap = false;
2114
2115 switch (code)
2116 {
2117 case NE: case EQ: case GT: case UNLE: case GE: case UNLT:
2118 case UNEQ: case LTGT: case ORDERED: case UNORDERED:
2119 break;
2120 case LT: case UNGE: case LE: case UNGT:
2121 code = swap_condition (code);
2122 swap = true;
2123 break;
2124 default:
2125 gcc_unreachable ();
2126 }
2127 if (currently_expanding_to_rtl)
2128 {
2129 if (swap)
2130 {
2131 tmp = x;
2132 x = y;
2133 y = tmp;
2134 }
2135 emit_move_insn (op0, x);
2136 emit_move_insn (op1, y);
2137 }
2138 else
2139 {
2140 gcc_assert (rtx_equal_p (op0, x));
2141 gcc_assert (rtx_equal_p (op1, y));
2142 if (swap)
2143 {
2144 op0 = y;
2145 op1 = x;
2146 }
2147 }
2148 emit_insn (gen_cmp_float (cc_reg, gen_rtx_COMPARE (mode, op0, op1)));
2149 }
2150 else
2151 emit_insn (gen_rtx_SET (cc_reg, gen_rtx_COMPARE (mode, x, y)));
2152 return gen_rtx_fmt_ee (code, omode, cc_reg, const0_rtx);
2153 }
2154
2155 /* Return true if VALUE, a const_double, will fit in a limm (4 byte number).
2156 We assume the value can be either signed or unsigned. */
2157
2158 bool
2159 arc_double_limm_p (rtx value)
2160 {
2161 HOST_WIDE_INT low, high;
2162
2163 gcc_assert (GET_CODE (value) == CONST_DOUBLE);
2164
2165 if (TARGET_DPFP)
2166 return true;
2167
2168 low = CONST_DOUBLE_LOW (value);
2169 high = CONST_DOUBLE_HIGH (value);
2170
2171 if (low & 0x80000000)
2172 {
2173 return (((unsigned HOST_WIDE_INT) low <= 0xffffffff && high == 0)
2174 || (((low & - (unsigned HOST_WIDE_INT) 0x80000000)
2175 == - (unsigned HOST_WIDE_INT) 0x80000000)
2176 && high == -1));
2177 }
2178 else
2179 {
2180 return (unsigned HOST_WIDE_INT) low <= 0x7fffffff && high == 0;
2181 }
2182 }
2183
2184 /* Do any needed setup for a variadic function. For the ARC, we must
2185 create a register parameter block, and then copy any anonymous arguments
2186 in registers to memory.
2187
2188 CUM has not been updated for the last named argument which has type TYPE
2189 and mode MODE, and we rely on this fact. */
2190
2191 static void
2192 arc_setup_incoming_varargs (cumulative_args_t args_so_far,
2193 machine_mode mode, tree type,
2194 int *pretend_size, int no_rtl)
2195 {
2196 int first_anon_arg;
2197 CUMULATIVE_ARGS next_cum;
2198
2199 /* We must treat `__builtin_va_alist' as an anonymous arg. */
2200
2201 next_cum = *get_cumulative_args (args_so_far);
2202 arc_function_arg_advance (pack_cumulative_args (&next_cum),
2203 mode, type, true);
2204 first_anon_arg = next_cum;
2205
2206 if (FUNCTION_ARG_REGNO_P (first_anon_arg))
2207 {
2208 /* First anonymous (unnamed) argument is in a reg. */
2209
2210 /* Note that first_reg_offset < MAX_ARC_PARM_REGS. */
2211 int first_reg_offset = first_anon_arg;
2212
2213 if (!no_rtl)
2214 {
2215 rtx regblock
2216 = gen_rtx_MEM (BLKmode, plus_constant (Pmode, arg_pointer_rtx,
2217 FIRST_PARM_OFFSET (0)));
2218 move_block_from_reg (first_reg_offset, regblock,
2219 MAX_ARC_PARM_REGS - first_reg_offset);
2220 }
2221
2222 *pretend_size
2223 = ((MAX_ARC_PARM_REGS - first_reg_offset ) * UNITS_PER_WORD);
2224 }
2225 }
2226
2227 /* Cost functions. */
2228
2229 /* Provide the costs of an addressing mode that contains ADDR.
2230 If ADDR is not a valid address, its cost is irrelevant. */
2231
2232 int
2233 arc_address_cost (rtx addr, machine_mode, addr_space_t, bool speed)
2234 {
2235 switch (GET_CODE (addr))
2236 {
2237 case REG :
2238 return speed || satisfies_constraint_Rcq (addr) ? 0 : 1;
2239 case PRE_INC: case PRE_DEC: case POST_INC: case POST_DEC:
2240 case PRE_MODIFY: case POST_MODIFY:
2241 return !speed;
2242
2243 case LABEL_REF :
2244 case SYMBOL_REF :
2245 case CONST :
2246 if (TARGET_NPS_CMEM && cmem_address (addr, SImode))
2247 return 0;
2248 /* Most likely needs a LIMM. */
2249 return COSTS_N_INSNS (1);
2250
2251 case PLUS :
2252 {
2253 register rtx plus0 = XEXP (addr, 0);
2254 register rtx plus1 = XEXP (addr, 1);
2255
2256 if (GET_CODE (plus0) != REG
2257 && (GET_CODE (plus0) != MULT
2258 || !CONST_INT_P (XEXP (plus0, 1))
2259 || (INTVAL (XEXP (plus0, 1)) != 2
2260 && INTVAL (XEXP (plus0, 1)) != 4)))
2261 break;
2262
2263 switch (GET_CODE (plus1))
2264 {
2265 case CONST_INT :
2266 return (!RTX_OK_FOR_OFFSET_P (SImode, plus1)
2267 ? COSTS_N_INSNS (1)
2268 : speed
2269 ? 0
2270 : (satisfies_constraint_Rcq (plus0)
2271 && satisfies_constraint_O (plus1))
2272 ? 0
2273 : 1);
2274 case REG:
2275 return (speed < 1 ? 0
2276 : (satisfies_constraint_Rcq (plus0)
2277 && satisfies_constraint_Rcq (plus1))
2278 ? 0 : 1);
2279 case CONST :
2280 case SYMBOL_REF :
2281 case LABEL_REF :
2282 return COSTS_N_INSNS (1);
2283 default:
2284 break;
2285 }
2286 break;
2287 }
2288 default:
2289 break;
2290 }
2291
2292 return 4;
2293 }
2294
2295 /* Emit instruction X with the frame related bit set. */
2296
2297 static rtx
2298 frame_insn (rtx x)
2299 {
2300 x = emit_insn (x);
2301 RTX_FRAME_RELATED_P (x) = 1;
2302 return x;
2303 }
2304
2305 /* Emit a frame insn to move SRC to DST. */
2306
2307 static rtx
2308 frame_move (rtx dst, rtx src)
2309 {
2310 rtx tmp = gen_rtx_SET (dst, src);
2311 RTX_FRAME_RELATED_P (tmp) = 1;
2312 return frame_insn (tmp);
2313 }
2314
2315 /* Like frame_move, but add a REG_INC note for REG if ADDR contains an
2316 auto increment address, or is zero. */
2317
2318 static rtx
2319 frame_move_inc (rtx dst, rtx src, rtx reg, rtx addr)
2320 {
2321 rtx insn = frame_move (dst, src);
2322
2323 if (!addr
2324 || GET_CODE (addr) == PRE_DEC || GET_CODE (addr) == POST_INC
2325 || GET_CODE (addr) == PRE_MODIFY || GET_CODE (addr) == POST_MODIFY)
2326 add_reg_note (insn, REG_INC, reg);
2327 return insn;
2328 }
2329
2330 /* Emit a frame insn which adjusts a frame address register REG by OFFSET. */
2331
2332 static rtx
2333 frame_add (rtx reg, HOST_WIDE_INT offset)
2334 {
2335 gcc_assert ((offset & 0x3) == 0);
2336 if (!offset)
2337 return NULL_RTX;
2338 return frame_move (reg, plus_constant (Pmode, reg, offset));
2339 }
2340
2341 /* Emit a frame insn which adjusts stack pointer by OFFSET. */
2342
2343 static rtx
2344 frame_stack_add (HOST_WIDE_INT offset)
2345 {
2346 return frame_add (stack_pointer_rtx, offset);
2347 }
2348
2349 /* Traditionally, we push saved registers first in the prologue,
2350 then we allocate the rest of the frame - and reverse in the epilogue.
2351 This has still its merits for ease of debugging, or saving code size
2352 or even execution time if the stack frame is so large that some accesses
2353 can't be encoded anymore with offsets in the instruction code when using
2354 a different scheme.
2355 Also, it would be a good starting point if we got instructions to help
2356 with register save/restore.
2357
2358 However, often stack frames are small, and the pushing / popping has
2359 some costs:
2360 - the stack modification prevents a lot of scheduling.
2361 - frame allocation / deallocation needs extra instructions.
2362 - unless we know that we compile ARC700 user code, we need to put
2363 a memory barrier after frame allocation / before deallocation to
2364 prevent interrupts clobbering our data in the frame.
2365 In particular, we don't have any such guarantees for library functions,
2366 which tend to, on the other hand, to have small frames.
2367
2368 Thus, for small frames, we'd like to use a different scheme:
2369 - The frame is allocated in full with the first prologue instruction,
2370 and deallocated in full with the last epilogue instruction.
2371 Thus, the instructions in-betwen can be freely scheduled.
2372 - If the function has no outgoing arguments on the stack, we can allocate
2373 one register save slot at the top of the stack. This register can then
2374 be saved simultanously with frame allocation, and restored with
2375 frame deallocation.
2376 This register can be picked depending on scheduling considerations,
2377 although same though should go into having some set of registers
2378 to be potentially lingering after a call, and others to be available
2379 immediately - i.e. in the absence of interprocedual optimization, we
2380 can use an ABI-like convention for register allocation to reduce
2381 stalls after function return. */
2382 /* Function prologue/epilogue handlers. */
2383
2384 /* ARCompact stack frames look like:
2385
2386 Before call After call
2387 high +-----------------------+ +-----------------------+
2388 mem | reg parm save area | | reg parm save area |
2389 | only created for | | only created for |
2390 | variable arg fns | | variable arg fns |
2391 AP +-----------------------+ +-----------------------+
2392 | return addr register | | return addr register |
2393 | (if required) | | (if required) |
2394 +-----------------------+ +-----------------------+
2395 | | | |
2396 | reg save area | | reg save area |
2397 | | | |
2398 +-----------------------+ +-----------------------+
2399 | frame pointer | | frame pointer |
2400 | (if required) | | (if required) |
2401 FP +-----------------------+ +-----------------------+
2402 | | | |
2403 | local/temp variables | | local/temp variables |
2404 | | | |
2405 +-----------------------+ +-----------------------+
2406 | | | |
2407 | arguments on stack | | arguments on stack |
2408 | | | |
2409 SP +-----------------------+ +-----------------------+
2410 | reg parm save area |
2411 | only created for |
2412 | variable arg fns |
2413 AP +-----------------------+
2414 | return addr register |
2415 | (if required) |
2416 +-----------------------+
2417 | |
2418 | reg save area |
2419 | |
2420 +-----------------------+
2421 | frame pointer |
2422 | (if required) |
2423 FP +-----------------------+
2424 | |
2425 | local/temp variables |
2426 | |
2427 +-----------------------+
2428 | |
2429 | arguments on stack |
2430 low | |
2431 mem SP +-----------------------+
2432
2433 Notes:
2434 1) The "reg parm save area" does not exist for non variable argument fns.
2435 The "reg parm save area" can be eliminated completely if we created our
2436 own va-arc.h, but that has tradeoffs as well (so it's not done). */
2437
2438 /* Structure to be filled in by arc_compute_frame_size with register
2439 save masks, and offsets for the current function. */
2440 struct GTY (()) arc_frame_info
2441 {
2442 unsigned int total_size; /* # bytes that the entire frame takes up. */
2443 unsigned int extra_size; /* # bytes of extra stuff. */
2444 unsigned int pretend_size; /* # bytes we push and pretend caller did. */
2445 unsigned int args_size; /* # bytes that outgoing arguments take up. */
2446 unsigned int reg_size; /* # bytes needed to store regs. */
2447 unsigned int var_size; /* # bytes that variables take up. */
2448 unsigned int reg_offset; /* Offset from new sp to store regs. */
2449 unsigned int gmask; /* Mask of saved gp registers. */
2450 int initialized; /* Nonzero if frame size already calculated. */
2451 short millicode_start_reg;
2452 short millicode_end_reg;
2453 bool save_return_addr;
2454 };
2455
2456 /* Defining data structures for per-function information. */
2457
2458 typedef struct GTY (()) machine_function
2459 {
2460 unsigned int fn_type;
2461 struct arc_frame_info frame_info;
2462 /* To keep track of unalignment caused by short insns. */
2463 int unalign;
2464 int force_short_suffix; /* Used when disgorging return delay slot insns. */
2465 const char *size_reason;
2466 struct arc_ccfsm ccfsm_current;
2467 /* Map from uid to ccfsm state during branch shortening. */
2468 rtx ccfsm_current_insn;
2469 char arc_reorg_started;
2470 char prescan_initialized;
2471 } machine_function;
2472
2473 /* Type of function DECL.
2474
2475 The result is cached. To reset the cache at the end of a function,
2476 call with DECL = NULL_TREE. */
2477
2478 unsigned int
2479 arc_compute_function_type (struct function *fun)
2480 {
2481 tree attr, decl = fun->decl;
2482 unsigned int fn_type = fun->machine->fn_type;
2483
2484 if (fn_type != ARC_FUNCTION_UNKNOWN)
2485 return fn_type;
2486
2487 /* Check if it is a naked function. */
2488 if (lookup_attribute ("naked", DECL_ATTRIBUTES (decl)) != NULL_TREE)
2489 fn_type |= ARC_FUNCTION_NAKED;
2490 else
2491 fn_type |= ARC_FUNCTION_NORMAL;
2492
2493 /* Now see if this is an interrupt handler. */
2494 attr = lookup_attribute ("interrupt", DECL_ATTRIBUTES (decl));
2495 if (attr != NULL_TREE)
2496 {
2497 tree value, args = TREE_VALUE (attr);
2498
2499 gcc_assert (list_length (args) == 1);
2500 value = TREE_VALUE (args);
2501 gcc_assert (TREE_CODE (value) == STRING_CST);
2502
2503 if (!strcmp (TREE_STRING_POINTER (value), "ilink1")
2504 || !strcmp (TREE_STRING_POINTER (value), "ilink"))
2505 fn_type |= ARC_FUNCTION_ILINK1;
2506 else if (!strcmp (TREE_STRING_POINTER (value), "ilink2"))
2507 fn_type |= ARC_FUNCTION_ILINK2;
2508 else if (!strcmp (TREE_STRING_POINTER (value), "firq"))
2509 fn_type |= ARC_FUNCTION_FIRQ;
2510 else
2511 gcc_unreachable ();
2512 }
2513
2514 return fun->machine->fn_type = fn_type;
2515 }
2516
2517 #define FRAME_POINTER_MASK (1 << (FRAME_POINTER_REGNUM))
2518 #define RETURN_ADDR_MASK (1 << (RETURN_ADDR_REGNUM))
2519
2520 /* Tell prologue and epilogue if register REGNO should be saved / restored.
2521 The return address and frame pointer are treated separately.
2522 Don't consider them here.
2523 Addition for pic: The gp register needs to be saved if the current
2524 function changes it to access gotoff variables.
2525 FIXME: This will not be needed if we used some arbitrary register
2526 instead of r26.
2527 */
2528
2529 static bool
2530 arc_must_save_register (int regno, struct function *func)
2531 {
2532 unsigned int fn_type = arc_compute_function_type (func);
2533 bool irq_auto_save_p = ((irq_ctrl_saved.irq_save_last_reg >= regno)
2534 && ARC_AUTO_IRQ_P (fn_type));
2535 bool firq_auto_save_p = ARC_FAST_INTERRUPT_P (fn_type);
2536
2537 switch (rgf_banked_register_count)
2538 {
2539 case 4:
2540 firq_auto_save_p &= (regno < 4);
2541 break;
2542 case 8:
2543 firq_auto_save_p &= ((regno < 4) || ((regno > 11) && (regno < 16)));
2544 break;
2545 case 16:
2546 firq_auto_save_p &= ((regno < 4) || ((regno > 9) && (regno < 16))
2547 || ((regno > 25) && (regno < 29))
2548 || ((regno > 29) && (regno < 32)));
2549 break;
2550 case 32:
2551 firq_auto_save_p &= (regno != 29) && (regno < 32);
2552 break;
2553 default:
2554 firq_auto_save_p = false;
2555 break;
2556 }
2557
2558 if ((regno) != RETURN_ADDR_REGNUM
2559 && (regno) != FRAME_POINTER_REGNUM
2560 && df_regs_ever_live_p (regno)
2561 && (!call_used_regs[regno]
2562 || ARC_INTERRUPT_P (fn_type))
2563 /* Do not emit code for auto saved regs. */
2564 && !irq_auto_save_p
2565 && !firq_auto_save_p)
2566 return true;
2567
2568 if (flag_pic && crtl->uses_pic_offset_table
2569 && regno == PIC_OFFSET_TABLE_REGNUM)
2570 return true;
2571
2572 return false;
2573 }
2574
2575 /* Return true if the return address must be saved in the current function,
2576 otherwise return false. */
2577
2578 static bool
2579 arc_must_save_return_addr (struct function *func)
2580 {
2581 if (func->machine->frame_info.save_return_addr)
2582 return true;
2583
2584 return false;
2585 }
2586
2587 /* Helper function to wrap FRAME_POINTER_NEEDED. We do this as
2588 FRAME_POINTER_NEEDED will not be true until the IRA (Integrated
2589 Register Allocator) pass, while we want to get the frame size
2590 correct earlier than the IRA pass. */
2591 static bool
2592 arc_frame_pointer_needed (void)
2593 {
2594 return (frame_pointer_needed);
2595 }
2596
2597
2598 /* Return non-zero if there are registers to be saved or loaded using
2599 millicode thunks. We can only use consecutive sequences starting
2600 with r13, and not going beyond r25.
2601 GMASK is a bitmask of registers to save. This function sets
2602 FRAME->millicod_start_reg .. FRAME->millicode_end_reg to the range
2603 of registers to be saved / restored with a millicode call. */
2604
2605 static int
2606 arc_compute_millicode_save_restore_regs (unsigned int gmask,
2607 struct arc_frame_info *frame)
2608 {
2609 int regno;
2610
2611 int start_reg = 13, end_reg = 25;
2612
2613 for (regno = start_reg; regno <= end_reg && (gmask & (1L << regno));)
2614 regno++;
2615 end_reg = regno - 1;
2616 /* There is no point in using millicode thunks if we don't save/restore
2617 at least three registers. For non-leaf functions we also have the
2618 blink restore. */
2619 if (regno - start_reg >= 3 - (crtl->is_leaf == 0))
2620 {
2621 frame->millicode_start_reg = 13;
2622 frame->millicode_end_reg = regno - 1;
2623 return 1;
2624 }
2625 return 0;
2626 }
2627
2628 /* Return the bytes needed to compute the frame pointer from the current
2629 stack pointer.
2630
2631 SIZE is the size needed for local variables. */
2632
2633 unsigned int
2634 arc_compute_frame_size (int size) /* size = # of var. bytes allocated. */
2635 {
2636 int regno;
2637 unsigned int total_size, var_size, args_size, pretend_size, extra_size;
2638 unsigned int reg_size, reg_offset;
2639 unsigned int gmask;
2640 struct arc_frame_info *frame_info = &cfun->machine->frame_info;
2641
2642 size = ARC_STACK_ALIGN (size);
2643
2644 /* 1) Size of locals and temporaries */
2645 var_size = size;
2646
2647 /* 2) Size of outgoing arguments */
2648 args_size = crtl->outgoing_args_size;
2649
2650 /* 3) Calculate space needed for saved registers.
2651 ??? We ignore the extension registers for now. */
2652
2653 /* See if this is an interrupt handler. Call used registers must be saved
2654 for them too. */
2655
2656 reg_size = 0;
2657 gmask = 0;
2658
2659 for (regno = 0; regno <= 31; regno++)
2660 {
2661 if (arc_must_save_register (regno, cfun))
2662 {
2663 reg_size += UNITS_PER_WORD;
2664 gmask |= 1L << regno;
2665 }
2666 }
2667
2668 /* 4) Space for back trace data structure.
2669 <return addr reg size> (if required) + <fp size> (if required). */
2670 frame_info->save_return_addr
2671 = (!crtl->is_leaf || df_regs_ever_live_p (RETURN_ADDR_REGNUM));
2672 /* Saving blink reg in case of leaf function for millicode thunk calls. */
2673 if (optimize_size && !TARGET_NO_MILLICODE_THUNK_SET)
2674 {
2675 if (arc_compute_millicode_save_restore_regs (gmask, frame_info))
2676 frame_info->save_return_addr = true;
2677 }
2678
2679 extra_size = 0;
2680 if (arc_must_save_return_addr (cfun))
2681 extra_size = 4;
2682 if (arc_frame_pointer_needed ())
2683 extra_size += 4;
2684
2685 /* 5) Space for variable arguments passed in registers */
2686 pretend_size = crtl->args.pretend_args_size;
2687
2688 /* Ensure everything before the locals is aligned appropriately. */
2689 {
2690 unsigned int extra_plus_reg_size;
2691 unsigned int extra_plus_reg_size_aligned;
2692
2693 extra_plus_reg_size = extra_size + reg_size;
2694 extra_plus_reg_size_aligned = ARC_STACK_ALIGN(extra_plus_reg_size);
2695 reg_size = extra_plus_reg_size_aligned - extra_size;
2696 }
2697
2698 /* Compute total frame size. */
2699 total_size = var_size + args_size + extra_size + pretend_size + reg_size;
2700
2701 total_size = ARC_STACK_ALIGN (total_size);
2702
2703 /* Compute offset of register save area from stack pointer:
2704 Frame: pretend_size <blink> reg_size <fp> var_size args_size <--sp
2705 */
2706 reg_offset = (total_size - (pretend_size + reg_size + extra_size)
2707 + (arc_frame_pointer_needed () ? 4 : 0));
2708
2709 /* Save computed information. */
2710 frame_info->total_size = total_size;
2711 frame_info->extra_size = extra_size;
2712 frame_info->pretend_size = pretend_size;
2713 frame_info->var_size = var_size;
2714 frame_info->args_size = args_size;
2715 frame_info->reg_size = reg_size;
2716 frame_info->reg_offset = reg_offset;
2717 frame_info->gmask = gmask;
2718 frame_info->initialized = reload_completed;
2719
2720 /* Ok, we're done. */
2721 return total_size;
2722 }
2723
2724 /* Common code to save/restore registers. */
2725 /* BASE_REG is the base register to use for addressing and to adjust.
2726 GMASK is a bitmask of general purpose registers to save/restore.
2727 epilogue_p 0: prologue 1:epilogue 2:epilogue, sibling thunk
2728 If *FIRST_OFFSET is non-zero, add it first to BASE_REG - preferably
2729 using a pre-modify for the first memory access. *FIRST_OFFSET is then
2730 zeroed. */
2731
2732 static void
2733 arc_save_restore (rtx base_reg,
2734 unsigned int gmask, int epilogue_p, int *first_offset)
2735 {
2736 unsigned int offset = 0;
2737 int regno;
2738 struct arc_frame_info *frame = &cfun->machine->frame_info;
2739 rtx sibthunk_insn = NULL_RTX;
2740
2741 if (gmask)
2742 {
2743 /* Millicode thunks implementation:
2744 Generates calls to millicodes for registers starting from r13 to r25
2745 Present Limitations:
2746 - Only one range supported. The remaining regs will have the ordinary
2747 st and ld instructions for store and loads. Hence a gmask asking
2748 to store r13-14, r16-r25 will only generate calls to store and
2749 load r13 to r14 while store and load insns will be generated for
2750 r16 to r25 in the prologue and epilogue respectively.
2751
2752 - Presently library only supports register ranges starting from r13.
2753 */
2754 if (epilogue_p == 2 || frame->millicode_end_reg > 14)
2755 {
2756 int start_call = frame->millicode_start_reg;
2757 int end_call = frame->millicode_end_reg;
2758 int n_regs = end_call - start_call + 1;
2759 int i = 0, r, off = 0;
2760 rtx insn;
2761 rtx ret_addr = gen_rtx_REG (Pmode, RETURN_ADDR_REGNUM);
2762
2763 if (*first_offset)
2764 {
2765 /* "reg_size" won't be more than 127 . */
2766 gcc_assert (epilogue_p || abs (*first_offset) <= 127);
2767 frame_add (base_reg, *first_offset);
2768 *first_offset = 0;
2769 }
2770 insn = gen_rtx_PARALLEL
2771 (VOIDmode, rtvec_alloc ((epilogue_p == 2) + n_regs + 1));
2772 if (epilogue_p == 2)
2773 i += 2;
2774 else
2775 XVECEXP (insn, 0, n_regs) = gen_rtx_CLOBBER (VOIDmode, ret_addr);
2776 for (r = start_call; r <= end_call; r++, off += UNITS_PER_WORD, i++)
2777 {
2778 rtx reg = gen_rtx_REG (SImode, r);
2779 rtx mem
2780 = gen_frame_mem (SImode, plus_constant (Pmode, base_reg, off));
2781
2782 if (epilogue_p)
2783 XVECEXP (insn, 0, i) = gen_rtx_SET (reg, mem);
2784 else
2785 XVECEXP (insn, 0, i) = gen_rtx_SET (mem, reg);
2786 gmask = gmask & ~(1L << r);
2787 }
2788 if (epilogue_p == 2)
2789 sibthunk_insn = insn;
2790 else
2791 {
2792 insn = frame_insn (insn);
2793 if (epilogue_p)
2794 for (r = start_call; r <= end_call; r++)
2795 {
2796 rtx reg = gen_rtx_REG (SImode, r);
2797 add_reg_note (insn, REG_CFA_RESTORE, reg);
2798 }
2799 }
2800 offset += off;
2801 }
2802
2803 for (regno = 0; regno <= 31; regno++)
2804 {
2805 machine_mode mode = SImode;
2806 bool found = false;
2807
2808 if (TARGET_LL64
2809 && (regno % 2 == 0)
2810 && ((gmask & (1L << regno)) != 0)
2811 && ((gmask & (1L << (regno+1))) != 0))
2812 {
2813 found = true;
2814 mode = DImode;
2815 }
2816 else if ((gmask & (1L << regno)) != 0)
2817 {
2818 found = true;
2819 mode = SImode;
2820 }
2821
2822 if (found)
2823 {
2824 rtx reg = gen_rtx_REG (mode, regno);
2825 rtx addr, mem;
2826 int cfa_adjust = *first_offset;
2827
2828 if (*first_offset)
2829 {
2830 gcc_assert (!offset);
2831 addr = plus_constant (Pmode, base_reg, *first_offset);
2832 addr = gen_rtx_PRE_MODIFY (Pmode, base_reg, addr);
2833 *first_offset = 0;
2834 }
2835 else
2836 {
2837 gcc_assert (SMALL_INT (offset));
2838 addr = plus_constant (Pmode, base_reg, offset);
2839 }
2840 mem = gen_frame_mem (mode, addr);
2841 if (epilogue_p)
2842 {
2843 rtx insn =
2844 frame_move_inc (reg, mem, base_reg, addr);
2845 add_reg_note (insn, REG_CFA_RESTORE, reg);
2846 if (cfa_adjust)
2847 {
2848 enum reg_note note = REG_CFA_ADJUST_CFA;
2849 add_reg_note (insn, note,
2850 gen_rtx_SET (stack_pointer_rtx,
2851 plus_constant (Pmode,
2852 stack_pointer_rtx,
2853 cfa_adjust)));
2854 }
2855 }
2856 else
2857 frame_move_inc (mem, reg, base_reg, addr);
2858 offset += UNITS_PER_WORD;
2859 if (mode == DImode)
2860 {
2861 offset += UNITS_PER_WORD;
2862 ++regno;
2863 }
2864 } /* if */
2865 } /* for */
2866 }/* if */
2867 if (sibthunk_insn)
2868 {
2869 int start_call = frame->millicode_start_reg;
2870 int end_call = frame->millicode_end_reg;
2871 int r;
2872
2873 rtx r12 = gen_rtx_REG (Pmode, 12);
2874
2875 frame_insn (gen_rtx_SET (r12, GEN_INT (offset)));
2876 XVECEXP (sibthunk_insn, 0, 0) = ret_rtx;
2877 XVECEXP (sibthunk_insn, 0, 1)
2878 = gen_rtx_SET (stack_pointer_rtx,
2879 gen_rtx_PLUS (Pmode, stack_pointer_rtx, r12));
2880 sibthunk_insn = emit_jump_insn (sibthunk_insn);
2881 RTX_FRAME_RELATED_P (sibthunk_insn) = 1;
2882
2883 /* Would be nice if we could do this earlier, when the PARALLEL
2884 is populated, but these need to be attached after the
2885 emit. */
2886 for (r = start_call; r <= end_call; r++)
2887 {
2888 rtx reg = gen_rtx_REG (SImode, r);
2889 add_reg_note (sibthunk_insn, REG_CFA_RESTORE, reg);
2890 }
2891 }
2892 } /* arc_save_restore */
2893
2894 /* Build dwarf information when the context is saved via AUX_IRQ_CTRL
2895 mechanism. */
2896
2897 static void
2898 arc_dwarf_emit_irq_save_regs (void)
2899 {
2900 rtx tmp, par, insn, reg;
2901 int i, offset, j;
2902
2903 par = gen_rtx_SEQUENCE (VOIDmode,
2904 rtvec_alloc (irq_ctrl_saved.irq_save_last_reg + 1
2905 + irq_ctrl_saved.irq_save_blink
2906 + irq_ctrl_saved.irq_save_lpcount
2907 + 1));
2908
2909 /* Build the stack adjustment note for unwind info. */
2910 j = 0;
2911 offset = UNITS_PER_WORD * (irq_ctrl_saved.irq_save_last_reg + 1
2912 + irq_ctrl_saved.irq_save_blink
2913 + irq_ctrl_saved.irq_save_lpcount);
2914 tmp = plus_constant (Pmode, stack_pointer_rtx, -1 * offset);
2915 tmp = gen_rtx_SET (stack_pointer_rtx, tmp);
2916 RTX_FRAME_RELATED_P (tmp) = 1;
2917 XVECEXP (par, 0, j++) = tmp;
2918
2919 offset -= UNITS_PER_WORD;
2920
2921 /* 1st goes LP_COUNT. */
2922 if (irq_ctrl_saved.irq_save_lpcount)
2923 {
2924 reg = gen_rtx_REG (SImode, 60);
2925 tmp = plus_constant (Pmode, stack_pointer_rtx, offset);
2926 tmp = gen_frame_mem (SImode, tmp);
2927 tmp = gen_rtx_SET (tmp, reg);
2928 RTX_FRAME_RELATED_P (tmp) = 1;
2929 XVECEXP (par, 0, j++) = tmp;
2930 offset -= UNITS_PER_WORD;
2931 }
2932
2933 /* 2nd goes BLINK. */
2934 if (irq_ctrl_saved.irq_save_blink)
2935 {
2936 reg = gen_rtx_REG (SImode, 31);
2937 tmp = plus_constant (Pmode, stack_pointer_rtx, offset);
2938 tmp = gen_frame_mem (SImode, tmp);
2939 tmp = gen_rtx_SET (tmp, reg);
2940 RTX_FRAME_RELATED_P (tmp) = 1;
2941 XVECEXP (par, 0, j++) = tmp;
2942 offset -= UNITS_PER_WORD;
2943 }
2944
2945 /* Build the parallel of the remaining registers recorded as saved
2946 for unwind. */
2947 for (i = irq_ctrl_saved.irq_save_last_reg; i >= 0; i--)
2948 {
2949 reg = gen_rtx_REG (SImode, i);
2950 tmp = plus_constant (Pmode, stack_pointer_rtx, offset);
2951 tmp = gen_frame_mem (SImode, tmp);
2952 tmp = gen_rtx_SET (tmp, reg);
2953 RTX_FRAME_RELATED_P (tmp) = 1;
2954 XVECEXP (par, 0, j++) = tmp;
2955 offset -= UNITS_PER_WORD;
2956 }
2957
2958 /* Dummy insn used to anchor the dwarf info. */
2959 insn = emit_insn (gen_stack_irq_dwarf());
2960 add_reg_note (insn, REG_FRAME_RELATED_EXPR, par);
2961 RTX_FRAME_RELATED_P (insn) = 1;
2962 }
2963
2964 /* Set up the stack and frame pointer (if desired) for the function. */
2965
2966 void
2967 arc_expand_prologue (void)
2968 {
2969 int size = get_frame_size ();
2970 unsigned int gmask = cfun->machine->frame_info.gmask;
2971 /* unsigned int frame_pointer_offset;*/
2972 unsigned int frame_size_to_allocate;
2973 /* (FIXME: The first store will use a PRE_MODIFY; this will usually be r13.
2974 Change the stack layout so that we rather store a high register with the
2975 PRE_MODIFY, thus enabling more short insn generation.) */
2976 int first_offset = 0;
2977 unsigned int fn_type = arc_compute_function_type (cfun);
2978
2979 /* Naked functions don't have prologue. */
2980 if (ARC_NAKED_P (fn_type))
2981 return;
2982
2983 size = ARC_STACK_ALIGN (size);
2984
2985 /* Compute/get total frame size. */
2986 size = (!cfun->machine->frame_info.initialized
2987 ? arc_compute_frame_size (size)
2988 : cfun->machine->frame_info.total_size);
2989
2990 if (flag_stack_usage_info)
2991 current_function_static_stack_size = size;
2992
2993 /* Keep track of frame size to be allocated. */
2994 frame_size_to_allocate = size;
2995
2996 /* These cases shouldn't happen. Catch them now. */
2997 gcc_assert (!(size == 0 && gmask));
2998
2999 /* Allocate space for register arguments if this is a variadic function. */
3000 if (cfun->machine->frame_info.pretend_size != 0)
3001 {
3002 /* Ensure pretend_size is maximum of 8 * word_size. */
3003 gcc_assert (cfun->machine->frame_info.pretend_size <= 32);
3004
3005 frame_stack_add (-(HOST_WIDE_INT)cfun->machine->frame_info.pretend_size);
3006 frame_size_to_allocate -= cfun->machine->frame_info.pretend_size;
3007 }
3008
3009 /* IRQ using automatic save mechanism will save the register before
3010 anything we do. */
3011 if (ARC_AUTO_IRQ_P (fn_type)
3012 && !ARC_FAST_INTERRUPT_P (fn_type))
3013 {
3014 arc_dwarf_emit_irq_save_regs ();
3015 }
3016
3017 /* The home-grown ABI says link register is saved first. */
3018 if (arc_must_save_return_addr (cfun)
3019 && !ARC_AUTOBLINK_IRQ_P (fn_type))
3020 {
3021 rtx ra = gen_rtx_REG (SImode, RETURN_ADDR_REGNUM);
3022 rtx mem = gen_frame_mem (Pmode,
3023 gen_rtx_PRE_DEC (Pmode,
3024 stack_pointer_rtx));
3025
3026 frame_move_inc (mem, ra, stack_pointer_rtx, 0);
3027 frame_size_to_allocate -= UNITS_PER_WORD;
3028 }
3029
3030 /* Save any needed call-saved regs (and call-used if this is an
3031 interrupt handler) for ARCompact ISA. */
3032 if (cfun->machine->frame_info.reg_size)
3033 {
3034 first_offset = -cfun->machine->frame_info.reg_size;
3035 /* N.B. FRAME_POINTER_MASK and RETURN_ADDR_MASK are cleared in gmask. */
3036 arc_save_restore (stack_pointer_rtx, gmask, 0, &first_offset);
3037 frame_size_to_allocate -= cfun->machine->frame_info.reg_size;
3038 }
3039
3040 /* Save frame pointer if needed. First save the FP on stack, if not
3041 autosaved. */
3042 if (arc_frame_pointer_needed ()
3043 && !ARC_AUTOFP_IRQ_P (fn_type))
3044 {
3045 rtx addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
3046 GEN_INT (-UNITS_PER_WORD + first_offset));
3047 rtx mem = gen_frame_mem (Pmode, gen_rtx_PRE_MODIFY (Pmode,
3048 stack_pointer_rtx,
3049 addr));
3050 frame_move_inc (mem, frame_pointer_rtx, stack_pointer_rtx, 0);
3051 frame_size_to_allocate -= UNITS_PER_WORD;
3052 first_offset = 0;
3053 }
3054
3055 /* Emit mov fp,sp. */
3056 if (arc_frame_pointer_needed ())
3057 {
3058 frame_move (frame_pointer_rtx, stack_pointer_rtx);
3059 }
3060
3061 /* ??? We don't handle the case where the saved regs are more than 252
3062 bytes away from sp. This can be handled by decrementing sp once, saving
3063 the regs, and then decrementing it again. The epilogue doesn't have this
3064 problem as the `ld' insn takes reg+limm values (though it would be more
3065 efficient to avoid reg+limm). */
3066
3067 frame_size_to_allocate -= first_offset;
3068 /* Allocate the stack frame. */
3069 if (frame_size_to_allocate > 0)
3070 {
3071 frame_stack_add ((HOST_WIDE_INT) 0 - frame_size_to_allocate);
3072 /* If the frame pointer is needed, emit a special barrier that
3073 will prevent the scheduler from moving stores to the frame
3074 before the stack adjustment. */
3075 if (arc_frame_pointer_needed ())
3076 emit_insn (gen_stack_tie (stack_pointer_rtx,
3077 hard_frame_pointer_rtx));
3078 }
3079
3080 /* Setup the gp register, if needed. */
3081 if (crtl->uses_pic_offset_table)
3082 arc_finalize_pic ();
3083 }
3084
3085 /* Do any necessary cleanup after a function to restore stack, frame,
3086 and regs. */
3087
3088 void
3089 arc_expand_epilogue (int sibcall_p)
3090 {
3091 int size = get_frame_size ();
3092 unsigned int fn_type = arc_compute_function_type (cfun);
3093
3094 size = ARC_STACK_ALIGN (size);
3095 size = (!cfun->machine->frame_info.initialized
3096 ? arc_compute_frame_size (size)
3097 : cfun->machine->frame_info.total_size);
3098
3099 unsigned int pretend_size = cfun->machine->frame_info.pretend_size;
3100 unsigned int frame_size;
3101 unsigned int size_to_deallocate;
3102 int restored;
3103 int can_trust_sp_p = !cfun->calls_alloca;
3104 int first_offset = 0;
3105 int millicode_p = cfun->machine->frame_info.millicode_end_reg > 0;
3106 rtx insn;
3107
3108 /* Naked functions don't have epilogue. */
3109 if (ARC_NAKED_P (fn_type))
3110 return;
3111
3112 size_to_deallocate = size;
3113
3114 frame_size = size - (pretend_size +
3115 cfun->machine->frame_info.reg_size +
3116 cfun->machine->frame_info.extra_size);
3117
3118 /* ??? There are lots of optimizations that can be done here.
3119 EG: Use fp to restore regs if it's closer.
3120 Maybe in time we'll do them all. For now, always restore regs from
3121 sp, but don't restore sp if we don't have to. */
3122
3123 if (!can_trust_sp_p)
3124 gcc_assert (arc_frame_pointer_needed ());
3125
3126 /* Restore stack pointer to the beginning of saved register area for
3127 ARCompact ISA. */
3128 if (frame_size)
3129 {
3130 if (arc_frame_pointer_needed ())
3131 frame_move (stack_pointer_rtx, frame_pointer_rtx);
3132 else
3133 first_offset = frame_size;
3134 size_to_deallocate -= frame_size;
3135 }
3136 else if (!can_trust_sp_p)
3137 frame_stack_add (-frame_size);
3138
3139
3140 /* Restore any saved registers. */
3141 if (arc_frame_pointer_needed ()
3142 && !ARC_AUTOFP_IRQ_P (fn_type))
3143 {
3144 rtx addr = gen_rtx_POST_INC (Pmode, stack_pointer_rtx);
3145
3146 insn = frame_move_inc (frame_pointer_rtx, gen_frame_mem (Pmode, addr),
3147 stack_pointer_rtx, 0);
3148 add_reg_note (insn, REG_CFA_RESTORE, frame_pointer_rtx);
3149 add_reg_note (insn, REG_CFA_DEF_CFA,
3150 plus_constant (SImode, stack_pointer_rtx,
3151 4));
3152 size_to_deallocate -= UNITS_PER_WORD;
3153 }
3154
3155 /* Load blink after the calls to thunk calls in case of optimize size. */
3156 if (millicode_p)
3157 {
3158 int sibthunk_p = (!sibcall_p
3159 && fn_type == ARC_FUNCTION_NORMAL
3160 && !cfun->machine->frame_info.pretend_size);
3161
3162 gcc_assert (!(cfun->machine->frame_info.gmask
3163 & (FRAME_POINTER_MASK | RETURN_ADDR_MASK)));
3164 arc_save_restore (stack_pointer_rtx,
3165 cfun->machine->frame_info.gmask,
3166 1 + sibthunk_p, &first_offset);
3167 if (sibthunk_p)
3168 return;
3169 }
3170 /* If we are to restore registers, and first_offset would require
3171 a limm to be encoded in a PRE_MODIFY, yet we can add it with a
3172 fast add to the stack pointer, do this now. */
3173 if ((!SMALL_INT (first_offset)
3174 && cfun->machine->frame_info.gmask
3175 && ((TARGET_ARC700 && !optimize_size)
3176 ? first_offset <= 0x800
3177 : satisfies_constraint_C2a (GEN_INT (first_offset))))
3178 /* Also do this if we have both gprs and return
3179 address to restore, and they both would need a LIMM. */
3180 || (arc_must_save_return_addr (cfun)
3181 && !SMALL_INT ((cfun->machine->frame_info.reg_size + first_offset) >> 2)
3182 && cfun->machine->frame_info.gmask))
3183 {
3184 frame_stack_add (first_offset);
3185 first_offset = 0;
3186 }
3187 if (arc_must_save_return_addr (cfun)
3188 && !ARC_AUTOBLINK_IRQ_P (fn_type))
3189 {
3190 rtx ra = gen_rtx_REG (Pmode, RETURN_ADDR_REGNUM);
3191 int ra_offs = cfun->machine->frame_info.reg_size + first_offset;
3192 rtx addr = plus_constant (Pmode, stack_pointer_rtx, ra_offs);
3193 HOST_WIDE_INT cfa_adjust = 0;
3194
3195 /* If the load of blink would need a LIMM, but we can add
3196 the offset quickly to sp, do the latter. */
3197 if (!SMALL_INT (ra_offs >> 2)
3198 && !cfun->machine->frame_info.gmask
3199 && ((TARGET_ARC700 && !optimize_size)
3200 ? ra_offs <= 0x800
3201 : satisfies_constraint_C2a (GEN_INT (ra_offs))))
3202 {
3203 size_to_deallocate -= ra_offs - first_offset;
3204 first_offset = 0;
3205 frame_stack_add (ra_offs);
3206 ra_offs = 0;
3207 addr = stack_pointer_rtx;
3208 }
3209 /* See if we can combine the load of the return address with the
3210 final stack adjustment.
3211 We need a separate load if there are still registers to
3212 restore. We also want a separate load if the combined insn
3213 would need a limm, but a separate load doesn't. */
3214 if (ra_offs
3215 && !cfun->machine->frame_info.gmask
3216 && (SMALL_INT (ra_offs) || !SMALL_INT (ra_offs >> 2)))
3217 {
3218 addr = gen_rtx_PRE_MODIFY (Pmode, stack_pointer_rtx, addr);
3219 cfa_adjust = ra_offs;
3220 first_offset = 0;
3221 size_to_deallocate -= cfun->machine->frame_info.reg_size;
3222 }
3223 else if (!ra_offs && size_to_deallocate == UNITS_PER_WORD)
3224 {
3225 addr = gen_rtx_POST_INC (Pmode, addr);
3226 cfa_adjust = GET_MODE_SIZE (Pmode);
3227 size_to_deallocate = 0;
3228 }
3229
3230 insn = frame_move_inc (ra, gen_frame_mem (Pmode, addr),
3231 stack_pointer_rtx, addr);
3232 if (cfa_adjust)
3233 {
3234 enum reg_note note = REG_CFA_ADJUST_CFA;
3235
3236 add_reg_note (insn, note,
3237 gen_rtx_SET (stack_pointer_rtx,
3238 plus_constant (SImode, stack_pointer_rtx,
3239 cfa_adjust)));
3240 }
3241 add_reg_note (insn, REG_CFA_RESTORE, ra);
3242 }
3243
3244 if (!millicode_p)
3245 {
3246 if (cfun->machine->frame_info.reg_size)
3247 arc_save_restore (stack_pointer_rtx,
3248 /* The zeroing of these two bits is unnecessary, but leave this in for clarity. */
3249 cfun->machine->frame_info.gmask
3250 & ~(FRAME_POINTER_MASK | RETURN_ADDR_MASK), 1, &first_offset);
3251 }
3252
3253 /* The rest of this function does the following:
3254 ARCompact : handle epilogue_delay, restore sp (phase-2), return
3255 */
3256
3257 /* Keep track of how much of the stack pointer we've restored.
3258 It makes the following a lot more readable. */
3259 size_to_deallocate += first_offset;
3260 restored = size - size_to_deallocate;
3261
3262 if (size > restored)
3263 frame_stack_add (size - restored);
3264
3265 /* Emit the return instruction. */
3266 if (sibcall_p == FALSE)
3267 emit_jump_insn (gen_simple_return ());
3268 }
3269
3270 /* Return the offset relative to the stack pointer where the return address
3271 is stored, or -1 if it is not stored. */
3272
3273 int
3274 arc_return_slot_offset ()
3275 {
3276 struct arc_frame_info *afi = &cfun->machine->frame_info;
3277
3278 return (afi->save_return_addr
3279 ? afi->total_size - afi->pretend_size - afi->extra_size : -1);
3280 }
3281
3282 /* PIC */
3283
3284 /* Helper to generate unspec constant. */
3285
3286 static rtx
3287 arc_unspec_offset (rtx loc, int unspec)
3288 {
3289 return gen_rtx_CONST (Pmode, gen_rtx_UNSPEC (Pmode, gen_rtvec (1, loc),
3290 unspec));
3291 }
3292
3293 /* Emit special PIC prologues and epilogues. */
3294 /* If the function has any GOTOFF relocations, then the GOTBASE
3295 register has to be setup in the prologue
3296 The instruction needed at the function start for setting up the
3297 GOTBASE register is
3298 add rdest, pc,
3299 ----------------------------------------------------------
3300 The rtl to be emitted for this should be:
3301 set (reg basereg)
3302 (plus (reg pc)
3303 (const (unspec (symref _DYNAMIC) 3)))
3304 ---------------------------------------------------------- */
3305
3306 static void
3307 arc_finalize_pic (void)
3308 {
3309 rtx pat;
3310 rtx baseptr_rtx = gen_rtx_REG (Pmode, PIC_OFFSET_TABLE_REGNUM);
3311
3312 if (crtl->uses_pic_offset_table == 0)
3313 return;
3314
3315 gcc_assert (flag_pic != 0);
3316
3317 pat = gen_rtx_SYMBOL_REF (Pmode, "_DYNAMIC");
3318 pat = arc_unspec_offset (pat, ARC_UNSPEC_GOT);
3319 pat = gen_rtx_SET (baseptr_rtx, pat);
3320
3321 emit_insn (pat);
3322 }
3323 \f
3324 /* !TARGET_BARREL_SHIFTER support. */
3325 /* Emit a shift insn to set OP0 to OP1 shifted by OP2; CODE specifies what
3326 kind of shift. */
3327
3328 void
3329 emit_shift (enum rtx_code code, rtx op0, rtx op1, rtx op2)
3330 {
3331 rtx shift = gen_rtx_fmt_ee (code, SImode, op1, op2);
3332 rtx pat
3333 = ((shift4_operator (shift, SImode) ? gen_shift_si3 : gen_shift_si3_loop)
3334 (op0, op1, op2, shift));
3335 emit_insn (pat);
3336 }
3337
3338 /* Output the assembler code for doing a shift.
3339 We go to a bit of trouble to generate efficient code as the ARC601 only has
3340 single bit shifts. This is taken from the h8300 port. We only have one
3341 mode of shifting and can't access individual bytes like the h8300 can, so
3342 this is greatly simplified (at the expense of not generating hyper-
3343 efficient code).
3344
3345 This function is not used if the variable shift insns are present. */
3346
3347 /* FIXME: This probably can be done using a define_split in arc.md.
3348 Alternately, generate rtx rather than output instructions. */
3349
3350 const char *
3351 output_shift (rtx *operands)
3352 {
3353 /* static int loopend_lab;*/
3354 rtx shift = operands[3];
3355 machine_mode mode = GET_MODE (shift);
3356 enum rtx_code code = GET_CODE (shift);
3357 const char *shift_one;
3358
3359 gcc_assert (mode == SImode);
3360
3361 switch (code)
3362 {
3363 case ASHIFT: shift_one = "add %0,%1,%1"; break;
3364 case ASHIFTRT: shift_one = "asr %0,%1"; break;
3365 case LSHIFTRT: shift_one = "lsr %0,%1"; break;
3366 default: gcc_unreachable ();
3367 }
3368
3369 if (GET_CODE (operands[2]) != CONST_INT)
3370 {
3371 output_asm_insn ("and.f lp_count,%2, 0x1f", operands);
3372 goto shiftloop;
3373 }
3374 else
3375 {
3376 int n;
3377
3378 n = INTVAL (operands[2]);
3379
3380 /* Only consider the lower 5 bits of the shift count. */
3381 n = n & 0x1f;
3382
3383 /* First see if we can do them inline. */
3384 /* ??? We could get better scheduling & shorter code (using short insns)
3385 by using splitters. Alas, that'd be even more verbose. */
3386 if (code == ASHIFT && n <= 9 && n > 2
3387 && dest_reg_operand (operands[4], SImode))
3388 {
3389 output_asm_insn ("mov %4,0\n\tadd3 %0,%4,%1", operands);
3390 for (n -=3 ; n >= 3; n -= 3)
3391 output_asm_insn ("add3 %0,%4,%0", operands);
3392 if (n == 2)
3393 output_asm_insn ("add2 %0,%4,%0", operands);
3394 else if (n)
3395 output_asm_insn ("add %0,%0,%0", operands);
3396 }
3397 else if (n <= 4)
3398 {
3399 while (--n >= 0)
3400 {
3401 output_asm_insn (shift_one, operands);
3402 operands[1] = operands[0];
3403 }
3404 }
3405 /* See if we can use a rotate/and. */
3406 else if (n == BITS_PER_WORD - 1)
3407 {
3408 switch (code)
3409 {
3410 case ASHIFT :
3411 output_asm_insn ("and %0,%1,1\n\tror %0,%0", operands);
3412 break;
3413 case ASHIFTRT :
3414 /* The ARC doesn't have a rol insn. Use something else. */
3415 output_asm_insn ("add.f 0,%1,%1\n\tsbc %0,%0,%0", operands);
3416 break;
3417 case LSHIFTRT :
3418 /* The ARC doesn't have a rol insn. Use something else. */
3419 output_asm_insn ("add.f 0,%1,%1\n\trlc %0,0", operands);
3420 break;
3421 default:
3422 break;
3423 }
3424 }
3425 else if (n == BITS_PER_WORD - 2 && dest_reg_operand (operands[4], SImode))
3426 {
3427 switch (code)
3428 {
3429 case ASHIFT :
3430 output_asm_insn ("and %0,%1,3\n\tror %0,%0\n\tror %0,%0", operands);
3431 break;
3432 case ASHIFTRT :
3433 #if 1 /* Need some scheduling comparisons. */
3434 output_asm_insn ("add.f %4,%1,%1\n\tsbc %0,%0,%0\n\t"
3435 "add.f 0,%4,%4\n\trlc %0,%0", operands);
3436 #else
3437 output_asm_insn ("add.f %4,%1,%1\n\tbxor %0,%4,31\n\t"
3438 "sbc.f %0,%0,%4\n\trlc %0,%0", operands);
3439 #endif
3440 break;
3441 case LSHIFTRT :
3442 #if 1
3443 output_asm_insn ("add.f %4,%1,%1\n\trlc %0,0\n\t"
3444 "add.f 0,%4,%4\n\trlc %0,%0", operands);
3445 #else
3446 output_asm_insn ("add.f %0,%1,%1\n\trlc.f %0,0\n\t"
3447 "and %0,%0,1\n\trlc %0,%0", operands);
3448 #endif
3449 break;
3450 default:
3451 break;
3452 }
3453 }
3454 else if (n == BITS_PER_WORD - 3 && code == ASHIFT)
3455 output_asm_insn ("and %0,%1,7\n\tror %0,%0\n\tror %0,%0\n\tror %0,%0",
3456 operands);
3457 /* Must loop. */
3458 else
3459 {
3460 operands[2] = GEN_INT (n);
3461 output_asm_insn ("mov.f lp_count, %2", operands);
3462
3463 shiftloop:
3464 {
3465 output_asm_insn ("lpnz\t2f", operands);
3466 output_asm_insn (shift_one, operands);
3467 output_asm_insn ("nop", operands);
3468 fprintf (asm_out_file, "2:\t%s end single insn loop\n",
3469 ASM_COMMENT_START);
3470 }
3471 }
3472 }
3473
3474 return "";
3475 }
3476 \f
3477 /* Nested function support. */
3478
3479 /* Directly store VALUE into memory object BLOCK at OFFSET. */
3480
3481 static void
3482 emit_store_direct (rtx block, int offset, int value)
3483 {
3484 emit_insn (gen_store_direct (adjust_address (block, SImode, offset),
3485 force_reg (SImode,
3486 gen_int_mode (value, SImode))));
3487 }
3488
3489 /* Emit RTL insns to initialize the variable parts of a trampoline.
3490 FNADDR is an RTX for the address of the function's pure code.
3491 CXT is an RTX for the static chain value for the function. */
3492 /* With potentially multiple shared objects loaded, and multiple stacks
3493 present for multiple thereds where trampolines might reside, a simple
3494 range check will likely not suffice for the profiler to tell if a callee
3495 is a trampoline. We a speedier check by making the trampoline start at
3496 an address that is not 4-byte aligned.
3497 A trampoline looks like this:
3498
3499 nop_s 0x78e0
3500 entry:
3501 ld_s r12,[pcl,12] 0xd403
3502 ld r11,[pcl,12] 0x170c 700b
3503 j_s [r12] 0x7c00
3504 nop_s 0x78e0
3505
3506 The fastest trampoline to execute for trampolines within +-8KB of CTX
3507 would be:
3508 add2 r11,pcl,s12
3509 j [limm] 0x20200f80 limm
3510 and that would also be faster to write to the stack by computing the offset
3511 from CTX to TRAMP at compile time. However, it would really be better to
3512 get rid of the high cost of cache invalidation when generating trampolines,
3513 which requires that the code part of trampolines stays constant, and
3514 additionally either
3515 - making sure that no executable code but trampolines is on the stack,
3516 no icache entries linger for the area of the stack from when before the
3517 stack was allocated, and allocating trampolines in trampoline-only
3518 cache lines
3519 or
3520 - allocate trampolines fram a special pool of pre-allocated trampolines. */
3521
3522 static void
3523 arc_initialize_trampoline (rtx tramp, tree fndecl, rtx cxt)
3524 {
3525 rtx fnaddr = XEXP (DECL_RTL (fndecl), 0);
3526
3527 emit_store_direct (tramp, 0, TARGET_BIG_ENDIAN ? 0x78e0d403 : 0xd40378e0);
3528 emit_store_direct (tramp, 4, TARGET_BIG_ENDIAN ? 0x170c700b : 0x700b170c);
3529 emit_store_direct (tramp, 8, TARGET_BIG_ENDIAN ? 0x7c0078e0 : 0x78e07c00);
3530 emit_move_insn (adjust_address (tramp, SImode, 12), fnaddr);
3531 emit_move_insn (adjust_address (tramp, SImode, 16), cxt);
3532 emit_insn (gen_flush_icache (adjust_address (tramp, SImode, 0)));
3533 }
3534
3535 /* Allow the profiler to easily distinguish trampolines from normal
3536 functions. */
3537
3538 static rtx
3539 arc_trampoline_adjust_address (rtx addr)
3540 {
3541 return plus_constant (Pmode, addr, 2);
3542 }
3543
3544 /* This is set briefly to 1 when we output a ".as" address modifer, and then
3545 reset when we output the scaled address. */
3546 static int output_scaled = 0;
3547
3548 /* Print operand X (an rtx) in assembler syntax to file FILE.
3549 CODE is a letter or dot (`z' in `%z0') or 0 if no letter was specified.
3550 For `%' followed by punctuation, CODE is the punctuation and X is null. */
3551 /* In final.c:output_asm_insn:
3552 'l' : label
3553 'a' : address
3554 'c' : constant address if CONSTANT_ADDRESS_P
3555 'n' : negative
3556 Here:
3557 'Z': log2(x+1)-1
3558 'z': log2
3559 'M': log2(~x)
3560 'p': bit Position of lsb
3561 's': size of bit field
3562 '#': condbranch delay slot suffix
3563 '*': jump delay slot suffix
3564 '?' : nonjump-insn suffix for conditional execution or short instruction
3565 '!' : jump / call suffix for conditional execution or short instruction
3566 '`': fold constant inside unary o-perator, re-recognize, and emit.
3567 'd'
3568 'D'
3569 'R': Second word
3570 'S'
3571 'B': Branch comparison operand - suppress sda reference
3572 'H': Most significant word
3573 'L': Least significant word
3574 'A': ASCII decimal representation of floating point value
3575 'U': Load/store update or scaling indicator
3576 'V': cache bypass indicator for volatile
3577 'P'
3578 'F'
3579 '^'
3580 'O': Operator
3581 'o': original symbol - no @ prepending. */
3582
3583 void
3584 arc_print_operand (FILE *file, rtx x, int code)
3585 {
3586 switch (code)
3587 {
3588 case 'Z':
3589 if (GET_CODE (x) == CONST_INT)
3590 fprintf (file, "%d",exact_log2(INTVAL (x) + 1) - 1 );
3591 else
3592 output_operand_lossage ("invalid operand to %%Z code");
3593
3594 return;
3595
3596 case 'z':
3597 if (GET_CODE (x) == CONST_INT)
3598 fprintf (file, "%d",exact_log2(INTVAL (x)) );
3599 else
3600 output_operand_lossage ("invalid operand to %%z code");
3601
3602 return;
3603
3604 case 'c':
3605 if (GET_CODE (x) == CONST_INT)
3606 fprintf (file, "%d", INTVAL (x) );
3607 else
3608 output_operand_lossage ("invalid operands to %%c code");
3609
3610 return;
3611
3612 case 'M':
3613 if (GET_CODE (x) == CONST_INT)
3614 fprintf (file, "%d",exact_log2(~INTVAL (x)) );
3615 else
3616 output_operand_lossage ("invalid operand to %%M code");
3617
3618 return;
3619
3620 case 'p':
3621 if (GET_CODE (x) == CONST_INT)
3622 fprintf (file, "%d", exact_log2 (INTVAL (x) & -INTVAL (x)));
3623 else
3624 output_operand_lossage ("invalid operand to %%p code");
3625 return;
3626
3627 case 's':
3628 if (GET_CODE (x) == CONST_INT)
3629 {
3630 HOST_WIDE_INT i = INTVAL (x);
3631 HOST_WIDE_INT s = exact_log2 (i & -i);
3632 fprintf (file, "%d", exact_log2 (((0xffffffffUL & i) >> s) + 1));
3633 }
3634 else
3635 output_operand_lossage ("invalid operand to %%s code");
3636 return;
3637
3638 case '#' :
3639 /* Conditional branches depending on condition codes.
3640 Note that this is only for branches that were known to depend on
3641 condition codes before delay slot scheduling;
3642 out-of-range brcc / bbit expansions should use '*'.
3643 This distinction is important because of the different
3644 allowable delay slot insns and the output of the delay suffix
3645 for TARGET_AT_DBR_COND_EXEC. */
3646 case '*' :
3647 /* Unconditional branches / branches not depending on condition codes.
3648 This could also be a CALL_INSN.
3649 Output the appropriate delay slot suffix. */
3650 if (final_sequence && final_sequence->len () != 1)
3651 {
3652 rtx_insn *jump = final_sequence->insn (0);
3653 rtx_insn *delay = final_sequence->insn (1);
3654
3655 /* For TARGET_PAD_RETURN we might have grabbed the delay insn. */
3656 if (delay->deleted ())
3657 return;
3658 if (JUMP_P (jump) && INSN_ANNULLED_BRANCH_P (jump))
3659 fputs (INSN_FROM_TARGET_P (delay) ? ".d"
3660 : TARGET_AT_DBR_CONDEXEC && code == '#' ? ".d"
3661 : get_attr_type (jump) == TYPE_RETURN && code == '#' ? ""
3662 : ".nd",
3663 file);
3664 else
3665 fputs (".d", file);
3666 }
3667 return;
3668 case '?' : /* with leading "." */
3669 case '!' : /* without leading "." */
3670 /* This insn can be conditionally executed. See if the ccfsm machinery
3671 says it should be conditionalized.
3672 If it shouldn't, we'll check the compact attribute if this insn
3673 has a short variant, which may be used depending on code size and
3674 alignment considerations. */
3675 if (current_insn_predicate)
3676 arc_ccfsm_current.cc
3677 = get_arc_condition_code (current_insn_predicate);
3678 if (ARC_CCFSM_COND_EXEC_P (&arc_ccfsm_current))
3679 {
3680 /* Is this insn in a delay slot sequence? */
3681 if (!final_sequence || XVECLEN (final_sequence, 0) < 2
3682 || current_insn_predicate
3683 || CALL_P (final_sequence->insn (0))
3684 || simplejump_p (final_sequence->insn (0)))
3685 {
3686 /* This insn isn't in a delay slot sequence, or conditionalized
3687 independently of its position in a delay slot. */
3688 fprintf (file, "%s%s",
3689 code == '?' ? "." : "",
3690 arc_condition_codes[arc_ccfsm_current.cc]);
3691 /* If this is a jump, there are still short variants. However,
3692 only beq_s / bne_s have the same offset range as b_s,
3693 and the only short conditional returns are jeq_s and jne_s. */
3694 if (code == '!'
3695 && (arc_ccfsm_current.cc == ARC_CC_EQ
3696 || arc_ccfsm_current.cc == ARC_CC_NE
3697 || 0 /* FIXME: check if branch in 7 bit range. */))
3698 output_short_suffix (file);
3699 }
3700 else if (code == '!') /* Jump with delay slot. */
3701 fputs (arc_condition_codes[arc_ccfsm_current.cc], file);
3702 else /* An Instruction in a delay slot of a jump or call. */
3703 {
3704 rtx jump = XVECEXP (final_sequence, 0, 0);
3705 rtx insn = XVECEXP (final_sequence, 0, 1);
3706
3707 /* If the insn is annulled and is from the target path, we need
3708 to inverse the condition test. */
3709 if (JUMP_P (jump) && INSN_ANNULLED_BRANCH_P (jump))
3710 {
3711 if (INSN_FROM_TARGET_P (insn))
3712 fprintf (file, "%s%s",
3713 code == '?' ? "." : "",
3714 arc_condition_codes[ARC_INVERSE_CONDITION_CODE (arc_ccfsm_current.cc)]);
3715 else
3716 fprintf (file, "%s%s",
3717 code == '?' ? "." : "",
3718 arc_condition_codes[arc_ccfsm_current.cc]);
3719 if (arc_ccfsm_current.state == 5)
3720 arc_ccfsm_current.state = 0;
3721 }
3722 else
3723 /* This insn is executed for either path, so don't
3724 conditionalize it at all. */
3725 output_short_suffix (file);
3726
3727 }
3728 }
3729 else
3730 output_short_suffix (file);
3731 return;
3732 case'`':
3733 /* FIXME: fold constant inside unary operator, re-recognize, and emit. */
3734 gcc_unreachable ();
3735 case 'd' :
3736 fputs (arc_condition_codes[get_arc_condition_code (x)], file);
3737 return;
3738 case 'D' :
3739 fputs (arc_condition_codes[ARC_INVERSE_CONDITION_CODE
3740 (get_arc_condition_code (x))],
3741 file);
3742 return;
3743 case 'R' :
3744 /* Write second word of DImode or DFmode reference,
3745 register or memory. */
3746 if (GET_CODE (x) == REG)
3747 fputs (reg_names[REGNO (x)+1], file);
3748 else if (GET_CODE (x) == MEM)
3749 {
3750 fputc ('[', file);
3751
3752 /* Handle possible auto-increment. For PRE_INC / PRE_DEC /
3753 PRE_MODIFY, we will have handled the first word already;
3754 For POST_INC / POST_DEC / POST_MODIFY, the access to the
3755 first word will be done later. In either case, the access
3756 to the first word will do the modify, and we only have
3757 to add an offset of four here. */
3758 if (GET_CODE (XEXP (x, 0)) == PRE_INC
3759 || GET_CODE (XEXP (x, 0)) == PRE_DEC
3760 || GET_CODE (XEXP (x, 0)) == PRE_MODIFY
3761 || GET_CODE (XEXP (x, 0)) == POST_INC
3762 || GET_CODE (XEXP (x, 0)) == POST_DEC
3763 || GET_CODE (XEXP (x, 0)) == POST_MODIFY)
3764 output_address (VOIDmode,
3765 plus_constant (Pmode, XEXP (XEXP (x, 0), 0), 4));
3766 else if (output_scaled)
3767 {
3768 rtx addr = XEXP (x, 0);
3769 int size = GET_MODE_SIZE (GET_MODE (x));
3770
3771 output_address (VOIDmode,
3772 plus_constant (Pmode, XEXP (addr, 0),
3773 ((INTVAL (XEXP (addr, 1)) + 4)
3774 >> (size == 2 ? 1 : 2))));
3775 output_scaled = 0;
3776 }
3777 else
3778 output_address (VOIDmode,
3779 plus_constant (Pmode, XEXP (x, 0), 4));
3780 fputc (']', file);
3781 }
3782 else
3783 output_operand_lossage ("invalid operand to %%R code");
3784 return;
3785 case 'S' :
3786 /* FIXME: remove %S option. */
3787 break;
3788 case 'B' /* Branch or other LIMM ref - must not use sda references. */ :
3789 if (CONSTANT_P (x))
3790 {
3791 output_addr_const (file, x);
3792 return;
3793 }
3794 break;
3795 case 'H' :
3796 case 'L' :
3797 if (GET_CODE (x) == REG)
3798 {
3799 /* L = least significant word, H = most significant word. */
3800 if ((WORDS_BIG_ENDIAN != 0) ^ (code == 'L'))
3801 fputs (reg_names[REGNO (x)], file);
3802 else
3803 fputs (reg_names[REGNO (x)+1], file);
3804 }
3805 else if (GET_CODE (x) == CONST_INT
3806 || GET_CODE (x) == CONST_DOUBLE)
3807 {
3808 rtx first, second, word;
3809
3810 split_double (x, &first, &second);
3811
3812 if((WORDS_BIG_ENDIAN) == 0)
3813 word = (code == 'L' ? first : second);
3814 else
3815 word = (code == 'L' ? second : first);
3816
3817 fprintf (file, "0x%08" PRIx32, ((uint32_t) INTVAL (word)));
3818 }
3819 else
3820 output_operand_lossage ("invalid operand to %%H/%%L code");
3821 return;
3822 case 'A' :
3823 {
3824 char str[30];
3825
3826 gcc_assert (GET_CODE (x) == CONST_DOUBLE
3827 && GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT);
3828
3829 real_to_decimal (str, CONST_DOUBLE_REAL_VALUE (x), sizeof (str), 0, 1);
3830 fprintf (file, "%s", str);
3831 return;
3832 }
3833 case 'U' :
3834 /* Output a load/store with update indicator if appropriate. */
3835 if (GET_CODE (x) == MEM)
3836 {
3837 rtx addr = XEXP (x, 0);
3838 switch (GET_CODE (addr))
3839 {
3840 case PRE_INC: case PRE_DEC: case PRE_MODIFY:
3841 fputs (".a", file); break;
3842 case POST_INC: case POST_DEC: case POST_MODIFY:
3843 fputs (".ab", file); break;
3844 case PLUS:
3845 /* Are we using a scaled index? */
3846 if (GET_CODE (XEXP (addr, 0)) == MULT)
3847 fputs (".as", file);
3848 /* Can we use a scaled offset? */
3849 else if (CONST_INT_P (XEXP (addr, 1))
3850 && GET_MODE_SIZE (GET_MODE (x)) > 1
3851 && (!(INTVAL (XEXP (addr, 1))
3852 & (GET_MODE_SIZE (GET_MODE (x)) - 1) & 3))
3853 /* Does it make a difference? */
3854 && !SMALL_INT_RANGE(INTVAL (XEXP (addr, 1)),
3855 GET_MODE_SIZE (GET_MODE (x)) - 2, 0))
3856 {
3857 fputs (".as", file);
3858 output_scaled = 1;
3859 }
3860 else if (LEGITIMATE_SMALL_DATA_ADDRESS_P (addr)
3861 && GET_MODE_SIZE (GET_MODE (x)) > 1)
3862 {
3863 tree decl = NULL_TREE;
3864 int align = 0;
3865 if (GET_CODE (XEXP (addr, 1)) == SYMBOL_REF)
3866 decl = SYMBOL_REF_DECL (XEXP (addr, 1));
3867 else if (GET_CODE (XEXP (XEXP (XEXP (addr, 1), 0), 0))
3868 == SYMBOL_REF)
3869 decl = SYMBOL_REF_DECL (XEXP (XEXP (XEXP (addr, 1), 0), 0));
3870 if (decl)
3871 align = DECL_ALIGN (decl);
3872 align = align / BITS_PER_UNIT;
3873 if ((GET_MODE_SIZE (GET_MODE (x)) == 2)
3874 && align && ((align & 1) == 0))
3875 fputs (".as", file);
3876 if ((GET_MODE_SIZE (GET_MODE (x)) >= 4)
3877 && align && ((align & 3) == 0))
3878 fputs (".as", file);
3879 }
3880 break;
3881 case REG:
3882 break;
3883 default:
3884 gcc_assert (CONSTANT_P (addr)); break;
3885 }
3886 }
3887 else
3888 output_operand_lossage ("invalid operand to %%U code");
3889 return;
3890 case 'V' :
3891 /* Output cache bypass indicator for a load/store insn. Volatile memory
3892 refs are defined to use the cache bypass mechanism. */
3893 if (GET_CODE (x) == MEM)
3894 {
3895 if (MEM_VOLATILE_P (x) && !TARGET_VOLATILE_CACHE_SET )
3896 fputs (".di", file);
3897 }
3898 else
3899 output_operand_lossage ("invalid operand to %%V code");
3900 return;
3901 /* plt code. */
3902 case 'P':
3903 case 0 :
3904 /* Do nothing special. */
3905 break;
3906 case 'F':
3907 fputs (reg_names[REGNO (x)]+1, file);
3908 return;
3909 case '^':
3910 /* This punctuation character is needed because label references are
3911 printed in the output template using %l. This is a front end
3912 character, and when we want to emit a '@' before it, we have to use
3913 this '^'. */
3914
3915 fputc('@',file);
3916 return;
3917 case 'O':
3918 /* Output an operator. */
3919 switch (GET_CODE (x))
3920 {
3921 case PLUS: fputs ("add", file); return;
3922 case SS_PLUS: fputs ("adds", file); return;
3923 case AND: fputs ("and", file); return;
3924 case IOR: fputs ("or", file); return;
3925 case XOR: fputs ("xor", file); return;
3926 case MINUS: fputs ("sub", file); return;
3927 case SS_MINUS: fputs ("subs", file); return;
3928 case ASHIFT: fputs ("asl", file); return;
3929 case ASHIFTRT: fputs ("asr", file); return;
3930 case LSHIFTRT: fputs ("lsr", file); return;
3931 case ROTATERT: fputs ("ror", file); return;
3932 case MULT: fputs ("mpy", file); return;
3933 case ABS: fputs ("abs", file); return; /* Unconditional. */
3934 case NEG: fputs ("neg", file); return;
3935 case SS_NEG: fputs ("negs", file); return;
3936 case NOT: fputs ("not", file); return; /* Unconditional. */
3937 case ZERO_EXTEND:
3938 fputs ("ext", file); /* bmsk allows predication. */
3939 goto size_suffix;
3940 case SIGN_EXTEND: /* Unconditional. */
3941 fputs ("sex", file);
3942 size_suffix:
3943 switch (GET_MODE (XEXP (x, 0)))
3944 {
3945 case E_QImode: fputs ("b", file); return;
3946 case E_HImode: fputs ("w", file); return;
3947 default: break;
3948 }
3949 break;
3950 case SS_TRUNCATE:
3951 if (GET_MODE (x) != HImode)
3952 break;
3953 fputs ("sat16", file);
3954 default: break;
3955 }
3956 output_operand_lossage ("invalid operand to %%O code"); return;
3957 case 'o':
3958 if (GET_CODE (x) == SYMBOL_REF)
3959 {
3960 assemble_name (file, XSTR (x, 0));
3961 return;
3962 }
3963 break;
3964 case '&':
3965 if (TARGET_ANNOTATE_ALIGN && cfun->machine->size_reason)
3966 fprintf (file, "; unalign: %d", cfun->machine->unalign);
3967 return;
3968 case '+':
3969 if (TARGET_V2)
3970 fputs ("m", file);
3971 else
3972 fputs ("h", file);
3973 return;
3974 case '_':
3975 if (TARGET_V2)
3976 fputs ("h", file);
3977 else
3978 fputs ("w", file);
3979 return;
3980 default :
3981 /* Unknown flag. */
3982 output_operand_lossage ("invalid operand output code");
3983 }
3984
3985 switch (GET_CODE (x))
3986 {
3987 case REG :
3988 fputs (reg_names[REGNO (x)], file);
3989 break;
3990 case MEM :
3991 {
3992 rtx addr = XEXP (x, 0);
3993 int size = GET_MODE_SIZE (GET_MODE (x));
3994
3995 fputc ('[', file);
3996
3997 switch (GET_CODE (addr))
3998 {
3999 case PRE_INC: case POST_INC:
4000 output_address (VOIDmode,
4001 plus_constant (Pmode, XEXP (addr, 0), size)); break;
4002 case PRE_DEC: case POST_DEC:
4003 output_address (VOIDmode,
4004 plus_constant (Pmode, XEXP (addr, 0), -size));
4005 break;
4006 case PRE_MODIFY: case POST_MODIFY:
4007 output_address (VOIDmode, XEXP (addr, 1)); break;
4008 case PLUS:
4009 if (output_scaled)
4010 {
4011 output_address (VOIDmode,
4012 plus_constant (Pmode, XEXP (addr, 0),
4013 (INTVAL (XEXP (addr, 1))
4014 >> (size == 2 ? 1 : 2))));
4015 output_scaled = 0;
4016 }
4017 else
4018 output_address (VOIDmode, addr);
4019 break;
4020 default:
4021 if (flag_pic && CONSTANT_ADDRESS_P (addr))
4022 arc_output_pic_addr_const (file, addr, code);
4023 else
4024 output_address (VOIDmode, addr);
4025 break;
4026 }
4027 fputc (']', file);
4028 break;
4029 }
4030 case CONST_DOUBLE :
4031 /* We handle SFmode constants here as output_addr_const doesn't. */
4032 if (GET_MODE (x) == SFmode)
4033 {
4034 long l;
4035
4036 REAL_VALUE_TO_TARGET_SINGLE (*CONST_DOUBLE_REAL_VALUE (x), l);
4037 fprintf (file, "0x%08lx", l);
4038 break;
4039 }
4040 /* FALLTHRU */
4041 /* Let output_addr_const deal with it. */
4042 default :
4043 if (flag_pic
4044 || (GET_CODE (x) == CONST
4045 && GET_CODE (XEXP (x, 0)) == UNSPEC
4046 && (XINT (XEXP (x, 0), 1) == UNSPEC_TLS_OFF
4047 || XINT (XEXP (x, 0), 1) == UNSPEC_TLS_GD))
4048 || (GET_CODE (x) == CONST
4049 && GET_CODE (XEXP (x, 0)) == PLUS
4050 && GET_CODE (XEXP (XEXP (x, 0), 0)) == UNSPEC
4051 && (XINT (XEXP (XEXP (x, 0), 0), 1) == UNSPEC_TLS_OFF
4052 || XINT (XEXP (XEXP (x, 0), 0), 1) == UNSPEC_TLS_GD)))
4053 arc_output_pic_addr_const (file, x, code);
4054 else
4055 {
4056 /* FIXME: Dirty way to handle @var@sda+const. Shd be handled
4057 with asm_output_symbol_ref */
4058 if (GET_CODE (x) == CONST && GET_CODE (XEXP (x, 0)) == PLUS)
4059 {
4060 x = XEXP (x, 0);
4061 output_addr_const (file, XEXP (x, 0));
4062 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF && SYMBOL_REF_SMALL_P (XEXP (x, 0)))
4063 fprintf (file, "@sda");
4064
4065 if (GET_CODE (XEXP (x, 1)) != CONST_INT
4066 || INTVAL (XEXP (x, 1)) >= 0)
4067 fprintf (file, "+");
4068 output_addr_const (file, XEXP (x, 1));
4069 }
4070 else
4071 output_addr_const (file, x);
4072 }
4073 if (GET_CODE (x) == SYMBOL_REF && SYMBOL_REF_SMALL_P (x))
4074 fprintf (file, "@sda");
4075 break;
4076 }
4077 }
4078
4079 /* Print a memory address as an operand to reference that memory location. */
4080
4081 void
4082 arc_print_operand_address (FILE *file , rtx addr)
4083 {
4084 register rtx base, index = 0;
4085
4086 switch (GET_CODE (addr))
4087 {
4088 case REG :
4089 fputs (reg_names[REGNO (addr)], file);
4090 break;
4091 case SYMBOL_REF :
4092 output_addr_const (file, addr);
4093 if (SYMBOL_REF_SMALL_P (addr))
4094 fprintf (file, "@sda");
4095 break;
4096 case PLUS :
4097 if (GET_CODE (XEXP (addr, 0)) == MULT)
4098 index = XEXP (XEXP (addr, 0), 0), base = XEXP (addr, 1);
4099 else if (CONST_INT_P (XEXP (addr, 0)))
4100 index = XEXP (addr, 0), base = XEXP (addr, 1);
4101 else
4102 base = XEXP (addr, 0), index = XEXP (addr, 1);
4103
4104 gcc_assert (OBJECT_P (base));
4105 arc_print_operand_address (file, base);
4106 if (CONSTANT_P (base) && CONST_INT_P (index))
4107 fputc ('+', file);
4108 else
4109 fputc (',', file);
4110 gcc_assert (OBJECT_P (index));
4111 arc_print_operand_address (file, index);
4112 break;
4113 case CONST:
4114 {
4115 rtx c = XEXP (addr, 0);
4116
4117 if ((GET_CODE (c) == UNSPEC
4118 && (XINT (c, 1) == UNSPEC_TLS_OFF
4119 || XINT (c, 1) == UNSPEC_TLS_IE))
4120 || (GET_CODE (c) == PLUS
4121 && GET_CODE (XEXP (c, 0)) == UNSPEC
4122 && (XINT (XEXP (c, 0), 1) == UNSPEC_TLS_OFF
4123 || XINT (XEXP (c, 0), 1) == ARC_UNSPEC_GOTOFFPC)))
4124 {
4125 arc_output_pic_addr_const (file, c, 0);
4126 break;
4127 }
4128 gcc_assert (GET_CODE (c) == PLUS);
4129 gcc_assert (GET_CODE (XEXP (c, 0)) == SYMBOL_REF);
4130 gcc_assert (GET_CODE (XEXP (c, 1)) == CONST_INT);
4131
4132 output_address (VOIDmode, XEXP (addr, 0));
4133
4134 break;
4135 }
4136 case PRE_INC :
4137 case PRE_DEC :
4138 /* We shouldn't get here as we've lost the mode of the memory object
4139 (which says how much to inc/dec by. */
4140 gcc_unreachable ();
4141 break;
4142 default :
4143 if (flag_pic)
4144 arc_output_pic_addr_const (file, addr, 0);
4145 else
4146 output_addr_const (file, addr);
4147 break;
4148 }
4149 }
4150
4151 /* Conditional execution support.
4152
4153 This is based on the ARM port but for now is much simpler.
4154
4155 A finite state machine takes care of noticing whether or not instructions
4156 can be conditionally executed, and thus decrease execution time and code
4157 size by deleting branch instructions. The fsm is controlled by
4158 arc_ccfsm_advance (called by arc_final_prescan_insn), and controls the
4159 actions of PRINT_OPERAND. The patterns in the .md file for the branch
4160 insns also have a hand in this. */
4161 /* The way we leave dealing with non-anulled or annull-false delay slot
4162 insns to the consumer is awkward. */
4163
4164 /* The state of the fsm controlling condition codes are:
4165 0: normal, do nothing special
4166 1: don't output this insn
4167 2: don't output this insn
4168 3: make insns conditional
4169 4: make insns conditional
4170 5: make insn conditional (only for outputting anulled delay slot insns)
4171
4172 special value for cfun->machine->uid_ccfsm_state:
4173 6: return with but one insn before it since function start / call
4174
4175 State transitions (state->state by whom, under what condition):
4176 0 -> 1 arc_ccfsm_advance, if insn is a conditional branch skipping over
4177 some instructions.
4178 0 -> 2 arc_ccfsm_advance, if insn is a conditional branch followed
4179 by zero or more non-jump insns and an unconditional branch with
4180 the same target label as the condbranch.
4181 1 -> 3 branch patterns, after having not output the conditional branch
4182 2 -> 4 branch patterns, after having not output the conditional branch
4183 0 -> 5 branch patterns, for anulled delay slot insn.
4184 3 -> 0 ASM_OUTPUT_INTERNAL_LABEL, if the `target' label is reached
4185 (the target label has CODE_LABEL_NUMBER equal to
4186 arc_ccfsm_target_label).
4187 4 -> 0 arc_ccfsm_advance, if `target' unconditional branch is reached
4188 3 -> 1 arc_ccfsm_advance, finding an 'else' jump skipping over some insns.
4189 5 -> 0 when outputting the delay slot insn
4190
4191 If the jump clobbers the conditions then we use states 2 and 4.
4192
4193 A similar thing can be done with conditional return insns.
4194
4195 We also handle separating branches from sets of the condition code.
4196 This is done here because knowledge of the ccfsm state is required,
4197 we may not be outputting the branch. */
4198
4199 /* arc_final_prescan_insn calls arc_ccfsm_advance to adjust arc_ccfsm_current,
4200 before letting final output INSN. */
4201
4202 static void
4203 arc_ccfsm_advance (rtx_insn *insn, struct arc_ccfsm *state)
4204 {
4205 /* BODY will hold the body of INSN. */
4206 register rtx body;
4207
4208 /* This will be 1 if trying to repeat the trick (ie: do the `else' part of
4209 an if/then/else), and things need to be reversed. */
4210 int reverse = 0;
4211
4212 /* If we start with a return insn, we only succeed if we find another one. */
4213 int seeking_return = 0;
4214
4215 /* START_INSN will hold the insn from where we start looking. This is the
4216 first insn after the following code_label if REVERSE is true. */
4217 rtx_insn *start_insn = insn;
4218
4219 /* Type of the jump_insn. Brcc insns don't affect ccfsm changes,
4220 since they don't rely on a cmp preceding the. */
4221 enum attr_type jump_insn_type;
4222
4223 /* Allow -mdebug-ccfsm to turn this off so we can see how well it does.
4224 We can't do this in macro FINAL_PRESCAN_INSN because its called from
4225 final_scan_insn which has `optimize' as a local. */
4226 if (optimize < 2 || TARGET_NO_COND_EXEC)
4227 return;
4228
4229 /* Ignore notes and labels. */
4230 if (!INSN_P (insn))
4231 return;
4232 body = PATTERN (insn);
4233 /* If in state 4, check if the target branch is reached, in order to
4234 change back to state 0. */
4235 if (state->state == 4)
4236 {
4237 if (insn == state->target_insn)
4238 {
4239 state->target_insn = NULL;
4240 state->state = 0;
4241 }
4242 return;
4243 }
4244
4245 /* If in state 3, it is possible to repeat the trick, if this insn is an
4246 unconditional branch to a label, and immediately following this branch
4247 is the previous target label which is only used once, and the label this
4248 branch jumps to is not too far off. Or in other words "we've done the
4249 `then' part, see if we can do the `else' part." */
4250 if (state->state == 3)
4251 {
4252 if (simplejump_p (insn))
4253 {
4254 start_insn = next_nonnote_insn (start_insn);
4255 if (GET_CODE (start_insn) == BARRIER)
4256 {
4257 /* ??? Isn't this always a barrier? */
4258 start_insn = next_nonnote_insn (start_insn);
4259 }
4260 if (GET_CODE (start_insn) == CODE_LABEL
4261 && CODE_LABEL_NUMBER (start_insn) == state->target_label
4262 && LABEL_NUSES (start_insn) == 1)
4263 reverse = TRUE;
4264 else
4265 return;
4266 }
4267 else if (GET_CODE (body) == SIMPLE_RETURN)
4268 {
4269 start_insn = next_nonnote_insn (start_insn);
4270 if (GET_CODE (start_insn) == BARRIER)
4271 start_insn = next_nonnote_insn (start_insn);
4272 if (GET_CODE (start_insn) == CODE_LABEL
4273 && CODE_LABEL_NUMBER (start_insn) == state->target_label
4274 && LABEL_NUSES (start_insn) == 1)
4275 {
4276 reverse = TRUE;
4277 seeking_return = 1;
4278 }
4279 else
4280 return;
4281 }
4282 else
4283 return;
4284 }
4285
4286 if (GET_CODE (insn) != JUMP_INSN
4287 || GET_CODE (PATTERN (insn)) == ADDR_VEC
4288 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC)
4289 return;
4290
4291 /* We can't predicate BRCC or loop ends.
4292 Also, when generating PIC code, and considering a medium range call,
4293 we can't predicate the call. */
4294 jump_insn_type = get_attr_type (insn);
4295 if (jump_insn_type == TYPE_BRCC
4296 || jump_insn_type == TYPE_BRCC_NO_DELAY_SLOT
4297 || jump_insn_type == TYPE_LOOP_END
4298 || (jump_insn_type == TYPE_CALL && !get_attr_predicable (insn)))
4299 return;
4300
4301 /* This jump might be paralleled with a clobber of the condition codes,
4302 the jump should always come first. */
4303 if (GET_CODE (body) == PARALLEL && XVECLEN (body, 0) > 0)
4304 body = XVECEXP (body, 0, 0);
4305
4306 if (reverse
4307 || (GET_CODE (body) == SET && GET_CODE (SET_DEST (body)) == PC
4308 && GET_CODE (SET_SRC (body)) == IF_THEN_ELSE))
4309 {
4310 int insns_skipped = 0, fail = FALSE, succeed = FALSE;
4311 /* Flag which part of the IF_THEN_ELSE is the LABEL_REF. */
4312 int then_not_else = TRUE;
4313 /* Nonzero if next insn must be the target label. */
4314 int next_must_be_target_label_p;
4315 rtx_insn *this_insn = start_insn;
4316 rtx label = 0;
4317
4318 /* Register the insn jumped to. */
4319 if (reverse)
4320 {
4321 if (!seeking_return)
4322 label = XEXP (SET_SRC (body), 0);
4323 }
4324 else if (GET_CODE (XEXP (SET_SRC (body), 1)) == LABEL_REF)
4325 label = XEXP (XEXP (SET_SRC (body), 1), 0);
4326 else if (GET_CODE (XEXP (SET_SRC (body), 2)) == LABEL_REF)
4327 {
4328 label = XEXP (XEXP (SET_SRC (body), 2), 0);
4329 then_not_else = FALSE;
4330 }
4331 else if (GET_CODE (XEXP (SET_SRC (body), 1)) == SIMPLE_RETURN)
4332 seeking_return = 1;
4333 else if (GET_CODE (XEXP (SET_SRC (body), 2)) == SIMPLE_RETURN)
4334 {
4335 seeking_return = 1;
4336 then_not_else = FALSE;
4337 }
4338 else
4339 gcc_unreachable ();
4340
4341 /* If this is a non-annulled branch with a delay slot, there is
4342 no need to conditionalize the delay slot. */
4343 if ((GET_CODE (PATTERN (NEXT_INSN (PREV_INSN (insn)))) == SEQUENCE)
4344 && state->state == 0 && !INSN_ANNULLED_BRANCH_P (insn))
4345 {
4346 this_insn = NEXT_INSN (this_insn);
4347 }
4348 /* See how many insns this branch skips, and what kind of insns. If all
4349 insns are okay, and the label or unconditional branch to the same
4350 label is not too far away, succeed. */
4351 for (insns_skipped = 0, next_must_be_target_label_p = FALSE;
4352 !fail && !succeed && insns_skipped < MAX_INSNS_SKIPPED;
4353 insns_skipped++)
4354 {
4355 rtx scanbody;
4356
4357 this_insn = next_nonnote_insn (this_insn);
4358 if (!this_insn)
4359 break;
4360
4361 if (next_must_be_target_label_p)
4362 {
4363 if (GET_CODE (this_insn) == BARRIER)
4364 continue;
4365 if (GET_CODE (this_insn) == CODE_LABEL
4366 && this_insn == label)
4367 {
4368 state->state = 1;
4369 succeed = TRUE;
4370 }
4371 else
4372 fail = TRUE;
4373 break;
4374 }
4375
4376 switch (GET_CODE (this_insn))
4377 {
4378 case CODE_LABEL:
4379 /* Succeed if it is the target label, otherwise fail since
4380 control falls in from somewhere else. */
4381 if (this_insn == label)
4382 {
4383 state->state = 1;
4384 succeed = TRUE;
4385 }
4386 else
4387 fail = TRUE;
4388 break;
4389
4390 case BARRIER:
4391 /* Succeed if the following insn is the target label.
4392 Otherwise fail.
4393 If return insns are used then the last insn in a function
4394 will be a barrier. */
4395 next_must_be_target_label_p = TRUE;
4396 break;
4397
4398 case CALL_INSN:
4399 /* Can handle a call insn if there are no insns after it.
4400 IE: The next "insn" is the target label. We don't have to
4401 worry about delay slots as such insns are SEQUENCE's inside
4402 INSN's. ??? It is possible to handle such insns though. */
4403 if (get_attr_cond (this_insn) == COND_CANUSE)
4404 next_must_be_target_label_p = TRUE;
4405 else
4406 fail = TRUE;
4407 break;
4408
4409 case JUMP_INSN:
4410 scanbody = PATTERN (this_insn);
4411
4412 /* If this is an unconditional branch to the same label, succeed.
4413 If it is to another label, do nothing. If it is conditional,
4414 fail. */
4415 /* ??? Probably, the test for the SET and the PC are
4416 unnecessary. */
4417
4418 if (GET_CODE (scanbody) == SET
4419 && GET_CODE (SET_DEST (scanbody)) == PC)
4420 {
4421 if (GET_CODE (SET_SRC (scanbody)) == LABEL_REF
4422 && XEXP (SET_SRC (scanbody), 0) == label && !reverse)
4423 {
4424 state->state = 2;
4425 succeed = TRUE;
4426 }
4427 else if (GET_CODE (SET_SRC (scanbody)) == IF_THEN_ELSE)
4428 fail = TRUE;
4429 else if (get_attr_cond (this_insn) != COND_CANUSE)
4430 fail = TRUE;
4431 }
4432 else if (GET_CODE (scanbody) == SIMPLE_RETURN
4433 && seeking_return)
4434 {
4435 state->state = 2;
4436 succeed = TRUE;
4437 }
4438 else if (GET_CODE (scanbody) == PARALLEL)
4439 {
4440 if (get_attr_cond (this_insn) != COND_CANUSE)
4441 fail = TRUE;
4442 }
4443 break;
4444
4445 case INSN:
4446 scanbody = PATTERN (this_insn);
4447
4448 /* We can only do this with insns that can use the condition
4449 codes (and don't set them). */
4450 if (GET_CODE (scanbody) == SET
4451 || GET_CODE (scanbody) == PARALLEL)
4452 {
4453 if (get_attr_cond (this_insn) != COND_CANUSE)
4454 fail = TRUE;
4455 }
4456 /* We can't handle other insns like sequences. */
4457 else
4458 fail = TRUE;
4459 break;
4460
4461 default:
4462 break;
4463 }
4464 }
4465
4466 if (succeed)
4467 {
4468 if ((!seeking_return) && (state->state == 1 || reverse))
4469 state->target_label = CODE_LABEL_NUMBER (label);
4470 else if (seeking_return || state->state == 2)
4471 {
4472 while (this_insn && GET_CODE (PATTERN (this_insn)) == USE)
4473 {
4474 this_insn = next_nonnote_insn (this_insn);
4475
4476 gcc_assert (!this_insn ||
4477 (GET_CODE (this_insn) != BARRIER
4478 && GET_CODE (this_insn) != CODE_LABEL));
4479 }
4480 if (!this_insn)
4481 {
4482 /* Oh dear! we ran off the end, give up. */
4483 extract_insn_cached (insn);
4484 state->state = 0;
4485 state->target_insn = NULL;
4486 return;
4487 }
4488 state->target_insn = this_insn;
4489 }
4490 else
4491 gcc_unreachable ();
4492
4493 /* If REVERSE is true, ARM_CURRENT_CC needs to be inverted from
4494 what it was. */
4495 if (!reverse)
4496 {
4497 state->cond = XEXP (SET_SRC (body), 0);
4498 state->cc = get_arc_condition_code (XEXP (SET_SRC (body), 0));
4499 }
4500
4501 if (reverse || then_not_else)
4502 state->cc = ARC_INVERSE_CONDITION_CODE (state->cc);
4503 }
4504
4505 /* Restore recog_operand. Getting the attributes of other insns can
4506 destroy this array, but final.c assumes that it remains intact
4507 across this call; since the insn has been recognized already we
4508 call insn_extract direct. */
4509 extract_insn_cached (insn);
4510 }
4511 }
4512
4513 /* Record that we are currently outputting label NUM with prefix PREFIX.
4514 It it's the label we're looking for, reset the ccfsm machinery.
4515
4516 Called from ASM_OUTPUT_INTERNAL_LABEL. */
4517
4518 static void
4519 arc_ccfsm_at_label (const char *prefix, int num, struct arc_ccfsm *state)
4520 {
4521 if (state->state == 3 && state->target_label == num
4522 && !strcmp (prefix, "L"))
4523 {
4524 state->state = 0;
4525 state->target_insn = NULL;
4526 }
4527 }
4528
4529 /* We are considering a conditional branch with the condition COND.
4530 Check if we want to conditionalize a delay slot insn, and if so modify
4531 the ccfsm state accordingly.
4532 REVERSE says branch will branch when the condition is false. */
4533 void
4534 arc_ccfsm_record_condition (rtx cond, bool reverse, rtx_insn *jump,
4535 struct arc_ccfsm *state)
4536 {
4537 rtx_insn *seq_insn = NEXT_INSN (PREV_INSN (jump));
4538 if (!state)
4539 state = &arc_ccfsm_current;
4540
4541 gcc_assert (state->state == 0);
4542 if (seq_insn != jump)
4543 {
4544 rtx insn = XVECEXP (PATTERN (seq_insn), 0, 1);
4545
4546 if (!as_a<rtx_insn *> (insn)->deleted ()
4547 && INSN_ANNULLED_BRANCH_P (jump)
4548 && (TARGET_AT_DBR_CONDEXEC || INSN_FROM_TARGET_P (insn)))
4549 {
4550 state->cond = cond;
4551 state->cc = get_arc_condition_code (cond);
4552 if (!reverse)
4553 arc_ccfsm_current.cc
4554 = ARC_INVERSE_CONDITION_CODE (state->cc);
4555 rtx pat = PATTERN (insn);
4556 if (GET_CODE (pat) == COND_EXEC)
4557 gcc_assert ((INSN_FROM_TARGET_P (insn)
4558 ? ARC_INVERSE_CONDITION_CODE (state->cc) : state->cc)
4559 == get_arc_condition_code (XEXP (pat, 0)));
4560 else
4561 state->state = 5;
4562 }
4563 }
4564 }
4565
4566 /* Update *STATE as we would when we emit INSN. */
4567
4568 static void
4569 arc_ccfsm_post_advance (rtx_insn *insn, struct arc_ccfsm *state)
4570 {
4571 enum attr_type type;
4572
4573 if (LABEL_P (insn))
4574 arc_ccfsm_at_label ("L", CODE_LABEL_NUMBER (insn), state);
4575 else if (JUMP_P (insn)
4576 && GET_CODE (PATTERN (insn)) != ADDR_VEC
4577 && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC
4578 && ((type = get_attr_type (insn)) == TYPE_BRANCH
4579 || ((type == TYPE_UNCOND_BRANCH
4580 || type == TYPE_RETURN)
4581 && ARC_CCFSM_BRANCH_DELETED_P (state))))
4582 {
4583 if (ARC_CCFSM_BRANCH_DELETED_P (state))
4584 ARC_CCFSM_RECORD_BRANCH_DELETED (state);
4585 else
4586 {
4587 rtx src = SET_SRC (PATTERN (insn));
4588 arc_ccfsm_record_condition (XEXP (src, 0), XEXP (src, 1) == pc_rtx,
4589 insn, state);
4590 }
4591 }
4592 else if (arc_ccfsm_current.state == 5)
4593 arc_ccfsm_current.state = 0;
4594 }
4595
4596 /* Return true if the current insn, which is a conditional branch, is to be
4597 deleted. */
4598
4599 bool
4600 arc_ccfsm_branch_deleted_p (void)
4601 {
4602 return ARC_CCFSM_BRANCH_DELETED_P (&arc_ccfsm_current);
4603 }
4604
4605 /* Record a branch isn't output because subsequent insns can be
4606 conditionalized. */
4607
4608 void
4609 arc_ccfsm_record_branch_deleted (void)
4610 {
4611 ARC_CCFSM_RECORD_BRANCH_DELETED (&arc_ccfsm_current);
4612 }
4613
4614 /* During insn output, indicate if the current insn is predicated. */
4615
4616 bool
4617 arc_ccfsm_cond_exec_p (void)
4618 {
4619 return (cfun->machine->prescan_initialized
4620 && ARC_CCFSM_COND_EXEC_P (&arc_ccfsm_current));
4621 }
4622
4623 /* Like next_active_insn, but return NULL if we find an ADDR_(DIFF_)VEC,
4624 and look inside SEQUENCEs. */
4625
4626 static rtx_insn *
4627 arc_next_active_insn (rtx_insn *insn, struct arc_ccfsm *statep)
4628 {
4629 rtx pat;
4630
4631 do
4632 {
4633 if (statep)
4634 arc_ccfsm_post_advance (insn, statep);
4635 insn = NEXT_INSN (insn);
4636 if (!insn || BARRIER_P (insn))
4637 return NULL;
4638 if (statep)
4639 arc_ccfsm_advance (insn, statep);
4640 }
4641 while (NOTE_P (insn)
4642 || (cfun->machine->arc_reorg_started
4643 && LABEL_P (insn) && !label_to_alignment (insn))
4644 || (NONJUMP_INSN_P (insn)
4645 && (GET_CODE (PATTERN (insn)) == USE
4646 || GET_CODE (PATTERN (insn)) == CLOBBER)));
4647 if (!LABEL_P (insn))
4648 {
4649 gcc_assert (INSN_P (insn));
4650 pat = PATTERN (insn);
4651 if (GET_CODE (pat) == ADDR_VEC || GET_CODE (pat) == ADDR_DIFF_VEC)
4652 return NULL;
4653 if (GET_CODE (pat) == SEQUENCE)
4654 return as_a <rtx_insn *> (XVECEXP (pat, 0, 0));
4655 }
4656 return insn;
4657 }
4658
4659 /* When deciding if an insn should be output short, we want to know something
4660 about the following insns:
4661 - if another insn follows which we know we can output as a short insn
4662 before an alignment-sensitive point, we can output this insn short:
4663 the decision about the eventual alignment can be postponed.
4664 - if a to-be-aligned label comes next, we should output this insn such
4665 as to get / preserve 4-byte alignment.
4666 - if a likely branch without delay slot insn, or a call with an immediately
4667 following short insn comes next, we should out output this insn such as to
4668 get / preserve 2 mod 4 unalignment.
4669 - do the same for a not completely unlikely branch with a short insn
4670 following before any other branch / label.
4671 - in order to decide if we are actually looking at a branch, we need to
4672 call arc_ccfsm_advance.
4673 - in order to decide if we are looking at a short insn, we should know
4674 if it is conditionalized. To a first order of approximation this is
4675 the case if the state from arc_ccfsm_advance from before this insn
4676 indicates the insn is conditionalized. However, a further refinement
4677 could be to not conditionalize an insn if the destination register(s)
4678 is/are dead in the non-executed case. */
4679 /* Return non-zero if INSN should be output as a short insn. UNALIGN is
4680 zero if the current insn is aligned to a 4-byte-boundary, two otherwise.
4681 If CHECK_ATTR is greater than 0, check the iscompact attribute first. */
4682
4683 int
4684 arc_verify_short (rtx_insn *insn, int, int check_attr)
4685 {
4686 enum attr_iscompact iscompact;
4687 struct machine_function *machine;
4688
4689 if (check_attr > 0)
4690 {
4691 iscompact = get_attr_iscompact (insn);
4692 if (iscompact == ISCOMPACT_FALSE)
4693 return 0;
4694 }
4695 machine = cfun->machine;
4696
4697 if (machine->force_short_suffix >= 0)
4698 return machine->force_short_suffix;
4699
4700 return (get_attr_length (insn) & 2) != 0;
4701 }
4702
4703 /* When outputting an instruction (alternative) that can potentially be short,
4704 output the short suffix if the insn is in fact short, and update
4705 cfun->machine->unalign accordingly. */
4706
4707 static void
4708 output_short_suffix (FILE *file)
4709 {
4710 rtx_insn *insn = current_output_insn;
4711
4712 if (arc_verify_short (insn, cfun->machine->unalign, 1))
4713 {
4714 fprintf (file, "_s");
4715 cfun->machine->unalign ^= 2;
4716 }
4717 /* Restore recog_operand. */
4718 extract_insn_cached (insn);
4719 }
4720
4721 /* Implement FINAL_PRESCAN_INSN. */
4722
4723 void
4724 arc_final_prescan_insn (rtx_insn *insn, rtx *opvec ATTRIBUTE_UNUSED,
4725 int noperands ATTRIBUTE_UNUSED)
4726 {
4727 if (TARGET_DUMPISIZE)
4728 fprintf (asm_out_file, "\n; at %04x\n", INSN_ADDRESSES (INSN_UID (insn)));
4729
4730 /* Output a nop if necessary to prevent a hazard.
4731 Don't do this for delay slots: inserting a nop would
4732 alter semantics, and the only time we would find a hazard is for a
4733 call function result - and in that case, the hazard is spurious to
4734 start with. */
4735 if (PREV_INSN (insn)
4736 && PREV_INSN (NEXT_INSN (insn)) == insn
4737 && arc_hazard (prev_real_insn (insn), insn))
4738 {
4739 current_output_insn =
4740 emit_insn_before (gen_nop (), NEXT_INSN (PREV_INSN (insn)));
4741 final_scan_insn (current_output_insn, asm_out_file, optimize, 1, NULL);
4742 current_output_insn = insn;
4743 }
4744 /* Restore extraction data which might have been clobbered by arc_hazard. */
4745 extract_constrain_insn_cached (insn);
4746
4747 if (!cfun->machine->prescan_initialized)
4748 {
4749 /* Clear lingering state from branch shortening. */
4750 memset (&arc_ccfsm_current, 0, sizeof arc_ccfsm_current);
4751 cfun->machine->prescan_initialized = 1;
4752 }
4753 arc_ccfsm_advance (insn, &arc_ccfsm_current);
4754
4755 cfun->machine->size_reason = 0;
4756 }
4757
4758 /* Given FROM and TO register numbers, say whether this elimination is allowed.
4759 Frame pointer elimination is automatically handled.
4760
4761 All eliminations are permissible. If we need a frame
4762 pointer, we must eliminate ARG_POINTER_REGNUM into
4763 FRAME_POINTER_REGNUM and not into STACK_POINTER_REGNUM. */
4764
4765 static bool
4766 arc_can_eliminate (const int from ATTRIBUTE_UNUSED, const int to)
4767 {
4768 return ((to == FRAME_POINTER_REGNUM) || !arc_frame_pointer_needed ());
4769 }
4770
4771 /* Define the offset between two registers, one to be eliminated, and
4772 the other its replacement, at the start of a routine. */
4773
4774 int
4775 arc_initial_elimination_offset (int from, int to)
4776 {
4777 if (! cfun->machine->frame_info.initialized)
4778 arc_compute_frame_size (get_frame_size ());
4779
4780 if (from == ARG_POINTER_REGNUM && to == FRAME_POINTER_REGNUM)
4781 {
4782 return (cfun->machine->frame_info.extra_size
4783 + cfun->machine->frame_info.reg_size);
4784 }
4785
4786 if (from == ARG_POINTER_REGNUM && to == STACK_POINTER_REGNUM)
4787 {
4788 return (cfun->machine->frame_info.total_size
4789 - cfun->machine->frame_info.pretend_size);
4790 }
4791
4792 if ((from == FRAME_POINTER_REGNUM) && (to == STACK_POINTER_REGNUM))
4793 {
4794 return (cfun->machine->frame_info.total_size
4795 - (cfun->machine->frame_info.pretend_size
4796 + cfun->machine->frame_info.extra_size
4797 + cfun->machine->frame_info.reg_size));
4798 }
4799
4800 gcc_unreachable ();
4801 }
4802
4803 static bool
4804 arc_frame_pointer_required (void)
4805 {
4806 return cfun->calls_alloca;
4807 }
4808
4809
4810 /* Return the destination address of a branch. */
4811
4812 int
4813 branch_dest (rtx branch)
4814 {
4815 rtx pat = PATTERN (branch);
4816 rtx dest = (GET_CODE (pat) == PARALLEL
4817 ? SET_SRC (XVECEXP (pat, 0, 0)) : SET_SRC (pat));
4818 int dest_uid;
4819
4820 if (GET_CODE (dest) == IF_THEN_ELSE)
4821 dest = XEXP (dest, XEXP (dest, 1) == pc_rtx ? 2 : 1);
4822
4823 dest = XEXP (dest, 0);
4824 dest_uid = INSN_UID (dest);
4825
4826 return INSN_ADDRESSES (dest_uid);
4827 }
4828
4829
4830 /* Implement TARGET_ENCODE_SECTION_INFO hook. */
4831
4832 static void
4833 arc_encode_section_info (tree decl, rtx rtl, int first)
4834 {
4835 /* For sdata, SYMBOL_FLAG_LOCAL and SYMBOL_FLAG_FUNCTION.
4836 This clears machine specific flags, so has to come first. */
4837 default_encode_section_info (decl, rtl, first);
4838
4839 /* Check if it is a function, and whether it has the
4840 [long/medium/short]_call attribute specified. */
4841 if (TREE_CODE (decl) == FUNCTION_DECL)
4842 {
4843 rtx symbol = XEXP (rtl, 0);
4844 int flags = SYMBOL_REF_FLAGS (symbol);
4845
4846 tree attr = (TREE_TYPE (decl) != error_mark_node
4847 ? TYPE_ATTRIBUTES (TREE_TYPE (decl)) : NULL_TREE);
4848 tree long_call_attr = lookup_attribute ("long_call", attr);
4849 tree medium_call_attr = lookup_attribute ("medium_call", attr);
4850 tree short_call_attr = lookup_attribute ("short_call", attr);
4851
4852 if (long_call_attr != NULL_TREE)
4853 flags |= SYMBOL_FLAG_LONG_CALL;
4854 else if (medium_call_attr != NULL_TREE)
4855 flags |= SYMBOL_FLAG_MEDIUM_CALL;
4856 else if (short_call_attr != NULL_TREE)
4857 flags |= SYMBOL_FLAG_SHORT_CALL;
4858
4859 SYMBOL_REF_FLAGS (symbol) = flags;
4860 }
4861 else if (TREE_CODE (decl) == VAR_DECL)
4862 {
4863 rtx symbol = XEXP (rtl, 0);
4864
4865 tree attr = (TREE_TYPE (decl) != error_mark_node
4866 ? DECL_ATTRIBUTES (decl) : NULL_TREE);
4867
4868 tree sec_attr = lookup_attribute ("section", attr);
4869 if (sec_attr)
4870 {
4871 const char *sec_name
4872 = TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (sec_attr)));
4873 if (strcmp (sec_name, ".cmem") == 0
4874 || strcmp (sec_name, ".cmem_shared") == 0
4875 || strcmp (sec_name, ".cmem_private") == 0)
4876 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_CMEM;
4877 }
4878 }
4879 }
4880
4881 /* This is how to output a definition of an internal numbered label where
4882 PREFIX is the class of label and NUM is the number within the class. */
4883
4884 static void arc_internal_label (FILE *stream, const char *prefix, unsigned long labelno)
4885 {
4886 if (cfun)
4887 arc_ccfsm_at_label (prefix, labelno, &arc_ccfsm_current);
4888 default_internal_label (stream, prefix, labelno);
4889 }
4890
4891 /* Set the cpu type and print out other fancy things,
4892 at the top of the file. */
4893
4894 static void arc_file_start (void)
4895 {
4896 default_file_start ();
4897 fprintf (asm_out_file, "\t.cpu %s\n", arc_cpu_string);
4898 }
4899
4900 /* Cost functions. */
4901
4902 /* Compute a (partial) cost for rtx X. Return true if the complete
4903 cost has been computed, and false if subexpressions should be
4904 scanned. In either case, *TOTAL contains the cost result. */
4905
4906 static bool
4907 arc_rtx_costs (rtx x, machine_mode mode, int outer_code,
4908 int opno ATTRIBUTE_UNUSED, int *total, bool speed)
4909 {
4910 int code = GET_CODE (x);
4911
4912 switch (code)
4913 {
4914 /* Small integers are as cheap as registers. */
4915 case CONST_INT:
4916 {
4917 bool nolimm = false; /* Can we do without long immediate? */
4918 bool fast = false; /* Is the result available immediately? */
4919 bool condexec = false; /* Does this allow conditiobnal execution? */
4920 bool compact = false; /* Is a 16 bit opcode available? */
4921 /* CONDEXEC also implies that we can have an unconditional
4922 3-address operation. */
4923
4924 nolimm = compact = condexec = false;
4925 if (UNSIGNED_INT6 (INTVAL (x)))
4926 nolimm = condexec = compact = true;
4927 else
4928 {
4929 if (SMALL_INT (INTVAL (x)))
4930 nolimm = fast = true;
4931 switch (outer_code)
4932 {
4933 case AND: /* bclr, bmsk, ext[bw] */
4934 if (satisfies_constraint_Ccp (x) /* bclr */
4935 || satisfies_constraint_C1p (x) /* bmsk */)
4936 nolimm = fast = condexec = compact = true;
4937 break;
4938 case IOR: /* bset */
4939 if (satisfies_constraint_C0p (x)) /* bset */
4940 nolimm = fast = condexec = compact = true;
4941 break;
4942 case XOR:
4943 if (satisfies_constraint_C0p (x)) /* bxor */
4944 nolimm = fast = condexec = true;
4945 break;
4946 case SET:
4947 if (satisfies_constraint_Crr (x)) /* ror b,u6 */
4948 nolimm = true;
4949 default:
4950 break;
4951 }
4952 }
4953 /* FIXME: Add target options to attach a small cost if
4954 condexec / compact is not true. */
4955 if (nolimm)
4956 {
4957 *total = 0;
4958 return true;
4959 }
4960 }
4961 /* FALLTHRU */
4962
4963 /* 4 byte values can be fetched as immediate constants -
4964 let's give that the cost of an extra insn. */
4965 case CONST:
4966 case LABEL_REF:
4967 case SYMBOL_REF:
4968 *total = COSTS_N_INSNS (1);
4969 return true;
4970
4971 case CONST_DOUBLE:
4972 {
4973 rtx first, second;
4974
4975 if (TARGET_DPFP)
4976 {
4977 *total = COSTS_N_INSNS (1);
4978 return true;
4979 }
4980 split_double (x, &first, &second);
4981 *total = COSTS_N_INSNS (!SMALL_INT (INTVAL (first))
4982 + !SMALL_INT (INTVAL (second)));
4983 return true;
4984 }
4985
4986 /* Encourage synth_mult to find a synthetic multiply when reasonable.
4987 If we need more than 12 insns to do a multiply, then go out-of-line,
4988 since the call overhead will be < 10% of the cost of the multiply. */
4989 case ASHIFT:
4990 case ASHIFTRT:
4991 case LSHIFTRT:
4992 if (TARGET_BARREL_SHIFTER)
4993 {
4994 /* If we want to shift a constant, we need a LIMM. */
4995 /* ??? when the optimizers want to know if a constant should be
4996 hoisted, they ask for the cost of the constant. OUTER_CODE is
4997 insufficient context for shifts since we don't know which operand
4998 we are looking at. */
4999 if (CONSTANT_P (XEXP (x, 0)))
5000 {
5001 *total += (COSTS_N_INSNS (2)
5002 + rtx_cost (XEXP (x, 1), mode, (enum rtx_code) code,
5003 0, speed));
5004 return true;
5005 }
5006 *total = COSTS_N_INSNS (1);
5007 }
5008 else if (GET_CODE (XEXP (x, 1)) != CONST_INT)
5009 *total = COSTS_N_INSNS (16);
5010 else
5011 {
5012 *total = COSTS_N_INSNS (INTVAL (XEXP ((x), 1)));
5013 /* ??? want_to_gcse_p can throw negative shift counts at us,
5014 and then panics when it gets a negative cost as result.
5015 Seen for gcc.c-torture/compile/20020710-1.c -Os . */
5016 if (*total < 0)
5017 *total = 0;
5018 }
5019 return false;
5020
5021 case DIV:
5022 case UDIV:
5023 if (speed)
5024 *total = COSTS_N_INSNS(30);
5025 else
5026 *total = COSTS_N_INSNS(1);
5027 return false;
5028
5029 case MULT:
5030 if ((TARGET_DPFP && GET_MODE (x) == DFmode))
5031 *total = COSTS_N_INSNS (1);
5032 else if (speed)
5033 *total= arc_multcost;
5034 /* We do not want synth_mult sequences when optimizing
5035 for size. */
5036 else if (TARGET_MUL64_SET || TARGET_ARC700_MPY)
5037 *total = COSTS_N_INSNS (1);
5038 else
5039 *total = COSTS_N_INSNS (2);
5040 return false;
5041 case PLUS:
5042 if ((GET_CODE (XEXP (x, 0)) == ASHIFT
5043 && _1_2_3_operand (XEXP (XEXP (x, 0), 1), VOIDmode))
5044 || (GET_CODE (XEXP (x, 0)) == MULT
5045 && _2_4_8_operand (XEXP (XEXP (x, 0), 1), VOIDmode)))
5046 {
5047 *total += (rtx_cost (XEXP (x, 1), mode, PLUS, 0, speed)
5048 + rtx_cost (XEXP (XEXP (x, 0), 0), mode, PLUS, 1, speed));
5049 return true;
5050 }
5051 return false;
5052 case MINUS:
5053 if ((GET_CODE (XEXP (x, 1)) == ASHIFT
5054 && _1_2_3_operand (XEXP (XEXP (x, 1), 1), VOIDmode))
5055 || (GET_CODE (XEXP (x, 1)) == MULT
5056 && _2_4_8_operand (XEXP (XEXP (x, 1), 1), VOIDmode)))
5057 {
5058 *total += (rtx_cost (XEXP (x, 0), mode, PLUS, 0, speed)
5059 + rtx_cost (XEXP (XEXP (x, 1), 0), mode, PLUS, 1, speed));
5060 return true;
5061 }
5062 return false;
5063 case COMPARE:
5064 {
5065 rtx op0 = XEXP (x, 0);
5066 rtx op1 = XEXP (x, 1);
5067
5068 if (GET_CODE (op0) == ZERO_EXTRACT && op1 == const0_rtx
5069 && XEXP (op0, 1) == const1_rtx)
5070 {
5071 /* btst / bbit0 / bbit1:
5072 Small integers and registers are free; everything else can
5073 be put in a register. */
5074 mode = GET_MODE (XEXP (op0, 0));
5075 *total = (rtx_cost (XEXP (op0, 0), mode, SET, 1, speed)
5076 + rtx_cost (XEXP (op0, 2), mode, SET, 1, speed));
5077 return true;
5078 }
5079 if (GET_CODE (op0) == AND && op1 == const0_rtx
5080 && satisfies_constraint_C1p (XEXP (op0, 1)))
5081 {
5082 /* bmsk.f */
5083 *total = rtx_cost (XEXP (op0, 0), VOIDmode, SET, 1, speed);
5084 return true;
5085 }
5086 /* add.f */
5087 if (GET_CODE (op1) == NEG)
5088 {
5089 /* op0 might be constant, the inside of op1 is rather
5090 unlikely to be so. So swapping the operands might lower
5091 the cost. */
5092 mode = GET_MODE (op0);
5093 *total = (rtx_cost (op0, mode, PLUS, 1, speed)
5094 + rtx_cost (XEXP (op1, 0), mode, PLUS, 0, speed));
5095 }
5096 return false;
5097 }
5098 case EQ: case NE:
5099 if (outer_code == IF_THEN_ELSE
5100 && GET_CODE (XEXP (x, 0)) == ZERO_EXTRACT
5101 && XEXP (x, 1) == const0_rtx
5102 && XEXP (XEXP (x, 0), 1) == const1_rtx)
5103 {
5104 /* btst / bbit0 / bbit1:
5105 Small integers and registers are free; everything else can
5106 be put in a register. */
5107 rtx op0 = XEXP (x, 0);
5108
5109 mode = GET_MODE (XEXP (op0, 0));
5110 *total = (rtx_cost (XEXP (op0, 0), mode, SET, 1, speed)
5111 + rtx_cost (XEXP (op0, 2), mode, SET, 1, speed));
5112 return true;
5113 }
5114 /* Fall through. */
5115 /* scc_insn expands into two insns. */
5116 case GTU: case GEU: case LEU:
5117 if (mode == SImode)
5118 *total += COSTS_N_INSNS (1);
5119 return false;
5120 case LTU: /* might use adc. */
5121 if (mode == SImode)
5122 *total += COSTS_N_INSNS (1) - 1;
5123 return false;
5124 default:
5125 return false;
5126 }
5127 }
5128
5129 /* Return true if ADDR is a valid pic address.
5130 A valid pic address on arc should look like
5131 const (unspec (SYMBOL_REF/LABEL) (ARC_UNSPEC_GOTOFF/ARC_UNSPEC_GOT)) */
5132
5133 bool
5134 arc_legitimate_pic_addr_p (rtx addr)
5135 {
5136 if (GET_CODE (addr) != CONST)
5137 return false;
5138
5139 addr = XEXP (addr, 0);
5140
5141
5142 if (GET_CODE (addr) == PLUS)
5143 {
5144 if (GET_CODE (XEXP (addr, 1)) != CONST_INT)
5145 return false;
5146 addr = XEXP (addr, 0);
5147 }
5148
5149 if (GET_CODE (addr) != UNSPEC
5150 || XVECLEN (addr, 0) != 1)
5151 return false;
5152
5153 /* Must be one of @GOT, @GOTOFF, @GOTOFFPC, @tlsgd, tlsie. */
5154 if (XINT (addr, 1) != ARC_UNSPEC_GOT
5155 && XINT (addr, 1) != ARC_UNSPEC_GOTOFF
5156 && XINT (addr, 1) != ARC_UNSPEC_GOTOFFPC
5157 && XINT (addr, 1) != UNSPEC_TLS_GD
5158 && XINT (addr, 1) != UNSPEC_TLS_IE)
5159 return false;
5160
5161 if (GET_CODE (XVECEXP (addr, 0, 0)) != SYMBOL_REF
5162 && GET_CODE (XVECEXP (addr, 0, 0)) != LABEL_REF)
5163 return false;
5164
5165 return true;
5166 }
5167
5168
5169
5170 /* Return true if OP contains a symbol reference. */
5171
5172 static bool
5173 symbolic_reference_mentioned_p (rtx op)
5174 {
5175 register const char *fmt;
5176 register int i;
5177
5178 if (GET_CODE (op) == SYMBOL_REF || GET_CODE (op) == LABEL_REF)
5179 return true;
5180
5181 fmt = GET_RTX_FORMAT (GET_CODE (op));
5182 for (i = GET_RTX_LENGTH (GET_CODE (op)) - 1; i >= 0; i--)
5183 {
5184 if (fmt[i] == 'E')
5185 {
5186 register int j;
5187
5188 for (j = XVECLEN (op, i) - 1; j >= 0; j--)
5189 if (symbolic_reference_mentioned_p (XVECEXP (op, i, j)))
5190 return true;
5191 }
5192
5193 else if (fmt[i] == 'e' && symbolic_reference_mentioned_p (XEXP (op, i)))
5194 return true;
5195 }
5196
5197 return false;
5198 }
5199
5200 /* Return true if OP contains a SYMBOL_REF that is not wrapped in an unspec.
5201 If SKIP_LOCAL is true, skip symbols that bind locally.
5202 This is used further down in this file, and, without SKIP_LOCAL,
5203 in the addsi3 / subsi3 expanders when generating PIC code. */
5204
5205 bool
5206 arc_raw_symbolic_reference_mentioned_p (rtx op, bool skip_local)
5207 {
5208 register const char *fmt;
5209 register int i;
5210
5211 if (GET_CODE(op) == UNSPEC)
5212 return false;
5213
5214 if (GET_CODE (op) == SYMBOL_REF)
5215 {
5216 if (SYMBOL_REF_TLS_MODEL (op))
5217 return true;
5218 if (!flag_pic)
5219 return false;
5220 tree decl = SYMBOL_REF_DECL (op);
5221 return !skip_local || !decl || !default_binds_local_p (decl);
5222 }
5223
5224 fmt = GET_RTX_FORMAT (GET_CODE (op));
5225 for (i = GET_RTX_LENGTH (GET_CODE (op)) - 1; i >= 0; i--)
5226 {
5227 if (fmt[i] == 'E')
5228 {
5229 register int j;
5230
5231 for (j = XVECLEN (op, i) - 1; j >= 0; j--)
5232 if (arc_raw_symbolic_reference_mentioned_p (XVECEXP (op, i, j),
5233 skip_local))
5234 return true;
5235 }
5236
5237 else if (fmt[i] == 'e'
5238 && arc_raw_symbolic_reference_mentioned_p (XEXP (op, i),
5239 skip_local))
5240 return true;
5241 }
5242
5243 return false;
5244 }
5245
5246 /* Get the thread pointer. */
5247
5248 static rtx
5249 arc_get_tp (void)
5250 {
5251 /* If arc_tp_regno has been set, we can use that hard register
5252 directly as a base register. */
5253 if (arc_tp_regno != -1)
5254 return gen_rtx_REG (Pmode, arc_tp_regno);
5255
5256 /* Otherwise, call __read_tp. Copy the result to a pseudo to avoid
5257 conflicts with function arguments / results. */
5258 rtx reg = gen_reg_rtx (Pmode);
5259 emit_insn (gen_tls_load_tp_soft ());
5260 emit_move_insn (reg, gen_rtx_REG (Pmode, R0_REG));
5261 return reg;
5262 }
5263
5264 /* Helper to be used by TLS Global dynamic model. */
5265
5266 static rtx
5267 arc_emit_call_tls_get_addr (rtx sym, int reloc, rtx eqv)
5268 {
5269 rtx r0 = gen_rtx_REG (Pmode, R0_REG);
5270 rtx call_fusage = NULL_RTX;
5271
5272 start_sequence ();
5273
5274 rtx x = arc_unspec_offset (sym, reloc);
5275 emit_move_insn (r0, x);
5276 use_reg (&call_fusage, r0);
5277
5278 gcc_assert (reloc == UNSPEC_TLS_GD);
5279 rtx call_insn = emit_call_insn (gen_tls_gd_get_addr (sym));
5280 /* Should we set RTL_CONST_CALL_P? We read memory, but not in a
5281 way that the application should care. */
5282 RTL_PURE_CALL_P (call_insn) = 1;
5283 add_function_usage_to (call_insn, call_fusage);
5284
5285 rtx_insn *insns = get_insns ();
5286 end_sequence ();
5287
5288 rtx dest = gen_reg_rtx (Pmode);
5289 emit_libcall_block (insns, dest, r0, eqv);
5290 return dest;
5291 }
5292
5293 #define DTPOFF_ZERO_SYM ".tdata"
5294
5295 /* Return a legitimized address for ADDR,
5296 which is a SYMBOL_REF with tls_model MODEL. */
5297
5298 static rtx
5299 arc_legitimize_tls_address (rtx addr, enum tls_model model)
5300 {
5301 if (!flag_pic && model == TLS_MODEL_LOCAL_DYNAMIC)
5302 model = TLS_MODEL_LOCAL_EXEC;
5303
5304 switch (model)
5305 {
5306 case TLS_MODEL_LOCAL_DYNAMIC:
5307 rtx base;
5308 tree decl;
5309 const char *base_name;
5310 rtvec v;
5311
5312 decl = SYMBOL_REF_DECL (addr);
5313 base_name = DTPOFF_ZERO_SYM;
5314 if (decl && bss_initializer_p (decl))
5315 base_name = ".tbss";
5316
5317 base = gen_rtx_SYMBOL_REF (Pmode, base_name);
5318 if (strcmp (base_name, DTPOFF_ZERO_SYM) == 0)
5319 {
5320 if (!flag_pic)
5321 goto local_exec;
5322 v = gen_rtvec (1, addr);
5323 }
5324 else
5325 v = gen_rtvec (2, addr, base);
5326 addr = gen_rtx_UNSPEC (Pmode, v, UNSPEC_TLS_OFF);
5327 addr = gen_rtx_CONST (Pmode, addr);
5328 base = arc_legitimize_tls_address (base, TLS_MODEL_GLOBAL_DYNAMIC);
5329 return gen_rtx_PLUS (Pmode, force_reg (Pmode, base), addr);
5330
5331 case TLS_MODEL_GLOBAL_DYNAMIC:
5332 return arc_emit_call_tls_get_addr (addr, UNSPEC_TLS_GD, addr);
5333
5334 case TLS_MODEL_INITIAL_EXEC:
5335 addr = arc_unspec_offset (addr, UNSPEC_TLS_IE);
5336 addr = copy_to_mode_reg (Pmode, gen_const_mem (Pmode, addr));
5337 return gen_rtx_PLUS (Pmode, arc_get_tp (), addr);
5338
5339 case TLS_MODEL_LOCAL_EXEC:
5340 local_exec:
5341 addr = arc_unspec_offset (addr, UNSPEC_TLS_OFF);
5342 return gen_rtx_PLUS (Pmode, arc_get_tp (), addr);
5343 default:
5344 gcc_unreachable ();
5345 }
5346 }
5347
5348 /* Legitimize a pic address reference in ORIG.
5349 The return value is the legitimated address.
5350 If OLDX is non-zero, it is the target to assign the address to first. */
5351
5352 static rtx
5353 arc_legitimize_pic_address (rtx orig, rtx oldx)
5354 {
5355 rtx addr = orig;
5356 rtx pat = orig;
5357 rtx base;
5358
5359 if (oldx == orig)
5360 oldx = NULL;
5361
5362 if (GET_CODE (addr) == LABEL_REF)
5363 ; /* Do nothing. */
5364 else if (GET_CODE (addr) == SYMBOL_REF)
5365 {
5366 enum tls_model model = SYMBOL_REF_TLS_MODEL (addr);
5367 if (model != 0)
5368 return arc_legitimize_tls_address (addr, model);
5369 else if (!flag_pic)
5370 return orig;
5371 else if (CONSTANT_POOL_ADDRESS_P (addr) || SYMBOL_REF_LOCAL_P (addr))
5372 return arc_unspec_offset (addr, ARC_UNSPEC_GOTOFFPC);
5373
5374 /* This symbol must be referenced via a load from the Global
5375 Offset Table (@GOTPC). */
5376 pat = arc_unspec_offset (addr, ARC_UNSPEC_GOT);
5377 pat = gen_const_mem (Pmode, pat);
5378
5379 if (oldx == NULL)
5380 oldx = gen_reg_rtx (Pmode);
5381
5382 emit_move_insn (oldx, pat);
5383 pat = oldx;
5384 }
5385 else
5386 {
5387 if (GET_CODE (addr) == CONST)
5388 {
5389 addr = XEXP (addr, 0);
5390 if (GET_CODE (addr) == UNSPEC)
5391 {
5392 /* Check that the unspec is one of the ones we generate? */
5393 return orig;
5394 }
5395 /* fwprop is placing in the REG_EQUIV notes constant pic
5396 unspecs expressions. Then, loop may use these notes for
5397 optimizations resulting in complex patterns that are not
5398 supported by the current implementation. The following
5399 two if-cases are simplifying the complex patters to
5400 simpler ones. */
5401 else if (GET_CODE (addr) == MINUS)
5402 {
5403 rtx op0 = XEXP (addr, 0);
5404 rtx op1 = XEXP (addr, 1);
5405 gcc_assert (oldx);
5406 gcc_assert (GET_CODE (op1) == UNSPEC);
5407
5408 emit_move_insn (oldx,
5409 gen_rtx_CONST (SImode,
5410 arc_legitimize_pic_address (op1,
5411 NULL_RTX)));
5412 emit_insn (gen_rtx_SET (oldx, gen_rtx_MINUS (SImode, op0, oldx)));
5413 return oldx;
5414
5415 }
5416 else if (GET_CODE (addr) != PLUS)
5417 {
5418 rtx tmp = XEXP (addr, 0);
5419 enum rtx_code code = GET_CODE (addr);
5420
5421 /* It only works for UNARY operations. */
5422 gcc_assert (UNARY_P (addr));
5423 gcc_assert (GET_CODE (tmp) == UNSPEC);
5424 gcc_assert (oldx);
5425
5426 emit_move_insn
5427 (oldx,
5428 gen_rtx_CONST (SImode,
5429 arc_legitimize_pic_address (tmp,
5430 NULL_RTX)));
5431
5432 emit_insn (gen_rtx_SET (oldx,
5433 gen_rtx_fmt_ee (code, SImode,
5434 oldx, const0_rtx)));
5435
5436 return oldx;
5437 }
5438 else
5439 {
5440 gcc_assert (GET_CODE (addr) == PLUS);
5441 if (GET_CODE (XEXP (addr, 0)) == UNSPEC)
5442 return orig;
5443 }
5444 }
5445
5446 if (GET_CODE (addr) == PLUS)
5447 {
5448 rtx op0 = XEXP (addr, 0), op1 = XEXP (addr, 1);
5449
5450 base = arc_legitimize_pic_address (op0, oldx);
5451 pat = arc_legitimize_pic_address (op1,
5452 base == oldx ? NULL_RTX : oldx);
5453
5454 if (base == op0 && pat == op1)
5455 return orig;
5456
5457 if (GET_CODE (pat) == CONST_INT)
5458 pat = plus_constant (Pmode, base, INTVAL (pat));
5459 else
5460 {
5461 if (GET_CODE (pat) == PLUS && CONSTANT_P (XEXP (pat, 1)))
5462 {
5463 base = gen_rtx_PLUS (Pmode, base, XEXP (pat, 0));
5464 pat = XEXP (pat, 1);
5465 }
5466 pat = gen_rtx_PLUS (Pmode, base, pat);
5467 }
5468 }
5469 }
5470
5471 return pat;
5472 }
5473
5474 /* Output address constant X to FILE, taking PIC into account. */
5475
5476 static void
5477 arc_output_pic_addr_const (FILE * file, rtx x, int code)
5478 {
5479 char buf[256];
5480
5481 restart:
5482 switch (GET_CODE (x))
5483 {
5484 case PC:
5485 if (flag_pic)
5486 putc ('.', file);
5487 else
5488 gcc_unreachable ();
5489 break;
5490
5491 case SYMBOL_REF:
5492 output_addr_const (file, x);
5493
5494 /* Local functions do not get references through the PLT. */
5495 if (code == 'P' && ! SYMBOL_REF_LOCAL_P (x))
5496 fputs ("@plt", file);
5497 break;
5498
5499 case LABEL_REF:
5500 ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (XEXP (x, 0)));
5501 assemble_name (file, buf);
5502 break;
5503
5504 case CODE_LABEL:
5505 ASM_GENERATE_INTERNAL_LABEL (buf, "L", CODE_LABEL_NUMBER (x));
5506 assemble_name (file, buf);
5507 break;
5508
5509 case CONST_INT:
5510 fprintf (file, HOST_WIDE_INT_PRINT_DEC, INTVAL (x));
5511 break;
5512
5513 case CONST:
5514 arc_output_pic_addr_const (file, XEXP (x, 0), code);
5515 break;
5516
5517 case CONST_DOUBLE:
5518 if (GET_MODE (x) == VOIDmode)
5519 {
5520 /* We can use %d if the number is one word and positive. */
5521 if (CONST_DOUBLE_HIGH (x))
5522 fprintf (file, HOST_WIDE_INT_PRINT_DOUBLE_HEX,
5523 CONST_DOUBLE_HIGH (x), CONST_DOUBLE_LOW (x));
5524 else if (CONST_DOUBLE_LOW (x) < 0)
5525 fprintf (file, HOST_WIDE_INT_PRINT_HEX, CONST_DOUBLE_LOW (x));
5526 else
5527 fprintf (file, HOST_WIDE_INT_PRINT_DEC, CONST_DOUBLE_LOW (x));
5528 }
5529 else
5530 /* We can't handle floating point constants;
5531 PRINT_OPERAND must handle them. */
5532 output_operand_lossage ("floating constant misused");
5533 break;
5534
5535 case PLUS:
5536 /* FIXME: Not needed here. */
5537 /* Some assemblers need integer constants to appear last (eg masm). */
5538 if (GET_CODE (XEXP (x, 0)) == CONST_INT)
5539 {
5540 arc_output_pic_addr_const (file, XEXP (x, 1), code);
5541 fprintf (file, "+");
5542 arc_output_pic_addr_const (file, XEXP (x, 0), code);
5543 }
5544 else if (GET_CODE (XEXP (x, 1)) == CONST_INT)
5545 {
5546 arc_output_pic_addr_const (file, XEXP (x, 0), code);
5547 if (INTVAL (XEXP (x, 1)) >= 0)
5548 fprintf (file, "+");
5549 arc_output_pic_addr_const (file, XEXP (x, 1), code);
5550 }
5551 else
5552 gcc_unreachable();
5553 break;
5554
5555 case MINUS:
5556 /* Avoid outputting things like x-x or x+5-x,
5557 since some assemblers can't handle that. */
5558 x = simplify_subtraction (x);
5559 if (GET_CODE (x) != MINUS)
5560 goto restart;
5561
5562 arc_output_pic_addr_const (file, XEXP (x, 0), code);
5563 fprintf (file, "-");
5564 if (GET_CODE (XEXP (x, 1)) == CONST_INT
5565 && INTVAL (XEXP (x, 1)) < 0)
5566 {
5567 fprintf (file, "(");
5568 arc_output_pic_addr_const (file, XEXP (x, 1), code);
5569 fprintf (file, ")");
5570 }
5571 else
5572 arc_output_pic_addr_const (file, XEXP (x, 1), code);
5573 break;
5574
5575 case ZERO_EXTEND:
5576 case SIGN_EXTEND:
5577 arc_output_pic_addr_const (file, XEXP (x, 0), code);
5578 break;
5579
5580
5581 case UNSPEC:
5582 const char *suffix;
5583 bool pcrel; pcrel = false;
5584 rtx base; base = NULL;
5585 gcc_assert (XVECLEN (x, 0) >= 1);
5586 switch (XINT (x, 1))
5587 {
5588 case ARC_UNSPEC_GOT:
5589 suffix = "@gotpc", pcrel = true;
5590 break;
5591 case ARC_UNSPEC_GOTOFF:
5592 suffix = "@gotoff";
5593 break;
5594 case ARC_UNSPEC_GOTOFFPC:
5595 suffix = "@pcl", pcrel = true;
5596 break;
5597 case ARC_UNSPEC_PLT:
5598 suffix = "@plt";
5599 break;
5600 case UNSPEC_TLS_GD:
5601 suffix = "@tlsgd", pcrel = true;
5602 break;
5603 case UNSPEC_TLS_IE:
5604 suffix = "@tlsie", pcrel = true;
5605 break;
5606 case UNSPEC_TLS_OFF:
5607 if (XVECLEN (x, 0) == 2)
5608 base = XVECEXP (x, 0, 1);
5609 if (SYMBOL_REF_TLS_MODEL (XVECEXP (x, 0, 0)) == TLS_MODEL_LOCAL_EXEC
5610 || (!flag_pic && !base))
5611 suffix = "@tpoff";
5612 else
5613 suffix = "@dtpoff";
5614 break;
5615 default:
5616 suffix = "@invalid";
5617 output_operand_lossage ("invalid UNSPEC as operand: %d", XINT (x,1));
5618 break;
5619 }
5620 if (pcrel)
5621 fputs ("pcl,", file);
5622 arc_output_pic_addr_const (file, XVECEXP (x, 0, 0), code);
5623 fputs (suffix, file);
5624 if (base)
5625 arc_output_pic_addr_const (file, base, code);
5626 break;
5627
5628 default:
5629 output_operand_lossage ("invalid expression as operand");
5630 }
5631 }
5632
5633 #define SYMBOLIC_CONST(X) \
5634 (GET_CODE (X) == SYMBOL_REF \
5635 || GET_CODE (X) == LABEL_REF \
5636 || (GET_CODE (X) == CONST && symbolic_reference_mentioned_p (X)))
5637
5638 /* Emit insns to move operands[1] into operands[0]. */
5639
5640 static void
5641 prepare_pic_move (rtx *operands, machine_mode)
5642 {
5643 if (GET_CODE (operands[0]) == MEM && SYMBOLIC_CONST (operands[1])
5644 && flag_pic)
5645 operands[1] = force_reg (Pmode, operands[1]);
5646 else
5647 {
5648 rtx temp = (reload_in_progress ? operands[0]
5649 : flag_pic? gen_reg_rtx (Pmode) : NULL_RTX);
5650 operands[1] = arc_legitimize_pic_address (operands[1], temp);
5651 }
5652 }
5653
5654
5655 /* The function returning the number of words, at the beginning of an
5656 argument, must be put in registers. The returned value must be
5657 zero for arguments that are passed entirely in registers or that
5658 are entirely pushed on the stack.
5659
5660 On some machines, certain arguments must be passed partially in
5661 registers and partially in memory. On these machines, typically
5662 the first N words of arguments are passed in registers, and the
5663 rest on the stack. If a multi-word argument (a `double' or a
5664 structure) crosses that boundary, its first few words must be
5665 passed in registers and the rest must be pushed. This function
5666 tells the compiler when this occurs, and how many of the words
5667 should go in registers.
5668
5669 `FUNCTION_ARG' for these arguments should return the first register
5670 to be used by the caller for this argument; likewise
5671 `FUNCTION_INCOMING_ARG', for the called function.
5672
5673 The function is used to implement macro FUNCTION_ARG_PARTIAL_NREGS. */
5674
5675 /* If REGNO is the least arg reg available then what is the total number of arg
5676 regs available. */
5677 #define GPR_REST_ARG_REGS(REGNO) \
5678 ((REGNO) <= MAX_ARC_PARM_REGS ? MAX_ARC_PARM_REGS - (REGNO) : 0 )
5679
5680 /* Since arc parm regs are contiguous. */
5681 #define ARC_NEXT_ARG_REG(REGNO) ( (REGNO) + 1 )
5682
5683 /* Implement TARGET_ARG_PARTIAL_BYTES. */
5684
5685 static int
5686 arc_arg_partial_bytes (cumulative_args_t cum_v, machine_mode mode,
5687 tree type, bool named ATTRIBUTE_UNUSED)
5688 {
5689 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
5690 int bytes = (mode == BLKmode
5691 ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode));
5692 int words = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
5693 int arg_num = *cum;
5694 int ret;
5695
5696 arg_num = ROUND_ADVANCE_CUM (arg_num, mode, type);
5697 ret = GPR_REST_ARG_REGS (arg_num);
5698
5699 /* ICEd at function.c:2361, and ret is copied to data->partial */
5700 ret = (ret >= words ? 0 : ret * UNITS_PER_WORD);
5701
5702 return ret;
5703 }
5704
5705 /* This function is used to control a function argument is passed in a
5706 register, and which register.
5707
5708 The arguments are CUM, of type CUMULATIVE_ARGS, which summarizes
5709 (in a way defined by INIT_CUMULATIVE_ARGS and FUNCTION_ARG_ADVANCE)
5710 all of the previous arguments so far passed in registers; MODE, the
5711 machine mode of the argument; TYPE, the data type of the argument
5712 as a tree node or 0 if that is not known (which happens for C
5713 support library functions); and NAMED, which is 1 for an ordinary
5714 argument and 0 for nameless arguments that correspond to `...' in
5715 the called function's prototype.
5716
5717 The returned value should either be a `reg' RTX for the hard
5718 register in which to pass the argument, or zero to pass the
5719 argument on the stack.
5720
5721 For machines like the Vax and 68000, where normally all arguments
5722 are pushed, zero suffices as a definition.
5723
5724 The usual way to make the ANSI library `stdarg.h' work on a machine
5725 where some arguments are usually passed in registers, is to cause
5726 nameless arguments to be passed on the stack instead. This is done
5727 by making the function return 0 whenever NAMED is 0.
5728
5729 You may use the macro `MUST_PASS_IN_STACK (MODE, TYPE)' in the
5730 definition of this function to determine if this argument is of a
5731 type that must be passed in the stack. If `REG_PARM_STACK_SPACE'
5732 is not defined and the function returns non-zero for such an
5733 argument, the compiler will abort. If `REG_PARM_STACK_SPACE' is
5734 defined, the argument will be computed in the stack and then loaded
5735 into a register.
5736
5737 The function is used to implement macro FUNCTION_ARG. */
5738 /* On the ARC the first MAX_ARC_PARM_REGS args are normally in registers
5739 and the rest are pushed. */
5740
5741 static rtx
5742 arc_function_arg (cumulative_args_t cum_v,
5743 machine_mode mode,
5744 const_tree type ATTRIBUTE_UNUSED,
5745 bool named ATTRIBUTE_UNUSED)
5746 {
5747 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
5748 int arg_num = *cum;
5749 rtx ret;
5750 const char *debstr ATTRIBUTE_UNUSED;
5751
5752 arg_num = ROUND_ADVANCE_CUM (arg_num, mode, type);
5753 /* Return a marker for use in the call instruction. */
5754 if (mode == VOIDmode)
5755 {
5756 ret = const0_rtx;
5757 debstr = "<0>";
5758 }
5759 else if (GPR_REST_ARG_REGS (arg_num) > 0)
5760 {
5761 ret = gen_rtx_REG (mode, arg_num);
5762 debstr = reg_names [arg_num];
5763 }
5764 else
5765 {
5766 ret = NULL_RTX;
5767 debstr = "memory";
5768 }
5769 return ret;
5770 }
5771
5772 /* The function to update the summarizer variable *CUM to advance past
5773 an argument in the argument list. The values MODE, TYPE and NAMED
5774 describe that argument. Once this is done, the variable *CUM is
5775 suitable for analyzing the *following* argument with
5776 `FUNCTION_ARG', etc.
5777
5778 This function need not do anything if the argument in question was
5779 passed on the stack. The compiler knows how to track the amount of
5780 stack space used for arguments without any special help.
5781
5782 The function is used to implement macro FUNCTION_ARG_ADVANCE. */
5783 /* For the ARC: the cum set here is passed on to function_arg where we
5784 look at its value and say which reg to use. Strategy: advance the
5785 regnumber here till we run out of arg regs, then set *cum to last
5786 reg. In function_arg, since *cum > last arg reg we would return 0
5787 and thus the arg will end up on the stack. For straddling args of
5788 course function_arg_partial_nregs will come into play. */
5789
5790 static void
5791 arc_function_arg_advance (cumulative_args_t cum_v,
5792 machine_mode mode,
5793 const_tree type,
5794 bool named ATTRIBUTE_UNUSED)
5795 {
5796 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
5797 int bytes = (mode == BLKmode
5798 ? int_size_in_bytes (type) : (int) GET_MODE_SIZE (mode));
5799 int words = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
5800 int i;
5801
5802 if (words)
5803 *cum = ROUND_ADVANCE_CUM (*cum, mode, type);
5804 for (i = 0; i < words; i++)
5805 *cum = ARC_NEXT_ARG_REG (*cum);
5806
5807 }
5808
5809 /* Define how to find the value returned by a function.
5810 VALTYPE is the data type of the value (as a tree).
5811 If the precise function being called is known, FN_DECL_OR_TYPE is its
5812 FUNCTION_DECL; otherwise, FN_DECL_OR_TYPE is its type. */
5813
5814 static rtx
5815 arc_function_value (const_tree valtype,
5816 const_tree fn_decl_or_type ATTRIBUTE_UNUSED,
5817 bool outgoing ATTRIBUTE_UNUSED)
5818 {
5819 machine_mode mode = TYPE_MODE (valtype);
5820 int unsignedp ATTRIBUTE_UNUSED;
5821
5822 unsignedp = TYPE_UNSIGNED (valtype);
5823 if (INTEGRAL_TYPE_P (valtype) || TREE_CODE (valtype) == OFFSET_TYPE)
5824 PROMOTE_MODE (mode, unsignedp, valtype);
5825 return gen_rtx_REG (mode, 0);
5826 }
5827
5828 /* Returns the return address that is used by builtin_return_address. */
5829
5830 rtx
5831 arc_return_addr_rtx (int count, ATTRIBUTE_UNUSED rtx frame)
5832 {
5833 if (count != 0)
5834 return const0_rtx;
5835
5836 return get_hard_reg_initial_val (Pmode , RETURN_ADDR_REGNUM);
5837 }
5838
5839 /* Determine if a given RTX is a valid constant. We already know this
5840 satisfies CONSTANT_P. */
5841
5842 bool
5843 arc_legitimate_constant_p (machine_mode mode, rtx x)
5844 {
5845 if (GET_CODE (x) == SYMBOL_REF && SYMBOL_REF_TLS_MODEL (x))
5846 return false;
5847
5848 if (!flag_pic && mode != Pmode)
5849 return true;
5850
5851 switch (GET_CODE (x))
5852 {
5853 case CONST:
5854 if (flag_pic)
5855 {
5856 if (arc_legitimate_pic_addr_p (x))
5857 return true;
5858 }
5859 return arc_legitimate_constant_p (mode, XEXP (x, 0));
5860
5861 case SYMBOL_REF:
5862 if (SYMBOL_REF_TLS_MODEL (x))
5863 return false;
5864 /* Fall through. */
5865 case LABEL_REF:
5866 if (flag_pic)
5867 return false;
5868 /* Fall through. */
5869 case CONST_INT:
5870 case CONST_DOUBLE:
5871 return true;
5872
5873 case NEG:
5874 return arc_legitimate_constant_p (mode, XEXP (x, 0));
5875
5876 case PLUS:
5877 case MINUS:
5878 {
5879 bool t1 = arc_legitimate_constant_p (mode, XEXP (x, 0));
5880 bool t2 = arc_legitimate_constant_p (mode, XEXP (x, 1));
5881
5882 return (t1 && t2);
5883 }
5884
5885 case CONST_VECTOR:
5886 switch (mode)
5887 {
5888 case E_V2HImode:
5889 return TARGET_PLUS_DMPY;
5890 case E_V2SImode:
5891 case E_V4HImode:
5892 return TARGET_PLUS_QMACW;
5893 default:
5894 return false;
5895 }
5896
5897 case UNSPEC:
5898 switch (XINT (x, 1))
5899 {
5900 case UNSPEC_TLS_GD:
5901 case UNSPEC_TLS_OFF:
5902 case UNSPEC_TLS_IE:
5903 return true;
5904 default:
5905 /* Any other unspec ending here are pic related, hence the above
5906 constant pic address checking returned false. */
5907 return false;
5908 }
5909 /* Fall through. */
5910
5911 default:
5912 fatal_insn ("unrecognized supposed constant", x);
5913 }
5914
5915 gcc_unreachable ();
5916 }
5917
5918 static bool
5919 arc_legitimate_address_p (machine_mode mode, rtx x, bool strict)
5920 {
5921 if (RTX_OK_FOR_BASE_P (x, strict))
5922 return true;
5923 if (legitimate_offset_address_p (mode, x, TARGET_INDEXED_LOADS, strict))
5924 return true;
5925 if (legitimate_scaled_address_p (mode, x, strict))
5926 return true;
5927 if (LEGITIMATE_SMALL_DATA_ADDRESS_P (x))
5928 return true;
5929 if (GET_CODE (x) == CONST_INT && LARGE_INT (INTVAL (x)))
5930 return true;
5931
5932 /* When we compile for size avoid const (@sym + offset)
5933 addresses. */
5934 if (!flag_pic && optimize_size && !reload_completed
5935 && (GET_CODE (x) == CONST)
5936 && (GET_CODE (XEXP (x, 0)) == PLUS)
5937 && (GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF)
5938 && SYMBOL_REF_TLS_MODEL (XEXP (XEXP (x, 0), 0)) == 0
5939 && !SYMBOL_REF_FUNCTION_P (XEXP (XEXP (x, 0), 0)))
5940 {
5941 rtx addend = XEXP (XEXP (x, 0), 1);
5942 gcc_assert (CONST_INT_P (addend));
5943 HOST_WIDE_INT offset = INTVAL (addend);
5944
5945 /* Allow addresses having a large offset to pass. Anyhow they
5946 will end in a limm. */
5947 return !(offset > -1024 && offset < 1020);
5948 }
5949
5950 if ((GET_MODE_SIZE (mode) != 16) && CONSTANT_P (x))
5951 {
5952 return arc_legitimate_constant_p (mode, x);
5953 }
5954 if ((GET_CODE (x) == PRE_DEC || GET_CODE (x) == PRE_INC
5955 || GET_CODE (x) == POST_DEC || GET_CODE (x) == POST_INC)
5956 && RTX_OK_FOR_BASE_P (XEXP (x, 0), strict))
5957 return true;
5958 /* We're restricted here by the `st' insn. */
5959 if ((GET_CODE (x) == PRE_MODIFY || GET_CODE (x) == POST_MODIFY)
5960 && GET_CODE (XEXP ((x), 1)) == PLUS
5961 && rtx_equal_p (XEXP ((x), 0), XEXP (XEXP (x, 1), 0))
5962 && legitimate_offset_address_p (QImode, XEXP (x, 1),
5963 TARGET_AUTO_MODIFY_REG, strict))
5964 return true;
5965 return false;
5966 }
5967
5968 /* Return true iff ADDR (a legitimate address expression)
5969 has an effect that depends on the machine mode it is used for. */
5970
5971 static bool
5972 arc_mode_dependent_address_p (const_rtx addr, addr_space_t)
5973 {
5974 /* SYMBOL_REF is not mode dependent: it is either a small data reference,
5975 which is valid for loads and stores, or a limm offset, which is valid for
5976 loads. Scaled indices are scaled by the access mode. */
5977 if (GET_CODE (addr) == PLUS
5978 && GET_CODE (XEXP ((addr), 0)) == MULT)
5979 return true;
5980 return false;
5981 }
5982
5983 /* Determine if it's legal to put X into the constant pool. */
5984
5985 static bool
5986 arc_cannot_force_const_mem (machine_mode mode, rtx x)
5987 {
5988 return !arc_legitimate_constant_p (mode, x);
5989 }
5990
5991 /* IDs for all the ARC builtins. */
5992
5993 enum arc_builtin_id
5994 {
5995 #define DEF_BUILTIN(NAME, N_ARGS, TYPE, ICODE, MASK) \
5996 ARC_BUILTIN_ ## NAME,
5997 #include "builtins.def"
5998 #undef DEF_BUILTIN
5999
6000 ARC_BUILTIN_COUNT
6001 };
6002
6003 struct GTY(()) arc_builtin_description
6004 {
6005 enum insn_code icode;
6006 int n_args;
6007 tree fndecl;
6008 };
6009
6010 static GTY(()) struct arc_builtin_description
6011 arc_bdesc[ARC_BUILTIN_COUNT] =
6012 {
6013 #define DEF_BUILTIN(NAME, N_ARGS, TYPE, ICODE, MASK) \
6014 { (enum insn_code) CODE_FOR_ ## ICODE, N_ARGS, NULL_TREE },
6015 #include "builtins.def"
6016 #undef DEF_BUILTIN
6017 };
6018
6019 /* Transform UP into lowercase and write the result to LO.
6020 You must provide enough space for LO. Return LO. */
6021
6022 static char*
6023 arc_tolower (char *lo, const char *up)
6024 {
6025 char *lo0 = lo;
6026
6027 for (; *up; up++, lo++)
6028 *lo = TOLOWER (*up);
6029
6030 *lo = '\0';
6031
6032 return lo0;
6033 }
6034
6035 /* Implement `TARGET_BUILTIN_DECL'. */
6036
6037 static tree
6038 arc_builtin_decl (unsigned id, bool initialize_p ATTRIBUTE_UNUSED)
6039 {
6040 if (id < ARC_BUILTIN_COUNT)
6041 return arc_bdesc[id].fndecl;
6042
6043 return error_mark_node;
6044 }
6045
6046 static void
6047 arc_init_builtins (void)
6048 {
6049 tree V4HI_type_node;
6050 tree V2SI_type_node;
6051 tree V2HI_type_node;
6052
6053 /* Vector types based on HS SIMD elements. */
6054 V4HI_type_node = build_vector_type_for_mode (intHI_type_node, V4HImode);
6055 V2SI_type_node = build_vector_type_for_mode (intSI_type_node, V2SImode);
6056 V2HI_type_node = build_vector_type_for_mode (intHI_type_node, V2HImode);
6057
6058 tree pcvoid_type_node
6059 = build_pointer_type (build_qualified_type (void_type_node,
6060 TYPE_QUAL_CONST));
6061 tree V8HI_type_node = build_vector_type_for_mode (intHI_type_node,
6062 V8HImode);
6063
6064 tree void_ftype_void
6065 = build_function_type_list (void_type_node, NULL_TREE);
6066 tree int_ftype_int
6067 = build_function_type_list (integer_type_node, integer_type_node,
6068 NULL_TREE);
6069 tree int_ftype_pcvoid_int
6070 = build_function_type_list (integer_type_node, pcvoid_type_node,
6071 integer_type_node, NULL_TREE);
6072 tree void_ftype_usint_usint
6073 = build_function_type_list (void_type_node, long_unsigned_type_node,
6074 long_unsigned_type_node, NULL_TREE);
6075 tree int_ftype_int_int
6076 = build_function_type_list (integer_type_node, integer_type_node,
6077 integer_type_node, NULL_TREE);
6078 tree usint_ftype_usint
6079 = build_function_type_list (long_unsigned_type_node,
6080 long_unsigned_type_node, NULL_TREE);
6081 tree void_ftype_usint
6082 = build_function_type_list (void_type_node, long_unsigned_type_node,
6083 NULL_TREE);
6084 tree int_ftype_void
6085 = build_function_type_list (integer_type_node, void_type_node,
6086 NULL_TREE);
6087 tree void_ftype_int
6088 = build_function_type_list (void_type_node, integer_type_node,
6089 NULL_TREE);
6090 tree int_ftype_short
6091 = build_function_type_list (integer_type_node, short_integer_type_node,
6092 NULL_TREE);
6093
6094 /* Old ARC SIMD types. */
6095 tree v8hi_ftype_v8hi_v8hi
6096 = build_function_type_list (V8HI_type_node, V8HI_type_node,
6097 V8HI_type_node, NULL_TREE);
6098 tree v8hi_ftype_v8hi_int
6099 = build_function_type_list (V8HI_type_node, V8HI_type_node,
6100 integer_type_node, NULL_TREE);
6101 tree v8hi_ftype_v8hi_int_int
6102 = build_function_type_list (V8HI_type_node, V8HI_type_node,
6103 integer_type_node, integer_type_node,
6104 NULL_TREE);
6105 tree void_ftype_v8hi_int_int
6106 = build_function_type_list (void_type_node, V8HI_type_node,
6107 integer_type_node, integer_type_node,
6108 NULL_TREE);
6109 tree void_ftype_v8hi_int_int_int
6110 = build_function_type_list (void_type_node, V8HI_type_node,
6111 integer_type_node, integer_type_node,
6112 integer_type_node, NULL_TREE);
6113 tree v8hi_ftype_int_int
6114 = build_function_type_list (V8HI_type_node, integer_type_node,
6115 integer_type_node, NULL_TREE);
6116 tree void_ftype_int_int
6117 = build_function_type_list (void_type_node, integer_type_node,
6118 integer_type_node, NULL_TREE);
6119 tree v8hi_ftype_v8hi
6120 = build_function_type_list (V8HI_type_node, V8HI_type_node,
6121 NULL_TREE);
6122 /* ARCv2 SIMD types. */
6123 tree long_ftype_v4hi_v4hi
6124 = build_function_type_list (long_long_integer_type_node,
6125 V4HI_type_node, V4HI_type_node, NULL_TREE);
6126 tree int_ftype_v2hi_v2hi
6127 = build_function_type_list (integer_type_node,
6128 V2HI_type_node, V2HI_type_node, NULL_TREE);
6129 tree v2si_ftype_v2hi_v2hi
6130 = build_function_type_list (V2SI_type_node,
6131 V2HI_type_node, V2HI_type_node, NULL_TREE);
6132 tree v2hi_ftype_v2hi_v2hi
6133 = build_function_type_list (V2HI_type_node,
6134 V2HI_type_node, V2HI_type_node, NULL_TREE);
6135 tree v2si_ftype_v2si_v2si
6136 = build_function_type_list (V2SI_type_node,
6137 V2SI_type_node, V2SI_type_node, NULL_TREE);
6138 tree v4hi_ftype_v4hi_v4hi
6139 = build_function_type_list (V4HI_type_node,
6140 V4HI_type_node, V4HI_type_node, NULL_TREE);
6141 tree long_ftype_v2si_v2hi
6142 = build_function_type_list (long_long_integer_type_node,
6143 V2SI_type_node, V2HI_type_node, NULL_TREE);
6144
6145 /* Add the builtins. */
6146 #define DEF_BUILTIN(NAME, N_ARGS, TYPE, ICODE, MASK) \
6147 { \
6148 int id = ARC_BUILTIN_ ## NAME; \
6149 const char *Name = "__builtin_arc_" #NAME; \
6150 char *name = (char*) alloca (1 + strlen (Name)); \
6151 \
6152 gcc_assert (id < ARC_BUILTIN_COUNT); \
6153 if (MASK) \
6154 arc_bdesc[id].fndecl \
6155 = add_builtin_function (arc_tolower(name, Name), TYPE, id, \
6156 BUILT_IN_MD, NULL, NULL_TREE); \
6157 }
6158 #include "builtins.def"
6159 #undef DEF_BUILTIN
6160 }
6161
6162 /* Helper to expand __builtin_arc_aligned (void* val, int
6163 alignval). */
6164
6165 static rtx
6166 arc_expand_builtin_aligned (tree exp)
6167 {
6168 tree arg0 = CALL_EXPR_ARG (exp, 0);
6169 tree arg1 = CALL_EXPR_ARG (exp, 1);
6170 fold (arg1);
6171 rtx op0 = expand_expr (arg0, NULL_RTX, VOIDmode, EXPAND_NORMAL);
6172 rtx op1 = expand_expr (arg1, NULL_RTX, VOIDmode, EXPAND_NORMAL);
6173
6174 if (!CONST_INT_P (op1))
6175 {
6176 /* If we can't fold the alignment to a constant integer
6177 whilst optimizing, this is probably a user error. */
6178 if (optimize)
6179 warning (0, "__builtin_arc_aligned with non-constant alignment");
6180 }
6181 else
6182 {
6183 HOST_WIDE_INT alignTest = INTVAL (op1);
6184 /* Check alignTest is positive, and a power of two. */
6185 if (alignTest <= 0 || alignTest != (alignTest & -alignTest))
6186 {
6187 error ("invalid alignment value for __builtin_arc_aligned");
6188 return NULL_RTX;
6189 }
6190
6191 if (CONST_INT_P (op0))
6192 {
6193 HOST_WIDE_INT pnt = INTVAL (op0);
6194
6195 if ((pnt & (alignTest - 1)) == 0)
6196 return const1_rtx;
6197 }
6198 else
6199 {
6200 unsigned align = get_pointer_alignment (arg0);
6201 unsigned numBits = alignTest * BITS_PER_UNIT;
6202
6203 if (align && align >= numBits)
6204 return const1_rtx;
6205 /* Another attempt to ascertain alignment. Check the type
6206 we are pointing to. */
6207 if (POINTER_TYPE_P (TREE_TYPE (arg0))
6208 && TYPE_ALIGN (TREE_TYPE (TREE_TYPE (arg0))) >= numBits)
6209 return const1_rtx;
6210 }
6211 }
6212
6213 /* Default to false. */
6214 return const0_rtx;
6215 }
6216
6217 /* Helper arc_expand_builtin, generates a pattern for the given icode
6218 and arguments. */
6219
6220 static rtx_insn *
6221 apply_GEN_FCN (enum insn_code icode, rtx *arg)
6222 {
6223 switch (insn_data[icode].n_generator_args)
6224 {
6225 case 0:
6226 return GEN_FCN (icode) ();
6227 case 1:
6228 return GEN_FCN (icode) (arg[0]);
6229 case 2:
6230 return GEN_FCN (icode) (arg[0], arg[1]);
6231 case 3:
6232 return GEN_FCN (icode) (arg[0], arg[1], arg[2]);
6233 case 4:
6234 return GEN_FCN (icode) (arg[0], arg[1], arg[2], arg[3]);
6235 case 5:
6236 return GEN_FCN (icode) (arg[0], arg[1], arg[2], arg[3], arg[4]);
6237 default:
6238 gcc_unreachable ();
6239 }
6240 }
6241
6242 /* Expand an expression EXP that calls a built-in function,
6243 with result going to TARGET if that's convenient
6244 (and in mode MODE if that's convenient).
6245 SUBTARGET may be used as the target for computing one of EXP's operands.
6246 IGNORE is nonzero if the value is to be ignored. */
6247
6248 static rtx
6249 arc_expand_builtin (tree exp,
6250 rtx target,
6251 rtx subtarget ATTRIBUTE_UNUSED,
6252 machine_mode mode ATTRIBUTE_UNUSED,
6253 int ignore ATTRIBUTE_UNUSED)
6254 {
6255 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0);
6256 unsigned int id = DECL_FUNCTION_CODE (fndecl);
6257 const struct arc_builtin_description *d = &arc_bdesc[id];
6258 int i, j, n_args = call_expr_nargs (exp);
6259 rtx pat = NULL_RTX;
6260 rtx xop[5];
6261 enum insn_code icode = d->icode;
6262 machine_mode tmode = insn_data[icode].operand[0].mode;
6263 int nonvoid;
6264 tree arg0;
6265 tree arg1;
6266 tree arg2;
6267 tree arg3;
6268 rtx op0;
6269 rtx op1;
6270 rtx op2;
6271 rtx op3;
6272 rtx op4;
6273 machine_mode mode0;
6274 machine_mode mode1;
6275 machine_mode mode2;
6276 machine_mode mode3;
6277 machine_mode mode4;
6278
6279 if (id >= ARC_BUILTIN_COUNT)
6280 internal_error ("bad builtin fcode");
6281
6282 /* 1st part: Expand special builtins. */
6283 switch (id)
6284 {
6285 case ARC_BUILTIN_NOP:
6286 emit_insn (gen_nopv ());
6287 return NULL_RTX;
6288
6289 case ARC_BUILTIN_RTIE:
6290 case ARC_BUILTIN_SYNC:
6291 case ARC_BUILTIN_BRK:
6292 case ARC_BUILTIN_SWI:
6293 case ARC_BUILTIN_UNIMP_S:
6294 gcc_assert (icode != 0);
6295 emit_insn (GEN_FCN (icode) (const1_rtx));
6296 return NULL_RTX;
6297
6298 case ARC_BUILTIN_ALIGNED:
6299 return arc_expand_builtin_aligned (exp);
6300
6301 case ARC_BUILTIN_CLRI:
6302 target = gen_reg_rtx (SImode);
6303 emit_insn (gen_clri (target, const1_rtx));
6304 return target;
6305
6306 case ARC_BUILTIN_TRAP_S:
6307 case ARC_BUILTIN_SLEEP:
6308 arg0 = CALL_EXPR_ARG (exp, 0);
6309 fold (arg0);
6310 op0 = expand_expr (arg0, NULL_RTX, VOIDmode, EXPAND_NORMAL);
6311
6312 if (!CONST_INT_P (op0) || !satisfies_constraint_L (op0))
6313 {
6314 error ("builtin operand should be an unsigned 6-bit value");
6315 return NULL_RTX;
6316 }
6317 gcc_assert (icode != 0);
6318 emit_insn (GEN_FCN (icode) (op0));
6319 return NULL_RTX;
6320
6321 case ARC_BUILTIN_VDORUN:
6322 case ARC_BUILTIN_VDIRUN:
6323 arg0 = CALL_EXPR_ARG (exp, 0);
6324 arg1 = CALL_EXPR_ARG (exp, 1);
6325 op0 = expand_expr (arg0, NULL_RTX, SImode, EXPAND_NORMAL);
6326 op1 = expand_expr (arg1, NULL_RTX, SImode, EXPAND_NORMAL);
6327
6328 target = gen_rtx_REG (SImode, (id == ARC_BUILTIN_VDIRUN) ? 131 : 139);
6329
6330 mode0 = insn_data[icode].operand[1].mode;
6331 mode1 = insn_data[icode].operand[2].mode;
6332
6333 if (!insn_data[icode].operand[1].predicate (op0, mode0))
6334 op0 = copy_to_mode_reg (mode0, op0);
6335
6336 if (!insn_data[icode].operand[2].predicate (op1, mode1))
6337 op1 = copy_to_mode_reg (mode1, op1);
6338
6339 pat = GEN_FCN (icode) (target, op0, op1);
6340 if (!pat)
6341 return NULL_RTX;
6342
6343 emit_insn (pat);
6344 return NULL_RTX;
6345
6346 case ARC_BUILTIN_VDIWR:
6347 case ARC_BUILTIN_VDOWR:
6348 arg0 = CALL_EXPR_ARG (exp, 0);
6349 arg1 = CALL_EXPR_ARG (exp, 1);
6350 op0 = expand_expr (arg0, NULL_RTX, SImode, EXPAND_NORMAL);
6351 op1 = expand_expr (arg1, NULL_RTX, SImode, EXPAND_NORMAL);
6352
6353 if (!CONST_INT_P (op0)
6354 || !(UNSIGNED_INT3 (INTVAL (op0))))
6355 error ("operand 1 should be an unsigned 3-bit immediate");
6356
6357 mode1 = insn_data[icode].operand[1].mode;
6358
6359 if (icode == CODE_FOR_vdiwr_insn)
6360 target = gen_rtx_REG (SImode,
6361 ARC_FIRST_SIMD_DMA_CONFIG_IN_REG + INTVAL (op0));
6362 else if (icode == CODE_FOR_vdowr_insn)
6363 target = gen_rtx_REG (SImode,
6364 ARC_FIRST_SIMD_DMA_CONFIG_OUT_REG + INTVAL (op0));
6365 else
6366 gcc_unreachable ();
6367
6368 if (!insn_data[icode].operand[2].predicate (op1, mode1))
6369 op1 = copy_to_mode_reg (mode1, op1);
6370
6371 pat = GEN_FCN (icode) (target, op1);
6372 if (!pat)
6373 return NULL_RTX;
6374
6375 emit_insn (pat);
6376 return NULL_RTX;
6377
6378 case ARC_BUILTIN_VASRW:
6379 case ARC_BUILTIN_VSR8:
6380 case ARC_BUILTIN_VSR8AW:
6381 arg0 = CALL_EXPR_ARG (exp, 0);
6382 arg1 = CALL_EXPR_ARG (exp, 1);
6383 op0 = expand_expr (arg0, NULL_RTX, V8HImode, EXPAND_NORMAL);
6384 op1 = expand_expr (arg1, NULL_RTX, SImode, EXPAND_NORMAL);
6385 op2 = gen_rtx_REG (V8HImode, ARC_FIRST_SIMD_VR_REG);
6386
6387 target = gen_reg_rtx (V8HImode);
6388 mode0 = insn_data[icode].operand[1].mode;
6389 mode1 = insn_data[icode].operand[2].mode;
6390
6391 if (!insn_data[icode].operand[1].predicate (op0, mode0))
6392 op0 = copy_to_mode_reg (mode0, op0);
6393
6394 if ((!insn_data[icode].operand[2].predicate (op1, mode1))
6395 || !(UNSIGNED_INT3 (INTVAL (op1))))
6396 error ("operand 2 should be an unsigned 3-bit value (I0-I7)");
6397
6398 pat = GEN_FCN (icode) (target, op0, op1, op2);
6399 if (!pat)
6400 return NULL_RTX;
6401
6402 emit_insn (pat);
6403 return target;
6404
6405 case ARC_BUILTIN_VLD32WH:
6406 case ARC_BUILTIN_VLD32WL:
6407 case ARC_BUILTIN_VLD64:
6408 case ARC_BUILTIN_VLD32:
6409 rtx src_vreg;
6410 icode = d->icode;
6411 arg0 = CALL_EXPR_ARG (exp, 0); /* source vreg. */
6412 arg1 = CALL_EXPR_ARG (exp, 1); /* [I]0-7. */
6413 arg2 = CALL_EXPR_ARG (exp, 2); /* u8. */
6414
6415 src_vreg = expand_expr (arg0, NULL_RTX, V8HImode, EXPAND_NORMAL);
6416 op0 = expand_expr (arg1, NULL_RTX, SImode, EXPAND_NORMAL);
6417 op1 = expand_expr (arg2, NULL_RTX, SImode, EXPAND_NORMAL);
6418 op2 = gen_rtx_REG (V8HImode, ARC_FIRST_SIMD_VR_REG);
6419
6420 /* target <- src vreg. */
6421 emit_insn (gen_move_insn (target, src_vreg));
6422
6423 /* target <- vec_concat: target, mem (Ib, u8). */
6424 mode0 = insn_data[icode].operand[3].mode;
6425 mode1 = insn_data[icode].operand[1].mode;
6426
6427 if ((!insn_data[icode].operand[3].predicate (op0, mode0))
6428 || !(UNSIGNED_INT3 (INTVAL (op0))))
6429 error ("operand 1 should be an unsigned 3-bit value (I0-I7)");
6430
6431 if ((!insn_data[icode].operand[1].predicate (op1, mode1))
6432 || !(UNSIGNED_INT8 (INTVAL (op1))))
6433 error ("operand 2 should be an unsigned 8-bit value");
6434
6435 pat = GEN_FCN (icode) (target, op1, op2, op0);
6436 if (!pat)
6437 return NULL_RTX;
6438
6439 emit_insn (pat);
6440 return target;
6441
6442 case ARC_BUILTIN_VLD64W:
6443 case ARC_BUILTIN_VLD128:
6444 arg0 = CALL_EXPR_ARG (exp, 0); /* dest vreg. */
6445 arg1 = CALL_EXPR_ARG (exp, 1); /* [I]0-7. */
6446
6447 op0 = gen_rtx_REG (V8HImode, ARC_FIRST_SIMD_VR_REG);
6448 op1 = expand_expr (arg0, NULL_RTX, SImode, EXPAND_NORMAL);
6449 op2 = expand_expr (arg1, NULL_RTX, SImode, EXPAND_NORMAL);
6450
6451 /* target <- src vreg. */
6452 target = gen_reg_rtx (V8HImode);
6453
6454 /* target <- vec_concat: target, mem (Ib, u8). */
6455 mode0 = insn_data[icode].operand[1].mode;
6456 mode1 = insn_data[icode].operand[2].mode;
6457 mode2 = insn_data[icode].operand[3].mode;
6458
6459 if ((!insn_data[icode].operand[2].predicate (op1, mode1))
6460 || !(UNSIGNED_INT3 (INTVAL (op1))))
6461 error ("operand 1 should be an unsigned 3-bit value (I0-I7)");
6462
6463 if ((!insn_data[icode].operand[3].predicate (op2, mode2))
6464 || !(UNSIGNED_INT8 (INTVAL (op2))))
6465 error ("operand 2 should be an unsigned 8-bit value");
6466
6467 pat = GEN_FCN (icode) (target, op0, op1, op2);
6468
6469 if (!pat)
6470 return NULL_RTX;
6471
6472 emit_insn (pat);
6473 return target;
6474
6475 case ARC_BUILTIN_VST128:
6476 case ARC_BUILTIN_VST64:
6477 arg0 = CALL_EXPR_ARG (exp, 0); /* src vreg. */
6478 arg1 = CALL_EXPR_ARG (exp, 1); /* [I]0-7. */
6479 arg2 = CALL_EXPR_ARG (exp, 2); /* u8. */
6480
6481 op0 = gen_rtx_REG (V8HImode, ARC_FIRST_SIMD_VR_REG);
6482 op1 = expand_expr (arg1, NULL_RTX, SImode, EXPAND_NORMAL);
6483 op2 = expand_expr (arg2, NULL_RTX, SImode, EXPAND_NORMAL);
6484 op3 = expand_expr (arg0, NULL_RTX, V8HImode, EXPAND_NORMAL);
6485
6486 mode0 = insn_data[icode].operand[0].mode;
6487 mode1 = insn_data[icode].operand[1].mode;
6488 mode2 = insn_data[icode].operand[2].mode;
6489 mode3 = insn_data[icode].operand[3].mode;
6490
6491 if ((!insn_data[icode].operand[1].predicate (op1, mode1))
6492 || !(UNSIGNED_INT3 (INTVAL (op1))))
6493 error ("operand 2 should be an unsigned 3-bit value (I0-I7)");
6494
6495 if ((!insn_data[icode].operand[2].predicate (op2, mode2))
6496 || !(UNSIGNED_INT8 (INTVAL (op2))))
6497 error ("operand 3 should be an unsigned 8-bit value");
6498
6499 if (!insn_data[icode].operand[3].predicate (op3, mode3))
6500 op3 = copy_to_mode_reg (mode3, op3);
6501
6502 pat = GEN_FCN (icode) (op0, op1, op2, op3);
6503 if (!pat)
6504 return NULL_RTX;
6505
6506 emit_insn (pat);
6507 return NULL_RTX;
6508
6509 case ARC_BUILTIN_VST16_N:
6510 case ARC_BUILTIN_VST32_N:
6511 arg0 = CALL_EXPR_ARG (exp, 0); /* source vreg. */
6512 arg1 = CALL_EXPR_ARG (exp, 1); /* u3. */
6513 arg2 = CALL_EXPR_ARG (exp, 2); /* [I]0-7. */
6514 arg3 = CALL_EXPR_ARG (exp, 3); /* u8. */
6515
6516 op0 = expand_expr (arg3, NULL_RTX, SImode, EXPAND_NORMAL);
6517 op1 = gen_rtx_REG (V8HImode, ARC_FIRST_SIMD_VR_REG);
6518 op2 = expand_expr (arg2, NULL_RTX, SImode, EXPAND_NORMAL);
6519 op3 = expand_expr (arg0, NULL_RTX, V8HImode, EXPAND_NORMAL);
6520 op4 = expand_expr (arg1, NULL_RTX, SImode, EXPAND_NORMAL);
6521
6522 mode0 = insn_data[icode].operand[0].mode;
6523 mode2 = insn_data[icode].operand[2].mode;
6524 mode3 = insn_data[icode].operand[3].mode;
6525 mode4 = insn_data[icode].operand[4].mode;
6526
6527 /* Do some correctness checks for the operands. */
6528 if ((!insn_data[icode].operand[0].predicate (op0, mode0))
6529 || !(UNSIGNED_INT8 (INTVAL (op0))))
6530 error ("operand 4 should be an unsigned 8-bit value (0-255)");
6531
6532 if ((!insn_data[icode].operand[2].predicate (op2, mode2))
6533 || !(UNSIGNED_INT3 (INTVAL (op2))))
6534 error ("operand 3 should be an unsigned 3-bit value (I0-I7)");
6535
6536 if (!insn_data[icode].operand[3].predicate (op3, mode3))
6537 op3 = copy_to_mode_reg (mode3, op3);
6538
6539 if ((!insn_data[icode].operand[4].predicate (op4, mode4))
6540 || !(UNSIGNED_INT3 (INTVAL (op4))))
6541 error ("operand 2 should be an unsigned 3-bit value (subreg 0-7)");
6542 else if (icode == CODE_FOR_vst32_n_insn
6543 && ((INTVAL (op4) % 2) != 0))
6544 error ("operand 2 should be an even 3-bit value (subreg 0,2,4,6)");
6545
6546 pat = GEN_FCN (icode) (op0, op1, op2, op3, op4);
6547 if (!pat)
6548 return NULL_RTX;
6549
6550 emit_insn (pat);
6551 return NULL_RTX;
6552
6553 default:
6554 break;
6555 }
6556
6557 /* 2nd part: Expand regular builtins. */
6558 if (icode == 0)
6559 internal_error ("bad builtin fcode");
6560
6561 nonvoid = TREE_TYPE (TREE_TYPE (fndecl)) != void_type_node;
6562 j = 0;
6563
6564 if (nonvoid)
6565 {
6566 if (target == NULL_RTX
6567 || GET_MODE (target) != tmode
6568 || !insn_data[icode].operand[0].predicate (target, tmode))
6569 {
6570 target = gen_reg_rtx (tmode);
6571 }
6572 xop[j++] = target;
6573 }
6574
6575 gcc_assert (n_args <= 4);
6576 for (i = 0; i < n_args; i++, j++)
6577 {
6578 tree arg = CALL_EXPR_ARG (exp, i);
6579 machine_mode mode = insn_data[icode].operand[j].mode;
6580 rtx op = expand_expr (arg, NULL_RTX, mode, EXPAND_NORMAL);
6581 machine_mode opmode = GET_MODE (op);
6582 char c = insn_data[icode].operand[j].constraint[0];
6583
6584 /* SIMD extension requires exact immediate operand match. */
6585 if ((id > ARC_BUILTIN_SIMD_BEGIN)
6586 && (id < ARC_BUILTIN_SIMD_END)
6587 && (c != 'v')
6588 && (c != 'r'))
6589 {
6590 if (!CONST_INT_P (op))
6591 error ("builtin requires an immediate for operand %d", j);
6592 switch (c)
6593 {
6594 case 'L':
6595 if (!satisfies_constraint_L (op))
6596 error ("operand %d should be a 6 bit unsigned immediate", j);
6597 break;
6598 case 'P':
6599 if (!satisfies_constraint_P (op))
6600 error ("operand %d should be a 8 bit unsigned immediate", j);
6601 break;
6602 case 'K':
6603 if (!satisfies_constraint_K (op))
6604 error ("operand %d should be a 3 bit unsigned immediate", j);
6605 break;
6606 default:
6607 error ("unknown builtin immediate operand type for operand %d",
6608 j);
6609 }
6610 }
6611
6612 if (CONST_INT_P (op))
6613 opmode = mode;
6614
6615 if ((opmode == SImode) && (mode == HImode))
6616 {
6617 opmode = HImode;
6618 op = gen_lowpart (HImode, op);
6619 }
6620
6621 /* In case the insn wants input operands in modes different from
6622 the result, abort. */
6623 gcc_assert (opmode == mode || opmode == VOIDmode);
6624
6625 if (!insn_data[icode].operand[i + nonvoid].predicate (op, mode))
6626 op = copy_to_mode_reg (mode, op);
6627
6628 xop[j] = op;
6629 }
6630
6631 pat = apply_GEN_FCN (icode, xop);
6632 if (pat == NULL_RTX)
6633 return NULL_RTX;
6634
6635 emit_insn (pat);
6636
6637 if (nonvoid)
6638 return target;
6639 else
6640 return const0_rtx;
6641 }
6642
6643 /* Returns true if the operands[opno] is a valid compile-time constant to be
6644 used as register number in the code for builtins. Else it flags an error
6645 and returns false. */
6646
6647 bool
6648 check_if_valid_regno_const (rtx *operands, int opno)
6649 {
6650
6651 switch (GET_CODE (operands[opno]))
6652 {
6653 case SYMBOL_REF :
6654 case CONST :
6655 case CONST_INT :
6656 return true;
6657 default:
6658 error ("register number must be a compile-time constant. Try giving higher optimization levels");
6659 break;
6660 }
6661 return false;
6662 }
6663
6664 /* Check that after all the constant folding, whether the operand to
6665 __builtin_arc_sleep is an unsigned int of 6 bits. If not, flag an error. */
6666
6667 bool
6668 check_if_valid_sleep_operand (rtx *operands, int opno)
6669 {
6670 switch (GET_CODE (operands[opno]))
6671 {
6672 case CONST :
6673 case CONST_INT :
6674 if( UNSIGNED_INT6 (INTVAL (operands[opno])))
6675 return true;
6676 /* FALLTHRU */
6677 default:
6678 fatal_error (input_location,
6679 "operand for sleep instruction must be an unsigned 6 bit compile-time constant");
6680 break;
6681 }
6682 return false;
6683 }
6684
6685 /* Return true if it is ok to make a tail-call to DECL. */
6686
6687 static bool
6688 arc_function_ok_for_sibcall (tree decl ATTRIBUTE_UNUSED,
6689 tree exp ATTRIBUTE_UNUSED)
6690 {
6691 /* Never tailcall from an ISR routine - it needs a special exit sequence. */
6692 if (ARC_INTERRUPT_P (arc_compute_function_type (cfun)))
6693 return false;
6694
6695 /* Everything else is ok. */
6696 return true;
6697 }
6698
6699 /* Output code to add DELTA to the first argument, and then jump
6700 to FUNCTION. Used for C++ multiple inheritance. */
6701
6702 static void
6703 arc_output_mi_thunk (FILE *file, tree thunk ATTRIBUTE_UNUSED,
6704 HOST_WIDE_INT delta,
6705 HOST_WIDE_INT vcall_offset,
6706 tree function)
6707 {
6708 int mi_delta = delta;
6709 const char *const mi_op = mi_delta < 0 ? "sub" : "add";
6710 int shift = 0;
6711 int this_regno
6712 = aggregate_value_p (TREE_TYPE (TREE_TYPE (function)), function) ? 1 : 0;
6713 rtx fnaddr;
6714
6715 if (mi_delta < 0)
6716 mi_delta = - mi_delta;
6717
6718 /* Add DELTA. When possible use a plain add, otherwise load it into
6719 a register first. */
6720
6721 while (mi_delta != 0)
6722 {
6723 if ((mi_delta & (3 << shift)) == 0)
6724 shift += 2;
6725 else
6726 {
6727 asm_fprintf (file, "\t%s\t%s, %s, %d\n",
6728 mi_op, reg_names[this_regno], reg_names[this_regno],
6729 mi_delta & (0xff << shift));
6730 mi_delta &= ~(0xff << shift);
6731 shift += 8;
6732 }
6733 }
6734
6735 /* If needed, add *(*THIS + VCALL_OFFSET) to THIS. */
6736 if (vcall_offset != 0)
6737 {
6738 /* ld r12,[this] --> temp = *this
6739 add r12,r12,vcall_offset --> temp = *(*this + vcall_offset)
6740 ld r12,[r12]
6741 add this,this,r12 --> this+ = *(*this + vcall_offset) */
6742 asm_fprintf (file, "\tld\t%s, [%s]\n",
6743 ARC_TEMP_SCRATCH_REG, reg_names[this_regno]);
6744 asm_fprintf (file, "\tadd\t%s, %s, " HOST_WIDE_INT_PRINT_DEC "\n",
6745 ARC_TEMP_SCRATCH_REG, ARC_TEMP_SCRATCH_REG, vcall_offset);
6746 asm_fprintf (file, "\tld\t%s, [%s]\n",
6747 ARC_TEMP_SCRATCH_REG, ARC_TEMP_SCRATCH_REG);
6748 asm_fprintf (file, "\tadd\t%s, %s, %s\n", reg_names[this_regno],
6749 reg_names[this_regno], ARC_TEMP_SCRATCH_REG);
6750 }
6751
6752 fnaddr = XEXP (DECL_RTL (function), 0);
6753
6754 if (arc_is_longcall_p (fnaddr))
6755 {
6756 if (flag_pic)
6757 {
6758 asm_fprintf (file, "\tld\t%s, [pcl, @",
6759 ARC_TEMP_SCRATCH_REG);
6760 assemble_name (file, XSTR (fnaddr, 0));
6761 fputs ("@gotpc]\n", file);
6762 asm_fprintf (file, "\tj\t[%s]", ARC_TEMP_SCRATCH_REG);
6763 }
6764 else
6765 {
6766 fputs ("\tj\t@", file);
6767 assemble_name (file, XSTR (fnaddr, 0));
6768 }
6769 }
6770 else
6771 {
6772 fputs ("\tb\t@", file);
6773 assemble_name (file, XSTR (fnaddr, 0));
6774 if (flag_pic)
6775 fputs ("@plt\n", file);
6776 }
6777 fputc ('\n', file);
6778 }
6779
6780 /* Return true if a 32 bit "long_call" should be generated for
6781 this calling SYM_REF. We generate a long_call if the function:
6782
6783 a. has an __attribute__((long call))
6784 or b. the -mlong-calls command line switch has been specified
6785
6786 However we do not generate a long call if the function has an
6787 __attribute__ ((short_call)) or __attribute__ ((medium_call))
6788
6789 This function will be called by C fragments contained in the machine
6790 description file. */
6791
6792 bool
6793 arc_is_longcall_p (rtx sym_ref)
6794 {
6795 if (GET_CODE (sym_ref) != SYMBOL_REF)
6796 return false;
6797
6798 return (SYMBOL_REF_LONG_CALL_P (sym_ref)
6799 || (TARGET_LONG_CALLS_SET
6800 && !SYMBOL_REF_SHORT_CALL_P (sym_ref)
6801 && !SYMBOL_REF_MEDIUM_CALL_P (sym_ref)));
6802
6803 }
6804
6805 /* Likewise for short calls. */
6806
6807 bool
6808 arc_is_shortcall_p (rtx sym_ref)
6809 {
6810 if (GET_CODE (sym_ref) != SYMBOL_REF)
6811 return false;
6812
6813 return (SYMBOL_REF_SHORT_CALL_P (sym_ref)
6814 || (!TARGET_LONG_CALLS_SET && !TARGET_MEDIUM_CALLS
6815 && !SYMBOL_REF_LONG_CALL_P (sym_ref)
6816 && !SYMBOL_REF_MEDIUM_CALL_P (sym_ref)));
6817
6818 }
6819
6820 /* Worker function for TARGET_RETURN_IN_MEMORY. */
6821
6822 static bool
6823 arc_return_in_memory (const_tree type, const_tree fntype ATTRIBUTE_UNUSED)
6824 {
6825 if (AGGREGATE_TYPE_P (type) || TREE_ADDRESSABLE (type))
6826 return true;
6827 else
6828 {
6829 HOST_WIDE_INT size = int_size_in_bytes (type);
6830 return (size == -1 || size > (TARGET_V2 ? 16 : 8));
6831 }
6832 }
6833
6834
6835 /* This was in rtlanal.c, and can go in there when we decide we want
6836 to submit the change for inclusion in the GCC tree. */
6837 /* Like note_stores, but allow the callback to have side effects on the rtl
6838 (like the note_stores of yore):
6839 Call FUN on each register or MEM that is stored into or clobbered by X.
6840 (X would be the pattern of an insn). DATA is an arbitrary pointer,
6841 ignored by note_stores, but passed to FUN.
6842 FUN may alter parts of the RTL.
6843
6844 FUN receives three arguments:
6845 1. the REG, MEM, CC0 or PC being stored in or clobbered,
6846 2. the SET or CLOBBER rtx that does the store,
6847 3. the pointer DATA provided to note_stores.
6848
6849 If the item being stored in or clobbered is a SUBREG of a hard register,
6850 the SUBREG will be passed. */
6851
6852 /* For now. */ static
6853 void
6854 walk_stores (rtx x, void (*fun) (rtx, rtx, void *), void *data)
6855 {
6856 int i;
6857
6858 if (GET_CODE (x) == COND_EXEC)
6859 x = COND_EXEC_CODE (x);
6860
6861 if (GET_CODE (x) == SET || GET_CODE (x) == CLOBBER)
6862 {
6863 rtx dest = SET_DEST (x);
6864
6865 while ((GET_CODE (dest) == SUBREG
6866 && (!REG_P (SUBREG_REG (dest))
6867 || REGNO (SUBREG_REG (dest)) >= FIRST_PSEUDO_REGISTER))
6868 || GET_CODE (dest) == ZERO_EXTRACT
6869 || GET_CODE (dest) == STRICT_LOW_PART)
6870 dest = XEXP (dest, 0);
6871
6872 /* If we have a PARALLEL, SET_DEST is a list of EXPR_LIST expressions,
6873 each of whose first operand is a register. */
6874 if (GET_CODE (dest) == PARALLEL)
6875 {
6876 for (i = XVECLEN (dest, 0) - 1; i >= 0; i--)
6877 if (XEXP (XVECEXP (dest, 0, i), 0) != 0)
6878 (*fun) (XEXP (XVECEXP (dest, 0, i), 0), x, data);
6879 }
6880 else
6881 (*fun) (dest, x, data);
6882 }
6883
6884 else if (GET_CODE (x) == PARALLEL)
6885 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
6886 walk_stores (XVECEXP (x, 0, i), fun, data);
6887 }
6888
6889 static bool
6890 arc_pass_by_reference (cumulative_args_t ca_v ATTRIBUTE_UNUSED,
6891 machine_mode mode ATTRIBUTE_UNUSED,
6892 const_tree type,
6893 bool named ATTRIBUTE_UNUSED)
6894 {
6895 return (type != 0
6896 && (TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST
6897 || TREE_ADDRESSABLE (type)));
6898 }
6899
6900 /* Implement TARGET_CAN_USE_DOLOOP_P. */
6901
6902 static bool
6903 arc_can_use_doloop_p (const widest_int &iterations, const widest_int &,
6904 unsigned int loop_depth, bool entered_at_top)
6905 {
6906 if (loop_depth > 1)
6907 return false;
6908 /* Setting up the loop with two sr instructions costs 6 cycles. */
6909 if (TARGET_ARC700
6910 && !entered_at_top
6911 && wi::gtu_p (iterations, 0)
6912 && wi::leu_p (iterations, flag_pic ? 6 : 3))
6913 return false;
6914 return true;
6915 }
6916
6917 /* NULL if INSN insn is valid within a low-overhead loop.
6918 Otherwise return why doloop cannot be applied. */
6919
6920 static const char *
6921 arc_invalid_within_doloop (const rtx_insn *insn)
6922 {
6923 if (CALL_P (insn))
6924 return "Function call in the loop.";
6925 return NULL;
6926 }
6927
6928 /* Return true if a load instruction (CONSUMER) uses the same address as a
6929 store instruction (PRODUCER). This function is used to avoid st/ld
6930 address hazard in ARC700 cores. */
6931 bool
6932 arc_store_addr_hazard_p (rtx_insn* producer, rtx_insn* consumer)
6933 {
6934 rtx in_set, out_set;
6935 rtx out_addr, in_addr;
6936
6937 if (!producer)
6938 return false;
6939
6940 if (!consumer)
6941 return false;
6942
6943 /* Peel the producer and the consumer for the address. */
6944 out_set = single_set (producer);
6945 if (out_set)
6946 {
6947 out_addr = SET_DEST (out_set);
6948 if (!out_addr)
6949 return false;
6950 if (GET_CODE (out_addr) == ZERO_EXTEND
6951 || GET_CODE (out_addr) == SIGN_EXTEND)
6952 out_addr = XEXP (out_addr, 0);
6953
6954 if (!MEM_P (out_addr))
6955 return false;
6956
6957 in_set = single_set (consumer);
6958 if (in_set)
6959 {
6960 in_addr = SET_SRC (in_set);
6961 if (!in_addr)
6962 return false;
6963 if (GET_CODE (in_addr) == ZERO_EXTEND
6964 || GET_CODE (in_addr) == SIGN_EXTEND)
6965 in_addr = XEXP (in_addr, 0);
6966
6967 if (!MEM_P (in_addr))
6968 return false;
6969 /* Get rid of the MEM and check if the addresses are
6970 equivalent. */
6971 in_addr = XEXP (in_addr, 0);
6972 out_addr = XEXP (out_addr, 0);
6973
6974 return exp_equiv_p (in_addr, out_addr, 0, true);
6975 }
6976 }
6977 return false;
6978 }
6979
6980 /* The same functionality as arc_hazard. It is called in machine
6981 reorg before any other optimization. Hence, the NOP size is taken
6982 into account when doing branch shortening. */
6983
6984 static void
6985 workaround_arc_anomaly (void)
6986 {
6987 rtx_insn *insn, *succ0;
6988
6989 /* For any architecture: call arc_hazard here. */
6990 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
6991 {
6992 succ0 = next_real_insn (insn);
6993 if (arc_hazard (insn, succ0))
6994 {
6995 emit_insn_before (gen_nopv (), succ0);
6996 }
6997 }
6998
6999 if (TARGET_ARC700)
7000 {
7001 rtx_insn *succ1;
7002
7003 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
7004 {
7005 succ0 = next_real_insn (insn);
7006 if (arc_store_addr_hazard_p (insn, succ0))
7007 {
7008 emit_insn_after (gen_nopv (), insn);
7009 emit_insn_after (gen_nopv (), insn);
7010 continue;
7011 }
7012
7013 /* Avoid adding nops if the instruction between the ST and LD is
7014 a call or jump. */
7015 succ1 = next_real_insn (succ0);
7016 if (succ0 && !JUMP_P (succ0) && !CALL_P (succ0)
7017 && arc_store_addr_hazard_p (insn, succ1))
7018 emit_insn_after (gen_nopv (), insn);
7019 }
7020 }
7021 }
7022
7023 static int arc_reorg_in_progress = 0;
7024
7025 /* ARC's machince specific reorg function. */
7026
7027 static void
7028 arc_reorg (void)
7029 {
7030 rtx_insn *insn;
7031 rtx pattern;
7032 rtx pc_target;
7033 long offset;
7034 int changed;
7035
7036 workaround_arc_anomaly ();
7037
7038 cfun->machine->arc_reorg_started = 1;
7039 arc_reorg_in_progress = 1;
7040
7041 /* Link up loop ends with their loop start. */
7042 {
7043 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
7044 if (GET_CODE (insn) == JUMP_INSN
7045 && recog_memoized (insn) == CODE_FOR_doloop_end_i)
7046 {
7047 rtx_insn *top_label
7048 = as_a <rtx_insn *> (XEXP (XEXP (SET_SRC (XVECEXP (PATTERN (insn), 0, 0)), 1), 0));
7049 rtx num = GEN_INT (CODE_LABEL_NUMBER (top_label));
7050 rtx_insn *lp, *prev = prev_nonnote_insn (top_label);
7051 rtx_insn *lp_simple = NULL;
7052 rtx_insn *next = NULL;
7053 rtx op0 = XEXP (XVECEXP (PATTERN (insn), 0, 1), 0);
7054 int seen_label = 0;
7055
7056 for (lp = prev;
7057 (lp && NONJUMP_INSN_P (lp)
7058 && recog_memoized (lp) != CODE_FOR_doloop_begin_i);
7059 lp = prev_nonnote_insn (lp))
7060 ;
7061 if (!lp || !NONJUMP_INSN_P (lp)
7062 || dead_or_set_regno_p (lp, LP_COUNT))
7063 {
7064 HOST_WIDE_INT loop_end_id
7065 = INTVAL (XEXP (XVECEXP (PATTERN (insn), 0, 4), 0));
7066
7067 for (prev = next = insn, lp = NULL ; prev || next;)
7068 {
7069 if (prev)
7070 {
7071 if (NONJUMP_INSN_P (prev)
7072 && recog_memoized (prev) == CODE_FOR_doloop_begin_i
7073 && (INTVAL (XEXP (XVECEXP (PATTERN (prev), 0, 5), 0))
7074 == loop_end_id))
7075 {
7076 lp = prev;
7077 break;
7078 }
7079 else if (LABEL_P (prev))
7080 seen_label = 1;
7081 prev = prev_nonnote_insn (prev);
7082 }
7083 if (next)
7084 {
7085 if (NONJUMP_INSN_P (next)
7086 && recog_memoized (next) == CODE_FOR_doloop_begin_i
7087 && (INTVAL (XEXP (XVECEXP (PATTERN (next), 0, 5), 0))
7088 == loop_end_id))
7089 {
7090 lp = next;
7091 break;
7092 }
7093 next = next_nonnote_insn (next);
7094 }
7095 }
7096 prev = NULL;
7097 }
7098 else
7099 lp_simple = lp;
7100 if (lp && !dead_or_set_regno_p (lp, LP_COUNT))
7101 {
7102 rtx begin_cnt = XEXP (XVECEXP (PATTERN (lp), 0 ,3), 0);
7103 if (INTVAL (XEXP (XVECEXP (PATTERN (lp), 0, 4), 0)))
7104 /* The loop end insn has been duplicated. That can happen
7105 when there is a conditional block at the very end of
7106 the loop. */
7107 goto failure;
7108 /* If Register allocation failed to allocate to the right
7109 register, There is no point into teaching reload to
7110 fix this up with reloads, as that would cost more
7111 than using an ordinary core register with the
7112 doloop_fallback pattern. */
7113 if ((true_regnum (op0) != LP_COUNT || !REG_P (begin_cnt))
7114 /* Likewise, if the loop setup is evidently inside the loop,
7115 we loose. */
7116 || (!lp_simple && lp != next && !seen_label))
7117 {
7118 remove_insn (lp);
7119 goto failure;
7120 }
7121 /* It is common that the optimizers copy the loop count from
7122 another register, and doloop_begin_i is stuck with the
7123 source of the move. Making doloop_begin_i only accept "l"
7124 is nonsentical, as this then makes reload evict the pseudo
7125 used for the loop end. The underlying cause is that the
7126 optimizers don't understand that the register allocation for
7127 doloop_begin_i should be treated as part of the loop.
7128 Try to work around this problem by verifying the previous
7129 move exists. */
7130 if (true_regnum (begin_cnt) != LP_COUNT)
7131 {
7132 rtx_insn *mov;
7133 rtx set, note;
7134
7135 for (mov = prev_nonnote_insn (lp); mov;
7136 mov = prev_nonnote_insn (mov))
7137 {
7138 if (!NONJUMP_INSN_P (mov))
7139 mov = 0;
7140 else if ((set = single_set (mov))
7141 && rtx_equal_p (SET_SRC (set), begin_cnt)
7142 && rtx_equal_p (SET_DEST (set), op0))
7143 break;
7144 }
7145 if (mov)
7146 {
7147 XEXP (XVECEXP (PATTERN (lp), 0 ,3), 0) = op0;
7148 note = find_regno_note (lp, REG_DEAD, REGNO (begin_cnt));
7149 if (note)
7150 remove_note (lp, note);
7151 }
7152 else
7153 {
7154 remove_insn (lp);
7155 goto failure;
7156 }
7157 }
7158 XEXP (XVECEXP (PATTERN (insn), 0, 4), 0) = num;
7159 XEXP (XVECEXP (PATTERN (lp), 0, 4), 0) = num;
7160 if (next == lp)
7161 XEXP (XVECEXP (PATTERN (lp), 0, 6), 0) = const2_rtx;
7162 else if (!lp_simple)
7163 XEXP (XVECEXP (PATTERN (lp), 0, 6), 0) = const1_rtx;
7164 else if (prev != lp)
7165 {
7166 remove_insn (lp);
7167 add_insn_after (lp, prev, NULL);
7168 }
7169 if (!lp_simple)
7170 {
7171 XEXP (XVECEXP (PATTERN (lp), 0, 7), 0)
7172 = gen_rtx_LABEL_REF (Pmode, top_label);
7173 add_reg_note (lp, REG_LABEL_OPERAND, top_label);
7174 LABEL_NUSES (top_label)++;
7175 }
7176 /* We can avoid tedious loop start / end setting for empty loops
7177 be merely setting the loop count to its final value. */
7178 if (next_active_insn (top_label) == insn)
7179 {
7180 rtx lc_set
7181 = gen_rtx_SET (XEXP (XVECEXP (PATTERN (lp), 0, 3), 0),
7182 const0_rtx);
7183
7184 rtx_insn *lc_set_insn = emit_insn_before (lc_set, insn);
7185 delete_insn (lp);
7186 delete_insn (insn);
7187 insn = lc_set_insn;
7188 }
7189 /* If the loop is non-empty with zero length, we can't make it
7190 a zero-overhead loop. That can happen for empty asms. */
7191 else
7192 {
7193 rtx_insn *scan;
7194
7195 for (scan = top_label;
7196 (scan && scan != insn
7197 && (!NONJUMP_INSN_P (scan) || !get_attr_length (scan)));
7198 scan = NEXT_INSN (scan));
7199 if (scan == insn)
7200 {
7201 remove_insn (lp);
7202 goto failure;
7203 }
7204 }
7205 }
7206 else
7207 {
7208 /* Sometimes the loop optimizer makes a complete hash of the
7209 loop. If it were only that the loop is not entered at the
7210 top, we could fix this up by setting LP_START with SR .
7211 However, if we can't find the loop begin were it should be,
7212 chances are that it does not even dominate the loop, but is
7213 inside the loop instead. Using SR there would kill
7214 performance.
7215 We use the doloop_fallback pattern here, which executes
7216 in two cycles on the ARC700 when predicted correctly. */
7217 failure:
7218 if (!REG_P (op0))
7219 {
7220 rtx op3 = XEXP (XVECEXP (PATTERN (insn), 0, 5), 0);
7221
7222 emit_insn_before (gen_move_insn (op3, op0), insn);
7223 PATTERN (insn)
7224 = gen_doloop_fallback_m (op3, JUMP_LABEL (insn), op0);
7225 }
7226 else
7227 XVEC (PATTERN (insn), 0)
7228 = gen_rtvec (2, XVECEXP (PATTERN (insn), 0, 0),
7229 XVECEXP (PATTERN (insn), 0, 1));
7230 INSN_CODE (insn) = -1;
7231 }
7232 }
7233 }
7234
7235 /* FIXME: should anticipate ccfsm action, generate special patterns for
7236 to-be-deleted branches that have no delay slot and have at least the
7237 length of the size increase forced on other insns that are conditionalized.
7238 This can also have an insn_list inside that enumerates insns which are
7239 not actually conditionalized because the destinations are dead in the
7240 not-execute case.
7241 Could also tag branches that we want to be unaligned if they get no delay
7242 slot, or even ones that we don't want to do delay slot sheduling for
7243 because we can unalign them.
7244
7245 However, there are cases when conditional execution is only possible after
7246 delay slot scheduling:
7247
7248 - If a delay slot is filled with a nocond/set insn from above, the previous
7249 basic block can become elegible for conditional execution.
7250 - If a delay slot is filled with a nocond insn from the fall-through path,
7251 the branch with that delay slot can become eligble for conditional
7252 execution (however, with the same sort of data flow analysis that dbr
7253 does, we could have figured out before that we don't need to
7254 conditionalize this insn.)
7255 - If a delay slot insn is filled with an insn from the target, the
7256 target label gets its uses decremented (even deleted if falling to zero),
7257 thus possibly creating more condexec opportunities there.
7258 Therefore, we should still be prepared to apply condexec optimization on
7259 non-prepared branches if the size increase of conditionalized insns is no
7260 more than the size saved from eliminating the branch. An invocation option
7261 could also be used to reserve a bit of extra size for condbranches so that
7262 this'll work more often (could also test in arc_reorg if the block is
7263 'close enough' to be eligible for condexec to make this likely, and
7264 estimate required size increase). */
7265 /* Generate BRcc insns, by combining cmp and Bcc insns wherever possible. */
7266 if (TARGET_NO_BRCC_SET)
7267 return;
7268
7269 do
7270 {
7271 init_insn_lengths();
7272 changed = 0;
7273
7274 if (optimize > 1 && !TARGET_NO_COND_EXEC)
7275 {
7276 arc_ifcvt ();
7277 unsigned int flags = pass_data_arc_ifcvt.todo_flags_finish;
7278 df_finish_pass ((flags & TODO_df_verify) != 0);
7279
7280 if (dump_file)
7281 {
7282 fprintf (dump_file, ";; After if conversion:\n\n");
7283 print_rtl (dump_file, get_insns ());
7284 }
7285 }
7286
7287 /* Call shorten_branches to calculate the insn lengths. */
7288 shorten_branches (get_insns());
7289 cfun->machine->ccfsm_current_insn = NULL_RTX;
7290
7291 if (!INSN_ADDRESSES_SET_P())
7292 fatal_error (input_location, "Insn addresses not set after shorten_branches");
7293
7294 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
7295 {
7296 rtx label;
7297 enum attr_type insn_type;
7298
7299 /* If a non-jump insn (or a casesi jump table), continue. */
7300 if (GET_CODE (insn) != JUMP_INSN ||
7301 GET_CODE (PATTERN (insn)) == ADDR_VEC
7302 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC)
7303 continue;
7304
7305 /* If we already have a brcc, note if it is suitable for brcc_s.
7306 Be a bit generous with the brcc_s range so that we can take
7307 advantage of any code shortening from delay slot scheduling. */
7308 if (recog_memoized (insn) == CODE_FOR_cbranchsi4_scratch)
7309 {
7310 rtx pat = PATTERN (insn);
7311 rtx op = XEXP (SET_SRC (XVECEXP (pat, 0, 0)), 0);
7312 rtx *ccp = &XEXP (XVECEXP (pat, 0, 1), 0);
7313
7314 offset = branch_dest (insn) - INSN_ADDRESSES (INSN_UID (insn));
7315 if ((offset >= -140 && offset < 140)
7316 && rtx_equal_p (XEXP (op, 1), const0_rtx)
7317 && compact_register_operand (XEXP (op, 0), VOIDmode)
7318 && equality_comparison_operator (op, VOIDmode))
7319 PUT_MODE (*ccp, CC_Zmode);
7320 else if (GET_MODE (*ccp) == CC_Zmode)
7321 PUT_MODE (*ccp, CC_ZNmode);
7322 continue;
7323 }
7324 if ((insn_type = get_attr_type (insn)) == TYPE_BRCC
7325 || insn_type == TYPE_BRCC_NO_DELAY_SLOT)
7326 continue;
7327
7328 /* OK. so we have a jump insn. */
7329 /* We need to check that it is a bcc. */
7330 /* Bcc => set (pc) (if_then_else ) */
7331 pattern = PATTERN (insn);
7332 if (GET_CODE (pattern) != SET
7333 || GET_CODE (SET_SRC (pattern)) != IF_THEN_ELSE
7334 || ANY_RETURN_P (XEXP (SET_SRC (pattern), 1)))
7335 continue;
7336
7337 /* Now check if the jump is beyond the s9 range. */
7338 if (CROSSING_JUMP_P (insn))
7339 continue;
7340 offset = branch_dest (insn) - INSN_ADDRESSES (INSN_UID (insn));
7341
7342 if(offset > 253 || offset < -254)
7343 continue;
7344
7345 pc_target = SET_SRC (pattern);
7346
7347 /* Avoid FPU instructions. */
7348 if ((GET_MODE (XEXP (XEXP (pc_target, 0), 0)) == CC_FPUmode)
7349 || (GET_MODE (XEXP (XEXP (pc_target, 0), 0)) == CC_FPU_UNEQmode))
7350 continue;
7351
7352 /* Now go back and search for the set cc insn. */
7353
7354 label = XEXP (pc_target, 1);
7355
7356 {
7357 rtx pat;
7358 rtx_insn *scan, *link_insn = NULL;
7359
7360 for (scan = PREV_INSN (insn);
7361 scan && GET_CODE (scan) != CODE_LABEL;
7362 scan = PREV_INSN (scan))
7363 {
7364 if (! INSN_P (scan))
7365 continue;
7366 pat = PATTERN (scan);
7367 if (GET_CODE (pat) == SET
7368 && cc_register (SET_DEST (pat), VOIDmode))
7369 {
7370 link_insn = scan;
7371 break;
7372 }
7373 }
7374 if (!link_insn)
7375 continue;
7376 else
7377 /* Check if this is a data dependency. */
7378 {
7379 rtx op, cc_clob_rtx, op0, op1, brcc_insn, note;
7380 rtx cmp0, cmp1;
7381
7382 /* Ok this is the set cc. copy args here. */
7383 op = XEXP (pc_target, 0);
7384
7385 op0 = cmp0 = XEXP (SET_SRC (pat), 0);
7386 op1 = cmp1 = XEXP (SET_SRC (pat), 1);
7387 if (GET_CODE (op0) == ZERO_EXTRACT
7388 && XEXP (op0, 1) == const1_rtx
7389 && (GET_CODE (op) == EQ
7390 || GET_CODE (op) == NE))
7391 {
7392 /* btst / b{eq,ne} -> bbit{0,1} */
7393 op0 = XEXP (cmp0, 0);
7394 op1 = XEXP (cmp0, 2);
7395 }
7396 else if (!register_operand (op0, VOIDmode)
7397 || !general_operand (op1, VOIDmode))
7398 continue;
7399 /* Be careful not to break what cmpsfpx_raw is
7400 trying to create for checking equality of
7401 single-precision floats. */
7402 else if (TARGET_SPFP
7403 && GET_MODE (op0) == SFmode
7404 && GET_MODE (op1) == SFmode)
7405 continue;
7406
7407 /* None of the two cmp operands should be set between the
7408 cmp and the branch. */
7409 if (reg_set_between_p (op0, link_insn, insn))
7410 continue;
7411
7412 if (reg_set_between_p (op1, link_insn, insn))
7413 continue;
7414
7415 /* Since the MODE check does not work, check that this is
7416 CC reg's last set location before insn, and also no
7417 instruction between the cmp and branch uses the
7418 condition codes. */
7419 if ((reg_set_between_p (SET_DEST (pat), link_insn, insn))
7420 || (reg_used_between_p (SET_DEST (pat), link_insn, insn)))
7421 continue;
7422
7423 /* CC reg should be dead after insn. */
7424 if (!find_regno_note (insn, REG_DEAD, CC_REG))
7425 continue;
7426
7427 op = gen_rtx_fmt_ee (GET_CODE (op),
7428 GET_MODE (op), cmp0, cmp1);
7429 /* If we create a LIMM where there was none before,
7430 we only benefit if we can avoid a scheduling bubble
7431 for the ARC600. Otherwise, we'd only forgo chances
7432 at short insn generation, and risk out-of-range
7433 branches. */
7434 if (!brcc_nolimm_operator (op, VOIDmode)
7435 && !long_immediate_operand (op1, VOIDmode)
7436 && (TARGET_ARC700
7437 || next_active_insn (link_insn) != insn))
7438 continue;
7439
7440 /* Emit bbit / brcc (or brcc_s if possible).
7441 CC_Zmode indicates that brcc_s is possible. */
7442
7443 if (op0 != cmp0)
7444 cc_clob_rtx = gen_rtx_REG (CC_ZNmode, CC_REG);
7445 else if ((offset >= -140 && offset < 140)
7446 && rtx_equal_p (op1, const0_rtx)
7447 && compact_register_operand (op0, VOIDmode)
7448 && (GET_CODE (op) == EQ
7449 || GET_CODE (op) == NE))
7450 cc_clob_rtx = gen_rtx_REG (CC_Zmode, CC_REG);
7451 else
7452 cc_clob_rtx = gen_rtx_REG (CCmode, CC_REG);
7453
7454 brcc_insn
7455 = gen_rtx_IF_THEN_ELSE (VOIDmode, op, label, pc_rtx);
7456 brcc_insn = gen_rtx_SET (pc_rtx, brcc_insn);
7457 cc_clob_rtx = gen_rtx_CLOBBER (VOIDmode, cc_clob_rtx);
7458 brcc_insn
7459 = gen_rtx_PARALLEL
7460 (VOIDmode, gen_rtvec (2, brcc_insn, cc_clob_rtx));
7461 brcc_insn = emit_jump_insn_before (brcc_insn, insn);
7462
7463 JUMP_LABEL (brcc_insn) = JUMP_LABEL (insn);
7464 note = find_reg_note (insn, REG_BR_PROB, 0);
7465 if (note)
7466 {
7467 XEXP (note, 1) = REG_NOTES (brcc_insn);
7468 REG_NOTES (brcc_insn) = note;
7469 }
7470 note = find_reg_note (link_insn, REG_DEAD, op0);
7471 if (note)
7472 {
7473 remove_note (link_insn, note);
7474 XEXP (note, 1) = REG_NOTES (brcc_insn);
7475 REG_NOTES (brcc_insn) = note;
7476 }
7477 note = find_reg_note (link_insn, REG_DEAD, op1);
7478 if (note)
7479 {
7480 XEXP (note, 1) = REG_NOTES (brcc_insn);
7481 REG_NOTES (brcc_insn) = note;
7482 }
7483
7484 changed = 1;
7485
7486 /* Delete the bcc insn. */
7487 set_insn_deleted (insn);
7488
7489 /* Delete the cmp insn. */
7490 set_insn_deleted (link_insn);
7491
7492 }
7493 }
7494 }
7495 /* Clear out insn_addresses. */
7496 INSN_ADDRESSES_FREE ();
7497
7498 } while (changed);
7499
7500 if (INSN_ADDRESSES_SET_P())
7501 fatal_error (input_location, "insn addresses not freed");
7502
7503 arc_reorg_in_progress = 0;
7504 }
7505
7506 /* Check if the operands are valid for BRcc.d generation
7507 Valid Brcc.d patterns are
7508 Brcc.d b, c, s9
7509 Brcc.d b, u6, s9
7510
7511 For cc={GT, LE, GTU, LEU}, u6=63 can not be allowed,
7512 since they are encoded by the assembler as {GE, LT, HS, LS} 64, which
7513 does not have a delay slot
7514
7515 Assumed precondition: Second operand is either a register or a u6 value. */
7516
7517 bool
7518 valid_brcc_with_delay_p (rtx *operands)
7519 {
7520 if (optimize_size && GET_MODE (operands[4]) == CC_Zmode)
7521 return false;
7522 return brcc_nolimm_operator (operands[0], VOIDmode);
7523 }
7524
7525 /* Implement TARGET_IN_SMALL_DATA_P. Return true if it would be safe to
7526 access DECL using %gp_rel(...)($gp). */
7527
7528 static bool
7529 arc_in_small_data_p (const_tree decl)
7530 {
7531 HOST_WIDE_INT size;
7532
7533 /* Only variables are going into small data area. */
7534 if (TREE_CODE (decl) != VAR_DECL)
7535 return false;
7536
7537 if (TARGET_NO_SDATA_SET)
7538 return false;
7539
7540 /* Disable sdata references to weak variables. */
7541 if (DECL_WEAK (decl))
7542 return false;
7543
7544 /* Don't put constants into the small data section: we want them to
7545 be in ROM rather than RAM. */
7546 if (TREE_READONLY (decl))
7547 return false;
7548
7549 /* To ensure -mvolatile-cache works ld.di does not have a
7550 gp-relative variant. */
7551 if (!TARGET_VOLATILE_CACHE_SET
7552 && TREE_THIS_VOLATILE (decl))
7553 return false;
7554
7555 if (DECL_SECTION_NAME (decl) != 0)
7556 {
7557 const char *name = DECL_SECTION_NAME (decl);
7558 if (strcmp (name, ".sdata") == 0
7559 || strcmp (name, ".sbss") == 0)
7560 return true;
7561 }
7562 /* If it's not public, there's no need to put it in the small data
7563 section. */
7564 else if (TREE_PUBLIC (decl))
7565 {
7566 size = int_size_in_bytes (TREE_TYPE (decl));
7567 return (size > 0 && size <= g_switch_value);
7568 }
7569 return false;
7570 }
7571
7572 /* Return true if X is a small data address that can be rewritten
7573 as a gp+symref. */
7574
7575 static bool
7576 arc_rewrite_small_data_p (const_rtx x)
7577 {
7578 if (GET_CODE (x) == CONST)
7579 x = XEXP (x, 0);
7580
7581 if (GET_CODE (x) == PLUS)
7582 {
7583 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
7584 x = XEXP (x, 0);
7585 }
7586
7587 if (GET_CODE (x) == SYMBOL_REF && SYMBOL_REF_SMALL_P (x))
7588 {
7589 gcc_assert (SYMBOL_REF_TLS_MODEL (x) == 0);
7590 return true;
7591 }
7592 return false;
7593 }
7594
7595 /* If possible, rewrite OP so that it refers to small data using
7596 explicit relocations. */
7597
7598 static rtx
7599 arc_rewrite_small_data_1 (rtx op)
7600 {
7601 rtx rgp = gen_rtx_REG (Pmode, SDATA_BASE_REGNUM);
7602 op = copy_insn (op);
7603 subrtx_ptr_iterator::array_type array;
7604 FOR_EACH_SUBRTX_PTR (iter, array, &op, ALL)
7605 {
7606 rtx *loc = *iter;
7607 if (arc_rewrite_small_data_p (*loc))
7608 {
7609 *loc = gen_rtx_PLUS (Pmode, rgp, *loc);
7610 iter.skip_subrtxes ();
7611 }
7612 else if (GET_CODE (*loc) == PLUS
7613 && rtx_equal_p (XEXP (*loc, 0), rgp))
7614 iter.skip_subrtxes ();
7615 }
7616 return op;
7617 }
7618
7619 rtx
7620 arc_rewrite_small_data (rtx op)
7621 {
7622 op = arc_rewrite_small_data_1 (op);
7623
7624 /* Check if we fit small data constraints. */
7625 if (MEM_P (op)
7626 && !LEGITIMATE_SMALL_DATA_ADDRESS_P (XEXP (op, 0)))
7627 {
7628 rtx addr = XEXP (op, 0);
7629 rtx tmp = gen_reg_rtx (Pmode);
7630 emit_move_insn (tmp, addr);
7631 op = replace_equiv_address_nv (op, tmp);
7632 }
7633 return op;
7634 }
7635
7636 /* Return true if OP refers to small data symbols directly, not through
7637 a PLUS. */
7638
7639 bool
7640 small_data_pattern (rtx op, machine_mode)
7641 {
7642 if (GET_CODE (op) == SEQUENCE)
7643 return false;
7644
7645 rtx rgp = gen_rtx_REG (Pmode, SDATA_BASE_REGNUM);
7646 subrtx_iterator::array_type array;
7647 FOR_EACH_SUBRTX (iter, array, op, ALL)
7648 {
7649 const_rtx x = *iter;
7650 if (GET_CODE (x) == PLUS
7651 && rtx_equal_p (XEXP (x, 0), rgp))
7652 iter.skip_subrtxes ();
7653 else if (arc_rewrite_small_data_p (x))
7654 return true;
7655 }
7656 return false;
7657 }
7658
7659 /* Return true if OP is an acceptable memory operand for ARCompact
7660 16-bit gp-relative load instructions.
7661 op shd look like : [r26, symref@sda]
7662 i.e. (mem (plus (reg 26) (symref with smalldata flag set))
7663 */
7664 /* volatile cache option still to be handled. */
7665
7666 bool
7667 compact_sda_memory_operand (rtx op, machine_mode mode, bool short_p)
7668 {
7669 rtx addr;
7670 int size;
7671 tree decl = NULL_TREE;
7672 int align = 0;
7673 int mask = 0;
7674
7675 /* Eliminate non-memory operations. */
7676 if (GET_CODE (op) != MEM)
7677 return false;
7678
7679 if (mode == VOIDmode)
7680 mode = GET_MODE (op);
7681
7682 size = GET_MODE_SIZE (mode);
7683
7684 /* dword operations really put out 2 instructions, so eliminate them. */
7685 if (size > UNITS_PER_WORD)
7686 return false;
7687
7688 /* Decode the address now. */
7689 addr = XEXP (op, 0);
7690
7691 if (!LEGITIMATE_SMALL_DATA_ADDRESS_P (addr))
7692 return false;
7693
7694 if (!short_p || size == 1)
7695 return true;
7696
7697 /* Now check for the alignment, the short loads using gp require the
7698 addresses to be aligned. */
7699 if (GET_CODE (XEXP (addr, 1)) == SYMBOL_REF)
7700 decl = SYMBOL_REF_DECL (XEXP (addr, 1));
7701 else if (GET_CODE (XEXP (XEXP (XEXP (addr, 1), 0), 0)) == SYMBOL_REF)
7702 decl = SYMBOL_REF_DECL (XEXP (XEXP (XEXP (addr, 1), 0), 0));
7703 if (decl)
7704 align = DECL_ALIGN (decl);
7705 align = align / BITS_PER_UNIT;
7706
7707 switch (mode)
7708 {
7709 case E_HImode:
7710 mask = 1;
7711 break;
7712 default:
7713 mask = 3;
7714 break;
7715 }
7716
7717 if (align && ((align & mask) == 0))
7718 return true;
7719 return false;
7720 }
7721
7722 /* Implement ASM_OUTPUT_ALIGNED_DECL_LOCAL. */
7723
7724 void
7725 arc_asm_output_aligned_decl_local (FILE * stream, tree decl, const char * name,
7726 unsigned HOST_WIDE_INT size,
7727 unsigned HOST_WIDE_INT align,
7728 unsigned HOST_WIDE_INT globalize_p)
7729 {
7730 int in_small_data = arc_in_small_data_p (decl);
7731
7732 if (in_small_data)
7733 switch_to_section (get_named_section (NULL, ".sbss", 0));
7734 /* named_section (0,".sbss",0); */
7735 else
7736 switch_to_section (bss_section);
7737
7738 if (globalize_p)
7739 (*targetm.asm_out.globalize_label) (stream, name);
7740
7741 ASM_OUTPUT_ALIGN (stream, floor_log2 ((align) / BITS_PER_UNIT));
7742 ASM_OUTPUT_TYPE_DIRECTIVE (stream, name, "object");
7743 ASM_OUTPUT_SIZE_DIRECTIVE (stream, name, size);
7744 ASM_OUTPUT_LABEL (stream, name);
7745
7746 if (size != 0)
7747 ASM_OUTPUT_SKIP (stream, size);
7748 }
7749
7750 static bool
7751 arc_preserve_reload_p (rtx in)
7752 {
7753 return (GET_CODE (in) == PLUS
7754 && RTX_OK_FOR_BASE_P (XEXP (in, 0), true)
7755 && CONST_INT_P (XEXP (in, 1))
7756 && !((INTVAL (XEXP (in, 1)) & 511)));
7757 }
7758
7759 int
7760 arc_register_move_cost (machine_mode,
7761 enum reg_class from_class, enum reg_class to_class)
7762 {
7763 /* The ARC600 has no bypass for extension registers, hence a nop might be
7764 needed to be inserted after a write so that reads are safe. */
7765 if (TARGET_ARC600)
7766 {
7767 if (to_class == MPY_WRITABLE_CORE_REGS)
7768 return 3;
7769 /* Instructions modifying LP_COUNT need 4 additional cycles before
7770 the register will actually contain the value. */
7771 else if (to_class == LPCOUNT_REG)
7772 return 6;
7773 else if (to_class == WRITABLE_CORE_REGS)
7774 return 6;
7775 }
7776
7777 /* The ARC700 stalls for 3 cycles when *reading* from lp_count. */
7778 if (TARGET_ARC700
7779 && (from_class == LPCOUNT_REG || from_class == ALL_CORE_REGS
7780 || from_class == WRITABLE_CORE_REGS))
7781 return 8;
7782
7783 /* Force an attempt to 'mov Dy,Dx' to spill. */
7784 if ((TARGET_ARC700 || TARGET_EM) && TARGET_DPFP
7785 && from_class == DOUBLE_REGS && to_class == DOUBLE_REGS)
7786 return 100;
7787
7788 return 2;
7789 }
7790
7791 /* Emit code for an addsi3 instruction with OPERANDS.
7792 COND_P indicates if this will use conditional execution.
7793 Return the length of the instruction.
7794 If OUTPUT_P is false, don't actually output the instruction, just return
7795 its length. */
7796 int
7797 arc_output_addsi (rtx *operands, bool cond_p, bool output_p)
7798 {
7799 char format[35];
7800
7801 int match = operands_match_p (operands[0], operands[1]);
7802 int match2 = operands_match_p (operands[0], operands[2]);
7803 int intval = (REG_P (operands[2]) ? 1
7804 : CONST_INT_P (operands[2]) ? INTVAL (operands[2]) : 0xbadc057);
7805 int neg_intval = -intval;
7806 int short_0 = satisfies_constraint_Rcq (operands[0]);
7807 int short_p = (!cond_p && short_0 && satisfies_constraint_Rcq (operands[1]));
7808 int ret = 0;
7809
7810 #define REG_H_P(OP) (REG_P (OP) && ((TARGET_V2 && REGNO (OP) <= 31 \
7811 && REGNO (OP) != 30) \
7812 || !TARGET_V2))
7813
7814 #define ADDSI_OUTPUT1(FORMAT) do {\
7815 if (output_p) \
7816 output_asm_insn (FORMAT, operands);\
7817 return ret; \
7818 } while (0)
7819 #define ADDSI_OUTPUT(LIST) do {\
7820 if (output_p) \
7821 sprintf LIST;\
7822 ADDSI_OUTPUT1 (format);\
7823 return ret; \
7824 } while (0)
7825
7826 /* First try to emit a 16 bit insn. */
7827 ret = 2;
7828 if (!cond_p
7829 /* If we are actually about to output this insn, don't try a 16 bit
7830 variant if we already decided that we don't want that
7831 (I.e. we upsized this insn to align some following insn.)
7832 E.g. add_s r0,sp,70 is 16 bit, but add r0,sp,70 requires a LIMM -
7833 but add1 r0,sp,35 doesn't. */
7834 && (!output_p || (get_attr_length (current_output_insn) & 2)))
7835 {
7836 /* Generate add_s a,b,c; add_s b,b,u7; add_s c,b,u3; add_s b,b,h
7837 patterns. */
7838 if (short_p
7839 && ((REG_H_P (operands[2])
7840 && (match || satisfies_constraint_Rcq (operands[2])))
7841 || (CONST_INT_P (operands[2])
7842 && ((unsigned) intval <= (match ? 127 : 7)))))
7843 ADDSI_OUTPUT1 ("add%? %0,%1,%2 ;1");
7844
7845 /* Generate add_s b,b,h patterns. */
7846 if (short_0 && match2 && REG_H_P (operands[1]))
7847 ADDSI_OUTPUT1 ("add%? %0,%2,%1 ;2");
7848
7849 /* Generate add_s b,sp,u7; add_s sp,sp,u7 patterns. */
7850 if ((short_0 || REGNO (operands[0]) == STACK_POINTER_REGNUM)
7851 && REGNO (operands[1]) == STACK_POINTER_REGNUM && !(intval & ~124))
7852 ADDSI_OUTPUT1 ("add%? %0,%1,%2 ;3");
7853
7854 if ((short_p && (unsigned) neg_intval <= (match ? 31 : 7))
7855 || (REGNO (operands[0]) == STACK_POINTER_REGNUM
7856 && match && !(neg_intval & ~124)))
7857 ADDSI_OUTPUT1 ("sub%? %0,%1,%n2 ;4");
7858
7859 /* Generate add_s h,h,s3 patterns. */
7860 if (REG_H_P (operands[0]) && match && TARGET_V2
7861 && CONST_INT_P (operands[2]) && ((intval>= -1) && (intval <= 6)))
7862 ADDSI_OUTPUT1 ("add%? %0,%1,%2 ;5");
7863
7864 /* Generate add_s r0,b,u6; add_s r1,b,u6 patterns. */
7865 if (TARGET_CODE_DENSITY && REG_P (operands[0]) && REG_P (operands[1])
7866 && ((REGNO (operands[0]) == 0) || (REGNO (operands[0]) == 1))
7867 && satisfies_constraint_Rcq (operands[1])
7868 && satisfies_constraint_L (operands[2]))
7869 ADDSI_OUTPUT1 ("add%? %0,%1,%2 ;6");
7870 }
7871
7872 /* Now try to emit a 32 bit insn without long immediate. */
7873 ret = 4;
7874 if (!match && match2 && REG_P (operands[1]))
7875 ADDSI_OUTPUT1 ("add%? %0,%2,%1");
7876 if (match || !cond_p)
7877 {
7878 int limit = (match && !cond_p) ? 0x7ff : 0x3f;
7879 int range_factor = neg_intval & intval;
7880 int shift;
7881
7882 if (intval == (HOST_WIDE_INT) (HOST_WIDE_INT_M1U << 31))
7883 ADDSI_OUTPUT1 ("bxor%? %0,%1,31");
7884
7885 /* If we can use a straight add / sub instead of a {add,sub}[123] of
7886 same size, do, so - the insn latency is lower. */
7887 /* -0x800 is a 12-bit constant for add /add3 / sub / sub3, but
7888 0x800 is not. */
7889 if ((intval >= 0 && intval <= limit)
7890 || (intval == -0x800 && limit == 0x7ff))
7891 ADDSI_OUTPUT1 ("add%? %0,%1,%2");
7892 else if ((intval < 0 && neg_intval <= limit)
7893 || (intval == 0x800 && limit == 0x7ff))
7894 ADDSI_OUTPUT1 ("sub%? %0,%1,%n2");
7895 shift = range_factor >= 8 ? 3 : (range_factor >> 1);
7896 gcc_assert (shift == 0 || shift == 1 || shift == 2 || shift == 3);
7897 gcc_assert ((((1 << shift) - 1) & intval) == 0);
7898 if (((intval < 0 && intval != -0x4000)
7899 /* sub[123] is slower than add_s / sub, only use it if it
7900 avoids a long immediate. */
7901 && neg_intval <= limit << shift)
7902 || (intval == 0x4000 && limit == 0x7ff))
7903 ADDSI_OUTPUT ((format, "sub%d%%? %%0,%%1,%d",
7904 shift, neg_intval >> shift));
7905 else if ((intval >= 0 && intval <= limit << shift)
7906 || (intval == -0x4000 && limit == 0x7ff))
7907 ADDSI_OUTPUT ((format, "add%d%%? %%0,%%1,%d", shift, intval >> shift));
7908 }
7909 /* Try to emit a 16 bit opcode with long immediate. */
7910 ret = 6;
7911 if (short_p && match)
7912 ADDSI_OUTPUT1 ("add%? %0,%1,%S2");
7913
7914 /* We have to use a 32 bit opcode, and with a long immediate. */
7915 ret = 8;
7916 ADDSI_OUTPUT1 (intval < 0 ? "sub%? %0,%1,%n2" : "add%? %0,%1,%S2");
7917 }
7918
7919 /* Emit code for an commutative_cond_exec instruction with OPERANDS.
7920 Return the length of the instruction.
7921 If OUTPUT_P is false, don't actually output the instruction, just return
7922 its length. */
7923 int
7924 arc_output_commutative_cond_exec (rtx *operands, bool output_p)
7925 {
7926 enum rtx_code commutative_op = GET_CODE (operands[3]);
7927 const char *pat = NULL;
7928
7929 /* Canonical rtl should not have a constant in the first operand position. */
7930 gcc_assert (!CONSTANT_P (operands[1]));
7931
7932 switch (commutative_op)
7933 {
7934 case AND:
7935 if (satisfies_constraint_C1p (operands[2]))
7936 pat = "bmsk%? %0,%1,%Z2";
7937 else if (satisfies_constraint_C2p (operands[2]))
7938 {
7939 operands[2] = GEN_INT ((~INTVAL (operands[2])));
7940 pat = "bmskn%? %0,%1,%Z2";
7941 }
7942 else if (satisfies_constraint_Ccp (operands[2]))
7943 pat = "bclr%? %0,%1,%M2";
7944 else if (satisfies_constraint_CnL (operands[2]))
7945 pat = "bic%? %0,%1,%n2-1";
7946 break;
7947 case IOR:
7948 if (satisfies_constraint_C0p (operands[2]))
7949 pat = "bset%? %0,%1,%z2";
7950 break;
7951 case XOR:
7952 if (satisfies_constraint_C0p (operands[2]))
7953 pat = "bxor%? %0,%1,%z2";
7954 break;
7955 case PLUS:
7956 return arc_output_addsi (operands, true, output_p);
7957 default: break;
7958 }
7959 if (output_p)
7960 output_asm_insn (pat ? pat : "%O3.%d5 %0,%1,%2", operands);
7961 if (pat || REG_P (operands[2]) || satisfies_constraint_L (operands[2]))
7962 return 4;
7963 return 8;
7964 }
7965
7966 /* Helper function of arc_expand_movmem. ADDR points to a chunk of memory.
7967 Emit code and return an potentially modified address such that offsets
7968 up to SIZE are can be added to yield a legitimate address.
7969 if REUSE is set, ADDR is a register that may be modified. */
7970
7971 static rtx
7972 force_offsettable (rtx addr, HOST_WIDE_INT size, bool reuse)
7973 {
7974 rtx base = addr;
7975 rtx offs = const0_rtx;
7976
7977 if (GET_CODE (base) == PLUS)
7978 {
7979 offs = XEXP (base, 1);
7980 base = XEXP (base, 0);
7981 }
7982 if (!REG_P (base)
7983 || (REGNO (base) != STACK_POINTER_REGNUM
7984 && REGNO_PTR_FRAME_P (REGNO (base)))
7985 || !CONST_INT_P (offs) || !SMALL_INT (INTVAL (offs))
7986 || !SMALL_INT (INTVAL (offs) + size))
7987 {
7988 if (reuse)
7989 emit_insn (gen_add2_insn (addr, offs));
7990 else
7991 addr = copy_to_mode_reg (Pmode, addr);
7992 }
7993 return addr;
7994 }
7995
7996 /* Like move_by_pieces, but take account of load latency, and actual
7997 offset ranges. Return true on success. */
7998
7999 bool
8000 arc_expand_movmem (rtx *operands)
8001 {
8002 rtx dst = operands[0];
8003 rtx src = operands[1];
8004 rtx dst_addr, src_addr;
8005 HOST_WIDE_INT size;
8006 int align = INTVAL (operands[3]);
8007 unsigned n_pieces;
8008 int piece = align;
8009 rtx store[2];
8010 rtx tmpx[2];
8011 int i;
8012
8013 if (!CONST_INT_P (operands[2]))
8014 return false;
8015 size = INTVAL (operands[2]);
8016 /* move_by_pieces_ninsns is static, so we can't use it. */
8017 if (align >= 4)
8018 {
8019 if (TARGET_LL64)
8020 n_pieces = (size + 4) / 8U + ((size >> 1) & 1) + (size & 1);
8021 else
8022 n_pieces = (size + 2) / 4U + (size & 1);
8023 }
8024 else if (align == 2)
8025 n_pieces = (size + 1) / 2U;
8026 else
8027 n_pieces = size;
8028 if (n_pieces >= (unsigned int) (optimize_size ? 3 : 15))
8029 return false;
8030 /* Force 32 bit aligned and larger datum to use 64 bit transfers, if
8031 possible. */
8032 if (TARGET_LL64 && (piece >= 4) && (size >= 8))
8033 piece = 8;
8034 else if (piece > 4)
8035 piece = 4;
8036 dst_addr = force_offsettable (XEXP (operands[0], 0), size, 0);
8037 src_addr = force_offsettable (XEXP (operands[1], 0), size, 0);
8038 store[0] = store[1] = NULL_RTX;
8039 tmpx[0] = tmpx[1] = NULL_RTX;
8040 for (i = 0; size > 0; i ^= 1, size -= piece)
8041 {
8042 rtx tmp;
8043 machine_mode mode;
8044
8045 while (piece > size)
8046 piece >>= 1;
8047 mode = smallest_int_mode_for_size (piece * BITS_PER_UNIT);
8048 /* If we don't re-use temporaries, the scheduler gets carried away,
8049 and the register pressure gets unnecessarily high. */
8050 if (0 && tmpx[i] && GET_MODE (tmpx[i]) == mode)
8051 tmp = tmpx[i];
8052 else
8053 tmpx[i] = tmp = gen_reg_rtx (mode);
8054 dst_addr = force_offsettable (dst_addr, piece, 1);
8055 src_addr = force_offsettable (src_addr, piece, 1);
8056 if (store[i])
8057 emit_insn (store[i]);
8058 emit_move_insn (tmp, change_address (src, mode, src_addr));
8059 store[i] = gen_move_insn (change_address (dst, mode, dst_addr), tmp);
8060 dst_addr = plus_constant (Pmode, dst_addr, piece);
8061 src_addr = plus_constant (Pmode, src_addr, piece);
8062 }
8063 if (store[i])
8064 emit_insn (store[i]);
8065 if (store[i^1])
8066 emit_insn (store[i^1]);
8067 return true;
8068 }
8069
8070 /* Prepare operands for move in MODE. Return true iff the move has
8071 been emitted. */
8072
8073 bool
8074 prepare_move_operands (rtx *operands, machine_mode mode)
8075 {
8076 /* We used to do this only for MODE_INT Modes, but addresses to floating
8077 point variables may well be in the small data section. */
8078 if (!TARGET_NO_SDATA_SET && small_data_pattern (operands[0], Pmode))
8079 operands[0] = arc_rewrite_small_data (operands[0]);
8080
8081 if (mode == SImode && SYMBOLIC_CONST (operands[1]))
8082 {
8083 prepare_pic_move (operands, SImode);
8084
8085 /* Disable any REG_EQUALs associated with the symref
8086 otherwise the optimization pass undoes the work done
8087 here and references the variable directly. */
8088 }
8089
8090 if (GET_CODE (operands[0]) != MEM
8091 && !TARGET_NO_SDATA_SET
8092 && small_data_pattern (operands[1], Pmode))
8093 {
8094 /* This is to take care of address calculations involving sdata
8095 variables. */
8096 operands[1] = arc_rewrite_small_data (operands[1]);
8097
8098 emit_insn (gen_rtx_SET (operands[0],operands[1]));
8099 /* ??? This note is useless, since it only restates the set itself.
8100 We should rather use the original SYMBOL_REF. However, there is
8101 the problem that we are lying to the compiler about these
8102 SYMBOL_REFs to start with. symbol@sda should be encoded specially
8103 so that we can tell it apart from an actual symbol. */
8104 set_unique_reg_note (get_last_insn (), REG_EQUAL, operands[1]);
8105
8106 /* Take care of the REG_EQUAL note that will be attached to mark the
8107 output reg equal to the initial symbol_ref after this code is
8108 executed. */
8109 emit_move_insn (operands[0], operands[0]);
8110 return true;
8111 }
8112
8113 if (MEM_P (operands[0])
8114 && !(reload_in_progress || reload_completed))
8115 {
8116 operands[1] = force_reg (mode, operands[1]);
8117 if (!move_dest_operand (operands[0], mode))
8118 {
8119 rtx addr = copy_to_mode_reg (Pmode, XEXP (operands[0], 0));
8120 /* This is like change_address_1 (operands[0], mode, 0, 1) ,
8121 except that we can't use that function because it is static. */
8122 rtx pat = change_address (operands[0], mode, addr);
8123 MEM_COPY_ATTRIBUTES (pat, operands[0]);
8124 operands[0] = pat;
8125 }
8126 if (!cse_not_expected)
8127 {
8128 rtx pat = XEXP (operands[0], 0);
8129
8130 pat = arc_legitimize_address_0 (pat, pat, mode);
8131 if (pat)
8132 {
8133 pat = change_address (operands[0], mode, pat);
8134 MEM_COPY_ATTRIBUTES (pat, operands[0]);
8135 operands[0] = pat;
8136 }
8137 }
8138 }
8139
8140 if (MEM_P (operands[1]) && !cse_not_expected)
8141 {
8142 rtx pat = XEXP (operands[1], 0);
8143
8144 pat = arc_legitimize_address_0 (pat, pat, mode);
8145 if (pat)
8146 {
8147 pat = change_address (operands[1], mode, pat);
8148 MEM_COPY_ATTRIBUTES (pat, operands[1]);
8149 operands[1] = pat;
8150 }
8151 }
8152
8153 return false;
8154 }
8155
8156 /* Prepare OPERANDS for an extension using CODE to OMODE.
8157 Return true iff the move has been emitted. */
8158
8159 bool
8160 prepare_extend_operands (rtx *operands, enum rtx_code code,
8161 machine_mode omode)
8162 {
8163 if (!TARGET_NO_SDATA_SET && small_data_pattern (operands[1], Pmode))
8164 {
8165 /* This is to take care of address calculations involving sdata
8166 variables. */
8167 operands[1]
8168 = gen_rtx_fmt_e (code, omode, arc_rewrite_small_data (operands[1]));
8169 emit_insn (gen_rtx_SET (operands[0], operands[1]));
8170 set_unique_reg_note (get_last_insn (), REG_EQUAL, operands[1]);
8171
8172 /* Take care of the REG_EQUAL note that will be attached to mark the
8173 output reg equal to the initial extension after this code is
8174 executed. */
8175 emit_move_insn (operands[0], operands[0]);
8176 return true;
8177 }
8178 return false;
8179 }
8180
8181 /* Output a library call to a function called FNAME that has been arranged
8182 to be local to any dso. */
8183
8184 const char *
8185 arc_output_libcall (const char *fname)
8186 {
8187 unsigned len = strlen (fname);
8188 static char buf[64];
8189
8190 gcc_assert (len < sizeof buf - 35);
8191 if (TARGET_LONG_CALLS_SET
8192 || (TARGET_MEDIUM_CALLS && arc_ccfsm_cond_exec_p ()))
8193 {
8194 if (flag_pic)
8195 sprintf (buf, "add r12,pcl,@%s@pcl\n\tjl%%!%%* [r12]", fname);
8196 else
8197 sprintf (buf, "jl%%! @%s", fname);
8198 }
8199 else
8200 sprintf (buf, "bl%%!%%* @%s", fname);
8201 return buf;
8202 }
8203
8204 /* Return the SImode highpart of the DImode value IN. */
8205
8206 rtx
8207 disi_highpart (rtx in)
8208 {
8209 return simplify_gen_subreg (SImode, in, DImode, TARGET_BIG_ENDIAN ? 0 : 4);
8210 }
8211
8212 /* Return length adjustment for INSN.
8213 For ARC600:
8214 A write to a core reg greater or equal to 32 must not be immediately
8215 followed by a use. Anticipate the length requirement to insert a nop
8216 between PRED and SUCC to prevent a hazard. */
8217
8218 static int
8219 arc600_corereg_hazard (rtx_insn *pred, rtx_insn *succ)
8220 {
8221 if (!TARGET_ARC600)
8222 return 0;
8223 /* If SUCC is a doloop_end_i with a preceding label, we must output a nop
8224 in front of SUCC anyway, so there will be separation between PRED and
8225 SUCC. */
8226 if (recog_memoized (succ) == CODE_FOR_doloop_end_i
8227 && LABEL_P (prev_nonnote_insn (succ)))
8228 return 0;
8229 if (recog_memoized (succ) == CODE_FOR_doloop_begin_i)
8230 return 0;
8231 if (GET_CODE (PATTERN (pred)) == SEQUENCE)
8232 pred = as_a <rtx_sequence *> (PATTERN (pred))->insn (1);
8233 if (GET_CODE (PATTERN (succ)) == SEQUENCE)
8234 succ = as_a <rtx_sequence *> (PATTERN (succ))->insn (0);
8235 if (recog_memoized (pred) == CODE_FOR_mulsi_600
8236 || recog_memoized (pred) == CODE_FOR_umul_600
8237 || recog_memoized (pred) == CODE_FOR_mac_600
8238 || recog_memoized (pred) == CODE_FOR_mul64_600
8239 || recog_memoized (pred) == CODE_FOR_mac64_600
8240 || recog_memoized (pred) == CODE_FOR_umul64_600
8241 || recog_memoized (pred) == CODE_FOR_umac64_600)
8242 return 0;
8243 subrtx_iterator::array_type array;
8244 FOR_EACH_SUBRTX (iter, array, PATTERN (pred), NONCONST)
8245 {
8246 const_rtx x = *iter;
8247 switch (GET_CODE (x))
8248 {
8249 case SET: case POST_INC: case POST_DEC: case PRE_INC: case PRE_DEC:
8250 break;
8251 default:
8252 /* This is also fine for PRE/POST_MODIFY, because they
8253 contain a SET. */
8254 continue;
8255 }
8256 rtx dest = XEXP (x, 0);
8257 /* Check if this sets a an extension register. N.B. we use 61 for the
8258 condition codes, which is definitely not an extension register. */
8259 if (REG_P (dest) && REGNO (dest) >= 32 && REGNO (dest) < 61
8260 /* Check if the same register is used by the PAT. */
8261 && (refers_to_regno_p
8262 (REGNO (dest),
8263 REGNO (dest) + (GET_MODE_SIZE (GET_MODE (dest)) + 3) / 4U,
8264 PATTERN (succ), 0)))
8265 return 4;
8266 }
8267 return 0;
8268 }
8269
8270 /* Given a rtx, check if it is an assembly instruction or not. */
8271
8272 static int
8273 arc_asm_insn_p (rtx x)
8274 {
8275 int i, j;
8276
8277 if (x == 0)
8278 return 0;
8279
8280 switch (GET_CODE (x))
8281 {
8282 case ASM_OPERANDS:
8283 case ASM_INPUT:
8284 return 1;
8285
8286 case SET:
8287 return arc_asm_insn_p (SET_SRC (x));
8288
8289 case PARALLEL:
8290 j = 0;
8291 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
8292 j += arc_asm_insn_p (XVECEXP (x, 0, i));
8293 if ( j > 0)
8294 return 1;
8295 break;
8296
8297 default:
8298 break;
8299 }
8300
8301 return 0;
8302 }
8303
8304 /* We might have a CALL to a non-returning function before a loop end.
8305 ??? Although the manual says that's OK (the target is outside the
8306 loop, and the loop counter unused there), the assembler barfs on
8307 this for ARC600, so we must insert a nop before such a call too.
8308 For ARC700, and ARCv2 is not allowed to have the last ZOL
8309 instruction a jump to a location where lp_count is modified. */
8310
8311 static bool
8312 arc_loop_hazard (rtx_insn *pred, rtx_insn *succ)
8313 {
8314 rtx_insn *jump = NULL;
8315 rtx label_rtx = NULL_RTX;
8316 rtx_insn *label = NULL;
8317 basic_block succ_bb;
8318
8319 if (recog_memoized (succ) != CODE_FOR_doloop_end_i)
8320 return false;
8321
8322 /* Phase 1: ARC600 and ARCv2HS doesn't allow any control instruction
8323 (i.e., jump/call) as the last instruction of a ZOL. */
8324 if (TARGET_ARC600 || TARGET_HS)
8325 if (JUMP_P (pred) || CALL_P (pred)
8326 || arc_asm_insn_p (PATTERN (pred))
8327 || GET_CODE (PATTERN (pred)) == SEQUENCE)
8328 return true;
8329
8330 /* Phase 2: Any architecture, it is not allowed to have the last ZOL
8331 instruction a jump to a location where lp_count is modified. */
8332
8333 /* Phase 2a: Dig for the jump instruction. */
8334 if (JUMP_P (pred))
8335 jump = pred;
8336 else if (GET_CODE (PATTERN (pred)) == SEQUENCE
8337 && JUMP_P (XVECEXP (PATTERN (pred), 0, 0)))
8338 jump = as_a <rtx_insn *> (XVECEXP (PATTERN (pred), 0, 0));
8339 else
8340 return false;
8341
8342 /* Phase 2b: Make sure is not a millicode jump. */
8343 if ((GET_CODE (PATTERN (jump)) == PARALLEL)
8344 && (XVECEXP (PATTERN (jump), 0, 0) == ret_rtx))
8345 return false;
8346
8347 label_rtx = JUMP_LABEL (jump);
8348 if (!label_rtx)
8349 return false;
8350
8351 /* Phase 2c: Make sure is not a return. */
8352 if (ANY_RETURN_P (label_rtx))
8353 return false;
8354
8355 /* Pahse 2d: Go to the target of the jump and check for aliveness of
8356 LP_COUNT register. */
8357 label = safe_as_a <rtx_insn *> (label_rtx);
8358 succ_bb = BLOCK_FOR_INSN (label);
8359 if (!succ_bb)
8360 {
8361 gcc_assert (NEXT_INSN (label));
8362 if (NOTE_INSN_BASIC_BLOCK_P (NEXT_INSN (label)))
8363 succ_bb = NOTE_BASIC_BLOCK (NEXT_INSN (label));
8364 else
8365 succ_bb = BLOCK_FOR_INSN (NEXT_INSN (label));
8366 }
8367
8368 if (succ_bb && REGNO_REG_SET_P (df_get_live_out (succ_bb), LP_COUNT))
8369 return true;
8370
8371 return false;
8372 }
8373
8374 /* For ARC600:
8375 A write to a core reg greater or equal to 32 must not be immediately
8376 followed by a use. Anticipate the length requirement to insert a nop
8377 between PRED and SUCC to prevent a hazard. */
8378
8379 int
8380 arc_hazard (rtx_insn *pred, rtx_insn *succ)
8381 {
8382 if (!pred || !INSN_P (pred) || !succ || !INSN_P (succ))
8383 return 0;
8384
8385 if (arc_loop_hazard (pred, succ))
8386 return 4;
8387
8388 if (TARGET_ARC600)
8389 return arc600_corereg_hazard (pred, succ);
8390
8391 return 0;
8392 }
8393
8394 /* Return length adjustment for INSN. */
8395
8396 int
8397 arc_adjust_insn_length (rtx_insn *insn, int len, bool)
8398 {
8399 if (!INSN_P (insn))
8400 return len;
8401 /* We already handle sequences by ignoring the delay sequence flag. */
8402 if (GET_CODE (PATTERN (insn)) == SEQUENCE)
8403 return len;
8404
8405 /* It is impossible to jump to the very end of a Zero-Overhead Loop, as
8406 the ZOL mechanism only triggers when advancing to the end address,
8407 so if there's a label at the end of a ZOL, we need to insert a nop.
8408 The ARC600 ZOL also has extra restrictions on jumps at the end of a
8409 loop. */
8410 if (recog_memoized (insn) == CODE_FOR_doloop_end_i)
8411 {
8412 rtx_insn *prev = prev_nonnote_insn (insn);
8413
8414 return ((LABEL_P (prev)
8415 || (TARGET_ARC600
8416 && (JUMP_P (prev)
8417 || CALL_P (prev) /* Could be a noreturn call. */
8418 || (NONJUMP_INSN_P (prev)
8419 && GET_CODE (PATTERN (prev)) == SEQUENCE))))
8420 ? len + 4 : len);
8421 }
8422
8423 /* Check for return with but one preceding insn since function
8424 start / call. */
8425 if (TARGET_PAD_RETURN
8426 && JUMP_P (insn)
8427 && GET_CODE (PATTERN (insn)) != ADDR_VEC
8428 && GET_CODE (PATTERN (insn)) != ADDR_DIFF_VEC
8429 && get_attr_type (insn) == TYPE_RETURN)
8430 {
8431 rtx_insn *prev = prev_active_insn (insn);
8432
8433 if (!prev || !(prev = prev_active_insn (prev))
8434 || ((NONJUMP_INSN_P (prev)
8435 && GET_CODE (PATTERN (prev)) == SEQUENCE)
8436 ? CALL_ATTR (as_a <rtx_sequence *> (PATTERN (prev))->insn (0),
8437 NON_SIBCALL)
8438 : CALL_ATTR (prev, NON_SIBCALL)))
8439 return len + 4;
8440 }
8441 if (TARGET_ARC600)
8442 {
8443 rtx_insn *succ = next_real_insn (insn);
8444
8445 /* One the ARC600, a write to an extension register must be separated
8446 from a read. */
8447 if (succ && INSN_P (succ))
8448 len += arc600_corereg_hazard (insn, succ);
8449 }
8450
8451 /* Restore extracted operands - otherwise splitters like the addsi3_mixed one
8452 can go awry. */
8453 extract_constrain_insn_cached (insn);
8454
8455 return len;
8456 }
8457
8458 /* Values for length_sensitive. */
8459 enum
8460 {
8461 ARC_LS_NONE,// Jcc
8462 ARC_LS_25, // 25 bit offset, B
8463 ARC_LS_21, // 21 bit offset, Bcc
8464 ARC_LS_U13,// 13 bit unsigned offset, LP
8465 ARC_LS_10, // 10 bit offset, B_s, Beq_s, Bne_s
8466 ARC_LS_9, // 9 bit offset, BRcc
8467 ARC_LS_8, // 8 bit offset, BRcc_s
8468 ARC_LS_U7, // 7 bit unsigned offset, LPcc
8469 ARC_LS_7 // 7 bit offset, Bcc_s
8470 };
8471
8472 /* While the infrastructure patch is waiting for review, duplicate the
8473 struct definitions, to allow this file to compile. */
8474 #if 1
8475 typedef struct
8476 {
8477 unsigned align_set;
8478 /* Cost as a branch / call target or call return address. */
8479 int target_cost;
8480 int fallthrough_cost;
8481 int branch_cost;
8482 int length;
8483 /* 0 for not length sensitive, 1 for largest offset range,
8484 * 2 for next smaller etc. */
8485 unsigned length_sensitive : 8;
8486 bool enabled;
8487 } insn_length_variant_t;
8488
8489 typedef struct insn_length_parameters_s
8490 {
8491 int align_unit_log;
8492 int align_base_log;
8493 int max_variants;
8494 int (*get_variants) (rtx_insn *, int, bool, bool, insn_length_variant_t *);
8495 } insn_length_parameters_t;
8496
8497 static void
8498 arc_insn_length_parameters (insn_length_parameters_t *ilp) ATTRIBUTE_UNUSED;
8499 #endif
8500
8501 static int
8502 arc_get_insn_variants (rtx_insn *insn, int len, bool, bool target_p,
8503 insn_length_variant_t *ilv)
8504 {
8505 if (!NONDEBUG_INSN_P (insn))
8506 return 0;
8507 enum attr_type type;
8508 /* shorten_branches doesn't take optimize_size into account yet for the
8509 get_variants mechanism, so turn this off for now. */
8510 if (optimize_size)
8511 return 0;
8512 if (rtx_sequence *pat = dyn_cast <rtx_sequence *> (PATTERN (insn)))
8513 {
8514 /* The interaction of a short delay slot insn with a short branch is
8515 too weird for shorten_branches to piece together, so describe the
8516 entire SEQUENCE. */
8517 rtx_insn *inner;
8518 if (TARGET_UPSIZE_DBR
8519 && get_attr_length (pat->insn (1)) <= 2
8520 && (((type = get_attr_type (inner = pat->insn (0)))
8521 == TYPE_UNCOND_BRANCH)
8522 || type == TYPE_BRANCH)
8523 && get_attr_delay_slot_filled (inner) == DELAY_SLOT_FILLED_YES)
8524 {
8525 int n_variants
8526 = arc_get_insn_variants (inner, get_attr_length (inner), true,
8527 target_p, ilv+1);
8528 /* The short variant gets split into a higher-cost aligned
8529 and a lower cost unaligned variant. */
8530 gcc_assert (n_variants);
8531 gcc_assert (ilv[1].length_sensitive == ARC_LS_7
8532 || ilv[1].length_sensitive == ARC_LS_10);
8533 gcc_assert (ilv[1].align_set == 3);
8534 ilv[0] = ilv[1];
8535 ilv[0].align_set = 1;
8536 ilv[0].branch_cost += 1;
8537 ilv[1].align_set = 2;
8538 n_variants++;
8539 for (int i = 0; i < n_variants; i++)
8540 ilv[i].length += 2;
8541 /* In case an instruction with aligned size is wanted, and
8542 the short variants are unavailable / too expensive, add
8543 versions of long branch + long delay slot. */
8544 for (int i = 2, end = n_variants; i < end; i++, n_variants++)
8545 {
8546 ilv[n_variants] = ilv[i];
8547 ilv[n_variants].length += 2;
8548 }
8549 return n_variants;
8550 }
8551 return 0;
8552 }
8553 insn_length_variant_t *first_ilv = ilv;
8554 type = get_attr_type (insn);
8555 bool delay_filled
8556 = (get_attr_delay_slot_filled (insn) == DELAY_SLOT_FILLED_YES);
8557 int branch_align_cost = delay_filled ? 0 : 1;
8558 int branch_unalign_cost = delay_filled ? 0 : TARGET_UNALIGN_BRANCH ? 0 : 1;
8559 /* If the previous instruction is an sfunc call, this insn is always
8560 a target, even though the middle-end is unaware of this. */
8561 bool force_target = false;
8562 rtx_insn *prev = prev_active_insn (insn);
8563 if (prev && arc_next_active_insn (prev, 0) == insn
8564 && ((NONJUMP_INSN_P (prev) && GET_CODE (PATTERN (prev)) == SEQUENCE)
8565 ? CALL_ATTR (as_a <rtx_sequence *> (PATTERN (prev))->insn (0),
8566 NON_SIBCALL)
8567 : (CALL_ATTR (prev, NON_SIBCALL)
8568 && NEXT_INSN (PREV_INSN (prev)) == prev)))
8569 force_target = true;
8570
8571 switch (type)
8572 {
8573 case TYPE_BRCC:
8574 /* Short BRCC only comes in no-delay-slot version, and without limm */
8575 if (!delay_filled)
8576 {
8577 ilv->align_set = 3;
8578 ilv->length = 2;
8579 ilv->branch_cost = 1;
8580 ilv->enabled = (len == 2);
8581 ilv->length_sensitive = ARC_LS_8;
8582 ilv++;
8583 }
8584 /* Fall through. */
8585 case TYPE_BRCC_NO_DELAY_SLOT:
8586 /* doloop_fallback* patterns are TYPE_BRCC_NO_DELAY_SLOT for
8587 (delay slot) scheduling purposes, but they are longer. */
8588 if (GET_CODE (PATTERN (insn)) == PARALLEL
8589 && GET_CODE (XVECEXP (PATTERN (insn), 0, 1)) == SET)
8590 return 0;
8591 /* Standard BRCC: 4 bytes, or 8 bytes with limm. */
8592 ilv->length = ((type == TYPE_BRCC) ? 4 : 8);
8593 ilv->align_set = 3;
8594 ilv->branch_cost = branch_align_cost;
8595 ilv->enabled = (len <= ilv->length);
8596 ilv->length_sensitive = ARC_LS_9;
8597 if ((target_p || force_target)
8598 || (!delay_filled && TARGET_UNALIGN_BRANCH))
8599 {
8600 ilv[1] = *ilv;
8601 ilv->align_set = 1;
8602 ilv++;
8603 ilv->align_set = 2;
8604 ilv->target_cost = 1;
8605 ilv->branch_cost = branch_unalign_cost;
8606 }
8607 ilv++;
8608
8609 rtx op, op0;
8610 op = XEXP (SET_SRC (XVECEXP (PATTERN (insn), 0, 0)), 0);
8611 op0 = XEXP (op, 0);
8612
8613 if (GET_CODE (op0) == ZERO_EXTRACT
8614 && satisfies_constraint_L (XEXP (op0, 2)))
8615 op0 = XEXP (op0, 0);
8616 if (satisfies_constraint_Rcq (op0))
8617 {
8618 ilv->length = ((type == TYPE_BRCC) ? 6 : 10);
8619 ilv->align_set = 3;
8620 ilv->branch_cost = 1 + branch_align_cost;
8621 ilv->fallthrough_cost = 1;
8622 ilv->enabled = true;
8623 ilv->length_sensitive = ARC_LS_21;
8624 if (!delay_filled && TARGET_UNALIGN_BRANCH)
8625 {
8626 ilv[1] = *ilv;
8627 ilv->align_set = 1;
8628 ilv++;
8629 ilv->align_set = 2;
8630 ilv->branch_cost = 1 + branch_unalign_cost;
8631 }
8632 ilv++;
8633 }
8634 ilv->length = ((type == TYPE_BRCC) ? 8 : 12);
8635 ilv->align_set = 3;
8636 ilv->branch_cost = 1 + branch_align_cost;
8637 ilv->fallthrough_cost = 1;
8638 ilv->enabled = true;
8639 ilv->length_sensitive = ARC_LS_21;
8640 if ((target_p || force_target)
8641 || (!delay_filled && TARGET_UNALIGN_BRANCH))
8642 {
8643 ilv[1] = *ilv;
8644 ilv->align_set = 1;
8645 ilv++;
8646 ilv->align_set = 2;
8647 ilv->target_cost = 1;
8648 ilv->branch_cost = 1 + branch_unalign_cost;
8649 }
8650 ilv++;
8651 break;
8652
8653 case TYPE_SFUNC:
8654 ilv->length = 12;
8655 goto do_call;
8656 case TYPE_CALL_NO_DELAY_SLOT:
8657 ilv->length = 8;
8658 goto do_call;
8659 case TYPE_CALL:
8660 ilv->length = 4;
8661 ilv->length_sensitive
8662 = GET_CODE (PATTERN (insn)) == COND_EXEC ? ARC_LS_21 : ARC_LS_25;
8663 do_call:
8664 ilv->align_set = 3;
8665 ilv->fallthrough_cost = branch_align_cost;
8666 ilv->enabled = true;
8667 if ((target_p || force_target)
8668 || (!delay_filled && TARGET_UNALIGN_BRANCH))
8669 {
8670 ilv[1] = *ilv;
8671 ilv->align_set = 1;
8672 ilv++;
8673 ilv->align_set = 2;
8674 ilv->target_cost = 1;
8675 ilv->fallthrough_cost = branch_unalign_cost;
8676 }
8677 ilv++;
8678 break;
8679 case TYPE_UNCOND_BRANCH:
8680 /* Strictly speaking, this should be ARC_LS_10 for equality comparisons,
8681 but that makes no difference at the moment. */
8682 ilv->length_sensitive = ARC_LS_7;
8683 ilv[1].length_sensitive = ARC_LS_25;
8684 goto do_branch;
8685 case TYPE_BRANCH:
8686 ilv->length_sensitive = ARC_LS_10;
8687 ilv[1].length_sensitive = ARC_LS_21;
8688 do_branch:
8689 ilv->align_set = 3;
8690 ilv->length = 2;
8691 ilv->branch_cost = branch_align_cost;
8692 ilv->enabled = (len == ilv->length);
8693 ilv++;
8694 ilv->length = 4;
8695 ilv->align_set = 3;
8696 ilv->branch_cost = branch_align_cost;
8697 ilv->enabled = true;
8698 if ((target_p || force_target)
8699 || (!delay_filled && TARGET_UNALIGN_BRANCH))
8700 {
8701 ilv[1] = *ilv;
8702 ilv->align_set = 1;
8703 ilv++;
8704 ilv->align_set = 2;
8705 ilv->target_cost = 1;
8706 ilv->branch_cost = branch_unalign_cost;
8707 }
8708 ilv++;
8709 break;
8710 case TYPE_JUMP:
8711 return 0;
8712 default:
8713 /* For every short insn, there is generally also a long insn.
8714 trap_s is an exception. */
8715 if ((len & 2) == 0 || recog_memoized (insn) == CODE_FOR_trap_s)
8716 return 0;
8717 ilv->align_set = 3;
8718 ilv->length = len;
8719 ilv->enabled = 1;
8720 ilv++;
8721 ilv->align_set = 3;
8722 ilv->length = len + 2;
8723 ilv->enabled = 1;
8724 if (target_p || force_target)
8725 {
8726 ilv[1] = *ilv;
8727 ilv->align_set = 1;
8728 ilv++;
8729 ilv->align_set = 2;
8730 ilv->target_cost = 1;
8731 }
8732 ilv++;
8733 }
8734 /* If the previous instruction is an sfunc call, this insn is always
8735 a target, even though the middle-end is unaware of this.
8736 Therefore, if we have a call predecessor, transfer the target cost
8737 to the fallthrough and branch costs. */
8738 if (force_target)
8739 {
8740 for (insn_length_variant_t *p = first_ilv; p < ilv; p++)
8741 {
8742 p->fallthrough_cost += p->target_cost;
8743 p->branch_cost += p->target_cost;
8744 p->target_cost = 0;
8745 }
8746 }
8747
8748 return ilv - first_ilv;
8749 }
8750
8751 static void
8752 arc_insn_length_parameters (insn_length_parameters_t *ilp)
8753 {
8754 ilp->align_unit_log = 1;
8755 ilp->align_base_log = 1;
8756 ilp->max_variants = 7;
8757 ilp->get_variants = arc_get_insn_variants;
8758 }
8759
8760 /* Return a copy of COND from *STATEP, inverted if that is indicated by the
8761 CC field of *STATEP. */
8762
8763 static rtx
8764 arc_get_ccfsm_cond (struct arc_ccfsm *statep, bool reverse)
8765 {
8766 rtx cond = statep->cond;
8767 int raw_cc = get_arc_condition_code (cond);
8768 if (reverse)
8769 raw_cc = ARC_INVERSE_CONDITION_CODE (raw_cc);
8770
8771 if (statep->cc == raw_cc)
8772 return copy_rtx (cond);
8773
8774 gcc_assert (ARC_INVERSE_CONDITION_CODE (raw_cc) == statep->cc);
8775
8776 machine_mode ccm = GET_MODE (XEXP (cond, 0));
8777 enum rtx_code code = reverse_condition (GET_CODE (cond));
8778 if (code == UNKNOWN || ccm == CC_FP_GTmode || ccm == CC_FP_GEmode)
8779 code = reverse_condition_maybe_unordered (GET_CODE (cond));
8780
8781 return gen_rtx_fmt_ee (code, GET_MODE (cond),
8782 copy_rtx (XEXP (cond, 0)), copy_rtx (XEXP (cond, 1)));
8783 }
8784
8785 /* Return version of PAT conditionalized with COND, which is part of INSN.
8786 ANNULLED indicates if INSN is an annulled delay-slot insn.
8787 Register further changes if necessary. */
8788 static rtx
8789 conditionalize_nonjump (rtx pat, rtx cond, rtx insn, bool annulled)
8790 {
8791 /* For commutative operators, we generally prefer to have
8792 the first source match the destination. */
8793 if (GET_CODE (pat) == SET)
8794 {
8795 rtx src = SET_SRC (pat);
8796
8797 if (COMMUTATIVE_P (src))
8798 {
8799 rtx src0 = XEXP (src, 0);
8800 rtx src1 = XEXP (src, 1);
8801 rtx dst = SET_DEST (pat);
8802
8803 if (rtx_equal_p (src1, dst) && !rtx_equal_p (src0, dst)
8804 /* Leave add_n alone - the canonical form is to
8805 have the complex summand first. */
8806 && REG_P (src0))
8807 pat = gen_rtx_SET (dst,
8808 gen_rtx_fmt_ee (GET_CODE (src), GET_MODE (src),
8809 src1, src0));
8810 }
8811 }
8812
8813 /* dwarf2out.c:dwarf2out_frame_debug_expr doesn't know
8814 what to do with COND_EXEC. */
8815 if (RTX_FRAME_RELATED_P (insn))
8816 {
8817 /* If this is the delay slot insn of an anulled branch,
8818 dwarf2out.c:scan_trace understands the anulling semantics
8819 without the COND_EXEC. */
8820 gcc_assert (annulled);
8821 rtx note = alloc_reg_note (REG_FRAME_RELATED_EXPR, pat,
8822 REG_NOTES (insn));
8823 validate_change (insn, &REG_NOTES (insn), note, 1);
8824 }
8825 pat = gen_rtx_COND_EXEC (VOIDmode, cond, pat);
8826 return pat;
8827 }
8828
8829 /* Use the ccfsm machinery to do if conversion. */
8830
8831 static unsigned
8832 arc_ifcvt (void)
8833 {
8834 struct arc_ccfsm *statep = &cfun->machine->ccfsm_current;
8835
8836 memset (statep, 0, sizeof *statep);
8837 for (rtx_insn *insn = get_insns (); insn; insn = next_insn (insn))
8838 {
8839 arc_ccfsm_advance (insn, statep);
8840
8841 switch (statep->state)
8842 {
8843 case 0:
8844 break;
8845 case 1: case 2:
8846 {
8847 /* Deleted branch. */
8848 arc_ccfsm_post_advance (insn, statep);
8849 gcc_assert (!IN_RANGE (statep->state, 1, 2));
8850 rtx_insn *seq = NEXT_INSN (PREV_INSN (insn));
8851 if (GET_CODE (PATTERN (seq)) == SEQUENCE)
8852 {
8853 rtx slot = XVECEXP (PATTERN (seq), 0, 1);
8854 rtx pat = PATTERN (slot);
8855 if (INSN_ANNULLED_BRANCH_P (insn))
8856 {
8857 rtx cond
8858 = arc_get_ccfsm_cond (statep, INSN_FROM_TARGET_P (slot));
8859 pat = gen_rtx_COND_EXEC (VOIDmode, cond, pat);
8860 }
8861 if (!validate_change (seq, &PATTERN (seq), pat, 0))
8862 gcc_unreachable ();
8863 PUT_CODE (slot, NOTE);
8864 NOTE_KIND (slot) = NOTE_INSN_DELETED;
8865 }
8866 else
8867 {
8868 set_insn_deleted (insn);
8869 }
8870 continue;
8871 }
8872 case 3:
8873 if (LABEL_P (insn)
8874 && statep->target_label == CODE_LABEL_NUMBER (insn))
8875 {
8876 arc_ccfsm_post_advance (insn, statep);
8877 if (--LABEL_NUSES (insn) == 0)
8878 delete_insn (insn);
8879 continue;
8880 }
8881 /* Fall through. */
8882 case 4: case 5:
8883 if (!NONDEBUG_INSN_P (insn))
8884 break;
8885
8886 /* Conditionalized insn. */
8887
8888 rtx_insn *prev, *pprev;
8889 rtx *patp, pat, cond;
8890 bool annulled; annulled = false;
8891
8892 /* If this is a delay slot insn in a non-annulled branch,
8893 don't conditionalize it. N.B., this should be fine for
8894 conditional return too. However, don't do this for
8895 unconditional branches, as these would be encountered when
8896 processing an 'else' part. */
8897 prev = PREV_INSN (insn);
8898 pprev = PREV_INSN (prev);
8899 if (pprev && NEXT_INSN (NEXT_INSN (pprev)) == NEXT_INSN (insn)
8900 && JUMP_P (prev) && get_attr_cond (prev) == COND_USE)
8901 {
8902 if (!INSN_ANNULLED_BRANCH_P (prev))
8903 break;
8904 annulled = true;
8905 }
8906
8907 patp = &PATTERN (insn);
8908 pat = *patp;
8909 cond = arc_get_ccfsm_cond (statep, INSN_FROM_TARGET_P (insn));
8910 if (NONJUMP_INSN_P (insn) || CALL_P (insn))
8911 {
8912 /* ??? don't conditionalize if all side effects are dead
8913 in the not-execute case. */
8914
8915 pat = conditionalize_nonjump (pat, cond, insn, annulled);
8916 }
8917 else if (simplejump_p (insn))
8918 {
8919 patp = &SET_SRC (pat);
8920 pat = gen_rtx_IF_THEN_ELSE (VOIDmode, cond, *patp, pc_rtx);
8921 }
8922 else if (JUMP_P (insn) && ANY_RETURN_P (PATTERN (insn)))
8923 {
8924 pat = gen_rtx_IF_THEN_ELSE (VOIDmode, cond, pat, pc_rtx);
8925 pat = gen_rtx_SET (pc_rtx, pat);
8926 }
8927 else
8928 gcc_unreachable ();
8929 validate_change (insn, patp, pat, 1);
8930 if (!apply_change_group ())
8931 gcc_unreachable ();
8932 if (JUMP_P (insn))
8933 {
8934 rtx_insn *next = next_nonnote_insn (insn);
8935 if (GET_CODE (next) == BARRIER)
8936 delete_insn (next);
8937 if (statep->state == 3)
8938 continue;
8939 }
8940 break;
8941 default:
8942 gcc_unreachable ();
8943 }
8944 arc_ccfsm_post_advance (insn, statep);
8945 }
8946 return 0;
8947 }
8948
8949 /* Find annulled delay insns and convert them to use the appropriate predicate.
8950 This allows branch shortening to size up these insns properly. */
8951
8952 static unsigned
8953 arc_predicate_delay_insns (void)
8954 {
8955 for (rtx_insn *insn = get_insns (); insn; insn = NEXT_INSN (insn))
8956 {
8957 rtx pat, jump, dlay, src, cond, *patp;
8958 int reverse;
8959
8960 if (!NONJUMP_INSN_P (insn)
8961 || GET_CODE (pat = PATTERN (insn)) != SEQUENCE)
8962 continue;
8963 jump = XVECEXP (pat, 0, 0);
8964 dlay = XVECEXP (pat, 0, 1);
8965 if (!JUMP_P (jump) || !INSN_ANNULLED_BRANCH_P (jump))
8966 continue;
8967 /* If the branch insn does the annulling, leave the delay insn alone. */
8968 if (!TARGET_AT_DBR_CONDEXEC && !INSN_FROM_TARGET_P (dlay))
8969 continue;
8970 /* ??? Could also leave DLAY un-conditionalized if its target is dead
8971 on the other path. */
8972 gcc_assert (GET_CODE (PATTERN (jump)) == SET);
8973 gcc_assert (SET_DEST (PATTERN (jump)) == pc_rtx);
8974 src = SET_SRC (PATTERN (jump));
8975 gcc_assert (GET_CODE (src) == IF_THEN_ELSE);
8976 cond = XEXP (src, 0);
8977 if (XEXP (src, 2) == pc_rtx)
8978 reverse = 0;
8979 else if (XEXP (src, 1) == pc_rtx)
8980 reverse = 1;
8981 else
8982 gcc_unreachable ();
8983 if (reverse != !INSN_FROM_TARGET_P (dlay))
8984 {
8985 machine_mode ccm = GET_MODE (XEXP (cond, 0));
8986 enum rtx_code code = reverse_condition (GET_CODE (cond));
8987 if (code == UNKNOWN || ccm == CC_FP_GTmode || ccm == CC_FP_GEmode)
8988 code = reverse_condition_maybe_unordered (GET_CODE (cond));
8989
8990 cond = gen_rtx_fmt_ee (code, GET_MODE (cond),
8991 copy_rtx (XEXP (cond, 0)),
8992 copy_rtx (XEXP (cond, 1)));
8993 }
8994 else
8995 cond = copy_rtx (cond);
8996 patp = &PATTERN (dlay);
8997 pat = *patp;
8998 pat = conditionalize_nonjump (pat, cond, dlay, true);
8999 validate_change (dlay, patp, pat, 1);
9000 if (!apply_change_group ())
9001 gcc_unreachable ();
9002 }
9003 return 0;
9004 }
9005
9006 /* For ARC600: If a write to a core reg >=32 appears in a delay slot
9007 (other than of a forward brcc), it creates a hazard when there is a read
9008 of the same register at the branch target. We can't know what is at the
9009 branch target of calls, and for branches, we don't really know before the
9010 end of delay slot scheduling, either. Not only can individual instruction
9011 be hoisted out into a delay slot, a basic block can also be emptied this
9012 way, and branch and/or fall through targets be redirected. Hence we don't
9013 want such writes in a delay slot. */
9014
9015 /* Return nonzreo iff INSN writes to an extension core register. */
9016
9017 int
9018 arc_write_ext_corereg (rtx insn)
9019 {
9020 subrtx_iterator::array_type array;
9021 FOR_EACH_SUBRTX (iter, array, PATTERN (insn), NONCONST)
9022 {
9023 const_rtx x = *iter;
9024 switch (GET_CODE (x))
9025 {
9026 case SET: case POST_INC: case POST_DEC: case PRE_INC: case PRE_DEC:
9027 break;
9028 default:
9029 /* This is also fine for PRE/POST_MODIFY, because they
9030 contain a SET. */
9031 continue;
9032 }
9033 const_rtx dest = XEXP (x, 0);
9034 if (REG_P (dest) && REGNO (dest) >= 32 && REGNO (dest) < 61)
9035 return 1;
9036 }
9037 return 0;
9038 }
9039
9040 /* This is like the hook, but returns NULL when it can't / won't generate
9041 a legitimate address. */
9042
9043 static rtx
9044 arc_legitimize_address_0 (rtx x, rtx oldx ATTRIBUTE_UNUSED,
9045 machine_mode mode)
9046 {
9047 rtx addr, inner;
9048
9049 if (flag_pic && SYMBOLIC_CONST (x))
9050 (x) = arc_legitimize_pic_address (x, 0);
9051 addr = x;
9052 if (GET_CODE (addr) == CONST)
9053 addr = XEXP (addr, 0);
9054 if (GET_CODE (addr) == PLUS
9055 && CONST_INT_P (XEXP (addr, 1))
9056 && ((GET_CODE (XEXP (addr, 0)) == SYMBOL_REF
9057 && !SYMBOL_REF_FUNCTION_P (XEXP (addr, 0)))
9058 || (REG_P (XEXP (addr, 0))
9059 && (INTVAL (XEXP (addr, 1)) & 252))))
9060 {
9061 HOST_WIDE_INT offs, upper;
9062 int size = GET_MODE_SIZE (mode);
9063
9064 offs = INTVAL (XEXP (addr, 1));
9065 upper = (offs + 256 * size) & ~511 * size;
9066 inner = plus_constant (Pmode, XEXP (addr, 0), upper);
9067 #if 0 /* ??? this produces worse code for EEMBC idctrn01 */
9068 if (GET_CODE (x) == CONST)
9069 inner = gen_rtx_CONST (Pmode, inner);
9070 #endif
9071 addr = plus_constant (Pmode, force_reg (Pmode, inner), offs - upper);
9072 x = addr;
9073 }
9074 else if (GET_CODE (addr) == SYMBOL_REF && !SYMBOL_REF_FUNCTION_P (addr))
9075 x = force_reg (Pmode, x);
9076 if (memory_address_p ((machine_mode) mode, x))
9077 return x;
9078 return NULL_RTX;
9079 }
9080
9081 static rtx
9082 arc_legitimize_address (rtx orig_x, rtx oldx, machine_mode mode)
9083 {
9084 if (GET_CODE (orig_x) == SYMBOL_REF)
9085 {
9086 enum tls_model model = SYMBOL_REF_TLS_MODEL (orig_x);
9087 if (model != 0)
9088 return arc_legitimize_tls_address (orig_x, model);
9089 }
9090
9091 rtx new_x = arc_legitimize_address_0 (orig_x, oldx, mode);
9092
9093 if (new_x)
9094 return new_x;
9095 return orig_x;
9096 }
9097
9098 static rtx
9099 arc_delegitimize_address_0 (rtx x)
9100 {
9101 rtx u, gp, p;
9102
9103 if (GET_CODE (x) == CONST && GET_CODE (u = XEXP (x, 0)) == UNSPEC)
9104 {
9105 if (XINT (u, 1) == ARC_UNSPEC_GOT
9106 || XINT (u, 1) == ARC_UNSPEC_GOTOFFPC)
9107 return XVECEXP (u, 0, 0);
9108 }
9109 else if (GET_CODE (x) == CONST && GET_CODE (p = XEXP (x, 0)) == PLUS
9110 && GET_CODE (u = XEXP (p, 0)) == UNSPEC
9111 && (XINT (u, 1) == ARC_UNSPEC_GOT
9112 || XINT (u, 1) == ARC_UNSPEC_GOTOFFPC))
9113 return gen_rtx_CONST
9114 (GET_MODE (x),
9115 gen_rtx_PLUS (GET_MODE (p), XVECEXP (u, 0, 0), XEXP (p, 1)));
9116 else if (GET_CODE (x) == PLUS
9117 && ((REG_P (gp = XEXP (x, 0))
9118 && REGNO (gp) == PIC_OFFSET_TABLE_REGNUM)
9119 || (GET_CODE (gp) == CONST
9120 && GET_CODE (u = XEXP (gp, 0)) == UNSPEC
9121 && XINT (u, 1) == ARC_UNSPEC_GOT
9122 && GET_CODE (XVECEXP (u, 0, 0)) == SYMBOL_REF
9123 && !strcmp (XSTR (XVECEXP (u, 0, 0), 0), "_DYNAMIC")))
9124 && GET_CODE (XEXP (x, 1)) == CONST
9125 && GET_CODE (u = XEXP (XEXP (x, 1), 0)) == UNSPEC
9126 && XINT (u, 1) == ARC_UNSPEC_GOTOFF)
9127 return XVECEXP (u, 0, 0);
9128 else if (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 0)) == PLUS
9129 && ((REG_P (gp = XEXP (XEXP (x, 0), 1))
9130 && REGNO (gp) == PIC_OFFSET_TABLE_REGNUM)
9131 || (GET_CODE (gp) == CONST
9132 && GET_CODE (u = XEXP (gp, 0)) == UNSPEC
9133 && XINT (u, 1) == ARC_UNSPEC_GOT
9134 && GET_CODE (XVECEXP (u, 0, 0)) == SYMBOL_REF
9135 && !strcmp (XSTR (XVECEXP (u, 0, 0), 0), "_DYNAMIC")))
9136 && GET_CODE (XEXP (x, 1)) == CONST
9137 && GET_CODE (u = XEXP (XEXP (x, 1), 0)) == UNSPEC
9138 && XINT (u, 1) == ARC_UNSPEC_GOTOFF)
9139 return gen_rtx_PLUS (GET_MODE (x), XEXP (XEXP (x, 0), 0),
9140 XVECEXP (u, 0, 0));
9141 else if (GET_CODE (x) == PLUS
9142 && (u = arc_delegitimize_address_0 (XEXP (x, 1))))
9143 return gen_rtx_PLUS (GET_MODE (x), XEXP (x, 0), u);
9144 return NULL_RTX;
9145 }
9146
9147 static rtx
9148 arc_delegitimize_address (rtx x)
9149 {
9150 rtx orig_x = x = delegitimize_mem_from_attrs (x);
9151 if (GET_CODE (x) == MEM)
9152 x = XEXP (x, 0);
9153 x = arc_delegitimize_address_0 (x);
9154 if (x)
9155 {
9156 if (MEM_P (orig_x))
9157 x = replace_equiv_address_nv (orig_x, x);
9158 return x;
9159 }
9160 return orig_x;
9161 }
9162
9163 /* Return a REG rtx for acc1. N.B. the gcc-internal representation may
9164 differ from the hardware register number in order to allow the generic
9165 code to correctly split the concatenation of acc1 and acc2. */
9166
9167 rtx
9168 gen_acc1 (void)
9169 {
9170 return gen_rtx_REG (SImode, TARGET_BIG_ENDIAN ? 56: 57);
9171 }
9172
9173 /* Return a REG rtx for acc2. N.B. the gcc-internal representation may
9174 differ from the hardware register number in order to allow the generic
9175 code to correctly split the concatenation of acc1 and acc2. */
9176
9177 rtx
9178 gen_acc2 (void)
9179 {
9180 return gen_rtx_REG (SImode, TARGET_BIG_ENDIAN ? 57: 56);
9181 }
9182
9183 /* Return a REG rtx for mlo. N.B. the gcc-internal representation may
9184 differ from the hardware register number in order to allow the generic
9185 code to correctly split the concatenation of mhi and mlo. */
9186
9187 rtx
9188 gen_mlo (void)
9189 {
9190 return gen_rtx_REG (SImode, TARGET_BIG_ENDIAN ? 59: 58);
9191 }
9192
9193 /* Return a REG rtx for mhi. N.B. the gcc-internal representation may
9194 differ from the hardware register number in order to allow the generic
9195 code to correctly split the concatenation of mhi and mlo. */
9196
9197 rtx
9198 gen_mhi (void)
9199 {
9200 return gen_rtx_REG (SImode, TARGET_BIG_ENDIAN ? 58: 59);
9201 }
9202
9203 /* FIXME: a parameter should be added, and code added to final.c,
9204 to reproduce this functionality in shorten_branches. */
9205 #if 0
9206 /* Return nonzero iff BRANCH should be unaligned if possible by upsizing
9207 a previous instruction. */
9208 int
9209 arc_unalign_branch_p (rtx branch)
9210 {
9211 rtx note;
9212
9213 if (!TARGET_UNALIGN_BRANCH)
9214 return 0;
9215 /* Do not do this if we have a filled delay slot. */
9216 if (get_attr_delay_slot_filled (branch) == DELAY_SLOT_FILLED_YES
9217 && !NEXT_INSN (branch)->deleted ())
9218 return 0;
9219 note = find_reg_note (branch, REG_BR_PROB, 0);
9220 return (!note
9221 || (arc_unalign_prob_threshold && !br_prob_note_reliable_p (note))
9222 || INTVAL (XEXP (note, 0)) < arc_unalign_prob_threshold);
9223 }
9224 #endif
9225
9226 /* When estimating sizes during arc_reorg, when optimizing for speed, there
9227 are three reasons why we need to consider branches to be length 6:
9228 - annull-false delay slot insns are implemented using conditional execution,
9229 thus preventing short insn formation where used.
9230 - for ARC600: annul-true delay slot insns are implemented where possible
9231 using conditional execution, preventing short insn formation where used.
9232 - for ARC700: likely or somewhat likely taken branches are made long and
9233 unaligned if possible to avoid branch penalty. */
9234
9235 bool
9236 arc_branch_size_unknown_p (void)
9237 {
9238 return !optimize_size && arc_reorg_in_progress;
9239 }
9240
9241 /* We are about to output a return insn. Add padding if necessary to avoid
9242 a mispredict. A return could happen immediately after the function
9243 start, but after a call we know that there will be at least a blink
9244 restore. */
9245
9246 void
9247 arc_pad_return (void)
9248 {
9249 rtx_insn *insn = current_output_insn;
9250 rtx_insn *prev = prev_active_insn (insn);
9251 int want_long;
9252
9253 if (!prev)
9254 {
9255 fputs ("\tnop_s\n", asm_out_file);
9256 cfun->machine->unalign ^= 2;
9257 want_long = 1;
9258 }
9259 /* If PREV is a sequence, we know it must be a branch / jump or a tailcall,
9260 because after a call, we'd have to restore blink first. */
9261 else if (GET_CODE (PATTERN (prev)) == SEQUENCE)
9262 return;
9263 else
9264 {
9265 want_long = (get_attr_length (prev) == 2);
9266 prev = prev_active_insn (prev);
9267 }
9268 if (!prev
9269 || ((NONJUMP_INSN_P (prev) && GET_CODE (PATTERN (prev)) == SEQUENCE)
9270 ? CALL_ATTR (as_a <rtx_sequence *> (PATTERN (prev))->insn (0),
9271 NON_SIBCALL)
9272 : CALL_ATTR (prev, NON_SIBCALL)))
9273 {
9274 if (want_long)
9275 cfun->machine->size_reason
9276 = "call/return and return/return must be 6 bytes apart to avoid mispredict";
9277 else if (TARGET_UNALIGN_BRANCH && cfun->machine->unalign)
9278 {
9279 cfun->machine->size_reason
9280 = "Long unaligned jump avoids non-delay slot penalty";
9281 want_long = 1;
9282 }
9283 /* Disgorge delay insn, if there is any, and it may be moved. */
9284 if (final_sequence
9285 /* ??? Annulled would be OK if we can and do conditionalize
9286 the delay slot insn accordingly. */
9287 && !INSN_ANNULLED_BRANCH_P (insn)
9288 && (get_attr_cond (insn) != COND_USE
9289 || !reg_set_p (gen_rtx_REG (CCmode, CC_REG),
9290 XVECEXP (final_sequence, 0, 1))))
9291 {
9292 prev = as_a <rtx_insn *> (XVECEXP (final_sequence, 0, 1));
9293 gcc_assert (!prev_real_insn (insn)
9294 || !arc_hazard (prev_real_insn (insn), prev));
9295 cfun->machine->force_short_suffix = !want_long;
9296 rtx save_pred = current_insn_predicate;
9297 final_scan_insn (prev, asm_out_file, optimize, 1, NULL);
9298 cfun->machine->force_short_suffix = -1;
9299 prev->set_deleted ();
9300 current_output_insn = insn;
9301 current_insn_predicate = save_pred;
9302 }
9303 else if (want_long)
9304 fputs ("\tnop\n", asm_out_file);
9305 else
9306 {
9307 fputs ("\tnop_s\n", asm_out_file);
9308 cfun->machine->unalign ^= 2;
9309 }
9310 }
9311 return;
9312 }
9313
9314 /* The usual; we set up our machine_function data. */
9315
9316 static struct machine_function *
9317 arc_init_machine_status (void)
9318 {
9319 struct machine_function *machine;
9320 machine = ggc_cleared_alloc<machine_function> ();
9321 machine->fn_type = ARC_FUNCTION_UNKNOWN;
9322 machine->force_short_suffix = -1;
9323
9324 return machine;
9325 }
9326
9327 /* Implements INIT_EXPANDERS. We just set up to call the above
9328 function. */
9329
9330 void
9331 arc_init_expanders (void)
9332 {
9333 init_machine_status = arc_init_machine_status;
9334 }
9335
9336 /* Check if OP is a proper parallel of a millicode call pattern. OFFSET
9337 indicates a number of elements to ignore - that allows to have a
9338 sibcall pattern that starts with (return). LOAD_P is zero for store
9339 multiple (for prologues), and one for load multiples (for epilogues),
9340 and two for load multiples where no final clobber of blink is required.
9341 We also skip the first load / store element since this is supposed to
9342 be checked in the instruction pattern. */
9343
9344 int
9345 arc_check_millicode (rtx op, int offset, int load_p)
9346 {
9347 int len = XVECLEN (op, 0) - offset;
9348 int i;
9349
9350 if (load_p == 2)
9351 {
9352 if (len < 2 || len > 13)
9353 return 0;
9354 load_p = 1;
9355 }
9356 else
9357 {
9358 rtx elt = XVECEXP (op, 0, --len);
9359
9360 if (GET_CODE (elt) != CLOBBER
9361 || !REG_P (XEXP (elt, 0))
9362 || REGNO (XEXP (elt, 0)) != RETURN_ADDR_REGNUM
9363 || len < 3 || len > 13)
9364 return 0;
9365 }
9366 for (i = 1; i < len; i++)
9367 {
9368 rtx elt = XVECEXP (op, 0, i + offset);
9369 rtx reg, mem, addr;
9370
9371 if (GET_CODE (elt) != SET)
9372 return 0;
9373 mem = XEXP (elt, load_p);
9374 reg = XEXP (elt, 1-load_p);
9375 if (!REG_P (reg) || REGNO (reg) != 13U+i || !MEM_P (mem))
9376 return 0;
9377 addr = XEXP (mem, 0);
9378 if (GET_CODE (addr) != PLUS
9379 || !rtx_equal_p (stack_pointer_rtx, XEXP (addr, 0))
9380 || !CONST_INT_P (XEXP (addr, 1)) || INTVAL (XEXP (addr, 1)) != i*4)
9381 return 0;
9382 }
9383 return 1;
9384 }
9385
9386 /* Accessor functions for cfun->machine->unalign. */
9387
9388 int
9389 arc_get_unalign (void)
9390 {
9391 return cfun->machine->unalign;
9392 }
9393
9394 void
9395 arc_clear_unalign (void)
9396 {
9397 if (cfun)
9398 cfun->machine->unalign = 0;
9399 }
9400
9401 void
9402 arc_toggle_unalign (void)
9403 {
9404 cfun->machine->unalign ^= 2;
9405 }
9406
9407 /* Operands 0..2 are the operands of a addsi which uses a 12 bit
9408 constant in operand 2, but which would require a LIMM because of
9409 operand mismatch.
9410 operands 3 and 4 are new SET_SRCs for operands 0. */
9411
9412 void
9413 split_addsi (rtx *operands)
9414 {
9415 int val = INTVAL (operands[2]);
9416
9417 /* Try for two short insns first. Lengths being equal, we prefer
9418 expansions with shorter register lifetimes. */
9419 if (val > 127 && val <= 255
9420 && satisfies_constraint_Rcq (operands[0]))
9421 {
9422 operands[3] = operands[2];
9423 operands[4] = gen_rtx_PLUS (SImode, operands[0], operands[1]);
9424 }
9425 else
9426 {
9427 operands[3] = operands[1];
9428 operands[4] = gen_rtx_PLUS (SImode, operands[0], operands[2]);
9429 }
9430 }
9431
9432 /* Operands 0..2 are the operands of a subsi which uses a 12 bit
9433 constant in operand 1, but which would require a LIMM because of
9434 operand mismatch.
9435 operands 3 and 4 are new SET_SRCs for operands 0. */
9436
9437 void
9438 split_subsi (rtx *operands)
9439 {
9440 int val = INTVAL (operands[1]);
9441
9442 /* Try for two short insns first. Lengths being equal, we prefer
9443 expansions with shorter register lifetimes. */
9444 if (satisfies_constraint_Rcq (operands[0])
9445 && satisfies_constraint_Rcq (operands[2]))
9446 {
9447 if (val >= -31 && val <= 127)
9448 {
9449 operands[3] = gen_rtx_NEG (SImode, operands[2]);
9450 operands[4] = gen_rtx_PLUS (SImode, operands[0], operands[1]);
9451 return;
9452 }
9453 else if (val >= 0 && val < 255)
9454 {
9455 operands[3] = operands[1];
9456 operands[4] = gen_rtx_MINUS (SImode, operands[0], operands[2]);
9457 return;
9458 }
9459 }
9460 /* If the destination is not an ARCompact16 register, we might
9461 still have a chance to make a short insn if the source is;
9462 we need to start with a reg-reg move for this. */
9463 operands[3] = operands[2];
9464 operands[4] = gen_rtx_MINUS (SImode, operands[1], operands[0]);
9465 }
9466
9467 /* Handle DOUBLE_REGS uses.
9468 Operand 0: destination register
9469 Operand 1: source register */
9470
9471 static bool
9472 arc_process_double_reg_moves (rtx *operands)
9473 {
9474 rtx dest = operands[0];
9475 rtx src = operands[1];
9476
9477 enum usesDxState { none, srcDx, destDx, maxDx };
9478 enum usesDxState state = none;
9479
9480 if (refers_to_regno_p (40, 44, src, 0))
9481 state = srcDx;
9482 if (refers_to_regno_p (40, 44, dest, 0))
9483 {
9484 /* Via arc_register_move_cost, we should never see D,D moves. */
9485 gcc_assert (state == none);
9486 state = destDx;
9487 }
9488
9489 if (state == none)
9490 return false;
9491
9492 if (state == srcDx)
9493 {
9494 /* Without the LR insn, we need to split this into a
9495 sequence of insns which will use the DEXCLx and DADDHxy
9496 insns to be able to read the Dx register in question. */
9497 if (TARGET_DPFP_DISABLE_LRSR)
9498 {
9499 /* gen *movdf_insn_nolrsr */
9500 rtx set = gen_rtx_SET (dest, src);
9501 rtx use1 = gen_rtx_USE (VOIDmode, const1_rtx);
9502 emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, set, use1)));
9503 }
9504 else
9505 {
9506 /* When we have 'mov D, r' or 'mov D, D' then get the target
9507 register pair for use with LR insn. */
9508 rtx destHigh = simplify_gen_subreg (SImode, dest, DFmode,
9509 TARGET_BIG_ENDIAN ? 0 : 4);
9510 rtx destLow = simplify_gen_subreg (SImode, dest, DFmode,
9511 TARGET_BIG_ENDIAN ? 4 : 0);
9512
9513 /* Produce the two LR insns to get the high and low parts. */
9514 emit_insn (gen_rtx_SET (destHigh,
9515 gen_rtx_UNSPEC_VOLATILE (Pmode,
9516 gen_rtvec (1, src),
9517 VUNSPEC_ARC_LR_HIGH)));
9518 emit_insn (gen_rtx_SET (destLow,
9519 gen_rtx_UNSPEC_VOLATILE (Pmode,
9520 gen_rtvec (1, src),
9521 VUNSPEC_ARC_LR)));
9522 }
9523 }
9524 else if (state == destDx)
9525 {
9526 /* When we have 'mov r, D' or 'mov D, D' and we have access to the
9527 LR insn get the target register pair. */
9528 rtx srcHigh = simplify_gen_subreg (SImode, src, DFmode,
9529 TARGET_BIG_ENDIAN ? 0 : 4);
9530 rtx srcLow = simplify_gen_subreg (SImode, src, DFmode,
9531 TARGET_BIG_ENDIAN ? 4 : 0);
9532
9533 emit_insn (gen_dexcl_2op (dest, srcHigh, srcLow));
9534 }
9535 else
9536 gcc_unreachable ();
9537
9538 return true;
9539 }
9540
9541 /* operands 0..1 are the operands of a 64 bit move instruction.
9542 split it into two moves with operands 2/3 and 4/5. */
9543
9544 void
9545 arc_split_move (rtx *operands)
9546 {
9547 machine_mode mode = GET_MODE (operands[0]);
9548 int i;
9549 int swap = 0;
9550 rtx xop[4];
9551
9552 if (TARGET_DPFP)
9553 {
9554 if (arc_process_double_reg_moves (operands))
9555 return;
9556 }
9557
9558 if (TARGET_LL64
9559 && ((memory_operand (operands[0], mode)
9560 && even_register_operand (operands[1], mode))
9561 || (memory_operand (operands[1], mode)
9562 && even_register_operand (operands[0], mode))))
9563 {
9564 emit_move_insn (operands[0], operands[1]);
9565 return;
9566 }
9567
9568 if (TARGET_PLUS_QMACW
9569 && GET_CODE (operands[1]) == CONST_VECTOR)
9570 {
9571 HOST_WIDE_INT intval0, intval1;
9572 if (GET_MODE (operands[1]) == V2SImode)
9573 {
9574 intval0 = INTVAL (XVECEXP (operands[1], 0, 0));
9575 intval1 = INTVAL (XVECEXP (operands[1], 0, 1));
9576 }
9577 else
9578 {
9579 intval1 = INTVAL (XVECEXP (operands[1], 0, 3)) << 16;
9580 intval1 |= INTVAL (XVECEXP (operands[1], 0, 2)) & 0xFFFF;
9581 intval0 = INTVAL (XVECEXP (operands[1], 0, 1)) << 16;
9582 intval0 |= INTVAL (XVECEXP (operands[1], 0, 0)) & 0xFFFF;
9583 }
9584 xop[0] = gen_rtx_REG (SImode, REGNO (operands[0]));
9585 xop[3] = gen_rtx_REG (SImode, REGNO (operands[0]) + 1);
9586 xop[2] = GEN_INT (trunc_int_for_mode (intval0, SImode));
9587 xop[1] = GEN_INT (trunc_int_for_mode (intval1, SImode));
9588 emit_move_insn (xop[0], xop[2]);
9589 emit_move_insn (xop[3], xop[1]);
9590 return;
9591 }
9592
9593 for (i = 0; i < 2; i++)
9594 {
9595 if (MEM_P (operands[i]) && auto_inc_p (XEXP (operands[i], 0)))
9596 {
9597 rtx addr = XEXP (operands[i], 0);
9598 rtx r, o;
9599 enum rtx_code code;
9600
9601 gcc_assert (!reg_overlap_mentioned_p (operands[0], addr));
9602 switch (GET_CODE (addr))
9603 {
9604 case PRE_DEC: o = GEN_INT (-8); goto pre_modify;
9605 case PRE_INC: o = GEN_INT (8); goto pre_modify;
9606 case PRE_MODIFY: o = XEXP (XEXP (addr, 1), 1);
9607 pre_modify:
9608 code = PRE_MODIFY;
9609 break;
9610 case POST_DEC: o = GEN_INT (-8); goto post_modify;
9611 case POST_INC: o = GEN_INT (8); goto post_modify;
9612 case POST_MODIFY: o = XEXP (XEXP (addr, 1), 1);
9613 post_modify:
9614 code = POST_MODIFY;
9615 swap = 2;
9616 break;
9617 default:
9618 gcc_unreachable ();
9619 }
9620 r = XEXP (addr, 0);
9621 xop[0+i] = adjust_automodify_address_nv
9622 (operands[i], SImode,
9623 gen_rtx_fmt_ee (code, Pmode, r,
9624 gen_rtx_PLUS (Pmode, r, o)),
9625 0);
9626 xop[2+i] = adjust_automodify_address_nv
9627 (operands[i], SImode, plus_constant (Pmode, r, 4), 4);
9628 }
9629 else
9630 {
9631 xop[0+i] = operand_subword (operands[i], 0, 0, mode);
9632 xop[2+i] = operand_subword (operands[i], 1, 0, mode);
9633 }
9634 }
9635 if (reg_overlap_mentioned_p (xop[0], xop[3]))
9636 {
9637 swap = 2;
9638 gcc_assert (!reg_overlap_mentioned_p (xop[2], xop[1]));
9639 }
9640
9641 emit_move_insn (xop[0 + swap], xop[1 + swap]);
9642 emit_move_insn (xop[2 - swap], xop[3 - swap]);
9643
9644 }
9645
9646 /* Select between the instruction output templates s_tmpl (for short INSNs)
9647 and l_tmpl (for long INSNs). */
9648
9649 const char *
9650 arc_short_long (rtx_insn *insn, const char *s_tmpl, const char *l_tmpl)
9651 {
9652 int is_short = arc_verify_short (insn, cfun->machine->unalign, -1);
9653
9654 extract_constrain_insn_cached (insn);
9655 return is_short ? s_tmpl : l_tmpl;
9656 }
9657
9658 /* Searches X for any reference to REGNO, returning the rtx of the
9659 reference found if any. Otherwise, returns NULL_RTX. */
9660
9661 rtx
9662 arc_regno_use_in (unsigned int regno, rtx x)
9663 {
9664 const char *fmt;
9665 int i, j;
9666 rtx tem;
9667
9668 if (REG_P (x) && refers_to_regno_p (regno, x))
9669 return x;
9670
9671 fmt = GET_RTX_FORMAT (GET_CODE (x));
9672 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
9673 {
9674 if (fmt[i] == 'e')
9675 {
9676 if ((tem = regno_use_in (regno, XEXP (x, i))))
9677 return tem;
9678 }
9679 else if (fmt[i] == 'E')
9680 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9681 if ((tem = regno_use_in (regno , XVECEXP (x, i, j))))
9682 return tem;
9683 }
9684
9685 return NULL_RTX;
9686 }
9687
9688 /* Return the integer value of the "type" attribute for INSN, or -1 if
9689 INSN can't have attributes. */
9690
9691 int
9692 arc_attr_type (rtx_insn *insn)
9693 {
9694 if (NONJUMP_INSN_P (insn)
9695 ? (GET_CODE (PATTERN (insn)) == USE
9696 || GET_CODE (PATTERN (insn)) == CLOBBER)
9697 : JUMP_P (insn)
9698 ? (GET_CODE (PATTERN (insn)) == ADDR_VEC
9699 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC)
9700 : !CALL_P (insn))
9701 return -1;
9702 return get_attr_type (insn);
9703 }
9704
9705 /* Return true if insn sets the condition codes. */
9706
9707 bool
9708 arc_sets_cc_p (rtx_insn *insn)
9709 {
9710 if (NONJUMP_INSN_P (insn))
9711 if (rtx_sequence *seq = dyn_cast <rtx_sequence *> (PATTERN (insn)))
9712 insn = seq->insn (seq->len () - 1);
9713 return arc_attr_type (insn) == TYPE_COMPARE;
9714 }
9715
9716 /* Return true if INSN is an instruction with a delay slot we may want
9717 to fill. */
9718
9719 bool
9720 arc_need_delay (rtx_insn *insn)
9721 {
9722 rtx_insn *next;
9723
9724 if (!flag_delayed_branch)
9725 return false;
9726 /* The return at the end of a function needs a delay slot. */
9727 if (NONJUMP_INSN_P (insn) && GET_CODE (PATTERN (insn)) == USE
9728 && (!(next = next_active_insn (insn))
9729 || ((!NONJUMP_INSN_P (next) || GET_CODE (PATTERN (next)) != SEQUENCE)
9730 && arc_attr_type (next) == TYPE_RETURN))
9731 && (!TARGET_PAD_RETURN
9732 || (prev_active_insn (insn)
9733 && prev_active_insn (prev_active_insn (insn))
9734 && prev_active_insn (prev_active_insn (prev_active_insn (insn))))))
9735 return true;
9736 if (NONJUMP_INSN_P (insn)
9737 ? (GET_CODE (PATTERN (insn)) == USE
9738 || GET_CODE (PATTERN (insn)) == CLOBBER
9739 || GET_CODE (PATTERN (insn)) == SEQUENCE)
9740 : JUMP_P (insn)
9741 ? (GET_CODE (PATTERN (insn)) == ADDR_VEC
9742 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC)
9743 : !CALL_P (insn))
9744 return false;
9745 return num_delay_slots (insn) != 0;
9746 }
9747
9748 /* Return true if the scheduling pass(es) has/have already run,
9749 i.e. where possible, we should try to mitigate high latencies
9750 by different instruction selection. */
9751
9752 bool
9753 arc_scheduling_not_expected (void)
9754 {
9755 return cfun->machine->arc_reorg_started;
9756 }
9757
9758 /* Oddly enough, sometimes we get a zero overhead loop that branch
9759 shortening doesn't think is a loop - observed with compile/pr24883.c
9760 -O3 -fomit-frame-pointer -funroll-loops. Make sure to include the
9761 alignment visible for branch shortening (we actually align the loop
9762 insn before it, but that is equivalent since the loop insn is 4 byte
9763 long.) */
9764
9765 int
9766 arc_label_align (rtx_insn *label)
9767 {
9768 int loop_align = LOOP_ALIGN (LABEL);
9769
9770 if (loop_align > align_labels_log)
9771 {
9772 rtx_insn *prev = prev_nonnote_insn (label);
9773
9774 if (prev && NONJUMP_INSN_P (prev)
9775 && GET_CODE (PATTERN (prev)) == PARALLEL
9776 && recog_memoized (prev) == CODE_FOR_doloop_begin_i)
9777 return loop_align;
9778 }
9779 /* Code has a minimum p2 alignment of 1, which we must restore after an
9780 ADDR_DIFF_VEC. */
9781 if (align_labels_log < 1)
9782 {
9783 rtx_insn *next = next_nonnote_nondebug_insn (label);
9784 if (INSN_P (next) && recog_memoized (next) >= 0)
9785 return 1;
9786 }
9787 return align_labels_log;
9788 }
9789
9790 /* Return true if LABEL is in executable code. */
9791
9792 bool
9793 arc_text_label (rtx_insn *label)
9794 {
9795 rtx_insn *next;
9796
9797 /* ??? We use deleted labels like they were still there, see
9798 gcc.c-torture/compile/20000326-2.c . */
9799 gcc_assert (GET_CODE (label) == CODE_LABEL
9800 || (GET_CODE (label) == NOTE
9801 && NOTE_KIND (label) == NOTE_INSN_DELETED_LABEL));
9802 next = next_nonnote_insn (label);
9803 if (next)
9804 return (!JUMP_TABLE_DATA_P (next)
9805 || GET_CODE (PATTERN (next)) != ADDR_VEC);
9806 else if (!PREV_INSN (label))
9807 /* ??? sometimes text labels get inserted very late, see
9808 gcc.dg/torture/stackalign/comp-goto-1.c */
9809 return true;
9810 return false;
9811 }
9812
9813 /* Without this, gcc.dg/tree-prof/bb-reorg.c fails to assemble
9814 when compiling with -O2 -freorder-blocks-and-partition -fprofile-use
9815 -D_PROFILE_USE; delay branch scheduling then follows a crossing jump
9816 to redirect two breqs. */
9817
9818 static bool
9819 arc_can_follow_jump (const rtx_insn *follower, const rtx_insn *followee)
9820 {
9821 /* ??? get_attr_type is declared to take an rtx. */
9822 union { const rtx_insn *c; rtx_insn *r; } u;
9823
9824 u.c = follower;
9825 if (CROSSING_JUMP_P (followee))
9826 switch (get_attr_type (u.r))
9827 {
9828 case TYPE_BRANCH:
9829 if (get_attr_length (u.r) != 2)
9830 break;
9831 case TYPE_BRCC:
9832 case TYPE_BRCC_NO_DELAY_SLOT:
9833 return false;
9834 default:
9835 return true;
9836 }
9837 return true;
9838 }
9839
9840 /* Return the register number of the register holding the return address
9841 for a function of type TYPE. */
9842
9843 int
9844 arc_return_address_register (unsigned int fn_type)
9845 {
9846 int regno = 0;
9847
9848 if (ARC_INTERRUPT_P (fn_type))
9849 {
9850 if (((fn_type & ARC_FUNCTION_ILINK1) | ARC_FUNCTION_FIRQ) != 0)
9851 regno = ILINK1_REGNUM;
9852 else if ((fn_type & ARC_FUNCTION_ILINK2) != 0)
9853 regno = ILINK2_REGNUM;
9854 else
9855 gcc_unreachable ();
9856 }
9857 else if (ARC_NORMAL_P (fn_type) || ARC_NAKED_P (fn_type))
9858 regno = RETURN_ADDR_REGNUM;
9859
9860 gcc_assert (regno != 0);
9861 return regno;
9862 }
9863
9864 /* Implement EPILOGUE_USES.
9865 Return true if REGNO should be added to the deemed uses of the epilogue.
9866
9867 We have to make sure all the register restore instructions are
9868 known to be live in interrupt functions, plus the blink register if
9869 it is clobbered by the isr. */
9870
9871 bool
9872 arc_epilogue_uses (int regno)
9873 {
9874 unsigned int fn_type;
9875
9876 if (regno == arc_tp_regno)
9877 return true;
9878
9879 fn_type = arc_compute_function_type (cfun);
9880 if (reload_completed)
9881 {
9882 if (ARC_INTERRUPT_P (cfun->machine->fn_type))
9883 {
9884 if (!fixed_regs[regno])
9885 return true;
9886 return ((regno == arc_return_address_register (fn_type))
9887 || (regno == RETURN_ADDR_REGNUM));
9888 }
9889 else
9890 return regno == RETURN_ADDR_REGNUM;
9891 }
9892 else
9893 return regno == arc_return_address_register (fn_type);
9894 }
9895
9896 /* Helper for EH_USES macro. */
9897
9898 bool
9899 arc_eh_uses (int regno)
9900 {
9901 if (regno == arc_tp_regno)
9902 return true;
9903 return false;
9904 }
9905
9906 #ifndef TARGET_NO_LRA
9907 #define TARGET_NO_LRA !TARGET_LRA
9908 #endif
9909
9910 static bool
9911 arc_lra_p (void)
9912 {
9913 return !TARGET_NO_LRA;
9914 }
9915
9916 /* ??? Should we define TARGET_REGISTER_PRIORITY? We might perfer to use
9917 Rcq registers, because some insn are shorter with them. OTOH we already
9918 have separate alternatives for this purpose, and other insns don't
9919 mind, so maybe we should rather prefer the other registers?
9920 We need more data, and we can only get that if we allow people to
9921 try all options. */
9922 static int
9923 arc_register_priority (int r)
9924 {
9925 switch (arc_lra_priority_tag)
9926 {
9927 case ARC_LRA_PRIORITY_NONE:
9928 return 0;
9929 case ARC_LRA_PRIORITY_NONCOMPACT:
9930 return ((((r & 7) ^ 4) - 4) & 15) != r;
9931 case ARC_LRA_PRIORITY_COMPACT:
9932 return ((((r & 7) ^ 4) - 4) & 15) == r;
9933 default:
9934 gcc_unreachable ();
9935 }
9936 }
9937
9938 static reg_class_t
9939 arc_spill_class (reg_class_t /* orig_class */, machine_mode)
9940 {
9941 return GENERAL_REGS;
9942 }
9943
9944 bool
9945 arc_legitimize_reload_address (rtx *p, machine_mode mode, int opnum,
9946 int itype)
9947 {
9948 rtx x = *p;
9949 enum reload_type type = (enum reload_type) itype;
9950
9951 if (GET_CODE (x) == PLUS
9952 && CONST_INT_P (XEXP (x, 1))
9953 && (RTX_OK_FOR_BASE_P (XEXP (x, 0), true)
9954 || (REG_P (XEXP (x, 0))
9955 && reg_equiv_constant (REGNO (XEXP (x, 0))))))
9956 {
9957 int scale = GET_MODE_SIZE (mode);
9958 int shift;
9959 rtx index_rtx = XEXP (x, 1);
9960 HOST_WIDE_INT offset = INTVAL (index_rtx), offset_base;
9961 rtx reg, sum, sum2;
9962
9963 if (scale > 4)
9964 scale = 4;
9965 if ((scale-1) & offset)
9966 scale = 1;
9967 shift = scale >> 1;
9968 offset_base
9969 = ((offset + (256 << shift))
9970 & ((HOST_WIDE_INT)((unsigned HOST_WIDE_INT) -512 << shift)));
9971 /* Sometimes the normal form does not suit DImode. We
9972 could avoid that by using smaller ranges, but that
9973 would give less optimized code when SImode is
9974 prevalent. */
9975 if (GET_MODE_SIZE (mode) + offset - offset_base <= (256 << shift))
9976 {
9977 int regno;
9978
9979 reg = XEXP (x, 0);
9980 regno = REGNO (reg);
9981 sum2 = sum = plus_constant (Pmode, reg, offset_base);
9982
9983 if (reg_equiv_constant (regno))
9984 {
9985 sum2 = plus_constant (Pmode, reg_equiv_constant (regno),
9986 offset_base);
9987 if (GET_CODE (sum2) == PLUS)
9988 sum2 = gen_rtx_CONST (Pmode, sum2);
9989 }
9990 *p = gen_rtx_PLUS (Pmode, sum, GEN_INT (offset - offset_base));
9991 push_reload (sum2, NULL_RTX, &XEXP (*p, 0), NULL,
9992 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0, opnum,
9993 type);
9994 return true;
9995 }
9996 }
9997 /* We must re-recognize what we created before. */
9998 else if (GET_CODE (x) == PLUS
9999 && GET_CODE (XEXP (x, 0)) == PLUS
10000 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
10001 && REG_P (XEXP (XEXP (x, 0), 0))
10002 && CONST_INT_P (XEXP (x, 1)))
10003 {
10004 /* Because this address is so complex, we know it must have
10005 been created by LEGITIMIZE_RELOAD_ADDRESS before; thus,
10006 it is already unshared, and needs no further unsharing. */
10007 push_reload (XEXP (x, 0), NULL_RTX, &XEXP (x, 0), NULL,
10008 BASE_REG_CLASS, Pmode, VOIDmode, 0, 0, opnum, type);
10009 return true;
10010 }
10011 return false;
10012 }
10013
10014 /* Implement TARGET_USE_BY_PIECES_INFRASTRUCTURE_P. */
10015
10016 static bool
10017 arc_use_by_pieces_infrastructure_p (unsigned HOST_WIDE_INT size,
10018 unsigned int align,
10019 enum by_pieces_operation op,
10020 bool speed_p)
10021 {
10022 /* Let the movmem expander handle small block moves. */
10023 if (op == MOVE_BY_PIECES)
10024 return false;
10025
10026 return default_use_by_pieces_infrastructure_p (size, align, op, speed_p);
10027 }
10028
10029 /* Emit a (pre) memory barrier around an atomic sequence according to
10030 MODEL. */
10031
10032 static void
10033 arc_pre_atomic_barrier (enum memmodel model)
10034 {
10035 if (need_atomic_barrier_p (model, true))
10036 emit_insn (gen_memory_barrier ());
10037 }
10038
10039 /* Emit a (post) memory barrier around an atomic sequence according to
10040 MODEL. */
10041
10042 static void
10043 arc_post_atomic_barrier (enum memmodel model)
10044 {
10045 if (need_atomic_barrier_p (model, false))
10046 emit_insn (gen_memory_barrier ());
10047 }
10048
10049 /* Expand a compare and swap pattern. */
10050
10051 static void
10052 emit_unlikely_jump (rtx insn)
10053 {
10054 rtx_insn *jump = emit_jump_insn (insn);
10055 add_reg_br_prob_note (jump, profile_probability::very_unlikely ());
10056 }
10057
10058 /* Expand code to perform a 8 or 16-bit compare and swap by doing
10059 32-bit compare and swap on the word containing the byte or
10060 half-word. The difference between a weak and a strong CAS is that
10061 the weak version may simply fail. The strong version relies on two
10062 loops, one checks if the SCOND op is succsfully or not, the other
10063 checks if the 32 bit accessed location which contains the 8 or 16
10064 bit datum is not changed by other thread. The first loop is
10065 implemented by the atomic_compare_and_swapsi_1 pattern. The second
10066 loops is implemented by this routine. */
10067
10068 static void
10069 arc_expand_compare_and_swap_qh (rtx bool_result, rtx result, rtx mem,
10070 rtx oldval, rtx newval, rtx weak,
10071 rtx mod_s, rtx mod_f)
10072 {
10073 rtx addr1 = force_reg (Pmode, XEXP (mem, 0));
10074 rtx addr = gen_reg_rtx (Pmode);
10075 rtx off = gen_reg_rtx (SImode);
10076 rtx oldv = gen_reg_rtx (SImode);
10077 rtx newv = gen_reg_rtx (SImode);
10078 rtx oldvalue = gen_reg_rtx (SImode);
10079 rtx newvalue = gen_reg_rtx (SImode);
10080 rtx res = gen_reg_rtx (SImode);
10081 rtx resv = gen_reg_rtx (SImode);
10082 rtx memsi, val, mask, end_label, loop_label, cc, x;
10083 machine_mode mode;
10084 bool is_weak = (weak != const0_rtx);
10085
10086 /* Truncate the address. */
10087 emit_insn (gen_rtx_SET (addr,
10088 gen_rtx_AND (Pmode, addr1, GEN_INT (-4))));
10089
10090 /* Compute the datum offset. */
10091 emit_insn (gen_rtx_SET (off,
10092 gen_rtx_AND (SImode, addr1, GEN_INT (3))));
10093 if (TARGET_BIG_ENDIAN)
10094 emit_insn (gen_rtx_SET (off,
10095 gen_rtx_MINUS (SImode,
10096 (GET_MODE (mem) == QImode) ?
10097 GEN_INT (3) : GEN_INT (2), off)));
10098
10099 /* Normal read from truncated address. */
10100 memsi = gen_rtx_MEM (SImode, addr);
10101 set_mem_alias_set (memsi, ALIAS_SET_MEMORY_BARRIER);
10102 MEM_VOLATILE_P (memsi) = MEM_VOLATILE_P (mem);
10103
10104 val = copy_to_reg (memsi);
10105
10106 /* Convert the offset in bits. */
10107 emit_insn (gen_rtx_SET (off,
10108 gen_rtx_ASHIFT (SImode, off, GEN_INT (3))));
10109
10110 /* Get the proper mask. */
10111 if (GET_MODE (mem) == QImode)
10112 mask = force_reg (SImode, GEN_INT (0xff));
10113 else
10114 mask = force_reg (SImode, GEN_INT (0xffff));
10115
10116 emit_insn (gen_rtx_SET (mask,
10117 gen_rtx_ASHIFT (SImode, mask, off)));
10118
10119 /* Prepare the old and new values. */
10120 emit_insn (gen_rtx_SET (val,
10121 gen_rtx_AND (SImode, gen_rtx_NOT (SImode, mask),
10122 val)));
10123
10124 oldval = gen_lowpart (SImode, oldval);
10125 emit_insn (gen_rtx_SET (oldv,
10126 gen_rtx_ASHIFT (SImode, oldval, off)));
10127
10128 newval = gen_lowpart_common (SImode, newval);
10129 emit_insn (gen_rtx_SET (newv,
10130 gen_rtx_ASHIFT (SImode, newval, off)));
10131
10132 emit_insn (gen_rtx_SET (oldv,
10133 gen_rtx_AND (SImode, oldv, mask)));
10134
10135 emit_insn (gen_rtx_SET (newv,
10136 gen_rtx_AND (SImode, newv, mask)));
10137
10138 if (!is_weak)
10139 {
10140 end_label = gen_label_rtx ();
10141 loop_label = gen_label_rtx ();
10142 emit_label (loop_label);
10143 }
10144
10145 /* Make the old and new values. */
10146 emit_insn (gen_rtx_SET (oldvalue,
10147 gen_rtx_IOR (SImode, oldv, val)));
10148
10149 emit_insn (gen_rtx_SET (newvalue,
10150 gen_rtx_IOR (SImode, newv, val)));
10151
10152 /* Try an 32bit atomic compare and swap. It clobbers the CC
10153 register. */
10154 emit_insn (gen_atomic_compare_and_swapsi_1 (res, memsi, oldvalue, newvalue,
10155 weak, mod_s, mod_f));
10156
10157 /* Regardless of the weakness of the operation, a proper boolean
10158 result needs to be provided. */
10159 x = gen_rtx_REG (CC_Zmode, CC_REG);
10160 x = gen_rtx_EQ (SImode, x, const0_rtx);
10161 emit_insn (gen_rtx_SET (bool_result, x));
10162
10163 if (!is_weak)
10164 {
10165 /* Check the results: if the atomic op is successfully the goto
10166 to end label. */
10167 x = gen_rtx_REG (CC_Zmode, CC_REG);
10168 x = gen_rtx_EQ (VOIDmode, x, const0_rtx);
10169 x = gen_rtx_IF_THEN_ELSE (VOIDmode, x,
10170 gen_rtx_LABEL_REF (Pmode, end_label), pc_rtx);
10171 emit_jump_insn (gen_rtx_SET (pc_rtx, x));
10172
10173 /* Wait for the right moment when the accessed 32-bit location
10174 is stable. */
10175 emit_insn (gen_rtx_SET (resv,
10176 gen_rtx_AND (SImode, gen_rtx_NOT (SImode, mask),
10177 res)));
10178 mode = SELECT_CC_MODE (NE, resv, val);
10179 cc = gen_rtx_REG (mode, CC_REG);
10180 emit_insn (gen_rtx_SET (cc, gen_rtx_COMPARE (mode, resv, val)));
10181
10182 /* Set the new value of the 32 bit location, proper masked. */
10183 emit_insn (gen_rtx_SET (val, resv));
10184
10185 /* Try again if location is unstable. Fall through if only
10186 scond op failed. */
10187 x = gen_rtx_NE (VOIDmode, cc, const0_rtx);
10188 x = gen_rtx_IF_THEN_ELSE (VOIDmode, x,
10189 gen_rtx_LABEL_REF (Pmode, loop_label), pc_rtx);
10190 emit_unlikely_jump (gen_rtx_SET (pc_rtx, x));
10191
10192 emit_label (end_label);
10193 }
10194
10195 /* End: proper return the result for the given mode. */
10196 emit_insn (gen_rtx_SET (res,
10197 gen_rtx_AND (SImode, res, mask)));
10198
10199 emit_insn (gen_rtx_SET (res,
10200 gen_rtx_LSHIFTRT (SImode, res, off)));
10201
10202 emit_move_insn (result, gen_lowpart (GET_MODE (result), res));
10203 }
10204
10205 /* Helper function used by "atomic_compare_and_swap" expand
10206 pattern. */
10207
10208 void
10209 arc_expand_compare_and_swap (rtx operands[])
10210 {
10211 rtx bval, rval, mem, oldval, newval, is_weak, mod_s, mod_f, x;
10212 machine_mode mode;
10213
10214 bval = operands[0];
10215 rval = operands[1];
10216 mem = operands[2];
10217 oldval = operands[3];
10218 newval = operands[4];
10219 is_weak = operands[5];
10220 mod_s = operands[6];
10221 mod_f = operands[7];
10222 mode = GET_MODE (mem);
10223
10224 if (reg_overlap_mentioned_p (rval, oldval))
10225 oldval = copy_to_reg (oldval);
10226
10227 if (mode == SImode)
10228 {
10229 emit_insn (gen_atomic_compare_and_swapsi_1 (rval, mem, oldval, newval,
10230 is_weak, mod_s, mod_f));
10231 x = gen_rtx_REG (CC_Zmode, CC_REG);
10232 x = gen_rtx_EQ (SImode, x, const0_rtx);
10233 emit_insn (gen_rtx_SET (bval, x));
10234 }
10235 else
10236 {
10237 arc_expand_compare_and_swap_qh (bval, rval, mem, oldval, newval,
10238 is_weak, mod_s, mod_f);
10239 }
10240 }
10241
10242 /* Helper function used by the "atomic_compare_and_swapsi_1"
10243 pattern. */
10244
10245 void
10246 arc_split_compare_and_swap (rtx operands[])
10247 {
10248 rtx rval, mem, oldval, newval;
10249 machine_mode mode;
10250 enum memmodel mod_s, mod_f;
10251 bool is_weak;
10252 rtx label1, label2, x, cond;
10253
10254 rval = operands[0];
10255 mem = operands[1];
10256 oldval = operands[2];
10257 newval = operands[3];
10258 is_weak = (operands[4] != const0_rtx);
10259 mod_s = (enum memmodel) INTVAL (operands[5]);
10260 mod_f = (enum memmodel) INTVAL (operands[6]);
10261 mode = GET_MODE (mem);
10262
10263 /* ARC atomic ops work only with 32-bit aligned memories. */
10264 gcc_assert (mode == SImode);
10265
10266 arc_pre_atomic_barrier (mod_s);
10267
10268 label1 = NULL_RTX;
10269 if (!is_weak)
10270 {
10271 label1 = gen_label_rtx ();
10272 emit_label (label1);
10273 }
10274 label2 = gen_label_rtx ();
10275
10276 /* Load exclusive. */
10277 emit_insn (gen_arc_load_exclusivesi (rval, mem));
10278
10279 /* Check if it is oldval. */
10280 mode = SELECT_CC_MODE (NE, rval, oldval);
10281 cond = gen_rtx_REG (mode, CC_REG);
10282 emit_insn (gen_rtx_SET (cond, gen_rtx_COMPARE (mode, rval, oldval)));
10283
10284 x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
10285 x = gen_rtx_IF_THEN_ELSE (VOIDmode, x,
10286 gen_rtx_LABEL_REF (Pmode, label2), pc_rtx);
10287 emit_unlikely_jump (gen_rtx_SET (pc_rtx, x));
10288
10289 /* Exclusively store new item. Store clobbers CC reg. */
10290 emit_insn (gen_arc_store_exclusivesi (mem, newval));
10291
10292 if (!is_weak)
10293 {
10294 /* Check the result of the store. */
10295 cond = gen_rtx_REG (CC_Zmode, CC_REG);
10296 x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
10297 x = gen_rtx_IF_THEN_ELSE (VOIDmode, x,
10298 gen_rtx_LABEL_REF (Pmode, label1), pc_rtx);
10299 emit_unlikely_jump (gen_rtx_SET (pc_rtx, x));
10300 }
10301
10302 if (mod_f != MEMMODEL_RELAXED)
10303 emit_label (label2);
10304
10305 arc_post_atomic_barrier (mod_s);
10306
10307 if (mod_f == MEMMODEL_RELAXED)
10308 emit_label (label2);
10309 }
10310
10311 /* Expand an atomic fetch-and-operate pattern. CODE is the binary operation
10312 to perform. MEM is the memory on which to operate. VAL is the second
10313 operand of the binary operator. BEFORE and AFTER are optional locations to
10314 return the value of MEM either before of after the operation. MODEL_RTX
10315 is a CONST_INT containing the memory model to use. */
10316
10317 void
10318 arc_expand_atomic_op (enum rtx_code code, rtx mem, rtx val,
10319 rtx orig_before, rtx orig_after, rtx model_rtx)
10320 {
10321 enum memmodel model = (enum memmodel) INTVAL (model_rtx);
10322 machine_mode mode = GET_MODE (mem);
10323 rtx label, x, cond;
10324 rtx before = orig_before, after = orig_after;
10325
10326 /* ARC atomic ops work only with 32-bit aligned memories. */
10327 gcc_assert (mode == SImode);
10328
10329 arc_pre_atomic_barrier (model);
10330
10331 label = gen_label_rtx ();
10332 emit_label (label);
10333 label = gen_rtx_LABEL_REF (VOIDmode, label);
10334
10335 if (before == NULL_RTX)
10336 before = gen_reg_rtx (mode);
10337
10338 if (after == NULL_RTX)
10339 after = gen_reg_rtx (mode);
10340
10341 /* Load exclusive. */
10342 emit_insn (gen_arc_load_exclusivesi (before, mem));
10343
10344 switch (code)
10345 {
10346 case NOT:
10347 x = gen_rtx_AND (mode, before, val);
10348 emit_insn (gen_rtx_SET (after, x));
10349 x = gen_rtx_NOT (mode, after);
10350 emit_insn (gen_rtx_SET (after, x));
10351 break;
10352
10353 case MINUS:
10354 if (CONST_INT_P (val))
10355 {
10356 val = GEN_INT (-INTVAL (val));
10357 code = PLUS;
10358 }
10359
10360 /* FALLTHRU. */
10361 default:
10362 x = gen_rtx_fmt_ee (code, mode, before, val);
10363 emit_insn (gen_rtx_SET (after, x));
10364 break;
10365 }
10366
10367 /* Exclusively store new item. Store clobbers CC reg. */
10368 emit_insn (gen_arc_store_exclusivesi (mem, after));
10369
10370 /* Check the result of the store. */
10371 cond = gen_rtx_REG (CC_Zmode, CC_REG);
10372 x = gen_rtx_NE (VOIDmode, cond, const0_rtx);
10373 x = gen_rtx_IF_THEN_ELSE (VOIDmode, x,
10374 label, pc_rtx);
10375 emit_unlikely_jump (gen_rtx_SET (pc_rtx, x));
10376
10377 arc_post_atomic_barrier (model);
10378 }
10379
10380 /* Implement TARGET_NO_SPECULATION_IN_DELAY_SLOTS_P. */
10381
10382 static bool
10383 arc_no_speculation_in_delay_slots_p ()
10384 {
10385 return true;
10386 }
10387
10388 /* Return a parallel of registers to represent where to find the
10389 register pieces if required, otherwise NULL_RTX. */
10390
10391 static rtx
10392 arc_dwarf_register_span (rtx rtl)
10393 {
10394 machine_mode mode = GET_MODE (rtl);
10395 unsigned regno;
10396 rtx p;
10397
10398 if (GET_MODE_SIZE (mode) != 8)
10399 return NULL_RTX;
10400
10401 p = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (2));
10402 regno = REGNO (rtl);
10403 XVECEXP (p, 0, 0) = gen_rtx_REG (SImode, regno);
10404 XVECEXP (p, 0, 1) = gen_rtx_REG (SImode, regno + 1);
10405
10406 return p;
10407 }
10408
10409 /* Return true if OP is an acceptable memory operand for ARCompact
10410 16-bit load instructions of MODE.
10411
10412 AV2SHORT: TRUE if address needs to fit into the new ARCv2 short
10413 non scaled instructions.
10414
10415 SCALED: TRUE if address can be scaled. */
10416
10417 bool
10418 compact_memory_operand_p (rtx op, machine_mode mode,
10419 bool av2short, bool scaled)
10420 {
10421 rtx addr, plus0, plus1;
10422 int size, off;
10423
10424 /* Eliminate non-memory operations. */
10425 if (GET_CODE (op) != MEM)
10426 return 0;
10427
10428 /* .di instructions have no 16-bit form. */
10429 if (MEM_VOLATILE_P (op) && !TARGET_VOLATILE_CACHE_SET)
10430 return false;
10431
10432 if (mode == VOIDmode)
10433 mode = GET_MODE (op);
10434
10435 size = GET_MODE_SIZE (mode);
10436
10437 /* dword operations really put out 2 instructions, so eliminate
10438 them. */
10439 if (size > UNITS_PER_WORD)
10440 return false;
10441
10442 /* Decode the address now. */
10443 addr = XEXP (op, 0);
10444 switch (GET_CODE (addr))
10445 {
10446 case REG:
10447 return (REGNO (addr) >= FIRST_PSEUDO_REGISTER
10448 || COMPACT_GP_REG_P (REGNO (addr))
10449 || (SP_REG_P (REGNO (addr)) && (size != 2)));
10450 case PLUS:
10451 plus0 = XEXP (addr, 0);
10452 plus1 = XEXP (addr, 1);
10453
10454 if ((GET_CODE (plus0) == REG)
10455 && ((REGNO (plus0) >= FIRST_PSEUDO_REGISTER)
10456 || COMPACT_GP_REG_P (REGNO (plus0)))
10457 && ((GET_CODE (plus1) == REG)
10458 && ((REGNO (plus1) >= FIRST_PSEUDO_REGISTER)
10459 || COMPACT_GP_REG_P (REGNO (plus1)))))
10460 {
10461 return !av2short;
10462 }
10463
10464 if ((GET_CODE (plus0) == REG)
10465 && ((REGNO (plus0) >= FIRST_PSEUDO_REGISTER)
10466 || (COMPACT_GP_REG_P (REGNO (plus0)) && !av2short)
10467 || (IN_RANGE (REGNO (plus0), 0, 31) && av2short))
10468 && (GET_CODE (plus1) == CONST_INT))
10469 {
10470 bool valid = false;
10471
10472 off = INTVAL (plus1);
10473
10474 /* Negative offset is not supported in 16-bit load/store insns. */
10475 if (off < 0)
10476 return 0;
10477
10478 /* Only u5 immediates allowed in code density instructions. */
10479 if (av2short)
10480 {
10481 switch (size)
10482 {
10483 case 1:
10484 return false;
10485 case 2:
10486 /* This is an ldh_s.x instruction, check the u6
10487 immediate. */
10488 if (COMPACT_GP_REG_P (REGNO (plus0)))
10489 valid = true;
10490 break;
10491 case 4:
10492 /* Only u5 immediates allowed in 32bit access code
10493 density instructions. */
10494 if (REGNO (plus0) <= 31)
10495 return ((off < 32) && (off % 4 == 0));
10496 break;
10497 default:
10498 return false;
10499 }
10500 }
10501 else
10502 if (COMPACT_GP_REG_P (REGNO (plus0)))
10503 valid = true;
10504
10505 if (valid)
10506 {
10507
10508 switch (size)
10509 {
10510 case 1:
10511 return (off < 32);
10512 case 2:
10513 /* The 6-bit constant get shifted to fit the real
10514 5-bits field. Check also for the alignment. */
10515 return ((off < 64) && (off % 2 == 0));
10516 case 4:
10517 return ((off < 128) && (off % 4 == 0));
10518 default:
10519 return false;
10520 }
10521 }
10522 }
10523
10524 if (REG_P (plus0) && CONST_INT_P (plus1)
10525 && ((REGNO (plus0) >= FIRST_PSEUDO_REGISTER)
10526 || SP_REG_P (REGNO (plus0)))
10527 && !av2short)
10528 {
10529 off = INTVAL (plus1);
10530 return ((size != 2) && (off >= 0 && off < 128) && (off % 4 == 0));
10531 }
10532
10533 if ((GET_CODE (plus0) == MULT)
10534 && (GET_CODE (XEXP (plus0, 0)) == REG)
10535 && ((REGNO (XEXP (plus0, 0)) >= FIRST_PSEUDO_REGISTER)
10536 || COMPACT_GP_REG_P (REGNO (XEXP (plus0, 0))))
10537 && (GET_CODE (plus1) == REG)
10538 && ((REGNO (plus1) >= FIRST_PSEUDO_REGISTER)
10539 || COMPACT_GP_REG_P (REGNO (plus1))))
10540 return scaled;
10541 default:
10542 break ;
10543 /* TODO: 'gp' and 'pcl' are to supported as base address operand
10544 for 16-bit load instructions. */
10545 }
10546 return false;
10547 }
10548
10549 /* Implement TARGET_USE_ANCHORS_FOR_SYMBOL_P. We don't want to use
10550 anchors for small data: the GP register acts as an anchor in that
10551 case. We also don't want to use them for PC-relative accesses,
10552 where the PC acts as an anchor. Prohibit also TLS symbols to use
10553 anchors. */
10554
10555 static bool
10556 arc_use_anchors_for_symbol_p (const_rtx symbol)
10557 {
10558 if (SYMBOL_REF_TLS_MODEL (symbol))
10559 return false;
10560
10561 if (flag_pic)
10562 return false;
10563
10564 if (SYMBOL_REF_SMALL_P (symbol))
10565 return false;
10566
10567 return default_use_anchors_for_symbol_p (symbol);
10568 }
10569
10570 #undef TARGET_USE_ANCHORS_FOR_SYMBOL_P
10571 #define TARGET_USE_ANCHORS_FOR_SYMBOL_P arc_use_anchors_for_symbol_p
10572
10573 struct gcc_target targetm = TARGET_INITIALIZER;
10574
10575 #include "gt-arc.h"