* Makefile.in (typeck2.o): Depend on output.h.
[official-gcc.git] / gcc / combine.c
blob4a1cb0ee62c8d36d4722e349572719debee561d2
1 /* Optimize by combining instructions for GNU compiler.
2 Copyright (C) 1987, 88, 92-98, 1999 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
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.
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).
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.
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.
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.
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.
52 There are a few exceptions where the dataflow information created by
53 flow.c aren't completely updated:
55 - reg_live_length is not updated
56 - reg_n_refs is not adjusted in the rare case when a register is
57 no longer required in a computation
58 - there are extremely rare cases (see distribute_regnotes) when a
59 REG_DEAD note is lost
60 - a LOG_LINKS entry that refers to an insn with multiple SETs may be
61 removed because there is no way to know which register it was
62 linking
64 To simplify substitution, we combine only when the earlier insn(s)
65 consist of only a single assignment. To simplify updating afterward,
66 we never combine when a subroutine call appears in the middle.
68 Since we do not represent assignments to CC0 explicitly except when that
69 is all an insn does, there is no LOG_LINKS entry in an insn that uses
70 the condition code for the insn that set the condition code.
71 Fortunately, these two insns must be consecutive.
72 Therefore, every JUMP_INSN is taken to have an implicit logical link
73 to the preceding insn. This is not quite right, since non-jumps can
74 also use the condition code; but in practice such insns would not
75 combine anyway. */
77 #include "config.h"
78 #include "system.h"
79 #include "rtl.h"
80 #include "tm_p.h"
81 #include "flags.h"
82 #include "regs.h"
83 #include "hard-reg-set.h"
84 #include "basic-block.h"
85 #include "insn-config.h"
86 #include "function.h"
87 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
88 #include "expr.h"
89 #include "insn-flags.h"
90 #include "insn-codes.h"
91 #include "insn-attr.h"
92 #include "recog.h"
93 #include "real.h"
94 #include "toplev.h"
96 /* It is not safe to use ordinary gen_lowpart in combine.
97 Use gen_lowpart_for_combine instead. See comments there. */
98 #define gen_lowpart dont_use_gen_lowpart_you_dummy
100 /* Number of attempts to combine instructions in this function. */
102 static int combine_attempts;
104 /* Number of attempts that got as far as substitution in this function. */
106 static int combine_merges;
108 /* Number of instructions combined with added SETs in this function. */
110 static int combine_extras;
112 /* Number of instructions combined in this function. */
114 static int combine_successes;
116 /* Totals over entire compilation. */
118 static int total_attempts, total_merges, total_extras, total_successes;
120 /* Define a default value for REVERSIBLE_CC_MODE.
121 We can never assume that a condition code mode is safe to reverse unless
122 the md tells us so. */
123 #ifndef REVERSIBLE_CC_MODE
124 #define REVERSIBLE_CC_MODE(MODE) 0
125 #endif
127 /* Vector mapping INSN_UIDs to cuids.
128 The cuids are like uids but increase monotonically always.
129 Combine always uses cuids so that it can compare them.
130 But actually renumbering the uids, which we used to do,
131 proves to be a bad idea because it makes it hard to compare
132 the dumps produced by earlier passes with those from later passes. */
134 static int *uid_cuid;
135 static int max_uid_cuid;
137 /* Get the cuid of an insn. */
139 #define INSN_CUID(INSN) \
140 (INSN_UID (INSN) > max_uid_cuid ? insn_cuid (INSN) : uid_cuid[INSN_UID (INSN)])
142 /* Maximum register number, which is the size of the tables below. */
144 static int combine_max_regno;
146 /* Record last point of death of (hard or pseudo) register n. */
148 static rtx *reg_last_death;
150 /* Record last point of modification of (hard or pseudo) register n. */
152 static rtx *reg_last_set;
154 /* Record the cuid of the last insn that invalidated memory
155 (anything that writes memory, and subroutine calls, but not pushes). */
157 static int mem_last_set;
159 /* Record the cuid of the last CALL_INSN
160 so we can tell whether a potential combination crosses any calls. */
162 static int last_call_cuid;
164 /* When `subst' is called, this is the insn that is being modified
165 (by combining in a previous insn). The PATTERN of this insn
166 is still the old pattern partially modified and it should not be
167 looked at, but this may be used to examine the successors of the insn
168 to judge whether a simplification is valid. */
170 static rtx subst_insn;
172 /* This is an insn that belongs before subst_insn, but is not currently
173 on the insn chain. */
175 static rtx subst_prev_insn;
177 /* This is the lowest CUID that `subst' is currently dealing with.
178 get_last_value will not return a value if the register was set at or
179 after this CUID. If not for this mechanism, we could get confused if
180 I2 or I1 in try_combine were an insn that used the old value of a register
181 to obtain a new value. In that case, we might erroneously get the
182 new value of the register when we wanted the old one. */
184 static int subst_low_cuid;
186 /* This contains any hard registers that are used in newpat; reg_dead_at_p
187 must consider all these registers to be always live. */
189 static HARD_REG_SET newpat_used_regs;
191 /* This is an insn to which a LOG_LINKS entry has been added. If this
192 insn is the earlier than I2 or I3, combine should rescan starting at
193 that location. */
195 static rtx added_links_insn;
197 /* Basic block number of the block in which we are performing combines. */
198 static int this_basic_block;
200 /* A bitmap indicating which blocks had registers go dead at entry.
201 After combine, we'll need to re-do global life analysis with
202 those blocks as starting points. */
203 static sbitmap refresh_blocks;
204 static int need_refresh;
206 /* The next group of arrays allows the recording of the last value assigned
207 to (hard or pseudo) register n. We use this information to see if a
208 operation being processed is redundant given a prior operation performed
209 on the register. For example, an `and' with a constant is redundant if
210 all the zero bits are already known to be turned off.
212 We use an approach similar to that used by cse, but change it in the
213 following ways:
215 (1) We do not want to reinitialize at each label.
216 (2) It is useful, but not critical, to know the actual value assigned
217 to a register. Often just its form is helpful.
219 Therefore, we maintain the following arrays:
221 reg_last_set_value the last value assigned
222 reg_last_set_label records the value of label_tick when the
223 register was assigned
224 reg_last_set_table_tick records the value of label_tick when a
225 value using the register is assigned
226 reg_last_set_invalid set to non-zero when it is not valid
227 to use the value of this register in some
228 register's value
230 To understand the usage of these tables, it is important to understand
231 the distinction between the value in reg_last_set_value being valid
232 and the register being validly contained in some other expression in the
233 table.
235 Entry I in reg_last_set_value is valid if it is non-zero, and either
236 reg_n_sets[i] is 1 or reg_last_set_label[i] == label_tick.
238 Register I may validly appear in any expression returned for the value
239 of another register if reg_n_sets[i] is 1. It may also appear in the
240 value for register J if reg_last_set_label[i] < reg_last_set_label[j] or
241 reg_last_set_invalid[j] is zero.
243 If an expression is found in the table containing a register which may
244 not validly appear in an expression, the register is replaced by
245 something that won't match, (clobber (const_int 0)).
247 reg_last_set_invalid[i] is set non-zero when register I is being assigned
248 to and reg_last_set_table_tick[i] == label_tick. */
250 /* Record last value assigned to (hard or pseudo) register n. */
252 static rtx *reg_last_set_value;
254 /* Record the value of label_tick when the value for register n is placed in
255 reg_last_set_value[n]. */
257 static int *reg_last_set_label;
259 /* Record the value of label_tick when an expression involving register n
260 is placed in reg_last_set_value. */
262 static int *reg_last_set_table_tick;
264 /* Set non-zero if references to register n in expressions should not be
265 used. */
267 static char *reg_last_set_invalid;
269 /* Incremented for each label. */
271 static int label_tick;
273 /* Some registers that are set more than once and used in more than one
274 basic block are nevertheless always set in similar ways. For example,
275 a QImode register may be loaded from memory in two places on a machine
276 where byte loads zero extend.
278 We record in the following array what we know about the nonzero
279 bits of a register, specifically which bits are known to be zero.
281 If an entry is zero, it means that we don't know anything special. */
283 static unsigned HOST_WIDE_INT *reg_nonzero_bits;
285 /* Mode used to compute significance in reg_nonzero_bits. It is the largest
286 integer mode that can fit in HOST_BITS_PER_WIDE_INT. */
288 static enum machine_mode nonzero_bits_mode;
290 /* Nonzero if we know that a register has some leading bits that are always
291 equal to the sign bit. */
293 static char *reg_sign_bit_copies;
295 /* Nonzero when reg_nonzero_bits and reg_sign_bit_copies can be safely used.
296 It is zero while computing them and after combine has completed. This
297 former test prevents propagating values based on previously set values,
298 which can be incorrect if a variable is modified in a loop. */
300 static int nonzero_sign_valid;
302 /* These arrays are maintained in parallel with reg_last_set_value
303 and are used to store the mode in which the register was last set,
304 the bits that were known to be zero when it was last set, and the
305 number of sign bits copies it was known to have when it was last set. */
307 static enum machine_mode *reg_last_set_mode;
308 static unsigned HOST_WIDE_INT *reg_last_set_nonzero_bits;
309 static char *reg_last_set_sign_bit_copies;
311 /* Record one modification to rtl structure
312 to be undone by storing old_contents into *where.
313 is_int is 1 if the contents are an int. */
315 struct undo
317 struct undo *next;
318 int is_int;
319 union {rtx r; int i;} old_contents;
320 union {rtx *r; int *i;} where;
323 /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
324 num_undo says how many are currently recorded.
326 storage is nonzero if we must undo the allocation of new storage.
327 The value of storage is what to pass to obfree.
329 other_insn is nonzero if we have modified some other insn in the process
330 of working on subst_insn. It must be verified too.
332 previous_undos is the value of undobuf.undos when we started processing
333 this substitution. This will prevent gen_rtx_combine from re-used a piece
334 from the previous expression. Doing so can produce circular rtl
335 structures. */
337 struct undobuf
339 char *storage;
340 struct undo *undos;
341 struct undo *frees;
342 struct undo *previous_undos;
343 rtx other_insn;
346 static struct undobuf undobuf;
348 /* Number of times the pseudo being substituted for
349 was found and replaced. */
351 static int n_occurrences;
353 static void do_SUBST PROTO((rtx *, rtx));
354 static void do_SUBST_INT PROTO((int *, int));
355 static void init_reg_last_arrays PROTO((void));
356 static void setup_incoming_promotions PROTO((void));
357 static void set_nonzero_bits_and_sign_copies PROTO((rtx, rtx, void *));
358 static int can_combine_p PROTO((rtx, rtx, rtx, rtx, rtx *, rtx *));
359 static int sets_function_arg_p PROTO((rtx));
360 static int combinable_i3pat PROTO((rtx, rtx *, rtx, rtx, int, rtx *));
361 static int contains_muldiv PROTO((rtx));
362 static rtx try_combine PROTO((rtx, rtx, rtx));
363 static void undo_all PROTO((void));
364 static void undo_commit PROTO((void));
365 static rtx *find_split_point PROTO((rtx *, rtx));
366 static rtx subst PROTO((rtx, rtx, rtx, int, int));
367 static rtx combine_simplify_rtx PROTO((rtx, enum machine_mode, int, int));
368 static rtx simplify_if_then_else PROTO((rtx));
369 static rtx simplify_set PROTO((rtx));
370 static rtx simplify_logical PROTO((rtx, int));
371 static rtx expand_compound_operation PROTO((rtx));
372 static rtx expand_field_assignment PROTO((rtx));
373 static rtx make_extraction PROTO((enum machine_mode, rtx, int, rtx, int,
374 int, int, int));
375 static rtx extract_left_shift PROTO((rtx, int));
376 static rtx make_compound_operation PROTO((rtx, enum rtx_code));
377 static int get_pos_from_mask PROTO((unsigned HOST_WIDE_INT, int *));
378 static rtx force_to_mode PROTO((rtx, enum machine_mode,
379 unsigned HOST_WIDE_INT, rtx, int));
380 static rtx if_then_else_cond PROTO((rtx, rtx *, rtx *));
381 static rtx known_cond PROTO((rtx, enum rtx_code, rtx, rtx));
382 static int rtx_equal_for_field_assignment_p PROTO((rtx, rtx));
383 static rtx make_field_assignment PROTO((rtx));
384 static rtx apply_distributive_law PROTO((rtx));
385 static rtx simplify_and_const_int PROTO((rtx, enum machine_mode, rtx,
386 unsigned HOST_WIDE_INT));
387 static unsigned HOST_WIDE_INT nonzero_bits PROTO((rtx, enum machine_mode));
388 static int num_sign_bit_copies PROTO((rtx, enum machine_mode));
389 static int merge_outer_ops PROTO((enum rtx_code *, HOST_WIDE_INT *,
390 enum rtx_code, HOST_WIDE_INT,
391 enum machine_mode, int *));
392 static rtx simplify_shift_const PROTO((rtx, enum rtx_code, enum machine_mode,
393 rtx, int));
394 static int recog_for_combine PROTO((rtx *, rtx, rtx *));
395 static rtx gen_lowpart_for_combine PROTO((enum machine_mode, rtx));
396 static rtx gen_rtx_combine PVPROTO((enum rtx_code code, enum machine_mode mode,
397 ...));
398 static rtx gen_binary PROTO((enum rtx_code, enum machine_mode,
399 rtx, rtx));
400 static rtx gen_unary PROTO((enum rtx_code, enum machine_mode,
401 enum machine_mode, rtx));
402 static enum rtx_code simplify_comparison PROTO((enum rtx_code, rtx *, rtx *));
403 static int reversible_comparison_p PROTO((rtx));
404 static void update_table_tick PROTO((rtx));
405 static void record_value_for_reg PROTO((rtx, rtx, rtx));
406 static void record_dead_and_set_regs_1 PROTO((rtx, rtx, void *));
407 static void record_dead_and_set_regs PROTO((rtx));
408 static int get_last_value_validate PROTO((rtx *, rtx, int, int));
409 static rtx get_last_value PROTO((rtx));
410 static int use_crosses_set_p PROTO((rtx, int));
411 static void reg_dead_at_p_1 PROTO((rtx, rtx, void *));
412 static int reg_dead_at_p PROTO((rtx, rtx));
413 static void move_deaths PROTO((rtx, rtx, int, rtx, rtx *));
414 static int reg_bitfield_target_p PROTO((rtx, rtx));
415 static void distribute_notes PROTO((rtx, rtx, rtx, rtx, rtx, rtx));
416 static void distribute_links PROTO((rtx));
417 static void mark_used_regs_combine PROTO((rtx));
418 static int insn_cuid PROTO((rtx));
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. */
426 static void
427 do_SUBST(into, newval)
428 rtx *into, newval;
430 struct undo *buf;
431 rtx oldval = *into;
433 if (oldval == newval)
434 return;
436 if (undobuf.frees)
437 buf = undobuf.frees, undobuf.frees = buf->next;
438 else
439 buf = (struct undo *) xmalloc (sizeof (struct undo));
441 buf->is_int = 0;
442 buf->where.r = into;
443 buf->old_contents.r = oldval;
444 *into = newval;
446 buf->next = undobuf.undos, undobuf.undos = buf;
449 #define SUBST(INTO, NEWVAL) do_SUBST(&(INTO), (NEWVAL))
451 /* Similar to SUBST, but NEWVAL is an int expression. Note that substitution
452 for the value of a HOST_WIDE_INT value (including CONST_INT) is
453 not safe. */
455 static void
456 do_SUBST_INT(into, newval)
457 int *into, newval;
459 struct undo *buf;
460 int oldval = *into;
462 if (oldval == newval)
463 return;
465 if (undobuf.frees)
466 buf = undobuf.frees, undobuf.frees = buf->next;
467 else
468 buf = (struct undo *) xmalloc (sizeof (struct undo));
470 buf->is_int = 1;
471 buf->where.i = into;
472 buf->old_contents.i = oldval;
473 *into = newval;
475 buf->next = undobuf.undos, undobuf.undos = buf;
478 #define SUBST_INT(INTO, NEWVAL) do_SUBST_INT(&(INTO), (NEWVAL))
480 /* Main entry point for combiner. F is the first insn of the function.
481 NREGS is the first unused pseudo-reg number. */
483 void
484 combine_instructions (f, nregs)
485 rtx f;
486 int nregs;
488 register rtx insn, next;
489 #ifdef HAVE_cc0
490 register rtx prev;
491 #endif
492 register int i;
493 register rtx links, nextlinks;
495 combine_attempts = 0;
496 combine_merges = 0;
497 combine_extras = 0;
498 combine_successes = 0;
500 combine_max_regno = nregs;
502 reg_nonzero_bits = ((unsigned HOST_WIDE_INT *)
503 xcalloc (nregs, sizeof (unsigned HOST_WIDE_INT)));
504 reg_sign_bit_copies = (char *) xcalloc (nregs, sizeof (char));
506 reg_last_death = (rtx *) xmalloc (nregs * sizeof (rtx));
507 reg_last_set = (rtx *) xmalloc (nregs * sizeof (rtx));
508 reg_last_set_value = (rtx *) xmalloc (nregs * sizeof (rtx));
509 reg_last_set_table_tick = (int *) xmalloc (nregs * sizeof (int));
510 reg_last_set_label = (int *) xmalloc (nregs * sizeof (int));
511 reg_last_set_invalid = (char *) xmalloc (nregs * sizeof (char));
512 reg_last_set_mode
513 = (enum machine_mode *) xmalloc (nregs * sizeof (enum machine_mode));
514 reg_last_set_nonzero_bits
515 = (unsigned HOST_WIDE_INT *) xmalloc (nregs * sizeof (HOST_WIDE_INT));
516 reg_last_set_sign_bit_copies
517 = (char *) xmalloc (nregs * sizeof (char));
519 init_reg_last_arrays ();
521 init_recog_no_volatile ();
523 /* Compute maximum uid value so uid_cuid can be allocated. */
525 for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
526 if (INSN_UID (insn) > i)
527 i = INSN_UID (insn);
529 uid_cuid = (int *) xmalloc ((i + 1) * sizeof (int));
530 max_uid_cuid = i;
532 nonzero_bits_mode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
534 /* Don't use reg_nonzero_bits when computing it. This can cause problems
535 when, for example, we have j <<= 1 in a loop. */
537 nonzero_sign_valid = 0;
539 /* Compute the mapping from uids to cuids.
540 Cuids are numbers assigned to insns, like uids,
541 except that cuids increase monotonically through the code.
543 Scan all SETs and see if we can deduce anything about what
544 bits are known to be zero for some registers and how many copies
545 of the sign bit are known to exist for those registers.
547 Also set any known values so that we can use it while searching
548 for what bits are known to be set. */
550 label_tick = 1;
552 /* We need to initialize it here, because record_dead_and_set_regs may call
553 get_last_value. */
554 subst_prev_insn = NULL_RTX;
556 setup_incoming_promotions ();
558 refresh_blocks = sbitmap_alloc (n_basic_blocks);
559 sbitmap_zero (refresh_blocks);
560 need_refresh = 0;
562 for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
564 uid_cuid[INSN_UID (insn)] = ++i;
565 subst_low_cuid = i;
566 subst_insn = insn;
568 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
570 note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies,
571 NULL);
572 record_dead_and_set_regs (insn);
574 #ifdef AUTO_INC_DEC
575 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
576 if (REG_NOTE_KIND (links) == REG_INC)
577 set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX,
578 NULL);
579 #endif
582 if (GET_CODE (insn) == CODE_LABEL)
583 label_tick++;
586 nonzero_sign_valid = 1;
588 /* Now scan all the insns in forward order. */
590 this_basic_block = -1;
591 label_tick = 1;
592 last_call_cuid = 0;
593 mem_last_set = 0;
594 init_reg_last_arrays ();
595 setup_incoming_promotions ();
597 for (insn = f; insn; insn = next ? next : NEXT_INSN (insn))
599 next = 0;
601 /* If INSN starts a new basic block, update our basic block number. */
602 if (this_basic_block + 1 < n_basic_blocks
603 && BLOCK_HEAD (this_basic_block + 1) == insn)
604 this_basic_block++;
606 if (GET_CODE (insn) == CODE_LABEL)
607 label_tick++;
609 else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
611 /* Try this insn with each insn it links back to. */
613 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
614 if ((next = try_combine (insn, XEXP (links, 0), NULL_RTX)) != 0)
615 goto retry;
617 /* Try each sequence of three linked insns ending with this one. */
619 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
620 for (nextlinks = LOG_LINKS (XEXP (links, 0)); nextlinks;
621 nextlinks = XEXP (nextlinks, 1))
622 if ((next = try_combine (insn, XEXP (links, 0),
623 XEXP (nextlinks, 0))) != 0)
624 goto retry;
626 #ifdef HAVE_cc0
627 /* Try to combine a jump insn that uses CC0
628 with a preceding insn that sets CC0, and maybe with its
629 logical predecessor as well.
630 This is how we make decrement-and-branch insns.
631 We need this special code because data flow connections
632 via CC0 do not get entered in LOG_LINKS. */
634 if (GET_CODE (insn) == JUMP_INSN
635 && (prev = prev_nonnote_insn (insn)) != 0
636 && GET_CODE (prev) == INSN
637 && sets_cc0_p (PATTERN (prev)))
639 if ((next = try_combine (insn, prev, NULL_RTX)) != 0)
640 goto retry;
642 for (nextlinks = LOG_LINKS (prev); nextlinks;
643 nextlinks = XEXP (nextlinks, 1))
644 if ((next = try_combine (insn, prev,
645 XEXP (nextlinks, 0))) != 0)
646 goto retry;
649 /* Do the same for an insn that explicitly references CC0. */
650 if (GET_CODE (insn) == INSN
651 && (prev = prev_nonnote_insn (insn)) != 0
652 && GET_CODE (prev) == INSN
653 && sets_cc0_p (PATTERN (prev))
654 && GET_CODE (PATTERN (insn)) == SET
655 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
657 if ((next = try_combine (insn, prev, NULL_RTX)) != 0)
658 goto retry;
660 for (nextlinks = LOG_LINKS (prev); nextlinks;
661 nextlinks = XEXP (nextlinks, 1))
662 if ((next = try_combine (insn, prev,
663 XEXP (nextlinks, 0))) != 0)
664 goto retry;
667 /* Finally, see if any of the insns that this insn links to
668 explicitly references CC0. If so, try this insn, that insn,
669 and its predecessor if it sets CC0. */
670 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
671 if (GET_CODE (XEXP (links, 0)) == INSN
672 && GET_CODE (PATTERN (XEXP (links, 0))) == SET
673 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (XEXP (links, 0))))
674 && (prev = prev_nonnote_insn (XEXP (links, 0))) != 0
675 && GET_CODE (prev) == INSN
676 && sets_cc0_p (PATTERN (prev))
677 && (next = try_combine (insn, XEXP (links, 0), prev)) != 0)
678 goto retry;
679 #endif
681 /* Try combining an insn with two different insns whose results it
682 uses. */
683 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
684 for (nextlinks = XEXP (links, 1); nextlinks;
685 nextlinks = XEXP (nextlinks, 1))
686 if ((next = try_combine (insn, XEXP (links, 0),
687 XEXP (nextlinks, 0))) != 0)
688 goto retry;
690 if (GET_CODE (insn) != NOTE)
691 record_dead_and_set_regs (insn);
693 retry:
698 if (need_refresh)
700 compute_bb_for_insn (get_max_uid ());
701 update_life_info (refresh_blocks, UPDATE_LIFE_GLOBAL_RM_NOTES,
702 PROP_DEATH_NOTES);
705 /* Clean up. */
706 sbitmap_free (refresh_blocks);
707 free (reg_nonzero_bits);
708 free (reg_sign_bit_copies);
709 free (reg_last_death);
710 free (reg_last_set);
711 free (reg_last_set_value);
712 free (reg_last_set_table_tick);
713 free (reg_last_set_label);
714 free (reg_last_set_invalid);
715 free (reg_last_set_mode);
716 free (reg_last_set_nonzero_bits);
717 free (reg_last_set_sign_bit_copies);
718 free (uid_cuid);
721 struct undo *undo, *next;
722 for (undo = undobuf.frees; undo; undo = next)
724 next = undo->next;
725 free (undo);
727 undobuf.frees = 0;
730 total_attempts += combine_attempts;
731 total_merges += combine_merges;
732 total_extras += combine_extras;
733 total_successes += combine_successes;
735 nonzero_sign_valid = 0;
737 /* Make recognizer allow volatile MEMs again. */
738 init_recog ();
741 /* Wipe the reg_last_xxx arrays in preparation for another pass. */
743 static void
744 init_reg_last_arrays ()
746 int nregs = combine_max_regno;
748 bzero ((char *) reg_last_death, nregs * sizeof (rtx));
749 bzero ((char *) reg_last_set, nregs * sizeof (rtx));
750 bzero ((char *) reg_last_set_value, nregs * sizeof (rtx));
751 bzero ((char *) reg_last_set_table_tick, nregs * sizeof (int));
752 bzero ((char *) reg_last_set_label, nregs * sizeof (int));
753 bzero (reg_last_set_invalid, nregs * sizeof (char));
754 bzero ((char *) reg_last_set_mode, nregs * sizeof (enum machine_mode));
755 bzero ((char *) reg_last_set_nonzero_bits, nregs * sizeof (HOST_WIDE_INT));
756 bzero (reg_last_set_sign_bit_copies, nregs * sizeof (char));
759 /* Set up any promoted values for incoming argument registers. */
761 static void
762 setup_incoming_promotions ()
764 #ifdef PROMOTE_FUNCTION_ARGS
765 int regno;
766 rtx reg;
767 enum machine_mode mode;
768 int unsignedp;
769 rtx first = get_insns ();
771 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
772 if (FUNCTION_ARG_REGNO_P (regno)
773 && (reg = promoted_input_arg (regno, &mode, &unsignedp)) != 0)
775 record_value_for_reg
776 (reg, first, gen_rtx_fmt_e ((unsignedp ? ZERO_EXTEND
777 : SIGN_EXTEND),
778 GET_MODE (reg),
779 gen_rtx_CLOBBER (mode, const0_rtx)));
781 #endif
784 /* Called via note_stores. If X is a pseudo that is narrower than
785 HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
787 If we are setting only a portion of X and we can't figure out what
788 portion, assume all bits will be used since we don't know what will
789 be happening.
791 Similarly, set how many bits of X are known to be copies of the sign bit
792 at all locations in the function. This is the smallest number implied
793 by any set of X. */
795 static void
796 set_nonzero_bits_and_sign_copies (x, set, data)
797 rtx x;
798 rtx set;
799 void *data ATTRIBUTE_UNUSED;
801 int num;
803 if (GET_CODE (x) == REG
804 && REGNO (x) >= FIRST_PSEUDO_REGISTER
805 /* If this register is undefined at the start of the file, we can't
806 say what its contents were. */
807 && ! REGNO_REG_SET_P (BASIC_BLOCK (0)->global_live_at_start, REGNO (x))
808 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
810 if (set == 0 || GET_CODE (set) == CLOBBER)
812 reg_nonzero_bits[REGNO (x)] = GET_MODE_MASK (GET_MODE (x));
813 reg_sign_bit_copies[REGNO (x)] = 1;
814 return;
817 /* If this is a complex assignment, see if we can convert it into a
818 simple assignment. */
819 set = expand_field_assignment (set);
821 /* If this is a simple assignment, or we have a paradoxical SUBREG,
822 set what we know about X. */
824 if (SET_DEST (set) == x
825 || (GET_CODE (SET_DEST (set)) == SUBREG
826 && (GET_MODE_SIZE (GET_MODE (SET_DEST (set)))
827 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (set)))))
828 && SUBREG_REG (SET_DEST (set)) == x))
830 rtx src = SET_SRC (set);
832 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
833 /* If X is narrower than a word and SRC is a non-negative
834 constant that would appear negative in the mode of X,
835 sign-extend it for use in reg_nonzero_bits because some
836 machines (maybe most) will actually do the sign-extension
837 and this is the conservative approach.
839 ??? For 2.5, try to tighten up the MD files in this regard
840 instead of this kludge. */
842 if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
843 && GET_CODE (src) == CONST_INT
844 && INTVAL (src) > 0
845 && 0 != (INTVAL (src)
846 & ((HOST_WIDE_INT) 1
847 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
848 src = GEN_INT (INTVAL (src)
849 | ((HOST_WIDE_INT) (-1)
850 << GET_MODE_BITSIZE (GET_MODE (x))));
851 #endif
853 reg_nonzero_bits[REGNO (x)]
854 |= nonzero_bits (src, nonzero_bits_mode);
855 num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
856 if (reg_sign_bit_copies[REGNO (x)] == 0
857 || reg_sign_bit_copies[REGNO (x)] > num)
858 reg_sign_bit_copies[REGNO (x)] = num;
860 else
862 reg_nonzero_bits[REGNO (x)] = GET_MODE_MASK (GET_MODE (x));
863 reg_sign_bit_copies[REGNO (x)] = 1;
868 /* See if INSN can be combined into I3. PRED and SUCC are optionally
869 insns that were previously combined into I3 or that will be combined
870 into the merger of INSN and I3.
872 Return 0 if the combination is not allowed for any reason.
874 If the combination is allowed, *PDEST will be set to the single
875 destination of INSN and *PSRC to the single source, and this function
876 will return 1. */
878 static int
879 can_combine_p (insn, i3, pred, succ, pdest, psrc)
880 rtx insn;
881 rtx i3;
882 rtx pred ATTRIBUTE_UNUSED;
883 rtx succ;
884 rtx *pdest, *psrc;
886 int i;
887 rtx set = 0, src, dest;
888 rtx p;
889 #ifdef AUTO_INC_DEC
890 rtx link;
891 #endif
892 int all_adjacent = (succ ? (next_active_insn (insn) == succ
893 && next_active_insn (succ) == i3)
894 : next_active_insn (insn) == i3);
896 /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
897 or a PARALLEL consisting of such a SET and CLOBBERs.
899 If INSN has CLOBBER parallel parts, ignore them for our processing.
900 By definition, these happen during the execution of the insn. When it
901 is merged with another insn, all bets are off. If they are, in fact,
902 needed and aren't also supplied in I3, they may be added by
903 recog_for_combine. Otherwise, it won't match.
905 We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
906 note.
908 Get the source and destination of INSN. If more than one, can't
909 combine. */
911 if (GET_CODE (PATTERN (insn)) == SET)
912 set = PATTERN (insn);
913 else if (GET_CODE (PATTERN (insn)) == PARALLEL
914 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
916 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
918 rtx elt = XVECEXP (PATTERN (insn), 0, i);
920 switch (GET_CODE (elt))
922 /* This is important to combine floating point insns
923 for the SH4 port. */
924 case USE:
925 /* Combining an isolated USE doesn't make sense.
926 We depend here on combinable_i3_pat to reject them. */
927 /* The code below this loop only verifies that the inputs of
928 the SET in INSN do not change. We call reg_set_between_p
929 to verify that the REG in the USE does not change betweeen
930 I3 and INSN.
931 If the USE in INSN was for a pseudo register, the matching
932 insn pattern will likely match any register; combining this
933 with any other USE would only be safe if we knew that the
934 used registers have identical values, or if there was
935 something to tell them apart, e.g. different modes. For
936 now, we forgo such compilcated tests and simply disallow
937 combining of USES of pseudo registers with any other USE. */
938 if (GET_CODE (XEXP (elt, 0)) == REG
939 && GET_CODE (PATTERN (i3)) == PARALLEL)
941 rtx i3pat = PATTERN (i3);
942 int i = XVECLEN (i3pat, 0) - 1;
943 int regno = REGNO (XEXP (elt, 0));
946 rtx i3elt = XVECEXP (i3pat, 0, i);
947 if (GET_CODE (i3elt) == USE
948 && GET_CODE (XEXP (i3elt, 0)) == REG
949 && (REGNO (XEXP (i3elt, 0)) == regno
950 ? reg_set_between_p (XEXP (elt, 0),
951 PREV_INSN (insn), i3)
952 : regno >= FIRST_PSEUDO_REGISTER))
953 return 0;
955 while (--i >= 0);
957 break;
959 /* We can ignore CLOBBERs. */
960 case CLOBBER:
961 break;
963 case SET:
964 /* Ignore SETs whose result isn't used but not those that
965 have side-effects. */
966 if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
967 && ! side_effects_p (elt))
968 break;
970 /* If we have already found a SET, this is a second one and
971 so we cannot combine with this insn. */
972 if (set)
973 return 0;
975 set = elt;
976 break;
978 default:
979 /* Anything else means we can't combine. */
980 return 0;
984 if (set == 0
985 /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
986 so don't do anything with it. */
987 || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
988 return 0;
990 else
991 return 0;
993 if (set == 0)
994 return 0;
996 set = expand_field_assignment (set);
997 src = SET_SRC (set), dest = SET_DEST (set);
999 /* Don't eliminate a store in the stack pointer. */
1000 if (dest == stack_pointer_rtx
1001 /* If we couldn't eliminate a field assignment, we can't combine. */
1002 || GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == STRICT_LOW_PART
1003 /* Don't combine with an insn that sets a register to itself if it has
1004 a REG_EQUAL note. This may be part of a REG_NO_CONFLICT sequence. */
1005 || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1006 /* Can't merge a function call. */
1007 || GET_CODE (src) == CALL
1008 /* Don't eliminate a function call argument. */
1009 || (GET_CODE (i3) == CALL_INSN
1010 && (find_reg_fusage (i3, USE, dest)
1011 || (GET_CODE (dest) == REG
1012 && REGNO (dest) < FIRST_PSEUDO_REGISTER
1013 && global_regs[REGNO (dest)])))
1014 /* Don't substitute into an incremented register. */
1015 || FIND_REG_INC_NOTE (i3, dest)
1016 || (succ && FIND_REG_INC_NOTE (succ, dest))
1017 #if 0
1018 /* Don't combine the end of a libcall into anything. */
1019 /* ??? This gives worse code, and appears to be unnecessary, since no
1020 pass after flow uses REG_LIBCALL/REG_RETVAL notes. Local-alloc does
1021 use REG_RETVAL notes for noconflict blocks, but other code here
1022 makes sure that those insns don't disappear. */
1023 || find_reg_note (insn, REG_RETVAL, NULL_RTX)
1024 #endif
1025 /* Make sure that DEST is not used after SUCC but before I3. */
1026 || (succ && ! all_adjacent
1027 && reg_used_between_p (dest, succ, i3))
1028 /* Make sure that the value that is to be substituted for the register
1029 does not use any registers whose values alter in between. However,
1030 If the insns are adjacent, a use can't cross a set even though we
1031 think it might (this can happen for a sequence of insns each setting
1032 the same destination; reg_last_set of that register might point to
1033 a NOTE). If INSN has a REG_EQUIV note, the register is always
1034 equivalent to the memory so the substitution is valid even if there
1035 are intervening stores. Also, don't move a volatile asm or
1036 UNSPEC_VOLATILE across any other insns. */
1037 || (! all_adjacent
1038 && (((GET_CODE (src) != MEM
1039 || ! find_reg_note (insn, REG_EQUIV, src))
1040 && use_crosses_set_p (src, INSN_CUID (insn)))
1041 || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
1042 || GET_CODE (src) == UNSPEC_VOLATILE))
1043 /* If there is a REG_NO_CONFLICT note for DEST in I3 or SUCC, we get
1044 better register allocation by not doing the combine. */
1045 || find_reg_note (i3, REG_NO_CONFLICT, dest)
1046 || (succ && find_reg_note (succ, REG_NO_CONFLICT, dest))
1047 /* Don't combine across a CALL_INSN, because that would possibly
1048 change whether the life span of some REGs crosses calls or not,
1049 and it is a pain to update that information.
1050 Exception: if source is a constant, moving it later can't hurt.
1051 Accept that special case, because it helps -fforce-addr a lot. */
1052 || (INSN_CUID (insn) < last_call_cuid && ! CONSTANT_P (src)))
1053 return 0;
1055 /* DEST must either be a REG or CC0. */
1056 if (GET_CODE (dest) == REG)
1058 /* If register alignment is being enforced for multi-word items in all
1059 cases except for parameters, it is possible to have a register copy
1060 insn referencing a hard register that is not allowed to contain the
1061 mode being copied and which would not be valid as an operand of most
1062 insns. Eliminate this problem by not combining with such an insn.
1064 Also, on some machines we don't want to extend the life of a hard
1065 register.
1067 This is the same test done in can_combine except that we don't test
1068 if SRC is a CALL operation to permit a hard register with
1069 SMALL_REGISTER_CLASSES, and that we have to take all_adjacent
1070 into account. */
1072 if (GET_CODE (src) == REG
1073 && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
1074 && ! HARD_REGNO_MODE_OK (REGNO (dest), GET_MODE (dest)))
1075 /* Don't extend the life of a hard register unless it is
1076 user variable (if we have few registers) or it can't
1077 fit into the desired register (meaning something special
1078 is going on).
1079 Also avoid substituting a return register into I3, because
1080 reload can't handle a conflict with constraints of other
1081 inputs. */
1082 || (REGNO (src) < FIRST_PSEUDO_REGISTER
1083 && (! HARD_REGNO_MODE_OK (REGNO (src), GET_MODE (src))
1084 || (SMALL_REGISTER_CLASSES
1085 && ((! all_adjacent && ! REG_USERVAR_P (src))
1086 || (FUNCTION_VALUE_REGNO_P (REGNO (src))
1087 && ! REG_USERVAR_P (src))))))))
1088 return 0;
1090 else if (GET_CODE (dest) != CC0)
1091 return 0;
1093 /* Don't substitute for a register intended as a clobberable operand.
1094 Similarly, don't substitute an expression containing a register that
1095 will be clobbered in I3. */
1096 if (GET_CODE (PATTERN (i3)) == PARALLEL)
1097 for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
1098 if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER
1099 && (reg_overlap_mentioned_p (XEXP (XVECEXP (PATTERN (i3), 0, i), 0),
1100 src)
1101 || rtx_equal_p (XEXP (XVECEXP (PATTERN (i3), 0, i), 0), dest)))
1102 return 0;
1104 /* If INSN contains anything volatile, or is an `asm' (whether volatile
1105 or not), reject, unless nothing volatile comes between it and I3 */
1107 if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
1109 /* Make sure succ doesn't contain a volatile reference. */
1110 if (succ != 0 && volatile_refs_p (PATTERN (succ)))
1111 return 0;
1113 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1114 if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
1115 && p != succ && volatile_refs_p (PATTERN (p)))
1116 return 0;
1119 /* If INSN is an asm, and DEST is a hard register, reject, since it has
1120 to be an explicit register variable, and was chosen for a reason. */
1122 if (GET_CODE (src) == ASM_OPERANDS
1123 && GET_CODE (dest) == REG && REGNO (dest) < FIRST_PSEUDO_REGISTER)
1124 return 0;
1126 /* If there are any volatile insns between INSN and I3, reject, because
1127 they might affect machine state. */
1129 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1130 if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
1131 && p != succ && volatile_insn_p (PATTERN (p)))
1132 return 0;
1134 /* If INSN or I2 contains an autoincrement or autodecrement,
1135 make sure that register is not used between there and I3,
1136 and not already used in I3 either.
1137 Also insist that I3 not be a jump; if it were one
1138 and the incremented register were spilled, we would lose. */
1140 #ifdef AUTO_INC_DEC
1141 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1142 if (REG_NOTE_KIND (link) == REG_INC
1143 && (GET_CODE (i3) == JUMP_INSN
1144 || reg_used_between_p (XEXP (link, 0), insn, i3)
1145 || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
1146 return 0;
1147 #endif
1149 #ifdef HAVE_cc0
1150 /* Don't combine an insn that follows a CC0-setting insn.
1151 An insn that uses CC0 must not be separated from the one that sets it.
1152 We do, however, allow I2 to follow a CC0-setting insn if that insn
1153 is passed as I1; in that case it will be deleted also.
1154 We also allow combining in this case if all the insns are adjacent
1155 because that would leave the two CC0 insns adjacent as well.
1156 It would be more logical to test whether CC0 occurs inside I1 or I2,
1157 but that would be much slower, and this ought to be equivalent. */
1159 p = prev_nonnote_insn (insn);
1160 if (p && p != pred && GET_CODE (p) == INSN && sets_cc0_p (PATTERN (p))
1161 && ! all_adjacent)
1162 return 0;
1163 #endif
1165 /* If we get here, we have passed all the tests and the combination is
1166 to be allowed. */
1168 *pdest = dest;
1169 *psrc = src;
1171 return 1;
1174 /* Check if PAT is an insn - or a part of it - used to set up an
1175 argument for a function in a hard register. */
1177 static int
1178 sets_function_arg_p (pat)
1179 rtx pat;
1181 int i;
1182 rtx inner_dest;
1184 switch (GET_CODE (pat))
1186 case INSN:
1187 return sets_function_arg_p (PATTERN (pat));
1189 case PARALLEL:
1190 for (i = XVECLEN (pat, 0); --i >= 0;)
1191 if (sets_function_arg_p (XVECEXP (pat, 0, i)))
1192 return 1;
1194 break;
1196 case SET:
1197 inner_dest = SET_DEST (pat);
1198 while (GET_CODE (inner_dest) == STRICT_LOW_PART
1199 || GET_CODE (inner_dest) == SUBREG
1200 || GET_CODE (inner_dest) == ZERO_EXTRACT)
1201 inner_dest = XEXP (inner_dest, 0);
1203 return (GET_CODE (inner_dest) == REG
1204 && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
1205 && FUNCTION_ARG_REGNO_P (REGNO (inner_dest)));
1207 default:
1208 break;
1211 return 0;
1214 /* LOC is the location within I3 that contains its pattern or the component
1215 of a PARALLEL of the pattern. We validate that it is valid for combining.
1217 One problem is if I3 modifies its output, as opposed to replacing it
1218 entirely, we can't allow the output to contain I2DEST or I1DEST as doing
1219 so would produce an insn that is not equivalent to the original insns.
1221 Consider:
1223 (set (reg:DI 101) (reg:DI 100))
1224 (set (subreg:SI (reg:DI 101) 0) <foo>)
1226 This is NOT equivalent to:
1228 (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
1229 (set (reg:DI 101) (reg:DI 100))])
1231 Not only does this modify 100 (in which case it might still be valid
1232 if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
1234 We can also run into a problem if I2 sets a register that I1
1235 uses and I1 gets directly substituted into I3 (not via I2). In that
1236 case, we would be getting the wrong value of I2DEST into I3, so we
1237 must reject the combination. This case occurs when I2 and I1 both
1238 feed into I3, rather than when I1 feeds into I2, which feeds into I3.
1239 If I1_NOT_IN_SRC is non-zero, it means that finding I1 in the source
1240 of a SET must prevent combination from occurring.
1242 On machines where SMALL_REGISTER_CLASSES is non-zero, we don't combine
1243 if the destination of a SET is a hard register that isn't a user
1244 variable.
1246 Before doing the above check, we first try to expand a field assignment
1247 into a set of logical operations.
1249 If PI3_DEST_KILLED is non-zero, it is a pointer to a location in which
1250 we place a register that is both set and used within I3. If more than one
1251 such register is detected, we fail.
1253 Return 1 if the combination is valid, zero otherwise. */
1255 static int
1256 combinable_i3pat (i3, loc, i2dest, i1dest, i1_not_in_src, pi3dest_killed)
1257 rtx i3;
1258 rtx *loc;
1259 rtx i2dest;
1260 rtx i1dest;
1261 int i1_not_in_src;
1262 rtx *pi3dest_killed;
1264 rtx x = *loc;
1266 if (GET_CODE (x) == SET)
1268 rtx set = expand_field_assignment (x);
1269 rtx dest = SET_DEST (set);
1270 rtx src = SET_SRC (set);
1271 rtx inner_dest = dest;
1273 #if 0
1274 rtx inner_src = src;
1275 #endif
1277 SUBST (*loc, set);
1279 while (GET_CODE (inner_dest) == STRICT_LOW_PART
1280 || GET_CODE (inner_dest) == SUBREG
1281 || GET_CODE (inner_dest) == ZERO_EXTRACT)
1282 inner_dest = XEXP (inner_dest, 0);
1284 /* We probably don't need this any more now that LIMIT_RELOAD_CLASS
1285 was added. */
1286 #if 0
1287 while (GET_CODE (inner_src) == STRICT_LOW_PART
1288 || GET_CODE (inner_src) == SUBREG
1289 || GET_CODE (inner_src) == ZERO_EXTRACT)
1290 inner_src = XEXP (inner_src, 0);
1292 /* If it is better that two different modes keep two different pseudos,
1293 avoid combining them. This avoids producing the following pattern
1294 on a 386:
1295 (set (subreg:SI (reg/v:QI 21) 0)
1296 (lshiftrt:SI (reg/v:SI 20)
1297 (const_int 24)))
1298 If that were made, reload could not handle the pair of
1299 reg 20/21, since it would try to get any GENERAL_REGS
1300 but some of them don't handle QImode. */
1302 if (rtx_equal_p (inner_src, i2dest)
1303 && GET_CODE (inner_dest) == REG
1304 && ! MODES_TIEABLE_P (GET_MODE (i2dest), GET_MODE (inner_dest)))
1305 return 0;
1306 #endif
1308 /* Check for the case where I3 modifies its output, as
1309 discussed above. */
1310 if ((inner_dest != dest
1311 && (reg_overlap_mentioned_p (i2dest, inner_dest)
1312 || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))))
1314 /* This is the same test done in can_combine_p except that we
1315 allow a hard register with SMALL_REGISTER_CLASSES if SRC is a
1316 CALL operation. Moreover, we can't test all_adjacent; we don't
1317 have to, since this instruction will stay in place, thus we are
1318 not considering increasing the lifetime of INNER_DEST.
1320 Also, if this insn sets a function argument, combining it with
1321 something that might need a spill could clobber a previous
1322 function argument; the all_adjacent test in can_combine_p also
1323 checks this; here, we do a more specific test for this case. */
1325 || (GET_CODE (inner_dest) == REG
1326 && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
1327 && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
1328 GET_MODE (inner_dest))
1329 || (SMALL_REGISTER_CLASSES && GET_CODE (src) != CALL
1330 && ! REG_USERVAR_P (inner_dest)
1331 && (FUNCTION_VALUE_REGNO_P (REGNO (inner_dest))
1332 || (FUNCTION_ARG_REGNO_P (REGNO (inner_dest))
1333 && i3 != 0
1334 && sets_function_arg_p (prev_nonnote_insn (i3)))))))
1335 || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src)))
1336 return 0;
1338 /* If DEST is used in I3, it is being killed in this insn,
1339 so record that for later.
1340 Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
1341 STACK_POINTER_REGNUM, since these are always considered to be
1342 live. Similarly for ARG_POINTER_REGNUM if it is fixed. */
1343 if (pi3dest_killed && GET_CODE (dest) == REG
1344 && reg_referenced_p (dest, PATTERN (i3))
1345 && REGNO (dest) != FRAME_POINTER_REGNUM
1346 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
1347 && REGNO (dest) != HARD_FRAME_POINTER_REGNUM
1348 #endif
1349 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
1350 && (REGNO (dest) != ARG_POINTER_REGNUM
1351 || ! fixed_regs [REGNO (dest)])
1352 #endif
1353 && REGNO (dest) != STACK_POINTER_REGNUM)
1355 if (*pi3dest_killed)
1356 return 0;
1358 *pi3dest_killed = dest;
1362 else if (GET_CODE (x) == PARALLEL)
1364 int i;
1366 for (i = 0; i < XVECLEN (x, 0); i++)
1367 if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest,
1368 i1_not_in_src, pi3dest_killed))
1369 return 0;
1372 return 1;
1375 /* Return 1 if X is an arithmetic expression that contains a multiplication
1376 and division. We don't count multiplications by powers of two here. */
1378 static int
1379 contains_muldiv (x)
1380 rtx x;
1382 switch (GET_CODE (x))
1384 case MOD: case DIV: case UMOD: case UDIV:
1385 return 1;
1387 case MULT:
1388 return ! (GET_CODE (XEXP (x, 1)) == CONST_INT
1389 && exact_log2 (INTVAL (XEXP (x, 1))) >= 0);
1390 default:
1391 switch (GET_RTX_CLASS (GET_CODE (x)))
1393 case 'c': case '<': case '2':
1394 return contains_muldiv (XEXP (x, 0))
1395 || contains_muldiv (XEXP (x, 1));
1397 case '1':
1398 return contains_muldiv (XEXP (x, 0));
1400 default:
1401 return 0;
1406 /* Try to combine the insns I1 and I2 into I3.
1407 Here I1 and I2 appear earlier than I3.
1408 I1 can be zero; then we combine just I2 into I3.
1410 It we are combining three insns and the resulting insn is not recognized,
1411 try splitting it into two insns. If that happens, I2 and I3 are retained
1412 and I1 is pseudo-deleted by turning it into a NOTE. Otherwise, I1 and I2
1413 are pseudo-deleted.
1415 Return 0 if the combination does not work. Then nothing is changed.
1416 If we did the combination, return the insn at which combine should
1417 resume scanning. */
1419 static rtx
1420 try_combine (i3, i2, i1)
1421 register rtx i3, i2, i1;
1423 /* New patterns for I3 and I3, respectively. */
1424 rtx newpat, newi2pat = 0;
1425 /* Indicates need to preserve SET in I1 or I2 in I3 if it is not dead. */
1426 int added_sets_1, added_sets_2;
1427 /* Total number of SETs to put into I3. */
1428 int total_sets;
1429 /* Nonzero is I2's body now appears in I3. */
1430 int i2_is_used;
1431 /* INSN_CODEs for new I3, new I2, and user of condition code. */
1432 int insn_code_number, i2_code_number = 0, other_code_number = 0;
1433 /* Contains I3 if the destination of I3 is used in its source, which means
1434 that the old life of I3 is being killed. If that usage is placed into
1435 I2 and not in I3, a REG_DEAD note must be made. */
1436 rtx i3dest_killed = 0;
1437 /* SET_DEST and SET_SRC of I2 and I1. */
1438 rtx i2dest, i2src, i1dest = 0, i1src = 0;
1439 /* PATTERN (I2), or a copy of it in certain cases. */
1440 rtx i2pat;
1441 /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC. */
1442 int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
1443 int i1_feeds_i3 = 0;
1444 /* Notes that must be added to REG_NOTES in I3 and I2. */
1445 rtx new_i3_notes, new_i2_notes;
1446 /* Notes that we substituted I3 into I2 instead of the normal case. */
1447 int i3_subst_into_i2 = 0;
1448 /* Notes that I1, I2 or I3 is a MULT operation. */
1449 int have_mult = 0;
1451 int maxreg;
1452 rtx temp;
1453 register rtx link;
1454 int i;
1456 /* If any of I1, I2, and I3 isn't really an insn, we can't do anything.
1457 This can occur when flow deletes an insn that it has merged into an
1458 auto-increment address. We also can't do anything if I3 has a
1459 REG_LIBCALL note since we don't want to disrupt the contiguity of a
1460 libcall. */
1462 if (GET_RTX_CLASS (GET_CODE (i3)) != 'i'
1463 || GET_RTX_CLASS (GET_CODE (i2)) != 'i'
1464 || (i1 && GET_RTX_CLASS (GET_CODE (i1)) != 'i')
1465 #if 0
1466 /* ??? This gives worse code, and appears to be unnecessary, since no
1467 pass after flow uses REG_LIBCALL/REG_RETVAL notes. */
1468 || find_reg_note (i3, REG_LIBCALL, NULL_RTX)
1469 #endif
1471 return 0;
1473 combine_attempts++;
1474 undobuf.other_insn = 0;
1476 /* Save the current high-water-mark so we can free storage if we didn't
1477 accept this combination. */
1478 undobuf.storage = (char *) oballoc (0);
1480 /* Reset the hard register usage information. */
1481 CLEAR_HARD_REG_SET (newpat_used_regs);
1483 /* If I1 and I2 both feed I3, they can be in any order. To simplify the
1484 code below, set I1 to be the earlier of the two insns. */
1485 if (i1 && INSN_CUID (i1) > INSN_CUID (i2))
1486 temp = i1, i1 = i2, i2 = temp;
1488 added_links_insn = 0;
1490 /* First check for one important special-case that the code below will
1491 not handle. Namely, the case where I1 is zero, I2 has multiple sets,
1492 and I3 is a SET whose SET_SRC is a SET_DEST in I2. In that case,
1493 we may be able to replace that destination with the destination of I3.
1494 This occurs in the common code where we compute both a quotient and
1495 remainder into a structure, in which case we want to do the computation
1496 directly into the structure to avoid register-register copies.
1498 We make very conservative checks below and only try to handle the
1499 most common cases of this. For example, we only handle the case
1500 where I2 and I3 are adjacent to avoid making difficult register
1501 usage tests. */
1503 if (i1 == 0 && GET_CODE (i3) == INSN && GET_CODE (PATTERN (i3)) == SET
1504 && GET_CODE (SET_SRC (PATTERN (i3))) == REG
1505 && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
1506 && (! SMALL_REGISTER_CLASSES
1507 || (GET_CODE (SET_DEST (PATTERN (i3))) != REG
1508 || REGNO (SET_DEST (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
1509 || REG_USERVAR_P (SET_DEST (PATTERN (i3)))))
1510 && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
1511 && GET_CODE (PATTERN (i2)) == PARALLEL
1512 && ! side_effects_p (SET_DEST (PATTERN (i3)))
1513 /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
1514 below would need to check what is inside (and reg_overlap_mentioned_p
1515 doesn't support those codes anyway). Don't allow those destinations;
1516 the resulting insn isn't likely to be recognized anyway. */
1517 && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
1518 && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
1519 && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
1520 SET_DEST (PATTERN (i3)))
1521 && next_real_insn (i2) == i3)
1523 rtx p2 = PATTERN (i2);
1525 /* Make sure that the destination of I3,
1526 which we are going to substitute into one output of I2,
1527 is not used within another output of I2. We must avoid making this:
1528 (parallel [(set (mem (reg 69)) ...)
1529 (set (reg 69) ...)])
1530 which is not well-defined as to order of actions.
1531 (Besides, reload can't handle output reloads for this.)
1533 The problem can also happen if the dest of I3 is a memory ref,
1534 if another dest in I2 is an indirect memory ref. */
1535 for (i = 0; i < XVECLEN (p2, 0); i++)
1536 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
1537 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
1538 && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
1539 SET_DEST (XVECEXP (p2, 0, i))))
1540 break;
1542 if (i == XVECLEN (p2, 0))
1543 for (i = 0; i < XVECLEN (p2, 0); i++)
1544 if (SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
1546 combine_merges++;
1548 subst_insn = i3;
1549 subst_low_cuid = INSN_CUID (i2);
1551 added_sets_2 = added_sets_1 = 0;
1552 i2dest = SET_SRC (PATTERN (i3));
1554 /* Replace the dest in I2 with our dest and make the resulting
1555 insn the new pattern for I3. Then skip to where we
1556 validate the pattern. Everything was set up above. */
1557 SUBST (SET_DEST (XVECEXP (p2, 0, i)),
1558 SET_DEST (PATTERN (i3)));
1560 newpat = p2;
1561 i3_subst_into_i2 = 1;
1562 goto validate_replacement;
1566 #ifndef HAVE_cc0
1567 /* If we have no I1 and I2 looks like:
1568 (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
1569 (set Y OP)])
1570 make up a dummy I1 that is
1571 (set Y OP)
1572 and change I2 to be
1573 (set (reg:CC X) (compare:CC Y (const_int 0)))
1575 (We can ignore any trailing CLOBBERs.)
1577 This undoes a previous combination and allows us to match a branch-and-
1578 decrement insn. */
1580 if (i1 == 0 && GET_CODE (PATTERN (i2)) == PARALLEL
1581 && XVECLEN (PATTERN (i2), 0) >= 2
1582 && GET_CODE (XVECEXP (PATTERN (i2), 0, 0)) == SET
1583 && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
1584 == MODE_CC)
1585 && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
1586 && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
1587 && GET_CODE (XVECEXP (PATTERN (i2), 0, 1)) == SET
1588 && GET_CODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 1))) == REG
1589 && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
1590 SET_SRC (XVECEXP (PATTERN (i2), 0, 1))))
1592 for (i = XVECLEN (PATTERN (i2), 0) - 1; i >= 2; i--)
1593 if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != CLOBBER)
1594 break;
1596 if (i == 1)
1598 /* We make I1 with the same INSN_UID as I2. This gives it
1599 the same INSN_CUID for value tracking. Our fake I1 will
1600 never appear in the insn stream so giving it the same INSN_UID
1601 as I2 will not cause a problem. */
1603 subst_prev_insn = i1
1604 = gen_rtx_INSN (VOIDmode, INSN_UID (i2), NULL_RTX, i2,
1605 XVECEXP (PATTERN (i2), 0, 1), -1, NULL_RTX,
1606 NULL_RTX);
1608 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
1609 SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
1610 SET_DEST (PATTERN (i1)));
1613 #endif
1615 /* Verify that I2 and I1 are valid for combining. */
1616 if (! can_combine_p (i2, i3, i1, NULL_RTX, &i2dest, &i2src)
1617 || (i1 && ! can_combine_p (i1, i3, NULL_RTX, i2, &i1dest, &i1src)))
1619 undo_all ();
1620 return 0;
1623 /* Record whether I2DEST is used in I2SRC and similarly for the other
1624 cases. Knowing this will help in register status updating below. */
1625 i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
1626 i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
1627 i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
1629 /* See if I1 directly feeds into I3. It does if I1DEST is not used
1630 in I2SRC. */
1631 i1_feeds_i3 = i1 && ! reg_overlap_mentioned_p (i1dest, i2src);
1633 /* Ensure that I3's pattern can be the destination of combines. */
1634 if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest,
1635 i1 && i2dest_in_i1src && i1_feeds_i3,
1636 &i3dest_killed))
1638 undo_all ();
1639 return 0;
1642 /* See if any of the insns is a MULT operation. Unless one is, we will
1643 reject a combination that is, since it must be slower. Be conservative
1644 here. */
1645 if (GET_CODE (i2src) == MULT
1646 || (i1 != 0 && GET_CODE (i1src) == MULT)
1647 || (GET_CODE (PATTERN (i3)) == SET
1648 && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
1649 have_mult = 1;
1651 /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
1652 We used to do this EXCEPT in one case: I3 has a post-inc in an
1653 output operand. However, that exception can give rise to insns like
1654 mov r3,(r3)+
1655 which is a famous insn on the PDP-11 where the value of r3 used as the
1656 source was model-dependent. Avoid this sort of thing. */
1658 #if 0
1659 if (!(GET_CODE (PATTERN (i3)) == SET
1660 && GET_CODE (SET_SRC (PATTERN (i3))) == REG
1661 && GET_CODE (SET_DEST (PATTERN (i3))) == MEM
1662 && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
1663 || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
1664 /* It's not the exception. */
1665 #endif
1666 #ifdef AUTO_INC_DEC
1667 for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
1668 if (REG_NOTE_KIND (link) == REG_INC
1669 && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
1670 || (i1 != 0
1671 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
1673 undo_all ();
1674 return 0;
1676 #endif
1678 /* See if the SETs in I1 or I2 need to be kept around in the merged
1679 instruction: whenever the value set there is still needed past I3.
1680 For the SETs in I2, this is easy: we see if I2DEST dies or is set in I3.
1682 For the SET in I1, we have two cases: If I1 and I2 independently
1683 feed into I3, the set in I1 needs to be kept around if I1DEST dies
1684 or is set in I3. Otherwise (if I1 feeds I2 which feeds I3), the set
1685 in I1 needs to be kept around unless I1DEST dies or is set in either
1686 I2 or I3. We can distinguish these cases by seeing if I2SRC mentions
1687 I1DEST. If so, we know I1 feeds into I2. */
1689 added_sets_2 = ! dead_or_set_p (i3, i2dest);
1691 added_sets_1
1692 = i1 && ! (i1_feeds_i3 ? dead_or_set_p (i3, i1dest)
1693 : (dead_or_set_p (i3, i1dest) || dead_or_set_p (i2, i1dest)));
1695 /* If the set in I2 needs to be kept around, we must make a copy of
1696 PATTERN (I2), so that when we substitute I1SRC for I1DEST in
1697 PATTERN (I2), we are only substituting for the original I1DEST, not into
1698 an already-substituted copy. This also prevents making self-referential
1699 rtx. If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
1700 I2DEST. */
1702 i2pat = (GET_CODE (PATTERN (i2)) == PARALLEL
1703 ? gen_rtx_SET (VOIDmode, i2dest, i2src)
1704 : PATTERN (i2));
1706 if (added_sets_2)
1707 i2pat = copy_rtx (i2pat);
1709 combine_merges++;
1711 /* Substitute in the latest insn for the regs set by the earlier ones. */
1713 maxreg = max_reg_num ();
1715 subst_insn = i3;
1717 /* It is possible that the source of I2 or I1 may be performing an
1718 unneeded operation, such as a ZERO_EXTEND of something that is known
1719 to have the high part zero. Handle that case by letting subst look at
1720 the innermost one of them.
1722 Another way to do this would be to have a function that tries to
1723 simplify a single insn instead of merging two or more insns. We don't
1724 do this because of the potential of infinite loops and because
1725 of the potential extra memory required. However, doing it the way
1726 we are is a bit of a kludge and doesn't catch all cases.
1728 But only do this if -fexpensive-optimizations since it slows things down
1729 and doesn't usually win. */
1731 if (flag_expensive_optimizations)
1733 /* Pass pc_rtx so no substitutions are done, just simplifications.
1734 The cases that we are interested in here do not involve the few
1735 cases were is_replaced is checked. */
1736 if (i1)
1738 subst_low_cuid = INSN_CUID (i1);
1739 i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0);
1741 else
1743 subst_low_cuid = INSN_CUID (i2);
1744 i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0);
1747 undobuf.previous_undos = undobuf.undos;
1750 #ifndef HAVE_cc0
1751 /* Many machines that don't use CC0 have insns that can both perform an
1752 arithmetic operation and set the condition code. These operations will
1753 be represented as a PARALLEL with the first element of the vector
1754 being a COMPARE of an arithmetic operation with the constant zero.
1755 The second element of the vector will set some pseudo to the result
1756 of the same arithmetic operation. If we simplify the COMPARE, we won't
1757 match such a pattern and so will generate an extra insn. Here we test
1758 for this case, where both the comparison and the operation result are
1759 needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
1760 I2SRC. Later we will make the PARALLEL that contains I2. */
1762 if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
1763 && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
1764 && XEXP (SET_SRC (PATTERN (i3)), 1) == const0_rtx
1765 && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
1767 #ifdef EXTRA_CC_MODES
1768 rtx *cc_use;
1769 enum machine_mode compare_mode;
1770 #endif
1772 newpat = PATTERN (i3);
1773 SUBST (XEXP (SET_SRC (newpat), 0), i2src);
1775 i2_is_used = 1;
1777 #ifdef EXTRA_CC_MODES
1778 /* See if a COMPARE with the operand we substituted in should be done
1779 with the mode that is currently being used. If not, do the same
1780 processing we do in `subst' for a SET; namely, if the destination
1781 is used only once, try to replace it with a register of the proper
1782 mode and also replace the COMPARE. */
1783 if (undobuf.other_insn == 0
1784 && (cc_use = find_single_use (SET_DEST (newpat), i3,
1785 &undobuf.other_insn))
1786 && ((compare_mode = SELECT_CC_MODE (GET_CODE (*cc_use),
1787 i2src, const0_rtx))
1788 != GET_MODE (SET_DEST (newpat))))
1790 int regno = REGNO (SET_DEST (newpat));
1791 rtx new_dest = gen_rtx_REG (compare_mode, regno);
1793 if (regno < FIRST_PSEUDO_REGISTER
1794 || (REG_N_SETS (regno) == 1 && ! added_sets_2
1795 && ! REG_USERVAR_P (SET_DEST (newpat))))
1797 if (regno >= FIRST_PSEUDO_REGISTER)
1798 SUBST (regno_reg_rtx[regno], new_dest);
1800 SUBST (SET_DEST (newpat), new_dest);
1801 SUBST (XEXP (*cc_use, 0), new_dest);
1802 SUBST (SET_SRC (newpat),
1803 gen_rtx_combine (COMPARE, compare_mode,
1804 i2src, const0_rtx));
1806 else
1807 undobuf.other_insn = 0;
1809 #endif
1811 else
1812 #endif
1814 n_occurrences = 0; /* `subst' counts here */
1816 /* If I1 feeds into I2 (not into I3) and I1DEST is in I1SRC, we
1817 need to make a unique copy of I2SRC each time we substitute it
1818 to avoid self-referential rtl. */
1820 subst_low_cuid = INSN_CUID (i2);
1821 newpat = subst (PATTERN (i3), i2dest, i2src, 0,
1822 ! i1_feeds_i3 && i1dest_in_i1src);
1823 undobuf.previous_undos = undobuf.undos;
1825 /* Record whether i2's body now appears within i3's body. */
1826 i2_is_used = n_occurrences;
1829 /* If we already got a failure, don't try to do more. Otherwise,
1830 try to substitute in I1 if we have it. */
1832 if (i1 && GET_CODE (newpat) != CLOBBER)
1834 /* Before we can do this substitution, we must redo the test done
1835 above (see detailed comments there) that ensures that I1DEST
1836 isn't mentioned in any SETs in NEWPAT that are field assignments. */
1838 if (! combinable_i3pat (NULL_RTX, &newpat, i1dest, NULL_RTX,
1839 0, NULL_PTR))
1841 undo_all ();
1842 return 0;
1845 n_occurrences = 0;
1846 subst_low_cuid = INSN_CUID (i1);
1847 newpat = subst (newpat, i1dest, i1src, 0, 0);
1848 undobuf.previous_undos = undobuf.undos;
1851 /* Fail if an autoincrement side-effect has been duplicated. Be careful
1852 to count all the ways that I2SRC and I1SRC can be used. */
1853 if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
1854 && i2_is_used + added_sets_2 > 1)
1855 || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
1856 && (n_occurrences + added_sets_1 + (added_sets_2 && ! i1_feeds_i3)
1857 > 1))
1858 /* Fail if we tried to make a new register (we used to abort, but there's
1859 really no reason to). */
1860 || max_reg_num () != maxreg
1861 /* Fail if we couldn't do something and have a CLOBBER. */
1862 || GET_CODE (newpat) == CLOBBER
1863 /* Fail if this new pattern is a MULT and we didn't have one before
1864 at the outer level. */
1865 || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
1866 && ! have_mult))
1868 undo_all ();
1869 return 0;
1872 /* If the actions of the earlier insns must be kept
1873 in addition to substituting them into the latest one,
1874 we must make a new PARALLEL for the latest insn
1875 to hold additional the SETs. */
1877 if (added_sets_1 || added_sets_2)
1879 combine_extras++;
1881 if (GET_CODE (newpat) == PARALLEL)
1883 rtvec old = XVEC (newpat, 0);
1884 total_sets = XVECLEN (newpat, 0) + added_sets_1 + added_sets_2;
1885 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
1886 bcopy ((char *) &old->elem[0], (char *) XVEC (newpat, 0)->elem,
1887 sizeof (old->elem[0]) * old->num_elem);
1889 else
1891 rtx old = newpat;
1892 total_sets = 1 + added_sets_1 + added_sets_2;
1893 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
1894 XVECEXP (newpat, 0, 0) = old;
1897 if (added_sets_1)
1898 XVECEXP (newpat, 0, --total_sets)
1899 = (GET_CODE (PATTERN (i1)) == PARALLEL
1900 ? gen_rtx_SET (VOIDmode, i1dest, i1src) : PATTERN (i1));
1902 if (added_sets_2)
1904 /* If there is no I1, use I2's body as is. We used to also not do
1905 the subst call below if I2 was substituted into I3,
1906 but that could lose a simplification. */
1907 if (i1 == 0)
1908 XVECEXP (newpat, 0, --total_sets) = i2pat;
1909 else
1910 /* See comment where i2pat is assigned. */
1911 XVECEXP (newpat, 0, --total_sets)
1912 = subst (i2pat, i1dest, i1src, 0, 0);
1916 /* We come here when we are replacing a destination in I2 with the
1917 destination of I3. */
1918 validate_replacement:
1920 /* Note which hard regs this insn has as inputs. */
1921 mark_used_regs_combine (newpat);
1923 /* Is the result of combination a valid instruction? */
1924 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
1926 /* If the result isn't valid, see if it is a PARALLEL of two SETs where
1927 the second SET's destination is a register that is unused. In that case,
1928 we just need the first SET. This can occur when simplifying a divmod
1929 insn. We *must* test for this case here because the code below that
1930 splits two independent SETs doesn't handle this case correctly when it
1931 updates the register status. Also check the case where the first
1932 SET's destination is unused. That would not cause incorrect code, but
1933 does cause an unneeded insn to remain. */
1935 if (insn_code_number < 0 && GET_CODE (newpat) == PARALLEL
1936 && XVECLEN (newpat, 0) == 2
1937 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
1938 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
1939 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == REG
1940 && find_reg_note (i3, REG_UNUSED, SET_DEST (XVECEXP (newpat, 0, 1)))
1941 && ! side_effects_p (SET_SRC (XVECEXP (newpat, 0, 1)))
1942 && asm_noperands (newpat) < 0)
1944 newpat = XVECEXP (newpat, 0, 0);
1945 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
1948 else if (insn_code_number < 0 && GET_CODE (newpat) == PARALLEL
1949 && XVECLEN (newpat, 0) == 2
1950 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
1951 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
1952 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) == REG
1953 && find_reg_note (i3, REG_UNUSED, SET_DEST (XVECEXP (newpat, 0, 0)))
1954 && ! side_effects_p (SET_SRC (XVECEXP (newpat, 0, 0)))
1955 && asm_noperands (newpat) < 0)
1957 newpat = XVECEXP (newpat, 0, 1);
1958 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
1961 /* If we were combining three insns and the result is a simple SET
1962 with no ASM_OPERANDS that wasn't recognized, try to split it into two
1963 insns. There are two ways to do this. It can be split using a
1964 machine-specific method (like when you have an addition of a large
1965 constant) or by combine in the function find_split_point. */
1967 if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
1968 && asm_noperands (newpat) < 0)
1970 rtx m_split, *split;
1971 rtx ni2dest = i2dest;
1973 /* See if the MD file can split NEWPAT. If it can't, see if letting it
1974 use I2DEST as a scratch register will help. In the latter case,
1975 convert I2DEST to the mode of the source of NEWPAT if we can. */
1977 m_split = split_insns (newpat, i3);
1979 /* We can only use I2DEST as a scratch reg if it doesn't overlap any
1980 inputs of NEWPAT. */
1982 /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
1983 possible to try that as a scratch reg. This would require adding
1984 more code to make it work though. */
1986 if (m_split == 0 && ! reg_overlap_mentioned_p (ni2dest, newpat))
1988 /* If I2DEST is a hard register or the only use of a pseudo,
1989 we can change its mode. */
1990 if (GET_MODE (SET_DEST (newpat)) != GET_MODE (i2dest)
1991 && GET_MODE (SET_DEST (newpat)) != VOIDmode
1992 && GET_CODE (i2dest) == REG
1993 && (REGNO (i2dest) < FIRST_PSEUDO_REGISTER
1994 || (REG_N_SETS (REGNO (i2dest)) == 1 && ! added_sets_2
1995 && ! REG_USERVAR_P (i2dest))))
1996 ni2dest = gen_rtx_REG (GET_MODE (SET_DEST (newpat)),
1997 REGNO (i2dest));
1999 m_split = split_insns (gen_rtx_PARALLEL
2000 (VOIDmode,
2001 gen_rtvec (2, newpat,
2002 gen_rtx_CLOBBER (VOIDmode,
2003 ni2dest))),
2004 i3);
2007 if (m_split && GET_CODE (m_split) == SEQUENCE
2008 && XVECLEN (m_split, 0) == 2
2009 && (next_real_insn (i2) == i3
2010 || ! use_crosses_set_p (PATTERN (XVECEXP (m_split, 0, 0)),
2011 INSN_CUID (i2))))
2013 rtx i2set, i3set;
2014 rtx newi3pat = PATTERN (XVECEXP (m_split, 0, 1));
2015 newi2pat = PATTERN (XVECEXP (m_split, 0, 0));
2017 i3set = single_set (XVECEXP (m_split, 0, 1));
2018 i2set = single_set (XVECEXP (m_split, 0, 0));
2020 /* In case we changed the mode of I2DEST, replace it in the
2021 pseudo-register table here. We can't do it above in case this
2022 code doesn't get executed and we do a split the other way. */
2024 if (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
2025 SUBST (regno_reg_rtx[REGNO (i2dest)], ni2dest);
2027 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2029 /* If I2 or I3 has multiple SETs, we won't know how to track
2030 register status, so don't use these insns. If I2's destination
2031 is used between I2 and I3, we also can't use these insns. */
2033 if (i2_code_number >= 0 && i2set && i3set
2034 && (next_real_insn (i2) == i3
2035 || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
2036 insn_code_number = recog_for_combine (&newi3pat, i3,
2037 &new_i3_notes);
2038 if (insn_code_number >= 0)
2039 newpat = newi3pat;
2041 /* It is possible that both insns now set the destination of I3.
2042 If so, we must show an extra use of it. */
2044 if (insn_code_number >= 0)
2046 rtx new_i3_dest = SET_DEST (i3set);
2047 rtx new_i2_dest = SET_DEST (i2set);
2049 while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
2050 || GET_CODE (new_i3_dest) == STRICT_LOW_PART
2051 || GET_CODE (new_i3_dest) == SUBREG)
2052 new_i3_dest = XEXP (new_i3_dest, 0);
2054 while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
2055 || GET_CODE (new_i2_dest) == STRICT_LOW_PART
2056 || GET_CODE (new_i2_dest) == SUBREG)
2057 new_i2_dest = XEXP (new_i2_dest, 0);
2059 if (GET_CODE (new_i3_dest) == REG
2060 && GET_CODE (new_i2_dest) == REG
2061 && REGNO (new_i3_dest) == REGNO (new_i2_dest))
2062 REG_N_SETS (REGNO (new_i2_dest))++;
2066 /* If we can split it and use I2DEST, go ahead and see if that
2067 helps things be recognized. Verify that none of the registers
2068 are set between I2 and I3. */
2069 if (insn_code_number < 0 && (split = find_split_point (&newpat, i3)) != 0
2070 #ifdef HAVE_cc0
2071 && GET_CODE (i2dest) == REG
2072 #endif
2073 /* We need I2DEST in the proper mode. If it is a hard register
2074 or the only use of a pseudo, we can change its mode. */
2075 && (GET_MODE (*split) == GET_MODE (i2dest)
2076 || GET_MODE (*split) == VOIDmode
2077 || REGNO (i2dest) < FIRST_PSEUDO_REGISTER
2078 || (REG_N_SETS (REGNO (i2dest)) == 1 && ! added_sets_2
2079 && ! REG_USERVAR_P (i2dest)))
2080 && (next_real_insn (i2) == i3
2081 || ! use_crosses_set_p (*split, INSN_CUID (i2)))
2082 /* We can't overwrite I2DEST if its value is still used by
2083 NEWPAT. */
2084 && ! reg_referenced_p (i2dest, newpat))
2086 rtx newdest = i2dest;
2087 enum rtx_code split_code = GET_CODE (*split);
2088 enum machine_mode split_mode = GET_MODE (*split);
2090 /* Get NEWDEST as a register in the proper mode. We have already
2091 validated that we can do this. */
2092 if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
2094 newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
2096 if (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
2097 SUBST (regno_reg_rtx[REGNO (i2dest)], newdest);
2100 /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
2101 an ASHIFT. This can occur if it was inside a PLUS and hence
2102 appeared to be a memory address. This is a kludge. */
2103 if (split_code == MULT
2104 && GET_CODE (XEXP (*split, 1)) == CONST_INT
2105 && (i = exact_log2 (INTVAL (XEXP (*split, 1)))) >= 0)
2107 SUBST (*split, gen_rtx_combine (ASHIFT, split_mode,
2108 XEXP (*split, 0), GEN_INT (i)));
2109 /* Update split_code because we may not have a multiply
2110 anymore. */
2111 split_code = GET_CODE (*split);
2114 #ifdef INSN_SCHEDULING
2115 /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
2116 be written as a ZERO_EXTEND. */
2117 if (split_code == SUBREG && GET_CODE (SUBREG_REG (*split)) == MEM)
2118 SUBST (*split, gen_rtx_combine (ZERO_EXTEND, split_mode,
2119 XEXP (*split, 0)));
2120 #endif
2122 newi2pat = gen_rtx_combine (SET, VOIDmode, newdest, *split);
2123 SUBST (*split, newdest);
2124 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2126 /* If the split point was a MULT and we didn't have one before,
2127 don't use one now. */
2128 if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
2129 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2133 /* Check for a case where we loaded from memory in a narrow mode and
2134 then sign extended it, but we need both registers. In that case,
2135 we have a PARALLEL with both loads from the same memory location.
2136 We can split this into a load from memory followed by a register-register
2137 copy. This saves at least one insn, more if register allocation can
2138 eliminate the copy.
2140 We cannot do this if the destination of the second assignment is
2141 a register that we have already assumed is zero-extended. Similarly
2142 for a SUBREG of such a register. */
2144 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
2145 && GET_CODE (newpat) == PARALLEL
2146 && XVECLEN (newpat, 0) == 2
2147 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2148 && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
2149 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2150 && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2151 XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
2152 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2153 INSN_CUID (i2))
2154 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
2155 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
2156 && ! (temp = SET_DEST (XVECEXP (newpat, 0, 1)),
2157 (GET_CODE (temp) == REG
2158 && reg_nonzero_bits[REGNO (temp)] != 0
2159 && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
2160 && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
2161 && (reg_nonzero_bits[REGNO (temp)]
2162 != GET_MODE_MASK (word_mode))))
2163 && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
2164 && (temp = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
2165 (GET_CODE (temp) == REG
2166 && reg_nonzero_bits[REGNO (temp)] != 0
2167 && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
2168 && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
2169 && (reg_nonzero_bits[REGNO (temp)]
2170 != GET_MODE_MASK (word_mode)))))
2171 && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
2172 SET_SRC (XVECEXP (newpat, 0, 1)))
2173 && ! find_reg_note (i3, REG_UNUSED,
2174 SET_DEST (XVECEXP (newpat, 0, 0))))
2176 rtx ni2dest;
2178 newi2pat = XVECEXP (newpat, 0, 0);
2179 ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
2180 newpat = XVECEXP (newpat, 0, 1);
2181 SUBST (SET_SRC (newpat),
2182 gen_lowpart_for_combine (GET_MODE (SET_SRC (newpat)), ni2dest));
2183 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2185 if (i2_code_number >= 0)
2186 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2188 if (insn_code_number >= 0)
2190 rtx insn;
2191 rtx link;
2193 /* If we will be able to accept this, we have made a change to the
2194 destination of I3. This can invalidate a LOG_LINKS pointing
2195 to I3. No other part of combine.c makes such a transformation.
2197 The new I3 will have a destination that was previously the
2198 destination of I1 or I2 and which was used in i2 or I3. Call
2199 distribute_links to make a LOG_LINK from the next use of
2200 that destination. */
2202 PATTERN (i3) = newpat;
2203 distribute_links (gen_rtx_INSN_LIST (VOIDmode, i3, NULL_RTX));
2205 /* I3 now uses what used to be its destination and which is
2206 now I2's destination. That means we need a LOG_LINK from
2207 I3 to I2. But we used to have one, so we still will.
2209 However, some later insn might be using I2's dest and have
2210 a LOG_LINK pointing at I3. We must remove this link.
2211 The simplest way to remove the link is to point it at I1,
2212 which we know will be a NOTE. */
2214 for (insn = NEXT_INSN (i3);
2215 insn && (this_basic_block == n_basic_blocks - 1
2216 || insn != BLOCK_HEAD (this_basic_block + 1));
2217 insn = NEXT_INSN (insn))
2219 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
2220 && reg_referenced_p (ni2dest, PATTERN (insn)))
2222 for (link = LOG_LINKS (insn); link;
2223 link = XEXP (link, 1))
2224 if (XEXP (link, 0) == i3)
2225 XEXP (link, 0) = i1;
2227 break;
2233 /* Similarly, check for a case where we have a PARALLEL of two independent
2234 SETs but we started with three insns. In this case, we can do the sets
2235 as two separate insns. This case occurs when some SET allows two
2236 other insns to combine, but the destination of that SET is still live. */
2238 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
2239 && GET_CODE (newpat) == PARALLEL
2240 && XVECLEN (newpat, 0) == 2
2241 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2242 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
2243 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
2244 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2245 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
2246 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
2247 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2248 INSN_CUID (i2))
2249 /* Don't pass sets with (USE (MEM ...)) dests to the following. */
2250 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != USE
2251 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != USE
2252 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
2253 XVECEXP (newpat, 0, 0))
2254 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
2255 XVECEXP (newpat, 0, 1))
2256 && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
2257 && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1)))))
2259 /* Normally, it doesn't matter which of the two is done first,
2260 but it does if one references cc0. In that case, it has to
2261 be first. */
2262 #ifdef HAVE_cc0
2263 if (reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 0)))
2265 newi2pat = XVECEXP (newpat, 0, 0);
2266 newpat = XVECEXP (newpat, 0, 1);
2268 else
2269 #endif
2271 newi2pat = XVECEXP (newpat, 0, 1);
2272 newpat = XVECEXP (newpat, 0, 0);
2275 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2277 if (i2_code_number >= 0)
2278 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2281 /* If it still isn't recognized, fail and change things back the way they
2282 were. */
2283 if ((insn_code_number < 0
2284 /* Is the result a reasonable ASM_OPERANDS? */
2285 && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
2287 undo_all ();
2288 return 0;
2291 /* If we had to change another insn, make sure it is valid also. */
2292 if (undobuf.other_insn)
2294 rtx other_pat = PATTERN (undobuf.other_insn);
2295 rtx new_other_notes;
2296 rtx note, next;
2298 CLEAR_HARD_REG_SET (newpat_used_regs);
2300 other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
2301 &new_other_notes);
2303 if (other_code_number < 0 && ! check_asm_operands (other_pat))
2305 undo_all ();
2306 return 0;
2309 PATTERN (undobuf.other_insn) = other_pat;
2311 /* If any of the notes in OTHER_INSN were REG_UNUSED, ensure that they
2312 are still valid. Then add any non-duplicate notes added by
2313 recog_for_combine. */
2314 for (note = REG_NOTES (undobuf.other_insn); note; note = next)
2316 next = XEXP (note, 1);
2318 if (REG_NOTE_KIND (note) == REG_UNUSED
2319 && ! reg_set_p (XEXP (note, 0), PATTERN (undobuf.other_insn)))
2321 if (GET_CODE (XEXP (note, 0)) == REG)
2322 REG_N_DEATHS (REGNO (XEXP (note, 0)))--;
2324 remove_note (undobuf.other_insn, note);
2328 for (note = new_other_notes; note; note = XEXP (note, 1))
2329 if (GET_CODE (XEXP (note, 0)) == REG)
2330 REG_N_DEATHS (REGNO (XEXP (note, 0)))++;
2332 distribute_notes (new_other_notes, undobuf.other_insn,
2333 undobuf.other_insn, NULL_RTX, NULL_RTX, NULL_RTX);
2336 /* We now know that we can do this combination. Merge the insns and
2337 update the status of registers and LOG_LINKS. */
2340 rtx i3notes, i2notes, i1notes = 0;
2341 rtx i3links, i2links, i1links = 0;
2342 rtx midnotes = 0;
2343 register int regno;
2344 /* Compute which registers we expect to eliminate. newi2pat may be setting
2345 either i3dest or i2dest, so we must check it. Also, i1dest may be the
2346 same as i3dest, in which case newi2pat may be setting i1dest. */
2347 rtx elim_i2 = ((newi2pat && reg_set_p (i2dest, newi2pat))
2348 || i2dest_in_i2src || i2dest_in_i1src
2349 ? 0 : i2dest);
2350 rtx elim_i1 = (i1 == 0 || i1dest_in_i1src
2351 || (newi2pat && reg_set_p (i1dest, newi2pat))
2352 ? 0 : i1dest);
2354 /* Get the old REG_NOTES and LOG_LINKS from all our insns and
2355 clear them. */
2356 i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
2357 i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
2358 if (i1)
2359 i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
2361 /* Ensure that we do not have something that should not be shared but
2362 occurs multiple times in the new insns. Check this by first
2363 resetting all the `used' flags and then copying anything is shared. */
2365 reset_used_flags (i3notes);
2366 reset_used_flags (i2notes);
2367 reset_used_flags (i1notes);
2368 reset_used_flags (newpat);
2369 reset_used_flags (newi2pat);
2370 if (undobuf.other_insn)
2371 reset_used_flags (PATTERN (undobuf.other_insn));
2373 i3notes = copy_rtx_if_shared (i3notes);
2374 i2notes = copy_rtx_if_shared (i2notes);
2375 i1notes = copy_rtx_if_shared (i1notes);
2376 newpat = copy_rtx_if_shared (newpat);
2377 newi2pat = copy_rtx_if_shared (newi2pat);
2378 if (undobuf.other_insn)
2379 reset_used_flags (PATTERN (undobuf.other_insn));
2381 INSN_CODE (i3) = insn_code_number;
2382 PATTERN (i3) = newpat;
2383 if (undobuf.other_insn)
2384 INSN_CODE (undobuf.other_insn) = other_code_number;
2386 /* We had one special case above where I2 had more than one set and
2387 we replaced a destination of one of those sets with the destination
2388 of I3. In that case, we have to update LOG_LINKS of insns later
2389 in this basic block. Note that this (expensive) case is rare.
2391 Also, in this case, we must pretend that all REG_NOTEs for I2
2392 actually came from I3, so that REG_UNUSED notes from I2 will be
2393 properly handled. */
2395 if (i3_subst_into_i2)
2397 for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
2398 if (GET_CODE (SET_DEST (XVECEXP (PATTERN (i2), 0, i))) == REG
2399 && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
2400 && ! find_reg_note (i2, REG_UNUSED,
2401 SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
2402 for (temp = NEXT_INSN (i2);
2403 temp && (this_basic_block == n_basic_blocks - 1
2404 || BLOCK_HEAD (this_basic_block) != temp);
2405 temp = NEXT_INSN (temp))
2406 if (temp != i3 && GET_RTX_CLASS (GET_CODE (temp)) == 'i')
2407 for (link = LOG_LINKS (temp); link; link = XEXP (link, 1))
2408 if (XEXP (link, 0) == i2)
2409 XEXP (link, 0) = i3;
2411 if (i3notes)
2413 rtx link = i3notes;
2414 while (XEXP (link, 1))
2415 link = XEXP (link, 1);
2416 XEXP (link, 1) = i2notes;
2418 else
2419 i3notes = i2notes;
2420 i2notes = 0;
2423 LOG_LINKS (i3) = 0;
2424 REG_NOTES (i3) = 0;
2425 LOG_LINKS (i2) = 0;
2426 REG_NOTES (i2) = 0;
2428 if (newi2pat)
2430 INSN_CODE (i2) = i2_code_number;
2431 PATTERN (i2) = newi2pat;
2433 else
2435 PUT_CODE (i2, NOTE);
2436 NOTE_LINE_NUMBER (i2) = NOTE_INSN_DELETED;
2437 NOTE_SOURCE_FILE (i2) = 0;
2440 if (i1)
2442 LOG_LINKS (i1) = 0;
2443 REG_NOTES (i1) = 0;
2444 PUT_CODE (i1, NOTE);
2445 NOTE_LINE_NUMBER (i1) = NOTE_INSN_DELETED;
2446 NOTE_SOURCE_FILE (i1) = 0;
2449 /* Get death notes for everything that is now used in either I3 or
2450 I2 and used to die in a previous insn. If we built two new
2451 patterns, move from I1 to I2 then I2 to I3 so that we get the
2452 proper movement on registers that I2 modifies. */
2454 if (newi2pat)
2456 move_deaths (newi2pat, NULL_RTX, INSN_CUID (i1), i2, &midnotes);
2457 move_deaths (newpat, newi2pat, INSN_CUID (i1), i3, &midnotes);
2459 else
2460 move_deaths (newpat, NULL_RTX, i1 ? INSN_CUID (i1) : INSN_CUID (i2),
2461 i3, &midnotes);
2463 /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3. */
2464 if (i3notes)
2465 distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL_RTX,
2466 elim_i2, elim_i1);
2467 if (i2notes)
2468 distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL_RTX,
2469 elim_i2, elim_i1);
2470 if (i1notes)
2471 distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL_RTX,
2472 elim_i2, elim_i1);
2473 if (midnotes)
2474 distribute_notes (midnotes, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
2475 elim_i2, elim_i1);
2477 /* Distribute any notes added to I2 or I3 by recog_for_combine. We
2478 know these are REG_UNUSED and want them to go to the desired insn,
2479 so we always pass it as i3. We have not counted the notes in
2480 reg_n_deaths yet, so we need to do so now. */
2482 if (newi2pat && new_i2_notes)
2484 for (temp = new_i2_notes; temp; temp = XEXP (temp, 1))
2485 if (GET_CODE (XEXP (temp, 0)) == REG)
2486 REG_N_DEATHS (REGNO (XEXP (temp, 0)))++;
2488 distribute_notes (new_i2_notes, i2, i2, NULL_RTX, NULL_RTX, NULL_RTX);
2491 if (new_i3_notes)
2493 for (temp = new_i3_notes; temp; temp = XEXP (temp, 1))
2494 if (GET_CODE (XEXP (temp, 0)) == REG)
2495 REG_N_DEATHS (REGNO (XEXP (temp, 0)))++;
2497 distribute_notes (new_i3_notes, i3, i3, NULL_RTX, NULL_RTX, NULL_RTX);
2500 /* If I3DEST was used in I3SRC, it really died in I3. We may need to
2501 put a REG_DEAD note for it somewhere. If NEWI2PAT exists and sets
2502 I3DEST, the death must be somewhere before I2, not I3. If we passed I3
2503 in that case, it might delete I2. Similarly for I2 and I1.
2504 Show an additional death due to the REG_DEAD note we make here. If
2505 we discard it in distribute_notes, we will decrement it again. */
2507 if (i3dest_killed)
2509 if (GET_CODE (i3dest_killed) == REG)
2510 REG_N_DEATHS (REGNO (i3dest_killed))++;
2512 if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
2513 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i3dest_killed,
2514 NULL_RTX),
2515 NULL_RTX, i2, NULL_RTX, elim_i2, elim_i1);
2516 else
2517 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i3dest_killed,
2518 NULL_RTX),
2519 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
2520 elim_i2, elim_i1);
2523 if (i2dest_in_i2src)
2525 if (GET_CODE (i2dest) == REG)
2526 REG_N_DEATHS (REGNO (i2dest))++;
2528 if (newi2pat && reg_set_p (i2dest, newi2pat))
2529 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i2dest, NULL_RTX),
2530 NULL_RTX, i2, NULL_RTX, NULL_RTX, NULL_RTX);
2531 else
2532 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i2dest, NULL_RTX),
2533 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
2534 NULL_RTX, NULL_RTX);
2537 if (i1dest_in_i1src)
2539 if (GET_CODE (i1dest) == REG)
2540 REG_N_DEATHS (REGNO (i1dest))++;
2542 if (newi2pat && reg_set_p (i1dest, newi2pat))
2543 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i1dest, NULL_RTX),
2544 NULL_RTX, i2, NULL_RTX, NULL_RTX, NULL_RTX);
2545 else
2546 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i1dest, NULL_RTX),
2547 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
2548 NULL_RTX, NULL_RTX);
2551 distribute_links (i3links);
2552 distribute_links (i2links);
2553 distribute_links (i1links);
2555 if (GET_CODE (i2dest) == REG)
2557 rtx link;
2558 rtx i2_insn = 0, i2_val = 0, set;
2560 /* The insn that used to set this register doesn't exist, and
2561 this life of the register may not exist either. See if one of
2562 I3's links points to an insn that sets I2DEST. If it does,
2563 that is now the last known value for I2DEST. If we don't update
2564 this and I2 set the register to a value that depended on its old
2565 contents, we will get confused. If this insn is used, thing
2566 will be set correctly in combine_instructions. */
2568 for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
2569 if ((set = single_set (XEXP (link, 0))) != 0
2570 && rtx_equal_p (i2dest, SET_DEST (set)))
2571 i2_insn = XEXP (link, 0), i2_val = SET_SRC (set);
2573 record_value_for_reg (i2dest, i2_insn, i2_val);
2575 /* If the reg formerly set in I2 died only once and that was in I3,
2576 zero its use count so it won't make `reload' do any work. */
2577 if (! added_sets_2
2578 && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
2579 && ! i2dest_in_i2src)
2581 regno = REGNO (i2dest);
2582 REG_N_SETS (regno)--;
2583 if (REG_N_SETS (regno) == 0
2584 && ! REGNO_REG_SET_P (BASIC_BLOCK (0)->global_live_at_start,
2585 regno))
2586 REG_N_REFS (regno) = 0;
2590 if (i1 && GET_CODE (i1dest) == REG)
2592 rtx link;
2593 rtx i1_insn = 0, i1_val = 0, set;
2595 for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
2596 if ((set = single_set (XEXP (link, 0))) != 0
2597 && rtx_equal_p (i1dest, SET_DEST (set)))
2598 i1_insn = XEXP (link, 0), i1_val = SET_SRC (set);
2600 record_value_for_reg (i1dest, i1_insn, i1_val);
2602 regno = REGNO (i1dest);
2603 if (! added_sets_1 && ! i1dest_in_i1src)
2605 REG_N_SETS (regno)--;
2606 if (REG_N_SETS (regno) == 0
2607 && ! REGNO_REG_SET_P (BASIC_BLOCK (0)->global_live_at_start,
2608 regno))
2609 REG_N_REFS (regno) = 0;
2613 /* Update reg_nonzero_bits et al for any changes that may have been made
2614 to this insn. */
2616 note_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
2617 if (newi2pat)
2618 note_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
2620 /* If I3 is now an unconditional jump, ensure that it has a
2621 BARRIER following it since it may have initially been a
2622 conditional jump. It may also be the last nonnote insn. */
2624 if ((GET_CODE (newpat) == RETURN || simplejump_p (i3))
2625 && ((temp = next_nonnote_insn (i3)) == NULL_RTX
2626 || GET_CODE (temp) != BARRIER))
2627 emit_barrier_after (i3);
2630 combine_successes++;
2631 undo_commit ();
2633 /* Clear this here, so that subsequent get_last_value calls are not
2634 affected. */
2635 subst_prev_insn = NULL_RTX;
2637 if (added_links_insn
2638 && (newi2pat == 0 || INSN_CUID (added_links_insn) < INSN_CUID (i2))
2639 && INSN_CUID (added_links_insn) < INSN_CUID (i3))
2640 return added_links_insn;
2641 else
2642 return newi2pat ? i2 : i3;
2645 /* Undo all the modifications recorded in undobuf. */
2647 static void
2648 undo_all ()
2650 struct undo *undo, *next;
2652 for (undo = undobuf.undos; undo; undo = next)
2654 next = undo->next;
2655 if (undo->is_int)
2656 *undo->where.i = undo->old_contents.i;
2657 else
2658 *undo->where.r = undo->old_contents.r;
2660 undo->next = undobuf.frees;
2661 undobuf.frees = undo;
2664 obfree (undobuf.storage);
2665 undobuf.undos = undobuf.previous_undos = 0;
2667 /* Clear this here, so that subsequent get_last_value calls are not
2668 affected. */
2669 subst_prev_insn = NULL_RTX;
2672 /* We've committed to accepting the changes we made. Move all
2673 of the undos to the free list. */
2675 static void
2676 undo_commit ()
2678 struct undo *undo, *next;
2680 for (undo = undobuf.undos; undo; undo = next)
2682 next = undo->next;
2683 undo->next = undobuf.frees;
2684 undobuf.frees = undo;
2686 undobuf.undos = undobuf.previous_undos = 0;
2690 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
2691 where we have an arithmetic expression and return that point. LOC will
2692 be inside INSN.
2694 try_combine will call this function to see if an insn can be split into
2695 two insns. */
2697 static rtx *
2698 find_split_point (loc, insn)
2699 rtx *loc;
2700 rtx insn;
2702 rtx x = *loc;
2703 enum rtx_code code = GET_CODE (x);
2704 rtx *split;
2705 int len = 0, pos = 0, unsignedp = 0;
2706 rtx inner = NULL_RTX;
2708 /* First special-case some codes. */
2709 switch (code)
2711 case SUBREG:
2712 #ifdef INSN_SCHEDULING
2713 /* If we are making a paradoxical SUBREG invalid, it becomes a split
2714 point. */
2715 if (GET_CODE (SUBREG_REG (x)) == MEM)
2716 return loc;
2717 #endif
2718 return find_split_point (&SUBREG_REG (x), insn);
2720 case MEM:
2721 #ifdef HAVE_lo_sum
2722 /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
2723 using LO_SUM and HIGH. */
2724 if (GET_CODE (XEXP (x, 0)) == CONST
2725 || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
2727 SUBST (XEXP (x, 0),
2728 gen_rtx_combine (LO_SUM, Pmode,
2729 gen_rtx_combine (HIGH, Pmode, XEXP (x, 0)),
2730 XEXP (x, 0)));
2731 return &XEXP (XEXP (x, 0), 0);
2733 #endif
2735 /* If we have a PLUS whose second operand is a constant and the
2736 address is not valid, perhaps will can split it up using
2737 the machine-specific way to split large constants. We use
2738 the first pseudo-reg (one of the virtual regs) as a placeholder;
2739 it will not remain in the result. */
2740 if (GET_CODE (XEXP (x, 0)) == PLUS
2741 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
2742 && ! memory_address_p (GET_MODE (x), XEXP (x, 0)))
2744 rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
2745 rtx seq = split_insns (gen_rtx_SET (VOIDmode, reg, XEXP (x, 0)),
2746 subst_insn);
2748 /* This should have produced two insns, each of which sets our
2749 placeholder. If the source of the second is a valid address,
2750 we can make put both sources together and make a split point
2751 in the middle. */
2753 if (seq && XVECLEN (seq, 0) == 2
2754 && GET_CODE (XVECEXP (seq, 0, 0)) == INSN
2755 && GET_CODE (PATTERN (XVECEXP (seq, 0, 0))) == SET
2756 && SET_DEST (PATTERN (XVECEXP (seq, 0, 0))) == reg
2757 && ! reg_mentioned_p (reg,
2758 SET_SRC (PATTERN (XVECEXP (seq, 0, 0))))
2759 && GET_CODE (XVECEXP (seq, 0, 1)) == INSN
2760 && GET_CODE (PATTERN (XVECEXP (seq, 0, 1))) == SET
2761 && SET_DEST (PATTERN (XVECEXP (seq, 0, 1))) == reg
2762 && memory_address_p (GET_MODE (x),
2763 SET_SRC (PATTERN (XVECEXP (seq, 0, 1)))))
2765 rtx src1 = SET_SRC (PATTERN (XVECEXP (seq, 0, 0)));
2766 rtx src2 = SET_SRC (PATTERN (XVECEXP (seq, 0, 1)));
2768 /* Replace the placeholder in SRC2 with SRC1. If we can
2769 find where in SRC2 it was placed, that can become our
2770 split point and we can replace this address with SRC2.
2771 Just try two obvious places. */
2773 src2 = replace_rtx (src2, reg, src1);
2774 split = 0;
2775 if (XEXP (src2, 0) == src1)
2776 split = &XEXP (src2, 0);
2777 else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
2778 && XEXP (XEXP (src2, 0), 0) == src1)
2779 split = &XEXP (XEXP (src2, 0), 0);
2781 if (split)
2783 SUBST (XEXP (x, 0), src2);
2784 return split;
2788 /* If that didn't work, perhaps the first operand is complex and
2789 needs to be computed separately, so make a split point there.
2790 This will occur on machines that just support REG + CONST
2791 and have a constant moved through some previous computation. */
2793 else if (GET_RTX_CLASS (GET_CODE (XEXP (XEXP (x, 0), 0))) != 'o'
2794 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
2795 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (XEXP (x, 0), 0))))
2796 == 'o')))
2797 return &XEXP (XEXP (x, 0), 0);
2799 break;
2801 case SET:
2802 #ifdef HAVE_cc0
2803 /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
2804 ZERO_EXTRACT, the most likely reason why this doesn't match is that
2805 we need to put the operand into a register. So split at that
2806 point. */
2808 if (SET_DEST (x) == cc0_rtx
2809 && GET_CODE (SET_SRC (x)) != COMPARE
2810 && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
2811 && GET_RTX_CLASS (GET_CODE (SET_SRC (x))) != 'o'
2812 && ! (GET_CODE (SET_SRC (x)) == SUBREG
2813 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (SET_SRC (x)))) == 'o'))
2814 return &SET_SRC (x);
2815 #endif
2817 /* See if we can split SET_SRC as it stands. */
2818 split = find_split_point (&SET_SRC (x), insn);
2819 if (split && split != &SET_SRC (x))
2820 return split;
2822 /* See if we can split SET_DEST as it stands. */
2823 split = find_split_point (&SET_DEST (x), insn);
2824 if (split && split != &SET_DEST (x))
2825 return split;
2827 /* See if this is a bitfield assignment with everything constant. If
2828 so, this is an IOR of an AND, so split it into that. */
2829 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
2830 && (GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)))
2831 <= HOST_BITS_PER_WIDE_INT)
2832 && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT
2833 && GET_CODE (XEXP (SET_DEST (x), 2)) == CONST_INT
2834 && GET_CODE (SET_SRC (x)) == CONST_INT
2835 && ((INTVAL (XEXP (SET_DEST (x), 1))
2836 + INTVAL (XEXP (SET_DEST (x), 2)))
2837 <= GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0))))
2838 && ! side_effects_p (XEXP (SET_DEST (x), 0)))
2840 int pos = INTVAL (XEXP (SET_DEST (x), 2));
2841 int len = INTVAL (XEXP (SET_DEST (x), 1));
2842 int src = INTVAL (SET_SRC (x));
2843 rtx dest = XEXP (SET_DEST (x), 0);
2844 enum machine_mode mode = GET_MODE (dest);
2845 unsigned HOST_WIDE_INT mask = ((HOST_WIDE_INT) 1 << len) - 1;
2847 if (BITS_BIG_ENDIAN)
2848 pos = GET_MODE_BITSIZE (mode) - len - pos;
2850 if ((unsigned HOST_WIDE_INT) src == mask)
2851 SUBST (SET_SRC (x),
2852 gen_binary (IOR, mode, dest, GEN_INT (src << pos)));
2853 else
2854 SUBST (SET_SRC (x),
2855 gen_binary (IOR, mode,
2856 gen_binary (AND, mode, dest,
2857 GEN_INT (~ (mask << pos)
2858 & GET_MODE_MASK (mode))),
2859 GEN_INT (src << pos)));
2861 SUBST (SET_DEST (x), dest);
2863 split = find_split_point (&SET_SRC (x), insn);
2864 if (split && split != &SET_SRC (x))
2865 return split;
2868 /* Otherwise, see if this is an operation that we can split into two.
2869 If so, try to split that. */
2870 code = GET_CODE (SET_SRC (x));
2872 switch (code)
2874 case AND:
2875 /* If we are AND'ing with a large constant that is only a single
2876 bit and the result is only being used in a context where we
2877 need to know if it is zero or non-zero, replace it with a bit
2878 extraction. This will avoid the large constant, which might
2879 have taken more than one insn to make. If the constant were
2880 not a valid argument to the AND but took only one insn to make,
2881 this is no worse, but if it took more than one insn, it will
2882 be better. */
2884 if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
2885 && GET_CODE (XEXP (SET_SRC (x), 0)) == REG
2886 && (pos = exact_log2 (INTVAL (XEXP (SET_SRC (x), 1)))) >= 7
2887 && GET_CODE (SET_DEST (x)) == REG
2888 && (split = find_single_use (SET_DEST (x), insn, NULL_PTR)) != 0
2889 && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
2890 && XEXP (*split, 0) == SET_DEST (x)
2891 && XEXP (*split, 1) == const0_rtx)
2893 rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
2894 XEXP (SET_SRC (x), 0),
2895 pos, NULL_RTX, 1, 1, 0, 0);
2896 if (extraction != 0)
2898 SUBST (SET_SRC (x), extraction);
2899 return find_split_point (loc, insn);
2902 break;
2904 case NE:
2905 /* if STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
2906 is known to be on, this can be converted into a NEG of a shift. */
2907 if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
2908 && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
2909 && 1 <= (pos = exact_log2
2910 (nonzero_bits (XEXP (SET_SRC (x), 0),
2911 GET_MODE (XEXP (SET_SRC (x), 0))))))
2913 enum machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
2915 SUBST (SET_SRC (x),
2916 gen_rtx_combine (NEG, mode,
2917 gen_rtx_combine (LSHIFTRT, mode,
2918 XEXP (SET_SRC (x), 0),
2919 GEN_INT (pos))));
2921 split = find_split_point (&SET_SRC (x), insn);
2922 if (split && split != &SET_SRC (x))
2923 return split;
2925 break;
2927 case SIGN_EXTEND:
2928 inner = XEXP (SET_SRC (x), 0);
2930 /* We can't optimize if either mode is a partial integer
2931 mode as we don't know how many bits are significant
2932 in those modes. */
2933 if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
2934 || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
2935 break;
2937 pos = 0;
2938 len = GET_MODE_BITSIZE (GET_MODE (inner));
2939 unsignedp = 0;
2940 break;
2942 case SIGN_EXTRACT:
2943 case ZERO_EXTRACT:
2944 if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
2945 && GET_CODE (XEXP (SET_SRC (x), 2)) == CONST_INT)
2947 inner = XEXP (SET_SRC (x), 0);
2948 len = INTVAL (XEXP (SET_SRC (x), 1));
2949 pos = INTVAL (XEXP (SET_SRC (x), 2));
2951 if (BITS_BIG_ENDIAN)
2952 pos = GET_MODE_BITSIZE (GET_MODE (inner)) - len - pos;
2953 unsignedp = (code == ZERO_EXTRACT);
2955 break;
2957 default:
2958 break;
2961 if (len && pos >= 0 && pos + len <= GET_MODE_BITSIZE (GET_MODE (inner)))
2963 enum machine_mode mode = GET_MODE (SET_SRC (x));
2965 /* For unsigned, we have a choice of a shift followed by an
2966 AND or two shifts. Use two shifts for field sizes where the
2967 constant might be too large. We assume here that we can
2968 always at least get 8-bit constants in an AND insn, which is
2969 true for every current RISC. */
2971 if (unsignedp && len <= 8)
2973 SUBST (SET_SRC (x),
2974 gen_rtx_combine
2975 (AND, mode,
2976 gen_rtx_combine (LSHIFTRT, mode,
2977 gen_lowpart_for_combine (mode, inner),
2978 GEN_INT (pos)),
2979 GEN_INT (((HOST_WIDE_INT) 1 << len) - 1)));
2981 split = find_split_point (&SET_SRC (x), insn);
2982 if (split && split != &SET_SRC (x))
2983 return split;
2985 else
2987 SUBST (SET_SRC (x),
2988 gen_rtx_combine
2989 (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
2990 gen_rtx_combine (ASHIFT, mode,
2991 gen_lowpart_for_combine (mode, inner),
2992 GEN_INT (GET_MODE_BITSIZE (mode)
2993 - len - pos)),
2994 GEN_INT (GET_MODE_BITSIZE (mode) - len)));
2996 split = find_split_point (&SET_SRC (x), insn);
2997 if (split && split != &SET_SRC (x))
2998 return split;
3002 /* See if this is a simple operation with a constant as the second
3003 operand. It might be that this constant is out of range and hence
3004 could be used as a split point. */
3005 if ((GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '2'
3006 || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == 'c'
3007 || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '<')
3008 && CONSTANT_P (XEXP (SET_SRC (x), 1))
3009 && (GET_RTX_CLASS (GET_CODE (XEXP (SET_SRC (x), 0))) == 'o'
3010 || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
3011 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (SET_SRC (x), 0))))
3012 == 'o'))))
3013 return &XEXP (SET_SRC (x), 1);
3015 /* Finally, see if this is a simple operation with its first operand
3016 not in a register. The operation might require this operand in a
3017 register, so return it as a split point. We can always do this
3018 because if the first operand were another operation, we would have
3019 already found it as a split point. */
3020 if ((GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '2'
3021 || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == 'c'
3022 || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '<'
3023 || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '1')
3024 && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
3025 return &XEXP (SET_SRC (x), 0);
3027 return 0;
3029 case AND:
3030 case IOR:
3031 /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
3032 it is better to write this as (not (ior A B)) so we can split it.
3033 Similarly for IOR. */
3034 if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
3036 SUBST (*loc,
3037 gen_rtx_combine (NOT, GET_MODE (x),
3038 gen_rtx_combine (code == IOR ? AND : IOR,
3039 GET_MODE (x),
3040 XEXP (XEXP (x, 0), 0),
3041 XEXP (XEXP (x, 1), 0))));
3042 return find_split_point (loc, insn);
3045 /* Many RISC machines have a large set of logical insns. If the
3046 second operand is a NOT, put it first so we will try to split the
3047 other operand first. */
3048 if (GET_CODE (XEXP (x, 1)) == NOT)
3050 rtx tem = XEXP (x, 0);
3051 SUBST (XEXP (x, 0), XEXP (x, 1));
3052 SUBST (XEXP (x, 1), tem);
3054 break;
3056 default:
3057 break;
3060 /* Otherwise, select our actions depending on our rtx class. */
3061 switch (GET_RTX_CLASS (code))
3063 case 'b': /* This is ZERO_EXTRACT and SIGN_EXTRACT. */
3064 case '3':
3065 split = find_split_point (&XEXP (x, 2), insn);
3066 if (split)
3067 return split;
3068 /* ... fall through ... */
3069 case '2':
3070 case 'c':
3071 case '<':
3072 split = find_split_point (&XEXP (x, 1), insn);
3073 if (split)
3074 return split;
3075 /* ... fall through ... */
3076 case '1':
3077 /* Some machines have (and (shift ...) ...) insns. If X is not
3078 an AND, but XEXP (X, 0) is, use it as our split point. */
3079 if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
3080 return &XEXP (x, 0);
3082 split = find_split_point (&XEXP (x, 0), insn);
3083 if (split)
3084 return split;
3085 return loc;
3088 /* Otherwise, we don't have a split point. */
3089 return 0;
3092 /* Throughout X, replace FROM with TO, and return the result.
3093 The result is TO if X is FROM;
3094 otherwise the result is X, but its contents may have been modified.
3095 If they were modified, a record was made in undobuf so that
3096 undo_all will (among other things) return X to its original state.
3098 If the number of changes necessary is too much to record to undo,
3099 the excess changes are not made, so the result is invalid.
3100 The changes already made can still be undone.
3101 undobuf.num_undo is incremented for such changes, so by testing that
3102 the caller can tell whether the result is valid.
3104 `n_occurrences' is incremented each time FROM is replaced.
3106 IN_DEST is non-zero if we are processing the SET_DEST of a SET.
3108 UNIQUE_COPY is non-zero if each substitution must be unique. We do this
3109 by copying if `n_occurrences' is non-zero. */
3111 static rtx
3112 subst (x, from, to, in_dest, unique_copy)
3113 register rtx x, from, to;
3114 int in_dest;
3115 int unique_copy;
3117 register enum rtx_code code = GET_CODE (x);
3118 enum machine_mode op0_mode = VOIDmode;
3119 register const char *fmt;
3120 register int len, i;
3121 rtx new;
3123 /* Two expressions are equal if they are identical copies of a shared
3124 RTX or if they are both registers with the same register number
3125 and mode. */
3127 #define COMBINE_RTX_EQUAL_P(X,Y) \
3128 ((X) == (Y) \
3129 || (GET_CODE (X) == REG && GET_CODE (Y) == REG \
3130 && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
3132 if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
3134 n_occurrences++;
3135 return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
3138 /* If X and FROM are the same register but different modes, they will
3139 not have been seen as equal above. However, flow.c will make a
3140 LOG_LINKS entry for that case. If we do nothing, we will try to
3141 rerecognize our original insn and, when it succeeds, we will
3142 delete the feeding insn, which is incorrect.
3144 So force this insn not to match in this (rare) case. */
3145 if (! in_dest && code == REG && GET_CODE (from) == REG
3146 && REGNO (x) == REGNO (from))
3147 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
3149 /* If this is an object, we are done unless it is a MEM or LO_SUM, both
3150 of which may contain things that can be combined. */
3151 if (code != MEM && code != LO_SUM && GET_RTX_CLASS (code) == 'o')
3152 return x;
3154 /* It is possible to have a subexpression appear twice in the insn.
3155 Suppose that FROM is a register that appears within TO.
3156 Then, after that subexpression has been scanned once by `subst',
3157 the second time it is scanned, TO may be found. If we were
3158 to scan TO here, we would find FROM within it and create a
3159 self-referent rtl structure which is completely wrong. */
3160 if (COMBINE_RTX_EQUAL_P (x, to))
3161 return to;
3163 /* Parallel asm_operands need special attention because all of the
3164 inputs are shared across the arms. Furthermore, unsharing the
3165 rtl results in recognition failures. Failure to handle this case
3166 specially can result in circular rtl.
3168 Solve this by doing a normal pass across the first entry of the
3169 parallel, and only processing the SET_DESTs of the subsequent
3170 entries. Ug. */
3172 if (code == PARALLEL
3173 && GET_CODE (XVECEXP (x, 0, 0)) == SET
3174 && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
3176 new = subst (XVECEXP (x, 0, 0), from, to, 0, unique_copy);
3178 /* If this substitution failed, this whole thing fails. */
3179 if (GET_CODE (new) == CLOBBER
3180 && XEXP (new, 0) == const0_rtx)
3181 return new;
3183 SUBST (XVECEXP (x, 0, 0), new);
3185 for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
3187 rtx dest = SET_DEST (XVECEXP (x, 0, i));
3189 if (GET_CODE (dest) != REG
3190 && GET_CODE (dest) != CC0
3191 && GET_CODE (dest) != PC)
3193 new = subst (dest, from, to, 0, unique_copy);
3195 /* If this substitution failed, this whole thing fails. */
3196 if (GET_CODE (new) == CLOBBER
3197 && XEXP (new, 0) == const0_rtx)
3198 return new;
3200 SUBST (SET_DEST (XVECEXP (x, 0, i)), new);
3204 else
3206 len = GET_RTX_LENGTH (code);
3207 fmt = GET_RTX_FORMAT (code);
3209 /* We don't need to process a SET_DEST that is a register, CC0,
3210 or PC, so set up to skip this common case. All other cases
3211 where we want to suppress replacing something inside a
3212 SET_SRC are handled via the IN_DEST operand. */
3213 if (code == SET
3214 && (GET_CODE (SET_DEST (x)) == REG
3215 || GET_CODE (SET_DEST (x)) == CC0
3216 || GET_CODE (SET_DEST (x)) == PC))
3217 fmt = "ie";
3219 /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
3220 constant. */
3221 if (fmt[0] == 'e')
3222 op0_mode = GET_MODE (XEXP (x, 0));
3224 for (i = 0; i < len; i++)
3226 if (fmt[i] == 'E')
3228 register int j;
3229 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3231 if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
3233 new = (unique_copy && n_occurrences
3234 ? copy_rtx (to) : to);
3235 n_occurrences++;
3237 else
3239 new = subst (XVECEXP (x, i, j), from, to, 0,
3240 unique_copy);
3242 /* If this substitution failed, this whole thing
3243 fails. */
3244 if (GET_CODE (new) == CLOBBER
3245 && XEXP (new, 0) == const0_rtx)
3246 return new;
3249 SUBST (XVECEXP (x, i, j), new);
3252 else if (fmt[i] == 'e')
3254 if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
3256 /* In general, don't install a subreg involving two
3257 modes not tieable. It can worsen register
3258 allocation, and can even make invalid reload
3259 insns, since the reg inside may need to be copied
3260 from in the outside mode, and that may be invalid
3261 if it is an fp reg copied in integer mode.
3263 We allow two exceptions to this: It is valid if
3264 it is inside another SUBREG and the mode of that
3265 SUBREG and the mode of the inside of TO is
3266 tieable and it is valid if X is a SET that copies
3267 FROM to CC0. */
3269 if (GET_CODE (to) == SUBREG
3270 && ! MODES_TIEABLE_P (GET_MODE (to),
3271 GET_MODE (SUBREG_REG (to)))
3272 && ! (code == SUBREG
3273 && MODES_TIEABLE_P (GET_MODE (x),
3274 GET_MODE (SUBREG_REG (to))))
3275 #ifdef HAVE_cc0
3276 && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
3277 #endif
3279 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
3281 new = (unique_copy && n_occurrences ? copy_rtx (to) : to);
3282 n_occurrences++;
3284 else
3285 /* If we are in a SET_DEST, suppress most cases unless we
3286 have gone inside a MEM, in which case we want to
3287 simplify the address. We assume here that things that
3288 are actually part of the destination have their inner
3289 parts in the first expression. This is true for SUBREG,
3290 STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
3291 things aside from REG and MEM that should appear in a
3292 SET_DEST. */
3293 new = subst (XEXP (x, i), from, to,
3294 (((in_dest
3295 && (code == SUBREG || code == STRICT_LOW_PART
3296 || code == ZERO_EXTRACT))
3297 || code == SET)
3298 && i == 0), unique_copy);
3300 /* If we found that we will have to reject this combination,
3301 indicate that by returning the CLOBBER ourselves, rather than
3302 an expression containing it. This will speed things up as
3303 well as prevent accidents where two CLOBBERs are considered
3304 to be equal, thus producing an incorrect simplification. */
3306 if (GET_CODE (new) == CLOBBER && XEXP (new, 0) == const0_rtx)
3307 return new;
3309 SUBST (XEXP (x, i), new);
3314 /* Try to simplify X. If the simplification changed the code, it is likely
3315 that further simplification will help, so loop, but limit the number
3316 of repetitions that will be performed. */
3318 for (i = 0; i < 4; i++)
3320 /* If X is sufficiently simple, don't bother trying to do anything
3321 with it. */
3322 if (code != CONST_INT && code != REG && code != CLOBBER)
3323 x = combine_simplify_rtx (x, op0_mode, i == 3, in_dest);
3325 if (GET_CODE (x) == code)
3326 break;
3328 code = GET_CODE (x);
3330 /* We no longer know the original mode of operand 0 since we
3331 have changed the form of X) */
3332 op0_mode = VOIDmode;
3335 return x;
3338 /* Simplify X, a piece of RTL. We just operate on the expression at the
3339 outer level; call `subst' to simplify recursively. Return the new
3340 expression.
3342 OP0_MODE is the original mode of XEXP (x, 0); LAST is nonzero if this
3343 will be the iteration even if an expression with a code different from
3344 X is returned; IN_DEST is nonzero if we are inside a SET_DEST. */
3346 static rtx
3347 combine_simplify_rtx (x, op0_mode, last, in_dest)
3348 rtx x;
3349 enum machine_mode op0_mode;
3350 int last;
3351 int in_dest;
3353 enum rtx_code code = GET_CODE (x);
3354 enum machine_mode mode = GET_MODE (x);
3355 rtx temp;
3356 int i;
3358 /* If this is a commutative operation, put a constant last and a complex
3359 expression first. We don't need to do this for comparisons here. */
3360 if (GET_RTX_CLASS (code) == 'c'
3361 && ((CONSTANT_P (XEXP (x, 0)) && GET_CODE (XEXP (x, 1)) != CONST_INT)
3362 || (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == 'o'
3363 && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) != 'o')
3364 || (GET_CODE (XEXP (x, 0)) == SUBREG
3365 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0)))) == 'o'
3366 && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) != 'o')))
3368 temp = XEXP (x, 0);
3369 SUBST (XEXP (x, 0), XEXP (x, 1));
3370 SUBST (XEXP (x, 1), temp);
3373 /* If this is a PLUS, MINUS, or MULT, and the first operand is the
3374 sign extension of a PLUS with a constant, reverse the order of the sign
3375 extension and the addition. Note that this not the same as the original
3376 code, but overflow is undefined for signed values. Also note that the
3377 PLUS will have been partially moved "inside" the sign-extension, so that
3378 the first operand of X will really look like:
3379 (ashiftrt (plus (ashift A C4) C5) C4).
3380 We convert this to
3381 (plus (ashiftrt (ashift A C4) C2) C4)
3382 and replace the first operand of X with that expression. Later parts
3383 of this function may simplify the expression further.
3385 For example, if we start with (mult (sign_extend (plus A C1)) C2),
3386 we swap the SIGN_EXTEND and PLUS. Later code will apply the
3387 distributive law to produce (plus (mult (sign_extend X) C1) C3).
3389 We do this to simplify address expressions. */
3391 if ((code == PLUS || code == MINUS || code == MULT)
3392 && GET_CODE (XEXP (x, 0)) == ASHIFTRT
3393 && GET_CODE (XEXP (XEXP (x, 0), 0)) == PLUS
3394 && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ASHIFT
3395 && GET_CODE (XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 1)) == CONST_INT
3396 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3397 && XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 1) == XEXP (XEXP (x, 0), 1)
3398 && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
3399 && (temp = simplify_binary_operation (ASHIFTRT, mode,
3400 XEXP (XEXP (XEXP (x, 0), 0), 1),
3401 XEXP (XEXP (x, 0), 1))) != 0)
3403 rtx new
3404 = simplify_shift_const (NULL_RTX, ASHIFT, mode,
3405 XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 0),
3406 INTVAL (XEXP (XEXP (x, 0), 1)));
3408 new = simplify_shift_const (NULL_RTX, ASHIFTRT, mode, new,
3409 INTVAL (XEXP (XEXP (x, 0), 1)));
3411 SUBST (XEXP (x, 0), gen_binary (PLUS, mode, new, temp));
3414 /* If this is a simple operation applied to an IF_THEN_ELSE, try
3415 applying it to the arms of the IF_THEN_ELSE. This often simplifies
3416 things. Check for cases where both arms are testing the same
3417 condition.
3419 Don't do anything if all operands are very simple. */
3421 if (((GET_RTX_CLASS (code) == '2' || GET_RTX_CLASS (code) == 'c'
3422 || GET_RTX_CLASS (code) == '<')
3423 && ((GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) != 'o'
3424 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
3425 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0))))
3426 == 'o')))
3427 || (GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) != 'o'
3428 && ! (GET_CODE (XEXP (x, 1)) == SUBREG
3429 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 1))))
3430 == 'o')))))
3431 || (GET_RTX_CLASS (code) == '1'
3432 && ((GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) != 'o'
3433 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
3434 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0))))
3435 == 'o'))))))
3437 rtx cond, true, false;
3439 cond = if_then_else_cond (x, &true, &false);
3440 if (cond != 0
3441 /* If everything is a comparison, what we have is highly unlikely
3442 to be simpler, so don't use it. */
3443 && ! (GET_RTX_CLASS (code) == '<'
3444 && (GET_RTX_CLASS (GET_CODE (true)) == '<'
3445 || GET_RTX_CLASS (GET_CODE (false)) == '<')))
3447 rtx cop1 = const0_rtx;
3448 enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
3450 if (cond_code == NE && GET_RTX_CLASS (GET_CODE (cond)) == '<')
3451 return x;
3453 /* Simplify the alternative arms; this may collapse the true and
3454 false arms to store-flag values. */
3455 true = subst (true, pc_rtx, pc_rtx, 0, 0);
3456 false = subst (false, pc_rtx, pc_rtx, 0, 0);
3458 /* Restarting if we generate a store-flag expression will cause
3459 us to loop. Just drop through in this case. */
3461 /* If the result values are STORE_FLAG_VALUE and zero, we can
3462 just make the comparison operation. */
3463 if (true == const_true_rtx && false == const0_rtx)
3464 x = gen_binary (cond_code, mode, cond, cop1);
3465 else if (true == const0_rtx && false == const_true_rtx)
3466 x = gen_binary (reverse_condition (cond_code), mode, cond, cop1);
3468 /* Likewise, we can make the negate of a comparison operation
3469 if the result values are - STORE_FLAG_VALUE and zero. */
3470 else if (GET_CODE (true) == CONST_INT
3471 && INTVAL (true) == - STORE_FLAG_VALUE
3472 && false == const0_rtx)
3473 x = gen_unary (NEG, mode, mode,
3474 gen_binary (cond_code, mode, cond, cop1));
3475 else if (GET_CODE (false) == CONST_INT
3476 && INTVAL (false) == - STORE_FLAG_VALUE
3477 && true == const0_rtx)
3478 x = gen_unary (NEG, mode, mode,
3479 gen_binary (reverse_condition (cond_code),
3480 mode, cond, cop1));
3481 else
3482 return gen_rtx_IF_THEN_ELSE (mode,
3483 gen_binary (cond_code, VOIDmode,
3484 cond, cop1),
3485 true, false);
3487 code = GET_CODE (x);
3488 op0_mode = VOIDmode;
3492 /* Try to fold this expression in case we have constants that weren't
3493 present before. */
3494 temp = 0;
3495 switch (GET_RTX_CLASS (code))
3497 case '1':
3498 temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
3499 break;
3500 case '<':
3501 temp = simplify_relational_operation (code, op0_mode,
3502 XEXP (x, 0), XEXP (x, 1));
3503 #ifdef FLOAT_STORE_FLAG_VALUE
3504 if (temp != 0 && GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
3505 temp = ((temp == const0_rtx) ? CONST0_RTX (GET_MODE (x))
3506 : immed_real_const_1 (FLOAT_STORE_FLAG_VALUE, GET_MODE (x)));
3507 #endif
3508 break;
3509 case 'c':
3510 case '2':
3511 temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
3512 break;
3513 case 'b':
3514 case '3':
3515 temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
3516 XEXP (x, 1), XEXP (x, 2));
3517 break;
3520 if (temp)
3521 x = temp, code = GET_CODE (temp);
3523 /* First see if we can apply the inverse distributive law. */
3524 if (code == PLUS || code == MINUS
3525 || code == AND || code == IOR || code == XOR)
3527 x = apply_distributive_law (x);
3528 code = GET_CODE (x);
3531 /* If CODE is an associative operation not otherwise handled, see if we
3532 can associate some operands. This can win if they are constants or
3533 if they are logically related (i.e. (a & b) & a. */
3534 if ((code == PLUS || code == MINUS
3535 || code == MULT || code == AND || code == IOR || code == XOR
3536 || code == DIV || code == UDIV
3537 || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
3538 && INTEGRAL_MODE_P (mode))
3540 if (GET_CODE (XEXP (x, 0)) == code)
3542 rtx other = XEXP (XEXP (x, 0), 0);
3543 rtx inner_op0 = XEXP (XEXP (x, 0), 1);
3544 rtx inner_op1 = XEXP (x, 1);
3545 rtx inner;
3547 /* Make sure we pass the constant operand if any as the second
3548 one if this is a commutative operation. */
3549 if (CONSTANT_P (inner_op0) && GET_RTX_CLASS (code) == 'c')
3551 rtx tem = inner_op0;
3552 inner_op0 = inner_op1;
3553 inner_op1 = tem;
3555 inner = simplify_binary_operation (code == MINUS ? PLUS
3556 : code == DIV ? MULT
3557 : code == UDIV ? MULT
3558 : code,
3559 mode, inner_op0, inner_op1);
3561 /* For commutative operations, try the other pair if that one
3562 didn't simplify. */
3563 if (inner == 0 && GET_RTX_CLASS (code) == 'c')
3565 other = XEXP (XEXP (x, 0), 1);
3566 inner = simplify_binary_operation (code, mode,
3567 XEXP (XEXP (x, 0), 0),
3568 XEXP (x, 1));
3571 if (inner)
3572 return gen_binary (code, mode, other, inner);
3576 /* A little bit of algebraic simplification here. */
3577 switch (code)
3579 case MEM:
3580 /* Ensure that our address has any ASHIFTs converted to MULT in case
3581 address-recognizing predicates are called later. */
3582 temp = make_compound_operation (XEXP (x, 0), MEM);
3583 SUBST (XEXP (x, 0), temp);
3584 break;
3586 case SUBREG:
3587 /* (subreg:A (mem:B X) N) becomes a modified MEM unless the SUBREG
3588 is paradoxical. If we can't do that safely, then it becomes
3589 something nonsensical so that this combination won't take place. */
3591 if (GET_CODE (SUBREG_REG (x)) == MEM
3592 && (GET_MODE_SIZE (mode)
3593 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))))
3595 rtx inner = SUBREG_REG (x);
3596 int endian_offset = 0;
3597 /* Don't change the mode of the MEM
3598 if that would change the meaning of the address. */
3599 if (MEM_VOLATILE_P (SUBREG_REG (x))
3600 || mode_dependent_address_p (XEXP (inner, 0)))
3601 return gen_rtx_CLOBBER (mode, const0_rtx);
3603 if (BYTES_BIG_ENDIAN)
3605 if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
3606 endian_offset += UNITS_PER_WORD - GET_MODE_SIZE (mode);
3607 if (GET_MODE_SIZE (GET_MODE (inner)) < UNITS_PER_WORD)
3608 endian_offset -= (UNITS_PER_WORD
3609 - GET_MODE_SIZE (GET_MODE (inner)));
3611 /* Note if the plus_constant doesn't make a valid address
3612 then this combination won't be accepted. */
3613 x = gen_rtx_MEM (mode,
3614 plus_constant (XEXP (inner, 0),
3615 (SUBREG_WORD (x) * UNITS_PER_WORD
3616 + endian_offset)));
3617 RTX_UNCHANGING_P (x) = RTX_UNCHANGING_P (inner);
3618 MEM_COPY_ATTRIBUTES (x, inner);
3619 return x;
3622 /* If we are in a SET_DEST, these other cases can't apply. */
3623 if (in_dest)
3624 return x;
3626 /* Changing mode twice with SUBREG => just change it once,
3627 or not at all if changing back to starting mode. */
3628 if (GET_CODE (SUBREG_REG (x)) == SUBREG)
3630 if (mode == GET_MODE (SUBREG_REG (SUBREG_REG (x)))
3631 && SUBREG_WORD (x) == 0 && SUBREG_WORD (SUBREG_REG (x)) == 0)
3632 return SUBREG_REG (SUBREG_REG (x));
3634 SUBST_INT (SUBREG_WORD (x),
3635 SUBREG_WORD (x) + SUBREG_WORD (SUBREG_REG (x)));
3636 SUBST (SUBREG_REG (x), SUBREG_REG (SUBREG_REG (x)));
3639 /* SUBREG of a hard register => just change the register number
3640 and/or mode. If the hard register is not valid in that mode,
3641 suppress this combination. If the hard register is the stack,
3642 frame, or argument pointer, leave this as a SUBREG. */
3644 if (GET_CODE (SUBREG_REG (x)) == REG
3645 && REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER
3646 && REGNO (SUBREG_REG (x)) != FRAME_POINTER_REGNUM
3647 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3648 && REGNO (SUBREG_REG (x)) != HARD_FRAME_POINTER_REGNUM
3649 #endif
3650 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3651 && REGNO (SUBREG_REG (x)) != ARG_POINTER_REGNUM
3652 #endif
3653 && REGNO (SUBREG_REG (x)) != STACK_POINTER_REGNUM)
3655 if (HARD_REGNO_MODE_OK (REGNO (SUBREG_REG (x)) + SUBREG_WORD (x),
3656 mode))
3657 return gen_rtx_REG (mode,
3658 REGNO (SUBREG_REG (x)) + SUBREG_WORD (x));
3659 else
3660 return gen_rtx_CLOBBER (mode, const0_rtx);
3663 /* For a constant, try to pick up the part we want. Handle a full
3664 word and low-order part. Only do this if we are narrowing
3665 the constant; if it is being widened, we have no idea what
3666 the extra bits will have been set to. */
3668 if (CONSTANT_P (SUBREG_REG (x)) && op0_mode != VOIDmode
3669 && GET_MODE_SIZE (mode) == UNITS_PER_WORD
3670 && GET_MODE_SIZE (op0_mode) > UNITS_PER_WORD
3671 && GET_MODE_CLASS (mode) == MODE_INT)
3673 temp = operand_subword (SUBREG_REG (x), SUBREG_WORD (x),
3674 0, op0_mode);
3675 if (temp)
3676 return temp;
3679 /* If we want a subreg of a constant, at offset 0,
3680 take the low bits. On a little-endian machine, that's
3681 always valid. On a big-endian machine, it's valid
3682 only if the constant's mode fits in one word. Note that we
3683 cannot use subreg_lowpart_p since SUBREG_REG may be VOIDmode. */
3684 if (CONSTANT_P (SUBREG_REG (x))
3685 && ((GET_MODE_SIZE (op0_mode) <= UNITS_PER_WORD
3686 || ! WORDS_BIG_ENDIAN)
3687 ? SUBREG_WORD (x) == 0
3688 : (SUBREG_WORD (x)
3689 == ((GET_MODE_SIZE (op0_mode)
3690 - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD))
3691 / UNITS_PER_WORD)))
3692 && GET_MODE_SIZE (mode) <= GET_MODE_SIZE (op0_mode)
3693 && (! WORDS_BIG_ENDIAN
3694 || GET_MODE_BITSIZE (op0_mode) <= BITS_PER_WORD))
3695 return gen_lowpart_for_combine (mode, SUBREG_REG (x));
3697 /* A paradoxical SUBREG of a VOIDmode constant is the same constant,
3698 since we are saying that the high bits don't matter. */
3699 if (CONSTANT_P (SUBREG_REG (x)) && GET_MODE (SUBREG_REG (x)) == VOIDmode
3700 && GET_MODE_SIZE (mode) > GET_MODE_SIZE (op0_mode))
3701 return SUBREG_REG (x);
3703 /* Note that we cannot do any narrowing for non-constants since
3704 we might have been counting on using the fact that some bits were
3705 zero. We now do this in the SET. */
3707 break;
3709 case NOT:
3710 /* (not (plus X -1)) can become (neg X). */
3711 if (GET_CODE (XEXP (x, 0)) == PLUS
3712 && XEXP (XEXP (x, 0), 1) == constm1_rtx)
3713 return gen_rtx_combine (NEG, mode, XEXP (XEXP (x, 0), 0));
3715 /* Similarly, (not (neg X)) is (plus X -1). */
3716 if (GET_CODE (XEXP (x, 0)) == NEG)
3717 return gen_rtx_combine (PLUS, mode, XEXP (XEXP (x, 0), 0),
3718 constm1_rtx);
3720 /* (not (xor X C)) for C constant is (xor X D) with D = ~ C. */
3721 if (GET_CODE (XEXP (x, 0)) == XOR
3722 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3723 && (temp = simplify_unary_operation (NOT, mode,
3724 XEXP (XEXP (x, 0), 1),
3725 mode)) != 0)
3726 return gen_binary (XOR, mode, XEXP (XEXP (x, 0), 0), temp);
3728 /* (not (ashift 1 X)) is (rotate ~1 X). We used to do this for operands
3729 other than 1, but that is not valid. We could do a similar
3730 simplification for (not (lshiftrt C X)) where C is just the sign bit,
3731 but this doesn't seem common enough to bother with. */
3732 if (GET_CODE (XEXP (x, 0)) == ASHIFT
3733 && XEXP (XEXP (x, 0), 0) == const1_rtx)
3734 return gen_rtx_ROTATE (mode, gen_unary (NOT, mode, mode, const1_rtx),
3735 XEXP (XEXP (x, 0), 1));
3737 if (GET_CODE (XEXP (x, 0)) == SUBREG
3738 && subreg_lowpart_p (XEXP (x, 0))
3739 && (GET_MODE_SIZE (GET_MODE (XEXP (x, 0)))
3740 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (x, 0)))))
3741 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == ASHIFT
3742 && XEXP (SUBREG_REG (XEXP (x, 0)), 0) == const1_rtx)
3744 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (XEXP (x, 0)));
3746 x = gen_rtx_ROTATE (inner_mode,
3747 gen_unary (NOT, inner_mode, inner_mode,
3748 const1_rtx),
3749 XEXP (SUBREG_REG (XEXP (x, 0)), 1));
3750 return gen_lowpart_for_combine (mode, x);
3753 /* If STORE_FLAG_VALUE is -1, (not (comparison foo bar)) can be done by
3754 reversing the comparison code if valid. */
3755 if (STORE_FLAG_VALUE == -1
3756 && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
3757 && reversible_comparison_p (XEXP (x, 0)))
3758 return gen_rtx_combine (reverse_condition (GET_CODE (XEXP (x, 0))),
3759 mode, XEXP (XEXP (x, 0), 0),
3760 XEXP (XEXP (x, 0), 1));
3762 /* (ashiftrt foo C) where C is the number of bits in FOO minus 1
3763 is (lt foo (const_int 0)) if STORE_FLAG_VALUE is -1, so we can
3764 perform the above simplification. */
3766 if (STORE_FLAG_VALUE == -1
3767 && XEXP (x, 1) == const1_rtx
3768 && GET_CODE (XEXP (x, 0)) == ASHIFTRT
3769 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3770 && INTVAL (XEXP (XEXP (x, 0), 1)) == GET_MODE_BITSIZE (mode) - 1)
3771 return gen_rtx_combine (GE, mode, XEXP (XEXP (x, 0), 0), const0_rtx);
3773 /* Apply De Morgan's laws to reduce number of patterns for machines
3774 with negating logical insns (and-not, nand, etc.). If result has
3775 only one NOT, put it first, since that is how the patterns are
3776 coded. */
3778 if (GET_CODE (XEXP (x, 0)) == IOR || GET_CODE (XEXP (x, 0)) == AND)
3780 rtx in1 = XEXP (XEXP (x, 0), 0), in2 = XEXP (XEXP (x, 0), 1);
3782 if (GET_CODE (in1) == NOT)
3783 in1 = XEXP (in1, 0);
3784 else
3785 in1 = gen_rtx_combine (NOT, GET_MODE (in1), in1);
3787 if (GET_CODE (in2) == NOT)
3788 in2 = XEXP (in2, 0);
3789 else if (GET_CODE (in2) == CONST_INT
3790 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
3791 in2 = GEN_INT (GET_MODE_MASK (mode) & ~ INTVAL (in2));
3792 else
3793 in2 = gen_rtx_combine (NOT, GET_MODE (in2), in2);
3795 if (GET_CODE (in2) == NOT)
3797 rtx tem = in2;
3798 in2 = in1; in1 = tem;
3801 return gen_rtx_combine (GET_CODE (XEXP (x, 0)) == IOR ? AND : IOR,
3802 mode, in1, in2);
3804 break;
3806 case NEG:
3807 /* (neg (plus X 1)) can become (not X). */
3808 if (GET_CODE (XEXP (x, 0)) == PLUS
3809 && XEXP (XEXP (x, 0), 1) == const1_rtx)
3810 return gen_rtx_combine (NOT, mode, XEXP (XEXP (x, 0), 0));
3812 /* Similarly, (neg (not X)) is (plus X 1). */
3813 if (GET_CODE (XEXP (x, 0)) == NOT)
3814 return plus_constant (XEXP (XEXP (x, 0), 0), 1);
3816 /* (neg (minus X Y)) can become (minus Y X). */
3817 if (GET_CODE (XEXP (x, 0)) == MINUS
3818 && (! FLOAT_MODE_P (mode)
3819 /* x-y != -(y-x) with IEEE floating point. */
3820 || TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
3821 || flag_fast_math))
3822 return gen_binary (MINUS, mode, XEXP (XEXP (x, 0), 1),
3823 XEXP (XEXP (x, 0), 0));
3825 /* (neg (xor A 1)) is (plus A -1) if A is known to be either 0 or 1. */
3826 if (GET_CODE (XEXP (x, 0)) == XOR && XEXP (XEXP (x, 0), 1) == const1_rtx
3827 && nonzero_bits (XEXP (XEXP (x, 0), 0), mode) == 1)
3828 return gen_binary (PLUS, mode, XEXP (XEXP (x, 0), 0), constm1_rtx);
3830 /* NEG commutes with ASHIFT since it is multiplication. Only do this
3831 if we can then eliminate the NEG (e.g.,
3832 if the operand is a constant). */
3834 if (GET_CODE (XEXP (x, 0)) == ASHIFT)
3836 temp = simplify_unary_operation (NEG, mode,
3837 XEXP (XEXP (x, 0), 0), mode);
3838 if (temp)
3840 SUBST (XEXP (XEXP (x, 0), 0), temp);
3841 return XEXP (x, 0);
3845 temp = expand_compound_operation (XEXP (x, 0));
3847 /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
3848 replaced by (lshiftrt X C). This will convert
3849 (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y). */
3851 if (GET_CODE (temp) == ASHIFTRT
3852 && GET_CODE (XEXP (temp, 1)) == CONST_INT
3853 && INTVAL (XEXP (temp, 1)) == GET_MODE_BITSIZE (mode) - 1)
3854 return simplify_shift_const (temp, LSHIFTRT, mode, XEXP (temp, 0),
3855 INTVAL (XEXP (temp, 1)));
3857 /* If X has only a single bit that might be nonzero, say, bit I, convert
3858 (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
3859 MODE minus 1. This will convert (neg (zero_extract X 1 Y)) to
3860 (sign_extract X 1 Y). But only do this if TEMP isn't a register
3861 or a SUBREG of one since we'd be making the expression more
3862 complex if it was just a register. */
3864 if (GET_CODE (temp) != REG
3865 && ! (GET_CODE (temp) == SUBREG
3866 && GET_CODE (SUBREG_REG (temp)) == REG)
3867 && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
3869 rtx temp1 = simplify_shift_const
3870 (NULL_RTX, ASHIFTRT, mode,
3871 simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
3872 GET_MODE_BITSIZE (mode) - 1 - i),
3873 GET_MODE_BITSIZE (mode) - 1 - i);
3875 /* If all we did was surround TEMP with the two shifts, we
3876 haven't improved anything, so don't use it. Otherwise,
3877 we are better off with TEMP1. */
3878 if (GET_CODE (temp1) != ASHIFTRT
3879 || GET_CODE (XEXP (temp1, 0)) != ASHIFT
3880 || XEXP (XEXP (temp1, 0), 0) != temp)
3881 return temp1;
3883 break;
3885 case TRUNCATE:
3886 /* We can't handle truncation to a partial integer mode here
3887 because we don't know the real bitsize of the partial
3888 integer mode. */
3889 if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
3890 break;
3892 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3893 && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
3894 GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))))
3895 SUBST (XEXP (x, 0),
3896 force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
3897 GET_MODE_MASK (mode), NULL_RTX, 0));
3899 /* (truncate:SI ({sign,zero}_extend:DI foo:SI)) == foo:SI. */
3900 if ((GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
3901 || GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
3902 && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode)
3903 return XEXP (XEXP (x, 0), 0);
3905 /* (truncate:SI (OP:DI ({sign,zero}_extend:DI foo:SI))) is
3906 (OP:SI foo:SI) if OP is NEG or ABS. */
3907 if ((GET_CODE (XEXP (x, 0)) == ABS
3908 || GET_CODE (XEXP (x, 0)) == NEG)
3909 && (GET_CODE (XEXP (XEXP (x, 0), 0)) == SIGN_EXTEND
3910 || GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND)
3911 && GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == mode)
3912 return gen_unary (GET_CODE (XEXP (x, 0)), mode, mode,
3913 XEXP (XEXP (XEXP (x, 0), 0), 0));
3915 /* (truncate:SI (subreg:DI (truncate:SI X) 0)) is
3916 (truncate:SI x). */
3917 if (GET_CODE (XEXP (x, 0)) == SUBREG
3918 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == TRUNCATE
3919 && subreg_lowpart_p (XEXP (x, 0)))
3920 return SUBREG_REG (XEXP (x, 0));
3922 /* If we know that the value is already truncated, we can
3923 replace the TRUNCATE with a SUBREG if TRULY_NOOP_TRUNCATION
3924 is nonzero for the corresponding modes. But don't do this
3925 for an (LSHIFTRT (MULT ...)) since this will cause problems
3926 with the umulXi3_highpart patterns. */
3927 if (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
3928 GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
3929 && num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
3930 >= GET_MODE_BITSIZE (mode) + 1
3931 && ! (GET_CODE (XEXP (x, 0)) == LSHIFTRT
3932 && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT))
3933 return gen_lowpart_for_combine (mode, XEXP (x, 0));
3935 /* A truncate of a comparison can be replaced with a subreg if
3936 STORE_FLAG_VALUE permits. This is like the previous test,
3937 but it works even if the comparison is done in a mode larger
3938 than HOST_BITS_PER_WIDE_INT. */
3939 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3940 && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
3941 && ((HOST_WIDE_INT) STORE_FLAG_VALUE &~ GET_MODE_MASK (mode)) == 0)
3942 return gen_lowpart_for_combine (mode, XEXP (x, 0));
3944 /* Similarly, a truncate of a register whose value is a
3945 comparison can be replaced with a subreg if STORE_FLAG_VALUE
3946 permits. */
3947 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3948 && ((HOST_WIDE_INT) STORE_FLAG_VALUE &~ GET_MODE_MASK (mode)) == 0
3949 && (temp = get_last_value (XEXP (x, 0)))
3950 && GET_RTX_CLASS (GET_CODE (temp)) == '<')
3951 return gen_lowpart_for_combine (mode, XEXP (x, 0));
3953 break;
3955 case FLOAT_TRUNCATE:
3956 /* (float_truncate:SF (float_extend:DF foo:SF)) = foo:SF. */
3957 if (GET_CODE (XEXP (x, 0)) == FLOAT_EXTEND
3958 && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode)
3959 return XEXP (XEXP (x, 0), 0);
3961 /* (float_truncate:SF (OP:DF (float_extend:DF foo:sf))) is
3962 (OP:SF foo:SF) if OP is NEG or ABS. */
3963 if ((GET_CODE (XEXP (x, 0)) == ABS
3964 || GET_CODE (XEXP (x, 0)) == NEG)
3965 && GET_CODE (XEXP (XEXP (x, 0), 0)) == FLOAT_EXTEND
3966 && GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == mode)
3967 return gen_unary (GET_CODE (XEXP (x, 0)), mode, mode,
3968 XEXP (XEXP (XEXP (x, 0), 0), 0));
3970 /* (float_truncate:SF (subreg:DF (float_truncate:SF X) 0))
3971 is (float_truncate:SF x). */
3972 if (GET_CODE (XEXP (x, 0)) == SUBREG
3973 && subreg_lowpart_p (XEXP (x, 0))
3974 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == FLOAT_TRUNCATE)
3975 return SUBREG_REG (XEXP (x, 0));
3976 break;
3978 #ifdef HAVE_cc0
3979 case COMPARE:
3980 /* Convert (compare FOO (const_int 0)) to FOO unless we aren't
3981 using cc0, in which case we want to leave it as a COMPARE
3982 so we can distinguish it from a register-register-copy. */
3983 if (XEXP (x, 1) == const0_rtx)
3984 return XEXP (x, 0);
3986 /* In IEEE floating point, x-0 is not the same as x. */
3987 if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
3988 || ! FLOAT_MODE_P (GET_MODE (XEXP (x, 0)))
3989 || flag_fast_math)
3990 && XEXP (x, 1) == CONST0_RTX (GET_MODE (XEXP (x, 0))))
3991 return XEXP (x, 0);
3992 break;
3993 #endif
3995 case CONST:
3996 /* (const (const X)) can become (const X). Do it this way rather than
3997 returning the inner CONST since CONST can be shared with a
3998 REG_EQUAL note. */
3999 if (GET_CODE (XEXP (x, 0)) == CONST)
4000 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4001 break;
4003 #ifdef HAVE_lo_sum
4004 case LO_SUM:
4005 /* Convert (lo_sum (high FOO) FOO) to FOO. This is necessary so we
4006 can add in an offset. find_split_point will split this address up
4007 again if it doesn't match. */
4008 if (GET_CODE (XEXP (x, 0)) == HIGH
4009 && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
4010 return XEXP (x, 1);
4011 break;
4012 #endif
4014 case PLUS:
4015 /* If we have (plus (plus (A const) B)), associate it so that CONST is
4016 outermost. That's because that's the way indexed addresses are
4017 supposed to appear. This code used to check many more cases, but
4018 they are now checked elsewhere. */
4019 if (GET_CODE (XEXP (x, 0)) == PLUS
4020 && CONSTANT_ADDRESS_P (XEXP (XEXP (x, 0), 1)))
4021 return gen_binary (PLUS, mode,
4022 gen_binary (PLUS, mode, XEXP (XEXP (x, 0), 0),
4023 XEXP (x, 1)),
4024 XEXP (XEXP (x, 0), 1));
4026 /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
4027 when c is (const_int (pow2 + 1) / 2) is a sign extension of a
4028 bit-field and can be replaced by either a sign_extend or a
4029 sign_extract. The `and' may be a zero_extend and the two
4030 <c>, -<c> constants may be reversed. */
4031 if (GET_CODE (XEXP (x, 0)) == XOR
4032 && GET_CODE (XEXP (x, 1)) == CONST_INT
4033 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
4034 && INTVAL (XEXP (x, 1)) == - INTVAL (XEXP (XEXP (x, 0), 1))
4035 && ((i = exact_log2 (INTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
4036 || (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
4037 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4038 && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
4039 && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
4040 && (INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
4041 == ((HOST_WIDE_INT) 1 << (i + 1)) - 1))
4042 || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
4043 && (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
4044 == i + 1))))
4045 return simplify_shift_const
4046 (NULL_RTX, ASHIFTRT, mode,
4047 simplify_shift_const (NULL_RTX, ASHIFT, mode,
4048 XEXP (XEXP (XEXP (x, 0), 0), 0),
4049 GET_MODE_BITSIZE (mode) - (i + 1)),
4050 GET_MODE_BITSIZE (mode) - (i + 1));
4052 /* (plus (comparison A B) C) can become (neg (rev-comp A B)) if
4053 C is 1 and STORE_FLAG_VALUE is -1 or if C is -1 and STORE_FLAG_VALUE
4054 is 1. This produces better code than the alternative immediately
4055 below. */
4056 if (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
4057 && reversible_comparison_p (XEXP (x, 0))
4058 && ((STORE_FLAG_VALUE == -1 && XEXP (x, 1) == const1_rtx)
4059 || (STORE_FLAG_VALUE == 1 && XEXP (x, 1) == constm1_rtx)))
4060 return
4061 gen_unary (NEG, mode, mode,
4062 gen_binary (reverse_condition (GET_CODE (XEXP (x, 0))),
4063 mode, XEXP (XEXP (x, 0), 0),
4064 XEXP (XEXP (x, 0), 1)));
4066 /* If only the low-order bit of X is possibly nonzero, (plus x -1)
4067 can become (ashiftrt (ashift (xor x 1) C) C) where C is
4068 the bitsize of the mode - 1. This allows simplification of
4069 "a = (b & 8) == 0;" */
4070 if (XEXP (x, 1) == constm1_rtx
4071 && GET_CODE (XEXP (x, 0)) != REG
4072 && ! (GET_CODE (XEXP (x,0)) == SUBREG
4073 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG)
4074 && nonzero_bits (XEXP (x, 0), mode) == 1)
4075 return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
4076 simplify_shift_const (NULL_RTX, ASHIFT, mode,
4077 gen_rtx_combine (XOR, mode,
4078 XEXP (x, 0), const1_rtx),
4079 GET_MODE_BITSIZE (mode) - 1),
4080 GET_MODE_BITSIZE (mode) - 1);
4082 /* If we are adding two things that have no bits in common, convert
4083 the addition into an IOR. This will often be further simplified,
4084 for example in cases like ((a & 1) + (a & 2)), which can
4085 become a & 3. */
4087 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4088 && (nonzero_bits (XEXP (x, 0), mode)
4089 & nonzero_bits (XEXP (x, 1), mode)) == 0)
4090 return gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
4091 break;
4093 case MINUS:
4094 /* If STORE_FLAG_VALUE is 1, (minus 1 (comparison foo bar)) can be done
4095 by reversing the comparison code if valid. */
4096 if (STORE_FLAG_VALUE == 1
4097 && XEXP (x, 0) == const1_rtx
4098 && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) == '<'
4099 && reversible_comparison_p (XEXP (x, 1)))
4100 return gen_binary (reverse_condition (GET_CODE (XEXP (x, 1))),
4101 mode, XEXP (XEXP (x, 1), 0),
4102 XEXP (XEXP (x, 1), 1));
4104 /* (minus <foo> (and <foo> (const_int -pow2))) becomes
4105 (and <foo> (const_int pow2-1)) */
4106 if (GET_CODE (XEXP (x, 1)) == AND
4107 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
4108 && exact_log2 (- INTVAL (XEXP (XEXP (x, 1), 1))) >= 0
4109 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
4110 return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
4111 - INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
4113 /* Canonicalize (minus A (plus B C)) to (minus (minus A B) C) for
4114 integers. */
4115 if (GET_CODE (XEXP (x, 1)) == PLUS && INTEGRAL_MODE_P (mode))
4116 return gen_binary (MINUS, mode,
4117 gen_binary (MINUS, mode, XEXP (x, 0),
4118 XEXP (XEXP (x, 1), 0)),
4119 XEXP (XEXP (x, 1), 1));
4120 break;
4122 case MULT:
4123 /* If we have (mult (plus A B) C), apply the distributive law and then
4124 the inverse distributive law to see if things simplify. This
4125 occurs mostly in addresses, often when unrolling loops. */
4127 if (GET_CODE (XEXP (x, 0)) == PLUS)
4129 x = apply_distributive_law
4130 (gen_binary (PLUS, mode,
4131 gen_binary (MULT, mode,
4132 XEXP (XEXP (x, 0), 0), XEXP (x, 1)),
4133 gen_binary (MULT, mode,
4134 XEXP (XEXP (x, 0), 1), XEXP (x, 1))));
4136 if (GET_CODE (x) != MULT)
4137 return x;
4139 break;
4141 case UDIV:
4142 /* If this is a divide by a power of two, treat it as a shift if
4143 its first operand is a shift. */
4144 if (GET_CODE (XEXP (x, 1)) == CONST_INT
4145 && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0
4146 && (GET_CODE (XEXP (x, 0)) == ASHIFT
4147 || GET_CODE (XEXP (x, 0)) == LSHIFTRT
4148 || GET_CODE (XEXP (x, 0)) == ASHIFTRT
4149 || GET_CODE (XEXP (x, 0)) == ROTATE
4150 || GET_CODE (XEXP (x, 0)) == ROTATERT))
4151 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
4152 break;
4154 case EQ: case NE:
4155 case GT: case GTU: case GE: case GEU:
4156 case LT: case LTU: case LE: case LEU:
4157 /* If the first operand is a condition code, we can't do anything
4158 with it. */
4159 if (GET_CODE (XEXP (x, 0)) == COMPARE
4160 || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
4161 #ifdef HAVE_cc0
4162 && XEXP (x, 0) != cc0_rtx
4163 #endif
4166 rtx op0 = XEXP (x, 0);
4167 rtx op1 = XEXP (x, 1);
4168 enum rtx_code new_code;
4170 if (GET_CODE (op0) == COMPARE)
4171 op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
4173 /* Simplify our comparison, if possible. */
4174 new_code = simplify_comparison (code, &op0, &op1);
4176 /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
4177 if only the low-order bit is possibly nonzero in X (such as when
4178 X is a ZERO_EXTRACT of one bit). Similarly, we can convert EQ to
4179 (xor X 1) or (minus 1 X); we use the former. Finally, if X is
4180 known to be either 0 or -1, NE becomes a NEG and EQ becomes
4181 (plus X 1).
4183 Remove any ZERO_EXTRACT we made when thinking this was a
4184 comparison. It may now be simpler to use, e.g., an AND. If a
4185 ZERO_EXTRACT is indeed appropriate, it will be placed back by
4186 the call to make_compound_operation in the SET case. */
4188 if (STORE_FLAG_VALUE == 1
4189 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4190 && op1 == const0_rtx && nonzero_bits (op0, mode) == 1)
4191 return gen_lowpart_for_combine (mode,
4192 expand_compound_operation (op0));
4194 else if (STORE_FLAG_VALUE == 1
4195 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4196 && op1 == const0_rtx
4197 && (num_sign_bit_copies (op0, mode)
4198 == GET_MODE_BITSIZE (mode)))
4200 op0 = expand_compound_operation (op0);
4201 return gen_unary (NEG, mode, mode,
4202 gen_lowpart_for_combine (mode, op0));
4205 else if (STORE_FLAG_VALUE == 1
4206 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4207 && op1 == const0_rtx
4208 && nonzero_bits (op0, mode) == 1)
4210 op0 = expand_compound_operation (op0);
4211 return gen_binary (XOR, mode,
4212 gen_lowpart_for_combine (mode, op0),
4213 const1_rtx);
4216 else if (STORE_FLAG_VALUE == 1
4217 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4218 && op1 == const0_rtx
4219 && (num_sign_bit_copies (op0, mode)
4220 == GET_MODE_BITSIZE (mode)))
4222 op0 = expand_compound_operation (op0);
4223 return plus_constant (gen_lowpart_for_combine (mode, op0), 1);
4226 /* If STORE_FLAG_VALUE is -1, we have cases similar to
4227 those above. */
4228 if (STORE_FLAG_VALUE == -1
4229 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4230 && op1 == const0_rtx
4231 && (num_sign_bit_copies (op0, mode)
4232 == GET_MODE_BITSIZE (mode)))
4233 return gen_lowpart_for_combine (mode,
4234 expand_compound_operation (op0));
4236 else if (STORE_FLAG_VALUE == -1
4237 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4238 && op1 == const0_rtx
4239 && nonzero_bits (op0, mode) == 1)
4241 op0 = expand_compound_operation (op0);
4242 return gen_unary (NEG, mode, mode,
4243 gen_lowpart_for_combine (mode, op0));
4246 else if (STORE_FLAG_VALUE == -1
4247 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4248 && op1 == const0_rtx
4249 && (num_sign_bit_copies (op0, mode)
4250 == GET_MODE_BITSIZE (mode)))
4252 op0 = expand_compound_operation (op0);
4253 return gen_unary (NOT, mode, mode,
4254 gen_lowpart_for_combine (mode, op0));
4257 /* If X is 0/1, (eq X 0) is X-1. */
4258 else if (STORE_FLAG_VALUE == -1
4259 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4260 && op1 == const0_rtx
4261 && nonzero_bits (op0, mode) == 1)
4263 op0 = expand_compound_operation (op0);
4264 return plus_constant (gen_lowpart_for_combine (mode, op0), -1);
4267 /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
4268 one bit that might be nonzero, we can convert (ne x 0) to
4269 (ashift x c) where C puts the bit in the sign bit. Remove any
4270 AND with STORE_FLAG_VALUE when we are done, since we are only
4271 going to test the sign bit. */
4272 if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4273 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4274 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
4275 == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE(mode)-1))
4276 && op1 == const0_rtx
4277 && mode == GET_MODE (op0)
4278 && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
4280 x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
4281 expand_compound_operation (op0),
4282 GET_MODE_BITSIZE (mode) - 1 - i);
4283 if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
4284 return XEXP (x, 0);
4285 else
4286 return x;
4289 /* If the code changed, return a whole new comparison. */
4290 if (new_code != code)
4291 return gen_rtx_combine (new_code, mode, op0, op1);
4293 /* Otherwise, keep this operation, but maybe change its operands.
4294 This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR). */
4295 SUBST (XEXP (x, 0), op0);
4296 SUBST (XEXP (x, 1), op1);
4298 break;
4300 case IF_THEN_ELSE:
4301 return simplify_if_then_else (x);
4303 case ZERO_EXTRACT:
4304 case SIGN_EXTRACT:
4305 case ZERO_EXTEND:
4306 case SIGN_EXTEND:
4307 /* If we are processing SET_DEST, we are done. */
4308 if (in_dest)
4309 return x;
4311 return expand_compound_operation (x);
4313 case SET:
4314 return simplify_set (x);
4316 case AND:
4317 case IOR:
4318 case XOR:
4319 return simplify_logical (x, last);
4321 case ABS:
4322 /* (abs (neg <foo>)) -> (abs <foo>) */
4323 if (GET_CODE (XEXP (x, 0)) == NEG)
4324 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4326 /* If the mode of the operand is VOIDmode (i.e. if it is ASM_OPERANDS),
4327 do nothing. */
4328 if (GET_MODE (XEXP (x, 0)) == VOIDmode)
4329 break;
4331 /* If operand is something known to be positive, ignore the ABS. */
4332 if (GET_CODE (XEXP (x, 0)) == FFS || GET_CODE (XEXP (x, 0)) == ABS
4333 || ((GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
4334 <= HOST_BITS_PER_WIDE_INT)
4335 && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
4336 & ((HOST_WIDE_INT) 1
4337 << (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - 1)))
4338 == 0)))
4339 return XEXP (x, 0);
4342 /* If operand is known to be only -1 or 0, convert ABS to NEG. */
4343 if (num_sign_bit_copies (XEXP (x, 0), mode) == GET_MODE_BITSIZE (mode))
4344 return gen_rtx_combine (NEG, mode, XEXP (x, 0));
4346 break;
4348 case FFS:
4349 /* (ffs (*_extend <X>)) = (ffs <X>) */
4350 if (GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
4351 || GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
4352 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4353 break;
4355 case FLOAT:
4356 /* (float (sign_extend <X>)) = (float <X>). */
4357 if (GET_CODE (XEXP (x, 0)) == SIGN_EXTEND)
4358 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4359 break;
4361 case ASHIFT:
4362 case LSHIFTRT:
4363 case ASHIFTRT:
4364 case ROTATE:
4365 case ROTATERT:
4366 /* If this is a shift by a constant amount, simplify it. */
4367 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4368 return simplify_shift_const (x, code, mode, XEXP (x, 0),
4369 INTVAL (XEXP (x, 1)));
4371 #ifdef SHIFT_COUNT_TRUNCATED
4372 else if (SHIFT_COUNT_TRUNCATED && GET_CODE (XEXP (x, 1)) != REG)
4373 SUBST (XEXP (x, 1),
4374 force_to_mode (XEXP (x, 1), GET_MODE (x),
4375 ((HOST_WIDE_INT) 1
4376 << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
4377 - 1,
4378 NULL_RTX, 0));
4379 #endif
4381 break;
4383 default:
4384 break;
4387 return x;
4390 /* Simplify X, an IF_THEN_ELSE expression. Return the new expression. */
4392 static rtx
4393 simplify_if_then_else (x)
4394 rtx x;
4396 enum machine_mode mode = GET_MODE (x);
4397 rtx cond = XEXP (x, 0);
4398 rtx true = XEXP (x, 1);
4399 rtx false = XEXP (x, 2);
4400 enum rtx_code true_code = GET_CODE (cond);
4401 int comparison_p = GET_RTX_CLASS (true_code) == '<';
4402 rtx temp;
4403 int i;
4405 /* Simplify storing of the truth value. */
4406 if (comparison_p && true == const_true_rtx && false == const0_rtx)
4407 return gen_binary (true_code, mode, XEXP (cond, 0), XEXP (cond, 1));
4409 /* Also when the truth value has to be reversed. */
4410 if (comparison_p && reversible_comparison_p (cond)
4411 && true == const0_rtx && false == const_true_rtx)
4412 return gen_binary (reverse_condition (true_code),
4413 mode, XEXP (cond, 0), XEXP (cond, 1));
4415 /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
4416 in it is being compared against certain values. Get the true and false
4417 comparisons and see if that says anything about the value of each arm. */
4419 if (comparison_p && reversible_comparison_p (cond)
4420 && GET_CODE (XEXP (cond, 0)) == REG)
4422 HOST_WIDE_INT nzb;
4423 rtx from = XEXP (cond, 0);
4424 enum rtx_code false_code = reverse_condition (true_code);
4425 rtx true_val = XEXP (cond, 1);
4426 rtx false_val = true_val;
4427 int swapped = 0;
4429 /* If FALSE_CODE is EQ, swap the codes and arms. */
4431 if (false_code == EQ)
4433 swapped = 1, true_code = EQ, false_code = NE;
4434 temp = true, true = false, false = temp;
4437 /* If we are comparing against zero and the expression being tested has
4438 only a single bit that might be nonzero, that is its value when it is
4439 not equal to zero. Similarly if it is known to be -1 or 0. */
4441 if (true_code == EQ && true_val == const0_rtx
4442 && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
4443 false_code = EQ, false_val = GEN_INT (nzb);
4444 else if (true_code == EQ && true_val == const0_rtx
4445 && (num_sign_bit_copies (from, GET_MODE (from))
4446 == GET_MODE_BITSIZE (GET_MODE (from))))
4447 false_code = EQ, false_val = constm1_rtx;
4449 /* Now simplify an arm if we know the value of the register in the
4450 branch and it is used in the arm. Be careful due to the potential
4451 of locally-shared RTL. */
4453 if (reg_mentioned_p (from, true))
4454 true = subst (known_cond (copy_rtx (true), true_code, from, true_val),
4455 pc_rtx, pc_rtx, 0, 0);
4456 if (reg_mentioned_p (from, false))
4457 false = subst (known_cond (copy_rtx (false), false_code,
4458 from, false_val),
4459 pc_rtx, pc_rtx, 0, 0);
4461 SUBST (XEXP (x, 1), swapped ? false : true);
4462 SUBST (XEXP (x, 2), swapped ? true : false);
4464 true = XEXP (x, 1), false = XEXP (x, 2), true_code = GET_CODE (cond);
4467 /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
4468 reversed, do so to avoid needing two sets of patterns for
4469 subtract-and-branch insns. Similarly if we have a constant in the true
4470 arm, the false arm is the same as the first operand of the comparison, or
4471 the false arm is more complicated than the true arm. */
4473 if (comparison_p && reversible_comparison_p (cond)
4474 && (true == pc_rtx
4475 || (CONSTANT_P (true)
4476 && GET_CODE (false) != CONST_INT && false != pc_rtx)
4477 || true == const0_rtx
4478 || (GET_RTX_CLASS (GET_CODE (true)) == 'o'
4479 && GET_RTX_CLASS (GET_CODE (false)) != 'o')
4480 || (GET_CODE (true) == SUBREG
4481 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (true))) == 'o'
4482 && GET_RTX_CLASS (GET_CODE (false)) != 'o')
4483 || reg_mentioned_p (true, false)
4484 || rtx_equal_p (false, XEXP (cond, 0))))
4486 true_code = reverse_condition (true_code);
4487 SUBST (XEXP (x, 0),
4488 gen_binary (true_code, GET_MODE (cond), XEXP (cond, 0),
4489 XEXP (cond, 1)));
4491 SUBST (XEXP (x, 1), false);
4492 SUBST (XEXP (x, 2), true);
4494 temp = true, true = false, false = temp, cond = XEXP (x, 0);
4496 /* It is possible that the conditional has been simplified out. */
4497 true_code = GET_CODE (cond);
4498 comparison_p = GET_RTX_CLASS (true_code) == '<';
4501 /* If the two arms are identical, we don't need the comparison. */
4503 if (rtx_equal_p (true, false) && ! side_effects_p (cond))
4504 return true;
4506 /* Convert a == b ? b : a to "a". */
4507 if (true_code == EQ && ! side_effects_p (cond)
4508 && rtx_equal_p (XEXP (cond, 0), false)
4509 && rtx_equal_p (XEXP (cond, 1), true))
4510 return false;
4511 else if (true_code == NE && ! side_effects_p (cond)
4512 && rtx_equal_p (XEXP (cond, 0), true)
4513 && rtx_equal_p (XEXP (cond, 1), false))
4514 return true;
4516 /* Look for cases where we have (abs x) or (neg (abs X)). */
4518 if (GET_MODE_CLASS (mode) == MODE_INT
4519 && GET_CODE (false) == NEG
4520 && rtx_equal_p (true, XEXP (false, 0))
4521 && comparison_p
4522 && rtx_equal_p (true, XEXP (cond, 0))
4523 && ! side_effects_p (true))
4524 switch (true_code)
4526 case GT:
4527 case GE:
4528 return gen_unary (ABS, mode, mode, true);
4529 case LT:
4530 case LE:
4531 return gen_unary (NEG, mode, mode, gen_unary (ABS, mode, mode, true));
4532 default:
4533 break;
4536 /* Look for MIN or MAX. */
4538 if ((! FLOAT_MODE_P (mode) || flag_fast_math)
4539 && comparison_p
4540 && rtx_equal_p (XEXP (cond, 0), true)
4541 && rtx_equal_p (XEXP (cond, 1), false)
4542 && ! side_effects_p (cond))
4543 switch (true_code)
4545 case GE:
4546 case GT:
4547 return gen_binary (SMAX, mode, true, false);
4548 case LE:
4549 case LT:
4550 return gen_binary (SMIN, mode, true, false);
4551 case GEU:
4552 case GTU:
4553 return gen_binary (UMAX, mode, true, false);
4554 case LEU:
4555 case LTU:
4556 return gen_binary (UMIN, mode, true, false);
4557 default:
4558 break;
4561 /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
4562 second operand is zero, this can be done as (OP Z (mult COND C2)) where
4563 C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
4564 SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
4565 We can do this kind of thing in some cases when STORE_FLAG_VALUE is
4566 neither 1 or -1, but it isn't worth checking for. */
4568 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
4569 && comparison_p && mode != VOIDmode && ! side_effects_p (x))
4571 rtx t = make_compound_operation (true, SET);
4572 rtx f = make_compound_operation (false, SET);
4573 rtx cond_op0 = XEXP (cond, 0);
4574 rtx cond_op1 = XEXP (cond, 1);
4575 enum rtx_code op = NIL, extend_op = NIL;
4576 enum machine_mode m = mode;
4577 rtx z = 0, c1 = NULL_RTX;
4579 if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
4580 || GET_CODE (t) == IOR || GET_CODE (t) == XOR
4581 || GET_CODE (t) == ASHIFT
4582 || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
4583 && rtx_equal_p (XEXP (t, 0), f))
4584 c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
4586 /* If an identity-zero op is commutative, check whether there
4587 would be a match if we swapped the operands. */
4588 else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
4589 || GET_CODE (t) == XOR)
4590 && rtx_equal_p (XEXP (t, 1), f))
4591 c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
4592 else if (GET_CODE (t) == SIGN_EXTEND
4593 && (GET_CODE (XEXP (t, 0)) == PLUS
4594 || GET_CODE (XEXP (t, 0)) == MINUS
4595 || GET_CODE (XEXP (t, 0)) == IOR
4596 || GET_CODE (XEXP (t, 0)) == XOR
4597 || GET_CODE (XEXP (t, 0)) == ASHIFT
4598 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
4599 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
4600 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
4601 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
4602 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
4603 && (num_sign_bit_copies (f, GET_MODE (f))
4604 > (GET_MODE_BITSIZE (mode)
4605 - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 0))))))
4607 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
4608 extend_op = SIGN_EXTEND;
4609 m = GET_MODE (XEXP (t, 0));
4611 else if (GET_CODE (t) == SIGN_EXTEND
4612 && (GET_CODE (XEXP (t, 0)) == PLUS
4613 || GET_CODE (XEXP (t, 0)) == IOR
4614 || GET_CODE (XEXP (t, 0)) == XOR)
4615 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
4616 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
4617 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
4618 && (num_sign_bit_copies (f, GET_MODE (f))
4619 > (GET_MODE_BITSIZE (mode)
4620 - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 1))))))
4622 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
4623 extend_op = SIGN_EXTEND;
4624 m = GET_MODE (XEXP (t, 0));
4626 else if (GET_CODE (t) == ZERO_EXTEND
4627 && (GET_CODE (XEXP (t, 0)) == PLUS
4628 || GET_CODE (XEXP (t, 0)) == MINUS
4629 || GET_CODE (XEXP (t, 0)) == IOR
4630 || GET_CODE (XEXP (t, 0)) == XOR
4631 || GET_CODE (XEXP (t, 0)) == ASHIFT
4632 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
4633 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
4634 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
4635 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4636 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
4637 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
4638 && ((nonzero_bits (f, GET_MODE (f))
4639 & ~ GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
4640 == 0))
4642 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
4643 extend_op = ZERO_EXTEND;
4644 m = GET_MODE (XEXP (t, 0));
4646 else if (GET_CODE (t) == ZERO_EXTEND
4647 && (GET_CODE (XEXP (t, 0)) == PLUS
4648 || GET_CODE (XEXP (t, 0)) == IOR
4649 || GET_CODE (XEXP (t, 0)) == XOR)
4650 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
4651 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4652 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
4653 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
4654 && ((nonzero_bits (f, GET_MODE (f))
4655 & ~ GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
4656 == 0))
4658 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
4659 extend_op = ZERO_EXTEND;
4660 m = GET_MODE (XEXP (t, 0));
4663 if (z)
4665 temp = subst (gen_binary (true_code, m, cond_op0, cond_op1),
4666 pc_rtx, pc_rtx, 0, 0);
4667 temp = gen_binary (MULT, m, temp,
4668 gen_binary (MULT, m, c1, const_true_rtx));
4669 temp = subst (temp, pc_rtx, pc_rtx, 0, 0);
4670 temp = gen_binary (op, m, gen_lowpart_for_combine (m, z), temp);
4672 if (extend_op != NIL)
4673 temp = gen_unary (extend_op, mode, m, temp);
4675 return temp;
4679 /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
4680 1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
4681 negation of a single bit, we can convert this operation to a shift. We
4682 can actually do this more generally, but it doesn't seem worth it. */
4684 if (true_code == NE && XEXP (cond, 1) == const0_rtx
4685 && false == const0_rtx && GET_CODE (true) == CONST_INT
4686 && ((1 == nonzero_bits (XEXP (cond, 0), mode)
4687 && (i = exact_log2 (INTVAL (true))) >= 0)
4688 || ((num_sign_bit_copies (XEXP (cond, 0), mode)
4689 == GET_MODE_BITSIZE (mode))
4690 && (i = exact_log2 (- INTVAL (true))) >= 0)))
4691 return
4692 simplify_shift_const (NULL_RTX, ASHIFT, mode,
4693 gen_lowpart_for_combine (mode, XEXP (cond, 0)), i);
4695 return x;
4698 /* Simplify X, a SET expression. Return the new expression. */
4700 static rtx
4701 simplify_set (x)
4702 rtx x;
4704 rtx src = SET_SRC (x);
4705 rtx dest = SET_DEST (x);
4706 enum machine_mode mode
4707 = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
4708 rtx other_insn;
4709 rtx *cc_use;
4711 /* (set (pc) (return)) gets written as (return). */
4712 if (GET_CODE (dest) == PC && GET_CODE (src) == RETURN)
4713 return src;
4715 /* Now that we know for sure which bits of SRC we are using, see if we can
4716 simplify the expression for the object knowing that we only need the
4717 low-order bits. */
4719 if (GET_MODE_CLASS (mode) == MODE_INT)
4721 src = force_to_mode (src, mode, GET_MODE_MASK (mode), NULL_RTX, 0);
4722 SUBST (SET_SRC (x), src);
4725 /* If we are setting CC0 or if the source is a COMPARE, look for the use of
4726 the comparison result and try to simplify it unless we already have used
4727 undobuf.other_insn. */
4728 if ((GET_CODE (src) == COMPARE
4729 #ifdef HAVE_cc0
4730 || dest == cc0_rtx
4731 #endif
4733 && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
4734 && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
4735 && GET_RTX_CLASS (GET_CODE (*cc_use)) == '<'
4736 && rtx_equal_p (XEXP (*cc_use, 0), dest))
4738 enum rtx_code old_code = GET_CODE (*cc_use);
4739 enum rtx_code new_code;
4740 rtx op0, op1;
4741 int other_changed = 0;
4742 enum machine_mode compare_mode = GET_MODE (dest);
4744 if (GET_CODE (src) == COMPARE)
4745 op0 = XEXP (src, 0), op1 = XEXP (src, 1);
4746 else
4747 op0 = src, op1 = const0_rtx;
4749 /* Simplify our comparison, if possible. */
4750 new_code = simplify_comparison (old_code, &op0, &op1);
4752 #ifdef EXTRA_CC_MODES
4753 /* If this machine has CC modes other than CCmode, check to see if we
4754 need to use a different CC mode here. */
4755 compare_mode = SELECT_CC_MODE (new_code, op0, op1);
4756 #endif /* EXTRA_CC_MODES */
4758 #if !defined (HAVE_cc0) && defined (EXTRA_CC_MODES)
4759 /* If the mode changed, we have to change SET_DEST, the mode in the
4760 compare, and the mode in the place SET_DEST is used. If SET_DEST is
4761 a hard register, just build new versions with the proper mode. If it
4762 is a pseudo, we lose unless it is only time we set the pseudo, in
4763 which case we can safely change its mode. */
4764 if (compare_mode != GET_MODE (dest))
4766 int regno = REGNO (dest);
4767 rtx new_dest = gen_rtx_REG (compare_mode, regno);
4769 if (regno < FIRST_PSEUDO_REGISTER
4770 || (REG_N_SETS (regno) == 1 && ! REG_USERVAR_P (dest)))
4772 if (regno >= FIRST_PSEUDO_REGISTER)
4773 SUBST (regno_reg_rtx[regno], new_dest);
4775 SUBST (SET_DEST (x), new_dest);
4776 SUBST (XEXP (*cc_use, 0), new_dest);
4777 other_changed = 1;
4779 dest = new_dest;
4782 #endif
4784 /* If the code changed, we have to build a new comparison in
4785 undobuf.other_insn. */
4786 if (new_code != old_code)
4788 unsigned HOST_WIDE_INT mask;
4790 SUBST (*cc_use, gen_rtx_combine (new_code, GET_MODE (*cc_use),
4791 dest, const0_rtx));
4793 /* If the only change we made was to change an EQ into an NE or
4794 vice versa, OP0 has only one bit that might be nonzero, and OP1
4795 is zero, check if changing the user of the condition code will
4796 produce a valid insn. If it won't, we can keep the original code
4797 in that insn by surrounding our operation with an XOR. */
4799 if (((old_code == NE && new_code == EQ)
4800 || (old_code == EQ && new_code == NE))
4801 && ! other_changed && op1 == const0_rtx
4802 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
4803 && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
4805 rtx pat = PATTERN (other_insn), note = 0;
4807 if ((recog_for_combine (&pat, other_insn, &note) < 0
4808 && ! check_asm_operands (pat)))
4810 PUT_CODE (*cc_use, old_code);
4811 other_insn = 0;
4813 op0 = gen_binary (XOR, GET_MODE (op0), op0, GEN_INT (mask));
4817 other_changed = 1;
4820 if (other_changed)
4821 undobuf.other_insn = other_insn;
4823 #ifdef HAVE_cc0
4824 /* If we are now comparing against zero, change our source if
4825 needed. If we do not use cc0, we always have a COMPARE. */
4826 if (op1 == const0_rtx && dest == cc0_rtx)
4828 SUBST (SET_SRC (x), op0);
4829 src = op0;
4831 else
4832 #endif
4834 /* Otherwise, if we didn't previously have a COMPARE in the
4835 correct mode, we need one. */
4836 if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
4838 SUBST (SET_SRC (x),
4839 gen_rtx_combine (COMPARE, compare_mode, op0, op1));
4840 src = SET_SRC (x);
4842 else
4844 /* Otherwise, update the COMPARE if needed. */
4845 SUBST (XEXP (src, 0), op0);
4846 SUBST (XEXP (src, 1), op1);
4849 else
4851 /* Get SET_SRC in a form where we have placed back any
4852 compound expressions. Then do the checks below. */
4853 src = make_compound_operation (src, SET);
4854 SUBST (SET_SRC (x), src);
4857 /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
4858 and X being a REG or (subreg (reg)), we may be able to convert this to
4859 (set (subreg:m2 x) (op)).
4861 We can always do this if M1 is narrower than M2 because that means that
4862 we only care about the low bits of the result.
4864 However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
4865 perform a narrower operation than requested since the high-order bits will
4866 be undefined. On machine where it is defined, this transformation is safe
4867 as long as M1 and M2 have the same number of words. */
4869 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
4870 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (src))) != 'o'
4871 && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
4872 / UNITS_PER_WORD)
4873 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
4874 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
4875 #ifndef WORD_REGISTER_OPERATIONS
4876 && (GET_MODE_SIZE (GET_MODE (src))
4877 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
4878 #endif
4879 #ifdef CLASS_CANNOT_CHANGE_SIZE
4880 && ! (GET_CODE (dest) == REG && REGNO (dest) < FIRST_PSEUDO_REGISTER
4881 && (TEST_HARD_REG_BIT
4882 (reg_class_contents[(int) CLASS_CANNOT_CHANGE_SIZE],
4883 REGNO (dest)))
4884 && (GET_MODE_SIZE (GET_MODE (src))
4885 != GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))))
4886 #endif
4887 && (GET_CODE (dest) == REG
4888 || (GET_CODE (dest) == SUBREG
4889 && GET_CODE (SUBREG_REG (dest)) == REG)))
4891 SUBST (SET_DEST (x),
4892 gen_lowpart_for_combine (GET_MODE (SUBREG_REG (src)),
4893 dest));
4894 SUBST (SET_SRC (x), SUBREG_REG (src));
4896 src = SET_SRC (x), dest = SET_DEST (x);
4899 #ifdef LOAD_EXTEND_OP
4900 /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
4901 would require a paradoxical subreg. Replace the subreg with a
4902 zero_extend to avoid the reload that would otherwise be required. */
4904 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
4905 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != NIL
4906 && SUBREG_WORD (src) == 0
4907 && (GET_MODE_SIZE (GET_MODE (src))
4908 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
4909 && GET_CODE (SUBREG_REG (src)) == MEM)
4911 SUBST (SET_SRC (x),
4912 gen_rtx_combine (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
4913 GET_MODE (src), XEXP (src, 0)));
4915 src = SET_SRC (x);
4917 #endif
4919 /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
4920 are comparing an item known to be 0 or -1 against 0, use a logical
4921 operation instead. Check for one of the arms being an IOR of the other
4922 arm with some value. We compute three terms to be IOR'ed together. In
4923 practice, at most two will be nonzero. Then we do the IOR's. */
4925 if (GET_CODE (dest) != PC
4926 && GET_CODE (src) == IF_THEN_ELSE
4927 && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
4928 && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
4929 && XEXP (XEXP (src, 0), 1) == const0_rtx
4930 && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
4931 #ifdef HAVE_conditional_move
4932 && ! can_conditionally_move_p (GET_MODE (src))
4933 #endif
4934 && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
4935 GET_MODE (XEXP (XEXP (src, 0), 0)))
4936 == GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (src, 0), 0))))
4937 && ! side_effects_p (src))
4939 rtx true = (GET_CODE (XEXP (src, 0)) == NE
4940 ? XEXP (src, 1) : XEXP (src, 2));
4941 rtx false = (GET_CODE (XEXP (src, 0)) == NE
4942 ? XEXP (src, 2) : XEXP (src, 1));
4943 rtx term1 = const0_rtx, term2, term3;
4945 if (GET_CODE (true) == IOR && rtx_equal_p (XEXP (true, 0), false))
4946 term1 = false, true = XEXP (true, 1), false = const0_rtx;
4947 else if (GET_CODE (true) == IOR
4948 && rtx_equal_p (XEXP (true, 1), false))
4949 term1 = false, true = XEXP (true, 0), false = const0_rtx;
4950 else if (GET_CODE (false) == IOR
4951 && rtx_equal_p (XEXP (false, 0), true))
4952 term1 = true, false = XEXP (false, 1), true = const0_rtx;
4953 else if (GET_CODE (false) == IOR
4954 && rtx_equal_p (XEXP (false, 1), true))
4955 term1 = true, false = XEXP (false, 0), true = const0_rtx;
4957 term2 = gen_binary (AND, GET_MODE (src), XEXP (XEXP (src, 0), 0), true);
4958 term3 = gen_binary (AND, GET_MODE (src),
4959 gen_unary (NOT, GET_MODE (src), GET_MODE (src),
4960 XEXP (XEXP (src, 0), 0)),
4961 false);
4963 SUBST (SET_SRC (x),
4964 gen_binary (IOR, GET_MODE (src),
4965 gen_binary (IOR, GET_MODE (src), term1, term2),
4966 term3));
4968 src = SET_SRC (x);
4971 #ifdef HAVE_conditional_arithmetic
4972 /* If we have conditional arithmetic and the operand of a SET is
4973 a conditional expression, replace this with an IF_THEN_ELSE.
4974 We can either have a conditional expression or a MULT of that expression
4975 with a constant. */
4976 if ((GET_RTX_CLASS (GET_CODE (src)) == '1'
4977 || GET_RTX_CLASS (GET_CODE (src)) == '2'
4978 || GET_RTX_CLASS (GET_CODE (src)) == 'c')
4979 && (GET_RTX_CLASS (GET_CODE (XEXP (src, 0))) == '<'
4980 || (GET_CODE (XEXP (src, 0)) == MULT
4981 && GET_RTX_CLASS (GET_CODE (XEXP (XEXP (src, 0), 0))) == '<'
4982 && GET_CODE (XEXP (XEXP (src, 0), 1)) == CONST_INT)))
4984 rtx cond = XEXP (src, 0);
4985 rtx true_val = const1_rtx;
4986 rtx false_arm, true_arm;
4988 if (GET_CODE (cond) == MULT)
4990 true_val = XEXP (cond, 1);
4991 cond = XEXP (cond, 0);
4994 if (GET_RTX_CLASS (GET_CODE (src)) == '1')
4996 true_arm = gen_unary (GET_CODE (src), GET_MODE (src),
4997 GET_MODE (XEXP (src, 0)), true_val);
4998 false_arm = gen_unary (GET_CODE (src), GET_MODE (src),
4999 GET_MODE (XEXP (src, 0)), const0_rtx);
5001 else
5003 true_arm = gen_binary (GET_CODE (src), GET_MODE (src),
5004 true_val, XEXP (src, 1));
5005 false_arm = gen_binary (GET_CODE (src), GET_MODE (src),
5006 const0_rtx, XEXP (src, 1));
5009 /* Canonicalize if true_arm is the simpler one. */
5010 if (GET_RTX_CLASS (GET_CODE (true_arm)) == 'o'
5011 && GET_RTX_CLASS (GET_CODE (false_arm)) != 'o'
5012 && reversible_comparison_p (cond))
5014 rtx temp = true_arm;
5016 true_arm = false_arm;
5017 false_arm = temp;
5019 cond = gen_rtx_combine (reverse_condition (GET_CODE (cond)),
5020 GET_MODE (cond), XEXP (cond, 0),
5021 XEXP (cond, 1));
5024 src = gen_rtx_combine (IF_THEN_ELSE, GET_MODE (src),
5025 gen_rtx_combine (GET_CODE (cond), VOIDmode,
5026 XEXP (cond, 0),
5027 XEXP (cond, 1)),
5028 true_arm, false_arm);
5029 SUBST (SET_SRC (x), src);
5031 #endif
5033 /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
5034 whole thing fail. */
5035 if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
5036 return src;
5037 else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
5038 return dest;
5039 else
5040 /* Convert this into a field assignment operation, if possible. */
5041 return make_field_assignment (x);
5044 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
5045 result. LAST is nonzero if this is the last retry. */
5047 static rtx
5048 simplify_logical (x, last)
5049 rtx x;
5050 int last;
5052 enum machine_mode mode = GET_MODE (x);
5053 rtx op0 = XEXP (x, 0);
5054 rtx op1 = XEXP (x, 1);
5056 switch (GET_CODE (x))
5058 case AND:
5059 /* Convert (A ^ B) & A to A & (~ B) since the latter is often a single
5060 insn (and may simplify more). */
5061 if (GET_CODE (op0) == XOR
5062 && rtx_equal_p (XEXP (op0, 0), op1)
5063 && ! side_effects_p (op1))
5064 x = gen_binary (AND, mode,
5065 gen_unary (NOT, mode, mode, XEXP (op0, 1)), op1);
5067 if (GET_CODE (op0) == XOR
5068 && rtx_equal_p (XEXP (op0, 1), op1)
5069 && ! side_effects_p (op1))
5070 x = gen_binary (AND, mode,
5071 gen_unary (NOT, mode, mode, XEXP (op0, 0)), op1);
5073 /* Similarly for (~ (A ^ B)) & A. */
5074 if (GET_CODE (op0) == NOT
5075 && GET_CODE (XEXP (op0, 0)) == XOR
5076 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), op1)
5077 && ! side_effects_p (op1))
5078 x = gen_binary (AND, mode, XEXP (XEXP (op0, 0), 1), op1);
5080 if (GET_CODE (op0) == NOT
5081 && GET_CODE (XEXP (op0, 0)) == XOR
5082 && rtx_equal_p (XEXP (XEXP (op0, 0), 1), op1)
5083 && ! side_effects_p (op1))
5084 x = gen_binary (AND, mode, XEXP (XEXP (op0, 0), 0), op1);
5086 /* We can call simplify_and_const_int only if we don't lose
5087 any (sign) bits when converting INTVAL (op1) to
5088 "unsigned HOST_WIDE_INT". */
5089 if (GET_CODE (op1) == CONST_INT
5090 && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5091 || INTVAL (op1) > 0))
5093 x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
5095 /* If we have (ior (and (X C1) C2)) and the next restart would be
5096 the last, simplify this by making C1 as small as possible
5097 and then exit. */
5098 if (last
5099 && GET_CODE (x) == IOR && GET_CODE (op0) == AND
5100 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5101 && GET_CODE (op1) == CONST_INT)
5102 return gen_binary (IOR, mode,
5103 gen_binary (AND, mode, XEXP (op0, 0),
5104 GEN_INT (INTVAL (XEXP (op0, 1))
5105 & ~ INTVAL (op1))), op1);
5107 if (GET_CODE (x) != AND)
5108 return x;
5110 if (GET_RTX_CLASS (GET_CODE (x)) == 'c'
5111 || GET_RTX_CLASS (GET_CODE (x)) == '2')
5112 op0 = XEXP (x, 0), op1 = XEXP (x, 1);
5115 /* Convert (A | B) & A to A. */
5116 if (GET_CODE (op0) == IOR
5117 && (rtx_equal_p (XEXP (op0, 0), op1)
5118 || rtx_equal_p (XEXP (op0, 1), op1))
5119 && ! side_effects_p (XEXP (op0, 0))
5120 && ! side_effects_p (XEXP (op0, 1)))
5121 return op1;
5123 /* In the following group of tests (and those in case IOR below),
5124 we start with some combination of logical operations and apply
5125 the distributive law followed by the inverse distributive law.
5126 Most of the time, this results in no change. However, if some of
5127 the operands are the same or inverses of each other, simplifications
5128 will result.
5130 For example, (and (ior A B) (not B)) can occur as the result of
5131 expanding a bit field assignment. When we apply the distributive
5132 law to this, we get (ior (and (A (not B))) (and (B (not B)))),
5133 which then simplifies to (and (A (not B))).
5135 If we have (and (ior A B) C), apply the distributive law and then
5136 the inverse distributive law to see if things simplify. */
5138 if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
5140 x = apply_distributive_law
5141 (gen_binary (GET_CODE (op0), mode,
5142 gen_binary (AND, mode, XEXP (op0, 0), op1),
5143 gen_binary (AND, mode, XEXP (op0, 1), op1)));
5144 if (GET_CODE (x) != AND)
5145 return x;
5148 if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
5149 return apply_distributive_law
5150 (gen_binary (GET_CODE (op1), mode,
5151 gen_binary (AND, mode, XEXP (op1, 0), op0),
5152 gen_binary (AND, mode, XEXP (op1, 1), op0)));
5154 /* Similarly, taking advantage of the fact that
5155 (and (not A) (xor B C)) == (xor (ior A B) (ior A C)) */
5157 if (GET_CODE (op0) == NOT && GET_CODE (op1) == XOR)
5158 return apply_distributive_law
5159 (gen_binary (XOR, mode,
5160 gen_binary (IOR, mode, XEXP (op0, 0), XEXP (op1, 0)),
5161 gen_binary (IOR, mode, XEXP (op0, 0), XEXP (op1, 1))));
5163 else if (GET_CODE (op1) == NOT && GET_CODE (op0) == XOR)
5164 return apply_distributive_law
5165 (gen_binary (XOR, mode,
5166 gen_binary (IOR, mode, XEXP (op1, 0), XEXP (op0, 0)),
5167 gen_binary (IOR, mode, XEXP (op1, 0), XEXP (op0, 1))));
5168 break;
5170 case IOR:
5171 /* (ior A C) is C if all bits of A that might be nonzero are on in C. */
5172 if (GET_CODE (op1) == CONST_INT
5173 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5174 && (nonzero_bits (op0, mode) & ~ INTVAL (op1)) == 0)
5175 return op1;
5177 /* Convert (A & B) | A to A. */
5178 if (GET_CODE (op0) == AND
5179 && (rtx_equal_p (XEXP (op0, 0), op1)
5180 || rtx_equal_p (XEXP (op0, 1), op1))
5181 && ! side_effects_p (XEXP (op0, 0))
5182 && ! side_effects_p (XEXP (op0, 1)))
5183 return op1;
5185 /* If we have (ior (and A B) C), apply the distributive law and then
5186 the inverse distributive law to see if things simplify. */
5188 if (GET_CODE (op0) == AND)
5190 x = apply_distributive_law
5191 (gen_binary (AND, mode,
5192 gen_binary (IOR, mode, XEXP (op0, 0), op1),
5193 gen_binary (IOR, mode, XEXP (op0, 1), op1)));
5195 if (GET_CODE (x) != IOR)
5196 return x;
5199 if (GET_CODE (op1) == AND)
5201 x = apply_distributive_law
5202 (gen_binary (AND, mode,
5203 gen_binary (IOR, mode, XEXP (op1, 0), op0),
5204 gen_binary (IOR, mode, XEXP (op1, 1), op0)));
5206 if (GET_CODE (x) != IOR)
5207 return x;
5210 /* Convert (ior (ashift A CX) (lshiftrt A CY)) where CX+CY equals the
5211 mode size to (rotate A CX). */
5213 if (((GET_CODE (op0) == ASHIFT && GET_CODE (op1) == LSHIFTRT)
5214 || (GET_CODE (op1) == ASHIFT && GET_CODE (op0) == LSHIFTRT))
5215 && rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
5216 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5217 && GET_CODE (XEXP (op1, 1)) == CONST_INT
5218 && (INTVAL (XEXP (op0, 1)) + INTVAL (XEXP (op1, 1))
5219 == GET_MODE_BITSIZE (mode)))
5220 return gen_rtx_ROTATE (mode, XEXP (op0, 0),
5221 (GET_CODE (op0) == ASHIFT
5222 ? XEXP (op0, 1) : XEXP (op1, 1)));
5224 /* If OP0 is (ashiftrt (plus ...) C), it might actually be
5225 a (sign_extend (plus ...)). If so, OP1 is a CONST_INT, and the PLUS
5226 does not affect any of the bits in OP1, it can really be done
5227 as a PLUS and we can associate. We do this by seeing if OP1
5228 can be safely shifted left C bits. */
5229 if (GET_CODE (op1) == CONST_INT && GET_CODE (op0) == ASHIFTRT
5230 && GET_CODE (XEXP (op0, 0)) == PLUS
5231 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
5232 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5233 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT)
5235 int count = INTVAL (XEXP (op0, 1));
5236 HOST_WIDE_INT mask = INTVAL (op1) << count;
5238 if (mask >> count == INTVAL (op1)
5239 && (mask & nonzero_bits (XEXP (op0, 0), mode)) == 0)
5241 SUBST (XEXP (XEXP (op0, 0), 1),
5242 GEN_INT (INTVAL (XEXP (XEXP (op0, 0), 1)) | mask));
5243 return op0;
5246 break;
5248 case XOR:
5249 /* If we are XORing two things that have no bits in common,
5250 convert them into an IOR. This helps to detect rotation encoded
5251 using those methods and possibly other simplifications. */
5253 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5254 && (nonzero_bits (op0, mode)
5255 & nonzero_bits (op1, mode)) == 0)
5256 return (gen_binary (IOR, mode, op0, op1));
5258 /* Convert (XOR (NOT x) (NOT y)) to (XOR x y).
5259 Also convert (XOR (NOT x) y) to (NOT (XOR x y)), similarly for
5260 (NOT y). */
5262 int num_negated = 0;
5264 if (GET_CODE (op0) == NOT)
5265 num_negated++, op0 = XEXP (op0, 0);
5266 if (GET_CODE (op1) == NOT)
5267 num_negated++, op1 = XEXP (op1, 0);
5269 if (num_negated == 2)
5271 SUBST (XEXP (x, 0), op0);
5272 SUBST (XEXP (x, 1), op1);
5274 else if (num_negated == 1)
5275 return gen_unary (NOT, mode, mode, gen_binary (XOR, mode, op0, op1));
5278 /* Convert (xor (and A B) B) to (and (not A) B). The latter may
5279 correspond to a machine insn or result in further simplifications
5280 if B is a constant. */
5282 if (GET_CODE (op0) == AND
5283 && rtx_equal_p (XEXP (op0, 1), op1)
5284 && ! side_effects_p (op1))
5285 return gen_binary (AND, mode,
5286 gen_unary (NOT, mode, mode, XEXP (op0, 0)),
5287 op1);
5289 else if (GET_CODE (op0) == AND
5290 && rtx_equal_p (XEXP (op0, 0), op1)
5291 && ! side_effects_p (op1))
5292 return gen_binary (AND, mode,
5293 gen_unary (NOT, mode, mode, XEXP (op0, 1)),
5294 op1);
5296 /* (xor (comparison foo bar) (const_int 1)) can become the reversed
5297 comparison if STORE_FLAG_VALUE is 1. */
5298 if (STORE_FLAG_VALUE == 1
5299 && op1 == const1_rtx
5300 && GET_RTX_CLASS (GET_CODE (op0)) == '<'
5301 && reversible_comparison_p (op0))
5302 return gen_rtx_combine (reverse_condition (GET_CODE (op0)),
5303 mode, XEXP (op0, 0), XEXP (op0, 1));
5305 /* (lshiftrt foo C) where C is the number of bits in FOO minus 1
5306 is (lt foo (const_int 0)), so we can perform the above
5307 simplification if STORE_FLAG_VALUE is 1. */
5309 if (STORE_FLAG_VALUE == 1
5310 && op1 == const1_rtx
5311 && GET_CODE (op0) == LSHIFTRT
5312 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5313 && INTVAL (XEXP (op0, 1)) == GET_MODE_BITSIZE (mode) - 1)
5314 return gen_rtx_combine (GE, mode, XEXP (op0, 0), const0_rtx);
5316 /* (xor (comparison foo bar) (const_int sign-bit))
5317 when STORE_FLAG_VALUE is the sign bit. */
5318 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5319 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
5320 == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
5321 && op1 == const_true_rtx
5322 && GET_RTX_CLASS (GET_CODE (op0)) == '<'
5323 && reversible_comparison_p (op0))
5324 return gen_rtx_combine (reverse_condition (GET_CODE (op0)),
5325 mode, XEXP (op0, 0), XEXP (op0, 1));
5327 break;
5329 default:
5330 abort ();
5333 return x;
5336 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
5337 operations" because they can be replaced with two more basic operations.
5338 ZERO_EXTEND is also considered "compound" because it can be replaced with
5339 an AND operation, which is simpler, though only one operation.
5341 The function expand_compound_operation is called with an rtx expression
5342 and will convert it to the appropriate shifts and AND operations,
5343 simplifying at each stage.
5345 The function make_compound_operation is called to convert an expression
5346 consisting of shifts and ANDs into the equivalent compound expression.
5347 It is the inverse of this function, loosely speaking. */
5349 static rtx
5350 expand_compound_operation (x)
5351 rtx x;
5353 int pos = 0, len;
5354 int unsignedp = 0;
5355 int modewidth;
5356 rtx tem;
5358 switch (GET_CODE (x))
5360 case ZERO_EXTEND:
5361 unsignedp = 1;
5362 case SIGN_EXTEND:
5363 /* We can't necessarily use a const_int for a multiword mode;
5364 it depends on implicitly extending the value.
5365 Since we don't know the right way to extend it,
5366 we can't tell whether the implicit way is right.
5368 Even for a mode that is no wider than a const_int,
5369 we can't win, because we need to sign extend one of its bits through
5370 the rest of it, and we don't know which bit. */
5371 if (GET_CODE (XEXP (x, 0)) == CONST_INT)
5372 return x;
5374 /* Return if (subreg:MODE FROM 0) is not a safe replacement for
5375 (zero_extend:MODE FROM) or (sign_extend:MODE FROM). It is for any MEM
5376 because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
5377 reloaded. If not for that, MEM's would very rarely be safe.
5379 Reject MODEs bigger than a word, because we might not be able
5380 to reference a two-register group starting with an arbitrary register
5381 (and currently gen_lowpart might crash for a SUBREG). */
5383 if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
5384 return x;
5386 len = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)));
5387 /* If the inner object has VOIDmode (the only way this can happen
5388 is if it is a ASM_OPERANDS), we can't do anything since we don't
5389 know how much masking to do. */
5390 if (len == 0)
5391 return x;
5393 break;
5395 case ZERO_EXTRACT:
5396 unsignedp = 1;
5397 case SIGN_EXTRACT:
5398 /* If the operand is a CLOBBER, just return it. */
5399 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
5400 return XEXP (x, 0);
5402 if (GET_CODE (XEXP (x, 1)) != CONST_INT
5403 || GET_CODE (XEXP (x, 2)) != CONST_INT
5404 || GET_MODE (XEXP (x, 0)) == VOIDmode)
5405 return x;
5407 len = INTVAL (XEXP (x, 1));
5408 pos = INTVAL (XEXP (x, 2));
5410 /* If this goes outside the object being extracted, replace the object
5411 with a (use (mem ...)) construct that only combine understands
5412 and is used only for this purpose. */
5413 if (len + pos > GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
5414 SUBST (XEXP (x, 0), gen_rtx_USE (GET_MODE (x), XEXP (x, 0)));
5416 if (BITS_BIG_ENDIAN)
5417 pos = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - len - pos;
5419 break;
5421 default:
5422 return x;
5425 /* We can optimize some special cases of ZERO_EXTEND. */
5426 if (GET_CODE (x) == ZERO_EXTEND)
5428 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
5429 know that the last value didn't have any inappropriate bits
5430 set. */
5431 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
5432 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
5433 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5434 && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
5435 & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5436 return XEXP (XEXP (x, 0), 0);
5438 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
5439 if (GET_CODE (XEXP (x, 0)) == SUBREG
5440 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
5441 && subreg_lowpart_p (XEXP (x, 0))
5442 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5443 && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
5444 & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5445 return SUBREG_REG (XEXP (x, 0));
5447 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
5448 is a comparison and STORE_FLAG_VALUE permits. This is like
5449 the first case, but it works even when GET_MODE (x) is larger
5450 than HOST_WIDE_INT. */
5451 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
5452 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
5453 && GET_RTX_CLASS (GET_CODE (XEXP (XEXP (x, 0), 0))) == '<'
5454 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5455 <= HOST_BITS_PER_WIDE_INT)
5456 && ((HOST_WIDE_INT) STORE_FLAG_VALUE
5457 & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5458 return XEXP (XEXP (x, 0), 0);
5460 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
5461 if (GET_CODE (XEXP (x, 0)) == SUBREG
5462 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
5463 && subreg_lowpart_p (XEXP (x, 0))
5464 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0)))) == '<'
5465 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5466 <= HOST_BITS_PER_WIDE_INT)
5467 && ((HOST_WIDE_INT) STORE_FLAG_VALUE
5468 & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5469 return SUBREG_REG (XEXP (x, 0));
5471 /* If sign extension is cheaper than zero extension, then use it
5472 if we know that no extraneous bits are set, and that the high
5473 bit is not set. */
5474 if (flag_expensive_optimizations
5475 && ((GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5476 && ((nonzero_bits (XEXP (x, 0), GET_MODE (x))
5477 & ~ (((unsigned HOST_WIDE_INT)
5478 GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
5479 >> 1))
5480 == 0))
5481 || (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
5482 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5483 <= HOST_BITS_PER_WIDE_INT)
5484 && (((HOST_WIDE_INT) STORE_FLAG_VALUE
5485 & ~ (((unsigned HOST_WIDE_INT)
5486 GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
5487 >> 1))
5488 == 0))))
5490 rtx temp = gen_rtx_SIGN_EXTEND (GET_MODE (x), XEXP (x, 0));
5492 if (rtx_cost (temp, SET) < rtx_cost (x, SET))
5493 return expand_compound_operation (temp);
5497 /* If we reach here, we want to return a pair of shifts. The inner
5498 shift is a left shift of BITSIZE - POS - LEN bits. The outer
5499 shift is a right shift of BITSIZE - LEN bits. It is arithmetic or
5500 logical depending on the value of UNSIGNEDP.
5502 If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
5503 converted into an AND of a shift.
5505 We must check for the case where the left shift would have a negative
5506 count. This can happen in a case like (x >> 31) & 255 on machines
5507 that can't shift by a constant. On those machines, we would first
5508 combine the shift with the AND to produce a variable-position
5509 extraction. Then the constant of 31 would be substituted in to produce
5510 a such a position. */
5512 modewidth = GET_MODE_BITSIZE (GET_MODE (x));
5513 if (modewidth >= pos - len)
5514 tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
5515 GET_MODE (x),
5516 simplify_shift_const (NULL_RTX, ASHIFT,
5517 GET_MODE (x),
5518 XEXP (x, 0),
5519 modewidth - pos - len),
5520 modewidth - len);
5522 else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
5523 tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
5524 simplify_shift_const (NULL_RTX, LSHIFTRT,
5525 GET_MODE (x),
5526 XEXP (x, 0), pos),
5527 ((HOST_WIDE_INT) 1 << len) - 1);
5528 else
5529 /* Any other cases we can't handle. */
5530 return x;
5533 /* If we couldn't do this for some reason, return the original
5534 expression. */
5535 if (GET_CODE (tem) == CLOBBER)
5536 return x;
5538 return tem;
5541 /* X is a SET which contains an assignment of one object into
5542 a part of another (such as a bit-field assignment, STRICT_LOW_PART,
5543 or certain SUBREGS). If possible, convert it into a series of
5544 logical operations.
5546 We half-heartedly support variable positions, but do not at all
5547 support variable lengths. */
5549 static rtx
5550 expand_field_assignment (x)
5551 rtx x;
5553 rtx inner;
5554 rtx pos; /* Always counts from low bit. */
5555 int len;
5556 rtx mask;
5557 enum machine_mode compute_mode;
5559 /* Loop until we find something we can't simplify. */
5560 while (1)
5562 if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
5563 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
5565 inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
5566 len = GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)));
5567 pos = GEN_INT (BITS_PER_WORD * SUBREG_WORD (XEXP (SET_DEST (x), 0)));
5569 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
5570 && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT)
5572 inner = XEXP (SET_DEST (x), 0);
5573 len = INTVAL (XEXP (SET_DEST (x), 1));
5574 pos = XEXP (SET_DEST (x), 2);
5576 /* If the position is constant and spans the width of INNER,
5577 surround INNER with a USE to indicate this. */
5578 if (GET_CODE (pos) == CONST_INT
5579 && INTVAL (pos) + len > GET_MODE_BITSIZE (GET_MODE (inner)))
5580 inner = gen_rtx_USE (GET_MODE (SET_DEST (x)), inner);
5582 if (BITS_BIG_ENDIAN)
5584 if (GET_CODE (pos) == CONST_INT)
5585 pos = GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner)) - len
5586 - INTVAL (pos));
5587 else if (GET_CODE (pos) == MINUS
5588 && GET_CODE (XEXP (pos, 1)) == CONST_INT
5589 && (INTVAL (XEXP (pos, 1))
5590 == GET_MODE_BITSIZE (GET_MODE (inner)) - len))
5591 /* If position is ADJUST - X, new position is X. */
5592 pos = XEXP (pos, 0);
5593 else
5594 pos = gen_binary (MINUS, GET_MODE (pos),
5595 GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner))
5596 - len),
5597 pos);
5601 /* A SUBREG between two modes that occupy the same numbers of words
5602 can be done by moving the SUBREG to the source. */
5603 else if (GET_CODE (SET_DEST (x)) == SUBREG
5604 && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
5605 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
5606 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
5607 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
5609 x = gen_rtx_SET (VOIDmode, SUBREG_REG (SET_DEST (x)),
5610 gen_lowpart_for_combine
5611 (GET_MODE (SUBREG_REG (SET_DEST (x))),
5612 SET_SRC (x)));
5613 continue;
5615 else
5616 break;
5618 while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
5619 inner = SUBREG_REG (inner);
5621 compute_mode = GET_MODE (inner);
5623 /* Don't attempt bitwise arithmetic on non-integral modes. */
5624 if (! INTEGRAL_MODE_P (compute_mode))
5626 enum machine_mode imode;
5628 /* Something is probably seriously wrong if this matches. */
5629 if (! FLOAT_MODE_P (compute_mode))
5630 break;
5632 /* Try to find an integral mode to pun with. */
5633 imode = mode_for_size (GET_MODE_BITSIZE (compute_mode), MODE_INT, 0);
5634 if (imode == BLKmode)
5635 break;
5637 compute_mode = imode;
5638 inner = gen_lowpart_for_combine (imode, inner);
5641 /* Compute a mask of LEN bits, if we can do this on the host machine. */
5642 if (len < HOST_BITS_PER_WIDE_INT)
5643 mask = GEN_INT (((HOST_WIDE_INT) 1 << len) - 1);
5644 else
5645 break;
5647 /* Now compute the equivalent expression. Make a copy of INNER
5648 for the SET_DEST in case it is a MEM into which we will substitute;
5649 we don't want shared RTL in that case. */
5650 x = gen_rtx_SET
5651 (VOIDmode, copy_rtx (inner),
5652 gen_binary (IOR, compute_mode,
5653 gen_binary (AND, compute_mode,
5654 gen_unary (NOT, compute_mode,
5655 compute_mode,
5656 gen_binary (ASHIFT,
5657 compute_mode,
5658 mask, pos)),
5659 inner),
5660 gen_binary (ASHIFT, compute_mode,
5661 gen_binary (AND, compute_mode,
5662 gen_lowpart_for_combine
5663 (compute_mode, SET_SRC (x)),
5664 mask),
5665 pos)));
5668 return x;
5671 /* Return an RTX for a reference to LEN bits of INNER. If POS_RTX is nonzero,
5672 it is an RTX that represents a variable starting position; otherwise,
5673 POS is the (constant) starting bit position (counted from the LSB).
5675 INNER may be a USE. This will occur when we started with a bitfield
5676 that went outside the boundary of the object in memory, which is
5677 allowed on most machines. To isolate this case, we produce a USE
5678 whose mode is wide enough and surround the MEM with it. The only
5679 code that understands the USE is this routine. If it is not removed,
5680 it will cause the resulting insn not to match.
5682 UNSIGNEDP is non-zero for an unsigned reference and zero for a
5683 signed reference.
5685 IN_DEST is non-zero if this is a reference in the destination of a
5686 SET. This is used when a ZERO_ or SIGN_EXTRACT isn't needed. If non-zero,
5687 a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
5688 be used.
5690 IN_COMPARE is non-zero if we are in a COMPARE. This means that a
5691 ZERO_EXTRACT should be built even for bits starting at bit 0.
5693 MODE is the desired mode of the result (if IN_DEST == 0).
5695 The result is an RTX for the extraction or NULL_RTX if the target
5696 can't handle it. */
5698 static rtx
5699 make_extraction (mode, inner, pos, pos_rtx, len,
5700 unsignedp, in_dest, in_compare)
5701 enum machine_mode mode;
5702 rtx inner;
5703 int pos;
5704 rtx pos_rtx;
5705 int len;
5706 int unsignedp;
5707 int in_dest, in_compare;
5709 /* This mode describes the size of the storage area
5710 to fetch the overall value from. Within that, we
5711 ignore the POS lowest bits, etc. */
5712 enum machine_mode is_mode = GET_MODE (inner);
5713 enum machine_mode inner_mode;
5714 enum machine_mode wanted_inner_mode = byte_mode;
5715 enum machine_mode wanted_inner_reg_mode = word_mode;
5716 enum machine_mode pos_mode = word_mode;
5717 enum machine_mode extraction_mode = word_mode;
5718 enum machine_mode tmode = mode_for_size (len, MODE_INT, 1);
5719 int spans_byte = 0;
5720 rtx new = 0;
5721 rtx orig_pos_rtx = pos_rtx;
5722 int orig_pos;
5724 /* Get some information about INNER and get the innermost object. */
5725 if (GET_CODE (inner) == USE)
5726 /* (use:SI (mem:QI foo)) stands for (mem:SI foo). */
5727 /* We don't need to adjust the position because we set up the USE
5728 to pretend that it was a full-word object. */
5729 spans_byte = 1, inner = XEXP (inner, 0);
5730 else if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
5732 /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
5733 consider just the QI as the memory to extract from.
5734 The subreg adds or removes high bits; its mode is
5735 irrelevant to the meaning of this extraction,
5736 since POS and LEN count from the lsb. */
5737 if (GET_CODE (SUBREG_REG (inner)) == MEM)
5738 is_mode = GET_MODE (SUBREG_REG (inner));
5739 inner = SUBREG_REG (inner);
5742 inner_mode = GET_MODE (inner);
5744 if (pos_rtx && GET_CODE (pos_rtx) == CONST_INT)
5745 pos = INTVAL (pos_rtx), pos_rtx = 0;
5747 /* See if this can be done without an extraction. We never can if the
5748 width of the field is not the same as that of some integer mode. For
5749 registers, we can only avoid the extraction if the position is at the
5750 low-order bit and this is either not in the destination or we have the
5751 appropriate STRICT_LOW_PART operation available.
5753 For MEM, we can avoid an extract if the field starts on an appropriate
5754 boundary and we can change the mode of the memory reference. However,
5755 we cannot directly access the MEM if we have a USE and the underlying
5756 MEM is not TMODE. This combination means that MEM was being used in a
5757 context where bits outside its mode were being referenced; that is only
5758 valid in bit-field insns. */
5760 if (tmode != BLKmode
5761 && ! (spans_byte && inner_mode != tmode)
5762 && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
5763 && GET_CODE (inner) != MEM
5764 && (! in_dest
5765 || (GET_CODE (inner) == REG
5766 && (movstrict_optab->handlers[(int) tmode].insn_code
5767 != CODE_FOR_nothing))))
5768 || (GET_CODE (inner) == MEM && pos_rtx == 0
5769 && (pos
5770 % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
5771 : BITS_PER_UNIT)) == 0
5772 /* We can't do this if we are widening INNER_MODE (it
5773 may not be aligned, for one thing). */
5774 && GET_MODE_BITSIZE (inner_mode) >= GET_MODE_BITSIZE (tmode)
5775 && (inner_mode == tmode
5776 || (! mode_dependent_address_p (XEXP (inner, 0))
5777 && ! MEM_VOLATILE_P (inner))))))
5779 /* If INNER is a MEM, make a new MEM that encompasses just the desired
5780 field. If the original and current mode are the same, we need not
5781 adjust the offset. Otherwise, we do if bytes big endian.
5783 If INNER is not a MEM, get a piece consisting of just the field
5784 of interest (in this case POS % BITS_PER_WORD must be 0). */
5786 if (GET_CODE (inner) == MEM)
5788 int offset;
5789 /* POS counts from lsb, but make OFFSET count in memory order. */
5790 if (BYTES_BIG_ENDIAN)
5791 offset = (GET_MODE_BITSIZE (is_mode) - len - pos) / BITS_PER_UNIT;
5792 else
5793 offset = pos / BITS_PER_UNIT;
5795 new = gen_rtx_MEM (tmode, plus_constant (XEXP (inner, 0), offset));
5796 RTX_UNCHANGING_P (new) = RTX_UNCHANGING_P (inner);
5797 MEM_COPY_ATTRIBUTES (new, inner);
5799 else if (GET_CODE (inner) == REG)
5801 /* We can't call gen_lowpart_for_combine here since we always want
5802 a SUBREG and it would sometimes return a new hard register. */
5803 if (tmode != inner_mode)
5804 new = gen_rtx_SUBREG (tmode, inner,
5805 (WORDS_BIG_ENDIAN
5806 && (GET_MODE_SIZE (inner_mode)
5807 > UNITS_PER_WORD)
5808 ? (((GET_MODE_SIZE (inner_mode)
5809 - GET_MODE_SIZE (tmode))
5810 / UNITS_PER_WORD)
5811 - pos / BITS_PER_WORD)
5812 : pos / BITS_PER_WORD));
5813 else
5814 new = inner;
5816 else
5817 new = force_to_mode (inner, tmode,
5818 len >= HOST_BITS_PER_WIDE_INT
5819 ? GET_MODE_MASK (tmode)
5820 : ((HOST_WIDE_INT) 1 << len) - 1,
5821 NULL_RTX, 0);
5823 /* If this extraction is going into the destination of a SET,
5824 make a STRICT_LOW_PART unless we made a MEM. */
5826 if (in_dest)
5827 return (GET_CODE (new) == MEM ? new
5828 : (GET_CODE (new) != SUBREG
5829 ? gen_rtx_CLOBBER (tmode, const0_rtx)
5830 : gen_rtx_combine (STRICT_LOW_PART, VOIDmode, new)));
5832 /* Otherwise, sign- or zero-extend unless we already are in the
5833 proper mode. */
5835 return (mode == tmode ? new
5836 : gen_rtx_combine (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
5837 mode, new));
5840 /* Unless this is a COMPARE or we have a funny memory reference,
5841 don't do anything with zero-extending field extracts starting at
5842 the low-order bit since they are simple AND operations. */
5843 if (pos_rtx == 0 && pos == 0 && ! in_dest
5844 && ! in_compare && ! spans_byte && unsignedp)
5845 return 0;
5847 /* Unless we are allowed to span bytes or INNER is not MEM, reject this if
5848 we would be spanning bytes or if the position is not a constant and the
5849 length is not 1. In all other cases, we would only be going outside
5850 our object in cases when an original shift would have been
5851 undefined. */
5852 if (! spans_byte && GET_CODE (inner) == MEM
5853 && ((pos_rtx == 0 && pos + len > GET_MODE_BITSIZE (is_mode))
5854 || (pos_rtx != 0 && len != 1)))
5855 return 0;
5857 /* Get the mode to use should INNER not be a MEM, the mode for the position,
5858 and the mode for the result. */
5859 #ifdef HAVE_insv
5860 if (in_dest)
5862 wanted_inner_reg_mode
5863 = insn_data[(int) CODE_FOR_insv].operand[0].mode;
5864 if (wanted_inner_reg_mode == VOIDmode)
5865 wanted_inner_reg_mode = word_mode;
5867 pos_mode = insn_data[(int) CODE_FOR_insv].operand[2].mode;
5868 if (pos_mode == VOIDmode)
5869 pos_mode = word_mode;
5871 extraction_mode = insn_data[(int) CODE_FOR_insv].operand[3].mode;
5872 if (extraction_mode == VOIDmode)
5873 extraction_mode = word_mode;
5875 #endif
5877 #ifdef HAVE_extzv
5878 if (! in_dest && unsignedp)
5880 wanted_inner_reg_mode
5881 = insn_data[(int) CODE_FOR_extzv].operand[1].mode;
5882 if (wanted_inner_reg_mode == VOIDmode)
5883 wanted_inner_reg_mode = word_mode;
5885 pos_mode = insn_data[(int) CODE_FOR_extzv].operand[3].mode;
5886 if (pos_mode == VOIDmode)
5887 pos_mode = word_mode;
5889 extraction_mode = insn_data[(int) CODE_FOR_extzv].operand[0].mode;
5890 if (extraction_mode == VOIDmode)
5891 extraction_mode = word_mode;
5893 #endif
5895 #ifdef HAVE_extv
5896 if (! in_dest && ! unsignedp)
5898 wanted_inner_reg_mode
5899 = insn_data[(int) CODE_FOR_extv].operand[1].mode;
5900 if (wanted_inner_reg_mode == VOIDmode)
5901 wanted_inner_reg_mode = word_mode;
5903 pos_mode = insn_data[(int) CODE_FOR_extv].operand[3].mode;
5904 if (pos_mode == VOIDmode)
5905 pos_mode = word_mode;
5907 extraction_mode = insn_data[(int) CODE_FOR_extv].operand[0].mode;
5908 if (extraction_mode == VOIDmode)
5909 extraction_mode = word_mode;
5911 #endif
5913 /* Never narrow an object, since that might not be safe. */
5915 if (mode != VOIDmode
5916 && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
5917 extraction_mode = mode;
5919 if (pos_rtx && GET_MODE (pos_rtx) != VOIDmode
5920 && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
5921 pos_mode = GET_MODE (pos_rtx);
5923 /* If this is not from memory, the desired mode is wanted_inner_reg_mode;
5924 if we have to change the mode of memory and cannot, the desired mode is
5925 EXTRACTION_MODE. */
5926 if (GET_CODE (inner) != MEM)
5927 wanted_inner_mode = wanted_inner_reg_mode;
5928 else if (inner_mode != wanted_inner_mode
5929 && (mode_dependent_address_p (XEXP (inner, 0))
5930 || MEM_VOLATILE_P (inner)))
5931 wanted_inner_mode = extraction_mode;
5933 orig_pos = pos;
5935 if (BITS_BIG_ENDIAN)
5937 /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
5938 BITS_BIG_ENDIAN style. If position is constant, compute new
5939 position. Otherwise, build subtraction.
5940 Note that POS is relative to the mode of the original argument.
5941 If it's a MEM we need to recompute POS relative to that.
5942 However, if we're extracting from (or inserting into) a register,
5943 we want to recompute POS relative to wanted_inner_mode. */
5944 int width = (GET_CODE (inner) == MEM
5945 ? GET_MODE_BITSIZE (is_mode)
5946 : GET_MODE_BITSIZE (wanted_inner_mode));
5948 if (pos_rtx == 0)
5949 pos = width - len - pos;
5950 else
5951 pos_rtx
5952 = gen_rtx_combine (MINUS, GET_MODE (pos_rtx),
5953 GEN_INT (width - len), pos_rtx);
5954 /* POS may be less than 0 now, but we check for that below.
5955 Note that it can only be less than 0 if GET_CODE (inner) != MEM. */
5958 /* If INNER has a wider mode, make it smaller. If this is a constant
5959 extract, try to adjust the byte to point to the byte containing
5960 the value. */
5961 if (wanted_inner_mode != VOIDmode
5962 && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
5963 && ((GET_CODE (inner) == MEM
5964 && (inner_mode == wanted_inner_mode
5965 || (! mode_dependent_address_p (XEXP (inner, 0))
5966 && ! MEM_VOLATILE_P (inner))))))
5968 int offset = 0;
5970 /* The computations below will be correct if the machine is big
5971 endian in both bits and bytes or little endian in bits and bytes.
5972 If it is mixed, we must adjust. */
5974 /* If bytes are big endian and we had a paradoxical SUBREG, we must
5975 adjust OFFSET to compensate. */
5976 if (BYTES_BIG_ENDIAN
5977 && ! spans_byte
5978 && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
5979 offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
5981 /* If this is a constant position, we can move to the desired byte. */
5982 if (pos_rtx == 0)
5984 offset += pos / BITS_PER_UNIT;
5985 pos %= GET_MODE_BITSIZE (wanted_inner_mode);
5988 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
5989 && ! spans_byte
5990 && is_mode != wanted_inner_mode)
5991 offset = (GET_MODE_SIZE (is_mode)
5992 - GET_MODE_SIZE (wanted_inner_mode) - offset);
5994 if (offset != 0 || inner_mode != wanted_inner_mode)
5996 rtx newmem = gen_rtx_MEM (wanted_inner_mode,
5997 plus_constant (XEXP (inner, 0), offset));
5998 RTX_UNCHANGING_P (newmem) = RTX_UNCHANGING_P (inner);
5999 MEM_COPY_ATTRIBUTES (newmem, inner);
6000 inner = newmem;
6004 /* If INNER is not memory, we can always get it into the proper mode. If we
6005 are changing its mode, POS must be a constant and smaller than the size
6006 of the new mode. */
6007 else if (GET_CODE (inner) != MEM)
6009 if (GET_MODE (inner) != wanted_inner_mode
6010 && (pos_rtx != 0
6011 || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
6012 return 0;
6014 inner = force_to_mode (inner, wanted_inner_mode,
6015 pos_rtx
6016 || len + orig_pos >= HOST_BITS_PER_WIDE_INT
6017 ? GET_MODE_MASK (wanted_inner_mode)
6018 : (((HOST_WIDE_INT) 1 << len) - 1) << orig_pos,
6019 NULL_RTX, 0);
6022 /* Adjust mode of POS_RTX, if needed. If we want a wider mode, we
6023 have to zero extend. Otherwise, we can just use a SUBREG. */
6024 if (pos_rtx != 0
6025 && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
6026 pos_rtx = gen_rtx_combine (ZERO_EXTEND, pos_mode, pos_rtx);
6027 else if (pos_rtx != 0
6028 && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
6029 pos_rtx = gen_lowpart_for_combine (pos_mode, pos_rtx);
6031 /* Make POS_RTX unless we already have it and it is correct. If we don't
6032 have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
6033 be a CONST_INT. */
6034 if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
6035 pos_rtx = orig_pos_rtx;
6037 else if (pos_rtx == 0)
6038 pos_rtx = GEN_INT (pos);
6040 /* Make the required operation. See if we can use existing rtx. */
6041 new = gen_rtx_combine (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
6042 extraction_mode, inner, GEN_INT (len), pos_rtx);
6043 if (! in_dest)
6044 new = gen_lowpart_for_combine (mode, new);
6046 return new;
6049 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
6050 with any other operations in X. Return X without that shift if so. */
6052 static rtx
6053 extract_left_shift (x, count)
6054 rtx x;
6055 int count;
6057 enum rtx_code code = GET_CODE (x);
6058 enum machine_mode mode = GET_MODE (x);
6059 rtx tem;
6061 switch (code)
6063 case ASHIFT:
6064 /* This is the shift itself. If it is wide enough, we will return
6065 either the value being shifted if the shift count is equal to
6066 COUNT or a shift for the difference. */
6067 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6068 && INTVAL (XEXP (x, 1)) >= count)
6069 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
6070 INTVAL (XEXP (x, 1)) - count);
6071 break;
6073 case NEG: case NOT:
6074 if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
6075 return gen_unary (code, mode, mode, tem);
6077 break;
6079 case PLUS: case IOR: case XOR: case AND:
6080 /* If we can safely shift this constant and we find the inner shift,
6081 make a new operation. */
6082 if (GET_CODE (XEXP (x,1)) == CONST_INT
6083 && (INTVAL (XEXP (x, 1)) & ((((HOST_WIDE_INT) 1 << count)) - 1)) == 0
6084 && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
6085 return gen_binary (code, mode, tem,
6086 GEN_INT (INTVAL (XEXP (x, 1)) >> count));
6088 break;
6090 default:
6091 break;
6094 return 0;
6097 /* Look at the expression rooted at X. Look for expressions
6098 equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
6099 Form these expressions.
6101 Return the new rtx, usually just X.
6103 Also, for machines like the Vax that don't have logical shift insns,
6104 try to convert logical to arithmetic shift operations in cases where
6105 they are equivalent. This undoes the canonicalizations to logical
6106 shifts done elsewhere.
6108 We try, as much as possible, to re-use rtl expressions to save memory.
6110 IN_CODE says what kind of expression we are processing. Normally, it is
6111 SET. In a memory address (inside a MEM, PLUS or minus, the latter two
6112 being kludges), it is MEM. When processing the arguments of a comparison
6113 or a COMPARE against zero, it is COMPARE. */
6115 static rtx
6116 make_compound_operation (x, in_code)
6117 rtx x;
6118 enum rtx_code in_code;
6120 enum rtx_code code = GET_CODE (x);
6121 enum machine_mode mode = GET_MODE (x);
6122 int mode_width = GET_MODE_BITSIZE (mode);
6123 rtx rhs, lhs;
6124 enum rtx_code next_code;
6125 int i;
6126 rtx new = 0;
6127 rtx tem;
6128 const char *fmt;
6130 /* Select the code to be used in recursive calls. Once we are inside an
6131 address, we stay there. If we have a comparison, set to COMPARE,
6132 but once inside, go back to our default of SET. */
6134 next_code = (code == MEM || code == PLUS || code == MINUS ? MEM
6135 : ((code == COMPARE || GET_RTX_CLASS (code) == '<')
6136 && XEXP (x, 1) == const0_rtx) ? COMPARE
6137 : in_code == COMPARE ? SET : in_code);
6139 /* Process depending on the code of this operation. If NEW is set
6140 non-zero, it will be returned. */
6142 switch (code)
6144 case ASHIFT:
6145 /* Convert shifts by constants into multiplications if inside
6146 an address. */
6147 if (in_code == MEM && GET_CODE (XEXP (x, 1)) == CONST_INT
6148 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
6149 && INTVAL (XEXP (x, 1)) >= 0)
6151 new = make_compound_operation (XEXP (x, 0), next_code);
6152 new = gen_rtx_combine (MULT, mode, new,
6153 GEN_INT ((HOST_WIDE_INT) 1
6154 << INTVAL (XEXP (x, 1))));
6156 break;
6158 case AND:
6159 /* If the second operand is not a constant, we can't do anything
6160 with it. */
6161 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
6162 break;
6164 /* If the constant is a power of two minus one and the first operand
6165 is a logical right shift, make an extraction. */
6166 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6167 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6169 new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
6170 new = make_extraction (mode, new, 0, XEXP (XEXP (x, 0), 1), i, 1,
6171 0, in_code == COMPARE);
6174 /* Same as previous, but for (subreg (lshiftrt ...)) in first op. */
6175 else if (GET_CODE (XEXP (x, 0)) == SUBREG
6176 && subreg_lowpart_p (XEXP (x, 0))
6177 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
6178 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6180 new = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
6181 next_code);
6182 new = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new, 0,
6183 XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
6184 0, in_code == COMPARE);
6186 /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)). */
6187 else if ((GET_CODE (XEXP (x, 0)) == XOR
6188 || GET_CODE (XEXP (x, 0)) == IOR)
6189 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
6190 && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
6191 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6193 /* Apply the distributive law, and then try to make extractions. */
6194 new = gen_rtx_combine (GET_CODE (XEXP (x, 0)), mode,
6195 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
6196 XEXP (x, 1)),
6197 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
6198 XEXP (x, 1)));
6199 new = make_compound_operation (new, in_code);
6202 /* If we are have (and (rotate X C) M) and C is larger than the number
6203 of bits in M, this is an extraction. */
6205 else if (GET_CODE (XEXP (x, 0)) == ROTATE
6206 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6207 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0
6208 && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
6210 new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
6211 new = make_extraction (mode, new,
6212 (GET_MODE_BITSIZE (mode)
6213 - INTVAL (XEXP (XEXP (x, 0), 1))),
6214 NULL_RTX, i, 1, 0, in_code == COMPARE);
6217 /* On machines without logical shifts, if the operand of the AND is
6218 a logical shift and our mask turns off all the propagated sign
6219 bits, we can replace the logical shift with an arithmetic shift. */
6220 else if (ashr_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing
6221 && (lshr_optab->handlers[(int) mode].insn_code
6222 == CODE_FOR_nothing)
6223 && GET_CODE (XEXP (x, 0)) == LSHIFTRT
6224 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6225 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
6226 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
6227 && mode_width <= HOST_BITS_PER_WIDE_INT)
6229 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
6231 mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
6232 if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
6233 SUBST (XEXP (x, 0),
6234 gen_rtx_combine (ASHIFTRT, mode,
6235 make_compound_operation (XEXP (XEXP (x, 0), 0),
6236 next_code),
6237 XEXP (XEXP (x, 0), 1)));
6240 /* If the constant is one less than a power of two, this might be
6241 representable by an extraction even if no shift is present.
6242 If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
6243 we are in a COMPARE. */
6244 else if ((i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6245 new = make_extraction (mode,
6246 make_compound_operation (XEXP (x, 0),
6247 next_code),
6248 0, NULL_RTX, i, 1, 0, in_code == COMPARE);
6250 /* If we are in a comparison and this is an AND with a power of two,
6251 convert this into the appropriate bit extract. */
6252 else if (in_code == COMPARE
6253 && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
6254 new = make_extraction (mode,
6255 make_compound_operation (XEXP (x, 0),
6256 next_code),
6257 i, NULL_RTX, 1, 1, 0, 1);
6259 break;
6261 case LSHIFTRT:
6262 /* If the sign bit is known to be zero, replace this with an
6263 arithmetic shift. */
6264 if (ashr_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing
6265 && lshr_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing
6266 && mode_width <= HOST_BITS_PER_WIDE_INT
6267 && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
6269 new = gen_rtx_combine (ASHIFTRT, mode,
6270 make_compound_operation (XEXP (x, 0),
6271 next_code),
6272 XEXP (x, 1));
6273 break;
6276 /* ... fall through ... */
6278 case ASHIFTRT:
6279 lhs = XEXP (x, 0);
6280 rhs = XEXP (x, 1);
6282 /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
6283 this is a SIGN_EXTRACT. */
6284 if (GET_CODE (rhs) == CONST_INT
6285 && GET_CODE (lhs) == ASHIFT
6286 && GET_CODE (XEXP (lhs, 1)) == CONST_INT
6287 && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1)))
6289 new = make_compound_operation (XEXP (lhs, 0), next_code);
6290 new = make_extraction (mode, new,
6291 INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
6292 NULL_RTX, mode_width - INTVAL (rhs),
6293 code == LSHIFTRT, 0, in_code == COMPARE);
6296 /* See if we have operations between an ASHIFTRT and an ASHIFT.
6297 If so, try to merge the shifts into a SIGN_EXTEND. We could
6298 also do this for some cases of SIGN_EXTRACT, but it doesn't
6299 seem worth the effort; the case checked for occurs on Alpha. */
6301 if (GET_RTX_CLASS (GET_CODE (lhs)) != 'o'
6302 && ! (GET_CODE (lhs) == SUBREG
6303 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (lhs))) == 'o'))
6304 && GET_CODE (rhs) == CONST_INT
6305 && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
6306 && (new = extract_left_shift (lhs, INTVAL (rhs))) != 0)
6307 new = make_extraction (mode, make_compound_operation (new, next_code),
6308 0, NULL_RTX, mode_width - INTVAL (rhs),
6309 code == LSHIFTRT, 0, in_code == COMPARE);
6311 break;
6313 case SUBREG:
6314 /* Call ourselves recursively on the inner expression. If we are
6315 narrowing the object and it has a different RTL code from
6316 what it originally did, do this SUBREG as a force_to_mode. */
6318 tem = make_compound_operation (SUBREG_REG (x), in_code);
6319 if (GET_CODE (tem) != GET_CODE (SUBREG_REG (x))
6320 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (tem))
6321 && subreg_lowpart_p (x))
6323 rtx newer = force_to_mode (tem, mode,
6324 GET_MODE_MASK (mode), NULL_RTX, 0);
6326 /* If we have something other than a SUBREG, we might have
6327 done an expansion, so rerun outselves. */
6328 if (GET_CODE (newer) != SUBREG)
6329 newer = make_compound_operation (newer, in_code);
6331 return newer;
6334 /* If this is a paradoxical subreg, and the new code is a sign or
6335 zero extension, omit the subreg and widen the extension. If it
6336 is a regular subreg, we can still get rid of the subreg by not
6337 widening so much, or in fact removing the extension entirely. */
6338 if ((GET_CODE (tem) == SIGN_EXTEND
6339 || GET_CODE (tem) == ZERO_EXTEND)
6340 && subreg_lowpart_p (x))
6342 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (tem))
6343 || (GET_MODE_SIZE (mode) >
6344 GET_MODE_SIZE (GET_MODE (XEXP (tem, 0)))))
6345 tem = gen_rtx_combine (GET_CODE (tem), mode, XEXP (tem, 0));
6346 else
6347 tem = gen_lowpart_for_combine (mode, XEXP (tem, 0));
6348 return tem;
6350 break;
6352 default:
6353 break;
6356 if (new)
6358 x = gen_lowpart_for_combine (mode, new);
6359 code = GET_CODE (x);
6362 /* Now recursively process each operand of this operation. */
6363 fmt = GET_RTX_FORMAT (code);
6364 for (i = 0; i < GET_RTX_LENGTH (code); i++)
6365 if (fmt[i] == 'e')
6367 new = make_compound_operation (XEXP (x, i), next_code);
6368 SUBST (XEXP (x, i), new);
6371 return x;
6374 /* Given M see if it is a value that would select a field of bits
6375 within an item, but not the entire word. Return -1 if not.
6376 Otherwise, return the starting position of the field, where 0 is the
6377 low-order bit.
6379 *PLEN is set to the length of the field. */
6381 static int
6382 get_pos_from_mask (m, plen)
6383 unsigned HOST_WIDE_INT m;
6384 int *plen;
6386 /* Get the bit number of the first 1 bit from the right, -1 if none. */
6387 int pos = exact_log2 (m & - m);
6389 if (pos < 0)
6390 return -1;
6392 /* Now shift off the low-order zero bits and see if we have a power of
6393 two minus 1. */
6394 *plen = exact_log2 ((m >> pos) + 1);
6396 if (*plen <= 0)
6397 return -1;
6399 return pos;
6402 /* See if X can be simplified knowing that we will only refer to it in
6403 MODE and will only refer to those bits that are nonzero in MASK.
6404 If other bits are being computed or if masking operations are done
6405 that select a superset of the bits in MASK, they can sometimes be
6406 ignored.
6408 Return a possibly simplified expression, but always convert X to
6409 MODE. If X is a CONST_INT, AND the CONST_INT with MASK.
6411 Also, if REG is non-zero and X is a register equal in value to REG,
6412 replace X with REG.
6414 If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
6415 are all off in X. This is used when X will be complemented, by either
6416 NOT, NEG, or XOR. */
6418 static rtx
6419 force_to_mode (x, mode, mask, reg, just_select)
6420 rtx x;
6421 enum machine_mode mode;
6422 unsigned HOST_WIDE_INT mask;
6423 rtx reg;
6424 int just_select;
6426 enum rtx_code code = GET_CODE (x);
6427 int next_select = just_select || code == XOR || code == NOT || code == NEG;
6428 enum machine_mode op_mode;
6429 unsigned HOST_WIDE_INT fuller_mask, nonzero;
6430 rtx op0, op1, temp;
6432 /* If this is a CALL or ASM_OPERANDS, don't do anything. Some of the
6433 code below will do the wrong thing since the mode of such an
6434 expression is VOIDmode.
6436 Also do nothing if X is a CLOBBER; this can happen if X was
6437 the return value from a call to gen_lowpart_for_combine. */
6438 if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
6439 return x;
6441 /* We want to perform the operation is its present mode unless we know
6442 that the operation is valid in MODE, in which case we do the operation
6443 in MODE. */
6444 op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
6445 && code_to_optab[(int) code] != 0
6446 && (code_to_optab[(int) code]->handlers[(int) mode].insn_code
6447 != CODE_FOR_nothing))
6448 ? mode : GET_MODE (x));
6450 /* It is not valid to do a right-shift in a narrower mode
6451 than the one it came in with. */
6452 if ((code == LSHIFTRT || code == ASHIFTRT)
6453 && GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (GET_MODE (x)))
6454 op_mode = GET_MODE (x);
6456 /* Truncate MASK to fit OP_MODE. */
6457 if (op_mode)
6458 mask &= GET_MODE_MASK (op_mode);
6460 /* When we have an arithmetic operation, or a shift whose count we
6461 do not know, we need to assume that all bit the up to the highest-order
6462 bit in MASK will be needed. This is how we form such a mask. */
6463 if (op_mode)
6464 fuller_mask = (GET_MODE_BITSIZE (op_mode) >= HOST_BITS_PER_WIDE_INT
6465 ? GET_MODE_MASK (op_mode)
6466 : ((HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1)) - 1);
6467 else
6468 fuller_mask = ~ (HOST_WIDE_INT) 0;
6470 /* Determine what bits of X are guaranteed to be (non)zero. */
6471 nonzero = nonzero_bits (x, mode);
6473 /* If none of the bits in X are needed, return a zero. */
6474 if (! just_select && (nonzero & mask) == 0)
6475 return const0_rtx;
6477 /* If X is a CONST_INT, return a new one. Do this here since the
6478 test below will fail. */
6479 if (GET_CODE (x) == CONST_INT)
6481 HOST_WIDE_INT cval = INTVAL (x) & mask;
6482 int width = GET_MODE_BITSIZE (mode);
6484 /* If MODE is narrower that HOST_WIDE_INT and CVAL is a negative
6485 number, sign extend it. */
6486 if (width > 0 && width < HOST_BITS_PER_WIDE_INT
6487 && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
6488 cval |= (HOST_WIDE_INT) -1 << width;
6490 return GEN_INT (cval);
6493 /* If X is narrower than MODE and we want all the bits in X's mode, just
6494 get X in the proper mode. */
6495 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
6496 && (GET_MODE_MASK (GET_MODE (x)) & ~ mask) == 0)
6497 return gen_lowpart_for_combine (mode, x);
6499 /* If we aren't changing the mode, X is not a SUBREG, and all zero bits in
6500 MASK are already known to be zero in X, we need not do anything. */
6501 if (GET_MODE (x) == mode && code != SUBREG && (~ mask & nonzero) == 0)
6502 return x;
6504 switch (code)
6506 case CLOBBER:
6507 /* If X is a (clobber (const_int)), return it since we know we are
6508 generating something that won't match. */
6509 return x;
6511 case USE:
6512 /* X is a (use (mem ..)) that was made from a bit-field extraction that
6513 spanned the boundary of the MEM. If we are now masking so it is
6514 within that boundary, we don't need the USE any more. */
6515 if (! BITS_BIG_ENDIAN
6516 && (mask & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6517 return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
6518 break;
6520 case SIGN_EXTEND:
6521 case ZERO_EXTEND:
6522 case ZERO_EXTRACT:
6523 case SIGN_EXTRACT:
6524 x = expand_compound_operation (x);
6525 if (GET_CODE (x) != code)
6526 return force_to_mode (x, mode, mask, reg, next_select);
6527 break;
6529 case REG:
6530 if (reg != 0 && (rtx_equal_p (get_last_value (reg), x)
6531 || rtx_equal_p (reg, get_last_value (x))))
6532 x = reg;
6533 break;
6535 case SUBREG:
6536 if (subreg_lowpart_p (x)
6537 /* We can ignore the effect of this SUBREG if it narrows the mode or
6538 if the constant masks to zero all the bits the mode doesn't
6539 have. */
6540 && ((GET_MODE_SIZE (GET_MODE (x))
6541 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
6542 || (0 == (mask
6543 & GET_MODE_MASK (GET_MODE (x))
6544 & ~ GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
6545 return force_to_mode (SUBREG_REG (x), mode, mask, reg, next_select);
6546 break;
6548 case AND:
6549 /* If this is an AND with a constant, convert it into an AND
6550 whose constant is the AND of that constant with MASK. If it
6551 remains an AND of MASK, delete it since it is redundant. */
6553 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
6555 x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
6556 mask & INTVAL (XEXP (x, 1)));
6558 /* If X is still an AND, see if it is an AND with a mask that
6559 is just some low-order bits. If so, and it is MASK, we don't
6560 need it. */
6562 if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
6563 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (x, 1)) == mask)
6564 x = XEXP (x, 0);
6566 /* If it remains an AND, try making another AND with the bits
6567 in the mode mask that aren't in MASK turned on. If the
6568 constant in the AND is wide enough, this might make a
6569 cheaper constant. */
6571 if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
6572 && GET_MODE_MASK (GET_MODE (x)) != mask
6573 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
6575 HOST_WIDE_INT cval = (INTVAL (XEXP (x, 1))
6576 | (GET_MODE_MASK (GET_MODE (x)) & ~ mask));
6577 int width = GET_MODE_BITSIZE (GET_MODE (x));
6578 rtx y;
6580 /* If MODE is narrower that HOST_WIDE_INT and CVAL is a negative
6581 number, sign extend it. */
6582 if (width > 0 && width < HOST_BITS_PER_WIDE_INT
6583 && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
6584 cval |= (HOST_WIDE_INT) -1 << width;
6586 y = gen_binary (AND, GET_MODE (x), XEXP (x, 0), GEN_INT (cval));
6587 if (rtx_cost (y, SET) < rtx_cost (x, SET))
6588 x = y;
6591 break;
6594 goto binop;
6596 case PLUS:
6597 /* In (and (plus FOO C1) M), if M is a mask that just turns off
6598 low-order bits (as in an alignment operation) and FOO is already
6599 aligned to that boundary, mask C1 to that boundary as well.
6600 This may eliminate that PLUS and, later, the AND. */
6603 int width = GET_MODE_BITSIZE (mode);
6604 unsigned HOST_WIDE_INT smask = mask;
6606 /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
6607 number, sign extend it. */
6609 if (width < HOST_BITS_PER_WIDE_INT
6610 && (smask & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
6611 smask |= (HOST_WIDE_INT) -1 << width;
6613 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6614 && exact_log2 (- smask) >= 0)
6616 #ifdef STACK_BIAS
6617 if (STACK_BIAS
6618 && (XEXP (x, 0) == stack_pointer_rtx
6619 || XEXP (x, 0) == frame_pointer_rtx))
6621 int sp_alignment = STACK_BOUNDARY / BITS_PER_UNIT;
6622 unsigned HOST_WIDE_INT sp_mask = GET_MODE_MASK (mode);
6624 sp_mask &= ~ (sp_alignment - 1);
6625 if ((sp_mask & ~ smask) == 0
6626 && ((INTVAL (XEXP (x, 1)) - STACK_BIAS) & ~ smask) != 0)
6627 return force_to_mode (plus_constant (XEXP (x, 0),
6628 ((INTVAL (XEXP (x, 1)) -
6629 STACK_BIAS) & smask)
6630 + STACK_BIAS),
6631 mode, smask, reg, next_select);
6633 #endif
6634 if ((nonzero_bits (XEXP (x, 0), mode) & ~ smask) == 0
6635 && (INTVAL (XEXP (x, 1)) & ~ smask) != 0)
6636 return force_to_mode (plus_constant (XEXP (x, 0),
6637 (INTVAL (XEXP (x, 1))
6638 & smask)),
6639 mode, smask, reg, next_select);
6643 /* ... fall through ... */
6645 case MINUS:
6646 case MULT:
6647 /* For PLUS, MINUS and MULT, we need any bits less significant than the
6648 most significant bit in MASK since carries from those bits will
6649 affect the bits we are interested in. */
6650 mask = fuller_mask;
6651 goto binop;
6653 case IOR:
6654 case XOR:
6655 /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
6656 LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
6657 operation which may be a bitfield extraction. Ensure that the
6658 constant we form is not wider than the mode of X. */
6660 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6661 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6662 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
6663 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
6664 && GET_CODE (XEXP (x, 1)) == CONST_INT
6665 && ((INTVAL (XEXP (XEXP (x, 0), 1))
6666 + floor_log2 (INTVAL (XEXP (x, 1))))
6667 < GET_MODE_BITSIZE (GET_MODE (x)))
6668 && (INTVAL (XEXP (x, 1))
6669 & ~ nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
6671 temp = GEN_INT ((INTVAL (XEXP (x, 1)) & mask)
6672 << INTVAL (XEXP (XEXP (x, 0), 1)));
6673 temp = gen_binary (GET_CODE (x), GET_MODE (x),
6674 XEXP (XEXP (x, 0), 0), temp);
6675 x = gen_binary (LSHIFTRT, GET_MODE (x), temp,
6676 XEXP (XEXP (x, 0), 1));
6677 return force_to_mode (x, mode, mask, reg, next_select);
6680 binop:
6681 /* For most binary operations, just propagate into the operation and
6682 change the mode if we have an operation of that mode. */
6684 op0 = gen_lowpart_for_combine (op_mode,
6685 force_to_mode (XEXP (x, 0), mode, mask,
6686 reg, next_select));
6687 op1 = gen_lowpart_for_combine (op_mode,
6688 force_to_mode (XEXP (x, 1), mode, mask,
6689 reg, next_select));
6691 /* If OP1 is a CONST_INT and X is an IOR or XOR, clear bits outside
6692 MASK since OP1 might have been sign-extended but we never want
6693 to turn on extra bits, since combine might have previously relied
6694 on them being off. */
6695 if (GET_CODE (op1) == CONST_INT && (code == IOR || code == XOR)
6696 && (INTVAL (op1) & mask) != 0)
6697 op1 = GEN_INT (INTVAL (op1) & mask);
6699 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
6700 x = gen_binary (code, op_mode, op0, op1);
6701 break;
6703 case ASHIFT:
6704 /* For left shifts, do the same, but just for the first operand.
6705 However, we cannot do anything with shifts where we cannot
6706 guarantee that the counts are smaller than the size of the mode
6707 because such a count will have a different meaning in a
6708 wider mode. */
6710 if (! (GET_CODE (XEXP (x, 1)) == CONST_INT
6711 && INTVAL (XEXP (x, 1)) >= 0
6712 && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (mode))
6713 && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
6714 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
6715 < (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (mode))))
6716 break;
6718 /* If the shift count is a constant and we can do arithmetic in
6719 the mode of the shift, refine which bits we need. Otherwise, use the
6720 conservative form of the mask. */
6721 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6722 && INTVAL (XEXP (x, 1)) >= 0
6723 && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (op_mode)
6724 && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
6725 mask >>= INTVAL (XEXP (x, 1));
6726 else
6727 mask = fuller_mask;
6729 op0 = gen_lowpart_for_combine (op_mode,
6730 force_to_mode (XEXP (x, 0), op_mode,
6731 mask, reg, next_select));
6733 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
6734 x = gen_binary (code, op_mode, op0, XEXP (x, 1));
6735 break;
6737 case LSHIFTRT:
6738 /* Here we can only do something if the shift count is a constant,
6739 this shift constant is valid for the host, and we can do arithmetic
6740 in OP_MODE. */
6742 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6743 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
6744 && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
6746 rtx inner = XEXP (x, 0);
6748 /* Select the mask of the bits we need for the shift operand. */
6749 mask <<= INTVAL (XEXP (x, 1));
6751 /* We can only change the mode of the shift if we can do arithmetic
6752 in the mode of the shift and MASK is no wider than the width of
6753 OP_MODE. */
6754 if (GET_MODE_BITSIZE (op_mode) > HOST_BITS_PER_WIDE_INT
6755 || (mask & ~ GET_MODE_MASK (op_mode)) != 0)
6756 op_mode = GET_MODE (x);
6758 inner = force_to_mode (inner, op_mode, mask, reg, next_select);
6760 if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
6761 x = gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
6764 /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
6765 shift and AND produces only copies of the sign bit (C2 is one less
6766 than a power of two), we can do this with just a shift. */
6768 if (GET_CODE (x) == LSHIFTRT
6769 && GET_CODE (XEXP (x, 1)) == CONST_INT
6770 && ((INTVAL (XEXP (x, 1))
6771 + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
6772 >= GET_MODE_BITSIZE (GET_MODE (x)))
6773 && exact_log2 (mask + 1) >= 0
6774 && (num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
6775 >= exact_log2 (mask + 1)))
6776 x = gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
6777 GEN_INT (GET_MODE_BITSIZE (GET_MODE (x))
6778 - exact_log2 (mask + 1)));
6780 goto shiftrt;
6782 case ASHIFTRT:
6783 /* If we are just looking for the sign bit, we don't need this shift at
6784 all, even if it has a variable count. */
6785 if (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6786 && (mask == ((unsigned HOST_WIDE_INT) 1
6787 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
6788 return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
6790 /* If this is a shift by a constant, get a mask that contains those bits
6791 that are not copies of the sign bit. We then have two cases: If
6792 MASK only includes those bits, this can be a logical shift, which may
6793 allow simplifications. If MASK is a single-bit field not within
6794 those bits, we are requesting a copy of the sign bit and hence can
6795 shift the sign bit to the appropriate location. */
6797 if (GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) >= 0
6798 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
6800 int i = -1;
6802 /* If the considered data is wider then HOST_WIDE_INT, we can't
6803 represent a mask for all its bits in a single scalar.
6804 But we only care about the lower bits, so calculate these. */
6806 if (GET_MODE_BITSIZE (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
6808 nonzero = ~ (HOST_WIDE_INT) 0;
6810 /* GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
6811 is the number of bits a full-width mask would have set.
6812 We need only shift if these are fewer than nonzero can
6813 hold. If not, we must keep all bits set in nonzero. */
6815 if (GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
6816 < HOST_BITS_PER_WIDE_INT)
6817 nonzero >>= INTVAL (XEXP (x, 1))
6818 + HOST_BITS_PER_WIDE_INT
6819 - GET_MODE_BITSIZE (GET_MODE (x)) ;
6821 else
6823 nonzero = GET_MODE_MASK (GET_MODE (x));
6824 nonzero >>= INTVAL (XEXP (x, 1));
6827 if ((mask & ~ nonzero) == 0
6828 || (i = exact_log2 (mask)) >= 0)
6830 x = simplify_shift_const
6831 (x, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
6832 i < 0 ? INTVAL (XEXP (x, 1))
6833 : GET_MODE_BITSIZE (GET_MODE (x)) - 1 - i);
6835 if (GET_CODE (x) != ASHIFTRT)
6836 return force_to_mode (x, mode, mask, reg, next_select);
6840 /* If MASK is 1, convert this to a LSHIFTRT. This can be done
6841 even if the shift count isn't a constant. */
6842 if (mask == 1)
6843 x = gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0), XEXP (x, 1));
6845 shiftrt:
6847 /* If this is a zero- or sign-extension operation that just affects bits
6848 we don't care about, remove it. Be sure the call above returned
6849 something that is still a shift. */
6851 if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
6852 && GET_CODE (XEXP (x, 1)) == CONST_INT
6853 && INTVAL (XEXP (x, 1)) >= 0
6854 && (INTVAL (XEXP (x, 1))
6855 <= GET_MODE_BITSIZE (GET_MODE (x)) - (floor_log2 (mask) + 1))
6856 && GET_CODE (XEXP (x, 0)) == ASHIFT
6857 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6858 && INTVAL (XEXP (XEXP (x, 0), 1)) == INTVAL (XEXP (x, 1)))
6859 return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
6860 reg, next_select);
6862 break;
6864 case ROTATE:
6865 case ROTATERT:
6866 /* If the shift count is constant and we can do computations
6867 in the mode of X, compute where the bits we care about are.
6868 Otherwise, we can't do anything. Don't change the mode of
6869 the shift or propagate MODE into the shift, though. */
6870 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6871 && INTVAL (XEXP (x, 1)) >= 0)
6873 temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
6874 GET_MODE (x), GEN_INT (mask),
6875 XEXP (x, 1));
6876 if (temp && GET_CODE(temp) == CONST_INT)
6877 SUBST (XEXP (x, 0),
6878 force_to_mode (XEXP (x, 0), GET_MODE (x),
6879 INTVAL (temp), reg, next_select));
6881 break;
6883 case NEG:
6884 /* If we just want the low-order bit, the NEG isn't needed since it
6885 won't change the low-order bit. */
6886 if (mask == 1)
6887 return force_to_mode (XEXP (x, 0), mode, mask, reg, just_select);
6889 /* We need any bits less significant than the most significant bit in
6890 MASK since carries from those bits will affect the bits we are
6891 interested in. */
6892 mask = fuller_mask;
6893 goto unop;
6895 case NOT:
6896 /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
6897 same as the XOR case above. Ensure that the constant we form is not
6898 wider than the mode of X. */
6900 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6901 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6902 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
6903 && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
6904 < GET_MODE_BITSIZE (GET_MODE (x)))
6905 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
6907 temp = GEN_INT (mask << INTVAL (XEXP (XEXP (x, 0), 1)));
6908 temp = gen_binary (XOR, GET_MODE (x), XEXP (XEXP (x, 0), 0), temp);
6909 x = gen_binary (LSHIFTRT, GET_MODE (x), temp, XEXP (XEXP (x, 0), 1));
6911 return force_to_mode (x, mode, mask, reg, next_select);
6914 /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
6915 use the full mask inside the NOT. */
6916 mask = fuller_mask;
6918 unop:
6919 op0 = gen_lowpart_for_combine (op_mode,
6920 force_to_mode (XEXP (x, 0), mode, mask,
6921 reg, next_select));
6922 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
6923 x = gen_unary (code, op_mode, op_mode, op0);
6924 break;
6926 case NE:
6927 /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
6928 in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
6929 which is equal to STORE_FLAG_VALUE. */
6930 if ((mask & ~ STORE_FLAG_VALUE) == 0 && XEXP (x, 1) == const0_rtx
6931 && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
6932 && nonzero_bits (XEXP (x, 0), mode) == STORE_FLAG_VALUE)
6933 return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
6935 break;
6937 case IF_THEN_ELSE:
6938 /* We have no way of knowing if the IF_THEN_ELSE can itself be
6939 written in a narrower mode. We play it safe and do not do so. */
6941 SUBST (XEXP (x, 1),
6942 gen_lowpart_for_combine (GET_MODE (x),
6943 force_to_mode (XEXP (x, 1), mode,
6944 mask, reg, next_select)));
6945 SUBST (XEXP (x, 2),
6946 gen_lowpart_for_combine (GET_MODE (x),
6947 force_to_mode (XEXP (x, 2), mode,
6948 mask, reg,next_select)));
6949 break;
6951 default:
6952 break;
6955 /* Ensure we return a value of the proper mode. */
6956 return gen_lowpart_for_combine (mode, x);
6959 /* Return nonzero if X is an expression that has one of two values depending on
6960 whether some other value is zero or nonzero. In that case, we return the
6961 value that is being tested, *PTRUE is set to the value if the rtx being
6962 returned has a nonzero value, and *PFALSE is set to the other alternative.
6964 If we return zero, we set *PTRUE and *PFALSE to X. */
6966 static rtx
6967 if_then_else_cond (x, ptrue, pfalse)
6968 rtx x;
6969 rtx *ptrue, *pfalse;
6971 enum machine_mode mode = GET_MODE (x);
6972 enum rtx_code code = GET_CODE (x);
6973 int size = GET_MODE_BITSIZE (mode);
6974 rtx cond0, cond1, true0, true1, false0, false1;
6975 unsigned HOST_WIDE_INT nz;
6977 /* If we are comparing a value against zero, we are done. */
6978 if ((code == NE || code == EQ)
6979 && GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 0)
6981 *ptrue = (code == NE) ? const1_rtx : const0_rtx;
6982 *pfalse = (code == NE) ? const0_rtx : const1_rtx;
6983 return XEXP (x, 0);
6986 /* If this is a unary operation whose operand has one of two values, apply
6987 our opcode to compute those values. */
6988 else if (GET_RTX_CLASS (code) == '1'
6989 && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
6991 *ptrue = gen_unary (code, mode, GET_MODE (XEXP (x, 0)), true0);
6992 *pfalse = gen_unary (code, mode, GET_MODE (XEXP (x, 0)), false0);
6993 return cond0;
6996 /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
6997 make can't possibly match and would suppress other optimizations. */
6998 else if (code == COMPARE)
7001 /* If this is a binary operation, see if either side has only one of two
7002 values. If either one does or if both do and they are conditional on
7003 the same value, compute the new true and false values. */
7004 else if (GET_RTX_CLASS (code) == 'c' || GET_RTX_CLASS (code) == '2'
7005 || GET_RTX_CLASS (code) == '<')
7007 cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
7008 cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
7010 if ((cond0 != 0 || cond1 != 0)
7011 && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
7013 /* If if_then_else_cond returned zero, then true/false are the
7014 same rtl. We must copy one of them to prevent invalid rtl
7015 sharing. */
7016 if (cond0 == 0)
7017 true0 = copy_rtx (true0);
7018 else if (cond1 == 0)
7019 true1 = copy_rtx (true1);
7021 *ptrue = gen_binary (code, mode, true0, true1);
7022 *pfalse = gen_binary (code, mode, false0, false1);
7023 return cond0 ? cond0 : cond1;
7026 /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
7027 operands is zero when the other is non-zero, and vice-versa,
7028 and STORE_FLAG_VALUE is 1 or -1. */
7030 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
7031 && (code == PLUS || code == IOR || code == XOR || code == MINUS
7032 || code == UMAX)
7033 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
7035 rtx op0 = XEXP (XEXP (x, 0), 1);
7036 rtx op1 = XEXP (XEXP (x, 1), 1);
7038 cond0 = XEXP (XEXP (x, 0), 0);
7039 cond1 = XEXP (XEXP (x, 1), 0);
7041 if (GET_RTX_CLASS (GET_CODE (cond0)) == '<'
7042 && GET_RTX_CLASS (GET_CODE (cond1)) == '<'
7043 && reversible_comparison_p (cond1)
7044 && ((GET_CODE (cond0) == reverse_condition (GET_CODE (cond1))
7045 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
7046 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
7047 || ((swap_condition (GET_CODE (cond0))
7048 == reverse_condition (GET_CODE (cond1)))
7049 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
7050 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
7051 && ! side_effects_p (x))
7053 *ptrue = gen_binary (MULT, mode, op0, const_true_rtx);
7054 *pfalse = gen_binary (MULT, mode,
7055 (code == MINUS
7056 ? gen_unary (NEG, mode, mode, op1) : op1),
7057 const_true_rtx);
7058 return cond0;
7062 /* Similarly for MULT, AND and UMIN, execpt that for these the result
7063 is always zero. */
7064 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
7065 && (code == MULT || code == AND || code == UMIN)
7066 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
7068 cond0 = XEXP (XEXP (x, 0), 0);
7069 cond1 = XEXP (XEXP (x, 1), 0);
7071 if (GET_RTX_CLASS (GET_CODE (cond0)) == '<'
7072 && GET_RTX_CLASS (GET_CODE (cond1)) == '<'
7073 && reversible_comparison_p (cond1)
7074 && ((GET_CODE (cond0) == reverse_condition (GET_CODE (cond1))
7075 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
7076 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
7077 || ((swap_condition (GET_CODE (cond0))
7078 == reverse_condition (GET_CODE (cond1)))
7079 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
7080 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
7081 && ! side_effects_p (x))
7083 *ptrue = *pfalse = const0_rtx;
7084 return cond0;
7089 else if (code == IF_THEN_ELSE)
7091 /* If we have IF_THEN_ELSE already, extract the condition and
7092 canonicalize it if it is NE or EQ. */
7093 cond0 = XEXP (x, 0);
7094 *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
7095 if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
7096 return XEXP (cond0, 0);
7097 else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
7099 *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
7100 return XEXP (cond0, 0);
7102 else
7103 return cond0;
7106 /* If X is a normal SUBREG with both inner and outer modes integral,
7107 we can narrow both the true and false values of the inner expression,
7108 if there is a condition. */
7109 else if (code == SUBREG && GET_MODE_CLASS (mode) == MODE_INT
7110 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_INT
7111 && GET_MODE_SIZE (mode) <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))
7112 && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
7113 &true0, &false0)))
7115 *ptrue = force_to_mode (true0, mode, GET_MODE_MASK (mode), NULL_RTX, 0);
7116 *pfalse
7117 = force_to_mode (false0, mode, GET_MODE_MASK (mode), NULL_RTX, 0);
7119 return cond0;
7122 /* If X is a constant, this isn't special and will cause confusions
7123 if we treat it as such. Likewise if it is equivalent to a constant. */
7124 else if (CONSTANT_P (x)
7125 || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
7128 /* If X is known to be either 0 or -1, those are the true and
7129 false values when testing X. */
7130 else if (num_sign_bit_copies (x, mode) == size)
7132 *ptrue = constm1_rtx, *pfalse = const0_rtx;
7133 return x;
7136 /* Likewise for 0 or a single bit. */
7137 else if (exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
7139 *ptrue = GEN_INT (nz), *pfalse = const0_rtx;
7140 return x;
7143 /* Otherwise fail; show no condition with true and false values the same. */
7144 *ptrue = *pfalse = x;
7145 return 0;
7148 /* Return the value of expression X given the fact that condition COND
7149 is known to be true when applied to REG as its first operand and VAL
7150 as its second. X is known to not be shared and so can be modified in
7151 place.
7153 We only handle the simplest cases, and specifically those cases that
7154 arise with IF_THEN_ELSE expressions. */
7156 static rtx
7157 known_cond (x, cond, reg, val)
7158 rtx x;
7159 enum rtx_code cond;
7160 rtx reg, val;
7162 enum rtx_code code = GET_CODE (x);
7163 rtx temp;
7164 const char *fmt;
7165 int i, j;
7167 if (side_effects_p (x))
7168 return x;
7170 if (cond == EQ && rtx_equal_p (x, reg))
7171 return val;
7173 /* If X is (abs REG) and we know something about REG's relationship
7174 with zero, we may be able to simplify this. */
7176 if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
7177 switch (cond)
7179 case GE: case GT: case EQ:
7180 return XEXP (x, 0);
7181 case LT: case LE:
7182 return gen_unary (NEG, GET_MODE (XEXP (x, 0)), GET_MODE (XEXP (x, 0)),
7183 XEXP (x, 0));
7184 default:
7185 break;
7188 /* The only other cases we handle are MIN, MAX, and comparisons if the
7189 operands are the same as REG and VAL. */
7191 else if (GET_RTX_CLASS (code) == '<' || GET_RTX_CLASS (code) == 'c')
7193 if (rtx_equal_p (XEXP (x, 0), val))
7194 cond = swap_condition (cond), temp = val, val = reg, reg = temp;
7196 if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
7198 if (GET_RTX_CLASS (code) == '<')
7199 return (comparison_dominates_p (cond, code) ? const_true_rtx
7200 : (comparison_dominates_p (cond,
7201 reverse_condition (code))
7202 ? const0_rtx : x));
7204 else if (code == SMAX || code == SMIN
7205 || code == UMIN || code == UMAX)
7207 int unsignedp = (code == UMIN || code == UMAX);
7209 if (code == SMAX || code == UMAX)
7210 cond = reverse_condition (cond);
7212 switch (cond)
7214 case GE: case GT:
7215 return unsignedp ? x : XEXP (x, 1);
7216 case LE: case LT:
7217 return unsignedp ? x : XEXP (x, 0);
7218 case GEU: case GTU:
7219 return unsignedp ? XEXP (x, 1) : x;
7220 case LEU: case LTU:
7221 return unsignedp ? XEXP (x, 0) : x;
7222 default:
7223 break;
7229 fmt = GET_RTX_FORMAT (code);
7230 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7232 if (fmt[i] == 'e')
7233 SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
7234 else if (fmt[i] == 'E')
7235 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
7236 SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
7237 cond, reg, val));
7240 return x;
7243 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
7244 assignment as a field assignment. */
7246 static int
7247 rtx_equal_for_field_assignment_p (x, y)
7248 rtx x;
7249 rtx y;
7251 if (x == y || rtx_equal_p (x, y))
7252 return 1;
7254 if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
7255 return 0;
7257 /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
7258 Note that all SUBREGs of MEM are paradoxical; otherwise they
7259 would have been rewritten. */
7260 if (GET_CODE (x) == MEM && GET_CODE (y) == SUBREG
7261 && GET_CODE (SUBREG_REG (y)) == MEM
7262 && rtx_equal_p (SUBREG_REG (y),
7263 gen_lowpart_for_combine (GET_MODE (SUBREG_REG (y)), x)))
7264 return 1;
7266 if (GET_CODE (y) == MEM && GET_CODE (x) == SUBREG
7267 && GET_CODE (SUBREG_REG (x)) == MEM
7268 && rtx_equal_p (SUBREG_REG (x),
7269 gen_lowpart_for_combine (GET_MODE (SUBREG_REG (x)), y)))
7270 return 1;
7272 /* We used to see if get_last_value of X and Y were the same but that's
7273 not correct. In one direction, we'll cause the assignment to have
7274 the wrong destination and in the case, we'll import a register into this
7275 insn that might have already have been dead. So fail if none of the
7276 above cases are true. */
7277 return 0;
7280 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
7281 Return that assignment if so.
7283 We only handle the most common cases. */
7285 static rtx
7286 make_field_assignment (x)
7287 rtx x;
7289 rtx dest = SET_DEST (x);
7290 rtx src = SET_SRC (x);
7291 rtx assign;
7292 rtx rhs, lhs;
7293 HOST_WIDE_INT c1;
7294 int pos, len;
7295 rtx other;
7296 enum machine_mode mode;
7298 /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
7299 a clear of a one-bit field. We will have changed it to
7300 (and (rotate (const_int -2) POS) DEST), so check for that. Also check
7301 for a SUBREG. */
7303 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
7304 && GET_CODE (XEXP (XEXP (src, 0), 0)) == CONST_INT
7305 && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
7306 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7308 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
7309 1, 1, 1, 0);
7310 if (assign != 0)
7311 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
7312 return x;
7315 else if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
7316 && subreg_lowpart_p (XEXP (src, 0))
7317 && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
7318 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
7319 && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
7320 && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
7321 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7323 assign = make_extraction (VOIDmode, dest, 0,
7324 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
7325 1, 1, 1, 0);
7326 if (assign != 0)
7327 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
7328 return x;
7331 /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
7332 one-bit field. */
7333 else if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
7334 && XEXP (XEXP (src, 0), 0) == const1_rtx
7335 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7337 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
7338 1, 1, 1, 0);
7339 if (assign != 0)
7340 return gen_rtx_SET (VOIDmode, assign, const1_rtx);
7341 return x;
7344 /* The other case we handle is assignments into a constant-position
7345 field. They look like (ior/xor (and DEST C1) OTHER). If C1 represents
7346 a mask that has all one bits except for a group of zero bits and
7347 OTHER is known to have zeros where C1 has ones, this is such an
7348 assignment. Compute the position and length from C1. Shift OTHER
7349 to the appropriate position, force it to the required mode, and
7350 make the extraction. Check for the AND in both operands. */
7352 if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
7353 return x;
7355 rhs = expand_compound_operation (XEXP (src, 0));
7356 lhs = expand_compound_operation (XEXP (src, 1));
7358 if (GET_CODE (rhs) == AND
7359 && GET_CODE (XEXP (rhs, 1)) == CONST_INT
7360 && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
7361 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
7362 else if (GET_CODE (lhs) == AND
7363 && GET_CODE (XEXP (lhs, 1)) == CONST_INT
7364 && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
7365 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
7366 else
7367 return x;
7369 pos = get_pos_from_mask ((~ c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
7370 if (pos < 0 || pos + len > GET_MODE_BITSIZE (GET_MODE (dest))
7371 || GET_MODE_BITSIZE (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
7372 || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
7373 return x;
7375 assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
7376 if (assign == 0)
7377 return x;
7379 /* The mode to use for the source is the mode of the assignment, or of
7380 what is inside a possible STRICT_LOW_PART. */
7381 mode = (GET_CODE (assign) == STRICT_LOW_PART
7382 ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
7384 /* Shift OTHER right POS places and make it the source, restricting it
7385 to the proper length and mode. */
7387 src = force_to_mode (simplify_shift_const (NULL_RTX, LSHIFTRT,
7388 GET_MODE (src), other, pos),
7389 mode,
7390 GET_MODE_BITSIZE (mode) >= HOST_BITS_PER_WIDE_INT
7391 ? GET_MODE_MASK (mode)
7392 : ((HOST_WIDE_INT) 1 << len) - 1,
7393 dest, 0);
7395 return gen_rtx_combine (SET, VOIDmode, assign, src);
7398 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
7399 if so. */
7401 static rtx
7402 apply_distributive_law (x)
7403 rtx x;
7405 enum rtx_code code = GET_CODE (x);
7406 rtx lhs, rhs, other;
7407 rtx tem;
7408 enum rtx_code inner_code;
7410 /* Distributivity is not true for floating point.
7411 It can change the value. So don't do it.
7412 -- rms and moshier@world.std.com. */
7413 if (FLOAT_MODE_P (GET_MODE (x)))
7414 return x;
7416 /* The outer operation can only be one of the following: */
7417 if (code != IOR && code != AND && code != XOR
7418 && code != PLUS && code != MINUS)
7419 return x;
7421 lhs = XEXP (x, 0), rhs = XEXP (x, 1);
7423 /* If either operand is a primitive we can't do anything, so get out
7424 fast. */
7425 if (GET_RTX_CLASS (GET_CODE (lhs)) == 'o'
7426 || GET_RTX_CLASS (GET_CODE (rhs)) == 'o')
7427 return x;
7429 lhs = expand_compound_operation (lhs);
7430 rhs = expand_compound_operation (rhs);
7431 inner_code = GET_CODE (lhs);
7432 if (inner_code != GET_CODE (rhs))
7433 return x;
7435 /* See if the inner and outer operations distribute. */
7436 switch (inner_code)
7438 case LSHIFTRT:
7439 case ASHIFTRT:
7440 case AND:
7441 case IOR:
7442 /* These all distribute except over PLUS. */
7443 if (code == PLUS || code == MINUS)
7444 return x;
7445 break;
7447 case MULT:
7448 if (code != PLUS && code != MINUS)
7449 return x;
7450 break;
7452 case ASHIFT:
7453 /* This is also a multiply, so it distributes over everything. */
7454 break;
7456 case SUBREG:
7457 /* Non-paradoxical SUBREGs distributes over all operations, provided
7458 the inner modes and word numbers are the same, this is an extraction
7459 of a low-order part, we don't convert an fp operation to int or
7460 vice versa, and we would not be converting a single-word
7461 operation into a multi-word operation. The latter test is not
7462 required, but it prevents generating unneeded multi-word operations.
7463 Some of the previous tests are redundant given the latter test, but
7464 are retained because they are required for correctness.
7466 We produce the result slightly differently in this case. */
7468 if (GET_MODE (SUBREG_REG (lhs)) != GET_MODE (SUBREG_REG (rhs))
7469 || SUBREG_WORD (lhs) != SUBREG_WORD (rhs)
7470 || ! subreg_lowpart_p (lhs)
7471 || (GET_MODE_CLASS (GET_MODE (lhs))
7472 != GET_MODE_CLASS (GET_MODE (SUBREG_REG (lhs))))
7473 || (GET_MODE_SIZE (GET_MODE (lhs))
7474 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))))
7475 || GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))) > UNITS_PER_WORD)
7476 return x;
7478 tem = gen_binary (code, GET_MODE (SUBREG_REG (lhs)),
7479 SUBREG_REG (lhs), SUBREG_REG (rhs));
7480 return gen_lowpart_for_combine (GET_MODE (x), tem);
7482 default:
7483 return x;
7486 /* Set LHS and RHS to the inner operands (A and B in the example
7487 above) and set OTHER to the common operand (C in the example).
7488 These is only one way to do this unless the inner operation is
7489 commutative. */
7490 if (GET_RTX_CLASS (inner_code) == 'c'
7491 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
7492 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
7493 else if (GET_RTX_CLASS (inner_code) == 'c'
7494 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
7495 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
7496 else if (GET_RTX_CLASS (inner_code) == 'c'
7497 && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
7498 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
7499 else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
7500 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
7501 else
7502 return x;
7504 /* Form the new inner operation, seeing if it simplifies first. */
7505 tem = gen_binary (code, GET_MODE (x), lhs, rhs);
7507 /* There is one exception to the general way of distributing:
7508 (a ^ b) | (a ^ c) -> (~a) & (b ^ c) */
7509 if (code == XOR && inner_code == IOR)
7511 inner_code = AND;
7512 other = gen_unary (NOT, GET_MODE (x), GET_MODE (x), other);
7515 /* We may be able to continuing distributing the result, so call
7516 ourselves recursively on the inner operation before forming the
7517 outer operation, which we return. */
7518 return gen_binary (inner_code, GET_MODE (x),
7519 apply_distributive_law (tem), other);
7522 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
7523 in MODE.
7525 Return an equivalent form, if different from X. Otherwise, return X. If
7526 X is zero, we are to always construct the equivalent form. */
7528 static rtx
7529 simplify_and_const_int (x, mode, varop, constop)
7530 rtx x;
7531 enum machine_mode mode;
7532 rtx varop;
7533 unsigned HOST_WIDE_INT constop;
7535 unsigned HOST_WIDE_INT nonzero;
7536 int i;
7538 /* Simplify VAROP knowing that we will be only looking at some of the
7539 bits in it. */
7540 varop = force_to_mode (varop, mode, constop, NULL_RTX, 0);
7542 /* If VAROP is a CLOBBER, we will fail so return it; if it is a
7543 CONST_INT, we are done. */
7544 if (GET_CODE (varop) == CLOBBER || GET_CODE (varop) == CONST_INT)
7545 return varop;
7547 /* See what bits may be nonzero in VAROP. Unlike the general case of
7548 a call to nonzero_bits, here we don't care about bits outside
7549 MODE. */
7551 nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
7552 nonzero = trunc_int_for_mode (nonzero, mode);
7554 /* Turn off all bits in the constant that are known to already be zero.
7555 Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
7556 which is tested below. */
7558 constop &= nonzero;
7560 /* If we don't have any bits left, return zero. */
7561 if (constop == 0)
7562 return const0_rtx;
7564 /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
7565 a power of two, we can replace this with a ASHIFT. */
7566 if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
7567 && (i = exact_log2 (constop)) >= 0)
7568 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
7570 /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
7571 or XOR, then try to apply the distributive law. This may eliminate
7572 operations if either branch can be simplified because of the AND.
7573 It may also make some cases more complex, but those cases probably
7574 won't match a pattern either with or without this. */
7576 if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
7577 return
7578 gen_lowpart_for_combine
7579 (mode,
7580 apply_distributive_law
7581 (gen_binary (GET_CODE (varop), GET_MODE (varop),
7582 simplify_and_const_int (NULL_RTX, GET_MODE (varop),
7583 XEXP (varop, 0), constop),
7584 simplify_and_const_int (NULL_RTX, GET_MODE (varop),
7585 XEXP (varop, 1), constop))));
7587 /* Get VAROP in MODE. Try to get a SUBREG if not. Don't make a new SUBREG
7588 if we already had one (just check for the simplest cases). */
7589 if (x && GET_CODE (XEXP (x, 0)) == SUBREG
7590 && GET_MODE (XEXP (x, 0)) == mode
7591 && SUBREG_REG (XEXP (x, 0)) == varop)
7592 varop = XEXP (x, 0);
7593 else
7594 varop = gen_lowpart_for_combine (mode, varop);
7596 /* If we can't make the SUBREG, try to return what we were given. */
7597 if (GET_CODE (varop) == CLOBBER)
7598 return x ? x : varop;
7600 /* If we are only masking insignificant bits, return VAROP. */
7601 if (constop == nonzero)
7602 x = varop;
7604 /* Otherwise, return an AND. See how much, if any, of X we can use. */
7605 else if (x == 0 || GET_CODE (x) != AND || GET_MODE (x) != mode)
7606 x = gen_binary (AND, mode, varop, GEN_INT (constop));
7608 else
7610 if (GET_CODE (XEXP (x, 1)) != CONST_INT
7611 || (unsigned HOST_WIDE_INT) INTVAL (XEXP (x, 1)) != constop)
7612 SUBST (XEXP (x, 1), GEN_INT (constop));
7614 SUBST (XEXP (x, 0), varop);
7617 return x;
7620 /* We let num_sign_bit_copies recur into nonzero_bits as that is useful.
7621 We don't let nonzero_bits recur into num_sign_bit_copies, because that
7622 is less useful. We can't allow both, because that results in exponential
7623 run time recursion. There is a nullstone testcase that triggered
7624 this. This macro avoids accidental uses of num_sign_bit_copies. */
7625 #define num_sign_bit_copies()
7627 /* Given an expression, X, compute which bits in X can be non-zero.
7628 We don't care about bits outside of those defined in MODE.
7630 For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
7631 a shift, AND, or zero_extract, we can do better. */
7633 static unsigned HOST_WIDE_INT
7634 nonzero_bits (x, mode)
7635 rtx x;
7636 enum machine_mode mode;
7638 unsigned HOST_WIDE_INT nonzero = GET_MODE_MASK (mode);
7639 unsigned HOST_WIDE_INT inner_nz;
7640 enum rtx_code code;
7641 int mode_width = GET_MODE_BITSIZE (mode);
7642 rtx tem;
7644 /* For floating-point values, assume all bits are needed. */
7645 if (FLOAT_MODE_P (GET_MODE (x)) || FLOAT_MODE_P (mode))
7646 return nonzero;
7648 /* If X is wider than MODE, use its mode instead. */
7649 if (GET_MODE_BITSIZE (GET_MODE (x)) > mode_width)
7651 mode = GET_MODE (x);
7652 nonzero = GET_MODE_MASK (mode);
7653 mode_width = GET_MODE_BITSIZE (mode);
7656 if (mode_width > HOST_BITS_PER_WIDE_INT)
7657 /* Our only callers in this case look for single bit values. So
7658 just return the mode mask. Those tests will then be false. */
7659 return nonzero;
7661 #ifndef WORD_REGISTER_OPERATIONS
7662 /* If MODE is wider than X, but both are a single word for both the host
7663 and target machines, we can compute this from which bits of the
7664 object might be nonzero in its own mode, taking into account the fact
7665 that on many CISC machines, accessing an object in a wider mode
7666 causes the high-order bits to become undefined. So they are
7667 not known to be zero. */
7669 if (GET_MODE (x) != VOIDmode && GET_MODE (x) != mode
7670 && GET_MODE_BITSIZE (GET_MODE (x)) <= BITS_PER_WORD
7671 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
7672 && GET_MODE_BITSIZE (mode) > GET_MODE_BITSIZE (GET_MODE (x)))
7674 nonzero &= nonzero_bits (x, GET_MODE (x));
7675 nonzero |= GET_MODE_MASK (mode) & ~ GET_MODE_MASK (GET_MODE (x));
7676 return nonzero;
7678 #endif
7680 code = GET_CODE (x);
7681 switch (code)
7683 case REG:
7684 #ifdef POINTERS_EXTEND_UNSIGNED
7685 /* If pointers extend unsigned and this is a pointer in Pmode, say that
7686 all the bits above ptr_mode are known to be zero. */
7687 if (POINTERS_EXTEND_UNSIGNED && GET_MODE (x) == Pmode
7688 && REGNO_POINTER_FLAG (REGNO (x)))
7689 nonzero &= GET_MODE_MASK (ptr_mode);
7690 #endif
7692 #ifdef STACK_BOUNDARY
7693 /* If this is the stack pointer, we may know something about its
7694 alignment. If PUSH_ROUNDING is defined, it is possible for the
7695 stack to be momentarily aligned only to that amount, so we pick
7696 the least alignment. */
7698 /* We can't check for arg_pointer_rtx here, because it is not
7699 guaranteed to have as much alignment as the stack pointer.
7700 In particular, in the Irix6 n64 ABI, the stack has 128 bit
7701 alignment but the argument pointer has only 64 bit alignment. */
7703 if ((x == frame_pointer_rtx
7704 || x == stack_pointer_rtx
7705 || x == hard_frame_pointer_rtx
7706 || (REGNO (x) >= FIRST_VIRTUAL_REGISTER
7707 && REGNO (x) <= LAST_VIRTUAL_REGISTER))
7708 #ifdef STACK_BIAS
7709 && !STACK_BIAS
7710 #endif
7713 int sp_alignment = STACK_BOUNDARY / BITS_PER_UNIT;
7715 #ifdef PUSH_ROUNDING
7716 if (REGNO (x) == STACK_POINTER_REGNUM)
7717 sp_alignment = MIN (PUSH_ROUNDING (1), sp_alignment);
7718 #endif
7720 /* We must return here, otherwise we may get a worse result from
7721 one of the choices below. There is nothing useful below as
7722 far as the stack pointer is concerned. */
7723 return nonzero &= ~ (sp_alignment - 1);
7725 #endif
7727 /* If X is a register whose nonzero bits value is current, use it.
7728 Otherwise, if X is a register whose value we can find, use that
7729 value. Otherwise, use the previously-computed global nonzero bits
7730 for this register. */
7732 if (reg_last_set_value[REGNO (x)] != 0
7733 && reg_last_set_mode[REGNO (x)] == mode
7734 && (reg_last_set_label[REGNO (x)] == label_tick
7735 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
7736 && REG_N_SETS (REGNO (x)) == 1
7737 && ! REGNO_REG_SET_P (BASIC_BLOCK (0)->global_live_at_start,
7738 REGNO (x))))
7739 && INSN_CUID (reg_last_set[REGNO (x)]) < subst_low_cuid)
7740 return reg_last_set_nonzero_bits[REGNO (x)];
7742 tem = get_last_value (x);
7744 if (tem)
7746 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
7747 /* If X is narrower than MODE and TEM is a non-negative
7748 constant that would appear negative in the mode of X,
7749 sign-extend it for use in reg_nonzero_bits because some
7750 machines (maybe most) will actually do the sign-extension
7751 and this is the conservative approach.
7753 ??? For 2.5, try to tighten up the MD files in this regard
7754 instead of this kludge. */
7756 if (GET_MODE_BITSIZE (GET_MODE (x)) < mode_width
7757 && GET_CODE (tem) == CONST_INT
7758 && INTVAL (tem) > 0
7759 && 0 != (INTVAL (tem)
7760 & ((HOST_WIDE_INT) 1
7761 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
7762 tem = GEN_INT (INTVAL (tem)
7763 | ((HOST_WIDE_INT) (-1)
7764 << GET_MODE_BITSIZE (GET_MODE (x))));
7765 #endif
7766 return nonzero_bits (tem, mode);
7768 else if (nonzero_sign_valid && reg_nonzero_bits[REGNO (x)])
7769 return reg_nonzero_bits[REGNO (x)] & nonzero;
7770 else
7771 return nonzero;
7773 case CONST_INT:
7774 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
7775 /* If X is negative in MODE, sign-extend the value. */
7776 if (INTVAL (x) > 0 && mode_width < BITS_PER_WORD
7777 && 0 != (INTVAL (x) & ((HOST_WIDE_INT) 1 << (mode_width - 1))))
7778 return (INTVAL (x) | ((HOST_WIDE_INT) (-1) << mode_width));
7779 #endif
7781 return INTVAL (x);
7783 case MEM:
7784 #ifdef LOAD_EXTEND_OP
7785 /* In many, if not most, RISC machines, reading a byte from memory
7786 zeros the rest of the register. Noticing that fact saves a lot
7787 of extra zero-extends. */
7788 if (LOAD_EXTEND_OP (GET_MODE (x)) == ZERO_EXTEND)
7789 nonzero &= GET_MODE_MASK (GET_MODE (x));
7790 #endif
7791 break;
7793 case EQ: case NE:
7794 case GT: case GTU:
7795 case LT: case LTU:
7796 case GE: case GEU:
7797 case LE: case LEU:
7799 /* If this produces an integer result, we know which bits are set.
7800 Code here used to clear bits outside the mode of X, but that is
7801 now done above. */
7803 if (GET_MODE_CLASS (mode) == MODE_INT
7804 && mode_width <= HOST_BITS_PER_WIDE_INT)
7805 nonzero = STORE_FLAG_VALUE;
7806 break;
7808 case NEG:
7809 #if 0
7810 /* Disabled to avoid exponential mutual recursion between nonzero_bits
7811 and num_sign_bit_copies. */
7812 if (num_sign_bit_copies (XEXP (x, 0), GET_MODE (x))
7813 == GET_MODE_BITSIZE (GET_MODE (x)))
7814 nonzero = 1;
7815 #endif
7817 if (GET_MODE_SIZE (GET_MODE (x)) < mode_width)
7818 nonzero |= (GET_MODE_MASK (mode) & ~ GET_MODE_MASK (GET_MODE (x)));
7819 break;
7821 case ABS:
7822 #if 0
7823 /* Disabled to avoid exponential mutual recursion between nonzero_bits
7824 and num_sign_bit_copies. */
7825 if (num_sign_bit_copies (XEXP (x, 0), GET_MODE (x))
7826 == GET_MODE_BITSIZE (GET_MODE (x)))
7827 nonzero = 1;
7828 #endif
7829 break;
7831 case TRUNCATE:
7832 nonzero &= (nonzero_bits (XEXP (x, 0), mode) & GET_MODE_MASK (mode));
7833 break;
7835 case ZERO_EXTEND:
7836 nonzero &= nonzero_bits (XEXP (x, 0), mode);
7837 if (GET_MODE (XEXP (x, 0)) != VOIDmode)
7838 nonzero &= GET_MODE_MASK (GET_MODE (XEXP (x, 0)));
7839 break;
7841 case SIGN_EXTEND:
7842 /* If the sign bit is known clear, this is the same as ZERO_EXTEND.
7843 Otherwise, show all the bits in the outer mode but not the inner
7844 may be non-zero. */
7845 inner_nz = nonzero_bits (XEXP (x, 0), mode);
7846 if (GET_MODE (XEXP (x, 0)) != VOIDmode)
7848 inner_nz &= GET_MODE_MASK (GET_MODE (XEXP (x, 0)));
7849 if (inner_nz
7850 & (((HOST_WIDE_INT) 1
7851 << (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - 1))))
7852 inner_nz |= (GET_MODE_MASK (mode)
7853 & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0))));
7856 nonzero &= inner_nz;
7857 break;
7859 case AND:
7860 nonzero &= (nonzero_bits (XEXP (x, 0), mode)
7861 & nonzero_bits (XEXP (x, 1), mode));
7862 break;
7864 case XOR: case IOR:
7865 case UMIN: case UMAX: case SMIN: case SMAX:
7866 nonzero &= (nonzero_bits (XEXP (x, 0), mode)
7867 | nonzero_bits (XEXP (x, 1), mode));
7868 break;
7870 case PLUS: case MINUS:
7871 case MULT:
7872 case DIV: case UDIV:
7873 case MOD: case UMOD:
7874 /* We can apply the rules of arithmetic to compute the number of
7875 high- and low-order zero bits of these operations. We start by
7876 computing the width (position of the highest-order non-zero bit)
7877 and the number of low-order zero bits for each value. */
7879 unsigned HOST_WIDE_INT nz0 = nonzero_bits (XEXP (x, 0), mode);
7880 unsigned HOST_WIDE_INT nz1 = nonzero_bits (XEXP (x, 1), mode);
7881 int width0 = floor_log2 (nz0) + 1;
7882 int width1 = floor_log2 (nz1) + 1;
7883 int low0 = floor_log2 (nz0 & -nz0);
7884 int low1 = floor_log2 (nz1 & -nz1);
7885 HOST_WIDE_INT op0_maybe_minusp
7886 = (nz0 & ((HOST_WIDE_INT) 1 << (mode_width - 1)));
7887 HOST_WIDE_INT op1_maybe_minusp
7888 = (nz1 & ((HOST_WIDE_INT) 1 << (mode_width - 1)));
7889 int result_width = mode_width;
7890 int result_low = 0;
7892 switch (code)
7894 case PLUS:
7895 #ifdef STACK_BIAS
7896 if (STACK_BIAS
7897 && (XEXP (x, 0) == stack_pointer_rtx
7898 || XEXP (x, 0) == frame_pointer_rtx)
7899 && GET_CODE (XEXP (x, 1)) == CONST_INT)
7901 int sp_alignment = STACK_BOUNDARY / BITS_PER_UNIT;
7903 nz0 = (GET_MODE_MASK (mode) & ~ (sp_alignment - 1));
7904 nz1 = INTVAL (XEXP (x, 1)) - STACK_BIAS;
7905 width0 = floor_log2 (nz0) + 1;
7906 width1 = floor_log2 (nz1) + 1;
7907 low0 = floor_log2 (nz0 & -nz0);
7908 low1 = floor_log2 (nz1 & -nz1);
7910 #endif
7911 result_width = MAX (width0, width1) + 1;
7912 result_low = MIN (low0, low1);
7913 break;
7914 case MINUS:
7915 result_low = MIN (low0, low1);
7916 break;
7917 case MULT:
7918 result_width = width0 + width1;
7919 result_low = low0 + low1;
7920 break;
7921 case DIV:
7922 if (! op0_maybe_minusp && ! op1_maybe_minusp)
7923 result_width = width0;
7924 break;
7925 case UDIV:
7926 result_width = width0;
7927 break;
7928 case MOD:
7929 if (! op0_maybe_minusp && ! op1_maybe_minusp)
7930 result_width = MIN (width0, width1);
7931 result_low = MIN (low0, low1);
7932 break;
7933 case UMOD:
7934 result_width = MIN (width0, width1);
7935 result_low = MIN (low0, low1);
7936 break;
7937 default:
7938 abort ();
7941 if (result_width < mode_width)
7942 nonzero &= ((HOST_WIDE_INT) 1 << result_width) - 1;
7944 if (result_low > 0)
7945 nonzero &= ~ (((HOST_WIDE_INT) 1 << result_low) - 1);
7947 break;
7949 case ZERO_EXTRACT:
7950 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7951 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
7952 nonzero &= ((HOST_WIDE_INT) 1 << INTVAL (XEXP (x, 1))) - 1;
7953 break;
7955 case SUBREG:
7956 /* If this is a SUBREG formed for a promoted variable that has
7957 been zero-extended, we know that at least the high-order bits
7958 are zero, though others might be too. */
7960 if (SUBREG_PROMOTED_VAR_P (x) && SUBREG_PROMOTED_UNSIGNED_P (x))
7961 nonzero = (GET_MODE_MASK (GET_MODE (x))
7962 & nonzero_bits (SUBREG_REG (x), GET_MODE (x)));
7964 /* If the inner mode is a single word for both the host and target
7965 machines, we can compute this from which bits of the inner
7966 object might be nonzero. */
7967 if (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))) <= BITS_PER_WORD
7968 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x)))
7969 <= HOST_BITS_PER_WIDE_INT))
7971 nonzero &= nonzero_bits (SUBREG_REG (x), mode);
7973 #if defined (WORD_REGISTER_OPERATIONS) && defined (LOAD_EXTEND_OP)
7974 /* If this is a typical RISC machine, we only have to worry
7975 about the way loads are extended. */
7976 if (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (x))) == SIGN_EXTEND
7977 ? (nonzero
7978 & (1L << (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))) - 1)))
7979 : LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (x))) != ZERO_EXTEND)
7980 #endif
7982 /* On many CISC machines, accessing an object in a wider mode
7983 causes the high-order bits to become undefined. So they are
7984 not known to be zero. */
7985 if (GET_MODE_SIZE (GET_MODE (x))
7986 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
7987 nonzero |= (GET_MODE_MASK (GET_MODE (x))
7988 & ~ GET_MODE_MASK (GET_MODE (SUBREG_REG (x))));
7991 break;
7993 case ASHIFTRT:
7994 case LSHIFTRT:
7995 case ASHIFT:
7996 case ROTATE:
7997 /* The nonzero bits are in two classes: any bits within MODE
7998 that aren't in GET_MODE (x) are always significant. The rest of the
7999 nonzero bits are those that are significant in the operand of
8000 the shift when shifted the appropriate number of bits. This
8001 shows that high-order bits are cleared by the right shift and
8002 low-order bits by left shifts. */
8003 if (GET_CODE (XEXP (x, 1)) == CONST_INT
8004 && INTVAL (XEXP (x, 1)) >= 0
8005 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
8007 enum machine_mode inner_mode = GET_MODE (x);
8008 int width = GET_MODE_BITSIZE (inner_mode);
8009 int count = INTVAL (XEXP (x, 1));
8010 unsigned HOST_WIDE_INT mode_mask = GET_MODE_MASK (inner_mode);
8011 unsigned HOST_WIDE_INT op_nonzero = nonzero_bits (XEXP (x, 0), mode);
8012 unsigned HOST_WIDE_INT inner = op_nonzero & mode_mask;
8013 unsigned HOST_WIDE_INT outer = 0;
8015 if (mode_width > width)
8016 outer = (op_nonzero & nonzero & ~ mode_mask);
8018 if (code == LSHIFTRT)
8019 inner >>= count;
8020 else if (code == ASHIFTRT)
8022 inner >>= count;
8024 /* If the sign bit may have been nonzero before the shift, we
8025 need to mark all the places it could have been copied to
8026 by the shift as possibly nonzero. */
8027 if (inner & ((HOST_WIDE_INT) 1 << (width - 1 - count)))
8028 inner |= (((HOST_WIDE_INT) 1 << count) - 1) << (width - count);
8030 else if (code == ASHIFT)
8031 inner <<= count;
8032 else
8033 inner = ((inner << (count % width)
8034 | (inner >> (width - (count % width)))) & mode_mask);
8036 nonzero &= (outer | inner);
8038 break;
8040 case FFS:
8041 /* This is at most the number of bits in the mode. */
8042 nonzero = ((HOST_WIDE_INT) 1 << (floor_log2 (mode_width) + 1)) - 1;
8043 break;
8045 case IF_THEN_ELSE:
8046 nonzero &= (nonzero_bits (XEXP (x, 1), mode)
8047 | nonzero_bits (XEXP (x, 2), mode));
8048 break;
8050 default:
8051 break;
8054 return nonzero;
8057 /* See the macro definition above. */
8058 #undef num_sign_bit_copies
8060 /* Return the number of bits at the high-order end of X that are known to
8061 be equal to the sign bit. X will be used in mode MODE; if MODE is
8062 VOIDmode, X will be used in its own mode. The returned value will always
8063 be between 1 and the number of bits in MODE. */
8065 static int
8066 num_sign_bit_copies (x, mode)
8067 rtx x;
8068 enum machine_mode mode;
8070 enum rtx_code code = GET_CODE (x);
8071 int bitwidth;
8072 int num0, num1, result;
8073 unsigned HOST_WIDE_INT nonzero;
8074 rtx tem;
8076 /* If we weren't given a mode, use the mode of X. If the mode is still
8077 VOIDmode, we don't know anything. Likewise if one of the modes is
8078 floating-point. */
8080 if (mode == VOIDmode)
8081 mode = GET_MODE (x);
8083 if (mode == VOIDmode || FLOAT_MODE_P (mode) || FLOAT_MODE_P (GET_MODE (x)))
8084 return 1;
8086 bitwidth = GET_MODE_BITSIZE (mode);
8088 /* For a smaller object, just ignore the high bits. */
8089 if (bitwidth < GET_MODE_BITSIZE (GET_MODE (x)))
8090 return MAX (1, (num_sign_bit_copies (x, GET_MODE (x))
8091 - (GET_MODE_BITSIZE (GET_MODE (x)) - bitwidth)));
8093 if (GET_MODE (x) != VOIDmode && bitwidth > GET_MODE_BITSIZE (GET_MODE (x)))
8095 #ifndef WORD_REGISTER_OPERATIONS
8096 /* If this machine does not do all register operations on the entire
8097 register and MODE is wider than the mode of X, we can say nothing
8098 at all about the high-order bits. */
8099 return 1;
8100 #else
8101 /* Likewise on machines that do, if the mode of the object is smaller
8102 than a word and loads of that size don't sign extend, we can say
8103 nothing about the high order bits. */
8104 if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
8105 #ifdef LOAD_EXTEND_OP
8106 && LOAD_EXTEND_OP (GET_MODE (x)) != SIGN_EXTEND
8107 #endif
8109 return 1;
8110 #endif
8113 switch (code)
8115 case REG:
8117 #ifdef POINTERS_EXTEND_UNSIGNED
8118 /* If pointers extend signed and this is a pointer in Pmode, say that
8119 all the bits above ptr_mode are known to be sign bit copies. */
8120 if (! POINTERS_EXTEND_UNSIGNED && GET_MODE (x) == Pmode && mode == Pmode
8121 && REGNO_POINTER_FLAG (REGNO (x)))
8122 return GET_MODE_BITSIZE (Pmode) - GET_MODE_BITSIZE (ptr_mode) + 1;
8123 #endif
8125 if (reg_last_set_value[REGNO (x)] != 0
8126 && reg_last_set_mode[REGNO (x)] == mode
8127 && (reg_last_set_label[REGNO (x)] == label_tick
8128 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
8129 && REG_N_SETS (REGNO (x)) == 1
8130 && ! REGNO_REG_SET_P (BASIC_BLOCK (0)->global_live_at_start,
8131 REGNO (x))))
8132 && INSN_CUID (reg_last_set[REGNO (x)]) < subst_low_cuid)
8133 return reg_last_set_sign_bit_copies[REGNO (x)];
8135 tem = get_last_value (x);
8136 if (tem != 0)
8137 return num_sign_bit_copies (tem, mode);
8139 if (nonzero_sign_valid && reg_sign_bit_copies[REGNO (x)] != 0)
8140 return reg_sign_bit_copies[REGNO (x)];
8141 break;
8143 case MEM:
8144 #ifdef LOAD_EXTEND_OP
8145 /* Some RISC machines sign-extend all loads of smaller than a word. */
8146 if (LOAD_EXTEND_OP (GET_MODE (x)) == SIGN_EXTEND)
8147 return MAX (1, bitwidth - GET_MODE_BITSIZE (GET_MODE (x)) + 1);
8148 #endif
8149 break;
8151 case CONST_INT:
8152 /* If the constant is negative, take its 1's complement and remask.
8153 Then see how many zero bits we have. */
8154 nonzero = INTVAL (x) & GET_MODE_MASK (mode);
8155 if (bitwidth <= HOST_BITS_PER_WIDE_INT
8156 && (nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
8157 nonzero = (~ nonzero) & GET_MODE_MASK (mode);
8159 return (nonzero == 0 ? bitwidth : bitwidth - floor_log2 (nonzero) - 1);
8161 case SUBREG:
8162 /* If this is a SUBREG for a promoted object that is sign-extended
8163 and we are looking at it in a wider mode, we know that at least the
8164 high-order bits are known to be sign bit copies. */
8166 if (SUBREG_PROMOTED_VAR_P (x) && ! SUBREG_PROMOTED_UNSIGNED_P (x))
8167 return MAX (bitwidth - GET_MODE_BITSIZE (GET_MODE (x)) + 1,
8168 num_sign_bit_copies (SUBREG_REG (x), mode));
8170 /* For a smaller object, just ignore the high bits. */
8171 if (bitwidth <= GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))))
8173 num0 = num_sign_bit_copies (SUBREG_REG (x), VOIDmode);
8174 return MAX (1, (num0
8175 - (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x)))
8176 - bitwidth)));
8179 #ifdef WORD_REGISTER_OPERATIONS
8180 #ifdef LOAD_EXTEND_OP
8181 /* For paradoxical SUBREGs on machines where all register operations
8182 affect the entire register, just look inside. Note that we are
8183 passing MODE to the recursive call, so the number of sign bit copies
8184 will remain relative to that mode, not the inner mode. */
8186 /* This works only if loads sign extend. Otherwise, if we get a
8187 reload for the inner part, it may be loaded from the stack, and
8188 then we lose all sign bit copies that existed before the store
8189 to the stack. */
8191 if ((GET_MODE_SIZE (GET_MODE (x))
8192 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
8193 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (x))) == SIGN_EXTEND)
8194 return num_sign_bit_copies (SUBREG_REG (x), mode);
8195 #endif
8196 #endif
8197 break;
8199 case SIGN_EXTRACT:
8200 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
8201 return MAX (1, bitwidth - INTVAL (XEXP (x, 1)));
8202 break;
8204 case SIGN_EXTEND:
8205 return (bitwidth - GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
8206 + num_sign_bit_copies (XEXP (x, 0), VOIDmode));
8208 case TRUNCATE:
8209 /* For a smaller object, just ignore the high bits. */
8210 num0 = num_sign_bit_copies (XEXP (x, 0), VOIDmode);
8211 return MAX (1, (num0 - (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
8212 - bitwidth)));
8214 case NOT:
8215 return num_sign_bit_copies (XEXP (x, 0), mode);
8217 case ROTATE: case ROTATERT:
8218 /* If we are rotating left by a number of bits less than the number
8219 of sign bit copies, we can just subtract that amount from the
8220 number. */
8221 if (GET_CODE (XEXP (x, 1)) == CONST_INT
8222 && INTVAL (XEXP (x, 1)) >= 0 && INTVAL (XEXP (x, 1)) < bitwidth)
8224 num0 = num_sign_bit_copies (XEXP (x, 0), mode);
8225 return MAX (1, num0 - (code == ROTATE ? INTVAL (XEXP (x, 1))
8226 : bitwidth - INTVAL (XEXP (x, 1))));
8228 break;
8230 case NEG:
8231 /* In general, this subtracts one sign bit copy. But if the value
8232 is known to be positive, the number of sign bit copies is the
8233 same as that of the input. Finally, if the input has just one bit
8234 that might be nonzero, all the bits are copies of the sign bit. */
8235 num0 = num_sign_bit_copies (XEXP (x, 0), mode);
8236 if (bitwidth > HOST_BITS_PER_WIDE_INT)
8237 return num0 > 1 ? num0 - 1 : 1;
8239 nonzero = nonzero_bits (XEXP (x, 0), mode);
8240 if (nonzero == 1)
8241 return bitwidth;
8243 if (num0 > 1
8244 && (((HOST_WIDE_INT) 1 << (bitwidth - 1)) & nonzero))
8245 num0--;
8247 return num0;
8249 case IOR: case AND: case XOR:
8250 case SMIN: case SMAX: case UMIN: case UMAX:
8251 /* Logical operations will preserve the number of sign-bit copies.
8252 MIN and MAX operations always return one of the operands. */
8253 num0 = num_sign_bit_copies (XEXP (x, 0), mode);
8254 num1 = num_sign_bit_copies (XEXP (x, 1), mode);
8255 return MIN (num0, num1);
8257 case PLUS: case MINUS:
8258 /* For addition and subtraction, we can have a 1-bit carry. However,
8259 if we are subtracting 1 from a positive number, there will not
8260 be such a carry. Furthermore, if the positive number is known to
8261 be 0 or 1, we know the result is either -1 or 0. */
8263 if (code == PLUS && XEXP (x, 1) == constm1_rtx
8264 && bitwidth <= HOST_BITS_PER_WIDE_INT)
8266 nonzero = nonzero_bits (XEXP (x, 0), mode);
8267 if ((((HOST_WIDE_INT) 1 << (bitwidth - 1)) & nonzero) == 0)
8268 return (nonzero == 1 || nonzero == 0 ? bitwidth
8269 : bitwidth - floor_log2 (nonzero) - 1);
8272 num0 = num_sign_bit_copies (XEXP (x, 0), mode);
8273 num1 = num_sign_bit_copies (XEXP (x, 1), mode);
8274 return MAX (1, MIN (num0, num1) - 1);
8276 case MULT:
8277 /* The number of bits of the product is the sum of the number of
8278 bits of both terms. However, unless one of the terms if known
8279 to be positive, we must allow for an additional bit since negating
8280 a negative number can remove one sign bit copy. */
8282 num0 = num_sign_bit_copies (XEXP (x, 0), mode);
8283 num1 = num_sign_bit_copies (XEXP (x, 1), mode);
8285 result = bitwidth - (bitwidth - num0) - (bitwidth - num1);
8286 if (result > 0
8287 && (bitwidth > HOST_BITS_PER_WIDE_INT
8288 || (((nonzero_bits (XEXP (x, 0), mode)
8289 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
8290 && ((nonzero_bits (XEXP (x, 1), mode)
8291 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0))))
8292 result--;
8294 return MAX (1, result);
8296 case UDIV:
8297 /* The result must be <= the first operand. If the first operand
8298 has the high bit set, we know nothing about the number of sign
8299 bit copies. */
8300 if (bitwidth > HOST_BITS_PER_WIDE_INT)
8301 return 1;
8302 else if ((nonzero_bits (XEXP (x, 0), mode)
8303 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
8304 return 1;
8305 else
8306 return num_sign_bit_copies (XEXP (x, 0), mode);
8308 case UMOD:
8309 /* The result must be <= the scond operand. */
8310 return num_sign_bit_copies (XEXP (x, 1), mode);
8312 case DIV:
8313 /* Similar to unsigned division, except that we have to worry about
8314 the case where the divisor is negative, in which case we have
8315 to add 1. */
8316 result = num_sign_bit_copies (XEXP (x, 0), mode);
8317 if (result > 1
8318 && (bitwidth > HOST_BITS_PER_WIDE_INT
8319 || (nonzero_bits (XEXP (x, 1), mode)
8320 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0))
8321 result--;
8323 return result;
8325 case MOD:
8326 result = num_sign_bit_copies (XEXP (x, 1), mode);
8327 if (result > 1
8328 && (bitwidth > HOST_BITS_PER_WIDE_INT
8329 || (nonzero_bits (XEXP (x, 1), mode)
8330 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0))
8331 result--;
8333 return result;
8335 case ASHIFTRT:
8336 /* Shifts by a constant add to the number of bits equal to the
8337 sign bit. */
8338 num0 = num_sign_bit_copies (XEXP (x, 0), mode);
8339 if (GET_CODE (XEXP (x, 1)) == CONST_INT
8340 && INTVAL (XEXP (x, 1)) > 0)
8341 num0 = MIN (bitwidth, num0 + INTVAL (XEXP (x, 1)));
8343 return num0;
8345 case ASHIFT:
8346 /* Left shifts destroy copies. */
8347 if (GET_CODE (XEXP (x, 1)) != CONST_INT
8348 || INTVAL (XEXP (x, 1)) < 0
8349 || INTVAL (XEXP (x, 1)) >= bitwidth)
8350 return 1;
8352 num0 = num_sign_bit_copies (XEXP (x, 0), mode);
8353 return MAX (1, num0 - INTVAL (XEXP (x, 1)));
8355 case IF_THEN_ELSE:
8356 num0 = num_sign_bit_copies (XEXP (x, 1), mode);
8357 num1 = num_sign_bit_copies (XEXP (x, 2), mode);
8358 return MIN (num0, num1);
8360 case EQ: case NE: case GE: case GT: case LE: case LT:
8361 case GEU: case GTU: case LEU: case LTU:
8362 if (STORE_FLAG_VALUE == -1)
8363 return bitwidth;
8364 break;
8366 default:
8367 break;
8370 /* If we haven't been able to figure it out by one of the above rules,
8371 see if some of the high-order bits are known to be zero. If so,
8372 count those bits and return one less than that amount. If we can't
8373 safely compute the mask for this mode, always return BITWIDTH. */
8375 if (bitwidth > HOST_BITS_PER_WIDE_INT)
8376 return 1;
8378 nonzero = nonzero_bits (x, mode);
8379 return (nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))
8380 ? 1 : bitwidth - floor_log2 (nonzero) - 1);
8383 /* Return the number of "extended" bits there are in X, when interpreted
8384 as a quantity in MODE whose signedness is indicated by UNSIGNEDP. For
8385 unsigned quantities, this is the number of high-order zero bits.
8386 For signed quantities, this is the number of copies of the sign bit
8387 minus 1. In both case, this function returns the number of "spare"
8388 bits. For example, if two quantities for which this function returns
8389 at least 1 are added, the addition is known not to overflow.
8391 This function will always return 0 unless called during combine, which
8392 implies that it must be called from a define_split. */
8395 extended_count (x, mode, unsignedp)
8396 rtx x;
8397 enum machine_mode mode;
8398 int unsignedp;
8400 if (nonzero_sign_valid == 0)
8401 return 0;
8403 return (unsignedp
8404 ? (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
8405 && (GET_MODE_BITSIZE (mode) - 1
8406 - floor_log2 (nonzero_bits (x, mode))))
8407 : num_sign_bit_copies (x, mode) - 1);
8410 /* This function is called from `simplify_shift_const' to merge two
8411 outer operations. Specifically, we have already found that we need
8412 to perform operation *POP0 with constant *PCONST0 at the outermost
8413 position. We would now like to also perform OP1 with constant CONST1
8414 (with *POP0 being done last).
8416 Return 1 if we can do the operation and update *POP0 and *PCONST0 with
8417 the resulting operation. *PCOMP_P is set to 1 if we would need to
8418 complement the innermost operand, otherwise it is unchanged.
8420 MODE is the mode in which the operation will be done. No bits outside
8421 the width of this mode matter. It is assumed that the width of this mode
8422 is smaller than or equal to HOST_BITS_PER_WIDE_INT.
8424 If *POP0 or OP1 are NIL, it means no operation is required. Only NEG, PLUS,
8425 IOR, XOR, and AND are supported. We may set *POP0 to SET if the proper
8426 result is simply *PCONST0.
8428 If the resulting operation cannot be expressed as one operation, we
8429 return 0 and do not change *POP0, *PCONST0, and *PCOMP_P. */
8431 static int
8432 merge_outer_ops (pop0, pconst0, op1, const1, mode, pcomp_p)
8433 enum rtx_code *pop0;
8434 HOST_WIDE_INT *pconst0;
8435 enum rtx_code op1;
8436 HOST_WIDE_INT const1;
8437 enum machine_mode mode;
8438 int *pcomp_p;
8440 enum rtx_code op0 = *pop0;
8441 HOST_WIDE_INT const0 = *pconst0;
8443 const0 &= GET_MODE_MASK (mode);
8444 const1 &= GET_MODE_MASK (mode);
8446 /* If OP0 is an AND, clear unimportant bits in CONST1. */
8447 if (op0 == AND)
8448 const1 &= const0;
8450 /* If OP0 or OP1 is NIL, this is easy. Similarly if they are the same or
8451 if OP0 is SET. */
8453 if (op1 == NIL || op0 == SET)
8454 return 1;
8456 else if (op0 == NIL)
8457 op0 = op1, const0 = const1;
8459 else if (op0 == op1)
8461 switch (op0)
8463 case AND:
8464 const0 &= const1;
8465 break;
8466 case IOR:
8467 const0 |= const1;
8468 break;
8469 case XOR:
8470 const0 ^= const1;
8471 break;
8472 case PLUS:
8473 const0 += const1;
8474 break;
8475 case NEG:
8476 op0 = NIL;
8477 break;
8478 default:
8479 break;
8483 /* Otherwise, if either is a PLUS or NEG, we can't do anything. */
8484 else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
8485 return 0;
8487 /* If the two constants aren't the same, we can't do anything. The
8488 remaining six cases can all be done. */
8489 else if (const0 != const1)
8490 return 0;
8492 else
8493 switch (op0)
8495 case IOR:
8496 if (op1 == AND)
8497 /* (a & b) | b == b */
8498 op0 = SET;
8499 else /* op1 == XOR */
8500 /* (a ^ b) | b == a | b */
8502 break;
8504 case XOR:
8505 if (op1 == AND)
8506 /* (a & b) ^ b == (~a) & b */
8507 op0 = AND, *pcomp_p = 1;
8508 else /* op1 == IOR */
8509 /* (a | b) ^ b == a & ~b */
8510 op0 = AND, *pconst0 = ~ const0;
8511 break;
8513 case AND:
8514 if (op1 == IOR)
8515 /* (a | b) & b == b */
8516 op0 = SET;
8517 else /* op1 == XOR */
8518 /* (a ^ b) & b) == (~a) & b */
8519 *pcomp_p = 1;
8520 break;
8521 default:
8522 break;
8525 /* Check for NO-OP cases. */
8526 const0 &= GET_MODE_MASK (mode);
8527 if (const0 == 0
8528 && (op0 == IOR || op0 == XOR || op0 == PLUS))
8529 op0 = NIL;
8530 else if (const0 == 0 && op0 == AND)
8531 op0 = SET;
8532 else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
8533 && op0 == AND)
8534 op0 = NIL;
8536 /* ??? Slightly redundant with the above mask, but not entirely.
8537 Moving this above means we'd have to sign-extend the mode mask
8538 for the final test. */
8539 const0 = trunc_int_for_mode (const0, mode);
8541 *pop0 = op0;
8542 *pconst0 = const0;
8544 return 1;
8547 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
8548 The result of the shift is RESULT_MODE. X, if non-zero, is an expression
8549 that we started with.
8551 The shift is normally computed in the widest mode we find in VAROP, as
8552 long as it isn't a different number of words than RESULT_MODE. Exceptions
8553 are ASHIFTRT and ROTATE, which are always done in their original mode, */
8555 static rtx
8556 simplify_shift_const (x, code, result_mode, varop, count)
8557 rtx x;
8558 enum rtx_code code;
8559 enum machine_mode result_mode;
8560 rtx varop;
8561 int count;
8563 enum rtx_code orig_code = code;
8564 int orig_count = count;
8565 enum machine_mode mode = result_mode;
8566 enum machine_mode shift_mode, tmode;
8567 int mode_words
8568 = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
8569 /* We form (outer_op (code varop count) (outer_const)). */
8570 enum rtx_code outer_op = NIL;
8571 HOST_WIDE_INT outer_const = 0;
8572 rtx const_rtx;
8573 int complement_p = 0;
8574 rtx new;
8576 /* If we were given an invalid count, don't do anything except exactly
8577 what was requested. */
8579 if (count < 0 || count > GET_MODE_BITSIZE (mode))
8581 if (x)
8582 return x;
8584 return gen_rtx_fmt_ee (code, mode, varop, GEN_INT (count));
8587 /* Unless one of the branches of the `if' in this loop does a `continue',
8588 we will `break' the loop after the `if'. */
8590 while (count != 0)
8592 /* If we have an operand of (clobber (const_int 0)), just return that
8593 value. */
8594 if (GET_CODE (varop) == CLOBBER)
8595 return varop;
8597 /* If we discovered we had to complement VAROP, leave. Making a NOT
8598 here would cause an infinite loop. */
8599 if (complement_p)
8600 break;
8602 /* Convert ROTATERT to ROTATE. */
8603 if (code == ROTATERT)
8604 code = ROTATE, count = GET_MODE_BITSIZE (result_mode) - count;
8606 /* We need to determine what mode we will do the shift in. If the
8607 shift is a right shift or a ROTATE, we must always do it in the mode
8608 it was originally done in. Otherwise, we can do it in MODE, the
8609 widest mode encountered. */
8610 shift_mode
8611 = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
8612 ? result_mode : mode);
8614 /* Handle cases where the count is greater than the size of the mode
8615 minus 1. For ASHIFT, use the size minus one as the count (this can
8616 occur when simplifying (lshiftrt (ashiftrt ..))). For rotates,
8617 take the count modulo the size. For other shifts, the result is
8618 zero.
8620 Since these shifts are being produced by the compiler by combining
8621 multiple operations, each of which are defined, we know what the
8622 result is supposed to be. */
8624 if (count > GET_MODE_BITSIZE (shift_mode) - 1)
8626 if (code == ASHIFTRT)
8627 count = GET_MODE_BITSIZE (shift_mode) - 1;
8628 else if (code == ROTATE || code == ROTATERT)
8629 count %= GET_MODE_BITSIZE (shift_mode);
8630 else
8632 /* We can't simply return zero because there may be an
8633 outer op. */
8634 varop = const0_rtx;
8635 count = 0;
8636 break;
8640 /* Negative counts are invalid and should not have been made (a
8641 programmer-specified negative count should have been handled
8642 above). */
8643 else if (count < 0)
8644 abort ();
8646 /* An arithmetic right shift of a quantity known to be -1 or 0
8647 is a no-op. */
8648 if (code == ASHIFTRT
8649 && (num_sign_bit_copies (varop, shift_mode)
8650 == GET_MODE_BITSIZE (shift_mode)))
8652 count = 0;
8653 break;
8656 /* If we are doing an arithmetic right shift and discarding all but
8657 the sign bit copies, this is equivalent to doing a shift by the
8658 bitsize minus one. Convert it into that shift because it will often
8659 allow other simplifications. */
8661 if (code == ASHIFTRT
8662 && (count + num_sign_bit_copies (varop, shift_mode)
8663 >= GET_MODE_BITSIZE (shift_mode)))
8664 count = GET_MODE_BITSIZE (shift_mode) - 1;
8666 /* We simplify the tests below and elsewhere by converting
8667 ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
8668 `make_compound_operation' will convert it to a ASHIFTRT for
8669 those machines (such as Vax) that don't have a LSHIFTRT. */
8670 if (GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
8671 && code == ASHIFTRT
8672 && ((nonzero_bits (varop, shift_mode)
8673 & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (shift_mode) - 1)))
8674 == 0))
8675 code = LSHIFTRT;
8677 switch (GET_CODE (varop))
8679 case SIGN_EXTEND:
8680 case ZERO_EXTEND:
8681 case SIGN_EXTRACT:
8682 case ZERO_EXTRACT:
8683 new = expand_compound_operation (varop);
8684 if (new != varop)
8686 varop = new;
8687 continue;
8689 break;
8691 case MEM:
8692 /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
8693 minus the width of a smaller mode, we can do this with a
8694 SIGN_EXTEND or ZERO_EXTEND from the narrower memory location. */
8695 if ((code == ASHIFTRT || code == LSHIFTRT)
8696 && ! mode_dependent_address_p (XEXP (varop, 0))
8697 && ! MEM_VOLATILE_P (varop)
8698 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
8699 MODE_INT, 1)) != BLKmode)
8701 if (BYTES_BIG_ENDIAN)
8702 new = gen_rtx_MEM (tmode, XEXP (varop, 0));
8703 else
8704 new = gen_rtx_MEM (tmode,
8705 plus_constant (XEXP (varop, 0),
8706 count / BITS_PER_UNIT));
8707 RTX_UNCHANGING_P (new) = RTX_UNCHANGING_P (varop);
8708 MEM_COPY_ATTRIBUTES (new, varop);
8709 varop = gen_rtx_combine (code == ASHIFTRT ? SIGN_EXTEND
8710 : ZERO_EXTEND, mode, new);
8711 count = 0;
8712 continue;
8714 break;
8716 case USE:
8717 /* Similar to the case above, except that we can only do this if
8718 the resulting mode is the same as that of the underlying
8719 MEM and adjust the address depending on the *bits* endianness
8720 because of the way that bit-field extract insns are defined. */
8721 if ((code == ASHIFTRT || code == LSHIFTRT)
8722 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
8723 MODE_INT, 1)) != BLKmode
8724 && tmode == GET_MODE (XEXP (varop, 0)))
8726 if (BITS_BIG_ENDIAN)
8727 new = XEXP (varop, 0);
8728 else
8730 new = copy_rtx (XEXP (varop, 0));
8731 SUBST (XEXP (new, 0),
8732 plus_constant (XEXP (new, 0),
8733 count / BITS_PER_UNIT));
8736 varop = gen_rtx_combine (code == ASHIFTRT ? SIGN_EXTEND
8737 : ZERO_EXTEND, mode, new);
8738 count = 0;
8739 continue;
8741 break;
8743 case SUBREG:
8744 /* If VAROP is a SUBREG, strip it as long as the inner operand has
8745 the same number of words as what we've seen so far. Then store
8746 the widest mode in MODE. */
8747 if (subreg_lowpart_p (varop)
8748 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
8749 > GET_MODE_SIZE (GET_MODE (varop)))
8750 && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
8751 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
8752 == mode_words))
8754 varop = SUBREG_REG (varop);
8755 if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
8756 mode = GET_MODE (varop);
8757 continue;
8759 break;
8761 case MULT:
8762 /* Some machines use MULT instead of ASHIFT because MULT
8763 is cheaper. But it is still better on those machines to
8764 merge two shifts into one. */
8765 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8766 && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
8768 varop = gen_binary (ASHIFT, GET_MODE (varop), XEXP (varop, 0),
8769 GEN_INT (exact_log2 (INTVAL (XEXP (varop, 1)))));
8770 continue;
8772 break;
8774 case UDIV:
8775 /* Similar, for when divides are cheaper. */
8776 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8777 && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
8779 varop = gen_binary (LSHIFTRT, GET_MODE (varop), XEXP (varop, 0),
8780 GEN_INT (exact_log2 (INTVAL (XEXP (varop, 1)))));
8781 continue;
8783 break;
8785 case ASHIFTRT:
8786 /* If we are extracting just the sign bit of an arithmetic right
8787 shift, that shift is not needed. */
8788 if (code == LSHIFTRT && count == GET_MODE_BITSIZE (result_mode) - 1)
8790 varop = XEXP (varop, 0);
8791 continue;
8794 /* ... fall through ... */
8796 case LSHIFTRT:
8797 case ASHIFT:
8798 case ROTATE:
8799 /* Here we have two nested shifts. The result is usually the
8800 AND of a new shift with a mask. We compute the result below. */
8801 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8802 && INTVAL (XEXP (varop, 1)) >= 0
8803 && INTVAL (XEXP (varop, 1)) < GET_MODE_BITSIZE (GET_MODE (varop))
8804 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
8805 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
8807 enum rtx_code first_code = GET_CODE (varop);
8808 int first_count = INTVAL (XEXP (varop, 1));
8809 unsigned HOST_WIDE_INT mask;
8810 rtx mask_rtx;
8812 /* We have one common special case. We can't do any merging if
8813 the inner code is an ASHIFTRT of a smaller mode. However, if
8814 we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
8815 with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
8816 we can convert it to
8817 (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0 C2) C3) C1).
8818 This simplifies certain SIGN_EXTEND operations. */
8819 if (code == ASHIFT && first_code == ASHIFTRT
8820 && (GET_MODE_BITSIZE (result_mode)
8821 - GET_MODE_BITSIZE (GET_MODE (varop))) == count)
8823 /* C3 has the low-order C1 bits zero. */
8825 mask = (GET_MODE_MASK (mode)
8826 & ~ (((HOST_WIDE_INT) 1 << first_count) - 1));
8828 varop = simplify_and_const_int (NULL_RTX, result_mode,
8829 XEXP (varop, 0), mask);
8830 varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
8831 varop, count);
8832 count = first_count;
8833 code = ASHIFTRT;
8834 continue;
8837 /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
8838 than C1 high-order bits equal to the sign bit, we can convert
8839 this to either an ASHIFT or a ASHIFTRT depending on the
8840 two counts.
8842 We cannot do this if VAROP's mode is not SHIFT_MODE. */
8844 if (code == ASHIFTRT && first_code == ASHIFT
8845 && GET_MODE (varop) == shift_mode
8846 && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
8847 > first_count))
8849 count -= first_count;
8850 if (count < 0)
8851 count = - count, code = ASHIFT;
8852 varop = XEXP (varop, 0);
8853 continue;
8856 /* There are some cases we can't do. If CODE is ASHIFTRT,
8857 we can only do this if FIRST_CODE is also ASHIFTRT.
8859 We can't do the case when CODE is ROTATE and FIRST_CODE is
8860 ASHIFTRT.
8862 If the mode of this shift is not the mode of the outer shift,
8863 we can't do this if either shift is a right shift or ROTATE.
8865 Finally, we can't do any of these if the mode is too wide
8866 unless the codes are the same.
8868 Handle the case where the shift codes are the same
8869 first. */
8871 if (code == first_code)
8873 if (GET_MODE (varop) != result_mode
8874 && (code == ASHIFTRT || code == LSHIFTRT
8875 || code == ROTATE))
8876 break;
8878 count += first_count;
8879 varop = XEXP (varop, 0);
8880 continue;
8883 if (code == ASHIFTRT
8884 || (code == ROTATE && first_code == ASHIFTRT)
8885 || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT
8886 || (GET_MODE (varop) != result_mode
8887 && (first_code == ASHIFTRT || first_code == LSHIFTRT
8888 || first_code == ROTATE
8889 || code == ROTATE)))
8890 break;
8892 /* To compute the mask to apply after the shift, shift the
8893 nonzero bits of the inner shift the same way the
8894 outer shift will. */
8896 mask_rtx = GEN_INT (nonzero_bits (varop, GET_MODE (varop)));
8898 mask_rtx
8899 = simplify_binary_operation (code, result_mode, mask_rtx,
8900 GEN_INT (count));
8902 /* Give up if we can't compute an outer operation to use. */
8903 if (mask_rtx == 0
8904 || GET_CODE (mask_rtx) != CONST_INT
8905 || ! merge_outer_ops (&outer_op, &outer_const, AND,
8906 INTVAL (mask_rtx),
8907 result_mode, &complement_p))
8908 break;
8910 /* If the shifts are in the same direction, we add the
8911 counts. Otherwise, we subtract them. */
8912 if ((code == ASHIFTRT || code == LSHIFTRT)
8913 == (first_code == ASHIFTRT || first_code == LSHIFTRT))
8914 count += first_count;
8915 else
8916 count -= first_count;
8918 /* If COUNT is positive, the new shift is usually CODE,
8919 except for the two exceptions below, in which case it is
8920 FIRST_CODE. If the count is negative, FIRST_CODE should
8921 always be used */
8922 if (count > 0
8923 && ((first_code == ROTATE && code == ASHIFT)
8924 || (first_code == ASHIFTRT && code == LSHIFTRT)))
8925 code = first_code;
8926 else if (count < 0)
8927 code = first_code, count = - count;
8929 varop = XEXP (varop, 0);
8930 continue;
8933 /* If we have (A << B << C) for any shift, we can convert this to
8934 (A << C << B). This wins if A is a constant. Only try this if
8935 B is not a constant. */
8937 else if (GET_CODE (varop) == code
8938 && GET_CODE (XEXP (varop, 1)) != CONST_INT
8939 && 0 != (new
8940 = simplify_binary_operation (code, mode,
8941 XEXP (varop, 0),
8942 GEN_INT (count))))
8944 varop = gen_rtx_combine (code, mode, new, XEXP (varop, 1));
8945 count = 0;
8946 continue;
8948 break;
8950 case NOT:
8951 /* Make this fit the case below. */
8952 varop = gen_rtx_combine (XOR, mode, XEXP (varop, 0),
8953 GEN_INT (GET_MODE_MASK (mode)));
8954 continue;
8956 case IOR:
8957 case AND:
8958 case XOR:
8959 /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
8960 with C the size of VAROP - 1 and the shift is logical if
8961 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
8962 we have an (le X 0) operation. If we have an arithmetic shift
8963 and STORE_FLAG_VALUE is 1 or we have a logical shift with
8964 STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation. */
8966 if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
8967 && XEXP (XEXP (varop, 0), 1) == constm1_rtx
8968 && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8969 && (code == LSHIFTRT || code == ASHIFTRT)
8970 && count == GET_MODE_BITSIZE (GET_MODE (varop)) - 1
8971 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
8973 count = 0;
8974 varop = gen_rtx_combine (LE, GET_MODE (varop), XEXP (varop, 1),
8975 const0_rtx);
8977 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
8978 varop = gen_rtx_combine (NEG, GET_MODE (varop), varop);
8980 continue;
8983 /* If we have (shift (logical)), move the logical to the outside
8984 to allow it to possibly combine with another logical and the
8985 shift to combine with another shift. This also canonicalizes to
8986 what a ZERO_EXTRACT looks like. Also, some machines have
8987 (and (shift)) insns. */
8989 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8990 && (new = simplify_binary_operation (code, result_mode,
8991 XEXP (varop, 1),
8992 GEN_INT (count))) != 0
8993 && GET_CODE(new) == CONST_INT
8994 && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
8995 INTVAL (new), result_mode, &complement_p))
8997 varop = XEXP (varop, 0);
8998 continue;
9001 /* If we can't do that, try to simplify the shift in each arm of the
9002 logical expression, make a new logical expression, and apply
9003 the inverse distributive law. */
9005 rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
9006 XEXP (varop, 0), count);
9007 rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
9008 XEXP (varop, 1), count);
9010 varop = gen_binary (GET_CODE (varop), shift_mode, lhs, rhs);
9011 varop = apply_distributive_law (varop);
9013 count = 0;
9015 break;
9017 case EQ:
9018 /* convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
9019 says that the sign bit can be tested, FOO has mode MODE, C is
9020 GET_MODE_BITSIZE (MODE) - 1, and FOO has only its low-order bit
9021 that may be nonzero. */
9022 if (code == LSHIFTRT
9023 && XEXP (varop, 1) == const0_rtx
9024 && GET_MODE (XEXP (varop, 0)) == result_mode
9025 && count == GET_MODE_BITSIZE (result_mode) - 1
9026 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9027 && ((STORE_FLAG_VALUE
9028 & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (result_mode) - 1))))
9029 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
9030 && merge_outer_ops (&outer_op, &outer_const, XOR,
9031 (HOST_WIDE_INT) 1, result_mode,
9032 &complement_p))
9034 varop = XEXP (varop, 0);
9035 count = 0;
9036 continue;
9038 break;
9040 case NEG:
9041 /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
9042 than the number of bits in the mode is equivalent to A. */
9043 if (code == LSHIFTRT && count == GET_MODE_BITSIZE (result_mode) - 1
9044 && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
9046 varop = XEXP (varop, 0);
9047 count = 0;
9048 continue;
9051 /* NEG commutes with ASHIFT since it is multiplication. Move the
9052 NEG outside to allow shifts to combine. */
9053 if (code == ASHIFT
9054 && merge_outer_ops (&outer_op, &outer_const, NEG,
9055 (HOST_WIDE_INT) 0, result_mode,
9056 &complement_p))
9058 varop = XEXP (varop, 0);
9059 continue;
9061 break;
9063 case PLUS:
9064 /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
9065 is one less than the number of bits in the mode is
9066 equivalent to (xor A 1). */
9067 if (code == LSHIFTRT && count == GET_MODE_BITSIZE (result_mode) - 1
9068 && XEXP (varop, 1) == constm1_rtx
9069 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
9070 && merge_outer_ops (&outer_op, &outer_const, XOR,
9071 (HOST_WIDE_INT) 1, result_mode,
9072 &complement_p))
9074 count = 0;
9075 varop = XEXP (varop, 0);
9076 continue;
9079 /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
9080 that might be nonzero in BAR are those being shifted out and those
9081 bits are known zero in FOO, we can replace the PLUS with FOO.
9082 Similarly in the other operand order. This code occurs when
9083 we are computing the size of a variable-size array. */
9085 if ((code == ASHIFTRT || code == LSHIFTRT)
9086 && count < HOST_BITS_PER_WIDE_INT
9087 && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
9088 && (nonzero_bits (XEXP (varop, 1), result_mode)
9089 & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
9091 varop = XEXP (varop, 0);
9092 continue;
9094 else if ((code == ASHIFTRT || code == LSHIFTRT)
9095 && count < HOST_BITS_PER_WIDE_INT
9096 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9097 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
9098 >> count)
9099 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
9100 & nonzero_bits (XEXP (varop, 1),
9101 result_mode)))
9103 varop = XEXP (varop, 1);
9104 continue;
9107 /* (ashift (plus foo C) N) is (plus (ashift foo N) C'). */
9108 if (code == ASHIFT
9109 && GET_CODE (XEXP (varop, 1)) == CONST_INT
9110 && (new = simplify_binary_operation (ASHIFT, result_mode,
9111 XEXP (varop, 1),
9112 GEN_INT (count))) != 0
9113 && GET_CODE(new) == CONST_INT
9114 && merge_outer_ops (&outer_op, &outer_const, PLUS,
9115 INTVAL (new), result_mode, &complement_p))
9117 varop = XEXP (varop, 0);
9118 continue;
9120 break;
9122 case MINUS:
9123 /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
9124 with C the size of VAROP - 1 and the shift is logical if
9125 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
9126 we have a (gt X 0) operation. If the shift is arithmetic with
9127 STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
9128 we have a (neg (gt X 0)) operation. */
9130 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9131 && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
9132 && count == GET_MODE_BITSIZE (GET_MODE (varop)) - 1
9133 && (code == LSHIFTRT || code == ASHIFTRT)
9134 && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
9135 && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
9136 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
9138 count = 0;
9139 varop = gen_rtx_combine (GT, GET_MODE (varop), XEXP (varop, 1),
9140 const0_rtx);
9142 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
9143 varop = gen_rtx_combine (NEG, GET_MODE (varop), varop);
9145 continue;
9147 break;
9149 case TRUNCATE:
9150 /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
9151 if the truncate does not affect the value. */
9152 if (code == LSHIFTRT
9153 && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
9154 && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
9155 && (INTVAL (XEXP (XEXP (varop, 0), 1))
9156 >= (GET_MODE_BITSIZE (GET_MODE (XEXP (varop, 0)))
9157 - GET_MODE_BITSIZE (GET_MODE (varop)))))
9159 rtx varop_inner = XEXP (varop, 0);
9161 varop_inner = gen_rtx_combine (LSHIFTRT,
9162 GET_MODE (varop_inner),
9163 XEXP (varop_inner, 0),
9164 GEN_INT (count + INTVAL (XEXP (varop_inner, 1))));
9165 varop = gen_rtx_combine (TRUNCATE, GET_MODE (varop),
9166 varop_inner);
9167 count = 0;
9168 continue;
9170 break;
9172 default:
9173 break;
9176 break;
9179 /* We need to determine what mode to do the shift in. If the shift is
9180 a right shift or ROTATE, we must always do it in the mode it was
9181 originally done in. Otherwise, we can do it in MODE, the widest mode
9182 encountered. The code we care about is that of the shift that will
9183 actually be done, not the shift that was originally requested. */
9184 shift_mode
9185 = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
9186 ? result_mode : mode);
9188 /* We have now finished analyzing the shift. The result should be
9189 a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places. If
9190 OUTER_OP is non-NIL, it is an operation that needs to be applied
9191 to the result of the shift. OUTER_CONST is the relevant constant,
9192 but we must turn off all bits turned off in the shift.
9194 If we were passed a value for X, see if we can use any pieces of
9195 it. If not, make new rtx. */
9197 if (x && GET_RTX_CLASS (GET_CODE (x)) == '2'
9198 && GET_CODE (XEXP (x, 1)) == CONST_INT
9199 && INTVAL (XEXP (x, 1)) == count)
9200 const_rtx = XEXP (x, 1);
9201 else
9202 const_rtx = GEN_INT (count);
9204 if (x && GET_CODE (XEXP (x, 0)) == SUBREG
9205 && GET_MODE (XEXP (x, 0)) == shift_mode
9206 && SUBREG_REG (XEXP (x, 0)) == varop)
9207 varop = XEXP (x, 0);
9208 else if (GET_MODE (varop) != shift_mode)
9209 varop = gen_lowpart_for_combine (shift_mode, varop);
9211 /* If we can't make the SUBREG, try to return what we were given. */
9212 if (GET_CODE (varop) == CLOBBER)
9213 return x ? x : varop;
9215 new = simplify_binary_operation (code, shift_mode, varop, const_rtx);
9216 if (new != 0)
9217 x = new;
9218 else
9220 if (x == 0 || GET_CODE (x) != code || GET_MODE (x) != shift_mode)
9221 x = gen_rtx_combine (code, shift_mode, varop, const_rtx);
9223 SUBST (XEXP (x, 0), varop);
9224 SUBST (XEXP (x, 1), const_rtx);
9227 /* If we have an outer operation and we just made a shift, it is
9228 possible that we could have simplified the shift were it not
9229 for the outer operation. So try to do the simplification
9230 recursively. */
9232 if (outer_op != NIL && GET_CODE (x) == code
9233 && GET_CODE (XEXP (x, 1)) == CONST_INT)
9234 x = simplify_shift_const (x, code, shift_mode, XEXP (x, 0),
9235 INTVAL (XEXP (x, 1)));
9237 /* If we were doing a LSHIFTRT in a wider mode than it was originally,
9238 turn off all the bits that the shift would have turned off. */
9239 if (orig_code == LSHIFTRT && result_mode != shift_mode)
9240 x = simplify_and_const_int (NULL_RTX, shift_mode, x,
9241 GET_MODE_MASK (result_mode) >> orig_count);
9243 /* Do the remainder of the processing in RESULT_MODE. */
9244 x = gen_lowpart_for_combine (result_mode, x);
9246 /* If COMPLEMENT_P is set, we have to complement X before doing the outer
9247 operation. */
9248 if (complement_p)
9249 x = gen_unary (NOT, result_mode, result_mode, x);
9251 if (outer_op != NIL)
9253 if (GET_MODE_BITSIZE (result_mode) < HOST_BITS_PER_WIDE_INT)
9254 outer_const = trunc_int_for_mode (outer_const, result_mode);
9256 if (outer_op == AND)
9257 x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
9258 else if (outer_op == SET)
9259 /* This means that we have determined that the result is
9260 equivalent to a constant. This should be rare. */
9261 x = GEN_INT (outer_const);
9262 else if (GET_RTX_CLASS (outer_op) == '1')
9263 x = gen_unary (outer_op, result_mode, result_mode, x);
9264 else
9265 x = gen_binary (outer_op, result_mode, x, GEN_INT (outer_const));
9268 return x;
9271 /* Like recog, but we receive the address of a pointer to a new pattern.
9272 We try to match the rtx that the pointer points to.
9273 If that fails, we may try to modify or replace the pattern,
9274 storing the replacement into the same pointer object.
9276 Modifications include deletion or addition of CLOBBERs.
9278 PNOTES is a pointer to a location where any REG_UNUSED notes added for
9279 the CLOBBERs are placed.
9281 The value is the final insn code from the pattern ultimately matched,
9282 or -1. */
9284 static int
9285 recog_for_combine (pnewpat, insn, pnotes)
9286 rtx *pnewpat;
9287 rtx insn;
9288 rtx *pnotes;
9290 register rtx pat = *pnewpat;
9291 int insn_code_number;
9292 int num_clobbers_to_add = 0;
9293 int i;
9294 rtx notes = 0;
9296 /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
9297 we use to indicate that something didn't match. If we find such a
9298 thing, force rejection. */
9299 if (GET_CODE (pat) == PARALLEL)
9300 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
9301 if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
9302 && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
9303 return -1;
9305 /* Is the result of combination a valid instruction? */
9306 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
9308 /* If it isn't, there is the possibility that we previously had an insn
9309 that clobbered some register as a side effect, but the combined
9310 insn doesn't need to do that. So try once more without the clobbers
9311 unless this represents an ASM insn. */
9313 if (insn_code_number < 0 && ! check_asm_operands (pat)
9314 && GET_CODE (pat) == PARALLEL)
9316 int pos;
9318 for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
9319 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
9321 if (i != pos)
9322 SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
9323 pos++;
9326 SUBST_INT (XVECLEN (pat, 0), pos);
9328 if (pos == 1)
9329 pat = XVECEXP (pat, 0, 0);
9331 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
9334 /* If we had any clobbers to add, make a new pattern than contains
9335 them. Then check to make sure that all of them are dead. */
9336 if (num_clobbers_to_add)
9338 rtx newpat = gen_rtx_PARALLEL (VOIDmode,
9339 gen_rtvec (GET_CODE (pat) == PARALLEL
9340 ? (XVECLEN (pat, 0)
9341 + num_clobbers_to_add)
9342 : num_clobbers_to_add + 1));
9344 if (GET_CODE (pat) == PARALLEL)
9345 for (i = 0; i < XVECLEN (pat, 0); i++)
9346 XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
9347 else
9348 XVECEXP (newpat, 0, 0) = pat;
9350 add_clobbers (newpat, insn_code_number);
9352 for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
9353 i < XVECLEN (newpat, 0); i++)
9355 if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) == REG
9356 && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
9357 return -1;
9358 notes = gen_rtx_EXPR_LIST (REG_UNUSED,
9359 XEXP (XVECEXP (newpat, 0, i), 0), notes);
9361 pat = newpat;
9364 *pnewpat = pat;
9365 *pnotes = notes;
9367 return insn_code_number;
9370 /* Like gen_lowpart but for use by combine. In combine it is not possible
9371 to create any new pseudoregs. However, it is safe to create
9372 invalid memory addresses, because combine will try to recognize
9373 them and all they will do is make the combine attempt fail.
9375 If for some reason this cannot do its job, an rtx
9376 (clobber (const_int 0)) is returned.
9377 An insn containing that will not be recognized. */
9379 #undef gen_lowpart
9381 static rtx
9382 gen_lowpart_for_combine (mode, x)
9383 enum machine_mode mode;
9384 register rtx x;
9386 rtx result;
9388 if (GET_MODE (x) == mode)
9389 return x;
9391 /* We can only support MODE being wider than a word if X is a
9392 constant integer or has a mode the same size. */
9394 if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
9395 && ! ((GET_MODE (x) == VOIDmode
9396 && (GET_CODE (x) == CONST_INT
9397 || GET_CODE (x) == CONST_DOUBLE))
9398 || GET_MODE_SIZE (GET_MODE (x)) == GET_MODE_SIZE (mode)))
9399 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
9401 /* X might be a paradoxical (subreg (mem)). In that case, gen_lowpart
9402 won't know what to do. So we will strip off the SUBREG here and
9403 process normally. */
9404 if (GET_CODE (x) == SUBREG && GET_CODE (SUBREG_REG (x)) == MEM)
9406 x = SUBREG_REG (x);
9407 if (GET_MODE (x) == mode)
9408 return x;
9411 result = gen_lowpart_common (mode, x);
9412 if (result != 0
9413 && GET_CODE (result) == SUBREG
9414 && GET_CODE (SUBREG_REG (result)) == REG
9415 && REGNO (SUBREG_REG (result)) >= FIRST_PSEUDO_REGISTER
9416 && (GET_MODE_SIZE (GET_MODE (result))
9417 != GET_MODE_SIZE (GET_MODE (SUBREG_REG (result)))))
9418 REG_CHANGES_SIZE (REGNO (SUBREG_REG (result))) = 1;
9420 if (result)
9421 return result;
9423 if (GET_CODE (x) == MEM)
9425 register int offset = 0;
9426 rtx new;
9428 /* Refuse to work on a volatile memory ref or one with a mode-dependent
9429 address. */
9430 if (MEM_VOLATILE_P (x) || mode_dependent_address_p (XEXP (x, 0)))
9431 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
9433 /* If we want to refer to something bigger than the original memref,
9434 generate a perverse subreg instead. That will force a reload
9435 of the original memref X. */
9436 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode))
9437 return gen_rtx_SUBREG (mode, x, 0);
9439 if (WORDS_BIG_ENDIAN)
9440 offset = (MAX (GET_MODE_SIZE (GET_MODE (x)), UNITS_PER_WORD)
9441 - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD));
9443 if (BYTES_BIG_ENDIAN)
9445 /* Adjust the address so that the address-after-the-data is
9446 unchanged. */
9447 offset -= (MIN (UNITS_PER_WORD, GET_MODE_SIZE (mode))
9448 - MIN (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (x))));
9450 new = gen_rtx_MEM (mode, plus_constant (XEXP (x, 0), offset));
9451 RTX_UNCHANGING_P (new) = RTX_UNCHANGING_P (x);
9452 MEM_COPY_ATTRIBUTES (new, x);
9453 return new;
9456 /* If X is a comparison operator, rewrite it in a new mode. This
9457 probably won't match, but may allow further simplifications. */
9458 else if (GET_RTX_CLASS (GET_CODE (x)) == '<')
9459 return gen_rtx_combine (GET_CODE (x), mode, XEXP (x, 0), XEXP (x, 1));
9461 /* If we couldn't simplify X any other way, just enclose it in a
9462 SUBREG. Normally, this SUBREG won't match, but some patterns may
9463 include an explicit SUBREG or we may simplify it further in combine. */
9464 else
9466 int word = 0;
9468 if (WORDS_BIG_ENDIAN && GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD)
9469 word = ((GET_MODE_SIZE (GET_MODE (x))
9470 - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD))
9471 / UNITS_PER_WORD);
9472 return gen_rtx_SUBREG (mode, x, word);
9476 /* Make an rtx expression. This is a subset of gen_rtx and only supports
9477 expressions of 1, 2, or 3 operands, each of which are rtx expressions.
9479 If the identical expression was previously in the insn (in the undobuf),
9480 it will be returned. Only if it is not found will a new expression
9481 be made. */
9483 /*VARARGS2*/
9484 static rtx
9485 gen_rtx_combine VPROTO((enum rtx_code code, enum machine_mode mode, ...))
9487 #ifndef ANSI_PROTOTYPES
9488 enum rtx_code code;
9489 enum machine_mode mode;
9490 #endif
9491 va_list p;
9492 int n_args;
9493 rtx args[3];
9494 int j;
9495 const char *fmt;
9496 rtx rt;
9497 struct undo *undo;
9499 VA_START (p, mode);
9501 #ifndef ANSI_PROTOTYPES
9502 code = va_arg (p, enum rtx_code);
9503 mode = va_arg (p, enum machine_mode);
9504 #endif
9506 n_args = GET_RTX_LENGTH (code);
9507 fmt = GET_RTX_FORMAT (code);
9509 if (n_args == 0 || n_args > 3)
9510 abort ();
9512 /* Get each arg and verify that it is supposed to be an expression. */
9513 for (j = 0; j < n_args; j++)
9515 if (*fmt++ != 'e')
9516 abort ();
9518 args[j] = va_arg (p, rtx);
9521 va_end (p);
9523 /* See if this is in undobuf. Be sure we don't use objects that came
9524 from another insn; this could produce circular rtl structures. */
9526 for (undo = undobuf.undos; undo != undobuf.previous_undos; undo = undo->next)
9527 if (!undo->is_int
9528 && GET_CODE (undo->old_contents.r) == code
9529 && GET_MODE (undo->old_contents.r) == mode)
9531 for (j = 0; j < n_args; j++)
9532 if (XEXP (undo->old_contents.r, j) != args[j])
9533 break;
9535 if (j == n_args)
9536 return undo->old_contents.r;
9539 /* Otherwise make a new rtx. We know we have 1, 2, or 3 args.
9540 Use rtx_alloc instead of gen_rtx because it's faster on RISC. */
9541 rt = rtx_alloc (code);
9542 PUT_MODE (rt, mode);
9543 XEXP (rt, 0) = args[0];
9544 if (n_args > 1)
9546 XEXP (rt, 1) = args[1];
9547 if (n_args > 2)
9548 XEXP (rt, 2) = args[2];
9550 return rt;
9553 /* These routines make binary and unary operations by first seeing if they
9554 fold; if not, a new expression is allocated. */
9556 static rtx
9557 gen_binary (code, mode, op0, op1)
9558 enum rtx_code code;
9559 enum machine_mode mode;
9560 rtx op0, op1;
9562 rtx result;
9563 rtx tem;
9565 if (GET_RTX_CLASS (code) == 'c'
9566 && (GET_CODE (op0) == CONST_INT
9567 || (CONSTANT_P (op0) && GET_CODE (op1) != CONST_INT)))
9568 tem = op0, op0 = op1, op1 = tem;
9570 if (GET_RTX_CLASS (code) == '<')
9572 enum machine_mode op_mode = GET_MODE (op0);
9574 /* Strip the COMPARE from (REL_OP (compare X Y) 0) to get
9575 just (REL_OP X Y). */
9576 if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
9578 op1 = XEXP (op0, 1);
9579 op0 = XEXP (op0, 0);
9580 op_mode = GET_MODE (op0);
9583 if (op_mode == VOIDmode)
9584 op_mode = GET_MODE (op1);
9585 result = simplify_relational_operation (code, op_mode, op0, op1);
9587 else
9588 result = simplify_binary_operation (code, mode, op0, op1);
9590 if (result)
9591 return result;
9593 /* Put complex operands first and constants second. */
9594 if (GET_RTX_CLASS (code) == 'c'
9595 && ((CONSTANT_P (op0) && GET_CODE (op1) != CONST_INT)
9596 || (GET_RTX_CLASS (GET_CODE (op0)) == 'o'
9597 && GET_RTX_CLASS (GET_CODE (op1)) != 'o')
9598 || (GET_CODE (op0) == SUBREG
9599 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (op0))) == 'o'
9600 && GET_RTX_CLASS (GET_CODE (op1)) != 'o')))
9601 return gen_rtx_combine (code, mode, op1, op0);
9603 /* If we are turning off bits already known off in OP0, we need not do
9604 an AND. */
9605 else if (code == AND && GET_CODE (op1) == CONST_INT
9606 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
9607 && (nonzero_bits (op0, mode) & ~ INTVAL (op1)) == 0)
9608 return op0;
9610 return gen_rtx_combine (code, mode, op0, op1);
9613 static rtx
9614 gen_unary (code, mode, op0_mode, op0)
9615 enum rtx_code code;
9616 enum machine_mode mode, op0_mode;
9617 rtx op0;
9619 rtx result = simplify_unary_operation (code, mode, op0, op0_mode);
9621 if (result)
9622 return result;
9624 return gen_rtx_combine (code, mode, op0);
9627 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
9628 comparison code that will be tested.
9630 The result is a possibly different comparison code to use. *POP0 and
9631 *POP1 may be updated.
9633 It is possible that we might detect that a comparison is either always
9634 true or always false. However, we do not perform general constant
9635 folding in combine, so this knowledge isn't useful. Such tautologies
9636 should have been detected earlier. Hence we ignore all such cases. */
9638 static enum rtx_code
9639 simplify_comparison (code, pop0, pop1)
9640 enum rtx_code code;
9641 rtx *pop0;
9642 rtx *pop1;
9644 rtx op0 = *pop0;
9645 rtx op1 = *pop1;
9646 rtx tem, tem1;
9647 int i;
9648 enum machine_mode mode, tmode;
9650 /* Try a few ways of applying the same transformation to both operands. */
9651 while (1)
9653 #ifndef WORD_REGISTER_OPERATIONS
9654 /* The test below this one won't handle SIGN_EXTENDs on these machines,
9655 so check specially. */
9656 if (code != GTU && code != GEU && code != LTU && code != LEU
9657 && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
9658 && GET_CODE (XEXP (op0, 0)) == ASHIFT
9659 && GET_CODE (XEXP (op1, 0)) == ASHIFT
9660 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
9661 && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
9662 && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
9663 == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
9664 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9665 && GET_CODE (XEXP (op1, 1)) == CONST_INT
9666 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
9667 && GET_CODE (XEXP (XEXP (op1, 0), 1)) == CONST_INT
9668 && INTVAL (XEXP (op0, 1)) == INTVAL (XEXP (op1, 1))
9669 && INTVAL (XEXP (op0, 1)) == INTVAL (XEXP (XEXP (op0, 0), 1))
9670 && INTVAL (XEXP (op0, 1)) == INTVAL (XEXP (XEXP (op1, 0), 1))
9671 && (INTVAL (XEXP (op0, 1))
9672 == (GET_MODE_BITSIZE (GET_MODE (op0))
9673 - (GET_MODE_BITSIZE
9674 (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
9676 op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
9677 op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
9679 #endif
9681 /* If both operands are the same constant shift, see if we can ignore the
9682 shift. We can if the shift is a rotate or if the bits shifted out of
9683 this shift are known to be zero for both inputs and if the type of
9684 comparison is compatible with the shift. */
9685 if (GET_CODE (op0) == GET_CODE (op1)
9686 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
9687 && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
9688 || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
9689 && (code != GT && code != LT && code != GE && code != LE))
9690 || (GET_CODE (op0) == ASHIFTRT
9691 && (code != GTU && code != LTU
9692 && code != GEU && code != GEU)))
9693 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9694 && INTVAL (XEXP (op0, 1)) >= 0
9695 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
9696 && XEXP (op0, 1) == XEXP (op1, 1))
9698 enum machine_mode mode = GET_MODE (op0);
9699 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
9700 int shift_count = INTVAL (XEXP (op0, 1));
9702 if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
9703 mask &= (mask >> shift_count) << shift_count;
9704 else if (GET_CODE (op0) == ASHIFT)
9705 mask = (mask & (mask << shift_count)) >> shift_count;
9707 if ((nonzero_bits (XEXP (op0, 0), mode) & ~ mask) == 0
9708 && (nonzero_bits (XEXP (op1, 0), mode) & ~ mask) == 0)
9709 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
9710 else
9711 break;
9714 /* If both operands are AND's of a paradoxical SUBREG by constant, the
9715 SUBREGs are of the same mode, and, in both cases, the AND would
9716 be redundant if the comparison was done in the narrower mode,
9717 do the comparison in the narrower mode (e.g., we are AND'ing with 1
9718 and the operand's possibly nonzero bits are 0xffffff01; in that case
9719 if we only care about QImode, we don't need the AND). This case
9720 occurs if the output mode of an scc insn is not SImode and
9721 STORE_FLAG_VALUE == 1 (e.g., the 386).
9723 Similarly, check for a case where the AND's are ZERO_EXTEND
9724 operations from some narrower mode even though a SUBREG is not
9725 present. */
9727 else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
9728 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9729 && GET_CODE (XEXP (op1, 1)) == CONST_INT)
9731 rtx inner_op0 = XEXP (op0, 0);
9732 rtx inner_op1 = XEXP (op1, 0);
9733 HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
9734 HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
9735 int changed = 0;
9737 if (GET_CODE (inner_op0) == SUBREG && GET_CODE (inner_op1) == SUBREG
9738 && (GET_MODE_SIZE (GET_MODE (inner_op0))
9739 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (inner_op0))))
9740 && (GET_MODE (SUBREG_REG (inner_op0))
9741 == GET_MODE (SUBREG_REG (inner_op1)))
9742 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (inner_op0)))
9743 <= HOST_BITS_PER_WIDE_INT)
9744 && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
9745 GET_MODE (SUBREG_REG (inner_op0)))))
9746 && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
9747 GET_MODE (SUBREG_REG (inner_op1))))))
9749 op0 = SUBREG_REG (inner_op0);
9750 op1 = SUBREG_REG (inner_op1);
9752 /* The resulting comparison is always unsigned since we masked
9753 off the original sign bit. */
9754 code = unsigned_condition (code);
9756 changed = 1;
9759 else if (c0 == c1)
9760 for (tmode = GET_CLASS_NARROWEST_MODE
9761 (GET_MODE_CLASS (GET_MODE (op0)));
9762 tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
9763 if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
9765 op0 = gen_lowpart_for_combine (tmode, inner_op0);
9766 op1 = gen_lowpart_for_combine (tmode, inner_op1);
9767 code = unsigned_condition (code);
9768 changed = 1;
9769 break;
9772 if (! changed)
9773 break;
9776 /* If both operands are NOT, we can strip off the outer operation
9777 and adjust the comparison code for swapped operands; similarly for
9778 NEG, except that this must be an equality comparison. */
9779 else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
9780 || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
9781 && (code == EQ || code == NE)))
9782 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
9784 else
9785 break;
9788 /* If the first operand is a constant, swap the operands and adjust the
9789 comparison code appropriately, but don't do this if the second operand
9790 is already a constant integer. */
9791 if (CONSTANT_P (op0) && GET_CODE (op1) != CONST_INT)
9793 tem = op0, op0 = op1, op1 = tem;
9794 code = swap_condition (code);
9797 /* We now enter a loop during which we will try to simplify the comparison.
9798 For the most part, we only are concerned with comparisons with zero,
9799 but some things may really be comparisons with zero but not start
9800 out looking that way. */
9802 while (GET_CODE (op1) == CONST_INT)
9804 enum machine_mode mode = GET_MODE (op0);
9805 int mode_width = GET_MODE_BITSIZE (mode);
9806 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
9807 int equality_comparison_p;
9808 int sign_bit_comparison_p;
9809 int unsigned_comparison_p;
9810 HOST_WIDE_INT const_op;
9812 /* We only want to handle integral modes. This catches VOIDmode,
9813 CCmode, and the floating-point modes. An exception is that we
9814 can handle VOIDmode if OP0 is a COMPARE or a comparison
9815 operation. */
9817 if (GET_MODE_CLASS (mode) != MODE_INT
9818 && ! (mode == VOIDmode
9819 && (GET_CODE (op0) == COMPARE
9820 || GET_RTX_CLASS (GET_CODE (op0)) == '<')))
9821 break;
9823 /* Get the constant we are comparing against and turn off all bits
9824 not on in our mode. */
9825 const_op = INTVAL (op1);
9826 if (mode_width <= HOST_BITS_PER_WIDE_INT)
9827 const_op &= mask;
9829 /* If we are comparing against a constant power of two and the value
9830 being compared can only have that single bit nonzero (e.g., it was
9831 `and'ed with that bit), we can replace this with a comparison
9832 with zero. */
9833 if (const_op
9834 && (code == EQ || code == NE || code == GE || code == GEU
9835 || code == LT || code == LTU)
9836 && mode_width <= HOST_BITS_PER_WIDE_INT
9837 && exact_log2 (const_op) >= 0
9838 && nonzero_bits (op0, mode) == (unsigned HOST_WIDE_INT) const_op)
9840 code = (code == EQ || code == GE || code == GEU ? NE : EQ);
9841 op1 = const0_rtx, const_op = 0;
9844 /* Similarly, if we are comparing a value known to be either -1 or
9845 0 with -1, change it to the opposite comparison against zero. */
9847 if (const_op == -1
9848 && (code == EQ || code == NE || code == GT || code == LE
9849 || code == GEU || code == LTU)
9850 && num_sign_bit_copies (op0, mode) == mode_width)
9852 code = (code == EQ || code == LE || code == GEU ? NE : EQ);
9853 op1 = const0_rtx, const_op = 0;
9856 /* Do some canonicalizations based on the comparison code. We prefer
9857 comparisons against zero and then prefer equality comparisons.
9858 If we can reduce the size of a constant, we will do that too. */
9860 switch (code)
9862 case LT:
9863 /* < C is equivalent to <= (C - 1) */
9864 if (const_op > 0)
9866 const_op -= 1;
9867 op1 = GEN_INT (const_op);
9868 code = LE;
9869 /* ... fall through to LE case below. */
9871 else
9872 break;
9874 case LE:
9875 /* <= C is equivalent to < (C + 1); we do this for C < 0 */
9876 if (const_op < 0)
9878 const_op += 1;
9879 op1 = GEN_INT (const_op);
9880 code = LT;
9883 /* If we are doing a <= 0 comparison on a value known to have
9884 a zero sign bit, we can replace this with == 0. */
9885 else if (const_op == 0
9886 && mode_width <= HOST_BITS_PER_WIDE_INT
9887 && (nonzero_bits (op0, mode)
9888 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
9889 code = EQ;
9890 break;
9892 case GE:
9893 /* >= C is equivalent to > (C - 1). */
9894 if (const_op > 0)
9896 const_op -= 1;
9897 op1 = GEN_INT (const_op);
9898 code = GT;
9899 /* ... fall through to GT below. */
9901 else
9902 break;
9904 case GT:
9905 /* > C is equivalent to >= (C + 1); we do this for C < 0*/
9906 if (const_op < 0)
9908 const_op += 1;
9909 op1 = GEN_INT (const_op);
9910 code = GE;
9913 /* If we are doing a > 0 comparison on a value known to have
9914 a zero sign bit, we can replace this with != 0. */
9915 else if (const_op == 0
9916 && mode_width <= HOST_BITS_PER_WIDE_INT
9917 && (nonzero_bits (op0, mode)
9918 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
9919 code = NE;
9920 break;
9922 case LTU:
9923 /* < C is equivalent to <= (C - 1). */
9924 if (const_op > 0)
9926 const_op -= 1;
9927 op1 = GEN_INT (const_op);
9928 code = LEU;
9929 /* ... fall through ... */
9932 /* (unsigned) < 0x80000000 is equivalent to >= 0. */
9933 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9934 && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
9936 const_op = 0, op1 = const0_rtx;
9937 code = GE;
9938 break;
9940 else
9941 break;
9943 case LEU:
9944 /* unsigned <= 0 is equivalent to == 0 */
9945 if (const_op == 0)
9946 code = EQ;
9948 /* (unsigned) <= 0x7fffffff is equivalent to >= 0. */
9949 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9950 && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
9952 const_op = 0, op1 = const0_rtx;
9953 code = GE;
9955 break;
9957 case GEU:
9958 /* >= C is equivalent to < (C - 1). */
9959 if (const_op > 1)
9961 const_op -= 1;
9962 op1 = GEN_INT (const_op);
9963 code = GTU;
9964 /* ... fall through ... */
9967 /* (unsigned) >= 0x80000000 is equivalent to < 0. */
9968 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9969 && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
9971 const_op = 0, op1 = const0_rtx;
9972 code = LT;
9973 break;
9975 else
9976 break;
9978 case GTU:
9979 /* unsigned > 0 is equivalent to != 0 */
9980 if (const_op == 0)
9981 code = NE;
9983 /* (unsigned) > 0x7fffffff is equivalent to < 0. */
9984 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9985 && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
9987 const_op = 0, op1 = const0_rtx;
9988 code = LT;
9990 break;
9992 default:
9993 break;
9996 /* Compute some predicates to simplify code below. */
9998 equality_comparison_p = (code == EQ || code == NE);
9999 sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
10000 unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
10001 || code == LEU);
10003 /* If this is a sign bit comparison and we can do arithmetic in
10004 MODE, say that we will only be needing the sign bit of OP0. */
10005 if (sign_bit_comparison_p
10006 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10007 op0 = force_to_mode (op0, mode,
10008 ((HOST_WIDE_INT) 1
10009 << (GET_MODE_BITSIZE (mode) - 1)),
10010 NULL_RTX, 0);
10012 /* Now try cases based on the opcode of OP0. If none of the cases
10013 does a "continue", we exit this loop immediately after the
10014 switch. */
10016 switch (GET_CODE (op0))
10018 case ZERO_EXTRACT:
10019 /* If we are extracting a single bit from a variable position in
10020 a constant that has only a single bit set and are comparing it
10021 with zero, we can convert this into an equality comparison
10022 between the position and the location of the single bit. */
10024 if (GET_CODE (XEXP (op0, 0)) == CONST_INT
10025 && XEXP (op0, 1) == const1_rtx
10026 && equality_comparison_p && const_op == 0
10027 && (i = exact_log2 (INTVAL (XEXP (op0, 0)))) >= 0)
10029 if (BITS_BIG_ENDIAN)
10031 #ifdef HAVE_extzv
10032 mode = insn_data[(int) CODE_FOR_extzv].operand[1].mode;
10033 if (mode == VOIDmode)
10034 mode = word_mode;
10035 i = (GET_MODE_BITSIZE (mode) - 1 - i);
10036 #else
10037 i = BITS_PER_WORD - 1 - i;
10038 #endif
10041 op0 = XEXP (op0, 2);
10042 op1 = GEN_INT (i);
10043 const_op = i;
10045 /* Result is nonzero iff shift count is equal to I. */
10046 code = reverse_condition (code);
10047 continue;
10050 /* ... fall through ... */
10052 case SIGN_EXTRACT:
10053 tem = expand_compound_operation (op0);
10054 if (tem != op0)
10056 op0 = tem;
10057 continue;
10059 break;
10061 case NOT:
10062 /* If testing for equality, we can take the NOT of the constant. */
10063 if (equality_comparison_p
10064 && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
10066 op0 = XEXP (op0, 0);
10067 op1 = tem;
10068 continue;
10071 /* If just looking at the sign bit, reverse the sense of the
10072 comparison. */
10073 if (sign_bit_comparison_p)
10075 op0 = XEXP (op0, 0);
10076 code = (code == GE ? LT : GE);
10077 continue;
10079 break;
10081 case NEG:
10082 /* If testing for equality, we can take the NEG of the constant. */
10083 if (equality_comparison_p
10084 && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
10086 op0 = XEXP (op0, 0);
10087 op1 = tem;
10088 continue;
10091 /* The remaining cases only apply to comparisons with zero. */
10092 if (const_op != 0)
10093 break;
10095 /* When X is ABS or is known positive,
10096 (neg X) is < 0 if and only if X != 0. */
10098 if (sign_bit_comparison_p
10099 && (GET_CODE (XEXP (op0, 0)) == ABS
10100 || (mode_width <= HOST_BITS_PER_WIDE_INT
10101 && (nonzero_bits (XEXP (op0, 0), mode)
10102 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)))
10104 op0 = XEXP (op0, 0);
10105 code = (code == LT ? NE : EQ);
10106 continue;
10109 /* If we have NEG of something whose two high-order bits are the
10110 same, we know that "(-a) < 0" is equivalent to "a > 0". */
10111 if (num_sign_bit_copies (op0, mode) >= 2)
10113 op0 = XEXP (op0, 0);
10114 code = swap_condition (code);
10115 continue;
10117 break;
10119 case ROTATE:
10120 /* If we are testing equality and our count is a constant, we
10121 can perform the inverse operation on our RHS. */
10122 if (equality_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
10123 && (tem = simplify_binary_operation (ROTATERT, mode,
10124 op1, XEXP (op0, 1))) != 0)
10126 op0 = XEXP (op0, 0);
10127 op1 = tem;
10128 continue;
10131 /* If we are doing a < 0 or >= 0 comparison, it means we are testing
10132 a particular bit. Convert it to an AND of a constant of that
10133 bit. This will be converted into a ZERO_EXTRACT. */
10134 if (const_op == 0 && sign_bit_comparison_p
10135 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10136 && mode_width <= HOST_BITS_PER_WIDE_INT)
10138 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10139 ((HOST_WIDE_INT) 1
10140 << (mode_width - 1
10141 - INTVAL (XEXP (op0, 1)))));
10142 code = (code == LT ? NE : EQ);
10143 continue;
10146 /* ... fall through ... */
10148 case ABS:
10149 /* ABS is ignorable inside an equality comparison with zero. */
10150 if (const_op == 0 && equality_comparison_p)
10152 op0 = XEXP (op0, 0);
10153 continue;
10155 break;
10158 case SIGN_EXTEND:
10159 /* Can simplify (compare (zero/sign_extend FOO) CONST)
10160 to (compare FOO CONST) if CONST fits in FOO's mode and we
10161 are either testing inequality or have an unsigned comparison
10162 with ZERO_EXTEND or a signed comparison with SIGN_EXTEND. */
10163 if (! unsigned_comparison_p
10164 && (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10165 <= HOST_BITS_PER_WIDE_INT)
10166 && ((unsigned HOST_WIDE_INT) const_op
10167 < (((unsigned HOST_WIDE_INT) 1
10168 << (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))) - 1)))))
10170 op0 = XEXP (op0, 0);
10171 continue;
10173 break;
10175 case SUBREG:
10176 /* Check for the case where we are comparing A - C1 with C2,
10177 both constants are smaller than 1/2 the maximum positive
10178 value in MODE, and the comparison is equality or unsigned.
10179 In that case, if A is either zero-extended to MODE or has
10180 sufficient sign bits so that the high-order bit in MODE
10181 is a copy of the sign in the inner mode, we can prove that it is
10182 safe to do the operation in the wider mode. This simplifies
10183 many range checks. */
10185 if (mode_width <= HOST_BITS_PER_WIDE_INT
10186 && subreg_lowpart_p (op0)
10187 && GET_CODE (SUBREG_REG (op0)) == PLUS
10188 && GET_CODE (XEXP (SUBREG_REG (op0), 1)) == CONST_INT
10189 && INTVAL (XEXP (SUBREG_REG (op0), 1)) < 0
10190 && (- INTVAL (XEXP (SUBREG_REG (op0), 1))
10191 < (HOST_WIDE_INT)(GET_MODE_MASK (mode) / 2))
10192 && (unsigned HOST_WIDE_INT) const_op < GET_MODE_MASK (mode) / 2
10193 && (0 == (nonzero_bits (XEXP (SUBREG_REG (op0), 0),
10194 GET_MODE (SUBREG_REG (op0)))
10195 & ~ GET_MODE_MASK (mode))
10196 || (num_sign_bit_copies (XEXP (SUBREG_REG (op0), 0),
10197 GET_MODE (SUBREG_REG (op0)))
10198 > (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
10199 - GET_MODE_BITSIZE (mode)))))
10201 op0 = SUBREG_REG (op0);
10202 continue;
10205 /* If the inner mode is narrower and we are extracting the low part,
10206 we can treat the SUBREG as if it were a ZERO_EXTEND. */
10207 if (subreg_lowpart_p (op0)
10208 && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) < mode_width)
10209 /* Fall through */ ;
10210 else
10211 break;
10213 /* ... fall through ... */
10215 case ZERO_EXTEND:
10216 if ((unsigned_comparison_p || equality_comparison_p)
10217 && (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10218 <= HOST_BITS_PER_WIDE_INT)
10219 && ((unsigned HOST_WIDE_INT) const_op
10220 < GET_MODE_MASK (GET_MODE (XEXP (op0, 0)))))
10222 op0 = XEXP (op0, 0);
10223 continue;
10225 break;
10227 case PLUS:
10228 /* (eq (plus X A) B) -> (eq X (minus B A)). We can only do
10229 this for equality comparisons due to pathological cases involving
10230 overflows. */
10231 if (equality_comparison_p
10232 && 0 != (tem = simplify_binary_operation (MINUS, mode,
10233 op1, XEXP (op0, 1))))
10235 op0 = XEXP (op0, 0);
10236 op1 = tem;
10237 continue;
10240 /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0. */
10241 if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
10242 && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
10244 op0 = XEXP (XEXP (op0, 0), 0);
10245 code = (code == LT ? EQ : NE);
10246 continue;
10248 break;
10250 case MINUS:
10251 /* (eq (minus A B) C) -> (eq A (plus B C)) or
10252 (eq B (minus A C)), whichever simplifies. We can only do
10253 this for equality comparisons due to pathological cases involving
10254 overflows. */
10255 if (equality_comparison_p
10256 && 0 != (tem = simplify_binary_operation (PLUS, mode,
10257 XEXP (op0, 1), op1)))
10259 op0 = XEXP (op0, 0);
10260 op1 = tem;
10261 continue;
10264 if (equality_comparison_p
10265 && 0 != (tem = simplify_binary_operation (MINUS, mode,
10266 XEXP (op0, 0), op1)))
10268 op0 = XEXP (op0, 1);
10269 op1 = tem;
10270 continue;
10273 /* The sign bit of (minus (ashiftrt X C) X), where C is the number
10274 of bits in X minus 1, is one iff X > 0. */
10275 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
10276 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10277 && INTVAL (XEXP (XEXP (op0, 0), 1)) == mode_width - 1
10278 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10280 op0 = XEXP (op0, 1);
10281 code = (code == GE ? LE : GT);
10282 continue;
10284 break;
10286 case XOR:
10287 /* (eq (xor A B) C) -> (eq A (xor B C)). This is a simplification
10288 if C is zero or B is a constant. */
10289 if (equality_comparison_p
10290 && 0 != (tem = simplify_binary_operation (XOR, mode,
10291 XEXP (op0, 1), op1)))
10293 op0 = XEXP (op0, 0);
10294 op1 = tem;
10295 continue;
10297 break;
10299 case EQ: case NE:
10300 case LT: case LTU: case LE: case LEU:
10301 case GT: case GTU: case GE: case GEU:
10302 /* We can't do anything if OP0 is a condition code value, rather
10303 than an actual data value. */
10304 if (const_op != 0
10305 #ifdef HAVE_cc0
10306 || XEXP (op0, 0) == cc0_rtx
10307 #endif
10308 || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
10309 break;
10311 /* Get the two operands being compared. */
10312 if (GET_CODE (XEXP (op0, 0)) == COMPARE)
10313 tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
10314 else
10315 tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
10317 /* Check for the cases where we simply want the result of the
10318 earlier test or the opposite of that result. */
10319 if (code == NE
10320 || (code == EQ && reversible_comparison_p (op0))
10321 || (GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
10322 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10323 && (STORE_FLAG_VALUE
10324 & (((HOST_WIDE_INT) 1
10325 << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
10326 && (code == LT
10327 || (code == GE && reversible_comparison_p (op0)))))
10329 code = (code == LT || code == NE
10330 ? GET_CODE (op0) : reverse_condition (GET_CODE (op0)));
10331 op0 = tem, op1 = tem1;
10332 continue;
10334 break;
10336 case IOR:
10337 /* The sign bit of (ior (plus X (const_int -1)) X) is non-zero
10338 iff X <= 0. */
10339 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
10340 && XEXP (XEXP (op0, 0), 1) == constm1_rtx
10341 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10343 op0 = XEXP (op0, 1);
10344 code = (code == GE ? GT : LE);
10345 continue;
10347 break;
10349 case AND:
10350 /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1). This
10351 will be converted to a ZERO_EXTRACT later. */
10352 if (const_op == 0 && equality_comparison_p
10353 && GET_CODE (XEXP (op0, 0)) == ASHIFT
10354 && XEXP (XEXP (op0, 0), 0) == const1_rtx)
10356 op0 = simplify_and_const_int
10357 (op0, mode, gen_rtx_combine (LSHIFTRT, mode,
10358 XEXP (op0, 1),
10359 XEXP (XEXP (op0, 0), 1)),
10360 (HOST_WIDE_INT) 1);
10361 continue;
10364 /* If we are comparing (and (lshiftrt X C1) C2) for equality with
10365 zero and X is a comparison and C1 and C2 describe only bits set
10366 in STORE_FLAG_VALUE, we can compare with X. */
10367 if (const_op == 0 && equality_comparison_p
10368 && mode_width <= HOST_BITS_PER_WIDE_INT
10369 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10370 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
10371 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10372 && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
10373 && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
10375 mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
10376 << INTVAL (XEXP (XEXP (op0, 0), 1)));
10377 if ((~ STORE_FLAG_VALUE & mask) == 0
10378 && (GET_RTX_CLASS (GET_CODE (XEXP (XEXP (op0, 0), 0))) == '<'
10379 || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
10380 && GET_RTX_CLASS (GET_CODE (tem)) == '<')))
10382 op0 = XEXP (XEXP (op0, 0), 0);
10383 continue;
10387 /* If we are doing an equality comparison of an AND of a bit equal
10388 to the sign bit, replace this with a LT or GE comparison of
10389 the underlying value. */
10390 if (equality_comparison_p
10391 && const_op == 0
10392 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10393 && mode_width <= HOST_BITS_PER_WIDE_INT
10394 && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
10395 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
10397 op0 = XEXP (op0, 0);
10398 code = (code == EQ ? GE : LT);
10399 continue;
10402 /* If this AND operation is really a ZERO_EXTEND from a narrower
10403 mode, the constant fits within that mode, and this is either an
10404 equality or unsigned comparison, try to do this comparison in
10405 the narrower mode. */
10406 if ((equality_comparison_p || unsigned_comparison_p)
10407 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10408 && (i = exact_log2 ((INTVAL (XEXP (op0, 1))
10409 & GET_MODE_MASK (mode))
10410 + 1)) >= 0
10411 && const_op >> i == 0
10412 && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode)
10414 op0 = gen_lowpart_for_combine (tmode, XEXP (op0, 0));
10415 continue;
10418 /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1 fits
10419 in both M1 and M2 and the SUBREG is either paradoxical or
10420 represents the low part, permute the SUBREG and the AND and
10421 try again. */
10422 if (GET_CODE (XEXP (op0, 0)) == SUBREG
10423 && (0
10424 #ifdef WORD_REGISTER_OPERATIONS
10425 || ((mode_width
10426 > (GET_MODE_BITSIZE
10427 (GET_MODE (SUBREG_REG (XEXP (op0, 0))))))
10428 && mode_width <= BITS_PER_WORD)
10429 #endif
10430 || ((mode_width
10431 <= (GET_MODE_BITSIZE
10432 (GET_MODE (SUBREG_REG (XEXP (op0, 0))))))
10433 && subreg_lowpart_p (XEXP (op0, 0))))
10434 #ifndef WORD_REGISTER_OPERATIONS
10435 /* It is unsafe to commute the AND into the SUBREG if the SUBREG
10436 is paradoxical and WORD_REGISTER_OPERATIONS is not defined.
10437 As originally written the upper bits have a defined value
10438 due to the AND operation. However, if we commute the AND
10439 inside the SUBREG then they no longer have defined values
10440 and the meaning of the code has been changed. */
10441 && (GET_MODE_SIZE (GET_MODE (XEXP (op0, 0)))
10442 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (op0, 0)))))
10443 #endif
10444 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10445 && mode_width <= HOST_BITS_PER_WIDE_INT
10446 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (XEXP (op0, 0))))
10447 <= HOST_BITS_PER_WIDE_INT)
10448 && (INTVAL (XEXP (op0, 1)) & ~ mask) == 0
10449 && 0 == (~ GET_MODE_MASK (GET_MODE (SUBREG_REG (XEXP (op0, 0))))
10450 & INTVAL (XEXP (op0, 1)))
10451 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1)) != mask
10452 && ((unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1))
10453 != GET_MODE_MASK (GET_MODE (SUBREG_REG (XEXP (op0, 0))))))
10457 = gen_lowpart_for_combine
10458 (mode,
10459 gen_binary (AND, GET_MODE (SUBREG_REG (XEXP (op0, 0))),
10460 SUBREG_REG (XEXP (op0, 0)), XEXP (op0, 1)));
10461 continue;
10464 break;
10466 case ASHIFT:
10467 /* If we have (compare (ashift FOO N) (const_int C)) and
10468 the high order N bits of FOO (N+1 if an inequality comparison)
10469 are known to be zero, we can do this by comparing FOO with C
10470 shifted right N bits so long as the low-order N bits of C are
10471 zero. */
10472 if (GET_CODE (XEXP (op0, 1)) == CONST_INT
10473 && INTVAL (XEXP (op0, 1)) >= 0
10474 && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
10475 < HOST_BITS_PER_WIDE_INT)
10476 && ((const_op
10477 & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0)
10478 && mode_width <= HOST_BITS_PER_WIDE_INT
10479 && (nonzero_bits (XEXP (op0, 0), mode)
10480 & ~ (mask >> (INTVAL (XEXP (op0, 1))
10481 + ! equality_comparison_p))) == 0)
10483 /* We must perform a logical shift, not an arithmetic one,
10484 as we want the top N bits of C to be zero. */
10485 unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
10487 temp >>= INTVAL (XEXP (op0, 1));
10488 op1 = GEN_INT (trunc_int_for_mode (temp, mode));
10489 op0 = XEXP (op0, 0);
10490 continue;
10493 /* If we are doing a sign bit comparison, it means we are testing
10494 a particular bit. Convert it to the appropriate AND. */
10495 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
10496 && mode_width <= HOST_BITS_PER_WIDE_INT)
10498 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10499 ((HOST_WIDE_INT) 1
10500 << (mode_width - 1
10501 - INTVAL (XEXP (op0, 1)))));
10502 code = (code == LT ? NE : EQ);
10503 continue;
10506 /* If this an equality comparison with zero and we are shifting
10507 the low bit to the sign bit, we can convert this to an AND of the
10508 low-order bit. */
10509 if (const_op == 0 && equality_comparison_p
10510 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10511 && INTVAL (XEXP (op0, 1)) == mode_width - 1)
10513 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10514 (HOST_WIDE_INT) 1);
10515 continue;
10517 break;
10519 case ASHIFTRT:
10520 /* If this is an equality comparison with zero, we can do this
10521 as a logical shift, which might be much simpler. */
10522 if (equality_comparison_p && const_op == 0
10523 && GET_CODE (XEXP (op0, 1)) == CONST_INT)
10525 op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
10526 XEXP (op0, 0),
10527 INTVAL (XEXP (op0, 1)));
10528 continue;
10531 /* If OP0 is a sign extension and CODE is not an unsigned comparison,
10532 do the comparison in a narrower mode. */
10533 if (! unsigned_comparison_p
10534 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10535 && GET_CODE (XEXP (op0, 0)) == ASHIFT
10536 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
10537 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
10538 MODE_INT, 1)) != BLKmode
10539 && ((unsigned HOST_WIDE_INT) const_op <= GET_MODE_MASK (tmode)
10540 || ((unsigned HOST_WIDE_INT) - const_op
10541 <= GET_MODE_MASK (tmode))))
10543 op0 = gen_lowpart_for_combine (tmode, XEXP (XEXP (op0, 0), 0));
10544 continue;
10547 /* Likewise if OP0 is a PLUS of a sign extension with a
10548 constant, which is usually represented with the PLUS
10549 between the shifts. */
10550 if (! unsigned_comparison_p
10551 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10552 && GET_CODE (XEXP (op0, 0)) == PLUS
10553 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10554 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
10555 && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
10556 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
10557 MODE_INT, 1)) != BLKmode
10558 && ((unsigned HOST_WIDE_INT) const_op <= GET_MODE_MASK (tmode)
10559 || ((unsigned HOST_WIDE_INT) - const_op
10560 <= GET_MODE_MASK (tmode))))
10562 rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
10563 rtx add_const = XEXP (XEXP (op0, 0), 1);
10564 rtx new_const = gen_binary (ASHIFTRT, GET_MODE (op0), add_const,
10565 XEXP (op0, 1));
10567 op0 = gen_binary (PLUS, tmode,
10568 gen_lowpart_for_combine (tmode, inner),
10569 new_const);
10570 continue;
10573 /* ... fall through ... */
10574 case LSHIFTRT:
10575 /* If we have (compare (xshiftrt FOO N) (const_int C)) and
10576 the low order N bits of FOO are known to be zero, we can do this
10577 by comparing FOO with C shifted left N bits so long as no
10578 overflow occurs. */
10579 if (GET_CODE (XEXP (op0, 1)) == CONST_INT
10580 && INTVAL (XEXP (op0, 1)) >= 0
10581 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
10582 && mode_width <= HOST_BITS_PER_WIDE_INT
10583 && (nonzero_bits (XEXP (op0, 0), mode)
10584 & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0
10585 && (const_op == 0
10586 || (floor_log2 (const_op) + INTVAL (XEXP (op0, 1))
10587 < mode_width)))
10589 const_op <<= INTVAL (XEXP (op0, 1));
10590 op1 = GEN_INT (const_op);
10591 op0 = XEXP (op0, 0);
10592 continue;
10595 /* If we are using this shift to extract just the sign bit, we
10596 can replace this with an LT or GE comparison. */
10597 if (const_op == 0
10598 && (equality_comparison_p || sign_bit_comparison_p)
10599 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10600 && INTVAL (XEXP (op0, 1)) == mode_width - 1)
10602 op0 = XEXP (op0, 0);
10603 code = (code == NE || code == GT ? LT : GE);
10604 continue;
10606 break;
10608 default:
10609 break;
10612 break;
10615 /* Now make any compound operations involved in this comparison. Then,
10616 check for an outmost SUBREG on OP0 that is not doing anything or is
10617 paradoxical. The latter case can only occur when it is known that the
10618 "extra" bits will be zero. Therefore, it is safe to remove the SUBREG.
10619 We can never remove a SUBREG for a non-equality comparison because the
10620 sign bit is in a different place in the underlying object. */
10622 op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
10623 op1 = make_compound_operation (op1, SET);
10625 if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
10626 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10627 && (code == NE || code == EQ)
10628 && ((GET_MODE_SIZE (GET_MODE (op0))
10629 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0))))))
10631 op0 = SUBREG_REG (op0);
10632 op1 = gen_lowpart_for_combine (GET_MODE (op0), op1);
10635 else if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
10636 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10637 && (code == NE || code == EQ)
10638 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
10639 <= HOST_BITS_PER_WIDE_INT)
10640 && (nonzero_bits (SUBREG_REG (op0), GET_MODE (SUBREG_REG (op0)))
10641 & ~ GET_MODE_MASK (GET_MODE (op0))) == 0
10642 && (tem = gen_lowpart_for_combine (GET_MODE (SUBREG_REG (op0)),
10643 op1),
10644 (nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
10645 & ~ GET_MODE_MASK (GET_MODE (op0))) == 0))
10646 op0 = SUBREG_REG (op0), op1 = tem;
10648 /* We now do the opposite procedure: Some machines don't have compare
10649 insns in all modes. If OP0's mode is an integer mode smaller than a
10650 word and we can't do a compare in that mode, see if there is a larger
10651 mode for which we can do the compare. There are a number of cases in
10652 which we can use the wider mode. */
10654 mode = GET_MODE (op0);
10655 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
10656 && GET_MODE_SIZE (mode) < UNITS_PER_WORD
10657 && cmp_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
10658 for (tmode = GET_MODE_WIDER_MODE (mode);
10659 (tmode != VOIDmode
10660 && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT);
10661 tmode = GET_MODE_WIDER_MODE (tmode))
10662 if (cmp_optab->handlers[(int) tmode].insn_code != CODE_FOR_nothing)
10664 /* If the only nonzero bits in OP0 and OP1 are those in the
10665 narrower mode and this is an equality or unsigned comparison,
10666 we can use the wider mode. Similarly for sign-extended
10667 values, in which case it is true for all comparisons. */
10668 if (((code == EQ || code == NE
10669 || code == GEU || code == GTU || code == LEU || code == LTU)
10670 && (nonzero_bits (op0, tmode) & ~ GET_MODE_MASK (mode)) == 0
10671 && (nonzero_bits (op1, tmode) & ~ GET_MODE_MASK (mode)) == 0)
10672 || ((num_sign_bit_copies (op0, tmode)
10673 > GET_MODE_BITSIZE (tmode) - GET_MODE_BITSIZE (mode))
10674 && (num_sign_bit_copies (op1, tmode)
10675 > GET_MODE_BITSIZE (tmode) - GET_MODE_BITSIZE (mode))))
10677 /* If OP0 is an AND and we don't have an AND in MODE either,
10678 make a new AND in the proper mode. */
10679 if (GET_CODE (op0) == AND
10680 && (add_optab->handlers[(int) mode].insn_code
10681 == CODE_FOR_nothing))
10682 op0 = gen_binary (AND, tmode,
10683 gen_lowpart_for_combine (tmode,
10684 XEXP (op0, 0)),
10685 gen_lowpart_for_combine (tmode,
10686 XEXP (op0, 1)));
10688 op0 = gen_lowpart_for_combine (tmode, op0);
10689 op1 = gen_lowpart_for_combine (tmode, op1);
10690 break;
10693 /* If this is a test for negative, we can make an explicit
10694 test of the sign bit. */
10696 if (op1 == const0_rtx && (code == LT || code == GE)
10697 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10699 op0 = gen_binary (AND, tmode,
10700 gen_lowpart_for_combine (tmode, op0),
10701 GEN_INT ((HOST_WIDE_INT) 1
10702 << (GET_MODE_BITSIZE (mode) - 1)));
10703 code = (code == LT) ? NE : EQ;
10704 break;
10708 #ifdef CANONICALIZE_COMPARISON
10709 /* If this machine only supports a subset of valid comparisons, see if we
10710 can convert an unsupported one into a supported one. */
10711 CANONICALIZE_COMPARISON (code, op0, op1);
10712 #endif
10714 *pop0 = op0;
10715 *pop1 = op1;
10717 return code;
10720 /* Return 1 if we know that X, a comparison operation, is not operating
10721 on a floating-point value or is EQ or NE, meaning that we can safely
10722 reverse it. */
10724 static int
10725 reversible_comparison_p (x)
10726 rtx x;
10728 if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
10729 || flag_fast_math
10730 || GET_CODE (x) == NE || GET_CODE (x) == EQ)
10731 return 1;
10733 switch (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))))
10735 case MODE_INT:
10736 case MODE_PARTIAL_INT:
10737 case MODE_COMPLEX_INT:
10738 return 1;
10740 case MODE_CC:
10741 /* If the mode of the condition codes tells us that this is safe,
10742 we need look no further. */
10743 if (REVERSIBLE_CC_MODE (GET_MODE (XEXP (x, 0))))
10744 return 1;
10746 /* Otherwise try and find where the condition codes were last set and
10747 use that. */
10748 x = get_last_value (XEXP (x, 0));
10749 return (x && GET_CODE (x) == COMPARE
10750 && ! FLOAT_MODE_P (GET_MODE (XEXP (x, 0))));
10752 default:
10753 return 0;
10757 /* Utility function for following routine. Called when X is part of a value
10758 being stored into reg_last_set_value. Sets reg_last_set_table_tick
10759 for each register mentioned. Similar to mention_regs in cse.c */
10761 static void
10762 update_table_tick (x)
10763 rtx x;
10765 register enum rtx_code code = GET_CODE (x);
10766 register const char *fmt = GET_RTX_FORMAT (code);
10767 register int i;
10769 if (code == REG)
10771 int regno = REGNO (x);
10772 int endregno = regno + (regno < FIRST_PSEUDO_REGISTER
10773 ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
10775 for (i = regno; i < endregno; i++)
10776 reg_last_set_table_tick[i] = label_tick;
10778 return;
10781 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
10782 /* Note that we can't have an "E" in values stored; see
10783 get_last_value_validate. */
10784 if (fmt[i] == 'e')
10785 update_table_tick (XEXP (x, i));
10788 /* Record that REG is set to VALUE in insn INSN. If VALUE is zero, we
10789 are saying that the register is clobbered and we no longer know its
10790 value. If INSN is zero, don't update reg_last_set; this is only permitted
10791 with VALUE also zero and is used to invalidate the register. */
10793 static void
10794 record_value_for_reg (reg, insn, value)
10795 rtx reg;
10796 rtx insn;
10797 rtx value;
10799 int regno = REGNO (reg);
10800 int endregno = regno + (regno < FIRST_PSEUDO_REGISTER
10801 ? HARD_REGNO_NREGS (regno, GET_MODE (reg)) : 1);
10802 int i;
10804 /* If VALUE contains REG and we have a previous value for REG, substitute
10805 the previous value. */
10806 if (value && insn && reg_overlap_mentioned_p (reg, value))
10808 rtx tem;
10810 /* Set things up so get_last_value is allowed to see anything set up to
10811 our insn. */
10812 subst_low_cuid = INSN_CUID (insn);
10813 tem = get_last_value (reg);
10815 /* If TEM is simply a binary operation with two CLOBBERs as operands,
10816 it isn't going to be useful and will take a lot of time to process,
10817 so just use the CLOBBER. */
10819 if (tem)
10821 if ((GET_RTX_CLASS (GET_CODE (tem)) == '2'
10822 || GET_RTX_CLASS (GET_CODE (tem)) == 'c')
10823 && GET_CODE (XEXP (tem, 0)) == CLOBBER
10824 && GET_CODE (XEXP (tem, 1)) == CLOBBER)
10825 tem = XEXP (tem, 0);
10827 value = replace_rtx (copy_rtx (value), reg, tem);
10831 /* For each register modified, show we don't know its value, that
10832 we don't know about its bitwise content, that its value has been
10833 updated, and that we don't know the location of the death of the
10834 register. */
10835 for (i = regno; i < endregno; i ++)
10837 if (insn)
10838 reg_last_set[i] = insn;
10839 reg_last_set_value[i] = 0;
10840 reg_last_set_mode[i] = 0;
10841 reg_last_set_nonzero_bits[i] = 0;
10842 reg_last_set_sign_bit_copies[i] = 0;
10843 reg_last_death[i] = 0;
10846 /* Mark registers that are being referenced in this value. */
10847 if (value)
10848 update_table_tick (value);
10850 /* Now update the status of each register being set.
10851 If someone is using this register in this block, set this register
10852 to invalid since we will get confused between the two lives in this
10853 basic block. This makes using this register always invalid. In cse, we
10854 scan the table to invalidate all entries using this register, but this
10855 is too much work for us. */
10857 for (i = regno; i < endregno; i++)
10859 reg_last_set_label[i] = label_tick;
10860 if (value && reg_last_set_table_tick[i] == label_tick)
10861 reg_last_set_invalid[i] = 1;
10862 else
10863 reg_last_set_invalid[i] = 0;
10866 /* The value being assigned might refer to X (like in "x++;"). In that
10867 case, we must replace it with (clobber (const_int 0)) to prevent
10868 infinite loops. */
10869 if (value && ! get_last_value_validate (&value, insn,
10870 reg_last_set_label[regno], 0))
10872 value = copy_rtx (value);
10873 if (! get_last_value_validate (&value, insn,
10874 reg_last_set_label[regno], 1))
10875 value = 0;
10878 /* For the main register being modified, update the value, the mode, the
10879 nonzero bits, and the number of sign bit copies. */
10881 reg_last_set_value[regno] = value;
10883 if (value)
10885 subst_low_cuid = INSN_CUID (insn);
10886 reg_last_set_mode[regno] = GET_MODE (reg);
10887 reg_last_set_nonzero_bits[regno] = nonzero_bits (value, GET_MODE (reg));
10888 reg_last_set_sign_bit_copies[regno]
10889 = num_sign_bit_copies (value, GET_MODE (reg));
10893 /* Called via note_stores from record_dead_and_set_regs to handle one
10894 SET or CLOBBER in an insn. DATA is the instruction in which the
10895 set is occurring. */
10897 static void
10898 record_dead_and_set_regs_1 (dest, setter, data)
10899 rtx dest, setter;
10900 void *data;
10902 rtx record_dead_insn = (rtx) data;
10904 if (GET_CODE (dest) == SUBREG)
10905 dest = SUBREG_REG (dest);
10907 if (GET_CODE (dest) == REG)
10909 /* If we are setting the whole register, we know its value. Otherwise
10910 show that we don't know the value. We can handle SUBREG in
10911 some cases. */
10912 if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
10913 record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
10914 else if (GET_CODE (setter) == SET
10915 && GET_CODE (SET_DEST (setter)) == SUBREG
10916 && SUBREG_REG (SET_DEST (setter)) == dest
10917 && GET_MODE_BITSIZE (GET_MODE (dest)) <= BITS_PER_WORD
10918 && subreg_lowpart_p (SET_DEST (setter)))
10919 record_value_for_reg (dest, record_dead_insn,
10920 gen_lowpart_for_combine (GET_MODE (dest),
10921 SET_SRC (setter)));
10922 else
10923 record_value_for_reg (dest, record_dead_insn, NULL_RTX);
10925 else if (GET_CODE (dest) == MEM
10926 /* Ignore pushes, they clobber nothing. */
10927 && ! push_operand (dest, GET_MODE (dest)))
10928 mem_last_set = INSN_CUID (record_dead_insn);
10931 /* Update the records of when each REG was most recently set or killed
10932 for the things done by INSN. This is the last thing done in processing
10933 INSN in the combiner loop.
10935 We update reg_last_set, reg_last_set_value, reg_last_set_mode,
10936 reg_last_set_nonzero_bits, reg_last_set_sign_bit_copies, reg_last_death,
10937 and also the similar information mem_last_set (which insn most recently
10938 modified memory) and last_call_cuid (which insn was the most recent
10939 subroutine call). */
10941 static void
10942 record_dead_and_set_regs (insn)
10943 rtx insn;
10945 register rtx link;
10946 int i;
10948 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
10950 if (REG_NOTE_KIND (link) == REG_DEAD
10951 && GET_CODE (XEXP (link, 0)) == REG)
10953 int regno = REGNO (XEXP (link, 0));
10954 int endregno
10955 = regno + (regno < FIRST_PSEUDO_REGISTER
10956 ? HARD_REGNO_NREGS (regno, GET_MODE (XEXP (link, 0)))
10957 : 1);
10959 for (i = regno; i < endregno; i++)
10960 reg_last_death[i] = insn;
10962 else if (REG_NOTE_KIND (link) == REG_INC)
10963 record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
10966 if (GET_CODE (insn) == CALL_INSN)
10968 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
10969 if (call_used_regs[i])
10971 reg_last_set_value[i] = 0;
10972 reg_last_set_mode[i] = 0;
10973 reg_last_set_nonzero_bits[i] = 0;
10974 reg_last_set_sign_bit_copies[i] = 0;
10975 reg_last_death[i] = 0;
10978 last_call_cuid = mem_last_set = INSN_CUID (insn);
10981 note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
10984 /* Utility routine for the following function. Verify that all the registers
10985 mentioned in *LOC are valid when *LOC was part of a value set when
10986 label_tick == TICK. Return 0 if some are not.
10988 If REPLACE is non-zero, replace the invalid reference with
10989 (clobber (const_int 0)) and return 1. This replacement is useful because
10990 we often can get useful information about the form of a value (e.g., if
10991 it was produced by a shift that always produces -1 or 0) even though
10992 we don't know exactly what registers it was produced from. */
10994 static int
10995 get_last_value_validate (loc, insn, tick, replace)
10996 rtx *loc;
10997 rtx insn;
10998 int tick;
10999 int replace;
11001 rtx x = *loc;
11002 const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
11003 int len = GET_RTX_LENGTH (GET_CODE (x));
11004 int i;
11006 if (GET_CODE (x) == REG)
11008 int regno = REGNO (x);
11009 int endregno = regno + (regno < FIRST_PSEUDO_REGISTER
11010 ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
11011 int j;
11013 for (j = regno; j < endregno; j++)
11014 if (reg_last_set_invalid[j]
11015 /* If this is a pseudo-register that was only set once and not
11016 live at the beginning of the function, it is always valid. */
11017 || (! (regno >= FIRST_PSEUDO_REGISTER
11018 && REG_N_SETS (regno) == 1
11019 && ! REGNO_REG_SET_P (BASIC_BLOCK (0)->global_live_at_start, regno))
11020 && reg_last_set_label[j] > tick))
11022 if (replace)
11023 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
11024 return replace;
11027 return 1;
11029 /* If this is a memory reference, make sure that there were
11030 no stores after it that might have clobbered the value. We don't
11031 have alias info, so we assume any store invalidates it. */
11032 else if (GET_CODE (x) == MEM && ! RTX_UNCHANGING_P (x)
11033 && INSN_CUID (insn) <= mem_last_set)
11035 if (replace)
11036 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
11037 return replace;
11040 for (i = 0; i < len; i++)
11041 if ((fmt[i] == 'e'
11042 && get_last_value_validate (&XEXP (x, i), insn, tick, replace) == 0)
11043 /* Don't bother with these. They shouldn't occur anyway. */
11044 || fmt[i] == 'E')
11045 return 0;
11047 /* If we haven't found a reason for it to be invalid, it is valid. */
11048 return 1;
11051 /* Get the last value assigned to X, if known. Some registers
11052 in the value may be replaced with (clobber (const_int 0)) if their value
11053 is known longer known reliably. */
11055 static rtx
11056 get_last_value (x)
11057 rtx x;
11059 int regno;
11060 rtx value;
11062 /* If this is a non-paradoxical SUBREG, get the value of its operand and
11063 then convert it to the desired mode. If this is a paradoxical SUBREG,
11064 we cannot predict what values the "extra" bits might have. */
11065 if (GET_CODE (x) == SUBREG
11066 && subreg_lowpart_p (x)
11067 && (GET_MODE_SIZE (GET_MODE (x))
11068 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
11069 && (value = get_last_value (SUBREG_REG (x))) != 0)
11070 return gen_lowpart_for_combine (GET_MODE (x), value);
11072 if (GET_CODE (x) != REG)
11073 return 0;
11075 regno = REGNO (x);
11076 value = reg_last_set_value[regno];
11078 /* If we don't have a value, or if it isn't for this basic block and
11079 it's either a hard register, set more than once, or it's a live
11080 at the beginning of the function, return 0.
11082 Because if it's not live at the beginnning of the function then the reg
11083 is always set before being used (is never used without being set).
11084 And, if it's set only once, and it's always set before use, then all
11085 uses must have the same last value, even if it's not from this basic
11086 block. */
11088 if (value == 0
11089 || (reg_last_set_label[regno] != label_tick
11090 && (regno < FIRST_PSEUDO_REGISTER
11091 || REG_N_SETS (regno) != 1
11092 || REGNO_REG_SET_P (BASIC_BLOCK (0)->global_live_at_start, regno))))
11093 return 0;
11095 /* If the value was set in a later insn than the ones we are processing,
11096 we can't use it even if the register was only set once. */
11097 if (INSN_CUID (reg_last_set[regno]) >= subst_low_cuid)
11098 return 0;
11100 /* If the value has all its registers valid, return it. */
11101 if (get_last_value_validate (&value, reg_last_set[regno],
11102 reg_last_set_label[regno], 0))
11103 return value;
11105 /* Otherwise, make a copy and replace any invalid register with
11106 (clobber (const_int 0)). If that fails for some reason, return 0. */
11108 value = copy_rtx (value);
11109 if (get_last_value_validate (&value, reg_last_set[regno],
11110 reg_last_set_label[regno], 1))
11111 return value;
11113 return 0;
11116 /* Return nonzero if expression X refers to a REG or to memory
11117 that is set in an instruction more recent than FROM_CUID. */
11119 static int
11120 use_crosses_set_p (x, from_cuid)
11121 register rtx x;
11122 int from_cuid;
11124 register const char *fmt;
11125 register int i;
11126 register enum rtx_code code = GET_CODE (x);
11128 if (code == REG)
11130 register int regno = REGNO (x);
11131 int endreg = regno + (regno < FIRST_PSEUDO_REGISTER
11132 ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
11134 #ifdef PUSH_ROUNDING
11135 /* Don't allow uses of the stack pointer to be moved,
11136 because we don't know whether the move crosses a push insn. */
11137 if (regno == STACK_POINTER_REGNUM)
11138 return 1;
11139 #endif
11140 for (;regno < endreg; regno++)
11141 if (reg_last_set[regno]
11142 && INSN_CUID (reg_last_set[regno]) > from_cuid)
11143 return 1;
11144 return 0;
11147 if (code == MEM && mem_last_set > from_cuid)
11148 return 1;
11150 fmt = GET_RTX_FORMAT (code);
11152 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11154 if (fmt[i] == 'E')
11156 register int j;
11157 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
11158 if (use_crosses_set_p (XVECEXP (x, i, j), from_cuid))
11159 return 1;
11161 else if (fmt[i] == 'e'
11162 && use_crosses_set_p (XEXP (x, i), from_cuid))
11163 return 1;
11165 return 0;
11168 /* Define three variables used for communication between the following
11169 routines. */
11171 static int reg_dead_regno, reg_dead_endregno;
11172 static int reg_dead_flag;
11174 /* Function called via note_stores from reg_dead_at_p.
11176 If DEST is within [reg_dead_regno, reg_dead_endregno), set
11177 reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET. */
11179 static void
11180 reg_dead_at_p_1 (dest, x, data)
11181 rtx dest;
11182 rtx x;
11183 void *data ATTRIBUTE_UNUSED;
11185 int regno, endregno;
11187 if (GET_CODE (dest) != REG)
11188 return;
11190 regno = REGNO (dest);
11191 endregno = regno + (regno < FIRST_PSEUDO_REGISTER
11192 ? HARD_REGNO_NREGS (regno, GET_MODE (dest)) : 1);
11194 if (reg_dead_endregno > regno && reg_dead_regno < endregno)
11195 reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
11198 /* Return non-zero if REG is known to be dead at INSN.
11200 We scan backwards from INSN. If we hit a REG_DEAD note or a CLOBBER
11201 referencing REG, it is dead. If we hit a SET referencing REG, it is
11202 live. Otherwise, see if it is live or dead at the start of the basic
11203 block we are in. Hard regs marked as being live in NEWPAT_USED_REGS
11204 must be assumed to be always live. */
11206 static int
11207 reg_dead_at_p (reg, insn)
11208 rtx reg;
11209 rtx insn;
11211 int block, i;
11213 /* Set variables for reg_dead_at_p_1. */
11214 reg_dead_regno = REGNO (reg);
11215 reg_dead_endregno = reg_dead_regno + (reg_dead_regno < FIRST_PSEUDO_REGISTER
11216 ? HARD_REGNO_NREGS (reg_dead_regno,
11217 GET_MODE (reg))
11218 : 1);
11220 reg_dead_flag = 0;
11222 /* Check that reg isn't mentioned in NEWPAT_USED_REGS. */
11223 if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
11225 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
11226 if (TEST_HARD_REG_BIT (newpat_used_regs, i))
11227 return 0;
11230 /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, label, or
11231 beginning of function. */
11232 for (; insn && GET_CODE (insn) != CODE_LABEL && GET_CODE (insn) != BARRIER;
11233 insn = prev_nonnote_insn (insn))
11235 note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
11236 if (reg_dead_flag)
11237 return reg_dead_flag == 1 ? 1 : 0;
11239 if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
11240 return 1;
11243 /* Get the basic block number that we were in. */
11244 if (insn == 0)
11245 block = 0;
11246 else
11248 for (block = 0; block < n_basic_blocks; block++)
11249 if (insn == BLOCK_HEAD (block))
11250 break;
11252 if (block == n_basic_blocks)
11253 return 0;
11256 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
11257 if (REGNO_REG_SET_P (BASIC_BLOCK (block)->global_live_at_start, i))
11258 return 0;
11260 return 1;
11263 /* Note hard registers in X that are used. This code is similar to
11264 that in flow.c, but much simpler since we don't care about pseudos. */
11266 static void
11267 mark_used_regs_combine (x)
11268 rtx x;
11270 register RTX_CODE code = GET_CODE (x);
11271 register int regno;
11272 int i;
11274 switch (code)
11276 case LABEL_REF:
11277 case SYMBOL_REF:
11278 case CONST_INT:
11279 case CONST:
11280 case CONST_DOUBLE:
11281 case PC:
11282 case ADDR_VEC:
11283 case ADDR_DIFF_VEC:
11284 case ASM_INPUT:
11285 #ifdef HAVE_cc0
11286 /* CC0 must die in the insn after it is set, so we don't need to take
11287 special note of it here. */
11288 case CC0:
11289 #endif
11290 return;
11292 case CLOBBER:
11293 /* If we are clobbering a MEM, mark any hard registers inside the
11294 address as used. */
11295 if (GET_CODE (XEXP (x, 0)) == MEM)
11296 mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
11297 return;
11299 case REG:
11300 regno = REGNO (x);
11301 /* A hard reg in a wide mode may really be multiple registers.
11302 If so, mark all of them just like the first. */
11303 if (regno < FIRST_PSEUDO_REGISTER)
11305 /* None of this applies to the stack, frame or arg pointers */
11306 if (regno == STACK_POINTER_REGNUM
11307 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
11308 || regno == HARD_FRAME_POINTER_REGNUM
11309 #endif
11310 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
11311 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
11312 #endif
11313 || regno == FRAME_POINTER_REGNUM)
11314 return;
11316 i = HARD_REGNO_NREGS (regno, GET_MODE (x));
11317 while (i-- > 0)
11318 SET_HARD_REG_BIT (newpat_used_regs, regno + i);
11320 return;
11322 case SET:
11324 /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
11325 the address. */
11326 register rtx testreg = SET_DEST (x);
11328 while (GET_CODE (testreg) == SUBREG
11329 || GET_CODE (testreg) == ZERO_EXTRACT
11330 || GET_CODE (testreg) == SIGN_EXTRACT
11331 || GET_CODE (testreg) == STRICT_LOW_PART)
11332 testreg = XEXP (testreg, 0);
11334 if (GET_CODE (testreg) == MEM)
11335 mark_used_regs_combine (XEXP (testreg, 0));
11337 mark_used_regs_combine (SET_SRC (x));
11339 return;
11341 default:
11342 break;
11345 /* Recursively scan the operands of this expression. */
11348 register const char *fmt = GET_RTX_FORMAT (code);
11350 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11352 if (fmt[i] == 'e')
11353 mark_used_regs_combine (XEXP (x, i));
11354 else if (fmt[i] == 'E')
11356 register int j;
11358 for (j = 0; j < XVECLEN (x, i); j++)
11359 mark_used_regs_combine (XVECEXP (x, i, j));
11366 /* Remove register number REGNO from the dead registers list of INSN.
11368 Return the note used to record the death, if there was one. */
11371 remove_death (regno, insn)
11372 int regno;
11373 rtx insn;
11375 register rtx note = find_regno_note (insn, REG_DEAD, regno);
11377 if (note)
11379 REG_N_DEATHS (regno)--;
11380 remove_note (insn, note);
11383 return note;
11386 /* For each register (hardware or pseudo) used within expression X, if its
11387 death is in an instruction with cuid between FROM_CUID (inclusive) and
11388 TO_INSN (exclusive), put a REG_DEAD note for that register in the
11389 list headed by PNOTES.
11391 That said, don't move registers killed by maybe_kill_insn.
11393 This is done when X is being merged by combination into TO_INSN. These
11394 notes will then be distributed as needed. */
11396 static void
11397 move_deaths (x, maybe_kill_insn, from_cuid, to_insn, pnotes)
11398 rtx x;
11399 rtx maybe_kill_insn;
11400 int from_cuid;
11401 rtx to_insn;
11402 rtx *pnotes;
11404 register const char *fmt;
11405 register int len, i;
11406 register enum rtx_code code = GET_CODE (x);
11408 if (code == REG)
11410 register int regno = REGNO (x);
11411 register rtx where_dead = reg_last_death[regno];
11412 register rtx before_dead, after_dead;
11414 /* Don't move the register if it gets killed in between from and to */
11415 if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
11416 && !reg_referenced_p (x, maybe_kill_insn))
11417 return;
11419 /* WHERE_DEAD could be a USE insn made by combine, so first we
11420 make sure that we have insns with valid INSN_CUID values. */
11421 before_dead = where_dead;
11422 while (before_dead && INSN_UID (before_dead) > max_uid_cuid)
11423 before_dead = PREV_INSN (before_dead);
11424 after_dead = where_dead;
11425 while (after_dead && INSN_UID (after_dead) > max_uid_cuid)
11426 after_dead = NEXT_INSN (after_dead);
11428 if (before_dead && after_dead
11429 && INSN_CUID (before_dead) >= from_cuid
11430 && (INSN_CUID (after_dead) < INSN_CUID (to_insn)
11431 || (where_dead != after_dead
11432 && INSN_CUID (after_dead) == INSN_CUID (to_insn))))
11434 rtx note = remove_death (regno, where_dead);
11436 /* It is possible for the call above to return 0. This can occur
11437 when reg_last_death points to I2 or I1 that we combined with.
11438 In that case make a new note.
11440 We must also check for the case where X is a hard register
11441 and NOTE is a death note for a range of hard registers
11442 including X. In that case, we must put REG_DEAD notes for
11443 the remaining registers in place of NOTE. */
11445 if (note != 0 && regno < FIRST_PSEUDO_REGISTER
11446 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
11447 > GET_MODE_SIZE (GET_MODE (x))))
11449 int deadregno = REGNO (XEXP (note, 0));
11450 int deadend
11451 = (deadregno + HARD_REGNO_NREGS (deadregno,
11452 GET_MODE (XEXP (note, 0))));
11453 int ourend = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
11454 int i;
11456 for (i = deadregno; i < deadend; i++)
11457 if (i < regno || i >= ourend)
11458 REG_NOTES (where_dead)
11459 = gen_rtx_EXPR_LIST (REG_DEAD,
11460 gen_rtx_REG (reg_raw_mode[i], i),
11461 REG_NOTES (where_dead));
11463 /* If we didn't find any note, or if we found a REG_DEAD note that
11464 covers only part of the given reg, and we have a multi-reg hard
11465 register, then to be safe we must check for REG_DEAD notes
11466 for each register other than the first. They could have
11467 their own REG_DEAD notes lying around. */
11468 else if ((note == 0
11469 || (note != 0
11470 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
11471 < GET_MODE_SIZE (GET_MODE (x)))))
11472 && regno < FIRST_PSEUDO_REGISTER
11473 && HARD_REGNO_NREGS (regno, GET_MODE (x)) > 1)
11475 int ourend = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
11476 int i, offset;
11477 rtx oldnotes = 0;
11479 if (note)
11480 offset = HARD_REGNO_NREGS (regno, GET_MODE (XEXP (note, 0)));
11481 else
11482 offset = 1;
11484 for (i = regno + offset; i < ourend; i++)
11485 move_deaths (gen_rtx_REG (reg_raw_mode[i], i),
11486 maybe_kill_insn, from_cuid, to_insn, &oldnotes);
11489 if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
11491 XEXP (note, 1) = *pnotes;
11492 *pnotes = note;
11494 else
11495 *pnotes = gen_rtx_EXPR_LIST (REG_DEAD, x, *pnotes);
11497 REG_N_DEATHS (regno)++;
11500 return;
11503 else if (GET_CODE (x) == SET)
11505 rtx dest = SET_DEST (x);
11507 move_deaths (SET_SRC (x), maybe_kill_insn, from_cuid, to_insn, pnotes);
11509 /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
11510 that accesses one word of a multi-word item, some
11511 piece of everything register in the expression is used by
11512 this insn, so remove any old death. */
11514 if (GET_CODE (dest) == ZERO_EXTRACT
11515 || GET_CODE (dest) == STRICT_LOW_PART
11516 || (GET_CODE (dest) == SUBREG
11517 && (((GET_MODE_SIZE (GET_MODE (dest))
11518 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
11519 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
11520 + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
11522 move_deaths (dest, maybe_kill_insn, from_cuid, to_insn, pnotes);
11523 return;
11526 /* If this is some other SUBREG, we know it replaces the entire
11527 value, so use that as the destination. */
11528 if (GET_CODE (dest) == SUBREG)
11529 dest = SUBREG_REG (dest);
11531 /* If this is a MEM, adjust deaths of anything used in the address.
11532 For a REG (the only other possibility), the entire value is
11533 being replaced so the old value is not used in this insn. */
11535 if (GET_CODE (dest) == MEM)
11536 move_deaths (XEXP (dest, 0), maybe_kill_insn, from_cuid,
11537 to_insn, pnotes);
11538 return;
11541 else if (GET_CODE (x) == CLOBBER)
11542 return;
11544 len = GET_RTX_LENGTH (code);
11545 fmt = GET_RTX_FORMAT (code);
11547 for (i = 0; i < len; i++)
11549 if (fmt[i] == 'E')
11551 register int j;
11552 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
11553 move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_cuid,
11554 to_insn, pnotes);
11556 else if (fmt[i] == 'e')
11557 move_deaths (XEXP (x, i), maybe_kill_insn, from_cuid, to_insn, pnotes);
11561 /* Return 1 if X is the target of a bit-field assignment in BODY, the
11562 pattern of an insn. X must be a REG. */
11564 static int
11565 reg_bitfield_target_p (x, body)
11566 rtx x;
11567 rtx body;
11569 int i;
11571 if (GET_CODE (body) == SET)
11573 rtx dest = SET_DEST (body);
11574 rtx target;
11575 int regno, tregno, endregno, endtregno;
11577 if (GET_CODE (dest) == ZERO_EXTRACT)
11578 target = XEXP (dest, 0);
11579 else if (GET_CODE (dest) == STRICT_LOW_PART)
11580 target = SUBREG_REG (XEXP (dest, 0));
11581 else
11582 return 0;
11584 if (GET_CODE (target) == SUBREG)
11585 target = SUBREG_REG (target);
11587 if (GET_CODE (target) != REG)
11588 return 0;
11590 tregno = REGNO (target), regno = REGNO (x);
11591 if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
11592 return target == x;
11594 endtregno = tregno + HARD_REGNO_NREGS (tregno, GET_MODE (target));
11595 endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
11597 return endregno > tregno && regno < endtregno;
11600 else if (GET_CODE (body) == PARALLEL)
11601 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
11602 if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
11603 return 1;
11605 return 0;
11608 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
11609 as appropriate. I3 and I2 are the insns resulting from the combination
11610 insns including FROM (I2 may be zero).
11612 ELIM_I2 and ELIM_I1 are either zero or registers that we know will
11613 not need REG_DEAD notes because they are being substituted for. This
11614 saves searching in the most common cases.
11616 Each note in the list is either ignored or placed on some insns, depending
11617 on the type of note. */
11619 static void
11620 distribute_notes (notes, from_insn, i3, i2, elim_i2, elim_i1)
11621 rtx notes;
11622 rtx from_insn;
11623 rtx i3, i2;
11624 rtx elim_i2, elim_i1;
11626 rtx note, next_note;
11627 rtx tem;
11629 for (note = notes; note; note = next_note)
11631 rtx place = 0, place2 = 0;
11633 /* If this NOTE references a pseudo register, ensure it references
11634 the latest copy of that register. */
11635 if (XEXP (note, 0) && GET_CODE (XEXP (note, 0)) == REG
11636 && REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER)
11637 XEXP (note, 0) = regno_reg_rtx[REGNO (XEXP (note, 0))];
11639 next_note = XEXP (note, 1);
11640 switch (REG_NOTE_KIND (note))
11642 case REG_BR_PROB:
11643 case REG_EXEC_COUNT:
11644 /* Doesn't matter much where we put this, as long as it's somewhere.
11645 It is preferable to keep these notes on branches, which is most
11646 likely to be i3. */
11647 place = i3;
11648 break;
11650 case REG_EH_REGION:
11651 case REG_EH_RETHROW:
11652 /* These notes must remain with the call. It should not be
11653 possible for both I2 and I3 to be a call. */
11654 if (GET_CODE (i3) == CALL_INSN)
11655 place = i3;
11656 else if (i2 && GET_CODE (i2) == CALL_INSN)
11657 place = i2;
11658 else
11659 abort ();
11660 break;
11662 case REG_UNUSED:
11663 /* Any clobbers for i3 may still exist, and so we must process
11664 REG_UNUSED notes from that insn.
11666 Any clobbers from i2 or i1 can only exist if they were added by
11667 recog_for_combine. In that case, recog_for_combine created the
11668 necessary REG_UNUSED notes. Trying to keep any original
11669 REG_UNUSED notes from these insns can cause incorrect output
11670 if it is for the same register as the original i3 dest.
11671 In that case, we will notice that the register is set in i3,
11672 and then add a REG_UNUSED note for the destination of i3, which
11673 is wrong. However, it is possible to have REG_UNUSED notes from
11674 i2 or i1 for register which were both used and clobbered, so
11675 we keep notes from i2 or i1 if they will turn into REG_DEAD
11676 notes. */
11678 /* If this register is set or clobbered in I3, put the note there
11679 unless there is one already. */
11680 if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
11682 if (from_insn != i3)
11683 break;
11685 if (! (GET_CODE (XEXP (note, 0)) == REG
11686 ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
11687 : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
11688 place = i3;
11690 /* Otherwise, if this register is used by I3, then this register
11691 now dies here, so we must put a REG_DEAD note here unless there
11692 is one already. */
11693 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
11694 && ! (GET_CODE (XEXP (note, 0)) == REG
11695 ? find_regno_note (i3, REG_DEAD, REGNO (XEXP (note, 0)))
11696 : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
11698 PUT_REG_NOTE_KIND (note, REG_DEAD);
11699 place = i3;
11701 break;
11703 case REG_EQUAL:
11704 case REG_EQUIV:
11705 case REG_NONNEG:
11706 case REG_NOALIAS:
11707 /* These notes say something about results of an insn. We can
11708 only support them if they used to be on I3 in which case they
11709 remain on I3. Otherwise they are ignored.
11711 If the note refers to an expression that is not a constant, we
11712 must also ignore the note since we cannot tell whether the
11713 equivalence is still true. It might be possible to do
11714 slightly better than this (we only have a problem if I2DEST
11715 or I1DEST is present in the expression), but it doesn't
11716 seem worth the trouble. */
11718 if (from_insn == i3
11719 && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
11720 place = i3;
11721 break;
11723 case REG_INC:
11724 case REG_NO_CONFLICT:
11725 /* These notes say something about how a register is used. They must
11726 be present on any use of the register in I2 or I3. */
11727 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
11728 place = i3;
11730 if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
11732 if (place)
11733 place2 = i2;
11734 else
11735 place = i2;
11737 break;
11739 case REG_LABEL:
11740 /* This can show up in several ways -- either directly in the
11741 pattern, or hidden off in the constant pool with (or without?)
11742 a REG_EQUAL note. */
11743 /* ??? Ignore the without-reg_equal-note problem for now. */
11744 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
11745 || ((tem = find_reg_note (i3, REG_EQUAL, NULL_RTX))
11746 && GET_CODE (XEXP (tem, 0)) == LABEL_REF
11747 && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0)))
11748 place = i3;
11750 if (i2
11751 && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
11752 || ((tem = find_reg_note (i2, REG_EQUAL, NULL_RTX))
11753 && GET_CODE (XEXP (tem, 0)) == LABEL_REF
11754 && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0))))
11756 if (place)
11757 place2 = i2;
11758 else
11759 place = i2;
11761 break;
11763 case REG_WAS_0:
11764 /* It is too much trouble to try to see if this note is still
11765 correct in all situations. It is better to simply delete it. */
11766 break;
11768 case REG_RETVAL:
11769 /* If the insn previously containing this note still exists,
11770 put it back where it was. Otherwise move it to the previous
11771 insn. Adjust the corresponding REG_LIBCALL note. */
11772 if (GET_CODE (from_insn) != NOTE)
11773 place = from_insn;
11774 else
11776 tem = find_reg_note (XEXP (note, 0), REG_LIBCALL, NULL_RTX);
11777 place = prev_real_insn (from_insn);
11778 if (tem && place)
11779 XEXP (tem, 0) = place;
11781 break;
11783 case REG_LIBCALL:
11784 /* This is handled similarly to REG_RETVAL. */
11785 if (GET_CODE (from_insn) != NOTE)
11786 place = from_insn;
11787 else
11789 tem = find_reg_note (XEXP (note, 0), REG_RETVAL, NULL_RTX);
11790 place = next_real_insn (from_insn);
11791 if (tem && place)
11792 XEXP (tem, 0) = place;
11794 break;
11796 case REG_DEAD:
11797 /* If the register is used as an input in I3, it dies there.
11798 Similarly for I2, if it is non-zero and adjacent to I3.
11800 If the register is not used as an input in either I3 or I2
11801 and it is not one of the registers we were supposed to eliminate,
11802 there are two possibilities. We might have a non-adjacent I2
11803 or we might have somehow eliminated an additional register
11804 from a computation. For example, we might have had A & B where
11805 we discover that B will always be zero. In this case we will
11806 eliminate the reference to A.
11808 In both cases, we must search to see if we can find a previous
11809 use of A and put the death note there. */
11811 if (from_insn
11812 && GET_CODE (from_insn) == CALL_INSN
11813 && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
11814 place = from_insn;
11815 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
11816 place = i3;
11817 else if (i2 != 0 && next_nonnote_insn (i2) == i3
11818 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
11819 place = i2;
11821 if (XEXP (note, 0) == elim_i2 || XEXP (note, 0) == elim_i1)
11822 break;
11824 /* If the register is used in both I2 and I3 and it dies in I3,
11825 we might have added another reference to it. If reg_n_refs
11826 was 2, bump it to 3. This has to be correct since the
11827 register must have been set somewhere. The reason this is
11828 done is because local-alloc.c treats 2 references as a
11829 special case. */
11831 if (place == i3 && i2 != 0 && GET_CODE (XEXP (note, 0)) == REG
11832 && REG_N_REFS (REGNO (XEXP (note, 0)))== 2
11833 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
11834 REG_N_REFS (REGNO (XEXP (note, 0))) = 3;
11836 if (place == 0)
11838 basic_block bb = BASIC_BLOCK (this_basic_block);
11840 for (tem = PREV_INSN (i3); place == 0; tem = PREV_INSN (tem))
11842 if (GET_RTX_CLASS (GET_CODE (tem)) != 'i')
11844 if (tem == bb->head)
11845 break;
11846 continue;
11849 /* If the register is being set at TEM, see if that is all
11850 TEM is doing. If so, delete TEM. Otherwise, make this
11851 into a REG_UNUSED note instead. */
11852 if (reg_set_p (XEXP (note, 0), PATTERN (tem)))
11854 rtx set = single_set (tem);
11855 rtx inner_dest = 0;
11856 #ifdef HAVE_cc0
11857 rtx cc0_setter = NULL_RTX;
11858 #endif
11860 if (set != 0)
11861 for (inner_dest = SET_DEST (set);
11862 GET_CODE (inner_dest) == STRICT_LOW_PART
11863 || GET_CODE (inner_dest) == SUBREG
11864 || GET_CODE (inner_dest) == ZERO_EXTRACT;
11865 inner_dest = XEXP (inner_dest, 0))
11868 /* Verify that it was the set, and not a clobber that
11869 modified the register.
11871 CC0 targets must be careful to maintain setter/user
11872 pairs. If we cannot delete the setter due to side
11873 effects, mark the user with an UNUSED note instead
11874 of deleting it. */
11876 if (set != 0 && ! side_effects_p (SET_SRC (set))
11877 && rtx_equal_p (XEXP (note, 0), inner_dest)
11878 #ifdef HAVE_cc0
11879 && (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
11880 || ((cc0_setter = prev_cc0_setter (tem)) != NULL
11881 && sets_cc0_p (PATTERN (cc0_setter)) > 0))
11882 #endif
11885 /* Move the notes and links of TEM elsewhere.
11886 This might delete other dead insns recursively.
11887 First set the pattern to something that won't use
11888 any register. */
11890 PATTERN (tem) = pc_rtx;
11892 distribute_notes (REG_NOTES (tem), tem, tem,
11893 NULL_RTX, NULL_RTX, NULL_RTX);
11894 distribute_links (LOG_LINKS (tem));
11896 PUT_CODE (tem, NOTE);
11897 NOTE_LINE_NUMBER (tem) = NOTE_INSN_DELETED;
11898 NOTE_SOURCE_FILE (tem) = 0;
11900 #ifdef HAVE_cc0
11901 /* Delete the setter too. */
11902 if (cc0_setter)
11904 PATTERN (cc0_setter) = pc_rtx;
11906 distribute_notes (REG_NOTES (cc0_setter),
11907 cc0_setter, cc0_setter,
11908 NULL_RTX, NULL_RTX, NULL_RTX);
11909 distribute_links (LOG_LINKS (cc0_setter));
11911 PUT_CODE (cc0_setter, NOTE);
11912 NOTE_LINE_NUMBER (cc0_setter)
11913 = NOTE_INSN_DELETED;
11914 NOTE_SOURCE_FILE (cc0_setter) = 0;
11916 #endif
11918 /* If the register is both set and used here, put the
11919 REG_DEAD note here, but place a REG_UNUSED note
11920 here too unless there already is one. */
11921 else if (reg_referenced_p (XEXP (note, 0),
11922 PATTERN (tem)))
11924 place = tem;
11926 if (! find_regno_note (tem, REG_UNUSED,
11927 REGNO (XEXP (note, 0))))
11928 REG_NOTES (tem)
11929 = gen_rtx_EXPR_LIST (REG_UNUSED, XEXP (note, 0),
11930 REG_NOTES (tem));
11932 else
11934 PUT_REG_NOTE_KIND (note, REG_UNUSED);
11936 /* If there isn't already a REG_UNUSED note, put one
11937 here. */
11938 if (! find_regno_note (tem, REG_UNUSED,
11939 REGNO (XEXP (note, 0))))
11940 place = tem;
11941 break;
11944 else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem))
11945 || (GET_CODE (tem) == CALL_INSN
11946 && find_reg_fusage (tem, USE, XEXP (note, 0))))
11948 place = tem;
11950 /* If we are doing a 3->2 combination, and we have a
11951 register which formerly died in i3 and was not used
11952 by i2, which now no longer dies in i3 and is used in
11953 i2 but does not die in i2, and place is between i2
11954 and i3, then we may need to move a link from place to
11955 i2. */
11956 if (i2 && INSN_UID (place) <= max_uid_cuid
11957 && INSN_CUID (place) > INSN_CUID (i2)
11958 && from_insn && INSN_CUID (from_insn) > INSN_CUID (i2)
11959 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
11961 rtx links = LOG_LINKS (place);
11962 LOG_LINKS (place) = 0;
11963 distribute_links (links);
11965 break;
11968 if (tem == bb->head)
11969 break;
11972 /* We haven't found an insn for the death note and it
11973 is still a REG_DEAD note, but we have hit the beginning
11974 of the block. If the existing life info says the reg
11975 was dead, there's nothing left to do. Otherwise, we'll
11976 need to do a global life update after combine. */
11977 if (REG_NOTE_KIND (note) == REG_DEAD && place == 0)
11979 int regno = REGNO (XEXP (note, 0));
11980 if (REGNO_REG_SET_P (bb->global_live_at_start, regno))
11982 SET_BIT (refresh_blocks, this_basic_block);
11983 need_refresh = 1;
11988 /* If the register is set or already dead at PLACE, we needn't do
11989 anything with this note if it is still a REG_DEAD note.
11990 We can here if it is set at all, not if is it totally replace,
11991 which is what `dead_or_set_p' checks, so also check for it being
11992 set partially. */
11994 if (place && REG_NOTE_KIND (note) == REG_DEAD)
11996 int regno = REGNO (XEXP (note, 0));
11998 if (dead_or_set_p (place, XEXP (note, 0))
11999 || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
12001 /* Unless the register previously died in PLACE, clear
12002 reg_last_death. [I no longer understand why this is
12003 being done.] */
12004 if (reg_last_death[regno] != place)
12005 reg_last_death[regno] = 0;
12006 place = 0;
12008 else
12009 reg_last_death[regno] = place;
12011 /* If this is a death note for a hard reg that is occupying
12012 multiple registers, ensure that we are still using all
12013 parts of the object. If we find a piece of the object
12014 that is unused, we must add a USE for that piece before
12015 PLACE and put the appropriate REG_DEAD note on it.
12017 An alternative would be to put a REG_UNUSED for the pieces
12018 on the insn that set the register, but that can't be done if
12019 it is not in the same block. It is simpler, though less
12020 efficient, to add the USE insns. */
12022 if (place && regno < FIRST_PSEUDO_REGISTER
12023 && HARD_REGNO_NREGS (regno, GET_MODE (XEXP (note, 0))) > 1)
12025 int endregno
12026 = regno + HARD_REGNO_NREGS (regno,
12027 GET_MODE (XEXP (note, 0)));
12028 int all_used = 1;
12029 int i;
12031 for (i = regno; i < endregno; i++)
12032 if (! refers_to_regno_p (i, i + 1, PATTERN (place), 0)
12033 && ! find_regno_fusage (place, USE, i))
12035 rtx piece = gen_rtx_REG (reg_raw_mode[i], i);
12036 rtx p;
12038 /* See if we already placed a USE note for this
12039 register in front of PLACE. */
12040 for (p = place;
12041 GET_CODE (PREV_INSN (p)) == INSN
12042 && GET_CODE (PATTERN (PREV_INSN (p))) == USE;
12043 p = PREV_INSN (p))
12044 if (rtx_equal_p (piece,
12045 XEXP (PATTERN (PREV_INSN (p)), 0)))
12047 p = 0;
12048 break;
12051 if (p)
12053 rtx use_insn
12054 = emit_insn_before (gen_rtx_USE (VOIDmode,
12055 piece),
12057 REG_NOTES (use_insn)
12058 = gen_rtx_EXPR_LIST (REG_DEAD, piece,
12059 REG_NOTES (use_insn));
12062 all_used = 0;
12065 /* Check for the case where the register dying partially
12066 overlaps the register set by this insn. */
12067 if (all_used)
12068 for (i = regno; i < endregno; i++)
12069 if (dead_or_set_regno_p (place, i))
12071 all_used = 0;
12072 break;
12075 if (! all_used)
12077 /* Put only REG_DEAD notes for pieces that are
12078 still used and that are not already dead or set. */
12080 for (i = regno; i < endregno; i++)
12082 rtx piece = gen_rtx_REG (reg_raw_mode[i], i);
12084 if ((reg_referenced_p (piece, PATTERN (place))
12085 || (GET_CODE (place) == CALL_INSN
12086 && find_reg_fusage (place, USE, piece)))
12087 && ! dead_or_set_p (place, piece)
12088 && ! reg_bitfield_target_p (piece,
12089 PATTERN (place)))
12090 REG_NOTES (place)
12091 = gen_rtx_EXPR_LIST (REG_DEAD, piece,
12092 REG_NOTES (place));
12095 place = 0;
12099 break;
12101 default:
12102 /* Any other notes should not be present at this point in the
12103 compilation. */
12104 abort ();
12107 if (place)
12109 XEXP (note, 1) = REG_NOTES (place);
12110 REG_NOTES (place) = note;
12112 else if ((REG_NOTE_KIND (note) == REG_DEAD
12113 || REG_NOTE_KIND (note) == REG_UNUSED)
12114 && GET_CODE (XEXP (note, 0)) == REG)
12115 REG_N_DEATHS (REGNO (XEXP (note, 0)))--;
12117 if (place2)
12119 if ((REG_NOTE_KIND (note) == REG_DEAD
12120 || REG_NOTE_KIND (note) == REG_UNUSED)
12121 && GET_CODE (XEXP (note, 0)) == REG)
12122 REG_N_DEATHS (REGNO (XEXP (note, 0)))++;
12124 REG_NOTES (place2) = gen_rtx_fmt_ee (GET_CODE (note),
12125 REG_NOTE_KIND (note),
12126 XEXP (note, 0),
12127 REG_NOTES (place2));
12132 /* Similarly to above, distribute the LOG_LINKS that used to be present on
12133 I3, I2, and I1 to new locations. This is also called in one case to
12134 add a link pointing at I3 when I3's destination is changed. */
12136 static void
12137 distribute_links (links)
12138 rtx links;
12140 rtx link, next_link;
12142 for (link = links; link; link = next_link)
12144 rtx place = 0;
12145 rtx insn;
12146 rtx set, reg;
12148 next_link = XEXP (link, 1);
12150 /* If the insn that this link points to is a NOTE or isn't a single
12151 set, ignore it. In the latter case, it isn't clear what we
12152 can do other than ignore the link, since we can't tell which
12153 register it was for. Such links wouldn't be used by combine
12154 anyway.
12156 It is not possible for the destination of the target of the link to
12157 have been changed by combine. The only potential of this is if we
12158 replace I3, I2, and I1 by I3 and I2. But in that case the
12159 destination of I2 also remains unchanged. */
12161 if (GET_CODE (XEXP (link, 0)) == NOTE
12162 || (set = single_set (XEXP (link, 0))) == 0)
12163 continue;
12165 reg = SET_DEST (set);
12166 while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
12167 || GET_CODE (reg) == SIGN_EXTRACT
12168 || GET_CODE (reg) == STRICT_LOW_PART)
12169 reg = XEXP (reg, 0);
12171 /* A LOG_LINK is defined as being placed on the first insn that uses
12172 a register and points to the insn that sets the register. Start
12173 searching at the next insn after the target of the link and stop
12174 when we reach a set of the register or the end of the basic block.
12176 Note that this correctly handles the link that used to point from
12177 I3 to I2. Also note that not much searching is typically done here
12178 since most links don't point very far away. */
12180 for (insn = NEXT_INSN (XEXP (link, 0));
12181 (insn && (this_basic_block == n_basic_blocks - 1
12182 || BLOCK_HEAD (this_basic_block + 1) != insn));
12183 insn = NEXT_INSN (insn))
12184 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
12185 && reg_overlap_mentioned_p (reg, PATTERN (insn)))
12187 if (reg_referenced_p (reg, PATTERN (insn)))
12188 place = insn;
12189 break;
12191 else if (GET_CODE (insn) == CALL_INSN
12192 && find_reg_fusage (insn, USE, reg))
12194 place = insn;
12195 break;
12198 /* If we found a place to put the link, place it there unless there
12199 is already a link to the same insn as LINK at that point. */
12201 if (place)
12203 rtx link2;
12205 for (link2 = LOG_LINKS (place); link2; link2 = XEXP (link2, 1))
12206 if (XEXP (link2, 0) == XEXP (link, 0))
12207 break;
12209 if (link2 == 0)
12211 XEXP (link, 1) = LOG_LINKS (place);
12212 LOG_LINKS (place) = link;
12214 /* Set added_links_insn to the earliest insn we added a
12215 link to. */
12216 if (added_links_insn == 0
12217 || INSN_CUID (added_links_insn) > INSN_CUID (place))
12218 added_links_insn = place;
12224 /* Compute INSN_CUID for INSN, which is an insn made by combine. */
12226 static int
12227 insn_cuid (insn)
12228 rtx insn;
12230 while (insn != 0 && INSN_UID (insn) > max_uid_cuid
12231 && GET_CODE (insn) == INSN && GET_CODE (PATTERN (insn)) == USE)
12232 insn = NEXT_INSN (insn);
12234 if (INSN_UID (insn) > max_uid_cuid)
12235 abort ();
12237 return INSN_CUID (insn);
12240 void
12241 dump_combine_stats (file)
12242 FILE *file;
12244 fnotice
12245 (file,
12246 ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
12247 combine_attempts, combine_merges, combine_extras, combine_successes);
12250 void
12251 dump_combine_total_stats (file)
12252 FILE *file;
12254 fnotice
12255 (file,
12256 "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
12257 total_attempts, total_merges, total_extras, total_successes);