Debian Codegraph @

gcc/gimple-lower-bitint.cc

raw
1 /* Lower _BitInt(N) operations to scalar operations.
2 Copyright (C) 2023-2025 Free Software Foundation, Inc.
3 Contributed by Jakub Jelinek <[email protected]>.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3, or (at your option) any
10 later version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "backend.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "gimple.h"
28 #include "cfghooks.h"
29 #include "tree-pass.h"
30 #include "ssa.h"
31 #include "fold-const.h"
32 #include "gimplify.h"
33 #include "gimple-iterator.h"
34 #include "tree-cfg.h"
35 #include "tree-dfa.h"
36 #include "cfgloop.h"
37 #include "cfganal.h"
38 #include "target.h"
39 #include "tree-ssa-live.h"
40 #include "tree-ssa-coalesce.h"
41 #include "domwalk.h"
42 #include "memmodel.h"
43 #include "optabs.h"
44 #include "varasm.h"
45 #include "gimple-range.h"
46 #include "value-range.h"
47 #include "langhooks.h"
48 #include "gimplify-me.h"
49 #include "diagnostic-core.h"
50 #include "tree-eh.h"
51 #include "tree-pretty-print.h"
52 #include "alloc-pool.h"
53 #include "tree-into-ssa.h"
54 #include "tree-cfgcleanup.h"
55 #include "tree-switch-conversion.h"
56 #include "ubsan.h"
57 #include "stor-layout.h"
58 #include "gimple-lower-bitint.h"
59
60 /* Split BITINT_TYPE precisions in 4 categories. Small _BitInt, where
61 target hook says it is a single limb, middle _BitInt which per ABI
62 does not, but there is some INTEGER_TYPE in which arithmetics can be
63 performed (operations on such _BitInt are lowered to casts to that
64 arithmetic type and cast back; e.g. on x86_64 limb is DImode, but
65 target supports TImode, so _BitInt(65) to _BitInt(128) are middle
66 ones), large _BitInt which should by straight line code and
67 finally huge _BitInt which should be handled by loops over the limbs. */
68
69 enum bitint_prec_kind {
70 bitint_prec_small,
71 bitint_prec_middle,
72 bitint_prec_large,
73 bitint_prec_huge
74 };
75
76 /* Caches to speed up bitint_precision_kind. */
77
78 static int small_max_prec, mid_min_prec, large_min_prec, huge_min_prec;
79 static int limb_prec, abi_limb_prec;
80
81 /* Categorize _BitInt(PREC) as small, middle, large or huge. */
82
83 static bitint_prec_kind
84 bitint_precision_kind (int prec)
85 {
86 if (prec <= small_max_prec)
87 return bitint_prec_small;
88 if (huge_min_prec && prec >= huge_min_prec)
89 return bitint_prec_huge;
90 if (large_min_prec && prec >= large_min_prec)
91 return bitint_prec_large;
92 if (mid_min_prec && prec >= mid_min_prec)
93 return bitint_prec_middle;
94
95 struct bitint_info info;
96 bool ok = targetm.c.bitint_type_info (prec, &info);
97 gcc_assert (ok);
98 scalar_int_mode limb_mode = as_a <scalar_int_mode> (info.limb_mode);
99 if (prec <= GET_MODE_PRECISION (limb_mode))
100 {
101 small_max_prec = prec;
102 return bitint_prec_small;
103 }
104 if (!large_min_prec
105 && GET_MODE_PRECISION (limb_mode) < MAX_FIXED_MODE_SIZE)
106 large_min_prec = MAX_FIXED_MODE_SIZE + 1;
107 if (!limb_prec)
108 limb_prec = GET_MODE_PRECISION (limb_mode);
109 if (!abi_limb_prec)
110 abi_limb_prec
111 = GET_MODE_PRECISION (as_a <scalar_int_mode> (info.abi_limb_mode));
112 if (!huge_min_prec)
113 {
114 if (4 * limb_prec >= MAX_FIXED_MODE_SIZE)
115 huge_min_prec = 4 * limb_prec;
116 else
117 huge_min_prec = MAX_FIXED_MODE_SIZE + 1;
118 }
119 if (prec <= MAX_FIXED_MODE_SIZE)
120 {
121 if (!mid_min_prec || prec < mid_min_prec)
122 mid_min_prec = prec;
123 return bitint_prec_middle;
124 }
125 if (large_min_prec && prec <= large_min_prec)
126 return bitint_prec_large;
127 return bitint_prec_huge;
128 }
129
130 /* Same for a TYPE. */
131
132 static bitint_prec_kind
133 bitint_precision_kind (tree type)
134 {
135 return bitint_precision_kind (TYPE_PRECISION (type));
136 }
137
138 /* Return minimum precision needed to describe INTEGER_CST
139 CST. All bits above that precision up to precision of
140 TREE_TYPE (CST) are cleared if EXT is set to 0, or set
141 if EXT is set to -1. */
142
143 static unsigned
144 bitint_min_cst_precision (tree cst, int &ext)
145 {
146 ext = tree_int_cst_sgn (cst) < 0 ? -1 : 0;
147 wide_int w = wi::to_wide (cst);
148 unsigned min_prec = wi::min_precision (w, TYPE_SIGN (TREE_TYPE (cst)));
149 /* For signed values, we don't need to count the sign bit,
150 we'll use constant 0 or -1 for the upper bits. */
151 if (!TYPE_UNSIGNED (TREE_TYPE (cst)))
152 --min_prec;
153 else
154 {
155 /* For unsigned values, also try signed min_precision
156 in case the constant has lots of most significant bits set. */
157 unsigned min_prec2 = wi::min_precision (w, SIGNED) - 1;
158 if (min_prec2 < min_prec)
159 {
160 ext = -1;
161 return min_prec2;
162 }
163 }
164 return min_prec;
165 }
166
167 namespace {
168
169 /* If OP is middle _BitInt, cast it to corresponding INTEGER_TYPE
170 cached in TYPE and return it. */
171
172 tree
173 maybe_cast_middle_bitint (gimple_stmt_iterator *gsi, tree op, tree &type)
174 {
175 if (op == NULL_TREE
176 || TREE_CODE (TREE_TYPE (op)) != BITINT_TYPE
177 || bitint_precision_kind (TREE_TYPE (op)) != bitint_prec_middle)
178 return op;
179
180 int prec = TYPE_PRECISION (TREE_TYPE (op));
181 int uns = TYPE_UNSIGNED (TREE_TYPE (op));
182 if (type == NULL_TREE
183 || TYPE_PRECISION (type) != prec
184 || TYPE_UNSIGNED (type) != uns)
185 type = build_nonstandard_integer_type (prec, uns);
186
187 if (TREE_CODE (op) != SSA_NAME)
188 {
189 tree nop = fold_convert (type, op);
190 if (is_gimple_val (nop))
191 return nop;
192 }
193
194 tree nop = make_ssa_name (type);
195 gimple *g = gimple_build_assign (nop, NOP_EXPR, op);
196 gsi_insert_before (gsi, g, GSI_SAME_STMT);
197 return nop;
198 }
199
200 /* Return true if STMT can be handled in a loop from least to most
201 significant limb together with its dependencies. */
202
203 bool
204 mergeable_op (gimple *stmt)
205 {
206 if (!is_gimple_assign (stmt))
207 return false;
208 switch (gimple_assign_rhs_code (stmt))
209 {
210 case PLUS_EXPR:
211 case MINUS_EXPR:
212 case NEGATE_EXPR:
213 case BIT_AND_EXPR:
214 case BIT_IOR_EXPR:
215 case BIT_XOR_EXPR:
216 case BIT_NOT_EXPR:
217 case SSA_NAME:
218 case INTEGER_CST:
219 case BIT_FIELD_REF:
220 return true;
221 case LSHIFT_EXPR:
222 {
223 tree cnt = gimple_assign_rhs2 (stmt);
224 if (tree_fits_uhwi_p (cnt)
225 && tree_to_uhwi (cnt) < (unsigned HOST_WIDE_INT) limb_prec)
226 return true;
227 }
228 break;
229 CASE_CONVERT:
230 case VIEW_CONVERT_EXPR:
231 {
232 tree lhs_type = TREE_TYPE (gimple_assign_lhs (stmt));
233 tree rhs_type = TREE_TYPE (gimple_assign_rhs1 (stmt));
234 if (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
235 && TREE_CODE (lhs_type) == BITINT_TYPE
236 && TREE_CODE (rhs_type) == BITINT_TYPE
237 && bitint_precision_kind (lhs_type) >= bitint_prec_large
238 && bitint_precision_kind (rhs_type) >= bitint_prec_large
239 && (CEIL (TYPE_PRECISION (lhs_type), limb_prec)
240 == CEIL (TYPE_PRECISION (rhs_type), limb_prec)))
241 {
242 if (TYPE_PRECISION (rhs_type) >= TYPE_PRECISION (lhs_type))
243 return true;
244 if ((unsigned) TYPE_PRECISION (lhs_type) % (2 * limb_prec) != 0)
245 return true;
246 if (bitint_precision_kind (lhs_type) == bitint_prec_large)
247 return true;
248 }
249 break;
250 }
251 default:
252 break;
253 }
254 return false;
255 }
256
257 /* Return non-zero if stmt is .{ADD,SUB,MUL}_OVERFLOW call with
258 _Complex large/huge _BitInt lhs which has at most two immediate uses,
259 at most one use in REALPART_EXPR stmt in the same bb and exactly one
260 IMAGPART_EXPR use in the same bb with a single use which casts it to
261 non-BITINT_TYPE integral type. If there is a REALPART_EXPR use,
262 return 2. Such cases (most common uses of those builtins) can be
263 optimized by marking their lhs and lhs of IMAGPART_EXPR and maybe lhs
264 of REALPART_EXPR as not needed to be backed up by a stack variable.
265 For .UBSAN_CHECK_{ADD,SUB,MUL} return 3. */
266
267 int
268 optimizable_arith_overflow (gimple *stmt)
269 {
270 bool is_ubsan = false;
271 if (!is_gimple_call (stmt) || !gimple_call_internal_p (stmt))
272 return false;
273 switch (gimple_call_internal_fn (stmt))
274 {
275 case IFN_ADD_OVERFLOW:
276 case IFN_SUB_OVERFLOW:
277 case IFN_MUL_OVERFLOW:
278 break;
279 case IFN_UBSAN_CHECK_ADD:
280 case IFN_UBSAN_CHECK_SUB:
281 case IFN_UBSAN_CHECK_MUL:
282 is_ubsan = true;
283 break;
284 default:
285 return 0;
286 }
287 tree lhs = gimple_call_lhs (stmt);
288 if (!lhs)
289 return 0;
290 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
291 return 0;
292 tree type = is_ubsan ? TREE_TYPE (lhs) : TREE_TYPE (TREE_TYPE (lhs));
293 if (TREE_CODE (type) != BITINT_TYPE
294 || bitint_precision_kind (type) < bitint_prec_large)
295 return 0;
296
297 if (is_ubsan)
298 {
299 use_operand_p use_p;
300 gimple *use_stmt;
301 if (!single_imm_use (lhs, &use_p, &use_stmt)
302 || gimple_bb (use_stmt) != gimple_bb (stmt)
303 || !gimple_store_p (use_stmt)
304 || !is_gimple_assign (use_stmt)
305 || gimple_has_volatile_ops (use_stmt)
306 || stmt_ends_bb_p (use_stmt))
307 return 0;
308 return 3;
309 }
310
311 imm_use_iterator ui;
312 use_operand_p use_p;
313 int seen = 0;
314 gimple *realpart = NULL, *cast = NULL;
315 FOR_EACH_IMM_USE_FAST (use_p, ui, lhs)
316 {
317 gimple *g = USE_STMT (use_p);
318 if (is_gimple_debug (g))
319 continue;
320 if (!is_gimple_assign (g) || gimple_bb (g) != gimple_bb (stmt))
321 return 0;
322 if (gimple_assign_rhs_code (g) == REALPART_EXPR)
323 {
324 if ((seen & 1) != 0)
325 return 0;
326 seen |= 1;
327 realpart = g;
328 }
329 else if (gimple_assign_rhs_code (g) == IMAGPART_EXPR)
330 {
331 if ((seen & 2) != 0)
332 return 0;
333 seen |= 2;
334
335 use_operand_p use2_p;
336 gimple *use_stmt;
337 tree lhs2 = gimple_assign_lhs (g);
338 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs2))
339 return 0;
340 if (!single_imm_use (lhs2, &use2_p, &use_stmt)
341 || gimple_bb (use_stmt) != gimple_bb (stmt)
342 || !gimple_assign_cast_p (use_stmt))
343 return 0;
344
345 lhs2 = gimple_assign_lhs (use_stmt);
346 if (!INTEGRAL_TYPE_P (TREE_TYPE (lhs2))
347 || TREE_CODE (TREE_TYPE (lhs2)) == BITINT_TYPE)
348 return 0;
349 cast = use_stmt;
350 }
351 else
352 return 0;
353 }
354 if ((seen & 2) == 0)
355 return 0;
356 if (seen == 3)
357 {
358 /* Punt if the cast stmt appears before realpart stmt, because
359 if both appear, the lowering wants to emit all the code
360 at the location of realpart stmt. */
361 gimple_stmt_iterator gsi = gsi_for_stmt (realpart);
362 unsigned int cnt = 0;
363 do
364 {
365 gsi_prev_nondebug (&gsi);
366 if (gsi_end_p (gsi) || gsi_stmt (gsi) == cast)
367 return 0;
368 if (gsi_stmt (gsi) == stmt)
369 return 2;
370 /* If realpart is too far from stmt, punt as well.
371 Usually it will appear right after it. */
372 if (++cnt == 32)
373 return 0;
374 }
375 while (1);
376 }
377 return 1;
378 }
379
380 /* If STMT is some kind of comparison (GIMPLE_COND, comparison assignment)
381 comparing large/huge _BitInt types, return the comparison code and if
382 non-NULL fill in the comparison operands to *POP1 and *POP2. */
383
384 tree_code
385 comparison_op (gimple *stmt, tree *pop1, tree *pop2)
386 {
387 tree op1 = NULL_TREE, op2 = NULL_TREE;
388 tree_code code = ERROR_MARK;
389 if (gimple_code (stmt) == GIMPLE_COND)
390 {
391 code = gimple_cond_code (stmt);
392 op1 = gimple_cond_lhs (stmt);
393 op2 = gimple_cond_rhs (stmt);
394 }
395 else if (is_gimple_assign (stmt))
396 {
397 code = gimple_assign_rhs_code (stmt);
398 op1 = gimple_assign_rhs1 (stmt);
399 if (TREE_CODE_CLASS (code) == tcc_comparison
400 || TREE_CODE_CLASS (code) == tcc_binary)
401 op2 = gimple_assign_rhs2 (stmt);
402 }
403 if (TREE_CODE_CLASS (code) != tcc_comparison)
404 return ERROR_MARK;
405 tree type = TREE_TYPE (op1);
406 if (TREE_CODE (type) != BITINT_TYPE
407 || bitint_precision_kind (type) < bitint_prec_large)
408 return ERROR_MARK;
409 if (pop1)
410 {
411 *pop1 = op1;
412 *pop2 = op2;
413 }
414 return code;
415 }
416
417 /* Class used during large/huge _BitInt lowering containing all the
418 state for the methods. */
419
420 struct bitint_large_huge
421 {
422 bitint_large_huge ()
423 : m_names (NULL), m_loads (NULL), m_preserved (NULL),
424 m_single_use_names (NULL), m_map (NULL), m_vars (NULL),
425 m_limb_type (NULL_TREE), m_data (vNULL),
426 m_returns_twice_calls (vNULL) {}
427
428 ~bitint_large_huge ();
429
430 void insert_before (gimple *);
431 tree limb_access_type (tree, tree);
432 tree limb_access (tree, tree, tree, bool);
433 tree build_bit_field_ref (tree, tree, unsigned HOST_WIDE_INT,
434 unsigned HOST_WIDE_INT);
435 void if_then (gimple *, profile_probability, edge &, edge &);
436 void if_then_else (gimple *, profile_probability, edge &, edge &);
437 void if_then_if_then_else (gimple *g, gimple *,
438 profile_probability, profile_probability,
439 edge &, edge &, edge &);
440 tree handle_operand (tree, tree);
441 tree prepare_data_in_out (tree, tree, tree *, tree = NULL_TREE);
442 tree add_cast (tree, tree);
443 tree handle_plus_minus (tree_code, tree, tree, tree);
444 tree handle_lshift (tree, tree, tree);
445 tree handle_cast (tree, tree, tree);
446 tree handle_bit_field_ref (tree, tree);
447 tree handle_load (gimple *, tree);
448 tree handle_stmt (gimple *, tree);
449 tree handle_operand_addr (tree, gimple *, int *, int *);
450 tree create_loop (tree, tree *);
451 tree lower_mergeable_stmt (gimple *, tree_code &, tree, tree);
452 tree lower_comparison_stmt (gimple *, tree_code &, tree, tree);
453 void lower_shift_stmt (tree, gimple *);
454 void lower_muldiv_stmt (tree, gimple *);
455 void lower_float_conv_stmt (tree, gimple *);
456 tree arith_overflow_extract_bits (unsigned int, unsigned int, tree,
457 unsigned int, bool);
458 void finish_arith_overflow (tree, tree, tree, tree, tree, tree, gimple *,
459 tree_code);
460 void lower_addsub_overflow (tree, gimple *);
461 void lower_mul_overflow (tree, gimple *);
462 void lower_cplxpart_stmt (tree, gimple *);
463 void lower_complexexpr_stmt (gimple *);
464 void lower_bit_query (gimple *);
465 void lower_call (tree, gimple *);
466 void lower_asm (gimple *);
467 void lower_stmt (gimple *);
468
469 /* Bitmap of large/huge _BitInt SSA_NAMEs except those can be
470 merged with their uses. */
471 bitmap m_names;
472 /* Subset of those for lhs of load statements. These will be
473 cleared in m_names if the loads will be mergeable with all
474 their uses. */
475 bitmap m_loads;
476 /* Bitmap of large/huge _BitInt SSA_NAMEs that should survive
477 to later passes (arguments or return values of calls). */
478 bitmap m_preserved;
479 /* Subset of m_names which have a single use. As the lowering
480 can replace various original statements with their lowered
481 form even before it is done iterating over all basic blocks,
482 testing has_single_use for the purpose of emitting clobbers
483 doesn't work properly. */
484 bitmap m_single_use_names;
485 /* Used for coalescing/partitioning of large/huge _BitInt SSA_NAMEs
486 set in m_names. */
487 var_map m_map;
488 /* Mapping of the partitions to corresponding decls. */
489 tree *m_vars;
490 /* Unsigned integer type with limb precision. */
491 tree m_limb_type;
492 /* Its TYPE_SIZE_UNIT. */
493 unsigned HOST_WIDE_INT m_limb_size;
494 /* Location of a gimple stmt which is being currently lowered. */
495 location_t m_loc;
496 /* Current stmt iterator where code is being lowered currently. */
497 gimple_stmt_iterator m_gsi;
498 /* Statement after which any clobbers should be added if non-NULL. */
499 gimple *m_after_stmt;
500 /* Set when creating loops to the loop header bb and its preheader. */
501 basic_block m_bb, m_preheader_bb;
502 /* Stmt iterator after which initialization statements should be emitted. */
503 gimple_stmt_iterator m_init_gsi;
504 /* Decl into which a mergeable statement stores result. */
505 tree m_lhs;
506 /* handle_operand/handle_stmt can be invoked in various ways.
507
508 lower_mergeable_stmt for large _BitInt calls those with constant
509 idx only, expanding to straight line code, for huge _BitInt
510 emits a loop from least significant limb upwards, where each loop
511 iteration handles 2 limbs, plus there can be up to one full limb
512 and one partial limb processed after the loop, where handle_operand
513 and/or handle_stmt are called with constant idx. m_upwards_2limb
514 is set for this case, false otherwise. m_upwards is true if it
515 is either large or huge _BitInt handled by lower_mergeable_stmt,
516 i.e. indexes always increase.
517
518 Another way is used by lower_comparison_stmt, which walks limbs
519 from most significant to least significant, partial limb if any
520 processed first with constant idx and then loop processing a single
521 limb per iteration with non-constant idx.
522
523 Another way is used in lower_shift_stmt, where for LSHIFT_EXPR
524 destination limbs are processed from most significant to least
525 significant or for RSHIFT_EXPR the other way around, in loops or
526 straight line code, but idx usually is non-constant (so from
527 handle_operand/handle_stmt POV random access). The LSHIFT_EXPR
528 handling there can access even partial limbs using non-constant
529 idx (then m_var_msb should be true, for all the other cases
530 including lower_mergeable_stmt/lower_comparison_stmt that is
531 not the case and so m_var_msb should be false.
532
533 m_first should be set the first time handle_operand/handle_stmt
534 is called and clear when it is called for some other limb with
535 the same argument. If the lowering of an operand (e.g. INTEGER_CST)
536 or statement (e.g. +/-/<< with < limb_prec constant) needs some
537 state between the different calls, when m_first is true it should
538 push some trees to m_data vector and also make sure m_data_cnt is
539 incremented by how many trees were pushed, and when m_first is
540 false, it can use the m_data[m_data_cnt] etc. data or update them,
541 just needs to bump m_data_cnt by the same amount as when it was
542 called with m_first set. The toplevel calls to
543 handle_operand/handle_stmt should set m_data_cnt to 0 and truncate
544 m_data vector when setting m_first to true.
545
546 m_cast_conditional and m_bitfld_load are used when handling a
547 bit-field load inside of a widening cast. handle_cast sometimes
548 needs to do runtime comparisons and handle_operand only conditionally
549 or even in two separate conditional blocks for one idx (once with
550 constant index after comparing the runtime one for equality with the
551 constant). In these cases, m_cast_conditional is set to true and
552 the bit-field load then communicates its m_data_cnt to handle_cast
553 using m_bitfld_load. */
554 bool m_first;
555 bool m_var_msb;
556 unsigned m_upwards_2limb;
557 bool m_upwards;
558 bool m_cast_conditional;
559 unsigned m_bitfld_load;
560 vec<tree> m_data;
561 unsigned int m_data_cnt;
562 vec<gimple *> m_returns_twice_calls;
563 };
564
565 bitint_large_huge::~bitint_large_huge ()
566 {
567 BITMAP_FREE (m_names);
568 BITMAP_FREE (m_loads);
569 BITMAP_FREE (m_preserved);
570 BITMAP_FREE (m_single_use_names);
571 if (m_map)
572 delete_var_map (m_map);
573 XDELETEVEC (m_vars);
574 m_data.release ();
575 m_returns_twice_calls.release ();
576 }
577
578 /* Insert gimple statement G before current location
579 and set its gimple_location. */
580
581 void
582 bitint_large_huge::insert_before (gimple *g)
583 {
584 gimple_set_location (g, m_loc);
585 gsi_insert_before (&m_gsi, g, GSI_SAME_STMT);
586 }
587
588 /* Return type for accessing limb IDX of BITINT_TYPE TYPE.
589 This is normally m_limb_type, except for a partial most
590 significant limb if any. */
591
592 tree
593 bitint_large_huge::limb_access_type (tree type, tree idx)
594 {
595 if (type == NULL_TREE)
596 return m_limb_type;
597 unsigned HOST_WIDE_INT i = tree_to_uhwi (idx);
598 unsigned int prec = TYPE_PRECISION (type);
599 gcc_assert (i * limb_prec < prec);
600 if ((i + 1) * limb_prec <= prec)
601 return m_limb_type;
602 else
603 return build_nonstandard_integer_type (prec % limb_prec,
604 TYPE_UNSIGNED (type));
605 }
606
607 /* Return a tree how to access limb IDX of VAR corresponding to BITINT_TYPE
608 TYPE. If WRITE_P is true, it will be a store, otherwise a read. */
609
610 tree
611 bitint_large_huge::limb_access (tree type, tree var, tree idx, bool write_p)
612 {
613 tree atype = (tree_fits_uhwi_p (idx)
614 ? limb_access_type (type, idx) : m_limb_type);
615 tree ltype = m_limb_type;
616 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (var));
617 if (as != TYPE_ADDR_SPACE (ltype))
618 ltype = build_qualified_type (ltype, TYPE_QUALS (ltype)
619 | ENCODE_QUAL_ADDR_SPACE (as));
620 tree ret;
621 if (DECL_P (var) && tree_fits_uhwi_p (idx))
622 {
623 tree ptype = build_pointer_type (strip_array_types (TREE_TYPE (var)));
624 unsigned HOST_WIDE_INT off = tree_to_uhwi (idx) * m_limb_size;
625 ret = build2 (MEM_REF, ltype,
626 build_fold_addr_expr (var),
627 build_int_cst (ptype, off));
628 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (var);
629 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (var);
630 }
631 else if (TREE_CODE (var) == MEM_REF && tree_fits_uhwi_p (idx))
632 {
633 ret
634 = build2 (MEM_REF, ltype, unshare_expr (TREE_OPERAND (var, 0)),
635 size_binop (PLUS_EXPR, TREE_OPERAND (var, 1),
636 build_int_cst (TREE_TYPE (TREE_OPERAND (var, 1)),
637 tree_to_uhwi (idx)
638 * m_limb_size)));
639 TREE_THIS_VOLATILE (ret) = TREE_THIS_VOLATILE (var);
640 TREE_SIDE_EFFECTS (ret) = TREE_SIDE_EFFECTS (var);
641 TREE_THIS_NOTRAP (ret) = TREE_THIS_NOTRAP (var);
642 }
643 else
644 {
645 var = unshare_expr (var);
646 if (TREE_CODE (TREE_TYPE (var)) != ARRAY_TYPE
647 || !useless_type_conversion_p (m_limb_type,
648 TREE_TYPE (TREE_TYPE (var))))
649 {
650 unsigned HOST_WIDE_INT nelts
651 = CEIL (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (var))), limb_prec);
652 tree atype = build_array_type_nelts (ltype, nelts);
653 var = build1 (VIEW_CONVERT_EXPR, atype, var);
654 }
655 ret = build4 (ARRAY_REF, ltype, var, idx, NULL_TREE, NULL_TREE);
656 }
657 if (!write_p && !useless_type_conversion_p (atype, m_limb_type))
658 {
659 gimple *g = gimple_build_assign (make_ssa_name (m_limb_type), ret);
660 insert_before (g);
661 ret = gimple_assign_lhs (g);
662 ret = build1 (NOP_EXPR, atype, ret);
663 }
664 return ret;
665 }
666
667 /* Build a BIT_FIELD_REF to access BITSIZE bits with FTYPE type at
668 offset BITPOS inside of OBJ. */
669
670 tree
671 bitint_large_huge::build_bit_field_ref (tree ftype, tree obj,
672 unsigned HOST_WIDE_INT bitsize,
673 unsigned HOST_WIDE_INT bitpos)
674 {
675 if (INTEGRAL_TYPE_P (TREE_TYPE (obj))
676 && !type_has_mode_precision_p (TREE_TYPE (obj)))
677 {
678 unsigned HOST_WIDE_INT nelts
679 = CEIL (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (obj))), limb_prec);
680 tree ltype = m_limb_type;
681 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (obj));
682 if (as != TYPE_ADDR_SPACE (ltype))
683 ltype = build_qualified_type (ltype, TYPE_QUALS (ltype)
684 | ENCODE_QUAL_ADDR_SPACE (as));
685 tree atype = build_array_type_nelts (ltype, nelts);
686 obj = build1 (VIEW_CONVERT_EXPR, atype, obj);
687 }
688 return build3 (BIT_FIELD_REF, ftype, obj, bitsize_int (bitsize),
689 bitsize_int (bitpos));
690 }
691
692 /* Emit a half diamond,
693 if (COND)
694 |\
695 | \
696 | \
697 | new_bb1
698 | /
699 | /
700 |/
701 or if (COND) new_bb1;
702 PROB is the probability that the condition is true.
703 Updates m_gsi to start of new_bb1.
704 Sets EDGE_TRUE to edge from new_bb1 to successor and
705 EDGE_FALSE to the EDGE_FALSE_VALUE edge from if (COND) bb. */
706
707 void
708 bitint_large_huge::if_then (gimple *cond, profile_probability prob,
709 edge &edge_true, edge &edge_false)
710 {
711 insert_before (cond);
712 edge e1 = split_block (gsi_bb (m_gsi), cond);
713 edge e2 = split_block (e1->dest, (gimple *) NULL);
714 edge e3 = make_edge (e1->src, e2->dest, EDGE_FALSE_VALUE);
715 e1->flags = EDGE_TRUE_VALUE;
716 e1->probability = prob;
717 e3->probability = prob.invert ();
718 set_immediate_dominator (CDI_DOMINATORS, e2->dest, e1->src);
719 edge_true = e2;
720 edge_false = e3;
721 m_gsi = gsi_after_labels (e1->dest);
722 }
723
724 /* Emit a full diamond,
725 if (COND)
726 /\
727 / \
728 / \
729 new_bb1 new_bb2
730 \ /
731 \ /
732 \/
733 or if (COND) new_bb2; else new_bb1;
734 PROB is the probability that the condition is true.
735 Updates m_gsi to start of new_bb2.
736 Sets EDGE_TRUE to edge from new_bb1 to successor and
737 EDGE_FALSE to the EDGE_FALSE_VALUE edge from if (COND) bb. */
738
739 void
740 bitint_large_huge::if_then_else (gimple *cond, profile_probability prob,
741 edge &edge_true, edge &edge_false)
742 {
743 insert_before (cond);
744 edge e1 = split_block (gsi_bb (m_gsi), cond);
745 edge e2 = split_block (e1->dest, (gimple *) NULL);
746 basic_block bb = create_empty_bb (e1->dest);
747 add_bb_to_loop (bb, e1->dest->loop_father);
748 edge e3 = make_edge (e1->src, bb, EDGE_TRUE_VALUE);
749 e1->flags = EDGE_FALSE_VALUE;
750 e3->probability = prob;
751 e1->probability = prob.invert ();
752 bb->count = e1->src->count.apply_probability (prob);
753 set_immediate_dominator (CDI_DOMINATORS, bb, e1->src);
754 set_immediate_dominator (CDI_DOMINATORS, e2->dest, e1->src);
755 edge_true = make_single_succ_edge (bb, e2->dest, EDGE_FALLTHRU);
756 edge_false = e2;
757 m_gsi = gsi_after_labels (bb);
758 }
759
760 /* Emit a half diamond with full diamond in it
761 if (COND1)
762 |\
763 | \
764 | \
765 | if (COND2)
766 | / \
767 | / \
768 |new_bb1 new_bb2
769 | | /
770 \ | /
771 \ | /
772 \ | /
773 \|/
774 or if (COND1) { if (COND2) new_bb2; else new_bb1; }
775 PROB1 is the probability that the condition 1 is true.
776 PROB2 is the probability that the condition 2 is true.
777 Updates m_gsi to start of new_bb1.
778 Sets EDGE_TRUE_TRUE to edge from new_bb2 to successor,
779 EDGE_TRUE_FALSE to edge from new_bb1 to successor and
780 EDGE_FALSE to the EDGE_FALSE_VALUE edge from if (COND1) bb.
781 If COND2 is NULL, this is equivalent to
782 if_then (COND1, PROB1, EDGE_TRUE_FALSE, EDGE_FALSE);
783 EDGE_TRUE_TRUE = NULL; */
784
785 void
786 bitint_large_huge::if_then_if_then_else (gimple *cond1, gimple *cond2,
787 profile_probability prob1,
788 profile_probability prob2,
789 edge &edge_true_true,
790 edge &edge_true_false,
791 edge &edge_false)
792 {
793 edge e2, e3, e4 = NULL;
794 if_then (cond1, prob1, e2, e3);
795 if (cond2 == NULL)
796 {
797 edge_true_true = NULL;
798 edge_true_false = e2;
799 edge_false = e3;
800 return;
801 }
802 insert_before (cond2);
803 e2 = split_block (gsi_bb (m_gsi), cond2);
804 basic_block bb = create_empty_bb (e2->dest);
805 add_bb_to_loop (bb, e2->dest->loop_father);
806 e4 = make_edge (e2->src, bb, EDGE_TRUE_VALUE);
807 set_immediate_dominator (CDI_DOMINATORS, bb, e2->src);
808 e4->probability = prob2;
809 e2->flags = EDGE_FALSE_VALUE;
810 e2->probability = prob2.invert ();
811 bb->count = e2->src->count.apply_probability (prob2);
812 e4 = make_single_succ_edge (bb, e3->dest, EDGE_FALLTHRU);
813 e2 = find_edge (e2->dest, e3->dest);
814 edge_true_true = e4;
815 edge_true_false = e2;
816 edge_false = e3;
817 m_gsi = gsi_after_labels (e2->src);
818 }
819
820 /* Emit code to access limb IDX from OP. */
821
822 tree
823 bitint_large_huge::handle_operand (tree op, tree idx)
824 {
825 switch (TREE_CODE (op))
826 {
827 case SSA_NAME:
828 if (m_names == NULL
829 || !bitmap_bit_p (m_names, SSA_NAME_VERSION (op)))
830 {
831 if (SSA_NAME_IS_DEFAULT_DEF (op))
832 {
833 if (m_first)
834 {
835 tree v = create_tmp_reg (m_limb_type);
836 if (SSA_NAME_VAR (op) && VAR_P (SSA_NAME_VAR (op)))
837 {
838 DECL_NAME (v) = DECL_NAME (SSA_NAME_VAR (op));
839 DECL_SOURCE_LOCATION (v)
840 = DECL_SOURCE_LOCATION (SSA_NAME_VAR (op));
841 }
842 v = get_or_create_ssa_default_def (cfun, v);
843 m_data.safe_push (v);
844 }
845 tree ret = m_data[m_data_cnt];
846 m_data_cnt++;
847 if (tree_fits_uhwi_p (idx))
848 {
849 tree type = limb_access_type (TREE_TYPE (op), idx);
850 ret = add_cast (type, ret);
851 }
852 return ret;
853 }
854 location_t loc_save = m_loc;
855 m_loc = gimple_location (SSA_NAME_DEF_STMT (op));
856 tree ret = handle_stmt (SSA_NAME_DEF_STMT (op), idx);
857 m_loc = loc_save;
858 return ret;
859 }
860 int p;
861 gimple *g;
862 tree t;
863 p = var_to_partition (m_map, op);
864 gcc_assert (m_vars[p] != NULL_TREE);
865 t = limb_access (TREE_TYPE (op), m_vars[p], idx, false);
866 g = gimple_build_assign (make_ssa_name (TREE_TYPE (t)), t);
867 insert_before (g);
868 t = gimple_assign_lhs (g);
869 if (m_first
870 && m_single_use_names
871 && m_vars[p] != m_lhs
872 && m_after_stmt
873 && bitmap_bit_p (m_single_use_names, SSA_NAME_VERSION (op)))
874 {
875 tree clobber = build_clobber (TREE_TYPE (m_vars[p]),
876 CLOBBER_STORAGE_END);
877 g = gimple_build_assign (m_vars[p], clobber);
878 gimple_stmt_iterator gsi = gsi_for_stmt (m_after_stmt);
879 gsi_insert_after (&gsi, g, GSI_SAME_STMT);
880 }
881 return t;
882 case INTEGER_CST:
883 if (tree_fits_uhwi_p (idx))
884 {
885 tree c, type = limb_access_type (TREE_TYPE (op), idx);
886 unsigned HOST_WIDE_INT i = tree_to_uhwi (idx);
887 if (m_first)
888 {
889 m_data.safe_push (NULL_TREE);
890 m_data.safe_push (NULL_TREE);
891 }
892 if (limb_prec != HOST_BITS_PER_WIDE_INT)
893 {
894 wide_int w = wi::rshift (wi::to_wide (op), i * limb_prec,
895 TYPE_SIGN (TREE_TYPE (op)));
896 c = wide_int_to_tree (type,
897 wide_int::from (w, TYPE_PRECISION (type),
898 UNSIGNED));
899 }
900 else if (i >= TREE_INT_CST_EXT_NUNITS (op))
901 c = build_int_cst (type,
902 tree_int_cst_sgn (op) < 0 ? -1 : 0);
903 else
904 c = build_int_cst (type, TREE_INT_CST_ELT (op, i));
905 m_data_cnt += 2;
906 return c;
907 }
908 if (m_first
909 || (m_data[m_data_cnt] == NULL_TREE
910 && m_data[m_data_cnt + 1] == NULL_TREE))
911 {
912 unsigned int prec = TYPE_PRECISION (TREE_TYPE (op));
913 unsigned int rem = prec % ((m_upwards_2limb ? 2 : 1) * limb_prec);
914 int ext;
915 unsigned min_prec = bitint_min_cst_precision (op, ext);
916 if (m_first)
917 {
918 m_data.safe_push (NULL_TREE);
919 m_data.safe_push (NULL_TREE);
920 }
921 if (integer_zerop (op))
922 {
923 tree c = build_zero_cst (m_limb_type);
924 m_data[m_data_cnt] = c;
925 m_data[m_data_cnt + 1] = c;
926 }
927 else if (integer_all_onesp (op))
928 {
929 tree c = build_all_ones_cst (m_limb_type);
930 m_data[m_data_cnt] = c;
931 m_data[m_data_cnt + 1] = c;
932 }
933 else if (m_upwards_2limb && min_prec <= (unsigned) limb_prec)
934 {
935 /* Single limb constant. Use a phi with that limb from
936 the preheader edge and 0 or -1 constant from the other edge
937 and for the second limb in the loop. */
938 tree out;
939 gcc_assert (m_first);
940 m_data.pop ();
941 m_data.pop ();
942 prepare_data_in_out (fold_convert (m_limb_type, op), idx, &out,
943 build_int_cst (m_limb_type, ext));
944 }
945 else if (min_prec > prec - rem - 2 * limb_prec)
946 {
947 /* Constant which has enough significant bits that it isn't
948 worth trying to save .rodata space by extending from smaller
949 number. */
950 tree type;
951 if (m_var_msb)
952 type = TREE_TYPE (op);
953 else
954 /* If we have a guarantee the most significant partial limb
955 (if any) will be only accessed through handle_operand
956 with INTEGER_CST idx, we don't need to include the partial
957 limb in .rodata. */
958 type = build_bitint_type (prec - rem, 1);
959 tree c = tree_output_constant_def (fold_convert (type, op));
960 m_data[m_data_cnt] = c;
961 m_data[m_data_cnt + 1] = NULL_TREE;
962 }
963 else if (m_upwards_2limb)
964 {
965 /* Constant with smaller number of bits. Trade conditional
966 code for .rodata space by extending from smaller number. */
967 min_prec = CEIL (min_prec, 2 * limb_prec) * (2 * limb_prec);
968 tree type = build_bitint_type (min_prec, 1);
969 tree c = tree_output_constant_def (fold_convert (type, op));
970 tree idx2 = make_ssa_name (sizetype);
971 g = gimple_build_assign (idx2, PLUS_EXPR, idx, size_one_node);
972 insert_before (g);
973 g = gimple_build_cond (LT_EXPR, idx,
974 size_int (min_prec / limb_prec),
975 NULL_TREE, NULL_TREE);
976 edge edge_true, edge_false;
977 if_then (g, (min_prec >= (prec - rem) / 2
978 ? profile_probability::likely ()
979 : profile_probability::unlikely ()),
980 edge_true, edge_false);
981 tree c1 = limb_access (TREE_TYPE (op), c, idx, false);
982 g = gimple_build_assign (make_ssa_name (TREE_TYPE (c1)), c1);
983 insert_before (g);
984 c1 = gimple_assign_lhs (g);
985 tree c2 = limb_access (TREE_TYPE (op), c, idx2, false);
986 g = gimple_build_assign (make_ssa_name (TREE_TYPE (c2)), c2);
987 insert_before (g);
988 c2 = gimple_assign_lhs (g);
989 tree c3 = build_int_cst (m_limb_type, ext);
990 m_gsi = gsi_after_labels (edge_true->dest);
991 m_data[m_data_cnt] = make_ssa_name (m_limb_type);
992 m_data[m_data_cnt + 1] = make_ssa_name (m_limb_type);
993 gphi *phi = create_phi_node (m_data[m_data_cnt],
994 edge_true->dest);
995 add_phi_arg (phi, c1, edge_true, UNKNOWN_LOCATION);
996 add_phi_arg (phi, c3, edge_false, UNKNOWN_LOCATION);
997 phi = create_phi_node (m_data[m_data_cnt + 1], edge_true->dest);
998 add_phi_arg (phi, c2, edge_true, UNKNOWN_LOCATION);
999 add_phi_arg (phi, c3, edge_false, UNKNOWN_LOCATION);
1000 }
1001 else
1002 {
1003 /* Constant with smaller number of bits. Trade conditional
1004 code for .rodata space by extending from smaller number.
1005 Version for loops with random access to the limbs or
1006 downwards loops. */
1007 min_prec = CEIL (min_prec, limb_prec) * limb_prec;
1008 tree c;
1009 if (min_prec <= (unsigned) limb_prec)
1010 c = fold_convert (m_limb_type, op);
1011 else
1012 {
1013 tree type = build_bitint_type (min_prec, 1);
1014 c = tree_output_constant_def (fold_convert (type, op));
1015 }
1016 m_data[m_data_cnt] = c;
1017 m_data[m_data_cnt + 1] = integer_type_node;
1018 }
1019 t = m_data[m_data_cnt];
1020 if (m_data[m_data_cnt + 1] == NULL_TREE)
1021 {
1022 t = limb_access (TREE_TYPE (op), t, idx, false);
1023 g = gimple_build_assign (make_ssa_name (TREE_TYPE (t)), t);
1024 insert_before (g);
1025 t = gimple_assign_lhs (g);
1026 }
1027 }
1028 else if (m_data[m_data_cnt + 1] == NULL_TREE)
1029 {
1030 t = limb_access (TREE_TYPE (op), m_data[m_data_cnt], idx, false);
1031 g = gimple_build_assign (make_ssa_name (TREE_TYPE (t)), t);
1032 insert_before (g);
1033 t = gimple_assign_lhs (g);
1034 }
1035 else
1036 t = m_data[m_data_cnt + 1];
1037 if (m_data[m_data_cnt + 1] == integer_type_node)
1038 {
1039 unsigned int prec = TYPE_PRECISION (TREE_TYPE (op));
1040 unsigned rem = prec % ((m_upwards_2limb ? 2 : 1) * limb_prec);
1041 int ext = wi::neg_p (wi::to_wide (op)) ? -1 : 0;
1042 tree c = m_data[m_data_cnt];
1043 unsigned min_prec = TYPE_PRECISION (TREE_TYPE (c));
1044 g = gimple_build_cond (LT_EXPR, idx,
1045 size_int (min_prec / limb_prec),
1046 NULL_TREE, NULL_TREE);
1047 edge edge_true, edge_false;
1048 if_then (g, (min_prec >= (prec - rem) / 2
1049 ? profile_probability::likely ()
1050 : profile_probability::unlikely ()),
1051 edge_true, edge_false);
1052 if (min_prec > (unsigned) limb_prec)
1053 {
1054 c = limb_access (TREE_TYPE (op), c, idx, false);
1055 g = gimple_build_assign (make_ssa_name (TREE_TYPE (c)), c);
1056 insert_before (g);
1057 c = gimple_assign_lhs (g);
1058 }
1059 tree c2 = build_int_cst (m_limb_type, ext);
1060 m_gsi = gsi_after_labels (edge_true->dest);
1061 t = make_ssa_name (m_limb_type);
1062 gphi *phi = create_phi_node (t, edge_true->dest);
1063 add_phi_arg (phi, c, edge_true, UNKNOWN_LOCATION);
1064 add_phi_arg (phi, c2, edge_false, UNKNOWN_LOCATION);
1065 }
1066 m_data_cnt += 2;
1067 return t;
1068 default:
1069 gcc_unreachable ();
1070 }
1071 }
1072
1073 /* Helper method, add a PHI node with VAL from preheader edge if
1074 inside of a loop and m_first. Keep state in a pair of m_data
1075 elements. If VAL_OUT is non-NULL, use that as PHI argument from
1076 the latch edge, otherwise create a new SSA_NAME for it and let
1077 caller initialize it. */
1078
1079 tree
1080 bitint_large_huge::prepare_data_in_out (tree val, tree idx, tree *data_out,
1081 tree val_out)
1082 {
1083 if (!m_first)
1084 {
1085 *data_out = tree_fits_uhwi_p (idx) ? NULL_TREE : m_data[m_data_cnt + 1];
1086 return m_data[m_data_cnt];
1087 }
1088
1089 *data_out = NULL_TREE;
1090 if (tree_fits_uhwi_p (idx))
1091 {
1092 m_data.safe_push (val);
1093 m_data.safe_push (NULL_TREE);
1094 return val;
1095 }
1096
1097 tree in = make_ssa_name (TREE_TYPE (val));
1098 gphi *phi = create_phi_node (in, m_bb);
1099 edge e1 = find_edge (m_preheader_bb, m_bb);
1100 edge e2 = EDGE_PRED (m_bb, 0);
1101 if (e1 == e2)
1102 e2 = EDGE_PRED (m_bb, 1);
1103 add_phi_arg (phi, val, e1, UNKNOWN_LOCATION);
1104 tree out = val_out ? val_out : make_ssa_name (TREE_TYPE (val));
1105 add_phi_arg (phi, out, e2, UNKNOWN_LOCATION);
1106 m_data.safe_push (in);
1107 m_data.safe_push (out);
1108 return in;
1109 }
1110
1111 /* Return VAL cast to TYPE. If VAL is INTEGER_CST, just
1112 convert it without emitting any code, otherwise emit
1113 the conversion statement before the current location. */
1114
1115 tree
1116 bitint_large_huge::add_cast (tree type, tree val)
1117 {
1118 if (TREE_CODE (val) == INTEGER_CST)
1119 return fold_convert (type, val);
1120
1121 tree lhs = make_ssa_name (type);
1122 gimple *g = gimple_build_assign (lhs, NOP_EXPR, val);
1123 insert_before (g);
1124 return lhs;
1125 }
1126
1127 /* Helper of handle_stmt method, handle PLUS_EXPR or MINUS_EXPR. */
1128
1129 tree
1130 bitint_large_huge::handle_plus_minus (tree_code code, tree rhs1, tree rhs2,
1131 tree idx)
1132 {
1133 tree lhs, data_out, ctype;
1134 tree rhs1_type = TREE_TYPE (rhs1);
1135 gimple *g;
1136 tree data_in = prepare_data_in_out (build_zero_cst (m_limb_type), idx,
1137 &data_out);
1138
1139 if (optab_handler (code == PLUS_EXPR ? uaddc5_optab : usubc5_optab,
1140 TYPE_MODE (m_limb_type)) != CODE_FOR_nothing)
1141 {
1142 ctype = build_complex_type (m_limb_type);
1143 if (!types_compatible_p (rhs1_type, m_limb_type))
1144 {
1145 if (!TYPE_UNSIGNED (rhs1_type))
1146 {
1147 tree type = unsigned_type_for (rhs1_type);
1148 rhs1 = add_cast (type, rhs1);
1149 rhs2 = add_cast (type, rhs2);
1150 }
1151 rhs1 = add_cast (m_limb_type, rhs1);
1152 rhs2 = add_cast (m_limb_type, rhs2);
1153 }
1154 lhs = make_ssa_name (ctype);
1155 g = gimple_build_call_internal (code == PLUS_EXPR
1156 ? IFN_UADDC : IFN_USUBC,
1157 3, rhs1, rhs2, data_in);
1158 gimple_call_set_lhs (g, lhs);
1159 insert_before (g);
1160 if (data_out == NULL_TREE)
1161 data_out = make_ssa_name (m_limb_type);
1162 g = gimple_build_assign (data_out, IMAGPART_EXPR,
1163 build1 (IMAGPART_EXPR, m_limb_type, lhs));
1164 insert_before (g);
1165 }
1166 else if (types_compatible_p (rhs1_type, m_limb_type))
1167 {
1168 ctype = build_complex_type (m_limb_type);
1169 lhs = make_ssa_name (ctype);
1170 g = gimple_build_call_internal (code == PLUS_EXPR
1171 ? IFN_ADD_OVERFLOW : IFN_SUB_OVERFLOW,
1172 2, rhs1, rhs2);
1173 gimple_call_set_lhs (g, lhs);
1174 insert_before (g);
1175 if (data_out == NULL_TREE)
1176 data_out = make_ssa_name (m_limb_type);
1177 if (!integer_zerop (data_in))
1178 {
1179 rhs1 = make_ssa_name (m_limb_type);
1180 g = gimple_build_assign (rhs1, REALPART_EXPR,
1181 build1 (REALPART_EXPR, m_limb_type, lhs));
1182 insert_before (g);
1183 rhs2 = make_ssa_name (m_limb_type);
1184 g = gimple_build_assign (rhs2, IMAGPART_EXPR,
1185 build1 (IMAGPART_EXPR, m_limb_type, lhs));
1186 insert_before (g);
1187 lhs = make_ssa_name (ctype);
1188 g = gimple_build_call_internal (code == PLUS_EXPR
1189 ? IFN_ADD_OVERFLOW
1190 : IFN_SUB_OVERFLOW,
1191 2, rhs1, data_in);
1192 gimple_call_set_lhs (g, lhs);
1193 insert_before (g);
1194 data_in = make_ssa_name (m_limb_type);
1195 g = gimple_build_assign (data_in, IMAGPART_EXPR,
1196 build1 (IMAGPART_EXPR, m_limb_type, lhs));
1197 insert_before (g);
1198 g = gimple_build_assign (data_out, PLUS_EXPR, rhs2, data_in);
1199 insert_before (g);
1200 }
1201 else
1202 {
1203 g = gimple_build_assign (data_out, IMAGPART_EXPR,
1204 build1 (IMAGPART_EXPR, m_limb_type, lhs));
1205 insert_before (g);
1206 }
1207 }
1208 else
1209 {
1210 tree in = add_cast (rhs1_type, data_in);
1211 lhs = make_ssa_name (rhs1_type);
1212 g = gimple_build_assign (lhs, code, rhs1, rhs2);
1213 insert_before (g);
1214 rhs1 = make_ssa_name (rhs1_type);
1215 g = gimple_build_assign (rhs1, code, lhs, in);
1216 insert_before (g);
1217 m_data[m_data_cnt] = NULL_TREE;
1218 m_data_cnt += 2;
1219 return rhs1;
1220 }
1221 rhs1 = make_ssa_name (m_limb_type);
1222 g = gimple_build_assign (rhs1, REALPART_EXPR,
1223 build1 (REALPART_EXPR, m_limb_type, lhs));
1224 insert_before (g);
1225 if (!types_compatible_p (rhs1_type, m_limb_type))
1226 rhs1 = add_cast (rhs1_type, rhs1);
1227 m_data[m_data_cnt] = data_out;
1228 m_data_cnt += 2;
1229 return rhs1;
1230 }
1231
1232 /* Helper function for handle_stmt method, handle LSHIFT_EXPR by
1233 count in [0, limb_prec - 1] range. */
1234
1235 tree
1236 bitint_large_huge::handle_lshift (tree rhs1, tree rhs2, tree idx)
1237 {
1238 unsigned HOST_WIDE_INT cnt = tree_to_uhwi (rhs2);
1239 gcc_checking_assert (cnt < (unsigned) limb_prec);
1240 if (cnt == 0)
1241 return rhs1;
1242
1243 tree lhs, data_out, rhs1_type = TREE_TYPE (rhs1);
1244 gimple *g;
1245 tree data_in = prepare_data_in_out (build_zero_cst (m_limb_type), idx,
1246 &data_out);
1247
1248 if (!integer_zerop (data_in))
1249 {
1250 lhs = make_ssa_name (m_limb_type);
1251 g = gimple_build_assign (lhs, RSHIFT_EXPR, data_in,
1252 build_int_cst (unsigned_type_node,
1253 limb_prec - cnt));
1254 insert_before (g);
1255 if (!types_compatible_p (rhs1_type, m_limb_type))
1256 lhs = add_cast (rhs1_type, lhs);
1257 data_in = lhs;
1258 }
1259 if (types_compatible_p (rhs1_type, m_limb_type))
1260 {
1261 if (data_out == NULL_TREE)
1262 data_out = make_ssa_name (m_limb_type);
1263 g = gimple_build_assign (data_out, rhs1);
1264 insert_before (g);
1265 }
1266 if (cnt < (unsigned) TYPE_PRECISION (rhs1_type))
1267 {
1268 lhs = make_ssa_name (rhs1_type);
1269 g = gimple_build_assign (lhs, LSHIFT_EXPR, rhs1, rhs2);
1270 insert_before (g);
1271 if (!integer_zerop (data_in))
1272 {
1273 rhs1 = lhs;
1274 lhs = make_ssa_name (rhs1_type);
1275 g = gimple_build_assign (lhs, BIT_IOR_EXPR, rhs1, data_in);
1276 insert_before (g);
1277 }
1278 }
1279 else
1280 lhs = data_in;
1281 m_data[m_data_cnt] = data_out;
1282 m_data_cnt += 2;
1283 return lhs;
1284 }
1285
1286 /* Helper function for handle_stmt method, handle an integral
1287 to integral conversion. */
1288
1289 tree
1290 bitint_large_huge::handle_cast (tree lhs_type, tree rhs1, tree idx)
1291 {
1292 tree rhs_type = TREE_TYPE (rhs1);
1293 gimple *g;
1294 if ((TREE_CODE (rhs1) == SSA_NAME || TREE_CODE (rhs1) == INTEGER_CST)
1295 && TREE_CODE (lhs_type) == BITINT_TYPE
1296 && TREE_CODE (rhs_type) == BITINT_TYPE
1297 && bitint_precision_kind (lhs_type) >= bitint_prec_large
1298 && bitint_precision_kind (rhs_type) >= bitint_prec_large)
1299 {
1300 if (TYPE_PRECISION (rhs_type) >= TYPE_PRECISION (lhs_type)
1301 /* If lhs has bigger precision than rhs, we can use
1302 the simple case only if there is a guarantee that
1303 the most significant limb is handled in straight
1304 line code. If m_var_msb (on left shifts) or
1305 if m_upwards_2limb * limb_prec is equal to
1306 lhs precision or if not m_upwards_2limb and lhs_type
1307 has precision which is multiple of limb_prec that is
1308 not the case. */
1309 || (!m_var_msb
1310 && (CEIL (TYPE_PRECISION (lhs_type), limb_prec)
1311 == CEIL (TYPE_PRECISION (rhs_type), limb_prec))
1312 && ((!m_upwards_2limb
1313 && (TYPE_PRECISION (lhs_type) % limb_prec != 0))
1314 || (m_upwards_2limb
1315 && (m_upwards_2limb * limb_prec
1316 < TYPE_PRECISION (lhs_type))))))
1317 {
1318 rhs1 = handle_operand (rhs1, idx);
1319 if (tree_fits_uhwi_p (idx))
1320 {
1321 tree type = limb_access_type (lhs_type, idx);
1322 if (!types_compatible_p (type, TREE_TYPE (rhs1)))
1323 rhs1 = add_cast (type, rhs1);
1324 }
1325 return rhs1;
1326 }
1327 tree t;
1328 /* Indexes lower than this don't need any special processing. */
1329 unsigned low = ((unsigned) TYPE_PRECISION (rhs_type)
1330 - !TYPE_UNSIGNED (rhs_type)) / limb_prec;
1331 /* Indexes >= than this always contain an extension. */
1332 unsigned high = CEIL ((unsigned) TYPE_PRECISION (rhs_type), limb_prec);
1333 bool save_first = m_first;
1334 if (m_first)
1335 {
1336 m_data.safe_push (NULL_TREE);
1337 m_data.safe_push (NULL_TREE);
1338 m_data.safe_push (NULL_TREE);
1339 if (TYPE_UNSIGNED (rhs_type))
1340 /* No need to keep state between iterations. */
1341 ;
1342 else if (m_upwards && !m_upwards_2limb)
1343 /* We need to keep state between iterations, but
1344 not within any loop, everything is straight line
1345 code with only increasing indexes. */
1346 ;
1347 else if (!m_upwards_2limb)
1348 {
1349 unsigned save_data_cnt = m_data_cnt;
1350 gimple_stmt_iterator save_gsi = m_gsi;
1351 m_gsi = m_init_gsi;
1352 if (gsi_end_p (m_gsi))
1353 m_gsi = gsi_after_labels (gsi_bb (m_gsi));
1354 else
1355 gsi_next (&m_gsi);
1356 m_data_cnt = save_data_cnt + 3;
1357 t = handle_operand (rhs1, size_int (low));
1358 m_first = false;
1359 m_data[save_data_cnt + 2]
1360 = build_int_cst (NULL_TREE, m_data_cnt);
1361 m_data_cnt = save_data_cnt;
1362 t = add_cast (signed_type_for (m_limb_type), t);
1363 tree lpm1 = build_int_cst (unsigned_type_node, limb_prec - 1);
1364 tree n = make_ssa_name (TREE_TYPE (t));
1365 g = gimple_build_assign (n, RSHIFT_EXPR, t, lpm1);
1366 insert_before (g);
1367 m_data[save_data_cnt + 1] = add_cast (m_limb_type, n);
1368 m_init_gsi = m_gsi;
1369 if (gsi_end_p (m_init_gsi))
1370 m_init_gsi = gsi_last_bb (gsi_bb (m_init_gsi));
1371 else
1372 gsi_prev (&m_init_gsi);
1373 m_gsi = save_gsi;
1374 }
1375 else if (m_upwards_2limb * limb_prec < TYPE_PRECISION (rhs_type))
1376 /* We need to keep state between iterations, but
1377 fortunately not within the loop, only afterwards. */
1378 ;
1379 else
1380 {
1381 tree out;
1382 m_data.truncate (m_data_cnt);
1383 prepare_data_in_out (build_zero_cst (m_limb_type), idx, &out);
1384 m_data.safe_push (NULL_TREE);
1385 }
1386 }
1387
1388 unsigned save_data_cnt = m_data_cnt;
1389 m_data_cnt += 3;
1390 if (!tree_fits_uhwi_p (idx))
1391 {
1392 if (m_upwards_2limb
1393 && low >= m_upwards_2limb - m_first)
1394 {
1395 rhs1 = handle_operand (rhs1, idx);
1396 if (m_first)
1397 m_data[save_data_cnt + 2]
1398 = build_int_cst (NULL_TREE, m_data_cnt);
1399 m_first = save_first;
1400 return rhs1;
1401 }
1402 bool single_comparison
1403 = low == high || (m_upwards_2limb && (low & 1) == m_first);
1404 tree idxc = idx;
1405 if (!single_comparison
1406 && m_upwards_2limb
1407 && !m_first
1408 && low + 1 == m_upwards_2limb)
1409 /* In this case we know that idx <= low always,
1410 so effectively we just needs a single comparison,
1411 idx < low or idx == low, but we'd need to emit different
1412 code for the 2 branches than single_comparison normally
1413 emits. So, instead of special-casing that, emit a
1414 low <= low comparison which cfg cleanup will clean up
1415 at the end of the pass. */
1416 idxc = size_int (low);
1417 g = gimple_build_cond (single_comparison ? LT_EXPR : LE_EXPR,
1418 idxc, size_int (low), NULL_TREE, NULL_TREE);
1419 edge edge_true_true, edge_true_false, edge_false;
1420 if_then_if_then_else (g, (single_comparison ? NULL
1421 : gimple_build_cond (EQ_EXPR, idx,
1422 size_int (low),
1423 NULL_TREE,
1424 NULL_TREE)),
1425 profile_probability::likely (),
1426 profile_probability::unlikely (),
1427 edge_true_true, edge_true_false, edge_false);
1428 bool save_cast_conditional = m_cast_conditional;
1429 m_cast_conditional = true;
1430 m_bitfld_load = 0;
1431 tree t1 = handle_operand (rhs1, idx), t2 = NULL_TREE;
1432 if (m_first)
1433 m_data[save_data_cnt + 2]
1434 = build_int_cst (NULL_TREE, m_data_cnt);
1435 tree ext = NULL_TREE;
1436 tree bitfld = NULL_TREE;
1437 if (!single_comparison)
1438 {
1439 m_gsi = gsi_after_labels (edge_true_true->src);
1440 m_first = false;
1441 m_data_cnt = save_data_cnt + 3;
1442 if (m_bitfld_load)
1443 {
1444 bitfld = m_data[m_bitfld_load];
1445 m_data[m_bitfld_load] = m_data[m_bitfld_load + 2];
1446 m_bitfld_load = 0;
1447 }
1448 t2 = handle_operand (rhs1, size_int (low));
1449 if (!useless_type_conversion_p (m_limb_type, TREE_TYPE (t2)))
1450 t2 = add_cast (m_limb_type, t2);
1451 if (!TYPE_UNSIGNED (rhs_type) && m_upwards_2limb)
1452 {
1453 ext = add_cast (signed_type_for (m_limb_type), t2);
1454 tree lpm1 = build_int_cst (unsigned_type_node,
1455 limb_prec - 1);
1456 tree n = make_ssa_name (TREE_TYPE (ext));
1457 g = gimple_build_assign (n, RSHIFT_EXPR, ext, lpm1);
1458 insert_before (g);
1459 ext = add_cast (m_limb_type, n);
1460 }
1461 }
1462 tree t3;
1463 if (TYPE_UNSIGNED (rhs_type))
1464 t3 = build_zero_cst (m_limb_type);
1465 else if (m_upwards_2limb && (save_first || ext != NULL_TREE))
1466 t3 = m_data[save_data_cnt];
1467 else
1468 t3 = m_data[save_data_cnt + 1];
1469 m_gsi = gsi_after_labels (edge_true_false->dest);
1470 t = make_ssa_name (m_limb_type);
1471 gphi *phi = create_phi_node (t, edge_true_false->dest);
1472 add_phi_arg (phi, t1, edge_true_false, UNKNOWN_LOCATION);
1473 add_phi_arg (phi, t3, edge_false, UNKNOWN_LOCATION);
1474 if (edge_true_true)
1475 add_phi_arg (phi, t2, edge_true_true, UNKNOWN_LOCATION);
1476 if (ext)
1477 {
1478 tree t4 = make_ssa_name (m_limb_type);
1479 phi = create_phi_node (t4, edge_true_false->dest);
1480 add_phi_arg (phi, build_zero_cst (m_limb_type), edge_true_false,
1481 UNKNOWN_LOCATION);
1482 add_phi_arg (phi, m_data[save_data_cnt], edge_false,
1483 UNKNOWN_LOCATION);
1484 add_phi_arg (phi, ext, edge_true_true, UNKNOWN_LOCATION);
1485 if (!save_cast_conditional)
1486 {
1487 g = gimple_build_assign (m_data[save_data_cnt + 1], t4);
1488 insert_before (g);
1489 }
1490 else
1491 for (basic_block bb = gsi_bb (m_gsi);;)
1492 {
1493 edge e1 = single_succ_edge (bb);
1494 edge e2 = find_edge (e1->dest, m_bb), e3;
1495 tree t5 = (e2 ? m_data[save_data_cnt + 1]
1496 : make_ssa_name (m_limb_type));
1497 phi = create_phi_node (t5, e1->dest);
1498 edge_iterator ei;
1499 FOR_EACH_EDGE (e3, ei, e1->dest->preds)
1500 add_phi_arg (phi, (e3 == e1 ? t4
1501 : build_zero_cst (m_limb_type)),
1502 e3, UNKNOWN_LOCATION);
1503 if (e2)
1504 break;
1505 t4 = t5;
1506 bb = e1->dest;
1507 }
1508 }
1509 if (m_bitfld_load)
1510 {
1511 tree t4;
1512 if (!save_first && !save_cast_conditional)
1513 t4 = m_data[m_bitfld_load + 1];
1514 else
1515 t4 = make_ssa_name (m_limb_type);
1516 phi = create_phi_node (t4, edge_true_false->dest);
1517 add_phi_arg (phi,
1518 edge_true_true ? bitfld : m_data[m_bitfld_load],
1519 edge_true_false, UNKNOWN_LOCATION);
1520 add_phi_arg (phi, m_data[m_bitfld_load + 2],
1521 edge_false, UNKNOWN_LOCATION);
1522 if (edge_true_true)
1523 add_phi_arg (phi, m_data[m_bitfld_load], edge_true_true,
1524 UNKNOWN_LOCATION);
1525 if (save_cast_conditional)
1526 for (basic_block bb = gsi_bb (m_gsi);;)
1527 {
1528 edge e1 = single_succ_edge (bb);
1529 edge e2 = find_edge (e1->dest, m_bb), e3;
1530 tree t5 = ((e2 && !save_first) ? m_data[m_bitfld_load + 1]
1531 : make_ssa_name (m_limb_type));
1532 phi = create_phi_node (t5, e1->dest);
1533 edge_iterator ei;
1534 FOR_EACH_EDGE (e3, ei, e1->dest->preds)
1535 add_phi_arg (phi, (e3 == e1 ? t4
1536 : build_zero_cst (m_limb_type)),
1537 e3, UNKNOWN_LOCATION);
1538 t4 = t5;
1539 if (e2)
1540 break;
1541 bb = e1->dest;
1542 }
1543 m_data[m_bitfld_load] = t4;
1544 m_data[m_bitfld_load + 2] = t4;
1545 m_bitfld_load = 0;
1546 }
1547 m_cast_conditional = save_cast_conditional;
1548 m_first = save_first;
1549 return t;
1550 }
1551 else
1552 {
1553 unsigned tidx = tree_to_uhwi (idx);
1554 if (tidx < low)
1555 {
1556 t = handle_operand (rhs1, idx);
1557 if (m_first)
1558 m_data[save_data_cnt + 2]
1559 = build_int_cst (NULL_TREE, m_data_cnt);
1560 }
1561 else if (tidx < high)
1562 {
1563 t = handle_operand (rhs1, size_int (low));
1564 if (m_first)
1565 m_data[save_data_cnt + 2]
1566 = build_int_cst (NULL_TREE, m_data_cnt);
1567 if (!useless_type_conversion_p (m_limb_type, TREE_TYPE (t)))
1568 t = add_cast (m_limb_type, t);
1569 tree ext = NULL_TREE;
1570 if (!TYPE_UNSIGNED (rhs_type) && m_upwards)
1571 {
1572 ext = add_cast (signed_type_for (m_limb_type), t);
1573 tree lpm1 = build_int_cst (unsigned_type_node,
1574 limb_prec - 1);
1575 tree n = make_ssa_name (TREE_TYPE (ext));
1576 g = gimple_build_assign (n, RSHIFT_EXPR, ext, lpm1);
1577 insert_before (g);
1578 ext = add_cast (m_limb_type, n);
1579 m_data[save_data_cnt + 1] = ext;
1580 }
1581 }
1582 else
1583 {
1584 if (TYPE_UNSIGNED (rhs_type) && m_first)
1585 {
1586 handle_operand (rhs1, size_zero_node);
1587 m_data[save_data_cnt + 2]
1588 = build_int_cst (NULL_TREE, m_data_cnt);
1589 }
1590 else
1591 m_data_cnt = tree_to_uhwi (m_data[save_data_cnt + 2]);
1592 if (TYPE_UNSIGNED (rhs_type))
1593 t = build_zero_cst (m_limb_type);
1594 else if (m_bb
1595 && m_data[save_data_cnt]
1596 && ((tidx & 1) == 0 || tidx != low + 1))
1597 t = m_data[save_data_cnt];
1598 else
1599 t = m_data[save_data_cnt + 1];
1600 }
1601 tree type = limb_access_type (lhs_type, idx);
1602 if (!useless_type_conversion_p (type, m_limb_type))
1603 t = add_cast (type, t);
1604 m_first = save_first;
1605 return t;
1606 }
1607 }
1608 else if (TREE_CODE (lhs_type) == BITINT_TYPE
1609 && bitint_precision_kind (lhs_type) >= bitint_prec_large
1610 && INTEGRAL_TYPE_P (rhs_type))
1611 {
1612 /* Add support for 3 or more limbs filled in from normal integral
1613 type if this assert fails. If no target chooses limb mode smaller
1614 than half of largest supported normal integral type, this will not
1615 be needed. */
1616 gcc_assert (TYPE_PRECISION (rhs_type) <= 2 * limb_prec);
1617 tree r1 = NULL_TREE, r2 = NULL_TREE, rext = NULL_TREE;
1618 if (m_first)
1619 {
1620 gimple_stmt_iterator save_gsi = m_gsi;
1621 m_gsi = m_init_gsi;
1622 if (gsi_end_p (m_gsi))
1623 m_gsi = gsi_after_labels (gsi_bb (m_gsi));
1624 else
1625 gsi_next (&m_gsi);
1626 if (TREE_CODE (rhs_type) == BITINT_TYPE
1627 && bitint_precision_kind (rhs_type) == bitint_prec_middle)
1628 {
1629 tree type = NULL_TREE;
1630 rhs1 = maybe_cast_middle_bitint (&m_gsi, rhs1, type);
1631 rhs_type = TREE_TYPE (rhs1);
1632 }
1633 r1 = rhs1;
1634 if (!useless_type_conversion_p (m_limb_type, TREE_TYPE (rhs1)))
1635 r1 = add_cast (m_limb_type, rhs1);
1636 if (TYPE_PRECISION (rhs_type) > limb_prec)
1637 {
1638 g = gimple_build_assign (make_ssa_name (rhs_type),
1639 RSHIFT_EXPR, rhs1,
1640 build_int_cst (unsigned_type_node,
1641 limb_prec));
1642 insert_before (g);
1643 r2 = add_cast (m_limb_type, gimple_assign_lhs (g));
1644 }
1645 if (TYPE_UNSIGNED (rhs_type))
1646 rext = build_zero_cst (m_limb_type);
1647 else
1648 {
1649 rext = add_cast (signed_type_for (m_limb_type), r2 ? r2 : r1);
1650 g = gimple_build_assign (make_ssa_name (TREE_TYPE (rext)),
1651 RSHIFT_EXPR, rext,
1652 build_int_cst (unsigned_type_node,
1653 limb_prec - 1));
1654 insert_before (g);
1655 rext = add_cast (m_limb_type, gimple_assign_lhs (g));
1656 }
1657 m_init_gsi = m_gsi;
1658 if (gsi_end_p (m_init_gsi))
1659 m_init_gsi = gsi_last_bb (gsi_bb (m_init_gsi));
1660 else
1661 gsi_prev (&m_init_gsi);
1662 m_gsi = save_gsi;
1663 }
1664 tree t;
1665 if (m_upwards_2limb)
1666 {
1667 if (m_first)
1668 {
1669 tree out1, out2;
1670 prepare_data_in_out (r1, idx, &out1, rext);
1671 if (TYPE_PRECISION (rhs_type) > limb_prec)
1672 {
1673 prepare_data_in_out (r2, idx, &out2, rext);
1674 m_data.pop ();
1675 t = m_data.pop ();
1676 m_data[m_data_cnt + 1] = t;
1677 }
1678 else
1679 m_data[m_data_cnt + 1] = rext;
1680 m_data.safe_push (rext);
1681 t = m_data[m_data_cnt];
1682 }
1683 else if (!tree_fits_uhwi_p (idx))
1684 t = m_data[m_data_cnt + 1];
1685 else
1686 {
1687 tree type = limb_access_type (lhs_type, idx);
1688 t = m_data[m_data_cnt + 2];
1689 if (!useless_type_conversion_p (type, m_limb_type))
1690 t = add_cast (type, t);
1691 }
1692 m_data_cnt += 3;
1693 return t;
1694 }
1695 else if (m_first)
1696 {
1697 m_data.safe_push (r1);
1698 m_data.safe_push (r2);
1699 m_data.safe_push (rext);
1700 }
1701 if (tree_fits_uhwi_p (idx))
1702 {
1703 tree type = limb_access_type (lhs_type, idx);
1704 if (integer_zerop (idx))
1705 t = m_data[m_data_cnt];
1706 else if (TYPE_PRECISION (rhs_type) > limb_prec
1707 && integer_onep (idx))
1708 t = m_data[m_data_cnt + 1];
1709 else
1710 t = m_data[m_data_cnt + 2];
1711 if (!useless_type_conversion_p (type, m_limb_type))
1712 t = add_cast (type, t);
1713 m_data_cnt += 3;
1714 return t;
1715 }
1716 g = gimple_build_cond (NE_EXPR, idx, size_zero_node,
1717 NULL_TREE, NULL_TREE);
1718 edge e2, e3, e4 = NULL;
1719 if_then (g, profile_probability::likely (), e2, e3);
1720 if (m_data[m_data_cnt + 1])
1721 {
1722 g = gimple_build_cond (EQ_EXPR, idx, size_one_node,
1723 NULL_TREE, NULL_TREE);
1724 insert_before (g);
1725 edge e5 = split_block (gsi_bb (m_gsi), g);
1726 e4 = make_edge (e5->src, e2->dest, EDGE_TRUE_VALUE);
1727 e2 = find_edge (e5->dest, e2->dest);
1728 e4->probability = profile_probability::unlikely ();
1729 e5->flags = EDGE_FALSE_VALUE;
1730 e5->probability = e4->probability.invert ();
1731 }
1732 m_gsi = gsi_after_labels (e2->dest);
1733 t = make_ssa_name (m_limb_type);
1734 gphi *phi = create_phi_node (t, e2->dest);
1735 add_phi_arg (phi, m_data[m_data_cnt + 2], e2, UNKNOWN_LOCATION);
1736 add_phi_arg (phi, m_data[m_data_cnt], e3, UNKNOWN_LOCATION);
1737 if (e4)
1738 add_phi_arg (phi, m_data[m_data_cnt + 1], e4, UNKNOWN_LOCATION);
1739 m_data_cnt += 3;
1740 return t;
1741 }
1742 return NULL_TREE;
1743 }
1744
1745 /* Helper function for handle_stmt method, handle a BIT_FIELD_REF. */
1746
1747 tree
1748 bitint_large_huge::handle_bit_field_ref (tree op, tree idx)
1749 {
1750 if (tree_fits_uhwi_p (idx))
1751 {
1752 if (m_first)
1753 m_data.safe_push (NULL);
1754 ++m_data_cnt;
1755 unsigned HOST_WIDE_INT sz = tree_to_uhwi (TYPE_SIZE (m_limb_type));
1756 tree bfr = build3 (BIT_FIELD_REF, m_limb_type,
1757 TREE_OPERAND (op, 0),
1758 TYPE_SIZE (m_limb_type),
1759 size_binop (PLUS_EXPR, TREE_OPERAND (op, 2),
1760 bitsize_int (tree_to_uhwi (idx) * sz)));
1761 tree r = make_ssa_name (m_limb_type);
1762 gimple *g = gimple_build_assign (r, bfr);
1763 insert_before (g);
1764 tree type = limb_access_type (TREE_TYPE (op), idx);
1765 if (!useless_type_conversion_p (type, m_limb_type))
1766 r = add_cast (type, r);
1767 return r;
1768 }
1769 tree var;
1770 if (m_first)
1771 {
1772 unsigned HOST_WIDE_INT sz = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (op)));
1773 machine_mode mode;
1774 tree type, bfr;
1775 if (bitwise_mode_for_size (sz).exists (&mode)
1776 && known_eq (GET_MODE_BITSIZE (mode), sz))
1777 type = bitwise_type_for_mode (mode);
1778 else
1779 {
1780 mode = VOIDmode;
1781 type = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (op, 0)));
1782 }
1783 if (TYPE_ALIGN (type) < TYPE_ALIGN (TREE_TYPE (op)))
1784 type = build_aligned_type (type, TYPE_ALIGN (TREE_TYPE (op)));
1785 var = create_tmp_var (type);
1786 TREE_ADDRESSABLE (var) = 1;
1787 gimple *g;
1788 if (mode != VOIDmode)
1789 {
1790 bfr = build3 (BIT_FIELD_REF, type, TREE_OPERAND (op, 0),
1791 TYPE_SIZE (type), TREE_OPERAND (op, 2));
1792 g = gimple_build_assign (make_ssa_name (type),
1793 BIT_FIELD_REF, bfr);
1794 gimple_set_location (g, m_loc);
1795 gsi_insert_after (&m_init_gsi, g, GSI_NEW_STMT);
1796 bfr = gimple_assign_lhs (g);
1797 }
1798 else
1799 bfr = TREE_OPERAND (op, 0);
1800 g = gimple_build_assign (var, bfr);
1801 gimple_set_location (g, m_loc);
1802 gsi_insert_after (&m_init_gsi, g, GSI_NEW_STMT);
1803 if (mode == VOIDmode)
1804 {
1805 unsigned HOST_WIDE_INT nelts
1806 = CEIL (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (op))), limb_prec);
1807 tree atype = build_array_type_nelts (m_limb_type, nelts);
1808 var = build2 (MEM_REF, atype, build_fold_addr_expr (var),
1809 build_int_cst (build_pointer_type (type),
1810 tree_to_uhwi (TREE_OPERAND (op, 2))
1811 / BITS_PER_UNIT));
1812 }
1813 m_data.safe_push (var);
1814 }
1815 else
1816 var = unshare_expr (m_data[m_data_cnt]);
1817 ++m_data_cnt;
1818 var = limb_access (TREE_TYPE (op), var, idx, false);
1819 tree r = make_ssa_name (m_limb_type);
1820 gimple *g = gimple_build_assign (r, var);
1821 insert_before (g);
1822 return r;
1823 }
1824
1825 /* Add a new EH edge from SRC to EH_EDGE->dest, where EH_EDGE
1826 is an older EH edge, and except for virtual PHIs duplicate the
1827 PHI argument from the EH_EDGE to the new EH edge. */
1828
1829 static void
1830 add_eh_edge (basic_block src, edge eh_edge)
1831 {
1832 edge e = make_edge (src, eh_edge->dest, EDGE_EH);
1833 e->probability = profile_probability::very_unlikely ();
1834 for (gphi_iterator gsi = gsi_start_phis (eh_edge->dest);
1835 !gsi_end_p (gsi); gsi_next (&gsi))
1836 {
1837 gphi *phi = gsi.phi ();
1838 tree lhs = gimple_phi_result (phi);
1839 if (virtual_operand_p (lhs))
1840 continue;
1841 const phi_arg_d *arg = gimple_phi_arg (phi, eh_edge->dest_idx);
1842 add_phi_arg (phi, arg->def, e, arg->locus);
1843 }
1844 }
1845
1846 /* Helper function for handle_stmt method, handle a load from memory. */
1847
1848 tree
1849 bitint_large_huge::handle_load (gimple *stmt, tree idx)
1850 {
1851 tree rhs1 = gimple_assign_rhs1 (stmt);
1852 tree rhs_type = TREE_TYPE (rhs1);
1853 bool eh = stmt_ends_bb_p (stmt);
1854 edge eh_edge = NULL;
1855 gimple *g;
1856
1857 if (TREE_CODE (rhs1) == BIT_FIELD_REF
1858 && integer_zerop (TREE_OPERAND (rhs1, 2)))
1859 rhs1 = TREE_OPERAND (rhs1, 0);
1860
1861 if (eh)
1862 {
1863 edge_iterator ei;
1864 basic_block bb = gimple_bb (stmt);
1865
1866 FOR_EACH_EDGE (eh_edge, ei, bb->succs)
1867 if (eh_edge->flags & EDGE_EH)
1868 break;
1869 }
1870
1871 if (TREE_CODE (rhs1) == COMPONENT_REF
1872 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (rhs1, 1)))
1873 {
1874 tree fld = TREE_OPERAND (rhs1, 1);
1875 /* For little-endian, we can allow as inputs bit-fields
1876 which start at a limb boundary. */
1877 gcc_assert (tree_fits_uhwi_p (DECL_FIELD_BIT_OFFSET (fld)));
1878 if (DECL_OFFSET_ALIGN (fld) >= TYPE_ALIGN (TREE_TYPE (rhs1))
1879 && (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (fld)) % limb_prec) == 0)
1880 goto normal_load;
1881 /* Even if DECL_FIELD_BIT_OFFSET (fld) is a multiple of UNITS_PER_BIT,
1882 handle it normally for now. */
1883 if ((tree_to_uhwi (DECL_FIELD_BIT_OFFSET (fld)) % BITS_PER_UNIT) == 0)
1884 goto normal_load;
1885 tree repr = DECL_BIT_FIELD_REPRESENTATIVE (fld);
1886 poly_int64 bitoffset;
1887 poly_uint64 field_offset, repr_offset;
1888 bool var_field_off = false;
1889 if (poly_int_tree_p (DECL_FIELD_OFFSET (fld), &field_offset)
1890 && poly_int_tree_p (DECL_FIELD_OFFSET (repr), &repr_offset))
1891 bitoffset = (field_offset - repr_offset) * BITS_PER_UNIT;
1892 else
1893 {
1894 bitoffset = 0;
1895 var_field_off = true;
1896 }
1897 bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (fld))
1898 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr)));
1899 tree nrhs1 = build3 (COMPONENT_REF, TREE_TYPE (repr),
1900 TREE_OPERAND (rhs1, 0), repr,
1901 var_field_off ? TREE_OPERAND (rhs1, 2) : NULL_TREE);
1902 HOST_WIDE_INT bo = bitoffset.to_constant ();
1903 unsigned bo_idx = (unsigned HOST_WIDE_INT) bo / limb_prec;
1904 unsigned bo_bit = (unsigned HOST_WIDE_INT) bo % limb_prec;
1905 if (m_first)
1906 {
1907 if (m_upwards)
1908 {
1909 gimple_stmt_iterator save_gsi = m_gsi;
1910 m_gsi = m_init_gsi;
1911 if (gsi_end_p (m_gsi))
1912 m_gsi = gsi_after_labels (gsi_bb (m_gsi));
1913 else
1914 gsi_next (&m_gsi);
1915 tree t = limb_access (NULL_TREE, nrhs1, size_int (bo_idx), true);
1916 tree iv = make_ssa_name (m_limb_type);
1917 g = gimple_build_assign (iv, t);
1918 insert_before (g);
1919 if (eh)
1920 {
1921 maybe_duplicate_eh_stmt (g, stmt);
1922 if (eh_edge)
1923 {
1924 edge e = split_block (gsi_bb (m_gsi), g);
1925 add_eh_edge (e->src, eh_edge);
1926 m_gsi = gsi_after_labels (e->dest);
1927 if (gsi_bb (save_gsi) == e->src)
1928 {
1929 if (gsi_end_p (save_gsi))
1930 save_gsi = gsi_end_bb (e->dest);
1931 else
1932 save_gsi = gsi_for_stmt (gsi_stmt (save_gsi));
1933 }
1934 if (m_preheader_bb == e->src)
1935 m_preheader_bb = e->dest;
1936 }
1937 }
1938 m_init_gsi = m_gsi;
1939 if (gsi_end_p (m_init_gsi))
1940 m_init_gsi = gsi_last_bb (gsi_bb (m_init_gsi));
1941 else
1942 gsi_prev (&m_init_gsi);
1943 m_gsi = save_gsi;
1944 tree out;
1945 prepare_data_in_out (iv, idx, &out);
1946 out = m_data[m_data_cnt];
1947 m_data.safe_push (out);
1948 }
1949 else
1950 {
1951 m_data.safe_push (NULL_TREE);
1952 m_data.safe_push (NULL_TREE);
1953 m_data.safe_push (NULL_TREE);
1954 }
1955 }
1956
1957 tree nidx0 = NULL_TREE, nidx1;
1958 tree iv = m_data[m_data_cnt];
1959 if (m_cast_conditional && iv)
1960 {
1961 gcc_assert (!m_bitfld_load);
1962 m_bitfld_load = m_data_cnt;
1963 }
1964 if (tree_fits_uhwi_p (idx))
1965 {
1966 unsigned prec = TYPE_PRECISION (rhs_type);
1967 unsigned HOST_WIDE_INT i = tree_to_uhwi (idx);
1968 gcc_assert (i * limb_prec < prec);
1969 nidx1 = size_int (i + bo_idx + 1);
1970 if ((i + 1) * limb_prec > prec)
1971 {
1972 prec %= limb_prec;
1973 if (prec + bo_bit <= (unsigned) limb_prec)
1974 nidx1 = NULL_TREE;
1975 }
1976 if (!iv)
1977 nidx0 = size_int (i + bo_idx);
1978 }
1979 else
1980 {
1981 if (!iv)
1982 {
1983 if (bo_idx == 0)
1984 nidx0 = idx;
1985 else
1986 {
1987 nidx0 = make_ssa_name (sizetype);
1988 g = gimple_build_assign (nidx0, PLUS_EXPR, idx,
1989 size_int (bo_idx));
1990 insert_before (g);
1991 }
1992 }
1993 nidx1 = make_ssa_name (sizetype);
1994 g = gimple_build_assign (nidx1, PLUS_EXPR, idx,
1995 size_int (bo_idx + 1));
1996 insert_before (g);
1997 }
1998
1999 tree iv2 = NULL_TREE;
2000 if (nidx0)
2001 {
2002 tree t = limb_access (NULL_TREE, nrhs1, nidx0, true);
2003 iv = make_ssa_name (m_limb_type);
2004 g = gimple_build_assign (iv, t);
2005 insert_before (g);
2006 gcc_assert (!eh);
2007 }
2008 if (nidx1)
2009 {
2010 bool conditional = m_var_msb && !tree_fits_uhwi_p (idx);
2011 unsigned prec = TYPE_PRECISION (rhs_type);
2012 if (conditional)
2013 {
2014 if ((prec % limb_prec) == 0
2015 || ((prec % limb_prec) + bo_bit > (unsigned) limb_prec))
2016 conditional = false;
2017 }
2018 edge edge_true = NULL, edge_false = NULL;
2019 if (conditional)
2020 {
2021 g = gimple_build_cond (NE_EXPR, idx,
2022 size_int (prec / limb_prec),
2023 NULL_TREE, NULL_TREE);
2024 if_then (g, profile_probability::likely (),
2025 edge_true, edge_false);
2026 }
2027 tree t = limb_access (NULL_TREE, nrhs1, nidx1, true);
2028 if (m_upwards_2limb
2029 && !m_first
2030 && !m_bitfld_load
2031 && !tree_fits_uhwi_p (idx))
2032 iv2 = m_data[m_data_cnt + 1];
2033 else
2034 iv2 = make_ssa_name (m_limb_type);
2035 g = gimple_build_assign (iv2, t);
2036 insert_before (g);
2037 if (eh)
2038 {
2039 maybe_duplicate_eh_stmt (g, stmt);
2040 if (eh_edge)
2041 {
2042 edge e = split_block (gsi_bb (m_gsi), g);
2043 m_gsi = gsi_after_labels (e->dest);
2044 add_eh_edge (e->src, eh_edge);
2045 }
2046 }
2047 if (conditional)
2048 {
2049 tree iv3 = make_ssa_name (m_limb_type);
2050 if (eh)
2051 edge_true = find_edge (gsi_bb (m_gsi), edge_false->dest);
2052 gphi *phi = create_phi_node (iv3, edge_true->dest);
2053 add_phi_arg (phi, iv2, edge_true, UNKNOWN_LOCATION);
2054 add_phi_arg (phi, build_zero_cst (m_limb_type),
2055 edge_false, UNKNOWN_LOCATION);
2056 m_gsi = gsi_after_labels (edge_true->dest);
2057 iv2 = iv3;
2058 }
2059 }
2060 g = gimple_build_assign (make_ssa_name (m_limb_type), RSHIFT_EXPR,
2061 iv, build_int_cst (unsigned_type_node, bo_bit));
2062 insert_before (g);
2063 iv = gimple_assign_lhs (g);
2064 if (iv2)
2065 {
2066 g = gimple_build_assign (make_ssa_name (m_limb_type), LSHIFT_EXPR,
2067 iv2, build_int_cst (unsigned_type_node,
2068 limb_prec - bo_bit));
2069 insert_before (g);
2070 g = gimple_build_assign (make_ssa_name (m_limb_type), BIT_IOR_EXPR,
2071 gimple_assign_lhs (g), iv);
2072 insert_before (g);
2073 iv = gimple_assign_lhs (g);
2074 if (m_data[m_data_cnt])
2075 m_data[m_data_cnt] = iv2;
2076 }
2077 if (tree_fits_uhwi_p (idx))
2078 {
2079 tree atype = limb_access_type (rhs_type, idx);
2080 if (!useless_type_conversion_p (atype, TREE_TYPE (iv)))
2081 iv = add_cast (atype, iv);
2082 }
2083 m_data_cnt += 3;
2084 return iv;
2085 }
2086
2087 normal_load:
2088 /* Use write_p = true for loads with EH edges to make
2089 sure limb_access doesn't add a cast as separate
2090 statement after it. */
2091 rhs1 = limb_access (rhs_type, rhs1, idx, eh);
2092 tree ret = make_ssa_name (TREE_TYPE (rhs1));
2093 g = gimple_build_assign (ret, rhs1);
2094 insert_before (g);
2095 if (eh)
2096 {
2097 maybe_duplicate_eh_stmt (g, stmt);
2098 if (eh_edge)
2099 {
2100 edge e = split_block (gsi_bb (m_gsi), g);
2101 m_gsi = gsi_after_labels (e->dest);
2102 add_eh_edge (e->src, eh_edge);
2103 }
2104 if (tree_fits_uhwi_p (idx))
2105 {
2106 tree atype = limb_access_type (rhs_type, idx);
2107 if (!useless_type_conversion_p (atype, TREE_TYPE (rhs1)))
2108 ret = add_cast (atype, ret);
2109 }
2110 }
2111 return ret;
2112 }
2113
2114 /* Return a limb IDX from a mergeable statement STMT. */
2115
2116 tree
2117 bitint_large_huge::handle_stmt (gimple *stmt, tree idx)
2118 {
2119 tree lhs, rhs1, rhs2 = NULL_TREE;
2120 gimple *g;
2121 switch (gimple_code (stmt))
2122 {
2123 case GIMPLE_ASSIGN:
2124 if (gimple_assign_load_p (stmt))
2125 return handle_load (stmt, idx);
2126 switch (gimple_assign_rhs_code (stmt))
2127 {
2128 case BIT_AND_EXPR:
2129 case BIT_IOR_EXPR:
2130 case BIT_XOR_EXPR:
2131 rhs2 = handle_operand (gimple_assign_rhs2 (stmt), idx);
2132 /* FALLTHRU */
2133 case BIT_NOT_EXPR:
2134 rhs1 = handle_operand (gimple_assign_rhs1 (stmt), idx);
2135 lhs = make_ssa_name (TREE_TYPE (rhs1));
2136 g = gimple_build_assign (lhs, gimple_assign_rhs_code (stmt),
2137 rhs1, rhs2);
2138 insert_before (g);
2139 return lhs;
2140 case PLUS_EXPR:
2141 case MINUS_EXPR:
2142 rhs1 = handle_operand (gimple_assign_rhs1 (stmt), idx);
2143 rhs2 = handle_operand (gimple_assign_rhs2 (stmt), idx);
2144 return handle_plus_minus (gimple_assign_rhs_code (stmt),
2145 rhs1, rhs2, idx);
2146 case NEGATE_EXPR:
2147 rhs2 = handle_operand (gimple_assign_rhs1 (stmt), idx);
2148 rhs1 = build_zero_cst (TREE_TYPE (rhs2));
2149 return handle_plus_minus (MINUS_EXPR, rhs1, rhs2, idx);
2150 case LSHIFT_EXPR:
2151 return handle_lshift (handle_operand (gimple_assign_rhs1 (stmt),
2152 idx),
2153 gimple_assign_rhs2 (stmt), idx);
2154 case SSA_NAME:
2155 case PAREN_EXPR:
2156 case INTEGER_CST:
2157 return handle_operand (gimple_assign_rhs1 (stmt), idx);
2158 CASE_CONVERT:
2159 return handle_cast (TREE_TYPE (gimple_assign_lhs (stmt)),
2160 gimple_assign_rhs1 (stmt), idx);
2161 case VIEW_CONVERT_EXPR:
2162 return handle_cast (TREE_TYPE (gimple_assign_lhs (stmt)),
2163 TREE_OPERAND (gimple_assign_rhs1 (stmt), 0),
2164 idx);
2165 case BIT_FIELD_REF:
2166 return handle_bit_field_ref (gimple_assign_rhs1 (stmt), idx);
2167 default:
2168 break;
2169 }
2170 break;
2171 default:
2172 break;
2173 }
2174 gcc_unreachable ();
2175 }
2176
2177 /* Return minimum precision of OP at STMT.
2178 Positive value is minimum precision above which all bits
2179 are zero, negative means all bits above negation of the
2180 value are copies of the sign bit. */
2181
2182 static int
2183 range_to_prec (tree op, gimple *stmt)
2184 {
2185 int_range_max r;
2186 wide_int w;
2187 tree type = TREE_TYPE (op);
2188 unsigned int prec = TYPE_PRECISION (type);
2189
2190 if (!optimize
2191 || !get_range_query (cfun)->range_of_expr (r, op, stmt)
2192 || r.undefined_p ())
2193 {
2194 if (TYPE_UNSIGNED (type))
2195 return prec;
2196 else
2197 return MIN ((int) -prec, -2);
2198 }
2199
2200 if (!TYPE_UNSIGNED (TREE_TYPE (op)))
2201 {
2202 w = r.lower_bound ();
2203 if (wi::neg_p (w))
2204 {
2205 int min_prec1 = wi::min_precision (w, SIGNED);
2206 w = r.upper_bound ();
2207 int min_prec2 = wi::min_precision (w, SIGNED);
2208 int min_prec = MAX (min_prec1, min_prec2);
2209 return MIN (-min_prec, -2);
2210 }
2211 }
2212
2213 w = r.upper_bound ();
2214 int min_prec = wi::min_precision (w, UNSIGNED);
2215 return MAX (min_prec, 1);
2216 }
2217
2218 /* Return address of the first limb of OP and write into *PREC
2219 its precision. If positive, the operand is zero extended
2220 from that precision, if it is negative, the operand is sign-extended
2221 from -*PREC. If PREC_STORED is NULL, it is the toplevel call,
2222 otherwise *PREC_STORED is prec from the innermost call without
2223 range optimizations (0 for uninitialized SSA_NAME). */
2224
2225 tree
2226 bitint_large_huge::handle_operand_addr (tree op, gimple *stmt,
2227 int *prec_stored, int *prec)
2228 {
2229 wide_int w;
2230 location_t loc_save = m_loc;
2231 if ((TREE_CODE (TREE_TYPE (op)) != BITINT_TYPE
2232 || bitint_precision_kind (TREE_TYPE (op)) < bitint_prec_large)
2233 && TREE_CODE (op) != INTEGER_CST)
2234 {
2235 do_int:
2236 *prec = range_to_prec (op, stmt);
2237 bitint_prec_kind kind = bitint_prec_small;
2238 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (op)));
2239 if (TREE_CODE (TREE_TYPE (op)) == BITINT_TYPE)
2240 kind = bitint_precision_kind (TREE_TYPE (op));
2241 if (kind == bitint_prec_middle)
2242 {
2243 tree type = NULL_TREE;
2244 op = maybe_cast_middle_bitint (&m_gsi, op, type);
2245 }
2246 tree op_type = TREE_TYPE (op);
2247 unsigned HOST_WIDE_INT nelts
2248 = CEIL (TYPE_PRECISION (op_type), limb_prec);
2249 /* Add support for 3 or more limbs filled in from normal
2250 integral type if this assert fails. If no target chooses
2251 limb mode smaller than half of largest supported normal
2252 integral type, this will not be needed. */
2253 gcc_assert (nelts <= 2);
2254 if (prec_stored)
2255 *prec_stored = (TYPE_UNSIGNED (op_type)
2256 ? TYPE_PRECISION (op_type)
2257 : -TYPE_PRECISION (op_type));
2258 if (*prec <= limb_prec && *prec >= -limb_prec)
2259 {
2260 nelts = 1;
2261 if (prec_stored)
2262 {
2263 if (TYPE_UNSIGNED (op_type))
2264 {
2265 if (*prec_stored > limb_prec)
2266 *prec_stored = limb_prec;
2267 }
2268 else if (*prec_stored < -limb_prec)
2269 *prec_stored = -limb_prec;
2270 }
2271 }
2272 tree atype = build_array_type_nelts (m_limb_type, nelts);
2273 tree var = create_tmp_var (atype);
2274 tree t1 = op;
2275 if (!useless_type_conversion_p (m_limb_type, op_type))
2276 t1 = add_cast (m_limb_type, t1);
2277 tree v = build4 (ARRAY_REF, m_limb_type, var, size_zero_node,
2278 NULL_TREE, NULL_TREE);
2279 gimple *g = gimple_build_assign (v, t1);
2280 insert_before (g);
2281 if (nelts > 1)
2282 {
2283 tree lp = build_int_cst (unsigned_type_node, limb_prec);
2284 g = gimple_build_assign (make_ssa_name (op_type),
2285 RSHIFT_EXPR, op, lp);
2286 insert_before (g);
2287 tree t2 = gimple_assign_lhs (g);
2288 t2 = add_cast (m_limb_type, t2);
2289 v = build4 (ARRAY_REF, m_limb_type, var, size_one_node,
2290 NULL_TREE, NULL_TREE);
2291 g = gimple_build_assign (v, t2);
2292 insert_before (g);
2293 }
2294 tree ret = build_fold_addr_expr (var);
2295 if (!stmt_ends_bb_p (gsi_stmt (m_gsi)))
2296 {
2297 tree clobber = build_clobber (atype, CLOBBER_STORAGE_END);
2298 g = gimple_build_assign (var, clobber);
2299 gsi_insert_after (&m_gsi, g, GSI_SAME_STMT);
2300 }
2301 m_loc = loc_save;
2302 return ret;
2303 }
2304 switch (TREE_CODE (op))
2305 {
2306 case SSA_NAME:
2307 if (m_names == NULL
2308 || !bitmap_bit_p (m_names, SSA_NAME_VERSION (op)))
2309 {
2310 gimple *g = SSA_NAME_DEF_STMT (op);
2311 tree ret;
2312 m_loc = gimple_location (g);
2313 if (gimple_assign_load_p (g))
2314 {
2315 *prec = range_to_prec (op, NULL);
2316 if (prec_stored)
2317 *prec_stored = (TYPE_UNSIGNED (TREE_TYPE (op))
2318 ? TYPE_PRECISION (TREE_TYPE (op))
2319 : -TYPE_PRECISION (TREE_TYPE (op)));
2320 ret = build_fold_addr_expr (gimple_assign_rhs1 (g));
2321 ret = force_gimple_operand_gsi (&m_gsi, ret, true,
2322 NULL_TREE, true, GSI_SAME_STMT);
2323 }
2324 else if (gimple_code (g) == GIMPLE_NOP)
2325 {
2326 *prec = TYPE_UNSIGNED (TREE_TYPE (op)) ? limb_prec : -limb_prec;
2327 if (prec_stored)
2328 *prec_stored = 0;
2329 tree var = create_tmp_var (m_limb_type);
2330 TREE_ADDRESSABLE (var) = 1;
2331 ret = build_fold_addr_expr (var);
2332 if (!stmt_ends_bb_p (gsi_stmt (m_gsi)))
2333 {
2334 tree clobber = build_clobber (m_limb_type,
2335 CLOBBER_STORAGE_END);
2336 g = gimple_build_assign (var, clobber);
2337 gsi_insert_after (&m_gsi, g, GSI_SAME_STMT);
2338 }
2339 }
2340 else
2341 {
2342 gcc_assert (gimple_assign_cast_p (g));
2343 tree rhs1 = gimple_assign_rhs1 (g);
2344 bitint_prec_kind kind = bitint_prec_small;
2345 if (TREE_CODE (rhs1) == VIEW_CONVERT_EXPR)
2346 rhs1 = TREE_OPERAND (rhs1, 0);
2347 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (rhs1)));
2348 if (TREE_CODE (TREE_TYPE (rhs1)) == BITINT_TYPE)
2349 kind = bitint_precision_kind (TREE_TYPE (rhs1));
2350 if (kind >= bitint_prec_large)
2351 {
2352 tree lhs_type = TREE_TYPE (op);
2353 tree rhs_type = TREE_TYPE (rhs1);
2354 int prec_stored_val = 0;
2355 ret = handle_operand_addr (rhs1, g, &prec_stored_val, prec);
2356 if (prec_stored)
2357 *prec_stored = prec_stored_val;
2358 if (TYPE_PRECISION (lhs_type) > TYPE_PRECISION (rhs_type))
2359 {
2360 if (TYPE_UNSIGNED (lhs_type)
2361 && !TYPE_UNSIGNED (rhs_type))
2362 gcc_assert (*prec >= 0 || prec_stored == NULL);
2363 }
2364 else
2365 {
2366 if (prec_stored_val == 0)
2367 /* Non-widening cast of uninitialized value. */;
2368 else if (*prec > 0 && *prec < TYPE_PRECISION (lhs_type))
2369 ;
2370 else if (TYPE_UNSIGNED (lhs_type))
2371 {
2372 gcc_assert (*prec > 0
2373 || prec_stored_val > 0
2374 || (-prec_stored_val
2375 >= TYPE_PRECISION (lhs_type)));
2376 *prec = TYPE_PRECISION (lhs_type);
2377 }
2378 else if (*prec < 0 && -*prec < TYPE_PRECISION (lhs_type))
2379 ;
2380 else
2381 *prec = -TYPE_PRECISION (lhs_type);
2382 }
2383 }
2384 else
2385 {
2386 op = rhs1;
2387 stmt = g;
2388 goto do_int;
2389 }
2390 }
2391 m_loc = loc_save;
2392 return ret;
2393 }
2394 else
2395 {
2396 int p = var_to_partition (m_map, op);
2397 gcc_assert (m_vars[p] != NULL_TREE);
2398 *prec = range_to_prec (op, stmt);
2399 if (prec_stored)
2400 *prec_stored = (TYPE_UNSIGNED (TREE_TYPE (op))
2401 ? TYPE_PRECISION (TREE_TYPE (op))
2402 : -TYPE_PRECISION (TREE_TYPE (op)));
2403 return build_fold_addr_expr (m_vars[p]);
2404 }
2405 case INTEGER_CST:
2406 unsigned int min_prec, mp;
2407 tree type;
2408 w = wi::to_wide (op);
2409 if (tree_int_cst_sgn (op) >= 0)
2410 {
2411 min_prec = wi::min_precision (w, UNSIGNED);
2412 *prec = MAX (min_prec, 1);
2413 }
2414 else
2415 {
2416 min_prec = wi::min_precision (w, SIGNED);
2417 *prec = MIN ((int) -min_prec, -2);
2418 }
2419 mp = CEIL (min_prec, limb_prec) * limb_prec;
2420 if (mp == 0)
2421 mp = 1;
2422 if (mp >= (unsigned) TYPE_PRECISION (TREE_TYPE (op))
2423 && (TREE_CODE (TREE_TYPE (op)) == BITINT_TYPE
2424 || TYPE_PRECISION (TREE_TYPE (op)) <= limb_prec))
2425 type = TREE_TYPE (op);
2426 else
2427 type = build_bitint_type (mp, 1);
2428 if (TREE_CODE (type) != BITINT_TYPE
2429 || bitint_precision_kind (type) == bitint_prec_small)
2430 {
2431 if (TYPE_PRECISION (type) <= limb_prec)
2432 type = m_limb_type;
2433 else
2434 {
2435 while (bitint_precision_kind (mp) == bitint_prec_small)
2436 mp += limb_prec;
2437 /* This case is for targets which e.g. have 64-bit
2438 limb but categorize up to 128-bits _BitInts as
2439 small. We could use type of m_limb_type[2] and
2440 similar instead to save space. */
2441 type = build_bitint_type (mp, 1);
2442 }
2443 }
2444 if (prec_stored)
2445 {
2446 if (tree_int_cst_sgn (op) >= 0)
2447 *prec_stored = MAX (TYPE_PRECISION (type), 1);
2448 else
2449 *prec_stored = MIN ((int) -TYPE_PRECISION (type), -2);
2450 }
2451 op = tree_output_constant_def (fold_convert (type, op));
2452 return build_fold_addr_expr (op);
2453 default:
2454 gcc_unreachable ();
2455 }
2456 }
2457
2458 /* Helper function, create a loop before the current location,
2459 start with sizetype INIT value from the preheader edge. Return
2460 a PHI result and set *IDX_NEXT to SSA_NAME it creates and uses
2461 from the latch edge. */
2462
2463 tree
2464 bitint_large_huge::create_loop (tree init, tree *idx_next)
2465 {
2466 if (!gsi_end_p (m_gsi))
2467 gsi_prev (&m_gsi);
2468 else
2469 m_gsi = gsi_last_bb (gsi_bb (m_gsi));
2470 edge e1 = split_block (gsi_bb (m_gsi), gsi_stmt (m_gsi));
2471 edge e2 = split_block (e1->dest, (gimple *) NULL);
2472 edge e3 = make_edge (e1->dest, e1->dest, EDGE_TRUE_VALUE);
2473 e3->probability = profile_probability::very_unlikely ();
2474 e2->flags = EDGE_FALSE_VALUE;
2475 e2->probability = e3->probability.invert ();
2476 tree idx = make_ssa_name (sizetype);
2477 gphi *phi = create_phi_node (idx, e1->dest);
2478 add_phi_arg (phi, init, e1, UNKNOWN_LOCATION);
2479 *idx_next = make_ssa_name (sizetype);
2480 add_phi_arg (phi, *idx_next, e3, UNKNOWN_LOCATION);
2481 m_gsi = gsi_after_labels (e1->dest);
2482 m_bb = e1->dest;
2483 m_preheader_bb = e1->src;
2484 class loop *loop = alloc_loop ();
2485 loop->header = e1->dest;
2486 add_loop (loop, e1->src->loop_father);
2487 return idx;
2488 }
2489
2490 /* Lower large/huge _BitInt statement mergeable or similar STMT which can be
2491 lowered using iteration from the least significant limb up to the most
2492 significant limb. For large _BitInt it is emitted as straight line code
2493 before current location, for huge _BitInt as a loop handling two limbs
2494 at once, followed by handling up to limbs in straight line code (at most
2495 one full and one partial limb). It can also handle EQ_EXPR/NE_EXPR
2496 comparisons, in that case CMP_CODE should be the comparison code and
2497 CMP_OP1/CMP_OP2 the comparison operands. */
2498
2499 tree
2500 bitint_large_huge::lower_mergeable_stmt (gimple *stmt, tree_code &cmp_code,
2501 tree cmp_op1, tree cmp_op2)
2502 {
2503 bool eq_p = cmp_code != ERROR_MARK;
2504 tree type;
2505 if (eq_p)
2506 type = TREE_TYPE (cmp_op1);
2507 else
2508 type = TREE_TYPE (gimple_assign_lhs (stmt));
2509 gcc_assert (TREE_CODE (type) == BITINT_TYPE);
2510 bitint_prec_kind kind = bitint_precision_kind (type);
2511 gcc_assert (kind >= bitint_prec_large);
2512 gimple *g;
2513 tree lhs = gimple_get_lhs (stmt);
2514 tree rhs1, lhs_type = lhs ? TREE_TYPE (lhs) : NULL_TREE;
2515 if (lhs
2516 && TREE_CODE (lhs) == SSA_NAME
2517 && TREE_CODE (TREE_TYPE (lhs)) == BITINT_TYPE
2518 && bitint_precision_kind (TREE_TYPE (lhs)) >= bitint_prec_large)
2519 {
2520 int p = var_to_partition (m_map, lhs);
2521 gcc_assert (m_vars[p] != NULL_TREE);
2522 m_lhs = lhs = m_vars[p];
2523 }
2524 unsigned cnt, rem = 0, end = 0, prec = TYPE_PRECISION (type);
2525 bool sext = false;
2526 tree ext = NULL_TREE, store_operand = NULL_TREE;
2527 bool eh = false;
2528 basic_block eh_pad = NULL;
2529 tree nlhs = NULL_TREE;
2530 unsigned HOST_WIDE_INT bo_idx = 0;
2531 unsigned HOST_WIDE_INT bo_bit = 0;
2532 tree bf_cur = NULL_TREE, bf_next = NULL_TREE;
2533 if (gimple_store_p (stmt))
2534 {
2535 store_operand = gimple_assign_rhs1 (stmt);
2536 eh = stmt_ends_bb_p (stmt);
2537 if (eh)
2538 {
2539 edge e;
2540 edge_iterator ei;
2541 basic_block bb = gimple_bb (stmt);
2542
2543 FOR_EACH_EDGE (e, ei, bb->succs)
2544 if (e->flags & EDGE_EH)
2545 {
2546 eh_pad = e->dest;
2547 break;
2548 }
2549 }
2550 if (TREE_CODE (lhs) == COMPONENT_REF
2551 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (lhs, 1)))
2552 {
2553 tree fld = TREE_OPERAND (lhs, 1);
2554 gcc_assert (tree_fits_uhwi_p (DECL_FIELD_BIT_OFFSET (fld)));
2555 tree repr = DECL_BIT_FIELD_REPRESENTATIVE (fld);
2556 poly_int64 bitoffset;
2557 poly_uint64 field_offset, repr_offset;
2558 if ((tree_to_uhwi (DECL_FIELD_BIT_OFFSET (fld)) % BITS_PER_UNIT) == 0)
2559 nlhs = lhs;
2560 else
2561 {
2562 bool var_field_off = false;
2563 if (poly_int_tree_p (DECL_FIELD_OFFSET (fld), &field_offset)
2564 && poly_int_tree_p (DECL_FIELD_OFFSET (repr), &repr_offset))
2565 bitoffset = (field_offset - repr_offset) * BITS_PER_UNIT;
2566 else
2567 {
2568 bitoffset = 0;
2569 var_field_off = true;
2570 }
2571 bitoffset += (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (fld))
2572 - tree_to_uhwi (DECL_FIELD_BIT_OFFSET (repr)));
2573 nlhs = build3 (COMPONENT_REF, TREE_TYPE (repr),
2574 TREE_OPERAND (lhs, 0), repr,
2575 var_field_off
2576 ? TREE_OPERAND (lhs, 2) : NULL_TREE);
2577 HOST_WIDE_INT bo = bitoffset.to_constant ();
2578 bo_idx = (unsigned HOST_WIDE_INT) bo / limb_prec;
2579 bo_bit = (unsigned HOST_WIDE_INT) bo % limb_prec;
2580 }
2581 }
2582 }
2583 if ((store_operand
2584 && TREE_CODE (store_operand) == SSA_NAME
2585 && (m_names == NULL
2586 || !bitmap_bit_p (m_names, SSA_NAME_VERSION (store_operand)))
2587 && gimple_assign_cast_p (SSA_NAME_DEF_STMT (store_operand)))
2588 || gimple_assign_cast_p (stmt))
2589 {
2590 rhs1 = gimple_assign_rhs1 (store_operand
2591 ? SSA_NAME_DEF_STMT (store_operand)
2592 : stmt);
2593 if (TREE_CODE (rhs1) == VIEW_CONVERT_EXPR)
2594 rhs1 = TREE_OPERAND (rhs1, 0);
2595 /* Optimize mergeable ops ending with widening cast to _BitInt
2596 (or followed by store). We can lower just the limbs of the
2597 cast operand and widen afterwards. */
2598 if (TREE_CODE (rhs1) == SSA_NAME
2599 && (m_names == NULL
2600 || !bitmap_bit_p (m_names, SSA_NAME_VERSION (rhs1)))
2601 && TREE_CODE (TREE_TYPE (rhs1)) == BITINT_TYPE
2602 && bitint_precision_kind (TREE_TYPE (rhs1)) >= bitint_prec_large
2603 && (CEIL ((unsigned) TYPE_PRECISION (TREE_TYPE (rhs1)),
2604 limb_prec) < CEIL (prec, limb_prec)
2605 || (kind == bitint_prec_huge
2606 && TYPE_PRECISION (TREE_TYPE (rhs1)) < prec)))
2607 {
2608 store_operand = rhs1;
2609 prec = TYPE_PRECISION (TREE_TYPE (rhs1));
2610 kind = bitint_precision_kind (TREE_TYPE (rhs1));
2611 if (!TYPE_UNSIGNED (TREE_TYPE (rhs1)))
2612 sext = true;
2613 }
2614 }
2615 tree idx = NULL_TREE, idx_first = NULL_TREE, idx_next = NULL_TREE;
2616 if (kind == bitint_prec_large)
2617 cnt = CEIL (prec, limb_prec);
2618 else
2619 {
2620 rem = (prec % (2 * limb_prec));
2621 end = (prec - rem) / limb_prec;
2622 cnt = 2 + CEIL (rem, limb_prec);
2623 idx = idx_first = create_loop (size_zero_node, &idx_next);
2624 }
2625
2626 basic_block edge_bb = NULL;
2627 if (eq_p)
2628 {
2629 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
2630 gsi_prev (&gsi);
2631 edge e = split_block (gsi_bb (gsi), gsi_stmt (gsi));
2632 edge_bb = e->src;
2633 if (kind == bitint_prec_large)
2634 m_gsi = gsi_end_bb (edge_bb);
2635 }
2636 else
2637 m_after_stmt = stmt;
2638 if (kind != bitint_prec_large)
2639 m_upwards_2limb = end;
2640 m_upwards = true;
2641
2642 bool separate_ext
2643 = (prec != (unsigned) TYPE_PRECISION (type)
2644 && (CEIL ((unsigned) TYPE_PRECISION (type), limb_prec)
2645 > CEIL (prec, limb_prec)));
2646
2647 for (unsigned i = 0; i < cnt; i++)
2648 {
2649 m_data_cnt = 0;
2650 if (kind == bitint_prec_large)
2651 idx = size_int (i);
2652 else if (i >= 2)
2653 idx = size_int (end + (i > 2));
2654 if (eq_p)
2655 {
2656 rhs1 = handle_operand (cmp_op1, idx);
2657 tree rhs2 = handle_operand (cmp_op2, idx);
2658 g = gimple_build_cond (NE_EXPR, rhs1, rhs2, NULL_TREE, NULL_TREE);
2659 insert_before (g);
2660 edge e1 = split_block (gsi_bb (m_gsi), g);
2661 e1->flags = EDGE_FALSE_VALUE;
2662 edge e2 = make_edge (e1->src, gimple_bb (stmt), EDGE_TRUE_VALUE);
2663 e1->probability = profile_probability::unlikely ();
2664 e2->probability = e1->probability.invert ();
2665 if (i == 0)
2666 set_immediate_dominator (CDI_DOMINATORS, e2->dest, e2->src);
2667 m_gsi = gsi_after_labels (e1->dest);
2668 }
2669 else
2670 {
2671 if (store_operand)
2672 rhs1 = handle_operand (store_operand, idx);
2673 else
2674 rhs1 = handle_stmt (stmt, idx);
2675 if (!useless_type_conversion_p (m_limb_type, TREE_TYPE (rhs1)))
2676 rhs1 = add_cast (m_limb_type, rhs1);
2677 if (sext && i == cnt - 1)
2678 ext = rhs1;
2679 tree nidx = idx;
2680 if (bo_idx)
2681 {
2682 if (tree_fits_uhwi_p (idx))
2683 nidx = size_int (tree_to_uhwi (idx) + bo_idx);
2684 else
2685 {
2686 nidx = make_ssa_name (sizetype);
2687 g = gimple_build_assign (nidx, PLUS_EXPR, idx,
2688 size_int (bo_idx));
2689 insert_before (g);
2690 }
2691 }
2692 bool done = false;
2693 basic_block new_bb = NULL;
2694 /* Handle stores into bit-fields. */
2695 if (bo_bit)
2696 {
2697 if (i == 0)
2698 {
2699 edge e2 = NULL;
2700 if (kind != bitint_prec_large)
2701 {
2702 prepare_data_in_out (build_zero_cst (m_limb_type),
2703 idx, &bf_next);
2704 bf_next = m_data.pop ();
2705 bf_cur = m_data.pop ();
2706 g = gimple_build_cond (EQ_EXPR, idx, size_zero_node,
2707 NULL_TREE, NULL_TREE);
2708 edge edge_true;
2709 if_then_else (g, profile_probability::unlikely (),
2710 edge_true, e2);
2711 new_bb = e2->dest;
2712 }
2713 tree ftype
2714 = build_nonstandard_integer_type (limb_prec - bo_bit, 1);
2715 tree bfr = build_bit_field_ref (ftype, unshare_expr (nlhs),
2716 limb_prec - bo_bit,
2717 bo_idx * limb_prec + bo_bit);
2718 tree t = add_cast (ftype, rhs1);
2719 g = gimple_build_assign (bfr, t);
2720 insert_before (g);
2721 if (eh)
2722 {
2723 maybe_duplicate_eh_stmt (g, stmt);
2724 if (eh_pad)
2725 {
2726 edge e = split_block (gsi_bb (m_gsi), g);
2727 m_gsi = gsi_after_labels (e->dest);
2728 add_eh_edge (e->src,
2729 find_edge (gimple_bb (stmt), eh_pad));
2730 }
2731 }
2732 if (kind == bitint_prec_large)
2733 {
2734 bf_cur = rhs1;
2735 done = true;
2736 }
2737 else if (e2)
2738 m_gsi = gsi_after_labels (e2->src);
2739 }
2740 if (!done)
2741 {
2742 tree t1 = make_ssa_name (m_limb_type);
2743 tree t2 = make_ssa_name (m_limb_type);
2744 tree t3 = make_ssa_name (m_limb_type);
2745 g = gimple_build_assign (t1, RSHIFT_EXPR, bf_cur,
2746 build_int_cst (unsigned_type_node,
2747 limb_prec - bo_bit));
2748 insert_before (g);
2749 g = gimple_build_assign (t2, LSHIFT_EXPR, rhs1,
2750 build_int_cst (unsigned_type_node,
2751 bo_bit));
2752 insert_before (g);
2753 bf_cur = rhs1;
2754 g = gimple_build_assign (t3, BIT_IOR_EXPR, t1, t2);
2755 insert_before (g);
2756 rhs1 = t3;
2757 if (bf_next && i == 1)
2758 {
2759 g = gimple_build_assign (bf_next, bf_cur);
2760 insert_before (g);
2761 }
2762 }
2763 }
2764 if (!done)
2765 {
2766 /* Handle bit-field access to partial last limb if needed. */
2767 if (nlhs
2768 && i == cnt - 1
2769 && !separate_ext
2770 && tree_fits_uhwi_p (idx))
2771 {
2772 unsigned int tprec = TYPE_PRECISION (type);
2773 unsigned int rprec = (tprec - 1) % limb_prec + 1;
2774 if (rprec + bo_bit < (unsigned) limb_prec)
2775 {
2776 tree ftype
2777 = build_nonstandard_integer_type (rprec + bo_bit, 1);
2778 tree bfr
2779 = build_bit_field_ref (ftype, unshare_expr (nlhs),
2780 rprec + bo_bit,
2781 (bo_idx + tprec / limb_prec)
2782 * limb_prec);
2783 tree t = add_cast (ftype, rhs1);
2784 g = gimple_build_assign (bfr, t);
2785 done = true;
2786 bf_cur = NULL_TREE;
2787 }
2788 else if (rprec + bo_bit == (unsigned) limb_prec)
2789 bf_cur = NULL_TREE;
2790 }
2791 /* Otherwise, stores to any other lhs. */
2792 if (!done)
2793 {
2794 tree l = limb_access (nlhs ? NULL_TREE : lhs_type,
2795 nlhs ? nlhs : lhs, nidx, true);
2796 g = gimple_build_assign (l, rhs1);
2797 }
2798 insert_before (g);
2799 if (eh)
2800 {
2801 maybe_duplicate_eh_stmt (g, stmt);
2802 if (eh_pad)
2803 {
2804 edge e = split_block (gsi_bb (m_gsi), g);
2805 m_gsi = gsi_after_labels (e->dest);
2806 add_eh_edge (e->src,
2807 find_edge (gimple_bb (stmt), eh_pad));
2808 }
2809 }
2810 if (new_bb)
2811 m_gsi = gsi_after_labels (new_bb);
2812 }
2813 }
2814 m_first = false;
2815 if (kind == bitint_prec_huge && i <= 1)
2816 {
2817 if (i == 0)
2818 {
2819 idx = make_ssa_name (sizetype);
2820 g = gimple_build_assign (idx, PLUS_EXPR, idx_first,
2821 size_one_node);
2822 insert_before (g);
2823 }
2824 else
2825 {
2826 g = gimple_build_assign (idx_next, PLUS_EXPR, idx_first,
2827 size_int (2));
2828 insert_before (g);
2829 g = gimple_build_cond (NE_EXPR, idx_next, size_int (end),
2830 NULL_TREE, NULL_TREE);
2831 insert_before (g);
2832 if (eq_p)
2833 m_gsi = gsi_after_labels (edge_bb);
2834 else
2835 m_gsi = gsi_for_stmt (stmt);
2836 m_bb = NULL;
2837 }
2838 }
2839 }
2840
2841 if (separate_ext)
2842 {
2843 if (sext)
2844 {
2845 ext = add_cast (signed_type_for (m_limb_type), ext);
2846 tree lpm1 = build_int_cst (unsigned_type_node,
2847 limb_prec - 1);
2848 tree n = make_ssa_name (TREE_TYPE (ext));
2849 g = gimple_build_assign (n, RSHIFT_EXPR, ext, lpm1);
2850 insert_before (g);
2851 ext = add_cast (m_limb_type, n);
2852 }
2853 else
2854 ext = build_zero_cst (m_limb_type);
2855 kind = bitint_precision_kind (type);
2856 unsigned start = CEIL (prec, limb_prec);
2857 prec = TYPE_PRECISION (type);
2858 idx = idx_first = idx_next = NULL_TREE;
2859 if (prec <= (start + 2 + (bo_bit != 0)) * limb_prec)
2860 kind = bitint_prec_large;
2861 if (kind == bitint_prec_large)
2862 cnt = CEIL (prec, limb_prec) - start;
2863 else
2864 {
2865 rem = prec % limb_prec;
2866 end = (prec - rem) / limb_prec;
2867 cnt = (bo_bit != 0) + 1 + (rem != 0);
2868 }
2869 for (unsigned i = 0; i < cnt; i++)
2870 {
2871 if (kind == bitint_prec_large || (i == 0 && bo_bit != 0))
2872 idx = size_int (start + i);
2873 else if (i == cnt - 1 && (rem != 0))
2874 idx = size_int (end);
2875 else if (i == (bo_bit != 0))
2876 idx = create_loop (size_int (start + i), &idx_next);
2877 rhs1 = ext;
2878 if (bf_cur != NULL_TREE && bf_cur != ext)
2879 {
2880 tree t1 = make_ssa_name (m_limb_type);
2881 g = gimple_build_assign (t1, RSHIFT_EXPR, bf_cur,
2882 build_int_cst (unsigned_type_node,
2883 limb_prec - bo_bit));
2884 insert_before (g);
2885 if (integer_zerop (ext))
2886 rhs1 = t1;
2887 else
2888 {
2889 tree t2 = make_ssa_name (m_limb_type);
2890 rhs1 = make_ssa_name (m_limb_type);
2891 g = gimple_build_assign (t2, LSHIFT_EXPR, ext,
2892 build_int_cst (unsigned_type_node,
2893 bo_bit));
2894 insert_before (g);
2895 g = gimple_build_assign (rhs1, BIT_IOR_EXPR, t1, t2);
2896 insert_before (g);
2897 }
2898 bf_cur = ext;
2899 }
2900 tree nidx = idx;
2901 if (bo_idx)
2902 {
2903 if (tree_fits_uhwi_p (idx))
2904 nidx = size_int (tree_to_uhwi (idx) + bo_idx);
2905 else
2906 {
2907 nidx = make_ssa_name (sizetype);
2908 g = gimple_build_assign (nidx, PLUS_EXPR, idx,
2909 size_int (bo_idx));
2910 insert_before (g);
2911 }
2912 }
2913 bool done = false;
2914 /* Handle bit-field access to partial last limb if needed. */
2915 if (nlhs && i == cnt - 1)
2916 {
2917 unsigned int tprec = TYPE_PRECISION (type);
2918 unsigned int rprec = (tprec - 1) % limb_prec + 1;
2919 if (rprec + bo_bit < (unsigned) limb_prec)
2920 {
2921 tree ftype
2922 = build_nonstandard_integer_type (rprec + bo_bit, 1);
2923 tree bfr
2924 = build_bit_field_ref (ftype, unshare_expr (nlhs),
2925 rprec + bo_bit,
2926 (bo_idx + tprec / limb_prec)
2927 * limb_prec);
2928 tree t = add_cast (ftype, rhs1);
2929 g = gimple_build_assign (bfr, t);
2930 done = true;
2931 bf_cur = NULL_TREE;
2932 }
2933 else if (rprec + bo_bit == (unsigned) limb_prec)
2934 bf_cur = NULL_TREE;
2935 }
2936 /* Otherwise, stores to any other lhs. */
2937 if (!done)
2938 {
2939 tree l = limb_access (nlhs ? NULL_TREE : lhs_type,
2940 nlhs ? nlhs : lhs, nidx, true);
2941 g = gimple_build_assign (l, rhs1);
2942 }
2943 insert_before (g);
2944 if (eh)
2945 {
2946 maybe_duplicate_eh_stmt (g, stmt);
2947 if (eh_pad)
2948 {
2949 edge e = split_block (gsi_bb (m_gsi), g);
2950 m_gsi = gsi_after_labels (e->dest);
2951 add_eh_edge (e->src, find_edge (gimple_bb (stmt), eh_pad));
2952 }
2953 }
2954 if (kind == bitint_prec_huge && i == (bo_bit != 0))
2955 {
2956 g = gimple_build_assign (idx_next, PLUS_EXPR, idx,
2957 size_one_node);
2958 insert_before (g);
2959 g = gimple_build_cond (NE_EXPR, idx_next, size_int (end),
2960 NULL_TREE, NULL_TREE);
2961 insert_before (g);
2962 m_gsi = gsi_for_stmt (stmt);
2963 m_bb = NULL;
2964 }
2965 }
2966 }
2967 if (bf_cur != NULL_TREE)
2968 {
2969 unsigned int tprec = TYPE_PRECISION (type);
2970 unsigned int rprec = (tprec + bo_bit) % limb_prec;
2971 tree ftype = build_nonstandard_integer_type (rprec, 1);
2972 tree bfr = build_bit_field_ref (ftype, unshare_expr (nlhs),
2973 rprec,
2974 (bo_idx + (tprec + bo_bit) / limb_prec)
2975 * limb_prec);
2976 rhs1 = bf_cur;
2977 if (bf_cur != ext)
2978 {
2979 rhs1 = make_ssa_name (TREE_TYPE (rhs1));
2980 g = gimple_build_assign (rhs1, RSHIFT_EXPR, bf_cur,
2981 build_int_cst (unsigned_type_node,
2982 limb_prec - bo_bit));
2983 insert_before (g);
2984 }
2985 rhs1 = add_cast (ftype, rhs1);
2986 g = gimple_build_assign (bfr, rhs1);
2987 insert_before (g);
2988 if (eh)
2989 {
2990 maybe_duplicate_eh_stmt (g, stmt);
2991 if (eh_pad)
2992 {
2993 edge e = split_block (gsi_bb (m_gsi), g);
2994 m_gsi = gsi_after_labels (e->dest);
2995 add_eh_edge (e->src, find_edge (gimple_bb (stmt), eh_pad));
2996 }
2997 }
2998 }
2999
3000 if (gimple_store_p (stmt))
3001 {
3002 unlink_stmt_vdef (stmt);
3003 release_ssa_name (gimple_vdef (stmt));
3004 gsi_remove (&m_gsi, true);
3005 }
3006 if (eq_p)
3007 {
3008 lhs = make_ssa_name (boolean_type_node);
3009 basic_block bb = gimple_bb (stmt);
3010 gphi *phi = create_phi_node (lhs, bb);
3011 edge e = find_edge (gsi_bb (m_gsi), bb);
3012 unsigned int n = EDGE_COUNT (bb->preds);
3013 for (unsigned int i = 0; i < n; i++)
3014 {
3015 edge e2 = EDGE_PRED (bb, i);
3016 add_phi_arg (phi, e == e2 ? boolean_true_node : boolean_false_node,
3017 e2, UNKNOWN_LOCATION);
3018 }
3019 cmp_code = cmp_code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
3020 return lhs;
3021 }
3022 else
3023 return NULL_TREE;
3024 }
3025
3026 /* Handle a large/huge _BitInt comparison statement STMT other than
3027 EQ_EXPR/NE_EXPR. CMP_CODE, CMP_OP1 and CMP_OP2 meaning is like in
3028 lower_mergeable_stmt. The {GT,GE,LT,LE}_EXPR comparisons are
3029 lowered by iteration from the most significant limb downwards to
3030 the least significant one, for large _BitInt in straight line code,
3031 otherwise with most significant limb handled in
3032 straight line code followed by a loop handling one limb at a time.
3033 Comparisons with unsigned huge _BitInt with precisions which are
3034 multiples of limb precision can use just the loop and don't need to
3035 handle most significant limb before the loop. The loop or straight
3036 line code jumps to final basic block if a particular pair of limbs
3037 is not equal. */
3038
3039 tree
3040 bitint_large_huge::lower_comparison_stmt (gimple *stmt, tree_code &cmp_code,
3041 tree cmp_op1, tree cmp_op2)
3042 {
3043 tree type = TREE_TYPE (cmp_op1);
3044 gcc_assert (TREE_CODE (type) == BITINT_TYPE);
3045 bitint_prec_kind kind = bitint_precision_kind (type);
3046 gcc_assert (kind >= bitint_prec_large);
3047 gimple *g;
3048 if (!TYPE_UNSIGNED (type)
3049 && integer_zerop (cmp_op2)
3050 && (cmp_code == GE_EXPR || cmp_code == LT_EXPR))
3051 {
3052 unsigned end = CEIL ((unsigned) TYPE_PRECISION (type), limb_prec) - 1;
3053 tree idx = size_int (end);
3054 m_data_cnt = 0;
3055 tree rhs1 = handle_operand (cmp_op1, idx);
3056 if (TYPE_UNSIGNED (TREE_TYPE (rhs1)))
3057 {
3058 tree stype = signed_type_for (TREE_TYPE (rhs1));
3059 rhs1 = add_cast (stype, rhs1);
3060 }
3061 tree lhs = make_ssa_name (boolean_type_node);
3062 g = gimple_build_assign (lhs, cmp_code, rhs1,
3063 build_zero_cst (TREE_TYPE (rhs1)));
3064 insert_before (g);
3065 cmp_code = NE_EXPR;
3066 return lhs;
3067 }
3068
3069 unsigned cnt, rem = 0, end = 0;
3070 tree idx = NULL_TREE, idx_next = NULL_TREE;
3071 if (kind == bitint_prec_large)
3072 cnt = CEIL ((unsigned) TYPE_PRECISION (type), limb_prec);
3073 else
3074 {
3075 rem = ((unsigned) TYPE_PRECISION (type) % limb_prec);
3076 if (rem == 0 && !TYPE_UNSIGNED (type))
3077 rem = limb_prec;
3078 end = ((unsigned) TYPE_PRECISION (type) - rem) / limb_prec;
3079 cnt = 1 + (rem != 0);
3080 }
3081
3082 basic_block edge_bb = NULL;
3083 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
3084 gsi_prev (&gsi);
3085 edge e = split_block (gsi_bb (gsi), gsi_stmt (gsi));
3086 edge_bb = e->src;
3087 m_gsi = gsi_end_bb (edge_bb);
3088
3089 edge *edges = XALLOCAVEC (edge, cnt * 2);
3090 for (unsigned i = 0; i < cnt; i++)
3091 {
3092 m_data_cnt = 0;
3093 if (kind == bitint_prec_large)
3094 idx = size_int (cnt - i - 1);
3095 else if (i == cnt - 1)
3096 idx = create_loop (size_int (end - 1), &idx_next);
3097 else
3098 idx = size_int (end);
3099 tree rhs1 = handle_operand (cmp_op1, idx);
3100 tree rhs2 = handle_operand (cmp_op2, idx);
3101 if (i == 0
3102 && !TYPE_UNSIGNED (type)
3103 && TYPE_UNSIGNED (TREE_TYPE (rhs1)))
3104 {
3105 tree stype = signed_type_for (TREE_TYPE (rhs1));
3106 rhs1 = add_cast (stype, rhs1);
3107 rhs2 = add_cast (stype, rhs2);
3108 }
3109 g = gimple_build_cond (GT_EXPR, rhs1, rhs2, NULL_TREE, NULL_TREE);
3110 insert_before (g);
3111 edge e1 = split_block (gsi_bb (m_gsi), g);
3112 e1->flags = EDGE_FALSE_VALUE;
3113 edge e2 = make_edge (e1->src, gimple_bb (stmt), EDGE_TRUE_VALUE);
3114 e1->probability = profile_probability::likely ();
3115 e2->probability = e1->probability.invert ();
3116 if (i == 0)
3117 set_immediate_dominator (CDI_DOMINATORS, e2->dest, e2->src);
3118 m_gsi = gsi_after_labels (e1->dest);
3119 edges[2 * i] = e2;
3120 g = gimple_build_cond (LT_EXPR, rhs1, rhs2, NULL_TREE, NULL_TREE);
3121 insert_before (g);
3122 e1 = split_block (gsi_bb (m_gsi), g);
3123 e1->flags = EDGE_FALSE_VALUE;
3124 e2 = make_edge (e1->src, gimple_bb (stmt), EDGE_TRUE_VALUE);
3125 e1->probability = profile_probability::unlikely ();
3126 e2->probability = e1->probability.invert ();
3127 m_gsi = gsi_after_labels (e1->dest);
3128 edges[2 * i + 1] = e2;
3129 m_first = false;
3130 if (kind == bitint_prec_huge && i == cnt - 1)
3131 {
3132 g = gimple_build_assign (idx_next, PLUS_EXPR, idx, size_int (-1));
3133 insert_before (g);
3134 g = gimple_build_cond (NE_EXPR, idx, size_zero_node,
3135 NULL_TREE, NULL_TREE);
3136 insert_before (g);
3137 edge true_edge, false_edge;
3138 extract_true_false_edges_from_block (gsi_bb (m_gsi),
3139 &true_edge, &false_edge);
3140 m_gsi = gsi_after_labels (false_edge->dest);
3141 m_bb = NULL;
3142 }
3143 }
3144
3145 tree lhs = make_ssa_name (boolean_type_node);
3146 basic_block bb = gimple_bb (stmt);
3147 gphi *phi = create_phi_node (lhs, bb);
3148 for (unsigned int i = 0; i < cnt * 2; i++)
3149 {
3150 tree val = ((cmp_code == GT_EXPR || cmp_code == GE_EXPR)
3151 ^ (i & 1)) ? boolean_true_node : boolean_false_node;
3152 add_phi_arg (phi, val, edges[i], UNKNOWN_LOCATION);
3153 }
3154 add_phi_arg (phi, (cmp_code == GE_EXPR || cmp_code == LE_EXPR)
3155 ? boolean_true_node : boolean_false_node,
3156 find_edge (gsi_bb (m_gsi), bb), UNKNOWN_LOCATION);
3157 cmp_code = NE_EXPR;
3158 return lhs;
3159 }
3160
3161 /* Lower large/huge _BitInt left and right shift except for left
3162 shift by < limb_prec constant. */
3163
3164 void
3165 bitint_large_huge::lower_shift_stmt (tree obj, gimple *stmt)
3166 {
3167 tree rhs1 = gimple_assign_rhs1 (stmt);
3168 tree lhs = gimple_assign_lhs (stmt);
3169 tree_code rhs_code = gimple_assign_rhs_code (stmt);
3170 tree type = TREE_TYPE (rhs1);
3171 gimple *final_stmt = gsi_stmt (m_gsi);
3172 gcc_assert (TREE_CODE (type) == BITINT_TYPE
3173 && bitint_precision_kind (type) >= bitint_prec_large);
3174 int prec = TYPE_PRECISION (type);
3175 tree n = gimple_assign_rhs2 (stmt), n1, n2, n3, n4;
3176 gimple *g;
3177 if (obj == NULL_TREE)
3178 {
3179 int part = var_to_partition (m_map, lhs);
3180 gcc_assert (m_vars[part] != NULL_TREE);
3181 obj = m_vars[part];
3182 }
3183 /* Preparation code common for both left and right shifts.
3184 unsigned n1 = n % limb_prec;
3185 size_t n2 = n / limb_prec;
3186 size_t n3 = n1 != 0;
3187 unsigned n4 = (limb_prec - n1) % limb_prec;
3188 (for power of 2 limb_prec n4 can be -n1 & (limb_prec)). */
3189 if (TREE_CODE (n) == INTEGER_CST)
3190 {
3191 tree lp = build_int_cst (TREE_TYPE (n), limb_prec);
3192 n1 = int_const_binop (TRUNC_MOD_EXPR, n, lp);
3193 n2 = fold_convert (sizetype, int_const_binop (TRUNC_DIV_EXPR, n, lp));
3194 n3 = size_int (!integer_zerop (n1));
3195 n4 = int_const_binop (TRUNC_MOD_EXPR,
3196 int_const_binop (MINUS_EXPR, lp, n1), lp);
3197 }
3198 else
3199 {
3200 n1 = make_ssa_name (TREE_TYPE (n));
3201 n2 = make_ssa_name (sizetype);
3202 n3 = make_ssa_name (sizetype);
3203 n4 = make_ssa_name (TREE_TYPE (n));
3204 if (pow2p_hwi (limb_prec))
3205 {
3206 tree lpm1 = build_int_cst (TREE_TYPE (n), limb_prec - 1);
3207 g = gimple_build_assign (n1, BIT_AND_EXPR, n, lpm1);
3208 insert_before (g);
3209 g = gimple_build_assign (useless_type_conversion_p (sizetype,
3210 TREE_TYPE (n))
3211 ? n2 : make_ssa_name (TREE_TYPE (n)),
3212 RSHIFT_EXPR, n,
3213 build_int_cst (TREE_TYPE (n),
3214 exact_log2 (limb_prec)));
3215 insert_before (g);
3216 if (gimple_assign_lhs (g) != n2)
3217 {
3218 g = gimple_build_assign (n2, NOP_EXPR, gimple_assign_lhs (g));
3219 insert_before (g);
3220 }
3221 g = gimple_build_assign (make_ssa_name (TREE_TYPE (n)),
3222 NEGATE_EXPR, n1);
3223 insert_before (g);
3224 g = gimple_build_assign (n4, BIT_AND_EXPR, gimple_assign_lhs (g),
3225 lpm1);
3226 insert_before (g);
3227 }
3228 else
3229 {
3230 tree lp = build_int_cst (TREE_TYPE (n), limb_prec);
3231 g = gimple_build_assign (n1, TRUNC_MOD_EXPR, n, lp);
3232 insert_before (g);
3233 g = gimple_build_assign (useless_type_conversion_p (sizetype,
3234 TREE_TYPE (n))
3235 ? n2 : make_ssa_name (TREE_TYPE (n)),
3236 TRUNC_DIV_EXPR, n, lp);
3237 insert_before (g);
3238 if (gimple_assign_lhs (g) != n2)
3239 {
3240 g = gimple_build_assign (n2, NOP_EXPR, gimple_assign_lhs (g));
3241 insert_before (g);
3242 }
3243 g = gimple_build_assign (make_ssa_name (TREE_TYPE (n)),
3244 MINUS_EXPR, lp, n1);
3245 insert_before (g);
3246 g = gimple_build_assign (n4, TRUNC_MOD_EXPR, gimple_assign_lhs (g),
3247 lp);
3248 insert_before (g);
3249 }
3250 g = gimple_build_assign (make_ssa_name (boolean_type_node), NE_EXPR, n1,
3251 build_zero_cst (TREE_TYPE (n)));
3252 insert_before (g);
3253 g = gimple_build_assign (n3, NOP_EXPR, gimple_assign_lhs (g));
3254 insert_before (g);
3255 }
3256 tree p = build_int_cst (sizetype,
3257 prec / limb_prec - (prec % limb_prec == 0));
3258 if (rhs_code == RSHIFT_EXPR)
3259 {
3260 /* Lower
3261 dst = src >> n;
3262 as
3263 unsigned n1 = n % limb_prec;
3264 size_t n2 = n / limb_prec;
3265 size_t n3 = n1 != 0;
3266 unsigned n4 = (limb_prec - n1) % limb_prec;
3267 size_t idx;
3268 size_t p = prec / limb_prec - (prec % limb_prec == 0);
3269 int signed_p = (typeof (src) -1) < 0;
3270 for (idx = n2; idx < ((!signed_p && (prec % limb_prec == 0))
3271 ? p : p - n3); ++idx)
3272 dst[idx - n2] = (src[idx] >> n1) | (src[idx + n3] << n4);
3273 limb_type ext;
3274 if (prec % limb_prec == 0)
3275 ext = src[p];
3276 else if (signed_p)
3277 ext = ((signed limb_type) (src[p] << (limb_prec
3278 - (prec % limb_prec))))
3279 >> (limb_prec - (prec % limb_prec));
3280 else
3281 ext = src[p] & (((limb_type) 1 << (prec % limb_prec)) - 1);
3282 if (!signed_p && (prec % limb_prec == 0))
3283 ;
3284 else if (idx < prec / 64)
3285 {
3286 dst[idx - n2] = (src[idx] >> n1) | (ext << n4);
3287 ++idx;
3288 }
3289 idx -= n2;
3290 if (signed_p)
3291 {
3292 dst[idx] = ((signed limb_type) ext) >> n1;
3293 ext = ((signed limb_type) ext) >> (limb_prec - 1);
3294 }
3295 else
3296 {
3297 dst[idx] = ext >> n1;
3298 ext = 0;
3299 }
3300 for (++idx; idx <= p; ++idx)
3301 dst[idx] = ext; */
3302 tree pmn3;
3303 if (TYPE_UNSIGNED (type) && prec % limb_prec == 0)
3304 pmn3 = p;
3305 else if (TREE_CODE (n3) == INTEGER_CST)
3306 pmn3 = int_const_binop (MINUS_EXPR, p, n3);
3307 else
3308 {
3309 pmn3 = make_ssa_name (sizetype);
3310 g = gimple_build_assign (pmn3, MINUS_EXPR, p, n3);
3311 insert_before (g);
3312 }
3313 g = gimple_build_cond (LT_EXPR, n2, pmn3, NULL_TREE, NULL_TREE);
3314 edge edge_true, edge_false;
3315 if_then (g, profile_probability::likely (), edge_true, edge_false);
3316 tree idx_next;
3317 tree idx = create_loop (n2, &idx_next);
3318 tree idxmn2 = make_ssa_name (sizetype);
3319 tree idxpn3 = make_ssa_name (sizetype);
3320 g = gimple_build_assign (idxmn2, MINUS_EXPR, idx, n2);
3321 insert_before (g);
3322 g = gimple_build_assign (idxpn3, PLUS_EXPR, idx, n3);
3323 insert_before (g);
3324 m_data_cnt = 0;
3325 tree t1 = handle_operand (rhs1, idx);
3326 m_first = false;
3327 g = gimple_build_assign (make_ssa_name (m_limb_type),
3328 RSHIFT_EXPR, t1, n1);
3329 insert_before (g);
3330 t1 = gimple_assign_lhs (g);
3331 if (!integer_zerop (n3))
3332 {
3333 m_data_cnt = 0;
3334 tree t2 = handle_operand (rhs1, idxpn3);
3335 g = gimple_build_assign (make_ssa_name (m_limb_type),
3336 LSHIFT_EXPR, t2, n4);
3337 insert_before (g);
3338 t2 = gimple_assign_lhs (g);
3339 g = gimple_build_assign (make_ssa_name (m_limb_type),
3340 BIT_IOR_EXPR, t1, t2);
3341 insert_before (g);
3342 t1 = gimple_assign_lhs (g);
3343 }
3344 tree l = limb_access (TREE_TYPE (lhs), obj, idxmn2, true);
3345 g = gimple_build_assign (l, t1);
3346 insert_before (g);
3347 g = gimple_build_assign (idx_next, PLUS_EXPR, idx, size_one_node);
3348 insert_before (g);
3349 g = gimple_build_cond (LT_EXPR, idx_next, pmn3, NULL_TREE, NULL_TREE);
3350 insert_before (g);
3351 idx = make_ssa_name (sizetype);
3352 m_gsi = gsi_for_stmt (final_stmt);
3353 gphi *phi = create_phi_node (idx, gsi_bb (m_gsi));
3354 edge_false = find_edge (edge_false->src, gsi_bb (m_gsi));
3355 edge_true = EDGE_PRED (gsi_bb (m_gsi),
3356 EDGE_PRED (gsi_bb (m_gsi), 0) == edge_false);
3357 add_phi_arg (phi, n2, edge_false, UNKNOWN_LOCATION);
3358 add_phi_arg (phi, idx_next, edge_true, UNKNOWN_LOCATION);
3359 m_data_cnt = 0;
3360 tree ms = handle_operand (rhs1, p);
3361 tree ext = ms;
3362 if (!types_compatible_p (TREE_TYPE (ms), m_limb_type))
3363 ext = add_cast (m_limb_type, ms);
3364 if (!(TYPE_UNSIGNED (type) && prec % limb_prec == 0)
3365 && !integer_zerop (n3))
3366 {
3367 g = gimple_build_cond (LT_EXPR, idx, p, NULL_TREE, NULL_TREE);
3368 if_then (g, profile_probability::likely (), edge_true, edge_false);
3369 m_data_cnt = 0;
3370 t1 = handle_operand (rhs1, idx);
3371 g = gimple_build_assign (make_ssa_name (m_limb_type),
3372 RSHIFT_EXPR, t1, n1);
3373 insert_before (g);
3374 t1 = gimple_assign_lhs (g);
3375 g = gimple_build_assign (make_ssa_name (m_limb_type),
3376 LSHIFT_EXPR, ext, n4);
3377 insert_before (g);
3378 tree t2 = gimple_assign_lhs (g);
3379 g = gimple_build_assign (make_ssa_name (m_limb_type),
3380 BIT_IOR_EXPR, t1, t2);
3381 insert_before (g);
3382 t1 = gimple_assign_lhs (g);
3383 idxmn2 = make_ssa_name (sizetype);
3384 g = gimple_build_assign (idxmn2, MINUS_EXPR, idx, n2);
3385 insert_before (g);
3386 l = limb_access (TREE_TYPE (lhs), obj, idxmn2, true);
3387 g = gimple_build_assign (l, t1);
3388 insert_before (g);
3389 idx_next = make_ssa_name (sizetype);
3390 g = gimple_build_assign (idx_next, PLUS_EXPR, idx, size_one_node);
3391 insert_before (g);
3392 m_gsi = gsi_for_stmt (final_stmt);
3393 tree nidx = make_ssa_name (sizetype);
3394 phi = create_phi_node (nidx, gsi_bb (m_gsi));
3395 edge_false = find_edge (edge_false->src, gsi_bb (m_gsi));
3396 edge_true = EDGE_PRED (gsi_bb (m_gsi),
3397 EDGE_PRED (gsi_bb (m_gsi), 0) == edge_false);
3398 add_phi_arg (phi, idx, edge_false, UNKNOWN_LOCATION);
3399 add_phi_arg (phi, idx_next, edge_true, UNKNOWN_LOCATION);
3400 idx = nidx;
3401 }
3402 g = gimple_build_assign (make_ssa_name (sizetype), MINUS_EXPR, idx, n2);
3403 insert_before (g);
3404 idx = gimple_assign_lhs (g);
3405 tree sext = ext;
3406 if (!TYPE_UNSIGNED (type))
3407 sext = add_cast (signed_type_for (m_limb_type), ext);
3408 g = gimple_build_assign (make_ssa_name (TREE_TYPE (sext)),
3409 RSHIFT_EXPR, sext, n1);
3410 insert_before (g);
3411 t1 = gimple_assign_lhs (g);
3412 if (!TYPE_UNSIGNED (type))
3413 {
3414 t1 = add_cast (m_limb_type, t1);
3415 g = gimple_build_assign (make_ssa_name (TREE_TYPE (sext)),
3416 RSHIFT_EXPR, sext,
3417 build_int_cst (TREE_TYPE (n),
3418 limb_prec - 1));
3419 insert_before (g);
3420 ext = add_cast (m_limb_type, gimple_assign_lhs (g));
3421 }
3422 else
3423 ext = build_zero_cst (m_limb_type);
3424 l = limb_access (TREE_TYPE (lhs), obj, idx, true);
3425 g = gimple_build_assign (l, t1);
3426 insert_before (g);
3427 g = gimple_build_assign (make_ssa_name (sizetype), PLUS_EXPR, idx,
3428 size_one_node);
3429 insert_before (g);
3430 idx = gimple_assign_lhs (g);
3431 g = gimple_build_cond (LE_EXPR, idx, p, NULL_TREE, NULL_TREE);
3432 if_then (g, profile_probability::likely (), edge_true, edge_false);
3433 idx = create_loop (idx, &idx_next);
3434 l = limb_access (TREE_TYPE (lhs), obj, idx, true);
3435 g = gimple_build_assign (l, ext);
3436 insert_before (g);
3437 g = gimple_build_assign (idx_next, PLUS_EXPR, idx, size_one_node);
3438 insert_before (g);
3439 g = gimple_build_cond (LE_EXPR, idx_next, p, NULL_TREE, NULL_TREE);
3440 insert_before (g);
3441 }
3442 else
3443 {
3444 /* Lower
3445 dst = src << n;
3446 as
3447 unsigned n1 = n % limb_prec;
3448 size_t n2 = n / limb_prec;
3449 size_t n3 = n1 != 0;
3450 unsigned n4 = (limb_prec - n1) % limb_prec;
3451 size_t idx;
3452 size_t p = prec / limb_prec - (prec % limb_prec == 0);
3453 for (idx = p; (ssize_t) idx >= (ssize_t) (n2 + n3); --idx)
3454 dst[idx] = (src[idx - n2] << n1) | (src[idx - n2 - n3] >> n4);
3455 if (n1)
3456 {
3457 dst[idx] = src[idx - n2] << n1;
3458 --idx;
3459 }
3460 for (; (ssize_t) idx >= 0; --idx)
3461 dst[idx] = 0; */
3462 tree n2pn3;
3463 if (TREE_CODE (n2) == INTEGER_CST && TREE_CODE (n3) == INTEGER_CST)
3464 n2pn3 = int_const_binop (PLUS_EXPR, n2, n3);
3465 else
3466 {
3467 n2pn3 = make_ssa_name (sizetype);
3468 g = gimple_build_assign (n2pn3, PLUS_EXPR, n2, n3);
3469 insert_before (g);
3470 }
3471 /* For LSHIFT_EXPR, we can use handle_operand with non-INTEGER_CST
3472 idx even to access the most significant partial limb. */
3473 m_var_msb = true;
3474 if (integer_zerop (n3))
3475 /* For n3 == 0 p >= n2 + n3 is always true for all valid shift
3476 counts. Emit if (true) condition that can be optimized later. */
3477 g = gimple_build_cond (NE_EXPR, boolean_true_node, boolean_false_node,
3478 NULL_TREE, NULL_TREE);
3479 else
3480 g = gimple_build_cond (LE_EXPR, n2pn3, p, NULL_TREE, NULL_TREE);
3481 edge edge_true, edge_false;
3482 if_then (g, profile_probability::likely (), edge_true, edge_false);
3483 tree idx_next;
3484 tree idx = create_loop (p, &idx_next);
3485 tree idxmn2 = make_ssa_name (sizetype);
3486 tree idxmn2mn3 = make_ssa_name (sizetype);
3487 g = gimple_build_assign (idxmn2, MINUS_EXPR, idx, n2);
3488 insert_before (g);
3489 g = gimple_build_assign (idxmn2mn3, MINUS_EXPR, idxmn2, n3);
3490 insert_before (g);
3491 m_data_cnt = 0;
3492 tree t1 = handle_operand (rhs1, idxmn2);
3493 m_first = false;
3494 g = gimple_build_assign (make_ssa_name (m_limb_type),
3495 LSHIFT_EXPR, t1, n1);
3496 insert_before (g);
3497 t1 = gimple_assign_lhs (g);
3498 if (!integer_zerop (n3))
3499 {
3500 m_data_cnt = 0;
3501 tree t2 = handle_operand (rhs1, idxmn2mn3);
3502 g = gimple_build_assign (make_ssa_name (m_limb_type),
3503 RSHIFT_EXPR, t2, n4);
3504 insert_before (g);
3505 t2 = gimple_assign_lhs (g);
3506 g = gimple_build_assign (make_ssa_name (m_limb_type),
3507 BIT_IOR_EXPR, t1, t2);
3508 insert_before (g);
3509 t1 = gimple_assign_lhs (g);
3510 }
3511 tree l = limb_access (TREE_TYPE (lhs), obj, idx, true);
3512 g = gimple_build_assign (l, t1);
3513 insert_before (g);
3514 g = gimple_build_assign (idx_next, PLUS_EXPR, idx, size_int (-1));
3515 insert_before (g);
3516 tree sn2pn3 = add_cast (ssizetype, n2pn3);
3517 g = gimple_build_cond (GE_EXPR, add_cast (ssizetype, idx_next), sn2pn3,
3518 NULL_TREE, NULL_TREE);
3519 insert_before (g);
3520 idx = make_ssa_name (sizetype);
3521 m_gsi = gsi_for_stmt (final_stmt);
3522 gphi *phi = create_phi_node (idx, gsi_bb (m_gsi));
3523 edge_false = find_edge (edge_false->src, gsi_bb (m_gsi));
3524 edge_true = EDGE_PRED (gsi_bb (m_gsi),
3525 EDGE_PRED (gsi_bb (m_gsi), 0) == edge_false);
3526 add_phi_arg (phi, p, edge_false, UNKNOWN_LOCATION);
3527 add_phi_arg (phi, idx_next, edge_true, UNKNOWN_LOCATION);
3528 m_data_cnt = 0;
3529 if (!integer_zerop (n3))
3530 {
3531 g = gimple_build_cond (NE_EXPR, n3, size_zero_node,
3532 NULL_TREE, NULL_TREE);
3533 if_then (g, profile_probability::likely (), edge_true, edge_false);
3534 idxmn2 = make_ssa_name (sizetype);
3535 g = gimple_build_assign (idxmn2, MINUS_EXPR, idx, n2);
3536 insert_before (g);
3537 m_data_cnt = 0;
3538 t1 = handle_operand (rhs1, idxmn2);
3539 g = gimple_build_assign (make_ssa_name (m_limb_type),
3540 LSHIFT_EXPR, t1, n1);
3541 insert_before (g);
3542 t1 = gimple_assign_lhs (g);
3543 l = limb_access (TREE_TYPE (lhs), obj, idx, true);
3544 g = gimple_build_assign (l, t1);
3545 insert_before (g);
3546 idx_next = make_ssa_name (sizetype);
3547 g = gimple_build_assign (idx_next, PLUS_EXPR, idx, size_int (-1));
3548 insert_before (g);
3549 m_gsi = gsi_for_stmt (final_stmt);
3550 tree nidx = make_ssa_name (sizetype);
3551 phi = create_phi_node (nidx, gsi_bb (m_gsi));
3552 edge_false = find_edge (edge_false->src, gsi_bb (m_gsi));
3553 edge_true = EDGE_PRED (gsi_bb (m_gsi),
3554 EDGE_PRED (gsi_bb (m_gsi), 0) == edge_false);
3555 add_phi_arg (phi, idx, edge_false, UNKNOWN_LOCATION);
3556 add_phi_arg (phi, idx_next, edge_true, UNKNOWN_LOCATION);
3557 idx = nidx;
3558 }
3559 g = gimple_build_cond (GE_EXPR, add_cast (ssizetype, idx),
3560 ssize_int (0), NULL_TREE, NULL_TREE);
3561 if_then (g, profile_probability::likely (), edge_true, edge_false);
3562 idx = create_loop (idx, &idx_next);
3563 l = limb_access (TREE_TYPE (lhs), obj, idx, true);
3564 g = gimple_build_assign (l, build_zero_cst (m_limb_type));
3565 insert_before (g);
3566 g = gimple_build_assign (idx_next, PLUS_EXPR, idx, size_int (-1));
3567 insert_before (g);
3568 g = gimple_build_cond (GE_EXPR, add_cast (ssizetype, idx_next),
3569 ssize_int (0), NULL_TREE, NULL_TREE);
3570 insert_before (g);
3571 }
3572 }
3573
3574 /* Lower large/huge _BitInt multiplication or division. */
3575
3576 void
3577 bitint_large_huge::lower_muldiv_stmt (tree obj, gimple *stmt)
3578 {
3579 tree rhs1 = gimple_assign_rhs1 (stmt);
3580 tree rhs2 = gimple_assign_rhs2 (stmt);
3581 tree lhs = gimple_assign_lhs (stmt);
3582 tree_code rhs_code = gimple_assign_rhs_code (stmt);
3583 tree type = TREE_TYPE (rhs1);
3584 gcc_assert (TREE_CODE (type) == BITINT_TYPE
3585 && bitint_precision_kind (type) >= bitint_prec_large);
3586 int prec = TYPE_PRECISION (type), prec1, prec2;
3587 rhs1 = handle_operand_addr (rhs1, stmt, NULL, &prec1);
3588 rhs2 = handle_operand_addr (rhs2, stmt, NULL, &prec2);
3589 if (obj == NULL_TREE)
3590 {
3591 int part = var_to_partition (m_map, lhs);
3592 gcc_assert (m_vars[part] != NULL_TREE);
3593 obj = m_vars[part];
3594 lhs = build_fold_addr_expr (obj);
3595 }
3596 else
3597 {
3598 lhs = build_fold_addr_expr (obj);
3599 lhs = force_gimple_operand_gsi (&m_gsi, lhs, true,
3600 NULL_TREE, true, GSI_SAME_STMT);
3601 }
3602 tree sitype = lang_hooks.types.type_for_mode (SImode, 0);
3603 gimple *g;
3604 switch (rhs_code)
3605 {
3606 case MULT_EXPR:
3607 g = gimple_build_call_internal (IFN_MULBITINT, 6,
3608 lhs, build_int_cst (sitype, prec),
3609 rhs1, build_int_cst (sitype, prec1),
3610 rhs2, build_int_cst (sitype, prec2));
3611 insert_before (g);
3612 break;
3613 case TRUNC_DIV_EXPR:
3614 case EXACT_DIV_EXPR:
3615 g = gimple_build_call_internal (IFN_DIVMODBITINT, 8,
3616 lhs, build_int_cst (sitype, prec),
3617 null_pointer_node,
3618 build_int_cst (sitype, 0),
3619 rhs1, build_int_cst (sitype, prec1),
3620 rhs2, build_int_cst (sitype, prec2));
3621 if (!stmt_ends_bb_p (stmt))
3622 gimple_call_set_nothrow (as_a <gcall *> (g), true);
3623 insert_before (g);
3624 break;
3625 case TRUNC_MOD_EXPR:
3626 g = gimple_build_call_internal (IFN_DIVMODBITINT, 8, null_pointer_node,
3627 build_int_cst (sitype, 0),
3628 lhs, build_int_cst (sitype, prec),
3629 rhs1, build_int_cst (sitype, prec1),
3630 rhs2, build_int_cst (sitype, prec2));
3631 if (!stmt_ends_bb_p (stmt))
3632 gimple_call_set_nothrow (as_a <gcall *> (g), true);
3633 insert_before (g);
3634 break;
3635 default:
3636 gcc_unreachable ();
3637 }
3638 if (stmt_ends_bb_p (stmt))
3639 {
3640 maybe_duplicate_eh_stmt (g, stmt);
3641 edge e1;
3642 edge_iterator ei;
3643 basic_block bb = gimple_bb (stmt);
3644
3645 FOR_EACH_EDGE (e1, ei, bb->succs)
3646 if (e1->flags & EDGE_EH)
3647 break;
3648 if (e1)
3649 {
3650 edge e2 = split_block (gsi_bb (m_gsi), g);
3651 m_gsi = gsi_after_labels (e2->dest);
3652 add_eh_edge (e2->src, e1);
3653 }
3654 }
3655 }
3656
3657 /* Lower large/huge _BitInt conversion to/from floating point. */
3658
3659 void
3660 bitint_large_huge::lower_float_conv_stmt (tree obj, gimple *stmt)
3661 {
3662 tree rhs1 = gimple_assign_rhs1 (stmt);
3663 tree lhs = gimple_assign_lhs (stmt);
3664 tree_code rhs_code = gimple_assign_rhs_code (stmt);
3665 tree sitype = lang_hooks.types.type_for_mode (SImode, 0);
3666 gimple *g;
3667 if (rhs_code == FIX_TRUNC_EXPR)
3668 {
3669 int prec = TYPE_PRECISION (TREE_TYPE (lhs));
3670 if (!TYPE_UNSIGNED (TREE_TYPE (lhs)))
3671 prec = -prec;
3672 if (obj == NULL_TREE)
3673 {
3674 int part = var_to_partition (m_map, lhs);
3675 gcc_assert (m_vars[part] != NULL_TREE);
3676 obj = m_vars[part];
3677 lhs = build_fold_addr_expr (obj);
3678 }
3679 else
3680 {
3681 lhs = build_fold_addr_expr (obj);
3682 lhs = force_gimple_operand_gsi (&m_gsi, lhs, true,
3683 NULL_TREE, true, GSI_SAME_STMT);
3684 }
3685 scalar_mode from_mode
3686 = as_a <scalar_mode> (TYPE_MODE (TREE_TYPE (rhs1)));
3687 #ifdef HAVE_SFmode
3688 /* IEEE single is a full superset of both IEEE half and
3689 bfloat formats, convert to float first and then to _BitInt
3690 to avoid the need of another 2 library routines. */
3691 if ((REAL_MODE_FORMAT (from_mode) == &arm_bfloat_half_format
3692 || REAL_MODE_FORMAT (from_mode) == &ieee_half_format)
3693 && REAL_MODE_FORMAT (SFmode) == &ieee_single_format)
3694 {
3695 tree type = lang_hooks.types.type_for_mode (SFmode, 0);
3696 if (type)
3697 rhs1 = add_cast (type, rhs1);
3698 }
3699 #endif
3700 g = gimple_build_call_internal (IFN_FLOATTOBITINT, 3,
3701 lhs, build_int_cst (sitype, prec),
3702 rhs1);
3703 insert_before (g);
3704 }
3705 else
3706 {
3707 int prec;
3708 rhs1 = handle_operand_addr (rhs1, stmt, NULL, &prec);
3709 g = gimple_build_call_internal (IFN_BITINTTOFLOAT, 2,
3710 rhs1, build_int_cst (sitype, prec));
3711 gimple_call_set_lhs (g, lhs);
3712 if (!stmt_ends_bb_p (stmt))
3713 gimple_call_set_nothrow (as_a <gcall *> (g), true);
3714 gsi_replace (&m_gsi, g, true);
3715 }
3716 }
3717
3718 /* Helper method for lower_addsub_overflow and lower_mul_overflow.
3719 If check_zero is true, caller wants to check if all bits in [start, end)
3720 are zero, otherwise if bits in [start, end) are either all zero or
3721 all ones. L is the limb with index LIMB, START and END are measured
3722 in bits. */
3723
3724 tree
3725 bitint_large_huge::arith_overflow_extract_bits (unsigned int start,
3726 unsigned int end, tree l,
3727 unsigned int limb,
3728 bool check_zero)
3729 {
3730 unsigned startlimb = start / limb_prec;
3731 unsigned endlimb = (end - 1) / limb_prec;
3732 gimple *g;
3733
3734 if ((start % limb_prec) == 0 && (end % limb_prec) == 0)
3735 return l;
3736 if (startlimb == endlimb && limb == startlimb)
3737 {
3738 if (check_zero)
3739 {
3740 wide_int w = wi::shifted_mask (start % limb_prec,
3741 end - start, false, limb_prec);
3742 g = gimple_build_assign (make_ssa_name (m_limb_type),
3743 BIT_AND_EXPR, l,
3744 wide_int_to_tree (m_limb_type, w));
3745 insert_before (g);
3746 return gimple_assign_lhs (g);
3747 }
3748 unsigned int shift = start % limb_prec;
3749 if ((end % limb_prec) != 0)
3750 {
3751 unsigned int lshift = (-end) % limb_prec;
3752 shift += lshift;
3753 g = gimple_build_assign (make_ssa_name (m_limb_type),
3754 LSHIFT_EXPR, l,
3755 build_int_cst (unsigned_type_node,
3756 lshift));
3757 insert_before (g);
3758 l = gimple_assign_lhs (g);
3759 }
3760 l = add_cast (signed_type_for (m_limb_type), l);
3761 g = gimple_build_assign (make_ssa_name (TREE_TYPE (l)),
3762 RSHIFT_EXPR, l,
3763 build_int_cst (unsigned_type_node, shift));
3764 insert_before (g);
3765 return add_cast (m_limb_type, gimple_assign_lhs (g));
3766 }
3767 else if (limb == startlimb)
3768 {
3769 if ((start % limb_prec) == 0)
3770 return l;
3771 if (!check_zero)
3772 l = add_cast (signed_type_for (m_limb_type), l);
3773 g = gimple_build_assign (make_ssa_name (TREE_TYPE (l)),
3774 RSHIFT_EXPR, l,
3775 build_int_cst (unsigned_type_node,
3776 start % limb_prec));
3777 insert_before (g);
3778 l = gimple_assign_lhs (g);
3779 if (!check_zero)
3780 l = add_cast (m_limb_type, l);
3781 return l;
3782 }
3783 else if (limb == endlimb)
3784 {
3785 if ((end % limb_prec) == 0)
3786 return l;
3787 if (check_zero)
3788 {
3789 wide_int w = wi::mask (end % limb_prec, false, limb_prec);
3790 g = gimple_build_assign (make_ssa_name (m_limb_type),
3791 BIT_AND_EXPR, l,
3792 wide_int_to_tree (m_limb_type, w));
3793 insert_before (g);
3794 return gimple_assign_lhs (g);
3795 }
3796 unsigned int shift = (-end) % limb_prec;
3797 g = gimple_build_assign (make_ssa_name (m_limb_type),
3798 LSHIFT_EXPR, l,
3799 build_int_cst (unsigned_type_node, shift));
3800 insert_before (g);
3801 l = add_cast (signed_type_for (m_limb_type), gimple_assign_lhs (g));
3802 g = gimple_build_assign (make_ssa_name (TREE_TYPE (l)),
3803 RSHIFT_EXPR, l,
3804 build_int_cst (unsigned_type_node, shift));
3805 insert_before (g);
3806 return add_cast (m_limb_type, gimple_assign_lhs (g));
3807 }
3808 return l;
3809 }
3810
3811 /* Helper method for lower_addsub_overflow and lower_mul_overflow. Store
3812 result including overflow flag into the right locations. */
3813
3814 void
3815 bitint_large_huge::finish_arith_overflow (tree var, tree obj, tree type,
3816 tree ovf, tree lhs, tree orig_obj,
3817 gimple *stmt, tree_code code)
3818 {
3819 gimple *g;
3820
3821 if (obj == NULL_TREE
3822 && (TREE_CODE (type) != BITINT_TYPE
3823 || bitint_precision_kind (type) < bitint_prec_large))
3824 {
3825 /* Add support for 3 or more limbs filled in from normal integral
3826 type if this assert fails. If no target chooses limb mode smaller
3827 than half of largest supported normal integral type, this will not
3828 be needed. */
3829 gcc_assert (TYPE_PRECISION (type) <= 2 * limb_prec);
3830 tree lhs_type = type;
3831 if (TREE_CODE (type) == BITINT_TYPE
3832 && bitint_precision_kind (type) == bitint_prec_middle)
3833 lhs_type = build_nonstandard_integer_type (TYPE_PRECISION (type),
3834 TYPE_UNSIGNED (type));
3835 tree r1 = limb_access (NULL_TREE, var, size_int (0), true);
3836 g = gimple_build_assign (make_ssa_name (m_limb_type), r1);
3837 insert_before (g);
3838 r1 = gimple_assign_lhs (g);
3839 if (!useless_type_conversion_p (lhs_type, TREE_TYPE (r1)))
3840 r1 = add_cast (lhs_type, r1);
3841 if (TYPE_PRECISION (lhs_type) > limb_prec)
3842 {
3843 tree r2 = limb_access (NULL_TREE, var, size_int (1), true);
3844 g = gimple_build_assign (make_ssa_name (m_limb_type), r2);
3845 insert_before (g);
3846 r2 = gimple_assign_lhs (g);
3847 r2 = add_cast (lhs_type, r2);
3848 g = gimple_build_assign (make_ssa_name (lhs_type), LSHIFT_EXPR, r2,
3849 build_int_cst (unsigned_type_node,
3850 limb_prec));
3851 insert_before (g);
3852 g = gimple_build_assign (make_ssa_name (lhs_type), BIT_IOR_EXPR, r1,
3853 gimple_assign_lhs (g));
3854 insert_before (g);
3855 r1 = gimple_assign_lhs (g);
3856 }
3857 if (lhs_type != type)
3858 r1 = add_cast (type, r1);
3859 ovf = add_cast (lhs_type, ovf);
3860 if (lhs_type != type)
3861 ovf = add_cast (type, ovf);
3862 g = gimple_build_assign (lhs, COMPLEX_EXPR, r1, ovf);
3863 m_gsi = gsi_for_stmt (stmt);
3864 gsi_replace (&m_gsi, g, true);
3865 }
3866 else
3867 {
3868 unsigned HOST_WIDE_INT nelts = 0;
3869 tree atype = NULL_TREE;
3870 if (obj)
3871 {
3872 nelts = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (obj))) / limb_prec;
3873 if (orig_obj == NULL_TREE)
3874 nelts >>= 1;
3875 atype = build_array_type_nelts (m_limb_type, nelts);
3876 }
3877 if (var && obj)
3878 {
3879 tree v1, v2;
3880 tree zero;
3881 if (orig_obj == NULL_TREE)
3882 {
3883 zero = build_zero_cst (build_pointer_type (TREE_TYPE (obj)));
3884 v1 = build2 (MEM_REF, atype,
3885 build_fold_addr_expr (unshare_expr (obj)), zero);
3886 }
3887 else if (!useless_type_conversion_p (atype, TREE_TYPE (obj)))
3888 v1 = build1 (VIEW_CONVERT_EXPR, atype, unshare_expr (obj));
3889 else
3890 v1 = unshare_expr (obj);
3891 zero = build_zero_cst (build_pointer_type (TREE_TYPE (var)));
3892 v2 = build2 (MEM_REF, atype, build_fold_addr_expr (var), zero);
3893 g = gimple_build_assign (v1, v2);
3894 insert_before (g);
3895 }
3896 if (orig_obj == NULL_TREE && obj)
3897 {
3898 ovf = add_cast (m_limb_type, ovf);
3899 tree l = limb_access (NULL_TREE, obj, size_int (nelts), true);
3900 g = gimple_build_assign (l, ovf);
3901 insert_before (g);
3902 if (nelts > 1)
3903 {
3904 atype = build_array_type_nelts (m_limb_type, nelts - 1);
3905 tree off = build_int_cst (build_pointer_type (TREE_TYPE (obj)),
3906 (nelts + 1) * m_limb_size);
3907 tree v1 = build2 (MEM_REF, atype,
3908 build_fold_addr_expr (unshare_expr (obj)),
3909 off);
3910 g = gimple_build_assign (v1, build_zero_cst (atype));
3911 insert_before (g);
3912 }
3913 }
3914 else if (TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE)
3915 {
3916 imm_use_iterator ui;
3917 use_operand_p use_p;
3918 FOR_EACH_IMM_USE_FAST (use_p, ui, lhs)
3919 {
3920 g = USE_STMT (use_p);
3921 if (!is_gimple_assign (g)
3922 || gimple_assign_rhs_code (g) != IMAGPART_EXPR)
3923 continue;
3924 tree lhs2 = gimple_assign_lhs (g);
3925 gimple *use_stmt;
3926 single_imm_use (lhs2, &use_p, &use_stmt);
3927 lhs2 = gimple_assign_lhs (use_stmt);
3928 gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
3929 if (useless_type_conversion_p (TREE_TYPE (lhs2), TREE_TYPE (ovf)))
3930 g = gimple_build_assign (lhs2, ovf);
3931 else
3932 g = gimple_build_assign (lhs2, NOP_EXPR, ovf);
3933 gsi_replace (&gsi, g, true);
3934 if (gsi_stmt (m_gsi) == use_stmt)
3935 m_gsi = gsi_for_stmt (g);
3936 break;
3937 }
3938 }
3939 else if (ovf != boolean_false_node)
3940 {
3941 g = gimple_build_cond (NE_EXPR, ovf, boolean_false_node,
3942 NULL_TREE, NULL_TREE);
3943 edge edge_true, edge_false;
3944 if_then (g, profile_probability::very_unlikely (),
3945 edge_true, edge_false);
3946 tree zero = build_zero_cst (TREE_TYPE (lhs));
3947 tree fn = ubsan_build_overflow_builtin (code, m_loc,
3948 TREE_TYPE (lhs),
3949 zero, zero, NULL);
3950 force_gimple_operand_gsi (&m_gsi, fn, true, NULL_TREE,
3951 true, GSI_SAME_STMT);
3952 m_gsi = gsi_after_labels (edge_true->dest);
3953 }
3954 }
3955 if (var)
3956 {
3957 tree clobber = build_clobber (TREE_TYPE (var), CLOBBER_STORAGE_END);
3958 g = gimple_build_assign (var, clobber);
3959 gsi_insert_after (&m_gsi, g, GSI_SAME_STMT);
3960 }
3961 }
3962
3963 /* Helper function for lower_addsub_overflow and lower_mul_overflow.
3964 Given precisions of result TYPE (PREC), argument 0 precision PREC0,
3965 argument 1 precision PREC1 and minimum precision for the result
3966 PREC2, compute *START, *END, *CHECK_ZERO and return OVF. */
3967
3968 static tree
3969 arith_overflow (tree_code code, tree type, int prec, int prec0, int prec1,
3970 int prec2, unsigned *start, unsigned *end, bool *check_zero)
3971 {
3972 *start = 0;
3973 *end = 0;
3974 *check_zero = true;
3975 /* Ignore this special rule for subtraction, even if both
3976 prec0 >= 0 and prec1 >= 0, their subtraction can be negative
3977 in infinite precision. */
3978 if (code != MINUS_EXPR && prec0 >= 0 && prec1 >= 0)
3979 {
3980 /* Result in [0, prec2) is unsigned, if prec > prec2,
3981 all bits above it will be zero. */
3982 if ((prec - !TYPE_UNSIGNED (type)) >= prec2)
3983 return boolean_false_node;
3984 else
3985 {
3986 /* ovf if any of bits in [start, end) is non-zero. */
3987 *start = prec - !TYPE_UNSIGNED (type);
3988 *end = prec2;
3989 }
3990 }
3991 else if (TYPE_UNSIGNED (type))
3992 {
3993 /* If result in [0, prec2) is signed and if prec > prec2,
3994 all bits above it will be sign bit copies. */
3995 if (prec >= prec2)
3996 {
3997 /* ovf if bit prec - 1 is non-zero. */
3998 *start = prec - 1;
3999 *end = prec;
4000 }
4001 else
4002 {
4003 /* ovf if any of bits in [start, end) is non-zero. */
4004 *start = prec;
4005 *end = prec2;
4006 }
4007 }
4008 else if (prec >= prec2)
4009 return boolean_false_node;
4010 else
4011 {
4012 /* ovf if [start, end) bits aren't all zeros or all ones. */
4013 *start = prec - 1;
4014 *end = prec2;
4015 *check_zero = false;
4016 }
4017 return NULL_TREE;
4018 }
4019
4020 /* Lower a .{ADD,SUB}_OVERFLOW call with at least one large/huge _BitInt
4021 argument or return type _Complex large/huge _BitInt. */
4022
4023 void
4024 bitint_large_huge::lower_addsub_overflow (tree obj, gimple *stmt)
4025 {
4026 tree arg0 = gimple_call_arg (stmt, 0);
4027 tree arg1 = gimple_call_arg (stmt, 1);
4028 tree lhs = gimple_call_lhs (stmt);
4029 gimple *g;
4030
4031 if (!lhs)
4032 {
4033 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
4034 gsi_remove (&gsi, true);
4035 return;
4036 }
4037 gimple *final_stmt = gsi_stmt (m_gsi);
4038 tree type = TREE_TYPE (lhs);
4039 if (TREE_CODE (type) == COMPLEX_TYPE)
4040 type = TREE_TYPE (type);
4041 int prec = TYPE_PRECISION (type);
4042 int prec0 = range_to_prec (arg0, stmt);
4043 int prec1 = range_to_prec (arg1, stmt);
4044 /* If PREC0 >= 0 && PREC1 >= 0 and CODE is not MINUS_EXPR, PREC2 is
4045 the be minimum unsigned precision of any possible operation's
4046 result, otherwise it is minimum signed precision.
4047 Some examples:
4048 If PREC0 or PREC1 is 8, it means that argument is [0, 0xff],
4049 if PREC0 or PREC1 is 10, it means that argument is [0, 0x3ff],
4050 if PREC0 or PREC1 is -8, it means that argument is [-0x80, 0x7f],
4051 if PREC0 or PREC1 is -10, it means that argument is [-0x200, 0x1ff].
4052 PREC0 CODE PREC1 RESULT PREC2 SIGNED vs. UNSIGNED
4053 8 + 8 [0, 0x1fe] 9 UNSIGNED
4054 8 + 10 [0, 0x4fe] 11 UNSIGNED
4055 -8 + -8 [-0x100, 0xfe] 9 SIGNED
4056 -8 + -10 [-0x280, 0x27e] 11 SIGNED
4057 8 + -8 [-0x80, 0x17e] 10 SIGNED
4058 8 + -10 [-0x200, 0x2fe] 11 SIGNED
4059 10 + -8 [-0x80, 0x47e] 12 SIGNED
4060 8 - 8 [-0xff, 0xff] 9 SIGNED
4061 8 - 10 [-0x3ff, 0xff] 11 SIGNED
4062 10 - 8 [-0xff, 0x3ff] 11 SIGNED
4063 -8 - -8 [-0xff, 0xff] 9 SIGNED
4064 -8 - -10 [-0x27f, 0x27f] 11 SIGNED
4065 -10 - -8 [-0x27f, 0x27f] 11 SIGNED
4066 8 - -8 [-0x7f, 0x17f] 10 SIGNED
4067 8 - -10 [-0x1ff, 0x2ff] 11 SIGNED
4068 10 - -8 [-0x7f, 0x47f] 12 SIGNED
4069 -8 - 8 [-0x17f, 0x7f] 10 SIGNED
4070 -8 - 10 [-0x47f, 0x7f] 12 SIGNED
4071 -10 - 8 [-0x2ff, 0x1ff] 11 SIGNED */
4072 int prec2 = MAX (prec0 < 0 ? -prec0 : prec0,
4073 prec1 < 0 ? -prec1 : prec1);
4074 /* If operands are either both signed or both unsigned,
4075 we need just one additional bit. */
4076 prec2 = (((prec0 < 0) == (prec1 < 0)
4077 /* If one operand is signed and one unsigned and
4078 the signed one has larger precision, we need
4079 just one extra bit, otherwise two. */
4080 || (prec0 < 0 ? (prec2 == -prec0 && prec2 != prec1)
4081 : (prec2 == -prec1 && prec2 != prec0)))
4082 ? prec2 + 1 : prec2 + 2);
4083 int prec3 = MAX (prec0 < 0 ? -prec0 : prec0,
4084 prec1 < 0 ? -prec1 : prec1);
4085 prec3 = MAX (prec3, prec);
4086 tree var = NULL_TREE;
4087 tree orig_obj = obj;
4088 if (obj == NULL_TREE
4089 && TREE_CODE (type) == BITINT_TYPE
4090 && bitint_precision_kind (type) >= bitint_prec_large
4091 && m_names
4092 && bitmap_bit_p (m_names, SSA_NAME_VERSION (lhs)))
4093 {
4094 int part = var_to_partition (m_map, lhs);
4095 gcc_assert (m_vars[part] != NULL_TREE);
4096 obj = m_vars[part];
4097 if (TREE_TYPE (lhs) == type)
4098 orig_obj = obj;
4099 }
4100 if (TREE_CODE (type) != BITINT_TYPE
4101 || bitint_precision_kind (type) < bitint_prec_large)
4102 {
4103 unsigned HOST_WIDE_INT nelts = CEIL (prec, limb_prec);
4104 tree atype = build_array_type_nelts (m_limb_type, nelts);
4105 var = create_tmp_var (atype);
4106 }
4107
4108 enum tree_code code;
4109 switch (gimple_call_internal_fn (stmt))
4110 {
4111 case IFN_ADD_OVERFLOW:
4112 case IFN_UBSAN_CHECK_ADD:
4113 code = PLUS_EXPR;
4114 break;
4115 case IFN_SUB_OVERFLOW:
4116 case IFN_UBSAN_CHECK_SUB:
4117 code = MINUS_EXPR;
4118 break;
4119 default:
4120 gcc_unreachable ();
4121 }
4122 unsigned start, end;
4123 bool check_zero;
4124 tree ovf = arith_overflow (code, type, prec, prec0, prec1, prec2,
4125 &start, &end, &check_zero);
4126
4127 unsigned startlimb, endlimb;
4128 if (ovf)
4129 {
4130 startlimb = ~0U;
4131 endlimb = ~0U;
4132 }
4133 else
4134 {
4135 startlimb = start / limb_prec;
4136 endlimb = (end - 1) / limb_prec;
4137 }
4138
4139 int prec4 = ovf != NULL_TREE ? prec : prec3;
4140 bitint_prec_kind kind = bitint_precision_kind (prec4);
4141 unsigned cnt, rem = 0, fin = 0;
4142 tree idx = NULL_TREE, idx_first = NULL_TREE, idx_next = NULL_TREE;
4143 bool last_ovf = (ovf == NULL_TREE
4144 && CEIL (prec2, limb_prec) > CEIL (prec3, limb_prec));
4145 if (kind != bitint_prec_huge)
4146 cnt = CEIL (prec4, limb_prec) + last_ovf;
4147 else
4148 {
4149 rem = (prec4 % (2 * limb_prec));
4150 fin = (prec4 - rem) / limb_prec;
4151 cnt = 2 + CEIL (rem, limb_prec) + last_ovf;
4152 idx = idx_first = create_loop (size_zero_node, &idx_next);
4153 }
4154
4155 if (kind == bitint_prec_huge)
4156 m_upwards_2limb = fin;
4157 m_upwards = true;
4158
4159 tree type0 = TREE_TYPE (arg0);
4160 tree type1 = TREE_TYPE (arg1);
4161 int prec5 = prec3;
4162 if (bitint_precision_kind (prec5) < bitint_prec_large)
4163 prec5 = MAX (TYPE_PRECISION (type0), TYPE_PRECISION (type1));
4164 if (TYPE_PRECISION (type0) < prec5)
4165 {
4166 type0 = build_bitint_type (prec5, TYPE_UNSIGNED (type0));
4167 if (TREE_CODE (arg0) == INTEGER_CST)
4168 arg0 = fold_convert (type0, arg0);
4169 }
4170 if (TYPE_PRECISION (type1) < prec5)
4171 {
4172 type1 = build_bitint_type (prec5, TYPE_UNSIGNED (type1));
4173 if (TREE_CODE (arg1) == INTEGER_CST)
4174 arg1 = fold_convert (type1, arg1);
4175 }
4176 unsigned int data_cnt = 0;
4177 tree last_rhs1 = NULL_TREE, last_rhs2 = NULL_TREE;
4178 tree cmp = build_zero_cst (m_limb_type);
4179 unsigned prec_limbs = CEIL ((unsigned) prec, limb_prec);
4180 tree ovf_out = NULL_TREE, cmp_out = NULL_TREE;
4181 for (unsigned i = 0; i < cnt; i++)
4182 {
4183 m_data_cnt = 0;
4184 tree rhs1, rhs2;
4185 if (kind != bitint_prec_huge)
4186 idx = size_int (i);
4187 else if (i >= 2)
4188 idx = size_int (fin + i - 2);
4189 if (!last_ovf || i < cnt - 1)
4190 {
4191 if (type0 != TREE_TYPE (arg0))
4192 rhs1 = handle_cast (type0, arg0, idx);
4193 else
4194 rhs1 = handle_operand (arg0, idx);
4195 if (type1 != TREE_TYPE (arg1))
4196 rhs2 = handle_cast (type1, arg1, idx);
4197 else
4198 rhs2 = handle_operand (arg1, idx);
4199 if (i == 0)
4200 data_cnt = m_data_cnt;
4201 if (!useless_type_conversion_p (m_limb_type, TREE_TYPE (rhs1)))
4202 rhs1 = add_cast (m_limb_type, rhs1);
4203 if (!useless_type_conversion_p (m_limb_type, TREE_TYPE (rhs2)))
4204 rhs2 = add_cast (m_limb_type, rhs2);
4205 last_rhs1 = rhs1;
4206 last_rhs2 = rhs2;
4207 }
4208 else
4209 {
4210 m_data_cnt = data_cnt;
4211 if (TYPE_UNSIGNED (type0) || prec0 >= 0)
4212 rhs1 = build_zero_cst (m_limb_type);
4213 else
4214 {
4215 rhs1 = add_cast (signed_type_for (m_limb_type), last_rhs1);
4216 if (TREE_CODE (rhs1) == INTEGER_CST)
4217 rhs1 = build_int_cst (m_limb_type,
4218 tree_int_cst_sgn (rhs1) < 0 ? -1 : 0);
4219 else
4220 {
4221 tree lpm1 = build_int_cst (unsigned_type_node,
4222 limb_prec - 1);
4223 g = gimple_build_assign (make_ssa_name (TREE_TYPE (rhs1)),
4224 RSHIFT_EXPR, rhs1, lpm1);
4225 insert_before (g);
4226 rhs1 = add_cast (m_limb_type, gimple_assign_lhs (g));
4227 }
4228 }
4229 if (TYPE_UNSIGNED (type1) || prec1 >= 0)
4230 rhs2 = build_zero_cst (m_limb_type);
4231 else
4232 {
4233 rhs2 = add_cast (signed_type_for (m_limb_type), last_rhs2);
4234 if (TREE_CODE (rhs2) == INTEGER_CST)
4235 rhs2 = build_int_cst (m_limb_type,
4236 tree_int_cst_sgn (rhs2) < 0 ? -1 : 0);
4237 else
4238 {
4239 tree lpm1 = build_int_cst (unsigned_type_node,
4240 limb_prec - 1);
4241 g = gimple_build_assign (make_ssa_name (TREE_TYPE (rhs2)),
4242 RSHIFT_EXPR, rhs2, lpm1);
4243 insert_before (g);
4244 rhs2 = add_cast (m_limb_type, gimple_assign_lhs (g));
4245 }
4246 }
4247 }
4248 tree rhs = handle_plus_minus (code, rhs1, rhs2, idx);
4249 if (ovf != boolean_false_node)
4250 {
4251 if (tree_fits_uhwi_p (idx))
4252 {
4253 unsigned limb = tree_to_uhwi (idx);
4254 if (limb >= startlimb && limb <= endlimb)
4255 {
4256 tree l = arith_overflow_extract_bits (start, end, rhs,
4257 limb, check_zero);
4258 tree this_ovf = make_ssa_name (boolean_type_node);
4259 if (ovf == NULL_TREE && !check_zero)
4260 {
4261 cmp = l;
4262 g = gimple_build_assign (make_ssa_name (m_limb_type),
4263 PLUS_EXPR, l,
4264 build_int_cst (m_limb_type, 1));
4265 insert_before (g);
4266 g = gimple_build_assign (this_ovf, GT_EXPR,
4267 gimple_assign_lhs (g),
4268 build_int_cst (m_limb_type, 1));
4269 }
4270 else
4271 g = gimple_build_assign (this_ovf, NE_EXPR, l, cmp);
4272 insert_before (g);
4273 if (ovf == NULL_TREE)
4274 ovf = this_ovf;
4275 else
4276 {
4277 tree b = make_ssa_name (boolean_type_node);
4278 g = gimple_build_assign (b, BIT_IOR_EXPR, ovf, this_ovf);
4279 insert_before (g);
4280 ovf = b;
4281 }
4282 }
4283 }
4284 else if (startlimb < fin)
4285 {
4286 if (m_first && startlimb + 2 < fin)
4287 {
4288 tree data_out;
4289 ovf = prepare_data_in_out (boolean_false_node, idx, &data_out);
4290 ovf_out = m_data.pop ();
4291 m_data.pop ();
4292 if (!check_zero)
4293 {
4294 cmp = prepare_data_in_out (cmp, idx, &data_out);
4295 cmp_out = m_data.pop ();
4296 m_data.pop ();
4297 }
4298 }
4299 if (i != 0 || startlimb != fin - 1)
4300 {
4301 tree_code cmp_code;
4302 bool single_comparison
4303 = (startlimb + 2 >= fin || (startlimb & 1) != (i & 1));
4304 if (!single_comparison)
4305 cmp_code = GE_EXPR;
4306 else if ((startlimb & 1) == (i & 1))
4307 cmp_code = EQ_EXPR;
4308 else
4309 cmp_code = GT_EXPR;
4310 g = gimple_build_cond (cmp_code, idx, size_int (startlimb),
4311 NULL_TREE, NULL_TREE);
4312 edge edge_true_true, edge_true_false, edge_false;
4313 gimple *g2 = NULL;
4314 if (!single_comparison)
4315 g2 = gimple_build_cond (NE_EXPR, idx,
4316 size_int (startlimb), NULL_TREE,
4317 NULL_TREE);
4318 if_then_if_then_else (g, g2, profile_probability::likely (),
4319 profile_probability::likely (),
4320 edge_true_true, edge_true_false,
4321 edge_false);
4322 unsigned tidx = startlimb + (cmp_code == GT_EXPR);
4323 tree l = arith_overflow_extract_bits (start, end, rhs, tidx,
4324 check_zero);
4325 tree this_ovf = make_ssa_name (boolean_type_node);
4326 if (cmp_code != GT_EXPR && !check_zero)
4327 {
4328 g = gimple_build_assign (make_ssa_name (m_limb_type),
4329 PLUS_EXPR, l,
4330 build_int_cst (m_limb_type, 1));
4331 insert_before (g);
4332 g = gimple_build_assign (this_ovf, GT_EXPR,
4333 gimple_assign_lhs (g),
4334 build_int_cst (m_limb_type, 1));
4335 }
4336 else
4337 g = gimple_build_assign (this_ovf, NE_EXPR, l, cmp);
4338 insert_before (g);
4339 if (cmp_code == GT_EXPR)
4340 {
4341 tree t = make_ssa_name (boolean_type_node);
4342 g = gimple_build_assign (t, BIT_IOR_EXPR, ovf, this_ovf);
4343 insert_before (g);
4344 this_ovf = t;
4345 }
4346 tree this_ovf2 = NULL_TREE;
4347 if (!single_comparison)
4348 {
4349 m_gsi = gsi_after_labels (edge_true_true->src);
4350 tree t = make_ssa_name (boolean_type_node);
4351 g = gimple_build_assign (t, NE_EXPR, rhs, cmp);
4352 insert_before (g);
4353 this_ovf2 = make_ssa_name (boolean_type_node);
4354 g = gimple_build_assign (this_ovf2, BIT_IOR_EXPR,
4355 ovf, t);
4356 insert_before (g);
4357 }
4358 m_gsi = gsi_after_labels (edge_true_false->dest);
4359 tree t;
4360 if (i == 1 && ovf_out)
4361 t = ovf_out;
4362 else
4363 t = make_ssa_name (boolean_type_node);
4364 gphi *phi = create_phi_node (t, edge_true_false->dest);
4365 add_phi_arg (phi, this_ovf, edge_true_false,
4366 UNKNOWN_LOCATION);
4367 add_phi_arg (phi, ovf ? ovf
4368 : boolean_false_node, edge_false,
4369 UNKNOWN_LOCATION);
4370 if (edge_true_true)
4371 add_phi_arg (phi, this_ovf2, edge_true_true,
4372 UNKNOWN_LOCATION);
4373 ovf = t;
4374 if (!check_zero && cmp_code != GT_EXPR)
4375 {
4376 t = cmp_out ? cmp_out : make_ssa_name (m_limb_type);
4377 phi = create_phi_node (t, edge_true_false->dest);
4378 add_phi_arg (phi, l, edge_true_false, UNKNOWN_LOCATION);
4379 add_phi_arg (phi, cmp, edge_false, UNKNOWN_LOCATION);
4380 if (edge_true_true)
4381 add_phi_arg (phi, cmp, edge_true_true,
4382 UNKNOWN_LOCATION);
4383 cmp = t;
4384 }
4385 }
4386 }
4387 }
4388
4389 if (var || obj)
4390 {
4391 if (tree_fits_uhwi_p (idx) && tree_to_uhwi (idx) >= prec_limbs)
4392 ;
4393 else if (!tree_fits_uhwi_p (idx)
4394 && (unsigned) prec < (fin - (i == 0)) * limb_prec)
4395 {
4396 bool single_comparison
4397 = (((unsigned) prec % limb_prec) == 0
4398 || prec_limbs + 1 >= fin
4399 || (prec_limbs & 1) == (i & 1));
4400 g = gimple_build_cond (LE_EXPR, idx, size_int (prec_limbs - 1),
4401 NULL_TREE, NULL_TREE);
4402 gimple *g2 = NULL;
4403 if (!single_comparison)
4404 g2 = gimple_build_cond (EQ_EXPR, idx,
4405 size_int (prec_limbs - 1),
4406 NULL_TREE, NULL_TREE);
4407 edge edge_true_true, edge_true_false, edge_false;
4408 if_then_if_then_else (g, g2, profile_probability::likely (),
4409 profile_probability::unlikely (),
4410 edge_true_true, edge_true_false,
4411 edge_false);
4412 tree l = limb_access (type, var ? var : obj, idx, true);
4413 g = gimple_build_assign (l, rhs);
4414 insert_before (g);
4415 if (!single_comparison)
4416 {
4417 m_gsi = gsi_after_labels (edge_true_true->src);
4418 tree plm1idx = size_int (prec_limbs - 1);
4419 tree plm1type = limb_access_type (type, plm1idx);
4420 l = limb_access (type, var ? var : obj, plm1idx, true);
4421 if (!useless_type_conversion_p (plm1type, TREE_TYPE (rhs)))
4422 rhs = add_cast (plm1type, rhs);
4423 if (!useless_type_conversion_p (TREE_TYPE (l),
4424 TREE_TYPE (rhs)))
4425 rhs = add_cast (TREE_TYPE (l), rhs);
4426 g = gimple_build_assign (l, rhs);
4427 insert_before (g);
4428 }
4429 m_gsi = gsi_after_labels (edge_true_false->dest);
4430 }
4431 else
4432 {
4433 tree l = limb_access (type, var ? var : obj, idx, true);
4434 if (!useless_type_conversion_p (TREE_TYPE (l), TREE_TYPE (rhs)))
4435 rhs = add_cast (TREE_TYPE (l), rhs);
4436 g = gimple_build_assign (l, rhs);
4437 insert_before (g);
4438 }
4439 }
4440 m_first = false;
4441 if (kind == bitint_prec_huge && i <= 1)
4442 {
4443 if (i == 0)
4444 {
4445 idx = make_ssa_name (sizetype);
4446 g = gimple_build_assign (idx, PLUS_EXPR, idx_first,
4447 size_one_node);
4448 insert_before (g);
4449 }
4450 else
4451 {
4452 g = gimple_build_assign (idx_next, PLUS_EXPR, idx_first,
4453 size_int (2));
4454 insert_before (g);
4455 g = gimple_build_cond (NE_EXPR, idx_next, size_int (fin),
4456 NULL_TREE, NULL_TREE);
4457 insert_before (g);
4458 m_gsi = gsi_for_stmt (final_stmt);
4459 m_bb = NULL;
4460 }
4461 }
4462 }
4463
4464 finish_arith_overflow (var, obj, type, ovf, lhs, orig_obj, stmt, code);
4465 }
4466
4467 /* Lower a .MUL_OVERFLOW call with at least one large/huge _BitInt
4468 argument or return type _Complex large/huge _BitInt. */
4469
4470 void
4471 bitint_large_huge::lower_mul_overflow (tree obj, gimple *stmt)
4472 {
4473 tree arg0 = gimple_call_arg (stmt, 0);
4474 tree arg1 = gimple_call_arg (stmt, 1);
4475 tree lhs = gimple_call_lhs (stmt);
4476 if (!lhs)
4477 {
4478 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
4479 gsi_remove (&gsi, true);
4480 return;
4481 }
4482 gimple *final_stmt = gsi_stmt (m_gsi);
4483 tree type = TREE_TYPE (lhs);
4484 if (TREE_CODE (type) == COMPLEX_TYPE)
4485 type = TREE_TYPE (type);
4486 int prec = TYPE_PRECISION (type), prec0, prec1;
4487 arg0 = handle_operand_addr (arg0, stmt, NULL, &prec0);
4488 arg1 = handle_operand_addr (arg1, stmt, NULL, &prec1);
4489 int prec2 = ((prec0 < 0 ? -prec0 : prec0)
4490 + (prec1 < 0 ? -prec1 : prec1));
4491 if (prec0 == 1 || prec1 == 1)
4492 --prec2;
4493 tree var = NULL_TREE;
4494 tree orig_obj = obj;
4495 bool force_var = false;
4496 if (obj == NULL_TREE
4497 && TREE_CODE (type) == BITINT_TYPE
4498 && bitint_precision_kind (type) >= bitint_prec_large
4499 && m_names
4500 && bitmap_bit_p (m_names, SSA_NAME_VERSION (lhs)))
4501 {
4502 int part = var_to_partition (m_map, lhs);
4503 gcc_assert (m_vars[part] != NULL_TREE);
4504 obj = m_vars[part];
4505 if (TREE_TYPE (lhs) == type)
4506 orig_obj = obj;
4507 }
4508 else if (obj != NULL_TREE && DECL_P (obj))
4509 {
4510 for (int i = 0; i < 2; ++i)
4511 {
4512 tree arg = i ? arg1 : arg0;
4513 if (TREE_CODE (arg) == ADDR_EXPR)
4514 arg = TREE_OPERAND (arg, 0);
4515 if (get_base_address (arg) == obj)
4516 {
4517 force_var = true;
4518 break;
4519 }
4520 }
4521 }
4522 if (obj == NULL_TREE
4523 || force_var
4524 || TREE_CODE (type) != BITINT_TYPE
4525 || bitint_precision_kind (type) < bitint_prec_large
4526 || prec2 > (CEIL (prec, limb_prec) * limb_prec * (orig_obj ? 1 : 2)))
4527 {
4528 unsigned HOST_WIDE_INT nelts = CEIL (MAX (prec, prec2), limb_prec);
4529 tree atype = build_array_type_nelts (m_limb_type, nelts);
4530 var = create_tmp_var (atype);
4531 }
4532 tree addr = build_fold_addr_expr (var ? var : obj);
4533 addr = force_gimple_operand_gsi (&m_gsi, addr, true,
4534 NULL_TREE, true, GSI_SAME_STMT);
4535 tree sitype = lang_hooks.types.type_for_mode (SImode, 0);
4536 gimple *g
4537 = gimple_build_call_internal (IFN_MULBITINT, 6,
4538 addr, build_int_cst (sitype,
4539 MAX (prec2, prec)),
4540 arg0, build_int_cst (sitype, prec0),
4541 arg1, build_int_cst (sitype, prec1));
4542 insert_before (g);
4543
4544 unsigned start, end;
4545 bool check_zero;
4546 tree ovf = arith_overflow (MULT_EXPR, type, prec, prec0, prec1, prec2,
4547 &start, &end, &check_zero);
4548 if (ovf == NULL_TREE)
4549 {
4550 unsigned startlimb = start / limb_prec;
4551 unsigned endlimb = (end - 1) / limb_prec;
4552 unsigned cnt;
4553 bool use_loop = false;
4554 if (startlimb == endlimb)
4555 cnt = 1;
4556 else if (startlimb + 1 == endlimb)
4557 cnt = 2;
4558 else if ((end % limb_prec) == 0)
4559 {
4560 cnt = 2;
4561 use_loop = true;
4562 }
4563 else
4564 {
4565 cnt = 3;
4566 use_loop = startlimb + 2 < endlimb;
4567 }
4568 if (cnt == 1)
4569 {
4570 tree l = limb_access (NULL_TREE, var ? var : obj,
4571 size_int (startlimb), true);
4572 g = gimple_build_assign (make_ssa_name (m_limb_type), l);
4573 insert_before (g);
4574 l = arith_overflow_extract_bits (start, end, gimple_assign_lhs (g),
4575 startlimb, check_zero);
4576 ovf = make_ssa_name (boolean_type_node);
4577 if (check_zero)
4578 g = gimple_build_assign (ovf, NE_EXPR, l,
4579 build_zero_cst (m_limb_type));
4580 else
4581 {
4582 g = gimple_build_assign (make_ssa_name (m_limb_type),
4583 PLUS_EXPR, l,
4584 build_int_cst (m_limb_type, 1));
4585 insert_before (g);
4586 g = gimple_build_assign (ovf, GT_EXPR, gimple_assign_lhs (g),
4587 build_int_cst (m_limb_type, 1));
4588 }
4589 insert_before (g);
4590 }
4591 else
4592 {
4593 basic_block edge_bb = NULL;
4594 gimple_stmt_iterator gsi = m_gsi;
4595 gsi_prev (&gsi);
4596 edge e = split_block (gsi_bb (gsi), gsi_stmt (gsi));
4597 edge_bb = e->src;
4598 m_gsi = gsi_end_bb (edge_bb);
4599
4600 tree cmp = build_zero_cst (m_limb_type);
4601 for (unsigned i = 0; i < cnt; i++)
4602 {
4603 tree idx, idx_next = NULL_TREE;
4604 if (i == 0)
4605 idx = size_int (startlimb);
4606 else if (i == 2)
4607 idx = size_int (endlimb);
4608 else if (use_loop)
4609 idx = create_loop (size_int (startlimb + 1), &idx_next);
4610 else
4611 idx = size_int (startlimb + 1);
4612 tree l = limb_access (NULL_TREE, var ? var : obj, idx, true);
4613 g = gimple_build_assign (make_ssa_name (m_limb_type), l);
4614 insert_before (g);
4615 l = gimple_assign_lhs (g);
4616 if (i == 0 || i == 2)
4617 l = arith_overflow_extract_bits (start, end, l,
4618 tree_to_uhwi (idx),
4619 check_zero);
4620 if (i == 0 && !check_zero)
4621 {
4622 cmp = l;
4623 g = gimple_build_assign (make_ssa_name (m_limb_type),
4624 PLUS_EXPR, l,
4625 build_int_cst (m_limb_type, 1));
4626 insert_before (g);
4627 g = gimple_build_cond (GT_EXPR, gimple_assign_lhs (g),
4628 build_int_cst (m_limb_type, 1),
4629 NULL_TREE, NULL_TREE);
4630 }
4631 else
4632 g = gimple_build_cond (NE_EXPR, l, cmp, NULL_TREE, NULL_TREE);
4633 insert_before (g);
4634 edge e1 = split_block (gsi_bb (m_gsi), g);
4635 e1->flags = EDGE_FALSE_VALUE;
4636 edge e2 = make_edge (e1->src, gimple_bb (final_stmt),
4637 EDGE_TRUE_VALUE);
4638 e1->probability = profile_probability::likely ();
4639 e2->probability = e1->probability.invert ();
4640 if (i == 0)
4641 set_immediate_dominator (CDI_DOMINATORS, e2->dest, e2->src);
4642 m_gsi = gsi_after_labels (e1->dest);
4643 if (i == 1 && use_loop)
4644 {
4645 g = gimple_build_assign (idx_next, PLUS_EXPR, idx,
4646 size_one_node);
4647 insert_before (g);
4648 g = gimple_build_cond (NE_EXPR, idx_next,
4649 size_int (endlimb + (cnt == 2)),
4650 NULL_TREE, NULL_TREE);
4651 insert_before (g);
4652 edge true_edge, false_edge;
4653 extract_true_false_edges_from_block (gsi_bb (m_gsi),
4654 &true_edge,
4655 &false_edge);
4656 m_gsi = gsi_after_labels (false_edge->dest);
4657 m_bb = NULL;
4658 }
4659 }
4660
4661 ovf = make_ssa_name (boolean_type_node);
4662 basic_block bb = gimple_bb (final_stmt);
4663 gphi *phi = create_phi_node (ovf, bb);
4664 edge e1 = find_edge (gsi_bb (m_gsi), bb);
4665 edge_iterator ei;
4666 FOR_EACH_EDGE (e, ei, bb->preds)
4667 {
4668 tree val = e == e1 ? boolean_false_node : boolean_true_node;
4669 add_phi_arg (phi, val, e, UNKNOWN_LOCATION);
4670 }
4671 m_gsi = gsi_for_stmt (final_stmt);
4672 }
4673 }
4674
4675 finish_arith_overflow (var, obj, type, ovf, lhs, orig_obj, stmt, MULT_EXPR);
4676 }
4677
4678 /* Lower REALPART_EXPR or IMAGPART_EXPR stmt extracting part of result from
4679 .{ADD,SUB,MUL}_OVERFLOW call. */
4680
4681 void
4682 bitint_large_huge::lower_cplxpart_stmt (tree obj, gimple *stmt)
4683 {
4684 tree rhs1 = gimple_assign_rhs1 (stmt);
4685 rhs1 = TREE_OPERAND (rhs1, 0);
4686 if (obj == NULL_TREE)
4687 {
4688 int part = var_to_partition (m_map, gimple_assign_lhs (stmt));
4689 gcc_assert (m_vars[part] != NULL_TREE);
4690 obj = m_vars[part];
4691 }
4692 if (TREE_CODE (rhs1) == SSA_NAME
4693 && (m_names == NULL
4694 || !bitmap_bit_p (m_names, SSA_NAME_VERSION (rhs1))))
4695 {
4696 lower_call (obj, SSA_NAME_DEF_STMT (rhs1));
4697 return;
4698 }
4699 int part = var_to_partition (m_map, rhs1);
4700 gcc_assert (m_vars[part] != NULL_TREE);
4701 tree var = m_vars[part];
4702 unsigned HOST_WIDE_INT nelts
4703 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (obj))) / limb_prec;
4704 tree atype = build_array_type_nelts (m_limb_type, nelts);
4705 if (!useless_type_conversion_p (atype, TREE_TYPE (obj)))
4706 obj = build1 (VIEW_CONVERT_EXPR, atype, obj);
4707 tree off = build_int_cst (build_pointer_type (TREE_TYPE (var)),
4708 gimple_assign_rhs_code (stmt) == REALPART_EXPR
4709 ? 0 : nelts * m_limb_size);
4710 tree v2 = build2 (MEM_REF, atype, build_fold_addr_expr (var), off);
4711 gimple *g = gimple_build_assign (obj, v2);
4712 insert_before (g);
4713 }
4714
4715 /* Lower COMPLEX_EXPR stmt. */
4716
4717 void
4718 bitint_large_huge::lower_complexexpr_stmt (gimple *stmt)
4719 {
4720 tree lhs = gimple_assign_lhs (stmt);
4721 tree rhs1 = gimple_assign_rhs1 (stmt);
4722 tree rhs2 = gimple_assign_rhs2 (stmt);
4723 int part = var_to_partition (m_map, lhs);
4724 gcc_assert (m_vars[part] != NULL_TREE);
4725 lhs = m_vars[part];
4726 unsigned HOST_WIDE_INT nelts
4727 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (rhs1))) / limb_prec;
4728 tree atype = build_array_type_nelts (m_limb_type, nelts);
4729 tree zero = build_zero_cst (build_pointer_type (TREE_TYPE (lhs)));
4730 tree v1 = build2 (MEM_REF, atype, build_fold_addr_expr (lhs), zero);
4731 tree v2;
4732 if (TREE_CODE (rhs1) == SSA_NAME)
4733 {
4734 part = var_to_partition (m_map, rhs1);
4735 gcc_assert (m_vars[part] != NULL_TREE);
4736 v2 = m_vars[part];
4737 }
4738 else if (integer_zerop (rhs1))
4739 v2 = build_zero_cst (atype);
4740 else
4741 v2 = tree_output_constant_def (rhs1);
4742 if (!useless_type_conversion_p (atype, TREE_TYPE (v2)))
4743 v2 = build1 (VIEW_CONVERT_EXPR, atype, v2);
4744 gimple *g = gimple_build_assign (v1, v2);
4745 insert_before (g);
4746 tree off = fold_convert (build_pointer_type (TREE_TYPE (lhs)),
4747 TYPE_SIZE_UNIT (atype));
4748 v1 = build2 (MEM_REF, atype, build_fold_addr_expr (lhs), off);
4749 if (TREE_CODE (rhs2) == SSA_NAME)
4750 {
4751 part = var_to_partition (m_map, rhs2);
4752 gcc_assert (m_vars[part] != NULL_TREE);
4753 v2 = m_vars[part];
4754 }
4755 else if (integer_zerop (rhs2))
4756 v2 = build_zero_cst (atype);
4757 else
4758 v2 = tree_output_constant_def (rhs2);
4759 if (!useless_type_conversion_p (atype, TREE_TYPE (v2)))
4760 v2 = build1 (VIEW_CONVERT_EXPR, atype, v2);
4761 g = gimple_build_assign (v1, v2);
4762 insert_before (g);
4763 }
4764
4765 /* Lower a .{CLZ,CTZ,CLRSB,FFS,PARITY,POPCOUNT} call with one large/huge _BitInt
4766 argument. */
4767
4768 void
4769 bitint_large_huge::lower_bit_query (gimple *stmt)
4770 {
4771 tree arg0 = gimple_call_arg (stmt, 0);
4772 tree arg1 = (gimple_call_num_args (stmt) == 2
4773 ? gimple_call_arg (stmt, 1) : NULL_TREE);
4774 tree lhs = gimple_call_lhs (stmt);
4775 gimple *g;
4776
4777 if (!lhs)
4778 {
4779 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
4780 gsi_remove (&gsi, true);
4781 return;
4782 }
4783 tree type = TREE_TYPE (arg0);
4784 gcc_assert (TREE_CODE (type) == BITINT_TYPE);
4785 bitint_prec_kind kind = bitint_precision_kind (type);
4786 gcc_assert (kind >= bitint_prec_large);
4787 enum internal_fn ifn = gimple_call_internal_fn (stmt);
4788 enum built_in_function fcode = END_BUILTINS;
4789 gcc_assert (TYPE_PRECISION (unsigned_type_node) == limb_prec
4790 || TYPE_PRECISION (long_unsigned_type_node) == limb_prec
4791 || TYPE_PRECISION (long_long_unsigned_type_node) == limb_prec);
4792 switch (ifn)
4793 {
4794 case IFN_CLZ:
4795 if (TYPE_PRECISION (unsigned_type_node) == limb_prec)
4796 fcode = BUILT_IN_CLZ;
4797 else if (TYPE_PRECISION (long_unsigned_type_node) == limb_prec)
4798 fcode = BUILT_IN_CLZL;
4799 else
4800 fcode = BUILT_IN_CLZLL;
4801 break;
4802 case IFN_FFS:
4803 /* .FFS (X) is .CTZ (X, -1) + 1, though under the hood
4804 we don't add the addend at the end. */
4805 arg1 = integer_zero_node;
4806 /* FALLTHRU */
4807 case IFN_CTZ:
4808 if (TYPE_PRECISION (unsigned_type_node) == limb_prec)
4809 fcode = BUILT_IN_CTZ;
4810 else if (TYPE_PRECISION (long_unsigned_type_node) == limb_prec)
4811 fcode = BUILT_IN_CTZL;
4812 else
4813 fcode = BUILT_IN_CTZLL;
4814 m_upwards = true;
4815 break;
4816 case IFN_CLRSB:
4817 if (TYPE_PRECISION (unsigned_type_node) == limb_prec)
4818 fcode = BUILT_IN_CLRSB;
4819 else if (TYPE_PRECISION (long_unsigned_type_node) == limb_prec)
4820 fcode = BUILT_IN_CLRSBL;
4821 else
4822 fcode = BUILT_IN_CLRSBLL;
4823 break;
4824 case IFN_PARITY:
4825 if (TYPE_PRECISION (unsigned_type_node) == limb_prec)
4826 fcode = BUILT_IN_PARITY;
4827 else if (TYPE_PRECISION (long_unsigned_type_node) == limb_prec)
4828 fcode = BUILT_IN_PARITYL;
4829 else
4830 fcode = BUILT_IN_PARITYLL;
4831 m_upwards = true;
4832 break;
4833 case IFN_POPCOUNT:
4834 if (TYPE_PRECISION (unsigned_type_node) == limb_prec)
4835 fcode = BUILT_IN_POPCOUNT;
4836 else if (TYPE_PRECISION (long_unsigned_type_node) == limb_prec)
4837 fcode = BUILT_IN_POPCOUNTL;
4838 else
4839 fcode = BUILT_IN_POPCOUNTLL;
4840 m_upwards = true;
4841 break;
4842 default:
4843 gcc_unreachable ();
4844 }
4845 tree fndecl = builtin_decl_explicit (fcode), res = NULL_TREE;
4846 unsigned cnt = 0, rem = 0, end = 0, prec = TYPE_PRECISION (type);
4847 struct bq_details { edge e; tree val, addend; } *bqp = NULL;
4848 basic_block edge_bb = NULL;
4849 if (m_upwards)
4850 {
4851 tree idx = NULL_TREE, idx_first = NULL_TREE, idx_next = NULL_TREE;
4852 if (kind == bitint_prec_large)
4853 cnt = CEIL (prec, limb_prec);
4854 else
4855 {
4856 rem = (prec % (2 * limb_prec));
4857 end = (prec - rem) / limb_prec;
4858 cnt = 2 + CEIL (rem, limb_prec);
4859 idx = idx_first = create_loop (size_zero_node, &idx_next);
4860 }
4861
4862 if (ifn == IFN_CTZ || ifn == IFN_FFS)
4863 {
4864 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
4865 gsi_prev (&gsi);
4866 edge e = split_block (gsi_bb (gsi), gsi_stmt (gsi));
4867 edge_bb = e->src;
4868 if (kind == bitint_prec_large)
4869 m_gsi = gsi_end_bb (edge_bb);
4870 bqp = XALLOCAVEC (struct bq_details, cnt);
4871 }
4872 else
4873 m_after_stmt = stmt;
4874 if (kind != bitint_prec_large)
4875 m_upwards_2limb = end;
4876
4877 for (unsigned i = 0; i < cnt; i++)
4878 {
4879 m_data_cnt = 0;
4880 if (kind == bitint_prec_large)
4881 idx = size_int (i);
4882 else if (i >= 2)
4883 idx = size_int (end + (i > 2));
4884
4885 tree rhs1 = handle_operand (arg0, idx);
4886 if (!useless_type_conversion_p (m_limb_type, TREE_TYPE (rhs1)))
4887 {
4888 if (!TYPE_UNSIGNED (TREE_TYPE (rhs1)))
4889 rhs1 = add_cast (unsigned_type_for (TREE_TYPE (rhs1)), rhs1);
4890 rhs1 = add_cast (m_limb_type, rhs1);
4891 }
4892
4893 tree in, out, tem;
4894 if (ifn == IFN_PARITY)
4895 in = prepare_data_in_out (build_zero_cst (m_limb_type), idx, &out);
4896 else if (ifn == IFN_FFS)
4897 in = prepare_data_in_out (integer_one_node, idx, &out);
4898 else
4899 in = prepare_data_in_out (integer_zero_node, idx, &out);
4900
4901 switch (ifn)
4902 {
4903 case IFN_CTZ:
4904 case IFN_FFS:
4905 g = gimple_build_cond (NE_EXPR, rhs1,
4906 build_zero_cst (m_limb_type),
4907 NULL_TREE, NULL_TREE);
4908 insert_before (g);
4909 edge e1, e2;
4910 e1 = split_block (gsi_bb (m_gsi), g);
4911 e1->flags = EDGE_FALSE_VALUE;
4912 e2 = make_edge (e1->src, gimple_bb (stmt), EDGE_TRUE_VALUE);
4913 e1->probability = profile_probability::unlikely ();
4914 e2->probability = e1->probability.invert ();
4915 if (i == 0)
4916 set_immediate_dominator (CDI_DOMINATORS, e2->dest, e2->src);
4917 m_gsi = gsi_after_labels (e1->dest);
4918 bqp[i].e = e2;
4919 bqp[i].val = rhs1;
4920 if (tree_fits_uhwi_p (idx))
4921 bqp[i].addend
4922 = build_int_cst (integer_type_node,
4923 tree_to_uhwi (idx) * limb_prec
4924 + (ifn == IFN_FFS));
4925 else
4926 {
4927 bqp[i].addend = in;
4928 if (i == 1)
4929 res = out;
4930 else
4931 res = make_ssa_name (integer_type_node);
4932 g = gimple_build_assign (res, PLUS_EXPR, in,
4933 build_int_cst (integer_type_node,
4934 limb_prec));
4935 insert_before (g);
4936 m_data[m_data_cnt] = res;
4937 }
4938 break;
4939 case IFN_PARITY:
4940 if (!integer_zerop (in))
4941 {
4942 if (kind == bitint_prec_huge && i == 1)
4943 res = out;
4944 else
4945 res = make_ssa_name (m_limb_type);
4946 g = gimple_build_assign (res, BIT_XOR_EXPR, in, rhs1);
4947 insert_before (g);
4948 }
4949 else
4950 res = rhs1;
4951 m_data[m_data_cnt] = res;
4952 break;
4953 case IFN_POPCOUNT:
4954 g = gimple_build_call (fndecl, 1, rhs1);
4955 tem = make_ssa_name (integer_type_node);
4956 gimple_call_set_lhs (g, tem);
4957 insert_before (g);
4958 if (!integer_zerop (in))
4959 {
4960 if (kind == bitint_prec_huge && i == 1)
4961 res = out;
4962 else
4963 res = make_ssa_name (integer_type_node);
4964 g = gimple_build_assign (res, PLUS_EXPR, in, tem);
4965 insert_before (g);
4966 }
4967 else
4968 res = tem;
4969 m_data[m_data_cnt] = res;
4970 break;
4971 default:
4972 gcc_unreachable ();
4973 }
4974
4975 m_first = false;
4976 if (kind == bitint_prec_huge && i <= 1)
4977 {
4978 if (i == 0)
4979 {
4980 idx = make_ssa_name (sizetype);
4981 g = gimple_build_assign (idx, PLUS_EXPR, idx_first,
4982 size_one_node);
4983 insert_before (g);
4984 }
4985 else
4986 {
4987 g = gimple_build_assign (idx_next, PLUS_EXPR, idx_first,
4988 size_int (2));
4989 insert_before (g);
4990 g = gimple_build_cond (NE_EXPR, idx_next, size_int (end),
4991 NULL_TREE, NULL_TREE);
4992 insert_before (g);
4993 if (ifn == IFN_CTZ || ifn == IFN_FFS)
4994 m_gsi = gsi_after_labels (edge_bb);
4995 else
4996 m_gsi = gsi_for_stmt (stmt);
4997 m_bb = NULL;
4998 }
4999 }
5000 }
5001 }
5002 else
5003 {
5004 tree idx = NULL_TREE, idx_next = NULL_TREE, first = NULL_TREE;
5005 int sub_one = 0;
5006 if (kind == bitint_prec_large)
5007 cnt = CEIL (prec, limb_prec);
5008 else
5009 {
5010 rem = prec % limb_prec;
5011 if (rem == 0 && (!TYPE_UNSIGNED (type) || ifn == IFN_CLRSB))
5012 rem = limb_prec;
5013 end = (prec - rem) / limb_prec;
5014 cnt = 1 + (rem != 0);
5015 if (ifn == IFN_CLRSB)
5016 sub_one = 1;
5017 }
5018
5019 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
5020 gsi_prev (&gsi);
5021 edge e = split_block (gsi_bb (gsi), gsi_stmt (gsi));
5022 edge_bb = e->src;
5023 m_gsi = gsi_end_bb (edge_bb);
5024
5025 if (ifn == IFN_CLZ)
5026 bqp = XALLOCAVEC (struct bq_details, cnt);
5027 else
5028 {
5029 gsi = gsi_for_stmt (stmt);
5030 gsi_prev (&gsi);
5031 e = split_block (gsi_bb (gsi), gsi_stmt (gsi));
5032 edge_bb = e->src;
5033 bqp = XALLOCAVEC (struct bq_details, 2 * cnt);
5034 }
5035
5036 for (unsigned i = 0; i < cnt; i++)
5037 {
5038 m_data_cnt = 0;
5039 if (kind == bitint_prec_large)
5040 idx = size_int (cnt - i - 1);
5041 else if (i == cnt - 1)
5042 idx = create_loop (size_int (end - 1), &idx_next);
5043 else
5044 idx = size_int (end);
5045
5046 tree rhs1 = handle_operand (arg0, idx);
5047 if (!useless_type_conversion_p (m_limb_type, TREE_TYPE (rhs1)))
5048 {
5049 if (ifn == IFN_CLZ && !TYPE_UNSIGNED (TREE_TYPE (rhs1)))
5050 rhs1 = add_cast (unsigned_type_for (TREE_TYPE (rhs1)), rhs1);
5051 else if (ifn == IFN_CLRSB && TYPE_UNSIGNED (TREE_TYPE (rhs1)))
5052 rhs1 = add_cast (signed_type_for (TREE_TYPE (rhs1)), rhs1);
5053 rhs1 = add_cast (m_limb_type, rhs1);
5054 }
5055
5056 if (ifn == IFN_CLZ)
5057 {
5058 g = gimple_build_cond (NE_EXPR, rhs1,
5059 build_zero_cst (m_limb_type),
5060 NULL_TREE, NULL_TREE);
5061 insert_before (g);
5062 edge e1 = split_block (gsi_bb (m_gsi), g);
5063 e1->flags = EDGE_FALSE_VALUE;
5064 edge e2 = make_edge (e1->src, gimple_bb (stmt), EDGE_TRUE_VALUE);
5065 e1->probability = profile_probability::unlikely ();
5066 e2->probability = e1->probability.invert ();
5067 if (i == 0)
5068 set_immediate_dominator (CDI_DOMINATORS, e2->dest, e2->src);
5069 m_gsi = gsi_after_labels (e1->dest);
5070 bqp[i].e = e2;
5071 bqp[i].val = rhs1;
5072 }
5073 else
5074 {
5075 if (i == 0)
5076 {
5077 first = rhs1;
5078 g = gimple_build_assign (make_ssa_name (m_limb_type),
5079 PLUS_EXPR, rhs1,
5080 build_int_cst (m_limb_type, 1));
5081 insert_before (g);
5082 g = gimple_build_cond (GT_EXPR, gimple_assign_lhs (g),
5083 build_int_cst (m_limb_type, 1),
5084 NULL_TREE, NULL_TREE);
5085 insert_before (g);
5086 }
5087 else
5088 {
5089 g = gimple_build_assign (make_ssa_name (m_limb_type),
5090 BIT_XOR_EXPR, rhs1, first);
5091 insert_before (g);
5092 tree stype = signed_type_for (m_limb_type);
5093 g = gimple_build_cond (LT_EXPR,
5094 add_cast (stype,
5095 gimple_assign_lhs (g)),
5096 build_zero_cst (stype),
5097 NULL_TREE, NULL_TREE);
5098 insert_before (g);
5099 edge e1 = split_block (gsi_bb (m_gsi), g);
5100 e1->flags = EDGE_FALSE_VALUE;
5101 edge e2 = make_edge (e1->src, gimple_bb (stmt),
5102 EDGE_TRUE_VALUE);
5103 e1->probability = profile_probability::unlikely ();
5104 e2->probability = e1->probability.invert ();
5105 if (i == 1)
5106 set_immediate_dominator (CDI_DOMINATORS, e2->dest,
5107 e2->src);
5108 m_gsi = gsi_after_labels (e1->dest);
5109 bqp[2 * i].e = e2;
5110 g = gimple_build_cond (NE_EXPR, rhs1, first,
5111 NULL_TREE, NULL_TREE);
5112 insert_before (g);
5113 }
5114 edge e1 = split_block (gsi_bb (m_gsi), g);
5115 e1->flags = EDGE_FALSE_VALUE;
5116 edge e2 = make_edge (e1->src, edge_bb, EDGE_TRUE_VALUE);
5117 e1->probability = profile_probability::unlikely ();
5118 e2->probability = e1->probability.invert ();
5119 if (i == 0)
5120 set_immediate_dominator (CDI_DOMINATORS, e2->dest, e2->src);
5121 m_gsi = gsi_after_labels (e1->dest);
5122 bqp[2 * i + 1].e = e2;
5123 bqp[i].val = rhs1;
5124 }
5125 if (tree_fits_uhwi_p (idx))
5126 bqp[i].addend
5127 = build_int_cst (integer_type_node,
5128 (int) prec
5129 - (((int) tree_to_uhwi (idx) + 1)
5130 * limb_prec) - sub_one);
5131 else
5132 {
5133 tree in, out;
5134 in = build_int_cst (integer_type_node, rem - sub_one);
5135 m_first = true;
5136 in = prepare_data_in_out (in, idx, &out);
5137 out = m_data[m_data_cnt + 1];
5138 bqp[i].addend = in;
5139 g = gimple_build_assign (out, PLUS_EXPR, in,
5140 build_int_cst (integer_type_node,
5141 limb_prec));
5142 insert_before (g);
5143 m_data[m_data_cnt] = out;
5144 }
5145
5146 m_first = false;
5147 if (kind == bitint_prec_huge && i == cnt - 1)
5148 {
5149 g = gimple_build_assign (idx_next, PLUS_EXPR, idx,
5150 size_int (-1));
5151 insert_before (g);
5152 g = gimple_build_cond (NE_EXPR, idx, size_zero_node,
5153 NULL_TREE, NULL_TREE);
5154 insert_before (g);
5155 edge true_edge, false_edge;
5156 extract_true_false_edges_from_block (gsi_bb (m_gsi),
5157 &true_edge, &false_edge);
5158 m_gsi = gsi_after_labels (false_edge->dest);
5159 m_bb = NULL;
5160 }
5161 }
5162 }
5163 switch (ifn)
5164 {
5165 case IFN_CLZ:
5166 case IFN_CTZ:
5167 case IFN_FFS:
5168 gphi *phi1, *phi2, *phi3;
5169 basic_block bb;
5170 bb = gsi_bb (m_gsi);
5171 remove_edge (find_edge (bb, gimple_bb (stmt)));
5172 phi1 = create_phi_node (make_ssa_name (m_limb_type),
5173 gimple_bb (stmt));
5174 phi2 = create_phi_node (make_ssa_name (integer_type_node),
5175 gimple_bb (stmt));
5176 for (unsigned i = 0; i < cnt; i++)
5177 {
5178 add_phi_arg (phi1, bqp[i].val, bqp[i].e, UNKNOWN_LOCATION);
5179 add_phi_arg (phi2, bqp[i].addend, bqp[i].e, UNKNOWN_LOCATION);
5180 }
5181 if (arg1 == NULL_TREE)
5182 {
5183 g = gimple_build_builtin_unreachable (m_loc);
5184 insert_before (g);
5185 }
5186 m_gsi = gsi_for_stmt (stmt);
5187 g = gimple_build_call (fndecl, 1, gimple_phi_result (phi1));
5188 gimple_call_set_lhs (g, make_ssa_name (integer_type_node));
5189 insert_before (g);
5190 if (arg1 == NULL_TREE)
5191 g = gimple_build_assign (lhs, PLUS_EXPR,
5192 gimple_phi_result (phi2),
5193 gimple_call_lhs (g));
5194 else
5195 {
5196 g = gimple_build_assign (make_ssa_name (integer_type_node),
5197 PLUS_EXPR, gimple_phi_result (phi2),
5198 gimple_call_lhs (g));
5199 insert_before (g);
5200 edge e1 = split_block (gimple_bb (stmt), g);
5201 edge e2 = make_edge (bb, e1->dest, EDGE_FALLTHRU);
5202 e2->probability = profile_probability::always ();
5203 set_immediate_dominator (CDI_DOMINATORS, e1->dest,
5204 get_immediate_dominator (CDI_DOMINATORS,
5205 e1->src));
5206 phi3 = create_phi_node (make_ssa_name (integer_type_node), e1->dest);
5207 add_phi_arg (phi3, gimple_assign_lhs (g), e1, UNKNOWN_LOCATION);
5208 add_phi_arg (phi3, arg1, e2, UNKNOWN_LOCATION);
5209 m_gsi = gsi_for_stmt (stmt);
5210 g = gimple_build_assign (lhs, gimple_phi_result (phi3));
5211 }
5212 gsi_replace (&m_gsi, g, true);
5213 break;
5214 case IFN_CLRSB:
5215 bb = gsi_bb (m_gsi);
5216 remove_edge (find_edge (bb, edge_bb));
5217 edge e;
5218 e = make_edge (bb, gimple_bb (stmt), EDGE_FALLTHRU);
5219 e->probability = profile_probability::always ();
5220 set_immediate_dominator (CDI_DOMINATORS, gimple_bb (stmt),
5221 get_immediate_dominator (CDI_DOMINATORS,
5222 edge_bb));
5223 phi1 = create_phi_node (make_ssa_name (m_limb_type),
5224 edge_bb);
5225 phi2 = create_phi_node (make_ssa_name (integer_type_node),
5226 edge_bb);
5227 phi3 = create_phi_node (make_ssa_name (integer_type_node),
5228 gimple_bb (stmt));
5229 for (unsigned i = 0; i < cnt; i++)
5230 {
5231 add_phi_arg (phi1, bqp[i].val, bqp[2 * i + 1].e, UNKNOWN_LOCATION);
5232 add_phi_arg (phi2, bqp[i].addend, bqp[2 * i + 1].e,
5233 UNKNOWN_LOCATION);
5234 tree a = bqp[i].addend;
5235 if (i && kind == bitint_prec_large)
5236 a = int_const_binop (PLUS_EXPR, a, integer_minus_one_node);
5237 if (i)
5238 add_phi_arg (phi3, a, bqp[2 * i].e, UNKNOWN_LOCATION);
5239 }
5240 add_phi_arg (phi3, build_int_cst (integer_type_node, prec - 1), e,
5241 UNKNOWN_LOCATION);
5242 m_gsi = gsi_after_labels (edge_bb);
5243 g = gimple_build_call (fndecl, 1,
5244 add_cast (signed_type_for (m_limb_type),
5245 gimple_phi_result (phi1)));
5246 gimple_call_set_lhs (g, make_ssa_name (integer_type_node));
5247 insert_before (g);
5248 g = gimple_build_assign (make_ssa_name (integer_type_node),
5249 PLUS_EXPR, gimple_call_lhs (g),
5250 gimple_phi_result (phi2));
5251 insert_before (g);
5252 if (kind != bitint_prec_large)
5253 {
5254 g = gimple_build_assign (make_ssa_name (integer_type_node),
5255 PLUS_EXPR, gimple_assign_lhs (g),
5256 integer_one_node);
5257 insert_before (g);
5258 }
5259 add_phi_arg (phi3, gimple_assign_lhs (g),
5260 find_edge (edge_bb, gimple_bb (stmt)), UNKNOWN_LOCATION);
5261 m_gsi = gsi_for_stmt (stmt);
5262 g = gimple_build_assign (lhs, gimple_phi_result (phi3));
5263 gsi_replace (&m_gsi, g, true);
5264 break;
5265 case IFN_PARITY:
5266 g = gimple_build_call (fndecl, 1, res);
5267 gimple_call_set_lhs (g, lhs);
5268 gsi_replace (&m_gsi, g, true);
5269 break;
5270 case IFN_POPCOUNT:
5271 g = gimple_build_assign (lhs, res);
5272 gsi_replace (&m_gsi, g, true);
5273 break;
5274 default:
5275 gcc_unreachable ();
5276 }
5277 }
5278
5279 /* Lower a call statement with one or more large/huge _BitInt
5280 arguments or large/huge _BitInt return value. */
5281
5282 void
5283 bitint_large_huge::lower_call (tree obj, gimple *stmt)
5284 {
5285 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
5286 unsigned int nargs = gimple_call_num_args (stmt);
5287 if (gimple_call_internal_p (stmt))
5288 switch (gimple_call_internal_fn (stmt))
5289 {
5290 case IFN_ADD_OVERFLOW:
5291 case IFN_SUB_OVERFLOW:
5292 case IFN_UBSAN_CHECK_ADD:
5293 case IFN_UBSAN_CHECK_SUB:
5294 lower_addsub_overflow (obj, stmt);
5295 return;
5296 case IFN_MUL_OVERFLOW:
5297 case IFN_UBSAN_CHECK_MUL:
5298 lower_mul_overflow (obj, stmt);
5299 return;
5300 case IFN_CLZ:
5301 case IFN_CTZ:
5302 case IFN_CLRSB:
5303 case IFN_FFS:
5304 case IFN_PARITY:
5305 case IFN_POPCOUNT:
5306 lower_bit_query (stmt);
5307 return;
5308 default:
5309 break;
5310 }
5311 bool returns_twice = (gimple_call_flags (stmt) & ECF_RETURNS_TWICE) != 0;
5312 for (unsigned int i = 0; i < nargs; ++i)
5313 {
5314 tree arg = gimple_call_arg (stmt, i);
5315 if (TREE_CODE (arg) != SSA_NAME
5316 || TREE_CODE (TREE_TYPE (arg)) != BITINT_TYPE
5317 || bitint_precision_kind (TREE_TYPE (arg)) <= bitint_prec_middle)
5318 continue;
5319 if (SSA_NAME_IS_DEFAULT_DEF (arg)
5320 && (!SSA_NAME_VAR (arg) || VAR_P (SSA_NAME_VAR (arg))))
5321 {
5322 tree var = create_tmp_reg (TREE_TYPE (arg));
5323 arg = get_or_create_ssa_default_def (cfun, var);
5324 }
5325 else
5326 {
5327 int p = var_to_partition (m_map, arg);
5328 tree v = m_vars[p];
5329 gcc_assert (v != NULL_TREE);
5330 if (!types_compatible_p (TREE_TYPE (arg), TREE_TYPE (v)))
5331 v = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (arg), v);
5332 arg = make_ssa_name (TREE_TYPE (arg));
5333 gimple *g = gimple_build_assign (arg, v);
5334 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
5335 if (returns_twice && bb_has_abnormal_pred (gimple_bb (stmt)))
5336 {
5337 m_returns_twice_calls.safe_push (stmt);
5338 returns_twice = false;
5339 }
5340 }
5341 gimple_call_set_arg (stmt, i, arg);
5342 if (m_preserved == NULL)
5343 m_preserved = BITMAP_ALLOC (NULL);
5344 bitmap_set_bit (m_preserved, SSA_NAME_VERSION (arg));
5345 }
5346 tree lhs = gimple_call_lhs (stmt);
5347 if (lhs
5348 && TREE_CODE (lhs) == SSA_NAME
5349 && TREE_CODE (TREE_TYPE (lhs)) == BITINT_TYPE
5350 && bitint_precision_kind (TREE_TYPE (lhs)) >= bitint_prec_large)
5351 {
5352 int p = var_to_partition (m_map, lhs);
5353 tree v = m_vars[p];
5354 gcc_assert (v != NULL_TREE);
5355 if (!types_compatible_p (TREE_TYPE (lhs), TREE_TYPE (v)))
5356 v = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (lhs), v);
5357 gimple_call_set_lhs (stmt, v);
5358 SSA_NAME_DEF_STMT (lhs) = gimple_build_nop ();
5359 }
5360 update_stmt (stmt);
5361 }
5362
5363 /* Lower __asm STMT which involves large/huge _BitInt values. */
5364
5365 void
5366 bitint_large_huge::lower_asm (gimple *stmt)
5367 {
5368 gasm *g = as_a <gasm *> (stmt);
5369 unsigned noutputs = gimple_asm_noutputs (g);
5370 unsigned ninputs = gimple_asm_ninputs (g);
5371
5372 for (unsigned i = 0; i < noutputs; ++i)
5373 {
5374 tree t = gimple_asm_output_op (g, i);
5375 tree s = TREE_VALUE (t);
5376 if (TREE_CODE (s) == SSA_NAME
5377 && TREE_CODE (TREE_TYPE (s)) == BITINT_TYPE
5378 && bitint_precision_kind (TREE_TYPE (s)) >= bitint_prec_large)
5379 {
5380 int part = var_to_partition (m_map, s);
5381 gcc_assert (m_vars[part] != NULL_TREE);
5382 TREE_VALUE (t) = m_vars[part];
5383 }
5384 }
5385 for (unsigned i = 0; i < ninputs; ++i)
5386 {
5387 tree t = gimple_asm_input_op (g, i);
5388 tree s = TREE_VALUE (t);
5389 if (TREE_CODE (s) == SSA_NAME
5390 && TREE_CODE (TREE_TYPE (s)) == BITINT_TYPE
5391 && bitint_precision_kind (TREE_TYPE (s)) >= bitint_prec_large)
5392 {
5393 if (SSA_NAME_IS_DEFAULT_DEF (s)
5394 && (!SSA_NAME_VAR (s) || VAR_P (SSA_NAME_VAR (s))))
5395 {
5396 TREE_VALUE (t) = create_tmp_var (TREE_TYPE (s), "bitint");
5397 mark_addressable (TREE_VALUE (t));
5398 }
5399 else
5400 {
5401 int part = var_to_partition (m_map, s);
5402 gcc_assert (m_vars[part] != NULL_TREE);
5403 TREE_VALUE (t) = m_vars[part];
5404 }
5405 }
5406 }
5407 update_stmt (stmt);
5408 }
5409
5410 /* Lower statement STMT which involves large/huge _BitInt values
5411 into code accessing individual limbs. */
5412
5413 void
5414 bitint_large_huge::lower_stmt (gimple *stmt)
5415 {
5416 m_first = true;
5417 m_lhs = NULL_TREE;
5418 m_data.truncate (0);
5419 m_data_cnt = 0;
5420 m_gsi = gsi_for_stmt (stmt);
5421 m_after_stmt = NULL;
5422 m_bb = NULL;
5423 m_init_gsi = m_gsi;
5424 gsi_prev (&m_init_gsi);
5425 m_preheader_bb = NULL;
5426 m_upwards_2limb = 0;
5427 m_upwards = false;
5428 m_var_msb = false;
5429 m_cast_conditional = false;
5430 m_bitfld_load = 0;
5431 m_loc = gimple_location (stmt);
5432 if (is_gimple_call (stmt))
5433 {
5434 lower_call (NULL_TREE, stmt);
5435 return;
5436 }
5437 if (gimple_code (stmt) == GIMPLE_ASM)
5438 {
5439 lower_asm (stmt);
5440 return;
5441 }
5442 tree lhs = NULL_TREE, cmp_op1 = NULL_TREE, cmp_op2 = NULL_TREE;
5443 tree_code cmp_code = comparison_op (stmt, &cmp_op1, &cmp_op2);
5444 bool eq_p = (cmp_code == EQ_EXPR || cmp_code == NE_EXPR);
5445 bool mergeable_cast_p = false;
5446 bool final_cast_p = false;
5447 if (gimple_assign_cast_p (stmt))
5448 {
5449 lhs = gimple_assign_lhs (stmt);
5450 tree rhs1 = gimple_assign_rhs1 (stmt);
5451 if (TREE_CODE (rhs1) == VIEW_CONVERT_EXPR)
5452 rhs1 = TREE_OPERAND (rhs1, 0);
5453 if (TREE_CODE (TREE_TYPE (lhs)) == BITINT_TYPE
5454 && bitint_precision_kind (TREE_TYPE (lhs)) >= bitint_prec_large
5455 && INTEGRAL_TYPE_P (TREE_TYPE (rhs1)))
5456 mergeable_cast_p = true;
5457 else if (TREE_CODE (TREE_TYPE (rhs1)) == BITINT_TYPE
5458 && bitint_precision_kind (TREE_TYPE (rhs1)) >= bitint_prec_large
5459 && (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
5460 || POINTER_TYPE_P (TREE_TYPE (lhs))
5461 || gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR))
5462 {
5463 final_cast_p = true;
5464 if (((TREE_CODE (TREE_TYPE (lhs)) == INTEGER_TYPE
5465 && TYPE_PRECISION (TREE_TYPE (lhs)) > MAX_FIXED_MODE_SIZE)
5466 || (!INTEGRAL_TYPE_P (TREE_TYPE (lhs))
5467 && !POINTER_TYPE_P (TREE_TYPE (lhs))))
5468 && gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR)
5469 {
5470 /* Handle VIEW_CONVERT_EXPRs to not generally supported
5471 huge INTEGER_TYPEs like uint256_t or uint512_t. These
5472 are usually emitted from memcpy folding and backends
5473 support moves with them but that is usually it.
5474 Similarly handle VCEs to vector/complex types etc. */
5475 gcc_assert (TREE_CODE (rhs1) == SSA_NAME);
5476 if (SSA_NAME_IS_DEFAULT_DEF (rhs1)
5477 && (!SSA_NAME_VAR (rhs1) || VAR_P (SSA_NAME_VAR (rhs1))))
5478 {
5479 tree var = create_tmp_reg (TREE_TYPE (lhs));
5480 rhs1 = get_or_create_ssa_default_def (cfun, var);
5481 gimple_assign_set_rhs1 (stmt, rhs1);
5482 gimple_assign_set_rhs_code (stmt, SSA_NAME);
5483 }
5484 else if (m_names == NULL
5485 || !bitmap_bit_p (m_names, SSA_NAME_VERSION (rhs1)))
5486 {
5487 gimple *g = SSA_NAME_DEF_STMT (rhs1);
5488 gcc_assert (gimple_assign_load_p (g));
5489 tree mem = gimple_assign_rhs1 (g);
5490 tree ltype = TREE_TYPE (lhs);
5491 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (mem));
5492 if (as != TYPE_ADDR_SPACE (ltype))
5493 ltype
5494 = build_qualified_type (ltype,
5495 TYPE_QUALS (ltype)
5496 | ENCODE_QUAL_ADDR_SPACE (as));
5497 rhs1 = build1 (VIEW_CONVERT_EXPR, ltype, unshare_expr (mem));
5498 gimple_assign_set_rhs1 (stmt, rhs1);
5499 }
5500 else
5501 {
5502 int part = var_to_partition (m_map, rhs1);
5503 gcc_assert (m_vars[part] != NULL_TREE);
5504 rhs1 = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (lhs),
5505 m_vars[part]);
5506 gimple_assign_set_rhs1 (stmt, rhs1);
5507 }
5508 update_stmt (stmt);
5509 return;
5510 }
5511 if (TREE_CODE (rhs1) == SSA_NAME
5512 && (m_names == NULL
5513 || !bitmap_bit_p (m_names, SSA_NAME_VERSION (rhs1))))
5514 {
5515 gimple *g = SSA_NAME_DEF_STMT (rhs1);
5516 if (is_gimple_assign (g)
5517 && gimple_assign_rhs_code (g) == IMAGPART_EXPR)
5518 {
5519 tree rhs2 = TREE_OPERAND (gimple_assign_rhs1 (g), 0);
5520 if (TREE_CODE (rhs2) == SSA_NAME
5521 && (m_names == NULL
5522 || !bitmap_bit_p (m_names, SSA_NAME_VERSION (rhs2))))
5523 {
5524 g = SSA_NAME_DEF_STMT (rhs2);
5525 int ovf = optimizable_arith_overflow (g);
5526 if (ovf == 2)
5527 /* If .{ADD,SUB,MUL}_OVERFLOW has both REALPART_EXPR
5528 and IMAGPART_EXPR uses, where the latter is cast to
5529 non-_BitInt, it will be optimized when handling
5530 the REALPART_EXPR. */
5531 return;
5532 if (ovf == 1)
5533 {
5534 lower_call (NULL_TREE, g);
5535 return;
5536 }
5537 }
5538 }
5539 }
5540 }
5541 else if (TREE_CODE (TREE_TYPE (lhs)) == BITINT_TYPE
5542 && bitint_precision_kind (TREE_TYPE (lhs)) >= bitint_prec_large
5543 && !INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
5544 && !POINTER_TYPE_P (TREE_TYPE (rhs1))
5545 && gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR)
5546 {
5547 int part = var_to_partition (m_map, lhs);
5548 gcc_assert (m_vars[part] != NULL_TREE);
5549 lhs = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (rhs1), m_vars[part]);
5550 insert_before (gimple_build_assign (lhs, rhs1));
5551 return;
5552 }
5553 }
5554 if (gimple_store_p (stmt))
5555 {
5556 tree rhs1 = gimple_assign_rhs1 (stmt);
5557 if (TREE_CODE (rhs1) == SSA_NAME
5558 && (m_names == NULL
5559 || !bitmap_bit_p (m_names, SSA_NAME_VERSION (rhs1))))
5560 {
5561 gimple *g = SSA_NAME_DEF_STMT (rhs1);
5562 m_loc = gimple_location (g);
5563 lhs = gimple_assign_lhs (stmt);
5564 if (is_gimple_assign (g) && !mergeable_op (g))
5565 switch (gimple_assign_rhs_code (g))
5566 {
5567 case LSHIFT_EXPR:
5568 case RSHIFT_EXPR:
5569 lower_shift_stmt (lhs, g);
5570 handled:
5571 m_gsi = gsi_for_stmt (stmt);
5572 unlink_stmt_vdef (stmt);
5573 release_ssa_name (gimple_vdef (stmt));
5574 gsi_remove (&m_gsi, true);
5575 return;
5576 case MULT_EXPR:
5577 case TRUNC_DIV_EXPR:
5578 case EXACT_DIV_EXPR:
5579 case TRUNC_MOD_EXPR:
5580 lower_muldiv_stmt (lhs, g);
5581 goto handled;
5582 case FIX_TRUNC_EXPR:
5583 lower_float_conv_stmt (lhs, g);
5584 goto handled;
5585 case REALPART_EXPR:
5586 case IMAGPART_EXPR:
5587 lower_cplxpart_stmt (lhs, g);
5588 goto handled;
5589 case VIEW_CONVERT_EXPR:
5590 {
5591 tree rhs1 = gimple_assign_rhs1 (g);
5592 rhs1 = TREE_OPERAND (rhs1, 0);
5593 if (!INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
5594 && !POINTER_TYPE_P (TREE_TYPE (rhs1)))
5595 {
5596 tree ltype = TREE_TYPE (rhs1);
5597 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (lhs));
5598 ltype
5599 = build_qualified_type (ltype,
5600 TYPE_QUALS (TREE_TYPE (lhs))
5601 | ENCODE_QUAL_ADDR_SPACE (as));
5602 lhs = build1 (VIEW_CONVERT_EXPR, ltype, lhs);
5603 gimple_assign_set_lhs (stmt, lhs);
5604 gimple_assign_set_rhs1 (stmt, rhs1);
5605 gimple_assign_set_rhs_code (stmt, TREE_CODE (rhs1));
5606 update_stmt (stmt);
5607 return;
5608 }
5609 }
5610 break;
5611 default:
5612 break;
5613 }
5614 else if (optimizable_arith_overflow (g) == 3)
5615 {
5616 lower_call (lhs, g);
5617 goto handled;
5618 }
5619 m_loc = gimple_location (stmt);
5620 }
5621 }
5622 if (mergeable_op (stmt)
5623 || gimple_store_p (stmt)
5624 || gimple_assign_load_p (stmt)
5625 || eq_p
5626 || mergeable_cast_p
5627 || (is_gimple_assign (stmt)
5628 && gimple_assign_rhs_code (stmt) == PAREN_EXPR))
5629 {
5630 lhs = lower_mergeable_stmt (stmt, cmp_code, cmp_op1, cmp_op2);
5631 if (!eq_p)
5632 return;
5633 }
5634 else if (cmp_code != ERROR_MARK)
5635 lhs = lower_comparison_stmt (stmt, cmp_code, cmp_op1, cmp_op2);
5636 if (cmp_code != ERROR_MARK)
5637 {
5638 if (gimple_code (stmt) == GIMPLE_COND)
5639 {
5640 gcond *cstmt = as_a <gcond *> (stmt);
5641 gimple_cond_set_lhs (cstmt, lhs);
5642 gimple_cond_set_rhs (cstmt, boolean_false_node);
5643 gimple_cond_set_code (cstmt, cmp_code);
5644 update_stmt (stmt);
5645 return;
5646 }
5647 if (gimple_assign_rhs_code (stmt) == COND_EXPR)
5648 {
5649 tree cond = build2 (cmp_code, boolean_type_node, lhs,
5650 boolean_false_node);
5651 gimple_assign_set_rhs1 (stmt, cond);
5652 lhs = gimple_assign_lhs (stmt);
5653 gcc_assert (TREE_CODE (TREE_TYPE (lhs)) != BITINT_TYPE
5654 || (bitint_precision_kind (TREE_TYPE (lhs))
5655 <= bitint_prec_middle));
5656 update_stmt (stmt);
5657 return;
5658 }
5659 gimple_assign_set_rhs1 (stmt, lhs);
5660 gimple_assign_set_rhs2 (stmt, boolean_false_node);
5661 gimple_assign_set_rhs_code (stmt, cmp_code);
5662 update_stmt (stmt);
5663 return;
5664 }
5665 if (final_cast_p)
5666 {
5667 tree lhs_type = TREE_TYPE (lhs);
5668 /* Add support for 3 or more limbs filled in from normal integral
5669 type if this assert fails. If no target chooses limb mode smaller
5670 than half of largest supported normal integral type, this will not
5671 be needed. */
5672 gcc_assert (TYPE_PRECISION (lhs_type) <= 2 * limb_prec);
5673 gimple *g;
5674 if ((TREE_CODE (lhs_type) == BITINT_TYPE
5675 && bitint_precision_kind (lhs_type) == bitint_prec_middle)
5676 || POINTER_TYPE_P (lhs_type))
5677 lhs_type = build_nonstandard_integer_type (TYPE_PRECISION (lhs_type),
5678 TYPE_UNSIGNED (lhs_type));
5679 m_data_cnt = 0;
5680 tree rhs1 = gimple_assign_rhs1 (stmt);
5681 tree r1 = handle_operand (rhs1, size_int (0));
5682 if (!useless_type_conversion_p (lhs_type, TREE_TYPE (r1)))
5683 r1 = add_cast (lhs_type, r1);
5684 if (TYPE_PRECISION (lhs_type) > limb_prec)
5685 {
5686 m_data_cnt = 0;
5687 m_first = false;
5688 tree r2 = handle_operand (rhs1, size_int (1));
5689 r2 = add_cast (lhs_type, r2);
5690 g = gimple_build_assign (make_ssa_name (lhs_type), LSHIFT_EXPR, r2,
5691 build_int_cst (unsigned_type_node,
5692 limb_prec));
5693 insert_before (g);
5694 g = gimple_build_assign (make_ssa_name (lhs_type), BIT_IOR_EXPR, r1,
5695 gimple_assign_lhs (g));
5696 insert_before (g);
5697 r1 = gimple_assign_lhs (g);
5698 }
5699 if (lhs_type != TREE_TYPE (lhs))
5700 g = gimple_build_assign (lhs, NOP_EXPR, r1);
5701 else
5702 g = gimple_build_assign (lhs, r1);
5703 gsi_replace (&m_gsi, g, true);
5704 return;
5705 }
5706 if (is_gimple_assign (stmt))
5707 switch (gimple_assign_rhs_code (stmt))
5708 {
5709 case LSHIFT_EXPR:
5710 case RSHIFT_EXPR:
5711 lower_shift_stmt (NULL_TREE, stmt);
5712 return;
5713 case MULT_EXPR:
5714 case TRUNC_DIV_EXPR:
5715 case EXACT_DIV_EXPR:
5716 case TRUNC_MOD_EXPR:
5717 lower_muldiv_stmt (NULL_TREE, stmt);
5718 return;
5719 case FIX_TRUNC_EXPR:
5720 case FLOAT_EXPR:
5721 lower_float_conv_stmt (NULL_TREE, stmt);
5722 return;
5723 case REALPART_EXPR:
5724 case IMAGPART_EXPR:
5725 lower_cplxpart_stmt (NULL_TREE, stmt);
5726 return;
5727 case COMPLEX_EXPR:
5728 lower_complexexpr_stmt (stmt);
5729 return;
5730 default:
5731 break;
5732 }
5733 gcc_unreachable ();
5734 }
5735
5736 /* Helper for walk_non_aliased_vuses. Determine if we arrived at
5737 the desired memory state. */
5738
5739 void *
5740 vuse_eq (ao_ref *, tree vuse1, void *data)
5741 {
5742 tree vuse2 = (tree) data;
5743 if (vuse1 == vuse2)
5744 return data;
5745
5746 return NULL;
5747 }
5748
5749 /* Return true if STMT uses a library function and needs to take
5750 address of its inputs. We need to avoid bit-fields in those
5751 cases. Similarly, we need to avoid overlap between destination
5752 and source limb arrays. */
5753
5754 bool
5755 stmt_needs_operand_addr (gimple *stmt)
5756 {
5757 if (is_gimple_assign (stmt))
5758 switch (gimple_assign_rhs_code (stmt))
5759 {
5760 case MULT_EXPR:
5761 case TRUNC_DIV_EXPR:
5762 case EXACT_DIV_EXPR:
5763 case TRUNC_MOD_EXPR:
5764 case FLOAT_EXPR:
5765 return true;
5766 default:
5767 break;
5768 }
5769 else if (gimple_call_internal_p (stmt, IFN_MUL_OVERFLOW)
5770 || gimple_call_internal_p (stmt, IFN_UBSAN_CHECK_MUL))
5771 return true;
5772 return false;
5773 }
5774
5775 /* Dominator walker used to discover which large/huge _BitInt
5776 loads could be sunk into all their uses. */
5777
5778 class bitint_dom_walker : public dom_walker
5779 {
5780 public:
5781 bitint_dom_walker (bitmap names, bitmap loads)
5782 : dom_walker (CDI_DOMINATORS), m_names (names), m_loads (loads) {}
5783
5784 edge before_dom_children (basic_block) final override;
5785
5786 private:
5787 bitmap m_names, m_loads;
5788 };
5789
5790 edge
5791 bitint_dom_walker::before_dom_children (basic_block bb)
5792 {
5793 gphi *phi = get_virtual_phi (bb);
5794 tree vop;
5795 if (phi)
5796 vop = gimple_phi_result (phi);
5797 else if (bb == ENTRY_BLOCK_PTR_FOR_FN (cfun))
5798 vop = NULL_TREE;
5799 else
5800 vop = (tree) get_immediate_dominator (CDI_DOMINATORS, bb)->aux;
5801
5802 auto_vec<tree, 16> worklist;
5803 for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
5804 !gsi_end_p (gsi); gsi_next (&gsi))
5805 {
5806 gimple *stmt = gsi_stmt (gsi);
5807 if (is_gimple_debug (stmt))
5808 continue;
5809
5810 if (!vop && gimple_vuse (stmt))
5811 vop = gimple_vuse (stmt);
5812
5813 tree cvop = vop;
5814 if (gimple_vdef (stmt))
5815 vop = gimple_vdef (stmt);
5816
5817 tree lhs = gimple_get_lhs (stmt);
5818 if (lhs
5819 && TREE_CODE (lhs) == SSA_NAME
5820 && TREE_CODE (TREE_TYPE (lhs)) == BITINT_TYPE
5821 && bitint_precision_kind (TREE_TYPE (lhs)) >= bitint_prec_large
5822 && !bitmap_bit_p (m_names, SSA_NAME_VERSION (lhs)))
5823 /* If lhs of stmt is large/huge _BitInt SSA_NAME not in m_names,
5824 it means it will be handled in a loop or straight line code
5825 at the location of its (ultimate) immediate use, so for
5826 vop checking purposes check these only at the ultimate
5827 immediate use. */
5828 continue;
5829
5830 ssa_op_iter oi;
5831 use_operand_p use_p;
5832 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, oi, SSA_OP_USE)
5833 {
5834 tree s = USE_FROM_PTR (use_p);
5835 if (TREE_CODE (TREE_TYPE (s)) == BITINT_TYPE
5836 && bitint_precision_kind (TREE_TYPE (s)) >= bitint_prec_large)
5837 worklist.safe_push (s);
5838 }
5839
5840 bool needs_operand_addr = stmt_needs_operand_addr (stmt);
5841 while (worklist.length () > 0)
5842 {
5843 tree s = worklist.pop ();
5844
5845 if (!bitmap_bit_p (m_names, SSA_NAME_VERSION (s)))
5846 {
5847 gimple *g = SSA_NAME_DEF_STMT (s);
5848 needs_operand_addr |= stmt_needs_operand_addr (g);
5849 FOR_EACH_SSA_USE_OPERAND (use_p, g, oi, SSA_OP_USE)
5850 {
5851 tree s2 = USE_FROM_PTR (use_p);
5852 if (TREE_CODE (TREE_TYPE (s2)) == BITINT_TYPE
5853 && (bitint_precision_kind (TREE_TYPE (s2))
5854 >= bitint_prec_large))
5855 worklist.safe_push (s2);
5856 }
5857 continue;
5858 }
5859 if (!SSA_NAME_OCCURS_IN_ABNORMAL_PHI (s)
5860 && gimple_assign_cast_p (SSA_NAME_DEF_STMT (s)))
5861 {
5862 tree rhs = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (s));
5863 if (TREE_CODE (rhs) == SSA_NAME
5864 && bitmap_bit_p (m_loads, SSA_NAME_VERSION (rhs)))
5865 s = rhs;
5866 else
5867 continue;
5868 }
5869 else if (!bitmap_bit_p (m_loads, SSA_NAME_VERSION (s)))
5870 continue;
5871
5872 gimple *g = SSA_NAME_DEF_STMT (s);
5873 tree rhs1 = gimple_assign_rhs1 (g);
5874 if (needs_operand_addr
5875 && TREE_CODE (rhs1) == COMPONENT_REF
5876 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (rhs1, 1)))
5877 {
5878 tree fld = TREE_OPERAND (rhs1, 1);
5879 /* For little-endian, we can allow as inputs bit-fields
5880 which start at a limb boundary. */
5881 if (DECL_OFFSET_ALIGN (fld) >= TYPE_ALIGN (TREE_TYPE (rhs1))
5882 && tree_fits_uhwi_p (DECL_FIELD_BIT_OFFSET (fld))
5883 && (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (fld))
5884 % limb_prec) == 0)
5885 ;
5886 else
5887 {
5888 bitmap_clear_bit (m_loads, SSA_NAME_VERSION (s));
5889 continue;
5890 }
5891 }
5892
5893 ao_ref ref;
5894 ao_ref_init (&ref, rhs1);
5895 tree lvop = gimple_vuse (g);
5896 unsigned limit = 64;
5897 tree vuse = cvop;
5898 if (vop != cvop
5899 && is_gimple_assign (stmt)
5900 && gimple_store_p (stmt)
5901 && (needs_operand_addr
5902 || !operand_equal_p (lhs, gimple_assign_rhs1 (g), 0)))
5903 vuse = vop;
5904 if (vuse != lvop
5905 && walk_non_aliased_vuses (&ref, vuse, false, vuse_eq,
5906 NULL, NULL, NULL, limit, lvop) == NULL)
5907 bitmap_clear_bit (m_loads, SSA_NAME_VERSION (s));
5908 }
5909 }
5910
5911 bb->aux = (void *) vop;
5912 return NULL;
5913 }
5914
5915 }
5916
5917 /* Replacement for normal processing of STMT in tree-ssa-coalesce.cc
5918 build_ssa_conflict_graph.
5919 The differences are:
5920 1) don't process assignments with large/huge _BitInt lhs not in NAMES
5921 2) for large/huge _BitInt multiplication/division/modulo process def
5922 only after processing uses rather than before to make uses conflict
5923 with the definition
5924 3) for large/huge _BitInt uses not in NAMES mark the uses of their
5925 SSA_NAME_DEF_STMT (recursively), because those uses will be sunk into
5926 the final statement. */
5927
5928 void
5929 build_bitint_stmt_ssa_conflicts (gimple *stmt, live_track *live,
5930 ssa_conflicts *graph, bitmap names,
5931 void (*def) (live_track *, tree,
5932 ssa_conflicts *),
5933 void (*use) (live_track *, tree),
5934 void (*clear) (live_track *, tree))
5935 {
5936 bool muldiv_p = false;
5937 tree lhs = NULL_TREE;
5938 if (is_gimple_assign (stmt))
5939 {
5940 lhs = gimple_assign_lhs (stmt);
5941 if (TREE_CODE (lhs) == SSA_NAME)
5942 {
5943 tree type = TREE_TYPE (lhs);
5944 if (TREE_CODE (type) == COMPLEX_TYPE)
5945 type = TREE_TYPE (type);
5946 if (TREE_CODE (type) == BITINT_TYPE
5947 && bitint_precision_kind (type) >= bitint_prec_large)
5948 {
5949 if (!bitmap_bit_p (names, SSA_NAME_VERSION (lhs)))
5950 return;
5951
5952 /* A copy between 2 partitions does not introduce an interference
5953 by itself. If they did, you would never be able to coalesce
5954 two things which are copied. If the two variables really do
5955 conflict, they will conflict elsewhere in the program.
5956
5957 This is handled by simply removing the SRC of the copy from
5958 the live list, and processing the stmt normally.
5959
5960 Don't do this if lhs is not in names though, in such cases
5961 it is actually used at some point later in the basic
5962 block. */
5963 if (gimple_assign_copy_p (stmt))
5964 {
5965 tree rhs1 = gimple_assign_rhs1 (stmt);
5966 if (TREE_CODE (rhs1) == SSA_NAME)
5967 clear (live, rhs1);
5968 }
5969
5970 switch (gimple_assign_rhs_code (stmt))
5971 {
5972 case MULT_EXPR:
5973 case TRUNC_DIV_EXPR:
5974 case EXACT_DIV_EXPR:
5975 case TRUNC_MOD_EXPR:
5976 muldiv_p = true;
5977 default:
5978 break;
5979 }
5980 }
5981 }
5982 }
5983
5984 ssa_op_iter iter;
5985 tree var;
5986 if (!muldiv_p)
5987 {
5988 /* For stmts with more than one SSA_NAME definition pretend all the
5989 SSA_NAME outputs but the first one are live at this point, so
5990 that conflicts are added in between all those even when they are
5991 actually not really live after the asm, because expansion might
5992 copy those into pseudos after the asm and if multiple outputs
5993 share the same partition, it might overwrite those that should
5994 be live. E.g.
5995 asm volatile (".." : "=r" (a) : "=r" (b) : "0" (a), "1" (a));
5996 return a;
5997 See PR70593. */
5998 bool first = true;
5999 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_DEF)
6000 if (first)
6001 first = false;
6002 else
6003 use (live, var);
6004
6005 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_DEF)
6006 def (live, var, graph);
6007 }
6008
6009 auto_vec<tree, 16> worklist;
6010 FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_USE)
6011 {
6012 tree type = TREE_TYPE (var);
6013 if (TREE_CODE (type) == COMPLEX_TYPE)
6014 type = TREE_TYPE (type);
6015 if (TREE_CODE (type) == BITINT_TYPE
6016 && bitint_precision_kind (type) >= bitint_prec_large)
6017 {
6018 if (bitmap_bit_p (names, SSA_NAME_VERSION (var)))
6019 use (live, var);
6020 else
6021 worklist.safe_push (var);
6022 }
6023 }
6024
6025 while (worklist.length () > 0)
6026 {
6027 tree s = worklist.pop ();
6028 FOR_EACH_SSA_TREE_OPERAND (var, SSA_NAME_DEF_STMT (s), iter, SSA_OP_USE)
6029 {
6030 tree type = TREE_TYPE (var);
6031 if (TREE_CODE (type) == COMPLEX_TYPE)
6032 type = TREE_TYPE (type);
6033 if (TREE_CODE (type) == BITINT_TYPE
6034 && bitint_precision_kind (type) >= bitint_prec_large)
6035 {
6036 if (bitmap_bit_p (names, SSA_NAME_VERSION (var)))
6037 use (live, var);
6038 else
6039 worklist.safe_push (var);
6040 }
6041 }
6042 }
6043
6044 if (muldiv_p)
6045 def (live, lhs, graph);
6046 }
6047
6048 /* If STMT is .{ADD,SUB,MUL}_OVERFLOW with INTEGER_CST arguments,
6049 return the largest bitint_prec_kind of them, otherwise return
6050 bitint_prec_small. */
6051
6052 static bitint_prec_kind
6053 arith_overflow_arg_kind (gimple *stmt)
6054 {
6055 bitint_prec_kind ret = bitint_prec_small;
6056 if (is_gimple_call (stmt) && gimple_call_internal_p (stmt))
6057 switch (gimple_call_internal_fn (stmt))
6058 {
6059 case IFN_ADD_OVERFLOW:
6060 case IFN_SUB_OVERFLOW:
6061 case IFN_MUL_OVERFLOW:
6062 for (int i = 0; i < 2; ++i)
6063 {
6064 tree a = gimple_call_arg (stmt, i);
6065 if (TREE_CODE (a) == INTEGER_CST
6066 && TREE_CODE (TREE_TYPE (a)) == BITINT_TYPE)
6067 {
6068 bitint_prec_kind kind = bitint_precision_kind (TREE_TYPE (a));
6069 ret = MAX (ret, kind);
6070 }
6071 }
6072 break;
6073 default:
6074 break;
6075 }
6076 return ret;
6077 }
6078
6079 /* Entry point for _BitInt(N) operation lowering during optimization. */
6080
6081 static unsigned int
6082 gimple_lower_bitint (void)
6083 {
6084 small_max_prec = mid_min_prec = large_min_prec = huge_min_prec = 0;
6085 limb_prec = abi_limb_prec = 0;
6086
6087 unsigned int i;
6088 for (i = 0; i < num_ssa_names; ++i)
6089 {
6090 tree s = ssa_name (i);
6091 if (s == NULL)
6092 continue;
6093 tree type = TREE_TYPE (s);
6094 if (TREE_CODE (type) == COMPLEX_TYPE)
6095 {
6096 if (arith_overflow_arg_kind (SSA_NAME_DEF_STMT (s))
6097 != bitint_prec_small)
6098 break;
6099 type = TREE_TYPE (type);
6100 }
6101 if (TREE_CODE (type) == BITINT_TYPE
6102 && bitint_precision_kind (type) != bitint_prec_small)
6103 break;
6104 /* We need to also rewrite stores of large/huge _BitInt INTEGER_CSTs
6105 into memory. Such functions could have no large/huge SSA_NAMEs. */
6106 if (SSA_NAME_IS_VIRTUAL_OPERAND (s))
6107 {
6108 gimple *g = SSA_NAME_DEF_STMT (s);
6109 if (is_gimple_assign (g) && gimple_store_p (g))
6110 {
6111 tree t = gimple_assign_rhs1 (g);
6112 if (TREE_CODE (TREE_TYPE (t)) == BITINT_TYPE
6113 && (bitint_precision_kind (TREE_TYPE (t))
6114 >= bitint_prec_large))
6115 break;
6116 }
6117 }
6118 /* Similarly, e.g. with -frounding-math casts from _BitInt INTEGER_CSTs
6119 to floating point types need to be rewritten. */
6120 else if (SCALAR_FLOAT_TYPE_P (type))
6121 {
6122 gimple *g = SSA_NAME_DEF_STMT (s);
6123 if (is_gimple_assign (g) && gimple_assign_rhs_code (g) == FLOAT_EXPR)
6124 {
6125 tree t = gimple_assign_rhs1 (g);
6126 if (TREE_CODE (t) == INTEGER_CST
6127 && TREE_CODE (TREE_TYPE (t)) == BITINT_TYPE
6128 && (bitint_precision_kind (TREE_TYPE (t))
6129 != bitint_prec_small))
6130 break;
6131 }
6132 }
6133 }
6134 if (i == num_ssa_names)
6135 return 0;
6136
6137 basic_block bb;
6138 auto_vec<gimple *, 4> switch_statements;
6139 FOR_EACH_BB_FN (bb, cfun)
6140 {
6141 if (gswitch *swtch = safe_dyn_cast <gswitch *> (*gsi_last_bb (bb)))
6142 {
6143 tree idx = gimple_switch_index (swtch);
6144 if (TREE_CODE (TREE_TYPE (idx)) != BITINT_TYPE
6145 || bitint_precision_kind (TREE_TYPE (idx)) < bitint_prec_large)
6146 continue;
6147
6148 if (optimize)
6149 group_case_labels_stmt (swtch);
6150 if (gimple_switch_num_labels (swtch) == 1)
6151 {
6152 single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
6153 gimple_stmt_iterator gsi = gsi_for_stmt (swtch);
6154 gsi_remove (&gsi, true);
6155 }
6156 else
6157 switch_statements.safe_push (swtch);
6158 }
6159 }
6160
6161 if (!switch_statements.is_empty ())
6162 {
6163 bool expanded = false;
6164 gimple *stmt;
6165 unsigned int j;
6166 i = 0;
6167 FOR_EACH_VEC_ELT (switch_statements, j, stmt)
6168 {
6169 gswitch *swtch = as_a<gswitch *> (stmt);
6170 tree_switch_conversion::switch_decision_tree dt (swtch);
6171 expanded |= dt.analyze_switch_statement ();
6172 }
6173
6174 if (expanded)
6175 {
6176 free_dominance_info (CDI_DOMINATORS);
6177 free_dominance_info (CDI_POST_DOMINATORS);
6178 mark_virtual_operands_for_renaming (cfun);
6179 cleanup_tree_cfg (TODO_update_ssa);
6180 }
6181 }
6182
6183 struct bitint_large_huge large_huge;
6184 bool has_large_huge_parm_result = false;
6185 bool has_large_huge = false;
6186 unsigned int ret = 0, first_large_huge = ~0U;
6187 bool edge_insertions = false;
6188 for (; i < num_ssa_names; ++i)
6189 {
6190 tree s = ssa_name (i);
6191 if (s == NULL)
6192 continue;
6193 tree type = TREE_TYPE (s);
6194 if (TREE_CODE (type) == COMPLEX_TYPE)
6195 {
6196 if (arith_overflow_arg_kind (SSA_NAME_DEF_STMT (s))
6197 >= bitint_prec_large)
6198 has_large_huge = true;
6199 type = TREE_TYPE (type);
6200 }
6201 if (TREE_CODE (type) == BITINT_TYPE
6202 && bitint_precision_kind (type) >= bitint_prec_large)
6203 {
6204 if (first_large_huge == ~0U)
6205 first_large_huge = i;
6206 gimple *stmt = SSA_NAME_DEF_STMT (s), *g;
6207 gimple_stmt_iterator gsi;
6208 tree_code rhs_code;
6209 /* Unoptimize certain constructs to simpler alternatives to
6210 avoid having to lower all of them. */
6211 if (is_gimple_assign (stmt) && gimple_bb (stmt))
6212 switch (rhs_code = gimple_assign_rhs_code (stmt))
6213 {
6214 default:
6215 break;
6216 case MULT_EXPR:
6217 case TRUNC_DIV_EXPR:
6218 case EXACT_DIV_EXPR:
6219 case TRUNC_MOD_EXPR:
6220 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (s))
6221 {
6222 location_t loc = gimple_location (stmt);
6223 gsi = gsi_for_stmt (stmt);
6224 tree rhs1 = gimple_assign_rhs1 (stmt);
6225 tree rhs2 = gimple_assign_rhs2 (stmt);
6226 /* For multiplication and division with (ab)
6227 lhs and one or both operands force the operands
6228 into new SSA_NAMEs to avoid coalescing failures. */
6229 if (TREE_CODE (rhs1) == SSA_NAME
6230 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1))
6231 {
6232 first_large_huge = 0;
6233 tree t = make_ssa_name (TREE_TYPE (rhs1));
6234 g = gimple_build_assign (t, SSA_NAME, rhs1);
6235 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
6236 gimple_set_location (g, loc);
6237 gimple_assign_set_rhs1 (stmt, t);
6238 if (rhs1 == rhs2)
6239 {
6240 gimple_assign_set_rhs2 (stmt, t);
6241 rhs2 = t;
6242 }
6243 update_stmt (stmt);
6244 }
6245 if (TREE_CODE (rhs2) == SSA_NAME
6246 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs2))
6247 {
6248 first_large_huge = 0;
6249 tree t = make_ssa_name (TREE_TYPE (rhs2));
6250 g = gimple_build_assign (t, SSA_NAME, rhs2);
6251 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
6252 gimple_set_location (g, loc);
6253 gimple_assign_set_rhs2 (stmt, t);
6254 update_stmt (stmt);
6255 }
6256 }
6257 break;
6258 case LROTATE_EXPR:
6259 case RROTATE_EXPR:
6260 {
6261 first_large_huge = 0;
6262 location_t loc = gimple_location (stmt);
6263 gsi = gsi_for_stmt (stmt);
6264 tree rhs1 = gimple_assign_rhs1 (stmt);
6265 tree type = TREE_TYPE (rhs1);
6266 tree n = gimple_assign_rhs2 (stmt), m;
6267 tree p = build_int_cst (TREE_TYPE (n),
6268 TYPE_PRECISION (type));
6269 if (TREE_CODE (n) == INTEGER_CST)
6270 {
6271 if (integer_zerop (n))
6272 m = n;
6273 else
6274 m = fold_build2 (MINUS_EXPR, TREE_TYPE (n), p, n);
6275 }
6276 else
6277 {
6278 tree tem = make_ssa_name (TREE_TYPE (n));
6279 g = gimple_build_assign (tem, MINUS_EXPR, p, n);
6280 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
6281 gimple_set_location (g, loc);
6282 m = make_ssa_name (TREE_TYPE (n));
6283 g = gimple_build_assign (m, TRUNC_MOD_EXPR, tem, p);
6284 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
6285 gimple_set_location (g, loc);
6286 }
6287 if (!TYPE_UNSIGNED (type))
6288 {
6289 tree utype = build_bitint_type (TYPE_PRECISION (type),
6290 1);
6291 if (TREE_CODE (rhs1) == INTEGER_CST)
6292 rhs1 = fold_convert (utype, rhs1);
6293 else
6294 {
6295 tree t = make_ssa_name (type);
6296 g = gimple_build_assign (t, NOP_EXPR, rhs1);
6297 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
6298 gimple_set_location (g, loc);
6299 }
6300 }
6301 g = gimple_build_assign (make_ssa_name (TREE_TYPE (rhs1)),
6302 rhs_code == LROTATE_EXPR
6303 ? LSHIFT_EXPR : RSHIFT_EXPR,
6304 rhs1, n);
6305 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
6306 gimple_set_location (g, loc);
6307 tree op1 = gimple_assign_lhs (g);
6308 g = gimple_build_assign (make_ssa_name (TREE_TYPE (rhs1)),
6309 rhs_code == LROTATE_EXPR
6310 ? RSHIFT_EXPR : LSHIFT_EXPR,
6311 rhs1, m);
6312 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
6313 gimple_set_location (g, loc);
6314 tree op2 = gimple_assign_lhs (g);
6315 tree lhs = gimple_assign_lhs (stmt);
6316 if (!TYPE_UNSIGNED (type))
6317 {
6318 g = gimple_build_assign (make_ssa_name (TREE_TYPE (op1)),
6319 BIT_IOR_EXPR, op1, op2);
6320 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
6321 gimple_set_location (g, loc);
6322 g = gimple_build_assign (lhs, NOP_EXPR,
6323 gimple_assign_lhs (g));
6324 }
6325 else
6326 g = gimple_build_assign (lhs, BIT_IOR_EXPR, op1, op2);
6327 gsi_replace (&gsi, g, true);
6328 gimple_set_location (g, loc);
6329 }
6330 break;
6331 case ABS_EXPR:
6332 case ABSU_EXPR:
6333 case MIN_EXPR:
6334 case MAX_EXPR:
6335 case COND_EXPR:
6336 first_large_huge = 0;
6337 gsi = gsi_for_stmt (stmt);
6338 tree lhs = gimple_assign_lhs (stmt);
6339 tree rhs1 = gimple_assign_rhs1 (stmt), rhs2 = NULL_TREE;
6340 location_t loc = gimple_location (stmt);
6341 if (rhs_code == ABS_EXPR)
6342 g = gimple_build_cond (LT_EXPR, rhs1,
6343 build_zero_cst (TREE_TYPE (rhs1)),
6344 NULL_TREE, NULL_TREE);
6345 else if (rhs_code == ABSU_EXPR)
6346 {
6347 rhs2 = make_ssa_name (TREE_TYPE (lhs));
6348 g = gimple_build_assign (rhs2, NOP_EXPR, rhs1);
6349 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
6350 gimple_set_location (g, loc);
6351 g = gimple_build_cond (LT_EXPR, rhs1,
6352 build_zero_cst (TREE_TYPE (rhs1)),
6353 NULL_TREE, NULL_TREE);
6354 rhs1 = rhs2;
6355 }
6356 else if (rhs_code == MIN_EXPR || rhs_code == MAX_EXPR)
6357 {
6358 rhs2 = gimple_assign_rhs2 (stmt);
6359 if (TREE_CODE (rhs1) == INTEGER_CST)
6360 std::swap (rhs1, rhs2);
6361 g = gimple_build_cond (LT_EXPR, rhs1, rhs2,
6362 NULL_TREE, NULL_TREE);
6363 if (rhs_code == MAX_EXPR)
6364 std::swap (rhs1, rhs2);
6365 }
6366 else
6367 {
6368 g = gimple_build_cond (NE_EXPR, rhs1,
6369 build_zero_cst (TREE_TYPE (rhs1)),
6370 NULL_TREE, NULL_TREE);
6371 rhs1 = gimple_assign_rhs2 (stmt);
6372 rhs2 = gimple_assign_rhs3 (stmt);
6373 }
6374 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
6375 gimple_set_location (g, loc);
6376 edge e1 = split_block (gsi_bb (gsi), g);
6377 edge e2 = split_block (e1->dest, (gimple *) NULL);
6378 edge e3 = make_edge (e1->src, e2->dest, EDGE_FALSE_VALUE);
6379 e3->probability = profile_probability::even ();
6380 e1->flags = EDGE_TRUE_VALUE;
6381 e1->probability = e3->probability.invert ();
6382 if (dom_info_available_p (CDI_DOMINATORS))
6383 set_immediate_dominator (CDI_DOMINATORS, e2->dest, e1->src);
6384 if (rhs_code == ABS_EXPR || rhs_code == ABSU_EXPR)
6385 {
6386 gsi = gsi_after_labels (e1->dest);
6387 g = gimple_build_assign (make_ssa_name (TREE_TYPE (rhs1)),
6388 NEGATE_EXPR, rhs1);
6389 gsi_insert_before (&gsi, g, GSI_SAME_STMT);
6390 gimple_set_location (g, loc);
6391 rhs2 = gimple_assign_lhs (g);
6392 std::swap (rhs1, rhs2);
6393 }
6394 gsi = gsi_for_stmt (stmt);
6395 gsi_remove (&gsi, true);
6396 gphi *phi = create_phi_node (lhs, e2->dest);
6397 add_phi_arg (phi, rhs1, e2, UNKNOWN_LOCATION);
6398 add_phi_arg (phi, rhs2, e3, UNKNOWN_LOCATION);
6399 break;
6400 }
6401 }
6402 /* We need to also rewrite stores of large/huge _BitInt INTEGER_CSTs
6403 into memory. Such functions could have no large/huge SSA_NAMEs. */
6404 else if (SSA_NAME_IS_VIRTUAL_OPERAND (s))
6405 {
6406 gimple *g = SSA_NAME_DEF_STMT (s);
6407 if (is_gimple_assign (g) && gimple_store_p (g))
6408 {
6409 tree t = gimple_assign_rhs1 (g);
6410 if (TREE_CODE (TREE_TYPE (t)) == BITINT_TYPE
6411 && (bitint_precision_kind (TREE_TYPE (t))
6412 >= bitint_prec_large))
6413 has_large_huge = true;
6414 }
6415 }
6416 /* Similarly, e.g. with -frounding-math casts from _BitInt INTEGER_CSTs
6417 to floating point types need to be rewritten. */
6418 else if (SCALAR_FLOAT_TYPE_P (type))
6419 {
6420 gimple *g = SSA_NAME_DEF_STMT (s);
6421 if (is_gimple_assign (g) && gimple_assign_rhs_code (g) == FLOAT_EXPR)
6422 {
6423 tree t = gimple_assign_rhs1 (g);
6424 if (TREE_CODE (t) == INTEGER_CST
6425 && TREE_CODE (TREE_TYPE (t)) == BITINT_TYPE
6426 && (bitint_precision_kind (TREE_TYPE (t))
6427 >= bitint_prec_large))
6428 has_large_huge = true;
6429 }
6430 }
6431 }
6432 for (i = first_large_huge; i < num_ssa_names; ++i)
6433 {
6434 tree s = ssa_name (i);
6435 if (s == NULL)
6436 continue;
6437 tree type = TREE_TYPE (s);
6438 if (TREE_CODE (type) == COMPLEX_TYPE)
6439 type = TREE_TYPE (type);
6440 if (TREE_CODE (type) == BITINT_TYPE
6441 && bitint_precision_kind (type) >= bitint_prec_large)
6442 {
6443 use_operand_p use_p;
6444 gimple *use_stmt;
6445 has_large_huge = true;
6446 if (optimize
6447 && optimizable_arith_overflow (SSA_NAME_DEF_STMT (s)))
6448 continue;
6449 /* Ignore large/huge _BitInt SSA_NAMEs which have single use in
6450 the same bb and could be handled in the same loop with the
6451 immediate use. */
6452 if (optimize
6453 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (s)
6454 && single_imm_use (s, &use_p, &use_stmt)
6455 && gimple_bb (SSA_NAME_DEF_STMT (s)) == gimple_bb (use_stmt))
6456 {
6457 if (mergeable_op (SSA_NAME_DEF_STMT (s)))
6458 {
6459 if (mergeable_op (use_stmt))
6460 continue;
6461 tree_code cmp_code = comparison_op (use_stmt, NULL, NULL);
6462 if (cmp_code == EQ_EXPR || cmp_code == NE_EXPR)
6463 continue;
6464 if (gimple_assign_cast_p (use_stmt))
6465 {
6466 tree lhs = gimple_assign_lhs (use_stmt);
6467 if (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6468 /* Don't merge with VIEW_CONVERT_EXPRs to
6469 huge INTEGER_TYPEs used sometimes in memcpy
6470 expansion. */
6471 && (TREE_CODE (TREE_TYPE (lhs)) != INTEGER_TYPE
6472 || (TYPE_PRECISION (TREE_TYPE (lhs))
6473 <= MAX_FIXED_MODE_SIZE)))
6474 continue;
6475 }
6476 else if (gimple_store_p (use_stmt)
6477 && is_gimple_assign (use_stmt)
6478 && !gimple_has_volatile_ops (use_stmt)
6479 && !stmt_ends_bb_p (use_stmt))
6480 continue;
6481 }
6482 if (gimple_assign_cast_p (SSA_NAME_DEF_STMT (s)))
6483 {
6484 tree rhs1 = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (s));
6485 if (TREE_CODE (rhs1) == VIEW_CONVERT_EXPR)
6486 {
6487 rhs1 = TREE_OPERAND (rhs1, 0);
6488 if (!INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
6489 && !POINTER_TYPE_P (TREE_TYPE (rhs1))
6490 && gimple_store_p (use_stmt))
6491 continue;
6492 }
6493 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
6494 && ((is_gimple_assign (use_stmt)
6495 && (gimple_assign_rhs_code (use_stmt)
6496 != COMPLEX_EXPR))
6497 || gimple_code (use_stmt) == GIMPLE_COND)
6498 && (!gimple_store_p (use_stmt)
6499 || (is_gimple_assign (use_stmt)
6500 && !gimple_has_volatile_ops (use_stmt)
6501 && !stmt_ends_bb_p (use_stmt)))
6502 && (TREE_CODE (rhs1) != SSA_NAME
6503 || !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs1)))
6504 {
6505 if (is_gimple_assign (use_stmt))
6506 switch (gimple_assign_rhs_code (use_stmt))
6507 {
6508 case TRUNC_DIV_EXPR:
6509 case EXACT_DIV_EXPR:
6510 case TRUNC_MOD_EXPR:
6511 case FLOAT_EXPR:
6512 /* For division, modulo and casts to floating
6513 point, avoid representing unsigned operands
6514 using negative prec if they were sign-extended
6515 from narrower precision. */
6516 if (TYPE_UNSIGNED (TREE_TYPE (s))
6517 && !TYPE_UNSIGNED (TREE_TYPE (rhs1))
6518 && (TYPE_PRECISION (TREE_TYPE (s))
6519 > TYPE_PRECISION (TREE_TYPE (rhs1))))
6520 goto force_name;
6521 /* FALLTHRU */
6522 case MULT_EXPR:
6523 if (TREE_CODE (TREE_TYPE (rhs1)) != BITINT_TYPE
6524 || (bitint_precision_kind (TREE_TYPE (rhs1))
6525 < bitint_prec_large))
6526 continue;
6527 /* Uses which use handle_operand_addr can't
6528 deal with nested casts. */
6529 if (TREE_CODE (rhs1) == SSA_NAME
6530 && gimple_assign_cast_p
6531 (SSA_NAME_DEF_STMT (rhs1))
6532 && has_single_use (rhs1)
6533 && (gimple_bb (SSA_NAME_DEF_STMT (rhs1))
6534 == gimple_bb (SSA_NAME_DEF_STMT (s))))
6535 goto force_name;
6536 break;
6537 case VIEW_CONVERT_EXPR:
6538 {
6539 tree lhs = gimple_assign_lhs (use_stmt);
6540 /* Don't merge with VIEW_CONVERT_EXPRs to
6541 non-integral types. */
6542 if (!INTEGRAL_TYPE_P (TREE_TYPE (lhs)))
6543 goto force_name;
6544 /* Don't merge with VIEW_CONVERT_EXPRs to
6545 huge INTEGER_TYPEs used sometimes in memcpy
6546 expansion. */
6547 if (TREE_CODE (TREE_TYPE (lhs)) == INTEGER_TYPE
6548 && (TYPE_PRECISION (TREE_TYPE (lhs))
6549 > MAX_FIXED_MODE_SIZE))
6550 goto force_name;
6551 }
6552 break;
6553 default:
6554 break;
6555 }
6556 if (TREE_CODE (TREE_TYPE (rhs1)) != BITINT_TYPE
6557 || (bitint_precision_kind (TREE_TYPE (rhs1))
6558 < bitint_prec_large))
6559 continue;
6560 if ((TYPE_PRECISION (TREE_TYPE (rhs1))
6561 >= TYPE_PRECISION (TREE_TYPE (s)))
6562 && mergeable_op (use_stmt))
6563 continue;
6564 /* Prevent merging a widening non-mergeable cast
6565 on result of some narrower mergeable op
6566 together with later mergeable operations. E.g.
6567 result of _BitInt(223) addition shouldn't be
6568 sign-extended to _BitInt(513) and have another
6569 _BitInt(513) added to it, as handle_plus_minus
6570 with its PHI node handling inside of handle_cast
6571 will not work correctly. An exception is if
6572 use_stmt is a store, this is handled directly
6573 in lower_mergeable_stmt. */
6574 if (TREE_CODE (rhs1) != SSA_NAME
6575 || !has_single_use (rhs1)
6576 || (gimple_bb (SSA_NAME_DEF_STMT (rhs1))
6577 != gimple_bb (SSA_NAME_DEF_STMT (s)))
6578 || !mergeable_op (SSA_NAME_DEF_STMT (rhs1))
6579 || gimple_store_p (use_stmt))
6580 continue;
6581 if ((TYPE_PRECISION (TREE_TYPE (rhs1))
6582 < TYPE_PRECISION (TREE_TYPE (s)))
6583 && gimple_assign_cast_p (SSA_NAME_DEF_STMT (rhs1)))
6584 {
6585 /* Another exception is if the widening cast is
6586 from mergeable same precision cast from something
6587 not mergeable. */
6588 tree rhs2
6589 = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (rhs1));
6590 if (TREE_CODE (TREE_TYPE (rhs2)) == BITINT_TYPE
6591 && (TYPE_PRECISION (TREE_TYPE (rhs1))
6592 == TYPE_PRECISION (TREE_TYPE (rhs2))))
6593 {
6594 if (TREE_CODE (rhs2) != SSA_NAME
6595 || !has_single_use (rhs2)
6596 || (gimple_bb (SSA_NAME_DEF_STMT (rhs2))
6597 != gimple_bb (SSA_NAME_DEF_STMT (s)))
6598 || !mergeable_op (SSA_NAME_DEF_STMT (rhs2)))
6599 continue;
6600 }
6601 }
6602 }
6603 }
6604 if (is_gimple_assign (SSA_NAME_DEF_STMT (s)))
6605 switch (gimple_assign_rhs_code (SSA_NAME_DEF_STMT (s)))
6606 {
6607 case REALPART_EXPR:
6608 case IMAGPART_EXPR:
6609 {
6610 gimple *ds = SSA_NAME_DEF_STMT (s);
6611 tree rhs1 = gimple_assign_rhs1 (ds);
6612 rhs1 = TREE_OPERAND (rhs1, 0);
6613 if (TREE_CODE (rhs1) == SSA_NAME)
6614 {
6615 gimple *g = SSA_NAME_DEF_STMT (rhs1);
6616 if (optimizable_arith_overflow (g))
6617 {
6618 if (gimple_assign_rhs_code (ds) == IMAGPART_EXPR)
6619 continue;
6620 if (gimple_store_p (use_stmt))
6621 {
6622 /* Punt if the cast use of IMAGPART_EXPR stmt
6623 appears before the store use_stmt, because
6624 optimizable arith overflow can't be
6625 lowered at the store location in that case.
6626 See PR121828. */
6627 gimple_stmt_iterator gsi
6628 = gsi_for_stmt (use_stmt);
6629 unsigned int cnt = 0;
6630 do
6631 {
6632 gsi_prev_nondebug (&gsi);
6633 if (gsi_end_p (gsi))
6634 break;
6635 gimple *g2 = gsi_stmt (gsi);
6636 if (g2 == ds)
6637 break;
6638 if (++cnt == 64)
6639 break;
6640 if (!gimple_assign_cast_p (g2))
6641 continue;
6642 tree rhs2 = gimple_assign_rhs1 (g2);
6643 if (TREE_CODE (rhs2) != SSA_NAME)
6644 continue;
6645 gimple *g3 = SSA_NAME_DEF_STMT (rhs2);
6646 if (!is_gimple_assign (g3))
6647 continue;
6648 if (gimple_assign_rhs_code (g3)
6649 != IMAGPART_EXPR)
6650 continue;
6651 rhs2 = gimple_assign_rhs1 (g3);
6652 rhs2 = TREE_OPERAND (rhs2, 0);
6653 if (rhs2 != rhs1)
6654 continue;
6655 cnt = 64;
6656 break;
6657 }
6658 while (1);
6659 if (cnt == 64)
6660 break;
6661 }
6662 }
6663 }
6664 }
6665 /* FALLTHRU */
6666 case LSHIFT_EXPR:
6667 case RSHIFT_EXPR:
6668 case MULT_EXPR:
6669 case TRUNC_DIV_EXPR:
6670 case EXACT_DIV_EXPR:
6671 case TRUNC_MOD_EXPR:
6672 case FIX_TRUNC_EXPR:
6673 if (gimple_store_p (use_stmt)
6674 && is_gimple_assign (use_stmt)
6675 && !gimple_has_volatile_ops (use_stmt)
6676 && !stmt_ends_bb_p (use_stmt))
6677 {
6678 tree lhs = gimple_assign_lhs (use_stmt);
6679 /* As multiply/division passes address of the lhs
6680 to library function and that assumes it can extend
6681 it to whole number of limbs, avoid merging those
6682 with bit-field stores. Don't allow it for
6683 shifts etc. either, so that the bit-field store
6684 handling doesn't have to be done everywhere. */
6685 if (TREE_CODE (lhs) == COMPONENT_REF
6686 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (lhs, 1)))
6687 break;
6688 continue;
6689 }
6690 break;
6691 default:
6692 break;
6693 }
6694 }
6695
6696 /* Also ignore uninitialized uses. */
6697 if (SSA_NAME_IS_DEFAULT_DEF (s)
6698 && (!SSA_NAME_VAR (s) || VAR_P (SSA_NAME_VAR (s))))
6699 continue;
6700
6701 force_name:
6702 if (!large_huge.m_names)
6703 large_huge.m_names = BITMAP_ALLOC (NULL);
6704 bitmap_set_bit (large_huge.m_names, SSA_NAME_VERSION (s));
6705 if (has_single_use (s))
6706 {
6707 tree s2 = s;
6708 /* The coalescing hook special cases SSA_NAME copies.
6709 Make sure not to mark in m_single_use_names single
6710 use SSA_NAMEs copied from non-single use SSA_NAMEs. */
6711 while (gimple_assign_copy_p (SSA_NAME_DEF_STMT (s2)))
6712 {
6713 s2 = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (s2));
6714 if (TREE_CODE (s2) != SSA_NAME)
6715 break;
6716 if (!has_single_use (s2))
6717 {
6718 s2 = NULL_TREE;
6719 break;
6720 }
6721 }
6722 if (s2)
6723 {
6724 if (!large_huge.m_single_use_names)
6725 large_huge.m_single_use_names = BITMAP_ALLOC (NULL);
6726 bitmap_set_bit (large_huge.m_single_use_names,
6727 SSA_NAME_VERSION (s));
6728 }
6729 }
6730 if (SSA_NAME_VAR (s)
6731 && ((TREE_CODE (SSA_NAME_VAR (s)) == PARM_DECL
6732 && SSA_NAME_IS_DEFAULT_DEF (s))
6733 || TREE_CODE (SSA_NAME_VAR (s)) == RESULT_DECL))
6734 has_large_huge_parm_result = true;
6735 if (optimize
6736 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (s)
6737 && gimple_assign_load_p (SSA_NAME_DEF_STMT (s))
6738 && !gimple_has_volatile_ops (SSA_NAME_DEF_STMT (s))
6739 && !stmt_ends_bb_p (SSA_NAME_DEF_STMT (s)))
6740 {
6741 use_operand_p use_p;
6742 imm_use_iterator iter;
6743 bool optimizable_load = true;
6744 FOR_EACH_IMM_USE_FAST (use_p, iter, s)
6745 {
6746 gimple *use_stmt = USE_STMT (use_p);
6747 if (is_gimple_debug (use_stmt))
6748 continue;
6749 if (gimple_code (use_stmt) == GIMPLE_PHI
6750 || is_gimple_call (use_stmt)
6751 || gimple_code (use_stmt) == GIMPLE_ASM
6752 || (is_gimple_assign (use_stmt)
6753 && (gimple_assign_rhs_code (use_stmt)
6754 == COMPLEX_EXPR)))
6755 {
6756 optimizable_load = false;
6757 break;
6758 }
6759 }
6760
6761 ssa_op_iter oi;
6762 FOR_EACH_SSA_USE_OPERAND (use_p, SSA_NAME_DEF_STMT (s),
6763 oi, SSA_OP_USE)
6764 {
6765 tree s2 = USE_FROM_PTR (use_p);
6766 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (s2))
6767 {
6768 optimizable_load = false;
6769 break;
6770 }
6771 }
6772
6773 if (optimizable_load && !stmt_ends_bb_p (SSA_NAME_DEF_STMT (s)))
6774 {
6775 if (!large_huge.m_loads)
6776 large_huge.m_loads = BITMAP_ALLOC (NULL);
6777 bitmap_set_bit (large_huge.m_loads, SSA_NAME_VERSION (s));
6778 }
6779 }
6780 }
6781 /* We need to also rewrite stores of large/huge _BitInt INTEGER_CSTs
6782 into memory. Such functions could have no large/huge SSA_NAMEs. */
6783 else if (SSA_NAME_IS_VIRTUAL_OPERAND (s))
6784 {
6785 gimple *g = SSA_NAME_DEF_STMT (s);
6786 if (is_gimple_assign (g) && gimple_store_p (g))
6787 {
6788 tree t = gimple_assign_rhs1 (g);
6789 if (TREE_CODE (TREE_TYPE (t)) == BITINT_TYPE
6790 && bitint_precision_kind (TREE_TYPE (t)) >= bitint_prec_large)
6791 has_large_huge = true;
6792 }
6793 }
6794 }
6795
6796 if (large_huge.m_names || has_large_huge)
6797 {
6798 ret = TODO_update_ssa_only_virtuals | TODO_cleanup_cfg;
6799 calculate_dominance_info (CDI_DOMINATORS);
6800 if (optimize)
6801 enable_ranger (cfun);
6802 if (large_huge.m_loads)
6803 {
6804 basic_block entry = ENTRY_BLOCK_PTR_FOR_FN (cfun);
6805 entry->aux = NULL;
6806 bitint_dom_walker (large_huge.m_names,
6807 large_huge.m_loads).walk (entry);
6808 bitmap_and_compl_into (large_huge.m_names, large_huge.m_loads);
6809 clear_aux_for_blocks ();
6810 BITMAP_FREE (large_huge.m_loads);
6811 }
6812 large_huge.m_limb_type = build_nonstandard_integer_type (limb_prec, 1);
6813 large_huge.m_limb_size
6814 = tree_to_uhwi (TYPE_SIZE_UNIT (large_huge.m_limb_type));
6815 }
6816 if (large_huge.m_names)
6817 {
6818 large_huge.m_map
6819 = init_var_map (num_ssa_names, NULL, large_huge.m_names);
6820 coalesce_ssa_name (large_huge.m_map);
6821 partition_view_normal (large_huge.m_map);
6822 if (dump_file && (dump_flags & TDF_DETAILS))
6823 {
6824 fprintf (dump_file, "After Coalescing:\n");
6825 dump_var_map (dump_file, large_huge.m_map);
6826 }
6827 large_huge.m_vars
6828 = XCNEWVEC (tree, num_var_partitions (large_huge.m_map));
6829 bitmap_iterator bi;
6830 if (has_large_huge_parm_result)
6831 EXECUTE_IF_SET_IN_BITMAP (large_huge.m_names, 0, i, bi)
6832 {
6833 tree s = ssa_name (i);
6834 if (SSA_NAME_VAR (s)
6835 && ((TREE_CODE (SSA_NAME_VAR (s)) == PARM_DECL
6836 && SSA_NAME_IS_DEFAULT_DEF (s))
6837 || TREE_CODE (SSA_NAME_VAR (s)) == RESULT_DECL))
6838 {
6839 int p = var_to_partition (large_huge.m_map, s);
6840 if (large_huge.m_vars[p] == NULL_TREE)
6841 {
6842 large_huge.m_vars[p] = SSA_NAME_VAR (s);
6843 mark_addressable (SSA_NAME_VAR (s));
6844 }
6845 }
6846 }
6847 tree atype = NULL_TREE;
6848 if (dump_file && (dump_flags & TDF_DETAILS))
6849 fprintf (dump_file, "Mapping SSA_NAMEs to decls:\n");
6850 EXECUTE_IF_SET_IN_BITMAP (large_huge.m_names, 0, i, bi)
6851 {
6852 tree s = ssa_name (i);
6853 int p = var_to_partition (large_huge.m_map, s);
6854 if (large_huge.m_vars[p] == NULL_TREE)
6855 {
6856 if (atype == NULL_TREE
6857 || !tree_int_cst_equal (TYPE_SIZE (atype),
6858 TYPE_SIZE (TREE_TYPE (s))))
6859 {
6860 unsigned HOST_WIDE_INT nelts
6861 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (s))) / limb_prec;
6862 atype = build_array_type_nelts (large_huge.m_limb_type,
6863 nelts);
6864 }
6865 large_huge.m_vars[p] = create_tmp_var (atype, "bitint");
6866 mark_addressable (large_huge.m_vars[p]);
6867 }
6868 if (dump_file && (dump_flags & TDF_DETAILS))
6869 {
6870 print_generic_expr (dump_file, s, TDF_SLIM);
6871 fprintf (dump_file, " -> ");
6872 print_generic_expr (dump_file, large_huge.m_vars[p], TDF_SLIM);
6873 fprintf (dump_file, "\n");
6874 }
6875 }
6876 }
6877
6878 FOR_EACH_BB_REVERSE_FN (bb, cfun)
6879 {
6880 gimple_stmt_iterator prev;
6881 for (gimple_stmt_iterator gsi = gsi_last_bb (bb); !gsi_end_p (gsi);
6882 gsi = prev)
6883 {
6884 prev = gsi;
6885 gsi_prev (&prev);
6886 ssa_op_iter iter;
6887 gimple *stmt = gsi_stmt (gsi);
6888 if (is_gimple_debug (stmt))
6889 continue;
6890 bitint_prec_kind kind = bitint_prec_small;
6891 tree t;
6892 FOR_EACH_SSA_TREE_OPERAND (t, stmt, iter, SSA_OP_ALL_OPERANDS)
6893 if (TREE_CODE (TREE_TYPE (t)) == BITINT_TYPE)
6894 {
6895 bitint_prec_kind this_kind
6896 = bitint_precision_kind (TREE_TYPE (t));
6897 kind = MAX (kind, this_kind);
6898 }
6899 if (is_gimple_assign (stmt) && gimple_store_p (stmt))
6900 {
6901 t = gimple_assign_rhs1 (stmt);
6902 if (TREE_CODE (TREE_TYPE (t)) == BITINT_TYPE)
6903 {
6904 bitint_prec_kind this_kind
6905 = bitint_precision_kind (TREE_TYPE (t));
6906 kind = MAX (kind, this_kind);
6907 }
6908 }
6909 if (is_gimple_assign (stmt)
6910 && gimple_assign_rhs_code (stmt) == FLOAT_EXPR)
6911 {
6912 t = gimple_assign_rhs1 (stmt);
6913 if (TREE_CODE (TREE_TYPE (t)) == BITINT_TYPE
6914 && TREE_CODE (t) == INTEGER_CST)
6915 {
6916 bitint_prec_kind this_kind
6917 = bitint_precision_kind (TREE_TYPE (t));
6918 kind = MAX (kind, this_kind);
6919 }
6920 }
6921 if (is_gimple_call (stmt))
6922 {
6923 t = gimple_call_lhs (stmt);
6924 if (t && TREE_CODE (TREE_TYPE (t)) == COMPLEX_TYPE)
6925 {
6926 bitint_prec_kind this_kind = arith_overflow_arg_kind (stmt);
6927 kind = MAX (kind, this_kind);
6928 if (TREE_CODE (TREE_TYPE (TREE_TYPE (t))) == BITINT_TYPE)
6929 {
6930 this_kind
6931 = bitint_precision_kind (TREE_TYPE (TREE_TYPE (t)));
6932 kind = MAX (kind, this_kind);
6933 }
6934 }
6935 }
6936 if (kind == bitint_prec_small)
6937 continue;
6938 switch (gimple_code (stmt))
6939 {
6940 case GIMPLE_CALL:
6941 /* For now. We'll need to handle some internal functions and
6942 perhaps some builtins. */
6943 if (kind == bitint_prec_middle)
6944 continue;
6945 break;
6946 case GIMPLE_ASM:
6947 if (kind == bitint_prec_middle)
6948 continue;
6949 break;
6950 case GIMPLE_RETURN:
6951 continue;
6952 case GIMPLE_ASSIGN:
6953 if (gimple_clobber_p (stmt))
6954 continue;
6955 if (kind >= bitint_prec_large)
6956 break;
6957 if (gimple_assign_single_p (stmt))
6958 /* No need to lower copies, loads or stores. */
6959 continue;
6960 if (gimple_assign_cast_p (stmt))
6961 {
6962 tree lhs = gimple_assign_lhs (stmt);
6963 tree rhs = gimple_assign_rhs1 (stmt);
6964 if (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
6965 && INTEGRAL_TYPE_P (TREE_TYPE (rhs))
6966 && (TYPE_PRECISION (TREE_TYPE (lhs))
6967 == TYPE_PRECISION (TREE_TYPE (rhs))))
6968 /* No need to lower casts to same precision. */
6969 continue;
6970 }
6971 break;
6972 default:
6973 break;
6974 }
6975
6976 if (kind == bitint_prec_middle)
6977 {
6978 tree type = NULL_TREE;
6979 /* Middle _BitInt(N) is rewritten to casts to INTEGER_TYPEs
6980 with the same precision and back. */
6981 unsigned int nops = gimple_num_ops (stmt);
6982 for (unsigned int i = is_gimple_assign (stmt) ? 1 : 0;
6983 i < nops; ++i)
6984 if (tree op = gimple_op (stmt, i))
6985 {
6986 tree nop = maybe_cast_middle_bitint (&gsi, op, type);
6987 if (nop != op)
6988 gimple_set_op (stmt, i, nop);
6989 else if (COMPARISON_CLASS_P (op))
6990 {
6991 TREE_OPERAND (op, 0)
6992 = maybe_cast_middle_bitint (&gsi,
6993 TREE_OPERAND (op, 0),
6994 type);
6995 TREE_OPERAND (op, 1)
6996 = maybe_cast_middle_bitint (&gsi,
6997 TREE_OPERAND (op, 1),
6998 type);
6999 }
7000 else if (TREE_CODE (op) == CASE_LABEL_EXPR)
7001 {
7002 CASE_LOW (op)
7003 = maybe_cast_middle_bitint (&gsi, CASE_LOW (op),
7004 type);
7005 CASE_HIGH (op)
7006 = maybe_cast_middle_bitint (&gsi, CASE_HIGH (op),
7007 type);
7008 }
7009 }
7010 if (tree lhs = gimple_get_lhs (stmt))
7011 if (TREE_CODE (TREE_TYPE (lhs)) == BITINT_TYPE
7012 && (bitint_precision_kind (TREE_TYPE (lhs))
7013 == bitint_prec_middle))
7014 {
7015 int prec = TYPE_PRECISION (TREE_TYPE (lhs));
7016 int uns = TYPE_UNSIGNED (TREE_TYPE (lhs));
7017 type = build_nonstandard_integer_type (prec, uns);
7018 tree lhs2 = make_ssa_name (type);
7019 gimple_set_lhs (stmt, lhs2);
7020 gimple *g = gimple_build_assign (lhs, NOP_EXPR, lhs2);
7021 if (stmt_ends_bb_p (stmt))
7022 {
7023 edge e = find_fallthru_edge (gsi_bb (gsi)->succs);
7024 gsi_insert_on_edge (e, g);
7025 edge_insertions = true;
7026 }
7027 else
7028 gsi_insert_after (&gsi, g, GSI_SAME_STMT);
7029 }
7030 update_stmt (stmt);
7031 continue;
7032 }
7033
7034 if (tree lhs = gimple_get_lhs (stmt))
7035 if (TREE_CODE (lhs) == SSA_NAME)
7036 {
7037 tree type = TREE_TYPE (lhs);
7038 if (TREE_CODE (type) == COMPLEX_TYPE)
7039 type = TREE_TYPE (type);
7040 if (TREE_CODE (type) == BITINT_TYPE
7041 && bitint_precision_kind (type) >= bitint_prec_large
7042 && (large_huge.m_names == NULL
7043 || !bitmap_bit_p (large_huge.m_names,
7044 SSA_NAME_VERSION (lhs))))
7045 continue;
7046 }
7047
7048 large_huge.lower_stmt (stmt);
7049 }
7050
7051 tree atype = NULL_TREE;
7052 for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
7053 gsi_next (&gsi))
7054 {
7055 gphi *phi = gsi.phi ();
7056 tree lhs = gimple_phi_result (phi);
7057 if (TREE_CODE (TREE_TYPE (lhs)) != BITINT_TYPE
7058 || bitint_precision_kind (TREE_TYPE (lhs)) < bitint_prec_large)
7059 continue;
7060 int p1 = var_to_partition (large_huge.m_map, lhs);
7061 gcc_assert (large_huge.m_vars[p1] != NULL_TREE);
7062 tree v1 = large_huge.m_vars[p1];
7063 for (unsigned i = 0; i < gimple_phi_num_args (phi); ++i)
7064 {
7065 tree arg = gimple_phi_arg_def (phi, i);
7066 edge e = gimple_phi_arg_edge (phi, i);
7067 gimple *g;
7068 switch (TREE_CODE (arg))
7069 {
7070 case INTEGER_CST:
7071 if (integer_zerop (arg) && VAR_P (v1))
7072 {
7073 tree zero = build_zero_cst (TREE_TYPE (v1));
7074 g = gimple_build_assign (v1, zero);
7075 gsi_insert_on_edge (e, g);
7076 edge_insertions = true;
7077 break;
7078 }
7079 int ext;
7080 unsigned int min_prec, prec, rem;
7081 tree c;
7082 prec = TYPE_PRECISION (TREE_TYPE (arg));
7083 rem = prec % (2 * limb_prec);
7084 min_prec = bitint_min_cst_precision (arg, ext);
7085 if (min_prec > prec - rem - 2 * limb_prec
7086 && min_prec > (unsigned) limb_prec)
7087 /* Constant which has enough significant bits that it
7088 isn't worth trying to save .rodata space by extending
7089 from smaller number. */
7090 min_prec = prec;
7091 else
7092 {
7093 min_prec = CEIL (min_prec, limb_prec) * limb_prec;
7094 if (min_prec > (unsigned) limb_prec
7095 && abi_limb_prec > limb_prec)
7096 {
7097 /* For targets with ABI limb precision higher than
7098 limb precision round to ABI limb precision,
7099 otherwise c can contain padding bits. */
7100 min_prec
7101 = CEIL (min_prec, abi_limb_prec) * abi_limb_prec;
7102 if (min_prec > prec - rem - 2 * limb_prec)
7103 min_prec = prec;
7104 }
7105 }
7106 if (min_prec == 0)
7107 c = NULL_TREE;
7108 else if (min_prec == prec)
7109 c = tree_output_constant_def (arg);
7110 else if (min_prec == (unsigned) limb_prec)
7111 c = fold_convert (large_huge.m_limb_type, arg);
7112 else
7113 {
7114 tree ctype = build_bitint_type (min_prec, 1);
7115 c = tree_output_constant_def (fold_convert (ctype, arg));
7116 }
7117 if (c)
7118 {
7119 if (VAR_P (v1) && min_prec == prec)
7120 {
7121 tree v2 = build1 (VIEW_CONVERT_EXPR,
7122 TREE_TYPE (v1), c);
7123 g = gimple_build_assign (v1, v2);
7124 gsi_insert_on_edge (e, g);
7125 edge_insertions = true;
7126 break;
7127 }
7128 if (TREE_CODE (TREE_TYPE (c)) == INTEGER_TYPE)
7129 g = gimple_build_assign (build1 (VIEW_CONVERT_EXPR,
7130 TREE_TYPE (c), v1),
7131 c);
7132 else
7133 {
7134 unsigned HOST_WIDE_INT nelts
7135 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (c)))
7136 / limb_prec;
7137 tree vtype
7138 = build_array_type_nelts (large_huge.m_limb_type,
7139 nelts);
7140 g = gimple_build_assign (build1 (VIEW_CONVERT_EXPR,
7141 vtype, v1),
7142 build1 (VIEW_CONVERT_EXPR,
7143 vtype, c));
7144 }
7145 gsi_insert_on_edge (e, g);
7146 if (min_prec == prec)
7147 {
7148 edge_insertions = true;
7149 break;
7150 }
7151 }
7152 if (ext == 0)
7153 {
7154 unsigned HOST_WIDE_INT nelts
7155 = (tree_to_uhwi (TYPE_SIZE (TREE_TYPE (v1)))
7156 - min_prec) / limb_prec;
7157 tree vtype
7158 = build_array_type_nelts (large_huge.m_limb_type,
7159 nelts);
7160 tree ptype = build_pointer_type (TREE_TYPE (v1));
7161 tree off;
7162 if (c)
7163 off = fold_convert (ptype,
7164 TYPE_SIZE_UNIT (TREE_TYPE (c)));
7165 else
7166 off = build_zero_cst (ptype);
7167 tree vd = build2 (MEM_REF, vtype,
7168 build_fold_addr_expr (v1), off);
7169 g = gimple_build_assign (vd, build_zero_cst (vtype));
7170 }
7171 else
7172 {
7173 tree vd = v1;
7174 if (c)
7175 {
7176 tree ptype = build_pointer_type (TREE_TYPE (v1));
7177 tree off
7178 = fold_convert (ptype,
7179 TYPE_SIZE_UNIT (TREE_TYPE (c)));
7180 vd = build2 (MEM_REF, large_huge.m_limb_type,
7181 build_fold_addr_expr (v1), off);
7182 }
7183 vd = build_fold_addr_expr (vd);
7184 unsigned HOST_WIDE_INT nbytes
7185 = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (v1)));
7186 if (c)
7187 nbytes
7188 -= tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (c)));
7189 tree fn = builtin_decl_implicit (BUILT_IN_MEMSET);
7190 g = gimple_build_call (fn, 3, vd,
7191 integer_minus_one_node,
7192 build_int_cst (sizetype,
7193 nbytes));
7194 }
7195 gsi_insert_on_edge (e, g);
7196 edge_insertions = true;
7197 break;
7198 default:
7199 gcc_unreachable ();
7200 case SSA_NAME:
7201 if (gimple_code (SSA_NAME_DEF_STMT (arg)) == GIMPLE_NOP)
7202 {
7203 if (large_huge.m_names == NULL
7204 || !bitmap_bit_p (large_huge.m_names,
7205 SSA_NAME_VERSION (arg)))
7206 continue;
7207 }
7208 int p2 = var_to_partition (large_huge.m_map, arg);
7209 if (p1 == p2)
7210 continue;
7211 gcc_assert (large_huge.m_vars[p2] != NULL_TREE);
7212 tree v2 = large_huge.m_vars[p2];
7213 if (VAR_P (v1) && VAR_P (v2))
7214 g = gimple_build_assign (v1, v2);
7215 else if (VAR_P (v1))
7216 g = gimple_build_assign (v1, build1 (VIEW_CONVERT_EXPR,
7217 TREE_TYPE (v1), v2));
7218 else if (VAR_P (v2))
7219 g = gimple_build_assign (build1 (VIEW_CONVERT_EXPR,
7220 TREE_TYPE (v2), v1), v2);
7221 else
7222 {
7223 if (atype == NULL_TREE
7224 || !tree_int_cst_equal (TYPE_SIZE (atype),
7225 TYPE_SIZE (TREE_TYPE (lhs))))
7226 {
7227 unsigned HOST_WIDE_INT nelts
7228 = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (lhs)))
7229 / limb_prec;
7230 atype
7231 = build_array_type_nelts (large_huge.m_limb_type,
7232 nelts);
7233 }
7234 g = gimple_build_assign (build1 (VIEW_CONVERT_EXPR,
7235 atype, v1),
7236 build1 (VIEW_CONVERT_EXPR,
7237 atype, v2));
7238 }
7239 gsi_insert_on_edge (e, g);
7240 edge_insertions = true;
7241 break;
7242 }
7243 }
7244 }
7245 }
7246
7247 if (large_huge.m_names || has_large_huge)
7248 {
7249 gimple *nop = NULL;
7250 for (i = 0; i < num_ssa_names; ++i)
7251 {
7252 tree s = ssa_name (i);
7253 if (s == NULL_TREE)
7254 continue;
7255 tree type = TREE_TYPE (s);
7256 if (TREE_CODE (type) == COMPLEX_TYPE)
7257 type = TREE_TYPE (type);
7258 if (TREE_CODE (type) == BITINT_TYPE
7259 && bitint_precision_kind (type) >= bitint_prec_large)
7260 {
7261 if (large_huge.m_preserved
7262 && bitmap_bit_p (large_huge.m_preserved,
7263 SSA_NAME_VERSION (s)))
7264 continue;
7265 gimple *g = SSA_NAME_DEF_STMT (s);
7266 if (gimple_code (g) == GIMPLE_NOP)
7267 {
7268 if (SSA_NAME_VAR (s))
7269 set_ssa_default_def (cfun, SSA_NAME_VAR (s), NULL_TREE);
7270 release_ssa_name (s);
7271 continue;
7272 }
7273 if (gimple_bb (g) == NULL)
7274 {
7275 release_ssa_name (s);
7276 continue;
7277 }
7278 if (gimple_code (g) != GIMPLE_ASM)
7279 {
7280 gimple_stmt_iterator gsi = gsi_for_stmt (g);
7281 bool save_vta = flag_var_tracking_assignments;
7282 flag_var_tracking_assignments = false;
7283 gsi_remove (&gsi, true);
7284 flag_var_tracking_assignments = save_vta;
7285 }
7286 if (nop == NULL)
7287 nop = gimple_build_nop ();
7288 SSA_NAME_DEF_STMT (s) = nop;
7289 release_ssa_name (s);
7290 }
7291 }
7292 if (optimize)
7293 disable_ranger (cfun);
7294 }
7295
7296 if (edge_insertions)
7297 gsi_commit_edge_inserts ();
7298
7299 /* Fix up arguments of ECF_RETURNS_TWICE calls. Those were temporarily
7300 inserted before the call, but that is invalid IL, so move them to the
7301 right place and add corresponding PHIs. */
7302 if (!large_huge.m_returns_twice_calls.is_empty ())
7303 {
7304 auto_vec<gimple *, 16> arg_stmts;
7305 while (!large_huge.m_returns_twice_calls.is_empty ())
7306 {
7307 gimple *stmt = large_huge.m_returns_twice_calls.pop ();
7308 gimple_stmt_iterator gsi = gsi_after_labels (gimple_bb (stmt));
7309 while (gsi_stmt (gsi) != stmt)
7310 {
7311 if (is_gimple_debug (gsi_stmt (gsi)))
7312 gsi_next (&gsi);
7313 else
7314 {
7315 arg_stmts.safe_push (gsi_stmt (gsi));
7316 gsi_remove (&gsi, false);
7317 }
7318 }
7319 gimple *g;
7320 basic_block bb = NULL;
7321 edge e = NULL, ead = NULL;
7322 FOR_EACH_VEC_ELT (arg_stmts, i, g)
7323 {
7324 gsi_safe_insert_before (&gsi, g);
7325 if (i == 0)
7326 {
7327 bb = gimple_bb (stmt);
7328 gcc_checking_assert (EDGE_COUNT (bb->preds) == 2);
7329 e = EDGE_PRED (bb, 0);
7330 ead = EDGE_PRED (bb, 1);
7331 if ((ead->flags & EDGE_ABNORMAL) == 0)
7332 std::swap (e, ead);
7333 gcc_checking_assert ((e->flags & EDGE_ABNORMAL) == 0
7334 && (ead->flags & EDGE_ABNORMAL));
7335 }
7336 tree lhs = gimple_assign_lhs (g);
7337 tree arg = lhs;
7338 gphi *phi = create_phi_node (copy_ssa_name (arg), bb);
7339 add_phi_arg (phi, arg, e, UNKNOWN_LOCATION);
7340 tree var = create_tmp_reg (TREE_TYPE (arg));
7341 suppress_warning (var, OPT_Wuninitialized);
7342 arg = get_or_create_ssa_default_def (cfun, var);
7343 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (arg) = 1;
7344 add_phi_arg (phi, arg, ead, UNKNOWN_LOCATION);
7345 arg = gimple_phi_result (phi);
7346 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (arg) = 1;
7347 imm_use_iterator iter;
7348 gimple *use_stmt;
7349 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
7350 {
7351 if (use_stmt == phi)
7352 continue;
7353 gcc_checking_assert (use_stmt == stmt);
7354 use_operand_p use_p;
7355 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
7356 SET_USE (use_p, arg);
7357 }
7358 }
7359 update_stmt (stmt);
7360 arg_stmts.truncate (0);
7361 }
7362 }
7363
7364 return ret;
7365 }
7366
7367 namespace {
7368
7369 const pass_data pass_data_lower_bitint =
7370 {
7371 GIMPLE_PASS, /* type */
7372 "bitintlower", /* name */
7373 OPTGROUP_NONE, /* optinfo_flags */
7374 TV_NONE, /* tv_id */
7375 PROP_ssa, /* properties_required */
7376 PROP_gimple_lbitint, /* properties_provided */
7377 0, /* properties_destroyed */
7378 0, /* todo_flags_start */
7379 0, /* todo_flags_finish */
7380 };
7381
7382 class pass_lower_bitint : public gimple_opt_pass
7383 {
7384 public:
7385 pass_lower_bitint (gcc::context *ctxt)
7386 : gimple_opt_pass (pass_data_lower_bitint, ctxt)
7387 {}
7388
7389 /* opt_pass methods: */
7390 opt_pass * clone () final override { return new pass_lower_bitint (m_ctxt); }
7391 unsigned int execute (function *) final override
7392 {
7393 return gimple_lower_bitint ();
7394 }
7395
7396 }; // class pass_lower_bitint
7397
7398 } // anon namespace
7399
7400 gimple_opt_pass *
7401 make_pass_lower_bitint (gcc::context *ctxt)
7402 {
7403 return new pass_lower_bitint (ctxt);
7404 }
7405
7406
7407 namespace {
7408
7409 const pass_data pass_data_lower_bitint_O0 =
7410 {
7411 GIMPLE_PASS, /* type */
7412 "bitintlower0", /* name */
7413 OPTGROUP_NONE, /* optinfo_flags */
7414 TV_NONE, /* tv_id */
7415 PROP_cfg, /* properties_required */
7416 PROP_gimple_lbitint, /* properties_provided */
7417 0, /* properties_destroyed */
7418 0, /* todo_flags_start */
7419 0, /* todo_flags_finish */
7420 };
7421
7422 class pass_lower_bitint_O0 : public gimple_opt_pass
7423 {
7424 public:
7425 pass_lower_bitint_O0 (gcc::context *ctxt)
7426 : gimple_opt_pass (pass_data_lower_bitint_O0, ctxt)
7427 {}
7428
7429 /* opt_pass methods: */
7430 bool gate (function *fun) final override
7431 {
7432 /* With errors, normal optimization passes are not run. If we don't
7433 lower bitint operations at all, rtl expansion will abort. */
7434 return !(fun->curr_properties & PROP_gimple_lbitint);
7435 }
7436
7437 unsigned int execute (function *) final override
7438 {
7439 return gimple_lower_bitint ();
7440 }
7441
7442 }; // class pass_lower_bitint_O0
7443
7444 } // anon namespace
7445
7446 gimple_opt_pass *
7447 make_pass_lower_bitint_O0 (gcc::context *ctxt)
7448 {
7449 return new pass_lower_bitint_O0 (ctxt);
7450 }