combine.c (simplify_set): Don't move a subreg in SET_SRC to SET_DEST if...
[gcc.git] / gcc / combine.c
1 /* Optimize by combining instructions for GNU compiler.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
21
22 /* This module is essentially the "combiner" phase of the U. of Arizona
23 Portable Optimizer, but redone to work on our list-structured
24 representation for RTL instead of their string representation.
25
26 The LOG_LINKS of each insn identify the most recent assignment
27 to each REG used in the insn. It is a list of previous insns,
28 each of which contains a SET for a REG that is used in this insn
29 and not used or set in between. LOG_LINKs never cross basic blocks.
30 They were set up by the preceding pass (lifetime analysis).
31
32 We try to combine each pair of insns joined by a logical link.
33 We also try to combine triples of insns A, B and C when
34 C has a link back to B and B has a link back to A.
35
36 LOG_LINKS does not have links for use of the CC0. They don't
37 need to, because the insn that sets the CC0 is always immediately
38 before the insn that tests it. So we always regard a branch
39 insn as having a logical link to the preceding insn. The same is true
40 for an insn explicitly using CC0.
41
42 We check (with use_crosses_set_p) to avoid combining in such a way
43 as to move a computation to a place where its value would be different.
44
45 Combination is done by mathematically substituting the previous
46 insn(s) values for the regs they set into the expressions in
47 the later insns that refer to these regs. If the result is a valid insn
48 for our target machine, according to the machine description,
49 we install it, delete the earlier insns, and update the data flow
50 information (LOG_LINKS and REG_NOTES) for what we did.
51
52 There are a few exceptions where the dataflow information created by
53 flow.c aren't completely updated:
54
55 - reg_live_length is not updated
56 - a LOG_LINKS entry that refers to an insn with multiple SETs may be
57 removed because there is no way to know which register it was
58 linking
59
60 To simplify substitution, we combine only when the earlier insn(s)
61 consist of only a single assignment. To simplify updating afterward,
62 we never combine when a subroutine call appears in the middle.
63
64 Since we do not represent assignments to CC0 explicitly except when that
65 is all an insn does, there is no LOG_LINKS entry in an insn that uses
66 the condition code for the insn that set the condition code.
67 Fortunately, these two insns must be consecutive.
68 Therefore, every JUMP_INSN is taken to have an implicit logical link
69 to the preceding insn. This is not quite right, since non-jumps can
70 also use the condition code; but in practice such insns would not
71 combine anyway. */
72
73 #include "config.h"
74 #include "system.h"
75 #include "coretypes.h"
76 #include "tm.h"
77 #include "rtl.h"
78 #include "tm_p.h"
79 #include "flags.h"
80 #include "regs.h"
81 #include "hard-reg-set.h"
82 #include "basic-block.h"
83 #include "insn-config.h"
84 #include "function.h"
85 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
86 #include "expr.h"
87 #include "insn-attr.h"
88 #include "recog.h"
89 #include "real.h"
90 #include "toplev.h"
91
92 /* It is not safe to use ordinary gen_lowpart in combine.
93 Use gen_lowpart_for_combine instead. See comments there. */
94 #define gen_lowpart dont_use_gen_lowpart_you_dummy
95
96 /* Number of attempts to combine instructions in this function. */
97
98 static int combine_attempts;
99
100 /* Number of attempts that got as far as substitution in this function. */
101
102 static int combine_merges;
103
104 /* Number of instructions combined with added SETs in this function. */
105
106 static int combine_extras;
107
108 /* Number of instructions combined in this function. */
109
110 static int combine_successes;
111
112 /* Totals over entire compilation. */
113
114 static int total_attempts, total_merges, total_extras, total_successes;
115
116 \f
117 /* Vector mapping INSN_UIDs to cuids.
118 The cuids are like uids but increase monotonically always.
119 Combine always uses cuids so that it can compare them.
120 But actually renumbering the uids, which we used to do,
121 proves to be a bad idea because it makes it hard to compare
122 the dumps produced by earlier passes with those from later passes. */
123
124 static int *uid_cuid;
125 static int max_uid_cuid;
126
127 /* Get the cuid of an insn. */
128
129 #define INSN_CUID(INSN) \
130 (INSN_UID (INSN) > max_uid_cuid ? insn_cuid (INSN) : uid_cuid[INSN_UID (INSN)])
131
132 /* In case BITS_PER_WORD == HOST_BITS_PER_WIDE_INT, shifting by
133 BITS_PER_WORD would invoke undefined behavior. Work around it. */
134
135 #define UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD(val) \
136 (((unsigned HOST_WIDE_INT) (val) << (BITS_PER_WORD - 1)) << 1)
137
138 #define nonzero_bits(X, M) \
139 cached_nonzero_bits (X, M, NULL_RTX, VOIDmode, 0)
140
141 #define num_sign_bit_copies(X, M) \
142 cached_num_sign_bit_copies (X, M, NULL_RTX, VOIDmode, 0)
143
144 /* Maximum register number, which is the size of the tables below. */
145
146 static unsigned int combine_max_regno;
147
148 /* Record last point of death of (hard or pseudo) register n. */
149
150 static rtx *reg_last_death;
151
152 /* Record last point of modification of (hard or pseudo) register n. */
153
154 static rtx *reg_last_set;
155
156 /* Record the cuid of the last insn that invalidated memory
157 (anything that writes memory, and subroutine calls, but not pushes). */
158
159 static int mem_last_set;
160
161 /* Record the cuid of the last CALL_INSN
162 so we can tell whether a potential combination crosses any calls. */
163
164 static int last_call_cuid;
165
166 /* When `subst' is called, this is the insn that is being modified
167 (by combining in a previous insn). The PATTERN of this insn
168 is still the old pattern partially modified and it should not be
169 looked at, but this may be used to examine the successors of the insn
170 to judge whether a simplification is valid. */
171
172 static rtx subst_insn;
173
174 /* This is the lowest CUID that `subst' is currently dealing with.
175 get_last_value will not return a value if the register was set at or
176 after this CUID. If not for this mechanism, we could get confused if
177 I2 or I1 in try_combine were an insn that used the old value of a register
178 to obtain a new value. In that case, we might erroneously get the
179 new value of the register when we wanted the old one. */
180
181 static int subst_low_cuid;
182
183 /* This contains any hard registers that are used in newpat; reg_dead_at_p
184 must consider all these registers to be always live. */
185
186 static HARD_REG_SET newpat_used_regs;
187
188 /* This is an insn to which a LOG_LINKS entry has been added. If this
189 insn is the earlier than I2 or I3, combine should rescan starting at
190 that location. */
191
192 static rtx added_links_insn;
193
194 /* Basic block in which we are performing combines. */
195 static basic_block this_basic_block;
196
197 /* A bitmap indicating which blocks had registers go dead at entry.
198 After combine, we'll need to re-do global life analysis with
199 those blocks as starting points. */
200 static sbitmap refresh_blocks;
201 \f
202 /* The next group of arrays allows the recording of the last value assigned
203 to (hard or pseudo) register n. We use this information to see if an
204 operation being processed is redundant given a prior operation performed
205 on the register. For example, an `and' with a constant is redundant if
206 all the zero bits are already known to be turned off.
207
208 We use an approach similar to that used by cse, but change it in the
209 following ways:
210
211 (1) We do not want to reinitialize at each label.
212 (2) It is useful, but not critical, to know the actual value assigned
213 to a register. Often just its form is helpful.
214
215 Therefore, we maintain the following arrays:
216
217 reg_last_set_value the last value assigned
218 reg_last_set_label records the value of label_tick when the
219 register was assigned
220 reg_last_set_table_tick records the value of label_tick when a
221 value using the register is assigned
222 reg_last_set_invalid set to nonzero when it is not valid
223 to use the value of this register in some
224 register's value
225
226 To understand the usage of these tables, it is important to understand
227 the distinction between the value in reg_last_set_value being valid
228 and the register being validly contained in some other expression in the
229 table.
230
231 Entry I in reg_last_set_value is valid if it is nonzero, and either
232 reg_n_sets[i] is 1 or reg_last_set_label[i] == label_tick.
233
234 Register I may validly appear in any expression returned for the value
235 of another register if reg_n_sets[i] is 1. It may also appear in the
236 value for register J if reg_last_set_label[i] < reg_last_set_label[j] or
237 reg_last_set_invalid[j] is zero.
238
239 If an expression is found in the table containing a register which may
240 not validly appear in an expression, the register is replaced by
241 something that won't match, (clobber (const_int 0)).
242
243 reg_last_set_invalid[i] is set nonzero when register I is being assigned
244 to and reg_last_set_table_tick[i] == label_tick. */
245
246 /* Record last value assigned to (hard or pseudo) register n. */
247
248 static rtx *reg_last_set_value;
249
250 /* Record the value of label_tick when the value for register n is placed in
251 reg_last_set_value[n]. */
252
253 static int *reg_last_set_label;
254
255 /* Record the value of label_tick when an expression involving register n
256 is placed in reg_last_set_value. */
257
258 static int *reg_last_set_table_tick;
259
260 /* Set nonzero if references to register n in expressions should not be
261 used. */
262
263 static char *reg_last_set_invalid;
264
265 /* Incremented for each label. */
266
267 static int label_tick;
268
269 /* Some registers that are set more than once and used in more than one
270 basic block are nevertheless always set in similar ways. For example,
271 a QImode register may be loaded from memory in two places on a machine
272 where byte loads zero extend.
273
274 We record in the following array what we know about the nonzero
275 bits of a register, specifically which bits are known to be zero.
276
277 If an entry is zero, it means that we don't know anything special. */
278
279 static unsigned HOST_WIDE_INT *reg_nonzero_bits;
280
281 /* Mode used to compute significance in reg_nonzero_bits. It is the largest
282 integer mode that can fit in HOST_BITS_PER_WIDE_INT. */
283
284 static enum machine_mode nonzero_bits_mode;
285
286 /* Nonzero if we know that a register has some leading bits that are always
287 equal to the sign bit. */
288
289 static unsigned char *reg_sign_bit_copies;
290
291 /* Nonzero when reg_nonzero_bits and reg_sign_bit_copies can be safely used.
292 It is zero while computing them and after combine has completed. This
293 former test prevents propagating values based on previously set values,
294 which can be incorrect if a variable is modified in a loop. */
295
296 static int nonzero_sign_valid;
297
298 /* These arrays are maintained in parallel with reg_last_set_value
299 and are used to store the mode in which the register was last set,
300 the bits that were known to be zero when it was last set, and the
301 number of sign bits copies it was known to have when it was last set. */
302
303 static enum machine_mode *reg_last_set_mode;
304 static unsigned HOST_WIDE_INT *reg_last_set_nonzero_bits;
305 static char *reg_last_set_sign_bit_copies;
306 \f
307 /* Record one modification to rtl structure
308 to be undone by storing old_contents into *where.
309 is_int is 1 if the contents are an int. */
310
311 struct undo
312 {
313 struct undo *next;
314 int is_int;
315 union {rtx r; int i;} old_contents;
316 union {rtx *r; int *i;} where;
317 };
318
319 /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
320 num_undo says how many are currently recorded.
321
322 other_insn is nonzero if we have modified some other insn in the process
323 of working on subst_insn. It must be verified too. */
324
325 struct undobuf
326 {
327 struct undo *undos;
328 struct undo *frees;
329 rtx other_insn;
330 };
331
332 static struct undobuf undobuf;
333
334 /* Number of times the pseudo being substituted for
335 was found and replaced. */
336
337 static int n_occurrences;
338
339 static void do_SUBST PARAMS ((rtx *, rtx));
340 static void do_SUBST_INT PARAMS ((int *, int));
341 static void init_reg_last_arrays PARAMS ((void));
342 static void setup_incoming_promotions PARAMS ((void));
343 static void set_nonzero_bits_and_sign_copies PARAMS ((rtx, rtx, void *));
344 static int cant_combine_insn_p PARAMS ((rtx));
345 static int can_combine_p PARAMS ((rtx, rtx, rtx, rtx, rtx *, rtx *));
346 static int sets_function_arg_p PARAMS ((rtx));
347 static int combinable_i3pat PARAMS ((rtx, rtx *, rtx, rtx, int, rtx *));
348 static int contains_muldiv PARAMS ((rtx));
349 static rtx try_combine PARAMS ((rtx, rtx, rtx, int *));
350 static void undo_all PARAMS ((void));
351 static void undo_commit PARAMS ((void));
352 static rtx *find_split_point PARAMS ((rtx *, rtx));
353 static rtx subst PARAMS ((rtx, rtx, rtx, int, int));
354 static rtx combine_simplify_rtx PARAMS ((rtx, enum machine_mode, int, int));
355 static rtx simplify_if_then_else PARAMS ((rtx));
356 static rtx simplify_set PARAMS ((rtx));
357 static rtx simplify_logical PARAMS ((rtx, int));
358 static rtx expand_compound_operation PARAMS ((rtx));
359 static rtx expand_field_assignment PARAMS ((rtx));
360 static rtx make_extraction PARAMS ((enum machine_mode, rtx, HOST_WIDE_INT,
361 rtx, unsigned HOST_WIDE_INT, int,
362 int, int));
363 static rtx extract_left_shift PARAMS ((rtx, int));
364 static rtx make_compound_operation PARAMS ((rtx, enum rtx_code));
365 static int get_pos_from_mask PARAMS ((unsigned HOST_WIDE_INT,
366 unsigned HOST_WIDE_INT *));
367 static rtx force_to_mode PARAMS ((rtx, enum machine_mode,
368 unsigned HOST_WIDE_INT, rtx, int));
369 static rtx if_then_else_cond PARAMS ((rtx, rtx *, rtx *));
370 static rtx known_cond PARAMS ((rtx, enum rtx_code, rtx, rtx));
371 static int rtx_equal_for_field_assignment_p PARAMS ((rtx, rtx));
372 static rtx make_field_assignment PARAMS ((rtx));
373 static rtx apply_distributive_law PARAMS ((rtx));
374 static rtx simplify_and_const_int PARAMS ((rtx, enum machine_mode, rtx,
375 unsigned HOST_WIDE_INT));
376 static unsigned HOST_WIDE_INT cached_nonzero_bits
377 PARAMS ((rtx, enum machine_mode, rtx,
378 enum machine_mode,
379 unsigned HOST_WIDE_INT));
380 static unsigned HOST_WIDE_INT nonzero_bits1
381 PARAMS ((rtx, enum machine_mode, rtx,
382 enum machine_mode,
383 unsigned HOST_WIDE_INT));
384 static unsigned int cached_num_sign_bit_copies
385 PARAMS ((rtx, enum machine_mode, rtx,
386 enum machine_mode, unsigned int));
387 static unsigned int num_sign_bit_copies1
388 PARAMS ((rtx, enum machine_mode, rtx,
389 enum machine_mode, unsigned int));
390 static int merge_outer_ops PARAMS ((enum rtx_code *, HOST_WIDE_INT *,
391 enum rtx_code, HOST_WIDE_INT,
392 enum machine_mode, int *));
393 static rtx simplify_shift_const PARAMS ((rtx, enum rtx_code, enum machine_mode,
394 rtx, int));
395 static int recog_for_combine PARAMS ((rtx *, rtx, rtx *));
396 static rtx gen_lowpart_for_combine PARAMS ((enum machine_mode, rtx));
397 static rtx gen_binary PARAMS ((enum rtx_code, enum machine_mode,
398 rtx, rtx));
399 static enum rtx_code simplify_comparison PARAMS ((enum rtx_code, rtx *, rtx *));
400 static void update_table_tick PARAMS ((rtx));
401 static void record_value_for_reg PARAMS ((rtx, rtx, rtx));
402 static void check_promoted_subreg PARAMS ((rtx, rtx));
403 static void record_dead_and_set_regs_1 PARAMS ((rtx, rtx, void *));
404 static void record_dead_and_set_regs PARAMS ((rtx));
405 static int get_last_value_validate PARAMS ((rtx *, rtx, int, int));
406 static rtx get_last_value PARAMS ((rtx));
407 static int use_crosses_set_p PARAMS ((rtx, int));
408 static void reg_dead_at_p_1 PARAMS ((rtx, rtx, void *));
409 static int reg_dead_at_p PARAMS ((rtx, rtx));
410 static void move_deaths PARAMS ((rtx, rtx, int, rtx, rtx *));
411 static int reg_bitfield_target_p PARAMS ((rtx, rtx));
412 static void distribute_notes PARAMS ((rtx, rtx, rtx, rtx));
413 static void distribute_links PARAMS ((rtx));
414 static void mark_used_regs_combine PARAMS ((rtx));
415 static int insn_cuid PARAMS ((rtx));
416 static void record_promoted_value PARAMS ((rtx, rtx));
417 static rtx reversed_comparison PARAMS ((rtx, enum machine_mode, rtx, rtx));
418 static enum rtx_code combine_reversed_comparison_code PARAMS ((rtx));
419 \f
420 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
421 insn. The substitution can be undone by undo_all. If INTO is already
422 set to NEWVAL, do not record this change. Because computing NEWVAL might
423 also call SUBST, we have to compute it before we put anything into
424 the undo table. */
425
426 static void
427 do_SUBST (into, newval)
428 rtx *into, newval;
429 {
430 struct undo *buf;
431 rtx oldval = *into;
432
433 if (oldval == newval)
434 return;
435
436 /* We'd like to catch as many invalid transformations here as
437 possible. Unfortunately, there are way too many mode changes
438 that are perfectly valid, so we'd waste too much effort for
439 little gain doing the checks here. Focus on catching invalid
440 transformations involving integer constants. */
441 if (GET_MODE_CLASS (GET_MODE (oldval)) == MODE_INT
442 && GET_CODE (newval) == CONST_INT)
443 {
444 /* Sanity check that we're replacing oldval with a CONST_INT
445 that is a valid sign-extension for the original mode. */
446 if (INTVAL (newval) != trunc_int_for_mode (INTVAL (newval),
447 GET_MODE (oldval)))
448 abort ();
449
450 /* Replacing the operand of a SUBREG or a ZERO_EXTEND with a
451 CONST_INT is not valid, because after the replacement, the
452 original mode would be gone. Unfortunately, we can't tell
453 when do_SUBST is called to replace the operand thereof, so we
454 perform this test on oldval instead, checking whether an
455 invalid replacement took place before we got here. */
456 if ((GET_CODE (oldval) == SUBREG
457 && GET_CODE (SUBREG_REG (oldval)) == CONST_INT)
458 || (GET_CODE (oldval) == ZERO_EXTEND
459 && GET_CODE (XEXP (oldval, 0)) == CONST_INT))
460 abort ();
461 }
462
463 if (undobuf.frees)
464 buf = undobuf.frees, undobuf.frees = buf->next;
465 else
466 buf = (struct undo *) xmalloc (sizeof (struct undo));
467
468 buf->is_int = 0;
469 buf->where.r = into;
470 buf->old_contents.r = oldval;
471 *into = newval;
472
473 buf->next = undobuf.undos, undobuf.undos = buf;
474 }
475
476 #define SUBST(INTO, NEWVAL) do_SUBST(&(INTO), (NEWVAL))
477
478 /* Similar to SUBST, but NEWVAL is an int expression. Note that substitution
479 for the value of a HOST_WIDE_INT value (including CONST_INT) is
480 not safe. */
481
482 static void
483 do_SUBST_INT (into, newval)
484 int *into, newval;
485 {
486 struct undo *buf;
487 int oldval = *into;
488
489 if (oldval == newval)
490 return;
491
492 if (undobuf.frees)
493 buf = undobuf.frees, undobuf.frees = buf->next;
494 else
495 buf = (struct undo *) xmalloc (sizeof (struct undo));
496
497 buf->is_int = 1;
498 buf->where.i = into;
499 buf->old_contents.i = oldval;
500 *into = newval;
501
502 buf->next = undobuf.undos, undobuf.undos = buf;
503 }
504
505 #define SUBST_INT(INTO, NEWVAL) do_SUBST_INT(&(INTO), (NEWVAL))
506 \f
507 /* Main entry point for combiner. F is the first insn of the function.
508 NREGS is the first unused pseudo-reg number.
509
510 Return nonzero if the combiner has turned an indirect jump
511 instruction into a direct jump. */
512 int
513 combine_instructions (f, nregs)
514 rtx f;
515 unsigned int nregs;
516 {
517 rtx insn, next;
518 #ifdef HAVE_cc0
519 rtx prev;
520 #endif
521 int i;
522 rtx links, nextlinks;
523
524 int new_direct_jump_p = 0;
525
526 combine_attempts = 0;
527 combine_merges = 0;
528 combine_extras = 0;
529 combine_successes = 0;
530
531 combine_max_regno = nregs;
532
533 reg_nonzero_bits = ((unsigned HOST_WIDE_INT *)
534 xcalloc (nregs, sizeof (unsigned HOST_WIDE_INT)));
535 reg_sign_bit_copies
536 = (unsigned char *) xcalloc (nregs, sizeof (unsigned char));
537
538 reg_last_death = (rtx *) xmalloc (nregs * sizeof (rtx));
539 reg_last_set = (rtx *) xmalloc (nregs * sizeof (rtx));
540 reg_last_set_value = (rtx *) xmalloc (nregs * sizeof (rtx));
541 reg_last_set_table_tick = (int *) xmalloc (nregs * sizeof (int));
542 reg_last_set_label = (int *) xmalloc (nregs * sizeof (int));
543 reg_last_set_invalid = (char *) xmalloc (nregs * sizeof (char));
544 reg_last_set_mode
545 = (enum machine_mode *) xmalloc (nregs * sizeof (enum machine_mode));
546 reg_last_set_nonzero_bits
547 = (unsigned HOST_WIDE_INT *) xmalloc (nregs * sizeof (HOST_WIDE_INT));
548 reg_last_set_sign_bit_copies
549 = (char *) xmalloc (nregs * sizeof (char));
550
551 init_reg_last_arrays ();
552
553 init_recog_no_volatile ();
554
555 /* Compute maximum uid value so uid_cuid can be allocated. */
556
557 for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
558 if (INSN_UID (insn) > i)
559 i = INSN_UID (insn);
560
561 uid_cuid = (int *) xmalloc ((i + 1) * sizeof (int));
562 max_uid_cuid = i;
563
564 nonzero_bits_mode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
565
566 /* Don't use reg_nonzero_bits when computing it. This can cause problems
567 when, for example, we have j <<= 1 in a loop. */
568
569 nonzero_sign_valid = 0;
570
571 /* Compute the mapping from uids to cuids.
572 Cuids are numbers assigned to insns, like uids,
573 except that cuids increase monotonically through the code.
574
575 Scan all SETs and see if we can deduce anything about what
576 bits are known to be zero for some registers and how many copies
577 of the sign bit are known to exist for those registers.
578
579 Also set any known values so that we can use it while searching
580 for what bits are known to be set. */
581
582 label_tick = 1;
583
584 setup_incoming_promotions ();
585
586 refresh_blocks = sbitmap_alloc (last_basic_block);
587 sbitmap_zero (refresh_blocks);
588
589 for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
590 {
591 uid_cuid[INSN_UID (insn)] = ++i;
592 subst_low_cuid = i;
593 subst_insn = insn;
594
595 if (INSN_P (insn))
596 {
597 note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies,
598 NULL);
599 record_dead_and_set_regs (insn);
600
601 #ifdef AUTO_INC_DEC
602 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
603 if (REG_NOTE_KIND (links) == REG_INC)
604 set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX,
605 NULL);
606 #endif
607 }
608
609 if (GET_CODE (insn) == CODE_LABEL)
610 label_tick++;
611 }
612
613 nonzero_sign_valid = 1;
614
615 /* Now scan all the insns in forward order. */
616
617 label_tick = 1;
618 last_call_cuid = 0;
619 mem_last_set = 0;
620 init_reg_last_arrays ();
621 setup_incoming_promotions ();
622
623 FOR_EACH_BB (this_basic_block)
624 {
625 for (insn = this_basic_block->head;
626 insn != NEXT_INSN (this_basic_block->end);
627 insn = next ? next : NEXT_INSN (insn))
628 {
629 next = 0;
630
631 if (GET_CODE (insn) == CODE_LABEL)
632 label_tick++;
633
634 else if (INSN_P (insn))
635 {
636 /* See if we know about function return values before this
637 insn based upon SUBREG flags. */
638 check_promoted_subreg (insn, PATTERN (insn));
639
640 /* Try this insn with each insn it links back to. */
641
642 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
643 if ((next = try_combine (insn, XEXP (links, 0),
644 NULL_RTX, &new_direct_jump_p)) != 0)
645 goto retry;
646
647 /* Try each sequence of three linked insns ending with this one. */
648
649 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
650 {
651 rtx link = XEXP (links, 0);
652
653 /* If the linked insn has been replaced by a note, then there
654 is no point in pursuing this chain any further. */
655 if (GET_CODE (link) == NOTE)
656 continue;
657
658 for (nextlinks = LOG_LINKS (link);
659 nextlinks;
660 nextlinks = XEXP (nextlinks, 1))
661 if ((next = try_combine (insn, link,
662 XEXP (nextlinks, 0),
663 &new_direct_jump_p)) != 0)
664 goto retry;
665 }
666
667 #ifdef HAVE_cc0
668 /* Try to combine a jump insn that uses CC0
669 with a preceding insn that sets CC0, and maybe with its
670 logical predecessor as well.
671 This is how we make decrement-and-branch insns.
672 We need this special code because data flow connections
673 via CC0 do not get entered in LOG_LINKS. */
674
675 if (GET_CODE (insn) == JUMP_INSN
676 && (prev = prev_nonnote_insn (insn)) != 0
677 && GET_CODE (prev) == INSN
678 && sets_cc0_p (PATTERN (prev)))
679 {
680 if ((next = try_combine (insn, prev,
681 NULL_RTX, &new_direct_jump_p)) != 0)
682 goto retry;
683
684 for (nextlinks = LOG_LINKS (prev); nextlinks;
685 nextlinks = XEXP (nextlinks, 1))
686 if ((next = try_combine (insn, prev,
687 XEXP (nextlinks, 0),
688 &new_direct_jump_p)) != 0)
689 goto retry;
690 }
691
692 /* Do the same for an insn that explicitly references CC0. */
693 if (GET_CODE (insn) == INSN
694 && (prev = prev_nonnote_insn (insn)) != 0
695 && GET_CODE (prev) == INSN
696 && sets_cc0_p (PATTERN (prev))
697 && GET_CODE (PATTERN (insn)) == SET
698 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
699 {
700 if ((next = try_combine (insn, prev,
701 NULL_RTX, &new_direct_jump_p)) != 0)
702 goto retry;
703
704 for (nextlinks = LOG_LINKS (prev); nextlinks;
705 nextlinks = XEXP (nextlinks, 1))
706 if ((next = try_combine (insn, prev,
707 XEXP (nextlinks, 0),
708 &new_direct_jump_p)) != 0)
709 goto retry;
710 }
711
712 /* Finally, see if any of the insns that this insn links to
713 explicitly references CC0. If so, try this insn, that insn,
714 and its predecessor if it sets CC0. */
715 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
716 if (GET_CODE (XEXP (links, 0)) == INSN
717 && GET_CODE (PATTERN (XEXP (links, 0))) == SET
718 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (XEXP (links, 0))))
719 && (prev = prev_nonnote_insn (XEXP (links, 0))) != 0
720 && GET_CODE (prev) == INSN
721 && sets_cc0_p (PATTERN (prev))
722 && (next = try_combine (insn, XEXP (links, 0),
723 prev, &new_direct_jump_p)) != 0)
724 goto retry;
725 #endif
726
727 /* Try combining an insn with two different insns whose results it
728 uses. */
729 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
730 for (nextlinks = XEXP (links, 1); nextlinks;
731 nextlinks = XEXP (nextlinks, 1))
732 if ((next = try_combine (insn, XEXP (links, 0),
733 XEXP (nextlinks, 0),
734 &new_direct_jump_p)) != 0)
735 goto retry;
736
737 if (GET_CODE (insn) != NOTE)
738 record_dead_and_set_regs (insn);
739
740 retry:
741 ;
742 }
743 }
744 }
745 clear_bb_flags ();
746
747 EXECUTE_IF_SET_IN_SBITMAP (refresh_blocks, 0, i,
748 BASIC_BLOCK (i)->flags |= BB_DIRTY);
749 new_direct_jump_p |= purge_all_dead_edges (0);
750 delete_noop_moves (f);
751
752 update_life_info_in_dirty_blocks (UPDATE_LIFE_GLOBAL_RM_NOTES,
753 PROP_DEATH_NOTES | PROP_SCAN_DEAD_CODE
754 | PROP_KILL_DEAD_CODE);
755
756 /* Clean up. */
757 sbitmap_free (refresh_blocks);
758 free (reg_nonzero_bits);
759 free (reg_sign_bit_copies);
760 free (reg_last_death);
761 free (reg_last_set);
762 free (reg_last_set_value);
763 free (reg_last_set_table_tick);
764 free (reg_last_set_label);
765 free (reg_last_set_invalid);
766 free (reg_last_set_mode);
767 free (reg_last_set_nonzero_bits);
768 free (reg_last_set_sign_bit_copies);
769 free (uid_cuid);
770
771 {
772 struct undo *undo, *next;
773 for (undo = undobuf.frees; undo; undo = next)
774 {
775 next = undo->next;
776 free (undo);
777 }
778 undobuf.frees = 0;
779 }
780
781 total_attempts += combine_attempts;
782 total_merges += combine_merges;
783 total_extras += combine_extras;
784 total_successes += combine_successes;
785
786 nonzero_sign_valid = 0;
787
788 /* Make recognizer allow volatile MEMs again. */
789 init_recog ();
790
791 return new_direct_jump_p;
792 }
793
794 /* Wipe the reg_last_xxx arrays in preparation for another pass. */
795
796 static void
797 init_reg_last_arrays ()
798 {
799 unsigned int nregs = combine_max_regno;
800
801 memset ((char *) reg_last_death, 0, nregs * sizeof (rtx));
802 memset ((char *) reg_last_set, 0, nregs * sizeof (rtx));
803 memset ((char *) reg_last_set_value, 0, nregs * sizeof (rtx));
804 memset ((char *) reg_last_set_table_tick, 0, nregs * sizeof (int));
805 memset ((char *) reg_last_set_label, 0, nregs * sizeof (int));
806 memset (reg_last_set_invalid, 0, nregs * sizeof (char));
807 memset ((char *) reg_last_set_mode, 0, nregs * sizeof (enum machine_mode));
808 memset ((char *) reg_last_set_nonzero_bits, 0, nregs * sizeof (HOST_WIDE_INT));
809 memset (reg_last_set_sign_bit_copies, 0, nregs * sizeof (char));
810 }
811 \f
812 /* Set up any promoted values for incoming argument registers. */
813
814 static void
815 setup_incoming_promotions ()
816 {
817 #ifdef PROMOTE_FUNCTION_ARGS
818 unsigned int regno;
819 rtx reg;
820 enum machine_mode mode;
821 int unsignedp;
822 rtx first = get_insns ();
823
824 #ifndef OUTGOING_REGNO
825 #define OUTGOING_REGNO(N) N
826 #endif
827 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
828 /* Check whether this register can hold an incoming pointer
829 argument. FUNCTION_ARG_REGNO_P tests outgoing register
830 numbers, so translate if necessary due to register windows. */
831 if (FUNCTION_ARG_REGNO_P (OUTGOING_REGNO (regno))
832 && (reg = promoted_input_arg (regno, &mode, &unsignedp)) != 0)
833 {
834 record_value_for_reg
835 (reg, first, gen_rtx_fmt_e ((unsignedp ? ZERO_EXTEND
836 : SIGN_EXTEND),
837 GET_MODE (reg),
838 gen_rtx_CLOBBER (mode, const0_rtx)));
839 }
840 #endif
841 }
842 \f
843 /* Called via note_stores. If X is a pseudo that is narrower than
844 HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
845
846 If we are setting only a portion of X and we can't figure out what
847 portion, assume all bits will be used since we don't know what will
848 be happening.
849
850 Similarly, set how many bits of X are known to be copies of the sign bit
851 at all locations in the function. This is the smallest number implied
852 by any set of X. */
853
854 static void
855 set_nonzero_bits_and_sign_copies (x, set, data)
856 rtx x;
857 rtx set;
858 void *data ATTRIBUTE_UNUSED;
859 {
860 unsigned int num;
861
862 if (GET_CODE (x) == REG
863 && REGNO (x) >= FIRST_PSEUDO_REGISTER
864 /* If this register is undefined at the start of the file, we can't
865 say what its contents were. */
866 && ! REGNO_REG_SET_P (ENTRY_BLOCK_PTR->next_bb->global_live_at_start, REGNO (x))
867 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
868 {
869 if (set == 0 || GET_CODE (set) == CLOBBER)
870 {
871 reg_nonzero_bits[REGNO (x)] = GET_MODE_MASK (GET_MODE (x));
872 reg_sign_bit_copies[REGNO (x)] = 1;
873 return;
874 }
875
876 /* If this is a complex assignment, see if we can convert it into a
877 simple assignment. */
878 set = expand_field_assignment (set);
879
880 /* If this is a simple assignment, or we have a paradoxical SUBREG,
881 set what we know about X. */
882
883 if (SET_DEST (set) == x
884 || (GET_CODE (SET_DEST (set)) == SUBREG
885 && (GET_MODE_SIZE (GET_MODE (SET_DEST (set)))
886 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (set)))))
887 && SUBREG_REG (SET_DEST (set)) == x))
888 {
889 rtx src = SET_SRC (set);
890
891 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
892 /* If X is narrower than a word and SRC is a non-negative
893 constant that would appear negative in the mode of X,
894 sign-extend it for use in reg_nonzero_bits because some
895 machines (maybe most) will actually do the sign-extension
896 and this is the conservative approach.
897
898 ??? For 2.5, try to tighten up the MD files in this regard
899 instead of this kludge. */
900
901 if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
902 && GET_CODE (src) == CONST_INT
903 && INTVAL (src) > 0
904 && 0 != (INTVAL (src)
905 & ((HOST_WIDE_INT) 1
906 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
907 src = GEN_INT (INTVAL (src)
908 | ((HOST_WIDE_INT) (-1)
909 << GET_MODE_BITSIZE (GET_MODE (x))));
910 #endif
911
912 /* Don't call nonzero_bits if it cannot change anything. */
913 if (reg_nonzero_bits[REGNO (x)] != ~(unsigned HOST_WIDE_INT) 0)
914 reg_nonzero_bits[REGNO (x)]
915 |= nonzero_bits (src, nonzero_bits_mode);
916 num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
917 if (reg_sign_bit_copies[REGNO (x)] == 0
918 || reg_sign_bit_copies[REGNO (x)] > num)
919 reg_sign_bit_copies[REGNO (x)] = num;
920 }
921 else
922 {
923 reg_nonzero_bits[REGNO (x)] = GET_MODE_MASK (GET_MODE (x));
924 reg_sign_bit_copies[REGNO (x)] = 1;
925 }
926 }
927 }
928 \f
929 /* See if INSN can be combined into I3. PRED and SUCC are optionally
930 insns that were previously combined into I3 or that will be combined
931 into the merger of INSN and I3.
932
933 Return 0 if the combination is not allowed for any reason.
934
935 If the combination is allowed, *PDEST will be set to the single
936 destination of INSN and *PSRC to the single source, and this function
937 will return 1. */
938
939 static int
940 can_combine_p (insn, i3, pred, succ, pdest, psrc)
941 rtx insn;
942 rtx i3;
943 rtx pred ATTRIBUTE_UNUSED;
944 rtx succ;
945 rtx *pdest, *psrc;
946 {
947 int i;
948 rtx set = 0, src, dest;
949 rtx p;
950 #ifdef AUTO_INC_DEC
951 rtx link;
952 #endif
953 int all_adjacent = (succ ? (next_active_insn (insn) == succ
954 && next_active_insn (succ) == i3)
955 : next_active_insn (insn) == i3);
956
957 /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
958 or a PARALLEL consisting of such a SET and CLOBBERs.
959
960 If INSN has CLOBBER parallel parts, ignore them for our processing.
961 By definition, these happen during the execution of the insn. When it
962 is merged with another insn, all bets are off. If they are, in fact,
963 needed and aren't also supplied in I3, they may be added by
964 recog_for_combine. Otherwise, it won't match.
965
966 We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
967 note.
968
969 Get the source and destination of INSN. If more than one, can't
970 combine. */
971
972 if (GET_CODE (PATTERN (insn)) == SET)
973 set = PATTERN (insn);
974 else if (GET_CODE (PATTERN (insn)) == PARALLEL
975 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
976 {
977 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
978 {
979 rtx elt = XVECEXP (PATTERN (insn), 0, i);
980
981 switch (GET_CODE (elt))
982 {
983 /* This is important to combine floating point insns
984 for the SH4 port. */
985 case USE:
986 /* Combining an isolated USE doesn't make sense.
987 We depend here on combinable_i3pat to reject them. */
988 /* The code below this loop only verifies that the inputs of
989 the SET in INSN do not change. We call reg_set_between_p
990 to verify that the REG in the USE does not change between
991 I3 and INSN.
992 If the USE in INSN was for a pseudo register, the matching
993 insn pattern will likely match any register; combining this
994 with any other USE would only be safe if we knew that the
995 used registers have identical values, or if there was
996 something to tell them apart, e.g. different modes. For
997 now, we forgo such complicated tests and simply disallow
998 combining of USES of pseudo registers with any other USE. */
999 if (GET_CODE (XEXP (elt, 0)) == REG
1000 && GET_CODE (PATTERN (i3)) == PARALLEL)
1001 {
1002 rtx i3pat = PATTERN (i3);
1003 int i = XVECLEN (i3pat, 0) - 1;
1004 unsigned int regno = REGNO (XEXP (elt, 0));
1005
1006 do
1007 {
1008 rtx i3elt = XVECEXP (i3pat, 0, i);
1009
1010 if (GET_CODE (i3elt) == USE
1011 && GET_CODE (XEXP (i3elt, 0)) == REG
1012 && (REGNO (XEXP (i3elt, 0)) == regno
1013 ? reg_set_between_p (XEXP (elt, 0),
1014 PREV_INSN (insn), i3)
1015 : regno >= FIRST_PSEUDO_REGISTER))
1016 return 0;
1017 }
1018 while (--i >= 0);
1019 }
1020 break;
1021
1022 /* We can ignore CLOBBERs. */
1023 case CLOBBER:
1024 break;
1025
1026 case SET:
1027 /* Ignore SETs whose result isn't used but not those that
1028 have side-effects. */
1029 if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
1030 && ! side_effects_p (elt))
1031 break;
1032
1033 /* If we have already found a SET, this is a second one and
1034 so we cannot combine with this insn. */
1035 if (set)
1036 return 0;
1037
1038 set = elt;
1039 break;
1040
1041 default:
1042 /* Anything else means we can't combine. */
1043 return 0;
1044 }
1045 }
1046
1047 if (set == 0
1048 /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
1049 so don't do anything with it. */
1050 || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
1051 return 0;
1052 }
1053 else
1054 return 0;
1055
1056 if (set == 0)
1057 return 0;
1058
1059 set = expand_field_assignment (set);
1060 src = SET_SRC (set), dest = SET_DEST (set);
1061
1062 /* Don't eliminate a store in the stack pointer. */
1063 if (dest == stack_pointer_rtx
1064 /* Don't combine with an insn that sets a register to itself if it has
1065 a REG_EQUAL note. This may be part of a REG_NO_CONFLICT sequence. */
1066 || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1067 /* Can't merge an ASM_OPERANDS. */
1068 || GET_CODE (src) == ASM_OPERANDS
1069 /* Can't merge a function call. */
1070 || GET_CODE (src) == CALL
1071 /* Don't eliminate a function call argument. */
1072 || (GET_CODE (i3) == CALL_INSN
1073 && (find_reg_fusage (i3, USE, dest)
1074 || (GET_CODE (dest) == REG
1075 && REGNO (dest) < FIRST_PSEUDO_REGISTER
1076 && global_regs[REGNO (dest)])))
1077 /* Don't substitute into an incremented register. */
1078 || FIND_REG_INC_NOTE (i3, dest)
1079 || (succ && FIND_REG_INC_NOTE (succ, dest))
1080 #if 0
1081 /* Don't combine the end of a libcall into anything. */
1082 /* ??? This gives worse code, and appears to be unnecessary, since no
1083 pass after flow uses REG_LIBCALL/REG_RETVAL notes. Local-alloc does
1084 use REG_RETVAL notes for noconflict blocks, but other code here
1085 makes sure that those insns don't disappear. */
1086 || find_reg_note (insn, REG_RETVAL, NULL_RTX)
1087 #endif
1088 /* Make sure that DEST is not used after SUCC but before I3. */
1089 || (succ && ! all_adjacent
1090 && reg_used_between_p (dest, succ, i3))
1091 /* Make sure that the value that is to be substituted for the register
1092 does not use any registers whose values alter in between. However,
1093 If the insns are adjacent, a use can't cross a set even though we
1094 think it might (this can happen for a sequence of insns each setting
1095 the same destination; reg_last_set of that register might point to
1096 a NOTE). If INSN has a REG_EQUIV note, the register is always
1097 equivalent to the memory so the substitution is valid even if there
1098 are intervening stores. Also, don't move a volatile asm or
1099 UNSPEC_VOLATILE across any other insns. */
1100 || (! all_adjacent
1101 && (((GET_CODE (src) != MEM
1102 || ! find_reg_note (insn, REG_EQUIV, src))
1103 && use_crosses_set_p (src, INSN_CUID (insn)))
1104 || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
1105 || GET_CODE (src) == UNSPEC_VOLATILE))
1106 /* If there is a REG_NO_CONFLICT note for DEST in I3 or SUCC, we get
1107 better register allocation by not doing the combine. */
1108 || find_reg_note (i3, REG_NO_CONFLICT, dest)
1109 || (succ && find_reg_note (succ, REG_NO_CONFLICT, dest))
1110 /* Don't combine across a CALL_INSN, because that would possibly
1111 change whether the life span of some REGs crosses calls or not,
1112 and it is a pain to update that information.
1113 Exception: if source is a constant, moving it later can't hurt.
1114 Accept that special case, because it helps -fforce-addr a lot. */
1115 || (INSN_CUID (insn) < last_call_cuid && ! CONSTANT_P (src)))
1116 return 0;
1117
1118 /* DEST must either be a REG or CC0. */
1119 if (GET_CODE (dest) == REG)
1120 {
1121 /* If register alignment is being enforced for multi-word items in all
1122 cases except for parameters, it is possible to have a register copy
1123 insn referencing a hard register that is not allowed to contain the
1124 mode being copied and which would not be valid as an operand of most
1125 insns. Eliminate this problem by not combining with such an insn.
1126
1127 Also, on some machines we don't want to extend the life of a hard
1128 register. */
1129
1130 if (GET_CODE (src) == REG
1131 && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
1132 && ! HARD_REGNO_MODE_OK (REGNO (dest), GET_MODE (dest)))
1133 /* Don't extend the life of a hard register unless it is
1134 user variable (if we have few registers) or it can't
1135 fit into the desired register (meaning something special
1136 is going on).
1137 Also avoid substituting a return register into I3, because
1138 reload can't handle a conflict with constraints of other
1139 inputs. */
1140 || (REGNO (src) < FIRST_PSEUDO_REGISTER
1141 && ! HARD_REGNO_MODE_OK (REGNO (src), GET_MODE (src)))))
1142 return 0;
1143 }
1144 else if (GET_CODE (dest) != CC0)
1145 return 0;
1146
1147 /* Don't substitute for a register intended as a clobberable operand.
1148 Similarly, don't substitute an expression containing a register that
1149 will be clobbered in I3. */
1150 if (GET_CODE (PATTERN (i3)) == PARALLEL)
1151 for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
1152 if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER
1153 && (reg_overlap_mentioned_p (XEXP (XVECEXP (PATTERN (i3), 0, i), 0),
1154 src)
1155 || rtx_equal_p (XEXP (XVECEXP (PATTERN (i3), 0, i), 0), dest)))
1156 return 0;
1157
1158 /* If INSN contains anything volatile, or is an `asm' (whether volatile
1159 or not), reject, unless nothing volatile comes between it and I3 */
1160
1161 if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
1162 {
1163 /* Make sure succ doesn't contain a volatile reference. */
1164 if (succ != 0 && volatile_refs_p (PATTERN (succ)))
1165 return 0;
1166
1167 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1168 if (INSN_P (p) && p != succ && volatile_refs_p (PATTERN (p)))
1169 return 0;
1170 }
1171
1172 /* If INSN is an asm, and DEST is a hard register, reject, since it has
1173 to be an explicit register variable, and was chosen for a reason. */
1174
1175 if (GET_CODE (src) == ASM_OPERANDS
1176 && GET_CODE (dest) == REG && REGNO (dest) < FIRST_PSEUDO_REGISTER)
1177 return 0;
1178
1179 /* If there are any volatile insns between INSN and I3, reject, because
1180 they might affect machine state. */
1181
1182 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1183 if (INSN_P (p) && p != succ && volatile_insn_p (PATTERN (p)))
1184 return 0;
1185
1186 /* If INSN or I2 contains an autoincrement or autodecrement,
1187 make sure that register is not used between there and I3,
1188 and not already used in I3 either.
1189 Also insist that I3 not be a jump; if it were one
1190 and the incremented register were spilled, we would lose. */
1191
1192 #ifdef AUTO_INC_DEC
1193 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1194 if (REG_NOTE_KIND (link) == REG_INC
1195 && (GET_CODE (i3) == JUMP_INSN
1196 || reg_used_between_p (XEXP (link, 0), insn, i3)
1197 || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
1198 return 0;
1199 #endif
1200
1201 #ifdef HAVE_cc0
1202 /* Don't combine an insn that follows a CC0-setting insn.
1203 An insn that uses CC0 must not be separated from the one that sets it.
1204 We do, however, allow I2 to follow a CC0-setting insn if that insn
1205 is passed as I1; in that case it will be deleted also.
1206 We also allow combining in this case if all the insns are adjacent
1207 because that would leave the two CC0 insns adjacent as well.
1208 It would be more logical to test whether CC0 occurs inside I1 or I2,
1209 but that would be much slower, and this ought to be equivalent. */
1210
1211 p = prev_nonnote_insn (insn);
1212 if (p && p != pred && GET_CODE (p) == INSN && sets_cc0_p (PATTERN (p))
1213 && ! all_adjacent)
1214 return 0;
1215 #endif
1216
1217 /* If we get here, we have passed all the tests and the combination is
1218 to be allowed. */
1219
1220 *pdest = dest;
1221 *psrc = src;
1222
1223 return 1;
1224 }
1225 \f
1226 /* Check if PAT is an insn - or a part of it - used to set up an
1227 argument for a function in a hard register. */
1228
1229 static int
1230 sets_function_arg_p (pat)
1231 rtx pat;
1232 {
1233 int i;
1234 rtx inner_dest;
1235
1236 switch (GET_CODE (pat))
1237 {
1238 case INSN:
1239 return sets_function_arg_p (PATTERN (pat));
1240
1241 case PARALLEL:
1242 for (i = XVECLEN (pat, 0); --i >= 0;)
1243 if (sets_function_arg_p (XVECEXP (pat, 0, i)))
1244 return 1;
1245
1246 break;
1247
1248 case SET:
1249 inner_dest = SET_DEST (pat);
1250 while (GET_CODE (inner_dest) == STRICT_LOW_PART
1251 || GET_CODE (inner_dest) == SUBREG
1252 || GET_CODE (inner_dest) == ZERO_EXTRACT)
1253 inner_dest = XEXP (inner_dest, 0);
1254
1255 return (GET_CODE (inner_dest) == REG
1256 && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
1257 && FUNCTION_ARG_REGNO_P (REGNO (inner_dest)));
1258
1259 default:
1260 break;
1261 }
1262
1263 return 0;
1264 }
1265
1266 /* LOC is the location within I3 that contains its pattern or the component
1267 of a PARALLEL of the pattern. We validate that it is valid for combining.
1268
1269 One problem is if I3 modifies its output, as opposed to replacing it
1270 entirely, we can't allow the output to contain I2DEST or I1DEST as doing
1271 so would produce an insn that is not equivalent to the original insns.
1272
1273 Consider:
1274
1275 (set (reg:DI 101) (reg:DI 100))
1276 (set (subreg:SI (reg:DI 101) 0) <foo>)
1277
1278 This is NOT equivalent to:
1279
1280 (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
1281 (set (reg:DI 101) (reg:DI 100))])
1282
1283 Not only does this modify 100 (in which case it might still be valid
1284 if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
1285
1286 We can also run into a problem if I2 sets a register that I1
1287 uses and I1 gets directly substituted into I3 (not via I2). In that
1288 case, we would be getting the wrong value of I2DEST into I3, so we
1289 must reject the combination. This case occurs when I2 and I1 both
1290 feed into I3, rather than when I1 feeds into I2, which feeds into I3.
1291 If I1_NOT_IN_SRC is nonzero, it means that finding I1 in the source
1292 of a SET must prevent combination from occurring.
1293
1294 Before doing the above check, we first try to expand a field assignment
1295 into a set of logical operations.
1296
1297 If PI3_DEST_KILLED is nonzero, it is a pointer to a location in which
1298 we place a register that is both set and used within I3. If more than one
1299 such register is detected, we fail.
1300
1301 Return 1 if the combination is valid, zero otherwise. */
1302
1303 static int
1304 combinable_i3pat (i3, loc, i2dest, i1dest, i1_not_in_src, pi3dest_killed)
1305 rtx i3;
1306 rtx *loc;
1307 rtx i2dest;
1308 rtx i1dest;
1309 int i1_not_in_src;
1310 rtx *pi3dest_killed;
1311 {
1312 rtx x = *loc;
1313
1314 if (GET_CODE (x) == SET)
1315 {
1316 rtx set = x ;
1317 rtx dest = SET_DEST (set);
1318 rtx src = SET_SRC (set);
1319 rtx inner_dest = dest;
1320
1321 while (GET_CODE (inner_dest) == STRICT_LOW_PART
1322 || GET_CODE (inner_dest) == SUBREG
1323 || GET_CODE (inner_dest) == ZERO_EXTRACT)
1324 inner_dest = XEXP (inner_dest, 0);
1325
1326 /* Check for the case where I3 modifies its output, as
1327 discussed above. */
1328 if ((inner_dest != dest
1329 && (reg_overlap_mentioned_p (i2dest, inner_dest)
1330 || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))))
1331
1332 /* This is the same test done in can_combine_p except we can't test
1333 all_adjacent; we don't have to, since this instruction will stay
1334 in place, thus we are not considering increasing the lifetime of
1335 INNER_DEST.
1336
1337 Also, if this insn sets a function argument, combining it with
1338 something that might need a spill could clobber a previous
1339 function argument; the all_adjacent test in can_combine_p also
1340 checks this; here, we do a more specific test for this case. */
1341
1342 || (GET_CODE (inner_dest) == REG
1343 && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
1344 && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
1345 GET_MODE (inner_dest))))
1346 || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src)))
1347 return 0;
1348
1349 /* If DEST is used in I3, it is being killed in this insn,
1350 so record that for later.
1351 Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
1352 STACK_POINTER_REGNUM, since these are always considered to be
1353 live. Similarly for ARG_POINTER_REGNUM if it is fixed. */
1354 if (pi3dest_killed && GET_CODE (dest) == REG
1355 && reg_referenced_p (dest, PATTERN (i3))
1356 && REGNO (dest) != FRAME_POINTER_REGNUM
1357 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
1358 && REGNO (dest) != HARD_FRAME_POINTER_REGNUM
1359 #endif
1360 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
1361 && (REGNO (dest) != ARG_POINTER_REGNUM
1362 || ! fixed_regs [REGNO (dest)])
1363 #endif
1364 && REGNO (dest) != STACK_POINTER_REGNUM)
1365 {
1366 if (*pi3dest_killed)
1367 return 0;
1368
1369 *pi3dest_killed = dest;
1370 }
1371 }
1372
1373 else if (GET_CODE (x) == PARALLEL)
1374 {
1375 int i;
1376
1377 for (i = 0; i < XVECLEN (x, 0); i++)
1378 if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest,
1379 i1_not_in_src, pi3dest_killed))
1380 return 0;
1381 }
1382
1383 return 1;
1384 }
1385 \f
1386 /* Return 1 if X is an arithmetic expression that contains a multiplication
1387 and division. We don't count multiplications by powers of two here. */
1388
1389 static int
1390 contains_muldiv (x)
1391 rtx x;
1392 {
1393 switch (GET_CODE (x))
1394 {
1395 case MOD: case DIV: case UMOD: case UDIV:
1396 return 1;
1397
1398 case MULT:
1399 return ! (GET_CODE (XEXP (x, 1)) == CONST_INT
1400 && exact_log2 (INTVAL (XEXP (x, 1))) >= 0);
1401 default:
1402 switch (GET_RTX_CLASS (GET_CODE (x)))
1403 {
1404 case 'c': case '<': case '2':
1405 return contains_muldiv (XEXP (x, 0))
1406 || contains_muldiv (XEXP (x, 1));
1407
1408 case '1':
1409 return contains_muldiv (XEXP (x, 0));
1410
1411 default:
1412 return 0;
1413 }
1414 }
1415 }
1416 \f
1417 /* Determine whether INSN can be used in a combination. Return nonzero if
1418 not. This is used in try_combine to detect early some cases where we
1419 can't perform combinations. */
1420
1421 static int
1422 cant_combine_insn_p (insn)
1423 rtx insn;
1424 {
1425 rtx set;
1426 rtx src, dest;
1427
1428 /* If this isn't really an insn, we can't do anything.
1429 This can occur when flow deletes an insn that it has merged into an
1430 auto-increment address. */
1431 if (! INSN_P (insn))
1432 return 1;
1433
1434 /* Never combine loads and stores involving hard regs that are likely
1435 to be spilled. The register allocator can usually handle such
1436 reg-reg moves by tying. If we allow the combiner to make
1437 substitutions of likely-spilled regs, we may abort in reload.
1438 As an exception, we allow combinations involving fixed regs; these are
1439 not available to the register allocator so there's no risk involved. */
1440
1441 set = single_set (insn);
1442 if (! set)
1443 return 0;
1444 src = SET_SRC (set);
1445 dest = SET_DEST (set);
1446 if (GET_CODE (src) == SUBREG)
1447 src = SUBREG_REG (src);
1448 if (GET_CODE (dest) == SUBREG)
1449 dest = SUBREG_REG (dest);
1450 if (REG_P (src) && REG_P (dest)
1451 && ((REGNO (src) < FIRST_PSEUDO_REGISTER
1452 && ! fixed_regs[REGNO (src)]
1453 && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (REGNO (src))))
1454 || (REGNO (dest) < FIRST_PSEUDO_REGISTER
1455 && ! fixed_regs[REGNO (dest)]
1456 && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (REGNO (dest))))))
1457 return 1;
1458
1459 return 0;
1460 }
1461
1462 /* Try to combine the insns I1 and I2 into I3.
1463 Here I1 and I2 appear earlier than I3.
1464 I1 can be zero; then we combine just I2 into I3.
1465
1466 If we are combining three insns and the resulting insn is not recognized,
1467 try splitting it into two insns. If that happens, I2 and I3 are retained
1468 and I1 is pseudo-deleted by turning it into a NOTE. Otherwise, I1 and I2
1469 are pseudo-deleted.
1470
1471 Return 0 if the combination does not work. Then nothing is changed.
1472 If we did the combination, return the insn at which combine should
1473 resume scanning.
1474
1475 Set NEW_DIRECT_JUMP_P to a nonzero value if try_combine creates a
1476 new direct jump instruction. */
1477
1478 static rtx
1479 try_combine (i3, i2, i1, new_direct_jump_p)
1480 rtx i3, i2, i1;
1481 int *new_direct_jump_p;
1482 {
1483 /* New patterns for I3 and I2, respectively. */
1484 rtx newpat, newi2pat = 0;
1485 int substed_i2 = 0, substed_i1 = 0;
1486 /* Indicates need to preserve SET in I1 or I2 in I3 if it is not dead. */
1487 int added_sets_1, added_sets_2;
1488 /* Total number of SETs to put into I3. */
1489 int total_sets;
1490 /* Nonzero is I2's body now appears in I3. */
1491 int i2_is_used;
1492 /* INSN_CODEs for new I3, new I2, and user of condition code. */
1493 int insn_code_number, i2_code_number = 0, other_code_number = 0;
1494 /* Contains I3 if the destination of I3 is used in its source, which means
1495 that the old life of I3 is being killed. If that usage is placed into
1496 I2 and not in I3, a REG_DEAD note must be made. */
1497 rtx i3dest_killed = 0;
1498 /* SET_DEST and SET_SRC of I2 and I1. */
1499 rtx i2dest, i2src, i1dest = 0, i1src = 0;
1500 /* PATTERN (I2), or a copy of it in certain cases. */
1501 rtx i2pat;
1502 /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC. */
1503 int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
1504 int i1_feeds_i3 = 0;
1505 /* Notes that must be added to REG_NOTES in I3 and I2. */
1506 rtx new_i3_notes, new_i2_notes;
1507 /* Notes that we substituted I3 into I2 instead of the normal case. */
1508 int i3_subst_into_i2 = 0;
1509 /* Notes that I1, I2 or I3 is a MULT operation. */
1510 int have_mult = 0;
1511
1512 int maxreg;
1513 rtx temp;
1514 rtx link;
1515 int i;
1516
1517 /* Exit early if one of the insns involved can't be used for
1518 combinations. */
1519 if (cant_combine_insn_p (i3)
1520 || cant_combine_insn_p (i2)
1521 || (i1 && cant_combine_insn_p (i1))
1522 /* We also can't do anything if I3 has a
1523 REG_LIBCALL note since we don't want to disrupt the contiguity of a
1524 libcall. */
1525 #if 0
1526 /* ??? This gives worse code, and appears to be unnecessary, since no
1527 pass after flow uses REG_LIBCALL/REG_RETVAL notes. */
1528 || find_reg_note (i3, REG_LIBCALL, NULL_RTX)
1529 #endif
1530 )
1531 return 0;
1532
1533 combine_attempts++;
1534 undobuf.other_insn = 0;
1535
1536 /* Reset the hard register usage information. */
1537 CLEAR_HARD_REG_SET (newpat_used_regs);
1538
1539 /* If I1 and I2 both feed I3, they can be in any order. To simplify the
1540 code below, set I1 to be the earlier of the two insns. */
1541 if (i1 && INSN_CUID (i1) > INSN_CUID (i2))
1542 temp = i1, i1 = i2, i2 = temp;
1543
1544 added_links_insn = 0;
1545
1546 /* First check for one important special-case that the code below will
1547 not handle. Namely, the case where I1 is zero, I2 is a PARALLEL
1548 and I3 is a SET whose SET_SRC is a SET_DEST in I2. In that case,
1549 we may be able to replace that destination with the destination of I3.
1550 This occurs in the common code where we compute both a quotient and
1551 remainder into a structure, in which case we want to do the computation
1552 directly into the structure to avoid register-register copies.
1553
1554 Note that this case handles both multiple sets in I2 and also
1555 cases where I2 has a number of CLOBBER or PARALLELs.
1556
1557 We make very conservative checks below and only try to handle the
1558 most common cases of this. For example, we only handle the case
1559 where I2 and I3 are adjacent to avoid making difficult register
1560 usage tests. */
1561
1562 if (i1 == 0 && GET_CODE (i3) == INSN && GET_CODE (PATTERN (i3)) == SET
1563 && GET_CODE (SET_SRC (PATTERN (i3))) == REG
1564 && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
1565 && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
1566 && GET_CODE (PATTERN (i2)) == PARALLEL
1567 && ! side_effects_p (SET_DEST (PATTERN (i3)))
1568 /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
1569 below would need to check what is inside (and reg_overlap_mentioned_p
1570 doesn't support those codes anyway). Don't allow those destinations;
1571 the resulting insn isn't likely to be recognized anyway. */
1572 && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
1573 && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
1574 && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
1575 SET_DEST (PATTERN (i3)))
1576 && next_real_insn (i2) == i3)
1577 {
1578 rtx p2 = PATTERN (i2);
1579
1580 /* Make sure that the destination of I3,
1581 which we are going to substitute into one output of I2,
1582 is not used within another output of I2. We must avoid making this:
1583 (parallel [(set (mem (reg 69)) ...)
1584 (set (reg 69) ...)])
1585 which is not well-defined as to order of actions.
1586 (Besides, reload can't handle output reloads for this.)
1587
1588 The problem can also happen if the dest of I3 is a memory ref,
1589 if another dest in I2 is an indirect memory ref. */
1590 for (i = 0; i < XVECLEN (p2, 0); i++)
1591 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
1592 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
1593 && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
1594 SET_DEST (XVECEXP (p2, 0, i))))
1595 break;
1596
1597 if (i == XVECLEN (p2, 0))
1598 for (i = 0; i < XVECLEN (p2, 0); i++)
1599 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
1600 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
1601 && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
1602 {
1603 combine_merges++;
1604
1605 subst_insn = i3;
1606 subst_low_cuid = INSN_CUID (i2);
1607
1608 added_sets_2 = added_sets_1 = 0;
1609 i2dest = SET_SRC (PATTERN (i3));
1610
1611 /* Replace the dest in I2 with our dest and make the resulting
1612 insn the new pattern for I3. Then skip to where we
1613 validate the pattern. Everything was set up above. */
1614 SUBST (SET_DEST (XVECEXP (p2, 0, i)),
1615 SET_DEST (PATTERN (i3)));
1616
1617 newpat = p2;
1618 i3_subst_into_i2 = 1;
1619 goto validate_replacement;
1620 }
1621 }
1622
1623 /* If I2 is setting a double-word pseudo to a constant and I3 is setting
1624 one of those words to another constant, merge them by making a new
1625 constant. */
1626 if (i1 == 0
1627 && (temp = single_set (i2)) != 0
1628 && (GET_CODE (SET_SRC (temp)) == CONST_INT
1629 || GET_CODE (SET_SRC (temp)) == CONST_DOUBLE)
1630 && GET_CODE (SET_DEST (temp)) == REG
1631 && GET_MODE_CLASS (GET_MODE (SET_DEST (temp))) == MODE_INT
1632 && GET_MODE_SIZE (GET_MODE (SET_DEST (temp))) == 2 * UNITS_PER_WORD
1633 && GET_CODE (PATTERN (i3)) == SET
1634 && GET_CODE (SET_DEST (PATTERN (i3))) == SUBREG
1635 && SUBREG_REG (SET_DEST (PATTERN (i3))) == SET_DEST (temp)
1636 && GET_MODE_CLASS (GET_MODE (SET_DEST (PATTERN (i3)))) == MODE_INT
1637 && GET_MODE_SIZE (GET_MODE (SET_DEST (PATTERN (i3)))) == UNITS_PER_WORD
1638 && GET_CODE (SET_SRC (PATTERN (i3))) == CONST_INT)
1639 {
1640 HOST_WIDE_INT lo, hi;
1641
1642 if (GET_CODE (SET_SRC (temp)) == CONST_INT)
1643 lo = INTVAL (SET_SRC (temp)), hi = lo < 0 ? -1 : 0;
1644 else
1645 {
1646 lo = CONST_DOUBLE_LOW (SET_SRC (temp));
1647 hi = CONST_DOUBLE_HIGH (SET_SRC (temp));
1648 }
1649
1650 if (subreg_lowpart_p (SET_DEST (PATTERN (i3))))
1651 {
1652 /* We don't handle the case of the target word being wider
1653 than a host wide int. */
1654 if (HOST_BITS_PER_WIDE_INT < BITS_PER_WORD)
1655 abort ();
1656
1657 lo &= ~(UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD (1) - 1);
1658 lo |= (INTVAL (SET_SRC (PATTERN (i3)))
1659 & (UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD (1) - 1));
1660 }
1661 else if (HOST_BITS_PER_WIDE_INT == BITS_PER_WORD)
1662 hi = INTVAL (SET_SRC (PATTERN (i3)));
1663 else if (HOST_BITS_PER_WIDE_INT >= 2 * BITS_PER_WORD)
1664 {
1665 int sign = -(int) ((unsigned HOST_WIDE_INT) lo
1666 >> (HOST_BITS_PER_WIDE_INT - 1));
1667
1668 lo &= ~ (UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD
1669 (UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD (1) - 1));
1670 lo |= (UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD
1671 (INTVAL (SET_SRC (PATTERN (i3)))));
1672 if (hi == sign)
1673 hi = lo < 0 ? -1 : 0;
1674 }
1675 else
1676 /* We don't handle the case of the higher word not fitting
1677 entirely in either hi or lo. */
1678 abort ();
1679
1680 combine_merges++;
1681 subst_insn = i3;
1682 subst_low_cuid = INSN_CUID (i2);
1683 added_sets_2 = added_sets_1 = 0;
1684 i2dest = SET_DEST (temp);
1685
1686 SUBST (SET_SRC (temp),
1687 immed_double_const (lo, hi, GET_MODE (SET_DEST (temp))));
1688
1689 newpat = PATTERN (i2);
1690 goto validate_replacement;
1691 }
1692
1693 #ifndef HAVE_cc0
1694 /* If we have no I1 and I2 looks like:
1695 (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
1696 (set Y OP)])
1697 make up a dummy I1 that is
1698 (set Y OP)
1699 and change I2 to be
1700 (set (reg:CC X) (compare:CC Y (const_int 0)))
1701
1702 (We can ignore any trailing CLOBBERs.)
1703
1704 This undoes a previous combination and allows us to match a branch-and-
1705 decrement insn. */
1706
1707 if (i1 == 0 && GET_CODE (PATTERN (i2)) == PARALLEL
1708 && XVECLEN (PATTERN (i2), 0) >= 2
1709 && GET_CODE (XVECEXP (PATTERN (i2), 0, 0)) == SET
1710 && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
1711 == MODE_CC)
1712 && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
1713 && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
1714 && GET_CODE (XVECEXP (PATTERN (i2), 0, 1)) == SET
1715 && GET_CODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 1))) == REG
1716 && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
1717 SET_SRC (XVECEXP (PATTERN (i2), 0, 1))))
1718 {
1719 for (i = XVECLEN (PATTERN (i2), 0) - 1; i >= 2; i--)
1720 if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != CLOBBER)
1721 break;
1722
1723 if (i == 1)
1724 {
1725 /* We make I1 with the same INSN_UID as I2. This gives it
1726 the same INSN_CUID for value tracking. Our fake I1 will
1727 never appear in the insn stream so giving it the same INSN_UID
1728 as I2 will not cause a problem. */
1729
1730 i1 = gen_rtx_INSN (VOIDmode, INSN_UID (i2), NULL_RTX, i2,
1731 BLOCK_FOR_INSN (i2), INSN_SCOPE (i2),
1732 XVECEXP (PATTERN (i2), 0, 1), -1, NULL_RTX,
1733 NULL_RTX);
1734
1735 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
1736 SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
1737 SET_DEST (PATTERN (i1)));
1738 }
1739 }
1740 #endif
1741
1742 /* Verify that I2 and I1 are valid for combining. */
1743 if (! can_combine_p (i2, i3, i1, NULL_RTX, &i2dest, &i2src)
1744 || (i1 && ! can_combine_p (i1, i3, NULL_RTX, i2, &i1dest, &i1src)))
1745 {
1746 undo_all ();
1747 return 0;
1748 }
1749
1750 /* Record whether I2DEST is used in I2SRC and similarly for the other
1751 cases. Knowing this will help in register status updating below. */
1752 i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
1753 i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
1754 i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
1755
1756 /* See if I1 directly feeds into I3. It does if I1DEST is not used
1757 in I2SRC. */
1758 i1_feeds_i3 = i1 && ! reg_overlap_mentioned_p (i1dest, i2src);
1759
1760 /* Ensure that I3's pattern can be the destination of combines. */
1761 if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest,
1762 i1 && i2dest_in_i1src && i1_feeds_i3,
1763 &i3dest_killed))
1764 {
1765 undo_all ();
1766 return 0;
1767 }
1768
1769 /* See if any of the insns is a MULT operation. Unless one is, we will
1770 reject a combination that is, since it must be slower. Be conservative
1771 here. */
1772 if (GET_CODE (i2src) == MULT
1773 || (i1 != 0 && GET_CODE (i1src) == MULT)
1774 || (GET_CODE (PATTERN (i3)) == SET
1775 && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
1776 have_mult = 1;
1777
1778 /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
1779 We used to do this EXCEPT in one case: I3 has a post-inc in an
1780 output operand. However, that exception can give rise to insns like
1781 mov r3,(r3)+
1782 which is a famous insn on the PDP-11 where the value of r3 used as the
1783 source was model-dependent. Avoid this sort of thing. */
1784
1785 #if 0
1786 if (!(GET_CODE (PATTERN (i3)) == SET
1787 && GET_CODE (SET_SRC (PATTERN (i3))) == REG
1788 && GET_CODE (SET_DEST (PATTERN (i3))) == MEM
1789 && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
1790 || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
1791 /* It's not the exception. */
1792 #endif
1793 #ifdef AUTO_INC_DEC
1794 for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
1795 if (REG_NOTE_KIND (link) == REG_INC
1796 && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
1797 || (i1 != 0
1798 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
1799 {
1800 undo_all ();
1801 return 0;
1802 }
1803 #endif
1804
1805 /* See if the SETs in I1 or I2 need to be kept around in the merged
1806 instruction: whenever the value set there is still needed past I3.
1807 For the SETs in I2, this is easy: we see if I2DEST dies or is set in I3.
1808
1809 For the SET in I1, we have two cases: If I1 and I2 independently
1810 feed into I3, the set in I1 needs to be kept around if I1DEST dies
1811 or is set in I3. Otherwise (if I1 feeds I2 which feeds I3), the set
1812 in I1 needs to be kept around unless I1DEST dies or is set in either
1813 I2 or I3. We can distinguish these cases by seeing if I2SRC mentions
1814 I1DEST. If so, we know I1 feeds into I2. */
1815
1816 added_sets_2 = ! dead_or_set_p (i3, i2dest);
1817
1818 added_sets_1
1819 = i1 && ! (i1_feeds_i3 ? dead_or_set_p (i3, i1dest)
1820 : (dead_or_set_p (i3, i1dest) || dead_or_set_p (i2, i1dest)));
1821
1822 /* If the set in I2 needs to be kept around, we must make a copy of
1823 PATTERN (I2), so that when we substitute I1SRC for I1DEST in
1824 PATTERN (I2), we are only substituting for the original I1DEST, not into
1825 an already-substituted copy. This also prevents making self-referential
1826 rtx. If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
1827 I2DEST. */
1828
1829 i2pat = (GET_CODE (PATTERN (i2)) == PARALLEL
1830 ? gen_rtx_SET (VOIDmode, i2dest, i2src)
1831 : PATTERN (i2));
1832
1833 if (added_sets_2)
1834 i2pat = copy_rtx (i2pat);
1835
1836 combine_merges++;
1837
1838 /* Substitute in the latest insn for the regs set by the earlier ones. */
1839
1840 maxreg = max_reg_num ();
1841
1842 subst_insn = i3;
1843
1844 /* It is possible that the source of I2 or I1 may be performing an
1845 unneeded operation, such as a ZERO_EXTEND of something that is known
1846 to have the high part zero. Handle that case by letting subst look at
1847 the innermost one of them.
1848
1849 Another way to do this would be to have a function that tries to
1850 simplify a single insn instead of merging two or more insns. We don't
1851 do this because of the potential of infinite loops and because
1852 of the potential extra memory required. However, doing it the way
1853 we are is a bit of a kludge and doesn't catch all cases.
1854
1855 But only do this if -fexpensive-optimizations since it slows things down
1856 and doesn't usually win. */
1857
1858 if (flag_expensive_optimizations)
1859 {
1860 /* Pass pc_rtx so no substitutions are done, just simplifications.
1861 The cases that we are interested in here do not involve the few
1862 cases were is_replaced is checked. */
1863 if (i1)
1864 {
1865 subst_low_cuid = INSN_CUID (i1);
1866 i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0);
1867 }
1868 else
1869 {
1870 subst_low_cuid = INSN_CUID (i2);
1871 i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0);
1872 }
1873 }
1874
1875 #ifndef HAVE_cc0
1876 /* Many machines that don't use CC0 have insns that can both perform an
1877 arithmetic operation and set the condition code. These operations will
1878 be represented as a PARALLEL with the first element of the vector
1879 being a COMPARE of an arithmetic operation with the constant zero.
1880 The second element of the vector will set some pseudo to the result
1881 of the same arithmetic operation. If we simplify the COMPARE, we won't
1882 match such a pattern and so will generate an extra insn. Here we test
1883 for this case, where both the comparison and the operation result are
1884 needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
1885 I2SRC. Later we will make the PARALLEL that contains I2. */
1886
1887 if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
1888 && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
1889 && XEXP (SET_SRC (PATTERN (i3)), 1) == const0_rtx
1890 && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
1891 {
1892 #ifdef EXTRA_CC_MODES
1893 rtx *cc_use;
1894 enum machine_mode compare_mode;
1895 #endif
1896
1897 newpat = PATTERN (i3);
1898 SUBST (XEXP (SET_SRC (newpat), 0), i2src);
1899
1900 i2_is_used = 1;
1901
1902 #ifdef EXTRA_CC_MODES
1903 /* See if a COMPARE with the operand we substituted in should be done
1904 with the mode that is currently being used. If not, do the same
1905 processing we do in `subst' for a SET; namely, if the destination
1906 is used only once, try to replace it with a register of the proper
1907 mode and also replace the COMPARE. */
1908 if (undobuf.other_insn == 0
1909 && (cc_use = find_single_use (SET_DEST (newpat), i3,
1910 &undobuf.other_insn))
1911 && ((compare_mode = SELECT_CC_MODE (GET_CODE (*cc_use),
1912 i2src, const0_rtx))
1913 != GET_MODE (SET_DEST (newpat))))
1914 {
1915 unsigned int regno = REGNO (SET_DEST (newpat));
1916 rtx new_dest = gen_rtx_REG (compare_mode, regno);
1917
1918 if (regno < FIRST_PSEUDO_REGISTER
1919 || (REG_N_SETS (regno) == 1 && ! added_sets_2
1920 && ! REG_USERVAR_P (SET_DEST (newpat))))
1921 {
1922 if (regno >= FIRST_PSEUDO_REGISTER)
1923 SUBST (regno_reg_rtx[regno], new_dest);
1924
1925 SUBST (SET_DEST (newpat), new_dest);
1926 SUBST (XEXP (*cc_use, 0), new_dest);
1927 SUBST (SET_SRC (newpat),
1928 gen_rtx_COMPARE (compare_mode, i2src, const0_rtx));
1929 }
1930 else
1931 undobuf.other_insn = 0;
1932 }
1933 #endif
1934 }
1935 else
1936 #endif
1937 {
1938 n_occurrences = 0; /* `subst' counts here */
1939
1940 /* If I1 feeds into I2 (not into I3) and I1DEST is in I1SRC, we
1941 need to make a unique copy of I2SRC each time we substitute it
1942 to avoid self-referential rtl. */
1943
1944 subst_low_cuid = INSN_CUID (i2);
1945 newpat = subst (PATTERN (i3), i2dest, i2src, 0,
1946 ! i1_feeds_i3 && i1dest_in_i1src);
1947 substed_i2 = 1;
1948
1949 /* Record whether i2's body now appears within i3's body. */
1950 i2_is_used = n_occurrences;
1951 }
1952
1953 /* If we already got a failure, don't try to do more. Otherwise,
1954 try to substitute in I1 if we have it. */
1955
1956 if (i1 && GET_CODE (newpat) != CLOBBER)
1957 {
1958 /* Before we can do this substitution, we must redo the test done
1959 above (see detailed comments there) that ensures that I1DEST
1960 isn't mentioned in any SETs in NEWPAT that are field assignments. */
1961
1962 if (! combinable_i3pat (NULL_RTX, &newpat, i1dest, NULL_RTX,
1963 0, (rtx*) 0))
1964 {
1965 undo_all ();
1966 return 0;
1967 }
1968
1969 n_occurrences = 0;
1970 subst_low_cuid = INSN_CUID (i1);
1971 newpat = subst (newpat, i1dest, i1src, 0, 0);
1972 substed_i1 = 1;
1973 }
1974
1975 /* Fail if an autoincrement side-effect has been duplicated. Be careful
1976 to count all the ways that I2SRC and I1SRC can be used. */
1977 if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
1978 && i2_is_used + added_sets_2 > 1)
1979 || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
1980 && (n_occurrences + added_sets_1 + (added_sets_2 && ! i1_feeds_i3)
1981 > 1))
1982 /* Fail if we tried to make a new register (we used to abort, but there's
1983 really no reason to). */
1984 || max_reg_num () != maxreg
1985 /* Fail if we couldn't do something and have a CLOBBER. */
1986 || GET_CODE (newpat) == CLOBBER
1987 /* Fail if this new pattern is a MULT and we didn't have one before
1988 at the outer level. */
1989 || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
1990 && ! have_mult))
1991 {
1992 undo_all ();
1993 return 0;
1994 }
1995
1996 /* If the actions of the earlier insns must be kept
1997 in addition to substituting them into the latest one,
1998 we must make a new PARALLEL for the latest insn
1999 to hold additional the SETs. */
2000
2001 if (added_sets_1 || added_sets_2)
2002 {
2003 combine_extras++;
2004
2005 if (GET_CODE (newpat) == PARALLEL)
2006 {
2007 rtvec old = XVEC (newpat, 0);
2008 total_sets = XVECLEN (newpat, 0) + added_sets_1 + added_sets_2;
2009 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
2010 memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
2011 sizeof (old->elem[0]) * old->num_elem);
2012 }
2013 else
2014 {
2015 rtx old = newpat;
2016 total_sets = 1 + added_sets_1 + added_sets_2;
2017 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
2018 XVECEXP (newpat, 0, 0) = old;
2019 }
2020
2021 if (added_sets_1)
2022 XVECEXP (newpat, 0, --total_sets)
2023 = (GET_CODE (PATTERN (i1)) == PARALLEL
2024 ? gen_rtx_SET (VOIDmode, i1dest, i1src) : PATTERN (i1));
2025
2026 if (added_sets_2)
2027 {
2028 /* If there is no I1, use I2's body as is. We used to also not do
2029 the subst call below if I2 was substituted into I3,
2030 but that could lose a simplification. */
2031 if (i1 == 0)
2032 XVECEXP (newpat, 0, --total_sets) = i2pat;
2033 else
2034 /* See comment where i2pat is assigned. */
2035 XVECEXP (newpat, 0, --total_sets)
2036 = subst (i2pat, i1dest, i1src, 0, 0);
2037 }
2038 }
2039
2040 /* We come here when we are replacing a destination in I2 with the
2041 destination of I3. */
2042 validate_replacement:
2043
2044 /* Note which hard regs this insn has as inputs. */
2045 mark_used_regs_combine (newpat);
2046
2047 /* Is the result of combination a valid instruction? */
2048 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2049
2050 /* If the result isn't valid, see if it is a PARALLEL of two SETs where
2051 the second SET's destination is a register that is unused. In that case,
2052 we just need the first SET. This can occur when simplifying a divmod
2053 insn. We *must* test for this case here because the code below that
2054 splits two independent SETs doesn't handle this case correctly when it
2055 updates the register status. Also check the case where the first
2056 SET's destination is unused. That would not cause incorrect code, but
2057 does cause an unneeded insn to remain. */
2058
2059 if (insn_code_number < 0 && GET_CODE (newpat) == PARALLEL
2060 && XVECLEN (newpat, 0) == 2
2061 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2062 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2063 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == REG
2064 && find_reg_note (i3, REG_UNUSED, SET_DEST (XVECEXP (newpat, 0, 1)))
2065 && ! side_effects_p (SET_SRC (XVECEXP (newpat, 0, 1)))
2066 && asm_noperands (newpat) < 0)
2067 {
2068 newpat = XVECEXP (newpat, 0, 0);
2069 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2070 }
2071
2072 else if (insn_code_number < 0 && GET_CODE (newpat) == PARALLEL
2073 && XVECLEN (newpat, 0) == 2
2074 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2075 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2076 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) == REG
2077 && find_reg_note (i3, REG_UNUSED, SET_DEST (XVECEXP (newpat, 0, 0)))
2078 && ! side_effects_p (SET_SRC (XVECEXP (newpat, 0, 0)))
2079 && asm_noperands (newpat) < 0)
2080 {
2081 newpat = XVECEXP (newpat, 0, 1);
2082 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2083 }
2084
2085 /* If we were combining three insns and the result is a simple SET
2086 with no ASM_OPERANDS that wasn't recognized, try to split it into two
2087 insns. There are two ways to do this. It can be split using a
2088 machine-specific method (like when you have an addition of a large
2089 constant) or by combine in the function find_split_point. */
2090
2091 if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
2092 && asm_noperands (newpat) < 0)
2093 {
2094 rtx m_split, *split;
2095 rtx ni2dest = i2dest;
2096
2097 /* See if the MD file can split NEWPAT. If it can't, see if letting it
2098 use I2DEST as a scratch register will help. In the latter case,
2099 convert I2DEST to the mode of the source of NEWPAT if we can. */
2100
2101 m_split = split_insns (newpat, i3);
2102
2103 /* We can only use I2DEST as a scratch reg if it doesn't overlap any
2104 inputs of NEWPAT. */
2105
2106 /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
2107 possible to try that as a scratch reg. This would require adding
2108 more code to make it work though. */
2109
2110 if (m_split == 0 && ! reg_overlap_mentioned_p (ni2dest, newpat))
2111 {
2112 /* If I2DEST is a hard register or the only use of a pseudo,
2113 we can change its mode. */
2114 if (GET_MODE (SET_DEST (newpat)) != GET_MODE (i2dest)
2115 && GET_MODE (SET_DEST (newpat)) != VOIDmode
2116 && GET_CODE (i2dest) == REG
2117 && (REGNO (i2dest) < FIRST_PSEUDO_REGISTER
2118 || (REG_N_SETS (REGNO (i2dest)) == 1 && ! added_sets_2
2119 && ! REG_USERVAR_P (i2dest))))
2120 ni2dest = gen_rtx_REG (GET_MODE (SET_DEST (newpat)),
2121 REGNO (i2dest));
2122
2123 m_split = split_insns (gen_rtx_PARALLEL
2124 (VOIDmode,
2125 gen_rtvec (2, newpat,
2126 gen_rtx_CLOBBER (VOIDmode,
2127 ni2dest))),
2128 i3);
2129 /* If the split with the mode-changed register didn't work, try
2130 the original register. */
2131 if (! m_split && ni2dest != i2dest)
2132 {
2133 ni2dest = i2dest;
2134 m_split = split_insns (gen_rtx_PARALLEL
2135 (VOIDmode,
2136 gen_rtvec (2, newpat,
2137 gen_rtx_CLOBBER (VOIDmode,
2138 i2dest))),
2139 i3);
2140 }
2141 }
2142
2143 if (m_split && NEXT_INSN (m_split) == NULL_RTX)
2144 {
2145 m_split = PATTERN (m_split);
2146 insn_code_number = recog_for_combine (&m_split, i3, &new_i3_notes);
2147 if (insn_code_number >= 0)
2148 newpat = m_split;
2149 }
2150 else if (m_split && NEXT_INSN (NEXT_INSN (m_split)) == NULL_RTX
2151 && (next_real_insn (i2) == i3
2152 || ! use_crosses_set_p (PATTERN (m_split), INSN_CUID (i2))))
2153 {
2154 rtx i2set, i3set;
2155 rtx newi3pat = PATTERN (NEXT_INSN (m_split));
2156 newi2pat = PATTERN (m_split);
2157
2158 i3set = single_set (NEXT_INSN (m_split));
2159 i2set = single_set (m_split);
2160
2161 /* In case we changed the mode of I2DEST, replace it in the
2162 pseudo-register table here. We can't do it above in case this
2163 code doesn't get executed and we do a split the other way. */
2164
2165 if (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
2166 SUBST (regno_reg_rtx[REGNO (i2dest)], ni2dest);
2167
2168 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2169
2170 /* If I2 or I3 has multiple SETs, we won't know how to track
2171 register status, so don't use these insns. If I2's destination
2172 is used between I2 and I3, we also can't use these insns. */
2173
2174 if (i2_code_number >= 0 && i2set && i3set
2175 && (next_real_insn (i2) == i3
2176 || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
2177 insn_code_number = recog_for_combine (&newi3pat, i3,
2178 &new_i3_notes);
2179 if (insn_code_number >= 0)
2180 newpat = newi3pat;
2181
2182 /* It is possible that both insns now set the destination of I3.
2183 If so, we must show an extra use of it. */
2184
2185 if (insn_code_number >= 0)
2186 {
2187 rtx new_i3_dest = SET_DEST (i3set);
2188 rtx new_i2_dest = SET_DEST (i2set);
2189
2190 while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
2191 || GET_CODE (new_i3_dest) == STRICT_LOW_PART
2192 || GET_CODE (new_i3_dest) == SUBREG)
2193 new_i3_dest = XEXP (new_i3_dest, 0);
2194
2195 while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
2196 || GET_CODE (new_i2_dest) == STRICT_LOW_PART
2197 || GET_CODE (new_i2_dest) == SUBREG)
2198 new_i2_dest = XEXP (new_i2_dest, 0);
2199
2200 if (GET_CODE (new_i3_dest) == REG
2201 && GET_CODE (new_i2_dest) == REG
2202 && REGNO (new_i3_dest) == REGNO (new_i2_dest))
2203 REG_N_SETS (REGNO (new_i2_dest))++;
2204 }
2205 }
2206
2207 /* If we can split it and use I2DEST, go ahead and see if that
2208 helps things be recognized. Verify that none of the registers
2209 are set between I2 and I3. */
2210 if (insn_code_number < 0 && (split = find_split_point (&newpat, i3)) != 0
2211 #ifdef HAVE_cc0
2212 && GET_CODE (i2dest) == REG
2213 #endif
2214 /* We need I2DEST in the proper mode. If it is a hard register
2215 or the only use of a pseudo, we can change its mode. */
2216 && (GET_MODE (*split) == GET_MODE (i2dest)
2217 || GET_MODE (*split) == VOIDmode
2218 || REGNO (i2dest) < FIRST_PSEUDO_REGISTER
2219 || (REG_N_SETS (REGNO (i2dest)) == 1 && ! added_sets_2
2220 && ! REG_USERVAR_P (i2dest)))
2221 && (next_real_insn (i2) == i3
2222 || ! use_crosses_set_p (*split, INSN_CUID (i2)))
2223 /* We can't overwrite I2DEST if its value is still used by
2224 NEWPAT. */
2225 && ! reg_referenced_p (i2dest, newpat))
2226 {
2227 rtx newdest = i2dest;
2228 enum rtx_code split_code = GET_CODE (*split);
2229 enum machine_mode split_mode = GET_MODE (*split);
2230
2231 /* Get NEWDEST as a register in the proper mode. We have already
2232 validated that we can do this. */
2233 if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
2234 {
2235 newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
2236
2237 if (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
2238 SUBST (regno_reg_rtx[REGNO (i2dest)], newdest);
2239 }
2240
2241 /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
2242 an ASHIFT. This can occur if it was inside a PLUS and hence
2243 appeared to be a memory address. This is a kludge. */
2244 if (split_code == MULT
2245 && GET_CODE (XEXP (*split, 1)) == CONST_INT
2246 && INTVAL (XEXP (*split, 1)) > 0
2247 && (i = exact_log2 (INTVAL (XEXP (*split, 1)))) >= 0)
2248 {
2249 SUBST (*split, gen_rtx_ASHIFT (split_mode,
2250 XEXP (*split, 0), GEN_INT (i)));
2251 /* Update split_code because we may not have a multiply
2252 anymore. */
2253 split_code = GET_CODE (*split);
2254 }
2255
2256 #ifdef INSN_SCHEDULING
2257 /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
2258 be written as a ZERO_EXTEND. */
2259 if (split_code == SUBREG && GET_CODE (SUBREG_REG (*split)) == MEM)
2260 {
2261 #ifdef LOAD_EXTEND_OP
2262 /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
2263 what it really is. */
2264 if (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (*split)))
2265 == SIGN_EXTEND)
2266 SUBST (*split, gen_rtx_SIGN_EXTEND (split_mode,
2267 SUBREG_REG (*split)));
2268 else
2269 #endif
2270 SUBST (*split, gen_rtx_ZERO_EXTEND (split_mode,
2271 SUBREG_REG (*split)));
2272 }
2273 #endif
2274
2275 newi2pat = gen_rtx_SET (VOIDmode, newdest, *split);
2276 SUBST (*split, newdest);
2277 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2278
2279 /* If the split point was a MULT and we didn't have one before,
2280 don't use one now. */
2281 if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
2282 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2283 }
2284 }
2285
2286 /* Check for a case where we loaded from memory in a narrow mode and
2287 then sign extended it, but we need both registers. In that case,
2288 we have a PARALLEL with both loads from the same memory location.
2289 We can split this into a load from memory followed by a register-register
2290 copy. This saves at least one insn, more if register allocation can
2291 eliminate the copy.
2292
2293 We cannot do this if the destination of the first assignment is a
2294 condition code register or cc0. We eliminate this case by making sure
2295 the SET_DEST and SET_SRC have the same mode.
2296
2297 We cannot do this if the destination of the second assignment is
2298 a register that we have already assumed is zero-extended. Similarly
2299 for a SUBREG of such a register. */
2300
2301 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
2302 && GET_CODE (newpat) == PARALLEL
2303 && XVECLEN (newpat, 0) == 2
2304 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2305 && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
2306 && (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0)))
2307 == GET_MODE (SET_SRC (XVECEXP (newpat, 0, 0))))
2308 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2309 && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2310 XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
2311 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2312 INSN_CUID (i2))
2313 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
2314 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
2315 && ! (temp = SET_DEST (XVECEXP (newpat, 0, 1)),
2316 (GET_CODE (temp) == REG
2317 && reg_nonzero_bits[REGNO (temp)] != 0
2318 && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
2319 && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
2320 && (reg_nonzero_bits[REGNO (temp)]
2321 != GET_MODE_MASK (word_mode))))
2322 && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
2323 && (temp = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
2324 (GET_CODE (temp) == REG
2325 && reg_nonzero_bits[REGNO (temp)] != 0
2326 && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
2327 && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
2328 && (reg_nonzero_bits[REGNO (temp)]
2329 != GET_MODE_MASK (word_mode)))))
2330 && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
2331 SET_SRC (XVECEXP (newpat, 0, 1)))
2332 && ! find_reg_note (i3, REG_UNUSED,
2333 SET_DEST (XVECEXP (newpat, 0, 0))))
2334 {
2335 rtx ni2dest;
2336
2337 newi2pat = XVECEXP (newpat, 0, 0);
2338 ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
2339 newpat = XVECEXP (newpat, 0, 1);
2340 SUBST (SET_SRC (newpat),
2341 gen_lowpart_for_combine (GET_MODE (SET_SRC (newpat)), ni2dest));
2342 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2343
2344 if (i2_code_number >= 0)
2345 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2346
2347 if (insn_code_number >= 0)
2348 {
2349 rtx insn;
2350 rtx link;
2351
2352 /* If we will be able to accept this, we have made a change to the
2353 destination of I3. This can invalidate a LOG_LINKS pointing
2354 to I3. No other part of combine.c makes such a transformation.
2355
2356 The new I3 will have a destination that was previously the
2357 destination of I1 or I2 and which was used in i2 or I3. Call
2358 distribute_links to make a LOG_LINK from the next use of
2359 that destination. */
2360
2361 PATTERN (i3) = newpat;
2362 distribute_links (gen_rtx_INSN_LIST (VOIDmode, i3, NULL_RTX));
2363
2364 /* I3 now uses what used to be its destination and which is
2365 now I2's destination. That means we need a LOG_LINK from
2366 I3 to I2. But we used to have one, so we still will.
2367
2368 However, some later insn might be using I2's dest and have
2369 a LOG_LINK pointing at I3. We must remove this link.
2370 The simplest way to remove the link is to point it at I1,
2371 which we know will be a NOTE. */
2372
2373 for (insn = NEXT_INSN (i3);
2374 insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
2375 || insn != this_basic_block->next_bb->head);
2376 insn = NEXT_INSN (insn))
2377 {
2378 if (INSN_P (insn) && reg_referenced_p (ni2dest, PATTERN (insn)))
2379 {
2380 for (link = LOG_LINKS (insn); link;
2381 link = XEXP (link, 1))
2382 if (XEXP (link, 0) == i3)
2383 XEXP (link, 0) = i1;
2384
2385 break;
2386 }
2387 }
2388 }
2389 }
2390
2391 /* Similarly, check for a case where we have a PARALLEL of two independent
2392 SETs but we started with three insns. In this case, we can do the sets
2393 as two separate insns. This case occurs when some SET allows two
2394 other insns to combine, but the destination of that SET is still live. */
2395
2396 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
2397 && GET_CODE (newpat) == PARALLEL
2398 && XVECLEN (newpat, 0) == 2
2399 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2400 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
2401 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
2402 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2403 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
2404 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
2405 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2406 INSN_CUID (i2))
2407 /* Don't pass sets with (USE (MEM ...)) dests to the following. */
2408 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != USE
2409 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != USE
2410 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
2411 XVECEXP (newpat, 0, 0))
2412 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
2413 XVECEXP (newpat, 0, 1))
2414 && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
2415 && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1)))))
2416 {
2417 /* Normally, it doesn't matter which of the two is done first,
2418 but it does if one references cc0. In that case, it has to
2419 be first. */
2420 #ifdef HAVE_cc0
2421 if (reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 0)))
2422 {
2423 newi2pat = XVECEXP (newpat, 0, 0);
2424 newpat = XVECEXP (newpat, 0, 1);
2425 }
2426 else
2427 #endif
2428 {
2429 newi2pat = XVECEXP (newpat, 0, 1);
2430 newpat = XVECEXP (newpat, 0, 0);
2431 }
2432
2433 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2434
2435 if (i2_code_number >= 0)
2436 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2437 }
2438
2439 /* If it still isn't recognized, fail and change things back the way they
2440 were. */
2441 if ((insn_code_number < 0
2442 /* Is the result a reasonable ASM_OPERANDS? */
2443 && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
2444 {
2445 undo_all ();
2446 return 0;
2447 }
2448
2449 /* If we had to change another insn, make sure it is valid also. */
2450 if (undobuf.other_insn)
2451 {
2452 rtx other_pat = PATTERN (undobuf.other_insn);
2453 rtx new_other_notes;
2454 rtx note, next;
2455
2456 CLEAR_HARD_REG_SET (newpat_used_regs);
2457
2458 other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
2459 &new_other_notes);
2460
2461 if (other_code_number < 0 && ! check_asm_operands (other_pat))
2462 {
2463 undo_all ();
2464 return 0;
2465 }
2466
2467 PATTERN (undobuf.other_insn) = other_pat;
2468
2469 /* If any of the notes in OTHER_INSN were REG_UNUSED, ensure that they
2470 are still valid. Then add any non-duplicate notes added by
2471 recog_for_combine. */
2472 for (note = REG_NOTES (undobuf.other_insn); note; note = next)
2473 {
2474 next = XEXP (note, 1);
2475
2476 if (REG_NOTE_KIND (note) == REG_UNUSED
2477 && ! reg_set_p (XEXP (note, 0), PATTERN (undobuf.other_insn)))
2478 {
2479 if (GET_CODE (XEXP (note, 0)) == REG)
2480 REG_N_DEATHS (REGNO (XEXP (note, 0)))--;
2481
2482 remove_note (undobuf.other_insn, note);
2483 }
2484 }
2485
2486 for (note = new_other_notes; note; note = XEXP (note, 1))
2487 if (GET_CODE (XEXP (note, 0)) == REG)
2488 REG_N_DEATHS (REGNO (XEXP (note, 0)))++;
2489
2490 distribute_notes (new_other_notes, undobuf.other_insn,
2491 undobuf.other_insn, NULL_RTX);
2492 }
2493 #ifdef HAVE_cc0
2494 /* If I2 is the setter CC0 and I3 is the user CC0 then check whether
2495 they are adjacent to each other or not. */
2496 {
2497 rtx p = prev_nonnote_insn (i3);
2498 if (p && p != i2 && GET_CODE (p) == INSN && newi2pat
2499 && sets_cc0_p (newi2pat))
2500 {
2501 undo_all ();
2502 return 0;
2503 }
2504 }
2505 #endif
2506
2507 /* We now know that we can do this combination. Merge the insns and
2508 update the status of registers and LOG_LINKS. */
2509
2510 {
2511 rtx i3notes, i2notes, i1notes = 0;
2512 rtx i3links, i2links, i1links = 0;
2513 rtx midnotes = 0;
2514 unsigned int regno;
2515
2516 /* Get the old REG_NOTES and LOG_LINKS from all our insns and
2517 clear them. */
2518 i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
2519 i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
2520 if (i1)
2521 i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
2522
2523 /* Ensure that we do not have something that should not be shared but
2524 occurs multiple times in the new insns. Check this by first
2525 resetting all the `used' flags and then copying anything is shared. */
2526
2527 reset_used_flags (i3notes);
2528 reset_used_flags (i2notes);
2529 reset_used_flags (i1notes);
2530 reset_used_flags (newpat);
2531 reset_used_flags (newi2pat);
2532 if (undobuf.other_insn)
2533 reset_used_flags (PATTERN (undobuf.other_insn));
2534
2535 i3notes = copy_rtx_if_shared (i3notes);
2536 i2notes = copy_rtx_if_shared (i2notes);
2537 i1notes = copy_rtx_if_shared (i1notes);
2538 newpat = copy_rtx_if_shared (newpat);
2539 newi2pat = copy_rtx_if_shared (newi2pat);
2540 if (undobuf.other_insn)
2541 reset_used_flags (PATTERN (undobuf.other_insn));
2542
2543 INSN_CODE (i3) = insn_code_number;
2544 PATTERN (i3) = newpat;
2545
2546 if (GET_CODE (i3) == CALL_INSN && CALL_INSN_FUNCTION_USAGE (i3))
2547 {
2548 rtx call_usage = CALL_INSN_FUNCTION_USAGE (i3);
2549
2550 reset_used_flags (call_usage);
2551 call_usage = copy_rtx (call_usage);
2552
2553 if (substed_i2)
2554 replace_rtx (call_usage, i2dest, i2src);
2555
2556 if (substed_i1)
2557 replace_rtx (call_usage, i1dest, i1src);
2558
2559 CALL_INSN_FUNCTION_USAGE (i3) = call_usage;
2560 }
2561
2562 if (undobuf.other_insn)
2563 INSN_CODE (undobuf.other_insn) = other_code_number;
2564
2565 /* We had one special case above where I2 had more than one set and
2566 we replaced a destination of one of those sets with the destination
2567 of I3. In that case, we have to update LOG_LINKS of insns later
2568 in this basic block. Note that this (expensive) case is rare.
2569
2570 Also, in this case, we must pretend that all REG_NOTEs for I2
2571 actually came from I3, so that REG_UNUSED notes from I2 will be
2572 properly handled. */
2573
2574 if (i3_subst_into_i2)
2575 {
2576 for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
2577 if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != USE
2578 && GET_CODE (SET_DEST (XVECEXP (PATTERN (i2), 0, i))) == REG
2579 && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
2580 && ! find_reg_note (i2, REG_UNUSED,
2581 SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
2582 for (temp = NEXT_INSN (i2);
2583 temp && (this_basic_block->next_bb == EXIT_BLOCK_PTR
2584 || this_basic_block->head != temp);
2585 temp = NEXT_INSN (temp))
2586 if (temp != i3 && INSN_P (temp))
2587 for (link = LOG_LINKS (temp); link; link = XEXP (link, 1))
2588 if (XEXP (link, 0) == i2)
2589 XEXP (link, 0) = i3;
2590
2591 if (i3notes)
2592 {
2593 rtx link = i3notes;
2594 while (XEXP (link, 1))
2595 link = XEXP (link, 1);
2596 XEXP (link, 1) = i2notes;
2597 }
2598 else
2599 i3notes = i2notes;
2600 i2notes = 0;
2601 }
2602
2603 LOG_LINKS (i3) = 0;
2604 REG_NOTES (i3) = 0;
2605 LOG_LINKS (i2) = 0;
2606 REG_NOTES (i2) = 0;
2607
2608 if (newi2pat)
2609 {
2610 INSN_CODE (i2) = i2_code_number;
2611 PATTERN (i2) = newi2pat;
2612 }
2613 else
2614 {
2615 PUT_CODE (i2, NOTE);
2616 NOTE_LINE_NUMBER (i2) = NOTE_INSN_DELETED;
2617 NOTE_SOURCE_FILE (i2) = 0;
2618 }
2619
2620 if (i1)
2621 {
2622 LOG_LINKS (i1) = 0;
2623 REG_NOTES (i1) = 0;
2624 PUT_CODE (i1, NOTE);
2625 NOTE_LINE_NUMBER (i1) = NOTE_INSN_DELETED;
2626 NOTE_SOURCE_FILE (i1) = 0;
2627 }
2628
2629 /* Get death notes for everything that is now used in either I3 or
2630 I2 and used to die in a previous insn. If we built two new
2631 patterns, move from I1 to I2 then I2 to I3 so that we get the
2632 proper movement on registers that I2 modifies. */
2633
2634 if (newi2pat)
2635 {
2636 move_deaths (newi2pat, NULL_RTX, INSN_CUID (i1), i2, &midnotes);
2637 move_deaths (newpat, newi2pat, INSN_CUID (i1), i3, &midnotes);
2638 }
2639 else
2640 move_deaths (newpat, NULL_RTX, i1 ? INSN_CUID (i1) : INSN_CUID (i2),
2641 i3, &midnotes);
2642
2643 /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3. */
2644 if (i3notes)
2645 distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL_RTX);
2646 if (i2notes)
2647 distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL_RTX);
2648 if (i1notes)
2649 distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL_RTX);
2650 if (midnotes)
2651 distribute_notes (midnotes, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX);
2652
2653 /* Distribute any notes added to I2 or I3 by recog_for_combine. We
2654 know these are REG_UNUSED and want them to go to the desired insn,
2655 so we always pass it as i3. We have not counted the notes in
2656 reg_n_deaths yet, so we need to do so now. */
2657
2658 if (newi2pat && new_i2_notes)
2659 {
2660 for (temp = new_i2_notes; temp; temp = XEXP (temp, 1))
2661 if (GET_CODE (XEXP (temp, 0)) == REG)
2662 REG_N_DEATHS (REGNO (XEXP (temp, 0)))++;
2663
2664 distribute_notes (new_i2_notes, i2, i2, NULL_RTX);
2665 }
2666
2667 if (new_i3_notes)
2668 {
2669 for (temp = new_i3_notes; temp; temp = XEXP (temp, 1))
2670 if (GET_CODE (XEXP (temp, 0)) == REG)
2671 REG_N_DEATHS (REGNO (XEXP (temp, 0)))++;
2672
2673 distribute_notes (new_i3_notes, i3, i3, NULL_RTX);
2674 }
2675
2676 /* If I3DEST was used in I3SRC, it really died in I3. We may need to
2677 put a REG_DEAD note for it somewhere. If NEWI2PAT exists and sets
2678 I3DEST, the death must be somewhere before I2, not I3. If we passed I3
2679 in that case, it might delete I2. Similarly for I2 and I1.
2680 Show an additional death due to the REG_DEAD note we make here. If
2681 we discard it in distribute_notes, we will decrement it again. */
2682
2683 if (i3dest_killed)
2684 {
2685 if (GET_CODE (i3dest_killed) == REG)
2686 REG_N_DEATHS (REGNO (i3dest_killed))++;
2687
2688 if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
2689 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i3dest_killed,
2690 NULL_RTX),
2691 NULL_RTX, i2, NULL_RTX);
2692 else
2693 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i3dest_killed,
2694 NULL_RTX),
2695 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX);
2696 }
2697
2698 if (i2dest_in_i2src)
2699 {
2700 if (GET_CODE (i2dest) == REG)
2701 REG_N_DEATHS (REGNO (i2dest))++;
2702
2703 if (newi2pat && reg_set_p (i2dest, newi2pat))
2704 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i2dest, NULL_RTX),
2705 NULL_RTX, i2, NULL_RTX);
2706 else
2707 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i2dest, NULL_RTX),
2708 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX);
2709 }
2710
2711 if (i1dest_in_i1src)
2712 {
2713 if (GET_CODE (i1dest) == REG)
2714 REG_N_DEATHS (REGNO (i1dest))++;
2715
2716 if (newi2pat && reg_set_p (i1dest, newi2pat))
2717 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i1dest, NULL_RTX),
2718 NULL_RTX, i2, NULL_RTX);
2719 else
2720 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i1dest, NULL_RTX),
2721 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX);
2722 }
2723
2724 distribute_links (i3links);
2725 distribute_links (i2links);
2726 distribute_links (i1links);
2727
2728 if (GET_CODE (i2dest) == REG)
2729 {
2730 rtx link;
2731 rtx i2_insn = 0, i2_val = 0, set;
2732
2733 /* The insn that used to set this register doesn't exist, and
2734 this life of the register may not exist either. See if one of
2735 I3's links points to an insn that sets I2DEST. If it does,
2736 that is now the last known value for I2DEST. If we don't update
2737 this and I2 set the register to a value that depended on its old
2738 contents, we will get confused. If this insn is used, thing
2739 will be set correctly in combine_instructions. */
2740
2741 for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
2742 if ((set = single_set (XEXP (link, 0))) != 0
2743 && rtx_equal_p (i2dest, SET_DEST (set)))
2744 i2_insn = XEXP (link, 0), i2_val = SET_SRC (set);
2745
2746 record_value_for_reg (i2dest, i2_insn, i2_val);
2747
2748 /* If the reg formerly set in I2 died only once and that was in I3,
2749 zero its use count so it won't make `reload' do any work. */
2750 if (! added_sets_2
2751 && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
2752 && ! i2dest_in_i2src)
2753 {
2754 regno = REGNO (i2dest);
2755 REG_N_SETS (regno)--;
2756 }
2757 }
2758
2759 if (i1 && GET_CODE (i1dest) == REG)
2760 {
2761 rtx link;
2762 rtx i1_insn = 0, i1_val = 0, set;
2763
2764 for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
2765 if ((set = single_set (XEXP (link, 0))) != 0
2766 && rtx_equal_p (i1dest, SET_DEST (set)))
2767 i1_insn = XEXP (link, 0), i1_val = SET_SRC (set);
2768
2769 record_value_for_reg (i1dest, i1_insn, i1_val);
2770
2771 regno = REGNO (i1dest);
2772 if (! added_sets_1 && ! i1dest_in_i1src)
2773 REG_N_SETS (regno)--;
2774 }
2775
2776 /* Update reg_nonzero_bits et al for any changes that may have been made
2777 to this insn. The order of set_nonzero_bits_and_sign_copies() is
2778 important. Because newi2pat can affect nonzero_bits of newpat */
2779 if (newi2pat)
2780 note_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
2781 note_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
2782
2783 /* Set new_direct_jump_p if a new return or simple jump instruction
2784 has been created.
2785
2786 If I3 is now an unconditional jump, ensure that it has a
2787 BARRIER following it since it may have initially been a
2788 conditional jump. It may also be the last nonnote insn. */
2789
2790 if (returnjump_p (i3) || any_uncondjump_p (i3))
2791 {
2792 *new_direct_jump_p = 1;
2793
2794 if ((temp = next_nonnote_insn (i3)) == NULL_RTX
2795 || GET_CODE (temp) != BARRIER)
2796 emit_barrier_after (i3);
2797 }
2798
2799 if (undobuf.other_insn != NULL_RTX
2800 && (returnjump_p (undobuf.other_insn)
2801 || any_uncondjump_p (undobuf.other_insn)))
2802 {
2803 *new_direct_jump_p = 1;
2804
2805 if ((temp = next_nonnote_insn (undobuf.other_insn)) == NULL_RTX
2806 || GET_CODE (temp) != BARRIER)
2807 emit_barrier_after (undobuf.other_insn);
2808 }
2809
2810 /* An NOOP jump does not need barrier, but it does need cleaning up
2811 of CFG. */
2812 if (GET_CODE (newpat) == SET
2813 && SET_SRC (newpat) == pc_rtx
2814 && SET_DEST (newpat) == pc_rtx)
2815 *new_direct_jump_p = 1;
2816 }
2817
2818 combine_successes++;
2819 undo_commit ();
2820
2821 if (added_links_insn
2822 && (newi2pat == 0 || INSN_CUID (added_links_insn) < INSN_CUID (i2))
2823 && INSN_CUID (added_links_insn) < INSN_CUID (i3))
2824 return added_links_insn;
2825 else
2826 return newi2pat ? i2 : i3;
2827 }
2828 \f
2829 /* Undo all the modifications recorded in undobuf. */
2830
2831 static void
2832 undo_all ()
2833 {
2834 struct undo *undo, *next;
2835
2836 for (undo = undobuf.undos; undo; undo = next)
2837 {
2838 next = undo->next;
2839 if (undo->is_int)
2840 *undo->where.i = undo->old_contents.i;
2841 else
2842 *undo->where.r = undo->old_contents.r;
2843
2844 undo->next = undobuf.frees;
2845 undobuf.frees = undo;
2846 }
2847
2848 undobuf.undos = 0;
2849 }
2850
2851 /* We've committed to accepting the changes we made. Move all
2852 of the undos to the free list. */
2853
2854 static void
2855 undo_commit ()
2856 {
2857 struct undo *undo, *next;
2858
2859 for (undo = undobuf.undos; undo; undo = next)
2860 {
2861 next = undo->next;
2862 undo->next = undobuf.frees;
2863 undobuf.frees = undo;
2864 }
2865 undobuf.undos = 0;
2866 }
2867
2868 \f
2869 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
2870 where we have an arithmetic expression and return that point. LOC will
2871 be inside INSN.
2872
2873 try_combine will call this function to see if an insn can be split into
2874 two insns. */
2875
2876 static rtx *
2877 find_split_point (loc, insn)
2878 rtx *loc;
2879 rtx insn;
2880 {
2881 rtx x = *loc;
2882 enum rtx_code code = GET_CODE (x);
2883 rtx *split;
2884 unsigned HOST_WIDE_INT len = 0;
2885 HOST_WIDE_INT pos = 0;
2886 int unsignedp = 0;
2887 rtx inner = NULL_RTX;
2888
2889 /* First special-case some codes. */
2890 switch (code)
2891 {
2892 case SUBREG:
2893 #ifdef INSN_SCHEDULING
2894 /* If we are making a paradoxical SUBREG invalid, it becomes a split
2895 point. */
2896 if (GET_CODE (SUBREG_REG (x)) == MEM)
2897 return loc;
2898 #endif
2899 return find_split_point (&SUBREG_REG (x), insn);
2900
2901 case MEM:
2902 #ifdef HAVE_lo_sum
2903 /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
2904 using LO_SUM and HIGH. */
2905 if (GET_CODE (XEXP (x, 0)) == CONST
2906 || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
2907 {
2908 SUBST (XEXP (x, 0),
2909 gen_rtx_LO_SUM (Pmode,
2910 gen_rtx_HIGH (Pmode, XEXP (x, 0)),
2911 XEXP (x, 0)));
2912 return &XEXP (XEXP (x, 0), 0);
2913 }
2914 #endif
2915
2916 /* If we have a PLUS whose second operand is a constant and the
2917 address is not valid, perhaps will can split it up using
2918 the machine-specific way to split large constants. We use
2919 the first pseudo-reg (one of the virtual regs) as a placeholder;
2920 it will not remain in the result. */
2921 if (GET_CODE (XEXP (x, 0)) == PLUS
2922 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
2923 && ! memory_address_p (GET_MODE (x), XEXP (x, 0)))
2924 {
2925 rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
2926 rtx seq = split_insns (gen_rtx_SET (VOIDmode, reg, XEXP (x, 0)),
2927 subst_insn);
2928
2929 /* This should have produced two insns, each of which sets our
2930 placeholder. If the source of the second is a valid address,
2931 we can make put both sources together and make a split point
2932 in the middle. */
2933
2934 if (seq
2935 && NEXT_INSN (seq) != NULL_RTX
2936 && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
2937 && GET_CODE (seq) == INSN
2938 && GET_CODE (PATTERN (seq)) == SET
2939 && SET_DEST (PATTERN (seq)) == reg
2940 && ! reg_mentioned_p (reg,
2941 SET_SRC (PATTERN (seq)))
2942 && GET_CODE (NEXT_INSN (seq)) == INSN
2943 && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
2944 && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
2945 && memory_address_p (GET_MODE (x),
2946 SET_SRC (PATTERN (NEXT_INSN (seq)))))
2947 {
2948 rtx src1 = SET_SRC (PATTERN (seq));
2949 rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
2950
2951 /* Replace the placeholder in SRC2 with SRC1. If we can
2952 find where in SRC2 it was placed, that can become our
2953 split point and we can replace this address with SRC2.
2954 Just try two obvious places. */
2955
2956 src2 = replace_rtx (src2, reg, src1);
2957 split = 0;
2958 if (XEXP (src2, 0) == src1)
2959 split = &XEXP (src2, 0);
2960 else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
2961 && XEXP (XEXP (src2, 0), 0) == src1)
2962 split = &XEXP (XEXP (src2, 0), 0);
2963
2964 if (split)
2965 {
2966 SUBST (XEXP (x, 0), src2);
2967 return split;
2968 }
2969 }
2970
2971 /* If that didn't work, perhaps the first operand is complex and
2972 needs to be computed separately, so make a split point there.
2973 This will occur on machines that just support REG + CONST
2974 and have a constant moved through some previous computation. */
2975
2976 else if (GET_RTX_CLASS (GET_CODE (XEXP (XEXP (x, 0), 0))) != 'o'
2977 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
2978 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (XEXP (x, 0), 0))))
2979 == 'o')))
2980 return &XEXP (XEXP (x, 0), 0);
2981 }
2982 break;
2983
2984 case SET:
2985 #ifdef HAVE_cc0
2986 /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
2987 ZERO_EXTRACT, the most likely reason why this doesn't match is that
2988 we need to put the operand into a register. So split at that
2989 point. */
2990
2991 if (SET_DEST (x) == cc0_rtx
2992 && GET_CODE (SET_SRC (x)) != COMPARE
2993 && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
2994 && GET_RTX_CLASS (GET_CODE (SET_SRC (x))) != 'o'
2995 && ! (GET_CODE (SET_SRC (x)) == SUBREG
2996 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (SET_SRC (x)))) == 'o'))
2997 return &SET_SRC (x);
2998 #endif
2999
3000 /* See if we can split SET_SRC as it stands. */
3001 split = find_split_point (&SET_SRC (x), insn);
3002 if (split && split != &SET_SRC (x))
3003 return split;
3004
3005 /* See if we can split SET_DEST as it stands. */
3006 split = find_split_point (&SET_DEST (x), insn);
3007 if (split && split != &SET_DEST (x))
3008 return split;
3009
3010 /* See if this is a bitfield assignment with everything constant. If
3011 so, this is an IOR of an AND, so split it into that. */
3012 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
3013 && (GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)))
3014 <= HOST_BITS_PER_WIDE_INT)
3015 && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT
3016 && GET_CODE (XEXP (SET_DEST (x), 2)) == CONST_INT
3017 && GET_CODE (SET_SRC (x)) == CONST_INT
3018 && ((INTVAL (XEXP (SET_DEST (x), 1))
3019 + INTVAL (XEXP (SET_DEST (x), 2)))
3020 <= GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0))))
3021 && ! side_effects_p (XEXP (SET_DEST (x), 0)))
3022 {
3023 HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
3024 unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
3025 unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x));
3026 rtx dest = XEXP (SET_DEST (x), 0);
3027 enum machine_mode mode = GET_MODE (dest);
3028 unsigned HOST_WIDE_INT mask = ((HOST_WIDE_INT) 1 << len) - 1;
3029
3030 if (BITS_BIG_ENDIAN)
3031 pos = GET_MODE_BITSIZE (mode) - len - pos;
3032
3033 if (src == mask)
3034 SUBST (SET_SRC (x),
3035 gen_binary (IOR, mode, dest, GEN_INT (src << pos)));
3036 else
3037 SUBST (SET_SRC (x),
3038 gen_binary (IOR, mode,
3039 gen_binary (AND, mode, dest,
3040 gen_int_mode (~(mask << pos),
3041 mode)),
3042 GEN_INT (src << pos)));
3043
3044 SUBST (SET_DEST (x), dest);
3045
3046 split = find_split_point (&SET_SRC (x), insn);
3047 if (split && split != &SET_SRC (x))
3048 return split;
3049 }
3050
3051 /* Otherwise, see if this is an operation that we can split into two.
3052 If so, try to split that. */
3053 code = GET_CODE (SET_SRC (x));
3054
3055 switch (code)
3056 {
3057 case AND:
3058 /* If we are AND'ing with a large constant that is only a single
3059 bit and the result is only being used in a context where we
3060 need to know if it is zero or nonzero, replace it with a bit
3061 extraction. This will avoid the large constant, which might
3062 have taken more than one insn to make. If the constant were
3063 not a valid argument to the AND but took only one insn to make,
3064 this is no worse, but if it took more than one insn, it will
3065 be better. */
3066
3067 if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
3068 && GET_CODE (XEXP (SET_SRC (x), 0)) == REG
3069 && (pos = exact_log2 (INTVAL (XEXP (SET_SRC (x), 1)))) >= 7
3070 && GET_CODE (SET_DEST (x)) == REG
3071 && (split = find_single_use (SET_DEST (x), insn, (rtx*) 0)) != 0
3072 && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
3073 && XEXP (*split, 0) == SET_DEST (x)
3074 && XEXP (*split, 1) == const0_rtx)
3075 {
3076 rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
3077 XEXP (SET_SRC (x), 0),
3078 pos, NULL_RTX, 1, 1, 0, 0);
3079 if (extraction != 0)
3080 {
3081 SUBST (SET_SRC (x), extraction);
3082 return find_split_point (loc, insn);
3083 }
3084 }
3085 break;
3086
3087 case NE:
3088 /* if STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
3089 is known to be on, this can be converted into a NEG of a shift. */
3090 if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
3091 && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
3092 && 1 <= (pos = exact_log2
3093 (nonzero_bits (XEXP (SET_SRC (x), 0),
3094 GET_MODE (XEXP (SET_SRC (x), 0))))))
3095 {
3096 enum machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
3097
3098 SUBST (SET_SRC (x),
3099 gen_rtx_NEG (mode,
3100 gen_rtx_LSHIFTRT (mode,
3101 XEXP (SET_SRC (x), 0),
3102 GEN_INT (pos))));
3103
3104 split = find_split_point (&SET_SRC (x), insn);
3105 if (split && split != &SET_SRC (x))
3106 return split;
3107 }
3108 break;
3109
3110 case SIGN_EXTEND:
3111 inner = XEXP (SET_SRC (x), 0);
3112
3113 /* We can't optimize if either mode is a partial integer
3114 mode as we don't know how many bits are significant
3115 in those modes. */
3116 if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
3117 || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
3118 break;
3119
3120 pos = 0;
3121 len = GET_MODE_BITSIZE (GET_MODE (inner));
3122 unsignedp = 0;
3123 break;
3124
3125 case SIGN_EXTRACT:
3126 case ZERO_EXTRACT:
3127 if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
3128 && GET_CODE (XEXP (SET_SRC (x), 2)) == CONST_INT)
3129 {
3130 inner = XEXP (SET_SRC (x), 0);
3131 len = INTVAL (XEXP (SET_SRC (x), 1));
3132 pos = INTVAL (XEXP (SET_SRC (x), 2));
3133
3134 if (BITS_BIG_ENDIAN)
3135 pos = GET_MODE_BITSIZE (GET_MODE (inner)) - len - pos;
3136 unsignedp = (code == ZERO_EXTRACT);
3137 }
3138 break;
3139
3140 default:
3141 break;
3142 }
3143
3144 if (len && pos >= 0 && pos + len <= GET_MODE_BITSIZE (GET_MODE (inner)))
3145 {
3146 enum machine_mode mode = GET_MODE (SET_SRC (x));
3147
3148 /* For unsigned, we have a choice of a shift followed by an
3149 AND or two shifts. Use two shifts for field sizes where the
3150 constant might be too large. We assume here that we can
3151 always at least get 8-bit constants in an AND insn, which is
3152 true for every current RISC. */
3153
3154 if (unsignedp && len <= 8)
3155 {
3156 SUBST (SET_SRC (x),
3157 gen_rtx_AND (mode,
3158 gen_rtx_LSHIFTRT
3159 (mode, gen_lowpart_for_combine (mode, inner),
3160 GEN_INT (pos)),
3161 GEN_INT (((HOST_WIDE_INT) 1 << len) - 1)));
3162
3163 split = find_split_point (&SET_SRC (x), insn);
3164 if (split && split != &SET_SRC (x))
3165 return split;
3166 }
3167 else
3168 {
3169 SUBST (SET_SRC (x),
3170 gen_rtx_fmt_ee
3171 (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
3172 gen_rtx_ASHIFT (mode,
3173 gen_lowpart_for_combine (mode, inner),
3174 GEN_INT (GET_MODE_BITSIZE (mode)
3175 - len - pos)),
3176 GEN_INT (GET_MODE_BITSIZE (mode) - len)));
3177
3178 split = find_split_point (&SET_SRC (x), insn);
3179 if (split && split != &SET_SRC (x))
3180 return split;
3181 }
3182 }
3183
3184 /* See if this is a simple operation with a constant as the second
3185 operand. It might be that this constant is out of range and hence
3186 could be used as a split point. */
3187 if ((GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '2'
3188 || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == 'c'
3189 || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '<')
3190 && CONSTANT_P (XEXP (SET_SRC (x), 1))
3191 && (GET_RTX_CLASS (GET_CODE (XEXP (SET_SRC (x), 0))) == 'o'
3192 || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
3193 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (SET_SRC (x), 0))))
3194 == 'o'))))
3195 return &XEXP (SET_SRC (x), 1);
3196
3197 /* Finally, see if this is a simple operation with its first operand
3198 not in a register. The operation might require this operand in a
3199 register, so return it as a split point. We can always do this
3200 because if the first operand were another operation, we would have
3201 already found it as a split point. */
3202 if ((GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '2'
3203 || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == 'c'
3204 || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '<'
3205 || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '1')
3206 && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
3207 return &XEXP (SET_SRC (x), 0);
3208
3209 return 0;
3210
3211 case AND:
3212 case IOR:
3213 /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
3214 it is better to write this as (not (ior A B)) so we can split it.
3215 Similarly for IOR. */
3216 if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
3217 {
3218 SUBST (*loc,
3219 gen_rtx_NOT (GET_MODE (x),
3220 gen_rtx_fmt_ee (code == IOR ? AND : IOR,
3221 GET_MODE (x),
3222 XEXP (XEXP (x, 0), 0),
3223 XEXP (XEXP (x, 1), 0))));
3224 return find_split_point (loc, insn);
3225 }
3226
3227 /* Many RISC machines have a large set of logical insns. If the
3228 second operand is a NOT, put it first so we will try to split the
3229 other operand first. */
3230 if (GET_CODE (XEXP (x, 1)) == NOT)
3231 {
3232 rtx tem = XEXP (x, 0);
3233 SUBST (XEXP (x, 0), XEXP (x, 1));
3234 SUBST (XEXP (x, 1), tem);
3235 }
3236 break;
3237
3238 default:
3239 break;
3240 }
3241
3242 /* Otherwise, select our actions depending on our rtx class. */
3243 switch (GET_RTX_CLASS (code))
3244 {
3245 case 'b': /* This is ZERO_EXTRACT and SIGN_EXTRACT. */
3246 case '3':
3247 split = find_split_point (&XEXP (x, 2), insn);
3248 if (split)
3249 return split;
3250 /* ... fall through ... */
3251 case '2':
3252 case 'c':
3253 case '<':
3254 split = find_split_point (&XEXP (x, 1), insn);
3255 if (split)
3256 return split;
3257 /* ... fall through ... */
3258 case '1':
3259 /* Some machines have (and (shift ...) ...) insns. If X is not
3260 an AND, but XEXP (X, 0) is, use it as our split point. */
3261 if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
3262 return &XEXP (x, 0);
3263
3264 split = find_split_point (&XEXP (x, 0), insn);
3265 if (split)
3266 return split;
3267 return loc;
3268 }
3269
3270 /* Otherwise, we don't have a split point. */
3271 return 0;
3272 }
3273 \f
3274 /* Throughout X, replace FROM with TO, and return the result.
3275 The result is TO if X is FROM;
3276 otherwise the result is X, but its contents may have been modified.
3277 If they were modified, a record was made in undobuf so that
3278 undo_all will (among other things) return X to its original state.
3279
3280 If the number of changes necessary is too much to record to undo,
3281 the excess changes are not made, so the result is invalid.
3282 The changes already made can still be undone.
3283 undobuf.num_undo is incremented for such changes, so by testing that
3284 the caller can tell whether the result is valid.
3285
3286 `n_occurrences' is incremented each time FROM is replaced.
3287
3288 IN_DEST is nonzero if we are processing the SET_DEST of a SET.
3289
3290 UNIQUE_COPY is nonzero if each substitution must be unique. We do this
3291 by copying if `n_occurrences' is nonzero. */
3292
3293 static rtx
3294 subst (x, from, to, in_dest, unique_copy)
3295 rtx x, from, to;
3296 int in_dest;
3297 int unique_copy;
3298 {
3299 enum rtx_code code = GET_CODE (x);
3300 enum machine_mode op0_mode = VOIDmode;
3301 const char *fmt;
3302 int len, i;
3303 rtx new;
3304
3305 /* Two expressions are equal if they are identical copies of a shared
3306 RTX or if they are both registers with the same register number
3307 and mode. */
3308
3309 #define COMBINE_RTX_EQUAL_P(X,Y) \
3310 ((X) == (Y) \
3311 || (GET_CODE (X) == REG && GET_CODE (Y) == REG \
3312 && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
3313
3314 if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
3315 {
3316 n_occurrences++;
3317 return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
3318 }
3319
3320 /* If X and FROM are the same register but different modes, they will
3321 not have been seen as equal above. However, flow.c will make a
3322 LOG_LINKS entry for that case. If we do nothing, we will try to
3323 rerecognize our original insn and, when it succeeds, we will
3324 delete the feeding insn, which is incorrect.
3325
3326 So force this insn not to match in this (rare) case. */
3327 if (! in_dest && code == REG && GET_CODE (from) == REG
3328 && REGNO (x) == REGNO (from))
3329 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
3330
3331 /* If this is an object, we are done unless it is a MEM or LO_SUM, both
3332 of which may contain things that can be combined. */
3333 if (code != MEM && code != LO_SUM && GET_RTX_CLASS (code) == 'o')
3334 return x;
3335
3336 /* It is possible to have a subexpression appear twice in the insn.
3337 Suppose that FROM is a register that appears within TO.
3338 Then, after that subexpression has been scanned once by `subst',
3339 the second time it is scanned, TO may be found. If we were
3340 to scan TO here, we would find FROM within it and create a
3341 self-referent rtl structure which is completely wrong. */
3342 if (COMBINE_RTX_EQUAL_P (x, to))
3343 return to;
3344
3345 /* Parallel asm_operands need special attention because all of the
3346 inputs are shared across the arms. Furthermore, unsharing the
3347 rtl results in recognition failures. Failure to handle this case
3348 specially can result in circular rtl.
3349
3350 Solve this by doing a normal pass across the first entry of the
3351 parallel, and only processing the SET_DESTs of the subsequent
3352 entries. Ug. */
3353
3354 if (code == PARALLEL
3355 && GET_CODE (XVECEXP (x, 0, 0)) == SET
3356 && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
3357 {
3358 new = subst (XVECEXP (x, 0, 0), from, to, 0, unique_copy);
3359
3360 /* If this substitution failed, this whole thing fails. */
3361 if (GET_CODE (new) == CLOBBER
3362 && XEXP (new, 0) == const0_rtx)
3363 return new;
3364
3365 SUBST (XVECEXP (x, 0, 0), new);
3366
3367 for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
3368 {
3369 rtx dest = SET_DEST (XVECEXP (x, 0, i));
3370
3371 if (GET_CODE (dest) != REG
3372 && GET_CODE (dest) != CC0
3373 && GET_CODE (dest) != PC)
3374 {
3375 new = subst (dest, from, to, 0, unique_copy);
3376
3377 /* If this substitution failed, this whole thing fails. */
3378 if (GET_CODE (new) == CLOBBER
3379 && XEXP (new, 0) == const0_rtx)
3380 return new;
3381
3382 SUBST (SET_DEST (XVECEXP (x, 0, i)), new);
3383 }
3384 }
3385 }
3386 else
3387 {
3388 len = GET_RTX_LENGTH (code);
3389 fmt = GET_RTX_FORMAT (code);
3390
3391 /* We don't need to process a SET_DEST that is a register, CC0,
3392 or PC, so set up to skip this common case. All other cases
3393 where we want to suppress replacing something inside a
3394 SET_SRC are handled via the IN_DEST operand. */
3395 if (code == SET
3396 && (GET_CODE (SET_DEST (x)) == REG
3397 || GET_CODE (SET_DEST (x)) == CC0
3398 || GET_CODE (SET_DEST (x)) == PC))
3399 fmt = "ie";
3400
3401 /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
3402 constant. */
3403 if (fmt[0] == 'e')
3404 op0_mode = GET_MODE (XEXP (x, 0));
3405
3406 for (i = 0; i < len; i++)
3407 {
3408 if (fmt[i] == 'E')
3409 {
3410 int j;
3411 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3412 {
3413 if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
3414 {
3415 new = (unique_copy && n_occurrences
3416 ? copy_rtx (to) : to);
3417 n_occurrences++;
3418 }
3419 else
3420 {
3421 new = subst (XVECEXP (x, i, j), from, to, 0,
3422 unique_copy);
3423
3424 /* If this substitution failed, this whole thing
3425 fails. */
3426 if (GET_CODE (new) == CLOBBER
3427 && XEXP (new, 0) == const0_rtx)
3428 return new;
3429 }
3430
3431 SUBST (XVECEXP (x, i, j), new);
3432 }
3433 }
3434 else if (fmt[i] == 'e')
3435 {
3436 /* If this is a register being set, ignore it. */
3437 new = XEXP (x, i);
3438 if (in_dest
3439 && (code == SUBREG || code == STRICT_LOW_PART
3440 || code == ZERO_EXTRACT)
3441 && i == 0
3442 && GET_CODE (new) == REG)
3443 ;
3444
3445 else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
3446 {
3447 /* In general, don't install a subreg involving two
3448 modes not tieable. It can worsen register
3449 allocation, and can even make invalid reload
3450 insns, since the reg inside may need to be copied
3451 from in the outside mode, and that may be invalid
3452 if it is an fp reg copied in integer mode.
3453
3454 We allow two exceptions to this: It is valid if
3455 it is inside another SUBREG and the mode of that
3456 SUBREG and the mode of the inside of TO is
3457 tieable and it is valid if X is a SET that copies
3458 FROM to CC0. */
3459
3460 if (GET_CODE (to) == SUBREG
3461 && ! MODES_TIEABLE_P (GET_MODE (to),
3462 GET_MODE (SUBREG_REG (to)))
3463 && ! (code == SUBREG
3464 && MODES_TIEABLE_P (GET_MODE (x),
3465 GET_MODE (SUBREG_REG (to))))
3466 #ifdef HAVE_cc0
3467 && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
3468 #endif
3469 )
3470 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
3471
3472 #ifdef CANNOT_CHANGE_MODE_CLASS
3473 if (code == SUBREG
3474 && GET_CODE (to) == REG
3475 && REGNO (to) < FIRST_PSEUDO_REGISTER
3476 && REG_CANNOT_CHANGE_MODE_P (REGNO (to),
3477 GET_MODE (to),
3478 GET_MODE (x)))
3479 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
3480 #endif
3481
3482 new = (unique_copy && n_occurrences ? copy_rtx (to) : to);
3483 n_occurrences++;
3484 }
3485 else
3486 /* If we are in a SET_DEST, suppress most cases unless we
3487 have gone inside a MEM, in which case we want to
3488 simplify the address. We assume here that things that
3489 are actually part of the destination have their inner
3490 parts in the first expression. This is true for SUBREG,
3491 STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
3492 things aside from REG and MEM that should appear in a
3493 SET_DEST. */
3494 new = subst (XEXP (x, i), from, to,
3495 (((in_dest
3496 && (code == SUBREG || code == STRICT_LOW_PART
3497 || code == ZERO_EXTRACT))
3498 || code == SET)
3499 && i == 0), unique_copy);
3500
3501 /* If we found that we will have to reject this combination,
3502 indicate that by returning the CLOBBER ourselves, rather than
3503 an expression containing it. This will speed things up as
3504 well as prevent accidents where two CLOBBERs are considered
3505 to be equal, thus producing an incorrect simplification. */
3506
3507 if (GET_CODE (new) == CLOBBER && XEXP (new, 0) == const0_rtx)
3508 return new;
3509
3510 if (GET_CODE (new) == CONST_INT && GET_CODE (x) == SUBREG)
3511 {
3512 enum machine_mode mode = GET_MODE (x);
3513
3514 x = simplify_subreg (GET_MODE (x), new,
3515 GET_MODE (SUBREG_REG (x)),
3516 SUBREG_BYTE (x));
3517 if (! x)
3518 x = gen_rtx_CLOBBER (mode, const0_rtx);
3519 }
3520 else if (GET_CODE (new) == CONST_INT
3521 && GET_CODE (x) == ZERO_EXTEND)
3522 {
3523 x = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
3524 new, GET_MODE (XEXP (x, 0)));
3525 if (! x)
3526 abort ();
3527 }
3528 else
3529 SUBST (XEXP (x, i), new);
3530 }
3531 }
3532 }
3533
3534 /* Try to simplify X. If the simplification changed the code, it is likely
3535 that further simplification will help, so loop, but limit the number
3536 of repetitions that will be performed. */
3537
3538 for (i = 0; i < 4; i++)
3539 {
3540 /* If X is sufficiently simple, don't bother trying to do anything
3541 with it. */
3542 if (code != CONST_INT && code != REG && code != CLOBBER)
3543 x = combine_simplify_rtx (x, op0_mode, i == 3, in_dest);
3544
3545 if (GET_CODE (x) == code)
3546 break;
3547
3548 code = GET_CODE (x);
3549
3550 /* We no longer know the original mode of operand 0 since we
3551 have changed the form of X) */
3552 op0_mode = VOIDmode;
3553 }
3554
3555 return x;
3556 }
3557 \f
3558 /* Simplify X, a piece of RTL. We just operate on the expression at the
3559 outer level; call `subst' to simplify recursively. Return the new
3560 expression.
3561
3562 OP0_MODE is the original mode of XEXP (x, 0); LAST is nonzero if this
3563 will be the iteration even if an expression with a code different from
3564 X is returned; IN_DEST is nonzero if we are inside a SET_DEST. */
3565
3566 static rtx
3567 combine_simplify_rtx (x, op0_mode, last, in_dest)
3568 rtx x;
3569 enum machine_mode op0_mode;
3570 int last;
3571 int in_dest;
3572 {
3573 enum rtx_code code = GET_CODE (x);
3574 enum machine_mode mode = GET_MODE (x);
3575 rtx temp;
3576 rtx reversed;
3577 int i;
3578
3579 /* If this is a commutative operation, put a constant last and a complex
3580 expression first. We don't need to do this for comparisons here. */
3581 if (GET_RTX_CLASS (code) == 'c'
3582 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
3583 {
3584 temp = XEXP (x, 0);
3585 SUBST (XEXP (x, 0), XEXP (x, 1));
3586 SUBST (XEXP (x, 1), temp);
3587 }
3588
3589 /* If this is a PLUS, MINUS, or MULT, and the first operand is the
3590 sign extension of a PLUS with a constant, reverse the order of the sign
3591 extension and the addition. Note that this not the same as the original
3592 code, but overflow is undefined for signed values. Also note that the
3593 PLUS will have been partially moved "inside" the sign-extension, so that
3594 the first operand of X will really look like:
3595 (ashiftrt (plus (ashift A C4) C5) C4).
3596 We convert this to
3597 (plus (ashiftrt (ashift A C4) C2) C4)
3598 and replace the first operand of X with that expression. Later parts
3599 of this function may simplify the expression further.
3600
3601 For example, if we start with (mult (sign_extend (plus A C1)) C2),
3602 we swap the SIGN_EXTEND and PLUS. Later code will apply the
3603 distributive law to produce (plus (mult (sign_extend X) C1) C3).
3604
3605 We do this to simplify address expressions. */
3606
3607 if ((code == PLUS || code == MINUS || code == MULT)
3608 && GET_CODE (XEXP (x, 0)) == ASHIFTRT
3609 && GET_CODE (XEXP (XEXP (x, 0), 0)) == PLUS
3610 && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ASHIFT
3611 && GET_CODE (XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 1)) == CONST_INT
3612 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3613 && XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 1) == XEXP (XEXP (x, 0), 1)
3614 && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
3615 && (temp = simplify_binary_operation (ASHIFTRT, mode,
3616 XEXP (XEXP (XEXP (x, 0), 0), 1),
3617 XEXP (XEXP (x, 0), 1))) != 0)
3618 {
3619 rtx new
3620 = simplify_shift_const (NULL_RTX, ASHIFT, mode,
3621 XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 0),
3622 INTVAL (XEXP (XEXP (x, 0), 1)));
3623
3624 new = simplify_shift_const (NULL_RTX, ASHIFTRT, mode, new,
3625 INTVAL (XEXP (XEXP (x, 0), 1)));
3626
3627 SUBST (XEXP (x, 0), gen_binary (PLUS, mode, new, temp));
3628 }
3629
3630 /* If this is a simple operation applied to an IF_THEN_ELSE, try
3631 applying it to the arms of the IF_THEN_ELSE. This often simplifies
3632 things. Check for cases where both arms are testing the same
3633 condition.
3634
3635 Don't do anything if all operands are very simple. */
3636
3637 if (((GET_RTX_CLASS (code) == '2' || GET_RTX_CLASS (code) == 'c'
3638 || GET_RTX_CLASS (code) == '<')
3639 && ((GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) != 'o'
3640 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
3641 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0))))
3642 == 'o')))
3643 || (GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) != 'o'
3644 && ! (GET_CODE (XEXP (x, 1)) == SUBREG
3645 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 1))))
3646 == 'o')))))
3647 || (GET_RTX_CLASS (code) == '1'
3648 && ((GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) != 'o'
3649 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
3650 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0))))
3651 == 'o'))))))
3652 {
3653 rtx cond, true_rtx, false_rtx;
3654
3655 cond = if_then_else_cond (x, &true_rtx, &false_rtx);
3656 if (cond != 0
3657 /* If everything is a comparison, what we have is highly unlikely
3658 to be simpler, so don't use it. */
3659 && ! (GET_RTX_CLASS (code) == '<'
3660 && (GET_RTX_CLASS (GET_CODE (true_rtx)) == '<'
3661 || GET_RTX_CLASS (GET_CODE (false_rtx)) == '<')))
3662 {
3663 rtx cop1 = const0_rtx;
3664 enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
3665
3666 if (cond_code == NE && GET_RTX_CLASS (GET_CODE (cond)) == '<')
3667 return x;
3668
3669 /* Simplify the alternative arms; this may collapse the true and
3670 false arms to store-flag values. */
3671 true_rtx = subst (true_rtx, pc_rtx, pc_rtx, 0, 0);
3672 false_rtx = subst (false_rtx, pc_rtx, pc_rtx, 0, 0);
3673
3674 /* If true_rtx and false_rtx are not general_operands, an if_then_else
3675 is unlikely to be simpler. */
3676 if (general_operand (true_rtx, VOIDmode)
3677 && general_operand (false_rtx, VOIDmode))
3678 {
3679 enum rtx_code reversed;
3680
3681 /* Restarting if we generate a store-flag expression will cause
3682 us to loop. Just drop through in this case. */
3683
3684 /* If the result values are STORE_FLAG_VALUE and zero, we can
3685 just make the comparison operation. */
3686 if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
3687 x = gen_binary (cond_code, mode, cond, cop1);
3688 else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
3689 && ((reversed = reversed_comparison_code_parts
3690 (cond_code, cond, cop1, NULL))
3691 != UNKNOWN))
3692 x = gen_binary (reversed, mode, cond, cop1);
3693
3694 /* Likewise, we can make the negate of a comparison operation
3695 if the result values are - STORE_FLAG_VALUE and zero. */
3696 else if (GET_CODE (true_rtx) == CONST_INT
3697 && INTVAL (true_rtx) == - STORE_FLAG_VALUE
3698 && false_rtx == const0_rtx)
3699 x = simplify_gen_unary (NEG, mode,
3700 gen_binary (cond_code, mode, cond,
3701 cop1),
3702 mode);
3703 else if (GET_CODE (false_rtx) == CONST_INT
3704 && INTVAL (false_rtx) == - STORE_FLAG_VALUE
3705 && true_rtx == const0_rtx
3706 && ((reversed = reversed_comparison_code_parts
3707 (cond_code, cond, cop1, NULL))
3708 != UNKNOWN))
3709 x = simplify_gen_unary (NEG, mode,
3710 gen_binary (reversed, mode,
3711 cond, cop1),
3712 mode);
3713 else
3714 return gen_rtx_IF_THEN_ELSE (mode,
3715 gen_binary (cond_code, VOIDmode,
3716 cond, cop1),
3717 true_rtx, false_rtx);
3718
3719 code = GET_CODE (x);
3720 op0_mode = VOIDmode;
3721 }
3722 }
3723 }
3724
3725 /* Try to fold this expression in case we have constants that weren't
3726 present before. */
3727 temp = 0;
3728 switch (GET_RTX_CLASS (code))
3729 {
3730 case '1':
3731 temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
3732 break;
3733 case '<':
3734 {
3735 enum machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
3736 if (cmp_mode == VOIDmode)
3737 {
3738 cmp_mode = GET_MODE (XEXP (x, 1));
3739 if (cmp_mode == VOIDmode)
3740 cmp_mode = op0_mode;
3741 }
3742 temp = simplify_relational_operation (code, cmp_mode,
3743 XEXP (x, 0), XEXP (x, 1));
3744 }
3745 #ifdef FLOAT_STORE_FLAG_VALUE
3746 if (temp != 0 && GET_MODE_CLASS (mode) == MODE_FLOAT)
3747 {
3748 if (temp == const0_rtx)
3749 temp = CONST0_RTX (mode);
3750 else
3751 temp = CONST_DOUBLE_FROM_REAL_VALUE (FLOAT_STORE_FLAG_VALUE (mode),
3752 mode);
3753 }
3754 #endif
3755 break;
3756 case 'c':
3757 case '2':
3758 temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
3759 break;
3760 case 'b':
3761 case '3':
3762 temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
3763 XEXP (x, 1), XEXP (x, 2));
3764 break;
3765 }
3766
3767 if (temp)
3768 {
3769 x = temp;
3770 code = GET_CODE (temp);
3771 op0_mode = VOIDmode;
3772 mode = GET_MODE (temp);
3773 }
3774
3775 /* First see if we can apply the inverse distributive law. */
3776 if (code == PLUS || code == MINUS
3777 || code == AND || code == IOR || code == XOR)
3778 {
3779 x = apply_distributive_law (x);
3780 code = GET_CODE (x);
3781 op0_mode = VOIDmode;
3782 }
3783
3784 /* If CODE is an associative operation not otherwise handled, see if we
3785 can associate some operands. This can win if they are constants or
3786 if they are logically related (i.e. (a & b) & a). */
3787 if ((code == PLUS || code == MINUS || code == MULT || code == DIV
3788 || code == AND || code == IOR || code == XOR
3789 || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
3790 && ((INTEGRAL_MODE_P (mode) && code != DIV)
3791 || (flag_unsafe_math_optimizations && FLOAT_MODE_P (mode))))
3792 {
3793 if (GET_CODE (XEXP (x, 0)) == code)
3794 {
3795 rtx other = XEXP (XEXP (x, 0), 0);
3796 rtx inner_op0 = XEXP (XEXP (x, 0), 1);
3797 rtx inner_op1 = XEXP (x, 1);
3798 rtx inner;
3799
3800 /* Make sure we pass the constant operand if any as the second
3801 one if this is a commutative operation. */
3802 if (CONSTANT_P (inner_op0) && GET_RTX_CLASS (code) == 'c')
3803 {
3804 rtx tem = inner_op0;
3805 inner_op0 = inner_op1;
3806 inner_op1 = tem;
3807 }
3808 inner = simplify_binary_operation (code == MINUS ? PLUS
3809 : code == DIV ? MULT
3810 : code,
3811 mode, inner_op0, inner_op1);
3812
3813 /* For commutative operations, try the other pair if that one
3814 didn't simplify. */
3815 if (inner == 0 && GET_RTX_CLASS (code) == 'c')
3816 {
3817 other = XEXP (XEXP (x, 0), 1);
3818 inner = simplify_binary_operation (code, mode,
3819 XEXP (XEXP (x, 0), 0),
3820 XEXP (x, 1));
3821 }
3822
3823 if (inner)
3824 return gen_binary (code, mode, other, inner);
3825 }
3826 }
3827
3828 /* A little bit of algebraic simplification here. */
3829 switch (code)
3830 {
3831 case MEM:
3832 /* Ensure that our address has any ASHIFTs converted to MULT in case
3833 address-recognizing predicates are called later. */
3834 temp = make_compound_operation (XEXP (x, 0), MEM);
3835 SUBST (XEXP (x, 0), temp);
3836 break;
3837
3838 case SUBREG:
3839 if (op0_mode == VOIDmode)
3840 op0_mode = GET_MODE (SUBREG_REG (x));
3841
3842 /* simplify_subreg can't use gen_lowpart_for_combine. */
3843 if (CONSTANT_P (SUBREG_REG (x))
3844 && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
3845 /* Don't call gen_lowpart_for_combine if the inner mode
3846 is VOIDmode and we cannot simplify it, as SUBREG without
3847 inner mode is invalid. */
3848 && (GET_MODE (SUBREG_REG (x)) != VOIDmode
3849 || gen_lowpart_common (mode, SUBREG_REG (x))))
3850 return gen_lowpart_for_combine (mode, SUBREG_REG (x));
3851
3852 if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
3853 break;
3854 {
3855 rtx temp;
3856 temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
3857 SUBREG_BYTE (x));
3858 if (temp)
3859 return temp;
3860 }
3861
3862 /* Don't change the mode of the MEM if that would change the meaning
3863 of the address. */
3864 if (GET_CODE (SUBREG_REG (x)) == MEM
3865 && (MEM_VOLATILE_P (SUBREG_REG (x))
3866 || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0))))
3867 return gen_rtx_CLOBBER (mode, const0_rtx);
3868
3869 /* Note that we cannot do any narrowing for non-constants since
3870 we might have been counting on using the fact that some bits were
3871 zero. We now do this in the SET. */
3872
3873 break;
3874
3875 case NOT:
3876 /* (not (plus X -1)) can become (neg X). */
3877 if (GET_CODE (XEXP (x, 0)) == PLUS
3878 && XEXP (XEXP (x, 0), 1) == constm1_rtx)
3879 return gen_rtx_NEG (mode, XEXP (XEXP (x, 0), 0));
3880
3881 /* Similarly, (not (neg X)) is (plus X -1). */
3882 if (GET_CODE (XEXP (x, 0)) == NEG)
3883 return gen_rtx_PLUS (mode, XEXP (XEXP (x, 0), 0), constm1_rtx);
3884
3885 /* (not (xor X C)) for C constant is (xor X D) with D = ~C. */
3886 if (GET_CODE (XEXP (x, 0)) == XOR
3887 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3888 && (temp = simplify_unary_operation (NOT, mode,
3889 XEXP (XEXP (x, 0), 1),
3890 mode)) != 0)
3891 return gen_binary (XOR, mode, XEXP (XEXP (x, 0), 0), temp);
3892
3893 /* (not (ashift 1 X)) is (rotate ~1 X). We used to do this for operands
3894 other than 1, but that is not valid. We could do a similar
3895 simplification for (not (lshiftrt C X)) where C is just the sign bit,
3896 but this doesn't seem common enough to bother with. */
3897 if (GET_CODE (XEXP (x, 0)) == ASHIFT
3898 && XEXP (XEXP (x, 0), 0) == const1_rtx)
3899 return gen_rtx_ROTATE (mode, simplify_gen_unary (NOT, mode,
3900 const1_rtx, mode),
3901 XEXP (XEXP (x, 0), 1));
3902
3903 if (GET_CODE (XEXP (x, 0)) == SUBREG
3904 && subreg_lowpart_p (XEXP (x, 0))
3905 && (GET_MODE_SIZE (GET_MODE (XEXP (x, 0)))
3906 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (x, 0)))))
3907 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == ASHIFT
3908 && XEXP (SUBREG_REG (XEXP (x, 0)), 0) == const1_rtx)
3909 {
3910 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (XEXP (x, 0)));
3911
3912 x = gen_rtx_ROTATE (inner_mode,
3913 simplify_gen_unary (NOT, inner_mode, const1_rtx,
3914 inner_mode),
3915 XEXP (SUBREG_REG (XEXP (x, 0)), 1));
3916 return gen_lowpart_for_combine (mode, x);
3917 }
3918
3919 /* If STORE_FLAG_VALUE is -1, (not (comparison foo bar)) can be done by
3920 reversing the comparison code if valid. */
3921 if (STORE_FLAG_VALUE == -1
3922 && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
3923 && (reversed = reversed_comparison (x, mode, XEXP (XEXP (x, 0), 0),
3924 XEXP (XEXP (x, 0), 1))))
3925 return reversed;
3926
3927 /* (not (ashiftrt foo C)) where C is the number of bits in FOO minus 1
3928 is (ge foo (const_int 0)) if STORE_FLAG_VALUE is -1, so we can
3929 perform the above simplification. */
3930
3931 if (STORE_FLAG_VALUE == -1
3932 && GET_CODE (XEXP (x, 0)) == ASHIFTRT
3933 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3934 && INTVAL (XEXP (XEXP (x, 0), 1)) == GET_MODE_BITSIZE (mode) - 1)
3935 return gen_rtx_GE (mode, XEXP (XEXP (x, 0), 0), const0_rtx);
3936
3937 /* Apply De Morgan's laws to reduce number of patterns for machines
3938 with negating logical insns (and-not, nand, etc.). If result has
3939 only one NOT, put it first, since that is how the patterns are
3940 coded. */
3941
3942 if (GET_CODE (XEXP (x, 0)) == IOR || GET_CODE (XEXP (x, 0)) == AND)
3943 {
3944 rtx in1 = XEXP (XEXP (x, 0), 0), in2 = XEXP (XEXP (x, 0), 1);
3945 enum machine_mode op_mode;
3946
3947 op_mode = GET_MODE (in1);
3948 in1 = simplify_gen_unary (NOT, op_mode, in1, op_mode);
3949
3950 op_mode = GET_MODE (in2);
3951 if (op_mode == VOIDmode)
3952 op_mode = mode;
3953 in2 = simplify_gen_unary (NOT, op_mode, in2, op_mode);
3954
3955 if (GET_CODE (in2) == NOT && GET_CODE (in1) != NOT)
3956 {
3957 rtx tem = in2;
3958 in2 = in1; in1 = tem;
3959 }
3960
3961 return gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)) == IOR ? AND : IOR,
3962 mode, in1, in2);
3963 }
3964 break;
3965
3966 case NEG:
3967 /* (neg (plus X 1)) can become (not X). */
3968 if (GET_CODE (XEXP (x, 0)) == PLUS
3969 && XEXP (XEXP (x, 0), 1) == const1_rtx)
3970 return gen_rtx_NOT (mode, XEXP (XEXP (x, 0), 0));
3971
3972 /* Similarly, (neg (not X)) is (plus X 1). */
3973 if (GET_CODE (XEXP (x, 0)) == NOT)
3974 return plus_constant (XEXP (XEXP (x, 0), 0), 1);
3975
3976 /* (neg (minus X Y)) can become (minus Y X). This transformation
3977 isn't safe for modes with signed zeros, since if X and Y are
3978 both +0, (minus Y X) is the same as (minus X Y). If the rounding
3979 mode is towards +infinity (or -infinity) then the two expressions
3980 will be rounded differently. */
3981 if (GET_CODE (XEXP (x, 0)) == MINUS
3982 && !HONOR_SIGNED_ZEROS (mode)
3983 && !HONOR_SIGN_DEPENDENT_ROUNDING (mode))
3984 return gen_binary (MINUS, mode, XEXP (XEXP (x, 0), 1),
3985 XEXP (XEXP (x, 0), 0));
3986
3987 /* (neg (plus A B)) is canonicalized to (minus (neg A) B). */
3988 if (GET_CODE (XEXP (x, 0)) == PLUS
3989 && !HONOR_SIGNED_ZEROS (mode)
3990 && !HONOR_SIGN_DEPENDENT_ROUNDING (mode))
3991 {
3992 temp = simplify_gen_unary (NEG, mode, XEXP (XEXP (x, 0), 0), mode);
3993 temp = combine_simplify_rtx (temp, mode, last, in_dest);
3994 return gen_binary (MINUS, mode, temp, XEXP (XEXP (x, 0), 1));
3995 }
3996
3997 /* (neg (mult A B)) becomes (mult (neg A) B).
3998 This works even for floating-point values. */
3999 if (GET_CODE (XEXP (x, 0)) == MULT)
4000 {
4001 temp = simplify_gen_unary (NEG, mode, XEXP (XEXP (x, 0), 0), mode);
4002 return gen_binary (MULT, mode, temp, XEXP (XEXP (x, 0), 1));
4003 }
4004
4005 /* (neg (xor A 1)) is (plus A -1) if A is known to be either 0 or 1. */
4006 if (GET_CODE (XEXP (x, 0)) == XOR && XEXP (XEXP (x, 0), 1) == const1_rtx
4007 && nonzero_bits (XEXP (XEXP (x, 0), 0), mode) == 1)
4008 return gen_binary (PLUS, mode, XEXP (XEXP (x, 0), 0), constm1_rtx);
4009
4010 /* NEG commutes with ASHIFT since it is multiplication. Only do this
4011 if we can then eliminate the NEG (e.g.,
4012 if the operand is a constant). */
4013
4014 if (GET_CODE (XEXP (x, 0)) == ASHIFT)
4015 {
4016 temp = simplify_unary_operation (NEG, mode,
4017 XEXP (XEXP (x, 0), 0), mode);
4018 if (temp)
4019 return gen_binary (ASHIFT, mode, temp, XEXP (XEXP (x, 0), 1));
4020 }
4021
4022 temp = expand_compound_operation (XEXP (x, 0));
4023
4024 /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
4025 replaced by (lshiftrt X C). This will convert
4026 (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y). */
4027
4028 if (GET_CODE (temp) == ASHIFTRT
4029 && GET_CODE (XEXP (temp, 1)) == CONST_INT
4030 && INTVAL (XEXP (temp, 1)) == GET_MODE_BITSIZE (mode) - 1)
4031 return simplify_shift_const (temp, LSHIFTRT, mode, XEXP (temp, 0),
4032 INTVAL (XEXP (temp, 1)));
4033
4034 /* If X has only a single bit that might be nonzero, say, bit I, convert
4035 (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
4036 MODE minus 1. This will convert (neg (zero_extract X 1 Y)) to
4037 (sign_extract X 1 Y). But only do this if TEMP isn't a register
4038 or a SUBREG of one since we'd be making the expression more
4039 complex if it was just a register. */
4040
4041 if (GET_CODE (temp) != REG
4042 && ! (GET_CODE (temp) == SUBREG
4043 && GET_CODE (SUBREG_REG (temp)) == REG)
4044 && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
4045 {
4046 rtx temp1 = simplify_shift_const
4047 (NULL_RTX, ASHIFTRT, mode,
4048 simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
4049 GET_MODE_BITSIZE (mode) - 1 - i),
4050 GET_MODE_BITSIZE (mode) - 1 - i);
4051
4052 /* If all we did was surround TEMP with the two shifts, we
4053 haven't improved anything, so don't use it. Otherwise,
4054 we are better off with TEMP1. */
4055 if (GET_CODE (temp1) != ASHIFTRT
4056 || GET_CODE (XEXP (temp1, 0)) != ASHIFT
4057 || XEXP (XEXP (temp1, 0), 0) != temp)
4058 return temp1;
4059 }
4060 break;
4061
4062 case TRUNCATE:
4063 /* We can't handle truncation to a partial integer mode here
4064 because we don't know the real bitsize of the partial
4065 integer mode. */
4066 if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
4067 break;
4068
4069 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4070 && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
4071 GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))))
4072 SUBST (XEXP (x, 0),
4073 force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
4074 GET_MODE_MASK (mode), NULL_RTX, 0));
4075
4076 /* (truncate:SI ({sign,zero}_extend:DI foo:SI)) == foo:SI. */
4077 if ((GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
4078 || GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
4079 && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode)
4080 return XEXP (XEXP (x, 0), 0);
4081
4082 /* (truncate:SI (OP:DI ({sign,zero}_extend:DI foo:SI))) is
4083 (OP:SI foo:SI) if OP is NEG or ABS. */
4084 if ((GET_CODE (XEXP (x, 0)) == ABS
4085 || GET_CODE (XEXP (x, 0)) == NEG)
4086 && (GET_CODE (XEXP (XEXP (x, 0), 0)) == SIGN_EXTEND
4087 || GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND)
4088 && GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == mode)
4089 return simplify_gen_unary (GET_CODE (XEXP (x, 0)), mode,
4090 XEXP (XEXP (XEXP (x, 0), 0), 0), mode);
4091
4092 /* (truncate:SI (subreg:DI (truncate:SI X) 0)) is
4093 (truncate:SI x). */
4094 if (GET_CODE (XEXP (x, 0)) == SUBREG
4095 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == TRUNCATE
4096 && subreg_lowpart_p (XEXP (x, 0)))
4097 return SUBREG_REG (XEXP (x, 0));
4098
4099 /* If we know that the value is already truncated, we can
4100 replace the TRUNCATE with a SUBREG if TRULY_NOOP_TRUNCATION
4101 is nonzero for the corresponding modes. But don't do this
4102 for an (LSHIFTRT (MULT ...)) since this will cause problems
4103 with the umulXi3_highpart patterns. */
4104 if (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
4105 GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
4106 && num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
4107 >= (unsigned int) (GET_MODE_BITSIZE (mode) + 1)
4108 && ! (GET_CODE (XEXP (x, 0)) == LSHIFTRT
4109 && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT))
4110 return gen_lowpart_for_combine (mode, XEXP (x, 0));
4111
4112 /* A truncate of a comparison can be replaced with a subreg if
4113 STORE_FLAG_VALUE permits. This is like the previous test,
4114 but it works even if the comparison is done in a mode larger
4115 than HOST_BITS_PER_WIDE_INT. */
4116 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4117 && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
4118 && ((HOST_WIDE_INT) STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0)
4119 return gen_lowpart_for_combine (mode, XEXP (x, 0));
4120
4121 /* Similarly, a truncate of a register whose value is a
4122 comparison can be replaced with a subreg if STORE_FLAG_VALUE
4123 permits. */
4124 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4125 && ((HOST_WIDE_INT) STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
4126 && (temp = get_last_value (XEXP (x, 0)))
4127 && GET_RTX_CLASS (GET_CODE (temp)) == '<')
4128 return gen_lowpart_for_combine (mode, XEXP (x, 0));
4129
4130 break;
4131
4132 case FLOAT_TRUNCATE:
4133 /* (float_truncate:SF (float_extend:DF foo:SF)) = foo:SF. */
4134 if (GET_CODE (XEXP (x, 0)) == FLOAT_EXTEND
4135 && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode)
4136 return XEXP (XEXP (x, 0), 0);
4137
4138 /* (float_truncate:SF (float_truncate:DF foo:XF))
4139 = (float_truncate:SF foo:XF).
4140 This may elliminate double rounding, so it is unsafe.
4141
4142 (float_truncate:SF (float_extend:XF foo:DF))
4143 = (float_truncate:SF foo:DF).
4144
4145 (float_truncate:DF (float_extend:XF foo:SF))
4146 = (float_extend:SF foo:DF). */
4147 if ((GET_CODE (XEXP (x, 0)) == FLOAT_TRUNCATE
4148 && flag_unsafe_math_optimizations)
4149 || GET_CODE (XEXP (x, 0)) == FLOAT_EXTEND)
4150 return simplify_gen_unary (GET_MODE_SIZE (GET_MODE (XEXP (XEXP (x, 0),
4151 0)))
4152 > GET_MODE_SIZE (mode)
4153 ? FLOAT_TRUNCATE : FLOAT_EXTEND,
4154 mode,
4155 XEXP (XEXP (x, 0), 0), mode);
4156
4157 /* (float_truncate (float x)) is (float x) */
4158 if (GET_CODE (XEXP (x, 0)) == FLOAT
4159 && (flag_unsafe_math_optimizations
4160 || ((unsigned)significand_size (GET_MODE (XEXP (x, 0)))
4161 >= (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (x, 0), 0)))
4162 - num_sign_bit_copies (XEXP (XEXP (x, 0), 0),
4163 GET_MODE (XEXP (XEXP (x, 0), 0)))))))
4164 return simplify_gen_unary (FLOAT, mode,
4165 XEXP (XEXP (x, 0), 0),
4166 GET_MODE (XEXP (XEXP (x, 0), 0)));
4167
4168 /* (float_truncate:SF (OP:DF (float_extend:DF foo:sf))) is
4169 (OP:SF foo:SF) if OP is NEG or ABS. */
4170 if ((GET_CODE (XEXP (x, 0)) == ABS
4171 || GET_CODE (XEXP (x, 0)) == NEG)
4172 && GET_CODE (XEXP (XEXP (x, 0), 0)) == FLOAT_EXTEND
4173 && GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == mode)
4174 return simplify_gen_unary (GET_CODE (XEXP (x, 0)), mode,
4175 XEXP (XEXP (XEXP (x, 0), 0), 0), mode);
4176
4177 /* (float_truncate:SF (subreg:DF (float_truncate:SF X) 0))
4178 is (float_truncate:SF x). */
4179 if (GET_CODE (XEXP (x, 0)) == SUBREG
4180 && subreg_lowpart_p (XEXP (x, 0))
4181 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == FLOAT_TRUNCATE)
4182 return SUBREG_REG (XEXP (x, 0));
4183 break;
4184 case FLOAT_EXTEND:
4185 /* (float_extend (float_extend x)) is (float_extend x)
4186
4187 (float_extend (float x)) is (float x) assuming that double
4188 rounding can't happen.
4189 */
4190 if (GET_CODE (XEXP (x, 0)) == FLOAT_EXTEND
4191 || (GET_CODE (XEXP (x, 0)) == FLOAT
4192 && ((unsigned)significand_size (GET_MODE (XEXP (x, 0)))
4193 >= (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (x, 0), 0)))
4194 - num_sign_bit_copies (XEXP (XEXP (x, 0), 0),
4195 GET_MODE (XEXP (XEXP (x, 0), 0)))))))
4196 return simplify_gen_unary (GET_CODE (XEXP (x, 0)), mode,
4197 XEXP (XEXP (x, 0), 0),
4198 GET_MODE (XEXP (XEXP (x, 0), 0)));
4199
4200 break;
4201 #ifdef HAVE_cc0
4202 case COMPARE:
4203 /* Convert (compare FOO (const_int 0)) to FOO unless we aren't
4204 using cc0, in which case we want to leave it as a COMPARE
4205 so we can distinguish it from a register-register-copy. */
4206 if (XEXP (x, 1) == const0_rtx)
4207 return XEXP (x, 0);
4208
4209 /* x - 0 is the same as x unless x's mode has signed zeros and
4210 allows rounding towards -infinity. Under those conditions,
4211 0 - 0 is -0. */
4212 if (!(HONOR_SIGNED_ZEROS (GET_MODE (XEXP (x, 0)))
4213 && HONOR_SIGN_DEPENDENT_ROUNDING (GET_MODE (XEXP (x, 0))))
4214 && XEXP (x, 1) == CONST0_RTX (GET_MODE (XEXP (x, 0))))
4215 return XEXP (x, 0);
4216 break;
4217 #endif
4218
4219 case CONST:
4220 /* (const (const X)) can become (const X). Do it this way rather than
4221 returning the inner CONST since CONST can be shared with a
4222 REG_EQUAL note. */
4223 if (GET_CODE (XEXP (x, 0)) == CONST)
4224 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4225 break;
4226
4227 #ifdef HAVE_lo_sum
4228 case LO_SUM:
4229 /* Convert (lo_sum (high FOO) FOO) to FOO. This is necessary so we
4230 can add in an offset. find_split_point will split this address up
4231 again if it doesn't match. */
4232 if (GET_CODE (XEXP (x, 0)) == HIGH
4233 && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
4234 return XEXP (x, 1);
4235 break;
4236 #endif
4237
4238 case PLUS:
4239 /* Canonicalize (plus (mult (neg B) C) A) to (minus A (mult B C)).
4240 */
4241 if (GET_CODE (XEXP (x, 0)) == MULT
4242 && GET_CODE (XEXP (XEXP (x, 0), 0)) == NEG)
4243 {
4244 rtx in1, in2;
4245
4246 in1 = XEXP (XEXP (XEXP (x, 0), 0), 0);
4247 in2 = XEXP (XEXP (x, 0), 1);
4248 return gen_binary (MINUS, mode, XEXP (x, 1),
4249 gen_binary (MULT, mode, in1, in2));
4250 }
4251
4252 /* If we have (plus (plus (A const) B)), associate it so that CONST is
4253 outermost. That's because that's the way indexed addresses are
4254 supposed to appear. This code used to check many more cases, but
4255 they are now checked elsewhere. */
4256 if (GET_CODE (XEXP (x, 0)) == PLUS
4257 && CONSTANT_ADDRESS_P (XEXP (XEXP (x, 0), 1)))
4258 return gen_binary (PLUS, mode,
4259 gen_binary (PLUS, mode, XEXP (XEXP (x, 0), 0),
4260 XEXP (x, 1)),
4261 XEXP (XEXP (x, 0), 1));
4262
4263 /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
4264 when c is (const_int (pow2 + 1) / 2) is a sign extension of a
4265 bit-field and can be replaced by either a sign_extend or a
4266 sign_extract. The `and' may be a zero_extend and the two
4267 <c>, -<c> constants may be reversed. */
4268 if (GET_CODE (XEXP (x, 0)) == XOR
4269 && GET_CODE (XEXP (x, 1)) == CONST_INT
4270 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
4271 && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
4272 && ((i = exact_log2 (INTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
4273 || (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
4274 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4275 && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
4276 && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
4277 && (INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
4278 == ((HOST_WIDE_INT) 1 << (i + 1)) - 1))
4279 || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
4280 && (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
4281 == (unsigned int) i + 1))))
4282 return simplify_shift_const
4283 (NULL_RTX, ASHIFTRT, mode,
4284 simplify_shift_const (NULL_RTX, ASHIFT, mode,
4285 XEXP (XEXP (XEXP (x, 0), 0), 0),
4286 GET_MODE_BITSIZE (mode) - (i + 1)),
4287 GET_MODE_BITSIZE (mode) - (i + 1));
4288
4289 /* (plus (comparison A B) C) can become (neg (rev-comp A B)) if
4290 C is 1 and STORE_FLAG_VALUE is -1 or if C is -1 and STORE_FLAG_VALUE
4291 is 1. This produces better code than the alternative immediately
4292 below. */
4293 if (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
4294 && ((STORE_FLAG_VALUE == -1 && XEXP (x, 1) == const1_rtx)
4295 || (STORE_FLAG_VALUE == 1 && XEXP (x, 1) == constm1_rtx))
4296 && (reversed = reversed_comparison (XEXP (x, 0), mode,
4297 XEXP (XEXP (x, 0), 0),
4298 XEXP (XEXP (x, 0), 1))))
4299 return
4300 simplify_gen_unary (NEG, mode, reversed, mode);
4301
4302 /* If only the low-order bit of X is possibly nonzero, (plus x -1)
4303 can become (ashiftrt (ashift (xor x 1) C) C) where C is
4304 the bitsize of the mode - 1. This allows simplification of
4305 "a = (b & 8) == 0;" */
4306 if (XEXP (x, 1) == constm1_rtx
4307 && GET_CODE (XEXP (x, 0)) != REG
4308 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
4309 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG)
4310 && nonzero_bits (XEXP (x, 0), mode) == 1)
4311 return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
4312 simplify_shift_const (NULL_RTX, ASHIFT, mode,
4313 gen_rtx_XOR (mode, XEXP (x, 0), const1_rtx),
4314 GET_MODE_BITSIZE (mode) - 1),
4315 GET_MODE_BITSIZE (mode) - 1);
4316
4317 /* If we are adding two things that have no bits in common, convert
4318 the addition into an IOR. This will often be further simplified,
4319 for example in cases like ((a & 1) + (a & 2)), which can
4320 become a & 3. */
4321
4322 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4323 && (nonzero_bits (XEXP (x, 0), mode)
4324 & nonzero_bits (XEXP (x, 1), mode)) == 0)
4325 {
4326 /* Try to simplify the expression further. */
4327 rtx tor = gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
4328 temp = combine_simplify_rtx (tor, mode, last, in_dest);
4329
4330 /* If we could, great. If not, do not go ahead with the IOR
4331 replacement, since PLUS appears in many special purpose
4332 address arithmetic instructions. */
4333 if (GET_CODE (temp) != CLOBBER && temp != tor)
4334 return temp;
4335 }
4336 break;
4337
4338 case MINUS:
4339 /* If STORE_FLAG_VALUE is 1, (minus 1 (comparison foo bar)) can be done
4340 by reversing the comparison code if valid. */
4341 if (STORE_FLAG_VALUE == 1
4342 && XEXP (x, 0) == const1_rtx
4343 && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) == '<'
4344 && (reversed = reversed_comparison (XEXP (x, 1), mode,
4345 XEXP (XEXP (x, 1), 0),
4346 XEXP (XEXP (x, 1), 1))))
4347 return reversed;
4348
4349 /* (minus <foo> (and <foo> (const_int -pow2))) becomes
4350 (and <foo> (const_int pow2-1)) */
4351 if (GET_CODE (XEXP (x, 1)) == AND
4352 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
4353 && exact_log2 (-INTVAL (XEXP (XEXP (x, 1), 1))) >= 0
4354 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
4355 return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
4356 -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
4357
4358 /* Canonicalize (minus A (mult (neg B) C)) to (plus (mult B C) A).
4359 */
4360 if (GET_CODE (XEXP (x, 1)) == MULT
4361 && GET_CODE (XEXP (XEXP (x, 1), 0)) == NEG)
4362 {
4363 rtx in1, in2;
4364
4365 in1 = XEXP (XEXP (XEXP (x, 1), 0), 0);
4366 in2 = XEXP (XEXP (x, 1), 1);
4367 return gen_binary (PLUS, mode, gen_binary (MULT, mode, in1, in2),
4368 XEXP (x, 0));
4369 }
4370
4371 /* Canonicalize (minus (neg A) (mult B C)) to
4372 (minus (mult (neg B) C) A). */
4373 if (GET_CODE (XEXP (x, 1)) == MULT
4374 && GET_CODE (XEXP (x, 0)) == NEG)
4375 {
4376 rtx in1, in2;
4377
4378 in1 = simplify_gen_unary (NEG, mode, XEXP (XEXP (x, 1), 0), mode);
4379 in2 = XEXP (XEXP (x, 1), 1);
4380 return gen_binary (MINUS, mode, gen_binary (MULT, mode, in1, in2),
4381 XEXP (XEXP (x, 0), 0));
4382 }
4383
4384 /* Canonicalize (minus A (plus B C)) to (minus (minus A B) C) for
4385 integers. */
4386 if (GET_CODE (XEXP (x, 1)) == PLUS && INTEGRAL_MODE_P (mode))
4387 return gen_binary (MINUS, mode,
4388 gen_binary (MINUS, mode, XEXP (x, 0),
4389 XEXP (XEXP (x, 1), 0)),
4390 XEXP (XEXP (x, 1), 1));
4391 break;
4392
4393 case MULT:
4394 /* If we have (mult (plus A B) C), apply the distributive law and then
4395 the inverse distributive law to see if things simplify. This
4396 occurs mostly in addresses, often when unrolling loops. */
4397
4398 if (GET_CODE (XEXP (x, 0)) == PLUS)
4399 {
4400 x = apply_distributive_law
4401 (gen_binary (PLUS, mode,
4402 gen_binary (MULT, mode,
4403 XEXP (XEXP (x, 0), 0), XEXP (x, 1)),
4404 gen_binary (MULT, mode,
4405 XEXP (XEXP (x, 0), 1),
4406 copy_rtx (XEXP (x, 1)))));
4407
4408 if (GET_CODE (x) != MULT)
4409 return x;
4410 }
4411 /* Try simplify a*(b/c) as (a*b)/c. */
4412 if (FLOAT_MODE_P (mode) && flag_unsafe_math_optimizations
4413 && GET_CODE (XEXP (x, 0)) == DIV)
4414 {
4415 rtx tem = simplify_binary_operation (MULT, mode,
4416 XEXP (XEXP (x, 0), 0),
4417 XEXP (x, 1));
4418 if (tem)
4419 return gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
4420 }
4421 break;
4422
4423 case UDIV:
4424 /* If this is a divide by a power of two, treat it as a shift if
4425 its first operand is a shift. */
4426 if (GET_CODE (XEXP (x, 1)) == CONST_INT
4427 && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0
4428 && (GET_CODE (XEXP (x, 0)) == ASHIFT
4429 || GET_CODE (XEXP (x, 0)) == LSHIFTRT
4430 || GET_CODE (XEXP (x, 0)) == ASHIFTRT
4431 || GET_CODE (XEXP (x, 0)) == ROTATE
4432 || GET_CODE (XEXP (x, 0)) == ROTATERT))
4433 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
4434 break;
4435
4436 case EQ: case NE:
4437 case GT: case GTU: case GE: case GEU:
4438 case LT: case LTU: case LE: case LEU:
4439 case UNEQ: case LTGT:
4440 case UNGT: case UNGE:
4441 case UNLT: case UNLE:
4442 case UNORDERED: case ORDERED:
4443 /* If the first operand is a condition code, we can't do anything
4444 with it. */
4445 if (GET_CODE (XEXP (x, 0)) == COMPARE
4446 || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
4447 && ! CC0_P (XEXP (x, 0))))
4448 {
4449 rtx op0 = XEXP (x, 0);
4450 rtx op1 = XEXP (x, 1);
4451 enum rtx_code new_code;
4452
4453 if (GET_CODE (op0) == COMPARE)
4454 op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
4455
4456 /* Simplify our comparison, if possible. */
4457 new_code = simplify_comparison (code, &op0, &op1);
4458
4459 /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
4460 if only the low-order bit is possibly nonzero in X (such as when
4461 X is a ZERO_EXTRACT of one bit). Similarly, we can convert EQ to
4462 (xor X 1) or (minus 1 X); we use the former. Finally, if X is
4463 known to be either 0 or -1, NE becomes a NEG and EQ becomes
4464 (plus X 1).
4465
4466 Remove any ZERO_EXTRACT we made when thinking this was a
4467 comparison. It may now be simpler to use, e.g., an AND. If a
4468 ZERO_EXTRACT is indeed appropriate, it will be placed back by
4469 the call to make_compound_operation in the SET case. */
4470
4471 if (STORE_FLAG_VALUE == 1
4472 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4473 && op1 == const0_rtx
4474 && mode == GET_MODE (op0)
4475 && nonzero_bits (op0, mode) == 1)
4476 return gen_lowpart_for_combine (mode,
4477 expand_compound_operation (op0));
4478
4479 else if (STORE_FLAG_VALUE == 1
4480 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4481 && op1 == const0_rtx
4482 && mode == GET_MODE (op0)
4483 && (num_sign_bit_copies (op0, mode)
4484 == GET_MODE_BITSIZE (mode)))
4485 {
4486 op0 = expand_compound_operation (op0);
4487 return simplify_gen_unary (NEG, mode,
4488 gen_lowpart_for_combine (mode, op0),
4489 mode);
4490 }
4491
4492 else if (STORE_FLAG_VALUE == 1
4493 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4494 && op1 == const0_rtx
4495 && mode == GET_MODE (op0)
4496 && nonzero_bits (op0, mode) == 1)
4497 {
4498 op0 = expand_compound_operation (op0);
4499 return gen_binary (XOR, mode,
4500 gen_lowpart_for_combine (mode, op0),
4501 const1_rtx);
4502 }
4503
4504 else if (STORE_FLAG_VALUE == 1
4505 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4506 && op1 == const0_rtx
4507 && mode == GET_MODE (op0)
4508 && (num_sign_bit_copies (op0, mode)
4509 == GET_MODE_BITSIZE (mode)))
4510 {
4511 op0 = expand_compound_operation (op0);
4512 return plus_constant (gen_lowpart_for_combine (mode, op0), 1);
4513 }
4514
4515 /* If STORE_FLAG_VALUE is -1, we have cases similar to
4516 those above. */
4517 if (STORE_FLAG_VALUE == -1
4518 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4519 && op1 == const0_rtx
4520 && (num_sign_bit_copies (op0, mode)
4521 == GET_MODE_BITSIZE (mode)))
4522 return gen_lowpart_for_combine (mode,
4523 expand_compound_operation (op0));
4524
4525 else if (STORE_FLAG_VALUE == -1
4526 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4527 && op1 == const0_rtx
4528 && mode == GET_MODE (op0)
4529 && nonzero_bits (op0, mode) == 1)
4530 {
4531 op0 = expand_compound_operation (op0);
4532 return simplify_gen_unary (NEG, mode,
4533 gen_lowpart_for_combine (mode, op0),
4534 mode);
4535 }
4536
4537 else if (STORE_FLAG_VALUE == -1
4538 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4539 && op1 == const0_rtx
4540 && mode == GET_MODE (op0)
4541 && (num_sign_bit_copies (op0, mode)
4542 == GET_MODE_BITSIZE (mode)))
4543 {
4544 op0 = expand_compound_operation (op0);
4545 return simplify_gen_unary (NOT, mode,
4546 gen_lowpart_for_combine (mode, op0),
4547 mode);
4548 }
4549
4550 /* If X is 0/1, (eq X 0) is X-1. */
4551 else if (STORE_FLAG_VALUE == -1
4552 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4553 && op1 == const0_rtx
4554 && mode == GET_MODE (op0)
4555 && nonzero_bits (op0, mode) == 1)
4556 {
4557 op0 = expand_compound_operation (op0);
4558 return plus_constant (gen_lowpart_for_combine (mode, op0), -1);
4559 }
4560
4561 /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
4562 one bit that might be nonzero, we can convert (ne x 0) to
4563 (ashift x c) where C puts the bit in the sign bit. Remove any
4564 AND with STORE_FLAG_VALUE when we are done, since we are only
4565 going to test the sign bit. */
4566 if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4567 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4568 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
4569 == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
4570 && op1 == const0_rtx
4571 && mode == GET_MODE (op0)
4572 && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
4573 {
4574 x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
4575 expand_compound_operation (op0),
4576 GET_MODE_BITSIZE (mode) - 1 - i);
4577 if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
4578 return XEXP (x, 0);
4579 else
4580 return x;
4581 }
4582
4583 /* If the code changed, return a whole new comparison. */
4584 if (new_code != code)
4585 return gen_rtx_fmt_ee (new_code, mode, op0, op1);
4586
4587 /* Otherwise, keep this operation, but maybe change its operands.
4588 This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR). */
4589 SUBST (XEXP (x, 0), op0);
4590 SUBST (XEXP (x, 1), op1);
4591 }
4592 break;
4593
4594 case IF_THEN_ELSE:
4595 return simplify_if_then_else (x);
4596
4597 case ZERO_EXTRACT:
4598 case SIGN_EXTRACT:
4599 case ZERO_EXTEND:
4600 case SIGN_EXTEND:
4601 /* If we are processing SET_DEST, we are done. */
4602 if (in_dest)
4603 return x;
4604
4605 return expand_compound_operation (x);
4606
4607 case SET:
4608 return simplify_set (x);
4609
4610 case AND:
4611 case IOR:
4612 case XOR:
4613 return simplify_logical (x, last);
4614
4615 case ABS:
4616 /* (abs (neg <foo>)) -> (abs <foo>) */
4617 if (GET_CODE (XEXP (x, 0)) == NEG)
4618 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4619
4620 /* If the mode of the operand is VOIDmode (i.e. if it is ASM_OPERANDS),
4621 do nothing. */
4622 if (GET_MODE (XEXP (x, 0)) == VOIDmode)
4623 break;
4624
4625 /* If operand is something known to be positive, ignore the ABS. */
4626 if (GET_CODE (XEXP (x, 0)) == FFS || GET_CODE (XEXP (x, 0)) == ABS
4627 || ((GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
4628 <= HOST_BITS_PER_WIDE_INT)
4629 && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
4630 & ((HOST_WIDE_INT) 1
4631 << (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - 1)))
4632 == 0)))
4633 return XEXP (x, 0);
4634
4635 /* If operand is known to be only -1 or 0, convert ABS to NEG. */
4636 if (num_sign_bit_copies (XEXP (x, 0), mode) == GET_MODE_BITSIZE (mode))
4637 return gen_rtx_NEG (mode, XEXP (x, 0));
4638
4639 break;
4640
4641 case FFS:
4642 /* (ffs (*_extend <X>)) = (ffs <X>) */
4643 if (GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
4644 || GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
4645 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4646 break;
4647
4648 case POPCOUNT:
4649 case PARITY:
4650 /* (pop* (zero_extend <X>)) = (pop* <X>) */
4651 if (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
4652 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4653 break;
4654
4655 case FLOAT:
4656 /* (float (sign_extend <X>)) = (float <X>). */
4657 if (GET_CODE (XEXP (x, 0)) == SIGN_EXTEND)
4658 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4659 break;
4660
4661 case ASHIFT:
4662 case LSHIFTRT:
4663 case ASHIFTRT:
4664 case ROTATE:
4665 case ROTATERT:
4666 /* If this is a shift by a constant amount, simplify it. */
4667 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4668 return simplify_shift_const (x, code, mode, XEXP (x, 0),
4669 INTVAL (XEXP (x, 1)));
4670
4671 #ifdef SHIFT_COUNT_TRUNCATED
4672 else if (SHIFT_COUNT_TRUNCATED && GET_CODE (XEXP (x, 1)) != REG)
4673 SUBST (XEXP (x, 1),
4674 force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
4675 ((HOST_WIDE_INT) 1
4676 << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
4677 - 1,
4678 NULL_RTX, 0));
4679 #endif
4680
4681 break;
4682
4683 case VEC_SELECT:
4684 {
4685 rtx op0 = XEXP (x, 0);
4686 rtx op1 = XEXP (x, 1);
4687 int len;
4688
4689 if (GET_CODE (op1) != PARALLEL)
4690 abort ();
4691 len = XVECLEN (op1, 0);
4692 if (len == 1
4693 && GET_CODE (XVECEXP (op1, 0, 0)) == CONST_INT
4694 && GET_CODE (op0) == VEC_CONCAT)
4695 {
4696 int offset = INTVAL (XVECEXP (op1, 0, 0)) * GET_MODE_SIZE (GET_MODE (x));
4697
4698 /* Try to find the element in the VEC_CONCAT. */
4699 for (;;)
4700 {
4701 if (GET_MODE (op0) == GET_MODE (x))
4702 return op0;
4703 if (GET_CODE (op0) == VEC_CONCAT)
4704 {
4705 HOST_WIDE_INT op0_size = GET_MODE_SIZE (GET_MODE (XEXP (op0, 0)));
4706 if (op0_size < offset)
4707 op0 = XEXP (op0, 0);
4708 else
4709 {
4710 offset -= op0_size;
4711 op0 = XEXP (op0, 1);
4712 }
4713 }
4714 else
4715 break;
4716 }
4717 }
4718 }
4719
4720 break;
4721
4722 default:
4723 break;
4724 }
4725
4726 return x;
4727 }
4728 \f
4729 /* Simplify X, an IF_THEN_ELSE expression. Return the new expression. */
4730
4731 static rtx
4732 simplify_if_then_else (x)
4733 rtx x;
4734 {
4735 enum machine_mode mode = GET_MODE (x);
4736 rtx cond = XEXP (x, 0);
4737 rtx true_rtx = XEXP (x, 1);
4738 rtx false_rtx = XEXP (x, 2);
4739 enum rtx_code true_code = GET_CODE (cond);
4740 int comparison_p = GET_RTX_CLASS (true_code) == '<';
4741 rtx temp;
4742 int i;
4743 enum rtx_code false_code;
4744 rtx reversed;
4745
4746 /* Simplify storing of the truth value. */
4747 if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
4748 return gen_binary (true_code, mode, XEXP (cond, 0), XEXP (cond, 1));
4749
4750 /* Also when the truth value has to be reversed. */
4751 if (comparison_p
4752 && true_rtx == const0_rtx && false_rtx == const_true_rtx
4753 && (reversed = reversed_comparison (cond, mode, XEXP (cond, 0),
4754 XEXP (cond, 1))))
4755 return reversed;
4756
4757 /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
4758 in it is being compared against certain values. Get the true and false
4759 comparisons and see if that says anything about the value of each arm. */
4760
4761 if (comparison_p
4762 && ((false_code = combine_reversed_comparison_code (cond))
4763 != UNKNOWN)
4764 && GET_CODE (XEXP (cond, 0)) == REG)
4765 {
4766 HOST_WIDE_INT nzb;
4767 rtx from = XEXP (cond, 0);
4768 rtx true_val = XEXP (cond, 1);
4769 rtx false_val = true_val;
4770 int swapped = 0;
4771
4772 /* If FALSE_CODE is EQ, swap the codes and arms. */
4773
4774 if (false_code == EQ)
4775 {
4776 swapped = 1, true_code = EQ, false_code = NE;
4777 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
4778 }
4779
4780 /* If we are comparing against zero and the expression being tested has
4781 only a single bit that might be nonzero, that is its value when it is
4782 not equal to zero. Similarly if it is known to be -1 or 0. */
4783
4784 if (true_code == EQ && true_val == const0_rtx
4785 && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
4786 false_code = EQ, false_val = GEN_INT (nzb);
4787 else if (true_code == EQ && true_val == const0_rtx
4788 && (num_sign_bit_copies (from, GET_MODE (from))
4789 == GET_MODE_BITSIZE (GET_MODE (from))))
4790 false_code = EQ, false_val = constm1_rtx;
4791
4792 /* Now simplify an arm if we know the value of the register in the
4793 branch and it is used in the arm. Be careful due to the potential
4794 of locally-shared RTL. */
4795
4796 if (reg_mentioned_p (from, true_rtx))
4797 true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
4798 from, true_val),
4799 pc_rtx, pc_rtx, 0, 0);
4800 if (reg_mentioned_p (from, false_rtx))
4801 false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
4802 from, false_val),
4803 pc_rtx, pc_rtx, 0, 0);
4804
4805 SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
4806 SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
4807
4808 true_rtx = XEXP (x, 1);
4809 false_rtx = XEXP (x, 2);
4810 true_code = GET_CODE (cond);
4811 }
4812
4813 /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
4814 reversed, do so to avoid needing two sets of patterns for
4815 subtract-and-branch insns. Similarly if we have a constant in the true
4816 arm, the false arm is the same as the first operand of the comparison, or
4817 the false arm is more complicated than the true arm. */
4818
4819 if (comparison_p
4820 && combine_reversed_comparison_code (cond) != UNKNOWN
4821 && (true_rtx == pc_rtx
4822 || (CONSTANT_P (true_rtx)
4823 && GET_CODE (false_rtx) != CONST_INT && false_rtx != pc_rtx)
4824 || true_rtx == const0_rtx
4825 || (GET_RTX_CLASS (GET_CODE (true_rtx)) == 'o'
4826 && GET_RTX_CLASS (GET_CODE (false_rtx)) != 'o')
4827 || (GET_CODE (true_rtx) == SUBREG
4828 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (true_rtx))) == 'o'
4829 && GET_RTX_CLASS (GET_CODE (false_rtx)) != 'o')
4830 || reg_mentioned_p (true_rtx, false_rtx)
4831 || rtx_equal_p (false_rtx, XEXP (cond, 0))))
4832 {
4833 true_code = reversed_comparison_code (cond, NULL);
4834 SUBST (XEXP (x, 0),
4835 reversed_comparison (cond, GET_MODE (cond), XEXP (cond, 0),
4836 XEXP (cond, 1)));
4837
4838 SUBST (XEXP (x, 1), false_rtx);
4839 SUBST (XEXP (x, 2), true_rtx);
4840
4841 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
4842 cond = XEXP (x, 0);
4843
4844 /* It is possible that the conditional has been simplified out. */
4845 true_code = GET_CODE (cond);
4846 comparison_p = GET_RTX_CLASS (true_code) == '<';
4847 }
4848
4849 /* If the two arms are identical, we don't need the comparison. */
4850
4851 if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
4852 return true_rtx;
4853
4854 /* Convert a == b ? b : a to "a". */
4855 if (true_code == EQ && ! side_effects_p (cond)
4856 && !HONOR_NANS (mode)
4857 && rtx_equal_p (XEXP (cond, 0), false_rtx)
4858 && rtx_equal_p (XEXP (cond, 1), true_rtx))
4859 return false_rtx;
4860 else if (true_code == NE && ! side_effects_p (cond)
4861 && !HONOR_NANS (mode)
4862 && rtx_equal_p (XEXP (cond, 0), true_rtx)
4863 && rtx_equal_p (XEXP (cond, 1), false_rtx))
4864 return true_rtx;
4865
4866 /* Look for cases where we have (abs x) or (neg (abs X)). */
4867
4868 if (GET_MODE_CLASS (mode) == MODE_INT
4869 && GET_CODE (false_rtx) == NEG
4870 && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
4871 && comparison_p
4872 && rtx_equal_p (true_rtx, XEXP (cond, 0))
4873 && ! side_effects_p (true_rtx))
4874 switch (true_code)
4875 {
4876 case GT:
4877 case GE:
4878 return simplify_gen_unary (ABS, mode, true_rtx, mode);
4879 case LT:
4880 case LE:
4881 return
4882 simplify_gen_unary (NEG, mode,
4883 simplify_gen_unary (ABS, mode, true_rtx, mode),
4884 mode);
4885 default:
4886 break;
4887 }
4888
4889 /* Look for MIN or MAX. */
4890
4891 if ((! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
4892 && comparison_p
4893 && rtx_equal_p (XEXP (cond, 0), true_rtx)
4894 && rtx_equal_p (XEXP (cond, 1), false_rtx)
4895 && ! side_effects_p (cond))
4896 switch (true_code)
4897 {
4898 case GE:
4899 case GT:
4900 return gen_binary (SMAX, mode, true_rtx, false_rtx);
4901 case LE:
4902 case LT:
4903 return gen_binary (SMIN, mode, true_rtx, false_rtx);
4904 case GEU:
4905 case GTU:
4906 return gen_binary (UMAX, mode, true_rtx, false_rtx);
4907 case LEU:
4908 case LTU:
4909 return gen_binary (UMIN, mode, true_rtx, false_rtx);
4910 default:
4911 break;
4912 }
4913
4914 /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
4915 second operand is zero, this can be done as (OP Z (mult COND C2)) where
4916 C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
4917 SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
4918 We can do this kind of thing in some cases when STORE_FLAG_VALUE is
4919 neither 1 or -1, but it isn't worth checking for. */
4920
4921 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
4922 && comparison_p
4923 && GET_MODE_CLASS (mode) == MODE_INT
4924 && ! side_effects_p (x))
4925 {
4926 rtx t = make_compound_operation (true_rtx, SET);
4927 rtx f = make_compound_operation (false_rtx, SET);
4928 rtx cond_op0 = XEXP (cond, 0);
4929 rtx cond_op1 = XEXP (cond, 1);
4930 enum rtx_code op = NIL, extend_op = NIL;
4931 enum machine_mode m = mode;
4932 rtx z = 0, c1 = NULL_RTX;
4933
4934 if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
4935 || GET_CODE (t) == IOR || GET_CODE (t) == XOR
4936 || GET_CODE (t) == ASHIFT
4937 || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
4938 && rtx_equal_p (XEXP (t, 0), f))
4939 c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
4940
4941 /* If an identity-zero op is commutative, check whether there
4942 would be a match if we swapped the operands. */
4943 else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
4944 || GET_CODE (t) == XOR)
4945 && rtx_equal_p (XEXP (t, 1), f))
4946 c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
4947 else if (GET_CODE (t) == SIGN_EXTEND
4948 && (GET_CODE (XEXP (t, 0)) == PLUS
4949 || GET_CODE (XEXP (t, 0)) == MINUS
4950 || GET_CODE (XEXP (t, 0)) == IOR
4951 || GET_CODE (XEXP (t, 0)) == XOR
4952 || GET_CODE (XEXP (t, 0)) == ASHIFT
4953 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
4954 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
4955 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
4956 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
4957 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
4958 && (num_sign_bit_copies (f, GET_MODE (f))
4959 > (unsigned int)
4960 (GET_MODE_BITSIZE (mode)
4961 - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 0))))))
4962 {
4963 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
4964 extend_op = SIGN_EXTEND;
4965 m = GET_MODE (XEXP (t, 0));
4966 }
4967 else if (GET_CODE (t) == SIGN_EXTEND
4968 && (GET_CODE (XEXP (t, 0)) == PLUS
4969 || GET_CODE (XEXP (t, 0)) == IOR
4970 || GET_CODE (XEXP (t, 0)) == XOR)
4971 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
4972 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
4973 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
4974 && (num_sign_bit_copies (f, GET_MODE (f))
4975 > (unsigned int)
4976 (GET_MODE_BITSIZE (mode)
4977 - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 1))))))
4978 {
4979 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
4980 extend_op = SIGN_EXTEND;
4981 m = GET_MODE (XEXP (t, 0));
4982 }
4983 else if (GET_CODE (t) == ZERO_EXTEND
4984 && (GET_CODE (XEXP (t, 0)) == PLUS
4985 || GET_CODE (XEXP (t, 0)) == MINUS
4986 || GET_CODE (XEXP (t, 0)) == IOR
4987 || GET_CODE (XEXP (t, 0)) == XOR
4988 || GET_CODE (XEXP (t, 0)) == ASHIFT
4989 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
4990 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
4991 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
4992 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4993 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
4994 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
4995 && ((nonzero_bits (f, GET_MODE (f))
4996 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
4997 == 0))
4998 {
4999 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
5000 extend_op = ZERO_EXTEND;
5001 m = GET_MODE (XEXP (t, 0));
5002 }
5003 else if (GET_CODE (t) == ZERO_EXTEND
5004 && (GET_CODE (XEXP (t, 0)) == PLUS
5005 || GET_CODE (XEXP (t, 0)) == IOR
5006 || GET_CODE (XEXP (t, 0)) == XOR)
5007 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
5008 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5009 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
5010 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
5011 && ((nonzero_bits (f, GET_MODE (f))
5012 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
5013 == 0))
5014 {
5015 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
5016 extend_op = ZERO_EXTEND;
5017 m = GET_MODE (XEXP (t, 0));
5018 }
5019
5020 if (z)
5021 {
5022 temp = subst (gen_binary (true_code, m, cond_op0, cond_op1),
5023 pc_rtx, pc_rtx, 0, 0);
5024 temp = gen_binary (MULT, m, temp,
5025 gen_binary (MULT, m, c1, const_true_rtx));
5026 temp = subst (temp, pc_rtx, pc_rtx, 0, 0);
5027 temp = gen_binary (op, m, gen_lowpart_for_combine (m, z), temp);
5028
5029 if (extend_op != NIL)
5030 temp = simplify_gen_unary (extend_op, mode, temp, m);
5031
5032 return temp;
5033 }
5034 }
5035
5036 /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
5037 1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
5038 negation of a single bit, we can convert this operation to a shift. We
5039 can actually do this more generally, but it doesn't seem worth it. */
5040
5041 if (true_code == NE && XEXP (cond, 1) == const0_rtx
5042 && false_rtx == const0_rtx && GET_CODE (true_rtx) == CONST_INT
5043 && ((1 == nonzero_bits (XEXP (cond, 0), mode)
5044 && (i = exact_log2 (INTVAL (true_rtx))) >= 0)
5045 || ((num_sign_bit_copies (XEXP (cond, 0), mode)
5046 == GET_MODE_BITSIZE (mode))
5047 && (i = exact_log2 (-INTVAL (true_rtx))) >= 0)))
5048 return
5049 simplify_shift_const (NULL_RTX, ASHIFT, mode,
5050 gen_lowpart_for_combine (mode, XEXP (cond, 0)), i);
5051
5052 return x;
5053 }
5054 \f
5055 /* Simplify X, a SET expression. Return the new expression. */
5056
5057 static rtx
5058 simplify_set (x)
5059 rtx x;
5060 {
5061 rtx src = SET_SRC (x);
5062 rtx dest = SET_DEST (x);
5063 enum machine_mode mode
5064 = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
5065 rtx other_insn;
5066 rtx *cc_use;
5067
5068 /* (set (pc) (return)) gets written as (return). */
5069 if (GET_CODE (dest) == PC && GET_CODE (src) == RETURN)
5070 return src;
5071
5072 /* Now that we know for sure which bits of SRC we are using, see if we can
5073 simplify the expression for the object knowing that we only need the
5074 low-order bits. */
5075
5076 if (GET_MODE_CLASS (mode) == MODE_INT
5077 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
5078 {
5079 src = force_to_mode (src, mode, ~(HOST_WIDE_INT) 0, NULL_RTX, 0);
5080 SUBST (SET_SRC (x), src);
5081 }
5082
5083 /* If we are setting CC0 or if the source is a COMPARE, look for the use of
5084 the comparison result and try to simplify it unless we already have used
5085 undobuf.other_insn. */
5086 if ((GET_MODE_CLASS (mode) == MODE_CC
5087 || GET_CODE (src) == COMPARE
5088 || CC0_P (dest))
5089 && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
5090 && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
5091 && GET_RTX_CLASS (GET_CODE (*cc_use)) == '<'
5092 && rtx_equal_p (XEXP (*cc_use, 0), dest))
5093 {
5094 enum rtx_code old_code = GET_CODE (*cc_use);
5095 enum rtx_code new_code;
5096 rtx op0, op1, tmp;
5097 int other_changed = 0;
5098 enum machine_mode compare_mode = GET_MODE (dest);
5099 enum machine_mode tmp_mode;
5100
5101 if (GET_CODE (src) == COMPARE)
5102 op0 = XEXP (src, 0), op1 = XEXP (src, 1);
5103 else
5104 op0 = src, op1 = const0_rtx;
5105
5106 /* Check whether the comparison is known at compile time. */
5107 if (GET_MODE (op0) != VOIDmode)
5108 tmp_mode = GET_MODE (op0);
5109 else if (GET_MODE (op1) != VOIDmode)
5110 tmp_mode = GET_MODE (op1);
5111 else
5112 tmp_mode = compare_mode;
5113 tmp = simplify_relational_operation (old_code, tmp_mode, op0, op1);
5114 if (tmp != NULL_RTX)
5115 {
5116 rtx pat = PATTERN (other_insn);
5117 undobuf.other_insn = other_insn;
5118 SUBST (*cc_use, tmp);
5119
5120 /* Attempt to simplify CC user. */
5121 if (GET_CODE (pat) == SET)
5122 {
5123 rtx new = simplify_rtx (SET_SRC (pat));
5124 if (new != NULL_RTX)
5125 SUBST (SET_SRC (pat), new);
5126 }
5127
5128 /* Convert X into a no-op move. */
5129 SUBST (SET_DEST (x), pc_rtx);
5130 SUBST (SET_SRC (x), pc_rtx);
5131 return x;
5132 }
5133
5134 /* Simplify our comparison, if possible. */
5135 new_code = simplify_comparison (old_code, &op0, &op1);
5136
5137 #ifdef EXTRA_CC_MODES
5138 /* If this machine has CC modes other than CCmode, check to see if we
5139 need to use a different CC mode here. */
5140 compare_mode = SELECT_CC_MODE (new_code, op0, op1);
5141 #endif /* EXTRA_CC_MODES */
5142
5143 #if !defined (HAVE_cc0) && defined (EXTRA_CC_MODES)
5144 /* If the mode changed, we have to change SET_DEST, the mode in the
5145 compare, and the mode in the place SET_DEST is used. If SET_DEST is
5146 a hard register, just build new versions with the proper mode. If it
5147 is a pseudo, we lose unless it is only time we set the pseudo, in
5148 which case we can safely change its mode. */
5149 if (compare_mode != GET_MODE (dest))
5150 {
5151 unsigned int regno = REGNO (dest);
5152 rtx new_dest = gen_rtx_REG (compare_mode, regno);
5153
5154 if (regno < FIRST_PSEUDO_REGISTER
5155 || (REG_N_SETS (regno) == 1 && ! REG_USERVAR_P (dest)))
5156 {
5157 if (regno >= FIRST_PSEUDO_REGISTER)
5158 SUBST (regno_reg_rtx[regno], new_dest);
5159
5160 SUBST (SET_DEST (x), new_dest);
5161 SUBST (XEXP (*cc_use, 0), new_dest);
5162 other_changed = 1;
5163
5164 dest = new_dest;
5165 }
5166 }
5167 #endif
5168
5169 /* If the code changed, we have to build a new comparison in
5170 undobuf.other_insn. */
5171 if (new_code != old_code)
5172 {
5173 unsigned HOST_WIDE_INT mask;
5174
5175 SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
5176 dest, const0_rtx));
5177
5178 /* If the only change we made was to change an EQ into an NE or
5179 vice versa, OP0 has only one bit that might be nonzero, and OP1
5180 is zero, check if changing the user of the condition code will
5181 produce a valid insn. If it won't, we can keep the original code
5182 in that insn by surrounding our operation with an XOR. */
5183
5184 if (((old_code == NE && new_code == EQ)
5185 || (old_code == EQ && new_code == NE))
5186 && ! other_changed && op1 == const0_rtx
5187 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
5188 && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
5189 {
5190 rtx pat = PATTERN (other_insn), note = 0;
5191
5192 if ((recog_for_combine (&pat, other_insn, &note) < 0
5193 && ! check_asm_operands (pat)))
5194 {
5195 PUT_CODE (*cc_use, old_code);
5196 other_insn = 0;
5197
5198 op0 = gen_binary (XOR, GET_MODE (op0), op0, GEN_INT (mask));
5199 }
5200 }
5201
5202 other_changed = 1;
5203 }
5204
5205 if (other_changed)
5206 undobuf.other_insn = other_insn;
5207
5208 #ifdef HAVE_cc0
5209 /* If we are now comparing against zero, change our source if
5210 needed. If we do not use cc0, we always have a COMPARE. */
5211 if (op1 == const0_rtx && dest == cc0_rtx)
5212 {
5213 SUBST (SET_SRC (x), op0);
5214 src = op0;
5215 }
5216 else
5217 #endif
5218
5219 /* Otherwise, if we didn't previously have a COMPARE in the
5220 correct mode, we need one. */
5221 if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
5222 {
5223 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
5224 src = SET_SRC (x);
5225 }
5226 else
5227 {
5228 /* Otherwise, update the COMPARE if needed. */
5229 SUBST (XEXP (src, 0), op0);
5230 SUBST (XEXP (src, 1), op1);
5231 }
5232 }
5233 else
5234 {
5235 /* Get SET_SRC in a form where we have placed back any
5236 compound expressions. Then do the checks below. */
5237 src = make_compound_operation (src, SET);
5238 SUBST (SET_SRC (x), src);
5239 }
5240
5241 #ifdef WORD_REGISTER_OPERATIONS
5242 /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
5243 and X being a REG or (subreg (reg)), we may be able to convert this to
5244 (set (subreg:m2 x) (op)).
5245
5246 On a machine where WORD_REGISTER_OPERATIONS is defined, this
5247 transformation is safe as long as M1 and M2 have the same number
5248 of words.
5249
5250 However, on a machine without WORD_REGISTER_OPERATIONS defined,
5251 we cannot apply this transformation because it would create a
5252 paradoxical subreg in SET_DEST. */
5253
5254 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
5255 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (src))) != 'o'
5256 && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
5257 / UNITS_PER_WORD)
5258 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
5259 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
5260 #ifdef CANNOT_CHANGE_MODE_CLASS
5261 && ! (GET_CODE (dest) == REG && REGNO (dest) < FIRST_PSEUDO_REGISTER
5262 && REG_CANNOT_CHANGE_MODE_P (REGNO (dest),
5263 GET_MODE (SUBREG_REG (src)),
5264 GET_MODE (src)))
5265 #endif
5266 && (GET_CODE (dest) == REG
5267 || (GET_CODE (dest) == SUBREG
5268 && GET_CODE (SUBREG_REG (dest)) == REG)))
5269 {
5270 SUBST (SET_DEST (x),
5271 gen_lowpart_for_combine (GET_MODE (SUBREG_REG (src)),
5272 dest));
5273 SUBST (SET_SRC (x), SUBREG_REG (src));
5274
5275 src = SET_SRC (x), dest = SET_DEST (x);
5276 }
5277 #endif
5278
5279 #ifdef HAVE_cc0
5280 /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
5281 in SRC. */
5282 if (dest == cc0_rtx
5283 && GET_CODE (src) == SUBREG
5284 && subreg_lowpart_p (src)
5285 && (GET_MODE_BITSIZE (GET_MODE (src))
5286 < GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (src)))))
5287 {
5288 rtx inner = SUBREG_REG (src);
5289 enum machine_mode inner_mode = GET_MODE (inner);
5290
5291 /* Here we make sure that we don't have a sign bit on. */
5292 if (GET_MODE_BITSIZE (inner_mode) <= HOST_BITS_PER_WIDE_INT
5293 && (nonzero_bits (inner, inner_mode)
5294 < ((unsigned HOST_WIDE_INT) 1
5295 << (GET_MODE_BITSIZE (GET_MODE (src)) - 1))))
5296 {
5297 SUBST (SET_SRC (x), inner);
5298 src = SET_SRC (x);
5299 }
5300 }
5301 #endif
5302
5303 #ifdef LOAD_EXTEND_OP
5304 /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
5305 would require a paradoxical subreg. Replace the subreg with a
5306 zero_extend to avoid the reload that would otherwise be required. */
5307
5308 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
5309 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != NIL
5310 && SUBREG_BYTE (src) == 0
5311 && (GET_MODE_SIZE (GET_MODE (src))
5312 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
5313 && GET_CODE (SUBREG_REG (src)) == MEM)
5314 {
5315 SUBST (SET_SRC (x),
5316 gen_rtx (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
5317 GET_MODE (src), SUBREG_REG (src)));
5318
5319 src = SET_SRC (x);
5320 }
5321 #endif
5322
5323 /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
5324 are comparing an item known to be 0 or -1 against 0, use a logical
5325 operation instead. Check for one of the arms being an IOR of the other
5326 arm with some value. We compute three terms to be IOR'ed together. In
5327 practice, at most two will be nonzero. Then we do the IOR's. */
5328
5329 if (GET_CODE (dest) != PC
5330 && GET_CODE (src) == IF_THEN_ELSE
5331 && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
5332 && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
5333 && XEXP (XEXP (src, 0), 1) == const0_rtx
5334 && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
5335 #ifdef HAVE_conditional_move
5336 && ! can_conditionally_move_p (GET_MODE (src))
5337 #endif
5338 && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
5339 GET_MODE (XEXP (XEXP (src, 0), 0)))
5340 == GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (src, 0), 0))))
5341 && ! side_effects_p (src))
5342 {
5343 rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
5344 ? XEXP (src, 1) : XEXP (src, 2));
5345 rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
5346 ? XEXP (src, 2) : XEXP (src, 1));
5347 rtx term1 = const0_rtx, term2, term3;
5348
5349 if (GET_CODE (true_rtx) == IOR
5350 && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
5351 term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
5352 else if (GET_CODE (true_rtx) == IOR
5353 && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
5354 term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
5355 else if (GET_CODE (false_rtx) == IOR
5356 && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
5357 term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
5358 else if (GET_CODE (false_rtx) == IOR
5359 && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
5360 term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
5361
5362 term2 = gen_binary (AND, GET_MODE (src),
5363 XEXP (XEXP (src, 0), 0), true_rtx);
5364 term3 = gen_binary (AND, GET_MODE (src),
5365 simplify_gen_unary (NOT, GET_MODE (src),
5366 XEXP (XEXP (src, 0), 0),
5367 GET_MODE (src)),
5368 false_rtx);
5369
5370 SUBST (SET_SRC (x),
5371 gen_binary (IOR, GET_MODE (src),
5372 gen_binary (IOR, GET_MODE (src), term1, term2),
5373 term3));
5374
5375 src = SET_SRC (x);
5376 }
5377
5378 /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
5379 whole thing fail. */
5380 if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
5381 return src;
5382 else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
5383 return dest;
5384 else
5385 /* Convert this into a field assignment operation, if possible. */
5386 return make_field_assignment (x);
5387 }
5388 \f
5389 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
5390 result. LAST is nonzero if this is the last retry. */
5391
5392 static rtx
5393 simplify_logical (x, last)
5394 rtx x;
5395 int last;
5396 {
5397 enum machine_mode mode = GET_MODE (x);
5398 rtx op0 = XEXP (x, 0);
5399 rtx op1 = XEXP (x, 1);
5400 rtx reversed;
5401
5402 switch (GET_CODE (x))
5403 {
5404 case AND:
5405 /* Convert (A ^ B) & A to A & (~B) since the latter is often a single
5406 insn (and may simplify more). */
5407 if (GET_CODE (op0) == XOR
5408 && rtx_equal_p (XEXP (op0, 0), op1)
5409 && ! side_effects_p (op1))
5410 x = gen_binary (AND, mode,
5411 simplify_gen_unary (NOT, mode, XEXP (op0, 1), mode),
5412 op1);
5413
5414 if (GET_CODE (op0) == XOR
5415 && rtx_equal_p (XEXP (op0, 1), op1)
5416 && ! side_effects_p (op1))
5417 x = gen_binary (AND, mode,
5418 simplify_gen_unary (NOT, mode, XEXP (op0, 0), mode),
5419 op1);
5420
5421 /* Similarly for (~(A ^ B)) & A. */
5422 if (GET_CODE (op0) == NOT
5423 && GET_CODE (XEXP (op0, 0)) == XOR
5424 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), op1)
5425 && ! side_effects_p (op1))
5426 x = gen_binary (AND, mode, XEXP (XEXP (op0, 0), 1), op1);
5427
5428 if (GET_CODE (op0) == NOT
5429 && GET_CODE (XEXP (op0, 0)) == XOR
5430 && rtx_equal_p (XEXP (XEXP (op0, 0), 1), op1)
5431 && ! side_effects_p (op1))
5432 x = gen_binary (AND, mode, XEXP (XEXP (op0, 0), 0), op1);
5433
5434 /* We can call simplify_and_const_int only if we don't lose
5435 any (sign) bits when converting INTVAL (op1) to
5436 "unsigned HOST_WIDE_INT". */
5437 if (GET_CODE (op1) == CONST_INT
5438 && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5439 || INTVAL (op1) > 0))
5440 {
5441 x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
5442
5443 /* If we have (ior (and (X C1) C2)) and the next restart would be
5444 the last, simplify this by making C1 as small as possible
5445 and then exit. */
5446 if (last
5447 && GET_CODE (x) == IOR && GET_CODE (op0) == AND
5448 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5449 && GET_CODE (op1) == CONST_INT)
5450 return gen_binary (IOR, mode,
5451 gen_binary (AND, mode, XEXP (op0, 0),
5452 GEN_INT (INTVAL (XEXP (op0, 1))
5453 & ~INTVAL (op1))), op1);
5454
5455 if (GET_CODE (x) != AND)
5456 return x;
5457
5458 if (GET_RTX_CLASS (GET_CODE (x)) == 'c'
5459 || GET_RTX_CLASS (GET_CODE (x)) == '2')
5460 op0 = XEXP (x, 0), op1 = XEXP (x, 1);
5461 }
5462
5463 /* Convert (A | B) & A to A. */
5464 if (GET_CODE (op0) == IOR
5465 && (rtx_equal_p (XEXP (op0, 0), op1)
5466 || rtx_equal_p (XEXP (op0, 1), op1))
5467 && ! side_effects_p (XEXP (op0, 0))
5468 && ! side_effects_p (XEXP (op0, 1)))
5469 return op1;
5470
5471 /* In the following group of tests (and those in case IOR below),
5472 we start with some combination of logical operations and apply
5473 the distributive law followed by the inverse distributive law.
5474 Most of the time, this results in no change. However, if some of
5475 the operands are the same or inverses of each other, simplifications
5476 will result.
5477
5478 For example, (and (ior A B) (not B)) can occur as the result of
5479 expanding a bit field assignment. When we apply the distributive
5480 law to this, we get (ior (and (A (not B))) (and (B (not B)))),
5481 which then simplifies to (and (A (not B))).
5482
5483 If we have (and (ior A B) C), apply the distributive law and then
5484 the inverse distributive law to see if things simplify. */
5485
5486 if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
5487 {
5488 x = apply_distributive_law
5489 (gen_binary (GET_CODE (op0), mode,
5490 gen_binary (AND, mode, XEXP (op0, 0), op1),
5491 gen_binary (AND, mode, XEXP (op0, 1),
5492 copy_rtx (op1))));
5493 if (GET_CODE (x) != AND)
5494 return x;
5495 }
5496
5497 if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
5498 return apply_distributive_law
5499 (gen_binary (GET_CODE (op1), mode,
5500 gen_binary (AND, mode, XEXP (op1, 0), op0),
5501 gen_binary (AND, mode, XEXP (op1, 1),
5502 copy_rtx (op0))));
5503
5504 /* Similarly, taking advantage of the fact that
5505 (and (not A) (xor B C)) == (xor (ior A B) (ior A C)) */
5506
5507 if (GET_CODE (op0) == NOT && GET_CODE (op1) == XOR)
5508 return apply_distributive_law
5509 (gen_binary (XOR, mode,
5510 gen_binary (IOR, mode, XEXP (op0, 0), XEXP (op1, 0)),
5511 gen_binary (IOR, mode, copy_rtx (XEXP (op0, 0)),
5512 XEXP (op1, 1))));
5513
5514 else if (GET_CODE (op1) == NOT && GET_CODE (op0) == XOR)
5515 return apply_distributive_law
5516 (gen_binary (XOR, mode,
5517 gen_binary (IOR, mode, XEXP (op1, 0), XEXP (op0, 0)),
5518 gen_binary (IOR, mode, copy_rtx (XEXP (op1, 0)), XEXP (op0, 1))));
5519 break;
5520
5521 case IOR:
5522 /* (ior A C) is C if all bits of A that might be nonzero are on in C. */
5523 if (GET_CODE (op1) == CONST_INT
5524 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5525 && (nonzero_bits (op0, mode) & ~INTVAL (op1)) == 0)
5526 return op1;
5527
5528 /* Convert (A & B) | A to A. */
5529 if (GET_CODE (op0) == AND
5530 && (rtx_equal_p (XEXP (op0, 0), op1)
5531 || rtx_equal_p (XEXP (op0, 1), op1))
5532 && ! side_effects_p (XEXP (op0, 0))
5533 && ! side_effects_p (XEXP (op0, 1)))
5534 return op1;
5535
5536 /* If we have (ior (and A B) C), apply the distributive law and then
5537 the inverse distributive law to see if things simplify. */
5538
5539 if (GET_CODE (op0) == AND)
5540 {
5541 x = apply_distributive_law
5542 (gen_binary (AND, mode,
5543 gen_binary (IOR, mode, XEXP (op0, 0), op1),
5544 gen_binary (IOR, mode, XEXP (op0, 1),
5545 copy_rtx (op1))));
5546
5547 if (GET_CODE (x) != IOR)
5548 return x;
5549 }
5550
5551 if (GET_CODE (op1) == AND)
5552 {
5553 x = apply_distributive_law
5554 (gen_binary (AND, mode,
5555 gen_binary (IOR, mode, XEXP (op1, 0), op0),
5556 gen_binary (IOR, mode, XEXP (op1, 1),
5557 copy_rtx (op0))));
5558
5559 if (GET_CODE (x) != IOR)
5560 return x;
5561 }
5562
5563 /* Convert (ior (ashift A CX) (lshiftrt A CY)) where CX+CY equals the
5564 mode size to (rotate A CX). */
5565
5566 if (((GET_CODE (op0) == ASHIFT && GET_CODE (op1) == LSHIFTRT)
5567 || (GET_CODE (op1) == ASHIFT && GET_CODE (op0) == LSHIFTRT))
5568 && rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
5569 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5570 && GET_CODE (XEXP (op1, 1)) == CONST_INT
5571 && (INTVAL (XEXP (op0, 1)) + INTVAL (XEXP (op1, 1))
5572 == GET_MODE_BITSIZE (mode)))
5573 return gen_rtx_ROTATE (mode, XEXP (op0, 0),
5574 (GET_CODE (op0) == ASHIFT
5575 ? XEXP (op0, 1) : XEXP (op1, 1)));
5576
5577 /* If OP0 is (ashiftrt (plus ...) C), it might actually be
5578 a (sign_extend (plus ...)). If so, OP1 is a CONST_INT, and the PLUS
5579 does not affect any of the bits in OP1, it can really be done
5580 as a PLUS and we can associate. We do this by seeing if OP1
5581 can be safely shifted left C bits. */
5582 if (GET_CODE (op1) == CONST_INT && GET_CODE (op0) == ASHIFTRT
5583 && GET_CODE (XEXP (op0, 0)) == PLUS
5584 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
5585 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5586 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT)
5587 {
5588 int count = INTVAL (XEXP (op0, 1));
5589 HOST_WIDE_INT mask = INTVAL (op1) << count;
5590
5591 if (mask >> count == INTVAL (op1)
5592 && (mask & nonzero_bits (XEXP (op0, 0), mode)) == 0)
5593 {
5594 SUBST (XEXP (XEXP (op0, 0), 1),
5595 GEN_INT (INTVAL (XEXP (XEXP (op0, 0), 1)) | mask));
5596 return op0;
5597 }
5598 }
5599 break;
5600
5601 case XOR:
5602 /* If we are XORing two things that have no bits in common,
5603 convert them into an IOR. This helps to detect rotation encoded
5604 using those methods and possibly other simplifications. */
5605
5606 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5607 && (nonzero_bits (op0, mode)
5608 & nonzero_bits (op1, mode)) == 0)
5609 return (gen_binary (IOR, mode, op0, op1));
5610
5611 /* Convert (XOR (NOT x) (NOT y)) to (XOR x y).
5612 Also convert (XOR (NOT x) y) to (NOT (XOR x y)), similarly for
5613 (NOT y). */
5614 {
5615 int num_negated = 0;
5616
5617 if (GET_CODE (op0) == NOT)
5618 num_negated++, op0 = XEXP (op0, 0);
5619 if (GET_CODE (op1) == NOT)
5620 num_negated++, op1 = XEXP (op1, 0);
5621
5622 if (num_negated == 2)
5623 {
5624 SUBST (XEXP (x, 0), op0);
5625 SUBST (XEXP (x, 1), op1);
5626 }
5627 else if (num_negated == 1)
5628 return
5629 simplify_gen_unary (NOT, mode, gen_binary (XOR, mode, op0, op1),
5630 mode);
5631 }
5632
5633 /* Convert (xor (and A B) B) to (and (not A) B). The latter may
5634 correspond to a machine insn or result in further simplifications
5635 if B is a constant. */
5636
5637 if (GET_CODE (op0) == AND
5638 && rtx_equal_p (XEXP (op0, 1), op1)
5639 && ! side_effects_p (op1))
5640 return gen_binary (AND, mode,
5641 simplify_gen_unary (NOT, mode, XEXP (op0, 0), mode),
5642 op1);
5643
5644 else if (GET_CODE (op0) == AND
5645 && rtx_equal_p (XEXP (op0, 0), op1)
5646 && ! side_effects_p (op1))
5647 return gen_binary (AND, mode,
5648 simplify_gen_unary (NOT, mode, XEXP (op0, 1), mode),
5649 op1);
5650
5651 /* (xor (comparison foo bar) (const_int 1)) can become the reversed
5652 comparison if STORE_FLAG_VALUE is 1. */
5653 if (STORE_FLAG_VALUE == 1
5654 && op1 == const1_rtx
5655 && GET_RTX_CLASS (GET_CODE (op0)) == '<'
5656 && (reversed = reversed_comparison (op0, mode, XEXP (op0, 0),
5657 XEXP (op0, 1))))
5658 return reversed;
5659
5660 /* (lshiftrt foo C) where C is the number of bits in FOO minus 1
5661 is (lt foo (const_int 0)), so we can perform the above
5662 simplification if STORE_FLAG_VALUE is 1. */
5663
5664 if (STORE_FLAG_VALUE == 1
5665 && op1 == const1_rtx
5666 && GET_CODE (op0) == LSHIFTRT
5667 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5668 && INTVAL (XEXP (op0, 1)) == GET_MODE_BITSIZE (mode) - 1)
5669 return gen_rtx_GE (mode, XEXP (op0, 0), const0_rtx);
5670
5671 /* (xor (comparison foo bar) (const_int sign-bit))
5672 when STORE_FLAG_VALUE is the sign bit. */
5673 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5674 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
5675 == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
5676 && op1 == const_true_rtx
5677 && GET_RTX_CLASS (GET_CODE (op0)) == '<'
5678 && (reversed = reversed_comparison (op0, mode, XEXP (op0, 0),
5679 XEXP (op0, 1))))
5680 return reversed;
5681
5682 break;
5683
5684 default:
5685 abort ();
5686 }
5687
5688 return x;
5689 }
5690 \f
5691 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
5692 operations" because they can be replaced with two more basic operations.
5693 ZERO_EXTEND is also considered "compound" because it can be replaced with
5694 an AND operation, which is simpler, though only one operation.
5695
5696 The function expand_compound_operation is called with an rtx expression
5697 and will convert it to the appropriate shifts and AND operations,
5698 simplifying at each stage.
5699
5700 The function make_compound_operation is called to convert an expression
5701 consisting of shifts and ANDs into the equivalent compound expression.
5702 It is the inverse of this function, loosely speaking. */
5703
5704 static rtx
5705 expand_compound_operation (x)
5706 rtx x;
5707 {
5708 unsigned HOST_WIDE_INT pos = 0, len;
5709 int unsignedp = 0;
5710 unsigned int modewidth;
5711 rtx tem;
5712
5713 switch (GET_CODE (x))
5714 {
5715 case ZERO_EXTEND:
5716 unsignedp = 1;
5717 case SIGN_EXTEND:
5718 /* We can't necessarily use a const_int for a multiword mode;
5719 it depends on implicitly extending the value.
5720 Since we don't know the right way to extend it,
5721 we can't tell whether the implicit way is right.
5722
5723 Even for a mode that is no wider than a const_int,
5724 we can't win, because we need to sign extend one of its bits through
5725 the rest of it, and we don't know which bit. */
5726 if (GET_CODE (XEXP (x, 0)) == CONST_INT)
5727 return x;
5728
5729 /* Return if (subreg:MODE FROM 0) is not a safe replacement for
5730 (zero_extend:MODE FROM) or (sign_extend:MODE FROM). It is for any MEM
5731 because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
5732 reloaded. If not for that, MEM's would very rarely be safe.
5733
5734 Reject MODEs bigger than a word, because we might not be able
5735 to reference a two-register group starting with an arbitrary register
5736 (and currently gen_lowpart might crash for a SUBREG). */
5737
5738 if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
5739 return x;
5740
5741 /* Reject MODEs that aren't scalar integers because turning vector
5742 or complex modes into shifts causes problems. */
5743
5744 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
5745 return x;
5746
5747 len = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)));
5748 /* If the inner object has VOIDmode (the only way this can happen
5749 is if it is an ASM_OPERANDS), we can't do anything since we don't
5750 know how much masking to do. */
5751 if (len == 0)
5752 return x;
5753
5754 break;
5755
5756 case ZERO_EXTRACT:
5757 unsignedp = 1;
5758 case SIGN_EXTRACT:
5759 /* If the operand is a CLOBBER, just return it. */
5760 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
5761 return XEXP (x, 0);
5762
5763 if (GET_CODE (XEXP (x, 1)) != CONST_INT
5764 || GET_CODE (XEXP (x, 2)) != CONST_INT
5765 || GET_MODE (XEXP (x, 0)) == VOIDmode)
5766 return x;
5767
5768 /* Reject MODEs that aren't scalar integers because turning vector
5769 or complex modes into shifts causes problems. */
5770
5771 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
5772 return x;
5773
5774 len = INTVAL (XEXP (x, 1));
5775 pos = INTVAL (XEXP (x, 2));
5776
5777 /* If this goes outside the object being extracted, replace the object
5778 with a (use (mem ...)) construct that only combine understands
5779 and is used only for this purpose. */
5780 if (len + pos > GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
5781 SUBST (XEXP (x, 0), gen_rtx_USE (GET_MODE (x), XEXP (x, 0)));
5782
5783 if (BITS_BIG_ENDIAN)
5784 pos = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - len - pos;
5785
5786 break;
5787
5788 default:
5789 return x;
5790 }
5791 /* Convert sign extension to zero extension, if we know that the high
5792 bit is not set, as this is easier to optimize. It will be converted
5793 back to cheaper alternative in make_extraction. */
5794 if (GET_CODE (x) == SIGN_EXTEND
5795 && (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5796 && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
5797 & ~(((unsigned HOST_WIDE_INT)
5798 GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
5799 >> 1))
5800 == 0)))
5801 {
5802 rtx temp = gen_rtx_ZERO_EXTEND (GET_MODE (x), XEXP (x, 0));
5803 rtx temp2 = expand_compound_operation (temp);
5804
5805 /* Make sure this is a profitable operation. */
5806 if (rtx_cost (x, SET) > rtx_cost (temp2, SET))
5807 return temp2;
5808 else if (rtx_cost (x, SET) > rtx_cost (temp, SET))
5809 return temp;
5810 else
5811 return x;
5812 }
5813
5814 /* We can optimize some special cases of ZERO_EXTEND. */
5815 if (GET_CODE (x) == ZERO_EXTEND)
5816 {
5817 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
5818 know that the last value didn't have any inappropriate bits
5819 set. */
5820 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
5821 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
5822 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5823 && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
5824 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5825 return XEXP (XEXP (x, 0), 0);
5826
5827 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
5828 if (GET_CODE (XEXP (x, 0)) == SUBREG
5829 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
5830 && subreg_lowpart_p (XEXP (x, 0))
5831 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5832 && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
5833 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5834 return SUBREG_REG (XEXP (x, 0));
5835
5836 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
5837 is a comparison and STORE_FLAG_VALUE permits. This is like
5838 the first case, but it works even when GET_MODE (x) is larger
5839 than HOST_WIDE_INT. */
5840 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
5841 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
5842 && GET_RTX_CLASS (GET_CODE (XEXP (XEXP (x, 0), 0))) == '<'
5843 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5844 <= HOST_BITS_PER_WIDE_INT)
5845 && ((HOST_WIDE_INT) STORE_FLAG_VALUE
5846 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5847 return XEXP (XEXP (x, 0), 0);
5848
5849 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
5850 if (GET_CODE (XEXP (x, 0)) == SUBREG
5851 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
5852 && subreg_lowpart_p (XEXP (x, 0))
5853 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0)))) == '<'
5854 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5855 <= HOST_BITS_PER_WIDE_INT)
5856 && ((HOST_WIDE_INT) STORE_FLAG_VALUE
5857 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5858 return SUBREG_REG (XEXP (x, 0));
5859
5860 }
5861
5862 /* If we reach here, we want to return a pair of shifts. The inner
5863 shift is a left shift of BITSIZE - POS - LEN bits. The outer
5864 shift is a right shift of BITSIZE - LEN bits. It is arithmetic or
5865 logical depending on the value of UNSIGNEDP.
5866
5867 If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
5868 converted into an AND of a shift.
5869
5870 We must check for the case where the left shift would have a negative
5871 count. This can happen in a case like (x >> 31) & 255 on machines
5872 that can't shift by a constant. On those machines, we would first
5873 combine the shift with the AND to produce a variable-position
5874 extraction. Then the constant of 31 would be substituted in to produce
5875 a such a position. */
5876
5877 modewidth = GET_MODE_BITSIZE (GET_MODE (x));
5878 if (modewidth + len >= pos)
5879 tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
5880 GET_MODE (x),
5881 simplify_shift_const (NULL_RTX, ASHIFT,
5882 GET_MODE (x),
5883 XEXP (x, 0),
5884 modewidth - pos - len),
5885 modewidth - len);
5886
5887 else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
5888 tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
5889 simplify_shift_const (NULL_RTX, LSHIFTRT,
5890 GET_MODE (x),
5891 XEXP (x, 0), pos),
5892 ((HOST_WIDE_INT) 1 << len) - 1);
5893 else
5894 /* Any other cases we can't handle. */
5895 return x;
5896
5897 /* If we couldn't do this for some reason, return the original
5898 expression. */
5899 if (GET_CODE (tem) == CLOBBER)
5900 return x;
5901
5902 return tem;
5903 }
5904 \f
5905 /* X is a SET which contains an assignment of one object into
5906 a part of another (such as a bit-field assignment, STRICT_LOW_PART,
5907 or certain SUBREGS). If possible, convert it into a series of
5908 logical operations.
5909
5910 We half-heartedly support variable positions, but do not at all
5911 support variable lengths. */
5912
5913 static rtx
5914 expand_field_assignment (x)
5915 rtx x;
5916 {
5917 rtx inner;
5918 rtx pos; /* Always counts from low bit. */
5919 int len;
5920 rtx mask;
5921 enum machine_mode compute_mode;
5922
5923 /* Loop until we find something we can't simplify. */
5924 while (1)
5925 {
5926 if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
5927 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
5928 {
5929 inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
5930 len = GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)));
5931 pos = GEN_INT (subreg_lsb (XEXP (SET_DEST (x), 0)));
5932 }
5933 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
5934 && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT)
5935 {
5936 inner = XEXP (SET_DEST (x), 0);
5937 len = INTVAL (XEXP (SET_DEST (x), 1));
5938 pos = XEXP (SET_DEST (x), 2);
5939
5940 /* If the position is constant and spans the width of INNER,
5941 surround INNER with a USE to indicate this. */
5942 if (GET_CODE (pos) == CONST_INT
5943 && INTVAL (pos) + len > GET_MODE_BITSIZE (GET_MODE (inner)))
5944 inner = gen_rtx_USE (GET_MODE (SET_DEST (x)), inner);
5945
5946 if (BITS_BIG_ENDIAN)
5947 {
5948 if (GET_CODE (pos) == CONST_INT)
5949 pos = GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner)) - len
5950 - INTVAL (pos));
5951 else if (GET_CODE (pos) == MINUS
5952 && GET_CODE (XEXP (pos, 1)) == CONST_INT
5953 && (INTVAL (XEXP (pos, 1))
5954 == GET_MODE_BITSIZE (GET_MODE (inner)) - len))
5955 /* If position is ADJUST - X, new position is X. */
5956 pos = XEXP (pos, 0);
5957 else
5958 pos = gen_binary (MINUS, GET_MODE (pos),
5959 GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner))
5960 - len),
5961 pos);
5962 }
5963 }
5964
5965 /* A SUBREG between two modes that occupy the same numbers of words
5966 can be done by moving the SUBREG to the source. */
5967 else if (GET_CODE (SET_DEST (x)) == SUBREG
5968 /* We need SUBREGs to compute nonzero_bits properly. */
5969 && nonzero_sign_valid
5970 && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
5971 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
5972 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
5973 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
5974 {
5975 x = gen_rtx_SET (VOIDmode, SUBREG_REG (SET_DEST (x)),
5976 gen_lowpart_for_combine
5977 (GET_MODE (SUBREG_REG (SET_DEST (x))),
5978 SET_SRC (x)));
5979 continue;
5980 }
5981 else
5982 break;
5983
5984 while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
5985 inner = SUBREG_REG (inner);
5986
5987 compute_mode = GET_MODE (inner);
5988
5989 /* Don't attempt bitwise arithmetic on non scalar integer modes. */
5990 if (! SCALAR_INT_MODE_P (compute_mode))
5991 {
5992 enum machine_mode imode;
5993
5994 /* Don't do anything for vector or complex integral types. */
5995 if (! FLOAT_MODE_P (compute_mode))
5996 break;
5997
5998 /* Try to find an integral mode to pun with. */
5999 imode = mode_for_size (GET_MODE_BITSIZE (compute_mode), MODE_INT, 0);
6000 if (imode == BLKmode)
6001 break;
6002
6003 compute_mode = imode;
6004 inner = gen_lowpart_for_combine (imode, inner);
6005 }
6006
6007 /* Compute a mask of LEN bits, if we can do this on the host machine. */
6008 if (len < HOST_BITS_PER_WIDE_INT)
6009 mask = GEN_INT (((HOST_WIDE_INT) 1 << len) - 1);
6010 else
6011 break;
6012
6013 /* Now compute the equivalent expression. Make a copy of INNER
6014 for the SET_DEST in case it is a MEM into which we will substitute;
6015 we don't want shared RTL in that case. */
6016 x = gen_rtx_SET
6017 (VOIDmode, copy_rtx (inner),
6018 gen_binary (IOR, compute_mode,
6019 gen_binary (AND, compute_mode,
6020 simplify_gen_unary (NOT, compute_mode,
6021 gen_binary (ASHIFT,
6022 compute_mode,
6023 mask, pos),
6024 compute_mode),
6025 inner),
6026 gen_binary (ASHIFT, compute_mode,
6027 gen_binary (AND, compute_mode,
6028 gen_lowpart_for_combine
6029 (compute_mode, SET_SRC (x)),
6030 mask),
6031 pos)));
6032 }
6033
6034 return x;
6035 }
6036 \f
6037 /* Return an RTX for a reference to LEN bits of INNER. If POS_RTX is nonzero,
6038 it is an RTX that represents a variable starting position; otherwise,
6039 POS is the (constant) starting bit position (counted from the LSB).
6040
6041 INNER may be a USE. This will occur when we started with a bitfield
6042 that went outside the boundary of the object in memory, which is
6043 allowed on most machines. To isolate this case, we produce a USE
6044 whose mode is wide enough and surround the MEM with it. The only
6045 code that understands the USE is this routine. If it is not removed,
6046 it will cause the resulting insn not to match.
6047
6048 UNSIGNEDP is nonzero for an unsigned reference and zero for a
6049 signed reference.
6050
6051 IN_DEST is nonzero if this is a reference in the destination of a
6052 SET. This is used when a ZERO_ or SIGN_EXTRACT isn't needed. If nonzero,
6053 a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
6054 be used.
6055
6056 IN_COMPARE is nonzero if we are in a COMPARE. This means that a
6057 ZERO_EXTRACT should be built even for bits starting at bit 0.
6058
6059 MODE is the desired mode of the result (if IN_DEST == 0).
6060
6061 The result is an RTX for the extraction or NULL_RTX if the target
6062 can't handle it. */
6063
6064 static rtx
6065 make_extraction (mode, inner, pos, pos_rtx, len,
6066 unsignedp, in_dest, in_compare)
6067 enum machine_mode mode;
6068 rtx inner;
6069 HOST_WIDE_INT pos;
6070 rtx pos_rtx;
6071 unsigned HOST_WIDE_INT len;
6072 int unsignedp;
6073 int in_dest, in_compare;
6074 {
6075 /* This mode describes the size of the storage area
6076 to fetch the overall value from. Within that, we
6077 ignore the POS lowest bits, etc. */
6078 enum machine_mode is_mode = GET_MODE (inner);
6079 enum machine_mode inner_mode;
6080 enum machine_mode wanted_inner_mode = byte_mode;
6081 enum machine_mode wanted_inner_reg_mode = word_mode;
6082 enum machine_mode pos_mode = word_mode;
6083 enum machine_mode extraction_mode = word_mode;
6084 enum machine_mode tmode = mode_for_size (len, MODE_INT, 1);
6085 int spans_byte = 0;
6086 rtx new = 0;
6087 rtx orig_pos_rtx = pos_rtx;
6088 HOST_WIDE_INT orig_pos;
6089
6090 /* Get some information about INNER and get the innermost object. */
6091 if (GET_CODE (inner) == USE)
6092 /* (use:SI (mem:QI foo)) stands for (mem:SI foo). */
6093 /* We don't need to adjust the position because we set up the USE
6094 to pretend that it was a full-word object. */
6095 spans_byte = 1, inner = XEXP (inner, 0);
6096 else if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
6097 {
6098 /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
6099 consider just the QI as the memory to extract from.
6100 The subreg adds or removes high bits; its mode is
6101 irrelevant to the meaning of this extraction,
6102 since POS and LEN count from the lsb. */
6103 if (GET_CODE (SUBREG_REG (inner)) == MEM)
6104 is_mode = GET_MODE (SUBREG_REG (inner));
6105 inner = SUBREG_REG (inner);
6106 }
6107 else if (GET_CODE (inner) == ASHIFT
6108 && GET_CODE (XEXP (inner, 1)) == CONST_INT
6109 && pos_rtx == 0 && pos == 0
6110 && len > (unsigned HOST_WIDE_INT) INTVAL (XEXP (inner, 1)))
6111 {
6112 /* We're extracting the least significant bits of an rtx
6113 (ashift X (const_int C)), where LEN > C. Extract the
6114 least significant (LEN - C) bits of X, giving an rtx
6115 whose mode is MODE, then shift it left C times. */
6116 new = make_extraction (mode, XEXP (inner, 0),
6117 0, 0, len - INTVAL (XEXP (inner, 1)),
6118 unsignedp, in_dest, in_compare);
6119 if (new != 0)
6120 return gen_rtx_ASHIFT (mode, new, XEXP (inner, 1));
6121 }
6122
6123 inner_mode = GET_MODE (inner);
6124
6125 if (pos_rtx && GET_CODE (pos_rtx) == CONST_INT)
6126 pos = INTVAL (pos_rtx), pos_rtx = 0;
6127
6128 /* See if this can be done without an extraction. We never can if the
6129 width of the field is not the same as that of some integer mode. For
6130 registers, we can only avoid the extraction if the position is at the
6131 low-order bit and this is either not in the destination or we have the
6132 appropriate STRICT_LOW_PART operation available.
6133
6134 For MEM, we can avoid an extract if the field starts on an appropriate
6135 boundary and we can change the mode of the memory reference. However,
6136 we cannot directly access the MEM if we have a USE and the underlying
6137 MEM is not TMODE. This combination means that MEM was being used in a
6138 context where bits outside its mode were being referenced; that is only
6139 valid in bit-field insns. */
6140
6141 if (tmode != BLKmode
6142 && ! (spans_byte && inner_mode != tmode)
6143 && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
6144 && GET_CODE (inner) != MEM
6145 && (! in_dest
6146 || (GET_CODE (inner) == REG
6147 && have_insn_for (STRICT_LOW_PART, tmode))))
6148 || (GET_CODE (inner) == MEM && pos_rtx == 0
6149 && (pos
6150 % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
6151 : BITS_PER_UNIT)) == 0
6152 /* We can't do this if we are widening INNER_MODE (it
6153 may not be aligned, for one thing). */
6154 && GET_MODE_BITSIZE (inner_mode) >= GET_MODE_BITSIZE (tmode)
6155 && (inner_mode == tmode
6156 || (! mode_dependent_address_p (XEXP (inner, 0))
6157 && ! MEM_VOLATILE_P (inner))))))
6158 {
6159 /* If INNER is a MEM, make a new MEM that encompasses just the desired
6160 field. If the original and current mode are the same, we need not
6161 adjust the offset. Otherwise, we do if bytes big endian.
6162
6163 If INNER is not a MEM, get a piece consisting of just the field
6164 of interest (in this case POS % BITS_PER_WORD must be 0). */
6165
6166 if (GET_CODE (inner) == MEM)
6167 {
6168 HOST_WIDE_INT offset;
6169
6170 /* POS counts from lsb, but make OFFSET count in memory order. */
6171 if (BYTES_BIG_ENDIAN)
6172 offset = (GET_MODE_BITSIZE (is_mode) - len - pos) / BITS_PER_UNIT;
6173 else
6174 offset = pos / BITS_PER_UNIT;
6175
6176 new = adjust_address_nv (inner, tmode, offset);
6177 }
6178 else if (GET_CODE (inner) == REG)
6179 {
6180 /* We can't call gen_lowpart_for_combine here since we always want
6181 a SUBREG and it would sometimes return a new hard register. */
6182 if (tmode != inner_mode)
6183 {
6184 HOST_WIDE_INT final_word = pos / BITS_PER_WORD;
6185
6186 if (WORDS_BIG_ENDIAN
6187 && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
6188 final_word = ((GET_MODE_SIZE (inner_mode)
6189 - GET_MODE_SIZE (tmode))
6190 / UNITS_PER_WORD) - final_word;
6191
6192 final_word *= UNITS_PER_WORD;
6193 if (BYTES_BIG_ENDIAN &&
6194 GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (tmode))
6195 final_word += (GET_MODE_SIZE (inner_mode)
6196 - GET_MODE_SIZE (tmode)) % UNITS_PER_WORD;
6197
6198 /* Avoid creating invalid subregs, for example when
6199 simplifying (x>>32)&255. */
6200 if (final_word >= GET_MODE_SIZE (inner_mode))
6201 return NULL_RTX;
6202
6203 new = gen_rtx_SUBREG (tmode, inner, final_word);
6204 }
6205 else
6206 new = inner;
6207 }
6208 else
6209 new = force_to_mode (inner, tmode,
6210 len >= HOST_BITS_PER_WIDE_INT
6211 ? ~(unsigned HOST_WIDE_INT) 0
6212 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
6213 NULL_RTX, 0);
6214
6215 /* If this extraction is going into the destination of a SET,
6216 make a STRICT_LOW_PART unless we made a MEM. */
6217
6218 if (in_dest)
6219 return (GET_CODE (new) == MEM ? new
6220 : (GET_CODE (new) != SUBREG
6221 ? gen_rtx_CLOBBER (tmode, const0_rtx)
6222 : gen_rtx_STRICT_LOW_PART (VOIDmode, new)));
6223
6224 if (mode == tmode)
6225 return new;
6226
6227 if (GET_CODE (new) == CONST_INT)
6228 return gen_int_mode (INTVAL (new), mode);
6229
6230 /* If we know that no extraneous bits are set, and that the high
6231 bit is not set, convert the extraction to the cheaper of
6232 sign and zero extension, that are equivalent in these cases. */
6233 if (flag_expensive_optimizations
6234 && (GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
6235 && ((nonzero_bits (new, tmode)
6236 & ~(((unsigned HOST_WIDE_INT)
6237 GET_MODE_MASK (tmode))
6238 >> 1))
6239 == 0)))
6240 {
6241 rtx temp = gen_rtx_ZERO_EXTEND (mode, new);
6242 rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new);
6243
6244 /* Prefer ZERO_EXTENSION, since it gives more information to
6245 backends. */
6246 if (rtx_cost (temp, SET) <= rtx_cost (temp1, SET))
6247 return temp;
6248 return temp1;
6249 }
6250
6251 /* Otherwise, sign- or zero-extend unless we already are in the
6252 proper mode. */
6253
6254 return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
6255 mode, new));
6256 }
6257
6258 /* Unless this is a COMPARE or we have a funny memory reference,
6259 don't do anything with zero-extending field extracts starting at
6260 the low-order bit since they are simple AND operations. */
6261 if (pos_rtx == 0 && pos == 0 && ! in_dest
6262 && ! in_compare && ! spans_byte && unsignedp)
6263 return 0;
6264
6265 /* Unless we are allowed to span bytes or INNER is not MEM, reject this if
6266 we would be spanning bytes or if the position is not a constant and the
6267 length is not 1. In all other cases, we would only be going outside
6268 our object in cases when an original shift would have been
6269 undefined. */
6270 if (! spans_byte && GET_CODE (inner) == MEM
6271 && ((pos_rtx == 0 && pos + len > GET_MODE_BITSIZE (is_mode))
6272 || (pos_rtx != 0 && len != 1)))
6273 return 0;
6274
6275 /* Get the mode to use should INNER not be a MEM, the mode for the position,
6276 and the mode for the result. */
6277 if (in_dest && mode_for_extraction (EP_insv, -1) != MAX_MACHINE_MODE)
6278 {
6279 wanted_inner_reg_mode = mode_for_extraction (EP_insv, 0);
6280 pos_mode = mode_for_extraction (EP_insv, 2);
6281 extraction_mode = mode_for_extraction (EP_insv, 3);
6282 }
6283
6284 if (! in_dest && unsignedp
6285 && mode_for_extraction (EP_extzv, -1) != MAX_MACHINE_MODE)
6286 {
6287 wanted_inner_reg_mode = mode_for_extraction (EP_extzv, 1);
6288 pos_mode = mode_for_extraction (EP_extzv, 3);
6289 extraction_mode = mode_for_extraction (EP_extzv, 0);
6290 }
6291
6292 if (! in_dest && ! unsignedp
6293 && mode_for_extraction (EP_extv, -1) != MAX_MACHINE_MODE)
6294 {
6295 wanted_inner_reg_mode = mode_for_extraction (EP_extv, 1);
6296 pos_mode = mode_for_extraction (EP_extv, 3);
6297 extraction_mode = mode_for_extraction (EP_extv, 0);
6298 }
6299
6300 /* Never narrow an object, since that might not be safe. */
6301
6302 if (mode != VOIDmode
6303 && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
6304 extraction_mode = mode;
6305
6306 if (pos_rtx && GET_MODE (pos_rtx) != VOIDmode
6307 && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
6308 pos_mode = GET_MODE (pos_rtx);
6309
6310 /* If this is not from memory, the desired mode is wanted_inner_reg_mode;
6311 if we have to change the mode of memory and cannot, the desired mode is
6312 EXTRACTION_MODE. */
6313 if (GET_CODE (inner) != MEM)
6314 wanted_inner_mode = wanted_inner_reg_mode;
6315 else if (inner_mode != wanted_inner_mode
6316 && (mode_dependent_address_p (XEXP (inner, 0))
6317 || MEM_VOLATILE_P (inner)))
6318 wanted_inner_mode = extraction_mode;
6319
6320 orig_pos = pos;
6321
6322 if (BITS_BIG_ENDIAN)
6323 {
6324 /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
6325 BITS_BIG_ENDIAN style. If position is constant, compute new
6326 position. Otherwise, build subtraction.
6327 Note that POS is relative to the mode of the original argument.
6328 If it's a MEM we need to recompute POS relative to that.
6329 However, if we're extracting from (or inserting into) a register,
6330 we want to recompute POS relative to wanted_inner_mode. */
6331 int width = (GET_CODE (inner) == MEM
6332 ? GET_MODE_BITSIZE (is_mode)
6333 : GET_MODE_BITSIZE (wanted_inner_mode));
6334
6335 if (pos_rtx == 0)
6336 pos = width - len - pos;
6337 else
6338 pos_rtx
6339 = gen_rtx_MINUS (GET_MODE (pos_rtx), GEN_INT (width - len), pos_rtx);
6340 /* POS may be less than 0 now, but we check for that below.
6341 Note that it can only be less than 0 if GET_CODE (inner) != MEM. */
6342 }
6343
6344 /* If INNER has a wider mode, make it smaller. If this is a constant
6345 extract, try to adjust the byte to point to the byte containing
6346 the value. */
6347 if (wanted_inner_mode != VOIDmode
6348 && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
6349 && ((GET_CODE (inner) == MEM
6350 && (inner_mode == wanted_inner_mode
6351 || (! mode_dependent_address_p (XEXP (inner, 0))
6352 && ! MEM_VOLATILE_P (inner))))))
6353 {
6354 int offset = 0;
6355
6356 /* The computations below will be correct if the machine is big
6357 endian in both bits and bytes or little endian in bits and bytes.
6358 If it is mixed, we must adjust. */
6359
6360 /* If bytes are big endian and we had a paradoxical SUBREG, we must
6361 adjust OFFSET to compensate. */
6362 if (BYTES_BIG_ENDIAN
6363 && ! spans_byte
6364 && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
6365 offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
6366
6367 /* If this is a constant position, we can move to the desired byte. */
6368 if (pos_rtx == 0)
6369 {
6370 offset += pos / BITS_PER_UNIT;
6371 pos %= GET_MODE_BITSIZE (wanted_inner_mode);
6372 }
6373
6374 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
6375 && ! spans_byte
6376 && is_mode != wanted_inner_mode)
6377 offset = (GET_MODE_SIZE (is_mode)
6378 - GET_MODE_SIZE (wanted_inner_mode) - offset);
6379
6380 if (offset != 0 || inner_mode != wanted_inner_mode)
6381 inner = adjust_address_nv (inner, wanted_inner_mode, offset);
6382 }
6383
6384 /* If INNER is not memory, we can always get it into the proper mode. If we
6385 are changing its mode, POS must be a constant and smaller than the size
6386 of the new mode. */
6387 else if (GET_CODE (inner) != MEM)
6388 {
6389 if (GET_MODE (inner) != wanted_inner_mode
6390 && (pos_rtx != 0
6391 || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
6392 return 0;
6393
6394 inner = force_to_mode (inner, wanted_inner_mode,
6395 pos_rtx
6396 || len + orig_pos >= HOST_BITS_PER_WIDE_INT
6397 ? ~(unsigned HOST_WIDE_INT) 0
6398 : ((((unsigned HOST_WIDE_INT) 1 << len) - 1)
6399 << orig_pos),
6400 NULL_RTX, 0);
6401 }
6402
6403 /* Adjust mode of POS_RTX, if needed. If we want a wider mode, we
6404 have to zero extend. Otherwise, we can just use a SUBREG. */
6405 if (pos_rtx != 0
6406 && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
6407 {
6408 rtx temp = gen_rtx_ZERO_EXTEND (pos_mode, pos_rtx);
6409
6410 /* If we know that no extraneous bits are set, and that the high
6411 bit is not set, convert extraction to cheaper one - either
6412 SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
6413 cases. */
6414 if (flag_expensive_optimizations
6415 && (GET_MODE_BITSIZE (GET_MODE (pos_rtx)) <= HOST_BITS_PER_WIDE_INT
6416 && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
6417 & ~(((unsigned HOST_WIDE_INT)
6418 GET_MODE_MASK (GET_MODE (pos_rtx)))
6419 >> 1))
6420 == 0)))
6421 {
6422 rtx temp1 = gen_rtx_SIGN_EXTEND (pos_mode, pos_rtx);
6423
6424 /* Prefer ZERO_EXTENSION, since it gives more information to
6425 backends. */
6426 if (rtx_cost (temp1, SET) < rtx_cost (temp, SET))
6427 temp = temp1;
6428 }
6429 pos_rtx = temp;
6430 }
6431 else if (pos_rtx != 0
6432 && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
6433 pos_rtx = gen_lowpart_for_combine (pos_mode, pos_rtx);
6434
6435 /* Make POS_RTX unless we already have it and it is correct. If we don't
6436 have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
6437 be a CONST_INT. */
6438 if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
6439 pos_rtx = orig_pos_rtx;
6440
6441 else if (pos_rtx == 0)
6442 pos_rtx = GEN_INT (pos);
6443
6444 /* Make the required operation. See if we can use existing rtx. */
6445 new = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
6446 extraction_mode, inner, GEN_INT (len), pos_rtx);
6447 if (! in_dest)
6448 new = gen_lowpart_for_combine (mode, new);
6449
6450 return new;
6451 }
6452 \f
6453 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
6454 with any other operations in X. Return X without that shift if so. */
6455
6456 static rtx
6457 extract_left_shift (x, count)
6458 rtx x;
6459 int count;
6460 {
6461 enum rtx_code code = GET_CODE (x);
6462 enum machine_mode mode = GET_MODE (x);
6463 rtx tem;
6464
6465 switch (code)
6466 {
6467 case ASHIFT:
6468 /* This is the shift itself. If it is wide enough, we will return
6469 either the value being shifted if the shift count is equal to
6470 COUNT or a shift for the difference. */
6471 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6472 && INTVAL (XEXP (x, 1)) >= count)
6473 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
6474 INTVAL (XEXP (x, 1)) - count);
6475 break;
6476
6477 case NEG: case NOT:
6478 if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
6479 return simplify_gen_unary (code, mode, tem, mode);
6480
6481 break;
6482
6483 case PLUS: case IOR: case XOR: case AND:
6484 /* If we can safely shift this constant and we find the inner shift,
6485 make a new operation. */
6486 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6487 && (INTVAL (XEXP (x, 1)) & ((((HOST_WIDE_INT) 1 << count)) - 1)) == 0
6488 && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
6489 return gen_binary (code, mode, tem,
6490 GEN_INT (INTVAL (XEXP (x, 1)) >> count));
6491
6492 break;
6493
6494 default:
6495 break;
6496 }
6497
6498 return 0;
6499 }
6500 \f
6501 /* Look at the expression rooted at X. Look for expressions
6502 equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
6503 Form these expressions.
6504
6505 Return the new rtx, usually just X.
6506
6507 Also, for machines like the VAX that don't have logical shift insns,
6508 try to convert logical to arithmetic shift operations in cases where
6509 they are equivalent. This undoes the canonicalizations to logical
6510 shifts done elsewhere.
6511
6512 We try, as much as possible, to re-use rtl expressions to save memory.
6513
6514 IN_CODE says what kind of expression we are processing. Normally, it is
6515 SET. In a memory address (inside a MEM, PLUS or minus, the latter two
6516 being kludges), it is MEM. When processing the arguments of a comparison
6517 or a COMPARE against zero, it is COMPARE. */
6518
6519 static rtx
6520 make_compound_operation (x, in_code)
6521 rtx x;
6522 enum rtx_code in_code;
6523 {
6524 enum rtx_code code = GET_CODE (x);
6525 enum machine_mode mode = GET_MODE (x);
6526 int mode_width = GET_MODE_BITSIZE (mode);
6527 rtx rhs, lhs;
6528 enum rtx_code next_code;
6529 int i;
6530 rtx new = 0;
6531 rtx tem;
6532 const char *fmt;
6533
6534 /* Select the code to be used in recursive calls. Once we are inside an
6535 address, we stay there. If we have a comparison, set to COMPARE,
6536 but once inside, go back to our default of SET. */
6537
6538 next_code = (code == MEM || code == PLUS || code == MINUS ? MEM
6539 : ((code == COMPARE || GET_RTX_CLASS (code) == '<')
6540 && XEXP (x, 1) == const0_rtx) ? COMPARE
6541 : in_code == COMPARE ? SET : in_code);
6542
6543 /* Process depending on the code of this operation. If NEW is set
6544 nonzero, it will be returned. */
6545
6546 switch (code)
6547 {
6548 case ASHIFT:
6549 /* Convert shifts by constants into multiplications if inside
6550 an address. */
6551 if (in_code == MEM && GET_CODE (XEXP (x, 1)) == CONST_INT
6552 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
6553 && INTVAL (XEXP (x, 1)) >= 0)
6554 {
6555 new = make_compound_operation (XEXP (x, 0), next_code);
6556 new = gen_rtx_MULT (mode, new,
6557 GEN_INT ((HOST_WIDE_INT) 1
6558 << INTVAL (XEXP (x, 1))));
6559 }
6560 break;
6561
6562 case AND:
6563 /* If the second operand is not a constant, we can't do anything
6564 with it. */
6565 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
6566 break;
6567
6568 /* If the constant is a power of two minus one and the first operand
6569 is a logical right shift, make an extraction. */
6570 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6571 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6572 {
6573 new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
6574 new = make_extraction (mode, new, 0, XEXP (XEXP (x, 0), 1), i, 1,
6575 0, in_code == COMPARE);
6576 }
6577
6578 /* Same as previous, but for (subreg (lshiftrt ...)) in first op. */
6579 else if (GET_CODE (XEXP (x, 0)) == SUBREG
6580 && subreg_lowpart_p (XEXP (x, 0))
6581 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
6582 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6583 {
6584 new = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
6585 next_code);
6586 new = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new, 0,
6587 XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
6588 0, in_code == COMPARE);
6589 }
6590 /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)). */
6591 else if ((GET_CODE (XEXP (x, 0)) == XOR
6592 || GET_CODE (XEXP (x, 0)) == IOR)
6593 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
6594 && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
6595 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6596 {
6597 /* Apply the distributive law, and then try to make extractions. */
6598 new = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
6599 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
6600 XEXP (x, 1)),
6601 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
6602 XEXP (x, 1)));
6603 new = make_compound_operation (new, in_code);
6604 }
6605
6606 /* If we are have (and (rotate X C) M) and C is larger than the number
6607 of bits in M, this is an extraction. */
6608
6609 else if (GET_CODE (XEXP (x, 0)) == ROTATE
6610 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6611 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0
6612 && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
6613 {
6614 new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
6615 new = make_extraction (mode, new,
6616 (GET_MODE_BITSIZE (mode)
6617 - INTVAL (XEXP (XEXP (x, 0), 1))),
6618 NULL_RTX, i, 1, 0, in_code == COMPARE);
6619 }
6620
6621 /* On machines without logical shifts, if the operand of the AND is
6622 a logical shift and our mask turns off all the propagated sign
6623 bits, we can replace the logical shift with an arithmetic shift. */
6624 else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6625 && !have_insn_for (LSHIFTRT, mode)
6626 && have_insn_for (ASHIFTRT, mode)
6627 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6628 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
6629 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
6630 && mode_width <= HOST_BITS_PER_WIDE_INT)
6631 {
6632 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
6633
6634 mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
6635 if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
6636 SUBST (XEXP (x, 0),
6637 gen_rtx_ASHIFTRT (mode,
6638 make_compound_operation
6639 (XEXP (XEXP (x, 0), 0), next_code),
6640 XEXP (XEXP (x, 0), 1)));
6641 }
6642
6643 /* If the constant is one less than a power of two, this might be
6644 representable by an extraction even if no shift is present.
6645 If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
6646 we are in a COMPARE. */
6647 else if ((i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6648 new = make_extraction (mode,
6649 make_compound_operation (XEXP (x, 0),
6650 next_code),
6651 0, NULL_RTX, i, 1, 0, in_code == COMPARE);
6652
6653 /* If we are in a comparison and this is an AND with a power of two,
6654 convert this into the appropriate bit extract. */
6655 else if (in_code == COMPARE
6656 && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
6657 new = make_extraction (mode,
6658 make_compound_operation (XEXP (x, 0),
6659 next_code),
6660 i, NULL_RTX, 1, 1, 0, 1);
6661
6662 break;
6663
6664 case LSHIFTRT:
6665 /* If the sign bit is known to be zero, replace this with an
6666 arithmetic shift. */
6667 if (have_insn_for (ASHIFTRT, mode)
6668 && ! have_insn_for (LSHIFTRT, mode)
6669 && mode_width <= HOST_BITS_PER_WIDE_INT
6670 && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
6671 {
6672 new = gen_rtx_ASHIFTRT (mode,
6673 make_compound_operation (XEXP (x, 0),
6674 next_code),
6675 XEXP (x, 1));
6676 break;
6677 }
6678
6679 /* ... fall through ... */
6680
6681 case ASHIFTRT:
6682 lhs = XEXP (x, 0);
6683 rhs = XEXP (x, 1);
6684
6685 /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
6686 this is a SIGN_EXTRACT. */
6687 if (GET_CODE (rhs) == CONST_INT
6688 && GET_CODE (lhs) == ASHIFT
6689 && GET_CODE (XEXP (lhs, 1)) == CONST_INT
6690 && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1)))
6691 {
6692 new = make_compound_operation (XEXP (lhs, 0), next_code);
6693 new = make_extraction (mode, new,
6694 INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
6695 NULL_RTX, mode_width - INTVAL (rhs),
6696 code == LSHIFTRT, 0, in_code == COMPARE);
6697 break;
6698 }
6699
6700 /* See if we have operations between an ASHIFTRT and an ASHIFT.
6701 If so, try to merge the shifts into a SIGN_EXTEND. We could
6702 also do this for some cases of SIGN_EXTRACT, but it doesn't
6703 seem worth the effort; the case checked for occurs on Alpha. */
6704
6705 if (GET_RTX_CLASS (GET_CODE (lhs)) != 'o'
6706 && ! (GET_CODE (lhs) == SUBREG
6707 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (lhs))) == 'o'))
6708 && GET_CODE (rhs) == CONST_INT
6709 && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
6710 && (new = extract_left_shift (lhs, INTVAL (rhs))) != 0)
6711 new = make_extraction (mode, make_compound_operation (new, next_code),
6712 0, NULL_RTX, mode_width - INTVAL (rhs),
6713 code == LSHIFTRT, 0, in_code == COMPARE);
6714
6715 break;
6716
6717 case SUBREG:
6718 /* Call ourselves recursively on the inner expression. If we are
6719 narrowing the object and it has a different RTL code from
6720 what it originally did, do this SUBREG as a force_to_mode. */
6721
6722 tem = make_compound_operation (SUBREG_REG (x), in_code);
6723 if (GET_CODE (tem) != GET_CODE (SUBREG_REG (x))
6724 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (tem))
6725 && subreg_lowpart_p (x))
6726 {
6727 rtx newer = force_to_mode (tem, mode, ~(HOST_WIDE_INT) 0,
6728 NULL_RTX, 0);
6729
6730 /* If we have something other than a SUBREG, we might have
6731 done an expansion, so rerun ourselves. */
6732 if (GET_CODE (newer) != SUBREG)
6733 newer = make_compound_operation (newer, in_code);
6734
6735 return newer;
6736 }
6737
6738 /* If this is a paradoxical subreg, and the new code is a sign or
6739 zero extension, omit the subreg and widen the extension. If it
6740 is a regular subreg, we can still get rid of the subreg by not
6741 widening so much, or in fact removing the extension entirely. */
6742 if ((GET_CODE (tem) == SIGN_EXTEND
6743 || GET_CODE (tem) == ZERO_EXTEND)
6744 && subreg_lowpart_p (x))
6745 {
6746 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (tem))
6747 || (GET_MODE_SIZE (mode) >
6748 GET_MODE_SIZE (GET_MODE (XEXP (tem, 0)))))
6749 {
6750 if (! SCALAR_INT_MODE_P (mode))
6751 break;
6752 tem = gen_rtx_fmt_e (GET_CODE (tem), mode, XEXP (tem, 0));
6753 }
6754 else
6755 tem = gen_lowpart_for_combine (mode, XEXP (tem, 0));
6756 return tem;
6757 }
6758 break;
6759
6760 default:
6761 break;
6762 }
6763
6764 if (new)
6765 {
6766 x = gen_lowpart_for_combine (mode, new);
6767 code = GET_CODE (x);
6768 }
6769
6770 /* Now recursively process each operand of this operation. */
6771 fmt = GET_RTX_FORMAT (code);
6772 for (i = 0; i < GET_RTX_LENGTH (code); i++)
6773 if (fmt[i] == 'e')
6774 {
6775 new = make_compound_operation (XEXP (x, i), next_code);
6776 SUBST (XEXP (x, i), new);
6777 }
6778
6779 return x;
6780 }
6781 \f
6782 /* Given M see if it is a value that would select a field of bits
6783 within an item, but not the entire word. Return -1 if not.
6784 Otherwise, return the starting position of the field, where 0 is the
6785 low-order bit.
6786
6787 *PLEN is set to the length of the field. */
6788
6789 static int
6790 get_pos_from_mask (m, plen)
6791 unsigned HOST_WIDE_INT m;
6792 unsigned HOST_WIDE_INT *plen;
6793 {
6794 /* Get the bit number of the first 1 bit from the right, -1 if none. */
6795 int pos = exact_log2 (m & -m);
6796 int len;
6797
6798 if (pos < 0)
6799 return -1;
6800
6801 /* Now shift off the low-order zero bits and see if we have a power of
6802 two minus 1. */
6803 len = exact_log2 ((m >> pos) + 1);
6804
6805 if (len <= 0)
6806 return -1;
6807
6808 *plen = len;
6809 return pos;
6810 }
6811 \f
6812 /* See if X can be simplified knowing that we will only refer to it in
6813 MODE and will only refer to those bits that are nonzero in MASK.
6814 If other bits are being computed or if masking operations are done
6815 that select a superset of the bits in MASK, they can sometimes be
6816 ignored.
6817
6818 Return a possibly simplified expression, but always convert X to
6819 MODE. If X is a CONST_INT, AND the CONST_INT with MASK.
6820
6821 Also, if REG is nonzero and X is a register equal in value to REG,
6822 replace X with REG.
6823
6824 If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
6825 are all off in X. This is used when X will be complemented, by either
6826 NOT, NEG, or XOR. */
6827
6828 static rtx
6829 force_to_mode (x, mode, mask, reg, just_select)
6830 rtx x;
6831 enum machine_mode mode;
6832 unsigned HOST_WIDE_INT mask;
6833 rtx reg;
6834 int just_select;
6835 {
6836 enum rtx_code code = GET_CODE (x);
6837 int next_select = just_select || code == XOR || code == NOT || code == NEG;
6838 enum machine_mode op_mode;
6839 unsigned HOST_WIDE_INT fuller_mask, nonzero;
6840 rtx op0, op1, temp;
6841
6842 /* If this is a CALL or ASM_OPERANDS, don't do anything. Some of the
6843 code below will do the wrong thing since the mode of such an
6844 expression is VOIDmode.
6845
6846 Also do nothing if X is a CLOBBER; this can happen if X was
6847 the return value from a call to gen_lowpart_for_combine. */
6848 if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
6849 return x;
6850
6851 /* We want to perform the operation is its present mode unless we know
6852 that the operation is valid in MODE, in which case we do the operation
6853 in MODE. */
6854 op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
6855 && have_insn_for (code, mode))
6856 ? mode : GET_MODE (x));
6857
6858 /* It is not valid to do a right-shift in a narrower mode
6859 than the one it came in with. */
6860 if ((code == LSHIFTRT || code == ASHIFTRT)
6861 && GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (GET_MODE (x)))
6862 op_mode = GET_MODE (x);
6863
6864 /* Truncate MASK to fit OP_MODE. */
6865 if (op_mode)
6866 mask &= GET_MODE_MASK (op_mode);
6867
6868 /* When we have an arithmetic operation, or a shift whose count we
6869 do not know, we need to assume that all bit the up to the highest-order
6870 bit in MASK will be needed. This is how we form such a mask. */
6871 if (op_mode)
6872 fuller_mask = (GET_MODE_BITSIZE (op_mode) >= HOST_BITS_PER_WIDE_INT
6873 ? GET_MODE_MASK (op_mode)
6874 : (((unsigned HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1))
6875 - 1));
6876 else
6877 fuller_mask = ~(HOST_WIDE_INT) 0;
6878
6879 /* Determine what bits of X are guaranteed to be (non)zero. */
6880 nonzero = nonzero_bits (x, mode);
6881
6882 /* If none of the bits in X are needed, return a zero. */
6883 if (! just_select && (nonzero & mask) == 0)
6884 x = const0_rtx;
6885
6886 /* If X is a CONST_INT, return a new one. Do this here since the
6887 test below will fail. */
6888 if (GET_CODE (x) == CONST_INT)
6889 {
6890 if (SCALAR_INT_MODE_P (mode))
6891 return gen_int_mode (INTVAL (x) & mask, mode);
6892 else
6893 {
6894 x = GEN_INT (INTVAL (x) & mask);
6895 return gen_lowpart_common (mode, x);
6896 }
6897 }
6898
6899 /* If X is narrower than MODE and we want all the bits in X's mode, just
6900 get X in the proper mode. */
6901 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
6902 && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
6903 return gen_lowpart_for_combine (mode, x);
6904
6905 /* If we aren't changing the mode, X is not a SUBREG, and all zero bits in
6906 MASK are already known to be zero in X, we need not do anything. */
6907 if (GET_MODE (x) == mode && code != SUBREG && (~mask & nonzero) == 0)
6908 return x;
6909
6910 switch (code)
6911 {
6912 case CLOBBER:
6913 /* If X is a (clobber (const_int)), return it since we know we are
6914 generating something that won't match. */
6915 return x;
6916
6917 case USE:
6918 /* X is a (use (mem ..)) that was made from a bit-field extraction that
6919 spanned the boundary of the MEM. If we are now masking so it is
6920 within that boundary, we don't need the USE any more. */
6921 if (! BITS_BIG_ENDIAN
6922 && (mask & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6923 return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
6924 break;
6925
6926 case SIGN_EXTEND:
6927 case ZERO_EXTEND:
6928 case ZERO_EXTRACT:
6929 case SIGN_EXTRACT:
6930 x = expand_compound_operation (x);
6931 if (GET_CODE (x) != code)
6932 return force_to_mode (x, mode, mask, reg, next_select);
6933 break;
6934
6935 case REG:
6936 if (reg != 0 && (rtx_equal_p (get_last_value (reg), x)
6937 || rtx_equal_p (reg, get_last_value (x))))
6938 x = reg;
6939 break;
6940
6941 case SUBREG:
6942 if (subreg_lowpart_p (x)
6943 /* We can ignore the effect of this SUBREG if it narrows the mode or
6944 if the constant masks to zero all the bits the mode doesn't
6945 have. */
6946 && ((GET_MODE_SIZE (GET_MODE (x))
6947 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
6948 || (0 == (mask
6949 & GET_MODE_MASK (GET_MODE (x))
6950 & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
6951 return force_to_mode (SUBREG_REG (x), mode, mask, reg, next_select);
6952 break;
6953
6954 case AND:
6955 /* If this is an AND with a constant, convert it into an AND
6956 whose constant is the AND of that constant with MASK. If it
6957 remains an AND of MASK, delete it since it is redundant. */
6958
6959 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
6960 {
6961 x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
6962 mask & INTVAL (XEXP (x, 1)));
6963
6964 /* If X is still an AND, see if it is an AND with a mask that
6965 is just some low-order bits. If so, and it is MASK, we don't
6966 need it. */
6967
6968 if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
6969 && ((INTVAL (XEXP (x, 1)) & GET_MODE_MASK (GET_MODE (x)))
6970 == mask))
6971 x = XEXP (x, 0);
6972
6973 /* If it remains an AND, try making another AND with the bits
6974 in the mode mask that aren't in MASK turned on. If the
6975 constant in the AND is wide enough, this might make a
6976 cheaper constant. */
6977
6978 if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
6979 && GET_MODE_MASK (GET_MODE (x)) != mask
6980 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
6981 {
6982 HOST_WIDE_INT cval = (INTVAL (XEXP (x, 1))
6983 | (GET_MODE_MASK (GET_MODE (x)) & ~mask));
6984 int width = GET_MODE_BITSIZE (GET_MODE (x));
6985 rtx y;
6986
6987 /* If MODE is narrower that HOST_WIDE_INT and CVAL is a negative
6988 number, sign extend it. */
6989 if (width > 0 && width < HOST_BITS_PER_WIDE_INT
6990 && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
6991 cval |= (HOST_WIDE_INT) -1 << width;
6992
6993 y = gen_binary (AND, GET_MODE (x), XEXP (x, 0), GEN_INT (cval));
6994 if (rtx_cost (y, SET) < rtx_cost (x, SET))
6995 x = y;
6996 }
6997
6998 break;
6999 }
7000
7001 goto binop;
7002
7003 case PLUS:
7004 /* In (and (plus FOO C1) M), if M is a mask that just turns off
7005 low-order bits (as in an alignment operation) and FOO is already
7006 aligned to that boundary, mask C1 to that boundary as well.
7007 This may eliminate that PLUS and, later, the AND. */
7008
7009 {
7010 unsigned int width = GET_MODE_BITSIZE (mode);
7011 unsigned HOST_WIDE_INT smask = mask;
7012
7013 /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
7014 number, sign extend it. */
7015
7016 if (width < HOST_BITS_PER_WIDE_INT
7017 && (smask & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
7018 smask |= (HOST_WIDE_INT) -1 << width;
7019
7020 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7021 && exact_log2 (- smask) >= 0
7022 && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
7023 && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
7024 return force_to_mode (plus_constant (XEXP (x, 0),
7025 (INTVAL (XEXP (x, 1)) & smask)),
7026 mode, smask, reg, next_select);
7027 }
7028
7029 /* ... fall through ... */
7030
7031 case MULT:
7032 /* For PLUS, MINUS and MULT, we need any bits less significant than the
7033 most significant bit in MASK since carries from those bits will
7034 affect the bits we are interested in. */
7035 mask = fuller_mask;
7036 goto binop;
7037
7038 case MINUS:
7039 /* If X is (minus C Y) where C's least set bit is larger than any bit
7040 in the mask, then we may replace with (neg Y). */
7041 if (GET_CODE (XEXP (x, 0)) == CONST_INT
7042 && (((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 0))
7043 & -INTVAL (XEXP (x, 0))))
7044 > mask))
7045 {
7046 x = simplify_gen_unary (NEG, GET_MODE (x), XEXP (x, 1),
7047 GET_MODE (x));
7048 return force_to_mode (x, mode, mask, reg, next_select);
7049 }
7050
7051 /* Similarly, if C contains every bit in the fuller_mask, then we may
7052 replace with (not Y). */
7053 if (GET_CODE (XEXP (x, 0)) == CONST_INT
7054 && ((INTVAL (XEXP (x, 0)) | (HOST_WIDE_INT) fuller_mask)
7055 == INTVAL (XEXP (x, 0))))
7056 {
7057 x = simplify_gen_unary (NOT, GET_MODE (x),
7058 XEXP (x, 1), GET_MODE (x));
7059 return force_to_mode (x, mode, mask, reg, next_select);
7060 }
7061
7062 mask = fuller_mask;
7063 goto binop;
7064
7065 case IOR:
7066 case XOR:
7067 /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
7068 LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
7069 operation which may be a bitfield extraction. Ensure that the
7070 constant we form is not wider than the mode of X. */
7071
7072 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7073 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
7074 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7075 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
7076 && GET_CODE (XEXP (x, 1)) == CONST_INT
7077 && ((INTVAL (XEXP (XEXP (x, 0), 1))
7078 + floor_log2 (INTVAL (XEXP (x, 1))))
7079 < GET_MODE_BITSIZE (GET_MODE (x)))
7080 && (INTVAL (XEXP (x, 1))
7081 & ~nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
7082 {
7083 temp = GEN_INT ((INTVAL (XEXP (x, 1)) & mask)
7084 << INTVAL (XEXP (XEXP (x, 0), 1)));
7085 temp = gen_binary (GET_CODE (x), GET_MODE (x),
7086 XEXP (XEXP (x, 0), 0), temp);
7087 x = gen_binary (LSHIFTRT, GET_MODE (x), temp,
7088 XEXP (XEXP (x, 0), 1));
7089 return force_to_mode (x, mode, mask, reg, next_select);
7090 }
7091
7092 binop:
7093 /* For most binary operations, just propagate into the operation and
7094 change the mode if we have an operation of that mode. */
7095
7096 op0 = gen_lowpart_for_combine (op_mode,
7097 force_to_mode (XEXP (x, 0), mode, mask,
7098 reg, next_select));
7099 op1 = gen_lowpart_for_combine (op_mode,
7100 force_to_mode (XEXP (x, 1), mode, mask,
7101 reg, next_select));
7102
7103 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7104 x = gen_binary (code, op_mode, op0, op1);
7105 break;
7106
7107 case ASHIFT:
7108 /* For left shifts, do the same, but just for the first operand.
7109 However, we cannot do anything with shifts where we cannot
7110 guarantee that the counts are smaller than the size of the mode
7111 because such a count will have a different meaning in a
7112 wider mode. */
7113
7114 if (! (GET_CODE (XEXP (x, 1)) == CONST_INT
7115 && INTVAL (XEXP (x, 1)) >= 0
7116 && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (mode))
7117 && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
7118 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
7119 < (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (mode))))
7120 break;
7121
7122 /* If the shift count is a constant and we can do arithmetic in
7123 the mode of the shift, refine which bits we need. Otherwise, use the
7124 conservative form of the mask. */
7125 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7126 && INTVAL (XEXP (x, 1)) >= 0
7127 && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (op_mode)
7128 && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
7129 mask >>= INTVAL (XEXP (x, 1));
7130 else
7131 mask = fuller_mask;
7132
7133 op0 = gen_lowpart_for_combine (op_mode,
7134 force_to_mode (XEXP (x, 0), op_mode,
7135 mask, reg, next_select));
7136
7137 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
7138 x = gen_binary (code, op_mode, op0, XEXP (x, 1));
7139 break;
7140
7141 case LSHIFTRT:
7142 /* Here we can only do something if the shift count is a constant,
7143 this shift constant is valid for the host, and we can do arithmetic
7144 in OP_MODE. */
7145
7146 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7147 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
7148 && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
7149 {
7150 rtx inner = XEXP (x, 0);
7151 unsigned HOST_WIDE_INT inner_mask;
7152
7153 /* Select the mask of the bits we need for the shift operand. */
7154 inner_mask = mask << INTVAL (XEXP (x, 1));
7155
7156 /* We can only change the mode of the shift if we can do arithmetic
7157 in the mode of the shift and INNER_MASK is no wider than the
7158 width of OP_MODE. */
7159 if (GET_MODE_BITSIZE (op_mode) > HOST_BITS_PER_WIDE_INT
7160 || (inner_mask & ~GET_MODE_MASK (op_mode)) != 0)
7161 op_mode = GET_MODE (x);
7162
7163 inner = force_to_mode (inner, op_mode, inner_mask, reg, next_select);
7164
7165 if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
7166 x = gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
7167 }
7168
7169 /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
7170 shift and AND produces only copies of the sign bit (C2 is one less
7171 than a power of two), we can do this with just a shift. */
7172
7173 if (GET_CODE (x) == LSHIFTRT
7174 && GET_CODE (XEXP (x, 1)) == CONST_INT
7175 /* The shift puts one of the sign bit copies in the least significant
7176 bit. */
7177 && ((INTVAL (XEXP (x, 1))
7178 + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
7179 >= GET_MODE_BITSIZE (GET_MODE (x)))
7180 && exact_log2 (mask + 1) >= 0
7181 /* Number of bits left after the shift must be more than the mask
7182 needs. */
7183 && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
7184 <= GET_MODE_BITSIZE (GET_MODE (x)))
7185 /* Must be more sign bit copies than the mask needs. */
7186 && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
7187 >= exact_log2 (mask + 1)))
7188 x = gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
7189 GEN_INT (GET_MODE_BITSIZE (GET_MODE (x))
7190 - exact_log2 (mask + 1)));
7191
7192 goto shiftrt;
7193
7194 case ASHIFTRT:
7195 /* If we are just looking for the sign bit, we don't need this shift at
7196 all, even if it has a variable count. */
7197 if (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
7198 && (mask == ((unsigned HOST_WIDE_INT) 1
7199 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
7200 return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
7201
7202 /* If this is a shift by a constant, get a mask that contains those bits
7203 that are not copies of the sign bit. We then have two cases: If
7204 MASK only includes those bits, this can be a logical shift, which may
7205 allow simplifications. If MASK is a single-bit field not within
7206 those bits, we are requesting a copy of the sign bit and hence can
7207 shift the sign bit to the appropriate location. */
7208
7209 if (GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) >= 0
7210 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
7211 {
7212 int i = -1;
7213
7214 /* If the considered data is wider than HOST_WIDE_INT, we can't
7215 represent a mask for all its bits in a single scalar.
7216 But we only care about the lower bits, so calculate these. */
7217
7218 if (GET_MODE_BITSIZE (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
7219 {
7220 nonzero = ~(HOST_WIDE_INT) 0;
7221
7222 /* GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
7223 is the number of bits a full-width mask would have set.
7224 We need only shift if these are fewer than nonzero can
7225 hold. If not, we must keep all bits set in nonzero. */
7226
7227 if (GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
7228 < HOST_BITS_PER_WIDE_INT)
7229 nonzero >>= INTVAL (XEXP (x, 1))
7230 + HOST_BITS_PER_WIDE_INT
7231 - GET_MODE_BITSIZE (GET_MODE (x)) ;
7232 }
7233 else
7234 {
7235 nonzero = GET_MODE_MASK (GET_MODE (x));
7236 nonzero >>= INTVAL (XEXP (x, 1));
7237 }
7238
7239 if ((mask & ~nonzero) == 0
7240 || (i = exact_log2 (mask)) >= 0)
7241 {
7242 x = simplify_shift_const
7243 (x, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
7244 i < 0 ? INTVAL (XEXP (x, 1))
7245 : GET_MODE_BITSIZE (GET_MODE (x)) - 1 - i);
7246
7247 if (GET_CODE (x) != ASHIFTRT)
7248 return force_to_mode (x, mode, mask, reg, next_select);
7249 }
7250 }
7251
7252 /* If MASK is 1, convert this to an LSHIFTRT. This can be done
7253 even if the shift count isn't a constant. */
7254 if (mask == 1)
7255 x = gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0), XEXP (x, 1));
7256
7257 shiftrt:
7258
7259 /* If this is a zero- or sign-extension operation that just affects bits
7260 we don't care about, remove it. Be sure the call above returned
7261 something that is still a shift. */
7262
7263 if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
7264 && GET_CODE (XEXP (x, 1)) == CONST_INT
7265 && INTVAL (XEXP (x, 1)) >= 0
7266 && (INTVAL (XEXP (x, 1))
7267 <= GET_MODE_BITSIZE (GET_MODE (x)) - (floor_log2 (mask) + 1))
7268 && GET_CODE (XEXP (x, 0)) == ASHIFT
7269 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
7270 && INTVAL (XEXP (XEXP (x, 0), 1)) == INTVAL (XEXP (x, 1)))
7271 return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
7272 reg, next_select);
7273
7274 break;
7275
7276 case ROTATE:
7277 case ROTATERT:
7278 /* If the shift count is constant and we can do computations
7279 in the mode of X, compute where the bits we care about are.
7280 Otherwise, we can't do anything. Don't change the mode of
7281 the shift or propagate MODE into the shift, though. */
7282 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7283 && INTVAL (XEXP (x, 1)) >= 0)
7284 {
7285 temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
7286 GET_MODE (x), GEN_INT (mask),
7287 XEXP (x, 1));
7288 if (temp && GET_CODE (temp) == CONST_INT)
7289 SUBST (XEXP (x, 0),
7290 force_to_mode (XEXP (x, 0), GET_MODE (x),
7291 INTVAL (temp), reg, next_select));
7292 }
7293 break;
7294
7295 case NEG:
7296 /* If we just want the low-order bit, the NEG isn't needed since it
7297 won't change the low-order bit. */
7298 if (mask == 1)
7299 return force_to_mode (XEXP (x, 0), mode, mask, reg, just_select);
7300
7301 /* We need any bits less significant than the most significant bit in
7302 MASK since carries from those bits will affect the bits we are
7303 interested in. */
7304 mask = fuller_mask;
7305 goto unop;
7306
7307 case NOT:
7308 /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
7309 same as the XOR case above. Ensure that the constant we form is not
7310 wider than the mode of X. */
7311
7312 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7313 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
7314 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7315 && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
7316 < GET_MODE_BITSIZE (GET_MODE (x)))
7317 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
7318 {
7319 temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)),
7320 GET_MODE (x));
7321 temp = gen_binary (XOR, GET_MODE (x), XEXP (XEXP (x, 0), 0), temp);
7322 x = gen_binary (LSHIFTRT, GET_MODE (x), temp, XEXP (XEXP (x, 0), 1));
7323
7324 return force_to_mode (x, mode, mask, reg, next_select);
7325 }
7326
7327 /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
7328 use the full mask inside the NOT. */
7329 mask = fuller_mask;
7330
7331 unop:
7332 op0 = gen_lowpart_for_combine (op_mode,
7333 force_to_mode (XEXP (x, 0), mode, mask,
7334 reg, next_select));
7335 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
7336 x = simplify_gen_unary (code, op_mode, op0, op_mode);
7337 break;
7338
7339 case NE:
7340 /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
7341 in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
7342 which is equal to STORE_FLAG_VALUE. */
7343 if ((mask & ~STORE_FLAG_VALUE) == 0 && XEXP (x, 1) == const0_rtx
7344 && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
7345 && (nonzero_bits (XEXP (x, 0), mode)
7346 == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
7347 return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
7348
7349 break;
7350
7351 case IF_THEN_ELSE:
7352 /* We have no way of knowing if the IF_THEN_ELSE can itself be
7353 written in a narrower mode. We play it safe and do not do so. */
7354
7355 SUBST (XEXP (x, 1),
7356 gen_lowpart_for_combine (GET_MODE (x),
7357 force_to_mode (XEXP (x, 1), mode,
7358 mask, reg, next_select)));
7359 SUBST (XEXP (x, 2),
7360 gen_lowpart_for_combine (GET_MODE (x),
7361 force_to_mode (XEXP (x, 2), mode,
7362 mask, reg, next_select)));
7363 break;
7364
7365 default:
7366 break;
7367 }
7368
7369 /* Ensure we return a value of the proper mode. */
7370 return gen_lowpart_for_combine (mode, x);
7371 }
7372 \f
7373 /* Return nonzero if X is an expression that has one of two values depending on
7374 whether some other value is zero or nonzero. In that case, we return the
7375 value that is being tested, *PTRUE is set to the value if the rtx being
7376 returned has a nonzero value, and *PFALSE is set to the other alternative.
7377
7378 If we return zero, we set *PTRUE and *PFALSE to X. */
7379
7380 static rtx
7381 if_then_else_cond (x, ptrue, pfalse)
7382 rtx x;
7383 rtx *ptrue, *pfalse;
7384 {
7385 enum machine_mode mode = GET_MODE (x);
7386 enum rtx_code code = GET_CODE (x);
7387 rtx cond0, cond1, true0, true1, false0, false1;
7388 unsigned HOST_WIDE_INT nz;
7389
7390 /* If we are comparing a value against zero, we are done. */
7391 if ((code == NE || code == EQ)
7392 && GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 0)
7393 {
7394 *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
7395 *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
7396 return XEXP (x, 0);
7397 }
7398
7399 /* If this is a unary operation whose operand has one of two values, apply
7400 our opcode to compute those values. */
7401 else if (GET_RTX_CLASS (code) == '1'
7402 && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
7403 {
7404 *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
7405 *pfalse = simplify_gen_unary (code, mode, false0,
7406 GET_MODE (XEXP (x, 0)));
7407 return cond0;
7408 }
7409
7410 /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
7411 make can't possibly match and would suppress other optimizations. */
7412 else if (code == COMPARE)
7413 ;
7414
7415 /* If this is a binary operation, see if either side has only one of two
7416 values. If either one does or if both do and they are conditional on
7417 the same value, compute the new true and false values. */
7418 else if (GET_RTX_CLASS (code) == 'c' || GET_RTX_CLASS (code) == '2'
7419 || GET_RTX_CLASS (code) == '<')
7420 {
7421 cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
7422 cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
7423
7424 if ((cond0 != 0 || cond1 != 0)
7425 && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
7426 {
7427 /* If if_then_else_cond returned zero, then true/false are the
7428 same rtl. We must copy one of them to prevent invalid rtl
7429 sharing. */
7430 if (cond0 == 0)
7431 true0 = copy_rtx (true0);
7432 else if (cond1 == 0)
7433 true1 = copy_rtx (true1);
7434
7435 *ptrue = gen_binary (code, mode, true0, true1);
7436 *pfalse = gen_binary (code, mode, false0, false1);
7437 return cond0 ? cond0 : cond1;
7438 }
7439
7440 /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
7441 operands is zero when the other is nonzero, and vice-versa,
7442 and STORE_FLAG_VALUE is 1 or -1. */
7443
7444 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
7445 && (code == PLUS || code == IOR || code == XOR || code == MINUS
7446 || code == UMAX)
7447 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
7448 {
7449 rtx op0 = XEXP (XEXP (x, 0), 1);
7450 rtx op1 = XEXP (XEXP (x, 1), 1);
7451
7452 cond0 = XEXP (XEXP (x, 0), 0);
7453 cond1 = XEXP (XEXP (x, 1), 0);
7454
7455 if (GET_RTX_CLASS (GET_CODE (cond0)) == '<'
7456 && GET_RTX_CLASS (GET_CODE (cond1)) == '<'
7457 && ((GET_CODE (cond0) == combine_reversed_comparison_code (cond1)
7458 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
7459 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
7460 || ((swap_condition (GET_CODE (cond0))
7461 == combine_reversed_comparison_code (cond1))
7462 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
7463 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
7464 && ! side_effects_p (x))
7465 {
7466 *ptrue = gen_binary (MULT, mode, op0, const_true_rtx);
7467 *pfalse = gen_binary (MULT, mode,
7468 (code == MINUS
7469 ? simplify_gen_unary (NEG, mode, op1,
7470 mode)
7471 : op1),
7472 const_true_rtx);
7473 return cond0;
7474 }
7475 }
7476
7477 /* Similarly for MULT, AND and UMIN, except that for these the result
7478 is always zero. */
7479 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
7480 && (code == MULT || code == AND || code == UMIN)
7481 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
7482 {
7483 cond0 = XEXP (XEXP (x, 0), 0);
7484 cond1 = XEXP (XEXP (x, 1), 0);
7485
7486 if (GET_RTX_CLASS (GET_CODE (cond0)) == '<'
7487 && GET_RTX_CLASS (GET_CODE (cond1)) == '<'
7488 && ((GET_CODE (cond0) == combine_reversed_comparison_code (cond1)
7489 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
7490 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
7491 || ((swap_condition (GET_CODE (cond0))
7492 == combine_reversed_comparison_code (cond1))
7493 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
7494 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
7495 && ! side_effects_p (x))
7496 {
7497 *ptrue = *pfalse = const0_rtx;
7498 return cond0;
7499 }
7500 }
7501 }
7502
7503 else if (code == IF_THEN_ELSE)
7504 {
7505 /* If we have IF_THEN_ELSE already, extract the condition and
7506 canonicalize it if it is NE or EQ. */
7507 cond0 = XEXP (x, 0);
7508 *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
7509 if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
7510 return XEXP (cond0, 0);
7511 else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
7512 {
7513 *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
7514 return XEXP (cond0, 0);
7515 }
7516 else
7517 return cond0;
7518 }
7519
7520 /* If X is a SUBREG, we can narrow both the true and false values
7521 if the inner expression, if there is a condition. */
7522 else if (code == SUBREG
7523 && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
7524 &true0, &false0)))
7525 {
7526 *ptrue = simplify_gen_subreg (mode, true0,
7527 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
7528 *pfalse = simplify_gen_subreg (mode, false0,
7529 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
7530
7531 return cond0;
7532 }
7533
7534 /* If X is a constant, this isn't special and will cause confusions
7535 if we treat it as such. Likewise if it is equivalent to a constant. */
7536 else if (CONSTANT_P (x)
7537 || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
7538 ;
7539
7540 /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
7541 will be least confusing to the rest of the compiler. */
7542 else if (mode == BImode)
7543 {
7544 *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
7545 return x;
7546 }
7547
7548 /* If X is known to be either 0 or -1, those are the true and
7549 false values when testing X. */
7550 else if (x == constm1_rtx || x == const0_rtx
7551 || (mode != VOIDmode
7552 && num_sign_bit_copies (x, mode) == GET_MODE_BITSIZE (mode)))
7553 {
7554 *ptrue = constm1_rtx, *pfalse = const0_rtx;
7555 return x;
7556 }
7557
7558 /* Likewise for 0 or a single bit. */
7559 else if (mode != VOIDmode
7560 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
7561 && exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
7562 {
7563 *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
7564 return x;
7565 }
7566
7567 /* Otherwise fail; show no condition with true and false values the same. */
7568 *ptrue = *pfalse = x;
7569 return 0;
7570 }
7571 \f
7572 /* Return the value of expression X given the fact that condition COND
7573 is known to be true when applied to REG as its first operand and VAL
7574 as its second. X is known to not be shared and so can be modified in
7575 place.
7576
7577 We only handle the simplest cases, and specifically those cases that
7578 arise with IF_THEN_ELSE expressions. */
7579
7580 static rtx
7581 known_cond (x, cond, reg, val)
7582 rtx x;
7583 enum rtx_code cond;
7584 rtx reg, val;
7585 {
7586 enum rtx_code code = GET_CODE (x);
7587 rtx temp;
7588 const char *fmt;
7589 int i, j;
7590
7591 if (side_effects_p (x))
7592 return x;
7593
7594 /* If either operand of the condition is a floating point value,
7595 then we have to avoid collapsing an EQ comparison. */
7596 if (cond == EQ
7597 && rtx_equal_p (x, reg)
7598 && ! FLOAT_MODE_P (GET_MODE (x))
7599 && ! FLOAT_MODE_P (GET_MODE (val)))
7600 return val;
7601
7602 if (cond == UNEQ && rtx_equal_p (x, reg))
7603 return val;
7604
7605 /* If X is (abs REG) and we know something about REG's relationship
7606 with zero, we may be able to simplify this. */
7607
7608 if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
7609 switch (cond)
7610 {
7611 case GE: case GT: case EQ:
7612 return XEXP (x, 0);
7613 case LT: case LE:
7614 return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
7615 XEXP (x, 0),
7616 GET_MODE (XEXP (x, 0)));
7617 default:
7618 break;
7619 }
7620
7621 /* The only other cases we handle are MIN, MAX, and comparisons if the
7622 operands are the same as REG and VAL. */
7623
7624 else if (GET_RTX_CLASS (code) == '<' || GET_RTX_CLASS (code) == 'c')
7625 {
7626 if (rtx_equal_p (XEXP (x, 0), val))
7627 cond = swap_condition (cond), temp = val, val = reg, reg = temp;
7628
7629 if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
7630 {
7631 if (GET_RTX_CLASS (code) == '<')
7632 {
7633 if (comparison_dominates_p (cond, code))
7634 return const_true_rtx;
7635
7636 code = combine_reversed_comparison_code (x);
7637 if (code != UNKNOWN
7638 && comparison_dominates_p (cond, code))
7639 return const0_rtx;
7640 else
7641 return x;
7642 }
7643 else if (code == SMAX || code == SMIN
7644 || code == UMIN || code == UMAX)
7645 {
7646 int unsignedp = (code == UMIN || code == UMAX);
7647
7648 /* Do not reverse the condition when it is NE or EQ.
7649 This is because we cannot conclude anything about
7650 the value of 'SMAX (x, y)' when x is not equal to y,
7651 but we can when x equals y. */
7652 if ((code == SMAX || code == UMAX)
7653 && ! (cond == EQ || cond == NE))
7654 cond = reverse_condition (cond);
7655
7656 switch (cond)
7657 {
7658 case GE: case GT:
7659 return unsignedp ? x : XEXP (x, 1);
7660 case LE: case LT:
7661 return unsignedp ? x : XEXP (x, 0);
7662 case GEU: case GTU:
7663 return unsignedp ? XEXP (x, 1) : x;
7664 case LEU: case LTU:
7665 return unsignedp ? XEXP (x, 0) : x;
7666 default:
7667 break;
7668 }
7669 }
7670 }
7671 }
7672 else if (code == SUBREG)
7673 {
7674 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
7675 rtx new, r = known_cond (SUBREG_REG (x), cond, reg, val);
7676
7677 if (SUBREG_REG (x) != r)
7678 {
7679 /* We must simplify subreg here, before we lose track of the
7680 original inner_mode. */
7681 new = simplify_subreg (GET_MODE (x), r,
7682 inner_mode, SUBREG_BYTE (x));
7683 if (new)
7684 return new;
7685 else
7686 SUBST (SUBREG_REG (x), r);
7687 }
7688
7689 return x;
7690 }
7691 /* We don't have to handle SIGN_EXTEND here, because even in the
7692 case of replacing something with a modeless CONST_INT, a
7693 CONST_INT is already (supposed to be) a valid sign extension for
7694 its narrower mode, which implies it's already properly
7695 sign-extended for the wider mode. Now, for ZERO_EXTEND, the
7696 story is different. */
7697 else if (code == ZERO_EXTEND)
7698 {
7699 enum machine_mode inner_mode = GET_MODE (XEXP (x, 0));
7700 rtx new, r = known_cond (XEXP (x, 0), cond, reg, val);
7701
7702 if (XEXP (x, 0) != r)
7703 {
7704 /* We must simplify the zero_extend here, before we lose
7705 track of the original inner_mode. */
7706 new = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
7707 r, inner_mode);
7708 if (new)
7709 return new;
7710 else
7711 SUBST (XEXP (x, 0), r);
7712 }
7713
7714 return x;
7715 }
7716
7717 fmt = GET_RTX_FORMAT (code);
7718 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7719 {
7720 if (fmt[i] == 'e')
7721 SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
7722 else if (fmt[i] == 'E')
7723 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
7724 SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
7725 cond, reg, val));
7726 }
7727
7728 return x;
7729 }
7730 \f
7731 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
7732 assignment as a field assignment. */
7733
7734 static int
7735 rtx_equal_for_field_assignment_p (x, y)
7736 rtx x;
7737 rtx y;
7738 {
7739 if (x == y || rtx_equal_p (x, y))
7740 return 1;
7741
7742 if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
7743 return 0;
7744
7745 /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
7746 Note that all SUBREGs of MEM are paradoxical; otherwise they
7747 would have been rewritten. */
7748 if (GET_CODE (x) == MEM && GET_CODE (y) == SUBREG
7749 && GET_CODE (SUBREG_REG (y)) == MEM
7750 && rtx_equal_p (SUBREG_REG (y),
7751 gen_lowpart_for_combine (GET_MODE (SUBREG_REG (y)), x)))
7752 return 1;
7753
7754 if (GET_CODE (y) == MEM && GET_CODE (x) == SUBREG
7755 && GET_CODE (SUBREG_REG (x)) == MEM
7756 && rtx_equal_p (SUBREG_REG (x),
7757 gen_lowpart_for_combine (GET_MODE (SUBREG_REG (x)), y)))
7758 return 1;
7759
7760 /* We used to see if get_last_value of X and Y were the same but that's
7761 not correct. In one direction, we'll cause the assignment to have
7762 the wrong destination and in the case, we'll import a register into this
7763 insn that might have already have been dead. So fail if none of the
7764 above cases are true. */
7765 return 0;
7766 }
7767 \f
7768 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
7769 Return that assignment if so.
7770
7771 We only handle the most common cases. */
7772
7773 static rtx
7774 make_field_assignment (x)
7775 rtx x;
7776 {
7777 rtx dest = SET_DEST (x);
7778 rtx src = SET_SRC (x);
7779 rtx assign;
7780 rtx rhs, lhs;
7781 HOST_WIDE_INT c1;
7782 HOST_WIDE_INT pos;
7783 unsigned HOST_WIDE_INT len;
7784 rtx other;
7785 enum machine_mode mode;
7786
7787 /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
7788 a clear of a one-bit field. We will have changed it to
7789 (and (rotate (const_int -2) POS) DEST), so check for that. Also check
7790 for a SUBREG. */
7791
7792 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
7793 && GET_CODE (XEXP (XEXP (src, 0), 0)) == CONST_INT
7794 && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
7795 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7796 {
7797 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
7798 1, 1, 1, 0);
7799 if (assign != 0)
7800 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
7801 return x;
7802 }
7803
7804 else if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
7805 && subreg_lowpart_p (XEXP (src, 0))
7806 && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
7807 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
7808 && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
7809 && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
7810 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7811 {
7812 assign = make_extraction (VOIDmode, dest, 0,
7813 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
7814 1, 1, 1, 0);
7815 if (assign != 0)
7816 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
7817 return x;
7818 }
7819
7820 /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
7821 one-bit field. */
7822 else if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
7823 && XEXP (XEXP (src, 0), 0) == const1_rtx
7824 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7825 {
7826 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
7827 1, 1, 1, 0);
7828 if (assign != 0)
7829 return gen_rtx_SET (VOIDmode, assign, const1_rtx);
7830 return x;
7831 }
7832
7833 /* The other case we handle is assignments into a constant-position
7834 field. They look like (ior/xor (and DEST C1) OTHER). If C1 represents
7835 a mask that has all one bits except for a group of zero bits and
7836 OTHER is known to have zeros where C1 has ones, this is such an
7837 assignment. Compute the position and length from C1. Shift OTHER
7838 to the appropriate position, force it to the required mode, and
7839 make the extraction. Check for the AND in both operands. */
7840
7841 if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
7842 return x;
7843
7844 rhs = expand_compound_operation (XEXP (src, 0));
7845 lhs = expand_compound_operation (XEXP (src, 1));
7846
7847 if (GET_CODE (rhs) == AND
7848 && GET_CODE (XEXP (rhs, 1)) == CONST_INT
7849 && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
7850 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
7851 else if (GET_CODE (lhs) == AND
7852 && GET_CODE (XEXP (lhs, 1)) == CONST_INT
7853 && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
7854 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
7855 else
7856 return x;
7857
7858 pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
7859 if (pos < 0 || pos + len > GET_MODE_BITSIZE (GET_MODE (dest))
7860 || GET_MODE_BITSIZE (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
7861 || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
7862 return x;
7863
7864 assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
7865 if (assign == 0)
7866 return x;
7867
7868 /* The mode to use for the source is the mode of the assignment, or of
7869 what is inside a possible STRICT_LOW_PART. */
7870 mode = (GET_CODE (assign) == STRICT_LOW_PART
7871 ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
7872
7873 /* Shift OTHER right POS places and make it the source, restricting it
7874 to the proper length and mode. */
7875
7876 src = force_to_mode (simplify_shift_const (NULL_RTX, LSHIFTRT,
7877 GET_MODE (src), other, pos),
7878 mode,
7879 GET_MODE_BITSIZE (mode) >= HOST_BITS_PER_WIDE_INT
7880 ? ~(unsigned HOST_WIDE_INT) 0
7881 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
7882 dest, 0);
7883
7884 /* If SRC is masked by an AND that does not make a difference in
7885 the value being stored, strip it. */
7886 if (GET_CODE (assign) == ZERO_EXTRACT
7887 && GET_CODE (XEXP (assign, 1)) == CONST_INT
7888 && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
7889 && GET_CODE (src) == AND
7890 && GET_CODE (XEXP (src, 1)) == CONST_INT
7891 && ((unsigned HOST_WIDE_INT) INTVAL (XEXP (src, 1))
7892 == ((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (assign, 1))) - 1))
7893 src = XEXP (src, 0);
7894
7895 return gen_rtx_SET (VOIDmode, assign, src);
7896 }
7897 \f
7898 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
7899 if so. */
7900
7901 static rtx
7902 apply_distributive_law (x)
7903 rtx x;
7904 {
7905 enum rtx_code code = GET_CODE (x);
7906 rtx lhs, rhs, other;
7907 rtx tem;
7908 enum rtx_code inner_code;
7909
7910 /* Distributivity is not true for floating point.
7911 It can change the value. So don't do it.
7912 -- rms and moshier@world.std.com. */
7913 if (FLOAT_MODE_P (GET_MODE (x)))
7914 return x;
7915
7916 /* The outer operation can only be one of the following: */
7917 if (code != IOR && code != AND && code != XOR
7918 && code != PLUS && code != MINUS)
7919 return x;
7920
7921 lhs = XEXP (x, 0), rhs = XEXP (x, 1);
7922
7923 /* If either operand is a primitive we can't do anything, so get out
7924 fast. */
7925 if (GET_RTX_CLASS (GET_CODE (lhs)) == 'o'
7926 || GET_RTX_CLASS (GET_CODE (rhs)) == 'o')
7927 return x;
7928
7929 lhs = expand_compound_operation (lhs);
7930 rhs = expand_compound_operation (rhs);
7931 inner_code = GET_CODE (lhs);
7932 if (inner_code != GET_CODE (rhs))
7933 return x;
7934
7935 /* See if the inner and outer operations distribute. */
7936 switch (inner_code)
7937 {
7938 case LSHIFTRT:
7939 case ASHIFTRT:
7940 case AND:
7941 case IOR:
7942 /* These all distribute except over PLUS. */
7943 if (code == PLUS || code == MINUS)
7944 return x;
7945 break;
7946
7947 case MULT:
7948 if (code != PLUS && code != MINUS)
7949 return x;
7950 break;
7951
7952 case ASHIFT:
7953 /* This is also a multiply, so it distributes over everything. */
7954 break;
7955
7956 case SUBREG:
7957 /* Non-paradoxical SUBREGs distributes over all operations, provided
7958 the inner modes and byte offsets are the same, this is an extraction
7959 of a low-order part, we don't convert an fp operation to int or
7960 vice versa, and we would not be converting a single-word
7961 operation into a multi-word operation. The latter test is not
7962 required, but it prevents generating unneeded multi-word operations.
7963 Some of the previous tests are redundant given the latter test, but
7964 are retained because they are required for correctness.
7965
7966 We produce the result slightly differently in this case. */
7967
7968 if (GET_MODE (SUBREG_REG (lhs)) != GET_MODE (SUBREG_REG (rhs))
7969 || SUBREG_BYTE (lhs) != SUBREG_BYTE (rhs)
7970 || ! subreg_lowpart_p (lhs)
7971 || (GET_MODE_CLASS (GET_MODE (lhs))
7972 != GET_MODE_CLASS (GET_MODE (SUBREG_REG (lhs))))
7973 || (GET_MODE_SIZE (GET_MODE (lhs))
7974 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))))
7975 || GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))) > UNITS_PER_WORD)
7976 return x;
7977
7978 tem = gen_binary (code, GET_MODE (SUBREG_REG (lhs)),
7979 SUBREG_REG (lhs), SUBREG_REG (rhs));
7980 return gen_lowpart_for_combine (GET_MODE (x), tem);
7981
7982 default:
7983 return x;
7984 }
7985
7986 /* Set LHS and RHS to the inner operands (A and B in the example
7987 above) and set OTHER to the common operand (C in the example).
7988 These is only one way to do this unless the inner operation is
7989 commutative. */
7990 if (GET_RTX_CLASS (inner_code) == 'c'
7991 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
7992 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
7993 else if (GET_RTX_CLASS (inner_code) == 'c'
7994 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
7995 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
7996 else if (GET_RTX_CLASS (inner_code) == 'c'
7997 && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
7998 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
7999 else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
8000 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
8001 else
8002 return x;
8003
8004 /* Form the new inner operation, seeing if it simplifies first. */
8005 tem = gen_binary (code, GET_MODE (x), lhs, rhs);
8006
8007 /* There is one exception to the general way of distributing:
8008 (a ^ b) | (a ^ c) -> (~a) & (b ^ c) */
8009 if (code == XOR && inner_code == IOR)
8010 {
8011 inner_code = AND;
8012 other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
8013 }
8014
8015 /* We may be able to continuing distributing the result, so call
8016 ourselves recursively on the inner operation before forming the
8017 outer operation, which we return. */
8018 return gen_binary (inner_code, GET_MODE (x),
8019 apply_distributive_law (tem), other);
8020 }
8021 \f
8022 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
8023 in MODE.
8024
8025 Return an equivalent form, if different from X. Otherwise, return X. If
8026 X is zero, we are to always construct the equivalent form. */
8027
8028 static rtx
8029 simplify_and_const_int (x, mode, varop, constop)
8030 rtx x;
8031 enum machine_mode mode;
8032 rtx varop;
8033 unsigned HOST_WIDE_INT constop;
8034 {
8035 unsigned HOST_WIDE_INT nonzero;
8036 int i;
8037
8038 /* Simplify VAROP knowing that we will be only looking at some of the
8039 bits in it.
8040
8041 Note by passing in CONSTOP, we guarantee that the bits not set in
8042 CONSTOP are not significant and will never be examined. We must
8043 ensure that is the case by explicitly masking out those bits
8044 before returning. */
8045 varop = force_to_mode (varop, mode, constop, NULL_RTX, 0);
8046
8047 /* If VAROP is a CLOBBER, we will fail so return it. */
8048 if (GET_CODE (varop) == CLOBBER)
8049 return varop;
8050
8051 /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
8052 to VAROP and return the new constant. */
8053 if (GET_CODE (varop) == CONST_INT)
8054 return GEN_INT (trunc_int_for_mode (INTVAL (varop) & constop, mode));
8055
8056 /* See what bits may be nonzero in VAROP. Unlike the general case of
8057 a call to nonzero_bits, here we don't care about bits outside
8058 MODE. */
8059
8060 nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
8061
8062 /* Turn off all bits in the constant that are known to already be zero.
8063 Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
8064 which is tested below. */
8065
8066 constop &= nonzero;
8067
8068 /* If we don't have any bits left, return zero. */
8069 if (constop == 0)
8070 return const0_rtx;
8071
8072 /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
8073 a power of two, we can replace this with an ASHIFT. */
8074 if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
8075 && (i = exact_log2 (constop)) >= 0)
8076 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
8077
8078 /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
8079 or XOR, then try to apply the distributive law. This may eliminate
8080 operations if either branch can be simplified because of the AND.
8081 It may also make some cases more complex, but those cases probably
8082 won't match a pattern either with or without this. */
8083
8084 if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
8085 return
8086 gen_lowpart_for_combine
8087 (mode,
8088 apply_distributive_law
8089 (gen_binary (GET_CODE (varop), GET_MODE (varop),
8090 simplify_and_const_int (NULL_RTX, GET_MODE (varop),
8091 XEXP (varop, 0), constop),
8092 simplify_and_const_int (NULL_RTX, GET_MODE (varop),
8093 XEXP (varop, 1), constop))));
8094
8095 /* If VAROP is PLUS, and the constant is a mask of low bite, distribute
8096 the AND and see if one of the operands simplifies to zero. If so, we
8097 may eliminate it. */
8098
8099 if (GET_CODE (varop) == PLUS
8100 && exact_log2 (constop + 1) >= 0)
8101 {
8102 rtx o0, o1;
8103
8104 o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
8105 o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
8106 if (o0 == const0_rtx)
8107 return o1;
8108 if (o1 == const0_rtx)
8109 return o0;
8110 }
8111
8112 /* Get VAROP in MODE. Try to get a SUBREG if not. Don't make a new SUBREG
8113 if we already had one (just check for the simplest cases). */
8114 if (x && GET_CODE (XEXP (x, 0)) == SUBREG
8115 && GET_MODE (XEXP (x, 0)) == mode
8116 && SUBREG_REG (XEXP (x, 0)) == varop)
8117 varop = XEXP (x, 0);
8118 else
8119 varop = gen_lowpart_for_combine (mode, varop);
8120
8121 /* If we can't make the SUBREG, try to return what we were given. */
8122 if (GET_CODE (varop) == CLOBBER)
8123 return x ? x : varop;
8124
8125 /* If we are only masking insignificant bits, return VAROP. */
8126 if (constop == nonzero)
8127 x = varop;
8128 else
8129 {
8130 /* Otherwise, return an AND. */
8131 constop = trunc_int_for_mode (constop, mode);
8132 /* See how much, if any, of X we can use. */
8133 if (x == 0 || GET_CODE (x) != AND || GET_MODE (x) != mode)
8134 x = gen_binary (AND, mode, varop, GEN_INT (constop));
8135
8136 else
8137 {
8138 if (GET_CODE (XEXP (x, 1)) != CONST_INT
8139 || (unsigned HOST_WIDE_INT) INTVAL (XEXP (x, 1)) != constop)
8140 SUBST (XEXP (x, 1), GEN_INT (constop));
8141
8142 SUBST (XEXP (x, 0), varop);
8143 }
8144 }
8145
8146 return x;
8147 }
8148 \f
8149 #define nonzero_bits_with_known(X, MODE) \
8150 cached_nonzero_bits (X, MODE, known_x, known_mode, known_ret)
8151
8152 /* The function cached_nonzero_bits is a wrapper around nonzero_bits1.
8153 It avoids exponential behavior in nonzero_bits1 when X has
8154 identical subexpressions on the first or the second level. */
8155
8156 static unsigned HOST_WIDE_INT
8157 cached_nonzero_bits (x, mode, known_x, known_mode, known_ret)
8158 rtx x;
8159 enum machine_mode mode;
8160 rtx known_x;
8161 enum machine_mode known_mode;
8162 unsigned HOST_WIDE_INT known_ret;
8163 {
8164 if (x == known_x && mode == known_mode)
8165 return known_ret;
8166
8167 /* Try to find identical subexpressions. If found call
8168 nonzero_bits1 on X with the subexpressions as KNOWN_X and the
8169 precomputed value for the subexpression as KNOWN_RET. */
8170
8171 if (GET_RTX_CLASS (GET_CODE (x)) == '2'
8172 || GET_RTX_CLASS (GET_CODE (x)) == 'c')
8173 {
8174 rtx x0 = XEXP (x, 0);
8175 rtx x1 = XEXP (x, 1);
8176
8177 /* Check the first level. */
8178 if (x0 == x1)
8179 return nonzero_bits1 (x, mode, x0, mode,
8180 nonzero_bits_with_known (x0, mode));
8181
8182 /* Check the second level. */
8183 if ((GET_RTX_CLASS (GET_CODE (x0)) == '2'
8184 || GET_RTX_CLASS (GET_CODE (x0)) == 'c')
8185 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
8186 return nonzero_bits1 (x, mode, x1, mode,
8187 nonzero_bits_with_known (x1, mode));
8188
8189 if ((GET_RTX_CLASS (GET_CODE (x1)) == '2'
8190 || GET_RTX_CLASS (GET_CODE (x1)) == 'c')
8191 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
8192 return nonzero_bits1 (x, mode, x0, mode,
8193 nonzero_bits_with_known (x0, mode));
8194 }
8195
8196 return nonzero_bits1 (x, mode, known_x, known_mode, known_ret);
8197 }
8198
8199 /* We let num_sign_bit_copies recur into nonzero_bits as that is useful.
8200 We don't let nonzero_bits recur into num_sign_bit_copies, because that
8201 is less useful. We can't allow both, because that results in exponential
8202 run time recursion. There is a nullstone testcase that triggered
8203 this. This macro avoids accidental uses of num_sign_bit_copies. */
8204 #define cached_num_sign_bit_copies()
8205
8206 /* Given an expression, X, compute which bits in X can be nonzero.
8207 We don't care about bits outside of those defined in MODE.
8208
8209 For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
8210 a shift, AND, or zero_extract, we can do better. */
8211
8212 static unsigned HOST_WIDE_INT
8213 nonzero_bits1 (x, mode, known_x, known_mode, known_ret)
8214 rtx x;
8215 enum machine_mode mode;
8216 rtx known_x;
8217 enum machine_mode known_mode;
8218 unsigned HOST_WIDE_INT known_ret;
8219 {
8220 unsigned HOST_WIDE_INT nonzero = GET_MODE_MASK (mode);
8221 unsigned HOST_WIDE_INT inner_nz;
8222 enum rtx_code code;
8223 unsigned int mode_width = GET_MODE_BITSIZE (mode);
8224 rtx tem;
8225
8226 /* For floating-point values, assume all bits are needed. */
8227 if (FLOAT_MODE_P (GET_MODE (x)) || FLOAT_MODE_P (mode))
8228 return nonzero;
8229
8230 /* If X is wider than MODE, use its mode instead. */
8231 if (GET_MODE_BITSIZE (GET_MODE (x)) > mode_width)
8232 {
8233 mode = GET_MODE (x);
8234 nonzero = GET_MODE_MASK (mode);
8235 mode_width = GET_MODE_BITSIZE (mode);
8236 }
8237
8238 if (mode_width > HOST_BITS_PER_WIDE_INT)
8239 /* Our only callers in this case look for single bit values. So
8240 just return the mode mask. Those tests will then be false. */
8241 return nonzero;
8242
8243 #ifndef WORD_REGISTER_OPERATIONS
8244 /* If MODE is wider than X, but both are a single word for both the host
8245 and target machines, we can compute this from which bits of the
8246 object might be nonzero in its own mode, taking into account the fact
8247 that on many CISC machines, accessing an object in a wider mode
8248 causes the high-order bits to become undefined. So they are
8249 not known to be zero. */
8250
8251 if (GET_MODE (x) != VOIDmode && GET_MODE (x) != mode
8252 && GET_MODE_BITSIZE (GET_MODE (x)) <= BITS_PER_WORD
8253 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
8254 && GET_MODE_BITSIZE (mode) > GET_MODE_BITSIZE (GET_MODE (x)))
8255 {
8256 nonzero &= nonzero_bits_with_known (x, GET_MODE (x));
8257 nonzero |= GET_MODE_MASK (mode) & ~GET_MODE_MASK (GET_MODE (x));
8258 return nonzero;
8259 }
8260 #endif
8261
8262 code = GET_CODE (x);
8263 switch (code)
8264 {
8265 case REG:
8266 #if defined(POINTERS_EXTEND_UNSIGNED) && !defined(HAVE_ptr_extend)
8267 /* If pointers extend unsigned and this is a pointer in Pmode, say that
8268 all the bits above ptr_mode are known to be zero. */
8269 if (POINTERS_EXTEND_UNSIGNED && GET_MODE (x) == Pmode
8270 && REG_POINTER (x))
8271 nonzero &= GET_MODE_MASK (ptr_mode);
8272 #endif
8273
8274 /* Include declared information about alignment of pointers. */
8275 /* ??? We don't properly preserve REG_POINTER changes across
8276 pointer-to-integer casts, so we can't trust it except for
8277 things that we know must be pointers. See execute/960116-1.c. */
8278 if ((x == stack_pointer_rtx
8279 || x == frame_pointer_rtx
8280 || x == arg_pointer_rtx)
8281 && REGNO_POINTER_ALIGN (REGNO (x)))
8282 {
8283 unsigned HOST_WIDE_INT alignment
8284 = REGNO_POINTER_ALIGN (REGNO (x)) / BITS_PER_UNIT;
8285
8286 #ifdef PUSH_ROUNDING
8287 /* If PUSH_ROUNDING is defined, it is possible for the
8288 stack to be momentarily aligned only to that amount,
8289 so we pick the least alignment. */
8290 if (x == stack_pointer_rtx && PUSH_ARGS)
8291 alignment = MIN (PUSH_ROUNDING (1), alignment);
8292 #endif
8293
8294 nonzero &= ~(alignment - 1);
8295 }
8296
8297 /* If X is a register whose nonzero bits value is current, use it.
8298 Otherwise, if X is a register whose value we can find, use that
8299 value. Otherwise, use the previously-computed global nonzero bits
8300 for this register. */
8301
8302 if (reg_last_set_value[REGNO (x)] != 0
8303 && (reg_last_set_mode[REGNO (x)] == mode
8304 || (GET_MODE_CLASS (reg_last_set_mode[REGNO (x)]) == MODE_INT
8305 && GET_MODE_CLASS (mode) == MODE_INT))
8306 && (reg_last_set_label[REGNO (x)] == label_tick
8307 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
8308 && REG_N_SETS (REGNO (x)) == 1
8309 && ! REGNO_REG_SET_P (ENTRY_BLOCK_PTR->next_bb->global_live_at_start,
8310 REGNO (x))))
8311 && INSN_CUID (reg_last_set[REGNO (x)]) < subst_low_cuid)
8312 return reg_last_set_nonzero_bits[REGNO (x)] & nonzero;
8313
8314 tem = get_last_value (x);
8315
8316 if (tem)
8317 {
8318 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
8319 /* If X is narrower than MODE and TEM is a non-negative
8320 constant that would appear negative in the mode of X,
8321 sign-extend it for use in reg_nonzero_bits because some
8322 machines (maybe most) will actually do the sign-extension
8323 and this is the conservative approach.
8324
8325 ??? For 2.5, try to tighten up the MD files in this regard
8326 instead of this kludge. */
8327
8328 if (GET_MODE_BITSIZE (GET_MODE (x)) < mode_width
8329 && GET_CODE (tem) == CONST_INT
8330 && INTVAL (tem) > 0
8331 && 0 != (INTVAL (tem)
8332 & ((HOST_WIDE_INT) 1
8333 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
8334 tem = GEN_INT (INTVAL (tem)
8335 | ((HOST_WIDE_INT) (-1)
8336 << GET_MODE_BITSIZE (GET_MODE (x))));
8337 #endif
8338 return nonzero_bits_with_known (tem, mode) & nonzero;
8339 }
8340 else if (nonzero_sign_valid && reg_nonzero_bits[REGNO (x)])
8341 {
8342 unsigned HOST_WIDE_INT mask = reg_nonzero_bits[REGNO (x)];
8343
8344 if (GET_MODE_BITSIZE (GET_MODE (x)) < mode_width)
8345 /* We don't know anything about the upper bits. */
8346 mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (GET_MODE (x));
8347 return nonzero & mask;
8348 }
8349 else
8350 return nonzero;
8351
8352 case CONST_INT:
8353 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
8354 /* If X is negative in MODE, sign-extend the value. */
8355 if (INTVAL (x) > 0 && mode_width < BITS_PER_WORD
8356 && 0 != (INTVAL (x) & ((HOST_WIDE_INT) 1 << (mode_width - 1))))
8357 return (INTVAL (x) | ((HOST_WIDE_INT) (-1) << mode_width));
8358 #endif
8359
8360 return INTVAL (x);
8361
8362 case MEM:
8363 #ifdef LOAD_EXTEND_OP
8364 /* In many, if not most, RISC machines, reading a byte from memory
8365 zeros the rest of the register. Noticing that fact saves a lot
8366 of extra zero-extends. */
8367 if (LOAD_EXTEND_OP (GET_MODE (x)) == ZERO_EXTEND)
8368 nonzero &= GET_MODE_MASK (GET_MODE (x));
8369 #endif
8370 break;
8371
8372 case EQ: case NE:
8373 case UNEQ: case LTGT:
8374 case GT: case GTU: case UNGT:
8375 case LT: case LTU: case UNLT:
8376 case GE: case GEU: case UNGE:
8377 case LE: case LEU: case UNLE:
8378 case UNORDERED: case ORDERED:
8379
8380 /* If this produces an integer result, we know which bits are set.
8381 Code here used to clear bits outside the mode of X, but that is
8382 now done above. */
8383
8384 if (GET_MODE_CLASS (mode) == MODE_INT
8385 && mode_width <= HOST_BITS_PER_WIDE_INT)
8386 nonzero = STORE_FLAG_VALUE;
8387 break;
8388
8389 case NEG:
8390 #if 0
8391 /* Disabled to avoid exponential mutual recursion between nonzero_bits
8392 and num_sign_bit_copies. */
8393 if (num_sign_bit_copies (XEXP (x, 0), GET_MODE (x))
8394 == GET_MODE_BITSIZE (GET_MODE (x)))
8395 nonzero = 1;
8396 #endif
8397
8398 if (GET_MODE_SIZE (GET_MODE (x)) < mode_width)
8399 nonzero |= (GET_MODE_MASK (mode) & ~GET_MODE_MASK (GET_MODE (x)));
8400 break;
8401
8402 case ABS:
8403 #if 0
8404 /* Disabled to avoid exponential mutual recursion between nonzero_bits
8405 and num_sign_bit_copies. */
8406 if (num_sign_bit_copies (XEXP (x, 0), GET_MODE (x))
8407 == GET_MODE_BITSIZE (GET_MODE (x)))
8408 nonzero = 1;
8409 #endif
8410 break;
8411
8412 case TRUNCATE:
8413 nonzero &= (nonzero_bits_with_known (XEXP (x, 0), mode)
8414 & GET_MODE_MASK (mode));
8415 break;
8416
8417 case ZERO_EXTEND:
8418 nonzero &= nonzero_bits_with_known (XEXP (x, 0), mode);
8419 if (GET_MODE (XEXP (x, 0)) != VOIDmode)
8420 nonzero &= GET_MODE_MASK (GET_MODE (XEXP (x, 0)));
8421 break;
8422
8423 case SIGN_EXTEND:
8424 /* If the sign bit is known clear, this is the same as ZERO_EXTEND.
8425 Otherwise, show all the bits in the outer mode but not the inner
8426 may be nonzero. */
8427 inner_nz = nonzero_bits_with_known (XEXP (x, 0), mode);
8428 if (GET_MODE (XEXP (x, 0)) != VOIDmode)
8429 {
8430 inner_nz &= GET_MODE_MASK (GET_MODE (XEXP (x, 0)));
8431 if (inner_nz
8432 & (((HOST_WIDE_INT) 1
8433 << (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - 1))))
8434 inner_nz |= (GET_MODE_MASK (mode)
8435 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0))));
8436 }
8437
8438 nonzero &= inner_nz;
8439 break;
8440
8441 case AND:
8442 nonzero &= (nonzero_bits_with_known (XEXP (x, 0), mode)
8443 & nonzero_bits_with_known (XEXP (x, 1), mode));
8444 break;
8445
8446 case XOR: case IOR:
8447 case UMIN: case UMAX: case SMIN: case SMAX:
8448 {
8449 unsigned HOST_WIDE_INT nonzero0 =
8450 nonzero_bits_with_known (XEXP (x, 0), mode);
8451
8452 /* Don't call nonzero_bits for the second time if it cannot change
8453 anything. */
8454 if ((nonzero & nonzero0) != nonzero)
8455 nonzero &= (nonzero0
8456 | nonzero_bits_with_known (XEXP (x, 1), mode));
8457 }
8458 break;
8459
8460 case PLUS: case MINUS:
8461 case MULT:
8462 case DIV: case UDIV:
8463 case MOD: case UMOD:
8464 /* We can apply the rules of arithmetic to compute the number of
8465 high- and low-order zero bits of these operations. We start by
8466 computing the width (position of the highest-order nonzero bit)
8467 and the number of low-order zero bits for each value. */
8468 {
8469 unsigned HOST_WIDE_INT nz0 =
8470 nonzero_bits_with_known (XEXP (x, 0), mode);
8471 unsigned HOST_WIDE_INT nz1 =
8472 nonzero_bits_with_known (XEXP (x, 1), mode);
8473 int sign_index = GET_MODE_BITSIZE (GET_MODE (x)) - 1;
8474 int width0 = floor_log2 (nz0) + 1;
8475 int width1 = floor_log2 (nz1) + 1;
8476 int low0 = floor_log2 (nz0 & -nz0);
8477 int low1 = floor_log2 (nz1 & -nz1);
8478 HOST_WIDE_INT op0_maybe_minusp
8479 = (nz0 & ((HOST_WIDE_INT) 1 << sign_index));
8480 HOST_WIDE_INT op1_maybe_minusp
8481 = (nz1 & ((HOST_WIDE_INT) 1 << sign_index));
8482 unsigned int result_width = mode_width;
8483 int result_low = 0;
8484
8485 switch (code)
8486 {
8487 case PLUS:
8488 result_width = MAX (width0, width1) + 1;
8489 result_low = MIN (low0, low1);
8490 break;
8491 case MINUS:
8492 result_low = MIN (low0, low1);
8493 break;
8494 case MULT:
8495 result_width = width0 + width1;
8496 result_low = low0 + low1;
8497 break;
8498 case DIV:
8499 if (width1 == 0)
8500 break;
8501 if (! op0_maybe_minusp && ! op1_maybe_minusp)
8502 result_width = width0;
8503 break;
8504 case UDIV:
8505 if (width1 == 0)
8506 break;
8507 result_width = width0;
8508 break;
8509 case MOD:
8510 if (width1 == 0)
8511 break;
8512 if (! op0_maybe_minusp && ! op1_maybe_minusp)
8513 result_width = MIN (width0, width1);
8514 result_low = MIN (low0, low1);
8515 break;
8516 case UMOD:
8517 if (width1 == 0)
8518 break;
8519 result_width = MIN (width0, width1);
8520 result_low = MIN (low0, low1);
8521 break;
8522 default:
8523 abort ();
8524 }
8525
8526 if (result_width < mode_width)
8527 nonzero &= ((HOST_WIDE_INT) 1 << result_width) - 1;
8528
8529 if (result_low > 0)
8530 nonzero &= ~(((HOST_WIDE_INT) 1 << result_low) - 1);
8531
8532 #ifdef POINTERS_EXTEND_UNSIGNED
8533 /* If pointers extend unsigned and this is an addition or subtraction
8534 to a pointer in Pmode, all the bits above ptr_mode are known to be
8535 zero. */
8536 if (POINTERS_EXTEND_UNSIGNED > 0 && GET_MODE (x) == Pmode
8537 && (code == PLUS || code == MINUS)
8538 && GET_CODE (XEXP (x, 0)) == REG && REG_POINTER (XEXP (x, 0)))
8539 nonzero &= GET_MODE_MASK (ptr_mode);
8540 #endif
8541 }
8542 break;
8543
8544 case ZERO_EXTRACT:
8545 if (GET_CODE (XEXP (x, 1)) == CONST_INT
8546 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
8547 nonzero &= ((HOST_WIDE_INT) 1 << INTVAL (XEXP (x, 1))) - 1;
8548 break;
8549
8550 case SUBREG:
8551 /* If this is a SUBREG formed for a promoted variable that has
8552 been zero-extended, we know that at least the high-order bits
8553 are zero, though others might be too. */
8554
8555 if (SUBREG_PROMOTED_VAR_P (x) && SUBREG_PROMOTED_UNSIGNED_P (x) > 0)
8556 nonzero = (GET_MODE_MASK (GET_MODE (x))
8557 & nonzero_bits_with_known (SUBREG_REG (x), GET_MODE (x)));
8558
8559 /* If the inner mode is a single word for both the host and target
8560 machines, we can compute this from which bits of the inner
8561 object might be nonzero. */
8562 if (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))) <= BITS_PER_WORD
8563 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x)))
8564 <= HOST_BITS_PER_WIDE_INT))
8565 {
8566 nonzero &= nonzero_bits_with_known (SUBREG_REG (x), mode);
8567
8568 #if defined (WORD_REGISTER_OPERATIONS) && defined (LOAD_EXTEND_OP)
8569 /* If this is a typical RISC machine, we only have to worry
8570 about the way loads are extended. */
8571 if ((LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (x))) == SIGN_EXTEND
8572 ? (((nonzero
8573 & (((unsigned HOST_WIDE_INT) 1
8574 << (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))) - 1))))
8575 != 0))
8576 : LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (x))) != ZERO_EXTEND)
8577 || GET_CODE (SUBREG_REG (x)) != MEM)
8578 #endif
8579 {
8580 /* On many CISC machines, accessing an object in a wider mode
8581 causes the high-order bits to become undefined. So they are
8582 not known to be zero. */
8583 if (GET_MODE_SIZE (GET_MODE (x))
8584 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
8585 nonzero |= (GET_MODE_MASK (GET_MODE (x))
8586 & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x))));
8587 }
8588 }
8589 break;
8590
8591 case ASHIFTRT:
8592 case LSHIFTRT:
8593 case ASHIFT:
8594 case ROTATE:
8595 /* The nonzero bits are in two classes: any bits within MODE
8596 that aren't in GET_MODE (x) are always significant. The rest of the
8597 nonzero bits are those that are significant in the operand of
8598 the shift when shifted the appropriate number of bits. This
8599 shows that high-order bits are cleared by the right shift and
8600 low-order bits by left shifts. */
8601 if (GET_CODE (XEXP (x, 1)) == CONST_INT
8602 && INTVAL (XEXP (x, 1)) >= 0
8603 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
8604 {
8605 enum machine_mode inner_mode = GET_MODE (x);
8606 unsigned int width = GET_MODE_BITSIZE (inner_mode);
8607 int count = INTVAL (XEXP (x, 1));
8608 unsigned HOST_WIDE_INT mode_mask = GET_MODE_MASK (inner_mode);
8609 unsigned HOST_WIDE_INT op_nonzero =
8610 nonzero_bits_with_known (XEXP (x, 0), mode);
8611 unsigned HOST_WIDE_INT inner = op_nonzero & mode_mask;
8612 unsigned HOST_WIDE_INT outer = 0;
8613
8614 if (mode_width > width)
8615 outer = (op_nonzero & nonzero & ~mode_mask);
8616
8617 if (code == LSHIFTRT)
8618 inner >>= count;
8619 else if (code == ASHIFTRT)
8620 {
8621 inner >>= count;
8622
8623 /* If the sign bit may have been nonzero before the shift, we
8624 need to mark all the places it could have been copied to
8625 by the shift as possibly nonzero. */
8626 if (inner & ((HOST_WIDE_INT) 1 << (width - 1 - count)))
8627 inner |= (((HOST_WIDE_INT) 1 << count) - 1) << (width - count);
8628 }
8629 else if (code == ASHIFT)
8630 inner <<= count;
8631 else
8632 inner = ((inner << (count % width)
8633 | (inner >> (width - (count % width)))) & mode_mask);
8634
8635 nonzero &= (outer | inner);
8636 }
8637 break;
8638
8639 case FFS:
8640 case POPCOUNT:
8641 /* This is at most the number of bits in the mode. */
8642 nonzero = ((HOST_WIDE_INT) 2 << (floor_log2 (mode_width))) - 1;
8643 break;
8644
8645 case CLZ:
8646 /* If CLZ has a known value at zero, then the nonzero bits are
8647 that value, plus the number of bits in the mode minus one. */
8648 if (CLZ_DEFINED_VALUE_AT_ZERO (mode, nonzero))
8649 nonzero |= ((HOST_WIDE_INT) 1 << (floor_log2 (mode_width))) - 1;
8650 else
8651 nonzero = -1;
8652 break;
8653
8654 case CTZ:
8655 /* If CTZ has a known value at zero, then the nonzero bits are
8656 that value, plus the number of bits in the mode minus one. */
8657 if (CTZ_DEFINED_VALUE_AT_ZERO (mode, nonzero))
8658 nonzero |= ((HOST_WIDE_INT) 1 << (floor_log2 (mode_width))) - 1;
8659 else
8660 nonzero = -1;
8661 break;
8662
8663 case PARITY:
8664 nonzero = 1;
8665 break;
8666
8667 case IF_THEN_ELSE:
8668 nonzero &= (nonzero_bits_with_known (XEXP (x, 1), mode)
8669 | nonzero_bits_with_known (XEXP (x, 2), mode));
8670 break;
8671
8672 default:
8673 break;
8674 }
8675
8676 return nonzero;
8677 }
8678
8679 /* See the macro definition above. */
8680 #undef cached_num_sign_bit_copies
8681 \f
8682 #define num_sign_bit_copies_with_known(X, M) \
8683 cached_num_sign_bit_copies (X, M, known_x, known_mode, known_ret)
8684
8685 /* The function cached_num_sign_bit_copies is a wrapper around
8686 num_sign_bit_copies1. It avoids exponential behavior in
8687 num_sign_bit_copies1 when X has identical subexpressions on the
8688 first or the second level. */
8689
8690 static unsigned int
8691 cached_num_sign_bit_copies (x, mode, known_x, known_mode, known_ret)
8692 rtx x;
8693 enum machine_mode mode;
8694 rtx known_x;
8695 enum machine_mode known_mode;
8696 unsigned int known_ret;
8697 {
8698 if (x == known_x && mode == known_mode)
8699 return known_ret;
8700
8701 /* Try to find identical subexpressions. If found call
8702 num_sign_bit_copies1 on X with the subexpressions as KNOWN_X and
8703 the precomputed value for the subexpression as KNOWN_RET. */
8704
8705 if (GET_RTX_CLASS (GET_CODE (x)) == '2'
8706 || GET_RTX_CLASS (GET_CODE (x)) == 'c')
8707 {
8708 rtx x0 = XEXP (x, 0);
8709 rtx x1 = XEXP (x, 1);
8710
8711 /* Check the first level. */
8712 if (x0 == x1)
8713 return
8714 num_sign_bit_copies1 (x, mode, x0, mode,
8715 num_sign_bit_copies_with_known (x0, mode));
8716
8717 /* Check the second level. */
8718 if ((GET_RTX_CLASS (GET_CODE (x0)) == '2'
8719 || GET_RTX_CLASS (GET_CODE (x0)) == 'c')
8720 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
8721 return
8722 num_sign_bit_copies1 (x, mode, x1, mode,
8723 num_sign_bit_copies_with_known (x1, mode));
8724
8725 if ((GET_RTX_CLASS (GET_CODE (x1)) == '2'
8726 || GET_RTX_CLASS (GET_CODE (x1)) == 'c')
8727 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
8728 return
8729 num_sign_bit_copies1 (x, mode, x0, mode,
8730 num_sign_bit_copies_with_known (x0, mode));
8731 }
8732
8733 return num_sign_bit_copies1 (x, mode, known_x, known_mode, known_ret);
8734 }
8735
8736 /* Return the number of bits at the high-order end of X that are known to
8737 be equal to the sign bit. X will be used in mode MODE; if MODE is
8738 VOIDmode, X will be used in its own mode. The returned value will always
8739 be between 1 and the number of bits in MODE. */
8740
8741 static unsigned int
8742 num_sign_bit_copies1 (x, mode, known_x, known_mode, known_ret)
8743 rtx x;
8744 enum machine_mode mode;
8745 rtx known_x;
8746 enum machine_mode known_mode;
8747 unsigned int known_ret;
8748 {
8749 enum rtx_code code = GET_CODE (x);
8750 unsigned int bitwidth;
8751 int num0, num1, result;
8752 unsigned HOST_WIDE_INT nonzero;
8753 rtx tem;
8754
8755 /* If we weren't given a mode, use the mode of X. If the mode is still
8756 VOIDmode, we don't know anything. Likewise if one of the modes is
8757 floating-point. */
8758
8759 if (mode == VOIDmode)
8760 mode = GET_MODE (x);
8761
8762 if (mode == VOIDmode || FLOAT_MODE_P (mode) || FLOAT_MODE_P (GET_MODE (x)))
8763 return 1;
8764
8765 bitwidth = GET_MODE_BITSIZE (mode);
8766
8767 /* For a smaller object, just ignore the high bits. */
8768 if (bitwidth < GET_MODE_BITSIZE (GET_MODE (x)))
8769 {
8770 num0 = num_sign_bit_copies_with_known (x, GET_MODE (x));
8771 return MAX (1,
8772 num0 - (int) (GET_MODE_BITSIZE (GET_MODE (x)) - bitwidth));
8773 }
8774
8775 if (GET_MODE (x) != VOIDmode && bitwidth > GET_MODE_BITSIZE (GET_MODE (x)))
8776 {
8777 #ifndef WORD_REGISTER_OPERATIONS
8778 /* If this machine does not do all register operations on the entire
8779 register and MODE is wider than the mode of X, we can say nothing
8780 at all about the high-order bits. */
8781 return 1;
8782 #else
8783 /* Likewise on machines that do, if the mode of the object is smaller
8784 than a word and loads of that size don't sign extend, we can say
8785 nothing about the high order bits. */
8786 if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
8787 #ifdef LOAD_EXTEND_OP
8788 && LOAD_EXTEND_OP (GET_MODE (x)) != SIGN_EXTEND
8789 #endif
8790 )
8791 return 1;
8792 #endif
8793 }
8794
8795 switch (code)
8796 {
8797 case REG:
8798
8799 #if defined(POINTERS_EXTEND_UNSIGNED) && !defined(HAVE_ptr_extend)
8800 /* If pointers extend signed and this is a pointer in Pmode, say that
8801 all the bits above ptr_mode are known to be sign bit copies. */
8802 if (! POINTERS_EXTEND_UNSIGNED && GET_MODE (x) == Pmode && mode == Pmode
8803 && REG_POINTER (x))
8804 return GET_MODE_BITSIZE (Pmode) - GET_MODE_BITSIZE (ptr_mode) + 1;
8805 #endif
8806
8807 if (reg_last_set_value[REGNO (x)] != 0
8808 && reg_last_set_mode[REGNO (x)] == mode
8809 && (reg_last_set_label[REGNO (x)] == label_tick
8810 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
8811 && REG_N_SETS (REGNO (x)) == 1
8812 && ! REGNO_REG_SET_P (ENTRY_BLOCK_PTR->next_bb->global_live_at_start,
8813 REGNO (x))))
8814 && INSN_CUID (reg_last_set[REGNO (x)]) < subst_low_cuid)
8815 return reg_last_set_sign_bit_copies[REGNO (x)];
8816
8817 tem = get_last_value (x);
8818 if (tem != 0)
8819 return num_sign_bit_copies_with_known (tem, mode);
8820
8821 if (nonzero_sign_valid && reg_sign_bit_copies[REGNO (x)] != 0
8822 && GET_MODE_BITSIZE (GET_MODE (x)) == bitwidth)
8823 return reg_sign_bit_copies[REGNO (x)];
8824 break;
8825
8826 case MEM:
8827 #ifdef LOAD_EXTEND_OP
8828 /* Some RISC machines sign-extend all loads of smaller than a word. */
8829 if (LOAD_EXTEND_OP (GET_MODE (x)) == SIGN_EXTEND)
8830 return MAX (1, ((int) bitwidth
8831 - (int) GET_MODE_BITSIZE (GET_MODE (x)) + 1));
8832 #endif
8833 break;
8834
8835 case CONST_INT:
8836 /* If the constant is negative, take its 1's complement and remask.
8837 Then see how many zero bits we have. */
8838 nonzero = INTVAL (x) & GET_MODE_MASK (mode);
8839 if (bitwidth <= HOST_BITS_PER_WIDE_INT
8840 && (nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
8841 nonzero = (~nonzero) & GET_MODE_MASK (mode);
8842
8843 return (nonzero == 0 ? bitwidth : bitwidth - floor_log2 (nonzero) - 1);
8844
8845 case SUBREG:
8846 /* If this is a SUBREG for a promoted object that is sign-extended
8847 and we are looking at it in a wider mode, we know that at least the
8848 high-order bits are known to be sign bit copies. */
8849
8850 if (SUBREG_PROMOTED_VAR_P (x) && ! SUBREG_PROMOTED_UNSIGNED_P (x))
8851 {
8852 num0 = num_sign_bit_copies_with_known (SUBREG_REG (x), mode);
8853 return MAX ((int) bitwidth
8854 - (int) GET_MODE_BITSIZE (GET_MODE (x)) + 1,
8855 num0);
8856 }
8857
8858 /* For a smaller object, just ignore the high bits. */
8859 if (bitwidth <= GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))))
8860 {
8861 num0 = num_sign_bit_copies_with_known (SUBREG_REG (x), VOIDmode);
8862 return MAX (1, (num0
8863 - (int) (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x)))
8864 - bitwidth)));
8865 }
8866
8867 #ifdef WORD_REGISTER_OPERATIONS
8868 #ifdef LOAD_EXTEND_OP
8869 /* For paradoxical SUBREGs on machines where all register operations
8870 affect the entire register, just look inside. Note that we are
8871 passing MODE to the recursive call, so the number of sign bit copies
8872 will remain relative to that mode, not the inner mode. */
8873
8874 /* This works only if loads sign extend. Otherwise, if we get a
8875 reload for the inner part, it may be loaded from the stack, and
8876 then we lose all sign bit copies that existed before the store
8877 to the stack. */
8878
8879 if ((GET_MODE_SIZE (GET_MODE (x))
8880 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
8881 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (x))) == SIGN_EXTEND
8882 && GET_CODE (SUBREG_REG (x)) == MEM)
8883 return num_sign_bit_copies_with_known (SUBREG_REG (x), mode);
8884 #endif
8885 #endif
8886 break;
8887
8888 case SIGN_EXTRACT:
8889 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
8890 return MAX (1, (int) bitwidth - INTVAL (XEXP (x, 1)));
8891 break;
8892
8893 case SIGN_EXTEND:
8894 return (bitwidth - GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
8895 + num_sign_bit_copies_with_known (XEXP (x, 0), VOIDmode));
8896
8897 case TRUNCATE:
8898 /* For a smaller object, just ignore the high bits. */
8899 num0 = num_sign_bit_copies_with_known (XEXP (x, 0), VOIDmode);
8900 return MAX (1, (num0 - (int) (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
8901 - bitwidth)));
8902
8903 case NOT:
8904 return num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8905
8906 case ROTATE: case ROTATERT:
8907 /* If we are rotating left by a number of bits less than the number
8908 of sign bit copies, we can just subtract that amount from the
8909 number. */
8910 if (GET_CODE (XEXP (x, 1)) == CONST_INT
8911 && INTVAL (XEXP (x, 1)) >= 0
8912 && INTVAL (XEXP (x, 1)) < (int) bitwidth)
8913 {
8914 num0 = num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8915 return MAX (1, num0 - (code == ROTATE ? INTVAL (XEXP (x, 1))
8916 : (int) bitwidth - INTVAL (XEXP (x, 1))));
8917 }
8918 break;
8919
8920 case NEG:
8921 /* In general, this subtracts one sign bit copy. But if the value
8922 is known to be positive, the number of sign bit copies is the
8923 same as that of the input. Finally, if the input has just one bit
8924 that might be nonzero, all the bits are copies of the sign bit. */
8925 num0 = num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8926 if (bitwidth > HOST_BITS_PER_WIDE_INT)
8927 return num0 > 1 ? num0 - 1 : 1;
8928
8929 nonzero = nonzero_bits (XEXP (x, 0), mode);
8930 if (nonzero == 1)
8931 return bitwidth;
8932
8933 if (num0 > 1
8934 && (((HOST_WIDE_INT) 1 << (bitwidth - 1)) & nonzero))
8935 num0--;
8936
8937 return num0;
8938
8939 case IOR: case AND: case XOR:
8940 case SMIN: case SMAX: case UMIN: case UMAX:
8941 /* Logical operations will preserve the number of sign-bit copies.
8942 MIN and MAX operations always return one of the operands. */
8943 num0 = num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8944 num1 = num_sign_bit_copies_with_known (XEXP (x, 1), mode);
8945 return MIN (num0, num1);
8946
8947 case PLUS: case MINUS:
8948 /* For addition and subtraction, we can have a 1-bit carry. However,
8949 if we are subtracting 1 from a positive number, there will not
8950 be such a carry. Furthermore, if the positive number is known to
8951 be 0 or 1, we know the result is either -1 or 0. */
8952
8953 if (code == PLUS && XEXP (x, 1) == constm1_rtx
8954 && bitwidth <= HOST_BITS_PER_WIDE_INT)
8955 {
8956 nonzero = nonzero_bits (XEXP (x, 0), mode);
8957 if ((((HOST_WIDE_INT) 1 << (bitwidth - 1)) & nonzero) == 0)
8958 return (nonzero == 1 || nonzero == 0 ? bitwidth
8959 : bitwidth - floor_log2 (nonzero) - 1);
8960 }
8961
8962 num0 = num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8963 num1 = num_sign_bit_copies_with_known (XEXP (x, 1), mode);
8964 result = MAX (1, MIN (num0, num1) - 1);
8965
8966 #ifdef POINTERS_EXTEND_UNSIGNED
8967 /* If pointers extend signed and this is an addition or subtraction
8968 to a pointer in Pmode, all the bits above ptr_mode are known to be
8969 sign bit copies. */
8970 if (! POINTERS_EXTEND_UNSIGNED && GET_MODE (x) == Pmode
8971 && (code == PLUS || code == MINUS)
8972 && GET_CODE (XEXP (x, 0)) == REG && REG_POINTER (XEXP (x, 0)))
8973 result = MAX ((int) (GET_MODE_BITSIZE (Pmode)
8974 - GET_MODE_BITSIZE (ptr_mode) + 1),
8975 result);
8976 #endif
8977 return result;
8978
8979 case MULT:
8980 /* The number of bits of the product is the sum of the number of
8981 bits of both terms. However, unless one of the terms if known
8982 to be positive, we must allow for an additional bit since negating
8983 a negative number can remove one sign bit copy. */
8984
8985 num0 = num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8986 num1 = num_sign_bit_copies_with_known (XEXP (x, 1), mode);
8987
8988 result = bitwidth - (bitwidth - num0) - (bitwidth - num1);
8989 if (result > 0
8990 && (bitwidth > HOST_BITS_PER_WIDE_INT
8991 || (((nonzero_bits (XEXP (x, 0), mode)
8992 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
8993 && ((nonzero_bits (XEXP (x, 1), mode)
8994 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0))))
8995 result--;
8996
8997 return MAX (1, result);
8998
8999 case UDIV:
9000 /* The result must be <= the first operand. If the first operand
9001 has the high bit set, we know nothing about the number of sign
9002 bit copies. */
9003 if (bitwidth > HOST_BITS_PER_WIDE_INT)
9004 return 1;
9005 else if ((nonzero_bits (XEXP (x, 0), mode)
9006 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
9007 return 1;
9008 else
9009 return num_sign_bit_copies_with_known (XEXP (x, 0), mode);
9010
9011 case UMOD:
9012 /* The result must be <= the second operand. */
9013 return num_sign_bit_copies_with_known (XEXP (x, 1), mode);
9014
9015 case DIV:
9016 /* Similar to unsigned division, except that we have to worry about
9017 the case where the divisor is negative, in which case we have
9018 to add 1. */
9019 result = num_sign_bit_copies_with_known (XEXP (x, 0), mode);
9020 if (result > 1
9021 && (bitwidth > HOST_BITS_PER_WIDE_INT
9022 || (nonzero_bits (XEXP (x, 1), mode)
9023 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0))
9024 result--;
9025
9026 return result;
9027
9028 case MOD:
9029 result = num_sign_bit_copies_with_known (XEXP (x, 1), mode);
9030 if (result > 1
9031 && (bitwidth > HOST_BITS_PER_WIDE_INT
9032 || (nonzero_bits (XEXP (x, 1), mode)
9033 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0))
9034 result--;
9035
9036 return result;
9037
9038 case ASHIFTRT:
9039 /* Shifts by a constant add to the number of bits equal to the
9040 sign bit. */
9041 num0 = num_sign_bit_copies_with_known (XEXP (x, 0), mode);
9042 if (GET_CODE (XEXP (x, 1)) == CONST_INT
9043 && INTVAL (XEXP (x, 1)) > 0)
9044 num0 = MIN ((int) bitwidth, num0 + INTVAL (XEXP (x, 1)));
9045
9046 return num0;
9047
9048 case ASHIFT:
9049 /* Left shifts destroy copies. */
9050 if (GET_CODE (XEXP (x, 1)) != CONST_INT
9051 || INTVAL (XEXP (x, 1)) < 0
9052 || INTVAL (XEXP (x, 1)) >= (int) bitwidth)
9053 return 1;
9054
9055 num0 = num_sign_bit_copies_with_known (XEXP (x, 0), mode);
9056 return MAX (1, num0 - INTVAL (XEXP (x, 1)));
9057
9058 case IF_THEN_ELSE:
9059 num0 = num_sign_bit_copies_with_known (XEXP (x, 1), mode);
9060 num1 = num_sign_bit_copies_with_known (XEXP (x, 2), mode);
9061 return MIN (num0, num1);
9062
9063 case EQ: case NE: case GE: case GT: case LE: case LT:
9064 case UNEQ: case LTGT: case UNGE: case UNGT: case UNLE: case UNLT:
9065 case GEU: case GTU: case LEU: case LTU:
9066 case UNORDERED: case ORDERED:
9067 /* If the constant is negative, take its 1's complement and remask.
9068 Then see how many zero bits we have. */
9069 nonzero = STORE_FLAG_VALUE;
9070 if (bitwidth <= HOST_BITS_PER_WIDE_INT
9071 && (nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
9072 nonzero = (~nonzero) & GET_MODE_MASK (mode);
9073
9074 return (nonzero == 0 ? bitwidth : bitwidth - floor_log2 (nonzero) - 1);
9075 break;
9076
9077 default:
9078 break;
9079 }
9080
9081 /* If we haven't been able to figure it out by one of the above rules,
9082 see if some of the high-order bits are known to be zero. If so,
9083 count those bits and return one less than that amount. If we can't
9084 safely compute the mask for this mode, always return BITWIDTH. */
9085
9086 if (bitwidth > HOST_BITS_PER_WIDE_INT)
9087 return 1;
9088
9089 nonzero = nonzero_bits (x, mode);
9090 return (nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))
9091 ? 1 : bitwidth - floor_log2 (nonzero) - 1);
9092 }
9093 \f
9094 /* Return the number of "extended" bits there are in X, when interpreted
9095 as a quantity in MODE whose signedness is indicated by UNSIGNEDP. For
9096 unsigned quantities, this is the number of high-order zero bits.
9097 For signed quantities, this is the number of copies of the sign bit
9098 minus 1. In both case, this function returns the number of "spare"
9099 bits. For example, if two quantities for which this function returns
9100 at least 1 are added, the addition is known not to overflow.
9101
9102 This function will always return 0 unless called during combine, which
9103 implies that it must be called from a define_split. */
9104
9105 unsigned int
9106 extended_count (x, mode, unsignedp)
9107 rtx x;
9108 enum machine_mode mode;
9109 int unsignedp;
9110 {
9111 if (nonzero_sign_valid == 0)
9112 return 0;
9113
9114 return (unsignedp
9115 ? (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
9116 ? (unsigned int) (GET_MODE_BITSIZE (mode) - 1
9117 - floor_log2 (nonzero_bits (x, mode)))
9118 : 0)
9119 : num_sign_bit_copies (x, mode) - 1);
9120 }
9121 \f
9122 /* This function is called from `simplify_shift_const' to merge two
9123 outer operations. Specifically, we have already found that we need
9124 to perform operation *POP0 with constant *PCONST0 at the outermost
9125 position. We would now like to also perform OP1 with constant CONST1
9126 (with *POP0 being done last).
9127
9128 Return 1 if we can do the operation and update *POP0 and *PCONST0 with
9129 the resulting operation. *PCOMP_P is set to 1 if we would need to
9130 complement the innermost operand, otherwise it is unchanged.
9131
9132 MODE is the mode in which the operation will be done. No bits outside
9133 the width of this mode matter. It is assumed that the width of this mode
9134 is smaller than or equal to HOST_BITS_PER_WIDE_INT.
9135
9136 If *POP0 or OP1 are NIL, it means no operation is required. Only NEG, PLUS,
9137 IOR, XOR, and AND are supported. We may set *POP0 to SET if the proper
9138 result is simply *PCONST0.
9139
9140 If the resulting operation cannot be expressed as one operation, we
9141 return 0 and do not change *POP0, *PCONST0, and *PCOMP_P. */
9142
9143 static int
9144 merge_outer_ops (pop0, pconst0, op1, const1, mode, pcomp_p)
9145 enum rtx_code *pop0;
9146 HOST_WIDE_INT *pconst0;
9147 enum rtx_code op1;
9148 HOST_WIDE_INT const1;
9149 enum machine_mode mode;
9150 int *pcomp_p;
9151 {
9152 enum rtx_code op0 = *pop0;
9153 HOST_WIDE_INT const0 = *pconst0;
9154
9155 const0 &= GET_MODE_MASK (mode);
9156 const1 &= GET_MODE_MASK (mode);
9157
9158 /* If OP0 is an AND, clear unimportant bits in CONST1. */
9159 if (op0 == AND)
9160 const1 &= const0;
9161
9162 /* If OP0 or OP1 is NIL, this is easy. Similarly if they are the same or
9163 if OP0 is SET. */
9164
9165 if (op1 == NIL || op0 == SET)
9166 return 1;
9167
9168 else if (op0 == NIL)
9169 op0 = op1, const0 = const1;
9170
9171 else if (op0 == op1)
9172 {
9173 switch (op0)
9174 {
9175 case AND:
9176 const0 &= const1;
9177 break;
9178 case IOR:
9179 const0 |= const1;
9180 break;
9181 case XOR:
9182 const0 ^= const1;
9183 break;
9184 case PLUS:
9185 const0 += const1;
9186 break;
9187 case NEG:
9188 op0 = NIL;
9189 break;
9190 default:
9191 break;
9192 }
9193 }
9194
9195 /* Otherwise, if either is a PLUS or NEG, we can't do anything. */
9196 else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
9197 return 0;
9198
9199 /* If the two constants aren't the same, we can't do anything. The
9200 remaining six cases can all be done. */
9201 else if (const0 != const1)
9202 return 0;
9203
9204 else
9205 switch (op0)
9206 {
9207 case IOR:
9208 if (op1 == AND)
9209 /* (a & b) | b == b */
9210 op0 = SET;
9211 else /* op1 == XOR */
9212 /* (a ^ b) | b == a | b */
9213 {;}
9214 break;
9215
9216 case XOR:
9217 if (op1 == AND)
9218 /* (a & b) ^ b == (~a) & b */
9219 op0 = AND, *pcomp_p = 1;
9220 else /* op1 == IOR */
9221 /* (a | b) ^ b == a & ~b */
9222 op0 = AND, const0 = ~const0;
9223 break;
9224
9225 case AND:
9226 if (op1 == IOR)
9227 /* (a | b) & b == b */
9228 op0 = SET;
9229 else /* op1 == XOR */
9230 /* (a ^ b) & b) == (~a) & b */
9231 *pcomp_p = 1;
9232 break;
9233 default:
9234 break;
9235 }
9236
9237 /* Check for NO-OP cases. */
9238 const0 &= GET_MODE_MASK (mode);
9239 if (const0 == 0
9240 && (op0 == IOR || op0 == XOR || op0 == PLUS))
9241 op0 = NIL;
9242 else if (const0 == 0 && op0 == AND)
9243 op0 = SET;
9244 else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
9245 && op0 == AND)
9246 op0 = NIL;
9247
9248 /* ??? Slightly redundant with the above mask, but not entirely.
9249 Moving this above means we'd have to sign-extend the mode mask
9250 for the final test. */
9251 const0 = trunc_int_for_mode (const0, mode);
9252
9253 *pop0 = op0;
9254 *pconst0 = const0;
9255
9256 return 1;
9257 }
9258 \f
9259 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
9260 The result of the shift is RESULT_MODE. X, if nonzero, is an expression
9261 that we started with.
9262
9263 The shift is normally computed in the widest mode we find in VAROP, as
9264 long as it isn't a different number of words than RESULT_MODE. Exceptions
9265 are ASHIFTRT and ROTATE, which are always done in their original mode, */
9266
9267 static rtx
9268 simplify_shift_const (x, code, result_mode, varop, orig_count)
9269 rtx x;
9270 enum rtx_code code;
9271 enum machine_mode result_mode;
9272 rtx varop;
9273 int orig_count;
9274 {
9275 enum rtx_code orig_code = code;
9276 unsigned int count;
9277 int signed_count;
9278 enum machine_mode mode = result_mode;
9279 enum machine_mode shift_mode, tmode;
9280 unsigned int mode_words
9281 = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
9282 /* We form (outer_op (code varop count) (outer_const)). */
9283 enum rtx_code outer_op = NIL;
9284 HOST_WIDE_INT outer_const = 0;
9285 rtx const_rtx;
9286 int complement_p = 0;
9287 rtx new;
9288
9289 /* Make sure and truncate the "natural" shift on the way in. We don't
9290 want to do this inside the loop as it makes it more difficult to
9291 combine shifts. */
9292 #ifdef SHIFT_COUNT_TRUNCATED
9293 if (SHIFT_COUNT_TRUNCATED)
9294 orig_count &= GET_MODE_BITSIZE (mode) - 1;
9295 #endif
9296
9297 /* If we were given an invalid count, don't do anything except exactly
9298 what was requested. */
9299
9300 if (orig_count < 0 || orig_count >= (int) GET_MODE_BITSIZE (mode))
9301 {
9302 if (x)
9303 return x;
9304
9305 return gen_rtx_fmt_ee (code, mode, varop, GEN_INT (orig_count));
9306 }
9307
9308 count = orig_count;
9309
9310 /* Unless one of the branches of the `if' in this loop does a `continue',
9311 we will `break' the loop after the `if'. */
9312
9313 while (count != 0)
9314 {
9315 /* If we have an operand of (clobber (const_int 0)), just return that
9316 value. */
9317 if (GET_CODE (varop) == CLOBBER)
9318 return varop;
9319
9320 /* If we discovered we had to complement VAROP, leave. Making a NOT
9321 here would cause an infinite loop. */
9322 if (complement_p)
9323 break;
9324
9325 /* Convert ROTATERT to ROTATE. */
9326 if (code == ROTATERT)
9327 {
9328 unsigned int bitsize = GET_MODE_BITSIZE (result_mode);;
9329 code = ROTATE;
9330 if (VECTOR_MODE_P (result_mode))
9331 count = bitsize / GET_MODE_NUNITS (result_mode) - count;
9332 else
9333 count = bitsize - count;
9334 }
9335
9336 /* We need to determine what mode we will do the shift in. If the
9337 shift is a right shift or a ROTATE, we must always do it in the mode
9338 it was originally done in. Otherwise, we can do it in MODE, the
9339 widest mode encountered. */
9340 shift_mode
9341 = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
9342 ? result_mode : mode);
9343
9344 /* Handle cases where the count is greater than the size of the mode
9345 minus 1. For ASHIFT, use the size minus one as the count (this can
9346 occur when simplifying (lshiftrt (ashiftrt ..))). For rotates,
9347 take the count modulo the size. For other shifts, the result is
9348 zero.
9349
9350 Since these shifts are being produced by the compiler by combining
9351 multiple operations, each of which are defined, we know what the
9352 result is supposed to be. */
9353
9354 if (count > (unsigned int) (GET_MODE_BITSIZE (shift_mode) - 1))
9355 {
9356 if (code == ASHIFTRT)
9357 count = GET_MODE_BITSIZE (shift_mode) - 1;
9358 else if (code == ROTATE || code == ROTATERT)
9359 count %= GET_MODE_BITSIZE (shift_mode);
9360 else
9361 {
9362 /* We can't simply return zero because there may be an
9363 outer op. */
9364 varop = const0_rtx;
9365 count = 0;
9366 break;
9367 }
9368 }
9369
9370 /* An arithmetic right shift of a quantity known to be -1 or 0
9371 is a no-op. */
9372 if (code == ASHIFTRT
9373 && (num_sign_bit_copies (varop, shift_mode)
9374 == GET_MODE_BITSIZE (shift_mode)))
9375 {
9376 count = 0;
9377 break;
9378 }
9379
9380 /* If we are doing an arithmetic right shift and discarding all but
9381 the sign bit copies, this is equivalent to doing a shift by the
9382 bitsize minus one. Convert it into that shift because it will often
9383 allow other simplifications. */
9384
9385 if (code == ASHIFTRT
9386 && (count + num_sign_bit_copies (varop, shift_mode)
9387 >= GET_MODE_BITSIZE (shift_mode)))
9388 count = GET_MODE_BITSIZE (shift_mode) - 1;
9389
9390 /* We simplify the tests below and elsewhere by converting
9391 ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
9392 `make_compound_operation' will convert it to an ASHIFTRT for
9393 those machines (such as VAX) that don't have an LSHIFTRT. */
9394 if (GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9395 && code == ASHIFTRT
9396 && ((nonzero_bits (varop, shift_mode)
9397 & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (shift_mode) - 1)))
9398 == 0))
9399 code = LSHIFTRT;
9400
9401 if (code == LSHIFTRT
9402 && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9403 && !(nonzero_bits (varop, shift_mode) >> count))
9404 varop = const0_rtx;
9405 if (code == ASHIFT
9406 && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9407 && !((nonzero_bits (varop, shift_mode) << count)
9408 & GET_MODE_MASK (shift_mode)))
9409 varop = const0_rtx;
9410
9411 switch (GET_CODE (varop))
9412 {
9413 case SIGN_EXTEND:
9414 case ZERO_EXTEND:
9415 case SIGN_EXTRACT:
9416 case ZERO_EXTRACT:
9417 new = expand_compound_operation (varop);
9418 if (new != varop)
9419 {
9420 varop = new;
9421 continue;
9422 }
9423 break;
9424
9425 case MEM:
9426 /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
9427 minus the width of a smaller mode, we can do this with a
9428 SIGN_EXTEND or ZERO_EXTEND from the narrower memory location. */
9429 if ((code == ASHIFTRT || code == LSHIFTRT)
9430 && ! mode_dependent_address_p (XEXP (varop, 0))
9431 && ! MEM_VOLATILE_P (varop)
9432 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
9433 MODE_INT, 1)) != BLKmode)
9434 {
9435 new = adjust_address_nv (varop, tmode,
9436 BYTES_BIG_ENDIAN ? 0
9437 : count / BITS_PER_UNIT);
9438
9439 varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
9440 : ZERO_EXTEND, mode, new);
9441 count = 0;
9442 continue;
9443 }
9444 break;
9445
9446 case USE:
9447 /* Similar to the case above, except that we can only do this if
9448 the resulting mode is the same as that of the underlying
9449 MEM and adjust the address depending on the *bits* endianness
9450 because of the way that bit-field extract insns are defined. */
9451 if ((code == ASHIFTRT || code == LSHIFTRT)
9452 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
9453 MODE_INT, 1)) != BLKmode
9454 && tmode == GET_MODE (XEXP (varop, 0)))
9455 {
9456 if (BITS_BIG_ENDIAN)
9457 new = XEXP (varop, 0);
9458 else
9459 {
9460 new = copy_rtx (XEXP (varop, 0));
9461 SUBST (XEXP (new, 0),
9462 plus_constant (XEXP (new, 0),
9463 count / BITS_PER_UNIT));
9464 }
9465
9466 varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
9467 : ZERO_EXTEND, mode, new);
9468 count = 0;
9469 continue;
9470 }
9471 break;
9472
9473 case SUBREG:
9474 /* If VAROP is a SUBREG, strip it as long as the inner operand has
9475 the same number of words as what we've seen so far. Then store
9476 the widest mode in MODE. */
9477 if (subreg_lowpart_p (varop)
9478 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
9479 > GET_MODE_SIZE (GET_MODE (varop)))
9480 && (unsigned int) ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
9481 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
9482 == mode_words)
9483 {
9484 varop = SUBREG_REG (varop);
9485 if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
9486 mode = GET_MODE (varop);
9487 continue;
9488 }
9489 break;
9490
9491 case MULT:
9492 /* Some machines use MULT instead of ASHIFT because MULT
9493 is cheaper. But it is still better on those machines to
9494 merge two shifts into one. */
9495 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9496 && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
9497 {
9498 varop
9499 = gen_binary (ASHIFT, GET_MODE (varop), XEXP (varop, 0),
9500 GEN_INT (exact_log2 (INTVAL (XEXP (varop, 1)))));
9501 continue;
9502 }
9503 break;
9504
9505 case UDIV:
9506 /* Similar, for when divides are cheaper. */
9507 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9508 && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
9509 {
9510 varop
9511 = gen_binary (LSHIFTRT, GET_MODE (varop), XEXP (varop, 0),
9512 GEN_INT (exact_log2 (INTVAL (XEXP (varop, 1)))));
9513 continue;
9514 }
9515 break;
9516
9517 case ASHIFTRT:
9518 /* If we are extracting just the sign bit of an arithmetic
9519 right shift, that shift is not needed. However, the sign
9520 bit of a wider mode may be different from what would be
9521 interpreted as the sign bit in a narrower mode, so, if
9522 the result is narrower, don't discard the shift. */
9523 if (code == LSHIFTRT
9524 && count == (unsigned int) (GET_MODE_BITSIZE (result_mode) - 1)
9525 && (GET_MODE_BITSIZE (result_mode)
9526 >= GET_MODE_BITSIZE (GET_MODE (varop))))
9527 {
9528 varop = XEXP (varop, 0);
9529 continue;
9530 }
9531
9532 /* ... fall through ... */
9533
9534 case LSHIFTRT:
9535 case ASHIFT:
9536 case ROTATE:
9537 /* Here we have two nested shifts. The result is usually the
9538 AND of a new shift with a mask. We compute the result below. */
9539 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9540 && INTVAL (XEXP (varop, 1)) >= 0
9541 && INTVAL (XEXP (varop, 1)) < GET_MODE_BITSIZE (GET_MODE (varop))
9542 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9543 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
9544 {
9545 enum rtx_code first_code = GET_CODE (varop);
9546 unsigned int first_count = INTVAL (XEXP (varop, 1));
9547 unsigned HOST_WIDE_INT mask;
9548 rtx mask_rtx;
9549
9550 /* We have one common special case. We can't do any merging if
9551 the inner code is an ASHIFTRT of a smaller mode. However, if
9552 we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
9553 with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
9554 we can convert it to
9555 (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0 C2) C3) C1).
9556 This simplifies certain SIGN_EXTEND operations. */
9557 if (code == ASHIFT && first_code == ASHIFTRT
9558 && count == (unsigned int)
9559 (GET_MODE_BITSIZE (result_mode)
9560 - GET_MODE_BITSIZE (GET_MODE (varop))))
9561 {
9562 /* C3 has the low-order C1 bits zero. */
9563
9564 mask = (GET_MODE_MASK (mode)
9565 & ~(((HOST_WIDE_INT) 1 << first_count) - 1));
9566
9567 varop = simplify_and_const_int (NULL_RTX, result_mode,
9568 XEXP (varop, 0), mask);
9569 varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
9570 varop, count);
9571 count = first_count;
9572 code = ASHIFTRT;
9573 continue;
9574 }
9575
9576 /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
9577 than C1 high-order bits equal to the sign bit, we can convert
9578 this to either an ASHIFT or an ASHIFTRT depending on the
9579 two counts.
9580
9581 We cannot do this if VAROP's mode is not SHIFT_MODE. */
9582
9583 if (code == ASHIFTRT && first_code == ASHIFT
9584 && GET_MODE (varop) == shift_mode
9585 && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
9586 > first_count))
9587 {
9588 varop = XEXP (varop, 0);
9589
9590 signed_count = count - first_count;
9591 if (signed_count < 0)
9592 count = -signed_count, code = ASHIFT;
9593 else
9594 count = signed_count;
9595
9596 continue;
9597 }
9598
9599 /* There are some cases we can't do. If CODE is ASHIFTRT,
9600 we can only do this if FIRST_CODE is also ASHIFTRT.
9601
9602 We can't do the case when CODE is ROTATE and FIRST_CODE is
9603 ASHIFTRT.
9604
9605 If the mode of this shift is not the mode of the outer shift,
9606 we can't do this if either shift is a right shift or ROTATE.
9607
9608 Finally, we can't do any of these if the mode is too wide
9609 unless the codes are the same.
9610
9611 Handle the case where the shift codes are the same
9612 first. */
9613
9614 if (code == first_code)
9615 {
9616 if (GET_MODE (varop) != result_mode
9617 && (code == ASHIFTRT || code == LSHIFTRT
9618 || code == ROTATE))
9619 break;
9620
9621 count += first_count;
9622 varop = XEXP (varop, 0);
9623 continue;
9624 }
9625
9626 if (code == ASHIFTRT
9627 || (code == ROTATE && first_code == ASHIFTRT)
9628 || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT
9629 || (GET_MODE (varop) != result_mode
9630 && (first_code == ASHIFTRT || first_code == LSHIFTRT
9631 || first_code == ROTATE
9632 || code == ROTATE)))
9633 break;
9634
9635 /* To compute the mask to apply after the shift, shift the
9636 nonzero bits of the inner shift the same way the
9637 outer shift will. */
9638
9639 mask_rtx = GEN_INT (nonzero_bits (varop, GET_MODE (varop)));
9640
9641 mask_rtx
9642 = simplify_binary_operation (code, result_mode, mask_rtx,
9643 GEN_INT (count));
9644
9645 /* Give up if we can't compute an outer operation to use. */
9646 if (mask_rtx == 0
9647 || GET_CODE (mask_rtx) != CONST_INT
9648 || ! merge_outer_ops (&outer_op, &outer_const, AND,
9649 INTVAL (mask_rtx),
9650 result_mode, &complement_p))
9651 break;
9652
9653 /* If the shifts are in the same direction, we add the
9654 counts. Otherwise, we subtract them. */
9655 signed_count = count;
9656 if ((code == ASHIFTRT || code == LSHIFTRT)
9657 == (first_code == ASHIFTRT || first_code == LSHIFTRT))
9658 signed_count += first_count;
9659 else
9660 signed_count -= first_count;
9661
9662 /* If COUNT is positive, the new shift is usually CODE,
9663 except for the two exceptions below, in which case it is
9664 FIRST_CODE. If the count is negative, FIRST_CODE should
9665 always be used */
9666 if (signed_count > 0
9667 && ((first_code == ROTATE && code == ASHIFT)
9668 || (first_code == ASHIFTRT && code == LSHIFTRT)))
9669 code = first_code, count = signed_count;
9670 else if (signed_count < 0)
9671 code = first_code, count = -signed_count;
9672 else
9673 count = signed_count;
9674
9675 varop = XEXP (varop, 0);
9676 continue;
9677 }
9678
9679 /* If we have (A << B << C) for any shift, we can convert this to
9680 (A << C << B). This wins if A is a constant. Only try this if
9681 B is not a constant. */
9682
9683 else if (GET_CODE (varop) == code
9684 && GET_CODE (XEXP (varop, 1)) != CONST_INT
9685 && 0 != (new
9686 = simplify_binary_operation (code, mode,
9687 XEXP (varop, 0),
9688 GEN_INT (count))))
9689 {
9690 varop = gen_rtx_fmt_ee (code, mode, new, XEXP (varop, 1));
9691 count = 0;
9692 continue;
9693 }
9694 break;
9695
9696 case NOT:
9697 /* Make this fit the case below. */
9698 varop = gen_rtx_XOR (mode, XEXP (varop, 0),
9699 GEN_INT (GET_MODE_MASK (mode)));
9700 continue;
9701
9702 case IOR:
9703 case AND:
9704 case XOR:
9705 /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
9706 with C the size of VAROP - 1 and the shift is logical if
9707 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
9708 we have an (le X 0) operation. If we have an arithmetic shift
9709 and STORE_FLAG_VALUE is 1 or we have a logical shift with
9710 STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation. */
9711
9712 if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
9713 && XEXP (XEXP (varop, 0), 1) == constm1_rtx
9714 && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9715 && (code == LSHIFTRT || code == ASHIFTRT)
9716 && count == (unsigned int)
9717 (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
9718 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
9719 {
9720 count = 0;
9721 varop = gen_rtx_LE (GET_MODE (varop), XEXP (varop, 1),
9722 const0_rtx);
9723
9724 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
9725 varop = gen_rtx_NEG (GET_MODE (varop), varop);
9726
9727 continue;
9728 }
9729
9730 /* If we have (shift (logical)), move the logical to the outside
9731 to allow it to possibly combine with another logical and the
9732 shift to combine with another shift. This also canonicalizes to
9733 what a ZERO_EXTRACT looks like. Also, some machines have
9734 (and (shift)) insns. */
9735
9736 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9737 && (new = simplify_binary_operation (code, result_mode,
9738 XEXP (varop, 1),
9739 GEN_INT (count))) != 0
9740 && GET_CODE (new) == CONST_INT
9741 && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
9742 INTVAL (new), result_mode, &complement_p))
9743 {
9744 varop = XEXP (varop, 0);
9745 continue;
9746 }
9747
9748 /* If we can't do that, try to simplify the shift in each arm of the
9749 logical expression, make a new logical expression, and apply
9750 the inverse distributive law. */
9751 {
9752 rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
9753 XEXP (varop, 0), count);
9754 rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
9755 XEXP (varop, 1), count);
9756
9757 varop = gen_binary (GET_CODE (varop), shift_mode, lhs, rhs);
9758 varop = apply_distributive_law (varop);
9759
9760 count = 0;
9761 }
9762 break;
9763
9764 case EQ:
9765 /* convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
9766 says that the sign bit can be tested, FOO has mode MODE, C is
9767 GET_MODE_BITSIZE (MODE) - 1, and FOO has only its low-order bit
9768 that may be nonzero. */
9769 if (code == LSHIFTRT
9770 && XEXP (varop, 1) == const0_rtx
9771 && GET_MODE (XEXP (varop, 0)) == result_mode
9772 && count == (unsigned int) (GET_MODE_BITSIZE (result_mode) - 1)
9773 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9774 && ((STORE_FLAG_VALUE
9775 & ((HOST_WIDE_INT) 1
9776 < (GET_MODE_BITSIZE (result_mode) - 1))))
9777 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
9778 && merge_outer_ops (&outer_op, &outer_const, XOR,
9779 (HOST_WIDE_INT) 1, result_mode,
9780 &complement_p))
9781 {
9782 varop = XEXP (varop, 0);
9783 count = 0;
9784 continue;
9785 }
9786 break;
9787
9788 case NEG:
9789 /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
9790 than the number of bits in the mode is equivalent to A. */
9791 if (code == LSHIFTRT
9792 && count == (unsigned int) (GET_MODE_BITSIZE (result_mode) - 1)
9793 && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
9794 {
9795 varop = XEXP (varop, 0);
9796 count = 0;
9797 continue;
9798 }
9799
9800 /* NEG commutes with ASHIFT since it is multiplication. Move the
9801 NEG outside to allow shifts to combine. */
9802 if (code == ASHIFT
9803 && merge_outer_ops (&outer_op, &outer_const, NEG,
9804 (HOST_WIDE_INT) 0, result_mode,
9805 &complement_p))
9806 {
9807 varop = XEXP (varop, 0);
9808 continue;
9809 }
9810 break;
9811
9812 case PLUS:
9813 /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
9814 is one less than the number of bits in the mode is
9815 equivalent to (xor A 1). */
9816 if (code == LSHIFTRT
9817 && count == (unsigned int) (GET_MODE_BITSIZE (result_mode) - 1)
9818 && XEXP (varop, 1) == constm1_rtx
9819 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
9820 && merge_outer_ops (&outer_op, &outer_const, XOR,
9821 (HOST_WIDE_INT) 1, result_mode,
9822 &complement_p))
9823 {
9824 count = 0;
9825 varop = XEXP (varop, 0);
9826 continue;
9827 }
9828
9829 /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
9830 that might be nonzero in BAR are those being shifted out and those
9831 bits are known zero in FOO, we can replace the PLUS with FOO.
9832 Similarly in the other operand order. This code occurs when
9833 we are computing the size of a variable-size array. */
9834
9835 if ((code == ASHIFTRT || code == LSHIFTRT)
9836 && count < HOST_BITS_PER_WIDE_INT
9837 && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
9838 && (nonzero_bits (XEXP (varop, 1), result_mode)
9839 & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
9840 {
9841 varop = XEXP (varop, 0);
9842 continue;
9843 }
9844 else if ((code == ASHIFTRT || code == LSHIFTRT)
9845 && count < HOST_BITS_PER_WIDE_INT
9846 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9847 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
9848 >> count)
9849 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
9850 & nonzero_bits (XEXP (varop, 1),
9851 result_mode)))
9852 {
9853 varop = XEXP (varop, 1);
9854 continue;
9855 }
9856
9857 /* (ashift (plus foo C) N) is (plus (ashift foo N) C'). */
9858 if (code == ASHIFT
9859 && GET_CODE (XEXP (varop, 1)) == CONST_INT
9860 && (new = simplify_binary_operation (ASHIFT, result_mode,
9861 XEXP (varop, 1),
9862 GEN_INT (count))) != 0
9863 && GET_CODE (new) == CONST_INT
9864 && merge_outer_ops (&outer_op, &outer_const, PLUS,
9865 INTVAL (new), result_mode, &complement_p))
9866 {
9867 varop = XEXP (varop, 0);
9868 continue;
9869 }
9870 break;
9871
9872 case MINUS:
9873 /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
9874 with C the size of VAROP - 1 and the shift is logical if
9875 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
9876 we have a (gt X 0) operation. If the shift is arithmetic with
9877 STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
9878 we have a (neg (gt X 0)) operation. */
9879
9880 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9881 && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
9882 && count == (unsigned int)
9883 (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
9884 && (code == LSHIFTRT || code == ASHIFTRT)
9885 && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
9886 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (varop, 0), 1))
9887 == count
9888 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
9889 {
9890 count = 0;
9891 varop = gen_rtx_GT (GET_MODE (varop), XEXP (varop, 1),
9892 const0_rtx);
9893
9894 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
9895 varop = gen_rtx_NEG (GET_MODE (varop), varop);
9896
9897 continue;
9898 }
9899 break;
9900
9901 case TRUNCATE:
9902 /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
9903 if the truncate does not affect the value. */
9904 if (code == LSHIFTRT
9905 && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
9906 && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
9907 && (INTVAL (XEXP (XEXP (varop, 0), 1))
9908 >= (GET_MODE_BITSIZE (GET_MODE (XEXP (varop, 0)))
9909 - GET_MODE_BITSIZE (GET_MODE (varop)))))
9910 {
9911 rtx varop_inner = XEXP (varop, 0);
9912
9913 varop_inner
9914 = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
9915 XEXP (varop_inner, 0),
9916 GEN_INT
9917 (count + INTVAL (XEXP (varop_inner, 1))));
9918 varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
9919 count = 0;
9920 continue;
9921 }
9922 break;
9923
9924 default:
9925 break;
9926 }
9927
9928 break;
9929 }
9930
9931 /* We need to determine what mode to do the shift in. If the shift is
9932 a right shift or ROTATE, we must always do it in the mode it was
9933 originally done in. Otherwise, we can do it in MODE, the widest mode
9934 encountered. The code we care about is that of the shift that will
9935 actually be done, not the shift that was originally requested. */
9936 shift_mode
9937 = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
9938 ? result_mode : mode);
9939
9940 /* We have now finished analyzing the shift. The result should be
9941 a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places. If
9942 OUTER_OP is non-NIL, it is an operation that needs to be applied
9943 to the result of the shift. OUTER_CONST is the relevant constant,
9944 but we must turn off all bits turned off in the shift.
9945
9946 If we were passed a value for X, see if we can use any pieces of
9947 it. If not, make new rtx. */
9948
9949 if (x && GET_RTX_CLASS (GET_CODE (x)) == '2'
9950 && GET_CODE (XEXP (x, 1)) == CONST_INT
9951 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (x, 1)) == count)
9952 const_rtx = XEXP (x, 1);
9953 else
9954 const_rtx = GEN_INT (count);
9955
9956 if (x && GET_CODE (XEXP (x, 0)) == SUBREG
9957 && GET_MODE (XEXP (x, 0)) == shift_mode
9958 && SUBREG_REG (XEXP (x, 0)) == varop)
9959 varop = XEXP (x, 0);
9960 else if (GET_MODE (varop) != shift_mode)
9961 varop = gen_lowpart_for_combine (shift_mode, varop);
9962
9963 /* If we can't make the SUBREG, try to return what we were given. */
9964 if (GET_CODE (varop) == CLOBBER)
9965 return x ? x : varop;
9966
9967 new = simplify_binary_operation (code, shift_mode, varop, const_rtx);
9968 if (new != 0)
9969 x = new;
9970 else
9971 x = gen_rtx_fmt_ee (code, shift_mode, varop, const_rtx);
9972
9973 /* If we have an outer operation and we just made a shift, it is
9974 possible that we could have simplified the shift were it not
9975 for the outer operation. So try to do the simplification
9976 recursively. */
9977
9978 if (outer_op != NIL && GET_CODE (x) == code
9979 && GET_CODE (XEXP (x, 1)) == CONST_INT)
9980 x = simplify_shift_const (x, code, shift_mode, XEXP (x, 0),
9981 INTVAL (XEXP (x, 1)));
9982
9983 /* If we were doing an LSHIFTRT in a wider mode than it was originally,
9984 turn off all the bits that the shift would have turned off. */
9985 if (orig_code == LSHIFTRT && result_mode != shift_mode)
9986 x = simplify_and_const_int (NULL_RTX, shift_mode, x,
9987 GET_MODE_MASK (result_mode) >> orig_count);
9988
9989 /* Do the remainder of the processing in RESULT_MODE. */
9990 x = gen_lowpart_for_combine (result_mode, x);
9991
9992 /* If COMPLEMENT_P is set, we have to complement X before doing the outer
9993 operation. */
9994 if (complement_p)
9995 x = simplify_gen_unary (NOT, result_mode, x, result_mode);
9996
9997 if (outer_op != NIL)
9998 {
9999 if (GET_MODE_BITSIZE (result_mode) < HOST_BITS_PER_WIDE_INT)
10000 outer_const = trunc_int_for_mode (outer_const, result_mode);
10001
10002 if (outer_op == AND)
10003 x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
10004 else if (outer_op == SET)
10005 /* This means that we have determined that the result is
10006 equivalent to a constant. This should be rare. */
10007 x = GEN_INT (outer_const);
10008 else if (GET_RTX_CLASS (outer_op) == '1')
10009 x = simplify_gen_unary (outer_op, result_mode, x, result_mode);
10010 else
10011 x = gen_binary (outer_op, result_mode, x, GEN_INT (outer_const));
10012 }
10013
10014 return x;
10015 }
10016 \f
10017 /* Like recog, but we receive the address of a pointer to a new pattern.
10018 We try to match the rtx that the pointer points to.
10019 If that fails, we may try to modify or replace the pattern,
10020 storing the replacement into the same pointer object.
10021
10022 Modifications include deletion or addition of CLOBBERs.
10023
10024 PNOTES is a pointer to a location where any REG_UNUSED notes added for
10025 the CLOBBERs are placed.
10026
10027 The value is the final insn code from the pattern ultimately matched,
10028 or -1. */
10029
10030 static int
10031 recog_for_combine (pnewpat, insn, pnotes)
10032 rtx *pnewpat;
10033 rtx insn;
10034 rtx *pnotes;
10035 {
10036 rtx pat = *pnewpat;
10037 int insn_code_number;
10038 int num_clobbers_to_add = 0;
10039 int i;
10040 rtx notes = 0;
10041 rtx dummy_insn;
10042
10043 /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
10044 we use to indicate that something didn't match. If we find such a
10045 thing, force rejection. */
10046 if (GET_CODE (pat) == PARALLEL)
10047 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
10048 if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
10049 && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
10050 return -1;
10051
10052 /* *pnewpat does not have to be actual PATTERN (insn), so make a dummy
10053 instruction for pattern recognition. */
10054 dummy_insn = shallow_copy_rtx (insn);
10055 PATTERN (dummy_insn) = pat;
10056 REG_NOTES (dummy_insn) = 0;
10057
10058 insn_code_number = recog (pat, dummy_insn, &num_clobbers_to_add);
10059
10060 /* If it isn't, there is the possibility that we previously had an insn
10061 that clobbered some register as a side effect, but the combined
10062 insn doesn't need to do that. So try once more without the clobbers
10063 unless this represents an ASM insn. */
10064
10065 if (insn_code_number < 0 && ! check_asm_operands (pat)
10066 && GET_CODE (pat) == PARALLEL)
10067 {
10068 int pos;
10069
10070 for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
10071 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
10072 {
10073 if (i != pos)
10074 SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
10075 pos++;
10076 }
10077
10078 SUBST_INT (XVECLEN (pat, 0), pos);
10079
10080 if (pos == 1)
10081 pat = XVECEXP (pat, 0, 0);
10082
10083 PATTERN (dummy_insn) = pat;
10084 insn_code_number = recog (pat, dummy_insn, &num_clobbers_to_add);
10085 }
10086
10087 /* Recognize all noop sets, these will be killed by followup pass. */
10088 if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
10089 insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
10090
10091 /* If we had any clobbers to add, make a new pattern than contains
10092 them. Then check to make sure that all of them are dead. */
10093 if (num_clobbers_to_add)
10094 {
10095 rtx newpat = gen_rtx_PARALLEL (VOIDmode,
10096 rtvec_alloc (GET_CODE (pat) == PARALLEL
10097 ? (XVECLEN (pat, 0)
10098 + num_clobbers_to_add)
10099 : num_clobbers_to_add + 1));
10100
10101 if (GET_CODE (pat) == PARALLEL)
10102 for (i = 0; i < XVECLEN (pat, 0); i++)
10103 XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
10104 else
10105 XVECEXP (newpat, 0, 0) = pat;
10106
10107 add_clobbers (newpat, insn_code_number);
10108
10109 for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
10110 i < XVECLEN (newpat, 0); i++)
10111 {
10112 if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) == REG
10113 && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
10114 return -1;
10115 notes = gen_rtx_EXPR_LIST (REG_UNUSED,
10116 XEXP (XVECEXP (newpat, 0, i), 0), notes);
10117 }
10118 pat = newpat;
10119 }
10120
10121 *pnewpat = pat;
10122 *pnotes = notes;
10123
10124 return insn_code_number;
10125 }
10126 \f
10127 /* Like gen_lowpart but for use by combine. In combine it is not possible
10128 to create any new pseudoregs. However, it is safe to create
10129 invalid memory addresses, because combine will try to recognize
10130 them and all they will do is make the combine attempt fail.
10131
10132 If for some reason this cannot do its job, an rtx
10133 (clobber (const_int 0)) is returned.
10134 An insn containing that will not be recognized. */
10135
10136 #undef gen_lowpart
10137
10138 static rtx
10139 gen_lowpart_for_combine (mode, x)
10140 enum machine_mode mode;
10141 rtx x;
10142 {
10143 rtx result;
10144
10145 if (GET_MODE (x) == mode)
10146 return x;
10147
10148 /* Return identity if this is a CONST or symbolic
10149 reference. */
10150 if (mode == Pmode
10151 && (GET_CODE (x) == CONST
10152 || GET_CODE (x) == SYMBOL_REF
10153 || GET_CODE (x) == LABEL_REF))
10154 return x;
10155
10156 /* We can only support MODE being wider than a word if X is a
10157 constant integer or has a mode the same size. */
10158
10159 if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
10160 && ! ((GET_MODE (x) == VOIDmode
10161 && (GET_CODE (x) == CONST_INT
10162 || GET_CODE (x) == CONST_DOUBLE))
10163 || GET_MODE_SIZE (GET_MODE (x)) == GET_MODE_SIZE (mode)))
10164 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
10165
10166 /* X might be a paradoxical (subreg (mem)). In that case, gen_lowpart
10167 won't know what to do. So we will strip off the SUBREG here and
10168 process normally. */
10169 if (GET_CODE (x) == SUBREG && GET_CODE (SUBREG_REG (x)) == MEM)
10170 {
10171 x = SUBREG_REG (x);
10172 if (GET_MODE (x) == mode)
10173 return x;
10174 }
10175
10176 result = gen_lowpart_common (mode, x);
10177 #ifdef CANNOT_CHANGE_MODE_CLASS
10178 if (result != 0
10179 && GET_CODE (result) == SUBREG
10180 && GET_CODE (SUBREG_REG (result)) == REG
10181 && REGNO (SUBREG_REG (result)) >= FIRST_PSEUDO_REGISTER)
10182 bitmap_set_bit (&subregs_of_mode, REGNO (SUBREG_REG (result))
10183 * MAX_MACHINE_MODE
10184 + GET_MODE (result));
10185 #endif
10186
10187 if (result)
10188 return result;
10189
10190 if (GET_CODE (x) == MEM)
10191 {
10192 int offset = 0;
10193
10194 /* Refuse to work on a volatile memory ref or one with a mode-dependent
10195 address. */
10196 if (MEM_VOLATILE_P (x) || mode_dependent_address_p (XEXP (x, 0)))
10197 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
10198
10199 /* If we want to refer to something bigger than the original memref,
10200 generate a perverse subreg instead. That will force a reload
10201 of the original memref X. */
10202 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode))
10203 return gen_rtx_SUBREG (mode, x, 0);
10204
10205 if (WORDS_BIG_ENDIAN)
10206 offset = (MAX (GET_MODE_SIZE (GET_MODE (x)), UNITS_PER_WORD)
10207 - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD));
10208
10209 if (BYTES_BIG_ENDIAN)
10210 {
10211 /* Adjust the address so that the address-after-the-data is
10212 unchanged. */
10213 offset -= (MIN (UNITS_PER_WORD, GET_MODE_SIZE (mode))
10214 - MIN (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (x))));
10215 }
10216
10217 return adjust_address_nv (x, mode, offset);
10218 }
10219
10220 /* If X is a comparison operator, rewrite it in a new mode. This
10221 probably won't match, but may allow further simplifications. */
10222 else if (GET_RTX_CLASS (GET_CODE (x)) == '<')
10223 return gen_rtx_fmt_ee (GET_CODE (x), mode, XEXP (x, 0), XEXP (x, 1));
10224
10225 /* If we couldn't simplify X any other way, just enclose it in a
10226 SUBREG. Normally, this SUBREG won't match, but some patterns may
10227 include an explicit SUBREG or we may simplify it further in combine. */
10228 else
10229 {
10230 int offset = 0;
10231 rtx res;
10232 enum machine_mode sub_mode = GET_MODE (x);
10233
10234 offset = subreg_lowpart_offset (mode, sub_mode);
10235 if (sub_mode == VOIDmode)
10236 {
10237 sub_mode = int_mode_for_mode (mode);
10238 x = gen_lowpart_common (sub_mode, x);
10239 if (x == 0)
10240 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
10241 }
10242 res = simplify_gen_subreg (mode, x, sub_mode, offset);
10243 if (res)
10244 return res;
10245 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
10246 }
10247 }
10248 \f
10249 /* These routines make binary and unary operations by first seeing if they
10250 fold; if not, a new expression is allocated. */
10251
10252 static rtx
10253 gen_binary (code, mode, op0, op1)
10254 enum rtx_code code;
10255 enum machine_mode mode;
10256 rtx op0, op1;
10257 {
10258 rtx result;
10259 rtx tem;
10260
10261 if (GET_RTX_CLASS (code) == 'c'
10262 && swap_commutative_operands_p (op0, op1))
10263 tem = op0, op0 = op1, op1 = tem;
10264
10265 if (GET_RTX_CLASS (code) == '<')
10266 {
10267 enum machine_mode op_mode = GET_MODE (op0);
10268
10269 /* Strip the COMPARE from (REL_OP (compare X Y) 0) to get
10270 just (REL_OP X Y). */
10271 if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
10272 {
10273 op1 = XEXP (op0, 1);
10274 op0 = XEXP (op0, 0);
10275 op_mode = GET_MODE (op0);
10276 }
10277
10278 if (op_mode == VOIDmode)
10279 op_mode = GET_MODE (op1);
10280 result = simplify_relational_operation (code, op_mode, op0, op1);
10281 }
10282 else
10283 result = simplify_binary_operation (code, mode, op0, op1);
10284
10285 if (result)
10286 return result;
10287
10288 /* Put complex operands first and constants second. */
10289 if (GET_RTX_CLASS (code) == 'c'
10290 && swap_commutative_operands_p (op0, op1))
10291 return gen_rtx_fmt_ee (code, mode, op1, op0);
10292
10293 /* If we are turning off bits already known off in OP0, we need not do
10294 an AND. */
10295 else if (code == AND && GET_CODE (op1) == CONST_INT
10296 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
10297 && (nonzero_bits (op0, mode) & ~INTVAL (op1)) == 0)
10298 return op0;
10299
10300 return gen_rtx_fmt_ee (code, mode, op0, op1);
10301 }
10302 \f
10303 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
10304 comparison code that will be tested.
10305
10306 The result is a possibly different comparison code to use. *POP0 and
10307 *POP1 may be updated.
10308
10309 It is possible that we might detect that a comparison is either always
10310 true or always false. However, we do not perform general constant
10311 folding in combine, so this knowledge isn't useful. Such tautologies
10312 should have been detected earlier. Hence we ignore all such cases. */
10313
10314 static enum rtx_code
10315 simplify_comparison (code, pop0, pop1)
10316 enum rtx_code code;
10317 rtx *pop0;
10318 rtx *pop1;
10319 {
10320 rtx op0 = *pop0;
10321 rtx op1 = *pop1;
10322 rtx tem, tem1;
10323 int i;
10324 enum machine_mode mode, tmode;
10325
10326 /* Try a few ways of applying the same transformation to both operands. */
10327 while (1)
10328 {
10329 #ifndef WORD_REGISTER_OPERATIONS
10330 /* The test below this one won't handle SIGN_EXTENDs on these machines,
10331 so check specially. */
10332 if (code != GTU && code != GEU && code != LTU && code != LEU
10333 && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
10334 && GET_CODE (XEXP (op0, 0)) == ASHIFT
10335 && GET_CODE (XEXP (op1, 0)) == ASHIFT
10336 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
10337 && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
10338 && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
10339 == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
10340 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10341 && GET_CODE (XEXP (op1, 1)) == CONST_INT
10342 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10343 && GET_CODE (XEXP (XEXP (op1, 0), 1)) == CONST_INT
10344 && INTVAL (XEXP (op0, 1)) == INTVAL (XEXP (op1, 1))
10345 && INTVAL (XEXP (op0, 1)) == INTVAL (XEXP (XEXP (op0, 0), 1))
10346 && INTVAL (XEXP (op0, 1)) == INTVAL (XEXP (XEXP (op1, 0), 1))
10347 && (INTVAL (XEXP (op0, 1))
10348 == (GET_MODE_BITSIZE (GET_MODE (op0))
10349 - (GET_MODE_BITSIZE
10350 (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
10351 {
10352 op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
10353 op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
10354 }
10355 #endif
10356
10357 /* If both operands are the same constant shift, see if we can ignore the
10358 shift. We can if the shift is a rotate or if the bits shifted out of
10359 this shift are known to be zero for both inputs and if the type of
10360 comparison is compatible with the shift. */
10361 if (GET_CODE (op0) == GET_CODE (op1)
10362 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
10363 && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
10364 || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
10365 && (code != GT && code != LT && code != GE && code != LE))
10366 || (GET_CODE (op0) == ASHIFTRT
10367 && (code != GTU && code != LTU
10368 && code != GEU && code != LEU)))
10369 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10370 && INTVAL (XEXP (op0, 1)) >= 0
10371 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
10372 && XEXP (op0, 1) == XEXP (op1, 1))
10373 {
10374 enum machine_mode mode = GET_MODE (op0);
10375 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
10376 int shift_count = INTVAL (XEXP (op0, 1));
10377
10378 if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
10379 mask &= (mask >> shift_count) << shift_count;
10380 else if (GET_CODE (op0) == ASHIFT)
10381 mask = (mask & (mask << shift_count)) >> shift_count;
10382
10383 if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
10384 && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
10385 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
10386 else
10387 break;
10388 }
10389
10390 /* If both operands are AND's of a paradoxical SUBREG by constant, the
10391 SUBREGs are of the same mode, and, in both cases, the AND would
10392 be redundant if the comparison was done in the narrower mode,
10393 do the comparison in the narrower mode (e.g., we are AND'ing with 1
10394 and the operand's possibly nonzero bits are 0xffffff01; in that case
10395 if we only care about QImode, we don't need the AND). This case
10396 occurs if the output mode of an scc insn is not SImode and
10397 STORE_FLAG_VALUE == 1 (e.g., the 386).
10398
10399 Similarly, check for a case where the AND's are ZERO_EXTEND
10400 operations from some narrower mode even though a SUBREG is not
10401 present. */
10402
10403 else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
10404 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10405 && GET_CODE (XEXP (op1, 1)) == CONST_INT)
10406 {
10407 rtx inner_op0 = XEXP (op0, 0);
10408 rtx inner_op1 = XEXP (op1, 0);
10409 HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
10410 HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
10411 int changed = 0;
10412
10413 if (GET_CODE (inner_op0) == SUBREG && GET_CODE (inner_op1) == SUBREG
10414 && (GET_MODE_SIZE (GET_MODE (inner_op0))
10415 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (inner_op0))))
10416 && (GET_MODE (SUBREG_REG (inner_op0))
10417 == GET_MODE (SUBREG_REG (inner_op1)))
10418 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (inner_op0)))
10419 <= HOST_BITS_PER_WIDE_INT)
10420 && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
10421 GET_MODE (SUBREG_REG (inner_op0)))))
10422 && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
10423 GET_MODE (SUBREG_REG (inner_op1))))))
10424 {
10425 op0 = SUBREG_REG (inner_op0);
10426 op1 = SUBREG_REG (inner_op1);
10427
10428 /* The resulting comparison is always unsigned since we masked
10429 off the original sign bit. */
10430 code = unsigned_condition (code);
10431
10432 changed = 1;
10433 }
10434
10435 else if (c0 == c1)
10436 for (tmode = GET_CLASS_NARROWEST_MODE
10437 (GET_MODE_CLASS (GET_MODE (op0)));
10438 tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
10439 if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
10440 {
10441 op0 = gen_lowpart_for_combine (tmode, inner_op0);
10442 op1 = gen_lowpart_for_combine (tmode, inner_op1);
10443 code = unsigned_condition (code);
10444 changed = 1;
10445 break;
10446 }
10447
10448 if (! changed)
10449 break;
10450 }
10451
10452 /* If both operands are NOT, we can strip off the outer operation
10453 and adjust the comparison code for swapped operands; similarly for
10454 NEG, except that this must be an equality comparison. */
10455 else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
10456 || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
10457 && (code == EQ || code == NE)))
10458 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
10459
10460 else
10461 break;
10462 }
10463
10464 /* If the first operand is a constant, swap the operands and adjust the
10465 comparison code appropriately, but don't do this if the second operand
10466 is already a constant integer. */
10467 if (swap_commutative_operands_p (op0, op1))
10468 {
10469 tem = op0, op0 = op1, op1 = tem;
10470 code = swap_condition (code);
10471 }
10472
10473 /* We now enter a loop during which we will try to simplify the comparison.
10474 For the most part, we only are concerned with comparisons with zero,
10475 but some things may really be comparisons with zero but not start
10476 out looking that way. */
10477
10478 while (GET_CODE (op1) == CONST_INT)
10479 {
10480 enum machine_mode mode = GET_MODE (op0);
10481 unsigned int mode_width = GET_MODE_BITSIZE (mode);
10482 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
10483 int equality_comparison_p;
10484 int sign_bit_comparison_p;
10485 int unsigned_comparison_p;
10486 HOST_WIDE_INT const_op;
10487
10488 /* We only want to handle integral modes. This catches VOIDmode,
10489 CCmode, and the floating-point modes. An exception is that we
10490 can handle VOIDmode if OP0 is a COMPARE or a comparison
10491 operation. */
10492
10493 if (GET_MODE_CLASS (mode) != MODE_INT
10494 && ! (mode == VOIDmode
10495 && (GET_CODE (op0) == COMPARE
10496 || GET_RTX_CLASS (GET_CODE (op0)) == '<')))
10497 break;
10498
10499 /* Get the constant we are comparing against and turn off all bits
10500 not on in our mode. */
10501 const_op = INTVAL (op1);
10502 if (mode != VOIDmode)
10503 const_op = trunc_int_for_mode (const_op, mode);
10504 op1 = GEN_INT (const_op);
10505
10506 /* If we are comparing against a constant power of two and the value
10507 being compared can only have that single bit nonzero (e.g., it was
10508 `and'ed with that bit), we can replace this with a comparison
10509 with zero. */
10510 if (const_op
10511 && (code == EQ || code == NE || code == GE || code == GEU
10512 || code == LT || code == LTU)
10513 && mode_width <= HOST_BITS_PER_WIDE_INT
10514 && exact_log2 (const_op) >= 0
10515 && nonzero_bits (op0, mode) == (unsigned HOST_WIDE_INT) const_op)
10516 {
10517 code = (code == EQ || code == GE || code == GEU ? NE : EQ);
10518 op1 = const0_rtx, const_op = 0;
10519 }
10520
10521 /* Similarly, if we are comparing a value known to be either -1 or
10522 0 with -1, change it to the opposite comparison against zero. */
10523
10524 if (const_op == -1
10525 && (code == EQ || code == NE || code == GT || code == LE
10526 || code == GEU || code == LTU)
10527 && num_sign_bit_copies (op0, mode) == mode_width)
10528 {
10529 code = (code == EQ || code == LE || code == GEU ? NE : EQ);
10530 op1 = const0_rtx, const_op = 0;
10531 }
10532
10533 /* Do some canonicalizations based on the comparison code. We prefer
10534 comparisons against zero and then prefer equality comparisons.
10535 If we can reduce the size of a constant, we will do that too. */
10536
10537 switch (code)
10538 {
10539 case LT:
10540 /* < C is equivalent to <= (C - 1) */
10541 if (const_op > 0)
10542 {
10543 const_op -= 1;
10544 op1 = GEN_INT (const_op);
10545 code = LE;
10546 /* ... fall through to LE case below. */
10547 }
10548 else
10549 break;
10550
10551 case LE:
10552 /* <= C is equivalent to < (C + 1); we do this for C < 0 */
10553 if (const_op < 0)
10554 {
10555 const_op += 1;
10556 op1 = GEN_INT (const_op);
10557 code = LT;
10558 }
10559
10560 /* If we are doing a <= 0 comparison on a value known to have
10561 a zero sign bit, we can replace this with == 0. */
10562 else if (const_op == 0
10563 && mode_width <= HOST_BITS_PER_WIDE_INT
10564 && (nonzero_bits (op0, mode)
10565 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
10566 code = EQ;
10567 break;
10568
10569 case GE:
10570 /* >= C is equivalent to > (C - 1). */
10571 if (const_op > 0)
10572 {
10573 const_op -= 1;
10574 op1 = GEN_INT (const_op);
10575 code = GT;
10576 /* ... fall through to GT below. */
10577 }
10578 else
10579 break;
10580
10581 case GT:
10582 /* > C is equivalent to >= (C + 1); we do this for C < 0. */
10583 if (const_op < 0)
10584 {
10585 const_op += 1;
10586 op1 = GEN_INT (const_op);
10587 code = GE;
10588 }
10589
10590 /* If we are doing a > 0 comparison on a value known to have
10591 a zero sign bit, we can replace this with != 0. */
10592 else if (const_op == 0
10593 && mode_width <= HOST_BITS_PER_WIDE_INT
10594 && (nonzero_bits (op0, mode)
10595 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
10596 code = NE;
10597 break;
10598
10599 case LTU:
10600 /* < C is equivalent to <= (C - 1). */
10601 if (const_op > 0)
10602 {
10603 const_op -= 1;
10604 op1 = GEN_INT (const_op);
10605 code = LEU;
10606 /* ... fall through ... */
10607 }
10608
10609 /* (unsigned) < 0x80000000 is equivalent to >= 0. */
10610 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10611 && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
10612 {
10613 const_op = 0, op1 = const0_rtx;
10614 code = GE;
10615 break;
10616 }
10617 else
10618 break;
10619
10620 case LEU:
10621 /* unsigned <= 0 is equivalent to == 0 */
10622 if (const_op == 0)
10623 code = EQ;
10624
10625 /* (unsigned) <= 0x7fffffff is equivalent to >= 0. */
10626 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10627 && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
10628 {
10629 const_op = 0, op1 = const0_rtx;
10630 code = GE;
10631 }
10632 break;
10633
10634 case GEU:
10635 /* >= C is equivalent to < (C - 1). */
10636 if (const_op > 1)
10637 {
10638 const_op -= 1;
10639 op1 = GEN_INT (const_op);
10640 code = GTU;
10641 /* ... fall through ... */
10642 }
10643
10644 /* (unsigned) >= 0x80000000 is equivalent to < 0. */
10645 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10646 && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
10647 {
10648 const_op = 0, op1 = const0_rtx;
10649 code = LT;
10650 break;
10651 }
10652 else
10653 break;
10654
10655 case GTU:
10656 /* unsigned > 0 is equivalent to != 0 */
10657 if (const_op == 0)
10658 code = NE;
10659
10660 /* (unsigned) > 0x7fffffff is equivalent to < 0. */
10661 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10662 && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
10663 {
10664 const_op = 0, op1 = const0_rtx;
10665 code = LT;
10666 }
10667 break;
10668
10669 default:
10670 break;
10671 }
10672
10673 /* Compute some predicates to simplify code below. */
10674
10675 equality_comparison_p = (code == EQ || code == NE);
10676 sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
10677 unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
10678 || code == GEU);
10679
10680 /* If this is a sign bit comparison and we can do arithmetic in
10681 MODE, say that we will only be needing the sign bit of OP0. */
10682 if (sign_bit_comparison_p
10683 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10684 op0 = force_to_mode (op0, mode,
10685 ((HOST_WIDE_INT) 1
10686 << (GET_MODE_BITSIZE (mode) - 1)),
10687 NULL_RTX, 0);
10688
10689 /* Now try cases based on the opcode of OP0. If none of the cases
10690 does a "continue", we exit this loop immediately after the
10691 switch. */
10692
10693 switch (GET_CODE (op0))
10694 {
10695 case ZERO_EXTRACT:
10696 /* If we are extracting a single bit from a variable position in
10697 a constant that has only a single bit set and are comparing it
10698 with zero, we can convert this into an equality comparison
10699 between the position and the location of the single bit. */
10700
10701 if (GET_CODE (XEXP (op0, 0)) == CONST_INT
10702 && XEXP (op0, 1) == const1_rtx
10703 && equality_comparison_p && const_op == 0
10704 && (i = exact_log2 (INTVAL (XEXP (op0, 0)))) >= 0)
10705 {
10706 if (BITS_BIG_ENDIAN)
10707 {
10708 enum machine_mode new_mode
10709 = mode_for_extraction (EP_extzv, 1);
10710 if (new_mode == MAX_MACHINE_MODE)
10711 i = BITS_PER_WORD - 1 - i;
10712 else
10713 {
10714 mode = new_mode;
10715 i = (GET_MODE_BITSIZE (mode) - 1 - i);
10716 }
10717 }
10718
10719 op0 = XEXP (op0, 2);
10720 op1 = GEN_INT (i);
10721 const_op = i;
10722
10723 /* Result is nonzero iff shift count is equal to I. */
10724 code = reverse_condition (code);
10725 continue;
10726 }
10727
10728 /* ... fall through ... */
10729
10730 case SIGN_EXTRACT:
10731 tem = expand_compound_operation (op0);
10732 if (tem != op0)
10733 {
10734 op0 = tem;
10735 continue;
10736 }
10737 break;
10738
10739 case NOT:
10740 /* If testing for equality, we can take the NOT of the constant. */
10741 if (equality_comparison_p
10742 && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
10743 {
10744 op0 = XEXP (op0, 0);
10745 op1 = tem;
10746 continue;
10747 }
10748
10749 /* If just looking at the sign bit, reverse the sense of the
10750 comparison. */
10751 if (sign_bit_comparison_p)
10752 {
10753 op0 = XEXP (op0, 0);
10754 code = (code == GE ? LT : GE);
10755 continue;
10756 }
10757 break;
10758
10759 case NEG:
10760 /* If testing for equality, we can take the NEG of the constant. */
10761 if (equality_comparison_p
10762 && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
10763 {
10764 op0 = XEXP (op0, 0);
10765 op1 = tem;
10766 continue;
10767 }
10768
10769 /* The remaining cases only apply to comparisons with zero. */
10770 if (const_op != 0)
10771 break;
10772
10773 /* When X is ABS or is known positive,
10774 (neg X) is < 0 if and only if X != 0. */
10775
10776 if (sign_bit_comparison_p
10777 && (GET_CODE (XEXP (op0, 0)) == ABS
10778 || (mode_width <= HOST_BITS_PER_WIDE_INT
10779 && (nonzero_bits (XEXP (op0, 0), mode)
10780 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)))
10781 {
10782 op0 = XEXP (op0, 0);
10783 code = (code == LT ? NE : EQ);
10784 continue;
10785 }
10786
10787 /* If we have NEG of something whose two high-order bits are the
10788 same, we know that "(-a) < 0" is equivalent to "a > 0". */
10789 if (num_sign_bit_copies (op0, mode) >= 2)
10790 {
10791 op0 = XEXP (op0, 0);
10792 code = swap_condition (code);
10793 continue;
10794 }
10795 break;
10796
10797 case ROTATE:
10798 /* If we are testing equality and our count is a constant, we
10799 can perform the inverse operation on our RHS. */
10800 if (equality_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
10801 && (tem = simplify_binary_operation (ROTATERT, mode,
10802 op1, XEXP (op0, 1))) != 0)
10803 {
10804 op0 = XEXP (op0, 0);
10805 op1 = tem;
10806 continue;
10807 }
10808
10809 /* If we are doing a < 0 or >= 0 comparison, it means we are testing
10810 a particular bit. Convert it to an AND of a constant of that
10811 bit. This will be converted into a ZERO_EXTRACT. */
10812 if (const_op == 0 && sign_bit_comparison_p
10813 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10814 && mode_width <= HOST_BITS_PER_WIDE_INT)
10815 {
10816 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10817 ((HOST_WIDE_INT) 1
10818 << (mode_width - 1
10819 - INTVAL (XEXP (op0, 1)))));
10820 code = (code == LT ? NE : EQ);
10821 continue;
10822 }
10823
10824 /* Fall through. */
10825
10826 case ABS:
10827 /* ABS is ignorable inside an equality comparison with zero. */
10828 if (const_op == 0 && equality_comparison_p)
10829 {
10830 op0 = XEXP (op0, 0);
10831 continue;
10832 }
10833 break;
10834
10835 case SIGN_EXTEND:
10836 /* Can simplify (compare (zero/sign_extend FOO) CONST)
10837 to (compare FOO CONST) if CONST fits in FOO's mode and we
10838 are either testing inequality or have an unsigned comparison
10839 with ZERO_EXTEND or a signed comparison with SIGN_EXTEND. */
10840 if (! unsigned_comparison_p
10841 && (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10842 <= HOST_BITS_PER_WIDE_INT)
10843 && ((unsigned HOST_WIDE_INT) const_op
10844 < (((unsigned HOST_WIDE_INT) 1
10845 << (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))) - 1)))))
10846 {
10847 op0 = XEXP (op0, 0);
10848 continue;
10849 }
10850 break;
10851
10852 case SUBREG:
10853 /* Check for the case where we are comparing A - C1 with C2,
10854 both constants are smaller than 1/2 the maximum positive
10855 value in MODE, and the comparison is equality or unsigned.
10856 In that case, if A is either zero-extended to MODE or has
10857 sufficient sign bits so that the high-order bit in MODE
10858 is a copy of the sign in the inner mode, we can prove that it is
10859 safe to do the operation in the wider mode. This simplifies
10860 many range checks. */
10861
10862 if (mode_width <= HOST_BITS_PER_WIDE_INT
10863 && subreg_lowpart_p (op0)
10864 && GET_CODE (SUBREG_REG (op0)) == PLUS
10865 && GET_CODE (XEXP (SUBREG_REG (op0), 1)) == CONST_INT
10866 && INTVAL (XEXP (SUBREG_REG (op0), 1)) < 0
10867 && (-INTVAL (XEXP (SUBREG_REG (op0), 1))
10868 < (HOST_WIDE_INT) (GET_MODE_MASK (mode) / 2))
10869 && (unsigned HOST_WIDE_INT) const_op < GET_MODE_MASK (mode) / 2
10870 && (0 == (nonzero_bits (XEXP (SUBREG_REG (op0), 0),
10871 GET_MODE (SUBREG_REG (op0)))
10872 & ~GET_MODE_MASK (mode))
10873 || (num_sign_bit_copies (XEXP (SUBREG_REG (op0), 0),
10874 GET_MODE (SUBREG_REG (op0)))
10875 > (unsigned int)
10876 (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
10877 - GET_MODE_BITSIZE (mode)))))
10878 {
10879 op0 = SUBREG_REG (op0);
10880 continue;
10881 }
10882
10883 /* If the inner mode is narrower and we are extracting the low part,
10884 we can treat the SUBREG as if it were a ZERO_EXTEND. */
10885 if (subreg_lowpart_p (op0)
10886 && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) < mode_width)
10887 /* Fall through */ ;
10888 else
10889 break;
10890
10891 /* ... fall through ... */
10892
10893 case ZERO_EXTEND:
10894 if ((unsigned_comparison_p || equality_comparison_p)
10895 && (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10896 <= HOST_BITS_PER_WIDE_INT)
10897 && ((unsigned HOST_WIDE_INT) const_op
10898 < GET_MODE_MASK (GET_MODE (XEXP (op0, 0)))))
10899 {
10900 op0 = XEXP (op0, 0);
10901 continue;
10902 }
10903 break;
10904
10905 case PLUS:
10906 /* (eq (plus X A) B) -> (eq X (minus B A)). We can only do
10907 this for equality comparisons due to pathological cases involving
10908 overflows. */
10909 if (equality_comparison_p
10910 && 0 != (tem = simplify_binary_operation (MINUS, mode,
10911 op1, XEXP (op0, 1))))
10912 {
10913 op0 = XEXP (op0, 0);
10914 op1 = tem;
10915 continue;
10916 }
10917
10918 /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0. */
10919 if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
10920 && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
10921 {
10922 op0 = XEXP (XEXP (op0, 0), 0);
10923 code = (code == LT ? EQ : NE);
10924 continue;
10925 }
10926 break;
10927
10928 case MINUS:
10929 /* We used to optimize signed comparisons against zero, but that
10930 was incorrect. Unsigned comparisons against zero (GTU, LEU)
10931 arrive here as equality comparisons, or (GEU, LTU) are
10932 optimized away. No need to special-case them. */
10933
10934 /* (eq (minus A B) C) -> (eq A (plus B C)) or
10935 (eq B (minus A C)), whichever simplifies. We can only do
10936 this for equality comparisons due to pathological cases involving
10937 overflows. */
10938 if (equality_comparison_p
10939 && 0 != (tem = simplify_binary_operation (PLUS, mode,
10940 XEXP (op0, 1), op1)))
10941 {
10942 op0 = XEXP (op0, 0);
10943 op1 = tem;
10944 continue;
10945 }
10946
10947 if (equality_comparison_p
10948 && 0 != (tem = simplify_binary_operation (MINUS, mode,
10949 XEXP (op0, 0), op1)))
10950 {
10951 op0 = XEXP (op0, 1);
10952 op1 = tem;
10953 continue;
10954 }
10955
10956 /* The sign bit of (minus (ashiftrt X C) X), where C is the number
10957 of bits in X minus 1, is one iff X > 0. */
10958 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
10959 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10960 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (op0, 0), 1))
10961 == mode_width - 1
10962 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10963 {
10964 op0 = XEXP (op0, 1);
10965 code = (code == GE ? LE : GT);
10966 continue;
10967 }
10968 break;
10969
10970 case XOR:
10971 /* (eq (xor A B) C) -> (eq A (xor B C)). This is a simplification
10972 if C is zero or B is a constant. */
10973 if (equality_comparison_p
10974 && 0 != (tem = simplify_binary_operation (XOR, mode,
10975 XEXP (op0, 1), op1)))
10976 {
10977 op0 = XEXP (op0, 0);
10978 op1 = tem;
10979 continue;
10980 }
10981 break;
10982
10983 case EQ: case NE:
10984 case UNEQ: case LTGT:
10985 case LT: case LTU: case UNLT: case LE: case LEU: case UNLE:
10986 case GT: case GTU: case UNGT: case GE: case GEU: case UNGE:
10987 case UNORDERED: case ORDERED:
10988 /* We can't do anything if OP0 is a condition code value, rather
10989 than an actual data value. */
10990 if (const_op != 0
10991 || CC0_P (XEXP (op0, 0))
10992 || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
10993 break;
10994
10995 /* Get the two operands being compared. */
10996 if (GET_CODE (XEXP (op0, 0)) == COMPARE)
10997 tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
10998 else
10999 tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
11000
11001 /* Check for the cases where we simply want the result of the
11002 earlier test or the opposite of that result. */
11003 if (code == NE || code == EQ
11004 || (GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
11005 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
11006 && (STORE_FLAG_VALUE
11007 & (((HOST_WIDE_INT) 1
11008 << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
11009 && (code == LT || code == GE)))
11010 {
11011 enum rtx_code new_code;
11012 if (code == LT || code == NE)
11013 new_code = GET_CODE (op0);
11014 else
11015 new_code = combine_reversed_comparison_code (op0);
11016
11017 if (new_code != UNKNOWN)
11018 {
11019 code = new_code;
11020 op0 = tem;
11021 op1 = tem1;
11022 continue;
11023 }
11024 }
11025 break;
11026
11027 case IOR:
11028 /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
11029 iff X <= 0. */
11030 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
11031 && XEXP (XEXP (op0, 0), 1) == constm1_rtx
11032 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
11033 {
11034 op0 = XEXP (op0, 1);
11035 code = (code == GE ? GT : LE);
11036 continue;
11037 }
11038 break;
11039
11040 case AND:
11041 /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1). This
11042 will be converted to a ZERO_EXTRACT later. */
11043 if (const_op == 0 && equality_comparison_p
11044 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11045 && XEXP (XEXP (op0, 0), 0) == const1_rtx)
11046 {
11047 op0 = simplify_and_const_int
11048 (op0, mode, gen_rtx_LSHIFTRT (mode,
11049 XEXP (op0, 1),
11050 XEXP (XEXP (op0, 0), 1)),
11051 (HOST_WIDE_INT) 1);
11052 continue;
11053 }
11054
11055 /* If we are comparing (and (lshiftrt X C1) C2) for equality with
11056 zero and X is a comparison and C1 and C2 describe only bits set
11057 in STORE_FLAG_VALUE, we can compare with X. */
11058 if (const_op == 0 && equality_comparison_p
11059 && mode_width <= HOST_BITS_PER_WIDE_INT
11060 && GET_CODE (XEXP (op0, 1)) == CONST_INT
11061 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
11062 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
11063 && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
11064 && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
11065 {
11066 mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
11067 << INTVAL (XEXP (XEXP (op0, 0), 1)));
11068 if ((~STORE_FLAG_VALUE & mask) == 0
11069 && (GET_RTX_CLASS (GET_CODE (XEXP (XEXP (op0, 0), 0))) == '<'
11070 || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
11071 && GET_RTX_CLASS (GET_CODE (tem)) == '<')))
11072 {
11073 op0 = XEXP (XEXP (op0, 0), 0);
11074 continue;
11075 }
11076 }
11077
11078 /* If we are doing an equality comparison of an AND of a bit equal
11079 to the sign bit, replace this with a LT or GE comparison of
11080 the underlying value. */
11081 if (equality_comparison_p
11082 && const_op == 0
11083 && GET_CODE (XEXP (op0, 1)) == CONST_INT
11084 && mode_width <= HOST_BITS_PER_WIDE_INT
11085 && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
11086 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11087 {
11088 op0 = XEXP (op0, 0);
11089 code = (code == EQ ? GE : LT);
11090 continue;
11091 }
11092
11093 /* If this AND operation is really a ZERO_EXTEND from a narrower
11094 mode, the constant fits within that mode, and this is either an
11095 equality or unsigned comparison, try to do this comparison in
11096 the narrower mode. */
11097 if ((equality_comparison_p || unsigned_comparison_p)
11098 && GET_CODE (XEXP (op0, 1)) == CONST_INT
11099 && (i = exact_log2 ((INTVAL (XEXP (op0, 1))
11100 & GET_MODE_MASK (mode))
11101 + 1)) >= 0
11102 && const_op >> i == 0
11103 && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode)
11104 {
11105 op0 = gen_lowpart_for_combine (tmode, XEXP (op0, 0));
11106 continue;
11107 }
11108
11109 /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1
11110 fits in both M1 and M2 and the SUBREG is either paradoxical
11111 or represents the low part, permute the SUBREG and the AND
11112 and try again. */
11113 if (GET_CODE (XEXP (op0, 0)) == SUBREG)
11114 {
11115 unsigned HOST_WIDE_INT c1;
11116 tmode = GET_MODE (SUBREG_REG (XEXP (op0, 0)));
11117 /* Require an integral mode, to avoid creating something like
11118 (AND:SF ...). */
11119 if (SCALAR_INT_MODE_P (tmode)
11120 /* It is unsafe to commute the AND into the SUBREG if the
11121 SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
11122 not defined. As originally written the upper bits
11123 have a defined value due to the AND operation.
11124 However, if we commute the AND inside the SUBREG then
11125 they no longer have defined values and the meaning of
11126 the code has been changed. */
11127 && (0
11128 #ifdef WORD_REGISTER_OPERATIONS
11129 || (mode_width > GET_MODE_BITSIZE (tmode)
11130 && mode_width <= BITS_PER_WORD)
11131 #endif
11132 || (mode_width <= GET_MODE_BITSIZE (tmode)
11133 && subreg_lowpart_p (XEXP (op0, 0))))
11134 && GET_CODE (XEXP (op0, 1)) == CONST_INT
11135 && mode_width <= HOST_BITS_PER_WIDE_INT
11136 && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
11137 && ((c1 = INTVAL (XEXP (op0, 1))) & ~mask) == 0
11138 && (c1 & ~GET_MODE_MASK (tmode)) == 0
11139 && c1 != mask
11140 && c1 != GET_MODE_MASK (tmode))
11141 {
11142 op0 = gen_binary (AND, tmode,
11143 SUBREG_REG (XEXP (op0, 0)),
11144 gen_int_mode (c1, tmode));
11145 op0 = gen_lowpart_for_combine (mode, op0);
11146 continue;
11147 }
11148 }
11149
11150 /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
11151 (eq (and (lshiftrt X) 1) 0). */
11152 if (const_op == 0 && equality_comparison_p
11153 && XEXP (op0, 1) == const1_rtx
11154 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
11155 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == NOT)
11156 {
11157 op0 = simplify_and_const_int
11158 (op0, mode,
11159 gen_rtx_LSHIFTRT (mode, XEXP (XEXP (XEXP (op0, 0), 0), 0),
11160 XEXP (XEXP (op0, 0), 1)),
11161 (HOST_WIDE_INT) 1);
11162 code = (code == NE ? EQ : NE);
11163 continue;
11164 }
11165 break;
11166
11167 case ASHIFT:
11168 /* If we have (compare (ashift FOO N) (const_int C)) and
11169 the high order N bits of FOO (N+1 if an inequality comparison)
11170 are known to be zero, we can do this by comparing FOO with C
11171 shifted right N bits so long as the low-order N bits of C are
11172 zero. */
11173 if (GET_CODE (XEXP (op0, 1)) == CONST_INT
11174 && INTVAL (XEXP (op0, 1)) >= 0
11175 && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
11176 < HOST_BITS_PER_WIDE_INT)
11177 && ((const_op
11178 & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0)
11179 && mode_width <= HOST_BITS_PER_WIDE_INT
11180 && (nonzero_bits (XEXP (op0, 0), mode)
11181 & ~(mask >> (INTVAL (XEXP (op0, 1))
11182 + ! equality_comparison_p))) == 0)
11183 {
11184 /* We must perform a logical shift, not an arithmetic one,
11185 as we want the top N bits of C to be zero. */
11186 unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
11187
11188 temp >>= INTVAL (XEXP (op0, 1));
11189 op1 = gen_int_mode (temp, mode);
11190 op0 = XEXP (op0, 0);
11191 continue;
11192 }
11193
11194 /* If we are doing a sign bit comparison, it means we are testing
11195 a particular bit. Convert it to the appropriate AND. */
11196 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
11197 && mode_width <= HOST_BITS_PER_WIDE_INT)
11198 {
11199 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11200 ((HOST_WIDE_INT) 1
11201 << (mode_width - 1
11202 - INTVAL (XEXP (op0, 1)))));
11203 code = (code == LT ? NE : EQ);
11204 continue;
11205 }
11206
11207 /* If this an equality comparison with zero and we are shifting
11208 the low bit to the sign bit, we can convert this to an AND of the
11209 low-order bit. */
11210 if (const_op == 0 && equality_comparison_p
11211 && GET_CODE (XEXP (op0, 1)) == CONST_INT
11212 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1))
11213 == mode_width - 1)
11214 {
11215 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11216 (HOST_WIDE_INT) 1);
11217 continue;
11218 }
11219 break;
11220
11221 case ASHIFTRT:
11222 /* If this is an equality comparison with zero, we can do this
11223 as a logical shift, which might be much simpler. */
11224 if (equality_comparison_p && const_op == 0
11225 && GET_CODE (XEXP (op0, 1)) == CONST_INT)
11226 {
11227 op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
11228 XEXP (op0, 0),
11229 INTVAL (XEXP (op0, 1)));
11230 continue;
11231 }
11232
11233 /* If OP0 is a sign extension and CODE is not an unsigned comparison,
11234 do the comparison in a narrower mode. */
11235 if (! unsigned_comparison_p
11236 && GET_CODE (XEXP (op0, 1)) == CONST_INT
11237 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11238 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
11239 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
11240 MODE_INT, 1)) != BLKmode
11241 && (((unsigned HOST_WIDE_INT) const_op
11242 + (GET_MODE_MASK (tmode) >> 1) + 1)
11243 <= GET_MODE_MASK (tmode)))
11244 {
11245 op0 = gen_lowpart_for_combine (tmode, XEXP (XEXP (op0, 0), 0));
11246 continue;
11247 }
11248
11249 /* Likewise if OP0 is a PLUS of a sign extension with a
11250 constant, which is usually represented with the PLUS
11251 between the shifts. */
11252 if (! unsigned_comparison_p
11253 && GET_CODE (XEXP (op0, 1)) == CONST_INT
11254 && GET_CODE (XEXP (op0, 0)) == PLUS
11255 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
11256 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
11257 && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
11258 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
11259 MODE_INT, 1)) != BLKmode
11260 && (((unsigned HOST_WIDE_INT) const_op
11261 + (GET_MODE_MASK (tmode) >> 1) + 1)
11262 <= GET_MODE_MASK (tmode)))
11263 {
11264 rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
11265 rtx add_const = XEXP (XEXP (op0, 0), 1);
11266 rtx new_const = gen_binary (ASHIFTRT, GET_MODE (op0), add_const,
11267 XEXP (op0, 1));
11268
11269 op0 = gen_binary (PLUS, tmode,
11270 gen_lowpart_for_combine (tmode, inner),
11271 new_const);
11272 continue;
11273 }
11274
11275 /* ... fall through ... */
11276 case LSHIFTRT:
11277 /* If we have (compare (xshiftrt FOO N) (const_int C)) and
11278 the low order N bits of FOO are known to be zero, we can do this
11279 by comparing FOO with C shifted left N bits so long as no
11280 overflow occurs. */
11281 if (GET_CODE (XEXP (op0, 1)) == CONST_INT
11282 && INTVAL (XEXP (op0, 1)) >= 0
11283 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
11284 && mode_width <= HOST_BITS_PER_WIDE_INT
11285 && (nonzero_bits (XEXP (op0, 0), mode)
11286 & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0
11287 && (((unsigned HOST_WIDE_INT) const_op
11288 + (GET_CODE (op0) != LSHIFTRT
11289 ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
11290 + 1)
11291 : 0))
11292 <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
11293 {
11294 /* If the shift was logical, then we must make the condition
11295 unsigned. */
11296 if (GET_CODE (op0) == LSHIFTRT)
11297 code = unsigned_condition (code);
11298
11299 const_op <<= INTVAL (XEXP (op0, 1));
11300 op1 = GEN_INT (const_op);
11301 op0 = XEXP (op0, 0);
11302 continue;
11303 }
11304
11305 /* If we are using this shift to extract just the sign bit, we
11306 can replace this with an LT or GE comparison. */
11307 if (const_op == 0
11308 && (equality_comparison_p || sign_bit_comparison_p)
11309 && GET_CODE (XEXP (op0, 1)) == CONST_INT
11310 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1))
11311 == mode_width - 1)
11312 {
11313 op0 = XEXP (op0, 0);
11314 code = (code == NE || code == GT ? LT : GE);
11315 continue;
11316 }
11317 break;
11318
11319 default:
11320 break;
11321 }
11322
11323 break;
11324 }
11325
11326 /* Now make any compound operations involved in this comparison. Then,
11327 check for an outmost SUBREG on OP0 that is not doing anything or is
11328 paradoxical. The latter transformation must only be performed when
11329 it is known that the "extra" bits will be the same in op0 and op1 or
11330 that they don't matter. There are three cases to consider:
11331
11332 1. SUBREG_REG (op0) is a register. In this case the bits are don't
11333 care bits and we can assume they have any convenient value. So
11334 making the transformation is safe.
11335
11336 2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not defined.
11337 In this case the upper bits of op0 are undefined. We should not make
11338 the simplification in that case as we do not know the contents of
11339 those bits.
11340
11341 3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is defined and not
11342 NIL. In that case we know those bits are zeros or ones. We must
11343 also be sure that they are the same as the upper bits of op1.
11344
11345 We can never remove a SUBREG for a non-equality comparison because
11346 the sign bit is in a different place in the underlying object. */
11347
11348 op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
11349 op1 = make_compound_operation (op1, SET);
11350
11351 if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
11352 /* Case 3 above, to sometimes allow (subreg (mem x)), isn't
11353 implemented. */
11354 && GET_CODE (SUBREG_REG (op0)) == REG
11355 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
11356 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (op0))) == MODE_INT
11357 && (code == NE || code == EQ))
11358 {
11359 if (GET_MODE_SIZE (GET_MODE (op0))
11360 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0))))
11361 {
11362 op0 = SUBREG_REG (op0);
11363 op1 = gen_lowpart_for_combine (GET_MODE (op0), op1);
11364 }
11365 else if ((GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
11366 <= HOST_BITS_PER_WIDE_INT)
11367 && (nonzero_bits (SUBREG_REG (op0),
11368 GET_MODE (SUBREG_REG (op0)))
11369 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
11370 {
11371 tem = gen_lowpart_for_combine (GET_MODE (SUBREG_REG (op0)), op1);
11372
11373 if ((nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
11374 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
11375 op0 = SUBREG_REG (op0), op1 = tem;
11376 }
11377 }
11378
11379 /* We now do the opposite procedure: Some machines don't have compare
11380 insns in all modes. If OP0's mode is an integer mode smaller than a
11381 word and we can't do a compare in that mode, see if there is a larger
11382 mode for which we can do the compare. There are a number of cases in
11383 which we can use the wider mode. */
11384
11385 mode = GET_MODE (op0);
11386 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
11387 && GET_MODE_SIZE (mode) < UNITS_PER_WORD
11388 && ! have_insn_for (COMPARE, mode))
11389 for (tmode = GET_MODE_WIDER_MODE (mode);
11390 (tmode != VOIDmode
11391 && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT);
11392 tmode = GET_MODE_WIDER_MODE (tmode))
11393 if (have_insn_for (COMPARE, tmode))
11394 {
11395 int zero_extended;
11396
11397 /* If the only nonzero bits in OP0 and OP1 are those in the
11398 narrower mode and this is an equality or unsigned comparison,
11399 we can use the wider mode. Similarly for sign-extended
11400 values, in which case it is true for all comparisons. */
11401 zero_extended = ((code == EQ || code == NE
11402 || code == GEU || code == GTU
11403 || code == LEU || code == LTU)
11404 && (nonzero_bits (op0, tmode)
11405 & ~GET_MODE_MASK (mode)) == 0
11406 && ((GET_CODE (op1) == CONST_INT
11407 || (nonzero_bits (op1, tmode)
11408 & ~GET_MODE_MASK (mode)) == 0)));
11409
11410 if (zero_extended
11411 || ((num_sign_bit_copies (op0, tmode)
11412 > (unsigned int) (GET_MODE_BITSIZE (tmode)
11413 - GET_MODE_BITSIZE (mode)))
11414 && (num_sign_bit_copies (op1, tmode)
11415 > (unsigned int) (GET_MODE_BITSIZE (tmode)
11416 - GET_MODE_BITSIZE (mode)))))
11417 {
11418 /* If OP0 is an AND and we don't have an AND in MODE either,
11419 make a new AND in the proper mode. */
11420 if (GET_CODE (op0) == AND
11421 && !have_insn_for (AND, mode))
11422 op0 = gen_binary (AND, tmode,
11423 gen_lowpart_for_combine (tmode,
11424 XEXP (op0, 0)),
11425 gen_lowpart_for_combine (tmode,
11426 XEXP (op0, 1)));
11427
11428 op0 = gen_lowpart_for_combine (tmode, op0);
11429 if (zero_extended && GET_CODE (op1) == CONST_INT)
11430 op1 = GEN_INT (INTVAL (op1) & GET_MODE_MASK (mode));
11431 op1 = gen_lowpart_for_combine (tmode, op1);
11432 break;
11433 }
11434
11435 /* If this is a test for negative, we can make an explicit
11436 test of the sign bit. */
11437
11438 if (op1 == const0_rtx && (code == LT || code == GE)
11439 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11440 {
11441 op0 = gen_binary (AND, tmode,
11442 gen_lowpart_for_combine (tmode, op0),
11443 GEN_INT ((HOST_WIDE_INT) 1
11444 << (GET_MODE_BITSIZE (mode) - 1)));
11445 code = (code == LT) ? NE : EQ;
11446 break;
11447 }
11448 }
11449
11450 #ifdef CANONICALIZE_COMPARISON
11451 /* If this machine only supports a subset of valid comparisons, see if we
11452 can convert an unsupported one into a supported one. */
11453 CANONICALIZE_COMPARISON (code, op0, op1);
11454 #endif
11455
11456 *pop0 = op0;
11457 *pop1 = op1;
11458
11459 return code;
11460 }
11461 \f
11462 /* Like jump.c' reversed_comparison_code, but use combine infrastructure for
11463 searching backward. */
11464 static enum rtx_code
11465 combine_reversed_comparison_code (exp)
11466 rtx exp;
11467 {
11468 enum rtx_code code1 = reversed_comparison_code (exp, NULL);
11469 rtx x;
11470
11471 if (code1 != UNKNOWN
11472 || GET_MODE_CLASS (GET_MODE (XEXP (exp, 0))) != MODE_CC)
11473 return code1;
11474 /* Otherwise try and find where the condition codes were last set and
11475 use that. */
11476 x = get_last_value (XEXP (exp, 0));
11477 if (!x || GET_CODE (x) != COMPARE)
11478 return UNKNOWN;
11479 return reversed_comparison_code_parts (GET_CODE (exp),
11480 XEXP (x, 0), XEXP (x, 1), NULL);
11481 }
11482
11483 /* Return comparison with reversed code of EXP and operands OP0 and OP1.
11484 Return NULL_RTX in case we fail to do the reversal. */
11485 static rtx
11486 reversed_comparison (exp, mode, op0, op1)
11487 rtx exp, op0, op1;
11488 enum machine_mode mode;
11489 {
11490 enum rtx_code reversed_code = combine_reversed_comparison_code (exp);
11491 if (reversed_code == UNKNOWN)
11492 return NULL_RTX;
11493 else
11494 return gen_binary (reversed_code, mode, op0, op1);
11495 }
11496 \f
11497 /* Utility function for following routine. Called when X is part of a value
11498 being stored into reg_last_set_value. Sets reg_last_set_table_tick
11499 for each register mentioned. Similar to mention_regs in cse.c */
11500
11501 static void
11502 update_table_tick (x)
11503 rtx x;
11504 {
11505 enum rtx_code code = GET_CODE (x);
11506 const char *fmt = GET_RTX_FORMAT (code);
11507 int i;
11508
11509 if (code == REG)
11510 {
11511 unsigned int regno = REGNO (x);
11512 unsigned int endregno
11513 = regno + (regno < FIRST_PSEUDO_REGISTER
11514 ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
11515 unsigned int r;
11516
11517 for (r = regno; r < endregno; r++)
11518 reg_last_set_table_tick[r] = label_tick;
11519
11520 return;
11521 }
11522
11523 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11524 /* Note that we can't have an "E" in values stored; see
11525 get_last_value_validate. */
11526 if (fmt[i] == 'e')
11527 {
11528 /* Check for identical subexpressions. If x contains
11529 identical subexpression we only have to traverse one of
11530 them. */
11531 if (i == 0
11532 && (GET_RTX_CLASS (code) == '2'
11533 || GET_RTX_CLASS (code) == 'c'))
11534 {
11535 /* Note that at this point x1 has already been
11536 processed. */
11537 rtx x0 = XEXP (x, 0);
11538 rtx x1 = XEXP (x, 1);
11539
11540 /* If x0 and x1 are identical then there is no need to
11541 process x0. */
11542 if (x0 == x1)
11543 break;
11544
11545 /* If x0 is identical to a subexpression of x1 then while
11546 processing x1, x0 has already been processed. Thus we
11547 are done with x. */
11548 if ((GET_RTX_CLASS (GET_CODE (x1)) == '2'
11549 || GET_RTX_CLASS (GET_CODE (x1)) == 'c')
11550 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
11551 break;
11552
11553 /* If x1 is identical to a subexpression of x0 then we
11554 still have to process the rest of x0. */
11555 if ((GET_RTX_CLASS (GET_CODE (x0)) == '2'
11556 || GET_RTX_CLASS (GET_CODE (x0)) == 'c')
11557 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
11558 {
11559 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
11560 break;
11561 }
11562 }
11563
11564 update_table_tick (XEXP (x, i));
11565 }
11566 }
11567
11568 /* Record that REG is set to VALUE in insn INSN. If VALUE is zero, we
11569 are saying that the register is clobbered and we no longer know its
11570 value. If INSN is zero, don't update reg_last_set; this is only permitted
11571 with VALUE also zero and is used to invalidate the register. */
11572
11573 static void
11574 record_value_for_reg (reg, insn, value)
11575 rtx reg;
11576 rtx insn;
11577 rtx value;
11578 {
11579 unsigned int regno = REGNO (reg);
11580 unsigned int endregno
11581 = regno + (regno < FIRST_PSEUDO_REGISTER
11582 ? HARD_REGNO_NREGS (regno, GET_MODE (reg)) : 1);
11583 unsigned int i;
11584
11585 /* If VALUE contains REG and we have a previous value for REG, substitute
11586 the previous value. */
11587 if (value && insn && reg_overlap_mentioned_p (reg, value))
11588 {
11589 rtx tem;
11590
11591 /* Set things up so get_last_value is allowed to see anything set up to
11592 our insn. */
11593 subst_low_cuid = INSN_CUID (insn);
11594 tem = get_last_value (reg);
11595
11596 /* If TEM is simply a binary operation with two CLOBBERs as operands,
11597 it isn't going to be useful and will take a lot of time to process,
11598 so just use the CLOBBER. */
11599
11600 if (tem)
11601 {
11602 if ((GET_RTX_CLASS (GET_CODE (tem)) == '2'
11603 || GET_RTX_CLASS (GET_CODE (tem)) == 'c')
11604 && GET_CODE (XEXP (tem, 0)) == CLOBBER
11605 && GET_CODE (XEXP (tem, 1)) == CLOBBER)
11606 tem = XEXP (tem, 0);
11607
11608 value = replace_rtx (copy_rtx (value), reg, tem);
11609 }
11610 }
11611
11612 /* For each register modified, show we don't know its value, that
11613 we don't know about its bitwise content, that its value has been
11614 updated, and that we don't know the location of the death of the
11615 register. */
11616 for (i = regno; i < endregno; i++)
11617 {
11618 if (insn)
11619 reg_last_set[i] = insn;
11620
11621 reg_last_set_value[i] = 0;
11622 reg_last_set_mode[i] = 0;
11623 reg_last_set_nonzero_bits[i] = 0;
11624 reg_last_set_sign_bit_copies[i] = 0;
11625 reg_last_death[i] = 0;
11626 }
11627
11628 /* Mark registers that are being referenced in this value. */
11629 if (value)
11630 update_table_tick (value);
11631
11632 /* Now update the status of each register being set.
11633 If someone is using this register in this block, set this register
11634 to invalid since we will get confused between the two lives in this
11635 basic block. This makes using this register always invalid. In cse, we
11636 scan the table to invalidate all entries using this register, but this
11637 is too much work for us. */
11638
11639 for (i = regno; i < endregno; i++)
11640 {
11641 reg_last_set_label[i] = label_tick;
11642 if (value && reg_last_set_table_tick[i] == label_tick)
11643 reg_last_set_invalid[i] = 1;
11644 else
11645 reg_last_set_invalid[i] = 0;
11646 }
11647
11648 /* The value being assigned might refer to X (like in "x++;"). In that
11649 case, we must replace it with (clobber (const_int 0)) to prevent
11650 infinite loops. */
11651 if (value && ! get_last_value_validate (&value, insn,
11652 reg_last_set_label[regno], 0))
11653 {
11654 value = copy_rtx (value);
11655 if (! get_last_value_validate (&value, insn,
11656 reg_last_set_label[regno], 1))
11657 value = 0;
11658 }
11659
11660 /* For the main register being modified, update the value, the mode, the
11661 nonzero bits, and the number of sign bit copies. */
11662
11663 reg_last_set_value[regno] = value;
11664
11665 if (value)
11666 {
11667 enum machine_mode mode = GET_MODE (reg);
11668 subst_low_cuid = INSN_CUID (insn);
11669 reg_last_set_mode[regno] = mode;
11670 if (GET_MODE_CLASS (mode) == MODE_INT
11671 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11672 mode = nonzero_bits_mode;
11673 reg_last_set_nonzero_bits[regno] = nonzero_bits (value, mode);
11674 reg_last_set_sign_bit_copies[regno]
11675 = num_sign_bit_copies (value, GET_MODE (reg));
11676 }
11677 }
11678
11679 /* Called via note_stores from record_dead_and_set_regs to handle one
11680 SET or CLOBBER in an insn. DATA is the instruction in which the
11681 set is occurring. */
11682
11683 static void
11684 record_dead_and_set_regs_1 (dest, setter, data)
11685 rtx dest, setter;
11686 void *data;
11687 {
11688 rtx record_dead_insn = (rtx) data;
11689
11690 if (GET_CODE (dest) == SUBREG)
11691 dest = SUBREG_REG (dest);
11692
11693 if (GET_CODE (dest) == REG)
11694 {
11695 /* If we are setting the whole register, we know its value. Otherwise
11696 show that we don't know the value. We can handle SUBREG in
11697 some cases. */
11698 if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
11699 record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
11700 else if (GET_CODE (setter) == SET
11701 && GET_CODE (SET_DEST (setter)) == SUBREG
11702 && SUBREG_REG (SET_DEST (setter)) == dest
11703 && GET_MODE_BITSIZE (GET_MODE (dest)) <= BITS_PER_WORD
11704 && subreg_lowpart_p (SET_DEST (setter)))
11705 record_value_for_reg (dest, record_dead_insn,
11706 gen_lowpart_for_combine (GET_MODE (dest),
11707 SET_SRC (setter)));
11708 else
11709 record_value_for_reg (dest, record_dead_insn, NULL_RTX);
11710 }
11711 else if (GET_CODE (dest) == MEM
11712 /* Ignore pushes, they clobber nothing. */
11713 && ! push_operand (dest, GET_MODE (dest)))
11714 mem_last_set = INSN_CUID (record_dead_insn);
11715 }
11716
11717 /* Update the records of when each REG was most recently set or killed
11718 for the things done by INSN. This is the last thing done in processing
11719 INSN in the combiner loop.
11720
11721 We update reg_last_set, reg_last_set_value, reg_last_set_mode,
11722 reg_last_set_nonzero_bits, reg_last_set_sign_bit_copies, reg_last_death,
11723 and also the similar information mem_last_set (which insn most recently
11724 modified memory) and last_call_cuid (which insn was the most recent
11725 subroutine call). */
11726
11727 static void
11728 record_dead_and_set_regs (insn)
11729 rtx insn;
11730 {
11731 rtx link;
11732 unsigned int i;
11733
11734 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
11735 {
11736 if (REG_NOTE_KIND (link) == REG_DEAD
11737 && GET_CODE (XEXP (link, 0)) == REG)
11738 {
11739 unsigned int regno = REGNO (XEXP (link, 0));
11740 unsigned int endregno
11741 = regno + (regno < FIRST_PSEUDO_REGISTER
11742 ? HARD_REGNO_NREGS (regno, GET_MODE (XEXP (link, 0)))
11743 : 1);
11744
11745 for (i = regno; i < endregno; i++)
11746 reg_last_death[i] = insn;
11747 }
11748 else if (REG_NOTE_KIND (link) == REG_INC)
11749 record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
11750 }
11751
11752 if (GET_CODE (insn) == CALL_INSN)
11753 {
11754 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
11755 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
11756 {
11757 reg_last_set_value[i] = 0;
11758 reg_last_set_mode[i] = 0;
11759 reg_last_set_nonzero_bits[i] = 0;
11760 reg_last_set_sign_bit_copies[i] = 0;
11761 reg_last_death[i] = 0;
11762 }
11763
11764 last_call_cuid = mem_last_set = INSN_CUID (insn);
11765
11766 /* Don't bother recording what this insn does. It might set the
11767 return value register, but we can't combine into a call
11768 pattern anyway, so there's no point trying (and it may cause
11769 a crash, if e.g. we wind up asking for last_set_value of a
11770 SUBREG of the return value register). */
11771 return;
11772 }
11773
11774 note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
11775 }
11776
11777 /* If a SUBREG has the promoted bit set, it is in fact a property of the
11778 register present in the SUBREG, so for each such SUBREG go back and
11779 adjust nonzero and sign bit information of the registers that are
11780 known to have some zero/sign bits set.
11781
11782 This is needed because when combine blows the SUBREGs away, the
11783 information on zero/sign bits is lost and further combines can be
11784 missed because of that. */
11785
11786 static void
11787 record_promoted_value (insn, subreg)
11788 rtx insn;
11789 rtx subreg;
11790 {
11791 rtx links, set;
11792 unsigned int regno = REGNO (SUBREG_REG (subreg));
11793 enum machine_mode mode = GET_MODE (subreg);
11794
11795 if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT)
11796 return;
11797
11798 for (links = LOG_LINKS (insn); links;)
11799 {
11800 insn = XEXP (links, 0);
11801 set = single_set (insn);
11802
11803 if (! set || GET_CODE (SET_DEST (set)) != REG
11804 || REGNO (SET_DEST (set)) != regno
11805 || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
11806 {
11807 links = XEXP (links, 1);
11808 continue;
11809 }
11810
11811 if (reg_last_set[regno] == insn)
11812 {
11813 if (SUBREG_PROMOTED_UNSIGNED_P (subreg) > 0)
11814 reg_last_set_nonzero_bits[regno] &= GET_MODE_MASK (mode);
11815 }
11816
11817 if (GET_CODE (SET_SRC (set)) == REG)
11818 {
11819 regno = REGNO (SET_SRC (set));
11820 links = LOG_LINKS (insn);
11821 }
11822 else
11823 break;
11824 }
11825 }
11826
11827 /* Scan X for promoted SUBREGs. For each one found,
11828 note what it implies to the registers used in it. */
11829
11830 static void
11831 check_promoted_subreg (insn, x)
11832 rtx insn;
11833 rtx x;
11834 {
11835 if (GET_CODE (x) == SUBREG && SUBREG_PROMOTED_VAR_P (x)
11836 && GET_CODE (SUBREG_REG (x)) == REG)
11837 record_promoted_value (insn, x);
11838 else
11839 {
11840 const char *format = GET_RTX_FORMAT (GET_CODE (x));
11841 int i, j;
11842
11843 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
11844 switch (format[i])
11845 {
11846 case 'e':
11847 check_promoted_subreg (insn, XEXP (x, i));
11848 break;
11849 case 'V':
11850 case 'E':
11851 if (XVEC (x, i) != 0)
11852 for (j = 0; j < XVECLEN (x, i); j++)
11853 check_promoted_subreg (insn, XVECEXP (x, i, j));
11854 break;
11855 }
11856 }
11857 }
11858 \f
11859 /* Utility routine for the following function. Verify that all the registers
11860 mentioned in *LOC are valid when *LOC was part of a value set when
11861 label_tick == TICK. Return 0 if some are not.
11862
11863 If REPLACE is nonzero, replace the invalid reference with
11864 (clobber (const_int 0)) and return 1. This replacement is useful because
11865 we often can get useful information about the form of a value (e.g., if
11866 it was produced by a shift that always produces -1 or 0) even though
11867 we don't know exactly what registers it was produced from. */
11868
11869 static int
11870 get_last_value_validate (loc, insn, tick, replace)
11871 rtx *loc;
11872 rtx insn;
11873 int tick;
11874 int replace;
11875 {
11876 rtx x = *loc;
11877 const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
11878 int len = GET_RTX_LENGTH (GET_CODE (x));
11879 int i;
11880
11881 if (GET_CODE (x) == REG)
11882 {
11883 unsigned int regno = REGNO (x);
11884 unsigned int endregno
11885 = regno + (regno < FIRST_PSEUDO_REGISTER
11886 ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
11887 unsigned int j;
11888
11889 for (j = regno; j < endregno; j++)
11890 if (reg_last_set_invalid[j]
11891 /* If this is a pseudo-register that was only set once and not
11892 live at the beginning of the function, it is always valid. */
11893 || (! (regno >= FIRST_PSEUDO_REGISTER
11894 && REG_N_SETS (regno) == 1
11895 && (! REGNO_REG_SET_P
11896 (ENTRY_BLOCK_PTR->next_bb->global_live_at_start, regno)))
11897 && reg_last_set_label[j] > tick))
11898 {
11899 if (replace)
11900 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
11901 return replace;
11902 }
11903
11904 return 1;
11905 }
11906 /* If this is a memory reference, make sure that there were
11907 no stores after it that might have clobbered the value. We don't
11908 have alias info, so we assume any store invalidates it. */
11909 else if (GET_CODE (x) == MEM && ! RTX_UNCHANGING_P (x)
11910 && INSN_CUID (insn) <= mem_last_set)
11911 {
11912 if (replace)
11913 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
11914 return replace;
11915 }
11916
11917 for (i = 0; i < len; i++)
11918 {
11919 if (fmt[i] == 'e')
11920 {
11921 /* Check for identical subexpressions. If x contains
11922 identical subexpression we only have to traverse one of
11923 them. */
11924 if (i == 1
11925 && (GET_RTX_CLASS (GET_CODE (x)) == '2'
11926 || GET_RTX_CLASS (GET_CODE (x)) == 'c'))
11927 {
11928 /* Note that at this point x0 has already been checked
11929 and found valid. */
11930 rtx x0 = XEXP (x, 0);
11931 rtx x1 = XEXP (x, 1);
11932
11933 /* If x0 and x1 are identical then x is also valid. */
11934 if (x0 == x1)
11935 return 1;
11936
11937 /* If x1 is identical to a subexpression of x0 then
11938 while checking x0, x1 has already been checked. Thus
11939 it is valid and so as x. */
11940 if ((GET_RTX_CLASS (GET_CODE (x0)) == '2'
11941 || GET_RTX_CLASS (GET_CODE (x0)) == 'c')
11942 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
11943 return 1;
11944
11945 /* If x0 is identical to a subexpression of x1 then x is
11946 valid iff the rest of x1 is valid. */
11947 if ((GET_RTX_CLASS (GET_CODE (x1)) == '2'
11948 || GET_RTX_CLASS (GET_CODE (x1)) == 'c')
11949 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
11950 return
11951 get_last_value_validate (&XEXP (x1,
11952 x0 == XEXP (x1, 0) ? 1 : 0),
11953 insn, tick, replace);
11954 }
11955
11956 if (get_last_value_validate (&XEXP (x, i), insn, tick,
11957 replace) == 0)
11958 return 0;
11959 }
11960 /* Don't bother with these. They shouldn't occur anyway. */
11961 else if (fmt[i] == 'E')
11962 return 0;
11963 }
11964
11965 /* If we haven't found a reason for it to be invalid, it is valid. */
11966 return 1;
11967 }
11968
11969 /* Get the last value assigned to X, if known. Some registers
11970 in the value may be replaced with (clobber (const_int 0)) if their value
11971 is known longer known reliably. */
11972
11973 static rtx
11974 get_last_value (x)
11975 rtx x;
11976 {
11977 unsigned int regno;
11978 rtx value;
11979
11980 /* If this is a non-paradoxical SUBREG, get the value of its operand and
11981 then convert it to the desired mode. If this is a paradoxical SUBREG,
11982 we cannot predict what values the "extra" bits might have. */
11983 if (GET_CODE (x) == SUBREG
11984 && subreg_lowpart_p (x)
11985 && (GET_MODE_SIZE (GET_MODE (x))
11986 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
11987 && (value = get_last_value (SUBREG_REG (x))) != 0)
11988 return gen_lowpart_for_combine (GET_MODE (x), value);
11989
11990 if (GET_CODE (x) != REG)
11991 return 0;
11992
11993 regno = REGNO (x);
11994 value = reg_last_set_value[regno];
11995
11996 /* If we don't have a value, or if it isn't for this basic block and
11997 it's either a hard register, set more than once, or it's a live
11998 at the beginning of the function, return 0.
11999
12000 Because if it's not live at the beginning of the function then the reg
12001 is always set before being used (is never used without being set).
12002 And, if it's set only once, and it's always set before use, then all
12003 uses must have the same last value, even if it's not from this basic
12004 block. */
12005
12006 if (value == 0
12007 || (reg_last_set_label[regno] != label_tick
12008 && (regno < FIRST_PSEUDO_REGISTER
12009 || REG_N_SETS (regno) != 1
12010 || (REGNO_REG_SET_P
12011 (ENTRY_BLOCK_PTR->next_bb->global_live_at_start, regno)))))
12012 return 0;
12013
12014 /* If the value was set in a later insn than the ones we are processing,
12015 we can't use it even if the register was only set once. */
12016 if (INSN_CUID (reg_last_set[regno]) >= subst_low_cuid)
12017 return 0;
12018
12019 /* If the value has all its registers valid, return it. */
12020 if (get_last_value_validate (&value, reg_last_set[regno],
12021 reg_last_set_label[regno], 0))
12022 return value;
12023
12024 /* Otherwise, make a copy and replace any invalid register with
12025 (clobber (const_int 0)). If that fails for some reason, return 0. */
12026
12027 value = copy_rtx (value);
12028 if (get_last_value_validate (&value, reg_last_set[regno],
12029 reg_last_set_label[regno], 1))
12030 return value;
12031
12032 return 0;
12033 }
12034 \f
12035 /* Return nonzero if expression X refers to a REG or to memory
12036 that is set in an instruction more recent than FROM_CUID. */
12037
12038 static int
12039 use_crosses_set_p (x, from_cuid)
12040 rtx x;
12041 int from_cuid;
12042 {
12043 const char *fmt;
12044 int i;
12045 enum rtx_code code = GET_CODE (x);
12046
12047 if (code == REG)
12048 {
12049 unsigned int regno = REGNO (x);
12050 unsigned endreg = regno + (regno < FIRST_PSEUDO_REGISTER
12051 ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
12052
12053 #ifdef PUSH_ROUNDING
12054 /* Don't allow uses of the stack pointer to be moved,
12055 because we don't know whether the move crosses a push insn. */
12056 if (regno == STACK_POINTER_REGNUM && PUSH_ARGS)
12057 return 1;
12058 #endif
12059 for (; regno < endreg; regno++)
12060 if (reg_last_set[regno]
12061 && INSN_CUID (reg_last_set[regno]) > from_cuid)
12062 return 1;
12063 return 0;
12064 }
12065
12066 if (code == MEM && mem_last_set > from_cuid)
12067 return 1;
12068
12069 fmt = GET_RTX_FORMAT (code);
12070
12071 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12072 {
12073 if (fmt[i] == 'E')
12074 {
12075 int j;
12076 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
12077 if (use_crosses_set_p (XVECEXP (x, i, j), from_cuid))
12078 return 1;
12079 }
12080 else if (fmt[i] == 'e'
12081 && use_crosses_set_p (XEXP (x, i), from_cuid))
12082 return 1;
12083 }
12084 return 0;
12085 }
12086 \f
12087 /* Define three variables used for communication between the following
12088 routines. */
12089
12090 static unsigned int reg_dead_regno, reg_dead_endregno;
12091 static int reg_dead_flag;
12092
12093 /* Function called via note_stores from reg_dead_at_p.
12094
12095 If DEST is within [reg_dead_regno, reg_dead_endregno), set
12096 reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET. */
12097
12098 static void
12099 reg_dead_at_p_1 (dest, x, data)
12100 rtx dest;
12101 rtx x;
12102 void *data ATTRIBUTE_UNUSED;
12103 {
12104 unsigned int regno, endregno;
12105
12106 if (GET_CODE (dest) != REG)
12107 return;
12108
12109 regno = REGNO (dest);
12110 endregno = regno + (regno < FIRST_PSEUDO_REGISTER
12111 ? HARD_REGNO_NREGS (regno, GET_MODE (dest)) : 1);
12112
12113 if (reg_dead_endregno > regno && reg_dead_regno < endregno)
12114 reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
12115 }
12116
12117 /* Return nonzero if REG is known to be dead at INSN.
12118
12119 We scan backwards from INSN. If we hit a REG_DEAD note or a CLOBBER
12120 referencing REG, it is dead. If we hit a SET referencing REG, it is
12121 live. Otherwise, see if it is live or dead at the start of the basic
12122 block we are in. Hard regs marked as being live in NEWPAT_USED_REGS
12123 must be assumed to be always live. */
12124
12125 static int
12126 reg_dead_at_p (reg, insn)
12127 rtx reg;
12128 rtx insn;
12129 {
12130 basic_block block;
12131 unsigned int i;
12132
12133 /* Set variables for reg_dead_at_p_1. */
12134 reg_dead_regno = REGNO (reg);
12135 reg_dead_endregno = reg_dead_regno + (reg_dead_regno < FIRST_PSEUDO_REGISTER
12136 ? HARD_REGNO_NREGS (reg_dead_regno,
12137 GET_MODE (reg))
12138 : 1);
12139
12140 reg_dead_flag = 0;
12141
12142 /* Check that reg isn't mentioned in NEWPAT_USED_REGS. */
12143 if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
12144 {
12145 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
12146 if (TEST_HARD_REG_BIT (newpat_used_regs, i))
12147 return 0;
12148 }
12149
12150 /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, label, or
12151 beginning of function. */
12152 for (; insn && GET_CODE (insn) != CODE_LABEL && GET_CODE (insn) != BARRIER;
12153 insn = prev_nonnote_insn (insn))
12154 {
12155 note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
12156 if (reg_dead_flag)
12157 return reg_dead_flag == 1 ? 1 : 0;
12158
12159 if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
12160 return 1;
12161 }
12162
12163 /* Get the basic block that we were in. */
12164 if (insn == 0)
12165 block = ENTRY_BLOCK_PTR->next_bb;
12166 else
12167 {
12168 FOR_EACH_BB (block)
12169 if (insn == block->head)
12170 break;
12171
12172 if (block == EXIT_BLOCK_PTR)
12173 return 0;
12174 }
12175
12176 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
12177 if (REGNO_REG_SET_P (block->global_live_at_start, i))
12178 return 0;
12179
12180 return 1;
12181 }
12182 \f
12183 /* Note hard registers in X that are used. This code is similar to
12184 that in flow.c, but much simpler since we don't care about pseudos. */
12185
12186 static void
12187 mark_used_regs_combine (x)
12188 rtx x;
12189 {
12190 RTX_CODE code = GET_CODE (x);
12191 unsigned int regno;
12192 int i;
12193
12194 switch (code)
12195 {
12196 case LABEL_REF:
12197 case SYMBOL_REF:
12198 case CONST_INT:
12199 case CONST:
12200 case CONST_DOUBLE:
12201 case CONST_VECTOR:
12202 case PC:
12203 case ADDR_VEC:
12204 case ADDR_DIFF_VEC:
12205 case ASM_INPUT:
12206 #ifdef HAVE_cc0
12207 /* CC0 must die in the insn after it is set, so we don't need to take
12208 special note of it here. */
12209 case CC0:
12210 #endif
12211 return;
12212
12213 case CLOBBER:
12214 /* If we are clobbering a MEM, mark any hard registers inside the
12215 address as used. */
12216 if (GET_CODE (XEXP (x, 0)) == MEM)
12217 mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
12218 return;
12219
12220 case REG:
12221 regno = REGNO (x);
12222 /* A hard reg in a wide mode may really be multiple registers.
12223 If so, mark all of them just like the first. */
12224 if (regno < FIRST_PSEUDO_REGISTER)
12225 {
12226 unsigned int endregno, r;
12227
12228 /* None of this applies to the stack, frame or arg pointers. */
12229 if (regno == STACK_POINTER_REGNUM
12230 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
12231 || regno == HARD_FRAME_POINTER_REGNUM
12232 #endif
12233 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
12234 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
12235 #endif
12236 || regno == FRAME_POINTER_REGNUM)
12237 return;
12238
12239 endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
12240 for (r = regno; r < endregno; r++)
12241 SET_HARD_REG_BIT (newpat_used_regs, r);
12242 }
12243 return;
12244
12245 case SET:
12246 {
12247 /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
12248 the address. */
12249 rtx testreg = SET_DEST (x);
12250
12251 while (GET_CODE (testreg) == SUBREG
12252 || GET_CODE (testreg) == ZERO_EXTRACT
12253 || GET_CODE (testreg) == SIGN_EXTRACT
12254 || GET_CODE (testreg) == STRICT_LOW_PART)
12255 testreg = XEXP (testreg, 0);
12256
12257 if (GET_CODE (testreg) == MEM)
12258 mark_used_regs_combine (XEXP (testreg, 0));
12259
12260 mark_used_regs_combine (SET_SRC (x));
12261 }
12262 return;
12263
12264 default:
12265 break;
12266 }
12267
12268 /* Recursively scan the operands of this expression. */
12269
12270 {
12271 const char *fmt = GET_RTX_FORMAT (code);
12272
12273 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12274 {
12275 if (fmt[i] == 'e')
12276 mark_used_regs_combine (XEXP (x, i));
12277 else if (fmt[i] == 'E')
12278 {
12279 int j;
12280
12281 for (j = 0; j < XVECLEN (x, i); j++)
12282 mark_used_regs_combine (XVECEXP (x, i, j));
12283 }
12284 }
12285 }
12286 }
12287 \f
12288 /* Remove register number REGNO from the dead registers list of INSN.
12289
12290 Return the note used to record the death, if there was one. */
12291
12292 rtx
12293 remove_death (regno, insn)
12294 unsigned int regno;
12295 rtx insn;
12296 {
12297 rtx note = find_regno_note (insn, REG_DEAD, regno);
12298
12299 if (note)
12300 {
12301 REG_N_DEATHS (regno)--;
12302 remove_note (insn, note);
12303 }
12304
12305 return note;
12306 }
12307
12308 /* For each register (hardware or pseudo) used within expression X, if its
12309 death is in an instruction with cuid between FROM_CUID (inclusive) and
12310 TO_INSN (exclusive), put a REG_DEAD note for that register in the
12311 list headed by PNOTES.
12312
12313 That said, don't move registers killed by maybe_kill_insn.
12314
12315 This is done when X is being merged by combination into TO_INSN. These
12316 notes will then be distributed as needed. */
12317
12318 static void
12319 move_deaths (x, maybe_kill_insn, from_cuid, to_insn, pnotes)
12320 rtx x;
12321 rtx maybe_kill_insn;
12322 int from_cuid;
12323 rtx to_insn;
12324 rtx *pnotes;
12325 {
12326 const char *fmt;
12327 int len, i;
12328 enum rtx_code code = GET_CODE (x);
12329
12330 if (code == REG)
12331 {
12332 unsigned int regno = REGNO (x);
12333 rtx where_dead = reg_last_death[regno];
12334 rtx before_dead, after_dead;
12335
12336 /* Don't move the register if it gets killed in between from and to. */
12337 if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
12338 && ! reg_referenced_p (x, maybe_kill_insn))
12339 return;
12340
12341 /* WHERE_DEAD could be a USE insn made by combine, so first we
12342 make sure that we have insns with valid INSN_CUID values. */
12343 before_dead = where_dead;
12344 while (before_dead && INSN_UID (before_dead) > max_uid_cuid)
12345 before_dead = PREV_INSN (before_dead);
12346
12347 after_dead = where_dead;
12348 while (after_dead && INSN_UID (after_dead) > max_uid_cuid)
12349 after_dead = NEXT_INSN (after_dead);
12350
12351 if (before_dead && after_dead
12352 && INSN_CUID (before_dead) >= from_cuid
12353 && (INSN_CUID (after_dead) < INSN_CUID (to_insn)
12354 || (where_dead != after_dead
12355 && INSN_CUID (after_dead) == INSN_CUID (to_insn))))
12356 {
12357 rtx note = remove_death (regno, where_dead);
12358
12359 /* It is possible for the call above to return 0. This can occur
12360 when reg_last_death points to I2 or I1 that we combined with.
12361 In that case make a new note.
12362
12363 We must also check for the case where X is a hard register
12364 and NOTE is a death note for a range of hard registers
12365 including X. In that case, we must put REG_DEAD notes for
12366 the remaining registers in place of NOTE. */
12367
12368 if (note != 0 && regno < FIRST_PSEUDO_REGISTER
12369 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
12370 > GET_MODE_SIZE (GET_MODE (x))))
12371 {
12372 unsigned int deadregno = REGNO (XEXP (note, 0));
12373 unsigned int deadend
12374 = (deadregno + HARD_REGNO_NREGS (deadregno,
12375 GET_MODE (XEXP (note, 0))));
12376 unsigned int ourend
12377 = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
12378 unsigned int i;
12379
12380 for (i = deadregno; i < deadend; i++)
12381 if (i < regno || i >= ourend)
12382 REG_NOTES (where_dead)
12383 = gen_rtx_EXPR_LIST (REG_DEAD,
12384 regno_reg_rtx[i],
12385 REG_NOTES (where_dead));
12386 }
12387
12388 /* If we didn't find any note, or if we found a REG_DEAD note that
12389 covers only part of the given reg, and we have a multi-reg hard
12390 register, then to be safe we must check for REG_DEAD notes
12391 for each register other than the first. They could have
12392 their own REG_DEAD notes lying around. */
12393 else if ((note == 0
12394 || (note != 0
12395 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
12396 < GET_MODE_SIZE (GET_MODE (x)))))
12397 && regno < FIRST_PSEUDO_REGISTER
12398 && HARD_REGNO_NREGS (regno, GET_MODE (x)) > 1)
12399 {
12400 unsigned int ourend
12401 = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
12402 unsigned int i, offset;
12403 rtx oldnotes = 0;
12404
12405 if (note)
12406 offset = HARD_REGNO_NREGS (regno, GET_MODE (XEXP (note, 0)));
12407 else
12408 offset = 1;
12409
12410 for (i = regno + offset; i < ourend; i++)
12411 move_deaths (regno_reg_rtx[i],
12412 maybe_kill_insn, from_cuid, to_insn, &oldnotes);
12413 }
12414
12415 if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
12416 {
12417 XEXP (note, 1) = *pnotes;
12418 *pnotes = note;
12419 }
12420 else
12421 *pnotes = gen_rtx_EXPR_LIST (REG_DEAD, x, *pnotes);
12422
12423 REG_N_DEATHS (regno)++;
12424 }
12425
12426 return;
12427 }
12428
12429 else if (GET_CODE (x) == SET)
12430 {
12431 rtx dest = SET_DEST (x);
12432
12433 move_deaths (SET_SRC (x), maybe_kill_insn, from_cuid, to_insn, pnotes);
12434
12435 /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
12436 that accesses one word of a multi-word item, some
12437 piece of everything register in the expression is used by
12438 this insn, so remove any old death. */
12439 /* ??? So why do we test for equality of the sizes? */
12440
12441 if (GET_CODE (dest) == ZERO_EXTRACT
12442 || GET_CODE (dest) == STRICT_LOW_PART
12443 || (GET_CODE (dest) == SUBREG
12444 && (((GET_MODE_SIZE (GET_MODE (dest))
12445 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
12446 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
12447 + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
12448 {
12449 move_deaths (dest, maybe_kill_insn, from_cuid, to_insn, pnotes);
12450 return;
12451 }
12452
12453 /* If this is some other SUBREG, we know it replaces the entire
12454 value, so use that as the destination. */
12455 if (GET_CODE (dest) == SUBREG)
12456 dest = SUBREG_REG (dest);
12457
12458 /* If this is a MEM, adjust deaths of anything used in the address.
12459 For a REG (the only other possibility), the entire value is
12460 being replaced so the old value is not used in this insn. */
12461
12462 if (GET_CODE (dest) == MEM)
12463 move_deaths (XEXP (dest, 0), maybe_kill_insn, from_cuid,
12464 to_insn, pnotes);
12465 return;
12466 }
12467
12468 else if (GET_CODE (x) == CLOBBER)
12469 return;
12470
12471 len = GET_RTX_LENGTH (code);
12472 fmt = GET_RTX_FORMAT (code);
12473
12474 for (i = 0; i < len; i++)
12475 {
12476 if (fmt[i] == 'E')
12477 {
12478 int j;
12479 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
12480 move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_cuid,
12481 to_insn, pnotes);
12482 }
12483 else if (fmt[i] == 'e')
12484 move_deaths (XEXP (x, i), maybe_kill_insn, from_cuid, to_insn, pnotes);
12485 }
12486 }
12487 \f
12488 /* Return 1 if X is the target of a bit-field assignment in BODY, the
12489 pattern of an insn. X must be a REG. */
12490
12491 static int
12492 reg_bitfield_target_p (x, body)
12493 rtx x;
12494 rtx body;
12495 {
12496 int i;
12497
12498 if (GET_CODE (body) == SET)
12499 {
12500 rtx dest = SET_DEST (body);
12501 rtx target;
12502 unsigned int regno, tregno, endregno, endtregno;
12503
12504 if (GET_CODE (dest) == ZERO_EXTRACT)
12505 target = XEXP (dest, 0);
12506 else if (GET_CODE (dest) == STRICT_LOW_PART)
12507 target = SUBREG_REG (XEXP (dest, 0));
12508 else
12509 return 0;
12510
12511 if (GET_CODE (target) == SUBREG)
12512 target = SUBREG_REG (target);
12513
12514 if (GET_CODE (target) != REG)
12515 return 0;
12516
12517 tregno = REGNO (target), regno = REGNO (x);
12518 if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
12519 return target == x;
12520
12521 endtregno = tregno + HARD_REGNO_NREGS (tregno, GET_MODE (target));
12522 endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
12523
12524 return endregno > tregno && regno < endtregno;
12525 }
12526
12527 else if (GET_CODE (body) == PARALLEL)
12528 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
12529 if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
12530 return 1;
12531
12532 return 0;
12533 }
12534 \f
12535 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
12536 as appropriate. I3 and I2 are the insns resulting from the combination
12537 insns including FROM (I2 may be zero).
12538
12539 Each note in the list is either ignored or placed on some insns, depending
12540 on the type of note. */
12541
12542 static void
12543 distribute_notes (notes, from_insn, i3, i2)
12544 rtx notes;
12545 rtx from_insn;
12546 rtx i3, i2;
12547 {
12548 rtx note, next_note;
12549 rtx tem;
12550
12551 for (note = notes; note; note = next_note)
12552 {
12553 rtx place = 0, place2 = 0;
12554
12555 /* If this NOTE references a pseudo register, ensure it references
12556 the latest copy of that register. */
12557 if (XEXP (note, 0) && GET_CODE (XEXP (note, 0)) == REG
12558 && REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER)
12559 XEXP (note, 0) = regno_reg_rtx[REGNO (XEXP (note, 0))];
12560
12561 next_note = XEXP (note, 1);
12562 switch (REG_NOTE_KIND (note))
12563 {
12564 case REG_BR_PROB:
12565 case REG_BR_PRED:
12566 /* Doesn't matter much where we put this, as long as it's somewhere.
12567 It is preferable to keep these notes on branches, which is most
12568 likely to be i3. */
12569 place = i3;
12570 break;
12571
12572 case REG_VTABLE_REF:
12573 /* ??? Should remain with *a particular* memory load. Given the
12574 nature of vtable data, the last insn seems relatively safe. */
12575 place = i3;
12576 break;
12577
12578 case REG_NON_LOCAL_GOTO:
12579 if (GET_CODE (i3) == JUMP_INSN)
12580 place = i3;
12581 else if (i2 && GET_CODE (i2) == JUMP_INSN)
12582 place = i2;
12583 else
12584 abort ();
12585 break;
12586
12587 case REG_EH_REGION:
12588 /* These notes must remain with the call or trapping instruction. */
12589 if (GET_CODE (i3) == CALL_INSN)
12590 place = i3;
12591 else if (i2 && GET_CODE (i2) == CALL_INSN)
12592 place = i2;
12593 else if (flag_non_call_exceptions)
12594 {
12595 if (may_trap_p (i3))
12596 place = i3;
12597 else if (i2 && may_trap_p (i2))
12598 place = i2;
12599 /* ??? Otherwise assume we've combined things such that we
12600 can now prove that the instructions can't trap. Drop the
12601 note in this case. */
12602 }
12603 else
12604 abort ();
12605 break;
12606
12607 case REG_NORETURN:
12608 case REG_SETJMP:
12609 /* These notes must remain with the call. It should not be
12610 possible for both I2 and I3 to be a call. */
12611 if (GET_CODE (i3) == CALL_INSN)
12612 place = i3;
12613 else if (i2 && GET_CODE (i2) == CALL_INSN)
12614 place = i2;
12615 else
12616 abort ();
12617 break;
12618
12619 case REG_UNUSED:
12620 /* Any clobbers for i3 may still exist, and so we must process
12621 REG_UNUSED notes from that insn.
12622
12623 Any clobbers from i2 or i1 can only exist if they were added by
12624 recog_for_combine. In that case, recog_for_combine created the
12625 necessary REG_UNUSED notes. Trying to keep any original
12626 REG_UNUSED notes from these insns can cause incorrect output
12627 if it is for the same register as the original i3 dest.
12628 In that case, we will notice that the register is set in i3,
12629 and then add a REG_UNUSED note for the destination of i3, which
12630 is wrong. However, it is possible to have REG_UNUSED notes from
12631 i2 or i1 for register which were both used and clobbered, so
12632 we keep notes from i2 or i1 if they will turn into REG_DEAD
12633 notes. */
12634
12635 /* If this register is set or clobbered in I3, put the note there
12636 unless there is one already. */
12637 if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
12638 {
12639 if (from_insn != i3)
12640 break;
12641
12642 if (! (GET_CODE (XEXP (note, 0)) == REG
12643 ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
12644 : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
12645 place = i3;
12646 }
12647 /* Otherwise, if this register is used by I3, then this register
12648 now dies here, so we must put a REG_DEAD note here unless there
12649 is one already. */
12650 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
12651 && ! (GET_CODE (XEXP (note, 0)) == REG
12652 ? find_regno_note (i3, REG_DEAD,
12653 REGNO (XEXP (note, 0)))
12654 : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
12655 {
12656 PUT_REG_NOTE_KIND (note, REG_DEAD);
12657 place = i3;
12658 }
12659 break;
12660
12661 case REG_EQUAL:
12662 case REG_EQUIV:
12663 case REG_NOALIAS:
12664 /* These notes say something about results of an insn. We can
12665 only support them if they used to be on I3 in which case they
12666 remain on I3. Otherwise they are ignored.
12667
12668 If the note refers to an expression that is not a constant, we
12669 must also ignore the note since we cannot tell whether the
12670 equivalence is still true. It might be possible to do
12671 slightly better than this (we only have a problem if I2DEST
12672 or I1DEST is present in the expression), but it doesn't
12673 seem worth the trouble. */
12674
12675 if (from_insn == i3
12676 && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
12677 place = i3;
12678 break;
12679
12680 case REG_INC:
12681 case REG_NO_CONFLICT:
12682 /* These notes say something about how a register is used. They must
12683 be present on any use of the register in I2 or I3. */
12684 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
12685 place = i3;
12686
12687 if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
12688 {
12689 if (place)
12690 place2 = i2;
12691 else
12692 place = i2;
12693 }
12694 break;
12695
12696 case REG_LABEL:
12697 /* This can show up in several ways -- either directly in the
12698 pattern, or hidden off in the constant pool with (or without?)
12699 a REG_EQUAL note. */
12700 /* ??? Ignore the without-reg_equal-note problem for now. */
12701 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
12702 || ((tem = find_reg_note (i3, REG_EQUAL, NULL_RTX))
12703 && GET_CODE (XEXP (tem, 0)) == LABEL_REF
12704 && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0)))
12705 place = i3;
12706
12707 if (i2
12708 && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
12709 || ((tem = find_reg_note (i2, REG_EQUAL, NULL_RTX))
12710 && GET_CODE (XEXP (tem, 0)) == LABEL_REF
12711 && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0))))
12712 {
12713 if (place)
12714 place2 = i2;
12715 else
12716 place = i2;
12717 }
12718
12719 /* Don't attach REG_LABEL note to a JUMP_INSN which has
12720 JUMP_LABEL already. Instead, decrement LABEL_NUSES. */
12721 if (place && GET_CODE (place) == JUMP_INSN && JUMP_LABEL (place))
12722 {
12723 if (JUMP_LABEL (place) != XEXP (note, 0))
12724 abort ();
12725 if (GET_CODE (JUMP_LABEL (place)) == CODE_LABEL)
12726 LABEL_NUSES (JUMP_LABEL (place))--;
12727 place = 0;
12728 }
12729 if (place2 && GET_CODE (place2) == JUMP_INSN && JUMP_LABEL (place2))
12730 {
12731 if (JUMP_LABEL (place2) != XEXP (note, 0))
12732 abort ();
12733 if (GET_CODE (JUMP_LABEL (place2)) == CODE_LABEL)
12734 LABEL_NUSES (JUMP_LABEL (place2))--;
12735 place2 = 0;
12736 }
12737 break;
12738
12739 case REG_NONNEG:
12740 case REG_WAS_0:
12741 /* These notes say something about the value of a register prior
12742 to the execution of an insn. It is too much trouble to see
12743 if the note is still correct in all situations. It is better
12744 to simply delete it. */
12745 break;
12746
12747 case REG_RETVAL:
12748 /* If the insn previously containing this note still exists,
12749 put it back where it was. Otherwise move it to the previous
12750 insn. Adjust the corresponding REG_LIBCALL note. */
12751 if (GET_CODE (from_insn) != NOTE)
12752 place = from_insn;
12753 else
12754 {
12755 tem = find_reg_note (XEXP (note, 0), REG_LIBCALL, NULL_RTX);
12756 place = prev_real_insn (from_insn);
12757 if (tem && place)
12758 XEXP (tem, 0) = place;
12759 /* If we're deleting the last remaining instruction of a
12760 libcall sequence, don't add the notes. */
12761 else if (XEXP (note, 0) == from_insn)
12762 tem = place = 0;
12763 }
12764 break;
12765
12766 case REG_LIBCALL:
12767 /* This is handled similarly to REG_RETVAL. */
12768 if (GET_CODE (from_insn) != NOTE)
12769 place = from_insn;
12770 else
12771 {
12772 tem = find_reg_note (XEXP (note, 0), REG_RETVAL, NULL_RTX);
12773 place = next_real_insn (from_insn);
12774 if (tem && place)
12775 XEXP (tem, 0) = place;
12776 /* If we're deleting the last remaining instruction of a
12777 libcall sequence, don't add the notes. */
12778 else if (XEXP (note, 0) == from_insn)
12779 tem = place = 0;
12780 }
12781 break;
12782
12783 case REG_DEAD:
12784 /* If the register is used as an input in I3, it dies there.
12785 Similarly for I2, if it is nonzero and adjacent to I3.
12786
12787 If the register is not used as an input in either I3 or I2
12788 and it is not one of the registers we were supposed to eliminate,
12789 there are two possibilities. We might have a non-adjacent I2
12790 or we might have somehow eliminated an additional register
12791 from a computation. For example, we might have had A & B where
12792 we discover that B will always be zero. In this case we will
12793 eliminate the reference to A.
12794
12795 In both cases, we must search to see if we can find a previous
12796 use of A and put the death note there. */
12797
12798 if (from_insn
12799 && GET_CODE (from_insn) == CALL_INSN
12800 && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
12801 place = from_insn;
12802 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
12803 place = i3;
12804 else if (i2 != 0 && next_nonnote_insn (i2) == i3
12805 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
12806 place = i2;
12807
12808 if (place == 0)
12809 {
12810 basic_block bb = this_basic_block;
12811
12812 for (tem = PREV_INSN (i3); place == 0; tem = PREV_INSN (tem))
12813 {
12814 if (! INSN_P (tem))
12815 {
12816 if (tem == bb->head)
12817 break;
12818 continue;
12819 }
12820
12821 /* If the register is being set at TEM, see if that is all
12822 TEM is doing. If so, delete TEM. Otherwise, make this
12823 into a REG_UNUSED note instead. */
12824 if (reg_set_p (XEXP (note, 0), PATTERN (tem)))
12825 {
12826 rtx set = single_set (tem);
12827 rtx inner_dest = 0;
12828 #ifdef HAVE_cc0
12829 rtx cc0_setter = NULL_RTX;
12830 #endif
12831
12832 if (set != 0)
12833 for (inner_dest = SET_DEST (set);
12834 (GET_CODE (inner_dest) == STRICT_LOW_PART
12835 || GET_CODE (inner_dest) == SUBREG
12836 || GET_CODE (inner_dest) == ZERO_EXTRACT);
12837 inner_dest = XEXP (inner_dest, 0))
12838 ;
12839
12840 /* Verify that it was the set, and not a clobber that
12841 modified the register.
12842
12843 CC0 targets must be careful to maintain setter/user
12844 pairs. If we cannot delete the setter due to side
12845 effects, mark the user with an UNUSED note instead
12846 of deleting it. */
12847
12848 if (set != 0 && ! side_effects_p (SET_SRC (set))
12849 && rtx_equal_p (XEXP (note, 0), inner_dest)
12850 #ifdef HAVE_cc0
12851 && (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
12852 || ((cc0_setter = prev_cc0_setter (tem)) != NULL
12853 && sets_cc0_p (PATTERN (cc0_setter)) > 0))
12854 #endif
12855 )
12856 {
12857 /* Move the notes and links of TEM elsewhere.
12858 This might delete other dead insns recursively.
12859 First set the pattern to something that won't use
12860 any register. */
12861
12862 PATTERN (tem) = pc_rtx;
12863
12864 distribute_notes (REG_NOTES (tem), tem, tem,
12865 NULL_RTX);
12866 distribute_links (LOG_LINKS (tem));
12867
12868 PUT_CODE (tem, NOTE);
12869 NOTE_LINE_NUMBER (tem) = NOTE_INSN_DELETED;
12870 NOTE_SOURCE_FILE (tem) = 0;
12871
12872 #ifdef HAVE_cc0
12873 /* Delete the setter too. */
12874 if (cc0_setter)
12875 {
12876 PATTERN (cc0_setter) = pc_rtx;
12877
12878 distribute_notes (REG_NOTES (cc0_setter),
12879 cc0_setter, cc0_setter,
12880 NULL_RTX);
12881 distribute_links (LOG_LINKS (cc0_setter));
12882
12883 PUT_CODE (cc0_setter, NOTE);
12884 NOTE_LINE_NUMBER (cc0_setter)
12885 = NOTE_INSN_DELETED;
12886 NOTE_SOURCE_FILE (cc0_setter) = 0;
12887 }
12888 #endif
12889 }
12890 /* If the register is both set and used here, put the
12891 REG_DEAD note here, but place a REG_UNUSED note
12892 here too unless there already is one. */
12893 else if (reg_referenced_p (XEXP (note, 0),
12894 PATTERN (tem)))
12895 {
12896 place = tem;
12897
12898 if (! find_regno_note (tem, REG_UNUSED,
12899 REGNO (XEXP (note, 0))))
12900 REG_NOTES (tem)
12901 = gen_rtx_EXPR_LIST (REG_UNUSED, XEXP (note, 0),
12902 REG_NOTES (tem));
12903 }
12904 else
12905 {
12906 PUT_REG_NOTE_KIND (note, REG_UNUSED);
12907
12908 /* If there isn't already a REG_UNUSED note, put one
12909 here. */
12910 if (! find_regno_note (tem, REG_UNUSED,
12911 REGNO (XEXP (note, 0))))
12912 place = tem;
12913 break;
12914 }
12915 }
12916 else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem))
12917 || (GET_CODE (tem) == CALL_INSN
12918 && find_reg_fusage (tem, USE, XEXP (note, 0))))
12919 {
12920 place = tem;
12921
12922 /* If we are doing a 3->2 combination, and we have a
12923 register which formerly died in i3 and was not used
12924 by i2, which now no longer dies in i3 and is used in
12925 i2 but does not die in i2, and place is between i2
12926 and i3, then we may need to move a link from place to
12927 i2. */
12928 if (i2 && INSN_UID (place) <= max_uid_cuid
12929 && INSN_CUID (place) > INSN_CUID (i2)
12930 && from_insn
12931 && INSN_CUID (from_insn) > INSN_CUID (i2)
12932 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
12933 {
12934 rtx links = LOG_LINKS (place);
12935 LOG_LINKS (place) = 0;
12936 distribute_links (links);
12937 }
12938 break;
12939 }
12940
12941 if (tem == bb->head)
12942 break;
12943 }
12944
12945 /* We haven't found an insn for the death note and it
12946 is still a REG_DEAD note, but we have hit the beginning
12947 of the block. If the existing life info says the reg
12948 was dead, there's nothing left to do. Otherwise, we'll
12949 need to do a global life update after combine. */
12950 if (REG_NOTE_KIND (note) == REG_DEAD && place == 0
12951 && REGNO_REG_SET_P (bb->global_live_at_start,
12952 REGNO (XEXP (note, 0))))
12953 SET_BIT (refresh_blocks, this_basic_block->index);
12954 }
12955
12956 /* If the register is set or already dead at PLACE, we needn't do
12957 anything with this note if it is still a REG_DEAD note.
12958 We can here if it is set at all, not if is it totally replace,
12959 which is what `dead_or_set_p' checks, so also check for it being
12960 set partially. */
12961
12962 if (place && REG_NOTE_KIND (note) == REG_DEAD)
12963 {
12964 unsigned int regno = REGNO (XEXP (note, 0));
12965
12966 /* Similarly, if the instruction on which we want to place
12967 the note is a noop, we'll need do a global live update
12968 after we remove them in delete_noop_moves. */
12969 if (noop_move_p (place))
12970 SET_BIT (refresh_blocks, this_basic_block->index);
12971
12972 if (dead_or_set_p (place, XEXP (note, 0))
12973 || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
12974 {
12975 /* Unless the register previously died in PLACE, clear
12976 reg_last_death. [I no longer understand why this is
12977 being done.] */
12978 if (reg_last_death[regno] != place)
12979 reg_last_death[regno] = 0;
12980 place = 0;
12981 }
12982 else
12983 reg_last_death[regno] = place;
12984
12985 /* If this is a death note for a hard reg that is occupying
12986 multiple registers, ensure that we are still using all
12987 parts of the object. If we find a piece of the object
12988 that is unused, we must arrange for an appropriate REG_DEAD
12989 note to be added for it. However, we can't just emit a USE
12990 and tag the note to it, since the register might actually
12991 be dead; so we recourse, and the recursive call then finds
12992 the previous insn that used this register. */
12993
12994 if (place && regno < FIRST_PSEUDO_REGISTER
12995 && HARD_REGNO_NREGS (regno, GET_MODE (XEXP (note, 0))) > 1)
12996 {
12997 unsigned int endregno
12998 = regno + HARD_REGNO_NREGS (regno,
12999 GET_MODE (XEXP (note, 0)));
13000 int all_used = 1;
13001 unsigned int i;
13002
13003 for (i = regno; i < endregno; i++)
13004 if ((! refers_to_regno_p (i, i + 1, PATTERN (place), 0)
13005 && ! find_regno_fusage (place, USE, i))
13006 || dead_or_set_regno_p (place, i))
13007 all_used = 0;
13008
13009 if (! all_used)
13010 {
13011 /* Put only REG_DEAD notes for pieces that are
13012 not already dead or set. */
13013
13014 for (i = regno; i < endregno;
13015 i += HARD_REGNO_NREGS (i, reg_raw_mode[i]))
13016 {
13017 rtx piece = regno_reg_rtx[i];
13018 basic_block bb = this_basic_block;
13019
13020 if (! dead_or_set_p (place, piece)
13021 && ! reg_bitfield_target_p (piece,
13022 PATTERN (place)))
13023 {
13024 rtx new_note
13025 = gen_rtx_EXPR_LIST (REG_DEAD, piece, NULL_RTX);
13026
13027 distribute_notes (new_note, place, place,
13028 NULL_RTX);
13029 }
13030 else if (! refers_to_regno_p (i, i + 1,
13031 PATTERN (place), 0)
13032 && ! find_regno_fusage (place, USE, i))
13033 for (tem = PREV_INSN (place); ;
13034 tem = PREV_INSN (tem))
13035 {
13036 if (! INSN_P (tem))
13037 {
13038 if (tem == bb->head)
13039 {
13040 SET_BIT (refresh_blocks,
13041 this_basic_block->index);
13042 break;
13043 }
13044 continue;
13045 }
13046 if (dead_or_set_p (tem, piece)
13047 || reg_bitfield_target_p (piece,
13048 PATTERN (tem)))
13049 {
13050 REG_NOTES (tem)
13051 = gen_rtx_EXPR_LIST (REG_UNUSED, piece,
13052 REG_NOTES (tem));
13053 break;
13054 }
13055 }
13056
13057 }
13058
13059 place = 0;
13060 }
13061 }
13062 }
13063 break;
13064
13065 default:
13066 /* Any other notes should not be present at this point in the
13067 compilation. */
13068 abort ();
13069 }
13070
13071 if (place)
13072 {
13073 XEXP (note, 1) = REG_NOTES (place);
13074 REG_NOTES (place) = note;
13075 }
13076 else if ((REG_NOTE_KIND (note) == REG_DEAD
13077 || REG_NOTE_KIND (note) == REG_UNUSED)
13078 && GET_CODE (XEXP (note, 0)) == REG)
13079 REG_N_DEATHS (REGNO (XEXP (note, 0)))--;
13080
13081 if (place2)
13082 {
13083 if ((REG_NOTE_KIND (note) == REG_DEAD
13084 || REG_NOTE_KIND (note) == REG_UNUSED)
13085 && GET_CODE (XEXP (note, 0)) == REG)
13086 REG_N_DEATHS (REGNO (XEXP (note, 0)))++;
13087
13088 REG_NOTES (place2) = gen_rtx_fmt_ee (GET_CODE (note),
13089 REG_NOTE_KIND (note),
13090 XEXP (note, 0),
13091 REG_NOTES (place2));
13092 }
13093 }
13094 }
13095 \f
13096 /* Similarly to above, distribute the LOG_LINKS that used to be present on
13097 I3, I2, and I1 to new locations. This is also called in one case to
13098 add a link pointing at I3 when I3's destination is changed. */
13099
13100 static void
13101 distribute_links (links)
13102 rtx links;
13103 {
13104 rtx link, next_link;
13105
13106 for (link = links; link; link = next_link)
13107 {
13108 rtx place = 0;
13109 rtx insn;
13110 rtx set, reg;
13111
13112 next_link = XEXP (link, 1);
13113
13114 /* If the insn that this link points to is a NOTE or isn't a single
13115 set, ignore it. In the latter case, it isn't clear what we
13116 can do other than ignore the link, since we can't tell which
13117 register it was for. Such links wouldn't be used by combine
13118 anyway.
13119
13120 It is not possible for the destination of the target of the link to
13121 have been changed by combine. The only potential of this is if we
13122 replace I3, I2, and I1 by I3 and I2. But in that case the
13123 destination of I2 also remains unchanged. */
13124
13125 if (GET_CODE (XEXP (link, 0)) == NOTE
13126 || (set = single_set (XEXP (link, 0))) == 0)
13127 continue;
13128
13129 reg = SET_DEST (set);
13130 while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
13131 || GET_CODE (reg) == SIGN_EXTRACT
13132 || GET_CODE (reg) == STRICT_LOW_PART)
13133 reg = XEXP (reg, 0);
13134
13135 /* A LOG_LINK is defined as being placed on the first insn that uses
13136 a register and points to the insn that sets the register. Start
13137 searching at the next insn after the target of the link and stop
13138 when we reach a set of the register or the end of the basic block.
13139
13140 Note that this correctly handles the link that used to point from
13141 I3 to I2. Also note that not much searching is typically done here
13142 since most links don't point very far away. */
13143
13144 for (insn = NEXT_INSN (XEXP (link, 0));
13145 (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
13146 || this_basic_block->next_bb->head != insn));
13147 insn = NEXT_INSN (insn))
13148 if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
13149 {
13150 if (reg_referenced_p (reg, PATTERN (insn)))
13151 place = insn;
13152 break;
13153 }
13154 else if (GET_CODE (insn) == CALL_INSN
13155 && find_reg_fusage (insn, USE, reg))
13156 {
13157 place = insn;
13158 break;
13159 }
13160
13161 /* If we found a place to put the link, place it there unless there
13162 is already a link to the same insn as LINK at that point. */
13163
13164 if (place)
13165 {
13166 rtx link2;
13167
13168 for (link2 = LOG_LINKS (place); link2; link2 = XEXP (link2, 1))
13169 if (XEXP (link2, 0) == XEXP (link, 0))
13170 break;
13171
13172 if (link2 == 0)
13173 {
13174 XEXP (link, 1) = LOG_LINKS (place);
13175 LOG_LINKS (place) = link;
13176
13177 /* Set added_links_insn to the earliest insn we added a
13178 link to. */
13179 if (added_links_insn == 0
13180 || INSN_CUID (added_links_insn) > INSN_CUID (place))
13181 added_links_insn = place;
13182 }
13183 }
13184 }
13185 }
13186 \f
13187 /* Compute INSN_CUID for INSN, which is an insn made by combine. */
13188
13189 static int
13190 insn_cuid (insn)
13191 rtx insn;
13192 {
13193 while (insn != 0 && INSN_UID (insn) > max_uid_cuid
13194 && GET_CODE (insn) == INSN && GET_CODE (PATTERN (insn)) == USE)
13195 insn = NEXT_INSN (insn);
13196
13197 if (INSN_UID (insn) > max_uid_cuid)
13198 abort ();
13199
13200 return INSN_CUID (insn);
13201 }
13202 \f
13203 void
13204 dump_combine_stats (file)
13205 FILE *file;
13206 {
13207 fnotice
13208 (file,
13209 ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
13210 combine_attempts, combine_merges, combine_extras, combine_successes);
13211 }
13212
13213 void
13214 dump_combine_total_stats (file)
13215 FILE *file;
13216 {
13217 fnotice
13218 (file,
13219 "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
13220 total_attempts, total_merges, total_extras, total_successes);
13221 }