]> gcc.gnu.org Git - gcc.git/blame - gcc/expmed.c
expr.c (emit_single_push_insn): If padding is needed downward...
[gcc.git] / gcc / expmed.c
CommitLineData
44037a66
TG
1/* Medium-level subroutines: convert bit-field store and extract
2 and shifts, multiplies and divides to rtl instructions.
ef58a523 3 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
32214c32 4 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
44037a66 5
1322177d 6This file is part of GCC.
44037a66 7
1322177d
LB
8GCC is free software; you can redistribute it and/or modify it under
9the terms of the GNU General Public License as published by the Free
10Software Foundation; either version 2, or (at your option) any later
11version.
44037a66 12
1322177d
LB
13GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16for more details.
44037a66
TG
17
18You should have received a copy of the GNU General Public License
1322177d
LB
19along with GCC; see the file COPYING. If not, write to the Free
20Software Foundation, 59 Temple Place - Suite 330, Boston, MA
2102111-1307, USA. */
44037a66
TG
22
23
24#include "config.h"
670ee920 25#include "system.h"
4977bab6
ZW
26#include "coretypes.h"
27#include "tm.h"
01198c2f 28#include "toplev.h"
44037a66
TG
29#include "rtl.h"
30#include "tree.h"
6baf1cc8 31#include "tm_p.h"
44037a66 32#include "flags.h"
44037a66
TG
33#include "insn-config.h"
34#include "expr.h"
e78d8e51 35#include "optabs.h"
44037a66
TG
36#include "real.h"
37#include "recog.h"
b0c48229 38#include "langhooks.h"
44037a66 39
770ae6cc
RK
40static void store_fixed_bit_field PARAMS ((rtx, unsigned HOST_WIDE_INT,
41 unsigned HOST_WIDE_INT,
04050c69 42 unsigned HOST_WIDE_INT, rtx));
770ae6cc 43static void store_split_bit_field PARAMS ((rtx, unsigned HOST_WIDE_INT,
04050c69 44 unsigned HOST_WIDE_INT, rtx));
770ae6cc
RK
45static rtx extract_fixed_bit_field PARAMS ((enum machine_mode, rtx,
46 unsigned HOST_WIDE_INT,
47 unsigned HOST_WIDE_INT,
48 unsigned HOST_WIDE_INT,
04050c69 49 rtx, int));
711d877c
KG
50static rtx mask_rtx PARAMS ((enum machine_mode, int,
51 int, int));
52static rtx lshift_value PARAMS ((enum machine_mode, rtx,
53 int, int));
770ae6cc 54static rtx extract_split_bit_field PARAMS ((rtx, unsigned HOST_WIDE_INT,
04050c69 55 unsigned HOST_WIDE_INT, int));
711d877c
KG
56static void do_cmp_and_jump PARAMS ((rtx, rtx, enum rtx_code,
57 enum machine_mode, rtx));
44037a66 58
cc2902df 59/* Nonzero means divides or modulus operations are relatively cheap for
c410d49e 60 powers of two, so don't use branches; emit the operation instead.
44037a66
TG
61 Usually, this will mean that the MD file will emit non-branch
62 sequences. */
63
64static int sdiv_pow2_cheap, smod_pow2_cheap;
65
c7e33f89 66#ifndef SLOW_UNALIGNED_ACCESS
e1565e65 67#define SLOW_UNALIGNED_ACCESS(MODE, ALIGN) STRICT_ALIGNMENT
c7e33f89
RS
68#endif
69
e49a094d
RS
70/* For compilers that support multiple targets with different word sizes,
71 MAX_BITS_PER_WORD contains the biggest value of BITS_PER_WORD. An example
72 is the H8/300(H) compiler. */
73
74#ifndef MAX_BITS_PER_WORD
75#define MAX_BITS_PER_WORD BITS_PER_WORD
76#endif
77
d523b40e
RH
78/* Reduce conditional compilation elsewhere. */
79#ifndef HAVE_insv
a242b083 80#define HAVE_insv 0
d523b40e
RH
81#define CODE_FOR_insv CODE_FOR_nothing
82#define gen_insv(a,b,c,d) NULL_RTX
83#endif
84#ifndef HAVE_extv
a242b083 85#define HAVE_extv 0
d523b40e
RH
86#define CODE_FOR_extv CODE_FOR_nothing
87#define gen_extv(a,b,c,d) NULL_RTX
88#endif
89#ifndef HAVE_extzv
a242b083 90#define HAVE_extzv 0
d523b40e
RH
91#define CODE_FOR_extzv CODE_FOR_nothing
92#define gen_extzv(a,b,c,d) NULL_RTX
93#endif
94
c5c76735
JL
95/* Cost of various pieces of RTL. Note that some of these are indexed by
96 shift count and some by mode. */
819126a6 97static int add_cost, negate_cost, zero_cost;
e49a094d
RS
98static int shift_cost[MAX_BITS_PER_WORD];
99static int shiftadd_cost[MAX_BITS_PER_WORD];
100static int shiftsub_cost[MAX_BITS_PER_WORD];
71af73bb
TG
101static int mul_cost[NUM_MACHINE_MODES];
102static int div_cost[NUM_MACHINE_MODES];
103static int mul_widen_cost[NUM_MACHINE_MODES];
104static int mul_highpart_cost[NUM_MACHINE_MODES];
44037a66 105
44037a66
TG
106void
107init_expmed ()
108{
2acf0fa9 109 rtx reg, shift_insn, shiftadd_insn, shiftsub_insn;
b1ec3c92 110 int dummy;
7963ac37 111 int m;
71af73bb 112 enum machine_mode mode, wider_mode;
44037a66 113
b385aeda
RK
114 start_sequence ();
115
2acf0fa9
DM
116 /* This is "some random pseudo register" for purposes of calling recog
117 to see what insns exist. */
c5c76735 118 reg = gen_rtx_REG (word_mode, 10000);
9ec36da5 119
172a1cb0 120 zero_cost = rtx_cost (const0_rtx, 0);
38a448ca
RH
121 add_cost = rtx_cost (gen_rtx_PLUS (word_mode, reg, reg), SET);
122
123 shift_insn = emit_insn (gen_rtx_SET (VOIDmode, reg,
124 gen_rtx_ASHIFT (word_mode, reg,
125 const0_rtx)));
126
127 shiftadd_insn
128 = emit_insn (gen_rtx_SET (VOIDmode, reg,
129 gen_rtx_PLUS (word_mode,
130 gen_rtx_MULT (word_mode,
131 reg, const0_rtx),
132 reg)));
133
134 shiftsub_insn
135 = emit_insn (gen_rtx_SET (VOIDmode, reg,
136 gen_rtx_MINUS (word_mode,
137 gen_rtx_MULT (word_mode,
138 reg, const0_rtx),
139 reg)));
7963ac37
RK
140
141 init_recog ();
b385aeda
RK
142
143 shift_cost[0] = 0;
144 shiftadd_cost[0] = shiftsub_cost[0] = add_cost;
145
55a6ba9f 146 for (m = 1; m < MAX_BITS_PER_WORD; m++)
7963ac37 147 {
b7c89afe 148 rtx c_int = GEN_INT ((HOST_WIDE_INT) 1 << m);
7963ac37 149 shift_cost[m] = shiftadd_cost[m] = shiftsub_cost[m] = 32000;
b385aeda
RK
150
151 XEXP (SET_SRC (PATTERN (shift_insn)), 1) = GEN_INT (m);
152 if (recog (PATTERN (shift_insn), shift_insn, &dummy) >= 0)
153 shift_cost[m] = rtx_cost (SET_SRC (PATTERN (shift_insn)), SET);
154
b7c89afe 155 XEXP (XEXP (SET_SRC (PATTERN (shiftadd_insn)), 0), 1) = c_int;
b385aeda 156 if (recog (PATTERN (shiftadd_insn), shiftadd_insn, &dummy) >= 0)
dac57de0 157 shiftadd_cost[m] = rtx_cost (SET_SRC (PATTERN (shiftadd_insn)), SET);
b385aeda 158
b7c89afe 159 XEXP (XEXP (SET_SRC (PATTERN (shiftsub_insn)), 0), 1) = c_int;
b385aeda 160 if (recog (PATTERN (shiftsub_insn), shiftsub_insn, &dummy) >= 0)
dac57de0 161 shiftsub_cost[m] = rtx_cost (SET_SRC (PATTERN (shiftsub_insn)), SET);
7963ac37
RK
162 }
163
38a448ca 164 negate_cost = rtx_cost (gen_rtx_NEG (word_mode, reg), SET);
44037a66 165
44037a66 166 sdiv_pow2_cheap
38a448ca 167 = (rtx_cost (gen_rtx_DIV (word_mode, reg, GEN_INT (32)), SET)
b385aeda 168 <= 2 * add_cost);
44037a66 169 smod_pow2_cheap
38a448ca 170 = (rtx_cost (gen_rtx_MOD (word_mode, reg, GEN_INT (32)), SET)
b385aeda 171 <= 2 * add_cost);
44037a66 172
71af73bb
TG
173 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
174 mode != VOIDmode;
175 mode = GET_MODE_WIDER_MODE (mode))
176 {
38a448ca
RH
177 reg = gen_rtx_REG (mode, 10000);
178 div_cost[(int) mode] = rtx_cost (gen_rtx_UDIV (mode, reg, reg), SET);
179 mul_cost[(int) mode] = rtx_cost (gen_rtx_MULT (mode, reg, reg), SET);
71af73bb
TG
180 wider_mode = GET_MODE_WIDER_MODE (mode);
181 if (wider_mode != VOIDmode)
182 {
183 mul_widen_cost[(int) wider_mode]
38a448ca
RH
184 = rtx_cost (gen_rtx_MULT (wider_mode,
185 gen_rtx_ZERO_EXTEND (wider_mode, reg),
186 gen_rtx_ZERO_EXTEND (wider_mode, reg)),
71af73bb
TG
187 SET);
188 mul_highpart_cost[(int) mode]
38a448ca
RH
189 = rtx_cost (gen_rtx_TRUNCATE
190 (mode,
c5c76735
JL
191 gen_rtx_LSHIFTRT (wider_mode,
192 gen_rtx_MULT (wider_mode,
193 gen_rtx_ZERO_EXTEND
194 (wider_mode, reg),
195 gen_rtx_ZERO_EXTEND
196 (wider_mode, reg)),
197 GEN_INT (GET_MODE_BITSIZE (mode)))),
71af73bb
TG
198 SET);
199 }
200 }
201
b385aeda 202 end_sequence ();
44037a66
TG
203}
204
205/* Return an rtx representing minus the value of X.
206 MODE is the intended mode of the result,
207 useful if X is a CONST_INT. */
208
209rtx
210negate_rtx (mode, x)
211 enum machine_mode mode;
212 rtx x;
213{
a39a7484
RK
214 rtx result = simplify_unary_operation (NEG, mode, x, mode);
215
fdb5537f 216 if (result == 0)
a39a7484
RK
217 result = expand_unop (mode, neg_optab, x, NULL_RTX, 0);
218
219 return result;
44037a66 220}
da920570
ZW
221
222/* Report on the availability of insv/extv/extzv and the desired mode
223 of each of their operands. Returns MAX_MACHINE_MODE if HAVE_foo
224 is false; else the mode of the specified operand. If OPNO is -1,
225 all the caller cares about is whether the insn is available. */
226enum machine_mode
227mode_for_extraction (pattern, opno)
228 enum extraction_pattern pattern;
229 int opno;
230{
231 const struct insn_data *data;
232
233 switch (pattern)
234 {
235 case EP_insv:
da920570
ZW
236 if (HAVE_insv)
237 {
238 data = &insn_data[CODE_FOR_insv];
239 break;
240 }
da920570
ZW
241 return MAX_MACHINE_MODE;
242
243 case EP_extv:
da920570
ZW
244 if (HAVE_extv)
245 {
246 data = &insn_data[CODE_FOR_extv];
247 break;
248 }
da920570
ZW
249 return MAX_MACHINE_MODE;
250
251 case EP_extzv:
da920570
ZW
252 if (HAVE_extzv)
253 {
254 data = &insn_data[CODE_FOR_extzv];
255 break;
256 }
da920570 257 return MAX_MACHINE_MODE;
a242b083
ZW
258
259 default:
260 abort ();
da920570
ZW
261 }
262
263 if (opno == -1)
264 return VOIDmode;
265
266 /* Everyone who uses this function used to follow it with
267 if (result == VOIDmode) result = word_mode; */
268 if (data->operand[opno].mode == VOIDmode)
269 return word_mode;
270 return data->operand[opno].mode;
271}
272
44037a66
TG
273\f
274/* Generate code to store value from rtx VALUE
275 into a bit-field within structure STR_RTX
276 containing BITSIZE bits starting at bit BITNUM.
277 FIELDMODE is the machine-mode of the FIELD_DECL node for this field.
0e805f81 278 ALIGN is the alignment that STR_RTX is known to have.
44037a66
TG
279 TOTAL_SIZE is the size of the structure in bytes, or -1 if varying. */
280
281/* ??? Note that there are two different ideas here for how
282 to determine the size to count bits within, for a register.
283 One is BITS_PER_WORD, and the other is the size of operand 3
0d8e55d8
JL
284 of the insv pattern.
285
286 If operand 3 of the insv pattern is VOIDmode, then we will use BITS_PER_WORD
287 else, we use the mode of operand 3. */
44037a66
TG
288
289rtx
04050c69 290store_bit_field (str_rtx, bitsize, bitnum, fieldmode, value, total_size)
44037a66 291 rtx str_rtx;
770ae6cc
RK
292 unsigned HOST_WIDE_INT bitsize;
293 unsigned HOST_WIDE_INT bitnum;
44037a66
TG
294 enum machine_mode fieldmode;
295 rtx value;
770ae6cc 296 HOST_WIDE_INT total_size;
44037a66 297{
770ae6cc
RK
298 unsigned int unit
299 = (GET_CODE (str_rtx) == MEM) ? BITS_PER_UNIT : BITS_PER_WORD;
300 unsigned HOST_WIDE_INT offset = bitnum / unit;
301 unsigned HOST_WIDE_INT bitpos = bitnum % unit;
b3694847 302 rtx op0 = str_rtx;
420e7dfa 303 int byte_offset;
da920570 304
a242b083 305 enum machine_mode op_mode = mode_for_extraction (EP_insv, 3);
44037a66 306
44037a66
TG
307 /* Discount the part of the structure before the desired byte.
308 We need to know how many bytes are safe to reference after it. */
309 if (total_size >= 0)
310 total_size -= (bitpos / BIGGEST_ALIGNMENT
311 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
312
313 while (GET_CODE (op0) == SUBREG)
314 {
315 /* The following line once was done only if WORDS_BIG_ENDIAN,
316 but I think that is a mistake. WORDS_BIG_ENDIAN is
317 meaningful at a much higher level; when structures are copied
318 between memory and regs, the higher-numbered regs
319 always get higher addresses. */
ddef6bc7 320 offset += (SUBREG_BYTE (op0) / UNITS_PER_WORD);
44037a66
TG
321 /* We used to adjust BITPOS here, but now we do the whole adjustment
322 right after the loop. */
323 op0 = SUBREG_REG (op0);
324 }
325
44037a66
TG
326 value = protect_from_queue (value, 0);
327
328 if (flag_force_mem)
32b069d3
RH
329 {
330 int old_generating_concat_p = generating_concat_p;
331 generating_concat_p = 0;
332 value = force_not_mem (value);
333 generating_concat_p = old_generating_concat_p;
334 }
44037a66 335
308ecea0
RH
336 /* If the target is a register, overwriting the entire object, or storing
337 a full-word or multi-word field can be done with just a SUBREG.
338
339 If the target is memory, storing any naturally aligned field can be
340 done with a simple store. For targets that support fast unaligned
341 memory, any naturally sized, unit aligned field can be done directly. */
c410d49e 342
420e7dfa
DN
343 byte_offset = (bitnum % BITS_PER_WORD) / BITS_PER_UNIT
344 + (offset * UNITS_PER_WORD);
345
57bfa49a 346 if (bitpos == 0
f21bdd05 347 && bitsize == GET_MODE_BITSIZE (fieldmode)
ea9ea008 348 && (GET_CODE (op0) != MEM
420e7dfa 349 ? ((GET_MODE_SIZE (fieldmode) >= UNITS_PER_WORD
ea9ea008 350 || GET_MODE_SIZE (GET_MODE (op0)) == GET_MODE_SIZE (fieldmode))
0fb7aeda 351 && byte_offset % GET_MODE_SIZE (fieldmode) == 0)
04050c69 352 : (! SLOW_UNALIGNED_ACCESS (fieldmode, MEM_ALIGN (op0))
ea9ea008 353 || (offset * BITS_PER_UNIT % bitsize == 0
04050c69 354 && MEM_ALIGN (op0) % GET_MODE_BITSIZE (fieldmode) == 0))))
44037a66 355 {
44037a66 356 if (GET_MODE (op0) != fieldmode)
c7e33f89 357 {
d006aa54
RH
358 if (GET_CODE (op0) == SUBREG)
359 {
360 if (GET_MODE (SUBREG_REG (op0)) == fieldmode
361 || GET_MODE_CLASS (fieldmode) == MODE_INT
362 || GET_MODE_CLASS (fieldmode) == MODE_PARTIAL_INT)
363 op0 = SUBREG_REG (op0);
364 else
365 /* Else we've got some float mode source being extracted into
366 a different float mode destination -- this combination of
367 subregs results in Severe Tire Damage. */
368 abort ();
369 }
c7e33f89 370 if (GET_CODE (op0) == REG)
420e7dfa 371 op0 = gen_rtx_SUBREG (fieldmode, op0, byte_offset);
c7e33f89 372 else
f4ef873c 373 op0 = adjust_address (op0, fieldmode, offset);
c7e33f89 374 }
44037a66
TG
375 emit_move_insn (op0, value);
376 return value;
377 }
378
a8ca7756
JW
379 /* Make sure we are playing with integral modes. Pun with subregs
380 if we aren't. This must come after the entire register case above,
381 since that case is valid for any mode. The following cases are only
382 valid for integral modes. */
383 {
384 enum machine_mode imode = int_mode_for_mode (GET_MODE (op0));
385 if (imode != GET_MODE (op0))
386 {
387 if (GET_CODE (op0) == MEM)
f4ef873c 388 op0 = adjust_address (op0, imode, 0);
a8ca7756
JW
389 else if (imode != BLKmode)
390 op0 = gen_lowpart (imode, op0);
391 else
392 abort ();
393 }
394 }
395
4e9bb42b
AH
396 /* We may be accessing data outside the field, which means
397 we can alias adjacent data. */
398 if (GET_CODE (op0) == MEM)
399 {
400 op0 = shallow_copy_rtx (op0);
401 set_mem_alias_set (op0, 0);
402 set_mem_expr (op0, 0);
403 }
404
57bfa49a
RZ
405 /* If OP0 is a register, BITPOS must count within a word.
406 But as we have it, it counts within whatever size OP0 now has.
407 On a bigendian machine, these are not the same, so convert. */
408 if (BYTES_BIG_ENDIAN
409 && GET_CODE (op0) != MEM
410 && unit > GET_MODE_BITSIZE (GET_MODE (op0)))
411 bitpos += unit - GET_MODE_BITSIZE (GET_MODE (op0));
412
44037a66
TG
413 /* Storing an lsb-aligned field in a register
414 can be done with a movestrict instruction. */
415
416 if (GET_CODE (op0) != MEM
f76b9db2 417 && (BYTES_BIG_ENDIAN ? bitpos + bitsize == unit : bitpos == 0)
44037a66 418 && bitsize == GET_MODE_BITSIZE (fieldmode)
5e4900c7
JW
419 && (movstrict_optab->handlers[(int) fieldmode].insn_code
420 != CODE_FOR_nothing))
44037a66 421 {
5e4900c7
JW
422 int icode = movstrict_optab->handlers[(int) fieldmode].insn_code;
423
44037a66
TG
424 /* Get appropriate low part of the value being stored. */
425 if (GET_CODE (value) == CONST_INT || GET_CODE (value) == REG)
426 value = gen_lowpart (fieldmode, value);
427 else if (!(GET_CODE (value) == SYMBOL_REF
428 || GET_CODE (value) == LABEL_REF
429 || GET_CODE (value) == CONST))
430 value = convert_to_mode (fieldmode, value, 0);
431
5e4900c7
JW
432 if (! (*insn_data[icode].operand[1].predicate) (value, fieldmode))
433 value = copy_to_mode_reg (fieldmode, value);
434
435 if (GET_CODE (op0) == SUBREG)
44037a66 436 {
5e4900c7
JW
437 if (GET_MODE (SUBREG_REG (op0)) == fieldmode
438 || GET_MODE_CLASS (fieldmode) == MODE_INT
439 || GET_MODE_CLASS (fieldmode) == MODE_PARTIAL_INT)
440 op0 = SUBREG_REG (op0);
441 else
442 /* Else we've got some float mode source being extracted into
443 a different float mode destination -- this combination of
444 subregs results in Severe Tire Damage. */
445 abort ();
446 }
470032d7 447
5e4900c7 448 emit_insn (GEN_FCN (icode)
ddef6bc7
JJ
449 (gen_rtx_SUBREG (fieldmode, op0,
450 (bitnum % BITS_PER_WORD) / BITS_PER_UNIT
451 + (offset * UNITS_PER_WORD)),
452 value));
470032d7 453
44037a66
TG
454 return value;
455 }
456
457 /* Handle fields bigger than a word. */
458
459 if (bitsize > BITS_PER_WORD)
460 {
461 /* Here we transfer the words of the field
462 in the order least significant first.
463 This is because the most significant word is the one which may
ad83e87b
PB
464 be less than full.
465 However, only do that if the value is not BLKmode. */
466
770ae6cc
RK
467 unsigned int backwards = WORDS_BIG_ENDIAN && fieldmode != BLKmode;
468 unsigned int nwords = (bitsize + (BITS_PER_WORD - 1)) / BITS_PER_WORD;
469 unsigned int i;
44037a66
TG
470
471 /* This is the mode we must force value to, so that there will be enough
472 subwords to extract. Note that fieldmode will often (always?) be
473 VOIDmode, because that is what store_field uses to indicate that this
474 is a bit field, but passing VOIDmode to operand_subword_force will
475 result in an abort. */
df7978d9 476 fieldmode = smallest_mode_for_size (nwords * BITS_PER_WORD, MODE_INT);
44037a66
TG
477
478 for (i = 0; i < nwords; i++)
479 {
ad83e87b
PB
480 /* If I is 0, use the low-order word in both field and target;
481 if I is 1, use the next to lowest word; and so on. */
770ae6cc
RK
482 unsigned int wordnum = (backwards ? nwords - i - 1 : i);
483 unsigned int bit_offset = (backwards
04050c69
RK
484 ? MAX ((int) bitsize - ((int) i + 1)
485 * BITS_PER_WORD,
486 0)
487 : (int) i * BITS_PER_WORD);
770ae6cc 488
44037a66
TG
489 store_bit_field (op0, MIN (BITS_PER_WORD,
490 bitsize - i * BITS_PER_WORD),
491 bitnum + bit_offset, word_mode,
b3487765
RS
492 operand_subword_force (value, wordnum,
493 (GET_MODE (value) == VOIDmode
494 ? fieldmode
495 : GET_MODE (value))),
04050c69 496 total_size);
44037a66
TG
497 }
498 return value;
499 }
500
501 /* From here on we can assume that the field to be stored in is
502 a full-word (whatever type that is), since it is shorter than a word. */
503
504 /* OFFSET is the number of words or bytes (UNIT says which)
505 from STR_RTX to the first word or byte containing part of the field. */
506
470032d7 507 if (GET_CODE (op0) != MEM)
44037a66
TG
508 {
509 if (offset != 0
510 || GET_MODE_SIZE (GET_MODE (op0)) > UNITS_PER_WORD)
470032d7
RH
511 {
512 if (GET_CODE (op0) != REG)
7be7a07d
JW
513 {
514 /* Since this is a destination (lvalue), we can't copy it to a
515 pseudo. We can trivially remove a SUBREG that does not
516 change the size of the operand. Such a SUBREG may have been
517 added above. Otherwise, abort. */
518 if (GET_CODE (op0) == SUBREG
519 && (GET_MODE_SIZE (GET_MODE (op0))
520 == GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0)))))
521 op0 = SUBREG_REG (op0);
522 else
523 abort ();
524 }
470032d7 525 op0 = gen_rtx_SUBREG (mode_for_size (BITS_PER_WORD, MODE_INT, 0),
ddef6bc7 526 op0, (offset * UNITS_PER_WORD));
470032d7 527 }
44037a66
TG
528 offset = 0;
529 }
530 else
04050c69 531 op0 = protect_from_queue (op0, 1);
44037a66 532
2305bcad
JW
533 /* If VALUE is a floating-point mode, access it as an integer of the
534 corresponding size. This can occur on a machine with 64 bit registers
535 that uses SFmode for float. This can also occur for unaligned float
536 structure fields. */
32b069d3
RH
537 if (GET_MODE_CLASS (GET_MODE (value)) != MODE_INT
538 && GET_MODE_CLASS (GET_MODE (value)) != MODE_PARTIAL_INT)
7ffc9761
R
539 value = gen_lowpart ((GET_MODE (value) == VOIDmode
540 ? word_mode : int_mode_for_mode (GET_MODE (value))),
541 value);
2305bcad 542
44037a66
TG
543 /* Now OFFSET is nonzero only if OP0 is memory
544 and is therefore always measured in bytes. */
545
a242b083 546 if (HAVE_insv
1d269b0c 547 && GET_MODE (value) != BLKmode
44037a66
TG
548 && !(bitsize == 1 && GET_CODE (value) == CONST_INT)
549 /* Ensure insv's size is wide enough for this field. */
a242b083 550 && (GET_MODE_BITSIZE (op_mode) >= bitsize)
39e0911f 551 && ! ((GET_CODE (op0) == REG || GET_CODE (op0) == SUBREG)
a242b083 552 && (bitsize + bitpos > GET_MODE_BITSIZE (op_mode))))
44037a66
TG
553 {
554 int xbitpos = bitpos;
555 rtx value1;
556 rtx xop0 = op0;
557 rtx last = get_last_insn ();
558 rtx pat;
d523b40e 559 enum machine_mode maxmode = mode_for_extraction (EP_insv, 3);
44037a66 560 int save_volatile_ok = volatile_ok;
0d8e55d8 561
44037a66
TG
562 volatile_ok = 1;
563
4dbebeb2
JW
564 /* If this machine's insv can only insert into a register, copy OP0
565 into a register and save it back later. */
566 /* This used to check flag_force_mem, but that was a serious
567 de-optimization now that flag_force_mem is enabled by -O2. */
44037a66 568 if (GET_CODE (op0) == MEM
a995e389 569 && ! ((*insn_data[(int) CODE_FOR_insv].operand[0].predicate)
4dbebeb2 570 (op0, VOIDmode)))
44037a66
TG
571 {
572 rtx tempreg;
573 enum machine_mode bestmode;
574
575 /* Get the mode to use for inserting into this field. If OP0 is
576 BLKmode, get the smallest mode consistent with the alignment. If
577 OP0 is a non-BLKmode object that is no wider than MAXMODE, use its
578 mode. Otherwise, use the smallest mode containing the field. */
579
580 if (GET_MODE (op0) == BLKmode
581 || GET_MODE_SIZE (GET_MODE (op0)) > GET_MODE_SIZE (maxmode))
582 bestmode
04050c69 583 = get_best_mode (bitsize, bitnum, MEM_ALIGN (op0), maxmode,
717702e6 584 MEM_VOLATILE_P (op0));
44037a66
TG
585 else
586 bestmode = GET_MODE (op0);
587
bd5d175a 588 if (bestmode == VOIDmode
04050c69
RK
589 || (SLOW_UNALIGNED_ACCESS (bestmode, MEM_ALIGN (op0))
590 && GET_MODE_BITSIZE (bestmode) > MEM_ALIGN (op0)))
44037a66
TG
591 goto insv_loses;
592
c410d49e 593 /* Adjust address to point to the containing unit of that mode.
04050c69 594 Compute offset as multiple of this unit, counting in bytes. */
44037a66 595 unit = GET_MODE_BITSIZE (bestmode);
44037a66
TG
596 offset = (bitnum / unit) * GET_MODE_SIZE (bestmode);
597 bitpos = bitnum % unit;
f4ef873c 598 op0 = adjust_address (op0, bestmode, offset);
44037a66 599
0e805f81
RK
600 /* Fetch that unit, store the bitfield in it, then store
601 the unit. */
44037a66
TG
602 tempreg = copy_to_reg (op0);
603 store_bit_field (tempreg, bitsize, bitpos, fieldmode, value,
04050c69 604 total_size);
44037a66
TG
605 emit_move_insn (op0, tempreg);
606 return value;
607 }
608 volatile_ok = save_volatile_ok;
609
610 /* Add OFFSET into OP0's address. */
611 if (GET_CODE (xop0) == MEM)
f4ef873c 612 xop0 = adjust_address (xop0, byte_mode, offset);
44037a66
TG
613
614 /* If xop0 is a register, we need it in MAXMODE
615 to make it acceptable to the format of insv. */
616 if (GET_CODE (xop0) == SUBREG)
bac7cdfd
DE
617 /* We can't just change the mode, because this might clobber op0,
618 and we will need the original value of op0 if insv fails. */
ddef6bc7 619 xop0 = gen_rtx_SUBREG (maxmode, SUBREG_REG (xop0), SUBREG_BYTE (xop0));
44037a66 620 if (GET_CODE (xop0) == REG && GET_MODE (xop0) != maxmode)
38a448ca 621 xop0 = gen_rtx_SUBREG (maxmode, xop0, 0);
44037a66
TG
622
623 /* On big-endian machines, we count bits from the most significant.
624 If the bit field insn does not, we must invert. */
625
f76b9db2
ILT
626 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
627 xbitpos = unit - bitsize - xbitpos;
628
44037a66
TG
629 /* We have been counting XBITPOS within UNIT.
630 Count instead within the size of the register. */
f76b9db2 631 if (BITS_BIG_ENDIAN && GET_CODE (xop0) != MEM)
44037a66 632 xbitpos += GET_MODE_BITSIZE (maxmode) - unit;
f76b9db2 633
44037a66
TG
634 unit = GET_MODE_BITSIZE (maxmode);
635
636 /* Convert VALUE to maxmode (which insv insn wants) in VALUE1. */
637 value1 = value;
638 if (GET_MODE (value) != maxmode)
639 {
640 if (GET_MODE_BITSIZE (GET_MODE (value)) >= bitsize)
641 {
642 /* Optimization: Don't bother really extending VALUE
f5df292e
RS
643 if it has all the bits we will actually use. However,
644 if we must narrow it, be sure we do it correctly. */
44037a66 645
f5df292e 646 if (GET_MODE_SIZE (GET_MODE (value)) < GET_MODE_SIZE (maxmode))
c410d49e
EC
647 {
648 rtx tmp;
649
650 tmp = simplify_subreg (maxmode, value1, GET_MODE (value), 0);
651 if (! tmp)
652 tmp = simplify_gen_subreg (maxmode,
653 force_reg (GET_MODE (value),
654 value1),
655 GET_MODE (value), 0);
656 value1 = tmp;
657 }
f5df292e
RS
658 else
659 value1 = gen_lowpart (maxmode, value1);
44037a66 660 }
69107307 661 else if (GET_CODE (value) == CONST_INT)
2496c7bd 662 value1 = gen_int_mode (INTVAL (value), maxmode);
44037a66
TG
663 else if (!CONSTANT_P (value))
664 /* Parse phase is supposed to make VALUE's data type
665 match that of the component reference, which is a type
666 at least as wide as the field; so VALUE should have
667 a mode that corresponds to that type. */
668 abort ();
669 }
670
671 /* If this machine's insv insists on a register,
672 get VALUE1 into a register. */
a995e389 673 if (! ((*insn_data[(int) CODE_FOR_insv].operand[3].predicate)
44037a66
TG
674 (value1, maxmode)))
675 value1 = force_reg (maxmode, value1);
676
b1ec3c92 677 pat = gen_insv (xop0, GEN_INT (bitsize), GEN_INT (xbitpos), value1);
44037a66
TG
678 if (pat)
679 emit_insn (pat);
680 else
0fb7aeda 681 {
44037a66 682 delete_insns_since (last);
04050c69 683 store_fixed_bit_field (op0, offset, bitsize, bitpos, value);
44037a66
TG
684 }
685 }
686 else
687 insv_loses:
44037a66 688 /* Insv is not available; store using shifts and boolean ops. */
04050c69 689 store_fixed_bit_field (op0, offset, bitsize, bitpos, value);
44037a66
TG
690 return value;
691}
692\f
693/* Use shifts and boolean operations to store VALUE
694 into a bit field of width BITSIZE
695 in a memory location specified by OP0 except offset by OFFSET bytes.
696 (OFFSET must be 0 if OP0 is a register.)
697 The field starts at position BITPOS within the byte.
698 (If OP0 is a register, it may be a full word or a narrower mode,
699 but BITPOS still counts within a full word,
700 which is significant on bigendian machines.)
44037a66
TG
701
702 Note that protect_from_queue has already been done on OP0 and VALUE. */
703
704static void
04050c69 705store_fixed_bit_field (op0, offset, bitsize, bitpos, value)
b3694847 706 rtx op0;
770ae6cc 707 unsigned HOST_WIDE_INT offset, bitsize, bitpos;
b3694847 708 rtx value;
44037a66 709{
b3694847 710 enum machine_mode mode;
770ae6cc 711 unsigned int total_bits = BITS_PER_WORD;
44037a66
TG
712 rtx subtarget, temp;
713 int all_zero = 0;
714 int all_one = 0;
715
44037a66
TG
716 /* There is a case not handled here:
717 a structure with a known alignment of just a halfword
718 and a field split across two aligned halfwords within the structure.
719 Or likewise a structure with a known alignment of just a byte
720 and a field split across two bytes.
721 Such cases are not supposed to be able to occur. */
722
723 if (GET_CODE (op0) == REG || GET_CODE (op0) == SUBREG)
724 {
725 if (offset != 0)
726 abort ();
727 /* Special treatment for a bit field split across two registers. */
728 if (bitsize + bitpos > BITS_PER_WORD)
729 {
04050c69 730 store_split_bit_field (op0, bitsize, bitpos, value);
44037a66
TG
731 return;
732 }
733 }
734 else
735 {
736 /* Get the proper mode to use for this field. We want a mode that
737 includes the entire field. If such a mode would be larger than
c410d49e 738 a word, we won't be doing the extraction the normal way.
053a35af 739 We don't want a mode bigger than the destination. */
44037a66 740
053a35af
AH
741 mode = GET_MODE (op0);
742 if (GET_MODE_BITSIZE (mode) == 0
0fb7aeda
KH
743 || GET_MODE_BITSIZE (mode) > GET_MODE_BITSIZE (word_mode))
744 mode = word_mode;
44037a66 745 mode = get_best_mode (bitsize, bitpos + offset * BITS_PER_UNIT,
04050c69 746 MEM_ALIGN (op0), mode, MEM_VOLATILE_P (op0));
44037a66
TG
747
748 if (mode == VOIDmode)
749 {
750 /* The only way this should occur is if the field spans word
751 boundaries. */
04050c69
RK
752 store_split_bit_field (op0, bitsize, bitpos + offset * BITS_PER_UNIT,
753 value);
44037a66
TG
754 return;
755 }
756
757 total_bits = GET_MODE_BITSIZE (mode);
758
3bd98790 759 /* Make sure bitpos is valid for the chosen mode. Adjust BITPOS to
38e01259 760 be in the range 0 to total_bits-1, and put any excess bytes in
3bd98790
JW
761 OFFSET. */
762 if (bitpos >= total_bits)
763 {
764 offset += (bitpos / total_bits) * (total_bits / BITS_PER_UNIT);
765 bitpos -= ((bitpos / total_bits) * (total_bits / BITS_PER_UNIT)
766 * BITS_PER_UNIT);
767 }
768
44037a66
TG
769 /* Get ref to an aligned byte, halfword, or word containing the field.
770 Adjust BITPOS to be position within a word,
771 and OFFSET to be the offset of that word.
772 Then alter OP0 to refer to that word. */
773 bitpos += (offset % (total_bits / BITS_PER_UNIT)) * BITS_PER_UNIT;
774 offset -= (offset % (total_bits / BITS_PER_UNIT));
f4ef873c 775 op0 = adjust_address (op0, mode, offset);
44037a66
TG
776 }
777
778 mode = GET_MODE (op0);
779
780 /* Now MODE is either some integral mode for a MEM as OP0,
781 or is a full-word for a REG as OP0. TOTAL_BITS corresponds.
782 The bit field is contained entirely within OP0.
783 BITPOS is the starting bit number within OP0.
784 (OP0's mode may actually be narrower than MODE.) */
785
f76b9db2
ILT
786 if (BYTES_BIG_ENDIAN)
787 /* BITPOS is the distance between our msb
788 and that of the containing datum.
789 Convert it to the distance from the lsb. */
790 bitpos = total_bits - bitsize - bitpos;
44037a66 791
44037a66
TG
792 /* Now BITPOS is always the distance between our lsb
793 and that of OP0. */
794
795 /* Shift VALUE left by BITPOS bits. If VALUE is not constant,
796 we must first convert its mode to MODE. */
797
798 if (GET_CODE (value) == CONST_INT)
799 {
b3694847 800 HOST_WIDE_INT v = INTVAL (value);
44037a66 801
b1ec3c92
CH
802 if (bitsize < HOST_BITS_PER_WIDE_INT)
803 v &= ((HOST_WIDE_INT) 1 << bitsize) - 1;
44037a66
TG
804
805 if (v == 0)
806 all_zero = 1;
b1ec3c92
CH
807 else if ((bitsize < HOST_BITS_PER_WIDE_INT
808 && v == ((HOST_WIDE_INT) 1 << bitsize) - 1)
809 || (bitsize == HOST_BITS_PER_WIDE_INT && v == -1))
44037a66
TG
810 all_one = 1;
811
812 value = lshift_value (mode, value, bitpos, bitsize);
813 }
814 else
815 {
816 int must_and = (GET_MODE_BITSIZE (GET_MODE (value)) != bitsize
817 && bitpos + bitsize != GET_MODE_BITSIZE (mode));
818
819 if (GET_MODE (value) != mode)
820 {
44037a66
TG
821 if ((GET_CODE (value) == REG || GET_CODE (value) == SUBREG)
822 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (value)))
823 value = gen_lowpart (mode, value);
824 else
825 value = convert_to_mode (mode, value, 1);
826 }
827
828 if (must_and)
829 value = expand_binop (mode, and_optab, value,
830 mask_rtx (mode, 0, bitsize, 0),
b1ec3c92 831 NULL_RTX, 1, OPTAB_LIB_WIDEN);
44037a66
TG
832 if (bitpos > 0)
833 value = expand_shift (LSHIFT_EXPR, mode, value,
b1ec3c92 834 build_int_2 (bitpos, 0), NULL_RTX, 1);
44037a66
TG
835 }
836
837 /* Now clear the chosen bits in OP0,
838 except that if VALUE is -1 we need not bother. */
839
840 subtarget = (GET_CODE (op0) == REG || ! flag_force_mem) ? op0 : 0;
841
842 if (! all_one)
843 {
844 temp = expand_binop (mode, and_optab, op0,
845 mask_rtx (mode, bitpos, bitsize, 1),
846 subtarget, 1, OPTAB_LIB_WIDEN);
847 subtarget = temp;
848 }
849 else
850 temp = op0;
851
852 /* Now logical-or VALUE into OP0, unless it is zero. */
853
854 if (! all_zero)
855 temp = expand_binop (mode, ior_optab, temp, value,
856 subtarget, 1, OPTAB_LIB_WIDEN);
857 if (op0 != temp)
858 emit_move_insn (op0, temp);
859}
860\f
06c94bce 861/* Store a bit field that is split across multiple accessible memory objects.
44037a66 862
06c94bce 863 OP0 is the REG, SUBREG or MEM rtx for the first of the objects.
44037a66
TG
864 BITSIZE is the field width; BITPOS the position of its first bit
865 (within the word).
06c94bce 866 VALUE is the value to store.
06c94bce
RS
867
868 This does not yet handle fields wider than BITS_PER_WORD. */
44037a66
TG
869
870static void
04050c69 871store_split_bit_field (op0, bitsize, bitpos, value)
44037a66 872 rtx op0;
770ae6cc 873 unsigned HOST_WIDE_INT bitsize, bitpos;
44037a66 874 rtx value;
44037a66 875{
770ae6cc
RK
876 unsigned int unit;
877 unsigned int bitsdone = 0;
4ee16841 878
0eb61c19
DE
879 /* Make sure UNIT isn't larger than BITS_PER_WORD, we can only handle that
880 much at a time. */
4ee16841
DE
881 if (GET_CODE (op0) == REG || GET_CODE (op0) == SUBREG)
882 unit = BITS_PER_WORD;
883 else
04050c69 884 unit = MIN (MEM_ALIGN (op0), BITS_PER_WORD);
e54d80d0 885
3d709ff0
RS
886 /* If VALUE is a constant other than a CONST_INT, get it into a register in
887 WORD_MODE. If we can do this using gen_lowpart_common, do so. Note
888 that VALUE might be a floating-point constant. */
44037a66 889 if (CONSTANT_P (value) && GET_CODE (value) != CONST_INT)
3d709ff0
RS
890 {
891 rtx word = gen_lowpart_common (word_mode, value);
892
bc8a0e39 893 if (word && (value != word))
3d709ff0
RS
894 value = word;
895 else
896 value = gen_lowpart_common (word_mode,
d01bc862
DE
897 force_reg (GET_MODE (value) != VOIDmode
898 ? GET_MODE (value)
899 : word_mode, value));
3d709ff0 900 }
956d6950
JL
901 else if (GET_CODE (value) == ADDRESSOF)
902 value = copy_to_reg (value);
44037a66 903
06c94bce 904 while (bitsdone < bitsize)
44037a66 905 {
770ae6cc 906 unsigned HOST_WIDE_INT thissize;
06c94bce 907 rtx part, word;
770ae6cc
RK
908 unsigned HOST_WIDE_INT thispos;
909 unsigned HOST_WIDE_INT offset;
44037a66 910
06c94bce
RS
911 offset = (bitpos + bitsdone) / unit;
912 thispos = (bitpos + bitsdone) % unit;
44037a66 913
0eb61c19
DE
914 /* THISSIZE must not overrun a word boundary. Otherwise,
915 store_fixed_bit_field will call us again, and we will mutually
916 recurse forever. */
917 thissize = MIN (bitsize - bitsdone, BITS_PER_WORD);
918 thissize = MIN (thissize, unit - thispos);
44037a66 919
f76b9db2
ILT
920 if (BYTES_BIG_ENDIAN)
921 {
37811a73
RK
922 int total_bits;
923
924 /* We must do an endian conversion exactly the same way as it is
925 done in extract_bit_field, so that the two calls to
926 extract_fixed_bit_field will have comparable arguments. */
1d269b0c 927 if (GET_CODE (value) != MEM || GET_MODE (value) == BLKmode)
37811a73
RK
928 total_bits = BITS_PER_WORD;
929 else
930 total_bits = GET_MODE_BITSIZE (GET_MODE (value));
931
f76b9db2
ILT
932 /* Fetch successively less significant portions. */
933 if (GET_CODE (value) == CONST_INT)
934 part = GEN_INT (((unsigned HOST_WIDE_INT) (INTVAL (value))
935 >> (bitsize - bitsdone - thissize))
936 & (((HOST_WIDE_INT) 1 << thissize) - 1));
937 else
938 /* The args are chosen so that the last part includes the
939 lsb. Give extract_bit_field the value it needs (with
04050c69
RK
940 endianness compensation) to fetch the piece we want. */
941 part = extract_fixed_bit_field (word_mode, value, 0, thissize,
942 total_bits - bitsize + bitsdone,
943 NULL_RTX, 1);
f76b9db2 944 }
06c94bce 945 else
f76b9db2
ILT
946 {
947 /* Fetch successively more significant portions. */
948 if (GET_CODE (value) == CONST_INT)
949 part = GEN_INT (((unsigned HOST_WIDE_INT) (INTVAL (value))
950 >> bitsdone)
951 & (((HOST_WIDE_INT) 1 << thissize) - 1));
952 else
04050c69
RK
953 part = extract_fixed_bit_field (word_mode, value, 0, thissize,
954 bitsdone, NULL_RTX, 1);
f76b9db2 955 }
44037a66 956
06c94bce 957 /* If OP0 is a register, then handle OFFSET here.
5f57dff0
JW
958
959 When handling multiword bitfields, extract_bit_field may pass
960 down a word_mode SUBREG of a larger REG for a bitfield that actually
961 crosses a word boundary. Thus, for a SUBREG, we must find
962 the current word starting from the base register. */
963 if (GET_CODE (op0) == SUBREG)
964 {
ddef6bc7
JJ
965 int word_offset = (SUBREG_BYTE (op0) / UNITS_PER_WORD) + offset;
966 word = operand_subword_force (SUBREG_REG (op0), word_offset,
4ee16841 967 GET_MODE (SUBREG_REG (op0)));
5f57dff0
JW
968 offset = 0;
969 }
970 else if (GET_CODE (op0) == REG)
06c94bce 971 {
4ee16841 972 word = operand_subword_force (op0, offset, GET_MODE (op0));
06c94bce
RS
973 offset = 0;
974 }
975 else
976 word = op0;
44037a66 977
0eb61c19
DE
978 /* OFFSET is in UNITs, and UNIT is in bits.
979 store_fixed_bit_field wants offset in bytes. */
04050c69
RK
980 store_fixed_bit_field (word, offset * unit / BITS_PER_UNIT, thissize,
981 thispos, part);
06c94bce
RS
982 bitsdone += thissize;
983 }
44037a66
TG
984}
985\f
986/* Generate code to extract a byte-field from STR_RTX
987 containing BITSIZE bits, starting at BITNUM,
988 and put it in TARGET if possible (if TARGET is nonzero).
989 Regardless of TARGET, we return the rtx for where the value is placed.
990 It may be a QUEUED.
991
992 STR_RTX is the structure containing the byte (a REG or MEM).
993 UNSIGNEDP is nonzero if this is an unsigned bit field.
994 MODE is the natural mode of the field value once extracted.
995 TMODE is the mode the caller would like the value to have;
996 but the value may be returned with type MODE instead.
997
44037a66
TG
998 TOTAL_SIZE is the size in bytes of the containing structure,
999 or -1 if varying.
1000
1001 If a TARGET is specified and we can store in it at no extra cost,
1002 we do so, and return TARGET.
1003 Otherwise, we return a REG of mode TMODE or MODE, with TMODE preferred
1004 if they are equally easy. */
1005
1006rtx
1007extract_bit_field (str_rtx, bitsize, bitnum, unsignedp,
04050c69 1008 target, mode, tmode, total_size)
44037a66 1009 rtx str_rtx;
770ae6cc
RK
1010 unsigned HOST_WIDE_INT bitsize;
1011 unsigned HOST_WIDE_INT bitnum;
44037a66
TG
1012 int unsignedp;
1013 rtx target;
1014 enum machine_mode mode, tmode;
770ae6cc 1015 HOST_WIDE_INT total_size;
44037a66 1016{
770ae6cc
RK
1017 unsigned int unit
1018 = (GET_CODE (str_rtx) == MEM) ? BITS_PER_UNIT : BITS_PER_WORD;
1019 unsigned HOST_WIDE_INT offset = bitnum / unit;
1020 unsigned HOST_WIDE_INT bitpos = bitnum % unit;
b3694847 1021 rtx op0 = str_rtx;
44037a66
TG
1022 rtx spec_target = target;
1023 rtx spec_target_subreg = 0;
3306eb80 1024 enum machine_mode int_mode;
a242b083
ZW
1025 enum machine_mode extv_mode = mode_for_extraction (EP_extv, 0);
1026 enum machine_mode extzv_mode = mode_for_extraction (EP_extzv, 0);
e98f90d3
GK
1027 enum machine_mode mode1;
1028 int byte_offset;
44037a66 1029
44037a66
TG
1030 /* Discount the part of the structure before the desired byte.
1031 We need to know how many bytes are safe to reference after it. */
1032 if (total_size >= 0)
1033 total_size -= (bitpos / BIGGEST_ALIGNMENT
1034 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
1035
1036 if (tmode == VOIDmode)
1037 tmode = mode;
6ca6193b 1038
44037a66
TG
1039 while (GET_CODE (op0) == SUBREG)
1040 {
6ca6193b
JDA
1041 bitpos += SUBREG_BYTE (op0) * BITS_PER_UNIT;
1042 if (bitpos > unit)
64191612 1043 {
6ca6193b
JDA
1044 offset += (bitpos / unit);
1045 bitpos %= unit;
64191612 1046 }
44037a66
TG
1047 op0 = SUBREG_REG (op0);
1048 }
77295dec 1049
aac280fb
DD
1050 if (GET_CODE (op0) == REG
1051 && mode == GET_MODE (op0)
1052 && bitnum == 0
1053 && bitsize == GET_MODE_BITSIZE (GET_MODE (op0)))
1054 {
1055 /* We're trying to extract a full register from itself. */
1056 return op0;
1057 }
1058
d006aa54
RH
1059 /* Make sure we are playing with integral modes. Pun with subregs
1060 if we aren't. */
1061 {
1062 enum machine_mode imode = int_mode_for_mode (GET_MODE (op0));
1063 if (imode != GET_MODE (op0))
1064 {
1065 if (GET_CODE (op0) == MEM)
f4ef873c 1066 op0 = adjust_address (op0, imode, 0);
d006aa54
RH
1067 else if (imode != BLKmode)
1068 op0 = gen_lowpart (imode, op0);
1069 else
1070 abort ();
1071 }
1072 }
1073
4e9bb42b
AH
1074 /* We may be accessing data outside the field, which means
1075 we can alias adjacent data. */
1076 if (GET_CODE (op0) == MEM)
1077 {
1078 op0 = shallow_copy_rtx (op0);
1079 set_mem_alias_set (op0, 0);
1080 set_mem_expr (op0, 0);
1081 }
1082
6ca6193b
JDA
1083 /* Extraction of a full-word or multi-word value from a structure
1084 in a register or aligned memory can be done with just a SUBREG.
1085 A subword value in the least significant part of a register
1086 can also be extracted with a SUBREG. For this, we need the
1087 byte offset of the value in op0. */
1088
1089 byte_offset = bitpos / BITS_PER_UNIT + offset * UNITS_PER_WORD;
c410d49e 1090
44037a66
TG
1091 /* If OP0 is a register, BITPOS must count within a word.
1092 But as we have it, it counts within whatever size OP0 now has.
1093 On a bigendian machine, these are not the same, so convert. */
db3cf6fb
MS
1094 if (BYTES_BIG_ENDIAN
1095 && GET_CODE (op0) != MEM
f76b9db2 1096 && unit > GET_MODE_BITSIZE (GET_MODE (op0)))
44037a66 1097 bitpos += unit - GET_MODE_BITSIZE (GET_MODE (op0));
44037a66 1098
6ca6193b
JDA
1099 /* ??? We currently assume TARGET is at least as big as BITSIZE.
1100 If that's wrong, the solution is to test for it and set TARGET to 0
1101 if needed. */
e98f90d3
GK
1102
1103 mode1 = (VECTOR_MODE_P (tmode)
0fb7aeda
KH
1104 ? mode
1105 : mode_for_size (bitsize, GET_MODE_CLASS (tmode), 0));
e98f90d3 1106
0d2f38ee
OH
1107 if (((bitsize >= BITS_PER_WORD && bitsize == GET_MODE_BITSIZE (mode)
1108 && bitpos % BITS_PER_WORD == 0)
1109 || (mode_for_size (bitsize, GET_MODE_CLASS (tmode), 0) != BLKmode
1110 /* ??? The big endian test here is wrong. This is correct
1111 if the value is in a register, and if mode_for_size is not
1112 the same mode as op0. This causes us to get unnecessarily
1113 inefficient code from the Thumb port when -mbig-endian. */
1114 && (BYTES_BIG_ENDIAN
1115 ? bitpos + bitsize == BITS_PER_WORD
1116 : bitpos == 0)))
1117 && ((GET_CODE (op0) != MEM
1118 && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
1119 GET_MODE_BITSIZE (GET_MODE (op0)))
1120 && GET_MODE_SIZE (mode1) != 0
1121 && byte_offset % GET_MODE_SIZE (mode1) == 0)
1122 || (GET_CODE (op0) == MEM
1123 && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (op0))
1124 || (offset * BITS_PER_UNIT % bitsize == 0
1125 && MEM_ALIGN (op0) % bitsize == 0)))))
44037a66 1126 {
44037a66 1127 if (mode1 != GET_MODE (op0))
c7e33f89 1128 {
d006aa54
RH
1129 if (GET_CODE (op0) == SUBREG)
1130 {
1131 if (GET_MODE (SUBREG_REG (op0)) == mode1
1132 || GET_MODE_CLASS (mode1) == MODE_INT
1133 || GET_MODE_CLASS (mode1) == MODE_PARTIAL_INT)
1134 op0 = SUBREG_REG (op0);
1135 else
1136 /* Else we've got some float mode source being extracted into
1137 a different float mode destination -- this combination of
1138 subregs results in Severe Tire Damage. */
28ce94d4 1139 goto no_subreg_mode_swap;
d006aa54 1140 }
c7e33f89 1141 if (GET_CODE (op0) == REG)
e98f90d3 1142 op0 = gen_rtx_SUBREG (mode1, op0, byte_offset);
c7e33f89 1143 else
f4ef873c 1144 op0 = adjust_address (op0, mode1, offset);
c7e33f89 1145 }
44037a66
TG
1146 if (mode1 != mode)
1147 return convert_to_mode (tmode, op0, unsignedp);
1148 return op0;
1149 }
28ce94d4 1150 no_subreg_mode_swap:
44037a66
TG
1151
1152 /* Handle fields bigger than a word. */
c410d49e 1153
44037a66
TG
1154 if (bitsize > BITS_PER_WORD)
1155 {
1156 /* Here we transfer the words of the field
1157 in the order least significant first.
1158 This is because the most significant word is the one which may
1159 be less than full. */
1160
770ae6cc
RK
1161 unsigned int nwords = (bitsize + (BITS_PER_WORD - 1)) / BITS_PER_WORD;
1162 unsigned int i;
44037a66
TG
1163
1164 if (target == 0 || GET_CODE (target) != REG)
1165 target = gen_reg_rtx (mode);
1166
34ea783b 1167 /* Indicate for flow that the entire target reg is being set. */
38a448ca 1168 emit_insn (gen_rtx_CLOBBER (VOIDmode, target));
34ea783b 1169
44037a66
TG
1170 for (i = 0; i < nwords; i++)
1171 {
1172 /* If I is 0, use the low-order word in both field and target;
1173 if I is 1, use the next to lowest word; and so on. */
77295dec 1174 /* Word number in TARGET to use. */
770ae6cc
RK
1175 unsigned int wordnum
1176 = (WORDS_BIG_ENDIAN
1177 ? GET_MODE_SIZE (GET_MODE (target)) / UNITS_PER_WORD - i - 1
1178 : i);
77295dec 1179 /* Offset from start of field in OP0. */
770ae6cc
RK
1180 unsigned int bit_offset = (WORDS_BIG_ENDIAN
1181 ? MAX (0, ((int) bitsize - ((int) i + 1)
75131237 1182 * (int) BITS_PER_WORD))
770ae6cc 1183 : (int) i * BITS_PER_WORD);
44037a66
TG
1184 rtx target_part = operand_subword (target, wordnum, 1, VOIDmode);
1185 rtx result_part
1186 = extract_bit_field (op0, MIN (BITS_PER_WORD,
1187 bitsize - i * BITS_PER_WORD),
19caa751 1188 bitnum + bit_offset, 1, target_part, mode,
04050c69 1189 word_mode, total_size);
44037a66
TG
1190
1191 if (target_part == 0)
1192 abort ();
1193
1194 if (result_part != target_part)
1195 emit_move_insn (target_part, result_part);
1196 }
1197
5f57dff0 1198 if (unsignedp)
77295dec
DE
1199 {
1200 /* Unless we've filled TARGET, the upper regs in a multi-reg value
1201 need to be zero'd out. */
1202 if (GET_MODE_SIZE (GET_MODE (target)) > nwords * UNITS_PER_WORD)
1203 {
770ae6cc 1204 unsigned int i, total_words;
77295dec
DE
1205
1206 total_words = GET_MODE_SIZE (GET_MODE (target)) / UNITS_PER_WORD;
1207 for (i = nwords; i < total_words; i++)
04050c69
RK
1208 emit_move_insn
1209 (operand_subword (target,
1210 WORDS_BIG_ENDIAN ? total_words - i - 1 : i,
1211 1, VOIDmode),
1212 const0_rtx);
77295dec
DE
1213 }
1214 return target;
1215 }
1216
5f57dff0
JW
1217 /* Signed bit field: sign-extend with two arithmetic shifts. */
1218 target = expand_shift (LSHIFT_EXPR, mode, target,
1219 build_int_2 (GET_MODE_BITSIZE (mode) - bitsize, 0),
1220 NULL_RTX, 0);
1221 return expand_shift (RSHIFT_EXPR, mode, target,
1222 build_int_2 (GET_MODE_BITSIZE (mode) - bitsize, 0),
1223 NULL_RTX, 0);
44037a66 1224 }
c410d49e 1225
3306eb80
GK
1226 /* From here on we know the desired field is smaller than a word. */
1227
1228 /* Check if there is a correspondingly-sized integer field, so we can
1229 safely extract it as one size of integer, if necessary; then
1230 truncate or extend to the size that is wanted; then use SUBREGs or
1231 convert_to_mode to get one of the modes we really wanted. */
c410d49e 1232
3306eb80
GK
1233 int_mode = int_mode_for_mode (tmode);
1234 if (int_mode == BLKmode)
1235 int_mode = int_mode_for_mode (mode);
1236 if (int_mode == BLKmode)
c4f2c499
KH
1237 abort (); /* Should probably push op0 out to memory and then
1238 do a load. */
44037a66
TG
1239
1240 /* OFFSET is the number of words or bytes (UNIT says which)
1241 from STR_RTX to the first word or byte containing part of the field. */
1242
d006aa54 1243 if (GET_CODE (op0) != MEM)
44037a66
TG
1244 {
1245 if (offset != 0
1246 || GET_MODE_SIZE (GET_MODE (op0)) > UNITS_PER_WORD)
470032d7
RH
1247 {
1248 if (GET_CODE (op0) != REG)
1249 op0 = copy_to_reg (op0);
1250 op0 = gen_rtx_SUBREG (mode_for_size (BITS_PER_WORD, MODE_INT, 0),
ddef6bc7 1251 op0, (offset * UNITS_PER_WORD));
470032d7 1252 }
44037a66
TG
1253 offset = 0;
1254 }
1255 else
04050c69 1256 op0 = protect_from_queue (str_rtx, 1);
44037a66
TG
1257
1258 /* Now OFFSET is nonzero only for memory operands. */
1259
1260 if (unsignedp)
1261 {
a242b083
ZW
1262 if (HAVE_extzv
1263 && (GET_MODE_BITSIZE (extzv_mode) >= bitsize)
39e0911f 1264 && ! ((GET_CODE (op0) == REG || GET_CODE (op0) == SUBREG)
a242b083 1265 && (bitsize + bitpos > GET_MODE_BITSIZE (extzv_mode))))
44037a66 1266 {
770ae6cc 1267 unsigned HOST_WIDE_INT xbitpos = bitpos, xoffset = offset;
44037a66 1268 rtx bitsize_rtx, bitpos_rtx;
e5e809f4 1269 rtx last = get_last_insn ();
44037a66
TG
1270 rtx xop0 = op0;
1271 rtx xtarget = target;
1272 rtx xspec_target = spec_target;
1273 rtx xspec_target_subreg = spec_target_subreg;
1274 rtx pat;
d523b40e 1275 enum machine_mode maxmode = mode_for_extraction (EP_extzv, 0);
44037a66
TG
1276
1277 if (GET_CODE (xop0) == MEM)
1278 {
1279 int save_volatile_ok = volatile_ok;
1280 volatile_ok = 1;
1281
1282 /* Is the memory operand acceptable? */
a995e389 1283 if (! ((*insn_data[(int) CODE_FOR_extzv].operand[1].predicate)
3bf1b082 1284 (xop0, GET_MODE (xop0))))
44037a66
TG
1285 {
1286 /* No, load into a reg and extract from there. */
1287 enum machine_mode bestmode;
1288
1289 /* Get the mode to use for inserting into this field. If
1290 OP0 is BLKmode, get the smallest mode consistent with the
1291 alignment. If OP0 is a non-BLKmode object that is no
1292 wider than MAXMODE, use its mode. Otherwise, use the
1293 smallest mode containing the field. */
1294
1295 if (GET_MODE (xop0) == BLKmode
1296 || (GET_MODE_SIZE (GET_MODE (op0))
1297 > GET_MODE_SIZE (maxmode)))
04050c69
RK
1298 bestmode = get_best_mode (bitsize, bitnum,
1299 MEM_ALIGN (xop0), maxmode,
717702e6 1300 MEM_VOLATILE_P (xop0));
44037a66
TG
1301 else
1302 bestmode = GET_MODE (xop0);
1303
bd5d175a 1304 if (bestmode == VOIDmode
04050c69
RK
1305 || (SLOW_UNALIGNED_ACCESS (bestmode, MEM_ALIGN (xop0))
1306 && GET_MODE_BITSIZE (bestmode) > MEM_ALIGN (xop0)))
44037a66
TG
1307 goto extzv_loses;
1308
1309 /* Compute offset as multiple of this unit,
1310 counting in bytes. */
1311 unit = GET_MODE_BITSIZE (bestmode);
1312 xoffset = (bitnum / unit) * GET_MODE_SIZE (bestmode);
1313 xbitpos = bitnum % unit;
f4ef873c
RK
1314 xop0 = adjust_address (xop0, bestmode, xoffset);
1315
44037a66
TG
1316 /* Fetch it to a register in that size. */
1317 xop0 = force_reg (bestmode, xop0);
1318
1319 /* XBITPOS counts within UNIT, which is what is expected. */
1320 }
1321 else
1322 /* Get ref to first byte containing part of the field. */
f4ef873c 1323 xop0 = adjust_address (xop0, byte_mode, xoffset);
44037a66
TG
1324
1325 volatile_ok = save_volatile_ok;
1326 }
1327
1328 /* If op0 is a register, we need it in MAXMODE (which is usually
1329 SImode). to make it acceptable to the format of extzv. */
1330 if (GET_CODE (xop0) == SUBREG && GET_MODE (xop0) != maxmode)
e5e809f4 1331 goto extzv_loses;
44037a66 1332 if (GET_CODE (xop0) == REG && GET_MODE (xop0) != maxmode)
38a448ca 1333 xop0 = gen_rtx_SUBREG (maxmode, xop0, 0);
44037a66
TG
1334
1335 /* On big-endian machines, we count bits from the most significant.
1336 If the bit field insn does not, we must invert. */
f76b9db2
ILT
1337 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
1338 xbitpos = unit - bitsize - xbitpos;
1339
44037a66 1340 /* Now convert from counting within UNIT to counting in MAXMODE. */
f76b9db2 1341 if (BITS_BIG_ENDIAN && GET_CODE (xop0) != MEM)
44037a66 1342 xbitpos += GET_MODE_BITSIZE (maxmode) - unit;
f76b9db2 1343
44037a66
TG
1344 unit = GET_MODE_BITSIZE (maxmode);
1345
1346 if (xtarget == 0
1347 || (flag_force_mem && GET_CODE (xtarget) == MEM))
1348 xtarget = xspec_target = gen_reg_rtx (tmode);
1349
1350 if (GET_MODE (xtarget) != maxmode)
1351 {
1352 if (GET_CODE (xtarget) == REG)
b7a09135
RS
1353 {
1354 int wider = (GET_MODE_SIZE (maxmode)
1355 > GET_MODE_SIZE (GET_MODE (xtarget)));
1356 xtarget = gen_lowpart (maxmode, xtarget);
1357 if (wider)
1358 xspec_target_subreg = xtarget;
1359 }
44037a66
TG
1360 else
1361 xtarget = gen_reg_rtx (maxmode);
1362 }
1363
1364 /* If this machine's extzv insists on a register target,
1365 make sure we have one. */
a995e389 1366 if (! ((*insn_data[(int) CODE_FOR_extzv].operand[0].predicate)
44037a66
TG
1367 (xtarget, maxmode)))
1368 xtarget = gen_reg_rtx (maxmode);
1369
b1ec3c92
CH
1370 bitsize_rtx = GEN_INT (bitsize);
1371 bitpos_rtx = GEN_INT (xbitpos);
44037a66
TG
1372
1373 pat = gen_extzv (protect_from_queue (xtarget, 1),
1374 xop0, bitsize_rtx, bitpos_rtx);
1375 if (pat)
1376 {
1377 emit_insn (pat);
1378 target = xtarget;
1379 spec_target = xspec_target;
1380 spec_target_subreg = xspec_target_subreg;
1381 }
1382 else
1383 {
1384 delete_insns_since (last);
3306eb80 1385 target = extract_fixed_bit_field (int_mode, op0, offset, bitsize,
04050c69 1386 bitpos, target, 1);
44037a66
TG
1387 }
1388 }
1389 else
04050c69 1390 extzv_loses:
c410d49e 1391 target = extract_fixed_bit_field (int_mode, op0, offset, bitsize,
04050c69 1392 bitpos, target, 1);
44037a66
TG
1393 }
1394 else
1395 {
a242b083
ZW
1396 if (HAVE_extv
1397 && (GET_MODE_BITSIZE (extv_mode) >= bitsize)
39e0911f 1398 && ! ((GET_CODE (op0) == REG || GET_CODE (op0) == SUBREG)
a242b083 1399 && (bitsize + bitpos > GET_MODE_BITSIZE (extv_mode))))
44037a66
TG
1400 {
1401 int xbitpos = bitpos, xoffset = offset;
1402 rtx bitsize_rtx, bitpos_rtx;
e5e809f4 1403 rtx last = get_last_insn ();
44037a66
TG
1404 rtx xop0 = op0, xtarget = target;
1405 rtx xspec_target = spec_target;
1406 rtx xspec_target_subreg = spec_target_subreg;
1407 rtx pat;
d523b40e 1408 enum machine_mode maxmode = mode_for_extraction (EP_extv, 0);
44037a66
TG
1409
1410 if (GET_CODE (xop0) == MEM)
1411 {
1412 /* Is the memory operand acceptable? */
a995e389 1413 if (! ((*insn_data[(int) CODE_FOR_extv].operand[1].predicate)
44037a66
TG
1414 (xop0, GET_MODE (xop0))))
1415 {
1416 /* No, load into a reg and extract from there. */
1417 enum machine_mode bestmode;
1418
1419 /* Get the mode to use for inserting into this field. If
1420 OP0 is BLKmode, get the smallest mode consistent with the
1421 alignment. If OP0 is a non-BLKmode object that is no
1422 wider than MAXMODE, use its mode. Otherwise, use the
1423 smallest mode containing the field. */
1424
1425 if (GET_MODE (xop0) == BLKmode
1426 || (GET_MODE_SIZE (GET_MODE (op0))
1427 > GET_MODE_SIZE (maxmode)))
04050c69
RK
1428 bestmode = get_best_mode (bitsize, bitnum,
1429 MEM_ALIGN (xop0), maxmode,
717702e6 1430 MEM_VOLATILE_P (xop0));
44037a66
TG
1431 else
1432 bestmode = GET_MODE (xop0);
1433
bd5d175a 1434 if (bestmode == VOIDmode
04050c69
RK
1435 || (SLOW_UNALIGNED_ACCESS (bestmode, MEM_ALIGN (xop0))
1436 && GET_MODE_BITSIZE (bestmode) > MEM_ALIGN (xop0)))
44037a66
TG
1437 goto extv_loses;
1438
1439 /* Compute offset as multiple of this unit,
1440 counting in bytes. */
1441 unit = GET_MODE_BITSIZE (bestmode);
1442 xoffset = (bitnum / unit) * GET_MODE_SIZE (bestmode);
1443 xbitpos = bitnum % unit;
f4ef873c
RK
1444 xop0 = adjust_address (xop0, bestmode, xoffset);
1445
44037a66
TG
1446 /* Fetch it to a register in that size. */
1447 xop0 = force_reg (bestmode, xop0);
1448
1449 /* XBITPOS counts within UNIT, which is what is expected. */
1450 }
1451 else
1452 /* Get ref to first byte containing part of the field. */
f4ef873c 1453 xop0 = adjust_address (xop0, byte_mode, xoffset);
44037a66
TG
1454 }
1455
1456 /* If op0 is a register, we need it in MAXMODE (which is usually
1457 SImode) to make it acceptable to the format of extv. */
1458 if (GET_CODE (xop0) == SUBREG && GET_MODE (xop0) != maxmode)
e5e809f4 1459 goto extv_loses;
44037a66 1460 if (GET_CODE (xop0) == REG && GET_MODE (xop0) != maxmode)
38a448ca 1461 xop0 = gen_rtx_SUBREG (maxmode, xop0, 0);
44037a66
TG
1462
1463 /* On big-endian machines, we count bits from the most significant.
1464 If the bit field insn does not, we must invert. */
f76b9db2
ILT
1465 if (BITS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
1466 xbitpos = unit - bitsize - xbitpos;
1467
44037a66
TG
1468 /* XBITPOS counts within a size of UNIT.
1469 Adjust to count within a size of MAXMODE. */
f76b9db2 1470 if (BITS_BIG_ENDIAN && GET_CODE (xop0) != MEM)
44037a66 1471 xbitpos += (GET_MODE_BITSIZE (maxmode) - unit);
f76b9db2 1472
44037a66
TG
1473 unit = GET_MODE_BITSIZE (maxmode);
1474
1475 if (xtarget == 0
1476 || (flag_force_mem && GET_CODE (xtarget) == MEM))
1477 xtarget = xspec_target = gen_reg_rtx (tmode);
1478
1479 if (GET_MODE (xtarget) != maxmode)
1480 {
1481 if (GET_CODE (xtarget) == REG)
b7a09135
RS
1482 {
1483 int wider = (GET_MODE_SIZE (maxmode)
1484 > GET_MODE_SIZE (GET_MODE (xtarget)));
1485 xtarget = gen_lowpart (maxmode, xtarget);
1486 if (wider)
1487 xspec_target_subreg = xtarget;
1488 }
44037a66
TG
1489 else
1490 xtarget = gen_reg_rtx (maxmode);
1491 }
1492
1493 /* If this machine's extv insists on a register target,
1494 make sure we have one. */
a995e389 1495 if (! ((*insn_data[(int) CODE_FOR_extv].operand[0].predicate)
44037a66
TG
1496 (xtarget, maxmode)))
1497 xtarget = gen_reg_rtx (maxmode);
1498
b1ec3c92
CH
1499 bitsize_rtx = GEN_INT (bitsize);
1500 bitpos_rtx = GEN_INT (xbitpos);
44037a66
TG
1501
1502 pat = gen_extv (protect_from_queue (xtarget, 1),
1503 xop0, bitsize_rtx, bitpos_rtx);
1504 if (pat)
1505 {
1506 emit_insn (pat);
1507 target = xtarget;
1508 spec_target = xspec_target;
1509 spec_target_subreg = xspec_target_subreg;
1510 }
1511 else
1512 {
1513 delete_insns_since (last);
3306eb80 1514 target = extract_fixed_bit_field (int_mode, op0, offset, bitsize,
04050c69 1515 bitpos, target, 0);
44037a66 1516 }
c410d49e 1517 }
44037a66 1518 else
04050c69 1519 extv_loses:
c410d49e 1520 target = extract_fixed_bit_field (int_mode, op0, offset, bitsize,
04050c69 1521 bitpos, target, 0);
44037a66
TG
1522 }
1523 if (target == spec_target)
1524 return target;
1525 if (target == spec_target_subreg)
1526 return spec_target;
1527 if (GET_MODE (target) != tmode && GET_MODE (target) != mode)
1528 {
1529 /* If the target mode is floating-point, first convert to the
1530 integer mode of that size and then access it as a floating-point
1531 value via a SUBREG. */
32b069d3
RH
1532 if (GET_MODE_CLASS (tmode) != MODE_INT
1533 && GET_MODE_CLASS (tmode) != MODE_PARTIAL_INT)
44037a66
TG
1534 {
1535 target = convert_to_mode (mode_for_size (GET_MODE_BITSIZE (tmode),
1536 MODE_INT, 0),
1537 target, unsignedp);
32b069d3 1538 return gen_lowpart (tmode, target);
44037a66
TG
1539 }
1540 else
1541 return convert_to_mode (tmode, target, unsignedp);
1542 }
1543 return target;
1544}
1545\f
1546/* Extract a bit field using shifts and boolean operations
1547 Returns an rtx to represent the value.
1548 OP0 addresses a register (word) or memory (byte).
1549 BITPOS says which bit within the word or byte the bit field starts in.
1550 OFFSET says how many bytes farther the bit field starts;
1551 it is 0 if OP0 is a register.
1552 BITSIZE says how many bits long the bit field is.
1553 (If OP0 is a register, it may be narrower than a full word,
1554 but BITPOS still counts within a full word,
1555 which is significant on bigendian machines.)
1556
1557 UNSIGNEDP is nonzero for an unsigned bit field (don't sign-extend value).
1558 If TARGET is nonzero, attempts to store the value there
1559 and return TARGET, but this is not guaranteed.
04050c69 1560 If TARGET is not used, create a pseudo-reg of mode TMODE for the value. */
44037a66
TG
1561
1562static rtx
1563extract_fixed_bit_field (tmode, op0, offset, bitsize, bitpos,
04050c69 1564 target, unsignedp)
44037a66 1565 enum machine_mode tmode;
b3694847 1566 rtx op0, target;
770ae6cc 1567 unsigned HOST_WIDE_INT offset, bitsize, bitpos;
44037a66 1568 int unsignedp;
44037a66 1569{
770ae6cc 1570 unsigned int total_bits = BITS_PER_WORD;
44037a66
TG
1571 enum machine_mode mode;
1572
1573 if (GET_CODE (op0) == SUBREG || GET_CODE (op0) == REG)
1574 {
1575 /* Special treatment for a bit field split across two registers. */
1576 if (bitsize + bitpos > BITS_PER_WORD)
04050c69 1577 return extract_split_bit_field (op0, bitsize, bitpos, unsignedp);
44037a66
TG
1578 }
1579 else
1580 {
1581 /* Get the proper mode to use for this field. We want a mode that
1582 includes the entire field. If such a mode would be larger than
1583 a word, we won't be doing the extraction the normal way. */
1584
04050c69
RK
1585 mode = get_best_mode (bitsize, bitpos + offset * BITS_PER_UNIT,
1586 MEM_ALIGN (op0), word_mode, MEM_VOLATILE_P (op0));
44037a66
TG
1587
1588 if (mode == VOIDmode)
1589 /* The only way this should occur is if the field spans word
1590 boundaries. */
1591 return extract_split_bit_field (op0, bitsize,
1592 bitpos + offset * BITS_PER_UNIT,
04050c69 1593 unsignedp);
44037a66
TG
1594
1595 total_bits = GET_MODE_BITSIZE (mode);
1596
401db791 1597 /* Make sure bitpos is valid for the chosen mode. Adjust BITPOS to
38e01259 1598 be in the range 0 to total_bits-1, and put any excess bytes in
401db791
JW
1599 OFFSET. */
1600 if (bitpos >= total_bits)
1601 {
1602 offset += (bitpos / total_bits) * (total_bits / BITS_PER_UNIT);
1603 bitpos -= ((bitpos / total_bits) * (total_bits / BITS_PER_UNIT)
1604 * BITS_PER_UNIT);
1605 }
1606
44037a66
TG
1607 /* Get ref to an aligned byte, halfword, or word containing the field.
1608 Adjust BITPOS to be position within a word,
1609 and OFFSET to be the offset of that word.
1610 Then alter OP0 to refer to that word. */
1611 bitpos += (offset % (total_bits / BITS_PER_UNIT)) * BITS_PER_UNIT;
1612 offset -= (offset % (total_bits / BITS_PER_UNIT));
f4ef873c 1613 op0 = adjust_address (op0, mode, offset);
44037a66
TG
1614 }
1615
37811a73
RK
1616 mode = GET_MODE (op0);
1617
f76b9db2 1618 if (BYTES_BIG_ENDIAN)
04050c69
RK
1619 /* BITPOS is the distance between our msb and that of OP0.
1620 Convert it to the distance from the lsb. */
1621 bitpos = total_bits - bitsize - bitpos;
44037a66 1622
44037a66
TG
1623 /* Now BITPOS is always the distance between the field's lsb and that of OP0.
1624 We have reduced the big-endian case to the little-endian case. */
1625
1626 if (unsignedp)
1627 {
1628 if (bitpos)
1629 {
1630 /* If the field does not already start at the lsb,
1631 shift it so it does. */
1632 tree amount = build_int_2 (bitpos, 0);
1633 /* Maybe propagate the target for the shift. */
1634 /* But not if we will return it--could confuse integrate.c. */
1635 rtx subtarget = (target != 0 && GET_CODE (target) == REG
1636 && !REG_FUNCTION_VALUE_P (target)
1637 ? target : 0);
1638 if (tmode != mode) subtarget = 0;
1639 op0 = expand_shift (RSHIFT_EXPR, mode, op0, amount, subtarget, 1);
1640 }
1641 /* Convert the value to the desired mode. */
1642 if (mode != tmode)
1643 op0 = convert_to_mode (tmode, op0, 1);
1644
1645 /* Unless the msb of the field used to be the msb when we shifted,
1646 mask out the upper bits. */
1647
c99d986a 1648 if (GET_MODE_BITSIZE (mode) != bitpos + bitsize)
44037a66
TG
1649 return expand_binop (GET_MODE (op0), and_optab, op0,
1650 mask_rtx (GET_MODE (op0), 0, bitsize, 0),
1651 target, 1, OPTAB_LIB_WIDEN);
1652 return op0;
1653 }
1654
1655 /* To extract a signed bit-field, first shift its msb to the msb of the word,
1656 then arithmetic-shift its lsb to the lsb of the word. */
1657 op0 = force_reg (mode, op0);
1658 if (mode != tmode)
1659 target = 0;
1660
1661 /* Find the narrowest integer mode that contains the field. */
1662
1663 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1664 mode = GET_MODE_WIDER_MODE (mode))
1665 if (GET_MODE_BITSIZE (mode) >= bitsize + bitpos)
1666 {
1667 op0 = convert_to_mode (mode, op0, 0);
1668 break;
1669 }
1670
1671 if (GET_MODE_BITSIZE (mode) != (bitsize + bitpos))
1672 {
04050c69
RK
1673 tree amount
1674 = build_int_2 (GET_MODE_BITSIZE (mode) - (bitsize + bitpos), 0);
44037a66
TG
1675 /* Maybe propagate the target for the shift. */
1676 /* But not if we will return the result--could confuse integrate.c. */
1677 rtx subtarget = (target != 0 && GET_CODE (target) == REG
1678 && ! REG_FUNCTION_VALUE_P (target)
1679 ? target : 0);
1680 op0 = expand_shift (LSHIFT_EXPR, mode, op0, amount, subtarget, 1);
1681 }
1682
1683 return expand_shift (RSHIFT_EXPR, mode, op0,
c410d49e 1684 build_int_2 (GET_MODE_BITSIZE (mode) - bitsize, 0),
44037a66
TG
1685 target, 0);
1686}
1687\f
1688/* Return a constant integer (CONST_INT or CONST_DOUBLE) mask value
1689 of mode MODE with BITSIZE ones followed by BITPOS zeros, or the
1690 complement of that if COMPLEMENT. The mask is truncated if
77295dec
DE
1691 necessary to the width of mode MODE. The mask is zero-extended if
1692 BITSIZE+BITPOS is too small for MODE. */
44037a66
TG
1693
1694static rtx
1695mask_rtx (mode, bitpos, bitsize, complement)
1696 enum machine_mode mode;
1697 int bitpos, bitsize, complement;
1698{
b1ec3c92 1699 HOST_WIDE_INT masklow, maskhigh;
44037a66 1700
21102f25
RK
1701 if (bitsize == 0)
1702 masklow = 0;
1703 else if (bitpos < HOST_BITS_PER_WIDE_INT)
b1ec3c92 1704 masklow = (HOST_WIDE_INT) -1 << bitpos;
44037a66
TG
1705 else
1706 masklow = 0;
1707
b1ec3c92
CH
1708 if (bitpos + bitsize < HOST_BITS_PER_WIDE_INT)
1709 masklow &= ((unsigned HOST_WIDE_INT) -1
1710 >> (HOST_BITS_PER_WIDE_INT - bitpos - bitsize));
c410d49e 1711
b1ec3c92 1712 if (bitpos <= HOST_BITS_PER_WIDE_INT)
44037a66
TG
1713 maskhigh = -1;
1714 else
b1ec3c92 1715 maskhigh = (HOST_WIDE_INT) -1 << (bitpos - HOST_BITS_PER_WIDE_INT);
44037a66 1716
21102f25
RK
1717 if (bitsize == 0)
1718 maskhigh = 0;
1719 else if (bitpos + bitsize > HOST_BITS_PER_WIDE_INT)
b1ec3c92
CH
1720 maskhigh &= ((unsigned HOST_WIDE_INT) -1
1721 >> (2 * HOST_BITS_PER_WIDE_INT - bitpos - bitsize));
44037a66
TG
1722 else
1723 maskhigh = 0;
1724
1725 if (complement)
1726 {
1727 maskhigh = ~maskhigh;
1728 masklow = ~masklow;
1729 }
1730
1731 return immed_double_const (masklow, maskhigh, mode);
1732}
1733
1734/* Return a constant integer (CONST_INT or CONST_DOUBLE) rtx with the value
1735 VALUE truncated to BITSIZE bits and then shifted left BITPOS bits. */
1736
1737static rtx
1738lshift_value (mode, value, bitpos, bitsize)
1739 enum machine_mode mode;
1740 rtx value;
1741 int bitpos, bitsize;
1742{
b1ec3c92
CH
1743 unsigned HOST_WIDE_INT v = INTVAL (value);
1744 HOST_WIDE_INT low, high;
44037a66 1745
b1ec3c92
CH
1746 if (bitsize < HOST_BITS_PER_WIDE_INT)
1747 v &= ~((HOST_WIDE_INT) -1 << bitsize);
44037a66 1748
b1ec3c92 1749 if (bitpos < HOST_BITS_PER_WIDE_INT)
44037a66
TG
1750 {
1751 low = v << bitpos;
b1ec3c92 1752 high = (bitpos > 0 ? (v >> (HOST_BITS_PER_WIDE_INT - bitpos)) : 0);
44037a66
TG
1753 }
1754 else
1755 {
1756 low = 0;
b1ec3c92 1757 high = v << (bitpos - HOST_BITS_PER_WIDE_INT);
44037a66
TG
1758 }
1759
1760 return immed_double_const (low, high, mode);
1761}
1762\f
1763/* Extract a bit field that is split across two words
1764 and return an RTX for the result.
1765
1766 OP0 is the REG, SUBREG or MEM rtx for the first of the two words.
1767 BITSIZE is the field width; BITPOS, position of its first bit, in the word.
04050c69 1768 UNSIGNEDP is 1 if should zero-extend the contents; else sign-extend. */
44037a66
TG
1769
1770static rtx
04050c69 1771extract_split_bit_field (op0, bitsize, bitpos, unsignedp)
44037a66 1772 rtx op0;
770ae6cc
RK
1773 unsigned HOST_WIDE_INT bitsize, bitpos;
1774 int unsignedp;
44037a66 1775{
770ae6cc
RK
1776 unsigned int unit;
1777 unsigned int bitsdone = 0;
c16ddde3 1778 rtx result = NULL_RTX;
06c94bce 1779 int first = 1;
44037a66 1780
4ee16841
DE
1781 /* Make sure UNIT isn't larger than BITS_PER_WORD, we can only handle that
1782 much at a time. */
1783 if (GET_CODE (op0) == REG || GET_CODE (op0) == SUBREG)
1784 unit = BITS_PER_WORD;
1785 else
04050c69 1786 unit = MIN (MEM_ALIGN (op0), BITS_PER_WORD);
4ee16841 1787
06c94bce
RS
1788 while (bitsdone < bitsize)
1789 {
770ae6cc 1790 unsigned HOST_WIDE_INT thissize;
06c94bce 1791 rtx part, word;
770ae6cc
RK
1792 unsigned HOST_WIDE_INT thispos;
1793 unsigned HOST_WIDE_INT offset;
06c94bce
RS
1794
1795 offset = (bitpos + bitsdone) / unit;
1796 thispos = (bitpos + bitsdone) % unit;
1797
0eb61c19
DE
1798 /* THISSIZE must not overrun a word boundary. Otherwise,
1799 extract_fixed_bit_field will call us again, and we will mutually
1800 recurse forever. */
1801 thissize = MIN (bitsize - bitsdone, BITS_PER_WORD);
1802 thissize = MIN (thissize, unit - thispos);
06c94bce
RS
1803
1804 /* If OP0 is a register, then handle OFFSET here.
5f57dff0
JW
1805
1806 When handling multiword bitfields, extract_bit_field may pass
1807 down a word_mode SUBREG of a larger REG for a bitfield that actually
1808 crosses a word boundary. Thus, for a SUBREG, we must find
1809 the current word starting from the base register. */
1810 if (GET_CODE (op0) == SUBREG)
1811 {
ddef6bc7
JJ
1812 int word_offset = (SUBREG_BYTE (op0) / UNITS_PER_WORD) + offset;
1813 word = operand_subword_force (SUBREG_REG (op0), word_offset,
5f57dff0
JW
1814 GET_MODE (SUBREG_REG (op0)));
1815 offset = 0;
1816 }
1817 else if (GET_CODE (op0) == REG)
06c94bce
RS
1818 {
1819 word = operand_subword_force (op0, offset, GET_MODE (op0));
1820 offset = 0;
1821 }
1822 else
1823 word = op0;
1824
06c94bce 1825 /* Extract the parts in bit-counting order,
0eb61c19
DE
1826 whose meaning is determined by BYTES_PER_UNIT.
1827 OFFSET is in UNITs, and UNIT is in bits.
1828 extract_fixed_bit_field wants offset in bytes. */
1829 part = extract_fixed_bit_field (word_mode, word,
1830 offset * unit / BITS_PER_UNIT,
04050c69 1831 thissize, thispos, 0, 1);
06c94bce 1832 bitsdone += thissize;
44037a66 1833
06c94bce 1834 /* Shift this part into place for the result. */
f76b9db2
ILT
1835 if (BYTES_BIG_ENDIAN)
1836 {
1837 if (bitsize != bitsdone)
1838 part = expand_shift (LSHIFT_EXPR, word_mode, part,
1839 build_int_2 (bitsize - bitsdone, 0), 0, 1);
1840 }
1841 else
1842 {
1843 if (bitsdone != thissize)
1844 part = expand_shift (LSHIFT_EXPR, word_mode, part,
1845 build_int_2 (bitsdone - thissize, 0), 0, 1);
1846 }
44037a66 1847
06c94bce
RS
1848 if (first)
1849 result = part;
1850 else
1851 /* Combine the parts with bitwise or. This works
1852 because we extracted each part as an unsigned bit field. */
1853 result = expand_binop (word_mode, ior_optab, part, result, NULL_RTX, 1,
1854 OPTAB_LIB_WIDEN);
1855
1856 first = 0;
1857 }
44037a66
TG
1858
1859 /* Unsigned bit field: we are done. */
1860 if (unsignedp)
1861 return result;
1862 /* Signed bit field: sign-extend with two arithmetic shifts. */
1863 result = expand_shift (LSHIFT_EXPR, word_mode, result,
b1ec3c92
CH
1864 build_int_2 (BITS_PER_WORD - bitsize, 0),
1865 NULL_RTX, 0);
44037a66 1866 return expand_shift (RSHIFT_EXPR, word_mode, result,
b1ec3c92 1867 build_int_2 (BITS_PER_WORD - bitsize, 0), NULL_RTX, 0);
44037a66
TG
1868}
1869\f
1870/* Add INC into TARGET. */
1871
1872void
1873expand_inc (target, inc)
1874 rtx target, inc;
1875{
1876 rtx value = expand_binop (GET_MODE (target), add_optab,
1877 target, inc,
1878 target, 0, OPTAB_LIB_WIDEN);
1879 if (value != target)
1880 emit_move_insn (target, value);
1881}
1882
1883/* Subtract DEC from TARGET. */
1884
1885void
1886expand_dec (target, dec)
1887 rtx target, dec;
1888{
1889 rtx value = expand_binop (GET_MODE (target), sub_optab,
1890 target, dec,
1891 target, 0, OPTAB_LIB_WIDEN);
1892 if (value != target)
1893 emit_move_insn (target, value);
1894}
1895\f
1896/* Output a shift instruction for expression code CODE,
1897 with SHIFTED being the rtx for the value to shift,
1898 and AMOUNT the tree for the amount to shift by.
1899 Store the result in the rtx TARGET, if that is convenient.
1900 If UNSIGNEDP is nonzero, do a logical shift; otherwise, arithmetic.
1901 Return the rtx for where the value is. */
1902
1903rtx
1904expand_shift (code, mode, shifted, amount, target, unsignedp)
1905 enum tree_code code;
b3694847 1906 enum machine_mode mode;
44037a66
TG
1907 rtx shifted;
1908 tree amount;
b3694847 1909 rtx target;
44037a66
TG
1910 int unsignedp;
1911{
b3694847
SS
1912 rtx op1, temp = 0;
1913 int left = (code == LSHIFT_EXPR || code == LROTATE_EXPR);
1914 int rotate = (code == LROTATE_EXPR || code == RROTATE_EXPR);
44037a66
TG
1915 int try;
1916
1917 /* Previously detected shift-counts computed by NEGATE_EXPR
1918 and shifted in the other direction; but that does not work
1919 on all machines. */
1920
b1ec3c92 1921 op1 = expand_expr (amount, NULL_RTX, VOIDmode, 0);
44037a66 1922
1433f0f9 1923#ifdef SHIFT_COUNT_TRUNCATED
166cdf4a
RH
1924 if (SHIFT_COUNT_TRUNCATED)
1925 {
1926 if (GET_CODE (op1) == CONST_INT
0fb7aeda 1927 && ((unsigned HOST_WIDE_INT) INTVAL (op1) >=
c84e2712 1928 (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (mode)))
0fb7aeda 1929 op1 = GEN_INT ((unsigned HOST_WIDE_INT) INTVAL (op1)
166cdf4a
RH
1930 % GET_MODE_BITSIZE (mode));
1931 else if (GET_CODE (op1) == SUBREG
4e07d762 1932 && subreg_lowpart_p (op1))
166cdf4a
RH
1933 op1 = SUBREG_REG (op1);
1934 }
2ab0a5c4
TG
1935#endif
1936
44037a66
TG
1937 if (op1 == const0_rtx)
1938 return shifted;
1939
1940 for (try = 0; temp == 0 && try < 3; try++)
1941 {
1942 enum optab_methods methods;
1943
1944 if (try == 0)
1945 methods = OPTAB_DIRECT;
1946 else if (try == 1)
1947 methods = OPTAB_WIDEN;
1948 else
1949 methods = OPTAB_LIB_WIDEN;
1950
1951 if (rotate)
1952 {
1953 /* Widening does not work for rotation. */
1954 if (methods == OPTAB_WIDEN)
1955 continue;
1956 else if (methods == OPTAB_LIB_WIDEN)
cbec710e 1957 {
39e71615 1958 /* If we have been unable to open-code this by a rotation,
cbec710e
RK
1959 do it as the IOR of two shifts. I.e., to rotate A
1960 by N bits, compute (A << N) | ((unsigned) A >> (C - N))
1961 where C is the bitsize of A.
1962
1963 It is theoretically possible that the target machine might
1964 not be able to perform either shift and hence we would
1965 be making two libcalls rather than just the one for the
1966 shift (similarly if IOR could not be done). We will allow
1967 this extremely unlikely lossage to avoid complicating the
1968 code below. */
1969
39e71615
RK
1970 rtx subtarget = target == shifted ? 0 : target;
1971 rtx temp1;
1972 tree type = TREE_TYPE (amount);
1973 tree new_amount = make_tree (type, op1);
1974 tree other_amount
1975 = fold (build (MINUS_EXPR, type,
1976 convert (type,
1977 build_int_2 (GET_MODE_BITSIZE (mode),
1978 0)),
1979 amount));
1980
1981 shifted = force_reg (mode, shifted);
1982
1983 temp = expand_shift (left ? LSHIFT_EXPR : RSHIFT_EXPR,
1984 mode, shifted, new_amount, subtarget, 1);
1985 temp1 = expand_shift (left ? RSHIFT_EXPR : LSHIFT_EXPR,
1986 mode, shifted, other_amount, 0, 1);
1987 return expand_binop (mode, ior_optab, temp, temp1, target,
1988 unsignedp, methods);
cbec710e 1989 }
44037a66
TG
1990
1991 temp = expand_binop (mode,
1992 left ? rotl_optab : rotr_optab,
1993 shifted, op1, target, unsignedp, methods);
cbec710e
RK
1994
1995 /* If we don't have the rotate, but we are rotating by a constant
1996 that is in range, try a rotate in the opposite direction. */
1997
1998 if (temp == 0 && GET_CODE (op1) == CONST_INT
da920570
ZW
1999 && INTVAL (op1) > 0
2000 && (unsigned int) INTVAL (op1) < GET_MODE_BITSIZE (mode))
cbec710e
RK
2001 temp = expand_binop (mode,
2002 left ? rotr_optab : rotl_optab,
c410d49e 2003 shifted,
cbec710e
RK
2004 GEN_INT (GET_MODE_BITSIZE (mode)
2005 - INTVAL (op1)),
2006 target, unsignedp, methods);
44037a66
TG
2007 }
2008 else if (unsignedp)
a34958c9
RK
2009 temp = expand_binop (mode,
2010 left ? ashl_optab : lshr_optab,
2011 shifted, op1, target, unsignedp, methods);
44037a66
TG
2012
2013 /* Do arithmetic shifts.
2014 Also, if we are going to widen the operand, we can just as well
2015 use an arithmetic right-shift instead of a logical one. */
2016 if (temp == 0 && ! rotate
2017 && (! unsignedp || (! left && methods == OPTAB_WIDEN)))
2018 {
2019 enum optab_methods methods1 = methods;
2020
2021 /* If trying to widen a log shift to an arithmetic shift,
2022 don't accept an arithmetic shift of the same size. */
2023 if (unsignedp)
2024 methods1 = OPTAB_MUST_WIDEN;
2025
2026 /* Arithmetic shift */
2027
2028 temp = expand_binop (mode,
2029 left ? ashl_optab : ashr_optab,
2030 shifted, op1, target, unsignedp, methods1);
2031 }
2032
711a5e64 2033 /* We used to try extzv here for logical right shifts, but that was
c410d49e 2034 only useful for one machine, the VAX, and caused poor code
711a5e64
RK
2035 generation there for lshrdi3, so the code was deleted and a
2036 define_expand for lshrsi3 was added to vax.md. */
44037a66
TG
2037 }
2038
2039 if (temp == 0)
2040 abort ();
2041 return temp;
2042}
2043\f
b385aeda 2044enum alg_code { alg_zero, alg_m, alg_shift,
b2fb324c 2045 alg_add_t_m2, alg_sub_t_m2,
7963ac37
RK
2046 alg_add_factor, alg_sub_factor,
2047 alg_add_t2_m, alg_sub_t2_m,
b385aeda 2048 alg_add, alg_subtract, alg_factor, alg_shiftop };
44037a66
TG
2049
2050/* This structure records a sequence of operations.
2051 `ops' is the number of operations recorded.
2052 `cost' is their total cost.
2053 The operations are stored in `op' and the corresponding
b385aeda
RK
2054 logarithms of the integer coefficients in `log'.
2055
44037a66 2056 These are the operations:
b385aeda
RK
2057 alg_zero total := 0;
2058 alg_m total := multiplicand;
b2fb324c 2059 alg_shift total := total * coeff
7963ac37
RK
2060 alg_add_t_m2 total := total + multiplicand * coeff;
2061 alg_sub_t_m2 total := total - multiplicand * coeff;
2062 alg_add_factor total := total * coeff + total;
2063 alg_sub_factor total := total * coeff - total;
2064 alg_add_t2_m total := total * coeff + multiplicand;
2065 alg_sub_t2_m total := total * coeff - multiplicand;
b385aeda
RK
2066
2067 The first operand must be either alg_zero or alg_m. */
44037a66 2068
44037a66
TG
2069struct algorithm
2070{
7963ac37
RK
2071 short cost;
2072 short ops;
b385aeda
RK
2073 /* The size of the OP and LOG fields are not directly related to the
2074 word size, but the worst-case algorithms will be if we have few
2075 consecutive ones or zeros, i.e., a multiplicand like 10101010101...
2076 In that case we will generate shift-by-2, add, shift-by-2, add,...,
2077 in total wordsize operations. */
44037a66 2078 enum alg_code op[MAX_BITS_PER_WORD];
b385aeda 2079 char log[MAX_BITS_PER_WORD];
44037a66
TG
2080};
2081
711d877c
KG
2082static void synth_mult PARAMS ((struct algorithm *,
2083 unsigned HOST_WIDE_INT,
2084 int));
2085static unsigned HOST_WIDE_INT choose_multiplier PARAMS ((unsigned HOST_WIDE_INT,
2086 int, int,
2087 unsigned HOST_WIDE_INT *,
2088 int *, int *));
2089static unsigned HOST_WIDE_INT invert_mod2n PARAMS ((unsigned HOST_WIDE_INT,
2090 int));
44037a66 2091/* Compute and return the best algorithm for multiplying by T.
7963ac37
RK
2092 The algorithm must cost less than cost_limit
2093 If retval.cost >= COST_LIMIT, no algorithm was found and all
2094 other field of the returned struct are undefined. */
44037a66 2095
819126a6
RK
2096static void
2097synth_mult (alg_out, t, cost_limit)
2098 struct algorithm *alg_out;
b1ec3c92 2099 unsigned HOST_WIDE_INT t;
7963ac37 2100 int cost_limit;
44037a66 2101{
b2fb324c 2102 int m;
52786026 2103 struct algorithm *alg_in, *best_alg;
c84e2712 2104 int cost;
b2fb324c 2105 unsigned HOST_WIDE_INT q;
44037a66 2106
7963ac37
RK
2107 /* Indicate that no algorithm is yet found. If no algorithm
2108 is found, this value will be returned and indicate failure. */
819126a6 2109 alg_out->cost = cost_limit;
44037a66 2110
b2fb324c 2111 if (cost_limit <= 0)
819126a6 2112 return;
44037a66 2113
b385aeda
RK
2114 /* t == 1 can be done in zero cost. */
2115 if (t == 1)
b2fb324c 2116 {
819126a6
RK
2117 alg_out->ops = 1;
2118 alg_out->cost = 0;
2119 alg_out->op[0] = alg_m;
2120 return;
b2fb324c
RK
2121 }
2122
b385aeda
RK
2123 /* t == 0 sometimes has a cost. If it does and it exceeds our limit,
2124 fail now. */
819126a6 2125 if (t == 0)
b385aeda
RK
2126 {
2127 if (zero_cost >= cost_limit)
819126a6 2128 return;
b385aeda
RK
2129 else
2130 {
819126a6
RK
2131 alg_out->ops = 1;
2132 alg_out->cost = zero_cost;
2133 alg_out->op[0] = alg_zero;
2134 return;
b385aeda
RK
2135 }
2136 }
2137
52786026
RK
2138 /* We'll be needing a couple extra algorithm structures now. */
2139
2140 alg_in = (struct algorithm *)alloca (sizeof (struct algorithm));
2141 best_alg = (struct algorithm *)alloca (sizeof (struct algorithm));
2142
b385aeda
RK
2143 /* If we have a group of zero bits at the low-order part of T, try
2144 multiplying by the remaining bits and then doing a shift. */
2145
b2fb324c 2146 if ((t & 1) == 0)
44037a66 2147 {
b2fb324c 2148 m = floor_log2 (t & -t); /* m = number of low zero bits */
02a65aef 2149 if (m < BITS_PER_WORD)
44037a66 2150 {
02a65aef
R
2151 q = t >> m;
2152 cost = shift_cost[m];
2153 synth_mult (alg_in, q, cost_limit - cost);
2154
2155 cost += alg_in->cost;
2156 if (cost < cost_limit)
2157 {
2158 struct algorithm *x;
2159 x = alg_in, alg_in = best_alg, best_alg = x;
2160 best_alg->log[best_alg->ops] = m;
2161 best_alg->op[best_alg->ops] = alg_shift;
2162 cost_limit = cost;
2163 }
819126a6
RK
2164 }
2165 }
2166
2167 /* If we have an odd number, add or subtract one. */
2168 if ((t & 1) != 0)
2169 {
2170 unsigned HOST_WIDE_INT w;
2171
2172 for (w = 1; (w & t) != 0; w <<= 1)
2173 ;
31031edd 2174 /* If T was -1, then W will be zero after the loop. This is another
c410d49e 2175 case where T ends with ...111. Handling this with (T + 1) and
31031edd
JL
2176 subtract 1 produces slightly better code and results in algorithm
2177 selection much faster than treating it like the ...0111 case
2178 below. */
2179 if (w == 0
2180 || (w > 2
2181 /* Reject the case where t is 3.
2182 Thus we prefer addition in that case. */
2183 && t != 3))
819126a6
RK
2184 {
2185 /* T ends with ...111. Multiply by (T + 1) and subtract 1. */
2186
2187 cost = add_cost;
2188 synth_mult (alg_in, t + 1, cost_limit - cost);
b2fb324c
RK
2189
2190 cost += alg_in->cost;
819126a6 2191 if (cost < cost_limit)
44037a66 2192 {
b2fb324c
RK
2193 struct algorithm *x;
2194 x = alg_in, alg_in = best_alg, best_alg = x;
819126a6
RK
2195 best_alg->log[best_alg->ops] = 0;
2196 best_alg->op[best_alg->ops] = alg_sub_t_m2;
2197 cost_limit = cost;
44037a66 2198 }
44037a66 2199 }
819126a6
RK
2200 else
2201 {
2202 /* T ends with ...01 or ...011. Multiply by (T - 1) and add 1. */
44037a66 2203
819126a6
RK
2204 cost = add_cost;
2205 synth_mult (alg_in, t - 1, cost_limit - cost);
2206
2207 cost += alg_in->cost;
2208 if (cost < cost_limit)
2209 {
2210 struct algorithm *x;
2211 x = alg_in, alg_in = best_alg, best_alg = x;
2212 best_alg->log[best_alg->ops] = 0;
2213 best_alg->op[best_alg->ops] = alg_add_t_m2;
2214 cost_limit = cost;
2215 }
2216 }
2217 }
63610db9 2218
44037a66 2219 /* Look for factors of t of the form
7963ac37 2220 t = q(2**m +- 1), 2 <= m <= floor(log2(t - 1)).
44037a66 2221 If we find such a factor, we can multiply by t using an algorithm that
7963ac37 2222 multiplies by q, shift the result by m and add/subtract it to itself.
44037a66 2223
7963ac37
RK
2224 We search for large factors first and loop down, even if large factors
2225 are less probable than small; if we find a large factor we will find a
2226 good sequence quickly, and therefore be able to prune (by decreasing
2227 COST_LIMIT) the search. */
2228
2229 for (m = floor_log2 (t - 1); m >= 2; m--)
44037a66 2230 {
7963ac37 2231 unsigned HOST_WIDE_INT d;
44037a66 2232
7963ac37 2233 d = ((unsigned HOST_WIDE_INT) 1 << m) + 1;
02a65aef 2234 if (t % d == 0 && t > d && m < BITS_PER_WORD)
44037a66 2235 {
b385aeda 2236 cost = MIN (shiftadd_cost[m], add_cost + shift_cost[m]);
819126a6 2237 synth_mult (alg_in, t / d, cost_limit - cost);
44037a66 2238
7963ac37 2239 cost += alg_in->cost;
819126a6 2240 if (cost < cost_limit)
44037a66 2241 {
7963ac37
RK
2242 struct algorithm *x;
2243 x = alg_in, alg_in = best_alg, best_alg = x;
b385aeda 2244 best_alg->log[best_alg->ops] = m;
819126a6
RK
2245 best_alg->op[best_alg->ops] = alg_add_factor;
2246 cost_limit = cost;
44037a66 2247 }
c0b262c1
TG
2248 /* Other factors will have been taken care of in the recursion. */
2249 break;
44037a66
TG
2250 }
2251
7963ac37 2252 d = ((unsigned HOST_WIDE_INT) 1 << m) - 1;
02a65aef 2253 if (t % d == 0 && t > d && m < BITS_PER_WORD)
44037a66 2254 {
b385aeda 2255 cost = MIN (shiftsub_cost[m], add_cost + shift_cost[m]);
819126a6 2256 synth_mult (alg_in, t / d, cost_limit - cost);
44037a66 2257
7963ac37 2258 cost += alg_in->cost;
819126a6 2259 if (cost < cost_limit)
44037a66 2260 {
7963ac37
RK
2261 struct algorithm *x;
2262 x = alg_in, alg_in = best_alg, best_alg = x;
b385aeda 2263 best_alg->log[best_alg->ops] = m;
819126a6
RK
2264 best_alg->op[best_alg->ops] = alg_sub_factor;
2265 cost_limit = cost;
44037a66 2266 }
c0b262c1 2267 break;
44037a66
TG
2268 }
2269 }
2270
7963ac37
RK
2271 /* Try shift-and-add (load effective address) instructions,
2272 i.e. do a*3, a*5, a*9. */
2273 if ((t & 1) != 0)
2274 {
7963ac37
RK
2275 q = t - 1;
2276 q = q & -q;
2277 m = exact_log2 (q);
02a65aef 2278 if (m >= 0 && m < BITS_PER_WORD)
b385aeda 2279 {
5eebe2eb 2280 cost = shiftadd_cost[m];
819126a6 2281 synth_mult (alg_in, (t - 1) >> m, cost_limit - cost);
5eebe2eb
RK
2282
2283 cost += alg_in->cost;
819126a6 2284 if (cost < cost_limit)
5eebe2eb
RK
2285 {
2286 struct algorithm *x;
2287 x = alg_in, alg_in = best_alg, best_alg = x;
2288 best_alg->log[best_alg->ops] = m;
819126a6
RK
2289 best_alg->op[best_alg->ops] = alg_add_t2_m;
2290 cost_limit = cost;
5eebe2eb 2291 }
7963ac37 2292 }
44037a66 2293
7963ac37
RK
2294 q = t + 1;
2295 q = q & -q;
2296 m = exact_log2 (q);
02a65aef 2297 if (m >= 0 && m < BITS_PER_WORD)
b385aeda 2298 {
5eebe2eb 2299 cost = shiftsub_cost[m];
819126a6 2300 synth_mult (alg_in, (t + 1) >> m, cost_limit - cost);
5eebe2eb
RK
2301
2302 cost += alg_in->cost;
819126a6 2303 if (cost < cost_limit)
5eebe2eb
RK
2304 {
2305 struct algorithm *x;
2306 x = alg_in, alg_in = best_alg, best_alg = x;
2307 best_alg->log[best_alg->ops] = m;
819126a6
RK
2308 best_alg->op[best_alg->ops] = alg_sub_t2_m;
2309 cost_limit = cost;
5eebe2eb 2310 }
7963ac37
RK
2311 }
2312 }
44037a66 2313
819126a6
RK
2314 /* If cost_limit has not decreased since we stored it in alg_out->cost,
2315 we have not found any algorithm. */
2316 if (cost_limit == alg_out->cost)
2317 return;
2318
52786026
RK
2319 /* If we are getting a too long sequence for `struct algorithm'
2320 to record, make this search fail. */
2321 if (best_alg->ops == MAX_BITS_PER_WORD)
2322 return;
2323
819126a6
RK
2324 /* Copy the algorithm from temporary space to the space at alg_out.
2325 We avoid using structure assignment because the majority of
2326 best_alg is normally undefined, and this is a critical function. */
2327 alg_out->ops = best_alg->ops + 1;
2328 alg_out->cost = cost_limit;
4e135bdd
KG
2329 memcpy (alg_out->op, best_alg->op,
2330 alg_out->ops * sizeof *alg_out->op);
2331 memcpy (alg_out->log, best_alg->log,
2332 alg_out->ops * sizeof *alg_out->log);
44037a66
TG
2333}
2334\f
2335/* Perform a multiplication and return an rtx for the result.
2336 MODE is mode of value; OP0 and OP1 are what to multiply (rtx's);
2337 TARGET is a suggestion for where to store the result (an rtx).
2338
2339 We check specially for a constant integer as OP1.
2340 If you want this check for OP0 as well, then before calling
2341 you should swap the two operands if OP0 would be constant. */
2342
2343rtx
2344expand_mult (mode, op0, op1, target, unsignedp)
2345 enum machine_mode mode;
b3694847 2346 rtx op0, op1, target;
44037a66
TG
2347 int unsignedp;
2348{
2349 rtx const_op1 = op1;
2350
ceb1d268
JW
2351 /* synth_mult does an `unsigned int' multiply. As long as the mode is
2352 less than or equal in size to `unsigned int' this doesn't matter.
2353 If the mode is larger than `unsigned int', then synth_mult works only
2354 if the constant value exactly fits in an `unsigned int' without any
2355 truncation. This means that multiplying by negative values does
2356 not work; results are off by 2^32 on a 32 bit machine. */
2357
44037a66
TG
2358 /* If we are multiplying in DImode, it may still be a win
2359 to try to work with shifts and adds. */
2360 if (GET_CODE (op1) == CONST_DOUBLE
2361 && GET_MODE_CLASS (GET_MODE (op1)) == MODE_INT
ceb1d268
JW
2362 && HOST_BITS_PER_INT >= BITS_PER_WORD
2363 && CONST_DOUBLE_HIGH (op1) == 0)
2364 const_op1 = GEN_INT (CONST_DOUBLE_LOW (op1));
2365 else if (HOST_BITS_PER_INT < GET_MODE_BITSIZE (mode)
2366 && GET_CODE (op1) == CONST_INT
2367 && INTVAL (op1) < 0)
2368 const_op1 = 0;
44037a66 2369
66c1f88e
RS
2370 /* We used to test optimize here, on the grounds that it's better to
2371 produce a smaller program when -O is not used.
2372 But this causes such a terrible slowdown sometimes
2373 that it seems better to use synth_mult always. */
b385aeda 2374
91ce572a
CC
2375 if (const_op1 && GET_CODE (const_op1) == CONST_INT
2376 && (unsignedp || ! flag_trapv))
44037a66
TG
2377 {
2378 struct algorithm alg;
55c2d311 2379 struct algorithm alg2;
7963ac37 2380 HOST_WIDE_INT val = INTVAL (op1);
b385aeda
RK
2381 HOST_WIDE_INT val_so_far;
2382 rtx insn;
819126a6 2383 int mult_cost;
55c2d311 2384 enum {basic_variant, negate_variant, add_variant} variant = basic_variant;
44037a66 2385
b972dd02
JH
2386 /* op0 must be register to make mult_cost match the precomputed
2387 shiftadd_cost array. */
2388 op0 = force_reg (mode, op0);
2389
55c2d311
TG
2390 /* Try to do the computation three ways: multiply by the negative of OP1
2391 and then negate, do the multiplication directly, or do multiplication
2392 by OP1 - 1. */
44037a66 2393
38a448ca 2394 mult_cost = rtx_cost (gen_rtx_MULT (mode, op0, op1), SET);
c0b262c1 2395 mult_cost = MIN (12 * add_cost, mult_cost);
819126a6
RK
2396
2397 synth_mult (&alg, val, mult_cost);
ceb1d268
JW
2398
2399 /* This works only if the inverted value actually fits in an
2400 `unsigned int' */
2401 if (HOST_BITS_PER_INT >= GET_MODE_BITSIZE (mode))
2402 {
2403 synth_mult (&alg2, - val,
2404 (alg.cost < mult_cost ? alg.cost : mult_cost) - negate_cost);
2405 if (alg2.cost + negate_cost < alg.cost)
2406 alg = alg2, variant = negate_variant;
2407 }
44037a66 2408
55c2d311 2409 /* This proves very useful for division-by-constant. */
98310eaa
RK
2410 synth_mult (&alg2, val - 1,
2411 (alg.cost < mult_cost ? alg.cost : mult_cost) - add_cost);
55c2d311
TG
2412 if (alg2.cost + add_cost < alg.cost)
2413 alg = alg2, variant = add_variant;
44037a66 2414
7963ac37 2415 if (alg.cost < mult_cost)
44037a66 2416 {
b2fb324c 2417 /* We found something cheaper than a multiply insn. */
7963ac37 2418 int opno;
44037a66 2419 rtx accum, tem;
23cb454a 2420 enum machine_mode nmode;
44037a66
TG
2421
2422 op0 = protect_from_queue (op0, 0);
2423
2424 /* Avoid referencing memory over and over.
2425 For speed, but also for correctness when mem is volatile. */
2426 if (GET_CODE (op0) == MEM)
2427 op0 = force_reg (mode, op0);
2428
b385aeda
RK
2429 /* ACCUM starts out either as OP0 or as a zero, depending on
2430 the first operation. */
2431
2432 if (alg.op[0] == alg_zero)
44037a66 2433 {
b385aeda
RK
2434 accum = copy_to_mode_reg (mode, const0_rtx);
2435 val_so_far = 0;
2436 }
2437 else if (alg.op[0] == alg_m)
2438 {
819126a6 2439 accum = copy_to_mode_reg (mode, op0);
b385aeda 2440 val_so_far = 1;
44037a66 2441 }
b385aeda
RK
2442 else
2443 abort ();
7963ac37
RK
2444
2445 for (opno = 1; opno < alg.ops; opno++)
44037a66 2446 {
b385aeda 2447 int log = alg.log[opno];
c0a08574
RK
2448 int preserve = preserve_subexpressions_p ();
2449 rtx shift_subtarget = preserve ? 0 : accum;
98310eaa
RK
2450 rtx add_target
2451 = (opno == alg.ops - 1 && target != 0 && variant != add_variant
9ec36da5
JL
2452 && ! preserve)
2453 ? target : 0;
c0a08574 2454 rtx accum_target = preserve ? 0 : accum;
c410d49e 2455
44037a66
TG
2456 switch (alg.op[opno])
2457 {
b2fb324c
RK
2458 case alg_shift:
2459 accum = expand_shift (LSHIFT_EXPR, mode, accum,
2460 build_int_2 (log, 0), NULL_RTX, 0);
b385aeda 2461 val_so_far <<= log;
b2fb324c
RK
2462 break;
2463
7963ac37 2464 case alg_add_t_m2:
b385aeda
RK
2465 tem = expand_shift (LSHIFT_EXPR, mode, op0,
2466 build_int_2 (log, 0), NULL_RTX, 0);
38a448ca 2467 accum = force_operand (gen_rtx_PLUS (mode, accum, tem),
c5c76735
JL
2468 add_target
2469 ? add_target : accum_target);
b385aeda 2470 val_so_far += (HOST_WIDE_INT) 1 << log;
44037a66
TG
2471 break;
2472
7963ac37 2473 case alg_sub_t_m2:
b385aeda
RK
2474 tem = expand_shift (LSHIFT_EXPR, mode, op0,
2475 build_int_2 (log, 0), NULL_RTX, 0);
38a448ca 2476 accum = force_operand (gen_rtx_MINUS (mode, accum, tem),
c5c76735
JL
2477 add_target
2478 ? add_target : accum_target);
b385aeda 2479 val_so_far -= (HOST_WIDE_INT) 1 << log;
7963ac37 2480 break;
44037a66 2481
7963ac37
RK
2482 case alg_add_t2_m:
2483 accum = expand_shift (LSHIFT_EXPR, mode, accum,
c0a08574
RK
2484 build_int_2 (log, 0), shift_subtarget,
2485 0);
38a448ca 2486 accum = force_operand (gen_rtx_PLUS (mode, accum, op0),
c5c76735
JL
2487 add_target
2488 ? add_target : accum_target);
b385aeda 2489 val_so_far = (val_so_far << log) + 1;
44037a66
TG
2490 break;
2491
7963ac37
RK
2492 case alg_sub_t2_m:
2493 accum = expand_shift (LSHIFT_EXPR, mode, accum,
c0a08574
RK
2494 build_int_2 (log, 0), shift_subtarget,
2495 0);
38a448ca 2496 accum = force_operand (gen_rtx_MINUS (mode, accum, op0),
c5c76735
JL
2497 add_target
2498 ? add_target : accum_target);
b385aeda 2499 val_so_far = (val_so_far << log) - 1;
7963ac37
RK
2500 break;
2501
2502 case alg_add_factor:
44037a66 2503 tem = expand_shift (LSHIFT_EXPR, mode, accum,
b1ec3c92 2504 build_int_2 (log, 0), NULL_RTX, 0);
38a448ca 2505 accum = force_operand (gen_rtx_PLUS (mode, accum, tem),
c5c76735
JL
2506 add_target
2507 ? add_target : accum_target);
b385aeda 2508 val_so_far += val_so_far << log;
7963ac37 2509 break;
44037a66 2510
7963ac37
RK
2511 case alg_sub_factor:
2512 tem = expand_shift (LSHIFT_EXPR, mode, accum,
2513 build_int_2 (log, 0), NULL_RTX, 0);
38a448ca 2514 accum = force_operand (gen_rtx_MINUS (mode, tem, accum),
c0a08574
RK
2515 (add_target ? add_target
2516 : preserve ? 0 : tem));
b385aeda 2517 val_so_far = (val_so_far << log) - val_so_far;
7963ac37 2518 break;
44037a66 2519
b385aeda 2520 default:
6d649d26 2521 abort ();
b385aeda 2522 }
44037a66 2523
b385aeda 2524 /* Write a REG_EQUAL note on the last insn so that we can cse
23cb454a
RK
2525 multiplication sequences. Note that if ACCUM is a SUBREG,
2526 we've set the inner register and must properly indicate
2527 that. */
2528
2529 tem = op0, nmode = mode;
2530 if (GET_CODE (accum) == SUBREG)
2531 {
2532 nmode = GET_MODE (SUBREG_REG (accum));
2533 tem = gen_lowpart (nmode, op0);
2534 }
44037a66 2535
b385aeda 2536 insn = get_last_insn ();
c410d49e 2537 set_unique_reg_note (insn,
7e5bda2c 2538 REG_EQUAL,
23cb454a 2539 gen_rtx_MULT (nmode, tem,
7e5bda2c 2540 GEN_INT (val_so_far)));
b385aeda 2541 }
44037a66 2542
55c2d311 2543 if (variant == negate_variant)
44037a66 2544 {
b385aeda
RK
2545 val_so_far = - val_so_far;
2546 accum = expand_unop (mode, neg_optab, accum, target, 0);
44037a66 2547 }
55c2d311
TG
2548 else if (variant == add_variant)
2549 {
2550 val_so_far = val_so_far + 1;
38a448ca 2551 accum = force_operand (gen_rtx_PLUS (mode, accum, op0), target);
55c2d311 2552 }
44037a66 2553
b385aeda
RK
2554 if (val != val_so_far)
2555 abort ();
2556
2557 return accum;
44037a66
TG
2558 }
2559 }
2560
819126a6
RK
2561 /* This used to use umul_optab if unsigned, but for non-widening multiply
2562 there is no difference between signed and unsigned. */
c410d49e 2563 op0 = expand_binop (mode,
91ce572a 2564 ! unsignedp
0fb7aeda
KH
2565 && flag_trapv && (GET_MODE_CLASS(mode) == MODE_INT)
2566 ? smulv_optab : smul_optab,
44037a66
TG
2567 op0, op1, target, unsignedp, OPTAB_LIB_WIDEN);
2568 if (op0 == 0)
2569 abort ();
2570 return op0;
2571}
2572\f
55c2d311
TG
2573/* Return the smallest n such that 2**n >= X. */
2574
2575int
2576ceil_log2 (x)
2577 unsigned HOST_WIDE_INT x;
2578{
2579 return floor_log2 (x - 1) + 1;
2580}
2581
2582/* Choose a minimal N + 1 bit approximation to 1/D that can be used to
2583 replace division by D, and put the least significant N bits of the result
2584 in *MULTIPLIER_PTR and return the most significant bit.
2585
2586 The width of operations is N (should be <= HOST_BITS_PER_WIDE_INT), the
2587 needed precision is in PRECISION (should be <= N).
2588
2589 PRECISION should be as small as possible so this function can choose
2590 multiplier more freely.
2591
2592 The rounded-up logarithm of D is placed in *lgup_ptr. A shift count that
2593 is to be used for a final right shift is placed in *POST_SHIFT_PTR.
2594
2595 Using this function, x/D will be equal to (x * m) >> (*POST_SHIFT_PTR),
2596 where m is the full HOST_BITS_PER_WIDE_INT + 1 bit multiplier. */
2597
2598static
2599unsigned HOST_WIDE_INT
2600choose_multiplier (d, n, precision, multiplier_ptr, post_shift_ptr, lgup_ptr)
2601 unsigned HOST_WIDE_INT d;
2602 int n;
2603 int precision;
2604 unsigned HOST_WIDE_INT *multiplier_ptr;
2605 int *post_shift_ptr;
2606 int *lgup_ptr;
2607{
f9e158c3
JM
2608 HOST_WIDE_INT mhigh_hi, mlow_hi;
2609 unsigned HOST_WIDE_INT mhigh_lo, mlow_lo;
55c2d311
TG
2610 int lgup, post_shift;
2611 int pow, pow2;
f9e158c3
JM
2612 unsigned HOST_WIDE_INT nl, dummy1;
2613 HOST_WIDE_INT nh, dummy2;
55c2d311
TG
2614
2615 /* lgup = ceil(log2(divisor)); */
2616 lgup = ceil_log2 (d);
2617
2618 if (lgup > n)
2619 abort ();
2620
2621 pow = n + lgup;
2622 pow2 = n + lgup - precision;
2623
2624 if (pow == 2 * HOST_BITS_PER_WIDE_INT)
2625 {
2626 /* We could handle this with some effort, but this case is much better
2627 handled directly with a scc insn, so rely on caller using that. */
2628 abort ();
2629 }
2630
2631 /* mlow = 2^(N + lgup)/d */
2632 if (pow >= HOST_BITS_PER_WIDE_INT)
2633 {
f9e158c3 2634 nh = (HOST_WIDE_INT) 1 << (pow - HOST_BITS_PER_WIDE_INT);
55c2d311
TG
2635 nl = 0;
2636 }
2637 else
2638 {
2639 nh = 0;
2640 nl = (unsigned HOST_WIDE_INT) 1 << pow;
2641 }
2642 div_and_round_double (TRUNC_DIV_EXPR, 1, nl, nh, d, (HOST_WIDE_INT) 0,
2643 &mlow_lo, &mlow_hi, &dummy1, &dummy2);
2644
2645 /* mhigh = (2^(N + lgup) + 2^N + lgup - precision)/d */
2646 if (pow2 >= HOST_BITS_PER_WIDE_INT)
f9e158c3 2647 nh |= (HOST_WIDE_INT) 1 << (pow2 - HOST_BITS_PER_WIDE_INT);
55c2d311
TG
2648 else
2649 nl |= (unsigned HOST_WIDE_INT) 1 << pow2;
2650 div_and_round_double (TRUNC_DIV_EXPR, 1, nl, nh, d, (HOST_WIDE_INT) 0,
2651 &mhigh_lo, &mhigh_hi, &dummy1, &dummy2);
2652
2653 if (mhigh_hi && nh - d >= d)
2654 abort ();
2655 if (mhigh_hi > 1 || mlow_hi > 1)
2656 abort ();
2657 /* assert that mlow < mhigh. */
2658 if (! (mlow_hi < mhigh_hi || (mlow_hi == mhigh_hi && mlow_lo < mhigh_lo)))
c4f2c499 2659 abort ();
55c2d311
TG
2660
2661 /* If precision == N, then mlow, mhigh exceed 2^N
2662 (but they do not exceed 2^(N+1)). */
2663
2664 /* Reduce to lowest terms */
2665 for (post_shift = lgup; post_shift > 0; post_shift--)
2666 {
2667 unsigned HOST_WIDE_INT ml_lo = (mlow_hi << (HOST_BITS_PER_WIDE_INT - 1)) | (mlow_lo >> 1);
2668 unsigned HOST_WIDE_INT mh_lo = (mhigh_hi << (HOST_BITS_PER_WIDE_INT - 1)) | (mhigh_lo >> 1);
2669 if (ml_lo >= mh_lo)
2670 break;
2671
2672 mlow_hi = 0;
2673 mlow_lo = ml_lo;
2674 mhigh_hi = 0;
2675 mhigh_lo = mh_lo;
2676 }
2677
2678 *post_shift_ptr = post_shift;
2679 *lgup_ptr = lgup;
2680 if (n < HOST_BITS_PER_WIDE_INT)
2681 {
2682 unsigned HOST_WIDE_INT mask = ((unsigned HOST_WIDE_INT) 1 << n) - 1;
2683 *multiplier_ptr = mhigh_lo & mask;
2684 return mhigh_lo >= mask;
2685 }
2686 else
2687 {
2688 *multiplier_ptr = mhigh_lo;
2689 return mhigh_hi;
2690 }
2691}
2692
2693/* Compute the inverse of X mod 2**n, i.e., find Y such that X * Y is
2694 congruent to 1 (mod 2**N). */
2695
2696static unsigned HOST_WIDE_INT
2697invert_mod2n (x, n)
2698 unsigned HOST_WIDE_INT x;
2699 int n;
2700{
0f41302f 2701 /* Solve x*y == 1 (mod 2^n), where x is odd. Return y. */
55c2d311
TG
2702
2703 /* The algorithm notes that the choice y = x satisfies
2704 x*y == 1 mod 2^3, since x is assumed odd.
2705 Each iteration doubles the number of bits of significance in y. */
2706
2707 unsigned HOST_WIDE_INT mask;
2708 unsigned HOST_WIDE_INT y = x;
2709 int nbit = 3;
2710
2711 mask = (n == HOST_BITS_PER_WIDE_INT
2712 ? ~(unsigned HOST_WIDE_INT) 0
2713 : ((unsigned HOST_WIDE_INT) 1 << n) - 1);
2714
2715 while (nbit < n)
2716 {
2717 y = y * (2 - x*y) & mask; /* Modulo 2^N */
2718 nbit *= 2;
2719 }
2720 return y;
2721}
2722
2723/* Emit code to adjust ADJ_OPERAND after multiplication of wrong signedness
2724 flavor of OP0 and OP1. ADJ_OPERAND is already the high half of the
2725 product OP0 x OP1. If UNSIGNEDP is nonzero, adjust the signed product
2726 to become unsigned, if UNSIGNEDP is zero, adjust the unsigned product to
2727 become signed.
2728
2729 The result is put in TARGET if that is convenient.
2730
2731 MODE is the mode of operation. */
2732
2733rtx
2734expand_mult_highpart_adjust (mode, adj_operand, op0, op1, target, unsignedp)
2735 enum machine_mode mode;
b3694847 2736 rtx adj_operand, op0, op1, target;
55c2d311
TG
2737 int unsignedp;
2738{
2739 rtx tem;
2740 enum rtx_code adj_code = unsignedp ? PLUS : MINUS;
2741
2742 tem = expand_shift (RSHIFT_EXPR, mode, op0,
2743 build_int_2 (GET_MODE_BITSIZE (mode) - 1, 0),
2744 NULL_RTX, 0);
22273300 2745 tem = expand_and (mode, tem, op1, NULL_RTX);
38a448ca
RH
2746 adj_operand
2747 = force_operand (gen_rtx_fmt_ee (adj_code, mode, adj_operand, tem),
2748 adj_operand);
55c2d311
TG
2749
2750 tem = expand_shift (RSHIFT_EXPR, mode, op1,
2751 build_int_2 (GET_MODE_BITSIZE (mode) - 1, 0),
2752 NULL_RTX, 0);
22273300 2753 tem = expand_and (mode, tem, op0, NULL_RTX);
38a448ca
RH
2754 target = force_operand (gen_rtx_fmt_ee (adj_code, mode, adj_operand, tem),
2755 target);
55c2d311
TG
2756
2757 return target;
2758}
2759
2760/* Emit code to multiply OP0 and CNST1, putting the high half of the result
2761 in TARGET if that is convenient, and return where the result is. If the
2762 operation can not be performed, 0 is returned.
2763
2764 MODE is the mode of operation and result.
2765
71af73bb
TG
2766 UNSIGNEDP nonzero means unsigned multiply.
2767
2768 MAX_COST is the total allowed cost for the expanded RTL. */
55c2d311
TG
2769
2770rtx
71af73bb 2771expand_mult_highpart (mode, op0, cnst1, target, unsignedp, max_cost)
55c2d311 2772 enum machine_mode mode;
b3694847 2773 rtx op0, target;
55c2d311
TG
2774 unsigned HOST_WIDE_INT cnst1;
2775 int unsignedp;
71af73bb 2776 int max_cost;
55c2d311
TG
2777{
2778 enum machine_mode wider_mode = GET_MODE_WIDER_MODE (mode);
2779 optab mul_highpart_optab;
2780 optab moptab;
2781 rtx tem;
2782 int size = GET_MODE_BITSIZE (mode);
5b0ce758 2783 rtx op1, wide_op1;
55c2d311 2784
0865c631 2785 /* We can't support modes wider than HOST_BITS_PER_INT. */
5b0ce758
RK
2786 if (size > HOST_BITS_PER_WIDE_INT)
2787 abort ();
2788
2496c7bd 2789 op1 = gen_int_mode (cnst1, mode);
5b0ce758 2790
d3c52658
JJ
2791 wide_op1
2792 = immed_double_const (cnst1,
2793 (unsignedp
2794 ? (HOST_WIDE_INT) 0
2795 : -(cnst1 >> (HOST_BITS_PER_WIDE_INT - 1))),
2796 wider_mode);
55c2d311
TG
2797
2798 /* expand_mult handles constant multiplication of word_mode
2799 or narrower. It does a poor job for large modes. */
71af73bb
TG
2800 if (size < BITS_PER_WORD
2801 && mul_cost[(int) wider_mode] + shift_cost[size-1] < max_cost)
55c2d311
TG
2802 {
2803 /* We have to do this, since expand_binop doesn't do conversion for
2804 multiply. Maybe change expand_binop to handle widening multiply? */
2805 op0 = convert_to_mode (wider_mode, op0, unsignedp);
2806
91ce572a
CC
2807 /* We know that this can't have signed overflow, so pretend this is
2808 an unsigned multiply. */
2809 tem = expand_mult (wider_mode, op0, wide_op1, NULL_RTX, 0);
55c2d311
TG
2810 tem = expand_shift (RSHIFT_EXPR, wider_mode, tem,
2811 build_int_2 (size, 0), NULL_RTX, 1);
2f97afcb 2812 return convert_modes (mode, wider_mode, tem, unsignedp);
55c2d311
TG
2813 }
2814
2815 if (target == 0)
2816 target = gen_reg_rtx (mode);
2817
2818 /* Firstly, try using a multiplication insn that only generates the needed
2819 high part of the product, and in the sign flavor of unsignedp. */
71af73bb
TG
2820 if (mul_highpart_cost[(int) mode] < max_cost)
2821 {
2822 mul_highpart_optab = unsignedp ? umul_highpart_optab : smul_highpart_optab;
2823 target = expand_binop (mode, mul_highpart_optab,
3b5286d7 2824 op0, op1, target, unsignedp, OPTAB_DIRECT);
71af73bb
TG
2825 if (target)
2826 return target;
2827 }
55c2d311
TG
2828
2829 /* Secondly, same as above, but use sign flavor opposite of unsignedp.
2830 Need to adjust the result after the multiplication. */
02a65aef
R
2831 if (size - 1 < BITS_PER_WORD
2832 && (mul_highpart_cost[(int) mode] + 2 * shift_cost[size-1] + 4 * add_cost
2833 < max_cost))
71af73bb
TG
2834 {
2835 mul_highpart_optab = unsignedp ? smul_highpart_optab : umul_highpart_optab;
2836 target = expand_binop (mode, mul_highpart_optab,
3b5286d7 2837 op0, op1, target, unsignedp, OPTAB_DIRECT);
71af73bb
TG
2838 if (target)
2839 /* We used the wrong signedness. Adjust the result. */
2840 return expand_mult_highpart_adjust (mode, target, op0,
2841 op1, target, unsignedp);
2842 }
55c2d311 2843
71af73bb 2844 /* Try widening multiplication. */
55c2d311 2845 moptab = unsignedp ? umul_widen_optab : smul_widen_optab;
71af73bb
TG
2846 if (moptab->handlers[(int) wider_mode].insn_code != CODE_FOR_nothing
2847 && mul_widen_cost[(int) wider_mode] < max_cost)
a295d331
TG
2848 {
2849 op1 = force_reg (mode, op1);
2850 goto try;
c410d49e 2851 }
71af73bb
TG
2852
2853 /* Try widening the mode and perform a non-widening multiplication. */
2854 moptab = smul_optab;
2855 if (smul_optab->handlers[(int) wider_mode].insn_code != CODE_FOR_nothing
02a65aef 2856 && size - 1 < BITS_PER_WORD
71af73bb 2857 && mul_cost[(int) wider_mode] + shift_cost[size-1] < max_cost)
a295d331
TG
2858 {
2859 op1 = wide_op1;
2860 goto try;
2861 }
71af73bb
TG
2862
2863 /* Try widening multiplication of opposite signedness, and adjust. */
2864 moptab = unsignedp ? smul_widen_optab : umul_widen_optab;
2865 if (moptab->handlers[(int) wider_mode].insn_code != CODE_FOR_nothing
02a65aef 2866 && size - 1 < BITS_PER_WORD
71af73bb
TG
2867 && (mul_widen_cost[(int) wider_mode]
2868 + 2 * shift_cost[size-1] + 4 * add_cost < max_cost))
55c2d311 2869 {
a295d331
TG
2870 rtx regop1 = force_reg (mode, op1);
2871 tem = expand_binop (wider_mode, moptab, op0, regop1,
71af73bb
TG
2872 NULL_RTX, ! unsignedp, OPTAB_WIDEN);
2873 if (tem != 0)
55c2d311 2874 {
71af73bb
TG
2875 /* Extract the high half of the just generated product. */
2876 tem = expand_shift (RSHIFT_EXPR, wider_mode, tem,
2877 build_int_2 (size, 0), NULL_RTX, 1);
2878 tem = convert_modes (mode, wider_mode, tem, unsignedp);
2879 /* We used the wrong signedness. Adjust the result. */
2880 return expand_mult_highpart_adjust (mode, tem, op0, op1,
2881 target, unsignedp);
55c2d311 2882 }
55c2d311
TG
2883 }
2884
71af73bb
TG
2885 return 0;
2886
2887 try:
55c2d311 2888 /* Pass NULL_RTX as target since TARGET has wrong mode. */
a295d331 2889 tem = expand_binop (wider_mode, moptab, op0, op1,
55c2d311
TG
2890 NULL_RTX, unsignedp, OPTAB_WIDEN);
2891 if (tem == 0)
2892 return 0;
2893
2894 /* Extract the high half of the just generated product. */
a295d331
TG
2895 if (mode == word_mode)
2896 {
2897 return gen_highpart (mode, tem);
2898 }
2899 else
2900 {
2901 tem = expand_shift (RSHIFT_EXPR, wider_mode, tem,
2902 build_int_2 (size, 0), NULL_RTX, 1);
2903 return convert_modes (mode, wider_mode, tem, unsignedp);
2904 }
55c2d311
TG
2905}
2906\f
44037a66
TG
2907/* Emit the code to divide OP0 by OP1, putting the result in TARGET
2908 if that is convenient, and returning where the result is.
2909 You may request either the quotient or the remainder as the result;
2910 specify REM_FLAG nonzero to get the remainder.
2911
2912 CODE is the expression code for which kind of division this is;
2913 it controls how rounding is done. MODE is the machine mode to use.
2914 UNSIGNEDP nonzero means do unsigned division. */
2915
2916/* ??? For CEIL_MOD_EXPR, can compute incorrect remainder with ANDI
2917 and then correct it by or'ing in missing high bits
2918 if result of ANDI is nonzero.
2919 For ROUND_MOD_EXPR, can use ANDI and then sign-extend the result.
2920 This could optimize to a bfexts instruction.
2921 But C doesn't use these operations, so their optimizations are
2922 left for later. */
5353610b
R
2923/* ??? For modulo, we don't actually need the highpart of the first product,
2924 the low part will do nicely. And for small divisors, the second multiply
2925 can also be a low-part only multiply or even be completely left out.
2926 E.g. to calculate the remainder of a division by 3 with a 32 bit
2927 multiply, multiply with 0x55555556 and extract the upper two bits;
2928 the result is exact for inputs up to 0x1fffffff.
2929 The input range can be reduced by using cross-sum rules.
2930 For odd divisors >= 3, the following table gives right shift counts
09da1532 2931 so that if a number is shifted by an integer multiple of the given
5353610b
R
2932 amount, the remainder stays the same:
2933 2, 4, 3, 6, 10, 12, 4, 8, 18, 6, 11, 20, 18, 0, 5, 10, 12, 0, 12, 20,
2934 14, 12, 23, 21, 8, 0, 20, 18, 0, 0, 6, 12, 0, 22, 0, 18, 20, 30, 0, 0,
2935 0, 8, 0, 11, 12, 10, 36, 0, 30, 0, 0, 12, 0, 0, 0, 0, 44, 12, 24, 0,
2936 20, 0, 7, 14, 0, 18, 36, 0, 0, 46, 60, 0, 42, 0, 15, 24, 20, 0, 0, 33,
2937 0, 20, 0, 0, 18, 0, 60, 0, 0, 0, 0, 0, 40, 18, 0, 0, 12
2938
2939 Cross-sum rules for even numbers can be derived by leaving as many bits
2940 to the right alone as the divisor has zeros to the right.
2941 E.g. if x is an unsigned 32 bit number:
2942 (x mod 12) == (((x & 1023) + ((x >> 8) & ~3)) * 0x15555558 >> 2 * 3) >> 28
2943 */
44037a66 2944
55c2d311
TG
2945#define EXACT_POWER_OF_2_OR_ZERO_P(x) (((x) & ((x) - 1)) == 0)
2946
44037a66
TG
2947rtx
2948expand_divmod (rem_flag, code, mode, op0, op1, target, unsignedp)
2949 int rem_flag;
2950 enum tree_code code;
2951 enum machine_mode mode;
b3694847 2952 rtx op0, op1, target;
44037a66
TG
2953 int unsignedp;
2954{
44037a66 2955 enum machine_mode compute_mode;
b3694847 2956 rtx tquotient;
55c2d311
TG
2957 rtx quotient = 0, remainder = 0;
2958 rtx last;
2c414fba 2959 int size;
4e430df8 2960 rtx insn, set;
44037a66 2961 optab optab1, optab2;
1c4a429a 2962 int op1_is_constant, op1_is_pow2 = 0;
71af73bb 2963 int max_cost, extra_cost;
9ec36da5 2964 static HOST_WIDE_INT last_div_const = 0;
1c4a429a 2965 static HOST_WIDE_INT ext_op1;
55c2d311
TG
2966
2967 op1_is_constant = GET_CODE (op1) == CONST_INT;
1c4a429a
JH
2968 if (op1_is_constant)
2969 {
2970 ext_op1 = INTVAL (op1);
2971 if (unsignedp)
2972 ext_op1 &= GET_MODE_MASK (mode);
2973 op1_is_pow2 = ((EXACT_POWER_OF_2_OR_ZERO_P (ext_op1)
2974 || (! unsignedp && EXACT_POWER_OF_2_OR_ZERO_P (-ext_op1))));
2975 }
55c2d311
TG
2976
2977 /*
2978 This is the structure of expand_divmod:
2979
2980 First comes code to fix up the operands so we can perform the operations
2981 correctly and efficiently.
2982
2983 Second comes a switch statement with code specific for each rounding mode.
2984 For some special operands this code emits all RTL for the desired
69f61901 2985 operation, for other cases, it generates only a quotient and stores it in
55c2d311
TG
2986 QUOTIENT. The case for trunc division/remainder might leave quotient = 0,
2987 to indicate that it has not done anything.
2988
69f61901
RK
2989 Last comes code that finishes the operation. If QUOTIENT is set and
2990 REM_FLAG is set, the remainder is computed as OP0 - QUOTIENT * OP1. If
2991 QUOTIENT is not set, it is computed using trunc rounding.
44037a66 2992
55c2d311
TG
2993 We try to generate special code for division and remainder when OP1 is a
2994 constant. If |OP1| = 2**n we can use shifts and some other fast
2995 operations. For other values of OP1, we compute a carefully selected
2996 fixed-point approximation m = 1/OP1, and generate code that multiplies OP0
2997 by m.
2998
2999 In all cases but EXACT_DIV_EXPR, this multiplication requires the upper
3000 half of the product. Different strategies for generating the product are
3001 implemented in expand_mult_highpart.
3002
3003 If what we actually want is the remainder, we generate that by another
3004 by-constant multiplication and a subtraction. */
3005
3006 /* We shouldn't be called with OP1 == const1_rtx, but some of the
3d32ffd1
TW
3007 code below will malfunction if we are, so check here and handle
3008 the special case if so. */
3009 if (op1 == const1_rtx)
3010 return rem_flag ? const0_rtx : op0;
3011
91ce572a
CC
3012 /* When dividing by -1, we could get an overflow.
3013 negv_optab can handle overflows. */
3014 if (! unsignedp && op1 == constm1_rtx)
3015 {
3016 if (rem_flag)
0fb7aeda 3017 return const0_rtx;
91ce572a 3018 return expand_unop (mode, flag_trapv && GET_MODE_CLASS(mode) == MODE_INT
0fb7aeda 3019 ? negv_optab : neg_optab, op0, target, 0);
91ce572a
CC
3020 }
3021
bc1c7e93
RK
3022 if (target
3023 /* Don't use the function value register as a target
3024 since we have to read it as well as write it,
3025 and function-inlining gets confused by this. */
3026 && ((REG_P (target) && REG_FUNCTION_VALUE_P (target))
3027 /* Don't clobber an operand while doing a multi-step calculation. */
515dfc7a 3028 || ((rem_flag || op1_is_constant)
bc1c7e93
RK
3029 && (reg_mentioned_p (target, op0)
3030 || (GET_CODE (op0) == MEM && GET_CODE (target) == MEM)))
3031 || reg_mentioned_p (target, op1)
3032 || (GET_CODE (op1) == MEM && GET_CODE (target) == MEM)))
44037a66
TG
3033 target = 0;
3034
44037a66
TG
3035 /* Get the mode in which to perform this computation. Normally it will
3036 be MODE, but sometimes we can't do the desired operation in MODE.
3037 If so, pick a wider mode in which we can do the operation. Convert
3038 to that mode at the start to avoid repeated conversions.
3039
3040 First see what operations we need. These depend on the expression
3041 we are evaluating. (We assume that divxx3 insns exist under the
3042 same conditions that modxx3 insns and that these insns don't normally
3043 fail. If these assumptions are not correct, we may generate less
3044 efficient code in some cases.)
3045
3046 Then see if we find a mode in which we can open-code that operation
3047 (either a division, modulus, or shift). Finally, check for the smallest
3048 mode for which we can do the operation with a library call. */
3049
55c2d311
TG
3050 /* We might want to refine this now that we have division-by-constant
3051 optimization. Since expand_mult_highpart tries so many variants, it is
3052 not straightforward to generalize this. Maybe we should make an array
3053 of possible modes in init_expmed? Save this for GCC 2.7. */
3054
556a56ac
DM
3055 optab1 = ((op1_is_pow2 && op1 != const0_rtx)
3056 ? (unsignedp ? lshr_optab : ashr_optab)
44037a66 3057 : (unsignedp ? udiv_optab : sdiv_optab));
556a56ac
DM
3058 optab2 = ((op1_is_pow2 && op1 != const0_rtx)
3059 ? optab1
3060 : (unsignedp ? udivmod_optab : sdivmod_optab));
44037a66
TG
3061
3062 for (compute_mode = mode; compute_mode != VOIDmode;
3063 compute_mode = GET_MODE_WIDER_MODE (compute_mode))
3064 if (optab1->handlers[(int) compute_mode].insn_code != CODE_FOR_nothing
3065 || optab2->handlers[(int) compute_mode].insn_code != CODE_FOR_nothing)
3066 break;
3067
3068 if (compute_mode == VOIDmode)
3069 for (compute_mode = mode; compute_mode != VOIDmode;
3070 compute_mode = GET_MODE_WIDER_MODE (compute_mode))
3071 if (optab1->handlers[(int) compute_mode].libfunc
3072 || optab2->handlers[(int) compute_mode].libfunc)
3073 break;
3074
bc1c7e93
RK
3075 /* If we still couldn't find a mode, use MODE, but we'll probably abort
3076 in expand_binop. */
44037a66
TG
3077 if (compute_mode == VOIDmode)
3078 compute_mode = mode;
3079
55c2d311
TG
3080 if (target && GET_MODE (target) == compute_mode)
3081 tquotient = target;
3082 else
3083 tquotient = gen_reg_rtx (compute_mode);
2c414fba 3084
55c2d311
TG
3085 size = GET_MODE_BITSIZE (compute_mode);
3086#if 0
3087 /* It should be possible to restrict the precision to GET_MODE_BITSIZE
71af73bb
TG
3088 (mode), and thereby get better code when OP1 is a constant. Do that
3089 later. It will require going over all usages of SIZE below. */
55c2d311
TG
3090 size = GET_MODE_BITSIZE (mode);
3091#endif
bc1c7e93 3092
9ec36da5
JL
3093 /* Only deduct something for a REM if the last divide done was
3094 for a different constant. Then set the constant of the last
3095 divide. */
71af73bb 3096 max_cost = div_cost[(int) compute_mode]
9ec36da5
JL
3097 - (rem_flag && ! (last_div_const != 0 && op1_is_constant
3098 && INTVAL (op1) == last_div_const)
3099 ? mul_cost[(int) compute_mode] + add_cost : 0);
3100
3101 last_div_const = ! rem_flag && op1_is_constant ? INTVAL (op1) : 0;
71af73bb 3102
55c2d311 3103 /* Now convert to the best mode to use. */
44037a66
TG
3104 if (compute_mode != mode)
3105 {
55c2d311 3106 op0 = convert_modes (compute_mode, mode, op0, unsignedp);
81722fa9 3107 op1 = convert_modes (compute_mode, mode, op1, unsignedp);
e13a25d5 3108
e9a25f70
JL
3109 /* convert_modes may have placed op1 into a register, so we
3110 must recompute the following. */
e13a25d5
DM
3111 op1_is_constant = GET_CODE (op1) == CONST_INT;
3112 op1_is_pow2 = (op1_is_constant
3113 && ((EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1))
3114 || (! unsignedp
e9a25f70 3115 && EXACT_POWER_OF_2_OR_ZERO_P (-INTVAL (op1)))))) ;
44037a66
TG
3116 }
3117
55c2d311 3118 /* If one of the operands is a volatile MEM, copy it into a register. */
c2a47e48 3119
55c2d311
TG
3120 if (GET_CODE (op0) == MEM && MEM_VOLATILE_P (op0))
3121 op0 = force_reg (compute_mode, op0);
3122 if (GET_CODE (op1) == MEM && MEM_VOLATILE_P (op1))
c2a47e48
RK
3123 op1 = force_reg (compute_mode, op1);
3124
ab0b6581
TG
3125 /* If we need the remainder or if OP1 is constant, we need to
3126 put OP0 in a register in case it has any queued subexpressions. */
3127 if (rem_flag || op1_is_constant)
3128 op0 = force_reg (compute_mode, op0);
bc1c7e93 3129
55c2d311 3130 last = get_last_insn ();
44037a66 3131
9faa82d8 3132 /* Promote floor rounding to trunc rounding for unsigned operations. */
55c2d311 3133 if (unsignedp)
44037a66 3134 {
55c2d311
TG
3135 if (code == FLOOR_DIV_EXPR)
3136 code = TRUNC_DIV_EXPR;
3137 if (code == FLOOR_MOD_EXPR)
3138 code = TRUNC_MOD_EXPR;
db7cafb0
JL
3139 if (code == EXACT_DIV_EXPR && op1_is_pow2)
3140 code = TRUNC_DIV_EXPR;
55c2d311 3141 }
bc1c7e93 3142
55c2d311
TG
3143 if (op1 != const0_rtx)
3144 switch (code)
3145 {
3146 case TRUNC_MOD_EXPR:
3147 case TRUNC_DIV_EXPR:
34f016ed 3148 if (op1_is_constant)
55c2d311 3149 {
d8f1376c 3150 if (unsignedp)
55c2d311
TG
3151 {
3152 unsigned HOST_WIDE_INT mh, ml;
3153 int pre_shift, post_shift;
3154 int dummy;
1c4a429a
JH
3155 unsigned HOST_WIDE_INT d = (INTVAL (op1)
3156 & GET_MODE_MASK (compute_mode));
55c2d311
TG
3157
3158 if (EXACT_POWER_OF_2_OR_ZERO_P (d))
3159 {
3160 pre_shift = floor_log2 (d);
3161 if (rem_flag)
3162 {
db3cf6fb
MS
3163 remainder
3164 = expand_binop (compute_mode, and_optab, op0,
3165 GEN_INT (((HOST_WIDE_INT) 1 << pre_shift) - 1),
3166 remainder, 1,
3167 OPTAB_LIB_WIDEN);
55c2d311 3168 if (remainder)
c8dbc8ca 3169 return gen_lowpart (mode, remainder);
55c2d311
TG
3170 }
3171 quotient = expand_shift (RSHIFT_EXPR, compute_mode, op0,
3172 build_int_2 (pre_shift, 0),
3173 tquotient, 1);
3174 }
34f016ed 3175 else if (size <= HOST_BITS_PER_WIDE_INT)
55c2d311 3176 {
dc1d6150 3177 if (d >= ((unsigned HOST_WIDE_INT) 1 << (size - 1)))
55c2d311 3178 {
dc1d6150
TG
3179 /* Most significant bit of divisor is set; emit an scc
3180 insn. */
3181 quotient = emit_store_flag (tquotient, GEU, op0, op1,
3182 compute_mode, 1, 1);
3183 if (quotient == 0)
55c2d311 3184 goto fail1;
55c2d311
TG
3185 }
3186 else
3187 {
dc1d6150
TG
3188 /* Find a suitable multiplier and right shift count
3189 instead of multiplying with D. */
3190
3191 mh = choose_multiplier (d, size, size,
3192 &ml, &post_shift, &dummy);
3193
3194 /* If the suggested multiplier is more than SIZE bits,
3195 we can do better for even divisors, using an
3196 initial right shift. */
3197 if (mh != 0 && (d & 1) == 0)
3198 {
3199 pre_shift = floor_log2 (d & -d);
3200 mh = choose_multiplier (d >> pre_shift, size,
3201 size - pre_shift,
3202 &ml, &post_shift, &dummy);
3203 if (mh)
3204 abort ();
3205 }
3206 else
3207 pre_shift = 0;
3208
3209 if (mh != 0)
3210 {
3211 rtx t1, t2, t3, t4;
3212
02a65aef
R
3213 if (post_shift - 1 >= BITS_PER_WORD)
3214 goto fail1;
3215
dc1d6150
TG
3216 extra_cost = (shift_cost[post_shift - 1]
3217 + shift_cost[1] + 2 * add_cost);
3218 t1 = expand_mult_highpart (compute_mode, op0, ml,
3219 NULL_RTX, 1,
3220 max_cost - extra_cost);
3221 if (t1 == 0)
3222 goto fail1;
38a448ca
RH
3223 t2 = force_operand (gen_rtx_MINUS (compute_mode,
3224 op0, t1),
dc1d6150
TG
3225 NULL_RTX);
3226 t3 = expand_shift (RSHIFT_EXPR, compute_mode, t2,
3227 build_int_2 (1, 0), NULL_RTX,1);
38a448ca
RH
3228 t4 = force_operand (gen_rtx_PLUS (compute_mode,
3229 t1, t3),
dc1d6150 3230 NULL_RTX);
db3cf6fb
MS
3231 quotient
3232 = expand_shift (RSHIFT_EXPR, compute_mode, t4,
3233 build_int_2 (post_shift - 1, 0),
3234 tquotient, 1);
dc1d6150
TG
3235 }
3236 else
3237 {
3238 rtx t1, t2;
3239
02a65aef
R
3240 if (pre_shift >= BITS_PER_WORD
3241 || post_shift >= BITS_PER_WORD)
3242 goto fail1;
3243
dc1d6150
TG
3244 t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
3245 build_int_2 (pre_shift, 0),
3246 NULL_RTX, 1);
3247 extra_cost = (shift_cost[pre_shift]
3248 + shift_cost[post_shift]);
3249 t2 = expand_mult_highpart (compute_mode, t1, ml,
3250 NULL_RTX, 1,
3251 max_cost - extra_cost);
3252 if (t2 == 0)
3253 goto fail1;
db3cf6fb
MS
3254 quotient
3255 = expand_shift (RSHIFT_EXPR, compute_mode, t2,
3256 build_int_2 (post_shift, 0),
3257 tquotient, 1);
dc1d6150 3258 }
55c2d311
TG
3259 }
3260 }
34f016ed
TG
3261 else /* Too wide mode to use tricky code */
3262 break;
55c2d311
TG
3263
3264 insn = get_last_insn ();
4e430df8
RK
3265 if (insn != last
3266 && (set = single_set (insn)) != 0
3267 && SET_DEST (set) == quotient)
c410d49e 3268 set_unique_reg_note (insn,
7e5bda2c
AM
3269 REG_EQUAL,
3270 gen_rtx_UDIV (compute_mode, op0, op1));
55c2d311
TG
3271 }
3272 else /* TRUNC_DIV, signed */
3273 {
3274 unsigned HOST_WIDE_INT ml;
3275 int lgup, post_shift;
3276 HOST_WIDE_INT d = INTVAL (op1);
3277 unsigned HOST_WIDE_INT abs_d = d >= 0 ? d : -d;
3278
3279 /* n rem d = n rem -d */
3280 if (rem_flag && d < 0)
3281 {
3282 d = abs_d;
2496c7bd 3283 op1 = gen_int_mode (abs_d, compute_mode);
55c2d311
TG
3284 }
3285
3286 if (d == 1)
3287 quotient = op0;
3288 else if (d == -1)
3289 quotient = expand_unop (compute_mode, neg_optab, op0,
3290 tquotient, 0);
f737b132
RK
3291 else if (abs_d == (unsigned HOST_WIDE_INT) 1 << (size - 1))
3292 {
3293 /* This case is not handled correctly below. */
3294 quotient = emit_store_flag (tquotient, EQ, op0, op1,
3295 compute_mode, 1, 1);
3296 if (quotient == 0)
3297 goto fail1;
3298 }
55c2d311 3299 else if (EXACT_POWER_OF_2_OR_ZERO_P (d)
a8c7e72d
RH
3300 && (rem_flag ? smod_pow2_cheap : sdiv_pow2_cheap)
3301 /* ??? The cheap metric is computed only for
3302 word_mode. If this operation is wider, this may
3303 not be so. Assume true if the optab has an
3304 expander for this mode. */
3305 && (((rem_flag ? smod_optab : sdiv_optab)
3306 ->handlers[(int) compute_mode].insn_code
3307 != CODE_FOR_nothing)
3308 || (sdivmod_optab->handlers[(int) compute_mode]
3309 .insn_code != CODE_FOR_nothing)))
55c2d311
TG
3310 ;
3311 else if (EXACT_POWER_OF_2_OR_ZERO_P (abs_d))
3312 {
3313 lgup = floor_log2 (abs_d);
1cf7847f 3314 if (BRANCH_COST < 1 || (abs_d != 2 && BRANCH_COST < 3))
55c2d311
TG
3315 {
3316 rtx label = gen_label_rtx ();
3317 rtx t1;
3318
3319 t1 = copy_to_mode_reg (compute_mode, op0);
f5963e61
JL
3320 do_cmp_and_jump (t1, const0_rtx, GE,
3321 compute_mode, label);
2496c7bd
LB
3322 expand_inc (t1, gen_int_mode (abs_d - 1,
3323 compute_mode));
55c2d311
TG
3324 emit_label (label);
3325 quotient = expand_shift (RSHIFT_EXPR, compute_mode, t1,
3326 build_int_2 (lgup, 0),
3327 tquotient, 0);
3328 }
3329 else
3330 {
3331 rtx t1, t2, t3;
3332 t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
3333 build_int_2 (size - 1, 0),
3334 NULL_RTX, 0);
3335 t2 = expand_shift (RSHIFT_EXPR, compute_mode, t1,
3336 build_int_2 (size - lgup, 0),
3337 NULL_RTX, 1);
38a448ca
RH
3338 t3 = force_operand (gen_rtx_PLUS (compute_mode,
3339 op0, t2),
55c2d311
TG
3340 NULL_RTX);
3341 quotient = expand_shift (RSHIFT_EXPR, compute_mode, t3,
3342 build_int_2 (lgup, 0),
3343 tquotient, 0);
3344 }
3345
e8031612
RK
3346 /* We have computed OP0 / abs(OP1). If OP1 is negative, negate
3347 the quotient. */
55c2d311
TG
3348 if (d < 0)
3349 {
3350 insn = get_last_insn ();
4e430df8
RK
3351 if (insn != last
3352 && (set = single_set (insn)) != 0
c8e7fe58
DE
3353 && SET_DEST (set) == quotient
3354 && abs_d < ((unsigned HOST_WIDE_INT) 1
3355 << (HOST_BITS_PER_WIDE_INT - 1)))
c410d49e 3356 set_unique_reg_note (insn,
7e5bda2c
AM
3357 REG_EQUAL,
3358 gen_rtx_DIV (compute_mode,
3359 op0,
69107307
AO
3360 GEN_INT
3361 (trunc_int_for_mode
3362 (abs_d,
3363 compute_mode))));
55c2d311
TG
3364
3365 quotient = expand_unop (compute_mode, neg_optab,
3366 quotient, quotient, 0);
3367 }
3368 }
34f016ed 3369 else if (size <= HOST_BITS_PER_WIDE_INT)
55c2d311
TG
3370 {
3371 choose_multiplier (abs_d, size, size - 1,
3372 &ml, &post_shift, &lgup);
3373 if (ml < (unsigned HOST_WIDE_INT) 1 << (size - 1))
3374 {
3375 rtx t1, t2, t3;
3376
02a65aef
R
3377 if (post_shift >= BITS_PER_WORD
3378 || size - 1 >= BITS_PER_WORD)
3379 goto fail1;
3380
71af73bb
TG
3381 extra_cost = (shift_cost[post_shift]
3382 + shift_cost[size - 1] + add_cost);
55c2d311 3383 t1 = expand_mult_highpart (compute_mode, op0, ml,
71af73bb
TG
3384 NULL_RTX, 0,
3385 max_cost - extra_cost);
55c2d311
TG
3386 if (t1 == 0)
3387 goto fail1;
3388 t2 = expand_shift (RSHIFT_EXPR, compute_mode, t1,
3389 build_int_2 (post_shift, 0), NULL_RTX, 0);
3390 t3 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
3391 build_int_2 (size - 1, 0), NULL_RTX, 0);
3392 if (d < 0)
c5c76735
JL
3393 quotient
3394 = force_operand (gen_rtx_MINUS (compute_mode,
3395 t3, t2),
3396 tquotient);
55c2d311 3397 else
c5c76735
JL
3398 quotient
3399 = force_operand (gen_rtx_MINUS (compute_mode,
3400 t2, t3),
3401 tquotient);
55c2d311
TG
3402 }
3403 else
3404 {
3405 rtx t1, t2, t3, t4;
3406
02a65aef
R
3407 if (post_shift >= BITS_PER_WORD
3408 || size - 1 >= BITS_PER_WORD)
3409 goto fail1;
3410
55c2d311 3411 ml |= (~(unsigned HOST_WIDE_INT) 0) << (size - 1);
71af73bb
TG
3412 extra_cost = (shift_cost[post_shift]
3413 + shift_cost[size - 1] + 2 * add_cost);
55c2d311 3414 t1 = expand_mult_highpart (compute_mode, op0, ml,
71af73bb
TG
3415 NULL_RTX, 0,
3416 max_cost - extra_cost);
55c2d311
TG
3417 if (t1 == 0)
3418 goto fail1;
c5c76735
JL
3419 t2 = force_operand (gen_rtx_PLUS (compute_mode,
3420 t1, op0),
55c2d311
TG
3421 NULL_RTX);
3422 t3 = expand_shift (RSHIFT_EXPR, compute_mode, t2,
c5c76735
JL
3423 build_int_2 (post_shift, 0),
3424 NULL_RTX, 0);
55c2d311 3425 t4 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
c5c76735
JL
3426 build_int_2 (size - 1, 0),
3427 NULL_RTX, 0);
55c2d311 3428 if (d < 0)
c5c76735
JL
3429 quotient
3430 = force_operand (gen_rtx_MINUS (compute_mode,
3431 t4, t3),
3432 tquotient);
55c2d311 3433 else
c5c76735
JL
3434 quotient
3435 = force_operand (gen_rtx_MINUS (compute_mode,
3436 t3, t4),
3437 tquotient);
55c2d311
TG
3438 }
3439 }
34f016ed
TG
3440 else /* Too wide mode to use tricky code */
3441 break;
55c2d311 3442
4e430df8
RK
3443 insn = get_last_insn ();
3444 if (insn != last
3445 && (set = single_set (insn)) != 0
3446 && SET_DEST (set) == quotient)
c410d49e 3447 set_unique_reg_note (insn,
7e5bda2c
AM
3448 REG_EQUAL,
3449 gen_rtx_DIV (compute_mode, op0, op1));
55c2d311
TG
3450 }
3451 break;
3452 }
3453 fail1:
3454 delete_insns_since (last);
3455 break;
44037a66 3456
55c2d311
TG
3457 case FLOOR_DIV_EXPR:
3458 case FLOOR_MOD_EXPR:
3459 /* We will come here only for signed operations. */
3460 if (op1_is_constant && HOST_BITS_PER_WIDE_INT >= size)
3461 {
3462 unsigned HOST_WIDE_INT mh, ml;
3463 int pre_shift, lgup, post_shift;
3464 HOST_WIDE_INT d = INTVAL (op1);
3465
3466 if (d > 0)
3467 {
3468 /* We could just as easily deal with negative constants here,
3469 but it does not seem worth the trouble for GCC 2.6. */
3470 if (EXACT_POWER_OF_2_OR_ZERO_P (d))
3471 {
3472 pre_shift = floor_log2 (d);
3473 if (rem_flag)
3474 {
3475 remainder = expand_binop (compute_mode, and_optab, op0,
3476 GEN_INT (((HOST_WIDE_INT) 1 << pre_shift) - 1),
3477 remainder, 0, OPTAB_LIB_WIDEN);
3478 if (remainder)
c8dbc8ca 3479 return gen_lowpart (mode, remainder);
55c2d311
TG
3480 }
3481 quotient = expand_shift (RSHIFT_EXPR, compute_mode, op0,
3482 build_int_2 (pre_shift, 0),
3483 tquotient, 0);
3484 }
3485 else
3486 {
3487 rtx t1, t2, t3, t4;
3488
3489 mh = choose_multiplier (d, size, size - 1,
3490 &ml, &post_shift, &lgup);
3491 if (mh)
3492 abort ();
3493
02a65aef
R
3494 if (post_shift < BITS_PER_WORD
3495 && size - 1 < BITS_PER_WORD)
55c2d311 3496 {
02a65aef
R
3497 t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
3498 build_int_2 (size - 1, 0),
3499 NULL_RTX, 0);
3500 t2 = expand_binop (compute_mode, xor_optab, op0, t1,
3501 NULL_RTX, 0, OPTAB_WIDEN);
3502 extra_cost = (shift_cost[post_shift]
3503 + shift_cost[size - 1] + 2 * add_cost);
3504 t3 = expand_mult_highpart (compute_mode, t2, ml,
3505 NULL_RTX, 1,
3506 max_cost - extra_cost);
3507 if (t3 != 0)
3508 {
3509 t4 = expand_shift (RSHIFT_EXPR, compute_mode, t3,
3510 build_int_2 (post_shift, 0),
3511 NULL_RTX, 1);
3512 quotient = expand_binop (compute_mode, xor_optab,
3513 t4, t1, tquotient, 0,
3514 OPTAB_WIDEN);
3515 }
55c2d311
TG
3516 }
3517 }
3518 }
3519 else
3520 {
3521 rtx nsign, t1, t2, t3, t4;
38a448ca
RH
3522 t1 = force_operand (gen_rtx_PLUS (compute_mode,
3523 op0, constm1_rtx), NULL_RTX);
55c2d311
TG
3524 t2 = expand_binop (compute_mode, ior_optab, op0, t1, NULL_RTX,
3525 0, OPTAB_WIDEN);
3526 nsign = expand_shift (RSHIFT_EXPR, compute_mode, t2,
3527 build_int_2 (size - 1, 0), NULL_RTX, 0);
38a448ca 3528 t3 = force_operand (gen_rtx_MINUS (compute_mode, t1, nsign),
55c2d311
TG
3529 NULL_RTX);
3530 t4 = expand_divmod (0, TRUNC_DIV_EXPR, compute_mode, t3, op1,
3531 NULL_RTX, 0);
3532 if (t4)
3533 {
3534 rtx t5;
3535 t5 = expand_unop (compute_mode, one_cmpl_optab, nsign,
3536 NULL_RTX, 0);
38a448ca
RH
3537 quotient = force_operand (gen_rtx_PLUS (compute_mode,
3538 t4, t5),
55c2d311
TG
3539 tquotient);
3540 }
3541 }
3542 }
3543
3544 if (quotient != 0)
3545 break;
3546 delete_insns_since (last);
3547
3548 /* Try using an instruction that produces both the quotient and
3549 remainder, using truncation. We can easily compensate the quotient
3550 or remainder to get floor rounding, once we have the remainder.
3551 Notice that we compute also the final remainder value here,
3552 and return the result right away. */
a45cf58c 3553 if (target == 0 || GET_MODE (target) != compute_mode)
55c2d311 3554 target = gen_reg_rtx (compute_mode);
668443c9 3555
55c2d311
TG
3556 if (rem_flag)
3557 {
668443c9
RK
3558 remainder
3559 = GET_CODE (target) == REG ? target : gen_reg_rtx (compute_mode);
55c2d311
TG
3560 quotient = gen_reg_rtx (compute_mode);
3561 }
3562 else
3563 {
668443c9
RK
3564 quotient
3565 = GET_CODE (target) == REG ? target : gen_reg_rtx (compute_mode);
55c2d311
TG
3566 remainder = gen_reg_rtx (compute_mode);
3567 }
3568
3569 if (expand_twoval_binop (sdivmod_optab, op0, op1,
3570 quotient, remainder, 0))
3571 {
3572 /* This could be computed with a branch-less sequence.
3573 Save that for later. */
3574 rtx tem;
3575 rtx label = gen_label_rtx ();
f5963e61 3576 do_cmp_and_jump (remainder, const0_rtx, EQ, compute_mode, label);
55c2d311
TG
3577 tem = expand_binop (compute_mode, xor_optab, op0, op1,
3578 NULL_RTX, 0, OPTAB_WIDEN);
f5963e61 3579 do_cmp_and_jump (tem, const0_rtx, GE, compute_mode, label);
55c2d311
TG
3580 expand_dec (quotient, const1_rtx);
3581 expand_inc (remainder, op1);
3582 emit_label (label);
c8dbc8ca 3583 return gen_lowpart (mode, rem_flag ? remainder : quotient);
55c2d311
TG
3584 }
3585
3586 /* No luck with division elimination or divmod. Have to do it
3587 by conditionally adjusting op0 *and* the result. */
44037a66 3588 {
55c2d311
TG
3589 rtx label1, label2, label3, label4, label5;
3590 rtx adjusted_op0;
3591 rtx tem;
3592
3593 quotient = gen_reg_rtx (compute_mode);
3594 adjusted_op0 = copy_to_mode_reg (compute_mode, op0);
3595 label1 = gen_label_rtx ();
3596 label2 = gen_label_rtx ();
3597 label3 = gen_label_rtx ();
3598 label4 = gen_label_rtx ();
3599 label5 = gen_label_rtx ();
f5963e61
JL
3600 do_cmp_and_jump (op1, const0_rtx, LT, compute_mode, label2);
3601 do_cmp_and_jump (adjusted_op0, const0_rtx, LT, compute_mode, label1);
55c2d311
TG
3602 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
3603 quotient, 0, OPTAB_LIB_WIDEN);
3604 if (tem != quotient)
3605 emit_move_insn (quotient, tem);
3606 emit_jump_insn (gen_jump (label5));
3607 emit_barrier ();
3608 emit_label (label1);
44037a66 3609 expand_inc (adjusted_op0, const1_rtx);
55c2d311
TG
3610 emit_jump_insn (gen_jump (label4));
3611 emit_barrier ();
3612 emit_label (label2);
f5963e61 3613 do_cmp_and_jump (adjusted_op0, const0_rtx, GT, compute_mode, label3);
55c2d311
TG
3614 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
3615 quotient, 0, OPTAB_LIB_WIDEN);
3616 if (tem != quotient)
3617 emit_move_insn (quotient, tem);
3618 emit_jump_insn (gen_jump (label5));
3619 emit_barrier ();
3620 emit_label (label3);
3621 expand_dec (adjusted_op0, const1_rtx);
3622 emit_label (label4);
3623 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
3624 quotient, 0, OPTAB_LIB_WIDEN);
3625 if (tem != quotient)
3626 emit_move_insn (quotient, tem);
3627 expand_dec (quotient, const1_rtx);
3628 emit_label (label5);
44037a66 3629 }
55c2d311 3630 break;
44037a66 3631
55c2d311
TG
3632 case CEIL_DIV_EXPR:
3633 case CEIL_MOD_EXPR:
3634 if (unsignedp)
3635 {
9176af2f
TG
3636 if (op1_is_constant && EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1)))
3637 {
3638 rtx t1, t2, t3;
3639 unsigned HOST_WIDE_INT d = INTVAL (op1);
3640 t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
3641 build_int_2 (floor_log2 (d), 0),
412381d9 3642 tquotient, 1);
9176af2f
TG
3643 t2 = expand_binop (compute_mode, and_optab, op0,
3644 GEN_INT (d - 1),
3645 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3646 t3 = gen_reg_rtx (compute_mode);
3647 t3 = emit_store_flag (t3, NE, t2, const0_rtx,
3648 compute_mode, 1, 1);
412381d9
TG
3649 if (t3 == 0)
3650 {
3651 rtx lab;
3652 lab = gen_label_rtx ();
f5963e61 3653 do_cmp_and_jump (t2, const0_rtx, EQ, compute_mode, lab);
412381d9
TG
3654 expand_inc (t1, const1_rtx);
3655 emit_label (lab);
3656 quotient = t1;
3657 }
3658 else
38a448ca
RH
3659 quotient = force_operand (gen_rtx_PLUS (compute_mode,
3660 t1, t3),
412381d9 3661 tquotient);
9176af2f
TG
3662 break;
3663 }
55c2d311
TG
3664
3665 /* Try using an instruction that produces both the quotient and
3666 remainder, using truncation. We can easily compensate the
3667 quotient or remainder to get ceiling rounding, once we have the
3668 remainder. Notice that we compute also the final remainder
3669 value here, and return the result right away. */
a45cf58c 3670 if (target == 0 || GET_MODE (target) != compute_mode)
55c2d311 3671 target = gen_reg_rtx (compute_mode);
668443c9 3672
55c2d311
TG
3673 if (rem_flag)
3674 {
668443c9
RK
3675 remainder = (GET_CODE (target) == REG
3676 ? target : gen_reg_rtx (compute_mode));
55c2d311
TG
3677 quotient = gen_reg_rtx (compute_mode);
3678 }
3679 else
3680 {
668443c9
RK
3681 quotient = (GET_CODE (target) == REG
3682 ? target : gen_reg_rtx (compute_mode));
55c2d311
TG
3683 remainder = gen_reg_rtx (compute_mode);
3684 }
3685
3686 if (expand_twoval_binop (udivmod_optab, op0, op1, quotient,
3687 remainder, 1))
3688 {
3689 /* This could be computed with a branch-less sequence.
3690 Save that for later. */
3691 rtx label = gen_label_rtx ();
f5963e61
JL
3692 do_cmp_and_jump (remainder, const0_rtx, EQ,
3693 compute_mode, label);
55c2d311
TG
3694 expand_inc (quotient, const1_rtx);
3695 expand_dec (remainder, op1);
3696 emit_label (label);
c8dbc8ca 3697 return gen_lowpart (mode, rem_flag ? remainder : quotient);
55c2d311
TG
3698 }
3699
3700 /* No luck with division elimination or divmod. Have to do it
3701 by conditionally adjusting op0 *and* the result. */
44037a66 3702 {
55c2d311
TG
3703 rtx label1, label2;
3704 rtx adjusted_op0, tem;
3705
3706 quotient = gen_reg_rtx (compute_mode);
3707 adjusted_op0 = copy_to_mode_reg (compute_mode, op0);
3708 label1 = gen_label_rtx ();
3709 label2 = gen_label_rtx ();
f5963e61
JL
3710 do_cmp_and_jump (adjusted_op0, const0_rtx, NE,
3711 compute_mode, label1);
55c2d311
TG
3712 emit_move_insn (quotient, const0_rtx);
3713 emit_jump_insn (gen_jump (label2));
3714 emit_barrier ();
3715 emit_label (label1);
3716 expand_dec (adjusted_op0, const1_rtx);
3717 tem = expand_binop (compute_mode, udiv_optab, adjusted_op0, op1,
3718 quotient, 1, OPTAB_LIB_WIDEN);
3719 if (tem != quotient)
3720 emit_move_insn (quotient, tem);
3721 expand_inc (quotient, const1_rtx);
3722 emit_label (label2);
44037a66 3723 }
55c2d311
TG
3724 }
3725 else /* signed */
3726 {
73f27728
RK
3727 if (op1_is_constant && EXACT_POWER_OF_2_OR_ZERO_P (INTVAL (op1))
3728 && INTVAL (op1) >= 0)
3729 {
3730 /* This is extremely similar to the code for the unsigned case
3731 above. For 2.7 we should merge these variants, but for
3732 2.6.1 I don't want to touch the code for unsigned since that
3733 get used in C. The signed case will only be used by other
3734 languages (Ada). */
3735
3736 rtx t1, t2, t3;
3737 unsigned HOST_WIDE_INT d = INTVAL (op1);
3738 t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
3739 build_int_2 (floor_log2 (d), 0),
3740 tquotient, 0);
3741 t2 = expand_binop (compute_mode, and_optab, op0,
3742 GEN_INT (d - 1),
3743 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3744 t3 = gen_reg_rtx (compute_mode);
3745 t3 = emit_store_flag (t3, NE, t2, const0_rtx,
3746 compute_mode, 1, 1);
3747 if (t3 == 0)
3748 {
3749 rtx lab;
3750 lab = gen_label_rtx ();
f5963e61 3751 do_cmp_and_jump (t2, const0_rtx, EQ, compute_mode, lab);
73f27728
RK
3752 expand_inc (t1, const1_rtx);
3753 emit_label (lab);
3754 quotient = t1;
3755 }
3756 else
38a448ca
RH
3757 quotient = force_operand (gen_rtx_PLUS (compute_mode,
3758 t1, t3),
73f27728
RK
3759 tquotient);
3760 break;
3761 }
3762
55c2d311
TG
3763 /* Try using an instruction that produces both the quotient and
3764 remainder, using truncation. We can easily compensate the
3765 quotient or remainder to get ceiling rounding, once we have the
3766 remainder. Notice that we compute also the final remainder
3767 value here, and return the result right away. */
a45cf58c 3768 if (target == 0 || GET_MODE (target) != compute_mode)
55c2d311
TG
3769 target = gen_reg_rtx (compute_mode);
3770 if (rem_flag)
3771 {
668443c9
RK
3772 remainder= (GET_CODE (target) == REG
3773 ? target : gen_reg_rtx (compute_mode));
55c2d311
TG
3774 quotient = gen_reg_rtx (compute_mode);
3775 }
3776 else
3777 {
668443c9
RK
3778 quotient = (GET_CODE (target) == REG
3779 ? target : gen_reg_rtx (compute_mode));
55c2d311
TG
3780 remainder = gen_reg_rtx (compute_mode);
3781 }
3782
3783 if (expand_twoval_binop (sdivmod_optab, op0, op1, quotient,
3784 remainder, 0))
3785 {
3786 /* This could be computed with a branch-less sequence.
3787 Save that for later. */
3788 rtx tem;
3789 rtx label = gen_label_rtx ();
f5963e61
JL
3790 do_cmp_and_jump (remainder, const0_rtx, EQ,
3791 compute_mode, label);
55c2d311
TG
3792 tem = expand_binop (compute_mode, xor_optab, op0, op1,
3793 NULL_RTX, 0, OPTAB_WIDEN);
f5963e61 3794 do_cmp_and_jump (tem, const0_rtx, LT, compute_mode, label);
55c2d311
TG
3795 expand_inc (quotient, const1_rtx);
3796 expand_dec (remainder, op1);
3797 emit_label (label);
c8dbc8ca 3798 return gen_lowpart (mode, rem_flag ? remainder : quotient);
55c2d311
TG
3799 }
3800
3801 /* No luck with division elimination or divmod. Have to do it
3802 by conditionally adjusting op0 *and* the result. */
44037a66 3803 {
55c2d311
TG
3804 rtx label1, label2, label3, label4, label5;
3805 rtx adjusted_op0;
3806 rtx tem;
3807
3808 quotient = gen_reg_rtx (compute_mode);
3809 adjusted_op0 = copy_to_mode_reg (compute_mode, op0);
3810 label1 = gen_label_rtx ();
3811 label2 = gen_label_rtx ();
3812 label3 = gen_label_rtx ();
3813 label4 = gen_label_rtx ();
3814 label5 = gen_label_rtx ();
f5963e61
JL
3815 do_cmp_and_jump (op1, const0_rtx, LT, compute_mode, label2);
3816 do_cmp_and_jump (adjusted_op0, const0_rtx, GT,
3817 compute_mode, label1);
55c2d311
TG
3818 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
3819 quotient, 0, OPTAB_LIB_WIDEN);
3820 if (tem != quotient)
3821 emit_move_insn (quotient, tem);
3822 emit_jump_insn (gen_jump (label5));
3823 emit_barrier ();
3824 emit_label (label1);
3825 expand_dec (adjusted_op0, const1_rtx);
3826 emit_jump_insn (gen_jump (label4));
3827 emit_barrier ();
3828 emit_label (label2);
f5963e61
JL
3829 do_cmp_and_jump (adjusted_op0, const0_rtx, LT,
3830 compute_mode, label3);
55c2d311
TG
3831 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
3832 quotient, 0, OPTAB_LIB_WIDEN);
3833 if (tem != quotient)
3834 emit_move_insn (quotient, tem);
3835 emit_jump_insn (gen_jump (label5));
3836 emit_barrier ();
3837 emit_label (label3);
3838 expand_inc (adjusted_op0, const1_rtx);
3839 emit_label (label4);
3840 tem = expand_binop (compute_mode, sdiv_optab, adjusted_op0, op1,
3841 quotient, 0, OPTAB_LIB_WIDEN);
3842 if (tem != quotient)
3843 emit_move_insn (quotient, tem);
3844 expand_inc (quotient, const1_rtx);
3845 emit_label (label5);
44037a66 3846 }
55c2d311
TG
3847 }
3848 break;
bc1c7e93 3849
55c2d311
TG
3850 case EXACT_DIV_EXPR:
3851 if (op1_is_constant && HOST_BITS_PER_WIDE_INT >= size)
3852 {
3853 HOST_WIDE_INT d = INTVAL (op1);
3854 unsigned HOST_WIDE_INT ml;
91ce572a 3855 int pre_shift;
55c2d311
TG
3856 rtx t1;
3857
91ce572a
CC
3858 pre_shift = floor_log2 (d & -d);
3859 ml = invert_mod2n (d >> pre_shift, size);
3860 t1 = expand_shift (RSHIFT_EXPR, compute_mode, op0,
3861 build_int_2 (pre_shift, 0), NULL_RTX, unsignedp);
69107307 3862 quotient = expand_mult (compute_mode, t1,
2496c7bd 3863 gen_int_mode (ml, compute_mode),
69107307 3864 NULL_RTX, 0);
55c2d311
TG
3865
3866 insn = get_last_insn ();
7e5bda2c
AM
3867 set_unique_reg_note (insn,
3868 REG_EQUAL,
3869 gen_rtx_fmt_ee (unsignedp ? UDIV : DIV,
3870 compute_mode,
3871 op0, op1));
55c2d311
TG
3872 }
3873 break;
3874
3875 case ROUND_DIV_EXPR:
3876 case ROUND_MOD_EXPR:
69f61901
RK
3877 if (unsignedp)
3878 {
3879 rtx tem;
3880 rtx label;
3881 label = gen_label_rtx ();
3882 quotient = gen_reg_rtx (compute_mode);
3883 remainder = gen_reg_rtx (compute_mode);
3884 if (expand_twoval_binop (udivmod_optab, op0, op1, quotient, remainder, 1) == 0)
3885 {
3886 rtx tem;
3887 quotient = expand_binop (compute_mode, udiv_optab, op0, op1,
3888 quotient, 1, OPTAB_LIB_WIDEN);
3889 tem = expand_mult (compute_mode, quotient, op1, NULL_RTX, 1);
3890 remainder = expand_binop (compute_mode, sub_optab, op0, tem,
3891 remainder, 1, OPTAB_LIB_WIDEN);
3892 }
3893 tem = plus_constant (op1, -1);
3894 tem = expand_shift (RSHIFT_EXPR, compute_mode, tem,
3895 build_int_2 (1, 0), NULL_RTX, 1);
f5963e61 3896 do_cmp_and_jump (remainder, tem, LEU, compute_mode, label);
69f61901
RK
3897 expand_inc (quotient, const1_rtx);
3898 expand_dec (remainder, op1);
3899 emit_label (label);
3900 }
3901 else
3902 {
3903 rtx abs_rem, abs_op1, tem, mask;
3904 rtx label;
3905 label = gen_label_rtx ();
3906 quotient = gen_reg_rtx (compute_mode);
3907 remainder = gen_reg_rtx (compute_mode);
3908 if (expand_twoval_binop (sdivmod_optab, op0, op1, quotient, remainder, 0) == 0)
3909 {
3910 rtx tem;
3911 quotient = expand_binop (compute_mode, sdiv_optab, op0, op1,
3912 quotient, 0, OPTAB_LIB_WIDEN);
3913 tem = expand_mult (compute_mode, quotient, op1, NULL_RTX, 0);
3914 remainder = expand_binop (compute_mode, sub_optab, op0, tem,
3915 remainder, 0, OPTAB_LIB_WIDEN);
3916 }
91ce572a
CC
3917 abs_rem = expand_abs (compute_mode, remainder, NULL_RTX, 1, 0);
3918 abs_op1 = expand_abs (compute_mode, op1, NULL_RTX, 1, 0);
69f61901
RK
3919 tem = expand_shift (LSHIFT_EXPR, compute_mode, abs_rem,
3920 build_int_2 (1, 0), NULL_RTX, 1);
f5963e61 3921 do_cmp_and_jump (tem, abs_op1, LTU, compute_mode, label);
69f61901
RK
3922 tem = expand_binop (compute_mode, xor_optab, op0, op1,
3923 NULL_RTX, 0, OPTAB_WIDEN);
3924 mask = expand_shift (RSHIFT_EXPR, compute_mode, tem,
3925 build_int_2 (size - 1, 0), NULL_RTX, 0);
3926 tem = expand_binop (compute_mode, xor_optab, mask, const1_rtx,
3927 NULL_RTX, 0, OPTAB_WIDEN);
3928 tem = expand_binop (compute_mode, sub_optab, tem, mask,
3929 NULL_RTX, 0, OPTAB_WIDEN);
3930 expand_inc (quotient, tem);
3931 tem = expand_binop (compute_mode, xor_optab, mask, op1,
3932 NULL_RTX, 0, OPTAB_WIDEN);
3933 tem = expand_binop (compute_mode, sub_optab, tem, mask,
3934 NULL_RTX, 0, OPTAB_WIDEN);
3935 expand_dec (remainder, tem);
3936 emit_label (label);
3937 }
3938 return gen_lowpart (mode, rem_flag ? remainder : quotient);
c410d49e 3939
e9a25f70
JL
3940 default:
3941 abort ();
55c2d311 3942 }
44037a66 3943
55c2d311 3944 if (quotient == 0)
44037a66 3945 {
a45cf58c
RK
3946 if (target && GET_MODE (target) != compute_mode)
3947 target = 0;
3948
55c2d311 3949 if (rem_flag)
44037a66 3950 {
32fdf36b 3951 /* Try to produce the remainder without producing the quotient.
d6a7951f 3952 If we seem to have a divmod pattern that does not require widening,
b20b352b 3953 don't try widening here. We should really have a WIDEN argument
32fdf36b
TG
3954 to expand_twoval_binop, since what we'd really like to do here is
3955 1) try a mod insn in compute_mode
3956 2) try a divmod insn in compute_mode
3957 3) try a div insn in compute_mode and multiply-subtract to get
3958 remainder
3959 4) try the same things with widening allowed. */
3960 remainder
3961 = sign_expand_binop (compute_mode, umod_optab, smod_optab,
3962 op0, op1, target,
3963 unsignedp,
3964 ((optab2->handlers[(int) compute_mode].insn_code
3965 != CODE_FOR_nothing)
3966 ? OPTAB_DIRECT : OPTAB_WIDEN));
55c2d311 3967 if (remainder == 0)
44037a66
TG
3968 {
3969 /* No luck there. Can we do remainder and divide at once
3970 without a library call? */
55c2d311
TG
3971 remainder = gen_reg_rtx (compute_mode);
3972 if (! expand_twoval_binop ((unsignedp
3973 ? udivmod_optab
3974 : sdivmod_optab),
3975 op0, op1,
3976 NULL_RTX, remainder, unsignedp))
3977 remainder = 0;
44037a66 3978 }
55c2d311
TG
3979
3980 if (remainder)
3981 return gen_lowpart (mode, remainder);
44037a66 3982 }
44037a66 3983
dc38b292
RK
3984 /* Produce the quotient. Try a quotient insn, but not a library call.
3985 If we have a divmod in this mode, use it in preference to widening
3986 the div (for this test we assume it will not fail). Note that optab2
3987 is set to the one of the two optabs that the call below will use. */
3988 quotient
3989 = sign_expand_binop (compute_mode, udiv_optab, sdiv_optab,
3990 op0, op1, rem_flag ? NULL_RTX : target,
3991 unsignedp,
3992 ((optab2->handlers[(int) compute_mode].insn_code
3993 != CODE_FOR_nothing)
3994 ? OPTAB_DIRECT : OPTAB_WIDEN));
3995
55c2d311 3996 if (quotient == 0)
44037a66
TG
3997 {
3998 /* No luck there. Try a quotient-and-remainder insn,
3999 keeping the quotient alone. */
55c2d311 4000 quotient = gen_reg_rtx (compute_mode);
44037a66 4001 if (! expand_twoval_binop (unsignedp ? udivmod_optab : sdivmod_optab,
55c2d311
TG
4002 op0, op1,
4003 quotient, NULL_RTX, unsignedp))
4004 {
4005 quotient = 0;
4006 if (! rem_flag)
4007 /* Still no luck. If we are not computing the remainder,
4008 use a library call for the quotient. */
4009 quotient = sign_expand_binop (compute_mode,
4010 udiv_optab, sdiv_optab,
4011 op0, op1, target,
4012 unsignedp, OPTAB_LIB_WIDEN);
4013 }
44037a66 4014 }
44037a66
TG
4015 }
4016
44037a66
TG
4017 if (rem_flag)
4018 {
a45cf58c
RK
4019 if (target && GET_MODE (target) != compute_mode)
4020 target = 0;
4021
55c2d311 4022 if (quotient == 0)
44037a66 4023 /* No divide instruction either. Use library for remainder. */
55c2d311
TG
4024 remainder = sign_expand_binop (compute_mode, umod_optab, smod_optab,
4025 op0, op1, target,
4026 unsignedp, OPTAB_LIB_WIDEN);
44037a66
TG
4027 else
4028 {
4029 /* We divided. Now finish doing X - Y * (X / Y). */
55c2d311
TG
4030 remainder = expand_mult (compute_mode, quotient, op1,
4031 NULL_RTX, unsignedp);
4032 remainder = expand_binop (compute_mode, sub_optab, op0,
4033 remainder, target, unsignedp,
4034 OPTAB_LIB_WIDEN);
44037a66
TG
4035 }
4036 }
4037
55c2d311 4038 return gen_lowpart (mode, rem_flag ? remainder : quotient);
44037a66
TG
4039}
4040\f
4041/* Return a tree node with data type TYPE, describing the value of X.
4042 Usually this is an RTL_EXPR, if there is no obvious better choice.
4043 X may be an expression, however we only support those expressions
6d2f8887 4044 generated by loop.c. */
44037a66
TG
4045
4046tree
4047make_tree (type, x)
4048 tree type;
4049 rtx x;
4050{
4051 tree t;
4052
4053 switch (GET_CODE (x))
4054 {
4055 case CONST_INT:
4056 t = build_int_2 (INTVAL (x),
172f4957
DM
4057 (TREE_UNSIGNED (type)
4058 && (GET_MODE_BITSIZE (TYPE_MODE (type)) < HOST_BITS_PER_WIDE_INT))
4059 || INTVAL (x) >= 0 ? 0 : -1);
44037a66
TG
4060 TREE_TYPE (t) = type;
4061 return t;
4062
4063 case CONST_DOUBLE:
4064 if (GET_MODE (x) == VOIDmode)
4065 {
4066 t = build_int_2 (CONST_DOUBLE_LOW (x), CONST_DOUBLE_HIGH (x));
4067 TREE_TYPE (t) = type;
4068 }
4069 else
4070 {
4071 REAL_VALUE_TYPE d;
4072
4073 REAL_VALUE_FROM_CONST_DOUBLE (d, x);
4074 t = build_real (type, d);
4075 }
4076
4077 return t;
69ef87e2
AH
4078
4079 case CONST_VECTOR:
4080 {
4081 int i, units;
4082 rtx elt;
4083 tree t = NULL_TREE;
4084
4085 units = CONST_VECTOR_NUNITS (x);
4086
4087 /* Build a tree with vector elements. */
4088 for (i = units - 1; i >= 0; --i)
4089 {
4090 elt = CONST_VECTOR_ELT (x, i);
4091 t = tree_cons (NULL_TREE, make_tree (type, elt), t);
4092 }
c410d49e 4093
69ef87e2
AH
4094 return build_vector (type, t);
4095 }
4096
44037a66
TG
4097 case PLUS:
4098 return fold (build (PLUS_EXPR, type, make_tree (type, XEXP (x, 0)),
4099 make_tree (type, XEXP (x, 1))));
c410d49e 4100
44037a66
TG
4101 case MINUS:
4102 return fold (build (MINUS_EXPR, type, make_tree (type, XEXP (x, 0)),
4103 make_tree (type, XEXP (x, 1))));
c410d49e 4104
44037a66
TG
4105 case NEG:
4106 return fold (build1 (NEGATE_EXPR, type, make_tree (type, XEXP (x, 0))));
4107
4108 case MULT:
4109 return fold (build (MULT_EXPR, type, make_tree (type, XEXP (x, 0)),
4110 make_tree (type, XEXP (x, 1))));
c410d49e 4111
44037a66
TG
4112 case ASHIFT:
4113 return fold (build (LSHIFT_EXPR, type, make_tree (type, XEXP (x, 0)),
4114 make_tree (type, XEXP (x, 1))));
c410d49e 4115
44037a66 4116 case LSHIFTRT:
ceef8ce4 4117 t = (*lang_hooks.types.unsigned_type) (type);
44037a66 4118 return fold (convert (type,
ceef8ce4
NB
4119 build (RSHIFT_EXPR, t,
4120 make_tree (t, XEXP (x, 0)),
44037a66 4121 make_tree (type, XEXP (x, 1)))));
c410d49e 4122
44037a66 4123 case ASHIFTRT:
ceef8ce4 4124 t = (*lang_hooks.types.signed_type) (type);
44037a66 4125 return fold (convert (type,
ceef8ce4
NB
4126 build (RSHIFT_EXPR, t,
4127 make_tree (t, XEXP (x, 0)),
44037a66 4128 make_tree (type, XEXP (x, 1)))));
c410d49e 4129
44037a66
TG
4130 case DIV:
4131 if (TREE_CODE (type) != REAL_TYPE)
ceef8ce4 4132 t = (*lang_hooks.types.signed_type) (type);
44037a66
TG
4133 else
4134 t = type;
4135
4136 return fold (convert (type,
4137 build (TRUNC_DIV_EXPR, t,
4138 make_tree (t, XEXP (x, 0)),
4139 make_tree (t, XEXP (x, 1)))));
4140 case UDIV:
ceef8ce4 4141 t = (*lang_hooks.types.unsigned_type) (type);
44037a66
TG
4142 return fold (convert (type,
4143 build (TRUNC_DIV_EXPR, t,
4144 make_tree (t, XEXP (x, 0)),
4145 make_tree (t, XEXP (x, 1)))));
5c45425b
RH
4146
4147 case SIGN_EXTEND:
4148 case ZERO_EXTEND:
4149 t = (*lang_hooks.types.type_for_mode) (GET_MODE (XEXP (x, 0)),
4150 GET_CODE (x) == ZERO_EXTEND);
4151 return fold (convert (type, make_tree (t, XEXP (x, 0))));
4152
44037a66
TG
4153 default:
4154 t = make_node (RTL_EXPR);
4155 TREE_TYPE (t) = type;
d1608933
RK
4156
4157#ifdef POINTERS_EXTEND_UNSIGNED
4158 /* If TYPE is a POINTER_TYPE, X might be Pmode with TYPE_MODE being
4159 ptr_mode. So convert. */
4160 if (POINTER_TYPE_P (type) && GET_MODE (x) != TYPE_MODE (type))
4161 x = convert_memory_address (TYPE_MODE (type), x);
4162#endif
4163
44037a66
TG
4164 RTL_EXPR_RTL (t) = x;
4165 /* There are no insns to be output
4166 when this rtl_expr is used. */
4167 RTL_EXPR_SEQUENCE (t) = 0;
4168 return t;
4169 }
4170}
4171
ce60bf25
EB
4172/* Check whether the multiplication X * MULT + ADD overflows.
4173 X, MULT and ADD must be CONST_*.
4174 MODE is the machine mode for the computation.
4175 X and MULT must have mode MODE. ADD may have a different mode.
4176 So can X (defaults to same as MODE).
cc2902df 4177 UNSIGNEDP is nonzero to do unsigned multiplication. */
ce60bf25
EB
4178
4179bool
4180const_mult_add_overflow_p (x, mult, add, mode, unsignedp)
4181 rtx x, mult, add;
4182 enum machine_mode mode;
4183 int unsignedp;
4184{
4185 tree type, mult_type, add_type, result;
4186
4187 type = (*lang_hooks.types.type_for_mode) (mode, unsignedp);
4188
4189 /* In order to get a proper overflow indication from an unsigned
4190 type, we have to pretend that it's a sizetype. */
4191 mult_type = type;
4192 if (unsignedp)
4193 {
4194 mult_type = copy_node (type);
4195 TYPE_IS_SIZETYPE (mult_type) = 1;
4196 }
4197
4198 add_type = (GET_MODE (add) == VOIDmode ? mult_type
4199 : (*lang_hooks.types.type_for_mode) (GET_MODE (add), unsignedp));
4200
4201 result = fold (build (PLUS_EXPR, mult_type,
4202 fold (build (MULT_EXPR, mult_type,
4203 make_tree (mult_type, x),
4204 make_tree (mult_type, mult))),
4205 make_tree (add_type, add)));
4206
4207 return TREE_CONSTANT_OVERFLOW (result);
4208}
4209
44037a66
TG
4210/* Return an rtx representing the value of X * MULT + ADD.
4211 TARGET is a suggestion for where to store the result (an rtx).
4212 MODE is the machine mode for the computation.
4213 X and MULT must have mode MODE. ADD may have a different mode.
4214 So can X (defaults to same as MODE).
cc2902df 4215 UNSIGNEDP is nonzero to do unsigned multiplication.
44037a66
TG
4216 This may emit insns. */
4217
4218rtx
4219expand_mult_add (x, target, mult, add, mode, unsignedp)
4220 rtx x, target, mult, add;
4221 enum machine_mode mode;
4222 int unsignedp;
4223{
b0c48229 4224 tree type = (*lang_hooks.types.type_for_mode) (mode, unsignedp);
44037a66 4225 tree add_type = (GET_MODE (add) == VOIDmode
b0c48229
NB
4226 ? type: (*lang_hooks.types.type_for_mode) (GET_MODE (add),
4227 unsignedp));
44037a66
TG
4228 tree result = fold (build (PLUS_EXPR, type,
4229 fold (build (MULT_EXPR, type,
4230 make_tree (type, x),
4231 make_tree (type, mult))),
4232 make_tree (add_type, add)));
4233
4234 return expand_expr (result, target, VOIDmode, 0);
4235}
4236\f
4237/* Compute the logical-and of OP0 and OP1, storing it in TARGET
4238 and returning TARGET.
4239
4240 If TARGET is 0, a pseudo-register or constant is returned. */
4241
4242rtx
22273300
JJ
4243expand_and (mode, op0, op1, target)
4244 enum machine_mode mode;
44037a66
TG
4245 rtx op0, op1, target;
4246{
22273300 4247 rtx tem = 0;
44037a66 4248
22273300
JJ
4249 if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
4250 tem = simplify_binary_operation (AND, mode, op0, op1);
4251 if (tem == 0)
44037a66 4252 tem = expand_binop (mode, and_optab, op0, op1, target, 0, OPTAB_LIB_WIDEN);
44037a66
TG
4253
4254 if (target == 0)
4255 target = tem;
4256 else if (tem != target)
4257 emit_move_insn (target, tem);
4258 return target;
4259}
4260\f
4261/* Emit a store-flags instruction for comparison CODE on OP0 and OP1
4262 and storing in TARGET. Normally return TARGET.
4263 Return 0 if that cannot be done.
4264
4265 MODE is the mode to use for OP0 and OP1 should they be CONST_INTs. If
c410d49e 4266 it is VOIDmode, they cannot both be CONST_INT.
44037a66
TG
4267
4268 UNSIGNEDP is for the case where we have to widen the operands
4269 to perform the operation. It says to use zero-extension.
4270
4271 NORMALIZEP is 1 if we should convert the result to be either zero
373e7d69 4272 or one. Normalize is -1 if we should convert the result to be
44037a66
TG
4273 either zero or -1. If NORMALIZEP is zero, the result will be left
4274 "raw" out of the scc insn. */
4275
4276rtx
4277emit_store_flag (target, code, op0, op1, mode, unsignedp, normalizep)
4278 rtx target;
4279 enum rtx_code code;
4280 rtx op0, op1;
4281 enum machine_mode mode;
4282 int unsignedp;
4283 int normalizep;
4284{
4285 rtx subtarget;
4286 enum insn_code icode;
4287 enum machine_mode compare_mode;
4288 enum machine_mode target_mode = GET_MODE (target);
4289 rtx tem;
db2f8a07 4290 rtx last = get_last_insn ();
44037a66
TG
4291 rtx pattern, comparison;
4292
20d32cc2
ZW
4293 /* ??? Ok to do this and then fail? */
4294 op0 = protect_from_queue (op0, 0);
4295 op1 = protect_from_queue (op1, 0);
4296
b30f05db
BS
4297 if (unsignedp)
4298 code = unsigned_condition (code);
4299
c2615a67
RK
4300 /* If one operand is constant, make it the second one. Only do this
4301 if the other operand is not constant as well. */
4302
8c9864f3 4303 if (swap_commutative_operands_p (op0, op1))
c2615a67
RK
4304 {
4305 tem = op0;
4306 op0 = op1;
4307 op1 = tem;
4308 code = swap_condition (code);
4309 }
4310
6405e07b
DE
4311 if (mode == VOIDmode)
4312 mode = GET_MODE (op0);
4313
c410d49e 4314 /* For some comparisons with 1 and -1, we can convert this to
44037a66 4315 comparisons with zero. This will often produce more opportunities for
0f41302f 4316 store-flag insns. */
44037a66
TG
4317
4318 switch (code)
4319 {
4320 case LT:
4321 if (op1 == const1_rtx)
4322 op1 = const0_rtx, code = LE;
4323 break;
4324 case LE:
4325 if (op1 == constm1_rtx)
4326 op1 = const0_rtx, code = LT;
4327 break;
4328 case GE:
4329 if (op1 == const1_rtx)
4330 op1 = const0_rtx, code = GT;
4331 break;
4332 case GT:
4333 if (op1 == constm1_rtx)
4334 op1 = const0_rtx, code = GE;
4335 break;
4336 case GEU:
4337 if (op1 == const1_rtx)
4338 op1 = const0_rtx, code = NE;
4339 break;
4340 case LTU:
4341 if (op1 == const1_rtx)
4342 op1 = const0_rtx, code = EQ;
4343 break;
e9a25f70
JL
4344 default:
4345 break;
44037a66
TG
4346 }
4347
6912b84b
RK
4348 /* If we are comparing a double-word integer with zero, we can convert
4349 the comparison into one involving a single word. */
4350 if (GET_MODE_BITSIZE (mode) == BITS_PER_WORD * 2
4351 && GET_MODE_CLASS (mode) == MODE_INT
170c56da
JJ
4352 && op1 == const0_rtx
4353 && (GET_CODE (op0) != MEM || ! MEM_VOLATILE_P (op0)))
6912b84b
RK
4354 {
4355 if (code == EQ || code == NE)
4356 {
8433f113
RH
4357 rtx op00, op01, op0both;
4358
6912b84b 4359 /* Do a logical OR of the two words and compare the result. */
8433f113
RH
4360 op00 = simplify_gen_subreg (word_mode, op0, mode, 0);
4361 op01 = simplify_gen_subreg (word_mode, op0, mode, UNITS_PER_WORD);
4362 op0both = expand_binop (word_mode, ior_optab, op00, op01,
4363 NULL_RTX, unsignedp, OPTAB_DIRECT);
6912b84b
RK
4364 if (op0both != 0)
4365 return emit_store_flag (target, code, op0both, op1, word_mode,
4366 unsignedp, normalizep);
4367 }
4368 else if (code == LT || code == GE)
8433f113
RH
4369 {
4370 rtx op0h;
4371
4372 /* If testing the sign bit, can just test on high word. */
4373 op0h = simplify_gen_subreg (word_mode, op0, mode,
4374 subreg_highpart_offset (word_mode, mode));
4375 return emit_store_flag (target, code, op0h, op1, word_mode,
4376 unsignedp, normalizep);
4377 }
6912b84b
RK
4378 }
4379
44037a66
TG
4380 /* From now on, we won't change CODE, so set ICODE now. */
4381 icode = setcc_gen_code[(int) code];
4382
4383 /* If this is A < 0 or A >= 0, we can do this by taking the ones
4384 complement of A (for GE) and shifting the sign bit to the low bit. */
4385 if (op1 == const0_rtx && (code == LT || code == GE)
4386 && GET_MODE_CLASS (mode) == MODE_INT
4387 && (normalizep || STORE_FLAG_VALUE == 1
b1ec3c92 4388 || (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
12dd565f 4389 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
32214c32 4390 == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1)))))
44037a66 4391 {
8deb7047 4392 subtarget = target;
44037a66
TG
4393
4394 /* If the result is to be wider than OP0, it is best to convert it
4395 first. If it is to be narrower, it is *incorrect* to convert it
4396 first. */
4397 if (GET_MODE_SIZE (target_mode) > GET_MODE_SIZE (mode))
4398 {
b3d4e1b2 4399 op0 = protect_from_queue (op0, 0);
81722fa9 4400 op0 = convert_modes (target_mode, mode, op0, 0);
44037a66
TG
4401 mode = target_mode;
4402 }
4403
4404 if (target_mode != mode)
4405 subtarget = 0;
4406
4407 if (code == GE)
1d6eaf3d
RK
4408 op0 = expand_unop (mode, one_cmpl_optab, op0,
4409 ((STORE_FLAG_VALUE == 1 || normalizep)
4410 ? 0 : subtarget), 0);
44037a66 4411
1d6eaf3d 4412 if (STORE_FLAG_VALUE == 1 || normalizep)
44037a66
TG
4413 /* If we are supposed to produce a 0/1 value, we want to do
4414 a logical shift from the sign bit to the low-order bit; for
4415 a -1/0 value, we do an arithmetic shift. */
4416 op0 = expand_shift (RSHIFT_EXPR, mode, op0,
4417 size_int (GET_MODE_BITSIZE (mode) - 1),
4418 subtarget, normalizep != -1);
4419
4420 if (mode != target_mode)
c2ec26b8 4421 op0 = convert_modes (target_mode, mode, op0, 0);
44037a66
TG
4422
4423 return op0;
4424 }
4425
4426 if (icode != CODE_FOR_nothing)
4427 {
a995e389
RH
4428 insn_operand_predicate_fn pred;
4429
44037a66
TG
4430 /* We think we may be able to do this with a scc insn. Emit the
4431 comparison and then the scc insn.
4432
4433 compare_from_rtx may call emit_queue, which would be deleted below
3bdf5848
BS
4434 if the scc insn fails. So call it ourselves before setting LAST.
4435 Likewise for do_pending_stack_adjust. */
44037a66
TG
4436
4437 emit_queue ();
3bdf5848 4438 do_pending_stack_adjust ();
44037a66
TG
4439 last = get_last_insn ();
4440
b1ec3c92 4441 comparison
d43e0b7d 4442 = compare_from_rtx (op0, op1, code, unsignedp, mode, NULL_RTX);
44037a66
TG
4443 if (GET_CODE (comparison) == CONST_INT)
4444 return (comparison == const0_rtx ? const0_rtx
4445 : normalizep == 1 ? const1_rtx
4446 : normalizep == -1 ? constm1_rtx
4447 : const_true_rtx);
4448
8f08e8c0
JL
4449 /* The code of COMPARISON may not match CODE if compare_from_rtx
4450 decided to swap its operands and reverse the original code.
c2615a67 4451
8f08e8c0
JL
4452 We know that compare_from_rtx returns either a CONST_INT or
4453 a new comparison code, so it is safe to just extract the
4454 code from COMPARISON. */
4455 code = GET_CODE (comparison);
8deb7047 4456
44037a66 4457 /* Get a reference to the target in the proper mode for this insn. */
a995e389 4458 compare_mode = insn_data[(int) icode].operand[0].mode;
44037a66 4459 subtarget = target;
a995e389 4460 pred = insn_data[(int) icode].operand[0].predicate;
44037a66 4461 if (preserve_subexpressions_p ()
a995e389 4462 || ! (*pred) (subtarget, compare_mode))
44037a66
TG
4463 subtarget = gen_reg_rtx (compare_mode);
4464
4465 pattern = GEN_FCN (icode) (subtarget);
4466 if (pattern)
4467 {
4468 emit_insn (pattern);
4469
4470 /* If we are converting to a wider mode, first convert to
4471 TARGET_MODE, then normalize. This produces better combining
4472 opportunities on machines that have a SIGN_EXTRACT when we are
4473 testing a single bit. This mostly benefits the 68k.
4474
4475 If STORE_FLAG_VALUE does not have the sign bit set when
4476 interpreted in COMPARE_MODE, we can do this conversion as
4477 unsigned, which is usually more efficient. */
4478 if (GET_MODE_SIZE (target_mode) > GET_MODE_SIZE (compare_mode))
4479 {
4480 convert_move (target, subtarget,
4481 (GET_MODE_BITSIZE (compare_mode)
b1ec3c92 4482 <= HOST_BITS_PER_WIDE_INT)
44037a66 4483 && 0 == (STORE_FLAG_VALUE
b1ec3c92
CH
4484 & ((HOST_WIDE_INT) 1
4485 << (GET_MODE_BITSIZE (compare_mode) -1))));
44037a66
TG
4486 op0 = target;
4487 compare_mode = target_mode;
4488 }
4489 else
4490 op0 = subtarget;
4491
4b980e20
RK
4492 /* If we want to keep subexpressions around, don't reuse our
4493 last target. */
4494
4495 if (preserve_subexpressions_p ())
4496 subtarget = 0;
4497
44037a66
TG
4498 /* Now normalize to the proper value in COMPARE_MODE. Sometimes
4499 we don't have to do anything. */
4500 if (normalizep == 0 || normalizep == STORE_FLAG_VALUE)
4501 ;
c5c76735
JL
4502 /* STORE_FLAG_VALUE might be the most negative number, so write
4503 the comparison this way to avoid a compiler-time warning. */
4504 else if (- normalizep == STORE_FLAG_VALUE)
44037a66
TG
4505 op0 = expand_unop (compare_mode, neg_optab, op0, subtarget, 0);
4506
4507 /* We don't want to use STORE_FLAG_VALUE < 0 below since this
4508 makes it hard to use a value of just the sign bit due to
4509 ANSI integer constant typing rules. */
b1ec3c92 4510 else if (GET_MODE_BITSIZE (compare_mode) <= HOST_BITS_PER_WIDE_INT
44037a66 4511 && (STORE_FLAG_VALUE
b1ec3c92
CH
4512 & ((HOST_WIDE_INT) 1
4513 << (GET_MODE_BITSIZE (compare_mode) - 1))))
44037a66
TG
4514 op0 = expand_shift (RSHIFT_EXPR, compare_mode, op0,
4515 size_int (GET_MODE_BITSIZE (compare_mode) - 1),
4516 subtarget, normalizep == 1);
4517 else if (STORE_FLAG_VALUE & 1)
4518 {
22273300 4519 op0 = expand_and (compare_mode, op0, const1_rtx, subtarget);
44037a66
TG
4520 if (normalizep == -1)
4521 op0 = expand_unop (compare_mode, neg_optab, op0, op0, 0);
4522 }
4523 else
4524 abort ();
4525
c410d49e 4526 /* If we were converting to a smaller mode, do the
44037a66
TG
4527 conversion now. */
4528 if (target_mode != compare_mode)
4529 {
522ae84c 4530 convert_move (target, op0, 0);
44037a66
TG
4531 return target;
4532 }
4533 else
4534 return op0;
4535 }
4536 }
4537
db2f8a07 4538 delete_insns_since (last);
44037a66 4539
91e66235
MM
4540 /* If expensive optimizations, use different pseudo registers for each
4541 insn, instead of reusing the same pseudo. This leads to better CSE,
4542 but slows down the compiler, since there are more pseudos */
4543 subtarget = (!flag_expensive_optimizations
4544 && (target_mode == mode)) ? target : NULL_RTX;
44037a66
TG
4545
4546 /* If we reached here, we can't do this with a scc insn. However, there
4547 are some comparisons that can be done directly. For example, if
4548 this is an equality comparison of integers, we can try to exclusive-or
4549 (or subtract) the two operands and use a recursive call to try the
4550 comparison with zero. Don't do any of these cases if branches are
4551 very cheap. */
4552
c8c1bde3 4553 if (BRANCH_COST > 0
44037a66
TG
4554 && GET_MODE_CLASS (mode) == MODE_INT && (code == EQ || code == NE)
4555 && op1 != const0_rtx)
4556 {
4557 tem = expand_binop (mode, xor_optab, op0, op1, subtarget, 1,
4558 OPTAB_WIDEN);
4559
4560 if (tem == 0)
4561 tem = expand_binop (mode, sub_optab, op0, op1, subtarget, 1,
4562 OPTAB_WIDEN);
4563 if (tem != 0)
4564 tem = emit_store_flag (target, code, tem, const0_rtx,
4565 mode, unsignedp, normalizep);
4566 if (tem == 0)
4567 delete_insns_since (last);
4568 return tem;
4569 }
4570
c410d49e 4571 /* Some other cases we can do are EQ, NE, LE, and GT comparisons with
44037a66
TG
4572 the constant zero. Reject all other comparisons at this point. Only
4573 do LE and GT if branches are expensive since they are expensive on
4574 2-operand machines. */
4575
4576 if (BRANCH_COST == 0
4577 || GET_MODE_CLASS (mode) != MODE_INT || op1 != const0_rtx
4578 || (code != EQ && code != NE
4579 && (BRANCH_COST <= 1 || (code != LE && code != GT))))
4580 return 0;
4581
4582 /* See what we need to return. We can only return a 1, -1, or the
4583 sign bit. */
4584
4585 if (normalizep == 0)
4586 {
4587 if (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
4588 normalizep = STORE_FLAG_VALUE;
4589
b1ec3c92 4590 else if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
12dd565f 4591 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
c84e2712 4592 == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1)))
44037a66
TG
4593 ;
4594 else
4595 return 0;
4596 }
4597
4598 /* Try to put the result of the comparison in the sign bit. Assume we can't
4599 do the necessary operation below. */
4600
4601 tem = 0;
4602
4603 /* To see if A <= 0, compute (A | (A - 1)). A <= 0 iff that result has
4604 the sign bit set. */
4605
4606 if (code == LE)
4607 {
4608 /* This is destructive, so SUBTARGET can't be OP0. */
4609 if (rtx_equal_p (subtarget, op0))
4610 subtarget = 0;
4611
4612 tem = expand_binop (mode, sub_optab, op0, const1_rtx, subtarget, 0,
4613 OPTAB_WIDEN);
4614 if (tem)
4615 tem = expand_binop (mode, ior_optab, op0, tem, subtarget, 0,
4616 OPTAB_WIDEN);
4617 }
4618
4619 /* To see if A > 0, compute (((signed) A) << BITS) - A, where BITS is the
4620 number of bits in the mode of OP0, minus one. */
4621
4622 if (code == GT)
4623 {
4624 if (rtx_equal_p (subtarget, op0))
4625 subtarget = 0;
4626
4627 tem = expand_shift (RSHIFT_EXPR, mode, op0,
4628 size_int (GET_MODE_BITSIZE (mode) - 1),
4629 subtarget, 0);
4630 tem = expand_binop (mode, sub_optab, tem, op0, subtarget, 0,
4631 OPTAB_WIDEN);
4632 }
c410d49e 4633
44037a66
TG
4634 if (code == EQ || code == NE)
4635 {
4636 /* For EQ or NE, one way to do the comparison is to apply an operation
cc2902df 4637 that converts the operand into a positive number if it is nonzero
44037a66
TG
4638 or zero if it was originally zero. Then, for EQ, we subtract 1 and
4639 for NE we negate. This puts the result in the sign bit. Then we
c410d49e 4640 normalize with a shift, if needed.
44037a66
TG
4641
4642 Two operations that can do the above actions are ABS and FFS, so try
4643 them. If that doesn't work, and MODE is smaller than a full word,
36d747f6 4644 we can use zero-extension to the wider mode (an unsigned conversion)
44037a66
TG
4645 as the operation. */
4646
c410d49e
EC
4647 /* Note that ABS doesn't yield a positive number for INT_MIN, but
4648 that is compensated by the subsequent overflow when subtracting
30f7a378 4649 one / negating. */
91ce572a 4650
44037a66
TG
4651 if (abs_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
4652 tem = expand_unop (mode, abs_optab, op0, subtarget, 1);
4653 else if (ffs_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
4654 tem = expand_unop (mode, ffs_optab, op0, subtarget, 1);
4655 else if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
4656 {
b3d4e1b2 4657 op0 = protect_from_queue (op0, 0);
c2ec26b8 4658 tem = convert_modes (word_mode, mode, op0, 1);
81722fa9 4659 mode = word_mode;
44037a66
TG
4660 }
4661
4662 if (tem != 0)
4663 {
4664 if (code == EQ)
4665 tem = expand_binop (mode, sub_optab, tem, const1_rtx, subtarget,
4666 0, OPTAB_WIDEN);
4667 else
4668 tem = expand_unop (mode, neg_optab, tem, subtarget, 0);
4669 }
4670
4671 /* If we couldn't do it that way, for NE we can "or" the two's complement
4672 of the value with itself. For EQ, we take the one's complement of
4673 that "or", which is an extra insn, so we only handle EQ if branches
4674 are expensive. */
4675
4676 if (tem == 0 && (code == NE || BRANCH_COST > 1))
4677 {
36d747f6
RS
4678 if (rtx_equal_p (subtarget, op0))
4679 subtarget = 0;
4680
44037a66
TG
4681 tem = expand_unop (mode, neg_optab, op0, subtarget, 0);
4682 tem = expand_binop (mode, ior_optab, tem, op0, subtarget, 0,
4683 OPTAB_WIDEN);
4684
4685 if (tem && code == EQ)
4686 tem = expand_unop (mode, one_cmpl_optab, tem, subtarget, 0);
4687 }
4688 }
4689
4690 if (tem && normalizep)
4691 tem = expand_shift (RSHIFT_EXPR, mode, tem,
4692 size_int (GET_MODE_BITSIZE (mode) - 1),
91e66235 4693 subtarget, normalizep == 1);
44037a66 4694
91e66235 4695 if (tem)
44037a66 4696 {
91e66235
MM
4697 if (GET_MODE (tem) != target_mode)
4698 {
4699 convert_move (target, tem, 0);
4700 tem = target;
4701 }
4702 else if (!subtarget)
4703 {
4704 emit_move_insn (target, tem);
4705 tem = target;
4706 }
44037a66 4707 }
91e66235 4708 else
44037a66
TG
4709 delete_insns_since (last);
4710
4711 return tem;
4712}
04a8ee2f
TG
4713
4714/* Like emit_store_flag, but always succeeds. */
4715
4716rtx
4717emit_store_flag_force (target, code, op0, op1, mode, unsignedp, normalizep)
4718 rtx target;
4719 enum rtx_code code;
4720 rtx op0, op1;
4721 enum machine_mode mode;
4722 int unsignedp;
4723 int normalizep;
4724{
4725 rtx tem, label;
4726
4727 /* First see if emit_store_flag can do the job. */
4728 tem = emit_store_flag (target, code, op0, op1, mode, unsignedp, normalizep);
4729 if (tem != 0)
4730 return tem;
4731
4732 if (normalizep == 0)
4733 normalizep = 1;
4734
4735 /* If this failed, we have to do this with set/compare/jump/set code. */
4736
4737 if (GET_CODE (target) != REG
4738 || reg_mentioned_p (target, op0) || reg_mentioned_p (target, op1))
4739 target = gen_reg_rtx (GET_MODE (target));
4740
e4565aff 4741 emit_move_insn (target, const1_rtx);
04a8ee2f 4742 label = gen_label_rtx ();
d43e0b7d 4743 do_compare_rtx_and_jump (op0, op1, code, unsignedp, mode, NULL_RTX,
b30f05db 4744 NULL_RTX, label);
04a8ee2f 4745
e4565aff 4746 emit_move_insn (target, const0_rtx);
44037a66
TG
4747 emit_label (label);
4748
4749 return target;
4750}
f5963e61
JL
4751\f
4752/* Perform possibly multi-word comparison and conditional jump to LABEL
4753 if ARG1 OP ARG2 true where ARG1 and ARG2 are of mode MODE
4754
4755 The algorithm is based on the code in expr.c:do_jump.
4756
4757 Note that this does not perform a general comparison. Only variants
4758 generated within expmed.c are correctly handled, others abort (but could
4759 be handled if needed). */
4760
4761static void
4762do_cmp_and_jump (arg1, arg2, op, mode, label)
4763 rtx arg1, arg2, label;
b30f05db
BS
4764 enum rtx_code op;
4765 enum machine_mode mode;
f5963e61
JL
4766{
4767 /* If this mode is an integer too wide to compare properly,
4768 compare word by word. Rely on cse to optimize constant cases. */
4769
1eb8759b
RH
4770 if (GET_MODE_CLASS (mode) == MODE_INT
4771 && ! can_compare_p (op, mode, ccp_jump))
f5963e61
JL
4772 {
4773 rtx label2 = gen_label_rtx ();
4774
4775 switch (op)
4776 {
4777 case LTU:
4778 do_jump_by_parts_greater_rtx (mode, 1, arg2, arg1, label2, label);
4779 break;
4780
4781 case LEU:
4782 do_jump_by_parts_greater_rtx (mode, 1, arg1, arg2, label, label2);
4783 break;
4784
4785 case LT:
4786 do_jump_by_parts_greater_rtx (mode, 0, arg2, arg1, label2, label);
4787 break;
4788
4789 case GT:
4790 do_jump_by_parts_greater_rtx (mode, 0, arg1, arg2, label2, label);
4791 break;
4792
4793 case GE:
4794 do_jump_by_parts_greater_rtx (mode, 0, arg2, arg1, label, label2);
4795 break;
4796
4797 /* do_jump_by_parts_equality_rtx compares with zero. Luckily
4798 that's the only equality operations we do */
4799 case EQ:
4800 if (arg2 != const0_rtx || mode != GET_MODE(arg1))
c4f2c499 4801 abort ();
f5963e61
JL
4802 do_jump_by_parts_equality_rtx (arg1, label2, label);
4803 break;
4804
4805 case NE:
4806 if (arg2 != const0_rtx || mode != GET_MODE(arg1))
c4f2c499 4807 abort ();
f5963e61
JL
4808 do_jump_by_parts_equality_rtx (arg1, label, label2);
4809 break;
4810
4811 default:
c4f2c499 4812 abort ();
f5963e61
JL
4813 }
4814
4815 emit_label (label2);
4816 }
4817 else
d43e0b7d 4818 emit_cmp_and_jump_insns (arg1, arg2, op, NULL_RTX, mode, 0, label);
f5963e61 4819}
This page took 2.22838 seconds and 5 git commands to generate.