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