Branch data Line data Source code
1 : : /* Internal functions.
2 : : Copyright (C) 2011-2025 Free Software Foundation, Inc.
3 : :
4 : : This file is part of GCC.
5 : :
6 : : GCC is free software; you can redistribute it and/or modify it under
7 : : the terms of the GNU General Public License as published by the Free
8 : : Software Foundation; either version 3, or (at your option) any later
9 : : version.
10 : :
11 : : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 : : WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 : : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 : : for more details.
15 : :
16 : : You should have received a copy of the GNU General Public License
17 : : along with GCC; see the file COPYING3. If not see
18 : : <http://www.gnu.org/licenses/>. */
19 : :
20 : : #include "config.h"
21 : : #define INCLUDE_MEMORY
22 : : #include "system.h"
23 : : #include "coretypes.h"
24 : : #include "backend.h"
25 : : #include "target.h"
26 : : #include "rtl.h"
27 : : #include "tree.h"
28 : : #include "gimple.h"
29 : : #include "predict.h"
30 : : #include "stringpool.h"
31 : : #include "tree-vrp.h"
32 : : #include "tree-ssanames.h"
33 : : #include "expmed.h"
34 : : #include "memmodel.h"
35 : : #include "optabs.h"
36 : : #include "emit-rtl.h"
37 : : #include "diagnostic-core.h"
38 : : #include "fold-const.h"
39 : : #include "internal-fn.h"
40 : : #include "stor-layout.h"
41 : : #include "dojump.h"
42 : : #include "expr.h"
43 : : #include "stringpool.h"
44 : : #include "attribs.h"
45 : : #include "asan.h"
46 : : #include "ubsan.h"
47 : : #include "recog.h"
48 : : #include "builtins.h"
49 : : #include "optabs-tree.h"
50 : : #include "gimple-ssa.h"
51 : : #include "tree-phinodes.h"
52 : : #include "ssa-iterators.h"
53 : : #include "explow.h"
54 : : #include "rtl-iter.h"
55 : : #include "gimple-range.h"
56 : : #include "fold-const-call.h"
57 : : #include "tree-ssa-live.h"
58 : : #include "tree-outof-ssa.h"
59 : : #include "gcc-urlifier.h"
60 : :
61 : : /* For lang_hooks.types.type_for_mode. */
62 : : #include "langhooks.h"
63 : :
64 : : /* The names of each internal function, indexed by function number. */
65 : : const char *const internal_fn_name_array[] = {
66 : : #define DEF_INTERNAL_FN(CODE, FLAGS, FNSPEC) #CODE,
67 : : #include "internal-fn.def"
68 : : "<invalid-fn>"
69 : : };
70 : :
71 : : /* The ECF_* flags of each internal function, indexed by function number. */
72 : : const int internal_fn_flags_array[] = {
73 : : #define DEF_INTERNAL_FN(CODE, FLAGS, FNSPEC) FLAGS,
74 : : #include "internal-fn.def"
75 : : 0
76 : : };
77 : :
78 : : /* Return the internal function called NAME, or IFN_LAST if there's
79 : : no such function. */
80 : :
81 : : internal_fn
82 : 3 : lookup_internal_fn (const char *name)
83 : : {
84 : 3 : typedef hash_map<nofree_string_hash, internal_fn> name_to_fn_map_type;
85 : 3 : static name_to_fn_map_type *name_to_fn_map;
86 : :
87 : 3 : if (!name_to_fn_map)
88 : : {
89 : 2 : name_to_fn_map = new name_to_fn_map_type (IFN_LAST);
90 : 556 : for (unsigned int i = 0; i < IFN_LAST; ++i)
91 : 1108 : name_to_fn_map->put (internal_fn_name (internal_fn (i)),
92 : 554 : internal_fn (i));
93 : : }
94 : 3 : internal_fn *entry = name_to_fn_map->get (name);
95 : 3 : return entry ? *entry : IFN_LAST;
96 : : }
97 : :
98 : : /* Geven an internal_fn IFN that is a widening function, return its
99 : : corresponding LO and HI internal_fns. */
100 : :
101 : : extern void
102 : 54701 : lookup_hilo_internal_fn (internal_fn ifn, internal_fn *lo, internal_fn *hi)
103 : : {
104 : 54701 : gcc_assert (widening_fn_p (ifn));
105 : :
106 : 54701 : switch (ifn)
107 : : {
108 : 0 : default:
109 : 0 : gcc_unreachable ();
110 : : #define DEF_INTERNAL_FN(NAME, FLAGS, TYPE)
111 : : #define DEF_INTERNAL_WIDENING_OPTAB_FN(NAME, F, S, SO, UO, T) \
112 : : case IFN_##NAME: \
113 : : *lo = internal_fn (IFN_##NAME##_LO); \
114 : : *hi = internal_fn (IFN_##NAME##_HI); \
115 : : break;
116 : : #include "internal-fn.def"
117 : : }
118 : 54701 : }
119 : :
120 : : /* Given an internal_fn IFN that is a widening function, return its
121 : : corresponding _EVEN and _ODD internal_fns in *EVEN and *ODD. */
122 : :
123 : : extern void
124 : 54701 : lookup_evenodd_internal_fn (internal_fn ifn, internal_fn *even,
125 : : internal_fn *odd)
126 : : {
127 : 54701 : gcc_assert (widening_fn_p (ifn));
128 : :
129 : 54701 : switch (ifn)
130 : : {
131 : 0 : default:
132 : 0 : gcc_unreachable ();
133 : : #define DEF_INTERNAL_FN(NAME, FLAGS, TYPE)
134 : : #define DEF_INTERNAL_WIDENING_OPTAB_FN(NAME, F, S, SO, UO, T) \
135 : : case IFN_##NAME: \
136 : : *even = internal_fn (IFN_##NAME##_EVEN); \
137 : : *odd = internal_fn (IFN_##NAME##_ODD); \
138 : : break;
139 : : #include "internal-fn.def"
140 : : }
141 : 54701 : }
142 : :
143 : :
144 : : /* Fnspec of each internal function, indexed by function number. */
145 : : const_tree internal_fn_fnspec_array[IFN_LAST + 1];
146 : :
147 : : void
148 : 284768 : init_internal_fns ()
149 : : {
150 : : #define DEF_INTERNAL_FN(CODE, FLAGS, FNSPEC) \
151 : : if (FNSPEC) internal_fn_fnspec_array[IFN_##CODE] = \
152 : : build_string ((int) sizeof (FNSPEC) - 1, FNSPEC ? FNSPEC : "");
153 : : #include "internal-fn.def"
154 : 284768 : internal_fn_fnspec_array[IFN_LAST] = 0;
155 : 284768 : }
156 : :
157 : : /* Create static initializers for the information returned by
158 : : direct_internal_fn. */
159 : : #define not_direct { -2, -2, false }
160 : : #define mask_load_direct { -1, 2, false }
161 : : #define load_lanes_direct { -1, -1, false }
162 : : #define mask_load_lanes_direct { -1, -1, false }
163 : : #define gather_load_direct { 3, 1, false }
164 : : #define strided_load_direct { -1, -1, false }
165 : : #define len_load_direct { -1, -1, false }
166 : : #define mask_len_load_direct { -1, 4, false }
167 : : #define mask_store_direct { 3, 2, false }
168 : : #define store_lanes_direct { 0, 0, false }
169 : : #define mask_store_lanes_direct { 0, 0, false }
170 : : #define vec_cond_mask_direct { 1, 0, false }
171 : : #define vec_cond_mask_len_direct { 1, 1, false }
172 : : #define vec_cond_direct { 2, 0, false }
173 : : #define scatter_store_direct { 3, 1, false }
174 : : #define strided_store_direct { 1, 1, false }
175 : : #define len_store_direct { 3, 3, false }
176 : : #define mask_len_store_direct { 4, 5, false }
177 : : #define vec_set_direct { 3, 3, false }
178 : : #define vec_extract_direct { 0, -1, false }
179 : : #define unary_direct { 0, 0, true }
180 : : #define unary_convert_direct { -1, 0, true }
181 : : #define binary_direct { 0, 0, true }
182 : : #define ternary_direct { 0, 0, true }
183 : : #define cond_unary_direct { 1, 1, true }
184 : : #define cond_binary_direct { 1, 1, true }
185 : : #define cond_ternary_direct { 1, 1, true }
186 : : #define cond_len_unary_direct { 1, 1, true }
187 : : #define cond_len_binary_direct { 1, 1, true }
188 : : #define cond_len_ternary_direct { 1, 1, true }
189 : : #define while_direct { 0, 2, false }
190 : : #define fold_extract_direct { 2, 2, false }
191 : : #define fold_len_extract_direct { 2, 2, false }
192 : : #define fold_left_direct { 1, 1, false }
193 : : #define mask_fold_left_direct { 1, 1, false }
194 : : #define mask_len_fold_left_direct { 1, 1, false }
195 : : #define check_ptrs_direct { 0, 0, false }
196 : : #define crc_direct { 1, -1, true }
197 : :
198 : : const direct_internal_fn_info direct_internal_fn_array[IFN_LAST + 1] = {
199 : : #define DEF_INTERNAL_FN(CODE, FLAGS, FNSPEC) not_direct,
200 : : #define DEF_INTERNAL_OPTAB_FN(CODE, FLAGS, OPTAB, TYPE) TYPE##_direct,
201 : : #define DEF_INTERNAL_SIGNED_OPTAB_FN(CODE, FLAGS, SELECTOR, SIGNED_OPTAB, \
202 : : UNSIGNED_OPTAB, TYPE) TYPE##_direct,
203 : : #include "internal-fn.def"
204 : : not_direct
205 : : };
206 : :
207 : : /* Like create_output_operand, but for callers that will use
208 : : assign_call_lhs afterwards. */
209 : :
210 : : static void
211 : 89084 : create_call_lhs_operand (expand_operand *op, rtx lhs_rtx, machine_mode mode)
212 : : {
213 : : /* Do not assign directly to a promoted subreg, since there is no
214 : : guarantee that the instruction will leave the upper bits of the
215 : : register in the state required by SUBREG_PROMOTED_SIGN. */
216 : 89084 : rtx dest = lhs_rtx;
217 : 89084 : if (dest && GET_CODE (dest) == SUBREG && SUBREG_PROMOTED_VAR_P (dest))
218 : : dest = NULL_RTX;
219 : 89084 : create_output_operand (op, dest, mode);
220 : 89084 : }
221 : :
222 : : /* Move the result of an expanded instruction into the lhs of a gimple call.
223 : : LHS is the lhs of the call, LHS_RTX is its expanded form, and OP is the
224 : : result of the expanded instruction. OP should have been set up by
225 : : create_call_lhs_operand. */
226 : :
227 : : static void
228 : 89084 : assign_call_lhs (tree lhs, rtx lhs_rtx, expand_operand *op)
229 : : {
230 : 89084 : if (rtx_equal_p (lhs_rtx, op->value))
231 : : return;
232 : :
233 : : /* If the return value has an integral type, convert the instruction
234 : : result to that type. This is useful for things that return an
235 : : int regardless of the size of the input. If the instruction result
236 : : is smaller than required, assume that it is signed.
237 : :
238 : : If the return value has a nonintegral type, its mode must match
239 : : the instruction result. */
240 : 6006 : if (GET_CODE (lhs_rtx) == SUBREG && SUBREG_PROMOTED_VAR_P (lhs_rtx))
241 : : {
242 : : /* If this is a scalar in a register that is stored in a wider
243 : : mode than the declared mode, compute the result into its
244 : : declared mode and then convert to the wider mode. */
245 : 0 : gcc_checking_assert (INTEGRAL_TYPE_P (TREE_TYPE (lhs)));
246 : 0 : rtx tmp = convert_to_mode (GET_MODE (lhs_rtx), op->value, 0);
247 : 0 : convert_move (SUBREG_REG (lhs_rtx), tmp,
248 : 0 : SUBREG_PROMOTED_SIGN (lhs_rtx));
249 : : }
250 : 6006 : else if (GET_MODE (lhs_rtx) == GET_MODE (op->value))
251 : 39 : emit_move_insn (lhs_rtx, op->value);
252 : : else
253 : : {
254 : 5967 : gcc_checking_assert (INTEGRAL_TYPE_P (TREE_TYPE (lhs)));
255 : 5967 : convert_move (lhs_rtx, op->value, 0);
256 : : }
257 : : }
258 : :
259 : : /* Expand STMT using instruction ICODE. The instruction has NOUTPUTS
260 : : output operands and NINPUTS input operands, where NOUTPUTS is either
261 : : 0 or 1. The output operand (if any) comes first, followed by the
262 : : NINPUTS input operands. */
263 : :
264 : : static void
265 : 81601 : expand_fn_using_insn (gcall *stmt, insn_code icode, unsigned int noutputs,
266 : : unsigned int ninputs)
267 : : {
268 : 81601 : gcc_assert (icode != CODE_FOR_nothing);
269 : :
270 : 81601 : expand_operand *ops = XALLOCAVEC (expand_operand, noutputs + ninputs);
271 : 81601 : unsigned int opno = 0;
272 : 81601 : rtx lhs_rtx = NULL_RTX;
273 : 81601 : tree lhs = gimple_call_lhs (stmt);
274 : :
275 : 81601 : if (noutputs)
276 : : {
277 : 81601 : gcc_assert (noutputs == 1);
278 : 81601 : if (lhs)
279 : 81601 : lhs_rtx = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
280 : 81601 : create_call_lhs_operand (&ops[opno], lhs_rtx,
281 : 81601 : insn_data[icode].operand[opno].mode);
282 : 81601 : opno += 1;
283 : : }
284 : : else
285 : 0 : gcc_assert (!lhs);
286 : :
287 : 228201 : for (unsigned int i = 0; i < ninputs; ++i)
288 : : {
289 : 146600 : tree rhs = gimple_call_arg (stmt, i);
290 : 146600 : tree rhs_type = TREE_TYPE (rhs);
291 : 146600 : rtx rhs_rtx = expand_normal (rhs);
292 : 146600 : if (INTEGRAL_TYPE_P (rhs_type))
293 : 10200 : create_convert_operand_from (&ops[opno], rhs_rtx,
294 : 10200 : TYPE_MODE (rhs_type),
295 : 10200 : TYPE_UNSIGNED (rhs_type));
296 : 136400 : else if (TREE_CODE (rhs) == SSA_NAME
297 : 112158 : && SSA_NAME_IS_DEFAULT_DEF (rhs)
298 : 141071 : && VAR_P (SSA_NAME_VAR (rhs)))
299 : 333 : create_undefined_input_operand (&ops[opno], TYPE_MODE (rhs_type));
300 : 48915 : else if (VECTOR_BOOLEAN_TYPE_P (rhs_type)
301 : 1177 : && SCALAR_INT_MODE_P (TYPE_MODE (rhs_type))
302 : 137244 : && maybe_ne (GET_MODE_PRECISION (TYPE_MODE (rhs_type)),
303 : 1177 : TYPE_VECTOR_SUBPARTS (rhs_type).to_constant ()))
304 : : {
305 : : /* Ensure that the vector bitmasks do not have excess bits. */
306 : 223 : int nunits = TYPE_VECTOR_SUBPARTS (rhs_type).to_constant ();
307 : 223 : rtx tmp = expand_binop (TYPE_MODE (rhs_type), and_optab, rhs_rtx,
308 : 223 : GEN_INT ((HOST_WIDE_INT_1U << nunits) - 1),
309 : : NULL_RTX, true, OPTAB_WIDEN);
310 : 223 : create_input_operand (&ops[opno], tmp, TYPE_MODE (rhs_type));
311 : : }
312 : : else
313 : 135844 : create_input_operand (&ops[opno], rhs_rtx, TYPE_MODE (rhs_type));
314 : 146600 : opno += 1;
315 : : }
316 : :
317 : 81601 : gcc_assert (opno == noutputs + ninputs);
318 : 81601 : expand_insn (icode, opno, ops);
319 : 81601 : if (lhs_rtx)
320 : 81601 : assign_call_lhs (lhs, lhs_rtx, &ops[0]);
321 : 81601 : }
322 : :
323 : : /* ARRAY_TYPE is an array of vector modes. Return the associated insn
324 : : for load-lanes-style optab OPTAB, or CODE_FOR_nothing if none. */
325 : :
326 : : static enum insn_code
327 : 0 : get_multi_vector_move (tree array_type, convert_optab optab)
328 : : {
329 : 0 : machine_mode imode;
330 : 0 : machine_mode vmode;
331 : :
332 : 0 : gcc_assert (TREE_CODE (array_type) == ARRAY_TYPE);
333 : 0 : imode = TYPE_MODE (array_type);
334 : 0 : vmode = TYPE_MODE (TREE_TYPE (array_type));
335 : :
336 : 0 : return convert_optab_handler (optab, imode, vmode);
337 : : }
338 : :
339 : : /* Add mask, else, and len arguments according to the STMT. */
340 : :
341 : : static unsigned int
342 : 1067 : add_mask_else_and_len_args (expand_operand *ops, unsigned int opno, gcall *stmt)
343 : : {
344 : 1067 : internal_fn ifn = gimple_call_internal_fn (stmt);
345 : 1067 : int len_index = internal_fn_len_index (ifn);
346 : : /* BIAS is always consecutive next of LEN. */
347 : 1067 : int bias_index = len_index + 1;
348 : 1067 : int mask_index = internal_fn_mask_index (ifn);
349 : :
350 : : /* The order of arguments is always {mask, else, len, bias}. */
351 : 1067 : if (mask_index >= 0)
352 : : {
353 : 1067 : tree mask = gimple_call_arg (stmt, mask_index);
354 : 1067 : rtx mask_rtx = expand_normal (mask);
355 : :
356 : 1067 : tree mask_type = TREE_TYPE (mask);
357 : 1067 : if (VECTOR_BOOLEAN_TYPE_P (mask_type)
358 : 1067 : && SCALAR_INT_MODE_P (TYPE_MODE (mask_type))
359 : 1847 : && maybe_ne (GET_MODE_PRECISION (TYPE_MODE (mask_type)),
360 : 1654 : TYPE_VECTOR_SUBPARTS (mask_type).to_constant ()))
361 : : {
362 : : /* Ensure that the vector bitmasks do not have excess bits. */
363 : 193 : int nunits = TYPE_VECTOR_SUBPARTS (mask_type).to_constant ();
364 : 193 : mask_rtx = expand_binop (TYPE_MODE (mask_type), and_optab, mask_rtx,
365 : 193 : GEN_INT ((HOST_WIDE_INT_1U << nunits) - 1),
366 : : NULL_RTX, true, OPTAB_WIDEN);
367 : : }
368 : :
369 : 1067 : create_input_operand (&ops[opno++], mask_rtx,
370 : 1067 : TYPE_MODE (TREE_TYPE (mask)));
371 : : }
372 : :
373 : 1067 : int els_index = internal_fn_else_index (ifn);
374 : 1067 : if (els_index >= 0)
375 : : {
376 : 541 : tree els = gimple_call_arg (stmt, els_index);
377 : 541 : tree els_type = TREE_TYPE (els);
378 : 541 : if (TREE_CODE (els) == SSA_NAME
379 : 0 : && SSA_NAME_IS_DEFAULT_DEF (els)
380 : 541 : && VAR_P (SSA_NAME_VAR (els)))
381 : 0 : create_undefined_input_operand (&ops[opno++], TYPE_MODE (els_type));
382 : : else
383 : : {
384 : 541 : rtx els_rtx = expand_normal (els);
385 : 541 : create_input_operand (&ops[opno++], els_rtx, TYPE_MODE (els_type));
386 : : }
387 : : }
388 : 1067 : if (len_index >= 0)
389 : : {
390 : 0 : tree len = gimple_call_arg (stmt, len_index);
391 : 0 : rtx len_rtx = expand_normal (len);
392 : 0 : create_convert_operand_from (&ops[opno++], len_rtx,
393 : 0 : TYPE_MODE (TREE_TYPE (len)),
394 : 0 : TYPE_UNSIGNED (TREE_TYPE (len)));
395 : 0 : tree biast = gimple_call_arg (stmt, bias_index);
396 : 0 : rtx bias = expand_normal (biast);
397 : 0 : create_input_operand (&ops[opno++], bias, QImode);
398 : : }
399 : 1067 : return opno;
400 : : }
401 : :
402 : : /* Expand LOAD_LANES call STMT using optab OPTAB. */
403 : :
404 : : static void
405 : 0 : expand_load_lanes_optab_fn (internal_fn, gcall *stmt, convert_optab optab)
406 : : {
407 : 0 : class expand_operand ops[2];
408 : 0 : tree type, lhs, rhs;
409 : 0 : rtx target, mem;
410 : :
411 : 0 : lhs = gimple_call_lhs (stmt);
412 : 0 : rhs = gimple_call_arg (stmt, 0);
413 : 0 : type = TREE_TYPE (lhs);
414 : :
415 : 0 : target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
416 : 0 : mem = expand_normal (rhs);
417 : :
418 : 0 : gcc_assert (MEM_P (mem));
419 : 0 : PUT_MODE (mem, TYPE_MODE (type));
420 : :
421 : 0 : create_call_lhs_operand (&ops[0], target, TYPE_MODE (type));
422 : 0 : create_fixed_operand (&ops[1], mem);
423 : 0 : expand_insn (get_multi_vector_move (type, optab), 2, ops);
424 : 0 : assign_call_lhs (lhs, target, &ops[0]);
425 : 0 : }
426 : :
427 : : /* Expand STORE_LANES call STMT using optab OPTAB. */
428 : :
429 : : static void
430 : 0 : expand_store_lanes_optab_fn (internal_fn, gcall *stmt, convert_optab optab)
431 : : {
432 : 0 : class expand_operand ops[2];
433 : 0 : tree type, lhs, rhs;
434 : 0 : rtx target, reg;
435 : :
436 : 0 : lhs = gimple_call_lhs (stmt);
437 : 0 : rhs = gimple_call_arg (stmt, 0);
438 : 0 : type = TREE_TYPE (rhs);
439 : :
440 : 0 : target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
441 : 0 : reg = expand_normal (rhs);
442 : :
443 : 0 : gcc_assert (MEM_P (target));
444 : 0 : PUT_MODE (target, TYPE_MODE (type));
445 : :
446 : 0 : create_fixed_operand (&ops[0], target);
447 : 0 : create_input_operand (&ops[1], reg, TYPE_MODE (type));
448 : 0 : expand_insn (get_multi_vector_move (type, optab), 2, ops);
449 : 0 : }
450 : :
451 : : static void
452 : 0 : expand_ANNOTATE (internal_fn, gcall *)
453 : : {
454 : 0 : gcc_unreachable ();
455 : : }
456 : :
457 : : /* This should get expanded in omp_device_lower pass. */
458 : :
459 : : static void
460 : 0 : expand_GOMP_USE_SIMT (internal_fn, gcall *)
461 : : {
462 : 0 : gcc_unreachable ();
463 : : }
464 : :
465 : : /* This should get expanded in omp_device_lower pass. */
466 : :
467 : : static void
468 : 0 : expand_GOMP_SIMT_ENTER (internal_fn, gcall *)
469 : : {
470 : 0 : gcc_unreachable ();
471 : : }
472 : :
473 : : /* Allocate per-lane storage and begin non-uniform execution region. */
474 : :
475 : : static void
476 : 0 : expand_GOMP_SIMT_ENTER_ALLOC (internal_fn, gcall *stmt)
477 : : {
478 : 0 : rtx target;
479 : 0 : tree lhs = gimple_call_lhs (stmt);
480 : 0 : if (lhs)
481 : 0 : target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
482 : : else
483 : 0 : target = gen_reg_rtx (Pmode);
484 : 0 : rtx size = expand_normal (gimple_call_arg (stmt, 0));
485 : 0 : rtx align = expand_normal (gimple_call_arg (stmt, 1));
486 : 0 : class expand_operand ops[3];
487 : 0 : create_call_lhs_operand (&ops[0], target, Pmode);
488 : 0 : create_input_operand (&ops[1], size, Pmode);
489 : 0 : create_input_operand (&ops[2], align, Pmode);
490 : 0 : gcc_assert (targetm.have_omp_simt_enter ());
491 : 0 : expand_insn (targetm.code_for_omp_simt_enter, 3, ops);
492 : 0 : assign_call_lhs (lhs, target, &ops[0]);
493 : 0 : }
494 : :
495 : : /* Deallocate per-lane storage and leave non-uniform execution region. */
496 : :
497 : : static void
498 : 0 : expand_GOMP_SIMT_EXIT (internal_fn, gcall *stmt)
499 : : {
500 : 0 : gcc_checking_assert (!gimple_call_lhs (stmt));
501 : 0 : rtx arg = expand_normal (gimple_call_arg (stmt, 0));
502 : 0 : class expand_operand ops[1];
503 : 0 : create_input_operand (&ops[0], arg, Pmode);
504 : 0 : gcc_assert (targetm.have_omp_simt_exit ());
505 : 0 : expand_insn (targetm.code_for_omp_simt_exit, 1, ops);
506 : 0 : }
507 : :
508 : : /* Lane index on SIMT targets: thread index in the warp on NVPTX. On targets
509 : : without SIMT execution this should be expanded in omp_device_lower pass. */
510 : :
511 : : static void
512 : 0 : expand_GOMP_SIMT_LANE (internal_fn, gcall *stmt)
513 : : {
514 : 0 : tree lhs = gimple_call_lhs (stmt);
515 : 0 : if (!lhs)
516 : : return;
517 : :
518 : 0 : rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
519 : 0 : gcc_assert (targetm.have_omp_simt_lane ());
520 : 0 : emit_insn (targetm.gen_omp_simt_lane (target));
521 : : }
522 : :
523 : : /* This should get expanded in omp_device_lower pass. */
524 : :
525 : : static void
526 : 0 : expand_GOMP_SIMT_VF (internal_fn, gcall *)
527 : : {
528 : 0 : gcc_unreachable ();
529 : : }
530 : :
531 : : /* This should get expanded in omp_device_lower pass. */
532 : :
533 : : static void
534 : 0 : expand_GOMP_MAX_VF (internal_fn, gcall *)
535 : : {
536 : 0 : gcc_unreachable ();
537 : : }
538 : :
539 : : /* This should get expanded in omp_device_lower pass. */
540 : :
541 : : static void
542 : 0 : expand_GOMP_TARGET_REV (internal_fn, gcall *)
543 : : {
544 : 0 : gcc_unreachable ();
545 : : }
546 : :
547 : : /* Lane index of the first SIMT lane that supplies a non-zero argument.
548 : : This is a SIMT counterpart to GOMP_SIMD_LAST_LANE, used to represent the
549 : : lane that executed the last iteration for handling OpenMP lastprivate. */
550 : :
551 : : static void
552 : 0 : expand_GOMP_SIMT_LAST_LANE (internal_fn, gcall *stmt)
553 : : {
554 : 0 : tree lhs = gimple_call_lhs (stmt);
555 : 0 : if (!lhs)
556 : 0 : return;
557 : :
558 : 0 : rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
559 : 0 : rtx cond = expand_normal (gimple_call_arg (stmt, 0));
560 : 0 : machine_mode mode = TYPE_MODE (TREE_TYPE (lhs));
561 : 0 : class expand_operand ops[2];
562 : 0 : create_call_lhs_operand (&ops[0], target, mode);
563 : 0 : create_input_operand (&ops[1], cond, mode);
564 : 0 : gcc_assert (targetm.have_omp_simt_last_lane ());
565 : 0 : expand_insn (targetm.code_for_omp_simt_last_lane, 2, ops);
566 : 0 : assign_call_lhs (lhs, target, &ops[0]);
567 : : }
568 : :
569 : : /* Non-transparent predicate used in SIMT lowering of OpenMP "ordered". */
570 : :
571 : : static void
572 : 0 : expand_GOMP_SIMT_ORDERED_PRED (internal_fn, gcall *stmt)
573 : : {
574 : 0 : tree lhs = gimple_call_lhs (stmt);
575 : 0 : if (!lhs)
576 : 0 : return;
577 : :
578 : 0 : rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
579 : 0 : rtx ctr = expand_normal (gimple_call_arg (stmt, 0));
580 : 0 : machine_mode mode = TYPE_MODE (TREE_TYPE (lhs));
581 : 0 : class expand_operand ops[2];
582 : 0 : create_call_lhs_operand (&ops[0], target, mode);
583 : 0 : create_input_operand (&ops[1], ctr, mode);
584 : 0 : gcc_assert (targetm.have_omp_simt_ordered ());
585 : 0 : expand_insn (targetm.code_for_omp_simt_ordered, 2, ops);
586 : 0 : assign_call_lhs (lhs, target, &ops[0]);
587 : : }
588 : :
589 : : /* "Or" boolean reduction across SIMT lanes: return non-zero in all lanes if
590 : : any lane supplies a non-zero argument. */
591 : :
592 : : static void
593 : 0 : expand_GOMP_SIMT_VOTE_ANY (internal_fn, gcall *stmt)
594 : : {
595 : 0 : tree lhs = gimple_call_lhs (stmt);
596 : 0 : if (!lhs)
597 : 0 : return;
598 : :
599 : 0 : rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
600 : 0 : rtx cond = expand_normal (gimple_call_arg (stmt, 0));
601 : 0 : machine_mode mode = TYPE_MODE (TREE_TYPE (lhs));
602 : 0 : class expand_operand ops[2];
603 : 0 : create_call_lhs_operand (&ops[0], target, mode);
604 : 0 : create_input_operand (&ops[1], cond, mode);
605 : 0 : gcc_assert (targetm.have_omp_simt_vote_any ());
606 : 0 : expand_insn (targetm.code_for_omp_simt_vote_any, 2, ops);
607 : 0 : assign_call_lhs (lhs, target, &ops[0]);
608 : : }
609 : :
610 : : /* Exchange between SIMT lanes with a "butterfly" pattern: source lane index
611 : : is destination lane index XOR given offset. */
612 : :
613 : : static void
614 : 0 : expand_GOMP_SIMT_XCHG_BFLY (internal_fn, gcall *stmt)
615 : : {
616 : 0 : tree lhs = gimple_call_lhs (stmt);
617 : 0 : if (!lhs)
618 : 0 : return;
619 : :
620 : 0 : rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
621 : 0 : rtx src = expand_normal (gimple_call_arg (stmt, 0));
622 : 0 : rtx idx = expand_normal (gimple_call_arg (stmt, 1));
623 : 0 : machine_mode mode = TYPE_MODE (TREE_TYPE (lhs));
624 : 0 : class expand_operand ops[3];
625 : 0 : create_call_lhs_operand (&ops[0], target, mode);
626 : 0 : create_input_operand (&ops[1], src, mode);
627 : 0 : create_input_operand (&ops[2], idx, SImode);
628 : 0 : gcc_assert (targetm.have_omp_simt_xchg_bfly ());
629 : 0 : expand_insn (targetm.code_for_omp_simt_xchg_bfly, 3, ops);
630 : 0 : assign_call_lhs (lhs, target, &ops[0]);
631 : : }
632 : :
633 : : /* Exchange between SIMT lanes according to given source lane index. */
634 : :
635 : : static void
636 : 0 : expand_GOMP_SIMT_XCHG_IDX (internal_fn, gcall *stmt)
637 : : {
638 : 0 : tree lhs = gimple_call_lhs (stmt);
639 : 0 : if (!lhs)
640 : 0 : return;
641 : :
642 : 0 : rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
643 : 0 : rtx src = expand_normal (gimple_call_arg (stmt, 0));
644 : 0 : rtx idx = expand_normal (gimple_call_arg (stmt, 1));
645 : 0 : machine_mode mode = TYPE_MODE (TREE_TYPE (lhs));
646 : 0 : class expand_operand ops[3];
647 : 0 : create_call_lhs_operand (&ops[0], target, mode);
648 : 0 : create_input_operand (&ops[1], src, mode);
649 : 0 : create_input_operand (&ops[2], idx, SImode);
650 : 0 : gcc_assert (targetm.have_omp_simt_xchg_idx ());
651 : 0 : expand_insn (targetm.code_for_omp_simt_xchg_idx, 3, ops);
652 : 0 : assign_call_lhs (lhs, target, &ops[0]);
653 : : }
654 : :
655 : : /* This should get expanded in adjust_simduid_builtins. */
656 : :
657 : : static void
658 : 0 : expand_GOMP_SIMD_LANE (internal_fn, gcall *)
659 : : {
660 : 0 : gcc_unreachable ();
661 : : }
662 : :
663 : : /* This should get expanded in adjust_simduid_builtins. */
664 : :
665 : : static void
666 : 0 : expand_GOMP_SIMD_VF (internal_fn, gcall *)
667 : : {
668 : 0 : gcc_unreachable ();
669 : : }
670 : :
671 : : /* This should get expanded in adjust_simduid_builtins. */
672 : :
673 : : static void
674 : 0 : expand_GOMP_SIMD_LAST_LANE (internal_fn, gcall *)
675 : : {
676 : 0 : gcc_unreachable ();
677 : : }
678 : :
679 : : /* This should get expanded in adjust_simduid_builtins. */
680 : :
681 : : static void
682 : 0 : expand_GOMP_SIMD_ORDERED_START (internal_fn, gcall *)
683 : : {
684 : 0 : gcc_unreachable ();
685 : : }
686 : :
687 : : /* This should get expanded in adjust_simduid_builtins. */
688 : :
689 : : static void
690 : 0 : expand_GOMP_SIMD_ORDERED_END (internal_fn, gcall *)
691 : : {
692 : 0 : gcc_unreachable ();
693 : : }
694 : :
695 : : /* This should get expanded in gimplify_omp_dispatch. */
696 : :
697 : : static void
698 : 0 : expand_GOMP_DISPATCH (internal_fn, gcall *)
699 : : {
700 : 0 : gcc_unreachable ();
701 : : }
702 : :
703 : : /* This should get expanded in the sanopt pass. */
704 : :
705 : : static void
706 : 0 : expand_UBSAN_NULL (internal_fn, gcall *)
707 : : {
708 : 0 : gcc_unreachable ();
709 : : }
710 : :
711 : : /* This should get expanded in the sanopt pass. */
712 : :
713 : : static void
714 : 0 : expand_UBSAN_BOUNDS (internal_fn, gcall *)
715 : : {
716 : 0 : gcc_unreachable ();
717 : : }
718 : :
719 : : /* This should get expanded in the sanopt pass. */
720 : :
721 : : static void
722 : 0 : expand_UBSAN_VPTR (internal_fn, gcall *)
723 : : {
724 : 0 : gcc_unreachable ();
725 : : }
726 : :
727 : : /* This should get expanded in the sanopt pass. */
728 : :
729 : : static void
730 : 0 : expand_UBSAN_PTR (internal_fn, gcall *)
731 : : {
732 : 0 : gcc_unreachable ();
733 : : }
734 : :
735 : : /* This should get expanded in the sanopt pass. */
736 : :
737 : : static void
738 : 0 : expand_UBSAN_OBJECT_SIZE (internal_fn, gcall *)
739 : : {
740 : 0 : gcc_unreachable ();
741 : : }
742 : :
743 : : /* This should get expanded in the sanopt pass. */
744 : :
745 : : static void
746 : 0 : expand_HWASAN_CHECK (internal_fn, gcall *)
747 : : {
748 : 0 : gcc_unreachable ();
749 : : }
750 : :
751 : : /* For hwasan stack tagging:
752 : : Clear tags on the dynamically allocated space.
753 : : For use after an object dynamically allocated on the stack goes out of
754 : : scope. */
755 : : static void
756 : 0 : expand_HWASAN_ALLOCA_UNPOISON (internal_fn, gcall *gc)
757 : : {
758 : 0 : gcc_assert (Pmode == ptr_mode);
759 : 0 : tree restored_position = gimple_call_arg (gc, 0);
760 : 0 : rtx restored_rtx = expand_expr (restored_position, NULL_RTX, VOIDmode,
761 : : EXPAND_NORMAL);
762 : 0 : rtx func = init_one_libfunc ("__hwasan_tag_memory");
763 : 0 : rtx off = expand_simple_binop (Pmode, MINUS, restored_rtx,
764 : : stack_pointer_rtx, NULL_RTX, 0,
765 : : OPTAB_WIDEN);
766 : 0 : emit_library_call_value (func, NULL_RTX, LCT_NORMAL, VOIDmode,
767 : 0 : virtual_stack_dynamic_rtx, Pmode,
768 : : HWASAN_STACK_BACKGROUND, QImode,
769 : 0 : off, Pmode);
770 : 0 : }
771 : :
772 : : /* For hwasan stack tagging:
773 : : Return a tag to be used for a dynamic allocation. */
774 : : static void
775 : 0 : expand_HWASAN_CHOOSE_TAG (internal_fn, gcall *gc)
776 : : {
777 : 0 : tree tag = gimple_call_lhs (gc);
778 : 0 : rtx target = expand_expr (tag, NULL_RTX, VOIDmode, EXPAND_NORMAL);
779 : 0 : machine_mode mode = GET_MODE (target);
780 : 0 : gcc_assert (mode == QImode);
781 : :
782 : 0 : rtx base_tag = targetm.memtag.extract_tag (hwasan_frame_base (), NULL_RTX);
783 : 0 : gcc_assert (base_tag);
784 : 0 : rtx tag_offset = gen_int_mode (hwasan_current_frame_tag (), QImode);
785 : 0 : rtx chosen_tag = expand_simple_binop (QImode, PLUS, base_tag, tag_offset,
786 : : target, /* unsignedp = */1,
787 : : OPTAB_WIDEN);
788 : 0 : chosen_tag = hwasan_truncate_to_tag_size (chosen_tag, target);
789 : :
790 : : /* Really need to put the tag into the `target` RTX. */
791 : 0 : if (chosen_tag != target)
792 : : {
793 : 0 : rtx temp = chosen_tag;
794 : 0 : gcc_assert (GET_MODE (chosen_tag) == mode);
795 : 0 : emit_move_insn (target, temp);
796 : : }
797 : :
798 : 0 : hwasan_increment_frame_tag ();
799 : 0 : }
800 : :
801 : : /* For hwasan stack tagging:
802 : : Tag a region of space in the shadow stack according to the base pointer of
803 : : an object on the stack. N.b. the length provided in the internal call is
804 : : required to be aligned to HWASAN_TAG_GRANULE_SIZE. */
805 : : static void
806 : 0 : expand_HWASAN_MARK (internal_fn, gcall *gc)
807 : : {
808 : 0 : gcc_assert (ptr_mode == Pmode);
809 : 0 : HOST_WIDE_INT flag = tree_to_shwi (gimple_call_arg (gc, 0));
810 : 0 : bool is_poison = ((asan_mark_flags)flag) == ASAN_MARK_POISON;
811 : :
812 : 0 : tree base = gimple_call_arg (gc, 1);
813 : 0 : gcc_checking_assert (TREE_CODE (base) == ADDR_EXPR);
814 : 0 : rtx base_rtx = expand_normal (base);
815 : :
816 : 0 : rtx tag = is_poison ? HWASAN_STACK_BACKGROUND
817 : 0 : : targetm.memtag.extract_tag (base_rtx, NULL_RTX);
818 : 0 : rtx address = targetm.memtag.untagged_pointer (base_rtx, NULL_RTX);
819 : :
820 : 0 : tree len = gimple_call_arg (gc, 2);
821 : 0 : rtx r_len = expand_normal (len);
822 : :
823 : 0 : rtx func = init_one_libfunc ("__hwasan_tag_memory");
824 : 0 : emit_library_call (func, LCT_NORMAL, VOIDmode, address, Pmode,
825 : 0 : tag, QImode, r_len, Pmode);
826 : 0 : }
827 : :
828 : : /* For hwasan stack tagging:
829 : : Store a tag into a pointer. */
830 : : static void
831 : 0 : expand_HWASAN_SET_TAG (internal_fn, gcall *gc)
832 : : {
833 : 0 : gcc_assert (ptr_mode == Pmode);
834 : 0 : tree g_target = gimple_call_lhs (gc);
835 : 0 : tree g_ptr = gimple_call_arg (gc, 0);
836 : 0 : tree g_tag = gimple_call_arg (gc, 1);
837 : :
838 : 0 : rtx ptr = expand_normal (g_ptr);
839 : 0 : rtx tag = expand_expr (g_tag, NULL_RTX, QImode, EXPAND_NORMAL);
840 : 0 : rtx target = expand_normal (g_target);
841 : :
842 : 0 : rtx untagged = targetm.memtag.untagged_pointer (ptr, target);
843 : 0 : rtx tagged_value = targetm.memtag.set_tag (untagged, tag, target);
844 : 0 : if (tagged_value != target)
845 : 0 : emit_move_insn (target, tagged_value);
846 : 0 : }
847 : :
848 : : /* This should get expanded in the sanopt pass. */
849 : :
850 : : static void
851 : 0 : expand_ASAN_CHECK (internal_fn, gcall *)
852 : : {
853 : 0 : gcc_unreachable ();
854 : : }
855 : :
856 : : /* This should get expanded in the sanopt pass. */
857 : :
858 : : static void
859 : 0 : expand_ASAN_MARK (internal_fn, gcall *)
860 : : {
861 : 0 : gcc_unreachable ();
862 : : }
863 : :
864 : : /* This should get expanded in the sanopt pass. */
865 : :
866 : : static void
867 : 0 : expand_ASAN_POISON (internal_fn, gcall *)
868 : : {
869 : 0 : gcc_unreachable ();
870 : : }
871 : :
872 : : /* This should get expanded in the sanopt pass. */
873 : :
874 : : static void
875 : 0 : expand_ASAN_POISON_USE (internal_fn, gcall *)
876 : : {
877 : 0 : gcc_unreachable ();
878 : : }
879 : :
880 : : /* This should get expanded in the tsan pass. */
881 : :
882 : : static void
883 : 0 : expand_TSAN_FUNC_EXIT (internal_fn, gcall *)
884 : : {
885 : 0 : gcc_unreachable ();
886 : : }
887 : :
888 : : /* This should get expanded in the lower pass. */
889 : :
890 : : static void
891 : 25 : expand_FALLTHROUGH (internal_fn, gcall *call)
892 : : {
893 : 25 : auto_urlify_attributes sentinel;
894 : 25 : error_at (gimple_location (call),
895 : : "invalid use of attribute %<fallthrough%>");
896 : 25 : }
897 : :
898 : : /* Return minimum precision needed to represent all values
899 : : of ARG in SIGNed integral type. */
900 : :
901 : : static int
902 : 156730 : get_min_precision (tree arg, signop sign)
903 : : {
904 : 156730 : int prec = TYPE_PRECISION (TREE_TYPE (arg));
905 : 156730 : int cnt = 0;
906 : 156730 : signop orig_sign = sign;
907 : 156730 : if (TREE_CODE (arg) == INTEGER_CST)
908 : : {
909 : 40768 : int p;
910 : 40768 : if (TYPE_SIGN (TREE_TYPE (arg)) != sign)
911 : : {
912 : 14785 : widest_int w = wi::to_widest (arg);
913 : 14785 : w = wi::ext (w, prec, sign);
914 : 14785 : p = wi::min_precision (w, sign);
915 : 14785 : }
916 : : else
917 : 25983 : p = wi::min_precision (wi::to_wide (arg), sign);
918 : 40768 : return MIN (p, prec);
919 : : }
920 : 115902 : while (CONVERT_EXPR_P (arg)
921 : 6561 : && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg, 0)))
922 : 129024 : && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg, 0))) <= prec)
923 : : {
924 : 6501 : arg = TREE_OPERAND (arg, 0);
925 : 6501 : if (TYPE_PRECISION (TREE_TYPE (arg)) < prec)
926 : : {
927 : 340 : if (TYPE_UNSIGNED (TREE_TYPE (arg)))
928 : : sign = UNSIGNED;
929 : 319 : else if (sign == UNSIGNED
930 : 319 : && (get_range_pos_neg (arg,
931 : : currently_expanding_gimple_stmt)
932 : : != 1))
933 : 0 : return prec + (orig_sign != sign);
934 : 340 : prec = TYPE_PRECISION (TREE_TYPE (arg));
935 : : }
936 : 6501 : if (++cnt > 30)
937 : 0 : return prec + (orig_sign != sign);
938 : : }
939 : 115902 : if (CONVERT_EXPR_P (arg)
940 : 60 : && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg, 0)))
941 : 116022 : && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg, 0))) > prec)
942 : : {
943 : : /* We have e.g. (unsigned short) y_2 where int y_2 = (int) x_1(D);
944 : : If y_2's min precision is smaller than prec, return that. */
945 : 60 : int oprec = get_min_precision (TREE_OPERAND (arg, 0), sign);
946 : 60 : if (oprec < prec)
947 : 60 : return oprec + (orig_sign != sign);
948 : : }
949 : 115902 : if (TREE_CODE (arg) != SSA_NAME)
950 : 0 : return prec + (orig_sign != sign);
951 : 115902 : int_range_max r;
952 : 115902 : gimple *cg = currently_expanding_gimple_stmt;
953 : 232043 : while (!get_range_query (cfun)->range_of_expr (r, arg, cg)
954 : 116141 : || r.varying_p ()
955 : 259456 : || r.undefined_p ())
956 : : {
957 : 88968 : gimple *g = SSA_NAME_DEF_STMT (arg);
958 : 88968 : if (is_gimple_assign (g)
959 : 88968 : && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (g)))
960 : : {
961 : 1181 : tree t = gimple_assign_rhs1 (g);
962 : 2362 : if (INTEGRAL_TYPE_P (TREE_TYPE (t))
963 : 1440 : && TYPE_PRECISION (TREE_TYPE (t)) <= prec)
964 : : {
965 : 239 : arg = t;
966 : 239 : if (TYPE_PRECISION (TREE_TYPE (arg)) < prec)
967 : : {
968 : 0 : if (TYPE_UNSIGNED (TREE_TYPE (arg)))
969 : : sign = UNSIGNED;
970 : 0 : else if (sign == UNSIGNED
971 : 0 : && get_range_pos_neg (arg, g) != 1)
972 : 0 : return prec + (orig_sign != sign);
973 : 0 : prec = TYPE_PRECISION (TREE_TYPE (arg));
974 : : }
975 : 239 : if (++cnt > 30)
976 : 0 : return prec + (orig_sign != sign);
977 : 239 : continue;
978 : : }
979 : : }
980 : 88729 : return prec + (orig_sign != sign);
981 : : }
982 : 27173 : if (sign == TYPE_SIGN (TREE_TYPE (arg)))
983 : : {
984 : 19792 : int p1 = wi::min_precision (r.lower_bound (), sign);
985 : 19792 : int p2 = wi::min_precision (r.upper_bound (), sign);
986 : 19792 : p1 = MAX (p1, p2);
987 : 19792 : prec = MIN (prec, p1);
988 : : }
989 : 14659 : else if (sign == UNSIGNED && !wi::neg_p (r.lower_bound (), SIGNED))
990 : : {
991 : 7274 : int p = wi::min_precision (r.upper_bound (), UNSIGNED);
992 : 7381 : prec = MIN (prec, p);
993 : : }
994 : 27173 : return prec + (orig_sign != sign);
995 : 115902 : }
996 : :
997 : : /* Helper for expand_*_overflow. Set the __imag__ part to true
998 : : (1 except for signed:1 type, in which case store -1). */
999 : :
1000 : : static void
1001 : 81028 : expand_arith_set_overflow (tree lhs, rtx target)
1002 : : {
1003 : 81028 : if (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (lhs))) == 1
1004 : 81028 : && !TYPE_UNSIGNED (TREE_TYPE (TREE_TYPE (lhs))))
1005 : 194 : write_complex_part (target, constm1_rtx, true, false);
1006 : : else
1007 : 80834 : write_complex_part (target, const1_rtx, true, false);
1008 : 81028 : }
1009 : :
1010 : : /* Helper for expand_*_overflow. Store RES into the __real__ part
1011 : : of TARGET. If RES has larger MODE than __real__ part of TARGET,
1012 : : set the __imag__ part to 1 if RES doesn't fit into it. Similarly
1013 : : if LHS has smaller precision than its mode. */
1014 : :
1015 : : static void
1016 : 72412 : expand_arith_overflow_result_store (tree lhs, rtx target,
1017 : : scalar_int_mode mode, rtx res)
1018 : : {
1019 : 72412 : scalar_int_mode tgtmode
1020 : 144824 : = as_a <scalar_int_mode> (GET_MODE_INNER (GET_MODE (target)));
1021 : 72412 : rtx lres = res;
1022 : 72412 : if (tgtmode != mode)
1023 : : {
1024 : 17928 : rtx_code_label *done_label = gen_label_rtx ();
1025 : 17928 : int uns = TYPE_UNSIGNED (TREE_TYPE (TREE_TYPE (lhs)));
1026 : 17928 : lres = convert_modes (tgtmode, mode, res, uns);
1027 : 17928 : gcc_assert (GET_MODE_PRECISION (tgtmode) < GET_MODE_PRECISION (mode));
1028 : 17928 : do_compare_rtx_and_jump (res, convert_modes (mode, tgtmode, lres, uns),
1029 : : EQ, true, mode, NULL_RTX, NULL, done_label,
1030 : : profile_probability::very_likely ());
1031 : 17928 : expand_arith_set_overflow (lhs, target);
1032 : 17928 : emit_label (done_label);
1033 : : }
1034 : 72412 : int prec = TYPE_PRECISION (TREE_TYPE (TREE_TYPE (lhs)));
1035 : 72412 : int tgtprec = GET_MODE_PRECISION (tgtmode);
1036 : 72412 : if (prec < tgtprec)
1037 : : {
1038 : 3544 : rtx_code_label *done_label = gen_label_rtx ();
1039 : 3544 : int uns = TYPE_UNSIGNED (TREE_TYPE (TREE_TYPE (lhs)));
1040 : 3544 : res = lres;
1041 : 3544 : if (uns)
1042 : : {
1043 : 2008 : rtx mask
1044 : 2008 : = immed_wide_int_const (wi::shifted_mask (0, prec, false, tgtprec),
1045 : : tgtmode);
1046 : 2008 : lres = expand_simple_binop (tgtmode, AND, res, mask, NULL_RTX,
1047 : : true, OPTAB_LIB_WIDEN);
1048 : : }
1049 : : else
1050 : : {
1051 : 1536 : lres = expand_shift (LSHIFT_EXPR, tgtmode, res, tgtprec - prec,
1052 : : NULL_RTX, 1);
1053 : 1536 : lres = expand_shift (RSHIFT_EXPR, tgtmode, lres, tgtprec - prec,
1054 : : NULL_RTX, 0);
1055 : : }
1056 : 3544 : do_compare_rtx_and_jump (res, lres,
1057 : : EQ, true, tgtmode, NULL_RTX, NULL, done_label,
1058 : : profile_probability::very_likely ());
1059 : 3544 : expand_arith_set_overflow (lhs, target);
1060 : 3544 : emit_label (done_label);
1061 : : }
1062 : 72412 : write_complex_part (target, lres, false, false);
1063 : 72412 : }
1064 : :
1065 : : /* Helper for expand_*_overflow. Store RES into TARGET. */
1066 : :
1067 : : static void
1068 : 4603 : expand_ubsan_result_store (tree lhs, rtx target, scalar_int_mode mode,
1069 : : rtx res, rtx_code_label *do_error)
1070 : : {
1071 : 4603 : if (TREE_CODE (TREE_TYPE (lhs)) == BITINT_TYPE
1072 : 4603 : && TYPE_PRECISION (TREE_TYPE (lhs)) < GET_MODE_PRECISION (mode))
1073 : : {
1074 : 54 : int uns = TYPE_UNSIGNED (TREE_TYPE (lhs));
1075 : 54 : int prec = TYPE_PRECISION (TREE_TYPE (lhs));
1076 : 54 : int tgtprec = GET_MODE_PRECISION (mode);
1077 : 54 : rtx resc = gen_reg_rtx (mode), lres;
1078 : 54 : emit_move_insn (resc, res);
1079 : 54 : if (uns)
1080 : : {
1081 : 0 : rtx mask
1082 : 0 : = immed_wide_int_const (wi::shifted_mask (0, prec, false, tgtprec),
1083 : : mode);
1084 : 0 : lres = expand_simple_binop (mode, AND, res, mask, NULL_RTX,
1085 : : true, OPTAB_LIB_WIDEN);
1086 : : }
1087 : : else
1088 : : {
1089 : 54 : lres = expand_shift (LSHIFT_EXPR, mode, res, tgtprec - prec,
1090 : : NULL_RTX, 1);
1091 : 54 : lres = expand_shift (RSHIFT_EXPR, mode, lres, tgtprec - prec,
1092 : : NULL_RTX, 0);
1093 : : }
1094 : 54 : if (lres != res)
1095 : 54 : emit_move_insn (res, lres);
1096 : 54 : do_compare_rtx_and_jump (res, resc,
1097 : : NE, true, mode, NULL_RTX, NULL, do_error,
1098 : : profile_probability::very_unlikely ());
1099 : : }
1100 : 4603 : if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
1101 : : /* If this is a scalar in a register that is stored in a wider mode
1102 : : than the declared mode, compute the result into its declared mode
1103 : : and then convert to the wider mode. Our value is the computed
1104 : : expression. */
1105 : 0 : convert_move (SUBREG_REG (target), res, SUBREG_PROMOTED_SIGN (target));
1106 : : else
1107 : 4603 : emit_move_insn (target, res);
1108 : 4603 : }
1109 : :
1110 : : /* Add sub/add overflow checking to the statement STMT.
1111 : : CODE says whether the operation is +, or -. */
1112 : :
1113 : : void
1114 : 39726 : expand_addsub_overflow (location_t loc, tree_code code, tree lhs,
1115 : : tree arg0, tree arg1, bool unsr_p, bool uns0_p,
1116 : : bool uns1_p, bool is_ubsan, tree *datap)
1117 : : {
1118 : 39726 : rtx res, target = NULL_RTX;
1119 : 39726 : tree fn;
1120 : 39726 : rtx_code_label *done_label = gen_label_rtx ();
1121 : 39726 : rtx_code_label *do_error = gen_label_rtx ();
1122 : 39726 : do_pending_stack_adjust ();
1123 : 39726 : rtx op0 = expand_normal (arg0);
1124 : 39726 : rtx op1 = expand_normal (arg1);
1125 : 39726 : scalar_int_mode mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (arg0));
1126 : 39726 : int prec = GET_MODE_PRECISION (mode);
1127 : 39726 : rtx sgn = immed_wide_int_const (wi::min_value (prec, SIGNED), mode);
1128 : 39726 : bool do_xor = false;
1129 : :
1130 : 39726 : if (is_ubsan)
1131 : 3337 : gcc_assert (!unsr_p && !uns0_p && !uns1_p);
1132 : :
1133 : 39726 : if (lhs)
1134 : : {
1135 : 39198 : target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
1136 : 39198 : if (!is_ubsan)
1137 : 36389 : write_complex_part (target, const0_rtx, true, false);
1138 : : }
1139 : :
1140 : : /* We assume both operands and result have the same precision
1141 : : here (GET_MODE_BITSIZE (mode)), S stands for signed type
1142 : : with that precision, U for unsigned type with that precision,
1143 : : sgn for unsigned most significant bit in that precision.
1144 : : s1 is signed first operand, u1 is unsigned first operand,
1145 : : s2 is signed second operand, u2 is unsigned second operand,
1146 : : sr is signed result, ur is unsigned result and the following
1147 : : rules say how to compute result (which is always result of
1148 : : the operands as if both were unsigned, cast to the right
1149 : : signedness) and how to compute whether operation overflowed.
1150 : :
1151 : : s1 + s2 -> sr
1152 : : res = (S) ((U) s1 + (U) s2)
1153 : : ovf = s2 < 0 ? res > s1 : res < s1 (or jump on overflow)
1154 : : s1 - s2 -> sr
1155 : : res = (S) ((U) s1 - (U) s2)
1156 : : ovf = s2 < 0 ? res < s1 : res > s2 (or jump on overflow)
1157 : : u1 + u2 -> ur
1158 : : res = u1 + u2
1159 : : ovf = res < u1 (or jump on carry, but RTL opts will handle it)
1160 : : u1 - u2 -> ur
1161 : : res = u1 - u2
1162 : : ovf = res > u1 (or jump on carry, but RTL opts will handle it)
1163 : : s1 + u2 -> sr
1164 : : res = (S) ((U) s1 + u2)
1165 : : ovf = ((U) res ^ sgn) < u2
1166 : : s1 + u2 -> ur
1167 : : t1 = (S) (u2 ^ sgn)
1168 : : t2 = s1 + t1
1169 : : res = (U) t2 ^ sgn
1170 : : ovf = t1 < 0 ? t2 > s1 : t2 < s1 (or jump on overflow)
1171 : : s1 - u2 -> sr
1172 : : res = (S) ((U) s1 - u2)
1173 : : ovf = u2 > ((U) s1 ^ sgn)
1174 : : s1 - u2 -> ur
1175 : : res = (U) s1 - u2
1176 : : ovf = s1 < 0 || u2 > (U) s1
1177 : : u1 - s2 -> sr
1178 : : res = u1 - (U) s2
1179 : : ovf = u1 >= ((U) s2 ^ sgn)
1180 : : u1 - s2 -> ur
1181 : : t1 = u1 ^ sgn
1182 : : t2 = t1 - (U) s2
1183 : : res = t2 ^ sgn
1184 : : ovf = s2 < 0 ? (S) t2 < (S) t1 : (S) t2 > (S) t1 (or jump on overflow)
1185 : : s1 + s2 -> ur
1186 : : res = (U) s1 + (U) s2
1187 : : ovf = s2 < 0 ? (s1 | (S) res) < 0) : (s1 & (S) res) < 0)
1188 : : u1 + u2 -> sr
1189 : : res = (S) (u1 + u2)
1190 : : ovf = (U) res < u2 || res < 0
1191 : : u1 - u2 -> sr
1192 : : res = (S) (u1 - u2)
1193 : : ovf = u1 >= u2 ? res < 0 : res >= 0
1194 : : s1 - s2 -> ur
1195 : : res = (U) s1 - (U) s2
1196 : : ovf = s2 >= 0 ? ((s1 | (S) res) < 0) : ((s1 & (S) res) < 0) */
1197 : :
1198 : 39726 : if (code == PLUS_EXPR && uns0_p && !uns1_p)
1199 : : {
1200 : : /* PLUS_EXPR is commutative, if operand signedness differs,
1201 : : canonicalize to the first operand being signed and second
1202 : : unsigned to simplify following code. */
1203 : : std::swap (op0, op1);
1204 : : std::swap (arg0, arg1);
1205 : : uns0_p = false;
1206 : : uns1_p = true;
1207 : : }
1208 : :
1209 : : /* u1 +- u2 -> ur */
1210 : 38891 : if (uns0_p && uns1_p && unsr_p)
1211 : : {
1212 : 21549 : insn_code icode = optab_handler (code == PLUS_EXPR ? uaddv4_optab
1213 : : : usubv4_optab, mode);
1214 : 16093 : if (icode != CODE_FOR_nothing)
1215 : : {
1216 : 15188 : class expand_operand ops[4];
1217 : 15188 : rtx_insn *last = get_last_insn ();
1218 : :
1219 : 15188 : res = gen_reg_rtx (mode);
1220 : 15188 : create_output_operand (&ops[0], res, mode);
1221 : 15188 : create_input_operand (&ops[1], op0, mode);
1222 : 15188 : create_input_operand (&ops[2], op1, mode);
1223 : 15188 : create_fixed_operand (&ops[3], do_error);
1224 : 15188 : if (maybe_expand_insn (icode, 4, ops))
1225 : : {
1226 : 15188 : last = get_last_insn ();
1227 : 15188 : if (profile_status_for_fn (cfun) != PROFILE_ABSENT
1228 : 12523 : && JUMP_P (last)
1229 : 12523 : && any_condjump_p (last)
1230 : 27711 : && !find_reg_note (last, REG_BR_PROB, 0))
1231 : 12523 : add_reg_br_prob_note (last,
1232 : : profile_probability::very_unlikely ());
1233 : 15188 : emit_jump (done_label);
1234 : 15188 : goto do_error_label;
1235 : : }
1236 : :
1237 : 0 : delete_insns_since (last);
1238 : : }
1239 : :
1240 : : /* Compute the operation. On RTL level, the addition is always
1241 : : unsigned. */
1242 : 1810 : res = expand_binop (mode, code == PLUS_EXPR ? add_optab : sub_optab,
1243 : : op0, op1, NULL_RTX, false, OPTAB_LIB_WIDEN);
1244 : 905 : rtx tem = op0;
1245 : : /* For PLUS_EXPR, the operation is commutative, so we can pick
1246 : : operand to compare against. For prec <= BITS_PER_WORD, I think
1247 : : preferring REG operand is better over CONST_INT, because
1248 : : the CONST_INT might enlarge the instruction or CSE would need
1249 : : to figure out we'd already loaded it into a register before.
1250 : : For prec > BITS_PER_WORD, I think CONST_INT might be more beneficial,
1251 : : as then the multi-word comparison can be perhaps simplified. */
1252 : 905 : if (code == PLUS_EXPR
1253 : 0 : && (prec <= BITS_PER_WORD
1254 : 0 : ? (CONST_SCALAR_INT_P (op0) && REG_P (op1))
1255 : 0 : : CONST_SCALAR_INT_P (op1)))
1256 : 905 : tem = op1;
1257 : 1810 : do_compare_rtx_and_jump (res, tem, code == PLUS_EXPR ? GEU : LEU,
1258 : : true, mode, NULL_RTX, NULL, done_label,
1259 : : profile_probability::very_likely ());
1260 : 905 : goto do_error_label;
1261 : : }
1262 : :
1263 : : /* s1 +- u2 -> sr */
1264 : 23633 : if (!uns0_p && uns1_p && !unsr_p)
1265 : : {
1266 : : /* Compute the operation. On RTL level, the addition is always
1267 : : unsigned. */
1268 : 2111 : res = expand_binop (mode, code == PLUS_EXPR ? add_optab : sub_optab,
1269 : : op0, op1, NULL_RTX, false, OPTAB_LIB_WIDEN);
1270 : 2111 : rtx tem = expand_binop (mode, add_optab,
1271 : : code == PLUS_EXPR ? res : op0, sgn,
1272 : : NULL_RTX, false, OPTAB_LIB_WIDEN);
1273 : 1309 : do_compare_rtx_and_jump (tem, op1, GEU, true, mode, NULL_RTX, NULL,
1274 : : done_label, profile_probability::very_likely ());
1275 : 1309 : goto do_error_label;
1276 : : }
1277 : :
1278 : : /* s1 + u2 -> ur */
1279 : 22324 : if (code == PLUS_EXPR && !uns0_p && uns1_p && unsr_p)
1280 : : {
1281 : 1919 : op1 = expand_binop (mode, add_optab, op1, sgn, NULL_RTX, false,
1282 : : OPTAB_LIB_WIDEN);
1283 : : /* As we've changed op1, we have to avoid using the value range
1284 : : for the original argument. */
1285 : 1919 : arg1 = error_mark_node;
1286 : 1919 : do_xor = true;
1287 : 1919 : goto do_signed;
1288 : : }
1289 : :
1290 : : /* u1 - s2 -> ur */
1291 : 20405 : if (code == MINUS_EXPR && uns0_p && !uns1_p && unsr_p)
1292 : : {
1293 : 819 : op0 = expand_binop (mode, add_optab, op0, sgn, NULL_RTX, false,
1294 : : OPTAB_LIB_WIDEN);
1295 : : /* As we've changed op0, we have to avoid using the value range
1296 : : for the original argument. */
1297 : 819 : arg0 = error_mark_node;
1298 : 819 : do_xor = true;
1299 : 819 : goto do_signed;
1300 : : }
1301 : :
1302 : : /* s1 - u2 -> ur */
1303 : 19586 : if (code == MINUS_EXPR && !uns0_p && uns1_p && unsr_p)
1304 : : {
1305 : : /* Compute the operation. On RTL level, the addition is always
1306 : : unsigned. */
1307 : 1358 : res = expand_binop (mode, sub_optab, op0, op1, NULL_RTX, false,
1308 : : OPTAB_LIB_WIDEN);
1309 : 1358 : int pos_neg = get_range_pos_neg (arg0, currently_expanding_gimple_stmt);
1310 : 1358 : if (pos_neg == 2)
1311 : : /* If ARG0 is known to be always negative, this is always overflow. */
1312 : 148 : emit_jump (do_error);
1313 : 1210 : else if (pos_neg == 3)
1314 : : /* If ARG0 is not known to be always positive, check at runtime. */
1315 : 1210 : do_compare_rtx_and_jump (op0, const0_rtx, LT, false, mode, NULL_RTX,
1316 : : NULL, do_error, profile_probability::very_unlikely ());
1317 : 1358 : do_compare_rtx_and_jump (op1, op0, LEU, true, mode, NULL_RTX, NULL,
1318 : : done_label, profile_probability::very_likely ());
1319 : 1358 : goto do_error_label;
1320 : : }
1321 : :
1322 : : /* u1 - s2 -> sr */
1323 : 18228 : if (code == MINUS_EXPR && uns0_p && !uns1_p && !unsr_p)
1324 : : {
1325 : : /* Compute the operation. On RTL level, the addition is always
1326 : : unsigned. */
1327 : 390 : res = expand_binop (mode, sub_optab, op0, op1, NULL_RTX, false,
1328 : : OPTAB_LIB_WIDEN);
1329 : 390 : rtx tem = expand_binop (mode, add_optab, op1, sgn, NULL_RTX, false,
1330 : : OPTAB_LIB_WIDEN);
1331 : 390 : do_compare_rtx_and_jump (op0, tem, LTU, true, mode, NULL_RTX, NULL,
1332 : : done_label, profile_probability::very_likely ());
1333 : 390 : goto do_error_label;
1334 : : }
1335 : :
1336 : : /* u1 + u2 -> sr */
1337 : 17838 : if (code == PLUS_EXPR && uns0_p && uns1_p && !unsr_p)
1338 : : {
1339 : : /* Compute the operation. On RTL level, the addition is always
1340 : : unsigned. */
1341 : 1394 : res = expand_binop (mode, add_optab, op0, op1, NULL_RTX, false,
1342 : : OPTAB_LIB_WIDEN);
1343 : 1394 : do_compare_rtx_and_jump (res, const0_rtx, LT, false, mode, NULL_RTX,
1344 : : NULL, do_error, profile_probability::very_unlikely ());
1345 : 1394 : rtx tem = op1;
1346 : : /* The operation is commutative, so we can pick operand to compare
1347 : : against. For prec <= BITS_PER_WORD, I think preferring REG operand
1348 : : is better over CONST_INT, because the CONST_INT might enlarge the
1349 : : instruction or CSE would need to figure out we'd already loaded it
1350 : : into a register before. For prec > BITS_PER_WORD, I think CONST_INT
1351 : : might be more beneficial, as then the multi-word comparison can be
1352 : : perhaps simplified. */
1353 : 1394 : if (prec <= BITS_PER_WORD
1354 : 1394 : ? (CONST_SCALAR_INT_P (op1) && REG_P (op0))
1355 : 448 : : CONST_SCALAR_INT_P (op0))
1356 : 1394 : tem = op0;
1357 : 1394 : do_compare_rtx_and_jump (res, tem, GEU, true, mode, NULL_RTX, NULL,
1358 : : done_label, profile_probability::very_likely ());
1359 : 1394 : goto do_error_label;
1360 : : }
1361 : :
1362 : : /* s1 +- s2 -> ur */
1363 : 16444 : if (!uns0_p && !uns1_p && unsr_p)
1364 : : {
1365 : : /* Compute the operation. On RTL level, the addition is always
1366 : : unsigned. */
1367 : 3516 : res = expand_binop (mode, code == PLUS_EXPR ? add_optab : sub_optab,
1368 : : op0, op1, NULL_RTX, false, OPTAB_LIB_WIDEN);
1369 : 2359 : int pos_neg = get_range_pos_neg (arg1, currently_expanding_gimple_stmt);
1370 : 2359 : if (code == PLUS_EXPR)
1371 : : {
1372 : 1202 : int pos_neg0 = get_range_pos_neg (arg0,
1373 : : currently_expanding_gimple_stmt);
1374 : 1202 : if (pos_neg0 != 3 && pos_neg == 3)
1375 : : {
1376 : : std::swap (op0, op1);
1377 : : pos_neg = pos_neg0;
1378 : : }
1379 : : }
1380 : 2290 : rtx tem;
1381 : 2290 : if (pos_neg != 3)
1382 : : {
1383 : 1941 : tem = expand_binop (mode, ((pos_neg == 1) ^ (code == MINUS_EXPR))
1384 : : ? and_optab : ior_optab,
1385 : : op0, res, NULL_RTX, false, OPTAB_LIB_WIDEN);
1386 : 1161 : do_compare_rtx_and_jump (tem, const0_rtx, GE, false, mode, NULL,
1387 : : NULL, done_label, profile_probability::very_likely ());
1388 : : }
1389 : : else
1390 : : {
1391 : 1198 : rtx_code_label *do_ior_label = gen_label_rtx ();
1392 : 1620 : do_compare_rtx_and_jump (op1, const0_rtx,
1393 : : code == MINUS_EXPR ? GE : LT, false, mode,
1394 : : NULL_RTX, NULL, do_ior_label,
1395 : : profile_probability::even ());
1396 : 1198 : tem = expand_binop (mode, and_optab, op0, res, NULL_RTX, false,
1397 : : OPTAB_LIB_WIDEN);
1398 : 1198 : do_compare_rtx_and_jump (tem, const0_rtx, GE, false, mode, NULL_RTX,
1399 : : NULL, done_label, profile_probability::very_likely ());
1400 : 1198 : emit_jump (do_error);
1401 : 1198 : emit_label (do_ior_label);
1402 : 1198 : tem = expand_binop (mode, ior_optab, op0, res, NULL_RTX, false,
1403 : : OPTAB_LIB_WIDEN);
1404 : 1198 : do_compare_rtx_and_jump (tem, const0_rtx, GE, false, mode, NULL_RTX,
1405 : : NULL, done_label, profile_probability::very_likely ());
1406 : : }
1407 : 2359 : goto do_error_label;
1408 : : }
1409 : :
1410 : : /* u1 - u2 -> sr */
1411 : 14085 : if (code == MINUS_EXPR && uns0_p && uns1_p && !unsr_p)
1412 : : {
1413 : : /* Compute the operation. On RTL level, the addition is always
1414 : : unsigned. */
1415 : 1959 : res = expand_binop (mode, sub_optab, op0, op1, NULL_RTX, false,
1416 : : OPTAB_LIB_WIDEN);
1417 : 1959 : rtx_code_label *op0_geu_op1 = gen_label_rtx ();
1418 : 1959 : do_compare_rtx_and_jump (op0, op1, GEU, true, mode, NULL_RTX, NULL,
1419 : : op0_geu_op1, profile_probability::even ());
1420 : 1959 : do_compare_rtx_and_jump (res, const0_rtx, LT, false, mode, NULL_RTX,
1421 : : NULL, done_label, profile_probability::very_likely ());
1422 : 1959 : emit_jump (do_error);
1423 : 1959 : emit_label (op0_geu_op1);
1424 : 1959 : do_compare_rtx_and_jump (res, const0_rtx, GE, false, mode, NULL_RTX,
1425 : : NULL, done_label, profile_probability::very_likely ());
1426 : 1959 : goto do_error_label;
1427 : : }
1428 : :
1429 : 12126 : gcc_assert (!uns0_p && !uns1_p && !unsr_p);
1430 : :
1431 : : /* s1 +- s2 -> sr */
1432 : 12126 : do_signed:
1433 : 14864 : {
1434 : 21443 : insn_code icode = optab_handler (code == PLUS_EXPR ? addv4_optab
1435 : : : subv4_optab, mode);
1436 : 14864 : if (icode != CODE_FOR_nothing)
1437 : : {
1438 : 14864 : class expand_operand ops[4];
1439 : 14864 : rtx_insn *last = get_last_insn ();
1440 : :
1441 : 14864 : res = gen_reg_rtx (mode);
1442 : 14864 : create_output_operand (&ops[0], res, mode);
1443 : 14864 : create_input_operand (&ops[1], op0, mode);
1444 : 14864 : create_input_operand (&ops[2], op1, mode);
1445 : 14864 : create_fixed_operand (&ops[3], do_error);
1446 : 14864 : if (maybe_expand_insn (icode, 4, ops))
1447 : : {
1448 : 14864 : last = get_last_insn ();
1449 : 14864 : if (profile_status_for_fn (cfun) != PROFILE_ABSENT
1450 : 9658 : && JUMP_P (last)
1451 : 9658 : && any_condjump_p (last)
1452 : 24522 : && !find_reg_note (last, REG_BR_PROB, 0))
1453 : 9658 : add_reg_br_prob_note (last,
1454 : : profile_probability::very_unlikely ());
1455 : 14864 : emit_jump (done_label);
1456 : 14864 : goto do_error_label;
1457 : : }
1458 : :
1459 : 0 : delete_insns_since (last);
1460 : : }
1461 : :
1462 : : /* Compute the operation. On RTL level, the addition is always
1463 : : unsigned. */
1464 : 0 : res = expand_binop (mode, code == PLUS_EXPR ? add_optab : sub_optab,
1465 : : op0, op1, NULL_RTX, false, OPTAB_LIB_WIDEN);
1466 : :
1467 : : /* If we can prove that one of the arguments (for MINUS_EXPR only
1468 : : the second operand, as subtraction is not commutative) is always
1469 : : non-negative or always negative, we can do just one comparison
1470 : : and conditional jump. */
1471 : 0 : int pos_neg = get_range_pos_neg (arg1, currently_expanding_gimple_stmt);
1472 : 0 : if (code == PLUS_EXPR)
1473 : : {
1474 : 0 : int pos_neg0 = get_range_pos_neg (arg0,
1475 : : currently_expanding_gimple_stmt);
1476 : 0 : if (pos_neg0 != 3 && pos_neg == 3)
1477 : : {
1478 : : std::swap (op0, op1);
1479 : : pos_neg = pos_neg0;
1480 : : }
1481 : : }
1482 : :
1483 : : /* Addition overflows if and only if the two operands have the same sign,
1484 : : and the result has the opposite sign. Subtraction overflows if and
1485 : : only if the two operands have opposite sign, and the subtrahend has
1486 : : the same sign as the result. Here 0 is counted as positive. */
1487 : 0 : if (pos_neg == 3)
1488 : : {
1489 : : /* Compute op0 ^ op1 (operands have opposite sign). */
1490 : 0 : rtx op_xor = expand_binop (mode, xor_optab, op0, op1, NULL_RTX, false,
1491 : : OPTAB_LIB_WIDEN);
1492 : :
1493 : : /* Compute res ^ op1 (result and 2nd operand have opposite sign). */
1494 : 0 : rtx res_xor = expand_binop (mode, xor_optab, res, op1, NULL_RTX, false,
1495 : : OPTAB_LIB_WIDEN);
1496 : :
1497 : 0 : rtx tem;
1498 : 0 : if (code == PLUS_EXPR)
1499 : : {
1500 : : /* Compute (res ^ op1) & ~(op0 ^ op1). */
1501 : 0 : tem = expand_unop (mode, one_cmpl_optab, op_xor, NULL_RTX, false);
1502 : 0 : tem = expand_binop (mode, and_optab, res_xor, tem, NULL_RTX, false,
1503 : : OPTAB_LIB_WIDEN);
1504 : : }
1505 : : else
1506 : : {
1507 : : /* Compute (op0 ^ op1) & ~(res ^ op1). */
1508 : 0 : tem = expand_unop (mode, one_cmpl_optab, res_xor, NULL_RTX, false);
1509 : 0 : tem = expand_binop (mode, and_optab, op_xor, tem, NULL_RTX, false,
1510 : : OPTAB_LIB_WIDEN);
1511 : : }
1512 : :
1513 : : /* No overflow if the result has bit sign cleared. */
1514 : 0 : do_compare_rtx_and_jump (tem, const0_rtx, GE, false, mode, NULL_RTX,
1515 : : NULL, done_label, profile_probability::very_likely ());
1516 : : }
1517 : :
1518 : : /* Compare the result of the operation with the first operand.
1519 : : No overflow for addition if second operand is positive and result
1520 : : is larger or second operand is negative and result is smaller.
1521 : : Likewise for subtraction with sign of second operand flipped. */
1522 : : else
1523 : 0 : do_compare_rtx_and_jump (res, op0,
1524 : 0 : (pos_neg == 1) ^ (code == MINUS_EXPR) ? GE : LE,
1525 : : false, mode, NULL_RTX, NULL, done_label,
1526 : : profile_probability::very_likely ());
1527 : : }
1528 : :
1529 : 39726 : do_error_label:
1530 : 39726 : emit_label (do_error);
1531 : 39726 : if (is_ubsan)
1532 : : {
1533 : : /* Expand the ubsan builtin call. */
1534 : 3337 : push_temp_slots ();
1535 : 3337 : fn = ubsan_build_overflow_builtin (code, loc, TREE_TYPE (arg0),
1536 : : arg0, arg1, datap);
1537 : 3337 : expand_normal (fn);
1538 : 3337 : pop_temp_slots ();
1539 : 3337 : do_pending_stack_adjust ();
1540 : : }
1541 : 36389 : else if (lhs)
1542 : 36389 : expand_arith_set_overflow (lhs, target);
1543 : :
1544 : : /* We're done. */
1545 : 39726 : emit_label (done_label);
1546 : :
1547 : 39726 : if (lhs)
1548 : : {
1549 : 39198 : if (is_ubsan)
1550 : 2809 : expand_ubsan_result_store (lhs, target, mode, res, do_error);
1551 : : else
1552 : : {
1553 : 36389 : if (do_xor)
1554 : 2738 : res = expand_binop (mode, add_optab, res, sgn, NULL_RTX, false,
1555 : : OPTAB_LIB_WIDEN);
1556 : :
1557 : 36389 : expand_arith_overflow_result_store (lhs, target, mode, res);
1558 : : }
1559 : : }
1560 : 39726 : }
1561 : :
1562 : : /* Add negate overflow checking to the statement STMT. */
1563 : :
1564 : : static void
1565 : 972 : expand_neg_overflow (location_t loc, tree lhs, tree arg1, bool is_ubsan,
1566 : : tree *datap)
1567 : : {
1568 : 972 : rtx res, op1;
1569 : 972 : tree fn;
1570 : 972 : rtx_code_label *done_label, *do_error;
1571 : 972 : rtx target = NULL_RTX;
1572 : :
1573 : 972 : done_label = gen_label_rtx ();
1574 : 972 : do_error = gen_label_rtx ();
1575 : :
1576 : 972 : do_pending_stack_adjust ();
1577 : 972 : op1 = expand_normal (arg1);
1578 : :
1579 : 972 : scalar_int_mode mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (arg1));
1580 : 972 : if (lhs)
1581 : : {
1582 : 840 : target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
1583 : 840 : if (!is_ubsan)
1584 : 402 : write_complex_part (target, const0_rtx, true, false);
1585 : : }
1586 : :
1587 : 972 : enum insn_code icode = optab_handler (negv3_optab, mode);
1588 : 972 : if (icode != CODE_FOR_nothing)
1589 : : {
1590 : 894 : class expand_operand ops[3];
1591 : 894 : rtx_insn *last = get_last_insn ();
1592 : :
1593 : 894 : res = gen_reg_rtx (mode);
1594 : 894 : create_output_operand (&ops[0], res, mode);
1595 : 894 : create_input_operand (&ops[1], op1, mode);
1596 : 894 : create_fixed_operand (&ops[2], do_error);
1597 : 894 : if (maybe_expand_insn (icode, 3, ops))
1598 : : {
1599 : 894 : last = get_last_insn ();
1600 : 894 : if (profile_status_for_fn (cfun) != PROFILE_ABSENT
1601 : 605 : && JUMP_P (last)
1602 : 605 : && any_condjump_p (last)
1603 : 1499 : && !find_reg_note (last, REG_BR_PROB, 0))
1604 : 605 : add_reg_br_prob_note (last,
1605 : : profile_probability::very_unlikely ());
1606 : 894 : emit_jump (done_label);
1607 : : }
1608 : : else
1609 : : {
1610 : 0 : delete_insns_since (last);
1611 : 0 : icode = CODE_FOR_nothing;
1612 : : }
1613 : : }
1614 : :
1615 : 894 : if (icode == CODE_FOR_nothing)
1616 : : {
1617 : : /* Compute the operation. On RTL level, the addition is always
1618 : : unsigned. */
1619 : 78 : res = expand_unop (mode, neg_optab, op1, NULL_RTX, false);
1620 : :
1621 : : /* Compare the operand with the most negative value. */
1622 : 78 : rtx minv = expand_normal (TYPE_MIN_VALUE (TREE_TYPE (arg1)));
1623 : 78 : do_compare_rtx_and_jump (op1, minv, NE, true, mode, NULL_RTX, NULL,
1624 : : done_label, profile_probability::very_likely ());
1625 : : }
1626 : :
1627 : 972 : emit_label (do_error);
1628 : 972 : if (is_ubsan)
1629 : : {
1630 : : /* Expand the ubsan builtin call. */
1631 : 570 : push_temp_slots ();
1632 : 570 : fn = ubsan_build_overflow_builtin (NEGATE_EXPR, loc, TREE_TYPE (arg1),
1633 : : arg1, NULL_TREE, datap);
1634 : 570 : expand_normal (fn);
1635 : 570 : pop_temp_slots ();
1636 : 570 : do_pending_stack_adjust ();
1637 : : }
1638 : 402 : else if (lhs)
1639 : 402 : expand_arith_set_overflow (lhs, target);
1640 : :
1641 : : /* We're done. */
1642 : 972 : emit_label (done_label);
1643 : :
1644 : 972 : if (lhs)
1645 : : {
1646 : 840 : if (is_ubsan)
1647 : 438 : expand_ubsan_result_store (lhs, target, mode, res, do_error);
1648 : : else
1649 : 402 : expand_arith_overflow_result_store (lhs, target, mode, res);
1650 : : }
1651 : 972 : }
1652 : :
1653 : : /* Return true if UNS WIDEN_MULT_EXPR with result mode WMODE and operand
1654 : : mode MODE can be expanded without using a libcall. */
1655 : :
1656 : : static bool
1657 : 0 : can_widen_mult_without_libcall (scalar_int_mode wmode, scalar_int_mode mode,
1658 : : rtx op0, rtx op1, bool uns)
1659 : : {
1660 : 0 : if (find_widening_optab_handler (umul_widen_optab, wmode, mode)
1661 : : != CODE_FOR_nothing)
1662 : : return true;
1663 : :
1664 : 0 : if (find_widening_optab_handler (smul_widen_optab, wmode, mode)
1665 : : != CODE_FOR_nothing)
1666 : : return true;
1667 : :
1668 : 0 : rtx_insn *last = get_last_insn ();
1669 : 0 : if (CONSTANT_P (op0))
1670 : 0 : op0 = convert_modes (wmode, mode, op0, uns);
1671 : : else
1672 : 0 : op0 = gen_raw_REG (wmode, LAST_VIRTUAL_REGISTER + 1);
1673 : 0 : if (CONSTANT_P (op1))
1674 : 0 : op1 = convert_modes (wmode, mode, op1, uns);
1675 : : else
1676 : 0 : op1 = gen_raw_REG (wmode, LAST_VIRTUAL_REGISTER + 2);
1677 : 0 : rtx ret = expand_mult (wmode, op0, op1, NULL_RTX, uns, true);
1678 : 0 : delete_insns_since (last);
1679 : 0 : return ret != NULL_RTX;
1680 : : }
1681 : :
1682 : : /* Add mul overflow checking to the statement STMT. */
1683 : :
1684 : : static void
1685 : 18808 : expand_mul_overflow (location_t loc, tree lhs, tree arg0, tree arg1,
1686 : : bool unsr_p, bool uns0_p, bool uns1_p, bool is_ubsan,
1687 : : tree *datap)
1688 : : {
1689 : 18808 : rtx res, op0, op1;
1690 : 18808 : tree fn, type;
1691 : 18808 : rtx_code_label *done_label, *do_error;
1692 : 18808 : rtx target = NULL_RTX;
1693 : 18808 : signop sign;
1694 : 18808 : enum insn_code icode;
1695 : 18808 : int save_flag_trapv = flag_trapv;
1696 : :
1697 : : /* We don't want any __mulv?i3 etc. calls from the expansion of
1698 : : these internal functions, so disable -ftrapv temporarily. */
1699 : 18808 : flag_trapv = 0;
1700 : 18808 : done_label = gen_label_rtx ();
1701 : 18808 : do_error = gen_label_rtx ();
1702 : :
1703 : 18808 : do_pending_stack_adjust ();
1704 : 18808 : op0 = expand_normal (arg0);
1705 : 18808 : op1 = expand_normal (arg1);
1706 : :
1707 : 18808 : scalar_int_mode mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (arg0));
1708 : 18808 : bool uns = unsr_p;
1709 : 18808 : if (lhs)
1710 : : {
1711 : 18664 : target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
1712 : 18664 : if (!is_ubsan)
1713 : 17308 : write_complex_part (target, const0_rtx, true, false);
1714 : : }
1715 : :
1716 : 17452 : if (is_ubsan)
1717 : 1500 : gcc_assert (!unsr_p && !uns0_p && !uns1_p);
1718 : :
1719 : : /* We assume both operands and result have the same precision
1720 : : here (GET_MODE_BITSIZE (mode)), S stands for signed type
1721 : : with that precision, U for unsigned type with that precision,
1722 : : sgn for unsigned most significant bit in that precision.
1723 : : s1 is signed first operand, u1 is unsigned first operand,
1724 : : s2 is signed second operand, u2 is unsigned second operand,
1725 : : sr is signed result, ur is unsigned result and the following
1726 : : rules say how to compute result (which is always result of
1727 : : the operands as if both were unsigned, cast to the right
1728 : : signedness) and how to compute whether operation overflowed.
1729 : : main_ovf (false) stands for jump on signed multiplication
1730 : : overflow or the main algorithm with uns == false.
1731 : : main_ovf (true) stands for jump on unsigned multiplication
1732 : : overflow or the main algorithm with uns == true.
1733 : :
1734 : : s1 * s2 -> sr
1735 : : res = (S) ((U) s1 * (U) s2)
1736 : : ovf = main_ovf (false)
1737 : : u1 * u2 -> ur
1738 : : res = u1 * u2
1739 : : ovf = main_ovf (true)
1740 : : s1 * u2 -> ur
1741 : : res = (U) s1 * u2
1742 : : ovf = (s1 < 0 && u2) || main_ovf (true)
1743 : : u1 * u2 -> sr
1744 : : res = (S) (u1 * u2)
1745 : : ovf = res < 0 || main_ovf (true)
1746 : : s1 * u2 -> sr
1747 : : res = (S) ((U) s1 * u2)
1748 : : ovf = (S) u2 >= 0 ? main_ovf (false)
1749 : : : (s1 != 0 && (s1 != -1 || u2 != (U) res))
1750 : : s1 * s2 -> ur
1751 : : t1 = (s1 & s2) < 0 ? (-(U) s1) : ((U) s1)
1752 : : t2 = (s1 & s2) < 0 ? (-(U) s2) : ((U) s2)
1753 : : res = t1 * t2
1754 : : ovf = (s1 ^ s2) < 0 ? (s1 && s2) : main_ovf (true) */
1755 : :
1756 : 18808 : if (uns0_p && !uns1_p)
1757 : : {
1758 : : /* Multiplication is commutative, if operand signedness differs,
1759 : : canonicalize to the first operand being signed and second
1760 : : unsigned to simplify following code. */
1761 : 1479 : std::swap (op0, op1);
1762 : 1479 : std::swap (arg0, arg1);
1763 : 1479 : uns0_p = false;
1764 : 1479 : uns1_p = true;
1765 : : }
1766 : :
1767 : 18808 : int pos_neg0 = get_range_pos_neg (arg0, currently_expanding_gimple_stmt);
1768 : 18808 : int pos_neg1 = get_range_pos_neg (arg1, currently_expanding_gimple_stmt);
1769 : : /* Unsigned types with smaller than mode precision, even if they have most
1770 : : significant bit set, are still zero-extended. */
1771 : 18808 : if (uns0_p && TYPE_PRECISION (TREE_TYPE (arg0)) < GET_MODE_PRECISION (mode))
1772 : : pos_neg0 = 1;
1773 : 18808 : if (uns1_p && TYPE_PRECISION (TREE_TYPE (arg1)) < GET_MODE_PRECISION (mode))
1774 : : pos_neg1 = 1;
1775 : :
1776 : : /* s1 * u2 -> ur */
1777 : 18808 : if (!uns0_p && uns1_p && unsr_p)
1778 : : {
1779 : 2403 : switch (pos_neg0)
1780 : : {
1781 : 0 : case 1:
1782 : : /* If s1 is non-negative, just perform normal u1 * u2 -> ur. */
1783 : 1676 : goto do_main;
1784 : 531 : case 2:
1785 : : /* If s1 is negative, avoid the main code, just multiply and
1786 : : signal overflow if op1 is not 0. */
1787 : 531 : struct separate_ops ops;
1788 : 531 : ops.code = MULT_EXPR;
1789 : 531 : ops.type = TREE_TYPE (arg1);
1790 : 531 : ops.op0 = make_tree (ops.type, op0);
1791 : 531 : ops.op1 = make_tree (ops.type, op1);
1792 : 531 : ops.op2 = NULL_TREE;
1793 : 531 : ops.location = loc;
1794 : 531 : res = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL);
1795 : 531 : do_compare_rtx_and_jump (op1, const0_rtx, EQ, true, mode, NULL_RTX,
1796 : : NULL, done_label, profile_probability::very_likely ());
1797 : 727 : goto do_error_label;
1798 : 1872 : case 3:
1799 : 3744 : if (get_min_precision (arg1, UNSIGNED)
1800 : 1872 : + get_min_precision (arg0, SIGNED) <= GET_MODE_PRECISION (mode))
1801 : : {
1802 : : /* If the first operand is sign extended from narrower type, the
1803 : : second operand is zero extended from narrower type and
1804 : : the sum of the two precisions is smaller or equal to the
1805 : : result precision: if the first argument is at runtime
1806 : : non-negative, maximum result will be 0x7e81 or 0x7f..fe80..01
1807 : : and there will be no overflow, if the first argument is
1808 : : negative and the second argument zero, the result will be
1809 : : 0 and there will be no overflow, if the first argument is
1810 : : negative and the second argument positive, the result when
1811 : : treated as signed will be negative (minimum -0x7f80 or
1812 : : -0x7f..f80..0) there will be always overflow. So, do
1813 : : res = (U) (s1 * u2)
1814 : : ovf = (S) res < 0 */
1815 : 196 : struct separate_ops ops;
1816 : 196 : ops.code = MULT_EXPR;
1817 : 196 : ops.type
1818 : 196 : = build_nonstandard_integer_type (GET_MODE_PRECISION (mode),
1819 : : 1);
1820 : 196 : ops.op0 = make_tree (ops.type, op0);
1821 : 196 : ops.op1 = make_tree (ops.type, op1);
1822 : 196 : ops.op2 = NULL_TREE;
1823 : 196 : ops.location = loc;
1824 : 196 : res = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL);
1825 : 196 : do_compare_rtx_and_jump (res, const0_rtx, GE, false,
1826 : : mode, NULL_RTX, NULL, done_label,
1827 : : profile_probability::very_likely ());
1828 : 196 : goto do_error_label;
1829 : : }
1830 : 1676 : rtx_code_label *do_main_label;
1831 : 1676 : do_main_label = gen_label_rtx ();
1832 : 1676 : do_compare_rtx_and_jump (op0, const0_rtx, GE, false, mode, NULL_RTX,
1833 : : NULL, do_main_label, profile_probability::very_likely ());
1834 : 1676 : do_compare_rtx_and_jump (op1, const0_rtx, EQ, true, mode, NULL_RTX,
1835 : : NULL, do_main_label, profile_probability::very_likely ());
1836 : 1676 : expand_arith_set_overflow (lhs, target);
1837 : 1676 : emit_label (do_main_label);
1838 : 1676 : goto do_main;
1839 : 0 : default:
1840 : 0 : gcc_unreachable ();
1841 : : }
1842 : : }
1843 : :
1844 : : /* u1 * u2 -> sr */
1845 : 16405 : if (uns0_p && uns1_p && !unsr_p)
1846 : : {
1847 : 1490 : if ((pos_neg0 | pos_neg1) == 1)
1848 : : {
1849 : : /* If both arguments are zero extended from narrower types,
1850 : : the MSB will be clear on both and so we can pretend it is
1851 : : a normal s1 * s2 -> sr multiplication. */
1852 : : uns0_p = false;
1853 : : uns1_p = false;
1854 : : }
1855 : : else
1856 : 1037 : uns = true;
1857 : : /* Rest of handling of this case after res is computed. */
1858 : 1490 : goto do_main;
1859 : : }
1860 : :
1861 : : /* s1 * u2 -> sr */
1862 : 14915 : if (!uns0_p && uns1_p && !unsr_p)
1863 : : {
1864 : 1553 : switch (pos_neg1)
1865 : : {
1866 : 48 : case 1:
1867 : 1016 : goto do_main;
1868 : 537 : case 2:
1869 : : /* If (S) u2 is negative (i.e. u2 is larger than maximum of S,
1870 : : avoid the main code, just multiply and signal overflow
1871 : : unless 0 * u2 or -1 * ((U) Smin). */
1872 : 537 : struct separate_ops ops;
1873 : 537 : ops.code = MULT_EXPR;
1874 : 537 : ops.type = TREE_TYPE (arg1);
1875 : 537 : ops.op0 = make_tree (ops.type, op0);
1876 : 537 : ops.op1 = make_tree (ops.type, op1);
1877 : 537 : ops.op2 = NULL_TREE;
1878 : 537 : ops.location = loc;
1879 : 537 : res = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL);
1880 : 537 : do_compare_rtx_and_jump (op0, const0_rtx, EQ, true, mode, NULL_RTX,
1881 : : NULL, done_label, profile_probability::very_likely ());
1882 : 537 : do_compare_rtx_and_jump (op0, constm1_rtx, NE, true, mode, NULL_RTX,
1883 : : NULL, do_error, profile_probability::very_unlikely ());
1884 : 537 : int prec;
1885 : 537 : prec = GET_MODE_PRECISION (mode);
1886 : 537 : rtx sgn;
1887 : 537 : sgn = immed_wide_int_const (wi::min_value (prec, SIGNED), mode);
1888 : 537 : do_compare_rtx_and_jump (op1, sgn, EQ, true, mode, NULL_RTX,
1889 : : NULL, done_label, profile_probability::very_likely ());
1890 : 537 : goto do_error_label;
1891 : 968 : case 3:
1892 : : /* Rest of handling of this case after res is computed. */
1893 : 968 : goto do_main;
1894 : 0 : default:
1895 : 0 : gcc_unreachable ();
1896 : : }
1897 : : }
1898 : :
1899 : : /* s1 * s2 -> ur */
1900 : 13362 : if (!uns0_p && !uns1_p && unsr_p)
1901 : : {
1902 : 2078 : rtx tem;
1903 : 2078 : switch (pos_neg0 | pos_neg1)
1904 : : {
1905 : 0 : case 1: /* Both operands known to be non-negative. */
1906 : 0 : goto do_main;
1907 : 77 : case 2: /* Both operands known to be negative. */
1908 : 77 : op0 = expand_unop (mode, neg_optab, op0, NULL_RTX, false);
1909 : 77 : op1 = expand_unop (mode, neg_optab, op1, NULL_RTX, false);
1910 : : /* Avoid looking at arg0/arg1 ranges, as we've changed
1911 : : the arguments. */
1912 : 77 : arg0 = error_mark_node;
1913 : 77 : arg1 = error_mark_node;
1914 : 77 : goto do_main;
1915 : 2001 : case 3:
1916 : 2001 : if ((pos_neg0 ^ pos_neg1) == 3)
1917 : : {
1918 : : /* If one operand is known to be negative and the other
1919 : : non-negative, this overflows always, unless the non-negative
1920 : : one is 0. Just do normal multiply and set overflow
1921 : : unless one of the operands is 0. */
1922 : 0 : struct separate_ops ops;
1923 : 0 : ops.code = MULT_EXPR;
1924 : 0 : ops.type
1925 : 0 : = build_nonstandard_integer_type (GET_MODE_PRECISION (mode),
1926 : : 1);
1927 : 0 : ops.op0 = make_tree (ops.type, op0);
1928 : 0 : ops.op1 = make_tree (ops.type, op1);
1929 : 0 : ops.op2 = NULL_TREE;
1930 : 0 : ops.location = loc;
1931 : 0 : res = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL);
1932 : 0 : do_compare_rtx_and_jump (pos_neg0 == 1 ? op0 : op1, const0_rtx, EQ,
1933 : : true, mode, NULL_RTX, NULL, done_label,
1934 : : profile_probability::very_likely ());
1935 : 0 : goto do_error_label;
1936 : : }
1937 : 4002 : if (get_min_precision (arg0, SIGNED)
1938 : 2001 : + get_min_precision (arg1, SIGNED) <= GET_MODE_PRECISION (mode))
1939 : : {
1940 : : /* If both operands are sign extended from narrower types and
1941 : : the sum of the two precisions is smaller or equal to the
1942 : : result precision: if both arguments are at runtime
1943 : : non-negative, maximum result will be 0x3f01 or 0x3f..f0..01
1944 : : and there will be no overflow, if both arguments are negative,
1945 : : maximum result will be 0x40..00 and there will be no overflow
1946 : : either, if one argument is positive and the other argument
1947 : : negative, the result when treated as signed will be negative
1948 : : and there will be always overflow, and if one argument is
1949 : : zero and the other negative the result will be zero and no
1950 : : overflow. So, do
1951 : : res = (U) (s1 * s2)
1952 : : ovf = (S) res < 0 */
1953 : 225 : struct separate_ops ops;
1954 : 225 : ops.code = MULT_EXPR;
1955 : 225 : ops.type
1956 : 225 : = build_nonstandard_integer_type (GET_MODE_PRECISION (mode),
1957 : : 1);
1958 : 225 : ops.op0 = make_tree (ops.type, op0);
1959 : 225 : ops.op1 = make_tree (ops.type, op1);
1960 : 225 : ops.op2 = NULL_TREE;
1961 : 225 : ops.location = loc;
1962 : 225 : res = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL);
1963 : 225 : do_compare_rtx_and_jump (res, const0_rtx, GE, false,
1964 : : mode, NULL_RTX, NULL, done_label,
1965 : : profile_probability::very_likely ());
1966 : 225 : goto do_error_label;
1967 : : }
1968 : : /* The general case, do all the needed comparisons at runtime. */
1969 : 1776 : rtx_code_label *do_main_label, *after_negate_label;
1970 : 1776 : rtx rop0, rop1;
1971 : 1776 : rop0 = gen_reg_rtx (mode);
1972 : 1776 : rop1 = gen_reg_rtx (mode);
1973 : 1776 : emit_move_insn (rop0, op0);
1974 : 1776 : emit_move_insn (rop1, op1);
1975 : 1776 : op0 = rop0;
1976 : 1776 : op1 = rop1;
1977 : 1776 : do_main_label = gen_label_rtx ();
1978 : 1776 : after_negate_label = gen_label_rtx ();
1979 : 1776 : tem = expand_binop (mode, and_optab, op0, op1, NULL_RTX, false,
1980 : : OPTAB_LIB_WIDEN);
1981 : 1776 : do_compare_rtx_and_jump (tem, const0_rtx, GE, false, mode, NULL_RTX,
1982 : : NULL, after_negate_label, profile_probability::very_likely ());
1983 : : /* Both arguments negative here, negate them and continue with
1984 : : normal unsigned overflow checking multiplication. */
1985 : 1776 : emit_move_insn (op0, expand_unop (mode, neg_optab, op0,
1986 : : NULL_RTX, false));
1987 : 1776 : emit_move_insn (op1, expand_unop (mode, neg_optab, op1,
1988 : : NULL_RTX, false));
1989 : : /* Avoid looking at arg0/arg1 ranges, as we might have changed
1990 : : the arguments. */
1991 : 1776 : arg0 = error_mark_node;
1992 : 1776 : arg1 = error_mark_node;
1993 : 1776 : emit_jump (do_main_label);
1994 : 1776 : emit_label (after_negate_label);
1995 : 1776 : tem = expand_binop (mode, xor_optab, op0, op1, NULL_RTX, false,
1996 : : OPTAB_LIB_WIDEN);
1997 : 1776 : do_compare_rtx_and_jump (tem, const0_rtx, GE, false, mode, NULL_RTX,
1998 : : NULL, do_main_label,
1999 : : profile_probability::very_likely ());
2000 : : /* One argument is negative here, the other positive. This
2001 : : overflows always, unless one of the arguments is 0. But
2002 : : if e.g. s2 is 0, (U) s1 * 0 doesn't overflow, whatever s1
2003 : : is, thus we can keep do_main code oring in overflow as is. */
2004 : 1776 : if (pos_neg0 != 2)
2005 : 1689 : do_compare_rtx_and_jump (op0, const0_rtx, EQ, true, mode, NULL_RTX,
2006 : : NULL, do_main_label,
2007 : : profile_probability::very_unlikely ());
2008 : 1776 : if (pos_neg1 != 2)
2009 : 699 : do_compare_rtx_and_jump (op1, const0_rtx, EQ, true, mode, NULL_RTX,
2010 : : NULL, do_main_label,
2011 : : profile_probability::very_unlikely ());
2012 : 1776 : expand_arith_set_overflow (lhs, target);
2013 : 1776 : emit_label (do_main_label);
2014 : 1776 : goto do_main;
2015 : 0 : default:
2016 : 0 : gcc_unreachable ();
2017 : : }
2018 : : }
2019 : :
2020 : 11284 : do_main:
2021 : 17319 : type = build_nonstandard_integer_type (GET_MODE_PRECISION (mode), uns);
2022 : 17319 : sign = uns ? UNSIGNED : SIGNED;
2023 : 17319 : icode = optab_handler (uns ? umulv4_optab : mulv4_optab, mode);
2024 : 17319 : if (uns
2025 : 10371 : && (integer_pow2p (arg0) || integer_pow2p (arg1))
2026 : 18181 : && (optimize_insn_for_speed_p () || icode == CODE_FOR_nothing))
2027 : : {
2028 : : /* Optimize unsigned multiplication by power of 2 constant
2029 : : using 2 shifts, one for result, one to extract the shifted
2030 : : out bits to see if they are all zero.
2031 : : Don't do this if optimizing for size and we have umulv4_optab,
2032 : : in that case assume multiplication will be shorter.
2033 : : This is heuristics based on the single target that provides
2034 : : umulv4 right now (i?86/x86_64), if further targets add it, this
2035 : : might need to be revisited.
2036 : : Cases where both operands are constant should be folded already
2037 : : during GIMPLE, and cases where one operand is constant but not
2038 : : power of 2 are questionable, either the WIDEN_MULT_EXPR case
2039 : : below can be done without multiplication, just by shifts and adds,
2040 : : or we'd need to divide the result (and hope it actually doesn't
2041 : : really divide nor multiply) and compare the result of the division
2042 : : with the original operand. */
2043 : 862 : rtx opn0 = op0;
2044 : 862 : rtx opn1 = op1;
2045 : 862 : tree argn0 = arg0;
2046 : 862 : tree argn1 = arg1;
2047 : 862 : if (integer_pow2p (arg0))
2048 : : {
2049 : 0 : std::swap (opn0, opn1);
2050 : 0 : std::swap (argn0, argn1);
2051 : : }
2052 : 862 : int cnt = tree_log2 (argn1);
2053 : 862 : if (cnt >= 0 && cnt < GET_MODE_PRECISION (mode))
2054 : : {
2055 : 862 : rtx upper = const0_rtx;
2056 : 862 : res = expand_shift (LSHIFT_EXPR, mode, opn0, cnt, NULL_RTX, uns);
2057 : 862 : if (cnt != 0)
2058 : 690 : upper = expand_shift (RSHIFT_EXPR, mode, opn0,
2059 : 690 : GET_MODE_PRECISION (mode) - cnt,
2060 : : NULL_RTX, uns);
2061 : 862 : do_compare_rtx_and_jump (upper, const0_rtx, EQ, true, mode,
2062 : : NULL_RTX, NULL, done_label,
2063 : : profile_probability::very_likely ());
2064 : 862 : goto do_error_label;
2065 : : }
2066 : : }
2067 : 16457 : if (icode != CODE_FOR_nothing)
2068 : : {
2069 : 13538 : class expand_operand ops[4];
2070 : 13538 : rtx_insn *last = get_last_insn ();
2071 : :
2072 : 13538 : res = gen_reg_rtx (mode);
2073 : 13538 : create_output_operand (&ops[0], res, mode);
2074 : 13538 : create_input_operand (&ops[1], op0, mode);
2075 : 13538 : create_input_operand (&ops[2], op1, mode);
2076 : 13538 : create_fixed_operand (&ops[3], do_error);
2077 : 13538 : if (maybe_expand_insn (icode, 4, ops))
2078 : : {
2079 : 13538 : last = get_last_insn ();
2080 : 13538 : if (profile_status_for_fn (cfun) != PROFILE_ABSENT
2081 : 9336 : && JUMP_P (last)
2082 : 9336 : && any_condjump_p (last)
2083 : 22874 : && !find_reg_note (last, REG_BR_PROB, 0))
2084 : 9336 : add_reg_br_prob_note (last,
2085 : : profile_probability::very_unlikely ());
2086 : 13538 : emit_jump (done_label);
2087 : : }
2088 : : else
2089 : : {
2090 : 0 : delete_insns_since (last);
2091 : 0 : icode = CODE_FOR_nothing;
2092 : : }
2093 : : }
2094 : :
2095 : 0 : if (icode == CODE_FOR_nothing)
2096 : : {
2097 : 2919 : struct separate_ops ops;
2098 : 2919 : int prec = GET_MODE_PRECISION (mode);
2099 : 2919 : scalar_int_mode hmode, wmode;
2100 : 2919 : ops.op0 = make_tree (type, op0);
2101 : 2919 : ops.op1 = make_tree (type, op1);
2102 : 2919 : ops.op2 = NULL_TREE;
2103 : 2919 : ops.location = loc;
2104 : :
2105 : : /* Optimize unsigned overflow check where we don't use the
2106 : : multiplication result, just whether overflow happened.
2107 : : If we can do MULT_HIGHPART_EXPR, that followed by
2108 : : comparison of the result against zero is cheapest.
2109 : : We'll still compute res, but it should be DCEd later. */
2110 : 2919 : use_operand_p use;
2111 : 2919 : gimple *use_stmt;
2112 : 2919 : if (!is_ubsan
2113 : 2919 : && lhs
2114 : 2899 : && uns
2115 : 1859 : && !(uns0_p && uns1_p && !unsr_p)
2116 : 1574 : && can_mult_highpart_p (mode, uns) == 1
2117 : 0 : && single_imm_use (lhs, &use, &use_stmt)
2118 : 0 : && is_gimple_assign (use_stmt)
2119 : 2919 : && gimple_assign_rhs_code (use_stmt) == IMAGPART_EXPR)
2120 : 0 : goto highpart;
2121 : :
2122 : 2919 : if (GET_MODE_2XWIDER_MODE (mode).exists (&wmode)
2123 : 2919 : && targetm.scalar_mode_supported_p (wmode)
2124 : 0 : && can_widen_mult_without_libcall (wmode, mode, op0, op1, uns))
2125 : : {
2126 : 0 : twoxwider:
2127 : 0 : ops.code = WIDEN_MULT_EXPR;
2128 : 0 : ops.type
2129 : 0 : = build_nonstandard_integer_type (GET_MODE_PRECISION (wmode), uns);
2130 : :
2131 : 0 : res = expand_expr_real_2 (&ops, NULL_RTX, wmode, EXPAND_NORMAL);
2132 : 0 : rtx hipart = expand_shift (RSHIFT_EXPR, wmode, res, prec,
2133 : : NULL_RTX, uns);
2134 : 0 : hipart = convert_modes (mode, wmode, hipart, uns);
2135 : 0 : res = convert_modes (mode, wmode, res, uns);
2136 : 0 : if (uns)
2137 : : /* For the unsigned multiplication, there was overflow if
2138 : : HIPART is non-zero. */
2139 : 0 : do_compare_rtx_and_jump (hipart, const0_rtx, EQ, true, mode,
2140 : : NULL_RTX, NULL, done_label,
2141 : : profile_probability::very_likely ());
2142 : : else
2143 : : {
2144 : : /* RES is used more than once, place it in a pseudo. */
2145 : 0 : res = force_reg (mode, res);
2146 : :
2147 : 0 : rtx signbit = expand_shift (RSHIFT_EXPR, mode, res, prec - 1,
2148 : : NULL_RTX, 0);
2149 : : /* RES is low half of the double width result, HIPART
2150 : : the high half. There was overflow if
2151 : : HIPART is different from RES < 0 ? -1 : 0. */
2152 : 0 : do_compare_rtx_and_jump (signbit, hipart, EQ, true, mode,
2153 : : NULL_RTX, NULL, done_label,
2154 : : profile_probability::very_likely ());
2155 : : }
2156 : : }
2157 : 2919 : else if (can_mult_highpart_p (mode, uns) == 1)
2158 : : {
2159 : 0 : highpart:
2160 : 0 : ops.code = MULT_HIGHPART_EXPR;
2161 : 0 : ops.type = type;
2162 : :
2163 : 0 : rtx hipart = expand_expr_real_2 (&ops, NULL_RTX, mode,
2164 : : EXPAND_NORMAL);
2165 : 0 : ops.code = MULT_EXPR;
2166 : 0 : res = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL);
2167 : 0 : if (uns)
2168 : : /* For the unsigned multiplication, there was overflow if
2169 : : HIPART is non-zero. */
2170 : 0 : do_compare_rtx_and_jump (hipart, const0_rtx, EQ, true, mode,
2171 : : NULL_RTX, NULL, done_label,
2172 : : profile_probability::very_likely ());
2173 : : else
2174 : : {
2175 : 0 : rtx signbit = expand_shift (RSHIFT_EXPR, mode, res, prec - 1,
2176 : : NULL_RTX, 0);
2177 : : /* RES is low half of the double width result, HIPART
2178 : : the high half. There was overflow if
2179 : : HIPART is different from RES < 0 ? -1 : 0. */
2180 : 0 : do_compare_rtx_and_jump (signbit, hipart, EQ, true, mode,
2181 : : NULL_RTX, NULL, done_label,
2182 : : profile_probability::very_likely ());
2183 : : }
2184 : :
2185 : : }
2186 : 2919 : else if (int_mode_for_size (prec / 2, 1).exists (&hmode)
2187 : 2919 : && 2 * GET_MODE_PRECISION (hmode) == prec)
2188 : : {
2189 : 2919 : rtx_code_label *large_op0 = gen_label_rtx ();
2190 : 2919 : rtx_code_label *small_op0_large_op1 = gen_label_rtx ();
2191 : 2919 : rtx_code_label *one_small_one_large = gen_label_rtx ();
2192 : 2919 : rtx_code_label *both_ops_large = gen_label_rtx ();
2193 : 2919 : rtx_code_label *after_hipart_neg = uns ? NULL : gen_label_rtx ();
2194 : 1060 : rtx_code_label *after_lopart_neg = uns ? NULL : gen_label_rtx ();
2195 : 2919 : rtx_code_label *do_overflow = gen_label_rtx ();
2196 : 2919 : rtx_code_label *hipart_different = uns ? NULL : gen_label_rtx ();
2197 : :
2198 : 2919 : unsigned int hprec = GET_MODE_PRECISION (hmode);
2199 : 2919 : rtx hipart0 = expand_shift (RSHIFT_EXPR, mode, op0, hprec,
2200 : : NULL_RTX, uns);
2201 : 2919 : hipart0 = convert_modes (hmode, mode, hipart0, uns);
2202 : 2919 : rtx lopart0 = convert_modes (hmode, mode, op0, uns);
2203 : 2919 : rtx signbit0 = const0_rtx;
2204 : 2919 : if (!uns)
2205 : 1060 : signbit0 = expand_shift (RSHIFT_EXPR, hmode, lopart0, hprec - 1,
2206 : : NULL_RTX, 0);
2207 : 2919 : rtx hipart1 = expand_shift (RSHIFT_EXPR, mode, op1, hprec,
2208 : : NULL_RTX, uns);
2209 : 2919 : hipart1 = convert_modes (hmode, mode, hipart1, uns);
2210 : 2919 : rtx lopart1 = convert_modes (hmode, mode, op1, uns);
2211 : 2919 : rtx signbit1 = const0_rtx;
2212 : 2919 : if (!uns)
2213 : 1060 : signbit1 = expand_shift (RSHIFT_EXPR, hmode, lopart1, hprec - 1,
2214 : : NULL_RTX, 0);
2215 : :
2216 : 2919 : res = gen_reg_rtx (mode);
2217 : :
2218 : : /* True if op0 resp. op1 are known to be in the range of
2219 : : halfstype. */
2220 : 2919 : bool op0_small_p = false;
2221 : 2919 : bool op1_small_p = false;
2222 : : /* True if op0 resp. op1 are known to have all zeros or all ones
2223 : : in the upper half of bits, but are not known to be
2224 : : op{0,1}_small_p. */
2225 : 2919 : bool op0_medium_p = false;
2226 : 2919 : bool op1_medium_p = false;
2227 : : /* -1 if op{0,1} is known to be negative, 0 if it is known to be
2228 : : nonnegative, 1 if unknown. */
2229 : 2919 : int op0_sign = 1;
2230 : 2919 : int op1_sign = 1;
2231 : :
2232 : 2919 : if (pos_neg0 == 1)
2233 : : op0_sign = 0;
2234 : 2594 : else if (pos_neg0 == 2)
2235 : 299 : op0_sign = -1;
2236 : 2919 : if (pos_neg1 == 1)
2237 : : op1_sign = 0;
2238 : 1633 : else if (pos_neg1 == 2)
2239 : 499 : op1_sign = -1;
2240 : :
2241 : 2919 : unsigned int mprec0 = prec;
2242 : 2919 : if (arg0 != error_mark_node)
2243 : 2275 : mprec0 = get_min_precision (arg0, sign);
2244 : 2919 : if (mprec0 <= hprec)
2245 : : op0_small_p = true;
2246 : 2672 : else if (!uns && mprec0 <= hprec + 1)
2247 : 2919 : op0_medium_p = true;
2248 : 2919 : unsigned int mprec1 = prec;
2249 : 2919 : if (arg1 != error_mark_node)
2250 : 2275 : mprec1 = get_min_precision (arg1, sign);
2251 : 2919 : if (mprec1 <= hprec)
2252 : : op1_small_p = true;
2253 : 2226 : else if (!uns && mprec1 <= hprec + 1)
2254 : 2919 : op1_medium_p = true;
2255 : :
2256 : 2919 : int smaller_sign = 1;
2257 : 2919 : int larger_sign = 1;
2258 : 2919 : if (op0_small_p)
2259 : : {
2260 : : smaller_sign = op0_sign;
2261 : : larger_sign = op1_sign;
2262 : : }
2263 : 2672 : else if (op1_small_p)
2264 : : {
2265 : : smaller_sign = op1_sign;
2266 : : larger_sign = op0_sign;
2267 : : }
2268 : 1979 : else if (op0_sign == op1_sign)
2269 : : {
2270 : 923 : smaller_sign = op0_sign;
2271 : 923 : larger_sign = op0_sign;
2272 : : }
2273 : :
2274 : 2672 : if (!op0_small_p)
2275 : 2672 : do_compare_rtx_and_jump (signbit0, hipart0, NE, true, hmode,
2276 : : NULL_RTX, NULL, large_op0,
2277 : : profile_probability::unlikely ());
2278 : :
2279 : 2919 : if (!op1_small_p)
2280 : 2226 : do_compare_rtx_and_jump (signbit1, hipart1, NE, true, hmode,
2281 : : NULL_RTX, NULL, small_op0_large_op1,
2282 : : profile_probability::unlikely ());
2283 : :
2284 : : /* If both op0 and op1 are sign (!uns) or zero (uns) extended from
2285 : : hmode to mode, the multiplication will never overflow. We can
2286 : : do just one hmode x hmode => mode widening multiplication. */
2287 : 2919 : tree halfstype = build_nonstandard_integer_type (hprec, uns);
2288 : 2919 : ops.op0 = make_tree (halfstype, lopart0);
2289 : 2919 : ops.op1 = make_tree (halfstype, lopart1);
2290 : 2919 : ops.code = WIDEN_MULT_EXPR;
2291 : 2919 : ops.type = type;
2292 : 2919 : rtx thisres
2293 : 2919 : = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL);
2294 : 2919 : emit_move_insn (res, thisres);
2295 : 2919 : emit_jump (done_label);
2296 : :
2297 : 2919 : emit_label (small_op0_large_op1);
2298 : :
2299 : : /* If op0 is sign (!uns) or zero (uns) extended from hmode to mode,
2300 : : but op1 is not, just swap the arguments and handle it as op1
2301 : : sign/zero extended, op0 not. */
2302 : 2919 : rtx larger = gen_reg_rtx (mode);
2303 : 2919 : rtx hipart = gen_reg_rtx (hmode);
2304 : 2919 : rtx lopart = gen_reg_rtx (hmode);
2305 : 2919 : emit_move_insn (larger, op1);
2306 : 2919 : emit_move_insn (hipart, hipart1);
2307 : 2919 : emit_move_insn (lopart, lopart0);
2308 : 2919 : emit_jump (one_small_one_large);
2309 : :
2310 : 2919 : emit_label (large_op0);
2311 : :
2312 : 2919 : if (!op1_small_p)
2313 : 2226 : do_compare_rtx_and_jump (signbit1, hipart1, NE, true, hmode,
2314 : : NULL_RTX, NULL, both_ops_large,
2315 : : profile_probability::unlikely ());
2316 : :
2317 : : /* If op1 is sign (!uns) or zero (uns) extended from hmode to mode,
2318 : : but op0 is not, prepare larger, hipart and lopart pseudos and
2319 : : handle it together with small_op0_large_op1. */
2320 : 2919 : emit_move_insn (larger, op0);
2321 : 2919 : emit_move_insn (hipart, hipart0);
2322 : 2919 : emit_move_insn (lopart, lopart1);
2323 : :
2324 : 2919 : emit_label (one_small_one_large);
2325 : :
2326 : : /* lopart is the low part of the operand that is sign extended
2327 : : to mode, larger is the other operand, hipart is the
2328 : : high part of larger and lopart0 and lopart1 are the low parts
2329 : : of both operands.
2330 : : We perform lopart0 * lopart1 and lopart * hipart widening
2331 : : multiplications. */
2332 : 2919 : tree halfutype = build_nonstandard_integer_type (hprec, 1);
2333 : 2919 : ops.op0 = make_tree (halfutype, lopart0);
2334 : 2919 : ops.op1 = make_tree (halfutype, lopart1);
2335 : 2919 : rtx lo0xlo1
2336 : 2919 : = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL);
2337 : :
2338 : 2919 : ops.op0 = make_tree (halfutype, lopart);
2339 : 2919 : ops.op1 = make_tree (halfutype, hipart);
2340 : 2919 : rtx loxhi = gen_reg_rtx (mode);
2341 : 2919 : rtx tem = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL);
2342 : 2919 : emit_move_insn (loxhi, tem);
2343 : :
2344 : 2919 : if (!uns)
2345 : : {
2346 : : /* if (hipart < 0) loxhi -= lopart << (bitsize / 2); */
2347 : 1060 : if (larger_sign == 0)
2348 : 111 : emit_jump (after_hipart_neg);
2349 : 949 : else if (larger_sign != -1)
2350 : 944 : do_compare_rtx_and_jump (hipart, const0_rtx, GE, false, hmode,
2351 : : NULL_RTX, NULL, after_hipart_neg,
2352 : : profile_probability::even ());
2353 : :
2354 : 1060 : tem = convert_modes (mode, hmode, lopart, 1);
2355 : 1060 : tem = expand_shift (LSHIFT_EXPR, mode, tem, hprec, NULL_RTX, 1);
2356 : 1060 : tem = expand_simple_binop (mode, MINUS, loxhi, tem, NULL_RTX,
2357 : : 1, OPTAB_WIDEN);
2358 : 1060 : emit_move_insn (loxhi, tem);
2359 : :
2360 : 1060 : emit_label (after_hipart_neg);
2361 : :
2362 : : /* if (lopart < 0) loxhi -= larger; */
2363 : 1060 : if (smaller_sign == 0)
2364 : 325 : emit_jump (after_lopart_neg);
2365 : 735 : else if (smaller_sign != -1)
2366 : 530 : do_compare_rtx_and_jump (lopart, const0_rtx, GE, false, hmode,
2367 : : NULL_RTX, NULL, after_lopart_neg,
2368 : : profile_probability::even ());
2369 : :
2370 : 1060 : tem = expand_simple_binop (mode, MINUS, loxhi, larger, NULL_RTX,
2371 : : 1, OPTAB_WIDEN);
2372 : 1060 : emit_move_insn (loxhi, tem);
2373 : :
2374 : 1060 : emit_label (after_lopart_neg);
2375 : : }
2376 : :
2377 : : /* loxhi += (uns) lo0xlo1 >> (bitsize / 2); */
2378 : 2919 : tem = expand_shift (RSHIFT_EXPR, mode, lo0xlo1, hprec, NULL_RTX, 1);
2379 : 2919 : tem = expand_simple_binop (mode, PLUS, loxhi, tem, NULL_RTX,
2380 : : 1, OPTAB_WIDEN);
2381 : 2919 : emit_move_insn (loxhi, tem);
2382 : :
2383 : : /* if (loxhi >> (bitsize / 2)
2384 : : == (hmode) loxhi >> (bitsize / 2 - 1)) (if !uns)
2385 : : if (loxhi >> (bitsize / 2) == 0 (if uns). */
2386 : 2919 : rtx hipartloxhi = expand_shift (RSHIFT_EXPR, mode, loxhi, hprec,
2387 : : NULL_RTX, 0);
2388 : 2919 : hipartloxhi = convert_modes (hmode, mode, hipartloxhi, 0);
2389 : 2919 : rtx signbitloxhi = const0_rtx;
2390 : 2919 : if (!uns)
2391 : 1060 : signbitloxhi = expand_shift (RSHIFT_EXPR, hmode,
2392 : : convert_modes (hmode, mode,
2393 : : loxhi, 0),
2394 : 1060 : hprec - 1, NULL_RTX, 0);
2395 : :
2396 : 2919 : do_compare_rtx_and_jump (signbitloxhi, hipartloxhi, NE, true, hmode,
2397 : : NULL_RTX, NULL, do_overflow,
2398 : : profile_probability::very_unlikely ());
2399 : :
2400 : : /* res = (loxhi << (bitsize / 2)) | (hmode) lo0xlo1; */
2401 : 2919 : rtx loxhishifted = expand_shift (LSHIFT_EXPR, mode, loxhi, hprec,
2402 : : NULL_RTX, 1);
2403 : 2919 : tem = convert_modes (mode, hmode,
2404 : : convert_modes (hmode, mode, lo0xlo1, 1), 1);
2405 : :
2406 : 2919 : tem = expand_simple_binop (mode, IOR, loxhishifted, tem, res,
2407 : : 1, OPTAB_WIDEN);
2408 : 2919 : if (tem != res)
2409 : 0 : emit_move_insn (res, tem);
2410 : 2919 : emit_jump (done_label);
2411 : :
2412 : 2919 : emit_label (both_ops_large);
2413 : :
2414 : : /* If both operands are large (not sign (!uns) or zero (uns)
2415 : : extended from hmode), then perform the full multiplication
2416 : : which will be the result of the operation.
2417 : : The only cases which don't overflow are for signed multiplication
2418 : : some cases where both hipart0 and highpart1 are 0 or -1.
2419 : : For unsigned multiplication when high parts are both non-zero
2420 : : this overflows always. */
2421 : 2919 : ops.code = MULT_EXPR;
2422 : 2919 : ops.op0 = make_tree (type, op0);
2423 : 2919 : ops.op1 = make_tree (type, op1);
2424 : 2919 : tem = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL);
2425 : 2919 : emit_move_insn (res, tem);
2426 : :
2427 : 2919 : if (!uns)
2428 : : {
2429 : 1060 : if (!op0_medium_p)
2430 : : {
2431 : 1060 : tem = expand_simple_binop (hmode, PLUS, hipart0, const1_rtx,
2432 : : NULL_RTX, 1, OPTAB_WIDEN);
2433 : 1060 : do_compare_rtx_and_jump (tem, const1_rtx, GTU, true, hmode,
2434 : : NULL_RTX, NULL, do_error,
2435 : : profile_probability::very_unlikely ());
2436 : : }
2437 : :
2438 : 1060 : if (!op1_medium_p)
2439 : : {
2440 : 1060 : tem = expand_simple_binop (hmode, PLUS, hipart1, const1_rtx,
2441 : : NULL_RTX, 1, OPTAB_WIDEN);
2442 : 1060 : do_compare_rtx_and_jump (tem, const1_rtx, GTU, true, hmode,
2443 : : NULL_RTX, NULL, do_error,
2444 : : profile_probability::very_unlikely ());
2445 : : }
2446 : :
2447 : : /* At this point hipart{0,1} are both in [-1, 0]. If they are
2448 : : the same, overflow happened if res is non-positive, if they
2449 : : are different, overflow happened if res is positive. */
2450 : 1060 : if (op0_sign != 1 && op1_sign != 1 && op0_sign != op1_sign)
2451 : 23 : emit_jump (hipart_different);
2452 : 1037 : else if (op0_sign == 1 || op1_sign == 1)
2453 : 935 : do_compare_rtx_and_jump (hipart0, hipart1, NE, true, hmode,
2454 : : NULL_RTX, NULL, hipart_different,
2455 : : profile_probability::even ());
2456 : :
2457 : 1060 : do_compare_rtx_and_jump (res, const0_rtx, LE, false, mode,
2458 : : NULL_RTX, NULL, do_error,
2459 : : profile_probability::very_unlikely ());
2460 : 1060 : emit_jump (done_label);
2461 : :
2462 : 1060 : emit_label (hipart_different);
2463 : :
2464 : 1060 : do_compare_rtx_and_jump (res, const0_rtx, GE, false, mode,
2465 : : NULL_RTX, NULL, do_error,
2466 : : profile_probability::very_unlikely ());
2467 : 1060 : emit_jump (done_label);
2468 : : }
2469 : :
2470 : 2919 : emit_label (do_overflow);
2471 : :
2472 : : /* Overflow, do full multiplication and fallthru into do_error. */
2473 : 2919 : ops.op0 = make_tree (type, op0);
2474 : 2919 : ops.op1 = make_tree (type, op1);
2475 : 2919 : tem = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL);
2476 : 2919 : emit_move_insn (res, tem);
2477 : : }
2478 : 0 : else if (GET_MODE_2XWIDER_MODE (mode).exists (&wmode)
2479 : 0 : && targetm.scalar_mode_supported_p (wmode))
2480 : : /* Even emitting a libcall is better than not detecting overflow
2481 : : at all. */
2482 : 0 : goto twoxwider;
2483 : : else
2484 : : {
2485 : 0 : gcc_assert (!is_ubsan);
2486 : 0 : ops.code = MULT_EXPR;
2487 : 0 : ops.type = type;
2488 : 0 : res = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL);
2489 : 0 : emit_jump (done_label);
2490 : : }
2491 : : }
2492 : :
2493 : 13538 : do_error_label:
2494 : 18808 : emit_label (do_error);
2495 : 18808 : if (is_ubsan)
2496 : : {
2497 : : /* Expand the ubsan builtin call. */
2498 : 1500 : push_temp_slots ();
2499 : 1500 : fn = ubsan_build_overflow_builtin (MULT_EXPR, loc, TREE_TYPE (arg0),
2500 : : arg0, arg1, datap);
2501 : 1500 : expand_normal (fn);
2502 : 1500 : pop_temp_slots ();
2503 : 1500 : do_pending_stack_adjust ();
2504 : : }
2505 : 17308 : else if (lhs)
2506 : 17308 : expand_arith_set_overflow (lhs, target);
2507 : :
2508 : : /* We're done. */
2509 : 18808 : emit_label (done_label);
2510 : :
2511 : : /* u1 * u2 -> sr */
2512 : 18808 : if (uns0_p && uns1_p && !unsr_p)
2513 : : {
2514 : 1037 : rtx_code_label *all_done_label = gen_label_rtx ();
2515 : 1037 : do_compare_rtx_and_jump (res, const0_rtx, GE, false, mode, NULL_RTX,
2516 : : NULL, all_done_label, profile_probability::very_likely ());
2517 : 1037 : expand_arith_set_overflow (lhs, target);
2518 : 1037 : emit_label (all_done_label);
2519 : : }
2520 : :
2521 : : /* s1 * u2 -> sr */
2522 : 18808 : if (!uns0_p && uns1_p && !unsr_p && pos_neg1 == 3)
2523 : : {
2524 : 968 : rtx_code_label *all_done_label = gen_label_rtx ();
2525 : 968 : rtx_code_label *set_noovf = gen_label_rtx ();
2526 : 968 : do_compare_rtx_and_jump (op1, const0_rtx, GE, false, mode, NULL_RTX,
2527 : : NULL, all_done_label, profile_probability::very_likely ());
2528 : 968 : expand_arith_set_overflow (lhs, target);
2529 : 968 : do_compare_rtx_and_jump (op0, const0_rtx, EQ, true, mode, NULL_RTX,
2530 : : NULL, set_noovf, profile_probability::very_likely ());
2531 : 968 : do_compare_rtx_and_jump (op0, constm1_rtx, NE, true, mode, NULL_RTX,
2532 : : NULL, all_done_label, profile_probability::very_unlikely ());
2533 : 968 : do_compare_rtx_and_jump (op1, res, NE, true, mode, NULL_RTX, NULL,
2534 : : all_done_label, profile_probability::very_unlikely ());
2535 : 968 : emit_label (set_noovf);
2536 : 968 : write_complex_part (target, const0_rtx, true, false);
2537 : 968 : emit_label (all_done_label);
2538 : : }
2539 : :
2540 : 18808 : if (lhs)
2541 : : {
2542 : 18664 : if (is_ubsan)
2543 : 1356 : expand_ubsan_result_store (lhs, target, mode, res, do_error);
2544 : : else
2545 : 17308 : expand_arith_overflow_result_store (lhs, target, mode, res);
2546 : : }
2547 : 18808 : flag_trapv = save_flag_trapv;
2548 : 18808 : }
2549 : :
2550 : : /* Expand UBSAN_CHECK_* internal function if it has vector operands. */
2551 : :
2552 : : static void
2553 : 549 : expand_vector_ubsan_overflow (location_t loc, enum tree_code code, tree lhs,
2554 : : tree arg0, tree arg1)
2555 : : {
2556 : 549 : poly_uint64 cnt = TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0));
2557 : 549 : rtx_code_label *loop_lab = NULL;
2558 : 549 : rtx cntvar = NULL_RTX;
2559 : 549 : tree cntv = NULL_TREE;
2560 : 549 : tree eltype = TREE_TYPE (TREE_TYPE (arg0));
2561 : 549 : tree sz = TYPE_SIZE (eltype);
2562 : 549 : tree data = NULL_TREE;
2563 : 549 : tree resv = NULL_TREE;
2564 : 549 : rtx lhsr = NULL_RTX;
2565 : 549 : rtx resvr = NULL_RTX;
2566 : 549 : unsigned HOST_WIDE_INT const_cnt = 0;
2567 : 549 : bool use_loop_p = (!cnt.is_constant (&const_cnt) || const_cnt > 4);
2568 : 549 : int save_flag_trapv = flag_trapv;
2569 : :
2570 : : /* We don't want any __mulv?i3 etc. calls from the expansion of
2571 : : these internal functions, so disable -ftrapv temporarily. */
2572 : 549 : flag_trapv = 0;
2573 : 549 : if (lhs)
2574 : : {
2575 : 549 : optab op;
2576 : 549 : lhsr = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
2577 : 141 : if (!VECTOR_MODE_P (GET_MODE (lhsr))
2578 : 408 : || (op = optab_for_tree_code (code, TREE_TYPE (arg0),
2579 : : optab_default)) == unknown_optab
2580 : 957 : || (optab_handler (op, TYPE_MODE (TREE_TYPE (arg0)))
2581 : : == CODE_FOR_nothing))
2582 : : {
2583 : 141 : if (MEM_P (lhsr))
2584 : 141 : resv = make_tree (TREE_TYPE (lhs), lhsr);
2585 : : else
2586 : : {
2587 : 0 : resvr = assign_temp (TREE_TYPE (lhs), 1, 1);
2588 : 0 : resv = make_tree (TREE_TYPE (lhs), resvr);
2589 : : }
2590 : : }
2591 : : }
2592 : 549 : if (use_loop_p)
2593 : : {
2594 : 417 : do_pending_stack_adjust ();
2595 : 417 : loop_lab = gen_label_rtx ();
2596 : 417 : cntvar = gen_reg_rtx (TYPE_MODE (sizetype));
2597 : 417 : cntv = make_tree (sizetype, cntvar);
2598 : 417 : emit_move_insn (cntvar, const0_rtx);
2599 : 417 : emit_label (loop_lab);
2600 : : }
2601 : 549 : if (TREE_CODE (arg0) != VECTOR_CST)
2602 : : {
2603 : 461 : rtx arg0r = expand_normal (arg0);
2604 : 461 : arg0 = make_tree (TREE_TYPE (arg0), arg0r);
2605 : : }
2606 : 549 : if (TREE_CODE (arg1) != VECTOR_CST)
2607 : : {
2608 : 537 : rtx arg1r = expand_normal (arg1);
2609 : 537 : arg1 = make_tree (TREE_TYPE (arg1), arg1r);
2610 : : }
2611 : 2154 : for (unsigned int i = 0; i < (use_loop_p ? 1 : const_cnt); i++)
2612 : : {
2613 : 945 : tree op0, op1, res = NULL_TREE;
2614 : 945 : if (use_loop_p)
2615 : : {
2616 : 417 : tree atype = build_array_type_nelts (eltype, cnt);
2617 : 417 : op0 = uniform_vector_p (arg0);
2618 : 417 : if (op0 == NULL_TREE)
2619 : : {
2620 : 351 : op0 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, atype, arg0);
2621 : 351 : op0 = build4_loc (loc, ARRAY_REF, eltype, op0, cntv,
2622 : : NULL_TREE, NULL_TREE);
2623 : : }
2624 : 417 : op1 = uniform_vector_p (arg1);
2625 : 417 : if (op1 == NULL_TREE)
2626 : : {
2627 : 411 : op1 = fold_build1_loc (loc, VIEW_CONVERT_EXPR, atype, arg1);
2628 : 411 : op1 = build4_loc (loc, ARRAY_REF, eltype, op1, cntv,
2629 : : NULL_TREE, NULL_TREE);
2630 : : }
2631 : 417 : if (resv)
2632 : : {
2633 : 141 : res = fold_build1_loc (loc, VIEW_CONVERT_EXPR, atype, resv);
2634 : 141 : res = build4_loc (loc, ARRAY_REF, eltype, res, cntv,
2635 : : NULL_TREE, NULL_TREE);
2636 : : }
2637 : : }
2638 : : else
2639 : : {
2640 : 528 : tree bitpos = bitsize_int (tree_to_uhwi (sz) * i);
2641 : 528 : op0 = fold_build3_loc (loc, BIT_FIELD_REF, eltype, arg0, sz, bitpos);
2642 : 528 : op1 = fold_build3_loc (loc, BIT_FIELD_REF, eltype, arg1, sz, bitpos);
2643 : 528 : if (resv)
2644 : 0 : res = fold_build3_loc (loc, BIT_FIELD_REF, eltype, resv, sz,
2645 : : bitpos);
2646 : : }
2647 : 945 : switch (code)
2648 : : {
2649 : 317 : case PLUS_EXPR:
2650 : 317 : expand_addsub_overflow (loc, PLUS_EXPR, res, op0, op1,
2651 : : false, false, false, true, &data);
2652 : 317 : break;
2653 : 462 : case MINUS_EXPR:
2654 : 462 : if (use_loop_p ? integer_zerop (arg0) : integer_zerop (op0))
2655 : 154 : expand_neg_overflow (loc, res, op1, true, &data);
2656 : : else
2657 : 308 : expand_addsub_overflow (loc, MINUS_EXPR, res, op0, op1,
2658 : : false, false, false, true, &data);
2659 : : break;
2660 : 166 : case MULT_EXPR:
2661 : 166 : expand_mul_overflow (loc, res, op0, op1, false, false, false,
2662 : : true, &data);
2663 : 166 : break;
2664 : 0 : default:
2665 : 0 : gcc_unreachable ();
2666 : : }
2667 : : }
2668 : 549 : if (use_loop_p)
2669 : : {
2670 : 417 : struct separate_ops ops;
2671 : 417 : ops.code = PLUS_EXPR;
2672 : 417 : ops.type = TREE_TYPE (cntv);
2673 : 417 : ops.op0 = cntv;
2674 : 417 : ops.op1 = build_int_cst (TREE_TYPE (cntv), 1);
2675 : 417 : ops.op2 = NULL_TREE;
2676 : 417 : ops.location = loc;
2677 : 417 : rtx ret = expand_expr_real_2 (&ops, cntvar, TYPE_MODE (sizetype),
2678 : : EXPAND_NORMAL);
2679 : 417 : if (ret != cntvar)
2680 : 0 : emit_move_insn (cntvar, ret);
2681 : 417 : rtx cntrtx = gen_int_mode (cnt, TYPE_MODE (sizetype));
2682 : 834 : do_compare_rtx_and_jump (cntvar, cntrtx, NE, false,
2683 : 417 : TYPE_MODE (sizetype), NULL_RTX, NULL, loop_lab,
2684 : : profile_probability::very_likely ());
2685 : : }
2686 : 549 : if (lhs && resv == NULL_TREE)
2687 : : {
2688 : 408 : struct separate_ops ops;
2689 : 408 : ops.code = code;
2690 : 408 : ops.type = TREE_TYPE (arg0);
2691 : 408 : ops.op0 = arg0;
2692 : 408 : ops.op1 = arg1;
2693 : 408 : ops.op2 = NULL_TREE;
2694 : 408 : ops.location = loc;
2695 : 408 : rtx ret = expand_expr_real_2 (&ops, lhsr, TYPE_MODE (TREE_TYPE (arg0)),
2696 : : EXPAND_NORMAL);
2697 : 408 : if (ret != lhsr)
2698 : 5 : emit_move_insn (lhsr, ret);
2699 : : }
2700 : 141 : else if (resvr)
2701 : 0 : emit_move_insn (lhsr, resvr);
2702 : 549 : flag_trapv = save_flag_trapv;
2703 : 549 : }
2704 : :
2705 : : /* Expand UBSAN_CHECK_ADD call STMT. */
2706 : :
2707 : : static void
2708 : 1767 : expand_UBSAN_CHECK_ADD (internal_fn, gcall *stmt)
2709 : : {
2710 : 1767 : location_t loc = gimple_location (stmt);
2711 : 1767 : tree lhs = gimple_call_lhs (stmt);
2712 : 1767 : tree arg0 = gimple_call_arg (stmt, 0);
2713 : 1767 : tree arg1 = gimple_call_arg (stmt, 1);
2714 : 1767 : if (VECTOR_TYPE_P (TREE_TYPE (arg0)))
2715 : 185 : expand_vector_ubsan_overflow (loc, PLUS_EXPR, lhs, arg0, arg1);
2716 : : else
2717 : 1582 : expand_addsub_overflow (loc, PLUS_EXPR, lhs, arg0, arg1,
2718 : : false, false, false, true, NULL);
2719 : 1767 : }
2720 : :
2721 : : /* Expand UBSAN_CHECK_SUB call STMT. */
2722 : :
2723 : : static void
2724 : 1810 : expand_UBSAN_CHECK_SUB (internal_fn, gcall *stmt)
2725 : : {
2726 : 1810 : location_t loc = gimple_location (stmt);
2727 : 1810 : tree lhs = gimple_call_lhs (stmt);
2728 : 1810 : tree arg0 = gimple_call_arg (stmt, 0);
2729 : 1810 : tree arg1 = gimple_call_arg (stmt, 1);
2730 : 1810 : if (VECTOR_TYPE_P (TREE_TYPE (arg0)))
2731 : 264 : expand_vector_ubsan_overflow (loc, MINUS_EXPR, lhs, arg0, arg1);
2732 : 1546 : else if (integer_zerop (arg0))
2733 : 416 : expand_neg_overflow (loc, lhs, arg1, true, NULL);
2734 : : else
2735 : 1130 : expand_addsub_overflow (loc, MINUS_EXPR, lhs, arg0, arg1,
2736 : : false, false, false, true, NULL);
2737 : 1810 : }
2738 : :
2739 : : /* Expand UBSAN_CHECK_MUL call STMT. */
2740 : :
2741 : : static void
2742 : 1434 : expand_UBSAN_CHECK_MUL (internal_fn, gcall *stmt)
2743 : : {
2744 : 1434 : location_t loc = gimple_location (stmt);
2745 : 1434 : tree lhs = gimple_call_lhs (stmt);
2746 : 1434 : tree arg0 = gimple_call_arg (stmt, 0);
2747 : 1434 : tree arg1 = gimple_call_arg (stmt, 1);
2748 : 1434 : if (VECTOR_TYPE_P (TREE_TYPE (arg0)))
2749 : 100 : expand_vector_ubsan_overflow (loc, MULT_EXPR, lhs, arg0, arg1);
2750 : : else
2751 : 1334 : expand_mul_overflow (loc, lhs, arg0, arg1, false, false, false, true,
2752 : : NULL);
2753 : 1434 : }
2754 : :
2755 : : /* Helper function for {ADD,SUB,MUL}_OVERFLOW call stmt expansion. */
2756 : :
2757 : : static void
2758 : 72187 : expand_arith_overflow (enum tree_code code, gimple *stmt)
2759 : : {
2760 : 72187 : tree lhs = gimple_call_lhs (stmt);
2761 : 72187 : if (lhs == NULL_TREE)
2762 : : return;
2763 : 72187 : tree arg0 = gimple_call_arg (stmt, 0);
2764 : 72187 : tree arg1 = gimple_call_arg (stmt, 1);
2765 : 72187 : tree type = TREE_TYPE (TREE_TYPE (lhs));
2766 : 72187 : int uns0_p = TYPE_UNSIGNED (TREE_TYPE (arg0));
2767 : 72187 : int uns1_p = TYPE_UNSIGNED (TREE_TYPE (arg1));
2768 : 72187 : int unsr_p = TYPE_UNSIGNED (type);
2769 : 72187 : int prec0 = TYPE_PRECISION (TREE_TYPE (arg0));
2770 : 72187 : int prec1 = TYPE_PRECISION (TREE_TYPE (arg1));
2771 : 72187 : int precres = TYPE_PRECISION (type);
2772 : 72187 : location_t loc = gimple_location (stmt);
2773 : 72187 : if (!uns0_p && get_range_pos_neg (arg0, stmt) == 1)
2774 : : uns0_p = true;
2775 : 72187 : if (!uns1_p && get_range_pos_neg (arg1, stmt) == 1)
2776 : : uns1_p = true;
2777 : 104124 : int pr = get_min_precision (arg0, uns0_p ? UNSIGNED : SIGNED);
2778 : 72187 : prec0 = MIN (prec0, pr);
2779 : 95244 : pr = get_min_precision (arg1, uns1_p ? UNSIGNED : SIGNED);
2780 : 72187 : prec1 = MIN (prec1, pr);
2781 : 72187 : int save_flag_trapv = flag_trapv;
2782 : :
2783 : : /* We don't want any __mulv?i3 etc. calls from the expansion of
2784 : : these internal functions, so disable -ftrapv temporarily. */
2785 : 72187 : flag_trapv = 0;
2786 : : /* If uns0_p && uns1_p, precop is minimum needed precision
2787 : : of unsigned type to hold the exact result, otherwise
2788 : : precop is minimum needed precision of signed type to
2789 : : hold the exact result. */
2790 : 72187 : int precop;
2791 : 72187 : if (code == MULT_EXPR)
2792 : 23543 : precop = prec0 + prec1 + (uns0_p != uns1_p);
2793 : : else
2794 : : {
2795 : 48644 : if (uns0_p == uns1_p)
2796 : 31219 : precop = MAX (prec0, prec1) + 1;
2797 : 17425 : else if (uns0_p)
2798 : 6114 : precop = MAX (prec0 + 1, prec1) + 1;
2799 : : else
2800 : 11311 : precop = MAX (prec0, prec1 + 1) + 1;
2801 : : }
2802 : 72187 : int orig_precres = precres;
2803 : :
2804 : 90727 : do
2805 : : {
2806 : 90727 : if ((uns0_p && uns1_p)
2807 : 90727 : ? ((precop + !unsr_p) <= precres
2808 : : /* u1 - u2 -> ur can overflow, no matter what precision
2809 : : the result has. */
2810 : 35895 : && (code != MINUS_EXPR || !unsr_p))
2811 : 54832 : : (!unsr_p && precop <= precres))
2812 : : {
2813 : : /* The infinity precision result will always fit into result. */
2814 : 18313 : rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
2815 : 18313 : write_complex_part (target, const0_rtx, true, false);
2816 : 18313 : scalar_int_mode mode = SCALAR_INT_TYPE_MODE (type);
2817 : 18313 : struct separate_ops ops;
2818 : 18313 : ops.code = code;
2819 : 18313 : ops.type = type;
2820 : 18313 : ops.op0 = fold_convert_loc (loc, type, arg0);
2821 : 18313 : ops.op1 = fold_convert_loc (loc, type, arg1);
2822 : 18313 : ops.op2 = NULL_TREE;
2823 : 18313 : ops.location = loc;
2824 : 18313 : rtx tem = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL);
2825 : 18313 : expand_arith_overflow_result_store (lhs, target, mode, tem);
2826 : 18313 : flag_trapv = save_flag_trapv;
2827 : 18313 : return;
2828 : : }
2829 : :
2830 : : /* For operations with low precision, if target doesn't have them, start
2831 : : with precres widening right away, otherwise do it only if the most
2832 : : simple cases can't be used. */
2833 : 72414 : const int min_precision = targetm.min_arithmetic_precision ();
2834 : 72414 : if (orig_precres == precres && precres < min_precision)
2835 : : ;
2836 : 71151 : else if ((uns0_p && uns1_p && unsr_p && prec0 <= precres
2837 : 21676 : && prec1 <= precres)
2838 : 49478 : || ((!uns0_p || !uns1_p) && !unsr_p
2839 : 19437 : && prec0 + uns0_p <= precres
2840 : 16656 : && prec1 + uns1_p <= precres))
2841 : : {
2842 : 34843 : arg0 = fold_convert_loc (loc, type, arg0);
2843 : 34843 : arg1 = fold_convert_loc (loc, type, arg1);
2844 : 34843 : switch (code)
2845 : : {
2846 : 10180 : case MINUS_EXPR:
2847 : 10180 : if (integer_zerop (arg0) && !unsr_p)
2848 : : {
2849 : 402 : expand_neg_overflow (loc, lhs, arg1, false, NULL);
2850 : 402 : flag_trapv = save_flag_trapv;
2851 : 402 : return;
2852 : : }
2853 : : /* FALLTHRU */
2854 : 24657 : case PLUS_EXPR:
2855 : 24657 : expand_addsub_overflow (loc, code, lhs, arg0, arg1, unsr_p,
2856 : : unsr_p, unsr_p, false, NULL);
2857 : 24657 : flag_trapv = save_flag_trapv;
2858 : 24657 : return;
2859 : 9784 : case MULT_EXPR:
2860 : 9784 : expand_mul_overflow (loc, lhs, arg0, arg1, unsr_p,
2861 : : unsr_p, unsr_p, false, NULL);
2862 : 9784 : flag_trapv = save_flag_trapv;
2863 : 9784 : return;
2864 : 0 : default:
2865 : 0 : gcc_unreachable ();
2866 : : }
2867 : : }
2868 : :
2869 : : /* For sub-word operations, retry with a wider type first. */
2870 : 37572 : if (orig_precres == precres && precop <= BITS_PER_WORD)
2871 : : {
2872 : 19639 : int p = MAX (min_precision, precop);
2873 : 19639 : scalar_int_mode m = smallest_int_mode_for_size (p).require ();
2874 : 19639 : tree optype = build_nonstandard_integer_type (GET_MODE_PRECISION (m),
2875 : : uns0_p && uns1_p
2876 : 19639 : && unsr_p);
2877 : 19639 : p = TYPE_PRECISION (optype);
2878 : 19639 : if (p > precres)
2879 : : {
2880 : 17984 : precres = p;
2881 : 17984 : unsr_p = TYPE_UNSIGNED (optype);
2882 : 17984 : type = optype;
2883 : 17984 : continue;
2884 : : }
2885 : : }
2886 : :
2887 : 19587 : if (prec0 <= precres && prec1 <= precres)
2888 : : {
2889 : 19031 : tree types[2];
2890 : 19031 : if (unsr_p)
2891 : : {
2892 : 10936 : types[0] = build_nonstandard_integer_type (precres, 0);
2893 : 10936 : types[1] = type;
2894 : : }
2895 : : else
2896 : : {
2897 : 8095 : types[0] = type;
2898 : 8095 : types[1] = build_nonstandard_integer_type (precres, 1);
2899 : : }
2900 : 19031 : arg0 = fold_convert_loc (loc, types[uns0_p], arg0);
2901 : 19031 : arg1 = fold_convert_loc (loc, types[uns1_p], arg1);
2902 : 19031 : if (code != MULT_EXPR)
2903 : 11507 : expand_addsub_overflow (loc, code, lhs, arg0, arg1, unsr_p,
2904 : : uns0_p, uns1_p, false, NULL);
2905 : : else
2906 : 7524 : expand_mul_overflow (loc, lhs, arg0, arg1, unsr_p,
2907 : : uns0_p, uns1_p, false, NULL);
2908 : 19031 : flag_trapv = save_flag_trapv;
2909 : 19031 : return;
2910 : : }
2911 : :
2912 : : /* Retry with a wider type. */
2913 : 556 : if (orig_precres == precres)
2914 : : {
2915 : 556 : int p = MAX (prec0, prec1);
2916 : 556 : scalar_int_mode m = smallest_int_mode_for_size (p).require ();
2917 : 556 : tree optype = build_nonstandard_integer_type (GET_MODE_PRECISION (m),
2918 : : uns0_p && uns1_p
2919 : 556 : && unsr_p);
2920 : 556 : p = TYPE_PRECISION (optype);
2921 : 556 : if (p > precres)
2922 : : {
2923 : 556 : precres = p;
2924 : 556 : unsr_p = TYPE_UNSIGNED (optype);
2925 : 556 : type = optype;
2926 : 556 : continue;
2927 : : }
2928 : : }
2929 : :
2930 : 0 : gcc_unreachable ();
2931 : : }
2932 : : while (1);
2933 : : }
2934 : :
2935 : : /* Expand ADD_OVERFLOW STMT. */
2936 : :
2937 : : static void
2938 : 25200 : expand_ADD_OVERFLOW (internal_fn, gcall *stmt)
2939 : : {
2940 : 25200 : expand_arith_overflow (PLUS_EXPR, stmt);
2941 : 25200 : }
2942 : :
2943 : : /* Expand SUB_OVERFLOW STMT. */
2944 : :
2945 : : static void
2946 : 23444 : expand_SUB_OVERFLOW (internal_fn, gcall *stmt)
2947 : : {
2948 : 23444 : expand_arith_overflow (MINUS_EXPR, stmt);
2949 : 23444 : }
2950 : :
2951 : : /* Expand MUL_OVERFLOW STMT. */
2952 : :
2953 : : static void
2954 : 23543 : expand_MUL_OVERFLOW (internal_fn, gcall *stmt)
2955 : : {
2956 : 23543 : expand_arith_overflow (MULT_EXPR, stmt);
2957 : 23543 : }
2958 : :
2959 : : /* Expand UADDC STMT. */
2960 : :
2961 : : static void
2962 : 15103 : expand_UADDC (internal_fn ifn, gcall *stmt)
2963 : : {
2964 : 15103 : tree lhs = gimple_call_lhs (stmt);
2965 : 15103 : tree arg1 = gimple_call_arg (stmt, 0);
2966 : 15103 : tree arg2 = gimple_call_arg (stmt, 1);
2967 : 15103 : tree arg3 = gimple_call_arg (stmt, 2);
2968 : 15103 : tree type = TREE_TYPE (arg1);
2969 : 15103 : machine_mode mode = TYPE_MODE (type);
2970 : 21212 : insn_code icode = optab_handler (ifn == IFN_UADDC
2971 : : ? uaddc5_optab : usubc5_optab, mode);
2972 : 15103 : rtx op1 = expand_normal (arg1);
2973 : 15103 : rtx op2 = expand_normal (arg2);
2974 : 15103 : rtx op3 = expand_normal (arg3);
2975 : 15103 : rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
2976 : 15103 : rtx re = gen_reg_rtx (mode);
2977 : 15103 : rtx im = gen_reg_rtx (mode);
2978 : 15103 : class expand_operand ops[5];
2979 : 15103 : create_output_operand (&ops[0], re, mode);
2980 : 15103 : create_output_operand (&ops[1], im, mode);
2981 : 15103 : create_input_operand (&ops[2], op1, mode);
2982 : 15103 : create_input_operand (&ops[3], op2, mode);
2983 : 15103 : create_input_operand (&ops[4], op3, mode);
2984 : 15103 : expand_insn (icode, 5, ops);
2985 : 15103 : write_complex_part (target, re, false, false);
2986 : 15103 : write_complex_part (target, im, true, false);
2987 : 15103 : }
2988 : :
2989 : : /* Expand USUBC STMT. */
2990 : :
2991 : : static void
2992 : 6109 : expand_USUBC (internal_fn ifn, gcall *stmt)
2993 : : {
2994 : 6109 : expand_UADDC (ifn, stmt);
2995 : 6109 : }
2996 : :
2997 : : /* This should get folded in tree-vectorizer.cc. */
2998 : :
2999 : : static void
3000 : 0 : expand_LOOP_VECTORIZED (internal_fn, gcall *)
3001 : : {
3002 : 0 : gcc_unreachable ();
3003 : : }
3004 : :
3005 : : /* This should get folded in tree-vectorizer.cc. */
3006 : :
3007 : : static void
3008 : 0 : expand_LOOP_DIST_ALIAS (internal_fn, gcall *)
3009 : : {
3010 : 0 : gcc_unreachable ();
3011 : : }
3012 : :
3013 : : /* Return a memory reference of type TYPE for argument INDEX of STMT.
3014 : : Use argument INDEX + 1 to derive the second (TBAA) operand. */
3015 : :
3016 : : static tree
3017 : 1067 : expand_call_mem_ref (tree type, gcall *stmt, int index)
3018 : : {
3019 : 1067 : tree addr = gimple_call_arg (stmt, index);
3020 : 1067 : tree alias_ptr_type = TREE_TYPE (gimple_call_arg (stmt, index + 1));
3021 : 1067 : unsigned int align = tree_to_shwi (gimple_call_arg (stmt, index + 1));
3022 : 1067 : if (TYPE_ALIGN (type) != align)
3023 : 827 : type = build_aligned_type (type, align);
3024 : :
3025 : 1067 : tree tmp = addr;
3026 : 1067 : if (TREE_CODE (tmp) == SSA_NAME)
3027 : : {
3028 : 951 : gimple *def = get_gimple_for_ssa_name (tmp);
3029 : 951 : if (def && gimple_assign_single_p (def))
3030 : 395 : tmp = gimple_assign_rhs1 (def);
3031 : : }
3032 : :
3033 : 1067 : if (TREE_CODE (tmp) == ADDR_EXPR)
3034 : : {
3035 : 511 : tree mem = TREE_OPERAND (tmp, 0);
3036 : 511 : if (TREE_CODE (mem) == TARGET_MEM_REF
3037 : 511 : && types_compatible_p (TREE_TYPE (mem), type))
3038 : : {
3039 : 395 : tree offset = TMR_OFFSET (mem);
3040 : 395 : if (type != TREE_TYPE (mem)
3041 : 132 : || alias_ptr_type != TREE_TYPE (offset)
3042 : 527 : || !integer_zerop (offset))
3043 : : {
3044 : 290 : mem = copy_node (mem);
3045 : 580 : TMR_OFFSET (mem) = wide_int_to_tree (alias_ptr_type,
3046 : 290 : wi::to_poly_wide (offset));
3047 : 290 : TREE_TYPE (mem) = type;
3048 : : }
3049 : 395 : return mem;
3050 : : }
3051 : : }
3052 : :
3053 : 672 : return fold_build2 (MEM_REF, type, addr, build_int_cst (alias_ptr_type, 0));
3054 : : }
3055 : :
3056 : : /* Expand MASK_LOAD{,_LANES}, MASK_LEN_LOAD or LEN_LOAD call STMT using optab
3057 : : * OPTAB. */
3058 : :
3059 : : static void
3060 : 541 : expand_partial_load_optab_fn (internal_fn ifn, gcall *stmt, convert_optab optab)
3061 : : {
3062 : 541 : int i = 0;
3063 : 541 : class expand_operand ops[6];
3064 : 541 : tree type, lhs, rhs, maskt;
3065 : 541 : rtx mem, target;
3066 : 541 : insn_code icode;
3067 : :
3068 : 541 : maskt = gimple_call_arg (stmt, internal_fn_mask_index (ifn));
3069 : 541 : lhs = gimple_call_lhs (stmt);
3070 : 541 : if (lhs == NULL_TREE)
3071 : 0 : return;
3072 : 541 : type = TREE_TYPE (lhs);
3073 : 541 : rhs = expand_call_mem_ref (type, stmt, 0);
3074 : :
3075 : 541 : if (optab == vec_mask_load_lanes_optab
3076 : 541 : || optab == vec_mask_len_load_lanes_optab)
3077 : 0 : icode = get_multi_vector_move (type, optab);
3078 : 541 : else if (optab == len_load_optab)
3079 : 0 : icode = direct_optab_handler (optab, TYPE_MODE (type));
3080 : : else
3081 : 541 : icode = convert_optab_handler (optab, TYPE_MODE (type),
3082 : 541 : TYPE_MODE (TREE_TYPE (maskt)));
3083 : :
3084 : 541 : mem = expand_expr (rhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
3085 : 541 : gcc_assert (MEM_P (mem));
3086 : : /* The built MEM_REF does not accurately reflect that the load
3087 : : is only partial. Clear it. */
3088 : 541 : set_mem_expr (mem, NULL_TREE);
3089 : 541 : clear_mem_offset (mem);
3090 : 541 : target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
3091 : 541 : create_call_lhs_operand (&ops[i++], target, TYPE_MODE (type));
3092 : 541 : create_fixed_operand (&ops[i++], mem);
3093 : 541 : i = add_mask_else_and_len_args (ops, i, stmt);
3094 : 541 : expand_insn (icode, i, ops);
3095 : :
3096 : 541 : assign_call_lhs (lhs, target, &ops[0]);
3097 : : }
3098 : :
3099 : : #define expand_mask_load_optab_fn expand_partial_load_optab_fn
3100 : : #define expand_mask_load_lanes_optab_fn expand_mask_load_optab_fn
3101 : : #define expand_len_load_optab_fn expand_partial_load_optab_fn
3102 : : #define expand_mask_len_load_optab_fn expand_partial_load_optab_fn
3103 : :
3104 : : /* Expand MASK_STORE{,_LANES}, MASK_LEN_STORE or LEN_STORE call STMT using optab
3105 : : * OPTAB. */
3106 : :
3107 : : static void
3108 : 526 : expand_partial_store_optab_fn (internal_fn ifn, gcall *stmt, convert_optab optab)
3109 : : {
3110 : 526 : int i = 0;
3111 : 526 : class expand_operand ops[5];
3112 : 526 : tree type, lhs, rhs, maskt;
3113 : 526 : rtx mem, reg;
3114 : 526 : insn_code icode;
3115 : :
3116 : 526 : maskt = gimple_call_arg (stmt, internal_fn_mask_index (ifn));
3117 : 526 : rhs = gimple_call_arg (stmt, internal_fn_stored_value_index (ifn));
3118 : 526 : type = TREE_TYPE (rhs);
3119 : 526 : lhs = expand_call_mem_ref (type, stmt, 0);
3120 : :
3121 : 526 : if (optab == vec_mask_store_lanes_optab
3122 : 526 : || optab == vec_mask_len_store_lanes_optab)
3123 : 0 : icode = get_multi_vector_move (type, optab);
3124 : 526 : else if (optab == len_store_optab)
3125 : 0 : icode = direct_optab_handler (optab, TYPE_MODE (type));
3126 : : else
3127 : 526 : icode = convert_optab_handler (optab, TYPE_MODE (type),
3128 : 526 : TYPE_MODE (TREE_TYPE (maskt)));
3129 : :
3130 : 526 : mem = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
3131 : 526 : gcc_assert (MEM_P (mem));
3132 : : /* The built MEM_REF does not accurately reflect that the store
3133 : : is only partial. Clear it. */
3134 : 526 : set_mem_expr (mem, NULL_TREE);
3135 : 526 : clear_mem_offset (mem);
3136 : 526 : reg = expand_normal (rhs);
3137 : 526 : create_fixed_operand (&ops[i++], mem);
3138 : 526 : create_input_operand (&ops[i++], reg, TYPE_MODE (type));
3139 : 526 : i = add_mask_else_and_len_args (ops, i, stmt);
3140 : 526 : expand_insn (icode, i, ops);
3141 : 526 : }
3142 : :
3143 : : #define expand_mask_store_optab_fn expand_partial_store_optab_fn
3144 : : #define expand_mask_store_lanes_optab_fn expand_mask_store_optab_fn
3145 : : #define expand_len_store_optab_fn expand_partial_store_optab_fn
3146 : : #define expand_mask_len_store_optab_fn expand_partial_store_optab_fn
3147 : :
3148 : : /* Expand VCOND_MASK optab internal function.
3149 : : The expansion of STMT happens based on OPTAB table associated. */
3150 : :
3151 : : static void
3152 : 6296 : expand_vec_cond_mask_optab_fn (internal_fn, gcall *stmt, convert_optab optab)
3153 : : {
3154 : 6296 : class expand_operand ops[4];
3155 : :
3156 : 6296 : tree lhs = gimple_call_lhs (stmt);
3157 : 6296 : tree op0 = gimple_call_arg (stmt, 0);
3158 : 6296 : tree op1 = gimple_call_arg (stmt, 1);
3159 : 6296 : tree op2 = gimple_call_arg (stmt, 2);
3160 : 6296 : tree vec_cond_type = TREE_TYPE (lhs);
3161 : :
3162 : 6296 : machine_mode mode = TYPE_MODE (vec_cond_type);
3163 : 6296 : machine_mode mask_mode = TYPE_MODE (TREE_TYPE (op0));
3164 : 6296 : enum insn_code icode = convert_optab_handler (optab, mode, mask_mode);
3165 : 6296 : rtx mask, rtx_op1, rtx_op2;
3166 : :
3167 : 6296 : gcc_assert (icode != CODE_FOR_nothing);
3168 : :
3169 : 6296 : mask = expand_normal (op0);
3170 : 6296 : rtx_op1 = expand_normal (op1);
3171 : 6296 : rtx_op2 = expand_normal (op2);
3172 : :
3173 : 6296 : rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
3174 : 6296 : create_call_lhs_operand (&ops[0], target, mode);
3175 : 6296 : create_input_operand (&ops[1], rtx_op1, mode);
3176 : 6296 : create_input_operand (&ops[2], rtx_op2, mode);
3177 : 6296 : create_input_operand (&ops[3], mask, mask_mode);
3178 : 6296 : expand_insn (icode, 4, ops);
3179 : 6296 : assign_call_lhs (lhs, target, &ops[0]);
3180 : 6296 : }
3181 : :
3182 : : /* Expand VEC_SET internal functions. */
3183 : :
3184 : : static void
3185 : 122 : expand_vec_set_optab_fn (internal_fn, gcall *stmt, convert_optab optab)
3186 : : {
3187 : 122 : tree lhs = gimple_call_lhs (stmt);
3188 : 122 : tree op0 = gimple_call_arg (stmt, 0);
3189 : 122 : tree op1 = gimple_call_arg (stmt, 1);
3190 : 122 : tree op2 = gimple_call_arg (stmt, 2);
3191 : 122 : rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
3192 : 122 : rtx src = expand_normal (op0);
3193 : :
3194 : 122 : machine_mode outermode = TYPE_MODE (TREE_TYPE (op0));
3195 : 122 : scalar_mode innermode = GET_MODE_INNER (outermode);
3196 : :
3197 : 122 : rtx value = expand_normal (op1);
3198 : 122 : rtx pos = expand_normal (op2);
3199 : :
3200 : 122 : class expand_operand ops[3];
3201 : 122 : enum insn_code icode = optab_handler (optab, outermode);
3202 : :
3203 : 122 : if (icode != CODE_FOR_nothing)
3204 : : {
3205 : 122 : rtx temp = gen_reg_rtx (outermode);
3206 : 122 : emit_move_insn (temp, src);
3207 : :
3208 : 122 : create_fixed_operand (&ops[0], temp);
3209 : 122 : create_input_operand (&ops[1], value, innermode);
3210 : 122 : create_convert_operand_from (&ops[2], pos, TYPE_MODE (TREE_TYPE (op2)),
3211 : : true);
3212 : 122 : if (maybe_expand_insn (icode, 3, ops))
3213 : : {
3214 : 122 : emit_move_insn (target, temp);
3215 : 122 : return;
3216 : : }
3217 : : }
3218 : 0 : gcc_unreachable ();
3219 : : }
3220 : :
3221 : : static void
3222 : 1792 : expand_ABNORMAL_DISPATCHER (internal_fn, gcall *)
3223 : : {
3224 : 1792 : }
3225 : :
3226 : : static void
3227 : 0 : expand_BUILTIN_EXPECT (internal_fn, gcall *stmt)
3228 : : {
3229 : : /* When guessing was done, the hints should be already stripped away. */
3230 : 0 : gcc_assert (!flag_guess_branch_prob || optimize == 0 || seen_error ());
3231 : :
3232 : 0 : rtx target;
3233 : 0 : tree lhs = gimple_call_lhs (stmt);
3234 : 0 : if (lhs)
3235 : 0 : target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
3236 : : else
3237 : 0 : target = const0_rtx;
3238 : 0 : rtx val = expand_expr (gimple_call_arg (stmt, 0), target, VOIDmode, EXPAND_NORMAL);
3239 : 0 : if (lhs && val != target)
3240 : 0 : emit_move_insn (target, val);
3241 : 0 : }
3242 : :
3243 : : /* IFN_VA_ARG is supposed to be expanded at pass_stdarg. So this dummy function
3244 : : should never be called. */
3245 : :
3246 : : static void
3247 : 0 : expand_VA_ARG (internal_fn, gcall *)
3248 : : {
3249 : 0 : gcc_unreachable ();
3250 : : }
3251 : :
3252 : : /* IFN_VEC_CONVERT is supposed to be expanded at pass_lower_vector. So this
3253 : : dummy function should never be called. */
3254 : :
3255 : : static void
3256 : 0 : expand_VEC_CONVERT (internal_fn, gcall *)
3257 : : {
3258 : 0 : gcc_unreachable ();
3259 : : }
3260 : :
3261 : : /* Expand IFN_RAWMEMCHR internal function. */
3262 : :
3263 : : void
3264 : 0 : expand_RAWMEMCHR (internal_fn, gcall *stmt)
3265 : : {
3266 : 0 : expand_operand ops[3];
3267 : :
3268 : 0 : tree lhs = gimple_call_lhs (stmt);
3269 : 0 : if (!lhs)
3270 : 0 : return;
3271 : 0 : machine_mode lhs_mode = TYPE_MODE (TREE_TYPE (lhs));
3272 : 0 : rtx lhs_rtx = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
3273 : 0 : create_call_lhs_operand (&ops[0], lhs_rtx, lhs_mode);
3274 : :
3275 : 0 : tree mem = gimple_call_arg (stmt, 0);
3276 : 0 : rtx mem_rtx = get_memory_rtx (mem, NULL);
3277 : 0 : create_fixed_operand (&ops[1], mem_rtx);
3278 : :
3279 : 0 : tree pattern = gimple_call_arg (stmt, 1);
3280 : 0 : machine_mode mode = TYPE_MODE (TREE_TYPE (pattern));
3281 : 0 : rtx pattern_rtx = expand_normal (pattern);
3282 : 0 : create_input_operand (&ops[2], pattern_rtx, mode);
3283 : :
3284 : 0 : insn_code icode = direct_optab_handler (rawmemchr_optab, mode);
3285 : :
3286 : 0 : expand_insn (icode, 3, ops);
3287 : 0 : assign_call_lhs (lhs, lhs_rtx, &ops[0]);
3288 : : }
3289 : :
3290 : : /* Expand the IFN_UNIQUE function according to its first argument. */
3291 : :
3292 : : static void
3293 : 0 : expand_UNIQUE (internal_fn, gcall *stmt)
3294 : : {
3295 : 0 : rtx pattern = NULL_RTX;
3296 : 0 : enum ifn_unique_kind kind
3297 : 0 : = (enum ifn_unique_kind) TREE_INT_CST_LOW (gimple_call_arg (stmt, 0));
3298 : :
3299 : 0 : switch (kind)
3300 : : {
3301 : 0 : default:
3302 : 0 : gcc_unreachable ();
3303 : :
3304 : 0 : case IFN_UNIQUE_UNSPEC:
3305 : 0 : if (targetm.have_unique ())
3306 : 0 : pattern = targetm.gen_unique ();
3307 : : break;
3308 : :
3309 : 0 : case IFN_UNIQUE_OACC_FORK:
3310 : 0 : case IFN_UNIQUE_OACC_JOIN:
3311 : 0 : if (targetm.have_oacc_fork () && targetm.have_oacc_join ())
3312 : : {
3313 : 0 : tree lhs = gimple_call_lhs (stmt);
3314 : 0 : rtx target = const0_rtx;
3315 : :
3316 : 0 : if (lhs)
3317 : 0 : target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
3318 : :
3319 : 0 : rtx data_dep = expand_normal (gimple_call_arg (stmt, 1));
3320 : 0 : rtx axis = expand_normal (gimple_call_arg (stmt, 2));
3321 : :
3322 : 0 : if (kind == IFN_UNIQUE_OACC_FORK)
3323 : 0 : pattern = targetm.gen_oacc_fork (target, data_dep, axis);
3324 : : else
3325 : 0 : pattern = targetm.gen_oacc_join (target, data_dep, axis);
3326 : : }
3327 : : else
3328 : 0 : gcc_unreachable ();
3329 : : break;
3330 : : }
3331 : :
3332 : 0 : if (pattern)
3333 : 0 : emit_insn (pattern);
3334 : 0 : }
3335 : :
3336 : : /* Expand the IFN_DEFERRED_INIT function:
3337 : : LHS = DEFERRED_INIT (SIZE of the DECL, INIT_TYPE, NAME of the DECL);
3338 : :
3339 : : Initialize the LHS with zero/pattern according to its second argument
3340 : : INIT_TYPE:
3341 : : if INIT_TYPE is AUTO_INIT_ZERO, use zeroes to initialize;
3342 : : if INIT_TYPE is AUTO_INIT_PATTERN, use 0xFE byte-repeatable pattern
3343 : : to initialize;
3344 : : The LHS variable is initialized including paddings.
3345 : : The reasons to choose 0xFE for pattern initialization are:
3346 : : 1. It is a non-canonical virtual address on x86_64, and at the
3347 : : high end of the i386 kernel address space.
3348 : : 2. It is a very large float value (-1.694739530317379e+38).
3349 : : 3. It is also an unusual number for integers. */
3350 : : #define INIT_PATTERN_VALUE 0xFE
3351 : : static void
3352 : 461 : expand_DEFERRED_INIT (internal_fn, gcall *stmt)
3353 : : {
3354 : 461 : tree lhs = gimple_call_lhs (stmt);
3355 : 461 : tree var_size = gimple_call_arg (stmt, 0);
3356 : 461 : enum auto_init_type init_type
3357 : 461 : = (enum auto_init_type) TREE_INT_CST_LOW (gimple_call_arg (stmt, 1));
3358 : 461 : bool reg_lhs = true;
3359 : :
3360 : 461 : tree var_type = TREE_TYPE (lhs);
3361 : 461 : gcc_assert (init_type > AUTO_INIT_UNINITIALIZED);
3362 : :
3363 : 461 : if (TREE_CODE (lhs) == SSA_NAME)
3364 : : reg_lhs = true;
3365 : : else
3366 : : {
3367 : : tree lhs_base = lhs;
3368 : 190 : while (handled_component_p (lhs_base))
3369 : 1 : lhs_base = TREE_OPERAND (lhs_base, 0);
3370 : 189 : reg_lhs = (mem_ref_refers_to_non_mem_p (lhs_base)
3371 : 189 : || non_mem_decl_p (lhs_base));
3372 : : /* If this expands to a register and the underlying decl is wrapped in
3373 : : a MEM_REF that just serves as an access type change expose the decl
3374 : : if it is of correct size. This avoids a situation as in PR103271
3375 : : if the target does not support a direct move to the registers mode. */
3376 : 4 : if (reg_lhs
3377 : 4 : && TREE_CODE (lhs_base) == MEM_REF
3378 : 4 : && TREE_CODE (TREE_OPERAND (lhs_base, 0)) == ADDR_EXPR
3379 : 4 : && DECL_P (TREE_OPERAND (TREE_OPERAND (lhs_base, 0), 0))
3380 : 4 : && integer_zerop (TREE_OPERAND (lhs_base, 1))
3381 : 4 : && tree_fits_uhwi_p (var_size)
3382 : 4 : && tree_int_cst_equal
3383 : 4 : (var_size,
3384 : 4 : DECL_SIZE_UNIT (TREE_OPERAND (TREE_OPERAND (lhs_base, 0), 0))))
3385 : : {
3386 : 4 : lhs = TREE_OPERAND (TREE_OPERAND (lhs_base, 0), 0);
3387 : 4 : var_type = TREE_TYPE (lhs);
3388 : : }
3389 : : }
3390 : :
3391 : 189 : if (!reg_lhs)
3392 : : {
3393 : : /* If the variable is not in register, expand to a memset
3394 : : to initialize it. */
3395 : 185 : mark_addressable (lhs);
3396 : 185 : tree var_addr = build_fold_addr_expr (lhs);
3397 : :
3398 : 185 : tree value = (init_type == AUTO_INIT_PATTERN)
3399 : 185 : ? build_int_cst (integer_type_node,
3400 : : INIT_PATTERN_VALUE)
3401 : 109 : : integer_zero_node;
3402 : 370 : tree m_call = build_call_expr (builtin_decl_implicit (BUILT_IN_MEMSET),
3403 : : 3, var_addr, value, var_size);
3404 : : /* Expand this memset call. */
3405 : 185 : expand_builtin_memset (m_call, NULL_RTX, TYPE_MODE (var_type));
3406 : : }
3407 : : else
3408 : : {
3409 : : /* If this variable is in a register use expand_assignment.
3410 : : For boolean scalars force zero-init. */
3411 : 276 : tree init;
3412 : 276 : scalar_int_mode var_mode;
3413 : 276 : if (TREE_CODE (TREE_TYPE (lhs)) != BOOLEAN_TYPE
3414 : 266 : && tree_fits_uhwi_p (var_size)
3415 : 266 : && (init_type == AUTO_INIT_PATTERN
3416 : 188 : || !is_gimple_reg_type (var_type))
3417 : 82 : && int_mode_for_size (tree_to_uhwi (var_size) * BITS_PER_UNIT,
3418 : 281 : 0).exists (&var_mode)
3419 : 358 : && have_insn_for (SET, var_mode))
3420 : : {
3421 : 77 : unsigned HOST_WIDE_INT total_bytes = tree_to_uhwi (var_size);
3422 : 77 : unsigned char *buf = XALLOCAVEC (unsigned char, total_bytes);
3423 : 77 : memset (buf, (init_type == AUTO_INIT_PATTERN
3424 : : ? INIT_PATTERN_VALUE : 0), total_bytes);
3425 : 77 : tree itype = build_nonstandard_integer_type
3426 : 77 : (total_bytes * BITS_PER_UNIT, 1);
3427 : 77 : wide_int w = wi::from_buffer (buf, total_bytes);
3428 : 77 : init = wide_int_to_tree (itype, w);
3429 : : /* Pun the LHS to make sure its type has constant size
3430 : : unless it is an SSA name where that's already known. */
3431 : 77 : if (TREE_CODE (lhs) != SSA_NAME)
3432 : 4 : lhs = build1 (VIEW_CONVERT_EXPR, itype, lhs);
3433 : : else
3434 : 73 : init = fold_build1 (VIEW_CONVERT_EXPR, TREE_TYPE (lhs), init);
3435 : 77 : }
3436 : : else
3437 : : /* Use zero-init also for variable-length sizes. */
3438 : 199 : init = build_zero_cst (var_type);
3439 : :
3440 : 276 : expand_assignment (lhs, init, false);
3441 : : }
3442 : 461 : }
3443 : :
3444 : : /* Expand the IFN_ACCESS_WITH_SIZE function:
3445 : : ACCESS_WITH_SIZE (REF_TO_OBJ, REF_TO_SIZE, CLASS_OF_SIZE,
3446 : : TYPE_OF_SIZE, ACCESS_MODE)
3447 : : which returns the REF_TO_OBJ same as the 1st argument;
3448 : :
3449 : : 1st argument REF_TO_OBJ: The reference to the object;
3450 : : 2nd argument REF_TO_SIZE: The reference to the size of the object,
3451 : : 3rd argument CLASS_OF_SIZE: The size referenced by the REF_TO_SIZE represents
3452 : : 0: the number of bytes.
3453 : : 1: the number of the elements of the object type;
3454 : : 4th argument TYPE_OF_SIZE: A constant 0 with its TYPE being the same as the TYPE
3455 : : of the object referenced by REF_TO_SIZE
3456 : : 5th argument ACCESS_MODE:
3457 : : -1: Unknown access semantics
3458 : : 0: none
3459 : : 1: read_only
3460 : : 2: write_only
3461 : : 3: read_write
3462 : : 6th argument: A constant 0 with the pointer TYPE to the original flexible
3463 : : array type.
3464 : :
3465 : : Both the return type and the type of the first argument of this
3466 : : function have been converted from the incomplete array type to
3467 : : the corresponding pointer type.
3468 : :
3469 : : For each call to a .ACCESS_WITH_SIZE, replace it with its 1st argument. */
3470 : :
3471 : : static void
3472 : 47 : expand_ACCESS_WITH_SIZE (internal_fn, gcall *stmt)
3473 : : {
3474 : 47 : tree lhs = gimple_call_lhs (stmt);
3475 : 47 : tree ref_to_obj = gimple_call_arg (stmt, 0);
3476 : 47 : if (lhs)
3477 : 47 : expand_assignment (lhs, ref_to_obj, false);
3478 : 47 : }
3479 : :
3480 : : /* The size of an OpenACC compute dimension. */
3481 : :
3482 : : static void
3483 : 5702 : expand_GOACC_DIM_SIZE (internal_fn, gcall *stmt)
3484 : : {
3485 : 5702 : tree lhs = gimple_call_lhs (stmt);
3486 : :
3487 : 5702 : if (!lhs)
3488 : : return;
3489 : :
3490 : 5702 : rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
3491 : 5702 : if (targetm.have_oacc_dim_size ())
3492 : : {
3493 : 0 : rtx dim = expand_expr (gimple_call_arg (stmt, 0), NULL_RTX,
3494 : : VOIDmode, EXPAND_NORMAL);
3495 : 0 : emit_insn (targetm.gen_oacc_dim_size (target, dim));
3496 : : }
3497 : : else
3498 : 5702 : emit_move_insn (target, GEN_INT (1));
3499 : : }
3500 : :
3501 : : /* The position of an OpenACC execution engine along one compute axis. */
3502 : :
3503 : : static void
3504 : 4572 : expand_GOACC_DIM_POS (internal_fn, gcall *stmt)
3505 : : {
3506 : 4572 : tree lhs = gimple_call_lhs (stmt);
3507 : :
3508 : 4572 : if (!lhs)
3509 : : return;
3510 : :
3511 : 4572 : rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
3512 : 4572 : if (targetm.have_oacc_dim_pos ())
3513 : : {
3514 : 0 : rtx dim = expand_expr (gimple_call_arg (stmt, 0), NULL_RTX,
3515 : : VOIDmode, EXPAND_NORMAL);
3516 : 0 : emit_insn (targetm.gen_oacc_dim_pos (target, dim));
3517 : : }
3518 : : else
3519 : 4572 : emit_move_insn (target, const0_rtx);
3520 : : }
3521 : :
3522 : : /* This is expanded by oacc_device_lower pass. */
3523 : :
3524 : : static void
3525 : 0 : expand_GOACC_LOOP (internal_fn, gcall *)
3526 : : {
3527 : 0 : gcc_unreachable ();
3528 : : }
3529 : :
3530 : : /* This is expanded by oacc_device_lower pass. */
3531 : :
3532 : : static void
3533 : 0 : expand_GOACC_REDUCTION (internal_fn, gcall *)
3534 : : {
3535 : 0 : gcc_unreachable ();
3536 : : }
3537 : :
3538 : : /* This is expanded by oacc_device_lower pass. */
3539 : :
3540 : : static void
3541 : 0 : expand_GOACC_TILE (internal_fn, gcall *)
3542 : : {
3543 : 0 : gcc_unreachable ();
3544 : : }
3545 : :
3546 : : /* Set errno to EDOM. */
3547 : :
3548 : : static void
3549 : 0 : expand_SET_EDOM (internal_fn, gcall *)
3550 : : {
3551 : : #ifdef TARGET_EDOM
3552 : : #ifdef GEN_ERRNO_RTX
3553 : : rtx errno_rtx = GEN_ERRNO_RTX;
3554 : : #else
3555 : : rtx errno_rtx = gen_rtx_MEM (word_mode, gen_rtx_SYMBOL_REF (Pmode, "errno"));
3556 : : #endif
3557 : : emit_move_insn (errno_rtx,
3558 : : gen_int_mode (TARGET_EDOM, GET_MODE (errno_rtx)));
3559 : : #else
3560 : 0 : gcc_unreachable ();
3561 : : #endif
3562 : : }
3563 : :
3564 : : /* Expand atomic bit test and set. */
3565 : :
3566 : : static void
3567 : 146 : expand_ATOMIC_BIT_TEST_AND_SET (internal_fn, gcall *call)
3568 : : {
3569 : 146 : expand_ifn_atomic_bit_test_and (call);
3570 : 146 : }
3571 : :
3572 : : /* Expand atomic bit test and complement. */
3573 : :
3574 : : static void
3575 : 146 : expand_ATOMIC_BIT_TEST_AND_COMPLEMENT (internal_fn, gcall *call)
3576 : : {
3577 : 146 : expand_ifn_atomic_bit_test_and (call);
3578 : 146 : }
3579 : :
3580 : : /* Expand atomic bit test and reset. */
3581 : :
3582 : : static void
3583 : 135 : expand_ATOMIC_BIT_TEST_AND_RESET (internal_fn, gcall *call)
3584 : : {
3585 : 135 : expand_ifn_atomic_bit_test_and (call);
3586 : 135 : }
3587 : :
3588 : : /* Expand atomic bit test and set. */
3589 : :
3590 : : static void
3591 : 13647 : expand_ATOMIC_COMPARE_EXCHANGE (internal_fn, gcall *call)
3592 : : {
3593 : 13647 : expand_ifn_atomic_compare_exchange (call);
3594 : 13647 : }
3595 : :
3596 : : /* Expand atomic add fetch and cmp with 0. */
3597 : :
3598 : : static void
3599 : 576 : expand_ATOMIC_ADD_FETCH_CMP_0 (internal_fn, gcall *call)
3600 : : {
3601 : 576 : expand_ifn_atomic_op_fetch_cmp_0 (call);
3602 : 576 : }
3603 : :
3604 : : /* Expand atomic sub fetch and cmp with 0. */
3605 : :
3606 : : static void
3607 : 921 : expand_ATOMIC_SUB_FETCH_CMP_0 (internal_fn, gcall *call)
3608 : : {
3609 : 921 : expand_ifn_atomic_op_fetch_cmp_0 (call);
3610 : 921 : }
3611 : :
3612 : : /* Expand atomic and fetch and cmp with 0. */
3613 : :
3614 : : static void
3615 : 176 : expand_ATOMIC_AND_FETCH_CMP_0 (internal_fn, gcall *call)
3616 : : {
3617 : 176 : expand_ifn_atomic_op_fetch_cmp_0 (call);
3618 : 176 : }
3619 : :
3620 : : /* Expand atomic or fetch and cmp with 0. */
3621 : :
3622 : : static void
3623 : 112 : expand_ATOMIC_OR_FETCH_CMP_0 (internal_fn, gcall *call)
3624 : : {
3625 : 112 : expand_ifn_atomic_op_fetch_cmp_0 (call);
3626 : 112 : }
3627 : :
3628 : : /* Expand atomic xor fetch and cmp with 0. */
3629 : :
3630 : : static void
3631 : 176 : expand_ATOMIC_XOR_FETCH_CMP_0 (internal_fn, gcall *call)
3632 : : {
3633 : 176 : expand_ifn_atomic_op_fetch_cmp_0 (call);
3634 : 176 : }
3635 : :
3636 : : /* Expand LAUNDER to assignment, lhs = arg0. */
3637 : :
3638 : : static void
3639 : 29 : expand_LAUNDER (internal_fn, gcall *call)
3640 : : {
3641 : 29 : tree lhs = gimple_call_lhs (call);
3642 : :
3643 : 29 : if (!lhs)
3644 : : return;
3645 : :
3646 : 28 : expand_assignment (lhs, gimple_call_arg (call, 0), false);
3647 : : }
3648 : :
3649 : : /* Expand {MASK_,}SCATTER_STORE{S,U} call CALL using optab OPTAB. */
3650 : :
3651 : : static void
3652 : 0 : expand_scatter_store_optab_fn (internal_fn, gcall *stmt, direct_optab optab)
3653 : : {
3654 : 0 : internal_fn ifn = gimple_call_internal_fn (stmt);
3655 : 0 : int rhs_index = internal_fn_stored_value_index (ifn);
3656 : 0 : tree base = gimple_call_arg (stmt, 0);
3657 : 0 : tree offset = gimple_call_arg (stmt, 1);
3658 : 0 : tree scale = gimple_call_arg (stmt, 2);
3659 : 0 : tree rhs = gimple_call_arg (stmt, rhs_index);
3660 : :
3661 : 0 : rtx base_rtx = expand_normal (base);
3662 : 0 : rtx offset_rtx = expand_normal (offset);
3663 : 0 : HOST_WIDE_INT scale_int = tree_to_shwi (scale);
3664 : 0 : rtx rhs_rtx = expand_normal (rhs);
3665 : :
3666 : 0 : class expand_operand ops[8];
3667 : 0 : int i = 0;
3668 : 0 : create_address_operand (&ops[i++], base_rtx);
3669 : 0 : create_input_operand (&ops[i++], offset_rtx, TYPE_MODE (TREE_TYPE (offset)));
3670 : 0 : create_integer_operand (&ops[i++], TYPE_UNSIGNED (TREE_TYPE (offset)));
3671 : 0 : create_integer_operand (&ops[i++], scale_int);
3672 : 0 : create_input_operand (&ops[i++], rhs_rtx, TYPE_MODE (TREE_TYPE (rhs)));
3673 : 0 : i = add_mask_else_and_len_args (ops, i, stmt);
3674 : :
3675 : 0 : insn_code icode = convert_optab_handler (optab, TYPE_MODE (TREE_TYPE (rhs)),
3676 : 0 : TYPE_MODE (TREE_TYPE (offset)));
3677 : 0 : expand_insn (icode, i, ops);
3678 : 0 : }
3679 : :
3680 : : /* Expand {MASK_,}GATHER_LOAD call CALL using optab OPTAB. */
3681 : :
3682 : : static void
3683 : 0 : expand_gather_load_optab_fn (internal_fn, gcall *stmt, direct_optab optab)
3684 : : {
3685 : 0 : tree lhs = gimple_call_lhs (stmt);
3686 : 0 : tree base = gimple_call_arg (stmt, 0);
3687 : 0 : tree offset = gimple_call_arg (stmt, 1);
3688 : 0 : tree scale = gimple_call_arg (stmt, 2);
3689 : :
3690 : 0 : rtx lhs_rtx = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
3691 : 0 : rtx base_rtx = expand_normal (base);
3692 : 0 : rtx offset_rtx = expand_normal (offset);
3693 : 0 : HOST_WIDE_INT scale_int = tree_to_shwi (scale);
3694 : :
3695 : 0 : int i = 0;
3696 : 0 : class expand_operand ops[9];
3697 : 0 : create_call_lhs_operand (&ops[i++], lhs_rtx, TYPE_MODE (TREE_TYPE (lhs)));
3698 : 0 : create_address_operand (&ops[i++], base_rtx);
3699 : 0 : create_input_operand (&ops[i++], offset_rtx, TYPE_MODE (TREE_TYPE (offset)));
3700 : 0 : create_integer_operand (&ops[i++], TYPE_UNSIGNED (TREE_TYPE (offset)));
3701 : 0 : create_integer_operand (&ops[i++], scale_int);
3702 : 0 : i = add_mask_else_and_len_args (ops, i, stmt);
3703 : 0 : insn_code icode = convert_optab_handler (optab, TYPE_MODE (TREE_TYPE (lhs)),
3704 : 0 : TYPE_MODE (TREE_TYPE (offset)));
3705 : 0 : expand_insn (icode, i, ops);
3706 : 0 : assign_call_lhs (lhs, lhs_rtx, &ops[0]);
3707 : 0 : }
3708 : :
3709 : : /* Expand MASK_LEN_STRIDED_LOAD call CALL by optab OPTAB. */
3710 : :
3711 : : static void
3712 : 0 : expand_strided_load_optab_fn (ATTRIBUTE_UNUSED internal_fn, gcall *stmt,
3713 : : direct_optab optab)
3714 : : {
3715 : 0 : tree lhs = gimple_call_lhs (stmt);
3716 : 0 : tree base = gimple_call_arg (stmt, 0);
3717 : 0 : tree stride = gimple_call_arg (stmt, 1);
3718 : :
3719 : 0 : rtx lhs_rtx = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
3720 : 0 : rtx base_rtx = expand_normal (base);
3721 : 0 : rtx stride_rtx = expand_normal (stride);
3722 : :
3723 : 0 : unsigned i = 0;
3724 : 0 : class expand_operand ops[7];
3725 : 0 : machine_mode mode = TYPE_MODE (TREE_TYPE (lhs));
3726 : :
3727 : 0 : create_output_operand (&ops[i++], lhs_rtx, mode);
3728 : 0 : create_address_operand (&ops[i++], base_rtx);
3729 : 0 : create_address_operand (&ops[i++], stride_rtx);
3730 : :
3731 : 0 : i = add_mask_else_and_len_args (ops, i, stmt);
3732 : 0 : expand_insn (direct_optab_handler (optab, mode), i, ops);
3733 : :
3734 : 0 : if (!rtx_equal_p (lhs_rtx, ops[0].value))
3735 : 0 : emit_move_insn (lhs_rtx, ops[0].value);
3736 : 0 : }
3737 : :
3738 : : /* Expand MASK_LEN_STRIDED_STORE call CALL by optab OPTAB. */
3739 : :
3740 : : static void
3741 : 0 : expand_strided_store_optab_fn (ATTRIBUTE_UNUSED internal_fn, gcall *stmt,
3742 : : direct_optab optab)
3743 : : {
3744 : 0 : internal_fn fn = gimple_call_internal_fn (stmt);
3745 : 0 : int rhs_index = internal_fn_stored_value_index (fn);
3746 : :
3747 : 0 : tree base = gimple_call_arg (stmt, 0);
3748 : 0 : tree stride = gimple_call_arg (stmt, 1);
3749 : 0 : tree rhs = gimple_call_arg (stmt, rhs_index);
3750 : :
3751 : 0 : rtx base_rtx = expand_normal (base);
3752 : 0 : rtx stride_rtx = expand_normal (stride);
3753 : 0 : rtx rhs_rtx = expand_normal (rhs);
3754 : :
3755 : 0 : unsigned i = 0;
3756 : 0 : class expand_operand ops[6];
3757 : 0 : machine_mode mode = TYPE_MODE (TREE_TYPE (rhs));
3758 : :
3759 : 0 : create_address_operand (&ops[i++], base_rtx);
3760 : 0 : create_address_operand (&ops[i++], stride_rtx);
3761 : 0 : create_input_operand (&ops[i++], rhs_rtx, mode);
3762 : :
3763 : 0 : i = add_mask_else_and_len_args (ops, i, stmt);
3764 : 0 : expand_insn (direct_optab_handler (optab, mode), i, ops);
3765 : 0 : }
3766 : :
3767 : : /* Helper for expand_DIVMOD. Return true if the sequence starting with
3768 : : INSN contains any call insns or insns with {,U}{DIV,MOD} rtxes. */
3769 : :
3770 : : static bool
3771 : 842 : contains_call_div_mod (rtx_insn *insn)
3772 : : {
3773 : 842 : subrtx_iterator::array_type array;
3774 : 4842 : for (; insn; insn = NEXT_INSN (insn))
3775 : 4842 : if (CALL_P (insn))
3776 : : return true;
3777 : 4000 : else if (INSN_P (insn))
3778 : 17884 : FOR_EACH_SUBRTX (iter, array, PATTERN (insn), NONCONST)
3779 : 13884 : switch (GET_CODE (*iter))
3780 : : {
3781 : 0 : case CALL:
3782 : 0 : case DIV:
3783 : 0 : case UDIV:
3784 : 0 : case MOD:
3785 : 0 : case UMOD:
3786 : 0 : return true;
3787 : 13884 : default:
3788 : 13884 : break;
3789 : : }
3790 : : return false;
3791 : 842 : }
3792 : :
3793 : : /* Expand DIVMOD() using:
3794 : : a) optab handler for udivmod/sdivmod if it is available.
3795 : : b) If optab_handler doesn't exist, generate call to
3796 : : target-specific divmod libfunc. */
3797 : :
3798 : : static void
3799 : 11341 : expand_DIVMOD (internal_fn, gcall *call_stmt)
3800 : : {
3801 : 11341 : tree lhs = gimple_call_lhs (call_stmt);
3802 : 11341 : tree arg0 = gimple_call_arg (call_stmt, 0);
3803 : 11341 : tree arg1 = gimple_call_arg (call_stmt, 1);
3804 : :
3805 : 11341 : gcc_assert (TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE);
3806 : 11341 : tree type = TREE_TYPE (TREE_TYPE (lhs));
3807 : 11341 : machine_mode mode = TYPE_MODE (type);
3808 : 11341 : bool unsignedp = TYPE_UNSIGNED (type);
3809 : 11341 : optab tab = (unsignedp) ? udivmod_optab : sdivmod_optab;
3810 : :
3811 : 11341 : rtx op0 = expand_normal (arg0);
3812 : 11341 : rtx op1 = expand_normal (arg1);
3813 : 11341 : rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
3814 : :
3815 : 11341 : rtx quotient = NULL_RTX, remainder = NULL_RTX;
3816 : 11341 : rtx_insn *insns = NULL;
3817 : :
3818 : 11341 : if (TREE_CODE (arg1) == INTEGER_CST)
3819 : : {
3820 : : /* For DIVMOD by integral constants, there could be efficient code
3821 : : expanded inline e.g. using shifts and plus/minus. Try to expand
3822 : : the division and modulo and if it emits any library calls or any
3823 : : {,U}{DIV,MOD} rtxes throw it away and use a divmod optab or
3824 : : divmod libcall. */
3825 : 1297 : scalar_int_mode int_mode;
3826 : 1297 : if (remainder == NULL_RTX
3827 : 1297 : && optimize
3828 : 1297 : && CONST_INT_P (op1)
3829 : 1291 : && !pow2p_hwi (INTVAL (op1))
3830 : 2582 : && is_int_mode (TYPE_MODE (type), &int_mode)
3831 : 1485 : && GET_MODE_SIZE (int_mode) == 2 * UNITS_PER_WORD
3832 : 1291 : && optab_handler (and_optab, word_mode) != CODE_FOR_nothing
3833 : 1291 : && optab_handler (add_optab, word_mode) != CODE_FOR_nothing
3834 : 1291 : && optimize_insn_for_speed_p ())
3835 : : {
3836 : 1288 : rtx_insn *last = get_last_insn ();
3837 : 1288 : remainder = NULL_RTX;
3838 : 3864 : quotient = expand_doubleword_divmod (int_mode, op0, op1, &remainder,
3839 : 1288 : TYPE_UNSIGNED (type));
3840 : 1288 : if (quotient != NULL_RTX)
3841 : : {
3842 : 455 : if (optab_handler (mov_optab, int_mode) != CODE_FOR_nothing)
3843 : : {
3844 : 455 : rtx_insn *move = emit_move_insn (quotient, quotient);
3845 : 910 : set_dst_reg_note (move, REG_EQUAL,
3846 : 455 : gen_rtx_fmt_ee (TYPE_UNSIGNED (type)
3847 : : ? UDIV : DIV, int_mode,
3848 : : copy_rtx (op0), op1),
3849 : : quotient);
3850 : 455 : move = emit_move_insn (remainder, remainder);
3851 : 910 : set_dst_reg_note (move, REG_EQUAL,
3852 : 455 : gen_rtx_fmt_ee (TYPE_UNSIGNED (type)
3853 : : ? UMOD : MOD, int_mode,
3854 : : copy_rtx (op0), op1),
3855 : : quotient);
3856 : : }
3857 : : }
3858 : : else
3859 : 833 : delete_insns_since (last);
3860 : : }
3861 : :
3862 : 1297 : if (remainder == NULL_RTX)
3863 : : {
3864 : 842 : struct separate_ops ops;
3865 : 842 : ops.code = TRUNC_DIV_EXPR;
3866 : 842 : ops.type = type;
3867 : 842 : ops.op0 = make_tree (ops.type, op0);
3868 : 842 : ops.op1 = arg1;
3869 : 842 : ops.op2 = NULL_TREE;
3870 : 842 : ops.location = gimple_location (call_stmt);
3871 : 842 : start_sequence ();
3872 : 842 : quotient = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL);
3873 : 842 : if (contains_call_div_mod (get_insns ()))
3874 : 842 : quotient = NULL_RTX;
3875 : : else
3876 : : {
3877 : 0 : ops.code = TRUNC_MOD_EXPR;
3878 : 0 : remainder = expand_expr_real_2 (&ops, NULL_RTX, mode,
3879 : : EXPAND_NORMAL);
3880 : 0 : if (contains_call_div_mod (get_insns ()))
3881 : 0 : remainder = NULL_RTX;
3882 : : }
3883 : 842 : if (remainder)
3884 : 0 : insns = get_insns ();
3885 : 842 : end_sequence ();
3886 : : }
3887 : : }
3888 : :
3889 : 11341 : if (remainder)
3890 : 455 : emit_insn (insns);
3891 : :
3892 : : /* Check if optab_handler exists for divmod_optab for given mode. */
3893 : 10886 : else if (optab_handler (tab, mode) != CODE_FOR_nothing)
3894 : : {
3895 : 9982 : quotient = gen_reg_rtx (mode);
3896 : 9982 : remainder = gen_reg_rtx (mode);
3897 : 9982 : expand_twoval_binop (tab, op0, op1, quotient, remainder, unsignedp);
3898 : : }
3899 : :
3900 : : /* Generate call to divmod libfunc if it exists. */
3901 : 904 : else if (rtx libfunc = optab_libfunc (tab, mode))
3902 : 904 : targetm.expand_divmod_libfunc (libfunc, mode, op0, op1,
3903 : : "ient, &remainder);
3904 : :
3905 : : else
3906 : 0 : gcc_unreachable ();
3907 : :
3908 : : /* Wrap the return value (quotient, remainder) within COMPLEX_EXPR. */
3909 : 22682 : expand_expr (build2 (COMPLEX_EXPR, TREE_TYPE (lhs),
3910 : 11341 : make_tree (TREE_TYPE (arg0), quotient),
3911 : 11341 : make_tree (TREE_TYPE (arg1), remainder)),
3912 : : target, VOIDmode, EXPAND_NORMAL);
3913 : 11341 : }
3914 : :
3915 : : /* Expand a NOP. */
3916 : :
3917 : : static void
3918 : 1 : expand_NOP (internal_fn, gcall *)
3919 : : {
3920 : : /* Nothing. But it shouldn't really prevail. */
3921 : 1 : }
3922 : :
3923 : : /* Coroutines, all should have been processed at this stage. */
3924 : :
3925 : : static void
3926 : 0 : expand_CO_FRAME (internal_fn, gcall *)
3927 : : {
3928 : 0 : gcc_unreachable ();
3929 : : }
3930 : :
3931 : : static void
3932 : 0 : expand_CO_YIELD (internal_fn, gcall *)
3933 : : {
3934 : 0 : gcc_unreachable ();
3935 : : }
3936 : :
3937 : : static void
3938 : 0 : expand_CO_SUSPN (internal_fn, gcall *)
3939 : : {
3940 : 0 : gcc_unreachable ();
3941 : : }
3942 : :
3943 : : static void
3944 : 0 : expand_CO_ACTOR (internal_fn, gcall *)
3945 : : {
3946 : 0 : gcc_unreachable ();
3947 : : }
3948 : :
3949 : : /* Expand a call to FN using the operands in STMT. FN has a single
3950 : : output operand and NARGS input operands. */
3951 : :
3952 : : static void
3953 : 81165 : expand_direct_optab_fn (internal_fn fn, gcall *stmt, direct_optab optab,
3954 : : unsigned int nargs)
3955 : : {
3956 : 81165 : tree_pair types = direct_internal_fn_types (fn, stmt);
3957 : 81165 : insn_code icode = direct_optab_handler (optab, TYPE_MODE (types.first));
3958 : 81165 : expand_fn_using_insn (stmt, icode, 1, nargs);
3959 : 81165 : }
3960 : :
3961 : : /* Expand WHILE_ULT call STMT using optab OPTAB. */
3962 : :
3963 : : static void
3964 : 0 : expand_while_optab_fn (internal_fn, gcall *stmt, convert_optab optab)
3965 : : {
3966 : 0 : expand_operand ops[4];
3967 : 0 : tree rhs_type[2];
3968 : :
3969 : 0 : tree lhs = gimple_call_lhs (stmt);
3970 : 0 : tree lhs_type = TREE_TYPE (lhs);
3971 : 0 : rtx lhs_rtx = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
3972 : 0 : create_call_lhs_operand (&ops[0], lhs_rtx, TYPE_MODE (lhs_type));
3973 : :
3974 : 0 : for (unsigned int i = 0; i < 2; ++i)
3975 : : {
3976 : 0 : tree rhs = gimple_call_arg (stmt, i);
3977 : 0 : rhs_type[i] = TREE_TYPE (rhs);
3978 : 0 : rtx rhs_rtx = expand_normal (rhs);
3979 : 0 : create_input_operand (&ops[i + 1], rhs_rtx, TYPE_MODE (rhs_type[i]));
3980 : : }
3981 : :
3982 : 0 : int opcnt;
3983 : 0 : if (!VECTOR_MODE_P (TYPE_MODE (lhs_type)))
3984 : : {
3985 : : /* When the mask is an integer mode the exact vector length may not
3986 : : be clear to the backend, so we pass it in operand[3].
3987 : : Use the vector in arg2 for the most reliable intended size. */
3988 : 0 : tree type = TREE_TYPE (gimple_call_arg (stmt, 2));
3989 : 0 : create_integer_operand (&ops[3], TYPE_VECTOR_SUBPARTS (type));
3990 : 0 : opcnt = 4;
3991 : : }
3992 : : else
3993 : : /* The mask has a vector type so the length operand is unnecessary. */
3994 : : opcnt = 3;
3995 : :
3996 : 0 : insn_code icode = convert_optab_handler (optab, TYPE_MODE (rhs_type[0]),
3997 : 0 : TYPE_MODE (lhs_type));
3998 : :
3999 : 0 : expand_insn (icode, opcnt, ops);
4000 : 0 : assign_call_lhs (lhs, lhs_rtx, &ops[0]);
4001 : 0 : }
4002 : :
4003 : : /* Expand a call to a convert-like optab using the operands in STMT.
4004 : : FN has a single output operand and NARGS input operands. */
4005 : :
4006 : : static void
4007 : 436 : expand_convert_optab_fn (internal_fn fn, gcall *stmt, convert_optab optab,
4008 : : unsigned int nargs)
4009 : : {
4010 : 436 : tree_pair types = direct_internal_fn_types (fn, stmt);
4011 : 436 : insn_code icode = convert_optab_handler (optab, TYPE_MODE (types.first),
4012 : 436 : TYPE_MODE (types.second));
4013 : 436 : expand_fn_using_insn (stmt, icode, 1, nargs);
4014 : 436 : }
4015 : :
4016 : : /* Expand CRC call STMT. */
4017 : :
4018 : : static void
4019 : 233 : expand_crc_optab_fn (internal_fn fn, gcall *stmt, convert_optab optab)
4020 : : {
4021 : 233 : tree lhs = gimple_call_lhs (stmt);
4022 : 233 : tree rhs1 = gimple_call_arg (stmt, 0); // crc
4023 : 233 : tree rhs2 = gimple_call_arg (stmt, 1); // data
4024 : 233 : tree rhs3 = gimple_call_arg (stmt, 2); // polynomial
4025 : :
4026 : 233 : tree result_type = TREE_TYPE (lhs);
4027 : 233 : tree data_type = TREE_TYPE (rhs2);
4028 : :
4029 : 233 : gcc_assert (TYPE_MODE (result_type) >= TYPE_MODE (data_type));
4030 : :
4031 : 233 : rtx dest = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
4032 : 233 : rtx crc = expand_normal (rhs1);
4033 : 233 : rtx data = expand_normal (rhs2);
4034 : 233 : gcc_assert (TREE_CODE (rhs3) == INTEGER_CST);
4035 : 233 : rtx polynomial = gen_rtx_CONST_INT (TYPE_MODE (result_type),
4036 : 233 : TREE_INT_CST_LOW (rhs3));
4037 : :
4038 : : /* Use target specific expansion if it exists.
4039 : : Otherwise, generate table-based CRC. */
4040 : 233 : if (direct_internal_fn_supported_p (fn, tree_pair (data_type, result_type),
4041 : : OPTIMIZE_FOR_SPEED))
4042 : : {
4043 : 0 : class expand_operand ops[4];
4044 : :
4045 : 0 : if (dump_file && (dump_flags & TDF_DETAILS))
4046 : : {
4047 : 0 : fprintf (dump_file,
4048 : : ";; using optab for crc_%u_polynomial_"
4049 : : HOST_WIDE_INT_PRINT_HEX "\n",
4050 : 0 : GET_MODE_BITSIZE (GET_MODE (dest)).to_constant (),
4051 : 0 : TREE_INT_CST_LOW (rhs3));
4052 : : }
4053 : :
4054 : 0 : create_call_lhs_operand (&ops[0], dest, TYPE_MODE (result_type));
4055 : 0 : create_input_operand (&ops[1], crc, TYPE_MODE (result_type));
4056 : 0 : create_input_operand (&ops[2], data, TYPE_MODE (data_type));
4057 : 0 : create_input_operand (&ops[3], polynomial, TYPE_MODE (result_type));
4058 : 0 : insn_code icode = convert_optab_handler (optab, TYPE_MODE (data_type),
4059 : 0 : TYPE_MODE (result_type));
4060 : 0 : expand_insn (icode, 4, ops);
4061 : 0 : assign_call_lhs (lhs, dest, &ops[0]);
4062 : : }
4063 : : else
4064 : : {
4065 : : /* We're bypassing all the operand conversions that are done in the
4066 : : case when we get an icode, operands and pass that off to expand_insn.
4067 : :
4068 : : That path has special case handling for promoted return values which
4069 : : we must emulate here (is the same kind of special treatment ever
4070 : : needed for input arguments here?).
4071 : :
4072 : : In particular we do not want to store directly into a promoted
4073 : : SUBREG destination, instead store into a suitably sized pseudo. */
4074 : 233 : rtx orig_dest = dest;
4075 : 233 : if (SUBREG_P (dest) && SUBREG_PROMOTED_VAR_P (dest))
4076 : 0 : dest = gen_reg_rtx (GET_MODE (dest));
4077 : :
4078 : : /* If it's IFN_CRC generate bit-forward CRC. */
4079 : 233 : if (fn == IFN_CRC)
4080 : 118 : expand_crc_table_based (dest, crc, data, polynomial,
4081 : 118 : TYPE_MODE (data_type));
4082 : : else
4083 : : /* If it's IFN_CRC_REV generate bit-reversed CRC. */
4084 : 115 : expand_reversed_crc_table_based (dest, crc, data, polynomial,
4085 : 115 : TYPE_MODE (data_type),
4086 : : generate_reflecting_code_standard);
4087 : :
4088 : : /* Now get the return value where it needs to be, taking care to
4089 : : ensure it's promoted appropriately if the ABI demands it.
4090 : :
4091 : : Re-use assign_call_lhs to handle the details. */
4092 : 233 : class expand_operand ops[4];
4093 : 233 : create_call_lhs_operand (&ops[0], dest, TYPE_MODE (result_type));
4094 : 233 : ops[0].value = dest;
4095 : 233 : assign_call_lhs (lhs, orig_dest, &ops[0]);
4096 : : }
4097 : 233 : }
4098 : :
4099 : : /* Expanders for optabs that can use expand_direct_optab_fn. */
4100 : :
4101 : : #define expand_unary_optab_fn(FN, STMT, OPTAB) \
4102 : : expand_direct_optab_fn (FN, STMT, OPTAB, 1)
4103 : :
4104 : : #define expand_binary_optab_fn(FN, STMT, OPTAB) \
4105 : : expand_direct_optab_fn (FN, STMT, OPTAB, 2)
4106 : :
4107 : : #define expand_ternary_optab_fn(FN, STMT, OPTAB) \
4108 : : expand_direct_optab_fn (FN, STMT, OPTAB, 3)
4109 : :
4110 : : #define expand_cond_unary_optab_fn(FN, STMT, OPTAB) \
4111 : : expand_direct_optab_fn (FN, STMT, OPTAB, 3)
4112 : :
4113 : : #define expand_cond_binary_optab_fn(FN, STMT, OPTAB) \
4114 : : expand_direct_optab_fn (FN, STMT, OPTAB, 4)
4115 : :
4116 : : #define expand_cond_ternary_optab_fn(FN, STMT, OPTAB) \
4117 : : expand_direct_optab_fn (FN, STMT, OPTAB, 5)
4118 : :
4119 : : #define expand_cond_len_unary_optab_fn(FN, STMT, OPTAB) \
4120 : : expand_direct_optab_fn (FN, STMT, OPTAB, 5)
4121 : :
4122 : : #define expand_cond_len_binary_optab_fn(FN, STMT, OPTAB) \
4123 : : expand_direct_optab_fn (FN, STMT, OPTAB, 6)
4124 : :
4125 : : #define expand_cond_len_ternary_optab_fn(FN, STMT, OPTAB) \
4126 : : expand_direct_optab_fn (FN, STMT, OPTAB, 7)
4127 : :
4128 : : #define expand_fold_extract_optab_fn(FN, STMT, OPTAB) \
4129 : : expand_direct_optab_fn (FN, STMT, OPTAB, 3)
4130 : :
4131 : : #define expand_fold_len_extract_optab_fn(FN, STMT, OPTAB) \
4132 : : expand_direct_optab_fn (FN, STMT, OPTAB, 5)
4133 : :
4134 : : #define expand_fold_left_optab_fn(FN, STMT, OPTAB) \
4135 : : expand_direct_optab_fn (FN, STMT, OPTAB, 2)
4136 : :
4137 : : #define expand_mask_fold_left_optab_fn(FN, STMT, OPTAB) \
4138 : : expand_direct_optab_fn (FN, STMT, OPTAB, 3)
4139 : :
4140 : : #define expand_mask_len_fold_left_optab_fn(FN, STMT, OPTAB) \
4141 : : expand_direct_optab_fn (FN, STMT, OPTAB, 5)
4142 : :
4143 : : #define expand_check_ptrs_optab_fn(FN, STMT, OPTAB) \
4144 : : expand_direct_optab_fn (FN, STMT, OPTAB, 4)
4145 : :
4146 : : /* Expanders for optabs that can use expand_convert_optab_fn. */
4147 : :
4148 : : #define expand_unary_convert_optab_fn(FN, STMT, OPTAB) \
4149 : : expand_convert_optab_fn (FN, STMT, OPTAB, 1)
4150 : :
4151 : : #define expand_vec_extract_optab_fn(FN, STMT, OPTAB) \
4152 : : expand_convert_optab_fn (FN, STMT, OPTAB, 2)
4153 : :
4154 : : /* RETURN_TYPE and ARGS are a return type and argument list that are
4155 : : in principle compatible with FN (which satisfies direct_internal_fn_p).
4156 : : Return the types that should be used to determine whether the
4157 : : target supports FN. */
4158 : :
4159 : : tree_pair
4160 : 8805 : direct_internal_fn_types (internal_fn fn, tree return_type, tree *args)
4161 : : {
4162 : 8805 : const direct_internal_fn_info &info = direct_internal_fn (fn);
4163 : 8805 : tree type0 = (info.type0 < 0 ? return_type : TREE_TYPE (args[info.type0]));
4164 : 8805 : tree type1 = (info.type1 < 0 ? return_type : TREE_TYPE (args[info.type1]));
4165 : 8805 : return tree_pair (type0, type1);
4166 : : }
4167 : :
4168 : : /* CALL is a call whose return type and arguments are in principle
4169 : : compatible with FN (which satisfies direct_internal_fn_p). Return the
4170 : : types that should be used to determine whether the target supports FN. */
4171 : :
4172 : : tree_pair
4173 : 145077 : direct_internal_fn_types (internal_fn fn, gcall *call)
4174 : : {
4175 : 145077 : const direct_internal_fn_info &info = direct_internal_fn (fn);
4176 : 145077 : tree op0 = (info.type0 < 0
4177 : 145077 : ? gimple_call_lhs (call)
4178 : 143615 : : gimple_call_arg (call, info.type0));
4179 : 145077 : tree op1 = (info.type1 < 0
4180 : 145077 : ? gimple_call_lhs (call)
4181 : 145044 : : gimple_call_arg (call, info.type1));
4182 : 145077 : return tree_pair (TREE_TYPE (op0), TREE_TYPE (op1));
4183 : : }
4184 : :
4185 : : /* Return true if OPTAB is supported for TYPES (whose modes should be
4186 : : the same) when the optimization type is OPT_TYPE. Used for simple
4187 : : direct optabs. */
4188 : :
4189 : : static bool
4190 : 5438674 : direct_optab_supported_p (direct_optab optab, tree_pair types,
4191 : : optimization_type opt_type)
4192 : : {
4193 : 5438674 : machine_mode mode = TYPE_MODE (types.first);
4194 : 5438674 : gcc_checking_assert (mode == TYPE_MODE (types.second));
4195 : 5438674 : return direct_optab_handler (optab, mode, opt_type) != CODE_FOR_nothing;
4196 : : }
4197 : :
4198 : : /* Return true if OPTAB is supported for TYPES, where the first type
4199 : : is the destination and the second type is the source. Used for
4200 : : convert optabs. */
4201 : :
4202 : : static bool
4203 : 1807 : convert_optab_supported_p (convert_optab optab, tree_pair types,
4204 : : optimization_type opt_type)
4205 : : {
4206 : 1807 : return (convert_optab_handler (optab, TYPE_MODE (types.first),
4207 : 1807 : TYPE_MODE (types.second), opt_type)
4208 : 1807 : != CODE_FOR_nothing);
4209 : : }
4210 : :
4211 : : /* Return true if load/store lanes optab OPTAB is supported for
4212 : : array type TYPES.first when the optimization type is OPT_TYPE. */
4213 : :
4214 : : static bool
4215 : 0 : multi_vector_optab_supported_p (convert_optab optab, tree_pair types,
4216 : : optimization_type opt_type)
4217 : : {
4218 : 0 : gcc_assert (TREE_CODE (types.first) == ARRAY_TYPE);
4219 : 0 : machine_mode imode = TYPE_MODE (types.first);
4220 : 0 : machine_mode vmode = TYPE_MODE (TREE_TYPE (types.first));
4221 : 0 : return (convert_optab_handler (optab, imode, vmode, opt_type)
4222 : 0 : != CODE_FOR_nothing);
4223 : : }
4224 : :
4225 : : #define direct_unary_optab_supported_p direct_optab_supported_p
4226 : : #define direct_unary_convert_optab_supported_p convert_optab_supported_p
4227 : : #define direct_binary_optab_supported_p direct_optab_supported_p
4228 : : #define direct_ternary_optab_supported_p direct_optab_supported_p
4229 : : #define direct_cond_unary_optab_supported_p direct_optab_supported_p
4230 : : #define direct_cond_binary_optab_supported_p direct_optab_supported_p
4231 : : #define direct_cond_ternary_optab_supported_p direct_optab_supported_p
4232 : : #define direct_cond_len_unary_optab_supported_p direct_optab_supported_p
4233 : : #define direct_cond_len_binary_optab_supported_p direct_optab_supported_p
4234 : : #define direct_cond_len_ternary_optab_supported_p direct_optab_supported_p
4235 : : #define direct_crc_optab_supported_p convert_optab_supported_p
4236 : : #define direct_mask_load_optab_supported_p convert_optab_supported_p
4237 : : #define direct_load_lanes_optab_supported_p multi_vector_optab_supported_p
4238 : : #define direct_mask_load_lanes_optab_supported_p multi_vector_optab_supported_p
4239 : : #define direct_gather_load_optab_supported_p convert_optab_supported_p
4240 : : #define direct_strided_load_optab_supported_p direct_optab_supported_p
4241 : : #define direct_len_load_optab_supported_p direct_optab_supported_p
4242 : : #define direct_mask_len_load_optab_supported_p convert_optab_supported_p
4243 : : #define direct_mask_store_optab_supported_p convert_optab_supported_p
4244 : : #define direct_store_lanes_optab_supported_p multi_vector_optab_supported_p
4245 : : #define direct_mask_store_lanes_optab_supported_p multi_vector_optab_supported_p
4246 : : #define direct_vec_cond_mask_optab_supported_p convert_optab_supported_p
4247 : : #define direct_vec_cond_optab_supported_p convert_optab_supported_p
4248 : : #define direct_scatter_store_optab_supported_p convert_optab_supported_p
4249 : : #define direct_strided_store_optab_supported_p direct_optab_supported_p
4250 : : #define direct_len_store_optab_supported_p direct_optab_supported_p
4251 : : #define direct_mask_len_store_optab_supported_p convert_optab_supported_p
4252 : : #define direct_while_optab_supported_p convert_optab_supported_p
4253 : : #define direct_fold_extract_optab_supported_p direct_optab_supported_p
4254 : : #define direct_fold_len_extract_optab_supported_p direct_optab_supported_p
4255 : : #define direct_fold_left_optab_supported_p direct_optab_supported_p
4256 : : #define direct_mask_fold_left_optab_supported_p direct_optab_supported_p
4257 : : #define direct_mask_len_fold_left_optab_supported_p direct_optab_supported_p
4258 : : #define direct_check_ptrs_optab_supported_p direct_optab_supported_p
4259 : : #define direct_vec_set_optab_supported_p direct_optab_supported_p
4260 : : #define direct_vec_extract_optab_supported_p convert_optab_supported_p
4261 : :
4262 : : /* Return the optab used by internal function FN. */
4263 : :
4264 : : optab
4265 : 219656 : direct_internal_fn_optab (internal_fn fn, tree_pair types)
4266 : : {
4267 : 219656 : switch (fn)
4268 : : {
4269 : : #define DEF_INTERNAL_FN(CODE, FLAGS, FNSPEC) \
4270 : : case IFN_##CODE: break;
4271 : : #define DEF_INTERNAL_OPTAB_FN(CODE, FLAGS, OPTAB, TYPE) \
4272 : : case IFN_##CODE: return OPTAB##_optab;
4273 : : #define DEF_INTERNAL_SIGNED_OPTAB_FN(CODE, FLAGS, SELECTOR, SIGNED_OPTAB, \
4274 : : UNSIGNED_OPTAB, TYPE) \
4275 : : case IFN_##CODE: return (TYPE_UNSIGNED (types.SELECTOR) \
4276 : : ? UNSIGNED_OPTAB ## _optab \
4277 : : : SIGNED_OPTAB ## _optab);
4278 : : #include "internal-fn.def"
4279 : :
4280 : : case IFN_LAST:
4281 : : break;
4282 : : }
4283 : 0 : gcc_unreachable ();
4284 : : }
4285 : :
4286 : : /* Return the optab used by internal function FN. */
4287 : :
4288 : : static optab
4289 : 198620 : direct_internal_fn_optab (internal_fn fn)
4290 : : {
4291 : 198620 : switch (fn)
4292 : : {
4293 : : #define DEF_INTERNAL_FN(CODE, FLAGS, FNSPEC) \
4294 : : case IFN_##CODE: break;
4295 : : #define DEF_INTERNAL_OPTAB_FN(CODE, FLAGS, OPTAB, TYPE) \
4296 : : case IFN_##CODE: return OPTAB##_optab;
4297 : : #include "internal-fn.def"
4298 : :
4299 : : case IFN_LAST:
4300 : : break;
4301 : : }
4302 : 0 : gcc_unreachable ();
4303 : : }
4304 : :
4305 : : /* Return true if TYPE's mode has the same format as TYPE, and if there is
4306 : : a 1:1 correspondence between the values that the mode can store and the
4307 : : values that the type can store. */
4308 : :
4309 : : static bool
4310 : 11161543 : type_strictly_matches_mode_p (const_tree type)
4311 : : {
4312 : : /* The masked vector operations have both vector data operands and vector
4313 : : boolean operands. The vector data operands are expected to have a vector
4314 : : mode, but the vector boolean operands can be an integer mode rather than
4315 : : a vector mode, depending on how TARGET_VECTORIZE_GET_MASK_MODE is
4316 : : defined. PR116103. */
4317 : 769386 : if (VECTOR_BOOLEAN_TYPE_P (type)
4318 : 72985 : && SCALAR_INT_MODE_P (TYPE_MODE (type))
4319 : 11164168 : && TYPE_PRECISION (TREE_TYPE (type)) == 1)
4320 : : return true;
4321 : :
4322 : 11158920 : if (VECTOR_TYPE_P (type))
4323 : 766763 : return VECTOR_MODE_P (TYPE_MODE (type));
4324 : :
4325 : 10392157 : if (INTEGRAL_TYPE_P (type))
4326 : 9921990 : return type_has_mode_precision_p (type);
4327 : :
4328 : 470167 : if (SCALAR_FLOAT_TYPE_P (type) || COMPLEX_FLOAT_TYPE_P (type))
4329 : : return true;
4330 : :
4331 : : return false;
4332 : : }
4333 : :
4334 : : /* Returns true if both types of TYPE_PAIR strictly match their modes,
4335 : : else returns false. */
4336 : :
4337 : : static bool
4338 : 5721062 : type_pair_strictly_matches_mode_p (tree_pair type_pair)
4339 : : {
4340 : 5721062 : return type_strictly_matches_mode_p (type_pair.first)
4341 : 5721062 : && type_strictly_matches_mode_p (type_pair.second);
4342 : : }
4343 : :
4344 : : /* Return true if FN is supported for the types in TYPES when the
4345 : : optimization type is OPT_TYPE. The types are those associated with
4346 : : the "type0" and "type1" fields of FN's direct_internal_fn_info
4347 : : structure. */
4348 : :
4349 : : bool
4350 : 5721062 : direct_internal_fn_supported_p (internal_fn fn, tree_pair types,
4351 : : optimization_type opt_type)
4352 : : {
4353 : 5721062 : if (!type_pair_strictly_matches_mode_p (types))
4354 : : return false;
4355 : :
4356 : 5440481 : switch (fn)
4357 : : {
4358 : : #define DEF_INTERNAL_FN(CODE, FLAGS, FNSPEC) \
4359 : : case IFN_##CODE: break;
4360 : : #define DEF_INTERNAL_OPTAB_FN(CODE, FLAGS, OPTAB, TYPE) \
4361 : : case IFN_##CODE: \
4362 : : return direct_##TYPE##_optab_supported_p (OPTAB##_optab, types, \
4363 : : opt_type);
4364 : : #define DEF_INTERNAL_SIGNED_OPTAB_FN(CODE, FLAGS, SELECTOR, SIGNED_OPTAB, \
4365 : : UNSIGNED_OPTAB, TYPE) \
4366 : : case IFN_##CODE: \
4367 : : { \
4368 : : optab which_optab = (TYPE_UNSIGNED (types.SELECTOR) \
4369 : : ? UNSIGNED_OPTAB ## _optab \
4370 : : : SIGNED_OPTAB ## _optab); \
4371 : : return direct_##TYPE##_optab_supported_p (which_optab, types, \
4372 : : opt_type); \
4373 : : }
4374 : : #include "internal-fn.def"
4375 : :
4376 : : case IFN_LAST:
4377 : : break;
4378 : : }
4379 : 0 : gcc_unreachable ();
4380 : : }
4381 : :
4382 : : /* Return true if FN is supported for type TYPE when the optimization
4383 : : type is OPT_TYPE. The caller knows that the "type0" and "type1"
4384 : : fields of FN's direct_internal_fn_info structure are the same. */
4385 : :
4386 : : bool
4387 : 5641207 : direct_internal_fn_supported_p (internal_fn fn, tree type,
4388 : : optimization_type opt_type)
4389 : : {
4390 : 5641207 : const direct_internal_fn_info &info = direct_internal_fn (fn);
4391 : 5641207 : gcc_checking_assert (info.type0 == info.type1);
4392 : 5641207 : return direct_internal_fn_supported_p (fn, tree_pair (type, type), opt_type);
4393 : : }
4394 : :
4395 : : /* Return true if the STMT is supported when the optimization type is OPT_TYPE,
4396 : : given that STMT is a call to a direct internal function. */
4397 : :
4398 : : bool
4399 : 445 : direct_internal_fn_supported_p (gcall *stmt, optimization_type opt_type)
4400 : : {
4401 : 445 : internal_fn fn = gimple_call_internal_fn (stmt);
4402 : 445 : tree_pair types = direct_internal_fn_types (fn, stmt);
4403 : 445 : return direct_internal_fn_supported_p (fn, types, opt_type);
4404 : : }
4405 : :
4406 : : /* Return true if FN is a binary operation and if FN is commutative. */
4407 : :
4408 : : bool
4409 : 32791179 : commutative_binary_fn_p (internal_fn fn)
4410 : : {
4411 : 32791179 : switch (fn)
4412 : : {
4413 : : case IFN_AVG_FLOOR:
4414 : : case IFN_AVG_CEIL:
4415 : : case IFN_MULH:
4416 : : case IFN_MULHS:
4417 : : case IFN_MULHRS:
4418 : : case IFN_FMIN:
4419 : : case IFN_FMAX:
4420 : : case IFN_COMPLEX_MUL:
4421 : : case IFN_UBSAN_CHECK_ADD:
4422 : : case IFN_UBSAN_CHECK_MUL:
4423 : : case IFN_ADD_OVERFLOW:
4424 : : case IFN_MUL_OVERFLOW:
4425 : : case IFN_SAT_ADD:
4426 : : case IFN_VEC_WIDEN_PLUS:
4427 : : case IFN_VEC_WIDEN_PLUS_LO:
4428 : : case IFN_VEC_WIDEN_PLUS_HI:
4429 : : case IFN_VEC_WIDEN_PLUS_EVEN:
4430 : : case IFN_VEC_WIDEN_PLUS_ODD:
4431 : : return true;
4432 : :
4433 : 31902892 : default:
4434 : 31902892 : return false;
4435 : : }
4436 : : }
4437 : :
4438 : : /* Return true if FN is a ternary operation and if its first two arguments
4439 : : are commutative. */
4440 : :
4441 : : bool
4442 : 26473063 : commutative_ternary_fn_p (internal_fn fn)
4443 : : {
4444 : 26473063 : switch (fn)
4445 : : {
4446 : : case IFN_FMA:
4447 : : case IFN_FMS:
4448 : : case IFN_FNMA:
4449 : : case IFN_FNMS:
4450 : : case IFN_UADDC:
4451 : : return true;
4452 : :
4453 : 26342434 : default:
4454 : 26342434 : return false;
4455 : : }
4456 : : }
4457 : :
4458 : : /* Return true if FN is an associative binary operation. */
4459 : :
4460 : : bool
4461 : 48 : associative_binary_fn_p (internal_fn fn)
4462 : : {
4463 : 48 : switch (fn)
4464 : : {
4465 : : case IFN_FMIN:
4466 : : case IFN_FMAX:
4467 : : return true;
4468 : :
4469 : 0 : default:
4470 : 0 : return false;
4471 : : }
4472 : : }
4473 : :
4474 : : /* If FN is commutative in two consecutive arguments, return the
4475 : : index of the first, otherwise return -1. */
4476 : :
4477 : : int
4478 : 26792984 : first_commutative_argument (internal_fn fn)
4479 : : {
4480 : 26792984 : switch (fn)
4481 : : {
4482 : : case IFN_COND_ADD:
4483 : : case IFN_COND_MUL:
4484 : : case IFN_COND_MIN:
4485 : : case IFN_COND_MAX:
4486 : : case IFN_COND_FMIN:
4487 : : case IFN_COND_FMAX:
4488 : : case IFN_COND_AND:
4489 : : case IFN_COND_IOR:
4490 : : case IFN_COND_XOR:
4491 : : case IFN_COND_FMA:
4492 : : case IFN_COND_FMS:
4493 : : case IFN_COND_FNMA:
4494 : : case IFN_COND_FNMS:
4495 : : case IFN_COND_LEN_ADD:
4496 : : case IFN_COND_LEN_MUL:
4497 : : case IFN_COND_LEN_MIN:
4498 : : case IFN_COND_LEN_MAX:
4499 : : case IFN_COND_LEN_FMIN:
4500 : : case IFN_COND_LEN_FMAX:
4501 : : case IFN_COND_LEN_AND:
4502 : : case IFN_COND_LEN_IOR:
4503 : : case IFN_COND_LEN_XOR:
4504 : : case IFN_COND_LEN_FMA:
4505 : : case IFN_COND_LEN_FMS:
4506 : : case IFN_COND_LEN_FNMA:
4507 : : case IFN_COND_LEN_FNMS:
4508 : : return 1;
4509 : :
4510 : 26778016 : default:
4511 : 26778016 : if (commutative_binary_fn_p (fn)
4512 : 26778016 : || commutative_ternary_fn_p (fn))
4513 : 435582 : return 0;
4514 : : return -1;
4515 : : }
4516 : : }
4517 : :
4518 : : /* Return true if this CODE describes an internal_fn that returns a vector with
4519 : : elements twice as wide as the element size of the input vectors. */
4520 : :
4521 : : bool
4522 : 1832414 : widening_fn_p (code_helper code)
4523 : : {
4524 : 1832414 : if (!code.is_fn_code ())
4525 : : return false;
4526 : :
4527 : 168421 : if (!internal_fn_p ((combined_fn) code))
4528 : : return false;
4529 : :
4530 : 168421 : internal_fn fn = as_internal_fn ((combined_fn) code);
4531 : 168421 : switch (fn)
4532 : : {
4533 : : #define DEF_INTERNAL_WIDENING_OPTAB_FN(NAME, F, S, SO, UO, T) \
4534 : : case IFN_##NAME: \
4535 : : case IFN_##NAME##_HI: \
4536 : : case IFN_##NAME##_LO: \
4537 : : case IFN_##NAME##_EVEN: \
4538 : : case IFN_##NAME##_ODD: \
4539 : : return true;
4540 : : #include "internal-fn.def"
4541 : :
4542 : : default:
4543 : : return false;
4544 : : }
4545 : : }
4546 : :
4547 : : /* Return true if IFN_SET_EDOM is supported. */
4548 : :
4549 : : bool
4550 : 135 : set_edom_supported_p (void)
4551 : : {
4552 : : #ifdef TARGET_EDOM
4553 : : return true;
4554 : : #else
4555 : 135 : return false;
4556 : : #endif
4557 : : }
4558 : :
4559 : : #define DEF_INTERNAL_OPTAB_FN(CODE, FLAGS, OPTAB, TYPE) \
4560 : : static void \
4561 : : expand_##CODE (internal_fn fn, gcall *stmt) \
4562 : : { \
4563 : : expand_##TYPE##_optab_fn (fn, stmt, OPTAB##_optab); \
4564 : : }
4565 : : #define DEF_INTERNAL_INT_EXT_FN(CODE, FLAGS, OPTAB, TYPE)
4566 : : #define DEF_INTERNAL_SIGNED_OPTAB_FN(CODE, FLAGS, SELECTOR, SIGNED_OPTAB, \
4567 : : UNSIGNED_OPTAB, TYPE) \
4568 : : static void \
4569 : : expand_##CODE (internal_fn fn, gcall *stmt) \
4570 : : { \
4571 : : tree_pair types = direct_internal_fn_types (fn, stmt); \
4572 : : optab which_optab = direct_internal_fn_optab (fn, types); \
4573 : : expand_##TYPE##_optab_fn (fn, stmt, which_optab); \
4574 : : }
4575 : : #include "internal-fn.def"
4576 : :
4577 : : /* Routines to expand each internal function, indexed by function number.
4578 : : Each routine has the prototype:
4579 : :
4580 : : expand_<NAME> (gcall *stmt)
4581 : :
4582 : : where STMT is the statement that performs the call. */
4583 : : static void (*const internal_fn_expanders[]) (internal_fn, gcall *) = {
4584 : :
4585 : : #define DEF_INTERNAL_FN(CODE, FLAGS, FNSPEC) expand_##CODE,
4586 : : #include "internal-fn.def"
4587 : : 0
4588 : : };
4589 : :
4590 : : /* Invoke T(CODE, SUFFIX) for each conditional function IFN_COND_##SUFFIX
4591 : : that maps to a tree code CODE. There is also an IFN_COND_LEN_##SUFFIX
4592 : : for each such IFN_COND_##SUFFIX. */
4593 : : #define FOR_EACH_CODE_MAPPING(T) \
4594 : : T (PLUS_EXPR, ADD) \
4595 : : T (MINUS_EXPR, SUB) \
4596 : : T (MULT_EXPR, MUL) \
4597 : : T (TRUNC_DIV_EXPR, DIV) \
4598 : : T (TRUNC_MOD_EXPR, MOD) \
4599 : : T (RDIV_EXPR, RDIV) \
4600 : : T (MIN_EXPR, MIN) \
4601 : : T (MAX_EXPR, MAX) \
4602 : : T (BIT_AND_EXPR, AND) \
4603 : : T (BIT_IOR_EXPR, IOR) \
4604 : : T (BIT_XOR_EXPR, XOR) \
4605 : : T (LSHIFT_EXPR, SHL) \
4606 : : T (RSHIFT_EXPR, SHR) \
4607 : : T (NEGATE_EXPR, NEG)
4608 : :
4609 : : /* Return a function that only performs CODE when a certain condition is met
4610 : : and that uses a given fallback value otherwise. For example, if CODE is
4611 : : a binary operation associated with conditional function FN:
4612 : :
4613 : : LHS = FN (COND, A, B, ELSE)
4614 : :
4615 : : is equivalent to the C expression:
4616 : :
4617 : : LHS = COND ? A CODE B : ELSE;
4618 : :
4619 : : operating elementwise if the operands are vectors.
4620 : :
4621 : : Return IFN_LAST if no such function exists. */
4622 : :
4623 : : internal_fn
4624 : 564778 : get_conditional_internal_fn (tree_code code)
4625 : : {
4626 : 564778 : switch (code)
4627 : : {
4628 : : #define CASE(CODE, IFN) case CODE: return IFN_COND_##IFN;
4629 : : FOR_EACH_CODE_MAPPING(CASE)
4630 : : #undef CASE
4631 : : default:
4632 : : return IFN_LAST;
4633 : : }
4634 : : }
4635 : :
4636 : : /* If IFN implements the conditional form of a tree code, return that
4637 : : tree code, otherwise return ERROR_MARK. */
4638 : :
4639 : : tree_code
4640 : 2616994 : conditional_internal_fn_code (internal_fn ifn)
4641 : : {
4642 : 2616994 : switch (ifn)
4643 : : {
4644 : : #define CASE(CODE, IFN) \
4645 : : case IFN_COND_##IFN: \
4646 : : case IFN_COND_LEN_##IFN: \
4647 : : return CODE;
4648 : : FOR_EACH_CODE_MAPPING (CASE)
4649 : : #undef CASE
4650 : : default:
4651 : : return ERROR_MARK;
4652 : : }
4653 : : }
4654 : :
4655 : : /* Like get_conditional_internal_fn, but return a function that
4656 : : additionally restricts the operation to the leading elements
4657 : : of a vector. The number of elements to process is given by a length
4658 : : and bias pair, as for IFN_LOAD_LEN. The values of the remaining
4659 : : elements are taken from the fallback ("else") argument.
4660 : :
4661 : : For example, if CODE is a binary operation associated with FN:
4662 : :
4663 : : LHS = FN (COND, A, B, ELSE, LEN, BIAS)
4664 : :
4665 : : is equivalent to the C code:
4666 : :
4667 : : for (int i = 0; i < NUNITS; i++)
4668 : : {
4669 : : if (i < LEN + BIAS && COND[i])
4670 : : LHS[i] = A[i] CODE B[i];
4671 : : else
4672 : : LHS[i] = ELSE[i];
4673 : : }
4674 : : */
4675 : :
4676 : : internal_fn
4677 : 547478 : get_conditional_len_internal_fn (tree_code code)
4678 : : {
4679 : 547478 : switch (code)
4680 : : {
4681 : : #define CASE(CODE, IFN) case CODE: return IFN_COND_LEN_##IFN;
4682 : : FOR_EACH_CODE_MAPPING(CASE)
4683 : : #undef CASE
4684 : : default:
4685 : : return IFN_LAST;
4686 : : }
4687 : : }
4688 : :
4689 : : /* Invoke T(IFN) for each internal function IFN that also has an
4690 : : IFN_COND_* form. */
4691 : : #define FOR_EACH_COND_FN_PAIR(T) \
4692 : : T (FMAX) \
4693 : : T (FMIN) \
4694 : : T (FMA) \
4695 : : T (FMS) \
4696 : : T (FNMA) \
4697 : : T (FNMS)
4698 : :
4699 : : /* Return a function that only performs internal function FN when a
4700 : : certain condition is met and that uses a given fallback value otherwise.
4701 : : In other words, the returned function FN' is such that:
4702 : :
4703 : : LHS = FN' (COND, A1, ... An, ELSE)
4704 : :
4705 : : is equivalent to the C expression:
4706 : :
4707 : : LHS = COND ? FN (A1, ..., An) : ELSE;
4708 : :
4709 : : operating elementwise if the operands are vectors.
4710 : :
4711 : : Return IFN_LAST if no such function exists. */
4712 : :
4713 : : internal_fn
4714 : 9777 : get_conditional_internal_fn (internal_fn fn)
4715 : : {
4716 : 9777 : switch (fn)
4717 : : {
4718 : : #define CASE(NAME) case IFN_##NAME: return IFN_COND_##NAME;
4719 : 757 : FOR_EACH_COND_FN_PAIR(CASE)
4720 : : #undef CASE
4721 : 9020 : default:
4722 : 9020 : return IFN_LAST;
4723 : : }
4724 : : }
4725 : :
4726 : : /* If there exists an internal function like IFN that operates on vectors,
4727 : : but with additional length and bias parameters, return the internal_fn
4728 : : for that function, otherwise return IFN_LAST. */
4729 : : internal_fn
4730 : 9072 : get_len_internal_fn (internal_fn fn)
4731 : : {
4732 : 9072 : switch (fn)
4733 : : {
4734 : : #define DEF_INTERNAL_COND_FN(NAME, ...) \
4735 : : case IFN_COND_##NAME: \
4736 : : return IFN_COND_LEN_##NAME;
4737 : : #define DEF_INTERNAL_SIGNED_COND_FN(NAME, ...) \
4738 : : case IFN_COND_##NAME: \
4739 : : return IFN_COND_LEN_##NAME;
4740 : : #include "internal-fn.def"
4741 : 6632 : default:
4742 : 6632 : break;
4743 : : }
4744 : :
4745 : 6632 : switch (fn)
4746 : : {
4747 : : case IFN_MASK_LOAD:
4748 : : return IFN_MASK_LEN_LOAD;
4749 : : case IFN_MASK_LOAD_LANES:
4750 : : return IFN_MASK_LEN_LOAD_LANES;
4751 : : case IFN_MASK_GATHER_LOAD:
4752 : : return IFN_MASK_LEN_GATHER_LOAD;
4753 : : default:
4754 : : return IFN_LAST;
4755 : : }
4756 : : }
4757 : :
4758 : : /* If IFN implements the conditional form of an unconditional internal
4759 : : function, return that unconditional function, otherwise return IFN_LAST. */
4760 : :
4761 : : internal_fn
4762 : 2578285 : get_unconditional_internal_fn (internal_fn ifn)
4763 : : {
4764 : 2578285 : switch (ifn)
4765 : : {
4766 : : #define CASE(NAME) \
4767 : : case IFN_COND_##NAME: \
4768 : : case IFN_COND_LEN_##NAME: \
4769 : : return IFN_##NAME;
4770 : : FOR_EACH_COND_FN_PAIR (CASE)
4771 : : #undef CASE
4772 : : default:
4773 : : return IFN_LAST;
4774 : : }
4775 : : }
4776 : :
4777 : : /* Return true if STMT can be interpreted as a conditional tree code
4778 : : operation of the form:
4779 : :
4780 : : LHS = COND ? OP (RHS1, ...) : ELSE;
4781 : :
4782 : : operating elementwise if the operands are vectors. This includes
4783 : : the case of an all-true COND, so that the operation always happens.
4784 : :
4785 : : There is an alternative approach to interpret the STMT when the operands
4786 : : are vectors which is the operation predicated by both conditional mask
4787 : : and loop control length, the equivalent C code:
4788 : :
4789 : : for (int i = 0; i < NUNTIS; i++)
4790 : : {
4791 : : if (i < LEN + BIAS && COND[i])
4792 : : LHS[i] = A[i] CODE B[i];
4793 : : else
4794 : : LHS[i] = ELSE[i];
4795 : : }
4796 : :
4797 : : When returning true, set:
4798 : :
4799 : : - *COND_OUT to the condition COND, or to NULL_TREE if the condition
4800 : : is known to be all-true
4801 : : - *CODE_OUT to the tree code
4802 : : - OPS[I] to operand I of *CODE_OUT
4803 : : - *ELSE_OUT to the fallback value ELSE, or to NULL_TREE if the
4804 : : condition is known to be all true.
4805 : : - *LEN to the len argument if it COND_LEN_* operations or to NULL_TREE.
4806 : : - *BIAS to the bias argument if it COND_LEN_* operations or to NULL_TREE. */
4807 : :
4808 : : bool
4809 : 41189 : can_interpret_as_conditional_op_p (gimple *stmt, tree *cond_out,
4810 : : tree_code *code_out,
4811 : : tree (&ops)[3], tree *else_out,
4812 : : tree *len, tree *bias)
4813 : : {
4814 : 41189 : *len = NULL_TREE;
4815 : 41189 : *bias = NULL_TREE;
4816 : 41189 : if (gassign *assign = dyn_cast <gassign *> (stmt))
4817 : : {
4818 : 39050 : *cond_out = NULL_TREE;
4819 : 39050 : *code_out = gimple_assign_rhs_code (assign);
4820 : 39050 : ops[0] = gimple_assign_rhs1 (assign);
4821 : 39050 : ops[1] = gimple_assign_rhs2 (assign);
4822 : 39050 : ops[2] = gimple_assign_rhs3 (assign);
4823 : 39050 : *else_out = NULL_TREE;
4824 : 39050 : return true;
4825 : : }
4826 : 2139 : if (gcall *call = dyn_cast <gcall *> (stmt))
4827 : 1564 : if (gimple_call_internal_p (call))
4828 : : {
4829 : 1534 : internal_fn ifn = gimple_call_internal_fn (call);
4830 : 1534 : tree_code code = conditional_internal_fn_code (ifn);
4831 : 1534 : int len_index = internal_fn_len_index (ifn);
4832 : 1534 : int cond_nargs = len_index >= 0 ? 4 : 2;
4833 : 1534 : if (code != ERROR_MARK)
4834 : : {
4835 : 216 : *cond_out = gimple_call_arg (call, 0);
4836 : 216 : *code_out = code;
4837 : 216 : unsigned int nops = gimple_call_num_args (call) - cond_nargs;
4838 : 864 : for (unsigned int i = 0; i < 3; ++i)
4839 : 648 : ops[i] = i < nops ? gimple_call_arg (call, i + 1) : NULL_TREE;
4840 : 216 : *else_out = gimple_call_arg (call, nops + 1);
4841 : 216 : if (len_index < 0)
4842 : : {
4843 : 216 : if (integer_truep (*cond_out))
4844 : : {
4845 : 0 : *cond_out = NULL_TREE;
4846 : 0 : *else_out = NULL_TREE;
4847 : : }
4848 : : }
4849 : : else
4850 : : {
4851 : 0 : *len = gimple_call_arg (call, len_index);
4852 : 0 : *bias = gimple_call_arg (call, len_index + 1);
4853 : : }
4854 : 216 : return true;
4855 : : }
4856 : : }
4857 : : return false;
4858 : : }
4859 : :
4860 : : /* Return true if IFN is some form of load from memory. */
4861 : :
4862 : : bool
4863 : 253277 : internal_load_fn_p (internal_fn fn)
4864 : : {
4865 : 253277 : switch (fn)
4866 : : {
4867 : : case IFN_MASK_LOAD:
4868 : : case IFN_LOAD_LANES:
4869 : : case IFN_MASK_LOAD_LANES:
4870 : : case IFN_MASK_LEN_LOAD_LANES:
4871 : : case IFN_GATHER_LOAD:
4872 : : case IFN_MASK_GATHER_LOAD:
4873 : : case IFN_MASK_LEN_GATHER_LOAD:
4874 : : case IFN_LEN_LOAD:
4875 : : case IFN_MASK_LEN_LOAD:
4876 : : return true;
4877 : :
4878 : 120546 : default:
4879 : 120546 : return false;
4880 : : }
4881 : : }
4882 : :
4883 : : /* Return true if IFN is some form of store to memory. */
4884 : :
4885 : : bool
4886 : 283648 : internal_store_fn_p (internal_fn fn)
4887 : : {
4888 : 283648 : switch (fn)
4889 : : {
4890 : : case IFN_MASK_STORE:
4891 : : case IFN_STORE_LANES:
4892 : : case IFN_MASK_STORE_LANES:
4893 : : case IFN_MASK_LEN_STORE_LANES:
4894 : : case IFN_SCATTER_STORE:
4895 : : case IFN_MASK_SCATTER_STORE:
4896 : : case IFN_MASK_LEN_SCATTER_STORE:
4897 : : case IFN_LEN_STORE:
4898 : : case IFN_MASK_LEN_STORE:
4899 : : return true;
4900 : :
4901 : 268877 : default:
4902 : 268877 : return false;
4903 : : }
4904 : : }
4905 : :
4906 : : /* Return true if IFN is some form of gather load or scatter store. */
4907 : :
4908 : : bool
4909 : 13147 : internal_gather_scatter_fn_p (internal_fn fn)
4910 : : {
4911 : 13147 : switch (fn)
4912 : : {
4913 : : case IFN_GATHER_LOAD:
4914 : : case IFN_MASK_GATHER_LOAD:
4915 : : case IFN_MASK_LEN_GATHER_LOAD:
4916 : : case IFN_SCATTER_STORE:
4917 : : case IFN_MASK_SCATTER_STORE:
4918 : : case IFN_MASK_LEN_SCATTER_STORE:
4919 : : return true;
4920 : :
4921 : 13147 : default:
4922 : 13147 : return false;
4923 : : }
4924 : : }
4925 : :
4926 : : /* If FN takes a vector len argument, return the index of that argument,
4927 : : otherwise return -1. */
4928 : :
4929 : : int
4930 : 2482639 : internal_fn_len_index (internal_fn fn)
4931 : : {
4932 : 2482639 : switch (fn)
4933 : : {
4934 : : case IFN_LEN_LOAD:
4935 : : case IFN_LEN_STORE:
4936 : : return 2;
4937 : :
4938 : : case IFN_MASK_LEN_SCATTER_STORE:
4939 : : case IFN_MASK_LEN_STRIDED_LOAD:
4940 : : return 5;
4941 : :
4942 : : case IFN_MASK_LEN_GATHER_LOAD:
4943 : : return 6;
4944 : :
4945 : : case IFN_COND_LEN_FMA:
4946 : : case IFN_COND_LEN_FMS:
4947 : : case IFN_COND_LEN_FNMA:
4948 : : case IFN_COND_LEN_FNMS:
4949 : : return 5;
4950 : :
4951 : : case IFN_COND_LEN_ADD:
4952 : : case IFN_COND_LEN_SUB:
4953 : : case IFN_COND_LEN_MUL:
4954 : : case IFN_COND_LEN_DIV:
4955 : : case IFN_COND_LEN_MOD:
4956 : : case IFN_COND_LEN_RDIV:
4957 : : case IFN_COND_LEN_MIN:
4958 : : case IFN_COND_LEN_MAX:
4959 : : case IFN_COND_LEN_FMIN:
4960 : : case IFN_COND_LEN_FMAX:
4961 : : case IFN_COND_LEN_AND:
4962 : : case IFN_COND_LEN_IOR:
4963 : : case IFN_COND_LEN_XOR:
4964 : : case IFN_COND_LEN_SHL:
4965 : : case IFN_COND_LEN_SHR:
4966 : : case IFN_MASK_LEN_STRIDED_STORE:
4967 : : return 4;
4968 : :
4969 : : case IFN_COND_LEN_NEG:
4970 : : case IFN_MASK_LEN_STORE:
4971 : : case IFN_MASK_LEN_STORE_LANES:
4972 : : case IFN_VCOND_MASK_LEN:
4973 : : return 3;
4974 : :
4975 : : case IFN_MASK_LEN_LOAD:
4976 : : case IFN_MASK_LEN_LOAD_LANES:
4977 : : return 4;
4978 : :
4979 : : default:
4980 : : return -1;
4981 : : }
4982 : : }
4983 : :
4984 : : /* If FN is an IFN_COND_* or IFN_COND_LEN_* function, return the index of the
4985 : : argument that is used when the condition is false. Return -1 otherwise. */
4986 : :
4987 : : int
4988 : 16690 : internal_fn_else_index (internal_fn fn)
4989 : : {
4990 : 16690 : switch (fn)
4991 : : {
4992 : : case IFN_COND_NEG:
4993 : : case IFN_COND_NOT:
4994 : : case IFN_COND_LEN_NEG:
4995 : : case IFN_COND_LEN_NOT:
4996 : : return 2;
4997 : :
4998 : : case IFN_COND_ADD:
4999 : : case IFN_COND_SUB:
5000 : : case IFN_COND_MUL:
5001 : : case IFN_COND_DIV:
5002 : : case IFN_COND_MOD:
5003 : : case IFN_COND_MIN:
5004 : : case IFN_COND_MAX:
5005 : : case IFN_COND_FMIN:
5006 : : case IFN_COND_FMAX:
5007 : : case IFN_COND_AND:
5008 : : case IFN_COND_IOR:
5009 : : case IFN_COND_XOR:
5010 : : case IFN_COND_SHL:
5011 : : case IFN_COND_SHR:
5012 : : case IFN_COND_LEN_ADD:
5013 : : case IFN_COND_LEN_SUB:
5014 : : case IFN_COND_LEN_MUL:
5015 : : case IFN_COND_LEN_DIV:
5016 : : case IFN_COND_LEN_MOD:
5017 : : case IFN_COND_LEN_MIN:
5018 : : case IFN_COND_LEN_MAX:
5019 : : case IFN_COND_LEN_FMIN:
5020 : : case IFN_COND_LEN_FMAX:
5021 : : case IFN_COND_LEN_AND:
5022 : : case IFN_COND_LEN_IOR:
5023 : : case IFN_COND_LEN_XOR:
5024 : : case IFN_COND_LEN_SHL:
5025 : : case IFN_COND_LEN_SHR:
5026 : : return 3;
5027 : :
5028 : : case IFN_MASK_LOAD:
5029 : : case IFN_MASK_LEN_LOAD:
5030 : : case IFN_MASK_LOAD_LANES:
5031 : : case IFN_MASK_LEN_LOAD_LANES:
5032 : : return 3;
5033 : :
5034 : : case IFN_COND_FMA:
5035 : : case IFN_COND_FMS:
5036 : : case IFN_COND_FNMA:
5037 : : case IFN_COND_FNMS:
5038 : : case IFN_COND_LEN_FMA:
5039 : : case IFN_COND_LEN_FMS:
5040 : : case IFN_COND_LEN_FNMA:
5041 : : case IFN_COND_LEN_FNMS:
5042 : : case IFN_MASK_LEN_STRIDED_LOAD:
5043 : : return 4;
5044 : :
5045 : : case IFN_MASK_GATHER_LOAD:
5046 : : case IFN_MASK_LEN_GATHER_LOAD:
5047 : : return 5;
5048 : :
5049 : : default:
5050 : : return -1;
5051 : : }
5052 : :
5053 : : return -1;
5054 : : }
5055 : :
5056 : : /* If FN takes a vector mask argument, return the index of that argument,
5057 : : otherwise return -1. */
5058 : :
5059 : : int
5060 : 163991 : internal_fn_mask_index (internal_fn fn)
5061 : : {
5062 : 163991 : switch (fn)
5063 : : {
5064 : : case IFN_MASK_LOAD:
5065 : : case IFN_MASK_LOAD_LANES:
5066 : : case IFN_MASK_LEN_LOAD_LANES:
5067 : : case IFN_MASK_STORE:
5068 : : case IFN_MASK_STORE_LANES:
5069 : : case IFN_MASK_LEN_STORE_LANES:
5070 : : case IFN_MASK_LEN_LOAD:
5071 : : case IFN_MASK_LEN_STORE:
5072 : : return 2;
5073 : :
5074 : 0 : case IFN_MASK_LEN_STRIDED_LOAD:
5075 : 0 : case IFN_MASK_LEN_STRIDED_STORE:
5076 : 0 : return 3;
5077 : :
5078 : 0 : case IFN_MASK_GATHER_LOAD:
5079 : 0 : case IFN_MASK_SCATTER_STORE:
5080 : 0 : case IFN_MASK_LEN_GATHER_LOAD:
5081 : 0 : case IFN_MASK_LEN_SCATTER_STORE:
5082 : 0 : return 4;
5083 : :
5084 : : case IFN_VCOND_MASK:
5085 : : case IFN_VCOND_MASK_LEN:
5086 : : return 0;
5087 : :
5088 : 135789 : default:
5089 : 135789 : return (conditional_internal_fn_code (fn) != ERROR_MARK
5090 : 135789 : || get_unconditional_internal_fn (fn) != IFN_LAST ? 0 : -1);
5091 : : }
5092 : : }
5093 : :
5094 : : /* If FN takes a value that should be stored to memory, return the index
5095 : : of that argument, otherwise return -1. */
5096 : :
5097 : : int
5098 : 25234 : internal_fn_stored_value_index (internal_fn fn)
5099 : : {
5100 : 25234 : switch (fn)
5101 : : {
5102 : : case IFN_MASK_LEN_STRIDED_STORE:
5103 : : return 2;
5104 : :
5105 : : case IFN_MASK_STORE:
5106 : : case IFN_MASK_STORE_LANES:
5107 : : case IFN_SCATTER_STORE:
5108 : : case IFN_MASK_SCATTER_STORE:
5109 : : case IFN_MASK_LEN_SCATTER_STORE:
5110 : : return 3;
5111 : :
5112 : : case IFN_LEN_STORE:
5113 : : return 4;
5114 : :
5115 : : case IFN_MASK_LEN_STORE:
5116 : : case IFN_MASK_LEN_STORE_LANES:
5117 : : return 5;
5118 : :
5119 : : default:
5120 : : return -1;
5121 : : }
5122 : : }
5123 : :
5124 : :
5125 : : /* Store all supported else values for the optab referred to by ICODE
5126 : : in ELSE_VALS. The index of the else operand must be specified in
5127 : : ELSE_INDEX. */
5128 : :
5129 : : void
5130 : 1476 : get_supported_else_vals (enum insn_code icode, unsigned else_index,
5131 : : vec<int> &else_vals)
5132 : : {
5133 : 1476 : const struct insn_data_d *data = &insn_data[icode];
5134 : 1476 : if ((char)else_index >= data->n_operands)
5135 : : return;
5136 : :
5137 : 1476 : machine_mode else_mode = data->operand[else_index].mode;
5138 : :
5139 : 1476 : else_vals.truncate (0);
5140 : :
5141 : : /* For now we only support else values of 0, -1, and "undefined". */
5142 : 1476 : if (insn_operand_matches (icode, else_index, CONST0_RTX (else_mode)))
5143 : 1476 : else_vals.safe_push (MASK_LOAD_ELSE_ZERO);
5144 : :
5145 : 1476 : if (insn_operand_matches (icode, else_index, gen_rtx_SCRATCH (else_mode)))
5146 : 0 : else_vals.safe_push (MASK_LOAD_ELSE_UNDEFINED);
5147 : :
5148 : 1476 : if (GET_MODE_CLASS (else_mode) == MODE_VECTOR_INT
5149 : 1476 : && insn_operand_matches (icode, else_index, CONSTM1_RTX (else_mode)))
5150 : 0 : else_vals.safe_push (MASK_LOAD_ELSE_M1);
5151 : : }
5152 : :
5153 : : /* Return true if the else value ELSE_VAL (one of MASK_LOAD_ELSE_ZERO,
5154 : : MASK_LOAD_ELSE_M1, and MASK_LOAD_ELSE_UNDEFINED) is valid fo the optab
5155 : : referred to by ICODE. The index of the else operand must be specified
5156 : : in ELSE_INDEX. */
5157 : :
5158 : : bool
5159 : 0 : supported_else_val_p (enum insn_code icode, unsigned else_index, int else_val)
5160 : : {
5161 : 0 : if (else_val != MASK_LOAD_ELSE_ZERO && else_val != MASK_LOAD_ELSE_M1
5162 : 0 : && else_val != MASK_LOAD_ELSE_UNDEFINED)
5163 : 0 : gcc_unreachable ();
5164 : :
5165 : 0 : auto_vec<int> else_vals;
5166 : 0 : get_supported_else_vals (icode, else_index, else_vals);
5167 : 0 : return else_vals.contains (else_val);
5168 : 0 : }
5169 : :
5170 : : /* Return true if the target supports gather load or scatter store function
5171 : : IFN. For loads, VECTOR_TYPE is the vector type of the load result,
5172 : : while for stores it is the vector type of the stored data argument.
5173 : : MEMORY_ELEMENT_TYPE is the type of the memory elements being loaded
5174 : : or stored. OFFSET_VECTOR_TYPE is the vector type that holds the
5175 : : offset from the shared base address of each loaded or stored element.
5176 : : SCALE is the amount by which these offsets should be multiplied
5177 : : *after* they have been extended to address width.
5178 : : If the target supports the gather load the supported else values
5179 : : will be added to the vector ELSVAL points to if it is nonzero. */
5180 : :
5181 : : bool
5182 : 815601 : internal_gather_scatter_fn_supported_p (internal_fn ifn, tree vector_type,
5183 : : tree memory_element_type,
5184 : : tree offset_vector_type, int scale,
5185 : : vec<int> *elsvals)
5186 : : {
5187 : 815601 : if (!tree_int_cst_equal (TYPE_SIZE (TREE_TYPE (vector_type)),
5188 : 815601 : TYPE_SIZE (memory_element_type)))
5189 : : return false;
5190 : 815601 : if (maybe_ne (TYPE_VECTOR_SUBPARTS (vector_type),
5191 : 1631202 : TYPE_VECTOR_SUBPARTS (offset_vector_type)))
5192 : : return false;
5193 : 193524 : optab optab = direct_internal_fn_optab (ifn);
5194 : 193524 : insn_code icode = convert_optab_handler (optab, TYPE_MODE (vector_type),
5195 : 193524 : TYPE_MODE (offset_vector_type));
5196 : 193524 : int output_ops = internal_load_fn_p (ifn) ? 1 : 0;
5197 : 193524 : bool unsigned_p = TYPE_UNSIGNED (TREE_TYPE (offset_vector_type));
5198 : 193524 : bool ok = icode != CODE_FOR_nothing
5199 : 0 : && insn_operand_matches (icode, 2 + output_ops, GEN_INT (unsigned_p))
5200 : 193524 : && insn_operand_matches (icode, 3 + output_ops, GEN_INT (scale));
5201 : :
5202 : : /* For gather the optab's operand indices do not match the IFN's because
5203 : : the latter does not have the extension operand (operand 3). It is
5204 : : implicitly added during expansion so we use the IFN's else index + 1.
5205 : : */
5206 : 193524 : if (ok && elsvals)
5207 : 0 : get_supported_else_vals
5208 : 0 : (icode, internal_fn_else_index (IFN_MASK_GATHER_LOAD) + 1, *elsvals);
5209 : :
5210 : : return ok;
5211 : : }
5212 : :
5213 : : /* Return true if the target supports IFN_CHECK_{RAW,WAR}_PTRS function IFN
5214 : : for pointers of type TYPE when the accesses have LENGTH bytes and their
5215 : : common byte alignment is ALIGN. */
5216 : :
5217 : : bool
5218 : 5096 : internal_check_ptrs_fn_supported_p (internal_fn ifn, tree type,
5219 : : poly_uint64 length, unsigned int align)
5220 : : {
5221 : 5096 : machine_mode mode = TYPE_MODE (type);
5222 : 5096 : optab optab = direct_internal_fn_optab (ifn);
5223 : 5096 : insn_code icode = direct_optab_handler (optab, mode);
5224 : 5096 : if (icode == CODE_FOR_nothing)
5225 : : return false;
5226 : 0 : rtx length_rtx = immed_wide_int_const (length, mode);
5227 : 0 : return (insn_operand_matches (icode, 3, length_rtx)
5228 : 0 : && insn_operand_matches (icode, 4, GEN_INT (align)));
5229 : : }
5230 : :
5231 : : /* Return the supported bias for IFN which is either IFN_{LEN_,MASK_LEN_,}LOAD
5232 : : or IFN_{LEN_,MASK_LEN_,}STORE. For now we only support the biases of 0 and
5233 : : -1 (in case 0 is not an allowable length for {len_,mask_len_}load or
5234 : : {len_,mask_len_}store). If none of the biases match what the backend
5235 : : provides, return VECT_PARTIAL_BIAS_UNSUPPORTED. */
5236 : :
5237 : : signed char
5238 : 0 : internal_len_load_store_bias (internal_fn ifn, machine_mode mode)
5239 : : {
5240 : 0 : optab optab = direct_internal_fn_optab (ifn);
5241 : 0 : insn_code icode = direct_optab_handler (optab, mode);
5242 : 0 : int bias_no = 3;
5243 : :
5244 : 0 : if (icode == CODE_FOR_nothing)
5245 : : {
5246 : 0 : machine_mode mask_mode;
5247 : 0 : if (!targetm.vectorize.get_mask_mode (mode).exists (&mask_mode))
5248 : 0 : return VECT_PARTIAL_BIAS_UNSUPPORTED;
5249 : 0 : if (ifn == IFN_LEN_LOAD)
5250 : : {
5251 : : /* Try MASK_LEN_LOAD. */
5252 : 0 : optab = direct_internal_fn_optab (IFN_MASK_LEN_LOAD);
5253 : : }
5254 : : else
5255 : : {
5256 : : /* Try MASK_LEN_STORE. */
5257 : 0 : optab = direct_internal_fn_optab (IFN_MASK_LEN_STORE);
5258 : : }
5259 : 0 : icode = convert_optab_handler (optab, mode, mask_mode);
5260 : 0 : bias_no = 4;
5261 : : }
5262 : :
5263 : 0 : if (icode != CODE_FOR_nothing)
5264 : : {
5265 : : /* For now we only support biases of 0 or -1. Try both of them. */
5266 : 0 : if (insn_operand_matches (icode, bias_no, GEN_INT (0)))
5267 : : return 0;
5268 : 0 : if (insn_operand_matches (icode, bias_no, GEN_INT (-1)))
5269 : : return -1;
5270 : : }
5271 : :
5272 : : return VECT_PARTIAL_BIAS_UNSUPPORTED;
5273 : : }
5274 : :
5275 : : /* Expand STMT as though it were a call to internal function FN. */
5276 : :
5277 : : void
5278 : 224043 : expand_internal_call (internal_fn fn, gcall *stmt)
5279 : : {
5280 : 224043 : internal_fn_expanders[fn] (fn, stmt);
5281 : 224043 : }
5282 : :
5283 : : /* Expand STMT, which is a call to internal function FN. */
5284 : :
5285 : : void
5286 : 187081 : expand_internal_call (gcall *stmt)
5287 : : {
5288 : 187081 : expand_internal_call (gimple_call_internal_fn (stmt), stmt);
5289 : 187081 : }
5290 : :
5291 : : /* If TYPE is a vector type, return true if IFN is a direct internal
5292 : : function that is supported for that type. If TYPE is a scalar type,
5293 : : return true if IFN is a direct internal function that is supported for
5294 : : the target's preferred vector version of TYPE. */
5295 : :
5296 : : bool
5297 : 13818 : vectorized_internal_fn_supported_p (internal_fn ifn, tree type)
5298 : : {
5299 : 13818 : if (VECTOR_MODE_P (TYPE_MODE (type)))
5300 : 7539 : return direct_internal_fn_supported_p (ifn, type, OPTIMIZE_FOR_SPEED);
5301 : :
5302 : 6279 : scalar_mode smode;
5303 : 6279 : if (VECTOR_TYPE_P (type)
5304 : 6279 : || !is_a <scalar_mode> (TYPE_MODE (type), &smode))
5305 : 11 : return false;
5306 : :
5307 : 6268 : machine_mode vmode = targetm.vectorize.preferred_simd_mode (smode);
5308 : 6268 : if (VECTOR_MODE_P (vmode))
5309 : : {
5310 : 6231 : tree vectype = build_vector_type_for_mode (type, vmode);
5311 : 6231 : if (direct_internal_fn_supported_p (ifn, vectype, OPTIMIZE_FOR_SPEED))
5312 : : return true;
5313 : : }
5314 : :
5315 : 4584 : auto_vector_modes vector_modes;
5316 : 4584 : targetm.vectorize.autovectorize_vector_modes (&vector_modes, true);
5317 : 28509 : for (machine_mode base_mode : vector_modes)
5318 : 14757 : if (related_vector_mode (base_mode, smode).exists (&vmode))
5319 : : {
5320 : 12711 : tree vectype = build_vector_type_for_mode (type, vmode);
5321 : 12711 : if (direct_internal_fn_supported_p (ifn, vectype, OPTIMIZE_FOR_SPEED))
5322 : : return true;
5323 : : }
5324 : :
5325 : : return false;
5326 : 4584 : }
5327 : :
5328 : : void
5329 : 0 : expand_SHUFFLEVECTOR (internal_fn, gcall *)
5330 : : {
5331 : 0 : gcc_unreachable ();
5332 : : }
5333 : :
5334 : : void
5335 : 0 : expand_PHI (internal_fn, gcall *)
5336 : : {
5337 : 0 : gcc_unreachable ();
5338 : : }
5339 : :
5340 : : void
5341 : 413 : expand_SPACESHIP (internal_fn, gcall *stmt)
5342 : : {
5343 : 413 : tree lhs = gimple_call_lhs (stmt);
5344 : 413 : tree rhs1 = gimple_call_arg (stmt, 0);
5345 : 413 : tree rhs2 = gimple_call_arg (stmt, 1);
5346 : 413 : tree rhs3 = gimple_call_arg (stmt, 2);
5347 : 413 : tree type = TREE_TYPE (rhs1);
5348 : :
5349 : 413 : do_pending_stack_adjust ();
5350 : :
5351 : 413 : rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
5352 : 413 : rtx op1 = expand_normal (rhs1);
5353 : 413 : rtx op2 = expand_normal (rhs2);
5354 : 413 : rtx op3 = expand_normal (rhs3);
5355 : :
5356 : 413 : class expand_operand ops[4];
5357 : 413 : create_call_lhs_operand (&ops[0], target, TYPE_MODE (TREE_TYPE (lhs)));
5358 : 413 : create_input_operand (&ops[1], op1, TYPE_MODE (type));
5359 : 413 : create_input_operand (&ops[2], op2, TYPE_MODE (type));
5360 : 413 : create_input_operand (&ops[3], op3, TYPE_MODE (TREE_TYPE (rhs3)));
5361 : 413 : insn_code icode = optab_handler (spaceship_optab, TYPE_MODE (type));
5362 : 413 : expand_insn (icode, 4, ops);
5363 : 413 : assign_call_lhs (lhs, target, &ops[0]);
5364 : 413 : }
5365 : :
5366 : : void
5367 : 0 : expand_ASSUME (internal_fn, gcall *)
5368 : : {
5369 : 0 : }
5370 : :
5371 : : void
5372 : 0 : expand_MASK_CALL (internal_fn, gcall *)
5373 : : {
5374 : : /* This IFN should only exist between ifcvt and vect passes. */
5375 : 0 : gcc_unreachable ();
5376 : : }
5377 : :
5378 : : void
5379 : 1563 : expand_MULBITINT (internal_fn, gcall *stmt)
5380 : : {
5381 : 1563 : rtx_mode_t args[6];
5382 : 10941 : for (int i = 0; i < 6; i++)
5383 : 9378 : args[i] = rtx_mode_t (expand_normal (gimple_call_arg (stmt, i)),
5384 : 18756 : (i & 1) ? SImode : ptr_mode);
5385 : 1563 : rtx fun = init_one_libfunc ("__mulbitint3");
5386 : 1563 : emit_library_call_value_1 (0, fun, NULL_RTX, LCT_NORMAL, VOIDmode, 6, args);
5387 : 1563 : }
5388 : :
5389 : : void
5390 : 131 : expand_DIVMODBITINT (internal_fn, gcall *stmt)
5391 : : {
5392 : 131 : rtx_mode_t args[8];
5393 : 1179 : for (int i = 0; i < 8; i++)
5394 : 1048 : args[i] = rtx_mode_t (expand_normal (gimple_call_arg (stmt, i)),
5395 : 2096 : (i & 1) ? SImode : ptr_mode);
5396 : 131 : rtx fun = init_one_libfunc ("__divmodbitint4");
5397 : 131 : emit_library_call_value_1 (0, fun, NULL_RTX, LCT_NORMAL, VOIDmode, 8, args);
5398 : 131 : }
5399 : :
5400 : : void
5401 : 167 : expand_FLOATTOBITINT (internal_fn, gcall *stmt)
5402 : : {
5403 : 167 : machine_mode mode = TYPE_MODE (TREE_TYPE (gimple_call_arg (stmt, 2)));
5404 : 167 : rtx arg0 = expand_normal (gimple_call_arg (stmt, 0));
5405 : 167 : rtx arg1 = expand_normal (gimple_call_arg (stmt, 1));
5406 : 167 : rtx arg2 = expand_normal (gimple_call_arg (stmt, 2));
5407 : 167 : const char *mname = GET_MODE_NAME (mode);
5408 : 167 : unsigned mname_len = strlen (mname);
5409 : 167 : int len = 12 + mname_len;
5410 : 167 : if (DECIMAL_FLOAT_MODE_P (mode))
5411 : 57 : len += 4;
5412 : 167 : char *libfunc_name = XALLOCAVEC (char, len);
5413 : 167 : char *p = libfunc_name;
5414 : 167 : const char *q;
5415 : 167 : if (DECIMAL_FLOAT_MODE_P (mode))
5416 : : {
5417 : : #if ENABLE_DECIMAL_BID_FORMAT
5418 : 57 : memcpy (p, "__bid_fix", 9);
5419 : : #else
5420 : : memcpy (p, "__dpd_fix", 9);
5421 : : #endif
5422 : 57 : p += 9;
5423 : : }
5424 : : else
5425 : : {
5426 : 110 : memcpy (p, "__fix", 5);
5427 : 110 : p += 5;
5428 : : }
5429 : 501 : for (q = mname; *q; q++)
5430 : 334 : *p++ = TOLOWER (*q);
5431 : 167 : memcpy (p, "bitint", 7);
5432 : 167 : rtx fun = init_one_libfunc (libfunc_name);
5433 : 167 : emit_library_call (fun, LCT_NORMAL, VOIDmode, arg0, ptr_mode, arg1,
5434 : : SImode, arg2, mode);
5435 : 167 : }
5436 : :
5437 : : void
5438 : 138 : expand_BITINTTOFLOAT (internal_fn, gcall *stmt)
5439 : : {
5440 : 138 : tree lhs = gimple_call_lhs (stmt);
5441 : 138 : if (!lhs)
5442 : : return;
5443 : 138 : machine_mode mode = TYPE_MODE (TREE_TYPE (lhs));
5444 : 138 : rtx arg0 = expand_normal (gimple_call_arg (stmt, 0));
5445 : 138 : rtx arg1 = expand_normal (gimple_call_arg (stmt, 1));
5446 : 138 : const char *mname = GET_MODE_NAME (mode);
5447 : 138 : unsigned mname_len = strlen (mname);
5448 : 138 : int len = 14 + mname_len;
5449 : 138 : if (DECIMAL_FLOAT_MODE_P (mode))
5450 : 17 : len += 4;
5451 : 138 : char *libfunc_name = XALLOCAVEC (char, len);
5452 : 138 : char *p = libfunc_name;
5453 : 138 : const char *q;
5454 : 138 : if (DECIMAL_FLOAT_MODE_P (mode))
5455 : : {
5456 : : #if ENABLE_DECIMAL_BID_FORMAT
5457 : 17 : memcpy (p, "__bid_floatbitint", 17);
5458 : : #else
5459 : : memcpy (p, "__dpd_floatbitint", 17);
5460 : : #endif
5461 : 17 : p += 17;
5462 : : }
5463 : : else
5464 : : {
5465 : 121 : memcpy (p, "__floatbitint", 13);
5466 : 121 : p += 13;
5467 : : }
5468 : 414 : for (q = mname; *q; q++)
5469 : 276 : *p++ = TOLOWER (*q);
5470 : 138 : *p = '\0';
5471 : 138 : rtx fun = init_one_libfunc (libfunc_name);
5472 : 138 : rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
5473 : 138 : rtx val = emit_library_call_value (fun, target, LCT_PURE, mode,
5474 : : arg0, ptr_mode, arg1, SImode);
5475 : 138 : if (val != target)
5476 : 0 : emit_move_insn (target, val);
5477 : : }
5478 : :
5479 : : static bool
5480 : 9868 : expand_bitquery (internal_fn fn, gcall *stmt)
5481 : : {
5482 : 9868 : tree lhs = gimple_call_lhs (stmt);
5483 : 9868 : if (lhs == NULL_TREE)
5484 : : return false;
5485 : 9862 : tree arg = gimple_call_arg (stmt, 0);
5486 : 9862 : if (TREE_CODE (arg) == INTEGER_CST)
5487 : : {
5488 : 0 : tree ret = fold_const_call (as_combined_fn (fn), TREE_TYPE (arg), arg);
5489 : 0 : gcc_checking_assert (ret && TREE_CODE (ret) == INTEGER_CST);
5490 : 0 : expand_assignment (lhs, ret, false);
5491 : 0 : return false;
5492 : : }
5493 : : return true;
5494 : : }
5495 : :
5496 : : void
5497 : 1 : expand_CLRSB (internal_fn fn, gcall *stmt)
5498 : : {
5499 : 1 : if (expand_bitquery (fn, stmt))
5500 : 0 : expand_unary_optab_fn (fn, stmt, clrsb_optab);
5501 : 1 : }
5502 : :
5503 : : void
5504 : 8518 : expand_CLZ (internal_fn fn, gcall *stmt)
5505 : : {
5506 : 8518 : if (expand_bitquery (fn, stmt))
5507 : 8517 : expand_unary_optab_fn (fn, stmt, clz_optab);
5508 : 8518 : }
5509 : :
5510 : : void
5511 : 836 : expand_CTZ (internal_fn fn, gcall *stmt)
5512 : : {
5513 : 836 : if (expand_bitquery (fn, stmt))
5514 : 835 : expand_unary_optab_fn (fn, stmt, ctz_optab);
5515 : 836 : }
5516 : :
5517 : : void
5518 : 131 : expand_FFS (internal_fn fn, gcall *stmt)
5519 : : {
5520 : 131 : if (expand_bitquery (fn, stmt))
5521 : 130 : expand_unary_optab_fn (fn, stmt, ffs_optab);
5522 : 131 : }
5523 : :
5524 : : void
5525 : 165 : expand_PARITY (internal_fn fn, gcall *stmt)
5526 : : {
5527 : 165 : if (expand_bitquery (fn, stmt))
5528 : 164 : expand_unary_optab_fn (fn, stmt, parity_optab);
5529 : 165 : }
5530 : :
5531 : : void
5532 : 217 : expand_POPCOUNT (internal_fn fn, gcall *stmt)
5533 : : {
5534 : 217 : if (!expand_bitquery (fn, stmt))
5535 : : return;
5536 : 216 : if (gimple_call_num_args (stmt) == 1)
5537 : : {
5538 : 208 : expand_unary_optab_fn (fn, stmt, popcount_optab);
5539 : 208 : return;
5540 : : }
5541 : : /* If .POPCOUNT call has 2 arguments, match_single_bit_test marked it
5542 : : because the result is only used in an equality comparison against 1.
5543 : : Use rtx costs in that case to determine if .POPCOUNT (arg) == 1
5544 : : or (arg ^ (arg - 1)) > arg - 1 is cheaper.
5545 : : If .POPCOUNT second argument is 0, we additionally know that arg
5546 : : is non-zero, so use arg & (arg - 1) == 0 instead.
5547 : : If .POPCOUNT second argument is -1, the comparison was either `<= 1`
5548 : : or `> 1`. */
5549 : 8 : bool speed_p = optimize_insn_for_speed_p ();
5550 : 8 : tree lhs = gimple_call_lhs (stmt);
5551 : 8 : tree arg = gimple_call_arg (stmt, 0);
5552 : 8 : bool nonzero_arg = integer_zerop (gimple_call_arg (stmt, 1));
5553 : 8 : bool was_le = integer_minus_onep (gimple_call_arg (stmt, 1));
5554 : 8 : if (was_le)
5555 : 0 : nonzero_arg = true;
5556 : 8 : tree type = TREE_TYPE (arg);
5557 : 8 : machine_mode mode = TYPE_MODE (type);
5558 : 8 : machine_mode lhsmode = TYPE_MODE (TREE_TYPE (lhs));
5559 : 8 : do_pending_stack_adjust ();
5560 : 8 : start_sequence ();
5561 : 8 : expand_unary_optab_fn (fn, stmt, popcount_optab);
5562 : 8 : rtx_insn *popcount_insns = end_sequence ();
5563 : 8 : start_sequence ();
5564 : 8 : rtx plhs = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
5565 : 8 : rtx pcmp = emit_store_flag (NULL_RTX, EQ, plhs, const1_rtx, lhsmode, 0, 0);
5566 : 8 : if (pcmp == NULL_RTX)
5567 : : {
5568 : 0 : fail:
5569 : 0 : end_sequence ();
5570 : 0 : emit_insn (popcount_insns);
5571 : 0 : return;
5572 : : }
5573 : 8 : rtx_insn *popcount_cmp_insns = end_sequence ();
5574 : 8 : start_sequence ();
5575 : 8 : rtx op0 = expand_normal (arg);
5576 : 8 : rtx argm1 = expand_simple_binop (mode, PLUS, op0, constm1_rtx, NULL_RTX,
5577 : : 1, OPTAB_WIDEN);
5578 : 8 : if (argm1 == NULL_RTX)
5579 : 0 : goto fail;
5580 : 12 : rtx argxorargm1 = expand_simple_binop (mode, nonzero_arg ? AND : XOR, op0,
5581 : : argm1, NULL_RTX, 1, OPTAB_WIDEN);
5582 : 8 : if (argxorargm1 == NULL_RTX)
5583 : 0 : goto fail;
5584 : 8 : rtx cmp;
5585 : 8 : if (nonzero_arg)
5586 : 4 : cmp = emit_store_flag (NULL_RTX, EQ, argxorargm1, const0_rtx, mode, 1, 1);
5587 : : else
5588 : 4 : cmp = emit_store_flag (NULL_RTX, GTU, argxorargm1, argm1, mode, 1, 1);
5589 : 8 : if (cmp == NULL_RTX)
5590 : 0 : goto fail;
5591 : 8 : rtx_insn *cmp_insns = end_sequence ();
5592 : 8 : unsigned popcount_cost = (seq_cost (popcount_insns, speed_p)
5593 : 8 : + seq_cost (popcount_cmp_insns, speed_p));
5594 : 8 : unsigned cmp_cost = seq_cost (cmp_insns, speed_p);
5595 : :
5596 : 8 : if (dump_file && (dump_flags & TDF_DETAILS))
5597 : 0 : fprintf(dump_file, "popcount == 1: popcount cost: %u; cmp cost: %u\n",
5598 : : popcount_cost, cmp_cost);
5599 : :
5600 : 8 : if (popcount_cost <= cmp_cost)
5601 : 8 : emit_insn (popcount_insns);
5602 : : else
5603 : : {
5604 : 0 : start_sequence ();
5605 : 0 : emit_insn (cmp_insns);
5606 : 0 : plhs = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
5607 : 0 : if (GET_MODE (cmp) != GET_MODE (plhs))
5608 : 0 : cmp = convert_to_mode (GET_MODE (plhs), cmp, 1);
5609 : : /* For `<= 1`, we need to produce `2 - cmp` or `cmp ? 1 : 2` as that
5610 : : then gets compared against 1 and we need the false case to be 2. */
5611 : 0 : if (was_le)
5612 : : {
5613 : 0 : cmp = expand_simple_binop (GET_MODE (cmp), MINUS, const2_rtx,
5614 : : cmp, NULL_RTX, 1, OPTAB_WIDEN);
5615 : 0 : if (!cmp)
5616 : 0 : goto fail;
5617 : : }
5618 : 0 : emit_move_insn (plhs, cmp);
5619 : 0 : rtx_insn *all_insns = end_sequence ();
5620 : 0 : emit_insn (all_insns);
5621 : : }
5622 : : }
|