]>
Commit | Line | Data |
---|---|---|
25583c4f | 1 | /* Internal functions. |
5624e564 | 2 | Copyright (C) 2011-2015 Free Software Foundation, Inc. |
25583c4f RS |
3 | |
4 | This file is part of GCC. | |
5 | ||
6 | GCC is free software; you can redistribute it and/or modify it under | |
7 | the terms of the GNU General Public License as published by the Free | |
8 | Software Foundation; either version 3, or (at your option) any later | |
9 | version. | |
10 | ||
11 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
12 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
13 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
14 | for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with GCC; see the file COPYING3. If not see | |
18 | <http://www.gnu.org/licenses/>. */ | |
19 | ||
20 | #include "config.h" | |
21 | #include "system.h" | |
22 | #include "coretypes.h" | |
40e23961 MC |
23 | #include "alias.h" |
24 | #include "symtab.h" | |
25 | #include "options.h" | |
25583c4f | 26 | #include "tree.h" |
40e23961 | 27 | #include "fold-const.h" |
0e37a2f3 | 28 | #include "internal-fn.h" |
d8a2d370 | 29 | #include "stor-layout.h" |
36566b39 PK |
30 | #include "tm.h" |
31 | #include "hard-reg-set.h" | |
32 | #include "function.h" | |
33 | #include "rtl.h" | |
34 | #include "flags.h" | |
36566b39 PK |
35 | #include "insn-config.h" |
36 | #include "expmed.h" | |
37 | #include "dojump.h" | |
38 | #include "explow.h" | |
39 | #include "calls.h" | |
40 | #include "emit-rtl.h" | |
41 | #include "varasm.h" | |
42 | #include "stmt.h" | |
25583c4f | 43 | #include "expr.h" |
b0710fe1 | 44 | #include "insn-codes.h" |
25583c4f | 45 | #include "optabs.h" |
60393bbc | 46 | #include "predict.h" |
60393bbc AM |
47 | #include "dominance.h" |
48 | #include "cfg.h" | |
2fb9a547 AM |
49 | #include "basic-block.h" |
50 | #include "tree-ssa-alias.h" | |
2fb9a547 | 51 | #include "gimple-expr.h" |
25583c4f | 52 | #include "gimple.h" |
31e071ae MP |
53 | #include "ubsan.h" |
54 | #include "target.h" | |
97286431 JJ |
55 | #include "stringpool.h" |
56 | #include "tree-ssanames.h" | |
ed9c79e1 | 57 | #include "diagnostic-core.h" |
25583c4f RS |
58 | |
59 | /* The names of each internal function, indexed by function number. */ | |
60 | const char *const internal_fn_name_array[] = { | |
b78475cf | 61 | #define DEF_INTERNAL_FN(CODE, FLAGS, FNSPEC) #CODE, |
25583c4f RS |
62 | #include "internal-fn.def" |
63 | #undef DEF_INTERNAL_FN | |
64 | "<invalid-fn>" | |
65 | }; | |
66 | ||
67 | /* The ECF_* flags of each internal function, indexed by function number. */ | |
68 | const int internal_fn_flags_array[] = { | |
b78475cf | 69 | #define DEF_INTERNAL_FN(CODE, FLAGS, FNSPEC) FLAGS, |
25583c4f RS |
70 | #include "internal-fn.def" |
71 | #undef DEF_INTERNAL_FN | |
72 | 0 | |
73 | }; | |
74 | ||
b78475cf YG |
75 | /* Fnspec of each internal function, indexed by function number. */ |
76 | const_tree internal_fn_fnspec_array[IFN_LAST + 1]; | |
77 | ||
78 | void | |
79 | init_internal_fns () | |
80 | { | |
81 | #define DEF_INTERNAL_FN(CODE, FLAGS, FNSPEC) \ | |
82 | if (FNSPEC) internal_fn_fnspec_array[IFN_##CODE] = \ | |
63a4184f | 83 | build_string ((int) sizeof (FNSPEC), FNSPEC ? FNSPEC : ""); |
b78475cf YG |
84 | #include "internal-fn.def" |
85 | #undef DEF_INTERNAL_FN | |
86 | internal_fn_fnspec_array[IFN_LAST] = 0; | |
87 | } | |
88 | ||
272c6793 RS |
89 | /* ARRAY_TYPE is an array of vector modes. Return the associated insn |
90 | for load-lanes-style optab OPTAB. The insn must exist. */ | |
91 | ||
92 | static enum insn_code | |
93 | get_multi_vector_move (tree array_type, convert_optab optab) | |
94 | { | |
95 | enum insn_code icode; | |
ef4bddc2 RS |
96 | machine_mode imode; |
97 | machine_mode vmode; | |
272c6793 RS |
98 | |
99 | gcc_assert (TREE_CODE (array_type) == ARRAY_TYPE); | |
100 | imode = TYPE_MODE (array_type); | |
101 | vmode = TYPE_MODE (TREE_TYPE (array_type)); | |
102 | ||
103 | icode = convert_optab_handler (optab, imode, vmode); | |
104 | gcc_assert (icode != CODE_FOR_nothing); | |
105 | return icode; | |
106 | } | |
107 | ||
108 | /* Expand LOAD_LANES call STMT. */ | |
109 | ||
110 | static void | |
538dd0b7 | 111 | expand_LOAD_LANES (gcall *stmt) |
272c6793 RS |
112 | { |
113 | struct expand_operand ops[2]; | |
114 | tree type, lhs, rhs; | |
115 | rtx target, mem; | |
116 | ||
117 | lhs = gimple_call_lhs (stmt); | |
118 | rhs = gimple_call_arg (stmt, 0); | |
119 | type = TREE_TYPE (lhs); | |
120 | ||
121 | target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE); | |
122 | mem = expand_normal (rhs); | |
123 | ||
124 | gcc_assert (MEM_P (mem)); | |
125 | PUT_MODE (mem, TYPE_MODE (type)); | |
126 | ||
127 | create_output_operand (&ops[0], target, TYPE_MODE (type)); | |
128 | create_fixed_operand (&ops[1], mem); | |
129 | expand_insn (get_multi_vector_move (type, vec_load_lanes_optab), 2, ops); | |
130 | } | |
131 | ||
132 | /* Expand STORE_LANES call STMT. */ | |
133 | ||
134 | static void | |
538dd0b7 | 135 | expand_STORE_LANES (gcall *stmt) |
272c6793 RS |
136 | { |
137 | struct expand_operand ops[2]; | |
138 | tree type, lhs, rhs; | |
139 | rtx target, reg; | |
140 | ||
141 | lhs = gimple_call_lhs (stmt); | |
142 | rhs = gimple_call_arg (stmt, 0); | |
143 | type = TREE_TYPE (rhs); | |
144 | ||
145 | target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE); | |
146 | reg = expand_normal (rhs); | |
147 | ||
148 | gcc_assert (MEM_P (target)); | |
149 | PUT_MODE (target, TYPE_MODE (type)); | |
150 | ||
151 | create_fixed_operand (&ops[0], target); | |
152 | create_input_operand (&ops[1], reg, TYPE_MODE (type)); | |
153 | expand_insn (get_multi_vector_move (type, vec_store_lanes_optab), 2, ops); | |
154 | } | |
155 | ||
8170608b | 156 | static void |
35228ac7 | 157 | expand_ANNOTATE (gcall *) |
8170608b TB |
158 | { |
159 | gcc_unreachable (); | |
160 | } | |
161 | ||
74bf76ed JJ |
162 | /* This should get expanded in adjust_simduid_builtins. */ |
163 | ||
164 | static void | |
35228ac7 | 165 | expand_GOMP_SIMD_LANE (gcall *) |
74bf76ed JJ |
166 | { |
167 | gcc_unreachable (); | |
168 | } | |
169 | ||
170 | /* This should get expanded in adjust_simduid_builtins. */ | |
171 | ||
172 | static void | |
35228ac7 | 173 | expand_GOMP_SIMD_VF (gcall *) |
74bf76ed JJ |
174 | { |
175 | gcc_unreachable (); | |
176 | } | |
177 | ||
178 | /* This should get expanded in adjust_simduid_builtins. */ | |
179 | ||
180 | static void | |
35228ac7 | 181 | expand_GOMP_SIMD_LAST_LANE (gcall *) |
74bf76ed JJ |
182 | { |
183 | gcc_unreachable (); | |
184 | } | |
185 | ||
b9a55b13 MP |
186 | /* This should get expanded in the sanopt pass. */ |
187 | ||
188 | static void | |
35228ac7 | 189 | expand_UBSAN_NULL (gcall *) |
b9a55b13 MP |
190 | { |
191 | gcc_unreachable (); | |
192 | } | |
193 | ||
0e37a2f3 MP |
194 | /* This should get expanded in the sanopt pass. */ |
195 | ||
196 | static void | |
35228ac7 | 197 | expand_UBSAN_BOUNDS (gcall *) |
0e82f089 MP |
198 | { |
199 | gcc_unreachable (); | |
200 | } | |
201 | ||
202 | /* This should get expanded in the sanopt pass. */ | |
203 | ||
204 | static void | |
35228ac7 | 205 | expand_UBSAN_VPTR (gcall *) |
0e37a2f3 MP |
206 | { |
207 | gcc_unreachable (); | |
208 | } | |
209 | ||
c62ccb9a YG |
210 | /* This should get expanded in the sanopt pass. */ |
211 | ||
212 | static void | |
35228ac7 JJ |
213 | expand_UBSAN_OBJECT_SIZE (gcall *) |
214 | { | |
215 | gcc_unreachable (); | |
216 | } | |
217 | ||
218 | /* This should get expanded in the sanopt pass. */ | |
219 | ||
220 | static void | |
221 | expand_ASAN_CHECK (gcall *) | |
c62ccb9a YG |
222 | { |
223 | gcc_unreachable (); | |
224 | } | |
225 | ||
fca4adf2 JJ |
226 | /* This should get expanded in the tsan pass. */ |
227 | ||
228 | static void | |
229 | expand_TSAN_FUNC_EXIT (gcall *) | |
230 | { | |
231 | gcc_unreachable (); | |
232 | } | |
233 | ||
1304953e JJ |
234 | /* Helper function for expand_addsub_overflow. Return 1 |
235 | if ARG interpreted as signed in its precision is known to be always | |
236 | positive or 2 if ARG is known to be always negative, or 3 if ARG may | |
237 | be positive or negative. */ | |
238 | ||
239 | static int | |
240 | get_range_pos_neg (tree arg) | |
241 | { | |
242 | if (arg == error_mark_node) | |
243 | return 3; | |
244 | ||
245 | int prec = TYPE_PRECISION (TREE_TYPE (arg)); | |
246 | int cnt = 0; | |
247 | if (TREE_CODE (arg) == INTEGER_CST) | |
248 | { | |
249 | wide_int w = wi::sext (arg, prec); | |
250 | if (wi::neg_p (w)) | |
251 | return 2; | |
252 | else | |
253 | return 1; | |
254 | } | |
255 | while (CONVERT_EXPR_P (arg) | |
256 | && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg, 0))) | |
257 | && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg, 0))) <= prec) | |
258 | { | |
259 | arg = TREE_OPERAND (arg, 0); | |
260 | /* Narrower value zero extended into wider type | |
261 | will always result in positive values. */ | |
262 | if (TYPE_UNSIGNED (TREE_TYPE (arg)) | |
263 | && TYPE_PRECISION (TREE_TYPE (arg)) < prec) | |
264 | return 1; | |
265 | prec = TYPE_PRECISION (TREE_TYPE (arg)); | |
266 | if (++cnt > 30) | |
267 | return 3; | |
268 | } | |
269 | ||
270 | if (TREE_CODE (arg) != SSA_NAME) | |
271 | return 3; | |
272 | wide_int arg_min, arg_max; | |
273 | while (get_range_info (arg, &arg_min, &arg_max) != VR_RANGE) | |
274 | { | |
275 | gimple g = SSA_NAME_DEF_STMT (arg); | |
276 | if (is_gimple_assign (g) | |
277 | && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (g))) | |
278 | { | |
279 | tree t = gimple_assign_rhs1 (g); | |
280 | if (INTEGRAL_TYPE_P (TREE_TYPE (t)) | |
281 | && TYPE_PRECISION (TREE_TYPE (t)) <= prec) | |
282 | { | |
283 | if (TYPE_UNSIGNED (TREE_TYPE (t)) | |
284 | && TYPE_PRECISION (TREE_TYPE (t)) < prec) | |
285 | return 1; | |
286 | prec = TYPE_PRECISION (TREE_TYPE (t)); | |
287 | arg = t; | |
288 | if (++cnt > 30) | |
289 | return 3; | |
290 | continue; | |
291 | } | |
292 | } | |
293 | return 3; | |
294 | } | |
295 | if (TYPE_UNSIGNED (TREE_TYPE (arg))) | |
296 | { | |
297 | /* For unsigned values, the "positive" range comes | |
298 | below the "negative" range. */ | |
299 | if (!wi::neg_p (wi::sext (arg_max, prec), SIGNED)) | |
300 | return 1; | |
301 | if (wi::neg_p (wi::sext (arg_min, prec), SIGNED)) | |
302 | return 2; | |
303 | } | |
304 | else | |
305 | { | |
306 | if (!wi::neg_p (wi::sext (arg_min, prec), SIGNED)) | |
307 | return 1; | |
308 | if (wi::neg_p (wi::sext (arg_max, prec), SIGNED)) | |
309 | return 2; | |
310 | } | |
311 | return 3; | |
312 | } | |
313 | ||
314 | /* Return minimum precision needed to represent all values | |
315 | of ARG in SIGNed integral type. */ | |
316 | ||
317 | static int | |
318 | get_min_precision (tree arg, signop sign) | |
319 | { | |
320 | int prec = TYPE_PRECISION (TREE_TYPE (arg)); | |
321 | int cnt = 0; | |
322 | signop orig_sign = sign; | |
323 | if (TREE_CODE (arg) == INTEGER_CST) | |
324 | { | |
325 | int p; | |
326 | if (TYPE_SIGN (TREE_TYPE (arg)) != sign) | |
327 | { | |
328 | widest_int w = wi::to_widest (arg); | |
329 | w = wi::ext (w, prec, sign); | |
330 | p = wi::min_precision (w, sign); | |
331 | } | |
332 | else | |
333 | p = wi::min_precision (arg, sign); | |
334 | return MIN (p, prec); | |
335 | } | |
336 | while (CONVERT_EXPR_P (arg) | |
337 | && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (arg, 0))) | |
338 | && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg, 0))) <= prec) | |
339 | { | |
340 | arg = TREE_OPERAND (arg, 0); | |
341 | if (TYPE_PRECISION (TREE_TYPE (arg)) < prec) | |
342 | { | |
343 | if (TYPE_UNSIGNED (TREE_TYPE (arg))) | |
344 | sign = UNSIGNED; | |
345 | else if (sign == UNSIGNED && get_range_pos_neg (arg) != 1) | |
346 | return prec + (orig_sign != sign); | |
347 | prec = TYPE_PRECISION (TREE_TYPE (arg)); | |
348 | } | |
349 | if (++cnt > 30) | |
350 | return prec + (orig_sign != sign); | |
351 | } | |
352 | if (TREE_CODE (arg) != SSA_NAME) | |
353 | return prec + (orig_sign != sign); | |
354 | wide_int arg_min, arg_max; | |
355 | while (get_range_info (arg, &arg_min, &arg_max) != VR_RANGE) | |
356 | { | |
357 | gimple g = SSA_NAME_DEF_STMT (arg); | |
358 | if (is_gimple_assign (g) | |
359 | && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (g))) | |
360 | { | |
361 | tree t = gimple_assign_rhs1 (g); | |
362 | if (INTEGRAL_TYPE_P (TREE_TYPE (t)) | |
363 | && TYPE_PRECISION (TREE_TYPE (t)) <= prec) | |
364 | { | |
365 | arg = t; | |
366 | if (TYPE_PRECISION (TREE_TYPE (arg)) < prec) | |
367 | { | |
368 | if (TYPE_UNSIGNED (TREE_TYPE (arg))) | |
369 | sign = UNSIGNED; | |
370 | else if (sign == UNSIGNED && get_range_pos_neg (arg) != 1) | |
371 | return prec + (orig_sign != sign); | |
372 | prec = TYPE_PRECISION (TREE_TYPE (arg)); | |
373 | } | |
374 | if (++cnt > 30) | |
375 | return prec + (orig_sign != sign); | |
376 | continue; | |
377 | } | |
378 | } | |
379 | return prec + (orig_sign != sign); | |
380 | } | |
381 | if (sign == TYPE_SIGN (TREE_TYPE (arg))) | |
382 | { | |
383 | int p1 = wi::min_precision (arg_min, sign); | |
384 | int p2 = wi::min_precision (arg_max, sign); | |
385 | p1 = MAX (p1, p2); | |
386 | prec = MIN (prec, p1); | |
387 | } | |
388 | else if (sign == UNSIGNED && !wi::neg_p (arg_min, SIGNED)) | |
389 | { | |
c1ee2e62 | 390 | int p = wi::min_precision (arg_max, UNSIGNED); |
1304953e JJ |
391 | prec = MIN (prec, p); |
392 | } | |
393 | return prec + (orig_sign != sign); | |
394 | } | |
395 | ||
396 | /* Helper for expand_*_overflow. Store RES into the __real__ part | |
397 | of TARGET. If RES has larger MODE than __real__ part of TARGET, | |
398 | set the __imag__ part to 1 if RES doesn't fit into it. */ | |
399 | ||
400 | static void | |
401 | expand_arith_overflow_result_store (tree lhs, rtx target, | |
402 | machine_mode mode, rtx res) | |
403 | { | |
404 | machine_mode tgtmode = GET_MODE_INNER (GET_MODE (target)); | |
405 | rtx lres = res; | |
406 | if (tgtmode != mode) | |
407 | { | |
408 | rtx_code_label *done_label = gen_label_rtx (); | |
409 | int uns = TYPE_UNSIGNED (TREE_TYPE (TREE_TYPE (lhs))); | |
410 | lres = convert_modes (tgtmode, mode, res, uns); | |
411 | gcc_assert (GET_MODE_PRECISION (tgtmode) < GET_MODE_PRECISION (mode)); | |
92344ed0 | 412 | do_compare_rtx_and_jump (res, convert_modes (mode, tgtmode, lres, uns), |
1476d1bd | 413 | EQ, true, mode, NULL_RTX, NULL, done_label, |
1304953e JJ |
414 | PROB_VERY_LIKELY); |
415 | write_complex_part (target, const1_rtx, true); | |
416 | emit_label (done_label); | |
417 | } | |
418 | write_complex_part (target, lres, false); | |
419 | } | |
420 | ||
5620052d JJ |
421 | /* Helper for expand_*_overflow. Store RES into TARGET. */ |
422 | ||
423 | static void | |
424 | expand_ubsan_result_store (rtx target, rtx res) | |
425 | { | |
426 | if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target)) | |
427 | /* If this is a scalar in a register that is stored in a wider mode | |
428 | than the declared mode, compute the result into its declared mode | |
429 | and then convert to the wider mode. Our value is the computed | |
430 | expression. */ | |
431 | convert_move (SUBREG_REG (target), res, SUBREG_PROMOTED_SIGN (target)); | |
432 | else | |
433 | emit_move_insn (target, res); | |
434 | } | |
435 | ||
31e071ae MP |
436 | /* Add sub/add overflow checking to the statement STMT. |
437 | CODE says whether the operation is +, or -. */ | |
438 | ||
1304953e JJ |
439 | static void |
440 | expand_addsub_overflow (location_t loc, tree_code code, tree lhs, | |
441 | tree arg0, tree arg1, bool unsr_p, bool uns0_p, | |
442 | bool uns1_p, bool is_ubsan) | |
31e071ae | 443 | { |
1304953e JJ |
444 | rtx res, target = NULL_RTX; |
445 | tree fn; | |
446 | rtx_code_label *done_label = gen_label_rtx (); | |
447 | rtx_code_label *do_error = gen_label_rtx (); | |
31e071ae | 448 | do_pending_stack_adjust (); |
1304953e JJ |
449 | rtx op0 = expand_normal (arg0); |
450 | rtx op1 = expand_normal (arg1); | |
ef4bddc2 | 451 | machine_mode mode = TYPE_MODE (TREE_TYPE (arg0)); |
1304953e JJ |
452 | int prec = GET_MODE_PRECISION (mode); |
453 | rtx sgn = immed_wide_int_const (wi::min_value (prec, SIGNED), mode); | |
454 | bool do_xor = false; | |
455 | ||
456 | if (is_ubsan) | |
457 | gcc_assert (!unsr_p && !uns0_p && !uns1_p); | |
458 | ||
31e071ae | 459 | if (lhs) |
1304953e JJ |
460 | { |
461 | target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE); | |
462 | if (!is_ubsan) | |
463 | write_complex_part (target, const0_rtx, true); | |
464 | } | |
465 | ||
466 | /* We assume both operands and result have the same precision | |
467 | here (GET_MODE_BITSIZE (mode)), S stands for signed type | |
468 | with that precision, U for unsigned type with that precision, | |
469 | sgn for unsigned most significant bit in that precision. | |
470 | s1 is signed first operand, u1 is unsigned first operand, | |
471 | s2 is signed second operand, u2 is unsigned second operand, | |
472 | sr is signed result, ur is unsigned result and the following | |
473 | rules say how to compute result (which is always result of | |
474 | the operands as if both were unsigned, cast to the right | |
475 | signedness) and how to compute whether operation overflowed. | |
476 | ||
477 | s1 + s2 -> sr | |
478 | res = (S) ((U) s1 + (U) s2) | |
479 | ovf = s2 < 0 ? res > s1 : res < s1 (or jump on overflow) | |
480 | s1 - s2 -> sr | |
481 | res = (S) ((U) s1 - (U) s2) | |
482 | ovf = s2 < 0 ? res < s1 : res > s2 (or jump on overflow) | |
483 | u1 + u2 -> ur | |
484 | res = u1 + u2 | |
485 | ovf = res < u1 (or jump on carry, but RTL opts will handle it) | |
486 | u1 - u2 -> ur | |
487 | res = u1 - u2 | |
488 | ovf = res > u1 (or jump on carry, but RTL opts will handle it) | |
489 | s1 + u2 -> sr | |
490 | res = (S) ((U) s1 + u2) | |
491 | ovf = ((U) res ^ sgn) < u2 | |
492 | s1 + u2 -> ur | |
493 | t1 = (S) (u2 ^ sgn) | |
494 | t2 = s1 + t1 | |
495 | res = (U) t2 ^ sgn | |
496 | ovf = t1 < 0 ? t2 > s1 : t2 < s1 (or jump on overflow) | |
497 | s1 - u2 -> sr | |
498 | res = (S) ((U) s1 - u2) | |
499 | ovf = u2 > ((U) s1 ^ sgn) | |
500 | s1 - u2 -> ur | |
501 | res = (U) s1 - u2 | |
502 | ovf = s1 < 0 || u2 > (U) s1 | |
503 | u1 - s2 -> sr | |
504 | res = u1 - (U) s2 | |
505 | ovf = u1 >= ((U) s2 ^ sgn) | |
506 | u1 - s2 -> ur | |
507 | t1 = u1 ^ sgn | |
508 | t2 = t1 - (U) s2 | |
509 | res = t2 ^ sgn | |
510 | ovf = s2 < 0 ? (S) t2 < (S) t1 : (S) t2 > (S) t1 (or jump on overflow) | |
511 | s1 + s2 -> ur | |
512 | res = (U) s1 + (U) s2 | |
513 | ovf = s2 < 0 ? (s1 | (S) res) < 0) : (s1 & (S) res) < 0) | |
514 | u1 + u2 -> sr | |
515 | res = (S) (u1 + u2) | |
516 | ovf = (U) res < u2 || res < 0 | |
517 | u1 - u2 -> sr | |
518 | res = (S) (u1 - u2) | |
519 | ovf = u1 >= u2 ? res < 0 : res >= 0 | |
520 | s1 - s2 -> ur | |
521 | res = (U) s1 - (U) s2 | |
522 | ovf = s2 >= 0 ? ((s1 | (S) res) < 0) : ((s1 & (S) res) < 0) */ | |
523 | ||
524 | if (code == PLUS_EXPR && uns0_p && !uns1_p) | |
525 | { | |
526 | /* PLUS_EXPR is commutative, if operand signedness differs, | |
527 | canonicalize to the first operand being signed and second | |
528 | unsigned to simplify following code. */ | |
6b4db501 MM |
529 | std::swap (op0, op1); |
530 | std::swap (arg0, arg1); | |
531 | uns0_p = false; | |
532 | uns1_p = true; | |
1304953e JJ |
533 | } |
534 | ||
535 | /* u1 +- u2 -> ur */ | |
536 | if (uns0_p && uns1_p && unsr_p) | |
537 | { | |
538 | /* Compute the operation. On RTL level, the addition is always | |
539 | unsigned. */ | |
540 | res = expand_binop (mode, code == PLUS_EXPR ? add_optab : sub_optab, | |
541 | op0, op1, NULL_RTX, false, OPTAB_LIB_WIDEN); | |
542 | rtx tem = op0; | |
543 | /* For PLUS_EXPR, the operation is commutative, so we can pick | |
544 | operand to compare against. For prec <= BITS_PER_WORD, I think | |
545 | preferring REG operand is better over CONST_INT, because | |
546 | the CONST_INT might enlarge the instruction or CSE would need | |
547 | to figure out we'd already loaded it into a register before. | |
548 | For prec > BITS_PER_WORD, I think CONST_INT might be more beneficial, | |
549 | as then the multi-word comparison can be perhaps simplified. */ | |
550 | if (code == PLUS_EXPR | |
551 | && (prec <= BITS_PER_WORD | |
552 | ? (CONST_SCALAR_INT_P (op0) && REG_P (op1)) | |
553 | : CONST_SCALAR_INT_P (op1))) | |
554 | tem = op1; | |
92344ed0 | 555 | do_compare_rtx_and_jump (res, tem, code == PLUS_EXPR ? GEU : LEU, |
1476d1bd | 556 | true, mode, NULL_RTX, NULL, done_label, |
1304953e JJ |
557 | PROB_VERY_LIKELY); |
558 | goto do_error_label; | |
559 | } | |
560 | ||
561 | /* s1 +- u2 -> sr */ | |
562 | if (!uns0_p && uns1_p && !unsr_p) | |
563 | { | |
564 | /* Compute the operation. On RTL level, the addition is always | |
565 | unsigned. */ | |
566 | res = expand_binop (mode, code == PLUS_EXPR ? add_optab : sub_optab, | |
567 | op0, op1, NULL_RTX, false, OPTAB_LIB_WIDEN); | |
568 | rtx tem = expand_binop (mode, add_optab, | |
569 | code == PLUS_EXPR ? res : op0, sgn, | |
570 | NULL_RTX, false, OPTAB_LIB_WIDEN); | |
1476d1bd | 571 | do_compare_rtx_and_jump (tem, op1, GEU, true, mode, NULL_RTX, NULL, |
1304953e JJ |
572 | done_label, PROB_VERY_LIKELY); |
573 | goto do_error_label; | |
574 | } | |
575 | ||
576 | /* s1 + u2 -> ur */ | |
577 | if (code == PLUS_EXPR && !uns0_p && uns1_p && unsr_p) | |
578 | { | |
579 | op1 = expand_binop (mode, add_optab, op1, sgn, NULL_RTX, false, | |
580 | OPTAB_LIB_WIDEN); | |
581 | /* As we've changed op1, we have to avoid using the value range | |
582 | for the original argument. */ | |
583 | arg1 = error_mark_node; | |
584 | do_xor = true; | |
585 | goto do_signed; | |
586 | } | |
587 | ||
588 | /* u1 - s2 -> ur */ | |
589 | if (code == MINUS_EXPR && uns0_p && !uns1_p && unsr_p) | |
590 | { | |
591 | op0 = expand_binop (mode, add_optab, op0, sgn, NULL_RTX, false, | |
592 | OPTAB_LIB_WIDEN); | |
593 | /* As we've changed op0, we have to avoid using the value range | |
594 | for the original argument. */ | |
595 | arg0 = error_mark_node; | |
596 | do_xor = true; | |
597 | goto do_signed; | |
598 | } | |
599 | ||
600 | /* s1 - u2 -> ur */ | |
601 | if (code == MINUS_EXPR && !uns0_p && uns1_p && unsr_p) | |
602 | { | |
603 | /* Compute the operation. On RTL level, the addition is always | |
604 | unsigned. */ | |
605 | res = expand_binop (mode, sub_optab, op0, op1, NULL_RTX, false, | |
606 | OPTAB_LIB_WIDEN); | |
607 | int pos_neg = get_range_pos_neg (arg0); | |
608 | if (pos_neg == 2) | |
609 | /* If ARG0 is known to be always negative, this is always overflow. */ | |
610 | emit_jump (do_error); | |
611 | else if (pos_neg == 3) | |
612 | /* If ARG0 is not known to be always positive, check at runtime. */ | |
92344ed0 | 613 | do_compare_rtx_and_jump (op0, const0_rtx, LT, false, mode, NULL_RTX, |
1476d1bd MM |
614 | NULL, do_error, PROB_VERY_UNLIKELY); |
615 | do_compare_rtx_and_jump (op1, op0, LEU, true, mode, NULL_RTX, NULL, | |
1304953e JJ |
616 | done_label, PROB_VERY_LIKELY); |
617 | goto do_error_label; | |
618 | } | |
619 | ||
620 | /* u1 - s2 -> sr */ | |
621 | if (code == MINUS_EXPR && uns0_p && !uns1_p && !unsr_p) | |
622 | { | |
623 | /* Compute the operation. On RTL level, the addition is always | |
624 | unsigned. */ | |
625 | res = expand_binop (mode, sub_optab, op0, op1, NULL_RTX, false, | |
626 | OPTAB_LIB_WIDEN); | |
627 | rtx tem = expand_binop (mode, add_optab, op1, sgn, NULL_RTX, false, | |
628 | OPTAB_LIB_WIDEN); | |
1476d1bd | 629 | do_compare_rtx_and_jump (op0, tem, LTU, true, mode, NULL_RTX, NULL, |
1304953e JJ |
630 | done_label, PROB_VERY_LIKELY); |
631 | goto do_error_label; | |
632 | } | |
633 | ||
634 | /* u1 + u2 -> sr */ | |
635 | if (code == PLUS_EXPR && uns0_p && uns1_p && !unsr_p) | |
636 | { | |
637 | /* Compute the operation. On RTL level, the addition is always | |
638 | unsigned. */ | |
639 | res = expand_binop (mode, add_optab, op0, op1, NULL_RTX, false, | |
640 | OPTAB_LIB_WIDEN); | |
92344ed0 | 641 | do_compare_rtx_and_jump (res, const0_rtx, LT, false, mode, NULL_RTX, |
1476d1bd | 642 | NULL, do_error, PROB_VERY_UNLIKELY); |
1304953e JJ |
643 | rtx tem = op1; |
644 | /* The operation is commutative, so we can pick operand to compare | |
645 | against. For prec <= BITS_PER_WORD, I think preferring REG operand | |
646 | is better over CONST_INT, because the CONST_INT might enlarge the | |
647 | instruction or CSE would need to figure out we'd already loaded it | |
648 | into a register before. For prec > BITS_PER_WORD, I think CONST_INT | |
649 | might be more beneficial, as then the multi-word comparison can be | |
650 | perhaps simplified. */ | |
651 | if (prec <= BITS_PER_WORD | |
652 | ? (CONST_SCALAR_INT_P (op1) && REG_P (op0)) | |
653 | : CONST_SCALAR_INT_P (op0)) | |
654 | tem = op0; | |
1476d1bd | 655 | do_compare_rtx_and_jump (res, tem, GEU, true, mode, NULL_RTX, NULL, |
1304953e JJ |
656 | done_label, PROB_VERY_LIKELY); |
657 | goto do_error_label; | |
658 | } | |
659 | ||
660 | /* s1 +- s2 -> ur */ | |
661 | if (!uns0_p && !uns1_p && unsr_p) | |
662 | { | |
663 | /* Compute the operation. On RTL level, the addition is always | |
664 | unsigned. */ | |
665 | res = expand_binop (mode, code == PLUS_EXPR ? add_optab : sub_optab, | |
666 | op0, op1, NULL_RTX, false, OPTAB_LIB_WIDEN); | |
667 | int pos_neg = get_range_pos_neg (arg1); | |
668 | if (code == PLUS_EXPR) | |
669 | { | |
670 | int pos_neg0 = get_range_pos_neg (arg0); | |
671 | if (pos_neg0 != 3 && pos_neg == 3) | |
672 | { | |
6b4db501 | 673 | std::swap (op0, op1); |
1304953e JJ |
674 | pos_neg = pos_neg0; |
675 | } | |
676 | } | |
677 | rtx tem; | |
678 | if (pos_neg != 3) | |
679 | { | |
680 | tem = expand_binop (mode, ((pos_neg == 1) ^ (code == MINUS_EXPR)) | |
681 | ? and_optab : ior_optab, | |
682 | op0, res, NULL_RTX, false, OPTAB_LIB_WIDEN); | |
1476d1bd MM |
683 | do_compare_rtx_and_jump (tem, const0_rtx, GE, false, mode, NULL, |
684 | NULL, done_label, PROB_VERY_LIKELY); | |
1304953e JJ |
685 | } |
686 | else | |
687 | { | |
688 | rtx_code_label *do_ior_label = gen_label_rtx (); | |
92344ed0 JJ |
689 | do_compare_rtx_and_jump (op1, const0_rtx, |
690 | code == MINUS_EXPR ? GE : LT, false, mode, | |
1476d1bd | 691 | NULL_RTX, NULL, do_ior_label, |
92344ed0 | 692 | PROB_EVEN); |
1304953e JJ |
693 | tem = expand_binop (mode, and_optab, op0, res, NULL_RTX, false, |
694 | OPTAB_LIB_WIDEN); | |
92344ed0 | 695 | do_compare_rtx_and_jump (tem, const0_rtx, GE, false, mode, NULL_RTX, |
1476d1bd | 696 | NULL, done_label, PROB_VERY_LIKELY); |
1304953e JJ |
697 | emit_jump (do_error); |
698 | emit_label (do_ior_label); | |
699 | tem = expand_binop (mode, ior_optab, op0, res, NULL_RTX, false, | |
700 | OPTAB_LIB_WIDEN); | |
92344ed0 | 701 | do_compare_rtx_and_jump (tem, const0_rtx, GE, false, mode, NULL_RTX, |
1476d1bd | 702 | NULL, done_label, PROB_VERY_LIKELY); |
1304953e JJ |
703 | } |
704 | goto do_error_label; | |
705 | } | |
706 | ||
707 | /* u1 - u2 -> sr */ | |
708 | if (code == MINUS_EXPR && uns0_p && uns1_p && !unsr_p) | |
709 | { | |
710 | /* Compute the operation. On RTL level, the addition is always | |
711 | unsigned. */ | |
712 | res = expand_binop (mode, sub_optab, op0, op1, NULL_RTX, false, | |
713 | OPTAB_LIB_WIDEN); | |
714 | rtx_code_label *op0_geu_op1 = gen_label_rtx (); | |
1476d1bd | 715 | do_compare_rtx_and_jump (op0, op1, GEU, true, mode, NULL_RTX, NULL, |
1304953e | 716 | op0_geu_op1, PROB_EVEN); |
92344ed0 | 717 | do_compare_rtx_and_jump (res, const0_rtx, LT, false, mode, NULL_RTX, |
1476d1bd | 718 | NULL, done_label, PROB_VERY_LIKELY); |
1304953e JJ |
719 | emit_jump (do_error); |
720 | emit_label (op0_geu_op1); | |
92344ed0 | 721 | do_compare_rtx_and_jump (res, const0_rtx, GE, false, mode, NULL_RTX, |
1476d1bd | 722 | NULL, done_label, PROB_VERY_LIKELY); |
1304953e JJ |
723 | goto do_error_label; |
724 | } | |
31e071ae | 725 | |
1304953e JJ |
726 | gcc_assert (!uns0_p && !uns1_p && !unsr_p); |
727 | ||
728 | /* s1 +- s2 -> sr */ | |
729 | do_signed: ; | |
730 | enum insn_code icode; | |
731 | icode = optab_handler (code == PLUS_EXPR ? addv4_optab : subv4_optab, mode); | |
31e071ae MP |
732 | if (icode != CODE_FOR_nothing) |
733 | { | |
734 | struct expand_operand ops[4]; | |
da664544 | 735 | rtx_insn *last = get_last_insn (); |
31e071ae MP |
736 | |
737 | res = gen_reg_rtx (mode); | |
738 | create_output_operand (&ops[0], res, mode); | |
739 | create_input_operand (&ops[1], op0, mode); | |
740 | create_input_operand (&ops[2], op1, mode); | |
741 | create_fixed_operand (&ops[3], do_error); | |
742 | if (maybe_expand_insn (icode, 4, ops)) | |
743 | { | |
744 | last = get_last_insn (); | |
0a6a6ac9 | 745 | if (profile_status_for_fn (cfun) != PROFILE_ABSENT |
31e071ae MP |
746 | && JUMP_P (last) |
747 | && any_condjump_p (last) | |
748 | && !find_reg_note (last, REG_BR_PROB, 0)) | |
749 | add_int_reg_note (last, REG_BR_PROB, PROB_VERY_UNLIKELY); | |
750 | emit_jump (done_label); | |
751 | } | |
752 | else | |
753 | { | |
754 | delete_insns_since (last); | |
755 | icode = CODE_FOR_nothing; | |
756 | } | |
757 | } | |
758 | ||
759 | if (icode == CODE_FOR_nothing) | |
760 | { | |
da664544 | 761 | rtx_code_label *sub_check = gen_label_rtx (); |
97286431 | 762 | int pos_neg = 3; |
31e071ae MP |
763 | |
764 | /* Compute the operation. On RTL level, the addition is always | |
765 | unsigned. */ | |
7ddf4d5a MP |
766 | res = expand_binop (mode, code == PLUS_EXPR ? add_optab : sub_optab, |
767 | op0, op1, NULL_RTX, false, OPTAB_LIB_WIDEN); | |
31e071ae | 768 | |
f451d3a8 JJ |
769 | /* If we can prove one of the arguments (for MINUS_EXPR only |
770 | the second operand, as subtraction is not commutative) is always | |
771 | non-negative or always negative, we can do just one comparison | |
772 | and conditional jump instead of 2 at runtime, 3 present in the | |
97286431 JJ |
773 | emitted code. If one of the arguments is CONST_INT, all we |
774 | need is to make sure it is op1, then the first | |
92344ed0 | 775 | do_compare_rtx_and_jump will be just folded. Otherwise try |
97286431 | 776 | to use range info if available. */ |
f451d3a8 | 777 | if (code == PLUS_EXPR && CONST_INT_P (op0)) |
6b4db501 | 778 | std::swap (op0, op1); |
97286431 JJ |
779 | else if (CONST_INT_P (op1)) |
780 | ; | |
f451d3a8 | 781 | else if (code == PLUS_EXPR && TREE_CODE (arg0) == SSA_NAME) |
97286431 | 782 | { |
1304953e | 783 | pos_neg = get_range_pos_neg (arg0); |
97286431 | 784 | if (pos_neg != 3) |
6b4db501 | 785 | std::swap (op0, op1); |
97286431 JJ |
786 | } |
787 | if (pos_neg == 3 && !CONST_INT_P (op1) && TREE_CODE (arg1) == SSA_NAME) | |
1304953e | 788 | pos_neg = get_range_pos_neg (arg1); |
97286431 | 789 | |
31e071ae | 790 | /* If the op1 is negative, we have to use a different check. */ |
97286431 | 791 | if (pos_neg == 3) |
92344ed0 | 792 | do_compare_rtx_and_jump (op1, const0_rtx, LT, false, mode, NULL_RTX, |
1476d1bd | 793 | NULL, sub_check, PROB_EVEN); |
31e071ae | 794 | |
7ddf4d5a | 795 | /* Compare the result of the operation with one of the operands. */ |
97286431 | 796 | if (pos_neg & 1) |
92344ed0 | 797 | do_compare_rtx_and_jump (res, op0, code == PLUS_EXPR ? GE : LE, |
1476d1bd | 798 | false, mode, NULL_RTX, NULL, done_label, |
97286431 JJ |
799 | PROB_VERY_LIKELY); |
800 | ||
31e071ae | 801 | /* If we get here, we have to print the error. */ |
97286431 JJ |
802 | if (pos_neg == 3) |
803 | { | |
804 | emit_jump (do_error); | |
805 | ||
806 | emit_label (sub_check); | |
807 | } | |
31e071ae | 808 | |
31e071ae | 809 | /* We have k = a + b for b < 0 here. k <= a must hold. */ |
97286431 | 810 | if (pos_neg & 2) |
92344ed0 | 811 | do_compare_rtx_and_jump (res, op0, code == PLUS_EXPR ? LE : GE, |
1476d1bd | 812 | false, mode, NULL_RTX, NULL, done_label, |
97286431 | 813 | PROB_VERY_LIKELY); |
31e071ae MP |
814 | } |
815 | ||
1304953e | 816 | do_error_label: |
1769415d | 817 | emit_label (do_error); |
1304953e JJ |
818 | if (is_ubsan) |
819 | { | |
820 | /* Expand the ubsan builtin call. */ | |
821 | push_temp_slots (); | |
822 | fn = ubsan_build_overflow_builtin (code, loc, TREE_TYPE (arg0), | |
823 | arg0, arg1); | |
824 | expand_normal (fn); | |
825 | pop_temp_slots (); | |
826 | do_pending_stack_adjust (); | |
827 | } | |
828 | else if (lhs) | |
829 | write_complex_part (target, const1_rtx, true); | |
31e071ae | 830 | |
1769415d MP |
831 | /* We're done. */ |
832 | emit_label (done_label); | |
31e071ae MP |
833 | |
834 | if (lhs) | |
1304953e JJ |
835 | { |
836 | if (is_ubsan) | |
5620052d | 837 | expand_ubsan_result_store (target, res); |
1304953e JJ |
838 | else |
839 | { | |
840 | if (do_xor) | |
841 | res = expand_binop (mode, add_optab, res, sgn, NULL_RTX, false, | |
842 | OPTAB_LIB_WIDEN); | |
843 | ||
844 | expand_arith_overflow_result_store (lhs, target, mode, res); | |
845 | } | |
846 | } | |
31e071ae MP |
847 | } |
848 | ||
849 | /* Add negate overflow checking to the statement STMT. */ | |
850 | ||
1304953e JJ |
851 | static void |
852 | expand_neg_overflow (location_t loc, tree lhs, tree arg1, bool is_ubsan) | |
31e071ae MP |
853 | { |
854 | rtx res, op1; | |
1304953e | 855 | tree fn; |
da664544 DM |
856 | rtx_code_label *done_label, *do_error; |
857 | rtx target = NULL_RTX; | |
31e071ae | 858 | |
31e071ae MP |
859 | done_label = gen_label_rtx (); |
860 | do_error = gen_label_rtx (); | |
31e071ae MP |
861 | |
862 | do_pending_stack_adjust (); | |
863 | op1 = expand_normal (arg1); | |
864 | ||
ef4bddc2 | 865 | machine_mode mode = TYPE_MODE (TREE_TYPE (arg1)); |
31e071ae | 866 | if (lhs) |
1304953e JJ |
867 | { |
868 | target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE); | |
869 | if (!is_ubsan) | |
870 | write_complex_part (target, const0_rtx, true); | |
871 | } | |
31e071ae MP |
872 | |
873 | enum insn_code icode = optab_handler (negv3_optab, mode); | |
874 | if (icode != CODE_FOR_nothing) | |
875 | { | |
876 | struct expand_operand ops[3]; | |
da664544 | 877 | rtx_insn *last = get_last_insn (); |
31e071ae MP |
878 | |
879 | res = gen_reg_rtx (mode); | |
880 | create_output_operand (&ops[0], res, mode); | |
881 | create_input_operand (&ops[1], op1, mode); | |
882 | create_fixed_operand (&ops[2], do_error); | |
883 | if (maybe_expand_insn (icode, 3, ops)) | |
884 | { | |
885 | last = get_last_insn (); | |
0a6a6ac9 | 886 | if (profile_status_for_fn (cfun) != PROFILE_ABSENT |
31e071ae MP |
887 | && JUMP_P (last) |
888 | && any_condjump_p (last) | |
889 | && !find_reg_note (last, REG_BR_PROB, 0)) | |
890 | add_int_reg_note (last, REG_BR_PROB, PROB_VERY_UNLIKELY); | |
891 | emit_jump (done_label); | |
892 | } | |
893 | else | |
894 | { | |
895 | delete_insns_since (last); | |
896 | icode = CODE_FOR_nothing; | |
897 | } | |
898 | } | |
899 | ||
900 | if (icode == CODE_FOR_nothing) | |
901 | { | |
902 | /* Compute the operation. On RTL level, the addition is always | |
903 | unsigned. */ | |
904 | res = expand_unop (mode, neg_optab, op1, NULL_RTX, false); | |
905 | ||
906 | /* Compare the operand with the most negative value. */ | |
907 | rtx minv = expand_normal (TYPE_MIN_VALUE (TREE_TYPE (arg1))); | |
1476d1bd | 908 | do_compare_rtx_and_jump (op1, minv, NE, true, mode, NULL_RTX, NULL, |
31e071ae MP |
909 | done_label, PROB_VERY_LIKELY); |
910 | } | |
911 | ||
912 | emit_label (do_error); | |
1304953e JJ |
913 | if (is_ubsan) |
914 | { | |
915 | /* Expand the ubsan builtin call. */ | |
916 | push_temp_slots (); | |
917 | fn = ubsan_build_overflow_builtin (NEGATE_EXPR, loc, TREE_TYPE (arg1), | |
918 | arg1, NULL_TREE); | |
919 | expand_normal (fn); | |
920 | pop_temp_slots (); | |
921 | do_pending_stack_adjust (); | |
922 | } | |
923 | else if (lhs) | |
924 | write_complex_part (target, const1_rtx, true); | |
31e071ae MP |
925 | |
926 | /* We're done. */ | |
927 | emit_label (done_label); | |
928 | ||
929 | if (lhs) | |
1304953e JJ |
930 | { |
931 | if (is_ubsan) | |
5620052d | 932 | expand_ubsan_result_store (target, res); |
1304953e JJ |
933 | else |
934 | expand_arith_overflow_result_store (lhs, target, mode, res); | |
935 | } | |
31e071ae MP |
936 | } |
937 | ||
938 | /* Add mul overflow checking to the statement STMT. */ | |
939 | ||
1304953e JJ |
940 | static void |
941 | expand_mul_overflow (location_t loc, tree lhs, tree arg0, tree arg1, | |
942 | bool unsr_p, bool uns0_p, bool uns1_p, bool is_ubsan) | |
31e071ae MP |
943 | { |
944 | rtx res, op0, op1; | |
1304953e | 945 | tree fn, type; |
da664544 DM |
946 | rtx_code_label *done_label, *do_error; |
947 | rtx target = NULL_RTX; | |
1304953e JJ |
948 | signop sign; |
949 | enum insn_code icode; | |
31e071ae | 950 | |
31e071ae MP |
951 | done_label = gen_label_rtx (); |
952 | do_error = gen_label_rtx (); | |
31e071ae MP |
953 | |
954 | do_pending_stack_adjust (); | |
955 | op0 = expand_normal (arg0); | |
956 | op1 = expand_normal (arg1); | |
957 | ||
ef4bddc2 | 958 | machine_mode mode = TYPE_MODE (TREE_TYPE (arg0)); |
1304953e | 959 | bool uns = unsr_p; |
31e071ae | 960 | if (lhs) |
1304953e JJ |
961 | { |
962 | target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE); | |
963 | if (!is_ubsan) | |
964 | write_complex_part (target, const0_rtx, true); | |
965 | } | |
966 | ||
967 | if (is_ubsan) | |
968 | gcc_assert (!unsr_p && !uns0_p && !uns1_p); | |
969 | ||
970 | /* We assume both operands and result have the same precision | |
971 | here (GET_MODE_BITSIZE (mode)), S stands for signed type | |
972 | with that precision, U for unsigned type with that precision, | |
973 | sgn for unsigned most significant bit in that precision. | |
974 | s1 is signed first operand, u1 is unsigned first operand, | |
975 | s2 is signed second operand, u2 is unsigned second operand, | |
976 | sr is signed result, ur is unsigned result and the following | |
977 | rules say how to compute result (which is always result of | |
978 | the operands as if both were unsigned, cast to the right | |
979 | signedness) and how to compute whether operation overflowed. | |
980 | main_ovf (false) stands for jump on signed multiplication | |
981 | overflow or the main algorithm with uns == false. | |
982 | main_ovf (true) stands for jump on unsigned multiplication | |
983 | overflow or the main algorithm with uns == true. | |
984 | ||
985 | s1 * s2 -> sr | |
986 | res = (S) ((U) s1 * (U) s2) | |
987 | ovf = main_ovf (false) | |
988 | u1 * u2 -> ur | |
989 | res = u1 * u2 | |
990 | ovf = main_ovf (true) | |
991 | s1 * u2 -> ur | |
992 | res = (U) s1 * u2 | |
993 | ovf = (s1 < 0 && u2) || main_ovf (true) | |
994 | u1 * u2 -> sr | |
995 | res = (S) (u1 * u2) | |
996 | ovf = res < 0 || main_ovf (true) | |
997 | s1 * u2 -> sr | |
998 | res = (S) ((U) s1 * u2) | |
999 | ovf = (S) u2 >= 0 ? main_ovf (false) | |
1000 | : (s1 != 0 && (s1 != -1 || u2 != (U) res)) | |
1001 | s1 * s2 -> ur | |
1002 | t1 = (s1 & s2) < 0 ? (-(U) s1) : ((U) s1) | |
1003 | t2 = (s1 & s2) < 0 ? (-(U) s2) : ((U) s2) | |
1004 | res = t1 * t2 | |
1005 | ovf = (s1 ^ s2) < 0 ? (s1 && s2) : main_ovf (true) */ | |
1006 | ||
1007 | if (uns0_p && !uns1_p) | |
1008 | { | |
1009 | /* Multiplication is commutative, if operand signedness differs, | |
1010 | canonicalize to the first operand being signed and second | |
1011 | unsigned to simplify following code. */ | |
6b4db501 MM |
1012 | std::swap (op0, op1); |
1013 | std::swap (arg0, arg1); | |
1014 | uns0_p = false; | |
1015 | uns1_p = true; | |
1304953e JJ |
1016 | } |
1017 | ||
1018 | int pos_neg0 = get_range_pos_neg (arg0); | |
1019 | int pos_neg1 = get_range_pos_neg (arg1); | |
1020 | ||
1021 | /* s1 * u2 -> ur */ | |
1022 | if (!uns0_p && uns1_p && unsr_p) | |
1023 | { | |
1024 | switch (pos_neg0) | |
1025 | { | |
1026 | case 1: | |
1027 | /* If s1 is non-negative, just perform normal u1 * u2 -> ur. */ | |
1028 | goto do_main; | |
1029 | case 2: | |
1030 | /* If s1 is negative, avoid the main code, just multiply and | |
1031 | signal overflow if op1 is not 0. */ | |
1032 | struct separate_ops ops; | |
1033 | ops.code = MULT_EXPR; | |
1034 | ops.type = TREE_TYPE (arg1); | |
1035 | ops.op0 = make_tree (ops.type, op0); | |
1036 | ops.op1 = make_tree (ops.type, op1); | |
1037 | ops.op2 = NULL_TREE; | |
1038 | ops.location = loc; | |
1039 | res = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL); | |
92344ed0 | 1040 | do_compare_rtx_and_jump (op1, const0_rtx, EQ, true, mode, NULL_RTX, |
1476d1bd | 1041 | NULL, done_label, PROB_VERY_LIKELY); |
1304953e JJ |
1042 | goto do_error_label; |
1043 | case 3: | |
1044 | rtx_code_label *do_main_label; | |
1045 | do_main_label = gen_label_rtx (); | |
92344ed0 | 1046 | do_compare_rtx_and_jump (op0, const0_rtx, GE, false, mode, NULL_RTX, |
1476d1bd | 1047 | NULL, do_main_label, PROB_VERY_LIKELY); |
92344ed0 | 1048 | do_compare_rtx_and_jump (op1, const0_rtx, EQ, true, mode, NULL_RTX, |
1476d1bd | 1049 | NULL, do_main_label, PROB_VERY_LIKELY); |
1304953e JJ |
1050 | write_complex_part (target, const1_rtx, true); |
1051 | emit_label (do_main_label); | |
1052 | goto do_main; | |
1053 | default: | |
1054 | gcc_unreachable (); | |
1055 | } | |
1056 | } | |
1057 | ||
1058 | /* u1 * u2 -> sr */ | |
1059 | if (uns0_p && uns1_p && !unsr_p) | |
1060 | { | |
1061 | uns = true; | |
1062 | /* Rest of handling of this case after res is computed. */ | |
1063 | goto do_main; | |
1064 | } | |
1065 | ||
1066 | /* s1 * u2 -> sr */ | |
1067 | if (!uns0_p && uns1_p && !unsr_p) | |
1068 | { | |
1069 | switch (pos_neg1) | |
1070 | { | |
1071 | case 1: | |
1072 | goto do_main; | |
1073 | case 2: | |
1074 | /* If (S) u2 is negative (i.e. u2 is larger than maximum of S, | |
1075 | avoid the main code, just multiply and signal overflow | |
1076 | unless 0 * u2 or -1 * ((U) Smin). */ | |
1077 | struct separate_ops ops; | |
1078 | ops.code = MULT_EXPR; | |
1079 | ops.type = TREE_TYPE (arg1); | |
1080 | ops.op0 = make_tree (ops.type, op0); | |
1081 | ops.op1 = make_tree (ops.type, op1); | |
1082 | ops.op2 = NULL_TREE; | |
1083 | ops.location = loc; | |
1084 | res = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL); | |
92344ed0 | 1085 | do_compare_rtx_and_jump (op0, const0_rtx, EQ, true, mode, NULL_RTX, |
1476d1bd | 1086 | NULL, done_label, PROB_VERY_LIKELY); |
92344ed0 | 1087 | do_compare_rtx_and_jump (op0, constm1_rtx, NE, true, mode, NULL_RTX, |
1476d1bd | 1088 | NULL, do_error, PROB_VERY_UNLIKELY); |
1304953e JJ |
1089 | int prec; |
1090 | prec = GET_MODE_PRECISION (mode); | |
1091 | rtx sgn; | |
1092 | sgn = immed_wide_int_const (wi::min_value (prec, SIGNED), mode); | |
92344ed0 | 1093 | do_compare_rtx_and_jump (op1, sgn, EQ, true, mode, NULL_RTX, |
1476d1bd | 1094 | NULL, done_label, PROB_VERY_LIKELY); |
1304953e JJ |
1095 | goto do_error_label; |
1096 | case 3: | |
1097 | /* Rest of handling of this case after res is computed. */ | |
1098 | goto do_main; | |
1099 | default: | |
1100 | gcc_unreachable (); | |
1101 | } | |
1102 | } | |
31e071ae | 1103 | |
1304953e JJ |
1104 | /* s1 * s2 -> ur */ |
1105 | if (!uns0_p && !uns1_p && unsr_p) | |
1106 | { | |
1107 | rtx tem, tem2; | |
1108 | switch (pos_neg0 | pos_neg1) | |
1109 | { | |
1110 | case 1: /* Both operands known to be non-negative. */ | |
1111 | goto do_main; | |
1112 | case 2: /* Both operands known to be negative. */ | |
1113 | op0 = expand_unop (mode, neg_optab, op0, NULL_RTX, false); | |
1114 | op1 = expand_unop (mode, neg_optab, op1, NULL_RTX, false); | |
1115 | /* Avoid looking at arg0/arg1 ranges, as we've changed | |
1116 | the arguments. */ | |
1117 | arg0 = error_mark_node; | |
1118 | arg1 = error_mark_node; | |
1119 | goto do_main; | |
1120 | case 3: | |
1121 | if ((pos_neg0 ^ pos_neg1) == 3) | |
1122 | { | |
1123 | /* If one operand is known to be negative and the other | |
1124 | non-negative, this overflows always, unless the non-negative | |
1125 | one is 0. Just do normal multiply and set overflow | |
1126 | unless one of the operands is 0. */ | |
1127 | struct separate_ops ops; | |
1128 | ops.code = MULT_EXPR; | |
1129 | ops.type | |
1130 | = build_nonstandard_integer_type (GET_MODE_PRECISION (mode), | |
1131 | 1); | |
1132 | ops.op0 = make_tree (ops.type, op0); | |
1133 | ops.op1 = make_tree (ops.type, op1); | |
1134 | ops.op2 = NULL_TREE; | |
1135 | ops.location = loc; | |
1136 | res = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL); | |
1137 | tem = expand_binop (mode, and_optab, op0, op1, NULL_RTX, false, | |
1138 | OPTAB_LIB_WIDEN); | |
92344ed0 | 1139 | do_compare_rtx_and_jump (tem, const0_rtx, EQ, true, mode, |
1476d1bd | 1140 | NULL_RTX, NULL, done_label, |
92344ed0 | 1141 | PROB_VERY_LIKELY); |
1304953e JJ |
1142 | goto do_error_label; |
1143 | } | |
1144 | /* The general case, do all the needed comparisons at runtime. */ | |
1145 | rtx_code_label *do_main_label, *after_negate_label; | |
1146 | rtx rop0, rop1; | |
1147 | rop0 = gen_reg_rtx (mode); | |
1148 | rop1 = gen_reg_rtx (mode); | |
1149 | emit_move_insn (rop0, op0); | |
1150 | emit_move_insn (rop1, op1); | |
1151 | op0 = rop0; | |
1152 | op1 = rop1; | |
1153 | do_main_label = gen_label_rtx (); | |
1154 | after_negate_label = gen_label_rtx (); | |
1155 | tem = expand_binop (mode, and_optab, op0, op1, NULL_RTX, false, | |
1156 | OPTAB_LIB_WIDEN); | |
92344ed0 | 1157 | do_compare_rtx_and_jump (tem, const0_rtx, GE, false, mode, NULL_RTX, |
1476d1bd | 1158 | NULL, after_negate_label, PROB_VERY_LIKELY); |
1304953e JJ |
1159 | /* Both arguments negative here, negate them and continue with |
1160 | normal unsigned overflow checking multiplication. */ | |
1161 | emit_move_insn (op0, expand_unop (mode, neg_optab, op0, | |
1162 | NULL_RTX, false)); | |
1163 | emit_move_insn (op1, expand_unop (mode, neg_optab, op1, | |
1164 | NULL_RTX, false)); | |
1165 | /* Avoid looking at arg0/arg1 ranges, as we might have changed | |
1166 | the arguments. */ | |
1167 | arg0 = error_mark_node; | |
1168 | arg1 = error_mark_node; | |
1169 | emit_jump (do_main_label); | |
1170 | emit_label (after_negate_label); | |
1171 | tem2 = expand_binop (mode, xor_optab, op0, op1, NULL_RTX, false, | |
1172 | OPTAB_LIB_WIDEN); | |
92344ed0 | 1173 | do_compare_rtx_and_jump (tem2, const0_rtx, GE, false, mode, NULL_RTX, |
1476d1bd | 1174 | NULL, do_main_label, PROB_VERY_LIKELY); |
1304953e JJ |
1175 | /* One argument is negative here, the other positive. This |
1176 | overflows always, unless one of the arguments is 0. But | |
1177 | if e.g. s2 is 0, (U) s1 * 0 doesn't overflow, whatever s1 | |
1178 | is, thus we can keep do_main code oring in overflow as is. */ | |
92344ed0 | 1179 | do_compare_rtx_and_jump (tem, const0_rtx, EQ, true, mode, NULL_RTX, |
1476d1bd | 1180 | NULL, do_main_label, PROB_VERY_LIKELY); |
1304953e JJ |
1181 | write_complex_part (target, const1_rtx, true); |
1182 | emit_label (do_main_label); | |
1183 | goto do_main; | |
1184 | default: | |
1185 | gcc_unreachable (); | |
1186 | } | |
1187 | } | |
1188 | ||
1189 | do_main: | |
1190 | type = build_nonstandard_integer_type (GET_MODE_PRECISION (mode), uns); | |
1191 | sign = uns ? UNSIGNED : SIGNED; | |
1192 | icode = optab_handler (uns ? umulv4_optab : mulv4_optab, mode); | |
31e071ae MP |
1193 | if (icode != CODE_FOR_nothing) |
1194 | { | |
1195 | struct expand_operand ops[4]; | |
da664544 | 1196 | rtx_insn *last = get_last_insn (); |
31e071ae MP |
1197 | |
1198 | res = gen_reg_rtx (mode); | |
1199 | create_output_operand (&ops[0], res, mode); | |
1200 | create_input_operand (&ops[1], op0, mode); | |
1201 | create_input_operand (&ops[2], op1, mode); | |
1202 | create_fixed_operand (&ops[3], do_error); | |
1203 | if (maybe_expand_insn (icode, 4, ops)) | |
1204 | { | |
1205 | last = get_last_insn (); | |
0a6a6ac9 | 1206 | if (profile_status_for_fn (cfun) != PROFILE_ABSENT |
31e071ae MP |
1207 | && JUMP_P (last) |
1208 | && any_condjump_p (last) | |
1209 | && !find_reg_note (last, REG_BR_PROB, 0)) | |
1210 | add_int_reg_note (last, REG_BR_PROB, PROB_VERY_UNLIKELY); | |
1211 | emit_jump (done_label); | |
1212 | } | |
1213 | else | |
1214 | { | |
1215 | delete_insns_since (last); | |
1216 | icode = CODE_FOR_nothing; | |
1217 | } | |
1218 | } | |
1219 | ||
1220 | if (icode == CODE_FOR_nothing) | |
1221 | { | |
1222 | struct separate_ops ops; | |
1304953e JJ |
1223 | int prec = GET_MODE_PRECISION (mode); |
1224 | machine_mode hmode = mode_for_size (prec / 2, MODE_INT, 1); | |
1225 | ops.op0 = make_tree (type, op0); | |
1226 | ops.op1 = make_tree (type, op1); | |
31e071ae | 1227 | ops.op2 = NULL_TREE; |
1304953e | 1228 | ops.location = loc; |
31e071ae MP |
1229 | if (GET_MODE_2XWIDER_MODE (mode) != VOIDmode |
1230 | && targetm.scalar_mode_supported_p (GET_MODE_2XWIDER_MODE (mode))) | |
1231 | { | |
ef4bddc2 | 1232 | machine_mode wmode = GET_MODE_2XWIDER_MODE (mode); |
31e071ae MP |
1233 | ops.code = WIDEN_MULT_EXPR; |
1234 | ops.type | |
1304953e | 1235 | = build_nonstandard_integer_type (GET_MODE_PRECISION (wmode), uns); |
31e071ae MP |
1236 | |
1237 | res = expand_expr_real_2 (&ops, NULL_RTX, wmode, EXPAND_NORMAL); | |
1304953e JJ |
1238 | rtx hipart = expand_shift (RSHIFT_EXPR, wmode, res, prec, |
1239 | NULL_RTX, uns); | |
31e071ae MP |
1240 | hipart = gen_lowpart (mode, hipart); |
1241 | res = gen_lowpart (mode, res); | |
1304953e JJ |
1242 | if (uns) |
1243 | /* For the unsigned multiplication, there was overflow if | |
1244 | HIPART is non-zero. */ | |
92344ed0 | 1245 | do_compare_rtx_and_jump (hipart, const0_rtx, EQ, true, mode, |
1476d1bd | 1246 | NULL_RTX, NULL, done_label, |
92344ed0 | 1247 | PROB_VERY_LIKELY); |
1304953e JJ |
1248 | else |
1249 | { | |
1250 | rtx signbit = expand_shift (RSHIFT_EXPR, mode, res, prec - 1, | |
1251 | NULL_RTX, 0); | |
1252 | /* RES is low half of the double width result, HIPART | |
1253 | the high half. There was overflow if | |
1254 | HIPART is different from RES < 0 ? -1 : 0. */ | |
92344ed0 | 1255 | do_compare_rtx_and_jump (signbit, hipart, EQ, true, mode, |
1476d1bd | 1256 | NULL_RTX, NULL, done_label, |
92344ed0 | 1257 | PROB_VERY_LIKELY); |
1304953e | 1258 | } |
31e071ae | 1259 | } |
1304953e | 1260 | else if (hmode != BLKmode && 2 * GET_MODE_PRECISION (hmode) == prec) |
d5fa9cc9 | 1261 | { |
da664544 DM |
1262 | rtx_code_label *large_op0 = gen_label_rtx (); |
1263 | rtx_code_label *small_op0_large_op1 = gen_label_rtx (); | |
1264 | rtx_code_label *one_small_one_large = gen_label_rtx (); | |
1265 | rtx_code_label *both_ops_large = gen_label_rtx (); | |
1304953e JJ |
1266 | rtx_code_label *after_hipart_neg = uns ? NULL : gen_label_rtx (); |
1267 | rtx_code_label *after_lopart_neg = uns ? NULL : gen_label_rtx (); | |
da664544 | 1268 | rtx_code_label *do_overflow = gen_label_rtx (); |
1304953e | 1269 | rtx_code_label *hipart_different = uns ? NULL : gen_label_rtx (); |
d5fa9cc9 | 1270 | |
807e902e | 1271 | unsigned int hprec = GET_MODE_PRECISION (hmode); |
d5fa9cc9 | 1272 | rtx hipart0 = expand_shift (RSHIFT_EXPR, mode, op0, hprec, |
1304953e | 1273 | NULL_RTX, uns); |
d5fa9cc9 JJ |
1274 | hipart0 = gen_lowpart (hmode, hipart0); |
1275 | rtx lopart0 = gen_lowpart (hmode, op0); | |
1304953e JJ |
1276 | rtx signbit0 = const0_rtx; |
1277 | if (!uns) | |
1278 | signbit0 = expand_shift (RSHIFT_EXPR, hmode, lopart0, hprec - 1, | |
1279 | NULL_RTX, 0); | |
d5fa9cc9 | 1280 | rtx hipart1 = expand_shift (RSHIFT_EXPR, mode, op1, hprec, |
1304953e | 1281 | NULL_RTX, uns); |
d5fa9cc9 JJ |
1282 | hipart1 = gen_lowpart (hmode, hipart1); |
1283 | rtx lopart1 = gen_lowpart (hmode, op1); | |
1304953e JJ |
1284 | rtx signbit1 = const0_rtx; |
1285 | if (!uns) | |
1286 | signbit1 = expand_shift (RSHIFT_EXPR, hmode, lopart1, hprec - 1, | |
1287 | NULL_RTX, 0); | |
d5fa9cc9 JJ |
1288 | |
1289 | res = gen_reg_rtx (mode); | |
1290 | ||
1291 | /* True if op0 resp. op1 are known to be in the range of | |
1292 | halfstype. */ | |
1293 | bool op0_small_p = false; | |
1294 | bool op1_small_p = false; | |
1295 | /* True if op0 resp. op1 are known to have all zeros or all ones | |
1296 | in the upper half of bits, but are not known to be | |
1297 | op{0,1}_small_p. */ | |
1298 | bool op0_medium_p = false; | |
1299 | bool op1_medium_p = false; | |
1300 | /* -1 if op{0,1} is known to be negative, 0 if it is known to be | |
1301 | nonnegative, 1 if unknown. */ | |
1302 | int op0_sign = 1; | |
1303 | int op1_sign = 1; | |
1304 | ||
1304953e JJ |
1305 | if (pos_neg0 == 1) |
1306 | op0_sign = 0; | |
1307 | else if (pos_neg0 == 2) | |
1308 | op0_sign = -1; | |
1309 | if (pos_neg1 == 1) | |
1310 | op1_sign = 0; | |
1311 | else if (pos_neg1 == 2) | |
1312 | op1_sign = -1; | |
1313 | ||
1314 | unsigned int mprec0 = prec; | |
1315 | if (arg0 != error_mark_node) | |
1316 | mprec0 = get_min_precision (arg0, sign); | |
1317 | if (mprec0 <= hprec) | |
1318 | op0_small_p = true; | |
1319 | else if (!uns && mprec0 <= hprec + 1) | |
1320 | op0_medium_p = true; | |
1321 | unsigned int mprec1 = prec; | |
1322 | if (arg1 != error_mark_node) | |
1323 | mprec1 = get_min_precision (arg1, sign); | |
1324 | if (mprec1 <= hprec) | |
1325 | op1_small_p = true; | |
1326 | else if (!uns && mprec1 <= hprec + 1) | |
1327 | op1_medium_p = true; | |
d5fa9cc9 JJ |
1328 | |
1329 | int smaller_sign = 1; | |
1330 | int larger_sign = 1; | |
1331 | if (op0_small_p) | |
1332 | { | |
1333 | smaller_sign = op0_sign; | |
1334 | larger_sign = op1_sign; | |
1335 | } | |
1336 | else if (op1_small_p) | |
1337 | { | |
1338 | smaller_sign = op1_sign; | |
1339 | larger_sign = op0_sign; | |
1340 | } | |
1341 | else if (op0_sign == op1_sign) | |
1342 | { | |
1343 | smaller_sign = op0_sign; | |
1344 | larger_sign = op0_sign; | |
1345 | } | |
1346 | ||
1347 | if (!op0_small_p) | |
92344ed0 | 1348 | do_compare_rtx_and_jump (signbit0, hipart0, NE, true, hmode, |
1476d1bd | 1349 | NULL_RTX, NULL, large_op0, |
92344ed0 | 1350 | PROB_UNLIKELY); |
d5fa9cc9 JJ |
1351 | |
1352 | if (!op1_small_p) | |
92344ed0 | 1353 | do_compare_rtx_and_jump (signbit1, hipart1, NE, true, hmode, |
1476d1bd | 1354 | NULL_RTX, NULL, small_op0_large_op1, |
d5fa9cc9 JJ |
1355 | PROB_UNLIKELY); |
1356 | ||
1304953e JJ |
1357 | /* If both op0 and op1 are sign (!uns) or zero (uns) extended from |
1358 | hmode to mode, the multiplication will never overflow. We can | |
1359 | do just one hmode x hmode => mode widening multiplication. */ | |
1360 | rtx lopart0s = lopart0, lopart1s = lopart1; | |
d5fa9cc9 JJ |
1361 | if (GET_CODE (lopart0) == SUBREG) |
1362 | { | |
1304953e JJ |
1363 | lopart0s = shallow_copy_rtx (lopart0); |
1364 | SUBREG_PROMOTED_VAR_P (lopart0s) = 1; | |
1365 | SUBREG_PROMOTED_SET (lopart0s, uns ? SRP_UNSIGNED : SRP_SIGNED); | |
d5fa9cc9 JJ |
1366 | } |
1367 | if (GET_CODE (lopart1) == SUBREG) | |
1368 | { | |
1304953e JJ |
1369 | lopart1s = shallow_copy_rtx (lopart1); |
1370 | SUBREG_PROMOTED_VAR_P (lopart1s) = 1; | |
1371 | SUBREG_PROMOTED_SET (lopart1s, uns ? SRP_UNSIGNED : SRP_SIGNED); | |
d5fa9cc9 | 1372 | } |
1304953e JJ |
1373 | tree halfstype = build_nonstandard_integer_type (hprec, uns); |
1374 | ops.op0 = make_tree (halfstype, lopart0s); | |
1375 | ops.op1 = make_tree (halfstype, lopart1s); | |
d5fa9cc9 | 1376 | ops.code = WIDEN_MULT_EXPR; |
1304953e | 1377 | ops.type = type; |
d5fa9cc9 JJ |
1378 | rtx thisres |
1379 | = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL); | |
1380 | emit_move_insn (res, thisres); | |
1381 | emit_jump (done_label); | |
1382 | ||
1383 | emit_label (small_op0_large_op1); | |
1384 | ||
1304953e JJ |
1385 | /* If op0 is sign (!uns) or zero (uns) extended from hmode to mode, |
1386 | but op1 is not, just swap the arguments and handle it as op1 | |
1387 | sign/zero extended, op0 not. */ | |
d5fa9cc9 JJ |
1388 | rtx larger = gen_reg_rtx (mode); |
1389 | rtx hipart = gen_reg_rtx (hmode); | |
1390 | rtx lopart = gen_reg_rtx (hmode); | |
1391 | emit_move_insn (larger, op1); | |
1392 | emit_move_insn (hipart, hipart1); | |
1393 | emit_move_insn (lopart, lopart0); | |
1394 | emit_jump (one_small_one_large); | |
1395 | ||
1396 | emit_label (large_op0); | |
1397 | ||
1398 | if (!op1_small_p) | |
92344ed0 | 1399 | do_compare_rtx_and_jump (signbit1, hipart1, NE, true, hmode, |
1476d1bd | 1400 | NULL_RTX, NULL, both_ops_large, |
92344ed0 | 1401 | PROB_UNLIKELY); |
d5fa9cc9 | 1402 | |
1304953e JJ |
1403 | /* If op1 is sign (!uns) or zero (uns) extended from hmode to mode, |
1404 | but op0 is not, prepare larger, hipart and lopart pseudos and | |
1405 | handle it together with small_op0_large_op1. */ | |
d5fa9cc9 JJ |
1406 | emit_move_insn (larger, op0); |
1407 | emit_move_insn (hipart, hipart0); | |
1408 | emit_move_insn (lopart, lopart1); | |
1409 | ||
1410 | emit_label (one_small_one_large); | |
1411 | ||
1412 | /* lopart is the low part of the operand that is sign extended | |
1413 | to mode, larger is the the other operand, hipart is the | |
1414 | high part of larger and lopart0 and lopart1 are the low parts | |
1415 | of both operands. | |
1416 | We perform lopart0 * lopart1 and lopart * hipart widening | |
1417 | multiplications. */ | |
1418 | tree halfutype = build_nonstandard_integer_type (hprec, 1); | |
1419 | ops.op0 = make_tree (halfutype, lopart0); | |
1420 | ops.op1 = make_tree (halfutype, lopart1); | |
1421 | rtx lo0xlo1 | |
1422 | = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL); | |
1423 | ||
1424 | ops.op0 = make_tree (halfutype, lopart); | |
1425 | ops.op1 = make_tree (halfutype, hipart); | |
1426 | rtx loxhi = gen_reg_rtx (mode); | |
1427 | rtx tem = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL); | |
1428 | emit_move_insn (loxhi, tem); | |
1429 | ||
1304953e JJ |
1430 | if (!uns) |
1431 | { | |
1432 | /* if (hipart < 0) loxhi -= lopart << (bitsize / 2); */ | |
1433 | if (larger_sign == 0) | |
1434 | emit_jump (after_hipart_neg); | |
1435 | else if (larger_sign != -1) | |
92344ed0 | 1436 | do_compare_rtx_and_jump (hipart, const0_rtx, GE, false, hmode, |
1476d1bd | 1437 | NULL_RTX, NULL, after_hipart_neg, |
1304953e JJ |
1438 | PROB_EVEN); |
1439 | ||
1440 | tem = convert_modes (mode, hmode, lopart, 1); | |
1441 | tem = expand_shift (LSHIFT_EXPR, mode, tem, hprec, NULL_RTX, 1); | |
1442 | tem = expand_simple_binop (mode, MINUS, loxhi, tem, NULL_RTX, | |
1443 | 1, OPTAB_DIRECT); | |
1444 | emit_move_insn (loxhi, tem); | |
1445 | ||
1446 | emit_label (after_hipart_neg); | |
1447 | ||
1448 | /* if (lopart < 0) loxhi -= larger; */ | |
1449 | if (smaller_sign == 0) | |
1450 | emit_jump (after_lopart_neg); | |
1451 | else if (smaller_sign != -1) | |
92344ed0 | 1452 | do_compare_rtx_and_jump (lopart, const0_rtx, GE, false, hmode, |
1476d1bd | 1453 | NULL_RTX, NULL, after_lopart_neg, |
1304953e JJ |
1454 | PROB_EVEN); |
1455 | ||
1456 | tem = expand_simple_binop (mode, MINUS, loxhi, larger, NULL_RTX, | |
1457 | 1, OPTAB_DIRECT); | |
1458 | emit_move_insn (loxhi, tem); | |
1459 | ||
1460 | emit_label (after_lopart_neg); | |
1461 | } | |
d5fa9cc9 JJ |
1462 | |
1463 | /* loxhi += (uns) lo0xlo1 >> (bitsize / 2); */ | |
1464 | tem = expand_shift (RSHIFT_EXPR, mode, lo0xlo1, hprec, NULL_RTX, 1); | |
1465 | tem = expand_simple_binop (mode, PLUS, loxhi, tem, NULL_RTX, | |
1466 | 1, OPTAB_DIRECT); | |
1467 | emit_move_insn (loxhi, tem); | |
1468 | ||
1469 | /* if (loxhi >> (bitsize / 2) | |
1304953e JJ |
1470 | == (hmode) loxhi >> (bitsize / 2 - 1)) (if !uns) |
1471 | if (loxhi >> (bitsize / 2) == 0 (if uns). */ | |
d5fa9cc9 JJ |
1472 | rtx hipartloxhi = expand_shift (RSHIFT_EXPR, mode, loxhi, hprec, |
1473 | NULL_RTX, 0); | |
1474 | hipartloxhi = gen_lowpart (hmode, hipartloxhi); | |
1304953e JJ |
1475 | rtx signbitloxhi = const0_rtx; |
1476 | if (!uns) | |
1477 | signbitloxhi = expand_shift (RSHIFT_EXPR, hmode, | |
1478 | gen_lowpart (hmode, loxhi), | |
1479 | hprec - 1, NULL_RTX, 0); | |
d5fa9cc9 | 1480 | |
92344ed0 | 1481 | do_compare_rtx_and_jump (signbitloxhi, hipartloxhi, NE, true, hmode, |
1476d1bd | 1482 | NULL_RTX, NULL, do_overflow, |
d5fa9cc9 JJ |
1483 | PROB_VERY_UNLIKELY); |
1484 | ||
1485 | /* res = (loxhi << (bitsize / 2)) | (hmode) lo0xlo1; */ | |
1486 | rtx loxhishifted = expand_shift (LSHIFT_EXPR, mode, loxhi, hprec, | |
1487 | NULL_RTX, 1); | |
1488 | tem = convert_modes (mode, hmode, gen_lowpart (hmode, lo0xlo1), 1); | |
1489 | ||
1490 | tem = expand_simple_binop (mode, IOR, loxhishifted, tem, res, | |
1491 | 1, OPTAB_DIRECT); | |
1492 | if (tem != res) | |
1493 | emit_move_insn (res, tem); | |
1494 | emit_jump (done_label); | |
1495 | ||
1496 | emit_label (both_ops_large); | |
1497 | ||
1304953e JJ |
1498 | /* If both operands are large (not sign (!uns) or zero (uns) |
1499 | extended from hmode), then perform the full multiplication | |
1500 | which will be the result of the operation. | |
1501 | The only cases which don't overflow are for signed multiplication | |
1502 | some cases where both hipart0 and highpart1 are 0 or -1. | |
1503 | For unsigned multiplication when high parts are both non-zero | |
1504 | this overflows always. */ | |
d5fa9cc9 | 1505 | ops.code = MULT_EXPR; |
1304953e JJ |
1506 | ops.op0 = make_tree (type, op0); |
1507 | ops.op1 = make_tree (type, op1); | |
d5fa9cc9 JJ |
1508 | tem = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL); |
1509 | emit_move_insn (res, tem); | |
1510 | ||
1304953e | 1511 | if (!uns) |
d5fa9cc9 | 1512 | { |
1304953e JJ |
1513 | if (!op0_medium_p) |
1514 | { | |
1515 | tem = expand_simple_binop (hmode, PLUS, hipart0, const1_rtx, | |
1516 | NULL_RTX, 1, OPTAB_DIRECT); | |
92344ed0 | 1517 | do_compare_rtx_and_jump (tem, const1_rtx, GTU, true, hmode, |
1476d1bd | 1518 | NULL_RTX, NULL, do_error, |
1304953e JJ |
1519 | PROB_VERY_UNLIKELY); |
1520 | } | |
d5fa9cc9 | 1521 | |
1304953e JJ |
1522 | if (!op1_medium_p) |
1523 | { | |
1524 | tem = expand_simple_binop (hmode, PLUS, hipart1, const1_rtx, | |
1525 | NULL_RTX, 1, OPTAB_DIRECT); | |
92344ed0 | 1526 | do_compare_rtx_and_jump (tem, const1_rtx, GTU, true, hmode, |
1476d1bd | 1527 | NULL_RTX, NULL, do_error, |
1304953e JJ |
1528 | PROB_VERY_UNLIKELY); |
1529 | } | |
d5fa9cc9 | 1530 | |
1304953e JJ |
1531 | /* At this point hipart{0,1} are both in [-1, 0]. If they are |
1532 | the same, overflow happened if res is negative, if they are | |
1533 | different, overflow happened if res is positive. */ | |
1534 | if (op0_sign != 1 && op1_sign != 1 && op0_sign != op1_sign) | |
1535 | emit_jump (hipart_different); | |
1536 | else if (op0_sign == 1 || op1_sign == 1) | |
92344ed0 | 1537 | do_compare_rtx_and_jump (hipart0, hipart1, NE, true, hmode, |
1476d1bd | 1538 | NULL_RTX, NULL, hipart_different, |
92344ed0 | 1539 | PROB_EVEN); |
d5fa9cc9 | 1540 | |
92344ed0 | 1541 | do_compare_rtx_and_jump (res, const0_rtx, LT, false, mode, |
1476d1bd | 1542 | NULL_RTX, NULL, do_error, |
92344ed0 | 1543 | PROB_VERY_UNLIKELY); |
1304953e | 1544 | emit_jump (done_label); |
d5fa9cc9 | 1545 | |
1304953e JJ |
1546 | emit_label (hipart_different); |
1547 | ||
92344ed0 | 1548 | do_compare_rtx_and_jump (res, const0_rtx, GE, false, mode, |
1476d1bd | 1549 | NULL_RTX, NULL, do_error, |
92344ed0 | 1550 | PROB_VERY_UNLIKELY); |
1304953e JJ |
1551 | emit_jump (done_label); |
1552 | } | |
d5fa9cc9 JJ |
1553 | |
1554 | emit_label (do_overflow); | |
1555 | ||
1556 | /* Overflow, do full multiplication and fallthru into do_error. */ | |
1304953e JJ |
1557 | ops.op0 = make_tree (type, op0); |
1558 | ops.op1 = make_tree (type, op1); | |
d5fa9cc9 JJ |
1559 | tem = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL); |
1560 | emit_move_insn (res, tem); | |
1561 | } | |
31e071ae MP |
1562 | else |
1563 | { | |
1304953e | 1564 | gcc_assert (!is_ubsan); |
31e071ae | 1565 | ops.code = MULT_EXPR; |
1304953e | 1566 | ops.type = type; |
31e071ae MP |
1567 | res = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL); |
1568 | emit_jump (done_label); | |
1569 | } | |
1570 | } | |
1571 | ||
1304953e | 1572 | do_error_label: |
31e071ae | 1573 | emit_label (do_error); |
1304953e JJ |
1574 | if (is_ubsan) |
1575 | { | |
1576 | /* Expand the ubsan builtin call. */ | |
1577 | push_temp_slots (); | |
1578 | fn = ubsan_build_overflow_builtin (MULT_EXPR, loc, TREE_TYPE (arg0), | |
1579 | arg0, arg1); | |
1580 | expand_normal (fn); | |
1581 | pop_temp_slots (); | |
1582 | do_pending_stack_adjust (); | |
1583 | } | |
1584 | else if (lhs) | |
1585 | write_complex_part (target, const1_rtx, true); | |
31e071ae MP |
1586 | |
1587 | /* We're done. */ | |
1588 | emit_label (done_label); | |
1589 | ||
1304953e JJ |
1590 | /* u1 * u2 -> sr */ |
1591 | if (uns0_p && uns1_p && !unsr_p) | |
1592 | { | |
1593 | rtx_code_label *all_done_label = gen_label_rtx (); | |
92344ed0 | 1594 | do_compare_rtx_and_jump (res, const0_rtx, GE, false, mode, NULL_RTX, |
1476d1bd | 1595 | NULL, all_done_label, PROB_VERY_LIKELY); |
1304953e JJ |
1596 | write_complex_part (target, const1_rtx, true); |
1597 | emit_label (all_done_label); | |
1598 | } | |
1599 | ||
1600 | /* s1 * u2 -> sr */ | |
1601 | if (!uns0_p && uns1_p && !unsr_p && pos_neg1 == 3) | |
1602 | { | |
1603 | rtx_code_label *all_done_label = gen_label_rtx (); | |
1604 | rtx_code_label *set_noovf = gen_label_rtx (); | |
92344ed0 | 1605 | do_compare_rtx_and_jump (op1, const0_rtx, GE, false, mode, NULL_RTX, |
1476d1bd | 1606 | NULL, all_done_label, PROB_VERY_LIKELY); |
1304953e | 1607 | write_complex_part (target, const1_rtx, true); |
92344ed0 | 1608 | do_compare_rtx_and_jump (op0, const0_rtx, EQ, true, mode, NULL_RTX, |
1476d1bd | 1609 | NULL, set_noovf, PROB_VERY_LIKELY); |
92344ed0 | 1610 | do_compare_rtx_and_jump (op0, constm1_rtx, NE, true, mode, NULL_RTX, |
1476d1bd MM |
1611 | NULL, all_done_label, PROB_VERY_UNLIKELY); |
1612 | do_compare_rtx_and_jump (op1, res, NE, true, mode, NULL_RTX, NULL, | |
92344ed0 | 1613 | all_done_label, PROB_VERY_UNLIKELY); |
1304953e JJ |
1614 | emit_label (set_noovf); |
1615 | write_complex_part (target, const0_rtx, true); | |
1616 | emit_label (all_done_label); | |
1617 | } | |
1618 | ||
31e071ae | 1619 | if (lhs) |
1304953e JJ |
1620 | { |
1621 | if (is_ubsan) | |
5620052d | 1622 | expand_ubsan_result_store (target, res); |
1304953e JJ |
1623 | else |
1624 | expand_arith_overflow_result_store (lhs, target, mode, res); | |
1625 | } | |
31e071ae MP |
1626 | } |
1627 | ||
1628 | /* Expand UBSAN_CHECK_ADD call STMT. */ | |
1629 | ||
1630 | static void | |
538dd0b7 | 1631 | expand_UBSAN_CHECK_ADD (gcall *stmt) |
31e071ae | 1632 | { |
1304953e JJ |
1633 | location_t loc = gimple_location (stmt); |
1634 | tree lhs = gimple_call_lhs (stmt); | |
1635 | tree arg0 = gimple_call_arg (stmt, 0); | |
1636 | tree arg1 = gimple_call_arg (stmt, 1); | |
1637 | expand_addsub_overflow (loc, PLUS_EXPR, lhs, arg0, arg1, | |
1638 | false, false, false, true); | |
31e071ae MP |
1639 | } |
1640 | ||
1641 | /* Expand UBSAN_CHECK_SUB call STMT. */ | |
1642 | ||
1643 | static void | |
538dd0b7 | 1644 | expand_UBSAN_CHECK_SUB (gcall *stmt) |
31e071ae | 1645 | { |
1304953e JJ |
1646 | location_t loc = gimple_location (stmt); |
1647 | tree lhs = gimple_call_lhs (stmt); | |
1648 | tree arg0 = gimple_call_arg (stmt, 0); | |
1649 | tree arg1 = gimple_call_arg (stmt, 1); | |
1650 | if (integer_zerop (arg0)) | |
1651 | expand_neg_overflow (loc, lhs, arg1, true); | |
31e071ae | 1652 | else |
1304953e JJ |
1653 | expand_addsub_overflow (loc, MINUS_EXPR, lhs, arg0, arg1, |
1654 | false, false, false, true); | |
31e071ae MP |
1655 | } |
1656 | ||
1657 | /* Expand UBSAN_CHECK_MUL call STMT. */ | |
1658 | ||
1659 | static void | |
538dd0b7 | 1660 | expand_UBSAN_CHECK_MUL (gcall *stmt) |
31e071ae | 1661 | { |
1304953e JJ |
1662 | location_t loc = gimple_location (stmt); |
1663 | tree lhs = gimple_call_lhs (stmt); | |
1664 | tree arg0 = gimple_call_arg (stmt, 0); | |
1665 | tree arg1 = gimple_call_arg (stmt, 1); | |
1666 | expand_mul_overflow (loc, lhs, arg0, arg1, false, false, false, true); | |
1667 | } | |
1668 | ||
1669 | /* Helper function for {ADD,SUB,MUL}_OVERFLOW call stmt expansion. */ | |
1670 | ||
1671 | static void | |
1672 | expand_arith_overflow (enum tree_code code, gimple stmt) | |
1673 | { | |
1674 | tree lhs = gimple_call_lhs (stmt); | |
1675 | if (lhs == NULL_TREE) | |
1676 | return; | |
1677 | tree arg0 = gimple_call_arg (stmt, 0); | |
1678 | tree arg1 = gimple_call_arg (stmt, 1); | |
1679 | tree type = TREE_TYPE (TREE_TYPE (lhs)); | |
1680 | int uns0_p = TYPE_UNSIGNED (TREE_TYPE (arg0)); | |
1681 | int uns1_p = TYPE_UNSIGNED (TREE_TYPE (arg1)); | |
1682 | int unsr_p = TYPE_UNSIGNED (type); | |
1683 | int prec0 = TYPE_PRECISION (TREE_TYPE (arg0)); | |
1684 | int prec1 = TYPE_PRECISION (TREE_TYPE (arg1)); | |
1685 | int precres = TYPE_PRECISION (type); | |
1686 | location_t loc = gimple_location (stmt); | |
1687 | if (!uns0_p && get_range_pos_neg (arg0) == 1) | |
1688 | uns0_p = true; | |
1689 | if (!uns1_p && get_range_pos_neg (arg1) == 1) | |
1690 | uns1_p = true; | |
1691 | int pr = get_min_precision (arg0, uns0_p ? UNSIGNED : SIGNED); | |
1692 | prec0 = MIN (prec0, pr); | |
1693 | pr = get_min_precision (arg1, uns1_p ? UNSIGNED : SIGNED); | |
1694 | prec1 = MIN (prec1, pr); | |
1695 | ||
1696 | /* If uns0_p && uns1_p, precop is minimum needed precision | |
1697 | of unsigned type to hold the exact result, otherwise | |
1698 | precop is minimum needed precision of signed type to | |
1699 | hold the exact result. */ | |
1700 | int precop; | |
1701 | if (code == MULT_EXPR) | |
1702 | precop = prec0 + prec1 + (uns0_p != uns1_p); | |
1703 | else | |
1704 | { | |
1705 | if (uns0_p == uns1_p) | |
1706 | precop = MAX (prec0, prec1) + 1; | |
1707 | else if (uns0_p) | |
1708 | precop = MAX (prec0 + 1, prec1) + 1; | |
1709 | else | |
1710 | precop = MAX (prec0, prec1 + 1) + 1; | |
1711 | } | |
1712 | int orig_precres = precres; | |
1713 | ||
1714 | do | |
1715 | { | |
1716 | if ((uns0_p && uns1_p) | |
1717 | ? ((precop + !unsr_p) <= precres | |
1718 | /* u1 - u2 -> ur can overflow, no matter what precision | |
1719 | the result has. */ | |
1720 | && (code != MINUS_EXPR || !unsr_p)) | |
1721 | : (!unsr_p && precop <= precres)) | |
1722 | { | |
1723 | /* The infinity precision result will always fit into result. */ | |
1724 | rtx target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE); | |
1725 | write_complex_part (target, const0_rtx, true); | |
1726 | enum machine_mode mode = TYPE_MODE (type); | |
1727 | struct separate_ops ops; | |
1728 | ops.code = code; | |
1729 | ops.type = type; | |
1730 | ops.op0 = fold_convert_loc (loc, type, arg0); | |
1731 | ops.op1 = fold_convert_loc (loc, type, arg1); | |
1732 | ops.op2 = NULL_TREE; | |
1733 | ops.location = loc; | |
1734 | rtx tem = expand_expr_real_2 (&ops, NULL_RTX, mode, EXPAND_NORMAL); | |
1735 | expand_arith_overflow_result_store (lhs, target, mode, tem); | |
1736 | return; | |
1737 | } | |
1738 | ||
1739 | #ifdef WORD_REGISTER_OPERATIONS | |
1740 | /* For sub-word operations, if target doesn't have them, start | |
1741 | with precres widening right away, otherwise do it only | |
1742 | if the most simple cases can't be used. */ | |
1743 | if (orig_precres == precres && precres < BITS_PER_WORD) | |
1744 | ; | |
1745 | else | |
1746 | #endif | |
1747 | if ((uns0_p && uns1_p && unsr_p && prec0 <= precres && prec1 <= precres) | |
1748 | || ((!uns0_p || !uns1_p) && !unsr_p | |
1749 | && prec0 + uns0_p <= precres | |
1750 | && prec1 + uns1_p <= precres)) | |
1751 | { | |
1752 | arg0 = fold_convert_loc (loc, type, arg0); | |
1753 | arg1 = fold_convert_loc (loc, type, arg1); | |
1754 | switch (code) | |
1755 | { | |
1756 | case MINUS_EXPR: | |
1757 | if (integer_zerop (arg0) && !unsr_p) | |
1758 | expand_neg_overflow (loc, lhs, arg1, false); | |
1759 | /* FALLTHRU */ | |
1760 | case PLUS_EXPR: | |
1761 | expand_addsub_overflow (loc, code, lhs, arg0, arg1, | |
1762 | unsr_p, unsr_p, unsr_p, false); | |
1763 | return; | |
1764 | case MULT_EXPR: | |
1765 | expand_mul_overflow (loc, lhs, arg0, arg1, | |
1766 | unsr_p, unsr_p, unsr_p, false); | |
1767 | return; | |
1768 | default: | |
1769 | gcc_unreachable (); | |
1770 | } | |
1771 | } | |
1772 | ||
1773 | /* For sub-word operations, retry with a wider type first. */ | |
1774 | if (orig_precres == precres && precop <= BITS_PER_WORD) | |
1775 | { | |
1776 | #ifdef WORD_REGISTER_OPERATIONS | |
1777 | int p = BITS_PER_WORD; | |
1778 | #else | |
1779 | int p = precop; | |
1780 | #endif | |
1781 | enum machine_mode m = smallest_mode_for_size (p, MODE_INT); | |
1782 | tree optype = build_nonstandard_integer_type (GET_MODE_PRECISION (m), | |
1783 | uns0_p && uns1_p | |
1784 | && unsr_p); | |
1785 | p = TYPE_PRECISION (optype); | |
1786 | if (p > precres) | |
1787 | { | |
1788 | precres = p; | |
1789 | unsr_p = TYPE_UNSIGNED (optype); | |
1790 | type = optype; | |
1791 | continue; | |
1792 | } | |
1793 | } | |
1794 | ||
1795 | if (prec0 <= precres && prec1 <= precres) | |
1796 | { | |
1797 | tree types[2]; | |
1798 | if (unsr_p) | |
1799 | { | |
1800 | types[0] = build_nonstandard_integer_type (precres, 0); | |
1801 | types[1] = type; | |
1802 | } | |
1803 | else | |
1804 | { | |
1805 | types[0] = type; | |
1806 | types[1] = build_nonstandard_integer_type (precres, 1); | |
1807 | } | |
1808 | arg0 = fold_convert_loc (loc, types[uns0_p], arg0); | |
1809 | arg1 = fold_convert_loc (loc, types[uns1_p], arg1); | |
1810 | if (code != MULT_EXPR) | |
1811 | expand_addsub_overflow (loc, code, lhs, arg0, arg1, unsr_p, | |
1812 | uns0_p, uns1_p, false); | |
1813 | else | |
1814 | expand_mul_overflow (loc, lhs, arg0, arg1, unsr_p, | |
1815 | uns0_p, uns1_p, false); | |
1816 | return; | |
1817 | } | |
1818 | ||
1819 | /* Retry with a wider type. */ | |
1820 | if (orig_precres == precres) | |
1821 | { | |
1822 | int p = MAX (prec0, prec1); | |
1823 | enum machine_mode m = smallest_mode_for_size (p, MODE_INT); | |
1824 | tree optype = build_nonstandard_integer_type (GET_MODE_PRECISION (m), | |
1825 | uns0_p && uns1_p | |
1826 | && unsr_p); | |
1827 | p = TYPE_PRECISION (optype); | |
1828 | if (p > precres) | |
1829 | { | |
1830 | precres = p; | |
1831 | unsr_p = TYPE_UNSIGNED (optype); | |
1832 | type = optype; | |
1833 | continue; | |
1834 | } | |
1835 | } | |
1836 | ||
1837 | gcc_unreachable (); | |
1838 | } | |
1839 | while (1); | |
1840 | } | |
1841 | ||
1842 | /* Expand ADD_OVERFLOW STMT. */ | |
1843 | ||
1844 | static void | |
538dd0b7 | 1845 | expand_ADD_OVERFLOW (gcall *stmt) |
1304953e JJ |
1846 | { |
1847 | expand_arith_overflow (PLUS_EXPR, stmt); | |
1848 | } | |
1849 | ||
1850 | /* Expand SUB_OVERFLOW STMT. */ | |
1851 | ||
1852 | static void | |
538dd0b7 | 1853 | expand_SUB_OVERFLOW (gcall *stmt) |
1304953e JJ |
1854 | { |
1855 | expand_arith_overflow (MINUS_EXPR, stmt); | |
1856 | } | |
1857 | ||
1858 | /* Expand MUL_OVERFLOW STMT. */ | |
1859 | ||
1860 | static void | |
538dd0b7 | 1861 | expand_MUL_OVERFLOW (gcall *stmt) |
1304953e JJ |
1862 | { |
1863 | expand_arith_overflow (MULT_EXPR, stmt); | |
31e071ae MP |
1864 | } |
1865 | ||
5ce9450f JJ |
1866 | /* This should get folded in tree-vectorizer.c. */ |
1867 | ||
1868 | static void | |
35228ac7 | 1869 | expand_LOOP_VECTORIZED (gcall *) |
5ce9450f JJ |
1870 | { |
1871 | gcc_unreachable (); | |
1872 | } | |
1873 | ||
1874 | static void | |
538dd0b7 | 1875 | expand_MASK_LOAD (gcall *stmt) |
5ce9450f JJ |
1876 | { |
1877 | struct expand_operand ops[3]; | |
1878 | tree type, lhs, rhs, maskt; | |
1879 | rtx mem, target, mask; | |
1880 | ||
1881 | maskt = gimple_call_arg (stmt, 2); | |
1882 | lhs = gimple_call_lhs (stmt); | |
8e91d222 JJ |
1883 | if (lhs == NULL_TREE) |
1884 | return; | |
5ce9450f JJ |
1885 | type = TREE_TYPE (lhs); |
1886 | rhs = fold_build2 (MEM_REF, type, gimple_call_arg (stmt, 0), | |
1887 | gimple_call_arg (stmt, 1)); | |
1888 | ||
1889 | mem = expand_expr (rhs, NULL_RTX, VOIDmode, EXPAND_WRITE); | |
1890 | gcc_assert (MEM_P (mem)); | |
1891 | mask = expand_normal (maskt); | |
1892 | target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE); | |
1893 | create_output_operand (&ops[0], target, TYPE_MODE (type)); | |
1894 | create_fixed_operand (&ops[1], mem); | |
1895 | create_input_operand (&ops[2], mask, TYPE_MODE (TREE_TYPE (maskt))); | |
1896 | expand_insn (optab_handler (maskload_optab, TYPE_MODE (type)), 3, ops); | |
1897 | } | |
1898 | ||
1899 | static void | |
538dd0b7 | 1900 | expand_MASK_STORE (gcall *stmt) |
5ce9450f JJ |
1901 | { |
1902 | struct expand_operand ops[3]; | |
1903 | tree type, lhs, rhs, maskt; | |
1904 | rtx mem, reg, mask; | |
1905 | ||
1906 | maskt = gimple_call_arg (stmt, 2); | |
1907 | rhs = gimple_call_arg (stmt, 3); | |
1908 | type = TREE_TYPE (rhs); | |
1909 | lhs = fold_build2 (MEM_REF, type, gimple_call_arg (stmt, 0), | |
1910 | gimple_call_arg (stmt, 1)); | |
1911 | ||
1912 | mem = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE); | |
1913 | gcc_assert (MEM_P (mem)); | |
1914 | mask = expand_normal (maskt); | |
1915 | reg = expand_normal (rhs); | |
1916 | create_fixed_operand (&ops[0], mem); | |
1917 | create_input_operand (&ops[1], reg, TYPE_MODE (type)); | |
1918 | create_input_operand (&ops[2], mask, TYPE_MODE (TREE_TYPE (maskt))); | |
1919 | expand_insn (optab_handler (maskstore_optab, TYPE_MODE (type)), 3, ops); | |
1920 | } | |
1921 | ||
09b22f48 | 1922 | static void |
538dd0b7 | 1923 | expand_ABNORMAL_DISPATCHER (gcall *) |
09b22f48 JJ |
1924 | { |
1925 | } | |
1926 | ||
ed9c79e1 | 1927 | static void |
538dd0b7 | 1928 | expand_BUILTIN_EXPECT (gcall *stmt) |
ed9c79e1 JJ |
1929 | { |
1930 | /* When guessing was done, the hints should be already stripped away. */ | |
1931 | gcc_assert (!flag_guess_branch_prob || optimize == 0 || seen_error ()); | |
1932 | ||
1933 | rtx target; | |
1934 | tree lhs = gimple_call_lhs (stmt); | |
1935 | if (lhs) | |
1936 | target = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE); | |
1937 | else | |
1938 | target = const0_rtx; | |
1939 | rtx val = expand_expr (gimple_call_arg (stmt, 0), target, VOIDmode, EXPAND_NORMAL); | |
1940 | if (lhs && val != target) | |
1941 | emit_move_insn (target, val); | |
1942 | } | |
1943 | ||
f8e89441 TV |
1944 | /* IFN_VA_ARG is supposed to be expanded at pass_stdarg. So this dummy function |
1945 | should never be called. */ | |
1946 | ||
1947 | static void | |
1948 | expand_VA_ARG (gcall *stmt ATTRIBUTE_UNUSED) | |
1949 | { | |
1950 | gcc_unreachable (); | |
1951 | } | |
1952 | ||
25583c4f RS |
1953 | /* Routines to expand each internal function, indexed by function number. |
1954 | Each routine has the prototype: | |
1955 | ||
538dd0b7 | 1956 | expand_<NAME> (gcall *stmt) |
25583c4f RS |
1957 | |
1958 | where STMT is the statement that performs the call. */ | |
538dd0b7 | 1959 | static void (*const internal_fn_expanders[]) (gcall *) = { |
b78475cf | 1960 | #define DEF_INTERNAL_FN(CODE, FLAGS, FNSPEC) expand_##CODE, |
25583c4f RS |
1961 | #include "internal-fn.def" |
1962 | #undef DEF_INTERNAL_FN | |
1963 | 0 | |
1964 | }; | |
1965 | ||
1966 | /* Expand STMT, which is a call to internal function FN. */ | |
1967 | ||
1968 | void | |
538dd0b7 | 1969 | expand_internal_call (gcall *stmt) |
25583c4f RS |
1970 | { |
1971 | internal_fn_expanders[(int) gimple_call_internal_fn (stmt)] (stmt); | |
1972 | } |