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