1 /* Target code for NVPTX.
2 Copyright (C) 2014-2015 Free Software Foundation, Inc.
3 Contributed by Bernd Schmidt <bernds@codesourcery.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published
9 by the Free Software Foundation; either version 3, or (at your
10 option) any later version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
31 #include "insn-flags.h"
33 #include "insn-attr.h"
34 #include "insn-codes.h"
35 #include "hard-reg-set.h"
38 #include "insn-config.h"
53 #include "tm-constrs.h"
54 #include "langhooks.h"
57 #include "target-def.h"
58 #include "diagnostic.h"
60 #include "basic-block.h"
62 #include "stor-layout.h"
66 /* Record the function decls we've written, and the libfuncs and function
67 decls corresponding to them. */
68 static std::stringstream func_decls
;
70 struct declared_libfunc_hasher
: ggc_cache_hasher
<rtx
>
72 static hashval_t
hash (rtx x
) { return htab_hash_pointer (x
); }
73 static bool equal (rtx a
, rtx b
) { return a
== b
; }
77 hash_table
<declared_libfunc_hasher
> *declared_libfuncs_htab
;
79 struct tree_hasher
: ggc_cache_hasher
<tree
>
81 static hashval_t
hash (tree t
) { return htab_hash_pointer (t
); }
82 static bool equal (tree a
, tree b
) { return a
== b
; }
85 static GTY((cache
)) hash_table
<tree_hasher
> *declared_fndecls_htab
;
86 static GTY((cache
)) hash_table
<tree_hasher
> *needed_fndecls_htab
;
88 /* Allocate a new, cleared machine_function structure. */
90 static struct machine_function
*
91 nvptx_init_machine_status (void)
93 struct machine_function
*p
= ggc_cleared_alloc
<machine_function
> ();
94 p
->ret_reg_mode
= VOIDmode
;
98 /* Implement TARGET_OPTION_OVERRIDE. */
101 nvptx_option_override (void)
103 init_machine_status
= nvptx_init_machine_status
;
104 /* Gives us a predictable order, which we need especially for variables. */
105 flag_toplevel_reorder
= 1;
106 /* Assumes that it will see only hard registers. */
107 flag_var_tracking
= 0;
108 write_symbols
= NO_DEBUG
;
109 debug_info_level
= DINFO_LEVEL_NONE
;
111 declared_fndecls_htab
= hash_table
<tree_hasher
>::create_ggc (17);
112 needed_fndecls_htab
= hash_table
<tree_hasher
>::create_ggc (17);
113 declared_libfuncs_htab
114 = hash_table
<declared_libfunc_hasher
>::create_ggc (17);
117 /* Return the mode to be used when declaring a ptx object for OBJ.
118 For objects with subparts such as complex modes this is the mode
122 nvptx_underlying_object_mode (rtx obj
)
124 if (GET_CODE (obj
) == SUBREG
)
125 obj
= SUBREG_REG (obj
);
126 machine_mode mode
= GET_MODE (obj
);
129 if (COMPLEX_MODE_P (mode
))
130 return GET_MODE_INNER (mode
);
134 /* Return a ptx type for MODE. If PROMOTE, then use .u32 for QImode to
135 deal with ptx ideosyncracies. */
138 nvptx_ptx_type_from_mode (machine_mode mode
, bool promote
)
168 /* Return the number of pieces to use when dealing with a pseudo of *PMODE.
169 Alter *PMODE if we return a number greater than one. */
172 maybe_split_mode (machine_mode
*pmode
)
174 machine_mode mode
= *pmode
;
176 if (COMPLEX_MODE_P (mode
))
178 *pmode
= GET_MODE_INNER (mode
);
181 else if (mode
== TImode
)
189 /* Like maybe_split_mode, but only return whether or not the mode
190 needs to be split. */
192 nvptx_split_reg_p (machine_mode mode
)
194 if (COMPLEX_MODE_P (mode
))
201 #define PASS_IN_REG_P(MODE, TYPE) \
202 ((GET_MODE_CLASS (MODE) == MODE_INT \
203 || GET_MODE_CLASS (MODE) == MODE_FLOAT \
204 || ((GET_MODE_CLASS (MODE) == MODE_COMPLEX_INT \
205 || GET_MODE_CLASS (MODE) == MODE_COMPLEX_FLOAT) \
206 && !AGGREGATE_TYPE_P (TYPE))) \
209 #define RETURN_IN_REG_P(MODE) \
210 ((GET_MODE_CLASS (MODE) == MODE_INT \
211 || GET_MODE_CLASS (MODE) == MODE_FLOAT) \
212 && GET_MODE_SIZE (MODE) <= 8)
214 /* Perform a mode promotion for a function argument with MODE. Return
215 the promoted mode. */
218 arg_promotion (machine_mode mode
)
220 if (mode
== QImode
|| mode
== HImode
)
225 /* Write the declaration of a function arg of TYPE to S. I is the index
226 of the argument, MODE its mode. NO_ARG_TYPES is true if this is for
227 a decl with zero TYPE_ARG_TYPES, i.e. an old-style C decl. */
230 write_one_arg (std::stringstream
&s
, tree type
, int i
, machine_mode mode
,
233 if (!PASS_IN_REG_P (mode
, type
))
236 int count
= maybe_split_mode (&mode
);
240 write_one_arg (s
, NULL_TREE
, i
, mode
, false);
241 write_one_arg (s
, NULL_TREE
, i
+ 1, mode
, false);
245 if (no_arg_types
&& !AGGREGATE_TYPE_P (type
))
249 mode
= arg_promotion (mode
);
254 s
<< ".param" << nvptx_ptx_type_from_mode (mode
, false) << " %in_ar"
255 << (i
+ 1) << (mode
== QImode
|| mode
== HImode
? "[1]" : "");
257 s
<< "[" << int_size_in_bytes (type
) << "]";
261 /* Look for attributes in ATTRS that would indicate we must write a function
262 as a .entry kernel rather than a .func. Return true if one is found. */
265 write_as_kernel (tree attrs
)
267 return (lookup_attribute ("kernel", attrs
) != NULL_TREE
268 || lookup_attribute ("omp target entrypoint", attrs
) != NULL_TREE
);
271 /* Write a function decl for DECL to S, where NAME is the name to be used. */
274 nvptx_write_function_decl (std::stringstream
&s
, const char *name
, const_tree decl
)
276 tree fntype
= TREE_TYPE (decl
);
277 tree result_type
= TREE_TYPE (fntype
);
278 tree args
= TYPE_ARG_TYPES (fntype
);
279 tree attrs
= DECL_ATTRIBUTES (decl
);
280 bool kernel
= write_as_kernel (attrs
);
281 bool is_main
= strcmp (name
, "main") == 0;
282 bool args_from_decl
= false;
285 NULL in TYPE_ARG_TYPES, for old-style functions
286 NULL in DECL_ARGUMENTS, for builtin functions without another
288 So we have to pick the best one we have. */
291 args
= DECL_ARGUMENTS (decl
);
292 args_from_decl
= true;
295 if (DECL_EXTERNAL (decl
))
297 else if (TREE_PUBLIC (decl
))
305 /* Declare the result. */
306 bool return_in_mem
= false;
307 if (TYPE_MODE (result_type
) != VOIDmode
)
309 machine_mode mode
= TYPE_MODE (result_type
);
310 if (!RETURN_IN_REG_P (mode
))
311 return_in_mem
= true;
314 mode
= arg_promotion (mode
);
315 s
<< "(.param" << nvptx_ptx_type_from_mode (mode
, false)
325 /* Declare argument types. */
326 if ((args
!= NULL_TREE
327 && !(TREE_CODE (args
) == TREE_LIST
&& TREE_VALUE (args
) == void_type_node
))
330 || DECL_STATIC_CHAIN (decl
))
334 bool any_args
= false;
337 s
<< ".param.u" << GET_MODE_BITSIZE (Pmode
) << " %in_ar1";
340 while (args
!= NULL_TREE
)
342 tree type
= args_from_decl
? TREE_TYPE (args
) : TREE_VALUE (args
);
343 machine_mode mode
= TYPE_MODE (type
);
345 if (mode
!= VOIDmode
)
347 i
= write_one_arg (s
, type
, i
, mode
,
348 TYPE_ARG_TYPES (fntype
) == 0);
352 args
= TREE_CHAIN (args
);
354 if (stdarg_p (fntype
))
357 s
<< ", .param.u" << GET_MODE_BITSIZE (Pmode
) << " %in_argp";
359 if (DECL_STATIC_CHAIN (decl
))
363 s
<< ".reg.u" << GET_MODE_BITSIZE (Pmode
)
364 << reg_names
[STATIC_CHAIN_REGNUM
];
366 if (!any_args
&& is_main
)
367 s
<< ".param.u32 %argc, .param.u" << GET_MODE_BITSIZE (Pmode
)
373 /* Walk either ARGTYPES or ARGS if the former is null, and write out part of
374 the function header to FILE. If WRITE_COPY is false, write reg
375 declarations, otherwise write the copy from the incoming argument to that
376 reg. RETURN_IN_MEM indicates whether to start counting arg numbers at 1
380 walk_args_for_param (FILE *file
, tree argtypes
, tree args
, bool write_copy
,
385 bool args_from_decl
= false;
387 args_from_decl
= true;
391 for (i
= return_in_mem
? 1 : 0; args
!= NULL_TREE
; args
= TREE_CHAIN (args
))
393 tree type
= args_from_decl
? TREE_TYPE (args
) : TREE_VALUE (args
);
394 machine_mode mode
= TYPE_MODE (type
);
396 if (mode
== VOIDmode
)
399 if (!PASS_IN_REG_P (mode
, type
))
402 int count
= maybe_split_mode (&mode
);
405 if (argtypes
== NULL
&& !AGGREGATE_TYPE_P (type
))
411 mode
= arg_promotion (mode
);
417 fprintf (file
, "\tld.param%s %%ar%d, [%%in_ar%d];\n",
418 nvptx_ptx_type_from_mode (mode
, false), i
, i
);
420 fprintf (file
, "\t.reg%s %%ar%d;\n",
421 nvptx_ptx_type_from_mode (mode
, false), i
);
426 /* Write a .func or .kernel declaration (not a definition) along with
427 a helper comment for use by ld. S is the stream to write to, DECL
428 the decl for the function with name NAME. */
431 write_function_decl_and_comment (std::stringstream
&s
, const char *name
, const_tree decl
)
434 if (TREE_PUBLIC (decl
))
436 s
<< " FUNCTION DECL: ";
442 nvptx_write_function_decl (s
, name
, decl
);
446 /* Check NAME for special function names and redirect them by returning a
447 replacement. This applies to malloc, free and realloc, for which we
448 want to use libgcc wrappers, and call, which triggers a bug in ptxas. */
451 nvptx_name_replacement (const char *name
)
453 if (strcmp (name
, "call") == 0)
454 return "__nvptx_call";
455 if (strcmp (name
, "malloc") == 0)
456 return "__nvptx_malloc";
457 if (strcmp (name
, "free") == 0)
458 return "__nvptx_free";
459 if (strcmp (name
, "realloc") == 0)
460 return "__nvptx_realloc";
464 /* If DECL is a FUNCTION_DECL, check the hash table to see if we
465 already encountered it, and if not, insert it and write a ptx
466 declarations that will be output at the end of compilation. */
469 nvptx_record_fndecl (tree decl
, bool force
= false)
471 if (decl
== NULL_TREE
|| TREE_CODE (decl
) != FUNCTION_DECL
472 || !DECL_EXTERNAL (decl
))
475 if (!force
&& TYPE_ARG_TYPES (TREE_TYPE (decl
)) == NULL_TREE
)
478 tree
*slot
= declared_fndecls_htab
->find_slot (decl
, INSERT
);
482 const char *name
= get_fnname_from_decl (decl
);
483 name
= nvptx_name_replacement (name
);
484 write_function_decl_and_comment (func_decls
, name
, decl
);
489 /* Record that we need to emit a ptx decl for DECL. Either do it now, or
490 record it for later in case we have no argument information at this
494 nvptx_record_needed_fndecl (tree decl
)
496 if (nvptx_record_fndecl (decl
))
499 tree
*slot
= needed_fndecls_htab
->find_slot (decl
, INSERT
);
504 /* Implement ASM_DECLARE_FUNCTION_NAME. Writes the start of a ptx
505 function, including local var decls and copies from the arguments to
509 nvptx_declare_function_name (FILE *file
, const char *name
, const_tree decl
)
511 tree fntype
= TREE_TYPE (decl
);
512 tree result_type
= TREE_TYPE (fntype
);
514 name
= nvptx_name_replacement (name
);
517 write_function_decl_and_comment (s
, name
, decl
);
519 if (TREE_PUBLIC (decl
))
521 s
<< " FUNCTION DEF: ";
529 nvptx_write_function_decl (s
, name
, decl
);
530 fprintf (file
, "%s", s
.str().c_str());
532 bool return_in_mem
= false;
533 if (TYPE_MODE (result_type
) != VOIDmode
)
535 machine_mode mode
= TYPE_MODE (result_type
);
536 if (!RETURN_IN_REG_P (mode
))
537 return_in_mem
= true;
540 fprintf (file
, "\n{\n");
542 /* Ensure all arguments that should live in a register have one
543 declared. We'll emit the copies below. */
544 walk_args_for_param (file
, TYPE_ARG_TYPES (fntype
), DECL_ARGUMENTS (decl
),
545 false, return_in_mem
);
547 fprintf (file
, "\t.reg.u%d %%ar1;\n", GET_MODE_BITSIZE (Pmode
));
548 else if (TYPE_MODE (result_type
) != VOIDmode
)
550 machine_mode mode
= arg_promotion (TYPE_MODE (result_type
));
551 fprintf (file
, ".reg%s %%retval;\n",
552 nvptx_ptx_type_from_mode (mode
, false));
555 if (stdarg_p (fntype
))
556 fprintf (file
, "\t.reg.u%d %%argp;\n", GET_MODE_BITSIZE (Pmode
));
558 fprintf (file
, "\t.reg.u%d %s;\n", GET_MODE_BITSIZE (Pmode
),
559 reg_names
[OUTGOING_STATIC_CHAIN_REGNUM
]);
561 /* Declare the pseudos we have as ptx registers. */
562 int maxregs
= max_reg_num ();
563 for (int i
= LAST_VIRTUAL_REGISTER
+ 1; i
< maxregs
; i
++)
565 if (regno_reg_rtx
[i
] != const0_rtx
)
567 machine_mode mode
= PSEUDO_REGNO_MODE (i
);
568 int count
= maybe_split_mode (&mode
);
572 fprintf (file
, "\t.reg%s %%r%d$%d;\n",
573 nvptx_ptx_type_from_mode (mode
, true),
577 fprintf (file
, "\t.reg%s %%r%d;\n",
578 nvptx_ptx_type_from_mode (mode
, true),
583 /* The only reason we might be using outgoing args is if we call a stdargs
584 function. Allocate the space for this. If we called varargs functions
585 without passing any variadic arguments, we'll see a reference to outargs
586 even with a zero outgoing_args_size. */
587 HOST_WIDE_INT sz
= crtl
->outgoing_args_size
;
590 if (cfun
->machine
->has_call_with_varargs
)
591 fprintf (file
, "\t.reg.u%d %%outargs;\n"
592 "\t.local.align 8 .b8 %%outargs_ar[" HOST_WIDE_INT_PRINT_DEC
"];\n",
594 if (cfun
->machine
->punning_buffer_size
> 0)
595 fprintf (file
, "\t.reg.u%d %%punbuffer;\n"
596 "\t.local.align 8 .b8 %%punbuffer_ar[%d];\n",
597 BITS_PER_WORD
, cfun
->machine
->punning_buffer_size
);
599 /* Declare a local variable for the frame. */
600 sz
= get_frame_size ();
601 if (sz
> 0 || cfun
->machine
->has_call_with_sc
)
603 fprintf (file
, "\t.reg.u%d %%frame;\n"
604 "\t.local.align 8 .b8 %%farray[" HOST_WIDE_INT_PRINT_DEC
"];\n",
605 BITS_PER_WORD
, sz
== 0 ? 1 : sz
);
606 fprintf (file
, "\tcvta.local.u%d %%frame, %%farray;\n",
610 if (cfun
->machine
->has_call_with_varargs
)
611 fprintf (file
, "\tcvta.local.u%d %%outargs, %%outargs_ar;\n",
613 if (cfun
->machine
->punning_buffer_size
> 0)
614 fprintf (file
, "\tcvta.local.u%d %%punbuffer, %%punbuffer_ar;\n",
617 /* Now emit any copies necessary for arguments. */
618 walk_args_for_param (file
, TYPE_ARG_TYPES (fntype
), DECL_ARGUMENTS (decl
),
619 true, return_in_mem
);
621 fprintf (file
, "ld.param.u%d %%ar1, [%%in_ar1];\n",
622 GET_MODE_BITSIZE (Pmode
));
623 if (stdarg_p (fntype
))
624 fprintf (file
, "ld.param.u%d %%argp, [%%in_argp];\n",
625 GET_MODE_BITSIZE (Pmode
));
628 /* Output a return instruction. Also copy the return value to its outgoing
632 nvptx_output_return (void)
634 tree fntype
= TREE_TYPE (current_function_decl
);
635 tree result_type
= TREE_TYPE (fntype
);
636 if (TYPE_MODE (result_type
) != VOIDmode
)
638 machine_mode mode
= TYPE_MODE (result_type
);
639 if (RETURN_IN_REG_P (mode
))
641 mode
= arg_promotion (mode
);
642 fprintf (asm_out_file
, "\tst.param%s\t[%%out_retval], %%retval;\n",
643 nvptx_ptx_type_from_mode (mode
, false));
650 /* Construct a function declaration from a call insn. This can be
651 necessary for two reasons - either we have an indirect call which
652 requires a .callprototype declaration, or we have a libcall
653 generated by emit_library_call for which no decl exists. */
656 write_func_decl_from_insn (std::stringstream
&s
, rtx result
, rtx pat
,
659 bool callprototype
= register_operand (callee
, Pmode
);
660 const char *name
= "_";
663 name
= XSTR (callee
, 0);
664 name
= nvptx_name_replacement (name
);
665 s
<< "// BEGIN GLOBAL FUNCTION DECL: " << name
<< "\n";
667 s
<< (callprototype
? "\t.callprototype\t" : "\t.extern .func ");
669 if (result
!= NULL_RTX
)
672 s
<< nvptx_ptx_type_from_mode (arg_promotion (GET_MODE (result
)),
684 int nargs
= XVECLEN (pat
, 0) - 1;
688 for (int i
= 0; i
< nargs
; i
++)
690 rtx t
= XEXP (XVECEXP (pat
, 0, i
+ 1), 0);
691 machine_mode mode
= GET_MODE (t
);
692 int count
= maybe_split_mode (&mode
);
697 s
<< nvptx_ptx_type_from_mode (mode
, false);
703 if (mode
== QImode
|| mode
== HImode
)
705 if (i
+ 1 < nargs
|| count
> 0)
714 /* Terminate a function by writing a closing brace to FILE. */
717 nvptx_function_end (FILE *file
)
719 fprintf (file
, "\t}\n");
722 /* Decide whether we can make a sibling call to a function. For ptx, we
726 nvptx_function_ok_for_sibcall (tree
, tree
)
731 /* Implement the TARGET_CALL_ARGS hook. Record information about one
732 argument to the next call. */
735 nvptx_call_args (rtx arg
, tree funtype
)
737 if (cfun
->machine
->start_call
== NULL_RTX
)
739 cfun
->machine
->call_args
= NULL
;
740 cfun
->machine
->funtype
= funtype
;
741 cfun
->machine
->start_call
= const0_rtx
;
746 rtx_expr_list
*args_so_far
= cfun
->machine
->call_args
;
748 cfun
->machine
->call_args
= alloc_EXPR_LIST (VOIDmode
, arg
, args_so_far
);
751 /* Implement the corresponding END_CALL_ARGS hook. Clear and free the
752 information we recorded. */
755 nvptx_end_call_args (void)
757 cfun
->machine
->start_call
= NULL_RTX
;
758 free_EXPR_LIST_list (&cfun
->machine
->call_args
);
761 /* Emit the sequence for a call. */
764 nvptx_expand_call (rtx retval
, rtx address
)
767 rtx callee
= XEXP (address
, 0);
770 bool external_decl
= false;
773 for (t
= cfun
->machine
->call_args
; t
; t
= XEXP (t
, 1))
776 bool has_varargs
= false;
777 tree decl_type
= NULL_TREE
;
779 if (!call_insn_operand (callee
, Pmode
))
781 callee
= force_reg (Pmode
, callee
);
782 address
= change_address (address
, QImode
, callee
);
785 if (GET_CODE (callee
) == SYMBOL_REF
)
787 tree decl
= SYMBOL_REF_DECL (callee
);
788 if (decl
!= NULL_TREE
)
790 decl_type
= TREE_TYPE (decl
);
791 if (DECL_STATIC_CHAIN (decl
))
792 cfun
->machine
->has_call_with_sc
= true;
793 if (DECL_EXTERNAL (decl
))
794 external_decl
= true;
797 if (cfun
->machine
->funtype
798 /* It's possible to construct testcases where we call a variable.
799 See compile/20020129-1.c. stdarg_p will crash so avoid calling it
801 && (TREE_CODE (cfun
->machine
->funtype
) == FUNCTION_TYPE
802 || TREE_CODE (cfun
->machine
->funtype
) == METHOD_TYPE
)
803 && stdarg_p (cfun
->machine
->funtype
))
806 cfun
->machine
->has_call_with_varargs
= true;
808 vec
= rtvec_alloc (nargs
+ 1 + (has_varargs
? 1 : 0));
809 pat
= gen_rtx_PARALLEL (VOIDmode
, vec
);
812 rtx this_arg
= gen_reg_rtx (Pmode
);
814 emit_move_insn (this_arg
, stack_pointer_rtx
);
816 emit_move_insn (this_arg
, stack_pointer_rtx
);
817 XVECEXP (pat
, 0, nargs
+ 1) = gen_rtx_USE (VOIDmode
, this_arg
);
822 for (i
= 1, arg
= cfun
->machine
->call_args
; arg
; arg
= XEXP (arg
, 1), i
++)
824 rtx this_arg
= XEXP (arg
, 0);
825 XVECEXP (pat
, 0, i
) = gen_rtx_USE (VOIDmode
, this_arg
);
828 rtx tmp_retval
= retval
;
829 t
= gen_rtx_CALL (VOIDmode
, address
, const0_rtx
);
830 if (retval
!= NULL_RTX
)
832 if (!nvptx_register_operand (retval
, GET_MODE (retval
)))
833 tmp_retval
= gen_reg_rtx (GET_MODE (retval
));
834 t
= gen_rtx_SET (tmp_retval
, t
);
836 XVECEXP (pat
, 0, 0) = t
;
838 && (decl_type
== NULL_TREE
839 || (external_decl
&& TYPE_ARG_TYPES (decl_type
) == NULL_TREE
)))
841 rtx
*slot
= declared_libfuncs_htab
->find_slot (callee
, INSERT
);
845 write_func_decl_from_insn (func_decls
, retval
, pat
, callee
);
848 emit_call_insn (pat
);
849 if (tmp_retval
!= retval
)
850 emit_move_insn (retval
, tmp_retval
);
853 /* Implement TARGET_FUNCTION_ARG. */
856 nvptx_function_arg (cumulative_args_t
, machine_mode mode
,
857 const_tree
, bool named
)
859 if (mode
== VOIDmode
)
863 return gen_reg_rtx (mode
);
867 /* Implement TARGET_FUNCTION_INCOMING_ARG. */
870 nvptx_function_incoming_arg (cumulative_args_t cum_v
, machine_mode mode
,
871 const_tree
, bool named
)
873 CUMULATIVE_ARGS
*cum
= get_cumulative_args (cum_v
);
874 if (mode
== VOIDmode
)
880 /* No need to deal with split modes here, the only case that can
881 happen is complex modes and those are dealt with by
882 TARGET_SPLIT_COMPLEX_ARG. */
883 return gen_rtx_UNSPEC (mode
,
884 gen_rtvec (1, GEN_INT (1 + cum
->count
)),
888 /* Implement TARGET_FUNCTION_ARG_ADVANCE. */
891 nvptx_function_arg_advance (cumulative_args_t cum_v
, machine_mode mode
,
892 const_tree type ATTRIBUTE_UNUSED
,
893 bool named ATTRIBUTE_UNUSED
)
895 CUMULATIVE_ARGS
*cum
= get_cumulative_args (cum_v
);
902 /* Handle the TARGET_STRICT_ARGUMENT_NAMING target hook.
904 For nvptx, we know how to handle functions declared as stdarg: by
905 passing an extra pointer to the unnamed arguments. However, the
906 Fortran frontend can produce a different situation, where a
907 function pointer is declared with no arguments, but the actual
908 function and calls to it take more arguments. In that case, we
909 want to ensure the call matches the definition of the function. */
912 nvptx_strict_argument_naming (cumulative_args_t cum_v
)
914 CUMULATIVE_ARGS
*cum
= get_cumulative_args (cum_v
);
915 return cum
->fntype
== NULL_TREE
|| stdarg_p (cum
->fntype
);
918 /* Implement TARGET_FUNCTION_ARG_BOUNDARY. */
921 nvptx_function_arg_boundary (machine_mode mode
, const_tree type
)
923 unsigned int boundary
= type
? TYPE_ALIGN (type
) : GET_MODE_BITSIZE (mode
);
925 if (boundary
> BITS_PER_WORD
)
926 return 2 * BITS_PER_WORD
;
930 HOST_WIDE_INT size
= int_size_in_bytes (type
);
932 return 2 * BITS_PER_WORD
;
933 if (boundary
< BITS_PER_WORD
)
936 return BITS_PER_WORD
;
938 return 2 * BITS_PER_UNIT
;
944 /* TARGET_FUNCTION_VALUE implementation. Returns an RTX representing the place
945 where function FUNC returns or receives a value of data type TYPE. */
948 nvptx_function_value (const_tree type
, const_tree func ATTRIBUTE_UNUSED
,
951 int unsignedp
= TYPE_UNSIGNED (type
);
952 machine_mode orig_mode
= TYPE_MODE (type
);
953 machine_mode mode
= promote_function_mode (type
, orig_mode
,
954 &unsignedp
, NULL_TREE
, 1);
956 return gen_rtx_REG (mode
, NVPTX_RETURN_REGNUM
);
957 if (cfun
->machine
->start_call
== NULL_RTX
)
958 /* Pretend to return in a hard reg for early uses before pseudos can be
960 return gen_rtx_REG (mode
, NVPTX_RETURN_REGNUM
);
961 return gen_reg_rtx (mode
);
964 /* Implement TARGET_LIBCALL_VALUE. */
967 nvptx_libcall_value (machine_mode mode
, const_rtx
)
969 if (cfun
->machine
->start_call
== NULL_RTX
)
970 /* Pretend to return in a hard reg for early uses before pseudos can be
972 return gen_rtx_REG (mode
, NVPTX_RETURN_REGNUM
);
973 return gen_reg_rtx (mode
);
976 /* Implement TARGET_FUNCTION_VALUE_REGNO_P. */
979 nvptx_function_value_regno_p (const unsigned int regno
)
981 return regno
== NVPTX_RETURN_REGNUM
;
984 /* Types with a mode other than those supported by the machine are passed by
985 reference in memory. */
988 nvptx_pass_by_reference (cumulative_args_t
, machine_mode mode
,
989 const_tree type
, bool)
991 return !PASS_IN_REG_P (mode
, type
);
994 /* Implement TARGET_RETURN_IN_MEMORY. */
997 nvptx_return_in_memory (const_tree type
, const_tree
)
999 machine_mode mode
= TYPE_MODE (type
);
1000 if (!RETURN_IN_REG_P (mode
))
1005 /* Implement TARGET_PROMOTE_FUNCTION_MODE. */
1008 nvptx_promote_function_mode (const_tree type
, machine_mode mode
,
1010 const_tree funtype
, int for_return
)
1012 if (type
== NULL_TREE
)
1015 return promote_mode (type
, mode
, punsignedp
);
1016 /* For K&R-style functions, try to match the language promotion rules to
1017 minimize type mismatches at assembly time. */
1018 if (TYPE_ARG_TYPES (funtype
) == NULL_TREE
1019 && type
!= NULL_TREE
1020 && !AGGREGATE_TYPE_P (type
))
1024 mode
= arg_promotion (mode
);
1030 /* Implement TARGET_STATIC_CHAIN. */
1033 nvptx_static_chain (const_tree fndecl
, bool incoming_p
)
1035 if (!DECL_STATIC_CHAIN (fndecl
))
1039 return gen_rtx_REG (Pmode
, STATIC_CHAIN_REGNUM
);
1041 return gen_rtx_REG (Pmode
, OUTGOING_STATIC_CHAIN_REGNUM
);
1044 /* Emit a comparison COMPARE, and return the new test to be used in the
1048 nvptx_expand_compare (rtx compare
)
1050 rtx pred
= gen_reg_rtx (BImode
);
1051 rtx cmp
= gen_rtx_fmt_ee (GET_CODE (compare
), BImode
,
1052 XEXP (compare
, 0), XEXP (compare
, 1));
1053 emit_insn (gen_rtx_SET (pred
, cmp
));
1054 return gen_rtx_NE (BImode
, pred
, const0_rtx
);
1057 /* When loading an operand ORIG_OP, verify whether an address space
1058 conversion to generic is required, and if so, perform it. Also
1059 check for SYMBOL_REFs for function decls and call
1060 nvptx_record_needed_fndecl as needed.
1061 Return either the original operand, or the converted one. */
1064 nvptx_maybe_convert_symbolic_operand (rtx orig_op
)
1066 if (GET_MODE (orig_op
) != Pmode
)
1070 while (GET_CODE (op
) == PLUS
|| GET_CODE (op
) == CONST
)
1072 if (GET_CODE (op
) != SYMBOL_REF
)
1075 tree decl
= SYMBOL_REF_DECL (op
);
1076 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
1078 nvptx_record_needed_fndecl (decl
);
1082 addr_space_t as
= nvptx_addr_space_from_address (op
);
1083 if (as
== ADDR_SPACE_GENERIC
)
1087 code
= (as
== ADDR_SPACE_GLOBAL
? UNSPEC_FROM_GLOBAL
1088 : as
== ADDR_SPACE_LOCAL
? UNSPEC_FROM_LOCAL
1089 : as
== ADDR_SPACE_SHARED
? UNSPEC_FROM_SHARED
1090 : as
== ADDR_SPACE_CONST
? UNSPEC_FROM_CONST
1091 : UNSPEC_FROM_PARAM
);
1092 rtx dest
= gen_reg_rtx (Pmode
);
1093 emit_insn (gen_rtx_SET (dest
, gen_rtx_UNSPEC (Pmode
, gen_rtvec (1, orig_op
),
1098 /* Returns true if X is a valid address for use in a memory reference. */
1101 nvptx_legitimate_address_p (machine_mode
, rtx x
, bool)
1103 enum rtx_code code
= GET_CODE (x
);
1111 if (REG_P (XEXP (x
, 0)) && CONST_INT_P (XEXP (x
, 1)))
1125 /* Implement HARD_REGNO_MODE_OK. We barely use hard regs, but we want
1126 to ensure that the return register's mode isn't changed. */
1129 nvptx_hard_regno_mode_ok (int regno
, machine_mode mode
)
1131 if (regno
!= NVPTX_RETURN_REGNUM
1132 || cfun
== NULL
|| cfun
->machine
->ret_reg_mode
== VOIDmode
)
1134 return mode
== cfun
->machine
->ret_reg_mode
;
1137 /* Convert an address space AS to the corresponding ptx string. */
1140 nvptx_section_from_addr_space (addr_space_t as
)
1144 case ADDR_SPACE_CONST
:
1147 case ADDR_SPACE_GLOBAL
:
1150 case ADDR_SPACE_SHARED
:
1153 case ADDR_SPACE_GENERIC
:
1161 /* Determine whether DECL goes into .const or .global. */
1164 nvptx_section_for_decl (const_tree decl
)
1166 bool is_const
= (CONSTANT_CLASS_P (decl
)
1167 || TREE_CODE (decl
) == CONST_DECL
1168 || TREE_READONLY (decl
));
1175 /* Look for a SYMBOL_REF in ADDR and return the address space to be used
1176 for the insn referencing this address. */
1179 nvptx_addr_space_from_address (rtx addr
)
1181 while (GET_CODE (addr
) == PLUS
|| GET_CODE (addr
) == CONST
)
1182 addr
= XEXP (addr
, 0);
1183 if (GET_CODE (addr
) != SYMBOL_REF
)
1184 return ADDR_SPACE_GENERIC
;
1186 tree decl
= SYMBOL_REF_DECL (addr
);
1187 if (decl
== NULL_TREE
|| TREE_CODE (decl
) == FUNCTION_DECL
)
1188 return ADDR_SPACE_GENERIC
;
1190 bool is_const
= (CONSTANT_CLASS_P (decl
)
1191 || TREE_CODE (decl
) == CONST_DECL
1192 || TREE_READONLY (decl
));
1194 return ADDR_SPACE_CONST
;
1196 return ADDR_SPACE_GLOBAL
;
1199 /* Machinery to output constant initializers. */
1201 /* Used when assembling integers to ensure data is emitted in
1202 pieces whose size matches the declaration we printed. */
1203 static unsigned int decl_chunk_size
;
1204 static machine_mode decl_chunk_mode
;
1205 /* Used in the same situation, to keep track of the byte offset
1206 into the initializer. */
1207 static unsigned HOST_WIDE_INT decl_offset
;
1208 /* The initializer part we are currently processing. */
1209 static HOST_WIDE_INT init_part
;
1210 /* The total size of the object. */
1211 static unsigned HOST_WIDE_INT object_size
;
1212 /* True if we found a skip extending to the end of the object. Used to
1213 assert that no data follows. */
1214 static bool object_finished
;
1216 /* Write the necessary separator string to begin a new initializer value. */
1219 begin_decl_field (void)
1221 /* We never see decl_offset at zero by the time we get here. */
1222 if (decl_offset
== decl_chunk_size
)
1223 fprintf (asm_out_file
, " = { ");
1225 fprintf (asm_out_file
, ", ");
1228 /* Output the currently stored chunk as an initializer value. */
1231 output_decl_chunk (void)
1233 begin_decl_field ();
1234 output_address (gen_int_mode (init_part
, decl_chunk_mode
));
1238 /* Add value VAL sized SIZE to the data we're emitting, and keep writing
1239 out chunks as they fill up. */
1242 nvptx_assemble_value (HOST_WIDE_INT val
, unsigned int size
)
1244 unsigned HOST_WIDE_INT chunk_offset
= decl_offset
% decl_chunk_size
;
1245 gcc_assert (!object_finished
);
1248 int this_part
= size
;
1249 if (chunk_offset
+ this_part
> decl_chunk_size
)
1250 this_part
= decl_chunk_size
- chunk_offset
;
1251 HOST_WIDE_INT val_part
;
1252 HOST_WIDE_INT mask
= 2;
1253 mask
<<= this_part
* BITS_PER_UNIT
- 1;
1254 val_part
= val
& (mask
- 1);
1255 init_part
|= val_part
<< (BITS_PER_UNIT
* chunk_offset
);
1256 val
>>= BITS_PER_UNIT
* this_part
;
1258 decl_offset
+= this_part
;
1259 if (decl_offset
% decl_chunk_size
== 0)
1260 output_decl_chunk ();
1266 /* Target hook for assembling integer object X of size SIZE. */
1269 nvptx_assemble_integer (rtx x
, unsigned int size
, int ARG_UNUSED (aligned_p
))
1271 if (GET_CODE (x
) == SYMBOL_REF
|| GET_CODE (x
) == CONST
)
1273 gcc_assert (size
= decl_chunk_size
);
1274 if (decl_offset
% decl_chunk_size
!= 0)
1275 sorry ("cannot emit unaligned pointers in ptx assembly");
1276 decl_offset
+= size
;
1277 begin_decl_field ();
1279 HOST_WIDE_INT off
= 0;
1280 if (GET_CODE (x
) == CONST
)
1282 if (GET_CODE (x
) == PLUS
)
1284 off
= INTVAL (XEXP (x
, 1));
1287 if (GET_CODE (x
) == SYMBOL_REF
)
1289 nvptx_record_needed_fndecl (SYMBOL_REF_DECL (x
));
1290 fprintf (asm_out_file
, "generic(");
1292 fprintf (asm_out_file
, ")");
1295 fprintf (asm_out_file
, " + " HOST_WIDE_INT_PRINT_DEC
, off
);
1300 switch (GET_CODE (x
))
1312 nvptx_assemble_value (val
, size
);
1316 /* Output SIZE zero bytes. We ignore the FILE argument since the
1317 functions we're calling to perform the output just use
1321 nvptx_output_skip (FILE *, unsigned HOST_WIDE_INT size
)
1323 if (decl_offset
+ size
>= object_size
)
1325 if (decl_offset
% decl_chunk_size
!= 0)
1326 nvptx_assemble_value (0, decl_chunk_size
);
1327 object_finished
= true;
1331 while (size
> decl_chunk_size
)
1333 nvptx_assemble_value (0, decl_chunk_size
);
1334 size
-= decl_chunk_size
;
1337 nvptx_assemble_value (0, 1);
1340 /* Output a string STR with length SIZE. As in nvptx_output_skip we
1341 ignore the FILE arg. */
1344 nvptx_output_ascii (FILE *, const char *str
, unsigned HOST_WIDE_INT size
)
1346 for (unsigned HOST_WIDE_INT i
= 0; i
< size
; i
++)
1347 nvptx_assemble_value (str
[i
], 1);
1350 /* Called when the initializer for a decl has been completely output through
1351 combinations of the three functions above. */
1354 nvptx_assemble_decl_end (void)
1356 if (decl_offset
!= 0)
1358 if (!object_finished
&& decl_offset
% decl_chunk_size
!= 0)
1359 nvptx_assemble_value (0, decl_chunk_size
);
1361 fprintf (asm_out_file
, " }");
1363 fprintf (asm_out_file
, ";\n");
1366 /* Start a declaration of a variable of TYPE with NAME to
1367 FILE. IS_PUBLIC says whether this will be externally visible.
1368 Here we just write the linker hint and decide on the chunk size
1372 init_output_initializer (FILE *file
, const char *name
, const_tree type
,
1375 fprintf (file
, "// BEGIN%s VAR DEF: ", is_public
? " GLOBAL" : "");
1376 assemble_name_raw (file
, name
);
1379 if (TREE_CODE (type
) == ARRAY_TYPE
)
1380 type
= TREE_TYPE (type
);
1381 int sz
= int_size_in_bytes (type
);
1382 if ((TREE_CODE (type
) != INTEGER_TYPE
1383 && TREE_CODE (type
) != ENUMERAL_TYPE
1384 && TREE_CODE (type
) != REAL_TYPE
)
1386 || sz
> HOST_BITS_PER_WIDE_INT
)
1387 type
= ptr_type_node
;
1388 decl_chunk_size
= int_size_in_bytes (type
);
1389 decl_chunk_mode
= int_mode_for_mode (TYPE_MODE (type
));
1392 object_finished
= false;
1395 /* Implement TARGET_ASM_DECLARE_CONSTANT_NAME. Begin the process of
1396 writing a constant variable EXP with NAME and SIZE and its
1397 initializer to FILE. */
1400 nvptx_asm_declare_constant_name (FILE *file
, const char *name
,
1401 const_tree exp
, HOST_WIDE_INT size
)
1403 tree type
= TREE_TYPE (exp
);
1404 init_output_initializer (file
, name
, type
, false);
1405 fprintf (file
, "\t.const .align %d .u%d ",
1406 TYPE_ALIGN (TREE_TYPE (exp
)) / BITS_PER_UNIT
,
1407 decl_chunk_size
* BITS_PER_UNIT
);
1408 assemble_name (file
, name
);
1409 fprintf (file
, "[" HOST_WIDE_INT_PRINT_DEC
"]",
1410 (size
+ decl_chunk_size
- 1) / decl_chunk_size
);
1414 /* Implement the ASM_DECLARE_OBJECT_NAME macro. Used to start writing
1415 a variable DECL with NAME to FILE. */
1418 nvptx_declare_object_name (FILE *file
, const char *name
, const_tree decl
)
1420 if (decl
&& DECL_SIZE (decl
))
1422 tree type
= TREE_TYPE (decl
);
1423 unsigned HOST_WIDE_INT size
;
1425 init_output_initializer (file
, name
, type
, TREE_PUBLIC (decl
));
1426 size
= tree_to_uhwi (DECL_SIZE_UNIT (decl
));
1427 const char *section
= nvptx_section_for_decl (decl
);
1428 fprintf (file
, "\t%s%s .align %d .u%d ",
1429 TREE_PUBLIC (decl
) ? " .visible" : "", section
,
1430 DECL_ALIGN (decl
) / BITS_PER_UNIT
,
1431 decl_chunk_size
* BITS_PER_UNIT
);
1432 assemble_name (file
, name
);
1434 fprintf (file
, "[" HOST_WIDE_INT_PRINT_DEC
"]",
1435 (size
+ decl_chunk_size
- 1) / decl_chunk_size
);
1437 object_finished
= true;
1442 /* Implement TARGET_ASM_GLOBALIZE_LABEL by doing nothing. */
1445 nvptx_globalize_label (FILE *, const char *)
1449 /* Implement TARGET_ASM_ASSEMBLE_UNDEFINED_DECL. Write an extern
1450 declaration only for variable DECL with NAME to FILE. */
1452 nvptx_assemble_undefined_decl (FILE *file
, const char *name
, const_tree decl
)
1454 if (TREE_CODE (decl
) != VAR_DECL
)
1456 const char *section
= nvptx_section_for_decl (decl
);
1457 fprintf (file
, "// BEGIN%s VAR DECL: ", TREE_PUBLIC (decl
) ? " GLOBAL" : "");
1458 assemble_name_raw (file
, name
);
1460 HOST_WIDE_INT size
= int_size_in_bytes (TREE_TYPE (decl
));
1461 fprintf (file
, ".extern %s .b8 ", section
);
1462 assemble_name_raw (file
, name
);
1464 fprintf (file
, "[" HOST_WIDE_INT_PRINT_DEC
"]", size
);
1465 fprintf (file
, ";\n\n");
1468 /* Output INSN, which is a call to CALLEE with result RESULT. For ptx, this
1469 involves writing .param declarations and in/out copies into them. */
1472 nvptx_output_call_insn (rtx_insn
*insn
, rtx result
, rtx callee
)
1476 bool needs_tgt
= register_operand (callee
, Pmode
);
1477 rtx pat
= PATTERN (insn
);
1478 int nargs
= XVECLEN (pat
, 0) - 1;
1479 tree decl
= NULL_TREE
;
1481 fprintf (asm_out_file
, "\t{\n");
1484 fprintf (asm_out_file
, "\t\t.param%s %%retval_in;\n",
1485 nvptx_ptx_type_from_mode (arg_promotion (GET_MODE (result
)),
1489 if (GET_CODE (callee
) == SYMBOL_REF
)
1491 decl
= SYMBOL_REF_DECL (callee
);
1492 if (decl
&& DECL_EXTERNAL (decl
))
1493 nvptx_record_fndecl (decl
);
1498 ASM_GENERATE_INTERNAL_LABEL (buf
, "LCT", labelno
);
1500 ASM_OUTPUT_LABEL (asm_out_file
, buf
);
1501 std::stringstream s
;
1502 write_func_decl_from_insn (s
, result
, pat
, callee
);
1503 fputs (s
.str().c_str(), asm_out_file
);
1506 for (int i
= 0, argno
= 0; i
< nargs
; i
++)
1508 rtx t
= XEXP (XVECEXP (pat
, 0, i
+ 1), 0);
1509 machine_mode mode
= GET_MODE (t
);
1510 int count
= maybe_split_mode (&mode
);
1513 fprintf (asm_out_file
, "\t\t.param%s %%out_arg%d%s;\n",
1514 nvptx_ptx_type_from_mode (mode
, false), argno
++,
1515 mode
== QImode
|| mode
== HImode
? "[1]" : "");
1517 for (int i
= 0, argno
= 0; i
< nargs
; i
++)
1519 rtx t
= XEXP (XVECEXP (pat
, 0, i
+ 1), 0);
1520 gcc_assert (REG_P (t
));
1521 machine_mode mode
= GET_MODE (t
);
1522 int count
= maybe_split_mode (&mode
);
1525 fprintf (asm_out_file
, "\t\tst.param%s [%%out_arg%d], %%r%d;\n",
1526 nvptx_ptx_type_from_mode (mode
, false), argno
++,
1532 fprintf (asm_out_file
, "\t\tst.param%s [%%out_arg%d], %%r%d$%d;\n",
1533 nvptx_ptx_type_from_mode (mode
, false), argno
++,
1538 fprintf (asm_out_file
, "\t\tcall ");
1539 if (result
!= NULL_RTX
)
1540 fprintf (asm_out_file
, "(%%retval_in), ");
1544 const char *name
= get_fnname_from_decl (decl
);
1545 name
= nvptx_name_replacement (name
);
1546 assemble_name (asm_out_file
, name
);
1549 output_address (callee
);
1551 if (nargs
> 0 || (decl
&& DECL_STATIC_CHAIN (decl
)))
1553 fprintf (asm_out_file
, ", (");
1555 for (i
= 0, argno
= 0; i
< nargs
; i
++)
1557 rtx t
= XEXP (XVECEXP (pat
, 0, i
+ 1), 0);
1558 machine_mode mode
= GET_MODE (t
);
1559 int count
= maybe_split_mode (&mode
);
1563 fprintf (asm_out_file
, "%%out_arg%d", argno
++);
1564 if (i
+ 1 < nargs
|| count
> 0)
1565 fprintf (asm_out_file
, ", ");
1568 if (decl
&& DECL_STATIC_CHAIN (decl
))
1571 fprintf (asm_out_file
, ", ");
1572 fprintf (asm_out_file
, "%s",
1573 reg_names
[OUTGOING_STATIC_CHAIN_REGNUM
]);
1576 fprintf (asm_out_file
, ")");
1580 fprintf (asm_out_file
, ", ");
1581 assemble_name (asm_out_file
, buf
);
1583 fprintf (asm_out_file
, ";\n");
1584 if (result
!= NULL_RTX
)
1585 return "ld.param%t0\t%0, [%%retval_in];\n\t}";
1590 /* Implement TARGET_PRINT_OPERAND_PUNCT_VALID_P. */
1593 nvptx_print_operand_punct_valid_p (unsigned char c
)
1595 return c
== '.' || c
== '#';
1598 static void nvptx_print_operand (FILE *, rtx
, int);
1600 /* Subroutine of nvptx_print_operand; used to print a memory reference X to FILE. */
1603 nvptx_print_address_operand (FILE *file
, rtx x
, machine_mode
)
1606 if (GET_CODE (x
) == CONST
)
1608 switch (GET_CODE (x
))
1612 output_address (XEXP (x
, 0));
1613 fprintf (file
, "+");
1614 output_address (off
);
1619 output_addr_const (file
, x
);
1623 gcc_assert (GET_CODE (x
) != MEM
);
1624 nvptx_print_operand (file
, x
, 0);
1629 /* Write assembly language output for the address ADDR to FILE. */
1632 nvptx_print_operand_address (FILE *file
, rtx addr
)
1634 nvptx_print_address_operand (file
, addr
, VOIDmode
);
1637 /* Print an operand, X, to FILE, with an optional modifier in CODE.
1640 . -- print the predicate for the instruction or an emptry string for an
1642 # -- print a rounding mode for the instruction
1644 A -- print an address space identifier for a MEM
1645 c -- print an opcode suffix for a comparison operator, including a type code
1646 d -- print a CONST_INT as a vector dimension (x, y, or z)
1647 f -- print a full reg even for something that must always be split
1648 t -- print a type opcode suffix, promoting QImode to 32 bits
1649 T -- print a type size in bits
1650 u -- print a type opcode suffix without promotions. */
1653 nvptx_print_operand (FILE *file
, rtx x
, int code
)
1656 machine_mode op_mode
;
1660 x
= current_insn_predicate
;
1663 unsigned int regno
= REGNO (XEXP (x
, 0));
1665 if (GET_CODE (x
) == EQ
)
1667 fputs (reg_names
[regno
], file
);
1672 else if (code
== '#')
1674 fputs (".rn", file
);
1678 enum rtx_code x_code
= GET_CODE (x
);
1684 addr_space_t as
= nvptx_addr_space_from_address (XEXP (x
, 0));
1685 fputs (nvptx_section_from_addr_space (as
), file
);
1690 gcc_assert (x_code
== CONST_INT
);
1691 if (INTVAL (x
) == 0)
1693 else if (INTVAL (x
) == 1)
1695 else if (INTVAL (x
) == 2)
1702 op_mode
= nvptx_underlying_object_mode (x
);
1703 fprintf (file
, "%s", nvptx_ptx_type_from_mode (op_mode
, true));
1707 op_mode
= nvptx_underlying_object_mode (x
);
1708 fprintf (file
, "%s", nvptx_ptx_type_from_mode (op_mode
, false));
1712 fprintf (file
, "%d", GET_MODE_BITSIZE (GET_MODE (x
)));
1716 fprintf (file
, "@");
1720 fprintf (file
, "@!");
1724 op_mode
= GET_MODE (XEXP (x
, 0));
1728 fputs (".eq", file
);
1731 if (FLOAT_MODE_P (op_mode
))
1732 fputs (".neu", file
);
1734 fputs (".ne", file
);
1737 fputs (".le", file
);
1740 fputs (".ge", file
);
1743 fputs (".lt", file
);
1746 fputs (".gt", file
);
1749 fputs (".ls", file
);
1752 fputs (".hs", file
);
1755 fputs (".lo", file
);
1758 fputs (".hi", file
);
1761 fputs (".ne", file
);
1764 fputs (".equ", file
);
1767 fputs (".leu", file
);
1770 fputs (".geu", file
);
1773 fputs (".ltu", file
);
1776 fputs (".gtu", file
);
1779 fputs (".nan", file
);
1782 fputs (".num", file
);
1787 if (FLOAT_MODE_P (op_mode
)
1788 || x_code
== EQ
|| x_code
== NE
1789 || x_code
== GEU
|| x_code
== GTU
1790 || x_code
== LEU
|| x_code
== LTU
)
1791 fputs (nvptx_ptx_type_from_mode (op_mode
, true), file
);
1793 fprintf (file
, ".s%d", GET_MODE_BITSIZE (op_mode
));
1804 if (HARD_REGISTER_P (x
))
1805 fprintf (file
, "%s", reg_names
[REGNO (x
)]);
1807 fprintf (file
, "%%r%d", REGNO (x
));
1808 if (code
!= 'f' && nvptx_split_reg_p (GET_MODE (x
)))
1810 gcc_assert (GET_CODE (orig_x
) == SUBREG
1811 && !nvptx_split_reg_p (GET_MODE (orig_x
)));
1812 fprintf (file
, "$%d", SUBREG_BYTE (orig_x
) / UNITS_PER_WORD
);
1818 nvptx_print_address_operand (file
, XEXP (x
, 0), GET_MODE (x
));
1823 output_addr_const (file
, x
);
1829 /* We could use output_addr_const, but that can print things like
1830 "x-8", which breaks ptxas. Need to ensure it is output as
1832 nvptx_print_address_operand (file
, x
, VOIDmode
);
1837 REAL_VALUE_TYPE real
;
1838 REAL_VALUE_FROM_CONST_DOUBLE (real
, x
);
1839 real_to_target (vals
, &real
, GET_MODE (x
));
1840 vals
[0] &= 0xffffffff;
1841 vals
[1] &= 0xffffffff;
1842 if (GET_MODE (x
) == SFmode
)
1843 fprintf (file
, "0f%08lx", vals
[0]);
1845 fprintf (file
, "0d%08lx%08lx", vals
[1], vals
[0]);
1849 output_addr_const (file
, x
);
1854 /* Record replacement regs used to deal with subreg operands. */
1857 rtx replacement
[MAX_RECOG_OPERANDS
];
1863 /* Allocate or reuse a replacement in R and return the rtx. */
1866 get_replacement (struct reg_replace
*r
)
1868 if (r
->n_allocated
== r
->n_in_use
)
1869 r
->replacement
[r
->n_allocated
++] = gen_reg_rtx (r
->mode
);
1870 return r
->replacement
[r
->n_in_use
++];
1873 /* Clean up subreg operands. In ptx assembly, everything is typed, and
1874 the presence of subregs would break the rules for most instructions.
1875 Replace them with a suitable new register of the right size, plus
1876 conversion copyin/copyout instructions. */
1881 struct reg_replace qiregs
, hiregs
, siregs
, diregs
;
1882 rtx_insn
*insn
, *next
;
1884 /* We are freeing block_for_insn in the toplev to keep compatibility
1885 with old MDEP_REORGS that are not CFG based. Recompute it now. */
1886 compute_bb_for_insn ();
1888 df_clear_flags (DF_LR_RUN_DCE
);
1891 thread_prologue_and_epilogue_insns ();
1893 qiregs
.n_allocated
= 0;
1894 hiregs
.n_allocated
= 0;
1895 siregs
.n_allocated
= 0;
1896 diregs
.n_allocated
= 0;
1897 qiregs
.mode
= QImode
;
1898 hiregs
.mode
= HImode
;
1899 siregs
.mode
= SImode
;
1900 diregs
.mode
= DImode
;
1902 for (insn
= get_insns (); insn
; insn
= next
)
1904 next
= NEXT_INSN (insn
);
1905 if (!NONDEBUG_INSN_P (insn
)
1906 || asm_noperands (insn
) >= 0
1907 || GET_CODE (PATTERN (insn
)) == USE
1908 || GET_CODE (PATTERN (insn
)) == CLOBBER
)
1910 qiregs
.n_in_use
= 0;
1911 hiregs
.n_in_use
= 0;
1912 siregs
.n_in_use
= 0;
1913 diregs
.n_in_use
= 0;
1914 extract_insn (insn
);
1915 enum attr_subregs_ok s_ok
= get_attr_subregs_ok (insn
);
1916 for (int i
= 0; i
< recog_data
.n_operands
; i
++)
1918 rtx op
= recog_data
.operand
[i
];
1919 if (GET_CODE (op
) != SUBREG
)
1922 rtx inner
= SUBREG_REG (op
);
1924 machine_mode outer_mode
= GET_MODE (op
);
1925 machine_mode inner_mode
= GET_MODE (inner
);
1928 && (GET_MODE_PRECISION (inner_mode
)
1929 >= GET_MODE_PRECISION (outer_mode
)))
1931 gcc_assert (SCALAR_INT_MODE_P (outer_mode
));
1932 struct reg_replace
*r
= (outer_mode
== QImode
? &qiregs
1933 : outer_mode
== HImode
? &hiregs
1934 : outer_mode
== SImode
? &siregs
1936 rtx new_reg
= get_replacement (r
);
1938 if (recog_data
.operand_type
[i
] != OP_OUT
)
1941 if (GET_MODE_PRECISION (inner_mode
)
1942 < GET_MODE_PRECISION (outer_mode
))
1947 rtx pat
= gen_rtx_SET (new_reg
,
1948 gen_rtx_fmt_e (code
, outer_mode
, inner
));
1949 emit_insn_before (pat
, insn
);
1952 if (recog_data
.operand_type
[i
] != OP_IN
)
1955 if (GET_MODE_PRECISION (inner_mode
)
1956 < GET_MODE_PRECISION (outer_mode
))
1961 rtx pat
= gen_rtx_SET (inner
,
1962 gen_rtx_fmt_e (code
, inner_mode
, new_reg
));
1963 emit_insn_after (pat
, insn
);
1965 validate_change (insn
, recog_data
.operand_loc
[i
], new_reg
, false);
1969 int maxregs
= max_reg_num ();
1970 regstat_init_n_sets_and_refs ();
1972 for (int i
= LAST_VIRTUAL_REGISTER
+ 1; i
< maxregs
; i
++)
1973 if (REG_N_SETS (i
) == 0 && REG_N_REFS (i
) == 0)
1974 regno_reg_rtx
[i
] = const0_rtx
;
1975 regstat_free_n_sets_and_refs ();
1978 /* Handle a "kernel" attribute; arguments as in
1979 struct attribute_spec.handler. */
1982 nvptx_handle_kernel_attribute (tree
*node
, tree name
, tree
ARG_UNUSED (args
),
1983 int ARG_UNUSED (flags
), bool *no_add_attrs
)
1987 if (TREE_CODE (decl
) != FUNCTION_DECL
)
1989 error ("%qE attribute only applies to functions", name
);
1990 *no_add_attrs
= true;
1993 else if (TREE_TYPE (TREE_TYPE (decl
)) != void_type_node
)
1995 error ("%qE attribute requires a void return type", name
);
1996 *no_add_attrs
= true;
2002 /* Table of valid machine attributes. */
2003 static const struct attribute_spec nvptx_attribute_table
[] =
2005 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
2006 affects_type_identity } */
2007 { "kernel", 0, 0, true, false, false, nvptx_handle_kernel_attribute
, false },
2008 { NULL
, 0, 0, false, false, false, NULL
, false }
2011 /* Limit vector alignments to BIGGEST_ALIGNMENT. */
2013 static HOST_WIDE_INT
2014 nvptx_vector_alignment (const_tree type
)
2016 HOST_WIDE_INT align
= tree_to_shwi (TYPE_SIZE (type
));
2018 return MIN (align
, BIGGEST_ALIGNMENT
);
2021 /* Record a symbol for mkoffload to enter into the mapping table. */
2024 nvptx_record_offload_symbol (tree decl
)
2026 fprintf (asm_out_file
, "//:%s_MAP %s\n",
2027 TREE_CODE (decl
) == VAR_DECL
? "VAR" : "FUNC",
2028 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
)));
2031 /* Implement TARGET_ASM_FILE_START. Write the kinds of things ptxas expects
2032 at the start of a file. */
2035 nvptx_file_start (void)
2037 fputs ("// BEGIN PREAMBLE\n", asm_out_file
);
2038 fputs ("\t.version\t3.1\n", asm_out_file
);
2039 fputs ("\t.target\tsm_30\n", asm_out_file
);
2040 fprintf (asm_out_file
, "\t.address_size %d\n", GET_MODE_BITSIZE (Pmode
));
2041 fputs ("// END PREAMBLE\n", asm_out_file
);
2044 /* Write out the function declarations we've collected. */
2047 nvptx_file_end (void)
2049 hash_table
<tree_hasher
>::iterator iter
;
2051 FOR_EACH_HASH_TABLE_ELEMENT (*needed_fndecls_htab
, decl
, tree
, iter
)
2052 nvptx_record_fndecl (decl
, true);
2053 fputs (func_decls
.str().c_str(), asm_out_file
);
2056 #undef TARGET_OPTION_OVERRIDE
2057 #define TARGET_OPTION_OVERRIDE nvptx_option_override
2059 #undef TARGET_ATTRIBUTE_TABLE
2060 #define TARGET_ATTRIBUTE_TABLE nvptx_attribute_table
2062 #undef TARGET_LEGITIMATE_ADDRESS_P
2063 #define TARGET_LEGITIMATE_ADDRESS_P nvptx_legitimate_address_p
2065 #undef TARGET_PROMOTE_FUNCTION_MODE
2066 #define TARGET_PROMOTE_FUNCTION_MODE nvptx_promote_function_mode
2068 #undef TARGET_FUNCTION_ARG
2069 #define TARGET_FUNCTION_ARG nvptx_function_arg
2070 #undef TARGET_FUNCTION_INCOMING_ARG
2071 #define TARGET_FUNCTION_INCOMING_ARG nvptx_function_incoming_arg
2072 #undef TARGET_FUNCTION_ARG_ADVANCE
2073 #define TARGET_FUNCTION_ARG_ADVANCE nvptx_function_arg_advance
2074 #undef TARGET_FUNCTION_ARG_BOUNDARY
2075 #define TARGET_FUNCTION_ARG_BOUNDARY nvptx_function_arg_boundary
2076 #undef TARGET_FUNCTION_ARG_ROUND_BOUNDARY
2077 #define TARGET_FUNCTION_ARG_ROUND_BOUNDARY nvptx_function_arg_boundary
2078 #undef TARGET_PASS_BY_REFERENCE
2079 #define TARGET_PASS_BY_REFERENCE nvptx_pass_by_reference
2080 #undef TARGET_FUNCTION_VALUE_REGNO_P
2081 #define TARGET_FUNCTION_VALUE_REGNO_P nvptx_function_value_regno_p
2082 #undef TARGET_FUNCTION_VALUE
2083 #define TARGET_FUNCTION_VALUE nvptx_function_value
2084 #undef TARGET_LIBCALL_VALUE
2085 #define TARGET_LIBCALL_VALUE nvptx_libcall_value
2086 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
2087 #define TARGET_FUNCTION_OK_FOR_SIBCALL nvptx_function_ok_for_sibcall
2088 #undef TARGET_SPLIT_COMPLEX_ARG
2089 #define TARGET_SPLIT_COMPLEX_ARG hook_bool_const_tree_true
2090 #undef TARGET_RETURN_IN_MEMORY
2091 #define TARGET_RETURN_IN_MEMORY nvptx_return_in_memory
2092 #undef TARGET_OMIT_STRUCT_RETURN_REG
2093 #define TARGET_OMIT_STRUCT_RETURN_REG true
2094 #undef TARGET_STRICT_ARGUMENT_NAMING
2095 #define TARGET_STRICT_ARGUMENT_NAMING nvptx_strict_argument_naming
2096 #undef TARGET_STATIC_CHAIN
2097 #define TARGET_STATIC_CHAIN nvptx_static_chain
2099 #undef TARGET_CALL_ARGS
2100 #define TARGET_CALL_ARGS nvptx_call_args
2101 #undef TARGET_END_CALL_ARGS
2102 #define TARGET_END_CALL_ARGS nvptx_end_call_args
2104 #undef TARGET_ASM_FILE_START
2105 #define TARGET_ASM_FILE_START nvptx_file_start
2106 #undef TARGET_ASM_FILE_END
2107 #define TARGET_ASM_FILE_END nvptx_file_end
2108 #undef TARGET_ASM_GLOBALIZE_LABEL
2109 #define TARGET_ASM_GLOBALIZE_LABEL nvptx_globalize_label
2110 #undef TARGET_ASM_ASSEMBLE_UNDEFINED_DECL
2111 #define TARGET_ASM_ASSEMBLE_UNDEFINED_DECL nvptx_assemble_undefined_decl
2112 #undef TARGET_PRINT_OPERAND
2113 #define TARGET_PRINT_OPERAND nvptx_print_operand
2114 #undef TARGET_PRINT_OPERAND_ADDRESS
2115 #define TARGET_PRINT_OPERAND_ADDRESS nvptx_print_operand_address
2116 #undef TARGET_PRINT_OPERAND_PUNCT_VALID_P
2117 #define TARGET_PRINT_OPERAND_PUNCT_VALID_P nvptx_print_operand_punct_valid_p
2118 #undef TARGET_ASM_INTEGER
2119 #define TARGET_ASM_INTEGER nvptx_assemble_integer
2120 #undef TARGET_ASM_DECL_END
2121 #define TARGET_ASM_DECL_END nvptx_assemble_decl_end
2122 #undef TARGET_ASM_DECLARE_CONSTANT_NAME
2123 #define TARGET_ASM_DECLARE_CONSTANT_NAME nvptx_asm_declare_constant_name
2124 #undef TARGET_USE_BLOCKS_FOR_CONSTANT_P
2125 #define TARGET_USE_BLOCKS_FOR_CONSTANT_P hook_bool_mode_const_rtx_true
2126 #undef TARGET_ASM_NEED_VAR_DECL_BEFORE_USE
2127 #define TARGET_ASM_NEED_VAR_DECL_BEFORE_USE true
2129 #undef TARGET_MACHINE_DEPENDENT_REORG
2130 #define TARGET_MACHINE_DEPENDENT_REORG nvptx_reorg
2131 #undef TARGET_NO_REGISTER_ALLOCATION
2132 #define TARGET_NO_REGISTER_ALLOCATION true
2134 #undef TARGET_RECORD_OFFLOAD_SYMBOL
2135 #define TARGET_RECORD_OFFLOAD_SYMBOL nvptx_record_offload_symbol
2137 #undef TARGET_VECTOR_ALIGNMENT
2138 #define TARGET_VECTOR_ALIGNMENT nvptx_vector_alignment
2140 struct gcc_target targetm
= TARGET_INITIALIZER
;
2142 #include "gt-nvptx.h"