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