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"
36 #include "insn-config.h"
51 #include "tm-constrs.h"
52 #include "langhooks.h"
55 #include "diagnostic.h"
57 #include "stor-layout.h"
60 #include "gomp-constants.h"
62 /* This file should be included last. */
63 #include "target-def.h"
65 /* Record the function decls we've written, and the libfuncs and function
66 decls corresponding to them. */
67 static std::stringstream func_decls
;
69 struct declared_libfunc_hasher
: ggc_cache_ptr_hash
<rtx_def
>
71 static hashval_t
hash (rtx x
) { return htab_hash_pointer (x
); }
72 static bool equal (rtx a
, rtx b
) { return a
== b
; }
76 hash_table
<declared_libfunc_hasher
> *declared_libfuncs_htab
;
78 struct tree_hasher
: ggc_cache_ptr_hash
<tree_node
>
80 static hashval_t
hash (tree t
) { return htab_hash_pointer (t
); }
81 static bool equal (tree a
, tree b
) { return a
== b
; }
84 static GTY((cache
)) hash_table
<tree_hasher
> *declared_fndecls_htab
;
85 static GTY((cache
)) hash_table
<tree_hasher
> *needed_fndecls_htab
;
87 /* Allocate a new, cleared machine_function structure. */
89 static struct machine_function
*
90 nvptx_init_machine_status (void)
92 struct machine_function
*p
= ggc_cleared_alloc
<machine_function
> ();
93 p
->ret_reg_mode
= VOIDmode
;
97 /* Implement TARGET_OPTION_OVERRIDE. */
100 nvptx_option_override (void)
102 init_machine_status
= nvptx_init_machine_status
;
103 /* Gives us a predictable order, which we need especially for variables. */
104 flag_toplevel_reorder
= 1;
105 /* Assumes that it will see only hard registers. */
106 flag_var_tracking
= 0;
107 write_symbols
= NO_DEBUG
;
108 debug_info_level
= DINFO_LEVEL_NONE
;
110 declared_fndecls_htab
= hash_table
<tree_hasher
>::create_ggc (17);
111 needed_fndecls_htab
= hash_table
<tree_hasher
>::create_ggc (17);
112 declared_libfuncs_htab
113 = hash_table
<declared_libfunc_hasher
>::create_ggc (17);
116 /* Return the mode to be used when declaring a ptx object for OBJ.
117 For objects with subparts such as complex modes this is the mode
121 nvptx_underlying_object_mode (rtx obj
)
123 if (GET_CODE (obj
) == SUBREG
)
124 obj
= SUBREG_REG (obj
);
125 machine_mode mode
= GET_MODE (obj
);
128 if (COMPLEX_MODE_P (mode
))
129 return GET_MODE_INNER (mode
);
133 /* Return a ptx type for MODE. If PROMOTE, then use .u32 for QImode to
134 deal with ptx ideosyncracies. */
137 nvptx_ptx_type_from_mode (machine_mode mode
, bool promote
)
167 /* Return the number of pieces to use when dealing with a pseudo of *PMODE.
168 Alter *PMODE if we return a number greater than one. */
171 maybe_split_mode (machine_mode
*pmode
)
173 machine_mode mode
= *pmode
;
175 if (COMPLEX_MODE_P (mode
))
177 *pmode
= GET_MODE_INNER (mode
);
180 else if (mode
== TImode
)
188 /* Like maybe_split_mode, but only return whether or not the mode
189 needs to be split. */
191 nvptx_split_reg_p (machine_mode mode
)
193 if (COMPLEX_MODE_P (mode
))
200 #define PASS_IN_REG_P(MODE, TYPE) \
201 ((GET_MODE_CLASS (MODE) == MODE_INT \
202 || GET_MODE_CLASS (MODE) == MODE_FLOAT \
203 || ((GET_MODE_CLASS (MODE) == MODE_COMPLEX_INT \
204 || GET_MODE_CLASS (MODE) == MODE_COMPLEX_FLOAT) \
205 && !AGGREGATE_TYPE_P (TYPE))) \
208 #define RETURN_IN_REG_P(MODE) \
209 ((GET_MODE_CLASS (MODE) == MODE_INT \
210 || GET_MODE_CLASS (MODE) == MODE_FLOAT) \
211 && GET_MODE_SIZE (MODE) <= 8)
213 /* Perform a mode promotion for a function argument with MODE. Return
214 the promoted mode. */
217 arg_promotion (machine_mode mode
)
219 if (mode
== QImode
|| mode
== HImode
)
224 /* Write the declaration of a function arg of TYPE to S. I is the index
225 of the argument, MODE its mode. NO_ARG_TYPES is true if this is for
226 a decl with zero TYPE_ARG_TYPES, i.e. an old-style C decl. */
229 write_one_arg (std::stringstream
&s
, tree type
, int i
, machine_mode mode
,
232 if (!PASS_IN_REG_P (mode
, type
))
235 int count
= maybe_split_mode (&mode
);
239 write_one_arg (s
, NULL_TREE
, i
, mode
, false);
240 write_one_arg (s
, NULL_TREE
, i
+ 1, mode
, false);
244 if (no_arg_types
&& !AGGREGATE_TYPE_P (type
))
248 mode
= arg_promotion (mode
);
253 s
<< ".param" << nvptx_ptx_type_from_mode (mode
, false) << " %in_ar"
254 << (i
+ 1) << (mode
== QImode
|| mode
== HImode
? "[1]" : "");
256 s
<< "[" << int_size_in_bytes (type
) << "]";
260 /* Look for attributes in ATTRS that would indicate we must write a function
261 as a .entry kernel rather than a .func. Return true if one is found. */
264 write_as_kernel (tree attrs
)
266 return (lookup_attribute ("kernel", attrs
) != NULL_TREE
267 || lookup_attribute ("omp target entrypoint", attrs
) != NULL_TREE
);
270 /* Write a function decl for DECL to S, where NAME is the name to be used.
271 This includes ptx .visible or .extern specifiers, .func or .kernel, and
272 argument and return types. */
275 nvptx_write_function_decl (std::stringstream
&s
, const char *name
, const_tree decl
)
277 tree fntype
= TREE_TYPE (decl
);
278 tree result_type
= TREE_TYPE (fntype
);
279 tree args
= TYPE_ARG_TYPES (fntype
);
280 tree attrs
= DECL_ATTRIBUTES (decl
);
281 bool kernel
= write_as_kernel (attrs
);
282 bool is_main
= strcmp (name
, "main") == 0;
283 bool args_from_decl
= false;
286 NULL in TYPE_ARG_TYPES, for old-style functions
287 NULL in DECL_ARGUMENTS, for builtin functions without another
289 So we have to pick the best one we have. */
292 args
= DECL_ARGUMENTS (decl
);
293 args_from_decl
= true;
296 if (DECL_EXTERNAL (decl
))
298 else if (TREE_PUBLIC (decl
))
306 /* Declare the result. */
307 bool return_in_mem
= false;
308 if (TYPE_MODE (result_type
) != VOIDmode
)
310 machine_mode mode
= TYPE_MODE (result_type
);
311 if (!RETURN_IN_REG_P (mode
))
312 return_in_mem
= true;
315 mode
= arg_promotion (mode
);
316 s
<< "(.param" << nvptx_ptx_type_from_mode (mode
, false)
326 /* Declare argument types. */
327 if ((args
!= NULL_TREE
328 && !(TREE_CODE (args
) == TREE_LIST
329 && TREE_VALUE (args
) == void_type_node
))
332 || DECL_STATIC_CHAIN (decl
))
336 bool any_args
= false;
339 s
<< ".param.u" << GET_MODE_BITSIZE (Pmode
) << " %in_ar1";
342 while (args
!= NULL_TREE
)
344 tree type
= args_from_decl
? TREE_TYPE (args
) : TREE_VALUE (args
);
345 machine_mode mode
= TYPE_MODE (type
);
347 if (mode
!= VOIDmode
)
349 i
= write_one_arg (s
, type
, i
, mode
,
350 TYPE_ARG_TYPES (fntype
) == 0);
354 args
= TREE_CHAIN (args
);
356 if (stdarg_p (fntype
))
359 s
<< ", .param.u" << GET_MODE_BITSIZE (Pmode
) << " %in_argp";
361 if (DECL_STATIC_CHAIN (decl
))
365 s
<< ".reg.u" << GET_MODE_BITSIZE (Pmode
)
366 << reg_names
[STATIC_CHAIN_REGNUM
];
368 if (!any_args
&& is_main
)
369 s
<< ".param.u32 %argc, .param.u" << GET_MODE_BITSIZE (Pmode
)
375 /* Walk either ARGTYPES or ARGS if the former is null, and write out part of
376 the function header to FILE. If WRITE_COPY is false, write reg
377 declarations, otherwise write the copy from the incoming argument to that
378 reg. RETURN_IN_MEM indicates whether to start counting arg numbers at 1
382 walk_args_for_param (FILE *file
, tree argtypes
, tree args
, bool write_copy
,
387 bool args_from_decl
= false;
389 args_from_decl
= true;
393 for (i
= return_in_mem
? 1 : 0; args
!= NULL_TREE
; args
= TREE_CHAIN (args
))
395 tree type
= args_from_decl
? TREE_TYPE (args
) : TREE_VALUE (args
);
396 machine_mode mode
= TYPE_MODE (type
);
398 if (mode
== VOIDmode
)
401 if (!PASS_IN_REG_P (mode
, type
))
404 int count
= maybe_split_mode (&mode
);
407 if (argtypes
== NULL
&& !AGGREGATE_TYPE_P (type
))
414 mode
= arg_promotion (mode
);
419 fprintf (file
, "\tld.param%s %%ar%d, [%%in_ar%d];\n",
420 nvptx_ptx_type_from_mode (mode
, false), i
, i
);
422 fprintf (file
, "\t.reg%s %%ar%d;\n",
423 nvptx_ptx_type_from_mode (mode
, false), i
);
428 /* Write a .func or .kernel declaration (not a definition) along with
429 a helper comment for use by ld. S is the stream to write to, DECL
430 the decl for the function with name NAME. */
433 write_function_decl_and_comment (std::stringstream
&s
, const char *name
, const_tree decl
)
436 if (TREE_PUBLIC (decl
))
438 s
<< " FUNCTION DECL: ";
444 nvptx_write_function_decl (s
, name
, decl
);
448 /* Check NAME for special function names and redirect them by returning a
449 replacement. This applies to malloc, free and realloc, for which we
450 want to use libgcc wrappers, and call, which triggers a bug in ptxas. */
453 nvptx_name_replacement (const char *name
)
455 if (strcmp (name
, "call") == 0)
456 return "__nvptx_call";
457 if (strcmp (name
, "malloc") == 0)
458 return "__nvptx_malloc";
459 if (strcmp (name
, "free") == 0)
460 return "__nvptx_free";
461 if (strcmp (name
, "realloc") == 0)
462 return "__nvptx_realloc";
466 /* If DECL is a FUNCTION_DECL, check the hash table to see if we
467 already encountered it, and if not, insert it and write a ptx
468 declarations that will be output at the end of compilation. */
471 nvptx_record_fndecl (tree decl
, bool force
= false)
473 if (decl
== NULL_TREE
|| TREE_CODE (decl
) != FUNCTION_DECL
474 || !DECL_EXTERNAL (decl
))
477 if (!force
&& TYPE_ARG_TYPES (TREE_TYPE (decl
)) == NULL_TREE
)
480 tree
*slot
= declared_fndecls_htab
->find_slot (decl
, INSERT
);
484 const char *name
= get_fnname_from_decl (decl
);
485 name
= nvptx_name_replacement (name
);
486 write_function_decl_and_comment (func_decls
, name
, decl
);
491 /* Record that we need to emit a ptx decl for DECL. Either do it now, or
492 record it for later in case we have no argument information at this
496 nvptx_record_needed_fndecl (tree decl
)
498 if (nvptx_record_fndecl (decl
))
501 tree
*slot
= needed_fndecls_htab
->find_slot (decl
, INSERT
);
506 /* Implement ASM_DECLARE_FUNCTION_NAME. Writes the start of a ptx
507 function, including local var decls and copies from the arguments to
511 nvptx_declare_function_name (FILE *file
, const char *name
, const_tree decl
)
513 tree fntype
= TREE_TYPE (decl
);
514 tree result_type
= TREE_TYPE (fntype
);
516 name
= nvptx_name_replacement (name
);
519 write_function_decl_and_comment (s
, name
, decl
);
521 if (TREE_PUBLIC (decl
))
523 s
<< " FUNCTION DEF: ";
531 nvptx_write_function_decl (s
, name
, decl
);
532 fprintf (file
, "%s", s
.str().c_str());
534 bool return_in_mem
= (TYPE_MODE (result_type
) != VOIDmode
535 && !RETURN_IN_REG_P (TYPE_MODE (result_type
)));
537 fprintf (file
, "\n{\n");
539 /* Ensure all arguments that should live in a register have one
540 declared. We'll emit the copies below. */
541 walk_args_for_param (file
, TYPE_ARG_TYPES (fntype
), DECL_ARGUMENTS (decl
),
542 false, return_in_mem
);
544 fprintf (file
, "\t.reg.u%d %%ar1;\n", GET_MODE_BITSIZE (Pmode
));
546 /* C++11 ABI causes us to return a reference to the passed in
547 pointer for return_in_mem. */
548 if (cfun
->machine
->ret_reg_mode
!= VOIDmode
)
550 machine_mode mode
= arg_promotion
551 ((machine_mode
)cfun
->machine
->ret_reg_mode
);
552 fprintf (file
, "\t.reg%s %%retval;\n",
553 nvptx_ptx_type_from_mode (mode
, false));
556 if (stdarg_p (fntype
))
557 fprintf (file
, "\t.reg.u%d %%argp;\n", GET_MODE_BITSIZE (Pmode
));
559 fprintf (file
, "\t.reg.u%d %s;\n", GET_MODE_BITSIZE (Pmode
),
560 reg_names
[OUTGOING_STATIC_CHAIN_REGNUM
]);
562 /* Declare the pseudos we have as ptx registers. */
563 int maxregs
= max_reg_num ();
564 for (int i
= LAST_VIRTUAL_REGISTER
+ 1; i
< maxregs
; i
++)
566 if (regno_reg_rtx
[i
] != const0_rtx
)
568 machine_mode mode
= PSEUDO_REGNO_MODE (i
);
569 int count
= maybe_split_mode (&mode
);
573 fprintf (file
, "\t.reg%s %%r%d$%d;\n",
574 nvptx_ptx_type_from_mode (mode
, true),
578 fprintf (file
, "\t.reg%s %%r%d;\n",
579 nvptx_ptx_type_from_mode (mode
, true),
584 /* The only reason we might be using outgoing args is if we call a stdargs
585 function. Allocate the space for this. If we called varargs functions
586 without passing any variadic arguments, we'll see a reference to outargs
587 even with a zero outgoing_args_size. */
588 HOST_WIDE_INT sz
= crtl
->outgoing_args_size
;
591 if (cfun
->machine
->has_call_with_varargs
)
592 fprintf (file
, "\t.reg.u%d %%outargs;\n"
593 "\t.local.align 8 .b8 %%outargs_ar[" HOST_WIDE_INT_PRINT_DEC
"];\n",
595 if (cfun
->machine
->punning_buffer_size
> 0)
596 fprintf (file
, "\t.reg.u%d %%punbuffer;\n"
597 "\t.local.align 8 .b8 %%punbuffer_ar[%d];\n",
598 BITS_PER_WORD
, cfun
->machine
->punning_buffer_size
);
600 /* Declare a local variable for the frame. */
601 sz
= get_frame_size ();
602 if (sz
> 0 || cfun
->machine
->has_call_with_sc
)
604 int alignment
= crtl
->stack_alignment_needed
/ BITS_PER_UNIT
;
606 fprintf (file
, "\t.reg.u%d %%frame;\n"
607 "\t.local.align %d .b8 %%farray[" HOST_WIDE_INT_PRINT_DEC
"];\n",
608 BITS_PER_WORD
, alignment
, sz
== 0 ? 1 : sz
);
609 fprintf (file
, "\tcvta.local.u%d %%frame, %%farray;\n",
613 if (cfun
->machine
->has_call_with_varargs
)
614 fprintf (file
, "\tcvta.local.u%d %%outargs, %%outargs_ar;\n",
616 if (cfun
->machine
->punning_buffer_size
> 0)
617 fprintf (file
, "\tcvta.local.u%d %%punbuffer, %%punbuffer_ar;\n",
620 /* Now emit any copies necessary for arguments. */
621 walk_args_for_param (file
, TYPE_ARG_TYPES (fntype
), DECL_ARGUMENTS (decl
),
622 true, return_in_mem
);
624 fprintf (file
, "\tld.param.u%d %%ar1, [%%in_ar1];\n",
625 GET_MODE_BITSIZE (Pmode
));
626 if (stdarg_p (fntype
))
627 fprintf (file
, "\tld.param.u%d %%argp, [%%in_argp];\n",
628 GET_MODE_BITSIZE (Pmode
));
631 /* Output a return instruction. Also copy the return value to its outgoing
635 nvptx_output_return (void)
637 machine_mode mode
= (machine_mode
)cfun
->machine
->ret_reg_mode
;
639 if (mode
!= VOIDmode
)
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));
649 /* Construct a function declaration from a call insn. This can be
650 necessary for two reasons - either we have an indirect call which
651 requires a .callprototype declaration, or we have a libcall
652 generated by emit_library_call for which no decl exists. */
655 write_func_decl_from_insn (std::stringstream
&s
, rtx result
, rtx pat
,
658 bool callprototype
= register_operand (callee
, Pmode
);
659 const char *name
= "_";
662 name
= XSTR (callee
, 0);
663 name
= nvptx_name_replacement (name
);
664 s
<< "// BEGIN GLOBAL FUNCTION DECL: " << name
<< "\n";
666 s
<< (callprototype
? "\t.callprototype\t" : "\t.extern .func ");
668 if (result
!= NULL_RTX
)
671 s
<< nvptx_ptx_type_from_mode (arg_promotion (GET_MODE (result
)),
683 int arg_end
= XVECLEN (pat
, 0);
687 const char *comma
= "";
689 for (int i
= 1; i
< arg_end
; i
++)
691 rtx t
= XEXP (XVECEXP (pat
, 0, i
), 0);
692 machine_mode mode
= GET_MODE (t
);
693 int count
= maybe_split_mode (&mode
);
697 s
<< comma
<< ".param";
698 s
<< nvptx_ptx_type_from_mode (mode
, false);
703 s
<< "%arg" << i
- 1;
704 if (mode
== QImode
|| mode
== HImode
)
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 /* Return Dynamic ReAlignment Pointer RTX. For PTX there isn't any. */
734 nvptx_get_drap_rtx (void)
739 /* Implement the TARGET_CALL_ARGS hook. Record information about one
740 argument to the next call. */
743 nvptx_call_args (rtx arg
, tree funtype
)
745 if (cfun
->machine
->start_call
== NULL_RTX
)
747 cfun
->machine
->call_args
= NULL
;
748 cfun
->machine
->funtype
= funtype
;
749 cfun
->machine
->start_call
= const0_rtx
;
754 rtx_expr_list
*args_so_far
= cfun
->machine
->call_args
;
756 cfun
->machine
->call_args
= alloc_EXPR_LIST (VOIDmode
, arg
, args_so_far
);
759 /* Implement the corresponding END_CALL_ARGS hook. Clear and free the
760 information we recorded. */
763 nvptx_end_call_args (void)
765 cfun
->machine
->start_call
= NULL_RTX
;
766 free_EXPR_LIST_list (&cfun
->machine
->call_args
);
769 /* Emit the sequence for a call to ADDRESS, setting RETVAL. Keep
770 track of whether calls involving static chains or varargs were seen
771 in the current function.
772 For libcalls, maintain a hash table of decls we have seen, and
773 record a function decl for later when encountering a new one. */
776 nvptx_expand_call (rtx retval
, rtx address
)
779 rtx callee
= XEXP (address
, 0);
782 bool external_decl
= false;
783 rtx varargs
= NULL_RTX
;
784 tree decl_type
= NULL_TREE
;
786 for (t
= cfun
->machine
->call_args
; t
; t
= XEXP (t
, 1))
789 if (!call_insn_operand (callee
, Pmode
))
791 callee
= force_reg (Pmode
, callee
);
792 address
= change_address (address
, QImode
, callee
);
795 if (GET_CODE (callee
) == SYMBOL_REF
)
797 tree decl
= SYMBOL_REF_DECL (callee
);
798 if (decl
!= NULL_TREE
)
800 decl_type
= TREE_TYPE (decl
);
801 if (DECL_STATIC_CHAIN (decl
))
802 cfun
->machine
->has_call_with_sc
= true;
803 if (DECL_EXTERNAL (decl
))
804 external_decl
= true;
808 if (cfun
->machine
->funtype
809 /* It's possible to construct testcases where we call a variable.
810 See compile/20020129-1.c. stdarg_p will crash so avoid calling it
812 && (TREE_CODE (cfun
->machine
->funtype
) == FUNCTION_TYPE
813 || TREE_CODE (cfun
->machine
->funtype
) == METHOD_TYPE
)
814 && stdarg_p (cfun
->machine
->funtype
))
816 varargs
= gen_reg_rtx (Pmode
);
818 emit_move_insn (varargs
, stack_pointer_rtx
);
820 emit_move_insn (varargs
, stack_pointer_rtx
);
821 cfun
->machine
->has_call_with_varargs
= true;
823 vec
= rtvec_alloc (nargs
+ 1 + (varargs
? 1 : 0));
824 pat
= gen_rtx_PARALLEL (VOIDmode
, vec
);
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, vec_pos
++) = t
;
838 /* Construct the call insn, including a USE for each argument pseudo
839 register. These will be used when printing the insn. */
840 for (rtx arg
= cfun
->machine
->call_args
; arg
; arg
= XEXP (arg
, 1))
842 rtx this_arg
= XEXP (arg
, 0);
843 XVECEXP (pat
, 0, vec_pos
++) = gen_rtx_USE (VOIDmode
, this_arg
);
847 XVECEXP (pat
, 0, vec_pos
++) = gen_rtx_USE (VOIDmode
, varargs
);
849 gcc_assert (vec_pos
= XVECLEN (pat
, 0));
851 /* If this is a libcall, decl_type is NULL. For a call to a non-libcall
852 undeclared function, we'll have an external decl without arg types.
853 In either case we have to try to construct a ptx declaration from one of
854 the calls to the function. */
856 && (decl_type
== NULL_TREE
857 || (external_decl
&& TYPE_ARG_TYPES (decl_type
) == NULL_TREE
)))
859 rtx
*slot
= declared_libfuncs_htab
->find_slot (callee
, INSERT
);
863 write_func_decl_from_insn (func_decls
, retval
, pat
, callee
);
866 emit_call_insn (pat
);
867 if (tmp_retval
!= retval
)
868 emit_move_insn (retval
, tmp_retval
);
871 /* Implement TARGET_FUNCTION_ARG. */
874 nvptx_function_arg (cumulative_args_t
, machine_mode mode
,
875 const_tree
, bool named
)
877 if (mode
== VOIDmode
)
881 return gen_reg_rtx (mode
);
885 /* Implement TARGET_FUNCTION_INCOMING_ARG. */
888 nvptx_function_incoming_arg (cumulative_args_t cum_v
, machine_mode mode
,
889 const_tree
, bool named
)
891 CUMULATIVE_ARGS
*cum
= get_cumulative_args (cum_v
);
892 if (mode
== VOIDmode
)
898 /* No need to deal with split modes here, the only case that can
899 happen is complex modes and those are dealt with by
900 TARGET_SPLIT_COMPLEX_ARG. */
901 return gen_rtx_UNSPEC (mode
,
902 gen_rtvec (1, GEN_INT (1 + cum
->count
)),
906 /* Implement TARGET_FUNCTION_ARG_ADVANCE. */
909 nvptx_function_arg_advance (cumulative_args_t cum_v
, machine_mode mode
,
910 const_tree type ATTRIBUTE_UNUSED
,
911 bool named ATTRIBUTE_UNUSED
)
913 CUMULATIVE_ARGS
*cum
= get_cumulative_args (cum_v
);
920 /* Handle the TARGET_STRICT_ARGUMENT_NAMING target hook.
922 For nvptx, we know how to handle functions declared as stdarg: by
923 passing an extra pointer to the unnamed arguments. However, the
924 Fortran frontend can produce a different situation, where a
925 function pointer is declared with no arguments, but the actual
926 function and calls to it take more arguments. In that case, we
927 want to ensure the call matches the definition of the function. */
930 nvptx_strict_argument_naming (cumulative_args_t cum_v
)
932 CUMULATIVE_ARGS
*cum
= get_cumulative_args (cum_v
);
933 return cum
->fntype
== NULL_TREE
|| stdarg_p (cum
->fntype
);
936 /* Implement TARGET_FUNCTION_ARG_BOUNDARY. */
939 nvptx_function_arg_boundary (machine_mode mode
, const_tree type
)
941 unsigned int boundary
= type
? TYPE_ALIGN (type
) : GET_MODE_BITSIZE (mode
);
943 if (boundary
> BITS_PER_WORD
)
944 return 2 * BITS_PER_WORD
;
948 HOST_WIDE_INT size
= int_size_in_bytes (type
);
950 return 2 * BITS_PER_WORD
;
951 if (boundary
< BITS_PER_WORD
)
954 return BITS_PER_WORD
;
956 return 2 * BITS_PER_UNIT
;
962 /* TARGET_FUNCTION_VALUE implementation. Returns an RTX representing the place
963 where function FUNC returns or receives a value of data type TYPE. */
966 nvptx_function_value (const_tree type
, const_tree func ATTRIBUTE_UNUSED
,
969 int unsignedp
= TYPE_UNSIGNED (type
);
970 machine_mode orig_mode
= TYPE_MODE (type
);
971 machine_mode mode
= promote_function_mode (type
, orig_mode
,
972 &unsignedp
, NULL_TREE
, 1);
974 return gen_rtx_REG (mode
, NVPTX_RETURN_REGNUM
);
975 if (cfun
->machine
->start_call
== NULL_RTX
)
976 /* Pretend to return in a hard reg for early uses before pseudos can be
978 return gen_rtx_REG (mode
, NVPTX_RETURN_REGNUM
);
979 return gen_reg_rtx (mode
);
982 /* Implement TARGET_LIBCALL_VALUE. */
985 nvptx_libcall_value (machine_mode mode
, const_rtx
)
987 if (cfun
->machine
->start_call
== NULL_RTX
)
988 /* Pretend to return in a hard reg for early uses before pseudos can be
990 return gen_rtx_REG (mode
, NVPTX_RETURN_REGNUM
);
991 return gen_reg_rtx (mode
);
994 /* Implement TARGET_FUNCTION_VALUE_REGNO_P. */
997 nvptx_function_value_regno_p (const unsigned int regno
)
999 return regno
== NVPTX_RETURN_REGNUM
;
1002 /* Types with a mode other than those supported by the machine are passed by
1003 reference in memory. */
1006 nvptx_pass_by_reference (cumulative_args_t
, machine_mode mode
,
1007 const_tree type
, bool)
1009 return !PASS_IN_REG_P (mode
, type
);
1012 /* Implement TARGET_RETURN_IN_MEMORY. */
1015 nvptx_return_in_memory (const_tree type
, const_tree
)
1017 machine_mode mode
= TYPE_MODE (type
);
1018 if (!RETURN_IN_REG_P (mode
))
1023 /* Implement TARGET_PROMOTE_FUNCTION_MODE. */
1026 nvptx_promote_function_mode (const_tree type
, machine_mode mode
,
1028 const_tree funtype
, int for_return
)
1030 if (type
== NULL_TREE
)
1033 return promote_mode (type
, mode
, punsignedp
);
1034 /* For K&R-style functions, try to match the language promotion rules to
1035 minimize type mismatches at assembly time. */
1036 if (TYPE_ARG_TYPES (funtype
) == NULL_TREE
1037 && type
!= NULL_TREE
1038 && !AGGREGATE_TYPE_P (type
))
1042 mode
= arg_promotion (mode
);
1048 /* Implement TARGET_STATIC_CHAIN. */
1051 nvptx_static_chain (const_tree fndecl
, bool incoming_p
)
1053 if (!DECL_STATIC_CHAIN (fndecl
))
1057 return gen_rtx_REG (Pmode
, STATIC_CHAIN_REGNUM
);
1059 return gen_rtx_REG (Pmode
, OUTGOING_STATIC_CHAIN_REGNUM
);
1062 /* Emit a comparison COMPARE, and return the new test to be used in the
1066 nvptx_expand_compare (rtx compare
)
1068 rtx pred
= gen_reg_rtx (BImode
);
1069 rtx cmp
= gen_rtx_fmt_ee (GET_CODE (compare
), BImode
,
1070 XEXP (compare
, 0), XEXP (compare
, 1));
1071 emit_insn (gen_rtx_SET (pred
, cmp
));
1072 return gen_rtx_NE (BImode
, pred
, const0_rtx
);
1075 /* When loading an operand ORIG_OP, verify whether an address space
1076 conversion to generic is required, and if so, perform it. Also
1077 check for SYMBOL_REFs for function decls and call
1078 nvptx_record_needed_fndecl as needed.
1079 Return either the original operand, or the converted one. */
1082 nvptx_maybe_convert_symbolic_operand (rtx orig_op
)
1084 if (GET_MODE (orig_op
) != Pmode
)
1088 while (GET_CODE (op
) == PLUS
|| GET_CODE (op
) == CONST
)
1090 if (GET_CODE (op
) != SYMBOL_REF
)
1093 tree decl
= SYMBOL_REF_DECL (op
);
1094 if (decl
&& TREE_CODE (decl
) == FUNCTION_DECL
)
1096 nvptx_record_needed_fndecl (decl
);
1100 addr_space_t as
= nvptx_addr_space_from_address (op
);
1101 if (as
== ADDR_SPACE_GENERIC
)
1105 code
= (as
== ADDR_SPACE_GLOBAL
? UNSPEC_FROM_GLOBAL
1106 : as
== ADDR_SPACE_LOCAL
? UNSPEC_FROM_LOCAL
1107 : as
== ADDR_SPACE_SHARED
? UNSPEC_FROM_SHARED
1108 : as
== ADDR_SPACE_CONST
? UNSPEC_FROM_CONST
1109 : UNSPEC_FROM_PARAM
);
1110 rtx dest
= gen_reg_rtx (Pmode
);
1111 emit_insn (gen_rtx_SET (dest
, gen_rtx_UNSPEC (Pmode
, gen_rtvec (1, orig_op
),
1116 /* Returns true if X is a valid address for use in a memory reference. */
1119 nvptx_legitimate_address_p (machine_mode
, rtx x
, bool)
1121 enum rtx_code code
= GET_CODE (x
);
1129 if (REG_P (XEXP (x
, 0)) && CONST_INT_P (XEXP (x
, 1)))
1143 /* Implement HARD_REGNO_MODE_OK. We barely use hard regs, but we want
1144 to ensure that the return register's mode isn't changed. */
1147 nvptx_hard_regno_mode_ok (int regno
, machine_mode mode
)
1149 if (regno
!= NVPTX_RETURN_REGNUM
1150 || cfun
== NULL
|| cfun
->machine
->ret_reg_mode
== VOIDmode
)
1152 return mode
== cfun
->machine
->ret_reg_mode
;
1155 /* Convert an address space AS to the corresponding ptx string. */
1158 nvptx_section_from_addr_space (addr_space_t as
)
1162 case ADDR_SPACE_CONST
:
1165 case ADDR_SPACE_GLOBAL
:
1168 case ADDR_SPACE_SHARED
:
1171 case ADDR_SPACE_GENERIC
:
1179 /* Determine whether DECL goes into .const or .global. */
1182 nvptx_section_for_decl (const_tree decl
)
1184 bool is_const
= (CONSTANT_CLASS_P (decl
)
1185 || TREE_CODE (decl
) == CONST_DECL
1186 || TREE_READONLY (decl
));
1193 /* Look for a SYMBOL_REF in ADDR and return the address space to be used
1194 for the insn referencing this address. */
1197 nvptx_addr_space_from_address (rtx addr
)
1199 while (GET_CODE (addr
) == PLUS
|| GET_CODE (addr
) == CONST
)
1200 addr
= XEXP (addr
, 0);
1201 if (GET_CODE (addr
) != SYMBOL_REF
)
1202 return ADDR_SPACE_GENERIC
;
1204 tree decl
= SYMBOL_REF_DECL (addr
);
1205 if (decl
== NULL_TREE
|| TREE_CODE (decl
) == FUNCTION_DECL
)
1206 return ADDR_SPACE_GENERIC
;
1208 bool is_const
= (CONSTANT_CLASS_P (decl
)
1209 || TREE_CODE (decl
) == CONST_DECL
1210 || TREE_READONLY (decl
));
1212 return ADDR_SPACE_CONST
;
1214 return ADDR_SPACE_GLOBAL
;
1217 /* Machinery to output constant initializers. When beginning an initializer,
1218 we decide on a chunk size (which is visible in ptx in the type used), and
1219 then all initializer data is buffered until a chunk is filled and ready to
1222 /* Used when assembling integers to ensure data is emitted in
1223 pieces whose size matches the declaration we printed. */
1224 static unsigned int decl_chunk_size
;
1225 static machine_mode decl_chunk_mode
;
1226 /* Used in the same situation, to keep track of the byte offset
1227 into the initializer. */
1228 static unsigned HOST_WIDE_INT decl_offset
;
1229 /* The initializer part we are currently processing. */
1230 static HOST_WIDE_INT init_part
;
1231 /* The total size of the object. */
1232 static unsigned HOST_WIDE_INT object_size
;
1233 /* True if we found a skip extending to the end of the object. Used to
1234 assert that no data follows. */
1235 static bool object_finished
;
1237 /* Write the necessary separator string to begin a new initializer value. */
1240 begin_decl_field (void)
1242 /* We never see decl_offset at zero by the time we get here. */
1243 if (decl_offset
== decl_chunk_size
)
1244 fprintf (asm_out_file
, " = { ");
1246 fprintf (asm_out_file
, ", ");
1249 /* Output the currently stored chunk as an initializer value. */
1252 output_decl_chunk (void)
1254 begin_decl_field ();
1255 output_address (gen_int_mode (init_part
, decl_chunk_mode
));
1259 /* Add value VAL sized SIZE to the data we're emitting, and keep writing
1260 out chunks as they fill up. */
1263 nvptx_assemble_value (HOST_WIDE_INT val
, unsigned int size
)
1265 unsigned HOST_WIDE_INT chunk_offset
= decl_offset
% decl_chunk_size
;
1266 gcc_assert (!object_finished
);
1269 int this_part
= size
;
1270 if (chunk_offset
+ this_part
> decl_chunk_size
)
1271 this_part
= decl_chunk_size
- chunk_offset
;
1272 HOST_WIDE_INT val_part
;
1273 HOST_WIDE_INT mask
= 2;
1274 mask
<<= this_part
* BITS_PER_UNIT
- 1;
1275 val_part
= val
& (mask
- 1);
1276 init_part
|= val_part
<< (BITS_PER_UNIT
* chunk_offset
);
1277 val
>>= BITS_PER_UNIT
* this_part
;
1279 decl_offset
+= this_part
;
1280 if (decl_offset
% decl_chunk_size
== 0)
1281 output_decl_chunk ();
1287 /* Target hook for assembling integer object X of size SIZE. */
1290 nvptx_assemble_integer (rtx x
, unsigned int size
, int ARG_UNUSED (aligned_p
))
1292 if (GET_CODE (x
) == SYMBOL_REF
|| GET_CODE (x
) == CONST
)
1294 gcc_assert (size
= decl_chunk_size
);
1295 if (decl_offset
% decl_chunk_size
!= 0)
1296 sorry ("cannot emit unaligned pointers in ptx assembly");
1297 decl_offset
+= size
;
1298 begin_decl_field ();
1300 HOST_WIDE_INT off
= 0;
1301 if (GET_CODE (x
) == CONST
)
1303 if (GET_CODE (x
) == PLUS
)
1305 off
= INTVAL (XEXP (x
, 1));
1308 if (GET_CODE (x
) == SYMBOL_REF
)
1310 nvptx_record_needed_fndecl (SYMBOL_REF_DECL (x
));
1311 fprintf (asm_out_file
, "generic(");
1313 fprintf (asm_out_file
, ")");
1316 fprintf (asm_out_file
, " + " HOST_WIDE_INT_PRINT_DEC
, off
);
1321 switch (GET_CODE (x
))
1333 nvptx_assemble_value (val
, size
);
1337 /* Output SIZE zero bytes. We ignore the FILE argument since the
1338 functions we're calling to perform the output just use
1342 nvptx_output_skip (FILE *, unsigned HOST_WIDE_INT size
)
1344 if (decl_offset
+ size
>= object_size
)
1346 if (decl_offset
% decl_chunk_size
!= 0)
1347 nvptx_assemble_value (0, decl_chunk_size
);
1348 object_finished
= true;
1352 while (size
> decl_chunk_size
)
1354 nvptx_assemble_value (0, decl_chunk_size
);
1355 size
-= decl_chunk_size
;
1358 nvptx_assemble_value (0, 1);
1361 /* Output a string STR with length SIZE. As in nvptx_output_skip we
1362 ignore the FILE arg. */
1365 nvptx_output_ascii (FILE *, const char *str
, unsigned HOST_WIDE_INT size
)
1367 for (unsigned HOST_WIDE_INT i
= 0; i
< size
; i
++)
1368 nvptx_assemble_value (str
[i
], 1);
1371 /* Called when the initializer for a decl has been completely output through
1372 combinations of the three functions above. */
1375 nvptx_assemble_decl_end (void)
1377 if (decl_offset
!= 0)
1379 if (!object_finished
&& decl_offset
% decl_chunk_size
!= 0)
1380 nvptx_assemble_value (0, decl_chunk_size
);
1382 fprintf (asm_out_file
, " }");
1384 fprintf (asm_out_file
, ";\n");
1387 /* Start a declaration of a variable of TYPE with NAME to
1388 FILE. IS_PUBLIC says whether this will be externally visible.
1389 Here we just write the linker hint and decide on the chunk size
1393 init_output_initializer (FILE *file
, const char *name
, const_tree type
,
1396 fprintf (file
, "// BEGIN%s VAR DEF: ", is_public
? " GLOBAL" : "");
1397 assemble_name_raw (file
, name
);
1400 if (TREE_CODE (type
) == ARRAY_TYPE
)
1401 type
= TREE_TYPE (type
);
1402 int sz
= int_size_in_bytes (type
);
1403 if ((TREE_CODE (type
) != INTEGER_TYPE
1404 && TREE_CODE (type
) != ENUMERAL_TYPE
1405 && TREE_CODE (type
) != REAL_TYPE
)
1407 || sz
> HOST_BITS_PER_WIDE_INT
)
1408 type
= ptr_type_node
;
1409 decl_chunk_size
= int_size_in_bytes (type
);
1410 decl_chunk_mode
= int_mode_for_mode (TYPE_MODE (type
));
1413 object_finished
= false;
1416 /* Implement TARGET_ASM_DECLARE_CONSTANT_NAME. Begin the process of
1417 writing a constant variable EXP with NAME and SIZE and its
1418 initializer to FILE. */
1421 nvptx_asm_declare_constant_name (FILE *file
, const char *name
,
1422 const_tree exp
, HOST_WIDE_INT size
)
1424 tree type
= TREE_TYPE (exp
);
1425 init_output_initializer (file
, name
, type
, false);
1426 fprintf (file
, "\t.const .align %d .u%d ",
1427 TYPE_ALIGN (TREE_TYPE (exp
)) / BITS_PER_UNIT
,
1428 decl_chunk_size
* BITS_PER_UNIT
);
1429 assemble_name (file
, name
);
1430 fprintf (file
, "[" HOST_WIDE_INT_PRINT_DEC
"]",
1431 (size
+ decl_chunk_size
- 1) / decl_chunk_size
);
1435 /* Implement the ASM_DECLARE_OBJECT_NAME macro. Used to start writing
1436 a variable DECL with NAME to FILE. */
1439 nvptx_declare_object_name (FILE *file
, const char *name
, const_tree decl
)
1441 if (decl
&& DECL_SIZE (decl
))
1443 tree type
= TREE_TYPE (decl
);
1444 unsigned HOST_WIDE_INT size
;
1446 init_output_initializer (file
, name
, type
, TREE_PUBLIC (decl
));
1447 size
= tree_to_uhwi (DECL_SIZE_UNIT (decl
));
1448 const char *section
= nvptx_section_for_decl (decl
);
1449 fprintf (file
, "\t%s%s .align %d .u%d ",
1450 TREE_PUBLIC (decl
) ? " .visible" : "", section
,
1451 DECL_ALIGN (decl
) / BITS_PER_UNIT
,
1452 decl_chunk_size
* BITS_PER_UNIT
);
1453 assemble_name (file
, name
);
1455 fprintf (file
, "[" HOST_WIDE_INT_PRINT_DEC
"]",
1456 (size
+ decl_chunk_size
- 1) / decl_chunk_size
);
1458 object_finished
= true;
1463 /* Implement TARGET_ASM_GLOBALIZE_LABEL by doing nothing. */
1466 nvptx_globalize_label (FILE *, const char *)
1470 /* Implement TARGET_ASM_ASSEMBLE_UNDEFINED_DECL. Write an extern
1471 declaration only for variable DECL with NAME to FILE. */
1473 nvptx_assemble_undefined_decl (FILE *file
, const char *name
, const_tree decl
)
1475 if (TREE_CODE (decl
) != VAR_DECL
)
1477 const char *section
= nvptx_section_for_decl (decl
);
1478 fprintf (file
, "// BEGIN%s VAR DECL: ", TREE_PUBLIC (decl
) ? " GLOBAL" : "");
1479 assemble_name_raw (file
, name
);
1481 HOST_WIDE_INT size
= int_size_in_bytes (TREE_TYPE (decl
));
1482 fprintf (file
, ".extern %s .b8 ", section
);
1483 assemble_name_raw (file
, name
);
1485 fprintf (file
, "[" HOST_WIDE_INT_PRINT_DEC
"]", size
);
1486 fprintf (file
, ";\n\n");
1489 /* Output INSN, which is a call to CALLEE with result RESULT. For ptx, this
1490 involves writing .param declarations and in/out copies into them. For
1491 indirect calls, also write the .callprototype. */
1494 nvptx_output_call_insn (rtx_insn
*insn
, rtx result
, rtx callee
)
1498 bool needs_tgt
= register_operand (callee
, Pmode
);
1499 rtx pat
= PATTERN (insn
);
1500 int arg_end
= XVECLEN (pat
, 0);
1501 tree decl
= NULL_TREE
;
1503 fprintf (asm_out_file
, "\t{\n");
1505 fprintf (asm_out_file
, "\t\t.param%s %%retval_in;\n",
1506 nvptx_ptx_type_from_mode (arg_promotion (GET_MODE (result
)),
1509 /* Ensure we have a ptx declaration in the output if necessary. */
1510 if (GET_CODE (callee
) == SYMBOL_REF
)
1512 decl
= SYMBOL_REF_DECL (callee
);
1513 if (decl
&& DECL_EXTERNAL (decl
))
1514 nvptx_record_fndecl (decl
);
1519 ASM_GENERATE_INTERNAL_LABEL (buf
, "LCT", labelno
);
1521 ASM_OUTPUT_LABEL (asm_out_file
, buf
);
1522 std::stringstream s
;
1523 write_func_decl_from_insn (s
, result
, pat
, callee
);
1524 fputs (s
.str().c_str(), asm_out_file
);
1527 for (int i
= 1, argno
= 0; i
< arg_end
; i
++)
1529 rtx t
= XEXP (XVECEXP (pat
, 0, i
), 0);
1530 machine_mode mode
= GET_MODE (t
);
1531 int count
= maybe_split_mode (&mode
);
1534 fprintf (asm_out_file
, "\t\t.param%s %%out_arg%d%s;\n",
1535 nvptx_ptx_type_from_mode (mode
, false), argno
++,
1536 mode
== QImode
|| mode
== HImode
? "[1]" : "");
1538 for (int i
= 1, argno
= 0; i
< arg_end
; i
++)
1540 rtx t
= XEXP (XVECEXP (pat
, 0, i
), 0);
1541 gcc_assert (REG_P (t
));
1542 machine_mode mode
= GET_MODE (t
);
1543 int count
= maybe_split_mode (&mode
);
1546 fprintf (asm_out_file
, "\t\tst.param%s [%%out_arg%d], %%r%d;\n",
1547 nvptx_ptx_type_from_mode (mode
, false), argno
++,
1553 fprintf (asm_out_file
, "\t\tst.param%s [%%out_arg%d], %%r%d$%d;\n",
1554 nvptx_ptx_type_from_mode (mode
, false), argno
++,
1559 fprintf (asm_out_file
, "\t\tcall ");
1560 if (result
!= NULL_RTX
)
1561 fprintf (asm_out_file
, "(%%retval_in), ");
1565 const char *name
= get_fnname_from_decl (decl
);
1566 name
= nvptx_name_replacement (name
);
1567 assemble_name (asm_out_file
, name
);
1570 output_address (callee
);
1572 if (arg_end
> 1 || (decl
&& DECL_STATIC_CHAIN (decl
)))
1574 const char *comma
= "";
1576 fprintf (asm_out_file
, ", (");
1577 for (int i
= 1, argno
= 0; i
< arg_end
; i
++)
1579 rtx t
= XEXP (XVECEXP (pat
, 0, i
), 0);
1580 machine_mode mode
= GET_MODE (t
);
1581 int count
= maybe_split_mode (&mode
);
1585 fprintf (asm_out_file
, "%s%%out_arg%d", comma
, argno
++);
1589 if (decl
&& DECL_STATIC_CHAIN (decl
))
1590 fprintf (asm_out_file
, "%s%s", comma
,
1591 reg_names
[OUTGOING_STATIC_CHAIN_REGNUM
]);
1593 fprintf (asm_out_file
, ")");
1598 fprintf (asm_out_file
, ", ");
1599 assemble_name (asm_out_file
, buf
);
1601 fprintf (asm_out_file
, ";\n");
1602 if (result
!= NULL_RTX
)
1603 return "ld.param%t0\t%0, [%%retval_in];\n\t}";
1608 /* Implement TARGET_PRINT_OPERAND_PUNCT_VALID_P. */
1611 nvptx_print_operand_punct_valid_p (unsigned char c
)
1613 return c
== '.' || c
== '#';
1616 static void nvptx_print_operand (FILE *, rtx
, int);
1618 /* Subroutine of nvptx_print_operand; used to print a memory reference X to FILE. */
1621 nvptx_print_address_operand (FILE *file
, rtx x
, machine_mode
)
1624 if (GET_CODE (x
) == CONST
)
1626 switch (GET_CODE (x
))
1630 output_address (XEXP (x
, 0));
1631 fprintf (file
, "+");
1632 output_address (off
);
1637 output_addr_const (file
, x
);
1641 gcc_assert (GET_CODE (x
) != MEM
);
1642 nvptx_print_operand (file
, x
, 0);
1647 /* Write assembly language output for the address ADDR to FILE. */
1650 nvptx_print_operand_address (FILE *file
, rtx addr
)
1652 nvptx_print_address_operand (file
, addr
, VOIDmode
);
1655 /* Print an operand, X, to FILE, with an optional modifier in CODE.
1658 . -- print the predicate for the instruction or an emptry string for an
1660 # -- print a rounding mode for the instruction
1662 A -- print an address space identifier for a MEM
1663 c -- print an opcode suffix for a comparison operator, including a type code
1664 d -- print a CONST_INT as a vector dimension (x, y, or z)
1665 f -- print a full reg even for something that must always be split
1666 t -- print a type opcode suffix, promoting QImode to 32 bits
1667 T -- print a type size in bits
1668 u -- print a type opcode suffix without promotions. */
1671 nvptx_print_operand (FILE *file
, rtx x
, int code
)
1674 machine_mode op_mode
;
1678 x
= current_insn_predicate
;
1681 unsigned int regno
= REGNO (XEXP (x
, 0));
1683 if (GET_CODE (x
) == EQ
)
1685 fputs (reg_names
[regno
], file
);
1690 else if (code
== '#')
1692 fputs (".rn", file
);
1696 enum rtx_code x_code
= GET_CODE (x
);
1702 addr_space_t as
= nvptx_addr_space_from_address (XEXP (x
, 0));
1703 fputs (nvptx_section_from_addr_space (as
), file
);
1708 gcc_assert (x_code
== CONST_INT
);
1709 if (INTVAL (x
) == 0)
1711 else if (INTVAL (x
) == 1)
1713 else if (INTVAL (x
) == 2)
1720 op_mode
= nvptx_underlying_object_mode (x
);
1721 fprintf (file
, "%s", nvptx_ptx_type_from_mode (op_mode
, true));
1725 op_mode
= nvptx_underlying_object_mode (x
);
1726 fprintf (file
, "%s", nvptx_ptx_type_from_mode (op_mode
, false));
1730 fprintf (file
, "%d", GET_MODE_BITSIZE (GET_MODE (x
)));
1734 fprintf (file
, "@");
1738 fprintf (file
, "@!");
1742 op_mode
= GET_MODE (XEXP (x
, 0));
1746 fputs (".eq", file
);
1749 if (FLOAT_MODE_P (op_mode
))
1750 fputs (".neu", file
);
1752 fputs (".ne", file
);
1755 fputs (".le", file
);
1758 fputs (".ge", file
);
1761 fputs (".lt", file
);
1764 fputs (".gt", file
);
1767 fputs (".ls", file
);
1770 fputs (".hs", file
);
1773 fputs (".lo", file
);
1776 fputs (".hi", file
);
1779 fputs (".ne", file
);
1782 fputs (".equ", file
);
1785 fputs (".leu", file
);
1788 fputs (".geu", file
);
1791 fputs (".ltu", file
);
1794 fputs (".gtu", file
);
1797 fputs (".nan", file
);
1800 fputs (".num", file
);
1805 if (FLOAT_MODE_P (op_mode
)
1806 || x_code
== EQ
|| x_code
== NE
1807 || x_code
== GEU
|| x_code
== GTU
1808 || x_code
== LEU
|| x_code
== LTU
)
1809 fputs (nvptx_ptx_type_from_mode (op_mode
, true), file
);
1811 fprintf (file
, ".s%d", GET_MODE_BITSIZE (op_mode
));
1822 if (HARD_REGISTER_P (x
))
1823 fprintf (file
, "%s", reg_names
[REGNO (x
)]);
1825 fprintf (file
, "%%r%d", REGNO (x
));
1826 if (code
!= 'f' && nvptx_split_reg_p (GET_MODE (x
)))
1828 gcc_assert (GET_CODE (orig_x
) == SUBREG
1829 && !nvptx_split_reg_p (GET_MODE (orig_x
)));
1830 fprintf (file
, "$%d", SUBREG_BYTE (orig_x
) / UNITS_PER_WORD
);
1836 nvptx_print_address_operand (file
, XEXP (x
, 0), GET_MODE (x
));
1841 output_addr_const (file
, x
);
1847 /* We could use output_addr_const, but that can print things like
1848 "x-8", which breaks ptxas. Need to ensure it is output as
1850 nvptx_print_address_operand (file
, x
, VOIDmode
);
1855 real_to_target (vals
, CONST_DOUBLE_REAL_VALUE (x
), GET_MODE (x
));
1856 vals
[0] &= 0xffffffff;
1857 vals
[1] &= 0xffffffff;
1858 if (GET_MODE (x
) == SFmode
)
1859 fprintf (file
, "0f%08lx", vals
[0]);
1861 fprintf (file
, "0d%08lx%08lx", vals
[1], vals
[0]);
1865 output_addr_const (file
, x
);
1870 /* Record replacement regs used to deal with subreg operands. */
1873 rtx replacement
[MAX_RECOG_OPERANDS
];
1879 /* Allocate or reuse a replacement in R and return the rtx. */
1882 get_replacement (struct reg_replace
*r
)
1884 if (r
->n_allocated
== r
->n_in_use
)
1885 r
->replacement
[r
->n_allocated
++] = gen_reg_rtx (r
->mode
);
1886 return r
->replacement
[r
->n_in_use
++];
1889 /* Clean up subreg operands. In ptx assembly, everything is typed, and
1890 the presence of subregs would break the rules for most instructions.
1891 Replace them with a suitable new register of the right size, plus
1892 conversion copyin/copyout instructions. */
1895 nvptx_reorg_subreg (void)
1897 struct reg_replace qiregs
, hiregs
, siregs
, diregs
;
1898 rtx_insn
*insn
, *next
;
1900 qiregs
.n_allocated
= 0;
1901 hiregs
.n_allocated
= 0;
1902 siregs
.n_allocated
= 0;
1903 diregs
.n_allocated
= 0;
1904 qiregs
.mode
= QImode
;
1905 hiregs
.mode
= HImode
;
1906 siregs
.mode
= SImode
;
1907 diregs
.mode
= DImode
;
1909 for (insn
= get_insns (); insn
; insn
= next
)
1911 next
= NEXT_INSN (insn
);
1912 if (!NONDEBUG_INSN_P (insn
)
1913 || asm_noperands (PATTERN (insn
)) >= 0
1914 || GET_CODE (PATTERN (insn
)) == USE
1915 || GET_CODE (PATTERN (insn
)) == CLOBBER
)
1918 qiregs
.n_in_use
= 0;
1919 hiregs
.n_in_use
= 0;
1920 siregs
.n_in_use
= 0;
1921 diregs
.n_in_use
= 0;
1922 extract_insn (insn
);
1923 enum attr_subregs_ok s_ok
= get_attr_subregs_ok (insn
);
1925 for (int i
= 0; i
< recog_data
.n_operands
; i
++)
1927 rtx op
= recog_data
.operand
[i
];
1928 if (GET_CODE (op
) != SUBREG
)
1931 rtx inner
= SUBREG_REG (op
);
1933 machine_mode outer_mode
= GET_MODE (op
);
1934 machine_mode inner_mode
= GET_MODE (inner
);
1937 && (GET_MODE_PRECISION (inner_mode
)
1938 >= GET_MODE_PRECISION (outer_mode
)))
1940 gcc_assert (SCALAR_INT_MODE_P (outer_mode
));
1941 struct reg_replace
*r
= (outer_mode
== QImode
? &qiregs
1942 : outer_mode
== HImode
? &hiregs
1943 : outer_mode
== SImode
? &siregs
1945 rtx new_reg
= get_replacement (r
);
1947 if (recog_data
.operand_type
[i
] != OP_OUT
)
1950 if (GET_MODE_PRECISION (inner_mode
)
1951 < GET_MODE_PRECISION (outer_mode
))
1956 rtx pat
= gen_rtx_SET (new_reg
,
1957 gen_rtx_fmt_e (code
, outer_mode
, inner
));
1958 emit_insn_before (pat
, insn
);
1961 if (recog_data
.operand_type
[i
] != OP_IN
)
1964 if (GET_MODE_PRECISION (inner_mode
)
1965 < GET_MODE_PRECISION (outer_mode
))
1970 rtx pat
= gen_rtx_SET (inner
,
1971 gen_rtx_fmt_e (code
, inner_mode
, new_reg
));
1972 emit_insn_after (pat
, insn
);
1974 validate_change (insn
, recog_data
.operand_loc
[i
], new_reg
, false);
1979 /* PTX-specific reorganization
1980 - Compute live registers
1981 - Mark now-unused registers, so function begin doesn't declare
1983 - Replace subregs with suitable sequences.
1989 /* We are freeing block_for_insn in the toplev to keep compatibility
1990 with old MDEP_REORGS that are not CFG based. Recompute it now. */
1991 compute_bb_for_insn ();
1993 thread_prologue_and_epilogue_insns ();
1995 /* Compute live regs */
1996 df_clear_flags (DF_LR_RUN_DCE
);
1997 df_set_flags (DF_NO_INSN_RESCAN
| DF_NO_HARD_REGS
);
1999 regstat_init_n_sets_and_refs ();
2001 int max_regs
= max_reg_num ();
2003 /* Mark unused regs as unused. */
2004 for (int i
= LAST_VIRTUAL_REGISTER
+ 1; i
< max_regs
; i
++)
2005 if (REG_N_SETS (i
) == 0 && REG_N_REFS (i
) == 0)
2006 regno_reg_rtx
[i
] = const0_rtx
;
2008 /* Replace subregs. */
2009 nvptx_reorg_subreg ();
2011 regstat_free_n_sets_and_refs ();
2013 df_finish_pass (true);
2016 /* Handle a "kernel" attribute; arguments as in
2017 struct attribute_spec.handler. */
2020 nvptx_handle_kernel_attribute (tree
*node
, tree name
, tree
ARG_UNUSED (args
),
2021 int ARG_UNUSED (flags
), bool *no_add_attrs
)
2025 if (TREE_CODE (decl
) != FUNCTION_DECL
)
2027 error ("%qE attribute only applies to functions", name
);
2028 *no_add_attrs
= true;
2031 else if (TREE_TYPE (TREE_TYPE (decl
)) != void_type_node
)
2033 error ("%qE attribute requires a void return type", name
);
2034 *no_add_attrs
= true;
2040 /* Table of valid machine attributes. */
2041 static const struct attribute_spec nvptx_attribute_table
[] =
2043 /* { name, min_len, max_len, decl_req, type_req, fn_type_req, handler,
2044 affects_type_identity } */
2045 { "kernel", 0, 0, true, false, false, nvptx_handle_kernel_attribute
, false },
2046 { NULL
, 0, 0, false, false, false, NULL
, false }
2049 /* Limit vector alignments to BIGGEST_ALIGNMENT. */
2051 static HOST_WIDE_INT
2052 nvptx_vector_alignment (const_tree type
)
2054 HOST_WIDE_INT align
= tree_to_shwi (TYPE_SIZE (type
));
2056 return MIN (align
, BIGGEST_ALIGNMENT
);
2059 /* Record a symbol for mkoffload to enter into the mapping table. */
2062 nvptx_record_offload_symbol (tree decl
)
2064 switch (TREE_CODE (decl
))
2067 fprintf (asm_out_file
, "//:VAR_MAP \"%s\"\n",
2068 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
)));
2073 tree attr
= get_oacc_fn_attrib (decl
);
2074 tree dims
= NULL_TREE
;
2078 dims
= TREE_VALUE (attr
);
2079 fprintf (asm_out_file
, "//:FUNC_MAP \"%s\"",
2080 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
)));
2082 for (ix
= 0; ix
!= GOMP_DIM_MAX
; ix
++)
2086 /* TODO: This check can go away once the dimension default
2087 machinery is merged to trunk. */
2090 tree dim
= TREE_VALUE (dims
);
2093 size
= TREE_INT_CST_LOW (dim
);
2095 gcc_assert (!TREE_PURPOSE (dims
));
2096 dims
= TREE_CHAIN (dims
);
2099 fprintf (asm_out_file
, ", %#x", size
);
2102 fprintf (asm_out_file
, "\n");
2111 /* Implement TARGET_ASM_FILE_START. Write the kinds of things ptxas expects
2112 at the start of a file. */
2115 nvptx_file_start (void)
2117 fputs ("// BEGIN PREAMBLE\n", asm_out_file
);
2118 fputs ("\t.version\t3.1\n", asm_out_file
);
2119 fputs ("\t.target\tsm_30\n", asm_out_file
);
2120 fprintf (asm_out_file
, "\t.address_size %d\n", GET_MODE_BITSIZE (Pmode
));
2121 fputs ("// END PREAMBLE\n", asm_out_file
);
2124 /* Write out the function declarations we've collected and declare storage
2125 for the broadcast buffer. */
2128 nvptx_file_end (void)
2130 hash_table
<tree_hasher
>::iterator iter
;
2132 FOR_EACH_HASH_TABLE_ELEMENT (*needed_fndecls_htab
, decl
, tree
, iter
)
2133 nvptx_record_fndecl (decl
, true);
2134 fputs (func_decls
.str().c_str(), asm_out_file
);
2137 /* Validate compute dimensions of an OpenACC offload or routine, fill
2138 in non-unity defaults. FN_LEVEL indicates the level at which a
2139 routine might spawn a loop. It is negative for non-routines. */
2142 nvptx_goacc_validate_dims (tree
ARG_UNUSED (decl
), int *ARG_UNUSED (dims
),
2143 int ARG_UNUSED (fn_level
))
2145 bool changed
= false;
2147 /* TODO: Leave dimensions unaltered. Partitioned execution needs
2148 porting before filtering dimensions makes sense. */
2153 #undef TARGET_OPTION_OVERRIDE
2154 #define TARGET_OPTION_OVERRIDE nvptx_option_override
2156 #undef TARGET_ATTRIBUTE_TABLE
2157 #define TARGET_ATTRIBUTE_TABLE nvptx_attribute_table
2159 #undef TARGET_LEGITIMATE_ADDRESS_P
2160 #define TARGET_LEGITIMATE_ADDRESS_P nvptx_legitimate_address_p
2162 #undef TARGET_PROMOTE_FUNCTION_MODE
2163 #define TARGET_PROMOTE_FUNCTION_MODE nvptx_promote_function_mode
2165 #undef TARGET_FUNCTION_ARG
2166 #define TARGET_FUNCTION_ARG nvptx_function_arg
2167 #undef TARGET_FUNCTION_INCOMING_ARG
2168 #define TARGET_FUNCTION_INCOMING_ARG nvptx_function_incoming_arg
2169 #undef TARGET_FUNCTION_ARG_ADVANCE
2170 #define TARGET_FUNCTION_ARG_ADVANCE nvptx_function_arg_advance
2171 #undef TARGET_FUNCTION_ARG_BOUNDARY
2172 #define TARGET_FUNCTION_ARG_BOUNDARY nvptx_function_arg_boundary
2173 #undef TARGET_FUNCTION_ARG_ROUND_BOUNDARY
2174 #define TARGET_FUNCTION_ARG_ROUND_BOUNDARY nvptx_function_arg_boundary
2175 #undef TARGET_PASS_BY_REFERENCE
2176 #define TARGET_PASS_BY_REFERENCE nvptx_pass_by_reference
2177 #undef TARGET_FUNCTION_VALUE_REGNO_P
2178 #define TARGET_FUNCTION_VALUE_REGNO_P nvptx_function_value_regno_p
2179 #undef TARGET_FUNCTION_VALUE
2180 #define TARGET_FUNCTION_VALUE nvptx_function_value
2181 #undef TARGET_LIBCALL_VALUE
2182 #define TARGET_LIBCALL_VALUE nvptx_libcall_value
2183 #undef TARGET_FUNCTION_OK_FOR_SIBCALL
2184 #define TARGET_FUNCTION_OK_FOR_SIBCALL nvptx_function_ok_for_sibcall
2185 #undef TARGET_GET_DRAP_RTX
2186 #define TARGET_GET_DRAP_RTX nvptx_get_drap_rtx
2187 #undef TARGET_SPLIT_COMPLEX_ARG
2188 #define TARGET_SPLIT_COMPLEX_ARG hook_bool_const_tree_true
2189 #undef TARGET_RETURN_IN_MEMORY
2190 #define TARGET_RETURN_IN_MEMORY nvptx_return_in_memory
2191 #undef TARGET_OMIT_STRUCT_RETURN_REG
2192 #define TARGET_OMIT_STRUCT_RETURN_REG true
2193 #undef TARGET_STRICT_ARGUMENT_NAMING
2194 #define TARGET_STRICT_ARGUMENT_NAMING nvptx_strict_argument_naming
2195 #undef TARGET_STATIC_CHAIN
2196 #define TARGET_STATIC_CHAIN nvptx_static_chain
2198 #undef TARGET_CALL_ARGS
2199 #define TARGET_CALL_ARGS nvptx_call_args
2200 #undef TARGET_END_CALL_ARGS
2201 #define TARGET_END_CALL_ARGS nvptx_end_call_args
2203 #undef TARGET_ASM_FILE_START
2204 #define TARGET_ASM_FILE_START nvptx_file_start
2205 #undef TARGET_ASM_FILE_END
2206 #define TARGET_ASM_FILE_END nvptx_file_end
2207 #undef TARGET_ASM_GLOBALIZE_LABEL
2208 #define TARGET_ASM_GLOBALIZE_LABEL nvptx_globalize_label
2209 #undef TARGET_ASM_ASSEMBLE_UNDEFINED_DECL
2210 #define TARGET_ASM_ASSEMBLE_UNDEFINED_DECL nvptx_assemble_undefined_decl
2211 #undef TARGET_PRINT_OPERAND
2212 #define TARGET_PRINT_OPERAND nvptx_print_operand
2213 #undef TARGET_PRINT_OPERAND_ADDRESS
2214 #define TARGET_PRINT_OPERAND_ADDRESS nvptx_print_operand_address
2215 #undef TARGET_PRINT_OPERAND_PUNCT_VALID_P
2216 #define TARGET_PRINT_OPERAND_PUNCT_VALID_P nvptx_print_operand_punct_valid_p
2217 #undef TARGET_ASM_INTEGER
2218 #define TARGET_ASM_INTEGER nvptx_assemble_integer
2219 #undef TARGET_ASM_DECL_END
2220 #define TARGET_ASM_DECL_END nvptx_assemble_decl_end
2221 #undef TARGET_ASM_DECLARE_CONSTANT_NAME
2222 #define TARGET_ASM_DECLARE_CONSTANT_NAME nvptx_asm_declare_constant_name
2223 #undef TARGET_USE_BLOCKS_FOR_CONSTANT_P
2224 #define TARGET_USE_BLOCKS_FOR_CONSTANT_P hook_bool_mode_const_rtx_true
2225 #undef TARGET_ASM_NEED_VAR_DECL_BEFORE_USE
2226 #define TARGET_ASM_NEED_VAR_DECL_BEFORE_USE true
2228 #undef TARGET_MACHINE_DEPENDENT_REORG
2229 #define TARGET_MACHINE_DEPENDENT_REORG nvptx_reorg
2230 #undef TARGET_NO_REGISTER_ALLOCATION
2231 #define TARGET_NO_REGISTER_ALLOCATION true
2233 #undef TARGET_RECORD_OFFLOAD_SYMBOL
2234 #define TARGET_RECORD_OFFLOAD_SYMBOL nvptx_record_offload_symbol
2236 #undef TARGET_VECTOR_ALIGNMENT
2237 #define TARGET_VECTOR_ALIGNMENT nvptx_vector_alignment
2239 #undef TARGET_GOACC_VALIDATE_DIMS
2240 #define TARGET_GOACC_VALIDATE_DIMS nvptx_goacc_validate_dims
2242 struct gcc_target targetm
= TARGET_INITIALIZER
;
2244 #include "gt-nvptx.h"