1/* Default target hook functions.
2   Copyright (C) 2003, 2004, 2005, 2007 Free Software Foundation, Inc.
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 2, or (at your option) any later
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
17along with GCC; see the file COPYING.  If not, write to the Free
18Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
1902110-1301, USA.  */
20
21/* The migration of target macros to target hooks works as follows:
22
23   1. Create a target hook that uses the existing target macros to
24      implement the same functionality.
25
26   2. Convert all the MI files to use the hook instead of the macro.
27
28   3. Repeat for a majority of the remaining target macros.  This will
29      take some time.
30
31   4. Tell target maintainers to start migrating.
32
33   5. Eventually convert the backends to override the hook instead of
34      defining the macros.  This will take some time too.
35
36   6. TBD when, poison the macros.  Unmigrated targets will break at
37      this point.
38
39   Note that we expect steps 1-3 to be done by the people that
40   understand what the MI does with each macro, and step 5 to be done
41   by the target maintainers for their respective targets.
42
43   Note that steps 1 and 2 don't have to be done together, but no
44   target can override the new hook until step 2 is complete for it.
45
46   Once the macros are poisoned, we will revert to the old migration
47   rules - migrate the macro, callers, and targets all at once.  This
48   comment can thus be removed at that point.  */
49
50#include "config.h"
51#include "system.h"
52#include "coretypes.h"
53#include "tm.h"
54#include "machmode.h"
55#include "rtl.h"
56#include "tree.h"
57#include "expr.h"
58#include "output.h"
59#include "toplev.h"
60#include "function.h"
61#include "target.h"
62#include "tm_p.h"
63#include "target-def.h"
64#include "ggc.h"
65#include "hard-reg-set.h"
66#include "reload.h"
67#include "optabs.h"
68#include "recog.h"
69
70
71void
72default_external_libcall (rtx fun ATTRIBUTE_UNUSED)
73{
74#ifdef ASM_OUTPUT_EXTERNAL_LIBCALL
75  ASM_OUTPUT_EXTERNAL_LIBCALL(asm_out_file, fun);
76#endif
77}
78
79enum machine_mode
80default_cc_modes_compatible (enum machine_mode m1, enum machine_mode m2)
81{
82  if (m1 == m2)
83    return m1;
84  return VOIDmode;
85}
86
87bool
88default_return_in_memory (tree type,
89			  tree fntype ATTRIBUTE_UNUSED)
90{
91#ifndef RETURN_IN_MEMORY
92  return (TYPE_MODE (type) == BLKmode);
93#else
94  return RETURN_IN_MEMORY (type);
95#endif
96}
97
98/* APPLE LOCAL begin radar 4781080 */
99bool
100default_objc_fpreturn_msgcall (tree type, bool no_long_double)
101{
102#ifndef OBJC_FPRETURN_MSGCALL
103  return type == NULL_TREE && no_long_double;
104#else
105  return OBJC_FPRETURN_MSGCALL (type, no_long_double);
106#endif
107}
108/* APPLE LOCAL end radar 4781080 */
109
110rtx
111default_expand_builtin_saveregs (void)
112{
113  error ("__builtin_saveregs not supported by this target");
114  return const0_rtx;
115}
116
117void
118default_setup_incoming_varargs (CUMULATIVE_ARGS *ca ATTRIBUTE_UNUSED,
119				enum machine_mode mode ATTRIBUTE_UNUSED,
120				tree type ATTRIBUTE_UNUSED,
121				int *pretend_arg_size ATTRIBUTE_UNUSED,
122				int second_time ATTRIBUTE_UNUSED)
123{
124}
125
126/* The default implementation of TARGET_BUILTIN_SETJMP_FRAME_VALUE.  */
127
128rtx
129default_builtin_setjmp_frame_value (void)
130{
131  return virtual_stack_vars_rtx;
132}
133
134/* Generic hook that takes a CUMULATIVE_ARGS pointer and returns false.  */
135
136bool
137hook_bool_CUMULATIVE_ARGS_false (CUMULATIVE_ARGS *ca ATTRIBUTE_UNUSED)
138{
139  return false;
140}
141
142bool
143default_pretend_outgoing_varargs_named (CUMULATIVE_ARGS *ca ATTRIBUTE_UNUSED)
144{
145  return (targetm.calls.setup_incoming_varargs
146	  != default_setup_incoming_varargs);
147}
148
149enum machine_mode
150default_eh_return_filter_mode (void)
151{
152  return word_mode;
153}
154
155/* The default implementation of TARGET_SHIFT_TRUNCATION_MASK.  */
156
157unsigned HOST_WIDE_INT
158default_shift_truncation_mask (enum machine_mode mode)
159{
160  return SHIFT_COUNT_TRUNCATED ? GET_MODE_BITSIZE (mode) - 1 : 0;
161}
162
163/* The default implementation of TARGET_MIN_DIVISIONS_FOR_RECIP_MUL.  */
164
165unsigned int
166default_min_divisions_for_recip_mul (enum machine_mode mode ATTRIBUTE_UNUSED)
167{
168  return have_insn_for (DIV, mode) ? 3 : 2;
169}
170
171/* The default implementation of TARGET_MODE_REP_EXTENDED.  */
172
173int
174default_mode_rep_extended (enum machine_mode mode ATTRIBUTE_UNUSED,
175			   enum machine_mode mode_rep ATTRIBUTE_UNUSED)
176{
177  return UNKNOWN;
178}
179
180/* Generic hook that takes a CUMULATIVE_ARGS pointer and returns true.  */
181
182bool
183hook_bool_CUMULATIVE_ARGS_true (CUMULATIVE_ARGS * a ATTRIBUTE_UNUSED)
184{
185  return true;
186}
187
188
189/* The generic C++ ABI specifies this is a 64-bit value.  */
190tree
191default_cxx_guard_type (void)
192{
193  return long_long_integer_type_node;
194}
195
196
197/* Returns the size of the cookie to use when allocating an array
198   whose elements have the indicated TYPE.  Assumes that it is already
199   known that a cookie is needed.  */
200
201tree
202default_cxx_get_cookie_size (tree type)
203{
204  tree cookie_size;
205
206  /* We need to allocate an additional max (sizeof (size_t), alignof
207     (true_type)) bytes.  */
208  tree sizetype_size;
209  tree type_align;
210
211  sizetype_size = size_in_bytes (sizetype);
212  type_align = size_int (TYPE_ALIGN_UNIT (type));
213  if (INT_CST_LT_UNSIGNED (type_align, sizetype_size))
214    cookie_size = sizetype_size;
215  else
216    cookie_size = type_align;
217
218  return cookie_size;
219}
220
221/* Return true if a parameter must be passed by reference.  This version
222   of the TARGET_PASS_BY_REFERENCE hook uses just MUST_PASS_IN_STACK.  */
223
224bool
225hook_pass_by_reference_must_pass_in_stack (CUMULATIVE_ARGS *c ATTRIBUTE_UNUSED,
226	enum machine_mode mode ATTRIBUTE_UNUSED, tree type ATTRIBUTE_UNUSED,
227	bool named_arg ATTRIBUTE_UNUSED)
228{
229  return targetm.calls.must_pass_in_stack (mode, type);
230}
231
232/* Return true if a parameter follows callee copies conventions.  This
233   version of the hook is true for all named arguments.  */
234
235bool
236hook_callee_copies_named (CUMULATIVE_ARGS *ca ATTRIBUTE_UNUSED,
237			  enum machine_mode mode ATTRIBUTE_UNUSED,
238			  tree type ATTRIBUTE_UNUSED, bool named)
239{
240  return named;
241}
242
243/* Emit any directives required to unwind this instruction.  */
244
245void
246default_unwind_emit (FILE * stream ATTRIBUTE_UNUSED,
247		     rtx insn ATTRIBUTE_UNUSED)
248{
249  /* Should never happen.  */
250  gcc_unreachable ();
251}
252
253/* True if MODE is valid for the target.  By "valid", we mean able to
254   be manipulated in non-trivial ways.  In particular, this means all
255   the arithmetic is supported.
256
257   By default we guess this means that any C type is supported.  If
258   we can't map the mode back to a type that would be available in C,
259   then reject it.  Special case, here, is the double-word arithmetic
260   supported by optabs.c.  */
261
262bool
263default_scalar_mode_supported_p (enum machine_mode mode)
264{
265  int precision = GET_MODE_PRECISION (mode);
266
267  switch (GET_MODE_CLASS (mode))
268    {
269    case MODE_PARTIAL_INT:
270    case MODE_INT:
271      if (precision == CHAR_TYPE_SIZE)
272	return true;
273      if (precision == SHORT_TYPE_SIZE)
274	return true;
275      if (precision == INT_TYPE_SIZE)
276	return true;
277      if (precision == LONG_TYPE_SIZE)
278	return true;
279      if (precision == LONG_LONG_TYPE_SIZE)
280	return true;
281      if (precision == 2 * BITS_PER_WORD)
282	return true;
283      return false;
284
285    case MODE_FLOAT:
286      if (precision == FLOAT_TYPE_SIZE)
287	return true;
288      if (precision == DOUBLE_TYPE_SIZE)
289	return true;
290      if (precision == LONG_DOUBLE_TYPE_SIZE)
291	return true;
292      return false;
293
294    case MODE_DECIMAL_FLOAT:
295      return false;
296
297    default:
298      gcc_unreachable ();
299    }
300}
301
302/* True if the target supports decimal floating point.  */
303
304bool
305default_decimal_float_supported_p (void)
306{
307  return ENABLE_DECIMAL_FLOAT;
308}
309
310/* NULL if INSN insn is valid within a low-overhead loop, otherwise returns
311   an error message.
312
313   This function checks whether a given INSN is valid within a low-overhead
314   loop.  If INSN is invalid it returns the reason for that, otherwise it
315   returns NULL. A called function may clobber any special registers required
316   for low-overhead looping. Additionally, some targets (eg, PPC) use the count
317   register for branch on table instructions. We reject the doloop pattern in
318   these cases.  */
319
320const char *
321default_invalid_within_doloop (rtx insn)
322{
323  if (CALL_P (insn))
324    return "Function call in loop.";
325
326  if (JUMP_P (insn)
327      && (GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC
328	  || GET_CODE (PATTERN (insn)) == ADDR_VEC))
329    return "Computed branch in the loop.";
330
331  return NULL;
332}
333
334bool
335hook_bool_CUMULATIVE_ARGS_mode_tree_bool_false (
336	CUMULATIVE_ARGS *ca ATTRIBUTE_UNUSED,
337	enum machine_mode mode ATTRIBUTE_UNUSED,
338	tree type ATTRIBUTE_UNUSED, bool named ATTRIBUTE_UNUSED)
339{
340  return false;
341}
342
343bool
344hook_bool_CUMULATIVE_ARGS_mode_tree_bool_true (
345	CUMULATIVE_ARGS *ca ATTRIBUTE_UNUSED,
346	enum machine_mode mode ATTRIBUTE_UNUSED,
347	tree type ATTRIBUTE_UNUSED, bool named ATTRIBUTE_UNUSED)
348{
349  return true;
350}
351
352int
353hook_int_CUMULATIVE_ARGS_mode_tree_bool_0 (
354	CUMULATIVE_ARGS *ca ATTRIBUTE_UNUSED,
355	enum machine_mode mode ATTRIBUTE_UNUSED,
356	tree type ATTRIBUTE_UNUSED, bool named ATTRIBUTE_UNUSED)
357{
358  return 0;
359}
360
361void
362hook_void_bitmap (bitmap regs ATTRIBUTE_UNUSED)
363{
364}
365
366const char *
367hook_invalid_arg_for_unprototyped_fn (
368	tree typelist ATTRIBUTE_UNUSED,
369	tree funcdecl ATTRIBUTE_UNUSED,
370	tree val ATTRIBUTE_UNUSED)
371{
372  return NULL;
373}
374
375/* Initialize the stack protection decls.  */
376
377/* Stack protection related decls living in libgcc.  */
378static GTY(()) tree stack_chk_guard_decl;
379
380tree
381default_stack_protect_guard (void)
382{
383  tree t = stack_chk_guard_decl;
384
385  if (t == NULL)
386    {
387      t = build_decl (VAR_DECL, get_identifier ("__stack_chk_guard"),
388		      ptr_type_node);
389      TREE_STATIC (t) = 1;
390      TREE_PUBLIC (t) = 1;
391      DECL_EXTERNAL (t) = 1;
392      TREE_USED (t) = 1;
393      TREE_THIS_VOLATILE (t) = 1;
394      DECL_ARTIFICIAL (t) = 1;
395      DECL_IGNORED_P (t) = 1;
396
397      stack_chk_guard_decl = t;
398    }
399
400  return t;
401}
402
403static GTY(()) tree stack_chk_fail_decl;
404
405tree
406default_external_stack_protect_fail (void)
407{
408  tree t = stack_chk_fail_decl;
409
410  if (t == NULL_TREE)
411    {
412      t = build_function_type_list (void_type_node, NULL_TREE);
413      t = build_decl (FUNCTION_DECL, get_identifier ("__stack_chk_fail"), t);
414      TREE_STATIC (t) = 1;
415      TREE_PUBLIC (t) = 1;
416      DECL_EXTERNAL (t) = 1;
417      TREE_USED (t) = 1;
418      TREE_THIS_VOLATILE (t) = 1;
419      TREE_NOTHROW (t) = 1;
420      DECL_ARTIFICIAL (t) = 1;
421      DECL_IGNORED_P (t) = 1;
422      DECL_VISIBILITY (t) = VISIBILITY_DEFAULT;
423      DECL_VISIBILITY_SPECIFIED (t) = 1;
424
425      stack_chk_fail_decl = t;
426    }
427
428  return build_function_call_expr (t, NULL_TREE);
429}
430
431tree
432default_hidden_stack_protect_fail (void)
433{
434#ifndef HAVE_GAS_HIDDEN
435  return default_external_stack_protect_fail ();
436#else
437  tree t = stack_chk_fail_decl;
438
439  if (!flag_pic)
440    return default_external_stack_protect_fail ();
441
442  if (t == NULL_TREE)
443    {
444      t = build_function_type_list (void_type_node, NULL_TREE);
445      t = build_decl (FUNCTION_DECL,
446		      get_identifier ("__stack_chk_fail_local"), t);
447      TREE_STATIC (t) = 1;
448      TREE_PUBLIC (t) = 1;
449      DECL_EXTERNAL (t) = 1;
450      TREE_USED (t) = 1;
451      TREE_THIS_VOLATILE (t) = 1;
452      TREE_NOTHROW (t) = 1;
453      DECL_ARTIFICIAL (t) = 1;
454      DECL_IGNORED_P (t) = 1;
455      DECL_VISIBILITY_SPECIFIED (t) = 1;
456      DECL_VISIBILITY (t) = VISIBILITY_HIDDEN;
457
458      stack_chk_fail_decl = t;
459    }
460
461  return build_function_call_expr (t, NULL_TREE);
462#endif
463}
464
465bool
466hook_bool_rtx_commutative_p (rtx x, int outer_code ATTRIBUTE_UNUSED)
467{
468  return COMMUTATIVE_P (x);
469}
470
471rtx
472default_function_value (tree ret_type ATTRIBUTE_UNUSED,
473			tree fn_decl_or_type,
474			bool outgoing ATTRIBUTE_UNUSED)
475{
476  /* The old interface doesn't handle receiving the function type.  */
477  if (fn_decl_or_type
478      && !DECL_P (fn_decl_or_type))
479    fn_decl_or_type = NULL;
480
481#ifdef FUNCTION_OUTGOING_VALUE
482  if (outgoing)
483    return FUNCTION_OUTGOING_VALUE (ret_type, fn_decl_or_type);
484#endif
485
486#ifdef FUNCTION_VALUE
487  return FUNCTION_VALUE (ret_type, fn_decl_or_type);
488#else
489  return NULL_RTX;
490#endif
491}
492
493rtx
494default_internal_arg_pointer (void)
495{
496  /* If the reg that the virtual arg pointer will be translated into is
497     not a fixed reg or is the stack pointer, make a copy of the virtual
498     arg pointer, and address parms via the copy.  The frame pointer is
499     considered fixed even though it is not marked as such.  */
500  if ((ARG_POINTER_REGNUM == STACK_POINTER_REGNUM
501       || ! (fixed_regs[ARG_POINTER_REGNUM]
502	     || ARG_POINTER_REGNUM == FRAME_POINTER_REGNUM)))
503    return copy_to_reg (virtual_incoming_args_rtx);
504  else
505    return virtual_incoming_args_rtx;
506}
507
508enum reg_class
509default_secondary_reload (bool in_p ATTRIBUTE_UNUSED, rtx x ATTRIBUTE_UNUSED,
510			  enum reg_class reload_class ATTRIBUTE_UNUSED,
511			  enum machine_mode reload_mode ATTRIBUTE_UNUSED,
512			  secondary_reload_info *sri)
513{
514  enum reg_class class = NO_REGS;
515
516  if (sri->prev_sri && sri->prev_sri->t_icode != CODE_FOR_nothing)
517    {
518      sri->icode = sri->prev_sri->t_icode;
519      return NO_REGS;
520    }
521#ifdef SECONDARY_INPUT_RELOAD_CLASS
522  if (in_p)
523    class = SECONDARY_INPUT_RELOAD_CLASS (reload_class, reload_mode, x);
524#endif
525#ifdef SECONDARY_OUTPUT_RELOAD_CLASS
526  if (! in_p)
527    class = SECONDARY_OUTPUT_RELOAD_CLASS (reload_class, reload_mode, x);
528#endif
529  if (class != NO_REGS)
530    {
531      enum insn_code icode = (in_p ? reload_in_optab[(int) reload_mode]
532			      : reload_out_optab[(int) reload_mode]);
533
534      if (icode != CODE_FOR_nothing
535	  && insn_data[(int) icode].operand[in_p].predicate
536	  && ! insn_data[(int) icode].operand[in_p].predicate (x, reload_mode))
537	icode = CODE_FOR_nothing;
538      else if (icode != CODE_FOR_nothing)
539	{
540	  const char *insn_constraint, *scratch_constraint;
541	  char insn_letter, scratch_letter;
542	  enum reg_class insn_class, scratch_class;
543
544	  gcc_assert (insn_data[(int) icode].n_operands == 3);
545	  insn_constraint = insn_data[(int) icode].operand[!in_p].constraint;
546	  if (!*insn_constraint)
547	    insn_class = ALL_REGS;
548	  else
549	    {
550	      if (in_p)
551		{
552		  gcc_assert (*insn_constraint == '=');
553		  insn_constraint++;
554		}
555	      insn_letter = *insn_constraint;
556	      insn_class
557		= (insn_letter == 'r' ? GENERAL_REGS
558		   : REG_CLASS_FROM_CONSTRAINT ((unsigned char) insn_letter,
559						insn_constraint));
560	      gcc_assert (insn_class != NO_REGS);
561	    }
562
563	  scratch_constraint = insn_data[(int) icode].operand[2].constraint;
564	  /* The scratch register's constraint must start with "=&",
565	     except for an input reload, where only "=" is necessary,
566	     and where it might be beneficial to re-use registers from
567	     the input.  */
568	  gcc_assert (scratch_constraint[0] == '='
569		      && (in_p || scratch_constraint[1] == '&'));
570	  scratch_constraint++;
571	  if (*scratch_constraint == '&')
572	    scratch_constraint++;
573	  scratch_letter = *scratch_constraint;
574	  scratch_class
575	    = (scratch_letter == 'r' ? GENERAL_REGS
576	       : REG_CLASS_FROM_CONSTRAINT ((unsigned char) scratch_letter,
577					    scratch_constraint));
578
579	  if (reg_class_subset_p (reload_class, insn_class))
580	    {
581	      gcc_assert (scratch_class == class);
582	      class = NO_REGS;
583	    }
584	  else
585	    class = insn_class;
586
587        }
588      if (class == NO_REGS)
589	sri->icode = icode;
590      else
591	sri->t_icode = icode;
592    }
593  return class;
594}
595
596
597/* If STRICT_ALIGNMENT is true we use the container type for accessing
598   volatile bitfields.  This is generally the preferred behavior for memory
599   mapped peripherals on RISC architectures.
600   If STRICT_ALIGNMENT is false we use the narrowest type possible.  This
601   is typically used to avoid spurious page faults and extra memory accesses
602   due to unaligned accesses on CISC architectures.  */
603
604bool
605default_narrow_bitfield (void)
606{
607  return !STRICT_ALIGNMENT;
608}
609
610/* By default, if flag_pic is true, then neither local nor global relocs
611   should be placed in readonly memory.  */
612
613int
614default_reloc_rw_mask (void)
615{
616  return flag_pic ? 3 : 0;
617}
618
619bool
620default_builtin_vector_alignment_reachable (tree type, bool is_packed)
621{
622  if (is_packed)
623    return false;
624
625  /* Assuming that types whose size is > pointer-size are not guaranteed to be
626     naturally aligned.  */
627  if (tree_int_cst_compare (TYPE_SIZE (type), bitsize_int (POINTER_SIZE)) > 0)
628    return false;
629
630  /* Assuming that types whose size is <= pointer-size
631     are naturally aligned.  */
632  return true;
633}
634
635#include "gt-targhooks.h"
636