Debian Codegraph @

gcc/config/lm32/lm32.cc

raw
1 /* Subroutines used for code generation on the Lattice Mico32 architecture.
2 Contributed by Jon Beniston <[email protected]>
3
4 Copyright (C) 2009-2025 Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published
10 by the Free Software Foundation; either version 3, or (at your
11 option) any later version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
16 License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 #define IN_TARGET_CODE 1
23
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "backend.h"
28 #include "target.h"
29 #include "rtl.h"
30 #include "tree.h"
31 #include "stringpool.h"
32 #include "attribs.h"
33 #include "df.h"
34 #include "memmodel.h"
35 #include "tm_p.h"
36 #include "optabs.h"
37 #include "regs.h"
38 #include "emit-rtl.h"
39 #include "recog.h"
40 #include "output.h"
41 #include "calls.h"
42 #include "alias.h"
43 #include "explow.h"
44 #include "expr.h"
45 #include "tm-constrs.h"
46 #include "builtins.h"
47 #include "langhooks.h"
48 #include "stor-layout.h"
49 #include "fold-const.h"
50 #include "gimple.h"
51 #include "gimplify.h"
52
53 /* This file should be included last. */
54 #include "target-def.h"
55
56 struct lm32_frame_info
57 {
58 HOST_WIDE_INT total_size; /* number of bytes of entire frame. */
59 HOST_WIDE_INT callee_size; /* number of bytes to save callee saves. */
60 HOST_WIDE_INT pretend_size; /* number of bytes we pretend caller did. */
61 HOST_WIDE_INT args_size; /* number of bytes for outgoing arguments. */
62 HOST_WIDE_INT locals_size; /* number of bytes for local variables. */
63 unsigned int reg_save_mask; /* mask of saved registers. */
64 };
65
66 /* Prototypes for static functions. */
67 static rtx emit_add (rtx dest, rtx src0, rtx src1);
68 static void expand_save_restore (struct lm32_frame_info *info, int op);
69 static void stack_adjust (HOST_WIDE_INT amount);
70 static bool lm32_in_small_data_p (const_tree);
71 static void lm32_setup_incoming_varargs (cumulative_args_t cum,
72 const function_arg_info &,
73 int *pretend_size, int no_rtl);
74 static tree lm32_build_builtin_va_list (void);
75 static void lm32_builtin_va_start (tree valist, rtx nextarg);
76 static tree lm32_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p,
77 gimple_seq *post_p);
78 static bool lm32_rtx_costs (rtx x, machine_mode mode, int outer_code, int opno,
79 int *total, bool speed);
80 static bool lm32_can_eliminate (const int, const int);
81 static bool lm32_legitimate_address_p (machine_mode mode, rtx x, bool strict,
82 code_helper = ERROR_MARK);
83 static HOST_WIDE_INT lm32_compute_frame_size (int size);
84 static void lm32_option_override (void);
85 static rtx lm32_function_arg (cumulative_args_t, const function_arg_info &);
86 static void lm32_function_arg_advance (cumulative_args_t cum,
87 const function_arg_info &);
88 static bool lm32_hard_regno_mode_ok (unsigned int, machine_mode);
89 static bool lm32_modes_tieable_p (machine_mode, machine_mode);
90 static HOST_WIDE_INT lm32_starting_frame_offset (void);
91
92 #undef TARGET_OPTION_OVERRIDE
93 #define TARGET_OPTION_OVERRIDE lm32_option_override
94 #undef TARGET_ADDRESS_COST
95 #define TARGET_ADDRESS_COST hook_int_rtx_mode_as_bool_0
96 #undef TARGET_RTX_COSTS
97 #define TARGET_RTX_COSTS lm32_rtx_costs
98 #undef TARGET_IN_SMALL_DATA_P
99 #define TARGET_IN_SMALL_DATA_P lm32_in_small_data_p
100 #undef TARGET_PROMOTE_FUNCTION_MODE
101 #define TARGET_PROMOTE_FUNCTION_MODE default_promote_function_mode_always_promote
102 #undef TARGET_SETUP_INCOMING_VARARGS
103 #define TARGET_SETUP_INCOMING_VARARGS lm32_setup_incoming_varargs
104 #undef TARGET_BUILD_BUILTIN_VA_LIST
105 #define TARGET_BUILD_BUILTIN_VA_LIST lm32_build_builtin_va_list
106 #undef TARGET_EXPAND_BUILTIN_VA_START
107 #define TARGET_EXPAND_BUILTIN_VA_START lm32_builtin_va_start
108 #undef TARGET_GIMPLIFY_VA_ARG_EXPR
109 #define TARGET_GIMPLIFY_VA_ARG_EXPR lm32_gimplify_va_arg_expr
110
111 #undef TARGET_FUNCTION_ARG
112 #define TARGET_FUNCTION_ARG lm32_function_arg
113 #undef TARGET_FUNCTION_ARG_ADVANCE
114 #define TARGET_FUNCTION_ARG_ADVANCE lm32_function_arg_advance
115 #undef TARGET_PROMOTE_PROTOTYPES
116 #define TARGET_PROMOTE_PROTOTYPES hook_bool_const_tree_true
117 #undef TARGET_MIN_ANCHOR_OFFSET
118 #define TARGET_MIN_ANCHOR_OFFSET -0x8000
119 #undef TARGET_MAX_ANCHOR_OFFSET
120 #define TARGET_MAX_ANCHOR_OFFSET 0x7fff
121 #undef TARGET_CAN_ELIMINATE
122 #define TARGET_CAN_ELIMINATE lm32_can_eliminate
123 #undef TARGET_LRA_P
124 #define TARGET_LRA_P hook_bool_void_false
125 #undef TARGET_LEGITIMATE_ADDRESS_P
126 #define TARGET_LEGITIMATE_ADDRESS_P lm32_legitimate_address_p
127 #undef TARGET_HARD_REGNO_MODE_OK
128 #define TARGET_HARD_REGNO_MODE_OK lm32_hard_regno_mode_ok
129 #undef TARGET_MODES_TIEABLE_P
130 #define TARGET_MODES_TIEABLE_P lm32_modes_tieable_p
131
132 #undef TARGET_CONSTANT_ALIGNMENT
133 #define TARGET_CONSTANT_ALIGNMENT constant_alignment_word_strings
134
135 #undef TARGET_STARTING_FRAME_OFFSET
136 #define TARGET_STARTING_FRAME_OFFSET lm32_starting_frame_offset
137
138 struct gcc_target targetm = TARGET_INITIALIZER;
139
140 /* Current frame information calculated by lm32_compute_frame_size. */
141 static struct lm32_frame_info current_frame_info;
142
143 /* Return non-zero if the given return type should be returned in memory. */
144
145 int
146 lm32_return_in_memory (tree type)
147 {
148 HOST_WIDE_INT size;
149
150 if (!AGGREGATE_TYPE_P (type))
151 {
152 /* All simple types are returned in registers. */
153 return 0;
154 }
155
156 size = int_size_in_bytes (type);
157 if (size >= 0 && size <= UNITS_PER_WORD)
158 {
159 /* If it can fit in one register. */
160 return 0;
161 }
162
163 return 1;
164 }
165
166 /* Generate an emit a word sized add instruction. */
167
168 static rtx
169 emit_add (rtx dest, rtx src0, rtx src1)
170 {
171 rtx insn;
172 insn = emit_insn (gen_addsi3 (dest, src0, src1));
173 return insn;
174 }
175
176 /* Generate the code to compare (and possibly branch) two integer values
177 TEST_CODE is the comparison code we are trying to emulate
178 (or implement directly)
179 RESULT is where to store the result of the comparison,
180 or null to emit a branch
181 CMP0 CMP1 are the two comparison operands
182 DESTINATION is the destination of the branch, or null to only compare
183 */
184
185 static void
186 gen_int_relational (enum rtx_code code,
187 rtx result,
188 rtx cmp0,
189 rtx cmp1,
190 rtx destination)
191 {
192 machine_mode mode;
193 int branch_p;
194
195 mode = GET_MODE (cmp0);
196 if (mode == VOIDmode)
197 mode = GET_MODE (cmp1);
198
199 /* Is this a branch or compare. */
200 branch_p = (destination != 0);
201
202 /* Instruction set doesn't support LE or LT, so swap operands and use
203 GE, GT. */
204 switch (code)
205 {
206 case LE:
207 case LT:
208 case LEU:
209 case LTU:
210 {
211 rtx temp;
212
213 code = swap_condition (code);
214 temp = cmp0;
215 cmp0 = cmp1;
216 cmp1 = temp;
217 break;
218 }
219 default:
220 break;
221 }
222
223 if (branch_p)
224 {
225 rtx insn, cond, label;
226
227 /* Operands must be in registers. */
228 if (!register_operand (cmp0, mode))
229 cmp0 = force_reg (mode, cmp0);
230 if (!register_operand (cmp1, mode))
231 cmp1 = force_reg (mode, cmp1);
232
233 /* Generate conditional branch instruction. */
234 cond = gen_rtx_fmt_ee (code, mode, cmp0, cmp1);
235 label = gen_rtx_LABEL_REF (VOIDmode, destination);
236 insn = gen_rtx_SET (pc_rtx, gen_rtx_IF_THEN_ELSE (VOIDmode,
237 cond, label, pc_rtx));
238 emit_jump_insn (insn);
239 }
240 else
241 {
242 /* We can't have const_ints in cmp0, other than 0. */
243 if ((GET_CODE (cmp0) == CONST_INT) && (INTVAL (cmp0) != 0))
244 cmp0 = force_reg (mode, cmp0);
245
246 /* If the comparison is against an int not in legal range
247 move it into a register. */
248 if (GET_CODE (cmp1) == CONST_INT)
249 {
250 switch (code)
251 {
252 case EQ:
253 case NE:
254 case LE:
255 case LT:
256 case GE:
257 case GT:
258 if (!satisfies_constraint_K (cmp1))
259 cmp1 = force_reg (mode, cmp1);
260 break;
261 case LEU:
262 case LTU:
263 case GEU:
264 case GTU:
265 if (!satisfies_constraint_L (cmp1))
266 cmp1 = force_reg (mode, cmp1);
267 break;
268 default:
269 gcc_unreachable ();
270 }
271 }
272
273 /* Generate compare instruction. */
274 emit_move_insn (result, gen_rtx_fmt_ee (code, mode, cmp0, cmp1));
275 }
276 }
277
278 /* Try performing the comparison in OPERANDS[1], whose arms are OPERANDS[2]
279 and OPERAND[3]. Store the result in OPERANDS[0]. */
280
281 void
282 lm32_expand_scc (rtx operands[])
283 {
284 rtx target = operands[0];
285 enum rtx_code code = GET_CODE (operands[1]);
286 rtx op0 = operands[2];
287 rtx op1 = operands[3];
288
289 gen_int_relational (code, target, op0, op1, NULL_RTX);
290 }
291
292 /* Compare OPERANDS[1] with OPERANDS[2] using comparison code
293 CODE and jump to OPERANDS[3] if the condition holds. */
294
295 void
296 lm32_expand_conditional_branch (rtx operands[])
297 {
298 enum rtx_code code = GET_CODE (operands[0]);
299 rtx op0 = operands[1];
300 rtx op1 = operands[2];
301 rtx destination = operands[3];
302
303 gen_int_relational (code, NULL_RTX, op0, op1, destination);
304 }
305
306 /* Generate and emit RTL to save or restore callee save registers. */
307 static void
308 expand_save_restore (struct lm32_frame_info *info, int op)
309 {
310 unsigned int reg_save_mask = info->reg_save_mask;
311 int regno;
312 HOST_WIDE_INT offset;
313 rtx insn;
314
315 /* Callee saves are below locals and above outgoing arguments. */
316 offset = info->args_size + info->callee_size;
317 for (regno = 0; regno <= 31; regno++)
318 {
319 if ((reg_save_mask & (1 << regno)) != 0)
320 {
321 rtx offset_rtx;
322 rtx mem;
323
324 offset_rtx = GEN_INT (offset);
325 if (satisfies_constraint_K (offset_rtx))
326 {
327 mem = gen_rtx_MEM (word_mode,
328 gen_rtx_PLUS (Pmode,
329 stack_pointer_rtx,
330 offset_rtx));
331 }
332 else
333 {
334 /* r10 is caller saved so it can be used as a temp reg. */
335 rtx r10;
336
337 r10 = gen_rtx_REG (word_mode, 10);
338 insn = emit_move_insn (r10, offset_rtx);
339 if (op == 0)
340 RTX_FRAME_RELATED_P (insn) = 1;
341 insn = emit_add (r10, r10, stack_pointer_rtx);
342 if (op == 0)
343 RTX_FRAME_RELATED_P (insn) = 1;
344 mem = gen_rtx_MEM (word_mode, r10);
345 }
346
347 if (op == 0)
348 insn = emit_move_insn (mem, gen_rtx_REG (word_mode, regno));
349 else
350 insn = emit_move_insn (gen_rtx_REG (word_mode, regno), mem);
351
352 /* only prologue instructions which set the sp fp or save a
353 register should be marked as frame related. */
354 if (op == 0)
355 RTX_FRAME_RELATED_P (insn) = 1;
356 offset -= UNITS_PER_WORD;
357 }
358 }
359 }
360
361 static void
362 stack_adjust (HOST_WIDE_INT amount)
363 {
364 rtx insn;
365
366 if (!IN_RANGE (amount, -32776, 32768))
367 {
368 /* r10 is caller saved so it can be used as a temp reg. */
369 rtx r10;
370 r10 = gen_rtx_REG (word_mode, 10);
371 insn = emit_move_insn (r10, GEN_INT (amount));
372 if (amount < 0)
373 RTX_FRAME_RELATED_P (insn) = 1;
374 insn = emit_add (stack_pointer_rtx, stack_pointer_rtx, r10);
375 if (amount < 0)
376 RTX_FRAME_RELATED_P (insn) = 1;
377 }
378 else
379 {
380 insn = emit_add (stack_pointer_rtx,
381 stack_pointer_rtx, GEN_INT (amount));
382 if (amount < 0)
383 RTX_FRAME_RELATED_P (insn) = 1;
384 }
385 }
386
387
388 /* Create and emit instructions for a functions prologue. */
389 void
390 lm32_expand_prologue (void)
391 {
392 rtx insn;
393
394 lm32_compute_frame_size (get_frame_size ());
395
396 if (current_frame_info.total_size > 0)
397 {
398 /* Add space on stack new frame. */
399 stack_adjust (-current_frame_info.total_size);
400
401 /* Save callee save registers. */
402 if (current_frame_info.reg_save_mask != 0)
403 expand_save_restore (&current_frame_info, 0);
404
405 /* Setup frame pointer if it's needed. */
406 if (frame_pointer_needed == 1)
407 {
408 /* Move sp to fp. */
409 insn = emit_move_insn (frame_pointer_rtx, stack_pointer_rtx);
410 RTX_FRAME_RELATED_P (insn) = 1;
411
412 /* Add offset - Don't use total_size, as that includes pretend_size,
413 which isn't part of this frame? */
414 insn = emit_add (frame_pointer_rtx,
415 frame_pointer_rtx,
416 GEN_INT (current_frame_info.args_size +
417 current_frame_info.callee_size +
418 current_frame_info.locals_size));
419 RTX_FRAME_RELATED_P (insn) = 1;
420 }
421
422 /* Prevent prologue from being scheduled into function body. */
423 emit_insn (gen_blockage ());
424 }
425 }
426
427 /* Create an emit instructions for a functions epilogue. */
428 void
429 lm32_expand_epilogue (void)
430 {
431 rtx ra_rtx = gen_rtx_REG (Pmode, RA_REGNUM);
432
433 lm32_compute_frame_size (get_frame_size ());
434
435 if (current_frame_info.total_size > 0)
436 {
437 /* Prevent stack code from being reordered. */
438 emit_insn (gen_blockage ());
439
440 /* Restore callee save registers. */
441 if (current_frame_info.reg_save_mask != 0)
442 expand_save_restore (&current_frame_info, 1);
443
444 /* Deallocate stack. */
445 stack_adjust (current_frame_info.total_size);
446
447 /* Return to calling function. */
448 emit_jump_insn (gen_return_internal (ra_rtx));
449 }
450 else
451 {
452 /* Return to calling function. */
453 emit_jump_insn (gen_return_internal (ra_rtx));
454 }
455 }
456
457 /* Return the bytes needed to compute the frame pointer from the current
458 stack pointer. */
459 static HOST_WIDE_INT
460 lm32_compute_frame_size (int size)
461 {
462 int regno;
463 HOST_WIDE_INT total_size, locals_size, args_size, pretend_size, callee_size;
464 unsigned int reg_save_mask;
465
466 locals_size = size;
467 args_size = crtl->outgoing_args_size;
468 pretend_size = crtl->args.pretend_args_size;
469 callee_size = 0;
470 reg_save_mask = 0;
471
472 /* Build mask that actually determines which regsiters we save
473 and calculate size required to store them in the stack. */
474 for (regno = 1; regno < SP_REGNUM; regno++)
475 {
476 if (df_regs_ever_live_p (regno) && !call_used_or_fixed_reg_p (regno))
477 {
478 reg_save_mask |= 1 << regno;
479 callee_size += UNITS_PER_WORD;
480 }
481 }
482 if (df_regs_ever_live_p (RA_REGNUM) || ! crtl->is_leaf
483 || !optimize)
484 {
485 reg_save_mask |= 1 << RA_REGNUM;
486 callee_size += UNITS_PER_WORD;
487 }
488 if (!(reg_save_mask & (1 << FP_REGNUM)) && frame_pointer_needed)
489 {
490 reg_save_mask |= 1 << FP_REGNUM;
491 callee_size += UNITS_PER_WORD;
492 }
493
494 /* Compute total frame size. */
495 total_size = pretend_size + args_size + locals_size + callee_size;
496
497 /* Align frame to appropriate boundary. */
498 total_size = (total_size + 3) & ~3;
499
500 /* Save computed information. */
501 current_frame_info.total_size = total_size;
502 current_frame_info.callee_size = callee_size;
503 current_frame_info.pretend_size = pretend_size;
504 current_frame_info.locals_size = locals_size;
505 current_frame_info.args_size = args_size;
506 current_frame_info.reg_save_mask = reg_save_mask;
507
508 return total_size;
509 }
510
511 void
512 lm32_print_operand (FILE * file, rtx op, int letter)
513 {
514 enum rtx_code code;
515
516 code = GET_CODE (op);
517
518 if (code == SIGN_EXTEND)
519 op = XEXP (op, 0), code = GET_CODE (op);
520 else if (code == REG || code == SUBREG)
521 {
522 int regnum;
523
524 if (code == REG)
525 regnum = REGNO (op);
526 else
527 regnum = true_regnum (op);
528
529 fprintf (file, "%s", reg_names[regnum]);
530 }
531 else if (code == HIGH)
532 output_addr_const (file, XEXP (op, 0));
533 else if (code == MEM)
534 output_address (GET_MODE (op), XEXP (op, 0));
535 else if (letter == 'z' && GET_CODE (op) == CONST_INT && INTVAL (op) == 0)
536 fprintf (file, "%s", reg_names[0]);
537 else if (GET_CODE (op) == CONST_DOUBLE)
538 {
539 if ((CONST_DOUBLE_LOW (op) != 0) || (CONST_DOUBLE_HIGH (op) != 0))
540 output_operand_lossage ("only 0.0 can be loaded as an immediate");
541 else
542 fprintf (file, "0");
543 }
544 else if (code == EQ)
545 fprintf (file, "e ");
546 else if (code == NE)
547 fprintf (file, "ne ");
548 else if (code == GT)
549 fprintf (file, "g ");
550 else if (code == GTU)
551 fprintf (file, "gu ");
552 else if (code == LT)
553 fprintf (file, "l ");
554 else if (code == LTU)
555 fprintf (file, "lu ");
556 else if (code == GE)
557 fprintf (file, "ge ");
558 else if (code == GEU)
559 fprintf (file, "geu");
560 else if (code == LE)
561 fprintf (file, "le ");
562 else if (code == LEU)
563 fprintf (file, "leu");
564 else
565 output_addr_const (file, op);
566 }
567
568 /* A C compound statement to output to stdio stream STREAM the
569 assembler syntax for an instruction operand that is a memory
570 reference whose address is ADDR. ADDR is an RTL expression.
571
572 On some machines, the syntax for a symbolic address depends on
573 the section that the address refers to. On these machines,
574 define the macro `ENCODE_SECTION_INFO' to store the information
575 into the `symbol_ref', and then check for it here. */
576
577 void
578 lm32_print_operand_address (FILE * file, rtx addr)
579 {
580 switch (GET_CODE (addr))
581 {
582 case REG:
583 fprintf (file, "(%s+0)", reg_names[REGNO (addr)]);
584 break;
585
586 case MEM:
587 output_address (VOIDmode, XEXP (addr, 0));
588 break;
589
590 case PLUS:
591 {
592 rtx arg0 = XEXP (addr, 0);
593 rtx arg1 = XEXP (addr, 1);
594
595 if (GET_CODE (arg0) == REG && CONSTANT_P (arg1))
596 {
597 if (GET_CODE (arg1) == CONST_INT)
598 fprintf (file, "(%s+%ld)", reg_names[REGNO (arg0)],
599 INTVAL (arg1));
600 else
601 {
602 fprintf (file, "(%s+", reg_names[REGNO (arg0)]);
603 output_addr_const (file, arg1);
604 fprintf (file, ")");
605 }
606 }
607 else if (CONSTANT_P (arg0) && CONSTANT_P (arg1))
608 output_addr_const (file, addr);
609 else
610 fatal_insn ("bad operand", addr);
611 }
612 break;
613
614 case SYMBOL_REF:
615 if (SYMBOL_REF_SMALL_P (addr))
616 {
617 fprintf (file, "gp(");
618 output_addr_const (file, addr);
619 fprintf (file, ")");
620 }
621 else
622 fatal_insn ("can't use non gp relative absolute address", addr);
623 break;
624
625 default:
626 fatal_insn ("invalid addressing mode", addr);
627 break;
628 }
629 }
630
631 /* Determine where to put an argument to a function.
632 Value is zero to push the argument on the stack,
633 or a hard register in which to store the argument.
634
635 CUM is a variable of type CUMULATIVE_ARGS which gives info about
636 the preceding args and about the function being called.
637 ARG is a description of the argument. */
638
639 static rtx
640 lm32_function_arg (cumulative_args_t cum_v, const function_arg_info &arg)
641 {
642 CUMULATIVE_ARGS *cum = get_cumulative_args (cum_v);
643
644 if (arg.end_marker_p ())
645 /* Compute operand 2 of the call insn. */
646 return GEN_INT (0);
647
648 if (targetm.calls.must_pass_in_stack (arg))
649 return NULL_RTX;
650
651 if (*cum + LM32_NUM_REGS2 (arg.mode, arg.type) > LM32_NUM_ARG_REGS)
652 return NULL_RTX;
653
654 return gen_rtx_REG (arg.mode, *cum + LM32_FIRST_ARG_REG);
655 }
656
657 static void
658 lm32_function_arg_advance (cumulative_args_t cum,
659 const function_arg_info &arg)
660 {
661 *get_cumulative_args (cum) += LM32_NUM_REGS2 (arg.mode, arg.type);
662 }
663
664 HOST_WIDE_INT
665 lm32_compute_initial_elimination_offset (int from, int to)
666 {
667 HOST_WIDE_INT offset = 0;
668
669 switch (from)
670 {
671 case ARG_POINTER_REGNUM:
672 switch (to)
673 {
674 case FRAME_POINTER_REGNUM:
675 offset = 0;
676 break;
677 case STACK_POINTER_REGNUM:
678 offset =
679 lm32_compute_frame_size (get_frame_size ()) -
680 current_frame_info.pretend_size;
681 break;
682 default:
683 gcc_unreachable ();
684 }
685 break;
686 default:
687 gcc_unreachable ();
688 }
689
690 return offset;
691 }
692
693 static void
694 lm32_setup_incoming_varargs (cumulative_args_t cum_v,
695 const function_arg_info &arg,
696 int *pretend_size, int no_rtl)
697 {
698 CUMULATIVE_ARGS next_cum = *get_cumulative_args (cum_v);
699 int first_anon_arg;
700 tree fntype;
701
702 fntype = TREE_TYPE (current_function_decl);
703
704 if (!TYPE_NO_NAMED_ARGS_STDARG_P (TREE_TYPE (current_function_decl))
705 || arg.type != NULL_TREE)
706 lm32_function_arg_advance (pack_cumulative_args (&next_cum), arg);
707
708 if (stdarg_p (fntype))
709 first_anon_arg = next_cum + LM32_FIRST_ARG_REG;
710 else
711 {
712 /* this is the common case, we have been passed details setup
713 for the last named argument, we want to skip over the
714 registers, if any used in passing this named parameter in
715 order to determine which is the first registers used to pass
716 anonymous arguments. */
717 int size = arg.promoted_size_in_bytes ();
718
719 first_anon_arg =
720 next_cum + LM32_FIRST_ARG_REG +
721 ((size + UNITS_PER_WORD - 1) / UNITS_PER_WORD);
722 }
723
724 if (FUNCTION_ARG_REGNO_P (first_anon_arg))
725 {
726 int size = LM32_FIRST_ARG_REG + LM32_NUM_ARG_REGS - first_anon_arg;
727
728 if (!no_rtl)
729 {
730 rtx regblock
731 = gen_rtx_MEM (BLKmode,
732 plus_constant (Pmode, arg_pointer_rtx,
733 FIRST_PARM_OFFSET (0)));
734 move_block_from_reg (first_anon_arg, regblock, size);
735 }
736
737 *pretend_size = size * UNITS_PER_WORD;
738 }
739 }
740 /* This is the "struct __va_list". */
741
742 static GTY(()) tree va_list_type;
743
744 /* Implement TARGET_BUILD_BUILTIN_VA_LIST. */
745
746 static tree
747 lm32_build_builtin_va_list (void)
748 {
749 /* We keep one pointer and a count
750
751 The pointer is the regular void *
752
753 The count tracks how many registers arguments
754 remain. When that goes to zero, we have to skip
755 over the reserved space that was the top of the
756 stack at function entry
757
758 */
759 tree va_list_name;
760 tree ap_field;
761 tree ap_reg_field;
762
763 va_list_type = lang_hooks.types.make_type (RECORD_TYPE);
764 /* Name it */
765 va_list_name = build_decl (BUILTINS_LOCATION,
766 TYPE_DECL,
767 get_identifier ("__va_list"),
768 va_list_type);
769
770 DECL_ARTIFICIAL (va_list_name) = 1;
771 TYPE_NAME (va_list_type) = va_list_name;
772 TYPE_STUB_DECL (va_list_type) = va_list_name;
773
774 ap_field = build_decl (BUILTINS_LOCATION,
775 FIELD_DECL,
776 get_identifier("__ap"),
777 ptr_type_node);
778 DECL_ARTIFICIAL (ap_field) = 1;
779 DECL_FIELD_CONTEXT (ap_field) = va_list_type;
780 TYPE_FIELDS (va_list_type) = ap_field;
781
782 ap_reg_field = build_decl(BUILTINS_LOCATION,
783 FIELD_DECL,
784 get_identifier("__ap_reg"),
785 ptr_type_node);
786 DECL_ARTIFICIAL (ap_reg_field) = 1;
787 DECL_FIELD_CONTEXT (ap_reg_field) = va_list_type;
788 DECL_CHAIN (ap_field) = ap_reg_field;
789
790 layout_type (va_list_type);
791
792 return va_list_type;
793 }
794
795 /* Implement TARGET_EXPAND_BUILTIN_VA_START. */
796
797 static void
798 lm32_builtin_va_start (tree valist, rtx nextarg)
799 {
800 const CUMULATIVE_ARGS *cum;
801 tree ap_field, ap_reg_field;
802 tree ap, ap_reg;
803 tree t;
804 int pretend_args_size = crtl->args.pretend_args_size;
805 cum = &crtl->args.info;
806
807 ap_field = TYPE_FIELDS(TREE_TYPE (valist));
808 ap = build3 (COMPONENT_REF, TREE_TYPE (ap_field), valist,
809 ap_field, NULL_TREE);
810
811 std_expand_builtin_va_start (ap, nextarg);
812
813 ap_reg_field = DECL_CHAIN(ap_field);
814 ap_reg = build3 (COMPONENT_REF, TREE_TYPE (ap_reg_field), valist,
815 ap_reg_field, NULL_TREE);
816
817 /* Emit code to initialize __ap_reg */
818
819 rtx last_reg_arg = expand_binop (ptr_mode, add_optab,
820 crtl->args.internal_arg_pointer,
821 gen_int_mode (pretend_args_size, Pmode),
822 NULL_RTX, 0, OPTAB_LIB_WIDEN);
823
824 rtx ap_reg_r = expand_expr (ap_reg, NULL_RTX, VOIDmode, EXPAND_WRITE);
825 convert_move (ap_reg_r, last_reg_arg, 0);
826 }
827
828 #ifndef PAD_VARARGS_DOWN
829 #define PAD_VARARGS_DOWN BYTES_BIG_ENDIAN
830 #endif
831
832 /*
833 * This was copied from "standard" implementation of va_arg, and then
834 * handling for overflow of the register parameters added.
835 */
836
837 static tree
838 lm32_std_gimplify_va_arg_expr (tree valist, tree ap_reg, tree type, gimple_seq *pre_p,
839 gimple_seq *post_p)
840 {
841 tree addr, t, type_size, rounded_size, valist_tmp;
842 unsigned HOST_WIDE_INT align, boundary;
843 bool indirect;
844
845 /* All of the alignment and movement below is for args-grow-up machines.
846 As of 2004, there are only 3 ARGS_GROW_DOWNWARD targets, and they all
847 implement their own specialized gimplify_va_arg_expr routines. */
848 if (ARGS_GROW_DOWNWARD)
849 gcc_unreachable ();
850
851 indirect = pass_va_arg_by_reference (type);
852 if (indirect)
853 type = build_pointer_type (type);
854
855 if (targetm.calls.split_complex_arg
856 && TREE_CODE (type) == COMPLEX_TYPE
857 && targetm.calls.split_complex_arg (type))
858 {
859 tree real_part, imag_part;
860
861 real_part = std_gimplify_va_arg_expr (valist,
862 TREE_TYPE (type), pre_p, NULL);
863 real_part = get_initialized_tmp_var (real_part, pre_p);
864
865 imag_part = std_gimplify_va_arg_expr (unshare_expr (valist),
866 TREE_TYPE (type), pre_p, NULL);
867 imag_part = get_initialized_tmp_var (imag_part, pre_p);
868
869 return build2 (COMPLEX_EXPR, type, real_part, imag_part);
870 }
871
872 align = PARM_BOUNDARY / BITS_PER_UNIT;
873 boundary = targetm.calls.function_arg_boundary (TYPE_MODE (type), type);
874
875 /* When we align parameter on stack for caller, if the parameter
876 alignment is beyond MAX_SUPPORTED_STACK_ALIGNMENT, it will be
877 aligned at MAX_SUPPORTED_STACK_ALIGNMENT. We will match callee
878 here with caller. */
879 if (boundary > MAX_SUPPORTED_STACK_ALIGNMENT)
880 boundary = MAX_SUPPORTED_STACK_ALIGNMENT;
881
882 boundary /= BITS_PER_UNIT;
883
884 /* Hoist the valist value into a temporary for the moment. */
885 valist_tmp = get_initialized_tmp_var (valist, pre_p);
886
887 /* va_list pointer is aligned to PARM_BOUNDARY. If argument actually
888 requires greater alignment, we must perform dynamic alignment. */
889 if (boundary > align
890 && !TYPE_EMPTY_P (type)
891 && !integer_zerop (TYPE_SIZE (type)))
892 {
893 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
894 fold_build_pointer_plus_hwi (valist_tmp, boundary - 1));
895 gimplify_and_add (t, pre_p);
896
897 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp,
898 fold_build2 (BIT_AND_EXPR, TREE_TYPE (valist),
899 valist_tmp,
900 build_int_cst (TREE_TYPE (valist), -boundary)));
901 gimplify_and_add (t, pre_p);
902 }
903 else
904 boundary = align;
905
906 /* If the actual alignment is less than the alignment of the type,
907 adjust the type accordingly so that we don't assume strict alignment
908 when dereferencing the pointer. */
909 boundary *= BITS_PER_UNIT;
910 if (boundary < TYPE_ALIGN (type))
911 {
912 type = build_variant_type_copy (type);
913 SET_TYPE_ALIGN (type, boundary);
914 }
915
916 /* Compute the rounded size of the type. */
917 type_size = arg_size_in_bytes (type);
918 rounded_size = round_up (type_size, align);
919
920 /* Reduce rounded_size so it's sharable with the postqueue. */
921 gimplify_expr (&rounded_size, pre_p, post_p, is_gimple_val, fb_rvalue);
922
923 /*
924 * Check for a large parameter which didn't fit in the remaining registers
925 * and got pushed off to the stack instead
926 */
927 if (int_size_in_bytes(type) > UNITS_PER_WORD) {
928
929 /* Hoist the ap_reg value into a temporary for the moment. */
930 tree ap_reg_tmp = get_initialized_tmp_var (ap_reg, pre_p);
931
932 t = fold_build2_loc (input_location, TRUTH_AND_EXPR,
933 boolean_type_node,
934 fold_build2_loc (input_location, LT_EXPR, boolean_type_node,
935 valist_tmp, ap_reg_tmp),
936 fold_build2_loc (input_location, GT_EXPR, boolean_type_node,
937 fold_build_pointer_plus (valist_tmp, rounded_size), ap_reg_tmp));
938
939 t = fold_build3 (COND_EXPR, TREE_TYPE(valist), t, ap_reg_tmp, valist_tmp);
940
941 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist_tmp, t);
942
943 gimplify_and_add (t, pre_p);
944 }
945
946 /* Get AP. */
947 addr = valist_tmp;
948 if (PAD_VARARGS_DOWN && !integer_zerop (rounded_size))
949 {
950 /* Small args are padded downward. */
951 t = fold_build2_loc (input_location, GT_EXPR, sizetype,
952 rounded_size, size_int (align));
953 t = fold_build3 (COND_EXPR, sizetype, t, size_zero_node,
954 size_binop (MINUS_EXPR, rounded_size, type_size));
955 addr = fold_build_pointer_plus (addr, t);
956 }
957
958 /* Compute new value for AP. */
959 t = fold_build_pointer_plus (valist_tmp, rounded_size);
960 t = build2 (MODIFY_EXPR, TREE_TYPE (valist), valist, t);
961 gimplify_and_add (t, pre_p);
962
963 addr = fold_convert (build_pointer_type (type), addr);
964
965 if (indirect)
966 addr = build_va_arg_indirect_ref (addr);
967
968 return build_va_arg_indirect_ref (addr);
969 }
970
971 /* Return an expression of type "void *" pointing to the next
972 available argument in a variable-argument list. VALIST is the
973 user-level va_list object, of type __builtin_va_list. */
974 /* Implement TARGET_GIMPLIFY_VA_ARG_EXPR. */
975 static tree
976 lm32_gimplify_va_arg_expr (tree valist, tree type, gimple_seq *pre_p,
977 gimple_seq *post_p)
978 {
979 tree ap_field = TYPE_FIELDS(TREE_TYPE (valist));
980 tree ap_reg_field = DECL_CHAIN(ap_field);
981 tree ap = build3 (COMPONENT_REF, TREE_TYPE (ap_field), valist,
982 ap_field, NULL_TREE);
983 tree ap_reg = build3 (COMPONENT_REF, TREE_TYPE (ap_reg_field), valist,
984 ap_reg_field, NULL_TREE);
985
986 return lm32_std_gimplify_va_arg_expr(ap, ap_reg, type, pre_p, post_p);
987 }
988
989 /* Override command line options. */
990 static void
991 lm32_option_override (void)
992 {
993 /* We must have sign-extend enabled if barrel-shift isn't. */
994 if (!TARGET_BARREL_SHIFT_ENABLED && !TARGET_SIGN_EXTEND_ENABLED)
995 target_flags |= MASK_SIGN_EXTEND_ENABLED;
996 }
997
998 /* Return nonzero if this function is known to have a null epilogue.
999 This allows the optimizer to omit jumps to jumps if no stack
1000 was created. */
1001 int
1002 lm32_can_use_return (void)
1003 {
1004 if (!reload_completed)
1005 return 0;
1006
1007 if (df_regs_ever_live_p (RA_REGNUM) || crtl->profile)
1008 return 0;
1009
1010 if (lm32_compute_frame_size (get_frame_size ()) != 0)
1011 return 0;
1012
1013 return 1;
1014 }
1015
1016 /* Support function to determine the return address of the function
1017 'count' frames back up the stack. */
1018 rtx
1019 lm32_return_addr_rtx (int count, rtx frame)
1020 {
1021 rtx r;
1022 if (count == 0)
1023 {
1024 if (!df_regs_ever_live_p (RA_REGNUM))
1025 r = gen_rtx_REG (Pmode, RA_REGNUM);
1026 else
1027 {
1028 r = gen_rtx_MEM (Pmode,
1029 gen_rtx_PLUS (Pmode, frame,
1030 GEN_INT (-2 * UNITS_PER_WORD)));
1031 set_mem_alias_set (r, get_frame_alias_set ());
1032 }
1033 }
1034 else if (flag_omit_frame_pointer)
1035 r = NULL_RTX;
1036 else
1037 {
1038 r = gen_rtx_MEM (Pmode,
1039 gen_rtx_PLUS (Pmode, frame,
1040 GEN_INT (-2 * UNITS_PER_WORD)));
1041 set_mem_alias_set (r, get_frame_alias_set ());
1042 }
1043 return r;
1044 }
1045
1046 /* Return true if EXP should be placed in the small data section. */
1047
1048 static bool
1049 lm32_in_small_data_p (const_tree exp)
1050 {
1051 /* We want to merge strings, so we never consider them small data. */
1052 if (TREE_CODE (exp) == STRING_CST)
1053 return false;
1054
1055 /* Functions are never in the small data area. Duh. */
1056 if (TREE_CODE (exp) == FUNCTION_DECL)
1057 return false;
1058
1059 if (VAR_P (exp) && DECL_SECTION_NAME (exp))
1060 {
1061 const char *section = DECL_SECTION_NAME (exp);
1062 if (strcmp (section, ".sdata") == 0 || strcmp (section, ".sbss") == 0)
1063 return true;
1064 }
1065 else
1066 {
1067 HOST_WIDE_INT size = int_size_in_bytes (TREE_TYPE (exp));
1068
1069 /* If this is an incomplete type with size 0, then we can't put it
1070 in sdata because it might be too big when completed. */
1071 if (size > 0 && size <= g_switch_value)
1072 return true;
1073 }
1074
1075 return false;
1076 }
1077
1078 /* Emit straight-line code to move LENGTH bytes from SRC to DEST.
1079 Assume that the areas do not overlap. */
1080
1081 static void
1082 lm32_block_move_inline (rtx dest, rtx src, HOST_WIDE_INT length,
1083 HOST_WIDE_INT alignment)
1084 {
1085 HOST_WIDE_INT offset, delta;
1086 unsigned HOST_WIDE_INT bits;
1087 int i;
1088 machine_mode mode;
1089 rtx *regs;
1090
1091 /* Work out how many bits to move at a time. */
1092 switch (alignment)
1093 {
1094 case 1:
1095 bits = 8;
1096 break;
1097 case 2:
1098 bits = 16;
1099 break;
1100 default:
1101 bits = 32;
1102 break;
1103 }
1104
1105 mode = int_mode_for_size (bits, 0).require ();
1106 delta = bits / BITS_PER_UNIT;
1107
1108 /* Allocate a buffer for the temporary registers. */
1109 regs = XALLOCAVEC (rtx, length / delta);
1110
1111 /* Load as many BITS-sized chunks as possible. */
1112 for (offset = 0, i = 0; offset + delta <= length; offset += delta, i++)
1113 {
1114 regs[i] = gen_reg_rtx (mode);
1115 emit_move_insn (regs[i], adjust_address (src, mode, offset));
1116 }
1117
1118 /* Copy the chunks to the destination. */
1119 for (offset = 0, i = 0; offset + delta <= length; offset += delta, i++)
1120 emit_move_insn (adjust_address (dest, mode, offset), regs[i]);
1121
1122 /* Mop up any left-over bytes. */
1123 if (offset < length)
1124 {
1125 src = adjust_address (src, BLKmode, offset);
1126 dest = adjust_address (dest, BLKmode, offset);
1127 move_by_pieces (dest, src, length - offset,
1128 MIN (MEM_ALIGN (src), MEM_ALIGN (dest)), RETURN_BEGIN);
1129 }
1130 }
1131
1132 /* Expand string/block move operations.
1133
1134 operands[0] is the pointer to the destination.
1135 operands[1] is the pointer to the source.
1136 operands[2] is the number of bytes to move.
1137 operands[3] is the alignment. */
1138
1139 int
1140 lm32_expand_block_move (rtx * operands)
1141 {
1142 if ((GET_CODE (operands[2]) == CONST_INT) && (INTVAL (operands[2]) <= 32))
1143 {
1144 lm32_block_move_inline (operands[0], operands[1], INTVAL (operands[2]),
1145 INTVAL (operands[3]));
1146 return 1;
1147 }
1148 return 0;
1149 }
1150
1151 /* Return TRUE if X references a SYMBOL_REF or LABEL_REF whose symbol
1152 isn't protected by a PIC unspec. */
1153 int
1154 nonpic_symbol_mentioned_p (rtx x)
1155 {
1156 const char *fmt;
1157 int i;
1158
1159 if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF
1160 || GET_CODE (x) == PC)
1161 return 1;
1162
1163 /* We don't want to look into the possible MEM location of a
1164 CONST_DOUBLE, since we're not going to use it, in general. */
1165 if (GET_CODE (x) == CONST_DOUBLE)
1166 return 0;
1167
1168 if (GET_CODE (x) == UNSPEC)
1169 return 0;
1170
1171 fmt = GET_RTX_FORMAT (GET_CODE (x));
1172 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
1173 {
1174 if (fmt[i] == 'E')
1175 {
1176 int j;
1177
1178 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1179 if (nonpic_symbol_mentioned_p (XVECEXP (x, i, j)))
1180 return 1;
1181 }
1182 else if (fmt[i] == 'e' && nonpic_symbol_mentioned_p (XEXP (x, i)))
1183 return 1;
1184 }
1185
1186 return 0;
1187 }
1188
1189 /* Compute a (partial) cost for rtx X. Return true if the complete
1190 cost has been computed, and false if subexpressions should be
1191 scanned. In either case, *TOTAL contains the cost result. */
1192
1193 static bool
1194 lm32_rtx_costs (rtx x, machine_mode mode, int outer_code,
1195 int opno ATTRIBUTE_UNUSED, int *total, bool speed)
1196 {
1197 int code = GET_CODE (x);
1198 bool small_mode;
1199
1200 const int arithmetic_latency = 1;
1201 const int shift_latency = 1;
1202 const int compare_latency = 2;
1203 const int multiply_latency = 3;
1204 const int load_latency = 3;
1205 const int libcall_size_cost = 5;
1206
1207 /* Determine if we can handle the given mode size in a single instruction. */
1208 small_mode = (mode == QImode) || (mode == HImode) || (mode == SImode);
1209
1210 switch (code)
1211 {
1212
1213 case PLUS:
1214 case MINUS:
1215 case AND:
1216 case IOR:
1217 case XOR:
1218 case NOT:
1219 case NEG:
1220 if (!speed)
1221 *total = COSTS_N_INSNS (LM32_NUM_REGS (mode));
1222 else
1223 *total =
1224 COSTS_N_INSNS (arithmetic_latency + (LM32_NUM_REGS (mode) - 1));
1225 break;
1226
1227 case COMPARE:
1228 if (small_mode)
1229 {
1230 if (!speed)
1231 *total = COSTS_N_INSNS (1);
1232 else
1233 *total = COSTS_N_INSNS (compare_latency);
1234 }
1235 else
1236 {
1237 /* FIXME. Guessing here. */
1238 *total = COSTS_N_INSNS (LM32_NUM_REGS (mode) * (2 + 3) / 2);
1239 }
1240 break;
1241
1242 case ASHIFT:
1243 case ASHIFTRT:
1244 case LSHIFTRT:
1245 if (TARGET_BARREL_SHIFT_ENABLED && small_mode)
1246 {
1247 if (!speed)
1248 *total = COSTS_N_INSNS (1);
1249 else
1250 *total = COSTS_N_INSNS (shift_latency);
1251 }
1252 else if (TARGET_BARREL_SHIFT_ENABLED)
1253 {
1254 /* FIXME: Guessing here. */
1255 *total = COSTS_N_INSNS (LM32_NUM_REGS (mode) * 4);
1256 }
1257 else if (small_mode && GET_CODE (XEXP (x, 1)) == CONST_INT)
1258 {
1259 *total = COSTS_N_INSNS (INTVAL (XEXP (x, 1)));
1260 }
1261 else
1262 {
1263 /* Libcall. */
1264 if (!speed)
1265 *total = COSTS_N_INSNS (libcall_size_cost);
1266 else
1267 *total = COSTS_N_INSNS (100);
1268 }
1269 break;
1270
1271 case MULT:
1272 if (TARGET_MULTIPLY_ENABLED && small_mode)
1273 {
1274 if (!speed)
1275 *total = COSTS_N_INSNS (1);
1276 else
1277 *total = COSTS_N_INSNS (multiply_latency);
1278 }
1279 else
1280 {
1281 /* Libcall. */
1282 if (!speed)
1283 *total = COSTS_N_INSNS (libcall_size_cost);
1284 else
1285 *total = COSTS_N_INSNS (100);
1286 }
1287 break;
1288
1289 case DIV:
1290 case MOD:
1291 case UDIV:
1292 case UMOD:
1293 if (TARGET_DIVIDE_ENABLED && small_mode)
1294 {
1295 if (!speed)
1296 *total = COSTS_N_INSNS (1);
1297 else
1298 {
1299 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
1300 {
1301 int cycles = 0;
1302 unsigned HOST_WIDE_INT i = INTVAL (XEXP (x, 1));
1303
1304 while (i)
1305 {
1306 i >>= 2;
1307 cycles++;
1308 }
1309 if (IN_RANGE (i, 0, 65536))
1310 *total = COSTS_N_INSNS (1 + 1 + cycles);
1311 else
1312 *total = COSTS_N_INSNS (2 + 1 + cycles);
1313 return true;
1314 }
1315 else if (GET_CODE (XEXP (x, 1)) == REG)
1316 {
1317 *total = COSTS_N_INSNS (1 + GET_MODE_SIZE (mode) / 2);
1318 return true;
1319 }
1320 else
1321 {
1322 *total = COSTS_N_INSNS (1 + GET_MODE_SIZE (mode) / 2);
1323 return false;
1324 }
1325 }
1326 }
1327 else
1328 {
1329 /* Libcall. */
1330 if (!speed)
1331 *total = COSTS_N_INSNS (libcall_size_cost);
1332 else
1333 *total = COSTS_N_INSNS (100);
1334 }
1335 break;
1336
1337 case HIGH:
1338 case LO_SUM:
1339 if (!speed)
1340 *total = COSTS_N_INSNS (1);
1341 else
1342 *total = COSTS_N_INSNS (arithmetic_latency);
1343 break;
1344
1345 case ZERO_EXTEND:
1346 if (MEM_P (XEXP (x, 0)))
1347 *total = COSTS_N_INSNS (0);
1348 else if (small_mode)
1349 {
1350 if (!speed)
1351 *total = COSTS_N_INSNS (1);
1352 else
1353 *total = COSTS_N_INSNS (arithmetic_latency);
1354 }
1355 else
1356 *total = COSTS_N_INSNS (LM32_NUM_REGS (mode) / 2);
1357 break;
1358
1359 case CONST_INT:
1360 {
1361 switch (outer_code)
1362 {
1363 case HIGH:
1364 case LO_SUM:
1365 *total = COSTS_N_INSNS (0);
1366 return true;
1367
1368 case AND:
1369 case XOR:
1370 case IOR:
1371 case ASHIFT:
1372 case ASHIFTRT:
1373 case LSHIFTRT:
1374 case ROTATE:
1375 case ROTATERT:
1376 if (satisfies_constraint_L (x))
1377 *total = COSTS_N_INSNS (0);
1378 else
1379 *total = COSTS_N_INSNS (2);
1380 return true;
1381
1382 case SET:
1383 case PLUS:
1384 case MINUS:
1385 case COMPARE:
1386 if (satisfies_constraint_K (x))
1387 *total = COSTS_N_INSNS (0);
1388 else
1389 *total = COSTS_N_INSNS (2);
1390 return true;
1391
1392 case MULT:
1393 if (TARGET_MULTIPLY_ENABLED)
1394 {
1395 if (satisfies_constraint_K (x))
1396 *total = COSTS_N_INSNS (0);
1397 else
1398 *total = COSTS_N_INSNS (2);
1399 return true;
1400 }
1401 /* Fall through. */
1402
1403 default:
1404 if (satisfies_constraint_K (x))
1405 *total = COSTS_N_INSNS (1);
1406 else
1407 *total = COSTS_N_INSNS (2);
1408 return true;
1409 }
1410 }
1411
1412 case SYMBOL_REF:
1413 case CONST:
1414 switch (outer_code)
1415 {
1416 case HIGH:
1417 case LO_SUM:
1418 *total = COSTS_N_INSNS (0);
1419 return true;
1420
1421 case MEM:
1422 case SET:
1423 if (g_switch_value)
1424 {
1425 *total = COSTS_N_INSNS (0);
1426 return true;
1427 }
1428 break;
1429 }
1430 /* Fall through. */
1431
1432 case LABEL_REF:
1433 case CONST_DOUBLE:
1434 *total = COSTS_N_INSNS (2);
1435 return true;
1436
1437 case SET:
1438 *total = COSTS_N_INSNS (1);
1439 break;
1440
1441 case MEM:
1442 if (!speed)
1443 *total = COSTS_N_INSNS (1);
1444 else
1445 *total = COSTS_N_INSNS (load_latency);
1446 break;
1447
1448 }
1449
1450 return false;
1451 }
1452
1453 /* Implemenent TARGET_CAN_ELIMINATE. */
1454
1455 bool
1456 lm32_can_eliminate (const int from ATTRIBUTE_UNUSED, const int to)
1457 {
1458 return (to == STACK_POINTER_REGNUM && frame_pointer_needed) ? false : true;
1459 }
1460
1461 /* Implement TARGET_LEGITIMATE_ADDRESS_P. */
1462
1463 static bool
1464 lm32_legitimate_address_p (machine_mode mode ATTRIBUTE_UNUSED, rtx x,
1465 bool strict, code_helper)
1466 {
1467 /* (rM) */
1468 if (strict && REG_P (x) && STRICT_REG_OK_FOR_BASE_P (x))
1469 return true;
1470 if (!strict && REG_P (x) && NONSTRICT_REG_OK_FOR_BASE_P (x))
1471 return true;
1472
1473 /* (rM)+literal) */
1474 if (GET_CODE (x) == PLUS
1475 && REG_P (XEXP (x, 0))
1476 && ((strict && STRICT_REG_OK_FOR_BASE_P (XEXP (x, 0)))
1477 || (!strict && NONSTRICT_REG_OK_FOR_BASE_P (XEXP (x, 0))))
1478 && GET_CODE (XEXP (x, 1)) == CONST_INT
1479 && satisfies_constraint_K (XEXP ((x), 1)))
1480 return true;
1481
1482 /* gp(sym) */
1483 if (GET_CODE (x) == SYMBOL_REF && SYMBOL_REF_SMALL_P (x))
1484 return true;
1485
1486 return false;
1487 }
1488
1489 /* Check a move is not memory to memory. */
1490
1491 bool
1492 lm32_move_ok (machine_mode mode, rtx operands[2]) {
1493 if (memory_operand (operands[0], mode))
1494 return register_or_zero_operand (operands[1], mode);
1495 return true;
1496 }
1497
1498 /* Implement TARGET_HARD_REGNO_MODE_OK. */
1499
1500 static bool
1501 lm32_hard_regno_mode_ok (unsigned int regno, machine_mode)
1502 {
1503 return G_REG_P (regno);
1504 }
1505
1506 /* Implement TARGET_MODES_TIEABLE_P. */
1507
1508 static bool
1509 lm32_modes_tieable_p (machine_mode mode1, machine_mode mode2)
1510 {
1511 return (GET_MODE_CLASS (mode1) == MODE_INT
1512 && GET_MODE_CLASS (mode2) == MODE_INT
1513 && GET_MODE_SIZE (mode1) <= UNITS_PER_WORD
1514 && GET_MODE_SIZE (mode2) <= UNITS_PER_WORD);
1515 }
1516
1517 /* Implement TARGET_STARTING_FRAME_OFFSET. */
1518
1519 static HOST_WIDE_INT
1520 lm32_starting_frame_offset (void)
1521 {
1522 return UNITS_PER_WORD;
1523 }
1524
1525 #include "gt-lm32.h"