]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/expmed.c
Widening optab cleanup
[thirdparty/gcc.git] / gcc / expmed.c
1 /* Medium-level subroutines: convert bit-field store and extract
2 and shifts, multiplies and divides to rtl instructions.
3 Copyright (C) 1987-2017 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "backend.h"
26 #include "target.h"
27 #include "rtl.h"
28 #include "tree.h"
29 #include "predict.h"
30 #include "memmodel.h"
31 #include "tm_p.h"
32 #include "expmed.h"
33 #include "optabs.h"
34 #include "regs.h"
35 #include "emit-rtl.h"
36 #include "diagnostic-core.h"
37 #include "fold-const.h"
38 #include "stor-layout.h"
39 #include "dojump.h"
40 #include "explow.h"
41 #include "expr.h"
42 #include "langhooks.h"
43
44 struct target_expmed default_target_expmed;
45 #if SWITCHABLE_TARGET
46 struct target_expmed *this_target_expmed = &default_target_expmed;
47 #endif
48
49 static void store_fixed_bit_field (rtx, opt_scalar_int_mode,
50 unsigned HOST_WIDE_INT,
51 unsigned HOST_WIDE_INT,
52 unsigned HOST_WIDE_INT,
53 unsigned HOST_WIDE_INT,
54 rtx, scalar_int_mode, bool);
55 static void store_fixed_bit_field_1 (rtx, scalar_int_mode,
56 unsigned HOST_WIDE_INT,
57 unsigned HOST_WIDE_INT,
58 rtx, scalar_int_mode, bool);
59 static void store_split_bit_field (rtx, opt_scalar_int_mode,
60 unsigned HOST_WIDE_INT,
61 unsigned HOST_WIDE_INT,
62 unsigned HOST_WIDE_INT,
63 unsigned HOST_WIDE_INT,
64 rtx, scalar_int_mode, bool);
65 static rtx extract_fixed_bit_field (machine_mode, rtx, opt_scalar_int_mode,
66 unsigned HOST_WIDE_INT,
67 unsigned HOST_WIDE_INT, rtx, int, bool);
68 static rtx extract_fixed_bit_field_1 (machine_mode, rtx, scalar_int_mode,
69 unsigned HOST_WIDE_INT,
70 unsigned HOST_WIDE_INT, rtx, int, bool);
71 static rtx lshift_value (machine_mode, unsigned HOST_WIDE_INT, int);
72 static rtx extract_split_bit_field (rtx, opt_scalar_int_mode,
73 unsigned HOST_WIDE_INT,
74 unsigned HOST_WIDE_INT, int, bool);
75 static void do_cmp_and_jump (rtx, rtx, enum rtx_code, machine_mode, rtx_code_label *);
76 static rtx expand_smod_pow2 (scalar_int_mode, rtx, HOST_WIDE_INT);
77 static rtx expand_sdiv_pow2 (scalar_int_mode, rtx, HOST_WIDE_INT);
78
79 /* Return a constant integer mask value of mode MODE with BITSIZE ones
80 followed by BITPOS zeros, or the complement of that if COMPLEMENT.
81 The mask is truncated if necessary to the width of mode MODE. The
82 mask is zero-extended if BITSIZE+BITPOS is too small for MODE. */
83
84 static inline rtx
85 mask_rtx (scalar_int_mode mode, int bitpos, int bitsize, bool complement)
86 {
87 return immed_wide_int_const
88 (wi::shifted_mask (bitpos, bitsize, complement,
89 GET_MODE_PRECISION (mode)), mode);
90 }
91
92 /* Test whether a value is zero of a power of two. */
93 #define EXACT_POWER_OF_2_OR_ZERO_P(x) \
94 (((x) & ((x) - HOST_WIDE_INT_1U)) == 0)
95
96 struct init_expmed_rtl
97 {
98 rtx reg;
99 rtx plus;
100 rtx neg;
101 rtx mult;
102 rtx sdiv;
103 rtx udiv;
104 rtx sdiv_32;
105 rtx smod_32;
106 rtx wide_mult;
107 rtx wide_lshr;
108 rtx wide_trunc;
109 rtx shift;
110 rtx shift_mult;
111 rtx shift_add;
112 rtx shift_sub0;
113 rtx shift_sub1;
114 rtx zext;
115 rtx trunc;
116
117 rtx pow2[MAX_BITS_PER_WORD];
118 rtx cint[MAX_BITS_PER_WORD];
119 };
120
121 static void
122 init_expmed_one_conv (struct init_expmed_rtl *all, scalar_int_mode to_mode,
123 scalar_int_mode from_mode, bool speed)
124 {
125 int to_size, from_size;
126 rtx which;
127
128 to_size = GET_MODE_PRECISION (to_mode);
129 from_size = GET_MODE_PRECISION (from_mode);
130
131 /* Most partial integers have a precision less than the "full"
132 integer it requires for storage. In case one doesn't, for
133 comparison purposes here, reduce the bit size by one in that
134 case. */
135 if (GET_MODE_CLASS (to_mode) == MODE_PARTIAL_INT
136 && pow2p_hwi (to_size))
137 to_size --;
138 if (GET_MODE_CLASS (from_mode) == MODE_PARTIAL_INT
139 && pow2p_hwi (from_size))
140 from_size --;
141
142 /* Assume cost of zero-extend and sign-extend is the same. */
143 which = (to_size < from_size ? all->trunc : all->zext);
144
145 PUT_MODE (all->reg, from_mode);
146 set_convert_cost (to_mode, from_mode, speed,
147 set_src_cost (which, to_mode, speed));
148 }
149
150 static void
151 init_expmed_one_mode (struct init_expmed_rtl *all,
152 machine_mode mode, int speed)
153 {
154 int m, n, mode_bitsize;
155 machine_mode mode_from;
156
157 mode_bitsize = GET_MODE_UNIT_BITSIZE (mode);
158
159 PUT_MODE (all->reg, mode);
160 PUT_MODE (all->plus, mode);
161 PUT_MODE (all->neg, mode);
162 PUT_MODE (all->mult, mode);
163 PUT_MODE (all->sdiv, mode);
164 PUT_MODE (all->udiv, mode);
165 PUT_MODE (all->sdiv_32, mode);
166 PUT_MODE (all->smod_32, mode);
167 PUT_MODE (all->wide_trunc, mode);
168 PUT_MODE (all->shift, mode);
169 PUT_MODE (all->shift_mult, mode);
170 PUT_MODE (all->shift_add, mode);
171 PUT_MODE (all->shift_sub0, mode);
172 PUT_MODE (all->shift_sub1, mode);
173 PUT_MODE (all->zext, mode);
174 PUT_MODE (all->trunc, mode);
175
176 set_add_cost (speed, mode, set_src_cost (all->plus, mode, speed));
177 set_neg_cost (speed, mode, set_src_cost (all->neg, mode, speed));
178 set_mul_cost (speed, mode, set_src_cost (all->mult, mode, speed));
179 set_sdiv_cost (speed, mode, set_src_cost (all->sdiv, mode, speed));
180 set_udiv_cost (speed, mode, set_src_cost (all->udiv, mode, speed));
181
182 set_sdiv_pow2_cheap (speed, mode, (set_src_cost (all->sdiv_32, mode, speed)
183 <= 2 * add_cost (speed, mode)));
184 set_smod_pow2_cheap (speed, mode, (set_src_cost (all->smod_32, mode, speed)
185 <= 4 * add_cost (speed, mode)));
186
187 set_shift_cost (speed, mode, 0, 0);
188 {
189 int cost = add_cost (speed, mode);
190 set_shiftadd_cost (speed, mode, 0, cost);
191 set_shiftsub0_cost (speed, mode, 0, cost);
192 set_shiftsub1_cost (speed, mode, 0, cost);
193 }
194
195 n = MIN (MAX_BITS_PER_WORD, mode_bitsize);
196 for (m = 1; m < n; m++)
197 {
198 XEXP (all->shift, 1) = all->cint[m];
199 XEXP (all->shift_mult, 1) = all->pow2[m];
200
201 set_shift_cost (speed, mode, m, set_src_cost (all->shift, mode, speed));
202 set_shiftadd_cost (speed, mode, m, set_src_cost (all->shift_add, mode,
203 speed));
204 set_shiftsub0_cost (speed, mode, m, set_src_cost (all->shift_sub0, mode,
205 speed));
206 set_shiftsub1_cost (speed, mode, m, set_src_cost (all->shift_sub1, mode,
207 speed));
208 }
209
210 scalar_int_mode int_mode_to;
211 if (is_a <scalar_int_mode> (mode, &int_mode_to))
212 {
213 for (mode_from = MIN_MODE_INT; mode_from <= MAX_MODE_INT;
214 mode_from = (machine_mode)(mode_from + 1))
215 init_expmed_one_conv (all, int_mode_to,
216 as_a <scalar_int_mode> (mode_from), speed);
217
218 scalar_int_mode wider_mode;
219 if (GET_MODE_CLASS (int_mode_to) == MODE_INT
220 && GET_MODE_WIDER_MODE (int_mode_to).exists (&wider_mode))
221 {
222 PUT_MODE (all->zext, wider_mode);
223 PUT_MODE (all->wide_mult, wider_mode);
224 PUT_MODE (all->wide_lshr, wider_mode);
225 XEXP (all->wide_lshr, 1) = GEN_INT (mode_bitsize);
226
227 set_mul_widen_cost (speed, wider_mode,
228 set_src_cost (all->wide_mult, wider_mode, speed));
229 set_mul_highpart_cost (speed, int_mode_to,
230 set_src_cost (all->wide_trunc,
231 int_mode_to, speed));
232 }
233 }
234 }
235
236 void
237 init_expmed (void)
238 {
239 struct init_expmed_rtl all;
240 machine_mode mode = QImode;
241 int m, speed;
242
243 memset (&all, 0, sizeof all);
244 for (m = 1; m < MAX_BITS_PER_WORD; m++)
245 {
246 all.pow2[m] = GEN_INT (HOST_WIDE_INT_1 << m);
247 all.cint[m] = GEN_INT (m);
248 }
249
250 /* Avoid using hard regs in ways which may be unsupported. */
251 all.reg = gen_raw_REG (mode, LAST_VIRTUAL_REGISTER + 1);
252 all.plus = gen_rtx_PLUS (mode, all.reg, all.reg);
253 all.neg = gen_rtx_NEG (mode, all.reg);
254 all.mult = gen_rtx_MULT (mode, all.reg, all.reg);
255 all.sdiv = gen_rtx_DIV (mode, all.reg, all.reg);
256 all.udiv = gen_rtx_UDIV (mode, all.reg, all.reg);
257 all.sdiv_32 = gen_rtx_DIV (mode, all.reg, all.pow2[5]);
258 all.smod_32 = gen_rtx_MOD (mode, all.reg, all.pow2[5]);
259 all.zext = gen_rtx_ZERO_EXTEND (mode, all.reg);
260 all.wide_mult = gen_rtx_MULT (mode, all.zext, all.zext);
261 all.wide_lshr = gen_rtx_LSHIFTRT (mode, all.wide_mult, all.reg);
262 all.wide_trunc = gen_rtx_TRUNCATE (mode, all.wide_lshr);
263 all.shift = gen_rtx_ASHIFT (mode, all.reg, all.reg);
264 all.shift_mult = gen_rtx_MULT (mode, all.reg, all.reg);
265 all.shift_add = gen_rtx_PLUS (mode, all.shift_mult, all.reg);
266 all.shift_sub0 = gen_rtx_MINUS (mode, all.shift_mult, all.reg);
267 all.shift_sub1 = gen_rtx_MINUS (mode, all.reg, all.shift_mult);
268 all.trunc = gen_rtx_TRUNCATE (mode, all.reg);
269
270 for (speed = 0; speed < 2; speed++)
271 {
272 crtl->maybe_hot_insn_p = speed;
273 set_zero_cost (speed, set_src_cost (const0_rtx, mode, speed));
274
275 for (mode = MIN_MODE_INT; mode <= MAX_MODE_INT;
276 mode = (machine_mode)(mode + 1))
277 init_expmed_one_mode (&all, mode, speed);
278
279 if (MIN_MODE_PARTIAL_INT != VOIDmode)
280 for (mode = MIN_MODE_PARTIAL_INT; mode <= MAX_MODE_PARTIAL_INT;
281 mode = (machine_mode)(mode + 1))
282 init_expmed_one_mode (&all, mode, speed);
283
284 if (MIN_MODE_VECTOR_INT != VOIDmode)
285 for (mode = MIN_MODE_VECTOR_INT; mode <= MAX_MODE_VECTOR_INT;
286 mode = (machine_mode)(mode + 1))
287 init_expmed_one_mode (&all, mode, speed);
288 }
289
290 if (alg_hash_used_p ())
291 {
292 struct alg_hash_entry *p = alg_hash_entry_ptr (0);
293 memset (p, 0, sizeof (*p) * NUM_ALG_HASH_ENTRIES);
294 }
295 else
296 set_alg_hash_used_p (true);
297 default_rtl_profile ();
298
299 ggc_free (all.trunc);
300 ggc_free (all.shift_sub1);
301 ggc_free (all.shift_sub0);
302 ggc_free (all.shift_add);
303 ggc_free (all.shift_mult);
304 ggc_free (all.shift);
305 ggc_free (all.wide_trunc);
306 ggc_free (all.wide_lshr);
307 ggc_free (all.wide_mult);
308 ggc_free (all.zext);
309 ggc_free (all.smod_32);
310 ggc_free (all.sdiv_32);
311 ggc_free (all.udiv);
312 ggc_free (all.sdiv);
313 ggc_free (all.mult);
314 ggc_free (all.neg);
315 ggc_free (all.plus);
316 ggc_free (all.reg);
317 }
318
319 /* Return an rtx representing minus the value of X.
320 MODE is the intended mode of the result,
321 useful if X is a CONST_INT. */
322
323 rtx
324 negate_rtx (machine_mode mode, rtx x)
325 {
326 rtx result = simplify_unary_operation (NEG, mode, x, mode);
327
328 if (result == 0)
329 result = expand_unop (mode, neg_optab, x, NULL_RTX, 0);
330
331 return result;
332 }
333
334 /* Whether reverse storage order is supported on the target. */
335 static int reverse_storage_order_supported = -1;
336
337 /* Check whether reverse storage order is supported on the target. */
338
339 static void
340 check_reverse_storage_order_support (void)
341 {
342 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
343 {
344 reverse_storage_order_supported = 0;
345 sorry ("reverse scalar storage order");
346 }
347 else
348 reverse_storage_order_supported = 1;
349 }
350
351 /* Whether reverse FP storage order is supported on the target. */
352 static int reverse_float_storage_order_supported = -1;
353
354 /* Check whether reverse FP storage order is supported on the target. */
355
356 static void
357 check_reverse_float_storage_order_support (void)
358 {
359 if (FLOAT_WORDS_BIG_ENDIAN != WORDS_BIG_ENDIAN)
360 {
361 reverse_float_storage_order_supported = 0;
362 sorry ("reverse floating-point scalar storage order");
363 }
364 else
365 reverse_float_storage_order_supported = 1;
366 }
367
368 /* Return an rtx representing value of X with reverse storage order.
369 MODE is the intended mode of the result,
370 useful if X is a CONST_INT. */
371
372 rtx
373 flip_storage_order (machine_mode mode, rtx x)
374 {
375 scalar_int_mode int_mode;
376 rtx result;
377
378 if (mode == QImode)
379 return x;
380
381 if (COMPLEX_MODE_P (mode))
382 {
383 rtx real = read_complex_part (x, false);
384 rtx imag = read_complex_part (x, true);
385
386 real = flip_storage_order (GET_MODE_INNER (mode), real);
387 imag = flip_storage_order (GET_MODE_INNER (mode), imag);
388
389 return gen_rtx_CONCAT (mode, real, imag);
390 }
391
392 if (__builtin_expect (reverse_storage_order_supported < 0, 0))
393 check_reverse_storage_order_support ();
394
395 if (!is_a <scalar_int_mode> (mode, &int_mode))
396 {
397 if (FLOAT_MODE_P (mode)
398 && __builtin_expect (reverse_float_storage_order_supported < 0, 0))
399 check_reverse_float_storage_order_support ();
400
401 if (!int_mode_for_size (GET_MODE_PRECISION (mode), 0).exists (&int_mode))
402 {
403 sorry ("reverse storage order for %smode", GET_MODE_NAME (mode));
404 return x;
405 }
406 x = gen_lowpart (int_mode, x);
407 }
408
409 result = simplify_unary_operation (BSWAP, int_mode, x, int_mode);
410 if (result == 0)
411 result = expand_unop (int_mode, bswap_optab, x, NULL_RTX, 1);
412
413 if (int_mode != mode)
414 result = gen_lowpart (mode, result);
415
416 return result;
417 }
418
419 /* If MODE is set, adjust bitfield memory MEM so that it points to the
420 first unit of mode MODE that contains a bitfield of size BITSIZE at
421 bit position BITNUM. If MODE is not set, return a BLKmode reference
422 to every byte in the bitfield. Set *NEW_BITNUM to the bit position
423 of the field within the new memory. */
424
425 static rtx
426 narrow_bit_field_mem (rtx mem, opt_scalar_int_mode mode,
427 unsigned HOST_WIDE_INT bitsize,
428 unsigned HOST_WIDE_INT bitnum,
429 unsigned HOST_WIDE_INT *new_bitnum)
430 {
431 scalar_int_mode imode;
432 if (mode.exists (&imode))
433 {
434 unsigned int unit = GET_MODE_BITSIZE (imode);
435 *new_bitnum = bitnum % unit;
436 HOST_WIDE_INT offset = (bitnum - *new_bitnum) / BITS_PER_UNIT;
437 return adjust_bitfield_address (mem, imode, offset);
438 }
439 else
440 {
441 *new_bitnum = bitnum % BITS_PER_UNIT;
442 HOST_WIDE_INT offset = bitnum / BITS_PER_UNIT;
443 HOST_WIDE_INT size = ((*new_bitnum + bitsize + BITS_PER_UNIT - 1)
444 / BITS_PER_UNIT);
445 return adjust_bitfield_address_size (mem, BLKmode, offset, size);
446 }
447 }
448
449 /* The caller wants to perform insertion or extraction PATTERN on a
450 bitfield of size BITSIZE at BITNUM bits into memory operand OP0.
451 BITREGION_START and BITREGION_END are as for store_bit_field
452 and FIELDMODE is the natural mode of the field.
453
454 Search for a mode that is compatible with the memory access
455 restrictions and (where applicable) with a register insertion or
456 extraction. Return the new memory on success, storing the adjusted
457 bit position in *NEW_BITNUM. Return null otherwise. */
458
459 static rtx
460 adjust_bit_field_mem_for_reg (enum extraction_pattern pattern,
461 rtx op0, HOST_WIDE_INT bitsize,
462 HOST_WIDE_INT bitnum,
463 unsigned HOST_WIDE_INT bitregion_start,
464 unsigned HOST_WIDE_INT bitregion_end,
465 machine_mode fieldmode,
466 unsigned HOST_WIDE_INT *new_bitnum)
467 {
468 bit_field_mode_iterator iter (bitsize, bitnum, bitregion_start,
469 bitregion_end, MEM_ALIGN (op0),
470 MEM_VOLATILE_P (op0));
471 scalar_int_mode best_mode;
472 if (iter.next_mode (&best_mode))
473 {
474 /* We can use a memory in BEST_MODE. See whether this is true for
475 any wider modes. All other things being equal, we prefer to
476 use the widest mode possible because it tends to expose more
477 CSE opportunities. */
478 if (!iter.prefer_smaller_modes ())
479 {
480 /* Limit the search to the mode required by the corresponding
481 register insertion or extraction instruction, if any. */
482 scalar_int_mode limit_mode = word_mode;
483 extraction_insn insn;
484 if (get_best_reg_extraction_insn (&insn, pattern,
485 GET_MODE_BITSIZE (best_mode),
486 fieldmode))
487 limit_mode = insn.field_mode;
488
489 scalar_int_mode wider_mode;
490 while (iter.next_mode (&wider_mode)
491 && GET_MODE_SIZE (wider_mode) <= GET_MODE_SIZE (limit_mode))
492 best_mode = wider_mode;
493 }
494 return narrow_bit_field_mem (op0, best_mode, bitsize, bitnum,
495 new_bitnum);
496 }
497 return NULL_RTX;
498 }
499
500 /* Return true if a bitfield of size BITSIZE at bit number BITNUM within
501 a structure of mode STRUCT_MODE represents a lowpart subreg. The subreg
502 offset is then BITNUM / BITS_PER_UNIT. */
503
504 static bool
505 lowpart_bit_field_p (unsigned HOST_WIDE_INT bitnum,
506 unsigned HOST_WIDE_INT bitsize,
507 machine_mode struct_mode)
508 {
509 if (BYTES_BIG_ENDIAN)
510 return (bitnum % BITS_PER_UNIT == 0
511 && (bitnum + bitsize == GET_MODE_BITSIZE (struct_mode)
512 || (bitnum + bitsize) % BITS_PER_WORD == 0));
513 else
514 return bitnum % BITS_PER_WORD == 0;
515 }
516
517 /* Return true if -fstrict-volatile-bitfields applies to an access of OP0
518 containing BITSIZE bits starting at BITNUM, with field mode FIELDMODE.
519 Return false if the access would touch memory outside the range
520 BITREGION_START to BITREGION_END for conformance to the C++ memory
521 model. */
522
523 static bool
524 strict_volatile_bitfield_p (rtx op0, unsigned HOST_WIDE_INT bitsize,
525 unsigned HOST_WIDE_INT bitnum,
526 scalar_int_mode fieldmode,
527 unsigned HOST_WIDE_INT bitregion_start,
528 unsigned HOST_WIDE_INT bitregion_end)
529 {
530 unsigned HOST_WIDE_INT modesize = GET_MODE_BITSIZE (fieldmode);
531
532 /* -fstrict-volatile-bitfields must be enabled and we must have a
533 volatile MEM. */
534 if (!MEM_P (op0)
535 || !MEM_VOLATILE_P (op0)
536 || flag_strict_volatile_bitfields <= 0)
537 return false;
538
539 /* The bit size must not be larger than the field mode, and
540 the field mode must not be larger than a word. */
541 if (bitsize > modesize || modesize > BITS_PER_WORD)
542 return false;
543
544 /* Check for cases of unaligned fields that must be split. */
545 if (bitnum % modesize + bitsize > modesize)
546 return false;
547
548 /* The memory must be sufficiently aligned for a MODESIZE access.
549 This condition guarantees, that the memory access will not
550 touch anything after the end of the structure. */
551 if (MEM_ALIGN (op0) < modesize)
552 return false;
553
554 /* Check for cases where the C++ memory model applies. */
555 if (bitregion_end != 0
556 && (bitnum - bitnum % modesize < bitregion_start
557 || bitnum - bitnum % modesize + modesize - 1 > bitregion_end))
558 return false;
559
560 return true;
561 }
562
563 /* Return true if OP is a memory and if a bitfield of size BITSIZE at
564 bit number BITNUM can be treated as a simple value of mode MODE. */
565
566 static bool
567 simple_mem_bitfield_p (rtx op0, unsigned HOST_WIDE_INT bitsize,
568 unsigned HOST_WIDE_INT bitnum, machine_mode mode)
569 {
570 return (MEM_P (op0)
571 && bitnum % BITS_PER_UNIT == 0
572 && bitsize == GET_MODE_BITSIZE (mode)
573 && (!targetm.slow_unaligned_access (mode, MEM_ALIGN (op0))
574 || (bitnum % GET_MODE_ALIGNMENT (mode) == 0
575 && MEM_ALIGN (op0) >= GET_MODE_ALIGNMENT (mode))));
576 }
577 \f
578 /* Try to use instruction INSV to store VALUE into a field of OP0.
579 If OP0_MODE is defined, it is the mode of OP0, otherwise OP0 is a
580 BLKmode MEM. VALUE_MODE is the mode of VALUE. BITSIZE and BITNUM
581 are as for store_bit_field. */
582
583 static bool
584 store_bit_field_using_insv (const extraction_insn *insv, rtx op0,
585 opt_scalar_int_mode op0_mode,
586 unsigned HOST_WIDE_INT bitsize,
587 unsigned HOST_WIDE_INT bitnum,
588 rtx value, scalar_int_mode value_mode)
589 {
590 struct expand_operand ops[4];
591 rtx value1;
592 rtx xop0 = op0;
593 rtx_insn *last = get_last_insn ();
594 bool copy_back = false;
595
596 scalar_int_mode op_mode = insv->field_mode;
597 unsigned int unit = GET_MODE_BITSIZE (op_mode);
598 if (bitsize == 0 || bitsize > unit)
599 return false;
600
601 if (MEM_P (xop0))
602 /* Get a reference to the first byte of the field. */
603 xop0 = narrow_bit_field_mem (xop0, insv->struct_mode, bitsize, bitnum,
604 &bitnum);
605 else
606 {
607 /* Convert from counting within OP0 to counting in OP_MODE. */
608 if (BYTES_BIG_ENDIAN)
609 bitnum += unit - GET_MODE_BITSIZE (op0_mode.require ());
610
611 /* If xop0 is a register, we need it in OP_MODE
612 to make it acceptable to the format of insv. */
613 if (GET_CODE (xop0) == SUBREG)
614 /* We can't just change the mode, because this might clobber op0,
615 and we will need the original value of op0 if insv fails. */
616 xop0 = gen_rtx_SUBREG (op_mode, SUBREG_REG (xop0), SUBREG_BYTE (xop0));
617 if (REG_P (xop0) && GET_MODE (xop0) != op_mode)
618 xop0 = gen_lowpart_SUBREG (op_mode, xop0);
619 }
620
621 /* If the destination is a paradoxical subreg such that we need a
622 truncate to the inner mode, perform the insertion on a temporary and
623 truncate the result to the original destination. Note that we can't
624 just truncate the paradoxical subreg as (truncate:N (subreg:W (reg:N
625 X) 0)) is (reg:N X). */
626 if (GET_CODE (xop0) == SUBREG
627 && REG_P (SUBREG_REG (xop0))
628 && !TRULY_NOOP_TRUNCATION_MODES_P (GET_MODE (SUBREG_REG (xop0)),
629 op_mode))
630 {
631 rtx tem = gen_reg_rtx (op_mode);
632 emit_move_insn (tem, xop0);
633 xop0 = tem;
634 copy_back = true;
635 }
636
637 /* There are similar overflow check at the start of store_bit_field_1,
638 but that only check the situation where the field lies completely
639 outside the register, while there do have situation where the field
640 lies partialy in the register, we need to adjust bitsize for this
641 partial overflow situation. Without this fix, pr48335-2.c on big-endian
642 will broken on those arch support bit insert instruction, like arm, aarch64
643 etc. */
644 if (bitsize + bitnum > unit && bitnum < unit)
645 {
646 warning (OPT_Wextra, "write of %wu-bit data outside the bound of "
647 "destination object, data truncated into %wu-bit",
648 bitsize, unit - bitnum);
649 bitsize = unit - bitnum;
650 }
651
652 /* If BITS_BIG_ENDIAN is zero on a BYTES_BIG_ENDIAN machine, we count
653 "backwards" from the size of the unit we are inserting into.
654 Otherwise, we count bits from the most significant on a
655 BYTES/BITS_BIG_ENDIAN machine. */
656
657 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
658 bitnum = unit - bitsize - bitnum;
659
660 /* Convert VALUE to op_mode (which insv insn wants) in VALUE1. */
661 value1 = value;
662 if (value_mode != op_mode)
663 {
664 if (GET_MODE_BITSIZE (value_mode) >= bitsize)
665 {
666 rtx tmp;
667 /* Optimization: Don't bother really extending VALUE
668 if it has all the bits we will actually use. However,
669 if we must narrow it, be sure we do it correctly. */
670
671 if (GET_MODE_SIZE (value_mode) < GET_MODE_SIZE (op_mode))
672 {
673 tmp = simplify_subreg (op_mode, value1, value_mode, 0);
674 if (! tmp)
675 tmp = simplify_gen_subreg (op_mode,
676 force_reg (value_mode, value1),
677 value_mode, 0);
678 }
679 else
680 {
681 tmp = gen_lowpart_if_possible (op_mode, value1);
682 if (! tmp)
683 tmp = gen_lowpart (op_mode, force_reg (value_mode, value1));
684 }
685 value1 = tmp;
686 }
687 else if (CONST_INT_P (value))
688 value1 = gen_int_mode (INTVAL (value), op_mode);
689 else
690 /* Parse phase is supposed to make VALUE's data type
691 match that of the component reference, which is a type
692 at least as wide as the field; so VALUE should have
693 a mode that corresponds to that type. */
694 gcc_assert (CONSTANT_P (value));
695 }
696
697 create_fixed_operand (&ops[0], xop0);
698 create_integer_operand (&ops[1], bitsize);
699 create_integer_operand (&ops[2], bitnum);
700 create_input_operand (&ops[3], value1, op_mode);
701 if (maybe_expand_insn (insv->icode, 4, ops))
702 {
703 if (copy_back)
704 convert_move (op0, xop0, true);
705 return true;
706 }
707 delete_insns_since (last);
708 return false;
709 }
710
711 /* A subroutine of store_bit_field, with the same arguments. Return true
712 if the operation could be implemented.
713
714 If FALLBACK_P is true, fall back to store_fixed_bit_field if we have
715 no other way of implementing the operation. If FALLBACK_P is false,
716 return false instead. */
717
718 static bool
719 store_bit_field_1 (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
720 unsigned HOST_WIDE_INT bitnum,
721 unsigned HOST_WIDE_INT bitregion_start,
722 unsigned HOST_WIDE_INT bitregion_end,
723 machine_mode fieldmode,
724 rtx value, bool reverse, bool fallback_p)
725 {
726 rtx op0 = str_rtx;
727 rtx orig_value;
728
729 while (GET_CODE (op0) == SUBREG)
730 {
731 bitnum += subreg_memory_offset (op0) * BITS_PER_UNIT;
732 op0 = SUBREG_REG (op0);
733 }
734
735 /* No action is needed if the target is a register and if the field
736 lies completely outside that register. This can occur if the source
737 code contains an out-of-bounds access to a small array. */
738 if (REG_P (op0) && bitnum >= GET_MODE_BITSIZE (GET_MODE (op0)))
739 return true;
740
741 /* Use vec_set patterns for inserting parts of vectors whenever
742 available. */
743 machine_mode outermode = GET_MODE (op0);
744 scalar_mode innermode = GET_MODE_INNER (outermode);
745 if (VECTOR_MODE_P (outermode)
746 && !MEM_P (op0)
747 && optab_handler (vec_set_optab, outermode) != CODE_FOR_nothing
748 && fieldmode == innermode
749 && bitsize == GET_MODE_BITSIZE (innermode)
750 && !(bitnum % GET_MODE_BITSIZE (innermode)))
751 {
752 struct expand_operand ops[3];
753 enum insn_code icode = optab_handler (vec_set_optab, outermode);
754 int pos = bitnum / GET_MODE_BITSIZE (innermode);
755
756 create_fixed_operand (&ops[0], op0);
757 create_input_operand (&ops[1], value, innermode);
758 create_integer_operand (&ops[2], pos);
759 if (maybe_expand_insn (icode, 3, ops))
760 return true;
761 }
762
763 /* If the target is a register, overwriting the entire object, or storing
764 a full-word or multi-word field can be done with just a SUBREG. */
765 if (!MEM_P (op0)
766 && bitsize == GET_MODE_BITSIZE (fieldmode)
767 && ((bitsize == GET_MODE_BITSIZE (GET_MODE (op0)) && bitnum == 0)
768 || (bitsize % BITS_PER_WORD == 0 && bitnum % BITS_PER_WORD == 0)))
769 {
770 /* Use the subreg machinery either to narrow OP0 to the required
771 words or to cope with mode punning between equal-sized modes.
772 In the latter case, use subreg on the rhs side, not lhs. */
773 rtx sub;
774
775 if (bitsize == GET_MODE_BITSIZE (GET_MODE (op0)))
776 {
777 sub = simplify_gen_subreg (GET_MODE (op0), value, fieldmode, 0);
778 if (sub)
779 {
780 if (reverse)
781 sub = flip_storage_order (GET_MODE (op0), sub);
782 emit_move_insn (op0, sub);
783 return true;
784 }
785 }
786 else
787 {
788 sub = simplify_gen_subreg (fieldmode, op0, GET_MODE (op0),
789 bitnum / BITS_PER_UNIT);
790 if (sub)
791 {
792 if (reverse)
793 value = flip_storage_order (fieldmode, value);
794 emit_move_insn (sub, value);
795 return true;
796 }
797 }
798 }
799
800 /* If the target is memory, storing any naturally aligned field can be
801 done with a simple store. For targets that support fast unaligned
802 memory, any naturally sized, unit aligned field can be done directly. */
803 if (simple_mem_bitfield_p (op0, bitsize, bitnum, fieldmode))
804 {
805 op0 = adjust_bitfield_address (op0, fieldmode, bitnum / BITS_PER_UNIT);
806 if (reverse)
807 value = flip_storage_order (fieldmode, value);
808 emit_move_insn (op0, value);
809 return true;
810 }
811
812 /* Make sure we are playing with integral modes. Pun with subregs
813 if we aren't. This must come after the entire register case above,
814 since that case is valid for any mode. The following cases are only
815 valid for integral modes. */
816 opt_scalar_int_mode op0_mode = int_mode_for_mode (GET_MODE (op0));
817 scalar_int_mode imode;
818 if (!op0_mode.exists (&imode) || imode != GET_MODE (op0))
819 {
820 if (MEM_P (op0))
821 op0 = adjust_bitfield_address_size (op0, op0_mode.else_blk (),
822 0, MEM_SIZE (op0));
823 else
824 op0 = gen_lowpart (op0_mode.require (), op0);
825 }
826
827 /* Storing an lsb-aligned field in a register
828 can be done with a movstrict instruction. */
829
830 if (!MEM_P (op0)
831 && !reverse
832 && lowpart_bit_field_p (bitnum, bitsize, GET_MODE (op0))
833 && bitsize == GET_MODE_BITSIZE (fieldmode)
834 && optab_handler (movstrict_optab, fieldmode) != CODE_FOR_nothing)
835 {
836 struct expand_operand ops[2];
837 enum insn_code icode = optab_handler (movstrict_optab, fieldmode);
838 rtx arg0 = op0;
839 unsigned HOST_WIDE_INT subreg_off;
840
841 if (GET_CODE (arg0) == SUBREG)
842 {
843 /* Else we've got some float mode source being extracted into
844 a different float mode destination -- this combination of
845 subregs results in Severe Tire Damage. */
846 gcc_assert (GET_MODE (SUBREG_REG (arg0)) == fieldmode
847 || GET_MODE_CLASS (fieldmode) == MODE_INT
848 || GET_MODE_CLASS (fieldmode) == MODE_PARTIAL_INT);
849 arg0 = SUBREG_REG (arg0);
850 }
851
852 subreg_off = bitnum / BITS_PER_UNIT;
853 if (validate_subreg (fieldmode, GET_MODE (arg0), arg0, subreg_off))
854 {
855 arg0 = gen_rtx_SUBREG (fieldmode, arg0, subreg_off);
856
857 create_fixed_operand (&ops[0], arg0);
858 /* Shrink the source operand to FIELDMODE. */
859 create_convert_operand_to (&ops[1], value, fieldmode, false);
860 if (maybe_expand_insn (icode, 2, ops))
861 return true;
862 }
863 }
864
865 /* Handle fields bigger than a word. */
866
867 if (bitsize > BITS_PER_WORD)
868 {
869 /* Here we transfer the words of the field
870 in the order least significant first.
871 This is because the most significant word is the one which may
872 be less than full.
873 However, only do that if the value is not BLKmode. */
874
875 const bool backwards = WORDS_BIG_ENDIAN && fieldmode != BLKmode;
876 unsigned int nwords = (bitsize + (BITS_PER_WORD - 1)) / BITS_PER_WORD;
877 unsigned int i;
878 rtx_insn *last;
879
880 /* This is the mode we must force value to, so that there will be enough
881 subwords to extract. Note that fieldmode will often (always?) be
882 VOIDmode, because that is what store_field uses to indicate that this
883 is a bit field, but passing VOIDmode to operand_subword_force
884 is not allowed. */
885 fieldmode = GET_MODE (value);
886 if (fieldmode == VOIDmode)
887 fieldmode = smallest_int_mode_for_size (nwords * BITS_PER_WORD);
888
889 last = get_last_insn ();
890 for (i = 0; i < nwords; i++)
891 {
892 /* If I is 0, use the low-order word in both field and target;
893 if I is 1, use the next to lowest word; and so on. */
894 unsigned int wordnum = (backwards
895 ? GET_MODE_SIZE (fieldmode) / UNITS_PER_WORD
896 - i - 1
897 : i);
898 unsigned int bit_offset = (backwards ^ reverse
899 ? MAX ((int) bitsize - ((int) i + 1)
900 * BITS_PER_WORD,
901 0)
902 : (int) i * BITS_PER_WORD);
903 rtx value_word = operand_subword_force (value, wordnum, fieldmode);
904 unsigned HOST_WIDE_INT new_bitsize =
905 MIN (BITS_PER_WORD, bitsize - i * BITS_PER_WORD);
906
907 /* If the remaining chunk doesn't have full wordsize we have
908 to make sure that for big-endian machines the higher order
909 bits are used. */
910 if (new_bitsize < BITS_PER_WORD && BYTES_BIG_ENDIAN && !backwards)
911 value_word = simplify_expand_binop (word_mode, lshr_optab,
912 value_word,
913 GEN_INT (BITS_PER_WORD
914 - new_bitsize),
915 NULL_RTX, true,
916 OPTAB_LIB_WIDEN);
917
918 if (!store_bit_field_1 (op0, new_bitsize,
919 bitnum + bit_offset,
920 bitregion_start, bitregion_end,
921 word_mode,
922 value_word, reverse, fallback_p))
923 {
924 delete_insns_since (last);
925 return false;
926 }
927 }
928 return true;
929 }
930
931 /* If VALUE has a floating-point or complex mode, access it as an
932 integer of the corresponding size. This can occur on a machine
933 with 64 bit registers that uses SFmode for float. It can also
934 occur for unaligned float or complex fields. */
935 orig_value = value;
936 scalar_int_mode value_mode;
937 if (GET_MODE (value) == VOIDmode)
938 /* By this point we've dealt with values that are bigger than a word,
939 so word_mode is a conservatively correct choice. */
940 value_mode = word_mode;
941 else if (!is_a <scalar_int_mode> (GET_MODE (value), &value_mode))
942 {
943 value_mode = int_mode_for_mode (GET_MODE (value)).require ();
944 value = gen_reg_rtx (value_mode);
945 emit_move_insn (gen_lowpart (GET_MODE (orig_value), value), orig_value);
946 }
947
948 /* If OP0 is a multi-word register, narrow it to the affected word.
949 If the region spans two words, defer to store_split_bit_field.
950 Don't do this if op0 is a single hard register wider than word
951 such as a float or vector register. */
952 if (!MEM_P (op0)
953 && GET_MODE_SIZE (op0_mode.require ()) > UNITS_PER_WORD
954 && (!REG_P (op0)
955 || !HARD_REGISTER_P (op0)
956 || hard_regno_nregs (REGNO (op0), op0_mode.require ()) != 1))
957 {
958 if (bitnum % BITS_PER_WORD + bitsize > BITS_PER_WORD)
959 {
960 if (!fallback_p)
961 return false;
962
963 store_split_bit_field (op0, op0_mode, bitsize, bitnum,
964 bitregion_start, bitregion_end,
965 value, value_mode, reverse);
966 return true;
967 }
968 op0 = simplify_gen_subreg (word_mode, op0, op0_mode.require (),
969 bitnum / BITS_PER_WORD * UNITS_PER_WORD);
970 gcc_assert (op0);
971 op0_mode = word_mode;
972 bitnum %= BITS_PER_WORD;
973 }
974
975 /* From here on we can assume that the field to be stored in fits
976 within a word. If the destination is a register, it too fits
977 in a word. */
978
979 extraction_insn insv;
980 if (!MEM_P (op0)
981 && !reverse
982 && get_best_reg_extraction_insn (&insv, EP_insv,
983 GET_MODE_BITSIZE (op0_mode.require ()),
984 fieldmode)
985 && store_bit_field_using_insv (&insv, op0, op0_mode,
986 bitsize, bitnum, value, value_mode))
987 return true;
988
989 /* If OP0 is a memory, try copying it to a register and seeing if a
990 cheap register alternative is available. */
991 if (MEM_P (op0) && !reverse)
992 {
993 if (get_best_mem_extraction_insn (&insv, EP_insv, bitsize, bitnum,
994 fieldmode)
995 && store_bit_field_using_insv (&insv, op0, op0_mode,
996 bitsize, bitnum, value, value_mode))
997 return true;
998
999 rtx_insn *last = get_last_insn ();
1000
1001 /* Try loading part of OP0 into a register, inserting the bitfield
1002 into that, and then copying the result back to OP0. */
1003 unsigned HOST_WIDE_INT bitpos;
1004 rtx xop0 = adjust_bit_field_mem_for_reg (EP_insv, op0, bitsize, bitnum,
1005 bitregion_start, bitregion_end,
1006 fieldmode, &bitpos);
1007 if (xop0)
1008 {
1009 rtx tempreg = copy_to_reg (xop0);
1010 if (store_bit_field_1 (tempreg, bitsize, bitpos,
1011 bitregion_start, bitregion_end,
1012 fieldmode, orig_value, reverse, false))
1013 {
1014 emit_move_insn (xop0, tempreg);
1015 return true;
1016 }
1017 delete_insns_since (last);
1018 }
1019 }
1020
1021 if (!fallback_p)
1022 return false;
1023
1024 store_fixed_bit_field (op0, op0_mode, bitsize, bitnum, bitregion_start,
1025 bitregion_end, value, value_mode, reverse);
1026 return true;
1027 }
1028
1029 /* Generate code to store value from rtx VALUE
1030 into a bit-field within structure STR_RTX
1031 containing BITSIZE bits starting at bit BITNUM.
1032
1033 BITREGION_START is bitpos of the first bitfield in this region.
1034 BITREGION_END is the bitpos of the ending bitfield in this region.
1035 These two fields are 0, if the C++ memory model does not apply,
1036 or we are not interested in keeping track of bitfield regions.
1037
1038 FIELDMODE is the machine-mode of the FIELD_DECL node for this field.
1039
1040 If REVERSE is true, the store is to be done in reverse order. */
1041
1042 void
1043 store_bit_field (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
1044 unsigned HOST_WIDE_INT bitnum,
1045 unsigned HOST_WIDE_INT bitregion_start,
1046 unsigned HOST_WIDE_INT bitregion_end,
1047 machine_mode fieldmode,
1048 rtx value, bool reverse)
1049 {
1050 /* Handle -fstrict-volatile-bitfields in the cases where it applies. */
1051 scalar_int_mode int_mode;
1052 if (is_a <scalar_int_mode> (fieldmode, &int_mode)
1053 && strict_volatile_bitfield_p (str_rtx, bitsize, bitnum, int_mode,
1054 bitregion_start, bitregion_end))
1055 {
1056 /* Storing of a full word can be done with a simple store.
1057 We know here that the field can be accessed with one single
1058 instruction. For targets that support unaligned memory,
1059 an unaligned access may be necessary. */
1060 if (bitsize == GET_MODE_BITSIZE (int_mode))
1061 {
1062 str_rtx = adjust_bitfield_address (str_rtx, int_mode,
1063 bitnum / BITS_PER_UNIT);
1064 if (reverse)
1065 value = flip_storage_order (int_mode, value);
1066 gcc_assert (bitnum % BITS_PER_UNIT == 0);
1067 emit_move_insn (str_rtx, value);
1068 }
1069 else
1070 {
1071 rtx temp;
1072
1073 str_rtx = narrow_bit_field_mem (str_rtx, int_mode, bitsize, bitnum,
1074 &bitnum);
1075 gcc_assert (bitnum + bitsize <= GET_MODE_BITSIZE (int_mode));
1076 temp = copy_to_reg (str_rtx);
1077 if (!store_bit_field_1 (temp, bitsize, bitnum, 0, 0,
1078 int_mode, value, reverse, true))
1079 gcc_unreachable ();
1080
1081 emit_move_insn (str_rtx, temp);
1082 }
1083
1084 return;
1085 }
1086
1087 /* Under the C++0x memory model, we must not touch bits outside the
1088 bit region. Adjust the address to start at the beginning of the
1089 bit region. */
1090 if (MEM_P (str_rtx) && bitregion_start > 0)
1091 {
1092 scalar_int_mode best_mode;
1093 machine_mode addr_mode = VOIDmode;
1094 HOST_WIDE_INT offset, size;
1095
1096 gcc_assert ((bitregion_start % BITS_PER_UNIT) == 0);
1097
1098 offset = bitregion_start / BITS_PER_UNIT;
1099 bitnum -= bitregion_start;
1100 size = (bitnum + bitsize + BITS_PER_UNIT - 1) / BITS_PER_UNIT;
1101 bitregion_end -= bitregion_start;
1102 bitregion_start = 0;
1103 if (get_best_mode (bitsize, bitnum,
1104 bitregion_start, bitregion_end,
1105 MEM_ALIGN (str_rtx), INT_MAX,
1106 MEM_VOLATILE_P (str_rtx), &best_mode))
1107 addr_mode = best_mode;
1108 str_rtx = adjust_bitfield_address_size (str_rtx, addr_mode,
1109 offset, size);
1110 }
1111
1112 if (!store_bit_field_1 (str_rtx, bitsize, bitnum,
1113 bitregion_start, bitregion_end,
1114 fieldmode, value, reverse, true))
1115 gcc_unreachable ();
1116 }
1117 \f
1118 /* Use shifts and boolean operations to store VALUE into a bit field of
1119 width BITSIZE in OP0, starting at bit BITNUM. If OP0_MODE is defined,
1120 it is the mode of OP0, otherwise OP0 is a BLKmode MEM. VALUE_MODE is
1121 the mode of VALUE.
1122
1123 If REVERSE is true, the store is to be done in reverse order. */
1124
1125 static void
1126 store_fixed_bit_field (rtx op0, opt_scalar_int_mode op0_mode,
1127 unsigned HOST_WIDE_INT bitsize,
1128 unsigned HOST_WIDE_INT bitnum,
1129 unsigned HOST_WIDE_INT bitregion_start,
1130 unsigned HOST_WIDE_INT bitregion_end,
1131 rtx value, scalar_int_mode value_mode, bool reverse)
1132 {
1133 /* There is a case not handled here:
1134 a structure with a known alignment of just a halfword
1135 and a field split across two aligned halfwords within the structure.
1136 Or likewise a structure with a known alignment of just a byte
1137 and a field split across two bytes.
1138 Such cases are not supposed to be able to occur. */
1139
1140 scalar_int_mode best_mode;
1141 if (MEM_P (op0))
1142 {
1143 unsigned int max_bitsize = BITS_PER_WORD;
1144 scalar_int_mode imode;
1145 if (op0_mode.exists (&imode) && GET_MODE_BITSIZE (imode) < max_bitsize)
1146 max_bitsize = GET_MODE_BITSIZE (imode);
1147
1148 if (!get_best_mode (bitsize, bitnum, bitregion_start, bitregion_end,
1149 MEM_ALIGN (op0), max_bitsize, MEM_VOLATILE_P (op0),
1150 &best_mode))
1151 {
1152 /* The only way this should occur is if the field spans word
1153 boundaries. */
1154 store_split_bit_field (op0, op0_mode, bitsize, bitnum,
1155 bitregion_start, bitregion_end,
1156 value, value_mode, reverse);
1157 return;
1158 }
1159
1160 op0 = narrow_bit_field_mem (op0, best_mode, bitsize, bitnum, &bitnum);
1161 }
1162 else
1163 best_mode = op0_mode.require ();
1164
1165 store_fixed_bit_field_1 (op0, best_mode, bitsize, bitnum,
1166 value, value_mode, reverse);
1167 }
1168
1169 /* Helper function for store_fixed_bit_field, stores
1170 the bit field always using MODE, which is the mode of OP0. The other
1171 arguments are as for store_fixed_bit_field. */
1172
1173 static void
1174 store_fixed_bit_field_1 (rtx op0, scalar_int_mode mode,
1175 unsigned HOST_WIDE_INT bitsize,
1176 unsigned HOST_WIDE_INT bitnum,
1177 rtx value, scalar_int_mode value_mode, bool reverse)
1178 {
1179 rtx temp;
1180 int all_zero = 0;
1181 int all_one = 0;
1182
1183 /* Note that bitsize + bitnum can be greater than GET_MODE_BITSIZE (mode)
1184 for invalid input, such as f5 from gcc.dg/pr48335-2.c. */
1185
1186 if (reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
1187 /* BITNUM is the distance between our msb
1188 and that of the containing datum.
1189 Convert it to the distance from the lsb. */
1190 bitnum = GET_MODE_BITSIZE (mode) - bitsize - bitnum;
1191
1192 /* Now BITNUM is always the distance between our lsb
1193 and that of OP0. */
1194
1195 /* Shift VALUE left by BITNUM bits. If VALUE is not constant,
1196 we must first convert its mode to MODE. */
1197
1198 if (CONST_INT_P (value))
1199 {
1200 unsigned HOST_WIDE_INT v = UINTVAL (value);
1201
1202 if (bitsize < HOST_BITS_PER_WIDE_INT)
1203 v &= (HOST_WIDE_INT_1U << bitsize) - 1;
1204
1205 if (v == 0)
1206 all_zero = 1;
1207 else if ((bitsize < HOST_BITS_PER_WIDE_INT
1208 && v == (HOST_WIDE_INT_1U << bitsize) - 1)
1209 || (bitsize == HOST_BITS_PER_WIDE_INT
1210 && v == HOST_WIDE_INT_M1U))
1211 all_one = 1;
1212
1213 value = lshift_value (mode, v, bitnum);
1214 }
1215 else
1216 {
1217 int must_and = (GET_MODE_BITSIZE (value_mode) != bitsize
1218 && bitnum + bitsize != GET_MODE_BITSIZE (mode));
1219
1220 if (value_mode != mode)
1221 value = convert_to_mode (mode, value, 1);
1222
1223 if (must_and)
1224 value = expand_binop (mode, and_optab, value,
1225 mask_rtx (mode, 0, bitsize, 0),
1226 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1227 if (bitnum > 0)
1228 value = expand_shift (LSHIFT_EXPR, mode, value,
1229 bitnum, NULL_RTX, 1);
1230 }
1231
1232 if (reverse)
1233 value = flip_storage_order (mode, value);
1234
1235 /* Now clear the chosen bits in OP0,
1236 except that if VALUE is -1 we need not bother. */
1237 /* We keep the intermediates in registers to allow CSE to combine
1238 consecutive bitfield assignments. */
1239
1240 temp = force_reg (mode, op0);
1241
1242 if (! all_one)
1243 {
1244 rtx mask = mask_rtx (mode, bitnum, bitsize, 1);
1245 if (reverse)
1246 mask = flip_storage_order (mode, mask);
1247 temp = expand_binop (mode, and_optab, temp, mask,
1248 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1249 temp = force_reg (mode, temp);
1250 }
1251
1252 /* Now logical-or VALUE into OP0, unless it is zero. */
1253
1254 if (! all_zero)
1255 {
1256 temp = expand_binop (mode, ior_optab, temp, value,
1257 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1258 temp = force_reg (mode, temp);
1259 }
1260
1261 if (op0 != temp)
1262 {
1263 op0 = copy_rtx (op0);
1264 emit_move_insn (op0, temp);
1265 }
1266 }
1267 \f
1268 /* Store a bit field that is split across multiple accessible memory objects.
1269
1270 OP0 is the REG, SUBREG or MEM rtx for the first of the objects.
1271 BITSIZE is the field width; BITPOS the position of its first bit
1272 (within the word).
1273 VALUE is the value to store, which has mode VALUE_MODE.
1274 If OP0_MODE is defined, it is the mode of OP0, otherwise OP0 is
1275 a BLKmode MEM.
1276
1277 If REVERSE is true, the store is to be done in reverse order.
1278
1279 This does not yet handle fields wider than BITS_PER_WORD. */
1280
1281 static void
1282 store_split_bit_field (rtx op0, opt_scalar_int_mode op0_mode,
1283 unsigned HOST_WIDE_INT bitsize,
1284 unsigned HOST_WIDE_INT bitpos,
1285 unsigned HOST_WIDE_INT bitregion_start,
1286 unsigned HOST_WIDE_INT bitregion_end,
1287 rtx value, scalar_int_mode value_mode, bool reverse)
1288 {
1289 unsigned int unit, total_bits, bitsdone = 0;
1290
1291 /* Make sure UNIT isn't larger than BITS_PER_WORD, we can only handle that
1292 much at a time. */
1293 if (REG_P (op0) || GET_CODE (op0) == SUBREG)
1294 unit = BITS_PER_WORD;
1295 else
1296 unit = MIN (MEM_ALIGN (op0), BITS_PER_WORD);
1297
1298 /* If OP0 is a memory with a mode, then UNIT must not be larger than
1299 OP0's mode as well. Otherwise, store_fixed_bit_field will call us
1300 again, and we will mutually recurse forever. */
1301 if (MEM_P (op0) && op0_mode.exists ())
1302 unit = MIN (unit, GET_MODE_BITSIZE (op0_mode.require ()));
1303
1304 /* If VALUE is a constant other than a CONST_INT, get it into a register in
1305 WORD_MODE. If we can do this using gen_lowpart_common, do so. Note
1306 that VALUE might be a floating-point constant. */
1307 if (CONSTANT_P (value) && !CONST_INT_P (value))
1308 {
1309 rtx word = gen_lowpart_common (word_mode, value);
1310
1311 if (word && (value != word))
1312 value = word;
1313 else
1314 value = gen_lowpart_common (word_mode, force_reg (value_mode, value));
1315 value_mode = word_mode;
1316 }
1317
1318 total_bits = GET_MODE_BITSIZE (value_mode);
1319
1320 while (bitsdone < bitsize)
1321 {
1322 unsigned HOST_WIDE_INT thissize;
1323 unsigned HOST_WIDE_INT thispos;
1324 unsigned HOST_WIDE_INT offset;
1325 rtx part;
1326
1327 offset = (bitpos + bitsdone) / unit;
1328 thispos = (bitpos + bitsdone) % unit;
1329
1330 /* When region of bytes we can touch is restricted, decrease
1331 UNIT close to the end of the region as needed. If op0 is a REG
1332 or SUBREG of REG, don't do this, as there can't be data races
1333 on a register and we can expand shorter code in some cases. */
1334 if (bitregion_end
1335 && unit > BITS_PER_UNIT
1336 && bitpos + bitsdone - thispos + unit > bitregion_end + 1
1337 && !REG_P (op0)
1338 && (GET_CODE (op0) != SUBREG || !REG_P (SUBREG_REG (op0))))
1339 {
1340 unit = unit / 2;
1341 continue;
1342 }
1343
1344 /* THISSIZE must not overrun a word boundary. Otherwise,
1345 store_fixed_bit_field will call us again, and we will mutually
1346 recurse forever. */
1347 thissize = MIN (bitsize - bitsdone, BITS_PER_WORD);
1348 thissize = MIN (thissize, unit - thispos);
1349
1350 if (reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
1351 {
1352 /* Fetch successively less significant portions. */
1353 if (CONST_INT_P (value))
1354 part = GEN_INT (((unsigned HOST_WIDE_INT) (INTVAL (value))
1355 >> (bitsize - bitsdone - thissize))
1356 & ((HOST_WIDE_INT_1 << thissize) - 1));
1357 /* Likewise, but the source is little-endian. */
1358 else if (reverse)
1359 part = extract_fixed_bit_field (word_mode, value, value_mode,
1360 thissize,
1361 bitsize - bitsdone - thissize,
1362 NULL_RTX, 1, false);
1363 else
1364 /* The args are chosen so that the last part includes the
1365 lsb. Give extract_bit_field the value it needs (with
1366 endianness compensation) to fetch the piece we want. */
1367 part = extract_fixed_bit_field (word_mode, value, value_mode,
1368 thissize,
1369 total_bits - bitsize + bitsdone,
1370 NULL_RTX, 1, false);
1371 }
1372 else
1373 {
1374 /* Fetch successively more significant portions. */
1375 if (CONST_INT_P (value))
1376 part = GEN_INT (((unsigned HOST_WIDE_INT) (INTVAL (value))
1377 >> bitsdone)
1378 & ((HOST_WIDE_INT_1 << thissize) - 1));
1379 /* Likewise, but the source is big-endian. */
1380 else if (reverse)
1381 part = extract_fixed_bit_field (word_mode, value, value_mode,
1382 thissize,
1383 total_bits - bitsdone - thissize,
1384 NULL_RTX, 1, false);
1385 else
1386 part = extract_fixed_bit_field (word_mode, value, value_mode,
1387 thissize, bitsdone, NULL_RTX,
1388 1, false);
1389 }
1390
1391 /* If OP0 is a register, then handle OFFSET here. */
1392 rtx op0_piece = op0;
1393 opt_scalar_int_mode op0_piece_mode = op0_mode;
1394 if (SUBREG_P (op0) || REG_P (op0))
1395 {
1396 scalar_int_mode imode;
1397 if (op0_mode.exists (&imode)
1398 && GET_MODE_SIZE (imode) < UNITS_PER_WORD)
1399 {
1400 if (offset)
1401 op0_piece = const0_rtx;
1402 }
1403 else
1404 {
1405 op0_piece = operand_subword_force (op0,
1406 offset * unit / BITS_PER_WORD,
1407 GET_MODE (op0));
1408 op0_piece_mode = word_mode;
1409 }
1410 offset &= BITS_PER_WORD / unit - 1;
1411 }
1412
1413 /* OFFSET is in UNITs, and UNIT is in bits. If WORD is const0_rtx,
1414 it is just an out-of-bounds access. Ignore it. */
1415 if (op0_piece != const0_rtx)
1416 store_fixed_bit_field (op0_piece, op0_piece_mode, thissize,
1417 offset * unit + thispos, bitregion_start,
1418 bitregion_end, part, word_mode, reverse);
1419 bitsdone += thissize;
1420 }
1421 }
1422 \f
1423 /* A subroutine of extract_bit_field_1 that converts return value X
1424 to either MODE or TMODE. MODE, TMODE and UNSIGNEDP are arguments
1425 to extract_bit_field. */
1426
1427 static rtx
1428 convert_extracted_bit_field (rtx x, machine_mode mode,
1429 machine_mode tmode, bool unsignedp)
1430 {
1431 if (GET_MODE (x) == tmode || GET_MODE (x) == mode)
1432 return x;
1433
1434 /* If the x mode is not a scalar integral, first convert to the
1435 integer mode of that size and then access it as a floating-point
1436 value via a SUBREG. */
1437 if (!SCALAR_INT_MODE_P (tmode))
1438 {
1439 scalar_int_mode int_mode = int_mode_for_mode (tmode).require ();
1440 x = convert_to_mode (int_mode, x, unsignedp);
1441 x = force_reg (int_mode, x);
1442 return gen_lowpart (tmode, x);
1443 }
1444
1445 return convert_to_mode (tmode, x, unsignedp);
1446 }
1447
1448 /* Try to use an ext(z)v pattern to extract a field from OP0.
1449 Return the extracted value on success, otherwise return null.
1450 EXTV describes the extraction instruction to use. If OP0_MODE
1451 is defined, it is the mode of OP0, otherwise OP0 is a BLKmode MEM.
1452 The other arguments are as for extract_bit_field. */
1453
1454 static rtx
1455 extract_bit_field_using_extv (const extraction_insn *extv, rtx op0,
1456 opt_scalar_int_mode op0_mode,
1457 unsigned HOST_WIDE_INT bitsize,
1458 unsigned HOST_WIDE_INT bitnum,
1459 int unsignedp, rtx target,
1460 machine_mode mode, machine_mode tmode)
1461 {
1462 struct expand_operand ops[4];
1463 rtx spec_target = target;
1464 rtx spec_target_subreg = 0;
1465 scalar_int_mode ext_mode = extv->field_mode;
1466 unsigned unit = GET_MODE_BITSIZE (ext_mode);
1467
1468 if (bitsize == 0 || unit < bitsize)
1469 return NULL_RTX;
1470
1471 if (MEM_P (op0))
1472 /* Get a reference to the first byte of the field. */
1473 op0 = narrow_bit_field_mem (op0, extv->struct_mode, bitsize, bitnum,
1474 &bitnum);
1475 else
1476 {
1477 /* Convert from counting within OP0 to counting in EXT_MODE. */
1478 if (BYTES_BIG_ENDIAN)
1479 bitnum += unit - GET_MODE_BITSIZE (op0_mode.require ());
1480
1481 /* If op0 is a register, we need it in EXT_MODE to make it
1482 acceptable to the format of ext(z)v. */
1483 if (GET_CODE (op0) == SUBREG && op0_mode.require () != ext_mode)
1484 return NULL_RTX;
1485 if (REG_P (op0) && op0_mode.require () != ext_mode)
1486 op0 = gen_lowpart_SUBREG (ext_mode, op0);
1487 }
1488
1489 /* If BITS_BIG_ENDIAN is zero on a BYTES_BIG_ENDIAN machine, we count
1490 "backwards" from the size of the unit we are extracting from.
1491 Otherwise, we count bits from the most significant on a
1492 BYTES/BITS_BIG_ENDIAN machine. */
1493
1494 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
1495 bitnum = unit - bitsize - bitnum;
1496
1497 if (target == 0)
1498 target = spec_target = gen_reg_rtx (tmode);
1499
1500 if (GET_MODE (target) != ext_mode)
1501 {
1502 /* Don't use LHS paradoxical subreg if explicit truncation is needed
1503 between the mode of the extraction (word_mode) and the target
1504 mode. Instead, create a temporary and use convert_move to set
1505 the target. */
1506 if (REG_P (target)
1507 && TRULY_NOOP_TRUNCATION_MODES_P (GET_MODE (target), ext_mode))
1508 {
1509 target = gen_lowpart (ext_mode, target);
1510 if (partial_subreg_p (GET_MODE (spec_target), ext_mode))
1511 spec_target_subreg = target;
1512 }
1513 else
1514 target = gen_reg_rtx (ext_mode);
1515 }
1516
1517 create_output_operand (&ops[0], target, ext_mode);
1518 create_fixed_operand (&ops[1], op0);
1519 create_integer_operand (&ops[2], bitsize);
1520 create_integer_operand (&ops[3], bitnum);
1521 if (maybe_expand_insn (extv->icode, 4, ops))
1522 {
1523 target = ops[0].value;
1524 if (target == spec_target)
1525 return target;
1526 if (target == spec_target_subreg)
1527 return spec_target;
1528 return convert_extracted_bit_field (target, mode, tmode, unsignedp);
1529 }
1530 return NULL_RTX;
1531 }
1532
1533 /* A subroutine of extract_bit_field, with the same arguments.
1534 If FALLBACK_P is true, fall back to extract_fixed_bit_field
1535 if we can find no other means of implementing the operation.
1536 if FALLBACK_P is false, return NULL instead. */
1537
1538 static rtx
1539 extract_bit_field_1 (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
1540 unsigned HOST_WIDE_INT bitnum, int unsignedp, rtx target,
1541 machine_mode mode, machine_mode tmode,
1542 bool reverse, bool fallback_p, rtx *alt_rtl)
1543 {
1544 rtx op0 = str_rtx;
1545 machine_mode mode1;
1546
1547 if (tmode == VOIDmode)
1548 tmode = mode;
1549
1550 while (GET_CODE (op0) == SUBREG)
1551 {
1552 bitnum += SUBREG_BYTE (op0) * BITS_PER_UNIT;
1553 op0 = SUBREG_REG (op0);
1554 }
1555
1556 /* If we have an out-of-bounds access to a register, just return an
1557 uninitialized register of the required mode. This can occur if the
1558 source code contains an out-of-bounds access to a small array. */
1559 if (REG_P (op0) && bitnum >= GET_MODE_BITSIZE (GET_MODE (op0)))
1560 return gen_reg_rtx (tmode);
1561
1562 if (REG_P (op0)
1563 && mode == GET_MODE (op0)
1564 && bitnum == 0
1565 && bitsize == GET_MODE_BITSIZE (GET_MODE (op0)))
1566 {
1567 if (reverse)
1568 op0 = flip_storage_order (mode, op0);
1569 /* We're trying to extract a full register from itself. */
1570 return op0;
1571 }
1572
1573 /* First try to check for vector from vector extractions. */
1574 if (VECTOR_MODE_P (GET_MODE (op0))
1575 && !MEM_P (op0)
1576 && VECTOR_MODE_P (tmode)
1577 && GET_MODE_SIZE (GET_MODE (op0)) > GET_MODE_SIZE (tmode))
1578 {
1579 machine_mode new_mode = GET_MODE (op0);
1580 if (GET_MODE_INNER (new_mode) != GET_MODE_INNER (tmode))
1581 {
1582 scalar_mode inner_mode = GET_MODE_INNER (tmode);
1583 unsigned int nunits = (GET_MODE_BITSIZE (GET_MODE (op0))
1584 / GET_MODE_UNIT_BITSIZE (tmode));
1585 if (!mode_for_vector (inner_mode, nunits).exists (&new_mode)
1586 || !VECTOR_MODE_P (new_mode)
1587 || GET_MODE_SIZE (new_mode) != GET_MODE_SIZE (GET_MODE (op0))
1588 || GET_MODE_INNER (new_mode) != GET_MODE_INNER (tmode)
1589 || !targetm.vector_mode_supported_p (new_mode))
1590 new_mode = VOIDmode;
1591 }
1592 if (new_mode != VOIDmode
1593 && (convert_optab_handler (vec_extract_optab, new_mode, tmode)
1594 != CODE_FOR_nothing)
1595 && ((bitnum + bitsize - 1) / GET_MODE_BITSIZE (tmode)
1596 == bitnum / GET_MODE_BITSIZE (tmode)))
1597 {
1598 struct expand_operand ops[3];
1599 machine_mode outermode = new_mode;
1600 machine_mode innermode = tmode;
1601 enum insn_code icode
1602 = convert_optab_handler (vec_extract_optab, outermode, innermode);
1603 unsigned HOST_WIDE_INT pos = bitnum / GET_MODE_BITSIZE (innermode);
1604
1605 if (new_mode != GET_MODE (op0))
1606 op0 = gen_lowpart (new_mode, op0);
1607 create_output_operand (&ops[0], target, innermode);
1608 ops[0].target = 1;
1609 create_input_operand (&ops[1], op0, outermode);
1610 create_integer_operand (&ops[2], pos);
1611 if (maybe_expand_insn (icode, 3, ops))
1612 {
1613 if (alt_rtl && ops[0].target)
1614 *alt_rtl = target;
1615 target = ops[0].value;
1616 if (GET_MODE (target) != mode)
1617 return gen_lowpart (tmode, target);
1618 return target;
1619 }
1620 }
1621 }
1622
1623 /* See if we can get a better vector mode before extracting. */
1624 if (VECTOR_MODE_P (GET_MODE (op0))
1625 && !MEM_P (op0)
1626 && GET_MODE_INNER (GET_MODE (op0)) != tmode)
1627 {
1628 machine_mode new_mode;
1629
1630 if (GET_MODE_CLASS (tmode) == MODE_FLOAT)
1631 new_mode = MIN_MODE_VECTOR_FLOAT;
1632 else if (GET_MODE_CLASS (tmode) == MODE_FRACT)
1633 new_mode = MIN_MODE_VECTOR_FRACT;
1634 else if (GET_MODE_CLASS (tmode) == MODE_UFRACT)
1635 new_mode = MIN_MODE_VECTOR_UFRACT;
1636 else if (GET_MODE_CLASS (tmode) == MODE_ACCUM)
1637 new_mode = MIN_MODE_VECTOR_ACCUM;
1638 else if (GET_MODE_CLASS (tmode) == MODE_UACCUM)
1639 new_mode = MIN_MODE_VECTOR_UACCUM;
1640 else
1641 new_mode = MIN_MODE_VECTOR_INT;
1642
1643 FOR_EACH_MODE_FROM (new_mode, new_mode)
1644 if (GET_MODE_SIZE (new_mode) == GET_MODE_SIZE (GET_MODE (op0))
1645 && GET_MODE_UNIT_SIZE (new_mode) == GET_MODE_SIZE (tmode)
1646 && targetm.vector_mode_supported_p (new_mode))
1647 break;
1648 if (new_mode != VOIDmode)
1649 op0 = gen_lowpart (new_mode, op0);
1650 }
1651
1652 /* Use vec_extract patterns for extracting parts of vectors whenever
1653 available. */
1654 machine_mode outermode = GET_MODE (op0);
1655 scalar_mode innermode = GET_MODE_INNER (outermode);
1656 if (VECTOR_MODE_P (outermode)
1657 && !MEM_P (op0)
1658 && (convert_optab_handler (vec_extract_optab, outermode, innermode)
1659 != CODE_FOR_nothing)
1660 && ((bitnum + bitsize - 1) / GET_MODE_BITSIZE (innermode)
1661 == bitnum / GET_MODE_BITSIZE (innermode)))
1662 {
1663 struct expand_operand ops[3];
1664 enum insn_code icode
1665 = convert_optab_handler (vec_extract_optab, outermode, innermode);
1666 unsigned HOST_WIDE_INT pos = bitnum / GET_MODE_BITSIZE (innermode);
1667
1668 create_output_operand (&ops[0], target, innermode);
1669 ops[0].target = 1;
1670 create_input_operand (&ops[1], op0, outermode);
1671 create_integer_operand (&ops[2], pos);
1672 if (maybe_expand_insn (icode, 3, ops))
1673 {
1674 if (alt_rtl && ops[0].target)
1675 *alt_rtl = target;
1676 target = ops[0].value;
1677 if (GET_MODE (target) != mode)
1678 return gen_lowpart (tmode, target);
1679 return target;
1680 }
1681 }
1682
1683 /* Make sure we are playing with integral modes. Pun with subregs
1684 if we aren't. */
1685 opt_scalar_int_mode op0_mode = int_mode_for_mode (GET_MODE (op0));
1686 scalar_int_mode imode;
1687 if (!op0_mode.exists (&imode) || imode != GET_MODE (op0))
1688 {
1689 if (MEM_P (op0))
1690 op0 = adjust_bitfield_address_size (op0, op0_mode.else_blk (),
1691 0, MEM_SIZE (op0));
1692 else if (op0_mode.exists (&imode))
1693 {
1694 op0 = gen_lowpart (imode, op0);
1695
1696 /* If we got a SUBREG, force it into a register since we
1697 aren't going to be able to do another SUBREG on it. */
1698 if (GET_CODE (op0) == SUBREG)
1699 op0 = force_reg (imode, op0);
1700 }
1701 else
1702 {
1703 HOST_WIDE_INT size = GET_MODE_SIZE (GET_MODE (op0));
1704 rtx mem = assign_stack_temp (GET_MODE (op0), size);
1705 emit_move_insn (mem, op0);
1706 op0 = adjust_bitfield_address_size (mem, BLKmode, 0, size);
1707 }
1708 }
1709
1710 /* ??? We currently assume TARGET is at least as big as BITSIZE.
1711 If that's wrong, the solution is to test for it and set TARGET to 0
1712 if needed. */
1713
1714 /* Get the mode of the field to use for atomic access or subreg
1715 conversion. */
1716 if (!SCALAR_INT_MODE_P (tmode)
1717 || !mode_for_size (bitsize, GET_MODE_CLASS (tmode), 0).exists (&mode1))
1718 mode1 = mode;
1719 gcc_assert (mode1 != BLKmode);
1720
1721 /* Extraction of a full MODE1 value can be done with a subreg as long
1722 as the least significant bit of the value is the least significant
1723 bit of either OP0 or a word of OP0. */
1724 if (!MEM_P (op0)
1725 && !reverse
1726 && lowpart_bit_field_p (bitnum, bitsize, op0_mode.require ())
1727 && bitsize == GET_MODE_BITSIZE (mode1)
1728 && TRULY_NOOP_TRUNCATION_MODES_P (mode1, op0_mode.require ()))
1729 {
1730 rtx sub = simplify_gen_subreg (mode1, op0, op0_mode.require (),
1731 bitnum / BITS_PER_UNIT);
1732 if (sub)
1733 return convert_extracted_bit_field (sub, mode, tmode, unsignedp);
1734 }
1735
1736 /* Extraction of a full MODE1 value can be done with a load as long as
1737 the field is on a byte boundary and is sufficiently aligned. */
1738 if (simple_mem_bitfield_p (op0, bitsize, bitnum, mode1))
1739 {
1740 op0 = adjust_bitfield_address (op0, mode1, bitnum / BITS_PER_UNIT);
1741 if (reverse)
1742 op0 = flip_storage_order (mode1, op0);
1743 return convert_extracted_bit_field (op0, mode, tmode, unsignedp);
1744 }
1745
1746 /* Handle fields bigger than a word. */
1747
1748 if (bitsize > BITS_PER_WORD)
1749 {
1750 /* Here we transfer the words of the field
1751 in the order least significant first.
1752 This is because the most significant word is the one which may
1753 be less than full. */
1754
1755 const bool backwards = WORDS_BIG_ENDIAN;
1756 unsigned int nwords = (bitsize + (BITS_PER_WORD - 1)) / BITS_PER_WORD;
1757 unsigned int i;
1758 rtx_insn *last;
1759
1760 if (target == 0 || !REG_P (target) || !valid_multiword_target_p (target))
1761 target = gen_reg_rtx (mode);
1762
1763 /* In case we're about to clobber a base register or something
1764 (see gcc.c-torture/execute/20040625-1.c). */
1765 if (reg_mentioned_p (target, str_rtx))
1766 target = gen_reg_rtx (mode);
1767
1768 /* Indicate for flow that the entire target reg is being set. */
1769 emit_clobber (target);
1770
1771 last = get_last_insn ();
1772 for (i = 0; i < nwords; i++)
1773 {
1774 /* If I is 0, use the low-order word in both field and target;
1775 if I is 1, use the next to lowest word; and so on. */
1776 /* Word number in TARGET to use. */
1777 unsigned int wordnum
1778 = (backwards
1779 ? GET_MODE_SIZE (GET_MODE (target)) / UNITS_PER_WORD - i - 1
1780 : i);
1781 /* Offset from start of field in OP0. */
1782 unsigned int bit_offset = (backwards ^ reverse
1783 ? MAX ((int) bitsize - ((int) i + 1)
1784 * BITS_PER_WORD,
1785 0)
1786 : (int) i * BITS_PER_WORD);
1787 rtx target_part = operand_subword (target, wordnum, 1, VOIDmode);
1788 rtx result_part
1789 = extract_bit_field_1 (op0, MIN (BITS_PER_WORD,
1790 bitsize - i * BITS_PER_WORD),
1791 bitnum + bit_offset, 1, target_part,
1792 mode, word_mode, reverse, fallback_p, NULL);
1793
1794 gcc_assert (target_part);
1795 if (!result_part)
1796 {
1797 delete_insns_since (last);
1798 return NULL;
1799 }
1800
1801 if (result_part != target_part)
1802 emit_move_insn (target_part, result_part);
1803 }
1804
1805 if (unsignedp)
1806 {
1807 /* Unless we've filled TARGET, the upper regs in a multi-reg value
1808 need to be zero'd out. */
1809 if (GET_MODE_SIZE (GET_MODE (target)) > nwords * UNITS_PER_WORD)
1810 {
1811 unsigned int i, total_words;
1812
1813 total_words = GET_MODE_SIZE (GET_MODE (target)) / UNITS_PER_WORD;
1814 for (i = nwords; i < total_words; i++)
1815 emit_move_insn
1816 (operand_subword (target,
1817 backwards ? total_words - i - 1 : i,
1818 1, VOIDmode),
1819 const0_rtx);
1820 }
1821 return target;
1822 }
1823
1824 /* Signed bit field: sign-extend with two arithmetic shifts. */
1825 target = expand_shift (LSHIFT_EXPR, mode, target,
1826 GET_MODE_BITSIZE (mode) - bitsize, NULL_RTX, 0);
1827 return expand_shift (RSHIFT_EXPR, mode, target,
1828 GET_MODE_BITSIZE (mode) - bitsize, NULL_RTX, 0);
1829 }
1830
1831 /* If OP0 is a multi-word register, narrow it to the affected word.
1832 If the region spans two words, defer to extract_split_bit_field. */
1833 if (!MEM_P (op0) && GET_MODE_SIZE (op0_mode.require ()) > UNITS_PER_WORD)
1834 {
1835 if (bitnum % BITS_PER_WORD + bitsize > BITS_PER_WORD)
1836 {
1837 if (!fallback_p)
1838 return NULL_RTX;
1839 target = extract_split_bit_field (op0, op0_mode, bitsize, bitnum,
1840 unsignedp, reverse);
1841 return convert_extracted_bit_field (target, mode, tmode, unsignedp);
1842 }
1843 op0 = simplify_gen_subreg (word_mode, op0, op0_mode.require (),
1844 bitnum / BITS_PER_WORD * UNITS_PER_WORD);
1845 op0_mode = word_mode;
1846 bitnum %= BITS_PER_WORD;
1847 }
1848
1849 /* From here on we know the desired field is smaller than a word.
1850 If OP0 is a register, it too fits within a word. */
1851 enum extraction_pattern pattern = unsignedp ? EP_extzv : EP_extv;
1852 extraction_insn extv;
1853 if (!MEM_P (op0)
1854 && !reverse
1855 /* ??? We could limit the structure size to the part of OP0 that
1856 contains the field, with appropriate checks for endianness
1857 and TARGET_TRULY_NOOP_TRUNCATION. */
1858 && get_best_reg_extraction_insn (&extv, pattern,
1859 GET_MODE_BITSIZE (op0_mode.require ()),
1860 tmode))
1861 {
1862 rtx result = extract_bit_field_using_extv (&extv, op0, op0_mode,
1863 bitsize, bitnum,
1864 unsignedp, target, mode,
1865 tmode);
1866 if (result)
1867 return result;
1868 }
1869
1870 /* If OP0 is a memory, try copying it to a register and seeing if a
1871 cheap register alternative is available. */
1872 if (MEM_P (op0) & !reverse)
1873 {
1874 if (get_best_mem_extraction_insn (&extv, pattern, bitsize, bitnum,
1875 tmode))
1876 {
1877 rtx result = extract_bit_field_using_extv (&extv, op0, op0_mode,
1878 bitsize, bitnum,
1879 unsignedp, target, mode,
1880 tmode);
1881 if (result)
1882 return result;
1883 }
1884
1885 rtx_insn *last = get_last_insn ();
1886
1887 /* Try loading part of OP0 into a register and extracting the
1888 bitfield from that. */
1889 unsigned HOST_WIDE_INT bitpos;
1890 rtx xop0 = adjust_bit_field_mem_for_reg (pattern, op0, bitsize, bitnum,
1891 0, 0, tmode, &bitpos);
1892 if (xop0)
1893 {
1894 xop0 = copy_to_reg (xop0);
1895 rtx result = extract_bit_field_1 (xop0, bitsize, bitpos,
1896 unsignedp, target,
1897 mode, tmode, reverse, false, NULL);
1898 if (result)
1899 return result;
1900 delete_insns_since (last);
1901 }
1902 }
1903
1904 if (!fallback_p)
1905 return NULL;
1906
1907 /* Find a correspondingly-sized integer field, so we can apply
1908 shifts and masks to it. */
1909 scalar_int_mode int_mode;
1910 if (!int_mode_for_mode (tmode).exists (&int_mode))
1911 /* If this fails, we should probably push op0 out to memory and then
1912 do a load. */
1913 int_mode = int_mode_for_mode (mode).require ();
1914
1915 target = extract_fixed_bit_field (int_mode, op0, op0_mode, bitsize,
1916 bitnum, target, unsignedp, reverse);
1917
1918 /* Complex values must be reversed piecewise, so we need to undo the global
1919 reversal, convert to the complex mode and reverse again. */
1920 if (reverse && COMPLEX_MODE_P (tmode))
1921 {
1922 target = flip_storage_order (int_mode, target);
1923 target = convert_extracted_bit_field (target, mode, tmode, unsignedp);
1924 target = flip_storage_order (tmode, target);
1925 }
1926 else
1927 target = convert_extracted_bit_field (target, mode, tmode, unsignedp);
1928
1929 return target;
1930 }
1931
1932 /* Generate code to extract a byte-field from STR_RTX
1933 containing BITSIZE bits, starting at BITNUM,
1934 and put it in TARGET if possible (if TARGET is nonzero).
1935 Regardless of TARGET, we return the rtx for where the value is placed.
1936
1937 STR_RTX is the structure containing the byte (a REG or MEM).
1938 UNSIGNEDP is nonzero if this is an unsigned bit field.
1939 MODE is the natural mode of the field value once extracted.
1940 TMODE is the mode the caller would like the value to have;
1941 but the value may be returned with type MODE instead.
1942
1943 If REVERSE is true, the extraction is to be done in reverse order.
1944
1945 If a TARGET is specified and we can store in it at no extra cost,
1946 we do so, and return TARGET.
1947 Otherwise, we return a REG of mode TMODE or MODE, with TMODE preferred
1948 if they are equally easy. */
1949
1950 rtx
1951 extract_bit_field (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
1952 unsigned HOST_WIDE_INT bitnum, int unsignedp, rtx target,
1953 machine_mode mode, machine_mode tmode, bool reverse,
1954 rtx *alt_rtl)
1955 {
1956 machine_mode mode1;
1957
1958 /* Handle -fstrict-volatile-bitfields in the cases where it applies. */
1959 if (GET_MODE_BITSIZE (GET_MODE (str_rtx)) > 0)
1960 mode1 = GET_MODE (str_rtx);
1961 else if (target && GET_MODE_BITSIZE (GET_MODE (target)) > 0)
1962 mode1 = GET_MODE (target);
1963 else
1964 mode1 = tmode;
1965
1966 scalar_int_mode int_mode;
1967 if (is_a <scalar_int_mode> (mode1, &int_mode)
1968 && strict_volatile_bitfield_p (str_rtx, bitsize, bitnum, int_mode, 0, 0))
1969 {
1970 /* Extraction of a full INT_MODE value can be done with a simple load.
1971 We know here that the field can be accessed with one single
1972 instruction. For targets that support unaligned memory,
1973 an unaligned access may be necessary. */
1974 if (bitsize == GET_MODE_BITSIZE (int_mode))
1975 {
1976 rtx result = adjust_bitfield_address (str_rtx, int_mode,
1977 bitnum / BITS_PER_UNIT);
1978 if (reverse)
1979 result = flip_storage_order (int_mode, result);
1980 gcc_assert (bitnum % BITS_PER_UNIT == 0);
1981 return convert_extracted_bit_field (result, mode, tmode, unsignedp);
1982 }
1983
1984 str_rtx = narrow_bit_field_mem (str_rtx, int_mode, bitsize, bitnum,
1985 &bitnum);
1986 gcc_assert (bitnum + bitsize <= GET_MODE_BITSIZE (int_mode));
1987 str_rtx = copy_to_reg (str_rtx);
1988 }
1989
1990 return extract_bit_field_1 (str_rtx, bitsize, bitnum, unsignedp,
1991 target, mode, tmode, reverse, true, alt_rtl);
1992 }
1993 \f
1994 /* Use shifts and boolean operations to extract a field of BITSIZE bits
1995 from bit BITNUM of OP0. If OP0_MODE is defined, it is the mode of OP0,
1996 otherwise OP0 is a BLKmode MEM.
1997
1998 UNSIGNEDP is nonzero for an unsigned bit field (don't sign-extend value).
1999 If REVERSE is true, the extraction is to be done in reverse order.
2000
2001 If TARGET is nonzero, attempts to store the value there
2002 and return TARGET, but this is not guaranteed.
2003 If TARGET is not used, create a pseudo-reg of mode TMODE for the value. */
2004
2005 static rtx
2006 extract_fixed_bit_field (machine_mode tmode, rtx op0,
2007 opt_scalar_int_mode op0_mode,
2008 unsigned HOST_WIDE_INT bitsize,
2009 unsigned HOST_WIDE_INT bitnum, rtx target,
2010 int unsignedp, bool reverse)
2011 {
2012 scalar_int_mode mode;
2013 if (MEM_P (op0))
2014 {
2015 if (!get_best_mode (bitsize, bitnum, 0, 0, MEM_ALIGN (op0),
2016 BITS_PER_WORD, MEM_VOLATILE_P (op0), &mode))
2017 /* The only way this should occur is if the field spans word
2018 boundaries. */
2019 return extract_split_bit_field (op0, op0_mode, bitsize, bitnum,
2020 unsignedp, reverse);
2021
2022 op0 = narrow_bit_field_mem (op0, mode, bitsize, bitnum, &bitnum);
2023 }
2024 else
2025 mode = op0_mode.require ();
2026
2027 return extract_fixed_bit_field_1 (tmode, op0, mode, bitsize, bitnum,
2028 target, unsignedp, reverse);
2029 }
2030
2031 /* Helper function for extract_fixed_bit_field, extracts
2032 the bit field always using MODE, which is the mode of OP0.
2033 The other arguments are as for extract_fixed_bit_field. */
2034
2035 static rtx
2036 extract_fixed_bit_field_1 (machine_mode tmode, rtx op0, scalar_int_mode mode,
2037 unsigned HOST_WIDE_INT bitsize,
2038 unsigned HOST_WIDE_INT bitnum, rtx target,
2039 int unsignedp, bool reverse)
2040 {
2041 /* Note that bitsize + bitnum can be greater than GET_MODE_BITSIZE (mode)
2042 for invalid input, such as extract equivalent of f5 from
2043 gcc.dg/pr48335-2.c. */
2044
2045 if (reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
2046 /* BITNUM is the distance between our msb and that of OP0.
2047 Convert it to the distance from the lsb. */
2048 bitnum = GET_MODE_BITSIZE (mode) - bitsize - bitnum;
2049
2050 /* Now BITNUM is always the distance between the field's lsb and that of OP0.
2051 We have reduced the big-endian case to the little-endian case. */
2052 if (reverse)
2053 op0 = flip_storage_order (mode, op0);
2054
2055 if (unsignedp)
2056 {
2057 if (bitnum)
2058 {
2059 /* If the field does not already start at the lsb,
2060 shift it so it does. */
2061 /* Maybe propagate the target for the shift. */
2062 rtx subtarget = (target != 0 && REG_P (target) ? target : 0);
2063 if (tmode != mode)
2064 subtarget = 0;
2065 op0 = expand_shift (RSHIFT_EXPR, mode, op0, bitnum, subtarget, 1);
2066 }
2067 /* Convert the value to the desired mode. TMODE must also be a
2068 scalar integer for this conversion to make sense, since we
2069 shouldn't reinterpret the bits. */
2070 scalar_int_mode new_mode = as_a <scalar_int_mode> (tmode);
2071 if (mode != new_mode)
2072 op0 = convert_to_mode (new_mode, op0, 1);
2073
2074 /* Unless the msb of the field used to be the msb when we shifted,
2075 mask out the upper bits. */
2076
2077 if (GET_MODE_BITSIZE (mode) != bitnum + bitsize)
2078 return expand_binop (new_mode, and_optab, op0,
2079 mask_rtx (new_mode, 0, bitsize, 0),
2080 target, 1, OPTAB_LIB_WIDEN);
2081 return op0;
2082 }
2083
2084 /* To extract a signed bit-field, first shift its msb to the msb of the word,
2085 then arithmetic-shift its lsb to the lsb of the word. */
2086 op0 = force_reg (mode, op0);
2087
2088 /* Find the narrowest integer mode that contains the field. */
2089
2090 opt_scalar_int_mode mode_iter;
2091 FOR_EACH_MODE_IN_CLASS (mode_iter, MODE_INT)
2092 if (GET_MODE_BITSIZE (mode_iter.require ()) >= bitsize + bitnum)
2093 break;
2094
2095 mode = mode_iter.require ();
2096 op0 = convert_to_mode (mode, op0, 0);
2097
2098 if (mode != tmode)
2099 target = 0;
2100
2101 if (GET_MODE_BITSIZE (mode) != (bitsize + bitnum))
2102 {
2103 int amount = GET_MODE_BITSIZE (mode) - (bitsize + bitnum);
2104 /* Maybe propagate the target for the shift. */
2105 rtx subtarget = (target != 0 && REG_P (target) ? target : 0);
2106 op0 = expand_shift (LSHIFT_EXPR, mode, op0, amount, subtarget, 1);
2107 }
2108
2109 return expand_shift (RSHIFT_EXPR, mode, op0,
2110 GET_MODE_BITSIZE (mode) - bitsize, target, 0);
2111 }
2112
2113 /* Return a constant integer (CONST_INT or CONST_DOUBLE) rtx with the value
2114 VALUE << BITPOS. */
2115
2116 static rtx
2117 lshift_value (machine_mode mode, unsigned HOST_WIDE_INT value,
2118 int bitpos)
2119 {
2120 return immed_wide_int_const (wi::lshift (value, bitpos), mode);
2121 }
2122 \f
2123 /* Extract a bit field that is split across two words
2124 and return an RTX for the result.
2125
2126 OP0 is the REG, SUBREG or MEM rtx for the first of the two words.
2127 BITSIZE is the field width; BITPOS, position of its first bit, in the word.
2128 UNSIGNEDP is 1 if should zero-extend the contents; else sign-extend.
2129 If OP0_MODE is defined, it is the mode of OP0, otherwise OP0 is
2130 a BLKmode MEM.
2131
2132 If REVERSE is true, the extraction is to be done in reverse order. */
2133
2134 static rtx
2135 extract_split_bit_field (rtx op0, opt_scalar_int_mode op0_mode,
2136 unsigned HOST_WIDE_INT bitsize,
2137 unsigned HOST_WIDE_INT bitpos, int unsignedp,
2138 bool reverse)
2139 {
2140 unsigned int unit;
2141 unsigned int bitsdone = 0;
2142 rtx result = NULL_RTX;
2143 int first = 1;
2144
2145 /* Make sure UNIT isn't larger than BITS_PER_WORD, we can only handle that
2146 much at a time. */
2147 if (REG_P (op0) || GET_CODE (op0) == SUBREG)
2148 unit = BITS_PER_WORD;
2149 else
2150 unit = MIN (MEM_ALIGN (op0), BITS_PER_WORD);
2151
2152 while (bitsdone < bitsize)
2153 {
2154 unsigned HOST_WIDE_INT thissize;
2155 rtx part;
2156 unsigned HOST_WIDE_INT thispos;
2157 unsigned HOST_WIDE_INT offset;
2158
2159 offset = (bitpos + bitsdone) / unit;
2160 thispos = (bitpos + bitsdone) % unit;
2161
2162 /* THISSIZE must not overrun a word boundary. Otherwise,
2163 extract_fixed_bit_field will call us again, and we will mutually
2164 recurse forever. */
2165 thissize = MIN (bitsize - bitsdone, BITS_PER_WORD);
2166 thissize = MIN (thissize, unit - thispos);
2167
2168 /* If OP0 is a register, then handle OFFSET here. */
2169 rtx op0_piece = op0;
2170 opt_scalar_int_mode op0_piece_mode = op0_mode;
2171 if (SUBREG_P (op0) || REG_P (op0))
2172 {
2173 op0_piece = operand_subword_force (op0, offset, op0_mode.require ());
2174 op0_piece_mode = word_mode;
2175 offset = 0;
2176 }
2177
2178 /* Extract the parts in bit-counting order,
2179 whose meaning is determined by BYTES_PER_UNIT.
2180 OFFSET is in UNITs, and UNIT is in bits. */
2181 part = extract_fixed_bit_field (word_mode, op0_piece, op0_piece_mode,
2182 thissize, offset * unit + thispos,
2183 0, 1, reverse);
2184 bitsdone += thissize;
2185
2186 /* Shift this part into place for the result. */
2187 if (reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
2188 {
2189 if (bitsize != bitsdone)
2190 part = expand_shift (LSHIFT_EXPR, word_mode, part,
2191 bitsize - bitsdone, 0, 1);
2192 }
2193 else
2194 {
2195 if (bitsdone != thissize)
2196 part = expand_shift (LSHIFT_EXPR, word_mode, part,
2197 bitsdone - thissize, 0, 1);
2198 }
2199
2200 if (first)
2201 result = part;
2202 else
2203 /* Combine the parts with bitwise or. This works
2204 because we extracted each part as an unsigned bit field. */
2205 result = expand_binop (word_mode, ior_optab, part, result, NULL_RTX, 1,
2206 OPTAB_LIB_WIDEN);
2207
2208 first = 0;
2209 }
2210
2211 /* Unsigned bit field: we are done. */
2212 if (unsignedp)
2213 return result;
2214 /* Signed bit field: sign-extend with two arithmetic shifts. */
2215 result = expand_shift (LSHIFT_EXPR, word_mode, result,
2216 BITS_PER_WORD - bitsize, NULL_RTX, 0);
2217 return expand_shift (RSHIFT_EXPR, word_mode, result,
2218 BITS_PER_WORD - bitsize, NULL_RTX, 0);
2219 }
2220 \f
2221 /* Try to read the low bits of SRC as an rvalue of mode MODE, preserving
2222 the bit pattern. SRC_MODE is the mode of SRC; if this is smaller than
2223 MODE, fill the upper bits with zeros. Fail if the layout of either
2224 mode is unknown (as for CC modes) or if the extraction would involve
2225 unprofitable mode punning. Return the value on success, otherwise
2226 return null.
2227
2228 This is different from gen_lowpart* in these respects:
2229
2230 - the returned value must always be considered an rvalue
2231
2232 - when MODE is wider than SRC_MODE, the extraction involves
2233 a zero extension
2234
2235 - when MODE is smaller than SRC_MODE, the extraction involves
2236 a truncation (and is thus subject to TARGET_TRULY_NOOP_TRUNCATION).
2237
2238 In other words, this routine performs a computation, whereas the
2239 gen_lowpart* routines are conceptually lvalue or rvalue subreg
2240 operations. */
2241
2242 rtx
2243 extract_low_bits (machine_mode mode, machine_mode src_mode, rtx src)
2244 {
2245 scalar_int_mode int_mode, src_int_mode;
2246
2247 if (mode == src_mode)
2248 return src;
2249
2250 if (CONSTANT_P (src))
2251 {
2252 /* simplify_gen_subreg can't be used here, as if simplify_subreg
2253 fails, it will happily create (subreg (symbol_ref)) or similar
2254 invalid SUBREGs. */
2255 unsigned int byte = subreg_lowpart_offset (mode, src_mode);
2256 rtx ret = simplify_subreg (mode, src, src_mode, byte);
2257 if (ret)
2258 return ret;
2259
2260 if (GET_MODE (src) == VOIDmode
2261 || !validate_subreg (mode, src_mode, src, byte))
2262 return NULL_RTX;
2263
2264 src = force_reg (GET_MODE (src), src);
2265 return gen_rtx_SUBREG (mode, src, byte);
2266 }
2267
2268 if (GET_MODE_CLASS (mode) == MODE_CC || GET_MODE_CLASS (src_mode) == MODE_CC)
2269 return NULL_RTX;
2270
2271 if (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (src_mode)
2272 && targetm.modes_tieable_p (mode, src_mode))
2273 {
2274 rtx x = gen_lowpart_common (mode, src);
2275 if (x)
2276 return x;
2277 }
2278
2279 if (!int_mode_for_mode (src_mode).exists (&src_int_mode)
2280 || !int_mode_for_mode (mode).exists (&int_mode))
2281 return NULL_RTX;
2282
2283 if (!targetm.modes_tieable_p (src_int_mode, src_mode))
2284 return NULL_RTX;
2285 if (!targetm.modes_tieable_p (int_mode, mode))
2286 return NULL_RTX;
2287
2288 src = gen_lowpart (src_int_mode, src);
2289 src = convert_modes (int_mode, src_int_mode, src, true);
2290 src = gen_lowpart (mode, src);
2291 return src;
2292 }
2293 \f
2294 /* Add INC into TARGET. */
2295
2296 void
2297 expand_inc (rtx target, rtx inc)
2298 {
2299 rtx value = expand_binop (GET_MODE (target), add_optab,
2300 target, inc,
2301 target, 0, OPTAB_LIB_WIDEN);
2302 if (value != target)
2303 emit_move_insn (target, value);
2304 }
2305
2306 /* Subtract DEC from TARGET. */
2307
2308 void
2309 expand_dec (rtx target, rtx dec)
2310 {
2311 rtx value = expand_binop (GET_MODE (target), sub_optab,
2312 target, dec,
2313 target, 0, OPTAB_LIB_WIDEN);
2314 if (value != target)
2315 emit_move_insn (target, value);
2316 }
2317 \f
2318 /* Output a shift instruction for expression code CODE,
2319 with SHIFTED being the rtx for the value to shift,
2320 and AMOUNT the rtx for the amount to shift by.
2321 Store the result in the rtx TARGET, if that is convenient.
2322 If UNSIGNEDP is nonzero, do a logical shift; otherwise, arithmetic.
2323 Return the rtx for where the value is.
2324 If that cannot be done, abort the compilation unless MAY_FAIL is true,
2325 in which case 0 is returned. */
2326
2327 static rtx
2328 expand_shift_1 (enum tree_code code, machine_mode mode, rtx shifted,
2329 rtx amount, rtx target, int unsignedp, bool may_fail = false)
2330 {
2331 rtx op1, temp = 0;
2332 int left = (code == LSHIFT_EXPR || code == LROTATE_EXPR);
2333 int rotate = (code == LROTATE_EXPR || code == RROTATE_EXPR);
2334 optab lshift_optab = ashl_optab;
2335 optab rshift_arith_optab = ashr_optab;
2336 optab rshift_uns_optab = lshr_optab;
2337 optab lrotate_optab = rotl_optab;
2338 optab rrotate_optab = rotr_optab;
2339 machine_mode op1_mode;
2340 scalar_mode scalar_mode = GET_MODE_INNER (mode);
2341 int attempt;
2342 bool speed = optimize_insn_for_speed_p ();
2343
2344 op1 = amount;
2345 op1_mode = GET_MODE (op1);
2346
2347 /* Determine whether the shift/rotate amount is a vector, or scalar. If the
2348 shift amount is a vector, use the vector/vector shift patterns. */
2349 if (VECTOR_MODE_P (mode) && VECTOR_MODE_P (op1_mode))
2350 {
2351 lshift_optab = vashl_optab;
2352 rshift_arith_optab = vashr_optab;
2353 rshift_uns_optab = vlshr_optab;
2354 lrotate_optab = vrotl_optab;
2355 rrotate_optab = vrotr_optab;
2356 }
2357
2358 /* Previously detected shift-counts computed by NEGATE_EXPR
2359 and shifted in the other direction; but that does not work
2360 on all machines. */
2361
2362 if (SHIFT_COUNT_TRUNCATED)
2363 {
2364 if (CONST_INT_P (op1)
2365 && ((unsigned HOST_WIDE_INT) INTVAL (op1) >=
2366 (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (scalar_mode)))
2367 op1 = GEN_INT ((unsigned HOST_WIDE_INT) INTVAL (op1)
2368 % GET_MODE_BITSIZE (scalar_mode));
2369 else if (GET_CODE (op1) == SUBREG
2370 && subreg_lowpart_p (op1)
2371 && SCALAR_INT_MODE_P (GET_MODE (SUBREG_REG (op1)))
2372 && SCALAR_INT_MODE_P (GET_MODE (op1)))
2373 op1 = SUBREG_REG (op1);
2374 }
2375
2376 /* Canonicalize rotates by constant amount. If op1 is bitsize / 2,
2377 prefer left rotation, if op1 is from bitsize / 2 + 1 to
2378 bitsize - 1, use other direction of rotate with 1 .. bitsize / 2 - 1
2379 amount instead. */
2380 if (rotate
2381 && CONST_INT_P (op1)
2382 && IN_RANGE (INTVAL (op1), GET_MODE_BITSIZE (scalar_mode) / 2 + left,
2383 GET_MODE_BITSIZE (scalar_mode) - 1))
2384 {
2385 op1 = GEN_INT (GET_MODE_BITSIZE (scalar_mode) - INTVAL (op1));
2386 left = !left;
2387 code = left ? LROTATE_EXPR : RROTATE_EXPR;
2388 }
2389
2390 /* Rotation of 16bit values by 8 bits is effectively equivalent to a bswaphi.
2391 Note that this is not the case for bigger values. For instance a rotation
2392 of 0x01020304 by 16 bits gives 0x03040102 which is different from
2393 0x04030201 (bswapsi). */
2394 if (rotate
2395 && CONST_INT_P (op1)
2396 && INTVAL (op1) == BITS_PER_UNIT
2397 && GET_MODE_SIZE (scalar_mode) == 2
2398 && optab_handler (bswap_optab, HImode) != CODE_FOR_nothing)
2399 return expand_unop (HImode, bswap_optab, shifted, NULL_RTX,
2400 unsignedp);
2401
2402 if (op1 == const0_rtx)
2403 return shifted;
2404
2405 /* Check whether its cheaper to implement a left shift by a constant
2406 bit count by a sequence of additions. */
2407 if (code == LSHIFT_EXPR
2408 && CONST_INT_P (op1)
2409 && INTVAL (op1) > 0
2410 && INTVAL (op1) < GET_MODE_PRECISION (scalar_mode)
2411 && INTVAL (op1) < MAX_BITS_PER_WORD
2412 && (shift_cost (speed, mode, INTVAL (op1))
2413 > INTVAL (op1) * add_cost (speed, mode))
2414 && shift_cost (speed, mode, INTVAL (op1)) != MAX_COST)
2415 {
2416 int i;
2417 for (i = 0; i < INTVAL (op1); i++)
2418 {
2419 temp = force_reg (mode, shifted);
2420 shifted = expand_binop (mode, add_optab, temp, temp, NULL_RTX,
2421 unsignedp, OPTAB_LIB_WIDEN);
2422 }
2423 return shifted;
2424 }
2425
2426 for (attempt = 0; temp == 0 && attempt < 3; attempt++)
2427 {
2428 enum optab_methods methods;
2429
2430 if (attempt == 0)
2431 methods = OPTAB_DIRECT;
2432 else if (attempt == 1)
2433 methods = OPTAB_WIDEN;
2434 else
2435 methods = OPTAB_LIB_WIDEN;
2436
2437 if (rotate)
2438 {
2439 /* Widening does not work for rotation. */
2440 if (methods == OPTAB_WIDEN)
2441 continue;
2442 else if (methods == OPTAB_LIB_WIDEN)
2443 {
2444 /* If we have been unable to open-code this by a rotation,
2445 do it as the IOR of two shifts. I.e., to rotate A
2446 by N bits, compute
2447 (A << N) | ((unsigned) A >> ((-N) & (C - 1)))
2448 where C is the bitsize of A.
2449
2450 It is theoretically possible that the target machine might
2451 not be able to perform either shift and hence we would
2452 be making two libcalls rather than just the one for the
2453 shift (similarly if IOR could not be done). We will allow
2454 this extremely unlikely lossage to avoid complicating the
2455 code below. */
2456
2457 rtx subtarget = target == shifted ? 0 : target;
2458 rtx new_amount, other_amount;
2459 rtx temp1;
2460
2461 new_amount = op1;
2462 if (op1 == const0_rtx)
2463 return shifted;
2464 else if (CONST_INT_P (op1))
2465 other_amount = GEN_INT (GET_MODE_BITSIZE (scalar_mode)
2466 - INTVAL (op1));
2467 else
2468 {
2469 other_amount
2470 = simplify_gen_unary (NEG, GET_MODE (op1),
2471 op1, GET_MODE (op1));
2472 HOST_WIDE_INT mask = GET_MODE_PRECISION (scalar_mode) - 1;
2473 other_amount
2474 = simplify_gen_binary (AND, GET_MODE (op1), other_amount,
2475 gen_int_mode (mask, GET_MODE (op1)));
2476 }
2477
2478 shifted = force_reg (mode, shifted);
2479
2480 temp = expand_shift_1 (left ? LSHIFT_EXPR : RSHIFT_EXPR,
2481 mode, shifted, new_amount, 0, 1);
2482 temp1 = expand_shift_1 (left ? RSHIFT_EXPR : LSHIFT_EXPR,
2483 mode, shifted, other_amount,
2484 subtarget, 1);
2485 return expand_binop (mode, ior_optab, temp, temp1, target,
2486 unsignedp, methods);
2487 }
2488
2489 temp = expand_binop (mode,
2490 left ? lrotate_optab : rrotate_optab,
2491 shifted, op1, target, unsignedp, methods);
2492 }
2493 else if (unsignedp)
2494 temp = expand_binop (mode,
2495 left ? lshift_optab : rshift_uns_optab,
2496 shifted, op1, target, unsignedp, methods);
2497
2498 /* Do arithmetic shifts.
2499 Also, if we are going to widen the operand, we can just as well
2500 use an arithmetic right-shift instead of a logical one. */
2501 if (temp == 0 && ! rotate
2502 && (! unsignedp || (! left && methods == OPTAB_WIDEN)))
2503 {
2504 enum optab_methods methods1 = methods;
2505
2506 /* If trying to widen a log shift to an arithmetic shift,
2507 don't accept an arithmetic shift of the same size. */
2508 if (unsignedp)
2509 methods1 = OPTAB_MUST_WIDEN;
2510
2511 /* Arithmetic shift */
2512
2513 temp = expand_binop (mode,
2514 left ? lshift_optab : rshift_arith_optab,
2515 shifted, op1, target, unsignedp, methods1);
2516 }
2517
2518 /* We used to try extzv here for logical right shifts, but that was
2519 only useful for one machine, the VAX, and caused poor code
2520 generation there for lshrdi3, so the code was deleted and a
2521 define_expand for lshrsi3 was added to vax.md. */
2522 }
2523
2524 gcc_assert (temp != NULL_RTX || may_fail);
2525 return temp;
2526 }
2527
2528 /* Output a shift instruction for expression code CODE,
2529 with SHIFTED being the rtx for the value to shift,
2530 and AMOUNT the amount to shift by.
2531 Store the result in the rtx TARGET, if that is convenient.
2532 If UNSIGNEDP is nonzero, do a logical shift; otherwise, arithmetic.
2533 Return the rtx for where the value is. */
2534
2535 rtx
2536 expand_shift (enum tree_code code, machine_mode mode, rtx shifted,
2537 int amount, rtx target, int unsignedp)
2538 {
2539 return expand_shift_1 (code, mode,
2540 shifted, GEN_INT (amount), target, unsignedp);
2541 }
2542
2543 /* Likewise, but return 0 if that cannot be done. */
2544
2545 static rtx
2546 maybe_expand_shift (enum tree_code code, machine_mode mode, rtx shifted,
2547 int amount, rtx target, int unsignedp)
2548 {
2549 return expand_shift_1 (code, mode,
2550 shifted, GEN_INT (amount), target, unsignedp, true);
2551 }
2552
2553 /* Output a shift instruction for expression code CODE,
2554 with SHIFTED being the rtx for the value to shift,
2555 and AMOUNT the tree for the amount to shift by.
2556 Store the result in the rtx TARGET, if that is convenient.
2557 If UNSIGNEDP is nonzero, do a logical shift; otherwise, arithmetic.
2558 Return the rtx for where the value is. */
2559
2560 rtx
2561 expand_variable_shift (enum tree_code code, machine_mode mode, rtx shifted,
2562 tree amount, rtx target, int unsignedp)
2563 {
2564 return expand_shift_1 (code, mode,
2565 shifted, expand_normal (amount), target, unsignedp);
2566 }
2567
2568 \f
2569 static void synth_mult (struct algorithm *, unsigned HOST_WIDE_INT,
2570 const struct mult_cost *, machine_mode mode);
2571 static rtx expand_mult_const (machine_mode, rtx, HOST_WIDE_INT, rtx,
2572 const struct algorithm *, enum mult_variant);
2573 static unsigned HOST_WIDE_INT invert_mod2n (unsigned HOST_WIDE_INT, int);
2574 static rtx extract_high_half (scalar_int_mode, rtx);
2575 static rtx expmed_mult_highpart (scalar_int_mode, rtx, rtx, rtx, int, int);
2576 static rtx expmed_mult_highpart_optab (scalar_int_mode, rtx, rtx, rtx,
2577 int, int);
2578 /* Compute and return the best algorithm for multiplying by T.
2579 The algorithm must cost less than cost_limit
2580 If retval.cost >= COST_LIMIT, no algorithm was found and all
2581 other field of the returned struct are undefined.
2582 MODE is the machine mode of the multiplication. */
2583
2584 static void
2585 synth_mult (struct algorithm *alg_out, unsigned HOST_WIDE_INT t,
2586 const struct mult_cost *cost_limit, machine_mode mode)
2587 {
2588 int m;
2589 struct algorithm *alg_in, *best_alg;
2590 struct mult_cost best_cost;
2591 struct mult_cost new_limit;
2592 int op_cost, op_latency;
2593 unsigned HOST_WIDE_INT orig_t = t;
2594 unsigned HOST_WIDE_INT q;
2595 int maxm, hash_index;
2596 bool cache_hit = false;
2597 enum alg_code cache_alg = alg_zero;
2598 bool speed = optimize_insn_for_speed_p ();
2599 scalar_int_mode imode;
2600 struct alg_hash_entry *entry_ptr;
2601
2602 /* Indicate that no algorithm is yet found. If no algorithm
2603 is found, this value will be returned and indicate failure. */
2604 alg_out->cost.cost = cost_limit->cost + 1;
2605 alg_out->cost.latency = cost_limit->latency + 1;
2606
2607 if (cost_limit->cost < 0
2608 || (cost_limit->cost == 0 && cost_limit->latency <= 0))
2609 return;
2610
2611 /* Be prepared for vector modes. */
2612 imode = as_a <scalar_int_mode> (GET_MODE_INNER (mode));
2613
2614 maxm = MIN (BITS_PER_WORD, GET_MODE_BITSIZE (imode));
2615
2616 /* Restrict the bits of "t" to the multiplication's mode. */
2617 t &= GET_MODE_MASK (imode);
2618
2619 /* t == 1 can be done in zero cost. */
2620 if (t == 1)
2621 {
2622 alg_out->ops = 1;
2623 alg_out->cost.cost = 0;
2624 alg_out->cost.latency = 0;
2625 alg_out->op[0] = alg_m;
2626 return;
2627 }
2628
2629 /* t == 0 sometimes has a cost. If it does and it exceeds our limit,
2630 fail now. */
2631 if (t == 0)
2632 {
2633 if (MULT_COST_LESS (cost_limit, zero_cost (speed)))
2634 return;
2635 else
2636 {
2637 alg_out->ops = 1;
2638 alg_out->cost.cost = zero_cost (speed);
2639 alg_out->cost.latency = zero_cost (speed);
2640 alg_out->op[0] = alg_zero;
2641 return;
2642 }
2643 }
2644
2645 /* We'll be needing a couple extra algorithm structures now. */
2646
2647 alg_in = XALLOCA (struct algorithm);
2648 best_alg = XALLOCA (struct algorithm);
2649 best_cost = *cost_limit;
2650
2651 /* Compute the hash index. */
2652 hash_index = (t ^ (unsigned int) mode ^ (speed * 256)) % NUM_ALG_HASH_ENTRIES;
2653
2654 /* See if we already know what to do for T. */
2655 entry_ptr = alg_hash_entry_ptr (hash_index);
2656 if (entry_ptr->t == t
2657 && entry_ptr->mode == mode
2658 && entry_ptr->speed == speed
2659 && entry_ptr->alg != alg_unknown)
2660 {
2661 cache_alg = entry_ptr->alg;
2662
2663 if (cache_alg == alg_impossible)
2664 {
2665 /* The cache tells us that it's impossible to synthesize
2666 multiplication by T within entry_ptr->cost. */
2667 if (!CHEAPER_MULT_COST (&entry_ptr->cost, cost_limit))
2668 /* COST_LIMIT is at least as restrictive as the one
2669 recorded in the hash table, in which case we have no
2670 hope of synthesizing a multiplication. Just
2671 return. */
2672 return;
2673
2674 /* If we get here, COST_LIMIT is less restrictive than the
2675 one recorded in the hash table, so we may be able to
2676 synthesize a multiplication. Proceed as if we didn't
2677 have the cache entry. */
2678 }
2679 else
2680 {
2681 if (CHEAPER_MULT_COST (cost_limit, &entry_ptr->cost))
2682 /* The cached algorithm shows that this multiplication
2683 requires more cost than COST_LIMIT. Just return. This
2684 way, we don't clobber this cache entry with
2685 alg_impossible but retain useful information. */
2686 return;
2687
2688 cache_hit = true;
2689
2690 switch (cache_alg)
2691 {
2692 case alg_shift:
2693 goto do_alg_shift;
2694
2695 case alg_add_t_m2:
2696 case alg_sub_t_m2:
2697 goto do_alg_addsub_t_m2;
2698
2699 case alg_add_factor:
2700 case alg_sub_factor:
2701 goto do_alg_addsub_factor;
2702
2703 case alg_add_t2_m:
2704 goto do_alg_add_t2_m;
2705
2706 case alg_sub_t2_m:
2707 goto do_alg_sub_t2_m;
2708
2709 default:
2710 gcc_unreachable ();
2711 }
2712 }
2713 }
2714
2715 /* If we have a group of zero bits at the low-order part of T, try
2716 multiplying by the remaining bits and then doing a shift. */
2717
2718 if ((t & 1) == 0)
2719 {
2720 do_alg_shift:
2721 m = ctz_or_zero (t); /* m = number of low zero bits */
2722 if (m < maxm)
2723 {
2724 q = t >> m;
2725 /* The function expand_shift will choose between a shift and
2726 a sequence of additions, so the observed cost is given as
2727 MIN (m * add_cost(speed, mode), shift_cost(speed, mode, m)). */
2728 op_cost = m * add_cost (speed, mode);
2729 if (shift_cost (speed, mode, m) < op_cost)
2730 op_cost = shift_cost (speed, mode, m);
2731 new_limit.cost = best_cost.cost - op_cost;
2732 new_limit.latency = best_cost.latency - op_cost;
2733 synth_mult (alg_in, q, &new_limit, mode);
2734
2735 alg_in->cost.cost += op_cost;
2736 alg_in->cost.latency += op_cost;
2737 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2738 {
2739 best_cost = alg_in->cost;
2740 std::swap (alg_in, best_alg);
2741 best_alg->log[best_alg->ops] = m;
2742 best_alg->op[best_alg->ops] = alg_shift;
2743 }
2744
2745 /* See if treating ORIG_T as a signed number yields a better
2746 sequence. Try this sequence only for a negative ORIG_T
2747 as it would be useless for a non-negative ORIG_T. */
2748 if ((HOST_WIDE_INT) orig_t < 0)
2749 {
2750 /* Shift ORIG_T as follows because a right shift of a
2751 negative-valued signed type is implementation
2752 defined. */
2753 q = ~(~orig_t >> m);
2754 /* The function expand_shift will choose between a shift
2755 and a sequence of additions, so the observed cost is
2756 given as MIN (m * add_cost(speed, mode),
2757 shift_cost(speed, mode, m)). */
2758 op_cost = m * add_cost (speed, mode);
2759 if (shift_cost (speed, mode, m) < op_cost)
2760 op_cost = shift_cost (speed, mode, m);
2761 new_limit.cost = best_cost.cost - op_cost;
2762 new_limit.latency = best_cost.latency - op_cost;
2763 synth_mult (alg_in, q, &new_limit, mode);
2764
2765 alg_in->cost.cost += op_cost;
2766 alg_in->cost.latency += op_cost;
2767 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2768 {
2769 best_cost = alg_in->cost;
2770 std::swap (alg_in, best_alg);
2771 best_alg->log[best_alg->ops] = m;
2772 best_alg->op[best_alg->ops] = alg_shift;
2773 }
2774 }
2775 }
2776 if (cache_hit)
2777 goto done;
2778 }
2779
2780 /* If we have an odd number, add or subtract one. */
2781 if ((t & 1) != 0)
2782 {
2783 unsigned HOST_WIDE_INT w;
2784
2785 do_alg_addsub_t_m2:
2786 for (w = 1; (w & t) != 0; w <<= 1)
2787 ;
2788 /* If T was -1, then W will be zero after the loop. This is another
2789 case where T ends with ...111. Handling this with (T + 1) and
2790 subtract 1 produces slightly better code and results in algorithm
2791 selection much faster than treating it like the ...0111 case
2792 below. */
2793 if (w == 0
2794 || (w > 2
2795 /* Reject the case where t is 3.
2796 Thus we prefer addition in that case. */
2797 && t != 3))
2798 {
2799 /* T ends with ...111. Multiply by (T + 1) and subtract T. */
2800
2801 op_cost = add_cost (speed, mode);
2802 new_limit.cost = best_cost.cost - op_cost;
2803 new_limit.latency = best_cost.latency - op_cost;
2804 synth_mult (alg_in, t + 1, &new_limit, mode);
2805
2806 alg_in->cost.cost += op_cost;
2807 alg_in->cost.latency += op_cost;
2808 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2809 {
2810 best_cost = alg_in->cost;
2811 std::swap (alg_in, best_alg);
2812 best_alg->log[best_alg->ops] = 0;
2813 best_alg->op[best_alg->ops] = alg_sub_t_m2;
2814 }
2815 }
2816 else
2817 {
2818 /* T ends with ...01 or ...011. Multiply by (T - 1) and add T. */
2819
2820 op_cost = add_cost (speed, mode);
2821 new_limit.cost = best_cost.cost - op_cost;
2822 new_limit.latency = best_cost.latency - op_cost;
2823 synth_mult (alg_in, t - 1, &new_limit, mode);
2824
2825 alg_in->cost.cost += op_cost;
2826 alg_in->cost.latency += op_cost;
2827 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2828 {
2829 best_cost = alg_in->cost;
2830 std::swap (alg_in, best_alg);
2831 best_alg->log[best_alg->ops] = 0;
2832 best_alg->op[best_alg->ops] = alg_add_t_m2;
2833 }
2834 }
2835
2836 /* We may be able to calculate a * -7, a * -15, a * -31, etc
2837 quickly with a - a * n for some appropriate constant n. */
2838 m = exact_log2 (-orig_t + 1);
2839 if (m >= 0 && m < maxm)
2840 {
2841 op_cost = add_cost (speed, mode) + shift_cost (speed, mode, m);
2842 /* If the target has a cheap shift-and-subtract insn use
2843 that in preference to a shift insn followed by a sub insn.
2844 Assume that the shift-and-sub is "atomic" with a latency
2845 equal to it's cost, otherwise assume that on superscalar
2846 hardware the shift may be executed concurrently with the
2847 earlier steps in the algorithm. */
2848 if (shiftsub1_cost (speed, mode, m) <= op_cost)
2849 {
2850 op_cost = shiftsub1_cost (speed, mode, m);
2851 op_latency = op_cost;
2852 }
2853 else
2854 op_latency = add_cost (speed, mode);
2855
2856 new_limit.cost = best_cost.cost - op_cost;
2857 new_limit.latency = best_cost.latency - op_latency;
2858 synth_mult (alg_in, (unsigned HOST_WIDE_INT) (-orig_t + 1) >> m,
2859 &new_limit, mode);
2860
2861 alg_in->cost.cost += op_cost;
2862 alg_in->cost.latency += op_latency;
2863 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2864 {
2865 best_cost = alg_in->cost;
2866 std::swap (alg_in, best_alg);
2867 best_alg->log[best_alg->ops] = m;
2868 best_alg->op[best_alg->ops] = alg_sub_t_m2;
2869 }
2870 }
2871
2872 if (cache_hit)
2873 goto done;
2874 }
2875
2876 /* Look for factors of t of the form
2877 t = q(2**m +- 1), 2 <= m <= floor(log2(t - 1)).
2878 If we find such a factor, we can multiply by t using an algorithm that
2879 multiplies by q, shift the result by m and add/subtract it to itself.
2880
2881 We search for large factors first and loop down, even if large factors
2882 are less probable than small; if we find a large factor we will find a
2883 good sequence quickly, and therefore be able to prune (by decreasing
2884 COST_LIMIT) the search. */
2885
2886 do_alg_addsub_factor:
2887 for (m = floor_log2 (t - 1); m >= 2; m--)
2888 {
2889 unsigned HOST_WIDE_INT d;
2890
2891 d = (HOST_WIDE_INT_1U << m) + 1;
2892 if (t % d == 0 && t > d && m < maxm
2893 && (!cache_hit || cache_alg == alg_add_factor))
2894 {
2895 op_cost = add_cost (speed, mode) + shift_cost (speed, mode, m);
2896 if (shiftadd_cost (speed, mode, m) <= op_cost)
2897 op_cost = shiftadd_cost (speed, mode, m);
2898
2899 op_latency = op_cost;
2900
2901
2902 new_limit.cost = best_cost.cost - op_cost;
2903 new_limit.latency = best_cost.latency - op_latency;
2904 synth_mult (alg_in, t / d, &new_limit, mode);
2905
2906 alg_in->cost.cost += op_cost;
2907 alg_in->cost.latency += op_latency;
2908 if (alg_in->cost.latency < op_cost)
2909 alg_in->cost.latency = op_cost;
2910 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2911 {
2912 best_cost = alg_in->cost;
2913 std::swap (alg_in, best_alg);
2914 best_alg->log[best_alg->ops] = m;
2915 best_alg->op[best_alg->ops] = alg_add_factor;
2916 }
2917 /* Other factors will have been taken care of in the recursion. */
2918 break;
2919 }
2920
2921 d = (HOST_WIDE_INT_1U << m) - 1;
2922 if (t % d == 0 && t > d && m < maxm
2923 && (!cache_hit || cache_alg == alg_sub_factor))
2924 {
2925 op_cost = add_cost (speed, mode) + shift_cost (speed, mode, m);
2926 if (shiftsub0_cost (speed, mode, m) <= op_cost)
2927 op_cost = shiftsub0_cost (speed, mode, m);
2928
2929 op_latency = op_cost;
2930
2931 new_limit.cost = best_cost.cost - op_cost;
2932 new_limit.latency = best_cost.latency - op_latency;
2933 synth_mult (alg_in, t / d, &new_limit, mode);
2934
2935 alg_in->cost.cost += op_cost;
2936 alg_in->cost.latency += op_latency;
2937 if (alg_in->cost.latency < op_cost)
2938 alg_in->cost.latency = op_cost;
2939 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2940 {
2941 best_cost = alg_in->cost;
2942 std::swap (alg_in, best_alg);
2943 best_alg->log[best_alg->ops] = m;
2944 best_alg->op[best_alg->ops] = alg_sub_factor;
2945 }
2946 break;
2947 }
2948 }
2949 if (cache_hit)
2950 goto done;
2951
2952 /* Try shift-and-add (load effective address) instructions,
2953 i.e. do a*3, a*5, a*9. */
2954 if ((t & 1) != 0)
2955 {
2956 do_alg_add_t2_m:
2957 q = t - 1;
2958 m = ctz_hwi (q);
2959 if (q && m < maxm)
2960 {
2961 op_cost = shiftadd_cost (speed, mode, m);
2962 new_limit.cost = best_cost.cost - op_cost;
2963 new_limit.latency = best_cost.latency - op_cost;
2964 synth_mult (alg_in, (t - 1) >> m, &new_limit, mode);
2965
2966 alg_in->cost.cost += op_cost;
2967 alg_in->cost.latency += op_cost;
2968 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2969 {
2970 best_cost = alg_in->cost;
2971 std::swap (alg_in, best_alg);
2972 best_alg->log[best_alg->ops] = m;
2973 best_alg->op[best_alg->ops] = alg_add_t2_m;
2974 }
2975 }
2976 if (cache_hit)
2977 goto done;
2978
2979 do_alg_sub_t2_m:
2980 q = t + 1;
2981 m = ctz_hwi (q);
2982 if (q && m < maxm)
2983 {
2984 op_cost = shiftsub0_cost (speed, mode, m);
2985 new_limit.cost = best_cost.cost - op_cost;
2986 new_limit.latency = best_cost.latency - op_cost;
2987 synth_mult (alg_in, (t + 1) >> m, &new_limit, mode);
2988
2989 alg_in->cost.cost += op_cost;
2990 alg_in->cost.latency += op_cost;
2991 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2992 {
2993 best_cost = alg_in->cost;
2994 std::swap (alg_in, best_alg);
2995 best_alg->log[best_alg->ops] = m;
2996 best_alg->op[best_alg->ops] = alg_sub_t2_m;
2997 }
2998 }
2999 if (cache_hit)
3000 goto done;
3001 }
3002
3003 done:
3004 /* If best_cost has not decreased, we have not found any algorithm. */
3005 if (!CHEAPER_MULT_COST (&best_cost, cost_limit))
3006 {
3007 /* We failed to find an algorithm. Record alg_impossible for
3008 this case (that is, <T, MODE, COST_LIMIT>) so that next time
3009 we are asked to find an algorithm for T within the same or
3010 lower COST_LIMIT, we can immediately return to the
3011 caller. */
3012 entry_ptr->t = t;
3013 entry_ptr->mode = mode;
3014 entry_ptr->speed = speed;
3015 entry_ptr->alg = alg_impossible;
3016 entry_ptr->cost = *cost_limit;
3017 return;
3018 }
3019
3020 /* Cache the result. */
3021 if (!cache_hit)
3022 {
3023 entry_ptr->t = t;
3024 entry_ptr->mode = mode;
3025 entry_ptr->speed = speed;
3026 entry_ptr->alg = best_alg->op[best_alg->ops];
3027 entry_ptr->cost.cost = best_cost.cost;
3028 entry_ptr->cost.latency = best_cost.latency;
3029 }
3030
3031 /* If we are getting a too long sequence for `struct algorithm'
3032 to record, make this search fail. */
3033 if (best_alg->ops == MAX_BITS_PER_WORD)
3034 return;
3035
3036 /* Copy the algorithm from temporary space to the space at alg_out.
3037 We avoid using structure assignment because the majority of
3038 best_alg is normally undefined, and this is a critical function. */
3039 alg_out->ops = best_alg->ops + 1;
3040 alg_out->cost = best_cost;
3041 memcpy (alg_out->op, best_alg->op,
3042 alg_out->ops * sizeof *alg_out->op);
3043 memcpy (alg_out->log, best_alg->log,
3044 alg_out->ops * sizeof *alg_out->log);
3045 }
3046 \f
3047 /* Find the cheapest way of multiplying a value of mode MODE by VAL.
3048 Try three variations:
3049
3050 - a shift/add sequence based on VAL itself
3051 - a shift/add sequence based on -VAL, followed by a negation
3052 - a shift/add sequence based on VAL - 1, followed by an addition.
3053
3054 Return true if the cheapest of these cost less than MULT_COST,
3055 describing the algorithm in *ALG and final fixup in *VARIANT. */
3056
3057 bool
3058 choose_mult_variant (machine_mode mode, HOST_WIDE_INT val,
3059 struct algorithm *alg, enum mult_variant *variant,
3060 int mult_cost)
3061 {
3062 struct algorithm alg2;
3063 struct mult_cost limit;
3064 int op_cost;
3065 bool speed = optimize_insn_for_speed_p ();
3066
3067 /* Fail quickly for impossible bounds. */
3068 if (mult_cost < 0)
3069 return false;
3070
3071 /* Ensure that mult_cost provides a reasonable upper bound.
3072 Any constant multiplication can be performed with less
3073 than 2 * bits additions. */
3074 op_cost = 2 * GET_MODE_UNIT_BITSIZE (mode) * add_cost (speed, mode);
3075 if (mult_cost > op_cost)
3076 mult_cost = op_cost;
3077
3078 *variant = basic_variant;
3079 limit.cost = mult_cost;
3080 limit.latency = mult_cost;
3081 synth_mult (alg, val, &limit, mode);
3082
3083 /* This works only if the inverted value actually fits in an
3084 `unsigned int' */
3085 if (HOST_BITS_PER_INT >= GET_MODE_UNIT_BITSIZE (mode))
3086 {
3087 op_cost = neg_cost (speed, mode);
3088 if (MULT_COST_LESS (&alg->cost, mult_cost))
3089 {
3090 limit.cost = alg->cost.cost - op_cost;
3091 limit.latency = alg->cost.latency - op_cost;
3092 }
3093 else
3094 {
3095 limit.cost = mult_cost - op_cost;
3096 limit.latency = mult_cost - op_cost;
3097 }
3098
3099 synth_mult (&alg2, -val, &limit, mode);
3100 alg2.cost.cost += op_cost;
3101 alg2.cost.latency += op_cost;
3102 if (CHEAPER_MULT_COST (&alg2.cost, &alg->cost))
3103 *alg = alg2, *variant = negate_variant;
3104 }
3105
3106 /* This proves very useful for division-by-constant. */
3107 op_cost = add_cost (speed, mode);
3108 if (MULT_COST_LESS (&alg->cost, mult_cost))
3109 {
3110 limit.cost = alg->cost.cost - op_cost;
3111 limit.latency = alg->cost.latency - op_cost;
3112 }
3113 else
3114 {
3115 limit.cost = mult_cost - op_cost;
3116 limit.latency = mult_cost - op_cost;
3117 }
3118
3119 synth_mult (&alg2, val - 1, &limit, mode);
3120 alg2.cost.cost += op_cost;
3121 alg2.cost.latency += op_cost;
3122 if (CHEAPER_MULT_COST (&alg2.cost, &alg->cost))
3123 *alg = alg2, *variant = add_variant;
3124
3125 return MULT_COST_LESS (&alg->cost, mult_cost);
3126 }
3127
3128 /* A subroutine of expand_mult, used for constant multiplications.
3129 Multiply OP0 by VAL in mode MODE, storing the result in TARGET if
3130 convenient. Use the shift/add sequence described by ALG and apply
3131 the final fixup specified by VARIANT. */
3132
3133 static rtx
3134 expand_mult_const (machine_mode mode, rtx op0, HOST_WIDE_INT val,
3135 rtx target, const struct algorithm *alg,
3136 enum mult_variant variant)
3137 {
3138 unsigned HOST_WIDE_INT val_so_far;
3139 rtx_insn *insn;
3140 rtx accum, tem;
3141 int opno;
3142 machine_mode nmode;
3143
3144 /* Avoid referencing memory over and over and invalid sharing
3145 on SUBREGs. */
3146 op0 = force_reg (mode, op0);
3147
3148 /* ACCUM starts out either as OP0 or as a zero, depending on
3149 the first operation. */
3150
3151 if (alg->op[0] == alg_zero)
3152 {
3153 accum = copy_to_mode_reg (mode, CONST0_RTX (mode));
3154 val_so_far = 0;
3155 }
3156 else if (alg->op[0] == alg_m)
3157 {
3158 accum = copy_to_mode_reg (mode, op0);
3159 val_so_far = 1;
3160 }
3161 else
3162 gcc_unreachable ();
3163
3164 for (opno = 1; opno < alg->ops; opno++)
3165 {
3166 int log = alg->log[opno];
3167 rtx shift_subtarget = optimize ? 0 : accum;
3168 rtx add_target
3169 = (opno == alg->ops - 1 && target != 0 && variant != add_variant
3170 && !optimize)
3171 ? target : 0;
3172 rtx accum_target = optimize ? 0 : accum;
3173 rtx accum_inner;
3174
3175 switch (alg->op[opno])
3176 {
3177 case alg_shift:
3178 tem = expand_shift (LSHIFT_EXPR, mode, accum, log, NULL_RTX, 0);
3179 /* REG_EQUAL note will be attached to the following insn. */
3180 emit_move_insn (accum, tem);
3181 val_so_far <<= log;
3182 break;
3183
3184 case alg_add_t_m2:
3185 tem = expand_shift (LSHIFT_EXPR, mode, op0, log, NULL_RTX, 0);
3186 accum = force_operand (gen_rtx_PLUS (mode, accum, tem),
3187 add_target ? add_target : accum_target);
3188 val_so_far += HOST_WIDE_INT_1U << log;
3189 break;
3190
3191 case alg_sub_t_m2:
3192 tem = expand_shift (LSHIFT_EXPR, mode, op0, log, NULL_RTX, 0);
3193 accum = force_operand (gen_rtx_MINUS (mode, accum, tem),
3194 add_target ? add_target : accum_target);
3195 val_so_far -= HOST_WIDE_INT_1U << log;
3196 break;
3197
3198 case alg_add_t2_m:
3199 accum = expand_shift (LSHIFT_EXPR, mode, accum,
3200 log, shift_subtarget, 0);
3201 accum = force_operand (gen_rtx_PLUS (mode, accum, op0),
3202 add_target ? add_target : accum_target);
3203 val_so_far = (val_so_far << log) + 1;
3204 break;
3205
3206 case alg_sub_t2_m:
3207 accum = expand_shift (LSHIFT_EXPR, mode, accum,
3208 log, shift_subtarget, 0);
3209 accum = force_operand (gen_rtx_MINUS (mode, accum, op0),
3210 add_target ? add_target : accum_target);
3211 val_so_far = (val_so_far << log) - 1;
3212 break;
3213
3214 case alg_add_factor:
3215 tem = expand_shift (LSHIFT_EXPR, mode, accum, log, NULL_RTX, 0);
3216 accum = force_operand (gen_rtx_PLUS (mode, accum, tem),
3217 add_target ? add_target : accum_target);
3218 val_so_far += val_so_far << log;
3219 break;
3220
3221 case alg_sub_factor:
3222 tem = expand_shift (LSHIFT_EXPR, mode, accum, log, NULL_RTX, 0);
3223 accum = force_operand (gen_rtx_MINUS (mode, tem, accum),
3224 (add_target
3225 ? add_target : (optimize ? 0 : tem)));
3226 val_so_far = (val_so_far << log) - val_so_far;
3227 break;
3228
3229 default:
3230 gcc_unreachable ();
3231 }
3232
3233 if (SCALAR_INT_MODE_P (mode))
3234 {
3235 /* Write a REG_EQUAL note on the last insn so that we can cse
3236 multiplication sequences. Note that if ACCUM is a SUBREG,
3237 we've set the inner register and must properly indicate that. */
3238 tem = op0, nmode = mode;
3239 accum_inner = accum;
3240 if (GET_CODE (accum) == SUBREG)
3241 {
3242 accum_inner = SUBREG_REG (accum);
3243 nmode = GET_MODE (accum_inner);
3244 tem = gen_lowpart (nmode, op0);
3245 }
3246
3247 insn = get_last_insn ();
3248 set_dst_reg_note (insn, REG_EQUAL,
3249 gen_rtx_MULT (nmode, tem,
3250 gen_int_mode (val_so_far, nmode)),
3251 accum_inner);
3252 }
3253 }
3254
3255 if (variant == negate_variant)
3256 {
3257 val_so_far = -val_so_far;
3258 accum = expand_unop (mode, neg_optab, accum, target, 0);
3259 }
3260 else if (variant == add_variant)
3261 {
3262 val_so_far = val_so_far + 1;
3263 accum = force_operand (gen_rtx_PLUS (mode, accum, op0), target);
3264 }
3265
3266 /* Compare only the bits of val and val_so_far that are significant
3267 in the result mode, to avoid sign-/zero-extension confusion. */
3268 nmode = GET_MODE_INNER (mode);
3269 val &= GET_MODE_MASK (nmode);
3270 val_so_far &= GET_MODE_MASK (nmode);
3271 gcc_assert (val == (HOST_WIDE_INT) val_so_far);
3272
3273 return accum;
3274 }
3275
3276 /* Perform a multiplication and return an rtx for the result.
3277 MODE is mode of value; OP0 and OP1 are what to multiply (rtx's);
3278 TARGET is a suggestion for where to store the result (an rtx).
3279
3280 We check specially for a constant integer as OP1.
3281 If you want this check for OP0 as well, then before calling
3282 you should swap the two operands if OP0 would be constant. */
3283
3284 rtx
3285 expand_mult (machine_mode mode, rtx op0, rtx op1, rtx target,
3286 int unsignedp)
3287 {
3288 enum mult_variant variant;
3289 struct algorithm algorithm;
3290 rtx scalar_op1;
3291 int max_cost;
3292 bool speed = optimize_insn_for_speed_p ();
3293 bool do_trapv = flag_trapv && SCALAR_INT_MODE_P (mode) && !unsignedp;
3294
3295 if (CONSTANT_P (op0))
3296 std::swap (op0, op1);
3297
3298 /* For vectors, there are several simplifications that can be made if
3299 all elements of the vector constant are identical. */
3300 scalar_op1 = unwrap_const_vec_duplicate (op1);
3301
3302 if (INTEGRAL_MODE_P (mode))
3303 {
3304 rtx fake_reg;
3305 HOST_WIDE_INT coeff;
3306 bool is_neg;
3307 int mode_bitsize;
3308
3309 if (op1 == CONST0_RTX (mode))
3310 return op1;
3311 if (op1 == CONST1_RTX (mode))
3312 return op0;
3313 if (op1 == CONSTM1_RTX (mode))
3314 return expand_unop (mode, do_trapv ? negv_optab : neg_optab,
3315 op0, target, 0);
3316
3317 if (do_trapv)
3318 goto skip_synth;
3319
3320 /* If mode is integer vector mode, check if the backend supports
3321 vector lshift (by scalar or vector) at all. If not, we can't use
3322 synthetized multiply. */
3323 if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
3324 && optab_handler (vashl_optab, mode) == CODE_FOR_nothing
3325 && optab_handler (ashl_optab, mode) == CODE_FOR_nothing)
3326 goto skip_synth;
3327
3328 /* These are the operations that are potentially turned into
3329 a sequence of shifts and additions. */
3330 mode_bitsize = GET_MODE_UNIT_BITSIZE (mode);
3331
3332 /* synth_mult does an `unsigned int' multiply. As long as the mode is
3333 less than or equal in size to `unsigned int' this doesn't matter.
3334 If the mode is larger than `unsigned int', then synth_mult works
3335 only if the constant value exactly fits in an `unsigned int' without
3336 any truncation. This means that multiplying by negative values does
3337 not work; results are off by 2^32 on a 32 bit machine. */
3338 if (CONST_INT_P (scalar_op1))
3339 {
3340 coeff = INTVAL (scalar_op1);
3341 is_neg = coeff < 0;
3342 }
3343 #if TARGET_SUPPORTS_WIDE_INT
3344 else if (CONST_WIDE_INT_P (scalar_op1))
3345 #else
3346 else if (CONST_DOUBLE_AS_INT_P (scalar_op1))
3347 #endif
3348 {
3349 int shift = wi::exact_log2 (rtx_mode_t (scalar_op1, mode));
3350 /* Perfect power of 2 (other than 1, which is handled above). */
3351 if (shift > 0)
3352 return expand_shift (LSHIFT_EXPR, mode, op0,
3353 shift, target, unsignedp);
3354 else
3355 goto skip_synth;
3356 }
3357 else
3358 goto skip_synth;
3359
3360 /* We used to test optimize here, on the grounds that it's better to
3361 produce a smaller program when -O is not used. But this causes
3362 such a terrible slowdown sometimes that it seems better to always
3363 use synth_mult. */
3364
3365 /* Special case powers of two. */
3366 if (EXACT_POWER_OF_2_OR_ZERO_P (coeff)
3367 && !(is_neg && mode_bitsize > HOST_BITS_PER_WIDE_INT))
3368 return expand_shift (LSHIFT_EXPR, mode, op0,
3369 floor_log2 (coeff), target, unsignedp);
3370
3371 fake_reg = gen_raw_REG (mode, LAST_VIRTUAL_REGISTER + 1);
3372
3373 /* Attempt to handle multiplication of DImode values by negative
3374 coefficients, by performing the multiplication by a positive
3375 multiplier and then inverting the result. */
3376 if (is_neg && mode_bitsize > HOST_BITS_PER_WIDE_INT)
3377 {
3378 /* Its safe to use -coeff even for INT_MIN, as the
3379 result is interpreted as an unsigned coefficient.
3380 Exclude cost of op0 from max_cost to match the cost
3381 calculation of the synth_mult. */
3382 coeff = -(unsigned HOST_WIDE_INT) coeff;
3383 max_cost = (set_src_cost (gen_rtx_MULT (mode, fake_reg, op1),
3384 mode, speed)
3385 - neg_cost (speed, mode));
3386 if (max_cost <= 0)
3387 goto skip_synth;
3388
3389 /* Special case powers of two. */
3390 if (EXACT_POWER_OF_2_OR_ZERO_P (coeff))
3391 {
3392 rtx temp = expand_shift (LSHIFT_EXPR, mode, op0,
3393 floor_log2 (coeff), target, unsignedp);
3394 return expand_unop (mode, neg_optab, temp, target, 0);
3395 }
3396
3397 if (choose_mult_variant (mode, coeff, &algorithm, &variant,
3398 max_cost))
3399 {
3400 rtx temp = expand_mult_const (mode, op0, coeff, NULL_RTX,
3401 &algorithm, variant);
3402 return expand_unop (mode, neg_optab, temp, target, 0);
3403 }
3404 goto skip_synth;
3405 }
3406
3407 /* Exclude cost of op0 from max_cost to match the cost
3408 calculation of the synth_mult. */
3409 max_cost = set_src_cost (gen_rtx_MULT (mode, fake_reg, op1), mode, speed);
3410 if (choose_mult_variant (mode, coeff, &algorithm, &variant, max_cost))
3411 return expand_mult_const (mode, op0, coeff, target,
3412 &algorithm, variant);
3413 }
3414 skip_synth:
3415
3416 /* Expand x*2.0 as x+x. */
3417 if (CONST_DOUBLE_AS_FLOAT_P (scalar_op1)
3418 && real_equal (CONST_DOUBLE_REAL_VALUE (scalar_op1), &dconst2))
3419 {
3420 op0 = force_reg (GET_MODE (op0), op0);
3421 return expand_binop (mode, add_optab, op0, op0,
3422 target, unsignedp, OPTAB_LIB_WIDEN);
3423 }
3424
3425 /* This used to use umul_optab if unsigned, but for non-widening multiply
3426 there is no difference between signed and unsigned. */
3427 op0 = expand_binop (mode, do_trapv ? smulv_optab : smul_optab,
3428 op0, op1, target, unsignedp, OPTAB_LIB_WIDEN);
3429 gcc_assert (op0);
3430 return op0;
3431 }
3432
3433 /* Return a cost estimate for multiplying a register by the given
3434 COEFFicient in the given MODE and SPEED. */
3435
3436 int
3437 mult_by_coeff_cost (HOST_WIDE_INT coeff, machine_mode mode, bool speed)
3438 {
3439 int max_cost;
3440 struct algorithm algorithm;
3441 enum mult_variant variant;
3442
3443 rtx fake_reg = gen_raw_REG (mode, LAST_VIRTUAL_REGISTER + 1);
3444 max_cost = set_src_cost (gen_rtx_MULT (mode, fake_reg, fake_reg),
3445 mode, speed);
3446 if (choose_mult_variant (mode, coeff, &algorithm, &variant, max_cost))
3447 return algorithm.cost.cost;
3448 else
3449 return max_cost;
3450 }
3451
3452 /* Perform a widening multiplication and return an rtx for the result.
3453 MODE is mode of value; OP0 and OP1 are what to multiply (rtx's);
3454 TARGET is a suggestion for where to store the result (an rtx).
3455 THIS_OPTAB is the optab we should use, it must be either umul_widen_optab
3456 or smul_widen_optab.
3457
3458 We check specially for a constant integer as OP1, comparing the
3459 cost of a widening multiply against the cost of a sequence of shifts
3460 and adds. */
3461
3462 rtx
3463 expand_widening_mult (machine_mode mode, rtx op0, rtx op1, rtx target,
3464 int unsignedp, optab this_optab)
3465 {
3466 bool speed = optimize_insn_for_speed_p ();
3467 rtx cop1;
3468
3469 if (CONST_INT_P (op1)
3470 && GET_MODE (op0) != VOIDmode
3471 && (cop1 = convert_modes (mode, GET_MODE (op0), op1,
3472 this_optab == umul_widen_optab))
3473 && CONST_INT_P (cop1)
3474 && (INTVAL (cop1) >= 0
3475 || HWI_COMPUTABLE_MODE_P (mode)))
3476 {
3477 HOST_WIDE_INT coeff = INTVAL (cop1);
3478 int max_cost;
3479 enum mult_variant variant;
3480 struct algorithm algorithm;
3481
3482 if (coeff == 0)
3483 return CONST0_RTX (mode);
3484
3485 /* Special case powers of two. */
3486 if (EXACT_POWER_OF_2_OR_ZERO_P (coeff))
3487 {
3488 op0 = convert_to_mode (mode, op0, this_optab == umul_widen_optab);
3489 return expand_shift (LSHIFT_EXPR, mode, op0,
3490 floor_log2 (coeff), target, unsignedp);
3491 }
3492
3493 /* Exclude cost of op0 from max_cost to match the cost
3494 calculation of the synth_mult. */
3495 max_cost = mul_widen_cost (speed, mode);
3496 if (choose_mult_variant (mode, coeff, &algorithm, &variant,
3497 max_cost))
3498 {
3499 op0 = convert_to_mode (mode, op0, this_optab == umul_widen_optab);
3500 return expand_mult_const (mode, op0, coeff, target,
3501 &algorithm, variant);
3502 }
3503 }
3504 return expand_binop (mode, this_optab, op0, op1, target,
3505 unsignedp, OPTAB_LIB_WIDEN);
3506 }
3507 \f
3508 /* Choose a minimal N + 1 bit approximation to 1/D that can be used to
3509 replace division by D, and put the least significant N bits of the result
3510 in *MULTIPLIER_PTR and return the most significant bit.
3511
3512 The width of operations is N (should be <= HOST_BITS_PER_WIDE_INT), the
3513 needed precision is in PRECISION (should be <= N).
3514
3515 PRECISION should be as small as possible so this function can choose
3516 multiplier more freely.
3517
3518 The rounded-up logarithm of D is placed in *lgup_ptr. A shift count that
3519 is to be used for a final right shift is placed in *POST_SHIFT_PTR.
3520
3521 Using this function, x/D will be equal to (x * m) >> (*POST_SHIFT_PTR),
3522 where m is the full HOST_BITS_PER_WIDE_INT + 1 bit multiplier. */
3523
3524 unsigned HOST_WIDE_INT
3525 choose_multiplier (unsigned HOST_WIDE_INT d, int n, int precision,
3526 unsigned HOST_WIDE_INT *multiplier_ptr,
3527 int *post_shift_ptr, int *lgup_ptr)
3528 {
3529 int lgup, post_shift;
3530 int pow, pow2;
3531
3532 /* lgup = ceil(log2(divisor)); */
3533 lgup = ceil_log2 (d);
3534
3535 gcc_assert (lgup <= n);
3536
3537 pow = n + lgup;
3538 pow2 = n + lgup - precision;
3539
3540 /* mlow = 2^(N + lgup)/d */
3541 wide_int val = wi::set_bit_in_zero (pow, HOST_BITS_PER_DOUBLE_INT);
3542 wide_int mlow = wi::udiv_trunc (val, d);
3543
3544 /* mhigh = (2^(N + lgup) + 2^(N + lgup - precision))/d */
3545 val |= wi::set_bit_in_zero (pow2, HOST_BITS_PER_DOUBLE_INT);
3546 wide_int mhigh = wi::udiv_trunc (val, d);
3547
3548 /* If precision == N, then mlow, mhigh exceed 2^N
3549 (but they do not exceed 2^(N+1)). */
3550
3551 /* Reduce to lowest terms. */
3552 for (post_shift = lgup; post_shift > 0; post_shift--)
3553 {
3554 unsigned HOST_WIDE_INT ml_lo = wi::extract_uhwi (mlow, 1,
3555 HOST_BITS_PER_WIDE_INT);
3556 unsigned HOST_WIDE_INT mh_lo = wi::extract_uhwi (mhigh, 1,
3557 HOST_BITS_PER_WIDE_INT);
3558 if (ml_lo >= mh_lo)
3559 break;
3560
3561 mlow = wi::uhwi (ml_lo, HOST_BITS_PER_DOUBLE_INT);
3562 mhigh = wi::uhwi (mh_lo, HOST_BITS_PER_DOUBLE_INT);
3563 }
3564
3565 *post_shift_ptr = post_shift;
3566 *lgup_ptr = lgup;
3567 if (n < HOST_BITS_PER_WIDE_INT)
3568 {
3569 unsigned HOST_WIDE_INT mask = (HOST_WIDE_INT_1U << n) - 1;
3570 *multiplier_ptr = mhigh.to_uhwi () & mask;
3571 return mhigh.to_uhwi () >= mask;
3572 }
3573 else
3574 {
3575 *multiplier_ptr = mhigh.to_uhwi ();
3576 return wi::extract_uhwi (mhigh, HOST_BITS_PER_WIDE_INT, 1);
3577 }
3578 }
3579
3580 /* Compute the inverse of X mod 2**n, i.e., find Y such that X * Y is
3581 congruent to 1 (mod 2**N). */
3582
3583 static unsigned HOST_WIDE_INT
3584 invert_mod2n (unsigned HOST_WIDE_INT x, int n)
3585 {
3586 /* Solve x*y == 1 (mod 2^n), where x is odd. Return y. */
3587
3588 /* The algorithm notes that the choice y = x satisfies
3589 x*y == 1 mod 2^3, since x is assumed odd.
3590 Each iteration doubles the number of bits of significance in y. */
3591
3592 unsigned HOST_WIDE_INT mask;
3593 unsigned HOST_WIDE_INT y = x;
3594 int nbit = 3;
3595
3596 mask = (n == HOST_BITS_PER_WIDE_INT
3597 ? HOST_WIDE_INT_M1U
3598 : (HOST_WIDE_INT_1U << n) - 1);
3599
3600 while (nbit < n)
3601 {
3602 y = y * (2 - x*y) & mask; /* Modulo 2^N */
3603 nbit *= 2;
3604 }
3605 return y;
3606 }
3607
3608 /* Emit code to adjust ADJ_OPERAND after multiplication of wrong signedness
3609 flavor of OP0 and OP1. ADJ_OPERAND is already the high half of the
3610 product OP0 x OP1. If UNSIGNEDP is nonzero, adjust the signed product
3611 to become unsigned, if UNSIGNEDP is zero, adjust the unsigned product to
3612 become signed.
3613
3614 The result is put in TARGET if that is convenient.
3615
3616 MODE is the mode of operation. */
3617
3618 rtx
3619 expand_mult_highpart_adjust (scalar_int_mode mode, rtx adj_operand, rtx op0,
3620 rtx op1, rtx target, int unsignedp)
3621 {
3622 rtx tem;
3623 enum rtx_code adj_code = unsignedp ? PLUS : MINUS;
3624
3625 tem = expand_shift (RSHIFT_EXPR, mode, op0,
3626 GET_MODE_BITSIZE (mode) - 1, NULL_RTX, 0);
3627 tem = expand_and (mode, tem, op1, NULL_RTX);
3628 adj_operand
3629 = force_operand (gen_rtx_fmt_ee (adj_code, mode, adj_operand, tem),
3630 adj_operand);
3631
3632 tem = expand_shift (RSHIFT_EXPR, mode, op1,
3633 GET_MODE_BITSIZE (mode) - 1, NULL_RTX, 0);
3634 tem = expand_and (mode, tem, op0, NULL_RTX);
3635 target = force_operand (gen_rtx_fmt_ee (adj_code, mode, adj_operand, tem),
3636 target);
3637
3638 return target;
3639 }
3640
3641 /* Subroutine of expmed_mult_highpart. Return the MODE high part of OP. */
3642
3643 static rtx
3644 extract_high_half (scalar_int_mode mode, rtx op)
3645 {
3646 if (mode == word_mode)
3647 return gen_highpart (mode, op);
3648
3649 scalar_int_mode wider_mode = GET_MODE_WIDER_MODE (mode).require ();
3650
3651 op = expand_shift (RSHIFT_EXPR, wider_mode, op,
3652 GET_MODE_BITSIZE (mode), 0, 1);
3653 return convert_modes (mode, wider_mode, op, 0);
3654 }
3655
3656 /* Like expmed_mult_highpart, but only consider using a multiplication
3657 optab. OP1 is an rtx for the constant operand. */
3658
3659 static rtx
3660 expmed_mult_highpart_optab (scalar_int_mode mode, rtx op0, rtx op1,
3661 rtx target, int unsignedp, int max_cost)
3662 {
3663 rtx narrow_op1 = gen_int_mode (INTVAL (op1), mode);
3664 optab moptab;
3665 rtx tem;
3666 int size;
3667 bool speed = optimize_insn_for_speed_p ();
3668
3669 scalar_int_mode wider_mode = GET_MODE_WIDER_MODE (mode).require ();
3670
3671 size = GET_MODE_BITSIZE (mode);
3672
3673 /* Firstly, try using a multiplication insn that only generates the needed
3674 high part of the product, and in the sign flavor of unsignedp. */
3675 if (mul_highpart_cost (speed, mode) < max_cost)
3676 {
3677 moptab = unsignedp ? umul_highpart_optab : smul_highpart_optab;
3678 tem = expand_binop (mode, moptab, op0, narrow_op1, target,
3679 unsignedp, OPTAB_DIRECT);
3680 if (tem)
3681 return tem;
3682 }
3683
3684 /* Secondly, same as above, but use sign flavor opposite of unsignedp.
3685 Need to adjust the result after the multiplication. */
3686 if (size - 1 < BITS_PER_WORD
3687 && (mul_highpart_cost (speed, mode)
3688 + 2 * shift_cost (speed, mode, size-1)
3689 + 4 * add_cost (speed, mode) < max_cost))
3690 {
3691 moptab = unsignedp ? smul_highpart_optab : umul_highpart_optab;
3692 tem = expand_binop (mode, moptab, op0, narrow_op1, target,
3693 unsignedp, OPTAB_DIRECT);
3694 if (tem)
3695 /* We used the wrong signedness. Adjust the result. */
3696 return expand_mult_highpart_adjust (mode, tem, op0, narrow_op1,
3697 tem, unsignedp);
3698 }
3699
3700 /* Try widening multiplication. */
3701 moptab = unsignedp ? umul_widen_optab : smul_widen_optab;
3702 if (convert_optab_handler (moptab, wider_mode, mode) != CODE_FOR_nothing
3703 && mul_widen_cost (speed, wider_mode) < max_cost)
3704 {
3705 tem = expand_binop (wider_mode, moptab, op0, narrow_op1, 0,
3706 unsignedp, OPTAB_WIDEN);
3707 if (tem)
3708 return extract_high_half (mode, tem);
3709 }
3710
3711 /* Try widening the mode and perform a non-widening multiplication. */
3712 if (optab_handler (smul_optab, wider_mode) != CODE_FOR_nothing
3713 && size - 1 < BITS_PER_WORD
3714 && (mul_cost (speed, wider_mode) + shift_cost (speed, mode, size-1)
3715 < max_cost))
3716 {
3717 rtx_insn *insns;
3718 rtx wop0, wop1;
3719
3720 /* We need to widen the operands, for example to ensure the
3721 constant multiplier is correctly sign or zero extended.
3722 Use a sequence to clean-up any instructions emitted by
3723 the conversions if things don't work out. */
3724 start_sequence ();
3725 wop0 = convert_modes (wider_mode, mode, op0, unsignedp);
3726 wop1 = convert_modes (wider_mode, mode, op1, unsignedp);
3727 tem = expand_binop (wider_mode, smul_optab, wop0, wop1, 0,
3728 unsignedp, OPTAB_WIDEN);
3729 insns = get_insns ();
3730 end_sequence ();
3731
3732 if (tem)
3733 {
3734 emit_insn (insns);
3735 return extract_high_half (mode, tem);
3736 }
3737 }
3738
3739 /* Try widening multiplication of opposite signedness, and adjust. */
3740 moptab = unsignedp ? smul_widen_optab : umul_widen_optab;
3741 if (convert_optab_handler (moptab, wider_mode, mode) != CODE_FOR_nothing
3742 && size - 1 < BITS_PER_WORD
3743 && (mul_widen_cost (speed, wider_mode)
3744 + 2 * shift_cost (speed, mode, size-1)
3745 + 4 * add_cost (speed, mode) < max_cost))
3746 {
3747 tem = expand_binop (wider_mode, moptab, op0, narrow_op1,
3748 NULL_RTX, ! unsignedp, OPTAB_WIDEN);
3749 if (tem != 0)
3750 {
3751 tem = extract_high_half (mode, tem);
3752 /* We used the wrong signedness. Adjust the result. */
3753 return expand_mult_highpart_adjust (mode, tem, op0, narrow_op1,
3754 target, unsignedp);
3755 }
3756 }
3757
3758 return 0;
3759 }
3760
3761 /* Emit code to multiply OP0 and OP1 (where OP1 is an integer constant),
3762 putting the high half of the result in TARGET if that is convenient,
3763 and return where the result is. If the operation can not be performed,
3764 0 is returned.
3765
3766 MODE is the mode of operation and result.
3767
3768 UNSIGNEDP nonzero means unsigned multiply.
3769
3770 MAX_COST is the total allowed cost for the expanded RTL. */
3771
3772 static rtx
3773 expmed_mult_highpart (scalar_int_mode mode, rtx op0, rtx op1,
3774 rtx target, int unsignedp, int max_cost)
3775 {
3776 unsigned HOST_WIDE_INT cnst1;
3777 int extra_cost;
3778 bool sign_adjust = false;
3779 enum mult_variant variant;
3780 struct algorithm alg;
3781 rtx tem;
3782 bool speed = optimize_insn_for_speed_p ();
3783
3784 /* We can't support modes wider than HOST_BITS_PER_INT. */
3785 gcc_assert (HWI_COMPUTABLE_MODE_P (mode));
3786
3787 cnst1 = INTVAL (op1) & GET_MODE_MASK (mode);
3788
3789 /* We can't optimize modes wider than BITS_PER_WORD.
3790 ??? We might be able to perform double-word arithmetic if
3791 mode == word_mode, however all the cost calculations in
3792 synth_mult etc. assume single-word operations. */
3793 scalar_int_mode wider_mode = GET_MODE_WIDER_MODE (mode).require ();
3794 if (GET_MODE_BITSIZE (wider_mode) > BITS_PER_WORD)
3795 return expmed_mult_highpart_optab (mode, op0, op1, target,
3796 unsignedp, max_cost);
3797
3798 extra_cost = shift_cost (speed, mode, GET_MODE_BITSIZE (mode) - 1);
3799
3800 /* Check whether we try to multiply by a negative constant. */
3801 if (!unsignedp && ((cnst1 >> (GET_MODE_BITSIZE (mode) - 1)) & 1))
3802 {
3803 sign_adjust = true;
3804 extra_cost += add_cost (speed, mode);
3805 }
3806
3807 /* See whether shift/add multiplication is cheap enough. */
3808 if (choose_mult_variant (wider_mode, cnst1, &alg, &variant,
3809 max_cost - extra_cost))
3810 {
3811 /* See whether the specialized multiplication optabs are
3812 cheaper than the shift/add version. */
3813 tem = expmed_mult_highpart_optab (mode, op0, op1, target, unsignedp,
3814 alg.cost.cost + extra_cost);
3815 if (tem)
3816 return tem;
3817
3818 tem = convert_to_mode (wider_mode, op0, unsignedp);
3819 tem = expand_mult_const (wider_mode, tem, cnst1, 0, &alg, variant);
3820 tem = extract_high_half (mode, tem);
3821
3822 /* Adjust result for signedness. */
3823 if (sign_adjust)
3824 tem = force_operand (gen_rtx_MINUS (mode, tem, op0), tem);
3825
3826 return tem;
3827 }
3828 return expmed_mult_highpart_optab (mode, op0, op1, target,
3829 unsignedp, max_cost);
3830 }
3831
3832
3833 /* Expand signed modulus of OP0 by a power of two D in mode MODE. */
3834
3835 static rtx
3836 expand_smod_pow2 (scalar_int_mode mode, rtx op0, HOST_WIDE_INT d)
3837 {
3838 rtx result, temp, shift;
3839 rtx_code_label *label;
3840 int logd;
3841 int prec = GET_MODE_PRECISION (mode);
3842
3843 logd = floor_log2 (d);
3844 result = gen_reg_rtx (mode);
3845
3846 /* Avoid conditional branches when they're expensive. */
3847 if (BRANCH_COST (optimize_insn_for_speed_p (), false) >= 2
3848 && optimize_insn_for_speed_p ())
3849 {
3850 rtx signmask = emit_store_flag (result, LT, op0, const0_rtx,
3851 mode, 0, -1);
3852 if (signmask)
3853 {
3854 HOST_WIDE_INT masklow = (HOST_WIDE_INT_1 << logd) - 1;
3855 signmask = force_reg (mode, signmask);
3856 shift = GEN_INT (GET_MODE_BITSIZE (mode) - logd);
3857
3858 /* Use the rtx_cost of a LSHIFTRT instruction to determine
3859 which instruction sequence to use. If logical right shifts
3860 are expensive the use 2 XORs, 2 SUBs and an AND, otherwise
3861 use a LSHIFTRT, 1 ADD, 1 SUB and an AND. */
3862
3863 temp = gen_rtx_LSHIFTRT (mode, result, shift);
3864 if (optab_handler (lshr_optab, mode) == CODE_FOR_nothing
3865 || (set_src_cost (temp, mode, optimize_insn_for_speed_p ())
3866 > COSTS_N_INSNS (2)))
3867 {
3868 temp = expand_binop (mode, xor_optab, op0, signmask,
3869 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3870 temp = expand_binop (mode, sub_optab, temp, signmask,
3871 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3872 temp = expand_binop (mode, and_optab, temp,
3873 gen_int_mode (masklow, mode),
3874 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3875 temp = expand_binop (mode, xor_optab, temp, signmask,
3876 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3877 temp = expand_binop (mode, sub_optab, temp, signmask,
3878 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3879 }
3880 else
3881 {
3882 signmask = expand_binop (mode, lshr_optab, signmask, shift,
3883 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3884 signmask = force_reg (mode, signmask);
3885
3886 temp = expand_binop (mode, add_optab, op0, signmask,
3887 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3888 temp = expand_binop (mode, and_optab, temp,
3889 gen_int_mode (masklow, mode),
3890 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3891 temp = expand_binop (mode, sub_optab, temp, signmask,
3892 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3893 }
3894 return temp;
3895 }
3896 }
3897
3898 /* Mask contains the mode's signbit and the significant bits of the
3899 modulus. By including the signbit in the operation, many targets
3900 can avoid an explicit compare operation in the following comparison
3901 against zero. */
3902 wide_int mask = wi::mask (logd, false, prec);
3903 mask = wi::set_bit (mask, prec - 1);
3904
3905 temp = expand_binop (mode, and_optab, op0,
3906 immed_wide_int_const (mask, mode),
3907 result, 1, OPTAB_LIB_WIDEN);
3908 if (temp != result)
3909 emit_move_insn (result, temp);
3910
3911 label = gen_label_rtx ();
3912 do_cmp_and_jump (result, const0_rtx, GE, mode, label);
3913
3914 temp = expand_binop (mode, sub_optab, result, const1_rtx, result,
3915 0, OPTAB_LIB_WIDEN);
3916
3917 mask = wi::mask (logd, true, prec);
3918 temp = expand_binop (mode, ior_optab, temp,
3919 immed_wide_int_const (mask, mode),
3920 result, 1, OPTAB_LIB_WIDEN);
3921 temp = expand_binop (mode, add_optab, temp, const1_rtx, result,
3922 0, OPTAB_LIB_WIDEN);
3923 if (temp != result)
3924 emit_move_insn (result, temp);
3925 emit_label (label);
3926 return result;
3927 }
3928
3929 /* Expand signed division of OP0 by a power of two D in mode MODE.
3930 This routine is only called for positive values of D. */
3931
3932 static rtx
3933 expand_sdiv_pow2 (scalar_int_mode mode, rtx op0, HOST_WIDE_INT d)
3934 {
3935 rtx temp;
3936 rtx_code_label *label;
3937 int logd;
3938
3939 logd = floor_log2 (d);
3940
3941 if (d == 2
3942 && BRANCH_COST (optimize_insn_for_speed_p (),
3943 false) >= 1)
3944 {
3945 temp = gen_reg_rtx (mode);
3946 temp = emit_store_flag (temp, LT, op0, const0_rtx, mode, 0, 1);
3947 temp = expand_binop (mode, add_optab, temp, op0, NULL_RTX,
3948 0, OPTAB_LIB_WIDEN);
3949 return expand_shift (RSHIFT_EXPR, mode, temp, logd, NULL_RTX, 0);
3950 }
3951
3952 if (HAVE_conditional_move
3953 && BRANCH_COST (optimize_insn_for_speed_p (), false) >= 2)
3954 {
3955 rtx temp2;
3956
3957 start_sequence ();
3958 temp2 = copy_to_mode_reg (mode, op0);
3959 temp = expand_binop (mode, add_optab, temp2, gen_int_mode (d - 1, mode),
3960 NULL_RTX, 0, OPTAB_LIB_WIDEN);
3961 temp = force_reg (mode, temp);
3962
3963 /* Construct "temp2 = (temp2 < 0) ? temp : temp2". */
3964 temp2 = emit_conditional_move (temp2, LT, temp2, const0_rtx,
3965 mode, temp, temp2, mode, 0);
3966 if (temp2)
3967 {
3968 rtx_insn *seq = get_insns ();
3969 end_sequence ();
3970 emit_insn (seq);
3971 return expand_shift (RSHIFT_EXPR, mode, temp2, logd, NULL_RTX, 0);
3972 }
3973 end_sequence ();
3974 }
3975
3976 if (BRANCH_COST (optimize_insn_for_speed_p (),
3977 false) >= 2)
3978 {
3979 int ushift = GET_MODE_BITSIZE (mode) - logd;
3980
3981 temp = gen_reg_rtx (mode);
3982 temp = emit_store_flag (temp, LT, op0, const0_rtx, mode, 0, -1);
3983 if (GET_MODE_BITSIZE (mode) >= BITS_PER_WORD
3984 || shift_cost (optimize_insn_for_speed_p (), mode, ushift)
3985 > COSTS_N_INSNS (1))
3986 temp = expand_binop (mode, and_optab, temp, gen_int_mode (d - 1, mode),
3987 NULL_RTX, 0, OPTAB_LIB_WIDEN);
3988 else
3989 temp = expand_shift (RSHIFT_EXPR, mode, temp,
3990 ushift, NULL_RTX, 1);
3991 temp = expand_binop (mode, add_optab, temp, op0, NULL_RTX,
3992 0, OPTAB_LIB_WIDEN);
3993 return expand_shift (RSHIFT_EXPR, mode, temp, logd, NULL_RTX, 0);
3994 }
3995
3996 label = gen_label_rtx ();
3997 temp = copy_to_mode_reg (mode, op0);
3998 do_cmp_and_jump (temp, const0_rtx, GE, mode, label);
3999 expand_inc (temp, gen_int_mode (d - 1, mode));
4000 emit_label (label);
4001 return expand_shift (RSHIFT_EXPR, mode, temp, logd, NULL_RTX, 0);
4002 }
4003 \f
4004 /* Emit the code to divide OP0 by OP1, putting the result in TARGET
4005 if that is convenient, and returning where the result is.
4006 You may request either the quotient or the remainder as the result;
4007 specify REM_FLAG nonzero to get the remainder.
4008
4009 CODE is the expression code for which kind of division this is;
4010 it controls how rounding is done. MODE is the machine mode to use.
4011 UNSIGNEDP nonzero means do unsigned division. */
4012
4013 /* ??? For CEIL_MOD_EXPR, can compute incorrect remainder with ANDI
4014 and then correct it by or'ing in missing high bits
4015 if result of ANDI is nonzero.
4016 For ROUND_MOD_EXPR, can use ANDI and then sign-extend the result.
4017 This could optimize to a bfexts instruction.
4018 But C doesn't use these operations, so their optimizations are
4019 left for later. */
4020 /* ??? For modulo, we don't actually need the highpart of the first product,
4021 the low part will do nicely. And for small divisors, the second multiply
4022 can also be a low-part only multiply or even be completely left out.
4023 E.g. to calculate the remainder of a division by 3 with a 32 bit
4024 multiply, multiply with 0x55555556 and extract the upper two bits;
4025 the result is exact for inputs up to 0x1fffffff.
4026 The input range can be reduced by using cross-sum rules.
4027 For odd divisors >= 3, the following table gives right shift counts
4028 so that if a number is shifted by an integer multiple of the given
4029 amount, the remainder stays the same:
4030 2, 4, 3, 6, 10, 12, 4, 8, 18, 6, 11, 20, 18, 0, 5, 10, 12, 0, 12, 20,
4031 14, 12, 23, 21, 8, 0, 20, 18, 0, 0, 6, 12, 0, 22, 0, 18, 20, 30, 0, 0,
4032 0, 8, 0, 11, 12, 10, 36, 0, 30, 0, 0, 12, 0, 0, 0, 0, 44, 12, 24, 0,
4033 20, 0, 7, 14, 0, 18, 36, 0, 0, 46, 60, 0, 42, 0, 15, 24, 20, 0, 0, 33,
4034 0, 20, 0, 0, 18, 0, 60, 0, 0, 0, 0, 0, 40, 18, 0, 0, 12
4035
4036 Cross-sum rules for even numbers can be derived by leaving as many bits
4037 to the right alone as the divisor has zeros to the right.
4038 E.g. if x is an unsigned 32 bit number:
4039 (x mod 12) == (((x & 1023) + ((x >> 8) & ~3)) * 0x15555558 >> 2 * 3) >> 28
4040 */
4041
4042 rtx
4043 expand_divmod (int rem_flag, enum tree_code code, machine_mode mode,
4044 rtx op0, rtx op1, rtx target, int unsignedp)
4045 {
4046 machine_mode compute_mode;
4047 rtx tquotient;
4048 rtx quotient = 0, remainder = 0;
4049 rtx_insn *last;
4050 rtx_insn *insn;
4051 optab optab1, optab2;
4052 int op1_is_constant, op1_is_pow2 = 0;
4053 int max_cost, extra_cost;
4054 static HOST_WIDE_INT last_div_const = 0;
4055 bool speed = optimize_insn_for_speed_p ();
4056
4057 op1_is_constant = CONST_INT_P (op1);
4058 if (op1_is_constant)
4059 {
4060 wide_int ext_op1 = rtx_mode_t (op1, mode);
4061 op1_is_pow2 = (wi::popcount (ext_op1) == 1
4062 || (! unsignedp
4063 && wi::popcount (wi::neg (ext_op1)) == 1));
4064 }
4065
4066 /*
4067 This is the structure of expand_divmod:
4068
4069 First comes code to fix up the operands so we can perform the operations
4070 correctly and efficiently.
4071
4072 Second comes a switch statement with code specific for each rounding mode.
4073 For some special operands this code emits all RTL for the desired
4074 operation, for other cases, it generates only a quotient and stores it in
4075 QUOTIENT. The case for trunc division/remainder might leave quotient = 0,
4076 to indicate that it has not done anything.
4077
4078 Last comes code that finishes the operation. If QUOTIENT is set and
4079 REM_FLAG is set, the remainder is computed as OP0 - QUOTIENT * OP1. If
4080 QUOTIENT is not set, it is computed using trunc rounding.
4081
4082 We try to generate special code for division and remainder when OP1 is a
4083 constant. If |OP1| = 2**n we can use shifts and some other fast
4084 operations. For other values of OP1, we compute a carefully selected
4085 fixed-point approximation m = 1/OP1, and generate code that multiplies OP0
4086 by m.
4087
4088 In all cases but EXACT_DIV_EXPR, this multiplication requires the upper
4089 half of the product. Different strategies for generating the product are
4090 implemented in expmed_mult_highpart.
4091
4092 If what we actually want is the remainder, we generate that by another
4093 by-constant multiplication and a subtraction. */
4094
4095 /* We shouldn't be called with OP1 == const1_rtx, but some of the
4096 code below will malfunction if we are, so check here and handle
4097 the special case if so. */
4098 if (op1 == const1_rtx)
4099 return rem_flag ? const0_rtx : op0;
4100
4101 /* When dividing by -1, we could get an overflow.
4102 negv_optab can handle overflows. */
4103 if (! unsignedp && op1 == constm1_rtx)
4104 {
4105 if (rem_flag)
4106 return const0_rtx;
4107 return expand_unop (mode, flag_trapv && GET_MODE_CLASS (mode) == MODE_INT
4108 ? negv_optab : neg_optab, op0, target, 0);
4109 }
4110
4111 if (target
4112 /* Don't use the function value register as a target
4113 since we have to read it as well as write it,
4114 and function-inlining gets confused by this. */
4115 && ((REG_P (target) && REG_FUNCTION_VALUE_P (target))
4116 /* Don't clobber an operand while doing a multi-step calculation. */
4117 || ((rem_flag || op1_is_constant)
4118 && (reg_mentioned_p (target, op0)
4119 || (MEM_P (op0) && MEM_P (target))))
4120 || reg_mentioned_p (target, op1)
4121 || (MEM_P (op1) && MEM_P (target))))
4122 target = 0;
4123
4124 /* Get the mode in which to perform this computation. Normally it will
4125 be MODE, but sometimes we can't do the desired operation in MODE.
4126 If so, pick a wider mode in which we can do the operation. Convert
4127 to that mode at the start to avoid repeated conversions.
4128
4129 First see what operations we need. These depend on the expression
4130 we are evaluating. (We assume that divxx3 insns exist under the
4131 same conditions that modxx3 insns and that these insns don't normally
4132 fail. If these assumptions are not correct, we may generate less
4133 efficient code in some cases.)
4134
4135 Then see if we find a mode in which we can open-code that operation
4136 (either a division, modulus, or shift). Finally, check for the smallest
4137 mode for which we can do the operation with a library call. */
4138
4139 /* We might want to refine this now that we have division-by-constant
4140 optimization. Since expmed_mult_highpart tries so many variants, it is
4141 not straightforward to generalize this. Maybe we should make an array
4142 of possible modes in init_expmed? Save this for GCC 2.7. */
4143
4144 optab1 = (op1_is_pow2
4145 ? (unsignedp ? lshr_optab : ashr_optab)
4146 : (unsignedp ? udiv_optab : sdiv_optab));
4147 optab2 = (op1_is_pow2 ? optab1
4148 : (unsignedp ? udivmod_optab : sdivmod_optab));
4149
4150 FOR_EACH_MODE_FROM (compute_mode, mode)
4151 if (optab_handler (optab1, compute_mode) != CODE_FOR_nothing
4152 || optab_handler (optab2, compute_mode) != CODE_FOR_nothing)
4153 break;
4154
4155 if (compute_mode == VOIDmode)
4156 FOR_EACH_MODE_FROM (compute_mode, mode)
4157 if (optab_libfunc (optab1, compute_mode)
4158 || optab_libfunc (optab2, compute_mode))
4159 break;
4160
4161 /* If we still couldn't find a mode, use MODE, but expand_binop will
4162 probably die. */
4163 if (compute_mode == VOIDmode)
4164 compute_mode = mode;
4165
4166 if (target && GET_MODE (target) == compute_mode)
4167 tquotient = target;
4168 else
4169 tquotient = gen_reg_rtx (compute_mode);
4170
4171 #if 0
4172 /* It should be possible to restrict the precision to GET_MODE_BITSIZE
4173 (mode), and thereby get better code when OP1 is a constant. Do that
4174 later. It will require going over all usages of SIZE below. */
4175 size = GET_MODE_BITSIZE (mode);
4176 #endif
4177
4178 /* Only deduct something for a REM if the last divide done was
4179 for a different constant. Then set the constant of the last
4180 divide. */
4181 max_cost = (unsignedp
4182 ? udiv_cost (speed, compute_mode)
4183 : sdiv_cost (speed, compute_mode));
4184 if (rem_flag && ! (last_div_const != 0 && op1_is_constant
4185 && INTVAL (op1) == last_div_const))
4186 max_cost -= (mul_cost (speed, compute_mode)
4187 + add_cost (speed, compute_mode));
4188
4189 last_div_const = ! rem_flag && op1_is_constant ? INTVAL (op1) : 0;
4190
4191 /* Now convert to the best mode to use. */
4192 if (compute_mode != mode)
4193 {
4194 op0 = convert_modes (compute_mode, mode, op0, unsignedp);
4195 op1 = convert_modes (compute_mode, mode, op1, unsignedp);
4196
4197 /* convert_modes may have placed op1 into a register, so we
4198 must recompute the following. */
4199 op1_is_constant = CONST_INT_P (op1);
4200 if (op1_is_constant)
4201 {
4202 wide_int ext_op1 = rtx_mode_t (op1, compute_mode);
4203 op1_is_pow2 = (wi::popcount (ext_op1) == 1
4204 || (! unsignedp
4205 && wi::popcount (wi::neg (ext_op1)) == 1));
4206 }
4207 else
4208 op1_is_pow2 = 0;
4209 }
4210
4211 /* If one of the operands is a volatile MEM, copy it into a register. */
4212
4213 if (MEM_P (op0) && MEM_VOLATILE_P (op0))
4214 op0 = force_reg (compute_mode, op0);
4215 if (MEM_P (op1) && MEM_VOLATILE_P (op1))
4216 op1 = force_reg (compute_mode, op1);
4217
4218 /* If we need the remainder or if OP1 is constant, we need to
4219 put OP0 in a register in case it has any queued subexpressions. */
4220 if (rem_flag || op1_is_constant)
4221 op0 = force_reg (compute_mode, op0);
4222
4223 last = get_last_insn ();
4224
4225 /* Promote floor rounding to trunc rounding for unsigned operations. */
4226 if (unsignedp)
4227 {
4228 if (code == FLOOR_DIV_EXPR)
4229 code = TRUNC_DIV_EXPR;
4230 if (code == FLOOR_MOD_EXPR)
4231 code = TRUNC_MOD_EXPR;
4232 if (code == EXACT_DIV_EXPR && op1_is_pow2)
4233 code = TRUNC_DIV_EXPR;
4234 }
4235
4236 if (op1 != const0_rtx)
4237 switch (code)
4238 {
4239 case TRUNC_MOD_EXPR:
4240 case TRUNC_DIV_EXPR:
4241 if (op1_is_constant)
4242 {
4243 scalar_int_mode int_mode = as_a <scalar_int_mode> (compute_mode);
4244 int size = GET_MODE_BITSIZE (int_mode);
4245 if (unsignedp)
4246 {
4247 unsigned HOST_WIDE_INT mh, ml;
4248 int pre_shift, post_shift;
4249 int dummy;
4250 wide_int wd = rtx_mode_t (op1, int_mode);
4251 unsigned HOST_WIDE_INT d = wd.to_uhwi ();
4252
4253 if (wi::popcount (wd) == 1)
4254 {
4255 pre_shift = floor_log2 (d);
4256 if (rem_flag)
4257 {
4258 unsigned HOST_WIDE_INT mask
4259 = (HOST_WIDE_INT_1U << pre_shift) - 1;
4260 remainder
4261 = expand_binop (int_mode, and_optab, op0,
4262 gen_int_mode (mask, int_mode),
4263 remainder, 1,
4264 OPTAB_LIB_WIDEN);
4265 if (remainder)
4266 return gen_lowpart (mode, remainder);
4267 }
4268 quotient = expand_shift (RSHIFT_EXPR, int_mode, op0,
4269 pre_shift, tquotient, 1);
4270 }
4271 else if (size <= HOST_BITS_PER_WIDE_INT)
4272 {
4273 if (d >= (HOST_WIDE_INT_1U << (size - 1)))
4274 {
4275 /* Most significant bit of divisor is set; emit an scc
4276 insn. */
4277 quotient = emit_store_flag_force (tquotient, GEU, op0, op1,
4278 int_mode, 1, 1);
4279 }
4280 else
4281 {
4282 /* Find a suitable multiplier and right shift count
4283 instead of multiplying with D. */
4284
4285 mh = choose_multiplier (d, size, size,
4286 &ml, &post_shift, &dummy);
4287
4288 /* If the suggested multiplier is more than SIZE bits,
4289 we can do better for even divisors, using an
4290 initial right shift. */
4291 if (mh != 0 && (d & 1) == 0)
4292 {
4293 pre_shift = ctz_or_zero (d);
4294 mh = choose_multiplier (d >> pre_shift, size,
4295 size - pre_shift,
4296 &ml, &post_shift, &dummy);
4297 gcc_assert (!mh);
4298 }
4299 else
4300 pre_shift = 0;
4301
4302 if (mh != 0)
4303 {
4304 rtx t1, t2, t3, t4;
4305
4306 if (post_shift - 1 >= BITS_PER_WORD)
4307 goto fail1;
4308
4309 extra_cost
4310 = (shift_cost (speed, int_mode, post_shift - 1)
4311 + shift_cost (speed, int_mode, 1)
4312 + 2 * add_cost (speed, int_mode));
4313 t1 = expmed_mult_highpart
4314 (int_mode, op0, gen_int_mode (ml, int_mode),
4315 NULL_RTX, 1, max_cost - extra_cost);
4316 if (t1 == 0)
4317 goto fail1;
4318 t2 = force_operand (gen_rtx_MINUS (int_mode,
4319 op0, t1),
4320 NULL_RTX);
4321 t3 = expand_shift (RSHIFT_EXPR, int_mode,
4322 t2, 1, NULL_RTX, 1);
4323 t4 = force_operand (gen_rtx_PLUS (int_mode,
4324 t1, t3),
4325 NULL_RTX);
4326 quotient = expand_shift
4327 (RSHIFT_EXPR, int_mode, t4,
4328 post_shift - 1, tquotient, 1);
4329 }
4330 else
4331 {
4332 rtx t1, t2;
4333
4334 if (pre_shift >= BITS_PER_WORD
4335 || post_shift >= BITS_PER_WORD)
4336 goto fail1;
4337
4338 t1 = expand_shift
4339 (RSHIFT_EXPR, int_mode, op0,
4340 pre_shift, NULL_RTX, 1);
4341 extra_cost
4342 = (shift_cost (speed, int_mode, pre_shift)
4343 + shift_cost (speed, int_mode, post_shift));
4344 t2 = expmed_mult_highpart
4345 (int_mode, t1,
4346 gen_int_mode (ml, int_mode),
4347 NULL_RTX, 1, max_cost - extra_cost);
4348 if (t2 == 0)
4349 goto fail1;
4350 quotient = expand_shift
4351 (RSHIFT_EXPR, int_mode, t2,
4352 post_shift, tquotient, 1);
4353 }
4354 }
4355 }
4356 else /* Too wide mode to use tricky code */
4357 break;
4358
4359 insn = get_last_insn ();
4360 if (insn != last)
4361 set_dst_reg_note (insn, REG_EQUAL,
4362 gen_rtx_UDIV (int_mode, op0, op1),
4363 quotient);
4364 }
4365 else /* TRUNC_DIV, signed */
4366 {
4367 unsigned HOST_WIDE_INT ml;
4368 int lgup, post_shift;
4369 rtx mlr;
4370 HOST_WIDE_INT d = INTVAL (op1);
4371 unsigned HOST_WIDE_INT abs_d;
4372
4373 /* Since d might be INT_MIN, we have to cast to
4374 unsigned HOST_WIDE_INT before negating to avoid
4375 undefined signed overflow. */
4376 abs_d = (d >= 0
4377 ? (unsigned HOST_WIDE_INT) d
4378 : - (unsigned HOST_WIDE_INT) d);
4379
4380 /* n rem d = n rem -d */
4381 if (rem_flag && d < 0)
4382 {
4383 d = abs_d;
4384 op1 = gen_int_mode (abs_d, int_mode);
4385 }
4386
4387 if (d == 1)
4388 quotient = op0;
4389 else if (d == -1)
4390 quotient = expand_unop (int_mode, neg_optab, op0,
4391 tquotient, 0);
4392 else if (size <= HOST_BITS_PER_WIDE_INT
4393 && abs_d == HOST_WIDE_INT_1U << (size - 1))
4394 {
4395 /* This case is not handled correctly below. */
4396 quotient = emit_store_flag (tquotient, EQ, op0, op1,
4397 int_mode, 1, 1);
4398 if (quotient == 0)
4399 goto fail1;
4400 }
4401 else if (EXACT_POWER_OF_2_OR_ZERO_P (d)
4402 && (size <= HOST_BITS_PER_WIDE_INT || d >= 0)
4403 && (rem_flag
4404 ? smod_pow2_cheap (speed, int_mode)
4405 : sdiv_pow2_cheap (speed, int_mode))
4406 /* We assume that cheap metric is true if the
4407 optab has an expander for this mode. */
4408 && ((optab_handler ((rem_flag ? smod_optab
4409 : sdiv_optab),
4410 int_mode)
4411 != CODE_FOR_nothing)
4412 || (optab_handler (sdivmod_optab, int_mode)
4413 != CODE_FOR_nothing)))
4414 ;
4415 else if (EXACT_POWER_OF_2_OR_ZERO_P (abs_d)
4416 && (size <= HOST_BITS_PER_WIDE_INT
4417 || abs_d != (unsigned HOST_WIDE_INT) d))
4418 {
4419 if (rem_flag)
4420 {
4421 remainder = expand_smod_pow2 (int_mode, op0, d);
4422 if (remainder)
4423 return gen_lowpart (mode, remainder);
4424 }
4425
4426 if (sdiv_pow2_cheap (speed, int_mode)
4427 && ((optab_handler (sdiv_optab, int_mode)
4428 != CODE_FOR_nothing)
4429 || (optab_handler (sdivmod_optab, int_mode)
4430 != CODE_FOR_nothing)))
4431 quotient = expand_divmod (0, TRUNC_DIV_EXPR,
4432 int_mode, op0,
4433 gen_int_mode (abs_d,
4434 int_mode),
4435 NULL_RTX, 0);
4436 else
4437 quotient = expand_sdiv_pow2 (int_mode, op0, abs_d);
4438
4439 /* We have computed OP0 / abs(OP1). If OP1 is negative,
4440 negate the quotient. */
4441 if (d < 0)
4442 {
4443 insn = get_last_insn ();
4444 if (insn != last
4445 && abs_d < (HOST_WIDE_INT_1U
4446 << (HOST_BITS_PER_WIDE_INT - 1)))
4447 set_dst_reg_note (insn, REG_EQUAL,
4448 gen_rtx_DIV (int_mode, op0,
4449 gen_int_mode
4450 (abs_d,
4451 int_mode)),
4452 quotient);
4453
4454 quotient = expand_unop (int_mode, neg_optab,
4455 quotient, quotient, 0);
4456 }
4457 }
4458 else if (size <= HOST_BITS_PER_WIDE_INT)
4459 {
4460 choose_multiplier (abs_d, size, size - 1,
4461 &ml, &post_shift, &lgup);
4462 if (ml < HOST_WIDE_INT_1U << (size - 1))
4463 {
4464 rtx t1, t2, t3;
4465
4466 if (post_shift >= BITS_PER_WORD
4467 || size - 1 >= BITS_PER_WORD)
4468 goto fail1;
4469
4470 extra_cost = (shift_cost (speed, int_mode, post_shift)
4471 + shift_cost (speed, int_mode, size - 1)
4472 + add_cost (speed, int_mode));
4473 t1 = expmed_mult_highpart
4474 (int_mode, op0, gen_int_mode (ml, int_mode),
4475 NULL_RTX, 0, max_cost - extra_cost);
4476 if (t1 == 0)
4477 goto fail1;
4478 t2 = expand_shift
4479 (RSHIFT_EXPR, int_mode, t1,
4480 post_shift, NULL_RTX, 0);
4481 t3 = expand_shift
4482 (RSHIFT_EXPR, int_mode, op0,
4483 size - 1, NULL_RTX, 0);
4484 if (d < 0)
4485 quotient
4486 = force_operand (gen_rtx_MINUS (int_mode, t3, t2),
4487 tquotient);
4488 else
4489 quotient
4490 = force_operand (gen_rtx_MINUS (int_mode, t2, t3),
4491 tquotient);
4492 }
4493 else
4494 {
4495 rtx t1, t2, t3, t4;
4496
4497 if (post_shift >= BITS_PER_WORD
4498 || size - 1 >= BITS_PER_WORD)
4499 goto fail1;
4500
4501 ml |= HOST_WIDE_INT_M1U << (size - 1);
4502 mlr = gen_int_mode (ml, int_mode);
4503 extra_cost = (shift_cost (speed, int_mode, post_shift)
4504 + shift_cost (speed, int_mode, size - 1)
4505 + 2 * add_cost (speed, int_mode));
4506 t1 = expmed_mult_highpart (int_mode, op0, mlr,
4507 NULL_RTX, 0,
4508 max_cost - extra_cost);
4509 if (t1 == 0)
4510 goto fail1;
4511 t2 = force_operand (gen_rtx_PLUS (int_mode, t1, op0),
4512 NULL_RTX);
4513 t3 = expand_shift
4514 (RSHIFT_EXPR, int_mode, t2,
4515 post_shift, NULL_RTX, 0);
4516 t4 = expand_shift
4517 (RSHIFT_EXPR, int_mode, op0,
4518 size - 1, NULL_RTX, 0);
4519 if (d < 0)
4520 quotient
4521 = force_operand (gen_rtx_MINUS (int_mode, t4, t3),
4522 tquotient);
4523 else
4524 quotient
4525 = force_operand (gen_rtx_MINUS (int_mode, t3, t4),
4526 tquotient);
4527 }
4528 }
4529 else /* Too wide mode to use tricky code */
4530 break;
4531
4532 insn = get_last_insn ();
4533 if (insn != last)
4534 set_dst_reg_note (insn, REG_EQUAL,
4535 gen_rtx_DIV (int_mode, op0, op1),
4536 quotient);
4537 }
4538 break;
4539 }
4540 fail1:
4541 delete_insns_since (last);
4542 break;
4543
4544 case FLOOR_DIV_EXPR:
4545 case FLOOR_MOD_EXPR:
4546 /* We will come here only for signed operations. */
4547 if (op1_is_constant && HWI_COMPUTABLE_MODE_P (compute_mode))
4548 {
4549 scalar_int_mode int_mode = as_a <scalar_int_mode> (compute_mode);
4550 int size = GET_MODE_BITSIZE (int_mode);
4551 unsigned HOST_WIDE_INT mh, ml;
4552 int pre_shift, lgup, post_shift;
4553 HOST_WIDE_INT d = INTVAL (op1);
4554
4555 if (d > 0)
4556 {
4557 /* We could just as easily deal with negative constants here,
4558 but it does not seem worth the trouble for GCC 2.6. */
4559 if (EXACT_POWER_OF_2_OR_ZERO_P (d))
4560 {
4561 pre_shift = floor_log2 (d);
4562 if (rem_flag)
4563 {
4564 unsigned HOST_WIDE_INT mask
4565 = (HOST_WIDE_INT_1U << pre_shift) - 1;
4566 remainder = expand_binop
4567 (int_mode, and_optab, op0,
4568 gen_int_mode (mask, int_mode),
4569 remainder, 0, OPTAB_LIB_WIDEN);
4570 if (remainder)
4571 return gen_lowpart (mode, remainder);
4572 }
4573 quotient = expand_shift
4574 (RSHIFT_EXPR, int_mode, op0,
4575 pre_shift, tquotient, 0);
4576 }
4577 else
4578 {
4579 rtx t1, t2, t3, t4;
4580
4581 mh = choose_multiplier (d, size, size - 1,
4582 &ml, &post_shift, &lgup);
4583 gcc_assert (!mh);
4584
4585 if (post_shift < BITS_PER_WORD
4586 && size - 1 < BITS_PER_WORD)
4587 {
4588 t1 = expand_shift
4589 (RSHIFT_EXPR, int_mode, op0,
4590 size - 1, NULL_RTX, 0);
4591 t2 = expand_binop (int_mode, xor_optab, op0, t1,
4592 NULL_RTX, 0, OPTAB_WIDEN);
4593 extra_cost = (shift_cost (speed, int_mode, post_shift)
4594 + shift_cost (speed, int_mode, size - 1)
4595 + 2 * add_cost (speed, int_mode));
4596 t3 = expmed_mult_highpart
4597 (int_mode, t2, gen_int_mode (ml, int_mode),
4598 NULL_RTX, 1, max_cost - extra_cost);
4599 if (t3 != 0)
4600 {
4601 t4 = expand_shift
4602 (RSHIFT_EXPR, int_mode, t3,
4603 post_shift, NULL_RTX, 1);
4604 quotient = expand_binop (int_mode, xor_optab,
4605 t4, t1, tquotient, 0,
4606 OPTAB_WIDEN);
4607 }
4608 }
4609 }
4610 }
4611 else
4612 {
4613 rtx nsign, t1, t2, t3, t4;
4614 t1 = force_operand (gen_rtx_PLUS (int_mode,
4615 op0, constm1_rtx), NULL_RTX);
4616 t2 = expand_binop (int_mode, ior_optab, op0, t1, NULL_RTX,
4617 0, OPTAB_WIDEN);
4618 nsign = expand_shift (RSHIFT_EXPR, int_mode, t2,
4619 size - 1, NULL_RTX, 0);
4620 t3 = force_operand (gen_rtx_MINUS (int_mode, t1, nsign),
4621 NULL_RTX);
4622 t4 = expand_divmod (0, TRUNC_DIV_EXPR, int_mode, t3, op1,
4623 NULL_RTX, 0);
4624 if (t4)
4625 {
4626 rtx t5;
4627 t5 = expand_unop (int_mode, one_cmpl_optab, nsign,
4628 NULL_RTX, 0);
4629 quotient = force_operand (gen_rtx_PLUS (int_mode, t4, t5),
4630 tquotient);
4631 }
4632 }
4633 }
4634
4635 if (quotient != 0)
4636 break;
4637 delete_insns_since (last);
4638
4639 /* Try using an instruction that produces both the quotient and
4640 remainder, using truncation. We can easily compensate the quotient
4641 or remainder to get floor rounding, once we have the remainder.
4642 Notice that we compute also the final remainder value here,
4643 and return the result right away. */
4644 if (target == 0 || GET_MODE (target) != compute_mode)
4645 target = gen_reg_rtx (compute_mode);
4646
4647 if (rem_flag)
4648 {
4649 remainder
4650 = REG_P (target) ? target : gen_reg_rtx (compute_mode);
4651 quotient = gen_reg_rtx (compute_mode);
4652 }
4653 else
4654 {
4655 quotient
4656 = REG_P (target) ? target : gen_reg_rtx (compute_mode);
4657 remainder = gen_reg_rtx (compute_mode);
4658 }
4659
4660 if (expand_twoval_binop (sdivmod_optab, op0, op1,
4661 quotient, remainder, 0))
4662 {
4663 /* This could be computed with a branch-less sequence.
4664 Save that for later. */
4665 rtx tem;
4666 rtx_code_label *label = gen_label_rtx ();
4667 do_cmp_and_jump (remainder, const0_rtx, EQ, compute_mode, label);
4668 tem = expand_binop (compute_mode, xor_optab, op0, op1,
4669 NULL_RTX, 0, OPTAB_WIDEN);
4670 do_cmp_and_jump (tem, const0_rtx, GE, compute_mode, label);
4671 expand_dec (quotient, const1_rtx);
4672 expand_inc (remainder, op1);
4673 emit_label (label);
4674 return gen_lowpart (mode, rem_flag ? remainder : quotient);
4675 }
4676
4677 /* No luck with division elimination or divmod. Have to do it
4678 by conditionally adjusting op0 *and* the result. */
4679 {
4680 rtx_code_label *label1, *label2, *label3, *label4, *label5;
4681 rtx adjusted_op0;
4682 rtx tem;
4683
4684 quotient = gen_reg_rtx (compute_mode);
4685 adjusted_op0 = copy_to_mode_reg (compute_mode, op0);
4686 label1 = gen_label_rtx ();
4687 label2 = gen_label_rtx ();
4688 label3 = gen_label_rtx ();
4689 label4 = gen_label_rtx ();
4690 label5 = gen_label_rtx ();
4691 do_cmp_and_jump (op1, const0_rtx, LT, compute_mode, label2);
4692 do_cmp_and_jump (adjusted_op0, const0_rtx, LT, compute_mode, label1);
4693 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4694 quotient, 0, OPTAB_LIB_WIDEN);
4695 if (tem != quotient)
4696 emit_move_insn (quotient, tem);
4697 emit_jump_insn (targetm.gen_jump (label5));
4698 emit_barrier ();
4699 emit_label (label1);
4700 expand_inc (adjusted_op0, const1_rtx);
4701 emit_jump_insn (targetm.gen_jump (label4));
4702 emit_barrier ();
4703 emit_label (label2);
4704 do_cmp_and_jump (adjusted_op0, const0_rtx, GT, compute_mode, label3);
4705 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4706 quotient, 0, OPTAB_LIB_WIDEN);
4707 if (tem != quotient)
4708 emit_move_insn (quotient, tem);
4709 emit_jump_insn (targetm.gen_jump (label5));
4710 emit_barrier ();
4711 emit_label (label3);
4712 expand_dec (adjusted_op0, const1_rtx);
4713 emit_label (label4);
4714 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4715 quotient, 0, OPTAB_LIB_WIDEN);
4716 if (tem != quotient)
4717 emit_move_insn (quotient, tem);
4718 expand_dec (quotient, const1_rtx);
4719 emit_label (label5);
4720 }
4721 break;
4722
4723 case CEIL_DIV_EXPR:
4724 case CEIL_MOD_EXPR:
4725 if (unsignedp)
4726 {
4727 if (op1_is_constant
4728 && EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1))
4729 && (HWI_COMPUTABLE_MODE_P (compute_mode)
4730 || INTVAL (op1) >= 0))
4731 {
4732 scalar_int_mode int_mode
4733 = as_a <scalar_int_mode> (compute_mode);
4734 rtx t1, t2, t3;
4735 unsigned HOST_WIDE_INT d = INTVAL (op1);
4736 t1 = expand_shift (RSHIFT_EXPR, int_mode, op0,
4737 floor_log2 (d), tquotient, 1);
4738 t2 = expand_binop (int_mode, and_optab, op0,
4739 gen_int_mode (d - 1, int_mode),
4740 NULL_RTX, 1, OPTAB_LIB_WIDEN);
4741 t3 = gen_reg_rtx (int_mode);
4742 t3 = emit_store_flag (t3, NE, t2, const0_rtx, int_mode, 1, 1);
4743 if (t3 == 0)
4744 {
4745 rtx_code_label *lab;
4746 lab = gen_label_rtx ();
4747 do_cmp_and_jump (t2, const0_rtx, EQ, int_mode, lab);
4748 expand_inc (t1, const1_rtx);
4749 emit_label (lab);
4750 quotient = t1;
4751 }
4752 else
4753 quotient = force_operand (gen_rtx_PLUS (int_mode, t1, t3),
4754 tquotient);
4755 break;
4756 }
4757
4758 /* Try using an instruction that produces both the quotient and
4759 remainder, using truncation. We can easily compensate the
4760 quotient or remainder to get ceiling rounding, once we have the
4761 remainder. Notice that we compute also the final remainder
4762 value here, and return the result right away. */
4763 if (target == 0 || GET_MODE (target) != compute_mode)
4764 target = gen_reg_rtx (compute_mode);
4765
4766 if (rem_flag)
4767 {
4768 remainder = (REG_P (target)
4769 ? target : gen_reg_rtx (compute_mode));
4770 quotient = gen_reg_rtx (compute_mode);
4771 }
4772 else
4773 {
4774 quotient = (REG_P (target)
4775 ? target : gen_reg_rtx (compute_mode));
4776 remainder = gen_reg_rtx (compute_mode);
4777 }
4778
4779 if (expand_twoval_binop (udivmod_optab, op0, op1, quotient,
4780 remainder, 1))
4781 {
4782 /* This could be computed with a branch-less sequence.
4783 Save that for later. */
4784 rtx_code_label *label = gen_label_rtx ();
4785 do_cmp_and_jump (remainder, const0_rtx, EQ,
4786 compute_mode, label);
4787 expand_inc (quotient, const1_rtx);
4788 expand_dec (remainder, op1);
4789 emit_label (label);
4790 return gen_lowpart (mode, rem_flag ? remainder : quotient);
4791 }
4792
4793 /* No luck with division elimination or divmod. Have to do it
4794 by conditionally adjusting op0 *and* the result. */
4795 {
4796 rtx_code_label *label1, *label2;
4797 rtx adjusted_op0, tem;
4798
4799 quotient = gen_reg_rtx (compute_mode);
4800 adjusted_op0 = copy_to_mode_reg (compute_mode, op0);
4801 label1 = gen_label_rtx ();
4802 label2 = gen_label_rtx ();
4803 do_cmp_and_jump (adjusted_op0, const0_rtx, NE,
4804 compute_mode, label1);
4805 emit_move_insn (quotient, const0_rtx);
4806 emit_jump_insn (targetm.gen_jump (label2));
4807 emit_barrier ();
4808 emit_label (label1);
4809 expand_dec (adjusted_op0, const1_rtx);
4810 tem = expand_binop (compute_mode, udiv_optab, adjusted_op0, op1,
4811 quotient, 1, OPTAB_LIB_WIDEN);
4812 if (tem != quotient)
4813 emit_move_insn (quotient, tem);
4814 expand_inc (quotient, const1_rtx);
4815 emit_label (label2);
4816 }
4817 }
4818 else /* signed */
4819 {
4820 if (op1_is_constant && EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1))
4821 && INTVAL (op1) >= 0)
4822 {
4823 /* This is extremely similar to the code for the unsigned case
4824 above. For 2.7 we should merge these variants, but for
4825 2.6.1 I don't want to touch the code for unsigned since that
4826 get used in C. The signed case will only be used by other
4827 languages (Ada). */
4828
4829 rtx t1, t2, t3;
4830 unsigned HOST_WIDE_INT d = INTVAL (op1);
4831 t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
4832 floor_log2 (d), tquotient, 0);
4833 t2 = expand_binop (compute_mode, and_optab, op0,
4834 gen_int_mode (d - 1, compute_mode),
4835 NULL_RTX, 1, OPTAB_LIB_WIDEN);
4836 t3 = gen_reg_rtx (compute_mode);
4837 t3 = emit_store_flag (t3, NE, t2, const0_rtx,
4838 compute_mode, 1, 1);
4839 if (t3 == 0)
4840 {
4841 rtx_code_label *lab;
4842 lab = gen_label_rtx ();
4843 do_cmp_and_jump (t2, const0_rtx, EQ, compute_mode, lab);
4844 expand_inc (t1, const1_rtx);
4845 emit_label (lab);
4846 quotient = t1;
4847 }
4848 else
4849 quotient = force_operand (gen_rtx_PLUS (compute_mode,
4850 t1, t3),
4851 tquotient);
4852 break;
4853 }
4854
4855 /* Try using an instruction that produces both the quotient and
4856 remainder, using truncation. We can easily compensate the
4857 quotient or remainder to get ceiling rounding, once we have the
4858 remainder. Notice that we compute also the final remainder
4859 value here, and return the result right away. */
4860 if (target == 0 || GET_MODE (target) != compute_mode)
4861 target = gen_reg_rtx (compute_mode);
4862 if (rem_flag)
4863 {
4864 remainder= (REG_P (target)
4865 ? target : gen_reg_rtx (compute_mode));
4866 quotient = gen_reg_rtx (compute_mode);
4867 }
4868 else
4869 {
4870 quotient = (REG_P (target)
4871 ? target : gen_reg_rtx (compute_mode));
4872 remainder = gen_reg_rtx (compute_mode);
4873 }
4874
4875 if (expand_twoval_binop (sdivmod_optab, op0, op1, quotient,
4876 remainder, 0))
4877 {
4878 /* This could be computed with a branch-less sequence.
4879 Save that for later. */
4880 rtx tem;
4881 rtx_code_label *label = gen_label_rtx ();
4882 do_cmp_and_jump (remainder, const0_rtx, EQ,
4883 compute_mode, label);
4884 tem = expand_binop (compute_mode, xor_optab, op0, op1,
4885 NULL_RTX, 0, OPTAB_WIDEN);
4886 do_cmp_and_jump (tem, const0_rtx, LT, compute_mode, label);
4887 expand_inc (quotient, const1_rtx);
4888 expand_dec (remainder, op1);
4889 emit_label (label);
4890 return gen_lowpart (mode, rem_flag ? remainder : quotient);
4891 }
4892
4893 /* No luck with division elimination or divmod. Have to do it
4894 by conditionally adjusting op0 *and* the result. */
4895 {
4896 rtx_code_label *label1, *label2, *label3, *label4, *label5;
4897 rtx adjusted_op0;
4898 rtx tem;
4899
4900 quotient = gen_reg_rtx (compute_mode);
4901 adjusted_op0 = copy_to_mode_reg (compute_mode, op0);
4902 label1 = gen_label_rtx ();
4903 label2 = gen_label_rtx ();
4904 label3 = gen_label_rtx ();
4905 label4 = gen_label_rtx ();
4906 label5 = gen_label_rtx ();
4907 do_cmp_and_jump (op1, const0_rtx, LT, compute_mode, label2);
4908 do_cmp_and_jump (adjusted_op0, const0_rtx, GT,
4909 compute_mode, label1);
4910 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4911 quotient, 0, OPTAB_LIB_WIDEN);
4912 if (tem != quotient)
4913 emit_move_insn (quotient, tem);
4914 emit_jump_insn (targetm.gen_jump (label5));
4915 emit_barrier ();
4916 emit_label (label1);
4917 expand_dec (adjusted_op0, const1_rtx);
4918 emit_jump_insn (targetm.gen_jump (label4));
4919 emit_barrier ();
4920 emit_label (label2);
4921 do_cmp_and_jump (adjusted_op0, const0_rtx, LT,
4922 compute_mode, label3);
4923 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4924 quotient, 0, OPTAB_LIB_WIDEN);
4925 if (tem != quotient)
4926 emit_move_insn (quotient, tem);
4927 emit_jump_insn (targetm.gen_jump (label5));
4928 emit_barrier ();
4929 emit_label (label3);
4930 expand_inc (adjusted_op0, const1_rtx);
4931 emit_label (label4);
4932 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4933 quotient, 0, OPTAB_LIB_WIDEN);
4934 if (tem != quotient)
4935 emit_move_insn (quotient, tem);
4936 expand_inc (quotient, const1_rtx);
4937 emit_label (label5);
4938 }
4939 }
4940 break;
4941
4942 case EXACT_DIV_EXPR:
4943 if (op1_is_constant && HWI_COMPUTABLE_MODE_P (compute_mode))
4944 {
4945 scalar_int_mode int_mode = as_a <scalar_int_mode> (compute_mode);
4946 int size = GET_MODE_BITSIZE (int_mode);
4947 HOST_WIDE_INT d = INTVAL (op1);
4948 unsigned HOST_WIDE_INT ml;
4949 int pre_shift;
4950 rtx t1;
4951
4952 pre_shift = ctz_or_zero (d);
4953 ml = invert_mod2n (d >> pre_shift, size);
4954 t1 = expand_shift (RSHIFT_EXPR, int_mode, op0,
4955 pre_shift, NULL_RTX, unsignedp);
4956 quotient = expand_mult (int_mode, t1, gen_int_mode (ml, int_mode),
4957 NULL_RTX, 1);
4958
4959 insn = get_last_insn ();
4960 set_dst_reg_note (insn, REG_EQUAL,
4961 gen_rtx_fmt_ee (unsignedp ? UDIV : DIV,
4962 int_mode, op0, op1),
4963 quotient);
4964 }
4965 break;
4966
4967 case ROUND_DIV_EXPR:
4968 case ROUND_MOD_EXPR:
4969 if (unsignedp)
4970 {
4971 scalar_int_mode int_mode = as_a <scalar_int_mode> (compute_mode);
4972 rtx tem;
4973 rtx_code_label *label;
4974 label = gen_label_rtx ();
4975 quotient = gen_reg_rtx (int_mode);
4976 remainder = gen_reg_rtx (int_mode);
4977 if (expand_twoval_binop (udivmod_optab, op0, op1, quotient, remainder, 1) == 0)
4978 {
4979 rtx tem;
4980 quotient = expand_binop (int_mode, udiv_optab, op0, op1,
4981 quotient, 1, OPTAB_LIB_WIDEN);
4982 tem = expand_mult (int_mode, quotient, op1, NULL_RTX, 1);
4983 remainder = expand_binop (int_mode, sub_optab, op0, tem,
4984 remainder, 1, OPTAB_LIB_WIDEN);
4985 }
4986 tem = plus_constant (int_mode, op1, -1);
4987 tem = expand_shift (RSHIFT_EXPR, int_mode, tem, 1, NULL_RTX, 1);
4988 do_cmp_and_jump (remainder, tem, LEU, int_mode, label);
4989 expand_inc (quotient, const1_rtx);
4990 expand_dec (remainder, op1);
4991 emit_label (label);
4992 }
4993 else
4994 {
4995 scalar_int_mode int_mode = as_a <scalar_int_mode> (compute_mode);
4996 int size = GET_MODE_BITSIZE (int_mode);
4997 rtx abs_rem, abs_op1, tem, mask;
4998 rtx_code_label *label;
4999 label = gen_label_rtx ();
5000 quotient = gen_reg_rtx (int_mode);
5001 remainder = gen_reg_rtx (int_mode);
5002 if (expand_twoval_binop (sdivmod_optab, op0, op1, quotient, remainder, 0) == 0)
5003 {
5004 rtx tem;
5005 quotient = expand_binop (int_mode, sdiv_optab, op0, op1,
5006 quotient, 0, OPTAB_LIB_WIDEN);
5007 tem = expand_mult (int_mode, quotient, op1, NULL_RTX, 0);
5008 remainder = expand_binop (int_mode, sub_optab, op0, tem,
5009 remainder, 0, OPTAB_LIB_WIDEN);
5010 }
5011 abs_rem = expand_abs (int_mode, remainder, NULL_RTX, 1, 0);
5012 abs_op1 = expand_abs (int_mode, op1, NULL_RTX, 1, 0);
5013 tem = expand_shift (LSHIFT_EXPR, int_mode, abs_rem,
5014 1, NULL_RTX, 1);
5015 do_cmp_and_jump (tem, abs_op1, LTU, int_mode, label);
5016 tem = expand_binop (int_mode, xor_optab, op0, op1,
5017 NULL_RTX, 0, OPTAB_WIDEN);
5018 mask = expand_shift (RSHIFT_EXPR, int_mode, tem,
5019 size - 1, NULL_RTX, 0);
5020 tem = expand_binop (int_mode, xor_optab, mask, const1_rtx,
5021 NULL_RTX, 0, OPTAB_WIDEN);
5022 tem = expand_binop (int_mode, sub_optab, tem, mask,
5023 NULL_RTX, 0, OPTAB_WIDEN);
5024 expand_inc (quotient, tem);
5025 tem = expand_binop (int_mode, xor_optab, mask, op1,
5026 NULL_RTX, 0, OPTAB_WIDEN);
5027 tem = expand_binop (int_mode, sub_optab, tem, mask,
5028 NULL_RTX, 0, OPTAB_WIDEN);
5029 expand_dec (remainder, tem);
5030 emit_label (label);
5031 }
5032 return gen_lowpart (mode, rem_flag ? remainder : quotient);
5033
5034 default:
5035 gcc_unreachable ();
5036 }
5037
5038 if (quotient == 0)
5039 {
5040 if (target && GET_MODE (target) != compute_mode)
5041 target = 0;
5042
5043 if (rem_flag)
5044 {
5045 /* Try to produce the remainder without producing the quotient.
5046 If we seem to have a divmod pattern that does not require widening,
5047 don't try widening here. We should really have a WIDEN argument
5048 to expand_twoval_binop, since what we'd really like to do here is
5049 1) try a mod insn in compute_mode
5050 2) try a divmod insn in compute_mode
5051 3) try a div insn in compute_mode and multiply-subtract to get
5052 remainder
5053 4) try the same things with widening allowed. */
5054 remainder
5055 = sign_expand_binop (compute_mode, umod_optab, smod_optab,
5056 op0, op1, target,
5057 unsignedp,
5058 ((optab_handler (optab2, compute_mode)
5059 != CODE_FOR_nothing)
5060 ? OPTAB_DIRECT : OPTAB_WIDEN));
5061 if (remainder == 0)
5062 {
5063 /* No luck there. Can we do remainder and divide at once
5064 without a library call? */
5065 remainder = gen_reg_rtx (compute_mode);
5066 if (! expand_twoval_binop ((unsignedp
5067 ? udivmod_optab
5068 : sdivmod_optab),
5069 op0, op1,
5070 NULL_RTX, remainder, unsignedp))
5071 remainder = 0;
5072 }
5073
5074 if (remainder)
5075 return gen_lowpart (mode, remainder);
5076 }
5077
5078 /* Produce the quotient. Try a quotient insn, but not a library call.
5079 If we have a divmod in this mode, use it in preference to widening
5080 the div (for this test we assume it will not fail). Note that optab2
5081 is set to the one of the two optabs that the call below will use. */
5082 quotient
5083 = sign_expand_binop (compute_mode, udiv_optab, sdiv_optab,
5084 op0, op1, rem_flag ? NULL_RTX : target,
5085 unsignedp,
5086 ((optab_handler (optab2, compute_mode)
5087 != CODE_FOR_nothing)
5088 ? OPTAB_DIRECT : OPTAB_WIDEN));
5089
5090 if (quotient == 0)
5091 {
5092 /* No luck there. Try a quotient-and-remainder insn,
5093 keeping the quotient alone. */
5094 quotient = gen_reg_rtx (compute_mode);
5095 if (! expand_twoval_binop (unsignedp ? udivmod_optab : sdivmod_optab,
5096 op0, op1,
5097 quotient, NULL_RTX, unsignedp))
5098 {
5099 quotient = 0;
5100 if (! rem_flag)
5101 /* Still no luck. If we are not computing the remainder,
5102 use a library call for the quotient. */
5103 quotient = sign_expand_binop (compute_mode,
5104 udiv_optab, sdiv_optab,
5105 op0, op1, target,
5106 unsignedp, OPTAB_LIB_WIDEN);
5107 }
5108 }
5109 }
5110
5111 if (rem_flag)
5112 {
5113 if (target && GET_MODE (target) != compute_mode)
5114 target = 0;
5115
5116 if (quotient == 0)
5117 {
5118 /* No divide instruction either. Use library for remainder. */
5119 remainder = sign_expand_binop (compute_mode, umod_optab, smod_optab,
5120 op0, op1, target,
5121 unsignedp, OPTAB_LIB_WIDEN);
5122 /* No remainder function. Try a quotient-and-remainder
5123 function, keeping the remainder. */
5124 if (!remainder)
5125 {
5126 remainder = gen_reg_rtx (compute_mode);
5127 if (!expand_twoval_binop_libfunc
5128 (unsignedp ? udivmod_optab : sdivmod_optab,
5129 op0, op1,
5130 NULL_RTX, remainder,
5131 unsignedp ? UMOD : MOD))
5132 remainder = NULL_RTX;
5133 }
5134 }
5135 else
5136 {
5137 /* We divided. Now finish doing X - Y * (X / Y). */
5138 remainder = expand_mult (compute_mode, quotient, op1,
5139 NULL_RTX, unsignedp);
5140 remainder = expand_binop (compute_mode, sub_optab, op0,
5141 remainder, target, unsignedp,
5142 OPTAB_LIB_WIDEN);
5143 }
5144 }
5145
5146 return gen_lowpart (mode, rem_flag ? remainder : quotient);
5147 }
5148 \f
5149 /* Return a tree node with data type TYPE, describing the value of X.
5150 Usually this is an VAR_DECL, if there is no obvious better choice.
5151 X may be an expression, however we only support those expressions
5152 generated by loop.c. */
5153
5154 tree
5155 make_tree (tree type, rtx x)
5156 {
5157 tree t;
5158
5159 switch (GET_CODE (x))
5160 {
5161 case CONST_INT:
5162 case CONST_WIDE_INT:
5163 t = wide_int_to_tree (type, rtx_mode_t (x, TYPE_MODE (type)));
5164 return t;
5165
5166 case CONST_DOUBLE:
5167 STATIC_ASSERT (HOST_BITS_PER_WIDE_INT * 2 <= MAX_BITSIZE_MODE_ANY_INT);
5168 if (TARGET_SUPPORTS_WIDE_INT == 0 && GET_MODE (x) == VOIDmode)
5169 t = wide_int_to_tree (type,
5170 wide_int::from_array (&CONST_DOUBLE_LOW (x), 2,
5171 HOST_BITS_PER_WIDE_INT * 2));
5172 else
5173 t = build_real (type, *CONST_DOUBLE_REAL_VALUE (x));
5174
5175 return t;
5176
5177 case CONST_VECTOR:
5178 {
5179 int units = CONST_VECTOR_NUNITS (x);
5180 tree itype = TREE_TYPE (type);
5181 int i;
5182
5183 /* Build a tree with vector elements. */
5184 auto_vec<tree, 32> elts (units);
5185 for (i = 0; i < units; ++i)
5186 {
5187 rtx elt = CONST_VECTOR_ELT (x, i);
5188 elts.quick_push (make_tree (itype, elt));
5189 }
5190
5191 return build_vector (type, elts);
5192 }
5193
5194 case PLUS:
5195 return fold_build2 (PLUS_EXPR, type, make_tree (type, XEXP (x, 0)),
5196 make_tree (type, XEXP (x, 1)));
5197
5198 case MINUS:
5199 return fold_build2 (MINUS_EXPR, type, make_tree (type, XEXP (x, 0)),
5200 make_tree (type, XEXP (x, 1)));
5201
5202 case NEG:
5203 return fold_build1 (NEGATE_EXPR, type, make_tree (type, XEXP (x, 0)));
5204
5205 case MULT:
5206 return fold_build2 (MULT_EXPR, type, make_tree (type, XEXP (x, 0)),
5207 make_tree (type, XEXP (x, 1)));
5208
5209 case ASHIFT:
5210 return fold_build2 (LSHIFT_EXPR, type, make_tree (type, XEXP (x, 0)),
5211 make_tree (type, XEXP (x, 1)));
5212
5213 case LSHIFTRT:
5214 t = unsigned_type_for (type);
5215 return fold_convert (type, build2 (RSHIFT_EXPR, t,
5216 make_tree (t, XEXP (x, 0)),
5217 make_tree (type, XEXP (x, 1))));
5218
5219 case ASHIFTRT:
5220 t = signed_type_for (type);
5221 return fold_convert (type, build2 (RSHIFT_EXPR, t,
5222 make_tree (t, XEXP (x, 0)),
5223 make_tree (type, XEXP (x, 1))));
5224
5225 case DIV:
5226 if (TREE_CODE (type) != REAL_TYPE)
5227 t = signed_type_for (type);
5228 else
5229 t = type;
5230
5231 return fold_convert (type, build2 (TRUNC_DIV_EXPR, t,
5232 make_tree (t, XEXP (x, 0)),
5233 make_tree (t, XEXP (x, 1))));
5234 case UDIV:
5235 t = unsigned_type_for (type);
5236 return fold_convert (type, build2 (TRUNC_DIV_EXPR, t,
5237 make_tree (t, XEXP (x, 0)),
5238 make_tree (t, XEXP (x, 1))));
5239
5240 case SIGN_EXTEND:
5241 case ZERO_EXTEND:
5242 t = lang_hooks.types.type_for_mode (GET_MODE (XEXP (x, 0)),
5243 GET_CODE (x) == ZERO_EXTEND);
5244 return fold_convert (type, make_tree (t, XEXP (x, 0)));
5245
5246 case CONST:
5247 {
5248 rtx op = XEXP (x, 0);
5249 if (GET_CODE (op) == VEC_DUPLICATE)
5250 {
5251 tree elt_tree = make_tree (TREE_TYPE (type), XEXP (op, 0));
5252 return build_vector_from_val (type, elt_tree);
5253 }
5254 return make_tree (type, op);
5255 }
5256
5257 case SYMBOL_REF:
5258 t = SYMBOL_REF_DECL (x);
5259 if (t)
5260 return fold_convert (type, build_fold_addr_expr (t));
5261 /* fall through. */
5262
5263 default:
5264 t = build_decl (RTL_LOCATION (x), VAR_DECL, NULL_TREE, type);
5265
5266 /* If TYPE is a POINTER_TYPE, we might need to convert X from
5267 address mode to pointer mode. */
5268 if (POINTER_TYPE_P (type))
5269 x = convert_memory_address_addr_space
5270 (SCALAR_INT_TYPE_MODE (type), x, TYPE_ADDR_SPACE (TREE_TYPE (type)));
5271
5272 /* Note that we do *not* use SET_DECL_RTL here, because we do not
5273 want set_decl_rtl to go adjusting REG_ATTRS for this temporary. */
5274 t->decl_with_rtl.rtl = x;
5275
5276 return t;
5277 }
5278 }
5279 \f
5280 /* Compute the logical-and of OP0 and OP1, storing it in TARGET
5281 and returning TARGET.
5282
5283 If TARGET is 0, a pseudo-register or constant is returned. */
5284
5285 rtx
5286 expand_and (machine_mode mode, rtx op0, rtx op1, rtx target)
5287 {
5288 rtx tem = 0;
5289
5290 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
5291 tem = simplify_binary_operation (AND, mode, op0, op1);
5292 if (tem == 0)
5293 tem = expand_binop (mode, and_optab, op0, op1, target, 0, OPTAB_LIB_WIDEN);
5294
5295 if (target == 0)
5296 target = tem;
5297 else if (tem != target)
5298 emit_move_insn (target, tem);
5299 return target;
5300 }
5301
5302 /* Helper function for emit_store_flag. */
5303 rtx
5304 emit_cstore (rtx target, enum insn_code icode, enum rtx_code code,
5305 machine_mode mode, machine_mode compare_mode,
5306 int unsignedp, rtx x, rtx y, int normalizep,
5307 machine_mode target_mode)
5308 {
5309 struct expand_operand ops[4];
5310 rtx op0, comparison, subtarget;
5311 rtx_insn *last;
5312 scalar_int_mode result_mode = targetm.cstore_mode (icode);
5313 scalar_int_mode int_target_mode;
5314
5315 last = get_last_insn ();
5316 x = prepare_operand (icode, x, 2, mode, compare_mode, unsignedp);
5317 y = prepare_operand (icode, y, 3, mode, compare_mode, unsignedp);
5318 if (!x || !y)
5319 {
5320 delete_insns_since (last);
5321 return NULL_RTX;
5322 }
5323
5324 if (target_mode == VOIDmode)
5325 int_target_mode = result_mode;
5326 else
5327 int_target_mode = as_a <scalar_int_mode> (target_mode);
5328 if (!target)
5329 target = gen_reg_rtx (int_target_mode);
5330
5331 comparison = gen_rtx_fmt_ee (code, result_mode, x, y);
5332
5333 create_output_operand (&ops[0], optimize ? NULL_RTX : target, result_mode);
5334 create_fixed_operand (&ops[1], comparison);
5335 create_fixed_operand (&ops[2], x);
5336 create_fixed_operand (&ops[3], y);
5337 if (!maybe_expand_insn (icode, 4, ops))
5338 {
5339 delete_insns_since (last);
5340 return NULL_RTX;
5341 }
5342 subtarget = ops[0].value;
5343
5344 /* If we are converting to a wider mode, first convert to
5345 INT_TARGET_MODE, then normalize. This produces better combining
5346 opportunities on machines that have a SIGN_EXTRACT when we are
5347 testing a single bit. This mostly benefits the 68k.
5348
5349 If STORE_FLAG_VALUE does not have the sign bit set when
5350 interpreted in MODE, we can do this conversion as unsigned, which
5351 is usually more efficient. */
5352 if (GET_MODE_SIZE (int_target_mode) > GET_MODE_SIZE (result_mode))
5353 {
5354 convert_move (target, subtarget,
5355 val_signbit_known_clear_p (result_mode,
5356 STORE_FLAG_VALUE));
5357 op0 = target;
5358 result_mode = int_target_mode;
5359 }
5360 else
5361 op0 = subtarget;
5362
5363 /* If we want to keep subexpressions around, don't reuse our last
5364 target. */
5365 if (optimize)
5366 subtarget = 0;
5367
5368 /* Now normalize to the proper value in MODE. Sometimes we don't
5369 have to do anything. */
5370 if (normalizep == 0 || normalizep == STORE_FLAG_VALUE)
5371 ;
5372 /* STORE_FLAG_VALUE might be the most negative number, so write
5373 the comparison this way to avoid a compiler-time warning. */
5374 else if (- normalizep == STORE_FLAG_VALUE)
5375 op0 = expand_unop (result_mode, neg_optab, op0, subtarget, 0);
5376
5377 /* We don't want to use STORE_FLAG_VALUE < 0 below since this makes
5378 it hard to use a value of just the sign bit due to ANSI integer
5379 constant typing rules. */
5380 else if (val_signbit_known_set_p (result_mode, STORE_FLAG_VALUE))
5381 op0 = expand_shift (RSHIFT_EXPR, result_mode, op0,
5382 GET_MODE_BITSIZE (result_mode) - 1, subtarget,
5383 normalizep == 1);
5384 else
5385 {
5386 gcc_assert (STORE_FLAG_VALUE & 1);
5387
5388 op0 = expand_and (result_mode, op0, const1_rtx, subtarget);
5389 if (normalizep == -1)
5390 op0 = expand_unop (result_mode, neg_optab, op0, op0, 0);
5391 }
5392
5393 /* If we were converting to a smaller mode, do the conversion now. */
5394 if (int_target_mode != result_mode)
5395 {
5396 convert_move (target, op0, 0);
5397 return target;
5398 }
5399 else
5400 return op0;
5401 }
5402
5403
5404 /* A subroutine of emit_store_flag only including "tricks" that do not
5405 need a recursive call. These are kept separate to avoid infinite
5406 loops. */
5407
5408 static rtx
5409 emit_store_flag_1 (rtx target, enum rtx_code code, rtx op0, rtx op1,
5410 machine_mode mode, int unsignedp, int normalizep,
5411 machine_mode target_mode)
5412 {
5413 rtx subtarget;
5414 enum insn_code icode;
5415 machine_mode compare_mode;
5416 enum mode_class mclass;
5417 enum rtx_code scode;
5418
5419 if (unsignedp)
5420 code = unsigned_condition (code);
5421 scode = swap_condition (code);
5422
5423 /* If one operand is constant, make it the second one. Only do this
5424 if the other operand is not constant as well. */
5425
5426 if (swap_commutative_operands_p (op0, op1))
5427 {
5428 std::swap (op0, op1);
5429 code = swap_condition (code);
5430 }
5431
5432 if (mode == VOIDmode)
5433 mode = GET_MODE (op0);
5434
5435 /* For some comparisons with 1 and -1, we can convert this to
5436 comparisons with zero. This will often produce more opportunities for
5437 store-flag insns. */
5438
5439 switch (code)
5440 {
5441 case LT:
5442 if (op1 == const1_rtx)
5443 op1 = const0_rtx, code = LE;
5444 break;
5445 case LE:
5446 if (op1 == constm1_rtx)
5447 op1 = const0_rtx, code = LT;
5448 break;
5449 case GE:
5450 if (op1 == const1_rtx)
5451 op1 = const0_rtx, code = GT;
5452 break;
5453 case GT:
5454 if (op1 == constm1_rtx)
5455 op1 = const0_rtx, code = GE;
5456 break;
5457 case GEU:
5458 if (op1 == const1_rtx)
5459 op1 = const0_rtx, code = NE;
5460 break;
5461 case LTU:
5462 if (op1 == const1_rtx)
5463 op1 = const0_rtx, code = EQ;
5464 break;
5465 default:
5466 break;
5467 }
5468
5469 /* If we are comparing a double-word integer with zero or -1, we can
5470 convert the comparison into one involving a single word. */
5471 scalar_int_mode int_mode;
5472 if (is_int_mode (mode, &int_mode)
5473 && GET_MODE_BITSIZE (int_mode) == BITS_PER_WORD * 2
5474 && (!MEM_P (op0) || ! MEM_VOLATILE_P (op0)))
5475 {
5476 rtx tem;
5477 if ((code == EQ || code == NE)
5478 && (op1 == const0_rtx || op1 == constm1_rtx))
5479 {
5480 rtx op00, op01;
5481
5482 /* Do a logical OR or AND of the two words and compare the
5483 result. */
5484 op00 = simplify_gen_subreg (word_mode, op0, int_mode, 0);
5485 op01 = simplify_gen_subreg (word_mode, op0, int_mode, UNITS_PER_WORD);
5486 tem = expand_binop (word_mode,
5487 op1 == const0_rtx ? ior_optab : and_optab,
5488 op00, op01, NULL_RTX, unsignedp,
5489 OPTAB_DIRECT);
5490
5491 if (tem != 0)
5492 tem = emit_store_flag (NULL_RTX, code, tem, op1, word_mode,
5493 unsignedp, normalizep);
5494 }
5495 else if ((code == LT || code == GE) && op1 == const0_rtx)
5496 {
5497 rtx op0h;
5498
5499 /* If testing the sign bit, can just test on high word. */
5500 op0h = simplify_gen_subreg (word_mode, op0, int_mode,
5501 subreg_highpart_offset (word_mode,
5502 int_mode));
5503 tem = emit_store_flag (NULL_RTX, code, op0h, op1, word_mode,
5504 unsignedp, normalizep);
5505 }
5506 else
5507 tem = NULL_RTX;
5508
5509 if (tem)
5510 {
5511 if (target_mode == VOIDmode || GET_MODE (tem) == target_mode)
5512 return tem;
5513 if (!target)
5514 target = gen_reg_rtx (target_mode);
5515
5516 convert_move (target, tem,
5517 !val_signbit_known_set_p (word_mode,
5518 (normalizep ? normalizep
5519 : STORE_FLAG_VALUE)));
5520 return target;
5521 }
5522 }
5523
5524 /* If this is A < 0 or A >= 0, we can do this by taking the ones
5525 complement of A (for GE) and shifting the sign bit to the low bit. */
5526 if (op1 == const0_rtx && (code == LT || code == GE)
5527 && is_int_mode (mode, &int_mode)
5528 && (normalizep || STORE_FLAG_VALUE == 1
5529 || val_signbit_p (int_mode, STORE_FLAG_VALUE)))
5530 {
5531 scalar_int_mode int_target_mode;
5532 subtarget = target;
5533
5534 if (!target)
5535 int_target_mode = int_mode;
5536 else
5537 {
5538 /* If the result is to be wider than OP0, it is best to convert it
5539 first. If it is to be narrower, it is *incorrect* to convert it
5540 first. */
5541 int_target_mode = as_a <scalar_int_mode> (target_mode);
5542 if (GET_MODE_SIZE (int_target_mode) > GET_MODE_SIZE (int_mode))
5543 {
5544 op0 = convert_modes (int_target_mode, int_mode, op0, 0);
5545 int_mode = int_target_mode;
5546 }
5547 }
5548
5549 if (int_target_mode != int_mode)
5550 subtarget = 0;
5551
5552 if (code == GE)
5553 op0 = expand_unop (int_mode, one_cmpl_optab, op0,
5554 ((STORE_FLAG_VALUE == 1 || normalizep)
5555 ? 0 : subtarget), 0);
5556
5557 if (STORE_FLAG_VALUE == 1 || normalizep)
5558 /* If we are supposed to produce a 0/1 value, we want to do
5559 a logical shift from the sign bit to the low-order bit; for
5560 a -1/0 value, we do an arithmetic shift. */
5561 op0 = expand_shift (RSHIFT_EXPR, int_mode, op0,
5562 GET_MODE_BITSIZE (int_mode) - 1,
5563 subtarget, normalizep != -1);
5564
5565 if (int_mode != int_target_mode)
5566 op0 = convert_modes (int_target_mode, int_mode, op0, 0);
5567
5568 return op0;
5569 }
5570
5571 mclass = GET_MODE_CLASS (mode);
5572 FOR_EACH_MODE_FROM (compare_mode, mode)
5573 {
5574 machine_mode optab_mode = mclass == MODE_CC ? CCmode : compare_mode;
5575 icode = optab_handler (cstore_optab, optab_mode);
5576 if (icode != CODE_FOR_nothing)
5577 {
5578 do_pending_stack_adjust ();
5579 rtx tem = emit_cstore (target, icode, code, mode, compare_mode,
5580 unsignedp, op0, op1, normalizep, target_mode);
5581 if (tem)
5582 return tem;
5583
5584 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5585 {
5586 tem = emit_cstore (target, icode, scode, mode, compare_mode,
5587 unsignedp, op1, op0, normalizep, target_mode);
5588 if (tem)
5589 return tem;
5590 }
5591 break;
5592 }
5593 }
5594
5595 return 0;
5596 }
5597
5598 /* Subroutine of emit_store_flag that handles cases in which the operands
5599 are scalar integers. SUBTARGET is the target to use for temporary
5600 operations and TRUEVAL is the value to store when the condition is
5601 true. All other arguments are as for emit_store_flag. */
5602
5603 rtx
5604 emit_store_flag_int (rtx target, rtx subtarget, enum rtx_code code, rtx op0,
5605 rtx op1, scalar_int_mode mode, int unsignedp,
5606 int normalizep, rtx trueval)
5607 {
5608 machine_mode target_mode = target ? GET_MODE (target) : VOIDmode;
5609 rtx_insn *last = get_last_insn ();
5610
5611 /* If this is an equality comparison of integers, we can try to exclusive-or
5612 (or subtract) the two operands and use a recursive call to try the
5613 comparison with zero. Don't do any of these cases if branches are
5614 very cheap. */
5615
5616 if ((code == EQ || code == NE) && op1 != const0_rtx)
5617 {
5618 rtx tem = expand_binop (mode, xor_optab, op0, op1, subtarget, 1,
5619 OPTAB_WIDEN);
5620
5621 if (tem == 0)
5622 tem = expand_binop (mode, sub_optab, op0, op1, subtarget, 1,
5623 OPTAB_WIDEN);
5624 if (tem != 0)
5625 tem = emit_store_flag (target, code, tem, const0_rtx,
5626 mode, unsignedp, normalizep);
5627 if (tem != 0)
5628 return tem;
5629
5630 delete_insns_since (last);
5631 }
5632
5633 /* For integer comparisons, try the reverse comparison. However, for
5634 small X and if we'd have anyway to extend, implementing "X != 0"
5635 as "-(int)X >> 31" is still cheaper than inverting "(int)X == 0". */
5636 rtx_code rcode = reverse_condition (code);
5637 if (can_compare_p (rcode, mode, ccp_store_flag)
5638 && ! (optab_handler (cstore_optab, mode) == CODE_FOR_nothing
5639 && code == NE
5640 && GET_MODE_SIZE (mode) < UNITS_PER_WORD
5641 && op1 == const0_rtx))
5642 {
5643 int want_add = ((STORE_FLAG_VALUE == 1 && normalizep == -1)
5644 || (STORE_FLAG_VALUE == -1 && normalizep == 1));
5645
5646 /* Again, for the reverse comparison, use either an addition or a XOR. */
5647 if (want_add
5648 && rtx_cost (GEN_INT (normalizep), mode, PLUS, 1,
5649 optimize_insn_for_speed_p ()) == 0)
5650 {
5651 rtx tem = emit_store_flag_1 (subtarget, rcode, op0, op1, mode, 0,
5652 STORE_FLAG_VALUE, target_mode);
5653 if (tem != 0)
5654 tem = expand_binop (target_mode, add_optab, tem,
5655 gen_int_mode (normalizep, target_mode),
5656 target, 0, OPTAB_WIDEN);
5657 if (tem != 0)
5658 return tem;
5659 }
5660 else if (!want_add
5661 && rtx_cost (trueval, mode, XOR, 1,
5662 optimize_insn_for_speed_p ()) == 0)
5663 {
5664 rtx tem = emit_store_flag_1 (subtarget, rcode, op0, op1, mode, 0,
5665 normalizep, target_mode);
5666 if (tem != 0)
5667 tem = expand_binop (target_mode, xor_optab, tem, trueval, target,
5668 INTVAL (trueval) >= 0, OPTAB_WIDEN);
5669 if (tem != 0)
5670 return tem;
5671 }
5672
5673 delete_insns_since (last);
5674 }
5675
5676 /* Some other cases we can do are EQ, NE, LE, and GT comparisons with
5677 the constant zero. Reject all other comparisons at this point. Only
5678 do LE and GT if branches are expensive since they are expensive on
5679 2-operand machines. */
5680
5681 if (op1 != const0_rtx
5682 || (code != EQ && code != NE
5683 && (BRANCH_COST (optimize_insn_for_speed_p (),
5684 false) <= 1 || (code != LE && code != GT))))
5685 return 0;
5686
5687 /* Try to put the result of the comparison in the sign bit. Assume we can't
5688 do the necessary operation below. */
5689
5690 rtx tem = 0;
5691
5692 /* To see if A <= 0, compute (A | (A - 1)). A <= 0 iff that result has
5693 the sign bit set. */
5694
5695 if (code == LE)
5696 {
5697 /* This is destructive, so SUBTARGET can't be OP0. */
5698 if (rtx_equal_p (subtarget, op0))
5699 subtarget = 0;
5700
5701 tem = expand_binop (mode, sub_optab, op0, const1_rtx, subtarget, 0,
5702 OPTAB_WIDEN);
5703 if (tem)
5704 tem = expand_binop (mode, ior_optab, op0, tem, subtarget, 0,
5705 OPTAB_WIDEN);
5706 }
5707
5708 /* To see if A > 0, compute (((signed) A) << BITS) - A, where BITS is the
5709 number of bits in the mode of OP0, minus one. */
5710
5711 if (code == GT)
5712 {
5713 if (rtx_equal_p (subtarget, op0))
5714 subtarget = 0;
5715
5716 tem = maybe_expand_shift (RSHIFT_EXPR, mode, op0,
5717 GET_MODE_BITSIZE (mode) - 1,
5718 subtarget, 0);
5719 if (tem)
5720 tem = expand_binop (mode, sub_optab, tem, op0, subtarget, 0,
5721 OPTAB_WIDEN);
5722 }
5723
5724 if (code == EQ || code == NE)
5725 {
5726 /* For EQ or NE, one way to do the comparison is to apply an operation
5727 that converts the operand into a positive number if it is nonzero
5728 or zero if it was originally zero. Then, for EQ, we subtract 1 and
5729 for NE we negate. This puts the result in the sign bit. Then we
5730 normalize with a shift, if needed.
5731
5732 Two operations that can do the above actions are ABS and FFS, so try
5733 them. If that doesn't work, and MODE is smaller than a full word,
5734 we can use zero-extension to the wider mode (an unsigned conversion)
5735 as the operation. */
5736
5737 /* Note that ABS doesn't yield a positive number for INT_MIN, but
5738 that is compensated by the subsequent overflow when subtracting
5739 one / negating. */
5740
5741 if (optab_handler (abs_optab, mode) != CODE_FOR_nothing)
5742 tem = expand_unop (mode, abs_optab, op0, subtarget, 1);
5743 else if (optab_handler (ffs_optab, mode) != CODE_FOR_nothing)
5744 tem = expand_unop (mode, ffs_optab, op0, subtarget, 1);
5745 else if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
5746 {
5747 tem = convert_modes (word_mode, mode, op0, 1);
5748 mode = word_mode;
5749 }
5750
5751 if (tem != 0)
5752 {
5753 if (code == EQ)
5754 tem = expand_binop (mode, sub_optab, tem, const1_rtx, subtarget,
5755 0, OPTAB_WIDEN);
5756 else
5757 tem = expand_unop (mode, neg_optab, tem, subtarget, 0);
5758 }
5759
5760 /* If we couldn't do it that way, for NE we can "or" the two's complement
5761 of the value with itself. For EQ, we take the one's complement of
5762 that "or", which is an extra insn, so we only handle EQ if branches
5763 are expensive. */
5764
5765 if (tem == 0
5766 && (code == NE
5767 || BRANCH_COST (optimize_insn_for_speed_p (),
5768 false) > 1))
5769 {
5770 if (rtx_equal_p (subtarget, op0))
5771 subtarget = 0;
5772
5773 tem = expand_unop (mode, neg_optab, op0, subtarget, 0);
5774 tem = expand_binop (mode, ior_optab, tem, op0, subtarget, 0,
5775 OPTAB_WIDEN);
5776
5777 if (tem && code == EQ)
5778 tem = expand_unop (mode, one_cmpl_optab, tem, subtarget, 0);
5779 }
5780 }
5781
5782 if (tem && normalizep)
5783 tem = maybe_expand_shift (RSHIFT_EXPR, mode, tem,
5784 GET_MODE_BITSIZE (mode) - 1,
5785 subtarget, normalizep == 1);
5786
5787 if (tem)
5788 {
5789 if (!target)
5790 ;
5791 else if (GET_MODE (tem) != target_mode)
5792 {
5793 convert_move (target, tem, 0);
5794 tem = target;
5795 }
5796 else if (!subtarget)
5797 {
5798 emit_move_insn (target, tem);
5799 tem = target;
5800 }
5801 }
5802 else
5803 delete_insns_since (last);
5804
5805 return tem;
5806 }
5807
5808 /* Emit a store-flags instruction for comparison CODE on OP0 and OP1
5809 and storing in TARGET. Normally return TARGET.
5810 Return 0 if that cannot be done.
5811
5812 MODE is the mode to use for OP0 and OP1 should they be CONST_INTs. If
5813 it is VOIDmode, they cannot both be CONST_INT.
5814
5815 UNSIGNEDP is for the case where we have to widen the operands
5816 to perform the operation. It says to use zero-extension.
5817
5818 NORMALIZEP is 1 if we should convert the result to be either zero
5819 or one. Normalize is -1 if we should convert the result to be
5820 either zero or -1. If NORMALIZEP is zero, the result will be left
5821 "raw" out of the scc insn. */
5822
5823 rtx
5824 emit_store_flag (rtx target, enum rtx_code code, rtx op0, rtx op1,
5825 machine_mode mode, int unsignedp, int normalizep)
5826 {
5827 machine_mode target_mode = target ? GET_MODE (target) : VOIDmode;
5828 enum rtx_code rcode;
5829 rtx subtarget;
5830 rtx tem, trueval;
5831 rtx_insn *last;
5832
5833 /* If we compare constants, we shouldn't use a store-flag operation,
5834 but a constant load. We can get there via the vanilla route that
5835 usually generates a compare-branch sequence, but will in this case
5836 fold the comparison to a constant, and thus elide the branch. */
5837 if (CONSTANT_P (op0) && CONSTANT_P (op1))
5838 return NULL_RTX;
5839
5840 tem = emit_store_flag_1 (target, code, op0, op1, mode, unsignedp, normalizep,
5841 target_mode);
5842 if (tem)
5843 return tem;
5844
5845 /* If we reached here, we can't do this with a scc insn, however there
5846 are some comparisons that can be done in other ways. Don't do any
5847 of these cases if branches are very cheap. */
5848 if (BRANCH_COST (optimize_insn_for_speed_p (), false) == 0)
5849 return 0;
5850
5851 /* See what we need to return. We can only return a 1, -1, or the
5852 sign bit. */
5853
5854 if (normalizep == 0)
5855 {
5856 if (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
5857 normalizep = STORE_FLAG_VALUE;
5858
5859 else if (val_signbit_p (mode, STORE_FLAG_VALUE))
5860 ;
5861 else
5862 return 0;
5863 }
5864
5865 last = get_last_insn ();
5866
5867 /* If optimizing, use different pseudo registers for each insn, instead
5868 of reusing the same pseudo. This leads to better CSE, but slows
5869 down the compiler, since there are more pseudos. */
5870 subtarget = (!optimize
5871 && (target_mode == mode)) ? target : NULL_RTX;
5872 trueval = GEN_INT (normalizep ? normalizep : STORE_FLAG_VALUE);
5873
5874 /* For floating-point comparisons, try the reverse comparison or try
5875 changing the "orderedness" of the comparison. */
5876 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5877 {
5878 enum rtx_code first_code;
5879 bool and_them;
5880
5881 rcode = reverse_condition_maybe_unordered (code);
5882 if (can_compare_p (rcode, mode, ccp_store_flag)
5883 && (code == ORDERED || code == UNORDERED
5884 || (! HONOR_NANS (mode) && (code == LTGT || code == UNEQ))
5885 || (! HONOR_SNANS (mode) && (code == EQ || code == NE))))
5886 {
5887 int want_add = ((STORE_FLAG_VALUE == 1 && normalizep == -1)
5888 || (STORE_FLAG_VALUE == -1 && normalizep == 1));
5889
5890 /* For the reverse comparison, use either an addition or a XOR. */
5891 if (want_add
5892 && rtx_cost (GEN_INT (normalizep), mode, PLUS, 1,
5893 optimize_insn_for_speed_p ()) == 0)
5894 {
5895 tem = emit_store_flag_1 (subtarget, rcode, op0, op1, mode, 0,
5896 STORE_FLAG_VALUE, target_mode);
5897 if (tem)
5898 return expand_binop (target_mode, add_optab, tem,
5899 gen_int_mode (normalizep, target_mode),
5900 target, 0, OPTAB_WIDEN);
5901 }
5902 else if (!want_add
5903 && rtx_cost (trueval, mode, XOR, 1,
5904 optimize_insn_for_speed_p ()) == 0)
5905 {
5906 tem = emit_store_flag_1 (subtarget, rcode, op0, op1, mode, 0,
5907 normalizep, target_mode);
5908 if (tem)
5909 return expand_binop (target_mode, xor_optab, tem, trueval,
5910 target, INTVAL (trueval) >= 0,
5911 OPTAB_WIDEN);
5912 }
5913 }
5914
5915 delete_insns_since (last);
5916
5917 /* Cannot split ORDERED and UNORDERED, only try the above trick. */
5918 if (code == ORDERED || code == UNORDERED)
5919 return 0;
5920
5921 and_them = split_comparison (code, mode, &first_code, &code);
5922
5923 /* If there are no NaNs, the first comparison should always fall through.
5924 Effectively change the comparison to the other one. */
5925 if (!HONOR_NANS (mode))
5926 {
5927 gcc_assert (first_code == (and_them ? ORDERED : UNORDERED));
5928 return emit_store_flag_1 (target, code, op0, op1, mode, 0, normalizep,
5929 target_mode);
5930 }
5931
5932 if (!HAVE_conditional_move)
5933 return 0;
5934
5935 /* Try using a setcc instruction for ORDERED/UNORDERED, followed by a
5936 conditional move. */
5937 tem = emit_store_flag_1 (subtarget, first_code, op0, op1, mode, 0,
5938 normalizep, target_mode);
5939 if (tem == 0)
5940 return 0;
5941
5942 if (and_them)
5943 tem = emit_conditional_move (target, code, op0, op1, mode,
5944 tem, const0_rtx, GET_MODE (tem), 0);
5945 else
5946 tem = emit_conditional_move (target, code, op0, op1, mode,
5947 trueval, tem, GET_MODE (tem), 0);
5948
5949 if (tem == 0)
5950 delete_insns_since (last);
5951 return tem;
5952 }
5953
5954 /* The remaining tricks only apply to integer comparisons. */
5955
5956 scalar_int_mode int_mode;
5957 if (is_int_mode (mode, &int_mode))
5958 return emit_store_flag_int (target, subtarget, code, op0, op1, int_mode,
5959 unsignedp, normalizep, trueval);
5960
5961 return 0;
5962 }
5963
5964 /* Like emit_store_flag, but always succeeds. */
5965
5966 rtx
5967 emit_store_flag_force (rtx target, enum rtx_code code, rtx op0, rtx op1,
5968 machine_mode mode, int unsignedp, int normalizep)
5969 {
5970 rtx tem;
5971 rtx_code_label *label;
5972 rtx trueval, falseval;
5973
5974 /* First see if emit_store_flag can do the job. */
5975 tem = emit_store_flag (target, code, op0, op1, mode, unsignedp, normalizep);
5976 if (tem != 0)
5977 return tem;
5978
5979 if (!target)
5980 target = gen_reg_rtx (word_mode);
5981
5982 /* If this failed, we have to do this with set/compare/jump/set code.
5983 For foo != 0, if foo is in OP0, just replace it with 1 if nonzero. */
5984 trueval = normalizep ? GEN_INT (normalizep) : const1_rtx;
5985 if (code == NE
5986 && GET_MODE_CLASS (mode) == MODE_INT
5987 && REG_P (target)
5988 && op0 == target
5989 && op1 == const0_rtx)
5990 {
5991 label = gen_label_rtx ();
5992 do_compare_rtx_and_jump (target, const0_rtx, EQ, unsignedp, mode,
5993 NULL_RTX, NULL, label,
5994 profile_probability::uninitialized ());
5995 emit_move_insn (target, trueval);
5996 emit_label (label);
5997 return target;
5998 }
5999
6000 if (!REG_P (target)
6001 || reg_mentioned_p (target, op0) || reg_mentioned_p (target, op1))
6002 target = gen_reg_rtx (GET_MODE (target));
6003
6004 /* Jump in the right direction if the target cannot implement CODE
6005 but can jump on its reverse condition. */
6006 falseval = const0_rtx;
6007 if (! can_compare_p (code, mode, ccp_jump)
6008 && (! FLOAT_MODE_P (mode)
6009 || code == ORDERED || code == UNORDERED
6010 || (! HONOR_NANS (mode) && (code == LTGT || code == UNEQ))
6011 || (! HONOR_SNANS (mode) && (code == EQ || code == NE))))
6012 {
6013 enum rtx_code rcode;
6014 if (FLOAT_MODE_P (mode))
6015 rcode = reverse_condition_maybe_unordered (code);
6016 else
6017 rcode = reverse_condition (code);
6018
6019 /* Canonicalize to UNORDERED for the libcall. */
6020 if (can_compare_p (rcode, mode, ccp_jump)
6021 || (code == ORDERED && ! can_compare_p (ORDERED, mode, ccp_jump)))
6022 {
6023 falseval = trueval;
6024 trueval = const0_rtx;
6025 code = rcode;
6026 }
6027 }
6028
6029 emit_move_insn (target, trueval);
6030 label = gen_label_rtx ();
6031 do_compare_rtx_and_jump (op0, op1, code, unsignedp, mode, NULL_RTX, NULL,
6032 label, profile_probability::uninitialized ());
6033
6034 emit_move_insn (target, falseval);
6035 emit_label (label);
6036
6037 return target;
6038 }
6039 \f
6040 /* Perform possibly multi-word comparison and conditional jump to LABEL
6041 if ARG1 OP ARG2 true where ARG1 and ARG2 are of mode MODE. This is
6042 now a thin wrapper around do_compare_rtx_and_jump. */
6043
6044 static void
6045 do_cmp_and_jump (rtx arg1, rtx arg2, enum rtx_code op, machine_mode mode,
6046 rtx_code_label *label)
6047 {
6048 int unsignedp = (op == LTU || op == LEU || op == GTU || op == GEU);
6049 do_compare_rtx_and_jump (arg1, arg2, op, unsignedp, mode, NULL_RTX,
6050 NULL, label, profile_probability::uninitialized ());
6051 }