* doc/extend.texi (ARM Built-in Functions): Replace with correct
[official-gcc.git] / gcc / combine.c
blob568862d6b0a7d51c93b8c7b446822409d5a4da3f
1 /* Optimize by combining instructions for GNU compiler.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
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 - a LOG_LINKS entry that refers to an insn with multiple SETs may be
57 removed because there is no way to know which register it was
58 linking
60 To simplify substitution, we combine only when the earlier insn(s)
61 consist of only a single assignment. To simplify updating afterward,
62 we never combine when a subroutine call appears in the middle.
64 Since we do not represent assignments to CC0 explicitly except when that
65 is all an insn does, there is no LOG_LINKS entry in an insn that uses
66 the condition code for the insn that set the condition code.
67 Fortunately, these two insns must be consecutive.
68 Therefore, every JUMP_INSN is taken to have an implicit logical link
69 to the preceding insn. This is not quite right, since non-jumps can
70 also use the condition code; but in practice such insns would not
71 combine anyway. */
73 #include "config.h"
74 #include "system.h"
75 #include "coretypes.h"
76 #include "tm.h"
77 #include "rtl.h"
78 #include "tree.h"
79 #include "tm_p.h"
80 #include "flags.h"
81 #include "regs.h"
82 #include "hard-reg-set.h"
83 #include "basic-block.h"
84 #include "insn-config.h"
85 #include "function.h"
86 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
87 #include "expr.h"
88 #include "insn-attr.h"
89 #include "recog.h"
90 #include "real.h"
91 #include "toplev.h"
92 #include "target.h"
94 /* Number of attempts to combine instructions in this function. */
96 static int combine_attempts;
98 /* Number of attempts that got as far as substitution in this function. */
100 static int combine_merges;
102 /* Number of instructions combined with added SETs in this function. */
104 static int combine_extras;
106 /* Number of instructions combined in this function. */
108 static int combine_successes;
110 /* Totals over entire compilation. */
112 static int total_attempts, total_merges, total_extras, total_successes;
115 /* Vector mapping INSN_UIDs to cuids.
116 The cuids are like uids but increase monotonically always.
117 Combine always uses cuids so that it can compare them.
118 But actually renumbering the uids, which we used to do,
119 proves to be a bad idea because it makes it hard to compare
120 the dumps produced by earlier passes with those from later passes. */
122 static int *uid_cuid;
123 static int max_uid_cuid;
125 /* Get the cuid of an insn. */
127 #define INSN_CUID(INSN) \
128 (INSN_UID (INSN) > max_uid_cuid ? insn_cuid (INSN) : uid_cuid[INSN_UID (INSN)])
130 /* In case BITS_PER_WORD == HOST_BITS_PER_WIDE_INT, shifting by
131 BITS_PER_WORD would invoke undefined behavior. Work around it. */
133 #define UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD(val) \
134 (((unsigned HOST_WIDE_INT) (val) << (BITS_PER_WORD - 1)) << 1)
136 #define nonzero_bits(X, M) \
137 cached_nonzero_bits (X, M, NULL_RTX, VOIDmode, 0)
139 #define num_sign_bit_copies(X, M) \
140 cached_num_sign_bit_copies (X, M, NULL_RTX, VOIDmode, 0)
142 /* Maximum register number, which is the size of the tables below. */
144 static unsigned 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 the lowest CUID that `subst' is currently dealing with.
173 get_last_value will not return a value if the register was set at or
174 after this CUID. If not for this mechanism, we could get confused if
175 I2 or I1 in try_combine were an insn that used the old value of a register
176 to obtain a new value. In that case, we might erroneously get the
177 new value of the register when we wanted the old one. */
179 static int subst_low_cuid;
181 /* This contains any hard registers that are used in newpat; reg_dead_at_p
182 must consider all these registers to be always live. */
184 static HARD_REG_SET newpat_used_regs;
186 /* This is an insn to which a LOG_LINKS entry has been added. If this
187 insn is the earlier than I2 or I3, combine should rescan starting at
188 that location. */
190 static rtx added_links_insn;
192 /* Basic block in which we are performing combines. */
193 static basic_block this_basic_block;
195 /* A bitmap indicating which blocks had registers go dead at entry.
196 After combine, we'll need to re-do global life analysis with
197 those blocks as starting points. */
198 static sbitmap refresh_blocks;
200 /* The next group of arrays allows the recording of the last value assigned
201 to (hard or pseudo) register n. We use this information to see if an
202 operation being processed is redundant given a prior operation performed
203 on the register. For example, an `and' with a constant is redundant if
204 all the zero bits are already known to be turned off.
206 We use an approach similar to that used by cse, but change it in the
207 following ways:
209 (1) We do not want to reinitialize at each label.
210 (2) It is useful, but not critical, to know the actual value assigned
211 to a register. Often just its form is helpful.
213 Therefore, we maintain the following arrays:
215 reg_last_set_value the last value assigned
216 reg_last_set_label records the value of label_tick when the
217 register was assigned
218 reg_last_set_table_tick records the value of label_tick when a
219 value using the register is assigned
220 reg_last_set_invalid set to nonzero when it is not valid
221 to use the value of this register in some
222 register's value
224 To understand the usage of these tables, it is important to understand
225 the distinction between the value in reg_last_set_value being valid
226 and the register being validly contained in some other expression in the
227 table.
229 Entry I in reg_last_set_value is valid if it is nonzero, and either
230 reg_n_sets[i] is 1 or reg_last_set_label[i] == label_tick.
232 Register I may validly appear in any expression returned for the value
233 of another register if reg_n_sets[i] is 1. It may also appear in the
234 value for register J if reg_last_set_label[i] < reg_last_set_label[j] or
235 reg_last_set_invalid[j] is zero.
237 If an expression is found in the table containing a register which may
238 not validly appear in an expression, the register is replaced by
239 something that won't match, (clobber (const_int 0)).
241 reg_last_set_invalid[i] is set nonzero when register I is being assigned
242 to and reg_last_set_table_tick[i] == label_tick. */
244 /* Record last value assigned to (hard or pseudo) register n. */
246 static rtx *reg_last_set_value;
248 /* Record the value of label_tick when the value for register n is placed in
249 reg_last_set_value[n]. */
251 static int *reg_last_set_label;
253 /* Record the value of label_tick when an expression involving register n
254 is placed in reg_last_set_value. */
256 static int *reg_last_set_table_tick;
258 /* Set nonzero if references to register n in expressions should not be
259 used. */
261 static char *reg_last_set_invalid;
263 /* Incremented for each label. */
265 static int label_tick;
267 /* Some registers that are set more than once and used in more than one
268 basic block are nevertheless always set in similar ways. For example,
269 a QImode register may be loaded from memory in two places on a machine
270 where byte loads zero extend.
272 We record in the following array what we know about the nonzero
273 bits of a register, specifically which bits are known to be zero.
275 If an entry is zero, it means that we don't know anything special. */
277 static unsigned HOST_WIDE_INT *reg_nonzero_bits;
279 /* Mode used to compute significance in reg_nonzero_bits. It is the largest
280 integer mode that can fit in HOST_BITS_PER_WIDE_INT. */
282 static enum machine_mode nonzero_bits_mode;
284 /* Nonzero if we know that a register has some leading bits that are always
285 equal to the sign bit. */
287 static unsigned char *reg_sign_bit_copies;
289 /* Nonzero when reg_nonzero_bits and reg_sign_bit_copies can be safely used.
290 It is zero while computing them and after combine has completed. This
291 former test prevents propagating values based on previously set values,
292 which can be incorrect if a variable is modified in a loop. */
294 static int nonzero_sign_valid;
296 /* These arrays are maintained in parallel with reg_last_set_value
297 and are used to store the mode in which the register was last set,
298 the bits that were known to be zero when it was last set, and the
299 number of sign bits copies it was known to have when it was last set. */
301 static enum machine_mode *reg_last_set_mode;
302 static unsigned HOST_WIDE_INT *reg_last_set_nonzero_bits;
303 static char *reg_last_set_sign_bit_copies;
305 /* Record one modification to rtl structure
306 to be undone by storing old_contents into *where.
307 is_int is 1 if the contents are an int. */
309 struct undo
311 struct undo *next;
312 int is_int;
313 union {rtx r; int i;} old_contents;
314 union {rtx *r; int *i;} where;
317 /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
318 num_undo says how many are currently recorded.
320 other_insn is nonzero if we have modified some other insn in the process
321 of working on subst_insn. It must be verified too. */
323 struct undobuf
325 struct undo *undos;
326 struct undo *frees;
327 rtx other_insn;
330 static struct undobuf undobuf;
332 /* Number of times the pseudo being substituted for
333 was found and replaced. */
335 static int n_occurrences;
337 static void do_SUBST (rtx *, rtx);
338 static void do_SUBST_INT (int *, int);
339 static void init_reg_last_arrays (void);
340 static void setup_incoming_promotions (void);
341 static void set_nonzero_bits_and_sign_copies (rtx, rtx, void *);
342 static int cant_combine_insn_p (rtx);
343 static int can_combine_p (rtx, rtx, rtx, rtx, rtx *, rtx *);
344 static int combinable_i3pat (rtx, rtx *, rtx, rtx, int, rtx *);
345 static int contains_muldiv (rtx);
346 static rtx try_combine (rtx, rtx, rtx, int *);
347 static void undo_all (void);
348 static void undo_commit (void);
349 static rtx *find_split_point (rtx *, rtx);
350 static rtx subst (rtx, rtx, rtx, int, int);
351 static rtx combine_simplify_rtx (rtx, enum machine_mode, int);
352 static rtx simplify_if_then_else (rtx);
353 static rtx simplify_set (rtx);
354 static rtx simplify_logical (rtx);
355 static rtx expand_compound_operation (rtx);
356 static rtx expand_field_assignment (rtx);
357 static rtx make_extraction (enum machine_mode, rtx, HOST_WIDE_INT,
358 rtx, unsigned HOST_WIDE_INT, int, int, int);
359 static rtx extract_left_shift (rtx, int);
360 static rtx make_compound_operation (rtx, enum rtx_code);
361 static int get_pos_from_mask (unsigned HOST_WIDE_INT,
362 unsigned HOST_WIDE_INT *);
363 static rtx force_to_mode (rtx, enum machine_mode,
364 unsigned HOST_WIDE_INT, rtx, int);
365 static rtx if_then_else_cond (rtx, rtx *, rtx *);
366 static rtx known_cond (rtx, enum rtx_code, rtx, rtx);
367 static int rtx_equal_for_field_assignment_p (rtx, rtx);
368 static rtx make_field_assignment (rtx);
369 static rtx apply_distributive_law (rtx);
370 static rtx simplify_and_const_int (rtx, enum machine_mode, rtx,
371 unsigned HOST_WIDE_INT);
372 static unsigned HOST_WIDE_INT cached_nonzero_bits (rtx, enum machine_mode,
373 rtx, enum machine_mode,
374 unsigned HOST_WIDE_INT);
375 static unsigned HOST_WIDE_INT nonzero_bits1 (rtx, enum machine_mode, rtx,
376 enum machine_mode,
377 unsigned HOST_WIDE_INT);
378 static unsigned int cached_num_sign_bit_copies (rtx, enum machine_mode, rtx,
379 enum machine_mode,
380 unsigned int);
381 static unsigned int num_sign_bit_copies1 (rtx, enum machine_mode, rtx,
382 enum machine_mode, unsigned int);
383 static int merge_outer_ops (enum rtx_code *, HOST_WIDE_INT *, enum rtx_code,
384 HOST_WIDE_INT, enum machine_mode, int *);
385 static rtx simplify_shift_const (rtx, enum rtx_code, enum machine_mode, rtx,
386 int);
387 static int recog_for_combine (rtx *, rtx, rtx *);
388 static rtx gen_lowpart_for_combine (enum machine_mode, rtx);
389 static rtx gen_binary (enum rtx_code, enum machine_mode, rtx, rtx);
390 static enum rtx_code simplify_comparison (enum rtx_code, rtx *, rtx *);
391 static void update_table_tick (rtx);
392 static void record_value_for_reg (rtx, rtx, rtx);
393 static void check_promoted_subreg (rtx, rtx);
394 static void record_dead_and_set_regs_1 (rtx, rtx, void *);
395 static void record_dead_and_set_regs (rtx);
396 static int get_last_value_validate (rtx *, rtx, int, int);
397 static rtx get_last_value (rtx);
398 static int use_crosses_set_p (rtx, int);
399 static void reg_dead_at_p_1 (rtx, rtx, void *);
400 static int reg_dead_at_p (rtx, rtx);
401 static void move_deaths (rtx, rtx, int, rtx, rtx *);
402 static int reg_bitfield_target_p (rtx, rtx);
403 static void distribute_notes (rtx, rtx, rtx, rtx);
404 static void distribute_links (rtx);
405 static void mark_used_regs_combine (rtx);
406 static int insn_cuid (rtx);
407 static void record_promoted_value (rtx, rtx);
408 static rtx reversed_comparison (rtx, enum machine_mode, rtx, rtx);
409 static enum rtx_code combine_reversed_comparison_code (rtx);
410 static int unmentioned_reg_p_1 (rtx *, void *);
411 static bool unmentioned_reg_p (rtx, rtx);
413 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
414 insn. The substitution can be undone by undo_all. If INTO is already
415 set to NEWVAL, do not record this change. Because computing NEWVAL might
416 also call SUBST, we have to compute it before we put anything into
417 the undo table. */
419 static void
420 do_SUBST (rtx *into, rtx newval)
422 struct undo *buf;
423 rtx oldval = *into;
425 if (oldval == newval)
426 return;
428 /* We'd like to catch as many invalid transformations here as
429 possible. Unfortunately, there are way too many mode changes
430 that are perfectly valid, so we'd waste too much effort for
431 little gain doing the checks here. Focus on catching invalid
432 transformations involving integer constants. */
433 if (GET_MODE_CLASS (GET_MODE (oldval)) == MODE_INT
434 && GET_CODE (newval) == CONST_INT)
436 /* Sanity check that we're replacing oldval with a CONST_INT
437 that is a valid sign-extension for the original mode. */
438 if (INTVAL (newval) != trunc_int_for_mode (INTVAL (newval),
439 GET_MODE (oldval)))
440 abort ();
442 /* Replacing the operand of a SUBREG or a ZERO_EXTEND with a
443 CONST_INT is not valid, because after the replacement, the
444 original mode would be gone. Unfortunately, we can't tell
445 when do_SUBST is called to replace the operand thereof, so we
446 perform this test on oldval instead, checking whether an
447 invalid replacement took place before we got here. */
448 if ((GET_CODE (oldval) == SUBREG
449 && GET_CODE (SUBREG_REG (oldval)) == CONST_INT)
450 || (GET_CODE (oldval) == ZERO_EXTEND
451 && GET_CODE (XEXP (oldval, 0)) == CONST_INT))
452 abort ();
455 if (undobuf.frees)
456 buf = undobuf.frees, undobuf.frees = buf->next;
457 else
458 buf = xmalloc (sizeof (struct undo));
460 buf->is_int = 0;
461 buf->where.r = into;
462 buf->old_contents.r = oldval;
463 *into = newval;
465 buf->next = undobuf.undos, undobuf.undos = buf;
468 #define SUBST(INTO, NEWVAL) do_SUBST(&(INTO), (NEWVAL))
470 /* Similar to SUBST, but NEWVAL is an int expression. Note that substitution
471 for the value of a HOST_WIDE_INT value (including CONST_INT) is
472 not safe. */
474 static void
475 do_SUBST_INT (int *into, int newval)
477 struct undo *buf;
478 int oldval = *into;
480 if (oldval == newval)
481 return;
483 if (undobuf.frees)
484 buf = undobuf.frees, undobuf.frees = buf->next;
485 else
486 buf = xmalloc (sizeof (struct undo));
488 buf->is_int = 1;
489 buf->where.i = into;
490 buf->old_contents.i = oldval;
491 *into = newval;
493 buf->next = undobuf.undos, undobuf.undos = buf;
496 #define SUBST_INT(INTO, NEWVAL) do_SUBST_INT(&(INTO), (NEWVAL))
498 /* Main entry point for combiner. F is the first insn of the function.
499 NREGS is the first unused pseudo-reg number.
501 Return nonzero if the combiner has turned an indirect jump
502 instruction into a direct jump. */
504 combine_instructions (rtx f, unsigned int nregs)
506 rtx insn, next;
507 #ifdef HAVE_cc0
508 rtx prev;
509 #endif
510 int i;
511 rtx links, nextlinks;
513 int new_direct_jump_p = 0;
515 combine_attempts = 0;
516 combine_merges = 0;
517 combine_extras = 0;
518 combine_successes = 0;
520 combine_max_regno = nregs;
522 /* It is not safe to use ordinary gen_lowpart in combine.
523 See comments in gen_lowpart_for_combine. */
524 gen_lowpart = gen_lowpart_for_combine;
526 reg_nonzero_bits = xcalloc (nregs, sizeof (unsigned HOST_WIDE_INT));
527 reg_sign_bit_copies = xcalloc (nregs, sizeof (unsigned char));
529 reg_last_death = xmalloc (nregs * sizeof (rtx));
530 reg_last_set = xmalloc (nregs * sizeof (rtx));
531 reg_last_set_value = xmalloc (nregs * sizeof (rtx));
532 reg_last_set_table_tick = xmalloc (nregs * sizeof (int));
533 reg_last_set_label = xmalloc (nregs * sizeof (int));
534 reg_last_set_invalid = xmalloc (nregs * sizeof (char));
535 reg_last_set_mode = xmalloc (nregs * sizeof (enum machine_mode));
536 reg_last_set_nonzero_bits = xmalloc (nregs * sizeof (HOST_WIDE_INT));
537 reg_last_set_sign_bit_copies = xmalloc (nregs * sizeof (char));
539 init_reg_last_arrays ();
541 init_recog_no_volatile ();
543 /* Compute maximum uid value so uid_cuid can be allocated. */
545 for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
546 if (INSN_UID (insn) > i)
547 i = INSN_UID (insn);
549 uid_cuid = xmalloc ((i + 1) * sizeof (int));
550 max_uid_cuid = i;
552 nonzero_bits_mode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
554 /* Don't use reg_nonzero_bits when computing it. This can cause problems
555 when, for example, we have j <<= 1 in a loop. */
557 nonzero_sign_valid = 0;
559 /* Compute the mapping from uids to cuids.
560 Cuids are numbers assigned to insns, like uids,
561 except that cuids increase monotonically through the code.
563 Scan all SETs and see if we can deduce anything about what
564 bits are known to be zero for some registers and how many copies
565 of the sign bit are known to exist for those registers.
567 Also set any known values so that we can use it while searching
568 for what bits are known to be set. */
570 label_tick = 1;
572 setup_incoming_promotions ();
574 refresh_blocks = sbitmap_alloc (last_basic_block);
575 sbitmap_zero (refresh_blocks);
577 for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
579 uid_cuid[INSN_UID (insn)] = ++i;
580 subst_low_cuid = i;
581 subst_insn = insn;
583 if (INSN_P (insn))
585 note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies,
586 NULL);
587 record_dead_and_set_regs (insn);
589 #ifdef AUTO_INC_DEC
590 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
591 if (REG_NOTE_KIND (links) == REG_INC)
592 set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX,
593 NULL);
594 #endif
597 if (GET_CODE (insn) == CODE_LABEL)
598 label_tick++;
601 nonzero_sign_valid = 1;
603 /* Now scan all the insns in forward order. */
605 label_tick = 1;
606 last_call_cuid = 0;
607 mem_last_set = 0;
608 init_reg_last_arrays ();
609 setup_incoming_promotions ();
611 FOR_EACH_BB (this_basic_block)
613 for (insn = BB_HEAD (this_basic_block);
614 insn != NEXT_INSN (BB_END (this_basic_block));
615 insn = next ? next : NEXT_INSN (insn))
617 next = 0;
619 if (GET_CODE (insn) == CODE_LABEL)
620 label_tick++;
622 else if (INSN_P (insn))
624 /* See if we know about function return values before this
625 insn based upon SUBREG flags. */
626 check_promoted_subreg (insn, PATTERN (insn));
628 /* Try this insn with each insn it links back to. */
630 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
631 if ((next = try_combine (insn, XEXP (links, 0),
632 NULL_RTX, &new_direct_jump_p)) != 0)
633 goto retry;
635 /* Try each sequence of three linked insns ending with this one. */
637 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
639 rtx link = XEXP (links, 0);
641 /* If the linked insn has been replaced by a note, then there
642 is no point in pursuing this chain any further. */
643 if (GET_CODE (link) == NOTE)
644 continue;
646 for (nextlinks = LOG_LINKS (link);
647 nextlinks;
648 nextlinks = XEXP (nextlinks, 1))
649 if ((next = try_combine (insn, link,
650 XEXP (nextlinks, 0),
651 &new_direct_jump_p)) != 0)
652 goto retry;
655 #ifdef HAVE_cc0
656 /* Try to combine a jump insn that uses CC0
657 with a preceding insn that sets CC0, and maybe with its
658 logical predecessor as well.
659 This is how we make decrement-and-branch insns.
660 We need this special code because data flow connections
661 via CC0 do not get entered in LOG_LINKS. */
663 if (GET_CODE (insn) == JUMP_INSN
664 && (prev = prev_nonnote_insn (insn)) != 0
665 && GET_CODE (prev) == INSN
666 && sets_cc0_p (PATTERN (prev)))
668 if ((next = try_combine (insn, prev,
669 NULL_RTX, &new_direct_jump_p)) != 0)
670 goto retry;
672 for (nextlinks = LOG_LINKS (prev); nextlinks;
673 nextlinks = XEXP (nextlinks, 1))
674 if ((next = try_combine (insn, prev,
675 XEXP (nextlinks, 0),
676 &new_direct_jump_p)) != 0)
677 goto retry;
680 /* Do the same for an insn that explicitly references CC0. */
681 if (GET_CODE (insn) == INSN
682 && (prev = prev_nonnote_insn (insn)) != 0
683 && GET_CODE (prev) == INSN
684 && sets_cc0_p (PATTERN (prev))
685 && GET_CODE (PATTERN (insn)) == SET
686 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
688 if ((next = try_combine (insn, prev,
689 NULL_RTX, &new_direct_jump_p)) != 0)
690 goto retry;
692 for (nextlinks = LOG_LINKS (prev); nextlinks;
693 nextlinks = XEXP (nextlinks, 1))
694 if ((next = try_combine (insn, prev,
695 XEXP (nextlinks, 0),
696 &new_direct_jump_p)) != 0)
697 goto retry;
700 /* Finally, see if any of the insns that this insn links to
701 explicitly references CC0. If so, try this insn, that insn,
702 and its predecessor if it sets CC0. */
703 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
704 if (GET_CODE (XEXP (links, 0)) == INSN
705 && GET_CODE (PATTERN (XEXP (links, 0))) == SET
706 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (XEXP (links, 0))))
707 && (prev = prev_nonnote_insn (XEXP (links, 0))) != 0
708 && GET_CODE (prev) == INSN
709 && sets_cc0_p (PATTERN (prev))
710 && (next = try_combine (insn, XEXP (links, 0),
711 prev, &new_direct_jump_p)) != 0)
712 goto retry;
713 #endif
715 /* Try combining an insn with two different insns whose results it
716 uses. */
717 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
718 for (nextlinks = XEXP (links, 1); nextlinks;
719 nextlinks = XEXP (nextlinks, 1))
720 if ((next = try_combine (insn, XEXP (links, 0),
721 XEXP (nextlinks, 0),
722 &new_direct_jump_p)) != 0)
723 goto retry;
725 /* Try this insn with each REG_EQUAL note it links back to. */
726 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
728 rtx set, note;
729 rtx temp = XEXP (links, 0);
730 if ((set = single_set (temp)) != 0
731 && (note = find_reg_equal_equiv_note (temp)) != 0
732 && GET_CODE (XEXP (note, 0)) != EXPR_LIST
733 /* Avoid using a register that may already been marked
734 dead by an earlier instruction. */
735 && ! unmentioned_reg_p (XEXP (note, 0), SET_SRC (set)))
737 /* Temporarily replace the set's source with the
738 contents of the REG_EQUAL note. The insn will
739 be deleted or recognized by try_combine. */
740 rtx orig = SET_SRC (set);
741 SET_SRC (set) = XEXP (note, 0);
742 next = try_combine (insn, temp, NULL_RTX,
743 &new_direct_jump_p);
744 if (next)
745 goto retry;
746 SET_SRC (set) = orig;
750 if (GET_CODE (insn) != NOTE)
751 record_dead_and_set_regs (insn);
753 retry:
758 clear_bb_flags ();
760 EXECUTE_IF_SET_IN_SBITMAP (refresh_blocks, 0, i,
761 BASIC_BLOCK (i)->flags |= BB_DIRTY);
762 new_direct_jump_p |= purge_all_dead_edges (0);
763 delete_noop_moves (f);
765 update_life_info_in_dirty_blocks (UPDATE_LIFE_GLOBAL_RM_NOTES,
766 PROP_DEATH_NOTES | PROP_SCAN_DEAD_CODE
767 | PROP_KILL_DEAD_CODE);
769 /* Clean up. */
770 sbitmap_free (refresh_blocks);
771 free (reg_nonzero_bits);
772 free (reg_sign_bit_copies);
773 free (reg_last_death);
774 free (reg_last_set);
775 free (reg_last_set_value);
776 free (reg_last_set_table_tick);
777 free (reg_last_set_label);
778 free (reg_last_set_invalid);
779 free (reg_last_set_mode);
780 free (reg_last_set_nonzero_bits);
781 free (reg_last_set_sign_bit_copies);
782 free (uid_cuid);
785 struct undo *undo, *next;
786 for (undo = undobuf.frees; undo; undo = next)
788 next = undo->next;
789 free (undo);
791 undobuf.frees = 0;
794 total_attempts += combine_attempts;
795 total_merges += combine_merges;
796 total_extras += combine_extras;
797 total_successes += combine_successes;
799 nonzero_sign_valid = 0;
800 gen_lowpart = gen_lowpart_general;
802 /* Make recognizer allow volatile MEMs again. */
803 init_recog ();
805 return new_direct_jump_p;
808 /* Wipe the reg_last_xxx arrays in preparation for another pass. */
810 static void
811 init_reg_last_arrays (void)
813 unsigned int nregs = combine_max_regno;
815 memset (reg_last_death, 0, nregs * sizeof (rtx));
816 memset (reg_last_set, 0, nregs * sizeof (rtx));
817 memset (reg_last_set_value, 0, nregs * sizeof (rtx));
818 memset (reg_last_set_table_tick, 0, nregs * sizeof (int));
819 memset (reg_last_set_label, 0, nregs * sizeof (int));
820 memset (reg_last_set_invalid, 0, nregs * sizeof (char));
821 memset (reg_last_set_mode, 0, nregs * sizeof (enum machine_mode));
822 memset (reg_last_set_nonzero_bits, 0, nregs * sizeof (HOST_WIDE_INT));
823 memset (reg_last_set_sign_bit_copies, 0, nregs * sizeof (char));
826 /* Set up any promoted values for incoming argument registers. */
828 static void
829 setup_incoming_promotions (void)
831 unsigned int regno;
832 rtx reg;
833 enum machine_mode mode;
834 int unsignedp;
835 rtx first = get_insns ();
837 if (targetm.calls.promote_function_args (TREE_TYPE (cfun->decl)))
839 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
840 /* Check whether this register can hold an incoming pointer
841 argument. FUNCTION_ARG_REGNO_P tests outgoing register
842 numbers, so translate if necessary due to register windows. */
843 if (FUNCTION_ARG_REGNO_P (OUTGOING_REGNO (regno))
844 && (reg = promoted_input_arg (regno, &mode, &unsignedp)) != 0)
846 record_value_for_reg
847 (reg, first, gen_rtx_fmt_e ((unsignedp ? ZERO_EXTEND
848 : SIGN_EXTEND),
849 GET_MODE (reg),
850 gen_rtx_CLOBBER (mode, const0_rtx)));
855 /* Called via note_stores. If X is a pseudo that is narrower than
856 HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
858 If we are setting only a portion of X and we can't figure out what
859 portion, assume all bits will be used since we don't know what will
860 be happening.
862 Similarly, set how many bits of X are known to be copies of the sign bit
863 at all locations in the function. This is the smallest number implied
864 by any set of X. */
866 static void
867 set_nonzero_bits_and_sign_copies (rtx x, rtx set,
868 void *data ATTRIBUTE_UNUSED)
870 unsigned int num;
872 if (GET_CODE (x) == REG
873 && REGNO (x) >= FIRST_PSEUDO_REGISTER
874 /* If this register is undefined at the start of the file, we can't
875 say what its contents were. */
876 && ! REGNO_REG_SET_P (ENTRY_BLOCK_PTR->next_bb->global_live_at_start, REGNO (x))
877 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
879 if (set == 0 || GET_CODE (set) == CLOBBER)
881 reg_nonzero_bits[REGNO (x)] = GET_MODE_MASK (GET_MODE (x));
882 reg_sign_bit_copies[REGNO (x)] = 1;
883 return;
886 /* If this is a complex assignment, see if we can convert it into a
887 simple assignment. */
888 set = expand_field_assignment (set);
890 /* If this is a simple assignment, or we have a paradoxical SUBREG,
891 set what we know about X. */
893 if (SET_DEST (set) == x
894 || (GET_CODE (SET_DEST (set)) == SUBREG
895 && (GET_MODE_SIZE (GET_MODE (SET_DEST (set)))
896 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (set)))))
897 && SUBREG_REG (SET_DEST (set)) == x))
899 rtx src = SET_SRC (set);
901 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
902 /* If X is narrower than a word and SRC is a non-negative
903 constant that would appear negative in the mode of X,
904 sign-extend it for use in reg_nonzero_bits because some
905 machines (maybe most) will actually do the sign-extension
906 and this is the conservative approach.
908 ??? For 2.5, try to tighten up the MD files in this regard
909 instead of this kludge. */
911 if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
912 && GET_CODE (src) == CONST_INT
913 && INTVAL (src) > 0
914 && 0 != (INTVAL (src)
915 & ((HOST_WIDE_INT) 1
916 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
917 src = GEN_INT (INTVAL (src)
918 | ((HOST_WIDE_INT) (-1)
919 << GET_MODE_BITSIZE (GET_MODE (x))));
920 #endif
922 /* Don't call nonzero_bits if it cannot change anything. */
923 if (reg_nonzero_bits[REGNO (x)] != ~(unsigned HOST_WIDE_INT) 0)
924 reg_nonzero_bits[REGNO (x)]
925 |= nonzero_bits (src, nonzero_bits_mode);
926 num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
927 if (reg_sign_bit_copies[REGNO (x)] == 0
928 || reg_sign_bit_copies[REGNO (x)] > num)
929 reg_sign_bit_copies[REGNO (x)] = num;
931 else
933 reg_nonzero_bits[REGNO (x)] = GET_MODE_MASK (GET_MODE (x));
934 reg_sign_bit_copies[REGNO (x)] = 1;
939 /* See if INSN can be combined into I3. PRED and SUCC are optionally
940 insns that were previously combined into I3 or that will be combined
941 into the merger of INSN and I3.
943 Return 0 if the combination is not allowed for any reason.
945 If the combination is allowed, *PDEST will be set to the single
946 destination of INSN and *PSRC to the single source, and this function
947 will return 1. */
949 static int
950 can_combine_p (rtx insn, rtx i3, rtx pred ATTRIBUTE_UNUSED, rtx succ,
951 rtx *pdest, rtx *psrc)
953 int i;
954 rtx set = 0, src, dest;
955 rtx p;
956 #ifdef AUTO_INC_DEC
957 rtx link;
958 #endif
959 int all_adjacent = (succ ? (next_active_insn (insn) == succ
960 && next_active_insn (succ) == i3)
961 : next_active_insn (insn) == i3);
963 /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
964 or a PARALLEL consisting of such a SET and CLOBBERs.
966 If INSN has CLOBBER parallel parts, ignore them for our processing.
967 By definition, these happen during the execution of the insn. When it
968 is merged with another insn, all bets are off. If they are, in fact,
969 needed and aren't also supplied in I3, they may be added by
970 recog_for_combine. Otherwise, it won't match.
972 We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
973 note.
975 Get the source and destination of INSN. If more than one, can't
976 combine. */
978 if (GET_CODE (PATTERN (insn)) == SET)
979 set = PATTERN (insn);
980 else if (GET_CODE (PATTERN (insn)) == PARALLEL
981 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
983 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
985 rtx elt = XVECEXP (PATTERN (insn), 0, i);
986 rtx note;
988 switch (GET_CODE (elt))
990 /* This is important to combine floating point insns
991 for the SH4 port. */
992 case USE:
993 /* Combining an isolated USE doesn't make sense.
994 We depend here on combinable_i3pat to reject them. */
995 /* The code below this loop only verifies that the inputs of
996 the SET in INSN do not change. We call reg_set_between_p
997 to verify that the REG in the USE does not change between
998 I3 and INSN.
999 If the USE in INSN was for a pseudo register, the matching
1000 insn pattern will likely match any register; combining this
1001 with any other USE would only be safe if we knew that the
1002 used registers have identical values, or if there was
1003 something to tell them apart, e.g. different modes. For
1004 now, we forgo such complicated tests and simply disallow
1005 combining of USES of pseudo registers with any other USE. */
1006 if (GET_CODE (XEXP (elt, 0)) == REG
1007 && GET_CODE (PATTERN (i3)) == PARALLEL)
1009 rtx i3pat = PATTERN (i3);
1010 int i = XVECLEN (i3pat, 0) - 1;
1011 unsigned int regno = REGNO (XEXP (elt, 0));
1015 rtx i3elt = XVECEXP (i3pat, 0, i);
1017 if (GET_CODE (i3elt) == USE
1018 && GET_CODE (XEXP (i3elt, 0)) == REG
1019 && (REGNO (XEXP (i3elt, 0)) == regno
1020 ? reg_set_between_p (XEXP (elt, 0),
1021 PREV_INSN (insn), i3)
1022 : regno >= FIRST_PSEUDO_REGISTER))
1023 return 0;
1025 while (--i >= 0);
1027 break;
1029 /* We can ignore CLOBBERs. */
1030 case CLOBBER:
1031 break;
1033 case SET:
1034 /* Ignore SETs whose result isn't used but not those that
1035 have side-effects. */
1036 if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
1037 && (!(note = find_reg_note (insn, REG_EH_REGION, NULL_RTX))
1038 || INTVAL (XEXP (note, 0)) <= 0)
1039 && ! side_effects_p (elt))
1040 break;
1042 /* If we have already found a SET, this is a second one and
1043 so we cannot combine with this insn. */
1044 if (set)
1045 return 0;
1047 set = elt;
1048 break;
1050 default:
1051 /* Anything else means we can't combine. */
1052 return 0;
1056 if (set == 0
1057 /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
1058 so don't do anything with it. */
1059 || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
1060 return 0;
1062 else
1063 return 0;
1065 if (set == 0)
1066 return 0;
1068 set = expand_field_assignment (set);
1069 src = SET_SRC (set), dest = SET_DEST (set);
1071 /* Don't eliminate a store in the stack pointer. */
1072 if (dest == stack_pointer_rtx
1073 /* Don't combine with an insn that sets a register to itself if it has
1074 a REG_EQUAL note. This may be part of a REG_NO_CONFLICT sequence. */
1075 || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1076 /* Can't merge an ASM_OPERANDS. */
1077 || GET_CODE (src) == ASM_OPERANDS
1078 /* Can't merge a function call. */
1079 || GET_CODE (src) == CALL
1080 /* Don't eliminate a function call argument. */
1081 || (GET_CODE (i3) == CALL_INSN
1082 && (find_reg_fusage (i3, USE, dest)
1083 || (GET_CODE (dest) == REG
1084 && REGNO (dest) < FIRST_PSEUDO_REGISTER
1085 && global_regs[REGNO (dest)])))
1086 /* Don't substitute into an incremented register. */
1087 || FIND_REG_INC_NOTE (i3, dest)
1088 || (succ && FIND_REG_INC_NOTE (succ, dest))
1089 #if 0
1090 /* Don't combine the end of a libcall into anything. */
1091 /* ??? This gives worse code, and appears to be unnecessary, since no
1092 pass after flow uses REG_LIBCALL/REG_RETVAL notes. Local-alloc does
1093 use REG_RETVAL notes for noconflict blocks, but other code here
1094 makes sure that those insns don't disappear. */
1095 || find_reg_note (insn, REG_RETVAL, NULL_RTX)
1096 #endif
1097 /* Make sure that DEST is not used after SUCC but before I3. */
1098 || (succ && ! all_adjacent
1099 && reg_used_between_p (dest, succ, i3))
1100 /* Make sure that the value that is to be substituted for the register
1101 does not use any registers whose values alter in between. However,
1102 If the insns are adjacent, a use can't cross a set even though we
1103 think it might (this can happen for a sequence of insns each setting
1104 the same destination; reg_last_set of that register might point to
1105 a NOTE). If INSN has a REG_EQUIV note, the register is always
1106 equivalent to the memory so the substitution is valid even if there
1107 are intervening stores. Also, don't move a volatile asm or
1108 UNSPEC_VOLATILE across any other insns. */
1109 || (! all_adjacent
1110 && (((GET_CODE (src) != MEM
1111 || ! find_reg_note (insn, REG_EQUIV, src))
1112 && use_crosses_set_p (src, INSN_CUID (insn)))
1113 || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
1114 || GET_CODE (src) == UNSPEC_VOLATILE))
1115 /* If there is a REG_NO_CONFLICT note for DEST in I3 or SUCC, we get
1116 better register allocation by not doing the combine. */
1117 || find_reg_note (i3, REG_NO_CONFLICT, dest)
1118 || (succ && find_reg_note (succ, REG_NO_CONFLICT, dest))
1119 /* Don't combine across a CALL_INSN, because that would possibly
1120 change whether the life span of some REGs crosses calls or not,
1121 and it is a pain to update that information.
1122 Exception: if source is a constant, moving it later can't hurt.
1123 Accept that special case, because it helps -fforce-addr a lot. */
1124 || (INSN_CUID (insn) < last_call_cuid && ! CONSTANT_P (src)))
1125 return 0;
1127 /* DEST must either be a REG or CC0. */
1128 if (GET_CODE (dest) == REG)
1130 /* If register alignment is being enforced for multi-word items in all
1131 cases except for parameters, it is possible to have a register copy
1132 insn referencing a hard register that is not allowed to contain the
1133 mode being copied and which would not be valid as an operand of most
1134 insns. Eliminate this problem by not combining with such an insn.
1136 Also, on some machines we don't want to extend the life of a hard
1137 register. */
1139 if (GET_CODE (src) == REG
1140 && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
1141 && ! HARD_REGNO_MODE_OK (REGNO (dest), GET_MODE (dest)))
1142 /* Don't extend the life of a hard register unless it is
1143 user variable (if we have few registers) or it can't
1144 fit into the desired register (meaning something special
1145 is going on).
1146 Also avoid substituting a return register into I3, because
1147 reload can't handle a conflict with constraints of other
1148 inputs. */
1149 || (REGNO (src) < FIRST_PSEUDO_REGISTER
1150 && ! HARD_REGNO_MODE_OK (REGNO (src), GET_MODE (src)))))
1151 return 0;
1153 else if (GET_CODE (dest) != CC0)
1154 return 0;
1156 /* Don't substitute for a register intended as a clobberable operand.
1157 Similarly, don't substitute an expression containing a register that
1158 will be clobbered in I3. */
1159 if (GET_CODE (PATTERN (i3)) == PARALLEL)
1160 for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
1161 if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER
1162 && (reg_overlap_mentioned_p (XEXP (XVECEXP (PATTERN (i3), 0, i), 0),
1163 src)
1164 || rtx_equal_p (XEXP (XVECEXP (PATTERN (i3), 0, i), 0), dest)))
1165 return 0;
1167 /* If INSN contains anything volatile, or is an `asm' (whether volatile
1168 or not), reject, unless nothing volatile comes between it and I3 */
1170 if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
1172 /* Make sure succ doesn't contain a volatile reference. */
1173 if (succ != 0 && volatile_refs_p (PATTERN (succ)))
1174 return 0;
1176 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1177 if (INSN_P (p) && p != succ && volatile_refs_p (PATTERN (p)))
1178 return 0;
1181 /* If INSN is an asm, and DEST is a hard register, reject, since it has
1182 to be an explicit register variable, and was chosen for a reason. */
1184 if (GET_CODE (src) == ASM_OPERANDS
1185 && GET_CODE (dest) == REG && REGNO (dest) < FIRST_PSEUDO_REGISTER)
1186 return 0;
1188 /* If there are any volatile insns between INSN and I3, reject, because
1189 they might affect machine state. */
1191 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1192 if (INSN_P (p) && p != succ && volatile_insn_p (PATTERN (p)))
1193 return 0;
1195 /* If INSN or I2 contains an autoincrement or autodecrement,
1196 make sure that register is not used between there and I3,
1197 and not already used in I3 either.
1198 Also insist that I3 not be a jump; if it were one
1199 and the incremented register were spilled, we would lose. */
1201 #ifdef AUTO_INC_DEC
1202 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1203 if (REG_NOTE_KIND (link) == REG_INC
1204 && (GET_CODE (i3) == JUMP_INSN
1205 || reg_used_between_p (XEXP (link, 0), insn, i3)
1206 || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
1207 return 0;
1208 #endif
1210 #ifdef HAVE_cc0
1211 /* Don't combine an insn that follows a CC0-setting insn.
1212 An insn that uses CC0 must not be separated from the one that sets it.
1213 We do, however, allow I2 to follow a CC0-setting insn if that insn
1214 is passed as I1; in that case it will be deleted also.
1215 We also allow combining in this case if all the insns are adjacent
1216 because that would leave the two CC0 insns adjacent as well.
1217 It would be more logical to test whether CC0 occurs inside I1 or I2,
1218 but that would be much slower, and this ought to be equivalent. */
1220 p = prev_nonnote_insn (insn);
1221 if (p && p != pred && GET_CODE (p) == INSN && sets_cc0_p (PATTERN (p))
1222 && ! all_adjacent)
1223 return 0;
1224 #endif
1226 /* If we get here, we have passed all the tests and the combination is
1227 to be allowed. */
1229 *pdest = dest;
1230 *psrc = src;
1232 return 1;
1235 /* LOC is the location within I3 that contains its pattern or the component
1236 of a PARALLEL of the pattern. We validate that it is valid for combining.
1238 One problem is if I3 modifies its output, as opposed to replacing it
1239 entirely, we can't allow the output to contain I2DEST or I1DEST as doing
1240 so would produce an insn that is not equivalent to the original insns.
1242 Consider:
1244 (set (reg:DI 101) (reg:DI 100))
1245 (set (subreg:SI (reg:DI 101) 0) <foo>)
1247 This is NOT equivalent to:
1249 (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
1250 (set (reg:DI 101) (reg:DI 100))])
1252 Not only does this modify 100 (in which case it might still be valid
1253 if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
1255 We can also run into a problem if I2 sets a register that I1
1256 uses and I1 gets directly substituted into I3 (not via I2). In that
1257 case, we would be getting the wrong value of I2DEST into I3, so we
1258 must reject the combination. This case occurs when I2 and I1 both
1259 feed into I3, rather than when I1 feeds into I2, which feeds into I3.
1260 If I1_NOT_IN_SRC is nonzero, it means that finding I1 in the source
1261 of a SET must prevent combination from occurring.
1263 Before doing the above check, we first try to expand a field assignment
1264 into a set of logical operations.
1266 If PI3_DEST_KILLED is nonzero, it is a pointer to a location in which
1267 we place a register that is both set and used within I3. If more than one
1268 such register is detected, we fail.
1270 Return 1 if the combination is valid, zero otherwise. */
1272 static int
1273 combinable_i3pat (rtx i3, rtx *loc, rtx i2dest, rtx i1dest,
1274 int i1_not_in_src, rtx *pi3dest_killed)
1276 rtx x = *loc;
1278 if (GET_CODE (x) == SET)
1280 rtx set = x ;
1281 rtx dest = SET_DEST (set);
1282 rtx src = SET_SRC (set);
1283 rtx inner_dest = dest;
1285 while (GET_CODE (inner_dest) == STRICT_LOW_PART
1286 || GET_CODE (inner_dest) == SUBREG
1287 || GET_CODE (inner_dest) == ZERO_EXTRACT)
1288 inner_dest = XEXP (inner_dest, 0);
1290 /* Check for the case where I3 modifies its output, as discussed
1291 above. We don't want to prevent pseudos from being combined
1292 into the address of a MEM, so only prevent the combination if
1293 i1 or i2 set the same MEM. */
1294 if ((inner_dest != dest &&
1295 (GET_CODE (inner_dest) != MEM
1296 || rtx_equal_p (i2dest, inner_dest)
1297 || (i1dest && rtx_equal_p (i1dest, inner_dest)))
1298 && (reg_overlap_mentioned_p (i2dest, inner_dest)
1299 || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))))
1301 /* This is the same test done in can_combine_p except we can't test
1302 all_adjacent; we don't have to, since this instruction will stay
1303 in place, thus we are not considering increasing the lifetime of
1304 INNER_DEST.
1306 Also, if this insn sets a function argument, combining it with
1307 something that might need a spill could clobber a previous
1308 function argument; the all_adjacent test in can_combine_p also
1309 checks this; here, we do a more specific test for this case. */
1311 || (GET_CODE (inner_dest) == REG
1312 && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
1313 && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
1314 GET_MODE (inner_dest))))
1315 || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src)))
1316 return 0;
1318 /* If DEST is used in I3, it is being killed in this insn,
1319 so record that for later.
1320 Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
1321 STACK_POINTER_REGNUM, since these are always considered to be
1322 live. Similarly for ARG_POINTER_REGNUM if it is fixed. */
1323 if (pi3dest_killed && GET_CODE (dest) == REG
1324 && reg_referenced_p (dest, PATTERN (i3))
1325 && REGNO (dest) != FRAME_POINTER_REGNUM
1326 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
1327 && REGNO (dest) != HARD_FRAME_POINTER_REGNUM
1328 #endif
1329 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
1330 && (REGNO (dest) != ARG_POINTER_REGNUM
1331 || ! fixed_regs [REGNO (dest)])
1332 #endif
1333 && REGNO (dest) != STACK_POINTER_REGNUM)
1335 if (*pi3dest_killed)
1336 return 0;
1338 *pi3dest_killed = dest;
1342 else if (GET_CODE (x) == PARALLEL)
1344 int i;
1346 for (i = 0; i < XVECLEN (x, 0); i++)
1347 if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest,
1348 i1_not_in_src, pi3dest_killed))
1349 return 0;
1352 return 1;
1355 /* Return 1 if X is an arithmetic expression that contains a multiplication
1356 and division. We don't count multiplications by powers of two here. */
1358 static int
1359 contains_muldiv (rtx x)
1361 switch (GET_CODE (x))
1363 case MOD: case DIV: case UMOD: case UDIV:
1364 return 1;
1366 case MULT:
1367 return ! (GET_CODE (XEXP (x, 1)) == CONST_INT
1368 && exact_log2 (INTVAL (XEXP (x, 1))) >= 0);
1369 default:
1370 if (BINARY_P (x))
1371 return contains_muldiv (XEXP (x, 0))
1372 || contains_muldiv (XEXP (x, 1));
1374 if (UNARY_P (x))
1375 return contains_muldiv (XEXP (x, 0));
1377 return 0;
1381 /* Determine whether INSN can be used in a combination. Return nonzero if
1382 not. This is used in try_combine to detect early some cases where we
1383 can't perform combinations. */
1385 static int
1386 cant_combine_insn_p (rtx insn)
1388 rtx set;
1389 rtx src, dest;
1391 /* If this isn't really an insn, we can't do anything.
1392 This can occur when flow deletes an insn that it has merged into an
1393 auto-increment address. */
1394 if (! INSN_P (insn))
1395 return 1;
1397 /* Never combine loads and stores involving hard regs that are likely
1398 to be spilled. The register allocator can usually handle such
1399 reg-reg moves by tying. If we allow the combiner to make
1400 substitutions of likely-spilled regs, we may abort in reload.
1401 As an exception, we allow combinations involving fixed regs; these are
1402 not available to the register allocator so there's no risk involved. */
1404 set = single_set (insn);
1405 if (! set)
1406 return 0;
1407 src = SET_SRC (set);
1408 dest = SET_DEST (set);
1409 if (GET_CODE (src) == SUBREG)
1410 src = SUBREG_REG (src);
1411 if (GET_CODE (dest) == SUBREG)
1412 dest = SUBREG_REG (dest);
1413 if (REG_P (src) && REG_P (dest)
1414 && ((REGNO (src) < FIRST_PSEUDO_REGISTER
1415 && ! fixed_regs[REGNO (src)]
1416 && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (REGNO (src))))
1417 || (REGNO (dest) < FIRST_PSEUDO_REGISTER
1418 && ! fixed_regs[REGNO (dest)]
1419 && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (REGNO (dest))))))
1420 return 1;
1422 return 0;
1425 /* Adjust INSN after we made a change to its destination.
1427 Changing the destination can invalidate notes that say something about
1428 the results of the insn and a LOG_LINK pointing to the insn. */
1430 static void
1431 adjust_for_new_dest (rtx insn)
1433 rtx *loc;
1435 /* For notes, be conservative and simply remove them. */
1436 loc = &REG_NOTES (insn);
1437 while (*loc)
1439 enum reg_note kind = REG_NOTE_KIND (*loc);
1440 if (kind == REG_EQUAL || kind == REG_EQUIV)
1441 *loc = XEXP (*loc, 1);
1442 else
1443 loc = &XEXP (*loc, 1);
1446 /* The new insn will have a destination that was previously the destination
1447 of an insn just above it. Call distribute_links to make a LOG_LINK from
1448 the next use of that destination. */
1449 distribute_links (gen_rtx_INSN_LIST (VOIDmode, insn, NULL_RTX));
1452 /* Try to combine the insns I1 and I2 into I3.
1453 Here I1 and I2 appear earlier than I3.
1454 I1 can be zero; then we combine just I2 into I3.
1456 If we are combining three insns and the resulting insn is not recognized,
1457 try splitting it into two insns. If that happens, I2 and I3 are retained
1458 and I1 is pseudo-deleted by turning it into a NOTE. Otherwise, I1 and I2
1459 are pseudo-deleted.
1461 Return 0 if the combination does not work. Then nothing is changed.
1462 If we did the combination, return the insn at which combine should
1463 resume scanning.
1465 Set NEW_DIRECT_JUMP_P to a nonzero value if try_combine creates a
1466 new direct jump instruction. */
1468 static rtx
1469 try_combine (rtx i3, rtx i2, rtx i1, int *new_direct_jump_p)
1471 /* New patterns for I3 and I2, respectively. */
1472 rtx newpat, newi2pat = 0;
1473 int substed_i2 = 0, substed_i1 = 0;
1474 /* Indicates need to preserve SET in I1 or I2 in I3 if it is not dead. */
1475 int added_sets_1, added_sets_2;
1476 /* Total number of SETs to put into I3. */
1477 int total_sets;
1478 /* Nonzero if I2's body now appears in I3. */
1479 int i2_is_used;
1480 /* INSN_CODEs for new I3, new I2, and user of condition code. */
1481 int insn_code_number, i2_code_number = 0, other_code_number = 0;
1482 /* Contains I3 if the destination of I3 is used in its source, which means
1483 that the old life of I3 is being killed. If that usage is placed into
1484 I2 and not in I3, a REG_DEAD note must be made. */
1485 rtx i3dest_killed = 0;
1486 /* SET_DEST and SET_SRC of I2 and I1. */
1487 rtx i2dest, i2src, i1dest = 0, i1src = 0;
1488 /* PATTERN (I2), or a copy of it in certain cases. */
1489 rtx i2pat;
1490 /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC. */
1491 int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
1492 int i1_feeds_i3 = 0;
1493 /* Notes that must be added to REG_NOTES in I3 and I2. */
1494 rtx new_i3_notes, new_i2_notes;
1495 /* Notes that we substituted I3 into I2 instead of the normal case. */
1496 int i3_subst_into_i2 = 0;
1497 /* Notes that I1, I2 or I3 is a MULT operation. */
1498 int have_mult = 0;
1500 int maxreg;
1501 rtx temp;
1502 rtx link;
1503 int i;
1505 /* Exit early if one of the insns involved can't be used for
1506 combinations. */
1507 if (cant_combine_insn_p (i3)
1508 || cant_combine_insn_p (i2)
1509 || (i1 && cant_combine_insn_p (i1))
1510 /* We also can't do anything if I3 has a
1511 REG_LIBCALL note since we don't want to disrupt the contiguity of a
1512 libcall. */
1513 #if 0
1514 /* ??? This gives worse code, and appears to be unnecessary, since no
1515 pass after flow uses REG_LIBCALL/REG_RETVAL notes. */
1516 || find_reg_note (i3, REG_LIBCALL, NULL_RTX)
1517 #endif
1519 return 0;
1521 combine_attempts++;
1522 undobuf.other_insn = 0;
1524 /* Reset the hard register usage information. */
1525 CLEAR_HARD_REG_SET (newpat_used_regs);
1527 /* If I1 and I2 both feed I3, they can be in any order. To simplify the
1528 code below, set I1 to be the earlier of the two insns. */
1529 if (i1 && INSN_CUID (i1) > INSN_CUID (i2))
1530 temp = i1, i1 = i2, i2 = temp;
1532 added_links_insn = 0;
1534 /* First check for one important special-case that the code below will
1535 not handle. Namely, the case where I1 is zero, I2 is a PARALLEL
1536 and I3 is a SET whose SET_SRC is a SET_DEST in I2. In that case,
1537 we may be able to replace that destination with the destination of I3.
1538 This occurs in the common code where we compute both a quotient and
1539 remainder into a structure, in which case we want to do the computation
1540 directly into the structure to avoid register-register copies.
1542 Note that this case handles both multiple sets in I2 and also
1543 cases where I2 has a number of CLOBBER or PARALLELs.
1545 We make very conservative checks below and only try to handle the
1546 most common cases of this. For example, we only handle the case
1547 where I2 and I3 are adjacent to avoid making difficult register
1548 usage tests. */
1550 if (i1 == 0 && GET_CODE (i3) == INSN && GET_CODE (PATTERN (i3)) == SET
1551 && GET_CODE (SET_SRC (PATTERN (i3))) == REG
1552 && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
1553 && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
1554 && GET_CODE (PATTERN (i2)) == PARALLEL
1555 && ! side_effects_p (SET_DEST (PATTERN (i3)))
1556 /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
1557 below would need to check what is inside (and reg_overlap_mentioned_p
1558 doesn't support those codes anyway). Don't allow those destinations;
1559 the resulting insn isn't likely to be recognized anyway. */
1560 && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
1561 && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
1562 && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
1563 SET_DEST (PATTERN (i3)))
1564 && next_real_insn (i2) == i3)
1566 rtx p2 = PATTERN (i2);
1568 /* Make sure that the destination of I3,
1569 which we are going to substitute into one output of I2,
1570 is not used within another output of I2. We must avoid making this:
1571 (parallel [(set (mem (reg 69)) ...)
1572 (set (reg 69) ...)])
1573 which is not well-defined as to order of actions.
1574 (Besides, reload can't handle output reloads for this.)
1576 The problem can also happen if the dest of I3 is a memory ref,
1577 if another dest in I2 is an indirect memory ref. */
1578 for (i = 0; i < XVECLEN (p2, 0); i++)
1579 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
1580 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
1581 && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
1582 SET_DEST (XVECEXP (p2, 0, i))))
1583 break;
1585 if (i == XVECLEN (p2, 0))
1586 for (i = 0; i < XVECLEN (p2, 0); i++)
1587 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
1588 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
1589 && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
1591 combine_merges++;
1593 subst_insn = i3;
1594 subst_low_cuid = INSN_CUID (i2);
1596 added_sets_2 = added_sets_1 = 0;
1597 i2dest = SET_SRC (PATTERN (i3));
1599 /* Replace the dest in I2 with our dest and make the resulting
1600 insn the new pattern for I3. Then skip to where we
1601 validate the pattern. Everything was set up above. */
1602 SUBST (SET_DEST (XVECEXP (p2, 0, i)),
1603 SET_DEST (PATTERN (i3)));
1605 newpat = p2;
1606 i3_subst_into_i2 = 1;
1607 goto validate_replacement;
1611 /* If I2 is setting a double-word pseudo to a constant and I3 is setting
1612 one of those words to another constant, merge them by making a new
1613 constant. */
1614 if (i1 == 0
1615 && (temp = single_set (i2)) != 0
1616 && (GET_CODE (SET_SRC (temp)) == CONST_INT
1617 || GET_CODE (SET_SRC (temp)) == CONST_DOUBLE)
1618 && GET_CODE (SET_DEST (temp)) == REG
1619 && GET_MODE_CLASS (GET_MODE (SET_DEST (temp))) == MODE_INT
1620 && GET_MODE_SIZE (GET_MODE (SET_DEST (temp))) == 2 * UNITS_PER_WORD
1621 && GET_CODE (PATTERN (i3)) == SET
1622 && GET_CODE (SET_DEST (PATTERN (i3))) == SUBREG
1623 && SUBREG_REG (SET_DEST (PATTERN (i3))) == SET_DEST (temp)
1624 && GET_MODE_CLASS (GET_MODE (SET_DEST (PATTERN (i3)))) == MODE_INT
1625 && GET_MODE_SIZE (GET_MODE (SET_DEST (PATTERN (i3)))) == UNITS_PER_WORD
1626 && GET_CODE (SET_SRC (PATTERN (i3))) == CONST_INT)
1628 HOST_WIDE_INT lo, hi;
1630 if (GET_CODE (SET_SRC (temp)) == CONST_INT)
1631 lo = INTVAL (SET_SRC (temp)), hi = lo < 0 ? -1 : 0;
1632 else
1634 lo = CONST_DOUBLE_LOW (SET_SRC (temp));
1635 hi = CONST_DOUBLE_HIGH (SET_SRC (temp));
1638 if (subreg_lowpart_p (SET_DEST (PATTERN (i3))))
1640 /* We don't handle the case of the target word being wider
1641 than a host wide int. */
1642 if (HOST_BITS_PER_WIDE_INT < BITS_PER_WORD)
1643 abort ();
1645 lo &= ~(UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD (1) - 1);
1646 lo |= (INTVAL (SET_SRC (PATTERN (i3)))
1647 & (UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD (1) - 1));
1649 else if (HOST_BITS_PER_WIDE_INT == BITS_PER_WORD)
1650 hi = INTVAL (SET_SRC (PATTERN (i3)));
1651 else if (HOST_BITS_PER_WIDE_INT >= 2 * BITS_PER_WORD)
1653 int sign = -(int) ((unsigned HOST_WIDE_INT) lo
1654 >> (HOST_BITS_PER_WIDE_INT - 1));
1656 lo &= ~ (UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD
1657 (UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD (1) - 1));
1658 lo |= (UWIDE_SHIFT_LEFT_BY_BITS_PER_WORD
1659 (INTVAL (SET_SRC (PATTERN (i3)))));
1660 if (hi == sign)
1661 hi = lo < 0 ? -1 : 0;
1663 else
1664 /* We don't handle the case of the higher word not fitting
1665 entirely in either hi or lo. */
1666 abort ();
1668 combine_merges++;
1669 subst_insn = i3;
1670 subst_low_cuid = INSN_CUID (i2);
1671 added_sets_2 = added_sets_1 = 0;
1672 i2dest = SET_DEST (temp);
1674 SUBST (SET_SRC (temp),
1675 immed_double_const (lo, hi, GET_MODE (SET_DEST (temp))));
1677 newpat = PATTERN (i2);
1678 goto validate_replacement;
1681 #ifndef HAVE_cc0
1682 /* If we have no I1 and I2 looks like:
1683 (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
1684 (set Y OP)])
1685 make up a dummy I1 that is
1686 (set Y OP)
1687 and change I2 to be
1688 (set (reg:CC X) (compare:CC Y (const_int 0)))
1690 (We can ignore any trailing CLOBBERs.)
1692 This undoes a previous combination and allows us to match a branch-and-
1693 decrement insn. */
1695 if (i1 == 0 && GET_CODE (PATTERN (i2)) == PARALLEL
1696 && XVECLEN (PATTERN (i2), 0) >= 2
1697 && GET_CODE (XVECEXP (PATTERN (i2), 0, 0)) == SET
1698 && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
1699 == MODE_CC)
1700 && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
1701 && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
1702 && GET_CODE (XVECEXP (PATTERN (i2), 0, 1)) == SET
1703 && GET_CODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 1))) == REG
1704 && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
1705 SET_SRC (XVECEXP (PATTERN (i2), 0, 1))))
1707 for (i = XVECLEN (PATTERN (i2), 0) - 1; i >= 2; i--)
1708 if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != CLOBBER)
1709 break;
1711 if (i == 1)
1713 /* We make I1 with the same INSN_UID as I2. This gives it
1714 the same INSN_CUID for value tracking. Our fake I1 will
1715 never appear in the insn stream so giving it the same INSN_UID
1716 as I2 will not cause a problem. */
1718 i1 = gen_rtx_INSN (VOIDmode, INSN_UID (i2), NULL_RTX, i2,
1719 BLOCK_FOR_INSN (i2), INSN_LOCATOR (i2),
1720 XVECEXP (PATTERN (i2), 0, 1), -1, NULL_RTX,
1721 NULL_RTX);
1723 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
1724 SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
1725 SET_DEST (PATTERN (i1)));
1728 #endif
1730 /* Verify that I2 and I1 are valid for combining. */
1731 if (! can_combine_p (i2, i3, i1, NULL_RTX, &i2dest, &i2src)
1732 || (i1 && ! can_combine_p (i1, i3, NULL_RTX, i2, &i1dest, &i1src)))
1734 undo_all ();
1735 return 0;
1738 /* Record whether I2DEST is used in I2SRC and similarly for the other
1739 cases. Knowing this will help in register status updating below. */
1740 i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
1741 i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
1742 i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
1744 /* See if I1 directly feeds into I3. It does if I1DEST is not used
1745 in I2SRC. */
1746 i1_feeds_i3 = i1 && ! reg_overlap_mentioned_p (i1dest, i2src);
1748 /* Ensure that I3's pattern can be the destination of combines. */
1749 if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest,
1750 i1 && i2dest_in_i1src && i1_feeds_i3,
1751 &i3dest_killed))
1753 undo_all ();
1754 return 0;
1757 /* See if any of the insns is a MULT operation. Unless one is, we will
1758 reject a combination that is, since it must be slower. Be conservative
1759 here. */
1760 if (GET_CODE (i2src) == MULT
1761 || (i1 != 0 && GET_CODE (i1src) == MULT)
1762 || (GET_CODE (PATTERN (i3)) == SET
1763 && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
1764 have_mult = 1;
1766 /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
1767 We used to do this EXCEPT in one case: I3 has a post-inc in an
1768 output operand. However, that exception can give rise to insns like
1769 mov r3,(r3)+
1770 which is a famous insn on the PDP-11 where the value of r3 used as the
1771 source was model-dependent. Avoid this sort of thing. */
1773 #if 0
1774 if (!(GET_CODE (PATTERN (i3)) == SET
1775 && GET_CODE (SET_SRC (PATTERN (i3))) == REG
1776 && GET_CODE (SET_DEST (PATTERN (i3))) == MEM
1777 && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
1778 || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
1779 /* It's not the exception. */
1780 #endif
1781 #ifdef AUTO_INC_DEC
1782 for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
1783 if (REG_NOTE_KIND (link) == REG_INC
1784 && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
1785 || (i1 != 0
1786 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
1788 undo_all ();
1789 return 0;
1791 #endif
1793 /* See if the SETs in I1 or I2 need to be kept around in the merged
1794 instruction: whenever the value set there is still needed past I3.
1795 For the SETs in I2, this is easy: we see if I2DEST dies or is set in I3.
1797 For the SET in I1, we have two cases: If I1 and I2 independently
1798 feed into I3, the set in I1 needs to be kept around if I1DEST dies
1799 or is set in I3. Otherwise (if I1 feeds I2 which feeds I3), the set
1800 in I1 needs to be kept around unless I1DEST dies or is set in either
1801 I2 or I3. We can distinguish these cases by seeing if I2SRC mentions
1802 I1DEST. If so, we know I1 feeds into I2. */
1804 added_sets_2 = ! dead_or_set_p (i3, i2dest);
1806 added_sets_1
1807 = i1 && ! (i1_feeds_i3 ? dead_or_set_p (i3, i1dest)
1808 : (dead_or_set_p (i3, i1dest) || dead_or_set_p (i2, i1dest)));
1810 /* If the set in I2 needs to be kept around, we must make a copy of
1811 PATTERN (I2), so that when we substitute I1SRC for I1DEST in
1812 PATTERN (I2), we are only substituting for the original I1DEST, not into
1813 an already-substituted copy. This also prevents making self-referential
1814 rtx. If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
1815 I2DEST. */
1817 i2pat = (GET_CODE (PATTERN (i2)) == PARALLEL
1818 ? gen_rtx_SET (VOIDmode, i2dest, i2src)
1819 : PATTERN (i2));
1821 if (added_sets_2)
1822 i2pat = copy_rtx (i2pat);
1824 combine_merges++;
1826 /* Substitute in the latest insn for the regs set by the earlier ones. */
1828 maxreg = max_reg_num ();
1830 subst_insn = i3;
1832 /* It is possible that the source of I2 or I1 may be performing an
1833 unneeded operation, such as a ZERO_EXTEND of something that is known
1834 to have the high part zero. Handle that case by letting subst look at
1835 the innermost one of them.
1837 Another way to do this would be to have a function that tries to
1838 simplify a single insn instead of merging two or more insns. We don't
1839 do this because of the potential of infinite loops and because
1840 of the potential extra memory required. However, doing it the way
1841 we are is a bit of a kludge and doesn't catch all cases.
1843 But only do this if -fexpensive-optimizations since it slows things down
1844 and doesn't usually win. */
1846 if (flag_expensive_optimizations)
1848 /* Pass pc_rtx so no substitutions are done, just simplifications. */
1849 if (i1)
1851 subst_low_cuid = INSN_CUID (i1);
1852 i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0);
1854 else
1856 subst_low_cuid = INSN_CUID (i2);
1857 i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0);
1861 #ifndef HAVE_cc0
1862 /* Many machines that don't use CC0 have insns that can both perform an
1863 arithmetic operation and set the condition code. These operations will
1864 be represented as a PARALLEL with the first element of the vector
1865 being a COMPARE of an arithmetic operation with the constant zero.
1866 The second element of the vector will set some pseudo to the result
1867 of the same arithmetic operation. If we simplify the COMPARE, we won't
1868 match such a pattern and so will generate an extra insn. Here we test
1869 for this case, where both the comparison and the operation result are
1870 needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
1871 I2SRC. Later we will make the PARALLEL that contains I2. */
1873 if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
1874 && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
1875 && XEXP (SET_SRC (PATTERN (i3)), 1) == const0_rtx
1876 && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
1878 #ifdef SELECT_CC_MODE
1879 rtx *cc_use;
1880 enum machine_mode compare_mode;
1881 #endif
1883 newpat = PATTERN (i3);
1884 SUBST (XEXP (SET_SRC (newpat), 0), i2src);
1886 i2_is_used = 1;
1888 #ifdef SELECT_CC_MODE
1889 /* See if a COMPARE with the operand we substituted in should be done
1890 with the mode that is currently being used. If not, do the same
1891 processing we do in `subst' for a SET; namely, if the destination
1892 is used only once, try to replace it with a register of the proper
1893 mode and also replace the COMPARE. */
1894 if (undobuf.other_insn == 0
1895 && (cc_use = find_single_use (SET_DEST (newpat), i3,
1896 &undobuf.other_insn))
1897 && ((compare_mode = SELECT_CC_MODE (GET_CODE (*cc_use),
1898 i2src, const0_rtx))
1899 != GET_MODE (SET_DEST (newpat))))
1901 unsigned int regno = REGNO (SET_DEST (newpat));
1902 rtx new_dest = gen_rtx_REG (compare_mode, regno);
1904 if (regno < FIRST_PSEUDO_REGISTER
1905 || (REG_N_SETS (regno) == 1 && ! added_sets_2
1906 && ! REG_USERVAR_P (SET_DEST (newpat))))
1908 if (regno >= FIRST_PSEUDO_REGISTER)
1909 SUBST (regno_reg_rtx[regno], new_dest);
1911 SUBST (SET_DEST (newpat), new_dest);
1912 SUBST (XEXP (*cc_use, 0), new_dest);
1913 SUBST (SET_SRC (newpat),
1914 gen_rtx_COMPARE (compare_mode, i2src, const0_rtx));
1916 else
1917 undobuf.other_insn = 0;
1919 #endif
1921 else
1922 #endif
1924 n_occurrences = 0; /* `subst' counts here */
1926 /* If I1 feeds into I2 (not into I3) and I1DEST is in I1SRC, we
1927 need to make a unique copy of I2SRC each time we substitute it
1928 to avoid self-referential rtl. */
1930 subst_low_cuid = INSN_CUID (i2);
1931 newpat = subst (PATTERN (i3), i2dest, i2src, 0,
1932 ! i1_feeds_i3 && i1dest_in_i1src);
1933 substed_i2 = 1;
1935 /* Record whether i2's body now appears within i3's body. */
1936 i2_is_used = n_occurrences;
1939 /* If we already got a failure, don't try to do more. Otherwise,
1940 try to substitute in I1 if we have it. */
1942 if (i1 && GET_CODE (newpat) != CLOBBER)
1944 /* Before we can do this substitution, we must redo the test done
1945 above (see detailed comments there) that ensures that I1DEST
1946 isn't mentioned in any SETs in NEWPAT that are field assignments. */
1948 if (! combinable_i3pat (NULL_RTX, &newpat, i1dest, NULL_RTX,
1949 0, (rtx*) 0))
1951 undo_all ();
1952 return 0;
1955 n_occurrences = 0;
1956 subst_low_cuid = INSN_CUID (i1);
1957 newpat = subst (newpat, i1dest, i1src, 0, 0);
1958 substed_i1 = 1;
1961 /* Fail if an autoincrement side-effect has been duplicated. Be careful
1962 to count all the ways that I2SRC and I1SRC can be used. */
1963 if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
1964 && i2_is_used + added_sets_2 > 1)
1965 || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
1966 && (n_occurrences + added_sets_1 + (added_sets_2 && ! i1_feeds_i3)
1967 > 1))
1968 /* Fail if we tried to make a new register (we used to abort, but there's
1969 really no reason to). */
1970 || max_reg_num () != maxreg
1971 /* Fail if we couldn't do something and have a CLOBBER. */
1972 || GET_CODE (newpat) == CLOBBER
1973 /* Fail if this new pattern is a MULT and we didn't have one before
1974 at the outer level. */
1975 || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
1976 && ! have_mult))
1978 undo_all ();
1979 return 0;
1982 /* If the actions of the earlier insns must be kept
1983 in addition to substituting them into the latest one,
1984 we must make a new PARALLEL for the latest insn
1985 to hold additional the SETs. */
1987 if (added_sets_1 || added_sets_2)
1989 combine_extras++;
1991 if (GET_CODE (newpat) == PARALLEL)
1993 rtvec old = XVEC (newpat, 0);
1994 total_sets = XVECLEN (newpat, 0) + added_sets_1 + added_sets_2;
1995 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
1996 memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
1997 sizeof (old->elem[0]) * old->num_elem);
1999 else
2001 rtx old = newpat;
2002 total_sets = 1 + added_sets_1 + added_sets_2;
2003 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
2004 XVECEXP (newpat, 0, 0) = old;
2007 if (added_sets_1)
2008 XVECEXP (newpat, 0, --total_sets)
2009 = (GET_CODE (PATTERN (i1)) == PARALLEL
2010 ? gen_rtx_SET (VOIDmode, i1dest, i1src) : PATTERN (i1));
2012 if (added_sets_2)
2014 /* If there is no I1, use I2's body as is. We used to also not do
2015 the subst call below if I2 was substituted into I3,
2016 but that could lose a simplification. */
2017 if (i1 == 0)
2018 XVECEXP (newpat, 0, --total_sets) = i2pat;
2019 else
2020 /* See comment where i2pat is assigned. */
2021 XVECEXP (newpat, 0, --total_sets)
2022 = subst (i2pat, i1dest, i1src, 0, 0);
2026 /* We come here when we are replacing a destination in I2 with the
2027 destination of I3. */
2028 validate_replacement:
2030 /* Note which hard regs this insn has as inputs. */
2031 mark_used_regs_combine (newpat);
2033 /* Is the result of combination a valid instruction? */
2034 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2036 /* If the result isn't valid, see if it is a PARALLEL of two SETs where
2037 the second SET's destination is a register that is unused and isn't
2038 marked as an instruction that might trap in an EH region. In that case,
2039 we just need the first SET. This can occur when simplifying a divmod
2040 insn. We *must* test for this case here because the code below that
2041 splits two independent SETs doesn't handle this case correctly when it
2042 updates the register status.
2044 It's pointless doing this if we originally had two sets, one from
2045 i3, and one from i2. Combining then splitting the parallel results
2046 in the original i2 again plus an invalid insn (which we delete).
2047 The net effect is only to move instructions around, which makes
2048 debug info less accurate.
2050 Also check the case where the first SET's destination is unused.
2051 That would not cause incorrect code, but does cause an unneeded
2052 insn to remain. */
2054 if (insn_code_number < 0
2055 && !(added_sets_2 && i1 == 0)
2056 && GET_CODE (newpat) == PARALLEL
2057 && XVECLEN (newpat, 0) == 2
2058 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2059 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2060 && asm_noperands (newpat) < 0)
2062 rtx set0 = XVECEXP (newpat, 0, 0);
2063 rtx set1 = XVECEXP (newpat, 0, 1);
2064 rtx note;
2066 if (((GET_CODE (SET_DEST (set1)) == REG
2067 && find_reg_note (i3, REG_UNUSED, SET_DEST (set1)))
2068 || (GET_CODE (SET_DEST (set1)) == SUBREG
2069 && find_reg_note (i3, REG_UNUSED, SUBREG_REG (SET_DEST (set1)))))
2070 && (!(note = find_reg_note (i3, REG_EH_REGION, NULL_RTX))
2071 || INTVAL (XEXP (note, 0)) <= 0)
2072 && ! side_effects_p (SET_SRC (set1)))
2074 newpat = set0;
2075 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2078 else if (((GET_CODE (SET_DEST (set0)) == REG
2079 && find_reg_note (i3, REG_UNUSED, SET_DEST (set0)))
2080 || (GET_CODE (SET_DEST (set0)) == SUBREG
2081 && find_reg_note (i3, REG_UNUSED,
2082 SUBREG_REG (SET_DEST (set0)))))
2083 && (!(note = find_reg_note (i3, REG_EH_REGION, NULL_RTX))
2084 || INTVAL (XEXP (note, 0)) <= 0)
2085 && ! side_effects_p (SET_SRC (set0)))
2087 newpat = set1;
2088 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2090 if (insn_code_number >= 0)
2092 /* If we will be able to accept this, we have made a
2093 change to the destination of I3. This requires us to
2094 do a few adjustments. */
2096 PATTERN (i3) = newpat;
2097 adjust_for_new_dest (i3);
2102 /* If we were combining three insns and the result is a simple SET
2103 with no ASM_OPERANDS that wasn't recognized, try to split it into two
2104 insns. There are two ways to do this. It can be split using a
2105 machine-specific method (like when you have an addition of a large
2106 constant) or by combine in the function find_split_point. */
2108 if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
2109 && asm_noperands (newpat) < 0)
2111 rtx m_split, *split;
2112 rtx ni2dest = i2dest;
2114 /* See if the MD file can split NEWPAT. If it can't, see if letting it
2115 use I2DEST as a scratch register will help. In the latter case,
2116 convert I2DEST to the mode of the source of NEWPAT if we can. */
2118 m_split = split_insns (newpat, i3);
2120 /* We can only use I2DEST as a scratch reg if it doesn't overlap any
2121 inputs of NEWPAT. */
2123 /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
2124 possible to try that as a scratch reg. This would require adding
2125 more code to make it work though. */
2127 if (m_split == 0 && ! reg_overlap_mentioned_p (ni2dest, newpat))
2129 /* If I2DEST is a hard register or the only use of a pseudo,
2130 we can change its mode. */
2131 if (GET_MODE (SET_DEST (newpat)) != GET_MODE (i2dest)
2132 && GET_MODE (SET_DEST (newpat)) != VOIDmode
2133 && GET_CODE (i2dest) == REG
2134 && (REGNO (i2dest) < FIRST_PSEUDO_REGISTER
2135 || (REG_N_SETS (REGNO (i2dest)) == 1 && ! added_sets_2
2136 && ! REG_USERVAR_P (i2dest))))
2137 ni2dest = gen_rtx_REG (GET_MODE (SET_DEST (newpat)),
2138 REGNO (i2dest));
2140 m_split = split_insns (gen_rtx_PARALLEL
2141 (VOIDmode,
2142 gen_rtvec (2, newpat,
2143 gen_rtx_CLOBBER (VOIDmode,
2144 ni2dest))),
2145 i3);
2146 /* If the split with the mode-changed register didn't work, try
2147 the original register. */
2148 if (! m_split && ni2dest != i2dest)
2150 ni2dest = i2dest;
2151 m_split = split_insns (gen_rtx_PARALLEL
2152 (VOIDmode,
2153 gen_rtvec (2, newpat,
2154 gen_rtx_CLOBBER (VOIDmode,
2155 i2dest))),
2156 i3);
2160 if (m_split && NEXT_INSN (m_split) == NULL_RTX)
2162 m_split = PATTERN (m_split);
2163 insn_code_number = recog_for_combine (&m_split, i3, &new_i3_notes);
2164 if (insn_code_number >= 0)
2165 newpat = m_split;
2167 else if (m_split && NEXT_INSN (NEXT_INSN (m_split)) == NULL_RTX
2168 && (next_real_insn (i2) == i3
2169 || ! use_crosses_set_p (PATTERN (m_split), INSN_CUID (i2))))
2171 rtx i2set, i3set;
2172 rtx newi3pat = PATTERN (NEXT_INSN (m_split));
2173 newi2pat = PATTERN (m_split);
2175 i3set = single_set (NEXT_INSN (m_split));
2176 i2set = single_set (m_split);
2178 /* In case we changed the mode of I2DEST, replace it in the
2179 pseudo-register table here. We can't do it above in case this
2180 code doesn't get executed and we do a split the other way. */
2182 if (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
2183 SUBST (regno_reg_rtx[REGNO (i2dest)], ni2dest);
2185 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2187 /* If I2 or I3 has multiple SETs, we won't know how to track
2188 register status, so don't use these insns. If I2's destination
2189 is used between I2 and I3, we also can't use these insns. */
2191 if (i2_code_number >= 0 && i2set && i3set
2192 && (next_real_insn (i2) == i3
2193 || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
2194 insn_code_number = recog_for_combine (&newi3pat, i3,
2195 &new_i3_notes);
2196 if (insn_code_number >= 0)
2197 newpat = newi3pat;
2199 /* It is possible that both insns now set the destination of I3.
2200 If so, we must show an extra use of it. */
2202 if (insn_code_number >= 0)
2204 rtx new_i3_dest = SET_DEST (i3set);
2205 rtx new_i2_dest = SET_DEST (i2set);
2207 while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
2208 || GET_CODE (new_i3_dest) == STRICT_LOW_PART
2209 || GET_CODE (new_i3_dest) == SUBREG)
2210 new_i3_dest = XEXP (new_i3_dest, 0);
2212 while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
2213 || GET_CODE (new_i2_dest) == STRICT_LOW_PART
2214 || GET_CODE (new_i2_dest) == SUBREG)
2215 new_i2_dest = XEXP (new_i2_dest, 0);
2217 if (GET_CODE (new_i3_dest) == REG
2218 && GET_CODE (new_i2_dest) == REG
2219 && REGNO (new_i3_dest) == REGNO (new_i2_dest))
2220 REG_N_SETS (REGNO (new_i2_dest))++;
2224 /* If we can split it and use I2DEST, go ahead and see if that
2225 helps things be recognized. Verify that none of the registers
2226 are set between I2 and I3. */
2227 if (insn_code_number < 0 && (split = find_split_point (&newpat, i3)) != 0
2228 #ifdef HAVE_cc0
2229 && GET_CODE (i2dest) == REG
2230 #endif
2231 /* We need I2DEST in the proper mode. If it is a hard register
2232 or the only use of a pseudo, we can change its mode. */
2233 && (GET_MODE (*split) == GET_MODE (i2dest)
2234 || GET_MODE (*split) == VOIDmode
2235 || REGNO (i2dest) < FIRST_PSEUDO_REGISTER
2236 || (REG_N_SETS (REGNO (i2dest)) == 1 && ! added_sets_2
2237 && ! REG_USERVAR_P (i2dest)))
2238 && (next_real_insn (i2) == i3
2239 || ! use_crosses_set_p (*split, INSN_CUID (i2)))
2240 /* We can't overwrite I2DEST if its value is still used by
2241 NEWPAT. */
2242 && ! reg_referenced_p (i2dest, newpat))
2244 rtx newdest = i2dest;
2245 enum rtx_code split_code = GET_CODE (*split);
2246 enum machine_mode split_mode = GET_MODE (*split);
2248 /* Get NEWDEST as a register in the proper mode. We have already
2249 validated that we can do this. */
2250 if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
2252 newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
2254 if (REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
2255 SUBST (regno_reg_rtx[REGNO (i2dest)], newdest);
2258 /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
2259 an ASHIFT. This can occur if it was inside a PLUS and hence
2260 appeared to be a memory address. This is a kludge. */
2261 if (split_code == MULT
2262 && GET_CODE (XEXP (*split, 1)) == CONST_INT
2263 && INTVAL (XEXP (*split, 1)) > 0
2264 && (i = exact_log2 (INTVAL (XEXP (*split, 1)))) >= 0)
2266 SUBST (*split, gen_rtx_ASHIFT (split_mode,
2267 XEXP (*split, 0), GEN_INT (i)));
2268 /* Update split_code because we may not have a multiply
2269 anymore. */
2270 split_code = GET_CODE (*split);
2273 #ifdef INSN_SCHEDULING
2274 /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
2275 be written as a ZERO_EXTEND. */
2276 if (split_code == SUBREG && GET_CODE (SUBREG_REG (*split)) == MEM)
2278 #ifdef LOAD_EXTEND_OP
2279 /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
2280 what it really is. */
2281 if (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (*split)))
2282 == SIGN_EXTEND)
2283 SUBST (*split, gen_rtx_SIGN_EXTEND (split_mode,
2284 SUBREG_REG (*split)));
2285 else
2286 #endif
2287 SUBST (*split, gen_rtx_ZERO_EXTEND (split_mode,
2288 SUBREG_REG (*split)));
2290 #endif
2292 newi2pat = gen_rtx_SET (VOIDmode, newdest, *split);
2293 SUBST (*split, newdest);
2294 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2296 /* If the split point was a MULT and we didn't have one before,
2297 don't use one now. */
2298 if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
2299 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2303 /* Check for a case where we loaded from memory in a narrow mode and
2304 then sign extended it, but we need both registers. In that case,
2305 we have a PARALLEL with both loads from the same memory location.
2306 We can split this into a load from memory followed by a register-register
2307 copy. This saves at least one insn, more if register allocation can
2308 eliminate the copy.
2310 We cannot do this if the destination of the first assignment is a
2311 condition code register or cc0. We eliminate this case by making sure
2312 the SET_DEST and SET_SRC have the same mode.
2314 We cannot do this if the destination of the second assignment is
2315 a register that we have already assumed is zero-extended. Similarly
2316 for a SUBREG of such a register. */
2318 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
2319 && GET_CODE (newpat) == PARALLEL
2320 && XVECLEN (newpat, 0) == 2
2321 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2322 && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
2323 && (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0)))
2324 == GET_MODE (SET_SRC (XVECEXP (newpat, 0, 0))))
2325 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2326 && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2327 XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
2328 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2329 INSN_CUID (i2))
2330 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
2331 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
2332 && ! (temp = SET_DEST (XVECEXP (newpat, 0, 1)),
2333 (GET_CODE (temp) == REG
2334 && reg_nonzero_bits[REGNO (temp)] != 0
2335 && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
2336 && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
2337 && (reg_nonzero_bits[REGNO (temp)]
2338 != GET_MODE_MASK (word_mode))))
2339 && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
2340 && (temp = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
2341 (GET_CODE (temp) == REG
2342 && reg_nonzero_bits[REGNO (temp)] != 0
2343 && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
2344 && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
2345 && (reg_nonzero_bits[REGNO (temp)]
2346 != GET_MODE_MASK (word_mode)))))
2347 && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
2348 SET_SRC (XVECEXP (newpat, 0, 1)))
2349 && ! find_reg_note (i3, REG_UNUSED,
2350 SET_DEST (XVECEXP (newpat, 0, 0))))
2352 rtx ni2dest;
2354 newi2pat = XVECEXP (newpat, 0, 0);
2355 ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
2356 newpat = XVECEXP (newpat, 0, 1);
2357 SUBST (SET_SRC (newpat),
2358 gen_lowpart (GET_MODE (SET_SRC (newpat)), ni2dest));
2359 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2361 if (i2_code_number >= 0)
2362 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2364 if (insn_code_number >= 0)
2366 rtx insn;
2367 rtx link;
2369 /* If we will be able to accept this, we have made a change to the
2370 destination of I3. This requires us to do a few adjustments. */
2371 PATTERN (i3) = newpat;
2372 adjust_for_new_dest (i3);
2374 /* I3 now uses what used to be its destination and which is
2375 now I2's destination. That means we need a LOG_LINK from
2376 I3 to I2. But we used to have one, so we still will.
2378 However, some later insn might be using I2's dest and have
2379 a LOG_LINK pointing at I3. We must remove this link.
2380 The simplest way to remove the link is to point it at I1,
2381 which we know will be a NOTE. */
2383 for (insn = NEXT_INSN (i3);
2384 insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
2385 || insn != BB_HEAD (this_basic_block->next_bb));
2386 insn = NEXT_INSN (insn))
2388 if (INSN_P (insn) && reg_referenced_p (ni2dest, PATTERN (insn)))
2390 for (link = LOG_LINKS (insn); link;
2391 link = XEXP (link, 1))
2392 if (XEXP (link, 0) == i3)
2393 XEXP (link, 0) = i1;
2395 break;
2401 /* Similarly, check for a case where we have a PARALLEL of two independent
2402 SETs but we started with three insns. In this case, we can do the sets
2403 as two separate insns. This case occurs when some SET allows two
2404 other insns to combine, but the destination of that SET is still live. */
2406 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
2407 && GET_CODE (newpat) == PARALLEL
2408 && XVECLEN (newpat, 0) == 2
2409 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2410 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
2411 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
2412 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2413 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
2414 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
2415 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
2416 INSN_CUID (i2))
2417 /* Don't pass sets with (USE (MEM ...)) dests to the following. */
2418 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != USE
2419 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != USE
2420 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
2421 XVECEXP (newpat, 0, 0))
2422 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
2423 XVECEXP (newpat, 0, 1))
2424 && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
2425 && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1)))))
2427 /* Normally, it doesn't matter which of the two is done first,
2428 but it does if one references cc0. In that case, it has to
2429 be first. */
2430 #ifdef HAVE_cc0
2431 if (reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 0)))
2433 newi2pat = XVECEXP (newpat, 0, 0);
2434 newpat = XVECEXP (newpat, 0, 1);
2436 else
2437 #endif
2439 newi2pat = XVECEXP (newpat, 0, 1);
2440 newpat = XVECEXP (newpat, 0, 0);
2443 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2445 if (i2_code_number >= 0)
2446 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2449 /* If it still isn't recognized, fail and change things back the way they
2450 were. */
2451 if ((insn_code_number < 0
2452 /* Is the result a reasonable ASM_OPERANDS? */
2453 && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
2455 undo_all ();
2456 return 0;
2459 /* If we had to change another insn, make sure it is valid also. */
2460 if (undobuf.other_insn)
2462 rtx other_pat = PATTERN (undobuf.other_insn);
2463 rtx new_other_notes;
2464 rtx note, next;
2466 CLEAR_HARD_REG_SET (newpat_used_regs);
2468 other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
2469 &new_other_notes);
2471 if (other_code_number < 0 && ! check_asm_operands (other_pat))
2473 undo_all ();
2474 return 0;
2477 PATTERN (undobuf.other_insn) = other_pat;
2479 /* If any of the notes in OTHER_INSN were REG_UNUSED, ensure that they
2480 are still valid. Then add any non-duplicate notes added by
2481 recog_for_combine. */
2482 for (note = REG_NOTES (undobuf.other_insn); note; note = next)
2484 next = XEXP (note, 1);
2486 if (REG_NOTE_KIND (note) == REG_UNUSED
2487 && ! reg_set_p (XEXP (note, 0), PATTERN (undobuf.other_insn)))
2489 if (GET_CODE (XEXP (note, 0)) == REG)
2490 REG_N_DEATHS (REGNO (XEXP (note, 0)))--;
2492 remove_note (undobuf.other_insn, note);
2496 for (note = new_other_notes; note; note = XEXP (note, 1))
2497 if (GET_CODE (XEXP (note, 0)) == REG)
2498 REG_N_DEATHS (REGNO (XEXP (note, 0)))++;
2500 distribute_notes (new_other_notes, undobuf.other_insn,
2501 undobuf.other_insn, NULL_RTX);
2503 #ifdef HAVE_cc0
2504 /* If I2 is the CC0 setter and I3 is the CC0 user then check whether
2505 they are adjacent to each other or not. */
2507 rtx p = prev_nonnote_insn (i3);
2508 if (p && p != i2 && GET_CODE (p) == INSN && newi2pat
2509 && sets_cc0_p (newi2pat))
2511 undo_all ();
2512 return 0;
2515 #endif
2517 /* We now know that we can do this combination. Merge the insns and
2518 update the status of registers and LOG_LINKS. */
2521 rtx i3notes, i2notes, i1notes = 0;
2522 rtx i3links, i2links, i1links = 0;
2523 rtx midnotes = 0;
2524 unsigned int regno;
2526 /* Get the old REG_NOTES and LOG_LINKS from all our insns and
2527 clear them. */
2528 i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
2529 i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
2530 if (i1)
2531 i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
2533 /* Ensure that we do not have something that should not be shared but
2534 occurs multiple times in the new insns. Check this by first
2535 resetting all the `used' flags and then copying anything is shared. */
2537 reset_used_flags (i3notes);
2538 reset_used_flags (i2notes);
2539 reset_used_flags (i1notes);
2540 reset_used_flags (newpat);
2541 reset_used_flags (newi2pat);
2542 if (undobuf.other_insn)
2543 reset_used_flags (PATTERN (undobuf.other_insn));
2545 i3notes = copy_rtx_if_shared (i3notes);
2546 i2notes = copy_rtx_if_shared (i2notes);
2547 i1notes = copy_rtx_if_shared (i1notes);
2548 newpat = copy_rtx_if_shared (newpat);
2549 newi2pat = copy_rtx_if_shared (newi2pat);
2550 if (undobuf.other_insn)
2551 reset_used_flags (PATTERN (undobuf.other_insn));
2553 INSN_CODE (i3) = insn_code_number;
2554 PATTERN (i3) = newpat;
2556 if (GET_CODE (i3) == CALL_INSN && CALL_INSN_FUNCTION_USAGE (i3))
2558 rtx call_usage = CALL_INSN_FUNCTION_USAGE (i3);
2560 reset_used_flags (call_usage);
2561 call_usage = copy_rtx (call_usage);
2563 if (substed_i2)
2564 replace_rtx (call_usage, i2dest, i2src);
2566 if (substed_i1)
2567 replace_rtx (call_usage, i1dest, i1src);
2569 CALL_INSN_FUNCTION_USAGE (i3) = call_usage;
2572 if (undobuf.other_insn)
2573 INSN_CODE (undobuf.other_insn) = other_code_number;
2575 /* We had one special case above where I2 had more than one set and
2576 we replaced a destination of one of those sets with the destination
2577 of I3. In that case, we have to update LOG_LINKS of insns later
2578 in this basic block. Note that this (expensive) case is rare.
2580 Also, in this case, we must pretend that all REG_NOTEs for I2
2581 actually came from I3, so that REG_UNUSED notes from I2 will be
2582 properly handled. */
2584 if (i3_subst_into_i2)
2586 for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
2587 if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != USE
2588 && GET_CODE (SET_DEST (XVECEXP (PATTERN (i2), 0, i))) == REG
2589 && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
2590 && ! find_reg_note (i2, REG_UNUSED,
2591 SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
2592 for (temp = NEXT_INSN (i2);
2593 temp && (this_basic_block->next_bb == EXIT_BLOCK_PTR
2594 || BB_HEAD (this_basic_block) != temp);
2595 temp = NEXT_INSN (temp))
2596 if (temp != i3 && INSN_P (temp))
2597 for (link = LOG_LINKS (temp); link; link = XEXP (link, 1))
2598 if (XEXP (link, 0) == i2)
2599 XEXP (link, 0) = i3;
2601 if (i3notes)
2603 rtx link = i3notes;
2604 while (XEXP (link, 1))
2605 link = XEXP (link, 1);
2606 XEXP (link, 1) = i2notes;
2608 else
2609 i3notes = i2notes;
2610 i2notes = 0;
2613 LOG_LINKS (i3) = 0;
2614 REG_NOTES (i3) = 0;
2615 LOG_LINKS (i2) = 0;
2616 REG_NOTES (i2) = 0;
2618 if (newi2pat)
2620 INSN_CODE (i2) = i2_code_number;
2621 PATTERN (i2) = newi2pat;
2623 else
2625 PUT_CODE (i2, NOTE);
2626 NOTE_LINE_NUMBER (i2) = NOTE_INSN_DELETED;
2627 NOTE_SOURCE_FILE (i2) = 0;
2630 if (i1)
2632 LOG_LINKS (i1) = 0;
2633 REG_NOTES (i1) = 0;
2634 PUT_CODE (i1, NOTE);
2635 NOTE_LINE_NUMBER (i1) = NOTE_INSN_DELETED;
2636 NOTE_SOURCE_FILE (i1) = 0;
2639 /* Get death notes for everything that is now used in either I3 or
2640 I2 and used to die in a previous insn. If we built two new
2641 patterns, move from I1 to I2 then I2 to I3 so that we get the
2642 proper movement on registers that I2 modifies. */
2644 if (newi2pat)
2646 move_deaths (newi2pat, NULL_RTX, INSN_CUID (i1), i2, &midnotes);
2647 move_deaths (newpat, newi2pat, INSN_CUID (i1), i3, &midnotes);
2649 else
2650 move_deaths (newpat, NULL_RTX, i1 ? INSN_CUID (i1) : INSN_CUID (i2),
2651 i3, &midnotes);
2653 /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3. */
2654 if (i3notes)
2655 distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL_RTX);
2656 if (i2notes)
2657 distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL_RTX);
2658 if (i1notes)
2659 distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL_RTX);
2660 if (midnotes)
2661 distribute_notes (midnotes, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX);
2663 /* Distribute any notes added to I2 or I3 by recog_for_combine. We
2664 know these are REG_UNUSED and want them to go to the desired insn,
2665 so we always pass it as i3. We have not counted the notes in
2666 reg_n_deaths yet, so we need to do so now. */
2668 if (newi2pat && new_i2_notes)
2670 for (temp = new_i2_notes; temp; temp = XEXP (temp, 1))
2671 if (GET_CODE (XEXP (temp, 0)) == REG)
2672 REG_N_DEATHS (REGNO (XEXP (temp, 0)))++;
2674 distribute_notes (new_i2_notes, i2, i2, NULL_RTX);
2677 if (new_i3_notes)
2679 for (temp = new_i3_notes; temp; temp = XEXP (temp, 1))
2680 if (GET_CODE (XEXP (temp, 0)) == REG)
2681 REG_N_DEATHS (REGNO (XEXP (temp, 0)))++;
2683 distribute_notes (new_i3_notes, i3, i3, NULL_RTX);
2686 /* If I3DEST was used in I3SRC, it really died in I3. We may need to
2687 put a REG_DEAD note for it somewhere. If NEWI2PAT exists and sets
2688 I3DEST, the death must be somewhere before I2, not I3. If we passed I3
2689 in that case, it might delete I2. Similarly for I2 and I1.
2690 Show an additional death due to the REG_DEAD note we make here. If
2691 we discard it in distribute_notes, we will decrement it again. */
2693 if (i3dest_killed)
2695 if (GET_CODE (i3dest_killed) == REG)
2696 REG_N_DEATHS (REGNO (i3dest_killed))++;
2698 if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
2699 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i3dest_killed,
2700 NULL_RTX),
2701 NULL_RTX, i2, NULL_RTX);
2702 else
2703 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i3dest_killed,
2704 NULL_RTX),
2705 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX);
2708 if (i2dest_in_i2src)
2710 if (GET_CODE (i2dest) == REG)
2711 REG_N_DEATHS (REGNO (i2dest))++;
2713 if (newi2pat && reg_set_p (i2dest, newi2pat))
2714 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i2dest, NULL_RTX),
2715 NULL_RTX, i2, NULL_RTX);
2716 else
2717 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i2dest, NULL_RTX),
2718 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX);
2721 if (i1dest_in_i1src)
2723 if (GET_CODE (i1dest) == REG)
2724 REG_N_DEATHS (REGNO (i1dest))++;
2726 if (newi2pat && reg_set_p (i1dest, newi2pat))
2727 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i1dest, NULL_RTX),
2728 NULL_RTX, i2, NULL_RTX);
2729 else
2730 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i1dest, NULL_RTX),
2731 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX);
2734 distribute_links (i3links);
2735 distribute_links (i2links);
2736 distribute_links (i1links);
2738 if (GET_CODE (i2dest) == REG)
2740 rtx link;
2741 rtx i2_insn = 0, i2_val = 0, set;
2743 /* The insn that used to set this register doesn't exist, and
2744 this life of the register may not exist either. See if one of
2745 I3's links points to an insn that sets I2DEST. If it does,
2746 that is now the last known value for I2DEST. If we don't update
2747 this and I2 set the register to a value that depended on its old
2748 contents, we will get confused. If this insn is used, thing
2749 will be set correctly in combine_instructions. */
2751 for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
2752 if ((set = single_set (XEXP (link, 0))) != 0
2753 && rtx_equal_p (i2dest, SET_DEST (set)))
2754 i2_insn = XEXP (link, 0), i2_val = SET_SRC (set);
2756 record_value_for_reg (i2dest, i2_insn, i2_val);
2758 /* If the reg formerly set in I2 died only once and that was in I3,
2759 zero its use count so it won't make `reload' do any work. */
2760 if (! added_sets_2
2761 && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
2762 && ! i2dest_in_i2src)
2764 regno = REGNO (i2dest);
2765 REG_N_SETS (regno)--;
2769 if (i1 && GET_CODE (i1dest) == REG)
2771 rtx link;
2772 rtx i1_insn = 0, i1_val = 0, set;
2774 for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
2775 if ((set = single_set (XEXP (link, 0))) != 0
2776 && rtx_equal_p (i1dest, SET_DEST (set)))
2777 i1_insn = XEXP (link, 0), i1_val = SET_SRC (set);
2779 record_value_for_reg (i1dest, i1_insn, i1_val);
2781 regno = REGNO (i1dest);
2782 if (! added_sets_1 && ! i1dest_in_i1src)
2783 REG_N_SETS (regno)--;
2786 /* Update reg_nonzero_bits et al for any changes that may have been made
2787 to this insn. The order of set_nonzero_bits_and_sign_copies() is
2788 important. Because newi2pat can affect nonzero_bits of newpat */
2789 if (newi2pat)
2790 note_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
2791 note_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
2793 /* Set new_direct_jump_p if a new return or simple jump instruction
2794 has been created.
2796 If I3 is now an unconditional jump, ensure that it has a
2797 BARRIER following it since it may have initially been a
2798 conditional jump. It may also be the last nonnote insn. */
2800 if (returnjump_p (i3) || any_uncondjump_p (i3))
2802 *new_direct_jump_p = 1;
2803 mark_jump_label (PATTERN (i3), i3, 0);
2805 if ((temp = next_nonnote_insn (i3)) == NULL_RTX
2806 || GET_CODE (temp) != BARRIER)
2807 emit_barrier_after (i3);
2810 if (undobuf.other_insn != NULL_RTX
2811 && (returnjump_p (undobuf.other_insn)
2812 || any_uncondjump_p (undobuf.other_insn)))
2814 *new_direct_jump_p = 1;
2816 if ((temp = next_nonnote_insn (undobuf.other_insn)) == NULL_RTX
2817 || GET_CODE (temp) != BARRIER)
2818 emit_barrier_after (undobuf.other_insn);
2821 /* An NOOP jump does not need barrier, but it does need cleaning up
2822 of CFG. */
2823 if (GET_CODE (newpat) == SET
2824 && SET_SRC (newpat) == pc_rtx
2825 && SET_DEST (newpat) == pc_rtx)
2826 *new_direct_jump_p = 1;
2829 combine_successes++;
2830 undo_commit ();
2832 if (added_links_insn
2833 && (newi2pat == 0 || INSN_CUID (added_links_insn) < INSN_CUID (i2))
2834 && INSN_CUID (added_links_insn) < INSN_CUID (i3))
2835 return added_links_insn;
2836 else
2837 return newi2pat ? i2 : i3;
2840 /* Undo all the modifications recorded in undobuf. */
2842 static void
2843 undo_all (void)
2845 struct undo *undo, *next;
2847 for (undo = undobuf.undos; undo; undo = next)
2849 next = undo->next;
2850 if (undo->is_int)
2851 *undo->where.i = undo->old_contents.i;
2852 else
2853 *undo->where.r = undo->old_contents.r;
2855 undo->next = undobuf.frees;
2856 undobuf.frees = undo;
2859 undobuf.undos = 0;
2862 /* We've committed to accepting the changes we made. Move all
2863 of the undos to the free list. */
2865 static void
2866 undo_commit (void)
2868 struct undo *undo, *next;
2870 for (undo = undobuf.undos; undo; undo = next)
2872 next = undo->next;
2873 undo->next = undobuf.frees;
2874 undobuf.frees = undo;
2876 undobuf.undos = 0;
2880 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
2881 where we have an arithmetic expression and return that point. LOC will
2882 be inside INSN.
2884 try_combine will call this function to see if an insn can be split into
2885 two insns. */
2887 static rtx *
2888 find_split_point (rtx *loc, rtx insn)
2890 rtx x = *loc;
2891 enum rtx_code code = GET_CODE (x);
2892 rtx *split;
2893 unsigned HOST_WIDE_INT len = 0;
2894 HOST_WIDE_INT pos = 0;
2895 int unsignedp = 0;
2896 rtx inner = NULL_RTX;
2898 /* First special-case some codes. */
2899 switch (code)
2901 case SUBREG:
2902 #ifdef INSN_SCHEDULING
2903 /* If we are making a paradoxical SUBREG invalid, it becomes a split
2904 point. */
2905 if (GET_CODE (SUBREG_REG (x)) == MEM)
2906 return loc;
2907 #endif
2908 return find_split_point (&SUBREG_REG (x), insn);
2910 case MEM:
2911 #ifdef HAVE_lo_sum
2912 /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
2913 using LO_SUM and HIGH. */
2914 if (GET_CODE (XEXP (x, 0)) == CONST
2915 || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
2917 SUBST (XEXP (x, 0),
2918 gen_rtx_LO_SUM (Pmode,
2919 gen_rtx_HIGH (Pmode, XEXP (x, 0)),
2920 XEXP (x, 0)));
2921 return &XEXP (XEXP (x, 0), 0);
2923 #endif
2925 /* If we have a PLUS whose second operand is a constant and the
2926 address is not valid, perhaps will can split it up using
2927 the machine-specific way to split large constants. We use
2928 the first pseudo-reg (one of the virtual regs) as a placeholder;
2929 it will not remain in the result. */
2930 if (GET_CODE (XEXP (x, 0)) == PLUS
2931 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
2932 && ! memory_address_p (GET_MODE (x), XEXP (x, 0)))
2934 rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
2935 rtx seq = split_insns (gen_rtx_SET (VOIDmode, reg, XEXP (x, 0)),
2936 subst_insn);
2938 /* This should have produced two insns, each of which sets our
2939 placeholder. If the source of the second is a valid address,
2940 we can make put both sources together and make a split point
2941 in the middle. */
2943 if (seq
2944 && NEXT_INSN (seq) != NULL_RTX
2945 && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
2946 && GET_CODE (seq) == INSN
2947 && GET_CODE (PATTERN (seq)) == SET
2948 && SET_DEST (PATTERN (seq)) == reg
2949 && ! reg_mentioned_p (reg,
2950 SET_SRC (PATTERN (seq)))
2951 && GET_CODE (NEXT_INSN (seq)) == INSN
2952 && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
2953 && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
2954 && memory_address_p (GET_MODE (x),
2955 SET_SRC (PATTERN (NEXT_INSN (seq)))))
2957 rtx src1 = SET_SRC (PATTERN (seq));
2958 rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
2960 /* Replace the placeholder in SRC2 with SRC1. If we can
2961 find where in SRC2 it was placed, that can become our
2962 split point and we can replace this address with SRC2.
2963 Just try two obvious places. */
2965 src2 = replace_rtx (src2, reg, src1);
2966 split = 0;
2967 if (XEXP (src2, 0) == src1)
2968 split = &XEXP (src2, 0);
2969 else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
2970 && XEXP (XEXP (src2, 0), 0) == src1)
2971 split = &XEXP (XEXP (src2, 0), 0);
2973 if (split)
2975 SUBST (XEXP (x, 0), src2);
2976 return split;
2980 /* If that didn't work, perhaps the first operand is complex and
2981 needs to be computed separately, so make a split point there.
2982 This will occur on machines that just support REG + CONST
2983 and have a constant moved through some previous computation. */
2985 else if (!OBJECT_P (XEXP (XEXP (x, 0), 0))
2986 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
2987 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
2988 return &XEXP (XEXP (x, 0), 0);
2990 break;
2992 case SET:
2993 #ifdef HAVE_cc0
2994 /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
2995 ZERO_EXTRACT, the most likely reason why this doesn't match is that
2996 we need to put the operand into a register. So split at that
2997 point. */
2999 if (SET_DEST (x) == cc0_rtx
3000 && GET_CODE (SET_SRC (x)) != COMPARE
3001 && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
3002 && !OBJECT_P (SET_SRC (x))
3003 && ! (GET_CODE (SET_SRC (x)) == SUBREG
3004 && OBJECT_P (SUBREG_REG (SET_SRC (x)))))
3005 return &SET_SRC (x);
3006 #endif
3008 /* See if we can split SET_SRC as it stands. */
3009 split = find_split_point (&SET_SRC (x), insn);
3010 if (split && split != &SET_SRC (x))
3011 return split;
3013 /* See if we can split SET_DEST as it stands. */
3014 split = find_split_point (&SET_DEST (x), insn);
3015 if (split && split != &SET_DEST (x))
3016 return split;
3018 /* See if this is a bitfield assignment with everything constant. If
3019 so, this is an IOR of an AND, so split it into that. */
3020 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
3021 && (GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)))
3022 <= HOST_BITS_PER_WIDE_INT)
3023 && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT
3024 && GET_CODE (XEXP (SET_DEST (x), 2)) == CONST_INT
3025 && GET_CODE (SET_SRC (x)) == CONST_INT
3026 && ((INTVAL (XEXP (SET_DEST (x), 1))
3027 + INTVAL (XEXP (SET_DEST (x), 2)))
3028 <= GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0))))
3029 && ! side_effects_p (XEXP (SET_DEST (x), 0)))
3031 HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
3032 unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
3033 unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x));
3034 rtx dest = XEXP (SET_DEST (x), 0);
3035 enum machine_mode mode = GET_MODE (dest);
3036 unsigned HOST_WIDE_INT mask = ((HOST_WIDE_INT) 1 << len) - 1;
3038 if (BITS_BIG_ENDIAN)
3039 pos = GET_MODE_BITSIZE (mode) - len - pos;
3041 if (src == mask)
3042 SUBST (SET_SRC (x),
3043 gen_binary (IOR, mode, dest, GEN_INT (src << pos)));
3044 else
3045 SUBST (SET_SRC (x),
3046 gen_binary (IOR, mode,
3047 gen_binary (AND, mode, dest,
3048 gen_int_mode (~(mask << pos),
3049 mode)),
3050 GEN_INT (src << pos)));
3052 SUBST (SET_DEST (x), dest);
3054 split = find_split_point (&SET_SRC (x), insn);
3055 if (split && split != &SET_SRC (x))
3056 return split;
3059 /* Otherwise, see if this is an operation that we can split into two.
3060 If so, try to split that. */
3061 code = GET_CODE (SET_SRC (x));
3063 switch (code)
3065 case AND:
3066 /* If we are AND'ing with a large constant that is only a single
3067 bit and the result is only being used in a context where we
3068 need to know if it is zero or nonzero, replace it with a bit
3069 extraction. This will avoid the large constant, which might
3070 have taken more than one insn to make. If the constant were
3071 not a valid argument to the AND but took only one insn to make,
3072 this is no worse, but if it took more than one insn, it will
3073 be better. */
3075 if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
3076 && GET_CODE (XEXP (SET_SRC (x), 0)) == REG
3077 && (pos = exact_log2 (INTVAL (XEXP (SET_SRC (x), 1)))) >= 7
3078 && GET_CODE (SET_DEST (x)) == REG
3079 && (split = find_single_use (SET_DEST (x), insn, (rtx*) 0)) != 0
3080 && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
3081 && XEXP (*split, 0) == SET_DEST (x)
3082 && XEXP (*split, 1) == const0_rtx)
3084 rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
3085 XEXP (SET_SRC (x), 0),
3086 pos, NULL_RTX, 1, 1, 0, 0);
3087 if (extraction != 0)
3089 SUBST (SET_SRC (x), extraction);
3090 return find_split_point (loc, insn);
3093 break;
3095 case NE:
3096 /* If STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
3097 is known to be on, this can be converted into a NEG of a shift. */
3098 if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
3099 && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
3100 && 1 <= (pos = exact_log2
3101 (nonzero_bits (XEXP (SET_SRC (x), 0),
3102 GET_MODE (XEXP (SET_SRC (x), 0))))))
3104 enum machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
3106 SUBST (SET_SRC (x),
3107 gen_rtx_NEG (mode,
3108 gen_rtx_LSHIFTRT (mode,
3109 XEXP (SET_SRC (x), 0),
3110 GEN_INT (pos))));
3112 split = find_split_point (&SET_SRC (x), insn);
3113 if (split && split != &SET_SRC (x))
3114 return split;
3116 break;
3118 case SIGN_EXTEND:
3119 inner = XEXP (SET_SRC (x), 0);
3121 /* We can't optimize if either mode is a partial integer
3122 mode as we don't know how many bits are significant
3123 in those modes. */
3124 if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
3125 || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
3126 break;
3128 pos = 0;
3129 len = GET_MODE_BITSIZE (GET_MODE (inner));
3130 unsignedp = 0;
3131 break;
3133 case SIGN_EXTRACT:
3134 case ZERO_EXTRACT:
3135 if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
3136 && GET_CODE (XEXP (SET_SRC (x), 2)) == CONST_INT)
3138 inner = XEXP (SET_SRC (x), 0);
3139 len = INTVAL (XEXP (SET_SRC (x), 1));
3140 pos = INTVAL (XEXP (SET_SRC (x), 2));
3142 if (BITS_BIG_ENDIAN)
3143 pos = GET_MODE_BITSIZE (GET_MODE (inner)) - len - pos;
3144 unsignedp = (code == ZERO_EXTRACT);
3146 break;
3148 default:
3149 break;
3152 if (len && pos >= 0 && pos + len <= GET_MODE_BITSIZE (GET_MODE (inner)))
3154 enum machine_mode mode = GET_MODE (SET_SRC (x));
3156 /* For unsigned, we have a choice of a shift followed by an
3157 AND or two shifts. Use two shifts for field sizes where the
3158 constant might be too large. We assume here that we can
3159 always at least get 8-bit constants in an AND insn, which is
3160 true for every current RISC. */
3162 if (unsignedp && len <= 8)
3164 SUBST (SET_SRC (x),
3165 gen_rtx_AND (mode,
3166 gen_rtx_LSHIFTRT
3167 (mode, gen_lowpart (mode, inner),
3168 GEN_INT (pos)),
3169 GEN_INT (((HOST_WIDE_INT) 1 << len) - 1)));
3171 split = find_split_point (&SET_SRC (x), insn);
3172 if (split && split != &SET_SRC (x))
3173 return split;
3175 else
3177 SUBST (SET_SRC (x),
3178 gen_rtx_fmt_ee
3179 (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
3180 gen_rtx_ASHIFT (mode,
3181 gen_lowpart (mode, inner),
3182 GEN_INT (GET_MODE_BITSIZE (mode)
3183 - len - pos)),
3184 GEN_INT (GET_MODE_BITSIZE (mode) - len)));
3186 split = find_split_point (&SET_SRC (x), insn);
3187 if (split && split != &SET_SRC (x))
3188 return split;
3192 /* See if this is a simple operation with a constant as the second
3193 operand. It might be that this constant is out of range and hence
3194 could be used as a split point. */
3195 if (BINARY_P (SET_SRC (x))
3196 && CONSTANT_P (XEXP (SET_SRC (x), 1))
3197 && (OBJECT_P (XEXP (SET_SRC (x), 0))
3198 || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
3199 && OBJECT_P (SUBREG_REG (XEXP (SET_SRC (x), 0))))))
3200 return &XEXP (SET_SRC (x), 1);
3202 /* Finally, see if this is a simple operation with its first operand
3203 not in a register. The operation might require this operand in a
3204 register, so return it as a split point. We can always do this
3205 because if the first operand were another operation, we would have
3206 already found it as a split point. */
3207 if ((BINARY_P (SET_SRC (x)) || UNARY_P (SET_SRC (x)))
3208 && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
3209 return &XEXP (SET_SRC (x), 0);
3211 return 0;
3213 case AND:
3214 case IOR:
3215 /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
3216 it is better to write this as (not (ior A B)) so we can split it.
3217 Similarly for IOR. */
3218 if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
3220 SUBST (*loc,
3221 gen_rtx_NOT (GET_MODE (x),
3222 gen_rtx_fmt_ee (code == IOR ? AND : IOR,
3223 GET_MODE (x),
3224 XEXP (XEXP (x, 0), 0),
3225 XEXP (XEXP (x, 1), 0))));
3226 return find_split_point (loc, insn);
3229 /* Many RISC machines have a large set of logical insns. If the
3230 second operand is a NOT, put it first so we will try to split the
3231 other operand first. */
3232 if (GET_CODE (XEXP (x, 1)) == NOT)
3234 rtx tem = XEXP (x, 0);
3235 SUBST (XEXP (x, 0), XEXP (x, 1));
3236 SUBST (XEXP (x, 1), tem);
3238 break;
3240 default:
3241 break;
3244 /* Otherwise, select our actions depending on our rtx class. */
3245 switch (GET_RTX_CLASS (code))
3247 case RTX_BITFIELD_OPS: /* This is ZERO_EXTRACT and SIGN_EXTRACT. */
3248 case RTX_TERNARY:
3249 split = find_split_point (&XEXP (x, 2), insn);
3250 if (split)
3251 return split;
3252 /* ... fall through ... */
3253 case RTX_BIN_ARITH:
3254 case RTX_COMM_ARITH:
3255 case RTX_COMPARE:
3256 case RTX_COMM_COMPARE:
3257 split = find_split_point (&XEXP (x, 1), insn);
3258 if (split)
3259 return split;
3260 /* ... fall through ... */
3261 case RTX_UNARY:
3262 /* Some machines have (and (shift ...) ...) insns. If X is not
3263 an AND, but XEXP (X, 0) is, use it as our split point. */
3264 if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
3265 return &XEXP (x, 0);
3267 split = find_split_point (&XEXP (x, 0), insn);
3268 if (split)
3269 return split;
3270 return loc;
3272 default:
3273 /* Otherwise, we don't have a split point. */
3274 return 0;
3278 /* Throughout X, replace FROM with TO, and return the result.
3279 The result is TO if X is FROM;
3280 otherwise the result is X, but its contents may have been modified.
3281 If they were modified, a record was made in undobuf so that
3282 undo_all will (among other things) return X to its original state.
3284 If the number of changes necessary is too much to record to undo,
3285 the excess changes are not made, so the result is invalid.
3286 The changes already made can still be undone.
3287 undobuf.num_undo is incremented for such changes, so by testing that
3288 the caller can tell whether the result is valid.
3290 `n_occurrences' is incremented each time FROM is replaced.
3292 IN_DEST is nonzero if we are processing the SET_DEST of a SET.
3294 UNIQUE_COPY is nonzero if each substitution must be unique. We do this
3295 by copying if `n_occurrences' is nonzero. */
3297 static rtx
3298 subst (rtx x, rtx from, rtx to, int in_dest, int unique_copy)
3300 enum rtx_code code = GET_CODE (x);
3301 enum machine_mode op0_mode = VOIDmode;
3302 const char *fmt;
3303 int len, i;
3304 rtx new;
3306 /* Two expressions are equal if they are identical copies of a shared
3307 RTX or if they are both registers with the same register number
3308 and mode. */
3310 #define COMBINE_RTX_EQUAL_P(X,Y) \
3311 ((X) == (Y) \
3312 || (GET_CODE (X) == REG && GET_CODE (Y) == REG \
3313 && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
3315 if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
3317 n_occurrences++;
3318 return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
3321 /* If X and FROM are the same register but different modes, they will
3322 not have been seen as equal above. However, flow.c will make a
3323 LOG_LINKS entry for that case. If we do nothing, we will try to
3324 rerecognize our original insn and, when it succeeds, we will
3325 delete the feeding insn, which is incorrect.
3327 So force this insn not to match in this (rare) case. */
3328 if (! in_dest && code == REG && GET_CODE (from) == REG
3329 && REGNO (x) == REGNO (from))
3330 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
3332 /* If this is an object, we are done unless it is a MEM or LO_SUM, both
3333 of which may contain things that can be combined. */
3334 if (code != MEM && code != LO_SUM && OBJECT_P (x))
3335 return x;
3337 /* It is possible to have a subexpression appear twice in the insn.
3338 Suppose that FROM is a register that appears within TO.
3339 Then, after that subexpression has been scanned once by `subst',
3340 the second time it is scanned, TO may be found. If we were
3341 to scan TO here, we would find FROM within it and create a
3342 self-referent rtl structure which is completely wrong. */
3343 if (COMBINE_RTX_EQUAL_P (x, to))
3344 return to;
3346 /* Parallel asm_operands need special attention because all of the
3347 inputs are shared across the arms. Furthermore, unsharing the
3348 rtl results in recognition failures. Failure to handle this case
3349 specially can result in circular rtl.
3351 Solve this by doing a normal pass across the first entry of the
3352 parallel, and only processing the SET_DESTs of the subsequent
3353 entries. Ug. */
3355 if (code == PARALLEL
3356 && GET_CODE (XVECEXP (x, 0, 0)) == SET
3357 && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
3359 new = subst (XVECEXP (x, 0, 0), from, to, 0, unique_copy);
3361 /* If this substitution failed, this whole thing fails. */
3362 if (GET_CODE (new) == CLOBBER
3363 && XEXP (new, 0) == const0_rtx)
3364 return new;
3366 SUBST (XVECEXP (x, 0, 0), new);
3368 for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
3370 rtx dest = SET_DEST (XVECEXP (x, 0, i));
3372 if (GET_CODE (dest) != REG
3373 && GET_CODE (dest) != CC0
3374 && GET_CODE (dest) != PC)
3376 new = subst (dest, from, to, 0, unique_copy);
3378 /* If this substitution failed, this whole thing fails. */
3379 if (GET_CODE (new) == CLOBBER
3380 && XEXP (new, 0) == const0_rtx)
3381 return new;
3383 SUBST (SET_DEST (XVECEXP (x, 0, i)), new);
3387 else
3389 len = GET_RTX_LENGTH (code);
3390 fmt = GET_RTX_FORMAT (code);
3392 /* We don't need to process a SET_DEST that is a register, CC0,
3393 or PC, so set up to skip this common case. All other cases
3394 where we want to suppress replacing something inside a
3395 SET_SRC are handled via the IN_DEST operand. */
3396 if (code == SET
3397 && (GET_CODE (SET_DEST (x)) == REG
3398 || GET_CODE (SET_DEST (x)) == CC0
3399 || GET_CODE (SET_DEST (x)) == PC))
3400 fmt = "ie";
3402 /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
3403 constant. */
3404 if (fmt[0] == 'e')
3405 op0_mode = GET_MODE (XEXP (x, 0));
3407 for (i = 0; i < len; i++)
3409 if (fmt[i] == 'E')
3411 int j;
3412 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3414 if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
3416 new = (unique_copy && n_occurrences
3417 ? copy_rtx (to) : to);
3418 n_occurrences++;
3420 else
3422 new = subst (XVECEXP (x, i, j), from, to, 0,
3423 unique_copy);
3425 /* If this substitution failed, this whole thing
3426 fails. */
3427 if (GET_CODE (new) == CLOBBER
3428 && XEXP (new, 0) == const0_rtx)
3429 return new;
3432 SUBST (XVECEXP (x, i, j), new);
3435 else if (fmt[i] == 'e')
3437 /* If this is a register being set, ignore it. */
3438 new = XEXP (x, i);
3439 if (in_dest
3440 && (code == SUBREG || code == STRICT_LOW_PART
3441 || code == ZERO_EXTRACT)
3442 && i == 0
3443 && GET_CODE (new) == REG)
3446 else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
3448 /* In general, don't install a subreg involving two
3449 modes not tieable. It can worsen register
3450 allocation, and can even make invalid reload
3451 insns, since the reg inside may need to be copied
3452 from in the outside mode, and that may be invalid
3453 if it is an fp reg copied in integer mode.
3455 We allow two exceptions to this: It is valid if
3456 it is inside another SUBREG and the mode of that
3457 SUBREG and the mode of the inside of TO is
3458 tieable and it is valid if X is a SET that copies
3459 FROM to CC0. */
3461 if (GET_CODE (to) == SUBREG
3462 && ! MODES_TIEABLE_P (GET_MODE (to),
3463 GET_MODE (SUBREG_REG (to)))
3464 && ! (code == SUBREG
3465 && MODES_TIEABLE_P (GET_MODE (x),
3466 GET_MODE (SUBREG_REG (to))))
3467 #ifdef HAVE_cc0
3468 && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
3469 #endif
3471 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
3473 #ifdef CANNOT_CHANGE_MODE_CLASS
3474 if (code == SUBREG
3475 && GET_CODE (to) == REG
3476 && REGNO (to) < FIRST_PSEUDO_REGISTER
3477 && REG_CANNOT_CHANGE_MODE_P (REGNO (to),
3478 GET_MODE (to),
3479 GET_MODE (x)))
3480 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
3481 #endif
3483 new = (unique_copy && n_occurrences ? copy_rtx (to) : to);
3484 n_occurrences++;
3486 else
3487 /* If we are in a SET_DEST, suppress most cases unless we
3488 have gone inside a MEM, in which case we want to
3489 simplify the address. We assume here that things that
3490 are actually part of the destination have their inner
3491 parts in the first expression. This is true for SUBREG,
3492 STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
3493 things aside from REG and MEM that should appear in a
3494 SET_DEST. */
3495 new = subst (XEXP (x, i), from, to,
3496 (((in_dest
3497 && (code == SUBREG || code == STRICT_LOW_PART
3498 || code == ZERO_EXTRACT))
3499 || code == SET)
3500 && i == 0), unique_copy);
3502 /* If we found that we will have to reject this combination,
3503 indicate that by returning the CLOBBER ourselves, rather than
3504 an expression containing it. This will speed things up as
3505 well as prevent accidents where two CLOBBERs are considered
3506 to be equal, thus producing an incorrect simplification. */
3508 if (GET_CODE (new) == CLOBBER && XEXP (new, 0) == const0_rtx)
3509 return new;
3511 if (GET_CODE (x) == SUBREG
3512 && (GET_CODE (new) == CONST_INT
3513 || GET_CODE (new) == CONST_DOUBLE))
3515 enum machine_mode mode = GET_MODE (x);
3517 x = simplify_subreg (GET_MODE (x), new,
3518 GET_MODE (SUBREG_REG (x)),
3519 SUBREG_BYTE (x));
3520 if (! x)
3521 x = gen_rtx_CLOBBER (mode, const0_rtx);
3523 else if (GET_CODE (new) == CONST_INT
3524 && GET_CODE (x) == ZERO_EXTEND)
3526 x = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
3527 new, GET_MODE (XEXP (x, 0)));
3528 if (! x)
3529 abort ();
3531 else
3532 SUBST (XEXP (x, i), new);
3537 /* Try to simplify X. If the simplification changed the code, it is likely
3538 that further simplification will help, so loop, but limit the number
3539 of repetitions that will be performed. */
3541 for (i = 0; i < 4; i++)
3543 /* If X is sufficiently simple, don't bother trying to do anything
3544 with it. */
3545 if (code != CONST_INT && code != REG && code != CLOBBER)
3546 x = combine_simplify_rtx (x, op0_mode, in_dest);
3548 if (GET_CODE (x) == code)
3549 break;
3551 code = GET_CODE (x);
3553 /* We no longer know the original mode of operand 0 since we
3554 have changed the form of X) */
3555 op0_mode = VOIDmode;
3558 return x;
3561 /* Simplify X, a piece of RTL. We just operate on the expression at the
3562 outer level; call `subst' to simplify recursively. Return the new
3563 expression.
3565 OP0_MODE is the original mode of XEXP (x, 0). IN_DEST is nonzero
3566 if we are inside a SET_DEST. */
3568 static rtx
3569 combine_simplify_rtx (rtx x, enum machine_mode op0_mode, int in_dest)
3571 enum rtx_code code = GET_CODE (x);
3572 enum machine_mode mode = GET_MODE (x);
3573 rtx temp;
3574 rtx reversed;
3575 int i;
3577 /* If this is a commutative operation, put a constant last and a complex
3578 expression first. We don't need to do this for comparisons here. */
3579 if (COMMUTATIVE_ARITH_P (x)
3580 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
3582 temp = XEXP (x, 0);
3583 SUBST (XEXP (x, 0), XEXP (x, 1));
3584 SUBST (XEXP (x, 1), temp);
3587 /* If this is a PLUS, MINUS, or MULT, and the first operand is the
3588 sign extension of a PLUS with a constant, reverse the order of the sign
3589 extension and the addition. Note that this not the same as the original
3590 code, but overflow is undefined for signed values. Also note that the
3591 PLUS will have been partially moved "inside" the sign-extension, so that
3592 the first operand of X will really look like:
3593 (ashiftrt (plus (ashift A C4) C5) C4).
3594 We convert this to
3595 (plus (ashiftrt (ashift A C4) C2) C4)
3596 and replace the first operand of X with that expression. Later parts
3597 of this function may simplify the expression further.
3599 For example, if we start with (mult (sign_extend (plus A C1)) C2),
3600 we swap the SIGN_EXTEND and PLUS. Later code will apply the
3601 distributive law to produce (plus (mult (sign_extend X) C1) C3).
3603 We do this to simplify address expressions. */
3605 if ((code == PLUS || code == MINUS || code == MULT)
3606 && GET_CODE (XEXP (x, 0)) == ASHIFTRT
3607 && GET_CODE (XEXP (XEXP (x, 0), 0)) == PLUS
3608 && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == ASHIFT
3609 && GET_CODE (XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 1)) == CONST_INT
3610 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3611 && XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 1) == XEXP (XEXP (x, 0), 1)
3612 && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
3613 && (temp = simplify_binary_operation (ASHIFTRT, mode,
3614 XEXP (XEXP (XEXP (x, 0), 0), 1),
3615 XEXP (XEXP (x, 0), 1))) != 0)
3617 rtx new
3618 = simplify_shift_const (NULL_RTX, ASHIFT, mode,
3619 XEXP (XEXP (XEXP (XEXP (x, 0), 0), 0), 0),
3620 INTVAL (XEXP (XEXP (x, 0), 1)));
3622 new = simplify_shift_const (NULL_RTX, ASHIFTRT, mode, new,
3623 INTVAL (XEXP (XEXP (x, 0), 1)));
3625 SUBST (XEXP (x, 0), gen_binary (PLUS, mode, new, temp));
3628 /* If this is a simple operation applied to an IF_THEN_ELSE, try
3629 applying it to the arms of the IF_THEN_ELSE. This often simplifies
3630 things. Check for cases where both arms are testing the same
3631 condition.
3633 Don't do anything if all operands are very simple. */
3635 if ((BINARY_P (x)
3636 && ((!OBJECT_P (XEXP (x, 0))
3637 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
3638 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))
3639 || (!OBJECT_P (XEXP (x, 1))
3640 && ! (GET_CODE (XEXP (x, 1)) == SUBREG
3641 && OBJECT_P (SUBREG_REG (XEXP (x, 1)))))))
3642 || (UNARY_P (x)
3643 && (!OBJECT_P (XEXP (x, 0))
3644 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
3645 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))))
3647 rtx cond, true_rtx, false_rtx;
3649 cond = if_then_else_cond (x, &true_rtx, &false_rtx);
3650 if (cond != 0
3651 /* If everything is a comparison, what we have is highly unlikely
3652 to be simpler, so don't use it. */
3653 && ! (COMPARISON_P (x)
3654 && (COMPARISON_P (true_rtx) || COMPARISON_P (false_rtx))))
3656 rtx cop1 = const0_rtx;
3657 enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
3659 if (cond_code == NE && COMPARISON_P (cond))
3660 return x;
3662 /* Simplify the alternative arms; this may collapse the true and
3663 false arms to store-flag values. Be careful to use copy_rtx
3664 here since true_rtx or false_rtx might share RTL with x as a
3665 result of the if_then_else_cond call above. */
3666 true_rtx = subst (copy_rtx (true_rtx), pc_rtx, pc_rtx, 0, 0);
3667 false_rtx = subst (copy_rtx (false_rtx), pc_rtx, pc_rtx, 0, 0);
3669 /* If true_rtx and false_rtx are not general_operands, an if_then_else
3670 is unlikely to be simpler. */
3671 if (general_operand (true_rtx, VOIDmode)
3672 && general_operand (false_rtx, VOIDmode))
3674 enum rtx_code reversed;
3676 /* Restarting if we generate a store-flag expression will cause
3677 us to loop. Just drop through in this case. */
3679 /* If the result values are STORE_FLAG_VALUE and zero, we can
3680 just make the comparison operation. */
3681 if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
3682 x = gen_binary (cond_code, mode, cond, cop1);
3683 else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
3684 && ((reversed = reversed_comparison_code_parts
3685 (cond_code, cond, cop1, NULL))
3686 != UNKNOWN))
3687 x = gen_binary (reversed, mode, cond, cop1);
3689 /* Likewise, we can make the negate of a comparison operation
3690 if the result values are - STORE_FLAG_VALUE and zero. */
3691 else if (GET_CODE (true_rtx) == CONST_INT
3692 && INTVAL (true_rtx) == - STORE_FLAG_VALUE
3693 && false_rtx == const0_rtx)
3694 x = simplify_gen_unary (NEG, mode,
3695 gen_binary (cond_code, mode, cond,
3696 cop1),
3697 mode);
3698 else if (GET_CODE (false_rtx) == CONST_INT
3699 && INTVAL (false_rtx) == - STORE_FLAG_VALUE
3700 && true_rtx == const0_rtx
3701 && ((reversed = reversed_comparison_code_parts
3702 (cond_code, cond, cop1, NULL))
3703 != UNKNOWN))
3704 x = simplify_gen_unary (NEG, mode,
3705 gen_binary (reversed, mode,
3706 cond, cop1),
3707 mode);
3708 else
3709 return gen_rtx_IF_THEN_ELSE (mode,
3710 gen_binary (cond_code, VOIDmode,
3711 cond, cop1),
3712 true_rtx, false_rtx);
3714 code = GET_CODE (x);
3715 op0_mode = VOIDmode;
3720 /* Try to fold this expression in case we have constants that weren't
3721 present before. */
3722 temp = 0;
3723 switch (GET_RTX_CLASS (code))
3725 case RTX_UNARY:
3726 if (op0_mode == VOIDmode)
3727 op0_mode = GET_MODE (XEXP (x, 0));
3728 temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
3729 break;
3730 case RTX_COMPARE:
3731 case RTX_COMM_COMPARE:
3733 enum machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
3734 if (cmp_mode == VOIDmode)
3736 cmp_mode = GET_MODE (XEXP (x, 1));
3737 if (cmp_mode == VOIDmode)
3738 cmp_mode = op0_mode;
3740 temp = simplify_relational_operation (code, mode, cmp_mode,
3741 XEXP (x, 0), XEXP (x, 1));
3743 break;
3744 case RTX_COMM_ARITH:
3745 case RTX_BIN_ARITH:
3746 temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
3747 break;
3748 case RTX_BITFIELD_OPS:
3749 case RTX_TERNARY:
3750 temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
3751 XEXP (x, 1), XEXP (x, 2));
3752 break;
3753 default:
3754 break;
3757 if (temp)
3759 x = temp;
3760 code = GET_CODE (temp);
3761 op0_mode = VOIDmode;
3762 mode = GET_MODE (temp);
3765 /* First see if we can apply the inverse distributive law. */
3766 if (code == PLUS || code == MINUS
3767 || code == AND || code == IOR || code == XOR)
3769 x = apply_distributive_law (x);
3770 code = GET_CODE (x);
3771 op0_mode = VOIDmode;
3774 /* If CODE is an associative operation not otherwise handled, see if we
3775 can associate some operands. This can win if they are constants or
3776 if they are logically related (i.e. (a & b) & a). */
3777 if ((code == PLUS || code == MINUS || code == MULT || code == DIV
3778 || code == AND || code == IOR || code == XOR
3779 || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
3780 && ((INTEGRAL_MODE_P (mode) && code != DIV)
3781 || (flag_unsafe_math_optimizations && FLOAT_MODE_P (mode))))
3783 if (GET_CODE (XEXP (x, 0)) == code)
3785 rtx other = XEXP (XEXP (x, 0), 0);
3786 rtx inner_op0 = XEXP (XEXP (x, 0), 1);
3787 rtx inner_op1 = XEXP (x, 1);
3788 rtx inner;
3790 /* Make sure we pass the constant operand if any as the second
3791 one if this is a commutative operation. */
3792 if (CONSTANT_P (inner_op0) && COMMUTATIVE_ARITH_P (x))
3794 rtx tem = inner_op0;
3795 inner_op0 = inner_op1;
3796 inner_op1 = tem;
3798 inner = simplify_binary_operation (code == MINUS ? PLUS
3799 : code == DIV ? MULT
3800 : code,
3801 mode, inner_op0, inner_op1);
3803 /* For commutative operations, try the other pair if that one
3804 didn't simplify. */
3805 if (inner == 0 && COMMUTATIVE_ARITH_P (x))
3807 other = XEXP (XEXP (x, 0), 1);
3808 inner = simplify_binary_operation (code, mode,
3809 XEXP (XEXP (x, 0), 0),
3810 XEXP (x, 1));
3813 if (inner)
3814 return gen_binary (code, mode, other, inner);
3818 /* A little bit of algebraic simplification here. */
3819 switch (code)
3821 case MEM:
3822 /* Ensure that our address has any ASHIFTs converted to MULT in case
3823 address-recognizing predicates are called later. */
3824 temp = make_compound_operation (XEXP (x, 0), MEM);
3825 SUBST (XEXP (x, 0), temp);
3826 break;
3828 case SUBREG:
3829 if (op0_mode == VOIDmode)
3830 op0_mode = GET_MODE (SUBREG_REG (x));
3832 /* See if this can be moved to simplify_subreg. */
3833 if (CONSTANT_P (SUBREG_REG (x))
3834 && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
3835 /* Don't call gen_lowpart if the inner mode
3836 is VOIDmode and we cannot simplify it, as SUBREG without
3837 inner mode is invalid. */
3838 && (GET_MODE (SUBREG_REG (x)) != VOIDmode
3839 || gen_lowpart_common (mode, SUBREG_REG (x))))
3840 return gen_lowpart (mode, SUBREG_REG (x));
3842 if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
3843 break;
3845 rtx temp;
3846 temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
3847 SUBREG_BYTE (x));
3848 if (temp)
3849 return temp;
3852 /* Don't change the mode of the MEM if that would change the meaning
3853 of the address. */
3854 if (GET_CODE (SUBREG_REG (x)) == MEM
3855 && (MEM_VOLATILE_P (SUBREG_REG (x))
3856 || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0))))
3857 return gen_rtx_CLOBBER (mode, const0_rtx);
3859 /* Note that we cannot do any narrowing for non-constants since
3860 we might have been counting on using the fact that some bits were
3861 zero. We now do this in the SET. */
3863 break;
3865 case NOT:
3866 if (GET_CODE (XEXP (x, 0)) == SUBREG
3867 && subreg_lowpart_p (XEXP (x, 0))
3868 && (GET_MODE_SIZE (GET_MODE (XEXP (x, 0)))
3869 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (x, 0)))))
3870 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == ASHIFT
3871 && XEXP (SUBREG_REG (XEXP (x, 0)), 0) == const1_rtx)
3873 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (XEXP (x, 0)));
3875 x = gen_rtx_ROTATE (inner_mode,
3876 simplify_gen_unary (NOT, inner_mode, const1_rtx,
3877 inner_mode),
3878 XEXP (SUBREG_REG (XEXP (x, 0)), 1));
3879 return gen_lowpart (mode, x);
3882 /* Apply De Morgan's laws to reduce number of patterns for machines
3883 with negating logical insns (and-not, nand, etc.). If result has
3884 only one NOT, put it first, since that is how the patterns are
3885 coded. */
3887 if (GET_CODE (XEXP (x, 0)) == IOR || GET_CODE (XEXP (x, 0)) == AND)
3889 rtx in1 = XEXP (XEXP (x, 0), 0), in2 = XEXP (XEXP (x, 0), 1);
3890 enum machine_mode op_mode;
3892 op_mode = GET_MODE (in1);
3893 in1 = simplify_gen_unary (NOT, op_mode, in1, op_mode);
3895 op_mode = GET_MODE (in2);
3896 if (op_mode == VOIDmode)
3897 op_mode = mode;
3898 in2 = simplify_gen_unary (NOT, op_mode, in2, op_mode);
3900 if (GET_CODE (in2) == NOT && GET_CODE (in1) != NOT)
3902 rtx tem = in2;
3903 in2 = in1; in1 = tem;
3906 return gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)) == IOR ? AND : IOR,
3907 mode, in1, in2);
3909 break;
3911 case NEG:
3912 /* (neg (xor A 1)) is (plus A -1) if A is known to be either 0 or 1. */
3913 if (GET_CODE (XEXP (x, 0)) == XOR
3914 && XEXP (XEXP (x, 0), 1) == const1_rtx
3915 && nonzero_bits (XEXP (XEXP (x, 0), 0), mode) == 1)
3916 return gen_binary (PLUS, mode, XEXP (XEXP (x, 0), 0), constm1_rtx);
3918 temp = expand_compound_operation (XEXP (x, 0));
3920 /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
3921 replaced by (lshiftrt X C). This will convert
3922 (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y). */
3924 if (GET_CODE (temp) == ASHIFTRT
3925 && GET_CODE (XEXP (temp, 1)) == CONST_INT
3926 && INTVAL (XEXP (temp, 1)) == GET_MODE_BITSIZE (mode) - 1)
3927 return simplify_shift_const (temp, LSHIFTRT, mode, XEXP (temp, 0),
3928 INTVAL (XEXP (temp, 1)));
3930 /* If X has only a single bit that might be nonzero, say, bit I, convert
3931 (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
3932 MODE minus 1. This will convert (neg (zero_extract X 1 Y)) to
3933 (sign_extract X 1 Y). But only do this if TEMP isn't a register
3934 or a SUBREG of one since we'd be making the expression more
3935 complex if it was just a register. */
3937 if (GET_CODE (temp) != REG
3938 && ! (GET_CODE (temp) == SUBREG
3939 && GET_CODE (SUBREG_REG (temp)) == REG)
3940 && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
3942 rtx temp1 = simplify_shift_const
3943 (NULL_RTX, ASHIFTRT, mode,
3944 simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
3945 GET_MODE_BITSIZE (mode) - 1 - i),
3946 GET_MODE_BITSIZE (mode) - 1 - i);
3948 /* If all we did was surround TEMP with the two shifts, we
3949 haven't improved anything, so don't use it. Otherwise,
3950 we are better off with TEMP1. */
3951 if (GET_CODE (temp1) != ASHIFTRT
3952 || GET_CODE (XEXP (temp1, 0)) != ASHIFT
3953 || XEXP (XEXP (temp1, 0), 0) != temp)
3954 return temp1;
3956 break;
3958 case TRUNCATE:
3959 /* We can't handle truncation to a partial integer mode here
3960 because we don't know the real bitsize of the partial
3961 integer mode. */
3962 if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
3963 break;
3965 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
3966 && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
3967 GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))))
3968 SUBST (XEXP (x, 0),
3969 force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
3970 GET_MODE_MASK (mode), NULL_RTX, 0));
3972 /* (truncate:SI ({sign,zero}_extend:DI foo:SI)) == foo:SI. */
3973 if ((GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
3974 || GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
3975 && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode)
3976 return XEXP (XEXP (x, 0), 0);
3978 /* (truncate:SI (OP:DI ({sign,zero}_extend:DI foo:SI))) is
3979 (OP:SI foo:SI) if OP is NEG or ABS. */
3980 if ((GET_CODE (XEXP (x, 0)) == ABS
3981 || GET_CODE (XEXP (x, 0)) == NEG)
3982 && (GET_CODE (XEXP (XEXP (x, 0), 0)) == SIGN_EXTEND
3983 || GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND)
3984 && GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == mode)
3985 return simplify_gen_unary (GET_CODE (XEXP (x, 0)), mode,
3986 XEXP (XEXP (XEXP (x, 0), 0), 0), mode);
3988 /* (truncate:SI (subreg:DI (truncate:SI X) 0)) is
3989 (truncate:SI x). */
3990 if (GET_CODE (XEXP (x, 0)) == SUBREG
3991 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == TRUNCATE
3992 && subreg_lowpart_p (XEXP (x, 0)))
3993 return SUBREG_REG (XEXP (x, 0));
3995 /* If we know that the value is already truncated, we can
3996 replace the TRUNCATE with a SUBREG if TRULY_NOOP_TRUNCATION
3997 is nonzero for the corresponding modes. But don't do this
3998 for an (LSHIFTRT (MULT ...)) since this will cause problems
3999 with the umulXi3_highpart patterns. */
4000 if (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
4001 GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
4002 && num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
4003 >= (unsigned int) (GET_MODE_BITSIZE (mode) + 1)
4004 && ! (GET_CODE (XEXP (x, 0)) == LSHIFTRT
4005 && GET_CODE (XEXP (XEXP (x, 0), 0)) == MULT))
4006 return gen_lowpart (mode, XEXP (x, 0));
4008 /* A truncate of a comparison can be replaced with a subreg if
4009 STORE_FLAG_VALUE permits. This is like the previous test,
4010 but it works even if the comparison is done in a mode larger
4011 than HOST_BITS_PER_WIDE_INT. */
4012 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4013 && COMPARISON_P (XEXP (x, 0))
4014 && ((HOST_WIDE_INT) STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0)
4015 return gen_lowpart (mode, XEXP (x, 0));
4017 /* Similarly, a truncate of a register whose value is a
4018 comparison can be replaced with a subreg if STORE_FLAG_VALUE
4019 permits. */
4020 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4021 && ((HOST_WIDE_INT) STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
4022 && (temp = get_last_value (XEXP (x, 0)))
4023 && COMPARISON_P (temp))
4024 return gen_lowpart (mode, XEXP (x, 0));
4026 break;
4028 case FLOAT_TRUNCATE:
4029 /* (float_truncate:SF (float_extend:DF foo:SF)) = foo:SF. */
4030 if (GET_CODE (XEXP (x, 0)) == FLOAT_EXTEND
4031 && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode)
4032 return XEXP (XEXP (x, 0), 0);
4034 /* (float_truncate:SF (float_truncate:DF foo:XF))
4035 = (float_truncate:SF foo:XF).
4036 This may eliminate double rounding, so it is unsafe.
4038 (float_truncate:SF (float_extend:XF foo:DF))
4039 = (float_truncate:SF foo:DF).
4041 (float_truncate:DF (float_extend:XF foo:SF))
4042 = (float_extend:SF foo:DF). */
4043 if ((GET_CODE (XEXP (x, 0)) == FLOAT_TRUNCATE
4044 && flag_unsafe_math_optimizations)
4045 || GET_CODE (XEXP (x, 0)) == FLOAT_EXTEND)
4046 return simplify_gen_unary (GET_MODE_SIZE (GET_MODE (XEXP (XEXP (x, 0),
4047 0)))
4048 > GET_MODE_SIZE (mode)
4049 ? FLOAT_TRUNCATE : FLOAT_EXTEND,
4050 mode,
4051 XEXP (XEXP (x, 0), 0), mode);
4053 /* (float_truncate (float x)) is (float x) */
4054 if (GET_CODE (XEXP (x, 0)) == FLOAT
4055 && (flag_unsafe_math_optimizations
4056 || ((unsigned)significand_size (GET_MODE (XEXP (x, 0)))
4057 >= (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (x, 0), 0)))
4058 - num_sign_bit_copies (XEXP (XEXP (x, 0), 0),
4059 GET_MODE (XEXP (XEXP (x, 0), 0)))))))
4060 return simplify_gen_unary (FLOAT, mode,
4061 XEXP (XEXP (x, 0), 0),
4062 GET_MODE (XEXP (XEXP (x, 0), 0)));
4064 /* (float_truncate:SF (OP:DF (float_extend:DF foo:sf))) is
4065 (OP:SF foo:SF) if OP is NEG or ABS. */
4066 if ((GET_CODE (XEXP (x, 0)) == ABS
4067 || GET_CODE (XEXP (x, 0)) == NEG)
4068 && GET_CODE (XEXP (XEXP (x, 0), 0)) == FLOAT_EXTEND
4069 && GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)) == mode)
4070 return simplify_gen_unary (GET_CODE (XEXP (x, 0)), mode,
4071 XEXP (XEXP (XEXP (x, 0), 0), 0), mode);
4073 /* (float_truncate:SF (subreg:DF (float_truncate:SF X) 0))
4074 is (float_truncate:SF x). */
4075 if (GET_CODE (XEXP (x, 0)) == SUBREG
4076 && subreg_lowpart_p (XEXP (x, 0))
4077 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == FLOAT_TRUNCATE)
4078 return SUBREG_REG (XEXP (x, 0));
4079 break;
4080 case FLOAT_EXTEND:
4081 /* (float_extend (float_extend x)) is (float_extend x)
4083 (float_extend (float x)) is (float x) assuming that double
4084 rounding can't happen.
4086 if (GET_CODE (XEXP (x, 0)) == FLOAT_EXTEND
4087 || (GET_CODE (XEXP (x, 0)) == FLOAT
4088 && ((unsigned)significand_size (GET_MODE (XEXP (x, 0)))
4089 >= (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (x, 0), 0)))
4090 - num_sign_bit_copies (XEXP (XEXP (x, 0), 0),
4091 GET_MODE (XEXP (XEXP (x, 0), 0)))))))
4092 return simplify_gen_unary (GET_CODE (XEXP (x, 0)), mode,
4093 XEXP (XEXP (x, 0), 0),
4094 GET_MODE (XEXP (XEXP (x, 0), 0)));
4096 break;
4097 #ifdef HAVE_cc0
4098 case COMPARE:
4099 /* Convert (compare FOO (const_int 0)) to FOO unless we aren't
4100 using cc0, in which case we want to leave it as a COMPARE
4101 so we can distinguish it from a register-register-copy. */
4102 if (XEXP (x, 1) == const0_rtx)
4103 return XEXP (x, 0);
4105 /* x - 0 is the same as x unless x's mode has signed zeros and
4106 allows rounding towards -infinity. Under those conditions,
4107 0 - 0 is -0. */
4108 if (!(HONOR_SIGNED_ZEROS (GET_MODE (XEXP (x, 0)))
4109 && HONOR_SIGN_DEPENDENT_ROUNDING (GET_MODE (XEXP (x, 0))))
4110 && XEXP (x, 1) == CONST0_RTX (GET_MODE (XEXP (x, 0))))
4111 return XEXP (x, 0);
4112 break;
4113 #endif
4115 case CONST:
4116 /* (const (const X)) can become (const X). Do it this way rather than
4117 returning the inner CONST since CONST can be shared with a
4118 REG_EQUAL note. */
4119 if (GET_CODE (XEXP (x, 0)) == CONST)
4120 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4121 break;
4123 #ifdef HAVE_lo_sum
4124 case LO_SUM:
4125 /* Convert (lo_sum (high FOO) FOO) to FOO. This is necessary so we
4126 can add in an offset. find_split_point will split this address up
4127 again if it doesn't match. */
4128 if (GET_CODE (XEXP (x, 0)) == HIGH
4129 && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
4130 return XEXP (x, 1);
4131 break;
4132 #endif
4134 case PLUS:
4135 /* Canonicalize (plus (mult (neg B) C) A) to (minus A (mult B C)).
4137 if (GET_CODE (XEXP (x, 0)) == MULT
4138 && GET_CODE (XEXP (XEXP (x, 0), 0)) == NEG)
4140 rtx in1, in2;
4142 in1 = XEXP (XEXP (XEXP (x, 0), 0), 0);
4143 in2 = XEXP (XEXP (x, 0), 1);
4144 return gen_binary (MINUS, mode, XEXP (x, 1),
4145 gen_binary (MULT, mode, in1, in2));
4148 /* If we have (plus (plus (A const) B)), associate it so that CONST is
4149 outermost. That's because that's the way indexed addresses are
4150 supposed to appear. This code used to check many more cases, but
4151 they are now checked elsewhere. */
4152 if (GET_CODE (XEXP (x, 0)) == PLUS
4153 && CONSTANT_ADDRESS_P (XEXP (XEXP (x, 0), 1)))
4154 return gen_binary (PLUS, mode,
4155 gen_binary (PLUS, mode, XEXP (XEXP (x, 0), 0),
4156 XEXP (x, 1)),
4157 XEXP (XEXP (x, 0), 1));
4159 /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
4160 when c is (const_int (pow2 + 1) / 2) is a sign extension of a
4161 bit-field and can be replaced by either a sign_extend or a
4162 sign_extract. The `and' may be a zero_extend and the two
4163 <c>, -<c> constants may be reversed. */
4164 if (GET_CODE (XEXP (x, 0)) == XOR
4165 && GET_CODE (XEXP (x, 1)) == CONST_INT
4166 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
4167 && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
4168 && ((i = exact_log2 (INTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
4169 || (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
4170 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4171 && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
4172 && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
4173 && (INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
4174 == ((HOST_WIDE_INT) 1 << (i + 1)) - 1))
4175 || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
4176 && (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
4177 == (unsigned int) i + 1))))
4178 return simplify_shift_const
4179 (NULL_RTX, ASHIFTRT, mode,
4180 simplify_shift_const (NULL_RTX, ASHIFT, mode,
4181 XEXP (XEXP (XEXP (x, 0), 0), 0),
4182 GET_MODE_BITSIZE (mode) - (i + 1)),
4183 GET_MODE_BITSIZE (mode) - (i + 1));
4185 /* (plus (comparison A B) C) can become (neg (rev-comp A B)) if
4186 C is 1 and STORE_FLAG_VALUE is -1 or if C is -1 and STORE_FLAG_VALUE
4187 is 1. This produces better code than the alternative immediately
4188 below. */
4189 if (COMPARISON_P (XEXP (x, 0))
4190 && ((STORE_FLAG_VALUE == -1 && XEXP (x, 1) == const1_rtx)
4191 || (STORE_FLAG_VALUE == 1 && XEXP (x, 1) == constm1_rtx))
4192 && (reversed = reversed_comparison (XEXP (x, 0), mode,
4193 XEXP (XEXP (x, 0), 0),
4194 XEXP (XEXP (x, 0), 1))))
4195 return
4196 simplify_gen_unary (NEG, mode, reversed, mode);
4198 /* If only the low-order bit of X is possibly nonzero, (plus x -1)
4199 can become (ashiftrt (ashift (xor x 1) C) C) where C is
4200 the bitsize of the mode - 1. This allows simplification of
4201 "a = (b & 8) == 0;" */
4202 if (XEXP (x, 1) == constm1_rtx
4203 && GET_CODE (XEXP (x, 0)) != REG
4204 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
4205 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == REG)
4206 && nonzero_bits (XEXP (x, 0), mode) == 1)
4207 return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
4208 simplify_shift_const (NULL_RTX, ASHIFT, mode,
4209 gen_rtx_XOR (mode, XEXP (x, 0), const1_rtx),
4210 GET_MODE_BITSIZE (mode) - 1),
4211 GET_MODE_BITSIZE (mode) - 1);
4213 /* If we are adding two things that have no bits in common, convert
4214 the addition into an IOR. This will often be further simplified,
4215 for example in cases like ((a & 1) + (a & 2)), which can
4216 become a & 3. */
4218 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4219 && (nonzero_bits (XEXP (x, 0), mode)
4220 & nonzero_bits (XEXP (x, 1), mode)) == 0)
4222 /* Try to simplify the expression further. */
4223 rtx tor = gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
4224 temp = combine_simplify_rtx (tor, mode, in_dest);
4226 /* If we could, great. If not, do not go ahead with the IOR
4227 replacement, since PLUS appears in many special purpose
4228 address arithmetic instructions. */
4229 if (GET_CODE (temp) != CLOBBER && temp != tor)
4230 return temp;
4232 break;
4234 case MINUS:
4235 /* If STORE_FLAG_VALUE is 1, (minus 1 (comparison foo bar)) can be done
4236 by reversing the comparison code if valid. */
4237 if (STORE_FLAG_VALUE == 1
4238 && XEXP (x, 0) == const1_rtx
4239 && COMPARISON_P (XEXP (x, 1))
4240 && (reversed = reversed_comparison (XEXP (x, 1), mode,
4241 XEXP (XEXP (x, 1), 0),
4242 XEXP (XEXP (x, 1), 1))))
4243 return reversed;
4245 /* (minus <foo> (and <foo> (const_int -pow2))) becomes
4246 (and <foo> (const_int pow2-1)) */
4247 if (GET_CODE (XEXP (x, 1)) == AND
4248 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
4249 && exact_log2 (-INTVAL (XEXP (XEXP (x, 1), 1))) >= 0
4250 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
4251 return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
4252 -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
4254 /* Canonicalize (minus A (mult (neg B) C)) to (plus (mult B C) A).
4256 if (GET_CODE (XEXP (x, 1)) == MULT
4257 && GET_CODE (XEXP (XEXP (x, 1), 0)) == NEG)
4259 rtx in1, in2;
4261 in1 = XEXP (XEXP (XEXP (x, 1), 0), 0);
4262 in2 = XEXP (XEXP (x, 1), 1);
4263 return gen_binary (PLUS, mode, gen_binary (MULT, mode, in1, in2),
4264 XEXP (x, 0));
4267 /* Canonicalize (minus (neg A) (mult B C)) to
4268 (minus (mult (neg B) C) A). */
4269 if (GET_CODE (XEXP (x, 1)) == MULT
4270 && GET_CODE (XEXP (x, 0)) == NEG)
4272 rtx in1, in2;
4274 in1 = simplify_gen_unary (NEG, mode, XEXP (XEXP (x, 1), 0), mode);
4275 in2 = XEXP (XEXP (x, 1), 1);
4276 return gen_binary (MINUS, mode, gen_binary (MULT, mode, in1, in2),
4277 XEXP (XEXP (x, 0), 0));
4280 /* Canonicalize (minus A (plus B C)) to (minus (minus A B) C) for
4281 integers. */
4282 if (GET_CODE (XEXP (x, 1)) == PLUS && INTEGRAL_MODE_P (mode))
4283 return gen_binary (MINUS, mode,
4284 gen_binary (MINUS, mode, XEXP (x, 0),
4285 XEXP (XEXP (x, 1), 0)),
4286 XEXP (XEXP (x, 1), 1));
4287 break;
4289 case MULT:
4290 /* If we have (mult (plus A B) C), apply the distributive law and then
4291 the inverse distributive law to see if things simplify. This
4292 occurs mostly in addresses, often when unrolling loops. */
4294 if (GET_CODE (XEXP (x, 0)) == PLUS)
4296 x = apply_distributive_law
4297 (gen_binary (PLUS, mode,
4298 gen_binary (MULT, mode,
4299 XEXP (XEXP (x, 0), 0), XEXP (x, 1)),
4300 gen_binary (MULT, mode,
4301 XEXP (XEXP (x, 0), 1),
4302 copy_rtx (XEXP (x, 1)))));
4304 if (GET_CODE (x) != MULT)
4305 return x;
4307 /* Try simplify a*(b/c) as (a*b)/c. */
4308 if (FLOAT_MODE_P (mode) && flag_unsafe_math_optimizations
4309 && GET_CODE (XEXP (x, 0)) == DIV)
4311 rtx tem = simplify_binary_operation (MULT, mode,
4312 XEXP (XEXP (x, 0), 0),
4313 XEXP (x, 1));
4314 if (tem)
4315 return gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
4317 break;
4319 case UDIV:
4320 /* If this is a divide by a power of two, treat it as a shift if
4321 its first operand is a shift. */
4322 if (GET_CODE (XEXP (x, 1)) == CONST_INT
4323 && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0
4324 && (GET_CODE (XEXP (x, 0)) == ASHIFT
4325 || GET_CODE (XEXP (x, 0)) == LSHIFTRT
4326 || GET_CODE (XEXP (x, 0)) == ASHIFTRT
4327 || GET_CODE (XEXP (x, 0)) == ROTATE
4328 || GET_CODE (XEXP (x, 0)) == ROTATERT))
4329 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
4330 break;
4332 case EQ: case NE:
4333 case GT: case GTU: case GE: case GEU:
4334 case LT: case LTU: case LE: case LEU:
4335 case UNEQ: case LTGT:
4336 case UNGT: case UNGE:
4337 case UNLT: case UNLE:
4338 case UNORDERED: case ORDERED:
4339 /* If the first operand is a condition code, we can't do anything
4340 with it. */
4341 if (GET_CODE (XEXP (x, 0)) == COMPARE
4342 || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
4343 && ! CC0_P (XEXP (x, 0))))
4345 rtx op0 = XEXP (x, 0);
4346 rtx op1 = XEXP (x, 1);
4347 enum rtx_code new_code;
4349 if (GET_CODE (op0) == COMPARE)
4350 op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
4352 /* Simplify our comparison, if possible. */
4353 new_code = simplify_comparison (code, &op0, &op1);
4355 /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
4356 if only the low-order bit is possibly nonzero in X (such as when
4357 X is a ZERO_EXTRACT of one bit). Similarly, we can convert EQ to
4358 (xor X 1) or (minus 1 X); we use the former. Finally, if X is
4359 known to be either 0 or -1, NE becomes a NEG and EQ becomes
4360 (plus X 1).
4362 Remove any ZERO_EXTRACT we made when thinking this was a
4363 comparison. It may now be simpler to use, e.g., an AND. If a
4364 ZERO_EXTRACT is indeed appropriate, it will be placed back by
4365 the call to make_compound_operation in the SET case. */
4367 if (STORE_FLAG_VALUE == 1
4368 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4369 && op1 == const0_rtx
4370 && mode == GET_MODE (op0)
4371 && nonzero_bits (op0, mode) == 1)
4372 return gen_lowpart (mode,
4373 expand_compound_operation (op0));
4375 else if (STORE_FLAG_VALUE == 1
4376 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4377 && op1 == const0_rtx
4378 && mode == GET_MODE (op0)
4379 && (num_sign_bit_copies (op0, mode)
4380 == GET_MODE_BITSIZE (mode)))
4382 op0 = expand_compound_operation (op0);
4383 return simplify_gen_unary (NEG, mode,
4384 gen_lowpart (mode, op0),
4385 mode);
4388 else if (STORE_FLAG_VALUE == 1
4389 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4390 && op1 == const0_rtx
4391 && mode == GET_MODE (op0)
4392 && nonzero_bits (op0, mode) == 1)
4394 op0 = expand_compound_operation (op0);
4395 return gen_binary (XOR, mode,
4396 gen_lowpart (mode, op0),
4397 const1_rtx);
4400 else if (STORE_FLAG_VALUE == 1
4401 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4402 && op1 == const0_rtx
4403 && mode == GET_MODE (op0)
4404 && (num_sign_bit_copies (op0, mode)
4405 == GET_MODE_BITSIZE (mode)))
4407 op0 = expand_compound_operation (op0);
4408 return plus_constant (gen_lowpart (mode, op0), 1);
4411 /* If STORE_FLAG_VALUE is -1, we have cases similar to
4412 those above. */
4413 if (STORE_FLAG_VALUE == -1
4414 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4415 && op1 == const0_rtx
4416 && (num_sign_bit_copies (op0, mode)
4417 == GET_MODE_BITSIZE (mode)))
4418 return gen_lowpart (mode,
4419 expand_compound_operation (op0));
4421 else if (STORE_FLAG_VALUE == -1
4422 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4423 && op1 == const0_rtx
4424 && mode == GET_MODE (op0)
4425 && nonzero_bits (op0, mode) == 1)
4427 op0 = expand_compound_operation (op0);
4428 return simplify_gen_unary (NEG, mode,
4429 gen_lowpart (mode, op0),
4430 mode);
4433 else if (STORE_FLAG_VALUE == -1
4434 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4435 && op1 == const0_rtx
4436 && mode == GET_MODE (op0)
4437 && (num_sign_bit_copies (op0, mode)
4438 == GET_MODE_BITSIZE (mode)))
4440 op0 = expand_compound_operation (op0);
4441 return simplify_gen_unary (NOT, mode,
4442 gen_lowpart (mode, op0),
4443 mode);
4446 /* If X is 0/1, (eq X 0) is X-1. */
4447 else if (STORE_FLAG_VALUE == -1
4448 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
4449 && op1 == const0_rtx
4450 && mode == GET_MODE (op0)
4451 && nonzero_bits (op0, mode) == 1)
4453 op0 = expand_compound_operation (op0);
4454 return plus_constant (gen_lowpart (mode, op0), -1);
4457 /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
4458 one bit that might be nonzero, we can convert (ne x 0) to
4459 (ashift x c) where C puts the bit in the sign bit. Remove any
4460 AND with STORE_FLAG_VALUE when we are done, since we are only
4461 going to test the sign bit. */
4462 if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
4463 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4464 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
4465 == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
4466 && op1 == const0_rtx
4467 && mode == GET_MODE (op0)
4468 && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
4470 x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
4471 expand_compound_operation (op0),
4472 GET_MODE_BITSIZE (mode) - 1 - i);
4473 if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
4474 return XEXP (x, 0);
4475 else
4476 return x;
4479 /* If the code changed, return a whole new comparison. */
4480 if (new_code != code)
4481 return gen_rtx_fmt_ee (new_code, mode, op0, op1);
4483 /* Otherwise, keep this operation, but maybe change its operands.
4484 This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR). */
4485 SUBST (XEXP (x, 0), op0);
4486 SUBST (XEXP (x, 1), op1);
4488 break;
4490 case IF_THEN_ELSE:
4491 return simplify_if_then_else (x);
4493 case ZERO_EXTRACT:
4494 case SIGN_EXTRACT:
4495 case ZERO_EXTEND:
4496 case SIGN_EXTEND:
4497 /* If we are processing SET_DEST, we are done. */
4498 if (in_dest)
4499 return x;
4501 return expand_compound_operation (x);
4503 case SET:
4504 return simplify_set (x);
4506 case AND:
4507 case IOR:
4508 case XOR:
4509 return simplify_logical (x);
4511 case ABS:
4512 /* (abs (neg <foo>)) -> (abs <foo>) */
4513 if (GET_CODE (XEXP (x, 0)) == NEG)
4514 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4516 /* If the mode of the operand is VOIDmode (i.e. if it is ASM_OPERANDS),
4517 do nothing. */
4518 if (GET_MODE (XEXP (x, 0)) == VOIDmode)
4519 break;
4521 /* If operand is something known to be positive, ignore the ABS. */
4522 if (GET_CODE (XEXP (x, 0)) == FFS || GET_CODE (XEXP (x, 0)) == ABS
4523 || ((GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
4524 <= HOST_BITS_PER_WIDE_INT)
4525 && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
4526 & ((HOST_WIDE_INT) 1
4527 << (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - 1)))
4528 == 0)))
4529 return XEXP (x, 0);
4531 /* If operand is known to be only -1 or 0, convert ABS to NEG. */
4532 if (num_sign_bit_copies (XEXP (x, 0), mode) == GET_MODE_BITSIZE (mode))
4533 return gen_rtx_NEG (mode, XEXP (x, 0));
4535 break;
4537 case FFS:
4538 /* (ffs (*_extend <X>)) = (ffs <X>) */
4539 if (GET_CODE (XEXP (x, 0)) == SIGN_EXTEND
4540 || GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
4541 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4542 break;
4544 case POPCOUNT:
4545 case PARITY:
4546 /* (pop* (zero_extend <X>)) = (pop* <X>) */
4547 if (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND)
4548 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4549 break;
4551 case FLOAT:
4552 /* (float (sign_extend <X>)) = (float <X>). */
4553 if (GET_CODE (XEXP (x, 0)) == SIGN_EXTEND)
4554 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4555 break;
4557 case ASHIFT:
4558 case LSHIFTRT:
4559 case ASHIFTRT:
4560 case ROTATE:
4561 case ROTATERT:
4562 /* If this is a shift by a constant amount, simplify it. */
4563 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
4564 return simplify_shift_const (x, code, mode, XEXP (x, 0),
4565 INTVAL (XEXP (x, 1)));
4567 else if (SHIFT_COUNT_TRUNCATED && GET_CODE (XEXP (x, 1)) != REG)
4568 SUBST (XEXP (x, 1),
4569 force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
4570 ((HOST_WIDE_INT) 1
4571 << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
4572 - 1,
4573 NULL_RTX, 0));
4574 break;
4576 case VEC_SELECT:
4578 rtx op0 = XEXP (x, 0);
4579 rtx op1 = XEXP (x, 1);
4580 int len;
4582 if (GET_CODE (op1) != PARALLEL)
4583 abort ();
4584 len = XVECLEN (op1, 0);
4585 if (len == 1
4586 && GET_CODE (XVECEXP (op1, 0, 0)) == CONST_INT
4587 && GET_CODE (op0) == VEC_CONCAT)
4589 int offset = INTVAL (XVECEXP (op1, 0, 0)) * GET_MODE_SIZE (GET_MODE (x));
4591 /* Try to find the element in the VEC_CONCAT. */
4592 for (;;)
4594 if (GET_MODE (op0) == GET_MODE (x))
4595 return op0;
4596 if (GET_CODE (op0) == VEC_CONCAT)
4598 HOST_WIDE_INT op0_size = GET_MODE_SIZE (GET_MODE (XEXP (op0, 0)));
4599 if (op0_size < offset)
4600 op0 = XEXP (op0, 0);
4601 else
4603 offset -= op0_size;
4604 op0 = XEXP (op0, 1);
4607 else
4608 break;
4613 break;
4615 default:
4616 break;
4619 return x;
4622 /* Simplify X, an IF_THEN_ELSE expression. Return the new expression. */
4624 static rtx
4625 simplify_if_then_else (rtx x)
4627 enum machine_mode mode = GET_MODE (x);
4628 rtx cond = XEXP (x, 0);
4629 rtx true_rtx = XEXP (x, 1);
4630 rtx false_rtx = XEXP (x, 2);
4631 enum rtx_code true_code = GET_CODE (cond);
4632 int comparison_p = COMPARISON_P (cond);
4633 rtx temp;
4634 int i;
4635 enum rtx_code false_code;
4636 rtx reversed;
4638 /* Simplify storing of the truth value. */
4639 if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
4640 return gen_binary (true_code, mode, XEXP (cond, 0), XEXP (cond, 1));
4642 /* Also when the truth value has to be reversed. */
4643 if (comparison_p
4644 && true_rtx == const0_rtx && false_rtx == const_true_rtx
4645 && (reversed = reversed_comparison (cond, mode, XEXP (cond, 0),
4646 XEXP (cond, 1))))
4647 return reversed;
4649 /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
4650 in it is being compared against certain values. Get the true and false
4651 comparisons and see if that says anything about the value of each arm. */
4653 if (comparison_p
4654 && ((false_code = combine_reversed_comparison_code (cond))
4655 != UNKNOWN)
4656 && GET_CODE (XEXP (cond, 0)) == REG)
4658 HOST_WIDE_INT nzb;
4659 rtx from = XEXP (cond, 0);
4660 rtx true_val = XEXP (cond, 1);
4661 rtx false_val = true_val;
4662 int swapped = 0;
4664 /* If FALSE_CODE is EQ, swap the codes and arms. */
4666 if (false_code == EQ)
4668 swapped = 1, true_code = EQ, false_code = NE;
4669 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
4672 /* If we are comparing against zero and the expression being tested has
4673 only a single bit that might be nonzero, that is its value when it is
4674 not equal to zero. Similarly if it is known to be -1 or 0. */
4676 if (true_code == EQ && true_val == const0_rtx
4677 && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
4678 false_code = EQ, false_val = GEN_INT (nzb);
4679 else if (true_code == EQ && true_val == const0_rtx
4680 && (num_sign_bit_copies (from, GET_MODE (from))
4681 == GET_MODE_BITSIZE (GET_MODE (from))))
4682 false_code = EQ, false_val = constm1_rtx;
4684 /* Now simplify an arm if we know the value of the register in the
4685 branch and it is used in the arm. Be careful due to the potential
4686 of locally-shared RTL. */
4688 if (reg_mentioned_p (from, true_rtx))
4689 true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
4690 from, true_val),
4691 pc_rtx, pc_rtx, 0, 0);
4692 if (reg_mentioned_p (from, false_rtx))
4693 false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
4694 from, false_val),
4695 pc_rtx, pc_rtx, 0, 0);
4697 SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
4698 SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
4700 true_rtx = XEXP (x, 1);
4701 false_rtx = XEXP (x, 2);
4702 true_code = GET_CODE (cond);
4705 /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
4706 reversed, do so to avoid needing two sets of patterns for
4707 subtract-and-branch insns. Similarly if we have a constant in the true
4708 arm, the false arm is the same as the first operand of the comparison, or
4709 the false arm is more complicated than the true arm. */
4711 if (comparison_p
4712 && combine_reversed_comparison_code (cond) != UNKNOWN
4713 && (true_rtx == pc_rtx
4714 || (CONSTANT_P (true_rtx)
4715 && GET_CODE (false_rtx) != CONST_INT && false_rtx != pc_rtx)
4716 || true_rtx == const0_rtx
4717 || (OBJECT_P (true_rtx) && !OBJECT_P (false_rtx))
4718 || (GET_CODE (true_rtx) == SUBREG && OBJECT_P (SUBREG_REG (true_rtx))
4719 && !OBJECT_P (false_rtx))
4720 || reg_mentioned_p (true_rtx, false_rtx)
4721 || rtx_equal_p (false_rtx, XEXP (cond, 0))))
4723 true_code = reversed_comparison_code (cond, NULL);
4724 SUBST (XEXP (x, 0),
4725 reversed_comparison (cond, GET_MODE (cond), XEXP (cond, 0),
4726 XEXP (cond, 1)));
4728 SUBST (XEXP (x, 1), false_rtx);
4729 SUBST (XEXP (x, 2), true_rtx);
4731 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
4732 cond = XEXP (x, 0);
4734 /* It is possible that the conditional has been simplified out. */
4735 true_code = GET_CODE (cond);
4736 comparison_p = COMPARISON_P (cond);
4739 /* If the two arms are identical, we don't need the comparison. */
4741 if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
4742 return true_rtx;
4744 /* Convert a == b ? b : a to "a". */
4745 if (true_code == EQ && ! side_effects_p (cond)
4746 && !HONOR_NANS (mode)
4747 && rtx_equal_p (XEXP (cond, 0), false_rtx)
4748 && rtx_equal_p (XEXP (cond, 1), true_rtx))
4749 return false_rtx;
4750 else if (true_code == NE && ! side_effects_p (cond)
4751 && !HONOR_NANS (mode)
4752 && rtx_equal_p (XEXP (cond, 0), true_rtx)
4753 && rtx_equal_p (XEXP (cond, 1), false_rtx))
4754 return true_rtx;
4756 /* Look for cases where we have (abs x) or (neg (abs X)). */
4758 if (GET_MODE_CLASS (mode) == MODE_INT
4759 && GET_CODE (false_rtx) == NEG
4760 && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
4761 && comparison_p
4762 && rtx_equal_p (true_rtx, XEXP (cond, 0))
4763 && ! side_effects_p (true_rtx))
4764 switch (true_code)
4766 case GT:
4767 case GE:
4768 return simplify_gen_unary (ABS, mode, true_rtx, mode);
4769 case LT:
4770 case LE:
4771 return
4772 simplify_gen_unary (NEG, mode,
4773 simplify_gen_unary (ABS, mode, true_rtx, mode),
4774 mode);
4775 default:
4776 break;
4779 /* Look for MIN or MAX. */
4781 if ((! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
4782 && comparison_p
4783 && rtx_equal_p (XEXP (cond, 0), true_rtx)
4784 && rtx_equal_p (XEXP (cond, 1), false_rtx)
4785 && ! side_effects_p (cond))
4786 switch (true_code)
4788 case GE:
4789 case GT:
4790 return gen_binary (SMAX, mode, true_rtx, false_rtx);
4791 case LE:
4792 case LT:
4793 return gen_binary (SMIN, mode, true_rtx, false_rtx);
4794 case GEU:
4795 case GTU:
4796 return gen_binary (UMAX, mode, true_rtx, false_rtx);
4797 case LEU:
4798 case LTU:
4799 return gen_binary (UMIN, mode, true_rtx, false_rtx);
4800 default:
4801 break;
4804 /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
4805 second operand is zero, this can be done as (OP Z (mult COND C2)) where
4806 C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
4807 SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
4808 We can do this kind of thing in some cases when STORE_FLAG_VALUE is
4809 neither 1 or -1, but it isn't worth checking for. */
4811 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
4812 && comparison_p
4813 && GET_MODE_CLASS (mode) == MODE_INT
4814 && ! side_effects_p (x))
4816 rtx t = make_compound_operation (true_rtx, SET);
4817 rtx f = make_compound_operation (false_rtx, SET);
4818 rtx cond_op0 = XEXP (cond, 0);
4819 rtx cond_op1 = XEXP (cond, 1);
4820 enum rtx_code op = NIL, extend_op = NIL;
4821 enum machine_mode m = mode;
4822 rtx z = 0, c1 = NULL_RTX;
4824 if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
4825 || GET_CODE (t) == IOR || GET_CODE (t) == XOR
4826 || GET_CODE (t) == ASHIFT
4827 || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
4828 && rtx_equal_p (XEXP (t, 0), f))
4829 c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
4831 /* If an identity-zero op is commutative, check whether there
4832 would be a match if we swapped the operands. */
4833 else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
4834 || GET_CODE (t) == XOR)
4835 && rtx_equal_p (XEXP (t, 1), f))
4836 c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
4837 else if (GET_CODE (t) == SIGN_EXTEND
4838 && (GET_CODE (XEXP (t, 0)) == PLUS
4839 || GET_CODE (XEXP (t, 0)) == MINUS
4840 || GET_CODE (XEXP (t, 0)) == IOR
4841 || GET_CODE (XEXP (t, 0)) == XOR
4842 || GET_CODE (XEXP (t, 0)) == ASHIFT
4843 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
4844 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
4845 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
4846 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
4847 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
4848 && (num_sign_bit_copies (f, GET_MODE (f))
4849 > (unsigned int)
4850 (GET_MODE_BITSIZE (mode)
4851 - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 0))))))
4853 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
4854 extend_op = SIGN_EXTEND;
4855 m = GET_MODE (XEXP (t, 0));
4857 else if (GET_CODE (t) == SIGN_EXTEND
4858 && (GET_CODE (XEXP (t, 0)) == PLUS
4859 || GET_CODE (XEXP (t, 0)) == IOR
4860 || GET_CODE (XEXP (t, 0)) == XOR)
4861 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
4862 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
4863 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
4864 && (num_sign_bit_copies (f, GET_MODE (f))
4865 > (unsigned int)
4866 (GET_MODE_BITSIZE (mode)
4867 - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 1))))))
4869 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
4870 extend_op = SIGN_EXTEND;
4871 m = GET_MODE (XEXP (t, 0));
4873 else if (GET_CODE (t) == ZERO_EXTEND
4874 && (GET_CODE (XEXP (t, 0)) == PLUS
4875 || GET_CODE (XEXP (t, 0)) == MINUS
4876 || GET_CODE (XEXP (t, 0)) == IOR
4877 || GET_CODE (XEXP (t, 0)) == XOR
4878 || GET_CODE (XEXP (t, 0)) == ASHIFT
4879 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
4880 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
4881 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
4882 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4883 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
4884 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
4885 && ((nonzero_bits (f, GET_MODE (f))
4886 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
4887 == 0))
4889 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
4890 extend_op = ZERO_EXTEND;
4891 m = GET_MODE (XEXP (t, 0));
4893 else if (GET_CODE (t) == ZERO_EXTEND
4894 && (GET_CODE (XEXP (t, 0)) == PLUS
4895 || GET_CODE (XEXP (t, 0)) == IOR
4896 || GET_CODE (XEXP (t, 0)) == XOR)
4897 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
4898 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4899 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
4900 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
4901 && ((nonzero_bits (f, GET_MODE (f))
4902 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
4903 == 0))
4905 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
4906 extend_op = ZERO_EXTEND;
4907 m = GET_MODE (XEXP (t, 0));
4910 if (z)
4912 temp = subst (gen_binary (true_code, m, cond_op0, cond_op1),
4913 pc_rtx, pc_rtx, 0, 0);
4914 temp = gen_binary (MULT, m, temp,
4915 gen_binary (MULT, m, c1, const_true_rtx));
4916 temp = subst (temp, pc_rtx, pc_rtx, 0, 0);
4917 temp = gen_binary (op, m, gen_lowpart (m, z), temp);
4919 if (extend_op != NIL)
4920 temp = simplify_gen_unary (extend_op, mode, temp, m);
4922 return temp;
4926 /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
4927 1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
4928 negation of a single bit, we can convert this operation to a shift. We
4929 can actually do this more generally, but it doesn't seem worth it. */
4931 if (true_code == NE && XEXP (cond, 1) == const0_rtx
4932 && false_rtx == const0_rtx && GET_CODE (true_rtx) == CONST_INT
4933 && ((1 == nonzero_bits (XEXP (cond, 0), mode)
4934 && (i = exact_log2 (INTVAL (true_rtx))) >= 0)
4935 || ((num_sign_bit_copies (XEXP (cond, 0), mode)
4936 == GET_MODE_BITSIZE (mode))
4937 && (i = exact_log2 (-INTVAL (true_rtx))) >= 0)))
4938 return
4939 simplify_shift_const (NULL_RTX, ASHIFT, mode,
4940 gen_lowpart (mode, XEXP (cond, 0)), i);
4942 /* (IF_THEN_ELSE (NE REG 0) (0) (8)) is REG for nonzero_bits (REG) == 8. */
4943 if (true_code == NE && XEXP (cond, 1) == const0_rtx
4944 && false_rtx == const0_rtx && GET_CODE (true_rtx) == CONST_INT
4945 && GET_MODE (XEXP (cond, 0)) == mode
4946 && (INTVAL (true_rtx) & GET_MODE_MASK (mode))
4947 == nonzero_bits (XEXP (cond, 0), mode)
4948 && (i = exact_log2 (INTVAL (true_rtx) & GET_MODE_MASK (mode))) >= 0)
4949 return XEXP (cond, 0);
4951 return x;
4954 /* Simplify X, a SET expression. Return the new expression. */
4956 static rtx
4957 simplify_set (rtx x)
4959 rtx src = SET_SRC (x);
4960 rtx dest = SET_DEST (x);
4961 enum machine_mode mode
4962 = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
4963 rtx other_insn;
4964 rtx *cc_use;
4966 /* (set (pc) (return)) gets written as (return). */
4967 if (GET_CODE (dest) == PC && GET_CODE (src) == RETURN)
4968 return src;
4970 /* Now that we know for sure which bits of SRC we are using, see if we can
4971 simplify the expression for the object knowing that we only need the
4972 low-order bits. */
4974 if (GET_MODE_CLASS (mode) == MODE_INT
4975 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
4977 src = force_to_mode (src, mode, ~(HOST_WIDE_INT) 0, NULL_RTX, 0);
4978 SUBST (SET_SRC (x), src);
4981 /* If we are setting CC0 or if the source is a COMPARE, look for the use of
4982 the comparison result and try to simplify it unless we already have used
4983 undobuf.other_insn. */
4984 if ((GET_MODE_CLASS (mode) == MODE_CC
4985 || GET_CODE (src) == COMPARE
4986 || CC0_P (dest))
4987 && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
4988 && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
4989 && COMPARISON_P (*cc_use)
4990 && rtx_equal_p (XEXP (*cc_use, 0), dest))
4992 enum rtx_code old_code = GET_CODE (*cc_use);
4993 enum rtx_code new_code;
4994 rtx op0, op1, tmp;
4995 int other_changed = 0;
4996 enum machine_mode compare_mode = GET_MODE (dest);
4997 enum machine_mode tmp_mode;
4999 if (GET_CODE (src) == COMPARE)
5000 op0 = XEXP (src, 0), op1 = XEXP (src, 1);
5001 else
5002 op0 = src, op1 = const0_rtx;
5004 /* Check whether the comparison is known at compile time. */
5005 if (GET_MODE (op0) != VOIDmode)
5006 tmp_mode = GET_MODE (op0);
5007 else if (GET_MODE (op1) != VOIDmode)
5008 tmp_mode = GET_MODE (op1);
5009 else
5010 tmp_mode = compare_mode;
5011 tmp = simplify_const_relational_operation (old_code, tmp_mode,
5012 op0, op1);
5013 if (tmp != NULL_RTX)
5015 rtx pat = PATTERN (other_insn);
5016 undobuf.other_insn = other_insn;
5017 SUBST (*cc_use, tmp);
5019 /* Attempt to simplify CC user. */
5020 if (GET_CODE (pat) == SET)
5022 rtx new = simplify_rtx (SET_SRC (pat));
5023 if (new != NULL_RTX)
5024 SUBST (SET_SRC (pat), new);
5027 /* Convert X into a no-op move. */
5028 SUBST (SET_DEST (x), pc_rtx);
5029 SUBST (SET_SRC (x), pc_rtx);
5030 return x;
5033 /* Simplify our comparison, if possible. */
5034 new_code = simplify_comparison (old_code, &op0, &op1);
5036 #ifdef SELECT_CC_MODE
5037 /* If this machine has CC modes other than CCmode, check to see if we
5038 need to use a different CC mode here. */
5039 compare_mode = SELECT_CC_MODE (new_code, op0, op1);
5041 #ifndef HAVE_cc0
5042 /* If the mode changed, we have to change SET_DEST, the mode in the
5043 compare, and the mode in the place SET_DEST is used. If SET_DEST is
5044 a hard register, just build new versions with the proper mode. If it
5045 is a pseudo, we lose unless it is only time we set the pseudo, in
5046 which case we can safely change its mode. */
5047 if (compare_mode != GET_MODE (dest))
5049 unsigned int regno = REGNO (dest);
5050 rtx new_dest = gen_rtx_REG (compare_mode, regno);
5052 if (regno < FIRST_PSEUDO_REGISTER
5053 || (REG_N_SETS (regno) == 1 && ! REG_USERVAR_P (dest)))
5055 if (regno >= FIRST_PSEUDO_REGISTER)
5056 SUBST (regno_reg_rtx[regno], new_dest);
5058 SUBST (SET_DEST (x), new_dest);
5059 SUBST (XEXP (*cc_use, 0), new_dest);
5060 other_changed = 1;
5062 dest = new_dest;
5065 #endif /* cc0 */
5066 #endif /* SELECT_CC_MODE */
5068 /* If the code changed, we have to build a new comparison in
5069 undobuf.other_insn. */
5070 if (new_code != old_code)
5072 int other_changed_previously = other_changed;
5073 unsigned HOST_WIDE_INT mask;
5075 SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
5076 dest, const0_rtx));
5077 other_changed = 1;
5079 /* If the only change we made was to change an EQ into an NE or
5080 vice versa, OP0 has only one bit that might be nonzero, and OP1
5081 is zero, check if changing the user of the condition code will
5082 produce a valid insn. If it won't, we can keep the original code
5083 in that insn by surrounding our operation with an XOR. */
5085 if (((old_code == NE && new_code == EQ)
5086 || (old_code == EQ && new_code == NE))
5087 && ! other_changed_previously && op1 == const0_rtx
5088 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
5089 && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
5091 rtx pat = PATTERN (other_insn), note = 0;
5093 if ((recog_for_combine (&pat, other_insn, &note) < 0
5094 && ! check_asm_operands (pat)))
5096 PUT_CODE (*cc_use, old_code);
5097 other_changed = 0;
5099 op0 = gen_binary (XOR, GET_MODE (op0), op0, GEN_INT (mask));
5104 if (other_changed)
5105 undobuf.other_insn = other_insn;
5107 #ifdef HAVE_cc0
5108 /* If we are now comparing against zero, change our source if
5109 needed. If we do not use cc0, we always have a COMPARE. */
5110 if (op1 == const0_rtx && dest == cc0_rtx)
5112 SUBST (SET_SRC (x), op0);
5113 src = op0;
5115 else
5116 #endif
5118 /* Otherwise, if we didn't previously have a COMPARE in the
5119 correct mode, we need one. */
5120 if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
5122 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
5123 src = SET_SRC (x);
5125 else
5127 /* Otherwise, update the COMPARE if needed. */
5128 SUBST (XEXP (src, 0), op0);
5129 SUBST (XEXP (src, 1), op1);
5132 else
5134 /* Get SET_SRC in a form where we have placed back any
5135 compound expressions. Then do the checks below. */
5136 src = make_compound_operation (src, SET);
5137 SUBST (SET_SRC (x), src);
5140 /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
5141 and X being a REG or (subreg (reg)), we may be able to convert this to
5142 (set (subreg:m2 x) (op)).
5144 We can always do this if M1 is narrower than M2 because that means that
5145 we only care about the low bits of the result.
5147 However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
5148 perform a narrower operation than requested since the high-order bits will
5149 be undefined. On machine where it is defined, this transformation is safe
5150 as long as M1 and M2 have the same number of words. */
5152 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
5153 && !OBJECT_P (SUBREG_REG (src))
5154 && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
5155 / UNITS_PER_WORD)
5156 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
5157 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
5158 #ifndef WORD_REGISTER_OPERATIONS
5159 && (GET_MODE_SIZE (GET_MODE (src))
5160 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
5161 #endif
5162 #ifdef CANNOT_CHANGE_MODE_CLASS
5163 && ! (GET_CODE (dest) == REG && REGNO (dest) < FIRST_PSEUDO_REGISTER
5164 && REG_CANNOT_CHANGE_MODE_P (REGNO (dest),
5165 GET_MODE (SUBREG_REG (src)),
5166 GET_MODE (src)))
5167 #endif
5168 && (GET_CODE (dest) == REG
5169 || (GET_CODE (dest) == SUBREG
5170 && GET_CODE (SUBREG_REG (dest)) == REG)))
5172 SUBST (SET_DEST (x),
5173 gen_lowpart (GET_MODE (SUBREG_REG (src)),
5174 dest));
5175 SUBST (SET_SRC (x), SUBREG_REG (src));
5177 src = SET_SRC (x), dest = SET_DEST (x);
5180 #ifdef HAVE_cc0
5181 /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
5182 in SRC. */
5183 if (dest == cc0_rtx
5184 && GET_CODE (src) == SUBREG
5185 && subreg_lowpart_p (src)
5186 && (GET_MODE_BITSIZE (GET_MODE (src))
5187 < GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (src)))))
5189 rtx inner = SUBREG_REG (src);
5190 enum machine_mode inner_mode = GET_MODE (inner);
5192 /* Here we make sure that we don't have a sign bit on. */
5193 if (GET_MODE_BITSIZE (inner_mode) <= HOST_BITS_PER_WIDE_INT
5194 && (nonzero_bits (inner, inner_mode)
5195 < ((unsigned HOST_WIDE_INT) 1
5196 << (GET_MODE_BITSIZE (GET_MODE (src)) - 1))))
5198 SUBST (SET_SRC (x), inner);
5199 src = SET_SRC (x);
5202 #endif
5204 #ifdef LOAD_EXTEND_OP
5205 /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
5206 would require a paradoxical subreg. Replace the subreg with a
5207 zero_extend to avoid the reload that would otherwise be required. */
5209 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
5210 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != NIL
5211 && SUBREG_BYTE (src) == 0
5212 && (GET_MODE_SIZE (GET_MODE (src))
5213 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
5214 && GET_CODE (SUBREG_REG (src)) == MEM)
5216 SUBST (SET_SRC (x),
5217 gen_rtx_fmt_e (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
5218 GET_MODE (src), SUBREG_REG (src)));
5220 src = SET_SRC (x);
5222 #endif
5224 /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
5225 are comparing an item known to be 0 or -1 against 0, use a logical
5226 operation instead. Check for one of the arms being an IOR of the other
5227 arm with some value. We compute three terms to be IOR'ed together. In
5228 practice, at most two will be nonzero. Then we do the IOR's. */
5230 if (GET_CODE (dest) != PC
5231 && GET_CODE (src) == IF_THEN_ELSE
5232 && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
5233 && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
5234 && XEXP (XEXP (src, 0), 1) == const0_rtx
5235 && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
5236 #ifdef HAVE_conditional_move
5237 && ! can_conditionally_move_p (GET_MODE (src))
5238 #endif
5239 && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
5240 GET_MODE (XEXP (XEXP (src, 0), 0)))
5241 == GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (src, 0), 0))))
5242 && ! side_effects_p (src))
5244 rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
5245 ? XEXP (src, 1) : XEXP (src, 2));
5246 rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
5247 ? XEXP (src, 2) : XEXP (src, 1));
5248 rtx term1 = const0_rtx, term2, term3;
5250 if (GET_CODE (true_rtx) == IOR
5251 && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
5252 term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
5253 else if (GET_CODE (true_rtx) == IOR
5254 && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
5255 term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
5256 else if (GET_CODE (false_rtx) == IOR
5257 && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
5258 term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
5259 else if (GET_CODE (false_rtx) == IOR
5260 && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
5261 term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
5263 term2 = gen_binary (AND, GET_MODE (src),
5264 XEXP (XEXP (src, 0), 0), true_rtx);
5265 term3 = gen_binary (AND, GET_MODE (src),
5266 simplify_gen_unary (NOT, GET_MODE (src),
5267 XEXP (XEXP (src, 0), 0),
5268 GET_MODE (src)),
5269 false_rtx);
5271 SUBST (SET_SRC (x),
5272 gen_binary (IOR, GET_MODE (src),
5273 gen_binary (IOR, GET_MODE (src), term1, term2),
5274 term3));
5276 src = SET_SRC (x);
5279 /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
5280 whole thing fail. */
5281 if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
5282 return src;
5283 else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
5284 return dest;
5285 else
5286 /* Convert this into a field assignment operation, if possible. */
5287 return make_field_assignment (x);
5290 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
5291 result. */
5293 static rtx
5294 simplify_logical (rtx x)
5296 enum machine_mode mode = GET_MODE (x);
5297 rtx op0 = XEXP (x, 0);
5298 rtx op1 = XEXP (x, 1);
5299 rtx reversed;
5301 switch (GET_CODE (x))
5303 case AND:
5304 /* Convert (A ^ B) & A to A & (~B) since the latter is often a single
5305 insn (and may simplify more). */
5306 if (GET_CODE (op0) == XOR
5307 && rtx_equal_p (XEXP (op0, 0), op1)
5308 && ! side_effects_p (op1))
5309 x = gen_binary (AND, mode,
5310 simplify_gen_unary (NOT, mode, XEXP (op0, 1), mode),
5311 op1);
5313 if (GET_CODE (op0) == XOR
5314 && rtx_equal_p (XEXP (op0, 1), op1)
5315 && ! side_effects_p (op1))
5316 x = gen_binary (AND, mode,
5317 simplify_gen_unary (NOT, mode, XEXP (op0, 0), mode),
5318 op1);
5320 /* Similarly for (~(A ^ B)) & A. */
5321 if (GET_CODE (op0) == NOT
5322 && GET_CODE (XEXP (op0, 0)) == XOR
5323 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), op1)
5324 && ! side_effects_p (op1))
5325 x = gen_binary (AND, mode, XEXP (XEXP (op0, 0), 1), op1);
5327 if (GET_CODE (op0) == NOT
5328 && GET_CODE (XEXP (op0, 0)) == XOR
5329 && rtx_equal_p (XEXP (XEXP (op0, 0), 1), op1)
5330 && ! side_effects_p (op1))
5331 x = gen_binary (AND, mode, XEXP (XEXP (op0, 0), 0), op1);
5333 /* We can call simplify_and_const_int only if we don't lose
5334 any (sign) bits when converting INTVAL (op1) to
5335 "unsigned HOST_WIDE_INT". */
5336 if (GET_CODE (op1) == CONST_INT
5337 && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5338 || INTVAL (op1) > 0))
5340 x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
5342 /* If we have (ior (and (X C1) C2)) and the next restart would be
5343 the last, simplify this by making C1 as small as possible
5344 and then exit. Only do this if C1 actually changes: for now
5345 this only saves memory but, should this transformation be
5346 moved to simplify-rtx.c, we'd risk unbounded recursion there. */
5347 if (GET_CODE (x) == IOR && GET_CODE (op0) == AND
5348 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5349 && GET_CODE (op1) == CONST_INT
5350 && (INTVAL (XEXP (op0, 1)) & INTVAL (op1)) != 0)
5351 return gen_binary (IOR, mode,
5352 gen_binary (AND, mode, XEXP (op0, 0),
5353 GEN_INT (INTVAL (XEXP (op0, 1))
5354 & ~INTVAL (op1))), op1);
5356 if (GET_CODE (x) != AND)
5357 return x;
5359 op0 = XEXP (x, 0);
5360 op1 = XEXP (x, 1);
5363 /* Convert (A | B) & A to A. */
5364 if (GET_CODE (op0) == IOR
5365 && (rtx_equal_p (XEXP (op0, 0), op1)
5366 || rtx_equal_p (XEXP (op0, 1), op1))
5367 && ! side_effects_p (XEXP (op0, 0))
5368 && ! side_effects_p (XEXP (op0, 1)))
5369 return op1;
5371 /* In the following group of tests (and those in case IOR below),
5372 we start with some combination of logical operations and apply
5373 the distributive law followed by the inverse distributive law.
5374 Most of the time, this results in no change. However, if some of
5375 the operands are the same or inverses of each other, simplifications
5376 will result.
5378 For example, (and (ior A B) (not B)) can occur as the result of
5379 expanding a bit field assignment. When we apply the distributive
5380 law to this, we get (ior (and (A (not B))) (and (B (not B)))),
5381 which then simplifies to (and (A (not B))).
5383 If we have (and (ior A B) C), apply the distributive law and then
5384 the inverse distributive law to see if things simplify. */
5386 if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
5388 x = apply_distributive_law
5389 (gen_binary (GET_CODE (op0), mode,
5390 gen_binary (AND, mode, XEXP (op0, 0), op1),
5391 gen_binary (AND, mode, XEXP (op0, 1),
5392 copy_rtx (op1))));
5393 if (GET_CODE (x) != AND)
5394 return x;
5397 if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
5398 return apply_distributive_law
5399 (gen_binary (GET_CODE (op1), mode,
5400 gen_binary (AND, mode, XEXP (op1, 0), op0),
5401 gen_binary (AND, mode, XEXP (op1, 1),
5402 copy_rtx (op0))));
5404 /* Similarly, taking advantage of the fact that
5405 (and (not A) (xor B C)) == (xor (ior A B) (ior A C)) */
5407 if (GET_CODE (op0) == NOT && GET_CODE (op1) == XOR)
5408 return apply_distributive_law
5409 (gen_binary (XOR, mode,
5410 gen_binary (IOR, mode, XEXP (op0, 0), XEXP (op1, 0)),
5411 gen_binary (IOR, mode, copy_rtx (XEXP (op0, 0)),
5412 XEXP (op1, 1))));
5414 else if (GET_CODE (op1) == NOT && GET_CODE (op0) == XOR)
5415 return apply_distributive_law
5416 (gen_binary (XOR, mode,
5417 gen_binary (IOR, mode, XEXP (op1, 0), XEXP (op0, 0)),
5418 gen_binary (IOR, mode, copy_rtx (XEXP (op1, 0)), XEXP (op0, 1))));
5419 break;
5421 case IOR:
5422 /* (ior A C) is C if all bits of A that might be nonzero are on in C. */
5423 if (GET_CODE (op1) == CONST_INT
5424 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5425 && (nonzero_bits (op0, mode) & ~INTVAL (op1)) == 0)
5426 return op1;
5428 /* Convert (A & B) | A to A. */
5429 if (GET_CODE (op0) == AND
5430 && (rtx_equal_p (XEXP (op0, 0), op1)
5431 || rtx_equal_p (XEXP (op0, 1), op1))
5432 && ! side_effects_p (XEXP (op0, 0))
5433 && ! side_effects_p (XEXP (op0, 1)))
5434 return op1;
5436 /* If we have (ior (and A B) C), apply the distributive law and then
5437 the inverse distributive law to see if things simplify. */
5439 if (GET_CODE (op0) == AND)
5441 x = apply_distributive_law
5442 (gen_binary (AND, mode,
5443 gen_binary (IOR, mode, XEXP (op0, 0), op1),
5444 gen_binary (IOR, mode, XEXP (op0, 1),
5445 copy_rtx (op1))));
5447 if (GET_CODE (x) != IOR)
5448 return x;
5451 if (GET_CODE (op1) == AND)
5453 x = apply_distributive_law
5454 (gen_binary (AND, mode,
5455 gen_binary (IOR, mode, XEXP (op1, 0), op0),
5456 gen_binary (IOR, mode, XEXP (op1, 1),
5457 copy_rtx (op0))));
5459 if (GET_CODE (x) != IOR)
5460 return x;
5463 /* Convert (ior (ashift A CX) (lshiftrt A CY)) where CX+CY equals the
5464 mode size to (rotate A CX). */
5466 if (((GET_CODE (op0) == ASHIFT && GET_CODE (op1) == LSHIFTRT)
5467 || (GET_CODE (op1) == ASHIFT && GET_CODE (op0) == LSHIFTRT))
5468 && rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
5469 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5470 && GET_CODE (XEXP (op1, 1)) == CONST_INT
5471 && (INTVAL (XEXP (op0, 1)) + INTVAL (XEXP (op1, 1))
5472 == GET_MODE_BITSIZE (mode)))
5473 return gen_rtx_ROTATE (mode, XEXP (op0, 0),
5474 (GET_CODE (op0) == ASHIFT
5475 ? XEXP (op0, 1) : XEXP (op1, 1)));
5477 /* If OP0 is (ashiftrt (plus ...) C), it might actually be
5478 a (sign_extend (plus ...)). If so, OP1 is a CONST_INT, and the PLUS
5479 does not affect any of the bits in OP1, it can really be done
5480 as a PLUS and we can associate. We do this by seeing if OP1
5481 can be safely shifted left C bits. */
5482 if (GET_CODE (op1) == CONST_INT && GET_CODE (op0) == ASHIFTRT
5483 && GET_CODE (XEXP (op0, 0)) == PLUS
5484 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
5485 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5486 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT)
5488 int count = INTVAL (XEXP (op0, 1));
5489 HOST_WIDE_INT mask = INTVAL (op1) << count;
5491 if (mask >> count == INTVAL (op1)
5492 && (mask & nonzero_bits (XEXP (op0, 0), mode)) == 0)
5494 SUBST (XEXP (XEXP (op0, 0), 1),
5495 GEN_INT (INTVAL (XEXP (XEXP (op0, 0), 1)) | mask));
5496 return op0;
5499 break;
5501 case XOR:
5502 /* If we are XORing two things that have no bits in common,
5503 convert them into an IOR. This helps to detect rotation encoded
5504 using those methods and possibly other simplifications. */
5506 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5507 && (nonzero_bits (op0, mode)
5508 & nonzero_bits (op1, mode)) == 0)
5509 return (gen_binary (IOR, mode, op0, op1));
5511 /* Convert (XOR (NOT x) (NOT y)) to (XOR x y).
5512 Also convert (XOR (NOT x) y) to (NOT (XOR x y)), similarly for
5513 (NOT y). */
5515 int num_negated = 0;
5517 if (GET_CODE (op0) == NOT)
5518 num_negated++, op0 = XEXP (op0, 0);
5519 if (GET_CODE (op1) == NOT)
5520 num_negated++, op1 = XEXP (op1, 0);
5522 if (num_negated == 2)
5524 SUBST (XEXP (x, 0), op0);
5525 SUBST (XEXP (x, 1), op1);
5527 else if (num_negated == 1)
5528 return
5529 simplify_gen_unary (NOT, mode, gen_binary (XOR, mode, op0, op1),
5530 mode);
5533 /* Convert (xor (and A B) B) to (and (not A) B). The latter may
5534 correspond to a machine insn or result in further simplifications
5535 if B is a constant. */
5537 if (GET_CODE (op0) == AND
5538 && rtx_equal_p (XEXP (op0, 1), op1)
5539 && ! side_effects_p (op1))
5540 return gen_binary (AND, mode,
5541 simplify_gen_unary (NOT, mode, XEXP (op0, 0), mode),
5542 op1);
5544 else if (GET_CODE (op0) == AND
5545 && rtx_equal_p (XEXP (op0, 0), op1)
5546 && ! side_effects_p (op1))
5547 return gen_binary (AND, mode,
5548 simplify_gen_unary (NOT, mode, XEXP (op0, 1), mode),
5549 op1);
5551 /* (xor (comparison foo bar) (const_int 1)) can become the reversed
5552 comparison if STORE_FLAG_VALUE is 1. */
5553 if (STORE_FLAG_VALUE == 1
5554 && op1 == const1_rtx
5555 && COMPARISON_P (op0)
5556 && (reversed = reversed_comparison (op0, mode, XEXP (op0, 0),
5557 XEXP (op0, 1))))
5558 return reversed;
5560 /* (lshiftrt foo C) where C is the number of bits in FOO minus 1
5561 is (lt foo (const_int 0)), so we can perform the above
5562 simplification if STORE_FLAG_VALUE is 1. */
5564 if (STORE_FLAG_VALUE == 1
5565 && op1 == const1_rtx
5566 && GET_CODE (op0) == LSHIFTRT
5567 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5568 && INTVAL (XEXP (op0, 1)) == GET_MODE_BITSIZE (mode) - 1)
5569 return gen_rtx_GE (mode, XEXP (op0, 0), const0_rtx);
5571 /* (xor (comparison foo bar) (const_int sign-bit))
5572 when STORE_FLAG_VALUE is the sign bit. */
5573 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5574 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
5575 == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
5576 && op1 == const_true_rtx
5577 && COMPARISON_P (op0)
5578 && (reversed = reversed_comparison (op0, mode, XEXP (op0, 0),
5579 XEXP (op0, 1))))
5580 return reversed;
5582 break;
5584 default:
5585 abort ();
5588 return x;
5591 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
5592 operations" because they can be replaced with two more basic operations.
5593 ZERO_EXTEND is also considered "compound" because it can be replaced with
5594 an AND operation, which is simpler, though only one operation.
5596 The function expand_compound_operation is called with an rtx expression
5597 and will convert it to the appropriate shifts and AND operations,
5598 simplifying at each stage.
5600 The function make_compound_operation is called to convert an expression
5601 consisting of shifts and ANDs into the equivalent compound expression.
5602 It is the inverse of this function, loosely speaking. */
5604 static rtx
5605 expand_compound_operation (rtx x)
5607 unsigned HOST_WIDE_INT pos = 0, len;
5608 int unsignedp = 0;
5609 unsigned int modewidth;
5610 rtx tem;
5612 switch (GET_CODE (x))
5614 case ZERO_EXTEND:
5615 unsignedp = 1;
5616 case SIGN_EXTEND:
5617 /* We can't necessarily use a const_int for a multiword mode;
5618 it depends on implicitly extending the value.
5619 Since we don't know the right way to extend it,
5620 we can't tell whether the implicit way is right.
5622 Even for a mode that is no wider than a const_int,
5623 we can't win, because we need to sign extend one of its bits through
5624 the rest of it, and we don't know which bit. */
5625 if (GET_CODE (XEXP (x, 0)) == CONST_INT)
5626 return x;
5628 /* Return if (subreg:MODE FROM 0) is not a safe replacement for
5629 (zero_extend:MODE FROM) or (sign_extend:MODE FROM). It is for any MEM
5630 because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
5631 reloaded. If not for that, MEM's would very rarely be safe.
5633 Reject MODEs bigger than a word, because we might not be able
5634 to reference a two-register group starting with an arbitrary register
5635 (and currently gen_lowpart might crash for a SUBREG). */
5637 if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
5638 return x;
5640 /* Reject MODEs that aren't scalar integers because turning vector
5641 or complex modes into shifts causes problems. */
5643 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
5644 return x;
5646 len = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)));
5647 /* If the inner object has VOIDmode (the only way this can happen
5648 is if it is an ASM_OPERANDS), we can't do anything since we don't
5649 know how much masking to do. */
5650 if (len == 0)
5651 return x;
5653 break;
5655 case ZERO_EXTRACT:
5656 unsignedp = 1;
5657 case SIGN_EXTRACT:
5658 /* If the operand is a CLOBBER, just return it. */
5659 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
5660 return XEXP (x, 0);
5662 if (GET_CODE (XEXP (x, 1)) != CONST_INT
5663 || GET_CODE (XEXP (x, 2)) != CONST_INT
5664 || GET_MODE (XEXP (x, 0)) == VOIDmode)
5665 return x;
5667 /* Reject MODEs that aren't scalar integers because turning vector
5668 or complex modes into shifts causes problems. */
5670 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
5671 return x;
5673 len = INTVAL (XEXP (x, 1));
5674 pos = INTVAL (XEXP (x, 2));
5676 /* If this goes outside the object being extracted, replace the object
5677 with a (use (mem ...)) construct that only combine understands
5678 and is used only for this purpose. */
5679 if (len + pos > GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
5680 SUBST (XEXP (x, 0), gen_rtx_USE (GET_MODE (x), XEXP (x, 0)));
5682 if (BITS_BIG_ENDIAN)
5683 pos = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - len - pos;
5685 break;
5687 default:
5688 return x;
5690 /* Convert sign extension to zero extension, if we know that the high
5691 bit is not set, as this is easier to optimize. It will be converted
5692 back to cheaper alternative in make_extraction. */
5693 if (GET_CODE (x) == SIGN_EXTEND
5694 && (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5695 && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
5696 & ~(((unsigned HOST_WIDE_INT)
5697 GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
5698 >> 1))
5699 == 0)))
5701 rtx temp = gen_rtx_ZERO_EXTEND (GET_MODE (x), XEXP (x, 0));
5702 rtx temp2 = expand_compound_operation (temp);
5704 /* Make sure this is a profitable operation. */
5705 if (rtx_cost (x, SET) > rtx_cost (temp2, SET))
5706 return temp2;
5707 else if (rtx_cost (x, SET) > rtx_cost (temp, SET))
5708 return temp;
5709 else
5710 return x;
5713 /* We can optimize some special cases of ZERO_EXTEND. */
5714 if (GET_CODE (x) == ZERO_EXTEND)
5716 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
5717 know that the last value didn't have any inappropriate bits
5718 set. */
5719 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
5720 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
5721 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5722 && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
5723 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5724 return XEXP (XEXP (x, 0), 0);
5726 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
5727 if (GET_CODE (XEXP (x, 0)) == SUBREG
5728 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
5729 && subreg_lowpart_p (XEXP (x, 0))
5730 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5731 && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
5732 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5733 return SUBREG_REG (XEXP (x, 0));
5735 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
5736 is a comparison and STORE_FLAG_VALUE permits. This is like
5737 the first case, but it works even when GET_MODE (x) is larger
5738 than HOST_WIDE_INT. */
5739 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
5740 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
5741 && COMPARISON_P (XEXP (XEXP (x, 0), 0))
5742 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5743 <= HOST_BITS_PER_WIDE_INT)
5744 && ((HOST_WIDE_INT) STORE_FLAG_VALUE
5745 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5746 return XEXP (XEXP (x, 0), 0);
5748 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
5749 if (GET_CODE (XEXP (x, 0)) == SUBREG
5750 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
5751 && subreg_lowpart_p (XEXP (x, 0))
5752 && COMPARISON_P (SUBREG_REG (XEXP (x, 0)))
5753 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5754 <= HOST_BITS_PER_WIDE_INT)
5755 && ((HOST_WIDE_INT) STORE_FLAG_VALUE
5756 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5757 return SUBREG_REG (XEXP (x, 0));
5761 /* If we reach here, we want to return a pair of shifts. The inner
5762 shift is a left shift of BITSIZE - POS - LEN bits. The outer
5763 shift is a right shift of BITSIZE - LEN bits. It is arithmetic or
5764 logical depending on the value of UNSIGNEDP.
5766 If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
5767 converted into an AND of a shift.
5769 We must check for the case where the left shift would have a negative
5770 count. This can happen in a case like (x >> 31) & 255 on machines
5771 that can't shift by a constant. On those machines, we would first
5772 combine the shift with the AND to produce a variable-position
5773 extraction. Then the constant of 31 would be substituted in to produce
5774 a such a position. */
5776 modewidth = GET_MODE_BITSIZE (GET_MODE (x));
5777 if (modewidth + len >= pos)
5778 tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
5779 GET_MODE (x),
5780 simplify_shift_const (NULL_RTX, ASHIFT,
5781 GET_MODE (x),
5782 XEXP (x, 0),
5783 modewidth - pos - len),
5784 modewidth - len);
5786 else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
5787 tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
5788 simplify_shift_const (NULL_RTX, LSHIFTRT,
5789 GET_MODE (x),
5790 XEXP (x, 0), pos),
5791 ((HOST_WIDE_INT) 1 << len) - 1);
5792 else
5793 /* Any other cases we can't handle. */
5794 return x;
5796 /* If we couldn't do this for some reason, return the original
5797 expression. */
5798 if (GET_CODE (tem) == CLOBBER)
5799 return x;
5801 return tem;
5804 /* X is a SET which contains an assignment of one object into
5805 a part of another (such as a bit-field assignment, STRICT_LOW_PART,
5806 or certain SUBREGS). If possible, convert it into a series of
5807 logical operations.
5809 We half-heartedly support variable positions, but do not at all
5810 support variable lengths. */
5812 static rtx
5813 expand_field_assignment (rtx x)
5815 rtx inner;
5816 rtx pos; /* Always counts from low bit. */
5817 int len;
5818 rtx mask;
5819 enum machine_mode compute_mode;
5821 /* Loop until we find something we can't simplify. */
5822 while (1)
5824 if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
5825 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
5827 inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
5828 len = GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)));
5829 pos = GEN_INT (subreg_lsb (XEXP (SET_DEST (x), 0)));
5831 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
5832 && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT)
5834 inner = XEXP (SET_DEST (x), 0);
5835 len = INTVAL (XEXP (SET_DEST (x), 1));
5836 pos = XEXP (SET_DEST (x), 2);
5838 /* If the position is constant and spans the width of INNER,
5839 surround INNER with a USE to indicate this. */
5840 if (GET_CODE (pos) == CONST_INT
5841 && INTVAL (pos) + len > GET_MODE_BITSIZE (GET_MODE (inner)))
5842 inner = gen_rtx_USE (GET_MODE (SET_DEST (x)), inner);
5844 if (BITS_BIG_ENDIAN)
5846 if (GET_CODE (pos) == CONST_INT)
5847 pos = GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner)) - len
5848 - INTVAL (pos));
5849 else if (GET_CODE (pos) == MINUS
5850 && GET_CODE (XEXP (pos, 1)) == CONST_INT
5851 && (INTVAL (XEXP (pos, 1))
5852 == GET_MODE_BITSIZE (GET_MODE (inner)) - len))
5853 /* If position is ADJUST - X, new position is X. */
5854 pos = XEXP (pos, 0);
5855 else
5856 pos = gen_binary (MINUS, GET_MODE (pos),
5857 GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner))
5858 - len),
5859 pos);
5863 /* A SUBREG between two modes that occupy the same numbers of words
5864 can be done by moving the SUBREG to the source. */
5865 else if (GET_CODE (SET_DEST (x)) == SUBREG
5866 /* We need SUBREGs to compute nonzero_bits properly. */
5867 && nonzero_sign_valid
5868 && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
5869 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
5870 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
5871 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
5873 x = gen_rtx_SET (VOIDmode, SUBREG_REG (SET_DEST (x)),
5874 gen_lowpart
5875 (GET_MODE (SUBREG_REG (SET_DEST (x))),
5876 SET_SRC (x)));
5877 continue;
5879 else
5880 break;
5882 while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
5883 inner = SUBREG_REG (inner);
5885 compute_mode = GET_MODE (inner);
5887 /* Don't attempt bitwise arithmetic on non scalar integer modes. */
5888 if (! SCALAR_INT_MODE_P (compute_mode))
5890 enum machine_mode imode;
5892 /* Don't do anything for vector or complex integral types. */
5893 if (! FLOAT_MODE_P (compute_mode))
5894 break;
5896 /* Try to find an integral mode to pun with. */
5897 imode = mode_for_size (GET_MODE_BITSIZE (compute_mode), MODE_INT, 0);
5898 if (imode == BLKmode)
5899 break;
5901 compute_mode = imode;
5902 inner = gen_lowpart (imode, inner);
5905 /* Compute a mask of LEN bits, if we can do this on the host machine. */
5906 if (len < HOST_BITS_PER_WIDE_INT)
5907 mask = GEN_INT (((HOST_WIDE_INT) 1 << len) - 1);
5908 else
5909 break;
5911 /* Now compute the equivalent expression. Make a copy of INNER
5912 for the SET_DEST in case it is a MEM into which we will substitute;
5913 we don't want shared RTL in that case. */
5914 x = gen_rtx_SET
5915 (VOIDmode, copy_rtx (inner),
5916 gen_binary (IOR, compute_mode,
5917 gen_binary (AND, compute_mode,
5918 simplify_gen_unary (NOT, compute_mode,
5919 gen_binary (ASHIFT,
5920 compute_mode,
5921 mask, pos),
5922 compute_mode),
5923 inner),
5924 gen_binary (ASHIFT, compute_mode,
5925 gen_binary (AND, compute_mode,
5926 gen_lowpart
5927 (compute_mode, SET_SRC (x)),
5928 mask),
5929 pos)));
5932 return x;
5935 /* Return an RTX for a reference to LEN bits of INNER. If POS_RTX is nonzero,
5936 it is an RTX that represents a variable starting position; otherwise,
5937 POS is the (constant) starting bit position (counted from the LSB).
5939 INNER may be a USE. This will occur when we started with a bitfield
5940 that went outside the boundary of the object in memory, which is
5941 allowed on most machines. To isolate this case, we produce a USE
5942 whose mode is wide enough and surround the MEM with it. The only
5943 code that understands the USE is this routine. If it is not removed,
5944 it will cause the resulting insn not to match.
5946 UNSIGNEDP is nonzero for an unsigned reference and zero for a
5947 signed reference.
5949 IN_DEST is nonzero if this is a reference in the destination of a
5950 SET. This is used when a ZERO_ or SIGN_EXTRACT isn't needed. If nonzero,
5951 a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
5952 be used.
5954 IN_COMPARE is nonzero if we are in a COMPARE. This means that a
5955 ZERO_EXTRACT should be built even for bits starting at bit 0.
5957 MODE is the desired mode of the result (if IN_DEST == 0).
5959 The result is an RTX for the extraction or NULL_RTX if the target
5960 can't handle it. */
5962 static rtx
5963 make_extraction (enum machine_mode mode, rtx inner, HOST_WIDE_INT pos,
5964 rtx pos_rtx, unsigned HOST_WIDE_INT len, int unsignedp,
5965 int in_dest, int in_compare)
5967 /* This mode describes the size of the storage area
5968 to fetch the overall value from. Within that, we
5969 ignore the POS lowest bits, etc. */
5970 enum machine_mode is_mode = GET_MODE (inner);
5971 enum machine_mode inner_mode;
5972 enum machine_mode wanted_inner_mode = byte_mode;
5973 enum machine_mode wanted_inner_reg_mode = word_mode;
5974 enum machine_mode pos_mode = word_mode;
5975 enum machine_mode extraction_mode = word_mode;
5976 enum machine_mode tmode = mode_for_size (len, MODE_INT, 1);
5977 int spans_byte = 0;
5978 rtx new = 0;
5979 rtx orig_pos_rtx = pos_rtx;
5980 HOST_WIDE_INT orig_pos;
5982 /* Get some information about INNER and get the innermost object. */
5983 if (GET_CODE (inner) == USE)
5984 /* (use:SI (mem:QI foo)) stands for (mem:SI foo). */
5985 /* We don't need to adjust the position because we set up the USE
5986 to pretend that it was a full-word object. */
5987 spans_byte = 1, inner = XEXP (inner, 0);
5988 else if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
5990 /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
5991 consider just the QI as the memory to extract from.
5992 The subreg adds or removes high bits; its mode is
5993 irrelevant to the meaning of this extraction,
5994 since POS and LEN count from the lsb. */
5995 if (GET_CODE (SUBREG_REG (inner)) == MEM)
5996 is_mode = GET_MODE (SUBREG_REG (inner));
5997 inner = SUBREG_REG (inner);
5999 else if (GET_CODE (inner) == ASHIFT
6000 && GET_CODE (XEXP (inner, 1)) == CONST_INT
6001 && pos_rtx == 0 && pos == 0
6002 && len > (unsigned HOST_WIDE_INT) INTVAL (XEXP (inner, 1)))
6004 /* We're extracting the least significant bits of an rtx
6005 (ashift X (const_int C)), where LEN > C. Extract the
6006 least significant (LEN - C) bits of X, giving an rtx
6007 whose mode is MODE, then shift it left C times. */
6008 new = make_extraction (mode, XEXP (inner, 0),
6009 0, 0, len - INTVAL (XEXP (inner, 1)),
6010 unsignedp, in_dest, in_compare);
6011 if (new != 0)
6012 return gen_rtx_ASHIFT (mode, new, XEXP (inner, 1));
6015 inner_mode = GET_MODE (inner);
6017 if (pos_rtx && GET_CODE (pos_rtx) == CONST_INT)
6018 pos = INTVAL (pos_rtx), pos_rtx = 0;
6020 /* See if this can be done without an extraction. We never can if the
6021 width of the field is not the same as that of some integer mode. For
6022 registers, we can only avoid the extraction if the position is at the
6023 low-order bit and this is either not in the destination or we have the
6024 appropriate STRICT_LOW_PART operation available.
6026 For MEM, we can avoid an extract if the field starts on an appropriate
6027 boundary and we can change the mode of the memory reference. However,
6028 we cannot directly access the MEM if we have a USE and the underlying
6029 MEM is not TMODE. This combination means that MEM was being used in a
6030 context where bits outside its mode were being referenced; that is only
6031 valid in bit-field insns. */
6033 if (tmode != BLKmode
6034 && ! (spans_byte && inner_mode != tmode)
6035 && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
6036 && GET_CODE (inner) != MEM
6037 && (! in_dest
6038 || (GET_CODE (inner) == REG
6039 && have_insn_for (STRICT_LOW_PART, tmode))))
6040 || (GET_CODE (inner) == MEM && pos_rtx == 0
6041 && (pos
6042 % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
6043 : BITS_PER_UNIT)) == 0
6044 /* We can't do this if we are widening INNER_MODE (it
6045 may not be aligned, for one thing). */
6046 && GET_MODE_BITSIZE (inner_mode) >= GET_MODE_BITSIZE (tmode)
6047 && (inner_mode == tmode
6048 || (! mode_dependent_address_p (XEXP (inner, 0))
6049 && ! MEM_VOLATILE_P (inner))))))
6051 /* If INNER is a MEM, make a new MEM that encompasses just the desired
6052 field. If the original and current mode are the same, we need not
6053 adjust the offset. Otherwise, we do if bytes big endian.
6055 If INNER is not a MEM, get a piece consisting of just the field
6056 of interest (in this case POS % BITS_PER_WORD must be 0). */
6058 if (GET_CODE (inner) == MEM)
6060 HOST_WIDE_INT offset;
6062 /* POS counts from lsb, but make OFFSET count in memory order. */
6063 if (BYTES_BIG_ENDIAN)
6064 offset = (GET_MODE_BITSIZE (is_mode) - len - pos) / BITS_PER_UNIT;
6065 else
6066 offset = pos / BITS_PER_UNIT;
6068 new = adjust_address_nv (inner, tmode, offset);
6070 else if (GET_CODE (inner) == REG)
6072 if (tmode != inner_mode)
6074 /* We can't call gen_lowpart in a DEST since we
6075 always want a SUBREG (see below) and it would sometimes
6076 return a new hard register. */
6077 if (pos || in_dest)
6079 HOST_WIDE_INT final_word = pos / BITS_PER_WORD;
6081 if (WORDS_BIG_ENDIAN
6082 && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
6083 final_word = ((GET_MODE_SIZE (inner_mode)
6084 - GET_MODE_SIZE (tmode))
6085 / UNITS_PER_WORD) - final_word;
6087 final_word *= UNITS_PER_WORD;
6088 if (BYTES_BIG_ENDIAN &&
6089 GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (tmode))
6090 final_word += (GET_MODE_SIZE (inner_mode)
6091 - GET_MODE_SIZE (tmode)) % UNITS_PER_WORD;
6093 /* Avoid creating invalid subregs, for example when
6094 simplifying (x>>32)&255. */
6095 if (final_word >= GET_MODE_SIZE (inner_mode))
6096 return NULL_RTX;
6098 new = gen_rtx_SUBREG (tmode, inner, final_word);
6100 else
6101 new = gen_lowpart (tmode, inner);
6103 else
6104 new = inner;
6106 else
6107 new = force_to_mode (inner, tmode,
6108 len >= HOST_BITS_PER_WIDE_INT
6109 ? ~(unsigned HOST_WIDE_INT) 0
6110 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
6111 NULL_RTX, 0);
6113 /* If this extraction is going into the destination of a SET,
6114 make a STRICT_LOW_PART unless we made a MEM. */
6116 if (in_dest)
6117 return (GET_CODE (new) == MEM ? new
6118 : (GET_CODE (new) != SUBREG
6119 ? gen_rtx_CLOBBER (tmode, const0_rtx)
6120 : gen_rtx_STRICT_LOW_PART (VOIDmode, new)));
6122 if (mode == tmode)
6123 return new;
6125 if (GET_CODE (new) == CONST_INT)
6126 return gen_int_mode (INTVAL (new), mode);
6128 /* If we know that no extraneous bits are set, and that the high
6129 bit is not set, convert the extraction to the cheaper of
6130 sign and zero extension, that are equivalent in these cases. */
6131 if (flag_expensive_optimizations
6132 && (GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
6133 && ((nonzero_bits (new, tmode)
6134 & ~(((unsigned HOST_WIDE_INT)
6135 GET_MODE_MASK (tmode))
6136 >> 1))
6137 == 0)))
6139 rtx temp = gen_rtx_ZERO_EXTEND (mode, new);
6140 rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new);
6142 /* Prefer ZERO_EXTENSION, since it gives more information to
6143 backends. */
6144 if (rtx_cost (temp, SET) <= rtx_cost (temp1, SET))
6145 return temp;
6146 return temp1;
6149 /* Otherwise, sign- or zero-extend unless we already are in the
6150 proper mode. */
6152 return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
6153 mode, new));
6156 /* Unless this is a COMPARE or we have a funny memory reference,
6157 don't do anything with zero-extending field extracts starting at
6158 the low-order bit since they are simple AND operations. */
6159 if (pos_rtx == 0 && pos == 0 && ! in_dest
6160 && ! in_compare && ! spans_byte && unsignedp)
6161 return 0;
6163 /* Unless we are allowed to span bytes or INNER is not MEM, reject this if
6164 we would be spanning bytes or if the position is not a constant and the
6165 length is not 1. In all other cases, we would only be going outside
6166 our object in cases when an original shift would have been
6167 undefined. */
6168 if (! spans_byte && GET_CODE (inner) == MEM
6169 && ((pos_rtx == 0 && pos + len > GET_MODE_BITSIZE (is_mode))
6170 || (pos_rtx != 0 && len != 1)))
6171 return 0;
6173 /* Get the mode to use should INNER not be a MEM, the mode for the position,
6174 and the mode for the result. */
6175 if (in_dest && mode_for_extraction (EP_insv, -1) != MAX_MACHINE_MODE)
6177 wanted_inner_reg_mode = mode_for_extraction (EP_insv, 0);
6178 pos_mode = mode_for_extraction (EP_insv, 2);
6179 extraction_mode = mode_for_extraction (EP_insv, 3);
6182 if (! in_dest && unsignedp
6183 && mode_for_extraction (EP_extzv, -1) != MAX_MACHINE_MODE)
6185 wanted_inner_reg_mode = mode_for_extraction (EP_extzv, 1);
6186 pos_mode = mode_for_extraction (EP_extzv, 3);
6187 extraction_mode = mode_for_extraction (EP_extzv, 0);
6190 if (! in_dest && ! unsignedp
6191 && mode_for_extraction (EP_extv, -1) != MAX_MACHINE_MODE)
6193 wanted_inner_reg_mode = mode_for_extraction (EP_extv, 1);
6194 pos_mode = mode_for_extraction (EP_extv, 3);
6195 extraction_mode = mode_for_extraction (EP_extv, 0);
6198 /* Never narrow an object, since that might not be safe. */
6200 if (mode != VOIDmode
6201 && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
6202 extraction_mode = mode;
6204 if (pos_rtx && GET_MODE (pos_rtx) != VOIDmode
6205 && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
6206 pos_mode = GET_MODE (pos_rtx);
6208 /* If this is not from memory, the desired mode is wanted_inner_reg_mode;
6209 if we have to change the mode of memory and cannot, the desired mode is
6210 EXTRACTION_MODE. */
6211 if (GET_CODE (inner) != MEM)
6212 wanted_inner_mode = wanted_inner_reg_mode;
6213 else if (inner_mode != wanted_inner_mode
6214 && (mode_dependent_address_p (XEXP (inner, 0))
6215 || MEM_VOLATILE_P (inner)))
6216 wanted_inner_mode = extraction_mode;
6218 orig_pos = pos;
6220 if (BITS_BIG_ENDIAN)
6222 /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
6223 BITS_BIG_ENDIAN style. If position is constant, compute new
6224 position. Otherwise, build subtraction.
6225 Note that POS is relative to the mode of the original argument.
6226 If it's a MEM we need to recompute POS relative to that.
6227 However, if we're extracting from (or inserting into) a register,
6228 we want to recompute POS relative to wanted_inner_mode. */
6229 int width = (GET_CODE (inner) == MEM
6230 ? GET_MODE_BITSIZE (is_mode)
6231 : GET_MODE_BITSIZE (wanted_inner_mode));
6233 if (pos_rtx == 0)
6234 pos = width - len - pos;
6235 else
6236 pos_rtx
6237 = gen_rtx_MINUS (GET_MODE (pos_rtx), GEN_INT (width - len), pos_rtx);
6238 /* POS may be less than 0 now, but we check for that below.
6239 Note that it can only be less than 0 if GET_CODE (inner) != MEM. */
6242 /* If INNER has a wider mode, make it smaller. If this is a constant
6243 extract, try to adjust the byte to point to the byte containing
6244 the value. */
6245 if (wanted_inner_mode != VOIDmode
6246 && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
6247 && ((GET_CODE (inner) == MEM
6248 && (inner_mode == wanted_inner_mode
6249 || (! mode_dependent_address_p (XEXP (inner, 0))
6250 && ! MEM_VOLATILE_P (inner))))))
6252 int offset = 0;
6254 /* The computations below will be correct if the machine is big
6255 endian in both bits and bytes or little endian in bits and bytes.
6256 If it is mixed, we must adjust. */
6258 /* If bytes are big endian and we had a paradoxical SUBREG, we must
6259 adjust OFFSET to compensate. */
6260 if (BYTES_BIG_ENDIAN
6261 && ! spans_byte
6262 && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
6263 offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
6265 /* If this is a constant position, we can move to the desired byte. */
6266 if (pos_rtx == 0)
6268 offset += pos / BITS_PER_UNIT;
6269 pos %= GET_MODE_BITSIZE (wanted_inner_mode);
6272 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
6273 && ! spans_byte
6274 && is_mode != wanted_inner_mode)
6275 offset = (GET_MODE_SIZE (is_mode)
6276 - GET_MODE_SIZE (wanted_inner_mode) - offset);
6278 if (offset != 0 || inner_mode != wanted_inner_mode)
6279 inner = adjust_address_nv (inner, wanted_inner_mode, offset);
6282 /* If INNER is not memory, we can always get it into the proper mode. If we
6283 are changing its mode, POS must be a constant and smaller than the size
6284 of the new mode. */
6285 else if (GET_CODE (inner) != MEM)
6287 if (GET_MODE (inner) != wanted_inner_mode
6288 && (pos_rtx != 0
6289 || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
6290 return 0;
6292 inner = force_to_mode (inner, wanted_inner_mode,
6293 pos_rtx
6294 || len + orig_pos >= HOST_BITS_PER_WIDE_INT
6295 ? ~(unsigned HOST_WIDE_INT) 0
6296 : ((((unsigned HOST_WIDE_INT) 1 << len) - 1)
6297 << orig_pos),
6298 NULL_RTX, 0);
6301 /* Adjust mode of POS_RTX, if needed. If we want a wider mode, we
6302 have to zero extend. Otherwise, we can just use a SUBREG. */
6303 if (pos_rtx != 0
6304 && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
6306 rtx temp = gen_rtx_ZERO_EXTEND (pos_mode, pos_rtx);
6308 /* If we know that no extraneous bits are set, and that the high
6309 bit is not set, convert extraction to cheaper one - either
6310 SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
6311 cases. */
6312 if (flag_expensive_optimizations
6313 && (GET_MODE_BITSIZE (GET_MODE (pos_rtx)) <= HOST_BITS_PER_WIDE_INT
6314 && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
6315 & ~(((unsigned HOST_WIDE_INT)
6316 GET_MODE_MASK (GET_MODE (pos_rtx)))
6317 >> 1))
6318 == 0)))
6320 rtx temp1 = gen_rtx_SIGN_EXTEND (pos_mode, pos_rtx);
6322 /* Prefer ZERO_EXTENSION, since it gives more information to
6323 backends. */
6324 if (rtx_cost (temp1, SET) < rtx_cost (temp, SET))
6325 temp = temp1;
6327 pos_rtx = temp;
6329 else if (pos_rtx != 0
6330 && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
6331 pos_rtx = gen_lowpart (pos_mode, pos_rtx);
6333 /* Make POS_RTX unless we already have it and it is correct. If we don't
6334 have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
6335 be a CONST_INT. */
6336 if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
6337 pos_rtx = orig_pos_rtx;
6339 else if (pos_rtx == 0)
6340 pos_rtx = GEN_INT (pos);
6342 /* Make the required operation. See if we can use existing rtx. */
6343 new = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
6344 extraction_mode, inner, GEN_INT (len), pos_rtx);
6345 if (! in_dest)
6346 new = gen_lowpart (mode, new);
6348 return new;
6351 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
6352 with any other operations in X. Return X without that shift if so. */
6354 static rtx
6355 extract_left_shift (rtx x, int count)
6357 enum rtx_code code = GET_CODE (x);
6358 enum machine_mode mode = GET_MODE (x);
6359 rtx tem;
6361 switch (code)
6363 case ASHIFT:
6364 /* This is the shift itself. If it is wide enough, we will return
6365 either the value being shifted if the shift count is equal to
6366 COUNT or a shift for the difference. */
6367 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6368 && INTVAL (XEXP (x, 1)) >= count)
6369 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
6370 INTVAL (XEXP (x, 1)) - count);
6371 break;
6373 case NEG: case NOT:
6374 if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
6375 return simplify_gen_unary (code, mode, tem, mode);
6377 break;
6379 case PLUS: case IOR: case XOR: case AND:
6380 /* If we can safely shift this constant and we find the inner shift,
6381 make a new operation. */
6382 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6383 && (INTVAL (XEXP (x, 1)) & ((((HOST_WIDE_INT) 1 << count)) - 1)) == 0
6384 && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
6385 return gen_binary (code, mode, tem,
6386 GEN_INT (INTVAL (XEXP (x, 1)) >> count));
6388 break;
6390 default:
6391 break;
6394 return 0;
6397 /* Look at the expression rooted at X. Look for expressions
6398 equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
6399 Form these expressions.
6401 Return the new rtx, usually just X.
6403 Also, for machines like the VAX that don't have logical shift insns,
6404 try to convert logical to arithmetic shift operations in cases where
6405 they are equivalent. This undoes the canonicalizations to logical
6406 shifts done elsewhere.
6408 We try, as much as possible, to re-use rtl expressions to save memory.
6410 IN_CODE says what kind of expression we are processing. Normally, it is
6411 SET. In a memory address (inside a MEM, PLUS or minus, the latter two
6412 being kludges), it is MEM. When processing the arguments of a comparison
6413 or a COMPARE against zero, it is COMPARE. */
6415 static rtx
6416 make_compound_operation (rtx x, enum rtx_code in_code)
6418 enum rtx_code code = GET_CODE (x);
6419 enum machine_mode mode = GET_MODE (x);
6420 int mode_width = GET_MODE_BITSIZE (mode);
6421 rtx rhs, lhs;
6422 enum rtx_code next_code;
6423 int i;
6424 rtx new = 0;
6425 rtx tem;
6426 const char *fmt;
6428 /* Select the code to be used in recursive calls. Once we are inside an
6429 address, we stay there. If we have a comparison, set to COMPARE,
6430 but once inside, go back to our default of SET. */
6432 next_code = (code == MEM || code == PLUS || code == MINUS ? MEM
6433 : ((code == COMPARE || COMPARISON_P (x))
6434 && XEXP (x, 1) == const0_rtx) ? COMPARE
6435 : in_code == COMPARE ? SET : in_code);
6437 /* Process depending on the code of this operation. If NEW is set
6438 nonzero, it will be returned. */
6440 switch (code)
6442 case ASHIFT:
6443 /* Convert shifts by constants into multiplications if inside
6444 an address. */
6445 if (in_code == MEM && GET_CODE (XEXP (x, 1)) == CONST_INT
6446 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
6447 && INTVAL (XEXP (x, 1)) >= 0)
6449 new = make_compound_operation (XEXP (x, 0), next_code);
6450 new = gen_rtx_MULT (mode, new,
6451 GEN_INT ((HOST_WIDE_INT) 1
6452 << INTVAL (XEXP (x, 1))));
6454 break;
6456 case AND:
6457 /* If the second operand is not a constant, we can't do anything
6458 with it. */
6459 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
6460 break;
6462 /* If the constant is a power of two minus one and the first operand
6463 is a logical right shift, make an extraction. */
6464 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6465 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6467 new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
6468 new = make_extraction (mode, new, 0, XEXP (XEXP (x, 0), 1), i, 1,
6469 0, in_code == COMPARE);
6472 /* Same as previous, but for (subreg (lshiftrt ...)) in first op. */
6473 else if (GET_CODE (XEXP (x, 0)) == SUBREG
6474 && subreg_lowpart_p (XEXP (x, 0))
6475 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
6476 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6478 new = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
6479 next_code);
6480 new = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new, 0,
6481 XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
6482 0, in_code == COMPARE);
6484 /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)). */
6485 else if ((GET_CODE (XEXP (x, 0)) == XOR
6486 || GET_CODE (XEXP (x, 0)) == IOR)
6487 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
6488 && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
6489 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6491 /* Apply the distributive law, and then try to make extractions. */
6492 new = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
6493 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
6494 XEXP (x, 1)),
6495 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
6496 XEXP (x, 1)));
6497 new = make_compound_operation (new, in_code);
6500 /* If we are have (and (rotate X C) M) and C is larger than the number
6501 of bits in M, this is an extraction. */
6503 else if (GET_CODE (XEXP (x, 0)) == ROTATE
6504 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6505 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0
6506 && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
6508 new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
6509 new = make_extraction (mode, new,
6510 (GET_MODE_BITSIZE (mode)
6511 - INTVAL (XEXP (XEXP (x, 0), 1))),
6512 NULL_RTX, i, 1, 0, in_code == COMPARE);
6515 /* On machines without logical shifts, if the operand of the AND is
6516 a logical shift and our mask turns off all the propagated sign
6517 bits, we can replace the logical shift with an arithmetic shift. */
6518 else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6519 && !have_insn_for (LSHIFTRT, mode)
6520 && have_insn_for (ASHIFTRT, mode)
6521 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6522 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
6523 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
6524 && mode_width <= HOST_BITS_PER_WIDE_INT)
6526 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
6528 mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
6529 if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
6530 SUBST (XEXP (x, 0),
6531 gen_rtx_ASHIFTRT (mode,
6532 make_compound_operation
6533 (XEXP (XEXP (x, 0), 0), next_code),
6534 XEXP (XEXP (x, 0), 1)));
6537 /* If the constant is one less than a power of two, this might be
6538 representable by an extraction even if no shift is present.
6539 If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
6540 we are in a COMPARE. */
6541 else if ((i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6542 new = make_extraction (mode,
6543 make_compound_operation (XEXP (x, 0),
6544 next_code),
6545 0, NULL_RTX, i, 1, 0, in_code == COMPARE);
6547 /* If we are in a comparison and this is an AND with a power of two,
6548 convert this into the appropriate bit extract. */
6549 else if (in_code == COMPARE
6550 && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
6551 new = make_extraction (mode,
6552 make_compound_operation (XEXP (x, 0),
6553 next_code),
6554 i, NULL_RTX, 1, 1, 0, 1);
6556 break;
6558 case LSHIFTRT:
6559 /* If the sign bit is known to be zero, replace this with an
6560 arithmetic shift. */
6561 if (have_insn_for (ASHIFTRT, mode)
6562 && ! have_insn_for (LSHIFTRT, mode)
6563 && mode_width <= HOST_BITS_PER_WIDE_INT
6564 && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
6566 new = gen_rtx_ASHIFTRT (mode,
6567 make_compound_operation (XEXP (x, 0),
6568 next_code),
6569 XEXP (x, 1));
6570 break;
6573 /* ... fall through ... */
6575 case ASHIFTRT:
6576 lhs = XEXP (x, 0);
6577 rhs = XEXP (x, 1);
6579 /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
6580 this is a SIGN_EXTRACT. */
6581 if (GET_CODE (rhs) == CONST_INT
6582 && GET_CODE (lhs) == ASHIFT
6583 && GET_CODE (XEXP (lhs, 1)) == CONST_INT
6584 && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1)))
6586 new = make_compound_operation (XEXP (lhs, 0), next_code);
6587 new = make_extraction (mode, new,
6588 INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
6589 NULL_RTX, mode_width - INTVAL (rhs),
6590 code == LSHIFTRT, 0, in_code == COMPARE);
6591 break;
6594 /* See if we have operations between an ASHIFTRT and an ASHIFT.
6595 If so, try to merge the shifts into a SIGN_EXTEND. We could
6596 also do this for some cases of SIGN_EXTRACT, but it doesn't
6597 seem worth the effort; the case checked for occurs on Alpha. */
6599 if (!OBJECT_P (lhs)
6600 && ! (GET_CODE (lhs) == SUBREG
6601 && (OBJECT_P (SUBREG_REG (lhs))))
6602 && GET_CODE (rhs) == CONST_INT
6603 && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
6604 && (new = extract_left_shift (lhs, INTVAL (rhs))) != 0)
6605 new = make_extraction (mode, make_compound_operation (new, next_code),
6606 0, NULL_RTX, mode_width - INTVAL (rhs),
6607 code == LSHIFTRT, 0, in_code == COMPARE);
6609 break;
6611 case SUBREG:
6612 /* Call ourselves recursively on the inner expression. If we are
6613 narrowing the object and it has a different RTL code from
6614 what it originally did, do this SUBREG as a force_to_mode. */
6616 tem = make_compound_operation (SUBREG_REG (x), in_code);
6617 if (GET_CODE (tem) != GET_CODE (SUBREG_REG (x))
6618 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (tem))
6619 && subreg_lowpart_p (x))
6621 rtx newer = force_to_mode (tem, mode, ~(HOST_WIDE_INT) 0,
6622 NULL_RTX, 0);
6624 /* If we have something other than a SUBREG, we might have
6625 done an expansion, so rerun ourselves. */
6626 if (GET_CODE (newer) != SUBREG)
6627 newer = make_compound_operation (newer, in_code);
6629 return newer;
6632 /* If this is a paradoxical subreg, and the new code is a sign or
6633 zero extension, omit the subreg and widen the extension. If it
6634 is a regular subreg, we can still get rid of the subreg by not
6635 widening so much, or in fact removing the extension entirely. */
6636 if ((GET_CODE (tem) == SIGN_EXTEND
6637 || GET_CODE (tem) == ZERO_EXTEND)
6638 && subreg_lowpart_p (x))
6640 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (tem))
6641 || (GET_MODE_SIZE (mode) >
6642 GET_MODE_SIZE (GET_MODE (XEXP (tem, 0)))))
6644 if (! SCALAR_INT_MODE_P (mode))
6645 break;
6646 tem = gen_rtx_fmt_e (GET_CODE (tem), mode, XEXP (tem, 0));
6648 else
6649 tem = gen_lowpart (mode, XEXP (tem, 0));
6650 return tem;
6652 break;
6654 default:
6655 break;
6658 if (new)
6660 x = gen_lowpart (mode, new);
6661 code = GET_CODE (x);
6664 /* Now recursively process each operand of this operation. */
6665 fmt = GET_RTX_FORMAT (code);
6666 for (i = 0; i < GET_RTX_LENGTH (code); i++)
6667 if (fmt[i] == 'e')
6669 new = make_compound_operation (XEXP (x, i), next_code);
6670 SUBST (XEXP (x, i), new);
6673 return x;
6676 /* Given M see if it is a value that would select a field of bits
6677 within an item, but not the entire word. Return -1 if not.
6678 Otherwise, return the starting position of the field, where 0 is the
6679 low-order bit.
6681 *PLEN is set to the length of the field. */
6683 static int
6684 get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
6686 /* Get the bit number of the first 1 bit from the right, -1 if none. */
6687 int pos = exact_log2 (m & -m);
6688 int len;
6690 if (pos < 0)
6691 return -1;
6693 /* Now shift off the low-order zero bits and see if we have a power of
6694 two minus 1. */
6695 len = exact_log2 ((m >> pos) + 1);
6697 if (len <= 0)
6698 return -1;
6700 *plen = len;
6701 return pos;
6704 /* See if X can be simplified knowing that we will only refer to it in
6705 MODE and will only refer to those bits that are nonzero in MASK.
6706 If other bits are being computed or if masking operations are done
6707 that select a superset of the bits in MASK, they can sometimes be
6708 ignored.
6710 Return a possibly simplified expression, but always convert X to
6711 MODE. If X is a CONST_INT, AND the CONST_INT with MASK.
6713 Also, if REG is nonzero and X is a register equal in value to REG,
6714 replace X with REG.
6716 If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
6717 are all off in X. This is used when X will be complemented, by either
6718 NOT, NEG, or XOR. */
6720 static rtx
6721 force_to_mode (rtx x, enum machine_mode mode, unsigned HOST_WIDE_INT mask,
6722 rtx reg, int just_select)
6724 enum rtx_code code = GET_CODE (x);
6725 int next_select = just_select || code == XOR || code == NOT || code == NEG;
6726 enum machine_mode op_mode;
6727 unsigned HOST_WIDE_INT fuller_mask, nonzero;
6728 rtx op0, op1, temp;
6730 /* If this is a CALL or ASM_OPERANDS, don't do anything. Some of the
6731 code below will do the wrong thing since the mode of such an
6732 expression is VOIDmode.
6734 Also do nothing if X is a CLOBBER; this can happen if X was
6735 the return value from a call to gen_lowpart. */
6736 if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
6737 return x;
6739 /* We want to perform the operation is its present mode unless we know
6740 that the operation is valid in MODE, in which case we do the operation
6741 in MODE. */
6742 op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
6743 && have_insn_for (code, mode))
6744 ? mode : GET_MODE (x));
6746 /* It is not valid to do a right-shift in a narrower mode
6747 than the one it came in with. */
6748 if ((code == LSHIFTRT || code == ASHIFTRT)
6749 && GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (GET_MODE (x)))
6750 op_mode = GET_MODE (x);
6752 /* Truncate MASK to fit OP_MODE. */
6753 if (op_mode)
6754 mask &= GET_MODE_MASK (op_mode);
6756 /* When we have an arithmetic operation, or a shift whose count we
6757 do not know, we need to assume that all bits up to the highest-order
6758 bit in MASK will be needed. This is how we form such a mask. */
6759 if (mask & ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
6760 fuller_mask = ~(unsigned HOST_WIDE_INT) 0;
6761 else
6762 fuller_mask = (((unsigned HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1))
6763 - 1);
6765 /* Determine what bits of X are guaranteed to be (non)zero. */
6766 nonzero = nonzero_bits (x, mode);
6768 /* If none of the bits in X are needed, return a zero. */
6769 if (! just_select && (nonzero & mask) == 0)
6770 x = const0_rtx;
6772 /* If X is a CONST_INT, return a new one. Do this here since the
6773 test below will fail. */
6774 if (GET_CODE (x) == CONST_INT)
6776 if (SCALAR_INT_MODE_P (mode))
6777 return gen_int_mode (INTVAL (x) & mask, mode);
6778 else
6780 x = GEN_INT (INTVAL (x) & mask);
6781 return gen_lowpart_common (mode, x);
6785 /* If X is narrower than MODE and we want all the bits in X's mode, just
6786 get X in the proper mode. */
6787 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
6788 && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
6789 return gen_lowpart (mode, x);
6791 /* If we aren't changing the mode, X is not a SUBREG, and all zero bits in
6792 MASK are already known to be zero in X, we need not do anything. */
6793 if (GET_MODE (x) == mode && code != SUBREG && (~mask & nonzero) == 0)
6794 return x;
6796 switch (code)
6798 case CLOBBER:
6799 /* If X is a (clobber (const_int)), return it since we know we are
6800 generating something that won't match. */
6801 return x;
6803 case USE:
6804 /* X is a (use (mem ..)) that was made from a bit-field extraction that
6805 spanned the boundary of the MEM. If we are now masking so it is
6806 within that boundary, we don't need the USE any more. */
6807 if (! BITS_BIG_ENDIAN
6808 && (mask & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6809 return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
6810 break;
6812 case SIGN_EXTEND:
6813 case ZERO_EXTEND:
6814 case ZERO_EXTRACT:
6815 case SIGN_EXTRACT:
6816 x = expand_compound_operation (x);
6817 if (GET_CODE (x) != code)
6818 return force_to_mode (x, mode, mask, reg, next_select);
6819 break;
6821 case REG:
6822 if (reg != 0 && (rtx_equal_p (get_last_value (reg), x)
6823 || rtx_equal_p (reg, get_last_value (x))))
6824 x = reg;
6825 break;
6827 case SUBREG:
6828 if (subreg_lowpart_p (x)
6829 /* We can ignore the effect of this SUBREG if it narrows the mode or
6830 if the constant masks to zero all the bits the mode doesn't
6831 have. */
6832 && ((GET_MODE_SIZE (GET_MODE (x))
6833 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
6834 || (0 == (mask
6835 & GET_MODE_MASK (GET_MODE (x))
6836 & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
6837 return force_to_mode (SUBREG_REG (x), mode, mask, reg, next_select);
6838 break;
6840 case AND:
6841 /* If this is an AND with a constant, convert it into an AND
6842 whose constant is the AND of that constant with MASK. If it
6843 remains an AND of MASK, delete it since it is redundant. */
6845 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
6847 x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
6848 mask & INTVAL (XEXP (x, 1)));
6850 /* If X is still an AND, see if it is an AND with a mask that
6851 is just some low-order bits. If so, and it is MASK, we don't
6852 need it. */
6854 if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
6855 && ((INTVAL (XEXP (x, 1)) & GET_MODE_MASK (GET_MODE (x)))
6856 == mask))
6857 x = XEXP (x, 0);
6859 /* If it remains an AND, try making another AND with the bits
6860 in the mode mask that aren't in MASK turned on. If the
6861 constant in the AND is wide enough, this might make a
6862 cheaper constant. */
6864 if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
6865 && GET_MODE_MASK (GET_MODE (x)) != mask
6866 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
6868 HOST_WIDE_INT cval = (INTVAL (XEXP (x, 1))
6869 | (GET_MODE_MASK (GET_MODE (x)) & ~mask));
6870 int width = GET_MODE_BITSIZE (GET_MODE (x));
6871 rtx y;
6873 /* If MODE is narrower than HOST_WIDE_INT and CVAL is a negative
6874 number, sign extend it. */
6875 if (width > 0 && width < HOST_BITS_PER_WIDE_INT
6876 && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
6877 cval |= (HOST_WIDE_INT) -1 << width;
6879 y = gen_binary (AND, GET_MODE (x), XEXP (x, 0), GEN_INT (cval));
6880 if (rtx_cost (y, SET) < rtx_cost (x, SET))
6881 x = y;
6884 break;
6887 goto binop;
6889 case PLUS:
6890 /* In (and (plus FOO C1) M), if M is a mask that just turns off
6891 low-order bits (as in an alignment operation) and FOO is already
6892 aligned to that boundary, mask C1 to that boundary as well.
6893 This may eliminate that PLUS and, later, the AND. */
6896 unsigned int width = GET_MODE_BITSIZE (mode);
6897 unsigned HOST_WIDE_INT smask = mask;
6899 /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
6900 number, sign extend it. */
6902 if (width < HOST_BITS_PER_WIDE_INT
6903 && (smask & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
6904 smask |= (HOST_WIDE_INT) -1 << width;
6906 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6907 && exact_log2 (- smask) >= 0
6908 && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
6909 && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
6910 return force_to_mode (plus_constant (XEXP (x, 0),
6911 (INTVAL (XEXP (x, 1)) & smask)),
6912 mode, smask, reg, next_select);
6915 /* ... fall through ... */
6917 case MULT:
6918 /* For PLUS, MINUS and MULT, we need any bits less significant than the
6919 most significant bit in MASK since carries from those bits will
6920 affect the bits we are interested in. */
6921 mask = fuller_mask;
6922 goto binop;
6924 case MINUS:
6925 /* If X is (minus C Y) where C's least set bit is larger than any bit
6926 in the mask, then we may replace with (neg Y). */
6927 if (GET_CODE (XEXP (x, 0)) == CONST_INT
6928 && (((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 0))
6929 & -INTVAL (XEXP (x, 0))))
6930 > mask))
6932 x = simplify_gen_unary (NEG, GET_MODE (x), XEXP (x, 1),
6933 GET_MODE (x));
6934 return force_to_mode (x, mode, mask, reg, next_select);
6937 /* Similarly, if C contains every bit in the fuller_mask, then we may
6938 replace with (not Y). */
6939 if (GET_CODE (XEXP (x, 0)) == CONST_INT
6940 && ((INTVAL (XEXP (x, 0)) | (HOST_WIDE_INT) fuller_mask)
6941 == INTVAL (XEXP (x, 0))))
6943 x = simplify_gen_unary (NOT, GET_MODE (x),
6944 XEXP (x, 1), GET_MODE (x));
6945 return force_to_mode (x, mode, mask, reg, next_select);
6948 mask = fuller_mask;
6949 goto binop;
6951 case IOR:
6952 case XOR:
6953 /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
6954 LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
6955 operation which may be a bitfield extraction. Ensure that the
6956 constant we form is not wider than the mode of X. */
6958 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6959 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6960 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
6961 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
6962 && GET_CODE (XEXP (x, 1)) == CONST_INT
6963 && ((INTVAL (XEXP (XEXP (x, 0), 1))
6964 + floor_log2 (INTVAL (XEXP (x, 1))))
6965 < GET_MODE_BITSIZE (GET_MODE (x)))
6966 && (INTVAL (XEXP (x, 1))
6967 & ~nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
6969 temp = GEN_INT ((INTVAL (XEXP (x, 1)) & mask)
6970 << INTVAL (XEXP (XEXP (x, 0), 1)));
6971 temp = gen_binary (GET_CODE (x), GET_MODE (x),
6972 XEXP (XEXP (x, 0), 0), temp);
6973 x = gen_binary (LSHIFTRT, GET_MODE (x), temp,
6974 XEXP (XEXP (x, 0), 1));
6975 return force_to_mode (x, mode, mask, reg, next_select);
6978 binop:
6979 /* For most binary operations, just propagate into the operation and
6980 change the mode if we have an operation of that mode. */
6982 op0 = gen_lowpart (op_mode,
6983 force_to_mode (XEXP (x, 0), mode, mask,
6984 reg, next_select));
6985 op1 = gen_lowpart (op_mode,
6986 force_to_mode (XEXP (x, 1), mode, mask,
6987 reg, next_select));
6989 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
6990 x = gen_binary (code, op_mode, op0, op1);
6991 break;
6993 case ASHIFT:
6994 /* For left shifts, do the same, but just for the first operand.
6995 However, we cannot do anything with shifts where we cannot
6996 guarantee that the counts are smaller than the size of the mode
6997 because such a count will have a different meaning in a
6998 wider mode. */
7000 if (! (GET_CODE (XEXP (x, 1)) == CONST_INT
7001 && INTVAL (XEXP (x, 1)) >= 0
7002 && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (mode))
7003 && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
7004 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
7005 < (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (mode))))
7006 break;
7008 /* If the shift count is a constant and we can do arithmetic in
7009 the mode of the shift, refine which bits we need. Otherwise, use the
7010 conservative form of the mask. */
7011 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7012 && INTVAL (XEXP (x, 1)) >= 0
7013 && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (op_mode)
7014 && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
7015 mask >>= INTVAL (XEXP (x, 1));
7016 else
7017 mask = fuller_mask;
7019 op0 = gen_lowpart (op_mode,
7020 force_to_mode (XEXP (x, 0), op_mode,
7021 mask, reg, next_select));
7023 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
7024 x = gen_binary (code, op_mode, op0, XEXP (x, 1));
7025 break;
7027 case LSHIFTRT:
7028 /* Here we can only do something if the shift count is a constant,
7029 this shift constant is valid for the host, and we can do arithmetic
7030 in OP_MODE. */
7032 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7033 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
7034 && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
7036 rtx inner = XEXP (x, 0);
7037 unsigned HOST_WIDE_INT inner_mask;
7039 /* Select the mask of the bits we need for the shift operand. */
7040 inner_mask = mask << INTVAL (XEXP (x, 1));
7042 /* We can only change the mode of the shift if we can do arithmetic
7043 in the mode of the shift and INNER_MASK is no wider than the
7044 width of OP_MODE. */
7045 if (GET_MODE_BITSIZE (op_mode) > HOST_BITS_PER_WIDE_INT
7046 || (inner_mask & ~GET_MODE_MASK (op_mode)) != 0)
7047 op_mode = GET_MODE (x);
7049 inner = force_to_mode (inner, op_mode, inner_mask, reg, next_select);
7051 if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
7052 x = gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
7055 /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
7056 shift and AND produces only copies of the sign bit (C2 is one less
7057 than a power of two), we can do this with just a shift. */
7059 if (GET_CODE (x) == LSHIFTRT
7060 && GET_CODE (XEXP (x, 1)) == CONST_INT
7061 /* The shift puts one of the sign bit copies in the least significant
7062 bit. */
7063 && ((INTVAL (XEXP (x, 1))
7064 + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
7065 >= GET_MODE_BITSIZE (GET_MODE (x)))
7066 && exact_log2 (mask + 1) >= 0
7067 /* Number of bits left after the shift must be more than the mask
7068 needs. */
7069 && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
7070 <= GET_MODE_BITSIZE (GET_MODE (x)))
7071 /* Must be more sign bit copies than the mask needs. */
7072 && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
7073 >= exact_log2 (mask + 1)))
7074 x = gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
7075 GEN_INT (GET_MODE_BITSIZE (GET_MODE (x))
7076 - exact_log2 (mask + 1)));
7078 goto shiftrt;
7080 case ASHIFTRT:
7081 /* If we are just looking for the sign bit, we don't need this shift at
7082 all, even if it has a variable count. */
7083 if (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
7084 && (mask == ((unsigned HOST_WIDE_INT) 1
7085 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
7086 return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
7088 /* If this is a shift by a constant, get a mask that contains those bits
7089 that are not copies of the sign bit. We then have two cases: If
7090 MASK only includes those bits, this can be a logical shift, which may
7091 allow simplifications. If MASK is a single-bit field not within
7092 those bits, we are requesting a copy of the sign bit and hence can
7093 shift the sign bit to the appropriate location. */
7095 if (GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) >= 0
7096 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
7098 int i = -1;
7100 /* If the considered data is wider than HOST_WIDE_INT, we can't
7101 represent a mask for all its bits in a single scalar.
7102 But we only care about the lower bits, so calculate these. */
7104 if (GET_MODE_BITSIZE (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
7106 nonzero = ~(HOST_WIDE_INT) 0;
7108 /* GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
7109 is the number of bits a full-width mask would have set.
7110 We need only shift if these are fewer than nonzero can
7111 hold. If not, we must keep all bits set in nonzero. */
7113 if (GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
7114 < HOST_BITS_PER_WIDE_INT)
7115 nonzero >>= INTVAL (XEXP (x, 1))
7116 + HOST_BITS_PER_WIDE_INT
7117 - GET_MODE_BITSIZE (GET_MODE (x)) ;
7119 else
7121 nonzero = GET_MODE_MASK (GET_MODE (x));
7122 nonzero >>= INTVAL (XEXP (x, 1));
7125 if ((mask & ~nonzero) == 0
7126 || (i = exact_log2 (mask)) >= 0)
7128 x = simplify_shift_const
7129 (x, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
7130 i < 0 ? INTVAL (XEXP (x, 1))
7131 : GET_MODE_BITSIZE (GET_MODE (x)) - 1 - i);
7133 if (GET_CODE (x) != ASHIFTRT)
7134 return force_to_mode (x, mode, mask, reg, next_select);
7138 /* If MASK is 1, convert this to an LSHIFTRT. This can be done
7139 even if the shift count isn't a constant. */
7140 if (mask == 1)
7141 x = gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0), XEXP (x, 1));
7143 shiftrt:
7145 /* If this is a zero- or sign-extension operation that just affects bits
7146 we don't care about, remove it. Be sure the call above returned
7147 something that is still a shift. */
7149 if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
7150 && GET_CODE (XEXP (x, 1)) == CONST_INT
7151 && INTVAL (XEXP (x, 1)) >= 0
7152 && (INTVAL (XEXP (x, 1))
7153 <= GET_MODE_BITSIZE (GET_MODE (x)) - (floor_log2 (mask) + 1))
7154 && GET_CODE (XEXP (x, 0)) == ASHIFT
7155 && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
7156 return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
7157 reg, next_select);
7159 break;
7161 case ROTATE:
7162 case ROTATERT:
7163 /* If the shift count is constant and we can do computations
7164 in the mode of X, compute where the bits we care about are.
7165 Otherwise, we can't do anything. Don't change the mode of
7166 the shift or propagate MODE into the shift, though. */
7167 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7168 && INTVAL (XEXP (x, 1)) >= 0)
7170 temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
7171 GET_MODE (x), GEN_INT (mask),
7172 XEXP (x, 1));
7173 if (temp && GET_CODE (temp) == CONST_INT)
7174 SUBST (XEXP (x, 0),
7175 force_to_mode (XEXP (x, 0), GET_MODE (x),
7176 INTVAL (temp), reg, next_select));
7178 break;
7180 case NEG:
7181 /* If we just want the low-order bit, the NEG isn't needed since it
7182 won't change the low-order bit. */
7183 if (mask == 1)
7184 return force_to_mode (XEXP (x, 0), mode, mask, reg, just_select);
7186 /* We need any bits less significant than the most significant bit in
7187 MASK since carries from those bits will affect the bits we are
7188 interested in. */
7189 mask = fuller_mask;
7190 goto unop;
7192 case NOT:
7193 /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
7194 same as the XOR case above. Ensure that the constant we form is not
7195 wider than the mode of X. */
7197 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7198 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
7199 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7200 && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
7201 < GET_MODE_BITSIZE (GET_MODE (x)))
7202 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
7204 temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)),
7205 GET_MODE (x));
7206 temp = gen_binary (XOR, GET_MODE (x), XEXP (XEXP (x, 0), 0), temp);
7207 x = gen_binary (LSHIFTRT, GET_MODE (x), temp, XEXP (XEXP (x, 0), 1));
7209 return force_to_mode (x, mode, mask, reg, next_select);
7212 /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
7213 use the full mask inside the NOT. */
7214 mask = fuller_mask;
7216 unop:
7217 op0 = gen_lowpart (op_mode,
7218 force_to_mode (XEXP (x, 0), mode, mask,
7219 reg, next_select));
7220 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
7221 x = simplify_gen_unary (code, op_mode, op0, op_mode);
7222 break;
7224 case NE:
7225 /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
7226 in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
7227 which is equal to STORE_FLAG_VALUE. */
7228 if ((mask & ~STORE_FLAG_VALUE) == 0 && XEXP (x, 1) == const0_rtx
7229 && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
7230 && (nonzero_bits (XEXP (x, 0), mode)
7231 == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
7232 return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
7234 break;
7236 case IF_THEN_ELSE:
7237 /* We have no way of knowing if the IF_THEN_ELSE can itself be
7238 written in a narrower mode. We play it safe and do not do so. */
7240 SUBST (XEXP (x, 1),
7241 gen_lowpart (GET_MODE (x),
7242 force_to_mode (XEXP (x, 1), mode,
7243 mask, reg, next_select)));
7244 SUBST (XEXP (x, 2),
7245 gen_lowpart (GET_MODE (x),
7246 force_to_mode (XEXP (x, 2), mode,
7247 mask, reg, next_select)));
7248 break;
7250 default:
7251 break;
7254 /* Ensure we return a value of the proper mode. */
7255 return gen_lowpart (mode, x);
7258 /* Return nonzero if X is an expression that has one of two values depending on
7259 whether some other value is zero or nonzero. In that case, we return the
7260 value that is being tested, *PTRUE is set to the value if the rtx being
7261 returned has a nonzero value, and *PFALSE is set to the other alternative.
7263 If we return zero, we set *PTRUE and *PFALSE to X. */
7265 static rtx
7266 if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
7268 enum machine_mode mode = GET_MODE (x);
7269 enum rtx_code code = GET_CODE (x);
7270 rtx cond0, cond1, true0, true1, false0, false1;
7271 unsigned HOST_WIDE_INT nz;
7273 /* If we are comparing a value against zero, we are done. */
7274 if ((code == NE || code == EQ)
7275 && XEXP (x, 1) == const0_rtx)
7277 *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
7278 *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
7279 return XEXP (x, 0);
7282 /* If this is a unary operation whose operand has one of two values, apply
7283 our opcode to compute those values. */
7284 else if (UNARY_P (x)
7285 && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
7287 *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
7288 *pfalse = simplify_gen_unary (code, mode, false0,
7289 GET_MODE (XEXP (x, 0)));
7290 return cond0;
7293 /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
7294 make can't possibly match and would suppress other optimizations. */
7295 else if (code == COMPARE)
7298 /* If this is a binary operation, see if either side has only one of two
7299 values. If either one does or if both do and they are conditional on
7300 the same value, compute the new true and false values. */
7301 else if (BINARY_P (x))
7303 cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
7304 cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
7306 if ((cond0 != 0 || cond1 != 0)
7307 && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
7309 /* If if_then_else_cond returned zero, then true/false are the
7310 same rtl. We must copy one of them to prevent invalid rtl
7311 sharing. */
7312 if (cond0 == 0)
7313 true0 = copy_rtx (true0);
7314 else if (cond1 == 0)
7315 true1 = copy_rtx (true1);
7317 *ptrue = gen_binary (code, mode, true0, true1);
7318 *pfalse = gen_binary (code, mode, false0, false1);
7319 return cond0 ? cond0 : cond1;
7322 /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
7323 operands is zero when the other is nonzero, and vice-versa,
7324 and STORE_FLAG_VALUE is 1 or -1. */
7326 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
7327 && (code == PLUS || code == IOR || code == XOR || code == MINUS
7328 || code == UMAX)
7329 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
7331 rtx op0 = XEXP (XEXP (x, 0), 1);
7332 rtx op1 = XEXP (XEXP (x, 1), 1);
7334 cond0 = XEXP (XEXP (x, 0), 0);
7335 cond1 = XEXP (XEXP (x, 1), 0);
7337 if (COMPARISON_P (cond0)
7338 && COMPARISON_P (cond1)
7339 && ((GET_CODE (cond0) == combine_reversed_comparison_code (cond1)
7340 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
7341 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
7342 || ((swap_condition (GET_CODE (cond0))
7343 == combine_reversed_comparison_code (cond1))
7344 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
7345 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
7346 && ! side_effects_p (x))
7348 *ptrue = gen_binary (MULT, mode, op0, const_true_rtx);
7349 *pfalse = gen_binary (MULT, mode,
7350 (code == MINUS
7351 ? simplify_gen_unary (NEG, mode, op1,
7352 mode)
7353 : op1),
7354 const_true_rtx);
7355 return cond0;
7359 /* Similarly for MULT, AND and UMIN, except that for these the result
7360 is always zero. */
7361 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
7362 && (code == MULT || code == AND || code == UMIN)
7363 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
7365 cond0 = XEXP (XEXP (x, 0), 0);
7366 cond1 = XEXP (XEXP (x, 1), 0);
7368 if (COMPARISON_P (cond0)
7369 && COMPARISON_P (cond1)
7370 && ((GET_CODE (cond0) == combine_reversed_comparison_code (cond1)
7371 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
7372 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
7373 || ((swap_condition (GET_CODE (cond0))
7374 == combine_reversed_comparison_code (cond1))
7375 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
7376 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
7377 && ! side_effects_p (x))
7379 *ptrue = *pfalse = const0_rtx;
7380 return cond0;
7385 else if (code == IF_THEN_ELSE)
7387 /* If we have IF_THEN_ELSE already, extract the condition and
7388 canonicalize it if it is NE or EQ. */
7389 cond0 = XEXP (x, 0);
7390 *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
7391 if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
7392 return XEXP (cond0, 0);
7393 else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
7395 *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
7396 return XEXP (cond0, 0);
7398 else
7399 return cond0;
7402 /* If X is a SUBREG, we can narrow both the true and false values
7403 if the inner expression, if there is a condition. */
7404 else if (code == SUBREG
7405 && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
7406 &true0, &false0)))
7408 true0 = simplify_gen_subreg (mode, true0,
7409 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
7410 false0 = simplify_gen_subreg (mode, false0,
7411 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
7412 if (true0 && false0)
7414 *ptrue = true0;
7415 *pfalse = false0;
7416 return cond0;
7420 /* If X is a constant, this isn't special and will cause confusions
7421 if we treat it as such. Likewise if it is equivalent to a constant. */
7422 else if (CONSTANT_P (x)
7423 || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
7426 /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
7427 will be least confusing to the rest of the compiler. */
7428 else if (mode == BImode)
7430 *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
7431 return x;
7434 /* If X is known to be either 0 or -1, those are the true and
7435 false values when testing X. */
7436 else if (x == constm1_rtx || x == const0_rtx
7437 || (mode != VOIDmode
7438 && num_sign_bit_copies (x, mode) == GET_MODE_BITSIZE (mode)))
7440 *ptrue = constm1_rtx, *pfalse = const0_rtx;
7441 return x;
7444 /* Likewise for 0 or a single bit. */
7445 else if (SCALAR_INT_MODE_P (mode)
7446 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
7447 && exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
7449 *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
7450 return x;
7453 /* Otherwise fail; show no condition with true and false values the same. */
7454 *ptrue = *pfalse = x;
7455 return 0;
7458 /* Return the value of expression X given the fact that condition COND
7459 is known to be true when applied to REG as its first operand and VAL
7460 as its second. X is known to not be shared and so can be modified in
7461 place.
7463 We only handle the simplest cases, and specifically those cases that
7464 arise with IF_THEN_ELSE expressions. */
7466 static rtx
7467 known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
7469 enum rtx_code code = GET_CODE (x);
7470 rtx temp;
7471 const char *fmt;
7472 int i, j;
7474 if (side_effects_p (x))
7475 return x;
7477 /* If either operand of the condition is a floating point value,
7478 then we have to avoid collapsing an EQ comparison. */
7479 if (cond == EQ
7480 && rtx_equal_p (x, reg)
7481 && ! FLOAT_MODE_P (GET_MODE (x))
7482 && ! FLOAT_MODE_P (GET_MODE (val)))
7483 return val;
7485 if (cond == UNEQ && rtx_equal_p (x, reg))
7486 return val;
7488 /* If X is (abs REG) and we know something about REG's relationship
7489 with zero, we may be able to simplify this. */
7491 if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
7492 switch (cond)
7494 case GE: case GT: case EQ:
7495 return XEXP (x, 0);
7496 case LT: case LE:
7497 return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
7498 XEXP (x, 0),
7499 GET_MODE (XEXP (x, 0)));
7500 default:
7501 break;
7504 /* The only other cases we handle are MIN, MAX, and comparisons if the
7505 operands are the same as REG and VAL. */
7507 else if (COMPARISON_P (x) || COMMUTATIVE_ARITH_P (x))
7509 if (rtx_equal_p (XEXP (x, 0), val))
7510 cond = swap_condition (cond), temp = val, val = reg, reg = temp;
7512 if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
7514 if (COMPARISON_P (x))
7516 if (comparison_dominates_p (cond, code))
7517 return const_true_rtx;
7519 code = combine_reversed_comparison_code (x);
7520 if (code != UNKNOWN
7521 && comparison_dominates_p (cond, code))
7522 return const0_rtx;
7523 else
7524 return x;
7526 else if (code == SMAX || code == SMIN
7527 || code == UMIN || code == UMAX)
7529 int unsignedp = (code == UMIN || code == UMAX);
7531 /* Do not reverse the condition when it is NE or EQ.
7532 This is because we cannot conclude anything about
7533 the value of 'SMAX (x, y)' when x is not equal to y,
7534 but we can when x equals y. */
7535 if ((code == SMAX || code == UMAX)
7536 && ! (cond == EQ || cond == NE))
7537 cond = reverse_condition (cond);
7539 switch (cond)
7541 case GE: case GT:
7542 return unsignedp ? x : XEXP (x, 1);
7543 case LE: case LT:
7544 return unsignedp ? x : XEXP (x, 0);
7545 case GEU: case GTU:
7546 return unsignedp ? XEXP (x, 1) : x;
7547 case LEU: case LTU:
7548 return unsignedp ? XEXP (x, 0) : x;
7549 default:
7550 break;
7555 else if (code == SUBREG)
7557 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
7558 rtx new, r = known_cond (SUBREG_REG (x), cond, reg, val);
7560 if (SUBREG_REG (x) != r)
7562 /* We must simplify subreg here, before we lose track of the
7563 original inner_mode. */
7564 new = simplify_subreg (GET_MODE (x), r,
7565 inner_mode, SUBREG_BYTE (x));
7566 if (new)
7567 return new;
7568 else
7569 SUBST (SUBREG_REG (x), r);
7572 return x;
7574 /* We don't have to handle SIGN_EXTEND here, because even in the
7575 case of replacing something with a modeless CONST_INT, a
7576 CONST_INT is already (supposed to be) a valid sign extension for
7577 its narrower mode, which implies it's already properly
7578 sign-extended for the wider mode. Now, for ZERO_EXTEND, the
7579 story is different. */
7580 else if (code == ZERO_EXTEND)
7582 enum machine_mode inner_mode = GET_MODE (XEXP (x, 0));
7583 rtx new, r = known_cond (XEXP (x, 0), cond, reg, val);
7585 if (XEXP (x, 0) != r)
7587 /* We must simplify the zero_extend here, before we lose
7588 track of the original inner_mode. */
7589 new = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
7590 r, inner_mode);
7591 if (new)
7592 return new;
7593 else
7594 SUBST (XEXP (x, 0), r);
7597 return x;
7600 fmt = GET_RTX_FORMAT (code);
7601 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7603 if (fmt[i] == 'e')
7604 SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
7605 else if (fmt[i] == 'E')
7606 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
7607 SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
7608 cond, reg, val));
7611 return x;
7614 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
7615 assignment as a field assignment. */
7617 static int
7618 rtx_equal_for_field_assignment_p (rtx x, rtx y)
7620 if (x == y || rtx_equal_p (x, y))
7621 return 1;
7623 if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
7624 return 0;
7626 /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
7627 Note that all SUBREGs of MEM are paradoxical; otherwise they
7628 would have been rewritten. */
7629 if (GET_CODE (x) == MEM && GET_CODE (y) == SUBREG
7630 && GET_CODE (SUBREG_REG (y)) == MEM
7631 && rtx_equal_p (SUBREG_REG (y),
7632 gen_lowpart (GET_MODE (SUBREG_REG (y)), x)))
7633 return 1;
7635 if (GET_CODE (y) == MEM && GET_CODE (x) == SUBREG
7636 && GET_CODE (SUBREG_REG (x)) == MEM
7637 && rtx_equal_p (SUBREG_REG (x),
7638 gen_lowpart (GET_MODE (SUBREG_REG (x)), y)))
7639 return 1;
7641 /* We used to see if get_last_value of X and Y were the same but that's
7642 not correct. In one direction, we'll cause the assignment to have
7643 the wrong destination and in the case, we'll import a register into this
7644 insn that might have already have been dead. So fail if none of the
7645 above cases are true. */
7646 return 0;
7649 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
7650 Return that assignment if so.
7652 We only handle the most common cases. */
7654 static rtx
7655 make_field_assignment (rtx x)
7657 rtx dest = SET_DEST (x);
7658 rtx src = SET_SRC (x);
7659 rtx assign;
7660 rtx rhs, lhs;
7661 HOST_WIDE_INT c1;
7662 HOST_WIDE_INT pos;
7663 unsigned HOST_WIDE_INT len;
7664 rtx other;
7665 enum machine_mode mode;
7667 /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
7668 a clear of a one-bit field. We will have changed it to
7669 (and (rotate (const_int -2) POS) DEST), so check for that. Also check
7670 for a SUBREG. */
7672 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
7673 && GET_CODE (XEXP (XEXP (src, 0), 0)) == CONST_INT
7674 && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
7675 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7677 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
7678 1, 1, 1, 0);
7679 if (assign != 0)
7680 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
7681 return x;
7684 else if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
7685 && subreg_lowpart_p (XEXP (src, 0))
7686 && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
7687 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
7688 && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
7689 && GET_CODE (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == CONST_INT
7690 && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
7691 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7693 assign = make_extraction (VOIDmode, dest, 0,
7694 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
7695 1, 1, 1, 0);
7696 if (assign != 0)
7697 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
7698 return x;
7701 /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
7702 one-bit field. */
7703 else if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
7704 && XEXP (XEXP (src, 0), 0) == const1_rtx
7705 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7707 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
7708 1, 1, 1, 0);
7709 if (assign != 0)
7710 return gen_rtx_SET (VOIDmode, assign, const1_rtx);
7711 return x;
7714 /* The other case we handle is assignments into a constant-position
7715 field. They look like (ior/xor (and DEST C1) OTHER). If C1 represents
7716 a mask that has all one bits except for a group of zero bits and
7717 OTHER is known to have zeros where C1 has ones, this is such an
7718 assignment. Compute the position and length from C1. Shift OTHER
7719 to the appropriate position, force it to the required mode, and
7720 make the extraction. Check for the AND in both operands. */
7722 if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
7723 return x;
7725 rhs = expand_compound_operation (XEXP (src, 0));
7726 lhs = expand_compound_operation (XEXP (src, 1));
7728 if (GET_CODE (rhs) == AND
7729 && GET_CODE (XEXP (rhs, 1)) == CONST_INT
7730 && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
7731 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
7732 else if (GET_CODE (lhs) == AND
7733 && GET_CODE (XEXP (lhs, 1)) == CONST_INT
7734 && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
7735 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
7736 else
7737 return x;
7739 pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
7740 if (pos < 0 || pos + len > GET_MODE_BITSIZE (GET_MODE (dest))
7741 || GET_MODE_BITSIZE (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
7742 || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
7743 return x;
7745 assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
7746 if (assign == 0)
7747 return x;
7749 /* The mode to use for the source is the mode of the assignment, or of
7750 what is inside a possible STRICT_LOW_PART. */
7751 mode = (GET_CODE (assign) == STRICT_LOW_PART
7752 ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
7754 /* Shift OTHER right POS places and make it the source, restricting it
7755 to the proper length and mode. */
7757 src = force_to_mode (simplify_shift_const (NULL_RTX, LSHIFTRT,
7758 GET_MODE (src), other, pos),
7759 mode,
7760 GET_MODE_BITSIZE (mode) >= HOST_BITS_PER_WIDE_INT
7761 ? ~(unsigned HOST_WIDE_INT) 0
7762 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
7763 dest, 0);
7765 /* If SRC is masked by an AND that does not make a difference in
7766 the value being stored, strip it. */
7767 if (GET_CODE (assign) == ZERO_EXTRACT
7768 && GET_CODE (XEXP (assign, 1)) == CONST_INT
7769 && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
7770 && GET_CODE (src) == AND
7771 && GET_CODE (XEXP (src, 1)) == CONST_INT
7772 && ((unsigned HOST_WIDE_INT) INTVAL (XEXP (src, 1))
7773 == ((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (assign, 1))) - 1))
7774 src = XEXP (src, 0);
7776 return gen_rtx_SET (VOIDmode, assign, src);
7779 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
7780 if so. */
7782 static rtx
7783 apply_distributive_law (rtx x)
7785 enum rtx_code code = GET_CODE (x);
7786 enum rtx_code inner_code;
7787 rtx lhs, rhs, other;
7788 rtx tem;
7790 /* Distributivity is not true for floating point as it can change the
7791 value. So we don't do it unless -funsafe-math-optimizations. */
7792 if (FLOAT_MODE_P (GET_MODE (x))
7793 && ! flag_unsafe_math_optimizations)
7794 return x;
7796 /* The outer operation can only be one of the following: */
7797 if (code != IOR && code != AND && code != XOR
7798 && code != PLUS && code != MINUS)
7799 return x;
7801 lhs = XEXP (x, 0);
7802 rhs = XEXP (x, 1);
7804 /* If either operand is a primitive we can't do anything, so get out
7805 fast. */
7806 if (OBJECT_P (lhs) || OBJECT_P (rhs))
7807 return x;
7809 lhs = expand_compound_operation (lhs);
7810 rhs = expand_compound_operation (rhs);
7811 inner_code = GET_CODE (lhs);
7812 if (inner_code != GET_CODE (rhs))
7813 return x;
7815 /* See if the inner and outer operations distribute. */
7816 switch (inner_code)
7818 case LSHIFTRT:
7819 case ASHIFTRT:
7820 case AND:
7821 case IOR:
7822 /* These all distribute except over PLUS. */
7823 if (code == PLUS || code == MINUS)
7824 return x;
7825 break;
7827 case MULT:
7828 if (code != PLUS && code != MINUS)
7829 return x;
7830 break;
7832 case ASHIFT:
7833 /* This is also a multiply, so it distributes over everything. */
7834 break;
7836 case SUBREG:
7837 /* Non-paradoxical SUBREGs distributes over all operations, provided
7838 the inner modes and byte offsets are the same, this is an extraction
7839 of a low-order part, we don't convert an fp operation to int or
7840 vice versa, and we would not be converting a single-word
7841 operation into a multi-word operation. The latter test is not
7842 required, but it prevents generating unneeded multi-word operations.
7843 Some of the previous tests are redundant given the latter test, but
7844 are retained because they are required for correctness.
7846 We produce the result slightly differently in this case. */
7848 if (GET_MODE (SUBREG_REG (lhs)) != GET_MODE (SUBREG_REG (rhs))
7849 || SUBREG_BYTE (lhs) != SUBREG_BYTE (rhs)
7850 || ! subreg_lowpart_p (lhs)
7851 || (GET_MODE_CLASS (GET_MODE (lhs))
7852 != GET_MODE_CLASS (GET_MODE (SUBREG_REG (lhs))))
7853 || (GET_MODE_SIZE (GET_MODE (lhs))
7854 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))))
7855 || GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))) > UNITS_PER_WORD)
7856 return x;
7858 tem = gen_binary (code, GET_MODE (SUBREG_REG (lhs)),
7859 SUBREG_REG (lhs), SUBREG_REG (rhs));
7860 return gen_lowpart (GET_MODE (x), tem);
7862 default:
7863 return x;
7866 /* Set LHS and RHS to the inner operands (A and B in the example
7867 above) and set OTHER to the common operand (C in the example).
7868 There is only one way to do this unless the inner operation is
7869 commutative. */
7870 if (COMMUTATIVE_ARITH_P (lhs)
7871 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
7872 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
7873 else if (COMMUTATIVE_ARITH_P (lhs)
7874 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
7875 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
7876 else if (COMMUTATIVE_ARITH_P (lhs)
7877 && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
7878 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
7879 else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
7880 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
7881 else
7882 return x;
7884 /* Form the new inner operation, seeing if it simplifies first. */
7885 tem = gen_binary (code, GET_MODE (x), lhs, rhs);
7887 /* There is one exception to the general way of distributing:
7888 (a | c) ^ (b | c) -> (a ^ b) & ~c */
7889 if (code == XOR && inner_code == IOR)
7891 inner_code = AND;
7892 other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
7895 /* We may be able to continuing distributing the result, so call
7896 ourselves recursively on the inner operation before forming the
7897 outer operation, which we return. */
7898 return gen_binary (inner_code, GET_MODE (x),
7899 apply_distributive_law (tem), other);
7902 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
7903 in MODE.
7905 Return an equivalent form, if different from X. Otherwise, return X. If
7906 X is zero, we are to always construct the equivalent form. */
7908 static rtx
7909 simplify_and_const_int (rtx x, enum machine_mode mode, rtx varop,
7910 unsigned HOST_WIDE_INT constop)
7912 unsigned HOST_WIDE_INT nonzero;
7913 int i;
7915 /* Simplify VAROP knowing that we will be only looking at some of the
7916 bits in it.
7918 Note by passing in CONSTOP, we guarantee that the bits not set in
7919 CONSTOP are not significant and will never be examined. We must
7920 ensure that is the case by explicitly masking out those bits
7921 before returning. */
7922 varop = force_to_mode (varop, mode, constop, NULL_RTX, 0);
7924 /* If VAROP is a CLOBBER, we will fail so return it. */
7925 if (GET_CODE (varop) == CLOBBER)
7926 return varop;
7928 /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
7929 to VAROP and return the new constant. */
7930 if (GET_CODE (varop) == CONST_INT)
7931 return GEN_INT (trunc_int_for_mode (INTVAL (varop) & constop, mode));
7933 /* See what bits may be nonzero in VAROP. Unlike the general case of
7934 a call to nonzero_bits, here we don't care about bits outside
7935 MODE. */
7937 nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
7939 /* Turn off all bits in the constant that are known to already be zero.
7940 Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
7941 which is tested below. */
7943 constop &= nonzero;
7945 /* If we don't have any bits left, return zero. */
7946 if (constop == 0)
7947 return const0_rtx;
7949 /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
7950 a power of two, we can replace this with an ASHIFT. */
7951 if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
7952 && (i = exact_log2 (constop)) >= 0)
7953 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
7955 /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
7956 or XOR, then try to apply the distributive law. This may eliminate
7957 operations if either branch can be simplified because of the AND.
7958 It may also make some cases more complex, but those cases probably
7959 won't match a pattern either with or without this. */
7961 if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
7962 return
7963 gen_lowpart
7964 (mode,
7965 apply_distributive_law
7966 (gen_binary (GET_CODE (varop), GET_MODE (varop),
7967 simplify_and_const_int (NULL_RTX, GET_MODE (varop),
7968 XEXP (varop, 0), constop),
7969 simplify_and_const_int (NULL_RTX, GET_MODE (varop),
7970 XEXP (varop, 1), constop))));
7972 /* If VAROP is PLUS, and the constant is a mask of low bite, distribute
7973 the AND and see if one of the operands simplifies to zero. If so, we
7974 may eliminate it. */
7976 if (GET_CODE (varop) == PLUS
7977 && exact_log2 (constop + 1) >= 0)
7979 rtx o0, o1;
7981 o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
7982 o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
7983 if (o0 == const0_rtx)
7984 return o1;
7985 if (o1 == const0_rtx)
7986 return o0;
7989 /* Get VAROP in MODE. Try to get a SUBREG if not. Don't make a new SUBREG
7990 if we already had one (just check for the simplest cases). */
7991 if (x && GET_CODE (XEXP (x, 0)) == SUBREG
7992 && GET_MODE (XEXP (x, 0)) == mode
7993 && SUBREG_REG (XEXP (x, 0)) == varop)
7994 varop = XEXP (x, 0);
7995 else
7996 varop = gen_lowpart (mode, varop);
7998 /* If we can't make the SUBREG, try to return what we were given. */
7999 if (GET_CODE (varop) == CLOBBER)
8000 return x ? x : varop;
8002 /* If we are only masking insignificant bits, return VAROP. */
8003 if (constop == nonzero)
8004 x = varop;
8005 else
8007 /* Otherwise, return an AND. */
8008 constop = trunc_int_for_mode (constop, mode);
8009 /* See how much, if any, of X we can use. */
8010 if (x == 0 || GET_CODE (x) != AND || GET_MODE (x) != mode)
8011 x = gen_binary (AND, mode, varop, GEN_INT (constop));
8013 else
8015 if (GET_CODE (XEXP (x, 1)) != CONST_INT
8016 || (unsigned HOST_WIDE_INT) INTVAL (XEXP (x, 1)) != constop)
8017 SUBST (XEXP (x, 1), GEN_INT (constop));
8019 SUBST (XEXP (x, 0), varop);
8023 return x;
8026 #define nonzero_bits_with_known(X, MODE) \
8027 cached_nonzero_bits (X, MODE, known_x, known_mode, known_ret)
8029 /* The function cached_nonzero_bits is a wrapper around nonzero_bits1.
8030 It avoids exponential behavior in nonzero_bits1 when X has
8031 identical subexpressions on the first or the second level. */
8033 static unsigned HOST_WIDE_INT
8034 cached_nonzero_bits (rtx x, enum machine_mode mode, rtx known_x,
8035 enum machine_mode known_mode,
8036 unsigned HOST_WIDE_INT known_ret)
8038 if (x == known_x && mode == known_mode)
8039 return known_ret;
8041 /* Try to find identical subexpressions. If found call
8042 nonzero_bits1 on X with the subexpressions as KNOWN_X and the
8043 precomputed value for the subexpression as KNOWN_RET. */
8045 if (ARITHMETIC_P (x))
8047 rtx x0 = XEXP (x, 0);
8048 rtx x1 = XEXP (x, 1);
8050 /* Check the first level. */
8051 if (x0 == x1)
8052 return nonzero_bits1 (x, mode, x0, mode,
8053 nonzero_bits_with_known (x0, mode));
8055 /* Check the second level. */
8056 if (ARITHMETIC_P (x0)
8057 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
8058 return nonzero_bits1 (x, mode, x1, mode,
8059 nonzero_bits_with_known (x1, mode));
8061 if (ARITHMETIC_P (x1)
8062 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
8063 return nonzero_bits1 (x, mode, x0, mode,
8064 nonzero_bits_with_known (x0, mode));
8067 return nonzero_bits1 (x, mode, known_x, known_mode, known_ret);
8070 /* We let num_sign_bit_copies recur into nonzero_bits as that is useful.
8071 We don't let nonzero_bits recur into num_sign_bit_copies, because that
8072 is less useful. We can't allow both, because that results in exponential
8073 run time recursion. There is a nullstone testcase that triggered
8074 this. This macro avoids accidental uses of num_sign_bit_copies. */
8075 #define cached_num_sign_bit_copies()
8077 /* Given an expression, X, compute which bits in X can be nonzero.
8078 We don't care about bits outside of those defined in MODE.
8080 For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
8081 a shift, AND, or zero_extract, we can do better. */
8083 static unsigned HOST_WIDE_INT
8084 nonzero_bits1 (rtx x, enum machine_mode mode, rtx known_x,
8085 enum machine_mode known_mode,
8086 unsigned HOST_WIDE_INT known_ret)
8088 unsigned HOST_WIDE_INT nonzero = GET_MODE_MASK (mode);
8089 unsigned HOST_WIDE_INT inner_nz;
8090 enum rtx_code code;
8091 unsigned int mode_width = GET_MODE_BITSIZE (mode);
8092 rtx tem;
8094 /* For floating-point values, assume all bits are needed. */
8095 if (FLOAT_MODE_P (GET_MODE (x)) || FLOAT_MODE_P (mode))
8096 return nonzero;
8098 /* If X is wider than MODE, use its mode instead. */
8099 if (GET_MODE_BITSIZE (GET_MODE (x)) > mode_width)
8101 mode = GET_MODE (x);
8102 nonzero = GET_MODE_MASK (mode);
8103 mode_width = GET_MODE_BITSIZE (mode);
8106 if (mode_width > HOST_BITS_PER_WIDE_INT)
8107 /* Our only callers in this case look for single bit values. So
8108 just return the mode mask. Those tests will then be false. */
8109 return nonzero;
8111 #ifndef WORD_REGISTER_OPERATIONS
8112 /* If MODE is wider than X, but both are a single word for both the host
8113 and target machines, we can compute this from which bits of the
8114 object might be nonzero in its own mode, taking into account the fact
8115 that on many CISC machines, accessing an object in a wider mode
8116 causes the high-order bits to become undefined. So they are
8117 not known to be zero. */
8119 if (GET_MODE (x) != VOIDmode && GET_MODE (x) != mode
8120 && GET_MODE_BITSIZE (GET_MODE (x)) <= BITS_PER_WORD
8121 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
8122 && GET_MODE_BITSIZE (mode) > GET_MODE_BITSIZE (GET_MODE (x)))
8124 nonzero &= nonzero_bits_with_known (x, GET_MODE (x));
8125 nonzero |= GET_MODE_MASK (mode) & ~GET_MODE_MASK (GET_MODE (x));
8126 return nonzero;
8128 #endif
8130 code = GET_CODE (x);
8131 switch (code)
8133 case REG:
8134 #if defined(POINTERS_EXTEND_UNSIGNED) && !defined(HAVE_ptr_extend)
8135 /* If pointers extend unsigned and this is a pointer in Pmode, say that
8136 all the bits above ptr_mode are known to be zero. */
8137 if (POINTERS_EXTEND_UNSIGNED && GET_MODE (x) == Pmode
8138 && REG_POINTER (x))
8139 nonzero &= GET_MODE_MASK (ptr_mode);
8140 #endif
8142 /* Include declared information about alignment of pointers. */
8143 /* ??? We don't properly preserve REG_POINTER changes across
8144 pointer-to-integer casts, so we can't trust it except for
8145 things that we know must be pointers. See execute/960116-1.c. */
8146 if ((x == stack_pointer_rtx
8147 || x == frame_pointer_rtx
8148 || x == arg_pointer_rtx)
8149 && REGNO_POINTER_ALIGN (REGNO (x)))
8151 unsigned HOST_WIDE_INT alignment
8152 = REGNO_POINTER_ALIGN (REGNO (x)) / BITS_PER_UNIT;
8154 #ifdef PUSH_ROUNDING
8155 /* If PUSH_ROUNDING is defined, it is possible for the
8156 stack to be momentarily aligned only to that amount,
8157 so we pick the least alignment. */
8158 if (x == stack_pointer_rtx && PUSH_ARGS)
8159 alignment = MIN ((unsigned HOST_WIDE_INT) PUSH_ROUNDING (1),
8160 alignment);
8161 #endif
8163 nonzero &= ~(alignment - 1);
8166 /* If X is a register whose nonzero bits value is current, use it.
8167 Otherwise, if X is a register whose value we can find, use that
8168 value. Otherwise, use the previously-computed global nonzero bits
8169 for this register. */
8171 if (reg_last_set_value[REGNO (x)] != 0
8172 && (reg_last_set_mode[REGNO (x)] == mode
8173 || (GET_MODE_CLASS (reg_last_set_mode[REGNO (x)]) == MODE_INT
8174 && GET_MODE_CLASS (mode) == MODE_INT))
8175 && (reg_last_set_label[REGNO (x)] == label_tick
8176 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
8177 && REG_N_SETS (REGNO (x)) == 1
8178 && ! REGNO_REG_SET_P (ENTRY_BLOCK_PTR->next_bb->global_live_at_start,
8179 REGNO (x))))
8180 && INSN_CUID (reg_last_set[REGNO (x)]) < subst_low_cuid)
8181 return reg_last_set_nonzero_bits[REGNO (x)] & nonzero;
8183 tem = get_last_value (x);
8185 if (tem)
8187 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
8188 /* If X is narrower than MODE and TEM is a non-negative
8189 constant that would appear negative in the mode of X,
8190 sign-extend it for use in reg_nonzero_bits because some
8191 machines (maybe most) will actually do the sign-extension
8192 and this is the conservative approach.
8194 ??? For 2.5, try to tighten up the MD files in this regard
8195 instead of this kludge. */
8197 if (GET_MODE_BITSIZE (GET_MODE (x)) < mode_width
8198 && GET_CODE (tem) == CONST_INT
8199 && INTVAL (tem) > 0
8200 && 0 != (INTVAL (tem)
8201 & ((HOST_WIDE_INT) 1
8202 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
8203 tem = GEN_INT (INTVAL (tem)
8204 | ((HOST_WIDE_INT) (-1)
8205 << GET_MODE_BITSIZE (GET_MODE (x))));
8206 #endif
8207 return nonzero_bits_with_known (tem, mode) & nonzero;
8209 else if (nonzero_sign_valid && reg_nonzero_bits[REGNO (x)])
8211 unsigned HOST_WIDE_INT mask = reg_nonzero_bits[REGNO (x)];
8213 if (GET_MODE_BITSIZE (GET_MODE (x)) < mode_width)
8214 /* We don't know anything about the upper bits. */
8215 mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (GET_MODE (x));
8216 return nonzero & mask;
8218 else
8219 return nonzero;
8221 case CONST_INT:
8222 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
8223 /* If X is negative in MODE, sign-extend the value. */
8224 if (INTVAL (x) > 0 && mode_width < BITS_PER_WORD
8225 && 0 != (INTVAL (x) & ((HOST_WIDE_INT) 1 << (mode_width - 1))))
8226 return (INTVAL (x) | ((HOST_WIDE_INT) (-1) << mode_width));
8227 #endif
8229 return INTVAL (x);
8231 case MEM:
8232 #ifdef LOAD_EXTEND_OP
8233 /* In many, if not most, RISC machines, reading a byte from memory
8234 zeros the rest of the register. Noticing that fact saves a lot
8235 of extra zero-extends. */
8236 if (LOAD_EXTEND_OP (GET_MODE (x)) == ZERO_EXTEND)
8237 nonzero &= GET_MODE_MASK (GET_MODE (x));
8238 #endif
8239 break;
8241 case EQ: case NE:
8242 case UNEQ: case LTGT:
8243 case GT: case GTU: case UNGT:
8244 case LT: case LTU: case UNLT:
8245 case GE: case GEU: case UNGE:
8246 case LE: case LEU: case UNLE:
8247 case UNORDERED: case ORDERED:
8249 /* If this produces an integer result, we know which bits are set.
8250 Code here used to clear bits outside the mode of X, but that is
8251 now done above. */
8253 if (GET_MODE_CLASS (mode) == MODE_INT
8254 && mode_width <= HOST_BITS_PER_WIDE_INT)
8255 nonzero = STORE_FLAG_VALUE;
8256 break;
8258 case NEG:
8259 #if 0
8260 /* Disabled to avoid exponential mutual recursion between nonzero_bits
8261 and num_sign_bit_copies. */
8262 if (num_sign_bit_copies (XEXP (x, 0), GET_MODE (x))
8263 == GET_MODE_BITSIZE (GET_MODE (x)))
8264 nonzero = 1;
8265 #endif
8267 if (GET_MODE_SIZE (GET_MODE (x)) < mode_width)
8268 nonzero |= (GET_MODE_MASK (mode) & ~GET_MODE_MASK (GET_MODE (x)));
8269 break;
8271 case ABS:
8272 #if 0
8273 /* Disabled to avoid exponential mutual recursion between nonzero_bits
8274 and num_sign_bit_copies. */
8275 if (num_sign_bit_copies (XEXP (x, 0), GET_MODE (x))
8276 == GET_MODE_BITSIZE (GET_MODE (x)))
8277 nonzero = 1;
8278 #endif
8279 break;
8281 case TRUNCATE:
8282 nonzero &= (nonzero_bits_with_known (XEXP (x, 0), mode)
8283 & GET_MODE_MASK (mode));
8284 break;
8286 case ZERO_EXTEND:
8287 nonzero &= nonzero_bits_with_known (XEXP (x, 0), mode);
8288 if (GET_MODE (XEXP (x, 0)) != VOIDmode)
8289 nonzero &= GET_MODE_MASK (GET_MODE (XEXP (x, 0)));
8290 break;
8292 case SIGN_EXTEND:
8293 /* If the sign bit is known clear, this is the same as ZERO_EXTEND.
8294 Otherwise, show all the bits in the outer mode but not the inner
8295 may be nonzero. */
8296 inner_nz = nonzero_bits_with_known (XEXP (x, 0), mode);
8297 if (GET_MODE (XEXP (x, 0)) != VOIDmode)
8299 inner_nz &= GET_MODE_MASK (GET_MODE (XEXP (x, 0)));
8300 if (inner_nz
8301 & (((HOST_WIDE_INT) 1
8302 << (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - 1))))
8303 inner_nz |= (GET_MODE_MASK (mode)
8304 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0))));
8307 nonzero &= inner_nz;
8308 break;
8310 case AND:
8311 nonzero &= (nonzero_bits_with_known (XEXP (x, 0), mode)
8312 & nonzero_bits_with_known (XEXP (x, 1), mode));
8313 break;
8315 case XOR: case IOR:
8316 case UMIN: case UMAX: case SMIN: case SMAX:
8318 unsigned HOST_WIDE_INT nonzero0 =
8319 nonzero_bits_with_known (XEXP (x, 0), mode);
8321 /* Don't call nonzero_bits for the second time if it cannot change
8322 anything. */
8323 if ((nonzero & nonzero0) != nonzero)
8324 nonzero &= (nonzero0
8325 | nonzero_bits_with_known (XEXP (x, 1), mode));
8327 break;
8329 case PLUS: case MINUS:
8330 case MULT:
8331 case DIV: case UDIV:
8332 case MOD: case UMOD:
8333 /* We can apply the rules of arithmetic to compute the number of
8334 high- and low-order zero bits of these operations. We start by
8335 computing the width (position of the highest-order nonzero bit)
8336 and the number of low-order zero bits for each value. */
8338 unsigned HOST_WIDE_INT nz0 =
8339 nonzero_bits_with_known (XEXP (x, 0), mode);
8340 unsigned HOST_WIDE_INT nz1 =
8341 nonzero_bits_with_known (XEXP (x, 1), mode);
8342 int sign_index = GET_MODE_BITSIZE (GET_MODE (x)) - 1;
8343 int width0 = floor_log2 (nz0) + 1;
8344 int width1 = floor_log2 (nz1) + 1;
8345 int low0 = floor_log2 (nz0 & -nz0);
8346 int low1 = floor_log2 (nz1 & -nz1);
8347 HOST_WIDE_INT op0_maybe_minusp
8348 = (nz0 & ((HOST_WIDE_INT) 1 << sign_index));
8349 HOST_WIDE_INT op1_maybe_minusp
8350 = (nz1 & ((HOST_WIDE_INT) 1 << sign_index));
8351 unsigned int result_width = mode_width;
8352 int result_low = 0;
8354 switch (code)
8356 case PLUS:
8357 result_width = MAX (width0, width1) + 1;
8358 result_low = MIN (low0, low1);
8359 break;
8360 case MINUS:
8361 result_low = MIN (low0, low1);
8362 break;
8363 case MULT:
8364 result_width = width0 + width1;
8365 result_low = low0 + low1;
8366 break;
8367 case DIV:
8368 if (width1 == 0)
8369 break;
8370 if (! op0_maybe_minusp && ! op1_maybe_minusp)
8371 result_width = width0;
8372 break;
8373 case UDIV:
8374 if (width1 == 0)
8375 break;
8376 result_width = width0;
8377 break;
8378 case MOD:
8379 if (width1 == 0)
8380 break;
8381 if (! op0_maybe_minusp && ! op1_maybe_minusp)
8382 result_width = MIN (width0, width1);
8383 result_low = MIN (low0, low1);
8384 break;
8385 case UMOD:
8386 if (width1 == 0)
8387 break;
8388 result_width = MIN (width0, width1);
8389 result_low = MIN (low0, low1);
8390 break;
8391 default:
8392 abort ();
8395 if (result_width < mode_width)
8396 nonzero &= ((HOST_WIDE_INT) 1 << result_width) - 1;
8398 if (result_low > 0)
8399 nonzero &= ~(((HOST_WIDE_INT) 1 << result_low) - 1);
8401 #ifdef POINTERS_EXTEND_UNSIGNED
8402 /* If pointers extend unsigned and this is an addition or subtraction
8403 to a pointer in Pmode, all the bits above ptr_mode are known to be
8404 zero. */
8405 if (POINTERS_EXTEND_UNSIGNED > 0 && GET_MODE (x) == Pmode
8406 && (code == PLUS || code == MINUS)
8407 && GET_CODE (XEXP (x, 0)) == REG && REG_POINTER (XEXP (x, 0)))
8408 nonzero &= GET_MODE_MASK (ptr_mode);
8409 #endif
8411 break;
8413 case ZERO_EXTRACT:
8414 if (GET_CODE (XEXP (x, 1)) == CONST_INT
8415 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
8416 nonzero &= ((HOST_WIDE_INT) 1 << INTVAL (XEXP (x, 1))) - 1;
8417 break;
8419 case SUBREG:
8420 /* If this is a SUBREG formed for a promoted variable that has
8421 been zero-extended, we know that at least the high-order bits
8422 are zero, though others might be too. */
8424 if (SUBREG_PROMOTED_VAR_P (x) && SUBREG_PROMOTED_UNSIGNED_P (x) > 0)
8425 nonzero = (GET_MODE_MASK (GET_MODE (x))
8426 & nonzero_bits_with_known (SUBREG_REG (x), GET_MODE (x)));
8428 /* If the inner mode is a single word for both the host and target
8429 machines, we can compute this from which bits of the inner
8430 object might be nonzero. */
8431 if (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))) <= BITS_PER_WORD
8432 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x)))
8433 <= HOST_BITS_PER_WIDE_INT))
8435 nonzero &= nonzero_bits_with_known (SUBREG_REG (x), mode);
8437 #if defined (WORD_REGISTER_OPERATIONS) && defined (LOAD_EXTEND_OP)
8438 /* If this is a typical RISC machine, we only have to worry
8439 about the way loads are extended. */
8440 if ((LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (x))) == SIGN_EXTEND
8441 ? (((nonzero
8442 & (((unsigned HOST_WIDE_INT) 1
8443 << (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))) - 1))))
8444 != 0))
8445 : LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (x))) != ZERO_EXTEND)
8446 || GET_CODE (SUBREG_REG (x)) != MEM)
8447 #endif
8449 /* On many CISC machines, accessing an object in a wider mode
8450 causes the high-order bits to become undefined. So they are
8451 not known to be zero. */
8452 if (GET_MODE_SIZE (GET_MODE (x))
8453 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
8454 nonzero |= (GET_MODE_MASK (GET_MODE (x))
8455 & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x))));
8458 break;
8460 case ASHIFTRT:
8461 case LSHIFTRT:
8462 case ASHIFT:
8463 case ROTATE:
8464 /* The nonzero bits are in two classes: any bits within MODE
8465 that aren't in GET_MODE (x) are always significant. The rest of the
8466 nonzero bits are those that are significant in the operand of
8467 the shift when shifted the appropriate number of bits. This
8468 shows that high-order bits are cleared by the right shift and
8469 low-order bits by left shifts. */
8470 if (GET_CODE (XEXP (x, 1)) == CONST_INT
8471 && INTVAL (XEXP (x, 1)) >= 0
8472 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
8474 enum machine_mode inner_mode = GET_MODE (x);
8475 unsigned int width = GET_MODE_BITSIZE (inner_mode);
8476 int count = INTVAL (XEXP (x, 1));
8477 unsigned HOST_WIDE_INT mode_mask = GET_MODE_MASK (inner_mode);
8478 unsigned HOST_WIDE_INT op_nonzero =
8479 nonzero_bits_with_known (XEXP (x, 0), mode);
8480 unsigned HOST_WIDE_INT inner = op_nonzero & mode_mask;
8481 unsigned HOST_WIDE_INT outer = 0;
8483 if (mode_width > width)
8484 outer = (op_nonzero & nonzero & ~mode_mask);
8486 if (code == LSHIFTRT)
8487 inner >>= count;
8488 else if (code == ASHIFTRT)
8490 inner >>= count;
8492 /* If the sign bit may have been nonzero before the shift, we
8493 need to mark all the places it could have been copied to
8494 by the shift as possibly nonzero. */
8495 if (inner & ((HOST_WIDE_INT) 1 << (width - 1 - count)))
8496 inner |= (((HOST_WIDE_INT) 1 << count) - 1) << (width - count);
8498 else if (code == ASHIFT)
8499 inner <<= count;
8500 else
8501 inner = ((inner << (count % width)
8502 | (inner >> (width - (count % width)))) & mode_mask);
8504 nonzero &= (outer | inner);
8506 break;
8508 case FFS:
8509 case POPCOUNT:
8510 /* This is at most the number of bits in the mode. */
8511 nonzero = ((HOST_WIDE_INT) 2 << (floor_log2 (mode_width))) - 1;
8512 break;
8514 case CLZ:
8515 /* If CLZ has a known value at zero, then the nonzero bits are
8516 that value, plus the number of bits in the mode minus one. */
8517 if (CLZ_DEFINED_VALUE_AT_ZERO (mode, nonzero))
8518 nonzero |= ((HOST_WIDE_INT) 1 << (floor_log2 (mode_width))) - 1;
8519 else
8520 nonzero = -1;
8521 break;
8523 case CTZ:
8524 /* If CTZ has a known value at zero, then the nonzero bits are
8525 that value, plus the number of bits in the mode minus one. */
8526 if (CTZ_DEFINED_VALUE_AT_ZERO (mode, nonzero))
8527 nonzero |= ((HOST_WIDE_INT) 1 << (floor_log2 (mode_width))) - 1;
8528 else
8529 nonzero = -1;
8530 break;
8532 case PARITY:
8533 nonzero = 1;
8534 break;
8536 case IF_THEN_ELSE:
8537 nonzero &= (nonzero_bits_with_known (XEXP (x, 1), mode)
8538 | nonzero_bits_with_known (XEXP (x, 2), mode));
8539 break;
8541 default:
8542 break;
8545 return nonzero;
8548 /* See the macro definition above. */
8549 #undef cached_num_sign_bit_copies
8551 #define num_sign_bit_copies_with_known(X, M) \
8552 cached_num_sign_bit_copies (X, M, known_x, known_mode, known_ret)
8554 /* The function cached_num_sign_bit_copies is a wrapper around
8555 num_sign_bit_copies1. It avoids exponential behavior in
8556 num_sign_bit_copies1 when X has identical subexpressions on the
8557 first or the second level. */
8559 static unsigned int
8560 cached_num_sign_bit_copies (rtx x, enum machine_mode mode, rtx known_x,
8561 enum machine_mode known_mode,
8562 unsigned int known_ret)
8564 if (x == known_x && mode == known_mode)
8565 return known_ret;
8567 /* Try to find identical subexpressions. If found call
8568 num_sign_bit_copies1 on X with the subexpressions as KNOWN_X and
8569 the precomputed value for the subexpression as KNOWN_RET. */
8571 if (ARITHMETIC_P (x))
8573 rtx x0 = XEXP (x, 0);
8574 rtx x1 = XEXP (x, 1);
8576 /* Check the first level. */
8577 if (x0 == x1)
8578 return
8579 num_sign_bit_copies1 (x, mode, x0, mode,
8580 num_sign_bit_copies_with_known (x0, mode));
8582 /* Check the second level. */
8583 if (ARITHMETIC_P (x0)
8584 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
8585 return
8586 num_sign_bit_copies1 (x, mode, x1, mode,
8587 num_sign_bit_copies_with_known (x1, mode));
8589 if (ARITHMETIC_P (x1)
8590 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
8591 return
8592 num_sign_bit_copies1 (x, mode, x0, mode,
8593 num_sign_bit_copies_with_known (x0, mode));
8596 return num_sign_bit_copies1 (x, mode, known_x, known_mode, known_ret);
8599 /* Return the number of bits at the high-order end of X that are known to
8600 be equal to the sign bit. X will be used in mode MODE; if MODE is
8601 VOIDmode, X will be used in its own mode. The returned value will always
8602 be between 1 and the number of bits in MODE. */
8604 static unsigned int
8605 num_sign_bit_copies1 (rtx x, enum machine_mode mode, rtx known_x,
8606 enum machine_mode known_mode,
8607 unsigned int known_ret)
8609 enum rtx_code code = GET_CODE (x);
8610 unsigned int bitwidth;
8611 int num0, num1, result;
8612 unsigned HOST_WIDE_INT nonzero;
8613 rtx tem;
8615 /* If we weren't given a mode, use the mode of X. If the mode is still
8616 VOIDmode, we don't know anything. Likewise if one of the modes is
8617 floating-point. */
8619 if (mode == VOIDmode)
8620 mode = GET_MODE (x);
8622 if (mode == VOIDmode || FLOAT_MODE_P (mode) || FLOAT_MODE_P (GET_MODE (x)))
8623 return 1;
8625 bitwidth = GET_MODE_BITSIZE (mode);
8627 /* For a smaller object, just ignore the high bits. */
8628 if (bitwidth < GET_MODE_BITSIZE (GET_MODE (x)))
8630 num0 = num_sign_bit_copies_with_known (x, GET_MODE (x));
8631 return MAX (1,
8632 num0 - (int) (GET_MODE_BITSIZE (GET_MODE (x)) - bitwidth));
8635 if (GET_MODE (x) != VOIDmode && bitwidth > GET_MODE_BITSIZE (GET_MODE (x)))
8637 #ifndef WORD_REGISTER_OPERATIONS
8638 /* If this machine does not do all register operations on the entire
8639 register and MODE is wider than the mode of X, we can say nothing
8640 at all about the high-order bits. */
8641 return 1;
8642 #else
8643 /* Likewise on machines that do, if the mode of the object is smaller
8644 than a word and loads of that size don't sign extend, we can say
8645 nothing about the high order bits. */
8646 if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
8647 #ifdef LOAD_EXTEND_OP
8648 && LOAD_EXTEND_OP (GET_MODE (x)) != SIGN_EXTEND
8649 #endif
8651 return 1;
8652 #endif
8655 switch (code)
8657 case REG:
8659 #if defined(POINTERS_EXTEND_UNSIGNED) && !defined(HAVE_ptr_extend)
8660 /* If pointers extend signed and this is a pointer in Pmode, say that
8661 all the bits above ptr_mode are known to be sign bit copies. */
8662 if (! POINTERS_EXTEND_UNSIGNED && GET_MODE (x) == Pmode && mode == Pmode
8663 && REG_POINTER (x))
8664 return GET_MODE_BITSIZE (Pmode) - GET_MODE_BITSIZE (ptr_mode) + 1;
8665 #endif
8667 if (reg_last_set_value[REGNO (x)] != 0
8668 && reg_last_set_mode[REGNO (x)] == mode
8669 && (reg_last_set_label[REGNO (x)] == label_tick
8670 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
8671 && REG_N_SETS (REGNO (x)) == 1
8672 && ! REGNO_REG_SET_P (ENTRY_BLOCK_PTR->next_bb->global_live_at_start,
8673 REGNO (x))))
8674 && INSN_CUID (reg_last_set[REGNO (x)]) < subst_low_cuid)
8675 return reg_last_set_sign_bit_copies[REGNO (x)];
8677 tem = get_last_value (x);
8678 if (tem != 0)
8679 return num_sign_bit_copies_with_known (tem, mode);
8681 if (nonzero_sign_valid && reg_sign_bit_copies[REGNO (x)] != 0
8682 && GET_MODE_BITSIZE (GET_MODE (x)) == bitwidth)
8683 return reg_sign_bit_copies[REGNO (x)];
8684 break;
8686 case MEM:
8687 #ifdef LOAD_EXTEND_OP
8688 /* Some RISC machines sign-extend all loads of smaller than a word. */
8689 if (LOAD_EXTEND_OP (GET_MODE (x)) == SIGN_EXTEND)
8690 return MAX (1, ((int) bitwidth
8691 - (int) GET_MODE_BITSIZE (GET_MODE (x)) + 1));
8692 #endif
8693 break;
8695 case CONST_INT:
8696 /* If the constant is negative, take its 1's complement and remask.
8697 Then see how many zero bits we have. */
8698 nonzero = INTVAL (x) & GET_MODE_MASK (mode);
8699 if (bitwidth <= HOST_BITS_PER_WIDE_INT
8700 && (nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
8701 nonzero = (~nonzero) & GET_MODE_MASK (mode);
8703 return (nonzero == 0 ? bitwidth : bitwidth - floor_log2 (nonzero) - 1);
8705 case SUBREG:
8706 /* If this is a SUBREG for a promoted object that is sign-extended
8707 and we are looking at it in a wider mode, we know that at least the
8708 high-order bits are known to be sign bit copies. */
8710 if (SUBREG_PROMOTED_VAR_P (x) && ! SUBREG_PROMOTED_UNSIGNED_P (x))
8712 num0 = num_sign_bit_copies_with_known (SUBREG_REG (x), mode);
8713 return MAX ((int) bitwidth
8714 - (int) GET_MODE_BITSIZE (GET_MODE (x)) + 1,
8715 num0);
8718 /* For a smaller object, just ignore the high bits. */
8719 if (bitwidth <= GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))))
8721 num0 = num_sign_bit_copies_with_known (SUBREG_REG (x), VOIDmode);
8722 return MAX (1, (num0
8723 - (int) (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x)))
8724 - bitwidth)));
8727 #ifdef WORD_REGISTER_OPERATIONS
8728 #ifdef LOAD_EXTEND_OP
8729 /* For paradoxical SUBREGs on machines where all register operations
8730 affect the entire register, just look inside. Note that we are
8731 passing MODE to the recursive call, so the number of sign bit copies
8732 will remain relative to that mode, not the inner mode. */
8734 /* This works only if loads sign extend. Otherwise, if we get a
8735 reload for the inner part, it may be loaded from the stack, and
8736 then we lose all sign bit copies that existed before the store
8737 to the stack. */
8739 if ((GET_MODE_SIZE (GET_MODE (x))
8740 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
8741 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (x))) == SIGN_EXTEND
8742 && GET_CODE (SUBREG_REG (x)) == MEM)
8743 return num_sign_bit_copies_with_known (SUBREG_REG (x), mode);
8744 #endif
8745 #endif
8746 break;
8748 case SIGN_EXTRACT:
8749 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
8750 return MAX (1, (int) bitwidth - INTVAL (XEXP (x, 1)));
8751 break;
8753 case SIGN_EXTEND:
8754 return (bitwidth - GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
8755 + num_sign_bit_copies_with_known (XEXP (x, 0), VOIDmode));
8757 case TRUNCATE:
8758 /* For a smaller object, just ignore the high bits. */
8759 num0 = num_sign_bit_copies_with_known (XEXP (x, 0), VOIDmode);
8760 return MAX (1, (num0 - (int) (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
8761 - bitwidth)));
8763 case NOT:
8764 return num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8766 case ROTATE: case ROTATERT:
8767 /* If we are rotating left by a number of bits less than the number
8768 of sign bit copies, we can just subtract that amount from the
8769 number. */
8770 if (GET_CODE (XEXP (x, 1)) == CONST_INT
8771 && INTVAL (XEXP (x, 1)) >= 0
8772 && INTVAL (XEXP (x, 1)) < (int) bitwidth)
8774 num0 = num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8775 return MAX (1, num0 - (code == ROTATE ? INTVAL (XEXP (x, 1))
8776 : (int) bitwidth - INTVAL (XEXP (x, 1))));
8778 break;
8780 case NEG:
8781 /* In general, this subtracts one sign bit copy. But if the value
8782 is known to be positive, the number of sign bit copies is the
8783 same as that of the input. Finally, if the input has just one bit
8784 that might be nonzero, all the bits are copies of the sign bit. */
8785 num0 = num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8786 if (bitwidth > HOST_BITS_PER_WIDE_INT)
8787 return num0 > 1 ? num0 - 1 : 1;
8789 nonzero = nonzero_bits (XEXP (x, 0), mode);
8790 if (nonzero == 1)
8791 return bitwidth;
8793 if (num0 > 1
8794 && (((HOST_WIDE_INT) 1 << (bitwidth - 1)) & nonzero))
8795 num0--;
8797 return num0;
8799 case IOR: case AND: case XOR:
8800 case SMIN: case SMAX: case UMIN: case UMAX:
8801 /* Logical operations will preserve the number of sign-bit copies.
8802 MIN and MAX operations always return one of the operands. */
8803 num0 = num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8804 num1 = num_sign_bit_copies_with_known (XEXP (x, 1), mode);
8805 return MIN (num0, num1);
8807 case PLUS: case MINUS:
8808 /* For addition and subtraction, we can have a 1-bit carry. However,
8809 if we are subtracting 1 from a positive number, there will not
8810 be such a carry. Furthermore, if the positive number is known to
8811 be 0 or 1, we know the result is either -1 or 0. */
8813 if (code == PLUS && XEXP (x, 1) == constm1_rtx
8814 && bitwidth <= HOST_BITS_PER_WIDE_INT)
8816 nonzero = nonzero_bits (XEXP (x, 0), mode);
8817 if ((((HOST_WIDE_INT) 1 << (bitwidth - 1)) & nonzero) == 0)
8818 return (nonzero == 1 || nonzero == 0 ? bitwidth
8819 : bitwidth - floor_log2 (nonzero) - 1);
8822 num0 = num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8823 num1 = num_sign_bit_copies_with_known (XEXP (x, 1), mode);
8824 result = MAX (1, MIN (num0, num1) - 1);
8826 #ifdef POINTERS_EXTEND_UNSIGNED
8827 /* If pointers extend signed and this is an addition or subtraction
8828 to a pointer in Pmode, all the bits above ptr_mode are known to be
8829 sign bit copies. */
8830 if (! POINTERS_EXTEND_UNSIGNED && GET_MODE (x) == Pmode
8831 && (code == PLUS || code == MINUS)
8832 && GET_CODE (XEXP (x, 0)) == REG && REG_POINTER (XEXP (x, 0)))
8833 result = MAX ((int) (GET_MODE_BITSIZE (Pmode)
8834 - GET_MODE_BITSIZE (ptr_mode) + 1),
8835 result);
8836 #endif
8837 return result;
8839 case MULT:
8840 /* The number of bits of the product is the sum of the number of
8841 bits of both terms. However, unless one of the terms if known
8842 to be positive, we must allow for an additional bit since negating
8843 a negative number can remove one sign bit copy. */
8845 num0 = num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8846 num1 = num_sign_bit_copies_with_known (XEXP (x, 1), mode);
8848 result = bitwidth - (bitwidth - num0) - (bitwidth - num1);
8849 if (result > 0
8850 && (bitwidth > HOST_BITS_PER_WIDE_INT
8851 || (((nonzero_bits (XEXP (x, 0), mode)
8852 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
8853 && ((nonzero_bits (XEXP (x, 1), mode)
8854 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0))))
8855 result--;
8857 return MAX (1, result);
8859 case UDIV:
8860 /* The result must be <= the first operand. If the first operand
8861 has the high bit set, we know nothing about the number of sign
8862 bit copies. */
8863 if (bitwidth > HOST_BITS_PER_WIDE_INT)
8864 return 1;
8865 else if ((nonzero_bits (XEXP (x, 0), mode)
8866 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
8867 return 1;
8868 else
8869 return num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8871 case UMOD:
8872 /* The result must be <= the second operand. */
8873 return num_sign_bit_copies_with_known (XEXP (x, 1), mode);
8875 case DIV:
8876 /* Similar to unsigned division, except that we have to worry about
8877 the case where the divisor is negative, in which case we have
8878 to add 1. */
8879 result = num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8880 if (result > 1
8881 && (bitwidth > HOST_BITS_PER_WIDE_INT
8882 || (nonzero_bits (XEXP (x, 1), mode)
8883 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0))
8884 result--;
8886 return result;
8888 case MOD:
8889 result = num_sign_bit_copies_with_known (XEXP (x, 1), mode);
8890 if (result > 1
8891 && (bitwidth > HOST_BITS_PER_WIDE_INT
8892 || (nonzero_bits (XEXP (x, 1), mode)
8893 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0))
8894 result--;
8896 return result;
8898 case ASHIFTRT:
8899 /* Shifts by a constant add to the number of bits equal to the
8900 sign bit. */
8901 num0 = num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8902 if (GET_CODE (XEXP (x, 1)) == CONST_INT
8903 && INTVAL (XEXP (x, 1)) > 0)
8904 num0 = MIN ((int) bitwidth, num0 + INTVAL (XEXP (x, 1)));
8906 return num0;
8908 case ASHIFT:
8909 /* Left shifts destroy copies. */
8910 if (GET_CODE (XEXP (x, 1)) != CONST_INT
8911 || INTVAL (XEXP (x, 1)) < 0
8912 || INTVAL (XEXP (x, 1)) >= (int) bitwidth)
8913 return 1;
8915 num0 = num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8916 return MAX (1, num0 - INTVAL (XEXP (x, 1)));
8918 case IF_THEN_ELSE:
8919 num0 = num_sign_bit_copies_with_known (XEXP (x, 1), mode);
8920 num1 = num_sign_bit_copies_with_known (XEXP (x, 2), mode);
8921 return MIN (num0, num1);
8923 case EQ: case NE: case GE: case GT: case LE: case LT:
8924 case UNEQ: case LTGT: case UNGE: case UNGT: case UNLE: case UNLT:
8925 case GEU: case GTU: case LEU: case LTU:
8926 case UNORDERED: case ORDERED:
8927 /* If the constant is negative, take its 1's complement and remask.
8928 Then see how many zero bits we have. */
8929 nonzero = STORE_FLAG_VALUE;
8930 if (bitwidth <= HOST_BITS_PER_WIDE_INT
8931 && (nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
8932 nonzero = (~nonzero) & GET_MODE_MASK (mode);
8934 return (nonzero == 0 ? bitwidth : bitwidth - floor_log2 (nonzero) - 1);
8935 break;
8937 default:
8938 break;
8941 /* If we haven't been able to figure it out by one of the above rules,
8942 see if some of the high-order bits are known to be zero. If so,
8943 count those bits and return one less than that amount. If we can't
8944 safely compute the mask for this mode, always return BITWIDTH. */
8946 if (bitwidth > HOST_BITS_PER_WIDE_INT)
8947 return 1;
8949 nonzero = nonzero_bits (x, mode);
8950 return (nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))
8951 ? 1 : bitwidth - floor_log2 (nonzero) - 1);
8954 /* Return the number of "extended" bits there are in X, when interpreted
8955 as a quantity in MODE whose signedness is indicated by UNSIGNEDP. For
8956 unsigned quantities, this is the number of high-order zero bits.
8957 For signed quantities, this is the number of copies of the sign bit
8958 minus 1. In both case, this function returns the number of "spare"
8959 bits. For example, if two quantities for which this function returns
8960 at least 1 are added, the addition is known not to overflow.
8962 This function will always return 0 unless called during combine, which
8963 implies that it must be called from a define_split. */
8965 unsigned int
8966 extended_count (rtx x, enum machine_mode mode, int unsignedp)
8968 if (nonzero_sign_valid == 0)
8969 return 0;
8971 return (unsignedp
8972 ? (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
8973 ? (unsigned int) (GET_MODE_BITSIZE (mode) - 1
8974 - floor_log2 (nonzero_bits (x, mode)))
8975 : 0)
8976 : num_sign_bit_copies (x, mode) - 1);
8979 /* This function is called from `simplify_shift_const' to merge two
8980 outer operations. Specifically, we have already found that we need
8981 to perform operation *POP0 with constant *PCONST0 at the outermost
8982 position. We would now like to also perform OP1 with constant CONST1
8983 (with *POP0 being done last).
8985 Return 1 if we can do the operation and update *POP0 and *PCONST0 with
8986 the resulting operation. *PCOMP_P is set to 1 if we would need to
8987 complement the innermost operand, otherwise it is unchanged.
8989 MODE is the mode in which the operation will be done. No bits outside
8990 the width of this mode matter. It is assumed that the width of this mode
8991 is smaller than or equal to HOST_BITS_PER_WIDE_INT.
8993 If *POP0 or OP1 are NIL, it means no operation is required. Only NEG, PLUS,
8994 IOR, XOR, and AND are supported. We may set *POP0 to SET if the proper
8995 result is simply *PCONST0.
8997 If the resulting operation cannot be expressed as one operation, we
8998 return 0 and do not change *POP0, *PCONST0, and *PCOMP_P. */
9000 static int
9001 merge_outer_ops (enum rtx_code *pop0, HOST_WIDE_INT *pconst0, enum rtx_code op1, HOST_WIDE_INT const1, enum machine_mode mode, int *pcomp_p)
9003 enum rtx_code op0 = *pop0;
9004 HOST_WIDE_INT const0 = *pconst0;
9006 const0 &= GET_MODE_MASK (mode);
9007 const1 &= GET_MODE_MASK (mode);
9009 /* If OP0 is an AND, clear unimportant bits in CONST1. */
9010 if (op0 == AND)
9011 const1 &= const0;
9013 /* If OP0 or OP1 is NIL, this is easy. Similarly if they are the same or
9014 if OP0 is SET. */
9016 if (op1 == NIL || op0 == SET)
9017 return 1;
9019 else if (op0 == NIL)
9020 op0 = op1, const0 = const1;
9022 else if (op0 == op1)
9024 switch (op0)
9026 case AND:
9027 const0 &= const1;
9028 break;
9029 case IOR:
9030 const0 |= const1;
9031 break;
9032 case XOR:
9033 const0 ^= const1;
9034 break;
9035 case PLUS:
9036 const0 += const1;
9037 break;
9038 case NEG:
9039 op0 = NIL;
9040 break;
9041 default:
9042 break;
9046 /* Otherwise, if either is a PLUS or NEG, we can't do anything. */
9047 else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
9048 return 0;
9050 /* If the two constants aren't the same, we can't do anything. The
9051 remaining six cases can all be done. */
9052 else if (const0 != const1)
9053 return 0;
9055 else
9056 switch (op0)
9058 case IOR:
9059 if (op1 == AND)
9060 /* (a & b) | b == b */
9061 op0 = SET;
9062 else /* op1 == XOR */
9063 /* (a ^ b) | b == a | b */
9065 break;
9067 case XOR:
9068 if (op1 == AND)
9069 /* (a & b) ^ b == (~a) & b */
9070 op0 = AND, *pcomp_p = 1;
9071 else /* op1 == IOR */
9072 /* (a | b) ^ b == a & ~b */
9073 op0 = AND, const0 = ~const0;
9074 break;
9076 case AND:
9077 if (op1 == IOR)
9078 /* (a | b) & b == b */
9079 op0 = SET;
9080 else /* op1 == XOR */
9081 /* (a ^ b) & b) == (~a) & b */
9082 *pcomp_p = 1;
9083 break;
9084 default:
9085 break;
9088 /* Check for NO-OP cases. */
9089 const0 &= GET_MODE_MASK (mode);
9090 if (const0 == 0
9091 && (op0 == IOR || op0 == XOR || op0 == PLUS))
9092 op0 = NIL;
9093 else if (const0 == 0 && op0 == AND)
9094 op0 = SET;
9095 else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
9096 && op0 == AND)
9097 op0 = NIL;
9099 /* ??? Slightly redundant with the above mask, but not entirely.
9100 Moving this above means we'd have to sign-extend the mode mask
9101 for the final test. */
9102 const0 = trunc_int_for_mode (const0, mode);
9104 *pop0 = op0;
9105 *pconst0 = const0;
9107 return 1;
9110 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
9111 The result of the shift is RESULT_MODE. X, if nonzero, is an expression
9112 that we started with.
9114 The shift is normally computed in the widest mode we find in VAROP, as
9115 long as it isn't a different number of words than RESULT_MODE. Exceptions
9116 are ASHIFTRT and ROTATE, which are always done in their original mode, */
9118 static rtx
9119 simplify_shift_const (rtx x, enum rtx_code code,
9120 enum machine_mode result_mode, rtx varop,
9121 int orig_count)
9123 enum rtx_code orig_code = code;
9124 unsigned int count;
9125 int signed_count;
9126 enum machine_mode mode = result_mode;
9127 enum machine_mode shift_mode, tmode;
9128 unsigned int mode_words
9129 = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
9130 /* We form (outer_op (code varop count) (outer_const)). */
9131 enum rtx_code outer_op = NIL;
9132 HOST_WIDE_INT outer_const = 0;
9133 rtx const_rtx;
9134 int complement_p = 0;
9135 rtx new;
9137 /* Make sure and truncate the "natural" shift on the way in. We don't
9138 want to do this inside the loop as it makes it more difficult to
9139 combine shifts. */
9140 if (SHIFT_COUNT_TRUNCATED)
9141 orig_count &= GET_MODE_BITSIZE (mode) - 1;
9143 /* If we were given an invalid count, don't do anything except exactly
9144 what was requested. */
9146 if (orig_count < 0 || orig_count >= (int) GET_MODE_BITSIZE (mode))
9148 if (x)
9149 return x;
9151 return gen_rtx_fmt_ee (code, mode, varop, GEN_INT (orig_count));
9154 count = orig_count;
9156 /* Unless one of the branches of the `if' in this loop does a `continue',
9157 we will `break' the loop after the `if'. */
9159 while (count != 0)
9161 /* If we have an operand of (clobber (const_int 0)), just return that
9162 value. */
9163 if (GET_CODE (varop) == CLOBBER)
9164 return varop;
9166 /* If we discovered we had to complement VAROP, leave. Making a NOT
9167 here would cause an infinite loop. */
9168 if (complement_p)
9169 break;
9171 /* Convert ROTATERT to ROTATE. */
9172 if (code == ROTATERT)
9174 unsigned int bitsize = GET_MODE_BITSIZE (result_mode);;
9175 code = ROTATE;
9176 if (VECTOR_MODE_P (result_mode))
9177 count = bitsize / GET_MODE_NUNITS (result_mode) - count;
9178 else
9179 count = bitsize - count;
9182 /* We need to determine what mode we will do the shift in. If the
9183 shift is a right shift or a ROTATE, we must always do it in the mode
9184 it was originally done in. Otherwise, we can do it in MODE, the
9185 widest mode encountered. */
9186 shift_mode
9187 = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
9188 ? result_mode : mode);
9190 /* Handle cases where the count is greater than the size of the mode
9191 minus 1. For ASHIFT, use the size minus one as the count (this can
9192 occur when simplifying (lshiftrt (ashiftrt ..))). For rotates,
9193 take the count modulo the size. For other shifts, the result is
9194 zero.
9196 Since these shifts are being produced by the compiler by combining
9197 multiple operations, each of which are defined, we know what the
9198 result is supposed to be. */
9200 if (count > (unsigned int) (GET_MODE_BITSIZE (shift_mode) - 1))
9202 if (code == ASHIFTRT)
9203 count = GET_MODE_BITSIZE (shift_mode) - 1;
9204 else if (code == ROTATE || code == ROTATERT)
9205 count %= GET_MODE_BITSIZE (shift_mode);
9206 else
9208 /* We can't simply return zero because there may be an
9209 outer op. */
9210 varop = const0_rtx;
9211 count = 0;
9212 break;
9216 /* An arithmetic right shift of a quantity known to be -1 or 0
9217 is a no-op. */
9218 if (code == ASHIFTRT
9219 && (num_sign_bit_copies (varop, shift_mode)
9220 == GET_MODE_BITSIZE (shift_mode)))
9222 count = 0;
9223 break;
9226 /* If we are doing an arithmetic right shift and discarding all but
9227 the sign bit copies, this is equivalent to doing a shift by the
9228 bitsize minus one. Convert it into that shift because it will often
9229 allow other simplifications. */
9231 if (code == ASHIFTRT
9232 && (count + num_sign_bit_copies (varop, shift_mode)
9233 >= GET_MODE_BITSIZE (shift_mode)))
9234 count = GET_MODE_BITSIZE (shift_mode) - 1;
9236 /* We simplify the tests below and elsewhere by converting
9237 ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
9238 `make_compound_operation' will convert it to an ASHIFTRT for
9239 those machines (such as VAX) that don't have an LSHIFTRT. */
9240 if (GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9241 && code == ASHIFTRT
9242 && ((nonzero_bits (varop, shift_mode)
9243 & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (shift_mode) - 1)))
9244 == 0))
9245 code = LSHIFTRT;
9247 if (code == LSHIFTRT
9248 && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9249 && !(nonzero_bits (varop, shift_mode) >> count))
9250 varop = const0_rtx;
9251 if (code == ASHIFT
9252 && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9253 && !((nonzero_bits (varop, shift_mode) << count)
9254 & GET_MODE_MASK (shift_mode)))
9255 varop = const0_rtx;
9257 switch (GET_CODE (varop))
9259 case SIGN_EXTEND:
9260 case ZERO_EXTEND:
9261 case SIGN_EXTRACT:
9262 case ZERO_EXTRACT:
9263 new = expand_compound_operation (varop);
9264 if (new != varop)
9266 varop = new;
9267 continue;
9269 break;
9271 case MEM:
9272 /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
9273 minus the width of a smaller mode, we can do this with a
9274 SIGN_EXTEND or ZERO_EXTEND from the narrower memory location. */
9275 if ((code == ASHIFTRT || code == LSHIFTRT)
9276 && ! mode_dependent_address_p (XEXP (varop, 0))
9277 && ! MEM_VOLATILE_P (varop)
9278 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
9279 MODE_INT, 1)) != BLKmode)
9281 new = adjust_address_nv (varop, tmode,
9282 BYTES_BIG_ENDIAN ? 0
9283 : count / BITS_PER_UNIT);
9285 varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
9286 : ZERO_EXTEND, mode, new);
9287 count = 0;
9288 continue;
9290 break;
9292 case USE:
9293 /* Similar to the case above, except that we can only do this if
9294 the resulting mode is the same as that of the underlying
9295 MEM and adjust the address depending on the *bits* endianness
9296 because of the way that bit-field extract insns are defined. */
9297 if ((code == ASHIFTRT || code == LSHIFTRT)
9298 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
9299 MODE_INT, 1)) != BLKmode
9300 && tmode == GET_MODE (XEXP (varop, 0)))
9302 if (BITS_BIG_ENDIAN)
9303 new = XEXP (varop, 0);
9304 else
9306 new = copy_rtx (XEXP (varop, 0));
9307 SUBST (XEXP (new, 0),
9308 plus_constant (XEXP (new, 0),
9309 count / BITS_PER_UNIT));
9312 varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
9313 : ZERO_EXTEND, mode, new);
9314 count = 0;
9315 continue;
9317 break;
9319 case SUBREG:
9320 /* If VAROP is a SUBREG, strip it as long as the inner operand has
9321 the same number of words as what we've seen so far. Then store
9322 the widest mode in MODE. */
9323 if (subreg_lowpart_p (varop)
9324 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
9325 > GET_MODE_SIZE (GET_MODE (varop)))
9326 && (unsigned int) ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
9327 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
9328 == mode_words)
9330 varop = SUBREG_REG (varop);
9331 if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
9332 mode = GET_MODE (varop);
9333 continue;
9335 break;
9337 case MULT:
9338 /* Some machines use MULT instead of ASHIFT because MULT
9339 is cheaper. But it is still better on those machines to
9340 merge two shifts into one. */
9341 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9342 && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
9344 varop
9345 = gen_binary (ASHIFT, GET_MODE (varop), XEXP (varop, 0),
9346 GEN_INT (exact_log2 (INTVAL (XEXP (varop, 1)))));
9347 continue;
9349 break;
9351 case UDIV:
9352 /* Similar, for when divides are cheaper. */
9353 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9354 && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
9356 varop
9357 = gen_binary (LSHIFTRT, GET_MODE (varop), XEXP (varop, 0),
9358 GEN_INT (exact_log2 (INTVAL (XEXP (varop, 1)))));
9359 continue;
9361 break;
9363 case ASHIFTRT:
9364 /* If we are extracting just the sign bit of an arithmetic
9365 right shift, that shift is not needed. However, the sign
9366 bit of a wider mode may be different from what would be
9367 interpreted as the sign bit in a narrower mode, so, if
9368 the result is narrower, don't discard the shift. */
9369 if (code == LSHIFTRT
9370 && count == (unsigned int) (GET_MODE_BITSIZE (result_mode) - 1)
9371 && (GET_MODE_BITSIZE (result_mode)
9372 >= GET_MODE_BITSIZE (GET_MODE (varop))))
9374 varop = XEXP (varop, 0);
9375 continue;
9378 /* ... fall through ... */
9380 case LSHIFTRT:
9381 case ASHIFT:
9382 case ROTATE:
9383 /* Here we have two nested shifts. The result is usually the
9384 AND of a new shift with a mask. We compute the result below. */
9385 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9386 && INTVAL (XEXP (varop, 1)) >= 0
9387 && INTVAL (XEXP (varop, 1)) < GET_MODE_BITSIZE (GET_MODE (varop))
9388 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9389 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
9391 enum rtx_code first_code = GET_CODE (varop);
9392 unsigned int first_count = INTVAL (XEXP (varop, 1));
9393 unsigned HOST_WIDE_INT mask;
9394 rtx mask_rtx;
9396 /* We have one common special case. We can't do any merging if
9397 the inner code is an ASHIFTRT of a smaller mode. However, if
9398 we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
9399 with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
9400 we can convert it to
9401 (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0 C2) C3) C1).
9402 This simplifies certain SIGN_EXTEND operations. */
9403 if (code == ASHIFT && first_code == ASHIFTRT
9404 && count == (unsigned int)
9405 (GET_MODE_BITSIZE (result_mode)
9406 - GET_MODE_BITSIZE (GET_MODE (varop))))
9408 /* C3 has the low-order C1 bits zero. */
9410 mask = (GET_MODE_MASK (mode)
9411 & ~(((HOST_WIDE_INT) 1 << first_count) - 1));
9413 varop = simplify_and_const_int (NULL_RTX, result_mode,
9414 XEXP (varop, 0), mask);
9415 varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
9416 varop, count);
9417 count = first_count;
9418 code = ASHIFTRT;
9419 continue;
9422 /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
9423 than C1 high-order bits equal to the sign bit, we can convert
9424 this to either an ASHIFT or an ASHIFTRT depending on the
9425 two counts.
9427 We cannot do this if VAROP's mode is not SHIFT_MODE. */
9429 if (code == ASHIFTRT && first_code == ASHIFT
9430 && GET_MODE (varop) == shift_mode
9431 && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
9432 > first_count))
9434 varop = XEXP (varop, 0);
9436 signed_count = count - first_count;
9437 if (signed_count < 0)
9438 count = -signed_count, code = ASHIFT;
9439 else
9440 count = signed_count;
9442 continue;
9445 /* There are some cases we can't do. If CODE is ASHIFTRT,
9446 we can only do this if FIRST_CODE is also ASHIFTRT.
9448 We can't do the case when CODE is ROTATE and FIRST_CODE is
9449 ASHIFTRT.
9451 If the mode of this shift is not the mode of the outer shift,
9452 we can't do this if either shift is a right shift or ROTATE.
9454 Finally, we can't do any of these if the mode is too wide
9455 unless the codes are the same.
9457 Handle the case where the shift codes are the same
9458 first. */
9460 if (code == first_code)
9462 if (GET_MODE (varop) != result_mode
9463 && (code == ASHIFTRT || code == LSHIFTRT
9464 || code == ROTATE))
9465 break;
9467 count += first_count;
9468 varop = XEXP (varop, 0);
9469 continue;
9472 if (code == ASHIFTRT
9473 || (code == ROTATE && first_code == ASHIFTRT)
9474 || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT
9475 || (GET_MODE (varop) != result_mode
9476 && (first_code == ASHIFTRT || first_code == LSHIFTRT
9477 || first_code == ROTATE
9478 || code == ROTATE)))
9479 break;
9481 /* To compute the mask to apply after the shift, shift the
9482 nonzero bits of the inner shift the same way the
9483 outer shift will. */
9485 mask_rtx = GEN_INT (nonzero_bits (varop, GET_MODE (varop)));
9487 mask_rtx
9488 = simplify_binary_operation (code, result_mode, mask_rtx,
9489 GEN_INT (count));
9491 /* Give up if we can't compute an outer operation to use. */
9492 if (mask_rtx == 0
9493 || GET_CODE (mask_rtx) != CONST_INT
9494 || ! merge_outer_ops (&outer_op, &outer_const, AND,
9495 INTVAL (mask_rtx),
9496 result_mode, &complement_p))
9497 break;
9499 /* If the shifts are in the same direction, we add the
9500 counts. Otherwise, we subtract them. */
9501 signed_count = count;
9502 if ((code == ASHIFTRT || code == LSHIFTRT)
9503 == (first_code == ASHIFTRT || first_code == LSHIFTRT))
9504 signed_count += first_count;
9505 else
9506 signed_count -= first_count;
9508 /* If COUNT is positive, the new shift is usually CODE,
9509 except for the two exceptions below, in which case it is
9510 FIRST_CODE. If the count is negative, FIRST_CODE should
9511 always be used */
9512 if (signed_count > 0
9513 && ((first_code == ROTATE && code == ASHIFT)
9514 || (first_code == ASHIFTRT && code == LSHIFTRT)))
9515 code = first_code, count = signed_count;
9516 else if (signed_count < 0)
9517 code = first_code, count = -signed_count;
9518 else
9519 count = signed_count;
9521 varop = XEXP (varop, 0);
9522 continue;
9525 /* If we have (A << B << C) for any shift, we can convert this to
9526 (A << C << B). This wins if A is a constant. Only try this if
9527 B is not a constant. */
9529 else if (GET_CODE (varop) == code
9530 && GET_CODE (XEXP (varop, 1)) != CONST_INT
9531 && 0 != (new
9532 = simplify_binary_operation (code, mode,
9533 XEXP (varop, 0),
9534 GEN_INT (count))))
9536 varop = gen_rtx_fmt_ee (code, mode, new, XEXP (varop, 1));
9537 count = 0;
9538 continue;
9540 break;
9542 case NOT:
9543 /* Make this fit the case below. */
9544 varop = gen_rtx_XOR (mode, XEXP (varop, 0),
9545 GEN_INT (GET_MODE_MASK (mode)));
9546 continue;
9548 case IOR:
9549 case AND:
9550 case XOR:
9551 /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
9552 with C the size of VAROP - 1 and the shift is logical if
9553 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
9554 we have an (le X 0) operation. If we have an arithmetic shift
9555 and STORE_FLAG_VALUE is 1 or we have a logical shift with
9556 STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation. */
9558 if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
9559 && XEXP (XEXP (varop, 0), 1) == constm1_rtx
9560 && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9561 && (code == LSHIFTRT || code == ASHIFTRT)
9562 && count == (unsigned int)
9563 (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
9564 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
9566 count = 0;
9567 varop = gen_rtx_LE (GET_MODE (varop), XEXP (varop, 1),
9568 const0_rtx);
9570 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
9571 varop = gen_rtx_NEG (GET_MODE (varop), varop);
9573 continue;
9576 /* If we have (shift (logical)), move the logical to the outside
9577 to allow it to possibly combine with another logical and the
9578 shift to combine with another shift. This also canonicalizes to
9579 what a ZERO_EXTRACT looks like. Also, some machines have
9580 (and (shift)) insns. */
9582 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9583 /* We can't do this if we have (ashiftrt (xor)) and the
9584 constant has its sign bit set in shift_mode. */
9585 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
9586 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
9587 shift_mode))
9588 && (new = simplify_binary_operation (code, result_mode,
9589 XEXP (varop, 1),
9590 GEN_INT (count))) != 0
9591 && GET_CODE (new) == CONST_INT
9592 && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
9593 INTVAL (new), result_mode, &complement_p))
9595 varop = XEXP (varop, 0);
9596 continue;
9599 /* If we can't do that, try to simplify the shift in each arm of the
9600 logical expression, make a new logical expression, and apply
9601 the inverse distributive law. This also can't be done
9602 for some (ashiftrt (xor)). */
9603 if (code != ASHIFTRT || GET_CODE (varop)!= XOR
9604 || 0 <= trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
9605 shift_mode))
9607 rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
9608 XEXP (varop, 0), count);
9609 rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
9610 XEXP (varop, 1), count);
9612 varop = gen_binary (GET_CODE (varop), shift_mode, lhs, rhs);
9613 varop = apply_distributive_law (varop);
9615 count = 0;
9617 break;
9619 case EQ:
9620 /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
9621 says that the sign bit can be tested, FOO has mode MODE, C is
9622 GET_MODE_BITSIZE (MODE) - 1, and FOO has only its low-order bit
9623 that may be nonzero. */
9624 if (code == LSHIFTRT
9625 && XEXP (varop, 1) == const0_rtx
9626 && GET_MODE (XEXP (varop, 0)) == result_mode
9627 && count == (unsigned int) (GET_MODE_BITSIZE (result_mode) - 1)
9628 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9629 && ((STORE_FLAG_VALUE
9630 & ((HOST_WIDE_INT) 1
9631 < (GET_MODE_BITSIZE (result_mode) - 1))))
9632 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
9633 && merge_outer_ops (&outer_op, &outer_const, XOR,
9634 (HOST_WIDE_INT) 1, result_mode,
9635 &complement_p))
9637 varop = XEXP (varop, 0);
9638 count = 0;
9639 continue;
9641 break;
9643 case NEG:
9644 /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
9645 than the number of bits in the mode is equivalent to A. */
9646 if (code == LSHIFTRT
9647 && count == (unsigned int) (GET_MODE_BITSIZE (result_mode) - 1)
9648 && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
9650 varop = XEXP (varop, 0);
9651 count = 0;
9652 continue;
9655 /* NEG commutes with ASHIFT since it is multiplication. Move the
9656 NEG outside to allow shifts to combine. */
9657 if (code == ASHIFT
9658 && merge_outer_ops (&outer_op, &outer_const, NEG,
9659 (HOST_WIDE_INT) 0, result_mode,
9660 &complement_p))
9662 varop = XEXP (varop, 0);
9663 continue;
9665 break;
9667 case PLUS:
9668 /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
9669 is one less than the number of bits in the mode is
9670 equivalent to (xor A 1). */
9671 if (code == LSHIFTRT
9672 && count == (unsigned int) (GET_MODE_BITSIZE (result_mode) - 1)
9673 && XEXP (varop, 1) == constm1_rtx
9674 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
9675 && merge_outer_ops (&outer_op, &outer_const, XOR,
9676 (HOST_WIDE_INT) 1, result_mode,
9677 &complement_p))
9679 count = 0;
9680 varop = XEXP (varop, 0);
9681 continue;
9684 /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
9685 that might be nonzero in BAR are those being shifted out and those
9686 bits are known zero in FOO, we can replace the PLUS with FOO.
9687 Similarly in the other operand order. This code occurs when
9688 we are computing the size of a variable-size array. */
9690 if ((code == ASHIFTRT || code == LSHIFTRT)
9691 && count < HOST_BITS_PER_WIDE_INT
9692 && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
9693 && (nonzero_bits (XEXP (varop, 1), result_mode)
9694 & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
9696 varop = XEXP (varop, 0);
9697 continue;
9699 else if ((code == ASHIFTRT || code == LSHIFTRT)
9700 && count < HOST_BITS_PER_WIDE_INT
9701 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9702 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
9703 >> count)
9704 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
9705 & nonzero_bits (XEXP (varop, 1),
9706 result_mode)))
9708 varop = XEXP (varop, 1);
9709 continue;
9712 /* (ashift (plus foo C) N) is (plus (ashift foo N) C'). */
9713 if (code == ASHIFT
9714 && GET_CODE (XEXP (varop, 1)) == CONST_INT
9715 && (new = simplify_binary_operation (ASHIFT, result_mode,
9716 XEXP (varop, 1),
9717 GEN_INT (count))) != 0
9718 && GET_CODE (new) == CONST_INT
9719 && merge_outer_ops (&outer_op, &outer_const, PLUS,
9720 INTVAL (new), result_mode, &complement_p))
9722 varop = XEXP (varop, 0);
9723 continue;
9725 break;
9727 case MINUS:
9728 /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
9729 with C the size of VAROP - 1 and the shift is logical if
9730 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
9731 we have a (gt X 0) operation. If the shift is arithmetic with
9732 STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
9733 we have a (neg (gt X 0)) operation. */
9735 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9736 && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
9737 && count == (unsigned int)
9738 (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
9739 && (code == LSHIFTRT || code == ASHIFTRT)
9740 && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
9741 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (varop, 0), 1))
9742 == count
9743 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
9745 count = 0;
9746 varop = gen_rtx_GT (GET_MODE (varop), XEXP (varop, 1),
9747 const0_rtx);
9749 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
9750 varop = gen_rtx_NEG (GET_MODE (varop), varop);
9752 continue;
9754 break;
9756 case TRUNCATE:
9757 /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
9758 if the truncate does not affect the value. */
9759 if (code == LSHIFTRT
9760 && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
9761 && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
9762 && (INTVAL (XEXP (XEXP (varop, 0), 1))
9763 >= (GET_MODE_BITSIZE (GET_MODE (XEXP (varop, 0)))
9764 - GET_MODE_BITSIZE (GET_MODE (varop)))))
9766 rtx varop_inner = XEXP (varop, 0);
9768 varop_inner
9769 = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
9770 XEXP (varop_inner, 0),
9771 GEN_INT
9772 (count + INTVAL (XEXP (varop_inner, 1))));
9773 varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
9774 count = 0;
9775 continue;
9777 break;
9779 default:
9780 break;
9783 break;
9786 /* We need to determine what mode to do the shift in. If the shift is
9787 a right shift or ROTATE, we must always do it in the mode it was
9788 originally done in. Otherwise, we can do it in MODE, the widest mode
9789 encountered. The code we care about is that of the shift that will
9790 actually be done, not the shift that was originally requested. */
9791 shift_mode
9792 = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
9793 ? result_mode : mode);
9795 /* We have now finished analyzing the shift. The result should be
9796 a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places. If
9797 OUTER_OP is non-NIL, it is an operation that needs to be applied
9798 to the result of the shift. OUTER_CONST is the relevant constant,
9799 but we must turn off all bits turned off in the shift.
9801 If we were passed a value for X, see if we can use any pieces of
9802 it. If not, make new rtx. */
9804 if (x && GET_RTX_CLASS (GET_CODE (x)) == RTX_BIN_ARITH
9805 && GET_CODE (XEXP (x, 1)) == CONST_INT
9806 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (x, 1)) == count)
9807 const_rtx = XEXP (x, 1);
9808 else
9809 const_rtx = GEN_INT (count);
9811 if (x && GET_CODE (XEXP (x, 0)) == SUBREG
9812 && GET_MODE (XEXP (x, 0)) == shift_mode
9813 && SUBREG_REG (XEXP (x, 0)) == varop)
9814 varop = XEXP (x, 0);
9815 else if (GET_MODE (varop) != shift_mode)
9816 varop = gen_lowpart (shift_mode, varop);
9818 /* If we can't make the SUBREG, try to return what we were given. */
9819 if (GET_CODE (varop) == CLOBBER)
9820 return x ? x : varop;
9822 new = simplify_binary_operation (code, shift_mode, varop, const_rtx);
9823 if (new != 0)
9824 x = new;
9825 else
9826 x = gen_rtx_fmt_ee (code, shift_mode, varop, const_rtx);
9828 /* If we have an outer operation and we just made a shift, it is
9829 possible that we could have simplified the shift were it not
9830 for the outer operation. So try to do the simplification
9831 recursively. */
9833 if (outer_op != NIL && GET_CODE (x) == code
9834 && GET_CODE (XEXP (x, 1)) == CONST_INT)
9835 x = simplify_shift_const (x, code, shift_mode, XEXP (x, 0),
9836 INTVAL (XEXP (x, 1)));
9838 /* If we were doing an LSHIFTRT in a wider mode than it was originally,
9839 turn off all the bits that the shift would have turned off. */
9840 if (orig_code == LSHIFTRT && result_mode != shift_mode)
9841 x = simplify_and_const_int (NULL_RTX, shift_mode, x,
9842 GET_MODE_MASK (result_mode) >> orig_count);
9844 /* Do the remainder of the processing in RESULT_MODE. */
9845 x = gen_lowpart (result_mode, x);
9847 /* If COMPLEMENT_P is set, we have to complement X before doing the outer
9848 operation. */
9849 if (complement_p)
9850 x = simplify_gen_unary (NOT, result_mode, x, result_mode);
9852 if (outer_op != NIL)
9854 if (GET_MODE_BITSIZE (result_mode) < HOST_BITS_PER_WIDE_INT)
9855 outer_const = trunc_int_for_mode (outer_const, result_mode);
9857 if (outer_op == AND)
9858 x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
9859 else if (outer_op == SET)
9860 /* This means that we have determined that the result is
9861 equivalent to a constant. This should be rare. */
9862 x = GEN_INT (outer_const);
9863 else if (GET_RTX_CLASS (outer_op) == RTX_UNARY)
9864 x = simplify_gen_unary (outer_op, result_mode, x, result_mode);
9865 else
9866 x = gen_binary (outer_op, result_mode, x, GEN_INT (outer_const));
9869 return x;
9872 /* Like recog, but we receive the address of a pointer to a new pattern.
9873 We try to match the rtx that the pointer points to.
9874 If that fails, we may try to modify or replace the pattern,
9875 storing the replacement into the same pointer object.
9877 Modifications include deletion or addition of CLOBBERs.
9879 PNOTES is a pointer to a location where any REG_UNUSED notes added for
9880 the CLOBBERs are placed.
9882 The value is the final insn code from the pattern ultimately matched,
9883 or -1. */
9885 static int
9886 recog_for_combine (rtx *pnewpat, rtx insn, rtx *pnotes)
9888 rtx pat = *pnewpat;
9889 int insn_code_number;
9890 int num_clobbers_to_add = 0;
9891 int i;
9892 rtx notes = 0;
9893 rtx old_notes, old_pat;
9895 /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
9896 we use to indicate that something didn't match. If we find such a
9897 thing, force rejection. */
9898 if (GET_CODE (pat) == PARALLEL)
9899 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
9900 if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
9901 && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
9902 return -1;
9904 old_pat = PATTERN (insn);
9905 old_notes = REG_NOTES (insn);
9906 PATTERN (insn) = pat;
9907 REG_NOTES (insn) = 0;
9909 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
9911 /* If it isn't, there is the possibility that we previously had an insn
9912 that clobbered some register as a side effect, but the combined
9913 insn doesn't need to do that. So try once more without the clobbers
9914 unless this represents an ASM insn. */
9916 if (insn_code_number < 0 && ! check_asm_operands (pat)
9917 && GET_CODE (pat) == PARALLEL)
9919 int pos;
9921 for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
9922 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
9924 if (i != pos)
9925 SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
9926 pos++;
9929 SUBST_INT (XVECLEN (pat, 0), pos);
9931 if (pos == 1)
9932 pat = XVECEXP (pat, 0, 0);
9934 PATTERN (insn) = pat;
9935 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
9937 PATTERN (insn) = old_pat;
9938 REG_NOTES (insn) = old_notes;
9940 /* Recognize all noop sets, these will be killed by followup pass. */
9941 if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
9942 insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
9944 /* If we had any clobbers to add, make a new pattern than contains
9945 them. Then check to make sure that all of them are dead. */
9946 if (num_clobbers_to_add)
9948 rtx newpat = gen_rtx_PARALLEL (VOIDmode,
9949 rtvec_alloc (GET_CODE (pat) == PARALLEL
9950 ? (XVECLEN (pat, 0)
9951 + num_clobbers_to_add)
9952 : num_clobbers_to_add + 1));
9954 if (GET_CODE (pat) == PARALLEL)
9955 for (i = 0; i < XVECLEN (pat, 0); i++)
9956 XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
9957 else
9958 XVECEXP (newpat, 0, 0) = pat;
9960 add_clobbers (newpat, insn_code_number);
9962 for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
9963 i < XVECLEN (newpat, 0); i++)
9965 if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) == REG
9966 && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
9967 return -1;
9968 notes = gen_rtx_EXPR_LIST (REG_UNUSED,
9969 XEXP (XVECEXP (newpat, 0, i), 0), notes);
9971 pat = newpat;
9974 *pnewpat = pat;
9975 *pnotes = notes;
9977 return insn_code_number;
9980 /* Like gen_lowpart_general but for use by combine. In combine it
9981 is not possible to create any new pseudoregs. However, it is
9982 safe to create invalid memory addresses, because combine will
9983 try to recognize them and all they will do is make the combine
9984 attempt fail.
9986 If for some reason this cannot do its job, an rtx
9987 (clobber (const_int 0)) is returned.
9988 An insn containing that will not be recognized. */
9990 static rtx
9991 gen_lowpart_for_combine (enum machine_mode mode, rtx x)
9993 rtx result;
9995 if (GET_MODE (x) == mode)
9996 return x;
9998 /* Return identity if this is a CONST or symbolic
9999 reference. */
10000 if (mode == Pmode
10001 && (GET_CODE (x) == CONST
10002 || GET_CODE (x) == SYMBOL_REF
10003 || GET_CODE (x) == LABEL_REF))
10004 return x;
10006 /* We can only support MODE being wider than a word if X is a
10007 constant integer or has a mode the same size. */
10009 if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
10010 && ! ((GET_MODE (x) == VOIDmode
10011 && (GET_CODE (x) == CONST_INT
10012 || GET_CODE (x) == CONST_DOUBLE))
10013 || GET_MODE_SIZE (GET_MODE (x)) == GET_MODE_SIZE (mode)))
10014 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
10016 /* X might be a paradoxical (subreg (mem)). In that case, gen_lowpart
10017 won't know what to do. So we will strip off the SUBREG here and
10018 process normally. */
10019 if (GET_CODE (x) == SUBREG && GET_CODE (SUBREG_REG (x)) == MEM)
10021 x = SUBREG_REG (x);
10022 if (GET_MODE (x) == mode)
10023 return x;
10026 result = gen_lowpart_common (mode, x);
10027 #ifdef CANNOT_CHANGE_MODE_CLASS
10028 if (result != 0
10029 && GET_CODE (result) == SUBREG
10030 && GET_CODE (SUBREG_REG (result)) == REG
10031 && REGNO (SUBREG_REG (result)) >= FIRST_PSEUDO_REGISTER)
10032 bitmap_set_bit (&subregs_of_mode, REGNO (SUBREG_REG (result))
10033 * MAX_MACHINE_MODE
10034 + GET_MODE (result));
10035 #endif
10037 if (result)
10038 return result;
10040 if (GET_CODE (x) == MEM)
10042 int offset = 0;
10044 /* Refuse to work on a volatile memory ref or one with a mode-dependent
10045 address. */
10046 if (MEM_VOLATILE_P (x) || mode_dependent_address_p (XEXP (x, 0)))
10047 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
10049 /* If we want to refer to something bigger than the original memref,
10050 generate a paradoxical subreg instead. That will force a reload
10051 of the original memref X. */
10052 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode))
10053 return gen_rtx_SUBREG (mode, x, 0);
10055 if (WORDS_BIG_ENDIAN)
10056 offset = (MAX (GET_MODE_SIZE (GET_MODE (x)), UNITS_PER_WORD)
10057 - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD));
10059 if (BYTES_BIG_ENDIAN)
10061 /* Adjust the address so that the address-after-the-data is
10062 unchanged. */
10063 offset -= (MIN (UNITS_PER_WORD, GET_MODE_SIZE (mode))
10064 - MIN (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (x))));
10067 return adjust_address_nv (x, mode, offset);
10070 /* If X is a comparison operator, rewrite it in a new mode. This
10071 probably won't match, but may allow further simplifications. */
10072 else if (COMPARISON_P (x))
10073 return gen_rtx_fmt_ee (GET_CODE (x), mode, XEXP (x, 0), XEXP (x, 1));
10075 /* If we couldn't simplify X any other way, just enclose it in a
10076 SUBREG. Normally, this SUBREG won't match, but some patterns may
10077 include an explicit SUBREG or we may simplify it further in combine. */
10078 else
10080 int offset = 0;
10081 rtx res;
10082 enum machine_mode sub_mode = GET_MODE (x);
10084 offset = subreg_lowpart_offset (mode, sub_mode);
10085 if (sub_mode == VOIDmode)
10087 sub_mode = int_mode_for_mode (mode);
10088 x = gen_lowpart_common (sub_mode, x);
10089 if (x == 0)
10090 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
10092 res = simplify_gen_subreg (mode, x, sub_mode, offset);
10093 if (res)
10094 return res;
10095 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
10099 /* These routines make binary and unary operations by first seeing if they
10100 fold; if not, a new expression is allocated. */
10102 static rtx
10103 gen_binary (enum rtx_code code, enum machine_mode mode, rtx op0, rtx op1)
10105 rtx result;
10106 rtx tem;
10108 if (GET_CODE (op0) == CLOBBER)
10109 return op0;
10110 else if (GET_CODE (op1) == CLOBBER)
10111 return op1;
10113 if (GET_RTX_CLASS (code) == RTX_COMM_ARITH
10114 && swap_commutative_operands_p (op0, op1))
10115 tem = op0, op0 = op1, op1 = tem;
10117 if (GET_RTX_CLASS (code) == RTX_COMPARE
10118 || GET_RTX_CLASS (code) == RTX_COMM_COMPARE)
10120 enum machine_mode op_mode = GET_MODE (op0);
10122 /* Strip the COMPARE from (REL_OP (compare X Y) 0) to get
10123 just (REL_OP X Y). */
10124 if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
10126 op1 = XEXP (op0, 1);
10127 op0 = XEXP (op0, 0);
10128 op_mode = GET_MODE (op0);
10131 if (op_mode == VOIDmode)
10132 op_mode = GET_MODE (op1);
10133 result = simplify_relational_operation (code, mode, op_mode, op0, op1);
10135 else
10136 result = simplify_binary_operation (code, mode, op0, op1);
10138 if (result)
10139 return result;
10141 /* Put complex operands first and constants second. */
10142 if (GET_RTX_CLASS (code) == RTX_COMM_ARITH
10143 && swap_commutative_operands_p (op0, op1))
10144 return gen_rtx_fmt_ee (code, mode, op1, op0);
10146 /* If we are turning off bits already known off in OP0, we need not do
10147 an AND. */
10148 else if (code == AND && GET_CODE (op1) == CONST_INT
10149 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
10150 && (nonzero_bits (op0, mode) & ~INTVAL (op1)) == 0)
10151 return op0;
10153 return gen_rtx_fmt_ee (code, mode, op0, op1);
10156 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
10157 comparison code that will be tested.
10159 The result is a possibly different comparison code to use. *POP0 and
10160 *POP1 may be updated.
10162 It is possible that we might detect that a comparison is either always
10163 true or always false. However, we do not perform general constant
10164 folding in combine, so this knowledge isn't useful. Such tautologies
10165 should have been detected earlier. Hence we ignore all such cases. */
10167 static enum rtx_code
10168 simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
10170 rtx op0 = *pop0;
10171 rtx op1 = *pop1;
10172 rtx tem, tem1;
10173 int i;
10174 enum machine_mode mode, tmode;
10176 /* Try a few ways of applying the same transformation to both operands. */
10177 while (1)
10179 #ifndef WORD_REGISTER_OPERATIONS
10180 /* The test below this one won't handle SIGN_EXTENDs on these machines,
10181 so check specially. */
10182 if (code != GTU && code != GEU && code != LTU && code != LEU
10183 && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
10184 && GET_CODE (XEXP (op0, 0)) == ASHIFT
10185 && GET_CODE (XEXP (op1, 0)) == ASHIFT
10186 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
10187 && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
10188 && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
10189 == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
10190 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10191 && XEXP (op0, 1) == XEXP (op1, 1)
10192 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
10193 && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
10194 && (INTVAL (XEXP (op0, 1))
10195 == (GET_MODE_BITSIZE (GET_MODE (op0))
10196 - (GET_MODE_BITSIZE
10197 (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
10199 op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
10200 op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
10202 #endif
10204 /* If both operands are the same constant shift, see if we can ignore the
10205 shift. We can if the shift is a rotate or if the bits shifted out of
10206 this shift are known to be zero for both inputs and if the type of
10207 comparison is compatible with the shift. */
10208 if (GET_CODE (op0) == GET_CODE (op1)
10209 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
10210 && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
10211 || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
10212 && (code != GT && code != LT && code != GE && code != LE))
10213 || (GET_CODE (op0) == ASHIFTRT
10214 && (code != GTU && code != LTU
10215 && code != GEU && code != LEU)))
10216 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10217 && INTVAL (XEXP (op0, 1)) >= 0
10218 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
10219 && XEXP (op0, 1) == XEXP (op1, 1))
10221 enum machine_mode mode = GET_MODE (op0);
10222 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
10223 int shift_count = INTVAL (XEXP (op0, 1));
10225 if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
10226 mask &= (mask >> shift_count) << shift_count;
10227 else if (GET_CODE (op0) == ASHIFT)
10228 mask = (mask & (mask << shift_count)) >> shift_count;
10230 if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
10231 && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
10232 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
10233 else
10234 break;
10237 /* If both operands are AND's of a paradoxical SUBREG by constant, the
10238 SUBREGs are of the same mode, and, in both cases, the AND would
10239 be redundant if the comparison was done in the narrower mode,
10240 do the comparison in the narrower mode (e.g., we are AND'ing with 1
10241 and the operand's possibly nonzero bits are 0xffffff01; in that case
10242 if we only care about QImode, we don't need the AND). This case
10243 occurs if the output mode of an scc insn is not SImode and
10244 STORE_FLAG_VALUE == 1 (e.g., the 386).
10246 Similarly, check for a case where the AND's are ZERO_EXTEND
10247 operations from some narrower mode even though a SUBREG is not
10248 present. */
10250 else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
10251 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10252 && GET_CODE (XEXP (op1, 1)) == CONST_INT)
10254 rtx inner_op0 = XEXP (op0, 0);
10255 rtx inner_op1 = XEXP (op1, 0);
10256 HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
10257 HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
10258 int changed = 0;
10260 if (GET_CODE (inner_op0) == SUBREG && GET_CODE (inner_op1) == SUBREG
10261 && (GET_MODE_SIZE (GET_MODE (inner_op0))
10262 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (inner_op0))))
10263 && (GET_MODE (SUBREG_REG (inner_op0))
10264 == GET_MODE (SUBREG_REG (inner_op1)))
10265 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (inner_op0)))
10266 <= HOST_BITS_PER_WIDE_INT)
10267 && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
10268 GET_MODE (SUBREG_REG (inner_op0)))))
10269 && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
10270 GET_MODE (SUBREG_REG (inner_op1))))))
10272 op0 = SUBREG_REG (inner_op0);
10273 op1 = SUBREG_REG (inner_op1);
10275 /* The resulting comparison is always unsigned since we masked
10276 off the original sign bit. */
10277 code = unsigned_condition (code);
10279 changed = 1;
10282 else if (c0 == c1)
10283 for (tmode = GET_CLASS_NARROWEST_MODE
10284 (GET_MODE_CLASS (GET_MODE (op0)));
10285 tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
10286 if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
10288 op0 = gen_lowpart (tmode, inner_op0);
10289 op1 = gen_lowpart (tmode, inner_op1);
10290 code = unsigned_condition (code);
10291 changed = 1;
10292 break;
10295 if (! changed)
10296 break;
10299 /* If both operands are NOT, we can strip off the outer operation
10300 and adjust the comparison code for swapped operands; similarly for
10301 NEG, except that this must be an equality comparison. */
10302 else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
10303 || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
10304 && (code == EQ || code == NE)))
10305 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
10307 else
10308 break;
10311 /* If the first operand is a constant, swap the operands and adjust the
10312 comparison code appropriately, but don't do this if the second operand
10313 is already a constant integer. */
10314 if (swap_commutative_operands_p (op0, op1))
10316 tem = op0, op0 = op1, op1 = tem;
10317 code = swap_condition (code);
10320 /* We now enter a loop during which we will try to simplify the comparison.
10321 For the most part, we only are concerned with comparisons with zero,
10322 but some things may really be comparisons with zero but not start
10323 out looking that way. */
10325 while (GET_CODE (op1) == CONST_INT)
10327 enum machine_mode mode = GET_MODE (op0);
10328 unsigned int mode_width = GET_MODE_BITSIZE (mode);
10329 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
10330 int equality_comparison_p;
10331 int sign_bit_comparison_p;
10332 int unsigned_comparison_p;
10333 HOST_WIDE_INT const_op;
10335 /* We only want to handle integral modes. This catches VOIDmode,
10336 CCmode, and the floating-point modes. An exception is that we
10337 can handle VOIDmode if OP0 is a COMPARE or a comparison
10338 operation. */
10340 if (GET_MODE_CLASS (mode) != MODE_INT
10341 && ! (mode == VOIDmode
10342 && (GET_CODE (op0) == COMPARE || COMPARISON_P (op0))))
10343 break;
10345 /* Get the constant we are comparing against and turn off all bits
10346 not on in our mode. */
10347 const_op = INTVAL (op1);
10348 if (mode != VOIDmode)
10349 const_op = trunc_int_for_mode (const_op, mode);
10350 op1 = GEN_INT (const_op);
10352 /* If we are comparing against a constant power of two and the value
10353 being compared can only have that single bit nonzero (e.g., it was
10354 `and'ed with that bit), we can replace this with a comparison
10355 with zero. */
10356 if (const_op
10357 && (code == EQ || code == NE || code == GE || code == GEU
10358 || code == LT || code == LTU)
10359 && mode_width <= HOST_BITS_PER_WIDE_INT
10360 && exact_log2 (const_op) >= 0
10361 && nonzero_bits (op0, mode) == (unsigned HOST_WIDE_INT) const_op)
10363 code = (code == EQ || code == GE || code == GEU ? NE : EQ);
10364 op1 = const0_rtx, const_op = 0;
10367 /* Similarly, if we are comparing a value known to be either -1 or
10368 0 with -1, change it to the opposite comparison against zero. */
10370 if (const_op == -1
10371 && (code == EQ || code == NE || code == GT || code == LE
10372 || code == GEU || code == LTU)
10373 && num_sign_bit_copies (op0, mode) == mode_width)
10375 code = (code == EQ || code == LE || code == GEU ? NE : EQ);
10376 op1 = const0_rtx, const_op = 0;
10379 /* Do some canonicalizations based on the comparison code. We prefer
10380 comparisons against zero and then prefer equality comparisons.
10381 If we can reduce the size of a constant, we will do that too. */
10383 switch (code)
10385 case LT:
10386 /* < C is equivalent to <= (C - 1) */
10387 if (const_op > 0)
10389 const_op -= 1;
10390 op1 = GEN_INT (const_op);
10391 code = LE;
10392 /* ... fall through to LE case below. */
10394 else
10395 break;
10397 case LE:
10398 /* <= C is equivalent to < (C + 1); we do this for C < 0 */
10399 if (const_op < 0)
10401 const_op += 1;
10402 op1 = GEN_INT (const_op);
10403 code = LT;
10406 /* If we are doing a <= 0 comparison on a value known to have
10407 a zero sign bit, we can replace this with == 0. */
10408 else if (const_op == 0
10409 && mode_width <= HOST_BITS_PER_WIDE_INT
10410 && (nonzero_bits (op0, mode)
10411 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
10412 code = EQ;
10413 break;
10415 case GE:
10416 /* >= C is equivalent to > (C - 1). */
10417 if (const_op > 0)
10419 const_op -= 1;
10420 op1 = GEN_INT (const_op);
10421 code = GT;
10422 /* ... fall through to GT below. */
10424 else
10425 break;
10427 case GT:
10428 /* > C is equivalent to >= (C + 1); we do this for C < 0. */
10429 if (const_op < 0)
10431 const_op += 1;
10432 op1 = GEN_INT (const_op);
10433 code = GE;
10436 /* If we are doing a > 0 comparison on a value known to have
10437 a zero sign bit, we can replace this with != 0. */
10438 else if (const_op == 0
10439 && mode_width <= HOST_BITS_PER_WIDE_INT
10440 && (nonzero_bits (op0, mode)
10441 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
10442 code = NE;
10443 break;
10445 case LTU:
10446 /* < C is equivalent to <= (C - 1). */
10447 if (const_op > 0)
10449 const_op -= 1;
10450 op1 = GEN_INT (const_op);
10451 code = LEU;
10452 /* ... fall through ... */
10455 /* (unsigned) < 0x80000000 is equivalent to >= 0. */
10456 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10457 && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
10459 const_op = 0, op1 = const0_rtx;
10460 code = GE;
10461 break;
10463 else
10464 break;
10466 case LEU:
10467 /* unsigned <= 0 is equivalent to == 0 */
10468 if (const_op == 0)
10469 code = EQ;
10471 /* (unsigned) <= 0x7fffffff is equivalent to >= 0. */
10472 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10473 && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
10475 const_op = 0, op1 = const0_rtx;
10476 code = GE;
10478 break;
10480 case GEU:
10481 /* >= C is equivalent to < (C - 1). */
10482 if (const_op > 1)
10484 const_op -= 1;
10485 op1 = GEN_INT (const_op);
10486 code = GTU;
10487 /* ... fall through ... */
10490 /* (unsigned) >= 0x80000000 is equivalent to < 0. */
10491 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10492 && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
10494 const_op = 0, op1 = const0_rtx;
10495 code = LT;
10496 break;
10498 else
10499 break;
10501 case GTU:
10502 /* unsigned > 0 is equivalent to != 0 */
10503 if (const_op == 0)
10504 code = NE;
10506 /* (unsigned) > 0x7fffffff is equivalent to < 0. */
10507 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10508 && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
10510 const_op = 0, op1 = const0_rtx;
10511 code = LT;
10513 break;
10515 default:
10516 break;
10519 /* Compute some predicates to simplify code below. */
10521 equality_comparison_p = (code == EQ || code == NE);
10522 sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
10523 unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
10524 || code == GEU);
10526 /* If this is a sign bit comparison and we can do arithmetic in
10527 MODE, say that we will only be needing the sign bit of OP0. */
10528 if (sign_bit_comparison_p
10529 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10530 op0 = force_to_mode (op0, mode,
10531 ((HOST_WIDE_INT) 1
10532 << (GET_MODE_BITSIZE (mode) - 1)),
10533 NULL_RTX, 0);
10535 /* Now try cases based on the opcode of OP0. If none of the cases
10536 does a "continue", we exit this loop immediately after the
10537 switch. */
10539 switch (GET_CODE (op0))
10541 case ZERO_EXTRACT:
10542 /* If we are extracting a single bit from a variable position in
10543 a constant that has only a single bit set and are comparing it
10544 with zero, we can convert this into an equality comparison
10545 between the position and the location of the single bit. */
10546 /* Except we can't if SHIFT_COUNT_TRUNCATED is set, since we might
10547 have already reduced the shift count modulo the word size. */
10548 if (!SHIFT_COUNT_TRUNCATED
10549 && GET_CODE (XEXP (op0, 0)) == CONST_INT
10550 && XEXP (op0, 1) == const1_rtx
10551 && equality_comparison_p && const_op == 0
10552 && (i = exact_log2 (INTVAL (XEXP (op0, 0)))) >= 0)
10554 if (BITS_BIG_ENDIAN)
10556 enum machine_mode new_mode
10557 = mode_for_extraction (EP_extzv, 1);
10558 if (new_mode == MAX_MACHINE_MODE)
10559 i = BITS_PER_WORD - 1 - i;
10560 else
10562 mode = new_mode;
10563 i = (GET_MODE_BITSIZE (mode) - 1 - i);
10567 op0 = XEXP (op0, 2);
10568 op1 = GEN_INT (i);
10569 const_op = i;
10571 /* Result is nonzero iff shift count is equal to I. */
10572 code = reverse_condition (code);
10573 continue;
10576 /* ... fall through ... */
10578 case SIGN_EXTRACT:
10579 tem = expand_compound_operation (op0);
10580 if (tem != op0)
10582 op0 = tem;
10583 continue;
10585 break;
10587 case NOT:
10588 /* If testing for equality, we can take the NOT of the constant. */
10589 if (equality_comparison_p
10590 && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
10592 op0 = XEXP (op0, 0);
10593 op1 = tem;
10594 continue;
10597 /* If just looking at the sign bit, reverse the sense of the
10598 comparison. */
10599 if (sign_bit_comparison_p)
10601 op0 = XEXP (op0, 0);
10602 code = (code == GE ? LT : GE);
10603 continue;
10605 break;
10607 case NEG:
10608 /* If testing for equality, we can take the NEG of the constant. */
10609 if (equality_comparison_p
10610 && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
10612 op0 = XEXP (op0, 0);
10613 op1 = tem;
10614 continue;
10617 /* The remaining cases only apply to comparisons with zero. */
10618 if (const_op != 0)
10619 break;
10621 /* When X is ABS or is known positive,
10622 (neg X) is < 0 if and only if X != 0. */
10624 if (sign_bit_comparison_p
10625 && (GET_CODE (XEXP (op0, 0)) == ABS
10626 || (mode_width <= HOST_BITS_PER_WIDE_INT
10627 && (nonzero_bits (XEXP (op0, 0), mode)
10628 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)))
10630 op0 = XEXP (op0, 0);
10631 code = (code == LT ? NE : EQ);
10632 continue;
10635 /* If we have NEG of something whose two high-order bits are the
10636 same, we know that "(-a) < 0" is equivalent to "a > 0". */
10637 if (num_sign_bit_copies (op0, mode) >= 2)
10639 op0 = XEXP (op0, 0);
10640 code = swap_condition (code);
10641 continue;
10643 break;
10645 case ROTATE:
10646 /* If we are testing equality and our count is a constant, we
10647 can perform the inverse operation on our RHS. */
10648 if (equality_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
10649 && (tem = simplify_binary_operation (ROTATERT, mode,
10650 op1, XEXP (op0, 1))) != 0)
10652 op0 = XEXP (op0, 0);
10653 op1 = tem;
10654 continue;
10657 /* If we are doing a < 0 or >= 0 comparison, it means we are testing
10658 a particular bit. Convert it to an AND of a constant of that
10659 bit. This will be converted into a ZERO_EXTRACT. */
10660 if (const_op == 0 && sign_bit_comparison_p
10661 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10662 && mode_width <= HOST_BITS_PER_WIDE_INT)
10664 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10665 ((HOST_WIDE_INT) 1
10666 << (mode_width - 1
10667 - INTVAL (XEXP (op0, 1)))));
10668 code = (code == LT ? NE : EQ);
10669 continue;
10672 /* Fall through. */
10674 case ABS:
10675 /* ABS is ignorable inside an equality comparison with zero. */
10676 if (const_op == 0 && equality_comparison_p)
10678 op0 = XEXP (op0, 0);
10679 continue;
10681 break;
10683 case SIGN_EXTEND:
10684 /* Can simplify (compare (zero/sign_extend FOO) CONST)
10685 to (compare FOO CONST) if CONST fits in FOO's mode and we
10686 are either testing inequality or have an unsigned comparison
10687 with ZERO_EXTEND or a signed comparison with SIGN_EXTEND. */
10688 if (! unsigned_comparison_p
10689 && (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10690 <= HOST_BITS_PER_WIDE_INT)
10691 && ((unsigned HOST_WIDE_INT) const_op
10692 < (((unsigned HOST_WIDE_INT) 1
10693 << (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))) - 1)))))
10695 op0 = XEXP (op0, 0);
10696 continue;
10698 break;
10700 case SUBREG:
10701 /* Check for the case where we are comparing A - C1 with C2,
10702 both constants are smaller than 1/2 the maximum positive
10703 value in MODE, and the comparison is equality or unsigned.
10704 In that case, if A is either zero-extended to MODE or has
10705 sufficient sign bits so that the high-order bit in MODE
10706 is a copy of the sign in the inner mode, we can prove that it is
10707 safe to do the operation in the wider mode. This simplifies
10708 many range checks. */
10710 if (mode_width <= HOST_BITS_PER_WIDE_INT
10711 && subreg_lowpart_p (op0)
10712 && GET_CODE (SUBREG_REG (op0)) == PLUS
10713 && GET_CODE (XEXP (SUBREG_REG (op0), 1)) == CONST_INT
10714 && INTVAL (XEXP (SUBREG_REG (op0), 1)) < 0
10715 && (-INTVAL (XEXP (SUBREG_REG (op0), 1))
10716 < (HOST_WIDE_INT) (GET_MODE_MASK (mode) / 2))
10717 && (unsigned HOST_WIDE_INT) const_op < GET_MODE_MASK (mode) / 2
10718 && (0 == (nonzero_bits (XEXP (SUBREG_REG (op0), 0),
10719 GET_MODE (SUBREG_REG (op0)))
10720 & ~GET_MODE_MASK (mode))
10721 || (num_sign_bit_copies (XEXP (SUBREG_REG (op0), 0),
10722 GET_MODE (SUBREG_REG (op0)))
10723 > (unsigned int)
10724 (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
10725 - GET_MODE_BITSIZE (mode)))))
10727 op0 = SUBREG_REG (op0);
10728 continue;
10731 /* If the inner mode is narrower and we are extracting the low part,
10732 we can treat the SUBREG as if it were a ZERO_EXTEND. */
10733 if (subreg_lowpart_p (op0)
10734 && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) < mode_width)
10735 /* Fall through */ ;
10736 else
10737 break;
10739 /* ... fall through ... */
10741 case ZERO_EXTEND:
10742 if ((unsigned_comparison_p || equality_comparison_p)
10743 && (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10744 <= HOST_BITS_PER_WIDE_INT)
10745 && ((unsigned HOST_WIDE_INT) const_op
10746 < GET_MODE_MASK (GET_MODE (XEXP (op0, 0)))))
10748 op0 = XEXP (op0, 0);
10749 continue;
10751 break;
10753 case PLUS:
10754 /* (eq (plus X A) B) -> (eq X (minus B A)). We can only do
10755 this for equality comparisons due to pathological cases involving
10756 overflows. */
10757 if (equality_comparison_p
10758 && 0 != (tem = simplify_binary_operation (MINUS, mode,
10759 op1, XEXP (op0, 1))))
10761 op0 = XEXP (op0, 0);
10762 op1 = tem;
10763 continue;
10766 /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0. */
10767 if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
10768 && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
10770 op0 = XEXP (XEXP (op0, 0), 0);
10771 code = (code == LT ? EQ : NE);
10772 continue;
10774 break;
10776 case MINUS:
10777 /* We used to optimize signed comparisons against zero, but that
10778 was incorrect. Unsigned comparisons against zero (GTU, LEU)
10779 arrive here as equality comparisons, or (GEU, LTU) are
10780 optimized away. No need to special-case them. */
10782 /* (eq (minus A B) C) -> (eq A (plus B C)) or
10783 (eq B (minus A C)), whichever simplifies. We can only do
10784 this for equality comparisons due to pathological cases involving
10785 overflows. */
10786 if (equality_comparison_p
10787 && 0 != (tem = simplify_binary_operation (PLUS, mode,
10788 XEXP (op0, 1), op1)))
10790 op0 = XEXP (op0, 0);
10791 op1 = tem;
10792 continue;
10795 if (equality_comparison_p
10796 && 0 != (tem = simplify_binary_operation (MINUS, mode,
10797 XEXP (op0, 0), op1)))
10799 op0 = XEXP (op0, 1);
10800 op1 = tem;
10801 continue;
10804 /* The sign bit of (minus (ashiftrt X C) X), where C is the number
10805 of bits in X minus 1, is one iff X > 0. */
10806 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
10807 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10808 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (op0, 0), 1))
10809 == mode_width - 1
10810 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10812 op0 = XEXP (op0, 1);
10813 code = (code == GE ? LE : GT);
10814 continue;
10816 break;
10818 case XOR:
10819 /* (eq (xor A B) C) -> (eq A (xor B C)). This is a simplification
10820 if C is zero or B is a constant. */
10821 if (equality_comparison_p
10822 && 0 != (tem = simplify_binary_operation (XOR, mode,
10823 XEXP (op0, 1), op1)))
10825 op0 = XEXP (op0, 0);
10826 op1 = tem;
10827 continue;
10829 break;
10831 case EQ: case NE:
10832 case UNEQ: case LTGT:
10833 case LT: case LTU: case UNLT: case LE: case LEU: case UNLE:
10834 case GT: case GTU: case UNGT: case GE: case GEU: case UNGE:
10835 case UNORDERED: case ORDERED:
10836 /* We can't do anything if OP0 is a condition code value, rather
10837 than an actual data value. */
10838 if (const_op != 0
10839 || CC0_P (XEXP (op0, 0))
10840 || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
10841 break;
10843 /* Get the two operands being compared. */
10844 if (GET_CODE (XEXP (op0, 0)) == COMPARE)
10845 tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
10846 else
10847 tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
10849 /* Check for the cases where we simply want the result of the
10850 earlier test or the opposite of that result. */
10851 if (code == NE || code == EQ
10852 || (GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
10853 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10854 && (STORE_FLAG_VALUE
10855 & (((HOST_WIDE_INT) 1
10856 << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
10857 && (code == LT || code == GE)))
10859 enum rtx_code new_code;
10860 if (code == LT || code == NE)
10861 new_code = GET_CODE (op0);
10862 else
10863 new_code = combine_reversed_comparison_code (op0);
10865 if (new_code != UNKNOWN)
10867 code = new_code;
10868 op0 = tem;
10869 op1 = tem1;
10870 continue;
10873 break;
10875 case IOR:
10876 /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
10877 iff X <= 0. */
10878 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
10879 && XEXP (XEXP (op0, 0), 1) == constm1_rtx
10880 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10882 op0 = XEXP (op0, 1);
10883 code = (code == GE ? GT : LE);
10884 continue;
10886 break;
10888 case AND:
10889 /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1). This
10890 will be converted to a ZERO_EXTRACT later. */
10891 if (const_op == 0 && equality_comparison_p
10892 && GET_CODE (XEXP (op0, 0)) == ASHIFT
10893 && XEXP (XEXP (op0, 0), 0) == const1_rtx)
10895 op0 = simplify_and_const_int
10896 (op0, mode, gen_rtx_LSHIFTRT (mode,
10897 XEXP (op0, 1),
10898 XEXP (XEXP (op0, 0), 1)),
10899 (HOST_WIDE_INT) 1);
10900 continue;
10903 /* If we are comparing (and (lshiftrt X C1) C2) for equality with
10904 zero and X is a comparison and C1 and C2 describe only bits set
10905 in STORE_FLAG_VALUE, we can compare with X. */
10906 if (const_op == 0 && equality_comparison_p
10907 && mode_width <= HOST_BITS_PER_WIDE_INT
10908 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10909 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
10910 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10911 && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
10912 && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
10914 mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
10915 << INTVAL (XEXP (XEXP (op0, 0), 1)));
10916 if ((~STORE_FLAG_VALUE & mask) == 0
10917 && (COMPARISON_P (XEXP (XEXP (op0, 0), 0))
10918 || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
10919 && COMPARISON_P (tem))))
10921 op0 = XEXP (XEXP (op0, 0), 0);
10922 continue;
10926 /* If we are doing an equality comparison of an AND of a bit equal
10927 to the sign bit, replace this with a LT or GE comparison of
10928 the underlying value. */
10929 if (equality_comparison_p
10930 && const_op == 0
10931 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10932 && mode_width <= HOST_BITS_PER_WIDE_INT
10933 && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
10934 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
10936 op0 = XEXP (op0, 0);
10937 code = (code == EQ ? GE : LT);
10938 continue;
10941 /* If this AND operation is really a ZERO_EXTEND from a narrower
10942 mode, the constant fits within that mode, and this is either an
10943 equality or unsigned comparison, try to do this comparison in
10944 the narrower mode. */
10945 if ((equality_comparison_p || unsigned_comparison_p)
10946 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10947 && (i = exact_log2 ((INTVAL (XEXP (op0, 1))
10948 & GET_MODE_MASK (mode))
10949 + 1)) >= 0
10950 && const_op >> i == 0
10951 && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode)
10953 op0 = gen_lowpart (tmode, XEXP (op0, 0));
10954 continue;
10957 /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1
10958 fits in both M1 and M2 and the SUBREG is either paradoxical
10959 or represents the low part, permute the SUBREG and the AND
10960 and try again. */
10961 if (GET_CODE (XEXP (op0, 0)) == SUBREG)
10963 unsigned HOST_WIDE_INT c1;
10964 tmode = GET_MODE (SUBREG_REG (XEXP (op0, 0)));
10965 /* Require an integral mode, to avoid creating something like
10966 (AND:SF ...). */
10967 if (SCALAR_INT_MODE_P (tmode)
10968 /* It is unsafe to commute the AND into the SUBREG if the
10969 SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
10970 not defined. As originally written the upper bits
10971 have a defined value due to the AND operation.
10972 However, if we commute the AND inside the SUBREG then
10973 they no longer have defined values and the meaning of
10974 the code has been changed. */
10975 && (0
10976 #ifdef WORD_REGISTER_OPERATIONS
10977 || (mode_width > GET_MODE_BITSIZE (tmode)
10978 && mode_width <= BITS_PER_WORD)
10979 #endif
10980 || (mode_width <= GET_MODE_BITSIZE (tmode)
10981 && subreg_lowpart_p (XEXP (op0, 0))))
10982 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10983 && mode_width <= HOST_BITS_PER_WIDE_INT
10984 && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
10985 && ((c1 = INTVAL (XEXP (op0, 1))) & ~mask) == 0
10986 && (c1 & ~GET_MODE_MASK (tmode)) == 0
10987 && c1 != mask
10988 && c1 != GET_MODE_MASK (tmode))
10990 op0 = gen_binary (AND, tmode,
10991 SUBREG_REG (XEXP (op0, 0)),
10992 gen_int_mode (c1, tmode));
10993 op0 = gen_lowpart (mode, op0);
10994 continue;
10998 /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0). */
10999 if (const_op == 0 && equality_comparison_p
11000 && XEXP (op0, 1) == const1_rtx
11001 && GET_CODE (XEXP (op0, 0)) == NOT)
11003 op0 = simplify_and_const_int
11004 (NULL_RTX, mode, XEXP (XEXP (op0, 0), 0), (HOST_WIDE_INT) 1);
11005 code = (code == NE ? EQ : NE);
11006 continue;
11009 /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
11010 (eq (and (lshiftrt X) 1) 0).
11011 Also handle the case where (not X) is expressed using xor. */
11012 if (const_op == 0 && equality_comparison_p
11013 && XEXP (op0, 1) == const1_rtx
11014 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT)
11016 rtx shift_op = XEXP (XEXP (op0, 0), 0);
11017 rtx shift_count = XEXP (XEXP (op0, 0), 1);
11019 if (GET_CODE (shift_op) == NOT
11020 || (GET_CODE (shift_op) == XOR
11021 && GET_CODE (XEXP (shift_op, 1)) == CONST_INT
11022 && GET_CODE (shift_count) == CONST_INT
11023 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
11024 && (INTVAL (XEXP (shift_op, 1))
11025 == (HOST_WIDE_INT) 1 << INTVAL (shift_count))))
11027 op0 = simplify_and_const_int
11028 (NULL_RTX, mode,
11029 gen_rtx_LSHIFTRT (mode, XEXP (shift_op, 0), shift_count),
11030 (HOST_WIDE_INT) 1);
11031 code = (code == NE ? EQ : NE);
11032 continue;
11035 break;
11037 case ASHIFT:
11038 /* If we have (compare (ashift FOO N) (const_int C)) and
11039 the high order N bits of FOO (N+1 if an inequality comparison)
11040 are known to be zero, we can do this by comparing FOO with C
11041 shifted right N bits so long as the low-order N bits of C are
11042 zero. */
11043 if (GET_CODE (XEXP (op0, 1)) == CONST_INT
11044 && INTVAL (XEXP (op0, 1)) >= 0
11045 && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
11046 < HOST_BITS_PER_WIDE_INT)
11047 && ((const_op
11048 & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0)
11049 && mode_width <= HOST_BITS_PER_WIDE_INT
11050 && (nonzero_bits (XEXP (op0, 0), mode)
11051 & ~(mask >> (INTVAL (XEXP (op0, 1))
11052 + ! equality_comparison_p))) == 0)
11054 /* We must perform a logical shift, not an arithmetic one,
11055 as we want the top N bits of C to be zero. */
11056 unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
11058 temp >>= INTVAL (XEXP (op0, 1));
11059 op1 = gen_int_mode (temp, mode);
11060 op0 = XEXP (op0, 0);
11061 continue;
11064 /* If we are doing a sign bit comparison, it means we are testing
11065 a particular bit. Convert it to the appropriate AND. */
11066 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
11067 && mode_width <= HOST_BITS_PER_WIDE_INT)
11069 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11070 ((HOST_WIDE_INT) 1
11071 << (mode_width - 1
11072 - INTVAL (XEXP (op0, 1)))));
11073 code = (code == LT ? NE : EQ);
11074 continue;
11077 /* If this an equality comparison with zero and we are shifting
11078 the low bit to the sign bit, we can convert this to an AND of the
11079 low-order bit. */
11080 if (const_op == 0 && equality_comparison_p
11081 && GET_CODE (XEXP (op0, 1)) == CONST_INT
11082 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1))
11083 == mode_width - 1)
11085 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11086 (HOST_WIDE_INT) 1);
11087 continue;
11089 break;
11091 case ASHIFTRT:
11092 /* If this is an equality comparison with zero, we can do this
11093 as a logical shift, which might be much simpler. */
11094 if (equality_comparison_p && const_op == 0
11095 && GET_CODE (XEXP (op0, 1)) == CONST_INT)
11097 op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
11098 XEXP (op0, 0),
11099 INTVAL (XEXP (op0, 1)));
11100 continue;
11103 /* If OP0 is a sign extension and CODE is not an unsigned comparison,
11104 do the comparison in a narrower mode. */
11105 if (! unsigned_comparison_p
11106 && GET_CODE (XEXP (op0, 1)) == CONST_INT
11107 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11108 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
11109 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
11110 MODE_INT, 1)) != BLKmode
11111 && (((unsigned HOST_WIDE_INT) const_op
11112 + (GET_MODE_MASK (tmode) >> 1) + 1)
11113 <= GET_MODE_MASK (tmode)))
11115 op0 = gen_lowpart (tmode, XEXP (XEXP (op0, 0), 0));
11116 continue;
11119 /* Likewise if OP0 is a PLUS of a sign extension with a
11120 constant, which is usually represented with the PLUS
11121 between the shifts. */
11122 if (! unsigned_comparison_p
11123 && GET_CODE (XEXP (op0, 1)) == CONST_INT
11124 && GET_CODE (XEXP (op0, 0)) == PLUS
11125 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
11126 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
11127 && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
11128 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
11129 MODE_INT, 1)) != BLKmode
11130 && (((unsigned HOST_WIDE_INT) const_op
11131 + (GET_MODE_MASK (tmode) >> 1) + 1)
11132 <= GET_MODE_MASK (tmode)))
11134 rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
11135 rtx add_const = XEXP (XEXP (op0, 0), 1);
11136 rtx new_const = gen_binary (ASHIFTRT, GET_MODE (op0), add_const,
11137 XEXP (op0, 1));
11139 op0 = gen_binary (PLUS, tmode,
11140 gen_lowpart (tmode, inner),
11141 new_const);
11142 continue;
11145 /* ... fall through ... */
11146 case LSHIFTRT:
11147 /* If we have (compare (xshiftrt FOO N) (const_int C)) and
11148 the low order N bits of FOO are known to be zero, we can do this
11149 by comparing FOO with C shifted left N bits so long as no
11150 overflow occurs. */
11151 if (GET_CODE (XEXP (op0, 1)) == CONST_INT
11152 && INTVAL (XEXP (op0, 1)) >= 0
11153 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
11154 && mode_width <= HOST_BITS_PER_WIDE_INT
11155 && (nonzero_bits (XEXP (op0, 0), mode)
11156 & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0
11157 && (((unsigned HOST_WIDE_INT) const_op
11158 + (GET_CODE (op0) != LSHIFTRT
11159 ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
11160 + 1)
11161 : 0))
11162 <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
11164 /* If the shift was logical, then we must make the condition
11165 unsigned. */
11166 if (GET_CODE (op0) == LSHIFTRT)
11167 code = unsigned_condition (code);
11169 const_op <<= INTVAL (XEXP (op0, 1));
11170 op1 = GEN_INT (const_op);
11171 op0 = XEXP (op0, 0);
11172 continue;
11175 /* If we are using this shift to extract just the sign bit, we
11176 can replace this with an LT or GE comparison. */
11177 if (const_op == 0
11178 && (equality_comparison_p || sign_bit_comparison_p)
11179 && GET_CODE (XEXP (op0, 1)) == CONST_INT
11180 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1))
11181 == mode_width - 1)
11183 op0 = XEXP (op0, 0);
11184 code = (code == NE || code == GT ? LT : GE);
11185 continue;
11187 break;
11189 default:
11190 break;
11193 break;
11196 /* Now make any compound operations involved in this comparison. Then,
11197 check for an outmost SUBREG on OP0 that is not doing anything or is
11198 paradoxical. The latter transformation must only be performed when
11199 it is known that the "extra" bits will be the same in op0 and op1 or
11200 that they don't matter. There are three cases to consider:
11202 1. SUBREG_REG (op0) is a register. In this case the bits are don't
11203 care bits and we can assume they have any convenient value. So
11204 making the transformation is safe.
11206 2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not defined.
11207 In this case the upper bits of op0 are undefined. We should not make
11208 the simplification in that case as we do not know the contents of
11209 those bits.
11211 3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is defined and not
11212 NIL. In that case we know those bits are zeros or ones. We must
11213 also be sure that they are the same as the upper bits of op1.
11215 We can never remove a SUBREG for a non-equality comparison because
11216 the sign bit is in a different place in the underlying object. */
11218 op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
11219 op1 = make_compound_operation (op1, SET);
11221 if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
11222 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
11223 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (op0))) == MODE_INT
11224 && (code == NE || code == EQ))
11226 if (GET_MODE_SIZE (GET_MODE (op0))
11227 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0))))
11229 /* For paradoxical subregs, allow case 1 as above. Case 3 isn't
11230 implemented. */
11231 if (GET_CODE (SUBREG_REG (op0)) == REG)
11233 op0 = SUBREG_REG (op0);
11234 op1 = gen_lowpart (GET_MODE (op0), op1);
11237 else if ((GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
11238 <= HOST_BITS_PER_WIDE_INT)
11239 && (nonzero_bits (SUBREG_REG (op0),
11240 GET_MODE (SUBREG_REG (op0)))
11241 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
11243 tem = gen_lowpart (GET_MODE (SUBREG_REG (op0)), op1);
11245 if ((nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
11246 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
11247 op0 = SUBREG_REG (op0), op1 = tem;
11251 /* We now do the opposite procedure: Some machines don't have compare
11252 insns in all modes. If OP0's mode is an integer mode smaller than a
11253 word and we can't do a compare in that mode, see if there is a larger
11254 mode for which we can do the compare. There are a number of cases in
11255 which we can use the wider mode. */
11257 mode = GET_MODE (op0);
11258 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
11259 && GET_MODE_SIZE (mode) < UNITS_PER_WORD
11260 && ! have_insn_for (COMPARE, mode))
11261 for (tmode = GET_MODE_WIDER_MODE (mode);
11262 (tmode != VOIDmode
11263 && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT);
11264 tmode = GET_MODE_WIDER_MODE (tmode))
11265 if (have_insn_for (COMPARE, tmode))
11267 int zero_extended;
11269 /* If the only nonzero bits in OP0 and OP1 are those in the
11270 narrower mode and this is an equality or unsigned comparison,
11271 we can use the wider mode. Similarly for sign-extended
11272 values, in which case it is true for all comparisons. */
11273 zero_extended = ((code == EQ || code == NE
11274 || code == GEU || code == GTU
11275 || code == LEU || code == LTU)
11276 && (nonzero_bits (op0, tmode)
11277 & ~GET_MODE_MASK (mode)) == 0
11278 && ((GET_CODE (op1) == CONST_INT
11279 || (nonzero_bits (op1, tmode)
11280 & ~GET_MODE_MASK (mode)) == 0)));
11282 if (zero_extended
11283 || ((num_sign_bit_copies (op0, tmode)
11284 > (unsigned int) (GET_MODE_BITSIZE (tmode)
11285 - GET_MODE_BITSIZE (mode)))
11286 && (num_sign_bit_copies (op1, tmode)
11287 > (unsigned int) (GET_MODE_BITSIZE (tmode)
11288 - GET_MODE_BITSIZE (mode)))))
11290 /* If OP0 is an AND and we don't have an AND in MODE either,
11291 make a new AND in the proper mode. */
11292 if (GET_CODE (op0) == AND
11293 && !have_insn_for (AND, mode))
11294 op0 = gen_binary (AND, tmode,
11295 gen_lowpart (tmode,
11296 XEXP (op0, 0)),
11297 gen_lowpart (tmode,
11298 XEXP (op0, 1)));
11300 op0 = gen_lowpart (tmode, op0);
11301 if (zero_extended && GET_CODE (op1) == CONST_INT)
11302 op1 = GEN_INT (INTVAL (op1) & GET_MODE_MASK (mode));
11303 op1 = gen_lowpart (tmode, op1);
11304 break;
11307 /* If this is a test for negative, we can make an explicit
11308 test of the sign bit. */
11310 if (op1 == const0_rtx && (code == LT || code == GE)
11311 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11313 op0 = gen_binary (AND, tmode,
11314 gen_lowpart (tmode, op0),
11315 GEN_INT ((HOST_WIDE_INT) 1
11316 << (GET_MODE_BITSIZE (mode) - 1)));
11317 code = (code == LT) ? NE : EQ;
11318 break;
11322 #ifdef CANONICALIZE_COMPARISON
11323 /* If this machine only supports a subset of valid comparisons, see if we
11324 can convert an unsupported one into a supported one. */
11325 CANONICALIZE_COMPARISON (code, op0, op1);
11326 #endif
11328 *pop0 = op0;
11329 *pop1 = op1;
11331 return code;
11334 /* Like jump.c' reversed_comparison_code, but use combine infrastructure for
11335 searching backward. */
11336 static enum rtx_code
11337 combine_reversed_comparison_code (rtx exp)
11339 enum rtx_code code1 = reversed_comparison_code (exp, NULL);
11340 rtx x;
11342 if (code1 != UNKNOWN
11343 || GET_MODE_CLASS (GET_MODE (XEXP (exp, 0))) != MODE_CC)
11344 return code1;
11345 /* Otherwise try and find where the condition codes were last set and
11346 use that. */
11347 x = get_last_value (XEXP (exp, 0));
11348 if (!x || GET_CODE (x) != COMPARE)
11349 return UNKNOWN;
11350 return reversed_comparison_code_parts (GET_CODE (exp),
11351 XEXP (x, 0), XEXP (x, 1), NULL);
11354 /* Return comparison with reversed code of EXP and operands OP0 and OP1.
11355 Return NULL_RTX in case we fail to do the reversal. */
11356 static rtx
11357 reversed_comparison (rtx exp, enum machine_mode mode, rtx op0, rtx op1)
11359 enum rtx_code reversed_code = combine_reversed_comparison_code (exp);
11360 if (reversed_code == UNKNOWN)
11361 return NULL_RTX;
11362 else
11363 return gen_binary (reversed_code, mode, op0, op1);
11366 /* Utility function for following routine. Called when X is part of a value
11367 being stored into reg_last_set_value. Sets reg_last_set_table_tick
11368 for each register mentioned. Similar to mention_regs in cse.c */
11370 static void
11371 update_table_tick (rtx x)
11373 enum rtx_code code = GET_CODE (x);
11374 const char *fmt = GET_RTX_FORMAT (code);
11375 int i;
11377 if (code == REG)
11379 unsigned int regno = REGNO (x);
11380 unsigned int endregno
11381 = regno + (regno < FIRST_PSEUDO_REGISTER
11382 ? hard_regno_nregs[regno][GET_MODE (x)] : 1);
11383 unsigned int r;
11385 for (r = regno; r < endregno; r++)
11386 reg_last_set_table_tick[r] = label_tick;
11388 return;
11391 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11392 /* Note that we can't have an "E" in values stored; see
11393 get_last_value_validate. */
11394 if (fmt[i] == 'e')
11396 /* Check for identical subexpressions. If x contains
11397 identical subexpression we only have to traverse one of
11398 them. */
11399 if (i == 0 && ARITHMETIC_P (x))
11401 /* Note that at this point x1 has already been
11402 processed. */
11403 rtx x0 = XEXP (x, 0);
11404 rtx x1 = XEXP (x, 1);
11406 /* If x0 and x1 are identical then there is no need to
11407 process x0. */
11408 if (x0 == x1)
11409 break;
11411 /* If x0 is identical to a subexpression of x1 then while
11412 processing x1, x0 has already been processed. Thus we
11413 are done with x. */
11414 if (ARITHMETIC_P (x1)
11415 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
11416 break;
11418 /* If x1 is identical to a subexpression of x0 then we
11419 still have to process the rest of x0. */
11420 if (ARITHMETIC_P (x0)
11421 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
11423 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
11424 break;
11428 update_table_tick (XEXP (x, i));
11432 /* Record that REG is set to VALUE in insn INSN. If VALUE is zero, we
11433 are saying that the register is clobbered and we no longer know its
11434 value. If INSN is zero, don't update reg_last_set; this is only permitted
11435 with VALUE also zero and is used to invalidate the register. */
11437 static void
11438 record_value_for_reg (rtx reg, rtx insn, rtx value)
11440 unsigned int regno = REGNO (reg);
11441 unsigned int endregno
11442 = regno + (regno < FIRST_PSEUDO_REGISTER
11443 ? hard_regno_nregs[regno][GET_MODE (reg)] : 1);
11444 unsigned int i;
11446 /* If VALUE contains REG and we have a previous value for REG, substitute
11447 the previous value. */
11448 if (value && insn && reg_overlap_mentioned_p (reg, value))
11450 rtx tem;
11452 /* Set things up so get_last_value is allowed to see anything set up to
11453 our insn. */
11454 subst_low_cuid = INSN_CUID (insn);
11455 tem = get_last_value (reg);
11457 /* If TEM is simply a binary operation with two CLOBBERs as operands,
11458 it isn't going to be useful and will take a lot of time to process,
11459 so just use the CLOBBER. */
11461 if (tem)
11463 if (ARITHMETIC_P (tem)
11464 && GET_CODE (XEXP (tem, 0)) == CLOBBER
11465 && GET_CODE (XEXP (tem, 1)) == CLOBBER)
11466 tem = XEXP (tem, 0);
11468 value = replace_rtx (copy_rtx (value), reg, tem);
11472 /* For each register modified, show we don't know its value, that
11473 we don't know about its bitwise content, that its value has been
11474 updated, and that we don't know the location of the death of the
11475 register. */
11476 for (i = regno; i < endregno; i++)
11478 if (insn)
11479 reg_last_set[i] = insn;
11481 reg_last_set_value[i] = 0;
11482 reg_last_set_mode[i] = 0;
11483 reg_last_set_nonzero_bits[i] = 0;
11484 reg_last_set_sign_bit_copies[i] = 0;
11485 reg_last_death[i] = 0;
11488 /* Mark registers that are being referenced in this value. */
11489 if (value)
11490 update_table_tick (value);
11492 /* Now update the status of each register being set.
11493 If someone is using this register in this block, set this register
11494 to invalid since we will get confused between the two lives in this
11495 basic block. This makes using this register always invalid. In cse, we
11496 scan the table to invalidate all entries using this register, but this
11497 is too much work for us. */
11499 for (i = regno; i < endregno; i++)
11501 reg_last_set_label[i] = label_tick;
11502 if (value && reg_last_set_table_tick[i] == label_tick)
11503 reg_last_set_invalid[i] = 1;
11504 else
11505 reg_last_set_invalid[i] = 0;
11508 /* The value being assigned might refer to X (like in "x++;"). In that
11509 case, we must replace it with (clobber (const_int 0)) to prevent
11510 infinite loops. */
11511 if (value && ! get_last_value_validate (&value, insn,
11512 reg_last_set_label[regno], 0))
11514 value = copy_rtx (value);
11515 if (! get_last_value_validate (&value, insn,
11516 reg_last_set_label[regno], 1))
11517 value = 0;
11520 /* For the main register being modified, update the value, the mode, the
11521 nonzero bits, and the number of sign bit copies. */
11523 reg_last_set_value[regno] = value;
11525 if (value)
11527 enum machine_mode mode = GET_MODE (reg);
11528 subst_low_cuid = INSN_CUID (insn);
11529 reg_last_set_mode[regno] = mode;
11530 if (GET_MODE_CLASS (mode) == MODE_INT
11531 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11532 mode = nonzero_bits_mode;
11533 reg_last_set_nonzero_bits[regno] = nonzero_bits (value, mode);
11534 reg_last_set_sign_bit_copies[regno]
11535 = num_sign_bit_copies (value, GET_MODE (reg));
11539 /* Called via note_stores from record_dead_and_set_regs to handle one
11540 SET or CLOBBER in an insn. DATA is the instruction in which the
11541 set is occurring. */
11543 static void
11544 record_dead_and_set_regs_1 (rtx dest, rtx setter, void *data)
11546 rtx record_dead_insn = (rtx) data;
11548 if (GET_CODE (dest) == SUBREG)
11549 dest = SUBREG_REG (dest);
11551 if (GET_CODE (dest) == REG)
11553 /* If we are setting the whole register, we know its value. Otherwise
11554 show that we don't know the value. We can handle SUBREG in
11555 some cases. */
11556 if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
11557 record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
11558 else if (GET_CODE (setter) == SET
11559 && GET_CODE (SET_DEST (setter)) == SUBREG
11560 && SUBREG_REG (SET_DEST (setter)) == dest
11561 && GET_MODE_BITSIZE (GET_MODE (dest)) <= BITS_PER_WORD
11562 && subreg_lowpart_p (SET_DEST (setter)))
11563 record_value_for_reg (dest, record_dead_insn,
11564 gen_lowpart (GET_MODE (dest),
11565 SET_SRC (setter)));
11566 else
11567 record_value_for_reg (dest, record_dead_insn, NULL_RTX);
11569 else if (GET_CODE (dest) == MEM
11570 /* Ignore pushes, they clobber nothing. */
11571 && ! push_operand (dest, GET_MODE (dest)))
11572 mem_last_set = INSN_CUID (record_dead_insn);
11575 /* Update the records of when each REG was most recently set or killed
11576 for the things done by INSN. This is the last thing done in processing
11577 INSN in the combiner loop.
11579 We update reg_last_set, reg_last_set_value, reg_last_set_mode,
11580 reg_last_set_nonzero_bits, reg_last_set_sign_bit_copies, reg_last_death,
11581 and also the similar information mem_last_set (which insn most recently
11582 modified memory) and last_call_cuid (which insn was the most recent
11583 subroutine call). */
11585 static void
11586 record_dead_and_set_regs (rtx insn)
11588 rtx link;
11589 unsigned int i;
11591 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
11593 if (REG_NOTE_KIND (link) == REG_DEAD
11594 && GET_CODE (XEXP (link, 0)) == REG)
11596 unsigned int regno = REGNO (XEXP (link, 0));
11597 unsigned int endregno
11598 = regno + (regno < FIRST_PSEUDO_REGISTER
11599 ? hard_regno_nregs[regno][GET_MODE (XEXP (link, 0))]
11600 : 1);
11602 for (i = regno; i < endregno; i++)
11603 reg_last_death[i] = insn;
11605 else if (REG_NOTE_KIND (link) == REG_INC)
11606 record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
11609 if (GET_CODE (insn) == CALL_INSN)
11611 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
11612 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
11614 reg_last_set_value[i] = 0;
11615 reg_last_set_mode[i] = 0;
11616 reg_last_set_nonzero_bits[i] = 0;
11617 reg_last_set_sign_bit_copies[i] = 0;
11618 reg_last_death[i] = 0;
11621 last_call_cuid = mem_last_set = INSN_CUID (insn);
11623 /* Don't bother recording what this insn does. It might set the
11624 return value register, but we can't combine into a call
11625 pattern anyway, so there's no point trying (and it may cause
11626 a crash, if e.g. we wind up asking for last_set_value of a
11627 SUBREG of the return value register). */
11628 return;
11631 note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
11634 /* If a SUBREG has the promoted bit set, it is in fact a property of the
11635 register present in the SUBREG, so for each such SUBREG go back and
11636 adjust nonzero and sign bit information of the registers that are
11637 known to have some zero/sign bits set.
11639 This is needed because when combine blows the SUBREGs away, the
11640 information on zero/sign bits is lost and further combines can be
11641 missed because of that. */
11643 static void
11644 record_promoted_value (rtx insn, rtx subreg)
11646 rtx links, set;
11647 unsigned int regno = REGNO (SUBREG_REG (subreg));
11648 enum machine_mode mode = GET_MODE (subreg);
11650 if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT)
11651 return;
11653 for (links = LOG_LINKS (insn); links;)
11655 insn = XEXP (links, 0);
11656 set = single_set (insn);
11658 if (! set || GET_CODE (SET_DEST (set)) != REG
11659 || REGNO (SET_DEST (set)) != regno
11660 || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
11662 links = XEXP (links, 1);
11663 continue;
11666 if (reg_last_set[regno] == insn)
11668 if (SUBREG_PROMOTED_UNSIGNED_P (subreg) > 0)
11669 reg_last_set_nonzero_bits[regno] &= GET_MODE_MASK (mode);
11672 if (GET_CODE (SET_SRC (set)) == REG)
11674 regno = REGNO (SET_SRC (set));
11675 links = LOG_LINKS (insn);
11677 else
11678 break;
11682 /* Scan X for promoted SUBREGs. For each one found,
11683 note what it implies to the registers used in it. */
11685 static void
11686 check_promoted_subreg (rtx insn, rtx x)
11688 if (GET_CODE (x) == SUBREG && SUBREG_PROMOTED_VAR_P (x)
11689 && GET_CODE (SUBREG_REG (x)) == REG)
11690 record_promoted_value (insn, x);
11691 else
11693 const char *format = GET_RTX_FORMAT (GET_CODE (x));
11694 int i, j;
11696 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
11697 switch (format[i])
11699 case 'e':
11700 check_promoted_subreg (insn, XEXP (x, i));
11701 break;
11702 case 'V':
11703 case 'E':
11704 if (XVEC (x, i) != 0)
11705 for (j = 0; j < XVECLEN (x, i); j++)
11706 check_promoted_subreg (insn, XVECEXP (x, i, j));
11707 break;
11712 /* Utility routine for the following function. Verify that all the registers
11713 mentioned in *LOC are valid when *LOC was part of a value set when
11714 label_tick == TICK. Return 0 if some are not.
11716 If REPLACE is nonzero, replace the invalid reference with
11717 (clobber (const_int 0)) and return 1. This replacement is useful because
11718 we often can get useful information about the form of a value (e.g., if
11719 it was produced by a shift that always produces -1 or 0) even though
11720 we don't know exactly what registers it was produced from. */
11722 static int
11723 get_last_value_validate (rtx *loc, rtx insn, int tick, int replace)
11725 rtx x = *loc;
11726 const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
11727 int len = GET_RTX_LENGTH (GET_CODE (x));
11728 int i;
11730 if (GET_CODE (x) == REG)
11732 unsigned int regno = REGNO (x);
11733 unsigned int endregno
11734 = regno + (regno < FIRST_PSEUDO_REGISTER
11735 ? hard_regno_nregs[regno][GET_MODE (x)] : 1);
11736 unsigned int j;
11738 for (j = regno; j < endregno; j++)
11739 if (reg_last_set_invalid[j]
11740 /* If this is a pseudo-register that was only set once and not
11741 live at the beginning of the function, it is always valid. */
11742 || (! (regno >= FIRST_PSEUDO_REGISTER
11743 && REG_N_SETS (regno) == 1
11744 && (! REGNO_REG_SET_P
11745 (ENTRY_BLOCK_PTR->next_bb->global_live_at_start, regno)))
11746 && reg_last_set_label[j] > tick))
11748 if (replace)
11749 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
11750 return replace;
11753 return 1;
11755 /* If this is a memory reference, make sure that there were
11756 no stores after it that might have clobbered the value. We don't
11757 have alias info, so we assume any store invalidates it. */
11758 else if (GET_CODE (x) == MEM && ! RTX_UNCHANGING_P (x)
11759 && INSN_CUID (insn) <= mem_last_set)
11761 if (replace)
11762 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
11763 return replace;
11766 for (i = 0; i < len; i++)
11768 if (fmt[i] == 'e')
11770 /* Check for identical subexpressions. If x contains
11771 identical subexpression we only have to traverse one of
11772 them. */
11773 if (i == 1 && ARITHMETIC_P (x))
11775 /* Note that at this point x0 has already been checked
11776 and found valid. */
11777 rtx x0 = XEXP (x, 0);
11778 rtx x1 = XEXP (x, 1);
11780 /* If x0 and x1 are identical then x is also valid. */
11781 if (x0 == x1)
11782 return 1;
11784 /* If x1 is identical to a subexpression of x0 then
11785 while checking x0, x1 has already been checked. Thus
11786 it is valid and so as x. */
11787 if (ARITHMETIC_P (x0)
11788 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
11789 return 1;
11791 /* If x0 is identical to a subexpression of x1 then x is
11792 valid iff the rest of x1 is valid. */
11793 if (ARITHMETIC_P (x1)
11794 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
11795 return
11796 get_last_value_validate (&XEXP (x1,
11797 x0 == XEXP (x1, 0) ? 1 : 0),
11798 insn, tick, replace);
11801 if (get_last_value_validate (&XEXP (x, i), insn, tick,
11802 replace) == 0)
11803 return 0;
11805 /* Don't bother with these. They shouldn't occur anyway. */
11806 else if (fmt[i] == 'E')
11807 return 0;
11810 /* If we haven't found a reason for it to be invalid, it is valid. */
11811 return 1;
11814 /* Get the last value assigned to X, if known. Some registers
11815 in the value may be replaced with (clobber (const_int 0)) if their value
11816 is known longer known reliably. */
11818 static rtx
11819 get_last_value (rtx x)
11821 unsigned int regno;
11822 rtx value;
11824 /* If this is a non-paradoxical SUBREG, get the value of its operand and
11825 then convert it to the desired mode. If this is a paradoxical SUBREG,
11826 we cannot predict what values the "extra" bits might have. */
11827 if (GET_CODE (x) == SUBREG
11828 && subreg_lowpart_p (x)
11829 && (GET_MODE_SIZE (GET_MODE (x))
11830 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
11831 && (value = get_last_value (SUBREG_REG (x))) != 0)
11832 return gen_lowpart (GET_MODE (x), value);
11834 if (GET_CODE (x) != REG)
11835 return 0;
11837 regno = REGNO (x);
11838 value = reg_last_set_value[regno];
11840 /* If we don't have a value, or if it isn't for this basic block and
11841 it's either a hard register, set more than once, or it's a live
11842 at the beginning of the function, return 0.
11844 Because if it's not live at the beginning of the function then the reg
11845 is always set before being used (is never used without being set).
11846 And, if it's set only once, and it's always set before use, then all
11847 uses must have the same last value, even if it's not from this basic
11848 block. */
11850 if (value == 0
11851 || (reg_last_set_label[regno] != label_tick
11852 && (regno < FIRST_PSEUDO_REGISTER
11853 || REG_N_SETS (regno) != 1
11854 || (REGNO_REG_SET_P
11855 (ENTRY_BLOCK_PTR->next_bb->global_live_at_start, regno)))))
11856 return 0;
11858 /* If the value was set in a later insn than the ones we are processing,
11859 we can't use it even if the register was only set once. */
11860 if (INSN_CUID (reg_last_set[regno]) >= subst_low_cuid)
11861 return 0;
11863 /* If the value has all its registers valid, return it. */
11864 if (get_last_value_validate (&value, reg_last_set[regno],
11865 reg_last_set_label[regno], 0))
11866 return value;
11868 /* Otherwise, make a copy and replace any invalid register with
11869 (clobber (const_int 0)). If that fails for some reason, return 0. */
11871 value = copy_rtx (value);
11872 if (get_last_value_validate (&value, reg_last_set[regno],
11873 reg_last_set_label[regno], 1))
11874 return value;
11876 return 0;
11879 /* Return nonzero if expression X refers to a REG or to memory
11880 that is set in an instruction more recent than FROM_CUID. */
11882 static int
11883 use_crosses_set_p (rtx x, int from_cuid)
11885 const char *fmt;
11886 int i;
11887 enum rtx_code code = GET_CODE (x);
11889 if (code == REG)
11891 unsigned int regno = REGNO (x);
11892 unsigned endreg = regno + (regno < FIRST_PSEUDO_REGISTER
11893 ? hard_regno_nregs[regno][GET_MODE (x)] : 1);
11895 #ifdef PUSH_ROUNDING
11896 /* Don't allow uses of the stack pointer to be moved,
11897 because we don't know whether the move crosses a push insn. */
11898 if (regno == STACK_POINTER_REGNUM && PUSH_ARGS)
11899 return 1;
11900 #endif
11901 for (; regno < endreg; regno++)
11902 if (reg_last_set[regno]
11903 && INSN_CUID (reg_last_set[regno]) > from_cuid)
11904 return 1;
11905 return 0;
11908 if (code == MEM && mem_last_set > from_cuid)
11909 return 1;
11911 fmt = GET_RTX_FORMAT (code);
11913 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11915 if (fmt[i] == 'E')
11917 int j;
11918 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
11919 if (use_crosses_set_p (XVECEXP (x, i, j), from_cuid))
11920 return 1;
11922 else if (fmt[i] == 'e'
11923 && use_crosses_set_p (XEXP (x, i), from_cuid))
11924 return 1;
11926 return 0;
11929 /* Define three variables used for communication between the following
11930 routines. */
11932 static unsigned int reg_dead_regno, reg_dead_endregno;
11933 static int reg_dead_flag;
11935 /* Function called via note_stores from reg_dead_at_p.
11937 If DEST is within [reg_dead_regno, reg_dead_endregno), set
11938 reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET. */
11940 static void
11941 reg_dead_at_p_1 (rtx dest, rtx x, void *data ATTRIBUTE_UNUSED)
11943 unsigned int regno, endregno;
11945 if (GET_CODE (dest) != REG)
11946 return;
11948 regno = REGNO (dest);
11949 endregno = regno + (regno < FIRST_PSEUDO_REGISTER
11950 ? hard_regno_nregs[regno][GET_MODE (dest)] : 1);
11952 if (reg_dead_endregno > regno && reg_dead_regno < endregno)
11953 reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
11956 /* Return nonzero if REG is known to be dead at INSN.
11958 We scan backwards from INSN. If we hit a REG_DEAD note or a CLOBBER
11959 referencing REG, it is dead. If we hit a SET referencing REG, it is
11960 live. Otherwise, see if it is live or dead at the start of the basic
11961 block we are in. Hard regs marked as being live in NEWPAT_USED_REGS
11962 must be assumed to be always live. */
11964 static int
11965 reg_dead_at_p (rtx reg, rtx insn)
11967 basic_block block;
11968 unsigned int i;
11970 /* Set variables for reg_dead_at_p_1. */
11971 reg_dead_regno = REGNO (reg);
11972 reg_dead_endregno = reg_dead_regno + (reg_dead_regno < FIRST_PSEUDO_REGISTER
11973 ? hard_regno_nregs[reg_dead_regno]
11974 [GET_MODE (reg)]
11975 : 1);
11977 reg_dead_flag = 0;
11979 /* Check that reg isn't mentioned in NEWPAT_USED_REGS. */
11980 if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
11982 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
11983 if (TEST_HARD_REG_BIT (newpat_used_regs, i))
11984 return 0;
11987 /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, label, or
11988 beginning of function. */
11989 for (; insn && GET_CODE (insn) != CODE_LABEL && GET_CODE (insn) != BARRIER;
11990 insn = prev_nonnote_insn (insn))
11992 note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
11993 if (reg_dead_flag)
11994 return reg_dead_flag == 1 ? 1 : 0;
11996 if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
11997 return 1;
12000 /* Get the basic block that we were in. */
12001 if (insn == 0)
12002 block = ENTRY_BLOCK_PTR->next_bb;
12003 else
12005 FOR_EACH_BB (block)
12006 if (insn == BB_HEAD (block))
12007 break;
12009 if (block == EXIT_BLOCK_PTR)
12010 return 0;
12013 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
12014 if (REGNO_REG_SET_P (block->global_live_at_start, i))
12015 return 0;
12017 return 1;
12020 /* Note hard registers in X that are used. This code is similar to
12021 that in flow.c, but much simpler since we don't care about pseudos. */
12023 static void
12024 mark_used_regs_combine (rtx x)
12026 RTX_CODE code = GET_CODE (x);
12027 unsigned int regno;
12028 int i;
12030 switch (code)
12032 case LABEL_REF:
12033 case SYMBOL_REF:
12034 case CONST_INT:
12035 case CONST:
12036 case CONST_DOUBLE:
12037 case CONST_VECTOR:
12038 case PC:
12039 case ADDR_VEC:
12040 case ADDR_DIFF_VEC:
12041 case ASM_INPUT:
12042 #ifdef HAVE_cc0
12043 /* CC0 must die in the insn after it is set, so we don't need to take
12044 special note of it here. */
12045 case CC0:
12046 #endif
12047 return;
12049 case CLOBBER:
12050 /* If we are clobbering a MEM, mark any hard registers inside the
12051 address as used. */
12052 if (GET_CODE (XEXP (x, 0)) == MEM)
12053 mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
12054 return;
12056 case REG:
12057 regno = REGNO (x);
12058 /* A hard reg in a wide mode may really be multiple registers.
12059 If so, mark all of them just like the first. */
12060 if (regno < FIRST_PSEUDO_REGISTER)
12062 unsigned int endregno, r;
12064 /* None of this applies to the stack, frame or arg pointers. */
12065 if (regno == STACK_POINTER_REGNUM
12066 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
12067 || regno == HARD_FRAME_POINTER_REGNUM
12068 #endif
12069 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
12070 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
12071 #endif
12072 || regno == FRAME_POINTER_REGNUM)
12073 return;
12075 endregno = regno + hard_regno_nregs[regno][GET_MODE (x)];
12076 for (r = regno; r < endregno; r++)
12077 SET_HARD_REG_BIT (newpat_used_regs, r);
12079 return;
12081 case SET:
12083 /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
12084 the address. */
12085 rtx testreg = SET_DEST (x);
12087 while (GET_CODE (testreg) == SUBREG
12088 || GET_CODE (testreg) == ZERO_EXTRACT
12089 || GET_CODE (testreg) == SIGN_EXTRACT
12090 || GET_CODE (testreg) == STRICT_LOW_PART)
12091 testreg = XEXP (testreg, 0);
12093 if (GET_CODE (testreg) == MEM)
12094 mark_used_regs_combine (XEXP (testreg, 0));
12096 mark_used_regs_combine (SET_SRC (x));
12098 return;
12100 default:
12101 break;
12104 /* Recursively scan the operands of this expression. */
12107 const char *fmt = GET_RTX_FORMAT (code);
12109 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12111 if (fmt[i] == 'e')
12112 mark_used_regs_combine (XEXP (x, i));
12113 else if (fmt[i] == 'E')
12115 int j;
12117 for (j = 0; j < XVECLEN (x, i); j++)
12118 mark_used_regs_combine (XVECEXP (x, i, j));
12124 /* Remove register number REGNO from the dead registers list of INSN.
12126 Return the note used to record the death, if there was one. */
12129 remove_death (unsigned int regno, rtx insn)
12131 rtx note = find_regno_note (insn, REG_DEAD, regno);
12133 if (note)
12135 REG_N_DEATHS (regno)--;
12136 remove_note (insn, note);
12139 return note;
12142 /* For each register (hardware or pseudo) used within expression X, if its
12143 death is in an instruction with cuid between FROM_CUID (inclusive) and
12144 TO_INSN (exclusive), put a REG_DEAD note for that register in the
12145 list headed by PNOTES.
12147 That said, don't move registers killed by maybe_kill_insn.
12149 This is done when X is being merged by combination into TO_INSN. These
12150 notes will then be distributed as needed. */
12152 static void
12153 move_deaths (rtx x, rtx maybe_kill_insn, int from_cuid, rtx to_insn,
12154 rtx *pnotes)
12156 const char *fmt;
12157 int len, i;
12158 enum rtx_code code = GET_CODE (x);
12160 if (code == REG)
12162 unsigned int regno = REGNO (x);
12163 rtx where_dead = reg_last_death[regno];
12164 rtx before_dead, after_dead;
12166 /* Don't move the register if it gets killed in between from and to. */
12167 if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
12168 && ! reg_referenced_p (x, maybe_kill_insn))
12169 return;
12171 /* WHERE_DEAD could be a USE insn made by combine, so first we
12172 make sure that we have insns with valid INSN_CUID values. */
12173 before_dead = where_dead;
12174 while (before_dead && INSN_UID (before_dead) > max_uid_cuid)
12175 before_dead = PREV_INSN (before_dead);
12177 after_dead = where_dead;
12178 while (after_dead && INSN_UID (after_dead) > max_uid_cuid)
12179 after_dead = NEXT_INSN (after_dead);
12181 if (before_dead && after_dead
12182 && INSN_CUID (before_dead) >= from_cuid
12183 && (INSN_CUID (after_dead) < INSN_CUID (to_insn)
12184 || (where_dead != after_dead
12185 && INSN_CUID (after_dead) == INSN_CUID (to_insn))))
12187 rtx note = remove_death (regno, where_dead);
12189 /* It is possible for the call above to return 0. This can occur
12190 when reg_last_death points to I2 or I1 that we combined with.
12191 In that case make a new note.
12193 We must also check for the case where X is a hard register
12194 and NOTE is a death note for a range of hard registers
12195 including X. In that case, we must put REG_DEAD notes for
12196 the remaining registers in place of NOTE. */
12198 if (note != 0 && regno < FIRST_PSEUDO_REGISTER
12199 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
12200 > GET_MODE_SIZE (GET_MODE (x))))
12202 unsigned int deadregno = REGNO (XEXP (note, 0));
12203 unsigned int deadend
12204 = (deadregno + hard_regno_nregs[deadregno]
12205 [GET_MODE (XEXP (note, 0))]);
12206 unsigned int ourend
12207 = regno + hard_regno_nregs[regno][GET_MODE (x)];
12208 unsigned int i;
12210 for (i = deadregno; i < deadend; i++)
12211 if (i < regno || i >= ourend)
12212 REG_NOTES (where_dead)
12213 = gen_rtx_EXPR_LIST (REG_DEAD,
12214 regno_reg_rtx[i],
12215 REG_NOTES (where_dead));
12218 /* If we didn't find any note, or if we found a REG_DEAD note that
12219 covers only part of the given reg, and we have a multi-reg hard
12220 register, then to be safe we must check for REG_DEAD notes
12221 for each register other than the first. They could have
12222 their own REG_DEAD notes lying around. */
12223 else if ((note == 0
12224 || (note != 0
12225 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
12226 < GET_MODE_SIZE (GET_MODE (x)))))
12227 && regno < FIRST_PSEUDO_REGISTER
12228 && hard_regno_nregs[regno][GET_MODE (x)] > 1)
12230 unsigned int ourend
12231 = regno + hard_regno_nregs[regno][GET_MODE (x)];
12232 unsigned int i, offset;
12233 rtx oldnotes = 0;
12235 if (note)
12236 offset = hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))];
12237 else
12238 offset = 1;
12240 for (i = regno + offset; i < ourend; i++)
12241 move_deaths (regno_reg_rtx[i],
12242 maybe_kill_insn, from_cuid, to_insn, &oldnotes);
12245 if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
12247 XEXP (note, 1) = *pnotes;
12248 *pnotes = note;
12250 else
12251 *pnotes = gen_rtx_EXPR_LIST (REG_DEAD, x, *pnotes);
12253 REG_N_DEATHS (regno)++;
12256 return;
12259 else if (GET_CODE (x) == SET)
12261 rtx dest = SET_DEST (x);
12263 move_deaths (SET_SRC (x), maybe_kill_insn, from_cuid, to_insn, pnotes);
12265 /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
12266 that accesses one word of a multi-word item, some
12267 piece of everything register in the expression is used by
12268 this insn, so remove any old death. */
12269 /* ??? So why do we test for equality of the sizes? */
12271 if (GET_CODE (dest) == ZERO_EXTRACT
12272 || GET_CODE (dest) == STRICT_LOW_PART
12273 || (GET_CODE (dest) == SUBREG
12274 && (((GET_MODE_SIZE (GET_MODE (dest))
12275 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
12276 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
12277 + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
12279 move_deaths (dest, maybe_kill_insn, from_cuid, to_insn, pnotes);
12280 return;
12283 /* If this is some other SUBREG, we know it replaces the entire
12284 value, so use that as the destination. */
12285 if (GET_CODE (dest) == SUBREG)
12286 dest = SUBREG_REG (dest);
12288 /* If this is a MEM, adjust deaths of anything used in the address.
12289 For a REG (the only other possibility), the entire value is
12290 being replaced so the old value is not used in this insn. */
12292 if (GET_CODE (dest) == MEM)
12293 move_deaths (XEXP (dest, 0), maybe_kill_insn, from_cuid,
12294 to_insn, pnotes);
12295 return;
12298 else if (GET_CODE (x) == CLOBBER)
12299 return;
12301 len = GET_RTX_LENGTH (code);
12302 fmt = GET_RTX_FORMAT (code);
12304 for (i = 0; i < len; i++)
12306 if (fmt[i] == 'E')
12308 int j;
12309 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
12310 move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_cuid,
12311 to_insn, pnotes);
12313 else if (fmt[i] == 'e')
12314 move_deaths (XEXP (x, i), maybe_kill_insn, from_cuid, to_insn, pnotes);
12318 /* Return 1 if X is the target of a bit-field assignment in BODY, the
12319 pattern of an insn. X must be a REG. */
12321 static int
12322 reg_bitfield_target_p (rtx x, rtx body)
12324 int i;
12326 if (GET_CODE (body) == SET)
12328 rtx dest = SET_DEST (body);
12329 rtx target;
12330 unsigned int regno, tregno, endregno, endtregno;
12332 if (GET_CODE (dest) == ZERO_EXTRACT)
12333 target = XEXP (dest, 0);
12334 else if (GET_CODE (dest) == STRICT_LOW_PART)
12335 target = SUBREG_REG (XEXP (dest, 0));
12336 else
12337 return 0;
12339 if (GET_CODE (target) == SUBREG)
12340 target = SUBREG_REG (target);
12342 if (GET_CODE (target) != REG)
12343 return 0;
12345 tregno = REGNO (target), regno = REGNO (x);
12346 if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
12347 return target == x;
12349 endtregno = tregno + hard_regno_nregs[tregno][GET_MODE (target)];
12350 endregno = regno + hard_regno_nregs[regno][GET_MODE (x)];
12352 return endregno > tregno && regno < endtregno;
12355 else if (GET_CODE (body) == PARALLEL)
12356 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
12357 if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
12358 return 1;
12360 return 0;
12363 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
12364 as appropriate. I3 and I2 are the insns resulting from the combination
12365 insns including FROM (I2 may be zero).
12367 Each note in the list is either ignored or placed on some insns, depending
12368 on the type of note. */
12370 static void
12371 distribute_notes (rtx notes, rtx from_insn, rtx i3, rtx i2)
12373 rtx note, next_note;
12374 rtx tem;
12376 for (note = notes; note; note = next_note)
12378 rtx place = 0, place2 = 0;
12380 /* If this NOTE references a pseudo register, ensure it references
12381 the latest copy of that register. */
12382 if (XEXP (note, 0) && GET_CODE (XEXP (note, 0)) == REG
12383 && REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER)
12384 XEXP (note, 0) = regno_reg_rtx[REGNO (XEXP (note, 0))];
12386 next_note = XEXP (note, 1);
12387 switch (REG_NOTE_KIND (note))
12389 case REG_BR_PROB:
12390 case REG_BR_PRED:
12391 /* Doesn't matter much where we put this, as long as it's somewhere.
12392 It is preferable to keep these notes on branches, which is most
12393 likely to be i3. */
12394 place = i3;
12395 break;
12397 case REG_VALUE_PROFILE:
12398 /* Just get rid of this note, as it is unused later anyway. */
12399 break;
12401 case REG_VTABLE_REF:
12402 /* ??? Should remain with *a particular* memory load. Given the
12403 nature of vtable data, the last insn seems relatively safe. */
12404 place = i3;
12405 break;
12407 case REG_NON_LOCAL_GOTO:
12408 if (GET_CODE (i3) == JUMP_INSN)
12409 place = i3;
12410 else if (i2 && GET_CODE (i2) == JUMP_INSN)
12411 place = i2;
12412 else
12413 abort ();
12414 break;
12416 case REG_EH_REGION:
12417 /* These notes must remain with the call or trapping instruction. */
12418 if (GET_CODE (i3) == CALL_INSN)
12419 place = i3;
12420 else if (i2 && GET_CODE (i2) == CALL_INSN)
12421 place = i2;
12422 else if (flag_non_call_exceptions)
12424 if (may_trap_p (i3))
12425 place = i3;
12426 else if (i2 && may_trap_p (i2))
12427 place = i2;
12428 /* ??? Otherwise assume we've combined things such that we
12429 can now prove that the instructions can't trap. Drop the
12430 note in this case. */
12432 else
12433 abort ();
12434 break;
12436 case REG_ALWAYS_RETURN:
12437 case REG_NORETURN:
12438 case REG_SETJMP:
12439 /* These notes must remain with the call. It should not be
12440 possible for both I2 and I3 to be a call. */
12441 if (GET_CODE (i3) == CALL_INSN)
12442 place = i3;
12443 else if (i2 && GET_CODE (i2) == CALL_INSN)
12444 place = i2;
12445 else
12446 abort ();
12447 break;
12449 case REG_UNUSED:
12450 /* Any clobbers for i3 may still exist, and so we must process
12451 REG_UNUSED notes from that insn.
12453 Any clobbers from i2 or i1 can only exist if they were added by
12454 recog_for_combine. In that case, recog_for_combine created the
12455 necessary REG_UNUSED notes. Trying to keep any original
12456 REG_UNUSED notes from these insns can cause incorrect output
12457 if it is for the same register as the original i3 dest.
12458 In that case, we will notice that the register is set in i3,
12459 and then add a REG_UNUSED note for the destination of i3, which
12460 is wrong. However, it is possible to have REG_UNUSED notes from
12461 i2 or i1 for register which were both used and clobbered, so
12462 we keep notes from i2 or i1 if they will turn into REG_DEAD
12463 notes. */
12465 /* If this register is set or clobbered in I3, put the note there
12466 unless there is one already. */
12467 if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
12469 if (from_insn != i3)
12470 break;
12472 if (! (GET_CODE (XEXP (note, 0)) == REG
12473 ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
12474 : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
12475 place = i3;
12477 /* Otherwise, if this register is used by I3, then this register
12478 now dies here, so we must put a REG_DEAD note here unless there
12479 is one already. */
12480 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
12481 && ! (GET_CODE (XEXP (note, 0)) == REG
12482 ? find_regno_note (i3, REG_DEAD,
12483 REGNO (XEXP (note, 0)))
12484 : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
12486 PUT_REG_NOTE_KIND (note, REG_DEAD);
12487 place = i3;
12489 break;
12491 case REG_EQUAL:
12492 case REG_EQUIV:
12493 case REG_NOALIAS:
12494 /* These notes say something about results of an insn. We can
12495 only support them if they used to be on I3 in which case they
12496 remain on I3. Otherwise they are ignored.
12498 If the note refers to an expression that is not a constant, we
12499 must also ignore the note since we cannot tell whether the
12500 equivalence is still true. It might be possible to do
12501 slightly better than this (we only have a problem if I2DEST
12502 or I1DEST is present in the expression), but it doesn't
12503 seem worth the trouble. */
12505 if (from_insn == i3
12506 && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
12507 place = i3;
12508 break;
12510 case REG_INC:
12511 case REG_NO_CONFLICT:
12512 /* These notes say something about how a register is used. They must
12513 be present on any use of the register in I2 or I3. */
12514 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
12515 place = i3;
12517 if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
12519 if (place)
12520 place2 = i2;
12521 else
12522 place = i2;
12524 break;
12526 case REG_LABEL:
12527 /* This can show up in several ways -- either directly in the
12528 pattern, or hidden off in the constant pool with (or without?)
12529 a REG_EQUAL note. */
12530 /* ??? Ignore the without-reg_equal-note problem for now. */
12531 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
12532 || ((tem = find_reg_note (i3, REG_EQUAL, NULL_RTX))
12533 && GET_CODE (XEXP (tem, 0)) == LABEL_REF
12534 && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0)))
12535 place = i3;
12537 if (i2
12538 && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
12539 || ((tem = find_reg_note (i2, REG_EQUAL, NULL_RTX))
12540 && GET_CODE (XEXP (tem, 0)) == LABEL_REF
12541 && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0))))
12543 if (place)
12544 place2 = i2;
12545 else
12546 place = i2;
12549 /* Don't attach REG_LABEL note to a JUMP_INSN which has
12550 JUMP_LABEL already. Instead, decrement LABEL_NUSES. */
12551 if (place && GET_CODE (place) == JUMP_INSN && JUMP_LABEL (place))
12553 if (JUMP_LABEL (place) != XEXP (note, 0))
12554 abort ();
12555 if (GET_CODE (JUMP_LABEL (place)) == CODE_LABEL)
12556 LABEL_NUSES (JUMP_LABEL (place))--;
12557 place = 0;
12559 if (place2 && GET_CODE (place2) == JUMP_INSN && JUMP_LABEL (place2))
12561 if (JUMP_LABEL (place2) != XEXP (note, 0))
12562 abort ();
12563 if (GET_CODE (JUMP_LABEL (place2)) == CODE_LABEL)
12564 LABEL_NUSES (JUMP_LABEL (place2))--;
12565 place2 = 0;
12567 break;
12569 case REG_NONNEG:
12570 /* This note says something about the value of a register prior
12571 to the execution of an insn. It is too much trouble to see
12572 if the note is still correct in all situations. It is better
12573 to simply delete it. */
12574 break;
12576 case REG_RETVAL:
12577 /* If the insn previously containing this note still exists,
12578 put it back where it was. Otherwise move it to the previous
12579 insn. Adjust the corresponding REG_LIBCALL note. */
12580 if (GET_CODE (from_insn) != NOTE)
12581 place = from_insn;
12582 else
12584 tem = find_reg_note (XEXP (note, 0), REG_LIBCALL, NULL_RTX);
12585 place = prev_real_insn (from_insn);
12586 if (tem && place)
12587 XEXP (tem, 0) = place;
12588 /* If we're deleting the last remaining instruction of a
12589 libcall sequence, don't add the notes. */
12590 else if (XEXP (note, 0) == from_insn)
12591 tem = place = 0;
12593 break;
12595 case REG_LIBCALL:
12596 /* This is handled similarly to REG_RETVAL. */
12597 if (GET_CODE (from_insn) != NOTE)
12598 place = from_insn;
12599 else
12601 tem = find_reg_note (XEXP (note, 0), REG_RETVAL, NULL_RTX);
12602 place = next_real_insn (from_insn);
12603 if (tem && place)
12604 XEXP (tem, 0) = place;
12605 /* If we're deleting the last remaining instruction of a
12606 libcall sequence, don't add the notes. */
12607 else if (XEXP (note, 0) == from_insn)
12608 tem = place = 0;
12610 break;
12612 case REG_DEAD:
12613 /* If the register is used as an input in I3, it dies there.
12614 Similarly for I2, if it is nonzero and adjacent to I3.
12616 If the register is not used as an input in either I3 or I2
12617 and it is not one of the registers we were supposed to eliminate,
12618 there are two possibilities. We might have a non-adjacent I2
12619 or we might have somehow eliminated an additional register
12620 from a computation. For example, we might have had A & B where
12621 we discover that B will always be zero. In this case we will
12622 eliminate the reference to A.
12624 In both cases, we must search to see if we can find a previous
12625 use of A and put the death note there. */
12627 if (from_insn
12628 && GET_CODE (from_insn) == CALL_INSN
12629 && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
12630 place = from_insn;
12631 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
12632 place = i3;
12633 else if (i2 != 0 && next_nonnote_insn (i2) == i3
12634 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
12635 place = i2;
12637 if (place == 0)
12639 basic_block bb = this_basic_block;
12641 for (tem = PREV_INSN (i3); place == 0; tem = PREV_INSN (tem))
12643 if (! INSN_P (tem))
12645 if (tem == BB_HEAD (bb))
12646 break;
12647 continue;
12650 /* If the register is being set at TEM, see if that is all
12651 TEM is doing. If so, delete TEM. Otherwise, make this
12652 into a REG_UNUSED note instead. */
12653 if (reg_set_p (XEXP (note, 0), PATTERN (tem)))
12655 rtx set = single_set (tem);
12656 rtx inner_dest = 0;
12657 #ifdef HAVE_cc0
12658 rtx cc0_setter = NULL_RTX;
12659 #endif
12661 if (set != 0)
12662 for (inner_dest = SET_DEST (set);
12663 (GET_CODE (inner_dest) == STRICT_LOW_PART
12664 || GET_CODE (inner_dest) == SUBREG
12665 || GET_CODE (inner_dest) == ZERO_EXTRACT);
12666 inner_dest = XEXP (inner_dest, 0))
12669 /* Verify that it was the set, and not a clobber that
12670 modified the register.
12672 CC0 targets must be careful to maintain setter/user
12673 pairs. If we cannot delete the setter due to side
12674 effects, mark the user with an UNUSED note instead
12675 of deleting it. */
12677 if (set != 0 && ! side_effects_p (SET_SRC (set))
12678 && rtx_equal_p (XEXP (note, 0), inner_dest)
12679 #ifdef HAVE_cc0
12680 && (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
12681 || ((cc0_setter = prev_cc0_setter (tem)) != NULL
12682 && sets_cc0_p (PATTERN (cc0_setter)) > 0))
12683 #endif
12686 /* Move the notes and links of TEM elsewhere.
12687 This might delete other dead insns recursively.
12688 First set the pattern to something that won't use
12689 any register. */
12690 rtx old_notes = REG_NOTES (tem);
12692 PATTERN (tem) = pc_rtx;
12693 REG_NOTES (tem) = NULL;
12695 distribute_notes (old_notes, tem, tem, NULL_RTX);
12696 distribute_links (LOG_LINKS (tem));
12698 PUT_CODE (tem, NOTE);
12699 NOTE_LINE_NUMBER (tem) = NOTE_INSN_DELETED;
12700 NOTE_SOURCE_FILE (tem) = 0;
12702 #ifdef HAVE_cc0
12703 /* Delete the setter too. */
12704 if (cc0_setter)
12706 PATTERN (cc0_setter) = pc_rtx;
12707 old_notes = REG_NOTES (cc0_setter);
12708 REG_NOTES (cc0_setter) = NULL;
12710 distribute_notes (old_notes, cc0_setter,
12711 cc0_setter, NULL_RTX);
12712 distribute_links (LOG_LINKS (cc0_setter));
12714 PUT_CODE (cc0_setter, NOTE);
12715 NOTE_LINE_NUMBER (cc0_setter)
12716 = NOTE_INSN_DELETED;
12717 NOTE_SOURCE_FILE (cc0_setter) = 0;
12719 #endif
12721 else
12723 PUT_REG_NOTE_KIND (note, REG_UNUSED);
12725 /* If there isn't already a REG_UNUSED note, put one
12726 here. Do not place a REG_DEAD note, even if
12727 the register is also used here; that would not
12728 match the algorithm used in lifetime analysis
12729 and can cause the consistency check in the
12730 scheduler to fail. */
12731 if (! find_regno_note (tem, REG_UNUSED,
12732 REGNO (XEXP (note, 0))))
12733 place = tem;
12734 break;
12737 else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem))
12738 || (GET_CODE (tem) == CALL_INSN
12739 && find_reg_fusage (tem, USE, XEXP (note, 0))))
12741 place = tem;
12743 /* If we are doing a 3->2 combination, and we have a
12744 register which formerly died in i3 and was not used
12745 by i2, which now no longer dies in i3 and is used in
12746 i2 but does not die in i2, and place is between i2
12747 and i3, then we may need to move a link from place to
12748 i2. */
12749 if (i2 && INSN_UID (place) <= max_uid_cuid
12750 && INSN_CUID (place) > INSN_CUID (i2)
12751 && from_insn
12752 && INSN_CUID (from_insn) > INSN_CUID (i2)
12753 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
12755 rtx links = LOG_LINKS (place);
12756 LOG_LINKS (place) = 0;
12757 distribute_links (links);
12759 break;
12762 if (tem == BB_HEAD (bb))
12763 break;
12766 /* We haven't found an insn for the death note and it
12767 is still a REG_DEAD note, but we have hit the beginning
12768 of the block. If the existing life info says the reg
12769 was dead, there's nothing left to do. Otherwise, we'll
12770 need to do a global life update after combine. */
12771 if (REG_NOTE_KIND (note) == REG_DEAD && place == 0
12772 && REGNO_REG_SET_P (bb->global_live_at_start,
12773 REGNO (XEXP (note, 0))))
12774 SET_BIT (refresh_blocks, this_basic_block->index);
12777 /* If the register is set or already dead at PLACE, we needn't do
12778 anything with this note if it is still a REG_DEAD note.
12779 We can here if it is set at all, not if is it totally replace,
12780 which is what `dead_or_set_p' checks, so also check for it being
12781 set partially. */
12783 if (place && REG_NOTE_KIND (note) == REG_DEAD)
12785 unsigned int regno = REGNO (XEXP (note, 0));
12787 /* Similarly, if the instruction on which we want to place
12788 the note is a noop, we'll need do a global live update
12789 after we remove them in delete_noop_moves. */
12790 if (noop_move_p (place))
12791 SET_BIT (refresh_blocks, this_basic_block->index);
12793 if (dead_or_set_p (place, XEXP (note, 0))
12794 || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
12796 /* Unless the register previously died in PLACE, clear
12797 reg_last_death. [I no longer understand why this is
12798 being done.] */
12799 if (reg_last_death[regno] != place)
12800 reg_last_death[regno] = 0;
12801 place = 0;
12803 else
12804 reg_last_death[regno] = place;
12806 /* If this is a death note for a hard reg that is occupying
12807 multiple registers, ensure that we are still using all
12808 parts of the object. If we find a piece of the object
12809 that is unused, we must arrange for an appropriate REG_DEAD
12810 note to be added for it. However, we can't just emit a USE
12811 and tag the note to it, since the register might actually
12812 be dead; so we recourse, and the recursive call then finds
12813 the previous insn that used this register. */
12815 if (place && regno < FIRST_PSEUDO_REGISTER
12816 && hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))] > 1)
12818 unsigned int endregno
12819 = regno + hard_regno_nregs[regno]
12820 [GET_MODE (XEXP (note, 0))];
12821 int all_used = 1;
12822 unsigned int i;
12824 for (i = regno; i < endregno; i++)
12825 if ((! refers_to_regno_p (i, i + 1, PATTERN (place), 0)
12826 && ! find_regno_fusage (place, USE, i))
12827 || dead_or_set_regno_p (place, i))
12828 all_used = 0;
12830 if (! all_used)
12832 /* Put only REG_DEAD notes for pieces that are
12833 not already dead or set. */
12835 for (i = regno; i < endregno;
12836 i += hard_regno_nregs[i][reg_raw_mode[i]])
12838 rtx piece = regno_reg_rtx[i];
12839 basic_block bb = this_basic_block;
12841 if (! dead_or_set_p (place, piece)
12842 && ! reg_bitfield_target_p (piece,
12843 PATTERN (place)))
12845 rtx new_note
12846 = gen_rtx_EXPR_LIST (REG_DEAD, piece, NULL_RTX);
12848 distribute_notes (new_note, place, place,
12849 NULL_RTX);
12851 else if (! refers_to_regno_p (i, i + 1,
12852 PATTERN (place), 0)
12853 && ! find_regno_fusage (place, USE, i))
12854 for (tem = PREV_INSN (place); ;
12855 tem = PREV_INSN (tem))
12857 if (! INSN_P (tem))
12859 if (tem == BB_HEAD (bb))
12861 SET_BIT (refresh_blocks,
12862 this_basic_block->index);
12863 break;
12865 continue;
12867 if (dead_or_set_p (tem, piece)
12868 || reg_bitfield_target_p (piece,
12869 PATTERN (tem)))
12871 REG_NOTES (tem)
12872 = gen_rtx_EXPR_LIST (REG_UNUSED, piece,
12873 REG_NOTES (tem));
12874 break;
12880 place = 0;
12884 break;
12886 default:
12887 /* Any other notes should not be present at this point in the
12888 compilation. */
12889 abort ();
12892 if (place)
12894 XEXP (note, 1) = REG_NOTES (place);
12895 REG_NOTES (place) = note;
12897 else if ((REG_NOTE_KIND (note) == REG_DEAD
12898 || REG_NOTE_KIND (note) == REG_UNUSED)
12899 && GET_CODE (XEXP (note, 0)) == REG)
12900 REG_N_DEATHS (REGNO (XEXP (note, 0)))--;
12902 if (place2)
12904 if ((REG_NOTE_KIND (note) == REG_DEAD
12905 || REG_NOTE_KIND (note) == REG_UNUSED)
12906 && GET_CODE (XEXP (note, 0)) == REG)
12907 REG_N_DEATHS (REGNO (XEXP (note, 0)))++;
12909 REG_NOTES (place2) = gen_rtx_fmt_ee (GET_CODE (note),
12910 REG_NOTE_KIND (note),
12911 XEXP (note, 0),
12912 REG_NOTES (place2));
12917 /* Similarly to above, distribute the LOG_LINKS that used to be present on
12918 I3, I2, and I1 to new locations. This is also called to add a link
12919 pointing at I3 when I3's destination is changed. */
12921 static void
12922 distribute_links (rtx links)
12924 rtx link, next_link;
12926 for (link = links; link; link = next_link)
12928 rtx place = 0;
12929 rtx insn;
12930 rtx set, reg;
12932 next_link = XEXP (link, 1);
12934 /* If the insn that this link points to is a NOTE or isn't a single
12935 set, ignore it. In the latter case, it isn't clear what we
12936 can do other than ignore the link, since we can't tell which
12937 register it was for. Such links wouldn't be used by combine
12938 anyway.
12940 It is not possible for the destination of the target of the link to
12941 have been changed by combine. The only potential of this is if we
12942 replace I3, I2, and I1 by I3 and I2. But in that case the
12943 destination of I2 also remains unchanged. */
12945 if (GET_CODE (XEXP (link, 0)) == NOTE
12946 || (set = single_set (XEXP (link, 0))) == 0)
12947 continue;
12949 reg = SET_DEST (set);
12950 while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
12951 || GET_CODE (reg) == SIGN_EXTRACT
12952 || GET_CODE (reg) == STRICT_LOW_PART)
12953 reg = XEXP (reg, 0);
12955 /* A LOG_LINK is defined as being placed on the first insn that uses
12956 a register and points to the insn that sets the register. Start
12957 searching at the next insn after the target of the link and stop
12958 when we reach a set of the register or the end of the basic block.
12960 Note that this correctly handles the link that used to point from
12961 I3 to I2. Also note that not much searching is typically done here
12962 since most links don't point very far away. */
12964 for (insn = NEXT_INSN (XEXP (link, 0));
12965 (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
12966 || BB_HEAD (this_basic_block->next_bb) != insn));
12967 insn = NEXT_INSN (insn))
12968 if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
12970 if (reg_referenced_p (reg, PATTERN (insn)))
12971 place = insn;
12972 break;
12974 else if (GET_CODE (insn) == CALL_INSN
12975 && find_reg_fusage (insn, USE, reg))
12977 place = insn;
12978 break;
12980 else if (INSN_P (insn) && reg_set_p (reg, insn))
12981 break;
12983 /* If we found a place to put the link, place it there unless there
12984 is already a link to the same insn as LINK at that point. */
12986 if (place)
12988 rtx link2;
12990 for (link2 = LOG_LINKS (place); link2; link2 = XEXP (link2, 1))
12991 if (XEXP (link2, 0) == XEXP (link, 0))
12992 break;
12994 if (link2 == 0)
12996 XEXP (link, 1) = LOG_LINKS (place);
12997 LOG_LINKS (place) = link;
12999 /* Set added_links_insn to the earliest insn we added a
13000 link to. */
13001 if (added_links_insn == 0
13002 || INSN_CUID (added_links_insn) > INSN_CUID (place))
13003 added_links_insn = place;
13009 /* Subroutine of unmentioned_reg_p and callback from for_each_rtx.
13010 Check whether the expression pointer to by LOC is a register or
13011 memory, and if so return 1 if it isn't mentioned in the rtx EXPR.
13012 Otherwise return zero. */
13014 static int
13015 unmentioned_reg_p_1 (rtx *loc, void *expr)
13017 rtx x = *loc;
13019 if (x != NULL_RTX
13020 && (GET_CODE (x) == REG || GET_CODE (x) == MEM)
13021 && ! reg_mentioned_p (x, (rtx) expr))
13022 return 1;
13023 return 0;
13026 /* Check for any register or memory mentioned in EQUIV that is not
13027 mentioned in EXPR. This is used to restrict EQUIV to "specializations"
13028 of EXPR where some registers may have been replaced by constants. */
13030 static bool
13031 unmentioned_reg_p (rtx equiv, rtx expr)
13033 return for_each_rtx (&equiv, unmentioned_reg_p_1, expr);
13036 /* Compute INSN_CUID for INSN, which is an insn made by combine. */
13038 static int
13039 insn_cuid (rtx insn)
13041 while (insn != 0 && INSN_UID (insn) > max_uid_cuid
13042 && GET_CODE (insn) == INSN && GET_CODE (PATTERN (insn)) == USE)
13043 insn = NEXT_INSN (insn);
13045 if (INSN_UID (insn) > max_uid_cuid)
13046 abort ();
13048 return INSN_CUID (insn);
13051 void
13052 dump_combine_stats (FILE *file)
13054 fnotice
13055 (file,
13056 ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
13057 combine_attempts, combine_merges, combine_extras, combine_successes);
13060 void
13061 dump_combine_total_stats (FILE *file)
13063 fnotice
13064 (file,
13065 "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
13066 total_attempts, total_merges, total_extras, total_successes);