allow all arm targets to use -mstructure-size-boundary=XX
[official-gcc.git] / gcc / combine.c
blob9f30666fe7200ba5fed60d218bbf7906cec8c266
1 /* Optimize by combining instructions for GNU compiler.
2 Copyright (C) 1987, 88, 92-98, 1999 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 /* This module is essentially the "combiner" phase of the U. of Arizona
23 Portable Optimizer, but redone to work on our list-structured
24 representation for RTL instead of their string representation.
26 The LOG_LINKS of each insn identify the most recent assignment
27 to each REG used in the insn. It is a list of previous insns,
28 each of which contains a SET for a REG that is used in this insn
29 and not used or set in between. LOG_LINKs never cross basic blocks.
30 They were set up by the preceding pass (lifetime analysis).
32 We try to combine each pair of insns joined by a logical link.
33 We also try to combine triples of insns A, B and C when
34 C has a link back to B and B has a link back to A.
36 LOG_LINKS does not have links for use of the CC0. They don't
37 need to, because the insn that sets the CC0 is always immediately
38 before the insn that tests it. So we always regard a branch
39 insn as having a logical link to the preceding insn. The same is true
40 for an insn explicitly using CC0.
42 We check (with use_crosses_set_p) to avoid combining in such a way
43 as to move a computation to a place where its value would be different.
45 Combination is done by mathematically substituting the previous
46 insn(s) values for the regs they set into the expressions in
47 the later insns that refer to these regs. If the result is a valid insn
48 for our target machine, according to the machine description,
49 we install it, delete the earlier insns, and update the data flow
50 information (LOG_LINKS and REG_NOTES) for what we did.
52 There are a few exceptions where the dataflow information created by
53 flow.c aren't completely updated:
55 - reg_live_length is not updated
56 - reg_n_refs is not adjusted in the rare case when a register is
57 no longer required in a computation
58 - there are extremely rare cases (see distribute_regnotes) when a
59 REG_DEAD note is lost
60 - a LOG_LINKS entry that refers to an insn with multiple SETs may be
61 removed because there is no way to know which register it was
62 linking
64 To simplify substitution, we combine only when the earlier insn(s)
65 consist of only a single assignment. To simplify updating afterward,
66 we never combine when a subroutine call appears in the middle.
68 Since we do not represent assignments to CC0 explicitly except when that
69 is all an insn does, there is no LOG_LINKS entry in an insn that uses
70 the condition code for the insn that set the condition code.
71 Fortunately, these two insns must be consecutive.
72 Therefore, every JUMP_INSN is taken to have an implicit logical link
73 to the preceding insn. This is not quite right, since non-jumps can
74 also use the condition code; but in practice such insns would not
75 combine anyway. */
77 #include "config.h"
78 #include "system.h"
79 #include "rtl.h"
80 #include "tm_p.h"
81 #include "flags.h"
82 #include "regs.h"
83 #include "hard-reg-set.h"
84 #include "basic-block.h"
85 #include "insn-config.h"
86 #include "function.h"
87 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
88 #include "expr.h"
89 #include "insn-flags.h"
90 #include "insn-codes.h"
91 #include "insn-attr.h"
92 #include "recog.h"
93 #include "real.h"
94 #include "toplev.h"
96 /* It is not safe to use ordinary gen_lowpart in combine.
97 Use gen_lowpart_for_combine instead. See comments there. */
98 #define gen_lowpart dont_use_gen_lowpart_you_dummy
100 /* Number of attempts to combine instructions in this function. */
102 static int combine_attempts;
104 /* Number of attempts that got as far as substitution in this function. */
106 static int combine_merges;
108 /* Number of instructions combined with added SETs in this function. */
110 static int combine_extras;
112 /* Number of instructions combined in this function. */
114 static int combine_successes;
116 /* Totals over entire compilation. */
118 static int total_attempts, total_merges, total_extras, total_successes;
120 /* Define a default value for REVERSIBLE_CC_MODE.
121 We can never assume that a condition code mode is safe to reverse unless
122 the md tells us so. */
123 #ifndef REVERSIBLE_CC_MODE
124 #define REVERSIBLE_CC_MODE(MODE) 0
125 #endif
127 /* Vector mapping INSN_UIDs to cuids.
128 The cuids are like uids but increase monotonically always.
129 Combine always uses cuids so that it can compare them.
130 But actually renumbering the uids, which we used to do,
131 proves to be a bad idea because it makes it hard to compare
132 the dumps produced by earlier passes with those from later passes. */
134 static int *uid_cuid;
135 static int max_uid_cuid;
137 /* Get the cuid of an insn. */
139 #define INSN_CUID(INSN) \
140 (INSN_UID (INSN) > max_uid_cuid ? insn_cuid (INSN) : uid_cuid[INSN_UID (INSN)])
142 /* Maximum register number, which is the size of the tables below. */
144 static int combine_max_regno;
146 /* Record last point of death of (hard or pseudo) register n. */
148 static rtx *reg_last_death;
150 /* Record last point of modification of (hard or pseudo) register n. */
152 static rtx *reg_last_set;
154 /* Record the cuid of the last insn that invalidated memory
155 (anything that writes memory, and subroutine calls, but not pushes). */
157 static int mem_last_set;
159 /* Record the cuid of the last CALL_INSN
160 so we can tell whether a potential combination crosses any calls. */
162 static int last_call_cuid;
164 /* When `subst' is called, this is the insn that is being modified
165 (by combining in a previous insn). The PATTERN of this insn
166 is still the old pattern partially modified and it should not be
167 looked at, but this may be used to examine the successors of the insn
168 to judge whether a simplification is valid. */
170 static rtx subst_insn;
172 /* This is an insn that belongs before subst_insn, but is not currently
173 on the insn chain. */
175 static rtx subst_prev_insn;
177 /* This is the lowest CUID that `subst' is currently dealing with.
178 get_last_value will not return a value if the register was set at or
179 after this CUID. If not for this mechanism, we could get confused if
180 I2 or I1 in try_combine were an insn that used the old value of a register
181 to obtain a new value. In that case, we might erroneously get the
182 new value of the register when we wanted the old one. */
184 static int subst_low_cuid;
186 /* This contains any hard registers that are used in newpat; reg_dead_at_p
187 must consider all these registers to be always live. */
189 static HARD_REG_SET newpat_used_regs;
191 /* This is an insn to which a LOG_LINKS entry has been added. If this
192 insn is the earlier than I2 or I3, combine should rescan starting at
193 that location. */
195 static rtx added_links_insn;
197 /* Basic block number of the block in which we are performing combines. */
198 static int this_basic_block;
200 /* A bitmap indicating which blocks had registers go dead at entry.
201 After combine, we'll need to re-do global life analysis with
202 those blocks as starting points. */
203 static sbitmap refresh_blocks;
204 static int need_refresh;
206 /* The next group of arrays allows the recording of the last value assigned
207 to (hard or pseudo) register n. We use this information to see if a
208 operation being processed is redundant given a prior operation performed
209 on the register. For example, an `and' with a constant is redundant if
210 all the zero bits are already known to be turned off.
212 We use an approach similar to that used by cse, but change it in the
213 following ways:
215 (1) We do not want to reinitialize at each label.
216 (2) It is useful, but not critical, to know the actual value assigned
217 to a register. Often just its form is helpful.
219 Therefore, we maintain the following arrays:
221 reg_last_set_value the last value assigned
222 reg_last_set_label records the value of label_tick when the
223 register was assigned
224 reg_last_set_table_tick records the value of label_tick when a
225 value using the register is assigned
226 reg_last_set_invalid set to non-zero when it is not valid
227 to use the value of this register in some
228 register's value
230 To understand the usage of these tables, it is important to understand
231 the distinction between the value in reg_last_set_value being valid
232 and the register being validly contained in some other expression in the
233 table.
235 Entry I in reg_last_set_value is valid if it is non-zero, and either
236 reg_n_sets[i] is 1 or reg_last_set_label[i] == label_tick.
238 Register I may validly appear in any expression returned for the value
239 of another register if reg_n_sets[i] is 1. It may also appear in the
240 value for register J if reg_last_set_label[i] < reg_last_set_label[j] or
241 reg_last_set_invalid[j] is zero.
243 If an expression is found in the table containing a register which may
244 not validly appear in an expression, the register is replaced by
245 something that won't match, (clobber (const_int 0)).
247 reg_last_set_invalid[i] is set non-zero when register I is being assigned
248 to and reg_last_set_table_tick[i] == label_tick. */
250 /* Record last value assigned to (hard or pseudo) register n. */
252 static rtx *reg_last_set_value;
254 /* Record the value of label_tick when the value for register n is placed in
255 reg_last_set_value[n]. */
257 static int *reg_last_set_label;
259 /* Record the value of label_tick when an expression involving register n
260 is placed in reg_last_set_value. */
262 static int *reg_last_set_table_tick;
264 /* Set non-zero if references to register n in expressions should not be
265 used. */
267 static char *reg_last_set_invalid;
269 /* Incremented for each label. */
271 static int label_tick;
273 /* Some registers that are set more than once and used in more than one
274 basic block are nevertheless always set in similar ways. For example,
275 a QImode register may be loaded from memory in two places on a machine
276 where byte loads zero extend.
278 We record in the following array what we know about the nonzero
279 bits of a register, specifically which bits are known to be zero.
281 If an entry is zero, it means that we don't know anything special. */
283 static unsigned HOST_WIDE_INT *reg_nonzero_bits;
285 /* Mode used to compute significance in reg_nonzero_bits. It is the largest
286 integer mode that can fit in HOST_BITS_PER_WIDE_INT. */
288 static enum machine_mode nonzero_bits_mode;
290 /* Nonzero if we know that a register has some leading bits that are always
291 equal to the sign bit. */
293 static char *reg_sign_bit_copies;
295 /* Nonzero when reg_nonzero_bits and reg_sign_bit_copies can be safely used.
296 It is zero while computing them and after combine has completed. This
297 former test prevents propagating values based on previously set values,
298 which can be incorrect if a variable is modified in a loop. */
300 static int nonzero_sign_valid;
302 /* These arrays are maintained in parallel with reg_last_set_value
303 and are used to store the mode in which the register was last set,
304 the bits that were known to be zero when it was last set, and the
305 number of sign bits copies it was known to have when it was last set. */
307 static enum machine_mode *reg_last_set_mode;
308 static unsigned HOST_WIDE_INT *reg_last_set_nonzero_bits;
309 static char *reg_last_set_sign_bit_copies;
311 /* Record one modification to rtl structure
312 to be undone by storing old_contents into *where.
313 is_int is 1 if the contents are an int. */
315 struct undo
317 struct undo *next;
318 int is_int;
319 union {rtx r; int i;} old_contents;
320 union {rtx *r; int *i;} where;
323 /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
324 num_undo says how many are currently recorded.
326 storage is nonzero if we must undo the allocation of new storage.
327 The value of storage is what to pass to obfree.
329 other_insn is nonzero if we have modified some other insn in the process
330 of working on subst_insn. It must be verified too.
332 previous_undos is the value of undobuf.undos when we started processing
333 this substitution. This will prevent gen_rtx_combine from re-used a piece
334 from the previous expression. Doing so can produce circular rtl
335 structures. */
337 struct undobuf
339 char *storage;
340 struct undo *undos;
341 struct undo *frees;
342 struct undo *previous_undos;
343 rtx other_insn;
346 static struct undobuf undobuf;
348 /* Number of times the pseudo being substituted for
349 was found and replaced. */
351 static int n_occurrences;
353 static void do_SUBST PROTO((rtx *, rtx));
354 static void do_SUBST_INT PROTO((int *, int));
355 static void init_reg_last_arrays PROTO((void));
356 static void setup_incoming_promotions PROTO((void));
357 static void set_nonzero_bits_and_sign_copies PROTO((rtx, rtx));
358 static int can_combine_p PROTO((rtx, rtx, rtx, rtx, rtx *, rtx *));
359 static int sets_function_arg_p PROTO((rtx));
360 static int combinable_i3pat PROTO((rtx, rtx *, rtx, rtx, int, rtx *));
361 static rtx try_combine PROTO((rtx, rtx, rtx));
362 static void undo_all PROTO((void));
363 static rtx *find_split_point PROTO((rtx *, rtx));
364 static rtx subst PROTO((rtx, rtx, rtx, int, int));
365 static rtx simplify_rtx PROTO((rtx, enum machine_mode, int, int));
366 static rtx simplify_if_then_else PROTO((rtx));
367 static rtx simplify_set PROTO((rtx));
368 static rtx simplify_logical PROTO((rtx, int));
369 static rtx expand_compound_operation PROTO((rtx));
370 static rtx expand_field_assignment PROTO((rtx));
371 static rtx make_extraction PROTO((enum machine_mode, rtx, int, rtx, int,
372 int, int, int));
373 static rtx extract_left_shift PROTO((rtx, int));
374 static rtx make_compound_operation PROTO((rtx, enum rtx_code));
375 static int get_pos_from_mask PROTO((unsigned HOST_WIDE_INT, int *));
376 static rtx force_to_mode PROTO((rtx, enum machine_mode,
377 unsigned HOST_WIDE_INT, rtx, int));
378 static rtx if_then_else_cond PROTO((rtx, rtx *, rtx *));
379 static rtx known_cond PROTO((rtx, enum rtx_code, rtx, rtx));
380 static int rtx_equal_for_field_assignment_p PROTO((rtx, rtx));
381 static rtx make_field_assignment PROTO((rtx));
382 static rtx apply_distributive_law PROTO((rtx));
383 static rtx simplify_and_const_int PROTO((rtx, enum machine_mode, rtx,
384 unsigned HOST_WIDE_INT));
385 static unsigned HOST_WIDE_INT nonzero_bits PROTO((rtx, enum machine_mode));
386 static int num_sign_bit_copies PROTO((rtx, enum machine_mode));
387 static int merge_outer_ops PROTO((enum rtx_code *, HOST_WIDE_INT *,
388 enum rtx_code, HOST_WIDE_INT,
389 enum machine_mode, int *));
390 static rtx simplify_shift_const PROTO((rtx, enum rtx_code, enum machine_mode,
391 rtx, int));
392 static int recog_for_combine PROTO((rtx *, rtx, rtx *));
393 static rtx gen_lowpart_for_combine PROTO((enum machine_mode, rtx));
394 static rtx gen_rtx_combine PVPROTO((enum rtx_code code, enum machine_mode mode,
395 ...));
396 static rtx gen_binary PROTO((enum rtx_code, enum machine_mode,
397 rtx, rtx));
398 static rtx gen_unary PROTO((enum rtx_code, enum machine_mode,
399 enum machine_mode, rtx));
400 static enum rtx_code simplify_comparison PROTO((enum rtx_code, rtx *, rtx *));
401 static int reversible_comparison_p PROTO((rtx));
402 static void update_table_tick PROTO((rtx));
403 static void record_value_for_reg PROTO((rtx, rtx, rtx));
404 static void record_dead_and_set_regs_1 PROTO((rtx, rtx));
405 static void record_dead_and_set_regs PROTO((rtx));
406 static int get_last_value_validate PROTO((rtx *, rtx, int, int));
407 static rtx get_last_value PROTO((rtx));
408 static int use_crosses_set_p PROTO((rtx, int));
409 static void reg_dead_at_p_1 PROTO((rtx, rtx));
410 static int reg_dead_at_p PROTO((rtx, rtx));
411 static void move_deaths PROTO((rtx, rtx, int, rtx, rtx *));
412 static int reg_bitfield_target_p PROTO((rtx, rtx));
413 static void distribute_notes PROTO((rtx, rtx, rtx, rtx, rtx, rtx));
414 static void distribute_links PROTO((rtx));
415 static void mark_used_regs_combine PROTO((rtx));
416 static int insn_cuid PROTO((rtx));
418 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
419 insn. The substitution can be undone by undo_all. If INTO is already
420 set to NEWVAL, do not record this change. Because computing NEWVAL might
421 also call SUBST, we have to compute it before we put anything into
422 the undo table. */
424 static void
425 do_SUBST(into, newval)
426 rtx *into, newval;
428 struct undo *buf;
429 rtx oldval = *into;
431 if (oldval == newval)
432 return;
434 if (undobuf.frees)
435 buf = undobuf.frees, undobuf.frees = buf->next;
436 else
437 buf = (struct undo *) xmalloc (sizeof (struct undo));
439 buf->is_int = 0;
440 buf->where.r = into;
441 buf->old_contents.r = oldval;
442 *into = newval;
444 buf->next = undobuf.undos, undobuf.undos = buf;
447 #define SUBST(INTO, NEWVAL) do_SUBST(&(INTO), (NEWVAL))
449 /* Similar to SUBST, but NEWVAL is an int expression. Note that substitution
450 for the value of a HOST_WIDE_INT value (including CONST_INT) is
451 not safe. */
453 static void
454 do_SUBST_INT(into, newval)
455 int *into, newval;
457 struct undo *buf;
458 int oldval = *into;
460 if (oldval == newval)
461 return;
463 if (undobuf.frees)
464 buf = undobuf.frees, undobuf.frees = buf->next;
465 else
466 buf = (struct undo *) xmalloc (sizeof (struct undo));
468 buf->is_int = 1;
469 buf->where.i = into;
470 buf->old_contents.i = oldval;
471 *into = newval;
473 buf->next = undobuf.undos, undobuf.undos = buf;
476 #define SUBST_INT(INTO, NEWVAL) do_SUBST_INT(&(INTO), (NEWVAL))
478 /* Main entry point for combiner. F is the first insn of the function.
479 NREGS is the first unused pseudo-reg number. */
481 void
482 combine_instructions (f, nregs)
483 rtx f;
484 int nregs;
486 register rtx insn, next;
487 #ifdef HAVE_cc0
488 register rtx prev;
489 #endif
490 register int i;
491 register rtx links, nextlinks;
493 combine_attempts = 0;
494 combine_merges = 0;
495 combine_extras = 0;
496 combine_successes = 0;
497 undobuf.undos = undobuf.previous_undos = 0;
499 combine_max_regno = nregs;
501 reg_nonzero_bits
502 = (unsigned HOST_WIDE_INT *) alloca (nregs * sizeof (HOST_WIDE_INT));
503 reg_sign_bit_copies = (char *) alloca (nregs * sizeof (char));
505 bzero ((char *) reg_nonzero_bits, nregs * sizeof (HOST_WIDE_INT));
506 bzero (reg_sign_bit_copies, nregs * sizeof (char));
508 reg_last_death = (rtx *) alloca (nregs * sizeof (rtx));
509 reg_last_set = (rtx *) alloca (nregs * sizeof (rtx));
510 reg_last_set_value = (rtx *) alloca (nregs * sizeof (rtx));
511 reg_last_set_table_tick = (int *) alloca (nregs * sizeof (int));
512 reg_last_set_label = (int *) alloca (nregs * sizeof (int));
513 reg_last_set_invalid = (char *) alloca (nregs * sizeof (char));
514 reg_last_set_mode
515 = (enum machine_mode *) alloca (nregs * sizeof (enum machine_mode));
516 reg_last_set_nonzero_bits
517 = (unsigned HOST_WIDE_INT *) alloca (nregs * sizeof (HOST_WIDE_INT));
518 reg_last_set_sign_bit_copies
519 = (char *) alloca (nregs * sizeof (char));
521 init_reg_last_arrays ();
523 init_recog_no_volatile ();
525 /* Compute maximum uid value so uid_cuid can be allocated. */
527 for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
528 if (INSN_UID (insn) > i)
529 i = INSN_UID (insn);
531 uid_cuid = (int *) alloca ((i + 1) * sizeof (int));
532 max_uid_cuid = i;
534 nonzero_bits_mode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
536 /* Don't use reg_nonzero_bits when computing it. This can cause problems
537 when, for example, we have j <<= 1 in a loop. */
539 nonzero_sign_valid = 0;
541 /* Compute the mapping from uids to cuids.
542 Cuids are numbers assigned to insns, like uids,
543 except that cuids increase monotonically through the code.
545 Scan all SETs and see if we can deduce anything about what
546 bits are known to be zero for some registers and how many copies
547 of the sign bit are known to exist for those registers.
549 Also set any known values so that we can use it while searching
550 for what bits are known to be set. */
552 label_tick = 1;
554 /* We need to initialize it here, because record_dead_and_set_regs may call
555 get_last_value. */
556 subst_prev_insn = NULL_RTX;
558 setup_incoming_promotions ();
560 refresh_blocks = sbitmap_alloc (n_basic_blocks);
561 sbitmap_zero (refresh_blocks);
562 need_refresh = 0;
564 for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
566 uid_cuid[INSN_UID (insn)] = ++i;
567 subst_low_cuid = i;
568 subst_insn = insn;
570 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
572 note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies);
573 record_dead_and_set_regs (insn);
575 #ifdef AUTO_INC_DEC
576 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
577 if (REG_NOTE_KIND (links) == REG_INC)
578 set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX);
579 #endif
582 if (GET_CODE (insn) == CODE_LABEL)
583 label_tick++;
586 nonzero_sign_valid = 1;
588 /* Now scan all the insns in forward order. */
590 this_basic_block = -1;
591 label_tick = 1;
592 last_call_cuid = 0;
593 mem_last_set = 0;
594 init_reg_last_arrays ();
595 setup_incoming_promotions ();
597 for (insn = f; insn; insn = next ? next : NEXT_INSN (insn))
599 next = 0;
601 /* If INSN starts a new basic block, update our basic block number. */
602 if (this_basic_block + 1 < n_basic_blocks
603 && BLOCK_HEAD (this_basic_block + 1) == insn)
604 this_basic_block++;
606 if (GET_CODE (insn) == CODE_LABEL)
607 label_tick++;
609 else if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
611 /* Try this insn with each insn it links back to. */
613 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
614 if ((next = try_combine (insn, XEXP (links, 0), NULL_RTX)) != 0)
615 goto retry;
617 /* Try each sequence of three linked insns ending with this one. */
619 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
620 for (nextlinks = LOG_LINKS (XEXP (links, 0)); nextlinks;
621 nextlinks = XEXP (nextlinks, 1))
622 if ((next = try_combine (insn, XEXP (links, 0),
623 XEXP (nextlinks, 0))) != 0)
624 goto retry;
626 #ifdef HAVE_cc0
627 /* Try to combine a jump insn that uses CC0
628 with a preceding insn that sets CC0, and maybe with its
629 logical predecessor as well.
630 This is how we make decrement-and-branch insns.
631 We need this special code because data flow connections
632 via CC0 do not get entered in LOG_LINKS. */
634 if (GET_CODE (insn) == JUMP_INSN
635 && (prev = prev_nonnote_insn (insn)) != 0
636 && GET_CODE (prev) == INSN
637 && sets_cc0_p (PATTERN (prev)))
639 if ((next = try_combine (insn, prev, NULL_RTX)) != 0)
640 goto retry;
642 for (nextlinks = LOG_LINKS (prev); nextlinks;
643 nextlinks = XEXP (nextlinks, 1))
644 if ((next = try_combine (insn, prev,
645 XEXP (nextlinks, 0))) != 0)
646 goto retry;
649 /* Do the same for an insn that explicitly references CC0. */
650 if (GET_CODE (insn) == INSN
651 && (prev = prev_nonnote_insn (insn)) != 0
652 && GET_CODE (prev) == INSN
653 && sets_cc0_p (PATTERN (prev))
654 && GET_CODE (PATTERN (insn)) == SET
655 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
657 if ((next = try_combine (insn, prev, NULL_RTX)) != 0)
658 goto retry;
660 for (nextlinks = LOG_LINKS (prev); nextlinks;
661 nextlinks = XEXP (nextlinks, 1))
662 if ((next = try_combine (insn, prev,
663 XEXP (nextlinks, 0))) != 0)
664 goto retry;
667 /* Finally, see if any of the insns that this insn links to
668 explicitly references CC0. If so, try this insn, that insn,
669 and its predecessor if it sets CC0. */
670 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
671 if (GET_CODE (XEXP (links, 0)) == INSN
672 && GET_CODE (PATTERN (XEXP (links, 0))) == SET
673 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (XEXP (links, 0))))
674 && (prev = prev_nonnote_insn (XEXP (links, 0))) != 0
675 && GET_CODE (prev) == INSN
676 && sets_cc0_p (PATTERN (prev))
677 && (next = try_combine (insn, XEXP (links, 0), prev)) != 0)
678 goto retry;
679 #endif
681 /* Try combining an insn with two different insns whose results it
682 uses. */
683 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
684 for (nextlinks = XEXP (links, 1); nextlinks;
685 nextlinks = XEXP (nextlinks, 1))
686 if ((next = try_combine (insn, XEXP (links, 0),
687 XEXP (nextlinks, 0))) != 0)
688 goto retry;
690 if (GET_CODE (insn) != NOTE)
691 record_dead_and_set_regs (insn);
693 retry:
698 if (need_refresh)
700 compute_bb_for_insn (get_max_uid ());
701 update_life_info (refresh_blocks, UPDATE_LIFE_GLOBAL_RM_NOTES,
702 PROP_DEATH_NOTES);
704 sbitmap_free (refresh_blocks);
706 total_attempts += combine_attempts;
707 total_merges += combine_merges;
708 total_extras += combine_extras;
709 total_successes += combine_successes;
711 nonzero_sign_valid = 0;
713 /* Make recognizer allow volatile MEMs again. */
714 init_recog ();
717 /* Wipe the reg_last_xxx arrays in preparation for another pass. */
719 static void
720 init_reg_last_arrays ()
722 int nregs = combine_max_regno;
724 bzero ((char *) reg_last_death, nregs * sizeof (rtx));
725 bzero ((char *) reg_last_set, nregs * sizeof (rtx));
726 bzero ((char *) reg_last_set_value, nregs * sizeof (rtx));
727 bzero ((char *) reg_last_set_table_tick, nregs * sizeof (int));
728 bzero ((char *) reg_last_set_label, nregs * sizeof (int));
729 bzero (reg_last_set_invalid, nregs * sizeof (char));
730 bzero ((char *) reg_last_set_mode, nregs * sizeof (enum machine_mode));
731 bzero ((char *) reg_last_set_nonzero_bits, nregs * sizeof (HOST_WIDE_INT));
732 bzero (reg_last_set_sign_bit_copies, nregs * sizeof (char));
735 /* Set up any promoted values for incoming argument registers. */
737 static void
738 setup_incoming_promotions ()
740 #ifdef PROMOTE_FUNCTION_ARGS
741 int regno;
742 rtx reg;
743 enum machine_mode mode;
744 int unsignedp;
745 rtx first = get_insns ();
747 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
748 if (FUNCTION_ARG_REGNO_P (regno)
749 && (reg = promoted_input_arg (regno, &mode, &unsignedp)) != 0)
751 record_value_for_reg
752 (reg, first, gen_rtx_fmt_e ((unsignedp ? ZERO_EXTEND
753 : SIGN_EXTEND),
754 GET_MODE (reg),
755 gen_rtx_CLOBBER (mode, const0_rtx)));
757 #endif
760 /* Called via note_stores. If X is a pseudo that is narrower than
761 HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
763 If we are setting only a portion of X and we can't figure out what
764 portion, assume all bits will be used since we don't know what will
765 be happening.
767 Similarly, set how many bits of X are known to be copies of the sign bit
768 at all locations in the function. This is the smallest number implied
769 by any set of X. */
771 static void
772 set_nonzero_bits_and_sign_copies (x, set)
773 rtx x;
774 rtx set;
776 int num;
778 if (GET_CODE (x) == REG
779 && REGNO (x) >= FIRST_PSEUDO_REGISTER
780 /* If this register is undefined at the start of the file, we can't
781 say what its contents were. */
782 && ! REGNO_REG_SET_P (BASIC_BLOCK (0)->global_live_at_start, REGNO (x))
783 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
785 if (set == 0 || GET_CODE (set) == CLOBBER)
787 reg_nonzero_bits[REGNO (x)] = GET_MODE_MASK (GET_MODE (x));
788 reg_sign_bit_copies[REGNO (x)] = 1;
789 return;
792 /* If this is a complex assignment, see if we can convert it into a
793 simple assignment. */
794 set = expand_field_assignment (set);
796 /* If this is a simple assignment, or we have a paradoxical SUBREG,
797 set what we know about X. */
799 if (SET_DEST (set) == x
800 || (GET_CODE (SET_DEST (set)) == SUBREG
801 && (GET_MODE_SIZE (GET_MODE (SET_DEST (set)))
802 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (set)))))
803 && SUBREG_REG (SET_DEST (set)) == x))
805 rtx src = SET_SRC (set);
807 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
808 /* If X is narrower than a word and SRC is a non-negative
809 constant that would appear negative in the mode of X,
810 sign-extend it for use in reg_nonzero_bits because some
811 machines (maybe most) will actually do the sign-extension
812 and this is the conservative approach.
814 ??? For 2.5, try to tighten up the MD files in this regard
815 instead of this kludge. */
817 if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
818 && GET_CODE (src) == CONST_INT
819 && INTVAL (src) > 0
820 && 0 != (INTVAL (src)
821 & ((HOST_WIDE_INT) 1
822 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
823 src = GEN_INT (INTVAL (src)
824 | ((HOST_WIDE_INT) (-1)
825 << GET_MODE_BITSIZE (GET_MODE (x))));
826 #endif
828 reg_nonzero_bits[REGNO (x)]
829 |= nonzero_bits (src, nonzero_bits_mode);
830 num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
831 if (reg_sign_bit_copies[REGNO (x)] == 0
832 || reg_sign_bit_copies[REGNO (x)] > num)
833 reg_sign_bit_copies[REGNO (x)] = num;
835 else
837 reg_nonzero_bits[REGNO (x)] = GET_MODE_MASK (GET_MODE (x));
838 reg_sign_bit_copies[REGNO (x)] = 1;
843 /* See if INSN can be combined into I3. PRED and SUCC are optionally
844 insns that were previously combined into I3 or that will be combined
845 into the merger of INSN and I3.
847 Return 0 if the combination is not allowed for any reason.
849 If the combination is allowed, *PDEST will be set to the single
850 destination of INSN and *PSRC to the single source, and this function
851 will return 1. */
853 static int
854 can_combine_p (insn, i3, pred, succ, pdest, psrc)
855 rtx insn;
856 rtx i3;
857 rtx pred ATTRIBUTE_UNUSED;
858 rtx succ;
859 rtx *pdest, *psrc;
861 int i;
862 rtx set = 0, src, dest;
863 rtx p;
864 #ifdef AUTO_INC_DEC
865 rtx link;
866 #endif
867 int all_adjacent = (succ ? (next_active_insn (insn) == succ
868 && next_active_insn (succ) == i3)
869 : next_active_insn (insn) == i3);
871 /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
872 or a PARALLEL consisting of such a SET and CLOBBERs.
874 If INSN has CLOBBER parallel parts, ignore them for our processing.
875 By definition, these happen during the execution of the insn. When it
876 is merged with another insn, all bets are off. If they are, in fact,
877 needed and aren't also supplied in I3, they may be added by
878 recog_for_combine. Otherwise, it won't match.
880 We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
881 note.
883 Get the source and destination of INSN. If more than one, can't
884 combine. */
886 if (GET_CODE (PATTERN (insn)) == SET)
887 set = PATTERN (insn);
888 else if (GET_CODE (PATTERN (insn)) == PARALLEL
889 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
891 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
893 rtx elt = XVECEXP (PATTERN (insn), 0, i);
895 switch (GET_CODE (elt))
897 /* This is important to combine floating point insns
898 for the SH4 port. */
899 case USE:
900 /* Combining an isolated USE doesn't make sense.
901 We depend here on combinable_i3_pat to reject them. */
902 /* The code below this loop only verifies that the inputs of
903 the SET in INSN do not change. We call reg_set_between_p
904 to verify that the REG in the USE does not change betweeen
905 I3 and INSN.
906 If the USE in INSN was for a pseudo register, the matching
907 insn pattern will likely match any register; combining this
908 with any other USE would only be safe if we knew that the
909 used registers have identical values, or if there was
910 something to tell them apart, e.g. different modes. For
911 now, we forgo such compilcated tests and simply disallow
912 combining of USES of pseudo registers with any other USE. */
913 if (GET_CODE (XEXP (elt, 0)) == REG
914 && GET_CODE (PATTERN (i3)) == PARALLEL)
916 rtx i3pat = PATTERN (i3);
917 int i = XVECLEN (i3pat, 0) - 1;
918 int regno = REGNO (XEXP (elt, 0));
921 rtx i3elt = XVECEXP (i3pat, 0, i);
922 if (GET_CODE (i3elt) == USE
923 && GET_CODE (XEXP (i3elt, 0)) == REG
924 && (REGNO (XEXP (i3elt, 0)) == regno
925 ? reg_set_between_p (XEXP (elt, 0),
926 PREV_INSN (insn), i3)
927 : regno >= FIRST_PSEUDO_REGISTER))
928 return 0;
930 while (--i >= 0);
932 break;
934 /* We can ignore CLOBBERs. */
935 case CLOBBER:
936 break;
938 case SET:
939 /* Ignore SETs whose result isn't used but not those that
940 have side-effects. */
941 if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
942 && ! side_effects_p (elt))
943 break;
945 /* If we have already found a SET, this is a second one and
946 so we cannot combine with this insn. */
947 if (set)
948 return 0;
950 set = elt;
951 break;
953 default:
954 /* Anything else means we can't combine. */
955 return 0;
959 if (set == 0
960 /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
961 so don't do anything with it. */
962 || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
963 return 0;
965 else
966 return 0;
968 if (set == 0)
969 return 0;
971 set = expand_field_assignment (set);
972 src = SET_SRC (set), dest = SET_DEST (set);
974 /* Don't eliminate a store in the stack pointer. */
975 if (dest == stack_pointer_rtx
976 /* If we couldn't eliminate a field assignment, we can't combine. */
977 || GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == STRICT_LOW_PART
978 /* Don't combine with an insn that sets a register to itself if it has
979 a REG_EQUAL note. This may be part of a REG_NO_CONFLICT sequence. */
980 || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
981 /* Can't merge a function call. */
982 || GET_CODE (src) == CALL
983 /* Don't eliminate a function call argument. */
984 || (GET_CODE (i3) == CALL_INSN
985 && (find_reg_fusage (i3, USE, dest)
986 || (GET_CODE (dest) == REG
987 && REGNO (dest) < FIRST_PSEUDO_REGISTER
988 && global_regs[REGNO (dest)])))
989 /* Don't substitute into an incremented register. */
990 || FIND_REG_INC_NOTE (i3, dest)
991 || (succ && FIND_REG_INC_NOTE (succ, dest))
992 #if 0
993 /* Don't combine the end of a libcall into anything. */
994 /* ??? This gives worse code, and appears to be unnecessary, since no
995 pass after flow uses REG_LIBCALL/REG_RETVAL notes. Local-alloc does
996 use REG_RETVAL notes for noconflict blocks, but other code here
997 makes sure that those insns don't disappear. */
998 || find_reg_note (insn, REG_RETVAL, NULL_RTX)
999 #endif
1000 /* Make sure that DEST is not used after SUCC but before I3. */
1001 || (succ && ! all_adjacent
1002 && reg_used_between_p (dest, succ, i3))
1003 /* Make sure that the value that is to be substituted for the register
1004 does not use any registers whose values alter in between. However,
1005 If the insns are adjacent, a use can't cross a set even though we
1006 think it might (this can happen for a sequence of insns each setting
1007 the same destination; reg_last_set of that register might point to
1008 a NOTE). If INSN has a REG_EQUIV note, the register is always
1009 equivalent to the memory so the substitution is valid even if there
1010 are intervening stores. Also, don't move a volatile asm or
1011 UNSPEC_VOLATILE across any other insns. */
1012 || (! all_adjacent
1013 && (((GET_CODE (src) != MEM
1014 || ! find_reg_note (insn, REG_EQUIV, src))
1015 && use_crosses_set_p (src, INSN_CUID (insn)))
1016 || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
1017 || GET_CODE (src) == UNSPEC_VOLATILE))
1018 /* If there is a REG_NO_CONFLICT note for DEST in I3 or SUCC, we get
1019 better register allocation by not doing the combine. */
1020 || find_reg_note (i3, REG_NO_CONFLICT, dest)
1021 || (succ && find_reg_note (succ, REG_NO_CONFLICT, dest))
1022 /* Don't combine across a CALL_INSN, because that would possibly
1023 change whether the life span of some REGs crosses calls or not,
1024 and it is a pain to update that information.
1025 Exception: if source is a constant, moving it later can't hurt.
1026 Accept that special case, because it helps -fforce-addr a lot. */
1027 || (INSN_CUID (insn) < last_call_cuid && ! CONSTANT_P (src)))
1028 return 0;
1030 /* DEST must either be a REG or CC0. */
1031 if (GET_CODE (dest) == REG)
1033 /* If register alignment is being enforced for multi-word items in all
1034 cases except for parameters, it is possible to have a register copy
1035 insn referencing a hard register that is not allowed to contain the
1036 mode being copied and which would not be valid as an operand of most
1037 insns. Eliminate this problem by not combining with such an insn.
1039 Also, on some machines we don't want to extend the life of a hard
1040 register.
1042 This is the same test done in can_combine except that we don't test
1043 if SRC is a CALL operation to permit a hard register with
1044 SMALL_REGISTER_CLASSES, and that we have to take all_adjacent
1045 into account. */
1047 if (GET_CODE (src) == REG
1048 && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
1049 && ! HARD_REGNO_MODE_OK (REGNO (dest), GET_MODE (dest)))
1050 /* Don't extend the life of a hard register unless it is
1051 user variable (if we have few registers) or it can't
1052 fit into the desired register (meaning something special
1053 is going on).
1054 Also avoid substituting a return register into I3, because
1055 reload can't handle a conflict with constraints of other
1056 inputs. */
1057 || (REGNO (src) < FIRST_PSEUDO_REGISTER
1058 && (! HARD_REGNO_MODE_OK (REGNO (src), GET_MODE (src))
1059 || (SMALL_REGISTER_CLASSES
1060 && ((! all_adjacent && ! REG_USERVAR_P (src))
1061 || (FUNCTION_VALUE_REGNO_P (REGNO (src))
1062 && ! REG_USERVAR_P (src))))))))
1063 return 0;
1065 else if (GET_CODE (dest) != CC0)
1066 return 0;
1068 /* Don't substitute for a register intended as a clobberable operand.
1069 Similarly, don't substitute an expression containing a register that
1070 will be clobbered in I3. */
1071 if (GET_CODE (PATTERN (i3)) == PARALLEL)
1072 for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
1073 if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER
1074 && (reg_overlap_mentioned_p (XEXP (XVECEXP (PATTERN (i3), 0, i), 0),
1075 src)
1076 || rtx_equal_p (XEXP (XVECEXP (PATTERN (i3), 0, i), 0), dest)))
1077 return 0;
1079 /* If INSN contains anything volatile, or is an `asm' (whether volatile
1080 or not), reject, unless nothing volatile comes between it and I3 */
1082 if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
1084 /* Make sure succ doesn't contain a volatile reference. */
1085 if (succ != 0 && volatile_refs_p (PATTERN (succ)))
1086 return 0;
1088 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1089 if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
1090 && p != succ && volatile_refs_p (PATTERN (p)))
1091 return 0;
1094 /* If INSN is an asm, and DEST is a hard register, reject, since it has
1095 to be an explicit register variable, and was chosen for a reason. */
1097 if (GET_CODE (src) == ASM_OPERANDS
1098 && GET_CODE (dest) == REG && REGNO (dest) < FIRST_PSEUDO_REGISTER)
1099 return 0;
1101 /* If there are any volatile insns between INSN and I3, reject, because
1102 they might affect machine state. */
1104 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1105 if (GET_RTX_CLASS (GET_CODE (p)) == 'i'
1106 && p != succ && volatile_insn_p (PATTERN (p)))
1107 return 0;
1109 /* If INSN or I2 contains an autoincrement or autodecrement,
1110 make sure that register is not used between there and I3,
1111 and not already used in I3 either.
1112 Also insist that I3 not be a jump; if it were one
1113 and the incremented register were spilled, we would lose. */
1115 #ifdef AUTO_INC_DEC
1116 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1117 if (REG_NOTE_KIND (link) == REG_INC
1118 && (GET_CODE (i3) == JUMP_INSN
1119 || reg_used_between_p (XEXP (link, 0), insn, i3)
1120 || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
1121 return 0;
1122 #endif
1124 #ifdef HAVE_cc0
1125 /* Don't combine an insn that follows a CC0-setting insn.
1126 An insn that uses CC0 must not be separated from the one that sets it.
1127 We do, however, allow I2 to follow a CC0-setting insn if that insn
1128 is passed as I1; in that case it will be deleted also.
1129 We also allow combining in this case if all the insns are adjacent
1130 because that would leave the two CC0 insns adjacent as well.
1131 It would be more logical to test whether CC0 occurs inside I1 or I2,
1132 but that would be much slower, and this ought to be equivalent. */
1134 p = prev_nonnote_insn (insn);
1135 if (p && p != pred && GET_CODE (p) == INSN && sets_cc0_p (PATTERN (p))
1136 && ! all_adjacent)
1137 return 0;
1138 #endif
1140 /* If we get here, we have passed all the tests and the combination is
1141 to be allowed. */
1143 *pdest = dest;
1144 *psrc = src;
1146 return 1;
1149 /* Check if PAT is an insn - or a part of it - used to set up an
1150 argument for a function in a hard register. */
1152 static int
1153 sets_function_arg_p (pat)
1154 rtx pat;
1156 int i;
1157 rtx inner_dest;
1159 switch (GET_CODE (pat))
1161 case INSN:
1162 return sets_function_arg_p (PATTERN (pat));
1164 case PARALLEL:
1165 for (i = XVECLEN (pat, 0); --i >= 0;)
1166 if (sets_function_arg_p (XVECEXP (pat, 0, i)))
1167 return 1;
1169 break;
1171 case SET:
1172 inner_dest = SET_DEST (pat);
1173 while (GET_CODE (inner_dest) == STRICT_LOW_PART
1174 || GET_CODE (inner_dest) == SUBREG
1175 || GET_CODE (inner_dest) == ZERO_EXTRACT)
1176 inner_dest = XEXP (inner_dest, 0);
1178 return (GET_CODE (inner_dest) == REG
1179 && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
1180 && FUNCTION_ARG_REGNO_P (REGNO (inner_dest)));
1182 default:
1183 break;
1186 return 0;
1189 /* LOC is the location within I3 that contains its pattern or the component
1190 of a PARALLEL of the pattern. We validate that it is valid for combining.
1192 One problem is if I3 modifies its output, as opposed to replacing it
1193 entirely, we can't allow the output to contain I2DEST or I1DEST as doing
1194 so would produce an insn that is not equivalent to the original insns.
1196 Consider:
1198 (set (reg:DI 101) (reg:DI 100))
1199 (set (subreg:SI (reg:DI 101) 0) <foo>)
1201 This is NOT equivalent to:
1203 (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
1204 (set (reg:DI 101) (reg:DI 100))])
1206 Not only does this modify 100 (in which case it might still be valid
1207 if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
1209 We can also run into a problem if I2 sets a register that I1
1210 uses and I1 gets directly substituted into I3 (not via I2). In that
1211 case, we would be getting the wrong value of I2DEST into I3, so we
1212 must reject the combination. This case occurs when I2 and I1 both
1213 feed into I3, rather than when I1 feeds into I2, which feeds into I3.
1214 If I1_NOT_IN_SRC is non-zero, it means that finding I1 in the source
1215 of a SET must prevent combination from occurring.
1217 On machines where SMALL_REGISTER_CLASSES is non-zero, we don't combine
1218 if the destination of a SET is a hard register that isn't a user
1219 variable.
1221 Before doing the above check, we first try to expand a field assignment
1222 into a set of logical operations.
1224 If PI3_DEST_KILLED is non-zero, it is a pointer to a location in which
1225 we place a register that is both set and used within I3. If more than one
1226 such register is detected, we fail.
1228 Return 1 if the combination is valid, zero otherwise. */
1230 static int
1231 combinable_i3pat (i3, loc, i2dest, i1dest, i1_not_in_src, pi3dest_killed)
1232 rtx i3;
1233 rtx *loc;
1234 rtx i2dest;
1235 rtx i1dest;
1236 int i1_not_in_src;
1237 rtx *pi3dest_killed;
1239 rtx x = *loc;
1241 if (GET_CODE (x) == SET)
1243 rtx set = expand_field_assignment (x);
1244 rtx dest = SET_DEST (set);
1245 rtx src = SET_SRC (set);
1246 rtx inner_dest = dest;
1248 #if 0
1249 rtx inner_src = src;
1250 #endif
1252 SUBST (*loc, set);
1254 while (GET_CODE (inner_dest) == STRICT_LOW_PART
1255 || GET_CODE (inner_dest) == SUBREG
1256 || GET_CODE (inner_dest) == ZERO_EXTRACT)
1257 inner_dest = XEXP (inner_dest, 0);
1259 /* We probably don't need this any more now that LIMIT_RELOAD_CLASS
1260 was added. */
1261 #if 0
1262 while (GET_CODE (inner_src) == STRICT_LOW_PART
1263 || GET_CODE (inner_src) == SUBREG
1264 || GET_CODE (inner_src) == ZERO_EXTRACT)
1265 inner_src = XEXP (inner_src, 0);
1267 /* If it is better that two different modes keep two different pseudos,
1268 avoid combining them. This avoids producing the following pattern
1269 on a 386:
1270 (set (subreg:SI (reg/v:QI 21) 0)
1271 (lshiftrt:SI (reg/v:SI 20)
1272 (const_int 24)))
1273 If that were made, reload could not handle the pair of
1274 reg 20/21, since it would try to get any GENERAL_REGS
1275 but some of them don't handle QImode. */
1277 if (rtx_equal_p (inner_src, i2dest)
1278 && GET_CODE (inner_dest) == REG
1279 && ! MODES_TIEABLE_P (GET_MODE (i2dest), GET_MODE (inner_dest)))
1280 return 0;
1281 #endif
1283 /* Check for the case where I3 modifies its output, as
1284 discussed above. */
1285 if ((inner_dest != dest
1286 && (reg_overlap_mentioned_p (i2dest, inner_dest)
1287 || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))))
1289 /* This is the same test done in can_combine_p except that we
1290 allow a hard register with SMALL_REGISTER_CLASSES if SRC is a
1291 CALL operation. Moreover, we can't test all_adjacent; we don't
1292 have to, since this instruction will stay in place, thus we are
1293 not considering increasing the lifetime of INNER_DEST.
1295 Also, if this insn sets a function argument, combining it with
1296 something that might need a spill could clobber a previous
1297 function argument; the all_adjacent test in can_combine_p also
1298 checks this; here, we do a more specific test for this case. */
1300 || (GET_CODE (inner_dest) == REG
1301 && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
1302 && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
1303 GET_MODE (inner_dest))
1304 || (SMALL_REGISTER_CLASSES && GET_CODE (src) != CALL
1305 && ! REG_USERVAR_P (inner_dest)
1306 && (FUNCTION_VALUE_REGNO_P (REGNO (inner_dest))
1307 || (FUNCTION_ARG_REGNO_P (REGNO (inner_dest))
1308 && i3 != 0
1309 && sets_function_arg_p (prev_nonnote_insn (i3)))))))
1310 || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src)))
1311 return 0;
1313 /* If DEST is used in I3, it is being killed in this insn,
1314 so record that for later.
1315 Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
1316 STACK_POINTER_REGNUM, since these are always considered to be
1317 live. Similarly for ARG_POINTER_REGNUM if it is fixed. */
1318 if (pi3dest_killed && GET_CODE (dest) == REG
1319 && reg_referenced_p (dest, PATTERN (i3))
1320 && REGNO (dest) != FRAME_POINTER_REGNUM
1321 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
1322 && REGNO (dest) != HARD_FRAME_POINTER_REGNUM
1323 #endif
1324 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
1325 && (REGNO (dest) != ARG_POINTER_REGNUM
1326 || ! fixed_regs [REGNO (dest)])
1327 #endif
1328 && REGNO (dest) != STACK_POINTER_REGNUM)
1330 if (*pi3dest_killed)
1331 return 0;
1333 *pi3dest_killed = dest;
1337 else if (GET_CODE (x) == PARALLEL)
1339 int i;
1341 for (i = 0; i < XVECLEN (x, 0); i++)
1342 if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest,
1343 i1_not_in_src, pi3dest_killed))
1344 return 0;
1347 return 1;
1350 /* Try to combine the insns I1 and I2 into I3.
1351 Here I1 and I2 appear earlier than I3.
1352 I1 can be zero; then we combine just I2 into I3.
1354 It we are combining three insns and the resulting insn is not recognized,
1355 try splitting it into two insns. If that happens, I2 and I3 are retained
1356 and I1 is pseudo-deleted by turning it into a NOTE. Otherwise, I1 and I2
1357 are pseudo-deleted.
1359 Return 0 if the combination does not work. Then nothing is changed.
1360 If we did the combination, return the insn at which combine should
1361 resume scanning. */
1363 static rtx
1364 try_combine (i3, i2, i1)
1365 register rtx i3, i2, i1;
1367 /* New patterns for I3 and I3, respectively. */
1368 rtx newpat, newi2pat = 0;
1369 /* Indicates need to preserve SET in I1 or I2 in I3 if it is not dead. */
1370 int added_sets_1, added_sets_2;
1371 /* Total number of SETs to put into I3. */
1372 int total_sets;
1373 /* Nonzero is I2's body now appears in I3. */
1374 int i2_is_used;
1375 /* INSN_CODEs for new I3, new I2, and user of condition code. */
1376 int insn_code_number, i2_code_number = 0, other_code_number = 0;
1377 /* Contains I3 if the destination of I3 is used in its source, which means
1378 that the old life of I3 is being killed. If that usage is placed into
1379 I2 and not in I3, a REG_DEAD note must be made. */
1380 rtx i3dest_killed = 0;
1381 /* SET_DEST and SET_SRC of I2 and I1. */
1382 rtx i2dest, i2src, i1dest = 0, i1src = 0;
1383 /* PATTERN (I2), or a copy of it in certain cases. */
1384 rtx i2pat;
1385 /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC. */
1386 int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
1387 int i1_feeds_i3 = 0;
1388 /* Notes that must be added to REG_NOTES in I3 and I2. */
1389 rtx new_i3_notes, new_i2_notes;
1390 /* Notes that we substituted I3 into I2 instead of the normal case. */
1391 int i3_subst_into_i2 = 0;
1392 /* Notes that I1, I2 or I3 is a MULT operation. */
1393 int have_mult = 0;
1395 int maxreg;
1396 rtx temp;
1397 register rtx link;
1398 int i;
1400 /* If any of I1, I2, and I3 isn't really an insn, we can't do anything.
1401 This can occur when flow deletes an insn that it has merged into an
1402 auto-increment address. We also can't do anything if I3 has a
1403 REG_LIBCALL note since we don't want to disrupt the contiguity of a
1404 libcall. */
1406 if (GET_RTX_CLASS (GET_CODE (i3)) != 'i'
1407 || GET_RTX_CLASS (GET_CODE (i2)) != 'i'
1408 || (i1 && GET_RTX_CLASS (GET_CODE (i1)) != 'i')
1409 #if 0
1410 /* ??? This gives worse code, and appears to be unnecessary, since no
1411 pass after flow uses REG_LIBCALL/REG_RETVAL notes. */
1412 || find_reg_note (i3, REG_LIBCALL, NULL_RTX)
1413 #endif
1415 return 0;
1417 combine_attempts++;
1419 undobuf.undos = undobuf.previous_undos = 0;
1420 undobuf.other_insn = 0;
1422 /* Save the current high-water-mark so we can free storage if we didn't
1423 accept this combination. */
1424 undobuf.storage = (char *) oballoc (0);
1426 /* Reset the hard register usage information. */
1427 CLEAR_HARD_REG_SET (newpat_used_regs);
1429 /* If I1 and I2 both feed I3, they can be in any order. To simplify the
1430 code below, set I1 to be the earlier of the two insns. */
1431 if (i1 && INSN_CUID (i1) > INSN_CUID (i2))
1432 temp = i1, i1 = i2, i2 = temp;
1434 added_links_insn = 0;
1436 /* First check for one important special-case that the code below will
1437 not handle. Namely, the case where I1 is zero, I2 has multiple sets,
1438 and I3 is a SET whose SET_SRC is a SET_DEST in I2. In that case,
1439 we may be able to replace that destination with the destination of I3.
1440 This occurs in the common code where we compute both a quotient and
1441 remainder into a structure, in which case we want to do the computation
1442 directly into the structure to avoid register-register copies.
1444 We make very conservative checks below and only try to handle the
1445 most common cases of this. For example, we only handle the case
1446 where I2 and I3 are adjacent to avoid making difficult register
1447 usage tests. */
1449 if (i1 == 0 && GET_CODE (i3) == INSN && GET_CODE (PATTERN (i3)) == SET
1450 && GET_CODE (SET_SRC (PATTERN (i3))) == REG
1451 && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
1452 && (! SMALL_REGISTER_CLASSES
1453 || (GET_CODE (SET_DEST (PATTERN (i3))) != REG
1454 || REGNO (SET_DEST (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
1455 || REG_USERVAR_P (SET_DEST (PATTERN (i3)))))
1456 && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
1457 && GET_CODE (PATTERN (i2)) == PARALLEL
1458 && ! side_effects_p (SET_DEST (PATTERN (i3)))
1459 /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
1460 below would need to check what is inside (and reg_overlap_mentioned_p
1461 doesn't support those codes anyway). Don't allow those destinations;
1462 the resulting insn isn't likely to be recognized anyway. */
1463 && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
1464 && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
1465 && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
1466 SET_DEST (PATTERN (i3)))
1467 && next_real_insn (i2) == i3)
1469 rtx p2 = PATTERN (i2);
1471 /* Make sure that the destination of I3,
1472 which we are going to substitute into one output of I2,
1473 is not used within another output of I2. We must avoid making this:
1474 (parallel [(set (mem (reg 69)) ...)
1475 (set (reg 69) ...)])
1476 which is not well-defined as to order of actions.
1477 (Besides, reload can't handle output reloads for this.)
1479 The problem can also happen if the dest of I3 is a memory ref,
1480 if another dest in I2 is an indirect memory ref. */
1481 for (i = 0; i < XVECLEN (p2, 0); i++)
1482 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
1483 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
1484 && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
1485 SET_DEST (XVECEXP (p2, 0, i))))
1486 break;
1488 if (i == XVECLEN (p2, 0))
1489 for (i = 0; i < XVECLEN (p2, 0); i++)
1490 if (SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
1492 combine_merges++;
1494 subst_insn = i3;
1495 subst_low_cuid = INSN_CUID (i2);
1497 added_sets_2 = added_sets_1 = 0;
1498 i2dest = SET_SRC (PATTERN (i3));
1500 /* Replace the dest in I2 with our dest and make the resulting
1501 insn the new pattern for I3. Then skip to where we
1502 validate the pattern. Everything was set up above. */
1503 SUBST (SET_DEST (XVECEXP (p2, 0, i)),
1504 SET_DEST (PATTERN (i3)));
1506 newpat = p2;
1507 i3_subst_into_i2 = 1;
1508 goto validate_replacement;
1512 #ifndef HAVE_cc0
1513 /* If we have no I1 and I2 looks like:
1514 (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
1515 (set Y OP)])
1516 make up a dummy I1 that is
1517 (set Y OP)
1518 and change I2 to be
1519 (set (reg:CC X) (compare:CC Y (const_int 0)))
1521 (We can ignore any trailing CLOBBERs.)
1523 This undoes a previous combination and allows us to match a branch-and-
1524 decrement insn. */
1526 if (i1 == 0 && GET_CODE (PATTERN (i2)) == PARALLEL
1527 && XVECLEN (PATTERN (i2), 0) >= 2
1528 && GET_CODE (XVECEXP (PATTERN (i2), 0, 0)) == SET
1529 && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
1530 == MODE_CC)
1531 && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
1532 && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
1533 && GET_CODE (XVECEXP (PATTERN (i2), 0, 1)) == SET
1534 && GET_CODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 1))) == REG
1535 && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
1536 SET_SRC (XVECEXP (PATTERN (i2), 0, 1))))
1538 for (i = XVECLEN (PATTERN (i2), 0) - 1; i >= 2; i--)
1539 if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != CLOBBER)
1540 break;
1542 if (i == 1)
1544 /* We make I1 with the same INSN_UID as I2. This gives it
1545 the same INSN_CUID for value tracking. Our fake I1 will
1546 never appear in the insn stream so giving it the same INSN_UID
1547 as I2 will not cause a problem. */
1549 subst_prev_insn = i1
1550 = gen_rtx_INSN (VOIDmode, INSN_UID (i2), NULL_RTX, i2,
1551 XVECEXP (PATTERN (i2), 0, 1), -1, NULL_RTX,
1552 NULL_RTX);
1554 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
1555 SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
1556 SET_DEST (PATTERN (i1)));
1559 #endif
1561 /* Verify that I2 and I1 are valid for combining. */
1562 if (! can_combine_p (i2, i3, i1, NULL_RTX, &i2dest, &i2src)
1563 || (i1 && ! can_combine_p (i1, i3, NULL_RTX, i2, &i1dest, &i1src)))
1565 undo_all ();
1566 return 0;
1569 /* Record whether I2DEST is used in I2SRC and similarly for the other
1570 cases. Knowing this will help in register status updating below. */
1571 i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
1572 i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
1573 i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
1575 /* See if I1 directly feeds into I3. It does if I1DEST is not used
1576 in I2SRC. */
1577 i1_feeds_i3 = i1 && ! reg_overlap_mentioned_p (i1dest, i2src);
1579 /* Ensure that I3's pattern can be the destination of combines. */
1580 if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest,
1581 i1 && i2dest_in_i1src && i1_feeds_i3,
1582 &i3dest_killed))
1584 undo_all ();
1585 return 0;
1588 /* See if any of the insns is a MULT operation. Unless one is, we will
1589 reject a combination that is, since it must be slower. Be conservative
1590 here. */
1591 if (GET_CODE (i2src) == MULT
1592 || (i1 != 0 && GET_CODE (i1src) == MULT)
1593 || (GET_CODE (PATTERN (i3)) == SET
1594 && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
1595 have_mult = 1;
1597 /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
1598 We used to do this EXCEPT in one case: I3 has a post-inc in an
1599 output operand. However, that exception can give rise to insns like
1600 mov r3,(r3)+
1601 which is a famous insn on the PDP-11 where the value of r3 used as the
1602 source was model-dependent. Avoid this sort of thing. */
1604 #if 0
1605 if (!(GET_CODE (PATTERN (i3)) == SET
1606 && GET_CODE (SET_SRC (PATTERN (i3))) == REG
1607 && GET_CODE (SET_DEST (PATTERN (i3))) == MEM
1608 && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
1609 || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
1610 /* It's not the exception. */
1611 #endif
1612 #ifdef AUTO_INC_DEC
1613 for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
1614 if (REG_NOTE_KIND (link) == REG_INC
1615 && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
1616 || (i1 != 0
1617 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
1619 undo_all ();
1620 return 0;
1622 #endif
1624 /* See if the SETs in I1 or I2 need to be kept around in the merged
1625 instruction: whenever the value set there is still needed past I3.
1626 For the SETs in I2, this is easy: we see if I2DEST dies or is set in I3.
1628 For the SET in I1, we have two cases: If I1 and I2 independently
1629 feed into I3, the set in I1 needs to be kept around if I1DEST dies
1630 or is set in I3. Otherwise (if I1 feeds I2 which feeds I3), the set
1631 in I1 needs to be kept around unless I1DEST dies or is set in either
1632 I2 or I3. We can distinguish these cases by seeing if I2SRC mentions
1633 I1DEST. If so, we know I1 feeds into I2. */
1635 added_sets_2 = ! dead_or_set_p (i3, i2dest);
1637 added_sets_1
1638 = i1 && ! (i1_feeds_i3 ? dead_or_set_p (i3, i1dest)
1639 : (dead_or_set_p (i3, i1dest) || dead_or_set_p (i2, i1dest)));
1641 /* If the set in I2 needs to be kept around, we must make a copy of
1642 PATTERN (I2), so that when we substitute I1SRC for I1DEST in
1643 PATTERN (I2), we are only substituting for the original I1DEST, not into
1644 an already-substituted copy. This also prevents making self-referential
1645 rtx. If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
1646 I2DEST. */
1648 i2pat = (GET_CODE (PATTERN (i2)) == PARALLEL
1649 ? gen_rtx_SET (VOIDmode, i2dest, i2src)
1650 : PATTERN (i2));
1652 if (added_sets_2)
1653 i2pat = copy_rtx (i2pat);
1655 combine_merges++;
1657 /* Substitute in the latest insn for the regs set by the earlier ones. */
1659 maxreg = max_reg_num ();
1661 subst_insn = i3;
1663 /* It is possible that the source of I2 or I1 may be performing an
1664 unneeded operation, such as a ZERO_EXTEND of something that is known
1665 to have the high part zero. Handle that case by letting subst look at
1666 the innermost one of them.
1668 Another way to do this would be to have a function that tries to
1669 simplify a single insn instead of merging two or more insns. We don't
1670 do this because of the potential of infinite loops and because
1671 of the potential extra memory required. However, doing it the way
1672 we are is a bit of a kludge and doesn't catch all cases.
1674 But only do this if -fexpensive-optimizations since it slows things down
1675 and doesn't usually win. */
1677 if (flag_expensive_optimizations)
1679 /* Pass pc_rtx so no substitutions are done, just simplifications.
1680 The cases that we are interested in here do not involve the few
1681 cases were is_replaced is checked. */
1682 if (i1)
1684 subst_low_cuid = INSN_CUID (i1);
1685 i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0);
1687 else
1689 subst_low_cuid = INSN_CUID (i2);
1690 i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0);
1693 undobuf.previous_undos = undobuf.undos;
1696 #ifndef HAVE_cc0
1697 /* Many machines that don't use CC0 have insns that can both perform an
1698 arithmetic operation and set the condition code. These operations will
1699 be represented as a PARALLEL with the first element of the vector
1700 being a COMPARE of an arithmetic operation with the constant zero.
1701 The second element of the vector will set some pseudo to the result
1702 of the same arithmetic operation. If we simplify the COMPARE, we won't
1703 match such a pattern and so will generate an extra insn. Here we test
1704 for this case, where both the comparison and the operation result are
1705 needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
1706 I2SRC. Later we will make the PARALLEL that contains I2. */
1708 if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
1709 && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
1710 && XEXP (SET_SRC (PATTERN (i3)), 1) == const0_rtx
1711 && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
1713 #ifdef EXTRA_CC_MODES
1714 rtx *cc_use;
1715 enum machine_mode compare_mode;
1716 #endif
1718 newpat = PATTERN (i3);
1719 SUBST (XEXP (SET_SRC (newpat), 0), i2src);
1721 i2_is_used = 1;
1723 #ifdef EXTRA_CC_MODES
1724 /* See if a COMPARE with the operand we substituted in should be done
1725 with the mode that is currently being used. If not, do the same
1726 processing we do in `subst' for a SET; namely, if the destination
1727 is used only once, try to replace it with a register of the proper
1728 mode and also replace the COMPARE. */
1729 if (undobuf.other_insn == 0
1730 && (cc_use = find_single_use (SET_DEST (newpat), i3,
1731 &undobuf.other_insn))
1732 && ((compare_mode = SELECT_CC_MODE (GET_CODE (*cc_use),
1733 i2src, const0_rtx))
1734 != GET_MODE (SET_DEST (newpat))))
1736 int regno = REGNO (SET_DEST (newpat));
1737 rtx new_dest = gen_rtx_REG (compare_mode, regno);
1739 if (regno < FIRST_PSEUDO_REGISTER
1740 || (REG_N_SETS (regno) == 1 && ! added_sets_2
1741 && ! REG_USERVAR_P (SET_DEST (newpat))))
1743 if (regno >= FIRST_PSEUDO_REGISTER)
1744 SUBST (regno_reg_rtx[regno], new_dest);
1746 SUBST (SET_DEST (newpat), new_dest);
1747 SUBST (XEXP (*cc_use, 0), new_dest);
1748 SUBST (SET_SRC (newpat),
1749 gen_rtx_combine (COMPARE, compare_mode,
1750 i2src, const0_rtx));
1752 else
1753 undobuf.other_insn = 0;
1755 #endif
1757 else
1758 #endif
1760 n_occurrences = 0; /* `subst' counts here */
1762 /* If I1 feeds into I2 (not into I3) and I1DEST is in I1SRC, we
1763 need to make a unique copy of I2SRC each time we substitute it
1764 to avoid self-referential rtl. */
1766 subst_low_cuid = INSN_CUID (i2);
1767 newpat = subst (PATTERN (i3), i2dest, i2src, 0,
1768 ! i1_feeds_i3 && i1dest_in_i1src);
1769 undobuf.previous_undos = undobuf.undos;
1771 /* Record whether i2's body now appears within i3's body. */
1772 i2_is_used = n_occurrences;
1775 /* If we already got a failure, don't try to do more. Otherwise,
1776 try to substitute in I1 if we have it. */
1778 if (i1 && GET_CODE (newpat) != CLOBBER)
1780 /* Before we can do this substitution, we must redo the test done
1781 above (see detailed comments there) that ensures that I1DEST
1782 isn't mentioned in any SETs in NEWPAT that are field assignments. */
1784 if (! combinable_i3pat (NULL_RTX, &newpat, i1dest, NULL_RTX,
1785 0, NULL_PTR))
1787 undo_all ();
1788 return 0;
1791 n_occurrences = 0;
1792 subst_low_cuid = INSN_CUID (i1);
1793 newpat = subst (newpat, i1dest, i1src, 0, 0);
1794 undobuf.previous_undos = undobuf.undos;
1797 /* Fail if an autoincrement side-effect has been duplicated. Be careful
1798 to count all the ways that I2SRC and I1SRC can be used. */
1799 if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
1800 && i2_is_used + added_sets_2 > 1)
1801 || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
1802 && (n_occurrences + added_sets_1 + (added_sets_2 && ! i1_feeds_i3)
1803 > 1))
1804 /* Fail if we tried to make a new register (we used to abort, but there's
1805 really no reason to). */
1806 || max_reg_num () != maxreg
1807 /* Fail if we couldn't do something and have a CLOBBER. */
1808 || GET_CODE (newpat) == CLOBBER
1809 /* Fail if this new pattern is a MULT and we didn't have one before
1810 at the outer level. */
1811 || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
1812 && ! have_mult))
1814 undo_all ();
1815 return 0;
1818 /* If the actions of the earlier insns must be kept
1819 in addition to substituting them into the latest one,
1820 we must make a new PARALLEL for the latest insn
1821 to hold additional the SETs. */
1823 if (added_sets_1 || added_sets_2)
1825 combine_extras++;
1827 if (GET_CODE (newpat) == PARALLEL)
1829 rtvec old = XVEC (newpat, 0);
1830 total_sets = XVECLEN (newpat, 0) + added_sets_1 + added_sets_2;
1831 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
1832 bcopy ((char *) &old->elem[0], (char *) XVEC (newpat, 0)->elem,
1833 sizeof (old->elem[0]) * old->num_elem);
1835 else
1837 rtx old = newpat;
1838 total_sets = 1 + added_sets_1 + added_sets_2;
1839 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
1840 XVECEXP (newpat, 0, 0) = old;
1843 if (added_sets_1)
1844 XVECEXP (newpat, 0, --total_sets)
1845 = (GET_CODE (PATTERN (i1)) == PARALLEL
1846 ? gen_rtx_SET (VOIDmode, i1dest, i1src) : PATTERN (i1));
1848 if (added_sets_2)
1850 /* If there is no I1, use I2's body as is. We used to also not do
1851 the subst call below if I2 was substituted into I3,
1852 but that could lose a simplification. */
1853 if (i1 == 0)
1854 XVECEXP (newpat, 0, --total_sets) = i2pat;
1855 else
1856 /* See comment where i2pat is assigned. */
1857 XVECEXP (newpat, 0, --total_sets)
1858 = subst (i2pat, i1dest, i1src, 0, 0);
1862 /* We come here when we are replacing a destination in I2 with the
1863 destination of I3. */
1864 validate_replacement:
1866 /* Note which hard regs this insn has as inputs. */
1867 mark_used_regs_combine (newpat);
1869 /* Is the result of combination a valid instruction? */
1870 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
1872 /* If the result isn't valid, see if it is a PARALLEL of two SETs where
1873 the second SET's destination is a register that is unused. In that case,
1874 we just need the first SET. This can occur when simplifying a divmod
1875 insn. We *must* test for this case here because the code below that
1876 splits two independent SETs doesn't handle this case correctly when it
1877 updates the register status. Also check the case where the first
1878 SET's destination is unused. That would not cause incorrect code, but
1879 does cause an unneeded insn to remain. */
1881 if (insn_code_number < 0 && GET_CODE (newpat) == PARALLEL
1882 && XVECLEN (newpat, 0) == 2
1883 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
1884 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
1885 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == REG
1886 && find_reg_note (i3, REG_UNUSED, SET_DEST (XVECEXP (newpat, 0, 1)))
1887 && ! side_effects_p (SET_SRC (XVECEXP (newpat, 0, 1)))
1888 && asm_noperands (newpat) < 0)
1890 newpat = XVECEXP (newpat, 0, 0);
1891 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
1894 else if (insn_code_number < 0 && GET_CODE (newpat) == PARALLEL
1895 && XVECLEN (newpat, 0) == 2
1896 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
1897 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
1898 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) == REG
1899 && find_reg_note (i3, REG_UNUSED, SET_DEST (XVECEXP (newpat, 0, 0)))
1900 && ! side_effects_p (SET_SRC (XVECEXP (newpat, 0, 0)))
1901 && asm_noperands (newpat) < 0)
1903 newpat = XVECEXP (newpat, 0, 1);
1904 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
1907 /* If we were combining three insns and the result is a simple SET
1908 with no ASM_OPERANDS that wasn't recognized, try to split it into two
1909 insns. There are two ways to do this. It can be split using a
1910 machine-specific method (like when you have an addition of a large
1911 constant) or by combine in the function find_split_point. */
1913 if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
1914 && asm_noperands (newpat) < 0)
1916 rtx m_split, *split;
1917 rtx ni2dest = i2dest;
1919 /* See if the MD file can split NEWPAT. If it can't, see if letting it
1920 use I2DEST as a scratch register will help. In the latter case,
1921 convert I2DEST to the mode of the source of NEWPAT if we can. */
1923 m_split = split_insns (newpat, i3);
1925 /* We can only use I2DEST as a scratch reg if it doesn't overlap any
1926 inputs of NEWPAT. */
1928 /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
1929 possible to try that as a scratch reg. This would require adding
1930 more code to make it work though. */
1932 if (m_split == 0 && ! reg_overlap_mentioned_p (ni2dest, newpat))
1934 /* If I2DEST is a hard register or the only use of a pseudo,
1935 we can change its mode. */
1936 if (GET_MODE (SET_DEST (newpat)) != GET_MODE (i2dest)
1937 && GET_MODE (SET_DEST (newpat)) != VOIDmode
1938 && GET_CODE (i2dest) == REG
1939 && (REGNO (i2dest) < FIRST_PSEUDO_REGISTER
1940 || (REG_N_SETS (REGNO (i2dest)) == 1 && ! added_sets_2
1941 && ! REG_USERVAR_P (i2dest))))
1942 ni2dest = gen_rtx_REG (GET_MODE (SET_DEST (newpat)),
1943 REGNO (i2dest));
1945 m_split = split_insns (gen_rtx_PARALLEL
1946 (VOIDmode,
1947 gen_rtvec (2, newpat,
1948 gen_rtx_CLOBBER (VOIDmode,
1949 ni2dest))),
1950 i3);
1953 if (m_split && GET_CODE (m_split) == SEQUENCE
1954 && XVECLEN (m_split, 0) == 2
1955 && (next_real_insn (i2) == i3
1956 || ! use_crosses_set_p (PATTERN (XVECEXP (m_split, 0, 0)),
1957 INSN_CUID (i2))))
1959 rtx i2set, i3set;
1960 rtx newi3pat = PATTERN (XVECEXP (m_split, 0, 1));
1961 newi2pat = PATTERN (XVECEXP (m_split, 0, 0));
1963 i3set = single_set (XVECEXP (m_split, 0, 1));
1964 i2set = single_set (XVECEXP (m_split, 0, 0));
1966 /* In case we changed the mode of I2DEST, replace it in the
1967 pseudo-register table here. We can't do it above in case this
1968 code doesn't get executed and we do a split the other way. */
1970 if (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
1971 SUBST (regno_reg_rtx[REGNO (i2dest)], ni2dest);
1973 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
1975 /* If I2 or I3 has multiple SETs, we won't know how to track
1976 register status, so don't use these insns. If I2's destination
1977 is used between I2 and I3, we also can't use these insns. */
1979 if (i2_code_number >= 0 && i2set && i3set
1980 && (next_real_insn (i2) == i3
1981 || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
1982 insn_code_number = recog_for_combine (&newi3pat, i3,
1983 &new_i3_notes);
1984 if (insn_code_number >= 0)
1985 newpat = newi3pat;
1987 /* It is possible that both insns now set the destination of I3.
1988 If so, we must show an extra use of it. */
1990 if (insn_code_number >= 0)
1992 rtx new_i3_dest = SET_DEST (i3set);
1993 rtx new_i2_dest = SET_DEST (i2set);
1995 while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
1996 || GET_CODE (new_i3_dest) == STRICT_LOW_PART
1997 || GET_CODE (new_i3_dest) == SUBREG)
1998 new_i3_dest = XEXP (new_i3_dest, 0);
2000 while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
2001 || GET_CODE (new_i2_dest) == STRICT_LOW_PART
2002 || GET_CODE (new_i2_dest) == SUBREG)
2003 new_i2_dest = XEXP (new_i2_dest, 0);
2005 if (GET_CODE (new_i3_dest) == REG
2006 && GET_CODE (new_i2_dest) == REG
2007 && REGNO (new_i3_dest) == REGNO (new_i2_dest))
2008 REG_N_SETS (REGNO (new_i2_dest))++;
2012 /* If we can split it and use I2DEST, go ahead and see if that
2013 helps things be recognized. Verify that none of the registers
2014 are set between I2 and I3. */
2015 if (insn_code_number < 0 && (split = find_split_point (&newpat, i3)) != 0
2016 #ifdef HAVE_cc0
2017 && GET_CODE (i2dest) == REG
2018 #endif
2019 /* We need I2DEST in the proper mode. If it is a hard register
2020 or the only use of a pseudo, we can change its mode. */
2021 && (GET_MODE (*split) == GET_MODE (i2dest)
2022 || GET_MODE (*split) == VOIDmode
2023 || REGNO (i2dest) < FIRST_PSEUDO_REGISTER
2024 || (REG_N_SETS (REGNO (i2dest)) == 1 && ! added_sets_2
2025 && ! REG_USERVAR_P (i2dest)))
2026 && (next_real_insn (i2) == i3
2027 || ! use_crosses_set_p (*split, INSN_CUID (i2)))
2028 /* We can't overwrite I2DEST if its value is still used by
2029 NEWPAT. */
2030 && ! reg_referenced_p (i2dest, newpat))
2032 rtx newdest = i2dest;
2033 enum rtx_code split_code = GET_CODE (*split);
2034 enum machine_mode split_mode = GET_MODE (*split);
2036 /* Get NEWDEST as a register in the proper mode. We have already
2037 validated that we can do this. */
2038 if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
2040 newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
2042 if (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
2043 SUBST (regno_reg_rtx[REGNO (i2dest)], newdest);
2046 /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
2047 an ASHIFT. This can occur if it was inside a PLUS and hence
2048 appeared to be a memory address. This is a kludge. */
2049 if (split_code == MULT
2050 && GET_CODE (XEXP (*split, 1)) == CONST_INT
2051 && (i = exact_log2 (INTVAL (XEXP (*split, 1)))) >= 0)
2053 SUBST (*split, gen_rtx_combine (ASHIFT, split_mode,
2054 XEXP (*split, 0), GEN_INT (i)));
2055 /* Update split_code because we may not have a multiply
2056 anymore. */
2057 split_code = GET_CODE (*split);
2060 #ifdef INSN_SCHEDULING
2061 /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
2062 be written as a ZERO_EXTEND. */
2063 if (split_code == SUBREG && GET_CODE (SUBREG_REG (*split)) == MEM)
2064 SUBST (*split, gen_rtx_combine (ZERO_EXTEND, split_mode,
2065 XEXP (*split, 0)));
2066 #endif
2068 newi2pat = gen_rtx_combine (SET, VOIDmode, newdest, *split);
2069 SUBST (*split, newdest);
2070 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2072 /* If the split point was a MULT and we didn't have one before,
2073 don't use one now. */
2074 if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
2075 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2079 /* Check for a case where we loaded from memory in a narrow mode and
2080 then sign extended it, but we need both registers. In that case,
2081 we have a PARALLEL with both loads from the same memory location.
2082 We can split this into a load from memory followed by a register-register
2083 copy. This saves at least one insn, more if register allocation can
2084 eliminate the copy.
2086 We cannot do this if the destination of the second assignment is
2087 a register that we have already assumed is zero-extended. Similarly
2088 for a SUBREG of such a register. */
2090 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
2091 && GET_CODE (newpat) == PARALLEL
2092 && XVECLEN (newpat, 0) == 2
2093 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2094 && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
2095 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2096 && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2097 XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
2098 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2099 INSN_CUID (i2))
2100 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
2101 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
2102 && ! (temp = SET_DEST (XVECEXP (newpat, 0, 1)),
2103 (GET_CODE (temp) == REG
2104 && reg_nonzero_bits[REGNO (temp)] != 0
2105 && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
2106 && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
2107 && (reg_nonzero_bits[REGNO (temp)]
2108 != GET_MODE_MASK (word_mode))))
2109 && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
2110 && (temp = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
2111 (GET_CODE (temp) == REG
2112 && reg_nonzero_bits[REGNO (temp)] != 0
2113 && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
2114 && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
2115 && (reg_nonzero_bits[REGNO (temp)]
2116 != GET_MODE_MASK (word_mode)))))
2117 && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
2118 SET_SRC (XVECEXP (newpat, 0, 1)))
2119 && ! find_reg_note (i3, REG_UNUSED,
2120 SET_DEST (XVECEXP (newpat, 0, 0))))
2122 rtx ni2dest;
2124 newi2pat = XVECEXP (newpat, 0, 0);
2125 ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
2126 newpat = XVECEXP (newpat, 0, 1);
2127 SUBST (SET_SRC (newpat),
2128 gen_lowpart_for_combine (GET_MODE (SET_SRC (newpat)), ni2dest));
2129 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2131 if (i2_code_number >= 0)
2132 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2134 if (insn_code_number >= 0)
2136 rtx insn;
2137 rtx link;
2139 /* If we will be able to accept this, we have made a change to the
2140 destination of I3. This can invalidate a LOG_LINKS pointing
2141 to I3. No other part of combine.c makes such a transformation.
2143 The new I3 will have a destination that was previously the
2144 destination of I1 or I2 and which was used in i2 or I3. Call
2145 distribute_links to make a LOG_LINK from the next use of
2146 that destination. */
2148 PATTERN (i3) = newpat;
2149 distribute_links (gen_rtx_INSN_LIST (VOIDmode, i3, NULL_RTX));
2151 /* I3 now uses what used to be its destination and which is
2152 now I2's destination. That means we need a LOG_LINK from
2153 I3 to I2. But we used to have one, so we still will.
2155 However, some later insn might be using I2's dest and have
2156 a LOG_LINK pointing at I3. We must remove this link.
2157 The simplest way to remove the link is to point it at I1,
2158 which we know will be a NOTE. */
2160 for (insn = NEXT_INSN (i3);
2161 insn && (this_basic_block == n_basic_blocks - 1
2162 || insn != BLOCK_HEAD (this_basic_block + 1));
2163 insn = NEXT_INSN (insn))
2165 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
2166 && reg_referenced_p (ni2dest, PATTERN (insn)))
2168 for (link = LOG_LINKS (insn); link;
2169 link = XEXP (link, 1))
2170 if (XEXP (link, 0) == i3)
2171 XEXP (link, 0) = i1;
2173 break;
2179 /* Similarly, check for a case where we have a PARALLEL of two independent
2180 SETs but we started with three insns. In this case, we can do the sets
2181 as two separate insns. This case occurs when some SET allows two
2182 other insns to combine, but the destination of that SET is still live. */
2184 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
2185 && GET_CODE (newpat) == PARALLEL
2186 && XVECLEN (newpat, 0) == 2
2187 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2188 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
2189 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
2190 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2191 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
2192 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
2193 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2194 INSN_CUID (i2))
2195 /* Don't pass sets with (USE (MEM ...)) dests to the following. */
2196 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != USE
2197 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != USE
2198 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
2199 XVECEXP (newpat, 0, 0))
2200 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
2201 XVECEXP (newpat, 0, 1)))
2203 /* Normally, it doesn't matter which of the two is done first,
2204 but it does if one references cc0. In that case, it has to
2205 be first. */
2206 #ifdef HAVE_cc0
2207 if (reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 0)))
2209 newi2pat = XVECEXP (newpat, 0, 0);
2210 newpat = XVECEXP (newpat, 0, 1);
2212 else
2213 #endif
2215 newi2pat = XVECEXP (newpat, 0, 1);
2216 newpat = XVECEXP (newpat, 0, 0);
2219 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2221 if (i2_code_number >= 0)
2222 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2225 /* If it still isn't recognized, fail and change things back the way they
2226 were. */
2227 if ((insn_code_number < 0
2228 /* Is the result a reasonable ASM_OPERANDS? */
2229 && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
2231 undo_all ();
2232 return 0;
2235 /* If we had to change another insn, make sure it is valid also. */
2236 if (undobuf.other_insn)
2238 rtx other_pat = PATTERN (undobuf.other_insn);
2239 rtx new_other_notes;
2240 rtx note, next;
2242 CLEAR_HARD_REG_SET (newpat_used_regs);
2244 other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
2245 &new_other_notes);
2247 if (other_code_number < 0 && ! check_asm_operands (other_pat))
2249 undo_all ();
2250 return 0;
2253 PATTERN (undobuf.other_insn) = other_pat;
2255 /* If any of the notes in OTHER_INSN were REG_UNUSED, ensure that they
2256 are still valid. Then add any non-duplicate notes added by
2257 recog_for_combine. */
2258 for (note = REG_NOTES (undobuf.other_insn); note; note = next)
2260 next = XEXP (note, 1);
2262 if (REG_NOTE_KIND (note) == REG_UNUSED
2263 && ! reg_set_p (XEXP (note, 0), PATTERN (undobuf.other_insn)))
2265 if (GET_CODE (XEXP (note, 0)) == REG)
2266 REG_N_DEATHS (REGNO (XEXP (note, 0)))--;
2268 remove_note (undobuf.other_insn, note);
2272 for (note = new_other_notes; note; note = XEXP (note, 1))
2273 if (GET_CODE (XEXP (note, 0)) == REG)
2274 REG_N_DEATHS (REGNO (XEXP (note, 0)))++;
2276 distribute_notes (new_other_notes, undobuf.other_insn,
2277 undobuf.other_insn, NULL_RTX, NULL_RTX, NULL_RTX);
2280 /* We now know that we can do this combination. Merge the insns and
2281 update the status of registers and LOG_LINKS. */
2284 rtx i3notes, i2notes, i1notes = 0;
2285 rtx i3links, i2links, i1links = 0;
2286 rtx midnotes = 0;
2287 register int regno;
2288 /* Compute which registers we expect to eliminate. newi2pat may be setting
2289 either i3dest or i2dest, so we must check it. Also, i1dest may be the
2290 same as i3dest, in which case newi2pat may be setting i1dest. */
2291 rtx elim_i2 = ((newi2pat && reg_set_p (i2dest, newi2pat))
2292 || i2dest_in_i2src || i2dest_in_i1src
2293 ? 0 : i2dest);
2294 rtx elim_i1 = (i1 == 0 || i1dest_in_i1src
2295 || (newi2pat && reg_set_p (i1dest, newi2pat))
2296 ? 0 : i1dest);
2298 /* Get the old REG_NOTES and LOG_LINKS from all our insns and
2299 clear them. */
2300 i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
2301 i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
2302 if (i1)
2303 i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
2305 /* Ensure that we do not have something that should not be shared but
2306 occurs multiple times in the new insns. Check this by first
2307 resetting all the `used' flags and then copying anything is shared. */
2309 reset_used_flags (i3notes);
2310 reset_used_flags (i2notes);
2311 reset_used_flags (i1notes);
2312 reset_used_flags (newpat);
2313 reset_used_flags (newi2pat);
2314 if (undobuf.other_insn)
2315 reset_used_flags (PATTERN (undobuf.other_insn));
2317 i3notes = copy_rtx_if_shared (i3notes);
2318 i2notes = copy_rtx_if_shared (i2notes);
2319 i1notes = copy_rtx_if_shared (i1notes);
2320 newpat = copy_rtx_if_shared (newpat);
2321 newi2pat = copy_rtx_if_shared (newi2pat);
2322 if (undobuf.other_insn)
2323 reset_used_flags (PATTERN (undobuf.other_insn));
2325 INSN_CODE (i3) = insn_code_number;
2326 PATTERN (i3) = newpat;
2327 if (undobuf.other_insn)
2328 INSN_CODE (undobuf.other_insn) = other_code_number;
2330 /* We had one special case above where I2 had more than one set and
2331 we replaced a destination of one of those sets with the destination
2332 of I3. In that case, we have to update LOG_LINKS of insns later
2333 in this basic block. Note that this (expensive) case is rare.
2335 Also, in this case, we must pretend that all REG_NOTEs for I2
2336 actually came from I3, so that REG_UNUSED notes from I2 will be
2337 properly handled. */
2339 if (i3_subst_into_i2)
2341 for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
2342 if (GET_CODE (SET_DEST (XVECEXP (PATTERN (i2), 0, i))) == REG
2343 && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
2344 && ! find_reg_note (i2, REG_UNUSED,
2345 SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
2346 for (temp = NEXT_INSN (i2);
2347 temp && (this_basic_block == n_basic_blocks - 1
2348 || BLOCK_HEAD (this_basic_block) != temp);
2349 temp = NEXT_INSN (temp))
2350 if (temp != i3 && GET_RTX_CLASS (GET_CODE (temp)) == 'i')
2351 for (link = LOG_LINKS (temp); link; link = XEXP (link, 1))
2352 if (XEXP (link, 0) == i2)
2353 XEXP (link, 0) = i3;
2355 if (i3notes)
2357 rtx link = i3notes;
2358 while (XEXP (link, 1))
2359 link = XEXP (link, 1);
2360 XEXP (link, 1) = i2notes;
2362 else
2363 i3notes = i2notes;
2364 i2notes = 0;
2367 LOG_LINKS (i3) = 0;
2368 REG_NOTES (i3) = 0;
2369 LOG_LINKS (i2) = 0;
2370 REG_NOTES (i2) = 0;
2372 if (newi2pat)
2374 INSN_CODE (i2) = i2_code_number;
2375 PATTERN (i2) = newi2pat;
2377 else
2379 PUT_CODE (i2, NOTE);
2380 NOTE_LINE_NUMBER (i2) = NOTE_INSN_DELETED;
2381 NOTE_SOURCE_FILE (i2) = 0;
2384 if (i1)
2386 LOG_LINKS (i1) = 0;
2387 REG_NOTES (i1) = 0;
2388 PUT_CODE (i1, NOTE);
2389 NOTE_LINE_NUMBER (i1) = NOTE_INSN_DELETED;
2390 NOTE_SOURCE_FILE (i1) = 0;
2393 /* Get death notes for everything that is now used in either I3 or
2394 I2 and used to die in a previous insn. If we built two new
2395 patterns, move from I1 to I2 then I2 to I3 so that we get the
2396 proper movement on registers that I2 modifies. */
2398 if (newi2pat)
2400 move_deaths (newi2pat, NULL_RTX, INSN_CUID (i1), i2, &midnotes);
2401 move_deaths (newpat, newi2pat, INSN_CUID (i1), i3, &midnotes);
2403 else
2404 move_deaths (newpat, NULL_RTX, i1 ? INSN_CUID (i1) : INSN_CUID (i2),
2405 i3, &midnotes);
2407 /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3. */
2408 if (i3notes)
2409 distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL_RTX,
2410 elim_i2, elim_i1);
2411 if (i2notes)
2412 distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL_RTX,
2413 elim_i2, elim_i1);
2414 if (i1notes)
2415 distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL_RTX,
2416 elim_i2, elim_i1);
2417 if (midnotes)
2418 distribute_notes (midnotes, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
2419 elim_i2, elim_i1);
2421 /* Distribute any notes added to I2 or I3 by recog_for_combine. We
2422 know these are REG_UNUSED and want them to go to the desired insn,
2423 so we always pass it as i3. We have not counted the notes in
2424 reg_n_deaths yet, so we need to do so now. */
2426 if (newi2pat && new_i2_notes)
2428 for (temp = new_i2_notes; temp; temp = XEXP (temp, 1))
2429 if (GET_CODE (XEXP (temp, 0)) == REG)
2430 REG_N_DEATHS (REGNO (XEXP (temp, 0)))++;
2432 distribute_notes (new_i2_notes, i2, i2, NULL_RTX, NULL_RTX, NULL_RTX);
2435 if (new_i3_notes)
2437 for (temp = new_i3_notes; temp; temp = XEXP (temp, 1))
2438 if (GET_CODE (XEXP (temp, 0)) == REG)
2439 REG_N_DEATHS (REGNO (XEXP (temp, 0)))++;
2441 distribute_notes (new_i3_notes, i3, i3, NULL_RTX, NULL_RTX, NULL_RTX);
2444 /* If I3DEST was used in I3SRC, it really died in I3. We may need to
2445 put a REG_DEAD note for it somewhere. If NEWI2PAT exists and sets
2446 I3DEST, the death must be somewhere before I2, not I3. If we passed I3
2447 in that case, it might delete I2. Similarly for I2 and I1.
2448 Show an additional death due to the REG_DEAD note we make here. If
2449 we discard it in distribute_notes, we will decrement it again. */
2451 if (i3dest_killed)
2453 if (GET_CODE (i3dest_killed) == REG)
2454 REG_N_DEATHS (REGNO (i3dest_killed))++;
2456 if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
2457 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i3dest_killed,
2458 NULL_RTX),
2459 NULL_RTX, i2, NULL_RTX, elim_i2, elim_i1);
2460 else
2461 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i3dest_killed,
2462 NULL_RTX),
2463 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
2464 elim_i2, elim_i1);
2467 if (i2dest_in_i2src)
2469 if (GET_CODE (i2dest) == REG)
2470 REG_N_DEATHS (REGNO (i2dest))++;
2472 if (newi2pat && reg_set_p (i2dest, newi2pat))
2473 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i2dest, NULL_RTX),
2474 NULL_RTX, i2, NULL_RTX, NULL_RTX, NULL_RTX);
2475 else
2476 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i2dest, NULL_RTX),
2477 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
2478 NULL_RTX, NULL_RTX);
2481 if (i1dest_in_i1src)
2483 if (GET_CODE (i1dest) == REG)
2484 REG_N_DEATHS (REGNO (i1dest))++;
2486 if (newi2pat && reg_set_p (i1dest, newi2pat))
2487 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i1dest, NULL_RTX),
2488 NULL_RTX, i2, NULL_RTX, NULL_RTX, NULL_RTX);
2489 else
2490 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i1dest, NULL_RTX),
2491 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
2492 NULL_RTX, NULL_RTX);
2495 distribute_links (i3links);
2496 distribute_links (i2links);
2497 distribute_links (i1links);
2499 if (GET_CODE (i2dest) == REG)
2501 rtx link;
2502 rtx i2_insn = 0, i2_val = 0, set;
2504 /* The insn that used to set this register doesn't exist, and
2505 this life of the register may not exist either. See if one of
2506 I3's links points to an insn that sets I2DEST. If it does,
2507 that is now the last known value for I2DEST. If we don't update
2508 this and I2 set the register to a value that depended on its old
2509 contents, we will get confused. If this insn is used, thing
2510 will be set correctly in combine_instructions. */
2512 for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
2513 if ((set = single_set (XEXP (link, 0))) != 0
2514 && rtx_equal_p (i2dest, SET_DEST (set)))
2515 i2_insn = XEXP (link, 0), i2_val = SET_SRC (set);
2517 record_value_for_reg (i2dest, i2_insn, i2_val);
2519 /* If the reg formerly set in I2 died only once and that was in I3,
2520 zero its use count so it won't make `reload' do any work. */
2521 if (! added_sets_2
2522 && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
2523 && ! i2dest_in_i2src)
2525 regno = REGNO (i2dest);
2526 REG_N_SETS (regno)--;
2527 if (REG_N_SETS (regno) == 0
2528 && ! REGNO_REG_SET_P (BASIC_BLOCK (0)->global_live_at_start,
2529 regno))
2530 REG_N_REFS (regno) = 0;
2534 if (i1 && GET_CODE (i1dest) == REG)
2536 rtx link;
2537 rtx i1_insn = 0, i1_val = 0, set;
2539 for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
2540 if ((set = single_set (XEXP (link, 0))) != 0
2541 && rtx_equal_p (i1dest, SET_DEST (set)))
2542 i1_insn = XEXP (link, 0), i1_val = SET_SRC (set);
2544 record_value_for_reg (i1dest, i1_insn, i1_val);
2546 regno = REGNO (i1dest);
2547 if (! added_sets_1 && ! i1dest_in_i1src)
2549 REG_N_SETS (regno)--;
2550 if (REG_N_SETS (regno) == 0
2551 && ! REGNO_REG_SET_P (BASIC_BLOCK (0)->global_live_at_start,
2552 regno))
2553 REG_N_REFS (regno) = 0;
2557 /* Update reg_nonzero_bits et al for any changes that may have been made
2558 to this insn. */
2560 note_stores (newpat, set_nonzero_bits_and_sign_copies);
2561 if (newi2pat)
2562 note_stores (newi2pat, set_nonzero_bits_and_sign_copies);
2564 /* If I3 is now an unconditional jump, ensure that it has a
2565 BARRIER following it since it may have initially been a
2566 conditional jump. It may also be the last nonnote insn. */
2568 if ((GET_CODE (newpat) == RETURN || simplejump_p (i3))
2569 && ((temp = next_nonnote_insn (i3)) == NULL_RTX
2570 || GET_CODE (temp) != BARRIER))
2571 emit_barrier_after (i3);
2574 combine_successes++;
2576 /* Clear this here, so that subsequent get_last_value calls are not
2577 affected. */
2578 subst_prev_insn = NULL_RTX;
2580 if (added_links_insn
2581 && (newi2pat == 0 || INSN_CUID (added_links_insn) < INSN_CUID (i2))
2582 && INSN_CUID (added_links_insn) < INSN_CUID (i3))
2583 return added_links_insn;
2584 else
2585 return newi2pat ? i2 : i3;
2588 /* Undo all the modifications recorded in undobuf. */
2590 static void
2591 undo_all ()
2593 struct undo *undo, *next;
2595 for (undo = undobuf.undos; undo; undo = next)
2597 next = undo->next;
2598 if (undo->is_int)
2599 *undo->where.i = undo->old_contents.i;
2600 else
2601 *undo->where.r = undo->old_contents.r;
2603 undo->next = undobuf.frees;
2604 undobuf.frees = undo;
2607 obfree (undobuf.storage);
2608 undobuf.undos = undobuf.previous_undos = 0;
2610 /* Clear this here, so that subsequent get_last_value calls are not
2611 affected. */
2612 subst_prev_insn = NULL_RTX;
2615 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
2616 where we have an arithmetic expression and return that point. LOC will
2617 be inside INSN.
2619 try_combine will call this function to see if an insn can be split into
2620 two insns. */
2622 static rtx *
2623 find_split_point (loc, insn)
2624 rtx *loc;
2625 rtx insn;
2627 rtx x = *loc;
2628 enum rtx_code code = GET_CODE (x);
2629 rtx *split;
2630 int len = 0, pos = 0, unsignedp = 0;
2631 rtx inner = NULL_RTX;
2633 /* First special-case some codes. */
2634 switch (code)
2636 case SUBREG:
2637 #ifdef INSN_SCHEDULING
2638 /* If we are making a paradoxical SUBREG invalid, it becomes a split
2639 point. */
2640 if (GET_CODE (SUBREG_REG (x)) == MEM)
2641 return loc;
2642 #endif
2643 return find_split_point (&SUBREG_REG (x), insn);
2645 case MEM:
2646 #ifdef HAVE_lo_sum
2647 /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
2648 using LO_SUM and HIGH. */
2649 if (GET_CODE (XEXP (x, 0)) == CONST
2650 || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
2652 SUBST (XEXP (x, 0),
2653 gen_rtx_combine (LO_SUM, Pmode,
2654 gen_rtx_combine (HIGH, Pmode, XEXP (x, 0)),
2655 XEXP (x, 0)));
2656 return &XEXP (XEXP (x, 0), 0);
2658 #endif
2660 /* If we have a PLUS whose second operand is a constant and the
2661 address is not valid, perhaps will can split it up using
2662 the machine-specific way to split large constants. We use
2663 the first pseudo-reg (one of the virtual regs) as a placeholder;
2664 it will not remain in the result. */
2665 if (GET_CODE (XEXP (x, 0)) == PLUS
2666 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
2667 && ! memory_address_p (GET_MODE (x), XEXP (x, 0)))
2669 rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
2670 rtx seq = split_insns (gen_rtx_SET (VOIDmode, reg, XEXP (x, 0)),
2671 subst_insn);
2673 /* This should have produced two insns, each of which sets our
2674 placeholder. If the source of the second is a valid address,
2675 we can make put both sources together and make a split point
2676 in the middle. */
2678 if (seq && XVECLEN (seq, 0) == 2
2679 && GET_CODE (XVECEXP (seq, 0, 0)) == INSN
2680 && GET_CODE (PATTERN (XVECEXP (seq, 0, 0))) == SET
2681 && SET_DEST (PATTERN (XVECEXP (seq, 0, 0))) == reg
2682 && ! reg_mentioned_p (reg,
2683 SET_SRC (PATTERN (XVECEXP (seq, 0, 0))))
2684 && GET_CODE (XVECEXP (seq, 0, 1)) == INSN
2685 && GET_CODE (PATTERN (XVECEXP (seq, 0, 1))) == SET
2686 && SET_DEST (PATTERN (XVECEXP (seq, 0, 1))) == reg
2687 && memory_address_p (GET_MODE (x),
2688 SET_SRC (PATTERN (XVECEXP (seq, 0, 1)))))
2690 rtx src1 = SET_SRC (PATTERN (XVECEXP (seq, 0, 0)));
2691 rtx src2 = SET_SRC (PATTERN (XVECEXP (seq, 0, 1)));
2693 /* Replace the placeholder in SRC2 with SRC1. If we can
2694 find where in SRC2 it was placed, that can become our
2695 split point and we can replace this address with SRC2.
2696 Just try two obvious places. */
2698 src2 = replace_rtx (src2, reg, src1);
2699 split = 0;
2700 if (XEXP (src2, 0) == src1)
2701 split = &XEXP (src2, 0);
2702 else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
2703 && XEXP (XEXP (src2, 0), 0) == src1)
2704 split = &XEXP (XEXP (src2, 0), 0);
2706 if (split)
2708 SUBST (XEXP (x, 0), src2);
2709 return split;
2713 /* If that didn't work, perhaps the first operand is complex and
2714 needs to be computed separately, so make a split point there.
2715 This will occur on machines that just support REG + CONST
2716 and have a constant moved through some previous computation. */
2718 else if (GET_RTX_CLASS (GET_CODE (XEXP (XEXP (x, 0), 0))) != 'o'
2719 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
2720 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (XEXP (x, 0), 0))))
2721 == 'o')))
2722 return &XEXP (XEXP (x, 0), 0);
2724 break;
2726 case SET:
2727 #ifdef HAVE_cc0
2728 /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
2729 ZERO_EXTRACT, the most likely reason why this doesn't match is that
2730 we need to put the operand into a register. So split at that
2731 point. */
2733 if (SET_DEST (x) == cc0_rtx
2734 && GET_CODE (SET_SRC (x)) != COMPARE
2735 && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
2736 && GET_RTX_CLASS (GET_CODE (SET_SRC (x))) != 'o'
2737 && ! (GET_CODE (SET_SRC (x)) == SUBREG
2738 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (SET_SRC (x)))) == 'o'))
2739 return &SET_SRC (x);
2740 #endif
2742 /* See if we can split SET_SRC as it stands. */
2743 split = find_split_point (&SET_SRC (x), insn);
2744 if (split && split != &SET_SRC (x))
2745 return split;
2747 /* See if we can split SET_DEST as it stands. */
2748 split = find_split_point (&SET_DEST (x), insn);
2749 if (split && split != &SET_DEST (x))
2750 return split;
2752 /* See if this is a bitfield assignment with everything constant. If
2753 so, this is an IOR of an AND, so split it into that. */
2754 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
2755 && (GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)))
2756 <= HOST_BITS_PER_WIDE_INT)
2757 && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT
2758 && GET_CODE (XEXP (SET_DEST (x), 2)) == CONST_INT
2759 && GET_CODE (SET_SRC (x)) == CONST_INT
2760 && ((INTVAL (XEXP (SET_DEST (x), 1))
2761 + INTVAL (XEXP (SET_DEST (x), 2)))
2762 <= GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0))))
2763 && ! side_effects_p (XEXP (SET_DEST (x), 0)))
2765 int pos = INTVAL (XEXP (SET_DEST (x), 2));
2766 int len = INTVAL (XEXP (SET_DEST (x), 1));
2767 int src = INTVAL (SET_SRC (x));
2768 rtx dest = XEXP (SET_DEST (x), 0);
2769 enum machine_mode mode = GET_MODE (dest);
2770 unsigned HOST_WIDE_INT mask = ((HOST_WIDE_INT) 1 << len) - 1;
2772 if (BITS_BIG_ENDIAN)
2773 pos = GET_MODE_BITSIZE (mode) - len - pos;
2775 if ((unsigned HOST_WIDE_INT) src == mask)
2776 SUBST (SET_SRC (x),
2777 gen_binary (IOR, mode, dest, GEN_INT (src << pos)));
2778 else
2779 SUBST (SET_SRC (x),
2780 gen_binary (IOR, mode,
2781 gen_binary (AND, mode, dest,
2782 GEN_INT (~ (mask << pos)
2783 & GET_MODE_MASK (mode))),
2784 GEN_INT (src << pos)));
2786 SUBST (SET_DEST (x), dest);
2788 split = find_split_point (&SET_SRC (x), insn);
2789 if (split && split != &SET_SRC (x))
2790 return split;
2793 /* Otherwise, see if this is an operation that we can split into two.
2794 If so, try to split that. */
2795 code = GET_CODE (SET_SRC (x));
2797 switch (code)
2799 case AND:
2800 /* If we are AND'ing with a large constant that is only a single
2801 bit and the result is only being used in a context where we
2802 need to know if it is zero or non-zero, replace it with a bit
2803 extraction. This will avoid the large constant, which might
2804 have taken more than one insn to make. If the constant were
2805 not a valid argument to the AND but took only one insn to make,
2806 this is no worse, but if it took more than one insn, it will
2807 be better. */
2809 if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
2810 && GET_CODE (XEXP (SET_SRC (x), 0)) == REG
2811 && (pos = exact_log2 (INTVAL (XEXP (SET_SRC (x), 1)))) >= 7
2812 && GET_CODE (SET_DEST (x)) == REG
2813 && (split = find_single_use (SET_DEST (x), insn, NULL_PTR)) != 0
2814 && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
2815 && XEXP (*split, 0) == SET_DEST (x)
2816 && XEXP (*split, 1) == const0_rtx)
2818 rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
2819 XEXP (SET_SRC (x), 0),
2820 pos, NULL_RTX, 1, 1, 0, 0);
2821 if (extraction != 0)
2823 SUBST (SET_SRC (x), extraction);
2824 return find_split_point (loc, insn);
2827 break;
2829 case NE:
2830 /* if STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
2831 is known to be on, this can be converted into a NEG of a shift. */
2832 if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
2833 && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
2834 && 1 <= (pos = exact_log2
2835 (nonzero_bits (XEXP (SET_SRC (x), 0),
2836 GET_MODE (XEXP (SET_SRC (x), 0))))))
2838 enum machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
2840 SUBST (SET_SRC (x),
2841 gen_rtx_combine (NEG, mode,
2842 gen_rtx_combine (LSHIFTRT, mode,
2843 XEXP (SET_SRC (x), 0),
2844 GEN_INT (pos))));
2846 split = find_split_point (&SET_SRC (x), insn);
2847 if (split && split != &SET_SRC (x))
2848 return split;
2850 break;
2852 case SIGN_EXTEND:
2853 inner = XEXP (SET_SRC (x), 0);
2855 /* We can't optimize if either mode is a partial integer
2856 mode as we don't know how many bits are significant
2857 in those modes. */
2858 if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
2859 || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
2860 break;
2862 pos = 0;
2863 len = GET_MODE_BITSIZE (GET_MODE (inner));
2864 unsignedp = 0;
2865 break;
2867 case SIGN_EXTRACT:
2868 case ZERO_EXTRACT:
2869 if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
2870 && GET_CODE (XEXP (SET_SRC (x), 2)) == CONST_INT)
2872 inner = XEXP (SET_SRC (x), 0);
2873 len = INTVAL (XEXP (SET_SRC (x), 1));
2874 pos = INTVAL (XEXP (SET_SRC (x), 2));
2876 if (BITS_BIG_ENDIAN)
2877 pos = GET_MODE_BITSIZE (GET_MODE (inner)) - len - pos;
2878 unsignedp = (code == ZERO_EXTRACT);
2880 break;
2882 default:
2883 break;
2886 if (len && pos >= 0 && pos + len <= GET_MODE_BITSIZE (GET_MODE (inner)))
2888 enum machine_mode mode = GET_MODE (SET_SRC (x));
2890 /* For unsigned, we have a choice of a shift followed by an
2891 AND or two shifts. Use two shifts for field sizes where the
2892 constant might be too large. We assume here that we can
2893 always at least get 8-bit constants in an AND insn, which is
2894 true for every current RISC. */
2896 if (unsignedp && len <= 8)
2898 SUBST (SET_SRC (x),
2899 gen_rtx_combine
2900 (AND, mode,
2901 gen_rtx_combine (LSHIFTRT, mode,
2902 gen_lowpart_for_combine (mode, inner),
2903 GEN_INT (pos)),
2904 GEN_INT (((HOST_WIDE_INT) 1 << len) - 1)));
2906 split = find_split_point (&SET_SRC (x), insn);
2907 if (split && split != &SET_SRC (x))
2908 return split;
2910 else
2912 SUBST (SET_SRC (x),
2913 gen_rtx_combine
2914 (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
2915 gen_rtx_combine (ASHIFT, mode,
2916 gen_lowpart_for_combine (mode, inner),
2917 GEN_INT (GET_MODE_BITSIZE (mode)
2918 - len - pos)),
2919 GEN_INT (GET_MODE_BITSIZE (mode) - len)));
2921 split = find_split_point (&SET_SRC (x), insn);
2922 if (split && split != &SET_SRC (x))
2923 return split;
2927 /* See if this is a simple operation with a constant as the second
2928 operand. It might be that this constant is out of range and hence
2929 could be used as a split point. */
2930 if ((GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '2'
2931 || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == 'c'
2932 || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '<')
2933 && CONSTANT_P (XEXP (SET_SRC (x), 1))
2934 && (GET_RTX_CLASS (GET_CODE (XEXP (SET_SRC (x), 0))) == 'o'
2935 || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
2936 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (SET_SRC (x), 0))))
2937 == 'o'))))
2938 return &XEXP (SET_SRC (x), 1);
2940 /* Finally, see if this is a simple operation with its first operand
2941 not in a register. The operation might require this operand in a
2942 register, so return it as a split point. We can always do this
2943 because if the first operand were another operation, we would have
2944 already found it as a split point. */
2945 if ((GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '2'
2946 || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == 'c'
2947 || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '<'
2948 || GET_RTX_CLASS (GET_CODE (SET_SRC (x))) == '1')
2949 && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
2950 return &XEXP (SET_SRC (x), 0);
2952 return 0;
2954 case AND:
2955 case IOR:
2956 /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
2957 it is better to write this as (not (ior A B)) so we can split it.
2958 Similarly for IOR. */
2959 if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
2961 SUBST (*loc,
2962 gen_rtx_combine (NOT, GET_MODE (x),
2963 gen_rtx_combine (code == IOR ? AND : IOR,
2964 GET_MODE (x),
2965 XEXP (XEXP (x, 0), 0),
2966 XEXP (XEXP (x, 1), 0))));
2967 return find_split_point (loc, insn);
2970 /* Many RISC machines have a large set of logical insns. If the
2971 second operand is a NOT, put it first so we will try to split the
2972 other operand first. */
2973 if (GET_CODE (XEXP (x, 1)) == NOT)
2975 rtx tem = XEXP (x, 0);
2976 SUBST (XEXP (x, 0), XEXP (x, 1));
2977 SUBST (XEXP (x, 1), tem);
2979 break;
2981 default:
2982 break;
2985 /* Otherwise, select our actions depending on our rtx class. */
2986 switch (GET_RTX_CLASS (code))
2988 case 'b': /* This is ZERO_EXTRACT and SIGN_EXTRACT. */
2989 case '3':
2990 split = find_split_point (&XEXP (x, 2), insn);
2991 if (split)
2992 return split;
2993 /* ... fall through ... */
2994 case '2':
2995 case 'c':
2996 case '<':
2997 split = find_split_point (&XEXP (x, 1), insn);
2998 if (split)
2999 return split;
3000 /* ... fall through ... */
3001 case '1':
3002 /* Some machines have (and (shift ...) ...) insns. If X is not
3003 an AND, but XEXP (X, 0) is, use it as our split point. */
3004 if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
3005 return &XEXP (x, 0);
3007 split = find_split_point (&XEXP (x, 0), insn);
3008 if (split)
3009 return split;
3010 return loc;
3013 /* Otherwise, we don't have a split point. */
3014 return 0;
3017 /* Throughout X, replace FROM with TO, and return the result.
3018 The result is TO if X is FROM;
3019 otherwise the result is X, but its contents may have been modified.
3020 If they were modified, a record was made in undobuf so that
3021 undo_all will (among other things) return X to its original state.
3023 If the number of changes necessary is too much to record to undo,
3024 the excess changes are not made, so the result is invalid.
3025 The changes already made can still be undone.
3026 undobuf.num_undo is incremented for such changes, so by testing that
3027 the caller can tell whether the result is valid.
3029 `n_occurrences' is incremented each time FROM is replaced.
3031 IN_DEST is non-zero if we are processing the SET_DEST of a SET.
3033 UNIQUE_COPY is non-zero if each substitution must be unique. We do this
3034 by copying if `n_occurrences' is non-zero. */
3036 static rtx
3037 subst (x, from, to, in_dest, unique_copy)
3038 register rtx x, from, to;
3039 int in_dest;
3040 int unique_copy;
3042 register enum rtx_code code = GET_CODE (x);
3043 enum machine_mode op0_mode = VOIDmode;
3044 register const char *fmt;
3045 register int len, i;
3046 rtx new;
3048 /* Two expressions are equal if they are identical copies of a shared
3049 RTX or if they are both registers with the same register number
3050 and mode. */
3052 #define COMBINE_RTX_EQUAL_P(X,Y) \
3053 ((X) == (Y) \
3054 || (GET_CODE (X) == REG && GET_CODE (Y) == REG \
3055 && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
3057 if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
3059 n_occurrences++;
3060 return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
3063 /* If X and FROM are the same register but different modes, they will
3064 not have been seen as equal above. However, flow.c will make a
3065 LOG_LINKS entry for that case. If we do nothing, we will try to
3066 rerecognize our original insn and, when it succeeds, we will
3067 delete the feeding insn, which is incorrect.
3069 So force this insn not to match in this (rare) case. */
3070 if (! in_dest && code == REG && GET_CODE (from) == REG
3071 && REGNO (x) == REGNO (from))
3072 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
3074 /* If this is an object, we are done unless it is a MEM or LO_SUM, both
3075 of which may contain things that can be combined. */
3076 if (code != MEM && code != LO_SUM && GET_RTX_CLASS (code) == 'o')
3077 return x;
3079 /* It is possible to have a subexpression appear twice in the insn.
3080 Suppose that FROM is a register that appears within TO.
3081 Then, after that subexpression has been scanned once by `subst',
3082 the second time it is scanned, TO may be found. If we were
3083 to scan TO here, we would find FROM within it and create a
3084 self-referent rtl structure which is completely wrong. */
3085 if (COMBINE_RTX_EQUAL_P (x, to))
3086 return to;
3088 /* Parallel asm_operands need special attention because all of the
3089 inputs are shared across the arms. Furthermore, unsharing the
3090 rtl results in recognition failures. Failure to handle this case
3091 specially can result in circular rtl.
3093 Solve this by doing a normal pass across the first entry of the
3094 parallel, and only processing the SET_DESTs of the subsequent
3095 entries. Ug. */
3097 if (code == PARALLEL
3098 && GET_CODE (XVECEXP (x, 0, 0)) == SET
3099 && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
3101 new = subst (XVECEXP (x, 0, 0), from, to, 0, unique_copy);
3103 /* If this substitution failed, this whole thing fails. */
3104 if (GET_CODE (new) == CLOBBER
3105 && XEXP (new, 0) == const0_rtx)
3106 return new;
3108 SUBST (XVECEXP (x, 0, 0), new);
3110 for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
3112 rtx dest = SET_DEST (XVECEXP (x, 0, i));
3114 if (GET_CODE (dest) != REG
3115 && GET_CODE (dest) != CC0
3116 && GET_CODE (dest) != PC)
3118 new = subst (dest, from, to, 0, unique_copy);
3120 /* If this substitution failed, this whole thing fails. */
3121 if (GET_CODE (new) == CLOBBER
3122 && XEXP (new, 0) == const0_rtx)
3123 return new;
3125 SUBST (SET_DEST (XVECEXP (x, 0, i)), new);
3129 else
3131 len = GET_RTX_LENGTH (code);
3132 fmt = GET_RTX_FORMAT (code);
3134 /* We don't need to process a SET_DEST that is a register, CC0,
3135 or PC, so set up to skip this common case. All other cases
3136 where we want to suppress replacing something inside a
3137 SET_SRC are handled via the IN_DEST operand. */
3138 if (code == SET
3139 && (GET_CODE (SET_DEST (x)) == REG
3140 || GET_CODE (SET_DEST (x)) == CC0
3141 || GET_CODE (SET_DEST (x)) == PC))
3142 fmt = "ie";
3144 /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
3145 constant. */
3146 if (fmt[0] == 'e')
3147 op0_mode = GET_MODE (XEXP (x, 0));
3149 for (i = 0; i < len; i++)
3151 if (fmt[i] == 'E')
3153 register int j;
3154 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3156 if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
3158 new = (unique_copy && n_occurrences
3159 ? copy_rtx (to) : to);
3160 n_occurrences++;
3162 else
3164 new = subst (XVECEXP (x, i, j), from, to, 0,
3165 unique_copy);
3167 /* If this substitution failed, this whole thing
3168 fails. */
3169 if (GET_CODE (new) == CLOBBER
3170 && XEXP (new, 0) == const0_rtx)
3171 return new;
3174 SUBST (XVECEXP (x, i, j), new);
3177 else if (fmt[i] == 'e')
3179 if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
3181 /* In general, don't install a subreg involving two
3182 modes not tieable. It can worsen register
3183 allocation, and can even make invalid reload
3184 insns, since the reg inside may need to be copied
3185 from in the outside mode, and that may be invalid
3186 if it is an fp reg copied in integer mode.
3188 We allow two exceptions to this: It is valid if
3189 it is inside another SUBREG and the mode of that
3190 SUBREG and the mode of the inside of TO is
3191 tieable and it is valid if X is a SET that copies
3192 FROM to CC0. */
3194 if (GET_CODE (to) == SUBREG
3195 && ! MODES_TIEABLE_P (GET_MODE (to),
3196 GET_MODE (SUBREG_REG (to)))
3197 && ! (code == SUBREG
3198 && MODES_TIEABLE_P (GET_MODE (x),
3199 GET_MODE (SUBREG_REG (to))))
3200 #ifdef HAVE_cc0
3201 && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
3202 #endif
3204 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
3206 new = (unique_copy && n_occurrences ? copy_rtx (to) : to);
3207 n_occurrences++;
3209 else
3210 /* If we are in a SET_DEST, suppress most cases unless we
3211 have gone inside a MEM, in which case we want to
3212 simplify the address. We assume here that things that
3213 are actually part of the destination have their inner
3214 parts in the first expression. This is true for SUBREG,
3215 STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
3216 things aside from REG and MEM that should appear in a
3217 SET_DEST. */
3218 new = subst (XEXP (x, i), from, to,
3219 (((in_dest
3220 && (code == SUBREG || code == STRICT_LOW_PART
3221 || code == ZERO_EXTRACT))
3222 || code == SET)
3223 && i == 0), unique_copy);
3225 /* If we found that we will have to reject this combination,
3226 indicate that by returning the CLOBBER ourselves, rather than
3227 an expression containing it. This will speed things up as
3228 well as prevent accidents where two CLOBBERs are considered
3229 to be equal, thus producing an incorrect simplification. */
3231 if (GET_CODE (new) == CLOBBER && XEXP (new, 0) == const0_rtx)
3232 return new;
3234 SUBST (XEXP (x, i), new);
3239 /* Try to simplify X. If the simplification changed the code, it is likely
3240 that further simplification will help, so loop, but limit the number
3241 of repetitions that will be performed. */
3243 for (i = 0; i < 4; i++)
3245 /* If X is sufficiently simple, don't bother trying to do anything
3246 with it. */
3247 if (code != CONST_INT && code != REG && code != CLOBBER)
3248 x = simplify_rtx (x, op0_mode, i == 3, in_dest);
3250 if (GET_CODE (x) == code)
3251 break;
3253 code = GET_CODE (x);
3255 /* We no longer know the original mode of operand 0 since we
3256 have changed the form of X) */
3257 op0_mode = VOIDmode;
3260 return x;
3263 /* Simplify X, a piece of RTL. We just operate on the expression at the
3264 outer level; call `subst' to simplify recursively. Return the new
3265 expression.
3267 OP0_MODE is the original mode of XEXP (x, 0); LAST is nonzero if this
3268 will be the iteration even if an expression with a code different from
3269 X is returned; IN_DEST is nonzero if we are inside a SET_DEST. */
3271 static rtx
3272 simplify_rtx (x, op0_mode, last, in_dest)
3273 rtx x;
3274 enum machine_mode op0_mode;
3275 int last;
3276 int in_dest;
3278 enum rtx_code code = GET_CODE (x);
3279 enum machine_mode mode = GET_MODE (x);
3280 rtx temp;
3281 int i;
3283 /* If this is a commutative operation, put a constant last and a complex
3284 expression first. We don't need to do this for comparisons here. */
3285 if (GET_RTX_CLASS (code) == 'c'
3286 && ((CONSTANT_P (XEXP (x, 0)) && GET_CODE (XEXP (x, 1)) != CONST_INT)
3287 || (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == 'o'
3288 && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) != 'o')
3289 || (GET_CODE (XEXP (x, 0)) == SUBREG
3290 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0)))) == 'o'
3291 && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) != 'o')))
3293 temp = XEXP (x, 0);
3294 SUBST (XEXP (x, 0), XEXP (x, 1));
3295 SUBST (XEXP (x, 1), temp);
3298 /* If this is a PLUS, MINUS, or MULT, and the first operand is the
3299 sign extension of a PLUS with a constant, reverse the order of the sign
3300 extension and the addition. Note that this not the same as the original
3301 code, but overflow is undefined for signed values. Also note that the
3302 PLUS will have been partially moved "inside" the sign-extension, so that
3303 the first operand of X will really look like:
3304 (ashiftrt (plus (ashift A C4) C5) C4).
3305 We convert this to
3306 (plus (ashiftrt (ashift A C4) C2) C4)
3307 and replace the first operand of X with that expression. Later parts
3308 of this function may simplify the expression further.
3310 For example, if we start with (mult (sign_extend (plus A C1)) C2),
3311 we swap the SIGN_EXTEND and PLUS. Later code will apply the
3312 distributive law to produce (plus (mult (sign_extend X) C1) C3).
3314 We do this to simplify address expressions. */
3316 if ((code == PLUS || code == MINUS || code == MULT)
3317 && GET_CODE (XEXP (x, 0)) == ASHIFTRT
3318 && GET_CODE (XEXP (XEXP (x, 0), 0)) == PLUS
3319 && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ASHIFT
3320 && GET_CODE (XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 1)) == CONST_INT
3321 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3322 && XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 1) == XEXP (XEXP (x, 0), 1)
3323 && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
3324 && (temp = simplify_binary_operation (ASHIFTRT, mode,
3325 XEXP (XEXP (XEXP (x, 0), 0), 1),
3326 XEXP (XEXP (x, 0), 1))) != 0)
3328 rtx new
3329 = simplify_shift_const (NULL_RTX, ASHIFT, mode,
3330 XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 0),
3331 INTVAL (XEXP (XEXP (x, 0), 1)));
3333 new = simplify_shift_const (NULL_RTX, ASHIFTRT, mode, new,
3334 INTVAL (XEXP (XEXP (x, 0), 1)));
3336 SUBST (XEXP (x, 0), gen_binary (PLUS, mode, new, temp));
3339 /* If this is a simple operation applied to an IF_THEN_ELSE, try
3340 applying it to the arms of the IF_THEN_ELSE. This often simplifies
3341 things. Check for cases where both arms are testing the same
3342 condition.
3344 Don't do anything if all operands are very simple. */
3346 if (((GET_RTX_CLASS (code) == '2' || GET_RTX_CLASS (code) == 'c'
3347 || GET_RTX_CLASS (code) == '<')
3348 && ((GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) != 'o'
3349 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
3350 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0))))
3351 == 'o')))
3352 || (GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) != 'o'
3353 && ! (GET_CODE (XEXP (x, 1)) == SUBREG
3354 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 1))))
3355 == 'o')))))
3356 || (GET_RTX_CLASS (code) == '1'
3357 && ((GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) != 'o'
3358 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
3359 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0))))
3360 == 'o'))))))
3362 rtx cond, true, false;
3364 cond = if_then_else_cond (x, &true, &false);
3365 if (cond != 0
3366 /* If everything is a comparison, what we have is highly unlikely
3367 to be simpler, so don't use it. */
3368 && ! (GET_RTX_CLASS (code) == '<'
3369 && (GET_RTX_CLASS (GET_CODE (true)) == '<'
3370 || GET_RTX_CLASS (GET_CODE (false)) == '<')))
3372 rtx cop1 = const0_rtx;
3373 enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
3375 if (cond_code == NE && GET_RTX_CLASS (GET_CODE (cond)) == '<')
3376 return x;
3378 /* Simplify the alternative arms; this may collapse the true and
3379 false arms to store-flag values. */
3380 true = subst (true, pc_rtx, pc_rtx, 0, 0);
3381 false = subst (false, pc_rtx, pc_rtx, 0, 0);
3383 /* Restarting if we generate a store-flag expression will cause
3384 us to loop. Just drop through in this case. */
3386 /* If the result values are STORE_FLAG_VALUE and zero, we can
3387 just make the comparison operation. */
3388 if (true == const_true_rtx && false == const0_rtx)
3389 x = gen_binary (cond_code, mode, cond, cop1);
3390 else if (true == const0_rtx && false == const_true_rtx)
3391 x = gen_binary (reverse_condition (cond_code), mode, cond, cop1);
3393 /* Likewise, we can make the negate of a comparison operation
3394 if the result values are - STORE_FLAG_VALUE and zero. */
3395 else if (GET_CODE (true) == CONST_INT
3396 && INTVAL (true) == - STORE_FLAG_VALUE
3397 && false == const0_rtx)
3398 x = gen_unary (NEG, mode, mode,
3399 gen_binary (cond_code, mode, cond, cop1));
3400 else if (GET_CODE (false) == CONST_INT
3401 && INTVAL (false) == - STORE_FLAG_VALUE
3402 && true == const0_rtx)
3403 x = gen_unary (NEG, mode, mode,
3404 gen_binary (reverse_condition (cond_code),
3405 mode, cond, cop1));
3406 else
3407 return gen_rtx_IF_THEN_ELSE (mode,
3408 gen_binary (cond_code, VOIDmode,
3409 cond, cop1),
3410 true, false);
3412 code = GET_CODE (x);
3413 op0_mode = VOIDmode;
3417 /* Try to fold this expression in case we have constants that weren't
3418 present before. */
3419 temp = 0;
3420 switch (GET_RTX_CLASS (code))
3422 case '1':
3423 temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
3424 break;
3425 case '<':
3426 temp = simplify_relational_operation (code, op0_mode,
3427 XEXP (x, 0), XEXP (x, 1));
3428 #ifdef FLOAT_STORE_FLAG_VALUE
3429 if (temp != 0 && GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
3430 temp = ((temp == const0_rtx) ? CONST0_RTX (GET_MODE (x))
3431 : immed_real_const_1 (FLOAT_STORE_FLAG_VALUE, GET_MODE (x)));
3432 #endif
3433 break;
3434 case 'c':
3435 case '2':
3436 temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
3437 break;
3438 case 'b':
3439 case '3':
3440 temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
3441 XEXP (x, 1), XEXP (x, 2));
3442 break;
3445 if (temp)
3446 x = temp, code = GET_CODE (temp);
3448 /* First see if we can apply the inverse distributive law. */
3449 if (code == PLUS || code == MINUS
3450 || code == AND || code == IOR || code == XOR)
3452 x = apply_distributive_law (x);
3453 code = GET_CODE (x);
3456 /* If CODE is an associative operation not otherwise handled, see if we
3457 can associate some operands. This can win if they are constants or
3458 if they are logically related (i.e. (a & b) & a. */
3459 if ((code == PLUS || code == MINUS
3460 || code == MULT || code == AND || code == IOR || code == XOR
3461 || code == DIV || code == UDIV
3462 || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
3463 && INTEGRAL_MODE_P (mode))
3465 if (GET_CODE (XEXP (x, 0)) == code)
3467 rtx other = XEXP (XEXP (x, 0), 0);
3468 rtx inner_op0 = XEXP (XEXP (x, 0), 1);
3469 rtx inner_op1 = XEXP (x, 1);
3470 rtx inner;
3472 /* Make sure we pass the constant operand if any as the second
3473 one if this is a commutative operation. */
3474 if (CONSTANT_P (inner_op0) && GET_RTX_CLASS (code) == 'c')
3476 rtx tem = inner_op0;
3477 inner_op0 = inner_op1;
3478 inner_op1 = tem;
3480 inner = simplify_binary_operation (code == MINUS ? PLUS
3481 : code == DIV ? MULT
3482 : code == UDIV ? MULT
3483 : code,
3484 mode, inner_op0, inner_op1);
3486 /* For commutative operations, try the other pair if that one
3487 didn't simplify. */
3488 if (inner == 0 && GET_RTX_CLASS (code) == 'c')
3490 other = XEXP (XEXP (x, 0), 1);
3491 inner = simplify_binary_operation (code, mode,
3492 XEXP (XEXP (x, 0), 0),
3493 XEXP (x, 1));
3496 if (inner)
3497 return gen_binary (code, mode, other, inner);
3501 /* A little bit of algebraic simplification here. */
3502 switch (code)
3504 case MEM:
3505 /* Ensure that our address has any ASHIFTs converted to MULT in case
3506 address-recognizing predicates are called later. */
3507 temp = make_compound_operation (XEXP (x, 0), MEM);
3508 SUBST (XEXP (x, 0), temp);
3509 break;
3511 case SUBREG:
3512 /* (subreg:A (mem:B X) N) becomes a modified MEM unless the SUBREG
3513 is paradoxical. If we can't do that safely, then it becomes
3514 something nonsensical so that this combination won't take place. */
3516 if (GET_CODE (SUBREG_REG (x)) == MEM
3517 && (GET_MODE_SIZE (mode)
3518 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))))
3520 rtx inner = SUBREG_REG (x);
3521 int endian_offset = 0;
3522 /* Don't change the mode of the MEM
3523 if that would change the meaning of the address. */
3524 if (MEM_VOLATILE_P (SUBREG_REG (x))
3525 || mode_dependent_address_p (XEXP (inner, 0)))
3526 return gen_rtx_CLOBBER (mode, const0_rtx);
3528 if (BYTES_BIG_ENDIAN)
3530 if (GET_MODE_SIZE (mode) < UNITS_PER_WORD)
3531 endian_offset += UNITS_PER_WORD - GET_MODE_SIZE (mode);
3532 if (GET_MODE_SIZE (GET_MODE (inner)) < UNITS_PER_WORD)
3533 endian_offset -= (UNITS_PER_WORD
3534 - GET_MODE_SIZE (GET_MODE (inner)));
3536 /* Note if the plus_constant doesn't make a valid address
3537 then this combination won't be accepted. */
3538 x = gen_rtx_MEM (mode,
3539 plus_constant (XEXP (inner, 0),
3540 (SUBREG_WORD (x) * UNITS_PER_WORD
3541 + endian_offset)));
3542 RTX_UNCHANGING_P (x) = RTX_UNCHANGING_P (inner);
3543 MEM_COPY_ATTRIBUTES (x, inner);
3544 return x;
3547 /* If we are in a SET_DEST, these other cases can't apply. */
3548 if (in_dest)
3549 return x;
3551 /* Changing mode twice with SUBREG => just change it once,
3552 or not at all if changing back to starting mode. */
3553 if (GET_CODE (SUBREG_REG (x)) == SUBREG)
3555 if (mode == GET_MODE (SUBREG_REG (SUBREG_REG (x)))
3556 && SUBREG_WORD (x) == 0 && SUBREG_WORD (SUBREG_REG (x)) == 0)
3557 return SUBREG_REG (SUBREG_REG (x));
3559 SUBST_INT (SUBREG_WORD (x),
3560 SUBREG_WORD (x) + SUBREG_WORD (SUBREG_REG (x)));
3561 SUBST (SUBREG_REG (x), SUBREG_REG (SUBREG_REG (x)));
3564 /* SUBREG of a hard register => just change the register number
3565 and/or mode. If the hard register is not valid in that mode,
3566 suppress this combination. If the hard register is the stack,
3567 frame, or argument pointer, leave this as a SUBREG. */
3569 if (GET_CODE (SUBREG_REG (x)) == REG
3570 && REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER
3571 && REGNO (SUBREG_REG (x)) != FRAME_POINTER_REGNUM
3572 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
3573 && REGNO (SUBREG_REG (x)) != HARD_FRAME_POINTER_REGNUM
3574 #endif
3575 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
3576 && REGNO (SUBREG_REG (x)) != ARG_POINTER_REGNUM
3577 #endif
3578 && REGNO (SUBREG_REG (x)) != STACK_POINTER_REGNUM)
3580 if (HARD_REGNO_MODE_OK (REGNO (SUBREG_REG (x)) + SUBREG_WORD (x),
3581 mode))
3582 return gen_rtx_REG (mode,
3583 REGNO (SUBREG_REG (x)) + SUBREG_WORD (x));
3584 else
3585 return gen_rtx_CLOBBER (mode, const0_rtx);
3588 /* For a constant, try to pick up the part we want. Handle a full
3589 word and low-order part. Only do this if we are narrowing
3590 the constant; if it is being widened, we have no idea what
3591 the extra bits will have been set to. */
3593 if (CONSTANT_P (SUBREG_REG (x)) && op0_mode != VOIDmode
3594 && GET_MODE_SIZE (mode) == UNITS_PER_WORD
3595 && GET_MODE_SIZE (op0_mode) > UNITS_PER_WORD
3596 && GET_MODE_CLASS (mode) == MODE_INT)
3598 temp = operand_subword (SUBREG_REG (x), SUBREG_WORD (x),
3599 0, op0_mode);
3600 if (temp)
3601 return temp;
3604 /* If we want a subreg of a constant, at offset 0,
3605 take the low bits. On a little-endian machine, that's
3606 always valid. On a big-endian machine, it's valid
3607 only if the constant's mode fits in one word. Note that we
3608 cannot use subreg_lowpart_p since SUBREG_REG may be VOIDmode. */
3609 if (CONSTANT_P (SUBREG_REG (x))
3610 && ((GET_MODE_SIZE (op0_mode) <= UNITS_PER_WORD
3611 || ! WORDS_BIG_ENDIAN)
3612 ? SUBREG_WORD (x) == 0
3613 : (SUBREG_WORD (x)
3614 == ((GET_MODE_SIZE (op0_mode)
3615 - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD))
3616 / UNITS_PER_WORD)))
3617 && GET_MODE_SIZE (mode) <= GET_MODE_SIZE (op0_mode)
3618 && (! WORDS_BIG_ENDIAN
3619 || GET_MODE_BITSIZE (op0_mode) <= BITS_PER_WORD))
3620 return gen_lowpart_for_combine (mode, SUBREG_REG (x));
3622 /* A paradoxical SUBREG of a VOIDmode constant is the same constant,
3623 since we are saying that the high bits don't matter. */
3624 if (CONSTANT_P (SUBREG_REG (x)) && GET_MODE (SUBREG_REG (x)) == VOIDmode
3625 && GET_MODE_SIZE (mode) > GET_MODE_SIZE (op0_mode))
3626 return SUBREG_REG (x);
3628 /* Note that we cannot do any narrowing for non-constants since
3629 we might have been counting on using the fact that some bits were
3630 zero. We now do this in the SET. */
3632 break;
3634 case NOT:
3635 /* (not (plus X -1)) can become (neg X). */
3636 if (GET_CODE (XEXP (x, 0)) == PLUS
3637 && XEXP (XEXP (x, 0), 1) == constm1_rtx)
3638 return gen_rtx_combine (NEG, mode, XEXP (XEXP (x, 0), 0));
3640 /* Similarly, (not (neg X)) is (plus X -1). */
3641 if (GET_CODE (XEXP (x, 0)) == NEG)
3642 return gen_rtx_combine (PLUS, mode, XEXP (XEXP (x, 0), 0),
3643 constm1_rtx);
3645 /* (not (xor X C)) for C constant is (xor X D) with D = ~ C. */
3646 if (GET_CODE (XEXP (x, 0)) == XOR
3647 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3648 && (temp = simplify_unary_operation (NOT, mode,
3649 XEXP (XEXP (x, 0), 1),
3650 mode)) != 0)
3651 return gen_binary (XOR, mode, XEXP (XEXP (x, 0), 0), temp);
3653 /* (not (ashift 1 X)) is (rotate ~1 X). We used to do this for operands
3654 other than 1, but that is not valid. We could do a similar
3655 simplification for (not (lshiftrt C X)) where C is just the sign bit,
3656 but this doesn't seem common enough to bother with. */
3657 if (GET_CODE (XEXP (x, 0)) == ASHIFT
3658 && XEXP (XEXP (x, 0), 0) == const1_rtx)
3659 return gen_rtx_ROTATE (mode, gen_unary (NOT, mode, mode, const1_rtx),
3660 XEXP (XEXP (x, 0), 1));
3662 if (GET_CODE (XEXP (x, 0)) == SUBREG
3663 && subreg_lowpart_p (XEXP (x, 0))
3664 && (GET_MODE_SIZE (GET_MODE (XEXP (x, 0)))
3665 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (x, 0)))))
3666 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == ASHIFT
3667 && XEXP (SUBREG_REG (XEXP (x, 0)), 0) == const1_rtx)
3669 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (XEXP (x, 0)));
3671 x = gen_rtx_ROTATE (inner_mode,
3672 gen_unary (NOT, inner_mode, inner_mode,
3673 const1_rtx),
3674 XEXP (SUBREG_REG (XEXP (x, 0)), 1));
3675 return gen_lowpart_for_combine (mode, x);
3678 /* If STORE_FLAG_VALUE is -1, (not (comparison foo bar)) can be done by
3679 reversing the comparison code if valid. */
3680 if (STORE_FLAG_VALUE == -1
3681 && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
3682 && reversible_comparison_p (XEXP (x, 0)))
3683 return gen_rtx_combine (reverse_condition (GET_CODE (XEXP (x, 0))),
3684 mode, XEXP (XEXP (x, 0), 0),
3685 XEXP (XEXP (x, 0), 1));
3687 /* (ashiftrt foo C) where C is the number of bits in FOO minus 1
3688 is (lt foo (const_int 0)) if STORE_FLAG_VALUE is -1, so we can
3689 perform the above simplification. */
3691 if (STORE_FLAG_VALUE == -1
3692 && XEXP (x, 1) == const1_rtx
3693 && GET_CODE (XEXP (x, 0)) == ASHIFTRT
3694 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3695 && INTVAL (XEXP (XEXP (x, 0), 1)) == GET_MODE_BITSIZE (mode) - 1)
3696 return gen_rtx_combine (GE, mode, XEXP (XEXP (x, 0), 0), const0_rtx);
3698 /* Apply De Morgan's laws to reduce number of patterns for machines
3699 with negating logical insns (and-not, nand, etc.). If result has
3700 only one NOT, put it first, since that is how the patterns are
3701 coded. */
3703 if (GET_CODE (XEXP (x, 0)) == IOR || GET_CODE (XEXP (x, 0)) == AND)
3705 rtx in1 = XEXP (XEXP (x, 0), 0), in2 = XEXP (XEXP (x, 0), 1);
3707 if (GET_CODE (in1) == NOT)
3708 in1 = XEXP (in1, 0);
3709 else
3710 in1 = gen_rtx_combine (NOT, GET_MODE (in1), in1);
3712 if (GET_CODE (in2) == NOT)
3713 in2 = XEXP (in2, 0);
3714 else if (GET_CODE (in2) == CONST_INT
3715 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
3716 in2 = GEN_INT (GET_MODE_MASK (mode) & ~ INTVAL (in2));
3717 else
3718 in2 = gen_rtx_combine (NOT, GET_MODE (in2), in2);
3720 if (GET_CODE (in2) == NOT)
3722 rtx tem = in2;
3723 in2 = in1; in1 = tem;
3726 return gen_rtx_combine (GET_CODE (XEXP (x, 0)) == IOR ? AND : IOR,
3727 mode, in1, in2);
3729 break;
3731 case NEG:
3732 /* (neg (plus X 1)) can become (not X). */
3733 if (GET_CODE (XEXP (x, 0)) == PLUS
3734 && XEXP (XEXP (x, 0), 1) == const1_rtx)
3735 return gen_rtx_combine (NOT, mode, XEXP (XEXP (x, 0), 0));
3737 /* Similarly, (neg (not X)) is (plus X 1). */
3738 if (GET_CODE (XEXP (x, 0)) == NOT)
3739 return plus_constant (XEXP (XEXP (x, 0), 0), 1);
3741 /* (neg (minus X Y)) can become (minus Y X). */
3742 if (GET_CODE (XEXP (x, 0)) == MINUS
3743 && (! FLOAT_MODE_P (mode)
3744 /* x-y != -(y-x) with IEEE floating point. */
3745 || TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
3746 || flag_fast_math))
3747 return gen_binary (MINUS, mode, XEXP (XEXP (x, 0), 1),
3748 XEXP (XEXP (x, 0), 0));
3750 /* (neg (xor A 1)) is (plus A -1) if A is known to be either 0 or 1. */
3751 if (GET_CODE (XEXP (x, 0)) == XOR && XEXP (XEXP (x, 0), 1) == const1_rtx
3752 && nonzero_bits (XEXP (XEXP (x, 0), 0), mode) == 1)
3753 return gen_binary (PLUS, mode, XEXP (XEXP (x, 0), 0), constm1_rtx);
3755 /* NEG commutes with ASHIFT since it is multiplication. Only do this
3756 if we can then eliminate the NEG (e.g.,
3757 if the operand is a constant). */
3759 if (GET_CODE (XEXP (x, 0)) == ASHIFT)
3761 temp = simplify_unary_operation (NEG, mode,
3762 XEXP (XEXP (x, 0), 0), mode);
3763 if (temp)
3765 SUBST (XEXP (XEXP (x, 0), 0), temp);
3766 return XEXP (x, 0);
3770 temp = expand_compound_operation (XEXP (x, 0));
3772 /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
3773 replaced by (lshiftrt X C). This will convert
3774 (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y). */
3776 if (GET_CODE (temp) == ASHIFTRT
3777 && GET_CODE (XEXP (temp, 1)) == CONST_INT
3778 && INTVAL (XEXP (temp, 1)) == GET_MODE_BITSIZE (mode) - 1)
3779 return simplify_shift_const (temp, LSHIFTRT, mode, XEXP (temp, 0),
3780 INTVAL (XEXP (temp, 1)));
3782 /* If X has only a single bit that might be nonzero, say, bit I, convert
3783 (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
3784 MODE minus 1. This will convert (neg (zero_extract X 1 Y)) to
3785 (sign_extract X 1 Y). But only do this if TEMP isn't a register
3786 or a SUBREG of one since we'd be making the expression more
3787 complex if it was just a register. */
3789 if (GET_CODE (temp) != REG
3790 && ! (GET_CODE (temp) == SUBREG
3791 && GET_CODE (SUBREG_REG (temp)) == REG)
3792 && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
3794 rtx temp1 = simplify_shift_const
3795 (NULL_RTX, ASHIFTRT, mode,
3796 simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
3797 GET_MODE_BITSIZE (mode) - 1 - i),
3798 GET_MODE_BITSIZE (mode) - 1 - i);
3800 /* If all we did was surround TEMP with the two shifts, we
3801 haven't improved anything, so don't use it. Otherwise,
3802 we are better off with TEMP1. */
3803 if (GET_CODE (temp1) != ASHIFTRT
3804 || GET_CODE (XEXP (temp1, 0)) != ASHIFT
3805 || XEXP (XEXP (temp1, 0), 0) != temp)
3806 return temp1;
3808 break;
3810 case TRUNCATE:
3811 /* We can't handle truncation to a partial integer mode here
3812 because we don't know the real bitsize of the partial
3813 integer mode. */
3814 if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
3815 break;
3817 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3818 && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
3819 GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))))
3820 SUBST (XEXP (x, 0),
3821 force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
3822 GET_MODE_MASK (mode), NULL_RTX, 0));
3824 /* (truncate:SI ({sign,zero}_extend:DI foo:SI)) == foo:SI. */
3825 if ((GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
3826 || GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
3827 && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode)
3828 return XEXP (XEXP (x, 0), 0);
3830 /* (truncate:SI (OP:DI ({sign,zero}_extend:DI foo:SI))) is
3831 (OP:SI foo:SI) if OP is NEG or ABS. */
3832 if ((GET_CODE (XEXP (x, 0)) == ABS
3833 || GET_CODE (XEXP (x, 0)) == NEG)
3834 && (GET_CODE (XEXP (XEXP (x, 0), 0)) == SIGN_EXTEND
3835 || GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND)
3836 && GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == mode)
3837 return gen_unary (GET_CODE (XEXP (x, 0)), mode, mode,
3838 XEXP (XEXP (XEXP (x, 0), 0), 0));
3840 /* (truncate:SI (subreg:DI (truncate:SI X) 0)) is
3841 (truncate:SI x). */
3842 if (GET_CODE (XEXP (x, 0)) == SUBREG
3843 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == TRUNCATE
3844 && subreg_lowpart_p (XEXP (x, 0)))
3845 return SUBREG_REG (XEXP (x, 0));
3847 /* If we know that the value is already truncated, we can
3848 replace the TRUNCATE with a SUBREG if TRULY_NOOP_TRUNCATION is
3849 nonzero for the corresponding modes. */
3850 if (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
3851 GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
3852 && num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
3853 >= GET_MODE_BITSIZE (mode) + 1)
3854 return gen_lowpart_for_combine (mode, XEXP (x, 0));
3856 /* A truncate of a comparison can be replaced with a subreg if
3857 STORE_FLAG_VALUE permits. This is like the previous test,
3858 but it works even if the comparison is done in a mode larger
3859 than HOST_BITS_PER_WIDE_INT. */
3860 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3861 && GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
3862 && ((HOST_WIDE_INT) STORE_FLAG_VALUE &~ GET_MODE_MASK (mode)) == 0)
3863 return gen_lowpart_for_combine (mode, XEXP (x, 0));
3865 /* Similarly, a truncate of a register whose value is a
3866 comparison can be replaced with a subreg if STORE_FLAG_VALUE
3867 permits. */
3868 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3869 && ((HOST_WIDE_INT) STORE_FLAG_VALUE &~ GET_MODE_MASK (mode)) == 0
3870 && (temp = get_last_value (XEXP (x, 0)))
3871 && GET_RTX_CLASS (GET_CODE (temp)) == '<')
3872 return gen_lowpart_for_combine (mode, XEXP (x, 0));
3874 break;
3876 case FLOAT_TRUNCATE:
3877 /* (float_truncate:SF (float_extend:DF foo:SF)) = foo:SF. */
3878 if (GET_CODE (XEXP (x, 0)) == FLOAT_EXTEND
3879 && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode)
3880 return XEXP (XEXP (x, 0), 0);
3882 /* (float_truncate:SF (OP:DF (float_extend:DF foo:sf))) is
3883 (OP:SF foo:SF) if OP is NEG or ABS. */
3884 if ((GET_CODE (XEXP (x, 0)) == ABS
3885 || GET_CODE (XEXP (x, 0)) == NEG)
3886 && GET_CODE (XEXP (XEXP (x, 0), 0)) == FLOAT_EXTEND
3887 && GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == mode)
3888 return gen_unary (GET_CODE (XEXP (x, 0)), mode, mode,
3889 XEXP (XEXP (XEXP (x, 0), 0), 0));
3891 /* (float_truncate:SF (subreg:DF (float_truncate:SF X) 0))
3892 is (float_truncate:SF x). */
3893 if (GET_CODE (XEXP (x, 0)) == SUBREG
3894 && subreg_lowpart_p (XEXP (x, 0))
3895 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == FLOAT_TRUNCATE)
3896 return SUBREG_REG (XEXP (x, 0));
3897 break;
3899 #ifdef HAVE_cc0
3900 case COMPARE:
3901 /* Convert (compare FOO (const_int 0)) to FOO unless we aren't
3902 using cc0, in which case we want to leave it as a COMPARE
3903 so we can distinguish it from a register-register-copy. */
3904 if (XEXP (x, 1) == const0_rtx)
3905 return XEXP (x, 0);
3907 /* In IEEE floating point, x-0 is not the same as x. */
3908 if ((TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
3909 || ! FLOAT_MODE_P (GET_MODE (XEXP (x, 0)))
3910 || flag_fast_math)
3911 && XEXP (x, 1) == CONST0_RTX (GET_MODE (XEXP (x, 0))))
3912 return XEXP (x, 0);
3913 break;
3914 #endif
3916 case CONST:
3917 /* (const (const X)) can become (const X). Do it this way rather than
3918 returning the inner CONST since CONST can be shared with a
3919 REG_EQUAL note. */
3920 if (GET_CODE (XEXP (x, 0)) == CONST)
3921 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
3922 break;
3924 #ifdef HAVE_lo_sum
3925 case LO_SUM:
3926 /* Convert (lo_sum (high FOO) FOO) to FOO. This is necessary so we
3927 can add in an offset. find_split_point will split this address up
3928 again if it doesn't match. */
3929 if (GET_CODE (XEXP (x, 0)) == HIGH
3930 && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
3931 return XEXP (x, 1);
3932 break;
3933 #endif
3935 case PLUS:
3936 /* If we have (plus (plus (A const) B)), associate it so that CONST is
3937 outermost. That's because that's the way indexed addresses are
3938 supposed to appear. This code used to check many more cases, but
3939 they are now checked elsewhere. */
3940 if (GET_CODE (XEXP (x, 0)) == PLUS
3941 && CONSTANT_ADDRESS_P (XEXP (XEXP (x, 0), 1)))
3942 return gen_binary (PLUS, mode,
3943 gen_binary (PLUS, mode, XEXP (XEXP (x, 0), 0),
3944 XEXP (x, 1)),
3945 XEXP (XEXP (x, 0), 1));
3947 /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
3948 when c is (const_int (pow2 + 1) / 2) is a sign extension of a
3949 bit-field and can be replaced by either a sign_extend or a
3950 sign_extract. The `and' may be a zero_extend and the two
3951 <c>, -<c> constants may be reversed. */
3952 if (GET_CODE (XEXP (x, 0)) == XOR
3953 && GET_CODE (XEXP (x, 1)) == CONST_INT
3954 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3955 && INTVAL (XEXP (x, 1)) == - INTVAL (XEXP (XEXP (x, 0), 1))
3956 && ((i = exact_log2 (INTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
3957 || (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
3958 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3959 && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
3960 && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
3961 && (INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
3962 == ((HOST_WIDE_INT) 1 << (i + 1)) - 1))
3963 || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
3964 && (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
3965 == i + 1))))
3966 return simplify_shift_const
3967 (NULL_RTX, ASHIFTRT, mode,
3968 simplify_shift_const (NULL_RTX, ASHIFT, mode,
3969 XEXP (XEXP (XEXP (x, 0), 0), 0),
3970 GET_MODE_BITSIZE (mode) - (i + 1)),
3971 GET_MODE_BITSIZE (mode) - (i + 1));
3973 /* (plus (comparison A B) C) can become (neg (rev-comp A B)) if
3974 C is 1 and STORE_FLAG_VALUE is -1 or if C is -1 and STORE_FLAG_VALUE
3975 is 1. This produces better code than the alternative immediately
3976 below. */
3977 if (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
3978 && reversible_comparison_p (XEXP (x, 0))
3979 && ((STORE_FLAG_VALUE == -1 && XEXP (x, 1) == const1_rtx)
3980 || (STORE_FLAG_VALUE == 1 && XEXP (x, 1) == constm1_rtx)))
3981 return
3982 gen_unary (NEG, mode, mode,
3983 gen_binary (reverse_condition (GET_CODE (XEXP (x, 0))),
3984 mode, XEXP (XEXP (x, 0), 0),
3985 XEXP (XEXP (x, 0), 1)));
3987 /* If only the low-order bit of X is possibly nonzero, (plus x -1)
3988 can become (ashiftrt (ashift (xor x 1) C) C) where C is
3989 the bitsize of the mode - 1. This allows simplification of
3990 "a = (b & 8) == 0;" */
3991 if (XEXP (x, 1) == constm1_rtx
3992 && GET_CODE (XEXP (x, 0)) != REG
3993 && ! (GET_CODE (XEXP (x,0)) == SUBREG
3994 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG)
3995 && nonzero_bits (XEXP (x, 0), mode) == 1)
3996 return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
3997 simplify_shift_const (NULL_RTX, ASHIFT, mode,
3998 gen_rtx_combine (XOR, mode,
3999 XEXP (x, 0), const1_rtx),
4000 GET_MODE_BITSIZE (mode) - 1),
4001 GET_MODE_BITSIZE (mode) - 1);
4003 /* If we are adding two things that have no bits in common, convert
4004 the addition into an IOR. This will often be further simplified,
4005 for example in cases like ((a & 1) + (a & 2)), which can
4006 become a & 3. */
4008 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4009 && (nonzero_bits (XEXP (x, 0), mode)
4010 & nonzero_bits (XEXP (x, 1), mode)) == 0)
4011 return gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
4012 break;
4014 case MINUS:
4015 /* If STORE_FLAG_VALUE is 1, (minus 1 (comparison foo bar)) can be done
4016 by reversing the comparison code if valid. */
4017 if (STORE_FLAG_VALUE == 1
4018 && XEXP (x, 0) == const1_rtx
4019 && GET_RTX_CLASS (GET_CODE (XEXP (x, 1))) == '<'
4020 && reversible_comparison_p (XEXP (x, 1)))
4021 return gen_binary (reverse_condition (GET_CODE (XEXP (x, 1))),
4022 mode, XEXP (XEXP (x, 1), 0),
4023 XEXP (XEXP (x, 1), 1));
4025 /* (minus <foo> (and <foo> (const_int -pow2))) becomes
4026 (and <foo> (const_int pow2-1)) */
4027 if (GET_CODE (XEXP (x, 1)) == AND
4028 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
4029 && exact_log2 (- INTVAL (XEXP (XEXP (x, 1), 1))) >= 0
4030 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
4031 return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
4032 - INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
4034 /* Canonicalize (minus A (plus B C)) to (minus (minus A B) C) for
4035 integers. */
4036 if (GET_CODE (XEXP (x, 1)) == PLUS && INTEGRAL_MODE_P (mode))
4037 return gen_binary (MINUS, mode,
4038 gen_binary (MINUS, mode, XEXP (x, 0),
4039 XEXP (XEXP (x, 1), 0)),
4040 XEXP (XEXP (x, 1), 1));
4041 break;
4043 case MULT:
4044 /* If we have (mult (plus A B) C), apply the distributive law and then
4045 the inverse distributive law to see if things simplify. This
4046 occurs mostly in addresses, often when unrolling loops. */
4048 if (GET_CODE (XEXP (x, 0)) == PLUS)
4050 x = apply_distributive_law
4051 (gen_binary (PLUS, mode,
4052 gen_binary (MULT, mode,
4053 XEXP (XEXP (x, 0), 0), XEXP (x, 1)),
4054 gen_binary (MULT, mode,
4055 XEXP (XEXP (x, 0), 1), XEXP (x, 1))));
4057 if (GET_CODE (x) != MULT)
4058 return x;
4060 break;
4062 case UDIV:
4063 /* If this is a divide by a power of two, treat it as a shift if
4064 its first operand is a shift. */
4065 if (GET_CODE (XEXP (x, 1)) == CONST_INT
4066 && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0
4067 && (GET_CODE (XEXP (x, 0)) == ASHIFT
4068 || GET_CODE (XEXP (x, 0)) == LSHIFTRT
4069 || GET_CODE (XEXP (x, 0)) == ASHIFTRT
4070 || GET_CODE (XEXP (x, 0)) == ROTATE
4071 || GET_CODE (XEXP (x, 0)) == ROTATERT))
4072 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
4073 break;
4075 case EQ: case NE:
4076 case GT: case GTU: case GE: case GEU:
4077 case LT: case LTU: case LE: case LEU:
4078 /* If the first operand is a condition code, we can't do anything
4079 with it. */
4080 if (GET_CODE (XEXP (x, 0)) == COMPARE
4081 || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
4082 #ifdef HAVE_cc0
4083 && XEXP (x, 0) != cc0_rtx
4084 #endif
4087 rtx op0 = XEXP (x, 0);
4088 rtx op1 = XEXP (x, 1);
4089 enum rtx_code new_code;
4091 if (GET_CODE (op0) == COMPARE)
4092 op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
4094 /* Simplify our comparison, if possible. */
4095 new_code = simplify_comparison (code, &op0, &op1);
4097 /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
4098 if only the low-order bit is possibly nonzero in X (such as when
4099 X is a ZERO_EXTRACT of one bit). Similarly, we can convert EQ to
4100 (xor X 1) or (minus 1 X); we use the former. Finally, if X is
4101 known to be either 0 or -1, NE becomes a NEG and EQ becomes
4102 (plus X 1).
4104 Remove any ZERO_EXTRACT we made when thinking this was a
4105 comparison. It may now be simpler to use, e.g., an AND. If a
4106 ZERO_EXTRACT is indeed appropriate, it will be placed back by
4107 the call to make_compound_operation in the SET case. */
4109 if (STORE_FLAG_VALUE == 1
4110 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4111 && op1 == const0_rtx && nonzero_bits (op0, mode) == 1)
4112 return gen_lowpart_for_combine (mode,
4113 expand_compound_operation (op0));
4115 else if (STORE_FLAG_VALUE == 1
4116 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4117 && op1 == const0_rtx
4118 && (num_sign_bit_copies (op0, mode)
4119 == GET_MODE_BITSIZE (mode)))
4121 op0 = expand_compound_operation (op0);
4122 return gen_unary (NEG, mode, mode,
4123 gen_lowpart_for_combine (mode, op0));
4126 else if (STORE_FLAG_VALUE == 1
4127 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4128 && op1 == const0_rtx
4129 && nonzero_bits (op0, mode) == 1)
4131 op0 = expand_compound_operation (op0);
4132 return gen_binary (XOR, mode,
4133 gen_lowpart_for_combine (mode, op0),
4134 const1_rtx);
4137 else if (STORE_FLAG_VALUE == 1
4138 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4139 && op1 == const0_rtx
4140 && (num_sign_bit_copies (op0, mode)
4141 == GET_MODE_BITSIZE (mode)))
4143 op0 = expand_compound_operation (op0);
4144 return plus_constant (gen_lowpart_for_combine (mode, op0), 1);
4147 /* If STORE_FLAG_VALUE is -1, we have cases similar to
4148 those above. */
4149 if (STORE_FLAG_VALUE == -1
4150 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4151 && op1 == const0_rtx
4152 && (num_sign_bit_copies (op0, mode)
4153 == GET_MODE_BITSIZE (mode)))
4154 return gen_lowpart_for_combine (mode,
4155 expand_compound_operation (op0));
4157 else if (STORE_FLAG_VALUE == -1
4158 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4159 && op1 == const0_rtx
4160 && nonzero_bits (op0, mode) == 1)
4162 op0 = expand_compound_operation (op0);
4163 return gen_unary (NEG, mode, mode,
4164 gen_lowpart_for_combine (mode, op0));
4167 else if (STORE_FLAG_VALUE == -1
4168 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4169 && op1 == const0_rtx
4170 && (num_sign_bit_copies (op0, mode)
4171 == GET_MODE_BITSIZE (mode)))
4173 op0 = expand_compound_operation (op0);
4174 return gen_unary (NOT, mode, mode,
4175 gen_lowpart_for_combine (mode, op0));
4178 /* If X is 0/1, (eq X 0) is X-1. */
4179 else if (STORE_FLAG_VALUE == -1
4180 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4181 && op1 == const0_rtx
4182 && nonzero_bits (op0, mode) == 1)
4184 op0 = expand_compound_operation (op0);
4185 return plus_constant (gen_lowpart_for_combine (mode, op0), -1);
4188 /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
4189 one bit that might be nonzero, we can convert (ne x 0) to
4190 (ashift x c) where C puts the bit in the sign bit. Remove any
4191 AND with STORE_FLAG_VALUE when we are done, since we are only
4192 going to test the sign bit. */
4193 if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4194 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4195 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
4196 == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE(mode)-1))
4197 && op1 == const0_rtx
4198 && mode == GET_MODE (op0)
4199 && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
4201 x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
4202 expand_compound_operation (op0),
4203 GET_MODE_BITSIZE (mode) - 1 - i);
4204 if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
4205 return XEXP (x, 0);
4206 else
4207 return x;
4210 /* If the code changed, return a whole new comparison. */
4211 if (new_code != code)
4212 return gen_rtx_combine (new_code, mode, op0, op1);
4214 /* Otherwise, keep this operation, but maybe change its operands.
4215 This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR). */
4216 SUBST (XEXP (x, 0), op0);
4217 SUBST (XEXP (x, 1), op1);
4219 break;
4221 case IF_THEN_ELSE:
4222 return simplify_if_then_else (x);
4224 case ZERO_EXTRACT:
4225 case SIGN_EXTRACT:
4226 case ZERO_EXTEND:
4227 case SIGN_EXTEND:
4228 /* If we are processing SET_DEST, we are done. */
4229 if (in_dest)
4230 return x;
4232 return expand_compound_operation (x);
4234 case SET:
4235 return simplify_set (x);
4237 case AND:
4238 case IOR:
4239 case XOR:
4240 return simplify_logical (x, last);
4242 case ABS:
4243 /* (abs (neg <foo>)) -> (abs <foo>) */
4244 if (GET_CODE (XEXP (x, 0)) == NEG)
4245 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4247 /* If the mode of the operand is VOIDmode (i.e. if it is ASM_OPERANDS),
4248 do nothing. */
4249 if (GET_MODE (XEXP (x, 0)) == VOIDmode)
4250 break;
4252 /* If operand is something known to be positive, ignore the ABS. */
4253 if (GET_CODE (XEXP (x, 0)) == FFS || GET_CODE (XEXP (x, 0)) == ABS
4254 || ((GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
4255 <= HOST_BITS_PER_WIDE_INT)
4256 && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
4257 & ((HOST_WIDE_INT) 1
4258 << (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - 1)))
4259 == 0)))
4260 return XEXP (x, 0);
4263 /* If operand is known to be only -1 or 0, convert ABS to NEG. */
4264 if (num_sign_bit_copies (XEXP (x, 0), mode) == GET_MODE_BITSIZE (mode))
4265 return gen_rtx_combine (NEG, mode, XEXP (x, 0));
4267 break;
4269 case FFS:
4270 /* (ffs (*_extend <X>)) = (ffs <X>) */
4271 if (GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
4272 || GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
4273 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4274 break;
4276 case FLOAT:
4277 /* (float (sign_extend <X>)) = (float <X>). */
4278 if (GET_CODE (XEXP (x, 0)) == SIGN_EXTEND)
4279 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4280 break;
4282 case ASHIFT:
4283 case LSHIFTRT:
4284 case ASHIFTRT:
4285 case ROTATE:
4286 case ROTATERT:
4287 /* If this is a shift by a constant amount, simplify it. */
4288 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4289 return simplify_shift_const (x, code, mode, XEXP (x, 0),
4290 INTVAL (XEXP (x, 1)));
4292 #ifdef SHIFT_COUNT_TRUNCATED
4293 else if (SHIFT_COUNT_TRUNCATED && GET_CODE (XEXP (x, 1)) != REG)
4294 SUBST (XEXP (x, 1),
4295 force_to_mode (XEXP (x, 1), GET_MODE (x),
4296 ((HOST_WIDE_INT) 1
4297 << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
4298 - 1,
4299 NULL_RTX, 0));
4300 #endif
4302 break;
4304 default:
4305 break;
4308 return x;
4311 /* Simplify X, an IF_THEN_ELSE expression. Return the new expression. */
4313 static rtx
4314 simplify_if_then_else (x)
4315 rtx x;
4317 enum machine_mode mode = GET_MODE (x);
4318 rtx cond = XEXP (x, 0);
4319 rtx true = XEXP (x, 1);
4320 rtx false = XEXP (x, 2);
4321 enum rtx_code true_code = GET_CODE (cond);
4322 int comparison_p = GET_RTX_CLASS (true_code) == '<';
4323 rtx temp;
4324 int i;
4326 /* Simplify storing of the truth value. */
4327 if (comparison_p && true == const_true_rtx && false == const0_rtx)
4328 return gen_binary (true_code, mode, XEXP (cond, 0), XEXP (cond, 1));
4330 /* Also when the truth value has to be reversed. */
4331 if (comparison_p && reversible_comparison_p (cond)
4332 && true == const0_rtx && false == const_true_rtx)
4333 return gen_binary (reverse_condition (true_code),
4334 mode, XEXP (cond, 0), XEXP (cond, 1));
4336 /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
4337 in it is being compared against certain values. Get the true and false
4338 comparisons and see if that says anything about the value of each arm. */
4340 if (comparison_p && reversible_comparison_p (cond)
4341 && GET_CODE (XEXP (cond, 0)) == REG)
4343 HOST_WIDE_INT nzb;
4344 rtx from = XEXP (cond, 0);
4345 enum rtx_code false_code = reverse_condition (true_code);
4346 rtx true_val = XEXP (cond, 1);
4347 rtx false_val = true_val;
4348 int swapped = 0;
4350 /* If FALSE_CODE is EQ, swap the codes and arms. */
4352 if (false_code == EQ)
4354 swapped = 1, true_code = EQ, false_code = NE;
4355 temp = true, true = false, false = temp;
4358 /* If we are comparing against zero and the expression being tested has
4359 only a single bit that might be nonzero, that is its value when it is
4360 not equal to zero. Similarly if it is known to be -1 or 0. */
4362 if (true_code == EQ && true_val == const0_rtx
4363 && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
4364 false_code = EQ, false_val = GEN_INT (nzb);
4365 else if (true_code == EQ && true_val == const0_rtx
4366 && (num_sign_bit_copies (from, GET_MODE (from))
4367 == GET_MODE_BITSIZE (GET_MODE (from))))
4368 false_code = EQ, false_val = constm1_rtx;
4370 /* Now simplify an arm if we know the value of the register in the
4371 branch and it is used in the arm. Be careful due to the potential
4372 of locally-shared RTL. */
4374 if (reg_mentioned_p (from, true))
4375 true = subst (known_cond (copy_rtx (true), true_code, from, true_val),
4376 pc_rtx, pc_rtx, 0, 0);
4377 if (reg_mentioned_p (from, false))
4378 false = subst (known_cond (copy_rtx (false), false_code,
4379 from, false_val),
4380 pc_rtx, pc_rtx, 0, 0);
4382 SUBST (XEXP (x, 1), swapped ? false : true);
4383 SUBST (XEXP (x, 2), swapped ? true : false);
4385 true = XEXP (x, 1), false = XEXP (x, 2), true_code = GET_CODE (cond);
4388 /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
4389 reversed, do so to avoid needing two sets of patterns for
4390 subtract-and-branch insns. Similarly if we have a constant in the true
4391 arm, the false arm is the same as the first operand of the comparison, or
4392 the false arm is more complicated than the true arm. */
4394 if (comparison_p && reversible_comparison_p (cond)
4395 && (true == pc_rtx
4396 || (CONSTANT_P (true)
4397 && GET_CODE (false) != CONST_INT && false != pc_rtx)
4398 || true == const0_rtx
4399 || (GET_RTX_CLASS (GET_CODE (true)) == 'o'
4400 && GET_RTX_CLASS (GET_CODE (false)) != 'o')
4401 || (GET_CODE (true) == SUBREG
4402 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (true))) == 'o'
4403 && GET_RTX_CLASS (GET_CODE (false)) != 'o')
4404 || reg_mentioned_p (true, false)
4405 || rtx_equal_p (false, XEXP (cond, 0))))
4407 true_code = reverse_condition (true_code);
4408 SUBST (XEXP (x, 0),
4409 gen_binary (true_code, GET_MODE (cond), XEXP (cond, 0),
4410 XEXP (cond, 1)));
4412 SUBST (XEXP (x, 1), false);
4413 SUBST (XEXP (x, 2), true);
4415 temp = true, true = false, false = temp, cond = XEXP (x, 0);
4417 /* It is possible that the conditional has been simplified out. */
4418 true_code = GET_CODE (cond);
4419 comparison_p = GET_RTX_CLASS (true_code) == '<';
4422 /* If the two arms are identical, we don't need the comparison. */
4424 if (rtx_equal_p (true, false) && ! side_effects_p (cond))
4425 return true;
4427 /* Convert a == b ? b : a to "a". */
4428 if (true_code == EQ && ! side_effects_p (cond)
4429 && rtx_equal_p (XEXP (cond, 0), false)
4430 && rtx_equal_p (XEXP (cond, 1), true))
4431 return false;
4432 else if (true_code == NE && ! side_effects_p (cond)
4433 && rtx_equal_p (XEXP (cond, 0), true)
4434 && rtx_equal_p (XEXP (cond, 1), false))
4435 return true;
4437 /* Look for cases where we have (abs x) or (neg (abs X)). */
4439 if (GET_MODE_CLASS (mode) == MODE_INT
4440 && GET_CODE (false) == NEG
4441 && rtx_equal_p (true, XEXP (false, 0))
4442 && comparison_p
4443 && rtx_equal_p (true, XEXP (cond, 0))
4444 && ! side_effects_p (true))
4445 switch (true_code)
4447 case GT:
4448 case GE:
4449 return gen_unary (ABS, mode, mode, true);
4450 case LT:
4451 case LE:
4452 return gen_unary (NEG, mode, mode, gen_unary (ABS, mode, mode, true));
4453 default:
4454 break;
4457 /* Look for MIN or MAX. */
4459 if ((! FLOAT_MODE_P (mode) || flag_fast_math)
4460 && comparison_p
4461 && rtx_equal_p (XEXP (cond, 0), true)
4462 && rtx_equal_p (XEXP (cond, 1), false)
4463 && ! side_effects_p (cond))
4464 switch (true_code)
4466 case GE:
4467 case GT:
4468 return gen_binary (SMAX, mode, true, false);
4469 case LE:
4470 case LT:
4471 return gen_binary (SMIN, mode, true, false);
4472 case GEU:
4473 case GTU:
4474 return gen_binary (UMAX, mode, true, false);
4475 case LEU:
4476 case LTU:
4477 return gen_binary (UMIN, mode, true, false);
4478 default:
4479 break;
4482 /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
4483 second operand is zero, this can be done as (OP Z (mult COND C2)) where
4484 C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
4485 SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
4486 We can do this kind of thing in some cases when STORE_FLAG_VALUE is
4487 neither 1 or -1, but it isn't worth checking for. */
4489 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
4490 && comparison_p && mode != VOIDmode && ! side_effects_p (x))
4492 rtx t = make_compound_operation (true, SET);
4493 rtx f = make_compound_operation (false, SET);
4494 rtx cond_op0 = XEXP (cond, 0);
4495 rtx cond_op1 = XEXP (cond, 1);
4496 enum rtx_code op = NIL, extend_op = NIL;
4497 enum machine_mode m = mode;
4498 rtx z = 0, c1 = NULL_RTX;
4500 if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
4501 || GET_CODE (t) == IOR || GET_CODE (t) == XOR
4502 || GET_CODE (t) == ASHIFT
4503 || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
4504 && rtx_equal_p (XEXP (t, 0), f))
4505 c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
4507 /* If an identity-zero op is commutative, check whether there
4508 would be a match if we swapped the operands. */
4509 else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
4510 || GET_CODE (t) == XOR)
4511 && rtx_equal_p (XEXP (t, 1), f))
4512 c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
4513 else if (GET_CODE (t) == SIGN_EXTEND
4514 && (GET_CODE (XEXP (t, 0)) == PLUS
4515 || GET_CODE (XEXP (t, 0)) == MINUS
4516 || GET_CODE (XEXP (t, 0)) == IOR
4517 || GET_CODE (XEXP (t, 0)) == XOR
4518 || GET_CODE (XEXP (t, 0)) == ASHIFT
4519 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
4520 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
4521 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
4522 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
4523 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
4524 && (num_sign_bit_copies (f, GET_MODE (f))
4525 > (GET_MODE_BITSIZE (mode)
4526 - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 0))))))
4528 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
4529 extend_op = SIGN_EXTEND;
4530 m = GET_MODE (XEXP (t, 0));
4532 else if (GET_CODE (t) == SIGN_EXTEND
4533 && (GET_CODE (XEXP (t, 0)) == PLUS
4534 || GET_CODE (XEXP (t, 0)) == IOR
4535 || GET_CODE (XEXP (t, 0)) == XOR)
4536 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
4537 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
4538 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
4539 && (num_sign_bit_copies (f, GET_MODE (f))
4540 > (GET_MODE_BITSIZE (mode)
4541 - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 1))))))
4543 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
4544 extend_op = SIGN_EXTEND;
4545 m = GET_MODE (XEXP (t, 0));
4547 else if (GET_CODE (t) == ZERO_EXTEND
4548 && (GET_CODE (XEXP (t, 0)) == PLUS
4549 || GET_CODE (XEXP (t, 0)) == MINUS
4550 || GET_CODE (XEXP (t, 0)) == IOR
4551 || GET_CODE (XEXP (t, 0)) == XOR
4552 || GET_CODE (XEXP (t, 0)) == ASHIFT
4553 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
4554 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
4555 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
4556 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4557 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
4558 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
4559 && ((nonzero_bits (f, GET_MODE (f))
4560 & ~ GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
4561 == 0))
4563 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
4564 extend_op = ZERO_EXTEND;
4565 m = GET_MODE (XEXP (t, 0));
4567 else if (GET_CODE (t) == ZERO_EXTEND
4568 && (GET_CODE (XEXP (t, 0)) == PLUS
4569 || GET_CODE (XEXP (t, 0)) == IOR
4570 || GET_CODE (XEXP (t, 0)) == XOR)
4571 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
4572 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4573 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
4574 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
4575 && ((nonzero_bits (f, GET_MODE (f))
4576 & ~ GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
4577 == 0))
4579 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
4580 extend_op = ZERO_EXTEND;
4581 m = GET_MODE (XEXP (t, 0));
4584 if (z)
4586 temp = subst (gen_binary (true_code, m, cond_op0, cond_op1),
4587 pc_rtx, pc_rtx, 0, 0);
4588 temp = gen_binary (MULT, m, temp,
4589 gen_binary (MULT, m, c1, const_true_rtx));
4590 temp = subst (temp, pc_rtx, pc_rtx, 0, 0);
4591 temp = gen_binary (op, m, gen_lowpart_for_combine (m, z), temp);
4593 if (extend_op != NIL)
4594 temp = gen_unary (extend_op, mode, m, temp);
4596 return temp;
4600 /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
4601 1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
4602 negation of a single bit, we can convert this operation to a shift. We
4603 can actually do this more generally, but it doesn't seem worth it. */
4605 if (true_code == NE && XEXP (cond, 1) == const0_rtx
4606 && false == const0_rtx && GET_CODE (true) == CONST_INT
4607 && ((1 == nonzero_bits (XEXP (cond, 0), mode)
4608 && (i = exact_log2 (INTVAL (true))) >= 0)
4609 || ((num_sign_bit_copies (XEXP (cond, 0), mode)
4610 == GET_MODE_BITSIZE (mode))
4611 && (i = exact_log2 (- INTVAL (true))) >= 0)))
4612 return
4613 simplify_shift_const (NULL_RTX, ASHIFT, mode,
4614 gen_lowpart_for_combine (mode, XEXP (cond, 0)), i);
4616 return x;
4619 /* Simplify X, a SET expression. Return the new expression. */
4621 static rtx
4622 simplify_set (x)
4623 rtx x;
4625 rtx src = SET_SRC (x);
4626 rtx dest = SET_DEST (x);
4627 enum machine_mode mode
4628 = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
4629 rtx other_insn;
4630 rtx *cc_use;
4632 /* (set (pc) (return)) gets written as (return). */
4633 if (GET_CODE (dest) == PC && GET_CODE (src) == RETURN)
4634 return src;
4636 /* Now that we know for sure which bits of SRC we are using, see if we can
4637 simplify the expression for the object knowing that we only need the
4638 low-order bits. */
4640 if (GET_MODE_CLASS (mode) == MODE_INT)
4642 src = force_to_mode (src, mode, GET_MODE_MASK (mode), NULL_RTX, 0);
4643 SUBST (SET_SRC (x), src);
4646 /* If we are setting CC0 or if the source is a COMPARE, look for the use of
4647 the comparison result and try to simplify it unless we already have used
4648 undobuf.other_insn. */
4649 if ((GET_CODE (src) == COMPARE
4650 #ifdef HAVE_cc0
4651 || dest == cc0_rtx
4652 #endif
4654 && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
4655 && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
4656 && GET_RTX_CLASS (GET_CODE (*cc_use)) == '<'
4657 && rtx_equal_p (XEXP (*cc_use, 0), dest))
4659 enum rtx_code old_code = GET_CODE (*cc_use);
4660 enum rtx_code new_code;
4661 rtx op0, op1;
4662 int other_changed = 0;
4663 enum machine_mode compare_mode = GET_MODE (dest);
4665 if (GET_CODE (src) == COMPARE)
4666 op0 = XEXP (src, 0), op1 = XEXP (src, 1);
4667 else
4668 op0 = src, op1 = const0_rtx;
4670 /* Simplify our comparison, if possible. */
4671 new_code = simplify_comparison (old_code, &op0, &op1);
4673 #ifdef EXTRA_CC_MODES
4674 /* If this machine has CC modes other than CCmode, check to see if we
4675 need to use a different CC mode here. */
4676 compare_mode = SELECT_CC_MODE (new_code, op0, op1);
4677 #endif /* EXTRA_CC_MODES */
4679 #if !defined (HAVE_cc0) && defined (EXTRA_CC_MODES)
4680 /* If the mode changed, we have to change SET_DEST, the mode in the
4681 compare, and the mode in the place SET_DEST is used. If SET_DEST is
4682 a hard register, just build new versions with the proper mode. If it
4683 is a pseudo, we lose unless it is only time we set the pseudo, in
4684 which case we can safely change its mode. */
4685 if (compare_mode != GET_MODE (dest))
4687 int regno = REGNO (dest);
4688 rtx new_dest = gen_rtx_REG (compare_mode, regno);
4690 if (regno < FIRST_PSEUDO_REGISTER
4691 || (REG_N_SETS (regno) == 1 && ! REG_USERVAR_P (dest)))
4693 if (regno >= FIRST_PSEUDO_REGISTER)
4694 SUBST (regno_reg_rtx[regno], new_dest);
4696 SUBST (SET_DEST (x), new_dest);
4697 SUBST (XEXP (*cc_use, 0), new_dest);
4698 other_changed = 1;
4700 dest = new_dest;
4703 #endif
4705 /* If the code changed, we have to build a new comparison in
4706 undobuf.other_insn. */
4707 if (new_code != old_code)
4709 unsigned HOST_WIDE_INT mask;
4711 SUBST (*cc_use, gen_rtx_combine (new_code, GET_MODE (*cc_use),
4712 dest, const0_rtx));
4714 /* If the only change we made was to change an EQ into an NE or
4715 vice versa, OP0 has only one bit that might be nonzero, and OP1
4716 is zero, check if changing the user of the condition code will
4717 produce a valid insn. If it won't, we can keep the original code
4718 in that insn by surrounding our operation with an XOR. */
4720 if (((old_code == NE && new_code == EQ)
4721 || (old_code == EQ && new_code == NE))
4722 && ! other_changed && op1 == const0_rtx
4723 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
4724 && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
4726 rtx pat = PATTERN (other_insn), note = 0;
4728 if ((recog_for_combine (&pat, other_insn, &note) < 0
4729 && ! check_asm_operands (pat)))
4731 PUT_CODE (*cc_use, old_code);
4732 other_insn = 0;
4734 op0 = gen_binary (XOR, GET_MODE (op0), op0, GEN_INT (mask));
4738 other_changed = 1;
4741 if (other_changed)
4742 undobuf.other_insn = other_insn;
4744 #ifdef HAVE_cc0
4745 /* If we are now comparing against zero, change our source if
4746 needed. If we do not use cc0, we always have a COMPARE. */
4747 if (op1 == const0_rtx && dest == cc0_rtx)
4749 SUBST (SET_SRC (x), op0);
4750 src = op0;
4752 else
4753 #endif
4755 /* Otherwise, if we didn't previously have a COMPARE in the
4756 correct mode, we need one. */
4757 if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
4759 SUBST (SET_SRC (x),
4760 gen_rtx_combine (COMPARE, compare_mode, op0, op1));
4761 src = SET_SRC (x);
4763 else
4765 /* Otherwise, update the COMPARE if needed. */
4766 SUBST (XEXP (src, 0), op0);
4767 SUBST (XEXP (src, 1), op1);
4770 else
4772 /* Get SET_SRC in a form where we have placed back any
4773 compound expressions. Then do the checks below. */
4774 src = make_compound_operation (src, SET);
4775 SUBST (SET_SRC (x), src);
4778 /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
4779 and X being a REG or (subreg (reg)), we may be able to convert this to
4780 (set (subreg:m2 x) (op)).
4782 We can always do this if M1 is narrower than M2 because that means that
4783 we only care about the low bits of the result.
4785 However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
4786 perform a narrower operation than requested since the high-order bits will
4787 be undefined. On machine where it is defined, this transformation is safe
4788 as long as M1 and M2 have the same number of words. */
4790 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
4791 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (src))) != 'o'
4792 && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
4793 / UNITS_PER_WORD)
4794 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
4795 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
4796 #ifndef WORD_REGISTER_OPERATIONS
4797 && (GET_MODE_SIZE (GET_MODE (src))
4798 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
4799 #endif
4800 #ifdef CLASS_CANNOT_CHANGE_SIZE
4801 && ! (GET_CODE (dest) == REG && REGNO (dest) < FIRST_PSEUDO_REGISTER
4802 && (TEST_HARD_REG_BIT
4803 (reg_class_contents[(int) CLASS_CANNOT_CHANGE_SIZE],
4804 REGNO (dest)))
4805 && (GET_MODE_SIZE (GET_MODE (src))
4806 != GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))))
4807 #endif
4808 && (GET_CODE (dest) == REG
4809 || (GET_CODE (dest) == SUBREG
4810 && GET_CODE (SUBREG_REG (dest)) == REG)))
4812 SUBST (SET_DEST (x),
4813 gen_lowpart_for_combine (GET_MODE (SUBREG_REG (src)),
4814 dest));
4815 SUBST (SET_SRC (x), SUBREG_REG (src));
4817 src = SET_SRC (x), dest = SET_DEST (x);
4820 #ifdef LOAD_EXTEND_OP
4821 /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
4822 would require a paradoxical subreg. Replace the subreg with a
4823 zero_extend to avoid the reload that would otherwise be required. */
4825 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
4826 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != NIL
4827 && SUBREG_WORD (src) == 0
4828 && (GET_MODE_SIZE (GET_MODE (src))
4829 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
4830 && GET_CODE (SUBREG_REG (src)) == MEM)
4832 SUBST (SET_SRC (x),
4833 gen_rtx_combine (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
4834 GET_MODE (src), XEXP (src, 0)));
4836 src = SET_SRC (x);
4838 #endif
4840 /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
4841 are comparing an item known to be 0 or -1 against 0, use a logical
4842 operation instead. Check for one of the arms being an IOR of the other
4843 arm with some value. We compute three terms to be IOR'ed together. In
4844 practice, at most two will be nonzero. Then we do the IOR's. */
4846 if (GET_CODE (dest) != PC
4847 && GET_CODE (src) == IF_THEN_ELSE
4848 && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
4849 && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
4850 && XEXP (XEXP (src, 0), 1) == const0_rtx
4851 && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
4852 #ifdef HAVE_conditional_move
4853 && ! can_conditionally_move_p (GET_MODE (src))
4854 #endif
4855 && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
4856 GET_MODE (XEXP (XEXP (src, 0), 0)))
4857 == GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (src, 0), 0))))
4858 && ! side_effects_p (src))
4860 rtx true = (GET_CODE (XEXP (src, 0)) == NE
4861 ? XEXP (src, 1) : XEXP (src, 2));
4862 rtx false = (GET_CODE (XEXP (src, 0)) == NE
4863 ? XEXP (src, 2) : XEXP (src, 1));
4864 rtx term1 = const0_rtx, term2, term3;
4866 if (GET_CODE (true) == IOR && rtx_equal_p (XEXP (true, 0), false))
4867 term1 = false, true = XEXP (true, 1), false = const0_rtx;
4868 else if (GET_CODE (true) == IOR
4869 && rtx_equal_p (XEXP (true, 1), false))
4870 term1 = false, true = XEXP (true, 0), false = const0_rtx;
4871 else if (GET_CODE (false) == IOR
4872 && rtx_equal_p (XEXP (false, 0), true))
4873 term1 = true, false = XEXP (false, 1), true = const0_rtx;
4874 else if (GET_CODE (false) == IOR
4875 && rtx_equal_p (XEXP (false, 1), true))
4876 term1 = true, false = XEXP (false, 0), true = const0_rtx;
4878 term2 = gen_binary (AND, GET_MODE (src), XEXP (XEXP (src, 0), 0), true);
4879 term3 = gen_binary (AND, GET_MODE (src),
4880 gen_unary (NOT, GET_MODE (src), GET_MODE (src),
4881 XEXP (XEXP (src, 0), 0)),
4882 false);
4884 SUBST (SET_SRC (x),
4885 gen_binary (IOR, GET_MODE (src),
4886 gen_binary (IOR, GET_MODE (src), term1, term2),
4887 term3));
4889 src = SET_SRC (x);
4892 #ifdef HAVE_conditional_arithmetic
4893 /* If we have conditional arithmetic and the operand of a SET is
4894 a conditional expression, replace this with an IF_THEN_ELSE.
4895 We can either have a conditional expression or a MULT of that expression
4896 with a constant. */
4897 if ((GET_RTX_CLASS (GET_CODE (src)) == '1'
4898 || GET_RTX_CLASS (GET_CODE (src)) == '2'
4899 || GET_RTX_CLASS (GET_CODE (src)) == 'c')
4900 && (GET_RTX_CLASS (GET_CODE (XEXP (src, 0))) == '<'
4901 || (GET_CODE (XEXP (src, 0)) == MULT
4902 && GET_RTX_CLASS (GET_CODE (XEXP (XEXP (src, 0), 0))) == '<'
4903 && GET_CODE (XEXP (XEXP (src, 0), 1)) == CONST_INT)))
4905 rtx cond = XEXP (src, 0);
4906 rtx true_val = const1_rtx;
4907 rtx false_arm, true_arm;
4909 if (GET_CODE (cond) == MULT)
4911 true_val = XEXP (cond, 1);
4912 cond = XEXP (cond, 0);
4915 if (GET_RTX_CLASS (GET_CODE (src)) == '1')
4917 true_arm = gen_unary (GET_CODE (src), GET_MODE (src),
4918 GET_MODE (XEXP (src, 0)), true_val);
4919 false_arm = gen_unary (GET_CODE (src), GET_MODE (src),
4920 GET_MODE (XEXP (src, 0)), const0_rtx);
4922 else
4924 true_arm = gen_binary (GET_CODE (src), GET_MODE (src),
4925 true_val, XEXP (src, 1));
4926 false_arm = gen_binary (GET_CODE (src), GET_MODE (src),
4927 const0_rtx, XEXP (src, 1));
4930 /* Canonicalize if true_arm is the simpler one. */
4931 if (GET_RTX_CLASS (GET_CODE (true_arm)) == 'o'
4932 && GET_RTX_CLASS (GET_CODE (false_arm)) != 'o'
4933 && reversible_comparison_p (cond))
4935 rtx temp = true_arm;
4937 true_arm = false_arm;
4938 false_arm = temp;
4940 cond = gen_rtx_combine (reverse_condition (GET_CODE (cond)),
4941 GET_MODE (cond), XEXP (cond, 0),
4942 XEXP (cond, 1));
4945 src = gen_rtx_combine (IF_THEN_ELSE, GET_MODE (src),
4946 gen_rtx_combine (GET_CODE (cond), VOIDmode,
4947 XEXP (cond, 0),
4948 XEXP (cond, 1)),
4949 true_arm, false_arm);
4950 SUBST (SET_SRC (x), src);
4952 #endif
4954 /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
4955 whole thing fail. */
4956 if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
4957 return src;
4958 else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
4959 return dest;
4960 else
4961 /* Convert this into a field assignment operation, if possible. */
4962 return make_field_assignment (x);
4965 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
4966 result. LAST is nonzero if this is the last retry. */
4968 static rtx
4969 simplify_logical (x, last)
4970 rtx x;
4971 int last;
4973 enum machine_mode mode = GET_MODE (x);
4974 rtx op0 = XEXP (x, 0);
4975 rtx op1 = XEXP (x, 1);
4977 switch (GET_CODE (x))
4979 case AND:
4980 /* Convert (A ^ B) & A to A & (~ B) since the latter is often a single
4981 insn (and may simplify more). */
4982 if (GET_CODE (op0) == XOR
4983 && rtx_equal_p (XEXP (op0, 0), op1)
4984 && ! side_effects_p (op1))
4985 x = gen_binary (AND, mode,
4986 gen_unary (NOT, mode, mode, XEXP (op0, 1)), op1);
4988 if (GET_CODE (op0) == XOR
4989 && rtx_equal_p (XEXP (op0, 1), op1)
4990 && ! side_effects_p (op1))
4991 x = gen_binary (AND, mode,
4992 gen_unary (NOT, mode, mode, XEXP (op0, 0)), op1);
4994 /* Similarly for (~ (A ^ B)) & A. */
4995 if (GET_CODE (op0) == NOT
4996 && GET_CODE (XEXP (op0, 0)) == XOR
4997 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), op1)
4998 && ! side_effects_p (op1))
4999 x = gen_binary (AND, mode, XEXP (XEXP (op0, 0), 1), op1);
5001 if (GET_CODE (op0) == NOT
5002 && GET_CODE (XEXP (op0, 0)) == XOR
5003 && rtx_equal_p (XEXP (XEXP (op0, 0), 1), op1)
5004 && ! side_effects_p (op1))
5005 x = gen_binary (AND, mode, XEXP (XEXP (op0, 0), 0), op1);
5007 /* We can call simplify_and_const_int only if we don't lose
5008 any (sign) bits when converting INTVAL (op1) to
5009 "unsigned HOST_WIDE_INT". */
5010 if (GET_CODE (op1) == CONST_INT
5011 && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5012 || INTVAL (op1) > 0))
5014 x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
5016 /* If we have (ior (and (X C1) C2)) and the next restart would be
5017 the last, simplify this by making C1 as small as possible
5018 and then exit. */
5019 if (last
5020 && GET_CODE (x) == IOR && GET_CODE (op0) == AND
5021 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5022 && GET_CODE (op1) == CONST_INT)
5023 return gen_binary (IOR, mode,
5024 gen_binary (AND, mode, XEXP (op0, 0),
5025 GEN_INT (INTVAL (XEXP (op0, 1))
5026 & ~ INTVAL (op1))), op1);
5028 if (GET_CODE (x) != AND)
5029 return x;
5031 if (GET_RTX_CLASS (GET_CODE (x)) == 'c'
5032 || GET_RTX_CLASS (GET_CODE (x)) == '2')
5033 op0 = XEXP (x, 0), op1 = XEXP (x, 1);
5036 /* Convert (A | B) & A to A. */
5037 if (GET_CODE (op0) == IOR
5038 && (rtx_equal_p (XEXP (op0, 0), op1)
5039 || rtx_equal_p (XEXP (op0, 1), op1))
5040 && ! side_effects_p (XEXP (op0, 0))
5041 && ! side_effects_p (XEXP (op0, 1)))
5042 return op1;
5044 /* In the following group of tests (and those in case IOR below),
5045 we start with some combination of logical operations and apply
5046 the distributive law followed by the inverse distributive law.
5047 Most of the time, this results in no change. However, if some of
5048 the operands are the same or inverses of each other, simplifications
5049 will result.
5051 For example, (and (ior A B) (not B)) can occur as the result of
5052 expanding a bit field assignment. When we apply the distributive
5053 law to this, we get (ior (and (A (not B))) (and (B (not B)))),
5054 which then simplifies to (and (A (not B))).
5056 If we have (and (ior A B) C), apply the distributive law and then
5057 the inverse distributive law to see if things simplify. */
5059 if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
5061 x = apply_distributive_law
5062 (gen_binary (GET_CODE (op0), mode,
5063 gen_binary (AND, mode, XEXP (op0, 0), op1),
5064 gen_binary (AND, mode, XEXP (op0, 1), op1)));
5065 if (GET_CODE (x) != AND)
5066 return x;
5069 if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
5070 return apply_distributive_law
5071 (gen_binary (GET_CODE (op1), mode,
5072 gen_binary (AND, mode, XEXP (op1, 0), op0),
5073 gen_binary (AND, mode, XEXP (op1, 1), op0)));
5075 /* Similarly, taking advantage of the fact that
5076 (and (not A) (xor B C)) == (xor (ior A B) (ior A C)) */
5078 if (GET_CODE (op0) == NOT && GET_CODE (op1) == XOR)
5079 return apply_distributive_law
5080 (gen_binary (XOR, mode,
5081 gen_binary (IOR, mode, XEXP (op0, 0), XEXP (op1, 0)),
5082 gen_binary (IOR, mode, XEXP (op0, 0), XEXP (op1, 1))));
5084 else if (GET_CODE (op1) == NOT && GET_CODE (op0) == XOR)
5085 return apply_distributive_law
5086 (gen_binary (XOR, mode,
5087 gen_binary (IOR, mode, XEXP (op1, 0), XEXP (op0, 0)),
5088 gen_binary (IOR, mode, XEXP (op1, 0), XEXP (op0, 1))));
5089 break;
5091 case IOR:
5092 /* (ior A C) is C if all bits of A that might be nonzero are on in C. */
5093 if (GET_CODE (op1) == CONST_INT
5094 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5095 && (nonzero_bits (op0, mode) & ~ INTVAL (op1)) == 0)
5096 return op1;
5098 /* Convert (A & B) | A to A. */
5099 if (GET_CODE (op0) == AND
5100 && (rtx_equal_p (XEXP (op0, 0), op1)
5101 || rtx_equal_p (XEXP (op0, 1), op1))
5102 && ! side_effects_p (XEXP (op0, 0))
5103 && ! side_effects_p (XEXP (op0, 1)))
5104 return op1;
5106 /* If we have (ior (and A B) C), apply the distributive law and then
5107 the inverse distributive law to see if things simplify. */
5109 if (GET_CODE (op0) == AND)
5111 x = apply_distributive_law
5112 (gen_binary (AND, mode,
5113 gen_binary (IOR, mode, XEXP (op0, 0), op1),
5114 gen_binary (IOR, mode, XEXP (op0, 1), op1)));
5116 if (GET_CODE (x) != IOR)
5117 return x;
5120 if (GET_CODE (op1) == AND)
5122 x = apply_distributive_law
5123 (gen_binary (AND, mode,
5124 gen_binary (IOR, mode, XEXP (op1, 0), op0),
5125 gen_binary (IOR, mode, XEXP (op1, 1), op0)));
5127 if (GET_CODE (x) != IOR)
5128 return x;
5131 /* Convert (ior (ashift A CX) (lshiftrt A CY)) where CX+CY equals the
5132 mode size to (rotate A CX). */
5134 if (((GET_CODE (op0) == ASHIFT && GET_CODE (op1) == LSHIFTRT)
5135 || (GET_CODE (op1) == ASHIFT && GET_CODE (op0) == LSHIFTRT))
5136 && rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
5137 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5138 && GET_CODE (XEXP (op1, 1)) == CONST_INT
5139 && (INTVAL (XEXP (op0, 1)) + INTVAL (XEXP (op1, 1))
5140 == GET_MODE_BITSIZE (mode)))
5141 return gen_rtx_ROTATE (mode, XEXP (op0, 0),
5142 (GET_CODE (op0) == ASHIFT
5143 ? XEXP (op0, 1) : XEXP (op1, 1)));
5145 /* If OP0 is (ashiftrt (plus ...) C), it might actually be
5146 a (sign_extend (plus ...)). If so, OP1 is a CONST_INT, and the PLUS
5147 does not affect any of the bits in OP1, it can really be done
5148 as a PLUS and we can associate. We do this by seeing if OP1
5149 can be safely shifted left C bits. */
5150 if (GET_CODE (op1) == CONST_INT && GET_CODE (op0) == ASHIFTRT
5151 && GET_CODE (XEXP (op0, 0)) == PLUS
5152 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
5153 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5154 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT)
5156 int count = INTVAL (XEXP (op0, 1));
5157 HOST_WIDE_INT mask = INTVAL (op1) << count;
5159 if (mask >> count == INTVAL (op1)
5160 && (mask & nonzero_bits (XEXP (op0, 0), mode)) == 0)
5162 SUBST (XEXP (XEXP (op0, 0), 1),
5163 GEN_INT (INTVAL (XEXP (XEXP (op0, 0), 1)) | mask));
5164 return op0;
5167 break;
5169 case XOR:
5170 /* If we are XORing two things that have no bits in common,
5171 convert them into an IOR. This helps to detect rotation encoded
5172 using those methods and possibly other simplifications. */
5174 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5175 && (nonzero_bits (op0, mode)
5176 & nonzero_bits (op1, mode)) == 0)
5177 return (gen_binary (IOR, mode, op0, op1));
5179 /* Convert (XOR (NOT x) (NOT y)) to (XOR x y).
5180 Also convert (XOR (NOT x) y) to (NOT (XOR x y)), similarly for
5181 (NOT y). */
5183 int num_negated = 0;
5185 if (GET_CODE (op0) == NOT)
5186 num_negated++, op0 = XEXP (op0, 0);
5187 if (GET_CODE (op1) == NOT)
5188 num_negated++, op1 = XEXP (op1, 0);
5190 if (num_negated == 2)
5192 SUBST (XEXP (x, 0), op0);
5193 SUBST (XEXP (x, 1), op1);
5195 else if (num_negated == 1)
5196 return gen_unary (NOT, mode, mode, gen_binary (XOR, mode, op0, op1));
5199 /* Convert (xor (and A B) B) to (and (not A) B). The latter may
5200 correspond to a machine insn or result in further simplifications
5201 if B is a constant. */
5203 if (GET_CODE (op0) == AND
5204 && rtx_equal_p (XEXP (op0, 1), op1)
5205 && ! side_effects_p (op1))
5206 return gen_binary (AND, mode,
5207 gen_unary (NOT, mode, mode, XEXP (op0, 0)),
5208 op1);
5210 else if (GET_CODE (op0) == AND
5211 && rtx_equal_p (XEXP (op0, 0), op1)
5212 && ! side_effects_p (op1))
5213 return gen_binary (AND, mode,
5214 gen_unary (NOT, mode, mode, XEXP (op0, 1)),
5215 op1);
5217 /* (xor (comparison foo bar) (const_int 1)) can become the reversed
5218 comparison if STORE_FLAG_VALUE is 1. */
5219 if (STORE_FLAG_VALUE == 1
5220 && op1 == const1_rtx
5221 && GET_RTX_CLASS (GET_CODE (op0)) == '<'
5222 && reversible_comparison_p (op0))
5223 return gen_rtx_combine (reverse_condition (GET_CODE (op0)),
5224 mode, XEXP (op0, 0), XEXP (op0, 1));
5226 /* (lshiftrt foo C) where C is the number of bits in FOO minus 1
5227 is (lt foo (const_int 0)), so we can perform the above
5228 simplification if STORE_FLAG_VALUE is 1. */
5230 if (STORE_FLAG_VALUE == 1
5231 && op1 == const1_rtx
5232 && GET_CODE (op0) == LSHIFTRT
5233 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5234 && INTVAL (XEXP (op0, 1)) == GET_MODE_BITSIZE (mode) - 1)
5235 return gen_rtx_combine (GE, mode, XEXP (op0, 0), const0_rtx);
5237 /* (xor (comparison foo bar) (const_int sign-bit))
5238 when STORE_FLAG_VALUE is the sign bit. */
5239 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5240 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
5241 == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
5242 && op1 == const_true_rtx
5243 && GET_RTX_CLASS (GET_CODE (op0)) == '<'
5244 && reversible_comparison_p (op0))
5245 return gen_rtx_combine (reverse_condition (GET_CODE (op0)),
5246 mode, XEXP (op0, 0), XEXP (op0, 1));
5248 break;
5250 default:
5251 abort ();
5254 return x;
5257 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
5258 operations" because they can be replaced with two more basic operations.
5259 ZERO_EXTEND is also considered "compound" because it can be replaced with
5260 an AND operation, which is simpler, though only one operation.
5262 The function expand_compound_operation is called with an rtx expression
5263 and will convert it to the appropriate shifts and AND operations,
5264 simplifying at each stage.
5266 The function make_compound_operation is called to convert an expression
5267 consisting of shifts and ANDs into the equivalent compound expression.
5268 It is the inverse of this function, loosely speaking. */
5270 static rtx
5271 expand_compound_operation (x)
5272 rtx x;
5274 int pos = 0, len;
5275 int unsignedp = 0;
5276 int modewidth;
5277 rtx tem;
5279 switch (GET_CODE (x))
5281 case ZERO_EXTEND:
5282 unsignedp = 1;
5283 case SIGN_EXTEND:
5284 /* We can't necessarily use a const_int for a multiword mode;
5285 it depends on implicitly extending the value.
5286 Since we don't know the right way to extend it,
5287 we can't tell whether the implicit way is right.
5289 Even for a mode that is no wider than a const_int,
5290 we can't win, because we need to sign extend one of its bits through
5291 the rest of it, and we don't know which bit. */
5292 if (GET_CODE (XEXP (x, 0)) == CONST_INT)
5293 return x;
5295 /* Return if (subreg:MODE FROM 0) is not a safe replacement for
5296 (zero_extend:MODE FROM) or (sign_extend:MODE FROM). It is for any MEM
5297 because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
5298 reloaded. If not for that, MEM's would very rarely be safe.
5300 Reject MODEs bigger than a word, because we might not be able
5301 to reference a two-register group starting with an arbitrary register
5302 (and currently gen_lowpart might crash for a SUBREG). */
5304 if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
5305 return x;
5307 len = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)));
5308 /* If the inner object has VOIDmode (the only way this can happen
5309 is if it is a ASM_OPERANDS), we can't do anything since we don't
5310 know how much masking to do. */
5311 if (len == 0)
5312 return x;
5314 break;
5316 case ZERO_EXTRACT:
5317 unsignedp = 1;
5318 case SIGN_EXTRACT:
5319 /* If the operand is a CLOBBER, just return it. */
5320 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
5321 return XEXP (x, 0);
5323 if (GET_CODE (XEXP (x, 1)) != CONST_INT
5324 || GET_CODE (XEXP (x, 2)) != CONST_INT
5325 || GET_MODE (XEXP (x, 0)) == VOIDmode)
5326 return x;
5328 len = INTVAL (XEXP (x, 1));
5329 pos = INTVAL (XEXP (x, 2));
5331 /* If this goes outside the object being extracted, replace the object
5332 with a (use (mem ...)) construct that only combine understands
5333 and is used only for this purpose. */
5334 if (len + pos > GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
5335 SUBST (XEXP (x, 0), gen_rtx_USE (GET_MODE (x), XEXP (x, 0)));
5337 if (BITS_BIG_ENDIAN)
5338 pos = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - len - pos;
5340 break;
5342 default:
5343 return x;
5346 /* We can optimize some special cases of ZERO_EXTEND. */
5347 if (GET_CODE (x) == ZERO_EXTEND)
5349 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
5350 know that the last value didn't have any inappropriate bits
5351 set. */
5352 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
5353 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
5354 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5355 && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
5356 & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5357 return XEXP (XEXP (x, 0), 0);
5359 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
5360 if (GET_CODE (XEXP (x, 0)) == SUBREG
5361 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
5362 && subreg_lowpart_p (XEXP (x, 0))
5363 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5364 && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
5365 & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5366 return SUBREG_REG (XEXP (x, 0));
5368 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
5369 is a comparison and STORE_FLAG_VALUE permits. This is like
5370 the first case, but it works even when GET_MODE (x) is larger
5371 than HOST_WIDE_INT. */
5372 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
5373 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
5374 && GET_RTX_CLASS (GET_CODE (XEXP (XEXP (x, 0), 0))) == '<'
5375 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5376 <= HOST_BITS_PER_WIDE_INT)
5377 && ((HOST_WIDE_INT) STORE_FLAG_VALUE
5378 & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5379 return XEXP (XEXP (x, 0), 0);
5381 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
5382 if (GET_CODE (XEXP (x, 0)) == SUBREG
5383 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
5384 && subreg_lowpart_p (XEXP (x, 0))
5385 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (XEXP (x, 0)))) == '<'
5386 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5387 <= HOST_BITS_PER_WIDE_INT)
5388 && ((HOST_WIDE_INT) STORE_FLAG_VALUE
5389 & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5390 return SUBREG_REG (XEXP (x, 0));
5392 /* If sign extension is cheaper than zero extension, then use it
5393 if we know that no extraneous bits are set, and that the high
5394 bit is not set. */
5395 if (flag_expensive_optimizations
5396 && ((GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5397 && ((nonzero_bits (XEXP (x, 0), GET_MODE (x))
5398 & ~ (((unsigned HOST_WIDE_INT)
5399 GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
5400 >> 1))
5401 == 0))
5402 || (GET_RTX_CLASS (GET_CODE (XEXP (x, 0))) == '<'
5403 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5404 <= HOST_BITS_PER_WIDE_INT)
5405 && (((HOST_WIDE_INT) STORE_FLAG_VALUE
5406 & ~ (((unsigned HOST_WIDE_INT)
5407 GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
5408 >> 1))
5409 == 0))))
5411 rtx temp = gen_rtx_SIGN_EXTEND (GET_MODE (x), XEXP (x, 0));
5413 if (rtx_cost (temp, SET) < rtx_cost (x, SET))
5414 return expand_compound_operation (temp);
5418 /* If we reach here, we want to return a pair of shifts. The inner
5419 shift is a left shift of BITSIZE - POS - LEN bits. The outer
5420 shift is a right shift of BITSIZE - LEN bits. It is arithmetic or
5421 logical depending on the value of UNSIGNEDP.
5423 If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
5424 converted into an AND of a shift.
5426 We must check for the case where the left shift would have a negative
5427 count. This can happen in a case like (x >> 31) & 255 on machines
5428 that can't shift by a constant. On those machines, we would first
5429 combine the shift with the AND to produce a variable-position
5430 extraction. Then the constant of 31 would be substituted in to produce
5431 a such a position. */
5433 modewidth = GET_MODE_BITSIZE (GET_MODE (x));
5434 if (modewidth >= pos - len)
5435 tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
5436 GET_MODE (x),
5437 simplify_shift_const (NULL_RTX, ASHIFT,
5438 GET_MODE (x),
5439 XEXP (x, 0),
5440 modewidth - pos - len),
5441 modewidth - len);
5443 else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
5444 tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
5445 simplify_shift_const (NULL_RTX, LSHIFTRT,
5446 GET_MODE (x),
5447 XEXP (x, 0), pos),
5448 ((HOST_WIDE_INT) 1 << len) - 1);
5449 else
5450 /* Any other cases we can't handle. */
5451 return x;
5454 /* If we couldn't do this for some reason, return the original
5455 expression. */
5456 if (GET_CODE (tem) == CLOBBER)
5457 return x;
5459 return tem;
5462 /* X is a SET which contains an assignment of one object into
5463 a part of another (such as a bit-field assignment, STRICT_LOW_PART,
5464 or certain SUBREGS). If possible, convert it into a series of
5465 logical operations.
5467 We half-heartedly support variable positions, but do not at all
5468 support variable lengths. */
5470 static rtx
5471 expand_field_assignment (x)
5472 rtx x;
5474 rtx inner;
5475 rtx pos; /* Always counts from low bit. */
5476 int len;
5477 rtx mask;
5478 enum machine_mode compute_mode;
5480 /* Loop until we find something we can't simplify. */
5481 while (1)
5483 if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
5484 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
5486 inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
5487 len = GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)));
5488 pos = GEN_INT (BITS_PER_WORD * SUBREG_WORD (XEXP (SET_DEST (x), 0)));
5490 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
5491 && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT)
5493 inner = XEXP (SET_DEST (x), 0);
5494 len = INTVAL (XEXP (SET_DEST (x), 1));
5495 pos = XEXP (SET_DEST (x), 2);
5497 /* If the position is constant and spans the width of INNER,
5498 surround INNER with a USE to indicate this. */
5499 if (GET_CODE (pos) == CONST_INT
5500 && INTVAL (pos) + len > GET_MODE_BITSIZE (GET_MODE (inner)))
5501 inner = gen_rtx_USE (GET_MODE (SET_DEST (x)), inner);
5503 if (BITS_BIG_ENDIAN)
5505 if (GET_CODE (pos) == CONST_INT)
5506 pos = GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner)) - len
5507 - INTVAL (pos));
5508 else if (GET_CODE (pos) == MINUS
5509 && GET_CODE (XEXP (pos, 1)) == CONST_INT
5510 && (INTVAL (XEXP (pos, 1))
5511 == GET_MODE_BITSIZE (GET_MODE (inner)) - len))
5512 /* If position is ADJUST - X, new position is X. */
5513 pos = XEXP (pos, 0);
5514 else
5515 pos = gen_binary (MINUS, GET_MODE (pos),
5516 GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner))
5517 - len),
5518 pos);
5522 /* A SUBREG between two modes that occupy the same numbers of words
5523 can be done by moving the SUBREG to the source. */
5524 else if (GET_CODE (SET_DEST (x)) == SUBREG
5525 && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
5526 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
5527 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
5528 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
5530 x = gen_rtx_SET (VOIDmode, SUBREG_REG (SET_DEST (x)),
5531 gen_lowpart_for_combine
5532 (GET_MODE (SUBREG_REG (SET_DEST (x))),
5533 SET_SRC (x)));
5534 continue;
5536 else
5537 break;
5539 while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
5540 inner = SUBREG_REG (inner);
5542 compute_mode = GET_MODE (inner);
5544 /* Don't attempt bitwise arithmetic on non-integral modes. */
5545 if (! INTEGRAL_MODE_P (compute_mode))
5547 enum machine_mode imode;
5549 /* Something is probably seriously wrong if this matches. */
5550 if (! FLOAT_MODE_P (compute_mode))
5551 break;
5553 /* Try to find an integral mode to pun with. */
5554 imode = mode_for_size (GET_MODE_BITSIZE (compute_mode), MODE_INT, 0);
5555 if (imode == BLKmode)
5556 break;
5558 compute_mode = imode;
5559 inner = gen_lowpart_for_combine (imode, inner);
5562 /* Compute a mask of LEN bits, if we can do this on the host machine. */
5563 if (len < HOST_BITS_PER_WIDE_INT)
5564 mask = GEN_INT (((HOST_WIDE_INT) 1 << len) - 1);
5565 else
5566 break;
5568 /* Now compute the equivalent expression. Make a copy of INNER
5569 for the SET_DEST in case it is a MEM into which we will substitute;
5570 we don't want shared RTL in that case. */
5571 x = gen_rtx_SET
5572 (VOIDmode, copy_rtx (inner),
5573 gen_binary (IOR, compute_mode,
5574 gen_binary (AND, compute_mode,
5575 gen_unary (NOT, compute_mode,
5576 compute_mode,
5577 gen_binary (ASHIFT,
5578 compute_mode,
5579 mask, pos)),
5580 inner),
5581 gen_binary (ASHIFT, compute_mode,
5582 gen_binary (AND, compute_mode,
5583 gen_lowpart_for_combine
5584 (compute_mode, SET_SRC (x)),
5585 mask),
5586 pos)));
5589 return x;
5592 /* Return an RTX for a reference to LEN bits of INNER. If POS_RTX is nonzero,
5593 it is an RTX that represents a variable starting position; otherwise,
5594 POS is the (constant) starting bit position (counted from the LSB).
5596 INNER may be a USE. This will occur when we started with a bitfield
5597 that went outside the boundary of the object in memory, which is
5598 allowed on most machines. To isolate this case, we produce a USE
5599 whose mode is wide enough and surround the MEM with it. The only
5600 code that understands the USE is this routine. If it is not removed,
5601 it will cause the resulting insn not to match.
5603 UNSIGNEDP is non-zero for an unsigned reference and zero for a
5604 signed reference.
5606 IN_DEST is non-zero if this is a reference in the destination of a
5607 SET. This is used when a ZERO_ or SIGN_EXTRACT isn't needed. If non-zero,
5608 a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
5609 be used.
5611 IN_COMPARE is non-zero if we are in a COMPARE. This means that a
5612 ZERO_EXTRACT should be built even for bits starting at bit 0.
5614 MODE is the desired mode of the result (if IN_DEST == 0).
5616 The result is an RTX for the extraction or NULL_RTX if the target
5617 can't handle it. */
5619 static rtx
5620 make_extraction (mode, inner, pos, pos_rtx, len,
5621 unsignedp, in_dest, in_compare)
5622 enum machine_mode mode;
5623 rtx inner;
5624 int pos;
5625 rtx pos_rtx;
5626 int len;
5627 int unsignedp;
5628 int in_dest, in_compare;
5630 /* This mode describes the size of the storage area
5631 to fetch the overall value from. Within that, we
5632 ignore the POS lowest bits, etc. */
5633 enum machine_mode is_mode = GET_MODE (inner);
5634 enum machine_mode inner_mode;
5635 enum machine_mode wanted_inner_mode = byte_mode;
5636 enum machine_mode wanted_inner_reg_mode = word_mode;
5637 enum machine_mode pos_mode = word_mode;
5638 enum machine_mode extraction_mode = word_mode;
5639 enum machine_mode tmode = mode_for_size (len, MODE_INT, 1);
5640 int spans_byte = 0;
5641 rtx new = 0;
5642 rtx orig_pos_rtx = pos_rtx;
5643 int orig_pos;
5645 /* Get some information about INNER and get the innermost object. */
5646 if (GET_CODE (inner) == USE)
5647 /* (use:SI (mem:QI foo)) stands for (mem:SI foo). */
5648 /* We don't need to adjust the position because we set up the USE
5649 to pretend that it was a full-word object. */
5650 spans_byte = 1, inner = XEXP (inner, 0);
5651 else if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
5653 /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
5654 consider just the QI as the memory to extract from.
5655 The subreg adds or removes high bits; its mode is
5656 irrelevant to the meaning of this extraction,
5657 since POS and LEN count from the lsb. */
5658 if (GET_CODE (SUBREG_REG (inner)) == MEM)
5659 is_mode = GET_MODE (SUBREG_REG (inner));
5660 inner = SUBREG_REG (inner);
5663 inner_mode = GET_MODE (inner);
5665 if (pos_rtx && GET_CODE (pos_rtx) == CONST_INT)
5666 pos = INTVAL (pos_rtx), pos_rtx = 0;
5668 /* See if this can be done without an extraction. We never can if the
5669 width of the field is not the same as that of some integer mode. For
5670 registers, we can only avoid the extraction if the position is at the
5671 low-order bit and this is either not in the destination or we have the
5672 appropriate STRICT_LOW_PART operation available.
5674 For MEM, we can avoid an extract if the field starts on an appropriate
5675 boundary and we can change the mode of the memory reference. However,
5676 we cannot directly access the MEM if we have a USE and the underlying
5677 MEM is not TMODE. This combination means that MEM was being used in a
5678 context where bits outside its mode were being referenced; that is only
5679 valid in bit-field insns. */
5681 if (tmode != BLKmode
5682 && ! (spans_byte && inner_mode != tmode)
5683 && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
5684 && GET_CODE (inner) != MEM
5685 && (! in_dest
5686 || (GET_CODE (inner) == REG
5687 && (movstrict_optab->handlers[(int) tmode].insn_code
5688 != CODE_FOR_nothing))))
5689 || (GET_CODE (inner) == MEM && pos_rtx == 0
5690 && (pos
5691 % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
5692 : BITS_PER_UNIT)) == 0
5693 /* We can't do this if we are widening INNER_MODE (it
5694 may not be aligned, for one thing). */
5695 && GET_MODE_BITSIZE (inner_mode) >= GET_MODE_BITSIZE (tmode)
5696 && (inner_mode == tmode
5697 || (! mode_dependent_address_p (XEXP (inner, 0))
5698 && ! MEM_VOLATILE_P (inner))))))
5700 /* If INNER is a MEM, make a new MEM that encompasses just the desired
5701 field. If the original and current mode are the same, we need not
5702 adjust the offset. Otherwise, we do if bytes big endian.
5704 If INNER is not a MEM, get a piece consisting of just the field
5705 of interest (in this case POS % BITS_PER_WORD must be 0). */
5707 if (GET_CODE (inner) == MEM)
5709 int offset;
5710 /* POS counts from lsb, but make OFFSET count in memory order. */
5711 if (BYTES_BIG_ENDIAN)
5712 offset = (GET_MODE_BITSIZE (is_mode) - len - pos) / BITS_PER_UNIT;
5713 else
5714 offset = pos / BITS_PER_UNIT;
5716 new = gen_rtx_MEM (tmode, plus_constant (XEXP (inner, 0), offset));
5717 RTX_UNCHANGING_P (new) = RTX_UNCHANGING_P (inner);
5718 MEM_COPY_ATTRIBUTES (new, inner);
5720 else if (GET_CODE (inner) == REG)
5722 /* We can't call gen_lowpart_for_combine here since we always want
5723 a SUBREG and it would sometimes return a new hard register. */
5724 if (tmode != inner_mode)
5725 new = gen_rtx_SUBREG (tmode, inner,
5726 (WORDS_BIG_ENDIAN
5727 && (GET_MODE_SIZE (inner_mode)
5728 > UNITS_PER_WORD)
5729 ? (((GET_MODE_SIZE (inner_mode)
5730 - GET_MODE_SIZE (tmode))
5731 / UNITS_PER_WORD)
5732 - pos / BITS_PER_WORD)
5733 : pos / BITS_PER_WORD));
5734 else
5735 new = inner;
5737 else
5738 new = force_to_mode (inner, tmode,
5739 len >= HOST_BITS_PER_WIDE_INT
5740 ? GET_MODE_MASK (tmode)
5741 : ((HOST_WIDE_INT) 1 << len) - 1,
5742 NULL_RTX, 0);
5744 /* If this extraction is going into the destination of a SET,
5745 make a STRICT_LOW_PART unless we made a MEM. */
5747 if (in_dest)
5748 return (GET_CODE (new) == MEM ? new
5749 : (GET_CODE (new) != SUBREG
5750 ? gen_rtx_CLOBBER (tmode, const0_rtx)
5751 : gen_rtx_combine (STRICT_LOW_PART, VOIDmode, new)));
5753 /* Otherwise, sign- or zero-extend unless we already are in the
5754 proper mode. */
5756 return (mode == tmode ? new
5757 : gen_rtx_combine (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
5758 mode, new));
5761 /* Unless this is a COMPARE or we have a funny memory reference,
5762 don't do anything with zero-extending field extracts starting at
5763 the low-order bit since they are simple AND operations. */
5764 if (pos_rtx == 0 && pos == 0 && ! in_dest
5765 && ! in_compare && ! spans_byte && unsignedp)
5766 return 0;
5768 /* Unless we are allowed to span bytes or INNER is not MEM, reject this if
5769 we would be spanning bytes or if the position is not a constant and the
5770 length is not 1. In all other cases, we would only be going outside
5771 our object in cases when an original shift would have been
5772 undefined. */
5773 if (! spans_byte && GET_CODE (inner) == MEM
5774 && ((pos_rtx == 0 && pos + len > GET_MODE_BITSIZE (is_mode))
5775 || (pos_rtx != 0 && len != 1)))
5776 return 0;
5778 /* Get the mode to use should INNER not be a MEM, the mode for the position,
5779 and the mode for the result. */
5780 #ifdef HAVE_insv
5781 if (in_dest)
5783 wanted_inner_reg_mode
5784 = insn_data[(int) CODE_FOR_insv].operand[0].mode;
5785 if (wanted_inner_reg_mode == VOIDmode)
5786 wanted_inner_reg_mode = word_mode;
5788 pos_mode = insn_data[(int) CODE_FOR_insv].operand[2].mode;
5789 if (pos_mode == VOIDmode)
5790 pos_mode = word_mode;
5792 extraction_mode = insn_data[(int) CODE_FOR_insv].operand[3].mode;
5793 if (extraction_mode == VOIDmode)
5794 extraction_mode = word_mode;
5796 #endif
5798 #ifdef HAVE_extzv
5799 if (! in_dest && unsignedp)
5801 wanted_inner_reg_mode
5802 = insn_data[(int) CODE_FOR_extzv].operand[1].mode;
5803 if (wanted_inner_reg_mode == VOIDmode)
5804 wanted_inner_reg_mode = word_mode;
5806 pos_mode = insn_data[(int) CODE_FOR_extzv].operand[3].mode;
5807 if (pos_mode == VOIDmode)
5808 pos_mode = word_mode;
5810 extraction_mode = insn_data[(int) CODE_FOR_extzv].operand[0].mode;
5811 if (extraction_mode == VOIDmode)
5812 extraction_mode = word_mode;
5814 #endif
5816 #ifdef HAVE_extv
5817 if (! in_dest && ! unsignedp)
5819 wanted_inner_reg_mode
5820 = insn_data[(int) CODE_FOR_extv].operand[1].mode;
5821 if (wanted_inner_reg_mode == VOIDmode)
5822 wanted_inner_reg_mode = word_mode;
5824 pos_mode = insn_data[(int) CODE_FOR_extv].operand[3].mode;
5825 if (pos_mode == VOIDmode)
5826 pos_mode = word_mode;
5828 extraction_mode = insn_data[(int) CODE_FOR_extv].operand[0].mode;
5829 if (extraction_mode == VOIDmode)
5830 extraction_mode = word_mode;
5832 #endif
5834 /* Never narrow an object, since that might not be safe. */
5836 if (mode != VOIDmode
5837 && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
5838 extraction_mode = mode;
5840 if (pos_rtx && GET_MODE (pos_rtx) != VOIDmode
5841 && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
5842 pos_mode = GET_MODE (pos_rtx);
5844 /* If this is not from memory, the desired mode is wanted_inner_reg_mode;
5845 if we have to change the mode of memory and cannot, the desired mode is
5846 EXTRACTION_MODE. */
5847 if (GET_CODE (inner) != MEM)
5848 wanted_inner_mode = wanted_inner_reg_mode;
5849 else if (inner_mode != wanted_inner_mode
5850 && (mode_dependent_address_p (XEXP (inner, 0))
5851 || MEM_VOLATILE_P (inner)))
5852 wanted_inner_mode = extraction_mode;
5854 orig_pos = pos;
5856 if (BITS_BIG_ENDIAN)
5858 /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
5859 BITS_BIG_ENDIAN style. If position is constant, compute new
5860 position. Otherwise, build subtraction.
5861 Note that POS is relative to the mode of the original argument.
5862 If it's a MEM we need to recompute POS relative to that.
5863 However, if we're extracting from (or inserting into) a register,
5864 we want to recompute POS relative to wanted_inner_mode. */
5865 int width = (GET_CODE (inner) == MEM
5866 ? GET_MODE_BITSIZE (is_mode)
5867 : GET_MODE_BITSIZE (wanted_inner_mode));
5869 if (pos_rtx == 0)
5870 pos = width - len - pos;
5871 else
5872 pos_rtx
5873 = gen_rtx_combine (MINUS, GET_MODE (pos_rtx),
5874 GEN_INT (width - len), pos_rtx);
5875 /* POS may be less than 0 now, but we check for that below.
5876 Note that it can only be less than 0 if GET_CODE (inner) != MEM. */
5879 /* If INNER has a wider mode, make it smaller. If this is a constant
5880 extract, try to adjust the byte to point to the byte containing
5881 the value. */
5882 if (wanted_inner_mode != VOIDmode
5883 && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
5884 && ((GET_CODE (inner) == MEM
5885 && (inner_mode == wanted_inner_mode
5886 || (! mode_dependent_address_p (XEXP (inner, 0))
5887 && ! MEM_VOLATILE_P (inner))))))
5889 int offset = 0;
5891 /* The computations below will be correct if the machine is big
5892 endian in both bits and bytes or little endian in bits and bytes.
5893 If it is mixed, we must adjust. */
5895 /* If bytes are big endian and we had a paradoxical SUBREG, we must
5896 adjust OFFSET to compensate. */
5897 if (BYTES_BIG_ENDIAN
5898 && ! spans_byte
5899 && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
5900 offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
5902 /* If this is a constant position, we can move to the desired byte. */
5903 if (pos_rtx == 0)
5905 offset += pos / BITS_PER_UNIT;
5906 pos %= GET_MODE_BITSIZE (wanted_inner_mode);
5909 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
5910 && ! spans_byte
5911 && is_mode != wanted_inner_mode)
5912 offset = (GET_MODE_SIZE (is_mode)
5913 - GET_MODE_SIZE (wanted_inner_mode) - offset);
5915 if (offset != 0 || inner_mode != wanted_inner_mode)
5917 rtx newmem = gen_rtx_MEM (wanted_inner_mode,
5918 plus_constant (XEXP (inner, 0), offset));
5919 RTX_UNCHANGING_P (newmem) = RTX_UNCHANGING_P (inner);
5920 MEM_COPY_ATTRIBUTES (newmem, inner);
5921 inner = newmem;
5925 /* If INNER is not memory, we can always get it into the proper mode. If we
5926 are changing its mode, POS must be a constant and smaller than the size
5927 of the new mode. */
5928 else if (GET_CODE (inner) != MEM)
5930 if (GET_MODE (inner) != wanted_inner_mode
5931 && (pos_rtx != 0
5932 || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
5933 return 0;
5935 inner = force_to_mode (inner, wanted_inner_mode,
5936 pos_rtx
5937 || len + orig_pos >= HOST_BITS_PER_WIDE_INT
5938 ? GET_MODE_MASK (wanted_inner_mode)
5939 : (((HOST_WIDE_INT) 1 << len) - 1) << orig_pos,
5940 NULL_RTX, 0);
5943 /* Adjust mode of POS_RTX, if needed. If we want a wider mode, we
5944 have to zero extend. Otherwise, we can just use a SUBREG. */
5945 if (pos_rtx != 0
5946 && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
5947 pos_rtx = gen_rtx_combine (ZERO_EXTEND, pos_mode, pos_rtx);
5948 else if (pos_rtx != 0
5949 && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
5950 pos_rtx = gen_lowpart_for_combine (pos_mode, pos_rtx);
5952 /* Make POS_RTX unless we already have it and it is correct. If we don't
5953 have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
5954 be a CONST_INT. */
5955 if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
5956 pos_rtx = orig_pos_rtx;
5958 else if (pos_rtx == 0)
5959 pos_rtx = GEN_INT (pos);
5961 /* Make the required operation. See if we can use existing rtx. */
5962 new = gen_rtx_combine (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
5963 extraction_mode, inner, GEN_INT (len), pos_rtx);
5964 if (! in_dest)
5965 new = gen_lowpart_for_combine (mode, new);
5967 return new;
5970 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
5971 with any other operations in X. Return X without that shift if so. */
5973 static rtx
5974 extract_left_shift (x, count)
5975 rtx x;
5976 int count;
5978 enum rtx_code code = GET_CODE (x);
5979 enum machine_mode mode = GET_MODE (x);
5980 rtx tem;
5982 switch (code)
5984 case ASHIFT:
5985 /* This is the shift itself. If it is wide enough, we will return
5986 either the value being shifted if the shift count is equal to
5987 COUNT or a shift for the difference. */
5988 if (GET_CODE (XEXP (x, 1)) == CONST_INT
5989 && INTVAL (XEXP (x, 1)) >= count)
5990 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
5991 INTVAL (XEXP (x, 1)) - count);
5992 break;
5994 case NEG: case NOT:
5995 if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
5996 return gen_unary (code, mode, mode, tem);
5998 break;
6000 case PLUS: case IOR: case XOR: case AND:
6001 /* If we can safely shift this constant and we find the inner shift,
6002 make a new operation. */
6003 if (GET_CODE (XEXP (x,1)) == CONST_INT
6004 && (INTVAL (XEXP (x, 1)) & ((((HOST_WIDE_INT) 1 << count)) - 1)) == 0
6005 && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
6006 return gen_binary (code, mode, tem,
6007 GEN_INT (INTVAL (XEXP (x, 1)) >> count));
6009 break;
6011 default:
6012 break;
6015 return 0;
6018 /* Look at the expression rooted at X. Look for expressions
6019 equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
6020 Form these expressions.
6022 Return the new rtx, usually just X.
6024 Also, for machines like the Vax that don't have logical shift insns,
6025 try to convert logical to arithmetic shift operations in cases where
6026 they are equivalent. This undoes the canonicalizations to logical
6027 shifts done elsewhere.
6029 We try, as much as possible, to re-use rtl expressions to save memory.
6031 IN_CODE says what kind of expression we are processing. Normally, it is
6032 SET. In a memory address (inside a MEM, PLUS or minus, the latter two
6033 being kludges), it is MEM. When processing the arguments of a comparison
6034 or a COMPARE against zero, it is COMPARE. */
6036 static rtx
6037 make_compound_operation (x, in_code)
6038 rtx x;
6039 enum rtx_code in_code;
6041 enum rtx_code code = GET_CODE (x);
6042 enum machine_mode mode = GET_MODE (x);
6043 int mode_width = GET_MODE_BITSIZE (mode);
6044 rtx rhs, lhs;
6045 enum rtx_code next_code;
6046 int i;
6047 rtx new = 0;
6048 rtx tem;
6049 const char *fmt;
6051 /* Select the code to be used in recursive calls. Once we are inside an
6052 address, we stay there. If we have a comparison, set to COMPARE,
6053 but once inside, go back to our default of SET. */
6055 next_code = (code == MEM || code == PLUS || code == MINUS ? MEM
6056 : ((code == COMPARE || GET_RTX_CLASS (code) == '<')
6057 && XEXP (x, 1) == const0_rtx) ? COMPARE
6058 : in_code == COMPARE ? SET : in_code);
6060 /* Process depending on the code of this operation. If NEW is set
6061 non-zero, it will be returned. */
6063 switch (code)
6065 case ASHIFT:
6066 /* Convert shifts by constants into multiplications if inside
6067 an address. */
6068 if (in_code == MEM && GET_CODE (XEXP (x, 1)) == CONST_INT
6069 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
6070 && INTVAL (XEXP (x, 1)) >= 0)
6072 new = make_compound_operation (XEXP (x, 0), next_code);
6073 new = gen_rtx_combine (MULT, mode, new,
6074 GEN_INT ((HOST_WIDE_INT) 1
6075 << INTVAL (XEXP (x, 1))));
6077 break;
6079 case AND:
6080 /* If the second operand is not a constant, we can't do anything
6081 with it. */
6082 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
6083 break;
6085 /* If the constant is a power of two minus one and the first operand
6086 is a logical right shift, make an extraction. */
6087 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6088 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6090 new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
6091 new = make_extraction (mode, new, 0, XEXP (XEXP (x, 0), 1), i, 1,
6092 0, in_code == COMPARE);
6095 /* Same as previous, but for (subreg (lshiftrt ...)) in first op. */
6096 else if (GET_CODE (XEXP (x, 0)) == SUBREG
6097 && subreg_lowpart_p (XEXP (x, 0))
6098 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
6099 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6101 new = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
6102 next_code);
6103 new = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new, 0,
6104 XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
6105 0, in_code == COMPARE);
6107 /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)). */
6108 else if ((GET_CODE (XEXP (x, 0)) == XOR
6109 || GET_CODE (XEXP (x, 0)) == IOR)
6110 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
6111 && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
6112 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6114 /* Apply the distributive law, and then try to make extractions. */
6115 new = gen_rtx_combine (GET_CODE (XEXP (x, 0)), mode,
6116 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
6117 XEXP (x, 1)),
6118 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
6119 XEXP (x, 1)));
6120 new = make_compound_operation (new, in_code);
6123 /* If we are have (and (rotate X C) M) and C is larger than the number
6124 of bits in M, this is an extraction. */
6126 else if (GET_CODE (XEXP (x, 0)) == ROTATE
6127 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6128 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0
6129 && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
6131 new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
6132 new = make_extraction (mode, new,
6133 (GET_MODE_BITSIZE (mode)
6134 - INTVAL (XEXP (XEXP (x, 0), 1))),
6135 NULL_RTX, i, 1, 0, in_code == COMPARE);
6138 /* On machines without logical shifts, if the operand of the AND is
6139 a logical shift and our mask turns off all the propagated sign
6140 bits, we can replace the logical shift with an arithmetic shift. */
6141 else if (ashr_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing
6142 && (lshr_optab->handlers[(int) mode].insn_code
6143 == CODE_FOR_nothing)
6144 && GET_CODE (XEXP (x, 0)) == LSHIFTRT
6145 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6146 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
6147 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
6148 && mode_width <= HOST_BITS_PER_WIDE_INT)
6150 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
6152 mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
6153 if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
6154 SUBST (XEXP (x, 0),
6155 gen_rtx_combine (ASHIFTRT, mode,
6156 make_compound_operation (XEXP (XEXP (x, 0), 0),
6157 next_code),
6158 XEXP (XEXP (x, 0), 1)));
6161 /* If the constant is one less than a power of two, this might be
6162 representable by an extraction even if no shift is present.
6163 If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
6164 we are in a COMPARE. */
6165 else if ((i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6166 new = make_extraction (mode,
6167 make_compound_operation (XEXP (x, 0),
6168 next_code),
6169 0, NULL_RTX, i, 1, 0, in_code == COMPARE);
6171 /* If we are in a comparison and this is an AND with a power of two,
6172 convert this into the appropriate bit extract. */
6173 else if (in_code == COMPARE
6174 && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
6175 new = make_extraction (mode,
6176 make_compound_operation (XEXP (x, 0),
6177 next_code),
6178 i, NULL_RTX, 1, 1, 0, 1);
6180 break;
6182 case LSHIFTRT:
6183 /* If the sign bit is known to be zero, replace this with an
6184 arithmetic shift. */
6185 if (ashr_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing
6186 && lshr_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing
6187 && mode_width <= HOST_BITS_PER_WIDE_INT
6188 && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
6190 new = gen_rtx_combine (ASHIFTRT, mode,
6191 make_compound_operation (XEXP (x, 0),
6192 next_code),
6193 XEXP (x, 1));
6194 break;
6197 /* ... fall through ... */
6199 case ASHIFTRT:
6200 lhs = XEXP (x, 0);
6201 rhs = XEXP (x, 1);
6203 /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
6204 this is a SIGN_EXTRACT. */
6205 if (GET_CODE (rhs) == CONST_INT
6206 && GET_CODE (lhs) == ASHIFT
6207 && GET_CODE (XEXP (lhs, 1)) == CONST_INT
6208 && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1)))
6210 new = make_compound_operation (XEXP (lhs, 0), next_code);
6211 new = make_extraction (mode, new,
6212 INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
6213 NULL_RTX, mode_width - INTVAL (rhs),
6214 code == LSHIFTRT, 0, in_code == COMPARE);
6217 /* See if we have operations between an ASHIFTRT and an ASHIFT.
6218 If so, try to merge the shifts into a SIGN_EXTEND. We could
6219 also do this for some cases of SIGN_EXTRACT, but it doesn't
6220 seem worth the effort; the case checked for occurs on Alpha. */
6222 if (GET_RTX_CLASS (GET_CODE (lhs)) != 'o'
6223 && ! (GET_CODE (lhs) == SUBREG
6224 && (GET_RTX_CLASS (GET_CODE (SUBREG_REG (lhs))) == 'o'))
6225 && GET_CODE (rhs) == CONST_INT
6226 && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
6227 && (new = extract_left_shift (lhs, INTVAL (rhs))) != 0)
6228 new = make_extraction (mode, make_compound_operation (new, next_code),
6229 0, NULL_RTX, mode_width - INTVAL (rhs),
6230 code == LSHIFTRT, 0, in_code == COMPARE);
6232 break;
6234 case SUBREG:
6235 /* Call ourselves recursively on the inner expression. If we are
6236 narrowing the object and it has a different RTL code from
6237 what it originally did, do this SUBREG as a force_to_mode. */
6239 tem = make_compound_operation (SUBREG_REG (x), in_code);
6240 if (GET_CODE (tem) != GET_CODE (SUBREG_REG (x))
6241 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (tem))
6242 && subreg_lowpart_p (x))
6244 rtx newer = force_to_mode (tem, mode,
6245 GET_MODE_MASK (mode), NULL_RTX, 0);
6247 /* If we have something other than a SUBREG, we might have
6248 done an expansion, so rerun outselves. */
6249 if (GET_CODE (newer) != SUBREG)
6250 newer = make_compound_operation (newer, in_code);
6252 return newer;
6255 /* If this is a paradoxical subreg, and the new code is a sign or
6256 zero extension, omit the subreg and widen the extension. If it
6257 is a regular subreg, we can still get rid of the subreg by not
6258 widening so much, or in fact removing the extension entirely. */
6259 if ((GET_CODE (tem) == SIGN_EXTEND
6260 || GET_CODE (tem) == ZERO_EXTEND)
6261 && subreg_lowpart_p (x))
6263 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (tem))
6264 || (GET_MODE_SIZE (mode) >
6265 GET_MODE_SIZE (GET_MODE (XEXP (tem, 0)))))
6266 tem = gen_rtx_combine (GET_CODE (tem), mode, XEXP (tem, 0));
6267 else
6268 tem = gen_lowpart_for_combine (mode, XEXP (tem, 0));
6269 return tem;
6271 break;
6273 default:
6274 break;
6277 if (new)
6279 x = gen_lowpart_for_combine (mode, new);
6280 code = GET_CODE (x);
6283 /* Now recursively process each operand of this operation. */
6284 fmt = GET_RTX_FORMAT (code);
6285 for (i = 0; i < GET_RTX_LENGTH (code); i++)
6286 if (fmt[i] == 'e')
6288 new = make_compound_operation (XEXP (x, i), next_code);
6289 SUBST (XEXP (x, i), new);
6292 return x;
6295 /* Given M see if it is a value that would select a field of bits
6296 within an item, but not the entire word. Return -1 if not.
6297 Otherwise, return the starting position of the field, where 0 is the
6298 low-order bit.
6300 *PLEN is set to the length of the field. */
6302 static int
6303 get_pos_from_mask (m, plen)
6304 unsigned HOST_WIDE_INT m;
6305 int *plen;
6307 /* Get the bit number of the first 1 bit from the right, -1 if none. */
6308 int pos = exact_log2 (m & - m);
6310 if (pos < 0)
6311 return -1;
6313 /* Now shift off the low-order zero bits and see if we have a power of
6314 two minus 1. */
6315 *plen = exact_log2 ((m >> pos) + 1);
6317 if (*plen <= 0)
6318 return -1;
6320 return pos;
6323 /* See if X can be simplified knowing that we will only refer to it in
6324 MODE and will only refer to those bits that are nonzero in MASK.
6325 If other bits are being computed or if masking operations are done
6326 that select a superset of the bits in MASK, they can sometimes be
6327 ignored.
6329 Return a possibly simplified expression, but always convert X to
6330 MODE. If X is a CONST_INT, AND the CONST_INT with MASK.
6332 Also, if REG is non-zero and X is a register equal in value to REG,
6333 replace X with REG.
6335 If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
6336 are all off in X. This is used when X will be complemented, by either
6337 NOT, NEG, or XOR. */
6339 static rtx
6340 force_to_mode (x, mode, mask, reg, just_select)
6341 rtx x;
6342 enum machine_mode mode;
6343 unsigned HOST_WIDE_INT mask;
6344 rtx reg;
6345 int just_select;
6347 enum rtx_code code = GET_CODE (x);
6348 int next_select = just_select || code == XOR || code == NOT || code == NEG;
6349 enum machine_mode op_mode;
6350 unsigned HOST_WIDE_INT fuller_mask, nonzero;
6351 rtx op0, op1, temp;
6353 /* If this is a CALL or ASM_OPERANDS, don't do anything. Some of the
6354 code below will do the wrong thing since the mode of such an
6355 expression is VOIDmode.
6357 Also do nothing if X is a CLOBBER; this can happen if X was
6358 the return value from a call to gen_lowpart_for_combine. */
6359 if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
6360 return x;
6362 /* We want to perform the operation is its present mode unless we know
6363 that the operation is valid in MODE, in which case we do the operation
6364 in MODE. */
6365 op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
6366 && code_to_optab[(int) code] != 0
6367 && (code_to_optab[(int) code]->handlers[(int) mode].insn_code
6368 != CODE_FOR_nothing))
6369 ? mode : GET_MODE (x));
6371 /* It is not valid to do a right-shift in a narrower mode
6372 than the one it came in with. */
6373 if ((code == LSHIFTRT || code == ASHIFTRT)
6374 && GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (GET_MODE (x)))
6375 op_mode = GET_MODE (x);
6377 /* Truncate MASK to fit OP_MODE. */
6378 if (op_mode)
6379 mask &= GET_MODE_MASK (op_mode);
6381 /* When we have an arithmetic operation, or a shift whose count we
6382 do not know, we need to assume that all bit the up to the highest-order
6383 bit in MASK will be needed. This is how we form such a mask. */
6384 if (op_mode)
6385 fuller_mask = (GET_MODE_BITSIZE (op_mode) >= HOST_BITS_PER_WIDE_INT
6386 ? GET_MODE_MASK (op_mode)
6387 : ((HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1)) - 1);
6388 else
6389 fuller_mask = ~ (HOST_WIDE_INT) 0;
6391 /* Determine what bits of X are guaranteed to be (non)zero. */
6392 nonzero = nonzero_bits (x, mode);
6394 /* If none of the bits in X are needed, return a zero. */
6395 if (! just_select && (nonzero & mask) == 0)
6396 return const0_rtx;
6398 /* If X is a CONST_INT, return a new one. Do this here since the
6399 test below will fail. */
6400 if (GET_CODE (x) == CONST_INT)
6402 HOST_WIDE_INT cval = INTVAL (x) & mask;
6403 int width = GET_MODE_BITSIZE (mode);
6405 /* If MODE is narrower that HOST_WIDE_INT and CVAL is a negative
6406 number, sign extend it. */
6407 if (width > 0 && width < HOST_BITS_PER_WIDE_INT
6408 && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
6409 cval |= (HOST_WIDE_INT) -1 << width;
6411 return GEN_INT (cval);
6414 /* If X is narrower than MODE and we want all the bits in X's mode, just
6415 get X in the proper mode. */
6416 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
6417 && (GET_MODE_MASK (GET_MODE (x)) & ~ mask) == 0)
6418 return gen_lowpart_for_combine (mode, x);
6420 /* If we aren't changing the mode, X is not a SUBREG, and all zero bits in
6421 MASK are already known to be zero in X, we need not do anything. */
6422 if (GET_MODE (x) == mode && code != SUBREG && (~ mask & nonzero) == 0)
6423 return x;
6425 switch (code)
6427 case CLOBBER:
6428 /* If X is a (clobber (const_int)), return it since we know we are
6429 generating something that won't match. */
6430 return x;
6432 case USE:
6433 /* X is a (use (mem ..)) that was made from a bit-field extraction that
6434 spanned the boundary of the MEM. If we are now masking so it is
6435 within that boundary, we don't need the USE any more. */
6436 if (! BITS_BIG_ENDIAN
6437 && (mask & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6438 return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
6439 break;
6441 case SIGN_EXTEND:
6442 case ZERO_EXTEND:
6443 case ZERO_EXTRACT:
6444 case SIGN_EXTRACT:
6445 x = expand_compound_operation (x);
6446 if (GET_CODE (x) != code)
6447 return force_to_mode (x, mode, mask, reg, next_select);
6448 break;
6450 case REG:
6451 if (reg != 0 && (rtx_equal_p (get_last_value (reg), x)
6452 || rtx_equal_p (reg, get_last_value (x))))
6453 x = reg;
6454 break;
6456 case SUBREG:
6457 if (subreg_lowpart_p (x)
6458 /* We can ignore the effect of this SUBREG if it narrows the mode or
6459 if the constant masks to zero all the bits the mode doesn't
6460 have. */
6461 && ((GET_MODE_SIZE (GET_MODE (x))
6462 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
6463 || (0 == (mask
6464 & GET_MODE_MASK (GET_MODE (x))
6465 & ~ GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
6466 return force_to_mode (SUBREG_REG (x), mode, mask, reg, next_select);
6467 break;
6469 case AND:
6470 /* If this is an AND with a constant, convert it into an AND
6471 whose constant is the AND of that constant with MASK. If it
6472 remains an AND of MASK, delete it since it is redundant. */
6474 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
6476 x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
6477 mask & INTVAL (XEXP (x, 1)));
6479 /* If X is still an AND, see if it is an AND with a mask that
6480 is just some low-order bits. If so, and it is MASK, we don't
6481 need it. */
6483 if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
6484 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (x, 1)) == mask)
6485 x = XEXP (x, 0);
6487 /* If it remains an AND, try making another AND with the bits
6488 in the mode mask that aren't in MASK turned on. If the
6489 constant in the AND is wide enough, this might make a
6490 cheaper constant. */
6492 if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
6493 && GET_MODE_MASK (GET_MODE (x)) != mask
6494 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
6496 HOST_WIDE_INT cval = (INTVAL (XEXP (x, 1))
6497 | (GET_MODE_MASK (GET_MODE (x)) & ~ mask));
6498 int width = GET_MODE_BITSIZE (GET_MODE (x));
6499 rtx y;
6501 /* If MODE is narrower that HOST_WIDE_INT and CVAL is a negative
6502 number, sign extend it. */
6503 if (width > 0 && width < HOST_BITS_PER_WIDE_INT
6504 && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
6505 cval |= (HOST_WIDE_INT) -1 << width;
6507 y = gen_binary (AND, GET_MODE (x), XEXP (x, 0), GEN_INT (cval));
6508 if (rtx_cost (y, SET) < rtx_cost (x, SET))
6509 x = y;
6512 break;
6515 goto binop;
6517 case PLUS:
6518 /* In (and (plus FOO C1) M), if M is a mask that just turns off
6519 low-order bits (as in an alignment operation) and FOO is already
6520 aligned to that boundary, mask C1 to that boundary as well.
6521 This may eliminate that PLUS and, later, the AND. */
6524 int width = GET_MODE_BITSIZE (mode);
6525 unsigned HOST_WIDE_INT smask = mask;
6527 /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
6528 number, sign extend it. */
6530 if (width < HOST_BITS_PER_WIDE_INT
6531 && (smask & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
6532 smask |= (HOST_WIDE_INT) -1 << width;
6534 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6535 && exact_log2 (- smask) >= 0)
6537 #ifdef STACK_BIAS
6538 if (STACK_BIAS
6539 && (XEXP (x, 0) == stack_pointer_rtx
6540 || XEXP (x, 0) == frame_pointer_rtx))
6542 int sp_alignment = STACK_BOUNDARY / BITS_PER_UNIT;
6543 unsigned HOST_WIDE_INT sp_mask = GET_MODE_MASK (mode);
6545 sp_mask &= ~ (sp_alignment - 1);
6546 if ((sp_mask & ~ smask) == 0
6547 && ((INTVAL (XEXP (x, 1)) - STACK_BIAS) & ~ smask) != 0)
6548 return force_to_mode (plus_constant (XEXP (x, 0),
6549 ((INTVAL (XEXP (x, 1)) -
6550 STACK_BIAS) & smask)
6551 + STACK_BIAS),
6552 mode, smask, reg, next_select);
6554 #endif
6555 if ((nonzero_bits (XEXP (x, 0), mode) & ~ smask) == 0
6556 && (INTVAL (XEXP (x, 1)) & ~ smask) != 0)
6557 return force_to_mode (plus_constant (XEXP (x, 0),
6558 (INTVAL (XEXP (x, 1))
6559 & smask)),
6560 mode, smask, reg, next_select);
6564 /* ... fall through ... */
6566 case MINUS:
6567 case MULT:
6568 /* For PLUS, MINUS and MULT, we need any bits less significant than the
6569 most significant bit in MASK since carries from those bits will
6570 affect the bits we are interested in. */
6571 mask = fuller_mask;
6572 goto binop;
6574 case IOR:
6575 case XOR:
6576 /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
6577 LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
6578 operation which may be a bitfield extraction. Ensure that the
6579 constant we form is not wider than the mode of X. */
6581 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6582 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6583 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
6584 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
6585 && GET_CODE (XEXP (x, 1)) == CONST_INT
6586 && ((INTVAL (XEXP (XEXP (x, 0), 1))
6587 + floor_log2 (INTVAL (XEXP (x, 1))))
6588 < GET_MODE_BITSIZE (GET_MODE (x)))
6589 && (INTVAL (XEXP (x, 1))
6590 & ~ nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
6592 temp = GEN_INT ((INTVAL (XEXP (x, 1)) & mask)
6593 << INTVAL (XEXP (XEXP (x, 0), 1)));
6594 temp = gen_binary (GET_CODE (x), GET_MODE (x),
6595 XEXP (XEXP (x, 0), 0), temp);
6596 x = gen_binary (LSHIFTRT, GET_MODE (x), temp,
6597 XEXP (XEXP (x, 0), 1));
6598 return force_to_mode (x, mode, mask, reg, next_select);
6601 binop:
6602 /* For most binary operations, just propagate into the operation and
6603 change the mode if we have an operation of that mode. */
6605 op0 = gen_lowpart_for_combine (op_mode,
6606 force_to_mode (XEXP (x, 0), mode, mask,
6607 reg, next_select));
6608 op1 = gen_lowpart_for_combine (op_mode,
6609 force_to_mode (XEXP (x, 1), mode, mask,
6610 reg, next_select));
6612 /* If OP1 is a CONST_INT and X is an IOR or XOR, clear bits outside
6613 MASK since OP1 might have been sign-extended but we never want
6614 to turn on extra bits, since combine might have previously relied
6615 on them being off. */
6616 if (GET_CODE (op1) == CONST_INT && (code == IOR || code == XOR)
6617 && (INTVAL (op1) & mask) != 0)
6618 op1 = GEN_INT (INTVAL (op1) & mask);
6620 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
6621 x = gen_binary (code, op_mode, op0, op1);
6622 break;
6624 case ASHIFT:
6625 /* For left shifts, do the same, but just for the first operand.
6626 However, we cannot do anything with shifts where we cannot
6627 guarantee that the counts are smaller than the size of the mode
6628 because such a count will have a different meaning in a
6629 wider mode. */
6631 if (! (GET_CODE (XEXP (x, 1)) == CONST_INT
6632 && INTVAL (XEXP (x, 1)) >= 0
6633 && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (mode))
6634 && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
6635 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
6636 < (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (mode))))
6637 break;
6639 /* If the shift count is a constant and we can do arithmetic in
6640 the mode of the shift, refine which bits we need. Otherwise, use the
6641 conservative form of the mask. */
6642 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6643 && INTVAL (XEXP (x, 1)) >= 0
6644 && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (op_mode)
6645 && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
6646 mask >>= INTVAL (XEXP (x, 1));
6647 else
6648 mask = fuller_mask;
6650 op0 = gen_lowpart_for_combine (op_mode,
6651 force_to_mode (XEXP (x, 0), op_mode,
6652 mask, reg, next_select));
6654 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
6655 x = gen_binary (code, op_mode, op0, XEXP (x, 1));
6656 break;
6658 case LSHIFTRT:
6659 /* Here we can only do something if the shift count is a constant,
6660 this shift constant is valid for the host, and we can do arithmetic
6661 in OP_MODE. */
6663 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6664 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
6665 && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
6667 rtx inner = XEXP (x, 0);
6669 /* Select the mask of the bits we need for the shift operand. */
6670 mask <<= INTVAL (XEXP (x, 1));
6672 /* We can only change the mode of the shift if we can do arithmetic
6673 in the mode of the shift and MASK is no wider than the width of
6674 OP_MODE. */
6675 if (GET_MODE_BITSIZE (op_mode) > HOST_BITS_PER_WIDE_INT
6676 || (mask & ~ GET_MODE_MASK (op_mode)) != 0)
6677 op_mode = GET_MODE (x);
6679 inner = force_to_mode (inner, op_mode, mask, reg, next_select);
6681 if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
6682 x = gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
6685 /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
6686 shift and AND produces only copies of the sign bit (C2 is one less
6687 than a power of two), we can do this with just a shift. */
6689 if (GET_CODE (x) == LSHIFTRT
6690 && GET_CODE (XEXP (x, 1)) == CONST_INT
6691 && ((INTVAL (XEXP (x, 1))
6692 + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
6693 >= GET_MODE_BITSIZE (GET_MODE (x)))
6694 && exact_log2 (mask + 1) >= 0
6695 && (num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
6696 >= exact_log2 (mask + 1)))
6697 x = gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
6698 GEN_INT (GET_MODE_BITSIZE (GET_MODE (x))
6699 - exact_log2 (mask + 1)));
6701 goto shiftrt;
6703 case ASHIFTRT:
6704 /* If we are just looking for the sign bit, we don't need this shift at
6705 all, even if it has a variable count. */
6706 if (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6707 && (mask == ((unsigned HOST_WIDE_INT) 1
6708 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
6709 return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
6711 /* If this is a shift by a constant, get a mask that contains those bits
6712 that are not copies of the sign bit. We then have two cases: If
6713 MASK only includes those bits, this can be a logical shift, which may
6714 allow simplifications. If MASK is a single-bit field not within
6715 those bits, we are requesting a copy of the sign bit and hence can
6716 shift the sign bit to the appropriate location. */
6718 if (GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) >= 0
6719 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
6721 int i = -1;
6723 /* If the considered data is wider then HOST_WIDE_INT, we can't
6724 represent a mask for all its bits in a single scalar.
6725 But we only care about the lower bits, so calculate these. */
6727 if (GET_MODE_BITSIZE (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
6729 nonzero = ~ (HOST_WIDE_INT) 0;
6731 /* GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
6732 is the number of bits a full-width mask would have set.
6733 We need only shift if these are fewer than nonzero can
6734 hold. If not, we must keep all bits set in nonzero. */
6736 if (GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
6737 < HOST_BITS_PER_WIDE_INT)
6738 nonzero >>= INTVAL (XEXP (x, 1))
6739 + HOST_BITS_PER_WIDE_INT
6740 - GET_MODE_BITSIZE (GET_MODE (x)) ;
6742 else
6744 nonzero = GET_MODE_MASK (GET_MODE (x));
6745 nonzero >>= INTVAL (XEXP (x, 1));
6748 if ((mask & ~ nonzero) == 0
6749 || (i = exact_log2 (mask)) >= 0)
6751 x = simplify_shift_const
6752 (x, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
6753 i < 0 ? INTVAL (XEXP (x, 1))
6754 : GET_MODE_BITSIZE (GET_MODE (x)) - 1 - i);
6756 if (GET_CODE (x) != ASHIFTRT)
6757 return force_to_mode (x, mode, mask, reg, next_select);
6761 /* If MASK is 1, convert this to a LSHIFTRT. This can be done
6762 even if the shift count isn't a constant. */
6763 if (mask == 1)
6764 x = gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0), XEXP (x, 1));
6766 shiftrt:
6768 /* If this is a zero- or sign-extension operation that just affects bits
6769 we don't care about, remove it. Be sure the call above returned
6770 something that is still a shift. */
6772 if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
6773 && GET_CODE (XEXP (x, 1)) == CONST_INT
6774 && INTVAL (XEXP (x, 1)) >= 0
6775 && (INTVAL (XEXP (x, 1))
6776 <= GET_MODE_BITSIZE (GET_MODE (x)) - (floor_log2 (mask) + 1))
6777 && GET_CODE (XEXP (x, 0)) == ASHIFT
6778 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6779 && INTVAL (XEXP (XEXP (x, 0), 1)) == INTVAL (XEXP (x, 1)))
6780 return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
6781 reg, next_select);
6783 break;
6785 case ROTATE:
6786 case ROTATERT:
6787 /* If the shift count is constant and we can do computations
6788 in the mode of X, compute where the bits we care about are.
6789 Otherwise, we can't do anything. Don't change the mode of
6790 the shift or propagate MODE into the shift, though. */
6791 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6792 && INTVAL (XEXP (x, 1)) >= 0)
6794 temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
6795 GET_MODE (x), GEN_INT (mask),
6796 XEXP (x, 1));
6797 if (temp && GET_CODE(temp) == CONST_INT)
6798 SUBST (XEXP (x, 0),
6799 force_to_mode (XEXP (x, 0), GET_MODE (x),
6800 INTVAL (temp), reg, next_select));
6802 break;
6804 case NEG:
6805 /* If we just want the low-order bit, the NEG isn't needed since it
6806 won't change the low-order bit. */
6807 if (mask == 1)
6808 return force_to_mode (XEXP (x, 0), mode, mask, reg, just_select);
6810 /* We need any bits less significant than the most significant bit in
6811 MASK since carries from those bits will affect the bits we are
6812 interested in. */
6813 mask = fuller_mask;
6814 goto unop;
6816 case NOT:
6817 /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
6818 same as the XOR case above. Ensure that the constant we form is not
6819 wider than the mode of X. */
6821 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6822 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6823 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
6824 && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
6825 < GET_MODE_BITSIZE (GET_MODE (x)))
6826 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
6828 temp = GEN_INT (mask << INTVAL (XEXP (XEXP (x, 0), 1)));
6829 temp = gen_binary (XOR, GET_MODE (x), XEXP (XEXP (x, 0), 0), temp);
6830 x = gen_binary (LSHIFTRT, GET_MODE (x), temp, XEXP (XEXP (x, 0), 1));
6832 return force_to_mode (x, mode, mask, reg, next_select);
6835 /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
6836 use the full mask inside the NOT. */
6837 mask = fuller_mask;
6839 unop:
6840 op0 = gen_lowpart_for_combine (op_mode,
6841 force_to_mode (XEXP (x, 0), mode, mask,
6842 reg, next_select));
6843 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
6844 x = gen_unary (code, op_mode, op_mode, op0);
6845 break;
6847 case NE:
6848 /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
6849 in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
6850 which is equal to STORE_FLAG_VALUE. */
6851 if ((mask & ~ STORE_FLAG_VALUE) == 0 && XEXP (x, 1) == const0_rtx
6852 && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
6853 && nonzero_bits (XEXP (x, 0), mode) == STORE_FLAG_VALUE)
6854 return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
6856 break;
6858 case IF_THEN_ELSE:
6859 /* We have no way of knowing if the IF_THEN_ELSE can itself be
6860 written in a narrower mode. We play it safe and do not do so. */
6862 SUBST (XEXP (x, 1),
6863 gen_lowpart_for_combine (GET_MODE (x),
6864 force_to_mode (XEXP (x, 1), mode,
6865 mask, reg, next_select)));
6866 SUBST (XEXP (x, 2),
6867 gen_lowpart_for_combine (GET_MODE (x),
6868 force_to_mode (XEXP (x, 2), mode,
6869 mask, reg,next_select)));
6870 break;
6872 default:
6873 break;
6876 /* Ensure we return a value of the proper mode. */
6877 return gen_lowpart_for_combine (mode, x);
6880 /* Return nonzero if X is an expression that has one of two values depending on
6881 whether some other value is zero or nonzero. In that case, we return the
6882 value that is being tested, *PTRUE is set to the value if the rtx being
6883 returned has a nonzero value, and *PFALSE is set to the other alternative.
6885 If we return zero, we set *PTRUE and *PFALSE to X. */
6887 static rtx
6888 if_then_else_cond (x, ptrue, pfalse)
6889 rtx x;
6890 rtx *ptrue, *pfalse;
6892 enum machine_mode mode = GET_MODE (x);
6893 enum rtx_code code = GET_CODE (x);
6894 int size = GET_MODE_BITSIZE (mode);
6895 rtx cond0, cond1, true0, true1, false0, false1;
6896 unsigned HOST_WIDE_INT nz;
6898 /* If this is a unary operation whose operand has one of two values, apply
6899 our opcode to compute those values. */
6900 if (GET_RTX_CLASS (code) == '1'
6901 && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
6903 *ptrue = gen_unary (code, mode, GET_MODE (XEXP (x, 0)), true0);
6904 *pfalse = gen_unary (code, mode, GET_MODE (XEXP (x, 0)), false0);
6905 return cond0;
6908 /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
6909 make can't possibly match and would suppress other optimizations. */
6910 else if (code == COMPARE)
6913 /* If this is a binary operation, see if either side has only one of two
6914 values. If either one does or if both do and they are conditional on
6915 the same value, compute the new true and false values. */
6916 else if (GET_RTX_CLASS (code) == 'c' || GET_RTX_CLASS (code) == '2'
6917 || GET_RTX_CLASS (code) == '<')
6919 cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
6920 cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
6922 if ((cond0 != 0 || cond1 != 0)
6923 && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
6925 /* If if_then_else_cond returned zero, then true/false are the
6926 same rtl. We must copy one of them to prevent invalid rtl
6927 sharing. */
6928 if (cond0 == 0)
6929 true0 = copy_rtx (true0);
6930 else if (cond1 == 0)
6931 true1 = copy_rtx (true1);
6933 *ptrue = gen_binary (code, mode, true0, true1);
6934 *pfalse = gen_binary (code, mode, false0, false1);
6935 return cond0 ? cond0 : cond1;
6938 /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
6939 operands is zero when the other is non-zero, and vice-versa,
6940 and STORE_FLAG_VALUE is 1 or -1. */
6942 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
6943 && (code == PLUS || code == IOR || code == XOR || code == MINUS
6944 || code == UMAX)
6945 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
6947 rtx op0 = XEXP (XEXP (x, 0), 1);
6948 rtx op1 = XEXP (XEXP (x, 1), 1);
6950 cond0 = XEXP (XEXP (x, 0), 0);
6951 cond1 = XEXP (XEXP (x, 1), 0);
6953 if (GET_RTX_CLASS (GET_CODE (cond0)) == '<'
6954 && GET_RTX_CLASS (GET_CODE (cond1)) == '<'
6955 && reversible_comparison_p (cond1)
6956 && ((GET_CODE (cond0) == reverse_condition (GET_CODE (cond1))
6957 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
6958 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
6959 || ((swap_condition (GET_CODE (cond0))
6960 == reverse_condition (GET_CODE (cond1)))
6961 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
6962 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
6963 && ! side_effects_p (x))
6965 *ptrue = gen_binary (MULT, mode, op0, const_true_rtx);
6966 *pfalse = gen_binary (MULT, mode,
6967 (code == MINUS
6968 ? gen_unary (NEG, mode, mode, op1) : op1),
6969 const_true_rtx);
6970 return cond0;
6974 /* Similarly for MULT, AND and UMIN, execpt that for these the result
6975 is always zero. */
6976 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
6977 && (code == MULT || code == AND || code == UMIN)
6978 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
6980 cond0 = XEXP (XEXP (x, 0), 0);
6981 cond1 = XEXP (XEXP (x, 1), 0);
6983 if (GET_RTX_CLASS (GET_CODE (cond0)) == '<'
6984 && GET_RTX_CLASS (GET_CODE (cond1)) == '<'
6985 && reversible_comparison_p (cond1)
6986 && ((GET_CODE (cond0) == reverse_condition (GET_CODE (cond1))
6987 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
6988 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
6989 || ((swap_condition (GET_CODE (cond0))
6990 == reverse_condition (GET_CODE (cond1)))
6991 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
6992 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
6993 && ! side_effects_p (x))
6995 *ptrue = *pfalse = const0_rtx;
6996 return cond0;
7001 else if (code == IF_THEN_ELSE)
7003 /* If we have IF_THEN_ELSE already, extract the condition and
7004 canonicalize it if it is NE or EQ. */
7005 cond0 = XEXP (x, 0);
7006 *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
7007 if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
7008 return XEXP (cond0, 0);
7009 else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
7011 *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
7012 return XEXP (cond0, 0);
7014 else
7015 return cond0;
7018 /* If X is a normal SUBREG with both inner and outer modes integral,
7019 we can narrow both the true and false values of the inner expression,
7020 if there is a condition. */
7021 else if (code == SUBREG && GET_MODE_CLASS (mode) == MODE_INT
7022 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_INT
7023 && GET_MODE_SIZE (mode) <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))
7024 && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
7025 &true0, &false0)))
7027 *ptrue = force_to_mode (true0, mode, GET_MODE_MASK (mode), NULL_RTX, 0);
7028 *pfalse
7029 = force_to_mode (false0, mode, GET_MODE_MASK (mode), NULL_RTX, 0);
7031 return cond0;
7034 /* If X is a constant, this isn't special and will cause confusions
7035 if we treat it as such. Likewise if it is equivalent to a constant. */
7036 else if (CONSTANT_P (x)
7037 || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
7040 /* If X is known to be either 0 or -1, those are the true and
7041 false values when testing X. */
7042 else if (num_sign_bit_copies (x, mode) == size)
7044 *ptrue = constm1_rtx, *pfalse = const0_rtx;
7045 return x;
7048 /* Likewise for 0 or a single bit. */
7049 else if (exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
7051 *ptrue = GEN_INT (nz), *pfalse = const0_rtx;
7052 return x;
7055 /* Otherwise fail; show no condition with true and false values the same. */
7056 *ptrue = *pfalse = x;
7057 return 0;
7060 /* Return the value of expression X given the fact that condition COND
7061 is known to be true when applied to REG as its first operand and VAL
7062 as its second. X is known to not be shared and so can be modified in
7063 place.
7065 We only handle the simplest cases, and specifically those cases that
7066 arise with IF_THEN_ELSE expressions. */
7068 static rtx
7069 known_cond (x, cond, reg, val)
7070 rtx x;
7071 enum rtx_code cond;
7072 rtx reg, val;
7074 enum rtx_code code = GET_CODE (x);
7075 rtx temp;
7076 const char *fmt;
7077 int i, j;
7079 if (side_effects_p (x))
7080 return x;
7082 if (cond == EQ && rtx_equal_p (x, reg))
7083 return val;
7085 /* If X is (abs REG) and we know something about REG's relationship
7086 with zero, we may be able to simplify this. */
7088 if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
7089 switch (cond)
7091 case GE: case GT: case EQ:
7092 return XEXP (x, 0);
7093 case LT: case LE:
7094 return gen_unary (NEG, GET_MODE (XEXP (x, 0)), GET_MODE (XEXP (x, 0)),
7095 XEXP (x, 0));
7096 default:
7097 break;
7100 /* The only other cases we handle are MIN, MAX, and comparisons if the
7101 operands are the same as REG and VAL. */
7103 else if (GET_RTX_CLASS (code) == '<' || GET_RTX_CLASS (code) == 'c')
7105 if (rtx_equal_p (XEXP (x, 0), val))
7106 cond = swap_condition (cond), temp = val, val = reg, reg = temp;
7108 if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
7110 if (GET_RTX_CLASS (code) == '<')
7111 return (comparison_dominates_p (cond, code) ? const_true_rtx
7112 : (comparison_dominates_p (cond,
7113 reverse_condition (code))
7114 ? const0_rtx : x));
7116 else if (code == SMAX || code == SMIN
7117 || code == UMIN || code == UMAX)
7119 int unsignedp = (code == UMIN || code == UMAX);
7121 if (code == SMAX || code == UMAX)
7122 cond = reverse_condition (cond);
7124 switch (cond)
7126 case GE: case GT:
7127 return unsignedp ? x : XEXP (x, 1);
7128 case LE: case LT:
7129 return unsignedp ? x : XEXP (x, 0);
7130 case GEU: case GTU:
7131 return unsignedp ? XEXP (x, 1) : x;
7132 case LEU: case LTU:
7133 return unsignedp ? XEXP (x, 0) : x;
7134 default:
7135 break;
7141 fmt = GET_RTX_FORMAT (code);
7142 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7144 if (fmt[i] == 'e')
7145 SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
7146 else if (fmt[i] == 'E')
7147 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
7148 SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
7149 cond, reg, val));
7152 return x;
7155 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
7156 assignment as a field assignment. */
7158 static int
7159 rtx_equal_for_field_assignment_p (x, y)
7160 rtx x;
7161 rtx y;
7163 if (x == y || rtx_equal_p (x, y))
7164 return 1;
7166 if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
7167 return 0;
7169 /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
7170 Note that all SUBREGs of MEM are paradoxical; otherwise they
7171 would have been rewritten. */
7172 if (GET_CODE (x) == MEM && GET_CODE (y) == SUBREG
7173 && GET_CODE (SUBREG_REG (y)) == MEM
7174 && rtx_equal_p (SUBREG_REG (y),
7175 gen_lowpart_for_combine (GET_MODE (SUBREG_REG (y)), x)))
7176 return 1;
7178 if (GET_CODE (y) == MEM && GET_CODE (x) == SUBREG
7179 && GET_CODE (SUBREG_REG (x)) == MEM
7180 && rtx_equal_p (SUBREG_REG (x),
7181 gen_lowpart_for_combine (GET_MODE (SUBREG_REG (x)), y)))
7182 return 1;
7184 /* We used to see if get_last_value of X and Y were the same but that's
7185 not correct. In one direction, we'll cause the assignment to have
7186 the wrong destination and in the case, we'll import a register into this
7187 insn that might have already have been dead. So fail if none of the
7188 above cases are true. */
7189 return 0;
7192 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
7193 Return that assignment if so.
7195 We only handle the most common cases. */
7197 static rtx
7198 make_field_assignment (x)
7199 rtx x;
7201 rtx dest = SET_DEST (x);
7202 rtx src = SET_SRC (x);
7203 rtx assign;
7204 rtx rhs, lhs;
7205 HOST_WIDE_INT c1;
7206 int pos, len;
7207 rtx other;
7208 enum machine_mode mode;
7210 /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
7211 a clear of a one-bit field. We will have changed it to
7212 (and (rotate (const_int -2) POS) DEST), so check for that. Also check
7213 for a SUBREG. */
7215 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
7216 && GET_CODE (XEXP (XEXP (src, 0), 0)) == CONST_INT
7217 && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
7218 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7220 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
7221 1, 1, 1, 0);
7222 if (assign != 0)
7223 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
7224 return x;
7227 else if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
7228 && subreg_lowpart_p (XEXP (src, 0))
7229 && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
7230 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
7231 && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
7232 && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
7233 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7235 assign = make_extraction (VOIDmode, dest, 0,
7236 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
7237 1, 1, 1, 0);
7238 if (assign != 0)
7239 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
7240 return x;
7243 /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
7244 one-bit field. */
7245 else if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
7246 && XEXP (XEXP (src, 0), 0) == const1_rtx
7247 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7249 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
7250 1, 1, 1, 0);
7251 if (assign != 0)
7252 return gen_rtx_SET (VOIDmode, assign, const1_rtx);
7253 return x;
7256 /* The other case we handle is assignments into a constant-position
7257 field. They look like (ior/xor (and DEST C1) OTHER). If C1 represents
7258 a mask that has all one bits except for a group of zero bits and
7259 OTHER is known to have zeros where C1 has ones, this is such an
7260 assignment. Compute the position and length from C1. Shift OTHER
7261 to the appropriate position, force it to the required mode, and
7262 make the extraction. Check for the AND in both operands. */
7264 if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
7265 return x;
7267 rhs = expand_compound_operation (XEXP (src, 0));
7268 lhs = expand_compound_operation (XEXP (src, 1));
7270 if (GET_CODE (rhs) == AND
7271 && GET_CODE (XEXP (rhs, 1)) == CONST_INT
7272 && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
7273 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
7274 else if (GET_CODE (lhs) == AND
7275 && GET_CODE (XEXP (lhs, 1)) == CONST_INT
7276 && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
7277 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
7278 else
7279 return x;
7281 pos = get_pos_from_mask ((~ c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
7282 if (pos < 0 || pos + len > GET_MODE_BITSIZE (GET_MODE (dest))
7283 || GET_MODE_BITSIZE (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
7284 || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
7285 return x;
7287 assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
7288 if (assign == 0)
7289 return x;
7291 /* The mode to use for the source is the mode of the assignment, or of
7292 what is inside a possible STRICT_LOW_PART. */
7293 mode = (GET_CODE (assign) == STRICT_LOW_PART
7294 ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
7296 /* Shift OTHER right POS places and make it the source, restricting it
7297 to the proper length and mode. */
7299 src = force_to_mode (simplify_shift_const (NULL_RTX, LSHIFTRT,
7300 GET_MODE (src), other, pos),
7301 mode,
7302 GET_MODE_BITSIZE (mode) >= HOST_BITS_PER_WIDE_INT
7303 ? GET_MODE_MASK (mode)
7304 : ((HOST_WIDE_INT) 1 << len) - 1,
7305 dest, 0);
7307 return gen_rtx_combine (SET, VOIDmode, assign, src);
7310 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
7311 if so. */
7313 static rtx
7314 apply_distributive_law (x)
7315 rtx x;
7317 enum rtx_code code = GET_CODE (x);
7318 rtx lhs, rhs, other;
7319 rtx tem;
7320 enum rtx_code inner_code;
7322 /* Distributivity is not true for floating point.
7323 It can change the value. So don't do it.
7324 -- rms and moshier@world.std.com. */
7325 if (FLOAT_MODE_P (GET_MODE (x)))
7326 return x;
7328 /* The outer operation can only be one of the following: */
7329 if (code != IOR && code != AND && code != XOR
7330 && code != PLUS && code != MINUS)
7331 return x;
7333 lhs = XEXP (x, 0), rhs = XEXP (x, 1);
7335 /* If either operand is a primitive we can't do anything, so get out
7336 fast. */
7337 if (GET_RTX_CLASS (GET_CODE (lhs)) == 'o'
7338 || GET_RTX_CLASS (GET_CODE (rhs)) == 'o')
7339 return x;
7341 lhs = expand_compound_operation (lhs);
7342 rhs = expand_compound_operation (rhs);
7343 inner_code = GET_CODE (lhs);
7344 if (inner_code != GET_CODE (rhs))
7345 return x;
7347 /* See if the inner and outer operations distribute. */
7348 switch (inner_code)
7350 case LSHIFTRT:
7351 case ASHIFTRT:
7352 case AND:
7353 case IOR:
7354 /* These all distribute except over PLUS. */
7355 if (code == PLUS || code == MINUS)
7356 return x;
7357 break;
7359 case MULT:
7360 if (code != PLUS && code != MINUS)
7361 return x;
7362 break;
7364 case ASHIFT:
7365 /* This is also a multiply, so it distributes over everything. */
7366 break;
7368 case SUBREG:
7369 /* Non-paradoxical SUBREGs distributes over all operations, provided
7370 the inner modes and word numbers are the same, this is an extraction
7371 of a low-order part, we don't convert an fp operation to int or
7372 vice versa, and we would not be converting a single-word
7373 operation into a multi-word operation. The latter test is not
7374 required, but it prevents generating unneeded multi-word operations.
7375 Some of the previous tests are redundant given the latter test, but
7376 are retained because they are required for correctness.
7378 We produce the result slightly differently in this case. */
7380 if (GET_MODE (SUBREG_REG (lhs)) != GET_MODE (SUBREG_REG (rhs))
7381 || SUBREG_WORD (lhs) != SUBREG_WORD (rhs)
7382 || ! subreg_lowpart_p (lhs)
7383 || (GET_MODE_CLASS (GET_MODE (lhs))
7384 != GET_MODE_CLASS (GET_MODE (SUBREG_REG (lhs))))
7385 || (GET_MODE_SIZE (GET_MODE (lhs))
7386 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))))
7387 || GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))) > UNITS_PER_WORD)
7388 return x;
7390 tem = gen_binary (code, GET_MODE (SUBREG_REG (lhs)),
7391 SUBREG_REG (lhs), SUBREG_REG (rhs));
7392 return gen_lowpart_for_combine (GET_MODE (x), tem);
7394 default:
7395 return x;
7398 /* Set LHS and RHS to the inner operands (A and B in the example
7399 above) and set OTHER to the common operand (C in the example).
7400 These is only one way to do this unless the inner operation is
7401 commutative. */
7402 if (GET_RTX_CLASS (inner_code) == 'c'
7403 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
7404 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
7405 else if (GET_RTX_CLASS (inner_code) == 'c'
7406 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
7407 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
7408 else if (GET_RTX_CLASS (inner_code) == 'c'
7409 && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
7410 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
7411 else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
7412 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
7413 else
7414 return x;
7416 /* Form the new inner operation, seeing if it simplifies first. */
7417 tem = gen_binary (code, GET_MODE (x), lhs, rhs);
7419 /* There is one exception to the general way of distributing:
7420 (a ^ b) | (a ^ c) -> (~a) & (b ^ c) */
7421 if (code == XOR && inner_code == IOR)
7423 inner_code = AND;
7424 other = gen_unary (NOT, GET_MODE (x), GET_MODE (x), other);
7427 /* We may be able to continuing distributing the result, so call
7428 ourselves recursively on the inner operation before forming the
7429 outer operation, which we return. */
7430 return gen_binary (inner_code, GET_MODE (x),
7431 apply_distributive_law (tem), other);
7434 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
7435 in MODE.
7437 Return an equivalent form, if different from X. Otherwise, return X. If
7438 X is zero, we are to always construct the equivalent form. */
7440 static rtx
7441 simplify_and_const_int (x, mode, varop, constop)
7442 rtx x;
7443 enum machine_mode mode;
7444 rtx varop;
7445 unsigned HOST_WIDE_INT constop;
7447 unsigned HOST_WIDE_INT nonzero;
7448 int i;
7450 /* Simplify VAROP knowing that we will be only looking at some of the
7451 bits in it. */
7452 varop = force_to_mode (varop, mode, constop, NULL_RTX, 0);
7454 /* If VAROP is a CLOBBER, we will fail so return it; if it is a
7455 CONST_INT, we are done. */
7456 if (GET_CODE (varop) == CLOBBER || GET_CODE (varop) == CONST_INT)
7457 return varop;
7459 /* See what bits may be nonzero in VAROP. Unlike the general case of
7460 a call to nonzero_bits, here we don't care about bits outside
7461 MODE. */
7463 nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
7464 nonzero = trunc_int_for_mode (nonzero, mode);
7466 /* Turn off all bits in the constant that are known to already be zero.
7467 Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
7468 which is tested below. */
7470 constop &= nonzero;
7472 /* If we don't have any bits left, return zero. */
7473 if (constop == 0)
7474 return const0_rtx;
7476 /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
7477 a power of two, we can replace this with a ASHIFT. */
7478 if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
7479 && (i = exact_log2 (constop)) >= 0)
7480 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
7482 /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
7483 or XOR, then try to apply the distributive law. This may eliminate
7484 operations if either branch can be simplified because of the AND.
7485 It may also make some cases more complex, but those cases probably
7486 won't match a pattern either with or without this. */
7488 if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
7489 return
7490 gen_lowpart_for_combine
7491 (mode,
7492 apply_distributive_law
7493 (gen_binary (GET_CODE (varop), GET_MODE (varop),
7494 simplify_and_const_int (NULL_RTX, GET_MODE (varop),
7495 XEXP (varop, 0), constop),
7496 simplify_and_const_int (NULL_RTX, GET_MODE (varop),
7497 XEXP (varop, 1), constop))));
7499 /* Get VAROP in MODE. Try to get a SUBREG if not. Don't make a new SUBREG
7500 if we already had one (just check for the simplest cases). */
7501 if (x && GET_CODE (XEXP (x, 0)) == SUBREG
7502 && GET_MODE (XEXP (x, 0)) == mode
7503 && SUBREG_REG (XEXP (x, 0)) == varop)
7504 varop = XEXP (x, 0);
7505 else
7506 varop = gen_lowpart_for_combine (mode, varop);
7508 /* If we can't make the SUBREG, try to return what we were given. */
7509 if (GET_CODE (varop) == CLOBBER)
7510 return x ? x : varop;
7512 /* If we are only masking insignificant bits, return VAROP. */
7513 if (constop == nonzero)
7514 x = varop;
7516 /* Otherwise, return an AND. See how much, if any, of X we can use. */
7517 else if (x == 0 || GET_CODE (x) != AND || GET_MODE (x) != mode)
7518 x = gen_binary (AND, mode, varop, GEN_INT (constop));
7520 else
7522 if (GET_CODE (XEXP (x, 1)) != CONST_INT
7523 || (unsigned HOST_WIDE_INT) INTVAL (XEXP (x, 1)) != constop)
7524 SUBST (XEXP (x, 1), GEN_INT (constop));
7526 SUBST (XEXP (x, 0), varop);
7529 return x;
7532 /* We let num_sign_bit_copies recur into nonzero_bits as that is useful.
7533 We don't let nonzero_bits recur into num_sign_bit_copies, because that
7534 is less useful. We can't allow both, because that results in exponential
7535 run time recursion. There is a nullstone testcase that triggered
7536 this. This macro avoids accidental uses of num_sign_bit_copies. */
7537 #define num_sign_bit_copies()
7539 /* Given an expression, X, compute which bits in X can be non-zero.
7540 We don't care about bits outside of those defined in MODE.
7542 For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
7543 a shift, AND, or zero_extract, we can do better. */
7545 static unsigned HOST_WIDE_INT
7546 nonzero_bits (x, mode)
7547 rtx x;
7548 enum machine_mode mode;
7550 unsigned HOST_WIDE_INT nonzero = GET_MODE_MASK (mode);
7551 unsigned HOST_WIDE_INT inner_nz;
7552 enum rtx_code code;
7553 int mode_width = GET_MODE_BITSIZE (mode);
7554 rtx tem;
7556 /* For floating-point values, assume all bits are needed. */
7557 if (FLOAT_MODE_P (GET_MODE (x)) || FLOAT_MODE_P (mode))
7558 return nonzero;
7560 /* If X is wider than MODE, use its mode instead. */
7561 if (GET_MODE_BITSIZE (GET_MODE (x)) > mode_width)
7563 mode = GET_MODE (x);
7564 nonzero = GET_MODE_MASK (mode);
7565 mode_width = GET_MODE_BITSIZE (mode);
7568 if (mode_width > HOST_BITS_PER_WIDE_INT)
7569 /* Our only callers in this case look for single bit values. So
7570 just return the mode mask. Those tests will then be false. */
7571 return nonzero;
7573 #ifndef WORD_REGISTER_OPERATIONS
7574 /* If MODE is wider than X, but both are a single word for both the host
7575 and target machines, we can compute this from which bits of the
7576 object might be nonzero in its own mode, taking into account the fact
7577 that on many CISC machines, accessing an object in a wider mode
7578 causes the high-order bits to become undefined. So they are
7579 not known to be zero. */
7581 if (GET_MODE (x) != VOIDmode && GET_MODE (x) != mode
7582 && GET_MODE_BITSIZE (GET_MODE (x)) <= BITS_PER_WORD
7583 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
7584 && GET_MODE_BITSIZE (mode) > GET_MODE_BITSIZE (GET_MODE (x)))
7586 nonzero &= nonzero_bits (x, GET_MODE (x));
7587 nonzero |= GET_MODE_MASK (mode) & ~ GET_MODE_MASK (GET_MODE (x));
7588 return nonzero;
7590 #endif
7592 code = GET_CODE (x);
7593 switch (code)
7595 case REG:
7596 #ifdef POINTERS_EXTEND_UNSIGNED
7597 /* If pointers extend unsigned and this is a pointer in Pmode, say that
7598 all the bits above ptr_mode are known to be zero. */
7599 if (POINTERS_EXTEND_UNSIGNED && GET_MODE (x) == Pmode
7600 && REGNO_POINTER_FLAG (REGNO (x)))
7601 nonzero &= GET_MODE_MASK (ptr_mode);
7602 #endif
7604 #ifdef STACK_BOUNDARY
7605 /* If this is the stack pointer, we may know something about its
7606 alignment. If PUSH_ROUNDING is defined, it is possible for the
7607 stack to be momentarily aligned only to that amount, so we pick
7608 the least alignment. */
7610 /* We can't check for arg_pointer_rtx here, because it is not
7611 guaranteed to have as much alignment as the stack pointer.
7612 In particular, in the Irix6 n64 ABI, the stack has 128 bit
7613 alignment but the argument pointer has only 64 bit alignment. */
7615 if ((x == frame_pointer_rtx
7616 || x == stack_pointer_rtx
7617 || x == hard_frame_pointer_rtx
7618 || (REGNO (x) >= FIRST_VIRTUAL_REGISTER
7619 && REGNO (x) <= LAST_VIRTUAL_REGISTER))
7620 #ifdef STACK_BIAS
7621 && !STACK_BIAS
7622 #endif
7625 int sp_alignment = STACK_BOUNDARY / BITS_PER_UNIT;
7627 #ifdef PUSH_ROUNDING
7628 if (REGNO (x) == STACK_POINTER_REGNUM)
7629 sp_alignment = MIN (PUSH_ROUNDING (1), sp_alignment);
7630 #endif
7632 /* We must return here, otherwise we may get a worse result from
7633 one of the choices below. There is nothing useful below as
7634 far as the stack pointer is concerned. */
7635 return nonzero &= ~ (sp_alignment - 1);
7637 #endif
7639 /* If X is a register whose nonzero bits value is current, use it.
7640 Otherwise, if X is a register whose value we can find, use that
7641 value. Otherwise, use the previously-computed global nonzero bits
7642 for this register. */
7644 if (reg_last_set_value[REGNO (x)] != 0
7645 && reg_last_set_mode[REGNO (x)] == mode
7646 && (reg_last_set_label[REGNO (x)] == label_tick
7647 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
7648 && REG_N_SETS (REGNO (x)) == 1
7649 && ! REGNO_REG_SET_P (BASIC_BLOCK (0)->global_live_at_start,
7650 REGNO (x))))
7651 && INSN_CUID (reg_last_set[REGNO (x)]) < subst_low_cuid)
7652 return reg_last_set_nonzero_bits[REGNO (x)];
7654 tem = get_last_value (x);
7656 if (tem)
7658 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
7659 /* If X is narrower than MODE and TEM is a non-negative
7660 constant that would appear negative in the mode of X,
7661 sign-extend it for use in reg_nonzero_bits because some
7662 machines (maybe most) will actually do the sign-extension
7663 and this is the conservative approach.
7665 ??? For 2.5, try to tighten up the MD files in this regard
7666 instead of this kludge. */
7668 if (GET_MODE_BITSIZE (GET_MODE (x)) < mode_width
7669 && GET_CODE (tem) == CONST_INT
7670 && INTVAL (tem) > 0
7671 && 0 != (INTVAL (tem)
7672 & ((HOST_WIDE_INT) 1
7673 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
7674 tem = GEN_INT (INTVAL (tem)
7675 | ((HOST_WIDE_INT) (-1)
7676 << GET_MODE_BITSIZE (GET_MODE (x))));
7677 #endif
7678 return nonzero_bits (tem, mode);
7680 else if (nonzero_sign_valid && reg_nonzero_bits[REGNO (x)])
7681 return reg_nonzero_bits[REGNO (x)] & nonzero;
7682 else
7683 return nonzero;
7685 case CONST_INT:
7686 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
7687 /* If X is negative in MODE, sign-extend the value. */
7688 if (INTVAL (x) > 0 && mode_width < BITS_PER_WORD
7689 && 0 != (INTVAL (x) & ((HOST_WIDE_INT) 1 << (mode_width - 1))))
7690 return (INTVAL (x) | ((HOST_WIDE_INT) (-1) << mode_width));
7691 #endif
7693 return INTVAL (x);
7695 case MEM:
7696 #ifdef LOAD_EXTEND_OP
7697 /* In many, if not most, RISC machines, reading a byte from memory
7698 zeros the rest of the register. Noticing that fact saves a lot
7699 of extra zero-extends. */
7700 if (LOAD_EXTEND_OP (GET_MODE (x)) == ZERO_EXTEND)
7701 nonzero &= GET_MODE_MASK (GET_MODE (x));
7702 #endif
7703 break;
7705 case EQ: case NE:
7706 case GT: case GTU:
7707 case LT: case LTU:
7708 case GE: case GEU:
7709 case LE: case LEU:
7711 /* If this produces an integer result, we know which bits are set.
7712 Code here used to clear bits outside the mode of X, but that is
7713 now done above. */
7715 if (GET_MODE_CLASS (mode) == MODE_INT
7716 && mode_width <= HOST_BITS_PER_WIDE_INT)
7717 nonzero = STORE_FLAG_VALUE;
7718 break;
7720 case NEG:
7721 #if 0
7722 /* Disabled to avoid exponential mutual recursion between nonzero_bits
7723 and num_sign_bit_copies. */
7724 if (num_sign_bit_copies (XEXP (x, 0), GET_MODE (x))
7725 == GET_MODE_BITSIZE (GET_MODE (x)))
7726 nonzero = 1;
7727 #endif
7729 if (GET_MODE_SIZE (GET_MODE (x)) < mode_width)
7730 nonzero |= (GET_MODE_MASK (mode) & ~ GET_MODE_MASK (GET_MODE (x)));
7731 break;
7733 case ABS:
7734 #if 0
7735 /* Disabled to avoid exponential mutual recursion between nonzero_bits
7736 and num_sign_bit_copies. */
7737 if (num_sign_bit_copies (XEXP (x, 0), GET_MODE (x))
7738 == GET_MODE_BITSIZE (GET_MODE (x)))
7739 nonzero = 1;
7740 #endif
7741 break;
7743 case TRUNCATE:
7744 nonzero &= (nonzero_bits (XEXP (x, 0), mode) & GET_MODE_MASK (mode));
7745 break;
7747 case ZERO_EXTEND:
7748 nonzero &= nonzero_bits (XEXP (x, 0), mode);
7749 if (GET_MODE (XEXP (x, 0)) != VOIDmode)
7750 nonzero &= GET_MODE_MASK (GET_MODE (XEXP (x, 0)));
7751 break;
7753 case SIGN_EXTEND:
7754 /* If the sign bit is known clear, this is the same as ZERO_EXTEND.
7755 Otherwise, show all the bits in the outer mode but not the inner
7756 may be non-zero. */
7757 inner_nz = nonzero_bits (XEXP (x, 0), mode);
7758 if (GET_MODE (XEXP (x, 0)) != VOIDmode)
7760 inner_nz &= GET_MODE_MASK (GET_MODE (XEXP (x, 0)));
7761 if (inner_nz
7762 & (((HOST_WIDE_INT) 1
7763 << (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - 1))))
7764 inner_nz |= (GET_MODE_MASK (mode)
7765 & ~ GET_MODE_MASK (GET_MODE (XEXP (x, 0))));
7768 nonzero &= inner_nz;
7769 break;
7771 case AND:
7772 nonzero &= (nonzero_bits (XEXP (x, 0), mode)
7773 & nonzero_bits (XEXP (x, 1), mode));
7774 break;
7776 case XOR: case IOR:
7777 case UMIN: case UMAX: case SMIN: case SMAX:
7778 nonzero &= (nonzero_bits (XEXP (x, 0), mode)
7779 | nonzero_bits (XEXP (x, 1), mode));
7780 break;
7782 case PLUS: case MINUS:
7783 case MULT:
7784 case DIV: case UDIV:
7785 case MOD: case UMOD:
7786 /* We can apply the rules of arithmetic to compute the number of
7787 high- and low-order zero bits of these operations. We start by
7788 computing the width (position of the highest-order non-zero bit)
7789 and the number of low-order zero bits for each value. */
7791 unsigned HOST_WIDE_INT nz0 = nonzero_bits (XEXP (x, 0), mode);
7792 unsigned HOST_WIDE_INT nz1 = nonzero_bits (XEXP (x, 1), mode);
7793 int width0 = floor_log2 (nz0) + 1;
7794 int width1 = floor_log2 (nz1) + 1;
7795 int low0 = floor_log2 (nz0 & -nz0);
7796 int low1 = floor_log2 (nz1 & -nz1);
7797 HOST_WIDE_INT op0_maybe_minusp
7798 = (nz0 & ((HOST_WIDE_INT) 1 << (mode_width - 1)));
7799 HOST_WIDE_INT op1_maybe_minusp
7800 = (nz1 & ((HOST_WIDE_INT) 1 << (mode_width - 1)));
7801 int result_width = mode_width;
7802 int result_low = 0;
7804 switch (code)
7806 case PLUS:
7807 #ifdef STACK_BIAS
7808 if (STACK_BIAS
7809 && (XEXP (x, 0) == stack_pointer_rtx
7810 || XEXP (x, 0) == frame_pointer_rtx)
7811 && GET_CODE (XEXP (x, 1)) == CONST_INT)
7813 int sp_alignment = STACK_BOUNDARY / BITS_PER_UNIT;
7815 nz0 = (GET_MODE_MASK (mode) & ~ (sp_alignment - 1));
7816 nz1 = INTVAL (XEXP (x, 1)) - STACK_BIAS;
7817 width0 = floor_log2 (nz0) + 1;
7818 width1 = floor_log2 (nz1) + 1;
7819 low0 = floor_log2 (nz0 & -nz0);
7820 low1 = floor_log2 (nz1 & -nz1);
7822 #endif
7823 result_width = MAX (width0, width1) + 1;
7824 result_low = MIN (low0, low1);
7825 break;
7826 case MINUS:
7827 result_low = MIN (low0, low1);
7828 break;
7829 case MULT:
7830 result_width = width0 + width1;
7831 result_low = low0 + low1;
7832 break;
7833 case DIV:
7834 if (! op0_maybe_minusp && ! op1_maybe_minusp)
7835 result_width = width0;
7836 break;
7837 case UDIV:
7838 result_width = width0;
7839 break;
7840 case MOD:
7841 if (! op0_maybe_minusp && ! op1_maybe_minusp)
7842 result_width = MIN (width0, width1);
7843 result_low = MIN (low0, low1);
7844 break;
7845 case UMOD:
7846 result_width = MIN (width0, width1);
7847 result_low = MIN (low0, low1);
7848 break;
7849 default:
7850 abort ();
7853 if (result_width < mode_width)
7854 nonzero &= ((HOST_WIDE_INT) 1 << result_width) - 1;
7856 if (result_low > 0)
7857 nonzero &= ~ (((HOST_WIDE_INT) 1 << result_low) - 1);
7859 break;
7861 case ZERO_EXTRACT:
7862 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7863 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
7864 nonzero &= ((HOST_WIDE_INT) 1 << INTVAL (XEXP (x, 1))) - 1;
7865 break;
7867 case SUBREG:
7868 /* If this is a SUBREG formed for a promoted variable that has
7869 been zero-extended, we know that at least the high-order bits
7870 are zero, though others might be too. */
7872 if (SUBREG_PROMOTED_VAR_P (x) && SUBREG_PROMOTED_UNSIGNED_P (x))
7873 nonzero = (GET_MODE_MASK (GET_MODE (x))
7874 & nonzero_bits (SUBREG_REG (x), GET_MODE (x)));
7876 /* If the inner mode is a single word for both the host and target
7877 machines, we can compute this from which bits of the inner
7878 object might be nonzero. */
7879 if (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))) <= BITS_PER_WORD
7880 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x)))
7881 <= HOST_BITS_PER_WIDE_INT))
7883 nonzero &= nonzero_bits (SUBREG_REG (x), mode);
7885 #if defined (WORD_REGISTER_OPERATIONS) && defined (LOAD_EXTEND_OP)
7886 /* If this is a typical RISC machine, we only have to worry
7887 about the way loads are extended. */
7888 if (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (x))) == SIGN_EXTEND
7889 ? (nonzero
7890 & (1L << (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))) - 1)))
7891 : LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (x))) != ZERO_EXTEND)
7892 #endif
7894 /* On many CISC machines, accessing an object in a wider mode
7895 causes the high-order bits to become undefined. So they are
7896 not known to be zero. */
7897 if (GET_MODE_SIZE (GET_MODE (x))
7898 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
7899 nonzero |= (GET_MODE_MASK (GET_MODE (x))
7900 & ~ GET_MODE_MASK (GET_MODE (SUBREG_REG (x))));
7903 break;
7905 case ASHIFTRT:
7906 case LSHIFTRT:
7907 case ASHIFT:
7908 case ROTATE:
7909 /* The nonzero bits are in two classes: any bits within MODE
7910 that aren't in GET_MODE (x) are always significant. The rest of the
7911 nonzero bits are those that are significant in the operand of
7912 the shift when shifted the appropriate number of bits. This
7913 shows that high-order bits are cleared by the right shift and
7914 low-order bits by left shifts. */
7915 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7916 && INTVAL (XEXP (x, 1)) >= 0
7917 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
7919 enum machine_mode inner_mode = GET_MODE (x);
7920 int width = GET_MODE_BITSIZE (inner_mode);
7921 int count = INTVAL (XEXP (x, 1));
7922 unsigned HOST_WIDE_INT mode_mask = GET_MODE_MASK (inner_mode);
7923 unsigned HOST_WIDE_INT op_nonzero = nonzero_bits (XEXP (x, 0), mode);
7924 unsigned HOST_WIDE_INT inner = op_nonzero & mode_mask;
7925 unsigned HOST_WIDE_INT outer = 0;
7927 if (mode_width > width)
7928 outer = (op_nonzero & nonzero & ~ mode_mask);
7930 if (code == LSHIFTRT)
7931 inner >>= count;
7932 else if (code == ASHIFTRT)
7934 inner >>= count;
7936 /* If the sign bit may have been nonzero before the shift, we
7937 need to mark all the places it could have been copied to
7938 by the shift as possibly nonzero. */
7939 if (inner & ((HOST_WIDE_INT) 1 << (width - 1 - count)))
7940 inner |= (((HOST_WIDE_INT) 1 << count) - 1) << (width - count);
7942 else if (code == ASHIFT)
7943 inner <<= count;
7944 else
7945 inner = ((inner << (count % width)
7946 | (inner >> (width - (count % width)))) & mode_mask);
7948 nonzero &= (outer | inner);
7950 break;
7952 case FFS:
7953 /* This is at most the number of bits in the mode. */
7954 nonzero = ((HOST_WIDE_INT) 1 << (floor_log2 (mode_width) + 1)) - 1;
7955 break;
7957 case IF_THEN_ELSE:
7958 nonzero &= (nonzero_bits (XEXP (x, 1), mode)
7959 | nonzero_bits (XEXP (x, 2), mode));
7960 break;
7962 default:
7963 break;
7966 return nonzero;
7969 /* See the macro definition above. */
7970 #undef num_sign_bit_copies
7972 /* Return the number of bits at the high-order end of X that are known to
7973 be equal to the sign bit. X will be used in mode MODE; if MODE is
7974 VOIDmode, X will be used in its own mode. The returned value will always
7975 be between 1 and the number of bits in MODE. */
7977 static int
7978 num_sign_bit_copies (x, mode)
7979 rtx x;
7980 enum machine_mode mode;
7982 enum rtx_code code = GET_CODE (x);
7983 int bitwidth;
7984 int num0, num1, result;
7985 unsigned HOST_WIDE_INT nonzero;
7986 rtx tem;
7988 /* If we weren't given a mode, use the mode of X. If the mode is still
7989 VOIDmode, we don't know anything. Likewise if one of the modes is
7990 floating-point. */
7992 if (mode == VOIDmode)
7993 mode = GET_MODE (x);
7995 if (mode == VOIDmode || FLOAT_MODE_P (mode) || FLOAT_MODE_P (GET_MODE (x)))
7996 return 1;
7998 bitwidth = GET_MODE_BITSIZE (mode);
8000 /* For a smaller object, just ignore the high bits. */
8001 if (bitwidth < GET_MODE_BITSIZE (GET_MODE (x)))
8002 return MAX (1, (num_sign_bit_copies (x, GET_MODE (x))
8003 - (GET_MODE_BITSIZE (GET_MODE (x)) - bitwidth)));
8005 if (GET_MODE (x) != VOIDmode && bitwidth > GET_MODE_BITSIZE (GET_MODE (x)))
8007 #ifndef WORD_REGISTER_OPERATIONS
8008 /* If this machine does not do all register operations on the entire
8009 register and MODE is wider than the mode of X, we can say nothing
8010 at all about the high-order bits. */
8011 return 1;
8012 #else
8013 /* Likewise on machines that do, if the mode of the object is smaller
8014 than a word and loads of that size don't sign extend, we can say
8015 nothing about the high order bits. */
8016 if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
8017 #ifdef LOAD_EXTEND_OP
8018 && LOAD_EXTEND_OP (GET_MODE (x)) != SIGN_EXTEND
8019 #endif
8021 return 1;
8022 #endif
8025 switch (code)
8027 case REG:
8029 #ifdef POINTERS_EXTEND_UNSIGNED
8030 /* If pointers extend signed and this is a pointer in Pmode, say that
8031 all the bits above ptr_mode are known to be sign bit copies. */
8032 if (! POINTERS_EXTEND_UNSIGNED && GET_MODE (x) == Pmode && mode == Pmode
8033 && REGNO_POINTER_FLAG (REGNO (x)))
8034 return GET_MODE_BITSIZE (Pmode) - GET_MODE_BITSIZE (ptr_mode) + 1;
8035 #endif
8037 if (reg_last_set_value[REGNO (x)] != 0
8038 && reg_last_set_mode[REGNO (x)] == mode
8039 && (reg_last_set_label[REGNO (x)] == label_tick
8040 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
8041 && REG_N_SETS (REGNO (x)) == 1
8042 && ! REGNO_REG_SET_P (BASIC_BLOCK (0)->global_live_at_start,
8043 REGNO (x))))
8044 && INSN_CUID (reg_last_set[REGNO (x)]) < subst_low_cuid)
8045 return reg_last_set_sign_bit_copies[REGNO (x)];
8047 tem = get_last_value (x);
8048 if (tem != 0)
8049 return num_sign_bit_copies (tem, mode);
8051 if (nonzero_sign_valid && reg_sign_bit_copies[REGNO (x)] != 0)
8052 return reg_sign_bit_copies[REGNO (x)];
8053 break;
8055 case MEM:
8056 #ifdef LOAD_EXTEND_OP
8057 /* Some RISC machines sign-extend all loads of smaller than a word. */
8058 if (LOAD_EXTEND_OP (GET_MODE (x)) == SIGN_EXTEND)
8059 return MAX (1, bitwidth - GET_MODE_BITSIZE (GET_MODE (x)) + 1);
8060 #endif
8061 break;
8063 case CONST_INT:
8064 /* If the constant is negative, take its 1's complement and remask.
8065 Then see how many zero bits we have. */
8066 nonzero = INTVAL (x) & GET_MODE_MASK (mode);
8067 if (bitwidth <= HOST_BITS_PER_WIDE_INT
8068 && (nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
8069 nonzero = (~ nonzero) & GET_MODE_MASK (mode);
8071 return (nonzero == 0 ? bitwidth : bitwidth - floor_log2 (nonzero) - 1);
8073 case SUBREG:
8074 /* If this is a SUBREG for a promoted object that is sign-extended
8075 and we are looking at it in a wider mode, we know that at least the
8076 high-order bits are known to be sign bit copies. */
8078 if (SUBREG_PROMOTED_VAR_P (x) && ! SUBREG_PROMOTED_UNSIGNED_P (x))
8079 return MAX (bitwidth - GET_MODE_BITSIZE (GET_MODE (x)) + 1,
8080 num_sign_bit_copies (SUBREG_REG (x), mode));
8082 /* For a smaller object, just ignore the high bits. */
8083 if (bitwidth <= GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))))
8085 num0 = num_sign_bit_copies (SUBREG_REG (x), VOIDmode);
8086 return MAX (1, (num0
8087 - (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x)))
8088 - bitwidth)));
8091 #ifdef WORD_REGISTER_OPERATIONS
8092 #ifdef LOAD_EXTEND_OP
8093 /* For paradoxical SUBREGs on machines where all register operations
8094 affect the entire register, just look inside. Note that we are
8095 passing MODE to the recursive call, so the number of sign bit copies
8096 will remain relative to that mode, not the inner mode. */
8098 /* This works only if loads sign extend. Otherwise, if we get a
8099 reload for the inner part, it may be loaded from the stack, and
8100 then we lose all sign bit copies that existed before the store
8101 to the stack. */
8103 if ((GET_MODE_SIZE (GET_MODE (x))
8104 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
8105 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (x))) == SIGN_EXTEND)
8106 return num_sign_bit_copies (SUBREG_REG (x), mode);
8107 #endif
8108 #endif
8109 break;
8111 case SIGN_EXTRACT:
8112 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
8113 return MAX (1, bitwidth - INTVAL (XEXP (x, 1)));
8114 break;
8116 case SIGN_EXTEND:
8117 return (bitwidth - GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
8118 + num_sign_bit_copies (XEXP (x, 0), VOIDmode));
8120 case TRUNCATE:
8121 /* For a smaller object, just ignore the high bits. */
8122 num0 = num_sign_bit_copies (XEXP (x, 0), VOIDmode);
8123 return MAX (1, (num0 - (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
8124 - bitwidth)));
8126 case NOT:
8127 return num_sign_bit_copies (XEXP (x, 0), mode);
8129 case ROTATE: case ROTATERT:
8130 /* If we are rotating left by a number of bits less than the number
8131 of sign bit copies, we can just subtract that amount from the
8132 number. */
8133 if (GET_CODE (XEXP (x, 1)) == CONST_INT
8134 && INTVAL (XEXP (x, 1)) >= 0 && INTVAL (XEXP (x, 1)) < bitwidth)
8136 num0 = num_sign_bit_copies (XEXP (x, 0), mode);
8137 return MAX (1, num0 - (code == ROTATE ? INTVAL (XEXP (x, 1))
8138 : bitwidth - INTVAL (XEXP (x, 1))));
8140 break;
8142 case NEG:
8143 /* In general, this subtracts one sign bit copy. But if the value
8144 is known to be positive, the number of sign bit copies is the
8145 same as that of the input. Finally, if the input has just one bit
8146 that might be nonzero, all the bits are copies of the sign bit. */
8147 num0 = num_sign_bit_copies (XEXP (x, 0), mode);
8148 if (bitwidth > HOST_BITS_PER_WIDE_INT)
8149 return num0 > 1 ? num0 - 1 : 1;
8151 nonzero = nonzero_bits (XEXP (x, 0), mode);
8152 if (nonzero == 1)
8153 return bitwidth;
8155 if (num0 > 1
8156 && (((HOST_WIDE_INT) 1 << (bitwidth - 1)) & nonzero))
8157 num0--;
8159 return num0;
8161 case IOR: case AND: case XOR:
8162 case SMIN: case SMAX: case UMIN: case UMAX:
8163 /* Logical operations will preserve the number of sign-bit copies.
8164 MIN and MAX operations always return one of the operands. */
8165 num0 = num_sign_bit_copies (XEXP (x, 0), mode);
8166 num1 = num_sign_bit_copies (XEXP (x, 1), mode);
8167 return MIN (num0, num1);
8169 case PLUS: case MINUS:
8170 /* For addition and subtraction, we can have a 1-bit carry. However,
8171 if we are subtracting 1 from a positive number, there will not
8172 be such a carry. Furthermore, if the positive number is known to
8173 be 0 or 1, we know the result is either -1 or 0. */
8175 if (code == PLUS && XEXP (x, 1) == constm1_rtx
8176 && bitwidth <= HOST_BITS_PER_WIDE_INT)
8178 nonzero = nonzero_bits (XEXP (x, 0), mode);
8179 if ((((HOST_WIDE_INT) 1 << (bitwidth - 1)) & nonzero) == 0)
8180 return (nonzero == 1 || nonzero == 0 ? bitwidth
8181 : bitwidth - floor_log2 (nonzero) - 1);
8184 num0 = num_sign_bit_copies (XEXP (x, 0), mode);
8185 num1 = num_sign_bit_copies (XEXP (x, 1), mode);
8186 return MAX (1, MIN (num0, num1) - 1);
8188 case MULT:
8189 /* The number of bits of the product is the sum of the number of
8190 bits of both terms. However, unless one of the terms if known
8191 to be positive, we must allow for an additional bit since negating
8192 a negative number can remove one sign bit copy. */
8194 num0 = num_sign_bit_copies (XEXP (x, 0), mode);
8195 num1 = num_sign_bit_copies (XEXP (x, 1), mode);
8197 result = bitwidth - (bitwidth - num0) - (bitwidth - num1);
8198 if (result > 0
8199 && (bitwidth > HOST_BITS_PER_WIDE_INT
8200 || (((nonzero_bits (XEXP (x, 0), mode)
8201 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
8202 && ((nonzero_bits (XEXP (x, 1), mode)
8203 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0))))
8204 result--;
8206 return MAX (1, result);
8208 case UDIV:
8209 /* The result must be <= the first operand. If the first operand
8210 has the high bit set, we know nothing about the number of sign
8211 bit copies. */
8212 if (bitwidth > HOST_BITS_PER_WIDE_INT)
8213 return 1;
8214 else if ((nonzero_bits (XEXP (x, 0), mode)
8215 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
8216 return 1;
8217 else
8218 return num_sign_bit_copies (XEXP (x, 0), mode);
8220 case UMOD:
8221 /* The result must be <= the scond operand. */
8222 return num_sign_bit_copies (XEXP (x, 1), mode);
8224 case DIV:
8225 /* Similar to unsigned division, except that we have to worry about
8226 the case where the divisor is negative, in which case we have
8227 to add 1. */
8228 result = num_sign_bit_copies (XEXP (x, 0), mode);
8229 if (result > 1
8230 && (bitwidth > HOST_BITS_PER_WIDE_INT
8231 || (nonzero_bits (XEXP (x, 1), mode)
8232 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0))
8233 result--;
8235 return result;
8237 case MOD:
8238 result = num_sign_bit_copies (XEXP (x, 1), mode);
8239 if (result > 1
8240 && (bitwidth > HOST_BITS_PER_WIDE_INT
8241 || (nonzero_bits (XEXP (x, 1), mode)
8242 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0))
8243 result--;
8245 return result;
8247 case ASHIFTRT:
8248 /* Shifts by a constant add to the number of bits equal to the
8249 sign bit. */
8250 num0 = num_sign_bit_copies (XEXP (x, 0), mode);
8251 if (GET_CODE (XEXP (x, 1)) == CONST_INT
8252 && INTVAL (XEXP (x, 1)) > 0)
8253 num0 = MIN (bitwidth, num0 + INTVAL (XEXP (x, 1)));
8255 return num0;
8257 case ASHIFT:
8258 /* Left shifts destroy copies. */
8259 if (GET_CODE (XEXP (x, 1)) != CONST_INT
8260 || INTVAL (XEXP (x, 1)) < 0
8261 || INTVAL (XEXP (x, 1)) >= bitwidth)
8262 return 1;
8264 num0 = num_sign_bit_copies (XEXP (x, 0), mode);
8265 return MAX (1, num0 - INTVAL (XEXP (x, 1)));
8267 case IF_THEN_ELSE:
8268 num0 = num_sign_bit_copies (XEXP (x, 1), mode);
8269 num1 = num_sign_bit_copies (XEXP (x, 2), mode);
8270 return MIN (num0, num1);
8272 case EQ: case NE: case GE: case GT: case LE: case LT:
8273 case GEU: case GTU: case LEU: case LTU:
8274 if (STORE_FLAG_VALUE == -1)
8275 return bitwidth;
8276 break;
8278 default:
8279 break;
8282 /* If we haven't been able to figure it out by one of the above rules,
8283 see if some of the high-order bits are known to be zero. If so,
8284 count those bits and return one less than that amount. If we can't
8285 safely compute the mask for this mode, always return BITWIDTH. */
8287 if (bitwidth > HOST_BITS_PER_WIDE_INT)
8288 return 1;
8290 nonzero = nonzero_bits (x, mode);
8291 return (nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))
8292 ? 1 : bitwidth - floor_log2 (nonzero) - 1);
8295 /* Return the number of "extended" bits there are in X, when interpreted
8296 as a quantity in MODE whose signedness is indicated by UNSIGNEDP. For
8297 unsigned quantities, this is the number of high-order zero bits.
8298 For signed quantities, this is the number of copies of the sign bit
8299 minus 1. In both case, this function returns the number of "spare"
8300 bits. For example, if two quantities for which this function returns
8301 at least 1 are added, the addition is known not to overflow.
8303 This function will always return 0 unless called during combine, which
8304 implies that it must be called from a define_split. */
8307 extended_count (x, mode, unsignedp)
8308 rtx x;
8309 enum machine_mode mode;
8310 int unsignedp;
8312 if (nonzero_sign_valid == 0)
8313 return 0;
8315 return (unsignedp
8316 ? (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
8317 && (GET_MODE_BITSIZE (mode) - 1
8318 - floor_log2 (nonzero_bits (x, mode))))
8319 : num_sign_bit_copies (x, mode) - 1);
8322 /* This function is called from `simplify_shift_const' to merge two
8323 outer operations. Specifically, we have already found that we need
8324 to perform operation *POP0 with constant *PCONST0 at the outermost
8325 position. We would now like to also perform OP1 with constant CONST1
8326 (with *POP0 being done last).
8328 Return 1 if we can do the operation and update *POP0 and *PCONST0 with
8329 the resulting operation. *PCOMP_P is set to 1 if we would need to
8330 complement the innermost operand, otherwise it is unchanged.
8332 MODE is the mode in which the operation will be done. No bits outside
8333 the width of this mode matter. It is assumed that the width of this mode
8334 is smaller than or equal to HOST_BITS_PER_WIDE_INT.
8336 If *POP0 or OP1 are NIL, it means no operation is required. Only NEG, PLUS,
8337 IOR, XOR, and AND are supported. We may set *POP0 to SET if the proper
8338 result is simply *PCONST0.
8340 If the resulting operation cannot be expressed as one operation, we
8341 return 0 and do not change *POP0, *PCONST0, and *PCOMP_P. */
8343 static int
8344 merge_outer_ops (pop0, pconst0, op1, const1, mode, pcomp_p)
8345 enum rtx_code *pop0;
8346 HOST_WIDE_INT *pconst0;
8347 enum rtx_code op1;
8348 HOST_WIDE_INT const1;
8349 enum machine_mode mode;
8350 int *pcomp_p;
8352 enum rtx_code op0 = *pop0;
8353 HOST_WIDE_INT const0 = *pconst0;
8355 const0 &= GET_MODE_MASK (mode);
8356 const1 &= GET_MODE_MASK (mode);
8358 /* If OP0 is an AND, clear unimportant bits in CONST1. */
8359 if (op0 == AND)
8360 const1 &= const0;
8362 /* If OP0 or OP1 is NIL, this is easy. Similarly if they are the same or
8363 if OP0 is SET. */
8365 if (op1 == NIL || op0 == SET)
8366 return 1;
8368 else if (op0 == NIL)
8369 op0 = op1, const0 = const1;
8371 else if (op0 == op1)
8373 switch (op0)
8375 case AND:
8376 const0 &= const1;
8377 break;
8378 case IOR:
8379 const0 |= const1;
8380 break;
8381 case XOR:
8382 const0 ^= const1;
8383 break;
8384 case PLUS:
8385 const0 += const1;
8386 break;
8387 case NEG:
8388 op0 = NIL;
8389 break;
8390 default:
8391 break;
8395 /* Otherwise, if either is a PLUS or NEG, we can't do anything. */
8396 else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
8397 return 0;
8399 /* If the two constants aren't the same, we can't do anything. The
8400 remaining six cases can all be done. */
8401 else if (const0 != const1)
8402 return 0;
8404 else
8405 switch (op0)
8407 case IOR:
8408 if (op1 == AND)
8409 /* (a & b) | b == b */
8410 op0 = SET;
8411 else /* op1 == XOR */
8412 /* (a ^ b) | b == a | b */
8414 break;
8416 case XOR:
8417 if (op1 == AND)
8418 /* (a & b) ^ b == (~a) & b */
8419 op0 = AND, *pcomp_p = 1;
8420 else /* op1 == IOR */
8421 /* (a | b) ^ b == a & ~b */
8422 op0 = AND, *pconst0 = ~ const0;
8423 break;
8425 case AND:
8426 if (op1 == IOR)
8427 /* (a | b) & b == b */
8428 op0 = SET;
8429 else /* op1 == XOR */
8430 /* (a ^ b) & b) == (~a) & b */
8431 *pcomp_p = 1;
8432 break;
8433 default:
8434 break;
8437 /* Check for NO-OP cases. */
8438 const0 &= GET_MODE_MASK (mode);
8439 if (const0 == 0
8440 && (op0 == IOR || op0 == XOR || op0 == PLUS))
8441 op0 = NIL;
8442 else if (const0 == 0 && op0 == AND)
8443 op0 = SET;
8444 else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
8445 && op0 == AND)
8446 op0 = NIL;
8448 /* ??? Slightly redundant with the above mask, but not entirely.
8449 Moving this above means we'd have to sign-extend the mode mask
8450 for the final test. */
8451 const0 = trunc_int_for_mode (const0, mode);
8453 *pop0 = op0;
8454 *pconst0 = const0;
8456 return 1;
8459 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
8460 The result of the shift is RESULT_MODE. X, if non-zero, is an expression
8461 that we started with.
8463 The shift is normally computed in the widest mode we find in VAROP, as
8464 long as it isn't a different number of words than RESULT_MODE. Exceptions
8465 are ASHIFTRT and ROTATE, which are always done in their original mode, */
8467 static rtx
8468 simplify_shift_const (x, code, result_mode, varop, count)
8469 rtx x;
8470 enum rtx_code code;
8471 enum machine_mode result_mode;
8472 rtx varop;
8473 int count;
8475 enum rtx_code orig_code = code;
8476 int orig_count = count;
8477 enum machine_mode mode = result_mode;
8478 enum machine_mode shift_mode, tmode;
8479 int mode_words
8480 = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
8481 /* We form (outer_op (code varop count) (outer_const)). */
8482 enum rtx_code outer_op = NIL;
8483 HOST_WIDE_INT outer_const = 0;
8484 rtx const_rtx;
8485 int complement_p = 0;
8486 rtx new;
8488 /* If we were given an invalid count, don't do anything except exactly
8489 what was requested. */
8491 if (count < 0 || count > GET_MODE_BITSIZE (mode))
8493 if (x)
8494 return x;
8496 return gen_rtx_fmt_ee (code, mode, varop, GEN_INT (count));
8499 /* Unless one of the branches of the `if' in this loop does a `continue',
8500 we will `break' the loop after the `if'. */
8502 while (count != 0)
8504 /* If we have an operand of (clobber (const_int 0)), just return that
8505 value. */
8506 if (GET_CODE (varop) == CLOBBER)
8507 return varop;
8509 /* If we discovered we had to complement VAROP, leave. Making a NOT
8510 here would cause an infinite loop. */
8511 if (complement_p)
8512 break;
8514 /* Convert ROTATERT to ROTATE. */
8515 if (code == ROTATERT)
8516 code = ROTATE, count = GET_MODE_BITSIZE (result_mode) - count;
8518 /* We need to determine what mode we will do the shift in. If the
8519 shift is a right shift or a ROTATE, we must always do it in the mode
8520 it was originally done in. Otherwise, we can do it in MODE, the
8521 widest mode encountered. */
8522 shift_mode
8523 = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
8524 ? result_mode : mode);
8526 /* Handle cases where the count is greater than the size of the mode
8527 minus 1. For ASHIFT, use the size minus one as the count (this can
8528 occur when simplifying (lshiftrt (ashiftrt ..))). For rotates,
8529 take the count modulo the size. For other shifts, the result is
8530 zero.
8532 Since these shifts are being produced by the compiler by combining
8533 multiple operations, each of which are defined, we know what the
8534 result is supposed to be. */
8536 if (count > GET_MODE_BITSIZE (shift_mode) - 1)
8538 if (code == ASHIFTRT)
8539 count = GET_MODE_BITSIZE (shift_mode) - 1;
8540 else if (code == ROTATE || code == ROTATERT)
8541 count %= GET_MODE_BITSIZE (shift_mode);
8542 else
8544 /* We can't simply return zero because there may be an
8545 outer op. */
8546 varop = const0_rtx;
8547 count = 0;
8548 break;
8552 /* Negative counts are invalid and should not have been made (a
8553 programmer-specified negative count should have been handled
8554 above). */
8555 else if (count < 0)
8556 abort ();
8558 /* An arithmetic right shift of a quantity known to be -1 or 0
8559 is a no-op. */
8560 if (code == ASHIFTRT
8561 && (num_sign_bit_copies (varop, shift_mode)
8562 == GET_MODE_BITSIZE (shift_mode)))
8564 count = 0;
8565 break;
8568 /* If we are doing an arithmetic right shift and discarding all but
8569 the sign bit copies, this is equivalent to doing a shift by the
8570 bitsize minus one. Convert it into that shift because it will often
8571 allow other simplifications. */
8573 if (code == ASHIFTRT
8574 && (count + num_sign_bit_copies (varop, shift_mode)
8575 >= GET_MODE_BITSIZE (shift_mode)))
8576 count = GET_MODE_BITSIZE (shift_mode) - 1;
8578 /* We simplify the tests below and elsewhere by converting
8579 ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
8580 `make_compound_operation' will convert it to a ASHIFTRT for
8581 those machines (such as Vax) that don't have a LSHIFTRT. */
8582 if (GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
8583 && code == ASHIFTRT
8584 && ((nonzero_bits (varop, shift_mode)
8585 & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (shift_mode) - 1)))
8586 == 0))
8587 code = LSHIFTRT;
8589 switch (GET_CODE (varop))
8591 case SIGN_EXTEND:
8592 case ZERO_EXTEND:
8593 case SIGN_EXTRACT:
8594 case ZERO_EXTRACT:
8595 new = expand_compound_operation (varop);
8596 if (new != varop)
8598 varop = new;
8599 continue;
8601 break;
8603 case MEM:
8604 /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
8605 minus the width of a smaller mode, we can do this with a
8606 SIGN_EXTEND or ZERO_EXTEND from the narrower memory location. */
8607 if ((code == ASHIFTRT || code == LSHIFTRT)
8608 && ! mode_dependent_address_p (XEXP (varop, 0))
8609 && ! MEM_VOLATILE_P (varop)
8610 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
8611 MODE_INT, 1)) != BLKmode)
8613 if (BYTES_BIG_ENDIAN)
8614 new = gen_rtx_MEM (tmode, XEXP (varop, 0));
8615 else
8616 new = gen_rtx_MEM (tmode,
8617 plus_constant (XEXP (varop, 0),
8618 count / BITS_PER_UNIT));
8619 RTX_UNCHANGING_P (new) = RTX_UNCHANGING_P (varop);
8620 MEM_COPY_ATTRIBUTES (new, varop);
8621 varop = gen_rtx_combine (code == ASHIFTRT ? SIGN_EXTEND
8622 : ZERO_EXTEND, mode, new);
8623 count = 0;
8624 continue;
8626 break;
8628 case USE:
8629 /* Similar to the case above, except that we can only do this if
8630 the resulting mode is the same as that of the underlying
8631 MEM and adjust the address depending on the *bits* endianness
8632 because of the way that bit-field extract insns are defined. */
8633 if ((code == ASHIFTRT || code == LSHIFTRT)
8634 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
8635 MODE_INT, 1)) != BLKmode
8636 && tmode == GET_MODE (XEXP (varop, 0)))
8638 if (BITS_BIG_ENDIAN)
8639 new = XEXP (varop, 0);
8640 else
8642 new = copy_rtx (XEXP (varop, 0));
8643 SUBST (XEXP (new, 0),
8644 plus_constant (XEXP (new, 0),
8645 count / BITS_PER_UNIT));
8648 varop = gen_rtx_combine (code == ASHIFTRT ? SIGN_EXTEND
8649 : ZERO_EXTEND, mode, new);
8650 count = 0;
8651 continue;
8653 break;
8655 case SUBREG:
8656 /* If VAROP is a SUBREG, strip it as long as the inner operand has
8657 the same number of words as what we've seen so far. Then store
8658 the widest mode in MODE. */
8659 if (subreg_lowpart_p (varop)
8660 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
8661 > GET_MODE_SIZE (GET_MODE (varop)))
8662 && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
8663 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
8664 == mode_words))
8666 varop = SUBREG_REG (varop);
8667 if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
8668 mode = GET_MODE (varop);
8669 continue;
8671 break;
8673 case MULT:
8674 /* Some machines use MULT instead of ASHIFT because MULT
8675 is cheaper. But it is still better on those machines to
8676 merge two shifts into one. */
8677 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8678 && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
8680 varop = gen_binary (ASHIFT, GET_MODE (varop), XEXP (varop, 0),
8681 GEN_INT (exact_log2 (INTVAL (XEXP (varop, 1)))));
8682 continue;
8684 break;
8686 case UDIV:
8687 /* Similar, for when divides are cheaper. */
8688 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8689 && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
8691 varop = gen_binary (LSHIFTRT, GET_MODE (varop), XEXP (varop, 0),
8692 GEN_INT (exact_log2 (INTVAL (XEXP (varop, 1)))));
8693 continue;
8695 break;
8697 case ASHIFTRT:
8698 /* If we are extracting just the sign bit of an arithmetic right
8699 shift, that shift is not needed. */
8700 if (code == LSHIFTRT && count == GET_MODE_BITSIZE (result_mode) - 1)
8702 varop = XEXP (varop, 0);
8703 continue;
8706 /* ... fall through ... */
8708 case LSHIFTRT:
8709 case ASHIFT:
8710 case ROTATE:
8711 /* Here we have two nested shifts. The result is usually the
8712 AND of a new shift with a mask. We compute the result below. */
8713 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8714 && INTVAL (XEXP (varop, 1)) >= 0
8715 && INTVAL (XEXP (varop, 1)) < GET_MODE_BITSIZE (GET_MODE (varop))
8716 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
8717 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
8719 enum rtx_code first_code = GET_CODE (varop);
8720 int first_count = INTVAL (XEXP (varop, 1));
8721 unsigned HOST_WIDE_INT mask;
8722 rtx mask_rtx;
8724 /* We have one common special case. We can't do any merging if
8725 the inner code is an ASHIFTRT of a smaller mode. However, if
8726 we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
8727 with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
8728 we can convert it to
8729 (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0 C2) C3) C1).
8730 This simplifies certain SIGN_EXTEND operations. */
8731 if (code == ASHIFT && first_code == ASHIFTRT
8732 && (GET_MODE_BITSIZE (result_mode)
8733 - GET_MODE_BITSIZE (GET_MODE (varop))) == count)
8735 /* C3 has the low-order C1 bits zero. */
8737 mask = (GET_MODE_MASK (mode)
8738 & ~ (((HOST_WIDE_INT) 1 << first_count) - 1));
8740 varop = simplify_and_const_int (NULL_RTX, result_mode,
8741 XEXP (varop, 0), mask);
8742 varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
8743 varop, count);
8744 count = first_count;
8745 code = ASHIFTRT;
8746 continue;
8749 /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
8750 than C1 high-order bits equal to the sign bit, we can convert
8751 this to either an ASHIFT or a ASHIFTRT depending on the
8752 two counts.
8754 We cannot do this if VAROP's mode is not SHIFT_MODE. */
8756 if (code == ASHIFTRT && first_code == ASHIFT
8757 && GET_MODE (varop) == shift_mode
8758 && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
8759 > first_count))
8761 count -= first_count;
8762 if (count < 0)
8763 count = - count, code = ASHIFT;
8764 varop = XEXP (varop, 0);
8765 continue;
8768 /* There are some cases we can't do. If CODE is ASHIFTRT,
8769 we can only do this if FIRST_CODE is also ASHIFTRT.
8771 We can't do the case when CODE is ROTATE and FIRST_CODE is
8772 ASHIFTRT.
8774 If the mode of this shift is not the mode of the outer shift,
8775 we can't do this if either shift is a right shift or ROTATE.
8777 Finally, we can't do any of these if the mode is too wide
8778 unless the codes are the same.
8780 Handle the case where the shift codes are the same
8781 first. */
8783 if (code == first_code)
8785 if (GET_MODE (varop) != result_mode
8786 && (code == ASHIFTRT || code == LSHIFTRT
8787 || code == ROTATE))
8788 break;
8790 count += first_count;
8791 varop = XEXP (varop, 0);
8792 continue;
8795 if (code == ASHIFTRT
8796 || (code == ROTATE && first_code == ASHIFTRT)
8797 || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT
8798 || (GET_MODE (varop) != result_mode
8799 && (first_code == ASHIFTRT || first_code == LSHIFTRT
8800 || first_code == ROTATE
8801 || code == ROTATE)))
8802 break;
8804 /* To compute the mask to apply after the shift, shift the
8805 nonzero bits of the inner shift the same way the
8806 outer shift will. */
8808 mask_rtx = GEN_INT (nonzero_bits (varop, GET_MODE (varop)));
8810 mask_rtx
8811 = simplify_binary_operation (code, result_mode, mask_rtx,
8812 GEN_INT (count));
8814 /* Give up if we can't compute an outer operation to use. */
8815 if (mask_rtx == 0
8816 || GET_CODE (mask_rtx) != CONST_INT
8817 || ! merge_outer_ops (&outer_op, &outer_const, AND,
8818 INTVAL (mask_rtx),
8819 result_mode, &complement_p))
8820 break;
8822 /* If the shifts are in the same direction, we add the
8823 counts. Otherwise, we subtract them. */
8824 if ((code == ASHIFTRT || code == LSHIFTRT)
8825 == (first_code == ASHIFTRT || first_code == LSHIFTRT))
8826 count += first_count;
8827 else
8828 count -= first_count;
8830 /* If COUNT is positive, the new shift is usually CODE,
8831 except for the two exceptions below, in which case it is
8832 FIRST_CODE. If the count is negative, FIRST_CODE should
8833 always be used */
8834 if (count > 0
8835 && ((first_code == ROTATE && code == ASHIFT)
8836 || (first_code == ASHIFTRT && code == LSHIFTRT)))
8837 code = first_code;
8838 else if (count < 0)
8839 code = first_code, count = - count;
8841 varop = XEXP (varop, 0);
8842 continue;
8845 /* If we have (A << B << C) for any shift, we can convert this to
8846 (A << C << B). This wins if A is a constant. Only try this if
8847 B is not a constant. */
8849 else if (GET_CODE (varop) == code
8850 && GET_CODE (XEXP (varop, 1)) != CONST_INT
8851 && 0 != (new
8852 = simplify_binary_operation (code, mode,
8853 XEXP (varop, 0),
8854 GEN_INT (count))))
8856 varop = gen_rtx_combine (code, mode, new, XEXP (varop, 1));
8857 count = 0;
8858 continue;
8860 break;
8862 case NOT:
8863 /* Make this fit the case below. */
8864 varop = gen_rtx_combine (XOR, mode, XEXP (varop, 0),
8865 GEN_INT (GET_MODE_MASK (mode)));
8866 continue;
8868 case IOR:
8869 case AND:
8870 case XOR:
8871 /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
8872 with C the size of VAROP - 1 and the shift is logical if
8873 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
8874 we have an (le X 0) operation. If we have an arithmetic shift
8875 and STORE_FLAG_VALUE is 1 or we have a logical shift with
8876 STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation. */
8878 if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
8879 && XEXP (XEXP (varop, 0), 1) == constm1_rtx
8880 && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8881 && (code == LSHIFTRT || code == ASHIFTRT)
8882 && count == GET_MODE_BITSIZE (GET_MODE (varop)) - 1
8883 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
8885 count = 0;
8886 varop = gen_rtx_combine (LE, GET_MODE (varop), XEXP (varop, 1),
8887 const0_rtx);
8889 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
8890 varop = gen_rtx_combine (NEG, GET_MODE (varop), varop);
8892 continue;
8895 /* If we have (shift (logical)), move the logical to the outside
8896 to allow it to possibly combine with another logical and the
8897 shift to combine with another shift. This also canonicalizes to
8898 what a ZERO_EXTRACT looks like. Also, some machines have
8899 (and (shift)) insns. */
8901 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
8902 && (new = simplify_binary_operation (code, result_mode,
8903 XEXP (varop, 1),
8904 GEN_INT (count))) != 0
8905 && GET_CODE(new) == CONST_INT
8906 && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
8907 INTVAL (new), result_mode, &complement_p))
8909 varop = XEXP (varop, 0);
8910 continue;
8913 /* If we can't do that, try to simplify the shift in each arm of the
8914 logical expression, make a new logical expression, and apply
8915 the inverse distributive law. */
8917 rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
8918 XEXP (varop, 0), count);
8919 rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
8920 XEXP (varop, 1), count);
8922 varop = gen_binary (GET_CODE (varop), shift_mode, lhs, rhs);
8923 varop = apply_distributive_law (varop);
8925 count = 0;
8927 break;
8929 case EQ:
8930 /* convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
8931 says that the sign bit can be tested, FOO has mode MODE, C is
8932 GET_MODE_BITSIZE (MODE) - 1, and FOO has only its low-order bit
8933 that may be nonzero. */
8934 if (code == LSHIFTRT
8935 && XEXP (varop, 1) == const0_rtx
8936 && GET_MODE (XEXP (varop, 0)) == result_mode
8937 && count == GET_MODE_BITSIZE (result_mode) - 1
8938 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
8939 && ((STORE_FLAG_VALUE
8940 & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (result_mode) - 1))))
8941 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
8942 && merge_outer_ops (&outer_op, &outer_const, XOR,
8943 (HOST_WIDE_INT) 1, result_mode,
8944 &complement_p))
8946 varop = XEXP (varop, 0);
8947 count = 0;
8948 continue;
8950 break;
8952 case NEG:
8953 /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
8954 than the number of bits in the mode is equivalent to A. */
8955 if (code == LSHIFTRT && count == GET_MODE_BITSIZE (result_mode) - 1
8956 && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
8958 varop = XEXP (varop, 0);
8959 count = 0;
8960 continue;
8963 /* NEG commutes with ASHIFT since it is multiplication. Move the
8964 NEG outside to allow shifts to combine. */
8965 if (code == ASHIFT
8966 && merge_outer_ops (&outer_op, &outer_const, NEG,
8967 (HOST_WIDE_INT) 0, result_mode,
8968 &complement_p))
8970 varop = XEXP (varop, 0);
8971 continue;
8973 break;
8975 case PLUS:
8976 /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
8977 is one less than the number of bits in the mode is
8978 equivalent to (xor A 1). */
8979 if (code == LSHIFTRT && count == GET_MODE_BITSIZE (result_mode) - 1
8980 && XEXP (varop, 1) == constm1_rtx
8981 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
8982 && merge_outer_ops (&outer_op, &outer_const, XOR,
8983 (HOST_WIDE_INT) 1, result_mode,
8984 &complement_p))
8986 count = 0;
8987 varop = XEXP (varop, 0);
8988 continue;
8991 /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
8992 that might be nonzero in BAR are those being shifted out and those
8993 bits are known zero in FOO, we can replace the PLUS with FOO.
8994 Similarly in the other operand order. This code occurs when
8995 we are computing the size of a variable-size array. */
8997 if ((code == ASHIFTRT || code == LSHIFTRT)
8998 && count < HOST_BITS_PER_WIDE_INT
8999 && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
9000 && (nonzero_bits (XEXP (varop, 1), result_mode)
9001 & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
9003 varop = XEXP (varop, 0);
9004 continue;
9006 else if ((code == ASHIFTRT || code == LSHIFTRT)
9007 && count < HOST_BITS_PER_WIDE_INT
9008 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9009 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
9010 >> count)
9011 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
9012 & nonzero_bits (XEXP (varop, 1),
9013 result_mode)))
9015 varop = XEXP (varop, 1);
9016 continue;
9019 /* (ashift (plus foo C) N) is (plus (ashift foo N) C'). */
9020 if (code == ASHIFT
9021 && GET_CODE (XEXP (varop, 1)) == CONST_INT
9022 && (new = simplify_binary_operation (ASHIFT, result_mode,
9023 XEXP (varop, 1),
9024 GEN_INT (count))) != 0
9025 && GET_CODE(new) == CONST_INT
9026 && merge_outer_ops (&outer_op, &outer_const, PLUS,
9027 INTVAL (new), result_mode, &complement_p))
9029 varop = XEXP (varop, 0);
9030 continue;
9032 break;
9034 case MINUS:
9035 /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
9036 with C the size of VAROP - 1 and the shift is logical if
9037 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
9038 we have a (gt X 0) operation. If the shift is arithmetic with
9039 STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
9040 we have a (neg (gt X 0)) operation. */
9042 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9043 && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
9044 && count == GET_MODE_BITSIZE (GET_MODE (varop)) - 1
9045 && (code == LSHIFTRT || code == ASHIFTRT)
9046 && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
9047 && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
9048 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
9050 count = 0;
9051 varop = gen_rtx_combine (GT, GET_MODE (varop), XEXP (varop, 1),
9052 const0_rtx);
9054 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
9055 varop = gen_rtx_combine (NEG, GET_MODE (varop), varop);
9057 continue;
9059 break;
9061 case TRUNCATE:
9062 /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
9063 if the truncate does not affect the value. */
9064 if (code == LSHIFTRT
9065 && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
9066 && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
9067 && (INTVAL (XEXP (XEXP (varop, 0), 1))
9068 >= (GET_MODE_BITSIZE (GET_MODE (XEXP (varop, 0)))
9069 - GET_MODE_BITSIZE (GET_MODE (varop)))))
9071 rtx varop_inner = XEXP (varop, 0);
9073 varop_inner = gen_rtx_combine (LSHIFTRT,
9074 GET_MODE (varop_inner),
9075 XEXP (varop_inner, 0),
9076 GEN_INT (count + INTVAL (XEXP (varop_inner, 1))));
9077 varop = gen_rtx_combine (TRUNCATE, GET_MODE (varop),
9078 varop_inner);
9079 count = 0;
9080 continue;
9082 break;
9084 default:
9085 break;
9088 break;
9091 /* We need to determine what mode to do the shift in. If the shift is
9092 a right shift or ROTATE, we must always do it in the mode it was
9093 originally done in. Otherwise, we can do it in MODE, the widest mode
9094 encountered. The code we care about is that of the shift that will
9095 actually be done, not the shift that was originally requested. */
9096 shift_mode
9097 = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
9098 ? result_mode : mode);
9100 /* We have now finished analyzing the shift. The result should be
9101 a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places. If
9102 OUTER_OP is non-NIL, it is an operation that needs to be applied
9103 to the result of the shift. OUTER_CONST is the relevant constant,
9104 but we must turn off all bits turned off in the shift.
9106 If we were passed a value for X, see if we can use any pieces of
9107 it. If not, make new rtx. */
9109 if (x && GET_RTX_CLASS (GET_CODE (x)) == '2'
9110 && GET_CODE (XEXP (x, 1)) == CONST_INT
9111 && INTVAL (XEXP (x, 1)) == count)
9112 const_rtx = XEXP (x, 1);
9113 else
9114 const_rtx = GEN_INT (count);
9116 if (x && GET_CODE (XEXP (x, 0)) == SUBREG
9117 && GET_MODE (XEXP (x, 0)) == shift_mode
9118 && SUBREG_REG (XEXP (x, 0)) == varop)
9119 varop = XEXP (x, 0);
9120 else if (GET_MODE (varop) != shift_mode)
9121 varop = gen_lowpart_for_combine (shift_mode, varop);
9123 /* If we can't make the SUBREG, try to return what we were given. */
9124 if (GET_CODE (varop) == CLOBBER)
9125 return x ? x : varop;
9127 new = simplify_binary_operation (code, shift_mode, varop, const_rtx);
9128 if (new != 0)
9129 x = new;
9130 else
9132 if (x == 0 || GET_CODE (x) != code || GET_MODE (x) != shift_mode)
9133 x = gen_rtx_combine (code, shift_mode, varop, const_rtx);
9135 SUBST (XEXP (x, 0), varop);
9136 SUBST (XEXP (x, 1), const_rtx);
9139 /* If we have an outer operation and we just made a shift, it is
9140 possible that we could have simplified the shift were it not
9141 for the outer operation. So try to do the simplification
9142 recursively. */
9144 if (outer_op != NIL && GET_CODE (x) == code
9145 && GET_CODE (XEXP (x, 1)) == CONST_INT)
9146 x = simplify_shift_const (x, code, shift_mode, XEXP (x, 0),
9147 INTVAL (XEXP (x, 1)));
9149 /* If we were doing a LSHIFTRT in a wider mode than it was originally,
9150 turn off all the bits that the shift would have turned off. */
9151 if (orig_code == LSHIFTRT && result_mode != shift_mode)
9152 x = simplify_and_const_int (NULL_RTX, shift_mode, x,
9153 GET_MODE_MASK (result_mode) >> orig_count);
9155 /* Do the remainder of the processing in RESULT_MODE. */
9156 x = gen_lowpart_for_combine (result_mode, x);
9158 /* If COMPLEMENT_P is set, we have to complement X before doing the outer
9159 operation. */
9160 if (complement_p)
9161 x = gen_unary (NOT, result_mode, result_mode, x);
9163 if (outer_op != NIL)
9165 if (GET_MODE_BITSIZE (result_mode) < HOST_BITS_PER_WIDE_INT)
9166 outer_const = trunc_int_for_mode (outer_const, result_mode);
9168 if (outer_op == AND)
9169 x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
9170 else if (outer_op == SET)
9171 /* This means that we have determined that the result is
9172 equivalent to a constant. This should be rare. */
9173 x = GEN_INT (outer_const);
9174 else if (GET_RTX_CLASS (outer_op) == '1')
9175 x = gen_unary (outer_op, result_mode, result_mode, x);
9176 else
9177 x = gen_binary (outer_op, result_mode, x, GEN_INT (outer_const));
9180 return x;
9183 /* Like recog, but we receive the address of a pointer to a new pattern.
9184 We try to match the rtx that the pointer points to.
9185 If that fails, we may try to modify or replace the pattern,
9186 storing the replacement into the same pointer object.
9188 Modifications include deletion or addition of CLOBBERs.
9190 PNOTES is a pointer to a location where any REG_UNUSED notes added for
9191 the CLOBBERs are placed.
9193 The value is the final insn code from the pattern ultimately matched,
9194 or -1. */
9196 static int
9197 recog_for_combine (pnewpat, insn, pnotes)
9198 rtx *pnewpat;
9199 rtx insn;
9200 rtx *pnotes;
9202 register rtx pat = *pnewpat;
9203 int insn_code_number;
9204 int num_clobbers_to_add = 0;
9205 int i;
9206 rtx notes = 0;
9208 /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
9209 we use to indicate that something didn't match. If we find such a
9210 thing, force rejection. */
9211 if (GET_CODE (pat) == PARALLEL)
9212 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
9213 if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
9214 && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
9215 return -1;
9217 /* Is the result of combination a valid instruction? */
9218 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
9220 /* If it isn't, there is the possibility that we previously had an insn
9221 that clobbered some register as a side effect, but the combined
9222 insn doesn't need to do that. So try once more without the clobbers
9223 unless this represents an ASM insn. */
9225 if (insn_code_number < 0 && ! check_asm_operands (pat)
9226 && GET_CODE (pat) == PARALLEL)
9228 int pos;
9230 for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
9231 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
9233 if (i != pos)
9234 SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
9235 pos++;
9238 SUBST_INT (XVECLEN (pat, 0), pos);
9240 if (pos == 1)
9241 pat = XVECEXP (pat, 0, 0);
9243 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
9246 /* If we had any clobbers to add, make a new pattern than contains
9247 them. Then check to make sure that all of them are dead. */
9248 if (num_clobbers_to_add)
9250 rtx newpat = gen_rtx_PARALLEL (VOIDmode,
9251 gen_rtvec (GET_CODE (pat) == PARALLEL
9252 ? (XVECLEN (pat, 0)
9253 + num_clobbers_to_add)
9254 : num_clobbers_to_add + 1));
9256 if (GET_CODE (pat) == PARALLEL)
9257 for (i = 0; i < XVECLEN (pat, 0); i++)
9258 XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
9259 else
9260 XVECEXP (newpat, 0, 0) = pat;
9262 add_clobbers (newpat, insn_code_number);
9264 for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
9265 i < XVECLEN (newpat, 0); i++)
9267 if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) == REG
9268 && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
9269 return -1;
9270 notes = gen_rtx_EXPR_LIST (REG_UNUSED,
9271 XEXP (XVECEXP (newpat, 0, i), 0), notes);
9273 pat = newpat;
9276 *pnewpat = pat;
9277 *pnotes = notes;
9279 return insn_code_number;
9282 /* Like gen_lowpart but for use by combine. In combine it is not possible
9283 to create any new pseudoregs. However, it is safe to create
9284 invalid memory addresses, because combine will try to recognize
9285 them and all they will do is make the combine attempt fail.
9287 If for some reason this cannot do its job, an rtx
9288 (clobber (const_int 0)) is returned.
9289 An insn containing that will not be recognized. */
9291 #undef gen_lowpart
9293 static rtx
9294 gen_lowpart_for_combine (mode, x)
9295 enum machine_mode mode;
9296 register rtx x;
9298 rtx result;
9300 if (GET_MODE (x) == mode)
9301 return x;
9303 /* We can only support MODE being wider than a word if X is a
9304 constant integer or has a mode the same size. */
9306 if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
9307 && ! ((GET_MODE (x) == VOIDmode
9308 && (GET_CODE (x) == CONST_INT
9309 || GET_CODE (x) == CONST_DOUBLE))
9310 || GET_MODE_SIZE (GET_MODE (x)) == GET_MODE_SIZE (mode)))
9311 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
9313 /* X might be a paradoxical (subreg (mem)). In that case, gen_lowpart
9314 won't know what to do. So we will strip off the SUBREG here and
9315 process normally. */
9316 if (GET_CODE (x) == SUBREG && GET_CODE (SUBREG_REG (x)) == MEM)
9318 x = SUBREG_REG (x);
9319 if (GET_MODE (x) == mode)
9320 return x;
9323 result = gen_lowpart_common (mode, x);
9324 if (result != 0
9325 && GET_CODE (result) == SUBREG
9326 && GET_CODE (SUBREG_REG (result)) == REG
9327 && REGNO (SUBREG_REG (result)) >= FIRST_PSEUDO_REGISTER
9328 && (GET_MODE_SIZE (GET_MODE (result))
9329 != GET_MODE_SIZE (GET_MODE (SUBREG_REG (result)))))
9330 REG_CHANGES_SIZE (REGNO (SUBREG_REG (result))) = 1;
9332 if (result)
9333 return result;
9335 if (GET_CODE (x) == MEM)
9337 register int offset = 0;
9338 rtx new;
9340 /* Refuse to work on a volatile memory ref or one with a mode-dependent
9341 address. */
9342 if (MEM_VOLATILE_P (x) || mode_dependent_address_p (XEXP (x, 0)))
9343 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
9345 /* If we want to refer to something bigger than the original memref,
9346 generate a perverse subreg instead. That will force a reload
9347 of the original memref X. */
9348 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode))
9349 return gen_rtx_SUBREG (mode, x, 0);
9351 if (WORDS_BIG_ENDIAN)
9352 offset = (MAX (GET_MODE_SIZE (GET_MODE (x)), UNITS_PER_WORD)
9353 - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD));
9355 if (BYTES_BIG_ENDIAN)
9357 /* Adjust the address so that the address-after-the-data is
9358 unchanged. */
9359 offset -= (MIN (UNITS_PER_WORD, GET_MODE_SIZE (mode))
9360 - MIN (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (x))));
9362 new = gen_rtx_MEM (mode, plus_constant (XEXP (x, 0), offset));
9363 RTX_UNCHANGING_P (new) = RTX_UNCHANGING_P (x);
9364 MEM_COPY_ATTRIBUTES (new, x);
9365 return new;
9368 /* If X is a comparison operator, rewrite it in a new mode. This
9369 probably won't match, but may allow further simplifications. */
9370 else if (GET_RTX_CLASS (GET_CODE (x)) == '<')
9371 return gen_rtx_combine (GET_CODE (x), mode, XEXP (x, 0), XEXP (x, 1));
9373 /* If we couldn't simplify X any other way, just enclose it in a
9374 SUBREG. Normally, this SUBREG won't match, but some patterns may
9375 include an explicit SUBREG or we may simplify it further in combine. */
9376 else
9378 int word = 0;
9380 if (WORDS_BIG_ENDIAN && GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD)
9381 word = ((GET_MODE_SIZE (GET_MODE (x))
9382 - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD))
9383 / UNITS_PER_WORD);
9384 return gen_rtx_SUBREG (mode, x, word);
9388 /* Make an rtx expression. This is a subset of gen_rtx and only supports
9389 expressions of 1, 2, or 3 operands, each of which are rtx expressions.
9391 If the identical expression was previously in the insn (in the undobuf),
9392 it will be returned. Only if it is not found will a new expression
9393 be made. */
9395 /*VARARGS2*/
9396 static rtx
9397 gen_rtx_combine VPROTO((enum rtx_code code, enum machine_mode mode, ...))
9399 #ifndef ANSI_PROTOTYPES
9400 enum rtx_code code;
9401 enum machine_mode mode;
9402 #endif
9403 va_list p;
9404 int n_args;
9405 rtx args[3];
9406 int j;
9407 const char *fmt;
9408 rtx rt;
9409 struct undo *undo;
9411 VA_START (p, mode);
9413 #ifndef ANSI_PROTOTYPES
9414 code = va_arg (p, enum rtx_code);
9415 mode = va_arg (p, enum machine_mode);
9416 #endif
9418 n_args = GET_RTX_LENGTH (code);
9419 fmt = GET_RTX_FORMAT (code);
9421 if (n_args == 0 || n_args > 3)
9422 abort ();
9424 /* Get each arg and verify that it is supposed to be an expression. */
9425 for (j = 0; j < n_args; j++)
9427 if (*fmt++ != 'e')
9428 abort ();
9430 args[j] = va_arg (p, rtx);
9433 va_end (p);
9435 /* See if this is in undobuf. Be sure we don't use objects that came
9436 from another insn; this could produce circular rtl structures. */
9438 for (undo = undobuf.undos; undo != undobuf.previous_undos; undo = undo->next)
9439 if (!undo->is_int
9440 && GET_CODE (undo->old_contents.r) == code
9441 && GET_MODE (undo->old_contents.r) == mode)
9443 for (j = 0; j < n_args; j++)
9444 if (XEXP (undo->old_contents.r, j) != args[j])
9445 break;
9447 if (j == n_args)
9448 return undo->old_contents.r;
9451 /* Otherwise make a new rtx. We know we have 1, 2, or 3 args.
9452 Use rtx_alloc instead of gen_rtx because it's faster on RISC. */
9453 rt = rtx_alloc (code);
9454 PUT_MODE (rt, mode);
9455 XEXP (rt, 0) = args[0];
9456 if (n_args > 1)
9458 XEXP (rt, 1) = args[1];
9459 if (n_args > 2)
9460 XEXP (rt, 2) = args[2];
9462 return rt;
9465 /* These routines make binary and unary operations by first seeing if they
9466 fold; if not, a new expression is allocated. */
9468 static rtx
9469 gen_binary (code, mode, op0, op1)
9470 enum rtx_code code;
9471 enum machine_mode mode;
9472 rtx op0, op1;
9474 rtx result;
9475 rtx tem;
9477 if (GET_RTX_CLASS (code) == 'c'
9478 && (GET_CODE (op0) == CONST_INT
9479 || (CONSTANT_P (op0) && GET_CODE (op1) != CONST_INT)))
9480 tem = op0, op0 = op1, op1 = tem;
9482 if (GET_RTX_CLASS (code) == '<')
9484 enum machine_mode op_mode = GET_MODE (op0);
9486 /* Strip the COMPARE from (REL_OP (compare X Y) 0) to get
9487 just (REL_OP X Y). */
9488 if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
9490 op1 = XEXP (op0, 1);
9491 op0 = XEXP (op0, 0);
9492 op_mode = GET_MODE (op0);
9495 if (op_mode == VOIDmode)
9496 op_mode = GET_MODE (op1);
9497 result = simplify_relational_operation (code, op_mode, op0, op1);
9499 else
9500 result = simplify_binary_operation (code, mode, op0, op1);
9502 if (result)
9503 return result;
9505 /* Put complex operands first and constants second. */
9506 if (GET_RTX_CLASS (code) == 'c'
9507 && ((CONSTANT_P (op0) && GET_CODE (op1) != CONST_INT)
9508 || (GET_RTX_CLASS (GET_CODE (op0)) == 'o'
9509 && GET_RTX_CLASS (GET_CODE (op1)) != 'o')
9510 || (GET_CODE (op0) == SUBREG
9511 && GET_RTX_CLASS (GET_CODE (SUBREG_REG (op0))) == 'o'
9512 && GET_RTX_CLASS (GET_CODE (op1)) != 'o')))
9513 return gen_rtx_combine (code, mode, op1, op0);
9515 /* If we are turning off bits already known off in OP0, we need not do
9516 an AND. */
9517 else if (code == AND && GET_CODE (op1) == CONST_INT
9518 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
9519 && (nonzero_bits (op0, mode) & ~ INTVAL (op1)) == 0)
9520 return op0;
9522 return gen_rtx_combine (code, mode, op0, op1);
9525 static rtx
9526 gen_unary (code, mode, op0_mode, op0)
9527 enum rtx_code code;
9528 enum machine_mode mode, op0_mode;
9529 rtx op0;
9531 rtx result = simplify_unary_operation (code, mode, op0, op0_mode);
9533 if (result)
9534 return result;
9536 return gen_rtx_combine (code, mode, op0);
9539 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
9540 comparison code that will be tested.
9542 The result is a possibly different comparison code to use. *POP0 and
9543 *POP1 may be updated.
9545 It is possible that we might detect that a comparison is either always
9546 true or always false. However, we do not perform general constant
9547 folding in combine, so this knowledge isn't useful. Such tautologies
9548 should have been detected earlier. Hence we ignore all such cases. */
9550 static enum rtx_code
9551 simplify_comparison (code, pop0, pop1)
9552 enum rtx_code code;
9553 rtx *pop0;
9554 rtx *pop1;
9556 rtx op0 = *pop0;
9557 rtx op1 = *pop1;
9558 rtx tem, tem1;
9559 int i;
9560 enum machine_mode mode, tmode;
9562 /* Try a few ways of applying the same transformation to both operands. */
9563 while (1)
9565 #ifndef WORD_REGISTER_OPERATIONS
9566 /* The test below this one won't handle SIGN_EXTENDs on these machines,
9567 so check specially. */
9568 if (code != GTU && code != GEU && code != LTU && code != LEU
9569 && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
9570 && GET_CODE (XEXP (op0, 0)) == ASHIFT
9571 && GET_CODE (XEXP (op1, 0)) == ASHIFT
9572 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
9573 && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
9574 && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
9575 == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
9576 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9577 && GET_CODE (XEXP (op1, 1)) == CONST_INT
9578 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
9579 && GET_CODE (XEXP (XEXP (op1, 0), 1)) == CONST_INT
9580 && INTVAL (XEXP (op0, 1)) == INTVAL (XEXP (op1, 1))
9581 && INTVAL (XEXP (op0, 1)) == INTVAL (XEXP (XEXP (op0, 0), 1))
9582 && INTVAL (XEXP (op0, 1)) == INTVAL (XEXP (XEXP (op1, 0), 1))
9583 && (INTVAL (XEXP (op0, 1))
9584 == (GET_MODE_BITSIZE (GET_MODE (op0))
9585 - (GET_MODE_BITSIZE
9586 (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
9588 op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
9589 op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
9591 #endif
9593 /* If both operands are the same constant shift, see if we can ignore the
9594 shift. We can if the shift is a rotate or if the bits shifted out of
9595 this shift are known to be zero for both inputs and if the type of
9596 comparison is compatible with the shift. */
9597 if (GET_CODE (op0) == GET_CODE (op1)
9598 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
9599 && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
9600 || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
9601 && (code != GT && code != LT && code != GE && code != LE))
9602 || (GET_CODE (op0) == ASHIFTRT
9603 && (code != GTU && code != LTU
9604 && code != GEU && code != GEU)))
9605 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9606 && INTVAL (XEXP (op0, 1)) >= 0
9607 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
9608 && XEXP (op0, 1) == XEXP (op1, 1))
9610 enum machine_mode mode = GET_MODE (op0);
9611 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
9612 int shift_count = INTVAL (XEXP (op0, 1));
9614 if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
9615 mask &= (mask >> shift_count) << shift_count;
9616 else if (GET_CODE (op0) == ASHIFT)
9617 mask = (mask & (mask << shift_count)) >> shift_count;
9619 if ((nonzero_bits (XEXP (op0, 0), mode) & ~ mask) == 0
9620 && (nonzero_bits (XEXP (op1, 0), mode) & ~ mask) == 0)
9621 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
9622 else
9623 break;
9626 /* If both operands are AND's of a paradoxical SUBREG by constant, the
9627 SUBREGs are of the same mode, and, in both cases, the AND would
9628 be redundant if the comparison was done in the narrower mode,
9629 do the comparison in the narrower mode (e.g., we are AND'ing with 1
9630 and the operand's possibly nonzero bits are 0xffffff01; in that case
9631 if we only care about QImode, we don't need the AND). This case
9632 occurs if the output mode of an scc insn is not SImode and
9633 STORE_FLAG_VALUE == 1 (e.g., the 386).
9635 Similarly, check for a case where the AND's are ZERO_EXTEND
9636 operations from some narrower mode even though a SUBREG is not
9637 present. */
9639 else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
9640 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9641 && GET_CODE (XEXP (op1, 1)) == CONST_INT)
9643 rtx inner_op0 = XEXP (op0, 0);
9644 rtx inner_op1 = XEXP (op1, 0);
9645 HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
9646 HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
9647 int changed = 0;
9649 if (GET_CODE (inner_op0) == SUBREG && GET_CODE (inner_op1) == SUBREG
9650 && (GET_MODE_SIZE (GET_MODE (inner_op0))
9651 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (inner_op0))))
9652 && (GET_MODE (SUBREG_REG (inner_op0))
9653 == GET_MODE (SUBREG_REG (inner_op1)))
9654 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (inner_op0)))
9655 <= HOST_BITS_PER_WIDE_INT)
9656 && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
9657 GET_MODE (SUBREG_REG (inner_op0)))))
9658 && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
9659 GET_MODE (SUBREG_REG (inner_op1))))))
9661 op0 = SUBREG_REG (inner_op0);
9662 op1 = SUBREG_REG (inner_op1);
9664 /* The resulting comparison is always unsigned since we masked
9665 off the original sign bit. */
9666 code = unsigned_condition (code);
9668 changed = 1;
9671 else if (c0 == c1)
9672 for (tmode = GET_CLASS_NARROWEST_MODE
9673 (GET_MODE_CLASS (GET_MODE (op0)));
9674 tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
9675 if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
9677 op0 = gen_lowpart_for_combine (tmode, inner_op0);
9678 op1 = gen_lowpart_for_combine (tmode, inner_op1);
9679 code = unsigned_condition (code);
9680 changed = 1;
9681 break;
9684 if (! changed)
9685 break;
9688 /* If both operands are NOT, we can strip off the outer operation
9689 and adjust the comparison code for swapped operands; similarly for
9690 NEG, except that this must be an equality comparison. */
9691 else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
9692 || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
9693 && (code == EQ || code == NE)))
9694 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
9696 else
9697 break;
9700 /* If the first operand is a constant, swap the operands and adjust the
9701 comparison code appropriately, but don't do this if the second operand
9702 is already a constant integer. */
9703 if (CONSTANT_P (op0) && GET_CODE (op1) != CONST_INT)
9705 tem = op0, op0 = op1, op1 = tem;
9706 code = swap_condition (code);
9709 /* We now enter a loop during which we will try to simplify the comparison.
9710 For the most part, we only are concerned with comparisons with zero,
9711 but some things may really be comparisons with zero but not start
9712 out looking that way. */
9714 while (GET_CODE (op1) == CONST_INT)
9716 enum machine_mode mode = GET_MODE (op0);
9717 int mode_width = GET_MODE_BITSIZE (mode);
9718 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
9719 int equality_comparison_p;
9720 int sign_bit_comparison_p;
9721 int unsigned_comparison_p;
9722 HOST_WIDE_INT const_op;
9724 /* We only want to handle integral modes. This catches VOIDmode,
9725 CCmode, and the floating-point modes. An exception is that we
9726 can handle VOIDmode if OP0 is a COMPARE or a comparison
9727 operation. */
9729 if (GET_MODE_CLASS (mode) != MODE_INT
9730 && ! (mode == VOIDmode
9731 && (GET_CODE (op0) == COMPARE
9732 || GET_RTX_CLASS (GET_CODE (op0)) == '<')))
9733 break;
9735 /* Get the constant we are comparing against and turn off all bits
9736 not on in our mode. */
9737 const_op = INTVAL (op1);
9738 if (mode_width <= HOST_BITS_PER_WIDE_INT)
9739 const_op &= mask;
9741 /* If we are comparing against a constant power of two and the value
9742 being compared can only have that single bit nonzero (e.g., it was
9743 `and'ed with that bit), we can replace this with a comparison
9744 with zero. */
9745 if (const_op
9746 && (code == EQ || code == NE || code == GE || code == GEU
9747 || code == LT || code == LTU)
9748 && mode_width <= HOST_BITS_PER_WIDE_INT
9749 && exact_log2 (const_op) >= 0
9750 && nonzero_bits (op0, mode) == (unsigned HOST_WIDE_INT) const_op)
9752 code = (code == EQ || code == GE || code == GEU ? NE : EQ);
9753 op1 = const0_rtx, const_op = 0;
9756 /* Similarly, if we are comparing a value known to be either -1 or
9757 0 with -1, change it to the opposite comparison against zero. */
9759 if (const_op == -1
9760 && (code == EQ || code == NE || code == GT || code == LE
9761 || code == GEU || code == LTU)
9762 && num_sign_bit_copies (op0, mode) == mode_width)
9764 code = (code == EQ || code == LE || code == GEU ? NE : EQ);
9765 op1 = const0_rtx, const_op = 0;
9768 /* Do some canonicalizations based on the comparison code. We prefer
9769 comparisons against zero and then prefer equality comparisons.
9770 If we can reduce the size of a constant, we will do that too. */
9772 switch (code)
9774 case LT:
9775 /* < C is equivalent to <= (C - 1) */
9776 if (const_op > 0)
9778 const_op -= 1;
9779 op1 = GEN_INT (const_op);
9780 code = LE;
9781 /* ... fall through to LE case below. */
9783 else
9784 break;
9786 case LE:
9787 /* <= C is equivalent to < (C + 1); we do this for C < 0 */
9788 if (const_op < 0)
9790 const_op += 1;
9791 op1 = GEN_INT (const_op);
9792 code = LT;
9795 /* If we are doing a <= 0 comparison on a value known to have
9796 a zero sign bit, we can replace this with == 0. */
9797 else if (const_op == 0
9798 && mode_width <= HOST_BITS_PER_WIDE_INT
9799 && (nonzero_bits (op0, mode)
9800 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
9801 code = EQ;
9802 break;
9804 case GE:
9805 /* >= C is equivalent to > (C - 1). */
9806 if (const_op > 0)
9808 const_op -= 1;
9809 op1 = GEN_INT (const_op);
9810 code = GT;
9811 /* ... fall through to GT below. */
9813 else
9814 break;
9816 case GT:
9817 /* > C is equivalent to >= (C + 1); we do this for C < 0*/
9818 if (const_op < 0)
9820 const_op += 1;
9821 op1 = GEN_INT (const_op);
9822 code = GE;
9825 /* If we are doing a > 0 comparison on a value known to have
9826 a zero sign bit, we can replace this with != 0. */
9827 else if (const_op == 0
9828 && mode_width <= HOST_BITS_PER_WIDE_INT
9829 && (nonzero_bits (op0, mode)
9830 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
9831 code = NE;
9832 break;
9834 case LTU:
9835 /* < C is equivalent to <= (C - 1). */
9836 if (const_op > 0)
9838 const_op -= 1;
9839 op1 = GEN_INT (const_op);
9840 code = LEU;
9841 /* ... fall through ... */
9844 /* (unsigned) < 0x80000000 is equivalent to >= 0. */
9845 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9846 && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
9848 const_op = 0, op1 = const0_rtx;
9849 code = GE;
9850 break;
9852 else
9853 break;
9855 case LEU:
9856 /* unsigned <= 0 is equivalent to == 0 */
9857 if (const_op == 0)
9858 code = EQ;
9860 /* (unsigned) <= 0x7fffffff is equivalent to >= 0. */
9861 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9862 && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
9864 const_op = 0, op1 = const0_rtx;
9865 code = GE;
9867 break;
9869 case GEU:
9870 /* >= C is equivalent to < (C - 1). */
9871 if (const_op > 1)
9873 const_op -= 1;
9874 op1 = GEN_INT (const_op);
9875 code = GTU;
9876 /* ... fall through ... */
9879 /* (unsigned) >= 0x80000000 is equivalent to < 0. */
9880 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9881 && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
9883 const_op = 0, op1 = const0_rtx;
9884 code = LT;
9885 break;
9887 else
9888 break;
9890 case GTU:
9891 /* unsigned > 0 is equivalent to != 0 */
9892 if (const_op == 0)
9893 code = NE;
9895 /* (unsigned) > 0x7fffffff is equivalent to < 0. */
9896 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
9897 && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
9899 const_op = 0, op1 = const0_rtx;
9900 code = LT;
9902 break;
9904 default:
9905 break;
9908 /* Compute some predicates to simplify code below. */
9910 equality_comparison_p = (code == EQ || code == NE);
9911 sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
9912 unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
9913 || code == LEU);
9915 /* If this is a sign bit comparison and we can do arithmetic in
9916 MODE, say that we will only be needing the sign bit of OP0. */
9917 if (sign_bit_comparison_p
9918 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
9919 op0 = force_to_mode (op0, mode,
9920 ((HOST_WIDE_INT) 1
9921 << (GET_MODE_BITSIZE (mode) - 1)),
9922 NULL_RTX, 0);
9924 /* Now try cases based on the opcode of OP0. If none of the cases
9925 does a "continue", we exit this loop immediately after the
9926 switch. */
9928 switch (GET_CODE (op0))
9930 case ZERO_EXTRACT:
9931 /* If we are extracting a single bit from a variable position in
9932 a constant that has only a single bit set and are comparing it
9933 with zero, we can convert this into an equality comparison
9934 between the position and the location of the single bit. */
9936 if (GET_CODE (XEXP (op0, 0)) == CONST_INT
9937 && XEXP (op0, 1) == const1_rtx
9938 && equality_comparison_p && const_op == 0
9939 && (i = exact_log2 (INTVAL (XEXP (op0, 0)))) >= 0)
9941 if (BITS_BIG_ENDIAN)
9943 #ifdef HAVE_extzv
9944 mode = insn_data[(int) CODE_FOR_extzv].operand[1].mode;
9945 if (mode == VOIDmode)
9946 mode = word_mode;
9947 i = (GET_MODE_BITSIZE (mode) - 1 - i);
9948 #else
9949 i = BITS_PER_WORD - 1 - i;
9950 #endif
9953 op0 = XEXP (op0, 2);
9954 op1 = GEN_INT (i);
9955 const_op = i;
9957 /* Result is nonzero iff shift count is equal to I. */
9958 code = reverse_condition (code);
9959 continue;
9962 /* ... fall through ... */
9964 case SIGN_EXTRACT:
9965 tem = expand_compound_operation (op0);
9966 if (tem != op0)
9968 op0 = tem;
9969 continue;
9971 break;
9973 case NOT:
9974 /* If testing for equality, we can take the NOT of the constant. */
9975 if (equality_comparison_p
9976 && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
9978 op0 = XEXP (op0, 0);
9979 op1 = tem;
9980 continue;
9983 /* If just looking at the sign bit, reverse the sense of the
9984 comparison. */
9985 if (sign_bit_comparison_p)
9987 op0 = XEXP (op0, 0);
9988 code = (code == GE ? LT : GE);
9989 continue;
9991 break;
9993 case NEG:
9994 /* If testing for equality, we can take the NEG of the constant. */
9995 if (equality_comparison_p
9996 && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
9998 op0 = XEXP (op0, 0);
9999 op1 = tem;
10000 continue;
10003 /* The remaining cases only apply to comparisons with zero. */
10004 if (const_op != 0)
10005 break;
10007 /* When X is ABS or is known positive,
10008 (neg X) is < 0 if and only if X != 0. */
10010 if (sign_bit_comparison_p
10011 && (GET_CODE (XEXP (op0, 0)) == ABS
10012 || (mode_width <= HOST_BITS_PER_WIDE_INT
10013 && (nonzero_bits (XEXP (op0, 0), mode)
10014 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)))
10016 op0 = XEXP (op0, 0);
10017 code = (code == LT ? NE : EQ);
10018 continue;
10021 /* If we have NEG of something whose two high-order bits are the
10022 same, we know that "(-a) < 0" is equivalent to "a > 0". */
10023 if (num_sign_bit_copies (op0, mode) >= 2)
10025 op0 = XEXP (op0, 0);
10026 code = swap_condition (code);
10027 continue;
10029 break;
10031 case ROTATE:
10032 /* If we are testing equality and our count is a constant, we
10033 can perform the inverse operation on our RHS. */
10034 if (equality_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
10035 && (tem = simplify_binary_operation (ROTATERT, mode,
10036 op1, XEXP (op0, 1))) != 0)
10038 op0 = XEXP (op0, 0);
10039 op1 = tem;
10040 continue;
10043 /* If we are doing a < 0 or >= 0 comparison, it means we are testing
10044 a particular bit. Convert it to an AND of a constant of that
10045 bit. This will be converted into a ZERO_EXTRACT. */
10046 if (const_op == 0 && sign_bit_comparison_p
10047 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10048 && mode_width <= HOST_BITS_PER_WIDE_INT)
10050 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10051 ((HOST_WIDE_INT) 1
10052 << (mode_width - 1
10053 - INTVAL (XEXP (op0, 1)))));
10054 code = (code == LT ? NE : EQ);
10055 continue;
10058 /* ... fall through ... */
10060 case ABS:
10061 /* ABS is ignorable inside an equality comparison with zero. */
10062 if (const_op == 0 && equality_comparison_p)
10064 op0 = XEXP (op0, 0);
10065 continue;
10067 break;
10070 case SIGN_EXTEND:
10071 /* Can simplify (compare (zero/sign_extend FOO) CONST)
10072 to (compare FOO CONST) if CONST fits in FOO's mode and we
10073 are either testing inequality or have an unsigned comparison
10074 with ZERO_EXTEND or a signed comparison with SIGN_EXTEND. */
10075 if (! unsigned_comparison_p
10076 && (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10077 <= HOST_BITS_PER_WIDE_INT)
10078 && ((unsigned HOST_WIDE_INT) const_op
10079 < (((unsigned HOST_WIDE_INT) 1
10080 << (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))) - 1)))))
10082 op0 = XEXP (op0, 0);
10083 continue;
10085 break;
10087 case SUBREG:
10088 /* Check for the case where we are comparing A - C1 with C2,
10089 both constants are smaller than 1/2 the maximum positive
10090 value in MODE, and the comparison is equality or unsigned.
10091 In that case, if A is either zero-extended to MODE or has
10092 sufficient sign bits so that the high-order bit in MODE
10093 is a copy of the sign in the inner mode, we can prove that it is
10094 safe to do the operation in the wider mode. This simplifies
10095 many range checks. */
10097 if (mode_width <= HOST_BITS_PER_WIDE_INT
10098 && subreg_lowpart_p (op0)
10099 && GET_CODE (SUBREG_REG (op0)) == PLUS
10100 && GET_CODE (XEXP (SUBREG_REG (op0), 1)) == CONST_INT
10101 && INTVAL (XEXP (SUBREG_REG (op0), 1)) < 0
10102 && (- INTVAL (XEXP (SUBREG_REG (op0), 1))
10103 < (HOST_WIDE_INT)(GET_MODE_MASK (mode) / 2))
10104 && (unsigned HOST_WIDE_INT) const_op < GET_MODE_MASK (mode) / 2
10105 && (0 == (nonzero_bits (XEXP (SUBREG_REG (op0), 0),
10106 GET_MODE (SUBREG_REG (op0)))
10107 & ~ GET_MODE_MASK (mode))
10108 || (num_sign_bit_copies (XEXP (SUBREG_REG (op0), 0),
10109 GET_MODE (SUBREG_REG (op0)))
10110 > (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
10111 - GET_MODE_BITSIZE (mode)))))
10113 op0 = SUBREG_REG (op0);
10114 continue;
10117 /* If the inner mode is narrower and we are extracting the low part,
10118 we can treat the SUBREG as if it were a ZERO_EXTEND. */
10119 if (subreg_lowpart_p (op0)
10120 && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) < mode_width)
10121 /* Fall through */ ;
10122 else
10123 break;
10125 /* ... fall through ... */
10127 case ZERO_EXTEND:
10128 if ((unsigned_comparison_p || equality_comparison_p)
10129 && (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10130 <= HOST_BITS_PER_WIDE_INT)
10131 && ((unsigned HOST_WIDE_INT) const_op
10132 < GET_MODE_MASK (GET_MODE (XEXP (op0, 0)))))
10134 op0 = XEXP (op0, 0);
10135 continue;
10137 break;
10139 case PLUS:
10140 /* (eq (plus X A) B) -> (eq X (minus B A)). We can only do
10141 this for equality comparisons due to pathological cases involving
10142 overflows. */
10143 if (equality_comparison_p
10144 && 0 != (tem = simplify_binary_operation (MINUS, mode,
10145 op1, XEXP (op0, 1))))
10147 op0 = XEXP (op0, 0);
10148 op1 = tem;
10149 continue;
10152 /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0. */
10153 if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
10154 && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
10156 op0 = XEXP (XEXP (op0, 0), 0);
10157 code = (code == LT ? EQ : NE);
10158 continue;
10160 break;
10162 case MINUS:
10163 /* (eq (minus A B) C) -> (eq A (plus B C)) or
10164 (eq B (minus A C)), whichever simplifies. We can only do
10165 this for equality comparisons due to pathological cases involving
10166 overflows. */
10167 if (equality_comparison_p
10168 && 0 != (tem = simplify_binary_operation (PLUS, mode,
10169 XEXP (op0, 1), op1)))
10171 op0 = XEXP (op0, 0);
10172 op1 = tem;
10173 continue;
10176 if (equality_comparison_p
10177 && 0 != (tem = simplify_binary_operation (MINUS, mode,
10178 XEXP (op0, 0), op1)))
10180 op0 = XEXP (op0, 1);
10181 op1 = tem;
10182 continue;
10185 /* The sign bit of (minus (ashiftrt X C) X), where C is the number
10186 of bits in X minus 1, is one iff X > 0. */
10187 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
10188 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10189 && INTVAL (XEXP (XEXP (op0, 0), 1)) == mode_width - 1
10190 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10192 op0 = XEXP (op0, 1);
10193 code = (code == GE ? LE : GT);
10194 continue;
10196 break;
10198 case XOR:
10199 /* (eq (xor A B) C) -> (eq A (xor B C)). This is a simplification
10200 if C is zero or B is a constant. */
10201 if (equality_comparison_p
10202 && 0 != (tem = simplify_binary_operation (XOR, mode,
10203 XEXP (op0, 1), op1)))
10205 op0 = XEXP (op0, 0);
10206 op1 = tem;
10207 continue;
10209 break;
10211 case EQ: case NE:
10212 case LT: case LTU: case LE: case LEU:
10213 case GT: case GTU: case GE: case GEU:
10214 /* We can't do anything if OP0 is a condition code value, rather
10215 than an actual data value. */
10216 if (const_op != 0
10217 #ifdef HAVE_cc0
10218 || XEXP (op0, 0) == cc0_rtx
10219 #endif
10220 || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
10221 break;
10223 /* Get the two operands being compared. */
10224 if (GET_CODE (XEXP (op0, 0)) == COMPARE)
10225 tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
10226 else
10227 tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
10229 /* Check for the cases where we simply want the result of the
10230 earlier test or the opposite of that result. */
10231 if (code == NE
10232 || (code == EQ && reversible_comparison_p (op0))
10233 || (GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
10234 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10235 && (STORE_FLAG_VALUE
10236 & (((HOST_WIDE_INT) 1
10237 << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
10238 && (code == LT
10239 || (code == GE && reversible_comparison_p (op0)))))
10241 code = (code == LT || code == NE
10242 ? GET_CODE (op0) : reverse_condition (GET_CODE (op0)));
10243 op0 = tem, op1 = tem1;
10244 continue;
10246 break;
10248 case IOR:
10249 /* The sign bit of (ior (plus X (const_int -1)) X) is non-zero
10250 iff X <= 0. */
10251 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
10252 && XEXP (XEXP (op0, 0), 1) == constm1_rtx
10253 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10255 op0 = XEXP (op0, 1);
10256 code = (code == GE ? GT : LE);
10257 continue;
10259 break;
10261 case AND:
10262 /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1). This
10263 will be converted to a ZERO_EXTRACT later. */
10264 if (const_op == 0 && equality_comparison_p
10265 && GET_CODE (XEXP (op0, 0)) == ASHIFT
10266 && XEXP (XEXP (op0, 0), 0) == const1_rtx)
10268 op0 = simplify_and_const_int
10269 (op0, mode, gen_rtx_combine (LSHIFTRT, mode,
10270 XEXP (op0, 1),
10271 XEXP (XEXP (op0, 0), 1)),
10272 (HOST_WIDE_INT) 1);
10273 continue;
10276 /* If we are comparing (and (lshiftrt X C1) C2) for equality with
10277 zero and X is a comparison and C1 and C2 describe only bits set
10278 in STORE_FLAG_VALUE, we can compare with X. */
10279 if (const_op == 0 && equality_comparison_p
10280 && mode_width <= HOST_BITS_PER_WIDE_INT
10281 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10282 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
10283 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10284 && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
10285 && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
10287 mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
10288 << INTVAL (XEXP (XEXP (op0, 0), 1)));
10289 if ((~ STORE_FLAG_VALUE & mask) == 0
10290 && (GET_RTX_CLASS (GET_CODE (XEXP (XEXP (op0, 0), 0))) == '<'
10291 || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
10292 && GET_RTX_CLASS (GET_CODE (tem)) == '<')))
10294 op0 = XEXP (XEXP (op0, 0), 0);
10295 continue;
10299 /* If we are doing an equality comparison of an AND of a bit equal
10300 to the sign bit, replace this with a LT or GE comparison of
10301 the underlying value. */
10302 if (equality_comparison_p
10303 && const_op == 0
10304 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10305 && mode_width <= HOST_BITS_PER_WIDE_INT
10306 && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
10307 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
10309 op0 = XEXP (op0, 0);
10310 code = (code == EQ ? GE : LT);
10311 continue;
10314 /* If this AND operation is really a ZERO_EXTEND from a narrower
10315 mode, the constant fits within that mode, and this is either an
10316 equality or unsigned comparison, try to do this comparison in
10317 the narrower mode. */
10318 if ((equality_comparison_p || unsigned_comparison_p)
10319 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10320 && (i = exact_log2 ((INTVAL (XEXP (op0, 1))
10321 & GET_MODE_MASK (mode))
10322 + 1)) >= 0
10323 && const_op >> i == 0
10324 && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode)
10326 op0 = gen_lowpart_for_combine (tmode, XEXP (op0, 0));
10327 continue;
10330 /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1 fits
10331 in both M1 and M2 and the SUBREG is either paradoxical or
10332 represents the low part, permute the SUBREG and the AND and
10333 try again. */
10334 if (GET_CODE (XEXP (op0, 0)) == SUBREG
10335 && (0
10336 #ifdef WORD_REGISTER_OPERATIONS
10337 || ((mode_width
10338 > (GET_MODE_BITSIZE
10339 (GET_MODE (SUBREG_REG (XEXP (op0, 0))))))
10340 && mode_width <= BITS_PER_WORD)
10341 #endif
10342 || ((mode_width
10343 <= (GET_MODE_BITSIZE
10344 (GET_MODE (SUBREG_REG (XEXP (op0, 0))))))
10345 && subreg_lowpart_p (XEXP (op0, 0))))
10346 #ifndef WORD_REGISTER_OPERATIONS
10347 /* It is unsafe to commute the AND into the SUBREG if the SUBREG
10348 is paradoxical and WORD_REGISTER_OPERATIONS is not defined.
10349 As originally written the upper bits have a defined value
10350 due to the AND operation. However, if we commute the AND
10351 inside the SUBREG then they no longer have defined values
10352 and the meaning of the code has been changed. */
10353 && (GET_MODE_SIZE (GET_MODE (XEXP (op0, 0)))
10354 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (op0, 0)))))
10355 #endif
10356 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10357 && mode_width <= HOST_BITS_PER_WIDE_INT
10358 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (XEXP (op0, 0))))
10359 <= HOST_BITS_PER_WIDE_INT)
10360 && (INTVAL (XEXP (op0, 1)) & ~ mask) == 0
10361 && 0 == (~ GET_MODE_MASK (GET_MODE (SUBREG_REG (XEXP (op0, 0))))
10362 & INTVAL (XEXP (op0, 1)))
10363 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1)) != mask
10364 && ((unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1))
10365 != GET_MODE_MASK (GET_MODE (SUBREG_REG (XEXP (op0, 0))))))
10369 = gen_lowpart_for_combine
10370 (mode,
10371 gen_binary (AND, GET_MODE (SUBREG_REG (XEXP (op0, 0))),
10372 SUBREG_REG (XEXP (op0, 0)), XEXP (op0, 1)));
10373 continue;
10376 break;
10378 case ASHIFT:
10379 /* If we have (compare (ashift FOO N) (const_int C)) and
10380 the high order N bits of FOO (N+1 if an inequality comparison)
10381 are known to be zero, we can do this by comparing FOO with C
10382 shifted right N bits so long as the low-order N bits of C are
10383 zero. */
10384 if (GET_CODE (XEXP (op0, 1)) == CONST_INT
10385 && INTVAL (XEXP (op0, 1)) >= 0
10386 && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
10387 < HOST_BITS_PER_WIDE_INT)
10388 && ((const_op
10389 & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0)
10390 && mode_width <= HOST_BITS_PER_WIDE_INT
10391 && (nonzero_bits (XEXP (op0, 0), mode)
10392 & ~ (mask >> (INTVAL (XEXP (op0, 1))
10393 + ! equality_comparison_p))) == 0)
10395 /* We must perform a logical shift, not an arithmetic one,
10396 as we want the top N bits of C to be zero. */
10397 unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
10399 temp >>= INTVAL (XEXP (op0, 1));
10400 op1 = GEN_INT (trunc_int_for_mode (temp, mode));
10401 op0 = XEXP (op0, 0);
10402 continue;
10405 /* If we are doing a sign bit comparison, it means we are testing
10406 a particular bit. Convert it to the appropriate AND. */
10407 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
10408 && mode_width <= HOST_BITS_PER_WIDE_INT)
10410 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10411 ((HOST_WIDE_INT) 1
10412 << (mode_width - 1
10413 - INTVAL (XEXP (op0, 1)))));
10414 code = (code == LT ? NE : EQ);
10415 continue;
10418 /* If this an equality comparison with zero and we are shifting
10419 the low bit to the sign bit, we can convert this to an AND of the
10420 low-order bit. */
10421 if (const_op == 0 && equality_comparison_p
10422 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10423 && INTVAL (XEXP (op0, 1)) == mode_width - 1)
10425 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10426 (HOST_WIDE_INT) 1);
10427 continue;
10429 break;
10431 case ASHIFTRT:
10432 /* If this is an equality comparison with zero, we can do this
10433 as a logical shift, which might be much simpler. */
10434 if (equality_comparison_p && const_op == 0
10435 && GET_CODE (XEXP (op0, 1)) == CONST_INT)
10437 op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
10438 XEXP (op0, 0),
10439 INTVAL (XEXP (op0, 1)));
10440 continue;
10443 /* If OP0 is a sign extension and CODE is not an unsigned comparison,
10444 do the comparison in a narrower mode. */
10445 if (! unsigned_comparison_p
10446 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10447 && GET_CODE (XEXP (op0, 0)) == ASHIFT
10448 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
10449 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
10450 MODE_INT, 1)) != BLKmode
10451 && ((unsigned HOST_WIDE_INT) const_op <= GET_MODE_MASK (tmode)
10452 || ((unsigned HOST_WIDE_INT) - const_op
10453 <= GET_MODE_MASK (tmode))))
10455 op0 = gen_lowpart_for_combine (tmode, XEXP (XEXP (op0, 0), 0));
10456 continue;
10459 /* ... fall through ... */
10460 case LSHIFTRT:
10461 /* If we have (compare (xshiftrt FOO N) (const_int C)) and
10462 the low order N bits of FOO are known to be zero, we can do this
10463 by comparing FOO with C shifted left N bits so long as no
10464 overflow occurs. */
10465 if (GET_CODE (XEXP (op0, 1)) == CONST_INT
10466 && INTVAL (XEXP (op0, 1)) >= 0
10467 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
10468 && mode_width <= HOST_BITS_PER_WIDE_INT
10469 && (nonzero_bits (XEXP (op0, 0), mode)
10470 & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0
10471 && (const_op == 0
10472 || (floor_log2 (const_op) + INTVAL (XEXP (op0, 1))
10473 < mode_width)))
10475 const_op <<= INTVAL (XEXP (op0, 1));
10476 op1 = GEN_INT (const_op);
10477 op0 = XEXP (op0, 0);
10478 continue;
10481 /* If we are using this shift to extract just the sign bit, we
10482 can replace this with an LT or GE comparison. */
10483 if (const_op == 0
10484 && (equality_comparison_p || sign_bit_comparison_p)
10485 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10486 && INTVAL (XEXP (op0, 1)) == mode_width - 1)
10488 op0 = XEXP (op0, 0);
10489 code = (code == NE || code == GT ? LT : GE);
10490 continue;
10492 break;
10494 default:
10495 break;
10498 break;
10501 /* Now make any compound operations involved in this comparison. Then,
10502 check for an outmost SUBREG on OP0 that is not doing anything or is
10503 paradoxical. The latter case can only occur when it is known that the
10504 "extra" bits will be zero. Therefore, it is safe to remove the SUBREG.
10505 We can never remove a SUBREG for a non-equality comparison because the
10506 sign bit is in a different place in the underlying object. */
10508 op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
10509 op1 = make_compound_operation (op1, SET);
10511 if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
10512 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10513 && (code == NE || code == EQ)
10514 && ((GET_MODE_SIZE (GET_MODE (op0))
10515 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0))))))
10517 op0 = SUBREG_REG (op0);
10518 op1 = gen_lowpart_for_combine (GET_MODE (op0), op1);
10521 else if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
10522 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10523 && (code == NE || code == EQ)
10524 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
10525 <= HOST_BITS_PER_WIDE_INT)
10526 && (nonzero_bits (SUBREG_REG (op0), GET_MODE (SUBREG_REG (op0)))
10527 & ~ GET_MODE_MASK (GET_MODE (op0))) == 0
10528 && (tem = gen_lowpart_for_combine (GET_MODE (SUBREG_REG (op0)),
10529 op1),
10530 (nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
10531 & ~ GET_MODE_MASK (GET_MODE (op0))) == 0))
10532 op0 = SUBREG_REG (op0), op1 = tem;
10534 /* We now do the opposite procedure: Some machines don't have compare
10535 insns in all modes. If OP0's mode is an integer mode smaller than a
10536 word and we can't do a compare in that mode, see if there is a larger
10537 mode for which we can do the compare. There are a number of cases in
10538 which we can use the wider mode. */
10540 mode = GET_MODE (op0);
10541 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
10542 && GET_MODE_SIZE (mode) < UNITS_PER_WORD
10543 && cmp_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
10544 for (tmode = GET_MODE_WIDER_MODE (mode);
10545 (tmode != VOIDmode
10546 && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT);
10547 tmode = GET_MODE_WIDER_MODE (tmode))
10548 if (cmp_optab->handlers[(int) tmode].insn_code != CODE_FOR_nothing)
10550 /* If the only nonzero bits in OP0 and OP1 are those in the
10551 narrower mode and this is an equality or unsigned comparison,
10552 we can use the wider mode. Similarly for sign-extended
10553 values, in which case it is true for all comparisons. */
10554 if (((code == EQ || code == NE
10555 || code == GEU || code == GTU || code == LEU || code == LTU)
10556 && (nonzero_bits (op0, tmode) & ~ GET_MODE_MASK (mode)) == 0
10557 && (nonzero_bits (op1, tmode) & ~ GET_MODE_MASK (mode)) == 0)
10558 || ((num_sign_bit_copies (op0, tmode)
10559 > GET_MODE_BITSIZE (tmode) - GET_MODE_BITSIZE (mode))
10560 && (num_sign_bit_copies (op1, tmode)
10561 > GET_MODE_BITSIZE (tmode) - GET_MODE_BITSIZE (mode))))
10563 op0 = gen_lowpart_for_combine (tmode, op0);
10564 op1 = gen_lowpart_for_combine (tmode, op1);
10565 break;
10568 /* If this is a test for negative, we can make an explicit
10569 test of the sign bit. */
10571 if (op1 == const0_rtx && (code == LT || code == GE)
10572 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10574 op0 = gen_binary (AND, tmode,
10575 gen_lowpart_for_combine (tmode, op0),
10576 GEN_INT ((HOST_WIDE_INT) 1
10577 << (GET_MODE_BITSIZE (mode) - 1)));
10578 code = (code == LT) ? NE : EQ;
10579 break;
10583 #ifdef CANONICALIZE_COMPARISON
10584 /* If this machine only supports a subset of valid comparisons, see if we
10585 can convert an unsupported one into a supported one. */
10586 CANONICALIZE_COMPARISON (code, op0, op1);
10587 #endif
10589 *pop0 = op0;
10590 *pop1 = op1;
10592 return code;
10595 /* Return 1 if we know that X, a comparison operation, is not operating
10596 on a floating-point value or is EQ or NE, meaning that we can safely
10597 reverse it. */
10599 static int
10600 reversible_comparison_p (x)
10601 rtx x;
10603 if (TARGET_FLOAT_FORMAT != IEEE_FLOAT_FORMAT
10604 || flag_fast_math
10605 || GET_CODE (x) == NE || GET_CODE (x) == EQ)
10606 return 1;
10608 switch (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))))
10610 case MODE_INT:
10611 case MODE_PARTIAL_INT:
10612 case MODE_COMPLEX_INT:
10613 return 1;
10615 case MODE_CC:
10616 /* If the mode of the condition codes tells us that this is safe,
10617 we need look no further. */
10618 if (REVERSIBLE_CC_MODE (GET_MODE (XEXP (x, 0))))
10619 return 1;
10621 /* Otherwise try and find where the condition codes were last set and
10622 use that. */
10623 x = get_last_value (XEXP (x, 0));
10624 return (x && GET_CODE (x) == COMPARE
10625 && ! FLOAT_MODE_P (GET_MODE (XEXP (x, 0))));
10627 default:
10628 return 0;
10632 /* Utility function for following routine. Called when X is part of a value
10633 being stored into reg_last_set_value. Sets reg_last_set_table_tick
10634 for each register mentioned. Similar to mention_regs in cse.c */
10636 static void
10637 update_table_tick (x)
10638 rtx x;
10640 register enum rtx_code code = GET_CODE (x);
10641 register const char *fmt = GET_RTX_FORMAT (code);
10642 register int i;
10644 if (code == REG)
10646 int regno = REGNO (x);
10647 int endregno = regno + (regno < FIRST_PSEUDO_REGISTER
10648 ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
10650 for (i = regno; i < endregno; i++)
10651 reg_last_set_table_tick[i] = label_tick;
10653 return;
10656 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
10657 /* Note that we can't have an "E" in values stored; see
10658 get_last_value_validate. */
10659 if (fmt[i] == 'e')
10660 update_table_tick (XEXP (x, i));
10663 /* Record that REG is set to VALUE in insn INSN. If VALUE is zero, we
10664 are saying that the register is clobbered and we no longer know its
10665 value. If INSN is zero, don't update reg_last_set; this is only permitted
10666 with VALUE also zero and is used to invalidate the register. */
10668 static void
10669 record_value_for_reg (reg, insn, value)
10670 rtx reg;
10671 rtx insn;
10672 rtx value;
10674 int regno = REGNO (reg);
10675 int endregno = regno + (regno < FIRST_PSEUDO_REGISTER
10676 ? HARD_REGNO_NREGS (regno, GET_MODE (reg)) : 1);
10677 int i;
10679 /* If VALUE contains REG and we have a previous value for REG, substitute
10680 the previous value. */
10681 if (value && insn && reg_overlap_mentioned_p (reg, value))
10683 rtx tem;
10685 /* Set things up so get_last_value is allowed to see anything set up to
10686 our insn. */
10687 subst_low_cuid = INSN_CUID (insn);
10688 tem = get_last_value (reg);
10690 if (tem)
10691 value = replace_rtx (copy_rtx (value), reg, tem);
10694 /* For each register modified, show we don't know its value, that
10695 we don't know about its bitwise content, that its value has been
10696 updated, and that we don't know the location of the death of the
10697 register. */
10698 for (i = regno; i < endregno; i ++)
10700 if (insn)
10701 reg_last_set[i] = insn;
10702 reg_last_set_value[i] = 0;
10703 reg_last_set_mode[i] = 0;
10704 reg_last_set_nonzero_bits[i] = 0;
10705 reg_last_set_sign_bit_copies[i] = 0;
10706 reg_last_death[i] = 0;
10709 /* Mark registers that are being referenced in this value. */
10710 if (value)
10711 update_table_tick (value);
10713 /* Now update the status of each register being set.
10714 If someone is using this register in this block, set this register
10715 to invalid since we will get confused between the two lives in this
10716 basic block. This makes using this register always invalid. In cse, we
10717 scan the table to invalidate all entries using this register, but this
10718 is too much work for us. */
10720 for (i = regno; i < endregno; i++)
10722 reg_last_set_label[i] = label_tick;
10723 if (value && reg_last_set_table_tick[i] == label_tick)
10724 reg_last_set_invalid[i] = 1;
10725 else
10726 reg_last_set_invalid[i] = 0;
10729 /* The value being assigned might refer to X (like in "x++;"). In that
10730 case, we must replace it with (clobber (const_int 0)) to prevent
10731 infinite loops. */
10732 if (value && ! get_last_value_validate (&value, insn,
10733 reg_last_set_label[regno], 0))
10735 value = copy_rtx (value);
10736 if (! get_last_value_validate (&value, insn,
10737 reg_last_set_label[regno], 1))
10738 value = 0;
10741 /* For the main register being modified, update the value, the mode, the
10742 nonzero bits, and the number of sign bit copies. */
10744 reg_last_set_value[regno] = value;
10746 if (value)
10748 subst_low_cuid = INSN_CUID (insn);
10749 reg_last_set_mode[regno] = GET_MODE (reg);
10750 reg_last_set_nonzero_bits[regno] = nonzero_bits (value, GET_MODE (reg));
10751 reg_last_set_sign_bit_copies[regno]
10752 = num_sign_bit_copies (value, GET_MODE (reg));
10756 /* Used for communication between the following two routines. */
10757 static rtx record_dead_insn;
10759 /* Called via note_stores from record_dead_and_set_regs to handle one
10760 SET or CLOBBER in an insn. */
10762 static void
10763 record_dead_and_set_regs_1 (dest, setter)
10764 rtx dest, setter;
10766 if (GET_CODE (dest) == SUBREG)
10767 dest = SUBREG_REG (dest);
10769 if (GET_CODE (dest) == REG)
10771 /* If we are setting the whole register, we know its value. Otherwise
10772 show that we don't know the value. We can handle SUBREG in
10773 some cases. */
10774 if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
10775 record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
10776 else if (GET_CODE (setter) == SET
10777 && GET_CODE (SET_DEST (setter)) == SUBREG
10778 && SUBREG_REG (SET_DEST (setter)) == dest
10779 && GET_MODE_BITSIZE (GET_MODE (dest)) <= BITS_PER_WORD
10780 && subreg_lowpart_p (SET_DEST (setter)))
10781 record_value_for_reg (dest, record_dead_insn,
10782 gen_lowpart_for_combine (GET_MODE (dest),
10783 SET_SRC (setter)));
10784 else
10785 record_value_for_reg (dest, record_dead_insn, NULL_RTX);
10787 else if (GET_CODE (dest) == MEM
10788 /* Ignore pushes, they clobber nothing. */
10789 && ! push_operand (dest, GET_MODE (dest)))
10790 mem_last_set = INSN_CUID (record_dead_insn);
10793 /* Update the records of when each REG was most recently set or killed
10794 for the things done by INSN. This is the last thing done in processing
10795 INSN in the combiner loop.
10797 We update reg_last_set, reg_last_set_value, reg_last_set_mode,
10798 reg_last_set_nonzero_bits, reg_last_set_sign_bit_copies, reg_last_death,
10799 and also the similar information mem_last_set (which insn most recently
10800 modified memory) and last_call_cuid (which insn was the most recent
10801 subroutine call). */
10803 static void
10804 record_dead_and_set_regs (insn)
10805 rtx insn;
10807 register rtx link;
10808 int i;
10810 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
10812 if (REG_NOTE_KIND (link) == REG_DEAD
10813 && GET_CODE (XEXP (link, 0)) == REG)
10815 int regno = REGNO (XEXP (link, 0));
10816 int endregno
10817 = regno + (regno < FIRST_PSEUDO_REGISTER
10818 ? HARD_REGNO_NREGS (regno, GET_MODE (XEXP (link, 0)))
10819 : 1);
10821 for (i = regno; i < endregno; i++)
10822 reg_last_death[i] = insn;
10824 else if (REG_NOTE_KIND (link) == REG_INC)
10825 record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
10828 if (GET_CODE (insn) == CALL_INSN)
10830 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
10831 if (call_used_regs[i])
10833 reg_last_set_value[i] = 0;
10834 reg_last_set_mode[i] = 0;
10835 reg_last_set_nonzero_bits[i] = 0;
10836 reg_last_set_sign_bit_copies[i] = 0;
10837 reg_last_death[i] = 0;
10840 last_call_cuid = mem_last_set = INSN_CUID (insn);
10843 record_dead_insn = insn;
10844 note_stores (PATTERN (insn), record_dead_and_set_regs_1);
10847 /* Utility routine for the following function. Verify that all the registers
10848 mentioned in *LOC are valid when *LOC was part of a value set when
10849 label_tick == TICK. Return 0 if some are not.
10851 If REPLACE is non-zero, replace the invalid reference with
10852 (clobber (const_int 0)) and return 1. This replacement is useful because
10853 we often can get useful information about the form of a value (e.g., if
10854 it was produced by a shift that always produces -1 or 0) even though
10855 we don't know exactly what registers it was produced from. */
10857 static int
10858 get_last_value_validate (loc, insn, tick, replace)
10859 rtx *loc;
10860 rtx insn;
10861 int tick;
10862 int replace;
10864 rtx x = *loc;
10865 const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
10866 int len = GET_RTX_LENGTH (GET_CODE (x));
10867 int i;
10869 if (GET_CODE (x) == REG)
10871 int regno = REGNO (x);
10872 int endregno = regno + (regno < FIRST_PSEUDO_REGISTER
10873 ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
10874 int j;
10876 for (j = regno; j < endregno; j++)
10877 if (reg_last_set_invalid[j]
10878 /* If this is a pseudo-register that was only set once and not
10879 live at the beginning of the function, it is always valid. */
10880 || (! (regno >= FIRST_PSEUDO_REGISTER
10881 && REG_N_SETS (regno) == 1
10882 && ! REGNO_REG_SET_P (BASIC_BLOCK (0)->global_live_at_start, regno))
10883 && reg_last_set_label[j] > tick))
10885 if (replace)
10886 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
10887 return replace;
10890 return 1;
10892 /* If this is a memory reference, make sure that there were
10893 no stores after it that might have clobbered the value. We don't
10894 have alias info, so we assume any store invalidates it. */
10895 else if (GET_CODE (x) == MEM && ! RTX_UNCHANGING_P (x)
10896 && INSN_CUID (insn) <= mem_last_set)
10898 if (replace)
10899 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
10900 return replace;
10903 for (i = 0; i < len; i++)
10904 if ((fmt[i] == 'e'
10905 && get_last_value_validate (&XEXP (x, i), insn, tick, replace) == 0)
10906 /* Don't bother with these. They shouldn't occur anyway. */
10907 || fmt[i] == 'E')
10908 return 0;
10910 /* If we haven't found a reason for it to be invalid, it is valid. */
10911 return 1;
10914 /* Get the last value assigned to X, if known. Some registers
10915 in the value may be replaced with (clobber (const_int 0)) if their value
10916 is known longer known reliably. */
10918 static rtx
10919 get_last_value (x)
10920 rtx x;
10922 int regno;
10923 rtx value;
10925 /* If this is a non-paradoxical SUBREG, get the value of its operand and
10926 then convert it to the desired mode. If this is a paradoxical SUBREG,
10927 we cannot predict what values the "extra" bits might have. */
10928 if (GET_CODE (x) == SUBREG
10929 && subreg_lowpart_p (x)
10930 && (GET_MODE_SIZE (GET_MODE (x))
10931 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
10932 && (value = get_last_value (SUBREG_REG (x))) != 0)
10933 return gen_lowpart_for_combine (GET_MODE (x), value);
10935 if (GET_CODE (x) != REG)
10936 return 0;
10938 regno = REGNO (x);
10939 value = reg_last_set_value[regno];
10941 /* If we don't have a value, or if it isn't for this basic block and
10942 it's either a hard register, set more than once, or it's a live
10943 at the beginning of the function, return 0.
10945 Because if it's not live at the beginnning of the function then the reg
10946 is always set before being used (is never used without being set).
10947 And, if it's set only once, and it's always set before use, then all
10948 uses must have the same last value, even if it's not from this basic
10949 block. */
10951 if (value == 0
10952 || (reg_last_set_label[regno] != label_tick
10953 && (regno < FIRST_PSEUDO_REGISTER
10954 || REG_N_SETS (regno) != 1
10955 || REGNO_REG_SET_P (BASIC_BLOCK (0)->global_live_at_start, regno))))
10956 return 0;
10958 /* If the value was set in a later insn than the ones we are processing,
10959 we can't use it even if the register was only set once. */
10960 if (INSN_CUID (reg_last_set[regno]) >= subst_low_cuid)
10961 return 0;
10963 /* If the value has all its registers valid, return it. */
10964 if (get_last_value_validate (&value, reg_last_set[regno],
10965 reg_last_set_label[regno], 0))
10966 return value;
10968 /* Otherwise, make a copy and replace any invalid register with
10969 (clobber (const_int 0)). If that fails for some reason, return 0. */
10971 value = copy_rtx (value);
10972 if (get_last_value_validate (&value, reg_last_set[regno],
10973 reg_last_set_label[regno], 1))
10974 return value;
10976 return 0;
10979 /* Return nonzero if expression X refers to a REG or to memory
10980 that is set in an instruction more recent than FROM_CUID. */
10982 static int
10983 use_crosses_set_p (x, from_cuid)
10984 register rtx x;
10985 int from_cuid;
10987 register const char *fmt;
10988 register int i;
10989 register enum rtx_code code = GET_CODE (x);
10991 if (code == REG)
10993 register int regno = REGNO (x);
10994 int endreg = regno + (regno < FIRST_PSEUDO_REGISTER
10995 ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
10997 #ifdef PUSH_ROUNDING
10998 /* Don't allow uses of the stack pointer to be moved,
10999 because we don't know whether the move crosses a push insn. */
11000 if (regno == STACK_POINTER_REGNUM)
11001 return 1;
11002 #endif
11003 for (;regno < endreg; regno++)
11004 if (reg_last_set[regno]
11005 && INSN_CUID (reg_last_set[regno]) > from_cuid)
11006 return 1;
11007 return 0;
11010 if (code == MEM && mem_last_set > from_cuid)
11011 return 1;
11013 fmt = GET_RTX_FORMAT (code);
11015 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11017 if (fmt[i] == 'E')
11019 register int j;
11020 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
11021 if (use_crosses_set_p (XVECEXP (x, i, j), from_cuid))
11022 return 1;
11024 else if (fmt[i] == 'e'
11025 && use_crosses_set_p (XEXP (x, i), from_cuid))
11026 return 1;
11028 return 0;
11031 /* Define three variables used for communication between the following
11032 routines. */
11034 static int reg_dead_regno, reg_dead_endregno;
11035 static int reg_dead_flag;
11037 /* Function called via note_stores from reg_dead_at_p.
11039 If DEST is within [reg_dead_regno, reg_dead_endregno), set
11040 reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET. */
11042 static void
11043 reg_dead_at_p_1 (dest, x)
11044 rtx dest;
11045 rtx x;
11047 int regno, endregno;
11049 if (GET_CODE (dest) != REG)
11050 return;
11052 regno = REGNO (dest);
11053 endregno = regno + (regno < FIRST_PSEUDO_REGISTER
11054 ? HARD_REGNO_NREGS (regno, GET_MODE (dest)) : 1);
11056 if (reg_dead_endregno > regno && reg_dead_regno < endregno)
11057 reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
11060 /* Return non-zero if REG is known to be dead at INSN.
11062 We scan backwards from INSN. If we hit a REG_DEAD note or a CLOBBER
11063 referencing REG, it is dead. If we hit a SET referencing REG, it is
11064 live. Otherwise, see if it is live or dead at the start of the basic
11065 block we are in. Hard regs marked as being live in NEWPAT_USED_REGS
11066 must be assumed to be always live. */
11068 static int
11069 reg_dead_at_p (reg, insn)
11070 rtx reg;
11071 rtx insn;
11073 int block, i;
11075 /* Set variables for reg_dead_at_p_1. */
11076 reg_dead_regno = REGNO (reg);
11077 reg_dead_endregno = reg_dead_regno + (reg_dead_regno < FIRST_PSEUDO_REGISTER
11078 ? HARD_REGNO_NREGS (reg_dead_regno,
11079 GET_MODE (reg))
11080 : 1);
11082 reg_dead_flag = 0;
11084 /* Check that reg isn't mentioned in NEWPAT_USED_REGS. */
11085 if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
11087 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
11088 if (TEST_HARD_REG_BIT (newpat_used_regs, i))
11089 return 0;
11092 /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, label, or
11093 beginning of function. */
11094 for (; insn && GET_CODE (insn) != CODE_LABEL && GET_CODE (insn) != BARRIER;
11095 insn = prev_nonnote_insn (insn))
11097 note_stores (PATTERN (insn), reg_dead_at_p_1);
11098 if (reg_dead_flag)
11099 return reg_dead_flag == 1 ? 1 : 0;
11101 if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
11102 return 1;
11105 /* Get the basic block number that we were in. */
11106 if (insn == 0)
11107 block = 0;
11108 else
11110 for (block = 0; block < n_basic_blocks; block++)
11111 if (insn == BLOCK_HEAD (block))
11112 break;
11114 if (block == n_basic_blocks)
11115 return 0;
11118 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
11119 if (REGNO_REG_SET_P (BASIC_BLOCK (block)->global_live_at_start, i))
11120 return 0;
11122 return 1;
11125 /* Note hard registers in X that are used. This code is similar to
11126 that in flow.c, but much simpler since we don't care about pseudos. */
11128 static void
11129 mark_used_regs_combine (x)
11130 rtx x;
11132 register RTX_CODE code = GET_CODE (x);
11133 register int regno;
11134 int i;
11136 switch (code)
11138 case LABEL_REF:
11139 case SYMBOL_REF:
11140 case CONST_INT:
11141 case CONST:
11142 case CONST_DOUBLE:
11143 case PC:
11144 case ADDR_VEC:
11145 case ADDR_DIFF_VEC:
11146 case ASM_INPUT:
11147 #ifdef HAVE_cc0
11148 /* CC0 must die in the insn after it is set, so we don't need to take
11149 special note of it here. */
11150 case CC0:
11151 #endif
11152 return;
11154 case CLOBBER:
11155 /* If we are clobbering a MEM, mark any hard registers inside the
11156 address as used. */
11157 if (GET_CODE (XEXP (x, 0)) == MEM)
11158 mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
11159 return;
11161 case REG:
11162 regno = REGNO (x);
11163 /* A hard reg in a wide mode may really be multiple registers.
11164 If so, mark all of them just like the first. */
11165 if (regno < FIRST_PSEUDO_REGISTER)
11167 /* None of this applies to the stack, frame or arg pointers */
11168 if (regno == STACK_POINTER_REGNUM
11169 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
11170 || regno == HARD_FRAME_POINTER_REGNUM
11171 #endif
11172 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
11173 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
11174 #endif
11175 || regno == FRAME_POINTER_REGNUM)
11176 return;
11178 i = HARD_REGNO_NREGS (regno, GET_MODE (x));
11179 while (i-- > 0)
11180 SET_HARD_REG_BIT (newpat_used_regs, regno + i);
11182 return;
11184 case SET:
11186 /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
11187 the address. */
11188 register rtx testreg = SET_DEST (x);
11190 while (GET_CODE (testreg) == SUBREG
11191 || GET_CODE (testreg) == ZERO_EXTRACT
11192 || GET_CODE (testreg) == SIGN_EXTRACT
11193 || GET_CODE (testreg) == STRICT_LOW_PART)
11194 testreg = XEXP (testreg, 0);
11196 if (GET_CODE (testreg) == MEM)
11197 mark_used_regs_combine (XEXP (testreg, 0));
11199 mark_used_regs_combine (SET_SRC (x));
11201 return;
11203 default:
11204 break;
11207 /* Recursively scan the operands of this expression. */
11210 register const char *fmt = GET_RTX_FORMAT (code);
11212 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11214 if (fmt[i] == 'e')
11215 mark_used_regs_combine (XEXP (x, i));
11216 else if (fmt[i] == 'E')
11218 register int j;
11220 for (j = 0; j < XVECLEN (x, i); j++)
11221 mark_used_regs_combine (XVECEXP (x, i, j));
11228 /* Remove register number REGNO from the dead registers list of INSN.
11230 Return the note used to record the death, if there was one. */
11233 remove_death (regno, insn)
11234 int regno;
11235 rtx insn;
11237 register rtx note = find_regno_note (insn, REG_DEAD, regno);
11239 if (note)
11241 REG_N_DEATHS (regno)--;
11242 remove_note (insn, note);
11245 return note;
11248 /* For each register (hardware or pseudo) used within expression X, if its
11249 death is in an instruction with cuid between FROM_CUID (inclusive) and
11250 TO_INSN (exclusive), put a REG_DEAD note for that register in the
11251 list headed by PNOTES.
11253 That said, don't move registers killed by maybe_kill_insn.
11255 This is done when X is being merged by combination into TO_INSN. These
11256 notes will then be distributed as needed. */
11258 static void
11259 move_deaths (x, maybe_kill_insn, from_cuid, to_insn, pnotes)
11260 rtx x;
11261 rtx maybe_kill_insn;
11262 int from_cuid;
11263 rtx to_insn;
11264 rtx *pnotes;
11266 register const char *fmt;
11267 register int len, i;
11268 register enum rtx_code code = GET_CODE (x);
11270 if (code == REG)
11272 register int regno = REGNO (x);
11273 register rtx where_dead = reg_last_death[regno];
11274 register rtx before_dead, after_dead;
11276 /* Don't move the register if it gets killed in between from and to */
11277 if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
11278 && !reg_referenced_p (x, maybe_kill_insn))
11279 return;
11281 /* WHERE_DEAD could be a USE insn made by combine, so first we
11282 make sure that we have insns with valid INSN_CUID values. */
11283 before_dead = where_dead;
11284 while (before_dead && INSN_UID (before_dead) > max_uid_cuid)
11285 before_dead = PREV_INSN (before_dead);
11286 after_dead = where_dead;
11287 while (after_dead && INSN_UID (after_dead) > max_uid_cuid)
11288 after_dead = NEXT_INSN (after_dead);
11290 if (before_dead && after_dead
11291 && INSN_CUID (before_dead) >= from_cuid
11292 && (INSN_CUID (after_dead) < INSN_CUID (to_insn)
11293 || (where_dead != after_dead
11294 && INSN_CUID (after_dead) == INSN_CUID (to_insn))))
11296 rtx note = remove_death (regno, where_dead);
11298 /* It is possible for the call above to return 0. This can occur
11299 when reg_last_death points to I2 or I1 that we combined with.
11300 In that case make a new note.
11302 We must also check for the case where X is a hard register
11303 and NOTE is a death note for a range of hard registers
11304 including X. In that case, we must put REG_DEAD notes for
11305 the remaining registers in place of NOTE. */
11307 if (note != 0 && regno < FIRST_PSEUDO_REGISTER
11308 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
11309 > GET_MODE_SIZE (GET_MODE (x))))
11311 int deadregno = REGNO (XEXP (note, 0));
11312 int deadend
11313 = (deadregno + HARD_REGNO_NREGS (deadregno,
11314 GET_MODE (XEXP (note, 0))));
11315 int ourend = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
11316 int i;
11318 for (i = deadregno; i < deadend; i++)
11319 if (i < regno || i >= ourend)
11320 REG_NOTES (where_dead)
11321 = gen_rtx_EXPR_LIST (REG_DEAD,
11322 gen_rtx_REG (reg_raw_mode[i], i),
11323 REG_NOTES (where_dead));
11325 /* If we didn't find any note, or if we found a REG_DEAD note that
11326 covers only part of the given reg, and we have a multi-reg hard
11327 register, then to be safe we must check for REG_DEAD notes
11328 for each register other than the first. They could have
11329 their own REG_DEAD notes lying around. */
11330 else if ((note == 0
11331 || (note != 0
11332 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
11333 < GET_MODE_SIZE (GET_MODE (x)))))
11334 && regno < FIRST_PSEUDO_REGISTER
11335 && HARD_REGNO_NREGS (regno, GET_MODE (x)) > 1)
11337 int ourend = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
11338 int i, offset;
11339 rtx oldnotes = 0;
11341 if (note)
11342 offset = HARD_REGNO_NREGS (regno, GET_MODE (XEXP (note, 0)));
11343 else
11344 offset = 1;
11346 for (i = regno + offset; i < ourend; i++)
11347 move_deaths (gen_rtx_REG (reg_raw_mode[i], i),
11348 maybe_kill_insn, from_cuid, to_insn, &oldnotes);
11351 if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
11353 XEXP (note, 1) = *pnotes;
11354 *pnotes = note;
11356 else
11357 *pnotes = gen_rtx_EXPR_LIST (REG_DEAD, x, *pnotes);
11359 REG_N_DEATHS (regno)++;
11362 return;
11365 else if (GET_CODE (x) == SET)
11367 rtx dest = SET_DEST (x);
11369 move_deaths (SET_SRC (x), maybe_kill_insn, from_cuid, to_insn, pnotes);
11371 /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
11372 that accesses one word of a multi-word item, some
11373 piece of everything register in the expression is used by
11374 this insn, so remove any old death. */
11376 if (GET_CODE (dest) == ZERO_EXTRACT
11377 || GET_CODE (dest) == STRICT_LOW_PART
11378 || (GET_CODE (dest) == SUBREG
11379 && (((GET_MODE_SIZE (GET_MODE (dest))
11380 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
11381 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
11382 + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
11384 move_deaths (dest, maybe_kill_insn, from_cuid, to_insn, pnotes);
11385 return;
11388 /* If this is some other SUBREG, we know it replaces the entire
11389 value, so use that as the destination. */
11390 if (GET_CODE (dest) == SUBREG)
11391 dest = SUBREG_REG (dest);
11393 /* If this is a MEM, adjust deaths of anything used in the address.
11394 For a REG (the only other possibility), the entire value is
11395 being replaced so the old value is not used in this insn. */
11397 if (GET_CODE (dest) == MEM)
11398 move_deaths (XEXP (dest, 0), maybe_kill_insn, from_cuid,
11399 to_insn, pnotes);
11400 return;
11403 else if (GET_CODE (x) == CLOBBER)
11404 return;
11406 len = GET_RTX_LENGTH (code);
11407 fmt = GET_RTX_FORMAT (code);
11409 for (i = 0; i < len; i++)
11411 if (fmt[i] == 'E')
11413 register int j;
11414 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
11415 move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_cuid,
11416 to_insn, pnotes);
11418 else if (fmt[i] == 'e')
11419 move_deaths (XEXP (x, i), maybe_kill_insn, from_cuid, to_insn, pnotes);
11423 /* Return 1 if X is the target of a bit-field assignment in BODY, the
11424 pattern of an insn. X must be a REG. */
11426 static int
11427 reg_bitfield_target_p (x, body)
11428 rtx x;
11429 rtx body;
11431 int i;
11433 if (GET_CODE (body) == SET)
11435 rtx dest = SET_DEST (body);
11436 rtx target;
11437 int regno, tregno, endregno, endtregno;
11439 if (GET_CODE (dest) == ZERO_EXTRACT)
11440 target = XEXP (dest, 0);
11441 else if (GET_CODE (dest) == STRICT_LOW_PART)
11442 target = SUBREG_REG (XEXP (dest, 0));
11443 else
11444 return 0;
11446 if (GET_CODE (target) == SUBREG)
11447 target = SUBREG_REG (target);
11449 if (GET_CODE (target) != REG)
11450 return 0;
11452 tregno = REGNO (target), regno = REGNO (x);
11453 if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
11454 return target == x;
11456 endtregno = tregno + HARD_REGNO_NREGS (tregno, GET_MODE (target));
11457 endregno = regno + HARD_REGNO_NREGS (regno, GET_MODE (x));
11459 return endregno > tregno && regno < endtregno;
11462 else if (GET_CODE (body) == PARALLEL)
11463 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
11464 if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
11465 return 1;
11467 return 0;
11470 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
11471 as appropriate. I3 and I2 are the insns resulting from the combination
11472 insns including FROM (I2 may be zero).
11474 ELIM_I2 and ELIM_I1 are either zero or registers that we know will
11475 not need REG_DEAD notes because they are being substituted for. This
11476 saves searching in the most common cases.
11478 Each note in the list is either ignored or placed on some insns, depending
11479 on the type of note. */
11481 static void
11482 distribute_notes (notes, from_insn, i3, i2, elim_i2, elim_i1)
11483 rtx notes;
11484 rtx from_insn;
11485 rtx i3, i2;
11486 rtx elim_i2, elim_i1;
11488 rtx note, next_note;
11489 rtx tem;
11491 for (note = notes; note; note = next_note)
11493 rtx place = 0, place2 = 0;
11495 /* If this NOTE references a pseudo register, ensure it references
11496 the latest copy of that register. */
11497 if (XEXP (note, 0) && GET_CODE (XEXP (note, 0)) == REG
11498 && REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER)
11499 XEXP (note, 0) = regno_reg_rtx[REGNO (XEXP (note, 0))];
11501 next_note = XEXP (note, 1);
11502 switch (REG_NOTE_KIND (note))
11504 case REG_BR_PROB:
11505 case REG_EXEC_COUNT:
11506 /* Doesn't matter much where we put this, as long as it's somewhere.
11507 It is preferable to keep these notes on branches, which is most
11508 likely to be i3. */
11509 place = i3;
11510 break;
11512 case REG_EH_REGION:
11513 case REG_EH_RETHROW:
11514 /* These notes must remain with the call. It should not be
11515 possible for both I2 and I3 to be a call. */
11516 if (GET_CODE (i3) == CALL_INSN)
11517 place = i3;
11518 else if (i2 && GET_CODE (i2) == CALL_INSN)
11519 place = i2;
11520 else
11521 abort ();
11522 break;
11524 case REG_UNUSED:
11525 /* Any clobbers for i3 may still exist, and so we must process
11526 REG_UNUSED notes from that insn.
11528 Any clobbers from i2 or i1 can only exist if they were added by
11529 recog_for_combine. In that case, recog_for_combine created the
11530 necessary REG_UNUSED notes. Trying to keep any original
11531 REG_UNUSED notes from these insns can cause incorrect output
11532 if it is for the same register as the original i3 dest.
11533 In that case, we will notice that the register is set in i3,
11534 and then add a REG_UNUSED note for the destination of i3, which
11535 is wrong. However, it is possible to have REG_UNUSED notes from
11536 i2 or i1 for register which were both used and clobbered, so
11537 we keep notes from i2 or i1 if they will turn into REG_DEAD
11538 notes. */
11540 /* If this register is set or clobbered in I3, put the note there
11541 unless there is one already. */
11542 if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
11544 if (from_insn != i3)
11545 break;
11547 if (! (GET_CODE (XEXP (note, 0)) == REG
11548 ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
11549 : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
11550 place = i3;
11552 /* Otherwise, if this register is used by I3, then this register
11553 now dies here, so we must put a REG_DEAD note here unless there
11554 is one already. */
11555 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
11556 && ! (GET_CODE (XEXP (note, 0)) == REG
11557 ? find_regno_note (i3, REG_DEAD, REGNO (XEXP (note, 0)))
11558 : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
11560 PUT_REG_NOTE_KIND (note, REG_DEAD);
11561 place = i3;
11563 break;
11565 case REG_EQUAL:
11566 case REG_EQUIV:
11567 case REG_NONNEG:
11568 case REG_NOALIAS:
11569 /* These notes say something about results of an insn. We can
11570 only support them if they used to be on I3 in which case they
11571 remain on I3. Otherwise they are ignored.
11573 If the note refers to an expression that is not a constant, we
11574 must also ignore the note since we cannot tell whether the
11575 equivalence is still true. It might be possible to do
11576 slightly better than this (we only have a problem if I2DEST
11577 or I1DEST is present in the expression), but it doesn't
11578 seem worth the trouble. */
11580 if (from_insn == i3
11581 && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
11582 place = i3;
11583 break;
11585 case REG_INC:
11586 case REG_NO_CONFLICT:
11587 /* These notes say something about how a register is used. They must
11588 be present on any use of the register in I2 or I3. */
11589 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
11590 place = i3;
11592 if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
11594 if (place)
11595 place2 = i2;
11596 else
11597 place = i2;
11599 break;
11601 case REG_LABEL:
11602 /* This can show up in several ways -- either directly in the
11603 pattern, or hidden off in the constant pool with (or without?)
11604 a REG_EQUAL note. */
11605 /* ??? Ignore the without-reg_equal-note problem for now. */
11606 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
11607 || ((tem = find_reg_note (i3, REG_EQUAL, NULL_RTX))
11608 && GET_CODE (XEXP (tem, 0)) == LABEL_REF
11609 && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0)))
11610 place = i3;
11612 if (i2
11613 && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
11614 || ((tem = find_reg_note (i2, REG_EQUAL, NULL_RTX))
11615 && GET_CODE (XEXP (tem, 0)) == LABEL_REF
11616 && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0))))
11618 if (place)
11619 place2 = i2;
11620 else
11621 place = i2;
11623 break;
11625 case REG_WAS_0:
11626 /* It is too much trouble to try to see if this note is still
11627 correct in all situations. It is better to simply delete it. */
11628 break;
11630 case REG_RETVAL:
11631 /* If the insn previously containing this note still exists,
11632 put it back where it was. Otherwise move it to the previous
11633 insn. Adjust the corresponding REG_LIBCALL note. */
11634 if (GET_CODE (from_insn) != NOTE)
11635 place = from_insn;
11636 else
11638 tem = find_reg_note (XEXP (note, 0), REG_LIBCALL, NULL_RTX);
11639 place = prev_real_insn (from_insn);
11640 if (tem && place)
11641 XEXP (tem, 0) = place;
11643 break;
11645 case REG_LIBCALL:
11646 /* This is handled similarly to REG_RETVAL. */
11647 if (GET_CODE (from_insn) != NOTE)
11648 place = from_insn;
11649 else
11651 tem = find_reg_note (XEXP (note, 0), REG_RETVAL, NULL_RTX);
11652 place = next_real_insn (from_insn);
11653 if (tem && place)
11654 XEXP (tem, 0) = place;
11656 break;
11658 case REG_DEAD:
11659 /* If the register is used as an input in I3, it dies there.
11660 Similarly for I2, if it is non-zero and adjacent to I3.
11662 If the register is not used as an input in either I3 or I2
11663 and it is not one of the registers we were supposed to eliminate,
11664 there are two possibilities. We might have a non-adjacent I2
11665 or we might have somehow eliminated an additional register
11666 from a computation. For example, we might have had A & B where
11667 we discover that B will always be zero. In this case we will
11668 eliminate the reference to A.
11670 In both cases, we must search to see if we can find a previous
11671 use of A and put the death note there. */
11673 if (from_insn
11674 && GET_CODE (from_insn) == CALL_INSN
11675 && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
11676 place = from_insn;
11677 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
11678 place = i3;
11679 else if (i2 != 0 && next_nonnote_insn (i2) == i3
11680 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
11681 place = i2;
11683 if (XEXP (note, 0) == elim_i2 || XEXP (note, 0) == elim_i1)
11684 break;
11686 /* If the register is used in both I2 and I3 and it dies in I3,
11687 we might have added another reference to it. If reg_n_refs
11688 was 2, bump it to 3. This has to be correct since the
11689 register must have been set somewhere. The reason this is
11690 done is because local-alloc.c treats 2 references as a
11691 special case. */
11693 if (place == i3 && i2 != 0 && GET_CODE (XEXP (note, 0)) == REG
11694 && REG_N_REFS (REGNO (XEXP (note, 0)))== 2
11695 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
11696 REG_N_REFS (REGNO (XEXP (note, 0))) = 3;
11698 if (place == 0)
11700 basic_block bb = BASIC_BLOCK (this_basic_block);
11702 for (tem = PREV_INSN (i3); place == 0; tem = PREV_INSN (tem))
11704 if (GET_RTX_CLASS (GET_CODE (tem)) != 'i')
11706 if (tem == bb->head)
11707 break;
11708 continue;
11711 /* If the register is being set at TEM, see if that is all
11712 TEM is doing. If so, delete TEM. Otherwise, make this
11713 into a REG_UNUSED note instead. */
11714 if (reg_set_p (XEXP (note, 0), PATTERN (tem)))
11716 rtx set = single_set (tem);
11717 rtx inner_dest = 0;
11718 #ifdef HAVE_cc0
11719 rtx cc0_setter = NULL_RTX;
11720 #endif
11722 if (set != 0)
11723 for (inner_dest = SET_DEST (set);
11724 GET_CODE (inner_dest) == STRICT_LOW_PART
11725 || GET_CODE (inner_dest) == SUBREG
11726 || GET_CODE (inner_dest) == ZERO_EXTRACT;
11727 inner_dest = XEXP (inner_dest, 0))
11730 /* Verify that it was the set, and not a clobber that
11731 modified the register.
11733 CC0 targets must be careful to maintain setter/user
11734 pairs. If we cannot delete the setter due to side
11735 effects, mark the user with an UNUSED note instead
11736 of deleting it. */
11738 if (set != 0 && ! side_effects_p (SET_SRC (set))
11739 && rtx_equal_p (XEXP (note, 0), inner_dest)
11740 #ifdef HAVE_cc0
11741 && (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
11742 || ((cc0_setter = prev_cc0_setter (tem)) != NULL
11743 && sets_cc0_p (PATTERN (cc0_setter)) > 0))
11744 #endif
11747 /* Move the notes and links of TEM elsewhere.
11748 This might delete other dead insns recursively.
11749 First set the pattern to something that won't use
11750 any register. */
11752 PATTERN (tem) = pc_rtx;
11754 distribute_notes (REG_NOTES (tem), tem, tem,
11755 NULL_RTX, NULL_RTX, NULL_RTX);
11756 distribute_links (LOG_LINKS (tem));
11758 PUT_CODE (tem, NOTE);
11759 NOTE_LINE_NUMBER (tem) = NOTE_INSN_DELETED;
11760 NOTE_SOURCE_FILE (tem) = 0;
11762 #ifdef HAVE_cc0
11763 /* Delete the setter too. */
11764 if (cc0_setter)
11766 PATTERN (cc0_setter) = pc_rtx;
11768 distribute_notes (REG_NOTES (cc0_setter),
11769 cc0_setter, cc0_setter,
11770 NULL_RTX, NULL_RTX, NULL_RTX);
11771 distribute_links (LOG_LINKS (cc0_setter));
11773 PUT_CODE (cc0_setter, NOTE);
11774 NOTE_LINE_NUMBER (cc0_setter)
11775 = NOTE_INSN_DELETED;
11776 NOTE_SOURCE_FILE (cc0_setter) = 0;
11778 #endif
11780 /* If the register is both set and used here, put the
11781 REG_DEAD note here, but place a REG_UNUSED note
11782 here too unless there already is one. */
11783 else if (reg_referenced_p (XEXP (note, 0),
11784 PATTERN (tem)))
11786 place = tem;
11788 if (! find_regno_note (tem, REG_UNUSED,
11789 REGNO (XEXP (note, 0))))
11790 REG_NOTES (tem)
11791 = gen_rtx_EXPR_LIST (REG_UNUSED, XEXP (note, 0),
11792 REG_NOTES (tem));
11794 else
11796 PUT_REG_NOTE_KIND (note, REG_UNUSED);
11798 /* If there isn't already a REG_UNUSED note, put one
11799 here. */
11800 if (! find_regno_note (tem, REG_UNUSED,
11801 REGNO (XEXP (note, 0))))
11802 place = tem;
11803 break;
11806 else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem))
11807 || (GET_CODE (tem) == CALL_INSN
11808 && find_reg_fusage (tem, USE, XEXP (note, 0))))
11810 place = tem;
11812 /* If we are doing a 3->2 combination, and we have a
11813 register which formerly died in i3 and was not used
11814 by i2, which now no longer dies in i3 and is used in
11815 i2 but does not die in i2, and place is between i2
11816 and i3, then we may need to move a link from place to
11817 i2. */
11818 if (i2 && INSN_UID (place) <= max_uid_cuid
11819 && INSN_CUID (place) > INSN_CUID (i2)
11820 && from_insn && INSN_CUID (from_insn) > INSN_CUID (i2)
11821 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
11823 rtx links = LOG_LINKS (place);
11824 LOG_LINKS (place) = 0;
11825 distribute_links (links);
11827 break;
11830 if (tem == bb->head)
11831 break;
11834 /* We haven't found an insn for the death note and it
11835 is still a REG_DEAD note, but we have hit the beginning
11836 of the block. If the existing life info says the reg
11837 was dead, there's nothing left to do. Otherwise, we'll
11838 need to do a global life update after combine. */
11839 if (REG_NOTE_KIND (note) == REG_DEAD && place == 0)
11841 int regno = REGNO (XEXP (note, 0));
11842 if (REGNO_REG_SET_P (bb->global_live_at_start, regno))
11844 SET_BIT (refresh_blocks, this_basic_block);
11845 need_refresh = 1;
11850 /* If the register is set or already dead at PLACE, we needn't do
11851 anything with this note if it is still a REG_DEAD note.
11852 We can here if it is set at all, not if is it totally replace,
11853 which is what `dead_or_set_p' checks, so also check for it being
11854 set partially. */
11856 if (place && REG_NOTE_KIND (note) == REG_DEAD)
11858 int regno = REGNO (XEXP (note, 0));
11860 if (dead_or_set_p (place, XEXP (note, 0))
11861 || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
11863 /* Unless the register previously died in PLACE, clear
11864 reg_last_death. [I no longer understand why this is
11865 being done.] */
11866 if (reg_last_death[regno] != place)
11867 reg_last_death[regno] = 0;
11868 place = 0;
11870 else
11871 reg_last_death[regno] = place;
11873 /* If this is a death note for a hard reg that is occupying
11874 multiple registers, ensure that we are still using all
11875 parts of the object. If we find a piece of the object
11876 that is unused, we must add a USE for that piece before
11877 PLACE and put the appropriate REG_DEAD note on it.
11879 An alternative would be to put a REG_UNUSED for the pieces
11880 on the insn that set the register, but that can't be done if
11881 it is not in the same block. It is simpler, though less
11882 efficient, to add the USE insns. */
11884 if (place && regno < FIRST_PSEUDO_REGISTER
11885 && HARD_REGNO_NREGS (regno, GET_MODE (XEXP (note, 0))) > 1)
11887 int endregno
11888 = regno + HARD_REGNO_NREGS (regno,
11889 GET_MODE (XEXP (note, 0)));
11890 int all_used = 1;
11891 int i;
11893 for (i = regno; i < endregno; i++)
11894 if (! refers_to_regno_p (i, i + 1, PATTERN (place), 0)
11895 && ! find_regno_fusage (place, USE, i))
11897 rtx piece = gen_rtx_REG (reg_raw_mode[i], i);
11898 rtx p;
11900 /* See if we already placed a USE note for this
11901 register in front of PLACE. */
11902 for (p = place;
11903 GET_CODE (PREV_INSN (p)) == INSN
11904 && GET_CODE (PATTERN (PREV_INSN (p))) == USE;
11905 p = PREV_INSN (p))
11906 if (rtx_equal_p (piece,
11907 XEXP (PATTERN (PREV_INSN (p)), 0)))
11909 p = 0;
11910 break;
11913 if (p)
11915 rtx use_insn
11916 = emit_insn_before (gen_rtx_USE (VOIDmode,
11917 piece),
11919 REG_NOTES (use_insn)
11920 = gen_rtx_EXPR_LIST (REG_DEAD, piece,
11921 REG_NOTES (use_insn));
11924 all_used = 0;
11927 /* Check for the case where the register dying partially
11928 overlaps the register set by this insn. */
11929 if (all_used)
11930 for (i = regno; i < endregno; i++)
11931 if (dead_or_set_regno_p (place, i))
11933 all_used = 0;
11934 break;
11937 if (! all_used)
11939 /* Put only REG_DEAD notes for pieces that are
11940 still used and that are not already dead or set. */
11942 for (i = regno; i < endregno; i++)
11944 rtx piece = gen_rtx_REG (reg_raw_mode[i], i);
11946 if ((reg_referenced_p (piece, PATTERN (place))
11947 || (GET_CODE (place) == CALL_INSN
11948 && find_reg_fusage (place, USE, piece)))
11949 && ! dead_or_set_p (place, piece)
11950 && ! reg_bitfield_target_p (piece,
11951 PATTERN (place)))
11952 REG_NOTES (place)
11953 = gen_rtx_EXPR_LIST (REG_DEAD, piece,
11954 REG_NOTES (place));
11957 place = 0;
11961 break;
11963 default:
11964 /* Any other notes should not be present at this point in the
11965 compilation. */
11966 abort ();
11969 if (place)
11971 XEXP (note, 1) = REG_NOTES (place);
11972 REG_NOTES (place) = note;
11974 else if ((REG_NOTE_KIND (note) == REG_DEAD
11975 || REG_NOTE_KIND (note) == REG_UNUSED)
11976 && GET_CODE (XEXP (note, 0)) == REG)
11977 REG_N_DEATHS (REGNO (XEXP (note, 0)))--;
11979 if (place2)
11981 if ((REG_NOTE_KIND (note) == REG_DEAD
11982 || REG_NOTE_KIND (note) == REG_UNUSED)
11983 && GET_CODE (XEXP (note, 0)) == REG)
11984 REG_N_DEATHS (REGNO (XEXP (note, 0)))++;
11986 REG_NOTES (place2) = gen_rtx_fmt_ee (GET_CODE (note),
11987 REG_NOTE_KIND (note),
11988 XEXP (note, 0),
11989 REG_NOTES (place2));
11994 /* Similarly to above, distribute the LOG_LINKS that used to be present on
11995 I3, I2, and I1 to new locations. This is also called in one case to
11996 add a link pointing at I3 when I3's destination is changed. */
11998 static void
11999 distribute_links (links)
12000 rtx links;
12002 rtx link, next_link;
12004 for (link = links; link; link = next_link)
12006 rtx place = 0;
12007 rtx insn;
12008 rtx set, reg;
12010 next_link = XEXP (link, 1);
12012 /* If the insn that this link points to is a NOTE or isn't a single
12013 set, ignore it. In the latter case, it isn't clear what we
12014 can do other than ignore the link, since we can't tell which
12015 register it was for. Such links wouldn't be used by combine
12016 anyway.
12018 It is not possible for the destination of the target of the link to
12019 have been changed by combine. The only potential of this is if we
12020 replace I3, I2, and I1 by I3 and I2. But in that case the
12021 destination of I2 also remains unchanged. */
12023 if (GET_CODE (XEXP (link, 0)) == NOTE
12024 || (set = single_set (XEXP (link, 0))) == 0)
12025 continue;
12027 reg = SET_DEST (set);
12028 while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
12029 || GET_CODE (reg) == SIGN_EXTRACT
12030 || GET_CODE (reg) == STRICT_LOW_PART)
12031 reg = XEXP (reg, 0);
12033 /* A LOG_LINK is defined as being placed on the first insn that uses
12034 a register and points to the insn that sets the register. Start
12035 searching at the next insn after the target of the link and stop
12036 when we reach a set of the register or the end of the basic block.
12038 Note that this correctly handles the link that used to point from
12039 I3 to I2. Also note that not much searching is typically done here
12040 since most links don't point very far away. */
12042 for (insn = NEXT_INSN (XEXP (link, 0));
12043 (insn && (this_basic_block == n_basic_blocks - 1
12044 || BLOCK_HEAD (this_basic_block + 1) != insn));
12045 insn = NEXT_INSN (insn))
12046 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i'
12047 && reg_overlap_mentioned_p (reg, PATTERN (insn)))
12049 if (reg_referenced_p (reg, PATTERN (insn)))
12050 place = insn;
12051 break;
12053 else if (GET_CODE (insn) == CALL_INSN
12054 && find_reg_fusage (insn, USE, reg))
12056 place = insn;
12057 break;
12060 /* If we found a place to put the link, place it there unless there
12061 is already a link to the same insn as LINK at that point. */
12063 if (place)
12065 rtx link2;
12067 for (link2 = LOG_LINKS (place); link2; link2 = XEXP (link2, 1))
12068 if (XEXP (link2, 0) == XEXP (link, 0))
12069 break;
12071 if (link2 == 0)
12073 XEXP (link, 1) = LOG_LINKS (place);
12074 LOG_LINKS (place) = link;
12076 /* Set added_links_insn to the earliest insn we added a
12077 link to. */
12078 if (added_links_insn == 0
12079 || INSN_CUID (added_links_insn) > INSN_CUID (place))
12080 added_links_insn = place;
12086 /* Compute INSN_CUID for INSN, which is an insn made by combine. */
12088 static int
12089 insn_cuid (insn)
12090 rtx insn;
12092 while (insn != 0 && INSN_UID (insn) > max_uid_cuid
12093 && GET_CODE (insn) == INSN && GET_CODE (PATTERN (insn)) == USE)
12094 insn = NEXT_INSN (insn);
12096 if (INSN_UID (insn) > max_uid_cuid)
12097 abort ();
12099 return INSN_CUID (insn);
12102 void
12103 dump_combine_stats (file)
12104 FILE *file;
12106 fnotice
12107 (file,
12108 ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
12109 combine_attempts, combine_merges, combine_extras, combine_successes);
12112 void
12113 dump_combine_total_stats (file)
12114 FILE *file;
12116 fnotice
12117 (file,
12118 "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
12119 total_attempts, total_merges, total_extras, total_successes);