2 Copyright (C) 2011-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
29 #include "fold-const.h"
30 #include "internal-fn.h"
31 #include "stor-layout.h"
33 #include "insn-config.h"
42 #include "insn-codes.h"
46 #include "stringpool.h"
47 #include "tree-ssanames.h"
48 #include "diagnostic-core.h"
50 /* The names of each internal function, indexed by function number. */
51 const char *const internal_fn_name_array
[] = {
52 #define DEF_INTERNAL_FN(CODE, FLAGS, FNSPEC) #CODE,
53 #include "internal-fn.def"
54 #undef DEF_INTERNAL_FN
58 /* The ECF_* flags of each internal function, indexed by function number. */
59 const int internal_fn_flags_array
[] = {
60 #define DEF_INTERNAL_FN(CODE, FLAGS, FNSPEC) FLAGS,
61 #include "internal-fn.def"
62 #undef DEF_INTERNAL_FN
66 /* Fnspec of each internal function, indexed by function number. */
67 const_tree internal_fn_fnspec_array
[IFN_LAST
+ 1];
72 #define DEF_INTERNAL_FN(CODE, FLAGS, FNSPEC) \
73 if (FNSPEC) internal_fn_fnspec_array[IFN_##CODE] = \
74 build_string ((int) sizeof (FNSPEC), FNSPEC ? FNSPEC : "");
75 #include "internal-fn.def"
76 #undef DEF_INTERNAL_FN
77 internal_fn_fnspec_array
[IFN_LAST
] = 0;
80 /* ARRAY_TYPE is an array of vector modes. Return the associated insn
81 for load-lanes-style optab OPTAB. The insn must exist. */
84 get_multi_vector_move (tree array_type
, convert_optab optab
)
90 gcc_assert (TREE_CODE (array_type
) == ARRAY_TYPE
);
91 imode
= TYPE_MODE (array_type
);
92 vmode
= TYPE_MODE (TREE_TYPE (array_type
));
94 icode
= convert_optab_handler (optab
, imode
, vmode
);
95 gcc_assert (icode
!= CODE_FOR_nothing
);
99 /* Expand LOAD_LANES call STMT. */
102 expand_LOAD_LANES (gcall
*stmt
)
104 struct expand_operand ops
[2];
108 lhs
= gimple_call_lhs (stmt
);
109 rhs
= gimple_call_arg (stmt
, 0);
110 type
= TREE_TYPE (lhs
);
112 target
= expand_expr (lhs
, NULL_RTX
, VOIDmode
, EXPAND_WRITE
);
113 mem
= expand_normal (rhs
);
115 gcc_assert (MEM_P (mem
));
116 PUT_MODE (mem
, TYPE_MODE (type
));
118 create_output_operand (&ops
[0], target
, TYPE_MODE (type
));
119 create_fixed_operand (&ops
[1], mem
);
120 expand_insn (get_multi_vector_move (type
, vec_load_lanes_optab
), 2, ops
);
123 /* Expand STORE_LANES call STMT. */
126 expand_STORE_LANES (gcall
*stmt
)
128 struct expand_operand ops
[2];
132 lhs
= gimple_call_lhs (stmt
);
133 rhs
= gimple_call_arg (stmt
, 0);
134 type
= TREE_TYPE (rhs
);
136 target
= expand_expr (lhs
, NULL_RTX
, VOIDmode
, EXPAND_WRITE
);
137 reg
= expand_normal (rhs
);
139 gcc_assert (MEM_P (target
));
140 PUT_MODE (target
, TYPE_MODE (type
));
142 create_fixed_operand (&ops
[0], target
);
143 create_input_operand (&ops
[1], reg
, TYPE_MODE (type
));
144 expand_insn (get_multi_vector_move (type
, vec_store_lanes_optab
), 2, ops
);
148 expand_ANNOTATE (gcall
*)
153 /* This should get expanded in adjust_simduid_builtins. */
156 expand_GOMP_SIMD_LANE (gcall
*)
161 /* This should get expanded in adjust_simduid_builtins. */
164 expand_GOMP_SIMD_VF (gcall
*)
169 /* This should get expanded in adjust_simduid_builtins. */
172 expand_GOMP_SIMD_LAST_LANE (gcall
*)
177 /* This should get expanded in the sanopt pass. */
180 expand_UBSAN_NULL (gcall
*)
185 /* This should get expanded in the sanopt pass. */
188 expand_UBSAN_BOUNDS (gcall
*)
193 /* This should get expanded in the sanopt pass. */
196 expand_UBSAN_VPTR (gcall
*)
201 /* This should get expanded in the sanopt pass. */
204 expand_UBSAN_OBJECT_SIZE (gcall
*)
209 /* This should get expanded in the sanopt pass. */
212 expand_ASAN_CHECK (gcall
*)
217 /* This should get expanded in the tsan pass. */
220 expand_TSAN_FUNC_EXIT (gcall
*)
225 /* Helper function for expand_addsub_overflow. Return 1
226 if ARG interpreted as signed in its precision is known to be always
227 positive or 2 if ARG is known to be always negative, or 3 if ARG may
228 be positive or negative. */
231 get_range_pos_neg (tree arg
)
233 if (arg
== error_mark_node
)
236 int prec
= TYPE_PRECISION (TREE_TYPE (arg
));
238 if (TREE_CODE (arg
) == INTEGER_CST
)
240 wide_int w
= wi::sext (arg
, prec
);
246 while (CONVERT_EXPR_P (arg
)
247 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg
, 0)))
248 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg
, 0))) <= prec
)
250 arg
= TREE_OPERAND (arg
, 0);
251 /* Narrower value zero extended into wider type
252 will always result in positive values. */
253 if (TYPE_UNSIGNED (TREE_TYPE (arg
))
254 && TYPE_PRECISION (TREE_TYPE (arg
)) < prec
)
256 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
261 if (TREE_CODE (arg
) != SSA_NAME
)
263 wide_int arg_min
, arg_max
;
264 while (get_range_info (arg
, &arg_min
, &arg_max
) != VR_RANGE
)
266 gimple g
= SSA_NAME_DEF_STMT (arg
);
267 if (is_gimple_assign (g
)
268 && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (g
)))
270 tree t
= gimple_assign_rhs1 (g
);
271 if (INTEGRAL_TYPE_P (TREE_TYPE (t
))
272 && TYPE_PRECISION (TREE_TYPE (t
)) <= prec
)
274 if (TYPE_UNSIGNED (TREE_TYPE (t
))
275 && TYPE_PRECISION (TREE_TYPE (t
)) < prec
)
277 prec
= TYPE_PRECISION (TREE_TYPE (t
));
286 if (TYPE_UNSIGNED (TREE_TYPE (arg
)))
288 /* For unsigned values, the "positive" range comes
289 below the "negative" range. */
290 if (!wi::neg_p (wi::sext (arg_max
, prec
), SIGNED
))
292 if (wi::neg_p (wi::sext (arg_min
, prec
), SIGNED
))
297 if (!wi::neg_p (wi::sext (arg_min
, prec
), SIGNED
))
299 if (wi::neg_p (wi::sext (arg_max
, prec
), SIGNED
))
305 /* Return minimum precision needed to represent all values
306 of ARG in SIGNed integral type. */
309 get_min_precision (tree arg
, signop sign
)
311 int prec
= TYPE_PRECISION (TREE_TYPE (arg
));
313 signop orig_sign
= sign
;
314 if (TREE_CODE (arg
) == INTEGER_CST
)
317 if (TYPE_SIGN (TREE_TYPE (arg
)) != sign
)
319 widest_int w
= wi::to_widest (arg
);
320 w
= wi::ext (w
, prec
, sign
);
321 p
= wi::min_precision (w
, sign
);
324 p
= wi::min_precision (arg
, sign
);
325 return MIN (p
, prec
);
327 while (CONVERT_EXPR_P (arg
)
328 && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg
, 0)))
329 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg
, 0))) <= prec
)
331 arg
= TREE_OPERAND (arg
, 0);
332 if (TYPE_PRECISION (TREE_TYPE (arg
)) < prec
)
334 if (TYPE_UNSIGNED (TREE_TYPE (arg
)))
336 else if (sign
== UNSIGNED
&& get_range_pos_neg (arg
) != 1)
337 return prec
+ (orig_sign
!= sign
);
338 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
341 return prec
+ (orig_sign
!= sign
);
343 if (TREE_CODE (arg
) != SSA_NAME
)
344 return prec
+ (orig_sign
!= sign
);
345 wide_int arg_min
, arg_max
;
346 while (get_range_info (arg
, &arg_min
, &arg_max
) != VR_RANGE
)
348 gimple g
= SSA_NAME_DEF_STMT (arg
);
349 if (is_gimple_assign (g
)
350 && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (g
)))
352 tree t
= gimple_assign_rhs1 (g
);
353 if (INTEGRAL_TYPE_P (TREE_TYPE (t
))
354 && TYPE_PRECISION (TREE_TYPE (t
)) <= prec
)
357 if (TYPE_PRECISION (TREE_TYPE (arg
)) < prec
)
359 if (TYPE_UNSIGNED (TREE_TYPE (arg
)))
361 else if (sign
== UNSIGNED
&& get_range_pos_neg (arg
) != 1)
362 return prec
+ (orig_sign
!= sign
);
363 prec
= TYPE_PRECISION (TREE_TYPE (arg
));
366 return prec
+ (orig_sign
!= sign
);
370 return prec
+ (orig_sign
!= sign
);
372 if (sign
== TYPE_SIGN (TREE_TYPE (arg
)))
374 int p1
= wi::min_precision (arg_min
, sign
);
375 int p2
= wi::min_precision (arg_max
, sign
);
377 prec
= MIN (prec
, p1
);
379 else if (sign
== UNSIGNED
&& !wi::neg_p (arg_min
, SIGNED
))
381 int p
= wi::min_precision (arg_max
, UNSIGNED
);
382 prec
= MIN (prec
, p
);
384 return prec
+ (orig_sign
!= sign
);
387 /* Helper for expand_*_overflow. Store RES into the __real__ part
388 of TARGET. If RES has larger MODE than __real__ part of TARGET,
389 set the __imag__ part to 1 if RES doesn't fit into it. */
392 expand_arith_overflow_result_store (tree lhs
, rtx target
,
393 machine_mode mode
, rtx res
)
395 machine_mode tgtmode
= GET_MODE_INNER (GET_MODE (target
));
399 rtx_code_label
*done_label
= gen_label_rtx ();
400 int uns
= TYPE_UNSIGNED (TREE_TYPE (TREE_TYPE (lhs
)));
401 lres
= convert_modes (tgtmode
, mode
, res
, uns
);
402 gcc_assert (GET_MODE_PRECISION (tgtmode
) < GET_MODE_PRECISION (mode
));
403 do_compare_rtx_and_jump (res
, convert_modes (mode
, tgtmode
, lres
, uns
),
404 EQ
, true, mode
, NULL_RTX
, NULL
, done_label
,
406 write_complex_part (target
, const1_rtx
, true);
407 emit_label (done_label
);
409 write_complex_part (target
, lres
, false);
412 /* Helper for expand_*_overflow. Store RES into TARGET. */
415 expand_ubsan_result_store (rtx target
, rtx res
)
417 if (GET_CODE (target
) == SUBREG
&& SUBREG_PROMOTED_VAR_P (target
))
418 /* If this is a scalar in a register that is stored in a wider mode
419 than the declared mode, compute the result into its declared mode
420 and then convert to the wider mode. Our value is the computed
422 convert_move (SUBREG_REG (target
), res
, SUBREG_PROMOTED_SIGN (target
));
424 emit_move_insn (target
, res
);
427 /* Add sub/add overflow checking to the statement STMT.
428 CODE says whether the operation is +, or -. */
431 expand_addsub_overflow (location_t loc
, tree_code code
, tree lhs
,
432 tree arg0
, tree arg1
, bool unsr_p
, bool uns0_p
,
433 bool uns1_p
, bool is_ubsan
)
435 rtx res
, target
= NULL_RTX
;
437 rtx_code_label
*done_label
= gen_label_rtx ();
438 rtx_code_label
*do_error
= gen_label_rtx ();
439 do_pending_stack_adjust ();
440 rtx op0
= expand_normal (arg0
);
441 rtx op1
= expand_normal (arg1
);
442 machine_mode mode
= TYPE_MODE (TREE_TYPE (arg0
));
443 int prec
= GET_MODE_PRECISION (mode
);
444 rtx sgn
= immed_wide_int_const (wi::min_value (prec
, SIGNED
), mode
);
448 gcc_assert (!unsr_p
&& !uns0_p
&& !uns1_p
);
452 target
= expand_expr (lhs
, NULL_RTX
, VOIDmode
, EXPAND_WRITE
);
454 write_complex_part (target
, const0_rtx
, true);
457 /* We assume both operands and result have the same precision
458 here (GET_MODE_BITSIZE (mode)), S stands for signed type
459 with that precision, U for unsigned type with that precision,
460 sgn for unsigned most significant bit in that precision.
461 s1 is signed first operand, u1 is unsigned first operand,
462 s2 is signed second operand, u2 is unsigned second operand,
463 sr is signed result, ur is unsigned result and the following
464 rules say how to compute result (which is always result of
465 the operands as if both were unsigned, cast to the right
466 signedness) and how to compute whether operation overflowed.
469 res = (S) ((U) s1 + (U) s2)
470 ovf = s2 < 0 ? res > s1 : res < s1 (or jump on overflow)
472 res = (S) ((U) s1 - (U) s2)
473 ovf = s2 < 0 ? res < s1 : res > s2 (or jump on overflow)
476 ovf = res < u1 (or jump on carry, but RTL opts will handle it)
479 ovf = res > u1 (or jump on carry, but RTL opts will handle it)
481 res = (S) ((U) s1 + u2)
482 ovf = ((U) res ^ sgn) < u2
487 ovf = t1 < 0 ? t2 > s1 : t2 < s1 (or jump on overflow)
489 res = (S) ((U) s1 - u2)
490 ovf = u2 > ((U) s1 ^ sgn)
493 ovf = s1 < 0 || u2 > (U) s1
496 ovf = u1 >= ((U) s2 ^ sgn)
501 ovf = s2 < 0 ? (S) t2 < (S) t1 : (S) t2 > (S) t1 (or jump on overflow)
503 res = (U) s1 + (U) s2
504 ovf = s2 < 0 ? (s1 | (S) res) < 0) : (s1 & (S) res) < 0)
507 ovf = (U) res < u2 || res < 0
510 ovf = u1 >= u2 ? res < 0 : res >= 0
512 res = (U) s1 - (U) s2
513 ovf = s2 >= 0 ? ((s1 | (S) res) < 0) : ((s1 & (S) res) < 0) */
515 if (code
== PLUS_EXPR
&& uns0_p
&& !uns1_p
)
517 /* PLUS_EXPR is commutative, if operand signedness differs,
518 canonicalize to the first operand being signed and second
519 unsigned to simplify following code. */
520 std::swap (op0
, op1
);
521 std::swap (arg0
, arg1
);
527 if (uns0_p
&& uns1_p
&& unsr_p
)
529 /* Compute the operation. On RTL level, the addition is always
531 res
= expand_binop (mode
, code
== PLUS_EXPR
? add_optab
: sub_optab
,
532 op0
, op1
, NULL_RTX
, false, OPTAB_LIB_WIDEN
);
534 /* For PLUS_EXPR, the operation is commutative, so we can pick
535 operand to compare against. For prec <= BITS_PER_WORD, I think
536 preferring REG operand is better over CONST_INT, because
537 the CONST_INT might enlarge the instruction or CSE would need
538 to figure out we'd already loaded it into a register before.
539 For prec > BITS_PER_WORD, I think CONST_INT might be more beneficial,
540 as then the multi-word comparison can be perhaps simplified. */
541 if (code
== PLUS_EXPR
542 && (prec
<= BITS_PER_WORD
543 ? (CONST_SCALAR_INT_P (op0
) && REG_P (op1
))
544 : CONST_SCALAR_INT_P (op1
)))
546 do_compare_rtx_and_jump (res
, tem
, code
== PLUS_EXPR
? GEU
: LEU
,
547 true, mode
, NULL_RTX
, NULL
, done_label
,
553 if (!uns0_p
&& uns1_p
&& !unsr_p
)
555 /* Compute the operation. On RTL level, the addition is always
557 res
= expand_binop (mode
, code
== PLUS_EXPR
? add_optab
: sub_optab
,
558 op0
, op1
, NULL_RTX
, false, OPTAB_LIB_WIDEN
);
559 rtx tem
= expand_binop (mode
, add_optab
,
560 code
== PLUS_EXPR
? res
: op0
, sgn
,
561 NULL_RTX
, false, OPTAB_LIB_WIDEN
);
562 do_compare_rtx_and_jump (tem
, op1
, GEU
, true, mode
, NULL_RTX
, NULL
,
563 done_label
, PROB_VERY_LIKELY
);
568 if (code
== PLUS_EXPR
&& !uns0_p
&& uns1_p
&& unsr_p
)
570 op1
= expand_binop (mode
, add_optab
, op1
, sgn
, NULL_RTX
, false,
572 /* As we've changed op1, we have to avoid using the value range
573 for the original argument. */
574 arg1
= error_mark_node
;
580 if (code
== MINUS_EXPR
&& uns0_p
&& !uns1_p
&& unsr_p
)
582 op0
= expand_binop (mode
, add_optab
, op0
, sgn
, NULL_RTX
, false,
584 /* As we've changed op0, we have to avoid using the value range
585 for the original argument. */
586 arg0
= error_mark_node
;
592 if (code
== MINUS_EXPR
&& !uns0_p
&& uns1_p
&& unsr_p
)
594 /* Compute the operation. On RTL level, the addition is always
596 res
= expand_binop (mode
, sub_optab
, op0
, op1
, NULL_RTX
, false,
598 int pos_neg
= get_range_pos_neg (arg0
);
600 /* If ARG0 is known to be always negative, this is always overflow. */
601 emit_jump (do_error
);
602 else if (pos_neg
== 3)
603 /* If ARG0 is not known to be always positive, check at runtime. */
604 do_compare_rtx_and_jump (op0
, const0_rtx
, LT
, false, mode
, NULL_RTX
,
605 NULL
, do_error
, PROB_VERY_UNLIKELY
);
606 do_compare_rtx_and_jump (op1
, op0
, LEU
, true, mode
, NULL_RTX
, NULL
,
607 done_label
, PROB_VERY_LIKELY
);
612 if (code
== MINUS_EXPR
&& uns0_p
&& !uns1_p
&& !unsr_p
)
614 /* Compute the operation. On RTL level, the addition is always
616 res
= expand_binop (mode
, sub_optab
, op0
, op1
, NULL_RTX
, false,
618 rtx tem
= expand_binop (mode
, add_optab
, op1
, sgn
, NULL_RTX
, false,
620 do_compare_rtx_and_jump (op0
, tem
, LTU
, true, mode
, NULL_RTX
, NULL
,
621 done_label
, PROB_VERY_LIKELY
);
626 if (code
== PLUS_EXPR
&& uns0_p
&& uns1_p
&& !unsr_p
)
628 /* Compute the operation. On RTL level, the addition is always
630 res
= expand_binop (mode
, add_optab
, op0
, op1
, NULL_RTX
, false,
632 do_compare_rtx_and_jump (res
, const0_rtx
, LT
, false, mode
, NULL_RTX
,
633 NULL
, do_error
, PROB_VERY_UNLIKELY
);
635 /* The operation is commutative, so we can pick operand to compare
636 against. For prec <= BITS_PER_WORD, I think preferring REG operand
637 is better over CONST_INT, because the CONST_INT might enlarge the
638 instruction or CSE would need to figure out we'd already loaded it
639 into a register before. For prec > BITS_PER_WORD, I think CONST_INT
640 might be more beneficial, as then the multi-word comparison can be
641 perhaps simplified. */
642 if (prec
<= BITS_PER_WORD
643 ? (CONST_SCALAR_INT_P (op1
) && REG_P (op0
))
644 : CONST_SCALAR_INT_P (op0
))
646 do_compare_rtx_and_jump (res
, tem
, GEU
, true, mode
, NULL_RTX
, NULL
,
647 done_label
, PROB_VERY_LIKELY
);
652 if (!uns0_p
&& !uns1_p
&& unsr_p
)
654 /* Compute the operation. On RTL level, the addition is always
656 res
= expand_binop (mode
, code
== PLUS_EXPR
? add_optab
: sub_optab
,
657 op0
, op1
, NULL_RTX
, false, OPTAB_LIB_WIDEN
);
658 int pos_neg
= get_range_pos_neg (arg1
);
659 if (code
== PLUS_EXPR
)
661 int pos_neg0
= get_range_pos_neg (arg0
);
662 if (pos_neg0
!= 3 && pos_neg
== 3)
664 std::swap (op0
, op1
);
671 tem
= expand_binop (mode
, ((pos_neg
== 1) ^ (code
== MINUS_EXPR
))
672 ? and_optab
: ior_optab
,
673 op0
, res
, NULL_RTX
, false, OPTAB_LIB_WIDEN
);
674 do_compare_rtx_and_jump (tem
, const0_rtx
, GE
, false, mode
, NULL
,
675 NULL
, done_label
, PROB_VERY_LIKELY
);
679 rtx_code_label
*do_ior_label
= gen_label_rtx ();
680 do_compare_rtx_and_jump (op1
, const0_rtx
,
681 code
== MINUS_EXPR
? GE
: LT
, false, mode
,
682 NULL_RTX
, NULL
, do_ior_label
,
684 tem
= expand_binop (mode
, and_optab
, op0
, res
, NULL_RTX
, false,
686 do_compare_rtx_and_jump (tem
, const0_rtx
, GE
, false, mode
, NULL_RTX
,
687 NULL
, done_label
, PROB_VERY_LIKELY
);
688 emit_jump (do_error
);
689 emit_label (do_ior_label
);
690 tem
= expand_binop (mode
, ior_optab
, op0
, res
, NULL_RTX
, false,
692 do_compare_rtx_and_jump (tem
, const0_rtx
, GE
, false, mode
, NULL_RTX
,
693 NULL
, done_label
, PROB_VERY_LIKELY
);
699 if (code
== MINUS_EXPR
&& uns0_p
&& uns1_p
&& !unsr_p
)
701 /* Compute the operation. On RTL level, the addition is always
703 res
= expand_binop (mode
, sub_optab
, op0
, op1
, NULL_RTX
, false,
705 rtx_code_label
*op0_geu_op1
= gen_label_rtx ();
706 do_compare_rtx_and_jump (op0
, op1
, GEU
, true, mode
, NULL_RTX
, NULL
,
707 op0_geu_op1
, PROB_EVEN
);
708 do_compare_rtx_and_jump (res
, const0_rtx
, LT
, false, mode
, NULL_RTX
,
709 NULL
, done_label
, PROB_VERY_LIKELY
);
710 emit_jump (do_error
);
711 emit_label (op0_geu_op1
);
712 do_compare_rtx_and_jump (res
, const0_rtx
, GE
, false, mode
, NULL_RTX
,
713 NULL
, done_label
, PROB_VERY_LIKELY
);
717 gcc_assert (!uns0_p
&& !uns1_p
&& !unsr_p
);
721 enum insn_code icode
;
722 icode
= optab_handler (code
== PLUS_EXPR
? addv4_optab
: subv4_optab
, mode
);
723 if (icode
!= CODE_FOR_nothing
)
725 struct expand_operand ops
[4];
726 rtx_insn
*last
= get_last_insn ();
728 res
= gen_reg_rtx (mode
);
729 create_output_operand (&ops
[0], res
, mode
);
730 create_input_operand (&ops
[1], op0
, mode
);
731 create_input_operand (&ops
[2], op1
, mode
);
732 create_fixed_operand (&ops
[3], do_error
);
733 if (maybe_expand_insn (icode
, 4, ops
))
735 last
= get_last_insn ();
736 if (profile_status_for_fn (cfun
) != PROFILE_ABSENT
738 && any_condjump_p (last
)
739 && !find_reg_note (last
, REG_BR_PROB
, 0))
740 add_int_reg_note (last
, REG_BR_PROB
, PROB_VERY_UNLIKELY
);
741 emit_jump (done_label
);
745 delete_insns_since (last
);
746 icode
= CODE_FOR_nothing
;
750 if (icode
== CODE_FOR_nothing
)
752 rtx_code_label
*sub_check
= gen_label_rtx ();
755 /* Compute the operation. On RTL level, the addition is always
757 res
= expand_binop (mode
, code
== PLUS_EXPR
? add_optab
: sub_optab
,
758 op0
, op1
, NULL_RTX
, false, OPTAB_LIB_WIDEN
);
760 /* If we can prove one of the arguments (for MINUS_EXPR only
761 the second operand, as subtraction is not commutative) is always
762 non-negative or always negative, we can do just one comparison
763 and conditional jump instead of 2 at runtime, 3 present in the
764 emitted code. If one of the arguments is CONST_INT, all we
765 need is to make sure it is op1, then the first
766 do_compare_rtx_and_jump will be just folded. Otherwise try
767 to use range info if available. */
768 if (code
== PLUS_EXPR
&& CONST_INT_P (op0
))
769 std::swap (op0
, op1
);
770 else if (CONST_INT_P (op1
))
772 else if (code
== PLUS_EXPR
&& TREE_CODE (arg0
) == SSA_NAME
)
774 pos_neg
= get_range_pos_neg (arg0
);
776 std::swap (op0
, op1
);
778 if (pos_neg
== 3 && !CONST_INT_P (op1
) && TREE_CODE (arg1
) == SSA_NAME
)
779 pos_neg
= get_range_pos_neg (arg1
);
781 /* If the op1 is negative, we have to use a different check. */
783 do_compare_rtx_and_jump (op1
, const0_rtx
, LT
, false, mode
, NULL_RTX
,
784 NULL
, sub_check
, PROB_EVEN
);
786 /* Compare the result of the operation with one of the operands. */
788 do_compare_rtx_and_jump (res
, op0
, code
== PLUS_EXPR
? GE
: LE
,
789 false, mode
, NULL_RTX
, NULL
, done_label
,
792 /* If we get here, we have to print the error. */
795 emit_jump (do_error
);
797 emit_label (sub_check
);
800 /* We have k = a + b for b < 0 here. k <= a must hold. */
802 do_compare_rtx_and_jump (res
, op0
, code
== PLUS_EXPR
? LE
: GE
,
803 false, mode
, NULL_RTX
, NULL
, done_label
,
808 emit_label (do_error
);
811 /* Expand the ubsan builtin call. */
813 fn
= ubsan_build_overflow_builtin (code
, loc
, TREE_TYPE (arg0
),
817 do_pending_stack_adjust ();
820 write_complex_part (target
, const1_rtx
, true);
823 emit_label (done_label
);
828 expand_ubsan_result_store (target
, res
);
832 res
= expand_binop (mode
, add_optab
, res
, sgn
, NULL_RTX
, false,
835 expand_arith_overflow_result_store (lhs
, target
, mode
, res
);
840 /* Add negate overflow checking to the statement STMT. */
843 expand_neg_overflow (location_t loc
, tree lhs
, tree arg1
, bool is_ubsan
)
847 rtx_code_label
*done_label
, *do_error
;
848 rtx target
= NULL_RTX
;
850 done_label
= gen_label_rtx ();
851 do_error
= gen_label_rtx ();
853 do_pending_stack_adjust ();
854 op1
= expand_normal (arg1
);
856 machine_mode mode
= TYPE_MODE (TREE_TYPE (arg1
));
859 target
= expand_expr (lhs
, NULL_RTX
, VOIDmode
, EXPAND_WRITE
);
861 write_complex_part (target
, const0_rtx
, true);
864 enum insn_code icode
= optab_handler (negv3_optab
, mode
);
865 if (icode
!= CODE_FOR_nothing
)
867 struct expand_operand ops
[3];
868 rtx_insn
*last
= get_last_insn ();
870 res
= gen_reg_rtx (mode
);
871 create_output_operand (&ops
[0], res
, mode
);
872 create_input_operand (&ops
[1], op1
, mode
);
873 create_fixed_operand (&ops
[2], do_error
);
874 if (maybe_expand_insn (icode
, 3, ops
))
876 last
= get_last_insn ();
877 if (profile_status_for_fn (cfun
) != PROFILE_ABSENT
879 && any_condjump_p (last
)
880 && !find_reg_note (last
, REG_BR_PROB
, 0))
881 add_int_reg_note (last
, REG_BR_PROB
, PROB_VERY_UNLIKELY
);
882 emit_jump (done_label
);
886 delete_insns_since (last
);
887 icode
= CODE_FOR_nothing
;
891 if (icode
== CODE_FOR_nothing
)
893 /* Compute the operation. On RTL level, the addition is always
895 res
= expand_unop (mode
, neg_optab
, op1
, NULL_RTX
, false);
897 /* Compare the operand with the most negative value. */
898 rtx minv
= expand_normal (TYPE_MIN_VALUE (TREE_TYPE (arg1
)));
899 do_compare_rtx_and_jump (op1
, minv
, NE
, true, mode
, NULL_RTX
, NULL
,
900 done_label
, PROB_VERY_LIKELY
);
903 emit_label (do_error
);
906 /* Expand the ubsan builtin call. */
908 fn
= ubsan_build_overflow_builtin (NEGATE_EXPR
, loc
, TREE_TYPE (arg1
),
912 do_pending_stack_adjust ();
915 write_complex_part (target
, const1_rtx
, true);
918 emit_label (done_label
);
923 expand_ubsan_result_store (target
, res
);
925 expand_arith_overflow_result_store (lhs
, target
, mode
, res
);
929 /* Add mul overflow checking to the statement STMT. */
932 expand_mul_overflow (location_t loc
, tree lhs
, tree arg0
, tree arg1
,
933 bool unsr_p
, bool uns0_p
, bool uns1_p
, bool is_ubsan
)
937 rtx_code_label
*done_label
, *do_error
;
938 rtx target
= NULL_RTX
;
940 enum insn_code icode
;
942 done_label
= gen_label_rtx ();
943 do_error
= gen_label_rtx ();
945 do_pending_stack_adjust ();
946 op0
= expand_normal (arg0
);
947 op1
= expand_normal (arg1
);
949 machine_mode mode
= TYPE_MODE (TREE_TYPE (arg0
));
953 target
= expand_expr (lhs
, NULL_RTX
, VOIDmode
, EXPAND_WRITE
);
955 write_complex_part (target
, const0_rtx
, true);
959 gcc_assert (!unsr_p
&& !uns0_p
&& !uns1_p
);
961 /* We assume both operands and result have the same precision
962 here (GET_MODE_BITSIZE (mode)), S stands for signed type
963 with that precision, U for unsigned type with that precision,
964 sgn for unsigned most significant bit in that precision.
965 s1 is signed first operand, u1 is unsigned first operand,
966 s2 is signed second operand, u2 is unsigned second operand,
967 sr is signed result, ur is unsigned result and the following
968 rules say how to compute result (which is always result of
969 the operands as if both were unsigned, cast to the right
970 signedness) and how to compute whether operation overflowed.
971 main_ovf (false) stands for jump on signed multiplication
972 overflow or the main algorithm with uns == false.
973 main_ovf (true) stands for jump on unsigned multiplication
974 overflow or the main algorithm with uns == true.
977 res = (S) ((U) s1 * (U) s2)
978 ovf = main_ovf (false)
981 ovf = main_ovf (true)
984 ovf = (s1 < 0 && u2) || main_ovf (true)
987 ovf = res < 0 || main_ovf (true)
989 res = (S) ((U) s1 * u2)
990 ovf = (S) u2 >= 0 ? main_ovf (false)
991 : (s1 != 0 && (s1 != -1 || u2 != (U) res))
993 t1 = (s1 & s2) < 0 ? (-(U) s1) : ((U) s1)
994 t2 = (s1 & s2) < 0 ? (-(U) s2) : ((U) s2)
996 ovf = (s1 ^ s2) < 0 ? (s1 && s2) : main_ovf (true) */
998 if (uns0_p
&& !uns1_p
)
1000 /* Multiplication is commutative, if operand signedness differs,
1001 canonicalize to the first operand being signed and second
1002 unsigned to simplify following code. */
1003 std::swap (op0
, op1
);
1004 std::swap (arg0
, arg1
);
1009 int pos_neg0
= get_range_pos_neg (arg0
);
1010 int pos_neg1
= get_range_pos_neg (arg1
);
1013 if (!uns0_p
&& uns1_p
&& unsr_p
)
1018 /* If s1 is non-negative, just perform normal u1 * u2 -> ur. */
1021 /* If s1 is negative, avoid the main code, just multiply and
1022 signal overflow if op1 is not 0. */
1023 struct separate_ops ops
;
1024 ops
.code
= MULT_EXPR
;
1025 ops
.type
= TREE_TYPE (arg1
);
1026 ops
.op0
= make_tree (ops
.type
, op0
);
1027 ops
.op1
= make_tree (ops
.type
, op1
);
1028 ops
.op2
= NULL_TREE
;
1030 res
= expand_expr_real_2 (&ops
, NULL_RTX
, mode
, EXPAND_NORMAL
);
1031 do_compare_rtx_and_jump (op1
, const0_rtx
, EQ
, true, mode
, NULL_RTX
,
1032 NULL
, done_label
, PROB_VERY_LIKELY
);
1033 goto do_error_label
;
1035 rtx_code_label
*do_main_label
;
1036 do_main_label
= gen_label_rtx ();
1037 do_compare_rtx_and_jump (op0
, const0_rtx
, GE
, false, mode
, NULL_RTX
,
1038 NULL
, do_main_label
, PROB_VERY_LIKELY
);
1039 do_compare_rtx_and_jump (op1
, const0_rtx
, EQ
, true, mode
, NULL_RTX
,
1040 NULL
, do_main_label
, PROB_VERY_LIKELY
);
1041 write_complex_part (target
, const1_rtx
, true);
1042 emit_label (do_main_label
);
1050 if (uns0_p
&& uns1_p
&& !unsr_p
)
1053 /* Rest of handling of this case after res is computed. */
1058 if (!uns0_p
&& uns1_p
&& !unsr_p
)
1065 /* If (S) u2 is negative (i.e. u2 is larger than maximum of S,
1066 avoid the main code, just multiply and signal overflow
1067 unless 0 * u2 or -1 * ((U) Smin). */
1068 struct separate_ops ops
;
1069 ops
.code
= MULT_EXPR
;
1070 ops
.type
= TREE_TYPE (arg1
);
1071 ops
.op0
= make_tree (ops
.type
, op0
);
1072 ops
.op1
= make_tree (ops
.type
, op1
);
1073 ops
.op2
= NULL_TREE
;
1075 res
= expand_expr_real_2 (&ops
, NULL_RTX
, mode
, EXPAND_NORMAL
);
1076 do_compare_rtx_and_jump (op0
, const0_rtx
, EQ
, true, mode
, NULL_RTX
,
1077 NULL
, done_label
, PROB_VERY_LIKELY
);
1078 do_compare_rtx_and_jump (op0
, constm1_rtx
, NE
, true, mode
, NULL_RTX
,
1079 NULL
, do_error
, PROB_VERY_UNLIKELY
);
1081 prec
= GET_MODE_PRECISION (mode
);
1083 sgn
= immed_wide_int_const (wi::min_value (prec
, SIGNED
), mode
);
1084 do_compare_rtx_and_jump (op1
, sgn
, EQ
, true, mode
, NULL_RTX
,
1085 NULL
, done_label
, PROB_VERY_LIKELY
);
1086 goto do_error_label
;
1088 /* Rest of handling of this case after res is computed. */
1096 if (!uns0_p
&& !uns1_p
&& unsr_p
)
1099 switch (pos_neg0
| pos_neg1
)
1101 case 1: /* Both operands known to be non-negative. */
1103 case 2: /* Both operands known to be negative. */
1104 op0
= expand_unop (mode
, neg_optab
, op0
, NULL_RTX
, false);
1105 op1
= expand_unop (mode
, neg_optab
, op1
, NULL_RTX
, false);
1106 /* Avoid looking at arg0/arg1 ranges, as we've changed
1108 arg0
= error_mark_node
;
1109 arg1
= error_mark_node
;
1112 if ((pos_neg0
^ pos_neg1
) == 3)
1114 /* If one operand is known to be negative and the other
1115 non-negative, this overflows always, unless the non-negative
1116 one is 0. Just do normal multiply and set overflow
1117 unless one of the operands is 0. */
1118 struct separate_ops ops
;
1119 ops
.code
= MULT_EXPR
;
1121 = build_nonstandard_integer_type (GET_MODE_PRECISION (mode
),
1123 ops
.op0
= make_tree (ops
.type
, op0
);
1124 ops
.op1
= make_tree (ops
.type
, op1
);
1125 ops
.op2
= NULL_TREE
;
1127 res
= expand_expr_real_2 (&ops
, NULL_RTX
, mode
, EXPAND_NORMAL
);
1128 tem
= expand_binop (mode
, and_optab
, op0
, op1
, NULL_RTX
, false,
1130 do_compare_rtx_and_jump (tem
, const0_rtx
, EQ
, true, mode
,
1131 NULL_RTX
, NULL
, done_label
,
1133 goto do_error_label
;
1135 /* The general case, do all the needed comparisons at runtime. */
1136 rtx_code_label
*do_main_label
, *after_negate_label
;
1138 rop0
= gen_reg_rtx (mode
);
1139 rop1
= gen_reg_rtx (mode
);
1140 emit_move_insn (rop0
, op0
);
1141 emit_move_insn (rop1
, op1
);
1144 do_main_label
= gen_label_rtx ();
1145 after_negate_label
= gen_label_rtx ();
1146 tem
= expand_binop (mode
, and_optab
, op0
, op1
, NULL_RTX
, false,
1148 do_compare_rtx_and_jump (tem
, const0_rtx
, GE
, false, mode
, NULL_RTX
,
1149 NULL
, after_negate_label
, PROB_VERY_LIKELY
);
1150 /* Both arguments negative here, negate them and continue with
1151 normal unsigned overflow checking multiplication. */
1152 emit_move_insn (op0
, expand_unop (mode
, neg_optab
, op0
,
1154 emit_move_insn (op1
, expand_unop (mode
, neg_optab
, op1
,
1156 /* Avoid looking at arg0/arg1 ranges, as we might have changed
1158 arg0
= error_mark_node
;
1159 arg1
= error_mark_node
;
1160 emit_jump (do_main_label
);
1161 emit_label (after_negate_label
);
1162 tem2
= expand_binop (mode
, xor_optab
, op0
, op1
, NULL_RTX
, false,
1164 do_compare_rtx_and_jump (tem2
, const0_rtx
, GE
, false, mode
, NULL_RTX
,
1165 NULL
, do_main_label
, PROB_VERY_LIKELY
);
1166 /* One argument is negative here, the other positive. This
1167 overflows always, unless one of the arguments is 0. But
1168 if e.g. s2 is 0, (U) s1 * 0 doesn't overflow, whatever s1
1169 is, thus we can keep do_main code oring in overflow as is. */
1170 do_compare_rtx_and_jump (tem
, const0_rtx
, EQ
, true, mode
, NULL_RTX
,
1171 NULL
, do_main_label
, PROB_VERY_LIKELY
);
1172 write_complex_part (target
, const1_rtx
, true);
1173 emit_label (do_main_label
);
1181 type
= build_nonstandard_integer_type (GET_MODE_PRECISION (mode
), uns
);
1182 sign
= uns
? UNSIGNED
: SIGNED
;
1183 icode
= optab_handler (uns
? umulv4_optab
: mulv4_optab
, mode
);
1184 if (icode
!= CODE_FOR_nothing
)
1186 struct expand_operand ops
[4];
1187 rtx_insn
*last
= get_last_insn ();
1189 res
= gen_reg_rtx (mode
);
1190 create_output_operand (&ops
[0], res
, mode
);
1191 create_input_operand (&ops
[1], op0
, mode
);
1192 create_input_operand (&ops
[2], op1
, mode
);
1193 create_fixed_operand (&ops
[3], do_error
);
1194 if (maybe_expand_insn (icode
, 4, ops
))
1196 last
= get_last_insn ();
1197 if (profile_status_for_fn (cfun
) != PROFILE_ABSENT
1199 && any_condjump_p (last
)
1200 && !find_reg_note (last
, REG_BR_PROB
, 0))
1201 add_int_reg_note (last
, REG_BR_PROB
, PROB_VERY_UNLIKELY
);
1202 emit_jump (done_label
);
1206 delete_insns_since (last
);
1207 icode
= CODE_FOR_nothing
;
1211 if (icode
== CODE_FOR_nothing
)
1213 struct separate_ops ops
;
1214 int prec
= GET_MODE_PRECISION (mode
);
1215 machine_mode hmode
= mode_for_size (prec
/ 2, MODE_INT
, 1);
1216 ops
.op0
= make_tree (type
, op0
);
1217 ops
.op1
= make_tree (type
, op1
);
1218 ops
.op2
= NULL_TREE
;
1220 if (GET_MODE_2XWIDER_MODE (mode
) != VOIDmode
1221 && targetm
.scalar_mode_supported_p (GET_MODE_2XWIDER_MODE (mode
)))
1223 machine_mode wmode
= GET_MODE_2XWIDER_MODE (mode
);
1224 ops
.code
= WIDEN_MULT_EXPR
;
1226 = build_nonstandard_integer_type (GET_MODE_PRECISION (wmode
), uns
);
1228 res
= expand_expr_real_2 (&ops
, NULL_RTX
, wmode
, EXPAND_NORMAL
);
1229 rtx hipart
= expand_shift (RSHIFT_EXPR
, wmode
, res
, prec
,
1231 hipart
= gen_lowpart (mode
, hipart
);
1232 res
= gen_lowpart (mode
, res
);
1234 /* For the unsigned multiplication, there was overflow if
1235 HIPART is non-zero. */
1236 do_compare_rtx_and_jump (hipart
, const0_rtx
, EQ
, true, mode
,
1237 NULL_RTX
, NULL
, done_label
,
1241 rtx signbit
= expand_shift (RSHIFT_EXPR
, mode
, res
, prec
- 1,
1243 /* RES is low half of the double width result, HIPART
1244 the high half. There was overflow if
1245 HIPART is different from RES < 0 ? -1 : 0. */
1246 do_compare_rtx_and_jump (signbit
, hipart
, EQ
, true, mode
,
1247 NULL_RTX
, NULL
, done_label
,
1251 else if (hmode
!= BLKmode
&& 2 * GET_MODE_PRECISION (hmode
) == prec
)
1253 rtx_code_label
*large_op0
= gen_label_rtx ();
1254 rtx_code_label
*small_op0_large_op1
= gen_label_rtx ();
1255 rtx_code_label
*one_small_one_large
= gen_label_rtx ();
1256 rtx_code_label
*both_ops_large
= gen_label_rtx ();
1257 rtx_code_label
*after_hipart_neg
= uns
? NULL
: gen_label_rtx ();
1258 rtx_code_label
*after_lopart_neg
= uns
? NULL
: gen_label_rtx ();
1259 rtx_code_label
*do_overflow
= gen_label_rtx ();
1260 rtx_code_label
*hipart_different
= uns
? NULL
: gen_label_rtx ();
1262 unsigned int hprec
= GET_MODE_PRECISION (hmode
);
1263 rtx hipart0
= expand_shift (RSHIFT_EXPR
, mode
, op0
, hprec
,
1265 hipart0
= gen_lowpart (hmode
, hipart0
);
1266 rtx lopart0
= gen_lowpart (hmode
, op0
);
1267 rtx signbit0
= const0_rtx
;
1269 signbit0
= expand_shift (RSHIFT_EXPR
, hmode
, lopart0
, hprec
- 1,
1271 rtx hipart1
= expand_shift (RSHIFT_EXPR
, mode
, op1
, hprec
,
1273 hipart1
= gen_lowpart (hmode
, hipart1
);
1274 rtx lopart1
= gen_lowpart (hmode
, op1
);
1275 rtx signbit1
= const0_rtx
;
1277 signbit1
= expand_shift (RSHIFT_EXPR
, hmode
, lopart1
, hprec
- 1,
1280 res
= gen_reg_rtx (mode
);
1282 /* True if op0 resp. op1 are known to be in the range of
1284 bool op0_small_p
= false;
1285 bool op1_small_p
= false;
1286 /* True if op0 resp. op1 are known to have all zeros or all ones
1287 in the upper half of bits, but are not known to be
1289 bool op0_medium_p
= false;
1290 bool op1_medium_p
= false;
1291 /* -1 if op{0,1} is known to be negative, 0 if it is known to be
1292 nonnegative, 1 if unknown. */
1298 else if (pos_neg0
== 2)
1302 else if (pos_neg1
== 2)
1305 unsigned int mprec0
= prec
;
1306 if (arg0
!= error_mark_node
)
1307 mprec0
= get_min_precision (arg0
, sign
);
1308 if (mprec0
<= hprec
)
1310 else if (!uns
&& mprec0
<= hprec
+ 1)
1311 op0_medium_p
= true;
1312 unsigned int mprec1
= prec
;
1313 if (arg1
!= error_mark_node
)
1314 mprec1
= get_min_precision (arg1
, sign
);
1315 if (mprec1
<= hprec
)
1317 else if (!uns
&& mprec1
<= hprec
+ 1)
1318 op1_medium_p
= true;
1320 int smaller_sign
= 1;
1321 int larger_sign
= 1;
1324 smaller_sign
= op0_sign
;
1325 larger_sign
= op1_sign
;
1327 else if (op1_small_p
)
1329 smaller_sign
= op1_sign
;
1330 larger_sign
= op0_sign
;
1332 else if (op0_sign
== op1_sign
)
1334 smaller_sign
= op0_sign
;
1335 larger_sign
= op0_sign
;
1339 do_compare_rtx_and_jump (signbit0
, hipart0
, NE
, true, hmode
,
1340 NULL_RTX
, NULL
, large_op0
,
1344 do_compare_rtx_and_jump (signbit1
, hipart1
, NE
, true, hmode
,
1345 NULL_RTX
, NULL
, small_op0_large_op1
,
1348 /* If both op0 and op1 are sign (!uns) or zero (uns) extended from
1349 hmode to mode, the multiplication will never overflow. We can
1350 do just one hmode x hmode => mode widening multiplication. */
1351 rtx lopart0s
= lopart0
, lopart1s
= lopart1
;
1352 if (GET_CODE (lopart0
) == SUBREG
)
1354 lopart0s
= shallow_copy_rtx (lopart0
);
1355 SUBREG_PROMOTED_VAR_P (lopart0s
) = 1;
1356 SUBREG_PROMOTED_SET (lopart0s
, uns
? SRP_UNSIGNED
: SRP_SIGNED
);
1358 if (GET_CODE (lopart1
) == SUBREG
)
1360 lopart1s
= shallow_copy_rtx (lopart1
);
1361 SUBREG_PROMOTED_VAR_P (lopart1s
) = 1;
1362 SUBREG_PROMOTED_SET (lopart1s
, uns
? SRP_UNSIGNED
: SRP_SIGNED
);
1364 tree halfstype
= build_nonstandard_integer_type (hprec
, uns
);
1365 ops
.op0
= make_tree (halfstype
, lopart0s
);
1366 ops
.op1
= make_tree (halfstype
, lopart1s
);
1367 ops
.code
= WIDEN_MULT_EXPR
;
1370 = expand_expr_real_2 (&ops
, NULL_RTX
, mode
, EXPAND_NORMAL
);
1371 emit_move_insn (res
, thisres
);
1372 emit_jump (done_label
);
1374 emit_label (small_op0_large_op1
);
1376 /* If op0 is sign (!uns) or zero (uns) extended from hmode to mode,
1377 but op1 is not, just swap the arguments and handle it as op1
1378 sign/zero extended, op0 not. */
1379 rtx larger
= gen_reg_rtx (mode
);
1380 rtx hipart
= gen_reg_rtx (hmode
);
1381 rtx lopart
= gen_reg_rtx (hmode
);
1382 emit_move_insn (larger
, op1
);
1383 emit_move_insn (hipart
, hipart1
);
1384 emit_move_insn (lopart
, lopart0
);
1385 emit_jump (one_small_one_large
);
1387 emit_label (large_op0
);
1390 do_compare_rtx_and_jump (signbit1
, hipart1
, NE
, true, hmode
,
1391 NULL_RTX
, NULL
, both_ops_large
,
1394 /* If op1 is sign (!uns) or zero (uns) extended from hmode to mode,
1395 but op0 is not, prepare larger, hipart and lopart pseudos and
1396 handle it together with small_op0_large_op1. */
1397 emit_move_insn (larger
, op0
);
1398 emit_move_insn (hipart
, hipart0
);
1399 emit_move_insn (lopart
, lopart1
);
1401 emit_label (one_small_one_large
);
1403 /* lopart is the low part of the operand that is sign extended
1404 to mode, larger is the the other operand, hipart is the
1405 high part of larger and lopart0 and lopart1 are the low parts
1407 We perform lopart0 * lopart1 and lopart * hipart widening
1409 tree halfutype
= build_nonstandard_integer_type (hprec
, 1);
1410 ops
.op0
= make_tree (halfutype
, lopart0
);
1411 ops
.op1
= make_tree (halfutype
, lopart1
);
1413 = expand_expr_real_2 (&ops
, NULL_RTX
, mode
, EXPAND_NORMAL
);
1415 ops
.op0
= make_tree (halfutype
, lopart
);
1416 ops
.op1
= make_tree (halfutype
, hipart
);
1417 rtx loxhi
= gen_reg_rtx (mode
);
1418 rtx tem
= expand_expr_real_2 (&ops
, NULL_RTX
, mode
, EXPAND_NORMAL
);
1419 emit_move_insn (loxhi
, tem
);
1423 /* if (hipart < 0) loxhi -= lopart << (bitsize / 2); */
1424 if (larger_sign
== 0)
1425 emit_jump (after_hipart_neg
);
1426 else if (larger_sign
!= -1)
1427 do_compare_rtx_and_jump (hipart
, const0_rtx
, GE
, false, hmode
,
1428 NULL_RTX
, NULL
, after_hipart_neg
,
1431 tem
= convert_modes (mode
, hmode
, lopart
, 1);
1432 tem
= expand_shift (LSHIFT_EXPR
, mode
, tem
, hprec
, NULL_RTX
, 1);
1433 tem
= expand_simple_binop (mode
, MINUS
, loxhi
, tem
, NULL_RTX
,
1435 emit_move_insn (loxhi
, tem
);
1437 emit_label (after_hipart_neg
);
1439 /* if (lopart < 0) loxhi -= larger; */
1440 if (smaller_sign
== 0)
1441 emit_jump (after_lopart_neg
);
1442 else if (smaller_sign
!= -1)
1443 do_compare_rtx_and_jump (lopart
, const0_rtx
, GE
, false, hmode
,
1444 NULL_RTX
, NULL
, after_lopart_neg
,
1447 tem
= expand_simple_binop (mode
, MINUS
, loxhi
, larger
, NULL_RTX
,
1449 emit_move_insn (loxhi
, tem
);
1451 emit_label (after_lopart_neg
);
1454 /* loxhi += (uns) lo0xlo1 >> (bitsize / 2); */
1455 tem
= expand_shift (RSHIFT_EXPR
, mode
, lo0xlo1
, hprec
, NULL_RTX
, 1);
1456 tem
= expand_simple_binop (mode
, PLUS
, loxhi
, tem
, NULL_RTX
,
1458 emit_move_insn (loxhi
, tem
);
1460 /* if (loxhi >> (bitsize / 2)
1461 == (hmode) loxhi >> (bitsize / 2 - 1)) (if !uns)
1462 if (loxhi >> (bitsize / 2) == 0 (if uns). */
1463 rtx hipartloxhi
= expand_shift (RSHIFT_EXPR
, mode
, loxhi
, hprec
,
1465 hipartloxhi
= gen_lowpart (hmode
, hipartloxhi
);
1466 rtx signbitloxhi
= const0_rtx
;
1468 signbitloxhi
= expand_shift (RSHIFT_EXPR
, hmode
,
1469 gen_lowpart (hmode
, loxhi
),
1470 hprec
- 1, NULL_RTX
, 0);
1472 do_compare_rtx_and_jump (signbitloxhi
, hipartloxhi
, NE
, true, hmode
,
1473 NULL_RTX
, NULL
, do_overflow
,
1474 PROB_VERY_UNLIKELY
);
1476 /* res = (loxhi << (bitsize / 2)) | (hmode) lo0xlo1; */
1477 rtx loxhishifted
= expand_shift (LSHIFT_EXPR
, mode
, loxhi
, hprec
,
1479 tem
= convert_modes (mode
, hmode
, gen_lowpart (hmode
, lo0xlo1
), 1);
1481 tem
= expand_simple_binop (mode
, IOR
, loxhishifted
, tem
, res
,
1484 emit_move_insn (res
, tem
);
1485 emit_jump (done_label
);
1487 emit_label (both_ops_large
);
1489 /* If both operands are large (not sign (!uns) or zero (uns)
1490 extended from hmode), then perform the full multiplication
1491 which will be the result of the operation.
1492 The only cases which don't overflow are for signed multiplication
1493 some cases where both hipart0 and highpart1 are 0 or -1.
1494 For unsigned multiplication when high parts are both non-zero
1495 this overflows always. */
1496 ops
.code
= MULT_EXPR
;
1497 ops
.op0
= make_tree (type
, op0
);
1498 ops
.op1
= make_tree (type
, op1
);
1499 tem
= expand_expr_real_2 (&ops
, NULL_RTX
, mode
, EXPAND_NORMAL
);
1500 emit_move_insn (res
, tem
);
1506 tem
= expand_simple_binop (hmode
, PLUS
, hipart0
, const1_rtx
,
1507 NULL_RTX
, 1, OPTAB_DIRECT
);
1508 do_compare_rtx_and_jump (tem
, const1_rtx
, GTU
, true, hmode
,
1509 NULL_RTX
, NULL
, do_error
,
1510 PROB_VERY_UNLIKELY
);
1515 tem
= expand_simple_binop (hmode
, PLUS
, hipart1
, const1_rtx
,
1516 NULL_RTX
, 1, OPTAB_DIRECT
);
1517 do_compare_rtx_and_jump (tem
, const1_rtx
, GTU
, true, hmode
,
1518 NULL_RTX
, NULL
, do_error
,
1519 PROB_VERY_UNLIKELY
);
1522 /* At this point hipart{0,1} are both in [-1, 0]. If they are
1523 the same, overflow happened if res is negative, if they are
1524 different, overflow happened if res is positive. */
1525 if (op0_sign
!= 1 && op1_sign
!= 1 && op0_sign
!= op1_sign
)
1526 emit_jump (hipart_different
);
1527 else if (op0_sign
== 1 || op1_sign
== 1)
1528 do_compare_rtx_and_jump (hipart0
, hipart1
, NE
, true, hmode
,
1529 NULL_RTX
, NULL
, hipart_different
,
1532 do_compare_rtx_and_jump (res
, const0_rtx
, LT
, false, mode
,
1533 NULL_RTX
, NULL
, do_error
,
1534 PROB_VERY_UNLIKELY
);
1535 emit_jump (done_label
);
1537 emit_label (hipart_different
);
1539 do_compare_rtx_and_jump (res
, const0_rtx
, GE
, false, mode
,
1540 NULL_RTX
, NULL
, do_error
,
1541 PROB_VERY_UNLIKELY
);
1542 emit_jump (done_label
);
1545 emit_label (do_overflow
);
1547 /* Overflow, do full multiplication and fallthru into do_error. */
1548 ops
.op0
= make_tree (type
, op0
);
1549 ops
.op1
= make_tree (type
, op1
);
1550 tem
= expand_expr_real_2 (&ops
, NULL_RTX
, mode
, EXPAND_NORMAL
);
1551 emit_move_insn (res
, tem
);
1555 gcc_assert (!is_ubsan
);
1556 ops
.code
= MULT_EXPR
;
1558 res
= expand_expr_real_2 (&ops
, NULL_RTX
, mode
, EXPAND_NORMAL
);
1559 emit_jump (done_label
);
1564 emit_label (do_error
);
1567 /* Expand the ubsan builtin call. */
1569 fn
= ubsan_build_overflow_builtin (MULT_EXPR
, loc
, TREE_TYPE (arg0
),
1573 do_pending_stack_adjust ();
1576 write_complex_part (target
, const1_rtx
, true);
1579 emit_label (done_label
);
1582 if (uns0_p
&& uns1_p
&& !unsr_p
)
1584 rtx_code_label
*all_done_label
= gen_label_rtx ();
1585 do_compare_rtx_and_jump (res
, const0_rtx
, GE
, false, mode
, NULL_RTX
,
1586 NULL
, all_done_label
, PROB_VERY_LIKELY
);
1587 write_complex_part (target
, const1_rtx
, true);
1588 emit_label (all_done_label
);
1592 if (!uns0_p
&& uns1_p
&& !unsr_p
&& pos_neg1
== 3)
1594 rtx_code_label
*all_done_label
= gen_label_rtx ();
1595 rtx_code_label
*set_noovf
= gen_label_rtx ();
1596 do_compare_rtx_and_jump (op1
, const0_rtx
, GE
, false, mode
, NULL_RTX
,
1597 NULL
, all_done_label
, PROB_VERY_LIKELY
);
1598 write_complex_part (target
, const1_rtx
, true);
1599 do_compare_rtx_and_jump (op0
, const0_rtx
, EQ
, true, mode
, NULL_RTX
,
1600 NULL
, set_noovf
, PROB_VERY_LIKELY
);
1601 do_compare_rtx_and_jump (op0
, constm1_rtx
, NE
, true, mode
, NULL_RTX
,
1602 NULL
, all_done_label
, PROB_VERY_UNLIKELY
);
1603 do_compare_rtx_and_jump (op1
, res
, NE
, true, mode
, NULL_RTX
, NULL
,
1604 all_done_label
, PROB_VERY_UNLIKELY
);
1605 emit_label (set_noovf
);
1606 write_complex_part (target
, const0_rtx
, true);
1607 emit_label (all_done_label
);
1613 expand_ubsan_result_store (target
, res
);
1615 expand_arith_overflow_result_store (lhs
, target
, mode
, res
);
1619 /* Expand UBSAN_CHECK_ADD call STMT. */
1622 expand_UBSAN_CHECK_ADD (gcall
*stmt
)
1624 location_t loc
= gimple_location (stmt
);
1625 tree lhs
= gimple_call_lhs (stmt
);
1626 tree arg0
= gimple_call_arg (stmt
, 0);
1627 tree arg1
= gimple_call_arg (stmt
, 1);
1628 expand_addsub_overflow (loc
, PLUS_EXPR
, lhs
, arg0
, arg1
,
1629 false, false, false, true);
1632 /* Expand UBSAN_CHECK_SUB call STMT. */
1635 expand_UBSAN_CHECK_SUB (gcall
*stmt
)
1637 location_t loc
= gimple_location (stmt
);
1638 tree lhs
= gimple_call_lhs (stmt
);
1639 tree arg0
= gimple_call_arg (stmt
, 0);
1640 tree arg1
= gimple_call_arg (stmt
, 1);
1641 if (integer_zerop (arg0
))
1642 expand_neg_overflow (loc
, lhs
, arg1
, true);
1644 expand_addsub_overflow (loc
, MINUS_EXPR
, lhs
, arg0
, arg1
,
1645 false, false, false, true);
1648 /* Expand UBSAN_CHECK_MUL call STMT. */
1651 expand_UBSAN_CHECK_MUL (gcall
*stmt
)
1653 location_t loc
= gimple_location (stmt
);
1654 tree lhs
= gimple_call_lhs (stmt
);
1655 tree arg0
= gimple_call_arg (stmt
, 0);
1656 tree arg1
= gimple_call_arg (stmt
, 1);
1657 expand_mul_overflow (loc
, lhs
, arg0
, arg1
, false, false, false, true);
1660 /* Helper function for {ADD,SUB,MUL}_OVERFLOW call stmt expansion. */
1663 expand_arith_overflow (enum tree_code code
, gimple stmt
)
1665 tree lhs
= gimple_call_lhs (stmt
);
1666 if (lhs
== NULL_TREE
)
1668 tree arg0
= gimple_call_arg (stmt
, 0);
1669 tree arg1
= gimple_call_arg (stmt
, 1);
1670 tree type
= TREE_TYPE (TREE_TYPE (lhs
));
1671 int uns0_p
= TYPE_UNSIGNED (TREE_TYPE (arg0
));
1672 int uns1_p
= TYPE_UNSIGNED (TREE_TYPE (arg1
));
1673 int unsr_p
= TYPE_UNSIGNED (type
);
1674 int prec0
= TYPE_PRECISION (TREE_TYPE (arg0
));
1675 int prec1
= TYPE_PRECISION (TREE_TYPE (arg1
));
1676 int precres
= TYPE_PRECISION (type
);
1677 location_t loc
= gimple_location (stmt
);
1678 if (!uns0_p
&& get_range_pos_neg (arg0
) == 1)
1680 if (!uns1_p
&& get_range_pos_neg (arg1
) == 1)
1682 int pr
= get_min_precision (arg0
, uns0_p
? UNSIGNED
: SIGNED
);
1683 prec0
= MIN (prec0
, pr
);
1684 pr
= get_min_precision (arg1
, uns1_p
? UNSIGNED
: SIGNED
);
1685 prec1
= MIN (prec1
, pr
);
1687 /* If uns0_p && uns1_p, precop is minimum needed precision
1688 of unsigned type to hold the exact result, otherwise
1689 precop is minimum needed precision of signed type to
1690 hold the exact result. */
1692 if (code
== MULT_EXPR
)
1693 precop
= prec0
+ prec1
+ (uns0_p
!= uns1_p
);
1696 if (uns0_p
== uns1_p
)
1697 precop
= MAX (prec0
, prec1
) + 1;
1699 precop
= MAX (prec0
+ 1, prec1
) + 1;
1701 precop
= MAX (prec0
, prec1
+ 1) + 1;
1703 int orig_precres
= precres
;
1707 if ((uns0_p
&& uns1_p
)
1708 ? ((precop
+ !unsr_p
) <= precres
1709 /* u1 - u2 -> ur can overflow, no matter what precision
1711 && (code
!= MINUS_EXPR
|| !unsr_p
))
1712 : (!unsr_p
&& precop
<= precres
))
1714 /* The infinity precision result will always fit into result. */
1715 rtx target
= expand_expr (lhs
, NULL_RTX
, VOIDmode
, EXPAND_WRITE
);
1716 write_complex_part (target
, const0_rtx
, true);
1717 enum machine_mode mode
= TYPE_MODE (type
);
1718 struct separate_ops ops
;
1721 ops
.op0
= fold_convert_loc (loc
, type
, arg0
);
1722 ops
.op1
= fold_convert_loc (loc
, type
, arg1
);
1723 ops
.op2
= NULL_TREE
;
1725 rtx tem
= expand_expr_real_2 (&ops
, NULL_RTX
, mode
, EXPAND_NORMAL
);
1726 expand_arith_overflow_result_store (lhs
, target
, mode
, tem
);
1730 /* For sub-word operations, if target doesn't have them, start
1731 with precres widening right away, otherwise do it only
1732 if the most simple cases can't be used. */
1733 if (WORD_REGISTER_OPERATIONS
1734 && orig_precres
== precres
1735 && precres
< BITS_PER_WORD
)
1737 else if ((uns0_p
&& uns1_p
&& unsr_p
&& prec0
<= precres
1738 && prec1
<= precres
)
1739 || ((!uns0_p
|| !uns1_p
) && !unsr_p
1740 && prec0
+ uns0_p
<= precres
1741 && prec1
+ uns1_p
<= precres
))
1743 arg0
= fold_convert_loc (loc
, type
, arg0
);
1744 arg1
= fold_convert_loc (loc
, type
, arg1
);
1748 if (integer_zerop (arg0
) && !unsr_p
)
1749 expand_neg_overflow (loc
, lhs
, arg1
, false);
1752 expand_addsub_overflow (loc
, code
, lhs
, arg0
, arg1
,
1753 unsr_p
, unsr_p
, unsr_p
, false);
1756 expand_mul_overflow (loc
, lhs
, arg0
, arg1
,
1757 unsr_p
, unsr_p
, unsr_p
, false);
1764 /* For sub-word operations, retry with a wider type first. */
1765 if (orig_precres
== precres
&& precop
<= BITS_PER_WORD
)
1767 #if WORD_REGISTER_OPERATIONS
1768 int p
= BITS_PER_WORD
;
1772 enum machine_mode m
= smallest_mode_for_size (p
, MODE_INT
);
1773 tree optype
= build_nonstandard_integer_type (GET_MODE_PRECISION (m
),
1776 p
= TYPE_PRECISION (optype
);
1780 unsr_p
= TYPE_UNSIGNED (optype
);
1786 if (prec0
<= precres
&& prec1
<= precres
)
1791 types
[0] = build_nonstandard_integer_type (precres
, 0);
1797 types
[1] = build_nonstandard_integer_type (precres
, 1);
1799 arg0
= fold_convert_loc (loc
, types
[uns0_p
], arg0
);
1800 arg1
= fold_convert_loc (loc
, types
[uns1_p
], arg1
);
1801 if (code
!= MULT_EXPR
)
1802 expand_addsub_overflow (loc
, code
, lhs
, arg0
, arg1
, unsr_p
,
1803 uns0_p
, uns1_p
, false);
1805 expand_mul_overflow (loc
, lhs
, arg0
, arg1
, unsr_p
,
1806 uns0_p
, uns1_p
, false);
1810 /* Retry with a wider type. */
1811 if (orig_precres
== precres
)
1813 int p
= MAX (prec0
, prec1
);
1814 enum machine_mode m
= smallest_mode_for_size (p
, MODE_INT
);
1815 tree optype
= build_nonstandard_integer_type (GET_MODE_PRECISION (m
),
1818 p
= TYPE_PRECISION (optype
);
1822 unsr_p
= TYPE_UNSIGNED (optype
);
1833 /* Expand ADD_OVERFLOW STMT. */
1836 expand_ADD_OVERFLOW (gcall
*stmt
)
1838 expand_arith_overflow (PLUS_EXPR
, stmt
);
1841 /* Expand SUB_OVERFLOW STMT. */
1844 expand_SUB_OVERFLOW (gcall
*stmt
)
1846 expand_arith_overflow (MINUS_EXPR
, stmt
);
1849 /* Expand MUL_OVERFLOW STMT. */
1852 expand_MUL_OVERFLOW (gcall
*stmt
)
1854 expand_arith_overflow (MULT_EXPR
, stmt
);
1857 /* This should get folded in tree-vectorizer.c. */
1860 expand_LOOP_VECTORIZED (gcall
*)
1866 expand_MASK_LOAD (gcall
*stmt
)
1868 struct expand_operand ops
[3];
1869 tree type
, lhs
, rhs
, maskt
;
1870 rtx mem
, target
, mask
;
1872 maskt
= gimple_call_arg (stmt
, 2);
1873 lhs
= gimple_call_lhs (stmt
);
1874 if (lhs
== NULL_TREE
)
1876 type
= TREE_TYPE (lhs
);
1877 rhs
= fold_build2 (MEM_REF
, type
, gimple_call_arg (stmt
, 0),
1878 gimple_call_arg (stmt
, 1));
1880 mem
= expand_expr (rhs
, NULL_RTX
, VOIDmode
, EXPAND_WRITE
);
1881 gcc_assert (MEM_P (mem
));
1882 mask
= expand_normal (maskt
);
1883 target
= expand_expr (lhs
, NULL_RTX
, VOIDmode
, EXPAND_WRITE
);
1884 create_output_operand (&ops
[0], target
, TYPE_MODE (type
));
1885 create_fixed_operand (&ops
[1], mem
);
1886 create_input_operand (&ops
[2], mask
, TYPE_MODE (TREE_TYPE (maskt
)));
1887 expand_insn (optab_handler (maskload_optab
, TYPE_MODE (type
)), 3, ops
);
1891 expand_MASK_STORE (gcall
*stmt
)
1893 struct expand_operand ops
[3];
1894 tree type
, lhs
, rhs
, maskt
;
1897 maskt
= gimple_call_arg (stmt
, 2);
1898 rhs
= gimple_call_arg (stmt
, 3);
1899 type
= TREE_TYPE (rhs
);
1900 lhs
= fold_build2 (MEM_REF
, type
, gimple_call_arg (stmt
, 0),
1901 gimple_call_arg (stmt
, 1));
1903 mem
= expand_expr (lhs
, NULL_RTX
, VOIDmode
, EXPAND_WRITE
);
1904 gcc_assert (MEM_P (mem
));
1905 mask
= expand_normal (maskt
);
1906 reg
= expand_normal (rhs
);
1907 create_fixed_operand (&ops
[0], mem
);
1908 create_input_operand (&ops
[1], reg
, TYPE_MODE (type
));
1909 create_input_operand (&ops
[2], mask
, TYPE_MODE (TREE_TYPE (maskt
)));
1910 expand_insn (optab_handler (maskstore_optab
, TYPE_MODE (type
)), 3, ops
);
1914 expand_ABNORMAL_DISPATCHER (gcall
*)
1919 expand_BUILTIN_EXPECT (gcall
*stmt
)
1921 /* When guessing was done, the hints should be already stripped away. */
1922 gcc_assert (!flag_guess_branch_prob
|| optimize
== 0 || seen_error ());
1925 tree lhs
= gimple_call_lhs (stmt
);
1927 target
= expand_expr (lhs
, NULL_RTX
, VOIDmode
, EXPAND_WRITE
);
1929 target
= const0_rtx
;
1930 rtx val
= expand_expr (gimple_call_arg (stmt
, 0), target
, VOIDmode
, EXPAND_NORMAL
);
1931 if (lhs
&& val
!= target
)
1932 emit_move_insn (target
, val
);
1935 /* IFN_VA_ARG is supposed to be expanded at pass_stdarg. So this dummy function
1936 should never be called. */
1939 expand_VA_ARG (gcall
*stmt ATTRIBUTE_UNUSED
)
1944 /* Routines to expand each internal function, indexed by function number.
1945 Each routine has the prototype:
1947 expand_<NAME> (gcall *stmt)
1949 where STMT is the statement that performs the call. */
1950 static void (*const internal_fn_expanders
[]) (gcall
*) = {
1951 #define DEF_INTERNAL_FN(CODE, FLAGS, FNSPEC) expand_##CODE,
1952 #include "internal-fn.def"
1953 #undef DEF_INTERNAL_FN
1957 /* Expand STMT, which is a call to internal function FN. */
1960 expand_internal_call (gcall
*stmt
)
1962 internal_fn_expanders
[(int) gimple_call_internal_fn (stmt
)] (stmt
);