* i386.c (notice_update_cc): Remove bogus pentium GCC code.
[official-gcc.git] / gcc / combine.c
blobbcb6fe9c783f3c6d9e915c6b66aba2a93869b649
1 /* Optimize by combining instructions for GNU compiler.
2 Copyright (C) 1987, 88, 92-96, 1997 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 #ifdef __STDC__
79 #include <stdarg.h>
80 #else
81 #include <varargs.h>
82 #endif
84 /* Must precede rtl.h for FFS. */
85 #include <stdio.h>
87 #include "rtl.h"
88 #include "flags.h"
89 #include "regs.h"
90 #include "hard-reg-set.h"
91 #include "expr.h"
92 #include "basic-block.h"
93 #include "insn-config.h"
94 #include "insn-flags.h"
95 #include "insn-codes.h"
96 #include "insn-attr.h"
97 #include "recog.h"
98 #include "real.h"
100 /* It is not safe to use ordinary gen_lowpart in combine.
101 Use gen_lowpart_for_combine instead. See comments there. */
102 #define gen_lowpart dont_use_gen_lowpart_you_dummy
104 /* Number of attempts to combine instructions in this function. */
106 static int combine_attempts;
108 /* Number of attempts that got as far as substitution in this function. */
110 static int combine_merges;
112 /* Number of instructions combined with added SETs in this function. */
114 static int combine_extras;
116 /* Number of instructions combined in this function. */
118 static int combine_successes;
120 /* Totals over entire compilation. */
122 static int total_attempts, total_merges, total_extras, total_successes;
124 /* Define a default value for REVERSIBLE_CC_MODE.
125 We can never assume that a condition code mode is safe to reverse unless
126 the md tells us so. */
127 #ifndef REVERSIBLE_CC_MODE
128 #define REVERSIBLE_CC_MODE(MODE) 0
129 #endif
131 /* Vector mapping INSN_UIDs to cuids.
132 The cuids are like uids but increase monotonically always.
133 Combine always uses cuids so that it can compare them.
134 But actually renumbering the uids, which we used to do,
135 proves to be a bad idea because it makes it hard to compare
136 the dumps produced by earlier passes with those from later passes. */
138 static int *uid_cuid;
139 static int max_uid_cuid;
141 /* Get the cuid of an insn. */
143 #define INSN_CUID(INSN) \
144 (INSN_UID (INSN) > max_uid_cuid ? insn_cuid (INSN) : uid_cuid[INSN_UID (INSN)])
146 /* Maximum register number, which is the size of the tables below. */
148 static int combine_max_regno;
150 /* Record last point of death of (hard or pseudo) register n. */
152 static rtx *reg_last_death;
154 /* Record last point of modification of (hard or pseudo) register n. */
156 static rtx *reg_last_set;
158 /* Record the cuid of the last insn that invalidated memory
159 (anything that writes memory, and subroutine calls, but not pushes). */
161 static int mem_last_set;
163 /* Record the cuid of the last CALL_INSN
164 so we can tell whether a potential combination crosses any calls. */
166 static int last_call_cuid;
168 /* When `subst' is called, this is the insn that is being modified
169 (by combining in a previous insn). The PATTERN of this insn
170 is still the old pattern partially modified and it should not be
171 looked at, but this may be used to examine the successors of the insn
172 to judge whether a simplification is valid. */
174 static rtx subst_insn;
176 /* This is an insn that belongs before subst_insn, but is not currently
177 on the insn chain. */
179 static rtx subst_prev_insn;
181 /* This is the lowest CUID that `subst' is currently dealing with.
182 get_last_value will not return a value if the register was set at or
183 after this CUID. If not for this mechanism, we could get confused if
184 I2 or I1 in try_combine were an insn that used the old value of a register
185 to obtain a new value. In that case, we might erroneously get the
186 new value of the register when we wanted the old one. */
188 static int subst_low_cuid;
190 /* This contains any hard registers that are used in newpat; reg_dead_at_p
191 must consider all these registers to be always live. */
193 static HARD_REG_SET newpat_used_regs;
195 /* This is an insn to which a LOG_LINKS entry has been added. If this
196 insn is the earlier than I2 or I3, combine should rescan starting at
197 that location. */
199 static rtx added_links_insn;
201 /* Basic block number of the block in which we are performing combines. */
202 static int this_basic_block;
204 /* The next group of arrays allows the recording of the last value assigned
205 to (hard or pseudo) register n. We use this information to see if a
206 operation being processed is redundant given a prior operation performed
207 on the register. For example, an `and' with a constant is redundant if
208 all the zero bits are already known to be turned off.
210 We use an approach similar to that used by cse, but change it in the
211 following ways:
213 (1) We do not want to reinitialize at each label.
214 (2) It is useful, but not critical, to know the actual value assigned
215 to a register. Often just its form is helpful.
217 Therefore, we maintain the following arrays:
219 reg_last_set_value the last value assigned
220 reg_last_set_label records the value of label_tick when the
221 register was assigned
222 reg_last_set_table_tick records the value of label_tick when a
223 value using the register is assigned
224 reg_last_set_invalid set to non-zero when it is not valid
225 to use the value of this register in some
226 register's value
228 To understand the usage of these tables, it is important to understand
229 the distinction between the value in reg_last_set_value being valid
230 and the register being validly contained in some other expression in the
231 table.
233 Entry I in reg_last_set_value is valid if it is non-zero, and either
234 reg_n_sets[i] is 1 or reg_last_set_label[i] == label_tick.
236 Register I may validly appear in any expression returned for the value
237 of another register if reg_n_sets[i] is 1. It may also appear in the
238 value for register J if reg_last_set_label[i] < reg_last_set_label[j] or
239 reg_last_set_invalid[j] is zero.
241 If an expression is found in the table containing a register which may
242 not validly appear in an expression, the register is replaced by
243 something that won't match, (clobber (const_int 0)).
245 reg_last_set_invalid[i] is set non-zero when register I is being assigned
246 to and reg_last_set_table_tick[i] == label_tick. */
248 /* Record last value assigned to (hard or pseudo) register n. */
250 static rtx *reg_last_set_value;
252 /* Record the value of label_tick when the value for register n is placed in
253 reg_last_set_value[n]. */
255 static int *reg_last_set_label;
257 /* Record the value of label_tick when an expression involving register n
258 is placed in reg_last_set_value. */
260 static int *reg_last_set_table_tick;
262 /* Set non-zero if references to register n in expressions should not be
263 used. */
265 static char *reg_last_set_invalid;
267 /* Incremented for each label. */
269 static int label_tick;
271 /* Some registers that are set more than once and used in more than one
272 basic block are nevertheless always set in similar ways. For example,
273 a QImode register may be loaded from memory in two places on a machine
274 where byte loads zero extend.
276 We record in the following array what we know about the nonzero
277 bits of a register, specifically which bits are known to be zero.
279 If an entry is zero, it means that we don't know anything special. */
281 static unsigned HOST_WIDE_INT *reg_nonzero_bits;
283 /* Mode used to compute significance in reg_nonzero_bits. It is the largest
284 integer mode that can fit in HOST_BITS_PER_WIDE_INT. */
286 static enum machine_mode nonzero_bits_mode;
288 /* Nonzero if we know that a register has some leading bits that are always
289 equal to the sign bit. */
291 static char *reg_sign_bit_copies;
293 /* Nonzero when reg_nonzero_bits and reg_sign_bit_copies can be safely used.
294 It is zero while computing them and after combine has completed. This
295 former test prevents propagating values based on previously set values,
296 which can be incorrect if a variable is modified in a loop. */
298 static int nonzero_sign_valid;
300 /* These arrays are maintained in parallel with reg_last_set_value
301 and are used to store the mode in which the register was last set,
302 the bits that were known to be zero when it was last set, and the
303 number of sign bits copies it was known to have when it was last set. */
305 static enum machine_mode *reg_last_set_mode;
306 static unsigned HOST_WIDE_INT *reg_last_set_nonzero_bits;
307 static char *reg_last_set_sign_bit_copies;
309 /* Record one modification to rtl structure
310 to be undone by storing old_contents into *where.
311 is_int is 1 if the contents are an int. */
313 struct undo
315 struct undo *next;
316 int is_int;
317 union {rtx r; int i;} old_contents;
318 union {rtx *r; int *i;} where;
321 /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
322 num_undo says how many are currently recorded.
324 storage is nonzero if we must undo the allocation of new storage.
325 The value of storage is what to pass to obfree.
327 other_insn is nonzero if we have modified some other insn in the process
328 of working on subst_insn. It must be verified too.
330 previous_undos is the value of undobuf.undos when we started processing
331 this substitution. This will prevent gen_rtx_combine from re-used a piece
332 from the previous expression. Doing so can produce circular rtl
333 structures. */
335 struct undobuf
337 char *storage;
338 struct undo *undos;
339 struct undo *frees;
340 struct undo *previous_undos;
341 rtx other_insn;
344 static struct undobuf undobuf;
346 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
347 insn. The substitution can be undone by undo_all. If INTO is already
348 set to NEWVAL, do not record this change. Because computing NEWVAL might
349 also call SUBST, we have to compute it before we put anything into
350 the undo table. */
352 #define SUBST(INTO, NEWVAL) \
353 do { rtx _new = (NEWVAL); \
354 struct undo *_buf; \
356 if (undobuf.frees) \
357 _buf = undobuf.frees, undobuf.frees = _buf->next; \
358 else \
359 _buf = (struct undo *) xmalloc (sizeof (struct undo)); \
361 _buf->is_int = 0; \
362 _buf->where.r = &INTO; \
363 _buf->old_contents.r = INTO; \
364 INTO = _new; \
365 if (_buf->old_contents.r == INTO) \
366 _buf->next = undobuf.frees, undobuf.frees = _buf; \
367 else \
368 _buf->next = undobuf.undos, undobuf.undos = _buf; \
369 } while (0)
371 /* Similar to SUBST, but NEWVAL is an int expression. Note that substitution
372 for the value of a HOST_WIDE_INT value (including CONST_INT) is
373 not safe. */
375 #define SUBST_INT(INTO, NEWVAL) \
376 do { struct undo *_buf; \
378 if (undobuf.frees) \
379 _buf = undobuf.frees, undobuf.frees = _buf->next; \
380 else \
381 _buf = (struct undo *) xmalloc (sizeof (struct undo)); \
383 _buf->is_int = 1; \
384 _buf->where.i = (int *) &INTO; \
385 _buf->old_contents.i = INTO; \
386 INTO = NEWVAL; \
387 if (_buf->old_contents.i == INTO) \
388 _buf->next = undobuf.frees, undobuf.frees = _buf; \
389 else \
390 _buf->next = undobuf.undos, undobuf.undos = _buf; \
391 } while (0)
393 /* Number of times the pseudo being substituted for
394 was found and replaced. */
396 static int n_occurrences;
398 static void init_reg_last_arrays PROTO((void));
399 static void setup_incoming_promotions PROTO((void));
400 static void set_nonzero_bits_and_sign_copies PROTO((rtx, rtx));
401 static int can_combine_p PROTO((rtx, rtx, rtx, rtx, rtx *, rtx *));
402 static int combinable_i3pat PROTO((rtx, rtx *, rtx, rtx, int, rtx *));
403 static rtx try_combine PROTO((rtx, rtx, rtx));
404 static void undo_all PROTO((void));
405 static rtx *find_split_point PROTO((rtx *, rtx));
406 static rtx subst PROTO((rtx, rtx, rtx, int, int));
407 static rtx simplify_rtx PROTO((rtx, enum machine_mode, int, int));
408 static rtx simplify_if_then_else PROTO((rtx));
409 static rtx simplify_set PROTO((rtx));
410 static rtx simplify_logical PROTO((rtx, int));
411 static rtx expand_compound_operation PROTO((rtx));
412 static rtx expand_field_assignment PROTO((rtx));
413 static rtx make_extraction PROTO((enum machine_mode, rtx, int, rtx, int,
414 int, int, int));
415 static rtx extract_left_shift PROTO((rtx, int));
416 static rtx make_compound_operation PROTO((rtx, enum rtx_code));
417 static int get_pos_from_mask PROTO((unsigned HOST_WIDE_INT, int *));
418 static rtx force_to_mode PROTO((rtx, enum machine_mode,
419 unsigned HOST_WIDE_INT, rtx, int));
420 static rtx if_then_else_cond PROTO((rtx, rtx *, rtx *));
421 static rtx known_cond PROTO((rtx, enum rtx_code, rtx, rtx));
422 static int rtx_equal_for_field_assignment_p PROTO((rtx, rtx));
423 static rtx make_field_assignment PROTO((rtx));
424 static rtx apply_distributive_law PROTO((rtx));
425 static rtx simplify_and_const_int PROTO((rtx, enum machine_mode, rtx,
426 unsigned HOST_WIDE_INT));
427 static unsigned HOST_WIDE_INT nonzero_bits PROTO((rtx, enum machine_mode));
428 static int num_sign_bit_copies PROTO((rtx, enum machine_mode));
429 static int merge_outer_ops PROTO((enum rtx_code *, HOST_WIDE_INT *,
430 enum rtx_code, HOST_WIDE_INT,
431 enum machine_mode, int *));
432 static rtx simplify_shift_const PROTO((rtx, enum rtx_code, enum machine_mode,
433 rtx, int));
434 static int recog_for_combine PROTO((rtx *, rtx, rtx *, int *));
435 static rtx gen_lowpart_for_combine PROTO((enum machine_mode, rtx));
436 static rtx gen_rtx_combine PVPROTO((enum rtx_code code, enum machine_mode mode,
437 ...));
438 static rtx gen_binary PROTO((enum rtx_code, enum machine_mode,
439 rtx, rtx));
440 static rtx gen_unary PROTO((enum rtx_code, enum machine_mode,
441 enum machine_mode, rtx));
442 static enum rtx_code simplify_comparison PROTO((enum rtx_code, rtx *, rtx *));
443 static int reversible_comparison_p PROTO((rtx));
444 static void update_table_tick PROTO((rtx));
445 static void record_value_for_reg PROTO((rtx, rtx, rtx));
446 static void record_dead_and_set_regs_1 PROTO((rtx, rtx));
447 static void record_dead_and_set_regs PROTO((rtx));
448 static int get_last_value_validate PROTO((rtx *, rtx, int, int));
449 static rtx get_last_value PROTO((rtx));
450 static int use_crosses_set_p PROTO((rtx, int));
451 static void reg_dead_at_p_1 PROTO((rtx, rtx));
452 static int reg_dead_at_p PROTO((rtx, rtx));
453 static void move_deaths PROTO((rtx, rtx, int, rtx, rtx *));
454 static int reg_bitfield_target_p PROTO((rtx, rtx));
455 static void distribute_notes PROTO((rtx, rtx, rtx, rtx, rtx, rtx));
456 static void distribute_links PROTO((rtx));
457 static void mark_used_regs_combine PROTO((rtx));
458 static int insn_cuid PROTO((rtx));
460 /* Main entry point for combiner. F is the first insn of the function.
461 NREGS is the first unused pseudo-reg number. */
463 void
464 combine_instructions (f, nregs)
465 rtx f;
466 int nregs;
468 register rtx insn, next, prev;
469 register int i;
470 register rtx links, nextlinks;
472 combine_attempts = 0;
473 combine_merges = 0;
474 combine_extras = 0;
475 combine_successes = 0;
476 undobuf.undos = undobuf.previous_undos = 0;
478 combine_max_regno = nregs;
480 reg_nonzero_bits
481 = (unsigned HOST_WIDE_INT *) alloca (nregs * sizeof (HOST_WIDE_INT));
482 reg_sign_bit_copies = (char *) alloca (nregs * sizeof (char));
484 bzero ((char *) reg_nonzero_bits, nregs * sizeof (HOST_WIDE_INT));
485 bzero (reg_sign_bit_copies, nregs * sizeof (char));
487 reg_last_death = (rtx *) alloca (nregs * sizeof (rtx));
488 reg_last_set = (rtx *) alloca (nregs * sizeof (rtx));
489 reg_last_set_value = (rtx *) alloca (nregs * sizeof (rtx));
490 reg_last_set_table_tick = (int *) alloca (nregs * sizeof (int));
491 reg_last_set_label = (int *) alloca (nregs * sizeof (int));
492 reg_last_set_invalid = (char *) alloca (nregs * sizeof (char));
493 reg_last_set_mode
494 = (enum machine_mode *) alloca (nregs * sizeof (enum machine_mode));
495 reg_last_set_nonzero_bits
496 = (unsigned HOST_WIDE_INT *) alloca (nregs * sizeof (HOST_WIDE_INT));
497 reg_last_set_sign_bit_copies
498 = (char *) alloca (nregs * sizeof (char));
500 init_reg_last_arrays ();
502 init_recog_no_volatile ();
504 /* Compute maximum uid value so uid_cuid can be allocated. */
506 for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
507 if (INSN_UID (insn) > i)
508 i = INSN_UID (insn);
510 uid_cuid = (int *) alloca ((i + 1) * sizeof (int));
511 max_uid_cuid = i;
513 nonzero_bits_mode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
515 /* Don't use reg_nonzero_bits when computing it. This can cause problems
516 when, for example, we have j <<= 1 in a loop. */
518 nonzero_sign_valid = 0;
520 /* Compute the mapping from uids to cuids.
521 Cuids are numbers assigned to insns, like uids,
522 except that cuids increase monotonically through the code.
524 Scan all SETs and see if we can deduce anything about what
525 bits are known to be zero for some registers and how many copies
526 of the sign bit are known to exist for those registers.
528 Also set any known values so that we can use it while searching
529 for what bits are known to be set. */
531 label_tick = 1;
533 /* We need to initialize it here, because record_dead_and_set_regs may call
534 get_last_value. */
535 subst_prev_insn = NULL_RTX;
537 setup_incoming_promotions ();
539 for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
541 uid_cuid[INSN_UID (insn)] = ++i;
542 subst_low_cuid = i;
543 subst_insn = insn;
545 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
547 note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies);
548 record_dead_and_set_regs (insn);
550 #ifdef AUTO_INC_DEC
551 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
552 if (REG_NOTE_KIND (links) == REG_INC)
553 set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX);
554 #endif
557 if (GET_CODE (insn) == CODE_LABEL)
558 label_tick++;
561 nonzero_sign_valid = 1;
563 /* Now scan all the insns in forward order. */
565 this_basic_block = -1;
566 label_tick = 1;
567 last_call_cuid = 0;
568 mem_last_set = 0;
569 init_reg_last_arrays ();
570 setup_incoming_promotions ();
572 for (insn = f; insn; insn = next ? next : NEXT_INSN (insn))
574 next = 0;
576 /* If INSN starts a new basic block, update our basic block number. */
577 if (this_basic_block + 1 < n_basic_blocks
578 && basic_block_head[this_basic_block + 1] == insn)
579 this_basic_block++;
581 if (GET_CODE (insn) == CODE_LABEL)
582 label_tick++;
584 else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
586 /* Try this insn with each insn it links back to. */
588 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
589 if ((next = try_combine (insn, XEXP (links, 0), NULL_RTX)) != 0)
590 goto retry;
592 /* Try each sequence of three linked insns ending with this one. */
594 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
595 for (nextlinks = LOG_LINKS (XEXP (links, 0)); nextlinks;
596 nextlinks = XEXP (nextlinks, 1))
597 if ((next = try_combine (insn, XEXP (links, 0),
598 XEXP (nextlinks, 0))) != 0)
599 goto retry;
601 #ifdef HAVE_cc0
602 /* Try to combine a jump insn that uses CC0
603 with a preceding insn that sets CC0, and maybe with its
604 logical predecessor as well.
605 This is how we make decrement-and-branch insns.
606 We need this special code because data flow connections
607 via CC0 do not get entered in LOG_LINKS. */
609 if (GET_CODE (insn) == JUMP_INSN
610 && (prev = prev_nonnote_insn (insn)) != 0
611 && GET_CODE (prev) == INSN
612 && sets_cc0_p (PATTERN (prev)))
614 if ((next = try_combine (insn, prev, NULL_RTX)) != 0)
615 goto retry;
617 for (nextlinks = LOG_LINKS (prev); nextlinks;
618 nextlinks = XEXP (nextlinks, 1))
619 if ((next = try_combine (insn, prev,
620 XEXP (nextlinks, 0))) != 0)
621 goto retry;
624 /* Do the same for an insn that explicitly references CC0. */
625 if (GET_CODE (insn) == INSN
626 && (prev = prev_nonnote_insn (insn)) != 0
627 && GET_CODE (prev) == INSN
628 && sets_cc0_p (PATTERN (prev))
629 && GET_CODE (PATTERN (insn)) == SET
630 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
632 if ((next = try_combine (insn, prev, NULL_RTX)) != 0)
633 goto retry;
635 for (nextlinks = LOG_LINKS (prev); nextlinks;
636 nextlinks = XEXP (nextlinks, 1))
637 if ((next = try_combine (insn, prev,
638 XEXP (nextlinks, 0))) != 0)
639 goto retry;
642 /* Finally, see if any of the insns that this insn links to
643 explicitly references CC0. If so, try this insn, that insn,
644 and its predecessor if it sets CC0. */
645 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
646 if (GET_CODE (XEXP (links, 0)) == INSN
647 && GET_CODE (PATTERN (XEXP (links, 0))) == SET
648 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (XEXP (links, 0))))
649 && (prev = prev_nonnote_insn (XEXP (links, 0))) != 0
650 && GET_CODE (prev) == INSN
651 && sets_cc0_p (PATTERN (prev))
652 && (next = try_combine (insn, XEXP (links, 0), prev)) != 0)
653 goto retry;
654 #endif
656 /* Try combining an insn with two different insns whose results it
657 uses. */
658 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
659 for (nextlinks = XEXP (links, 1); nextlinks;
660 nextlinks = XEXP (nextlinks, 1))
661 if ((next = try_combine (insn, XEXP (links, 0),
662 XEXP (nextlinks, 0))) != 0)
663 goto retry;
665 if (GET_CODE (insn) != NOTE)
666 record_dead_and_set_regs (insn);
668 retry:
673 total_attempts += combine_attempts;
674 total_merges += combine_merges;
675 total_extras += combine_extras;
676 total_successes += combine_successes;
678 nonzero_sign_valid = 0;
681 /* Wipe the reg_last_xxx arrays in preparation for another pass. */
683 static void
684 init_reg_last_arrays ()
686 int nregs = combine_max_regno;
688 bzero ((char *) reg_last_death, nregs * sizeof (rtx));
689 bzero ((char *) reg_last_set, nregs * sizeof (rtx));
690 bzero ((char *) reg_last_set_value, nregs * sizeof (rtx));
691 bzero ((char *) reg_last_set_table_tick, nregs * sizeof (int));
692 bzero ((char *) reg_last_set_label, nregs * sizeof (int));
693 bzero (reg_last_set_invalid, nregs * sizeof (char));
694 bzero ((char *) reg_last_set_mode, nregs * sizeof (enum machine_mode));
695 bzero ((char *) reg_last_set_nonzero_bits, nregs * sizeof (HOST_WIDE_INT));
696 bzero (reg_last_set_sign_bit_copies, nregs * sizeof (char));
699 /* Set up any promoted values for incoming argument registers. */
701 static void
702 setup_incoming_promotions ()
704 #ifdef PROMOTE_FUNCTION_ARGS
705 int regno;
706 rtx reg;
707 enum machine_mode mode;
708 int unsignedp;
709 rtx first = get_insns ();
711 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
712 if (FUNCTION_ARG_REGNO_P (regno)
713 && (reg = promoted_input_arg (regno, &mode, &unsignedp)) != 0)
714 record_value_for_reg (reg, first,
715 gen_rtx (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
716 GET_MODE (reg),
717 gen_rtx (CLOBBER, mode, const0_rtx)));
718 #endif
721 /* Called via note_stores. If X is a pseudo that is narrower than
722 HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
724 If we are setting only a portion of X and we can't figure out what
725 portion, assume all bits will be used since we don't know what will
726 be happening.
728 Similarly, set how many bits of X are known to be copies of the sign bit
729 at all locations in the function. This is the smallest number implied
730 by any set of X. */
732 static void
733 set_nonzero_bits_and_sign_copies (x, set)
734 rtx x;
735 rtx set;
737 int num;
739 if (GET_CODE (x) == REG
740 && REGNO (x) >= FIRST_PSEUDO_REGISTER
741 /* If this register is undefined at the start of the file, we can't
742 say what its contents were. */
743 && ! REGNO_REG_SET_P (basic_block_live_at_start[0], REGNO (x))
744 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
746 if (set == 0 || GET_CODE (set) == CLOBBER)
748 reg_nonzero_bits[REGNO (x)] = GET_MODE_MASK (GET_MODE (x));
749 reg_sign_bit_copies[REGNO (x)] = 1;
750 return;
753 /* If this is a complex assignment, see if we can convert it into a
754 simple assignment. */
755 set = expand_field_assignment (set);
757 /* If this is a simple assignment, or we have a paradoxical SUBREG,
758 set what we know about X. */
760 if (SET_DEST (set) == x
761 || (GET_CODE (SET_DEST (set)) == SUBREG
762 && (GET_MODE_SIZE (GET_MODE (SET_DEST (set)))
763 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (set)))))
764 && SUBREG_REG (SET_DEST (set)) == x))
766 rtx src = SET_SRC (set);
768 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
769 /* If X is narrower than a word and SRC is a non-negative
770 constant that would appear negative in the mode of X,
771 sign-extend it for use in reg_nonzero_bits because some
772 machines (maybe most) will actually do the sign-extension
773 and this is the conservative approach.
775 ??? For 2.5, try to tighten up the MD files in this regard
776 instead of this kludge. */
778 if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
779 && GET_CODE (src) == CONST_INT
780 && INTVAL (src) > 0
781 && 0 != (INTVAL (src)
782 & ((HOST_WIDE_INT) 1
783 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
784 src = GEN_INT (INTVAL (src)
785 | ((HOST_WIDE_INT) (-1)
786 << GET_MODE_BITSIZE (GET_MODE (x))));
787 #endif
789 reg_nonzero_bits[REGNO (x)]
790 |= nonzero_bits (src, nonzero_bits_mode);
791 num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
792 if (reg_sign_bit_copies[REGNO (x)] == 0
793 || reg_sign_bit_copies[REGNO (x)] > num)
794 reg_sign_bit_copies[REGNO (x)] = num;
796 else
798 reg_nonzero_bits[REGNO (x)] = GET_MODE_MASK (GET_MODE (x));
799 reg_sign_bit_copies[REGNO (x)] = 1;
804 /* See if INSN can be combined into I3. PRED and SUCC are optionally
805 insns that were previously combined into I3 or that will be combined
806 into the merger of INSN and I3.
808 Return 0 if the combination is not allowed for any reason.
810 If the combination is allowed, *PDEST will be set to the single
811 destination of INSN and *PSRC to the single source, and this function
812 will return 1. */
814 static int
815 can_combine_p (insn, i3, pred, succ, pdest, psrc)
816 rtx insn;
817 rtx i3;
818 rtx pred, succ;
819 rtx *pdest, *psrc;
821 int i;
822 rtx set = 0, src, dest;
823 rtx p, link;
824 int all_adjacent = (succ ? (next_active_insn (insn) == succ
825 && next_active_insn (succ) == i3)
826 : next_active_insn (insn) == i3);
828 /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
829 or a PARALLEL consisting of such a SET and CLOBBERs.
831 If INSN has CLOBBER parallel parts, ignore them for our processing.
832 By definition, these happen during the execution of the insn. When it
833 is merged with another insn, all bets are off. If they are, in fact,
834 needed and aren't also supplied in I3, they may be added by
835 recog_for_combine. Otherwise, it won't match.
837 We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
838 note.
840 Get the source and destination of INSN. If more than one, can't
841 combine. */
843 if (GET_CODE (PATTERN (insn)) == SET)
844 set = PATTERN (insn);
845 else if (GET_CODE (PATTERN (insn)) == PARALLEL
846 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
848 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
850 rtx elt = XVECEXP (PATTERN (insn), 0, i);
852 switch (GET_CODE (elt))
854 /* We can ignore CLOBBERs. */
855 case CLOBBER:
856 break;
858 case SET:
859 /* Ignore SETs whose result isn't used but not those that
860 have side-effects. */
861 if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
862 && ! side_effects_p (elt))
863 break;
865 /* If we have already found a SET, this is a second one and
866 so we cannot combine with this insn. */
867 if (set)
868 return 0;
870 set = elt;
871 break;
873 default:
874 /* Anything else means we can't combine. */
875 return 0;
879 if (set == 0
880 /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
881 so don't do anything with it. */
882 || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
883 return 0;
885 else
886 return 0;
888 if (set == 0)
889 return 0;
891 set = expand_field_assignment (set);
892 src = SET_SRC (set), dest = SET_DEST (set);
894 /* Don't eliminate a store in the stack pointer. */
895 if (dest == stack_pointer_rtx
896 /* If we couldn't eliminate a field assignment, we can't combine. */
897 || GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == STRICT_LOW_PART
898 /* Don't combine with an insn that sets a register to itself if it has
899 a REG_EQUAL note. This may be part of a REG_NO_CONFLICT sequence. */
900 || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
901 /* Can't merge a function call. */
902 || GET_CODE (src) == CALL
903 /* Don't eliminate a function call argument. */
904 || (GET_CODE (i3) == CALL_INSN
905 && (find_reg_fusage (i3, USE, dest)
906 || (GET_CODE (dest) == REG
907 && REGNO (dest) < FIRST_PSEUDO_REGISTER
908 && global_regs[REGNO (dest)])))
909 /* Don't substitute into an incremented register. */
910 || FIND_REG_INC_NOTE (i3, dest)
911 || (succ && FIND_REG_INC_NOTE (succ, dest))
912 /* Don't combine the end of a libcall into anything. */
913 || find_reg_note (insn, REG_RETVAL, NULL_RTX)
914 /* Make sure that DEST is not used after SUCC but before I3. */
915 || (succ && ! all_adjacent
916 && reg_used_between_p (dest, succ, i3))
917 /* Make sure that the value that is to be substituted for the register
918 does not use any registers whose values alter in between. However,
919 If the insns are adjacent, a use can't cross a set even though we
920 think it might (this can happen for a sequence of insns each setting
921 the same destination; reg_last_set of that register might point to
922 a NOTE). If INSN has a REG_EQUIV note, the register is always
923 equivalent to the memory so the substitution is valid even if there
924 are intervening stores. Also, don't move a volatile asm or
925 UNSPEC_VOLATILE across any other insns. */
926 || (! all_adjacent
927 && (((GET_CODE (src) != MEM
928 || ! find_reg_note (insn, REG_EQUIV, src))
929 && use_crosses_set_p (src, INSN_CUID (insn)))
930 || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
931 || GET_CODE (src) == UNSPEC_VOLATILE))
932 /* If there is a REG_NO_CONFLICT note for DEST in I3 or SUCC, we get
933 better register allocation by not doing the combine. */
934 || find_reg_note (i3, REG_NO_CONFLICT, dest)
935 || (succ && find_reg_note (succ, REG_NO_CONFLICT, dest))
936 /* Don't combine across a CALL_INSN, because that would possibly
937 change whether the life span of some REGs crosses calls or not,
938 and it is a pain to update that information.
939 Exception: if source is a constant, moving it later can't hurt.
940 Accept that special case, because it helps -fforce-addr a lot. */
941 || (INSN_CUID (insn) < last_call_cuid && ! CONSTANT_P (src)))
942 return 0;
944 /* DEST must either be a REG or CC0. */
945 if (GET_CODE (dest) == REG)
947 /* If register alignment is being enforced for multi-word items in all
948 cases except for parameters, it is possible to have a register copy
949 insn referencing a hard register that is not allowed to contain the
950 mode being copied and which would not be valid as an operand of most
951 insns. Eliminate this problem by not combining with such an insn.
953 Also, on some machines we don't want to extend the life of a hard
954 register.
956 This is the same test done in can_combine except that we don't test
957 if SRC is a CALL operation to permit a hard register with
958 SMALL_REGISTER_CLASSES, and that we have to take all_adjacent
959 into account. */
961 if (GET_CODE (src) == REG
962 && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
963 && ! HARD_REGNO_MODE_OK (REGNO (dest), GET_MODE (dest)))
964 /* Don't extend the life of a hard register unless it is
965 user variable (if we have few registers) or it can't
966 fit into the desired register (meaning something special
967 is going on).
968 Also avoid substituting a return register into I3, because
969 reload can't handle a conflict with constraints of other
970 inputs. */
971 || (REGNO (src) < FIRST_PSEUDO_REGISTER
972 && (! HARD_REGNO_MODE_OK (REGNO (src), GET_MODE (src))
973 #ifdef SMALL_REGISTER_CLASSES
974 || (SMALL_REGISTER_CLASSES
975 && ((! all_adjacent && ! REG_USERVAR_P (src))
976 || (FUNCTION_VALUE_REGNO_P (REGNO (src))
977 && ! REG_USERVAR_P (src))))
978 #endif
979 ))))
980 return 0;
982 else if (GET_CODE (dest) != CC0)
983 return 0;
985 /* Don't substitute for a register intended as a clobberable operand.
986 Similarly, don't substitute an expression containing a register that
987 will be clobbered in I3. */
988 if (GET_CODE (PATTERN (i3)) == PARALLEL)
989 for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
990 if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER
991 && (reg_overlap_mentioned_p (XEXP (XVECEXP (PATTERN (i3), 0, i), 0),
992 src)
993 || rtx_equal_p (XEXP (XVECEXP (PATTERN (i3), 0, i), 0), dest)))
994 return 0;
996 /* If INSN contains anything volatile, or is an `asm' (whether volatile
997 or not), reject, unless nothing volatile comes between it and I3,
998 with the exception of SUCC. */
1000 if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
1001 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1002 if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
1003 && p != succ && volatile_refs_p (PATTERN (p)))
1004 return 0;
1006 /* If INSN is an asm, and DEST is a hard register, reject, since it has
1007 to be an explicit register variable, and was chosen for a reason. */
1009 if (GET_CODE (src) == ASM_OPERANDS
1010 && GET_CODE (dest) == REG && REGNO (dest) < FIRST_PSEUDO_REGISTER)
1011 return 0;
1013 /* If there are any volatile insns between INSN and I3, reject, because
1014 they might affect machine state. */
1016 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1017 if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
1018 && p != succ && volatile_insn_p (PATTERN (p)))
1019 return 0;
1021 /* If INSN or I2 contains an autoincrement or autodecrement,
1022 make sure that register is not used between there and I3,
1023 and not already used in I3 either.
1024 Also insist that I3 not be a jump; if it were one
1025 and the incremented register were spilled, we would lose. */
1027 #ifdef AUTO_INC_DEC
1028 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1029 if (REG_NOTE_KIND (link) == REG_INC
1030 && (GET_CODE (i3) == JUMP_INSN
1031 || reg_used_between_p (XEXP (link, 0), insn, i3)
1032 || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
1033 return 0;
1034 #endif
1036 #ifdef HAVE_cc0
1037 /* Don't combine an insn that follows a CC0-setting insn.
1038 An insn that uses CC0 must not be separated from the one that sets it.
1039 We do, however, allow I2 to follow a CC0-setting insn if that insn
1040 is passed as I1; in that case it will be deleted also.
1041 We also allow combining in this case if all the insns are adjacent
1042 because that would leave the two CC0 insns adjacent as well.
1043 It would be more logical to test whether CC0 occurs inside I1 or I2,
1044 but that would be much slower, and this ought to be equivalent. */
1046 p = prev_nonnote_insn (insn);
1047 if (p && p != pred && GET_CODE (p) == INSN && sets_cc0_p (PATTERN (p))
1048 && ! all_adjacent)
1049 return 0;
1050 #endif
1052 /* If we get here, we have passed all the tests and the combination is
1053 to be allowed. */
1055 *pdest = dest;
1056 *psrc = src;
1058 return 1;
1061 /* LOC is the location within I3 that contains its pattern or the component
1062 of a PARALLEL of the pattern. We validate that it is valid for combining.
1064 One problem is if I3 modifies its output, as opposed to replacing it
1065 entirely, we can't allow the output to contain I2DEST or I1DEST as doing
1066 so would produce an insn that is not equivalent to the original insns.
1068 Consider:
1070 (set (reg:DI 101) (reg:DI 100))
1071 (set (subreg:SI (reg:DI 101) 0) <foo>)
1073 This is NOT equivalent to:
1075 (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
1076 (set (reg:DI 101) (reg:DI 100))])
1078 Not only does this modify 100 (in which case it might still be valid
1079 if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
1081 We can also run into a problem if I2 sets a register that I1
1082 uses and I1 gets directly substituted into I3 (not via I2). In that
1083 case, we would be getting the wrong value of I2DEST into I3, so we
1084 must reject the combination. This case occurs when I2 and I1 both
1085 feed into I3, rather than when I1 feeds into I2, which feeds into I3.
1086 If I1_NOT_IN_SRC is non-zero, it means that finding I1 in the source
1087 of a SET must prevent combination from occurring.
1089 On machines where SMALL_REGISTER_CLASSES is defined, we don't combine
1090 if the destination of a SET is a hard register that isn't a user
1091 variable.
1093 Before doing the above check, we first try to expand a field assignment
1094 into a set of logical operations.
1096 If PI3_DEST_KILLED is non-zero, it is a pointer to a location in which
1097 we place a register that is both set and used within I3. If more than one
1098 such register is detected, we fail.
1100 Return 1 if the combination is valid, zero otherwise. */
1102 static int
1103 combinable_i3pat (i3, loc, i2dest, i1dest, i1_not_in_src, pi3dest_killed)
1104 rtx i3;
1105 rtx *loc;
1106 rtx i2dest;
1107 rtx i1dest;
1108 int i1_not_in_src;
1109 rtx *pi3dest_killed;
1111 rtx x = *loc;
1113 if (GET_CODE (x) == SET)
1115 rtx set = expand_field_assignment (x);
1116 rtx dest = SET_DEST (set);
1117 rtx src = SET_SRC (set);
1118 rtx inner_dest = dest, inner_src = src;
1120 SUBST (*loc, set);
1122 while (GET_CODE (inner_dest) == STRICT_LOW_PART
1123 || GET_CODE (inner_dest) == SUBREG
1124 || GET_CODE (inner_dest) == ZERO_EXTRACT)
1125 inner_dest = XEXP (inner_dest, 0);
1127 /* We probably don't need this any more now that LIMIT_RELOAD_CLASS
1128 was added. */
1129 #if 0
1130 while (GET_CODE (inner_src) == STRICT_LOW_PART
1131 || GET_CODE (inner_src) == SUBREG
1132 || GET_CODE (inner_src) == ZERO_EXTRACT)
1133 inner_src = XEXP (inner_src, 0);
1135 /* If it is better that two different modes keep two different pseudos,
1136 avoid combining them. This avoids producing the following pattern
1137 on a 386:
1138 (set (subreg:SI (reg/v:QI 21) 0)
1139 (lshiftrt:SI (reg/v:SI 20)
1140 (const_int 24)))
1141 If that were made, reload could not handle the pair of
1142 reg 20/21, since it would try to get any GENERAL_REGS
1143 but some of them don't handle QImode. */
1145 if (rtx_equal_p (inner_src, i2dest)
1146 && GET_CODE (inner_dest) == REG
1147 && ! MODES_TIEABLE_P (GET_MODE (i2dest), GET_MODE (inner_dest)))
1148 return 0;
1149 #endif
1151 /* Check for the case where I3 modifies its output, as
1152 discussed above. */
1153 if ((inner_dest != dest
1154 && (reg_overlap_mentioned_p (i2dest, inner_dest)
1155 || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))))
1156 /* This is the same test done in can_combine_p except that we
1157 allow a hard register with SMALL_REGISTER_CLASSES if SRC is a
1158 CALL operation.
1159 Moreover, we can't test all_adjacent; we don't have to, since
1160 this instruction will stay in place, thus we are not considering
1161 to increase the lifetime of INNER_DEST. */
1162 || (GET_CODE (inner_dest) == REG
1163 && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
1164 && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
1165 GET_MODE (inner_dest))
1166 #ifdef SMALL_REGISTER_CLASSES
1167 || (SMALL_REGISTER_CLASSES
1168 && GET_CODE (src) != CALL && ! REG_USERVAR_P (inner_dest)
1169 && FUNCTION_VALUE_REGNO_P (REGNO (inner_dest)))
1170 #endif
1172 || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src)))
1173 return 0;
1175 /* If DEST is used in I3, it is being killed in this insn,
1176 so record that for later.
1177 Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
1178 STACK_POINTER_REGNUM, since these are always considered to be
1179 live. Similarly for ARG_POINTER_REGNUM if it is fixed. */
1180 if (pi3dest_killed && GET_CODE (dest) == REG
1181 && reg_referenced_p (dest, PATTERN (i3))
1182 && REGNO (dest) != FRAME_POINTER_REGNUM
1183 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
1184 && REGNO (dest) != HARD_FRAME_POINTER_REGNUM
1185 #endif
1186 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
1187 && (REGNO (dest) != ARG_POINTER_REGNUM
1188 || ! fixed_regs [REGNO (dest)])
1189 #endif
1190 && REGNO (dest) != STACK_POINTER_REGNUM)
1192 if (*pi3dest_killed)
1193 return 0;
1195 *pi3dest_killed = dest;
1199 else if (GET_CODE (x) == PARALLEL)
1201 int i;
1203 for (i = 0; i < XVECLEN (x, 0); i++)
1204 if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest,
1205 i1_not_in_src, pi3dest_killed))
1206 return 0;
1209 return 1;
1212 /* Try to combine the insns I1 and I2 into I3.
1213 Here I1 and I2 appear earlier than I3.
1214 I1 can be zero; then we combine just I2 into I3.
1216 It we are combining three insns and the resulting insn is not recognized,
1217 try splitting it into two insns. If that happens, I2 and I3 are retained
1218 and I1 is pseudo-deleted by turning it into a NOTE. Otherwise, I1 and I2
1219 are pseudo-deleted.
1221 Return 0 if the combination does not work. Then nothing is changed.
1222 If we did the combination, return the insn at which combine should
1223 resume scanning. */
1225 static rtx
1226 try_combine (i3, i2, i1)
1227 register rtx i3, i2, i1;
1229 /* New patterns for I3 and I3, respectively. */
1230 rtx newpat, newi2pat = 0;
1231 /* Indicates need to preserve SET in I1 or I2 in I3 if it is not dead. */
1232 int added_sets_1, added_sets_2;
1233 /* Total number of SETs to put into I3. */
1234 int total_sets;
1235 /* Nonzero is I2's body now appears in I3. */
1236 int i2_is_used;
1237 /* INSN_CODEs for new I3, new I2, and user of condition code. */
1238 int insn_code_number, i2_code_number, other_code_number;
1239 /* Contains I3 if the destination of I3 is used in its source, which means
1240 that the old life of I3 is being killed. If that usage is placed into
1241 I2 and not in I3, a REG_DEAD note must be made. */
1242 rtx i3dest_killed = 0;
1243 /* SET_DEST and SET_SRC of I2 and I1. */
1244 rtx i2dest, i2src, i1dest = 0, i1src = 0;
1245 /* PATTERN (I2), or a copy of it in certain cases. */
1246 rtx i2pat;
1247 /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC. */
1248 int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
1249 int i1_feeds_i3 = 0;
1250 /* Notes that must be added to REG_NOTES in I3 and I2. */
1251 rtx new_i3_notes, new_i2_notes;
1252 /* Notes that we substituted I3 into I2 instead of the normal case. */
1253 int i3_subst_into_i2 = 0;
1254 /* Notes that I1, I2 or I3 is a MULT operation. */
1255 int have_mult = 0;
1256 /* Number of clobbers of SCRATCH we had to add. */
1257 int i3_scratches = 0, i2_scratches = 0, other_scratches = 0;
1259 int maxreg;
1260 rtx temp;
1261 register rtx link;
1262 int i;
1264 /* If any of I1, I2, and I3 isn't really an insn, we can't do anything.
1265 This can occur when flow deletes an insn that it has merged into an
1266 auto-increment address. We also can't do anything if I3 has a
1267 REG_LIBCALL note since we don't want to disrupt the contiguity of a
1268 libcall. */
1270 if (GET_RTX_CLASS (GET_CODE (i3)) != 'i'
1271 || GET_RTX_CLASS (GET_CODE (i2)) != 'i'
1272 || (i1 && GET_RTX_CLASS (GET_CODE (i1)) != 'i')
1273 || find_reg_note (i3, REG_LIBCALL, NULL_RTX))
1274 return 0;
1276 combine_attempts++;
1278 undobuf.undos = undobuf.previous_undos = 0;
1279 undobuf.other_insn = 0;
1281 /* Save the current high-water-mark so we can free storage if we didn't
1282 accept this combination. */
1283 undobuf.storage = (char *) oballoc (0);
1285 /* Reset the hard register usage information. */
1286 CLEAR_HARD_REG_SET (newpat_used_regs);
1288 /* If I1 and I2 both feed I3, they can be in any order. To simplify the
1289 code below, set I1 to be the earlier of the two insns. */
1290 if (i1 && INSN_CUID (i1) > INSN_CUID (i2))
1291 temp = i1, i1 = i2, i2 = temp;
1293 added_links_insn = 0;
1295 /* First check for one important special-case that the code below will
1296 not handle. Namely, the case where I1 is zero, I2 has multiple sets,
1297 and I3 is a SET whose SET_SRC is a SET_DEST in I2. In that case,
1298 we may be able to replace that destination with the destination of I3.
1299 This occurs in the common code where we compute both a quotient and
1300 remainder into a structure, in which case we want to do the computation
1301 directly into the structure to avoid register-register copies.
1303 We make very conservative checks below and only try to handle the
1304 most common cases of this. For example, we only handle the case
1305 where I2 and I3 are adjacent to avoid making difficult register
1306 usage tests. */
1308 if (i1 == 0 && GET_CODE (i3) == INSN && GET_CODE (PATTERN (i3)) == SET
1309 && GET_CODE (SET_SRC (PATTERN (i3))) == REG
1310 && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
1311 #ifdef SMALL_REGISTER_CLASSES
1312 && (! SMALL_REGISTER_CLASSES
1313 || GET_CODE (SET_DEST (PATTERN (i3))) != REG
1314 || REGNO (SET_DEST (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
1315 || REG_USERVAR_P (SET_DEST (PATTERN (i3))))
1316 #endif
1317 && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
1318 && GET_CODE (PATTERN (i2)) == PARALLEL
1319 && ! side_effects_p (SET_DEST (PATTERN (i3)))
1320 /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
1321 below would need to check what is inside (and reg_overlap_mentioned_p
1322 doesn't support those codes anyway). Don't allow those destinations;
1323 the resulting insn isn't likely to be recognized anyway. */
1324 && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
1325 && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
1326 && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
1327 SET_DEST (PATTERN (i3)))
1328 && next_real_insn (i2) == i3)
1330 rtx p2 = PATTERN (i2);
1332 /* Make sure that the destination of I3,
1333 which we are going to substitute into one output of I2,
1334 is not used within another output of I2. We must avoid making this:
1335 (parallel [(set (mem (reg 69)) ...)
1336 (set (reg 69) ...)])
1337 which is not well-defined as to order of actions.
1338 (Besides, reload can't handle output reloads for this.)
1340 The problem can also happen if the dest of I3 is a memory ref,
1341 if another dest in I2 is an indirect memory ref. */
1342 for (i = 0; i < XVECLEN (p2, 0); i++)
1343 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
1344 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
1345 && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
1346 SET_DEST (XVECEXP (p2, 0, i))))
1347 break;
1349 if (i == XVECLEN (p2, 0))
1350 for (i = 0; i < XVECLEN (p2, 0); i++)
1351 if (SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
1353 combine_merges++;
1355 subst_insn = i3;
1356 subst_low_cuid = INSN_CUID (i2);
1358 added_sets_2 = added_sets_1 = 0;
1359 i2dest = SET_SRC (PATTERN (i3));
1361 /* Replace the dest in I2 with our dest and make the resulting
1362 insn the new pattern for I3. Then skip to where we
1363 validate the pattern. Everything was set up above. */
1364 SUBST (SET_DEST (XVECEXP (p2, 0, i)),
1365 SET_DEST (PATTERN (i3)));
1367 newpat = p2;
1368 i3_subst_into_i2 = 1;
1369 goto validate_replacement;
1373 #ifndef HAVE_cc0
1374 /* If we have no I1 and I2 looks like:
1375 (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
1376 (set Y OP)])
1377 make up a dummy I1 that is
1378 (set Y OP)
1379 and change I2 to be
1380 (set (reg:CC X) (compare:CC Y (const_int 0)))
1382 (We can ignore any trailing CLOBBERs.)
1384 This undoes a previous combination and allows us to match a branch-and-
1385 decrement insn. */
1387 if (i1 == 0 && GET_CODE (PATTERN (i2)) == PARALLEL
1388 && XVECLEN (PATTERN (i2), 0) >= 2
1389 && GET_CODE (XVECEXP (PATTERN (i2), 0, 0)) == SET
1390 && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
1391 == MODE_CC)
1392 && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
1393 && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
1394 && GET_CODE (XVECEXP (PATTERN (i2), 0, 1)) == SET
1395 && GET_CODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 1))) == REG
1396 && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
1397 SET_SRC (XVECEXP (PATTERN (i2), 0, 1))))
1399 for (i = XVECLEN (PATTERN (i2), 0) - 1; i >= 2; i--)
1400 if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != CLOBBER)
1401 break;
1403 if (i == 1)
1405 /* We make I1 with the same INSN_UID as I2. This gives it
1406 the same INSN_CUID for value tracking. Our fake I1 will
1407 never appear in the insn stream so giving it the same INSN_UID
1408 as I2 will not cause a problem. */
1410 subst_prev_insn = i1
1411 = gen_rtx (INSN, VOIDmode, INSN_UID (i2), NULL_RTX, i2,
1412 XVECEXP (PATTERN (i2), 0, 1), -1, NULL_RTX, NULL_RTX);
1414 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
1415 SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
1416 SET_DEST (PATTERN (i1)));
1419 #endif
1421 /* Verify that I2 and I1 are valid for combining. */
1422 if (! can_combine_p (i2, i3, i1, NULL_RTX, &i2dest, &i2src)
1423 || (i1 && ! can_combine_p (i1, i3, NULL_RTX, i2, &i1dest, &i1src)))
1425 undo_all ();
1426 return 0;
1429 /* Record whether I2DEST is used in I2SRC and similarly for the other
1430 cases. Knowing this will help in register status updating below. */
1431 i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
1432 i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
1433 i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
1435 /* See if I1 directly feeds into I3. It does if I1DEST is not used
1436 in I2SRC. */
1437 i1_feeds_i3 = i1 && ! reg_overlap_mentioned_p (i1dest, i2src);
1439 /* Ensure that I3's pattern can be the destination of combines. */
1440 if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest,
1441 i1 && i2dest_in_i1src && i1_feeds_i3,
1442 &i3dest_killed))
1444 undo_all ();
1445 return 0;
1448 /* See if any of the insns is a MULT operation. Unless one is, we will
1449 reject a combination that is, since it must be slower. Be conservative
1450 here. */
1451 if (GET_CODE (i2src) == MULT
1452 || (i1 != 0 && GET_CODE (i1src) == MULT)
1453 || (GET_CODE (PATTERN (i3)) == SET
1454 && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
1455 have_mult = 1;
1457 /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
1458 We used to do this EXCEPT in one case: I3 has a post-inc in an
1459 output operand. However, that exception can give rise to insns like
1460 mov r3,(r3)+
1461 which is a famous insn on the PDP-11 where the value of r3 used as the
1462 source was model-dependent. Avoid this sort of thing. */
1464 #if 0
1465 if (!(GET_CODE (PATTERN (i3)) == SET
1466 && GET_CODE (SET_SRC (PATTERN (i3))) == REG
1467 && GET_CODE (SET_DEST (PATTERN (i3))) == MEM
1468 && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
1469 || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
1470 /* It's not the exception. */
1471 #endif
1472 #ifdef AUTO_INC_DEC
1473 for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
1474 if (REG_NOTE_KIND (link) == REG_INC
1475 && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
1476 || (i1 != 0
1477 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
1479 undo_all ();
1480 return 0;
1482 #endif
1484 /* See if the SETs in I1 or I2 need to be kept around in the merged
1485 instruction: whenever the value set there is still needed past I3.
1486 For the SETs in I2, this is easy: we see if I2DEST dies or is set in I3.
1488 For the SET in I1, we have two cases: If I1 and I2 independently
1489 feed into I3, the set in I1 needs to be kept around if I1DEST dies
1490 or is set in I3. Otherwise (if I1 feeds I2 which feeds I3), the set
1491 in I1 needs to be kept around unless I1DEST dies or is set in either
1492 I2 or I3. We can distinguish these cases by seeing if I2SRC mentions
1493 I1DEST. If so, we know I1 feeds into I2. */
1495 added_sets_2 = ! dead_or_set_p (i3, i2dest);
1497 added_sets_1
1498 = i1 && ! (i1_feeds_i3 ? dead_or_set_p (i3, i1dest)
1499 : (dead_or_set_p (i3, i1dest) || dead_or_set_p (i2, i1dest)));
1501 /* If the set in I2 needs to be kept around, we must make a copy of
1502 PATTERN (I2), so that when we substitute I1SRC for I1DEST in
1503 PATTERN (I2), we are only substituting for the original I1DEST, not into
1504 an already-substituted copy. This also prevents making self-referential
1505 rtx. If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
1506 I2DEST. */
1508 i2pat = (GET_CODE (PATTERN (i2)) == PARALLEL
1509 ? gen_rtx (SET, VOIDmode, i2dest, i2src)
1510 : PATTERN (i2));
1512 if (added_sets_2)
1513 i2pat = copy_rtx (i2pat);
1515 combine_merges++;
1517 /* Substitute in the latest insn for the regs set by the earlier ones. */
1519 maxreg = max_reg_num ();
1521 subst_insn = i3;
1523 /* It is possible that the source of I2 or I1 may be performing an
1524 unneeded operation, such as a ZERO_EXTEND of something that is known
1525 to have the high part zero. Handle that case by letting subst look at
1526 the innermost one of them.
1528 Another way to do this would be to have a function that tries to
1529 simplify a single insn instead of merging two or more insns. We don't
1530 do this because of the potential of infinite loops and because
1531 of the potential extra memory required. However, doing it the way
1532 we are is a bit of a kludge and doesn't catch all cases.
1534 But only do this if -fexpensive-optimizations since it slows things down
1535 and doesn't usually win. */
1537 if (flag_expensive_optimizations)
1539 /* Pass pc_rtx so no substitutions are done, just simplifications.
1540 The cases that we are interested in here do not involve the few
1541 cases were is_replaced is checked. */
1542 if (i1)
1544 subst_low_cuid = INSN_CUID (i1);
1545 i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0);
1547 else
1549 subst_low_cuid = INSN_CUID (i2);
1550 i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0);
1553 undobuf.previous_undos = undobuf.undos;
1556 #ifndef HAVE_cc0
1557 /* Many machines that don't use CC0 have insns that can both perform an
1558 arithmetic operation and set the condition code. These operations will
1559 be represented as a PARALLEL with the first element of the vector
1560 being a COMPARE of an arithmetic operation with the constant zero.
1561 The second element of the vector will set some pseudo to the result
1562 of the same arithmetic operation. If we simplify the COMPARE, we won't
1563 match such a pattern and so will generate an extra insn. Here we test
1564 for this case, where both the comparison and the operation result are
1565 needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
1566 I2SRC. Later we will make the PARALLEL that contains I2. */
1568 if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
1569 && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
1570 && XEXP (SET_SRC (PATTERN (i3)), 1) == const0_rtx
1571 && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
1573 rtx *cc_use;
1574 enum machine_mode compare_mode;
1576 newpat = PATTERN (i3);
1577 SUBST (XEXP (SET_SRC (newpat), 0), i2src);
1579 i2_is_used = 1;
1581 #ifdef EXTRA_CC_MODES
1582 /* See if a COMPARE with the operand we substituted in should be done
1583 with the mode that is currently being used. If not, do the same
1584 processing we do in `subst' for a SET; namely, if the destination
1585 is used only once, try to replace it with a register of the proper
1586 mode and also replace the COMPARE. */
1587 if (undobuf.other_insn == 0
1588 && (cc_use = find_single_use (SET_DEST (newpat), i3,
1589 &undobuf.other_insn))
1590 && ((compare_mode = SELECT_CC_MODE (GET_CODE (*cc_use),
1591 i2src, const0_rtx))
1592 != GET_MODE (SET_DEST (newpat))))
1594 int regno = REGNO (SET_DEST (newpat));
1595 rtx new_dest = gen_rtx (REG, compare_mode, regno);
1597 if (regno < FIRST_PSEUDO_REGISTER
1598 || (REG_N_SETS (regno) == 1 && ! added_sets_2
1599 && ! REG_USERVAR_P (SET_DEST (newpat))))
1601 if (regno >= FIRST_PSEUDO_REGISTER)
1602 SUBST (regno_reg_rtx[regno], new_dest);
1604 SUBST (SET_DEST (newpat), new_dest);
1605 SUBST (XEXP (*cc_use, 0), new_dest);
1606 SUBST (SET_SRC (newpat),
1607 gen_rtx_combine (COMPARE, compare_mode,
1608 i2src, const0_rtx));
1610 else
1611 undobuf.other_insn = 0;
1613 #endif
1615 else
1616 #endif
1618 n_occurrences = 0; /* `subst' counts here */
1620 /* If I1 feeds into I2 (not into I3) and I1DEST is in I1SRC, we
1621 need to make a unique copy of I2SRC each time we substitute it
1622 to avoid self-referential rtl. */
1624 subst_low_cuid = INSN_CUID (i2);
1625 newpat = subst (PATTERN (i3), i2dest, i2src, 0,
1626 ! i1_feeds_i3 && i1dest_in_i1src);
1627 undobuf.previous_undos = undobuf.undos;
1629 /* Record whether i2's body now appears within i3's body. */
1630 i2_is_used = n_occurrences;
1633 /* If we already got a failure, don't try to do more. Otherwise,
1634 try to substitute in I1 if we have it. */
1636 if (i1 && GET_CODE (newpat) != CLOBBER)
1638 /* Before we can do this substitution, we must redo the test done
1639 above (see detailed comments there) that ensures that I1DEST
1640 isn't mentioned in any SETs in NEWPAT that are field assignments. */
1642 if (! combinable_i3pat (NULL_RTX, &newpat, i1dest, NULL_RTX,
1643 0, NULL_PTR))
1645 undo_all ();
1646 return 0;
1649 n_occurrences = 0;
1650 subst_low_cuid = INSN_CUID (i1);
1651 newpat = subst (newpat, i1dest, i1src, 0, 0);
1652 undobuf.previous_undos = undobuf.undos;
1655 /* Fail if an autoincrement side-effect has been duplicated. Be careful
1656 to count all the ways that I2SRC and I1SRC can be used. */
1657 if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
1658 && i2_is_used + added_sets_2 > 1)
1659 || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
1660 && (n_occurrences + added_sets_1 + (added_sets_2 && ! i1_feeds_i3)
1661 > 1))
1662 /* Fail if we tried to make a new register (we used to abort, but there's
1663 really no reason to). */
1664 || max_reg_num () != maxreg
1665 /* Fail if we couldn't do something and have a CLOBBER. */
1666 || GET_CODE (newpat) == CLOBBER
1667 /* Fail if this new pattern is a MULT and we didn't have one before
1668 at the outer level. */
1669 || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
1670 && ! have_mult))
1672 undo_all ();
1673 return 0;
1676 /* If the actions of the earlier insns must be kept
1677 in addition to substituting them into the latest one,
1678 we must make a new PARALLEL for the latest insn
1679 to hold additional the SETs. */
1681 if (added_sets_1 || added_sets_2)
1683 combine_extras++;
1685 if (GET_CODE (newpat) == PARALLEL)
1687 rtvec old = XVEC (newpat, 0);
1688 total_sets = XVECLEN (newpat, 0) + added_sets_1 + added_sets_2;
1689 newpat = gen_rtx (PARALLEL, VOIDmode, rtvec_alloc (total_sets));
1690 bcopy ((char *) &old->elem[0], (char *) XVEC (newpat, 0)->elem,
1691 sizeof (old->elem[0]) * old->num_elem);
1693 else
1695 rtx old = newpat;
1696 total_sets = 1 + added_sets_1 + added_sets_2;
1697 newpat = gen_rtx (PARALLEL, VOIDmode, rtvec_alloc (total_sets));
1698 XVECEXP (newpat, 0, 0) = old;
1701 if (added_sets_1)
1702 XVECEXP (newpat, 0, --total_sets)
1703 = (GET_CODE (PATTERN (i1)) == PARALLEL
1704 ? gen_rtx (SET, VOIDmode, i1dest, i1src) : PATTERN (i1));
1706 if (added_sets_2)
1708 /* If there is no I1, use I2's body as is. We used to also not do
1709 the subst call below if I2 was substituted into I3,
1710 but that could lose a simplification. */
1711 if (i1 == 0)
1712 XVECEXP (newpat, 0, --total_sets) = i2pat;
1713 else
1714 /* See comment where i2pat is assigned. */
1715 XVECEXP (newpat, 0, --total_sets)
1716 = subst (i2pat, i1dest, i1src, 0, 0);
1720 /* We come here when we are replacing a destination in I2 with the
1721 destination of I3. */
1722 validate_replacement:
1724 /* Note which hard regs this insn has as inputs. */
1725 mark_used_regs_combine (newpat);
1727 /* Is the result of combination a valid instruction? */
1728 insn_code_number
1729 = recog_for_combine (&newpat, i3, &new_i3_notes, &i3_scratches);
1731 /* If the result isn't valid, see if it is a PARALLEL of two SETs where
1732 the second SET's destination is a register that is unused. In that case,
1733 we just need the first SET. This can occur when simplifying a divmod
1734 insn. We *must* test for this case here because the code below that
1735 splits two independent SETs doesn't handle this case correctly when it
1736 updates the register status. Also check the case where the first
1737 SET's destination is unused. That would not cause incorrect code, but
1738 does cause an unneeded insn to remain. */
1740 if (insn_code_number < 0 && GET_CODE (newpat) == PARALLEL
1741 && XVECLEN (newpat, 0) == 2
1742 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
1743 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
1744 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == REG
1745 && find_reg_note (i3, REG_UNUSED, SET_DEST (XVECEXP (newpat, 0, 1)))
1746 && ! side_effects_p (SET_SRC (XVECEXP (newpat, 0, 1)))
1747 && asm_noperands (newpat) < 0)
1749 newpat = XVECEXP (newpat, 0, 0);
1750 insn_code_number
1751 = recog_for_combine (&newpat, i3, &new_i3_notes, &i3_scratches);
1754 else if (insn_code_number < 0 && GET_CODE (newpat) == PARALLEL
1755 && XVECLEN (newpat, 0) == 2
1756 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
1757 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
1758 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) == REG
1759 && find_reg_note (i3, REG_UNUSED, SET_DEST (XVECEXP (newpat, 0, 0)))
1760 && ! side_effects_p (SET_SRC (XVECEXP (newpat, 0, 0)))
1761 && asm_noperands (newpat) < 0)
1763 newpat = XVECEXP (newpat, 0, 1);
1764 insn_code_number
1765 = recog_for_combine (&newpat, i3, &new_i3_notes, &i3_scratches);
1768 /* If we were combining three insns and the result is a simple SET
1769 with no ASM_OPERANDS that wasn't recognized, try to split it into two
1770 insns. There are two ways to do this. It can be split using a
1771 machine-specific method (like when you have an addition of a large
1772 constant) or by combine in the function find_split_point. */
1774 if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
1775 && asm_noperands (newpat) < 0)
1777 rtx m_split, *split;
1778 rtx ni2dest = i2dest;
1780 /* See if the MD file can split NEWPAT. If it can't, see if letting it
1781 use I2DEST as a scratch register will help. In the latter case,
1782 convert I2DEST to the mode of the source of NEWPAT if we can. */
1784 m_split = split_insns (newpat, i3);
1786 /* We can only use I2DEST as a scratch reg if it doesn't overlap any
1787 inputs of NEWPAT. */
1789 /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
1790 possible to try that as a scratch reg. This would require adding
1791 more code to make it work though. */
1793 if (m_split == 0 && ! reg_overlap_mentioned_p (ni2dest, newpat))
1795 /* If I2DEST is a hard register or the only use of a pseudo,
1796 we can change its mode. */
1797 if (GET_MODE (SET_DEST (newpat)) != GET_MODE (i2dest)
1798 && GET_MODE (SET_DEST (newpat)) != VOIDmode
1799 && GET_CODE (i2dest) == REG
1800 && (REGNO (i2dest) < FIRST_PSEUDO_REGISTER
1801 || (REG_N_SETS (REGNO (i2dest)) == 1 && ! added_sets_2
1802 && ! REG_USERVAR_P (i2dest))))
1803 ni2dest = gen_rtx (REG, GET_MODE (SET_DEST (newpat)),
1804 REGNO (i2dest));
1806 m_split = split_insns (gen_rtx (PARALLEL, VOIDmode,
1807 gen_rtvec (2, newpat,
1808 gen_rtx (CLOBBER,
1809 VOIDmode,
1810 ni2dest))),
1811 i3);
1814 if (m_split && GET_CODE (m_split) == SEQUENCE
1815 && XVECLEN (m_split, 0) == 2
1816 && (next_real_insn (i2) == i3
1817 || ! use_crosses_set_p (PATTERN (XVECEXP (m_split, 0, 0)),
1818 INSN_CUID (i2))))
1820 rtx i2set, i3set;
1821 rtx newi3pat = PATTERN (XVECEXP (m_split, 0, 1));
1822 newi2pat = PATTERN (XVECEXP (m_split, 0, 0));
1824 i3set = single_set (XVECEXP (m_split, 0, 1));
1825 i2set = single_set (XVECEXP (m_split, 0, 0));
1827 /* In case we changed the mode of I2DEST, replace it in the
1828 pseudo-register table here. We can't do it above in case this
1829 code doesn't get executed and we do a split the other way. */
1831 if (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
1832 SUBST (regno_reg_rtx[REGNO (i2dest)], ni2dest);
1834 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes,
1835 &i2_scratches);
1837 /* If I2 or I3 has multiple SETs, we won't know how to track
1838 register status, so don't use these insns. If I2's destination
1839 is used between I2 and I3, we also can't use these insns. */
1841 if (i2_code_number >= 0 && i2set && i3set
1842 && (next_real_insn (i2) == i3
1843 || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
1844 insn_code_number = recog_for_combine (&newi3pat, i3, &new_i3_notes,
1845 &i3_scratches);
1846 if (insn_code_number >= 0)
1847 newpat = newi3pat;
1849 /* It is possible that both insns now set the destination of I3.
1850 If so, we must show an extra use of it. */
1852 if (insn_code_number >= 0)
1854 rtx new_i3_dest = SET_DEST (i3set);
1855 rtx new_i2_dest = SET_DEST (i2set);
1857 while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
1858 || GET_CODE (new_i3_dest) == STRICT_LOW_PART
1859 || GET_CODE (new_i3_dest) == SUBREG)
1860 new_i3_dest = XEXP (new_i3_dest, 0);
1862 while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
1863 || GET_CODE (new_i2_dest) == STRICT_LOW_PART
1864 || GET_CODE (new_i2_dest) == SUBREG)
1865 new_i2_dest = XEXP (new_i2_dest, 0);
1867 if (GET_CODE (new_i3_dest) == REG
1868 && GET_CODE (new_i2_dest) == REG
1869 && REGNO (new_i3_dest) == REGNO (new_i2_dest))
1870 REG_N_SETS (REGNO (new_i2_dest))++;
1874 /* If we can split it and use I2DEST, go ahead and see if that
1875 helps things be recognized. Verify that none of the registers
1876 are set between I2 and I3. */
1877 if (insn_code_number < 0 && (split = find_split_point (&newpat, i3)) != 0
1878 #ifdef HAVE_cc0
1879 && GET_CODE (i2dest) == REG
1880 #endif
1881 /* We need I2DEST in the proper mode. If it is a hard register
1882 or the only use of a pseudo, we can change its mode. */
1883 && (GET_MODE (*split) == GET_MODE (i2dest)
1884 || GET_MODE (*split) == VOIDmode
1885 || REGNO (i2dest) < FIRST_PSEUDO_REGISTER
1886 || (REG_N_SETS (REGNO (i2dest)) == 1 && ! added_sets_2
1887 && ! REG_USERVAR_P (i2dest)))
1888 && (next_real_insn (i2) == i3
1889 || ! use_crosses_set_p (*split, INSN_CUID (i2)))
1890 /* We can't overwrite I2DEST if its value is still used by
1891 NEWPAT. */
1892 && ! reg_referenced_p (i2dest, newpat))
1894 rtx newdest = i2dest;
1895 enum rtx_code split_code = GET_CODE (*split);
1896 enum machine_mode split_mode = GET_MODE (*split);
1898 /* Get NEWDEST as a register in the proper mode. We have already
1899 validated that we can do this. */
1900 if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
1902 newdest = gen_rtx (REG, split_mode, REGNO (i2dest));
1904 if (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
1905 SUBST (regno_reg_rtx[REGNO (i2dest)], newdest);
1908 /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
1909 an ASHIFT. This can occur if it was inside a PLUS and hence
1910 appeared to be a memory address. This is a kludge. */
1911 if (split_code == MULT
1912 && GET_CODE (XEXP (*split, 1)) == CONST_INT
1913 && (i = exact_log2 (INTVAL (XEXP (*split, 1)))) >= 0)
1915 SUBST (*split, gen_rtx_combine (ASHIFT, split_mode,
1916 XEXP (*split, 0), GEN_INT (i)));
1917 /* Update split_code because we may not have a multiply
1918 anymore. */
1919 split_code = GET_CODE (*split);
1922 #ifdef INSN_SCHEDULING
1923 /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
1924 be written as a ZERO_EXTEND. */
1925 if (split_code == SUBREG && GET_CODE (SUBREG_REG (*split)) == MEM)
1926 SUBST (*split, gen_rtx_combine (ZERO_EXTEND, split_mode,
1927 XEXP (*split, 0)));
1928 #endif
1930 newi2pat = gen_rtx_combine (SET, VOIDmode, newdest, *split);
1931 SUBST (*split, newdest);
1932 i2_code_number
1933 = recog_for_combine (&newi2pat, i2, &new_i2_notes, &i2_scratches);
1935 /* If the split point was a MULT and we didn't have one before,
1936 don't use one now. */
1937 if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
1938 insn_code_number
1939 = recog_for_combine (&newpat, i3, &new_i3_notes, &i3_scratches);
1943 /* Check for a case where we loaded from memory in a narrow mode and
1944 then sign extended it, but we need both registers. In that case,
1945 we have a PARALLEL with both loads from the same memory location.
1946 We can split this into a load from memory followed by a register-register
1947 copy. This saves at least one insn, more if register allocation can
1948 eliminate the copy.
1950 We cannot do this if the destination of the second assignment is
1951 a register that we have already assumed is zero-extended. Similarly
1952 for a SUBREG of such a register. */
1954 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
1955 && GET_CODE (newpat) == PARALLEL
1956 && XVECLEN (newpat, 0) == 2
1957 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
1958 && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
1959 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
1960 && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
1961 XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
1962 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
1963 INSN_CUID (i2))
1964 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
1965 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
1966 && ! (temp = SET_DEST (XVECEXP (newpat, 0, 1)),
1967 (GET_CODE (temp) == REG
1968 && reg_nonzero_bits[REGNO (temp)] != 0
1969 && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
1970 && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
1971 && (reg_nonzero_bits[REGNO (temp)]
1972 != GET_MODE_MASK (word_mode))))
1973 && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
1974 && (temp = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
1975 (GET_CODE (temp) == REG
1976 && reg_nonzero_bits[REGNO (temp)] != 0
1977 && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
1978 && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
1979 && (reg_nonzero_bits[REGNO (temp)]
1980 != GET_MODE_MASK (word_mode)))))
1981 && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
1982 SET_SRC (XVECEXP (newpat, 0, 1)))
1983 && ! find_reg_note (i3, REG_UNUSED,
1984 SET_DEST (XVECEXP (newpat, 0, 0))))
1986 rtx ni2dest;
1988 newi2pat = XVECEXP (newpat, 0, 0);
1989 ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
1990 newpat = XVECEXP (newpat, 0, 1);
1991 SUBST (SET_SRC (newpat),
1992 gen_lowpart_for_combine (GET_MODE (SET_SRC (newpat)), ni2dest));
1993 i2_code_number
1994 = recog_for_combine (&newi2pat, i2, &new_i2_notes, &i2_scratches);
1996 if (i2_code_number >= 0)
1997 insn_code_number
1998 = recog_for_combine (&newpat, i3, &new_i3_notes, &i3_scratches);
2000 if (insn_code_number >= 0)
2002 rtx insn;
2003 rtx link;
2005 /* If we will be able to accept this, we have made a change to the
2006 destination of I3. This can invalidate a LOG_LINKS pointing
2007 to I3. No other part of combine.c makes such a transformation.
2009 The new I3 will have a destination that was previously the
2010 destination of I1 or I2 and which was used in i2 or I3. Call
2011 distribute_links to make a LOG_LINK from the next use of
2012 that destination. */
2014 PATTERN (i3) = newpat;
2015 distribute_links (gen_rtx (INSN_LIST, VOIDmode, i3, NULL_RTX));
2017 /* I3 now uses what used to be its destination and which is
2018 now I2's destination. That means we need a LOG_LINK from
2019 I3 to I2. But we used to have one, so we still will.
2021 However, some later insn might be using I2's dest and have
2022 a LOG_LINK pointing at I3. We must remove this link.
2023 The simplest way to remove the link is to point it at I1,
2024 which we know will be a NOTE. */
2026 for (insn = NEXT_INSN (i3);
2027 insn && (this_basic_block == n_basic_blocks - 1
2028 || insn != basic_block_head[this_basic_block + 1]);
2029 insn = NEXT_INSN (insn))
2031 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
2032 && reg_referenced_p (ni2dest, PATTERN (insn)))
2034 for (link = LOG_LINKS (insn); link;
2035 link = XEXP (link, 1))
2036 if (XEXP (link, 0) == i3)
2037 XEXP (link, 0) = i1;
2039 break;
2045 /* Similarly, check for a case where we have a PARALLEL of two independent
2046 SETs but we started with three insns. In this case, we can do the sets
2047 as two separate insns. This case occurs when some SET allows two
2048 other insns to combine, but the destination of that SET is still live. */
2050 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
2051 && GET_CODE (newpat) == PARALLEL
2052 && XVECLEN (newpat, 0) == 2
2053 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2054 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
2055 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
2056 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2057 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
2058 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
2059 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2060 INSN_CUID (i2))
2061 /* Don't pass sets with (USE (MEM ...)) dests to the following. */
2062 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != USE
2063 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != USE
2064 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
2065 XVECEXP (newpat, 0, 0))
2066 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
2067 XVECEXP (newpat, 0, 1)))
2069 newi2pat = XVECEXP (newpat, 0, 1);
2070 newpat = XVECEXP (newpat, 0, 0);
2072 i2_code_number
2073 = recog_for_combine (&newi2pat, i2, &new_i2_notes, &i2_scratches);
2075 if (i2_code_number >= 0)
2076 insn_code_number
2077 = recog_for_combine (&newpat, i3, &new_i3_notes, &i3_scratches);
2080 /* If it still isn't recognized, fail and change things back the way they
2081 were. */
2082 if ((insn_code_number < 0
2083 /* Is the result a reasonable ASM_OPERANDS? */
2084 && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
2086 undo_all ();
2087 return 0;
2090 /* If we had to change another insn, make sure it is valid also. */
2091 if (undobuf.other_insn)
2093 rtx other_pat = PATTERN (undobuf.other_insn);
2094 rtx new_other_notes;
2095 rtx note, next;
2097 CLEAR_HARD_REG_SET (newpat_used_regs);
2099 other_code_number
2100 = recog_for_combine (&other_pat, undobuf.other_insn,
2101 &new_other_notes, &other_scratches);
2103 if (other_code_number < 0 && ! check_asm_operands (other_pat))
2105 undo_all ();
2106 return 0;
2109 PATTERN (undobuf.other_insn) = other_pat;
2111 /* If any of the notes in OTHER_INSN were REG_UNUSED, ensure that they
2112 are still valid. Then add any non-duplicate notes added by
2113 recog_for_combine. */
2114 for (note = REG_NOTES (undobuf.other_insn); note; note = next)
2116 next = XEXP (note, 1);
2118 if (REG_NOTE_KIND (note) == REG_UNUSED
2119 && ! reg_set_p (XEXP (note, 0), PATTERN (undobuf.other_insn)))
2121 if (GET_CODE (XEXP (note, 0)) == REG)
2122 REG_N_DEATHS (REGNO (XEXP (note, 0)))--;
2124 remove_note (undobuf.other_insn, note);
2128 for (note = new_other_notes; note; note = XEXP (note, 1))
2129 if (GET_CODE (XEXP (note, 0)) == REG)
2130 REG_N_DEATHS (REGNO (XEXP (note, 0)))++;
2132 distribute_notes (new_other_notes, undobuf.other_insn,
2133 undobuf.other_insn, NULL_RTX, NULL_RTX, NULL_RTX);
2136 /* We now know that we can do this combination. Merge the insns and
2137 update the status of registers and LOG_LINKS. */
2140 rtx i3notes, i2notes, i1notes = 0;
2141 rtx i3links, i2links, i1links = 0;
2142 rtx midnotes = 0;
2143 register int regno;
2144 /* Compute which registers we expect to eliminate. newi2pat may be setting
2145 either i3dest or i2dest, so we must check it. */
2146 rtx elim_i2 = ((newi2pat && reg_set_p (i2dest, newi2pat))
2147 || i2dest_in_i2src || i2dest_in_i1src
2148 ? 0 : i2dest);
2149 rtx elim_i1 = (i1 == 0 || i1dest_in_i1src
2150 || (newi2pat && reg_set_p (i1dest, newi2pat))
2151 ? 0 : i1dest);
2153 /* Get the old REG_NOTES and LOG_LINKS from all our insns and
2154 clear them. */
2155 i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
2156 i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
2157 if (i1)
2158 i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
2160 /* Ensure that we do not have something that should not be shared but
2161 occurs multiple times in the new insns. Check this by first
2162 resetting all the `used' flags and then copying anything is shared. */
2164 reset_used_flags (i3notes);
2165 reset_used_flags (i2notes);
2166 reset_used_flags (i1notes);
2167 reset_used_flags (newpat);
2168 reset_used_flags (newi2pat);
2169 if (undobuf.other_insn)
2170 reset_used_flags (PATTERN (undobuf.other_insn));
2172 i3notes = copy_rtx_if_shared (i3notes);
2173 i2notes = copy_rtx_if_shared (i2notes);
2174 i1notes = copy_rtx_if_shared (i1notes);
2175 newpat = copy_rtx_if_shared (newpat);
2176 newi2pat = copy_rtx_if_shared (newi2pat);
2177 if (undobuf.other_insn)
2178 reset_used_flags (PATTERN (undobuf.other_insn));
2180 INSN_CODE (i3) = insn_code_number;
2181 PATTERN (i3) = newpat;
2182 if (undobuf.other_insn)
2183 INSN_CODE (undobuf.other_insn) = other_code_number;
2185 /* We had one special case above where I2 had more than one set and
2186 we replaced a destination of one of those sets with the destination
2187 of I3. In that case, we have to update LOG_LINKS of insns later
2188 in this basic block. Note that this (expensive) case is rare.
2190 Also, in this case, we must pretend that all REG_NOTEs for I2
2191 actually came from I3, so that REG_UNUSED notes from I2 will be
2192 properly handled. */
2194 if (i3_subst_into_i2)
2196 for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
2197 if (GET_CODE (SET_DEST (XVECEXP (PATTERN (i2), 0, i))) == REG
2198 && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
2199 && ! find_reg_note (i2, REG_UNUSED,
2200 SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
2201 for (temp = NEXT_INSN (i2);
2202 temp && (this_basic_block == n_basic_blocks - 1
2203 || basic_block_head[this_basic_block] != temp);
2204 temp = NEXT_INSN (temp))
2205 if (temp != i3 && GET_RTX_CLASS (GET_CODE (temp)) == 'i')
2206 for (link = LOG_LINKS (temp); link; link = XEXP (link, 1))
2207 if (XEXP (link, 0) == i2)
2208 XEXP (link, 0) = i3;
2210 if (i3notes)
2212 rtx link = i3notes;
2213 while (XEXP (link, 1))
2214 link = XEXP (link, 1);
2215 XEXP (link, 1) = i2notes;
2217 else
2218 i3notes = i2notes;
2219 i2notes = 0;
2222 LOG_LINKS (i3) = 0;
2223 REG_NOTES (i3) = 0;
2224 LOG_LINKS (i2) = 0;
2225 REG_NOTES (i2) = 0;
2227 if (newi2pat)
2229 INSN_CODE (i2) = i2_code_number;
2230 PATTERN (i2) = newi2pat;
2232 else
2234 PUT_CODE (i2, NOTE);
2235 NOTE_LINE_NUMBER (i2) = NOTE_INSN_DELETED;
2236 NOTE_SOURCE_FILE (i2) = 0;
2239 if (i1)
2241 LOG_LINKS (i1) = 0;
2242 REG_NOTES (i1) = 0;
2243 PUT_CODE (i1, NOTE);
2244 NOTE_LINE_NUMBER (i1) = NOTE_INSN_DELETED;
2245 NOTE_SOURCE_FILE (i1) = 0;
2248 /* Get death notes for everything that is now used in either I3 or
2249 I2 and used to die in a previous insn. If we built two new
2250 patterns, move from I1 to I2 then I2 to I3 so that we get the
2251 proper movement on registers that I2 modifies. */
2253 if (newi2pat)
2255 move_deaths (newi2pat, NULL_RTX, INSN_CUID (i1), i2, &midnotes);
2256 move_deaths (newpat, newi2pat, INSN_CUID (i1), i3, &midnotes);
2258 else
2259 move_deaths (newpat, NULL_RTX, i1 ? INSN_CUID (i1) : INSN_CUID (i2),
2260 i3, &midnotes);
2262 /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3. */
2263 if (i3notes)
2264 distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL_RTX,
2265 elim_i2, elim_i1);
2266 if (i2notes)
2267 distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL_RTX,
2268 elim_i2, elim_i1);
2269 if (i1notes)
2270 distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL_RTX,
2271 elim_i2, elim_i1);
2272 if (midnotes)
2273 distribute_notes (midnotes, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
2274 elim_i2, elim_i1);
2276 /* Distribute any notes added to I2 or I3 by recog_for_combine. We
2277 know these are REG_UNUSED and want them to go to the desired insn,
2278 so we always pass it as i3. We have not counted the notes in
2279 reg_n_deaths yet, so we need to do so now. */
2281 if (newi2pat && new_i2_notes)
2283 for (temp = new_i2_notes; temp; temp = XEXP (temp, 1))
2284 if (GET_CODE (XEXP (temp, 0)) == REG)
2285 REG_N_DEATHS (REGNO (XEXP (temp, 0)))++;
2287 distribute_notes (new_i2_notes, i2, i2, NULL_RTX, NULL_RTX, NULL_RTX);
2290 if (new_i3_notes)
2292 for (temp = new_i3_notes; temp; temp = XEXP (temp, 1))
2293 if (GET_CODE (XEXP (temp, 0)) == REG)
2294 REG_N_DEATHS (REGNO (XEXP (temp, 0)))++;
2296 distribute_notes (new_i3_notes, i3, i3, NULL_RTX, NULL_RTX, NULL_RTX);
2299 /* If I3DEST was used in I3SRC, it really died in I3. We may need to
2300 put a REG_DEAD note for it somewhere. Similarly for I2 and I1.
2301 Show an additional death due to the REG_DEAD note we make here. If
2302 we discard it in distribute_notes, we will decrement it again. */
2304 if (i3dest_killed)
2306 if (GET_CODE (i3dest_killed) == REG)
2307 REG_N_DEATHS (REGNO (i3dest_killed))++;
2309 distribute_notes (gen_rtx (EXPR_LIST, REG_DEAD, i3dest_killed,
2310 NULL_RTX),
2311 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
2312 elim_i2, elim_i1);
2315 /* For I2 and I1, we have to be careful. If NEWI2PAT exists and sets
2316 I2DEST or I1DEST, the death must be somewhere before I2, not I3. If
2317 we passed I3 in that case, it might delete I2. */
2319 if (i2dest_in_i2src)
2321 if (GET_CODE (i2dest) == REG)
2322 REG_N_DEATHS (REGNO (i2dest))++;
2324 if (newi2pat && reg_set_p (i2dest, newi2pat))
2325 distribute_notes (gen_rtx (EXPR_LIST, REG_DEAD, i2dest, NULL_RTX),
2326 NULL_RTX, i2, NULL_RTX, NULL_RTX, NULL_RTX);
2327 else
2328 distribute_notes (gen_rtx (EXPR_LIST, REG_DEAD, i2dest, NULL_RTX),
2329 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
2330 NULL_RTX, NULL_RTX);
2333 if (i1dest_in_i1src)
2335 if (GET_CODE (i1dest) == REG)
2336 REG_N_DEATHS (REGNO (i1dest))++;
2338 if (newi2pat && reg_set_p (i1dest, newi2pat))
2339 distribute_notes (gen_rtx (EXPR_LIST, REG_DEAD, i1dest, NULL_RTX),
2340 NULL_RTX, i2, NULL_RTX, NULL_RTX, NULL_RTX);
2341 else
2342 distribute_notes (gen_rtx (EXPR_LIST, REG_DEAD, i1dest, NULL_RTX),
2343 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
2344 NULL_RTX, NULL_RTX);
2347 distribute_links (i3links);
2348 distribute_links (i2links);
2349 distribute_links (i1links);
2351 if (GET_CODE (i2dest) == REG)
2353 rtx link;
2354 rtx i2_insn = 0, i2_val = 0, set;
2356 /* The insn that used to set this register doesn't exist, and
2357 this life of the register may not exist either. See if one of
2358 I3's links points to an insn that sets I2DEST. If it does,
2359 that is now the last known value for I2DEST. If we don't update
2360 this and I2 set the register to a value that depended on its old
2361 contents, we will get confused. If this insn is used, thing
2362 will be set correctly in combine_instructions. */
2364 for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
2365 if ((set = single_set (XEXP (link, 0))) != 0
2366 && rtx_equal_p (i2dest, SET_DEST (set)))
2367 i2_insn = XEXP (link, 0), i2_val = SET_SRC (set);
2369 record_value_for_reg (i2dest, i2_insn, i2_val);
2371 /* If the reg formerly set in I2 died only once and that was in I3,
2372 zero its use count so it won't make `reload' do any work. */
2373 if (! added_sets_2
2374 && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
2375 && ! i2dest_in_i2src)
2377 regno = REGNO (i2dest);
2378 REG_N_SETS (regno)--;
2379 if (REG_N_SETS (regno) == 0
2380 && ! REGNO_REG_SET_P (basic_block_live_at_start[0], regno))
2381 REG_N_REFS (regno) = 0;
2385 if (i1 && GET_CODE (i1dest) == REG)
2387 rtx link;
2388 rtx i1_insn = 0, i1_val = 0, set;
2390 for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
2391 if ((set = single_set (XEXP (link, 0))) != 0
2392 && rtx_equal_p (i1dest, SET_DEST (set)))
2393 i1_insn = XEXP (link, 0), i1_val = SET_SRC (set);
2395 record_value_for_reg (i1dest, i1_insn, i1_val);
2397 regno = REGNO (i1dest);
2398 if (! added_sets_1 && ! i1dest_in_i1src)
2400 REG_N_SETS (regno)--;
2401 if (REG_N_SETS (regno) == 0
2402 && ! REGNO_REG_SET_P (basic_block_live_at_start[0], regno))
2403 REG_N_REFS (regno) = 0;
2407 /* Update reg_nonzero_bits et al for any changes that may have been made
2408 to this insn. */
2410 note_stores (newpat, set_nonzero_bits_and_sign_copies);
2411 if (newi2pat)
2412 note_stores (newi2pat, set_nonzero_bits_and_sign_copies);
2414 /* If we added any (clobber (scratch)), add them to the max for a
2415 block. This is a very pessimistic calculation, since we might
2416 have had them already and this might not be the worst block, but
2417 it's not worth doing any better. */
2418 max_scratch += i3_scratches + i2_scratches + other_scratches;
2420 /* If I3 is now an unconditional jump, ensure that it has a
2421 BARRIER following it since it may have initially been a
2422 conditional jump. It may also be the last nonnote insn. */
2424 if ((GET_CODE (newpat) == RETURN || simplejump_p (i3))
2425 && ((temp = next_nonnote_insn (i3)) == NULL_RTX
2426 || GET_CODE (temp) != BARRIER))
2427 emit_barrier_after (i3);
2430 combine_successes++;
2432 /* Clear this here, so that subsequent get_last_value calls are not
2433 affected. */
2434 subst_prev_insn = NULL_RTX;
2436 if (added_links_insn
2437 && (newi2pat == 0 || INSN_CUID (added_links_insn) < INSN_CUID (i2))
2438 && INSN_CUID (added_links_insn) < INSN_CUID (i3))
2439 return added_links_insn;
2440 else
2441 return newi2pat ? i2 : i3;
2444 /* Undo all the modifications recorded in undobuf. */
2446 static void
2447 undo_all ()
2449 struct undo *undo, *next;
2451 for (undo = undobuf.undos; undo; undo = next)
2453 next = undo->next;
2454 if (undo->is_int)
2455 *undo->where.i = undo->old_contents.i;
2456 else
2457 *undo->where.r = undo->old_contents.r;
2459 undo->next = undobuf.frees;
2460 undobuf.frees = undo;
2463 obfree (undobuf.storage);
2464 undobuf.undos = undobuf.previous_undos = 0;
2466 /* Clear this here, so that subsequent get_last_value calls are not
2467 affected. */
2468 subst_prev_insn = NULL_RTX;
2471 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
2472 where we have an arithmetic expression and return that point. LOC will
2473 be inside INSN.
2475 try_combine will call this function to see if an insn can be split into
2476 two insns. */
2478 static rtx *
2479 find_split_point (loc, insn)
2480 rtx *loc;
2481 rtx insn;
2483 rtx x = *loc;
2484 enum rtx_code code = GET_CODE (x);
2485 rtx *split;
2486 int len = 0, pos, unsignedp;
2487 rtx inner;
2489 /* First special-case some codes. */
2490 switch (code)
2492 case SUBREG:
2493 #ifdef INSN_SCHEDULING
2494 /* If we are making a paradoxical SUBREG invalid, it becomes a split
2495 point. */
2496 if (GET_CODE (SUBREG_REG (x)) == MEM)
2497 return loc;
2498 #endif
2499 return find_split_point (&SUBREG_REG (x), insn);
2501 case MEM:
2502 #ifdef HAVE_lo_sum
2503 /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
2504 using LO_SUM and HIGH. */
2505 if (GET_CODE (XEXP (x, 0)) == CONST
2506 || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
2508 SUBST (XEXP (x, 0),
2509 gen_rtx_combine (LO_SUM, Pmode,
2510 gen_rtx_combine (HIGH, Pmode, XEXP (x, 0)),
2511 XEXP (x, 0)));
2512 return &XEXP (XEXP (x, 0), 0);
2514 #endif
2516 /* If we have a PLUS whose second operand is a constant and the
2517 address is not valid, perhaps will can split it up using
2518 the machine-specific way to split large constants. We use
2519 the first pseudo-reg (one of the virtual regs) as a placeholder;
2520 it will not remain in the result. */
2521 if (GET_CODE (XEXP (x, 0)) == PLUS
2522 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
2523 && ! memory_address_p (GET_MODE (x), XEXP (x, 0)))
2525 rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
2526 rtx seq = split_insns (gen_rtx (SET, VOIDmode, reg, XEXP (x, 0)),
2527 subst_insn);
2529 /* This should have produced two insns, each of which sets our
2530 placeholder. If the source of the second is a valid address,
2531 we can make put both sources together and make a split point
2532 in the middle. */
2534 if (seq && XVECLEN (seq, 0) == 2
2535 && GET_CODE (XVECEXP (seq, 0, 0)) == INSN
2536 && GET_CODE (PATTERN (XVECEXP (seq, 0, 0))) == SET
2537 && SET_DEST (PATTERN (XVECEXP (seq, 0, 0))) == reg
2538 && ! reg_mentioned_p (reg,
2539 SET_SRC (PATTERN (XVECEXP (seq, 0, 0))))
2540 && GET_CODE (XVECEXP (seq, 0, 1)) == INSN
2541 && GET_CODE (PATTERN (XVECEXP (seq, 0, 1))) == SET
2542 && SET_DEST (PATTERN (XVECEXP (seq, 0, 1))) == reg
2543 && memory_address_p (GET_MODE (x),
2544 SET_SRC (PATTERN (XVECEXP (seq, 0, 1)))))
2546 rtx src1 = SET_SRC (PATTERN (XVECEXP (seq, 0, 0)));
2547 rtx src2 = SET_SRC (PATTERN (XVECEXP (seq, 0, 1)));
2549 /* Replace the placeholder in SRC2 with SRC1. If we can
2550 find where in SRC2 it was placed, that can become our
2551 split point and we can replace this address with SRC2.
2552 Just try two obvious places. */
2554 src2 = replace_rtx (src2, reg, src1);
2555 split = 0;
2556 if (XEXP (src2, 0) == src1)
2557 split = &XEXP (src2, 0);
2558 else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
2559 && XEXP (XEXP (src2, 0), 0) == src1)
2560 split = &XEXP (XEXP (src2, 0), 0);
2562 if (split)
2564 SUBST (XEXP (x, 0), src2);
2565 return split;
2569 /* If that didn't work, perhaps the first operand is complex and
2570 needs to be computed separately, so make a split point there.
2571 This will occur on machines that just support REG + CONST
2572 and have a constant moved through some previous computation. */
2574 else if (GET_RTX_CLASS (GET_CODE (XEXP (XEXP (x, 0), 0))) != 'o'
2575 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
2576 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (XEXP (x, 0), 0))))
2577 == 'o')))
2578 return &XEXP (XEXP (x, 0), 0);
2580 break;
2582 case SET:
2583 #ifdef HAVE_cc0
2584 /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
2585 ZERO_EXTRACT, the most likely reason why this doesn't match is that
2586 we need to put the operand into a register. So split at that
2587 point. */
2589 if (SET_DEST (x) == cc0_rtx
2590 && GET_CODE (SET_SRC (x)) != COMPARE
2591 && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
2592 && GET_RTX_CLASS (GET_CODE (SET_SRC (x))) != 'o'
2593 && ! (GET_CODE (SET_SRC (x)) == SUBREG
2594 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (SET_SRC (x)))) == 'o'))
2595 return &SET_SRC (x);
2596 #endif
2598 /* See if we can split SET_SRC as it stands. */
2599 split = find_split_point (&SET_SRC (x), insn);
2600 if (split && split != &SET_SRC (x))
2601 return split;
2603 /* See if we can split SET_DEST as it stands. */
2604 split = find_split_point (&SET_DEST (x), insn);
2605 if (split && split != &SET_DEST (x))
2606 return split;
2608 /* See if this is a bitfield assignment with everything constant. If
2609 so, this is an IOR of an AND, so split it into that. */
2610 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
2611 && (GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)))
2612 <= HOST_BITS_PER_WIDE_INT)
2613 && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT
2614 && GET_CODE (XEXP (SET_DEST (x), 2)) == CONST_INT
2615 && GET_CODE (SET_SRC (x)) == CONST_INT
2616 && ((INTVAL (XEXP (SET_DEST (x), 1))
2617 + INTVAL (XEXP (SET_DEST (x), 2)))
2618 <= GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0))))
2619 && ! side_effects_p (XEXP (SET_DEST (x), 0)))
2621 int pos = INTVAL (XEXP (SET_DEST (x), 2));
2622 int len = INTVAL (XEXP (SET_DEST (x), 1));
2623 int src = INTVAL (SET_SRC (x));
2624 rtx dest = XEXP (SET_DEST (x), 0);
2625 enum machine_mode mode = GET_MODE (dest);
2626 unsigned HOST_WIDE_INT mask = ((HOST_WIDE_INT) 1 << len) - 1;
2628 if (BITS_BIG_ENDIAN)
2629 pos = GET_MODE_BITSIZE (mode) - len - pos;
2631 if (src == mask)
2632 SUBST (SET_SRC (x),
2633 gen_binary (IOR, mode, dest, GEN_INT (src << pos)));
2634 else
2635 SUBST (SET_SRC (x),
2636 gen_binary (IOR, mode,
2637 gen_binary (AND, mode, dest,
2638 GEN_INT (~ (mask << pos)
2639 & GET_MODE_MASK (mode))),
2640 GEN_INT (src << pos)));
2642 SUBST (SET_DEST (x), dest);
2644 split = find_split_point (&SET_SRC (x), insn);
2645 if (split && split != &SET_SRC (x))
2646 return split;
2649 /* Otherwise, see if this is an operation that we can split into two.
2650 If so, try to split that. */
2651 code = GET_CODE (SET_SRC (x));
2653 switch (code)
2655 case AND:
2656 /* If we are AND'ing with a large constant that is only a single
2657 bit and the result is only being used in a context where we
2658 need to know if it is zero or non-zero, replace it with a bit
2659 extraction. This will avoid the large constant, which might
2660 have taken more than one insn to make. If the constant were
2661 not a valid argument to the AND but took only one insn to make,
2662 this is no worse, but if it took more than one insn, it will
2663 be better. */
2665 if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
2666 && GET_CODE (XEXP (SET_SRC (x), 0)) == REG
2667 && (pos = exact_log2 (INTVAL (XEXP (SET_SRC (x), 1)))) >= 7
2668 && GET_CODE (SET_DEST (x)) == REG
2669 && (split = find_single_use (SET_DEST (x), insn, NULL_PTR)) != 0
2670 && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
2671 && XEXP (*split, 0) == SET_DEST (x)
2672 && XEXP (*split, 1) == const0_rtx)
2674 rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
2675 XEXP (SET_SRC (x), 0),
2676 pos, NULL_RTX, 1, 1, 0, 0);
2677 if (extraction != 0)
2679 SUBST (SET_SRC (x), extraction);
2680 return find_split_point (loc, insn);
2683 break;
2685 case NE:
2686 /* if STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
2687 is known to be on, this can be converted into a NEG of a shift. */
2688 if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
2689 && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
2690 && 1 <= (pos = exact_log2
2691 (nonzero_bits (XEXP (SET_SRC (x), 0),
2692 GET_MODE (XEXP (SET_SRC (x), 0))))))
2694 enum machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
2696 SUBST (SET_SRC (x),
2697 gen_rtx_combine (NEG, mode,
2698 gen_rtx_combine (LSHIFTRT, mode,
2699 XEXP (SET_SRC (x), 0),
2700 GEN_INT (pos))));
2702 split = find_split_point (&SET_SRC (x), insn);
2703 if (split && split != &SET_SRC (x))
2704 return split;
2706 break;
2708 case SIGN_EXTEND:
2709 inner = XEXP (SET_SRC (x), 0);
2711 /* We can't optimize if either mode is a partial integer
2712 mode as we don't know how many bits are significant
2713 in those modes. */
2714 if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
2715 || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
2716 break;
2718 pos = 0;
2719 len = GET_MODE_BITSIZE (GET_MODE (inner));
2720 unsignedp = 0;
2721 break;
2723 case SIGN_EXTRACT:
2724 case ZERO_EXTRACT:
2725 if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
2726 && GET_CODE (XEXP (SET_SRC (x), 2)) == CONST_INT)
2728 inner = XEXP (SET_SRC (x), 0);
2729 len = INTVAL (XEXP (SET_SRC (x), 1));
2730 pos = INTVAL (XEXP (SET_SRC (x), 2));
2732 if (BITS_BIG_ENDIAN)
2733 pos = GET_MODE_BITSIZE (GET_MODE (inner)) - len - pos;
2734 unsignedp = (code == ZERO_EXTRACT);
2736 break;
2739 if (len && pos >= 0 && pos + len <= GET_MODE_BITSIZE (GET_MODE (inner)))
2741 enum machine_mode mode = GET_MODE (SET_SRC (x));
2743 /* For unsigned, we have a choice of a shift followed by an
2744 AND or two shifts. Use two shifts for field sizes where the
2745 constant might be too large. We assume here that we can
2746 always at least get 8-bit constants in an AND insn, which is
2747 true for every current RISC. */
2749 if (unsignedp && len <= 8)
2751 SUBST (SET_SRC (x),
2752 gen_rtx_combine
2753 (AND, mode,
2754 gen_rtx_combine (LSHIFTRT, mode,
2755 gen_lowpart_for_combine (mode, inner),
2756 GEN_INT (pos)),
2757 GEN_INT (((HOST_WIDE_INT) 1 << len) - 1)));
2759 split = find_split_point (&SET_SRC (x), insn);
2760 if (split && split != &SET_SRC (x))
2761 return split;
2763 else
2765 SUBST (SET_SRC (x),
2766 gen_rtx_combine
2767 (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
2768 gen_rtx_combine (ASHIFT, mode,
2769 gen_lowpart_for_combine (mode, inner),
2770 GEN_INT (GET_MODE_BITSIZE (mode)
2771 - len - pos)),
2772 GEN_INT (GET_MODE_BITSIZE (mode) - len)));
2774 split = find_split_point (&SET_SRC (x), insn);
2775 if (split && split != &SET_SRC (x))
2776 return split;
2780 /* See if this is a simple operation with a constant as the second
2781 operand. It might be that this constant is out of range and hence
2782 could be used as a split point. */
2783 if ((GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '2'
2784 || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == 'c'
2785 || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '<')
2786 && CONSTANT_P (XEXP (SET_SRC (x), 1))
2787 && (GET_RTX_CLASS (GET_CODE (XEXP (SET_SRC (x), 0))) == 'o'
2788 || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
2789 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (SET_SRC (x), 0))))
2790 == 'o'))))
2791 return &XEXP (SET_SRC (x), 1);
2793 /* Finally, see if this is a simple operation with its first operand
2794 not in a register. The operation might require this operand in a
2795 register, so return it as a split point. We can always do this
2796 because if the first operand were another operation, we would have
2797 already found it as a split point. */
2798 if ((GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '2'
2799 || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == 'c'
2800 || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '<'
2801 || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '1')
2802 && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
2803 return &XEXP (SET_SRC (x), 0);
2805 return 0;
2807 case AND:
2808 case IOR:
2809 /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
2810 it is better to write this as (not (ior A B)) so we can split it.
2811 Similarly for IOR. */
2812 if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
2814 SUBST (*loc,
2815 gen_rtx_combine (NOT, GET_MODE (x),
2816 gen_rtx_combine (code == IOR ? AND : IOR,
2817 GET_MODE (x),
2818 XEXP (XEXP (x, 0), 0),
2819 XEXP (XEXP (x, 1), 0))));
2820 return find_split_point (loc, insn);
2823 /* Many RISC machines have a large set of logical insns. If the
2824 second operand is a NOT, put it first so we will try to split the
2825 other operand first. */
2826 if (GET_CODE (XEXP (x, 1)) == NOT)
2828 rtx tem = XEXP (x, 0);
2829 SUBST (XEXP (x, 0), XEXP (x, 1));
2830 SUBST (XEXP (x, 1), tem);
2832 break;
2835 /* Otherwise, select our actions depending on our rtx class. */
2836 switch (GET_RTX_CLASS (code))
2838 case 'b': /* This is ZERO_EXTRACT and SIGN_EXTRACT. */
2839 case '3':
2840 split = find_split_point (&XEXP (x, 2), insn);
2841 if (split)
2842 return split;
2843 /* ... fall through ... */
2844 case '2':
2845 case 'c':
2846 case '<':
2847 split = find_split_point (&XEXP (x, 1), insn);
2848 if (split)
2849 return split;
2850 /* ... fall through ... */
2851 case '1':
2852 /* Some machines have (and (shift ...) ...) insns. If X is not
2853 an AND, but XEXP (X, 0) is, use it as our split point. */
2854 if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
2855 return &XEXP (x, 0);
2857 split = find_split_point (&XEXP (x, 0), insn);
2858 if (split)
2859 return split;
2860 return loc;
2863 /* Otherwise, we don't have a split point. */
2864 return 0;
2867 /* Throughout X, replace FROM with TO, and return the result.
2868 The result is TO if X is FROM;
2869 otherwise the result is X, but its contents may have been modified.
2870 If they were modified, a record was made in undobuf so that
2871 undo_all will (among other things) return X to its original state.
2873 If the number of changes necessary is too much to record to undo,
2874 the excess changes are not made, so the result is invalid.
2875 The changes already made can still be undone.
2876 undobuf.num_undo is incremented for such changes, so by testing that
2877 the caller can tell whether the result is valid.
2879 `n_occurrences' is incremented each time FROM is replaced.
2881 IN_DEST is non-zero if we are processing the SET_DEST of a SET.
2883 UNIQUE_COPY is non-zero if each substitution must be unique. We do this
2884 by copying if `n_occurrences' is non-zero. */
2886 static rtx
2887 subst (x, from, to, in_dest, unique_copy)
2888 register rtx x, from, to;
2889 int in_dest;
2890 int unique_copy;
2892 register enum rtx_code code = GET_CODE (x);
2893 enum machine_mode op0_mode = VOIDmode;
2894 register char *fmt;
2895 register int len, i;
2896 rtx new;
2898 /* Two expressions are equal if they are identical copies of a shared
2899 RTX or if they are both registers with the same register number
2900 and mode. */
2902 #define COMBINE_RTX_EQUAL_P(X,Y) \
2903 ((X) == (Y) \
2904 || (GET_CODE (X) == REG && GET_CODE (Y) == REG \
2905 && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
2907 if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
2909 n_occurrences++;
2910 return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
2913 /* If X and FROM are the same register but different modes, they will
2914 not have been seen as equal above. However, flow.c will make a
2915 LOG_LINKS entry for that case. If we do nothing, we will try to
2916 rerecognize our original insn and, when it succeeds, we will
2917 delete the feeding insn, which is incorrect.
2919 So force this insn not to match in this (rare) case. */
2920 if (! in_dest && code == REG && GET_CODE (from) == REG
2921 && REGNO (x) == REGNO (from))
2922 return gen_rtx (CLOBBER, GET_MODE (x), const0_rtx);
2924 /* If this is an object, we are done unless it is a MEM or LO_SUM, both
2925 of which may contain things that can be combined. */
2926 if (code != MEM && code != LO_SUM && GET_RTX_CLASS (code) == 'o')
2927 return x;
2929 /* It is possible to have a subexpression appear twice in the insn.
2930 Suppose that FROM is a register that appears within TO.
2931 Then, after that subexpression has been scanned once by `subst',
2932 the second time it is scanned, TO may be found. If we were
2933 to scan TO here, we would find FROM within it and create a
2934 self-referent rtl structure which is completely wrong. */
2935 if (COMBINE_RTX_EQUAL_P (x, to))
2936 return to;
2938 len = GET_RTX_LENGTH (code);
2939 fmt = GET_RTX_FORMAT (code);
2941 /* We don't need to process a SET_DEST that is a register, CC0, or PC, so
2942 set up to skip this common case. All other cases where we want to
2943 suppress replacing something inside a SET_SRC are handled via the
2944 IN_DEST operand. */
2945 if (code == SET
2946 && (GET_CODE (SET_DEST (x)) == REG
2947 || GET_CODE (SET_DEST (x)) == CC0
2948 || GET_CODE (SET_DEST (x)) == PC))
2949 fmt = "ie";
2951 /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
2952 constant. */
2953 if (fmt[0] == 'e')
2954 op0_mode = GET_MODE (XEXP (x, 0));
2956 for (i = 0; i < len; i++)
2958 if (fmt[i] == 'E')
2960 register int j;
2961 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2963 if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
2965 new = (unique_copy && n_occurrences ? copy_rtx (to) : to);
2966 n_occurrences++;
2968 else
2970 new = subst (XVECEXP (x, i, j), from, to, 0, unique_copy);
2972 /* If this substitution failed, this whole thing fails. */
2973 if (GET_CODE (new) == CLOBBER && XEXP (new, 0) == const0_rtx)
2974 return new;
2977 SUBST (XVECEXP (x, i, j), new);
2980 else if (fmt[i] == 'e')
2982 if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
2984 /* In general, don't install a subreg involving two modes not
2985 tieable. It can worsen register allocation, and can even
2986 make invalid reload insns, since the reg inside may need to
2987 be copied from in the outside mode, and that may be invalid
2988 if it is an fp reg copied in integer mode.
2990 We allow two exceptions to this: It is valid if it is inside
2991 another SUBREG and the mode of that SUBREG and the mode of
2992 the inside of TO is tieable and it is valid if X is a SET
2993 that copies FROM to CC0. */
2994 if (GET_CODE (to) == SUBREG
2995 && ! MODES_TIEABLE_P (GET_MODE (to),
2996 GET_MODE (SUBREG_REG (to)))
2997 && ! (code == SUBREG
2998 && MODES_TIEABLE_P (GET_MODE (x),
2999 GET_MODE (SUBREG_REG (to))))
3000 #ifdef HAVE_cc0
3001 && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
3002 #endif
3004 return gen_rtx (CLOBBER, VOIDmode, const0_rtx);
3006 new = (unique_copy && n_occurrences ? copy_rtx (to) : to);
3007 n_occurrences++;
3009 else
3010 /* If we are in a SET_DEST, suppress most cases unless we
3011 have gone inside a MEM, in which case we want to
3012 simplify the address. We assume here that things that
3013 are actually part of the destination have their inner
3014 parts in the first expression. This is true for SUBREG,
3015 STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
3016 things aside from REG and MEM that should appear in a
3017 SET_DEST. */
3018 new = subst (XEXP (x, i), from, to,
3019 (((in_dest
3020 && (code == SUBREG || code == STRICT_LOW_PART
3021 || code == ZERO_EXTRACT))
3022 || code == SET)
3023 && i == 0), unique_copy);
3025 /* If we found that we will have to reject this combination,
3026 indicate that by returning the CLOBBER ourselves, rather than
3027 an expression containing it. This will speed things up as
3028 well as prevent accidents where two CLOBBERs are considered
3029 to be equal, thus producing an incorrect simplification. */
3031 if (GET_CODE (new) == CLOBBER && XEXP (new, 0) == const0_rtx)
3032 return new;
3034 SUBST (XEXP (x, i), new);
3038 /* Try to simplify X. If the simplification changed the code, it is likely
3039 that further simplification will help, so loop, but limit the number
3040 of repetitions that will be performed. */
3042 for (i = 0; i < 4; i++)
3044 /* If X is sufficiently simple, don't bother trying to do anything
3045 with it. */
3046 if (code != CONST_INT && code != REG && code != CLOBBER)
3047 x = simplify_rtx (x, op0_mode, i == 3, in_dest);
3049 if (GET_CODE (x) == code)
3050 break;
3052 code = GET_CODE (x);
3054 /* We no longer know the original mode of operand 0 since we
3055 have changed the form of X) */
3056 op0_mode = VOIDmode;
3059 return x;
3062 /* Simplify X, a piece of RTL. We just operate on the expression at the
3063 outer level; call `subst' to simplify recursively. Return the new
3064 expression.
3066 OP0_MODE is the original mode of XEXP (x, 0); LAST is nonzero if this
3067 will be the iteration even if an expression with a code different from
3068 X is returned; IN_DEST is nonzero if we are inside a SET_DEST. */
3070 static rtx
3071 simplify_rtx (x, op0_mode, last, in_dest)
3072 rtx x;
3073 enum machine_mode op0_mode;
3074 int last;
3075 int in_dest;
3077 enum rtx_code code = GET_CODE (x);
3078 enum machine_mode mode = GET_MODE (x);
3079 rtx temp;
3080 int i;
3082 /* If this is a commutative operation, put a constant last and a complex
3083 expression first. We don't need to do this for comparisons here. */
3084 if (GET_RTX_CLASS (code) == 'c'
3085 && ((CONSTANT_P (XEXP (x, 0)) && GET_CODE (XEXP (x, 1)) != CONST_INT)
3086 || (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == 'o'
3087 && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) != 'o')
3088 || (GET_CODE (XEXP (x, 0)) == SUBREG
3089 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0)))) == 'o'
3090 && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) != 'o')))
3092 temp = XEXP (x, 0);
3093 SUBST (XEXP (x, 0), XEXP (x, 1));
3094 SUBST (XEXP (x, 1), temp);
3097 /* If this is a PLUS, MINUS, or MULT, and the first operand is the
3098 sign extension of a PLUS with a constant, reverse the order of the sign
3099 extension and the addition. Note that this not the same as the original
3100 code, but overflow is undefined for signed values. Also note that the
3101 PLUS will have been partially moved "inside" the sign-extension, so that
3102 the first operand of X will really look like:
3103 (ashiftrt (plus (ashift A C4) C5) C4).
3104 We convert this to
3105 (plus (ashiftrt (ashift A C4) C2) C4)
3106 and replace the first operand of X with that expression. Later parts
3107 of this function may simplify the expression further.
3109 For example, if we start with (mult (sign_extend (plus A C1)) C2),
3110 we swap the SIGN_EXTEND and PLUS. Later code will apply the
3111 distributive law to produce (plus (mult (sign_extend X) C1) C3).
3113 We do this to simplify address expressions. */
3115 if ((code == PLUS || code == MINUS || code == MULT)
3116 && GET_CODE (XEXP (x, 0)) == ASHIFTRT
3117 && GET_CODE (XEXP (XEXP (x, 0), 0)) == PLUS
3118 && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ASHIFT
3119 && GET_CODE (XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 1)) == CONST_INT
3120 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3121 && XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 1) == XEXP (XEXP (x, 0), 1)
3122 && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
3123 && (temp = simplify_binary_operation (ASHIFTRT, mode,
3124 XEXP (XEXP (XEXP (x, 0), 0), 1),
3125 XEXP (XEXP (x, 0), 1))) != 0)
3127 rtx new
3128 = simplify_shift_const (NULL_RTX, ASHIFT, mode,
3129 XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 0),
3130 INTVAL (XEXP (XEXP (x, 0), 1)));
3132 new = simplify_shift_const (NULL_RTX, ASHIFTRT, mode, new,
3133 INTVAL (XEXP (XEXP (x, 0), 1)));
3135 SUBST (XEXP (x, 0), gen_binary (PLUS, mode, new, temp));
3138 /* If this is a simple operation applied to an IF_THEN_ELSE, try
3139 applying it to the arms of the IF_THEN_ELSE. This often simplifies
3140 things. Check for cases where both arms are testing the same
3141 condition.
3143 Don't do anything if all operands are very simple. */
3145 if (((GET_RTX_CLASS (code) == '2' || GET_RTX_CLASS (code) == 'c'
3146 || GET_RTX_CLASS (code) == '<')
3147 && ((GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) != 'o'
3148 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
3149 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0))))
3150 == 'o')))
3151 || (GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) != 'o'
3152 && ! (GET_CODE (XEXP (x, 1)) == SUBREG
3153 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 1))))
3154 == 'o')))))
3155 || (GET_RTX_CLASS (code) == '1'
3156 && ((GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) != 'o'
3157 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
3158 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0))))
3159 == 'o'))))))
3161 rtx cond, true, false;
3163 cond = if_then_else_cond (x, &true, &false);
3164 if (cond != 0
3165 /* If everything is a comparison, what we have is highly unlikely
3166 to be simpler, so don't use it. */
3167 && ! (GET_RTX_CLASS (code) == '<'
3168 && (GET_RTX_CLASS (GET_CODE (true)) == '<'
3169 || GET_RTX_CLASS (GET_CODE (false)) == '<')))
3171 rtx cop1 = const0_rtx;
3172 enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
3174 if (cond_code == NE && GET_RTX_CLASS (GET_CODE (cond)) == '<')
3175 return x;
3177 /* Simplify the alternative arms; this may collapse the true and
3178 false arms to store-flag values. */
3179 true = subst (true, pc_rtx, pc_rtx, 0, 0);
3180 false = subst (false, pc_rtx, pc_rtx, 0, 0);
3182 /* Restarting if we generate a store-flag expression will cause
3183 us to loop. Just drop through in this case. */
3185 /* If the result values are STORE_FLAG_VALUE and zero, we can
3186 just make the comparison operation. */
3187 if (true == const_true_rtx && false == const0_rtx)
3188 x = gen_binary (cond_code, mode, cond, cop1);
3189 else if (true == const0_rtx && false == const_true_rtx)
3190 x = gen_binary (reverse_condition (cond_code), mode, cond, cop1);
3192 /* Likewise, we can make the negate of a comparison operation
3193 if the result values are - STORE_FLAG_VALUE and zero. */
3194 else if (GET_CODE (true) == CONST_INT
3195 && INTVAL (true) == - STORE_FLAG_VALUE
3196 && false == const0_rtx)
3197 x = gen_unary (NEG, mode, mode,
3198 gen_binary (cond_code, mode, cond, cop1));
3199 else if (GET_CODE (false) == CONST_INT
3200 && INTVAL (false) == - STORE_FLAG_VALUE
3201 && true == const0_rtx)
3202 x = gen_unary (NEG, mode, mode,
3203 gen_binary (reverse_condition (cond_code),
3204 mode, cond, cop1));
3205 else
3206 return gen_rtx (IF_THEN_ELSE, mode,
3207 gen_binary (cond_code, VOIDmode, cond, cop1),
3208 true, false);
3210 code = GET_CODE (x);
3211 op0_mode = VOIDmode;
3215 /* Try to fold this expression in case we have constants that weren't
3216 present before. */
3217 temp = 0;
3218 switch (GET_RTX_CLASS (code))
3220 case '1':
3221 temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
3222 break;
3223 case '<':
3224 temp = simplify_relational_operation (code, op0_mode,
3225 XEXP (x, 0), XEXP (x, 1));
3226 #ifdef FLOAT_STORE_FLAG_VALUE
3227 if (temp != 0 && GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
3228 temp = ((temp == const0_rtx) ? CONST0_RTX (GET_MODE (x))
3229 : immed_real_const_1 (FLOAT_STORE_FLAG_VALUE, GET_MODE (x)));
3230 #endif
3231 break;
3232 case 'c':
3233 case '2':
3234 temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
3235 break;
3236 case 'b':
3237 case '3':
3238 temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
3239 XEXP (x, 1), XEXP (x, 2));
3240 break;
3243 if (temp)
3244 x = temp, code = GET_CODE (temp);
3246 /* First see if we can apply the inverse distributive law. */
3247 if (code == PLUS || code == MINUS
3248 || code == AND || code == IOR || code == XOR)
3250 x = apply_distributive_law (x);
3251 code = GET_CODE (x);
3254 /* If CODE is an associative operation not otherwise handled, see if we
3255 can associate some operands. This can win if they are constants or
3256 if they are logically related (i.e. (a & b) & a. */
3257 if ((code == PLUS || code == MINUS
3258 || code == MULT || code == AND || code == IOR || code == XOR
3259 || code == DIV || code == UDIV
3260 || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
3261 && INTEGRAL_MODE_P (mode))
3263 if (GET_CODE (XEXP (x, 0)) == code)
3265 rtx other = XEXP (XEXP (x, 0), 0);
3266 rtx inner_op0 = XEXP (XEXP (x, 0), 1);
3267 rtx inner_op1 = XEXP (x, 1);
3268 rtx inner;
3270 /* Make sure we pass the constant operand if any as the second
3271 one if this is a commutative operation. */
3272 if (CONSTANT_P (inner_op0) && GET_RTX_CLASS (code) == 'c')
3274 rtx tem = inner_op0;
3275 inner_op0 = inner_op1;
3276 inner_op1 = tem;
3278 inner = simplify_binary_operation (code == MINUS ? PLUS
3279 : code == DIV ? MULT
3280 : code == UDIV ? MULT
3281 : code,
3282 mode, inner_op0, inner_op1);
3284 /* For commutative operations, try the other pair if that one
3285 didn't simplify. */
3286 if (inner == 0 && GET_RTX_CLASS (code) == 'c')
3288 other = XEXP (XEXP (x, 0), 1);
3289 inner = simplify_binary_operation (code, mode,
3290 XEXP (XEXP (x, 0), 0),
3291 XEXP (x, 1));
3294 if (inner)
3295 return gen_binary (code, mode, other, inner);
3299 /* A little bit of algebraic simplification here. */
3300 switch (code)
3302 case MEM:
3303 /* Ensure that our address has any ASHIFTs converted to MULT in case
3304 address-recognizing predicates are called later. */
3305 temp = make_compound_operation (XEXP (x, 0), MEM);
3306 SUBST (XEXP (x, 0), temp);
3307 break;
3309 case SUBREG:
3310 /* (subreg:A (mem:B X) N) becomes a modified MEM unless the SUBREG
3311 is paradoxical. If we can't do that safely, then it becomes
3312 something nonsensical so that this combination won't take place. */
3314 if (GET_CODE (SUBREG_REG (x)) == MEM
3315 && (GET_MODE_SIZE (mode)
3316 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))))
3318 rtx inner = SUBREG_REG (x);
3319 int endian_offset = 0;
3320 /* Don't change the mode of the MEM
3321 if that would change the meaning of the address. */
3322 if (MEM_VOLATILE_P (SUBREG_REG (x))
3323 || mode_dependent_address_p (XEXP (inner, 0)))
3324 return gen_rtx (CLOBBER, mode, const0_rtx);
3326 if (BYTES_BIG_ENDIAN)
3328 if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
3329 endian_offset += UNITS_PER_WORD - GET_MODE_SIZE (mode);
3330 if (GET_MODE_SIZE (GET_MODE (inner)) < UNITS_PER_WORD)
3331 endian_offset -= (UNITS_PER_WORD
3332 - GET_MODE_SIZE (GET_MODE (inner)));
3334 /* Note if the plus_constant doesn't make a valid address
3335 then this combination won't be accepted. */
3336 x = gen_rtx (MEM, mode,
3337 plus_constant (XEXP (inner, 0),
3338 (SUBREG_WORD (x) * UNITS_PER_WORD
3339 + endian_offset)));
3340 MEM_VOLATILE_P (x) = MEM_VOLATILE_P (inner);
3341 RTX_UNCHANGING_P (x) = RTX_UNCHANGING_P (inner);
3342 MEM_IN_STRUCT_P (x) = MEM_IN_STRUCT_P (inner);
3343 return x;
3346 /* If we are in a SET_DEST, these other cases can't apply. */
3347 if (in_dest)
3348 return x;
3350 /* Changing mode twice with SUBREG => just change it once,
3351 or not at all if changing back to starting mode. */
3352 if (GET_CODE (SUBREG_REG (x)) == SUBREG)
3354 if (mode == GET_MODE (SUBREG_REG (SUBREG_REG (x)))
3355 && SUBREG_WORD (x) == 0 && SUBREG_WORD (SUBREG_REG (x)) == 0)
3356 return SUBREG_REG (SUBREG_REG (x));
3358 SUBST_INT (SUBREG_WORD (x),
3359 SUBREG_WORD (x) + SUBREG_WORD (SUBREG_REG (x)));
3360 SUBST (SUBREG_REG (x), SUBREG_REG (SUBREG_REG (x)));
3363 /* SUBREG of a hard register => just change the register number
3364 and/or mode. If the hard register is not valid in that mode,
3365 suppress this combination. If the hard register is the stack,
3366 frame, or argument pointer, leave this as a SUBREG. */
3368 if (GET_CODE (SUBREG_REG (x)) == REG
3369 && REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER
3370 && REGNO (SUBREG_REG (x)) != FRAME_POINTER_REGNUM
3371 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3372 && REGNO (SUBREG_REG (x)) != HARD_FRAME_POINTER_REGNUM
3373 #endif
3374 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3375 && REGNO (SUBREG_REG (x)) != ARG_POINTER_REGNUM
3376 #endif
3377 && REGNO (SUBREG_REG (x)) != STACK_POINTER_REGNUM)
3379 if (HARD_REGNO_MODE_OK (REGNO (SUBREG_REG (x)) + SUBREG_WORD (x),
3380 mode))
3381 return gen_rtx (REG, mode,
3382 REGNO (SUBREG_REG (x)) + SUBREG_WORD (x));
3383 else
3384 return gen_rtx (CLOBBER, mode, const0_rtx);
3387 /* For a constant, try to pick up the part we want. Handle a full
3388 word and low-order part. Only do this if we are narrowing
3389 the constant; if it is being widened, we have no idea what
3390 the extra bits will have been set to. */
3392 if (CONSTANT_P (SUBREG_REG (x)) && op0_mode != VOIDmode
3393 && GET_MODE_SIZE (mode) == UNITS_PER_WORD
3394 && GET_MODE_SIZE (op0_mode) > UNITS_PER_WORD
3395 && GET_MODE_CLASS (mode) == MODE_INT)
3397 temp = operand_subword (SUBREG_REG (x), SUBREG_WORD (x),
3398 0, op0_mode);
3399 if (temp)
3400 return temp;
3403 /* If we want a subreg of a constant, at offset 0,
3404 take the low bits. On a little-endian machine, that's
3405 always valid. On a big-endian machine, it's valid
3406 only if the constant's mode fits in one word. Note that we
3407 cannot use subreg_lowpart_p since SUBREG_REG may be VOIDmode. */
3408 if (CONSTANT_P (SUBREG_REG (x))
3409 && ((GET_MODE_SIZE (op0_mode) <= UNITS_PER_WORD
3410 || ! WORDS_BIG_ENDIAN)
3411 ? SUBREG_WORD (x) == 0
3412 : (SUBREG_WORD (x)
3413 == ((GET_MODE_SIZE (op0_mode)
3414 - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD))
3415 / UNITS_PER_WORD)))
3416 && GET_MODE_SIZE (mode) <= GET_MODE_SIZE (op0_mode)
3417 && (! WORDS_BIG_ENDIAN
3418 || GET_MODE_BITSIZE (op0_mode) <= BITS_PER_WORD))
3419 return gen_lowpart_for_combine (mode, SUBREG_REG (x));
3421 /* A paradoxical SUBREG of a VOIDmode constant is the same constant,
3422 since we are saying that the high bits don't matter. */
3423 if (CONSTANT_P (SUBREG_REG (x)) && GET_MODE (SUBREG_REG (x)) == VOIDmode
3424 && GET_MODE_SIZE (mode) > GET_MODE_SIZE (op0_mode))
3425 return SUBREG_REG (x);
3427 /* Note that we cannot do any narrowing for non-constants since
3428 we might have been counting on using the fact that some bits were
3429 zero. We now do this in the SET. */
3431 break;
3433 case NOT:
3434 /* (not (plus X -1)) can become (neg X). */
3435 if (GET_CODE (XEXP (x, 0)) == PLUS
3436 && XEXP (XEXP (x, 0), 1) == constm1_rtx)
3437 return gen_rtx_combine (NEG, mode, XEXP (XEXP (x, 0), 0));
3439 /* Similarly, (not (neg X)) is (plus X -1). */
3440 if (GET_CODE (XEXP (x, 0)) == NEG)
3441 return gen_rtx_combine (PLUS, mode, XEXP (XEXP (x, 0), 0),
3442 constm1_rtx);
3444 /* (not (xor X C)) for C constant is (xor X D) with D = ~ C. */
3445 if (GET_CODE (XEXP (x, 0)) == XOR
3446 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3447 && (temp = simplify_unary_operation (NOT, mode,
3448 XEXP (XEXP (x, 0), 1),
3449 mode)) != 0)
3450 return gen_binary (XOR, mode, XEXP (XEXP (x, 0), 0), temp);
3452 /* (not (ashift 1 X)) is (rotate ~1 X). We used to do this for operands
3453 other than 1, but that is not valid. We could do a similar
3454 simplification for (not (lshiftrt C X)) where C is just the sign bit,
3455 but this doesn't seem common enough to bother with. */
3456 if (GET_CODE (XEXP (x, 0)) == ASHIFT
3457 && XEXP (XEXP (x, 0), 0) == const1_rtx)
3458 return gen_rtx (ROTATE, mode, gen_unary (NOT, mode, mode, const1_rtx),
3459 XEXP (XEXP (x, 0), 1));
3461 if (GET_CODE (XEXP (x, 0)) == SUBREG
3462 && subreg_lowpart_p (XEXP (x, 0))
3463 && (GET_MODE_SIZE (GET_MODE (XEXP (x, 0)))
3464 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (x, 0)))))
3465 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == ASHIFT
3466 && XEXP (SUBREG_REG (XEXP (x, 0)), 0) == const1_rtx)
3468 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (XEXP (x, 0)));
3470 x = gen_rtx (ROTATE, inner_mode,
3471 gen_unary (NOT, inner_mode, inner_mode, const1_rtx),
3472 XEXP (SUBREG_REG (XEXP (x, 0)), 1));
3473 return gen_lowpart_for_combine (mode, x);
3476 /* If STORE_FLAG_VALUE is -1, (not (comparison foo bar)) can be done by
3477 reversing the comparison code if valid. */
3478 if (STORE_FLAG_VALUE == -1
3479 && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
3480 && reversible_comparison_p (XEXP (x, 0)))
3481 return gen_rtx_combine (reverse_condition (GET_CODE (XEXP (x, 0))),
3482 mode, XEXP (XEXP (x, 0), 0),
3483 XEXP (XEXP (x, 0), 1));
3485 /* (ashiftrt foo C) where C is the number of bits in FOO minus 1
3486 is (lt foo (const_int 0)) if STORE_FLAG_VALUE is -1, so we can
3487 perform the above simplification. */
3489 if (STORE_FLAG_VALUE == -1
3490 && XEXP (x, 1) == const1_rtx
3491 && GET_CODE (XEXP (x, 0)) == ASHIFTRT
3492 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3493 && INTVAL (XEXP (XEXP (x, 0), 1)) == GET_MODE_BITSIZE (mode) - 1)
3494 return gen_rtx_combine (GE, mode, XEXP (XEXP (x, 0), 0), const0_rtx);
3496 /* Apply De Morgan's laws to reduce number of patterns for machines
3497 with negating logical insns (and-not, nand, etc.). If result has
3498 only one NOT, put it first, since that is how the patterns are
3499 coded. */
3501 if (GET_CODE (XEXP (x, 0)) == IOR || GET_CODE (XEXP (x, 0)) == AND)
3503 rtx in1 = XEXP (XEXP (x, 0), 0), in2 = XEXP (XEXP (x, 0), 1);
3505 if (GET_CODE (in1) == NOT)
3506 in1 = XEXP (in1, 0);
3507 else
3508 in1 = gen_rtx_combine (NOT, GET_MODE (in1), in1);
3510 if (GET_CODE (in2) == NOT)
3511 in2 = XEXP (in2, 0);
3512 else if (GET_CODE (in2) == CONST_INT
3513 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
3514 in2 = GEN_INT (GET_MODE_MASK (mode) & ~ INTVAL (in2));
3515 else
3516 in2 = gen_rtx_combine (NOT, GET_MODE (in2), in2);
3518 if (GET_CODE (in2) == NOT)
3520 rtx tem = in2;
3521 in2 = in1; in1 = tem;
3524 return gen_rtx_combine (GET_CODE (XEXP (x, 0)) == IOR ? AND : IOR,
3525 mode, in1, in2);
3527 break;
3529 case NEG:
3530 /* (neg (plus X 1)) can become (not X). */
3531 if (GET_CODE (XEXP (x, 0)) == PLUS
3532 && XEXP (XEXP (x, 0), 1) == const1_rtx)
3533 return gen_rtx_combine (NOT, mode, XEXP (XEXP (x, 0), 0));
3535 /* Similarly, (neg (not X)) is (plus X 1). */
3536 if (GET_CODE (XEXP (x, 0)) == NOT)
3537 return plus_constant (XEXP (XEXP (x, 0), 0), 1);
3539 /* (neg (minus X Y)) can become (minus Y X). */
3540 if (GET_CODE (XEXP (x, 0)) == MINUS
3541 && (! FLOAT_MODE_P (mode)
3542 /* x-y != -(y-x) with IEEE floating point. */
3543 || TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
3544 || flag_fast_math))
3545 return gen_binary (MINUS, mode, XEXP (XEXP (x, 0), 1),
3546 XEXP (XEXP (x, 0), 0));
3548 /* (neg (xor A 1)) is (plus A -1) if A is known to be either 0 or 1. */
3549 if (GET_CODE (XEXP (x, 0)) == XOR && XEXP (XEXP (x, 0), 1) == const1_rtx
3550 && nonzero_bits (XEXP (XEXP (x, 0), 0), mode) == 1)
3551 return gen_binary (PLUS, mode, XEXP (XEXP (x, 0), 0), constm1_rtx);
3553 /* NEG commutes with ASHIFT since it is multiplication. Only do this
3554 if we can then eliminate the NEG (e.g.,
3555 if the operand is a constant). */
3557 if (GET_CODE (XEXP (x, 0)) == ASHIFT)
3559 temp = simplify_unary_operation (NEG, mode,
3560 XEXP (XEXP (x, 0), 0), mode);
3561 if (temp)
3563 SUBST (XEXP (XEXP (x, 0), 0), temp);
3564 return XEXP (x, 0);
3568 temp = expand_compound_operation (XEXP (x, 0));
3570 /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
3571 replaced by (lshiftrt X C). This will convert
3572 (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y). */
3574 if (GET_CODE (temp) == ASHIFTRT
3575 && GET_CODE (XEXP (temp, 1)) == CONST_INT
3576 && INTVAL (XEXP (temp, 1)) == GET_MODE_BITSIZE (mode) - 1)
3577 return simplify_shift_const (temp, LSHIFTRT, mode, XEXP (temp, 0),
3578 INTVAL (XEXP (temp, 1)));
3580 /* If X has only a single bit that might be nonzero, say, bit I, convert
3581 (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
3582 MODE minus 1. This will convert (neg (zero_extract X 1 Y)) to
3583 (sign_extract X 1 Y). But only do this if TEMP isn't a register
3584 or a SUBREG of one since we'd be making the expression more
3585 complex if it was just a register. */
3587 if (GET_CODE (temp) != REG
3588 && ! (GET_CODE (temp) == SUBREG
3589 && GET_CODE (SUBREG_REG (temp)) == REG)
3590 && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
3592 rtx temp1 = simplify_shift_const
3593 (NULL_RTX, ASHIFTRT, mode,
3594 simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
3595 GET_MODE_BITSIZE (mode) - 1 - i),
3596 GET_MODE_BITSIZE (mode) - 1 - i);
3598 /* If all we did was surround TEMP with the two shifts, we
3599 haven't improved anything, so don't use it. Otherwise,
3600 we are better off with TEMP1. */
3601 if (GET_CODE (temp1) != ASHIFTRT
3602 || GET_CODE (XEXP (temp1, 0)) != ASHIFT
3603 || XEXP (XEXP (temp1, 0), 0) != temp)
3604 return temp1;
3606 break;
3608 case TRUNCATE:
3609 /* We can't handle truncation to a partial integer mode here
3610 because we don't know the real bitsize of the partial
3611 integer mode. */
3612 if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
3613 break;
3615 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
3616 SUBST (XEXP (x, 0),
3617 force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
3618 GET_MODE_MASK (mode), NULL_RTX, 0));
3620 /* (truncate:SI ({sign,zero}_extend:DI foo:SI)) == foo:SI. */
3621 if ((GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
3622 || GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
3623 && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode)
3624 return XEXP (XEXP (x, 0), 0);
3626 /* (truncate:SI (OP:DI ({sign,zero}_extend:DI foo:SI))) is
3627 (OP:SI foo:SI) if OP is NEG or ABS. */
3628 if ((GET_CODE (XEXP (x, 0)) == ABS
3629 || GET_CODE (XEXP (x, 0)) == NEG)
3630 && (GET_CODE (XEXP (XEXP (x, 0), 0)) == SIGN_EXTEND
3631 || GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND)
3632 && GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == mode)
3633 return gen_unary (GET_CODE (XEXP (x, 0)), mode, mode,
3634 XEXP (XEXP (XEXP (x, 0), 0), 0));
3636 /* (truncate:SI (subreg:DI (truncate:SI X) 0)) is
3637 (truncate:SI x). */
3638 if (GET_CODE (XEXP (x, 0)) == SUBREG
3639 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == TRUNCATE
3640 && subreg_lowpart_p (XEXP (x, 0)))
3641 return SUBREG_REG (XEXP (x, 0));
3643 /* If we know that the value is already truncated, we can
3644 replace the TRUNCATE with a SUBREG. */
3645 if (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) <= HOST_BITS_PER_WIDE_INT
3646 && (nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
3647 &~ GET_MODE_MASK (mode)) == 0)
3648 return gen_lowpart_for_combine (mode, XEXP (x, 0));
3650 /* A truncate of a comparison can be replaced with a subreg if
3651 STORE_FLAG_VALUE permits. This is like the previous test,
3652 but it works even if the comparison is done in a mode larger
3653 than HOST_BITS_PER_WIDE_INT. */
3654 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3655 && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
3656 && ((HOST_WIDE_INT) STORE_FLAG_VALUE &~ GET_MODE_MASK (mode)) == 0)
3657 return gen_lowpart_for_combine (mode, XEXP (x, 0));
3659 /* Similarly, a truncate of a register whose value is a
3660 comparison can be replaced with a subreg if STORE_FLAG_VALUE
3661 permits. */
3662 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3663 && ((HOST_WIDE_INT) STORE_FLAG_VALUE &~ GET_MODE_MASK (mode)) == 0
3664 && (temp = get_last_value (XEXP (x, 0)))
3665 && GET_RTX_CLASS (GET_CODE (temp)) == '<')
3666 return gen_lowpart_for_combine (mode, XEXP (x, 0));
3668 break;
3670 case FLOAT_TRUNCATE:
3671 /* (float_truncate:SF (float_extend:DF foo:SF)) = foo:SF. */
3672 if (GET_CODE (XEXP (x, 0)) == FLOAT_EXTEND
3673 && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode)
3674 return XEXP (XEXP (x, 0), 0);
3676 /* (float_truncate:SF (OP:DF (float_extend:DF foo:sf))) is
3677 (OP:SF foo:SF) if OP is NEG or ABS. */
3678 if ((GET_CODE (XEXP (x, 0)) == ABS
3679 || GET_CODE (XEXP (x, 0)) == NEG)
3680 && GET_CODE (XEXP (XEXP (x, 0), 0)) == FLOAT_EXTEND
3681 && GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == mode)
3682 return gen_unary (GET_CODE (XEXP (x, 0)), mode, mode,
3683 XEXP (XEXP (XEXP (x, 0), 0), 0));
3685 /* (float_truncate:SF (subreg:DF (float_truncate:SF X) 0))
3686 is (float_truncate:SF x). */
3687 if (GET_CODE (XEXP (x, 0)) == SUBREG
3688 && subreg_lowpart_p (XEXP (x, 0))
3689 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == FLOAT_TRUNCATE)
3690 return SUBREG_REG (XEXP (x, 0));
3691 break;
3693 #ifdef HAVE_cc0
3694 case COMPARE:
3695 /* Convert (compare FOO (const_int 0)) to FOO unless we aren't
3696 using cc0, in which case we want to leave it as a COMPARE
3697 so we can distinguish it from a register-register-copy. */
3698 if (XEXP (x, 1) == const0_rtx)
3699 return XEXP (x, 0);
3701 /* In IEEE floating point, x-0 is not the same as x. */
3702 if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
3703 || ! FLOAT_MODE_P (GET_MODE (XEXP (x, 0)))
3704 || flag_fast_math)
3705 && XEXP (x, 1) == CONST0_RTX (GET_MODE (XEXP (x, 0))))
3706 return XEXP (x, 0);
3707 break;
3708 #endif
3710 case CONST:
3711 /* (const (const X)) can become (const X). Do it this way rather than
3712 returning the inner CONST since CONST can be shared with a
3713 REG_EQUAL note. */
3714 if (GET_CODE (XEXP (x, 0)) == CONST)
3715 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
3716 break;
3718 #ifdef HAVE_lo_sum
3719 case LO_SUM:
3720 /* Convert (lo_sum (high FOO) FOO) to FOO. This is necessary so we
3721 can add in an offset. find_split_point will split this address up
3722 again if it doesn't match. */
3723 if (GET_CODE (XEXP (x, 0)) == HIGH
3724 && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
3725 return XEXP (x, 1);
3726 break;
3727 #endif
3729 case PLUS:
3730 /* If we have (plus (plus (A const) B)), associate it so that CONST is
3731 outermost. That's because that's the way indexed addresses are
3732 supposed to appear. This code used to check many more cases, but
3733 they are now checked elsewhere. */
3734 if (GET_CODE (XEXP (x, 0)) == PLUS
3735 && CONSTANT_ADDRESS_P (XEXP (XEXP (x, 0), 1)))
3736 return gen_binary (PLUS, mode,
3737 gen_binary (PLUS, mode, XEXP (XEXP (x, 0), 0),
3738 XEXP (x, 1)),
3739 XEXP (XEXP (x, 0), 1));
3741 /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
3742 when c is (const_int (pow2 + 1) / 2) is a sign extension of a
3743 bit-field and can be replaced by either a sign_extend or a
3744 sign_extract. The `and' may be a zero_extend. */
3745 if (GET_CODE (XEXP (x, 0)) == XOR
3746 && GET_CODE (XEXP (x, 1)) == CONST_INT
3747 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3748 && INTVAL (XEXP (x, 1)) == - INTVAL (XEXP (XEXP (x, 0), 1))
3749 && (i = exact_log2 (INTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
3750 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3751 && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
3752 && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
3753 && (INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
3754 == ((HOST_WIDE_INT) 1 << (i + 1)) - 1))
3755 || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
3756 && (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
3757 == i + 1))))
3758 return simplify_shift_const
3759 (NULL_RTX, ASHIFTRT, mode,
3760 simplify_shift_const (NULL_RTX, ASHIFT, mode,
3761 XEXP (XEXP (XEXP (x, 0), 0), 0),
3762 GET_MODE_BITSIZE (mode) - (i + 1)),
3763 GET_MODE_BITSIZE (mode) - (i + 1));
3765 /* (plus (comparison A B) C) can become (neg (rev-comp A B)) if
3766 C is 1 and STORE_FLAG_VALUE is -1 or if C is -1 and STORE_FLAG_VALUE
3767 is 1. This produces better code than the alternative immediately
3768 below. */
3769 if (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
3770 && reversible_comparison_p (XEXP (x, 0))
3771 && ((STORE_FLAG_VALUE == -1 && XEXP (x, 1) == const1_rtx)
3772 || (STORE_FLAG_VALUE == 1 && XEXP (x, 1) == constm1_rtx)))
3773 return
3774 gen_unary (NEG, mode, mode,
3775 gen_binary (reverse_condition (GET_CODE (XEXP (x, 0))),
3776 mode, XEXP (XEXP (x, 0), 0),
3777 XEXP (XEXP (x, 0), 1)));
3779 /* If only the low-order bit of X is possibly nonzero, (plus x -1)
3780 can become (ashiftrt (ashift (xor x 1) C) C) where C is
3781 the bitsize of the mode - 1. This allows simplification of
3782 "a = (b & 8) == 0;" */
3783 if (XEXP (x, 1) == constm1_rtx
3784 && GET_CODE (XEXP (x, 0)) != REG
3785 && ! (GET_CODE (XEXP (x,0)) == SUBREG
3786 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG)
3787 && nonzero_bits (XEXP (x, 0), mode) == 1)
3788 return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
3789 simplify_shift_const (NULL_RTX, ASHIFT, mode,
3790 gen_rtx_combine (XOR, mode,
3791 XEXP (x, 0), const1_rtx),
3792 GET_MODE_BITSIZE (mode) - 1),
3793 GET_MODE_BITSIZE (mode) - 1);
3795 /* If we are adding two things that have no bits in common, convert
3796 the addition into an IOR. This will often be further simplified,
3797 for example in cases like ((a & 1) + (a & 2)), which can
3798 become a & 3. */
3800 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3801 && (nonzero_bits (XEXP (x, 0), mode)
3802 & nonzero_bits (XEXP (x, 1), mode)) == 0)
3803 return gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
3804 break;
3806 case MINUS:
3807 /* If STORE_FLAG_VALUE is 1, (minus 1 (comparison foo bar)) can be done
3808 by reversing the comparison code if valid. */
3809 if (STORE_FLAG_VALUE == 1
3810 && XEXP (x, 0) == const1_rtx
3811 && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) == '<'
3812 && reversible_comparison_p (XEXP (x, 1)))
3813 return gen_binary (reverse_condition (GET_CODE (XEXP (x, 1))),
3814 mode, XEXP (XEXP (x, 1), 0),
3815 XEXP (XEXP (x, 1), 1));
3817 /* (minus <foo> (and <foo> (const_int -pow2))) becomes
3818 (and <foo> (const_int pow2-1)) */
3819 if (GET_CODE (XEXP (x, 1)) == AND
3820 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
3821 && exact_log2 (- INTVAL (XEXP (XEXP (x, 1), 1))) >= 0
3822 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
3823 return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
3824 - INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
3826 /* Canonicalize (minus A (plus B C)) to (minus (minus A B) C) for
3827 integers. */
3828 if (GET_CODE (XEXP (x, 1)) == PLUS && INTEGRAL_MODE_P (mode))
3829 return gen_binary (MINUS, mode,
3830 gen_binary (MINUS, mode, XEXP (x, 0),
3831 XEXP (XEXP (x, 1), 0)),
3832 XEXP (XEXP (x, 1), 1));
3833 break;
3835 case MULT:
3836 /* If we have (mult (plus A B) C), apply the distributive law and then
3837 the inverse distributive law to see if things simplify. This
3838 occurs mostly in addresses, often when unrolling loops. */
3840 if (GET_CODE (XEXP (x, 0)) == PLUS)
3842 x = apply_distributive_law
3843 (gen_binary (PLUS, mode,
3844 gen_binary (MULT, mode,
3845 XEXP (XEXP (x, 0), 0), XEXP (x, 1)),
3846 gen_binary (MULT, mode,
3847 XEXP (XEXP (x, 0), 1), XEXP (x, 1))));
3849 if (GET_CODE (x) != MULT)
3850 return x;
3852 break;
3854 case UDIV:
3855 /* If this is a divide by a power of two, treat it as a shift if
3856 its first operand is a shift. */
3857 if (GET_CODE (XEXP (x, 1)) == CONST_INT
3858 && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0
3859 && (GET_CODE (XEXP (x, 0)) == ASHIFT
3860 || GET_CODE (XEXP (x, 0)) == LSHIFTRT
3861 || GET_CODE (XEXP (x, 0)) == ASHIFTRT
3862 || GET_CODE (XEXP (x, 0)) == ROTATE
3863 || GET_CODE (XEXP (x, 0)) == ROTATERT))
3864 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
3865 break;
3867 case EQ: case NE:
3868 case GT: case GTU: case GE: case GEU:
3869 case LT: case LTU: case LE: case LEU:
3870 /* If the first operand is a condition code, we can't do anything
3871 with it. */
3872 if (GET_CODE (XEXP (x, 0)) == COMPARE
3873 || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
3874 #ifdef HAVE_cc0
3875 && XEXP (x, 0) != cc0_rtx
3876 #endif
3879 rtx op0 = XEXP (x, 0);
3880 rtx op1 = XEXP (x, 1);
3881 enum rtx_code new_code;
3883 if (GET_CODE (op0) == COMPARE)
3884 op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
3886 /* Simplify our comparison, if possible. */
3887 new_code = simplify_comparison (code, &op0, &op1);
3889 /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
3890 if only the low-order bit is possibly nonzero in X (such as when
3891 X is a ZERO_EXTRACT of one bit). Similarly, we can convert EQ to
3892 (xor X 1) or (minus 1 X); we use the former. Finally, if X is
3893 known to be either 0 or -1, NE becomes a NEG and EQ becomes
3894 (plus X 1).
3896 Remove any ZERO_EXTRACT we made when thinking this was a
3897 comparison. It may now be simpler to use, e.g., an AND. If a
3898 ZERO_EXTRACT is indeed appropriate, it will be placed back by
3899 the call to make_compound_operation in the SET case. */
3901 if (STORE_FLAG_VALUE == 1
3902 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
3903 && op1 == const0_rtx && nonzero_bits (op0, mode) == 1)
3904 return gen_lowpart_for_combine (mode,
3905 expand_compound_operation (op0));
3907 else if (STORE_FLAG_VALUE == 1
3908 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
3909 && op1 == const0_rtx
3910 && (num_sign_bit_copies (op0, mode)
3911 == GET_MODE_BITSIZE (mode)))
3913 op0 = expand_compound_operation (op0);
3914 return gen_unary (NEG, mode, mode,
3915 gen_lowpart_for_combine (mode, op0));
3918 else if (STORE_FLAG_VALUE == 1
3919 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
3920 && op1 == const0_rtx
3921 && nonzero_bits (op0, mode) == 1)
3923 op0 = expand_compound_operation (op0);
3924 return gen_binary (XOR, mode,
3925 gen_lowpart_for_combine (mode, op0),
3926 const1_rtx);
3929 else if (STORE_FLAG_VALUE == 1
3930 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
3931 && op1 == const0_rtx
3932 && (num_sign_bit_copies (op0, mode)
3933 == GET_MODE_BITSIZE (mode)))
3935 op0 = expand_compound_operation (op0);
3936 return plus_constant (gen_lowpart_for_combine (mode, op0), 1);
3939 /* If STORE_FLAG_VALUE is -1, we have cases similar to
3940 those above. */
3941 if (STORE_FLAG_VALUE == -1
3942 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
3943 && op1 == const0_rtx
3944 && (num_sign_bit_copies (op0, mode)
3945 == GET_MODE_BITSIZE (mode)))
3946 return gen_lowpart_for_combine (mode,
3947 expand_compound_operation (op0));
3949 else if (STORE_FLAG_VALUE == -1
3950 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
3951 && op1 == const0_rtx
3952 && nonzero_bits (op0, mode) == 1)
3954 op0 = expand_compound_operation (op0);
3955 return gen_unary (NEG, mode, mode,
3956 gen_lowpart_for_combine (mode, op0));
3959 else if (STORE_FLAG_VALUE == -1
3960 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
3961 && op1 == const0_rtx
3962 && (num_sign_bit_copies (op0, mode)
3963 == GET_MODE_BITSIZE (mode)))
3965 op0 = expand_compound_operation (op0);
3966 return gen_unary (NOT, mode, mode,
3967 gen_lowpart_for_combine (mode, op0));
3970 /* If X is 0/1, (eq X 0) is X-1. */
3971 else if (STORE_FLAG_VALUE == -1
3972 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
3973 && op1 == const0_rtx
3974 && nonzero_bits (op0, mode) == 1)
3976 op0 = expand_compound_operation (op0);
3977 return plus_constant (gen_lowpart_for_combine (mode, op0), -1);
3980 /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
3981 one bit that might be nonzero, we can convert (ne x 0) to
3982 (ashift x c) where C puts the bit in the sign bit. Remove any
3983 AND with STORE_FLAG_VALUE when we are done, since we are only
3984 going to test the sign bit. */
3985 if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
3986 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3987 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
3988 == (HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
3989 && op1 == const0_rtx
3990 && mode == GET_MODE (op0)
3991 && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
3993 x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
3994 expand_compound_operation (op0),
3995 GET_MODE_BITSIZE (mode) - 1 - i);
3996 if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
3997 return XEXP (x, 0);
3998 else
3999 return x;
4002 /* If the code changed, return a whole new comparison. */
4003 if (new_code != code)
4004 return gen_rtx_combine (new_code, mode, op0, op1);
4006 /* Otherwise, keep this operation, but maybe change its operands.
4007 This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR). */
4008 SUBST (XEXP (x, 0), op0);
4009 SUBST (XEXP (x, 1), op1);
4011 break;
4013 case IF_THEN_ELSE:
4014 return simplify_if_then_else (x);
4016 case ZERO_EXTRACT:
4017 case SIGN_EXTRACT:
4018 case ZERO_EXTEND:
4019 case SIGN_EXTEND:
4020 /* If we are processing SET_DEST, we are done. */
4021 if (in_dest)
4022 return x;
4024 return expand_compound_operation (x);
4026 case SET:
4027 return simplify_set (x);
4029 case AND:
4030 case IOR:
4031 case XOR:
4032 return simplify_logical (x, last);
4034 case ABS:
4035 /* (abs (neg <foo>)) -> (abs <foo>) */
4036 if (GET_CODE (XEXP (x, 0)) == NEG)
4037 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4039 /* If operand is something known to be positive, ignore the ABS. */
4040 if (GET_CODE (XEXP (x, 0)) == FFS || GET_CODE (XEXP (x, 0)) == ABS
4041 || ((GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
4042 <= HOST_BITS_PER_WIDE_INT)
4043 && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
4044 & ((HOST_WIDE_INT) 1
4045 << (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - 1)))
4046 == 0)))
4047 return XEXP (x, 0);
4050 /* If operand is known to be only -1 or 0, convert ABS to NEG. */
4051 if (num_sign_bit_copies (XEXP (x, 0), mode) == GET_MODE_BITSIZE (mode))
4052 return gen_rtx_combine (NEG, mode, XEXP (x, 0));
4054 break;
4056 case FFS:
4057 /* (ffs (*_extend <X>)) = (ffs <X>) */
4058 if (GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
4059 || GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
4060 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4061 break;
4063 case FLOAT:
4064 /* (float (sign_extend <X>)) = (float <X>). */
4065 if (GET_CODE (XEXP (x, 0)) == SIGN_EXTEND)
4066 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4067 break;
4069 case ASHIFT:
4070 case LSHIFTRT:
4071 case ASHIFTRT:
4072 case ROTATE:
4073 case ROTATERT:
4074 /* If this is a shift by a constant amount, simplify it. */
4075 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4076 return simplify_shift_const (x, code, mode, XEXP (x, 0),
4077 INTVAL (XEXP (x, 1)));
4079 #ifdef SHIFT_COUNT_TRUNCATED
4080 else if (SHIFT_COUNT_TRUNCATED && GET_CODE (XEXP (x, 1)) != REG)
4081 SUBST (XEXP (x, 1),
4082 force_to_mode (XEXP (x, 1), GET_MODE (x),
4083 ((HOST_WIDE_INT) 1
4084 << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
4085 - 1,
4086 NULL_RTX, 0));
4087 #endif
4089 break;
4092 return x;
4095 /* Simplify X, an IF_THEN_ELSE expression. Return the new expression. */
4097 static rtx
4098 simplify_if_then_else (x)
4099 rtx x;
4101 enum machine_mode mode = GET_MODE (x);
4102 rtx cond = XEXP (x, 0);
4103 rtx true = XEXP (x, 1);
4104 rtx false = XEXP (x, 2);
4105 enum rtx_code true_code = GET_CODE (cond);
4106 int comparison_p = GET_RTX_CLASS (true_code) == '<';
4107 rtx temp;
4108 int i;
4110 /* Simplify storing of the truth value. */
4111 if (comparison_p && true == const_true_rtx && false == const0_rtx)
4112 return gen_binary (true_code, mode, XEXP (cond, 0), XEXP (cond, 1));
4114 /* Also when the truth value has to be reversed. */
4115 if (comparison_p && reversible_comparison_p (cond)
4116 && true == const0_rtx && false == const_true_rtx)
4117 return gen_binary (reverse_condition (true_code),
4118 mode, XEXP (cond, 0), XEXP (cond, 1));
4120 /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
4121 in it is being compared against certain values. Get the true and false
4122 comparisons and see if that says anything about the value of each arm. */
4124 if (comparison_p && reversible_comparison_p (cond)
4125 && GET_CODE (XEXP (cond, 0)) == REG)
4127 HOST_WIDE_INT nzb;
4128 rtx from = XEXP (cond, 0);
4129 enum rtx_code false_code = reverse_condition (true_code);
4130 rtx true_val = XEXP (cond, 1);
4131 rtx false_val = true_val;
4132 int swapped = 0;
4134 /* If FALSE_CODE is EQ, swap the codes and arms. */
4136 if (false_code == EQ)
4138 swapped = 1, true_code = EQ, false_code = NE;
4139 temp = true, true = false, false = temp;
4142 /* If we are comparing against zero and the expression being tested has
4143 only a single bit that might be nonzero, that is its value when it is
4144 not equal to zero. Similarly if it is known to be -1 or 0. */
4146 if (true_code == EQ && true_val == const0_rtx
4147 && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
4148 false_code = EQ, false_val = GEN_INT (nzb);
4149 else if (true_code == EQ && true_val == const0_rtx
4150 && (num_sign_bit_copies (from, GET_MODE (from))
4151 == GET_MODE_BITSIZE (GET_MODE (from))))
4152 false_code = EQ, false_val = constm1_rtx;
4154 /* Now simplify an arm if we know the value of the register in the
4155 branch and it is used in the arm. Be careful due to the potential
4156 of locally-shared RTL. */
4158 if (reg_mentioned_p (from, true))
4159 true = subst (known_cond (copy_rtx (true), true_code, from, true_val),
4160 pc_rtx, pc_rtx, 0, 0);
4161 if (reg_mentioned_p (from, false))
4162 false = subst (known_cond (copy_rtx (false), false_code,
4163 from, false_val),
4164 pc_rtx, pc_rtx, 0, 0);
4166 SUBST (XEXP (x, 1), swapped ? false : true);
4167 SUBST (XEXP (x, 2), swapped ? true : false);
4169 true = XEXP (x, 1), false = XEXP (x, 2), true_code = GET_CODE (cond);
4172 /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
4173 reversed, do so to avoid needing two sets of patterns for
4174 subtract-and-branch insns. Similarly if we have a constant in the true
4175 arm, the false arm is the same as the first operand of the comparison, or
4176 the false arm is more complicated than the true arm. */
4178 if (comparison_p && reversible_comparison_p (cond)
4179 && (true == pc_rtx
4180 || (CONSTANT_P (true)
4181 && GET_CODE (false) != CONST_INT && false != pc_rtx)
4182 || true == const0_rtx
4183 || (GET_RTX_CLASS (GET_CODE (true)) == 'o'
4184 && GET_RTX_CLASS (GET_CODE (false)) != 'o')
4185 || (GET_CODE (true) == SUBREG
4186 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (true))) == 'o'
4187 && GET_RTX_CLASS (GET_CODE (false)) != 'o')
4188 || reg_mentioned_p (true, false)
4189 || rtx_equal_p (false, XEXP (cond, 0))))
4191 true_code = reverse_condition (true_code);
4192 SUBST (XEXP (x, 0),
4193 gen_binary (true_code, GET_MODE (cond), XEXP (cond, 0),
4194 XEXP (cond, 1)));
4196 SUBST (XEXP (x, 1), false);
4197 SUBST (XEXP (x, 2), true);
4199 temp = true, true = false, false = temp, cond = XEXP (x, 0);
4201 /* It is possible that the conditional has been simplified out. */
4202 true_code = GET_CODE (cond);
4203 comparison_p = GET_RTX_CLASS (true_code) == '<';
4206 /* If the two arms are identical, we don't need the comparison. */
4208 if (rtx_equal_p (true, false) && ! side_effects_p (cond))
4209 return true;
4211 /* Convert a == b ? b : a to "a". */
4212 if (true_code == EQ && ! side_effects_p (cond)
4213 && rtx_equal_p (XEXP (cond, 0), false)
4214 && rtx_equal_p (XEXP (cond, 1), true))
4215 return false;
4216 else if (true_code == NE && ! side_effects_p (cond)
4217 && rtx_equal_p (XEXP (cond, 0), true)
4218 && rtx_equal_p (XEXP (cond, 1), false))
4219 return true;
4221 /* Look for cases where we have (abs x) or (neg (abs X)). */
4223 if (GET_MODE_CLASS (mode) == MODE_INT
4224 && GET_CODE (false) == NEG
4225 && rtx_equal_p (true, XEXP (false, 0))
4226 && comparison_p
4227 && rtx_equal_p (true, XEXP (cond, 0))
4228 && ! side_effects_p (true))
4229 switch (true_code)
4231 case GT:
4232 case GE:
4233 return gen_unary (ABS, mode, mode, true);
4234 case LT:
4235 case LE:
4236 return gen_unary (NEG, mode, mode, gen_unary (ABS, mode, mode, true));
4239 /* Look for MIN or MAX. */
4241 if ((! FLOAT_MODE_P (mode) || flag_fast_math)
4242 && comparison_p
4243 && rtx_equal_p (XEXP (cond, 0), true)
4244 && rtx_equal_p (XEXP (cond, 1), false)
4245 && ! side_effects_p (cond))
4246 switch (true_code)
4248 case GE:
4249 case GT:
4250 return gen_binary (SMAX, mode, true, false);
4251 case LE:
4252 case LT:
4253 return gen_binary (SMIN, mode, true, false);
4254 case GEU:
4255 case GTU:
4256 return gen_binary (UMAX, mode, true, false);
4257 case LEU:
4258 case LTU:
4259 return gen_binary (UMIN, mode, true, false);
4262 /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
4263 second operand is zero, this can be done as (OP Z (mult COND C2)) where
4264 C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
4265 SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
4266 We can do this kind of thing in some cases when STORE_FLAG_VALUE is
4267 neither 1 or -1, but it isn't worth checking for. */
4269 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
4270 && comparison_p && mode != VOIDmode && ! side_effects_p (x))
4272 rtx t = make_compound_operation (true, SET);
4273 rtx f = make_compound_operation (false, SET);
4274 rtx cond_op0 = XEXP (cond, 0);
4275 rtx cond_op1 = XEXP (cond, 1);
4276 enum rtx_code op, extend_op = NIL;
4277 enum machine_mode m = mode;
4278 rtx z = 0, c1;
4280 if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
4281 || GET_CODE (t) == IOR || GET_CODE (t) == XOR
4282 || GET_CODE (t) == ASHIFT
4283 || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
4284 && rtx_equal_p (XEXP (t, 0), f))
4285 c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
4287 /* If an identity-zero op is commutative, check whether there
4288 would be a match if we swapped the operands. */
4289 else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
4290 || GET_CODE (t) == XOR)
4291 && rtx_equal_p (XEXP (t, 1), f))
4292 c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
4293 else if (GET_CODE (t) == SIGN_EXTEND
4294 && (GET_CODE (XEXP (t, 0)) == PLUS
4295 || GET_CODE (XEXP (t, 0)) == MINUS
4296 || GET_CODE (XEXP (t, 0)) == IOR
4297 || GET_CODE (XEXP (t, 0)) == XOR
4298 || GET_CODE (XEXP (t, 0)) == ASHIFT
4299 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
4300 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
4301 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
4302 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
4303 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
4304 && (num_sign_bit_copies (f, GET_MODE (f))
4305 > (GET_MODE_BITSIZE (mode)
4306 - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 0))))))
4308 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
4309 extend_op = SIGN_EXTEND;
4310 m = GET_MODE (XEXP (t, 0));
4312 else if (GET_CODE (t) == SIGN_EXTEND
4313 && (GET_CODE (XEXP (t, 0)) == PLUS
4314 || GET_CODE (XEXP (t, 0)) == IOR
4315 || GET_CODE (XEXP (t, 0)) == XOR)
4316 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
4317 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
4318 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
4319 && (num_sign_bit_copies (f, GET_MODE (f))
4320 > (GET_MODE_BITSIZE (mode)
4321 - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 1))))))
4323 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
4324 extend_op = SIGN_EXTEND;
4325 m = GET_MODE (XEXP (t, 0));
4327 else if (GET_CODE (t) == ZERO_EXTEND
4328 && (GET_CODE (XEXP (t, 0)) == PLUS
4329 || GET_CODE (XEXP (t, 0)) == MINUS
4330 || GET_CODE (XEXP (t, 0)) == IOR
4331 || GET_CODE (XEXP (t, 0)) == XOR
4332 || GET_CODE (XEXP (t, 0)) == ASHIFT
4333 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
4334 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
4335 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
4336 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4337 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
4338 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
4339 && ((nonzero_bits (f, GET_MODE (f))
4340 & ~ GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
4341 == 0))
4343 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
4344 extend_op = ZERO_EXTEND;
4345 m = GET_MODE (XEXP (t, 0));
4347 else if (GET_CODE (t) == ZERO_EXTEND
4348 && (GET_CODE (XEXP (t, 0)) == PLUS
4349 || GET_CODE (XEXP (t, 0)) == IOR
4350 || GET_CODE (XEXP (t, 0)) == XOR)
4351 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
4352 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4353 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
4354 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
4355 && ((nonzero_bits (f, GET_MODE (f))
4356 & ~ GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
4357 == 0))
4359 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
4360 extend_op = ZERO_EXTEND;
4361 m = GET_MODE (XEXP (t, 0));
4364 if (z)
4366 temp = subst (gen_binary (true_code, m, cond_op0, cond_op1),
4367 pc_rtx, pc_rtx, 0, 0);
4368 temp = gen_binary (MULT, m, temp,
4369 gen_binary (MULT, m, c1, const_true_rtx));
4370 temp = subst (temp, pc_rtx, pc_rtx, 0, 0);
4371 temp = gen_binary (op, m, gen_lowpart_for_combine (m, z), temp);
4373 if (extend_op != NIL)
4374 temp = gen_unary (extend_op, mode, m, temp);
4376 return temp;
4380 /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
4381 1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
4382 negation of a single bit, we can convert this operation to a shift. We
4383 can actually do this more generally, but it doesn't seem worth it. */
4385 if (true_code == NE && XEXP (cond, 1) == const0_rtx
4386 && false == const0_rtx && GET_CODE (true) == CONST_INT
4387 && ((1 == nonzero_bits (XEXP (cond, 0), mode)
4388 && (i = exact_log2 (INTVAL (true))) >= 0)
4389 || ((num_sign_bit_copies (XEXP (cond, 0), mode)
4390 == GET_MODE_BITSIZE (mode))
4391 && (i = exact_log2 (- INTVAL (true))) >= 0)))
4392 return
4393 simplify_shift_const (NULL_RTX, ASHIFT, mode,
4394 gen_lowpart_for_combine (mode, XEXP (cond, 0)), i);
4396 return x;
4399 /* Simplify X, a SET expression. Return the new expression. */
4401 static rtx
4402 simplify_set (x)
4403 rtx x;
4405 rtx src = SET_SRC (x);
4406 rtx dest = SET_DEST (x);
4407 enum machine_mode mode
4408 = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
4409 rtx other_insn;
4410 rtx *cc_use;
4412 /* (set (pc) (return)) gets written as (return). */
4413 if (GET_CODE (dest) == PC && GET_CODE (src) == RETURN)
4414 return src;
4416 /* Now that we know for sure which bits of SRC we are using, see if we can
4417 simplify the expression for the object knowing that we only need the
4418 low-order bits. */
4420 if (GET_MODE_CLASS (mode) == MODE_INT)
4421 src = force_to_mode (src, mode, GET_MODE_MASK (mode), NULL_RTX, 0);
4423 /* If we are setting CC0 or if the source is a COMPARE, look for the use of
4424 the comparison result and try to simplify it unless we already have used
4425 undobuf.other_insn. */
4426 if ((GET_CODE (src) == COMPARE
4427 #ifdef HAVE_cc0
4428 || dest == cc0_rtx
4429 #endif
4431 && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
4432 && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
4433 && GET_RTX_CLASS (GET_CODE (*cc_use)) == '<'
4434 && rtx_equal_p (XEXP (*cc_use, 0), dest))
4436 enum rtx_code old_code = GET_CODE (*cc_use);
4437 enum rtx_code new_code;
4438 rtx op0, op1;
4439 int other_changed = 0;
4440 enum machine_mode compare_mode = GET_MODE (dest);
4442 if (GET_CODE (src) == COMPARE)
4443 op0 = XEXP (src, 0), op1 = XEXP (src, 1);
4444 else
4445 op0 = src, op1 = const0_rtx;
4447 /* Simplify our comparison, if possible. */
4448 new_code = simplify_comparison (old_code, &op0, &op1);
4450 #ifdef EXTRA_CC_MODES
4451 /* If this machine has CC modes other than CCmode, check to see if we
4452 need to use a different CC mode here. */
4453 compare_mode = SELECT_CC_MODE (new_code, op0, op1);
4454 #endif /* EXTRA_CC_MODES */
4456 #if !defined (HAVE_cc0) && defined (EXTRA_CC_MODES)
4457 /* If the mode changed, we have to change SET_DEST, the mode in the
4458 compare, and the mode in the place SET_DEST is used. If SET_DEST is
4459 a hard register, just build new versions with the proper mode. If it
4460 is a pseudo, we lose unless it is only time we set the pseudo, in
4461 which case we can safely change its mode. */
4462 if (compare_mode != GET_MODE (dest))
4464 int regno = REGNO (dest);
4465 rtx new_dest = gen_rtx (REG, compare_mode, regno);
4467 if (regno < FIRST_PSEUDO_REGISTER
4468 || (REG_N_SETS (regno) == 1 && ! REG_USERVAR_P (dest)))
4470 if (regno >= FIRST_PSEUDO_REGISTER)
4471 SUBST (regno_reg_rtx[regno], new_dest);
4473 SUBST (SET_DEST (x), new_dest);
4474 SUBST (XEXP (*cc_use, 0), new_dest);
4475 other_changed = 1;
4477 dest = new_dest;
4480 #endif
4482 /* If the code changed, we have to build a new comparison in
4483 undobuf.other_insn. */
4484 if (new_code != old_code)
4486 unsigned HOST_WIDE_INT mask;
4488 SUBST (*cc_use, gen_rtx_combine (new_code, GET_MODE (*cc_use),
4489 dest, const0_rtx));
4491 /* If the only change we made was to change an EQ into an NE or
4492 vice versa, OP0 has only one bit that might be nonzero, and OP1
4493 is zero, check if changing the user of the condition code will
4494 produce a valid insn. If it won't, we can keep the original code
4495 in that insn by surrounding our operation with an XOR. */
4497 if (((old_code == NE && new_code == EQ)
4498 || (old_code == EQ && new_code == NE))
4499 && ! other_changed && op1 == const0_rtx
4500 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
4501 && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
4503 rtx pat = PATTERN (other_insn), note = 0;
4504 int scratches;
4506 if ((recog_for_combine (&pat, other_insn, &note, &scratches) < 0
4507 && ! check_asm_operands (pat)))
4509 PUT_CODE (*cc_use, old_code);
4510 other_insn = 0;
4512 op0 = gen_binary (XOR, GET_MODE (op0), op0, GEN_INT (mask));
4516 other_changed = 1;
4519 if (other_changed)
4520 undobuf.other_insn = other_insn;
4522 #ifdef HAVE_cc0
4523 /* If we are now comparing against zero, change our source if
4524 needed. If we do not use cc0, we always have a COMPARE. */
4525 if (op1 == const0_rtx && dest == cc0_rtx)
4527 SUBST (SET_SRC (x), op0);
4528 src = op0;
4530 else
4531 #endif
4533 /* Otherwise, if we didn't previously have a COMPARE in the
4534 correct mode, we need one. */
4535 if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
4537 SUBST (SET_SRC (x),
4538 gen_rtx_combine (COMPARE, compare_mode, op0, op1));
4539 src = SET_SRC (x);
4541 else
4543 /* Otherwise, update the COMPARE if needed. */
4544 SUBST (XEXP (src, 0), op0);
4545 SUBST (XEXP (src, 1), op1);
4548 else
4550 /* Get SET_SRC in a form where we have placed back any
4551 compound expressions. Then do the checks below. */
4552 src = make_compound_operation (src, SET);
4553 SUBST (SET_SRC (x), src);
4556 /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
4557 and X being a REG or (subreg (reg)), we may be able to convert this to
4558 (set (subreg:m2 x) (op)).
4560 We can always do this if M1 is narrower than M2 because that means that
4561 we only care about the low bits of the result.
4563 However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
4564 perform a narrower operation that requested since the high-order bits will
4565 be undefined. On machine where it is defined, this transformation is safe
4566 as long as M1 and M2 have the same number of words. */
4568 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
4569 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (src))) != 'o'
4570 && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
4571 / UNITS_PER_WORD)
4572 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
4573 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
4574 #ifndef WORD_REGISTER_OPERATIONS
4575 && (GET_MODE_SIZE (GET_MODE (src))
4576 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
4577 #endif
4578 #ifdef CLASS_CANNOT_CHANGE_SIZE
4579 && ! (GET_CODE (dest) == REG && REGNO (dest) < FIRST_PSEUDO_REGISTER
4580 && (TEST_HARD_REG_BIT
4581 (reg_class_contents[(int) CLASS_CANNOT_CHANGE_SIZE],
4582 REGNO (dest)))
4583 && (GET_MODE_SIZE (GET_MODE (src))
4584 != GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))))
4585 #endif
4586 && (GET_CODE (dest) == REG
4587 || (GET_CODE (dest) == SUBREG
4588 && GET_CODE (SUBREG_REG (dest)) == REG)))
4590 SUBST (SET_DEST (x),
4591 gen_lowpart_for_combine (GET_MODE (SUBREG_REG (src)),
4592 dest));
4593 SUBST (SET_SRC (x), SUBREG_REG (src));
4595 src = SET_SRC (x), dest = SET_DEST (x);
4598 #ifdef LOAD_EXTEND_OP
4599 /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
4600 would require a paradoxical subreg. Replace the subreg with a
4601 zero_extend to avoid the reload that would otherwise be required. */
4603 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
4604 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != NIL
4605 && SUBREG_WORD (src) == 0
4606 && (GET_MODE_SIZE (GET_MODE (src))
4607 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
4608 && GET_CODE (SUBREG_REG (src)) == MEM)
4610 SUBST (SET_SRC (x),
4611 gen_rtx_combine (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
4612 GET_MODE (src), XEXP (src, 0)));
4614 src = SET_SRC (x);
4616 #endif
4618 /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
4619 are comparing an item known to be 0 or -1 against 0, use a logical
4620 operation instead. Check for one of the arms being an IOR of the other
4621 arm with some value. We compute three terms to be IOR'ed together. In
4622 practice, at most two will be nonzero. Then we do the IOR's. */
4624 if (GET_CODE (dest) != PC
4625 && GET_CODE (src) == IF_THEN_ELSE
4626 && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
4627 && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
4628 && XEXP (XEXP (src, 0), 1) == const0_rtx
4629 && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
4630 #ifdef HAVE_conditional_move
4631 && ! can_conditionally_move_p (GET_MODE (src))
4632 #endif
4633 && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
4634 GET_MODE (XEXP (XEXP (src, 0), 0)))
4635 == GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (src, 0), 0))))
4636 && ! side_effects_p (src))
4638 rtx true = (GET_CODE (XEXP (src, 0)) == NE
4639 ? XEXP (src, 1) : XEXP (src, 2));
4640 rtx false = (GET_CODE (XEXP (src, 0)) == NE
4641 ? XEXP (src, 2) : XEXP (src, 1));
4642 rtx term1 = const0_rtx, term2, term3;
4644 if (GET_CODE (true) == IOR && rtx_equal_p (XEXP (true, 0), false))
4645 term1 = false, true = XEXP (true, 1), false = const0_rtx;
4646 else if (GET_CODE (true) == IOR
4647 && rtx_equal_p (XEXP (true, 1), false))
4648 term1 = false, true = XEXP (true, 0), false = const0_rtx;
4649 else if (GET_CODE (false) == IOR
4650 && rtx_equal_p (XEXP (false, 0), true))
4651 term1 = true, false = XEXP (false, 1), true = const0_rtx;
4652 else if (GET_CODE (false) == IOR
4653 && rtx_equal_p (XEXP (false, 1), true))
4654 term1 = true, false = XEXP (false, 0), true = const0_rtx;
4656 term2 = gen_binary (AND, GET_MODE (src), XEXP (XEXP (src, 0), 0), true);
4657 term3 = gen_binary (AND, GET_MODE (src),
4658 gen_unary (NOT, GET_MODE (src), GET_MODE (src),
4659 XEXP (XEXP (src, 0), 0)),
4660 false);
4662 SUBST (SET_SRC (x),
4663 gen_binary (IOR, GET_MODE (src),
4664 gen_binary (IOR, GET_MODE (src), term1, term2),
4665 term3));
4667 src = SET_SRC (x);
4670 /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
4671 whole thing fail. */
4672 if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
4673 return src;
4674 else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
4675 return dest;
4676 else
4677 /* Convert this into a field assignment operation, if possible. */
4678 return make_field_assignment (x);
4681 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
4682 result. LAST is nonzero if this is the last retry. */
4684 static rtx
4685 simplify_logical (x, last)
4686 rtx x;
4687 int last;
4689 enum machine_mode mode = GET_MODE (x);
4690 rtx op0 = XEXP (x, 0);
4691 rtx op1 = XEXP (x, 1);
4693 switch (GET_CODE (x))
4695 case AND:
4696 /* Convert (A ^ B) & A to A & (~ B) since the latter is often a single
4697 insn (and may simplify more). */
4698 if (GET_CODE (op0) == XOR
4699 && rtx_equal_p (XEXP (op0, 0), op1)
4700 && ! side_effects_p (op1))
4701 x = gen_binary (AND, mode,
4702 gen_unary (NOT, mode, mode, XEXP (op0, 1)), op1);
4704 if (GET_CODE (op0) == XOR
4705 && rtx_equal_p (XEXP (op0, 1), op1)
4706 && ! side_effects_p (op1))
4707 x = gen_binary (AND, mode,
4708 gen_unary (NOT, mode, mode, XEXP (op0, 0)), op1);
4710 /* Similarly for (~ (A ^ B)) & A. */
4711 if (GET_CODE (op0) == NOT
4712 && GET_CODE (XEXP (op0, 0)) == XOR
4713 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), op1)
4714 && ! side_effects_p (op1))
4715 x = gen_binary (AND, mode, XEXP (XEXP (op0, 0), 1), op1);
4717 if (GET_CODE (op0) == NOT
4718 && GET_CODE (XEXP (op0, 0)) == XOR
4719 && rtx_equal_p (XEXP (XEXP (op0, 0), 1), op1)
4720 && ! side_effects_p (op1))
4721 x = gen_binary (AND, mode, XEXP (XEXP (op0, 0), 0), op1);
4723 if (GET_CODE (op1) == CONST_INT)
4725 x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
4727 /* If we have (ior (and (X C1) C2)) and the next restart would be
4728 the last, simplify this by making C1 as small as possible
4729 and then exit. */
4730 if (last
4731 && GET_CODE (x) == IOR && GET_CODE (op0) == AND
4732 && GET_CODE (XEXP (op0, 1)) == CONST_INT
4733 && GET_CODE (op1) == CONST_INT)
4734 return gen_binary (IOR, mode,
4735 gen_binary (AND, mode, XEXP (op0, 0),
4736 GEN_INT (INTVAL (XEXP (op0, 1))
4737 & ~ INTVAL (op1))), op1);
4739 if (GET_CODE (x) != AND)
4740 return x;
4742 if (GET_RTX_CLASS (GET_CODE (x)) == 'c'
4743 || GET_RTX_CLASS (GET_CODE (x)) == '2')
4744 op0 = XEXP (x, 0), op1 = XEXP (x, 1);
4747 /* Convert (A | B) & A to A. */
4748 if (GET_CODE (op0) == IOR
4749 && (rtx_equal_p (XEXP (op0, 0), op1)
4750 || rtx_equal_p (XEXP (op0, 1), op1))
4751 && ! side_effects_p (XEXP (op0, 0))
4752 && ! side_effects_p (XEXP (op0, 1)))
4753 return op1;
4755 /* In the following group of tests (and those in case IOR below),
4756 we start with some combination of logical operations and apply
4757 the distributive law followed by the inverse distributive law.
4758 Most of the time, this results in no change. However, if some of
4759 the operands are the same or inverses of each other, simplifications
4760 will result.
4762 For example, (and (ior A B) (not B)) can occur as the result of
4763 expanding a bit field assignment. When we apply the distributive
4764 law to this, we get (ior (and (A (not B))) (and (B (not B)))),
4765 which then simplifies to (and (A (not B))).
4767 If we have (and (ior A B) C), apply the distributive law and then
4768 the inverse distributive law to see if things simplify. */
4770 if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
4772 x = apply_distributive_law
4773 (gen_binary (GET_CODE (op0), mode,
4774 gen_binary (AND, mode, XEXP (op0, 0), op1),
4775 gen_binary (AND, mode, XEXP (op0, 1), op1)));
4776 if (GET_CODE (x) != AND)
4777 return x;
4780 if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
4781 return apply_distributive_law
4782 (gen_binary (GET_CODE (op1), mode,
4783 gen_binary (AND, mode, XEXP (op1, 0), op0),
4784 gen_binary (AND, mode, XEXP (op1, 1), op0)));
4786 /* Similarly, taking advantage of the fact that
4787 (and (not A) (xor B C)) == (xor (ior A B) (ior A C)) */
4789 if (GET_CODE (op0) == NOT && GET_CODE (op1) == XOR)
4790 return apply_distributive_law
4791 (gen_binary (XOR, mode,
4792 gen_binary (IOR, mode, XEXP (op0, 0), XEXP (op1, 0)),
4793 gen_binary (IOR, mode, XEXP (op0, 0), XEXP (op1, 1))));
4795 else if (GET_CODE (op1) == NOT && GET_CODE (op0) == XOR)
4796 return apply_distributive_law
4797 (gen_binary (XOR, mode,
4798 gen_binary (IOR, mode, XEXP (op1, 0), XEXP (op0, 0)),
4799 gen_binary (IOR, mode, XEXP (op1, 0), XEXP (op0, 1))));
4800 break;
4802 case IOR:
4803 /* (ior A C) is C if all bits of A that might be nonzero are on in C. */
4804 if (GET_CODE (op1) == CONST_INT
4805 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4806 && (nonzero_bits (op0, mode) & ~ INTVAL (op1)) == 0)
4807 return op1;
4809 /* Convert (A & B) | A to A. */
4810 if (GET_CODE (op0) == AND
4811 && (rtx_equal_p (XEXP (op0, 0), op1)
4812 || rtx_equal_p (XEXP (op0, 1), op1))
4813 && ! side_effects_p (XEXP (op0, 0))
4814 && ! side_effects_p (XEXP (op0, 1)))
4815 return op1;
4817 /* If we have (ior (and A B) C), apply the distributive law and then
4818 the inverse distributive law to see if things simplify. */
4820 if (GET_CODE (op0) == AND)
4822 x = apply_distributive_law
4823 (gen_binary (AND, mode,
4824 gen_binary (IOR, mode, XEXP (op0, 0), op1),
4825 gen_binary (IOR, mode, XEXP (op0, 1), op1)));
4827 if (GET_CODE (x) != IOR)
4828 return x;
4831 if (GET_CODE (op1) == AND)
4833 x = apply_distributive_law
4834 (gen_binary (AND, mode,
4835 gen_binary (IOR, mode, XEXP (op1, 0), op0),
4836 gen_binary (IOR, mode, XEXP (op1, 1), op0)));
4838 if (GET_CODE (x) != IOR)
4839 return x;
4842 /* Convert (ior (ashift A CX) (lshiftrt A CY)) where CX+CY equals the
4843 mode size to (rotate A CX). */
4845 if (((GET_CODE (op0) == ASHIFT && GET_CODE (op1) == LSHIFTRT)
4846 || (GET_CODE (op1) == ASHIFT && GET_CODE (op0) == LSHIFTRT))
4847 && rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
4848 && GET_CODE (XEXP (op0, 1)) == CONST_INT
4849 && GET_CODE (XEXP (op1, 1)) == CONST_INT
4850 && (INTVAL (XEXP (op0, 1)) + INTVAL (XEXP (op1, 1))
4851 == GET_MODE_BITSIZE (mode)))
4852 return gen_rtx (ROTATE, mode, XEXP (op0, 0),
4853 (GET_CODE (op0) == ASHIFT
4854 ? XEXP (op0, 1) : XEXP (op1, 1)));
4856 /* If OP0 is (ashiftrt (plus ...) C), it might actually be
4857 a (sign_extend (plus ...)). If so, OP1 is a CONST_INT, and the PLUS
4858 does not affect any of the bits in OP1, it can really be done
4859 as a PLUS and we can associate. We do this by seeing if OP1
4860 can be safely shifted left C bits. */
4861 if (GET_CODE (op1) == CONST_INT && GET_CODE (op0) == ASHIFTRT
4862 && GET_CODE (XEXP (op0, 0)) == PLUS
4863 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
4864 && GET_CODE (XEXP (op0, 1)) == CONST_INT
4865 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT)
4867 int count = INTVAL (XEXP (op0, 1));
4868 HOST_WIDE_INT mask = INTVAL (op1) << count;
4870 if (mask >> count == INTVAL (op1)
4871 && (mask & nonzero_bits (XEXP (op0, 0), mode)) == 0)
4873 SUBST (XEXP (XEXP (op0, 0), 1),
4874 GEN_INT (INTVAL (XEXP (XEXP (op0, 0), 1)) | mask));
4875 return op0;
4878 break;
4880 case XOR:
4881 /* Convert (XOR (NOT x) (NOT y)) to (XOR x y).
4882 Also convert (XOR (NOT x) y) to (NOT (XOR x y)), similarly for
4883 (NOT y). */
4885 int num_negated = 0;
4887 if (GET_CODE (op0) == NOT)
4888 num_negated++, op0 = XEXP (op0, 0);
4889 if (GET_CODE (op1) == NOT)
4890 num_negated++, op1 = XEXP (op1, 0);
4892 if (num_negated == 2)
4894 SUBST (XEXP (x, 0), op0);
4895 SUBST (XEXP (x, 1), op1);
4897 else if (num_negated == 1)
4898 return gen_unary (NOT, mode, mode, gen_binary (XOR, mode, op0, op1));
4901 /* Convert (xor (and A B) B) to (and (not A) B). The latter may
4902 correspond to a machine insn or result in further simplifications
4903 if B is a constant. */
4905 if (GET_CODE (op0) == AND
4906 && rtx_equal_p (XEXP (op0, 1), op1)
4907 && ! side_effects_p (op1))
4908 return gen_binary (AND, mode,
4909 gen_unary (NOT, mode, mode, XEXP (op0, 0)),
4910 op1);
4912 else if (GET_CODE (op0) == AND
4913 && rtx_equal_p (XEXP (op0, 0), op1)
4914 && ! side_effects_p (op1))
4915 return gen_binary (AND, mode,
4916 gen_unary (NOT, mode, mode, XEXP (op0, 1)),
4917 op1);
4919 /* (xor (comparison foo bar) (const_int 1)) can become the reversed
4920 comparison if STORE_FLAG_VALUE is 1. */
4921 if (STORE_FLAG_VALUE == 1
4922 && op1 == const1_rtx
4923 && GET_RTX_CLASS (GET_CODE (op0)) == '<'
4924 && reversible_comparison_p (op0))
4925 return gen_rtx_combine (reverse_condition (GET_CODE (op0)),
4926 mode, XEXP (op0, 0), XEXP (op0, 1));
4928 /* (lshiftrt foo C) where C is the number of bits in FOO minus 1
4929 is (lt foo (const_int 0)), so we can perform the above
4930 simplification if STORE_FLAG_VALUE is 1. */
4932 if (STORE_FLAG_VALUE == 1
4933 && op1 == const1_rtx
4934 && GET_CODE (op0) == LSHIFTRT
4935 && GET_CODE (XEXP (op0, 1)) == CONST_INT
4936 && INTVAL (XEXP (op0, 1)) == GET_MODE_BITSIZE (mode) - 1)
4937 return gen_rtx_combine (GE, mode, XEXP (op0, 0), const0_rtx);
4939 /* (xor (comparison foo bar) (const_int sign-bit))
4940 when STORE_FLAG_VALUE is the sign bit. */
4941 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4942 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
4943 == (HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
4944 && op1 == const_true_rtx
4945 && GET_RTX_CLASS (GET_CODE (op0)) == '<'
4946 && reversible_comparison_p (op0))
4947 return gen_rtx_combine (reverse_condition (GET_CODE (op0)),
4948 mode, XEXP (op0, 0), XEXP (op0, 1));
4949 break;
4952 return x;
4955 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
4956 operations" because they can be replaced with two more basic operations.
4957 ZERO_EXTEND is also considered "compound" because it can be replaced with
4958 an AND operation, which is simpler, though only one operation.
4960 The function expand_compound_operation is called with an rtx expression
4961 and will convert it to the appropriate shifts and AND operations,
4962 simplifying at each stage.
4964 The function make_compound_operation is called to convert an expression
4965 consisting of shifts and ANDs into the equivalent compound expression.
4966 It is the inverse of this function, loosely speaking. */
4968 static rtx
4969 expand_compound_operation (x)
4970 rtx x;
4972 int pos = 0, len;
4973 int unsignedp = 0;
4974 int modewidth;
4975 rtx tem;
4977 switch (GET_CODE (x))
4979 case ZERO_EXTEND:
4980 unsignedp = 1;
4981 case SIGN_EXTEND:
4982 /* We can't necessarily use a const_int for a multiword mode;
4983 it depends on implicitly extending the value.
4984 Since we don't know the right way to extend it,
4985 we can't tell whether the implicit way is right.
4987 Even for a mode that is no wider than a const_int,
4988 we can't win, because we need to sign extend one of its bits through
4989 the rest of it, and we don't know which bit. */
4990 if (GET_CODE (XEXP (x, 0)) == CONST_INT)
4991 return x;
4993 /* Return if (subreg:MODE FROM 0) is not a safe replacement for
4994 (zero_extend:MODE FROM) or (sign_extend:MODE FROM). It is for any MEM
4995 because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
4996 reloaded. If not for that, MEM's would very rarely be safe.
4998 Reject MODEs bigger than a word, because we might not be able
4999 to reference a two-register group starting with an arbitrary register
5000 (and currently gen_lowpart might crash for a SUBREG). */
5002 if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
5003 return x;
5005 len = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)));
5006 /* If the inner object has VOIDmode (the only way this can happen
5007 is if it is a ASM_OPERANDS), we can't do anything since we don't
5008 know how much masking to do. */
5009 if (len == 0)
5010 return x;
5012 break;
5014 case ZERO_EXTRACT:
5015 unsignedp = 1;
5016 case SIGN_EXTRACT:
5017 /* If the operand is a CLOBBER, just return it. */
5018 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
5019 return XEXP (x, 0);
5021 if (GET_CODE (XEXP (x, 1)) != CONST_INT
5022 || GET_CODE (XEXP (x, 2)) != CONST_INT
5023 || GET_MODE (XEXP (x, 0)) == VOIDmode)
5024 return x;
5026 len = INTVAL (XEXP (x, 1));
5027 pos = INTVAL (XEXP (x, 2));
5029 /* If this goes outside the object being extracted, replace the object
5030 with a (use (mem ...)) construct that only combine understands
5031 and is used only for this purpose. */
5032 if (len + pos > GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
5033 SUBST (XEXP (x, 0), gen_rtx (USE, GET_MODE (x), XEXP (x, 0)));
5035 if (BITS_BIG_ENDIAN)
5036 pos = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - len - pos;
5038 break;
5040 default:
5041 return x;
5044 /* We can optimize some special cases of ZERO_EXTEND. */
5045 if (GET_CODE (x) == ZERO_EXTEND)
5047 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
5048 know that the last value didn't have any inappropriate bits
5049 set. */
5050 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
5051 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
5052 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5053 && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
5054 & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5055 return XEXP (XEXP (x, 0), 0);
5057 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
5058 if (GET_CODE (XEXP (x, 0)) == SUBREG
5059 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
5060 && subreg_lowpart_p (XEXP (x, 0))
5061 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5062 && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
5063 & ~ GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))) == 0)
5064 return SUBREG_REG (XEXP (x, 0));
5066 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
5067 is a comparison and STORE_FLAG_VALUE permits. This is like
5068 the first case, but it works even when GET_MODE (x) is larger
5069 than HOST_WIDE_INT. */
5070 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
5071 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
5072 && GET_RTX_CLASS (GET_CODE (XEXP (XEXP (x, 0), 0))) == '<'
5073 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5074 <= HOST_BITS_PER_WIDE_INT)
5075 && ((HOST_WIDE_INT) STORE_FLAG_VALUE
5076 & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5077 return XEXP (XEXP (x, 0), 0);
5079 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
5080 if (GET_CODE (XEXP (x, 0)) == SUBREG
5081 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
5082 && subreg_lowpart_p (XEXP (x, 0))
5083 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0)))) == '<'
5084 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5085 <= HOST_BITS_PER_WIDE_INT)
5086 && ((HOST_WIDE_INT) STORE_FLAG_VALUE
5087 & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5088 return SUBREG_REG (XEXP (x, 0));
5090 /* If sign extension is cheaper than zero extension, then use it
5091 if we know that no extraneous bits are set, and that the high
5092 bit is not set. */
5093 if (flag_expensive_optimizations
5094 && ((GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5095 && ((nonzero_bits (XEXP (x, 0), GET_MODE (x))
5096 & ~ (((unsigned HOST_WIDE_INT)
5097 GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
5098 >> 1))
5099 == 0))
5100 || (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
5101 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5102 <= HOST_BITS_PER_WIDE_INT)
5103 && (((HOST_WIDE_INT) STORE_FLAG_VALUE
5104 & ~ (((unsigned HOST_WIDE_INT)
5105 GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
5106 >> 1))
5107 == 0))))
5109 rtx temp = gen_rtx (SIGN_EXTEND, GET_MODE (x), XEXP (x, 0));
5111 if (rtx_cost (temp, SET) < rtx_cost (x, SET))
5112 return expand_compound_operation (temp);
5116 /* If we reach here, we want to return a pair of shifts. The inner
5117 shift is a left shift of BITSIZE - POS - LEN bits. The outer
5118 shift is a right shift of BITSIZE - LEN bits. It is arithmetic or
5119 logical depending on the value of UNSIGNEDP.
5121 If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
5122 converted into an AND of a shift.
5124 We must check for the case where the left shift would have a negative
5125 count. This can happen in a case like (x >> 31) & 255 on machines
5126 that can't shift by a constant. On those machines, we would first
5127 combine the shift with the AND to produce a variable-position
5128 extraction. Then the constant of 31 would be substituted in to produce
5129 a such a position. */
5131 modewidth = GET_MODE_BITSIZE (GET_MODE (x));
5132 if (modewidth >= pos - len)
5133 tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
5134 GET_MODE (x),
5135 simplify_shift_const (NULL_RTX, ASHIFT,
5136 GET_MODE (x),
5137 XEXP (x, 0),
5138 modewidth - pos - len),
5139 modewidth - len);
5141 else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
5142 tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
5143 simplify_shift_const (NULL_RTX, LSHIFTRT,
5144 GET_MODE (x),
5145 XEXP (x, 0), pos),
5146 ((HOST_WIDE_INT) 1 << len) - 1);
5147 else
5148 /* Any other cases we can't handle. */
5149 return x;
5152 /* If we couldn't do this for some reason, return the original
5153 expression. */
5154 if (GET_CODE (tem) == CLOBBER)
5155 return x;
5157 return tem;
5160 /* X is a SET which contains an assignment of one object into
5161 a part of another (such as a bit-field assignment, STRICT_LOW_PART,
5162 or certain SUBREGS). If possible, convert it into a series of
5163 logical operations.
5165 We half-heartedly support variable positions, but do not at all
5166 support variable lengths. */
5168 static rtx
5169 expand_field_assignment (x)
5170 rtx x;
5172 rtx inner;
5173 rtx pos; /* Always counts from low bit. */
5174 int len;
5175 rtx mask;
5176 enum machine_mode compute_mode;
5178 /* Loop until we find something we can't simplify. */
5179 while (1)
5181 if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
5182 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
5184 inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
5185 len = GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)));
5186 pos = GEN_INT (BITS_PER_WORD * SUBREG_WORD (XEXP (SET_DEST (x), 0)));
5188 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
5189 && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT)
5191 inner = XEXP (SET_DEST (x), 0);
5192 len = INTVAL (XEXP (SET_DEST (x), 1));
5193 pos = XEXP (SET_DEST (x), 2);
5195 /* If the position is constant and spans the width of INNER,
5196 surround INNER with a USE to indicate this. */
5197 if (GET_CODE (pos) == CONST_INT
5198 && INTVAL (pos) + len > GET_MODE_BITSIZE (GET_MODE (inner)))
5199 inner = gen_rtx (USE, GET_MODE (SET_DEST (x)), inner);
5201 if (BITS_BIG_ENDIAN)
5203 if (GET_CODE (pos) == CONST_INT)
5204 pos = GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner)) - len
5205 - INTVAL (pos));
5206 else if (GET_CODE (pos) == MINUS
5207 && GET_CODE (XEXP (pos, 1)) == CONST_INT
5208 && (INTVAL (XEXP (pos, 1))
5209 == GET_MODE_BITSIZE (GET_MODE (inner)) - len))
5210 /* If position is ADJUST - X, new position is X. */
5211 pos = XEXP (pos, 0);
5212 else
5213 pos = gen_binary (MINUS, GET_MODE (pos),
5214 GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner))
5215 - len),
5216 pos);
5220 /* A SUBREG between two modes that occupy the same numbers of words
5221 can be done by moving the SUBREG to the source. */
5222 else if (GET_CODE (SET_DEST (x)) == SUBREG
5223 && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
5224 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
5225 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
5226 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
5228 x = gen_rtx (SET, VOIDmode, SUBREG_REG (SET_DEST (x)),
5229 gen_lowpart_for_combine (GET_MODE (SUBREG_REG (SET_DEST (x))),
5230 SET_SRC (x)));
5231 continue;
5233 else
5234 break;
5236 while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
5237 inner = SUBREG_REG (inner);
5239 compute_mode = GET_MODE (inner);
5241 /* Compute a mask of LEN bits, if we can do this on the host machine. */
5242 if (len < HOST_BITS_PER_WIDE_INT)
5243 mask = GEN_INT (((HOST_WIDE_INT) 1 << len) - 1);
5244 else
5245 break;
5247 /* Now compute the equivalent expression. Make a copy of INNER
5248 for the SET_DEST in case it is a MEM into which we will substitute;
5249 we don't want shared RTL in that case. */
5250 x = gen_rtx (SET, VOIDmode, copy_rtx (inner),
5251 gen_binary (IOR, compute_mode,
5252 gen_binary (AND, compute_mode,
5253 gen_unary (NOT, compute_mode,
5254 compute_mode,
5255 gen_binary (ASHIFT,
5256 compute_mode,
5257 mask, pos)),
5258 inner),
5259 gen_binary (ASHIFT, compute_mode,
5260 gen_binary (AND, compute_mode,
5261 gen_lowpart_for_combine
5262 (compute_mode,
5263 SET_SRC (x)),
5264 mask),
5265 pos)));
5268 return x;
5271 /* Return an RTX for a reference to LEN bits of INNER. If POS_RTX is nonzero,
5272 it is an RTX that represents a variable starting position; otherwise,
5273 POS is the (constant) starting bit position (counted from the LSB).
5275 INNER may be a USE. This will occur when we started with a bitfield
5276 that went outside the boundary of the object in memory, which is
5277 allowed on most machines. To isolate this case, we produce a USE
5278 whose mode is wide enough and surround the MEM with it. The only
5279 code that understands the USE is this routine. If it is not removed,
5280 it will cause the resulting insn not to match.
5282 UNSIGNEDP is non-zero for an unsigned reference and zero for a
5283 signed reference.
5285 IN_DEST is non-zero if this is a reference in the destination of a
5286 SET. This is used when a ZERO_ or SIGN_EXTRACT isn't needed. If non-zero,
5287 a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
5288 be used.
5290 IN_COMPARE is non-zero if we are in a COMPARE. This means that a
5291 ZERO_EXTRACT should be built even for bits starting at bit 0.
5293 MODE is the desired mode of the result (if IN_DEST == 0).
5295 The result is an RTX for the extraction or NULL_RTX if the target
5296 can't handle it. */
5298 static rtx
5299 make_extraction (mode, inner, pos, pos_rtx, len,
5300 unsignedp, in_dest, in_compare)
5301 enum machine_mode mode;
5302 rtx inner;
5303 int pos;
5304 rtx pos_rtx;
5305 int len;
5306 int unsignedp;
5307 int in_dest, in_compare;
5309 /* This mode describes the size of the storage area
5310 to fetch the overall value from. Within that, we
5311 ignore the POS lowest bits, etc. */
5312 enum machine_mode is_mode = GET_MODE (inner);
5313 enum machine_mode inner_mode;
5314 enum machine_mode wanted_inner_mode = byte_mode;
5315 enum machine_mode wanted_inner_reg_mode = word_mode;
5316 enum machine_mode pos_mode = word_mode;
5317 enum machine_mode extraction_mode = word_mode;
5318 enum machine_mode tmode = mode_for_size (len, MODE_INT, 1);
5319 int spans_byte = 0;
5320 rtx new = 0;
5321 rtx orig_pos_rtx = pos_rtx;
5322 int orig_pos;
5324 /* Get some information about INNER and get the innermost object. */
5325 if (GET_CODE (inner) == USE)
5326 /* (use:SI (mem:QI foo)) stands for (mem:SI foo). */
5327 /* We don't need to adjust the position because we set up the USE
5328 to pretend that it was a full-word object. */
5329 spans_byte = 1, inner = XEXP (inner, 0);
5330 else if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
5332 /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
5333 consider just the QI as the memory to extract from.
5334 The subreg adds or removes high bits; its mode is
5335 irrelevant to the meaning of this extraction,
5336 since POS and LEN count from the lsb. */
5337 if (GET_CODE (SUBREG_REG (inner)) == MEM)
5338 is_mode = GET_MODE (SUBREG_REG (inner));
5339 inner = SUBREG_REG (inner);
5342 inner_mode = GET_MODE (inner);
5344 if (pos_rtx && GET_CODE (pos_rtx) == CONST_INT)
5345 pos = INTVAL (pos_rtx), pos_rtx = 0;
5347 /* See if this can be done without an extraction. We never can if the
5348 width of the field is not the same as that of some integer mode. For
5349 registers, we can only avoid the extraction if the position is at the
5350 low-order bit and this is either not in the destination or we have the
5351 appropriate STRICT_LOW_PART operation available.
5353 For MEM, we can avoid an extract if the field starts on an appropriate
5354 boundary and we can change the mode of the memory reference. However,
5355 we cannot directly access the MEM if we have a USE and the underlying
5356 MEM is not TMODE. This combination means that MEM was being used in a
5357 context where bits outside its mode were being referenced; that is only
5358 valid in bit-field insns. */
5360 if (tmode != BLKmode
5361 && ! (spans_byte && inner_mode != tmode)
5362 && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
5363 && GET_CODE (inner) != MEM
5364 && (! in_dest
5365 || (GET_CODE (inner) == REG
5366 && (movstrict_optab->handlers[(int) tmode].insn_code
5367 != CODE_FOR_nothing))))
5368 || (GET_CODE (inner) == MEM && pos_rtx == 0
5369 && (pos
5370 % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
5371 : BITS_PER_UNIT)) == 0
5372 /* We can't do this if we are widening INNER_MODE (it
5373 may not be aligned, for one thing). */
5374 && GET_MODE_BITSIZE (inner_mode) >= GET_MODE_BITSIZE (tmode)
5375 && (inner_mode == tmode
5376 || (! mode_dependent_address_p (XEXP (inner, 0))
5377 && ! MEM_VOLATILE_P (inner))))))
5379 /* If INNER is a MEM, make a new MEM that encompasses just the desired
5380 field. If the original and current mode are the same, we need not
5381 adjust the offset. Otherwise, we do if bytes big endian.
5383 If INNER is not a MEM, get a piece consisting of just the field
5384 of interest (in this case POS % BITS_PER_WORD must be 0). */
5386 if (GET_CODE (inner) == MEM)
5388 int offset;
5389 /* POS counts from lsb, but make OFFSET count in memory order. */
5390 if (BYTES_BIG_ENDIAN)
5391 offset = (GET_MODE_BITSIZE (is_mode) - len - pos) / BITS_PER_UNIT;
5392 else
5393 offset = pos / BITS_PER_UNIT;
5395 new = gen_rtx (MEM, tmode, plus_constant (XEXP (inner, 0), offset));
5396 RTX_UNCHANGING_P (new) = RTX_UNCHANGING_P (inner);
5397 MEM_VOLATILE_P (new) = MEM_VOLATILE_P (inner);
5398 MEM_IN_STRUCT_P (new) = MEM_IN_STRUCT_P (inner);
5400 else if (GET_CODE (inner) == REG)
5402 /* We can't call gen_lowpart_for_combine here since we always want
5403 a SUBREG and it would sometimes return a new hard register. */
5404 if (tmode != inner_mode)
5405 new = gen_rtx (SUBREG, tmode, inner,
5406 (WORDS_BIG_ENDIAN
5407 && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD
5408 ? (((GET_MODE_SIZE (inner_mode)
5409 - GET_MODE_SIZE (tmode))
5410 / UNITS_PER_WORD)
5411 - pos / BITS_PER_WORD)
5412 : pos / BITS_PER_WORD));
5413 else
5414 new = inner;
5416 else
5417 new = force_to_mode (inner, tmode,
5418 len >= HOST_BITS_PER_WIDE_INT
5419 ? GET_MODE_MASK (tmode)
5420 : ((HOST_WIDE_INT) 1 << len) - 1,
5421 NULL_RTX, 0);
5423 /* If this extraction is going into the destination of a SET,
5424 make a STRICT_LOW_PART unless we made a MEM. */
5426 if (in_dest)
5427 return (GET_CODE (new) == MEM ? new
5428 : (GET_CODE (new) != SUBREG
5429 ? gen_rtx (CLOBBER, tmode, const0_rtx)
5430 : gen_rtx_combine (STRICT_LOW_PART, VOIDmode, new)));
5432 /* Otherwise, sign- or zero-extend unless we already are in the
5433 proper mode. */
5435 return (mode == tmode ? new
5436 : gen_rtx_combine (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
5437 mode, new));
5440 /* Unless this is a COMPARE or we have a funny memory reference,
5441 don't do anything with zero-extending field extracts starting at
5442 the low-order bit since they are simple AND operations. */
5443 if (pos_rtx == 0 && pos == 0 && ! in_dest
5444 && ! in_compare && ! spans_byte && unsignedp)
5445 return 0;
5447 /* Unless we are allowed to span bytes, reject this if we would be
5448 spanning bytes or if the position is not a constant and the length
5449 is not 1. In all other cases, we would only be going outside
5450 out object in cases when an original shift would have been
5451 undefined. */
5452 if (! spans_byte
5453 && ((pos_rtx == 0 && pos + len > GET_MODE_BITSIZE (is_mode))
5454 || (pos_rtx != 0 && len != 1)))
5455 return 0;
5457 /* Get the mode to use should INNER not be a MEM, the mode for the position,
5458 and the mode for the result. */
5459 #ifdef HAVE_insv
5460 if (in_dest)
5462 wanted_inner_reg_mode = insn_operand_mode[(int) CODE_FOR_insv][0];
5463 pos_mode = insn_operand_mode[(int) CODE_FOR_insv][2];
5464 extraction_mode = insn_operand_mode[(int) CODE_FOR_insv][3];
5466 #endif
5468 #ifdef HAVE_extzv
5469 if (! in_dest && unsignedp)
5471 wanted_inner_reg_mode = insn_operand_mode[(int) CODE_FOR_extzv][1];
5472 pos_mode = insn_operand_mode[(int) CODE_FOR_extzv][3];
5473 extraction_mode = insn_operand_mode[(int) CODE_FOR_extzv][0];
5475 #endif
5477 #ifdef HAVE_extv
5478 if (! in_dest && ! unsignedp)
5480 wanted_inner_reg_mode = insn_operand_mode[(int) CODE_FOR_extv][1];
5481 pos_mode = insn_operand_mode[(int) CODE_FOR_extv][3];
5482 extraction_mode = insn_operand_mode[(int) CODE_FOR_extv][0];
5484 #endif
5486 /* Never narrow an object, since that might not be safe. */
5488 if (mode != VOIDmode
5489 && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
5490 extraction_mode = mode;
5492 if (pos_rtx && GET_MODE (pos_rtx) != VOIDmode
5493 && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
5494 pos_mode = GET_MODE (pos_rtx);
5496 /* If this is not from memory, the desired mode is wanted_inner_reg_mode;
5497 if we have to change the mode of memory and cannot, the desired mode is
5498 EXTRACTION_MODE. */
5499 if (GET_CODE (inner) != MEM)
5500 wanted_inner_mode = wanted_inner_reg_mode;
5501 else if (inner_mode != wanted_inner_mode
5502 && (mode_dependent_address_p (XEXP (inner, 0))
5503 || MEM_VOLATILE_P (inner)))
5504 wanted_inner_mode = extraction_mode;
5506 orig_pos = pos;
5508 if (BITS_BIG_ENDIAN)
5510 /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
5511 BITS_BIG_ENDIAN style. If position is constant, compute new
5512 position. Otherwise, build subtraction.
5513 Note that POS is relative to the mode of the original argument.
5514 If it's a MEM we need to recompute POS relative to that.
5515 However, if we're extracting from (or inserting into) a register,
5516 we want to recompute POS relative to wanted_inner_mode. */
5517 int width = (GET_CODE (inner) == MEM
5518 ? GET_MODE_BITSIZE (is_mode)
5519 : GET_MODE_BITSIZE (wanted_inner_mode));
5521 if (pos_rtx == 0)
5522 pos = width - len - pos;
5523 else
5524 pos_rtx
5525 = gen_rtx_combine (MINUS, GET_MODE (pos_rtx),
5526 GEN_INT (width - len), pos_rtx);
5527 /* POS may be less than 0 now, but we check for that below.
5528 Note that it can only be less than 0 if GET_CODE (inner) != MEM. */
5531 /* If INNER has a wider mode, make it smaller. If this is a constant
5532 extract, try to adjust the byte to point to the byte containing
5533 the value. */
5534 if (wanted_inner_mode != VOIDmode
5535 && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
5536 && ((GET_CODE (inner) == MEM
5537 && (inner_mode == wanted_inner_mode
5538 || (! mode_dependent_address_p (XEXP (inner, 0))
5539 && ! MEM_VOLATILE_P (inner))))))
5541 int offset = 0;
5543 /* The computations below will be correct if the machine is big
5544 endian in both bits and bytes or little endian in bits and bytes.
5545 If it is mixed, we must adjust. */
5547 /* If bytes are big endian and we had a paradoxical SUBREG, we must
5548 adjust OFFSET to compensate. */
5549 if (BYTES_BIG_ENDIAN
5550 && ! spans_byte
5551 && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
5552 offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
5554 /* If this is a constant position, we can move to the desired byte. */
5555 if (pos_rtx == 0)
5557 offset += pos / BITS_PER_UNIT;
5558 pos %= GET_MODE_BITSIZE (wanted_inner_mode);
5561 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
5562 && ! spans_byte
5563 && is_mode != wanted_inner_mode)
5564 offset = (GET_MODE_SIZE (is_mode)
5565 - GET_MODE_SIZE (wanted_inner_mode) - offset);
5567 if (offset != 0 || inner_mode != wanted_inner_mode)
5569 rtx newmem = gen_rtx (MEM, wanted_inner_mode,
5570 plus_constant (XEXP (inner, 0), offset));
5571 RTX_UNCHANGING_P (newmem) = RTX_UNCHANGING_P (inner);
5572 MEM_VOLATILE_P (newmem) = MEM_VOLATILE_P (inner);
5573 MEM_IN_STRUCT_P (newmem) = MEM_IN_STRUCT_P (inner);
5574 inner = newmem;
5578 /* If INNER is not memory, we can always get it into the proper mode. If we
5579 are changing its mode, POS must be a constant and smaller than the size
5580 of the new mode. */
5581 else if (GET_CODE (inner) != MEM)
5583 if (GET_MODE (inner) != wanted_inner_mode
5584 && (pos_rtx != 0
5585 || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
5586 return 0;
5588 inner = force_to_mode (inner, wanted_inner_mode,
5589 pos_rtx
5590 || len + orig_pos >= HOST_BITS_PER_WIDE_INT
5591 ? GET_MODE_MASK (wanted_inner_mode)
5592 : (((HOST_WIDE_INT) 1 << len) - 1) << orig_pos,
5593 NULL_RTX, 0);
5596 /* Adjust mode of POS_RTX, if needed. If we want a wider mode, we
5597 have to zero extend. Otherwise, we can just use a SUBREG. */
5598 if (pos_rtx != 0
5599 && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
5600 pos_rtx = gen_rtx_combine (ZERO_EXTEND, pos_mode, pos_rtx);
5601 else if (pos_rtx != 0
5602 && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
5603 pos_rtx = gen_lowpart_for_combine (pos_mode, pos_rtx);
5605 /* Make POS_RTX unless we already have it and it is correct. If we don't
5606 have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
5607 be a CONST_INT. */
5608 if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
5609 pos_rtx = orig_pos_rtx;
5611 else if (pos_rtx == 0)
5612 pos_rtx = GEN_INT (pos);
5614 /* Make the required operation. See if we can use existing rtx. */
5615 new = gen_rtx_combine (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
5616 extraction_mode, inner, GEN_INT (len), pos_rtx);
5617 if (! in_dest)
5618 new = gen_lowpart_for_combine (mode, new);
5620 return new;
5623 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
5624 with any other operations in X. Return X without that shift if so. */
5626 static rtx
5627 extract_left_shift (x, count)
5628 rtx x;
5629 int count;
5631 enum rtx_code code = GET_CODE (x);
5632 enum machine_mode mode = GET_MODE (x);
5633 rtx tem;
5635 switch (code)
5637 case ASHIFT:
5638 /* This is the shift itself. If it is wide enough, we will return
5639 either the value being shifted if the shift count is equal to
5640 COUNT or a shift for the difference. */
5641 if (GET_CODE (XEXP (x, 1)) == CONST_INT
5642 && INTVAL (XEXP (x, 1)) >= count)
5643 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
5644 INTVAL (XEXP (x, 1)) - count);
5645 break;
5647 case NEG: case NOT:
5648 if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
5649 return gen_unary (code, mode, mode, tem);
5651 break;
5653 case PLUS: case IOR: case XOR: case AND:
5654 /* If we can safely shift this constant and we find the inner shift,
5655 make a new operation. */
5656 if (GET_CODE (XEXP (x,1)) == CONST_INT
5657 && (INTVAL (XEXP (x, 1)) & (((HOST_WIDE_INT) 1 << count)) - 1) == 0
5658 && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
5659 return gen_binary (code, mode, tem,
5660 GEN_INT (INTVAL (XEXP (x, 1)) >> count));
5662 break;
5665 return 0;
5668 /* Look at the expression rooted at X. Look for expressions
5669 equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
5670 Form these expressions.
5672 Return the new rtx, usually just X.
5674 Also, for machines like the Vax that don't have logical shift insns,
5675 try to convert logical to arithmetic shift operations in cases where
5676 they are equivalent. This undoes the canonicalizations to logical
5677 shifts done elsewhere.
5679 We try, as much as possible, to re-use rtl expressions to save memory.
5681 IN_CODE says what kind of expression we are processing. Normally, it is
5682 SET. In a memory address (inside a MEM, PLUS or minus, the latter two
5683 being kludges), it is MEM. When processing the arguments of a comparison
5684 or a COMPARE against zero, it is COMPARE. */
5686 static rtx
5687 make_compound_operation (x, in_code)
5688 rtx x;
5689 enum rtx_code in_code;
5691 enum rtx_code code = GET_CODE (x);
5692 enum machine_mode mode = GET_MODE (x);
5693 int mode_width = GET_MODE_BITSIZE (mode);
5694 rtx rhs, lhs;
5695 enum rtx_code next_code;
5696 int i;
5697 rtx new = 0;
5698 rtx tem;
5699 char *fmt;
5701 /* Select the code to be used in recursive calls. Once we are inside an
5702 address, we stay there. If we have a comparison, set to COMPARE,
5703 but once inside, go back to our default of SET. */
5705 next_code = (code == MEM || code == PLUS || code == MINUS ? MEM
5706 : ((code == COMPARE || GET_RTX_CLASS (code) == '<')
5707 && XEXP (x, 1) == const0_rtx) ? COMPARE
5708 : in_code == COMPARE ? SET : in_code);
5710 /* Process depending on the code of this operation. If NEW is set
5711 non-zero, it will be returned. */
5713 switch (code)
5715 case ASHIFT:
5716 /* Convert shifts by constants into multiplications if inside
5717 an address. */
5718 if (in_code == MEM && GET_CODE (XEXP (x, 1)) == CONST_INT
5719 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
5720 && INTVAL (XEXP (x, 1)) >= 0)
5722 new = make_compound_operation (XEXP (x, 0), next_code);
5723 new = gen_rtx_combine (MULT, mode, new,
5724 GEN_INT ((HOST_WIDE_INT) 1
5725 << INTVAL (XEXP (x, 1))));
5727 break;
5729 case AND:
5730 /* If the second operand is not a constant, we can't do anything
5731 with it. */
5732 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
5733 break;
5735 /* If the constant is a power of two minus one and the first operand
5736 is a logical right shift, make an extraction. */
5737 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
5738 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
5740 new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
5741 new = make_extraction (mode, new, 0, XEXP (XEXP (x, 0), 1), i, 1,
5742 0, in_code == COMPARE);
5745 /* Same as previous, but for (subreg (lshiftrt ...)) in first op. */
5746 else if (GET_CODE (XEXP (x, 0)) == SUBREG
5747 && subreg_lowpart_p (XEXP (x, 0))
5748 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
5749 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
5751 new = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
5752 next_code);
5753 new = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new, 0,
5754 XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
5755 0, in_code == COMPARE);
5757 /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)). */
5758 else if ((GET_CODE (XEXP (x, 0)) == XOR
5759 || GET_CODE (XEXP (x, 0)) == IOR)
5760 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
5761 && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
5762 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
5764 /* Apply the distributive law, and then try to make extractions. */
5765 new = gen_rtx_combine (GET_CODE (XEXP (x, 0)), mode,
5766 gen_rtx (AND, mode, XEXP (XEXP (x, 0), 0),
5767 XEXP (x, 1)),
5768 gen_rtx (AND, mode, XEXP (XEXP (x, 0), 1),
5769 XEXP (x, 1)));
5770 new = make_compound_operation (new, in_code);
5773 /* If we are have (and (rotate X C) M) and C is larger than the number
5774 of bits in M, this is an extraction. */
5776 else if (GET_CODE (XEXP (x, 0)) == ROTATE
5777 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
5778 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0
5779 && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
5781 new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
5782 new = make_extraction (mode, new,
5783 (GET_MODE_BITSIZE (mode)
5784 - INTVAL (XEXP (XEXP (x, 0), 1))),
5785 NULL_RTX, i, 1, 0, in_code == COMPARE);
5788 /* On machines without logical shifts, if the operand of the AND is
5789 a logical shift and our mask turns off all the propagated sign
5790 bits, we can replace the logical shift with an arithmetic shift. */
5791 else if (ashr_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing
5792 && (lshr_optab->handlers[(int) mode].insn_code
5793 == CODE_FOR_nothing)
5794 && GET_CODE (XEXP (x, 0)) == LSHIFTRT
5795 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
5796 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
5797 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
5798 && mode_width <= HOST_BITS_PER_WIDE_INT)
5800 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
5802 mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
5803 if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
5804 SUBST (XEXP (x, 0),
5805 gen_rtx_combine (ASHIFTRT, mode,
5806 make_compound_operation (XEXP (XEXP (x, 0), 0),
5807 next_code),
5808 XEXP (XEXP (x, 0), 1)));
5811 /* If the constant is one less than a power of two, this might be
5812 representable by an extraction even if no shift is present.
5813 If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
5814 we are in a COMPARE. */
5815 else if ((i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
5816 new = make_extraction (mode,
5817 make_compound_operation (XEXP (x, 0),
5818 next_code),
5819 0, NULL_RTX, i, 1, 0, in_code == COMPARE);
5821 /* If we are in a comparison and this is an AND with a power of two,
5822 convert this into the appropriate bit extract. */
5823 else if (in_code == COMPARE
5824 && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
5825 new = make_extraction (mode,
5826 make_compound_operation (XEXP (x, 0),
5827 next_code),
5828 i, NULL_RTX, 1, 1, 0, 1);
5830 break;
5832 case LSHIFTRT:
5833 /* If the sign bit is known to be zero, replace this with an
5834 arithmetic shift. */
5835 if (ashr_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing
5836 && lshr_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing
5837 && mode_width <= HOST_BITS_PER_WIDE_INT
5838 && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
5840 new = gen_rtx_combine (ASHIFTRT, mode,
5841 make_compound_operation (XEXP (x, 0),
5842 next_code),
5843 XEXP (x, 1));
5844 break;
5847 /* ... fall through ... */
5849 case ASHIFTRT:
5850 lhs = XEXP (x, 0);
5851 rhs = XEXP (x, 1);
5853 /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
5854 this is a SIGN_EXTRACT. */
5855 if (GET_CODE (rhs) == CONST_INT
5856 && GET_CODE (lhs) == ASHIFT
5857 && GET_CODE (XEXP (lhs, 1)) == CONST_INT
5858 && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1)))
5860 new = make_compound_operation (XEXP (lhs, 0), next_code);
5861 new = make_extraction (mode, new,
5862 INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
5863 NULL_RTX, mode_width - INTVAL (rhs),
5864 code == LSHIFTRT, 0, in_code == COMPARE);
5867 /* See if we have operations between an ASHIFTRT and an ASHIFT.
5868 If so, try to merge the shifts into a SIGN_EXTEND. We could
5869 also do this for some cases of SIGN_EXTRACT, but it doesn't
5870 seem worth the effort; the case checked for occurs on Alpha. */
5872 if (GET_RTX_CLASS (GET_CODE (lhs)) != 'o'
5873 && ! (GET_CODE (lhs) == SUBREG
5874 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (lhs))) == 'o'))
5875 && GET_CODE (rhs) == CONST_INT
5876 && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
5877 && (new = extract_left_shift (lhs, INTVAL (rhs))) != 0)
5878 new = make_extraction (mode, make_compound_operation (new, next_code),
5879 0, NULL_RTX, mode_width - INTVAL (rhs),
5880 code == LSHIFTRT, 0, in_code == COMPARE);
5882 break;
5884 case SUBREG:
5885 /* Call ourselves recursively on the inner expression. If we are
5886 narrowing the object and it has a different RTL code from
5887 what it originally did, do this SUBREG as a force_to_mode. */
5889 tem = make_compound_operation (SUBREG_REG (x), in_code);
5890 if (GET_CODE (tem) != GET_CODE (SUBREG_REG (x))
5891 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (tem))
5892 && subreg_lowpart_p (x))
5894 rtx newer = force_to_mode (tem, mode,
5895 GET_MODE_MASK (mode), NULL_RTX, 0);
5897 /* If we have something other than a SUBREG, we might have
5898 done an expansion, so rerun outselves. */
5899 if (GET_CODE (newer) != SUBREG)
5900 newer = make_compound_operation (newer, in_code);
5902 return newer;
5906 if (new)
5908 x = gen_lowpart_for_combine (mode, new);
5909 code = GET_CODE (x);
5912 /* Now recursively process each operand of this operation. */
5913 fmt = GET_RTX_FORMAT (code);
5914 for (i = 0; i < GET_RTX_LENGTH (code); i++)
5915 if (fmt[i] == 'e')
5917 new = make_compound_operation (XEXP (x, i), next_code);
5918 SUBST (XEXP (x, i), new);
5921 return x;
5924 /* Given M see if it is a value that would select a field of bits
5925 within an item, but not the entire word. Return -1 if not.
5926 Otherwise, return the starting position of the field, where 0 is the
5927 low-order bit.
5929 *PLEN is set to the length of the field. */
5931 static int
5932 get_pos_from_mask (m, plen)
5933 unsigned HOST_WIDE_INT m;
5934 int *plen;
5936 /* Get the bit number of the first 1 bit from the right, -1 if none. */
5937 int pos = exact_log2 (m & - m);
5939 if (pos < 0)
5940 return -1;
5942 /* Now shift off the low-order zero bits and see if we have a power of
5943 two minus 1. */
5944 *plen = exact_log2 ((m >> pos) + 1);
5946 if (*plen <= 0)
5947 return -1;
5949 return pos;
5952 /* See if X can be simplified knowing that we will only refer to it in
5953 MODE and will only refer to those bits that are nonzero in MASK.
5954 If other bits are being computed or if masking operations are done
5955 that select a superset of the bits in MASK, they can sometimes be
5956 ignored.
5958 Return a possibly simplified expression, but always convert X to
5959 MODE. If X is a CONST_INT, AND the CONST_INT with MASK.
5961 Also, if REG is non-zero and X is a register equal in value to REG,
5962 replace X with REG.
5964 If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
5965 are all off in X. This is used when X will be complemented, by either
5966 NOT, NEG, or XOR. */
5968 static rtx
5969 force_to_mode (x, mode, mask, reg, just_select)
5970 rtx x;
5971 enum machine_mode mode;
5972 unsigned HOST_WIDE_INT mask;
5973 rtx reg;
5974 int just_select;
5976 enum rtx_code code = GET_CODE (x);
5977 int next_select = just_select || code == XOR || code == NOT || code == NEG;
5978 enum machine_mode op_mode;
5979 unsigned HOST_WIDE_INT fuller_mask, nonzero;
5980 rtx op0, op1, temp;
5982 /* If this is a CALL or ASM_OPERANDS, don't do anything. Some of the
5983 code below will do the wrong thing since the mode of such an
5984 expression is VOIDmode. */
5985 if (code == CALL || code == ASM_OPERANDS)
5986 return x;
5988 /* We want to perform the operation is its present mode unless we know
5989 that the operation is valid in MODE, in which case we do the operation
5990 in MODE. */
5991 op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
5992 && code_to_optab[(int) code] != 0
5993 && (code_to_optab[(int) code]->handlers[(int) mode].insn_code
5994 != CODE_FOR_nothing))
5995 ? mode : GET_MODE (x));
5997 /* It is not valid to do a right-shift in a narrower mode
5998 than the one it came in with. */
5999 if ((code == LSHIFTRT || code == ASHIFTRT)
6000 && GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (GET_MODE (x)))
6001 op_mode = GET_MODE (x);
6003 /* Truncate MASK to fit OP_MODE. */
6004 if (op_mode)
6005 mask &= GET_MODE_MASK (op_mode);
6007 /* When we have an arithmetic operation, or a shift whose count we
6008 do not know, we need to assume that all bit the up to the highest-order
6009 bit in MASK will be needed. This is how we form such a mask. */
6010 if (op_mode)
6011 fuller_mask = (GET_MODE_BITSIZE (op_mode) >= HOST_BITS_PER_WIDE_INT
6012 ? GET_MODE_MASK (op_mode)
6013 : ((HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1)) - 1);
6014 else
6015 fuller_mask = ~ (HOST_WIDE_INT) 0;
6017 /* Determine what bits of X are guaranteed to be (non)zero. */
6018 nonzero = nonzero_bits (x, mode);
6020 /* If none of the bits in X are needed, return a zero. */
6021 if (! just_select && (nonzero & mask) == 0)
6022 return const0_rtx;
6024 /* If X is a CONST_INT, return a new one. Do this here since the
6025 test below will fail. */
6026 if (GET_CODE (x) == CONST_INT)
6028 HOST_WIDE_INT cval = INTVAL (x) & mask;
6029 int width = GET_MODE_BITSIZE (mode);
6031 /* If MODE is narrower that HOST_WIDE_INT and CVAL is a negative
6032 number, sign extend it. */
6033 if (width > 0 && width < HOST_BITS_PER_WIDE_INT
6034 && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
6035 cval |= (HOST_WIDE_INT) -1 << width;
6037 return GEN_INT (cval);
6040 /* If X is narrower than MODE and we want all the bits in X's mode, just
6041 get X in the proper mode. */
6042 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
6043 && (GET_MODE_MASK (GET_MODE (x)) & ~ mask) == 0)
6044 return gen_lowpart_for_combine (mode, x);
6046 /* If we aren't changing the mode, X is not a SUBREG, and all zero bits in
6047 MASK are already known to be zero in X, we need not do anything. */
6048 if (GET_MODE (x) == mode && code != SUBREG && (~ mask & nonzero) == 0)
6049 return x;
6051 switch (code)
6053 case CLOBBER:
6054 /* If X is a (clobber (const_int)), return it since we know we are
6055 generating something that won't match. */
6056 return x;
6058 case USE:
6059 /* X is a (use (mem ..)) that was made from a bit-field extraction that
6060 spanned the boundary of the MEM. If we are now masking so it is
6061 within that boundary, we don't need the USE any more. */
6062 if (! BITS_BIG_ENDIAN
6063 && (mask & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6064 return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
6065 break;
6067 case SIGN_EXTEND:
6068 case ZERO_EXTEND:
6069 case ZERO_EXTRACT:
6070 case SIGN_EXTRACT:
6071 x = expand_compound_operation (x);
6072 if (GET_CODE (x) != code)
6073 return force_to_mode (x, mode, mask, reg, next_select);
6074 break;
6076 case REG:
6077 if (reg != 0 && (rtx_equal_p (get_last_value (reg), x)
6078 || rtx_equal_p (reg, get_last_value (x))))
6079 x = reg;
6080 break;
6082 case SUBREG:
6083 if (subreg_lowpart_p (x)
6084 /* We can ignore the effect of this SUBREG if it narrows the mode or
6085 if the constant masks to zero all the bits the mode doesn't
6086 have. */
6087 && ((GET_MODE_SIZE (GET_MODE (x))
6088 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
6089 || (0 == (mask
6090 & GET_MODE_MASK (GET_MODE (x))
6091 & ~ GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
6092 return force_to_mode (SUBREG_REG (x), mode, mask, reg, next_select);
6093 break;
6095 case AND:
6096 /* If this is an AND with a constant, convert it into an AND
6097 whose constant is the AND of that constant with MASK. If it
6098 remains an AND of MASK, delete it since it is redundant. */
6100 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
6102 x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
6103 mask & INTVAL (XEXP (x, 1)));
6105 /* If X is still an AND, see if it is an AND with a mask that
6106 is just some low-order bits. If so, and it is MASK, we don't
6107 need it. */
6109 if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
6110 && INTVAL (XEXP (x, 1)) == mask)
6111 x = XEXP (x, 0);
6113 /* If it remains an AND, try making another AND with the bits
6114 in the mode mask that aren't in MASK turned on. If the
6115 constant in the AND is wide enough, this might make a
6116 cheaper constant. */
6118 if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
6119 && GET_MODE_MASK (GET_MODE (x)) != mask
6120 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
6122 HOST_WIDE_INT cval = (INTVAL (XEXP (x, 1))
6123 | (GET_MODE_MASK (GET_MODE (x)) & ~ mask));
6124 int width = GET_MODE_BITSIZE (GET_MODE (x));
6125 rtx y;
6127 /* If MODE is narrower that HOST_WIDE_INT and CVAL is a negative
6128 number, sign extend it. */
6129 if (width > 0 && width < HOST_BITS_PER_WIDE_INT
6130 && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
6131 cval |= (HOST_WIDE_INT) -1 << width;
6133 y = gen_binary (AND, GET_MODE (x), XEXP (x, 0), GEN_INT (cval));
6134 if (rtx_cost (y, SET) < rtx_cost (x, SET))
6135 x = y;
6138 break;
6141 goto binop;
6143 case PLUS:
6144 /* In (and (plus FOO C1) M), if M is a mask that just turns off
6145 low-order bits (as in an alignment operation) and FOO is already
6146 aligned to that boundary, mask C1 to that boundary as well.
6147 This may eliminate that PLUS and, later, the AND. */
6150 int width = GET_MODE_BITSIZE (mode);
6151 unsigned HOST_WIDE_INT smask = mask;
6153 /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
6154 number, sign extend it. */
6156 if (width < HOST_BITS_PER_WIDE_INT
6157 && (smask & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
6158 smask |= (HOST_WIDE_INT) -1 << width;
6160 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6161 && exact_log2 (- smask) >= 0
6162 && (nonzero_bits (XEXP (x, 0), mode) & ~ mask) == 0
6163 && (INTVAL (XEXP (x, 1)) & ~ mask) != 0)
6164 return force_to_mode (plus_constant (XEXP (x, 0),
6165 INTVAL (XEXP (x, 1)) & mask),
6166 mode, mask, reg, next_select);
6169 /* ... fall through ... */
6171 case MINUS:
6172 case MULT:
6173 /* For PLUS, MINUS and MULT, we need any bits less significant than the
6174 most significant bit in MASK since carries from those bits will
6175 affect the bits we are interested in. */
6176 mask = fuller_mask;
6177 goto binop;
6179 case IOR:
6180 case XOR:
6181 /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
6182 LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
6183 operation which may be a bitfield extraction. Ensure that the
6184 constant we form is not wider than the mode of X. */
6186 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6187 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6188 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
6189 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
6190 && GET_CODE (XEXP (x, 1)) == CONST_INT
6191 && ((INTVAL (XEXP (XEXP (x, 0), 1))
6192 + floor_log2 (INTVAL (XEXP (x, 1))))
6193 < GET_MODE_BITSIZE (GET_MODE (x)))
6194 && (INTVAL (XEXP (x, 1))
6195 & ~ nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
6197 temp = GEN_INT ((INTVAL (XEXP (x, 1)) & mask)
6198 << INTVAL (XEXP (XEXP (x, 0), 1)));
6199 temp = gen_binary (GET_CODE (x), GET_MODE (x),
6200 XEXP (XEXP (x, 0), 0), temp);
6201 x = gen_binary (LSHIFTRT, GET_MODE (x), temp,
6202 XEXP (XEXP (x, 0), 1));
6203 return force_to_mode (x, mode, mask, reg, next_select);
6206 binop:
6207 /* For most binary operations, just propagate into the operation and
6208 change the mode if we have an operation of that mode. */
6210 op0 = gen_lowpart_for_combine (op_mode,
6211 force_to_mode (XEXP (x, 0), mode, mask,
6212 reg, next_select));
6213 op1 = gen_lowpart_for_combine (op_mode,
6214 force_to_mode (XEXP (x, 1), mode, mask,
6215 reg, next_select));
6217 /* If OP1 is a CONST_INT and X is an IOR or XOR, clear bits outside
6218 MASK since OP1 might have been sign-extended but we never want
6219 to turn on extra bits, since combine might have previously relied
6220 on them being off. */
6221 if (GET_CODE (op1) == CONST_INT && (code == IOR || code == XOR)
6222 && (INTVAL (op1) & mask) != 0)
6223 op1 = GEN_INT (INTVAL (op1) & mask);
6225 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
6226 x = gen_binary (code, op_mode, op0, op1);
6227 break;
6229 case ASHIFT:
6230 /* For left shifts, do the same, but just for the first operand.
6231 However, we cannot do anything with shifts where we cannot
6232 guarantee that the counts are smaller than the size of the mode
6233 because such a count will have a different meaning in a
6234 wider mode. */
6236 if (! (GET_CODE (XEXP (x, 1)) == CONST_INT
6237 && INTVAL (XEXP (x, 1)) >= 0
6238 && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (mode))
6239 && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
6240 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
6241 < (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (mode))))
6242 break;
6244 /* If the shift count is a constant and we can do arithmetic in
6245 the mode of the shift, refine which bits we need. Otherwise, use the
6246 conservative form of the mask. */
6247 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6248 && INTVAL (XEXP (x, 1)) >= 0
6249 && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (op_mode)
6250 && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
6251 mask >>= INTVAL (XEXP (x, 1));
6252 else
6253 mask = fuller_mask;
6255 op0 = gen_lowpart_for_combine (op_mode,
6256 force_to_mode (XEXP (x, 0), op_mode,
6257 mask, reg, next_select));
6259 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
6260 x = gen_binary (code, op_mode, op0, XEXP (x, 1));
6261 break;
6263 case LSHIFTRT:
6264 /* Here we can only do something if the shift count is a constant,
6265 this shift constant is valid for the host, and we can do arithmetic
6266 in OP_MODE. */
6268 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6269 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
6270 && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
6272 rtx inner = XEXP (x, 0);
6274 /* Select the mask of the bits we need for the shift operand. */
6275 mask <<= INTVAL (XEXP (x, 1));
6277 /* We can only change the mode of the shift if we can do arithmetic
6278 in the mode of the shift and MASK is no wider than the width of
6279 OP_MODE. */
6280 if (GET_MODE_BITSIZE (op_mode) > HOST_BITS_PER_WIDE_INT
6281 || (mask & ~ GET_MODE_MASK (op_mode)) != 0)
6282 op_mode = GET_MODE (x);
6284 inner = force_to_mode (inner, op_mode, mask, reg, next_select);
6286 if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
6287 x = gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
6290 /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
6291 shift and AND produces only copies of the sign bit (C2 is one less
6292 than a power of two), we can do this with just a shift. */
6294 if (GET_CODE (x) == LSHIFTRT
6295 && GET_CODE (XEXP (x, 1)) == CONST_INT
6296 && ((INTVAL (XEXP (x, 1))
6297 + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
6298 >= GET_MODE_BITSIZE (GET_MODE (x)))
6299 && exact_log2 (mask + 1) >= 0
6300 && (num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
6301 >= exact_log2 (mask + 1)))
6302 x = gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
6303 GEN_INT (GET_MODE_BITSIZE (GET_MODE (x))
6304 - exact_log2 (mask + 1)));
6305 break;
6307 case ASHIFTRT:
6308 /* If we are just looking for the sign bit, we don't need this shift at
6309 all, even if it has a variable count. */
6310 if (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6311 && (mask == ((HOST_WIDE_INT) 1
6312 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
6313 return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
6315 /* If this is a shift by a constant, get a mask that contains those bits
6316 that are not copies of the sign bit. We then have two cases: If
6317 MASK only includes those bits, this can be a logical shift, which may
6318 allow simplifications. If MASK is a single-bit field not within
6319 those bits, we are requesting a copy of the sign bit and hence can
6320 shift the sign bit to the appropriate location. */
6322 if (GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) >= 0
6323 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
6325 int i = -1;
6327 /* If the considered data is wider then HOST_WIDE_INT, we can't
6328 represent a mask for all its bits in a single scalar.
6329 But we only care about the lower bits, so calculate these. */
6331 if (GET_MODE_BITSIZE (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
6333 nonzero = ~ (HOST_WIDE_INT) 0;
6335 /* GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
6336 is the number of bits a full-width mask would have set.
6337 We need only shift if these are fewer than nonzero can
6338 hold. If not, we must keep all bits set in nonzero. */
6340 if (GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
6341 < HOST_BITS_PER_WIDE_INT)
6342 nonzero >>= INTVAL (XEXP (x, 1))
6343 + HOST_BITS_PER_WIDE_INT
6344 - GET_MODE_BITSIZE (GET_MODE (x)) ;
6346 else
6348 nonzero = GET_MODE_MASK (GET_MODE (x));
6349 nonzero >>= INTVAL (XEXP (x, 1));
6352 if ((mask & ~ nonzero) == 0
6353 || (i = exact_log2 (mask)) >= 0)
6355 x = simplify_shift_const
6356 (x, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
6357 i < 0 ? INTVAL (XEXP (x, 1))
6358 : GET_MODE_BITSIZE (GET_MODE (x)) - 1 - i);
6360 if (GET_CODE (x) != ASHIFTRT)
6361 return force_to_mode (x, mode, mask, reg, next_select);
6365 /* If MASK is 1, convert this to a LSHIFTRT. This can be done
6366 even if the shift count isn't a constant. */
6367 if (mask == 1)
6368 x = gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0), XEXP (x, 1));
6370 /* If this is a sign-extension operation that just affects bits
6371 we don't care about, remove it. Be sure the call above returned
6372 something that is still a shift. */
6374 if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
6375 && GET_CODE (XEXP (x, 1)) == CONST_INT
6376 && INTVAL (XEXP (x, 1)) >= 0
6377 && (INTVAL (XEXP (x, 1))
6378 <= GET_MODE_BITSIZE (GET_MODE (x)) - (floor_log2 (mask) + 1))
6379 && GET_CODE (XEXP (x, 0)) == ASHIFT
6380 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6381 && INTVAL (XEXP (XEXP (x, 0), 1)) == INTVAL (XEXP (x, 1)))
6382 return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
6383 reg, next_select);
6385 break;
6387 case ROTATE:
6388 case ROTATERT:
6389 /* If the shift count is constant and we can do computations
6390 in the mode of X, compute where the bits we care about are.
6391 Otherwise, we can't do anything. Don't change the mode of
6392 the shift or propagate MODE into the shift, though. */
6393 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6394 && INTVAL (XEXP (x, 1)) >= 0)
6396 temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
6397 GET_MODE (x), GEN_INT (mask),
6398 XEXP (x, 1));
6399 if (temp && GET_CODE(temp) == CONST_INT)
6400 SUBST (XEXP (x, 0),
6401 force_to_mode (XEXP (x, 0), GET_MODE (x),
6402 INTVAL (temp), reg, next_select));
6404 break;
6406 case NEG:
6407 /* If we just want the low-order bit, the NEG isn't needed since it
6408 won't change the low-order bit. */
6409 if (mask == 1)
6410 return force_to_mode (XEXP (x, 0), mode, mask, reg, just_select);
6412 /* We need any bits less significant than the most significant bit in
6413 MASK since carries from those bits will affect the bits we are
6414 interested in. */
6415 mask = fuller_mask;
6416 goto unop;
6418 case NOT:
6419 /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
6420 same as the XOR case above. Ensure that the constant we form is not
6421 wider than the mode of X. */
6423 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6424 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6425 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
6426 && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
6427 < GET_MODE_BITSIZE (GET_MODE (x)))
6428 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
6430 temp = GEN_INT (mask << INTVAL (XEXP (XEXP (x, 0), 1)));
6431 temp = gen_binary (XOR, GET_MODE (x), XEXP (XEXP (x, 0), 0), temp);
6432 x = gen_binary (LSHIFTRT, GET_MODE (x), temp, XEXP (XEXP (x, 0), 1));
6434 return force_to_mode (x, mode, mask, reg, next_select);
6437 /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
6438 use the full mask inside the NOT. */
6439 mask = fuller_mask;
6441 unop:
6442 op0 = gen_lowpart_for_combine (op_mode,
6443 force_to_mode (XEXP (x, 0), mode, mask,
6444 reg, next_select));
6445 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
6446 x = gen_unary (code, op_mode, op_mode, op0);
6447 break;
6449 case NE:
6450 /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
6451 in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
6452 which is equal to STORE_FLAG_VALUE. */
6453 if ((mask & ~ STORE_FLAG_VALUE) == 0 && XEXP (x, 1) == const0_rtx
6454 && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
6455 && nonzero_bits (XEXP (x, 0), mode) == STORE_FLAG_VALUE)
6456 return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
6458 break;
6460 case IF_THEN_ELSE:
6461 /* We have no way of knowing if the IF_THEN_ELSE can itself be
6462 written in a narrower mode. We play it safe and do not do so. */
6464 SUBST (XEXP (x, 1),
6465 gen_lowpart_for_combine (GET_MODE (x),
6466 force_to_mode (XEXP (x, 1), mode,
6467 mask, reg, next_select)));
6468 SUBST (XEXP (x, 2),
6469 gen_lowpart_for_combine (GET_MODE (x),
6470 force_to_mode (XEXP (x, 2), mode,
6471 mask, reg,next_select)));
6472 break;
6475 /* Ensure we return a value of the proper mode. */
6476 return gen_lowpart_for_combine (mode, x);
6479 /* Return nonzero if X is an expression that has one of two values depending on
6480 whether some other value is zero or nonzero. In that case, we return the
6481 value that is being tested, *PTRUE is set to the value if the rtx being
6482 returned has a nonzero value, and *PFALSE is set to the other alternative.
6484 If we return zero, we set *PTRUE and *PFALSE to X. */
6486 static rtx
6487 if_then_else_cond (x, ptrue, pfalse)
6488 rtx x;
6489 rtx *ptrue, *pfalse;
6491 enum machine_mode mode = GET_MODE (x);
6492 enum rtx_code code = GET_CODE (x);
6493 int size = GET_MODE_BITSIZE (mode);
6494 rtx cond0, cond1, true0, true1, false0, false1;
6495 unsigned HOST_WIDE_INT nz;
6497 /* If this is a unary operation whose operand has one of two values, apply
6498 our opcode to compute those values. */
6499 if (GET_RTX_CLASS (code) == '1'
6500 && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
6502 *ptrue = gen_unary (code, mode, GET_MODE (XEXP (x, 0)), true0);
6503 *pfalse = gen_unary (code, mode, GET_MODE (XEXP (x, 0)), false0);
6504 return cond0;
6507 /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
6508 make can't possibly match and would suppress other optimizations. */
6509 else if (code == COMPARE)
6512 /* If this is a binary operation, see if either side has only one of two
6513 values. If either one does or if both do and they are conditional on
6514 the same value, compute the new true and false values. */
6515 else if (GET_RTX_CLASS (code) == 'c' || GET_RTX_CLASS (code) == '2'
6516 || GET_RTX_CLASS (code) == '<')
6518 cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
6519 cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
6521 if ((cond0 != 0 || cond1 != 0)
6522 && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
6524 /* If if_then_else_cond returned zero, then true/false are the
6525 same rtl. We must copy one of them to prevent invalid rtl
6526 sharing. */
6527 if (cond0 == 0)
6528 true0 = copy_rtx (true0);
6529 else if (cond1 == 0)
6530 true1 = copy_rtx (true1);
6532 *ptrue = gen_binary (code, mode, true0, true1);
6533 *pfalse = gen_binary (code, mode, false0, false1);
6534 return cond0 ? cond0 : cond1;
6537 /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
6538 operands is zero when the other is non-zero, and vice-versa,
6539 and STORE_FLAG_VALUE is 1 or -1. */
6541 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
6542 && (code == PLUS || code == IOR || code == XOR || code == MINUS
6543 || code == UMAX)
6544 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
6546 rtx op0 = XEXP (XEXP (x, 0), 1);
6547 rtx op1 = XEXP (XEXP (x, 1), 1);
6549 cond0 = XEXP (XEXP (x, 0), 0);
6550 cond1 = XEXP (XEXP (x, 1), 0);
6552 if (GET_RTX_CLASS (GET_CODE (cond0)) == '<'
6553 && GET_RTX_CLASS (GET_CODE (cond1)) == '<'
6554 && reversible_comparison_p (cond1)
6555 && ((GET_CODE (cond0) == reverse_condition (GET_CODE (cond1))
6556 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
6557 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
6558 || ((swap_condition (GET_CODE (cond0))
6559 == reverse_condition (GET_CODE (cond1)))
6560 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
6561 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
6562 && ! side_effects_p (x))
6564 *ptrue = gen_binary (MULT, mode, op0, const_true_rtx);
6565 *pfalse = gen_binary (MULT, mode,
6566 (code == MINUS
6567 ? gen_unary (NEG, mode, mode, op1) : op1),
6568 const_true_rtx);
6569 return cond0;
6573 /* Similarly for MULT, AND and UMIN, execpt that for these the result
6574 is always zero. */
6575 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
6576 && (code == MULT || code == AND || code == UMIN)
6577 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
6579 cond0 = XEXP (XEXP (x, 0), 0);
6580 cond1 = XEXP (XEXP (x, 1), 0);
6582 if (GET_RTX_CLASS (GET_CODE (cond0)) == '<'
6583 && GET_RTX_CLASS (GET_CODE (cond1)) == '<'
6584 && reversible_comparison_p (cond1)
6585 && ((GET_CODE (cond0) == reverse_condition (GET_CODE (cond1))
6586 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
6587 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
6588 || ((swap_condition (GET_CODE (cond0))
6589 == reverse_condition (GET_CODE (cond1)))
6590 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
6591 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
6592 && ! side_effects_p (x))
6594 *ptrue = *pfalse = const0_rtx;
6595 return cond0;
6600 else if (code == IF_THEN_ELSE)
6602 /* If we have IF_THEN_ELSE already, extract the condition and
6603 canonicalize it if it is NE or EQ. */
6604 cond0 = XEXP (x, 0);
6605 *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
6606 if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
6607 return XEXP (cond0, 0);
6608 else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
6610 *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
6611 return XEXP (cond0, 0);
6613 else
6614 return cond0;
6617 /* If X is a normal SUBREG with both inner and outer modes integral,
6618 we can narrow both the true and false values of the inner expression,
6619 if there is a condition. */
6620 else if (code == SUBREG && GET_MODE_CLASS (mode) == MODE_INT
6621 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_INT
6622 && GET_MODE_SIZE (mode) <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))
6623 && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
6624 &true0, &false0)))
6626 *ptrue = force_to_mode (true0, mode, GET_MODE_MASK (mode), NULL_RTX, 0);
6627 *pfalse
6628 = force_to_mode (false0, mode, GET_MODE_MASK (mode), NULL_RTX, 0);
6630 return cond0;
6633 /* If X is a constant, this isn't special and will cause confusions
6634 if we treat it as such. Likewise if it is equivalent to a constant. */
6635 else if (CONSTANT_P (x)
6636 || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
6639 /* If X is known to be either 0 or -1, those are the true and
6640 false values when testing X. */
6641 else if (num_sign_bit_copies (x, mode) == size)
6643 *ptrue = constm1_rtx, *pfalse = const0_rtx;
6644 return x;
6647 /* Likewise for 0 or a single bit. */
6648 else if (exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
6650 *ptrue = GEN_INT (nz), *pfalse = const0_rtx;
6651 return x;
6654 /* Otherwise fail; show no condition with true and false values the same. */
6655 *ptrue = *pfalse = x;
6656 return 0;
6659 /* Return the value of expression X given the fact that condition COND
6660 is known to be true when applied to REG as its first operand and VAL
6661 as its second. X is known to not be shared and so can be modified in
6662 place.
6664 We only handle the simplest cases, and specifically those cases that
6665 arise with IF_THEN_ELSE expressions. */
6667 static rtx
6668 known_cond (x, cond, reg, val)
6669 rtx x;
6670 enum rtx_code cond;
6671 rtx reg, val;
6673 enum rtx_code code = GET_CODE (x);
6674 rtx temp;
6675 char *fmt;
6676 int i, j;
6678 if (side_effects_p (x))
6679 return x;
6681 if (cond == EQ && rtx_equal_p (x, reg))
6682 return val;
6684 /* If X is (abs REG) and we know something about REG's relationship
6685 with zero, we may be able to simplify this. */
6687 if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
6688 switch (cond)
6690 case GE: case GT: case EQ:
6691 return XEXP (x, 0);
6692 case LT: case LE:
6693 return gen_unary (NEG, GET_MODE (XEXP (x, 0)), GET_MODE (XEXP (x, 0)),
6694 XEXP (x, 0));
6697 /* The only other cases we handle are MIN, MAX, and comparisons if the
6698 operands are the same as REG and VAL. */
6700 else if (GET_RTX_CLASS (code) == '<' || GET_RTX_CLASS (code) == 'c')
6702 if (rtx_equal_p (XEXP (x, 0), val))
6703 cond = swap_condition (cond), temp = val, val = reg, reg = temp;
6705 if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
6707 if (GET_RTX_CLASS (code) == '<')
6708 return (comparison_dominates_p (cond, code) ? const_true_rtx
6709 : (comparison_dominates_p (cond,
6710 reverse_condition (code))
6711 ? const0_rtx : x));
6713 else if (code == SMAX || code == SMIN
6714 || code == UMIN || code == UMAX)
6716 int unsignedp = (code == UMIN || code == UMAX);
6718 if (code == SMAX || code == UMAX)
6719 cond = reverse_condition (cond);
6721 switch (cond)
6723 case GE: case GT:
6724 return unsignedp ? x : XEXP (x, 1);
6725 case LE: case LT:
6726 return unsignedp ? x : XEXP (x, 0);
6727 case GEU: case GTU:
6728 return unsignedp ? XEXP (x, 1) : x;
6729 case LEU: case LTU:
6730 return unsignedp ? XEXP (x, 0) : x;
6736 fmt = GET_RTX_FORMAT (code);
6737 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
6739 if (fmt[i] == 'e')
6740 SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
6741 else if (fmt[i] == 'E')
6742 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6743 SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
6744 cond, reg, val));
6747 return x;
6750 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
6751 assignment as a field assignment. */
6753 static int
6754 rtx_equal_for_field_assignment_p (x, y)
6755 rtx x;
6756 rtx y;
6758 rtx last_x, last_y;
6760 if (x == y || rtx_equal_p (x, y))
6761 return 1;
6763 if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
6764 return 0;
6766 /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
6767 Note that all SUBREGs of MEM are paradoxical; otherwise they
6768 would have been rewritten. */
6769 if (GET_CODE (x) == MEM && GET_CODE (y) == SUBREG
6770 && GET_CODE (SUBREG_REG (y)) == MEM
6771 && rtx_equal_p (SUBREG_REG (y),
6772 gen_lowpart_for_combine (GET_MODE (SUBREG_REG (y)), x)))
6773 return 1;
6775 if (GET_CODE (y) == MEM && GET_CODE (x) == SUBREG
6776 && GET_CODE (SUBREG_REG (x)) == MEM
6777 && rtx_equal_p (SUBREG_REG (x),
6778 gen_lowpart_for_combine (GET_MODE (SUBREG_REG (x)), y)))
6779 return 1;
6781 last_x = get_last_value (x);
6782 last_y = get_last_value (y);
6784 return ((last_x != 0
6785 && GET_CODE (last_x) != CLOBBER
6786 && rtx_equal_for_field_assignment_p (last_x, y))
6787 || (last_y != 0
6788 && GET_CODE (last_y) != CLOBBER
6789 && rtx_equal_for_field_assignment_p (x, last_y))
6790 || (last_x != 0 && last_y != 0
6791 && GET_CODE (last_x) != CLOBBER
6792 && GET_CODE (last_y) != CLOBBER
6793 && rtx_equal_for_field_assignment_p (last_x, last_y)));
6796 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
6797 Return that assignment if so.
6799 We only handle the most common cases. */
6801 static rtx
6802 make_field_assignment (x)
6803 rtx x;
6805 rtx dest = SET_DEST (x);
6806 rtx src = SET_SRC (x);
6807 rtx assign;
6808 rtx rhs, lhs;
6809 HOST_WIDE_INT c1;
6810 int pos, len;
6811 rtx other;
6812 enum machine_mode mode;
6814 /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
6815 a clear of a one-bit field. We will have changed it to
6816 (and (rotate (const_int -2) POS) DEST), so check for that. Also check
6817 for a SUBREG. */
6819 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
6820 && GET_CODE (XEXP (XEXP (src, 0), 0)) == CONST_INT
6821 && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
6822 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
6824 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
6825 1, 1, 1, 0);
6826 if (assign != 0)
6827 return gen_rtx (SET, VOIDmode, assign, const0_rtx);
6828 return x;
6831 else if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
6832 && subreg_lowpart_p (XEXP (src, 0))
6833 && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
6834 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
6835 && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
6836 && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
6837 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
6839 assign = make_extraction (VOIDmode, dest, 0,
6840 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
6841 1, 1, 1, 0);
6842 if (assign != 0)
6843 return gen_rtx (SET, VOIDmode, assign, const0_rtx);
6844 return x;
6847 /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
6848 one-bit field. */
6849 else if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
6850 && XEXP (XEXP (src, 0), 0) == const1_rtx
6851 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
6853 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
6854 1, 1, 1, 0);
6855 if (assign != 0)
6856 return gen_rtx (SET, VOIDmode, assign, const1_rtx);
6857 return x;
6860 /* The other case we handle is assignments into a constant-position
6861 field. They look like (ior/xor (and DEST C1) OTHER). If C1 represents
6862 a mask that has all one bits except for a group of zero bits and
6863 OTHER is known to have zeros where C1 has ones, this is such an
6864 assignment. Compute the position and length from C1. Shift OTHER
6865 to the appropriate position, force it to the required mode, and
6866 make the extraction. Check for the AND in both operands. */
6868 if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
6869 return x;
6871 rhs = expand_compound_operation (XEXP (src, 0));
6872 lhs = expand_compound_operation (XEXP (src, 1));
6874 if (GET_CODE (rhs) == AND
6875 && GET_CODE (XEXP (rhs, 1)) == CONST_INT
6876 && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
6877 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
6878 else if (GET_CODE (lhs) == AND
6879 && GET_CODE (XEXP (lhs, 1)) == CONST_INT
6880 && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
6881 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
6882 else
6883 return x;
6885 pos = get_pos_from_mask ((~ c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
6886 if (pos < 0 || pos + len > GET_MODE_BITSIZE (GET_MODE (dest))
6887 || (GET_MODE_BITSIZE (GET_MODE (other)) <= HOST_BITS_PER_WIDE_INT
6888 && (c1 & nonzero_bits (other, GET_MODE (other))) != 0))
6889 return x;
6891 assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
6892 if (assign == 0)
6893 return x;
6895 /* The mode to use for the source is the mode of the assignment, or of
6896 what is inside a possible STRICT_LOW_PART. */
6897 mode = (GET_CODE (assign) == STRICT_LOW_PART
6898 ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
6900 /* Shift OTHER right POS places and make it the source, restricting it
6901 to the proper length and mode. */
6903 src = force_to_mode (simplify_shift_const (NULL_RTX, LSHIFTRT,
6904 GET_MODE (src), other, pos),
6905 mode,
6906 GET_MODE_BITSIZE (mode) >= HOST_BITS_PER_WIDE_INT
6907 ? GET_MODE_MASK (mode)
6908 : ((HOST_WIDE_INT) 1 << len) - 1,
6909 dest, 0);
6911 return gen_rtx_combine (SET, VOIDmode, assign, src);
6914 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
6915 if so. */
6917 static rtx
6918 apply_distributive_law (x)
6919 rtx x;
6921 enum rtx_code code = GET_CODE (x);
6922 rtx lhs, rhs, other;
6923 rtx tem;
6924 enum rtx_code inner_code;
6926 /* Distributivity is not true for floating point.
6927 It can change the value. So don't do it.
6928 -- rms and moshier@world.std.com. */
6929 if (FLOAT_MODE_P (GET_MODE (x)))
6930 return x;
6932 /* The outer operation can only be one of the following: */
6933 if (code != IOR && code != AND && code != XOR
6934 && code != PLUS && code != MINUS)
6935 return x;
6937 lhs = XEXP (x, 0), rhs = XEXP (x, 1);
6939 /* If either operand is a primitive we can't do anything, so get out
6940 fast. */
6941 if (GET_RTX_CLASS (GET_CODE (lhs)) == 'o'
6942 || GET_RTX_CLASS (GET_CODE (rhs)) == 'o')
6943 return x;
6945 lhs = expand_compound_operation (lhs);
6946 rhs = expand_compound_operation (rhs);
6947 inner_code = GET_CODE (lhs);
6948 if (inner_code != GET_CODE (rhs))
6949 return x;
6951 /* See if the inner and outer operations distribute. */
6952 switch (inner_code)
6954 case LSHIFTRT:
6955 case ASHIFTRT:
6956 case AND:
6957 case IOR:
6958 /* These all distribute except over PLUS. */
6959 if (code == PLUS || code == MINUS)
6960 return x;
6961 break;
6963 case MULT:
6964 if (code != PLUS && code != MINUS)
6965 return x;
6966 break;
6968 case ASHIFT:
6969 /* This is also a multiply, so it distributes over everything. */
6970 break;
6972 case SUBREG:
6973 /* Non-paradoxical SUBREGs distributes over all operations, provided
6974 the inner modes and word numbers are the same, this is an extraction
6975 of a low-order part, we don't convert an fp operation to int or
6976 vice versa, and we would not be converting a single-word
6977 operation into a multi-word operation. The latter test is not
6978 required, but it prevents generating unneeded multi-word operations.
6979 Some of the previous tests are redundant given the latter test, but
6980 are retained because they are required for correctness.
6982 We produce the result slightly differently in this case. */
6984 if (GET_MODE (SUBREG_REG (lhs)) != GET_MODE (SUBREG_REG (rhs))
6985 || SUBREG_WORD (lhs) != SUBREG_WORD (rhs)
6986 || ! subreg_lowpart_p (lhs)
6987 || (GET_MODE_CLASS (GET_MODE (lhs))
6988 != GET_MODE_CLASS (GET_MODE (SUBREG_REG (lhs))))
6989 || (GET_MODE_SIZE (GET_MODE (lhs))
6990 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))))
6991 || GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))) > UNITS_PER_WORD)
6992 return x;
6994 tem = gen_binary (code, GET_MODE (SUBREG_REG (lhs)),
6995 SUBREG_REG (lhs), SUBREG_REG (rhs));
6996 return gen_lowpart_for_combine (GET_MODE (x), tem);
6998 default:
6999 return x;
7002 /* Set LHS and RHS to the inner operands (A and B in the example
7003 above) and set OTHER to the common operand (C in the example).
7004 These is only one way to do this unless the inner operation is
7005 commutative. */
7006 if (GET_RTX_CLASS (inner_code) == 'c'
7007 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
7008 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
7009 else if (GET_RTX_CLASS (inner_code) == 'c'
7010 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
7011 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
7012 else if (GET_RTX_CLASS (inner_code) == 'c'
7013 && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
7014 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
7015 else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
7016 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
7017 else
7018 return x;
7020 /* Form the new inner operation, seeing if it simplifies first. */
7021 tem = gen_binary (code, GET_MODE (x), lhs, rhs);
7023 /* There is one exception to the general way of distributing:
7024 (a ^ b) | (a ^ c) -> (~a) & (b ^ c) */
7025 if (code == XOR && inner_code == IOR)
7027 inner_code = AND;
7028 other = gen_unary (NOT, GET_MODE (x), GET_MODE (x), other);
7031 /* We may be able to continuing distributing the result, so call
7032 ourselves recursively on the inner operation before forming the
7033 outer operation, which we return. */
7034 return gen_binary (inner_code, GET_MODE (x),
7035 apply_distributive_law (tem), other);
7038 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
7039 in MODE.
7041 Return an equivalent form, if different from X. Otherwise, return X. If
7042 X is zero, we are to always construct the equivalent form. */
7044 static rtx
7045 simplify_and_const_int (x, mode, varop, constop)
7046 rtx x;
7047 enum machine_mode mode;
7048 rtx varop;
7049 unsigned HOST_WIDE_INT constop;
7051 unsigned HOST_WIDE_INT nonzero;
7052 int width = GET_MODE_BITSIZE (mode);
7053 int i;
7055 /* Simplify VAROP knowing that we will be only looking at some of the
7056 bits in it. */
7057 varop = force_to_mode (varop, mode, constop, NULL_RTX, 0);
7059 /* If VAROP is a CLOBBER, we will fail so return it; if it is a
7060 CONST_INT, we are done. */
7061 if (GET_CODE (varop) == CLOBBER || GET_CODE (varop) == CONST_INT)
7062 return varop;
7064 /* See what bits may be nonzero in VAROP. Unlike the general case of
7065 a call to nonzero_bits, here we don't care about bits outside
7066 MODE. */
7068 nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
7070 /* If this would be an entire word for the target, but is not for
7071 the host, then sign-extend on the host so that the number will look
7072 the same way on the host that it would on the target.
7074 For example, when building a 64 bit alpha hosted 32 bit sparc
7075 targeted compiler, then we want the 32 bit unsigned value -1 to be
7076 represented as a 64 bit value -1, and not as 0x00000000ffffffff.
7077 The later confuses the sparc backend. */
7079 if (BITS_PER_WORD < HOST_BITS_PER_WIDE_INT && BITS_PER_WORD == width
7080 && (nonzero & ((HOST_WIDE_INT) 1 << (width - 1))))
7081 nonzero |= ((HOST_WIDE_INT) (-1) << width);
7083 /* Turn off all bits in the constant that are known to already be zero.
7084 Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
7085 which is tested below. */
7087 constop &= nonzero;
7089 /* If we don't have any bits left, return zero. */
7090 if (constop == 0)
7091 return const0_rtx;
7093 /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
7094 a power of two, we can replace this with a ASHIFT. */
7095 if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
7096 && (i = exact_log2 (constop)) >= 0)
7097 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
7099 /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
7100 or XOR, then try to apply the distributive law. This may eliminate
7101 operations if either branch can be simplified because of the AND.
7102 It may also make some cases more complex, but those cases probably
7103 won't match a pattern either with or without this. */
7105 if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
7106 return
7107 gen_lowpart_for_combine
7108 (mode,
7109 apply_distributive_law
7110 (gen_binary (GET_CODE (varop), GET_MODE (varop),
7111 simplify_and_const_int (NULL_RTX, GET_MODE (varop),
7112 XEXP (varop, 0), constop),
7113 simplify_and_const_int (NULL_RTX, GET_MODE (varop),
7114 XEXP (varop, 1), constop))));
7116 /* Get VAROP in MODE. Try to get a SUBREG if not. Don't make a new SUBREG
7117 if we already had one (just check for the simplest cases). */
7118 if (x && GET_CODE (XEXP (x, 0)) == SUBREG
7119 && GET_MODE (XEXP (x, 0)) == mode
7120 && SUBREG_REG (XEXP (x, 0)) == varop)
7121 varop = XEXP (x, 0);
7122 else
7123 varop = gen_lowpart_for_combine (mode, varop);
7125 /* If we can't make the SUBREG, try to return what we were given. */
7126 if (GET_CODE (varop) == CLOBBER)
7127 return x ? x : varop;
7129 /* If we are only masking insignificant bits, return VAROP. */
7130 if (constop == nonzero)
7131 x = varop;
7133 /* Otherwise, return an AND. See how much, if any, of X we can use. */
7134 else if (x == 0 || GET_CODE (x) != AND || GET_MODE (x) != mode)
7135 x = gen_binary (AND, mode, varop, GEN_INT (constop));
7137 else
7139 if (GET_CODE (XEXP (x, 1)) != CONST_INT
7140 || INTVAL (XEXP (x, 1)) != constop)
7141 SUBST (XEXP (x, 1), GEN_INT (constop));
7143 SUBST (XEXP (x, 0), varop);
7146 return x;
7149 /* We let num_sign_bit_copies recur into nonzero_bits as that is useful.
7150 We don't let nonzero_bits recur into num_sign_bit_copies, because that
7151 is less useful. We can't allow both, because that results in exponential
7152 run time recusion. There is a nullstone testcase that triggered
7153 this. This macro avoids accidental uses of num_sign_bit_copies. */
7154 #define num_sign_bit_copies()
7156 /* Given an expression, X, compute which bits in X can be non-zero.
7157 We don't care about bits outside of those defined in MODE.
7159 For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
7160 a shift, AND, or zero_extract, we can do better. */
7162 static unsigned HOST_WIDE_INT
7163 nonzero_bits (x, mode)
7164 rtx x;
7165 enum machine_mode mode;
7167 unsigned HOST_WIDE_INT nonzero = GET_MODE_MASK (mode);
7168 unsigned HOST_WIDE_INT inner_nz;
7169 enum rtx_code code;
7170 int mode_width = GET_MODE_BITSIZE (mode);
7171 rtx tem;
7173 /* For floating-point values, assume all bits are needed. */
7174 if (FLOAT_MODE_P (GET_MODE (x)) || FLOAT_MODE_P (mode))
7175 return nonzero;
7177 /* If X is wider than MODE, use its mode instead. */
7178 if (GET_MODE_BITSIZE (GET_MODE (x)) > mode_width)
7180 mode = GET_MODE (x);
7181 nonzero = GET_MODE_MASK (mode);
7182 mode_width = GET_MODE_BITSIZE (mode);
7185 if (mode_width > HOST_BITS_PER_WIDE_INT)
7186 /* Our only callers in this case look for single bit values. So
7187 just return the mode mask. Those tests will then be false. */
7188 return nonzero;
7190 #ifndef WORD_REGISTER_OPERATIONS
7191 /* If MODE is wider than X, but both are a single word for both the host
7192 and target machines, we can compute this from which bits of the
7193 object might be nonzero in its own mode, taking into account the fact
7194 that on many CISC machines, accessing an object in a wider mode
7195 causes the high-order bits to become undefined. So they are
7196 not known to be zero. */
7198 if (GET_MODE (x) != VOIDmode && GET_MODE (x) != mode
7199 && GET_MODE_BITSIZE (GET_MODE (x)) <= BITS_PER_WORD
7200 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
7201 && GET_MODE_BITSIZE (mode) > GET_MODE_BITSIZE (GET_MODE (x)))
7203 nonzero &= nonzero_bits (x, GET_MODE (x));
7204 nonzero |= GET_MODE_MASK (mode) & ~ GET_MODE_MASK (GET_MODE (x));
7205 return nonzero;
7207 #endif
7209 code = GET_CODE (x);
7210 switch (code)
7212 case REG:
7213 #ifdef POINTERS_EXTEND_UNSIGNED
7214 /* If pointers extend unsigned and this is a pointer in Pmode, say that
7215 all the bits above ptr_mode are known to be zero. */
7216 if (POINTERS_EXTEND_UNSIGNED && GET_MODE (x) == Pmode
7217 && REGNO_POINTER_FLAG (REGNO (x)))
7218 nonzero &= GET_MODE_MASK (ptr_mode);
7219 #endif
7221 #ifdef STACK_BOUNDARY
7222 /* If this is the stack pointer, we may know something about its
7223 alignment. If PUSH_ROUNDING is defined, it is possible for the
7224 stack to be momentarily aligned only to that amount, so we pick
7225 the least alignment. */
7227 /* We can't check for arg_pointer_rtx here, because it is not
7228 guaranteed to have as much alignment as the stack pointer.
7229 In particular, in the Irix6 n64 ABI, the stack has 128 bit
7230 alignment but the argument pointer has only 64 bit alignment. */
7232 if (x == stack_pointer_rtx || x == frame_pointer_rtx
7233 || x == hard_frame_pointer_rtx
7234 || (REGNO (x) >= FIRST_VIRTUAL_REGISTER
7235 && REGNO (x) <= LAST_VIRTUAL_REGISTER))
7237 int sp_alignment = STACK_BOUNDARY / BITS_PER_UNIT;
7239 #ifdef PUSH_ROUNDING
7240 if (REGNO (x) == STACK_POINTER_REGNUM)
7241 sp_alignment = MIN (PUSH_ROUNDING (1), sp_alignment);
7242 #endif
7244 /* We must return here, otherwise we may get a worse result from
7245 one of the choices below. There is nothing useful below as
7246 far as the stack pointer is concerned. */
7247 return nonzero &= ~ (sp_alignment - 1);
7249 #endif
7251 /* If X is a register whose nonzero bits value is current, use it.
7252 Otherwise, if X is a register whose value we can find, use that
7253 value. Otherwise, use the previously-computed global nonzero bits
7254 for this register. */
7256 if (reg_last_set_value[REGNO (x)] != 0
7257 && reg_last_set_mode[REGNO (x)] == mode
7258 && (REG_N_SETS (REGNO (x)) == 1
7259 || reg_last_set_label[REGNO (x)] == label_tick)
7260 && INSN_CUID (reg_last_set[REGNO (x)]) < subst_low_cuid)
7261 return reg_last_set_nonzero_bits[REGNO (x)];
7263 tem = get_last_value (x);
7265 if (tem)
7267 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
7268 /* If X is narrower than MODE and TEM is a non-negative
7269 constant that would appear negative in the mode of X,
7270 sign-extend it for use in reg_nonzero_bits because some
7271 machines (maybe most) will actually do the sign-extension
7272 and this is the conservative approach.
7274 ??? For 2.5, try to tighten up the MD files in this regard
7275 instead of this kludge. */
7277 if (GET_MODE_BITSIZE (GET_MODE (x)) < mode_width
7278 && GET_CODE (tem) == CONST_INT
7279 && INTVAL (tem) > 0
7280 && 0 != (INTVAL (tem)
7281 & ((HOST_WIDE_INT) 1
7282 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
7283 tem = GEN_INT (INTVAL (tem)
7284 | ((HOST_WIDE_INT) (-1)
7285 << GET_MODE_BITSIZE (GET_MODE (x))));
7286 #endif
7287 return nonzero_bits (tem, mode);
7289 else if (nonzero_sign_valid && reg_nonzero_bits[REGNO (x)])
7290 return reg_nonzero_bits[REGNO (x)] & nonzero;
7291 else
7292 return nonzero;
7294 case CONST_INT:
7295 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
7296 /* If X is negative in MODE, sign-extend the value. */
7297 if (INTVAL (x) > 0 && mode_width < BITS_PER_WORD
7298 && 0 != (INTVAL (x) & ((HOST_WIDE_INT) 1 << (mode_width - 1))))
7299 return (INTVAL (x) | ((HOST_WIDE_INT) (-1) << mode_width));
7300 #endif
7302 return INTVAL (x);
7304 case MEM:
7305 #ifdef LOAD_EXTEND_OP
7306 /* In many, if not most, RISC machines, reading a byte from memory
7307 zeros the rest of the register. Noticing that fact saves a lot
7308 of extra zero-extends. */
7309 if (LOAD_EXTEND_OP (GET_MODE (x)) == ZERO_EXTEND)
7310 nonzero &= GET_MODE_MASK (GET_MODE (x));
7311 #endif
7312 break;
7314 case EQ: case NE:
7315 case GT: case GTU:
7316 case LT: case LTU:
7317 case GE: case GEU:
7318 case LE: case LEU:
7320 /* If this produces an integer result, we know which bits are set.
7321 Code here used to clear bits outside the mode of X, but that is
7322 now done above. */
7324 if (GET_MODE_CLASS (mode) == MODE_INT
7325 && mode_width <= HOST_BITS_PER_WIDE_INT)
7326 nonzero = STORE_FLAG_VALUE;
7327 break;
7329 case NEG:
7330 #if 0
7331 /* Disabled to avoid exponential mutual recursion between nonzero_bits
7332 and num_sign_bit_copies. */
7333 if (num_sign_bit_copies (XEXP (x, 0), GET_MODE (x))
7334 == GET_MODE_BITSIZE (GET_MODE (x)))
7335 nonzero = 1;
7336 #endif
7338 if (GET_MODE_SIZE (GET_MODE (x)) < mode_width)
7339 nonzero |= (GET_MODE_MASK (mode) & ~ GET_MODE_MASK (GET_MODE (x)));
7340 break;
7342 case ABS:
7343 #if 0
7344 /* Disabled to avoid exponential mutual recursion between nonzero_bits
7345 and num_sign_bit_copies. */
7346 if (num_sign_bit_copies (XEXP (x, 0), GET_MODE (x))
7347 == GET_MODE_BITSIZE (GET_MODE (x)))
7348 nonzero = 1;
7349 #endif
7350 break;
7352 case TRUNCATE:
7353 nonzero &= (nonzero_bits (XEXP (x, 0), mode) & GET_MODE_MASK (mode));
7354 break;
7356 case ZERO_EXTEND:
7357 nonzero &= nonzero_bits (XEXP (x, 0), mode);
7358 if (GET_MODE (XEXP (x, 0)) != VOIDmode)
7359 nonzero &= GET_MODE_MASK (GET_MODE (XEXP (x, 0)));
7360 break;
7362 case SIGN_EXTEND:
7363 /* If the sign bit is known clear, this is the same as ZERO_EXTEND.
7364 Otherwise, show all the bits in the outer mode but not the inner
7365 may be non-zero. */
7366 inner_nz = nonzero_bits (XEXP (x, 0), mode);
7367 if (GET_MODE (XEXP (x, 0)) != VOIDmode)
7369 inner_nz &= GET_MODE_MASK (GET_MODE (XEXP (x, 0)));
7370 if (inner_nz
7371 & (((HOST_WIDE_INT) 1
7372 << (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - 1))))
7373 inner_nz |= (GET_MODE_MASK (mode)
7374 & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0))));
7377 nonzero &= inner_nz;
7378 break;
7380 case AND:
7381 nonzero &= (nonzero_bits (XEXP (x, 0), mode)
7382 & nonzero_bits (XEXP (x, 1), mode));
7383 break;
7385 case XOR: case IOR:
7386 case UMIN: case UMAX: case SMIN: case SMAX:
7387 nonzero &= (nonzero_bits (XEXP (x, 0), mode)
7388 | nonzero_bits (XEXP (x, 1), mode));
7389 break;
7391 case PLUS: case MINUS:
7392 case MULT:
7393 case DIV: case UDIV:
7394 case MOD: case UMOD:
7395 /* We can apply the rules of arithmetic to compute the number of
7396 high- and low-order zero bits of these operations. We start by
7397 computing the width (position of the highest-order non-zero bit)
7398 and the number of low-order zero bits for each value. */
7400 unsigned HOST_WIDE_INT nz0 = nonzero_bits (XEXP (x, 0), mode);
7401 unsigned HOST_WIDE_INT nz1 = nonzero_bits (XEXP (x, 1), mode);
7402 int width0 = floor_log2 (nz0) + 1;
7403 int width1 = floor_log2 (nz1) + 1;
7404 int low0 = floor_log2 (nz0 & -nz0);
7405 int low1 = floor_log2 (nz1 & -nz1);
7406 HOST_WIDE_INT op0_maybe_minusp
7407 = (nz0 & ((HOST_WIDE_INT) 1 << (mode_width - 1)));
7408 HOST_WIDE_INT op1_maybe_minusp
7409 = (nz1 & ((HOST_WIDE_INT) 1 << (mode_width - 1)));
7410 int result_width = mode_width;
7411 int result_low = 0;
7413 switch (code)
7415 case PLUS:
7416 result_width = MAX (width0, width1) + 1;
7417 result_low = MIN (low0, low1);
7418 break;
7419 case MINUS:
7420 result_low = MIN (low0, low1);
7421 break;
7422 case MULT:
7423 result_width = width0 + width1;
7424 result_low = low0 + low1;
7425 break;
7426 case DIV:
7427 if (! op0_maybe_minusp && ! op1_maybe_minusp)
7428 result_width = width0;
7429 break;
7430 case UDIV:
7431 result_width = width0;
7432 break;
7433 case MOD:
7434 if (! op0_maybe_minusp && ! op1_maybe_minusp)
7435 result_width = MIN (width0, width1);
7436 result_low = MIN (low0, low1);
7437 break;
7438 case UMOD:
7439 result_width = MIN (width0, width1);
7440 result_low = MIN (low0, low1);
7441 break;
7444 if (result_width < mode_width)
7445 nonzero &= ((HOST_WIDE_INT) 1 << result_width) - 1;
7447 if (result_low > 0)
7448 nonzero &= ~ (((HOST_WIDE_INT) 1 << result_low) - 1);
7450 break;
7452 case ZERO_EXTRACT:
7453 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7454 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
7455 nonzero &= ((HOST_WIDE_INT) 1 << INTVAL (XEXP (x, 1))) - 1;
7456 break;
7458 case SUBREG:
7459 /* If this is a SUBREG formed for a promoted variable that has
7460 been zero-extended, we know that at least the high-order bits
7461 are zero, though others might be too. */
7463 if (SUBREG_PROMOTED_VAR_P (x) && SUBREG_PROMOTED_UNSIGNED_P (x))
7464 nonzero = (GET_MODE_MASK (GET_MODE (x))
7465 & nonzero_bits (SUBREG_REG (x), GET_MODE (x)));
7467 /* If the inner mode is a single word for both the host and target
7468 machines, we can compute this from which bits of the inner
7469 object might be nonzero. */
7470 if (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))) <= BITS_PER_WORD
7471 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x)))
7472 <= HOST_BITS_PER_WIDE_INT))
7474 nonzero &= nonzero_bits (SUBREG_REG (x), mode);
7476 #ifndef WORD_REGISTER_OPERATIONS
7477 /* On many CISC machines, accessing an object in a wider mode
7478 causes the high-order bits to become undefined. So they are
7479 not known to be zero. */
7480 if (GET_MODE_SIZE (GET_MODE (x))
7481 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
7482 nonzero |= (GET_MODE_MASK (GET_MODE (x))
7483 & ~ GET_MODE_MASK (GET_MODE (SUBREG_REG (x))));
7484 #endif
7486 break;
7488 case ASHIFTRT:
7489 case LSHIFTRT:
7490 case ASHIFT:
7491 case ROTATE:
7492 /* The nonzero bits are in two classes: any bits within MODE
7493 that aren't in GET_MODE (x) are always significant. The rest of the
7494 nonzero bits are those that are significant in the operand of
7495 the shift when shifted the appropriate number of bits. This
7496 shows that high-order bits are cleared by the right shift and
7497 low-order bits by left shifts. */
7498 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7499 && INTVAL (XEXP (x, 1)) >= 0
7500 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
7502 enum machine_mode inner_mode = GET_MODE (x);
7503 int width = GET_MODE_BITSIZE (inner_mode);
7504 int count = INTVAL (XEXP (x, 1));
7505 unsigned HOST_WIDE_INT mode_mask = GET_MODE_MASK (inner_mode);
7506 unsigned HOST_WIDE_INT op_nonzero = nonzero_bits (XEXP (x, 0), mode);
7507 unsigned HOST_WIDE_INT inner = op_nonzero & mode_mask;
7508 unsigned HOST_WIDE_INT outer = 0;
7510 if (mode_width > width)
7511 outer = (op_nonzero & nonzero & ~ mode_mask);
7513 if (code == LSHIFTRT)
7514 inner >>= count;
7515 else if (code == ASHIFTRT)
7517 inner >>= count;
7519 /* If the sign bit may have been nonzero before the shift, we
7520 need to mark all the places it could have been copied to
7521 by the shift as possibly nonzero. */
7522 if (inner & ((HOST_WIDE_INT) 1 << (width - 1 - count)))
7523 inner |= (((HOST_WIDE_INT) 1 << count) - 1) << (width - count);
7525 else if (code == ASHIFT)
7526 inner <<= count;
7527 else
7528 inner = ((inner << (count % width)
7529 | (inner >> (width - (count % width)))) & mode_mask);
7531 nonzero &= (outer | inner);
7533 break;
7535 case FFS:
7536 /* This is at most the number of bits in the mode. */
7537 nonzero = ((HOST_WIDE_INT) 1 << (floor_log2 (mode_width) + 1)) - 1;
7538 break;
7540 case IF_THEN_ELSE:
7541 nonzero &= (nonzero_bits (XEXP (x, 1), mode)
7542 | nonzero_bits (XEXP (x, 2), mode));
7543 break;
7546 return nonzero;
7549 /* See the macro definition above. */
7550 #undef num_sign_bit_copies
7552 /* Return the number of bits at the high-order end of X that are known to
7553 be equal to the sign bit. X will be used in mode MODE; if MODE is
7554 VOIDmode, X will be used in its own mode. The returned value will always
7555 be between 1 and the number of bits in MODE. */
7557 static int
7558 num_sign_bit_copies (x, mode)
7559 rtx x;
7560 enum machine_mode mode;
7562 enum rtx_code code = GET_CODE (x);
7563 int bitwidth;
7564 int num0, num1, result;
7565 unsigned HOST_WIDE_INT nonzero;
7566 rtx tem;
7568 /* If we weren't given a mode, use the mode of X. If the mode is still
7569 VOIDmode, we don't know anything. Likewise if one of the modes is
7570 floating-point. */
7572 if (mode == VOIDmode)
7573 mode = GET_MODE (x);
7575 if (mode == VOIDmode || FLOAT_MODE_P (mode) || FLOAT_MODE_P (GET_MODE (x)))
7576 return 1;
7578 bitwidth = GET_MODE_BITSIZE (mode);
7580 /* For a smaller object, just ignore the high bits. */
7581 if (bitwidth < GET_MODE_BITSIZE (GET_MODE (x)))
7582 return MAX (1, (num_sign_bit_copies (x, GET_MODE (x))
7583 - (GET_MODE_BITSIZE (GET_MODE (x)) - bitwidth)));
7585 #ifndef WORD_REGISTER_OPERATIONS
7586 /* If this machine does not do all register operations on the entire
7587 register and MODE is wider than the mode of X, we can say nothing
7588 at all about the high-order bits. */
7589 if (GET_MODE (x) != VOIDmode && bitwidth > GET_MODE_BITSIZE (GET_MODE (x)))
7590 return 1;
7591 #endif
7593 switch (code)
7595 case REG:
7597 #ifdef POINTERS_EXTEND_UNSIGNED
7598 /* If pointers extend signed and this is a pointer in Pmode, say that
7599 all the bits above ptr_mode are known to be sign bit copies. */
7600 if (! POINTERS_EXTEND_UNSIGNED && GET_MODE (x) == Pmode && mode == Pmode
7601 && REGNO_POINTER_FLAG (REGNO (x)))
7602 return GET_MODE_BITSIZE (Pmode) - GET_MODE_BITSIZE (ptr_mode) + 1;
7603 #endif
7605 if (reg_last_set_value[REGNO (x)] != 0
7606 && reg_last_set_mode[REGNO (x)] == mode
7607 && (REG_N_SETS (REGNO (x)) == 1
7608 || reg_last_set_label[REGNO (x)] == label_tick)
7609 && INSN_CUID (reg_last_set[REGNO (x)]) < subst_low_cuid)
7610 return reg_last_set_sign_bit_copies[REGNO (x)];
7612 tem = get_last_value (x);
7613 if (tem != 0)
7614 return num_sign_bit_copies (tem, mode);
7616 if (nonzero_sign_valid && reg_sign_bit_copies[REGNO (x)] != 0)
7617 return reg_sign_bit_copies[REGNO (x)];
7618 break;
7620 case MEM:
7621 #ifdef LOAD_EXTEND_OP
7622 /* Some RISC machines sign-extend all loads of smaller than a word. */
7623 if (LOAD_EXTEND_OP (GET_MODE (x)) == SIGN_EXTEND)
7624 return MAX (1, bitwidth - GET_MODE_BITSIZE (GET_MODE (x)) + 1);
7625 #endif
7626 break;
7628 case CONST_INT:
7629 /* If the constant is negative, take its 1's complement and remask.
7630 Then see how many zero bits we have. */
7631 nonzero = INTVAL (x) & GET_MODE_MASK (mode);
7632 if (bitwidth <= HOST_BITS_PER_WIDE_INT
7633 && (nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
7634 nonzero = (~ nonzero) & GET_MODE_MASK (mode);
7636 return (nonzero == 0 ? bitwidth : bitwidth - floor_log2 (nonzero) - 1);
7638 case SUBREG:
7639 /* If this is a SUBREG for a promoted object that is sign-extended
7640 and we are looking at it in a wider mode, we know that at least the
7641 high-order bits are known to be sign bit copies. */
7643 if (SUBREG_PROMOTED_VAR_P (x) && ! SUBREG_PROMOTED_UNSIGNED_P (x))
7644 return MAX (bitwidth - GET_MODE_BITSIZE (GET_MODE (x)) + 1,
7645 num_sign_bit_copies (SUBREG_REG (x), mode));
7647 /* For a smaller object, just ignore the high bits. */
7648 if (bitwidth <= GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))))
7650 num0 = num_sign_bit_copies (SUBREG_REG (x), VOIDmode);
7651 return MAX (1, (num0
7652 - (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x)))
7653 - bitwidth)));
7656 #ifdef WORD_REGISTER_OPERATIONS
7657 #ifdef LOAD_EXTEND_OP
7658 /* For paradoxical SUBREGs on machines where all register operations
7659 affect the entire register, just look inside. Note that we are
7660 passing MODE to the recursive call, so the number of sign bit copies
7661 will remain relative to that mode, not the inner mode. */
7663 /* This works only if loads sign extend. Otherwise, if we get a
7664 reload for the inner part, it may be loaded from the stack, and
7665 then we lose all sign bit copies that existed before the store
7666 to the stack. */
7668 if ((GET_MODE_SIZE (GET_MODE (x))
7669 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
7670 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (x))) == SIGN_EXTEND)
7671 return num_sign_bit_copies (SUBREG_REG (x), mode);
7672 #endif
7673 #endif
7674 break;
7676 case SIGN_EXTRACT:
7677 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
7678 return MAX (1, bitwidth - INTVAL (XEXP (x, 1)));
7679 break;
7681 case SIGN_EXTEND:
7682 return (bitwidth - GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
7683 + num_sign_bit_copies (XEXP (x, 0), VOIDmode));
7685 case TRUNCATE:
7686 /* For a smaller object, just ignore the high bits. */
7687 num0 = num_sign_bit_copies (XEXP (x, 0), VOIDmode);
7688 return MAX (1, (num0 - (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
7689 - bitwidth)));
7691 case NOT:
7692 return num_sign_bit_copies (XEXP (x, 0), mode);
7694 case ROTATE: case ROTATERT:
7695 /* If we are rotating left by a number of bits less than the number
7696 of sign bit copies, we can just subtract that amount from the
7697 number. */
7698 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7699 && INTVAL (XEXP (x, 1)) >= 0 && INTVAL (XEXP (x, 1)) < bitwidth)
7701 num0 = num_sign_bit_copies (XEXP (x, 0), mode);
7702 return MAX (1, num0 - (code == ROTATE ? INTVAL (XEXP (x, 1))
7703 : bitwidth - INTVAL (XEXP (x, 1))));
7705 break;
7707 case NEG:
7708 /* In general, this subtracts one sign bit copy. But if the value
7709 is known to be positive, the number of sign bit copies is the
7710 same as that of the input. Finally, if the input has just one bit
7711 that might be nonzero, all the bits are copies of the sign bit. */
7712 nonzero = nonzero_bits (XEXP (x, 0), mode);
7713 if (nonzero == 1)
7714 return bitwidth;
7716 num0 = num_sign_bit_copies (XEXP (x, 0), mode);
7717 if (num0 > 1
7718 && bitwidth <= HOST_BITS_PER_WIDE_INT
7719 && (((HOST_WIDE_INT) 1 << (bitwidth - 1)) & nonzero))
7720 num0--;
7722 return num0;
7724 case IOR: case AND: case XOR:
7725 case SMIN: case SMAX: case UMIN: case UMAX:
7726 /* Logical operations will preserve the number of sign-bit copies.
7727 MIN and MAX operations always return one of the operands. */
7728 num0 = num_sign_bit_copies (XEXP (x, 0), mode);
7729 num1 = num_sign_bit_copies (XEXP (x, 1), mode);
7730 return MIN (num0, num1);
7732 case PLUS: case MINUS:
7733 /* For addition and subtraction, we can have a 1-bit carry. However,
7734 if we are subtracting 1 from a positive number, there will not
7735 be such a carry. Furthermore, if the positive number is known to
7736 be 0 or 1, we know the result is either -1 or 0. */
7738 if (code == PLUS && XEXP (x, 1) == constm1_rtx
7739 && bitwidth <= HOST_BITS_PER_WIDE_INT)
7741 nonzero = nonzero_bits (XEXP (x, 0), mode);
7742 if ((((HOST_WIDE_INT) 1 << (bitwidth - 1)) & nonzero) == 0)
7743 return (nonzero == 1 || nonzero == 0 ? bitwidth
7744 : bitwidth - floor_log2 (nonzero) - 1);
7747 num0 = num_sign_bit_copies (XEXP (x, 0), mode);
7748 num1 = num_sign_bit_copies (XEXP (x, 1), mode);
7749 return MAX (1, MIN (num0, num1) - 1);
7751 case MULT:
7752 /* The number of bits of the product is the sum of the number of
7753 bits of both terms. However, unless one of the terms if known
7754 to be positive, we must allow for an additional bit since negating
7755 a negative number can remove one sign bit copy. */
7757 num0 = num_sign_bit_copies (XEXP (x, 0), mode);
7758 num1 = num_sign_bit_copies (XEXP (x, 1), mode);
7760 result = bitwidth - (bitwidth - num0) - (bitwidth - num1);
7761 if (result > 0
7762 && bitwidth <= HOST_BITS_PER_WIDE_INT
7763 && ((nonzero_bits (XEXP (x, 0), mode)
7764 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
7765 && ((nonzero_bits (XEXP (x, 1), mode)
7766 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0))
7767 result--;
7769 return MAX (1, result);
7771 case UDIV:
7772 /* The result must be <= the first operand. */
7773 return num_sign_bit_copies (XEXP (x, 0), mode);
7775 case UMOD:
7776 /* The result must be <= the scond operand. */
7777 return num_sign_bit_copies (XEXP (x, 1), mode);
7779 case DIV:
7780 /* Similar to unsigned division, except that we have to worry about
7781 the case where the divisor is negative, in which case we have
7782 to add 1. */
7783 result = num_sign_bit_copies (XEXP (x, 0), mode);
7784 if (result > 1
7785 && bitwidth <= HOST_BITS_PER_WIDE_INT
7786 && (nonzero_bits (XEXP (x, 1), mode)
7787 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
7788 result --;
7790 return result;
7792 case MOD:
7793 result = num_sign_bit_copies (XEXP (x, 1), mode);
7794 if (result > 1
7795 && bitwidth <= HOST_BITS_PER_WIDE_INT
7796 && (nonzero_bits (XEXP (x, 1), mode)
7797 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
7798 result --;
7800 return result;
7802 case ASHIFTRT:
7803 /* Shifts by a constant add to the number of bits equal to the
7804 sign bit. */
7805 num0 = num_sign_bit_copies (XEXP (x, 0), mode);
7806 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7807 && INTVAL (XEXP (x, 1)) > 0)
7808 num0 = MIN (bitwidth, num0 + INTVAL (XEXP (x, 1)));
7810 return num0;
7812 case ASHIFT:
7813 /* Left shifts destroy copies. */
7814 if (GET_CODE (XEXP (x, 1)) != CONST_INT
7815 || INTVAL (XEXP (x, 1)) < 0
7816 || INTVAL (XEXP (x, 1)) >= bitwidth)
7817 return 1;
7819 num0 = num_sign_bit_copies (XEXP (x, 0), mode);
7820 return MAX (1, num0 - INTVAL (XEXP (x, 1)));
7822 case IF_THEN_ELSE:
7823 num0 = num_sign_bit_copies (XEXP (x, 1), mode);
7824 num1 = num_sign_bit_copies (XEXP (x, 2), mode);
7825 return MIN (num0, num1);
7827 case EQ: case NE: case GE: case GT: case LE: case LT:
7828 case GEU: case GTU: case LEU: case LTU:
7829 if (STORE_FLAG_VALUE == -1)
7830 return bitwidth;
7833 /* If we haven't been able to figure it out by one of the above rules,
7834 see if some of the high-order bits are known to be zero. If so,
7835 count those bits and return one less than that amount. If we can't
7836 safely compute the mask for this mode, always return BITWIDTH. */
7838 if (bitwidth > HOST_BITS_PER_WIDE_INT)
7839 return 1;
7841 nonzero = nonzero_bits (x, mode);
7842 return (nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))
7843 ? 1 : bitwidth - floor_log2 (nonzero) - 1);
7846 /* Return the number of "extended" bits there are in X, when interpreted
7847 as a quantity in MODE whose signedness is indicated by UNSIGNEDP. For
7848 unsigned quantities, this is the number of high-order zero bits.
7849 For signed quantities, this is the number of copies of the sign bit
7850 minus 1. In both case, this function returns the number of "spare"
7851 bits. For example, if two quantities for which this function returns
7852 at least 1 are added, the addition is known not to overflow.
7854 This function will always return 0 unless called during combine, which
7855 implies that it must be called from a define_split. */
7858 extended_count (x, mode, unsignedp)
7859 rtx x;
7860 enum machine_mode mode;
7861 int unsignedp;
7863 if (nonzero_sign_valid == 0)
7864 return 0;
7866 return (unsignedp
7867 ? (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
7868 && (GET_MODE_BITSIZE (mode) - 1
7869 - floor_log2 (nonzero_bits (x, mode))))
7870 : num_sign_bit_copies (x, mode) - 1);
7873 /* This function is called from `simplify_shift_const' to merge two
7874 outer operations. Specifically, we have already found that we need
7875 to perform operation *POP0 with constant *PCONST0 at the outermost
7876 position. We would now like to also perform OP1 with constant CONST1
7877 (with *POP0 being done last).
7879 Return 1 if we can do the operation and update *POP0 and *PCONST0 with
7880 the resulting operation. *PCOMP_P is set to 1 if we would need to
7881 complement the innermost operand, otherwise it is unchanged.
7883 MODE is the mode in which the operation will be done. No bits outside
7884 the width of this mode matter. It is assumed that the width of this mode
7885 is smaller than or equal to HOST_BITS_PER_WIDE_INT.
7887 If *POP0 or OP1 are NIL, it means no operation is required. Only NEG, PLUS,
7888 IOR, XOR, and AND are supported. We may set *POP0 to SET if the proper
7889 result is simply *PCONST0.
7891 If the resulting operation cannot be expressed as one operation, we
7892 return 0 and do not change *POP0, *PCONST0, and *PCOMP_P. */
7894 static int
7895 merge_outer_ops (pop0, pconst0, op1, const1, mode, pcomp_p)
7896 enum rtx_code *pop0;
7897 HOST_WIDE_INT *pconst0;
7898 enum rtx_code op1;
7899 HOST_WIDE_INT const1;
7900 enum machine_mode mode;
7901 int *pcomp_p;
7903 enum rtx_code op0 = *pop0;
7904 HOST_WIDE_INT const0 = *pconst0;
7905 int width = GET_MODE_BITSIZE (mode);
7907 const0 &= GET_MODE_MASK (mode);
7908 const1 &= GET_MODE_MASK (mode);
7910 /* If OP0 is an AND, clear unimportant bits in CONST1. */
7911 if (op0 == AND)
7912 const1 &= const0;
7914 /* If OP0 or OP1 is NIL, this is easy. Similarly if they are the same or
7915 if OP0 is SET. */
7917 if (op1 == NIL || op0 == SET)
7918 return 1;
7920 else if (op0 == NIL)
7921 op0 = op1, const0 = const1;
7923 else if (op0 == op1)
7925 switch (op0)
7927 case AND:
7928 const0 &= const1;
7929 break;
7930 case IOR:
7931 const0 |= const1;
7932 break;
7933 case XOR:
7934 const0 ^= const1;
7935 break;
7936 case PLUS:
7937 const0 += const1;
7938 break;
7939 case NEG:
7940 op0 = NIL;
7941 break;
7945 /* Otherwise, if either is a PLUS or NEG, we can't do anything. */
7946 else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
7947 return 0;
7949 /* If the two constants aren't the same, we can't do anything. The
7950 remaining six cases can all be done. */
7951 else if (const0 != const1)
7952 return 0;
7954 else
7955 switch (op0)
7957 case IOR:
7958 if (op1 == AND)
7959 /* (a & b) | b == b */
7960 op0 = SET;
7961 else /* op1 == XOR */
7962 /* (a ^ b) | b == a | b */
7964 break;
7966 case XOR:
7967 if (op1 == AND)
7968 /* (a & b) ^ b == (~a) & b */
7969 op0 = AND, *pcomp_p = 1;
7970 else /* op1 == IOR */
7971 /* (a | b) ^ b == a & ~b */
7972 op0 = AND, *pconst0 = ~ const0;
7973 break;
7975 case AND:
7976 if (op1 == IOR)
7977 /* (a | b) & b == b */
7978 op0 = SET;
7979 else /* op1 == XOR */
7980 /* (a ^ b) & b) == (~a) & b */
7981 *pcomp_p = 1;
7982 break;
7985 /* Check for NO-OP cases. */
7986 const0 &= GET_MODE_MASK (mode);
7987 if (const0 == 0
7988 && (op0 == IOR || op0 == XOR || op0 == PLUS))
7989 op0 = NIL;
7990 else if (const0 == 0 && op0 == AND)
7991 op0 = SET;
7992 else if (const0 == GET_MODE_MASK (mode) && op0 == AND)
7993 op0 = NIL;
7995 /* If this would be an entire word for the target, but is not for
7996 the host, then sign-extend on the host so that the number will look
7997 the same way on the host that it would on the target.
7999 For example, when building a 64 bit alpha hosted 32 bit sparc
8000 targeted compiler, then we want the 32 bit unsigned value -1 to be
8001 represented as a 64 bit value -1, and not as 0x00000000ffffffff.
8002 The later confuses the sparc backend. */
8004 if (BITS_PER_WORD < HOST_BITS_PER_WIDE_INT && BITS_PER_WORD == width
8005 && (const0 & ((HOST_WIDE_INT) 1 << (width - 1))))
8006 const0 |= ((HOST_WIDE_INT) (-1) << width);
8008 *pop0 = op0;
8009 *pconst0 = const0;
8011 return 1;
8014 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
8015 The result of the shift is RESULT_MODE. X, if non-zero, is an expression
8016 that we started with.
8018 The shift is normally computed in the widest mode we find in VAROP, as
8019 long as it isn't a different number of words than RESULT_MODE. Exceptions
8020 are ASHIFTRT and ROTATE, which are always done in their original mode, */
8022 static rtx
8023 simplify_shift_const (x, code, result_mode, varop, count)
8024 rtx x;
8025 enum rtx_code code;
8026 enum machine_mode result_mode;
8027 rtx varop;
8028 int count;
8030 enum rtx_code orig_code = code;
8031 int orig_count = count;
8032 enum machine_mode mode = result_mode;
8033 enum machine_mode shift_mode, tmode;
8034 int mode_words
8035 = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
8036 /* We form (outer_op (code varop count) (outer_const)). */
8037 enum rtx_code outer_op = NIL;
8038 HOST_WIDE_INT outer_const = 0;
8039 rtx const_rtx;
8040 int complement_p = 0;
8041 rtx new;
8043 /* If we were given an invalid count, don't do anything except exactly
8044 what was requested. */
8046 if (count < 0 || count > GET_MODE_BITSIZE (mode))
8048 if (x)
8049 return x;
8051 return gen_rtx (code, mode, varop, GEN_INT (count));
8054 /* Unless one of the branches of the `if' in this loop does a `continue',
8055 we will `break' the loop after the `if'. */
8057 while (count != 0)
8059 /* If we have an operand of (clobber (const_int 0)), just return that
8060 value. */
8061 if (GET_CODE (varop) == CLOBBER)
8062 return varop;
8064 /* If we discovered we had to complement VAROP, leave. Making a NOT
8065 here would cause an infinite loop. */
8066 if (complement_p)
8067 break;
8069 /* Convert ROTATERT to ROTATE. */
8070 if (code == ROTATERT)
8071 code = ROTATE, count = GET_MODE_BITSIZE (result_mode) - count;
8073 /* We need to determine what mode we will do the shift in. If the
8074 shift is a right shift or a ROTATE, we must always do it in the mode
8075 it was originally done in. Otherwise, we can do it in MODE, the
8076 widest mode encountered. */
8077 shift_mode
8078 = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
8079 ? result_mode : mode);
8081 /* Handle cases where the count is greater than the size of the mode
8082 minus 1. For ASHIFT, use the size minus one as the count (this can
8083 occur when simplifying (lshiftrt (ashiftrt ..))). For rotates,
8084 take the count modulo the size. For other shifts, the result is
8085 zero.
8087 Since these shifts are being produced by the compiler by combining
8088 multiple operations, each of which are defined, we know what the
8089 result is supposed to be. */
8091 if (count > GET_MODE_BITSIZE (shift_mode) - 1)
8093 if (code == ASHIFTRT)
8094 count = GET_MODE_BITSIZE (shift_mode) - 1;
8095 else if (code == ROTATE || code == ROTATERT)
8096 count %= GET_MODE_BITSIZE (shift_mode);
8097 else
8099 /* We can't simply return zero because there may be an
8100 outer op. */
8101 varop = const0_rtx;
8102 count = 0;
8103 break;
8107 /* Negative counts are invalid and should not have been made (a
8108 programmer-specified negative count should have been handled
8109 above). */
8110 else if (count < 0)
8111 abort ();
8113 /* An arithmetic right shift of a quantity known to be -1 or 0
8114 is a no-op. */
8115 if (code == ASHIFTRT
8116 && (num_sign_bit_copies (varop, shift_mode)
8117 == GET_MODE_BITSIZE (shift_mode)))
8119 count = 0;
8120 break;
8123 /* If we are doing an arithmetic right shift and discarding all but
8124 the sign bit copies, this is equivalent to doing a shift by the
8125 bitsize minus one. Convert it into that shift because it will often
8126 allow other simplifications. */
8128 if (code == ASHIFTRT
8129 && (count + num_sign_bit_copies (varop, shift_mode)
8130 >= GET_MODE_BITSIZE (shift_mode)))
8131 count = GET_MODE_BITSIZE (shift_mode) - 1;
8133 /* We simplify the tests below and elsewhere by converting
8134 ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
8135 `make_compound_operation' will convert it to a ASHIFTRT for
8136 those machines (such as Vax) that don't have a LSHIFTRT. */
8137 if (GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
8138 && code == ASHIFTRT
8139 && ((nonzero_bits (varop, shift_mode)
8140 & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (shift_mode) - 1)))
8141 == 0))
8142 code = LSHIFTRT;
8144 switch (GET_CODE (varop))
8146 case SIGN_EXTEND:
8147 case ZERO_EXTEND:
8148 case SIGN_EXTRACT:
8149 case ZERO_EXTRACT:
8150 new = expand_compound_operation (varop);
8151 if (new != varop)
8153 varop = new;
8154 continue;
8156 break;
8158 case MEM:
8159 /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
8160 minus the width of a smaller mode, we can do this with a
8161 SIGN_EXTEND or ZERO_EXTEND from the narrower memory location. */
8162 if ((code == ASHIFTRT || code == LSHIFTRT)
8163 && ! mode_dependent_address_p (XEXP (varop, 0))
8164 && ! MEM_VOLATILE_P (varop)
8165 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
8166 MODE_INT, 1)) != BLKmode)
8168 if (BYTES_BIG_ENDIAN)
8169 new = gen_rtx (MEM, tmode, XEXP (varop, 0));
8170 else
8171 new = gen_rtx (MEM, tmode,
8172 plus_constant (XEXP (varop, 0),
8173 count / BITS_PER_UNIT));
8174 RTX_UNCHANGING_P (new) = RTX_UNCHANGING_P (varop);
8175 MEM_VOLATILE_P (new) = MEM_VOLATILE_P (varop);
8176 MEM_IN_STRUCT_P (new) = MEM_IN_STRUCT_P (varop);
8177 varop = gen_rtx_combine (code == ASHIFTRT ? SIGN_EXTEND
8178 : ZERO_EXTEND, mode, new);
8179 count = 0;
8180 continue;
8182 break;
8184 case USE:
8185 /* Similar to the case above, except that we can only do this if
8186 the resulting mode is the same as that of the underlying
8187 MEM and adjust the address depending on the *bits* endianness
8188 because of the way that bit-field extract insns are defined. */
8189 if ((code == ASHIFTRT || code == LSHIFTRT)
8190 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
8191 MODE_INT, 1)) != BLKmode
8192 && tmode == GET_MODE (XEXP (varop, 0)))
8194 if (BITS_BIG_ENDIAN)
8195 new = XEXP (varop, 0);
8196 else
8198 new = copy_rtx (XEXP (varop, 0));
8199 SUBST (XEXP (new, 0),
8200 plus_constant (XEXP (new, 0),
8201 count / BITS_PER_UNIT));
8204 varop = gen_rtx_combine (code == ASHIFTRT ? SIGN_EXTEND
8205 : ZERO_EXTEND, mode, new);
8206 count = 0;
8207 continue;
8209 break;
8211 case SUBREG:
8212 /* If VAROP is a SUBREG, strip it as long as the inner operand has
8213 the same number of words as what we've seen so far. Then store
8214 the widest mode in MODE. */
8215 if (subreg_lowpart_p (varop)
8216 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
8217 > GET_MODE_SIZE (GET_MODE (varop)))
8218 && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
8219 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
8220 == mode_words))
8222 varop = SUBREG_REG (varop);
8223 if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
8224 mode = GET_MODE (varop);
8225 continue;
8227 break;
8229 case MULT:
8230 /* Some machines use MULT instead of ASHIFT because MULT
8231 is cheaper. But it is still better on those machines to
8232 merge two shifts into one. */
8233 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8234 && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
8236 varop = gen_binary (ASHIFT, GET_MODE (varop), XEXP (varop, 0),
8237 GEN_INT (exact_log2 (INTVAL (XEXP (varop, 1)))));;
8238 continue;
8240 break;
8242 case UDIV:
8243 /* Similar, for when divides are cheaper. */
8244 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8245 && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
8247 varop = gen_binary (LSHIFTRT, GET_MODE (varop), XEXP (varop, 0),
8248 GEN_INT (exact_log2 (INTVAL (XEXP (varop, 1)))));
8249 continue;
8251 break;
8253 case ASHIFTRT:
8254 /* If we are extracting just the sign bit of an arithmetic right
8255 shift, that shift is not needed. */
8256 if (code == LSHIFTRT && count == GET_MODE_BITSIZE (result_mode) - 1)
8258 varop = XEXP (varop, 0);
8259 continue;
8262 /* ... fall through ... */
8264 case LSHIFTRT:
8265 case ASHIFT:
8266 case ROTATE:
8267 /* Here we have two nested shifts. The result is usually the
8268 AND of a new shift with a mask. We compute the result below. */
8269 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8270 && INTVAL (XEXP (varop, 1)) >= 0
8271 && INTVAL (XEXP (varop, 1)) < GET_MODE_BITSIZE (GET_MODE (varop))
8272 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
8273 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
8275 enum rtx_code first_code = GET_CODE (varop);
8276 int first_count = INTVAL (XEXP (varop, 1));
8277 unsigned HOST_WIDE_INT mask;
8278 rtx mask_rtx;
8280 /* We have one common special case. We can't do any merging if
8281 the inner code is an ASHIFTRT of a smaller mode. However, if
8282 we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
8283 with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
8284 we can convert it to
8285 (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0 C2) C3) C1).
8286 This simplifies certain SIGN_EXTEND operations. */
8287 if (code == ASHIFT && first_code == ASHIFTRT
8288 && (GET_MODE_BITSIZE (result_mode)
8289 - GET_MODE_BITSIZE (GET_MODE (varop))) == count)
8291 /* C3 has the low-order C1 bits zero. */
8293 mask = (GET_MODE_MASK (mode)
8294 & ~ (((HOST_WIDE_INT) 1 << first_count) - 1));
8296 varop = simplify_and_const_int (NULL_RTX, result_mode,
8297 XEXP (varop, 0), mask);
8298 varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
8299 varop, count);
8300 count = first_count;
8301 code = ASHIFTRT;
8302 continue;
8305 /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
8306 than C1 high-order bits equal to the sign bit, we can convert
8307 this to either an ASHIFT or a ASHIFTRT depending on the
8308 two counts.
8310 We cannot do this if VAROP's mode is not SHIFT_MODE. */
8312 if (code == ASHIFTRT && first_code == ASHIFT
8313 && GET_MODE (varop) == shift_mode
8314 && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
8315 > first_count))
8317 count -= first_count;
8318 if (count < 0)
8319 count = - count, code = ASHIFT;
8320 varop = XEXP (varop, 0);
8321 continue;
8324 /* There are some cases we can't do. If CODE is ASHIFTRT,
8325 we can only do this if FIRST_CODE is also ASHIFTRT.
8327 We can't do the case when CODE is ROTATE and FIRST_CODE is
8328 ASHIFTRT.
8330 If the mode of this shift is not the mode of the outer shift,
8331 we can't do this if either shift is a right shift or ROTATE.
8333 Finally, we can't do any of these if the mode is too wide
8334 unless the codes are the same.
8336 Handle the case where the shift codes are the same
8337 first. */
8339 if (code == first_code)
8341 if (GET_MODE (varop) != result_mode
8342 && (code == ASHIFTRT || code == LSHIFTRT
8343 || code == ROTATE))
8344 break;
8346 count += first_count;
8347 varop = XEXP (varop, 0);
8348 continue;
8351 if (code == ASHIFTRT
8352 || (code == ROTATE && first_code == ASHIFTRT)
8353 || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT
8354 || (GET_MODE (varop) != result_mode
8355 && (first_code == ASHIFTRT || first_code == LSHIFTRT
8356 || first_code == ROTATE
8357 || code == ROTATE)))
8358 break;
8360 /* To compute the mask to apply after the shift, shift the
8361 nonzero bits of the inner shift the same way the
8362 outer shift will. */
8364 mask_rtx = GEN_INT (nonzero_bits (varop, GET_MODE (varop)));
8366 mask_rtx
8367 = simplify_binary_operation (code, result_mode, mask_rtx,
8368 GEN_INT (count));
8370 /* Give up if we can't compute an outer operation to use. */
8371 if (mask_rtx == 0
8372 || GET_CODE (mask_rtx) != CONST_INT
8373 || ! merge_outer_ops (&outer_op, &outer_const, AND,
8374 INTVAL (mask_rtx),
8375 result_mode, &complement_p))
8376 break;
8378 /* If the shifts are in the same direction, we add the
8379 counts. Otherwise, we subtract them. */
8380 if ((code == ASHIFTRT || code == LSHIFTRT)
8381 == (first_code == ASHIFTRT || first_code == LSHIFTRT))
8382 count += first_count;
8383 else
8384 count -= first_count;
8386 /* If COUNT is positive, the new shift is usually CODE,
8387 except for the two exceptions below, in which case it is
8388 FIRST_CODE. If the count is negative, FIRST_CODE should
8389 always be used */
8390 if (count > 0
8391 && ((first_code == ROTATE && code == ASHIFT)
8392 || (first_code == ASHIFTRT && code == LSHIFTRT)))
8393 code = first_code;
8394 else if (count < 0)
8395 code = first_code, count = - count;
8397 varop = XEXP (varop, 0);
8398 continue;
8401 /* If we have (A << B << C) for any shift, we can convert this to
8402 (A << C << B). This wins if A is a constant. Only try this if
8403 B is not a constant. */
8405 else if (GET_CODE (varop) == code
8406 && GET_CODE (XEXP (varop, 1)) != CONST_INT
8407 && 0 != (new
8408 = simplify_binary_operation (code, mode,
8409 XEXP (varop, 0),
8410 GEN_INT (count))))
8412 varop = gen_rtx_combine (code, mode, new, XEXP (varop, 1));
8413 count = 0;
8414 continue;
8416 break;
8418 case NOT:
8419 /* Make this fit the case below. */
8420 varop = gen_rtx_combine (XOR, mode, XEXP (varop, 0),
8421 GEN_INT (GET_MODE_MASK (mode)));
8422 continue;
8424 case IOR:
8425 case AND:
8426 case XOR:
8427 /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
8428 with C the size of VAROP - 1 and the shift is logical if
8429 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
8430 we have an (le X 0) operation. If we have an arithmetic shift
8431 and STORE_FLAG_VALUE is 1 or we have a logical shift with
8432 STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation. */
8434 if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
8435 && XEXP (XEXP (varop, 0), 1) == constm1_rtx
8436 && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8437 && (code == LSHIFTRT || code == ASHIFTRT)
8438 && count == GET_MODE_BITSIZE (GET_MODE (varop)) - 1
8439 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
8441 count = 0;
8442 varop = gen_rtx_combine (LE, GET_MODE (varop), XEXP (varop, 1),
8443 const0_rtx);
8445 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
8446 varop = gen_rtx_combine (NEG, GET_MODE (varop), varop);
8448 continue;
8451 /* If we have (shift (logical)), move the logical to the outside
8452 to allow it to possibly combine with another logical and the
8453 shift to combine with another shift. This also canonicalizes to
8454 what a ZERO_EXTRACT looks like. Also, some machines have
8455 (and (shift)) insns. */
8457 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8458 && (new = simplify_binary_operation (code, result_mode,
8459 XEXP (varop, 1),
8460 GEN_INT (count))) != 0
8461 && GET_CODE(new) == CONST_INT
8462 && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
8463 INTVAL (new), result_mode, &complement_p))
8465 varop = XEXP (varop, 0);
8466 continue;
8469 /* If we can't do that, try to simplify the shift in each arm of the
8470 logical expression, make a new logical expression, and apply
8471 the inverse distributive law. */
8473 rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
8474 XEXP (varop, 0), count);
8475 rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
8476 XEXP (varop, 1), count);
8478 varop = gen_binary (GET_CODE (varop), shift_mode, lhs, rhs);
8479 varop = apply_distributive_law (varop);
8481 count = 0;
8483 break;
8485 case EQ:
8486 /* convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
8487 says that the sign bit can be tested, FOO has mode MODE, C is
8488 GET_MODE_BITSIZE (MODE) - 1, and FOO has only its low-order bit
8489 that may be nonzero. */
8490 if (code == LSHIFTRT
8491 && XEXP (varop, 1) == const0_rtx
8492 && GET_MODE (XEXP (varop, 0)) == result_mode
8493 && count == GET_MODE_BITSIZE (result_mode) - 1
8494 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
8495 && ((STORE_FLAG_VALUE
8496 & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (result_mode) - 1))))
8497 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
8498 && merge_outer_ops (&outer_op, &outer_const, XOR,
8499 (HOST_WIDE_INT) 1, result_mode,
8500 &complement_p))
8502 varop = XEXP (varop, 0);
8503 count = 0;
8504 continue;
8506 break;
8508 case NEG:
8509 /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
8510 than the number of bits in the mode is equivalent to A. */
8511 if (code == LSHIFTRT && count == GET_MODE_BITSIZE (result_mode) - 1
8512 && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
8514 varop = XEXP (varop, 0);
8515 count = 0;
8516 continue;
8519 /* NEG commutes with ASHIFT since it is multiplication. Move the
8520 NEG outside to allow shifts to combine. */
8521 if (code == ASHIFT
8522 && merge_outer_ops (&outer_op, &outer_const, NEG,
8523 (HOST_WIDE_INT) 0, result_mode,
8524 &complement_p))
8526 varop = XEXP (varop, 0);
8527 continue;
8529 break;
8531 case PLUS:
8532 /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
8533 is one less than the number of bits in the mode is
8534 equivalent to (xor A 1). */
8535 if (code == LSHIFTRT && count == GET_MODE_BITSIZE (result_mode) - 1
8536 && XEXP (varop, 1) == constm1_rtx
8537 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
8538 && merge_outer_ops (&outer_op, &outer_const, XOR,
8539 (HOST_WIDE_INT) 1, result_mode,
8540 &complement_p))
8542 count = 0;
8543 varop = XEXP (varop, 0);
8544 continue;
8547 /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
8548 that might be nonzero in BAR are those being shifted out and those
8549 bits are known zero in FOO, we can replace the PLUS with FOO.
8550 Similarly in the other operand order. This code occurs when
8551 we are computing the size of a variable-size array. */
8553 if ((code == ASHIFTRT || code == LSHIFTRT)
8554 && count < HOST_BITS_PER_WIDE_INT
8555 && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
8556 && (nonzero_bits (XEXP (varop, 1), result_mode)
8557 & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
8559 varop = XEXP (varop, 0);
8560 continue;
8562 else if ((code == ASHIFTRT || code == LSHIFTRT)
8563 && count < HOST_BITS_PER_WIDE_INT
8564 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
8565 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
8566 >> count)
8567 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
8568 & nonzero_bits (XEXP (varop, 1),
8569 result_mode)))
8571 varop = XEXP (varop, 1);
8572 continue;
8575 /* (ashift (plus foo C) N) is (plus (ashift foo N) C'). */
8576 if (code == ASHIFT
8577 && GET_CODE (XEXP (varop, 1)) == CONST_INT
8578 && (new = simplify_binary_operation (ASHIFT, result_mode,
8579 XEXP (varop, 1),
8580 GEN_INT (count))) != 0
8581 && GET_CODE(new) == CONST_INT
8582 && merge_outer_ops (&outer_op, &outer_const, PLUS,
8583 INTVAL (new), result_mode, &complement_p))
8585 varop = XEXP (varop, 0);
8586 continue;
8588 break;
8590 case MINUS:
8591 /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
8592 with C the size of VAROP - 1 and the shift is logical if
8593 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
8594 we have a (gt X 0) operation. If the shift is arithmetic with
8595 STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
8596 we have a (neg (gt X 0)) operation. */
8598 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8599 && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
8600 && count == GET_MODE_BITSIZE (GET_MODE (varop)) - 1
8601 && (code == LSHIFTRT || code == ASHIFTRT)
8602 && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
8603 && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
8604 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
8606 count = 0;
8607 varop = gen_rtx_combine (GT, GET_MODE (varop), XEXP (varop, 1),
8608 const0_rtx);
8610 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
8611 varop = gen_rtx_combine (NEG, GET_MODE (varop), varop);
8613 continue;
8615 break;
8618 break;
8621 /* We need to determine what mode to do the shift in. If the shift is
8622 a right shift or ROTATE, we must always do it in the mode it was
8623 originally done in. Otherwise, we can do it in MODE, the widest mode
8624 encountered. The code we care about is that of the shift that will
8625 actually be done, not the shift that was originally requested. */
8626 shift_mode
8627 = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
8628 ? result_mode : mode);
8630 /* We have now finished analyzing the shift. The result should be
8631 a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places. If
8632 OUTER_OP is non-NIL, it is an operation that needs to be applied
8633 to the result of the shift. OUTER_CONST is the relevant constant,
8634 but we must turn off all bits turned off in the shift.
8636 If we were passed a value for X, see if we can use any pieces of
8637 it. If not, make new rtx. */
8639 if (x && GET_RTX_CLASS (GET_CODE (x)) == '2'
8640 && GET_CODE (XEXP (x, 1)) == CONST_INT
8641 && INTVAL (XEXP (x, 1)) == count)
8642 const_rtx = XEXP (x, 1);
8643 else
8644 const_rtx = GEN_INT (count);
8646 if (x && GET_CODE (XEXP (x, 0)) == SUBREG
8647 && GET_MODE (XEXP (x, 0)) == shift_mode
8648 && SUBREG_REG (XEXP (x, 0)) == varop)
8649 varop = XEXP (x, 0);
8650 else if (GET_MODE (varop) != shift_mode)
8651 varop = gen_lowpart_for_combine (shift_mode, varop);
8653 /* If we can't make the SUBREG, try to return what we were given. */
8654 if (GET_CODE (varop) == CLOBBER)
8655 return x ? x : varop;
8657 new = simplify_binary_operation (code, shift_mode, varop, const_rtx);
8658 if (new != 0)
8659 x = new;
8660 else
8662 if (x == 0 || GET_CODE (x) != code || GET_MODE (x) != shift_mode)
8663 x = gen_rtx_combine (code, shift_mode, varop, const_rtx);
8665 SUBST (XEXP (x, 0), varop);
8666 SUBST (XEXP (x, 1), const_rtx);
8669 /* If we have an outer operation and we just made a shift, it is
8670 possible that we could have simplified the shift were it not
8671 for the outer operation. So try to do the simplification
8672 recursively. */
8674 if (outer_op != NIL && GET_CODE (x) == code
8675 && GET_CODE (XEXP (x, 1)) == CONST_INT)
8676 x = simplify_shift_const (x, code, shift_mode, XEXP (x, 0),
8677 INTVAL (XEXP (x, 1)));
8679 /* If we were doing a LSHIFTRT in a wider mode than it was originally,
8680 turn off all the bits that the shift would have turned off. */
8681 if (orig_code == LSHIFTRT && result_mode != shift_mode)
8682 x = simplify_and_const_int (NULL_RTX, shift_mode, x,
8683 GET_MODE_MASK (result_mode) >> orig_count);
8685 /* Do the remainder of the processing in RESULT_MODE. */
8686 x = gen_lowpart_for_combine (result_mode, x);
8688 /* If COMPLEMENT_P is set, we have to complement X before doing the outer
8689 operation. */
8690 if (complement_p)
8691 x = gen_unary (NOT, result_mode, result_mode, x);
8693 if (outer_op != NIL)
8695 if (GET_MODE_BITSIZE (result_mode) < HOST_BITS_PER_WIDE_INT)
8697 int width = GET_MODE_BITSIZE (result_mode);
8699 outer_const &= GET_MODE_MASK (result_mode);
8701 /* If this would be an entire word for the target, but is not for
8702 the host, then sign-extend on the host so that the number will
8703 look the same way on the host that it would on the target.
8705 For example, when building a 64 bit alpha hosted 32 bit sparc
8706 targeted compiler, then we want the 32 bit unsigned value -1 to be
8707 represented as a 64 bit value -1, and not as 0x00000000ffffffff.
8708 The later confuses the sparc backend. */
8710 if (BITS_PER_WORD < HOST_BITS_PER_WIDE_INT && BITS_PER_WORD == width
8711 && (outer_const & ((HOST_WIDE_INT) 1 << (width - 1))))
8712 outer_const |= ((HOST_WIDE_INT) (-1) << width);
8715 if (outer_op == AND)
8716 x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
8717 else if (outer_op == SET)
8718 /* This means that we have determined that the result is
8719 equivalent to a constant. This should be rare. */
8720 x = GEN_INT (outer_const);
8721 else if (GET_RTX_CLASS (outer_op) == '1')
8722 x = gen_unary (outer_op, result_mode, result_mode, x);
8723 else
8724 x = gen_binary (outer_op, result_mode, x, GEN_INT (outer_const));
8727 return x;
8730 /* Like recog, but we receive the address of a pointer to a new pattern.
8731 We try to match the rtx that the pointer points to.
8732 If that fails, we may try to modify or replace the pattern,
8733 storing the replacement into the same pointer object.
8735 Modifications include deletion or addition of CLOBBERs.
8737 PNOTES is a pointer to a location where any REG_UNUSED notes added for
8738 the CLOBBERs are placed.
8740 PADDED_SCRATCHES is set to the number of (clobber (scratch)) patterns
8741 we had to add.
8743 The value is the final insn code from the pattern ultimately matched,
8744 or -1. */
8746 static int
8747 recog_for_combine (pnewpat, insn, pnotes, padded_scratches)
8748 rtx *pnewpat;
8749 rtx insn;
8750 rtx *pnotes;
8751 int *padded_scratches;
8753 register rtx pat = *pnewpat;
8754 int insn_code_number;
8755 int num_clobbers_to_add = 0;
8756 int i;
8757 rtx notes = 0;
8759 *padded_scratches = 0;
8761 /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
8762 we use to indicate that something didn't match. If we find such a
8763 thing, force rejection. */
8764 if (GET_CODE (pat) == PARALLEL)
8765 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
8766 if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
8767 && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
8768 return -1;
8770 /* Is the result of combination a valid instruction? */
8771 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
8773 /* If it isn't, there is the possibility that we previously had an insn
8774 that clobbered some register as a side effect, but the combined
8775 insn doesn't need to do that. So try once more without the clobbers
8776 unless this represents an ASM insn. */
8778 if (insn_code_number < 0 && ! check_asm_operands (pat)
8779 && GET_CODE (pat) == PARALLEL)
8781 int pos;
8783 for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
8784 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
8786 if (i != pos)
8787 SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
8788 pos++;
8791 SUBST_INT (XVECLEN (pat, 0), pos);
8793 if (pos == 1)
8794 pat = XVECEXP (pat, 0, 0);
8796 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
8799 /* If we had any clobbers to add, make a new pattern than contains
8800 them. Then check to make sure that all of them are dead. */
8801 if (num_clobbers_to_add)
8803 rtx newpat = gen_rtx (PARALLEL, VOIDmode,
8804 gen_rtvec (GET_CODE (pat) == PARALLEL
8805 ? XVECLEN (pat, 0) + num_clobbers_to_add
8806 : num_clobbers_to_add + 1));
8808 if (GET_CODE (pat) == PARALLEL)
8809 for (i = 0; i < XVECLEN (pat, 0); i++)
8810 XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
8811 else
8812 XVECEXP (newpat, 0, 0) = pat;
8814 add_clobbers (newpat, insn_code_number);
8816 for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
8817 i < XVECLEN (newpat, 0); i++)
8819 if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) == REG
8820 && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
8821 return -1;
8822 else if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) == SCRATCH)
8823 (*padded_scratches)++;
8824 notes = gen_rtx (EXPR_LIST, REG_UNUSED,
8825 XEXP (XVECEXP (newpat, 0, i), 0), notes);
8827 pat = newpat;
8830 *pnewpat = pat;
8831 *pnotes = notes;
8833 return insn_code_number;
8836 /* Like gen_lowpart but for use by combine. In combine it is not possible
8837 to create any new pseudoregs. However, it is safe to create
8838 invalid memory addresses, because combine will try to recognize
8839 them and all they will do is make the combine attempt fail.
8841 If for some reason this cannot do its job, an rtx
8842 (clobber (const_int 0)) is returned.
8843 An insn containing that will not be recognized. */
8845 #undef gen_lowpart
8847 static rtx
8848 gen_lowpart_for_combine (mode, x)
8849 enum machine_mode mode;
8850 register rtx x;
8852 rtx result;
8854 if (GET_MODE (x) == mode)
8855 return x;
8857 /* We can only support MODE being wider than a word if X is a
8858 constant integer or has a mode the same size. */
8860 if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
8861 && ! ((GET_MODE (x) == VOIDmode
8862 && (GET_CODE (x) == CONST_INT
8863 || GET_CODE (x) == CONST_DOUBLE))
8864 || GET_MODE_SIZE (GET_MODE (x)) == GET_MODE_SIZE (mode)))
8865 return gen_rtx (CLOBBER, GET_MODE (x), const0_rtx);
8867 /* X might be a paradoxical (subreg (mem)). In that case, gen_lowpart
8868 won't know what to do. So we will strip off the SUBREG here and
8869 process normally. */
8870 if (GET_CODE (x) == SUBREG && GET_CODE (SUBREG_REG (x)) == MEM)
8872 x = SUBREG_REG (x);
8873 if (GET_MODE (x) == mode)
8874 return x;
8877 result = gen_lowpart_common (mode, x);
8878 if (result != 0
8879 && GET_CODE (result) == SUBREG
8880 && GET_CODE (SUBREG_REG (result)) == REG
8881 && REGNO (SUBREG_REG (result)) >= FIRST_PSEUDO_REGISTER
8882 && (GET_MODE_SIZE (GET_MODE (result))
8883 != GET_MODE_SIZE (GET_MODE (SUBREG_REG (result)))))
8884 REG_CHANGES_SIZE (REGNO (SUBREG_REG (result))) = 1;
8886 if (result)
8887 return result;
8889 if (GET_CODE (x) == MEM)
8891 register int offset = 0;
8892 rtx new;
8894 /* Refuse to work on a volatile memory ref or one with a mode-dependent
8895 address. */
8896 if (MEM_VOLATILE_P (x) || mode_dependent_address_p (XEXP (x, 0)))
8897 return gen_rtx (CLOBBER, GET_MODE (x), const0_rtx);
8899 /* If we want to refer to something bigger than the original memref,
8900 generate a perverse subreg instead. That will force a reload
8901 of the original memref X. */
8902 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode))
8903 return gen_rtx (SUBREG, mode, x, 0);
8905 if (WORDS_BIG_ENDIAN)
8906 offset = (MAX (GET_MODE_SIZE (GET_MODE (x)), UNITS_PER_WORD)
8907 - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD));
8908 if (BYTES_BIG_ENDIAN)
8910 /* Adjust the address so that the address-after-the-data is
8911 unchanged. */
8912 offset -= (MIN (UNITS_PER_WORD, GET_MODE_SIZE (mode))
8913 - MIN (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (x))));
8915 new = gen_rtx (MEM, mode, plus_constant (XEXP (x, 0), offset));
8916 RTX_UNCHANGING_P (new) = RTX_UNCHANGING_P (x);
8917 MEM_VOLATILE_P (new) = MEM_VOLATILE_P (x);
8918 MEM_IN_STRUCT_P (new) = MEM_IN_STRUCT_P (x);
8919 return new;
8922 /* If X is a comparison operator, rewrite it in a new mode. This
8923 probably won't match, but may allow further simplifications. */
8924 else if (GET_RTX_CLASS (GET_CODE (x)) == '<')
8925 return gen_rtx_combine (GET_CODE (x), mode, XEXP (x, 0), XEXP (x, 1));
8927 /* If we couldn't simplify X any other way, just enclose it in a
8928 SUBREG. Normally, this SUBREG won't match, but some patterns may
8929 include an explicit SUBREG or we may simplify it further in combine. */
8930 else
8932 int word = 0;
8934 if (WORDS_BIG_ENDIAN && GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD)
8935 word = ((GET_MODE_SIZE (GET_MODE (x))
8936 - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD))
8937 / UNITS_PER_WORD);
8938 return gen_rtx (SUBREG, mode, x, word);
8942 /* Make an rtx expression. This is a subset of gen_rtx and only supports
8943 expressions of 1, 2, or 3 operands, each of which are rtx expressions.
8945 If the identical expression was previously in the insn (in the undobuf),
8946 it will be returned. Only if it is not found will a new expression
8947 be made. */
8949 /*VARARGS2*/
8950 static rtx
8951 gen_rtx_combine VPROTO((enum rtx_code code, enum machine_mode mode, ...))
8953 #ifndef __STDC__
8954 enum rtx_code code;
8955 enum machine_mode mode;
8956 #endif
8957 va_list p;
8958 int n_args;
8959 rtx args[3];
8960 int i, j;
8961 char *fmt;
8962 rtx rt;
8963 struct undo *undo;
8965 VA_START (p, mode);
8967 #ifndef __STDC__
8968 code = va_arg (p, enum rtx_code);
8969 mode = va_arg (p, enum machine_mode);
8970 #endif
8972 n_args = GET_RTX_LENGTH (code);
8973 fmt = GET_RTX_FORMAT (code);
8975 if (n_args == 0 || n_args > 3)
8976 abort ();
8978 /* Get each arg and verify that it is supposed to be an expression. */
8979 for (j = 0; j < n_args; j++)
8981 if (*fmt++ != 'e')
8982 abort ();
8984 args[j] = va_arg (p, rtx);
8987 /* See if this is in undobuf. Be sure we don't use objects that came
8988 from another insn; this could produce circular rtl structures. */
8990 for (undo = undobuf.undos; undo != undobuf.previous_undos; undo = undo->next)
8991 if (!undo->is_int
8992 && GET_CODE (undo->old_contents.r) == code
8993 && GET_MODE (undo->old_contents.r) == mode)
8995 for (j = 0; j < n_args; j++)
8996 if (XEXP (undo->old_contents.r, j) != args[j])
8997 break;
8999 if (j == n_args)
9000 return undo->old_contents.r;
9003 /* Otherwise make a new rtx. We know we have 1, 2, or 3 args.
9004 Use rtx_alloc instead of gen_rtx because it's faster on RISC. */
9005 rt = rtx_alloc (code);
9006 PUT_MODE (rt, mode);
9007 XEXP (rt, 0) = args[0];
9008 if (n_args > 1)
9010 XEXP (rt, 1) = args[1];
9011 if (n_args > 2)
9012 XEXP (rt, 2) = args[2];
9014 return rt;
9017 /* These routines make binary and unary operations by first seeing if they
9018 fold; if not, a new expression is allocated. */
9020 static rtx
9021 gen_binary (code, mode, op0, op1)
9022 enum rtx_code code;
9023 enum machine_mode mode;
9024 rtx op0, op1;
9026 rtx result;
9027 rtx tem;
9029 if (GET_RTX_CLASS (code) == 'c'
9030 && (GET_CODE (op0) == CONST_INT
9031 || (CONSTANT_P (op0) && GET_CODE (op1) != CONST_INT)))
9032 tem = op0, op0 = op1, op1 = tem;
9034 if (GET_RTX_CLASS (code) == '<')
9036 enum machine_mode op_mode = GET_MODE (op0);
9038 /* Strip the COMPARE from (REL_OP (compare X Y) 0) to get
9039 just (REL_OP X Y). */
9040 if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
9042 op1 = XEXP (op0, 1);
9043 op0 = XEXP (op0, 0);
9044 op_mode = GET_MODE (op0);
9047 if (op_mode == VOIDmode)
9048 op_mode = GET_MODE (op1);
9049 result = simplify_relational_operation (code, op_mode, op0, op1);
9051 else
9052 result = simplify_binary_operation (code, mode, op0, op1);
9054 if (result)
9055 return result;
9057 /* Put complex operands first and constants second. */
9058 if (GET_RTX_CLASS (code) == 'c'
9059 && ((CONSTANT_P (op0) && GET_CODE (op1) != CONST_INT)
9060 || (GET_RTX_CLASS (GET_CODE (op0)) == 'o'
9061 && GET_RTX_CLASS (GET_CODE (op1)) != 'o')
9062 || (GET_CODE (op0) == SUBREG
9063 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (op0))) == 'o'
9064 && GET_RTX_CLASS (GET_CODE (op1)) != 'o')))
9065 return gen_rtx_combine (code, mode, op1, op0);
9067 return gen_rtx_combine (code, mode, op0, op1);
9070 static rtx
9071 gen_unary (code, mode, op0_mode, op0)
9072 enum rtx_code code;
9073 enum machine_mode mode, op0_mode;
9074 rtx op0;
9076 rtx result = simplify_unary_operation (code, mode, op0, op0_mode);
9078 if (result)
9079 return result;
9081 return gen_rtx_combine (code, mode, op0);
9084 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
9085 comparison code that will be tested.
9087 The result is a possibly different comparison code to use. *POP0 and
9088 *POP1 may be updated.
9090 It is possible that we might detect that a comparison is either always
9091 true or always false. However, we do not perform general constant
9092 folding in combine, so this knowledge isn't useful. Such tautologies
9093 should have been detected earlier. Hence we ignore all such cases. */
9095 static enum rtx_code
9096 simplify_comparison (code, pop0, pop1)
9097 enum rtx_code code;
9098 rtx *pop0;
9099 rtx *pop1;
9101 rtx op0 = *pop0;
9102 rtx op1 = *pop1;
9103 rtx tem, tem1;
9104 int i;
9105 enum machine_mode mode, tmode;
9107 /* Try a few ways of applying the same transformation to both operands. */
9108 while (1)
9110 #ifndef WORD_REGISTER_OPERATIONS
9111 /* The test below this one won't handle SIGN_EXTENDs on these machines,
9112 so check specially. */
9113 if (code != GTU && code != GEU && code != LTU && code != LEU
9114 && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
9115 && GET_CODE (XEXP (op0, 0)) == ASHIFT
9116 && GET_CODE (XEXP (op1, 0)) == ASHIFT
9117 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
9118 && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
9119 && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
9120 == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
9121 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9122 && GET_CODE (XEXP (op1, 1)) == CONST_INT
9123 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
9124 && GET_CODE (XEXP (XEXP (op1, 0), 1)) == CONST_INT
9125 && INTVAL (XEXP (op0, 1)) == INTVAL (XEXP (op1, 1))
9126 && INTVAL (XEXP (op0, 1)) == INTVAL (XEXP (XEXP (op0, 0), 1))
9127 && INTVAL (XEXP (op0, 1)) == INTVAL (XEXP (XEXP (op1, 0), 1))
9128 && (INTVAL (XEXP (op0, 1))
9129 == (GET_MODE_BITSIZE (GET_MODE (op0))
9130 - (GET_MODE_BITSIZE
9131 (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
9133 op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
9134 op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
9136 #endif
9138 /* If both operands are the same constant shift, see if we can ignore the
9139 shift. We can if the shift is a rotate or if the bits shifted out of
9140 this shift are known to be zero for both inputs and if the type of
9141 comparison is compatible with the shift. */
9142 if (GET_CODE (op0) == GET_CODE (op1)
9143 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
9144 && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
9145 || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
9146 && (code != GT && code != LT && code != GE && code != LE))
9147 || (GET_CODE (op0) == ASHIFTRT
9148 && (code != GTU && code != LTU
9149 && code != GEU && code != GEU)))
9150 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9151 && INTVAL (XEXP (op0, 1)) >= 0
9152 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
9153 && XEXP (op0, 1) == XEXP (op1, 1))
9155 enum machine_mode mode = GET_MODE (op0);
9156 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
9157 int shift_count = INTVAL (XEXP (op0, 1));
9159 if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
9160 mask &= (mask >> shift_count) << shift_count;
9161 else if (GET_CODE (op0) == ASHIFT)
9162 mask = (mask & (mask << shift_count)) >> shift_count;
9164 if ((nonzero_bits (XEXP (op0, 0), mode) & ~ mask) == 0
9165 && (nonzero_bits (XEXP (op1, 0), mode) & ~ mask) == 0)
9166 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
9167 else
9168 break;
9171 /* If both operands are AND's of a paradoxical SUBREG by constant, the
9172 SUBREGs are of the same mode, and, in both cases, the AND would
9173 be redundant if the comparison was done in the narrower mode,
9174 do the comparison in the narrower mode (e.g., we are AND'ing with 1
9175 and the operand's possibly nonzero bits are 0xffffff01; in that case
9176 if we only care about QImode, we don't need the AND). This case
9177 occurs if the output mode of an scc insn is not SImode and
9178 STORE_FLAG_VALUE == 1 (e.g., the 386).
9180 Similarly, check for a case where the AND's are ZERO_EXTEND
9181 operations from some narrower mode even though a SUBREG is not
9182 present. */
9184 else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
9185 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9186 && GET_CODE (XEXP (op1, 1)) == CONST_INT)
9188 rtx inner_op0 = XEXP (op0, 0);
9189 rtx inner_op1 = XEXP (op1, 0);
9190 HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
9191 HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
9192 int changed = 0;
9194 if (GET_CODE (inner_op0) == SUBREG && GET_CODE (inner_op1) == SUBREG
9195 && (GET_MODE_SIZE (GET_MODE (inner_op0))
9196 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (inner_op0))))
9197 && (GET_MODE (SUBREG_REG (inner_op0))
9198 == GET_MODE (SUBREG_REG (inner_op1)))
9199 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
9200 <= HOST_BITS_PER_WIDE_INT)
9201 && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
9202 GET_MODE (SUBREG_REG (op0)))))
9203 && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
9204 GET_MODE (SUBREG_REG (inner_op1))))))
9206 op0 = SUBREG_REG (inner_op0);
9207 op1 = SUBREG_REG (inner_op1);
9209 /* The resulting comparison is always unsigned since we masked
9210 off the original sign bit. */
9211 code = unsigned_condition (code);
9213 changed = 1;
9216 else if (c0 == c1)
9217 for (tmode = GET_CLASS_NARROWEST_MODE
9218 (GET_MODE_CLASS (GET_MODE (op0)));
9219 tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
9220 if (c0 == GET_MODE_MASK (tmode))
9222 op0 = gen_lowpart_for_combine (tmode, inner_op0);
9223 op1 = gen_lowpart_for_combine (tmode, inner_op1);
9224 code = unsigned_condition (code);
9225 changed = 1;
9226 break;
9229 if (! changed)
9230 break;
9233 /* If both operands are NOT, we can strip off the outer operation
9234 and adjust the comparison code for swapped operands; similarly for
9235 NEG, except that this must be an equality comparison. */
9236 else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
9237 || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
9238 && (code == EQ || code == NE)))
9239 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
9241 else
9242 break;
9245 /* If the first operand is a constant, swap the operands and adjust the
9246 comparison code appropriately, but don't do this if the second operand
9247 is already a constant integer. */
9248 if (CONSTANT_P (op0) && GET_CODE (op1) != CONST_INT)
9250 tem = op0, op0 = op1, op1 = tem;
9251 code = swap_condition (code);
9254 /* We now enter a loop during which we will try to simplify the comparison.
9255 For the most part, we only are concerned with comparisons with zero,
9256 but some things may really be comparisons with zero but not start
9257 out looking that way. */
9259 while (GET_CODE (op1) == CONST_INT)
9261 enum machine_mode mode = GET_MODE (op0);
9262 int mode_width = GET_MODE_BITSIZE (mode);
9263 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
9264 int equality_comparison_p;
9265 int sign_bit_comparison_p;
9266 int unsigned_comparison_p;
9267 HOST_WIDE_INT const_op;
9269 /* We only want to handle integral modes. This catches VOIDmode,
9270 CCmode, and the floating-point modes. An exception is that we
9271 can handle VOIDmode if OP0 is a COMPARE or a comparison
9272 operation. */
9274 if (GET_MODE_CLASS (mode) != MODE_INT
9275 && ! (mode == VOIDmode
9276 && (GET_CODE (op0) == COMPARE
9277 || GET_RTX_CLASS (GET_CODE (op0)) == '<')))
9278 break;
9280 /* Get the constant we are comparing against and turn off all bits
9281 not on in our mode. */
9282 const_op = INTVAL (op1);
9283 if (mode_width <= HOST_BITS_PER_WIDE_INT)
9284 const_op &= mask;
9286 /* If we are comparing against a constant power of two and the value
9287 being compared can only have that single bit nonzero (e.g., it was
9288 `and'ed with that bit), we can replace this with a comparison
9289 with zero. */
9290 if (const_op
9291 && (code == EQ || code == NE || code == GE || code == GEU
9292 || code == LT || code == LTU)
9293 && mode_width <= HOST_BITS_PER_WIDE_INT
9294 && exact_log2 (const_op) >= 0
9295 && nonzero_bits (op0, mode) == const_op)
9297 code = (code == EQ || code == GE || code == GEU ? NE : EQ);
9298 op1 = const0_rtx, const_op = 0;
9301 /* Similarly, if we are comparing a value known to be either -1 or
9302 0 with -1, change it to the opposite comparison against zero. */
9304 if (const_op == -1
9305 && (code == EQ || code == NE || code == GT || code == LE
9306 || code == GEU || code == LTU)
9307 && num_sign_bit_copies (op0, mode) == mode_width)
9309 code = (code == EQ || code == LE || code == GEU ? NE : EQ);
9310 op1 = const0_rtx, const_op = 0;
9313 /* Do some canonicalizations based on the comparison code. We prefer
9314 comparisons against zero and then prefer equality comparisons.
9315 If we can reduce the size of a constant, we will do that too. */
9317 switch (code)
9319 case LT:
9320 /* < C is equivalent to <= (C - 1) */
9321 if (const_op > 0)
9323 const_op -= 1;
9324 op1 = GEN_INT (const_op);
9325 code = LE;
9326 /* ... fall through to LE case below. */
9328 else
9329 break;
9331 case LE:
9332 /* <= C is equivalent to < (C + 1); we do this for C < 0 */
9333 if (const_op < 0)
9335 const_op += 1;
9336 op1 = GEN_INT (const_op);
9337 code = LT;
9340 /* If we are doing a <= 0 comparison on a value known to have
9341 a zero sign bit, we can replace this with == 0. */
9342 else if (const_op == 0
9343 && mode_width <= HOST_BITS_PER_WIDE_INT
9344 && (nonzero_bits (op0, mode)
9345 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
9346 code = EQ;
9347 break;
9349 case GE:
9350 /* >= C is equivalent to > (C - 1). */
9351 if (const_op > 0)
9353 const_op -= 1;
9354 op1 = GEN_INT (const_op);
9355 code = GT;
9356 /* ... fall through to GT below. */
9358 else
9359 break;
9361 case GT:
9362 /* > C is equivalent to >= (C + 1); we do this for C < 0*/
9363 if (const_op < 0)
9365 const_op += 1;
9366 op1 = GEN_INT (const_op);
9367 code = GE;
9370 /* If we are doing a > 0 comparison on a value known to have
9371 a zero sign bit, we can replace this with != 0. */
9372 else if (const_op == 0
9373 && mode_width <= HOST_BITS_PER_WIDE_INT
9374 && (nonzero_bits (op0, mode)
9375 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
9376 code = NE;
9377 break;
9379 case LTU:
9380 /* < C is equivalent to <= (C - 1). */
9381 if (const_op > 0)
9383 const_op -= 1;
9384 op1 = GEN_INT (const_op);
9385 code = LEU;
9386 /* ... fall through ... */
9389 /* (unsigned) < 0x80000000 is equivalent to >= 0. */
9390 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9391 && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
9393 const_op = 0, op1 = const0_rtx;
9394 code = GE;
9395 break;
9397 else
9398 break;
9400 case LEU:
9401 /* unsigned <= 0 is equivalent to == 0 */
9402 if (const_op == 0)
9403 code = EQ;
9405 /* (unsigned) <= 0x7fffffff is equivalent to >= 0. */
9406 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9407 && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
9409 const_op = 0, op1 = const0_rtx;
9410 code = GE;
9412 break;
9414 case GEU:
9415 /* >= C is equivalent to < (C - 1). */
9416 if (const_op > 1)
9418 const_op -= 1;
9419 op1 = GEN_INT (const_op);
9420 code = GTU;
9421 /* ... fall through ... */
9424 /* (unsigned) >= 0x80000000 is equivalent to < 0. */
9425 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9426 && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
9428 const_op = 0, op1 = const0_rtx;
9429 code = LT;
9430 break;
9432 else
9433 break;
9435 case GTU:
9436 /* unsigned > 0 is equivalent to != 0 */
9437 if (const_op == 0)
9438 code = NE;
9440 /* (unsigned) > 0x7fffffff is equivalent to < 0. */
9441 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9442 && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
9444 const_op = 0, op1 = const0_rtx;
9445 code = LT;
9447 break;
9450 /* Compute some predicates to simplify code below. */
9452 equality_comparison_p = (code == EQ || code == NE);
9453 sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
9454 unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
9455 || code == LEU);
9457 /* If this is a sign bit comparison and we can do arithmetic in
9458 MODE, say that we will only be needing the sign bit of OP0. */
9459 if (sign_bit_comparison_p
9460 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
9461 op0 = force_to_mode (op0, mode,
9462 ((HOST_WIDE_INT) 1
9463 << (GET_MODE_BITSIZE (mode) - 1)),
9464 NULL_RTX, 0);
9466 /* Now try cases based on the opcode of OP0. If none of the cases
9467 does a "continue", we exit this loop immediately after the
9468 switch. */
9470 switch (GET_CODE (op0))
9472 case ZERO_EXTRACT:
9473 /* If we are extracting a single bit from a variable position in
9474 a constant that has only a single bit set and are comparing it
9475 with zero, we can convert this into an equality comparison
9476 between the position and the location of the single bit. */
9478 if (GET_CODE (XEXP (op0, 0)) == CONST_INT
9479 && XEXP (op0, 1) == const1_rtx
9480 && equality_comparison_p && const_op == 0
9481 && (i = exact_log2 (INTVAL (XEXP (op0, 0)))) >= 0)
9483 if (BITS_BIG_ENDIAN)
9484 #ifdef HAVE_extzv
9485 i = (GET_MODE_BITSIZE
9486 (insn_operand_mode[(int) CODE_FOR_extzv][1]) - 1 - i);
9487 #else
9488 i = BITS_PER_WORD - 1 - i;
9489 #endif
9491 op0 = XEXP (op0, 2);
9492 op1 = GEN_INT (i);
9493 const_op = i;
9495 /* Result is nonzero iff shift count is equal to I. */
9496 code = reverse_condition (code);
9497 continue;
9500 /* ... fall through ... */
9502 case SIGN_EXTRACT:
9503 tem = expand_compound_operation (op0);
9504 if (tem != op0)
9506 op0 = tem;
9507 continue;
9509 break;
9511 case NOT:
9512 /* If testing for equality, we can take the NOT of the constant. */
9513 if (equality_comparison_p
9514 && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
9516 op0 = XEXP (op0, 0);
9517 op1 = tem;
9518 continue;
9521 /* If just looking at the sign bit, reverse the sense of the
9522 comparison. */
9523 if (sign_bit_comparison_p)
9525 op0 = XEXP (op0, 0);
9526 code = (code == GE ? LT : GE);
9527 continue;
9529 break;
9531 case NEG:
9532 /* If testing for equality, we can take the NEG of the constant. */
9533 if (equality_comparison_p
9534 && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
9536 op0 = XEXP (op0, 0);
9537 op1 = tem;
9538 continue;
9541 /* The remaining cases only apply to comparisons with zero. */
9542 if (const_op != 0)
9543 break;
9545 /* When X is ABS or is known positive,
9546 (neg X) is < 0 if and only if X != 0. */
9548 if (sign_bit_comparison_p
9549 && (GET_CODE (XEXP (op0, 0)) == ABS
9550 || (mode_width <= HOST_BITS_PER_WIDE_INT
9551 && (nonzero_bits (XEXP (op0, 0), mode)
9552 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)))
9554 op0 = XEXP (op0, 0);
9555 code = (code == LT ? NE : EQ);
9556 continue;
9559 /* If we have NEG of something whose two high-order bits are the
9560 same, we know that "(-a) < 0" is equivalent to "a > 0". */
9561 if (num_sign_bit_copies (op0, mode) >= 2)
9563 op0 = XEXP (op0, 0);
9564 code = swap_condition (code);
9565 continue;
9567 break;
9569 case ROTATE:
9570 /* If we are testing equality and our count is a constant, we
9571 can perform the inverse operation on our RHS. */
9572 if (equality_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
9573 && (tem = simplify_binary_operation (ROTATERT, mode,
9574 op1, XEXP (op0, 1))) != 0)
9576 op0 = XEXP (op0, 0);
9577 op1 = tem;
9578 continue;
9581 /* If we are doing a < 0 or >= 0 comparison, it means we are testing
9582 a particular bit. Convert it to an AND of a constant of that
9583 bit. This will be converted into a ZERO_EXTRACT. */
9584 if (const_op == 0 && sign_bit_comparison_p
9585 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9586 && mode_width <= HOST_BITS_PER_WIDE_INT)
9588 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
9589 ((HOST_WIDE_INT) 1
9590 << (mode_width - 1
9591 - INTVAL (XEXP (op0, 1)))));
9592 code = (code == LT ? NE : EQ);
9593 continue;
9596 /* ... fall through ... */
9598 case ABS:
9599 /* ABS is ignorable inside an equality comparison with zero. */
9600 if (const_op == 0 && equality_comparison_p)
9602 op0 = XEXP (op0, 0);
9603 continue;
9605 break;
9608 case SIGN_EXTEND:
9609 /* Can simplify (compare (zero/sign_extend FOO) CONST)
9610 to (compare FOO CONST) if CONST fits in FOO's mode and we
9611 are either testing inequality or have an unsigned comparison
9612 with ZERO_EXTEND or a signed comparison with SIGN_EXTEND. */
9613 if (! unsigned_comparison_p
9614 && (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
9615 <= HOST_BITS_PER_WIDE_INT)
9616 && ((unsigned HOST_WIDE_INT) const_op
9617 < (((HOST_WIDE_INT) 1
9618 << (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))) - 1)))))
9620 op0 = XEXP (op0, 0);
9621 continue;
9623 break;
9625 case SUBREG:
9626 /* Check for the case where we are comparing A - C1 with C2,
9627 both constants are smaller than 1/2 the maximum positive
9628 value in MODE, and the comparison is equality or unsigned.
9629 In that case, if A is either zero-extended to MODE or has
9630 sufficient sign bits so that the high-order bit in MODE
9631 is a copy of the sign in the inner mode, we can prove that it is
9632 safe to do the operation in the wider mode. This simplifies
9633 many range checks. */
9635 if (mode_width <= HOST_BITS_PER_WIDE_INT
9636 && subreg_lowpart_p (op0)
9637 && GET_CODE (SUBREG_REG (op0)) == PLUS
9638 && GET_CODE (XEXP (SUBREG_REG (op0), 1)) == CONST_INT
9639 && INTVAL (XEXP (SUBREG_REG (op0), 1)) < 0
9640 && (- INTVAL (XEXP (SUBREG_REG (op0), 1))
9641 < GET_MODE_MASK (mode) / 2)
9642 && (unsigned HOST_WIDE_INT) const_op < GET_MODE_MASK (mode) / 2
9643 && (0 == (nonzero_bits (XEXP (SUBREG_REG (op0), 0),
9644 GET_MODE (SUBREG_REG (op0)))
9645 & ~ GET_MODE_MASK (mode))
9646 || (num_sign_bit_copies (XEXP (SUBREG_REG (op0), 0),
9647 GET_MODE (SUBREG_REG (op0)))
9648 > (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
9649 - GET_MODE_BITSIZE (mode)))))
9651 op0 = SUBREG_REG (op0);
9652 continue;
9655 /* If the inner mode is narrower and we are extracting the low part,
9656 we can treat the SUBREG as if it were a ZERO_EXTEND. */
9657 if (subreg_lowpart_p (op0)
9658 && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) < mode_width)
9659 /* Fall through */ ;
9660 else
9661 break;
9663 /* ... fall through ... */
9665 case ZERO_EXTEND:
9666 if ((unsigned_comparison_p || equality_comparison_p)
9667 && (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
9668 <= HOST_BITS_PER_WIDE_INT)
9669 && ((unsigned HOST_WIDE_INT) const_op
9670 < GET_MODE_MASK (GET_MODE (XEXP (op0, 0)))))
9672 op0 = XEXP (op0, 0);
9673 continue;
9675 break;
9677 case PLUS:
9678 /* (eq (plus X A) B) -> (eq X (minus B A)). We can only do
9679 this for equality comparisons due to pathological cases involving
9680 overflows. */
9681 if (equality_comparison_p
9682 && 0 != (tem = simplify_binary_operation (MINUS, mode,
9683 op1, XEXP (op0, 1))))
9685 op0 = XEXP (op0, 0);
9686 op1 = tem;
9687 continue;
9690 /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0. */
9691 if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
9692 && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
9694 op0 = XEXP (XEXP (op0, 0), 0);
9695 code = (code == LT ? EQ : NE);
9696 continue;
9698 break;
9700 case MINUS:
9701 /* (eq (minus A B) C) -> (eq A (plus B C)) or
9702 (eq B (minus A C)), whichever simplifies. We can only do
9703 this for equality comparisons due to pathological cases involving
9704 overflows. */
9705 if (equality_comparison_p
9706 && 0 != (tem = simplify_binary_operation (PLUS, mode,
9707 XEXP (op0, 1), op1)))
9709 op0 = XEXP (op0, 0);
9710 op1 = tem;
9711 continue;
9714 if (equality_comparison_p
9715 && 0 != (tem = simplify_binary_operation (MINUS, mode,
9716 XEXP (op0, 0), op1)))
9718 op0 = XEXP (op0, 1);
9719 op1 = tem;
9720 continue;
9723 /* The sign bit of (minus (ashiftrt X C) X), where C is the number
9724 of bits in X minus 1, is one iff X > 0. */
9725 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
9726 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
9727 && INTVAL (XEXP (XEXP (op0, 0), 1)) == mode_width - 1
9728 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
9730 op0 = XEXP (op0, 1);
9731 code = (code == GE ? LE : GT);
9732 continue;
9734 break;
9736 case XOR:
9737 /* (eq (xor A B) C) -> (eq A (xor B C)). This is a simplification
9738 if C is zero or B is a constant. */
9739 if (equality_comparison_p
9740 && 0 != (tem = simplify_binary_operation (XOR, mode,
9741 XEXP (op0, 1), op1)))
9743 op0 = XEXP (op0, 0);
9744 op1 = tem;
9745 continue;
9747 break;
9749 case EQ: case NE:
9750 case LT: case LTU: case LE: case LEU:
9751 case GT: case GTU: case GE: case GEU:
9752 /* We can't do anything if OP0 is a condition code value, rather
9753 than an actual data value. */
9754 if (const_op != 0
9755 #ifdef HAVE_cc0
9756 || XEXP (op0, 0) == cc0_rtx
9757 #endif
9758 || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
9759 break;
9761 /* Get the two operands being compared. */
9762 if (GET_CODE (XEXP (op0, 0)) == COMPARE)
9763 tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
9764 else
9765 tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
9767 /* Check for the cases where we simply want the result of the
9768 earlier test or the opposite of that result. */
9769 if (code == NE
9770 || (code == EQ && reversible_comparison_p (op0))
9771 || (GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
9772 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
9773 && (STORE_FLAG_VALUE
9774 & (((HOST_WIDE_INT) 1
9775 << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
9776 && (code == LT
9777 || (code == GE && reversible_comparison_p (op0)))))
9779 code = (code == LT || code == NE
9780 ? GET_CODE (op0) : reverse_condition (GET_CODE (op0)));
9781 op0 = tem, op1 = tem1;
9782 continue;
9784 break;
9786 case IOR:
9787 /* The sign bit of (ior (plus X (const_int -1)) X) is non-zero
9788 iff X <= 0. */
9789 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
9790 && XEXP (XEXP (op0, 0), 1) == constm1_rtx
9791 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
9793 op0 = XEXP (op0, 1);
9794 code = (code == GE ? GT : LE);
9795 continue;
9797 break;
9799 case AND:
9800 /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1). This
9801 will be converted to a ZERO_EXTRACT later. */
9802 if (const_op == 0 && equality_comparison_p
9803 && GET_CODE (XEXP (op0, 0)) == ASHIFT
9804 && XEXP (XEXP (op0, 0), 0) == const1_rtx)
9806 op0 = simplify_and_const_int
9807 (op0, mode, gen_rtx_combine (LSHIFTRT, mode,
9808 XEXP (op0, 1),
9809 XEXP (XEXP (op0, 0), 1)),
9810 (HOST_WIDE_INT) 1);
9811 continue;
9814 /* If we are comparing (and (lshiftrt X C1) C2) for equality with
9815 zero and X is a comparison and C1 and C2 describe only bits set
9816 in STORE_FLAG_VALUE, we can compare with X. */
9817 if (const_op == 0 && equality_comparison_p
9818 && mode_width <= HOST_BITS_PER_WIDE_INT
9819 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9820 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
9821 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
9822 && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
9823 && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
9825 mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
9826 << INTVAL (XEXP (XEXP (op0, 0), 1)));
9827 if ((~ STORE_FLAG_VALUE & mask) == 0
9828 && (GET_RTX_CLASS (GET_CODE (XEXP (XEXP (op0, 0), 0))) == '<'
9829 || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
9830 && GET_RTX_CLASS (GET_CODE (tem)) == '<')))
9832 op0 = XEXP (XEXP (op0, 0), 0);
9833 continue;
9837 /* If we are doing an equality comparison of an AND of a bit equal
9838 to the sign bit, replace this with a LT or GE comparison of
9839 the underlying value. */
9840 if (equality_comparison_p
9841 && const_op == 0
9842 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9843 && mode_width <= HOST_BITS_PER_WIDE_INT
9844 && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
9845 == (HOST_WIDE_INT) 1 << (mode_width - 1)))
9847 op0 = XEXP (op0, 0);
9848 code = (code == EQ ? GE : LT);
9849 continue;
9852 /* If this AND operation is really a ZERO_EXTEND from a narrower
9853 mode, the constant fits within that mode, and this is either an
9854 equality or unsigned comparison, try to do this comparison in
9855 the narrower mode. */
9856 if ((equality_comparison_p || unsigned_comparison_p)
9857 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9858 && (i = exact_log2 ((INTVAL (XEXP (op0, 1))
9859 & GET_MODE_MASK (mode))
9860 + 1)) >= 0
9861 && const_op >> i == 0
9862 && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode)
9864 op0 = gen_lowpart_for_combine (tmode, XEXP (op0, 0));
9865 continue;
9867 break;
9869 case ASHIFT:
9870 /* If we have (compare (ashift FOO N) (const_int C)) and
9871 the high order N bits of FOO (N+1 if an inequality comparison)
9872 are known to be zero, we can do this by comparing FOO with C
9873 shifted right N bits so long as the low-order N bits of C are
9874 zero. */
9875 if (GET_CODE (XEXP (op0, 1)) == CONST_INT
9876 && INTVAL (XEXP (op0, 1)) >= 0
9877 && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
9878 < HOST_BITS_PER_WIDE_INT)
9879 && ((const_op
9880 & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0)
9881 && mode_width <= HOST_BITS_PER_WIDE_INT
9882 && (nonzero_bits (XEXP (op0, 0), mode)
9883 & ~ (mask >> (INTVAL (XEXP (op0, 1))
9884 + ! equality_comparison_p))) == 0)
9886 const_op >>= INTVAL (XEXP (op0, 1));
9887 op1 = GEN_INT (const_op);
9888 op0 = XEXP (op0, 0);
9889 continue;
9892 /* If we are doing a sign bit comparison, it means we are testing
9893 a particular bit. Convert it to the appropriate AND. */
9894 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
9895 && mode_width <= HOST_BITS_PER_WIDE_INT)
9897 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
9898 ((HOST_WIDE_INT) 1
9899 << (mode_width - 1
9900 - INTVAL (XEXP (op0, 1)))));
9901 code = (code == LT ? NE : EQ);
9902 continue;
9905 /* If this an equality comparison with zero and we are shifting
9906 the low bit to the sign bit, we can convert this to an AND of the
9907 low-order bit. */
9908 if (const_op == 0 && equality_comparison_p
9909 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9910 && INTVAL (XEXP (op0, 1)) == mode_width - 1)
9912 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
9913 (HOST_WIDE_INT) 1);
9914 continue;
9916 break;
9918 case ASHIFTRT:
9919 /* If this is an equality comparison with zero, we can do this
9920 as a logical shift, which might be much simpler. */
9921 if (equality_comparison_p && const_op == 0
9922 && GET_CODE (XEXP (op0, 1)) == CONST_INT)
9924 op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
9925 XEXP (op0, 0),
9926 INTVAL (XEXP (op0, 1)));
9927 continue;
9930 /* If OP0 is a sign extension and CODE is not an unsigned comparison,
9931 do the comparison in a narrower mode. */
9932 if (! unsigned_comparison_p
9933 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9934 && GET_CODE (XEXP (op0, 0)) == ASHIFT
9935 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
9936 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
9937 MODE_INT, 1)) != BLKmode
9938 && ((unsigned HOST_WIDE_INT) const_op <= GET_MODE_MASK (tmode)
9939 || ((unsigned HOST_WIDE_INT) - const_op
9940 <= GET_MODE_MASK (tmode))))
9942 op0 = gen_lowpart_for_combine (tmode, XEXP (XEXP (op0, 0), 0));
9943 continue;
9946 /* ... fall through ... */
9947 case LSHIFTRT:
9948 /* If we have (compare (xshiftrt FOO N) (const_int C)) and
9949 the low order N bits of FOO are known to be zero, we can do this
9950 by comparing FOO with C shifted left N bits so long as no
9951 overflow occurs. */
9952 if (GET_CODE (XEXP (op0, 1)) == CONST_INT
9953 && INTVAL (XEXP (op0, 1)) >= 0
9954 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
9955 && mode_width <= HOST_BITS_PER_WIDE_INT
9956 && (nonzero_bits (XEXP (op0, 0), mode)
9957 & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0
9958 && (const_op == 0
9959 || (floor_log2 (const_op) + INTVAL (XEXP (op0, 1))
9960 < mode_width)))
9962 const_op <<= INTVAL (XEXP (op0, 1));
9963 op1 = GEN_INT (const_op);
9964 op0 = XEXP (op0, 0);
9965 continue;
9968 /* If we are using this shift to extract just the sign bit, we
9969 can replace this with an LT or GE comparison. */
9970 if (const_op == 0
9971 && (equality_comparison_p || sign_bit_comparison_p)
9972 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9973 && INTVAL (XEXP (op0, 1)) == mode_width - 1)
9975 op0 = XEXP (op0, 0);
9976 code = (code == NE || code == GT ? LT : GE);
9977 continue;
9979 break;
9982 break;
9985 /* Now make any compound operations involved in this comparison. Then,
9986 check for an outmost SUBREG on OP0 that isn't doing anything or is
9987 paradoxical. The latter case can only occur when it is known that the
9988 "extra" bits will be zero. Therefore, it is safe to remove the SUBREG.
9989 We can never remove a SUBREG for a non-equality comparison because the
9990 sign bit is in a different place in the underlying object. */
9992 op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
9993 op1 = make_compound_operation (op1, SET);
9995 if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
9996 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
9997 && (code == NE || code == EQ)
9998 && ((GET_MODE_SIZE (GET_MODE (op0))
9999 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0))))))
10001 op0 = SUBREG_REG (op0);
10002 op1 = gen_lowpart_for_combine (GET_MODE (op0), op1);
10005 else if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
10006 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10007 && (code == NE || code == EQ)
10008 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
10009 <= HOST_BITS_PER_WIDE_INT)
10010 && (nonzero_bits (SUBREG_REG (op0), GET_MODE (SUBREG_REG (op0)))
10011 & ~ GET_MODE_MASK (GET_MODE (op0))) == 0
10012 && (tem = gen_lowpart_for_combine (GET_MODE (SUBREG_REG (op0)),
10013 op1),
10014 (nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
10015 & ~ GET_MODE_MASK (GET_MODE (op0))) == 0))
10016 op0 = SUBREG_REG (op0), op1 = tem;
10018 /* We now do the opposite procedure: Some machines don't have compare
10019 insns in all modes. If OP0's mode is an integer mode smaller than a
10020 word and we can't do a compare in that mode, see if there is a larger
10021 mode for which we can do the compare. There are a number of cases in
10022 which we can use the wider mode. */
10024 mode = GET_MODE (op0);
10025 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
10026 && GET_MODE_SIZE (mode) < UNITS_PER_WORD
10027 && cmp_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
10028 for (tmode = GET_MODE_WIDER_MODE (mode);
10029 (tmode != VOIDmode
10030 && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT);
10031 tmode = GET_MODE_WIDER_MODE (tmode))
10032 if (cmp_optab->handlers[(int) tmode].insn_code != CODE_FOR_nothing)
10034 /* If the only nonzero bits in OP0 and OP1 are those in the
10035 narrower mode and this is an equality or unsigned comparison,
10036 we can use the wider mode. Similarly for sign-extended
10037 values, in which case it is true for all comparisons. */
10038 if (((code == EQ || code == NE
10039 || code == GEU || code == GTU || code == LEU || code == LTU)
10040 && (nonzero_bits (op0, tmode) & ~ GET_MODE_MASK (mode)) == 0
10041 && (nonzero_bits (op1, tmode) & ~ GET_MODE_MASK (mode)) == 0)
10042 || ((num_sign_bit_copies (op0, tmode)
10043 > GET_MODE_BITSIZE (tmode) - GET_MODE_BITSIZE (mode))
10044 && (num_sign_bit_copies (op1, tmode)
10045 > GET_MODE_BITSIZE (tmode) - GET_MODE_BITSIZE (mode))))
10047 op0 = gen_lowpart_for_combine (tmode, op0);
10048 op1 = gen_lowpart_for_combine (tmode, op1);
10049 break;
10052 /* If this is a test for negative, we can make an explicit
10053 test of the sign bit. */
10055 if (op1 == const0_rtx && (code == LT || code == GE)
10056 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10058 op0 = gen_binary (AND, tmode,
10059 gen_lowpart_for_combine (tmode, op0),
10060 GEN_INT ((HOST_WIDE_INT) 1
10061 << (GET_MODE_BITSIZE (mode) - 1)));
10062 code = (code == LT) ? NE : EQ;
10063 break;
10067 #ifdef CANONICALIZE_COMPARISON
10068 /* If this machine only supports a subset of valid comparisons, see if we
10069 can convert an unsupported one into a supported one. */
10070 CANONICALIZE_COMPARISON (code, op0, op1);
10071 #endif
10073 *pop0 = op0;
10074 *pop1 = op1;
10076 return code;
10079 /* Return 1 if we know that X, a comparison operation, is not operating
10080 on a floating-point value or is EQ or NE, meaning that we can safely
10081 reverse it. */
10083 static int
10084 reversible_comparison_p (x)
10085 rtx x;
10087 if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
10088 || flag_fast_math
10089 || GET_CODE (x) == NE || GET_CODE (x) == EQ)
10090 return 1;
10092 switch (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))))
10094 case MODE_INT:
10095 case MODE_PARTIAL_INT:
10096 case MODE_COMPLEX_INT:
10097 return 1;
10099 case MODE_CC:
10100 /* If the mode of the condition codes tells us that this is safe,
10101 we need look no further. */
10102 if (REVERSIBLE_CC_MODE (GET_MODE (XEXP (x, 0))))
10103 return 1;
10105 /* Otherwise try and find where the condition codes were last set and
10106 use that. */
10107 x = get_last_value (XEXP (x, 0));
10108 return (x && GET_CODE (x) == COMPARE
10109 && ! FLOAT_MODE_P (GET_MODE (XEXP (x, 0))));
10112 return 0;
10115 /* Utility function for following routine. Called when X is part of a value
10116 being stored into reg_last_set_value. Sets reg_last_set_table_tick
10117 for each register mentioned. Similar to mention_regs in cse.c */
10119 static void
10120 update_table_tick (x)
10121 rtx x;
10123 register enum rtx_code code = GET_CODE (x);
10124 register char *fmt = GET_RTX_FORMAT (code);
10125 register int i;
10127 if (code == REG)
10129 int regno = REGNO (x);
10130 int endregno = regno + (regno < FIRST_PSEUDO_REGISTER
10131 ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
10133 for (i = regno; i < endregno; i++)
10134 reg_last_set_table_tick[i] = label_tick;
10136 return;
10139 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
10140 /* Note that we can't have an "E" in values stored; see
10141 get_last_value_validate. */
10142 if (fmt[i] == 'e')
10143 update_table_tick (XEXP (x, i));
10146 /* Record that REG is set to VALUE in insn INSN. If VALUE is zero, we
10147 are saying that the register is clobbered and we no longer know its
10148 value. If INSN is zero, don't update reg_last_set; this is only permitted
10149 with VALUE also zero and is used to invalidate the register. */
10151 static void
10152 record_value_for_reg (reg, insn, value)
10153 rtx reg;
10154 rtx insn;
10155 rtx value;
10157 int regno = REGNO (reg);
10158 int endregno = regno + (regno < FIRST_PSEUDO_REGISTER
10159 ? HARD_REGNO_NREGS (regno, GET_MODE (reg)) : 1);
10160 int i;
10162 /* If VALUE contains REG and we have a previous value for REG, substitute
10163 the previous value. */
10164 if (value && insn && reg_overlap_mentioned_p (reg, value))
10166 rtx tem;
10168 /* Set things up so get_last_value is allowed to see anything set up to
10169 our insn. */
10170 subst_low_cuid = INSN_CUID (insn);
10171 tem = get_last_value (reg);
10173 if (tem)
10174 value = replace_rtx (copy_rtx (value), reg, tem);
10177 /* For each register modified, show we don't know its value, that
10178 we don't know about its bitwise content, that its value has been
10179 updated, and that we don't know the location of the death of the
10180 register. */
10181 for (i = regno; i < endregno; i ++)
10183 if (insn)
10184 reg_last_set[i] = insn;
10185 reg_last_set_value[i] = 0;
10186 reg_last_set_mode[i] = 0;
10187 reg_last_set_nonzero_bits[i] = 0;
10188 reg_last_set_sign_bit_copies[i] = 0;
10189 reg_last_death[i] = 0;
10192 /* Mark registers that are being referenced in this value. */
10193 if (value)
10194 update_table_tick (value);
10196 /* Now update the status of each register being set.
10197 If someone is using this register in this block, set this register
10198 to invalid since we will get confused between the two lives in this
10199 basic block. This makes using this register always invalid. In cse, we
10200 scan the table to invalidate all entries using this register, but this
10201 is too much work for us. */
10203 for (i = regno; i < endregno; i++)
10205 reg_last_set_label[i] = label_tick;
10206 if (value && reg_last_set_table_tick[i] == label_tick)
10207 reg_last_set_invalid[i] = 1;
10208 else
10209 reg_last_set_invalid[i] = 0;
10212 /* The value being assigned might refer to X (like in "x++;"). In that
10213 case, we must replace it with (clobber (const_int 0)) to prevent
10214 infinite loops. */
10215 if (value && ! get_last_value_validate (&value, insn,
10216 reg_last_set_label[regno], 0))
10218 value = copy_rtx (value);
10219 if (! get_last_value_validate (&value, insn,
10220 reg_last_set_label[regno], 1))
10221 value = 0;
10224 /* For the main register being modified, update the value, the mode, the
10225 nonzero bits, and the number of sign bit copies. */
10227 reg_last_set_value[regno] = value;
10229 if (value)
10231 subst_low_cuid = INSN_CUID (insn);
10232 reg_last_set_mode[regno] = GET_MODE (reg);
10233 reg_last_set_nonzero_bits[regno] = nonzero_bits (value, GET_MODE (reg));
10234 reg_last_set_sign_bit_copies[regno]
10235 = num_sign_bit_copies (value, GET_MODE (reg));
10239 /* Used for communication between the following two routines. */
10240 static rtx record_dead_insn;
10242 /* Called via note_stores from record_dead_and_set_regs to handle one
10243 SET or CLOBBER in an insn. */
10245 static void
10246 record_dead_and_set_regs_1 (dest, setter)
10247 rtx dest, setter;
10249 if (GET_CODE (dest) == SUBREG)
10250 dest = SUBREG_REG (dest);
10252 if (GET_CODE (dest) == REG)
10254 /* If we are setting the whole register, we know its value. Otherwise
10255 show that we don't know the value. We can handle SUBREG in
10256 some cases. */
10257 if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
10258 record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
10259 else if (GET_CODE (setter) == SET
10260 && GET_CODE (SET_DEST (setter)) == SUBREG
10261 && SUBREG_REG (SET_DEST (setter)) == dest
10262 && GET_MODE_BITSIZE (GET_MODE (dest)) <= BITS_PER_WORD
10263 && subreg_lowpart_p (SET_DEST (setter)))
10264 record_value_for_reg (dest, record_dead_insn,
10265 gen_lowpart_for_combine (GET_MODE (dest),
10266 SET_SRC (setter)));
10267 else
10268 record_value_for_reg (dest, record_dead_insn, NULL_RTX);
10270 else if (GET_CODE (dest) == MEM
10271 /* Ignore pushes, they clobber nothing. */
10272 && ! push_operand (dest, GET_MODE (dest)))
10273 mem_last_set = INSN_CUID (record_dead_insn);
10276 /* Update the records of when each REG was most recently set or killed
10277 for the things done by INSN. This is the last thing done in processing
10278 INSN in the combiner loop.
10280 We update reg_last_set, reg_last_set_value, reg_last_set_mode,
10281 reg_last_set_nonzero_bits, reg_last_set_sign_bit_copies, reg_last_death,
10282 and also the similar information mem_last_set (which insn most recently
10283 modified memory) and last_call_cuid (which insn was the most recent
10284 subroutine call). */
10286 static void
10287 record_dead_and_set_regs (insn)
10288 rtx insn;
10290 register rtx link;
10291 int i;
10293 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
10295 if (REG_NOTE_KIND (link) == REG_DEAD
10296 && GET_CODE (XEXP (link, 0)) == REG)
10298 int regno = REGNO (XEXP (link, 0));
10299 int endregno
10300 = regno + (regno < FIRST_PSEUDO_REGISTER
10301 ? HARD_REGNO_NREGS (regno, GET_MODE (XEXP (link, 0)))
10302 : 1);
10304 for (i = regno; i < endregno; i++)
10305 reg_last_death[i] = insn;
10307 else if (REG_NOTE_KIND (link) == REG_INC)
10308 record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
10311 if (GET_CODE (insn) == CALL_INSN)
10313 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
10314 if (call_used_regs[i])
10316 reg_last_set_value[i] = 0;
10317 reg_last_set_mode[i] = 0;
10318 reg_last_set_nonzero_bits[i] = 0;
10319 reg_last_set_sign_bit_copies[i] = 0;
10320 reg_last_death[i] = 0;
10323 last_call_cuid = mem_last_set = INSN_CUID (insn);
10326 record_dead_insn = insn;
10327 note_stores (PATTERN (insn), record_dead_and_set_regs_1);
10330 /* Utility routine for the following function. Verify that all the registers
10331 mentioned in *LOC are valid when *LOC was part of a value set when
10332 label_tick == TICK. Return 0 if some are not.
10334 If REPLACE is non-zero, replace the invalid reference with
10335 (clobber (const_int 0)) and return 1. This replacement is useful because
10336 we often can get useful information about the form of a value (e.g., if
10337 it was produced by a shift that always produces -1 or 0) even though
10338 we don't know exactly what registers it was produced from. */
10340 static int
10341 get_last_value_validate (loc, insn, tick, replace)
10342 rtx *loc;
10343 rtx insn;
10344 int tick;
10345 int replace;
10347 rtx x = *loc;
10348 char *fmt = GET_RTX_FORMAT (GET_CODE (x));
10349 int len = GET_RTX_LENGTH (GET_CODE (x));
10350 int i;
10352 if (GET_CODE (x) == REG)
10354 int regno = REGNO (x);
10355 int endregno = regno + (regno < FIRST_PSEUDO_REGISTER
10356 ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
10357 int j;
10359 for (j = regno; j < endregno; j++)
10360 if (reg_last_set_invalid[j]
10361 /* If this is a pseudo-register that was only set once, it is
10362 always valid. */
10363 || (! (regno >= FIRST_PSEUDO_REGISTER && REG_N_SETS (regno) == 1)
10364 && reg_last_set_label[j] > tick))
10366 if (replace)
10367 *loc = gen_rtx (CLOBBER, GET_MODE (x), const0_rtx);
10368 return replace;
10371 return 1;
10373 /* If this is a memory reference, make sure that there were
10374 no stores after it that might have clobbered the value. We don't
10375 have alias info, so we assume any store invalidates it. */
10376 else if (GET_CODE (x) == MEM && ! RTX_UNCHANGING_P (x)
10377 && INSN_CUID (insn) <= mem_last_set)
10379 if (replace)
10380 *loc = gen_rtx (CLOBBER, GET_MODE (x), const0_rtx);
10381 return replace;
10384 for (i = 0; i < len; i++)
10385 if ((fmt[i] == 'e'
10386 && get_last_value_validate (&XEXP (x, i), insn, tick, replace) == 0)
10387 /* Don't bother with these. They shouldn't occur anyway. */
10388 || fmt[i] == 'E')
10389 return 0;
10391 /* If we haven't found a reason for it to be invalid, it is valid. */
10392 return 1;
10395 /* Get the last value assigned to X, if known. Some registers
10396 in the value may be replaced with (clobber (const_int 0)) if their value
10397 is known longer known reliably. */
10399 static rtx
10400 get_last_value (x)
10401 rtx x;
10403 int regno;
10404 rtx value;
10406 /* If this is a non-paradoxical SUBREG, get the value of its operand and
10407 then convert it to the desired mode. If this is a paradoxical SUBREG,
10408 we cannot predict what values the "extra" bits might have. */
10409 if (GET_CODE (x) == SUBREG
10410 && subreg_lowpart_p (x)
10411 && (GET_MODE_SIZE (GET_MODE (x))
10412 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
10413 && (value = get_last_value (SUBREG_REG (x))) != 0)
10414 return gen_lowpart_for_combine (GET_MODE (x), value);
10416 if (GET_CODE (x) != REG)
10417 return 0;
10419 regno = REGNO (x);
10420 value = reg_last_set_value[regno];
10422 /* If we don't have a value or if it isn't for this basic block,
10423 return 0. */
10425 if (value == 0
10426 || (REG_N_SETS (regno) != 1
10427 && reg_last_set_label[regno] != label_tick))
10428 return 0;
10430 /* If the value was set in a later insn than the ones we are processing,
10431 we can't use it even if the register was only set once, but make a quick
10432 check to see if the previous insn set it to something. This is commonly
10433 the case when the same pseudo is used by repeated insns.
10435 This does not work if there exists an instruction which is temporarily
10436 not on the insn chain. */
10438 if (INSN_CUID (reg_last_set[regno]) >= subst_low_cuid)
10440 rtx insn, set;
10442 /* We can not do anything useful in this case, because there is
10443 an instruction which is not on the insn chain. */
10444 if (subst_prev_insn)
10445 return 0;
10447 /* Skip over USE insns. They are not useful here, and they may have
10448 been made by combine, in which case they do not have a INSN_CUID
10449 value. We can't use prev_real_insn, because that would incorrectly
10450 take us backwards across labels. Skip over BARRIERs also, since
10451 they could have been made by combine. If we see one, we must be
10452 optimizing dead code, so it doesn't matter what we do. */
10453 for (insn = prev_nonnote_insn (subst_insn);
10454 insn && ((GET_CODE (insn) == INSN
10455 && GET_CODE (PATTERN (insn)) == USE)
10456 || GET_CODE (insn) == BARRIER
10457 || INSN_CUID (insn) >= subst_low_cuid);
10458 insn = prev_nonnote_insn (insn))
10461 if (insn
10462 && (set = single_set (insn)) != 0
10463 && rtx_equal_p (SET_DEST (set), x))
10465 value = SET_SRC (set);
10467 /* Make sure that VALUE doesn't reference X. Replace any
10468 explicit references with a CLOBBER. If there are any remaining
10469 references (rare), don't use the value. */
10471 if (reg_mentioned_p (x, value))
10472 value = replace_rtx (copy_rtx (value), x,
10473 gen_rtx (CLOBBER, GET_MODE (x), const0_rtx));
10475 if (reg_overlap_mentioned_p (x, value))
10476 return 0;
10478 else
10479 return 0;
10482 /* If the value has all its registers valid, return it. */
10483 if (get_last_value_validate (&value, reg_last_set[regno],
10484 reg_last_set_label[regno], 0))
10485 return value;
10487 /* Otherwise, make a copy and replace any invalid register with
10488 (clobber (const_int 0)). If that fails for some reason, return 0. */
10490 value = copy_rtx (value);
10491 if (get_last_value_validate (&value, reg_last_set[regno],
10492 reg_last_set_label[regno], 1))
10493 return value;
10495 return 0;
10498 /* Return nonzero if expression X refers to a REG or to memory
10499 that is set in an instruction more recent than FROM_CUID. */
10501 static int
10502 use_crosses_set_p (x, from_cuid)
10503 register rtx x;
10504 int from_cuid;
10506 register char *fmt;
10507 register int i;
10508 register enum rtx_code code = GET_CODE (x);
10510 if (code == REG)
10512 register int regno = REGNO (x);
10513 int endreg = regno + (regno < FIRST_PSEUDO_REGISTER
10514 ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
10516 #ifdef PUSH_ROUNDING
10517 /* Don't allow uses of the stack pointer to be moved,
10518 because we don't know whether the move crosses a push insn. */
10519 if (regno == STACK_POINTER_REGNUM)
10520 return 1;
10521 #endif
10522 for (;regno < endreg; regno++)
10523 if (reg_last_set[regno]
10524 && INSN_CUID (reg_last_set[regno]) > from_cuid)
10525 return 1;
10526 return 0;
10529 if (code == MEM && mem_last_set > from_cuid)
10530 return 1;
10532 fmt = GET_RTX_FORMAT (code);
10534 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
10536 if (fmt[i] == 'E')
10538 register int j;
10539 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
10540 if (use_crosses_set_p (XVECEXP (x, i, j), from_cuid))
10541 return 1;
10543 else if (fmt[i] == 'e'
10544 && use_crosses_set_p (XEXP (x, i), from_cuid))
10545 return 1;
10547 return 0;
10550 /* Define three variables used for communication between the following
10551 routines. */
10553 static int reg_dead_regno, reg_dead_endregno;
10554 static int reg_dead_flag;
10556 /* Function called via note_stores from reg_dead_at_p.
10558 If DEST is within [reg_dead_regno, reg_dead_endregno), set
10559 reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET. */
10561 static void
10562 reg_dead_at_p_1 (dest, x)
10563 rtx dest;
10564 rtx x;
10566 int regno, endregno;
10568 if (GET_CODE (dest) != REG)
10569 return;
10571 regno = REGNO (dest);
10572 endregno = regno + (regno < FIRST_PSEUDO_REGISTER
10573 ? HARD_REGNO_NREGS (regno, GET_MODE (dest)) : 1);
10575 if (reg_dead_endregno > regno && reg_dead_regno < endregno)
10576 reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
10579 /* Return non-zero if REG is known to be dead at INSN.
10581 We scan backwards from INSN. If we hit a REG_DEAD note or a CLOBBER
10582 referencing REG, it is dead. If we hit a SET referencing REG, it is
10583 live. Otherwise, see if it is live or dead at the start of the basic
10584 block we are in. Hard regs marked as being live in NEWPAT_USED_REGS
10585 must be assumed to be always live. */
10587 static int
10588 reg_dead_at_p (reg, insn)
10589 rtx reg;
10590 rtx insn;
10592 int block, i;
10594 /* Set variables for reg_dead_at_p_1. */
10595 reg_dead_regno = REGNO (reg);
10596 reg_dead_endregno = reg_dead_regno + (reg_dead_regno < FIRST_PSEUDO_REGISTER
10597 ? HARD_REGNO_NREGS (reg_dead_regno,
10598 GET_MODE (reg))
10599 : 1);
10601 reg_dead_flag = 0;
10603 /* Check that reg isn't mentioned in NEWPAT_USED_REGS. */
10604 if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
10606 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
10607 if (TEST_HARD_REG_BIT (newpat_used_regs, i))
10608 return 0;
10611 /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, label, or
10612 beginning of function. */
10613 for (; insn && GET_CODE (insn) != CODE_LABEL && GET_CODE (insn) != BARRIER;
10614 insn = prev_nonnote_insn (insn))
10616 note_stores (PATTERN (insn), reg_dead_at_p_1);
10617 if (reg_dead_flag)
10618 return reg_dead_flag == 1 ? 1 : 0;
10620 if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
10621 return 1;
10624 /* Get the basic block number that we were in. */
10625 if (insn == 0)
10626 block = 0;
10627 else
10629 for (block = 0; block < n_basic_blocks; block++)
10630 if (insn == basic_block_head[block])
10631 break;
10633 if (block == n_basic_blocks)
10634 return 0;
10637 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
10638 if (REGNO_REG_SET_P (basic_block_live_at_start[block], i))
10639 return 0;
10641 return 1;
10644 /* Note hard registers in X that are used. This code is similar to
10645 that in flow.c, but much simpler since we don't care about pseudos. */
10647 static void
10648 mark_used_regs_combine (x)
10649 rtx x;
10651 register RTX_CODE code = GET_CODE (x);
10652 register int regno;
10653 int i;
10655 switch (code)
10657 case LABEL_REF:
10658 case SYMBOL_REF:
10659 case CONST_INT:
10660 case CONST:
10661 case CONST_DOUBLE:
10662 case PC:
10663 case ADDR_VEC:
10664 case ADDR_DIFF_VEC:
10665 case ASM_INPUT:
10666 #ifdef HAVE_cc0
10667 /* CC0 must die in the insn after it is set, so we don't need to take
10668 special note of it here. */
10669 case CC0:
10670 #endif
10671 return;
10673 case CLOBBER:
10674 /* If we are clobbering a MEM, mark any hard registers inside the
10675 address as used. */
10676 if (GET_CODE (XEXP (x, 0)) == MEM)
10677 mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
10678 return;
10680 case REG:
10681 regno = REGNO (x);
10682 /* A hard reg in a wide mode may really be multiple registers.
10683 If so, mark all of them just like the first. */
10684 if (regno < FIRST_PSEUDO_REGISTER)
10686 /* None of this applies to the stack, frame or arg pointers */
10687 if (regno == STACK_POINTER_REGNUM
10688 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
10689 || regno == HARD_FRAME_POINTER_REGNUM
10690 #endif
10691 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
10692 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
10693 #endif
10694 || regno == FRAME_POINTER_REGNUM)
10695 return;
10697 i = HARD_REGNO_NREGS (regno, GET_MODE (x));
10698 while (i-- > 0)
10699 SET_HARD_REG_BIT (newpat_used_regs, regno + i);
10701 return;
10703 case SET:
10705 /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
10706 the address. */
10707 register rtx testreg = SET_DEST (x);
10709 while (GET_CODE (testreg) == SUBREG
10710 || GET_CODE (testreg) == ZERO_EXTRACT
10711 || GET_CODE (testreg) == SIGN_EXTRACT
10712 || GET_CODE (testreg) == STRICT_LOW_PART)
10713 testreg = XEXP (testreg, 0);
10715 if (GET_CODE (testreg) == MEM)
10716 mark_used_regs_combine (XEXP (testreg, 0));
10718 mark_used_regs_combine (SET_SRC (x));
10719 return;
10723 /* Recursively scan the operands of this expression. */
10726 register char *fmt = GET_RTX_FORMAT (code);
10728 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
10730 if (fmt[i] == 'e')
10731 mark_used_regs_combine (XEXP (x, i));
10732 else if (fmt[i] == 'E')
10734 register int j;
10736 for (j = 0; j < XVECLEN (x, i); j++)
10737 mark_used_regs_combine (XVECEXP (x, i, j));
10744 /* Remove register number REGNO from the dead registers list of INSN.
10746 Return the note used to record the death, if there was one. */
10749 remove_death (regno, insn)
10750 int regno;
10751 rtx insn;
10753 register rtx note = find_regno_note (insn, REG_DEAD, regno);
10755 if (note)
10757 REG_N_DEATHS (regno)--;
10758 remove_note (insn, note);
10761 return note;
10764 /* For each register (hardware or pseudo) used within expression X, if its
10765 death is in an instruction with cuid between FROM_CUID (inclusive) and
10766 TO_INSN (exclusive), put a REG_DEAD note for that register in the
10767 list headed by PNOTES.
10769 That said, don't move registers killed by maybe_kill_insn.
10771 This is done when X is being merged by combination into TO_INSN. These
10772 notes will then be distributed as needed. */
10774 static void
10775 move_deaths (x, maybe_kill_insn, from_cuid, to_insn, pnotes)
10776 rtx x;
10777 rtx maybe_kill_insn;
10778 int from_cuid;
10779 rtx to_insn;
10780 rtx *pnotes;
10782 register char *fmt;
10783 register int len, i;
10784 register enum rtx_code code = GET_CODE (x);
10786 if (code == REG)
10788 register int regno = REGNO (x);
10789 register rtx where_dead = reg_last_death[regno];
10790 register rtx before_dead, after_dead;
10792 /* Don't move the register if it gets killed in between from and to */
10793 if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
10794 && !reg_referenced_p (x, maybe_kill_insn))
10795 return;
10797 /* WHERE_DEAD could be a USE insn made by combine, so first we
10798 make sure that we have insns with valid INSN_CUID values. */
10799 before_dead = where_dead;
10800 while (before_dead && INSN_UID (before_dead) > max_uid_cuid)
10801 before_dead = PREV_INSN (before_dead);
10802 after_dead = where_dead;
10803 while (after_dead && INSN_UID (after_dead) > max_uid_cuid)
10804 after_dead = NEXT_INSN (after_dead);
10806 if (before_dead && after_dead
10807 && INSN_CUID (before_dead) >= from_cuid
10808 && (INSN_CUID (after_dead) < INSN_CUID (to_insn)
10809 || (where_dead != after_dead
10810 && INSN_CUID (after_dead) == INSN_CUID (to_insn))))
10812 rtx note = remove_death (regno, where_dead);
10814 /* It is possible for the call above to return 0. This can occur
10815 when reg_last_death points to I2 or I1 that we combined with.
10816 In that case make a new note.
10818 We must also check for the case where X is a hard register
10819 and NOTE is a death note for a range of hard registers
10820 including X. In that case, we must put REG_DEAD notes for
10821 the remaining registers in place of NOTE. */
10823 if (note != 0 && regno < FIRST_PSEUDO_REGISTER
10824 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
10825 > GET_MODE_SIZE (GET_MODE (x))))
10827 int deadregno = REGNO (XEXP (note, 0));
10828 int deadend
10829 = (deadregno + HARD_REGNO_NREGS (deadregno,
10830 GET_MODE (XEXP (note, 0))));
10831 int ourend = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
10832 int i;
10834 for (i = deadregno; i < deadend; i++)
10835 if (i < regno || i >= ourend)
10836 REG_NOTES (where_dead)
10837 = gen_rtx (EXPR_LIST, REG_DEAD,
10838 gen_rtx (REG, reg_raw_mode[i], i),
10839 REG_NOTES (where_dead));
10841 /* If we didn't find any note, or if we found a REG_DEAD note that
10842 covers only part of the given reg, and we have a multi-reg hard
10843 register, then to be safe we must check for REG_DEAD notes
10844 for each register other than the first. They could have
10845 their own REG_DEAD notes lying around. */
10846 else if ((note == 0
10847 || (note != 0
10848 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
10849 < GET_MODE_SIZE (GET_MODE (x)))))
10850 && regno < FIRST_PSEUDO_REGISTER
10851 && HARD_REGNO_NREGS (regno, GET_MODE (x)) > 1)
10853 int ourend = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
10854 int i, offset;
10855 rtx oldnotes = 0;
10857 if (note)
10858 offset = HARD_REGNO_NREGS (regno, GET_MODE (XEXP (note, 0)));
10859 else
10860 offset = 1;
10862 for (i = regno + offset; i < ourend; i++)
10863 move_deaths (gen_rtx (REG, reg_raw_mode[i], i),
10864 maybe_kill_insn, from_cuid, to_insn, &oldnotes);
10867 if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
10869 XEXP (note, 1) = *pnotes;
10870 *pnotes = note;
10872 else
10873 *pnotes = gen_rtx (EXPR_LIST, REG_DEAD, x, *pnotes);
10875 REG_N_DEATHS (regno)++;
10878 return;
10881 else if (GET_CODE (x) == SET)
10883 rtx dest = SET_DEST (x);
10885 move_deaths (SET_SRC (x), maybe_kill_insn, from_cuid, to_insn, pnotes);
10887 /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
10888 that accesses one word of a multi-word item, some
10889 piece of everything register in the expression is used by
10890 this insn, so remove any old death. */
10892 if (GET_CODE (dest) == ZERO_EXTRACT
10893 || GET_CODE (dest) == STRICT_LOW_PART
10894 || (GET_CODE (dest) == SUBREG
10895 && (((GET_MODE_SIZE (GET_MODE (dest))
10896 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
10897 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
10898 + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
10900 move_deaths (dest, maybe_kill_insn, from_cuid, to_insn, pnotes);
10901 return;
10904 /* If this is some other SUBREG, we know it replaces the entire
10905 value, so use that as the destination. */
10906 if (GET_CODE (dest) == SUBREG)
10907 dest = SUBREG_REG (dest);
10909 /* If this is a MEM, adjust deaths of anything used in the address.
10910 For a REG (the only other possibility), the entire value is
10911 being replaced so the old value is not used in this insn. */
10913 if (GET_CODE (dest) == MEM)
10914 move_deaths (XEXP (dest, 0), maybe_kill_insn, from_cuid,
10915 to_insn, pnotes);
10916 return;
10919 else if (GET_CODE (x) == CLOBBER)
10920 return;
10922 len = GET_RTX_LENGTH (code);
10923 fmt = GET_RTX_FORMAT (code);
10925 for (i = 0; i < len; i++)
10927 if (fmt[i] == 'E')
10929 register int j;
10930 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
10931 move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_cuid,
10932 to_insn, pnotes);
10934 else if (fmt[i] == 'e')
10935 move_deaths (XEXP (x, i), maybe_kill_insn, from_cuid, to_insn, pnotes);
10939 /* Return 1 if X is the target of a bit-field assignment in BODY, the
10940 pattern of an insn. X must be a REG. */
10942 static int
10943 reg_bitfield_target_p (x, body)
10944 rtx x;
10945 rtx body;
10947 int i;
10949 if (GET_CODE (body) == SET)
10951 rtx dest = SET_DEST (body);
10952 rtx target;
10953 int regno, tregno, endregno, endtregno;
10955 if (GET_CODE (dest) == ZERO_EXTRACT)
10956 target = XEXP (dest, 0);
10957 else if (GET_CODE (dest) == STRICT_LOW_PART)
10958 target = SUBREG_REG (XEXP (dest, 0));
10959 else
10960 return 0;
10962 if (GET_CODE (target) == SUBREG)
10963 target = SUBREG_REG (target);
10965 if (GET_CODE (target) != REG)
10966 return 0;
10968 tregno = REGNO (target), regno = REGNO (x);
10969 if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
10970 return target == x;
10972 endtregno = tregno + HARD_REGNO_NREGS (tregno, GET_MODE (target));
10973 endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
10975 return endregno > tregno && regno < endtregno;
10978 else if (GET_CODE (body) == PARALLEL)
10979 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
10980 if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
10981 return 1;
10983 return 0;
10986 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
10987 as appropriate. I3 and I2 are the insns resulting from the combination
10988 insns including FROM (I2 may be zero).
10990 ELIM_I2 and ELIM_I1 are either zero or registers that we know will
10991 not need REG_DEAD notes because they are being substituted for. This
10992 saves searching in the most common cases.
10994 Each note in the list is either ignored or placed on some insns, depending
10995 on the type of note. */
10997 static void
10998 distribute_notes (notes, from_insn, i3, i2, elim_i2, elim_i1)
10999 rtx notes;
11000 rtx from_insn;
11001 rtx i3, i2;
11002 rtx elim_i2, elim_i1;
11004 rtx note, next_note;
11005 rtx tem;
11007 for (note = notes; note; note = next_note)
11009 rtx place = 0, place2 = 0;
11011 /* If this NOTE references a pseudo register, ensure it references
11012 the latest copy of that register. */
11013 if (XEXP (note, 0) && GET_CODE (XEXP (note, 0)) == REG
11014 && REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER)
11015 XEXP (note, 0) = regno_reg_rtx[REGNO (XEXP (note, 0))];
11017 next_note = XEXP (note, 1);
11018 switch (REG_NOTE_KIND (note))
11020 case REG_BR_PROB:
11021 case REG_EXEC_COUNT:
11022 /* Doesn't matter much where we put this, as long as it's somewhere.
11023 It is preferable to keep these notes on branches, which is most
11024 likely to be i3. */
11025 place = i3;
11026 break;
11028 case REG_UNUSED:
11029 /* Any clobbers for i3 may still exist, and so we must process
11030 REG_UNUSED notes from that insn.
11032 Any clobbers from i2 or i1 can only exist if they were added by
11033 recog_for_combine. In that case, recog_for_combine created the
11034 necessary REG_UNUSED notes. Trying to keep any original
11035 REG_UNUSED notes from these insns can cause incorrect output
11036 if it is for the same register as the original i3 dest.
11037 In that case, we will notice that the register is set in i3,
11038 and then add a REG_UNUSED note for the destination of i3, which
11039 is wrong. However, it is possible to have REG_UNUSED notes from
11040 i2 or i1 for register which were both used and clobbered, so
11041 we keep notes from i2 or i1 if they will turn into REG_DEAD
11042 notes. */
11044 /* If this register is set or clobbered in I3, put the note there
11045 unless there is one already. */
11046 if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
11048 if (from_insn != i3)
11049 break;
11051 if (! (GET_CODE (XEXP (note, 0)) == REG
11052 ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
11053 : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
11054 place = i3;
11056 /* Otherwise, if this register is used by I3, then this register
11057 now dies here, so we must put a REG_DEAD note here unless there
11058 is one already. */
11059 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
11060 && ! (GET_CODE (XEXP (note, 0)) == REG
11061 ? find_regno_note (i3, REG_DEAD, REGNO (XEXP (note, 0)))
11062 : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
11064 PUT_REG_NOTE_KIND (note, REG_DEAD);
11065 place = i3;
11067 break;
11069 case REG_EQUAL:
11070 case REG_EQUIV:
11071 case REG_NONNEG:
11072 case REG_NOALIAS:
11073 /* These notes say something about results of an insn. We can
11074 only support them if they used to be on I3 in which case they
11075 remain on I3. Otherwise they are ignored.
11077 If the note refers to an expression that is not a constant, we
11078 must also ignore the note since we cannot tell whether the
11079 equivalence is still true. It might be possible to do
11080 slightly better than this (we only have a problem if I2DEST
11081 or I1DEST is present in the expression), but it doesn't
11082 seem worth the trouble. */
11084 if (from_insn == i3
11085 && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
11086 place = i3;
11087 break;
11089 case REG_INC:
11090 case REG_NO_CONFLICT:
11091 case REG_LABEL:
11092 /* These notes say something about how a register is used. They must
11093 be present on any use of the register in I2 or I3. */
11094 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
11095 place = i3;
11097 if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
11099 if (place)
11100 place2 = i2;
11101 else
11102 place = i2;
11104 break;
11106 case REG_WAS_0:
11107 /* It is too much trouble to try to see if this note is still
11108 correct in all situations. It is better to simply delete it. */
11109 break;
11111 case REG_RETVAL:
11112 /* If the insn previously containing this note still exists,
11113 put it back where it was. Otherwise move it to the previous
11114 insn. Adjust the corresponding REG_LIBCALL note. */
11115 if (GET_CODE (from_insn) != NOTE)
11116 place = from_insn;
11117 else
11119 tem = find_reg_note (XEXP (note, 0), REG_LIBCALL, NULL_RTX);
11120 place = prev_real_insn (from_insn);
11121 if (tem && place)
11122 XEXP (tem, 0) = place;
11124 break;
11126 case REG_LIBCALL:
11127 /* This is handled similarly to REG_RETVAL. */
11128 if (GET_CODE (from_insn) != NOTE)
11129 place = from_insn;
11130 else
11132 tem = find_reg_note (XEXP (note, 0), REG_RETVAL, NULL_RTX);
11133 place = next_real_insn (from_insn);
11134 if (tem && place)
11135 XEXP (tem, 0) = place;
11137 break;
11139 case REG_DEAD:
11140 /* If the register is used as an input in I3, it dies there.
11141 Similarly for I2, if it is non-zero and adjacent to I3.
11143 If the register is not used as an input in either I3 or I2
11144 and it is not one of the registers we were supposed to eliminate,
11145 there are two possibilities. We might have a non-adjacent I2
11146 or we might have somehow eliminated an additional register
11147 from a computation. For example, we might have had A & B where
11148 we discover that B will always be zero. In this case we will
11149 eliminate the reference to A.
11151 In both cases, we must search to see if we can find a previous
11152 use of A and put the death note there. */
11154 if (from_insn
11155 && GET_CODE (from_insn) == CALL_INSN
11156 && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
11157 place = from_insn;
11158 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
11159 place = i3;
11160 else if (i2 != 0 && next_nonnote_insn (i2) == i3
11161 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
11162 place = i2;
11164 if (XEXP (note, 0) == elim_i2 || XEXP (note, 0) == elim_i1)
11165 break;
11167 /* If the register is used in both I2 and I3 and it dies in I3,
11168 we might have added another reference to it. If reg_n_refs
11169 was 2, bump it to 3. This has to be correct since the
11170 register must have been set somewhere. The reason this is
11171 done is because local-alloc.c treats 2 references as a
11172 special case. */
11174 if (place == i3 && i2 != 0 && GET_CODE (XEXP (note, 0)) == REG
11175 && REG_N_REFS (REGNO (XEXP (note, 0)))== 2
11176 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
11177 REG_N_REFS (REGNO (XEXP (note, 0))) = 3;
11179 if (place == 0)
11181 for (tem = prev_nonnote_insn (i3);
11182 place == 0 && tem
11183 && (GET_CODE (tem) == INSN || GET_CODE (tem) == CALL_INSN);
11184 tem = prev_nonnote_insn (tem))
11186 /* If the register is being set at TEM, see if that is all
11187 TEM is doing. If so, delete TEM. Otherwise, make this
11188 into a REG_UNUSED note instead. */
11189 if (reg_set_p (XEXP (note, 0), PATTERN (tem)))
11191 rtx set = single_set (tem);
11193 /* Verify that it was the set, and not a clobber that
11194 modified the register. */
11196 if (set != 0 && ! side_effects_p (SET_SRC (set))
11197 && (rtx_equal_p (XEXP (note, 0), SET_DEST (set))
11198 || (GET_CODE (SET_DEST (set)) == SUBREG
11199 && rtx_equal_p (XEXP (note, 0),
11200 XEXP (SET_DEST (set), 0)))))
11202 /* Move the notes and links of TEM elsewhere.
11203 This might delete other dead insns recursively.
11204 First set the pattern to something that won't use
11205 any register. */
11207 PATTERN (tem) = pc_rtx;
11209 distribute_notes (REG_NOTES (tem), tem, tem,
11210 NULL_RTX, NULL_RTX, NULL_RTX);
11211 distribute_links (LOG_LINKS (tem));
11213 PUT_CODE (tem, NOTE);
11214 NOTE_LINE_NUMBER (tem) = NOTE_INSN_DELETED;
11215 NOTE_SOURCE_FILE (tem) = 0;
11217 else
11219 PUT_REG_NOTE_KIND (note, REG_UNUSED);
11221 /* If there isn't already a REG_UNUSED note, put one
11222 here. */
11223 if (! find_regno_note (tem, REG_UNUSED,
11224 REGNO (XEXP (note, 0))))
11225 place = tem;
11226 break;
11229 else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem))
11230 || (GET_CODE (tem) == CALL_INSN
11231 && find_reg_fusage (tem, USE, XEXP (note, 0))))
11233 place = tem;
11235 /* If we are doing a 3->2 combination, and we have a
11236 register which formerly died in i3 and was not used
11237 by i2, which now no longer dies in i3 and is used in
11238 i2 but does not die in i2, and place is between i2
11239 and i3, then we may need to move a link from place to
11240 i2. */
11241 if (i2 && INSN_UID (place) <= max_uid_cuid
11242 && INSN_CUID (place) > INSN_CUID (i2)
11243 && from_insn && INSN_CUID (from_insn) > INSN_CUID (i2)
11244 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
11246 rtx links = LOG_LINKS (place);
11247 LOG_LINKS (place) = 0;
11248 distribute_links (links);
11250 break;
11254 /* If we haven't found an insn for the death note and it
11255 is still a REG_DEAD note, but we have hit a CODE_LABEL,
11256 insert a USE insn for the register at that label and
11257 put the death node there. This prevents problems with
11258 call-state tracking in caller-save.c. */
11259 if (REG_NOTE_KIND (note) == REG_DEAD && place == 0 && tem != 0)
11261 place
11262 = emit_insn_after (gen_rtx (USE, VOIDmode, XEXP (note, 0)),
11263 tem);
11265 /* If this insn was emitted between blocks, then update
11266 basic_block_head of the current block to include it. */
11267 if (basic_block_end[this_basic_block - 1] == tem)
11268 basic_block_head[this_basic_block] = place;
11272 /* If the register is set or already dead at PLACE, we needn't do
11273 anything with this note if it is still a REG_DEAD note.
11275 Note that we cannot use just `dead_or_set_p' here since we can
11276 convert an assignment to a register into a bit-field assignment.
11277 Therefore, we must also omit the note if the register is the
11278 target of a bitfield assignment. */
11280 if (place && REG_NOTE_KIND (note) == REG_DEAD)
11282 int regno = REGNO (XEXP (note, 0));
11284 if (dead_or_set_p (place, XEXP (note, 0))
11285 || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
11287 /* Unless the register previously died in PLACE, clear
11288 reg_last_death. [I no longer understand why this is
11289 being done.] */
11290 if (reg_last_death[regno] != place)
11291 reg_last_death[regno] = 0;
11292 place = 0;
11294 else
11295 reg_last_death[regno] = place;
11297 /* If this is a death note for a hard reg that is occupying
11298 multiple registers, ensure that we are still using all
11299 parts of the object. If we find a piece of the object
11300 that is unused, we must add a USE for that piece before
11301 PLACE and put the appropriate REG_DEAD note on it.
11303 An alternative would be to put a REG_UNUSED for the pieces
11304 on the insn that set the register, but that can't be done if
11305 it is not in the same block. It is simpler, though less
11306 efficient, to add the USE insns. */
11308 if (place && regno < FIRST_PSEUDO_REGISTER
11309 && HARD_REGNO_NREGS (regno, GET_MODE (XEXP (note, 0))) > 1)
11311 int endregno
11312 = regno + HARD_REGNO_NREGS (regno,
11313 GET_MODE (XEXP (note, 0)));
11314 int all_used = 1;
11315 int i;
11317 for (i = regno; i < endregno; i++)
11318 if (! refers_to_regno_p (i, i + 1, PATTERN (place), 0)
11319 && ! find_regno_fusage (place, USE, i))
11321 rtx piece = gen_rtx (REG, reg_raw_mode[i], i);
11322 rtx p;
11324 /* See if we already placed a USE note for this
11325 register in front of PLACE. */
11326 for (p = place;
11327 GET_CODE (PREV_INSN (p)) == INSN
11328 && GET_CODE (PATTERN (PREV_INSN (p))) == USE;
11329 p = PREV_INSN (p))
11330 if (rtx_equal_p (piece,
11331 XEXP (PATTERN (PREV_INSN (p)), 0)))
11333 p = 0;
11334 break;
11337 if (p)
11339 rtx use_insn
11340 = emit_insn_before (gen_rtx (USE, VOIDmode,
11341 piece),
11343 REG_NOTES (use_insn)
11344 = gen_rtx (EXPR_LIST, REG_DEAD, piece,
11345 REG_NOTES (use_insn));
11348 all_used = 0;
11351 /* Check for the case where the register dying partially
11352 overlaps the register set by this insn. */
11353 if (all_used)
11354 for (i = regno; i < endregno; i++)
11355 if (dead_or_set_regno_p (place, i))
11357 all_used = 0;
11358 break;
11361 if (! all_used)
11363 /* Put only REG_DEAD notes for pieces that are
11364 still used and that are not already dead or set. */
11366 for (i = regno; i < endregno; i++)
11368 rtx piece = gen_rtx (REG, reg_raw_mode[i], i);
11370 if ((reg_referenced_p (piece, PATTERN (place))
11371 || (GET_CODE (place) == CALL_INSN
11372 && find_reg_fusage (place, USE, piece)))
11373 && ! dead_or_set_p (place, piece)
11374 && ! reg_bitfield_target_p (piece,
11375 PATTERN (place)))
11376 REG_NOTES (place) = gen_rtx (EXPR_LIST, REG_DEAD,
11377 piece,
11378 REG_NOTES (place));
11381 place = 0;
11385 break;
11387 default:
11388 /* Any other notes should not be present at this point in the
11389 compilation. */
11390 abort ();
11393 if (place)
11395 XEXP (note, 1) = REG_NOTES (place);
11396 REG_NOTES (place) = note;
11398 else if ((REG_NOTE_KIND (note) == REG_DEAD
11399 || REG_NOTE_KIND (note) == REG_UNUSED)
11400 && GET_CODE (XEXP (note, 0)) == REG)
11401 REG_N_DEATHS (REGNO (XEXP (note, 0)))--;
11403 if (place2)
11405 if ((REG_NOTE_KIND (note) == REG_DEAD
11406 || REG_NOTE_KIND (note) == REG_UNUSED)
11407 && GET_CODE (XEXP (note, 0)) == REG)
11408 REG_N_DEATHS (REGNO (XEXP (note, 0)))++;
11410 REG_NOTES (place2) = gen_rtx (GET_CODE (note), REG_NOTE_KIND (note),
11411 XEXP (note, 0), REG_NOTES (place2));
11416 /* Similarly to above, distribute the LOG_LINKS that used to be present on
11417 I3, I2, and I1 to new locations. This is also called in one case to
11418 add a link pointing at I3 when I3's destination is changed. */
11420 static void
11421 distribute_links (links)
11422 rtx links;
11424 rtx link, next_link;
11426 for (link = links; link; link = next_link)
11428 rtx place = 0;
11429 rtx insn;
11430 rtx set, reg;
11432 next_link = XEXP (link, 1);
11434 /* If the insn that this link points to is a NOTE or isn't a single
11435 set, ignore it. In the latter case, it isn't clear what we
11436 can do other than ignore the link, since we can't tell which
11437 register it was for. Such links wouldn't be used by combine
11438 anyway.
11440 It is not possible for the destination of the target of the link to
11441 have been changed by combine. The only potential of this is if we
11442 replace I3, I2, and I1 by I3 and I2. But in that case the
11443 destination of I2 also remains unchanged. */
11445 if (GET_CODE (XEXP (link, 0)) == NOTE
11446 || (set = single_set (XEXP (link, 0))) == 0)
11447 continue;
11449 reg = SET_DEST (set);
11450 while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
11451 || GET_CODE (reg) == SIGN_EXTRACT
11452 || GET_CODE (reg) == STRICT_LOW_PART)
11453 reg = XEXP (reg, 0);
11455 /* A LOG_LINK is defined as being placed on the first insn that uses
11456 a register and points to the insn that sets the register. Start
11457 searching at the next insn after the target of the link and stop
11458 when we reach a set of the register or the end of the basic block.
11460 Note that this correctly handles the link that used to point from
11461 I3 to I2. Also note that not much searching is typically done here
11462 since most links don't point very far away. */
11464 for (insn = NEXT_INSN (XEXP (link, 0));
11465 (insn && (this_basic_block == n_basic_blocks - 1
11466 || basic_block_head[this_basic_block + 1] != insn));
11467 insn = NEXT_INSN (insn))
11468 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
11469 && reg_overlap_mentioned_p (reg, PATTERN (insn)))
11471 if (reg_referenced_p (reg, PATTERN (insn)))
11472 place = insn;
11473 break;
11475 else if (GET_CODE (insn) == CALL_INSN
11476 && find_reg_fusage (insn, USE, reg))
11478 place = insn;
11479 break;
11482 /* If we found a place to put the link, place it there unless there
11483 is already a link to the same insn as LINK at that point. */
11485 if (place)
11487 rtx link2;
11489 for (link2 = LOG_LINKS (place); link2; link2 = XEXP (link2, 1))
11490 if (XEXP (link2, 0) == XEXP (link, 0))
11491 break;
11493 if (link2 == 0)
11495 XEXP (link, 1) = LOG_LINKS (place);
11496 LOG_LINKS (place) = link;
11498 /* Set added_links_insn to the earliest insn we added a
11499 link to. */
11500 if (added_links_insn == 0
11501 || INSN_CUID (added_links_insn) > INSN_CUID (place))
11502 added_links_insn = place;
11508 /* Compute INSN_CUID for INSN, which is an insn made by combine. */
11510 static int
11511 insn_cuid (insn)
11512 rtx insn;
11514 while (insn != 0 && INSN_UID (insn) > max_uid_cuid
11515 && GET_CODE (insn) == INSN && GET_CODE (PATTERN (insn)) == USE)
11516 insn = NEXT_INSN (insn);
11518 if (INSN_UID (insn) > max_uid_cuid)
11519 abort ();
11521 return INSN_CUID (insn);
11524 void
11525 dump_combine_stats (file)
11526 FILE *file;
11528 fprintf
11529 (file,
11530 ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
11531 combine_attempts, combine_merges, combine_extras, combine_successes);
11534 void
11535 dump_combine_total_stats (file)
11536 FILE *file;
11538 fprintf
11539 (file,
11540 "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
11541 total_attempts, total_merges, total_extras, total_successes);