Merge with gcc-4_3-branch up to revision 175516.
[official-gcc.git] / gcc / expmed.c
blob829d6bbdca082c715cccc9ac54f19417d5513c7f
1 /* Medium-level subroutines: convert bit-field store and extract
2 and shifts, multiplies and divides to rtl instructions.
3 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
5 Free Software Foundation, Inc.
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "toplev.h"
29 #include "rtl.h"
30 #include "tree.h"
31 #include "tm_p.h"
32 #include "flags.h"
33 #include "insn-config.h"
34 #include "expr.h"
35 #include "optabs.h"
36 #include "real.h"
37 #include "recog.h"
38 #include "langhooks.h"
39 #include "df.h"
40 #include "target.h"
42 static void store_fixed_bit_field (rtx, unsigned HOST_WIDE_INT,
43 unsigned HOST_WIDE_INT,
44 unsigned HOST_WIDE_INT, rtx);
45 static void store_split_bit_field (rtx, unsigned HOST_WIDE_INT,
46 unsigned HOST_WIDE_INT, rtx);
47 static rtx extract_fixed_bit_field (enum machine_mode, rtx,
48 unsigned HOST_WIDE_INT,
49 unsigned HOST_WIDE_INT,
50 unsigned HOST_WIDE_INT, rtx, int);
51 static rtx mask_rtx (enum machine_mode, int, int, int);
52 static rtx lshift_value (enum machine_mode, rtx, int, int);
53 static rtx extract_split_bit_field (rtx, unsigned HOST_WIDE_INT,
54 unsigned HOST_WIDE_INT, int);
55 static void do_cmp_and_jump (rtx, rtx, enum rtx_code, enum machine_mode, rtx);
56 static rtx expand_smod_pow2 (enum machine_mode, rtx, HOST_WIDE_INT);
57 static rtx expand_sdiv_pow2 (enum machine_mode, rtx, HOST_WIDE_INT);
59 /* Test whether a value is zero of a power of two. */
60 #define EXACT_POWER_OF_2_OR_ZERO_P(x) (((x) & ((x) - 1)) == 0)
62 /* Nonzero means divides or modulus operations are relatively cheap for
63 powers of two, so don't use branches; emit the operation instead.
64 Usually, this will mean that the MD file will emit non-branch
65 sequences. */
67 static bool sdiv_pow2_cheap[NUM_MACHINE_MODES];
68 static bool smod_pow2_cheap[NUM_MACHINE_MODES];
70 #ifndef SLOW_UNALIGNED_ACCESS
71 #define SLOW_UNALIGNED_ACCESS(MODE, ALIGN) STRICT_ALIGNMENT
72 #endif
74 /* For compilers that support multiple targets with different word sizes,
75 MAX_BITS_PER_WORD contains the biggest value of BITS_PER_WORD. An example
76 is the H8/300(H) compiler. */
78 #ifndef MAX_BITS_PER_WORD
79 #define MAX_BITS_PER_WORD BITS_PER_WORD
80 #endif
82 /* Reduce conditional compilation elsewhere. */
83 #ifndef HAVE_insv
84 #define HAVE_insv 0
85 #define CODE_FOR_insv CODE_FOR_nothing
86 #define gen_insv(a,b,c,d) NULL_RTX
87 #endif
88 #ifndef HAVE_extv
89 #define HAVE_extv 0
90 #define CODE_FOR_extv CODE_FOR_nothing
91 #define gen_extv(a,b,c,d) NULL_RTX
92 #endif
93 #ifndef HAVE_extzv
94 #define HAVE_extzv 0
95 #define CODE_FOR_extzv CODE_FOR_nothing
96 #define gen_extzv(a,b,c,d) NULL_RTX
97 #endif
99 /* Cost of various pieces of RTL. Note that some of these are indexed by
100 shift count and some by mode. */
101 static int zero_cost;
102 static int add_cost[NUM_MACHINE_MODES];
103 static int neg_cost[NUM_MACHINE_MODES];
104 static int shift_cost[NUM_MACHINE_MODES][MAX_BITS_PER_WORD];
105 static int shiftadd_cost[NUM_MACHINE_MODES][MAX_BITS_PER_WORD];
106 static int shiftsub_cost[NUM_MACHINE_MODES][MAX_BITS_PER_WORD];
107 static int mul_cost[NUM_MACHINE_MODES];
108 static int sdiv_cost[NUM_MACHINE_MODES];
109 static int udiv_cost[NUM_MACHINE_MODES];
110 static int mul_widen_cost[NUM_MACHINE_MODES];
111 static int mul_highpart_cost[NUM_MACHINE_MODES];
113 void
114 init_expmed (void)
116 struct
118 struct rtx_def reg; rtunion reg_fld[2];
119 struct rtx_def plus; rtunion plus_fld1;
120 struct rtx_def neg;
121 struct rtx_def mult; rtunion mult_fld1;
122 struct rtx_def sdiv; rtunion sdiv_fld1;
123 struct rtx_def udiv; rtunion udiv_fld1;
124 struct rtx_def zext;
125 struct rtx_def sdiv_32; rtunion sdiv_32_fld1;
126 struct rtx_def smod_32; rtunion smod_32_fld1;
127 struct rtx_def wide_mult; rtunion wide_mult_fld1;
128 struct rtx_def wide_lshr; rtunion wide_lshr_fld1;
129 struct rtx_def wide_trunc;
130 struct rtx_def shift; rtunion shift_fld1;
131 struct rtx_def shift_mult; rtunion shift_mult_fld1;
132 struct rtx_def shift_add; rtunion shift_add_fld1;
133 struct rtx_def shift_sub; rtunion shift_sub_fld1;
134 } all;
136 rtx pow2[MAX_BITS_PER_WORD];
137 rtx cint[MAX_BITS_PER_WORD];
138 int m, n;
139 enum machine_mode mode, wider_mode;
141 zero_cost = rtx_cost (const0_rtx, 0);
143 for (m = 1; m < MAX_BITS_PER_WORD; m++)
145 pow2[m] = GEN_INT ((HOST_WIDE_INT) 1 << m);
146 cint[m] = GEN_INT (m);
149 memset (&all, 0, sizeof all);
151 PUT_CODE (&all.reg, REG);
152 /* Avoid using hard regs in ways which may be unsupported. */
153 SET_REGNO (&all.reg, LAST_VIRTUAL_REGISTER + 1);
155 PUT_CODE (&all.plus, PLUS);
156 XEXP (&all.plus, 0) = &all.reg;
157 XEXP (&all.plus, 1) = &all.reg;
159 PUT_CODE (&all.neg, NEG);
160 XEXP (&all.neg, 0) = &all.reg;
162 PUT_CODE (&all.mult, MULT);
163 XEXP (&all.mult, 0) = &all.reg;
164 XEXP (&all.mult, 1) = &all.reg;
166 PUT_CODE (&all.sdiv, DIV);
167 XEXP (&all.sdiv, 0) = &all.reg;
168 XEXP (&all.sdiv, 1) = &all.reg;
170 PUT_CODE (&all.udiv, UDIV);
171 XEXP (&all.udiv, 0) = &all.reg;
172 XEXP (&all.udiv, 1) = &all.reg;
174 PUT_CODE (&all.sdiv_32, DIV);
175 XEXP (&all.sdiv_32, 0) = &all.reg;
176 XEXP (&all.sdiv_32, 1) = 32 < MAX_BITS_PER_WORD ? cint[32] : GEN_INT (32);
178 PUT_CODE (&all.smod_32, MOD);
179 XEXP (&all.smod_32, 0) = &all.reg;
180 XEXP (&all.smod_32, 1) = XEXP (&all.sdiv_32, 1);
182 PUT_CODE (&all.zext, ZERO_EXTEND);
183 XEXP (&all.zext, 0) = &all.reg;
185 PUT_CODE (&all.wide_mult, MULT);
186 XEXP (&all.wide_mult, 0) = &all.zext;
187 XEXP (&all.wide_mult, 1) = &all.zext;
189 PUT_CODE (&all.wide_lshr, LSHIFTRT);
190 XEXP (&all.wide_lshr, 0) = &all.wide_mult;
192 PUT_CODE (&all.wide_trunc, TRUNCATE);
193 XEXP (&all.wide_trunc, 0) = &all.wide_lshr;
195 PUT_CODE (&all.shift, ASHIFT);
196 XEXP (&all.shift, 0) = &all.reg;
198 PUT_CODE (&all.shift_mult, MULT);
199 XEXP (&all.shift_mult, 0) = &all.reg;
201 PUT_CODE (&all.shift_add, PLUS);
202 XEXP (&all.shift_add, 0) = &all.shift_mult;
203 XEXP (&all.shift_add, 1) = &all.reg;
205 PUT_CODE (&all.shift_sub, MINUS);
206 XEXP (&all.shift_sub, 0) = &all.shift_mult;
207 XEXP (&all.shift_sub, 1) = &all.reg;
209 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
210 mode != VOIDmode;
211 mode = GET_MODE_WIDER_MODE (mode))
213 PUT_MODE (&all.reg, mode);
214 PUT_MODE (&all.plus, mode);
215 PUT_MODE (&all.neg, mode);
216 PUT_MODE (&all.mult, mode);
217 PUT_MODE (&all.sdiv, mode);
218 PUT_MODE (&all.udiv, mode);
219 PUT_MODE (&all.sdiv_32, mode);
220 PUT_MODE (&all.smod_32, mode);
221 PUT_MODE (&all.wide_trunc, mode);
222 PUT_MODE (&all.shift, mode);
223 PUT_MODE (&all.shift_mult, mode);
224 PUT_MODE (&all.shift_add, mode);
225 PUT_MODE (&all.shift_sub, mode);
227 add_cost[mode] = rtx_cost (&all.plus, SET);
228 neg_cost[mode] = rtx_cost (&all.neg, SET);
229 mul_cost[mode] = rtx_cost (&all.mult, SET);
230 sdiv_cost[mode] = rtx_cost (&all.sdiv, SET);
231 udiv_cost[mode] = rtx_cost (&all.udiv, SET);
233 sdiv_pow2_cheap[mode] = (rtx_cost (&all.sdiv_32, SET)
234 <= 2 * add_cost[mode]);
235 smod_pow2_cheap[mode] = (rtx_cost (&all.smod_32, SET)
236 <= 4 * add_cost[mode]);
238 wider_mode = GET_MODE_WIDER_MODE (mode);
239 if (wider_mode != VOIDmode)
241 PUT_MODE (&all.zext, wider_mode);
242 PUT_MODE (&all.wide_mult, wider_mode);
243 PUT_MODE (&all.wide_lshr, wider_mode);
244 XEXP (&all.wide_lshr, 1) = GEN_INT (GET_MODE_BITSIZE (mode));
246 mul_widen_cost[wider_mode] = rtx_cost (&all.wide_mult, SET);
247 mul_highpart_cost[mode] = rtx_cost (&all.wide_trunc, SET);
250 shift_cost[mode][0] = 0;
251 shiftadd_cost[mode][0] = shiftsub_cost[mode][0] = add_cost[mode];
253 n = MIN (MAX_BITS_PER_WORD, GET_MODE_BITSIZE (mode));
254 for (m = 1; m < n; m++)
256 XEXP (&all.shift, 1) = cint[m];
257 XEXP (&all.shift_mult, 1) = pow2[m];
259 shift_cost[mode][m] = rtx_cost (&all.shift, SET);
260 shiftadd_cost[mode][m] = rtx_cost (&all.shift_add, SET);
261 shiftsub_cost[mode][m] = rtx_cost (&all.shift_sub, SET);
266 /* Return an rtx representing minus the value of X.
267 MODE is the intended mode of the result,
268 useful if X is a CONST_INT. */
271 negate_rtx (enum machine_mode mode, rtx x)
273 rtx result = simplify_unary_operation (NEG, mode, x, mode);
275 if (result == 0)
276 result = expand_unop (mode, neg_optab, x, NULL_RTX, 0);
278 return result;
281 /* Report on the availability of insv/extv/extzv and the desired mode
282 of each of their operands. Returns MAX_MACHINE_MODE if HAVE_foo
283 is false; else the mode of the specified operand. If OPNO is -1,
284 all the caller cares about is whether the insn is available. */
285 enum machine_mode
286 mode_for_extraction (enum extraction_pattern pattern, int opno)
288 const struct insn_data *data;
290 switch (pattern)
292 case EP_insv:
293 if (HAVE_insv)
295 data = &insn_data[CODE_FOR_insv];
296 break;
298 return MAX_MACHINE_MODE;
300 case EP_extv:
301 if (HAVE_extv)
303 data = &insn_data[CODE_FOR_extv];
304 break;
306 return MAX_MACHINE_MODE;
308 case EP_extzv:
309 if (HAVE_extzv)
311 data = &insn_data[CODE_FOR_extzv];
312 break;
314 return MAX_MACHINE_MODE;
316 default:
317 gcc_unreachable ();
320 if (opno == -1)
321 return VOIDmode;
323 /* Everyone who uses this function used to follow it with
324 if (result == VOIDmode) result = word_mode; */
325 if (data->operand[opno].mode == VOIDmode)
326 return word_mode;
327 return data->operand[opno].mode;
330 /* Return true if X, of mode MODE, matches the predicate for operand
331 OPNO of instruction ICODE. Allow volatile memories, regardless of
332 the ambient volatile_ok setting. */
334 static bool
335 check_predicate_volatile_ok (enum insn_code icode, int opno,
336 rtx x, enum machine_mode mode)
338 bool save_volatile_ok, result;
340 save_volatile_ok = volatile_ok;
341 result = insn_data[(int) icode].operand[opno].predicate (x, mode);
342 volatile_ok = save_volatile_ok;
343 return result;
346 /* A subroutine of store_bit_field, with the same arguments. Return true
347 if the operation could be implemented.
349 If FALLBACK_P is true, fall back to store_fixed_bit_field if we have
350 no other way of implementing the operation. If FALLBACK_P is false,
351 return false instead. */
353 static bool
354 store_bit_field_1 (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
355 unsigned HOST_WIDE_INT bitnum, enum machine_mode fieldmode,
356 rtx value, bool fallback_p)
358 unsigned int unit
359 = (MEM_P (str_rtx)) ? BITS_PER_UNIT : BITS_PER_WORD;
360 unsigned HOST_WIDE_INT offset, bitpos;
361 rtx op0 = str_rtx;
362 int byte_offset;
363 rtx orig_value;
365 enum machine_mode op_mode = mode_for_extraction (EP_insv, 3);
367 while (GET_CODE (op0) == SUBREG)
369 /* The following line once was done only if WORDS_BIG_ENDIAN,
370 but I think that is a mistake. WORDS_BIG_ENDIAN is
371 meaningful at a much higher level; when structures are copied
372 between memory and regs, the higher-numbered regs
373 always get higher addresses. */
374 int inner_mode_size = GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0)));
375 int outer_mode_size = GET_MODE_SIZE (GET_MODE (op0));
377 byte_offset = 0;
379 /* Paradoxical subregs need special handling on big endian machines. */
380 if (SUBREG_BYTE (op0) == 0 && inner_mode_size < outer_mode_size)
382 int difference = inner_mode_size - outer_mode_size;
384 if (WORDS_BIG_ENDIAN)
385 byte_offset += (difference / UNITS_PER_WORD) * UNITS_PER_WORD;
386 if (BYTES_BIG_ENDIAN)
387 byte_offset += difference % UNITS_PER_WORD;
389 else
390 byte_offset = SUBREG_BYTE (op0);
392 bitnum += byte_offset * BITS_PER_UNIT;
393 op0 = SUBREG_REG (op0);
396 /* No action is needed if the target is a register and if the field
397 lies completely outside that register. This can occur if the source
398 code contains an out-of-bounds access to a small array. */
399 if (REG_P (op0) && bitnum >= GET_MODE_BITSIZE (GET_MODE (op0)))
400 return true;
402 /* Use vec_set patterns for inserting parts of vectors whenever
403 available. */
404 if (VECTOR_MODE_P (GET_MODE (op0))
405 && !MEM_P (op0)
406 && (optab_handler (vec_set_optab, GET_MODE (op0))->insn_code
407 != CODE_FOR_nothing)
408 && fieldmode == GET_MODE_INNER (GET_MODE (op0))
409 && bitsize == GET_MODE_BITSIZE (GET_MODE_INNER (GET_MODE (op0)))
410 && !(bitnum % GET_MODE_BITSIZE (GET_MODE_INNER (GET_MODE (op0)))))
412 enum machine_mode outermode = GET_MODE (op0);
413 enum machine_mode innermode = GET_MODE_INNER (outermode);
414 int icode = (int) optab_handler (vec_set_optab, outermode)->insn_code;
415 int pos = bitnum / GET_MODE_BITSIZE (innermode);
416 rtx rtxpos = GEN_INT (pos);
417 rtx src = value;
418 rtx dest = op0;
419 rtx pat, seq;
420 enum machine_mode mode0 = insn_data[icode].operand[0].mode;
421 enum machine_mode mode1 = insn_data[icode].operand[1].mode;
422 enum machine_mode mode2 = insn_data[icode].operand[2].mode;
424 start_sequence ();
426 if (! (*insn_data[icode].operand[1].predicate) (src, mode1))
427 src = copy_to_mode_reg (mode1, src);
429 if (! (*insn_data[icode].operand[2].predicate) (rtxpos, mode2))
430 rtxpos = copy_to_mode_reg (mode1, rtxpos);
432 /* We could handle this, but we should always be called with a pseudo
433 for our targets and all insns should take them as outputs. */
434 gcc_assert ((*insn_data[icode].operand[0].predicate) (dest, mode0)
435 && (*insn_data[icode].operand[1].predicate) (src, mode1)
436 && (*insn_data[icode].operand[2].predicate) (rtxpos, mode2));
437 pat = GEN_FCN (icode) (dest, src, rtxpos);
438 seq = get_insns ();
439 end_sequence ();
440 if (pat)
442 emit_insn (seq);
443 emit_insn (pat);
444 return true;
448 /* If the target is a register, overwriting the entire object, or storing
449 a full-word or multi-word field can be done with just a SUBREG.
451 If the target is memory, storing any naturally aligned field can be
452 done with a simple store. For targets that support fast unaligned
453 memory, any naturally sized, unit aligned field can be done directly. */
455 offset = bitnum / unit;
456 bitpos = bitnum % unit;
457 byte_offset = (bitnum % BITS_PER_WORD) / BITS_PER_UNIT
458 + (offset * UNITS_PER_WORD);
460 if (bitpos == 0
461 && bitsize == GET_MODE_BITSIZE (fieldmode)
462 && (!MEM_P (op0)
463 ? ((GET_MODE_SIZE (fieldmode) >= UNITS_PER_WORD
464 || GET_MODE_SIZE (GET_MODE (op0)) == GET_MODE_SIZE (fieldmode))
465 && byte_offset % GET_MODE_SIZE (fieldmode) == 0)
466 : (! SLOW_UNALIGNED_ACCESS (fieldmode, MEM_ALIGN (op0))
467 || (offset * BITS_PER_UNIT % bitsize == 0
468 && MEM_ALIGN (op0) % GET_MODE_BITSIZE (fieldmode) == 0))))
470 if (MEM_P (op0))
471 op0 = adjust_address (op0, fieldmode, offset);
472 else if (GET_MODE (op0) != fieldmode)
473 op0 = simplify_gen_subreg (fieldmode, op0, GET_MODE (op0),
474 byte_offset);
475 emit_move_insn (op0, value);
476 return true;
479 /* Make sure we are playing with integral modes. Pun with subregs
480 if we aren't. This must come after the entire register case above,
481 since that case is valid for any mode. The following cases are only
482 valid for integral modes. */
484 enum machine_mode imode = int_mode_for_mode (GET_MODE (op0));
485 if (imode != GET_MODE (op0))
487 if (MEM_P (op0))
488 op0 = adjust_address (op0, imode, 0);
489 else
491 gcc_assert (imode != BLKmode);
492 op0 = gen_lowpart (imode, op0);
497 /* We may be accessing data outside the field, which means
498 we can alias adjacent data. */
499 if (MEM_P (op0))
501 op0 = shallow_copy_rtx (op0);
502 set_mem_alias_set (op0, 0);
503 set_mem_expr (op0, 0);
506 /* If OP0 is a register, BITPOS must count within a word.
507 But as we have it, it counts within whatever size OP0 now has.
508 On a bigendian machine, these are not the same, so convert. */
509 if (BYTES_BIG_ENDIAN
510 && !MEM_P (op0)
511 && unit > GET_MODE_BITSIZE (GET_MODE (op0)))
512 bitpos += unit - GET_MODE_BITSIZE (GET_MODE (op0));
514 /* Storing an lsb-aligned field in a register
515 can be done with a movestrict instruction. */
517 if (!MEM_P (op0)
518 && (BYTES_BIG_ENDIAN ? bitpos + bitsize == unit : bitpos == 0)
519 && bitsize == GET_MODE_BITSIZE (fieldmode)
520 && (optab_handler (movstrict_optab, fieldmode)->insn_code
521 != CODE_FOR_nothing))
523 int icode = optab_handler (movstrict_optab, fieldmode)->insn_code;
525 /* Get appropriate low part of the value being stored. */
526 if (GET_CODE (value) == CONST_INT || REG_P (value))
527 value = gen_lowpart (fieldmode, value);
528 else if (!(GET_CODE (value) == SYMBOL_REF
529 || GET_CODE (value) == LABEL_REF
530 || GET_CODE (value) == CONST))
531 value = convert_to_mode (fieldmode, value, 0);
533 if (! (*insn_data[icode].operand[1].predicate) (value, fieldmode))
534 value = copy_to_mode_reg (fieldmode, value);
536 if (GET_CODE (op0) == SUBREG)
538 /* Else we've got some float mode source being extracted into
539 a different float mode destination -- this combination of
540 subregs results in Severe Tire Damage. */
541 gcc_assert (GET_MODE (SUBREG_REG (op0)) == fieldmode
542 || GET_MODE_CLASS (fieldmode) == MODE_INT
543 || GET_MODE_CLASS (fieldmode) == MODE_PARTIAL_INT);
544 op0 = SUBREG_REG (op0);
547 emit_insn (GEN_FCN (icode)
548 (gen_rtx_SUBREG (fieldmode, op0,
549 (bitnum % BITS_PER_WORD) / BITS_PER_UNIT
550 + (offset * UNITS_PER_WORD)),
551 value));
553 return true;
556 /* Handle fields bigger than a word. */
558 if (bitsize > BITS_PER_WORD)
560 /* Here we transfer the words of the field
561 in the order least significant first.
562 This is because the most significant word is the one which may
563 be less than full.
564 However, only do that if the value is not BLKmode. */
566 unsigned int backwards = WORDS_BIG_ENDIAN && fieldmode != BLKmode;
567 unsigned int nwords = (bitsize + (BITS_PER_WORD - 1)) / BITS_PER_WORD;
568 unsigned int i;
569 rtx last;
571 /* This is the mode we must force value to, so that there will be enough
572 subwords to extract. Note that fieldmode will often (always?) be
573 VOIDmode, because that is what store_field uses to indicate that this
574 is a bit field, but passing VOIDmode to operand_subword_force
575 is not allowed. */
576 fieldmode = GET_MODE (value);
577 if (fieldmode == VOIDmode)
578 fieldmode = smallest_mode_for_size (nwords * BITS_PER_WORD, MODE_INT);
580 last = get_last_insn ();
581 for (i = 0; i < nwords; i++)
583 /* If I is 0, use the low-order word in both field and target;
584 if I is 1, use the next to lowest word; and so on. */
585 unsigned int wordnum = (backwards ? nwords - i - 1 : i);
586 unsigned int bit_offset = (backwards
587 ? MAX ((int) bitsize - ((int) i + 1)
588 * BITS_PER_WORD,
590 : (int) i * BITS_PER_WORD);
591 rtx value_word = operand_subword_force (value, wordnum, fieldmode);
593 if (!store_bit_field_1 (op0, MIN (BITS_PER_WORD,
594 bitsize - i * BITS_PER_WORD),
595 bitnum + bit_offset, word_mode,
596 value_word, fallback_p))
598 delete_insns_since (last);
599 return false;
602 return true;
605 /* From here on we can assume that the field to be stored in is
606 a full-word (whatever type that is), since it is shorter than a word. */
608 /* OFFSET is the number of words or bytes (UNIT says which)
609 from STR_RTX to the first word or byte containing part of the field. */
611 if (!MEM_P (op0))
613 if (offset != 0
614 || GET_MODE_SIZE (GET_MODE (op0)) > UNITS_PER_WORD)
616 if (!REG_P (op0))
618 /* Since this is a destination (lvalue), we can't copy
619 it to a pseudo. We can remove a SUBREG that does not
620 change the size of the operand. Such a SUBREG may
621 have been added above. */
622 gcc_assert (GET_CODE (op0) == SUBREG
623 && (GET_MODE_SIZE (GET_MODE (op0))
624 == GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0)))));
625 op0 = SUBREG_REG (op0);
627 op0 = gen_rtx_SUBREG (mode_for_size (BITS_PER_WORD, MODE_INT, 0),
628 op0, (offset * UNITS_PER_WORD));
630 offset = 0;
633 /* If VALUE has a floating-point or complex mode, access it as an
634 integer of the corresponding size. This can occur on a machine
635 with 64 bit registers that uses SFmode for float. It can also
636 occur for unaligned float or complex fields. */
637 orig_value = value;
638 if (GET_MODE (value) != VOIDmode
639 && GET_MODE_CLASS (GET_MODE (value)) != MODE_INT
640 && GET_MODE_CLASS (GET_MODE (value)) != MODE_PARTIAL_INT)
642 value = gen_reg_rtx (int_mode_for_mode (GET_MODE (value)));
643 emit_move_insn (gen_lowpart (GET_MODE (orig_value), value), orig_value);
646 /* Now OFFSET is nonzero only if OP0 is memory
647 and is therefore always measured in bytes. */
649 if (HAVE_insv
650 && GET_MODE (value) != BLKmode
651 && bitsize > 0
652 && GET_MODE_BITSIZE (op_mode) >= bitsize
653 && ! ((REG_P (op0) || GET_CODE (op0) == SUBREG)
654 && (bitsize + bitpos > GET_MODE_BITSIZE (op_mode)))
655 && insn_data[CODE_FOR_insv].operand[1].predicate (GEN_INT (bitsize),
656 VOIDmode)
657 && check_predicate_volatile_ok (CODE_FOR_insv, 0, op0, VOIDmode))
659 int xbitpos = bitpos;
660 rtx value1;
661 rtx xop0 = op0;
662 rtx last = get_last_insn ();
663 rtx pat;
665 /* Add OFFSET into OP0's address. */
666 if (MEM_P (xop0))
667 xop0 = adjust_address (xop0, byte_mode, offset);
669 /* If xop0 is a register, we need it in OP_MODE
670 to make it acceptable to the format of insv. */
671 if (GET_CODE (xop0) == SUBREG)
672 /* We can't just change the mode, because this might clobber op0,
673 and we will need the original value of op0 if insv fails. */
674 xop0 = gen_rtx_SUBREG (op_mode, SUBREG_REG (xop0), SUBREG_BYTE (xop0));
675 if (REG_P (xop0) && GET_MODE (xop0) != op_mode)
676 xop0 = gen_rtx_SUBREG (op_mode, xop0, 0);
678 /* On big-endian machines, we count bits from the most significant.
679 If the bit field insn does not, we must invert. */
681 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
682 xbitpos = unit - bitsize - xbitpos;
684 /* We have been counting XBITPOS within UNIT.
685 Count instead within the size of the register. */
686 if (BITS_BIG_ENDIAN && !MEM_P (xop0))
687 xbitpos += GET_MODE_BITSIZE (op_mode) - unit;
689 unit = GET_MODE_BITSIZE (op_mode);
691 /* Convert VALUE to op_mode (which insv insn wants) in VALUE1. */
692 value1 = value;
693 if (GET_MODE (value) != op_mode)
695 if (GET_MODE_BITSIZE (GET_MODE (value)) >= bitsize)
697 /* Optimization: Don't bother really extending VALUE
698 if it has all the bits we will actually use. However,
699 if we must narrow it, be sure we do it correctly. */
701 if (GET_MODE_SIZE (GET_MODE (value)) < GET_MODE_SIZE (op_mode))
703 rtx tmp;
705 tmp = simplify_subreg (op_mode, value1, GET_MODE (value), 0);
706 if (! tmp)
707 tmp = simplify_gen_subreg (op_mode,
708 force_reg (GET_MODE (value),
709 value1),
710 GET_MODE (value), 0);
711 value1 = tmp;
713 else
714 value1 = gen_lowpart (op_mode, value1);
716 else if (GET_CODE (value) == CONST_INT)
717 value1 = gen_int_mode (INTVAL (value), op_mode);
718 else
719 /* Parse phase is supposed to make VALUE's data type
720 match that of the component reference, which is a type
721 at least as wide as the field; so VALUE should have
722 a mode that corresponds to that type. */
723 gcc_assert (CONSTANT_P (value));
726 /* If this machine's insv insists on a register,
727 get VALUE1 into a register. */
728 if (! ((*insn_data[(int) CODE_FOR_insv].operand[3].predicate)
729 (value1, op_mode)))
730 value1 = force_reg (op_mode, value1);
732 pat = gen_insv (xop0, GEN_INT (bitsize), GEN_INT (xbitpos), value1);
733 if (pat)
735 emit_insn (pat);
736 return true;
738 delete_insns_since (last);
741 /* If OP0 is a memory, try copying it to a register and seeing if a
742 cheap register alternative is available. */
743 if (HAVE_insv && MEM_P (op0))
745 enum machine_mode bestmode;
747 /* Get the mode to use for inserting into this field. If OP0 is
748 BLKmode, get the smallest mode consistent with the alignment. If
749 OP0 is a non-BLKmode object that is no wider than OP_MODE, use its
750 mode. Otherwise, use the smallest mode containing the field. */
752 if (GET_MODE (op0) == BLKmode
753 || (op_mode != MAX_MACHINE_MODE
754 && GET_MODE_SIZE (GET_MODE (op0)) > GET_MODE_SIZE (op_mode)))
755 bestmode = get_best_mode (bitsize, bitnum, MEM_ALIGN (op0),
756 (op_mode == MAX_MACHINE_MODE
757 ? VOIDmode : op_mode),
758 MEM_VOLATILE_P (op0));
759 else
760 bestmode = GET_MODE (op0);
762 if (bestmode != VOIDmode
763 && GET_MODE_SIZE (bestmode) >= GET_MODE_SIZE (fieldmode)
764 && !(SLOW_UNALIGNED_ACCESS (bestmode, MEM_ALIGN (op0))
765 && GET_MODE_BITSIZE (bestmode) > MEM_ALIGN (op0)))
767 rtx last, tempreg, xop0;
768 unsigned HOST_WIDE_INT xoffset, xbitpos;
770 last = get_last_insn ();
772 /* Adjust address to point to the containing unit of
773 that mode. Compute the offset as a multiple of this unit,
774 counting in bytes. */
775 unit = GET_MODE_BITSIZE (bestmode);
776 xoffset = (bitnum / unit) * GET_MODE_SIZE (bestmode);
777 xbitpos = bitnum % unit;
778 xop0 = adjust_address (op0, bestmode, xoffset);
780 /* Fetch that unit, store the bitfield in it, then store
781 the unit. */
782 tempreg = copy_to_reg (xop0);
783 if (store_bit_field_1 (tempreg, bitsize, xbitpos,
784 fieldmode, orig_value, false))
786 emit_move_insn (xop0, tempreg);
787 return true;
789 delete_insns_since (last);
793 if (!fallback_p)
794 return false;
796 store_fixed_bit_field (op0, offset, bitsize, bitpos, value);
797 return true;
800 /* Generate code to store value from rtx VALUE
801 into a bit-field within structure STR_RTX
802 containing BITSIZE bits starting at bit BITNUM.
803 FIELDMODE is the machine-mode of the FIELD_DECL node for this field. */
805 void
806 store_bit_field (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
807 unsigned HOST_WIDE_INT bitnum, enum machine_mode fieldmode,
808 rtx value)
810 if (!store_bit_field_1 (str_rtx, bitsize, bitnum, fieldmode, value, true))
811 gcc_unreachable ();
814 /* Use shifts and boolean operations to store VALUE
815 into a bit field of width BITSIZE
816 in a memory location specified by OP0 except offset by OFFSET bytes.
817 (OFFSET must be 0 if OP0 is a register.)
818 The field starts at position BITPOS within the byte.
819 (If OP0 is a register, it may be a full word or a narrower mode,
820 but BITPOS still counts within a full word,
821 which is significant on bigendian machines.) */
823 static void
824 store_fixed_bit_field (rtx op0, unsigned HOST_WIDE_INT offset,
825 unsigned HOST_WIDE_INT bitsize,
826 unsigned HOST_WIDE_INT bitpos, rtx value)
828 enum machine_mode mode;
829 unsigned int total_bits = BITS_PER_WORD;
830 rtx temp;
831 int all_zero = 0;
832 int all_one = 0;
834 /* There is a case not handled here:
835 a structure with a known alignment of just a halfword
836 and a field split across two aligned halfwords within the structure.
837 Or likewise a structure with a known alignment of just a byte
838 and a field split across two bytes.
839 Such cases are not supposed to be able to occur. */
841 if (REG_P (op0) || GET_CODE (op0) == SUBREG)
843 gcc_assert (!offset);
844 /* Special treatment for a bit field split across two registers. */
845 if (bitsize + bitpos > BITS_PER_WORD)
847 store_split_bit_field (op0, bitsize, bitpos, value);
848 return;
851 else
853 /* Get the proper mode to use for this field. We want a mode that
854 includes the entire field. If such a mode would be larger than
855 a word, we won't be doing the extraction the normal way.
856 We don't want a mode bigger than the destination. */
858 mode = GET_MODE (op0);
859 if (GET_MODE_BITSIZE (mode) == 0
860 || GET_MODE_BITSIZE (mode) > GET_MODE_BITSIZE (word_mode))
861 mode = word_mode;
862 mode = get_best_mode (bitsize, bitpos + offset * BITS_PER_UNIT,
863 MEM_ALIGN (op0), mode, MEM_VOLATILE_P (op0));
865 if (mode == VOIDmode)
867 /* The only way this should occur is if the field spans word
868 boundaries. */
869 store_split_bit_field (op0, bitsize, bitpos + offset * BITS_PER_UNIT,
870 value);
871 return;
874 total_bits = GET_MODE_BITSIZE (mode);
876 /* Make sure bitpos is valid for the chosen mode. Adjust BITPOS to
877 be in the range 0 to total_bits-1, and put any excess bytes in
878 OFFSET. */
879 if (bitpos >= total_bits)
881 offset += (bitpos / total_bits) * (total_bits / BITS_PER_UNIT);
882 bitpos -= ((bitpos / total_bits) * (total_bits / BITS_PER_UNIT)
883 * BITS_PER_UNIT);
886 /* Get ref to an aligned byte, halfword, or word containing the field.
887 Adjust BITPOS to be position within a word,
888 and OFFSET to be the offset of that word.
889 Then alter OP0 to refer to that word. */
890 bitpos += (offset % (total_bits / BITS_PER_UNIT)) * BITS_PER_UNIT;
891 offset -= (offset % (total_bits / BITS_PER_UNIT));
892 op0 = adjust_address (op0, mode, offset);
895 mode = GET_MODE (op0);
897 /* Now MODE is either some integral mode for a MEM as OP0,
898 or is a full-word for a REG as OP0. TOTAL_BITS corresponds.
899 The bit field is contained entirely within OP0.
900 BITPOS is the starting bit number within OP0.
901 (OP0's mode may actually be narrower than MODE.) */
903 if (BYTES_BIG_ENDIAN)
904 /* BITPOS is the distance between our msb
905 and that of the containing datum.
906 Convert it to the distance from the lsb. */
907 bitpos = total_bits - bitsize - bitpos;
909 /* Now BITPOS is always the distance between our lsb
910 and that of OP0. */
912 /* Shift VALUE left by BITPOS bits. If VALUE is not constant,
913 we must first convert its mode to MODE. */
915 if (GET_CODE (value) == CONST_INT)
917 HOST_WIDE_INT v = INTVAL (value);
919 if (bitsize < HOST_BITS_PER_WIDE_INT)
920 v &= ((HOST_WIDE_INT) 1 << bitsize) - 1;
922 if (v == 0)
923 all_zero = 1;
924 else if ((bitsize < HOST_BITS_PER_WIDE_INT
925 && v == ((HOST_WIDE_INT) 1 << bitsize) - 1)
926 || (bitsize == HOST_BITS_PER_WIDE_INT && v == -1))
927 all_one = 1;
929 value = lshift_value (mode, value, bitpos, bitsize);
931 else
933 int must_and = (GET_MODE_BITSIZE (GET_MODE (value)) != bitsize
934 && bitpos + bitsize != GET_MODE_BITSIZE (mode));
936 if (GET_MODE (value) != mode)
938 if ((REG_P (value) || GET_CODE (value) == SUBREG)
939 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (value)))
940 value = gen_lowpart (mode, value);
941 else
942 value = convert_to_mode (mode, value, 1);
945 if (must_and)
946 value = expand_binop (mode, and_optab, value,
947 mask_rtx (mode, 0, bitsize, 0),
948 NULL_RTX, 1, OPTAB_LIB_WIDEN);
949 if (bitpos > 0)
950 value = expand_shift (LSHIFT_EXPR, mode, value,
951 build_int_cst (NULL_TREE, bitpos), NULL_RTX, 1);
954 /* Now clear the chosen bits in OP0,
955 except that if VALUE is -1 we need not bother. */
956 /* We keep the intermediates in registers to allow CSE to combine
957 consecutive bitfield assignments. */
959 temp = force_reg (mode, op0);
961 if (! all_one)
963 temp = expand_binop (mode, and_optab, temp,
964 mask_rtx (mode, bitpos, bitsize, 1),
965 NULL_RTX, 1, OPTAB_LIB_WIDEN);
966 temp = force_reg (mode, temp);
969 /* Now logical-or VALUE into OP0, unless it is zero. */
971 if (! all_zero)
973 temp = expand_binop (mode, ior_optab, temp, value,
974 NULL_RTX, 1, OPTAB_LIB_WIDEN);
975 temp = force_reg (mode, temp);
978 if (op0 != temp)
979 emit_move_insn (op0, temp);
982 /* Store a bit field that is split across multiple accessible memory objects.
984 OP0 is the REG, SUBREG or MEM rtx for the first of the objects.
985 BITSIZE is the field width; BITPOS the position of its first bit
986 (within the word).
987 VALUE is the value to store.
989 This does not yet handle fields wider than BITS_PER_WORD. */
991 static void
992 store_split_bit_field (rtx op0, unsigned HOST_WIDE_INT bitsize,
993 unsigned HOST_WIDE_INT bitpos, rtx value)
995 unsigned int unit;
996 unsigned int bitsdone = 0;
998 /* Make sure UNIT isn't larger than BITS_PER_WORD, we can only handle that
999 much at a time. */
1000 if (REG_P (op0) || GET_CODE (op0) == SUBREG)
1001 unit = BITS_PER_WORD;
1002 else
1003 unit = MIN (MEM_ALIGN (op0), BITS_PER_WORD);
1005 /* If VALUE is a constant other than a CONST_INT, get it into a register in
1006 WORD_MODE. If we can do this using gen_lowpart_common, do so. Note
1007 that VALUE might be a floating-point constant. */
1008 if (CONSTANT_P (value) && GET_CODE (value) != CONST_INT)
1010 rtx word = gen_lowpart_common (word_mode, value);
1012 if (word && (value != word))
1013 value = word;
1014 else
1015 value = gen_lowpart_common (word_mode,
1016 force_reg (GET_MODE (value) != VOIDmode
1017 ? GET_MODE (value)
1018 : word_mode, value));
1021 while (bitsdone < bitsize)
1023 unsigned HOST_WIDE_INT thissize;
1024 rtx part, word;
1025 unsigned HOST_WIDE_INT thispos;
1026 unsigned HOST_WIDE_INT offset;
1028 offset = (bitpos + bitsdone) / unit;
1029 thispos = (bitpos + bitsdone) % unit;
1031 /* THISSIZE must not overrun a word boundary. Otherwise,
1032 store_fixed_bit_field will call us again, and we will mutually
1033 recurse forever. */
1034 thissize = MIN (bitsize - bitsdone, BITS_PER_WORD);
1035 thissize = MIN (thissize, unit - thispos);
1037 if (BYTES_BIG_ENDIAN)
1039 int total_bits;
1041 /* We must do an endian conversion exactly the same way as it is
1042 done in extract_bit_field, so that the two calls to
1043 extract_fixed_bit_field will have comparable arguments. */
1044 if (!MEM_P (value) || GET_MODE (value) == BLKmode)
1045 total_bits = BITS_PER_WORD;
1046 else
1047 total_bits = GET_MODE_BITSIZE (GET_MODE (value));
1049 /* Fetch successively less significant portions. */
1050 if (GET_CODE (value) == CONST_INT)
1051 part = GEN_INT (((unsigned HOST_WIDE_INT) (INTVAL (value))
1052 >> (bitsize - bitsdone - thissize))
1053 & (((HOST_WIDE_INT) 1 << thissize) - 1));
1054 else
1055 /* The args are chosen so that the last part includes the
1056 lsb. Give extract_bit_field the value it needs (with
1057 endianness compensation) to fetch the piece we want. */
1058 part = extract_fixed_bit_field (word_mode, value, 0, thissize,
1059 total_bits - bitsize + bitsdone,
1060 NULL_RTX, 1);
1062 else
1064 /* Fetch successively more significant portions. */
1065 if (GET_CODE (value) == CONST_INT)
1066 part = GEN_INT (((unsigned HOST_WIDE_INT) (INTVAL (value))
1067 >> bitsdone)
1068 & (((HOST_WIDE_INT) 1 << thissize) - 1));
1069 else
1070 part = extract_fixed_bit_field (word_mode, value, 0, thissize,
1071 bitsdone, NULL_RTX, 1);
1074 /* If OP0 is a register, then handle OFFSET here.
1076 When handling multiword bitfields, extract_bit_field may pass
1077 down a word_mode SUBREG of a larger REG for a bitfield that actually
1078 crosses a word boundary. Thus, for a SUBREG, we must find
1079 the current word starting from the base register. */
1080 if (GET_CODE (op0) == SUBREG)
1082 int word_offset = (SUBREG_BYTE (op0) / UNITS_PER_WORD) + offset;
1083 word = operand_subword_force (SUBREG_REG (op0), word_offset,
1084 GET_MODE (SUBREG_REG (op0)));
1085 offset = 0;
1087 else if (REG_P (op0))
1089 word = operand_subword_force (op0, offset, GET_MODE (op0));
1090 offset = 0;
1092 else
1093 word = op0;
1095 /* OFFSET is in UNITs, and UNIT is in bits.
1096 store_fixed_bit_field wants offset in bytes. */
1097 store_fixed_bit_field (word, offset * unit / BITS_PER_UNIT, thissize,
1098 thispos, part);
1099 bitsdone += thissize;
1103 /* A subroutine of extract_bit_field_1 that converts return value X
1104 to either MODE or TMODE. MODE, TMODE and UNSIGNEDP are arguments
1105 to extract_bit_field. */
1107 static rtx
1108 convert_extracted_bit_field (rtx x, enum machine_mode mode,
1109 enum machine_mode tmode, bool unsignedp)
1111 if (GET_MODE (x) == tmode || GET_MODE (x) == mode)
1112 return x;
1114 /* If the x mode is not a scalar integral, first convert to the
1115 integer mode of that size and then access it as a floating-point
1116 value via a SUBREG. */
1117 if (!SCALAR_INT_MODE_P (tmode))
1119 enum machine_mode smode;
1121 smode = mode_for_size (GET_MODE_BITSIZE (tmode), MODE_INT, 0);
1122 x = convert_to_mode (smode, x, unsignedp);
1123 x = force_reg (smode, x);
1124 return gen_lowpart (tmode, x);
1127 return convert_to_mode (tmode, x, unsignedp);
1130 /* A subroutine of extract_bit_field, with the same arguments.
1131 If FALLBACK_P is true, fall back to extract_fixed_bit_field
1132 if we can find no other means of implementing the operation.
1133 if FALLBACK_P is false, return NULL instead. */
1135 static rtx
1136 extract_bit_field_1 (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
1137 unsigned HOST_WIDE_INT bitnum, int unsignedp, rtx target,
1138 enum machine_mode mode, enum machine_mode tmode,
1139 bool fallback_p)
1141 unsigned int unit
1142 = (MEM_P (str_rtx)) ? BITS_PER_UNIT : BITS_PER_WORD;
1143 unsigned HOST_WIDE_INT offset, bitpos;
1144 rtx op0 = str_rtx;
1145 enum machine_mode int_mode;
1146 enum machine_mode ext_mode;
1147 enum machine_mode mode1;
1148 enum insn_code icode;
1149 int byte_offset;
1151 if (tmode == VOIDmode)
1152 tmode = mode;
1154 while (GET_CODE (op0) == SUBREG)
1156 bitnum += SUBREG_BYTE (op0) * BITS_PER_UNIT;
1157 op0 = SUBREG_REG (op0);
1160 /* If we have an out-of-bounds access to a register, just return an
1161 uninitialized register of the required mode. This can occur if the
1162 source code contains an out-of-bounds access to a small array. */
1163 if (REG_P (op0) && bitnum >= GET_MODE_BITSIZE (GET_MODE (op0)))
1164 return gen_reg_rtx (tmode);
1166 if (REG_P (op0)
1167 && mode == GET_MODE (op0)
1168 && bitnum == 0
1169 && bitsize == GET_MODE_BITSIZE (GET_MODE (op0)))
1171 /* We're trying to extract a full register from itself. */
1172 return op0;
1175 /* See if we can get a better vector mode before extracting. */
1176 if (VECTOR_MODE_P (GET_MODE (op0))
1177 && !MEM_P (op0)
1178 && GET_MODE_INNER (GET_MODE (op0)) != tmode)
1180 enum machine_mode new_mode;
1181 int nunits = GET_MODE_NUNITS (GET_MODE (op0));
1183 if (GET_MODE_CLASS (tmode) == MODE_FLOAT)
1184 new_mode = MIN_MODE_VECTOR_FLOAT;
1185 else if (GET_MODE_CLASS (tmode) == MODE_FRACT)
1186 new_mode = MIN_MODE_VECTOR_FRACT;
1187 else if (GET_MODE_CLASS (tmode) == MODE_UFRACT)
1188 new_mode = MIN_MODE_VECTOR_UFRACT;
1189 else if (GET_MODE_CLASS (tmode) == MODE_ACCUM)
1190 new_mode = MIN_MODE_VECTOR_ACCUM;
1191 else if (GET_MODE_CLASS (tmode) == MODE_UACCUM)
1192 new_mode = MIN_MODE_VECTOR_UACCUM;
1193 else
1194 new_mode = MIN_MODE_VECTOR_INT;
1196 for (; new_mode != VOIDmode ; new_mode = GET_MODE_WIDER_MODE (new_mode))
1197 if (GET_MODE_NUNITS (new_mode) == nunits
1198 && GET_MODE_INNER (new_mode) == tmode
1199 && targetm.vector_mode_supported_p (new_mode))
1200 break;
1201 if (new_mode != VOIDmode)
1202 op0 = gen_lowpart (new_mode, op0);
1205 /* Use vec_extract patterns for extracting parts of vectors whenever
1206 available. */
1207 if (VECTOR_MODE_P (GET_MODE (op0))
1208 && !MEM_P (op0)
1209 && (optab_handler (vec_extract_optab, GET_MODE (op0))->insn_code
1210 != CODE_FOR_nothing)
1211 && ((bitnum + bitsize - 1) / GET_MODE_BITSIZE (GET_MODE_INNER (GET_MODE (op0)))
1212 == bitnum / GET_MODE_BITSIZE (GET_MODE_INNER (GET_MODE (op0)))))
1214 enum machine_mode outermode = GET_MODE (op0);
1215 enum machine_mode innermode = GET_MODE_INNER (outermode);
1216 int icode = (int) optab_handler (vec_extract_optab, outermode)->insn_code;
1217 unsigned HOST_WIDE_INT pos = bitnum / GET_MODE_BITSIZE (innermode);
1218 rtx rtxpos = GEN_INT (pos);
1219 rtx src = op0;
1220 rtx dest = NULL, pat, seq;
1221 enum machine_mode mode0 = insn_data[icode].operand[0].mode;
1222 enum machine_mode mode1 = insn_data[icode].operand[1].mode;
1223 enum machine_mode mode2 = insn_data[icode].operand[2].mode;
1225 if (innermode == tmode || innermode == mode)
1226 dest = target;
1228 if (!dest)
1229 dest = gen_reg_rtx (innermode);
1231 start_sequence ();
1233 if (! (*insn_data[icode].operand[0].predicate) (dest, mode0))
1234 dest = copy_to_mode_reg (mode0, dest);
1236 if (! (*insn_data[icode].operand[1].predicate) (src, mode1))
1237 src = copy_to_mode_reg (mode1, src);
1239 if (! (*insn_data[icode].operand[2].predicate) (rtxpos, mode2))
1240 rtxpos = copy_to_mode_reg (mode1, rtxpos);
1242 /* We could handle this, but we should always be called with a pseudo
1243 for our targets and all insns should take them as outputs. */
1244 gcc_assert ((*insn_data[icode].operand[0].predicate) (dest, mode0)
1245 && (*insn_data[icode].operand[1].predicate) (src, mode1)
1246 && (*insn_data[icode].operand[2].predicate) (rtxpos, mode2));
1248 pat = GEN_FCN (icode) (dest, src, rtxpos);
1249 seq = get_insns ();
1250 end_sequence ();
1251 if (pat)
1253 emit_insn (seq);
1254 emit_insn (pat);
1255 if (mode0 != mode)
1256 return gen_lowpart (tmode, dest);
1257 return dest;
1261 /* Make sure we are playing with integral modes. Pun with subregs
1262 if we aren't. */
1264 enum machine_mode imode = int_mode_for_mode (GET_MODE (op0));
1265 if (imode != GET_MODE (op0))
1267 if (MEM_P (op0))
1268 op0 = adjust_address (op0, imode, 0);
1269 else if (imode != BLKmode)
1271 op0 = gen_lowpart (imode, op0);
1273 /* If we got a SUBREG, force it into a register since we
1274 aren't going to be able to do another SUBREG on it. */
1275 if (GET_CODE (op0) == SUBREG)
1276 op0 = force_reg (imode, op0);
1278 else if (REG_P (op0))
1280 rtx reg, subreg;
1281 imode = smallest_mode_for_size (GET_MODE_BITSIZE (GET_MODE (op0)),
1282 MODE_INT);
1283 reg = gen_reg_rtx (imode);
1284 subreg = gen_lowpart_SUBREG (GET_MODE (op0), reg);
1285 emit_move_insn (subreg, op0);
1286 op0 = reg;
1287 bitnum += SUBREG_BYTE (subreg) * BITS_PER_UNIT;
1289 else
1291 rtx mem = assign_stack_temp (GET_MODE (op0),
1292 GET_MODE_SIZE (GET_MODE (op0)), 0);
1293 emit_move_insn (mem, op0);
1294 op0 = adjust_address (mem, BLKmode, 0);
1299 /* We may be accessing data outside the field, which means
1300 we can alias adjacent data. */
1301 if (MEM_P (op0))
1303 op0 = shallow_copy_rtx (op0);
1304 set_mem_alias_set (op0, 0);
1305 set_mem_expr (op0, 0);
1308 /* Extraction of a full-word or multi-word value from a structure
1309 in a register or aligned memory can be done with just a SUBREG.
1310 A subword value in the least significant part of a register
1311 can also be extracted with a SUBREG. For this, we need the
1312 byte offset of the value in op0. */
1314 bitpos = bitnum % unit;
1315 offset = bitnum / unit;
1316 byte_offset = bitpos / BITS_PER_UNIT + offset * UNITS_PER_WORD;
1318 /* If OP0 is a register, BITPOS must count within a word.
1319 But as we have it, it counts within whatever size OP0 now has.
1320 On a bigendian machine, these are not the same, so convert. */
1321 if (BYTES_BIG_ENDIAN
1322 && !MEM_P (op0)
1323 && unit > GET_MODE_BITSIZE (GET_MODE (op0)))
1324 bitpos += unit - GET_MODE_BITSIZE (GET_MODE (op0));
1326 /* ??? We currently assume TARGET is at least as big as BITSIZE.
1327 If that's wrong, the solution is to test for it and set TARGET to 0
1328 if needed. */
1330 /* Only scalar integer modes can be converted via subregs. There is an
1331 additional problem for FP modes here in that they can have a precision
1332 which is different from the size. mode_for_size uses precision, but
1333 we want a mode based on the size, so we must avoid calling it for FP
1334 modes. */
1335 mode1 = (SCALAR_INT_MODE_P (tmode)
1336 ? mode_for_size (bitsize, GET_MODE_CLASS (tmode), 0)
1337 : mode);
1339 if (((bitsize >= BITS_PER_WORD && bitsize == GET_MODE_BITSIZE (mode)
1340 && bitpos % BITS_PER_WORD == 0)
1341 || (mode1 != BLKmode
1342 /* ??? The big endian test here is wrong. This is correct
1343 if the value is in a register, and if mode_for_size is not
1344 the same mode as op0. This causes us to get unnecessarily
1345 inefficient code from the Thumb port when -mbig-endian. */
1346 && (BYTES_BIG_ENDIAN
1347 ? bitpos + bitsize == BITS_PER_WORD
1348 : bitpos == 0)))
1349 && ((!MEM_P (op0)
1350 && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
1351 GET_MODE_BITSIZE (GET_MODE (op0)))
1352 && GET_MODE_SIZE (mode1) != 0
1353 && byte_offset % GET_MODE_SIZE (mode1) == 0)
1354 || (MEM_P (op0)
1355 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (op0))
1356 || (offset * BITS_PER_UNIT % bitsize == 0
1357 && MEM_ALIGN (op0) % bitsize == 0)))))
1359 if (MEM_P (op0))
1360 op0 = adjust_address (op0, mode1, offset);
1361 else if (mode1 != GET_MODE (op0))
1363 rtx sub = simplify_gen_subreg (mode1, op0, GET_MODE (op0),
1364 byte_offset);
1365 if (sub == NULL)
1366 goto no_subreg_mode_swap;
1367 op0 = sub;
1369 if (mode1 != mode)
1370 return convert_to_mode (tmode, op0, unsignedp);
1371 return op0;
1373 no_subreg_mode_swap:
1375 /* Handle fields bigger than a word. */
1377 if (bitsize > BITS_PER_WORD)
1379 /* Here we transfer the words of the field
1380 in the order least significant first.
1381 This is because the most significant word is the one which may
1382 be less than full. */
1384 unsigned int nwords = (bitsize + (BITS_PER_WORD - 1)) / BITS_PER_WORD;
1385 unsigned int i;
1387 if (target == 0 || !REG_P (target))
1388 target = gen_reg_rtx (mode);
1390 /* Indicate for flow that the entire target reg is being set. */
1391 emit_insn (gen_rtx_CLOBBER (VOIDmode, target));
1393 for (i = 0; i < nwords; i++)
1395 /* If I is 0, use the low-order word in both field and target;
1396 if I is 1, use the next to lowest word; and so on. */
1397 /* Word number in TARGET to use. */
1398 unsigned int wordnum
1399 = (WORDS_BIG_ENDIAN
1400 ? GET_MODE_SIZE (GET_MODE (target)) / UNITS_PER_WORD - i - 1
1401 : i);
1402 /* Offset from start of field in OP0. */
1403 unsigned int bit_offset = (WORDS_BIG_ENDIAN
1404 ? MAX (0, ((int) bitsize - ((int) i + 1)
1405 * (int) BITS_PER_WORD))
1406 : (int) i * BITS_PER_WORD);
1407 rtx target_part = operand_subword (target, wordnum, 1, VOIDmode);
1408 rtx result_part
1409 = extract_bit_field (op0, MIN (BITS_PER_WORD,
1410 bitsize - i * BITS_PER_WORD),
1411 bitnum + bit_offset, 1, target_part, mode,
1412 word_mode);
1414 gcc_assert (target_part);
1416 if (result_part != target_part)
1417 emit_move_insn (target_part, result_part);
1420 if (unsignedp)
1422 /* Unless we've filled TARGET, the upper regs in a multi-reg value
1423 need to be zero'd out. */
1424 if (GET_MODE_SIZE (GET_MODE (target)) > nwords * UNITS_PER_WORD)
1426 unsigned int i, total_words;
1428 total_words = GET_MODE_SIZE (GET_MODE (target)) / UNITS_PER_WORD;
1429 for (i = nwords; i < total_words; i++)
1430 emit_move_insn
1431 (operand_subword (target,
1432 WORDS_BIG_ENDIAN ? total_words - i - 1 : i,
1433 1, VOIDmode),
1434 const0_rtx);
1436 return target;
1439 /* Signed bit field: sign-extend with two arithmetic shifts. */
1440 target = expand_shift (LSHIFT_EXPR, mode, target,
1441 build_int_cst (NULL_TREE,
1442 GET_MODE_BITSIZE (mode) - bitsize),
1443 NULL_RTX, 0);
1444 return expand_shift (RSHIFT_EXPR, mode, target,
1445 build_int_cst (NULL_TREE,
1446 GET_MODE_BITSIZE (mode) - bitsize),
1447 NULL_RTX, 0);
1450 /* From here on we know the desired field is smaller than a word. */
1452 /* Check if there is a correspondingly-sized integer field, so we can
1453 safely extract it as one size of integer, if necessary; then
1454 truncate or extend to the size that is wanted; then use SUBREGs or
1455 convert_to_mode to get one of the modes we really wanted. */
1457 int_mode = int_mode_for_mode (tmode);
1458 if (int_mode == BLKmode)
1459 int_mode = int_mode_for_mode (mode);
1460 /* Should probably push op0 out to memory and then do a load. */
1461 gcc_assert (int_mode != BLKmode);
1463 /* OFFSET is the number of words or bytes (UNIT says which)
1464 from STR_RTX to the first word or byte containing part of the field. */
1465 if (!MEM_P (op0))
1467 if (offset != 0
1468 || GET_MODE_SIZE (GET_MODE (op0)) > UNITS_PER_WORD)
1470 if (!REG_P (op0))
1471 op0 = copy_to_reg (op0);
1472 op0 = gen_rtx_SUBREG (mode_for_size (BITS_PER_WORD, MODE_INT, 0),
1473 op0, (offset * UNITS_PER_WORD));
1475 offset = 0;
1478 /* Now OFFSET is nonzero only for memory operands. */
1479 ext_mode = mode_for_extraction (unsignedp ? EP_extzv : EP_extv, 0);
1480 icode = unsignedp ? CODE_FOR_extzv : CODE_FOR_extv;
1481 if (ext_mode != MAX_MACHINE_MODE
1482 && bitsize > 0
1483 && GET_MODE_BITSIZE (ext_mode) >= bitsize
1484 /* If op0 is a register, we need it in EXT_MODE to make it
1485 acceptable to the format of ext(z)v. */
1486 && !(GET_CODE (op0) == SUBREG && GET_MODE (op0) != ext_mode)
1487 && !((REG_P (op0) || GET_CODE (op0) == SUBREG)
1488 && (bitsize + bitpos > GET_MODE_BITSIZE (ext_mode)))
1489 && check_predicate_volatile_ok (icode, 1, op0, GET_MODE (op0)))
1491 unsigned HOST_WIDE_INT xbitpos = bitpos, xoffset = offset;
1492 rtx bitsize_rtx, bitpos_rtx;
1493 rtx last = get_last_insn ();
1494 rtx xop0 = op0;
1495 rtx xtarget = target;
1496 rtx xspec_target = target;
1497 rtx xspec_target_subreg = 0;
1498 rtx pat;
1500 /* If op0 is a register, we need it in EXT_MODE to make it
1501 acceptable to the format of ext(z)v. */
1502 if (REG_P (xop0) && GET_MODE (xop0) != ext_mode)
1503 xop0 = gen_rtx_SUBREG (ext_mode, xop0, 0);
1504 if (MEM_P (xop0))
1505 /* Get ref to first byte containing part of the field. */
1506 xop0 = adjust_address (xop0, byte_mode, xoffset);
1508 /* On big-endian machines, we count bits from the most significant.
1509 If the bit field insn does not, we must invert. */
1510 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
1511 xbitpos = unit - bitsize - xbitpos;
1513 /* Now convert from counting within UNIT to counting in EXT_MODE. */
1514 if (BITS_BIG_ENDIAN && !MEM_P (xop0))
1515 xbitpos += GET_MODE_BITSIZE (ext_mode) - unit;
1517 unit = GET_MODE_BITSIZE (ext_mode);
1519 if (xtarget == 0)
1520 xtarget = xspec_target = gen_reg_rtx (tmode);
1522 if (GET_MODE (xtarget) != ext_mode)
1524 if (REG_P (xtarget))
1526 xtarget = gen_lowpart (ext_mode, xtarget);
1527 if (GET_MODE_SIZE (ext_mode)
1528 > GET_MODE_SIZE (GET_MODE (xspec_target)))
1529 xspec_target_subreg = xtarget;
1531 else
1532 xtarget = gen_reg_rtx (ext_mode);
1535 /* If this machine's ext(z)v insists on a register target,
1536 make sure we have one. */
1537 if (!insn_data[(int) icode].operand[0].predicate (xtarget, ext_mode))
1538 xtarget = gen_reg_rtx (ext_mode);
1540 bitsize_rtx = GEN_INT (bitsize);
1541 bitpos_rtx = GEN_INT (xbitpos);
1543 pat = (unsignedp
1544 ? gen_extzv (xtarget, xop0, bitsize_rtx, bitpos_rtx)
1545 : gen_extv (xtarget, xop0, bitsize_rtx, bitpos_rtx));
1546 if (pat)
1548 emit_insn (pat);
1549 if (xtarget == xspec_target)
1550 return xtarget;
1551 if (xtarget == xspec_target_subreg)
1552 return xspec_target;
1553 return convert_extracted_bit_field (xtarget, mode, tmode, unsignedp);
1555 delete_insns_since (last);
1558 /* If OP0 is a memory, try copying it to a register and seeing if a
1559 cheap register alternative is available. */
1560 if (ext_mode != MAX_MACHINE_MODE && MEM_P (op0))
1562 enum machine_mode bestmode;
1564 /* Get the mode to use for inserting into this field. If
1565 OP0 is BLKmode, get the smallest mode consistent with the
1566 alignment. If OP0 is a non-BLKmode object that is no
1567 wider than EXT_MODE, use its mode. Otherwise, use the
1568 smallest mode containing the field. */
1570 if (GET_MODE (op0) == BLKmode
1571 || (ext_mode != MAX_MACHINE_MODE
1572 && GET_MODE_SIZE (GET_MODE (op0)) > GET_MODE_SIZE (ext_mode)))
1573 bestmode = get_best_mode (bitsize, bitnum, MEM_ALIGN (op0),
1574 (ext_mode == MAX_MACHINE_MODE
1575 ? VOIDmode : ext_mode),
1576 MEM_VOLATILE_P (op0));
1577 else
1578 bestmode = GET_MODE (op0);
1580 if (bestmode != VOIDmode
1581 && !(SLOW_UNALIGNED_ACCESS (bestmode, MEM_ALIGN (op0))
1582 && GET_MODE_BITSIZE (bestmode) > MEM_ALIGN (op0)))
1584 unsigned HOST_WIDE_INT xoffset, xbitpos;
1586 /* Compute the offset as a multiple of this unit,
1587 counting in bytes. */
1588 unit = GET_MODE_BITSIZE (bestmode);
1589 xoffset = (bitnum / unit) * GET_MODE_SIZE (bestmode);
1590 xbitpos = bitnum % unit;
1592 /* Make sure the register is big enough for the whole field. */
1593 if (xoffset * BITS_PER_UNIT + unit
1594 >= offset * BITS_PER_UNIT + bitsize)
1596 rtx last, result, xop0;
1598 last = get_last_insn ();
1600 /* Fetch it to a register in that size. */
1601 xop0 = adjust_address (op0, bestmode, xoffset);
1602 xop0 = force_reg (bestmode, xop0);
1603 result = extract_bit_field_1 (xop0, bitsize, xbitpos,
1604 unsignedp, target,
1605 mode, tmode, false);
1606 if (result)
1607 return result;
1609 delete_insns_since (last);
1614 if (!fallback_p)
1615 return NULL;
1617 target = extract_fixed_bit_field (int_mode, op0, offset, bitsize,
1618 bitpos, target, unsignedp);
1619 return convert_extracted_bit_field (target, mode, tmode, unsignedp);
1622 /* Generate code to extract a byte-field from STR_RTX
1623 containing BITSIZE bits, starting at BITNUM,
1624 and put it in TARGET if possible (if TARGET is nonzero).
1625 Regardless of TARGET, we return the rtx for where the value is placed.
1627 STR_RTX is the structure containing the byte (a REG or MEM).
1628 UNSIGNEDP is nonzero if this is an unsigned bit field.
1629 MODE is the natural mode of the field value once extracted.
1630 TMODE is the mode the caller would like the value to have;
1631 but the value may be returned with type MODE instead.
1633 If a TARGET is specified and we can store in it at no extra cost,
1634 we do so, and return TARGET.
1635 Otherwise, we return a REG of mode TMODE or MODE, with TMODE preferred
1636 if they are equally easy. */
1639 extract_bit_field (rtx str_rtx, unsigned HOST_WIDE_INT bitsize,
1640 unsigned HOST_WIDE_INT bitnum, int unsignedp, rtx target,
1641 enum machine_mode mode, enum machine_mode tmode)
1643 return extract_bit_field_1 (str_rtx, bitsize, bitnum, unsignedp,
1644 target, mode, tmode, true);
1647 /* Extract a bit field using shifts and boolean operations
1648 Returns an rtx to represent the value.
1649 OP0 addresses a register (word) or memory (byte).
1650 BITPOS says which bit within the word or byte the bit field starts in.
1651 OFFSET says how many bytes farther the bit field starts;
1652 it is 0 if OP0 is a register.
1653 BITSIZE says how many bits long the bit field is.
1654 (If OP0 is a register, it may be narrower than a full word,
1655 but BITPOS still counts within a full word,
1656 which is significant on bigendian machines.)
1658 UNSIGNEDP is nonzero for an unsigned bit field (don't sign-extend value).
1659 If TARGET is nonzero, attempts to store the value there
1660 and return TARGET, but this is not guaranteed.
1661 If TARGET is not used, create a pseudo-reg of mode TMODE for the value. */
1663 static rtx
1664 extract_fixed_bit_field (enum machine_mode tmode, rtx op0,
1665 unsigned HOST_WIDE_INT offset,
1666 unsigned HOST_WIDE_INT bitsize,
1667 unsigned HOST_WIDE_INT bitpos, rtx target,
1668 int unsignedp)
1670 unsigned int total_bits = BITS_PER_WORD;
1671 enum machine_mode mode;
1673 if (GET_CODE (op0) == SUBREG || REG_P (op0))
1675 /* Special treatment for a bit field split across two registers. */
1676 if (bitsize + bitpos > BITS_PER_WORD)
1677 return extract_split_bit_field (op0, bitsize, bitpos, unsignedp);
1679 else
1681 /* Get the proper mode to use for this field. We want a mode that
1682 includes the entire field. If such a mode would be larger than
1683 a word, we won't be doing the extraction the normal way. */
1685 mode = get_best_mode (bitsize, bitpos + offset * BITS_PER_UNIT,
1686 MEM_ALIGN (op0), word_mode, MEM_VOLATILE_P (op0));
1688 if (mode == VOIDmode)
1689 /* The only way this should occur is if the field spans word
1690 boundaries. */
1691 return extract_split_bit_field (op0, bitsize,
1692 bitpos + offset * BITS_PER_UNIT,
1693 unsignedp);
1695 total_bits = GET_MODE_BITSIZE (mode);
1697 /* Make sure bitpos is valid for the chosen mode. Adjust BITPOS to
1698 be in the range 0 to total_bits-1, and put any excess bytes in
1699 OFFSET. */
1700 if (bitpos >= total_bits)
1702 offset += (bitpos / total_bits) * (total_bits / BITS_PER_UNIT);
1703 bitpos -= ((bitpos / total_bits) * (total_bits / BITS_PER_UNIT)
1704 * BITS_PER_UNIT);
1707 /* Get ref to an aligned byte, halfword, or word containing the field.
1708 Adjust BITPOS to be position within a word,
1709 and OFFSET to be the offset of that word.
1710 Then alter OP0 to refer to that word. */
1711 bitpos += (offset % (total_bits / BITS_PER_UNIT)) * BITS_PER_UNIT;
1712 offset -= (offset % (total_bits / BITS_PER_UNIT));
1713 op0 = adjust_address (op0, mode, offset);
1716 mode = GET_MODE (op0);
1718 if (BYTES_BIG_ENDIAN)
1719 /* BITPOS is the distance between our msb and that of OP0.
1720 Convert it to the distance from the lsb. */
1721 bitpos = total_bits - bitsize - bitpos;
1723 /* Now BITPOS is always the distance between the field's lsb and that of OP0.
1724 We have reduced the big-endian case to the little-endian case. */
1726 if (unsignedp)
1728 if (bitpos)
1730 /* If the field does not already start at the lsb,
1731 shift it so it does. */
1732 tree amount = build_int_cst (NULL_TREE, bitpos);
1733 /* Maybe propagate the target for the shift. */
1734 /* But not if we will return it--could confuse integrate.c. */
1735 rtx subtarget = (target != 0 && REG_P (target) ? target : 0);
1736 if (tmode != mode) subtarget = 0;
1737 op0 = expand_shift (RSHIFT_EXPR, mode, op0, amount, subtarget, 1);
1739 /* Convert the value to the desired mode. */
1740 if (mode != tmode)
1741 op0 = convert_to_mode (tmode, op0, 1);
1743 /* Unless the msb of the field used to be the msb when we shifted,
1744 mask out the upper bits. */
1746 if (GET_MODE_BITSIZE (mode) != bitpos + bitsize)
1747 return expand_binop (GET_MODE (op0), and_optab, op0,
1748 mask_rtx (GET_MODE (op0), 0, bitsize, 0),
1749 target, 1, OPTAB_LIB_WIDEN);
1750 return op0;
1753 /* To extract a signed bit-field, first shift its msb to the msb of the word,
1754 then arithmetic-shift its lsb to the lsb of the word. */
1755 op0 = force_reg (mode, op0);
1756 if (mode != tmode)
1757 target = 0;
1759 /* Find the narrowest integer mode that contains the field. */
1761 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1762 mode = GET_MODE_WIDER_MODE (mode))
1763 if (GET_MODE_BITSIZE (mode) >= bitsize + bitpos)
1765 op0 = convert_to_mode (mode, op0, 0);
1766 break;
1769 if (GET_MODE_BITSIZE (mode) != (bitsize + bitpos))
1771 tree amount
1772 = build_int_cst (NULL_TREE,
1773 GET_MODE_BITSIZE (mode) - (bitsize + bitpos));
1774 /* Maybe propagate the target for the shift. */
1775 rtx subtarget = (target != 0 && REG_P (target) ? target : 0);
1776 op0 = expand_shift (LSHIFT_EXPR, mode, op0, amount, subtarget, 1);
1779 return expand_shift (RSHIFT_EXPR, mode, op0,
1780 build_int_cst (NULL_TREE,
1781 GET_MODE_BITSIZE (mode) - bitsize),
1782 target, 0);
1785 /* Return a constant integer (CONST_INT or CONST_DOUBLE) mask value
1786 of mode MODE with BITSIZE ones followed by BITPOS zeros, or the
1787 complement of that if COMPLEMENT. The mask is truncated if
1788 necessary to the width of mode MODE. The mask is zero-extended if
1789 BITSIZE+BITPOS is too small for MODE. */
1791 static rtx
1792 mask_rtx (enum machine_mode mode, int bitpos, int bitsize, int complement)
1794 HOST_WIDE_INT masklow, maskhigh;
1796 if (bitsize == 0)
1797 masklow = 0;
1798 else if (bitpos < HOST_BITS_PER_WIDE_INT)
1799 masklow = (HOST_WIDE_INT) -1 << bitpos;
1800 else
1801 masklow = 0;
1803 if (bitpos + bitsize < HOST_BITS_PER_WIDE_INT)
1804 masklow &= ((unsigned HOST_WIDE_INT) -1
1805 >> (HOST_BITS_PER_WIDE_INT - bitpos - bitsize));
1807 if (bitpos <= HOST_BITS_PER_WIDE_INT)
1808 maskhigh = -1;
1809 else
1810 maskhigh = (HOST_WIDE_INT) -1 << (bitpos - HOST_BITS_PER_WIDE_INT);
1812 if (bitsize == 0)
1813 maskhigh = 0;
1814 else if (bitpos + bitsize > HOST_BITS_PER_WIDE_INT)
1815 maskhigh &= ((unsigned HOST_WIDE_INT) -1
1816 >> (2 * HOST_BITS_PER_WIDE_INT - bitpos - bitsize));
1817 else
1818 maskhigh = 0;
1820 if (complement)
1822 maskhigh = ~maskhigh;
1823 masklow = ~masklow;
1826 return immed_double_const (masklow, maskhigh, mode);
1829 /* Return a constant integer (CONST_INT or CONST_DOUBLE) rtx with the value
1830 VALUE truncated to BITSIZE bits and then shifted left BITPOS bits. */
1832 static rtx
1833 lshift_value (enum machine_mode mode, rtx value, int bitpos, int bitsize)
1835 unsigned HOST_WIDE_INT v = INTVAL (value);
1836 HOST_WIDE_INT low, high;
1838 if (bitsize < HOST_BITS_PER_WIDE_INT)
1839 v &= ~((HOST_WIDE_INT) -1 << bitsize);
1841 if (bitpos < HOST_BITS_PER_WIDE_INT)
1843 low = v << bitpos;
1844 high = (bitpos > 0 ? (v >> (HOST_BITS_PER_WIDE_INT - bitpos)) : 0);
1846 else
1848 low = 0;
1849 high = v << (bitpos - HOST_BITS_PER_WIDE_INT);
1852 return immed_double_const (low, high, mode);
1855 /* Extract a bit field from a memory by forcing the alignment of the
1856 memory. This efficient only if the field spans at least 4 boundaries.
1858 OP0 is the MEM.
1859 BITSIZE is the field width; BITPOS is the position of the first bit.
1860 UNSIGNEDP is true if the result should be zero-extended. */
1862 static rtx
1863 extract_force_align_mem_bit_field (rtx op0, unsigned HOST_WIDE_INT bitsize,
1864 unsigned HOST_WIDE_INT bitpos,
1865 int unsignedp)
1867 enum machine_mode mode, dmode;
1868 unsigned int m_bitsize, m_size;
1869 unsigned int sign_shift_up, sign_shift_dn;
1870 rtx base, a1, a2, v1, v2, comb, shift, result, start;
1872 /* Choose a mode that will fit BITSIZE. */
1873 mode = smallest_mode_for_size (bitsize, MODE_INT);
1874 m_size = GET_MODE_SIZE (mode);
1875 m_bitsize = GET_MODE_BITSIZE (mode);
1877 /* Choose a mode twice as wide. Fail if no such mode exists. */
1878 dmode = mode_for_size (m_bitsize * 2, MODE_INT, false);
1879 if (dmode == BLKmode)
1880 return NULL;
1882 do_pending_stack_adjust ();
1883 start = get_last_insn ();
1885 /* At the end, we'll need an additional shift to deal with sign/zero
1886 extension. By default this will be a left+right shift of the
1887 appropriate size. But we may be able to eliminate one of them. */
1888 sign_shift_up = sign_shift_dn = m_bitsize - bitsize;
1890 if (STRICT_ALIGNMENT)
1892 base = plus_constant (XEXP (op0, 0), bitpos / BITS_PER_UNIT);
1893 bitpos %= BITS_PER_UNIT;
1895 /* We load two values to be concatenate. There's an edge condition
1896 that bears notice -- an aligned value at the end of a page can
1897 only load one value lest we segfault. So the two values we load
1898 are at "base & -size" and "(base + size - 1) & -size". If base
1899 is unaligned, the addresses will be aligned and sequential; if
1900 base is aligned, the addresses will both be equal to base. */
1902 a1 = expand_simple_binop (Pmode, AND, force_operand (base, NULL),
1903 GEN_INT (-(HOST_WIDE_INT)m_size),
1904 NULL, true, OPTAB_LIB_WIDEN);
1905 mark_reg_pointer (a1, m_bitsize);
1906 v1 = gen_rtx_MEM (mode, a1);
1907 set_mem_align (v1, m_bitsize);
1908 v1 = force_reg (mode, validize_mem (v1));
1910 a2 = plus_constant (base, GET_MODE_SIZE (mode) - 1);
1911 a2 = expand_simple_binop (Pmode, AND, force_operand (a2, NULL),
1912 GEN_INT (-(HOST_WIDE_INT)m_size),
1913 NULL, true, OPTAB_LIB_WIDEN);
1914 v2 = gen_rtx_MEM (mode, a2);
1915 set_mem_align (v2, m_bitsize);
1916 v2 = force_reg (mode, validize_mem (v2));
1918 /* Combine these two values into a double-word value. */
1919 if (m_bitsize == BITS_PER_WORD)
1921 comb = gen_reg_rtx (dmode);
1922 emit_insn (gen_rtx_CLOBBER (VOIDmode, comb));
1923 emit_move_insn (gen_rtx_SUBREG (mode, comb, 0), v1);
1924 emit_move_insn (gen_rtx_SUBREG (mode, comb, m_size), v2);
1926 else
1928 if (BYTES_BIG_ENDIAN)
1929 comb = v1, v1 = v2, v2 = comb;
1930 v1 = convert_modes (dmode, mode, v1, true);
1931 if (v1 == NULL)
1932 goto fail;
1933 v2 = convert_modes (dmode, mode, v2, true);
1934 v2 = expand_simple_binop (dmode, ASHIFT, v2, GEN_INT (m_bitsize),
1935 NULL, true, OPTAB_LIB_WIDEN);
1936 if (v2 == NULL)
1937 goto fail;
1938 comb = expand_simple_binop (dmode, IOR, v1, v2, NULL,
1939 true, OPTAB_LIB_WIDEN);
1940 if (comb == NULL)
1941 goto fail;
1944 shift = expand_simple_binop (Pmode, AND, base, GEN_INT (m_size - 1),
1945 NULL, true, OPTAB_LIB_WIDEN);
1946 shift = expand_mult (Pmode, shift, GEN_INT (BITS_PER_UNIT), NULL, 1);
1948 if (bitpos != 0)
1950 if (sign_shift_up <= bitpos)
1951 bitpos -= sign_shift_up, sign_shift_up = 0;
1952 shift = expand_simple_binop (Pmode, PLUS, shift, GEN_INT (bitpos),
1953 NULL, true, OPTAB_LIB_WIDEN);
1956 else
1958 unsigned HOST_WIDE_INT offset = bitpos / BITS_PER_UNIT;
1959 bitpos %= BITS_PER_UNIT;
1961 /* When strict alignment is not required, we can just load directly
1962 from memory without masking. If the remaining BITPOS offset is
1963 small enough, we may be able to do all operations in MODE as
1964 opposed to DMODE. */
1965 if (bitpos + bitsize <= m_bitsize)
1966 dmode = mode;
1967 comb = adjust_address (op0, dmode, offset);
1969 if (sign_shift_up <= bitpos)
1970 bitpos -= sign_shift_up, sign_shift_up = 0;
1971 shift = GEN_INT (bitpos);
1974 /* Shift down the double-word such that the requested value is at bit 0. */
1975 if (shift != const0_rtx)
1976 comb = expand_simple_binop (dmode, unsignedp ? LSHIFTRT : ASHIFTRT,
1977 comb, shift, NULL, unsignedp, OPTAB_LIB_WIDEN);
1978 if (comb == NULL)
1979 goto fail;
1981 /* If the field exactly matches MODE, then all we need to do is return the
1982 lowpart. Otherwise, shift to get the sign bits set properly. */
1983 result = force_reg (mode, gen_lowpart (mode, comb));
1985 if (sign_shift_up)
1986 result = expand_simple_binop (mode, ASHIFT, result,
1987 GEN_INT (sign_shift_up),
1988 NULL_RTX, 0, OPTAB_LIB_WIDEN);
1989 if (sign_shift_dn)
1990 result = expand_simple_binop (mode, unsignedp ? LSHIFTRT : ASHIFTRT,
1991 result, GEN_INT (sign_shift_dn),
1992 NULL_RTX, 0, OPTAB_LIB_WIDEN);
1994 return result;
1996 fail:
1997 delete_insns_since (start);
1998 return NULL;
2001 /* Extract a bit field that is split across two words
2002 and return an RTX for the result.
2004 OP0 is the REG, SUBREG or MEM rtx for the first of the two words.
2005 BITSIZE is the field width; BITPOS, position of its first bit, in the word.
2006 UNSIGNEDP is 1 if should zero-extend the contents; else sign-extend. */
2008 static rtx
2009 extract_split_bit_field (rtx op0, unsigned HOST_WIDE_INT bitsize,
2010 unsigned HOST_WIDE_INT bitpos, int unsignedp)
2012 unsigned int unit;
2013 unsigned int bitsdone = 0;
2014 rtx result = NULL_RTX;
2015 int first = 1;
2017 /* Make sure UNIT isn't larger than BITS_PER_WORD, we can only handle that
2018 much at a time. */
2019 if (REG_P (op0) || GET_CODE (op0) == SUBREG)
2020 unit = BITS_PER_WORD;
2021 else
2023 unit = MIN (MEM_ALIGN (op0), BITS_PER_WORD);
2024 if (0 && bitsize / unit > 2)
2026 rtx tmp = extract_force_align_mem_bit_field (op0, bitsize, bitpos,
2027 unsignedp);
2028 if (tmp)
2029 return tmp;
2033 while (bitsdone < bitsize)
2035 unsigned HOST_WIDE_INT thissize;
2036 rtx part, word;
2037 unsigned HOST_WIDE_INT thispos;
2038 unsigned HOST_WIDE_INT offset;
2040 offset = (bitpos + bitsdone) / unit;
2041 thispos = (bitpos + bitsdone) % unit;
2043 /* THISSIZE must not overrun a word boundary. Otherwise,
2044 extract_fixed_bit_field will call us again, and we will mutually
2045 recurse forever. */
2046 thissize = MIN (bitsize - bitsdone, BITS_PER_WORD);
2047 thissize = MIN (thissize, unit - thispos);
2049 /* If OP0 is a register, then handle OFFSET here.
2051 When handling multiword bitfields, extract_bit_field may pass
2052 down a word_mode SUBREG of a larger REG for a bitfield that actually
2053 crosses a word boundary. Thus, for a SUBREG, we must find
2054 the current word starting from the base register. */
2055 if (GET_CODE (op0) == SUBREG)
2057 int word_offset = (SUBREG_BYTE (op0) / UNITS_PER_WORD) + offset;
2058 word = operand_subword_force (SUBREG_REG (op0), word_offset,
2059 GET_MODE (SUBREG_REG (op0)));
2060 offset = 0;
2062 else if (REG_P (op0))
2064 word = operand_subword_force (op0, offset, GET_MODE (op0));
2065 offset = 0;
2067 else
2068 word = op0;
2070 /* Extract the parts in bit-counting order,
2071 whose meaning is determined by BYTES_PER_UNIT.
2072 OFFSET is in UNITs, and UNIT is in bits.
2073 extract_fixed_bit_field wants offset in bytes. */
2074 part = extract_fixed_bit_field (word_mode, word,
2075 offset * unit / BITS_PER_UNIT,
2076 thissize, thispos, 0, 1);
2077 bitsdone += thissize;
2079 /* Shift this part into place for the result. */
2080 if (BYTES_BIG_ENDIAN)
2082 if (bitsize != bitsdone)
2083 part = expand_shift (LSHIFT_EXPR, word_mode, part,
2084 build_int_cst (NULL_TREE, bitsize - bitsdone),
2085 0, 1);
2087 else
2089 if (bitsdone != thissize)
2090 part = expand_shift (LSHIFT_EXPR, word_mode, part,
2091 build_int_cst (NULL_TREE,
2092 bitsdone - thissize), 0, 1);
2095 if (first)
2096 result = part;
2097 else
2098 /* Combine the parts with bitwise or. This works
2099 because we extracted each part as an unsigned bit field. */
2100 result = expand_binop (word_mode, ior_optab, part, result, NULL_RTX, 1,
2101 OPTAB_LIB_WIDEN);
2103 first = 0;
2106 /* Unsigned bit field: we are done. */
2107 if (unsignedp)
2108 return result;
2109 /* Signed bit field: sign-extend with two arithmetic shifts. */
2110 result = expand_shift (LSHIFT_EXPR, word_mode, result,
2111 build_int_cst (NULL_TREE, BITS_PER_WORD - bitsize),
2112 NULL_RTX, 0);
2113 return expand_shift (RSHIFT_EXPR, word_mode, result,
2114 build_int_cst (NULL_TREE, BITS_PER_WORD - bitsize),
2115 NULL_RTX, 0);
2118 /* Add INC into TARGET. */
2120 void
2121 expand_inc (rtx target, rtx inc)
2123 rtx value = expand_binop (GET_MODE (target), add_optab,
2124 target, inc,
2125 target, 0, OPTAB_LIB_WIDEN);
2126 if (value != target)
2127 emit_move_insn (target, value);
2130 /* Subtract DEC from TARGET. */
2132 void
2133 expand_dec (rtx target, rtx dec)
2135 rtx value = expand_binop (GET_MODE (target), sub_optab,
2136 target, dec,
2137 target, 0, OPTAB_LIB_WIDEN);
2138 if (value != target)
2139 emit_move_insn (target, value);
2142 /* Output a shift instruction for expression code CODE,
2143 with SHIFTED being the rtx for the value to shift,
2144 and AMOUNT the tree for the amount to shift by.
2145 Store the result in the rtx TARGET, if that is convenient.
2146 If UNSIGNEDP is nonzero, do a logical shift; otherwise, arithmetic.
2147 Return the rtx for where the value is. */
2150 expand_shift (enum tree_code code, enum machine_mode mode, rtx shifted,
2151 tree amount, rtx target, int unsignedp)
2153 rtx op1, temp = 0;
2154 int left = (code == LSHIFT_EXPR || code == LROTATE_EXPR);
2155 int rotate = (code == LROTATE_EXPR || code == RROTATE_EXPR);
2156 int try;
2158 /* Previously detected shift-counts computed by NEGATE_EXPR
2159 and shifted in the other direction; but that does not work
2160 on all machines. */
2162 op1 = expand_normal (amount);
2164 if (SHIFT_COUNT_TRUNCATED)
2166 if (GET_CODE (op1) == CONST_INT
2167 && ((unsigned HOST_WIDE_INT) INTVAL (op1) >=
2168 (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (mode)))
2169 op1 = GEN_INT ((unsigned HOST_WIDE_INT) INTVAL (op1)
2170 % GET_MODE_BITSIZE (mode));
2171 else if (GET_CODE (op1) == SUBREG
2172 && subreg_lowpart_p (op1))
2173 op1 = SUBREG_REG (op1);
2176 if (op1 == const0_rtx)
2177 return shifted;
2179 /* Check whether its cheaper to implement a left shift by a constant
2180 bit count by a sequence of additions. */
2181 if (code == LSHIFT_EXPR
2182 && GET_CODE (op1) == CONST_INT
2183 && INTVAL (op1) > 0
2184 && INTVAL (op1) < GET_MODE_BITSIZE (mode)
2185 && INTVAL (op1) < MAX_BITS_PER_WORD
2186 && shift_cost[mode][INTVAL (op1)] > INTVAL (op1) * add_cost[mode]
2187 && shift_cost[mode][INTVAL (op1)] != MAX_COST)
2189 int i;
2190 for (i = 0; i < INTVAL (op1); i++)
2192 temp = force_reg (mode, shifted);
2193 shifted = expand_binop (mode, add_optab, temp, temp, NULL_RTX,
2194 unsignedp, OPTAB_LIB_WIDEN);
2196 return shifted;
2199 for (try = 0; temp == 0 && try < 3; try++)
2201 enum optab_methods methods;
2203 if (try == 0)
2204 methods = OPTAB_DIRECT;
2205 else if (try == 1)
2206 methods = OPTAB_WIDEN;
2207 else
2208 methods = OPTAB_LIB_WIDEN;
2210 if (rotate)
2212 /* Widening does not work for rotation. */
2213 if (methods == OPTAB_WIDEN)
2214 continue;
2215 else if (methods == OPTAB_LIB_WIDEN)
2217 /* If we have been unable to open-code this by a rotation,
2218 do it as the IOR of two shifts. I.e., to rotate A
2219 by N bits, compute (A << N) | ((unsigned) A >> (C - N))
2220 where C is the bitsize of A.
2222 It is theoretically possible that the target machine might
2223 not be able to perform either shift and hence we would
2224 be making two libcalls rather than just the one for the
2225 shift (similarly if IOR could not be done). We will allow
2226 this extremely unlikely lossage to avoid complicating the
2227 code below. */
2229 rtx subtarget = target == shifted ? 0 : target;
2230 tree new_amount, other_amount;
2231 rtx temp1;
2232 tree type = TREE_TYPE (amount);
2233 if (GET_MODE (op1) != TYPE_MODE (type)
2234 && GET_MODE (op1) != VOIDmode)
2235 op1 = convert_to_mode (TYPE_MODE (type), op1, 1);
2236 new_amount = make_tree (type, op1);
2237 other_amount
2238 = fold_build2 (MINUS_EXPR, type,
2239 build_int_cst (type, GET_MODE_BITSIZE (mode)),
2240 new_amount);
2242 shifted = force_reg (mode, shifted);
2244 temp = expand_shift (left ? LSHIFT_EXPR : RSHIFT_EXPR,
2245 mode, shifted, new_amount, 0, 1);
2246 temp1 = expand_shift (left ? RSHIFT_EXPR : LSHIFT_EXPR,
2247 mode, shifted, other_amount, subtarget, 1);
2248 return expand_binop (mode, ior_optab, temp, temp1, target,
2249 unsignedp, methods);
2252 temp = expand_binop (mode,
2253 left ? rotl_optab : rotr_optab,
2254 shifted, op1, target, unsignedp, methods);
2256 else if (unsignedp)
2257 temp = expand_binop (mode,
2258 left ? ashl_optab : lshr_optab,
2259 shifted, op1, target, unsignedp, methods);
2261 /* Do arithmetic shifts.
2262 Also, if we are going to widen the operand, we can just as well
2263 use an arithmetic right-shift instead of a logical one. */
2264 if (temp == 0 && ! rotate
2265 && (! unsignedp || (! left && methods == OPTAB_WIDEN)))
2267 enum optab_methods methods1 = methods;
2269 /* If trying to widen a log shift to an arithmetic shift,
2270 don't accept an arithmetic shift of the same size. */
2271 if (unsignedp)
2272 methods1 = OPTAB_MUST_WIDEN;
2274 /* Arithmetic shift */
2276 temp = expand_binop (mode,
2277 left ? ashl_optab : ashr_optab,
2278 shifted, op1, target, unsignedp, methods1);
2281 /* We used to try extzv here for logical right shifts, but that was
2282 only useful for one machine, the VAX, and caused poor code
2283 generation there for lshrdi3, so the code was deleted and a
2284 define_expand for lshrsi3 was added to vax.md. */
2287 gcc_assert (temp);
2288 return temp;
2291 enum alg_code {
2292 alg_unknown,
2293 alg_zero,
2294 alg_m, alg_shift,
2295 alg_add_t_m2,
2296 alg_sub_t_m2,
2297 alg_add_factor,
2298 alg_sub_factor,
2299 alg_add_t2_m,
2300 alg_sub_t2_m,
2301 alg_impossible
2304 /* This structure holds the "cost" of a multiply sequence. The
2305 "cost" field holds the total rtx_cost of every operator in the
2306 synthetic multiplication sequence, hence cost(a op b) is defined
2307 as rtx_cost(op) + cost(a) + cost(b), where cost(leaf) is zero.
2308 The "latency" field holds the minimum possible latency of the
2309 synthetic multiply, on a hypothetical infinitely parallel CPU.
2310 This is the critical path, or the maximum height, of the expression
2311 tree which is the sum of rtx_costs on the most expensive path from
2312 any leaf to the root. Hence latency(a op b) is defined as zero for
2313 leaves and rtx_cost(op) + max(latency(a), latency(b)) otherwise. */
2315 struct mult_cost {
2316 short cost; /* Total rtx_cost of the multiplication sequence. */
2317 short latency; /* The latency of the multiplication sequence. */
2320 /* This macro is used to compare a pointer to a mult_cost against an
2321 single integer "rtx_cost" value. This is equivalent to the macro
2322 CHEAPER_MULT_COST(X,Z) where Z = {Y,Y}. */
2323 #define MULT_COST_LESS(X,Y) ((X)->cost < (Y) \
2324 || ((X)->cost == (Y) && (X)->latency < (Y)))
2326 /* This macro is used to compare two pointers to mult_costs against
2327 each other. The macro returns true if X is cheaper than Y.
2328 Currently, the cheaper of two mult_costs is the one with the
2329 lower "cost". If "cost"s are tied, the lower latency is cheaper. */
2330 #define CHEAPER_MULT_COST(X,Y) ((X)->cost < (Y)->cost \
2331 || ((X)->cost == (Y)->cost \
2332 && (X)->latency < (Y)->latency))
2334 /* This structure records a sequence of operations.
2335 `ops' is the number of operations recorded.
2336 `cost' is their total cost.
2337 The operations are stored in `op' and the corresponding
2338 logarithms of the integer coefficients in `log'.
2340 These are the operations:
2341 alg_zero total := 0;
2342 alg_m total := multiplicand;
2343 alg_shift total := total * coeff
2344 alg_add_t_m2 total := total + multiplicand * coeff;
2345 alg_sub_t_m2 total := total - multiplicand * coeff;
2346 alg_add_factor total := total * coeff + total;
2347 alg_sub_factor total := total * coeff - total;
2348 alg_add_t2_m total := total * coeff + multiplicand;
2349 alg_sub_t2_m total := total * coeff - multiplicand;
2351 The first operand must be either alg_zero or alg_m. */
2353 struct algorithm
2355 struct mult_cost cost;
2356 short ops;
2357 /* The size of the OP and LOG fields are not directly related to the
2358 word size, but the worst-case algorithms will be if we have few
2359 consecutive ones or zeros, i.e., a multiplicand like 10101010101...
2360 In that case we will generate shift-by-2, add, shift-by-2, add,...,
2361 in total wordsize operations. */
2362 enum alg_code op[MAX_BITS_PER_WORD];
2363 char log[MAX_BITS_PER_WORD];
2366 /* The entry for our multiplication cache/hash table. */
2367 struct alg_hash_entry {
2368 /* The number we are multiplying by. */
2369 unsigned HOST_WIDE_INT t;
2371 /* The mode in which we are multiplying something by T. */
2372 enum machine_mode mode;
2374 /* The best multiplication algorithm for t. */
2375 enum alg_code alg;
2377 /* The cost of multiplication if ALG_CODE is not alg_impossible.
2378 Otherwise, the cost within which multiplication by T is
2379 impossible. */
2380 struct mult_cost cost;
2383 /* The number of cache/hash entries. */
2384 #if HOST_BITS_PER_WIDE_INT == 64
2385 #define NUM_ALG_HASH_ENTRIES 1031
2386 #else
2387 #define NUM_ALG_HASH_ENTRIES 307
2388 #endif
2390 /* Each entry of ALG_HASH caches alg_code for some integer. This is
2391 actually a hash table. If we have a collision, that the older
2392 entry is kicked out. */
2393 static struct alg_hash_entry alg_hash[NUM_ALG_HASH_ENTRIES];
2395 /* Indicates the type of fixup needed after a constant multiplication.
2396 BASIC_VARIANT means no fixup is needed, NEGATE_VARIANT means that
2397 the result should be negated, and ADD_VARIANT means that the
2398 multiplicand should be added to the result. */
2399 enum mult_variant {basic_variant, negate_variant, add_variant};
2401 static void synth_mult (struct algorithm *, unsigned HOST_WIDE_INT,
2402 const struct mult_cost *, enum machine_mode mode);
2403 static bool choose_mult_variant (enum machine_mode, HOST_WIDE_INT,
2404 struct algorithm *, enum mult_variant *, int);
2405 static rtx expand_mult_const (enum machine_mode, rtx, HOST_WIDE_INT, rtx,
2406 const struct algorithm *, enum mult_variant);
2407 static unsigned HOST_WIDE_INT choose_multiplier (unsigned HOST_WIDE_INT, int,
2408 int, rtx *, int *, int *);
2409 static unsigned HOST_WIDE_INT invert_mod2n (unsigned HOST_WIDE_INT, int);
2410 static rtx extract_high_half (enum machine_mode, rtx);
2411 static rtx expand_mult_highpart (enum machine_mode, rtx, rtx, rtx, int, int);
2412 static rtx expand_mult_highpart_optab (enum machine_mode, rtx, rtx, rtx,
2413 int, int);
2414 /* Compute and return the best algorithm for multiplying by T.
2415 The algorithm must cost less than cost_limit
2416 If retval.cost >= COST_LIMIT, no algorithm was found and all
2417 other field of the returned struct are undefined.
2418 MODE is the machine mode of the multiplication. */
2420 static void
2421 synth_mult (struct algorithm *alg_out, unsigned HOST_WIDE_INT t,
2422 const struct mult_cost *cost_limit, enum machine_mode mode)
2424 int m;
2425 struct algorithm *alg_in, *best_alg;
2426 struct mult_cost best_cost;
2427 struct mult_cost new_limit;
2428 int op_cost, op_latency;
2429 unsigned HOST_WIDE_INT q;
2430 int maxm = MIN (BITS_PER_WORD, GET_MODE_BITSIZE (mode));
2431 int hash_index;
2432 bool cache_hit = false;
2433 enum alg_code cache_alg = alg_zero;
2435 /* Indicate that no algorithm is yet found. If no algorithm
2436 is found, this value will be returned and indicate failure. */
2437 alg_out->cost.cost = cost_limit->cost + 1;
2438 alg_out->cost.latency = cost_limit->latency + 1;
2440 if (cost_limit->cost < 0
2441 || (cost_limit->cost == 0 && cost_limit->latency <= 0))
2442 return;
2444 /* Restrict the bits of "t" to the multiplication's mode. */
2445 t &= GET_MODE_MASK (mode);
2447 /* t == 1 can be done in zero cost. */
2448 if (t == 1)
2450 alg_out->ops = 1;
2451 alg_out->cost.cost = 0;
2452 alg_out->cost.latency = 0;
2453 alg_out->op[0] = alg_m;
2454 return;
2457 /* t == 0 sometimes has a cost. If it does and it exceeds our limit,
2458 fail now. */
2459 if (t == 0)
2461 if (MULT_COST_LESS (cost_limit, zero_cost))
2462 return;
2463 else
2465 alg_out->ops = 1;
2466 alg_out->cost.cost = zero_cost;
2467 alg_out->cost.latency = zero_cost;
2468 alg_out->op[0] = alg_zero;
2469 return;
2473 /* We'll be needing a couple extra algorithm structures now. */
2475 alg_in = alloca (sizeof (struct algorithm));
2476 best_alg = alloca (sizeof (struct algorithm));
2477 best_cost = *cost_limit;
2479 /* Compute the hash index. */
2480 hash_index = (t ^ (unsigned int) mode) % NUM_ALG_HASH_ENTRIES;
2482 /* See if we already know what to do for T. */
2483 if (alg_hash[hash_index].t == t
2484 && alg_hash[hash_index].mode == mode
2485 && alg_hash[hash_index].alg != alg_unknown)
2487 cache_alg = alg_hash[hash_index].alg;
2489 if (cache_alg == alg_impossible)
2491 /* The cache tells us that it's impossible to synthesize
2492 multiplication by T within alg_hash[hash_index].cost. */
2493 if (!CHEAPER_MULT_COST (&alg_hash[hash_index].cost, cost_limit))
2494 /* COST_LIMIT is at least as restrictive as the one
2495 recorded in the hash table, in which case we have no
2496 hope of synthesizing a multiplication. Just
2497 return. */
2498 return;
2500 /* If we get here, COST_LIMIT is less restrictive than the
2501 one recorded in the hash table, so we may be able to
2502 synthesize a multiplication. Proceed as if we didn't
2503 have the cache entry. */
2505 else
2507 if (CHEAPER_MULT_COST (cost_limit, &alg_hash[hash_index].cost))
2508 /* The cached algorithm shows that this multiplication
2509 requires more cost than COST_LIMIT. Just return. This
2510 way, we don't clobber this cache entry with
2511 alg_impossible but retain useful information. */
2512 return;
2514 cache_hit = true;
2516 switch (cache_alg)
2518 case alg_shift:
2519 goto do_alg_shift;
2521 case alg_add_t_m2:
2522 case alg_sub_t_m2:
2523 goto do_alg_addsub_t_m2;
2525 case alg_add_factor:
2526 case alg_sub_factor:
2527 goto do_alg_addsub_factor;
2529 case alg_add_t2_m:
2530 goto do_alg_add_t2_m;
2532 case alg_sub_t2_m:
2533 goto do_alg_sub_t2_m;
2535 default:
2536 gcc_unreachable ();
2541 /* If we have a group of zero bits at the low-order part of T, try
2542 multiplying by the remaining bits and then doing a shift. */
2544 if ((t & 1) == 0)
2546 do_alg_shift:
2547 m = floor_log2 (t & -t); /* m = number of low zero bits */
2548 if (m < maxm)
2550 q = t >> m;
2551 /* The function expand_shift will choose between a shift and
2552 a sequence of additions, so the observed cost is given as
2553 MIN (m * add_cost[mode], shift_cost[mode][m]). */
2554 op_cost = m * add_cost[mode];
2555 if (shift_cost[mode][m] < op_cost)
2556 op_cost = shift_cost[mode][m];
2557 new_limit.cost = best_cost.cost - op_cost;
2558 new_limit.latency = best_cost.latency - op_cost;
2559 synth_mult (alg_in, q, &new_limit, mode);
2561 alg_in->cost.cost += op_cost;
2562 alg_in->cost.latency += op_cost;
2563 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2565 struct algorithm *x;
2566 best_cost = alg_in->cost;
2567 x = alg_in, alg_in = best_alg, best_alg = x;
2568 best_alg->log[best_alg->ops] = m;
2569 best_alg->op[best_alg->ops] = alg_shift;
2572 if (cache_hit)
2573 goto done;
2576 /* If we have an odd number, add or subtract one. */
2577 if ((t & 1) != 0)
2579 unsigned HOST_WIDE_INT w;
2581 do_alg_addsub_t_m2:
2582 for (w = 1; (w & t) != 0; w <<= 1)
2584 /* If T was -1, then W will be zero after the loop. This is another
2585 case where T ends with ...111. Handling this with (T + 1) and
2586 subtract 1 produces slightly better code and results in algorithm
2587 selection much faster than treating it like the ...0111 case
2588 below. */
2589 if (w == 0
2590 || (w > 2
2591 /* Reject the case where t is 3.
2592 Thus we prefer addition in that case. */
2593 && t != 3))
2595 /* T ends with ...111. Multiply by (T + 1) and subtract 1. */
2597 op_cost = add_cost[mode];
2598 new_limit.cost = best_cost.cost - op_cost;
2599 new_limit.latency = best_cost.latency - op_cost;
2600 synth_mult (alg_in, t + 1, &new_limit, mode);
2602 alg_in->cost.cost += op_cost;
2603 alg_in->cost.latency += op_cost;
2604 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2606 struct algorithm *x;
2607 best_cost = alg_in->cost;
2608 x = alg_in, alg_in = best_alg, best_alg = x;
2609 best_alg->log[best_alg->ops] = 0;
2610 best_alg->op[best_alg->ops] = alg_sub_t_m2;
2613 else
2615 /* T ends with ...01 or ...011. Multiply by (T - 1) and add 1. */
2617 op_cost = add_cost[mode];
2618 new_limit.cost = best_cost.cost - op_cost;
2619 new_limit.latency = best_cost.latency - op_cost;
2620 synth_mult (alg_in, t - 1, &new_limit, mode);
2622 alg_in->cost.cost += op_cost;
2623 alg_in->cost.latency += op_cost;
2624 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2626 struct algorithm *x;
2627 best_cost = alg_in->cost;
2628 x = alg_in, alg_in = best_alg, best_alg = x;
2629 best_alg->log[best_alg->ops] = 0;
2630 best_alg->op[best_alg->ops] = alg_add_t_m2;
2633 if (cache_hit)
2634 goto done;
2637 /* Look for factors of t of the form
2638 t = q(2**m +- 1), 2 <= m <= floor(log2(t - 1)).
2639 If we find such a factor, we can multiply by t using an algorithm that
2640 multiplies by q, shift the result by m and add/subtract it to itself.
2642 We search for large factors first and loop down, even if large factors
2643 are less probable than small; if we find a large factor we will find a
2644 good sequence quickly, and therefore be able to prune (by decreasing
2645 COST_LIMIT) the search. */
2647 do_alg_addsub_factor:
2648 for (m = floor_log2 (t - 1); m >= 2; m--)
2650 unsigned HOST_WIDE_INT d;
2652 d = ((unsigned HOST_WIDE_INT) 1 << m) + 1;
2653 if (t % d == 0 && t > d && m < maxm
2654 && (!cache_hit || cache_alg == alg_add_factor))
2656 /* If the target has a cheap shift-and-add instruction use
2657 that in preference to a shift insn followed by an add insn.
2658 Assume that the shift-and-add is "atomic" with a latency
2659 equal to its cost, otherwise assume that on superscalar
2660 hardware the shift may be executed concurrently with the
2661 earlier steps in the algorithm. */
2662 op_cost = add_cost[mode] + shift_cost[mode][m];
2663 if (shiftadd_cost[mode][m] < op_cost)
2665 op_cost = shiftadd_cost[mode][m];
2666 op_latency = op_cost;
2668 else
2669 op_latency = add_cost[mode];
2671 new_limit.cost = best_cost.cost - op_cost;
2672 new_limit.latency = best_cost.latency - op_latency;
2673 synth_mult (alg_in, t / d, &new_limit, mode);
2675 alg_in->cost.cost += op_cost;
2676 alg_in->cost.latency += op_latency;
2677 if (alg_in->cost.latency < op_cost)
2678 alg_in->cost.latency = op_cost;
2679 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2681 struct algorithm *x;
2682 best_cost = alg_in->cost;
2683 x = alg_in, alg_in = best_alg, best_alg = x;
2684 best_alg->log[best_alg->ops] = m;
2685 best_alg->op[best_alg->ops] = alg_add_factor;
2687 /* Other factors will have been taken care of in the recursion. */
2688 break;
2691 d = ((unsigned HOST_WIDE_INT) 1 << m) - 1;
2692 if (t % d == 0 && t > d && m < maxm
2693 && (!cache_hit || cache_alg == alg_sub_factor))
2695 /* If the target has a cheap shift-and-subtract insn use
2696 that in preference to a shift insn followed by a sub insn.
2697 Assume that the shift-and-sub is "atomic" with a latency
2698 equal to it's cost, otherwise assume that on superscalar
2699 hardware the shift may be executed concurrently with the
2700 earlier steps in the algorithm. */
2701 op_cost = add_cost[mode] + shift_cost[mode][m];
2702 if (shiftsub_cost[mode][m] < op_cost)
2704 op_cost = shiftsub_cost[mode][m];
2705 op_latency = op_cost;
2707 else
2708 op_latency = add_cost[mode];
2710 new_limit.cost = best_cost.cost - op_cost;
2711 new_limit.latency = best_cost.latency - op_latency;
2712 synth_mult (alg_in, t / d, &new_limit, mode);
2714 alg_in->cost.cost += op_cost;
2715 alg_in->cost.latency += op_latency;
2716 if (alg_in->cost.latency < op_cost)
2717 alg_in->cost.latency = op_cost;
2718 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2720 struct algorithm *x;
2721 best_cost = alg_in->cost;
2722 x = alg_in, alg_in = best_alg, best_alg = x;
2723 best_alg->log[best_alg->ops] = m;
2724 best_alg->op[best_alg->ops] = alg_sub_factor;
2726 break;
2729 if (cache_hit)
2730 goto done;
2732 /* Try shift-and-add (load effective address) instructions,
2733 i.e. do a*3, a*5, a*9. */
2734 if ((t & 1) != 0)
2736 do_alg_add_t2_m:
2737 q = t - 1;
2738 q = q & -q;
2739 m = exact_log2 (q);
2740 if (m >= 0 && m < maxm)
2742 op_cost = shiftadd_cost[mode][m];
2743 new_limit.cost = best_cost.cost - op_cost;
2744 new_limit.latency = best_cost.latency - op_cost;
2745 synth_mult (alg_in, (t - 1) >> m, &new_limit, mode);
2747 alg_in->cost.cost += op_cost;
2748 alg_in->cost.latency += op_cost;
2749 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2751 struct algorithm *x;
2752 best_cost = alg_in->cost;
2753 x = alg_in, alg_in = best_alg, best_alg = x;
2754 best_alg->log[best_alg->ops] = m;
2755 best_alg->op[best_alg->ops] = alg_add_t2_m;
2758 if (cache_hit)
2759 goto done;
2761 do_alg_sub_t2_m:
2762 q = t + 1;
2763 q = q & -q;
2764 m = exact_log2 (q);
2765 if (m >= 0 && m < maxm)
2767 op_cost = shiftsub_cost[mode][m];
2768 new_limit.cost = best_cost.cost - op_cost;
2769 new_limit.latency = best_cost.latency - op_cost;
2770 synth_mult (alg_in, (t + 1) >> m, &new_limit, mode);
2772 alg_in->cost.cost += op_cost;
2773 alg_in->cost.latency += op_cost;
2774 if (CHEAPER_MULT_COST (&alg_in->cost, &best_cost))
2776 struct algorithm *x;
2777 best_cost = alg_in->cost;
2778 x = alg_in, alg_in = best_alg, best_alg = x;
2779 best_alg->log[best_alg->ops] = m;
2780 best_alg->op[best_alg->ops] = alg_sub_t2_m;
2783 if (cache_hit)
2784 goto done;
2787 done:
2788 /* If best_cost has not decreased, we have not found any algorithm. */
2789 if (!CHEAPER_MULT_COST (&best_cost, cost_limit))
2791 /* We failed to find an algorithm. Record alg_impossible for
2792 this case (that is, <T, MODE, COST_LIMIT>) so that next time
2793 we are asked to find an algorithm for T within the same or
2794 lower COST_LIMIT, we can immediately return to the
2795 caller. */
2796 alg_hash[hash_index].t = t;
2797 alg_hash[hash_index].mode = mode;
2798 alg_hash[hash_index].alg = alg_impossible;
2799 alg_hash[hash_index].cost = *cost_limit;
2800 return;
2803 /* Cache the result. */
2804 if (!cache_hit)
2806 alg_hash[hash_index].t = t;
2807 alg_hash[hash_index].mode = mode;
2808 alg_hash[hash_index].alg = best_alg->op[best_alg->ops];
2809 alg_hash[hash_index].cost.cost = best_cost.cost;
2810 alg_hash[hash_index].cost.latency = best_cost.latency;
2813 /* If we are getting a too long sequence for `struct algorithm'
2814 to record, make this search fail. */
2815 if (best_alg->ops == MAX_BITS_PER_WORD)
2816 return;
2818 /* Copy the algorithm from temporary space to the space at alg_out.
2819 We avoid using structure assignment because the majority of
2820 best_alg is normally undefined, and this is a critical function. */
2821 alg_out->ops = best_alg->ops + 1;
2822 alg_out->cost = best_cost;
2823 memcpy (alg_out->op, best_alg->op,
2824 alg_out->ops * sizeof *alg_out->op);
2825 memcpy (alg_out->log, best_alg->log,
2826 alg_out->ops * sizeof *alg_out->log);
2829 /* Find the cheapest way of multiplying a value of mode MODE by VAL.
2830 Try three variations:
2832 - a shift/add sequence based on VAL itself
2833 - a shift/add sequence based on -VAL, followed by a negation
2834 - a shift/add sequence based on VAL - 1, followed by an addition.
2836 Return true if the cheapest of these cost less than MULT_COST,
2837 describing the algorithm in *ALG and final fixup in *VARIANT. */
2839 static bool
2840 choose_mult_variant (enum machine_mode mode, HOST_WIDE_INT val,
2841 struct algorithm *alg, enum mult_variant *variant,
2842 int mult_cost)
2844 struct algorithm alg2;
2845 struct mult_cost limit;
2846 int op_cost;
2848 /* Fail quickly for impossible bounds. */
2849 if (mult_cost < 0)
2850 return false;
2852 /* Ensure that mult_cost provides a reasonable upper bound.
2853 Any constant multiplication can be performed with less
2854 than 2 * bits additions. */
2855 op_cost = 2 * GET_MODE_BITSIZE (mode) * add_cost[mode];
2856 if (mult_cost > op_cost)
2857 mult_cost = op_cost;
2859 *variant = basic_variant;
2860 limit.cost = mult_cost;
2861 limit.latency = mult_cost;
2862 synth_mult (alg, val, &limit, mode);
2864 /* This works only if the inverted value actually fits in an
2865 `unsigned int' */
2866 if (HOST_BITS_PER_INT >= GET_MODE_BITSIZE (mode))
2868 op_cost = neg_cost[mode];
2869 if (MULT_COST_LESS (&alg->cost, mult_cost))
2871 limit.cost = alg->cost.cost - op_cost;
2872 limit.latency = alg->cost.latency - op_cost;
2874 else
2876 limit.cost = mult_cost - op_cost;
2877 limit.latency = mult_cost - op_cost;
2880 synth_mult (&alg2, -val, &limit, mode);
2881 alg2.cost.cost += op_cost;
2882 alg2.cost.latency += op_cost;
2883 if (CHEAPER_MULT_COST (&alg2.cost, &alg->cost))
2884 *alg = alg2, *variant = negate_variant;
2887 /* This proves very useful for division-by-constant. */
2888 op_cost = add_cost[mode];
2889 if (MULT_COST_LESS (&alg->cost, mult_cost))
2891 limit.cost = alg->cost.cost - op_cost;
2892 limit.latency = alg->cost.latency - op_cost;
2894 else
2896 limit.cost = mult_cost - op_cost;
2897 limit.latency = mult_cost - op_cost;
2900 synth_mult (&alg2, val - 1, &limit, mode);
2901 alg2.cost.cost += op_cost;
2902 alg2.cost.latency += op_cost;
2903 if (CHEAPER_MULT_COST (&alg2.cost, &alg->cost))
2904 *alg = alg2, *variant = add_variant;
2906 return MULT_COST_LESS (&alg->cost, mult_cost);
2909 /* A subroutine of expand_mult, used for constant multiplications.
2910 Multiply OP0 by VAL in mode MODE, storing the result in TARGET if
2911 convenient. Use the shift/add sequence described by ALG and apply
2912 the final fixup specified by VARIANT. */
2914 static rtx
2915 expand_mult_const (enum machine_mode mode, rtx op0, HOST_WIDE_INT val,
2916 rtx target, const struct algorithm *alg,
2917 enum mult_variant variant)
2919 HOST_WIDE_INT val_so_far;
2920 rtx insn, accum, tem;
2921 int opno;
2922 enum machine_mode nmode;
2924 /* Avoid referencing memory over and over and invalid sharing
2925 on SUBREGs. */
2926 op0 = force_reg (mode, op0);
2928 /* ACCUM starts out either as OP0 or as a zero, depending on
2929 the first operation. */
2931 if (alg->op[0] == alg_zero)
2933 accum = copy_to_mode_reg (mode, const0_rtx);
2934 val_so_far = 0;
2936 else if (alg->op[0] == alg_m)
2938 accum = copy_to_mode_reg (mode, op0);
2939 val_so_far = 1;
2941 else
2942 gcc_unreachable ();
2944 for (opno = 1; opno < alg->ops; opno++)
2946 int log = alg->log[opno];
2947 rtx shift_subtarget = optimize ? 0 : accum;
2948 rtx add_target
2949 = (opno == alg->ops - 1 && target != 0 && variant != add_variant
2950 && !optimize)
2951 ? target : 0;
2952 rtx accum_target = optimize ? 0 : accum;
2954 switch (alg->op[opno])
2956 case alg_shift:
2957 tem = expand_shift (LSHIFT_EXPR, mode, accum,
2958 build_int_cst (NULL_TREE, log),
2959 NULL_RTX, 0);
2960 /* REG_EQUAL note will be attached to the following insn. */
2961 emit_move_insn (accum, tem);
2962 val_so_far <<= log;
2963 break;
2965 case alg_add_t_m2:
2966 tem = expand_shift (LSHIFT_EXPR, mode, op0,
2967 build_int_cst (NULL_TREE, log),
2968 NULL_RTX, 0);
2969 accum = force_operand (gen_rtx_PLUS (mode, accum, tem),
2970 add_target ? add_target : accum_target);
2971 val_so_far += (HOST_WIDE_INT) 1 << log;
2972 break;
2974 case alg_sub_t_m2:
2975 tem = expand_shift (LSHIFT_EXPR, mode, op0,
2976 build_int_cst (NULL_TREE, log),
2977 NULL_RTX, 0);
2978 accum = force_operand (gen_rtx_MINUS (mode, accum, tem),
2979 add_target ? add_target : accum_target);
2980 val_so_far -= (HOST_WIDE_INT) 1 << log;
2981 break;
2983 case alg_add_t2_m:
2984 accum = expand_shift (LSHIFT_EXPR, mode, accum,
2985 build_int_cst (NULL_TREE, log),
2986 shift_subtarget,
2988 accum = force_operand (gen_rtx_PLUS (mode, accum, op0),
2989 add_target ? add_target : accum_target);
2990 val_so_far = (val_so_far << log) + 1;
2991 break;
2993 case alg_sub_t2_m:
2994 accum = expand_shift (LSHIFT_EXPR, mode, accum,
2995 build_int_cst (NULL_TREE, log),
2996 shift_subtarget, 0);
2997 accum = force_operand (gen_rtx_MINUS (mode, accum, op0),
2998 add_target ? add_target : accum_target);
2999 val_so_far = (val_so_far << log) - 1;
3000 break;
3002 case alg_add_factor:
3003 tem = expand_shift (LSHIFT_EXPR, mode, accum,
3004 build_int_cst (NULL_TREE, log),
3005 NULL_RTX, 0);
3006 accum = force_operand (gen_rtx_PLUS (mode, accum, tem),
3007 add_target ? add_target : accum_target);
3008 val_so_far += val_so_far << log;
3009 break;
3011 case alg_sub_factor:
3012 tem = expand_shift (LSHIFT_EXPR, mode, accum,
3013 build_int_cst (NULL_TREE, log),
3014 NULL_RTX, 0);
3015 accum = force_operand (gen_rtx_MINUS (mode, tem, accum),
3016 (add_target
3017 ? add_target : (optimize ? 0 : tem)));
3018 val_so_far = (val_so_far << log) - val_so_far;
3019 break;
3021 default:
3022 gcc_unreachable ();
3025 /* Write a REG_EQUAL note on the last insn so that we can cse
3026 multiplication sequences. Note that if ACCUM is a SUBREG,
3027 we've set the inner register and must properly indicate
3028 that. */
3030 tem = op0, nmode = mode;
3031 if (GET_CODE (accum) == SUBREG)
3033 nmode = GET_MODE (SUBREG_REG (accum));
3034 tem = gen_lowpart (nmode, op0);
3037 insn = get_last_insn ();
3038 set_unique_reg_note (insn, REG_EQUAL,
3039 gen_rtx_MULT (nmode, tem,
3040 GEN_INT (val_so_far)));
3043 if (variant == negate_variant)
3045 val_so_far = -val_so_far;
3046 accum = expand_unop (mode, neg_optab, accum, target, 0);
3048 else if (variant == add_variant)
3050 val_so_far = val_so_far + 1;
3051 accum = force_operand (gen_rtx_PLUS (mode, accum, op0), target);
3054 /* Compare only the bits of val and val_so_far that are significant
3055 in the result mode, to avoid sign-/zero-extension confusion. */
3056 val &= GET_MODE_MASK (mode);
3057 val_so_far &= GET_MODE_MASK (mode);
3058 gcc_assert (val == val_so_far);
3060 return accum;
3063 /* Perform a multiplication and return an rtx for the result.
3064 MODE is mode of value; OP0 and OP1 are what to multiply (rtx's);
3065 TARGET is a suggestion for where to store the result (an rtx).
3067 We check specially for a constant integer as OP1.
3068 If you want this check for OP0 as well, then before calling
3069 you should swap the two operands if OP0 would be constant. */
3072 expand_mult (enum machine_mode mode, rtx op0, rtx op1, rtx target,
3073 int unsignedp)
3075 enum mult_variant variant;
3076 struct algorithm algorithm;
3077 int max_cost;
3079 /* Handling const0_rtx here allows us to use zero as a rogue value for
3080 coeff below. */
3081 if (op1 == const0_rtx)
3082 return const0_rtx;
3083 if (op1 == const1_rtx)
3084 return op0;
3085 if (op1 == constm1_rtx)
3086 return expand_unop (mode,
3087 GET_MODE_CLASS (mode) == MODE_INT
3088 && !unsignedp && flag_trapv
3089 ? negv_optab : neg_optab,
3090 op0, target, 0);
3092 /* These are the operations that are potentially turned into a sequence
3093 of shifts and additions. */
3094 if (SCALAR_INT_MODE_P (mode)
3095 && (unsignedp || !flag_trapv))
3097 HOST_WIDE_INT coeff = 0;
3098 rtx fake_reg = gen_raw_REG (mode, LAST_VIRTUAL_REGISTER + 1);
3100 /* synth_mult does an `unsigned int' multiply. As long as the mode is
3101 less than or equal in size to `unsigned int' this doesn't matter.
3102 If the mode is larger than `unsigned int', then synth_mult works
3103 only if the constant value exactly fits in an `unsigned int' without
3104 any truncation. This means that multiplying by negative values does
3105 not work; results are off by 2^32 on a 32 bit machine. */
3107 if (GET_CODE (op1) == CONST_INT)
3109 /* Attempt to handle multiplication of DImode values by negative
3110 coefficients, by performing the multiplication by a positive
3111 multiplier and then inverting the result. */
3112 if (INTVAL (op1) < 0
3113 && GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT)
3115 /* Its safe to use -INTVAL (op1) even for INT_MIN, as the
3116 result is interpreted as an unsigned coefficient.
3117 Exclude cost of op0 from max_cost to match the cost
3118 calculation of the synth_mult. */
3119 max_cost = rtx_cost (gen_rtx_MULT (mode, fake_reg, op1), SET)
3120 - neg_cost[mode];
3121 if (max_cost > 0
3122 && choose_mult_variant (mode, -INTVAL (op1), &algorithm,
3123 &variant, max_cost))
3125 rtx temp = expand_mult_const (mode, op0, -INTVAL (op1),
3126 NULL_RTX, &algorithm,
3127 variant);
3128 return expand_unop (mode, neg_optab, temp, target, 0);
3131 else coeff = INTVAL (op1);
3133 else if (GET_CODE (op1) == CONST_DOUBLE)
3135 /* If we are multiplying in DImode, it may still be a win
3136 to try to work with shifts and adds. */
3137 if (CONST_DOUBLE_HIGH (op1) == 0
3138 && CONST_DOUBLE_LOW (op1) > 0)
3139 coeff = CONST_DOUBLE_LOW (op1);
3140 else if (CONST_DOUBLE_LOW (op1) == 0
3141 && EXACT_POWER_OF_2_OR_ZERO_P (CONST_DOUBLE_HIGH (op1)))
3143 int shift = floor_log2 (CONST_DOUBLE_HIGH (op1))
3144 + HOST_BITS_PER_WIDE_INT;
3145 return expand_shift (LSHIFT_EXPR, mode, op0,
3146 build_int_cst (NULL_TREE, shift),
3147 target, unsignedp);
3151 /* We used to test optimize here, on the grounds that it's better to
3152 produce a smaller program when -O is not used. But this causes
3153 such a terrible slowdown sometimes that it seems better to always
3154 use synth_mult. */
3155 if (coeff != 0)
3157 /* Special case powers of two. */
3158 if (EXACT_POWER_OF_2_OR_ZERO_P (coeff))
3159 return expand_shift (LSHIFT_EXPR, mode, op0,
3160 build_int_cst (NULL_TREE, floor_log2 (coeff)),
3161 target, unsignedp);
3163 /* Exclude cost of op0 from max_cost to match the cost
3164 calculation of the synth_mult. */
3165 max_cost = rtx_cost (gen_rtx_MULT (mode, fake_reg, op1), SET);
3166 if (choose_mult_variant (mode, coeff, &algorithm, &variant,
3167 max_cost))
3168 return expand_mult_const (mode, op0, coeff, target,
3169 &algorithm, variant);
3173 if (GET_CODE (op0) == CONST_DOUBLE)
3175 rtx temp = op0;
3176 op0 = op1;
3177 op1 = temp;
3180 /* Expand x*2.0 as x+x. */
3181 if (GET_CODE (op1) == CONST_DOUBLE
3182 && SCALAR_FLOAT_MODE_P (mode))
3184 REAL_VALUE_TYPE d;
3185 REAL_VALUE_FROM_CONST_DOUBLE (d, op1);
3187 if (REAL_VALUES_EQUAL (d, dconst2))
3189 op0 = force_reg (GET_MODE (op0), op0);
3190 return expand_binop (mode, add_optab, op0, op0,
3191 target, unsignedp, OPTAB_LIB_WIDEN);
3195 /* This used to use umul_optab if unsigned, but for non-widening multiply
3196 there is no difference between signed and unsigned. */
3197 op0 = expand_binop (mode,
3198 ! unsignedp
3199 && flag_trapv && (GET_MODE_CLASS(mode) == MODE_INT)
3200 ? smulv_optab : smul_optab,
3201 op0, op1, target, unsignedp, OPTAB_LIB_WIDEN);
3202 gcc_assert (op0);
3203 return op0;
3206 /* Return the smallest n such that 2**n >= X. */
3209 ceil_log2 (unsigned HOST_WIDE_INT x)
3211 return floor_log2 (x - 1) + 1;
3214 /* Choose a minimal N + 1 bit approximation to 1/D that can be used to
3215 replace division by D, and put the least significant N bits of the result
3216 in *MULTIPLIER_PTR and return the most significant bit.
3218 The width of operations is N (should be <= HOST_BITS_PER_WIDE_INT), the
3219 needed precision is in PRECISION (should be <= N).
3221 PRECISION should be as small as possible so this function can choose
3222 multiplier more freely.
3224 The rounded-up logarithm of D is placed in *lgup_ptr. A shift count that
3225 is to be used for a final right shift is placed in *POST_SHIFT_PTR.
3227 Using this function, x/D will be equal to (x * m) >> (*POST_SHIFT_PTR),
3228 where m is the full HOST_BITS_PER_WIDE_INT + 1 bit multiplier. */
3230 static
3231 unsigned HOST_WIDE_INT
3232 choose_multiplier (unsigned HOST_WIDE_INT d, int n, int precision,
3233 rtx *multiplier_ptr, int *post_shift_ptr, int *lgup_ptr)
3235 HOST_WIDE_INT mhigh_hi, mlow_hi;
3236 unsigned HOST_WIDE_INT mhigh_lo, mlow_lo;
3237 int lgup, post_shift;
3238 int pow, pow2;
3239 unsigned HOST_WIDE_INT nl, dummy1;
3240 HOST_WIDE_INT nh, dummy2;
3242 /* lgup = ceil(log2(divisor)); */
3243 lgup = ceil_log2 (d);
3245 gcc_assert (lgup <= n);
3247 pow = n + lgup;
3248 pow2 = n + lgup - precision;
3250 /* We could handle this with some effort, but this case is much
3251 better handled directly with a scc insn, so rely on caller using
3252 that. */
3253 gcc_assert (pow != 2 * HOST_BITS_PER_WIDE_INT);
3255 /* mlow = 2^(N + lgup)/d */
3256 if (pow >= HOST_BITS_PER_WIDE_INT)
3258 nh = (HOST_WIDE_INT) 1 << (pow - HOST_BITS_PER_WIDE_INT);
3259 nl = 0;
3261 else
3263 nh = 0;
3264 nl = (unsigned HOST_WIDE_INT) 1 << pow;
3266 div_and_round_double (TRUNC_DIV_EXPR, 1, nl, nh, d, (HOST_WIDE_INT) 0,
3267 &mlow_lo, &mlow_hi, &dummy1, &dummy2);
3269 /* mhigh = (2^(N + lgup) + 2^N + lgup - precision)/d */
3270 if (pow2 >= HOST_BITS_PER_WIDE_INT)
3271 nh |= (HOST_WIDE_INT) 1 << (pow2 - HOST_BITS_PER_WIDE_INT);
3272 else
3273 nl |= (unsigned HOST_WIDE_INT) 1 << pow2;
3274 div_and_round_double (TRUNC_DIV_EXPR, 1, nl, nh, d, (HOST_WIDE_INT) 0,
3275 &mhigh_lo, &mhigh_hi, &dummy1, &dummy2);
3277 gcc_assert (!mhigh_hi || nh - d < d);
3278 gcc_assert (mhigh_hi <= 1 && mlow_hi <= 1);
3279 /* Assert that mlow < mhigh. */
3280 gcc_assert (mlow_hi < mhigh_hi
3281 || (mlow_hi == mhigh_hi && mlow_lo < mhigh_lo));
3283 /* If precision == N, then mlow, mhigh exceed 2^N
3284 (but they do not exceed 2^(N+1)). */
3286 /* Reduce to lowest terms. */
3287 for (post_shift = lgup; post_shift > 0; post_shift--)
3289 unsigned HOST_WIDE_INT ml_lo = (mlow_hi << (HOST_BITS_PER_WIDE_INT - 1)) | (mlow_lo >> 1);
3290 unsigned HOST_WIDE_INT mh_lo = (mhigh_hi << (HOST_BITS_PER_WIDE_INT - 1)) | (mhigh_lo >> 1);
3291 if (ml_lo >= mh_lo)
3292 break;
3294 mlow_hi = 0;
3295 mlow_lo = ml_lo;
3296 mhigh_hi = 0;
3297 mhigh_lo = mh_lo;
3300 *post_shift_ptr = post_shift;
3301 *lgup_ptr = lgup;
3302 if (n < HOST_BITS_PER_WIDE_INT)
3304 unsigned HOST_WIDE_INT mask = ((unsigned HOST_WIDE_INT) 1 << n) - 1;
3305 *multiplier_ptr = GEN_INT (mhigh_lo & mask);
3306 return mhigh_lo >= mask;
3308 else
3310 *multiplier_ptr = GEN_INT (mhigh_lo);
3311 return mhigh_hi;
3315 /* Compute the inverse of X mod 2**n, i.e., find Y such that X * Y is
3316 congruent to 1 (mod 2**N). */
3318 static unsigned HOST_WIDE_INT
3319 invert_mod2n (unsigned HOST_WIDE_INT x, int n)
3321 /* Solve x*y == 1 (mod 2^n), where x is odd. Return y. */
3323 /* The algorithm notes that the choice y = x satisfies
3324 x*y == 1 mod 2^3, since x is assumed odd.
3325 Each iteration doubles the number of bits of significance in y. */
3327 unsigned HOST_WIDE_INT mask;
3328 unsigned HOST_WIDE_INT y = x;
3329 int nbit = 3;
3331 mask = (n == HOST_BITS_PER_WIDE_INT
3332 ? ~(unsigned HOST_WIDE_INT) 0
3333 : ((unsigned HOST_WIDE_INT) 1 << n) - 1);
3335 while (nbit < n)
3337 y = y * (2 - x*y) & mask; /* Modulo 2^N */
3338 nbit *= 2;
3340 return y;
3343 /* Emit code to adjust ADJ_OPERAND after multiplication of wrong signedness
3344 flavor of OP0 and OP1. ADJ_OPERAND is already the high half of the
3345 product OP0 x OP1. If UNSIGNEDP is nonzero, adjust the signed product
3346 to become unsigned, if UNSIGNEDP is zero, adjust the unsigned product to
3347 become signed.
3349 The result is put in TARGET if that is convenient.
3351 MODE is the mode of operation. */
3354 expand_mult_highpart_adjust (enum machine_mode mode, rtx adj_operand, rtx op0,
3355 rtx op1, rtx target, int unsignedp)
3357 rtx tem;
3358 enum rtx_code adj_code = unsignedp ? PLUS : MINUS;
3360 tem = expand_shift (RSHIFT_EXPR, mode, op0,
3361 build_int_cst (NULL_TREE, GET_MODE_BITSIZE (mode) - 1),
3362 NULL_RTX, 0);
3363 tem = expand_and (mode, tem, op1, NULL_RTX);
3364 adj_operand
3365 = force_operand (gen_rtx_fmt_ee (adj_code, mode, adj_operand, tem),
3366 adj_operand);
3368 tem = expand_shift (RSHIFT_EXPR, mode, op1,
3369 build_int_cst (NULL_TREE, GET_MODE_BITSIZE (mode) - 1),
3370 NULL_RTX, 0);
3371 tem = expand_and (mode, tem, op0, NULL_RTX);
3372 target = force_operand (gen_rtx_fmt_ee (adj_code, mode, adj_operand, tem),
3373 target);
3375 return target;
3378 /* Subroutine of expand_mult_highpart. Return the MODE high part of OP. */
3380 static rtx
3381 extract_high_half (enum machine_mode mode, rtx op)
3383 enum machine_mode wider_mode;
3385 if (mode == word_mode)
3386 return gen_highpart (mode, op);
3388 gcc_assert (!SCALAR_FLOAT_MODE_P (mode));
3390 wider_mode = GET_MODE_WIDER_MODE (mode);
3391 op = expand_shift (RSHIFT_EXPR, wider_mode, op,
3392 build_int_cst (NULL_TREE, GET_MODE_BITSIZE (mode)), 0, 1);
3393 return convert_modes (mode, wider_mode, op, 0);
3396 /* Like expand_mult_highpart, but only consider using a multiplication
3397 optab. OP1 is an rtx for the constant operand. */
3399 static rtx
3400 expand_mult_highpart_optab (enum machine_mode mode, rtx op0, rtx op1,
3401 rtx target, int unsignedp, int max_cost)
3403 rtx narrow_op1 = gen_int_mode (INTVAL (op1), mode);
3404 enum machine_mode wider_mode;
3405 optab moptab;
3406 rtx tem;
3407 int size;
3409 gcc_assert (!SCALAR_FLOAT_MODE_P (mode));
3411 wider_mode = GET_MODE_WIDER_MODE (mode);
3412 size = GET_MODE_BITSIZE (mode);
3414 /* Firstly, try using a multiplication insn that only generates the needed
3415 high part of the product, and in the sign flavor of unsignedp. */
3416 if (mul_highpart_cost[mode] < max_cost)
3418 moptab = unsignedp ? umul_highpart_optab : smul_highpart_optab;
3419 tem = expand_binop (mode, moptab, op0, narrow_op1, target,
3420 unsignedp, OPTAB_DIRECT);
3421 if (tem)
3422 return tem;
3425 /* Secondly, same as above, but use sign flavor opposite of unsignedp.
3426 Need to adjust the result after the multiplication. */
3427 if (size - 1 < BITS_PER_WORD
3428 && (mul_highpart_cost[mode] + 2 * shift_cost[mode][size-1]
3429 + 4 * add_cost[mode] < max_cost))
3431 moptab = unsignedp ? smul_highpart_optab : umul_highpart_optab;
3432 tem = expand_binop (mode, moptab, op0, narrow_op1, target,
3433 unsignedp, OPTAB_DIRECT);
3434 if (tem)
3435 /* We used the wrong signedness. Adjust the result. */
3436 return expand_mult_highpart_adjust (mode, tem, op0, narrow_op1,
3437 tem, unsignedp);
3440 /* Try widening multiplication. */
3441 moptab = unsignedp ? umul_widen_optab : smul_widen_optab;
3442 if (optab_handler (moptab, wider_mode)->insn_code != CODE_FOR_nothing
3443 && mul_widen_cost[wider_mode] < max_cost)
3445 tem = expand_binop (wider_mode, moptab, op0, narrow_op1, 0,
3446 unsignedp, OPTAB_WIDEN);
3447 if (tem)
3448 return extract_high_half (mode, tem);
3451 /* Try widening the mode and perform a non-widening multiplication. */
3452 if (optab_handler (smul_optab, wider_mode)->insn_code != CODE_FOR_nothing
3453 && size - 1 < BITS_PER_WORD
3454 && mul_cost[wider_mode] + shift_cost[mode][size-1] < max_cost)
3456 rtx insns, wop0, wop1;
3458 /* We need to widen the operands, for example to ensure the
3459 constant multiplier is correctly sign or zero extended.
3460 Use a sequence to clean-up any instructions emitted by
3461 the conversions if things don't work out. */
3462 start_sequence ();
3463 wop0 = convert_modes (wider_mode, mode, op0, unsignedp);
3464 wop1 = convert_modes (wider_mode, mode, op1, unsignedp);
3465 tem = expand_binop (wider_mode, smul_optab, wop0, wop1, 0,
3466 unsignedp, OPTAB_WIDEN);
3467 insns = get_insns ();
3468 end_sequence ();
3470 if (tem)
3472 emit_insn (insns);
3473 return extract_high_half (mode, tem);
3477 /* Try widening multiplication of opposite signedness, and adjust. */
3478 moptab = unsignedp ? smul_widen_optab : umul_widen_optab;
3479 if (optab_handler (moptab, wider_mode)->insn_code != CODE_FOR_nothing
3480 && size - 1 < BITS_PER_WORD
3481 && (mul_widen_cost[wider_mode] + 2 * shift_cost[mode][size-1]
3482 + 4 * add_cost[mode] < max_cost))
3484 tem = expand_binop (wider_mode, moptab, op0, narrow_op1,
3485 NULL_RTX, ! unsignedp, OPTAB_WIDEN);
3486 if (tem != 0)
3488 tem = extract_high_half (mode, tem);
3489 /* We used the wrong signedness. Adjust the result. */
3490 return expand_mult_highpart_adjust (mode, tem, op0, narrow_op1,
3491 target, unsignedp);
3495 return 0;
3498 /* Emit code to multiply OP0 and OP1 (where OP1 is an integer constant),
3499 putting the high half of the result in TARGET if that is convenient,
3500 and return where the result is. If the operation can not be performed,
3501 0 is returned.
3503 MODE is the mode of operation and result.
3505 UNSIGNEDP nonzero means unsigned multiply.
3507 MAX_COST is the total allowed cost for the expanded RTL. */
3509 static rtx
3510 expand_mult_highpart (enum machine_mode mode, rtx op0, rtx op1,
3511 rtx target, int unsignedp, int max_cost)
3513 enum machine_mode wider_mode = GET_MODE_WIDER_MODE (mode);
3514 unsigned HOST_WIDE_INT cnst1;
3515 int extra_cost;
3516 bool sign_adjust = false;
3517 enum mult_variant variant;
3518 struct algorithm alg;
3519 rtx tem;
3521 gcc_assert (!SCALAR_FLOAT_MODE_P (mode));
3522 /* We can't support modes wider than HOST_BITS_PER_INT. */
3523 gcc_assert (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT);
3525 cnst1 = INTVAL (op1) & GET_MODE_MASK (mode);
3527 /* We can't optimize modes wider than BITS_PER_WORD.
3528 ??? We might be able to perform double-word arithmetic if
3529 mode == word_mode, however all the cost calculations in
3530 synth_mult etc. assume single-word operations. */
3531 if (GET_MODE_BITSIZE (wider_mode) > BITS_PER_WORD)
3532 return expand_mult_highpart_optab (mode, op0, op1, target,
3533 unsignedp, max_cost);
3535 extra_cost = shift_cost[mode][GET_MODE_BITSIZE (mode) - 1];
3537 /* Check whether we try to multiply by a negative constant. */
3538 if (!unsignedp && ((cnst1 >> (GET_MODE_BITSIZE (mode) - 1)) & 1))
3540 sign_adjust = true;
3541 extra_cost += add_cost[mode];
3544 /* See whether shift/add multiplication is cheap enough. */
3545 if (choose_mult_variant (wider_mode, cnst1, &alg, &variant,
3546 max_cost - extra_cost))
3548 /* See whether the specialized multiplication optabs are
3549 cheaper than the shift/add version. */
3550 tem = expand_mult_highpart_optab (mode, op0, op1, target, unsignedp,
3551 alg.cost.cost + extra_cost);
3552 if (tem)
3553 return tem;
3555 tem = convert_to_mode (wider_mode, op0, unsignedp);
3556 tem = expand_mult_const (wider_mode, tem, cnst1, 0, &alg, variant);
3557 tem = extract_high_half (mode, tem);
3559 /* Adjust result for signedness. */
3560 if (sign_adjust)
3561 tem = force_operand (gen_rtx_MINUS (mode, tem, op0), tem);
3563 return tem;
3565 return expand_mult_highpart_optab (mode, op0, op1, target,
3566 unsignedp, max_cost);
3570 /* Expand signed modulus of OP0 by a power of two D in mode MODE. */
3572 static rtx
3573 expand_smod_pow2 (enum machine_mode mode, rtx op0, HOST_WIDE_INT d)
3575 unsigned HOST_WIDE_INT masklow, maskhigh;
3576 rtx result, temp, shift, label;
3577 int logd;
3579 logd = floor_log2 (d);
3580 result = gen_reg_rtx (mode);
3582 /* Avoid conditional branches when they're expensive. */
3583 if (BRANCH_COST >= 2
3584 && !optimize_size)
3586 rtx signmask = emit_store_flag (result, LT, op0, const0_rtx,
3587 mode, 0, -1);
3588 if (signmask)
3590 signmask = force_reg (mode, signmask);
3591 masklow = ((HOST_WIDE_INT) 1 << logd) - 1;
3592 shift = GEN_INT (GET_MODE_BITSIZE (mode) - logd);
3594 /* Use the rtx_cost of a LSHIFTRT instruction to determine
3595 which instruction sequence to use. If logical right shifts
3596 are expensive the use 2 XORs, 2 SUBs and an AND, otherwise
3597 use a LSHIFTRT, 1 ADD, 1 SUB and an AND. */
3599 temp = gen_rtx_LSHIFTRT (mode, result, shift);
3600 if (optab_handler (lshr_optab, mode)->insn_code == CODE_FOR_nothing
3601 || rtx_cost (temp, SET) > COSTS_N_INSNS (2))
3603 temp = expand_binop (mode, xor_optab, op0, signmask,
3604 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3605 temp = expand_binop (mode, sub_optab, temp, signmask,
3606 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3607 temp = expand_binop (mode, and_optab, temp, GEN_INT (masklow),
3608 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3609 temp = expand_binop (mode, xor_optab, temp, signmask,
3610 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3611 temp = expand_binop (mode, sub_optab, temp, signmask,
3612 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3614 else
3616 signmask = expand_binop (mode, lshr_optab, signmask, shift,
3617 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3618 signmask = force_reg (mode, signmask);
3620 temp = expand_binop (mode, add_optab, op0, signmask,
3621 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3622 temp = expand_binop (mode, and_optab, temp, GEN_INT (masklow),
3623 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3624 temp = expand_binop (mode, sub_optab, temp, signmask,
3625 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3627 return temp;
3631 /* Mask contains the mode's signbit and the significant bits of the
3632 modulus. By including the signbit in the operation, many targets
3633 can avoid an explicit compare operation in the following comparison
3634 against zero. */
3636 masklow = ((HOST_WIDE_INT) 1 << logd) - 1;
3637 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
3639 masklow |= (HOST_WIDE_INT) -1 << (GET_MODE_BITSIZE (mode) - 1);
3640 maskhigh = -1;
3642 else
3643 maskhigh = (HOST_WIDE_INT) -1
3644 << (GET_MODE_BITSIZE (mode) - HOST_BITS_PER_WIDE_INT - 1);
3646 temp = expand_binop (mode, and_optab, op0,
3647 immed_double_const (masklow, maskhigh, mode),
3648 result, 1, OPTAB_LIB_WIDEN);
3649 if (temp != result)
3650 emit_move_insn (result, temp);
3652 label = gen_label_rtx ();
3653 do_cmp_and_jump (result, const0_rtx, GE, mode, label);
3655 temp = expand_binop (mode, sub_optab, result, const1_rtx, result,
3656 0, OPTAB_LIB_WIDEN);
3657 masklow = (HOST_WIDE_INT) -1 << logd;
3658 maskhigh = -1;
3659 temp = expand_binop (mode, ior_optab, temp,
3660 immed_double_const (masklow, maskhigh, mode),
3661 result, 1, OPTAB_LIB_WIDEN);
3662 temp = expand_binop (mode, add_optab, temp, const1_rtx, result,
3663 0, OPTAB_LIB_WIDEN);
3664 if (temp != result)
3665 emit_move_insn (result, temp);
3666 emit_label (label);
3667 return result;
3670 /* Expand signed division of OP0 by a power of two D in mode MODE.
3671 This routine is only called for positive values of D. */
3673 static rtx
3674 expand_sdiv_pow2 (enum machine_mode mode, rtx op0, HOST_WIDE_INT d)
3676 rtx temp, label;
3677 tree shift;
3678 int logd;
3680 logd = floor_log2 (d);
3681 shift = build_int_cst (NULL_TREE, logd);
3683 if (d == 2 && BRANCH_COST >= 1)
3685 temp = gen_reg_rtx (mode);
3686 temp = emit_store_flag (temp, LT, op0, const0_rtx, mode, 0, 1);
3687 temp = expand_binop (mode, add_optab, temp, op0, NULL_RTX,
3688 0, OPTAB_LIB_WIDEN);
3689 return expand_shift (RSHIFT_EXPR, mode, temp, shift, NULL_RTX, 0);
3692 #ifdef HAVE_conditional_move
3693 if (BRANCH_COST >= 2)
3695 rtx temp2;
3697 /* ??? emit_conditional_move forces a stack adjustment via
3698 compare_from_rtx so, if the sequence is discarded, it will
3699 be lost. Do it now instead. */
3700 do_pending_stack_adjust ();
3702 start_sequence ();
3703 temp2 = copy_to_mode_reg (mode, op0);
3704 temp = expand_binop (mode, add_optab, temp2, GEN_INT (d-1),
3705 NULL_RTX, 0, OPTAB_LIB_WIDEN);
3706 temp = force_reg (mode, temp);
3708 /* Construct "temp2 = (temp2 < 0) ? temp : temp2". */
3709 temp2 = emit_conditional_move (temp2, LT, temp2, const0_rtx,
3710 mode, temp, temp2, mode, 0);
3711 if (temp2)
3713 rtx seq = get_insns ();
3714 end_sequence ();
3715 emit_insn (seq);
3716 return expand_shift (RSHIFT_EXPR, mode, temp2, shift, NULL_RTX, 0);
3718 end_sequence ();
3720 #endif
3722 if (BRANCH_COST >= 2)
3724 int ushift = GET_MODE_BITSIZE (mode) - logd;
3726 temp = gen_reg_rtx (mode);
3727 temp = emit_store_flag (temp, LT, op0, const0_rtx, mode, 0, -1);
3728 if (shift_cost[mode][ushift] > COSTS_N_INSNS (1))
3729 temp = expand_binop (mode, and_optab, temp, GEN_INT (d - 1),
3730 NULL_RTX, 0, OPTAB_LIB_WIDEN);
3731 else
3732 temp = expand_shift (RSHIFT_EXPR, mode, temp,
3733 build_int_cst (NULL_TREE, ushift),
3734 NULL_RTX, 1);
3735 temp = expand_binop (mode, add_optab, temp, op0, NULL_RTX,
3736 0, OPTAB_LIB_WIDEN);
3737 return expand_shift (RSHIFT_EXPR, mode, temp, shift, NULL_RTX, 0);
3740 label = gen_label_rtx ();
3741 temp = copy_to_mode_reg (mode, op0);
3742 do_cmp_and_jump (temp, const0_rtx, GE, mode, label);
3743 expand_inc (temp, GEN_INT (d - 1));
3744 emit_label (label);
3745 return expand_shift (RSHIFT_EXPR, mode, temp, shift, NULL_RTX, 0);
3748 /* Emit the code to divide OP0 by OP1, putting the result in TARGET
3749 if that is convenient, and returning where the result is.
3750 You may request either the quotient or the remainder as the result;
3751 specify REM_FLAG nonzero to get the remainder.
3753 CODE is the expression code for which kind of division this is;
3754 it controls how rounding is done. MODE is the machine mode to use.
3755 UNSIGNEDP nonzero means do unsigned division. */
3757 /* ??? For CEIL_MOD_EXPR, can compute incorrect remainder with ANDI
3758 and then correct it by or'ing in missing high bits
3759 if result of ANDI is nonzero.
3760 For ROUND_MOD_EXPR, can use ANDI and then sign-extend the result.
3761 This could optimize to a bfexts instruction.
3762 But C doesn't use these operations, so their optimizations are
3763 left for later. */
3764 /* ??? For modulo, we don't actually need the highpart of the first product,
3765 the low part will do nicely. And for small divisors, the second multiply
3766 can also be a low-part only multiply or even be completely left out.
3767 E.g. to calculate the remainder of a division by 3 with a 32 bit
3768 multiply, multiply with 0x55555556 and extract the upper two bits;
3769 the result is exact for inputs up to 0x1fffffff.
3770 The input range can be reduced by using cross-sum rules.
3771 For odd divisors >= 3, the following table gives right shift counts
3772 so that if a number is shifted by an integer multiple of the given
3773 amount, the remainder stays the same:
3774 2, 4, 3, 6, 10, 12, 4, 8, 18, 6, 11, 20, 18, 0, 5, 10, 12, 0, 12, 20,
3775 14, 12, 23, 21, 8, 0, 20, 18, 0, 0, 6, 12, 0, 22, 0, 18, 20, 30, 0, 0,
3776 0, 8, 0, 11, 12, 10, 36, 0, 30, 0, 0, 12, 0, 0, 0, 0, 44, 12, 24, 0,
3777 20, 0, 7, 14, 0, 18, 36, 0, 0, 46, 60, 0, 42, 0, 15, 24, 20, 0, 0, 33,
3778 0, 20, 0, 0, 18, 0, 60, 0, 0, 0, 0, 0, 40, 18, 0, 0, 12
3780 Cross-sum rules for even numbers can be derived by leaving as many bits
3781 to the right alone as the divisor has zeros to the right.
3782 E.g. if x is an unsigned 32 bit number:
3783 (x mod 12) == (((x & 1023) + ((x >> 8) & ~3)) * 0x15555558 >> 2 * 3) >> 28
3787 expand_divmod (int rem_flag, enum tree_code code, enum machine_mode mode,
3788 rtx op0, rtx op1, rtx target, int unsignedp)
3790 enum machine_mode compute_mode;
3791 rtx tquotient;
3792 rtx quotient = 0, remainder = 0;
3793 rtx last;
3794 int size;
3795 rtx insn, set;
3796 optab optab1, optab2;
3797 int op1_is_constant, op1_is_pow2 = 0;
3798 int max_cost, extra_cost;
3799 static HOST_WIDE_INT last_div_const = 0;
3800 static HOST_WIDE_INT ext_op1;
3802 op1_is_constant = GET_CODE (op1) == CONST_INT;
3803 if (op1_is_constant)
3805 ext_op1 = INTVAL (op1);
3806 if (unsignedp)
3807 ext_op1 &= GET_MODE_MASK (mode);
3808 op1_is_pow2 = ((EXACT_POWER_OF_2_OR_ZERO_P (ext_op1)
3809 || (! unsignedp && EXACT_POWER_OF_2_OR_ZERO_P (-ext_op1))));
3813 This is the structure of expand_divmod:
3815 First comes code to fix up the operands so we can perform the operations
3816 correctly and efficiently.
3818 Second comes a switch statement with code specific for each rounding mode.
3819 For some special operands this code emits all RTL for the desired
3820 operation, for other cases, it generates only a quotient and stores it in
3821 QUOTIENT. The case for trunc division/remainder might leave quotient = 0,
3822 to indicate that it has not done anything.
3824 Last comes code that finishes the operation. If QUOTIENT is set and
3825 REM_FLAG is set, the remainder is computed as OP0 - QUOTIENT * OP1. If
3826 QUOTIENT is not set, it is computed using trunc rounding.
3828 We try to generate special code for division and remainder when OP1 is a
3829 constant. If |OP1| = 2**n we can use shifts and some other fast
3830 operations. For other values of OP1, we compute a carefully selected
3831 fixed-point approximation m = 1/OP1, and generate code that multiplies OP0
3832 by m.
3834 In all cases but EXACT_DIV_EXPR, this multiplication requires the upper
3835 half of the product. Different strategies for generating the product are
3836 implemented in expand_mult_highpart.
3838 If what we actually want is the remainder, we generate that by another
3839 by-constant multiplication and a subtraction. */
3841 /* We shouldn't be called with OP1 == const1_rtx, but some of the
3842 code below will malfunction if we are, so check here and handle
3843 the special case if so. */
3844 if (op1 == const1_rtx)
3845 return rem_flag ? const0_rtx : op0;
3847 /* When dividing by -1, we could get an overflow.
3848 negv_optab can handle overflows. */
3849 if (! unsignedp && op1 == constm1_rtx)
3851 if (rem_flag)
3852 return const0_rtx;
3853 return expand_unop (mode, flag_trapv && GET_MODE_CLASS(mode) == MODE_INT
3854 ? negv_optab : neg_optab, op0, target, 0);
3857 if (target
3858 /* Don't use the function value register as a target
3859 since we have to read it as well as write it,
3860 and function-inlining gets confused by this. */
3861 && ((REG_P (target) && REG_FUNCTION_VALUE_P (target))
3862 /* Don't clobber an operand while doing a multi-step calculation. */
3863 || ((rem_flag || op1_is_constant)
3864 && (reg_mentioned_p (target, op0)
3865 || (MEM_P (op0) && MEM_P (target))))
3866 || reg_mentioned_p (target, op1)
3867 || (MEM_P (op1) && MEM_P (target))))
3868 target = 0;
3870 /* Get the mode in which to perform this computation. Normally it will
3871 be MODE, but sometimes we can't do the desired operation in MODE.
3872 If so, pick a wider mode in which we can do the operation. Convert
3873 to that mode at the start to avoid repeated conversions.
3875 First see what operations we need. These depend on the expression
3876 we are evaluating. (We assume that divxx3 insns exist under the
3877 same conditions that modxx3 insns and that these insns don't normally
3878 fail. If these assumptions are not correct, we may generate less
3879 efficient code in some cases.)
3881 Then see if we find a mode in which we can open-code that operation
3882 (either a division, modulus, or shift). Finally, check for the smallest
3883 mode for which we can do the operation with a library call. */
3885 /* We might want to refine this now that we have division-by-constant
3886 optimization. Since expand_mult_highpart tries so many variants, it is
3887 not straightforward to generalize this. Maybe we should make an array
3888 of possible modes in init_expmed? Save this for GCC 2.7. */
3890 optab1 = ((op1_is_pow2 && op1 != const0_rtx)
3891 ? (unsignedp ? lshr_optab : ashr_optab)
3892 : (unsignedp ? udiv_optab : sdiv_optab));
3893 optab2 = ((op1_is_pow2 && op1 != const0_rtx)
3894 ? optab1
3895 : (unsignedp ? udivmod_optab : sdivmod_optab));
3897 for (compute_mode = mode; compute_mode != VOIDmode;
3898 compute_mode = GET_MODE_WIDER_MODE (compute_mode))
3899 if (optab_handler (optab1, compute_mode)->insn_code != CODE_FOR_nothing
3900 || optab_handler (optab2, compute_mode)->insn_code != CODE_FOR_nothing)
3901 break;
3903 if (compute_mode == VOIDmode)
3904 for (compute_mode = mode; compute_mode != VOIDmode;
3905 compute_mode = GET_MODE_WIDER_MODE (compute_mode))
3906 if (optab_libfunc (optab1, compute_mode)
3907 || optab_libfunc (optab2, compute_mode))
3908 break;
3910 /* If we still couldn't find a mode, use MODE, but expand_binop will
3911 probably die. */
3912 if (compute_mode == VOIDmode)
3913 compute_mode = mode;
3915 if (target && GET_MODE (target) == compute_mode)
3916 tquotient = target;
3917 else
3918 tquotient = gen_reg_rtx (compute_mode);
3920 size = GET_MODE_BITSIZE (compute_mode);
3921 #if 0
3922 /* It should be possible to restrict the precision to GET_MODE_BITSIZE
3923 (mode), and thereby get better code when OP1 is a constant. Do that
3924 later. It will require going over all usages of SIZE below. */
3925 size = GET_MODE_BITSIZE (mode);
3926 #endif
3928 /* Only deduct something for a REM if the last divide done was
3929 for a different constant. Then set the constant of the last
3930 divide. */
3931 max_cost = unsignedp ? udiv_cost[compute_mode] : sdiv_cost[compute_mode];
3932 if (rem_flag && ! (last_div_const != 0 && op1_is_constant
3933 && INTVAL (op1) == last_div_const))
3934 max_cost -= mul_cost[compute_mode] + add_cost[compute_mode];
3936 last_div_const = ! rem_flag && op1_is_constant ? INTVAL (op1) : 0;
3938 /* Now convert to the best mode to use. */
3939 if (compute_mode != mode)
3941 op0 = convert_modes (compute_mode, mode, op0, unsignedp);
3942 op1 = convert_modes (compute_mode, mode, op1, unsignedp);
3944 /* convert_modes may have placed op1 into a register, so we
3945 must recompute the following. */
3946 op1_is_constant = GET_CODE (op1) == CONST_INT;
3947 op1_is_pow2 = (op1_is_constant
3948 && ((EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1))
3949 || (! unsignedp
3950 && EXACT_POWER_OF_2_OR_ZERO_P (-INTVAL (op1)))))) ;
3953 /* If one of the operands is a volatile MEM, copy it into a register. */
3955 if (MEM_P (op0) && MEM_VOLATILE_P (op0))
3956 op0 = force_reg (compute_mode, op0);
3957 if (MEM_P (op1) && MEM_VOLATILE_P (op1))
3958 op1 = force_reg (compute_mode, op1);
3960 /* If we need the remainder or if OP1 is constant, we need to
3961 put OP0 in a register in case it has any queued subexpressions. */
3962 if (rem_flag || op1_is_constant)
3963 op0 = force_reg (compute_mode, op0);
3965 last = get_last_insn ();
3967 /* Promote floor rounding to trunc rounding for unsigned operations. */
3968 if (unsignedp)
3970 if (code == FLOOR_DIV_EXPR)
3971 code = TRUNC_DIV_EXPR;
3972 if (code == FLOOR_MOD_EXPR)
3973 code = TRUNC_MOD_EXPR;
3974 if (code == EXACT_DIV_EXPR && op1_is_pow2)
3975 code = TRUNC_DIV_EXPR;
3978 if (op1 != const0_rtx)
3979 switch (code)
3981 case TRUNC_MOD_EXPR:
3982 case TRUNC_DIV_EXPR:
3983 if (op1_is_constant)
3985 if (unsignedp)
3987 unsigned HOST_WIDE_INT mh;
3988 int pre_shift, post_shift;
3989 int dummy;
3990 rtx ml;
3991 unsigned HOST_WIDE_INT d = (INTVAL (op1)
3992 & GET_MODE_MASK (compute_mode));
3994 if (EXACT_POWER_OF_2_OR_ZERO_P (d))
3996 pre_shift = floor_log2 (d);
3997 if (rem_flag)
3999 remainder
4000 = expand_binop (compute_mode, and_optab, op0,
4001 GEN_INT (((HOST_WIDE_INT) 1 << pre_shift) - 1),
4002 remainder, 1,
4003 OPTAB_LIB_WIDEN);
4004 if (remainder)
4005 return gen_lowpart (mode, remainder);
4007 quotient = expand_shift (RSHIFT_EXPR, compute_mode, op0,
4008 build_int_cst (NULL_TREE,
4009 pre_shift),
4010 tquotient, 1);
4012 else if (size <= HOST_BITS_PER_WIDE_INT)
4014 if (d >= ((unsigned HOST_WIDE_INT) 1 << (size - 1)))
4016 /* Most significant bit of divisor is set; emit an scc
4017 insn. */
4018 quotient = emit_store_flag (tquotient, GEU, op0, op1,
4019 compute_mode, 1, 1);
4020 if (quotient == 0)
4021 goto fail1;
4023 else
4025 /* Find a suitable multiplier and right shift count
4026 instead of multiplying with D. */
4028 mh = choose_multiplier (d, size, size,
4029 &ml, &post_shift, &dummy);
4031 /* If the suggested multiplier is more than SIZE bits,
4032 we can do better for even divisors, using an
4033 initial right shift. */
4034 if (mh != 0 && (d & 1) == 0)
4036 pre_shift = floor_log2 (d & -d);
4037 mh = choose_multiplier (d >> pre_shift, size,
4038 size - pre_shift,
4039 &ml, &post_shift, &dummy);
4040 gcc_assert (!mh);
4042 else
4043 pre_shift = 0;
4045 if (mh != 0)
4047 rtx t1, t2, t3, t4;
4049 if (post_shift - 1 >= BITS_PER_WORD)
4050 goto fail1;
4052 extra_cost
4053 = (shift_cost[compute_mode][post_shift - 1]
4054 + shift_cost[compute_mode][1]
4055 + 2 * add_cost[compute_mode]);
4056 t1 = expand_mult_highpart (compute_mode, op0, ml,
4057 NULL_RTX, 1,
4058 max_cost - extra_cost);
4059 if (t1 == 0)
4060 goto fail1;
4061 t2 = force_operand (gen_rtx_MINUS (compute_mode,
4062 op0, t1),
4063 NULL_RTX);
4064 t3 = expand_shift
4065 (RSHIFT_EXPR, compute_mode, t2,
4066 build_int_cst (NULL_TREE, 1),
4067 NULL_RTX,1);
4068 t4 = force_operand (gen_rtx_PLUS (compute_mode,
4069 t1, t3),
4070 NULL_RTX);
4071 quotient = expand_shift
4072 (RSHIFT_EXPR, compute_mode, t4,
4073 build_int_cst (NULL_TREE, post_shift - 1),
4074 tquotient, 1);
4076 else
4078 rtx t1, t2;
4080 if (pre_shift >= BITS_PER_WORD
4081 || post_shift >= BITS_PER_WORD)
4082 goto fail1;
4084 t1 = expand_shift
4085 (RSHIFT_EXPR, compute_mode, op0,
4086 build_int_cst (NULL_TREE, pre_shift),
4087 NULL_RTX, 1);
4088 extra_cost
4089 = (shift_cost[compute_mode][pre_shift]
4090 + shift_cost[compute_mode][post_shift]);
4091 t2 = expand_mult_highpart (compute_mode, t1, ml,
4092 NULL_RTX, 1,
4093 max_cost - extra_cost);
4094 if (t2 == 0)
4095 goto fail1;
4096 quotient = expand_shift
4097 (RSHIFT_EXPR, compute_mode, t2,
4098 build_int_cst (NULL_TREE, post_shift),
4099 tquotient, 1);
4103 else /* Too wide mode to use tricky code */
4104 break;
4106 insn = get_last_insn ();
4107 if (insn != last
4108 && (set = single_set (insn)) != 0
4109 && SET_DEST (set) == quotient)
4110 set_unique_reg_note (insn,
4111 REG_EQUAL,
4112 gen_rtx_UDIV (compute_mode, op0, op1));
4114 else /* TRUNC_DIV, signed */
4116 unsigned HOST_WIDE_INT ml;
4117 int lgup, post_shift;
4118 rtx mlr;
4119 HOST_WIDE_INT d = INTVAL (op1);
4120 unsigned HOST_WIDE_INT abs_d;
4122 /* Since d might be INT_MIN, we have to cast to
4123 unsigned HOST_WIDE_INT before negating to avoid
4124 undefined signed overflow. */
4125 abs_d = (d >= 0
4126 ? (unsigned HOST_WIDE_INT) d
4127 : - (unsigned HOST_WIDE_INT) d);
4129 /* n rem d = n rem -d */
4130 if (rem_flag && d < 0)
4132 d = abs_d;
4133 op1 = gen_int_mode (abs_d, compute_mode);
4136 if (d == 1)
4137 quotient = op0;
4138 else if (d == -1)
4139 quotient = expand_unop (compute_mode, neg_optab, op0,
4140 tquotient, 0);
4141 else if (HOST_BITS_PER_WIDE_INT >= size
4142 && abs_d == (unsigned HOST_WIDE_INT) 1 << (size - 1))
4144 /* This case is not handled correctly below. */
4145 quotient = emit_store_flag (tquotient, EQ, op0, op1,
4146 compute_mode, 1, 1);
4147 if (quotient == 0)
4148 goto fail1;
4150 else if (EXACT_POWER_OF_2_OR_ZERO_P (d)
4151 && (rem_flag ? smod_pow2_cheap[compute_mode]
4152 : sdiv_pow2_cheap[compute_mode])
4153 /* We assume that cheap metric is true if the
4154 optab has an expander for this mode. */
4155 && ((optab_handler ((rem_flag ? smod_optab
4156 : sdiv_optab),
4157 compute_mode)->insn_code
4158 != CODE_FOR_nothing)
4159 || (optab_handler(sdivmod_optab,
4160 compute_mode)
4161 ->insn_code != CODE_FOR_nothing)))
4163 else if (EXACT_POWER_OF_2_OR_ZERO_P (abs_d))
4165 if (rem_flag)
4167 remainder = expand_smod_pow2 (compute_mode, op0, d);
4168 if (remainder)
4169 return gen_lowpart (mode, remainder);
4172 if (sdiv_pow2_cheap[compute_mode]
4173 && ((optab_handler (sdiv_optab, compute_mode)->insn_code
4174 != CODE_FOR_nothing)
4175 || (optab_handler (sdivmod_optab, compute_mode)->insn_code
4176 != CODE_FOR_nothing)))
4177 quotient = expand_divmod (0, TRUNC_DIV_EXPR,
4178 compute_mode, op0,
4179 gen_int_mode (abs_d,
4180 compute_mode),
4181 NULL_RTX, 0);
4182 else
4183 quotient = expand_sdiv_pow2 (compute_mode, op0, abs_d);
4185 /* We have computed OP0 / abs(OP1). If OP1 is negative,
4186 negate the quotient. */
4187 if (d < 0)
4189 insn = get_last_insn ();
4190 if (insn != last
4191 && (set = single_set (insn)) != 0
4192 && SET_DEST (set) == quotient
4193 && abs_d < ((unsigned HOST_WIDE_INT) 1
4194 << (HOST_BITS_PER_WIDE_INT - 1)))
4195 set_unique_reg_note (insn,
4196 REG_EQUAL,
4197 gen_rtx_DIV (compute_mode,
4198 op0,
4199 GEN_INT
4200 (trunc_int_for_mode
4201 (abs_d,
4202 compute_mode))));
4204 quotient = expand_unop (compute_mode, neg_optab,
4205 quotient, quotient, 0);
4208 else if (size <= HOST_BITS_PER_WIDE_INT)
4210 choose_multiplier (abs_d, size, size - 1,
4211 &mlr, &post_shift, &lgup);
4212 ml = (unsigned HOST_WIDE_INT) INTVAL (mlr);
4213 if (ml < (unsigned HOST_WIDE_INT) 1 << (size - 1))
4215 rtx t1, t2, t3;
4217 if (post_shift >= BITS_PER_WORD
4218 || size - 1 >= BITS_PER_WORD)
4219 goto fail1;
4221 extra_cost = (shift_cost[compute_mode][post_shift]
4222 + shift_cost[compute_mode][size - 1]
4223 + add_cost[compute_mode]);
4224 t1 = expand_mult_highpart (compute_mode, op0, mlr,
4225 NULL_RTX, 0,
4226 max_cost - extra_cost);
4227 if (t1 == 0)
4228 goto fail1;
4229 t2 = expand_shift
4230 (RSHIFT_EXPR, compute_mode, t1,
4231 build_int_cst (NULL_TREE, post_shift),
4232 NULL_RTX, 0);
4233 t3 = expand_shift
4234 (RSHIFT_EXPR, compute_mode, op0,
4235 build_int_cst (NULL_TREE, size - 1),
4236 NULL_RTX, 0);
4237 if (d < 0)
4238 quotient
4239 = force_operand (gen_rtx_MINUS (compute_mode,
4240 t3, t2),
4241 tquotient);
4242 else
4243 quotient
4244 = force_operand (gen_rtx_MINUS (compute_mode,
4245 t2, t3),
4246 tquotient);
4248 else
4250 rtx t1, t2, t3, t4;
4252 if (post_shift >= BITS_PER_WORD
4253 || size - 1 >= BITS_PER_WORD)
4254 goto fail1;
4256 ml |= (~(unsigned HOST_WIDE_INT) 0) << (size - 1);
4257 mlr = gen_int_mode (ml, compute_mode);
4258 extra_cost = (shift_cost[compute_mode][post_shift]
4259 + shift_cost[compute_mode][size - 1]
4260 + 2 * add_cost[compute_mode]);
4261 t1 = expand_mult_highpart (compute_mode, op0, mlr,
4262 NULL_RTX, 0,
4263 max_cost - extra_cost);
4264 if (t1 == 0)
4265 goto fail1;
4266 t2 = force_operand (gen_rtx_PLUS (compute_mode,
4267 t1, op0),
4268 NULL_RTX);
4269 t3 = expand_shift
4270 (RSHIFT_EXPR, compute_mode, t2,
4271 build_int_cst (NULL_TREE, post_shift),
4272 NULL_RTX, 0);
4273 t4 = expand_shift
4274 (RSHIFT_EXPR, compute_mode, op0,
4275 build_int_cst (NULL_TREE, size - 1),
4276 NULL_RTX, 0);
4277 if (d < 0)
4278 quotient
4279 = force_operand (gen_rtx_MINUS (compute_mode,
4280 t4, t3),
4281 tquotient);
4282 else
4283 quotient
4284 = force_operand (gen_rtx_MINUS (compute_mode,
4285 t3, t4),
4286 tquotient);
4289 else /* Too wide mode to use tricky code */
4290 break;
4292 insn = get_last_insn ();
4293 if (insn != last
4294 && (set = single_set (insn)) != 0
4295 && SET_DEST (set) == quotient)
4296 set_unique_reg_note (insn,
4297 REG_EQUAL,
4298 gen_rtx_DIV (compute_mode, op0, op1));
4300 break;
4302 fail1:
4303 delete_insns_since (last);
4304 break;
4306 case FLOOR_DIV_EXPR:
4307 case FLOOR_MOD_EXPR:
4308 /* We will come here only for signed operations. */
4309 if (op1_is_constant && HOST_BITS_PER_WIDE_INT >= size)
4311 unsigned HOST_WIDE_INT mh;
4312 int pre_shift, lgup, post_shift;
4313 HOST_WIDE_INT d = INTVAL (op1);
4314 rtx ml;
4316 if (d > 0)
4318 /* We could just as easily deal with negative constants here,
4319 but it does not seem worth the trouble for GCC 2.6. */
4320 if (EXACT_POWER_OF_2_OR_ZERO_P (d))
4322 pre_shift = floor_log2 (d);
4323 if (rem_flag)
4325 remainder = expand_binop (compute_mode, and_optab, op0,
4326 GEN_INT (((HOST_WIDE_INT) 1 << pre_shift) - 1),
4327 remainder, 0, OPTAB_LIB_WIDEN);
4328 if (remainder)
4329 return gen_lowpart (mode, remainder);
4331 quotient = expand_shift
4332 (RSHIFT_EXPR, compute_mode, op0,
4333 build_int_cst (NULL_TREE, pre_shift),
4334 tquotient, 0);
4336 else
4338 rtx t1, t2, t3, t4;
4340 mh = choose_multiplier (d, size, size - 1,
4341 &ml, &post_shift, &lgup);
4342 gcc_assert (!mh);
4344 if (post_shift < BITS_PER_WORD
4345 && size - 1 < BITS_PER_WORD)
4347 t1 = expand_shift
4348 (RSHIFT_EXPR, compute_mode, op0,
4349 build_int_cst (NULL_TREE, size - 1),
4350 NULL_RTX, 0);
4351 t2 = expand_binop (compute_mode, xor_optab, op0, t1,
4352 NULL_RTX, 0, OPTAB_WIDEN);
4353 extra_cost = (shift_cost[compute_mode][post_shift]
4354 + shift_cost[compute_mode][size - 1]
4355 + 2 * add_cost[compute_mode]);
4356 t3 = expand_mult_highpart (compute_mode, t2, ml,
4357 NULL_RTX, 1,
4358 max_cost - extra_cost);
4359 if (t3 != 0)
4361 t4 = expand_shift
4362 (RSHIFT_EXPR, compute_mode, t3,
4363 build_int_cst (NULL_TREE, post_shift),
4364 NULL_RTX, 1);
4365 quotient = expand_binop (compute_mode, xor_optab,
4366 t4, t1, tquotient, 0,
4367 OPTAB_WIDEN);
4372 else
4374 rtx nsign, t1, t2, t3, t4;
4375 t1 = force_operand (gen_rtx_PLUS (compute_mode,
4376 op0, constm1_rtx), NULL_RTX);
4377 t2 = expand_binop (compute_mode, ior_optab, op0, t1, NULL_RTX,
4378 0, OPTAB_WIDEN);
4379 nsign = expand_shift
4380 (RSHIFT_EXPR, compute_mode, t2,
4381 build_int_cst (NULL_TREE, size - 1),
4382 NULL_RTX, 0);
4383 t3 = force_operand (gen_rtx_MINUS (compute_mode, t1, nsign),
4384 NULL_RTX);
4385 t4 = expand_divmod (0, TRUNC_DIV_EXPR, compute_mode, t3, op1,
4386 NULL_RTX, 0);
4387 if (t4)
4389 rtx t5;
4390 t5 = expand_unop (compute_mode, one_cmpl_optab, nsign,
4391 NULL_RTX, 0);
4392 quotient = force_operand (gen_rtx_PLUS (compute_mode,
4393 t4, t5),
4394 tquotient);
4399 if (quotient != 0)
4400 break;
4401 delete_insns_since (last);
4403 /* Try using an instruction that produces both the quotient and
4404 remainder, using truncation. We can easily compensate the quotient
4405 or remainder to get floor rounding, once we have the remainder.
4406 Notice that we compute also the final remainder value here,
4407 and return the result right away. */
4408 if (target == 0 || GET_MODE (target) != compute_mode)
4409 target = gen_reg_rtx (compute_mode);
4411 if (rem_flag)
4413 remainder
4414 = REG_P (target) ? target : gen_reg_rtx (compute_mode);
4415 quotient = gen_reg_rtx (compute_mode);
4417 else
4419 quotient
4420 = REG_P (target) ? target : gen_reg_rtx (compute_mode);
4421 remainder = gen_reg_rtx (compute_mode);
4424 if (expand_twoval_binop (sdivmod_optab, op0, op1,
4425 quotient, remainder, 0))
4427 /* This could be computed with a branch-less sequence.
4428 Save that for later. */
4429 rtx tem;
4430 rtx label = gen_label_rtx ();
4431 do_cmp_and_jump (remainder, const0_rtx, EQ, compute_mode, label);
4432 tem = expand_binop (compute_mode, xor_optab, op0, op1,
4433 NULL_RTX, 0, OPTAB_WIDEN);
4434 do_cmp_and_jump (tem, const0_rtx, GE, compute_mode, label);
4435 expand_dec (quotient, const1_rtx);
4436 expand_inc (remainder, op1);
4437 emit_label (label);
4438 return gen_lowpart (mode, rem_flag ? remainder : quotient);
4441 /* No luck with division elimination or divmod. Have to do it
4442 by conditionally adjusting op0 *and* the result. */
4444 rtx label1, label2, label3, label4, label5;
4445 rtx adjusted_op0;
4446 rtx tem;
4448 quotient = gen_reg_rtx (compute_mode);
4449 adjusted_op0 = copy_to_mode_reg (compute_mode, op0);
4450 label1 = gen_label_rtx ();
4451 label2 = gen_label_rtx ();
4452 label3 = gen_label_rtx ();
4453 label4 = gen_label_rtx ();
4454 label5 = gen_label_rtx ();
4455 do_cmp_and_jump (op1, const0_rtx, LT, compute_mode, label2);
4456 do_cmp_and_jump (adjusted_op0, const0_rtx, LT, compute_mode, label1);
4457 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4458 quotient, 0, OPTAB_LIB_WIDEN);
4459 if (tem != quotient)
4460 emit_move_insn (quotient, tem);
4461 emit_jump_insn (gen_jump (label5));
4462 emit_barrier ();
4463 emit_label (label1);
4464 expand_inc (adjusted_op0, const1_rtx);
4465 emit_jump_insn (gen_jump (label4));
4466 emit_barrier ();
4467 emit_label (label2);
4468 do_cmp_and_jump (adjusted_op0, const0_rtx, GT, compute_mode, label3);
4469 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4470 quotient, 0, OPTAB_LIB_WIDEN);
4471 if (tem != quotient)
4472 emit_move_insn (quotient, tem);
4473 emit_jump_insn (gen_jump (label5));
4474 emit_barrier ();
4475 emit_label (label3);
4476 expand_dec (adjusted_op0, const1_rtx);
4477 emit_label (label4);
4478 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4479 quotient, 0, OPTAB_LIB_WIDEN);
4480 if (tem != quotient)
4481 emit_move_insn (quotient, tem);
4482 expand_dec (quotient, const1_rtx);
4483 emit_label (label5);
4485 break;
4487 case CEIL_DIV_EXPR:
4488 case CEIL_MOD_EXPR:
4489 if (unsignedp)
4491 if (op1_is_constant && EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1)))
4493 rtx t1, t2, t3;
4494 unsigned HOST_WIDE_INT d = INTVAL (op1);
4495 t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
4496 build_int_cst (NULL_TREE, floor_log2 (d)),
4497 tquotient, 1);
4498 t2 = expand_binop (compute_mode, and_optab, op0,
4499 GEN_INT (d - 1),
4500 NULL_RTX, 1, OPTAB_LIB_WIDEN);
4501 t3 = gen_reg_rtx (compute_mode);
4502 t3 = emit_store_flag (t3, NE, t2, const0_rtx,
4503 compute_mode, 1, 1);
4504 if (t3 == 0)
4506 rtx lab;
4507 lab = gen_label_rtx ();
4508 do_cmp_and_jump (t2, const0_rtx, EQ, compute_mode, lab);
4509 expand_inc (t1, const1_rtx);
4510 emit_label (lab);
4511 quotient = t1;
4513 else
4514 quotient = force_operand (gen_rtx_PLUS (compute_mode,
4515 t1, t3),
4516 tquotient);
4517 break;
4520 /* Try using an instruction that produces both the quotient and
4521 remainder, using truncation. We can easily compensate the
4522 quotient or remainder to get ceiling rounding, once we have the
4523 remainder. Notice that we compute also the final remainder
4524 value here, and return the result right away. */
4525 if (target == 0 || GET_MODE (target) != compute_mode)
4526 target = gen_reg_rtx (compute_mode);
4528 if (rem_flag)
4530 remainder = (REG_P (target)
4531 ? target : gen_reg_rtx (compute_mode));
4532 quotient = gen_reg_rtx (compute_mode);
4534 else
4536 quotient = (REG_P (target)
4537 ? target : gen_reg_rtx (compute_mode));
4538 remainder = gen_reg_rtx (compute_mode);
4541 if (expand_twoval_binop (udivmod_optab, op0, op1, quotient,
4542 remainder, 1))
4544 /* This could be computed with a branch-less sequence.
4545 Save that for later. */
4546 rtx label = gen_label_rtx ();
4547 do_cmp_and_jump (remainder, const0_rtx, EQ,
4548 compute_mode, label);
4549 expand_inc (quotient, const1_rtx);
4550 expand_dec (remainder, op1);
4551 emit_label (label);
4552 return gen_lowpart (mode, rem_flag ? remainder : quotient);
4555 /* No luck with division elimination or divmod. Have to do it
4556 by conditionally adjusting op0 *and* the result. */
4558 rtx label1, label2;
4559 rtx adjusted_op0, tem;
4561 quotient = gen_reg_rtx (compute_mode);
4562 adjusted_op0 = copy_to_mode_reg (compute_mode, op0);
4563 label1 = gen_label_rtx ();
4564 label2 = gen_label_rtx ();
4565 do_cmp_and_jump (adjusted_op0, const0_rtx, NE,
4566 compute_mode, label1);
4567 emit_move_insn (quotient, const0_rtx);
4568 emit_jump_insn (gen_jump (label2));
4569 emit_barrier ();
4570 emit_label (label1);
4571 expand_dec (adjusted_op0, const1_rtx);
4572 tem = expand_binop (compute_mode, udiv_optab, adjusted_op0, op1,
4573 quotient, 1, OPTAB_LIB_WIDEN);
4574 if (tem != quotient)
4575 emit_move_insn (quotient, tem);
4576 expand_inc (quotient, const1_rtx);
4577 emit_label (label2);
4580 else /* signed */
4582 if (op1_is_constant && EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1))
4583 && INTVAL (op1) >= 0)
4585 /* This is extremely similar to the code for the unsigned case
4586 above. For 2.7 we should merge these variants, but for
4587 2.6.1 I don't want to touch the code for unsigned since that
4588 get used in C. The signed case will only be used by other
4589 languages (Ada). */
4591 rtx t1, t2, t3;
4592 unsigned HOST_WIDE_INT d = INTVAL (op1);
4593 t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
4594 build_int_cst (NULL_TREE, floor_log2 (d)),
4595 tquotient, 0);
4596 t2 = expand_binop (compute_mode, and_optab, op0,
4597 GEN_INT (d - 1),
4598 NULL_RTX, 1, OPTAB_LIB_WIDEN);
4599 t3 = gen_reg_rtx (compute_mode);
4600 t3 = emit_store_flag (t3, NE, t2, const0_rtx,
4601 compute_mode, 1, 1);
4602 if (t3 == 0)
4604 rtx lab;
4605 lab = gen_label_rtx ();
4606 do_cmp_and_jump (t2, const0_rtx, EQ, compute_mode, lab);
4607 expand_inc (t1, const1_rtx);
4608 emit_label (lab);
4609 quotient = t1;
4611 else
4612 quotient = force_operand (gen_rtx_PLUS (compute_mode,
4613 t1, t3),
4614 tquotient);
4615 break;
4618 /* Try using an instruction that produces both the quotient and
4619 remainder, using truncation. We can easily compensate the
4620 quotient or remainder to get ceiling rounding, once we have the
4621 remainder. Notice that we compute also the final remainder
4622 value here, and return the result right away. */
4623 if (target == 0 || GET_MODE (target) != compute_mode)
4624 target = gen_reg_rtx (compute_mode);
4625 if (rem_flag)
4627 remainder= (REG_P (target)
4628 ? target : gen_reg_rtx (compute_mode));
4629 quotient = gen_reg_rtx (compute_mode);
4631 else
4633 quotient = (REG_P (target)
4634 ? target : gen_reg_rtx (compute_mode));
4635 remainder = gen_reg_rtx (compute_mode);
4638 if (expand_twoval_binop (sdivmod_optab, op0, op1, quotient,
4639 remainder, 0))
4641 /* This could be computed with a branch-less sequence.
4642 Save that for later. */
4643 rtx tem;
4644 rtx label = gen_label_rtx ();
4645 do_cmp_and_jump (remainder, const0_rtx, EQ,
4646 compute_mode, label);
4647 tem = expand_binop (compute_mode, xor_optab, op0, op1,
4648 NULL_RTX, 0, OPTAB_WIDEN);
4649 do_cmp_and_jump (tem, const0_rtx, LT, compute_mode, label);
4650 expand_inc (quotient, const1_rtx);
4651 expand_dec (remainder, op1);
4652 emit_label (label);
4653 return gen_lowpart (mode, rem_flag ? remainder : quotient);
4656 /* No luck with division elimination or divmod. Have to do it
4657 by conditionally adjusting op0 *and* the result. */
4659 rtx label1, label2, label3, label4, label5;
4660 rtx adjusted_op0;
4661 rtx tem;
4663 quotient = gen_reg_rtx (compute_mode);
4664 adjusted_op0 = copy_to_mode_reg (compute_mode, op0);
4665 label1 = gen_label_rtx ();
4666 label2 = gen_label_rtx ();
4667 label3 = gen_label_rtx ();
4668 label4 = gen_label_rtx ();
4669 label5 = gen_label_rtx ();
4670 do_cmp_and_jump (op1, const0_rtx, LT, compute_mode, label2);
4671 do_cmp_and_jump (adjusted_op0, const0_rtx, GT,
4672 compute_mode, label1);
4673 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4674 quotient, 0, OPTAB_LIB_WIDEN);
4675 if (tem != quotient)
4676 emit_move_insn (quotient, tem);
4677 emit_jump_insn (gen_jump (label5));
4678 emit_barrier ();
4679 emit_label (label1);
4680 expand_dec (adjusted_op0, const1_rtx);
4681 emit_jump_insn (gen_jump (label4));
4682 emit_barrier ();
4683 emit_label (label2);
4684 do_cmp_and_jump (adjusted_op0, const0_rtx, LT,
4685 compute_mode, label3);
4686 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4687 quotient, 0, OPTAB_LIB_WIDEN);
4688 if (tem != quotient)
4689 emit_move_insn (quotient, tem);
4690 emit_jump_insn (gen_jump (label5));
4691 emit_barrier ();
4692 emit_label (label3);
4693 expand_inc (adjusted_op0, const1_rtx);
4694 emit_label (label4);
4695 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
4696 quotient, 0, OPTAB_LIB_WIDEN);
4697 if (tem != quotient)
4698 emit_move_insn (quotient, tem);
4699 expand_inc (quotient, const1_rtx);
4700 emit_label (label5);
4703 break;
4705 case EXACT_DIV_EXPR:
4706 if (op1_is_constant && HOST_BITS_PER_WIDE_INT >= size)
4708 HOST_WIDE_INT d = INTVAL (op1);
4709 unsigned HOST_WIDE_INT ml;
4710 int pre_shift;
4711 rtx t1;
4713 pre_shift = floor_log2 (d & -d);
4714 ml = invert_mod2n (d >> pre_shift, size);
4715 t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
4716 build_int_cst (NULL_TREE, pre_shift),
4717 NULL_RTX, unsignedp);
4718 quotient = expand_mult (compute_mode, t1,
4719 gen_int_mode (ml, compute_mode),
4720 NULL_RTX, 1);
4722 insn = get_last_insn ();
4723 set_unique_reg_note (insn,
4724 REG_EQUAL,
4725 gen_rtx_fmt_ee (unsignedp ? UDIV : DIV,
4726 compute_mode,
4727 op0, op1));
4729 break;
4731 case ROUND_DIV_EXPR:
4732 case ROUND_MOD_EXPR:
4733 if (unsignedp)
4735 rtx tem;
4736 rtx label;
4737 label = gen_label_rtx ();
4738 quotient = gen_reg_rtx (compute_mode);
4739 remainder = gen_reg_rtx (compute_mode);
4740 if (expand_twoval_binop (udivmod_optab, op0, op1, quotient, remainder, 1) == 0)
4742 rtx tem;
4743 quotient = expand_binop (compute_mode, udiv_optab, op0, op1,
4744 quotient, 1, OPTAB_LIB_WIDEN);
4745 tem = expand_mult (compute_mode, quotient, op1, NULL_RTX, 1);
4746 remainder = expand_binop (compute_mode, sub_optab, op0, tem,
4747 remainder, 1, OPTAB_LIB_WIDEN);
4749 tem = plus_constant (op1, -1);
4750 tem = expand_shift (RSHIFT_EXPR, compute_mode, tem,
4751 build_int_cst (NULL_TREE, 1),
4752 NULL_RTX, 1);
4753 do_cmp_and_jump (remainder, tem, LEU, compute_mode, label);
4754 expand_inc (quotient, const1_rtx);
4755 expand_dec (remainder, op1);
4756 emit_label (label);
4758 else
4760 rtx abs_rem, abs_op1, tem, mask;
4761 rtx label;
4762 label = gen_label_rtx ();
4763 quotient = gen_reg_rtx (compute_mode);
4764 remainder = gen_reg_rtx (compute_mode);
4765 if (expand_twoval_binop (sdivmod_optab, op0, op1, quotient, remainder, 0) == 0)
4767 rtx tem;
4768 quotient = expand_binop (compute_mode, sdiv_optab, op0, op1,
4769 quotient, 0, OPTAB_LIB_WIDEN);
4770 tem = expand_mult (compute_mode, quotient, op1, NULL_RTX, 0);
4771 remainder = expand_binop (compute_mode, sub_optab, op0, tem,
4772 remainder, 0, OPTAB_LIB_WIDEN);
4774 abs_rem = expand_abs (compute_mode, remainder, NULL_RTX, 1, 0);
4775 abs_op1 = expand_abs (compute_mode, op1, NULL_RTX, 1, 0);
4776 tem = expand_shift (LSHIFT_EXPR, compute_mode, abs_rem,
4777 build_int_cst (NULL_TREE, 1),
4778 NULL_RTX, 1);
4779 do_cmp_and_jump (tem, abs_op1, LTU, compute_mode, label);
4780 tem = expand_binop (compute_mode, xor_optab, op0, op1,
4781 NULL_RTX, 0, OPTAB_WIDEN);
4782 mask = expand_shift (RSHIFT_EXPR, compute_mode, tem,
4783 build_int_cst (NULL_TREE, size - 1),
4784 NULL_RTX, 0);
4785 tem = expand_binop (compute_mode, xor_optab, mask, const1_rtx,
4786 NULL_RTX, 0, OPTAB_WIDEN);
4787 tem = expand_binop (compute_mode, sub_optab, tem, mask,
4788 NULL_RTX, 0, OPTAB_WIDEN);
4789 expand_inc (quotient, tem);
4790 tem = expand_binop (compute_mode, xor_optab, mask, op1,
4791 NULL_RTX, 0, OPTAB_WIDEN);
4792 tem = expand_binop (compute_mode, sub_optab, tem, mask,
4793 NULL_RTX, 0, OPTAB_WIDEN);
4794 expand_dec (remainder, tem);
4795 emit_label (label);
4797 return gen_lowpart (mode, rem_flag ? remainder : quotient);
4799 default:
4800 gcc_unreachable ();
4803 if (quotient == 0)
4805 if (target && GET_MODE (target) != compute_mode)
4806 target = 0;
4808 if (rem_flag)
4810 /* Try to produce the remainder without producing the quotient.
4811 If we seem to have a divmod pattern that does not require widening,
4812 don't try widening here. We should really have a WIDEN argument
4813 to expand_twoval_binop, since what we'd really like to do here is
4814 1) try a mod insn in compute_mode
4815 2) try a divmod insn in compute_mode
4816 3) try a div insn in compute_mode and multiply-subtract to get
4817 remainder
4818 4) try the same things with widening allowed. */
4819 remainder
4820 = sign_expand_binop (compute_mode, umod_optab, smod_optab,
4821 op0, op1, target,
4822 unsignedp,
4823 ((optab_handler (optab2, compute_mode)->insn_code
4824 != CODE_FOR_nothing)
4825 ? OPTAB_DIRECT : OPTAB_WIDEN));
4826 if (remainder == 0)
4828 /* No luck there. Can we do remainder and divide at once
4829 without a library call? */
4830 remainder = gen_reg_rtx (compute_mode);
4831 if (! expand_twoval_binop ((unsignedp
4832 ? udivmod_optab
4833 : sdivmod_optab),
4834 op0, op1,
4835 NULL_RTX, remainder, unsignedp))
4836 remainder = 0;
4839 if (remainder)
4840 return gen_lowpart (mode, remainder);
4843 /* Produce the quotient. Try a quotient insn, but not a library call.
4844 If we have a divmod in this mode, use it in preference to widening
4845 the div (for this test we assume it will not fail). Note that optab2
4846 is set to the one of the two optabs that the call below will use. */
4847 quotient
4848 = sign_expand_binop (compute_mode, udiv_optab, sdiv_optab,
4849 op0, op1, rem_flag ? NULL_RTX : target,
4850 unsignedp,
4851 ((optab_handler (optab2, compute_mode)->insn_code
4852 != CODE_FOR_nothing)
4853 ? OPTAB_DIRECT : OPTAB_WIDEN));
4855 if (quotient == 0)
4857 /* No luck there. Try a quotient-and-remainder insn,
4858 keeping the quotient alone. */
4859 quotient = gen_reg_rtx (compute_mode);
4860 if (! expand_twoval_binop (unsignedp ? udivmod_optab : sdivmod_optab,
4861 op0, op1,
4862 quotient, NULL_RTX, unsignedp))
4864 quotient = 0;
4865 if (! rem_flag)
4866 /* Still no luck. If we are not computing the remainder,
4867 use a library call for the quotient. */
4868 quotient = sign_expand_binop (compute_mode,
4869 udiv_optab, sdiv_optab,
4870 op0, op1, target,
4871 unsignedp, OPTAB_LIB_WIDEN);
4876 if (rem_flag)
4878 if (target && GET_MODE (target) != compute_mode)
4879 target = 0;
4881 if (quotient == 0)
4883 /* No divide instruction either. Use library for remainder. */
4884 remainder = sign_expand_binop (compute_mode, umod_optab, smod_optab,
4885 op0, op1, target,
4886 unsignedp, OPTAB_LIB_WIDEN);
4887 /* No remainder function. Try a quotient-and-remainder
4888 function, keeping the remainder. */
4889 if (!remainder)
4891 remainder = gen_reg_rtx (compute_mode);
4892 if (!expand_twoval_binop_libfunc
4893 (unsignedp ? udivmod_optab : sdivmod_optab,
4894 op0, op1,
4895 NULL_RTX, remainder,
4896 unsignedp ? UMOD : MOD))
4897 remainder = NULL_RTX;
4900 else
4902 /* We divided. Now finish doing X - Y * (X / Y). */
4903 remainder = expand_mult (compute_mode, quotient, op1,
4904 NULL_RTX, unsignedp);
4905 remainder = expand_binop (compute_mode, sub_optab, op0,
4906 remainder, target, unsignedp,
4907 OPTAB_LIB_WIDEN);
4911 return gen_lowpart (mode, rem_flag ? remainder : quotient);
4914 /* Return a tree node with data type TYPE, describing the value of X.
4915 Usually this is an VAR_DECL, if there is no obvious better choice.
4916 X may be an expression, however we only support those expressions
4917 generated by loop.c. */
4919 tree
4920 make_tree (tree type, rtx x)
4922 tree t;
4924 switch (GET_CODE (x))
4926 case CONST_INT:
4928 HOST_WIDE_INT hi = 0;
4930 if (INTVAL (x) < 0
4931 && !(TYPE_UNSIGNED (type)
4932 && (GET_MODE_BITSIZE (TYPE_MODE (type))
4933 < HOST_BITS_PER_WIDE_INT)))
4934 hi = -1;
4936 t = build_int_cst_wide (type, INTVAL (x), hi);
4938 return t;
4941 case CONST_DOUBLE:
4942 if (GET_MODE (x) == VOIDmode)
4943 t = build_int_cst_wide (type,
4944 CONST_DOUBLE_LOW (x), CONST_DOUBLE_HIGH (x));
4945 else
4947 REAL_VALUE_TYPE d;
4949 REAL_VALUE_FROM_CONST_DOUBLE (d, x);
4950 t = build_real (type, d);
4953 return t;
4955 case CONST_VECTOR:
4957 int units = CONST_VECTOR_NUNITS (x);
4958 tree itype = TREE_TYPE (type);
4959 tree t = NULL_TREE;
4960 int i;
4963 /* Build a tree with vector elements. */
4964 for (i = units - 1; i >= 0; --i)
4966 rtx elt = CONST_VECTOR_ELT (x, i);
4967 t = tree_cons (NULL_TREE, make_tree (itype, elt), t);
4970 return build_vector (type, t);
4973 case PLUS:
4974 return fold_build2 (PLUS_EXPR, type, make_tree (type, XEXP (x, 0)),
4975 make_tree (type, XEXP (x, 1)));
4977 case MINUS:
4978 return fold_build2 (MINUS_EXPR, type, make_tree (type, XEXP (x, 0)),
4979 make_tree (type, XEXP (x, 1)));
4981 case NEG:
4982 return fold_build1 (NEGATE_EXPR, type, make_tree (type, XEXP (x, 0)));
4984 case MULT:
4985 return fold_build2 (MULT_EXPR, type, make_tree (type, XEXP (x, 0)),
4986 make_tree (type, XEXP (x, 1)));
4988 case ASHIFT:
4989 return fold_build2 (LSHIFT_EXPR, type, make_tree (type, XEXP (x, 0)),
4990 make_tree (type, XEXP (x, 1)));
4992 case LSHIFTRT:
4993 t = unsigned_type_for (type);
4994 return fold_convert (type, build2 (RSHIFT_EXPR, t,
4995 make_tree (t, XEXP (x, 0)),
4996 make_tree (type, XEXP (x, 1))));
4998 case ASHIFTRT:
4999 t = signed_type_for (type);
5000 return fold_convert (type, build2 (RSHIFT_EXPR, t,
5001 make_tree (t, XEXP (x, 0)),
5002 make_tree (type, XEXP (x, 1))));
5004 case DIV:
5005 if (TREE_CODE (type) != REAL_TYPE)
5006 t = signed_type_for (type);
5007 else
5008 t = type;
5010 return fold_convert (type, build2 (TRUNC_DIV_EXPR, t,
5011 make_tree (t, XEXP (x, 0)),
5012 make_tree (t, XEXP (x, 1))));
5013 case UDIV:
5014 t = unsigned_type_for (type);
5015 return fold_convert (type, build2 (TRUNC_DIV_EXPR, t,
5016 make_tree (t, XEXP (x, 0)),
5017 make_tree (t, XEXP (x, 1))));
5019 case SIGN_EXTEND:
5020 case ZERO_EXTEND:
5021 t = lang_hooks.types.type_for_mode (GET_MODE (XEXP (x, 0)),
5022 GET_CODE (x) == ZERO_EXTEND);
5023 return fold_convert (type, make_tree (t, XEXP (x, 0)));
5025 case CONST:
5026 return make_tree (type, XEXP (x, 0));
5028 case SYMBOL_REF:
5029 t = SYMBOL_REF_DECL (x);
5030 if (t)
5031 return fold_convert (type, build_fold_addr_expr (t));
5032 /* else fall through. */
5034 default:
5035 t = build_decl (VAR_DECL, NULL_TREE, type);
5037 /* If TYPE is a POINTER_TYPE, X might be Pmode with TYPE_MODE being
5038 ptr_mode. So convert. */
5039 if (POINTER_TYPE_P (type))
5040 x = convert_memory_address (TYPE_MODE (type), x);
5042 /* Note that we do *not* use SET_DECL_RTL here, because we do not
5043 want set_decl_rtl to go adjusting REG_ATTRS for this temporary. */
5044 t->decl_with_rtl.rtl = x;
5046 return t;
5050 /* Compute the logical-and of OP0 and OP1, storing it in TARGET
5051 and returning TARGET.
5053 If TARGET is 0, a pseudo-register or constant is returned. */
5056 expand_and (enum machine_mode mode, rtx op0, rtx op1, rtx target)
5058 rtx tem = 0;
5060 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
5061 tem = simplify_binary_operation (AND, mode, op0, op1);
5062 if (tem == 0)
5063 tem = expand_binop (mode, and_optab, op0, op1, target, 0, OPTAB_LIB_WIDEN);
5065 if (target == 0)
5066 target = tem;
5067 else if (tem != target)
5068 emit_move_insn (target, tem);
5069 return target;
5072 /* Helper function for emit_store_flag. */
5073 static rtx
5074 emit_store_flag_1 (rtx target, rtx subtarget, enum machine_mode mode,
5075 int normalizep)
5077 rtx op0;
5078 enum machine_mode target_mode = GET_MODE (target);
5080 /* If we are converting to a wider mode, first convert to
5081 TARGET_MODE, then normalize. This produces better combining
5082 opportunities on machines that have a SIGN_EXTRACT when we are
5083 testing a single bit. This mostly benefits the 68k.
5085 If STORE_FLAG_VALUE does not have the sign bit set when
5086 interpreted in MODE, we can do this conversion as unsigned, which
5087 is usually more efficient. */
5088 if (GET_MODE_SIZE (target_mode) > GET_MODE_SIZE (mode))
5090 convert_move (target, subtarget,
5091 (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
5092 && 0 == (STORE_FLAG_VALUE
5093 & ((HOST_WIDE_INT) 1
5094 << (GET_MODE_BITSIZE (mode) -1))));
5095 op0 = target;
5096 mode = target_mode;
5098 else
5099 op0 = subtarget;
5101 /* If we want to keep subexpressions around, don't reuse our last
5102 target. */
5103 if (optimize)
5104 subtarget = 0;
5106 /* Now normalize to the proper value in MODE. Sometimes we don't
5107 have to do anything. */
5108 if (normalizep == 0 || normalizep == STORE_FLAG_VALUE)
5110 /* STORE_FLAG_VALUE might be the most negative number, so write
5111 the comparison this way to avoid a compiler-time warning. */
5112 else if (- normalizep == STORE_FLAG_VALUE)
5113 op0 = expand_unop (mode, neg_optab, op0, subtarget, 0);
5115 /* We don't want to use STORE_FLAG_VALUE < 0 below since this makes
5116 it hard to use a value of just the sign bit due to ANSI integer
5117 constant typing rules. */
5118 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5119 && (STORE_FLAG_VALUE
5120 & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))))
5121 op0 = expand_shift (RSHIFT_EXPR, mode, op0,
5122 size_int (GET_MODE_BITSIZE (mode) - 1), subtarget,
5123 normalizep == 1);
5124 else
5126 gcc_assert (STORE_FLAG_VALUE & 1);
5128 op0 = expand_and (mode, op0, const1_rtx, subtarget);
5129 if (normalizep == -1)
5130 op0 = expand_unop (mode, neg_optab, op0, op0, 0);
5133 /* If we were converting to a smaller mode, do the conversion now. */
5134 if (target_mode != mode)
5136 convert_move (target, op0, 0);
5137 return target;
5139 else
5140 return op0;
5143 /* Emit a store-flags instruction for comparison CODE on OP0 and OP1
5144 and storing in TARGET. Normally return TARGET.
5145 Return 0 if that cannot be done.
5147 MODE is the mode to use for OP0 and OP1 should they be CONST_INTs. If
5148 it is VOIDmode, they cannot both be CONST_INT.
5150 UNSIGNEDP is for the case where we have to widen the operands
5151 to perform the operation. It says to use zero-extension.
5153 NORMALIZEP is 1 if we should convert the result to be either zero
5154 or one. Normalize is -1 if we should convert the result to be
5155 either zero or -1. If NORMALIZEP is zero, the result will be left
5156 "raw" out of the scc insn. */
5159 emit_store_flag (rtx target, enum rtx_code code, rtx op0, rtx op1,
5160 enum machine_mode mode, int unsignedp, int normalizep)
5162 rtx subtarget;
5163 enum insn_code icode;
5164 enum machine_mode compare_mode;
5165 enum machine_mode target_mode = GET_MODE (target);
5166 rtx tem;
5167 rtx last = get_last_insn ();
5168 rtx pattern, comparison;
5170 if (unsignedp)
5171 code = unsigned_condition (code);
5173 /* If one operand is constant, make it the second one. Only do this
5174 if the other operand is not constant as well. */
5176 if (swap_commutative_operands_p (op0, op1))
5178 tem = op0;
5179 op0 = op1;
5180 op1 = tem;
5181 code = swap_condition (code);
5184 if (mode == VOIDmode)
5185 mode = GET_MODE (op0);
5187 /* For some comparisons with 1 and -1, we can convert this to
5188 comparisons with zero. This will often produce more opportunities for
5189 store-flag insns. */
5191 switch (code)
5193 case LT:
5194 if (op1 == const1_rtx)
5195 op1 = const0_rtx, code = LE;
5196 break;
5197 case LE:
5198 if (op1 == constm1_rtx)
5199 op1 = const0_rtx, code = LT;
5200 break;
5201 case GE:
5202 if (op1 == const1_rtx)
5203 op1 = const0_rtx, code = GT;
5204 break;
5205 case GT:
5206 if (op1 == constm1_rtx)
5207 op1 = const0_rtx, code = GE;
5208 break;
5209 case GEU:
5210 if (op1 == const1_rtx)
5211 op1 = const0_rtx, code = NE;
5212 break;
5213 case LTU:
5214 if (op1 == const1_rtx)
5215 op1 = const0_rtx, code = EQ;
5216 break;
5217 default:
5218 break;
5221 /* If we are comparing a double-word integer with zero or -1, we can
5222 convert the comparison into one involving a single word. */
5223 if (GET_MODE_BITSIZE (mode) == BITS_PER_WORD * 2
5224 && GET_MODE_CLASS (mode) == MODE_INT
5225 && (!MEM_P (op0) || ! MEM_VOLATILE_P (op0)))
5227 if ((code == EQ || code == NE)
5228 && (op1 == const0_rtx || op1 == constm1_rtx))
5230 rtx op00, op01, op0both;
5232 /* Do a logical OR or AND of the two words and compare the
5233 result. */
5234 op00 = simplify_gen_subreg (word_mode, op0, mode, 0);
5235 op01 = simplify_gen_subreg (word_mode, op0, mode, UNITS_PER_WORD);
5236 op0both = expand_binop (word_mode,
5237 op1 == const0_rtx ? ior_optab : and_optab,
5238 op00, op01, NULL_RTX, unsignedp,
5239 OPTAB_DIRECT);
5241 if (op0both != 0)
5242 return emit_store_flag (target, code, op0both, op1, word_mode,
5243 unsignedp, normalizep);
5245 else if ((code == LT || code == GE) && op1 == const0_rtx)
5247 rtx op0h;
5249 /* If testing the sign bit, can just test on high word. */
5250 op0h = simplify_gen_subreg (word_mode, op0, mode,
5251 subreg_highpart_offset (word_mode,
5252 mode));
5253 return emit_store_flag (target, code, op0h, op1, word_mode,
5254 unsignedp, normalizep);
5258 /* If this is A < 0 or A >= 0, we can do this by taking the ones
5259 complement of A (for GE) and shifting the sign bit to the low bit. */
5260 if (op1 == const0_rtx && (code == LT || code == GE)
5261 && GET_MODE_CLASS (mode) == MODE_INT
5262 && (normalizep || STORE_FLAG_VALUE == 1
5263 || (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5264 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
5265 == ((unsigned HOST_WIDE_INT) 1
5266 << (GET_MODE_BITSIZE (mode) - 1))))))
5268 subtarget = target;
5270 /* If the result is to be wider than OP0, it is best to convert it
5271 first. If it is to be narrower, it is *incorrect* to convert it
5272 first. */
5273 if (GET_MODE_SIZE (target_mode) > GET_MODE_SIZE (mode))
5275 op0 = convert_modes (target_mode, mode, op0, 0);
5276 mode = target_mode;
5279 if (target_mode != mode)
5280 subtarget = 0;
5282 if (code == GE)
5283 op0 = expand_unop (mode, one_cmpl_optab, op0,
5284 ((STORE_FLAG_VALUE == 1 || normalizep)
5285 ? 0 : subtarget), 0);
5287 if (STORE_FLAG_VALUE == 1 || normalizep)
5288 /* If we are supposed to produce a 0/1 value, we want to do
5289 a logical shift from the sign bit to the low-order bit; for
5290 a -1/0 value, we do an arithmetic shift. */
5291 op0 = expand_shift (RSHIFT_EXPR, mode, op0,
5292 size_int (GET_MODE_BITSIZE (mode) - 1),
5293 subtarget, normalizep != -1);
5295 if (mode != target_mode)
5296 op0 = convert_modes (target_mode, mode, op0, 0);
5298 return op0;
5301 icode = setcc_gen_code[(int) code];
5303 if (icode != CODE_FOR_nothing)
5305 insn_operand_predicate_fn pred;
5307 /* We think we may be able to do this with a scc insn. Emit the
5308 comparison and then the scc insn. */
5310 do_pending_stack_adjust ();
5311 last = get_last_insn ();
5313 comparison
5314 = compare_from_rtx (op0, op1, code, unsignedp, mode, NULL_RTX);
5315 if (CONSTANT_P (comparison))
5317 switch (GET_CODE (comparison))
5319 case CONST_INT:
5320 if (comparison == const0_rtx)
5321 return const0_rtx;
5322 break;
5324 #ifdef FLOAT_STORE_FLAG_VALUE
5325 case CONST_DOUBLE:
5326 if (comparison == CONST0_RTX (GET_MODE (comparison)))
5327 return const0_rtx;
5328 break;
5329 #endif
5330 default:
5331 gcc_unreachable ();
5334 if (normalizep == 1)
5335 return const1_rtx;
5336 if (normalizep == -1)
5337 return constm1_rtx;
5338 return const_true_rtx;
5341 /* The code of COMPARISON may not match CODE if compare_from_rtx
5342 decided to swap its operands and reverse the original code.
5344 We know that compare_from_rtx returns either a CONST_INT or
5345 a new comparison code, so it is safe to just extract the
5346 code from COMPARISON. */
5347 code = GET_CODE (comparison);
5349 /* Get a reference to the target in the proper mode for this insn. */
5350 compare_mode = insn_data[(int) icode].operand[0].mode;
5351 subtarget = target;
5352 pred = insn_data[(int) icode].operand[0].predicate;
5353 if (optimize || ! (*pred) (subtarget, compare_mode))
5354 subtarget = gen_reg_rtx (compare_mode);
5356 pattern = GEN_FCN (icode) (subtarget);
5357 if (pattern)
5359 emit_insn (pattern);
5360 return emit_store_flag_1 (target, subtarget, compare_mode,
5361 normalizep);
5364 else
5366 /* We don't have an scc insn, so try a cstore insn. */
5368 for (compare_mode = mode; compare_mode != VOIDmode;
5369 compare_mode = GET_MODE_WIDER_MODE (compare_mode))
5371 icode = optab_handler (cstore_optab, compare_mode)->insn_code;
5372 if (icode != CODE_FOR_nothing)
5373 break;
5376 if (icode != CODE_FOR_nothing)
5378 enum machine_mode result_mode
5379 = insn_data[(int) icode].operand[0].mode;
5380 rtx cstore_op0 = op0;
5381 rtx cstore_op1 = op1;
5383 do_pending_stack_adjust ();
5384 last = get_last_insn ();
5386 if (compare_mode != mode)
5388 cstore_op0 = convert_modes (compare_mode, mode, cstore_op0,
5389 unsignedp);
5390 cstore_op1 = convert_modes (compare_mode, mode, cstore_op1,
5391 unsignedp);
5394 if (!insn_data[(int) icode].operand[2].predicate (cstore_op0,
5395 compare_mode))
5396 cstore_op0 = copy_to_mode_reg (compare_mode, cstore_op0);
5398 if (!insn_data[(int) icode].operand[3].predicate (cstore_op1,
5399 compare_mode))
5400 cstore_op1 = copy_to_mode_reg (compare_mode, cstore_op1);
5402 comparison = gen_rtx_fmt_ee (code, result_mode, cstore_op0,
5403 cstore_op1);
5404 subtarget = target;
5406 if (optimize || !(insn_data[(int) icode].operand[0].predicate
5407 (subtarget, result_mode)))
5408 subtarget = gen_reg_rtx (result_mode);
5410 pattern = GEN_FCN (icode) (subtarget, comparison, cstore_op0,
5411 cstore_op1);
5413 if (pattern)
5415 emit_insn (pattern);
5416 return emit_store_flag_1 (target, subtarget, result_mode,
5417 normalizep);
5422 delete_insns_since (last);
5424 /* If optimizing, use different pseudo registers for each insn, instead
5425 of reusing the same pseudo. This leads to better CSE, but slows
5426 down the compiler, since there are more pseudos */
5427 subtarget = (!optimize
5428 && (target_mode == mode)) ? target : NULL_RTX;
5430 /* If we reached here, we can't do this with a scc insn. However, there
5431 are some comparisons that can be done directly. For example, if
5432 this is an equality comparison of integers, we can try to exclusive-or
5433 (or subtract) the two operands and use a recursive call to try the
5434 comparison with zero. Don't do any of these cases if branches are
5435 very cheap. */
5437 if (BRANCH_COST > 0
5438 && GET_MODE_CLASS (mode) == MODE_INT && (code == EQ || code == NE)
5439 && op1 != const0_rtx)
5441 tem = expand_binop (mode, xor_optab, op0, op1, subtarget, 1,
5442 OPTAB_WIDEN);
5444 if (tem == 0)
5445 tem = expand_binop (mode, sub_optab, op0, op1, subtarget, 1,
5446 OPTAB_WIDEN);
5447 if (tem != 0)
5448 tem = emit_store_flag (target, code, tem, const0_rtx,
5449 mode, unsignedp, normalizep);
5450 if (tem == 0)
5451 delete_insns_since (last);
5452 return tem;
5455 /* Some other cases we can do are EQ, NE, LE, and GT comparisons with
5456 the constant zero. Reject all other comparisons at this point. Only
5457 do LE and GT if branches are expensive since they are expensive on
5458 2-operand machines. */
5460 if (BRANCH_COST == 0
5461 || GET_MODE_CLASS (mode) != MODE_INT || op1 != const0_rtx
5462 || (code != EQ && code != NE
5463 && (BRANCH_COST <= 1 || (code != LE && code != GT))))
5464 return 0;
5466 /* See what we need to return. We can only return a 1, -1, or the
5467 sign bit. */
5469 if (normalizep == 0)
5471 if (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
5472 normalizep = STORE_FLAG_VALUE;
5474 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5475 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
5476 == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1)))
5478 else
5479 return 0;
5482 /* Try to put the result of the comparison in the sign bit. Assume we can't
5483 do the necessary operation below. */
5485 tem = 0;
5487 /* To see if A <= 0, compute (A | (A - 1)). A <= 0 iff that result has
5488 the sign bit set. */
5490 if (code == LE)
5492 /* This is destructive, so SUBTARGET can't be OP0. */
5493 if (rtx_equal_p (subtarget, op0))
5494 subtarget = 0;
5496 tem = expand_binop (mode, sub_optab, op0, const1_rtx, subtarget, 0,
5497 OPTAB_WIDEN);
5498 if (tem)
5499 tem = expand_binop (mode, ior_optab, op0, tem, subtarget, 0,
5500 OPTAB_WIDEN);
5503 /* To see if A > 0, compute (((signed) A) << BITS) - A, where BITS is the
5504 number of bits in the mode of OP0, minus one. */
5506 if (code == GT)
5508 if (rtx_equal_p (subtarget, op0))
5509 subtarget = 0;
5511 tem = expand_shift (RSHIFT_EXPR, mode, op0,
5512 size_int (GET_MODE_BITSIZE (mode) - 1),
5513 subtarget, 0);
5514 tem = expand_binop (mode, sub_optab, tem, op0, subtarget, 0,
5515 OPTAB_WIDEN);
5518 if (code == EQ || code == NE)
5520 /* For EQ or NE, one way to do the comparison is to apply an operation
5521 that converts the operand into a positive number if it is nonzero
5522 or zero if it was originally zero. Then, for EQ, we subtract 1 and
5523 for NE we negate. This puts the result in the sign bit. Then we
5524 normalize with a shift, if needed.
5526 Two operations that can do the above actions are ABS and FFS, so try
5527 them. If that doesn't work, and MODE is smaller than a full word,
5528 we can use zero-extension to the wider mode (an unsigned conversion)
5529 as the operation. */
5531 /* Note that ABS doesn't yield a positive number for INT_MIN, but
5532 that is compensated by the subsequent overflow when subtracting
5533 one / negating. */
5535 if (optab_handler (abs_optab, mode)->insn_code != CODE_FOR_nothing)
5536 tem = expand_unop (mode, abs_optab, op0, subtarget, 1);
5537 else if (optab_handler (ffs_optab, mode)->insn_code != CODE_FOR_nothing)
5538 tem = expand_unop (mode, ffs_optab, op0, subtarget, 1);
5539 else if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
5541 tem = convert_modes (word_mode, mode, op0, 1);
5542 mode = word_mode;
5545 if (tem != 0)
5547 if (code == EQ)
5548 tem = expand_binop (mode, sub_optab, tem, const1_rtx, subtarget,
5549 0, OPTAB_WIDEN);
5550 else
5551 tem = expand_unop (mode, neg_optab, tem, subtarget, 0);
5554 /* If we couldn't do it that way, for NE we can "or" the two's complement
5555 of the value with itself. For EQ, we take the one's complement of
5556 that "or", which is an extra insn, so we only handle EQ if branches
5557 are expensive. */
5559 if (tem == 0 && (code == NE || BRANCH_COST > 1))
5561 if (rtx_equal_p (subtarget, op0))
5562 subtarget = 0;
5564 tem = expand_unop (mode, neg_optab, op0, subtarget, 0);
5565 tem = expand_binop (mode, ior_optab, tem, op0, subtarget, 0,
5566 OPTAB_WIDEN);
5568 if (tem && code == EQ)
5569 tem = expand_unop (mode, one_cmpl_optab, tem, subtarget, 0);
5573 if (tem && normalizep)
5574 tem = expand_shift (RSHIFT_EXPR, mode, tem,
5575 size_int (GET_MODE_BITSIZE (mode) - 1),
5576 subtarget, normalizep == 1);
5578 if (tem)
5580 if (GET_MODE (tem) != target_mode)
5582 convert_move (target, tem, 0);
5583 tem = target;
5585 else if (!subtarget)
5587 emit_move_insn (target, tem);
5588 tem = target;
5591 else
5592 delete_insns_since (last);
5594 return tem;
5597 /* Like emit_store_flag, but always succeeds. */
5600 emit_store_flag_force (rtx target, enum rtx_code code, rtx op0, rtx op1,
5601 enum machine_mode mode, int unsignedp, int normalizep)
5603 rtx tem, label;
5605 /* First see if emit_store_flag can do the job. */
5606 tem = emit_store_flag (target, code, op0, op1, mode, unsignedp, normalizep);
5607 if (tem != 0)
5608 return tem;
5610 if (normalizep == 0)
5611 normalizep = 1;
5613 /* If this failed, we have to do this with set/compare/jump/set code. */
5615 if (!REG_P (target)
5616 || reg_mentioned_p (target, op0) || reg_mentioned_p (target, op1))
5617 target = gen_reg_rtx (GET_MODE (target));
5619 emit_move_insn (target, const1_rtx);
5620 label = gen_label_rtx ();
5621 do_compare_rtx_and_jump (op0, op1, code, unsignedp, mode, NULL_RTX,
5622 NULL_RTX, label);
5624 emit_move_insn (target, const0_rtx);
5625 emit_label (label);
5627 return target;
5630 /* Perform possibly multi-word comparison and conditional jump to LABEL
5631 if ARG1 OP ARG2 true where ARG1 and ARG2 are of mode MODE. This is
5632 now a thin wrapper around do_compare_rtx_and_jump. */
5634 static void
5635 do_cmp_and_jump (rtx arg1, rtx arg2, enum rtx_code op, enum machine_mode mode,
5636 rtx label)
5638 int unsignedp = (op == LTU || op == LEU || op == GTU || op == GEU);
5639 do_compare_rtx_and_jump (arg1, arg2, op, unsignedp, mode,
5640 NULL_RTX, NULL_RTX, label);