1 /* Subroutines used for expanding RISC-V builtins. 2 Copyright (C) 2011-2017 Free Software Foundation, Inc. 3 Contributed by Andrew Waterman (andrew@sifive.com). 4 5 This file is part of GCC. 6 7 GCC is free software; you can redistribute it and/or modify 8 it under the terms of the GNU General Public License as published by 9 the Free Software Foundation; either version 3, or (at your option) 10 any later version. 11 12 GCC is distributed in the hope that it will be useful, 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 GNU General Public License for more details. 16 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/>. */ 20 21 #include "config.h" 22 #include "system.h" 23 #include "coretypes.h" 24 #include "tm.h" 25 #include "rtl.h" 26 #include "tree.h" 27 #include "gimple-expr.h" 28 #include "memmodel.h" 29 #include "expmed.h" 30 #include "optabs.h" 31 #include "recog.h" 32 #include "diagnostic-core.h" 33 #include "stor-layout.h" 34 #include "expr.h" 35 #include "langhooks.h" 36 37 /* Macros to create an enumeration identifier for a function prototype. */ 38 #define RISCV_FTYPE_NAME1(A, B) RISCV_##A##_FTYPE_##B 39 40 /* Classifies the prototype of a built-in function. */ 41 enum riscv_function_type { 42 #define DEF_RISCV_FTYPE(NARGS, LIST) RISCV_FTYPE_NAME##NARGS LIST, 43 #include "config/riscv/riscv-ftypes.def" 44 #undef DEF_RISCV_FTYPE 45 RISCV_MAX_FTYPE_MAX 46 }; 47 48 /* Specifies how a built-in function should be converted into rtl. */ 49 enum riscv_builtin_type { 50 /* The function corresponds directly to an .md pattern. */ 51 RISCV_BUILTIN_DIRECT, 52 53 /* Likewise, but with return type VOID. */ 54 RISCV_BUILTIN_DIRECT_NO_TARGET 55 }; 56 57 /* Declare an availability predicate for built-in functions. */ 58 #define AVAIL(NAME, COND) \ 59 static unsigned int \ 60 riscv_builtin_avail_##NAME (void) \ 61 { \ 62 return (COND); \ 63 } 64 65 /* This structure describes a single built-in function. */ 66 struct riscv_builtin_description { 67 /* The code of the main .md file instruction. See riscv_builtin_type 68 for more information. */ 69 enum insn_code icode; 70 71 /* The name of the built-in function. */ 72 const char *name; 73 74 /* Specifies how the function should be expanded. */ 75 enum riscv_builtin_type builtin_type; 76 77 /* The function's prototype. */ 78 enum riscv_function_type prototype; 79 80 /* Whether the function is available. */ 81 unsigned int (*avail) (void); 82 }; 83 84 AVAIL (hard_float, TARGET_HARD_FLOAT) 85 86 /* Construct a riscv_builtin_description from the given arguments. 87 88 INSN is the name of the associated instruction pattern, without the 89 leading CODE_FOR_riscv_. 90 91 NAME is the name of the function itself, without the leading 92 "__builtin_riscv_". 93 94 BUILTIN_TYPE and FUNCTION_TYPE are riscv_builtin_description fields. 95 96 AVAIL is the name of the availability predicate, without the leading 97 riscv_builtin_avail_. */ 98 #define RISCV_BUILTIN(INSN, NAME, BUILTIN_TYPE, FUNCTION_TYPE, AVAIL) \ 99 { CODE_FOR_riscv_ ## INSN, "__builtin_riscv_" NAME, \ 100 BUILTIN_TYPE, FUNCTION_TYPE, riscv_builtin_avail_ ## AVAIL } 101 102 /* Define __builtin_riscv_<INSN>, which is a RISCV_BUILTIN_DIRECT function 103 mapped to instruction CODE_FOR_riscv_<INSN>, FUNCTION_TYPE and AVAIL 104 are as for RISCV_BUILTIN. */ 105 #define DIRECT_BUILTIN(INSN, FUNCTION_TYPE, AVAIL) \ 106 RISCV_BUILTIN (INSN, #INSN, RISCV_BUILTIN_DIRECT, FUNCTION_TYPE, AVAIL) 107 108 /* Define __builtin_riscv_<INSN>, which is a RISCV_BUILTIN_DIRECT_NO_TARGET 109 function mapped to instruction CODE_FOR_riscv_<INSN>, FUNCTION_TYPE 110 and AVAIL are as for RISCV_BUILTIN. */ 111 #define DIRECT_NO_TARGET_BUILTIN(INSN, FUNCTION_TYPE, AVAIL) \ 112 RISCV_BUILTIN (INSN, #INSN, RISCV_BUILTIN_DIRECT_NO_TARGET, \ 113 FUNCTION_TYPE, AVAIL) 114 115 /* Argument types. */ 116 #define RISCV_ATYPE_VOID void_type_node 117 #define RISCV_ATYPE_USI unsigned_intSI_type_node 118 119 /* RISCV_FTYPE_ATYPESN takes N RISCV_FTYPES-like type codes and lists 120 their associated RISCV_ATYPEs. */ 121 #define RISCV_FTYPE_ATYPES1(A, B) \ 122 RISCV_ATYPE_##A, RISCV_ATYPE_##B 123 124 static const struct riscv_builtin_description riscv_builtins[] = { 125 DIRECT_BUILTIN (frflags, RISCV_USI_FTYPE_VOID, hard_float), 126 DIRECT_NO_TARGET_BUILTIN (fsflags, RISCV_VOID_FTYPE_USI, hard_float) 127 }; 128 129 /* Index I is the function declaration for riscv_builtins[I], or null if the 130 function isn't defined on this target. */ 131 static GTY(()) tree riscv_builtin_decls[ARRAY_SIZE (riscv_builtins)]; 132 133 /* Get the index I of the function declaration for riscv_builtin_decls[I] 134 using the instruction code or return null if not defined for the target. */ 135 static GTY(()) int riscv_builtin_decl_index[NUM_INSN_CODES]; 136 137 #define GET_BUILTIN_DECL(CODE) \ 138 riscv_builtin_decls[riscv_builtin_decl_index[(CODE)]] 139 140 /* Return the function type associated with function prototype TYPE. */ 141 142 static tree 143 riscv_build_function_type (enum riscv_function_type type) 144 { 145 static tree types[(int) RISCV_MAX_FTYPE_MAX]; 146 147 if (types[(int) type] == NULL_TREE) 148 switch (type) 149 { 150 #define DEF_RISCV_FTYPE(NUM, ARGS) \ 151 case RISCV_FTYPE_NAME##NUM ARGS: \ 152 types[(int) type] \ 153 = build_function_type_list (RISCV_FTYPE_ATYPES##NUM ARGS, \ 154 NULL_TREE); \ 155 break; 156 #include "config/riscv/riscv-ftypes.def" 157 #undef DEF_RISCV_FTYPE 158 default: 159 gcc_unreachable (); 160 } 161 162 return types[(int) type]; 163 } 164 165 /* Implement TARGET_INIT_BUILTINS. */ 166 167 void 168 riscv_init_builtins (void) 169 { 170 for (size_t i = 0; i < ARRAY_SIZE (riscv_builtins); i++) 171 { 172 const struct riscv_builtin_description *d = &riscv_builtins[i]; 173 if (d->avail ()) 174 { 175 tree type = riscv_build_function_type (d->prototype); 176 riscv_builtin_decls[i] 177 = add_builtin_function (d->name, type, i, BUILT_IN_MD, NULL, NULL); 178 riscv_builtin_decl_index[d->icode] = i; 179 } 180 } 181 } 182 183 /* Implement TARGET_BUILTIN_DECL. */ 184 185 tree 186 riscv_builtin_decl (unsigned int code, bool initialize_p ATTRIBUTE_UNUSED) 187 { 188 if (code >= ARRAY_SIZE (riscv_builtins)) 189 return error_mark_node; 190 return riscv_builtin_decls[code]; 191 } 192 193 /* Take argument ARGNO from EXP's argument list and convert it into 194 an expand operand. Store the operand in *OP. */ 195 196 static void 197 riscv_prepare_builtin_arg (struct expand_operand *op, tree exp, unsigned argno) 198 { 199 tree arg = CALL_EXPR_ARG (exp, argno); 200 create_input_operand (op, expand_normal (arg), TYPE_MODE (TREE_TYPE (arg))); 201 } 202 203 /* Expand instruction ICODE as part of a built-in function sequence. 204 Use the first NOPS elements of OPS as the instruction's operands. 205 HAS_TARGET_P is true if operand 0 is a target; it is false if the 206 instruction has no target. 207 208 Return the target rtx if HAS_TARGET_P, otherwise return const0_rtx. */ 209 210 static rtx 211 riscv_expand_builtin_insn (enum insn_code icode, unsigned int n_ops, 212 struct expand_operand *ops, bool has_target_p) 213 { 214 if (!maybe_expand_insn (icode, n_ops, ops)) 215 { 216 error ("invalid argument to built-in function"); 217 return has_target_p ? gen_reg_rtx (ops[0].mode) : const0_rtx; 218 } 219 220 return has_target_p ? ops[0].value : const0_rtx; 221 } 222 223 /* Expand a RISCV_BUILTIN_DIRECT or RISCV_BUILTIN_DIRECT_NO_TARGET function; 224 HAS_TARGET_P says which. EXP is the CALL_EXPR that calls the function 225 and ICODE is the code of the associated .md pattern. TARGET, if nonnull, 226 suggests a good place to put the result. */ 227 228 static rtx 229 riscv_expand_builtin_direct (enum insn_code icode, rtx target, tree exp, 230 bool has_target_p) 231 { 232 struct expand_operand ops[MAX_RECOG_OPERANDS]; 233 234 /* Map any target to operand 0. */ 235 int opno = 0; 236 if (has_target_p) 237 create_output_operand (&ops[opno++], target, TYPE_MODE (TREE_TYPE (exp))); 238 239 /* Map the arguments to the other operands. */ 240 gcc_assert (opno + call_expr_nargs (exp) 241 == insn_data[icode].n_generator_args); 242 for (int argno = 0; argno < call_expr_nargs (exp); argno++) 243 riscv_prepare_builtin_arg (&ops[opno++], exp, argno); 244 245 return riscv_expand_builtin_insn (icode, opno, ops, has_target_p); 246 } 247 248 /* Implement TARGET_EXPAND_BUILTIN. */ 249 250 rtx 251 riscv_expand_builtin (tree exp, rtx target, rtx subtarget ATTRIBUTE_UNUSED, 252 machine_mode mode ATTRIBUTE_UNUSED, 253 int ignore ATTRIBUTE_UNUSED) 254 { 255 tree fndecl = TREE_OPERAND (CALL_EXPR_FN (exp), 0); 256 unsigned int fcode = DECL_FUNCTION_CODE (fndecl); 257 const struct riscv_builtin_description *d = &riscv_builtins[fcode]; 258 259 switch (d->builtin_type) 260 { 261 case RISCV_BUILTIN_DIRECT: 262 return riscv_expand_builtin_direct (d->icode, target, exp, true); 263 264 case RISCV_BUILTIN_DIRECT_NO_TARGET: 265 return riscv_expand_builtin_direct (d->icode, target, exp, false); 266 } 267 268 gcc_unreachable (); 269 } 270 271 /* Implement TARGET_ATOMIC_ASSIGN_EXPAND_FENV. */ 272 273 void 274 riscv_atomic_assign_expand_fenv (tree *hold, tree *clear, tree *update) 275 { 276 if (!TARGET_HARD_FLOAT) 277 return; 278 279 tree frflags = GET_BUILTIN_DECL (CODE_FOR_riscv_frflags); 280 tree fsflags = GET_BUILTIN_DECL (CODE_FOR_riscv_fsflags); 281 tree old_flags = create_tmp_var_raw (RISCV_ATYPE_USI); 282 283 *hold = build2 (MODIFY_EXPR, RISCV_ATYPE_USI, old_flags, 284 build_call_expr (frflags, 0)); 285 *clear = build_call_expr (fsflags, 1, old_flags); 286 *update = NULL_TREE; 287 } 288