* config/i386/i386.md (*fscalexf4): Correct insn "mode"
[official-gcc.git] / gcc / combine.c
blobeea9bf2849d66bf2095301652cea2ad51fe594b9
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);
4998 if (GET_CODE (src) == COMPARE)
4999 op0 = XEXP (src, 0), op1 = XEXP (src, 1);
5000 else
5001 op0 = src, op1 = const0_rtx;
5003 tmp = simplify_relational_operation (old_code, compare_mode, VOIDmode,
5004 op0, op1);
5005 if (!tmp)
5006 new_code = old_code;
5007 else if (!CONSTANT_P (tmp))
5009 new_code = GET_CODE (tmp);
5010 op0 = XEXP (tmp, 0);
5011 op1 = XEXP (tmp, 1);
5013 else
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 (new_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 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
5040 compare_mode = GET_MODE (op0);
5041 else
5042 compare_mode = SELECT_CC_MODE (new_code, op0, op1);
5044 #ifndef HAVE_cc0
5045 /* If the mode changed, we have to change SET_DEST, the mode in the
5046 compare, and the mode in the place SET_DEST is used. If SET_DEST is
5047 a hard register, just build new versions with the proper mode. If it
5048 is a pseudo, we lose unless it is only time we set the pseudo, in
5049 which case we can safely change its mode. */
5050 if (compare_mode != GET_MODE (dest))
5052 unsigned int regno = REGNO (dest);
5053 rtx new_dest = gen_rtx_REG (compare_mode, regno);
5055 if (regno < FIRST_PSEUDO_REGISTER
5056 || (REG_N_SETS (regno) == 1 && ! REG_USERVAR_P (dest)))
5058 if (regno >= FIRST_PSEUDO_REGISTER)
5059 SUBST (regno_reg_rtx[regno], new_dest);
5061 SUBST (SET_DEST (x), new_dest);
5062 SUBST (XEXP (*cc_use, 0), new_dest);
5063 other_changed = 1;
5065 dest = new_dest;
5068 #endif /* cc0 */
5069 #endif /* SELECT_CC_MODE */
5071 /* If the code changed, we have to build a new comparison in
5072 undobuf.other_insn. */
5073 if (new_code != old_code)
5075 int other_changed_previously = other_changed;
5076 unsigned HOST_WIDE_INT mask;
5078 SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
5079 dest, const0_rtx));
5080 other_changed = 1;
5082 /* If the only change we made was to change an EQ into an NE or
5083 vice versa, OP0 has only one bit that might be nonzero, and OP1
5084 is zero, check if changing the user of the condition code will
5085 produce a valid insn. If it won't, we can keep the original code
5086 in that insn by surrounding our operation with an XOR. */
5088 if (((old_code == NE && new_code == EQ)
5089 || (old_code == EQ && new_code == NE))
5090 && ! other_changed_previously && op1 == const0_rtx
5091 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
5092 && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
5094 rtx pat = PATTERN (other_insn), note = 0;
5096 if ((recog_for_combine (&pat, other_insn, &note) < 0
5097 && ! check_asm_operands (pat)))
5099 PUT_CODE (*cc_use, old_code);
5100 other_changed = 0;
5102 op0 = gen_binary (XOR, GET_MODE (op0), op0, GEN_INT (mask));
5107 if (other_changed)
5108 undobuf.other_insn = other_insn;
5110 #ifdef HAVE_cc0
5111 /* If we are now comparing against zero, change our source if
5112 needed. If we do not use cc0, we always have a COMPARE. */
5113 if (op1 == const0_rtx && dest == cc0_rtx)
5115 SUBST (SET_SRC (x), op0);
5116 src = op0;
5118 else
5119 #endif
5121 /* Otherwise, if we didn't previously have a COMPARE in the
5122 correct mode, we need one. */
5123 if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
5125 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
5126 src = SET_SRC (x);
5128 else
5130 /* Otherwise, update the COMPARE if needed. */
5131 SUBST (XEXP (src, 0), op0);
5132 SUBST (XEXP (src, 1), op1);
5135 else
5137 /* Get SET_SRC in a form where we have placed back any
5138 compound expressions. Then do the checks below. */
5139 src = make_compound_operation (src, SET);
5140 SUBST (SET_SRC (x), src);
5143 /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
5144 and X being a REG or (subreg (reg)), we may be able to convert this to
5145 (set (subreg:m2 x) (op)).
5147 We can always do this if M1 is narrower than M2 because that means that
5148 we only care about the low bits of the result.
5150 However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
5151 perform a narrower operation than requested since the high-order bits will
5152 be undefined. On machine where it is defined, this transformation is safe
5153 as long as M1 and M2 have the same number of words. */
5155 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
5156 && !OBJECT_P (SUBREG_REG (src))
5157 && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
5158 / UNITS_PER_WORD)
5159 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
5160 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
5161 #ifndef WORD_REGISTER_OPERATIONS
5162 && (GET_MODE_SIZE (GET_MODE (src))
5163 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
5164 #endif
5165 #ifdef CANNOT_CHANGE_MODE_CLASS
5166 && ! (GET_CODE (dest) == REG && REGNO (dest) < FIRST_PSEUDO_REGISTER
5167 && REG_CANNOT_CHANGE_MODE_P (REGNO (dest),
5168 GET_MODE (SUBREG_REG (src)),
5169 GET_MODE (src)))
5170 #endif
5171 && (GET_CODE (dest) == REG
5172 || (GET_CODE (dest) == SUBREG
5173 && GET_CODE (SUBREG_REG (dest)) == REG)))
5175 SUBST (SET_DEST (x),
5176 gen_lowpart (GET_MODE (SUBREG_REG (src)),
5177 dest));
5178 SUBST (SET_SRC (x), SUBREG_REG (src));
5180 src = SET_SRC (x), dest = SET_DEST (x);
5183 #ifdef HAVE_cc0
5184 /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
5185 in SRC. */
5186 if (dest == cc0_rtx
5187 && GET_CODE (src) == SUBREG
5188 && subreg_lowpart_p (src)
5189 && (GET_MODE_BITSIZE (GET_MODE (src))
5190 < GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (src)))))
5192 rtx inner = SUBREG_REG (src);
5193 enum machine_mode inner_mode = GET_MODE (inner);
5195 /* Here we make sure that we don't have a sign bit on. */
5196 if (GET_MODE_BITSIZE (inner_mode) <= HOST_BITS_PER_WIDE_INT
5197 && (nonzero_bits (inner, inner_mode)
5198 < ((unsigned HOST_WIDE_INT) 1
5199 << (GET_MODE_BITSIZE (GET_MODE (src)) - 1))))
5201 SUBST (SET_SRC (x), inner);
5202 src = SET_SRC (x);
5205 #endif
5207 #ifdef LOAD_EXTEND_OP
5208 /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
5209 would require a paradoxical subreg. Replace the subreg with a
5210 zero_extend to avoid the reload that would otherwise be required. */
5212 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
5213 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != NIL
5214 && SUBREG_BYTE (src) == 0
5215 && (GET_MODE_SIZE (GET_MODE (src))
5216 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
5217 && GET_CODE (SUBREG_REG (src)) == MEM)
5219 SUBST (SET_SRC (x),
5220 gen_rtx_fmt_e (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
5221 GET_MODE (src), SUBREG_REG (src)));
5223 src = SET_SRC (x);
5225 #endif
5227 /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
5228 are comparing an item known to be 0 or -1 against 0, use a logical
5229 operation instead. Check for one of the arms being an IOR of the other
5230 arm with some value. We compute three terms to be IOR'ed together. In
5231 practice, at most two will be nonzero. Then we do the IOR's. */
5233 if (GET_CODE (dest) != PC
5234 && GET_CODE (src) == IF_THEN_ELSE
5235 && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
5236 && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
5237 && XEXP (XEXP (src, 0), 1) == const0_rtx
5238 && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
5239 #ifdef HAVE_conditional_move
5240 && ! can_conditionally_move_p (GET_MODE (src))
5241 #endif
5242 && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
5243 GET_MODE (XEXP (XEXP (src, 0), 0)))
5244 == GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (src, 0), 0))))
5245 && ! side_effects_p (src))
5247 rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
5248 ? XEXP (src, 1) : XEXP (src, 2));
5249 rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
5250 ? XEXP (src, 2) : XEXP (src, 1));
5251 rtx term1 = const0_rtx, term2, term3;
5253 if (GET_CODE (true_rtx) == IOR
5254 && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
5255 term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
5256 else if (GET_CODE (true_rtx) == IOR
5257 && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
5258 term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
5259 else if (GET_CODE (false_rtx) == IOR
5260 && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
5261 term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
5262 else if (GET_CODE (false_rtx) == IOR
5263 && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
5264 term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
5266 term2 = gen_binary (AND, GET_MODE (src),
5267 XEXP (XEXP (src, 0), 0), true_rtx);
5268 term3 = gen_binary (AND, GET_MODE (src),
5269 simplify_gen_unary (NOT, GET_MODE (src),
5270 XEXP (XEXP (src, 0), 0),
5271 GET_MODE (src)),
5272 false_rtx);
5274 SUBST (SET_SRC (x),
5275 gen_binary (IOR, GET_MODE (src),
5276 gen_binary (IOR, GET_MODE (src), term1, term2),
5277 term3));
5279 src = SET_SRC (x);
5282 /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
5283 whole thing fail. */
5284 if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
5285 return src;
5286 else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
5287 return dest;
5288 else
5289 /* Convert this into a field assignment operation, if possible. */
5290 return make_field_assignment (x);
5293 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
5294 result. */
5296 static rtx
5297 simplify_logical (rtx x)
5299 enum machine_mode mode = GET_MODE (x);
5300 rtx op0 = XEXP (x, 0);
5301 rtx op1 = XEXP (x, 1);
5302 rtx reversed;
5304 switch (GET_CODE (x))
5306 case AND:
5307 /* Convert (A ^ B) & A to A & (~B) since the latter is often a single
5308 insn (and may simplify more). */
5309 if (GET_CODE (op0) == XOR
5310 && rtx_equal_p (XEXP (op0, 0), op1)
5311 && ! side_effects_p (op1))
5312 x = gen_binary (AND, mode,
5313 simplify_gen_unary (NOT, mode, XEXP (op0, 1), mode),
5314 op1);
5316 if (GET_CODE (op0) == XOR
5317 && rtx_equal_p (XEXP (op0, 1), op1)
5318 && ! side_effects_p (op1))
5319 x = gen_binary (AND, mode,
5320 simplify_gen_unary (NOT, mode, XEXP (op0, 0), mode),
5321 op1);
5323 /* Similarly for (~(A ^ B)) & A. */
5324 if (GET_CODE (op0) == NOT
5325 && GET_CODE (XEXP (op0, 0)) == XOR
5326 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), op1)
5327 && ! side_effects_p (op1))
5328 x = gen_binary (AND, mode, XEXP (XEXP (op0, 0), 1), op1);
5330 if (GET_CODE (op0) == NOT
5331 && GET_CODE (XEXP (op0, 0)) == XOR
5332 && rtx_equal_p (XEXP (XEXP (op0, 0), 1), op1)
5333 && ! side_effects_p (op1))
5334 x = gen_binary (AND, mode, XEXP (XEXP (op0, 0), 0), op1);
5336 /* We can call simplify_and_const_int only if we don't lose
5337 any (sign) bits when converting INTVAL (op1) to
5338 "unsigned HOST_WIDE_INT". */
5339 if (GET_CODE (op1) == CONST_INT
5340 && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5341 || INTVAL (op1) > 0))
5343 x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
5345 /* If we have (ior (and (X C1) C2)) and the next restart would be
5346 the last, simplify this by making C1 as small as possible
5347 and then exit. Only do this if C1 actually changes: for now
5348 this only saves memory but, should this transformation be
5349 moved to simplify-rtx.c, we'd risk unbounded recursion there. */
5350 if (GET_CODE (x) == IOR && GET_CODE (op0) == AND
5351 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5352 && GET_CODE (op1) == CONST_INT
5353 && (INTVAL (XEXP (op0, 1)) & INTVAL (op1)) != 0)
5354 return gen_binary (IOR, mode,
5355 gen_binary (AND, mode, XEXP (op0, 0),
5356 GEN_INT (INTVAL (XEXP (op0, 1))
5357 & ~INTVAL (op1))), op1);
5359 if (GET_CODE (x) != AND)
5360 return x;
5362 op0 = XEXP (x, 0);
5363 op1 = XEXP (x, 1);
5366 /* Convert (A | B) & A to A. */
5367 if (GET_CODE (op0) == IOR
5368 && (rtx_equal_p (XEXP (op0, 0), op1)
5369 || rtx_equal_p (XEXP (op0, 1), op1))
5370 && ! side_effects_p (XEXP (op0, 0))
5371 && ! side_effects_p (XEXP (op0, 1)))
5372 return op1;
5374 /* In the following group of tests (and those in case IOR below),
5375 we start with some combination of logical operations and apply
5376 the distributive law followed by the inverse distributive law.
5377 Most of the time, this results in no change. However, if some of
5378 the operands are the same or inverses of each other, simplifications
5379 will result.
5381 For example, (and (ior A B) (not B)) can occur as the result of
5382 expanding a bit field assignment. When we apply the distributive
5383 law to this, we get (ior (and (A (not B))) (and (B (not B)))),
5384 which then simplifies to (and (A (not B))).
5386 If we have (and (ior A B) C), apply the distributive law and then
5387 the inverse distributive law to see if things simplify. */
5389 if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
5391 x = apply_distributive_law
5392 (gen_binary (GET_CODE (op0), mode,
5393 gen_binary (AND, mode, XEXP (op0, 0), op1),
5394 gen_binary (AND, mode, XEXP (op0, 1),
5395 copy_rtx (op1))));
5396 if (GET_CODE (x) != AND)
5397 return x;
5400 if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
5401 return apply_distributive_law
5402 (gen_binary (GET_CODE (op1), mode,
5403 gen_binary (AND, mode, XEXP (op1, 0), op0),
5404 gen_binary (AND, mode, XEXP (op1, 1),
5405 copy_rtx (op0))));
5407 /* Similarly, taking advantage of the fact that
5408 (and (not A) (xor B C)) == (xor (ior A B) (ior A C)) */
5410 if (GET_CODE (op0) == NOT && GET_CODE (op1) == XOR)
5411 return apply_distributive_law
5412 (gen_binary (XOR, mode,
5413 gen_binary (IOR, mode, XEXP (op0, 0), XEXP (op1, 0)),
5414 gen_binary (IOR, mode, copy_rtx (XEXP (op0, 0)),
5415 XEXP (op1, 1))));
5417 else if (GET_CODE (op1) == NOT && GET_CODE (op0) == XOR)
5418 return apply_distributive_law
5419 (gen_binary (XOR, mode,
5420 gen_binary (IOR, mode, XEXP (op1, 0), XEXP (op0, 0)),
5421 gen_binary (IOR, mode, copy_rtx (XEXP (op1, 0)), XEXP (op0, 1))));
5422 break;
5424 case IOR:
5425 /* (ior A C) is C if all bits of A that might be nonzero are on in C. */
5426 if (GET_CODE (op1) == CONST_INT
5427 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5428 && (nonzero_bits (op0, mode) & ~INTVAL (op1)) == 0)
5429 return op1;
5431 /* Convert (A & B) | A to A. */
5432 if (GET_CODE (op0) == AND
5433 && (rtx_equal_p (XEXP (op0, 0), op1)
5434 || rtx_equal_p (XEXP (op0, 1), op1))
5435 && ! side_effects_p (XEXP (op0, 0))
5436 && ! side_effects_p (XEXP (op0, 1)))
5437 return op1;
5439 /* If we have (ior (and A B) C), apply the distributive law and then
5440 the inverse distributive law to see if things simplify. */
5442 if (GET_CODE (op0) == AND)
5444 x = apply_distributive_law
5445 (gen_binary (AND, mode,
5446 gen_binary (IOR, mode, XEXP (op0, 0), op1),
5447 gen_binary (IOR, mode, XEXP (op0, 1),
5448 copy_rtx (op1))));
5450 if (GET_CODE (x) != IOR)
5451 return x;
5454 if (GET_CODE (op1) == AND)
5456 x = apply_distributive_law
5457 (gen_binary (AND, mode,
5458 gen_binary (IOR, mode, XEXP (op1, 0), op0),
5459 gen_binary (IOR, mode, XEXP (op1, 1),
5460 copy_rtx (op0))));
5462 if (GET_CODE (x) != IOR)
5463 return x;
5466 /* Convert (ior (ashift A CX) (lshiftrt A CY)) where CX+CY equals the
5467 mode size to (rotate A CX). */
5469 if (((GET_CODE (op0) == ASHIFT && GET_CODE (op1) == LSHIFTRT)
5470 || (GET_CODE (op1) == ASHIFT && GET_CODE (op0) == LSHIFTRT))
5471 && rtx_equal_p (XEXP (op0, 0), XEXP (op1, 0))
5472 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5473 && GET_CODE (XEXP (op1, 1)) == CONST_INT
5474 && (INTVAL (XEXP (op0, 1)) + INTVAL (XEXP (op1, 1))
5475 == GET_MODE_BITSIZE (mode)))
5476 return gen_rtx_ROTATE (mode, XEXP (op0, 0),
5477 (GET_CODE (op0) == ASHIFT
5478 ? XEXP (op0, 1) : XEXP (op1, 1)));
5480 /* If OP0 is (ashiftrt (plus ...) C), it might actually be
5481 a (sign_extend (plus ...)). If so, OP1 is a CONST_INT, and the PLUS
5482 does not affect any of the bits in OP1, it can really be done
5483 as a PLUS and we can associate. We do this by seeing if OP1
5484 can be safely shifted left C bits. */
5485 if (GET_CODE (op1) == CONST_INT && GET_CODE (op0) == ASHIFTRT
5486 && GET_CODE (XEXP (op0, 0)) == PLUS
5487 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
5488 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5489 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT)
5491 int count = INTVAL (XEXP (op0, 1));
5492 HOST_WIDE_INT mask = INTVAL (op1) << count;
5494 if (mask >> count == INTVAL (op1)
5495 && (mask & nonzero_bits (XEXP (op0, 0), mode)) == 0)
5497 SUBST (XEXP (XEXP (op0, 0), 1),
5498 GEN_INT (INTVAL (XEXP (XEXP (op0, 0), 1)) | mask));
5499 return op0;
5502 break;
5504 case XOR:
5505 /* If we are XORing two things that have no bits in common,
5506 convert them into an IOR. This helps to detect rotation encoded
5507 using those methods and possibly other simplifications. */
5509 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5510 && (nonzero_bits (op0, mode)
5511 & nonzero_bits (op1, mode)) == 0)
5512 return (gen_binary (IOR, mode, op0, op1));
5514 /* Convert (XOR (NOT x) (NOT y)) to (XOR x y).
5515 Also convert (XOR (NOT x) y) to (NOT (XOR x y)), similarly for
5516 (NOT y). */
5518 int num_negated = 0;
5520 if (GET_CODE (op0) == NOT)
5521 num_negated++, op0 = XEXP (op0, 0);
5522 if (GET_CODE (op1) == NOT)
5523 num_negated++, op1 = XEXP (op1, 0);
5525 if (num_negated == 2)
5527 SUBST (XEXP (x, 0), op0);
5528 SUBST (XEXP (x, 1), op1);
5530 else if (num_negated == 1)
5531 return
5532 simplify_gen_unary (NOT, mode, gen_binary (XOR, mode, op0, op1),
5533 mode);
5536 /* Convert (xor (and A B) B) to (and (not A) B). The latter may
5537 correspond to a machine insn or result in further simplifications
5538 if B is a constant. */
5540 if (GET_CODE (op0) == AND
5541 && rtx_equal_p (XEXP (op0, 1), op1)
5542 && ! side_effects_p (op1))
5543 return gen_binary (AND, mode,
5544 simplify_gen_unary (NOT, mode, XEXP (op0, 0), mode),
5545 op1);
5547 else if (GET_CODE (op0) == AND
5548 && rtx_equal_p (XEXP (op0, 0), op1)
5549 && ! side_effects_p (op1))
5550 return gen_binary (AND, mode,
5551 simplify_gen_unary (NOT, mode, XEXP (op0, 1), mode),
5552 op1);
5554 /* (xor (comparison foo bar) (const_int 1)) can become the reversed
5555 comparison if STORE_FLAG_VALUE is 1. */
5556 if (STORE_FLAG_VALUE == 1
5557 && op1 == const1_rtx
5558 && COMPARISON_P (op0)
5559 && (reversed = reversed_comparison (op0, mode, XEXP (op0, 0),
5560 XEXP (op0, 1))))
5561 return reversed;
5563 /* (lshiftrt foo C) where C is the number of bits in FOO minus 1
5564 is (lt foo (const_int 0)), so we can perform the above
5565 simplification if STORE_FLAG_VALUE is 1. */
5567 if (STORE_FLAG_VALUE == 1
5568 && op1 == const1_rtx
5569 && GET_CODE (op0) == LSHIFTRT
5570 && GET_CODE (XEXP (op0, 1)) == CONST_INT
5571 && INTVAL (XEXP (op0, 1)) == GET_MODE_BITSIZE (mode) - 1)
5572 return gen_rtx_GE (mode, XEXP (op0, 0), const0_rtx);
5574 /* (xor (comparison foo bar) (const_int sign-bit))
5575 when STORE_FLAG_VALUE is the sign bit. */
5576 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5577 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
5578 == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
5579 && op1 == const_true_rtx
5580 && COMPARISON_P (op0)
5581 && (reversed = reversed_comparison (op0, mode, XEXP (op0, 0),
5582 XEXP (op0, 1))))
5583 return reversed;
5585 break;
5587 default:
5588 abort ();
5591 return x;
5594 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
5595 operations" because they can be replaced with two more basic operations.
5596 ZERO_EXTEND is also considered "compound" because it can be replaced with
5597 an AND operation, which is simpler, though only one operation.
5599 The function expand_compound_operation is called with an rtx expression
5600 and will convert it to the appropriate shifts and AND operations,
5601 simplifying at each stage.
5603 The function make_compound_operation is called to convert an expression
5604 consisting of shifts and ANDs into the equivalent compound expression.
5605 It is the inverse of this function, loosely speaking. */
5607 static rtx
5608 expand_compound_operation (rtx x)
5610 unsigned HOST_WIDE_INT pos = 0, len;
5611 int unsignedp = 0;
5612 unsigned int modewidth;
5613 rtx tem;
5615 switch (GET_CODE (x))
5617 case ZERO_EXTEND:
5618 unsignedp = 1;
5619 case SIGN_EXTEND:
5620 /* We can't necessarily use a const_int for a multiword mode;
5621 it depends on implicitly extending the value.
5622 Since we don't know the right way to extend it,
5623 we can't tell whether the implicit way is right.
5625 Even for a mode that is no wider than a const_int,
5626 we can't win, because we need to sign extend one of its bits through
5627 the rest of it, and we don't know which bit. */
5628 if (GET_CODE (XEXP (x, 0)) == CONST_INT)
5629 return x;
5631 /* Return if (subreg:MODE FROM 0) is not a safe replacement for
5632 (zero_extend:MODE FROM) or (sign_extend:MODE FROM). It is for any MEM
5633 because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
5634 reloaded. If not for that, MEM's would very rarely be safe.
5636 Reject MODEs bigger than a word, because we might not be able
5637 to reference a two-register group starting with an arbitrary register
5638 (and currently gen_lowpart might crash for a SUBREG). */
5640 if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
5641 return x;
5643 /* Reject MODEs that aren't scalar integers because turning vector
5644 or complex modes into shifts causes problems. */
5646 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
5647 return x;
5649 len = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)));
5650 /* If the inner object has VOIDmode (the only way this can happen
5651 is if it is an ASM_OPERANDS), we can't do anything since we don't
5652 know how much masking to do. */
5653 if (len == 0)
5654 return x;
5656 break;
5658 case ZERO_EXTRACT:
5659 unsignedp = 1;
5660 case SIGN_EXTRACT:
5661 /* If the operand is a CLOBBER, just return it. */
5662 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
5663 return XEXP (x, 0);
5665 if (GET_CODE (XEXP (x, 1)) != CONST_INT
5666 || GET_CODE (XEXP (x, 2)) != CONST_INT
5667 || GET_MODE (XEXP (x, 0)) == VOIDmode)
5668 return x;
5670 /* Reject MODEs that aren't scalar integers because turning vector
5671 or complex modes into shifts causes problems. */
5673 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
5674 return x;
5676 len = INTVAL (XEXP (x, 1));
5677 pos = INTVAL (XEXP (x, 2));
5679 /* If this goes outside the object being extracted, replace the object
5680 with a (use (mem ...)) construct that only combine understands
5681 and is used only for this purpose. */
5682 if (len + pos > GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
5683 SUBST (XEXP (x, 0), gen_rtx_USE (GET_MODE (x), XEXP (x, 0)));
5685 if (BITS_BIG_ENDIAN)
5686 pos = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - len - pos;
5688 break;
5690 default:
5691 return x;
5693 /* Convert sign extension to zero extension, if we know that the high
5694 bit is not set, as this is easier to optimize. It will be converted
5695 back to cheaper alternative in make_extraction. */
5696 if (GET_CODE (x) == SIGN_EXTEND
5697 && (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5698 && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
5699 & ~(((unsigned HOST_WIDE_INT)
5700 GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
5701 >> 1))
5702 == 0)))
5704 rtx temp = gen_rtx_ZERO_EXTEND (GET_MODE (x), XEXP (x, 0));
5705 rtx temp2 = expand_compound_operation (temp);
5707 /* Make sure this is a profitable operation. */
5708 if (rtx_cost (x, SET) > rtx_cost (temp2, SET))
5709 return temp2;
5710 else if (rtx_cost (x, SET) > rtx_cost (temp, SET))
5711 return temp;
5712 else
5713 return x;
5716 /* We can optimize some special cases of ZERO_EXTEND. */
5717 if (GET_CODE (x) == ZERO_EXTEND)
5719 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
5720 know that the last value didn't have any inappropriate bits
5721 set. */
5722 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
5723 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
5724 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5725 && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
5726 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5727 return XEXP (XEXP (x, 0), 0);
5729 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
5730 if (GET_CODE (XEXP (x, 0)) == SUBREG
5731 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
5732 && subreg_lowpart_p (XEXP (x, 0))
5733 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
5734 && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
5735 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5736 return SUBREG_REG (XEXP (x, 0));
5738 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
5739 is a comparison and STORE_FLAG_VALUE permits. This is like
5740 the first case, but it works even when GET_MODE (x) is larger
5741 than HOST_WIDE_INT. */
5742 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
5743 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
5744 && COMPARISON_P (XEXP (XEXP (x, 0), 0))
5745 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5746 <= HOST_BITS_PER_WIDE_INT)
5747 && ((HOST_WIDE_INT) STORE_FLAG_VALUE
5748 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5749 return XEXP (XEXP (x, 0), 0);
5751 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
5752 if (GET_CODE (XEXP (x, 0)) == SUBREG
5753 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
5754 && subreg_lowpart_p (XEXP (x, 0))
5755 && COMPARISON_P (SUBREG_REG (XEXP (x, 0)))
5756 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
5757 <= HOST_BITS_PER_WIDE_INT)
5758 && ((HOST_WIDE_INT) STORE_FLAG_VALUE
5759 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
5760 return SUBREG_REG (XEXP (x, 0));
5764 /* If we reach here, we want to return a pair of shifts. The inner
5765 shift is a left shift of BITSIZE - POS - LEN bits. The outer
5766 shift is a right shift of BITSIZE - LEN bits. It is arithmetic or
5767 logical depending on the value of UNSIGNEDP.
5769 If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
5770 converted into an AND of a shift.
5772 We must check for the case where the left shift would have a negative
5773 count. This can happen in a case like (x >> 31) & 255 on machines
5774 that can't shift by a constant. On those machines, we would first
5775 combine the shift with the AND to produce a variable-position
5776 extraction. Then the constant of 31 would be substituted in to produce
5777 a such a position. */
5779 modewidth = GET_MODE_BITSIZE (GET_MODE (x));
5780 if (modewidth + len >= pos)
5781 tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
5782 GET_MODE (x),
5783 simplify_shift_const (NULL_RTX, ASHIFT,
5784 GET_MODE (x),
5785 XEXP (x, 0),
5786 modewidth - pos - len),
5787 modewidth - len);
5789 else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
5790 tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
5791 simplify_shift_const (NULL_RTX, LSHIFTRT,
5792 GET_MODE (x),
5793 XEXP (x, 0), pos),
5794 ((HOST_WIDE_INT) 1 << len) - 1);
5795 else
5796 /* Any other cases we can't handle. */
5797 return x;
5799 /* If we couldn't do this for some reason, return the original
5800 expression. */
5801 if (GET_CODE (tem) == CLOBBER)
5802 return x;
5804 return tem;
5807 /* X is a SET which contains an assignment of one object into
5808 a part of another (such as a bit-field assignment, STRICT_LOW_PART,
5809 or certain SUBREGS). If possible, convert it into a series of
5810 logical operations.
5812 We half-heartedly support variable positions, but do not at all
5813 support variable lengths. */
5815 static rtx
5816 expand_field_assignment (rtx x)
5818 rtx inner;
5819 rtx pos; /* Always counts from low bit. */
5820 int len;
5821 rtx mask;
5822 enum machine_mode compute_mode;
5824 /* Loop until we find something we can't simplify. */
5825 while (1)
5827 if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
5828 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
5830 inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
5831 len = GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)));
5832 pos = GEN_INT (subreg_lsb (XEXP (SET_DEST (x), 0)));
5834 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
5835 && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT)
5837 inner = XEXP (SET_DEST (x), 0);
5838 len = INTVAL (XEXP (SET_DEST (x), 1));
5839 pos = XEXP (SET_DEST (x), 2);
5841 /* If the position is constant and spans the width of INNER,
5842 surround INNER with a USE to indicate this. */
5843 if (GET_CODE (pos) == CONST_INT
5844 && INTVAL (pos) + len > GET_MODE_BITSIZE (GET_MODE (inner)))
5845 inner = gen_rtx_USE (GET_MODE (SET_DEST (x)), inner);
5847 if (BITS_BIG_ENDIAN)
5849 if (GET_CODE (pos) == CONST_INT)
5850 pos = GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner)) - len
5851 - INTVAL (pos));
5852 else if (GET_CODE (pos) == MINUS
5853 && GET_CODE (XEXP (pos, 1)) == CONST_INT
5854 && (INTVAL (XEXP (pos, 1))
5855 == GET_MODE_BITSIZE (GET_MODE (inner)) - len))
5856 /* If position is ADJUST - X, new position is X. */
5857 pos = XEXP (pos, 0);
5858 else
5859 pos = gen_binary (MINUS, GET_MODE (pos),
5860 GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner))
5861 - len),
5862 pos);
5866 /* A SUBREG between two modes that occupy the same numbers of words
5867 can be done by moving the SUBREG to the source. */
5868 else if (GET_CODE (SET_DEST (x)) == SUBREG
5869 /* We need SUBREGs to compute nonzero_bits properly. */
5870 && nonzero_sign_valid
5871 && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
5872 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
5873 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
5874 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
5876 x = gen_rtx_SET (VOIDmode, SUBREG_REG (SET_DEST (x)),
5877 gen_lowpart
5878 (GET_MODE (SUBREG_REG (SET_DEST (x))),
5879 SET_SRC (x)));
5880 continue;
5882 else
5883 break;
5885 while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
5886 inner = SUBREG_REG (inner);
5888 compute_mode = GET_MODE (inner);
5890 /* Don't attempt bitwise arithmetic on non scalar integer modes. */
5891 if (! SCALAR_INT_MODE_P (compute_mode))
5893 enum machine_mode imode;
5895 /* Don't do anything for vector or complex integral types. */
5896 if (! FLOAT_MODE_P (compute_mode))
5897 break;
5899 /* Try to find an integral mode to pun with. */
5900 imode = mode_for_size (GET_MODE_BITSIZE (compute_mode), MODE_INT, 0);
5901 if (imode == BLKmode)
5902 break;
5904 compute_mode = imode;
5905 inner = gen_lowpart (imode, inner);
5908 /* Compute a mask of LEN bits, if we can do this on the host machine. */
5909 if (len < HOST_BITS_PER_WIDE_INT)
5910 mask = GEN_INT (((HOST_WIDE_INT) 1 << len) - 1);
5911 else
5912 break;
5914 /* Now compute the equivalent expression. Make a copy of INNER
5915 for the SET_DEST in case it is a MEM into which we will substitute;
5916 we don't want shared RTL in that case. */
5917 x = gen_rtx_SET
5918 (VOIDmode, copy_rtx (inner),
5919 gen_binary (IOR, compute_mode,
5920 gen_binary (AND, compute_mode,
5921 simplify_gen_unary (NOT, compute_mode,
5922 gen_binary (ASHIFT,
5923 compute_mode,
5924 mask, pos),
5925 compute_mode),
5926 inner),
5927 gen_binary (ASHIFT, compute_mode,
5928 gen_binary (AND, compute_mode,
5929 gen_lowpart
5930 (compute_mode, SET_SRC (x)),
5931 mask),
5932 pos)));
5935 return x;
5938 /* Return an RTX for a reference to LEN bits of INNER. If POS_RTX is nonzero,
5939 it is an RTX that represents a variable starting position; otherwise,
5940 POS is the (constant) starting bit position (counted from the LSB).
5942 INNER may be a USE. This will occur when we started with a bitfield
5943 that went outside the boundary of the object in memory, which is
5944 allowed on most machines. To isolate this case, we produce a USE
5945 whose mode is wide enough and surround the MEM with it. The only
5946 code that understands the USE is this routine. If it is not removed,
5947 it will cause the resulting insn not to match.
5949 UNSIGNEDP is nonzero for an unsigned reference and zero for a
5950 signed reference.
5952 IN_DEST is nonzero if this is a reference in the destination of a
5953 SET. This is used when a ZERO_ or SIGN_EXTRACT isn't needed. If nonzero,
5954 a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
5955 be used.
5957 IN_COMPARE is nonzero if we are in a COMPARE. This means that a
5958 ZERO_EXTRACT should be built even for bits starting at bit 0.
5960 MODE is the desired mode of the result (if IN_DEST == 0).
5962 The result is an RTX for the extraction or NULL_RTX if the target
5963 can't handle it. */
5965 static rtx
5966 make_extraction (enum machine_mode mode, rtx inner, HOST_WIDE_INT pos,
5967 rtx pos_rtx, unsigned HOST_WIDE_INT len, int unsignedp,
5968 int in_dest, int in_compare)
5970 /* This mode describes the size of the storage area
5971 to fetch the overall value from. Within that, we
5972 ignore the POS lowest bits, etc. */
5973 enum machine_mode is_mode = GET_MODE (inner);
5974 enum machine_mode inner_mode;
5975 enum machine_mode wanted_inner_mode = byte_mode;
5976 enum machine_mode wanted_inner_reg_mode = word_mode;
5977 enum machine_mode pos_mode = word_mode;
5978 enum machine_mode extraction_mode = word_mode;
5979 enum machine_mode tmode = mode_for_size (len, MODE_INT, 1);
5980 int spans_byte = 0;
5981 rtx new = 0;
5982 rtx orig_pos_rtx = pos_rtx;
5983 HOST_WIDE_INT orig_pos;
5985 /* Get some information about INNER and get the innermost object. */
5986 if (GET_CODE (inner) == USE)
5987 /* (use:SI (mem:QI foo)) stands for (mem:SI foo). */
5988 /* We don't need to adjust the position because we set up the USE
5989 to pretend that it was a full-word object. */
5990 spans_byte = 1, inner = XEXP (inner, 0);
5991 else if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
5993 /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
5994 consider just the QI as the memory to extract from.
5995 The subreg adds or removes high bits; its mode is
5996 irrelevant to the meaning of this extraction,
5997 since POS and LEN count from the lsb. */
5998 if (GET_CODE (SUBREG_REG (inner)) == MEM)
5999 is_mode = GET_MODE (SUBREG_REG (inner));
6000 inner = SUBREG_REG (inner);
6002 else if (GET_CODE (inner) == ASHIFT
6003 && GET_CODE (XEXP (inner, 1)) == CONST_INT
6004 && pos_rtx == 0 && pos == 0
6005 && len > (unsigned HOST_WIDE_INT) INTVAL (XEXP (inner, 1)))
6007 /* We're extracting the least significant bits of an rtx
6008 (ashift X (const_int C)), where LEN > C. Extract the
6009 least significant (LEN - C) bits of X, giving an rtx
6010 whose mode is MODE, then shift it left C times. */
6011 new = make_extraction (mode, XEXP (inner, 0),
6012 0, 0, len - INTVAL (XEXP (inner, 1)),
6013 unsignedp, in_dest, in_compare);
6014 if (new != 0)
6015 return gen_rtx_ASHIFT (mode, new, XEXP (inner, 1));
6018 inner_mode = GET_MODE (inner);
6020 if (pos_rtx && GET_CODE (pos_rtx) == CONST_INT)
6021 pos = INTVAL (pos_rtx), pos_rtx = 0;
6023 /* See if this can be done without an extraction. We never can if the
6024 width of the field is not the same as that of some integer mode. For
6025 registers, we can only avoid the extraction if the position is at the
6026 low-order bit and this is either not in the destination or we have the
6027 appropriate STRICT_LOW_PART operation available.
6029 For MEM, we can avoid an extract if the field starts on an appropriate
6030 boundary and we can change the mode of the memory reference. However,
6031 we cannot directly access the MEM if we have a USE and the underlying
6032 MEM is not TMODE. This combination means that MEM was being used in a
6033 context where bits outside its mode were being referenced; that is only
6034 valid in bit-field insns. */
6036 if (tmode != BLKmode
6037 && ! (spans_byte && inner_mode != tmode)
6038 && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
6039 && GET_CODE (inner) != MEM
6040 && (! in_dest
6041 || (GET_CODE (inner) == REG
6042 && have_insn_for (STRICT_LOW_PART, tmode))))
6043 || (GET_CODE (inner) == MEM && pos_rtx == 0
6044 && (pos
6045 % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
6046 : BITS_PER_UNIT)) == 0
6047 /* We can't do this if we are widening INNER_MODE (it
6048 may not be aligned, for one thing). */
6049 && GET_MODE_BITSIZE (inner_mode) >= GET_MODE_BITSIZE (tmode)
6050 && (inner_mode == tmode
6051 || (! mode_dependent_address_p (XEXP (inner, 0))
6052 && ! MEM_VOLATILE_P (inner))))))
6054 /* If INNER is a MEM, make a new MEM that encompasses just the desired
6055 field. If the original and current mode are the same, we need not
6056 adjust the offset. Otherwise, we do if bytes big endian.
6058 If INNER is not a MEM, get a piece consisting of just the field
6059 of interest (in this case POS % BITS_PER_WORD must be 0). */
6061 if (GET_CODE (inner) == MEM)
6063 HOST_WIDE_INT offset;
6065 /* POS counts from lsb, but make OFFSET count in memory order. */
6066 if (BYTES_BIG_ENDIAN)
6067 offset = (GET_MODE_BITSIZE (is_mode) - len - pos) / BITS_PER_UNIT;
6068 else
6069 offset = pos / BITS_PER_UNIT;
6071 new = adjust_address_nv (inner, tmode, offset);
6073 else if (GET_CODE (inner) == REG)
6075 if (tmode != inner_mode)
6077 /* We can't call gen_lowpart in a DEST since we
6078 always want a SUBREG (see below) and it would sometimes
6079 return a new hard register. */
6080 if (pos || in_dest)
6082 HOST_WIDE_INT final_word = pos / BITS_PER_WORD;
6084 if (WORDS_BIG_ENDIAN
6085 && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
6086 final_word = ((GET_MODE_SIZE (inner_mode)
6087 - GET_MODE_SIZE (tmode))
6088 / UNITS_PER_WORD) - final_word;
6090 final_word *= UNITS_PER_WORD;
6091 if (BYTES_BIG_ENDIAN &&
6092 GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (tmode))
6093 final_word += (GET_MODE_SIZE (inner_mode)
6094 - GET_MODE_SIZE (tmode)) % UNITS_PER_WORD;
6096 /* Avoid creating invalid subregs, for example when
6097 simplifying (x>>32)&255. */
6098 if (final_word >= GET_MODE_SIZE (inner_mode))
6099 return NULL_RTX;
6101 new = gen_rtx_SUBREG (tmode, inner, final_word);
6103 else
6104 new = gen_lowpart (tmode, inner);
6106 else
6107 new = inner;
6109 else
6110 new = force_to_mode (inner, tmode,
6111 len >= HOST_BITS_PER_WIDE_INT
6112 ? ~(unsigned HOST_WIDE_INT) 0
6113 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
6114 NULL_RTX, 0);
6116 /* If this extraction is going into the destination of a SET,
6117 make a STRICT_LOW_PART unless we made a MEM. */
6119 if (in_dest)
6120 return (GET_CODE (new) == MEM ? new
6121 : (GET_CODE (new) != SUBREG
6122 ? gen_rtx_CLOBBER (tmode, const0_rtx)
6123 : gen_rtx_STRICT_LOW_PART (VOIDmode, new)));
6125 if (mode == tmode)
6126 return new;
6128 if (GET_CODE (new) == CONST_INT)
6129 return gen_int_mode (INTVAL (new), mode);
6131 /* If we know that no extraneous bits are set, and that the high
6132 bit is not set, convert the extraction to the cheaper of
6133 sign and zero extension, that are equivalent in these cases. */
6134 if (flag_expensive_optimizations
6135 && (GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
6136 && ((nonzero_bits (new, tmode)
6137 & ~(((unsigned HOST_WIDE_INT)
6138 GET_MODE_MASK (tmode))
6139 >> 1))
6140 == 0)))
6142 rtx temp = gen_rtx_ZERO_EXTEND (mode, new);
6143 rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new);
6145 /* Prefer ZERO_EXTENSION, since it gives more information to
6146 backends. */
6147 if (rtx_cost (temp, SET) <= rtx_cost (temp1, SET))
6148 return temp;
6149 return temp1;
6152 /* Otherwise, sign- or zero-extend unless we already are in the
6153 proper mode. */
6155 return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
6156 mode, new));
6159 /* Unless this is a COMPARE or we have a funny memory reference,
6160 don't do anything with zero-extending field extracts starting at
6161 the low-order bit since they are simple AND operations. */
6162 if (pos_rtx == 0 && pos == 0 && ! in_dest
6163 && ! in_compare && ! spans_byte && unsignedp)
6164 return 0;
6166 /* Unless we are allowed to span bytes or INNER is not MEM, reject this if
6167 we would be spanning bytes or if the position is not a constant and the
6168 length is not 1. In all other cases, we would only be going outside
6169 our object in cases when an original shift would have been
6170 undefined. */
6171 if (! spans_byte && GET_CODE (inner) == MEM
6172 && ((pos_rtx == 0 && pos + len > GET_MODE_BITSIZE (is_mode))
6173 || (pos_rtx != 0 && len != 1)))
6174 return 0;
6176 /* Get the mode to use should INNER not be a MEM, the mode for the position,
6177 and the mode for the result. */
6178 if (in_dest && mode_for_extraction (EP_insv, -1) != MAX_MACHINE_MODE)
6180 wanted_inner_reg_mode = mode_for_extraction (EP_insv, 0);
6181 pos_mode = mode_for_extraction (EP_insv, 2);
6182 extraction_mode = mode_for_extraction (EP_insv, 3);
6185 if (! in_dest && unsignedp
6186 && mode_for_extraction (EP_extzv, -1) != MAX_MACHINE_MODE)
6188 wanted_inner_reg_mode = mode_for_extraction (EP_extzv, 1);
6189 pos_mode = mode_for_extraction (EP_extzv, 3);
6190 extraction_mode = mode_for_extraction (EP_extzv, 0);
6193 if (! in_dest && ! unsignedp
6194 && mode_for_extraction (EP_extv, -1) != MAX_MACHINE_MODE)
6196 wanted_inner_reg_mode = mode_for_extraction (EP_extv, 1);
6197 pos_mode = mode_for_extraction (EP_extv, 3);
6198 extraction_mode = mode_for_extraction (EP_extv, 0);
6201 /* Never narrow an object, since that might not be safe. */
6203 if (mode != VOIDmode
6204 && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
6205 extraction_mode = mode;
6207 if (pos_rtx && GET_MODE (pos_rtx) != VOIDmode
6208 && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
6209 pos_mode = GET_MODE (pos_rtx);
6211 /* If this is not from memory, the desired mode is wanted_inner_reg_mode;
6212 if we have to change the mode of memory and cannot, the desired mode is
6213 EXTRACTION_MODE. */
6214 if (GET_CODE (inner) != MEM)
6215 wanted_inner_mode = wanted_inner_reg_mode;
6216 else if (inner_mode != wanted_inner_mode
6217 && (mode_dependent_address_p (XEXP (inner, 0))
6218 || MEM_VOLATILE_P (inner)))
6219 wanted_inner_mode = extraction_mode;
6221 orig_pos = pos;
6223 if (BITS_BIG_ENDIAN)
6225 /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
6226 BITS_BIG_ENDIAN style. If position is constant, compute new
6227 position. Otherwise, build subtraction.
6228 Note that POS is relative to the mode of the original argument.
6229 If it's a MEM we need to recompute POS relative to that.
6230 However, if we're extracting from (or inserting into) a register,
6231 we want to recompute POS relative to wanted_inner_mode. */
6232 int width = (GET_CODE (inner) == MEM
6233 ? GET_MODE_BITSIZE (is_mode)
6234 : GET_MODE_BITSIZE (wanted_inner_mode));
6236 if (pos_rtx == 0)
6237 pos = width - len - pos;
6238 else
6239 pos_rtx
6240 = gen_rtx_MINUS (GET_MODE (pos_rtx), GEN_INT (width - len), pos_rtx);
6241 /* POS may be less than 0 now, but we check for that below.
6242 Note that it can only be less than 0 if GET_CODE (inner) != MEM. */
6245 /* If INNER has a wider mode, make it smaller. If this is a constant
6246 extract, try to adjust the byte to point to the byte containing
6247 the value. */
6248 if (wanted_inner_mode != VOIDmode
6249 && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
6250 && ((GET_CODE (inner) == MEM
6251 && (inner_mode == wanted_inner_mode
6252 || (! mode_dependent_address_p (XEXP (inner, 0))
6253 && ! MEM_VOLATILE_P (inner))))))
6255 int offset = 0;
6257 /* The computations below will be correct if the machine is big
6258 endian in both bits and bytes or little endian in bits and bytes.
6259 If it is mixed, we must adjust. */
6261 /* If bytes are big endian and we had a paradoxical SUBREG, we must
6262 adjust OFFSET to compensate. */
6263 if (BYTES_BIG_ENDIAN
6264 && ! spans_byte
6265 && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
6266 offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
6268 /* If this is a constant position, we can move to the desired byte. */
6269 if (pos_rtx == 0)
6271 offset += pos / BITS_PER_UNIT;
6272 pos %= GET_MODE_BITSIZE (wanted_inner_mode);
6275 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
6276 && ! spans_byte
6277 && is_mode != wanted_inner_mode)
6278 offset = (GET_MODE_SIZE (is_mode)
6279 - GET_MODE_SIZE (wanted_inner_mode) - offset);
6281 if (offset != 0 || inner_mode != wanted_inner_mode)
6282 inner = adjust_address_nv (inner, wanted_inner_mode, offset);
6285 /* If INNER is not memory, we can always get it into the proper mode. If we
6286 are changing its mode, POS must be a constant and smaller than the size
6287 of the new mode. */
6288 else if (GET_CODE (inner) != MEM)
6290 if (GET_MODE (inner) != wanted_inner_mode
6291 && (pos_rtx != 0
6292 || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
6293 return 0;
6295 inner = force_to_mode (inner, wanted_inner_mode,
6296 pos_rtx
6297 || len + orig_pos >= HOST_BITS_PER_WIDE_INT
6298 ? ~(unsigned HOST_WIDE_INT) 0
6299 : ((((unsigned HOST_WIDE_INT) 1 << len) - 1)
6300 << orig_pos),
6301 NULL_RTX, 0);
6304 /* Adjust mode of POS_RTX, if needed. If we want a wider mode, we
6305 have to zero extend. Otherwise, we can just use a SUBREG. */
6306 if (pos_rtx != 0
6307 && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
6309 rtx temp = gen_rtx_ZERO_EXTEND (pos_mode, pos_rtx);
6311 /* If we know that no extraneous bits are set, and that the high
6312 bit is not set, convert extraction to cheaper one - either
6313 SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
6314 cases. */
6315 if (flag_expensive_optimizations
6316 && (GET_MODE_BITSIZE (GET_MODE (pos_rtx)) <= HOST_BITS_PER_WIDE_INT
6317 && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
6318 & ~(((unsigned HOST_WIDE_INT)
6319 GET_MODE_MASK (GET_MODE (pos_rtx)))
6320 >> 1))
6321 == 0)))
6323 rtx temp1 = gen_rtx_SIGN_EXTEND (pos_mode, pos_rtx);
6325 /* Prefer ZERO_EXTENSION, since it gives more information to
6326 backends. */
6327 if (rtx_cost (temp1, SET) < rtx_cost (temp, SET))
6328 temp = temp1;
6330 pos_rtx = temp;
6332 else if (pos_rtx != 0
6333 && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
6334 pos_rtx = gen_lowpart (pos_mode, pos_rtx);
6336 /* Make POS_RTX unless we already have it and it is correct. If we don't
6337 have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
6338 be a CONST_INT. */
6339 if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
6340 pos_rtx = orig_pos_rtx;
6342 else if (pos_rtx == 0)
6343 pos_rtx = GEN_INT (pos);
6345 /* Make the required operation. See if we can use existing rtx. */
6346 new = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
6347 extraction_mode, inner, GEN_INT (len), pos_rtx);
6348 if (! in_dest)
6349 new = gen_lowpart (mode, new);
6351 return new;
6354 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
6355 with any other operations in X. Return X without that shift if so. */
6357 static rtx
6358 extract_left_shift (rtx x, int count)
6360 enum rtx_code code = GET_CODE (x);
6361 enum machine_mode mode = GET_MODE (x);
6362 rtx tem;
6364 switch (code)
6366 case ASHIFT:
6367 /* This is the shift itself. If it is wide enough, we will return
6368 either the value being shifted if the shift count is equal to
6369 COUNT or a shift for the difference. */
6370 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6371 && INTVAL (XEXP (x, 1)) >= count)
6372 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
6373 INTVAL (XEXP (x, 1)) - count);
6374 break;
6376 case NEG: case NOT:
6377 if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
6378 return simplify_gen_unary (code, mode, tem, mode);
6380 break;
6382 case PLUS: case IOR: case XOR: case AND:
6383 /* If we can safely shift this constant and we find the inner shift,
6384 make a new operation. */
6385 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6386 && (INTVAL (XEXP (x, 1)) & ((((HOST_WIDE_INT) 1 << count)) - 1)) == 0
6387 && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
6388 return gen_binary (code, mode, tem,
6389 GEN_INT (INTVAL (XEXP (x, 1)) >> count));
6391 break;
6393 default:
6394 break;
6397 return 0;
6400 /* Look at the expression rooted at X. Look for expressions
6401 equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
6402 Form these expressions.
6404 Return the new rtx, usually just X.
6406 Also, for machines like the VAX that don't have logical shift insns,
6407 try to convert logical to arithmetic shift operations in cases where
6408 they are equivalent. This undoes the canonicalizations to logical
6409 shifts done elsewhere.
6411 We try, as much as possible, to re-use rtl expressions to save memory.
6413 IN_CODE says what kind of expression we are processing. Normally, it is
6414 SET. In a memory address (inside a MEM, PLUS or minus, the latter two
6415 being kludges), it is MEM. When processing the arguments of a comparison
6416 or a COMPARE against zero, it is COMPARE. */
6418 static rtx
6419 make_compound_operation (rtx x, enum rtx_code in_code)
6421 enum rtx_code code = GET_CODE (x);
6422 enum machine_mode mode = GET_MODE (x);
6423 int mode_width = GET_MODE_BITSIZE (mode);
6424 rtx rhs, lhs;
6425 enum rtx_code next_code;
6426 int i;
6427 rtx new = 0;
6428 rtx tem;
6429 const char *fmt;
6431 /* Select the code to be used in recursive calls. Once we are inside an
6432 address, we stay there. If we have a comparison, set to COMPARE,
6433 but once inside, go back to our default of SET. */
6435 next_code = (code == MEM || code == PLUS || code == MINUS ? MEM
6436 : ((code == COMPARE || COMPARISON_P (x))
6437 && XEXP (x, 1) == const0_rtx) ? COMPARE
6438 : in_code == COMPARE ? SET : in_code);
6440 /* Process depending on the code of this operation. If NEW is set
6441 nonzero, it will be returned. */
6443 switch (code)
6445 case ASHIFT:
6446 /* Convert shifts by constants into multiplications if inside
6447 an address. */
6448 if (in_code == MEM && GET_CODE (XEXP (x, 1)) == CONST_INT
6449 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
6450 && INTVAL (XEXP (x, 1)) >= 0)
6452 new = make_compound_operation (XEXP (x, 0), next_code);
6453 new = gen_rtx_MULT (mode, new,
6454 GEN_INT ((HOST_WIDE_INT) 1
6455 << INTVAL (XEXP (x, 1))));
6457 break;
6459 case AND:
6460 /* If the second operand is not a constant, we can't do anything
6461 with it. */
6462 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
6463 break;
6465 /* If the constant is a power of two minus one and the first operand
6466 is a logical right shift, make an extraction. */
6467 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6468 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6470 new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
6471 new = make_extraction (mode, new, 0, XEXP (XEXP (x, 0), 1), i, 1,
6472 0, in_code == COMPARE);
6475 /* Same as previous, but for (subreg (lshiftrt ...)) in first op. */
6476 else if (GET_CODE (XEXP (x, 0)) == SUBREG
6477 && subreg_lowpart_p (XEXP (x, 0))
6478 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
6479 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6481 new = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
6482 next_code);
6483 new = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new, 0,
6484 XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
6485 0, in_code == COMPARE);
6487 /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)). */
6488 else if ((GET_CODE (XEXP (x, 0)) == XOR
6489 || GET_CODE (XEXP (x, 0)) == IOR)
6490 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
6491 && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
6492 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6494 /* Apply the distributive law, and then try to make extractions. */
6495 new = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
6496 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
6497 XEXP (x, 1)),
6498 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
6499 XEXP (x, 1)));
6500 new = make_compound_operation (new, in_code);
6503 /* If we are have (and (rotate X C) M) and C is larger than the number
6504 of bits in M, this is an extraction. */
6506 else if (GET_CODE (XEXP (x, 0)) == ROTATE
6507 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6508 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0
6509 && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
6511 new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
6512 new = make_extraction (mode, new,
6513 (GET_MODE_BITSIZE (mode)
6514 - INTVAL (XEXP (XEXP (x, 0), 1))),
6515 NULL_RTX, i, 1, 0, in_code == COMPARE);
6518 /* On machines without logical shifts, if the operand of the AND is
6519 a logical shift and our mask turns off all the propagated sign
6520 bits, we can replace the logical shift with an arithmetic shift. */
6521 else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6522 && !have_insn_for (LSHIFTRT, mode)
6523 && have_insn_for (ASHIFTRT, mode)
6524 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6525 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
6526 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
6527 && mode_width <= HOST_BITS_PER_WIDE_INT)
6529 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
6531 mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
6532 if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
6533 SUBST (XEXP (x, 0),
6534 gen_rtx_ASHIFTRT (mode,
6535 make_compound_operation
6536 (XEXP (XEXP (x, 0), 0), next_code),
6537 XEXP (XEXP (x, 0), 1)));
6540 /* If the constant is one less than a power of two, this might be
6541 representable by an extraction even if no shift is present.
6542 If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
6543 we are in a COMPARE. */
6544 else if ((i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6545 new = make_extraction (mode,
6546 make_compound_operation (XEXP (x, 0),
6547 next_code),
6548 0, NULL_RTX, i, 1, 0, in_code == COMPARE);
6550 /* If we are in a comparison and this is an AND with a power of two,
6551 convert this into the appropriate bit extract. */
6552 else if (in_code == COMPARE
6553 && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
6554 new = make_extraction (mode,
6555 make_compound_operation (XEXP (x, 0),
6556 next_code),
6557 i, NULL_RTX, 1, 1, 0, 1);
6559 break;
6561 case LSHIFTRT:
6562 /* If the sign bit is known to be zero, replace this with an
6563 arithmetic shift. */
6564 if (have_insn_for (ASHIFTRT, mode)
6565 && ! have_insn_for (LSHIFTRT, mode)
6566 && mode_width <= HOST_BITS_PER_WIDE_INT
6567 && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
6569 new = gen_rtx_ASHIFTRT (mode,
6570 make_compound_operation (XEXP (x, 0),
6571 next_code),
6572 XEXP (x, 1));
6573 break;
6576 /* ... fall through ... */
6578 case ASHIFTRT:
6579 lhs = XEXP (x, 0);
6580 rhs = XEXP (x, 1);
6582 /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
6583 this is a SIGN_EXTRACT. */
6584 if (GET_CODE (rhs) == CONST_INT
6585 && GET_CODE (lhs) == ASHIFT
6586 && GET_CODE (XEXP (lhs, 1)) == CONST_INT
6587 && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1)))
6589 new = make_compound_operation (XEXP (lhs, 0), next_code);
6590 new = make_extraction (mode, new,
6591 INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
6592 NULL_RTX, mode_width - INTVAL (rhs),
6593 code == LSHIFTRT, 0, in_code == COMPARE);
6594 break;
6597 /* See if we have operations between an ASHIFTRT and an ASHIFT.
6598 If so, try to merge the shifts into a SIGN_EXTEND. We could
6599 also do this for some cases of SIGN_EXTRACT, but it doesn't
6600 seem worth the effort; the case checked for occurs on Alpha. */
6602 if (!OBJECT_P (lhs)
6603 && ! (GET_CODE (lhs) == SUBREG
6604 && (OBJECT_P (SUBREG_REG (lhs))))
6605 && GET_CODE (rhs) == CONST_INT
6606 && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
6607 && (new = extract_left_shift (lhs, INTVAL (rhs))) != 0)
6608 new = make_extraction (mode, make_compound_operation (new, next_code),
6609 0, NULL_RTX, mode_width - INTVAL (rhs),
6610 code == LSHIFTRT, 0, in_code == COMPARE);
6612 break;
6614 case SUBREG:
6615 /* Call ourselves recursively on the inner expression. If we are
6616 narrowing the object and it has a different RTL code from
6617 what it originally did, do this SUBREG as a force_to_mode. */
6619 tem = make_compound_operation (SUBREG_REG (x), in_code);
6620 if (GET_CODE (tem) != GET_CODE (SUBREG_REG (x))
6621 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (tem))
6622 && subreg_lowpart_p (x))
6624 rtx newer = force_to_mode (tem, mode, ~(HOST_WIDE_INT) 0,
6625 NULL_RTX, 0);
6627 /* If we have something other than a SUBREG, we might have
6628 done an expansion, so rerun ourselves. */
6629 if (GET_CODE (newer) != SUBREG)
6630 newer = make_compound_operation (newer, in_code);
6632 return newer;
6635 /* If this is a paradoxical subreg, and the new code is a sign or
6636 zero extension, omit the subreg and widen the extension. If it
6637 is a regular subreg, we can still get rid of the subreg by not
6638 widening so much, or in fact removing the extension entirely. */
6639 if ((GET_CODE (tem) == SIGN_EXTEND
6640 || GET_CODE (tem) == ZERO_EXTEND)
6641 && subreg_lowpart_p (x))
6643 if (GET_MODE_SIZE (mode) > GET_MODE_SIZE (GET_MODE (tem))
6644 || (GET_MODE_SIZE (mode) >
6645 GET_MODE_SIZE (GET_MODE (XEXP (tem, 0)))))
6647 if (! SCALAR_INT_MODE_P (mode))
6648 break;
6649 tem = gen_rtx_fmt_e (GET_CODE (tem), mode, XEXP (tem, 0));
6651 else
6652 tem = gen_lowpart (mode, XEXP (tem, 0));
6653 return tem;
6655 break;
6657 default:
6658 break;
6661 if (new)
6663 x = gen_lowpart (mode, new);
6664 code = GET_CODE (x);
6667 /* Now recursively process each operand of this operation. */
6668 fmt = GET_RTX_FORMAT (code);
6669 for (i = 0; i < GET_RTX_LENGTH (code); i++)
6670 if (fmt[i] == 'e')
6672 new = make_compound_operation (XEXP (x, i), next_code);
6673 SUBST (XEXP (x, i), new);
6676 return x;
6679 /* Given M see if it is a value that would select a field of bits
6680 within an item, but not the entire word. Return -1 if not.
6681 Otherwise, return the starting position of the field, where 0 is the
6682 low-order bit.
6684 *PLEN is set to the length of the field. */
6686 static int
6687 get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
6689 /* Get the bit number of the first 1 bit from the right, -1 if none. */
6690 int pos = exact_log2 (m & -m);
6691 int len;
6693 if (pos < 0)
6694 return -1;
6696 /* Now shift off the low-order zero bits and see if we have a power of
6697 two minus 1. */
6698 len = exact_log2 ((m >> pos) + 1);
6700 if (len <= 0)
6701 return -1;
6703 *plen = len;
6704 return pos;
6707 /* See if X can be simplified knowing that we will only refer to it in
6708 MODE and will only refer to those bits that are nonzero in MASK.
6709 If other bits are being computed or if masking operations are done
6710 that select a superset of the bits in MASK, they can sometimes be
6711 ignored.
6713 Return a possibly simplified expression, but always convert X to
6714 MODE. If X is a CONST_INT, AND the CONST_INT with MASK.
6716 Also, if REG is nonzero and X is a register equal in value to REG,
6717 replace X with REG.
6719 If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
6720 are all off in X. This is used when X will be complemented, by either
6721 NOT, NEG, or XOR. */
6723 static rtx
6724 force_to_mode (rtx x, enum machine_mode mode, unsigned HOST_WIDE_INT mask,
6725 rtx reg, int just_select)
6727 enum rtx_code code = GET_CODE (x);
6728 int next_select = just_select || code == XOR || code == NOT || code == NEG;
6729 enum machine_mode op_mode;
6730 unsigned HOST_WIDE_INT fuller_mask, nonzero;
6731 rtx op0, op1, temp;
6733 /* If this is a CALL or ASM_OPERANDS, don't do anything. Some of the
6734 code below will do the wrong thing since the mode of such an
6735 expression is VOIDmode.
6737 Also do nothing if X is a CLOBBER; this can happen if X was
6738 the return value from a call to gen_lowpart. */
6739 if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
6740 return x;
6742 /* We want to perform the operation is its present mode unless we know
6743 that the operation is valid in MODE, in which case we do the operation
6744 in MODE. */
6745 op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
6746 && have_insn_for (code, mode))
6747 ? mode : GET_MODE (x));
6749 /* It is not valid to do a right-shift in a narrower mode
6750 than the one it came in with. */
6751 if ((code == LSHIFTRT || code == ASHIFTRT)
6752 && GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (GET_MODE (x)))
6753 op_mode = GET_MODE (x);
6755 /* Truncate MASK to fit OP_MODE. */
6756 if (op_mode)
6757 mask &= GET_MODE_MASK (op_mode);
6759 /* When we have an arithmetic operation, or a shift whose count we
6760 do not know, we need to assume that all bits up to the highest-order
6761 bit in MASK will be needed. This is how we form such a mask. */
6762 if (mask & ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
6763 fuller_mask = ~(unsigned HOST_WIDE_INT) 0;
6764 else
6765 fuller_mask = (((unsigned HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1))
6766 - 1);
6768 /* Determine what bits of X are guaranteed to be (non)zero. */
6769 nonzero = nonzero_bits (x, mode);
6771 /* If none of the bits in X are needed, return a zero. */
6772 if (! just_select && (nonzero & mask) == 0)
6773 x = const0_rtx;
6775 /* If X is a CONST_INT, return a new one. Do this here since the
6776 test below will fail. */
6777 if (GET_CODE (x) == CONST_INT)
6779 if (SCALAR_INT_MODE_P (mode))
6780 return gen_int_mode (INTVAL (x) & mask, mode);
6781 else
6783 x = GEN_INT (INTVAL (x) & mask);
6784 return gen_lowpart_common (mode, x);
6788 /* If X is narrower than MODE and we want all the bits in X's mode, just
6789 get X in the proper mode. */
6790 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
6791 && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
6792 return gen_lowpart (mode, x);
6794 /* If we aren't changing the mode, X is not a SUBREG, and all zero bits in
6795 MASK are already known to be zero in X, we need not do anything. */
6796 if (GET_MODE (x) == mode && code != SUBREG && (~mask & nonzero) == 0)
6797 return x;
6799 switch (code)
6801 case CLOBBER:
6802 /* If X is a (clobber (const_int)), return it since we know we are
6803 generating something that won't match. */
6804 return x;
6806 case USE:
6807 /* X is a (use (mem ..)) that was made from a bit-field extraction that
6808 spanned the boundary of the MEM. If we are now masking so it is
6809 within that boundary, we don't need the USE any more. */
6810 if (! BITS_BIG_ENDIAN
6811 && (mask & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6812 return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
6813 break;
6815 case SIGN_EXTEND:
6816 case ZERO_EXTEND:
6817 case ZERO_EXTRACT:
6818 case SIGN_EXTRACT:
6819 x = expand_compound_operation (x);
6820 if (GET_CODE (x) != code)
6821 return force_to_mode (x, mode, mask, reg, next_select);
6822 break;
6824 case REG:
6825 if (reg != 0 && (rtx_equal_p (get_last_value (reg), x)
6826 || rtx_equal_p (reg, get_last_value (x))))
6827 x = reg;
6828 break;
6830 case SUBREG:
6831 if (subreg_lowpart_p (x)
6832 /* We can ignore the effect of this SUBREG if it narrows the mode or
6833 if the constant masks to zero all the bits the mode doesn't
6834 have. */
6835 && ((GET_MODE_SIZE (GET_MODE (x))
6836 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
6837 || (0 == (mask
6838 & GET_MODE_MASK (GET_MODE (x))
6839 & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
6840 return force_to_mode (SUBREG_REG (x), mode, mask, reg, next_select);
6841 break;
6843 case AND:
6844 /* If this is an AND with a constant, convert it into an AND
6845 whose constant is the AND of that constant with MASK. If it
6846 remains an AND of MASK, delete it since it is redundant. */
6848 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
6850 x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
6851 mask & INTVAL (XEXP (x, 1)));
6853 /* If X is still an AND, see if it is an AND with a mask that
6854 is just some low-order bits. If so, and it is MASK, we don't
6855 need it. */
6857 if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
6858 && ((INTVAL (XEXP (x, 1)) & GET_MODE_MASK (GET_MODE (x)))
6859 == mask))
6860 x = XEXP (x, 0);
6862 /* If it remains an AND, try making another AND with the bits
6863 in the mode mask that aren't in MASK turned on. If the
6864 constant in the AND is wide enough, this might make a
6865 cheaper constant. */
6867 if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
6868 && GET_MODE_MASK (GET_MODE (x)) != mask
6869 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
6871 HOST_WIDE_INT cval = (INTVAL (XEXP (x, 1))
6872 | (GET_MODE_MASK (GET_MODE (x)) & ~mask));
6873 int width = GET_MODE_BITSIZE (GET_MODE (x));
6874 rtx y;
6876 /* If MODE is narrower than HOST_WIDE_INT and CVAL is a negative
6877 number, sign extend it. */
6878 if (width > 0 && width < HOST_BITS_PER_WIDE_INT
6879 && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
6880 cval |= (HOST_WIDE_INT) -1 << width;
6882 y = gen_binary (AND, GET_MODE (x), XEXP (x, 0), GEN_INT (cval));
6883 if (rtx_cost (y, SET) < rtx_cost (x, SET))
6884 x = y;
6887 break;
6890 goto binop;
6892 case PLUS:
6893 /* In (and (plus FOO C1) M), if M is a mask that just turns off
6894 low-order bits (as in an alignment operation) and FOO is already
6895 aligned to that boundary, mask C1 to that boundary as well.
6896 This may eliminate that PLUS and, later, the AND. */
6899 unsigned int width = GET_MODE_BITSIZE (mode);
6900 unsigned HOST_WIDE_INT smask = mask;
6902 /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
6903 number, sign extend it. */
6905 if (width < HOST_BITS_PER_WIDE_INT
6906 && (smask & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
6907 smask |= (HOST_WIDE_INT) -1 << width;
6909 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6910 && exact_log2 (- smask) >= 0
6911 && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
6912 && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
6913 return force_to_mode (plus_constant (XEXP (x, 0),
6914 (INTVAL (XEXP (x, 1)) & smask)),
6915 mode, smask, reg, next_select);
6918 /* ... fall through ... */
6920 case MULT:
6921 /* For PLUS, MINUS and MULT, we need any bits less significant than the
6922 most significant bit in MASK since carries from those bits will
6923 affect the bits we are interested in. */
6924 mask = fuller_mask;
6925 goto binop;
6927 case MINUS:
6928 /* If X is (minus C Y) where C's least set bit is larger than any bit
6929 in the mask, then we may replace with (neg Y). */
6930 if (GET_CODE (XEXP (x, 0)) == CONST_INT
6931 && (((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 0))
6932 & -INTVAL (XEXP (x, 0))))
6933 > mask))
6935 x = simplify_gen_unary (NEG, GET_MODE (x), XEXP (x, 1),
6936 GET_MODE (x));
6937 return force_to_mode (x, mode, mask, reg, next_select);
6940 /* Similarly, if C contains every bit in the fuller_mask, then we may
6941 replace with (not Y). */
6942 if (GET_CODE (XEXP (x, 0)) == CONST_INT
6943 && ((INTVAL (XEXP (x, 0)) | (HOST_WIDE_INT) fuller_mask)
6944 == INTVAL (XEXP (x, 0))))
6946 x = simplify_gen_unary (NOT, GET_MODE (x),
6947 XEXP (x, 1), GET_MODE (x));
6948 return force_to_mode (x, mode, mask, reg, next_select);
6951 mask = fuller_mask;
6952 goto binop;
6954 case IOR:
6955 case XOR:
6956 /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
6957 LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
6958 operation which may be a bitfield extraction. Ensure that the
6959 constant we form is not wider than the mode of X. */
6961 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6962 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6963 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
6964 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
6965 && GET_CODE (XEXP (x, 1)) == CONST_INT
6966 && ((INTVAL (XEXP (XEXP (x, 0), 1))
6967 + floor_log2 (INTVAL (XEXP (x, 1))))
6968 < GET_MODE_BITSIZE (GET_MODE (x)))
6969 && (INTVAL (XEXP (x, 1))
6970 & ~nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
6972 temp = GEN_INT ((INTVAL (XEXP (x, 1)) & mask)
6973 << INTVAL (XEXP (XEXP (x, 0), 1)));
6974 temp = gen_binary (GET_CODE (x), GET_MODE (x),
6975 XEXP (XEXP (x, 0), 0), temp);
6976 x = gen_binary (LSHIFTRT, GET_MODE (x), temp,
6977 XEXP (XEXP (x, 0), 1));
6978 return force_to_mode (x, mode, mask, reg, next_select);
6981 binop:
6982 /* For most binary operations, just propagate into the operation and
6983 change the mode if we have an operation of that mode. */
6985 op0 = gen_lowpart (op_mode,
6986 force_to_mode (XEXP (x, 0), mode, mask,
6987 reg, next_select));
6988 op1 = gen_lowpart (op_mode,
6989 force_to_mode (XEXP (x, 1), mode, mask,
6990 reg, next_select));
6992 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
6993 x = gen_binary (code, op_mode, op0, op1);
6994 break;
6996 case ASHIFT:
6997 /* For left shifts, do the same, but just for the first operand.
6998 However, we cannot do anything with shifts where we cannot
6999 guarantee that the counts are smaller than the size of the mode
7000 because such a count will have a different meaning in a
7001 wider mode. */
7003 if (! (GET_CODE (XEXP (x, 1)) == CONST_INT
7004 && INTVAL (XEXP (x, 1)) >= 0
7005 && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (mode))
7006 && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
7007 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
7008 < (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (mode))))
7009 break;
7011 /* If the shift count is a constant and we can do arithmetic in
7012 the mode of the shift, refine which bits we need. Otherwise, use the
7013 conservative form of the mask. */
7014 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7015 && INTVAL (XEXP (x, 1)) >= 0
7016 && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (op_mode)
7017 && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
7018 mask >>= INTVAL (XEXP (x, 1));
7019 else
7020 mask = fuller_mask;
7022 op0 = gen_lowpart (op_mode,
7023 force_to_mode (XEXP (x, 0), op_mode,
7024 mask, reg, next_select));
7026 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
7027 x = gen_binary (code, op_mode, op0, XEXP (x, 1));
7028 break;
7030 case LSHIFTRT:
7031 /* Here we can only do something if the shift count is a constant,
7032 this shift constant is valid for the host, and we can do arithmetic
7033 in OP_MODE. */
7035 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7036 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
7037 && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
7039 rtx inner = XEXP (x, 0);
7040 unsigned HOST_WIDE_INT inner_mask;
7042 /* Select the mask of the bits we need for the shift operand. */
7043 inner_mask = mask << INTVAL (XEXP (x, 1));
7045 /* We can only change the mode of the shift if we can do arithmetic
7046 in the mode of the shift and INNER_MASK is no wider than the
7047 width of OP_MODE. */
7048 if (GET_MODE_BITSIZE (op_mode) > HOST_BITS_PER_WIDE_INT
7049 || (inner_mask & ~GET_MODE_MASK (op_mode)) != 0)
7050 op_mode = GET_MODE (x);
7052 inner = force_to_mode (inner, op_mode, inner_mask, reg, next_select);
7054 if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
7055 x = gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
7058 /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
7059 shift and AND produces only copies of the sign bit (C2 is one less
7060 than a power of two), we can do this with just a shift. */
7062 if (GET_CODE (x) == LSHIFTRT
7063 && GET_CODE (XEXP (x, 1)) == CONST_INT
7064 /* The shift puts one of the sign bit copies in the least significant
7065 bit. */
7066 && ((INTVAL (XEXP (x, 1))
7067 + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
7068 >= GET_MODE_BITSIZE (GET_MODE (x)))
7069 && exact_log2 (mask + 1) >= 0
7070 /* Number of bits left after the shift must be more than the mask
7071 needs. */
7072 && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
7073 <= GET_MODE_BITSIZE (GET_MODE (x)))
7074 /* Must be more sign bit copies than the mask needs. */
7075 && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
7076 >= exact_log2 (mask + 1)))
7077 x = gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
7078 GEN_INT (GET_MODE_BITSIZE (GET_MODE (x))
7079 - exact_log2 (mask + 1)));
7081 goto shiftrt;
7083 case ASHIFTRT:
7084 /* If we are just looking for the sign bit, we don't need this shift at
7085 all, even if it has a variable count. */
7086 if (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
7087 && (mask == ((unsigned HOST_WIDE_INT) 1
7088 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
7089 return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
7091 /* If this is a shift by a constant, get a mask that contains those bits
7092 that are not copies of the sign bit. We then have two cases: If
7093 MASK only includes those bits, this can be a logical shift, which may
7094 allow simplifications. If MASK is a single-bit field not within
7095 those bits, we are requesting a copy of the sign bit and hence can
7096 shift the sign bit to the appropriate location. */
7098 if (GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) >= 0
7099 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
7101 int i = -1;
7103 /* If the considered data is wider than HOST_WIDE_INT, we can't
7104 represent a mask for all its bits in a single scalar.
7105 But we only care about the lower bits, so calculate these. */
7107 if (GET_MODE_BITSIZE (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
7109 nonzero = ~(HOST_WIDE_INT) 0;
7111 /* GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
7112 is the number of bits a full-width mask would have set.
7113 We need only shift if these are fewer than nonzero can
7114 hold. If not, we must keep all bits set in nonzero. */
7116 if (GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
7117 < HOST_BITS_PER_WIDE_INT)
7118 nonzero >>= INTVAL (XEXP (x, 1))
7119 + HOST_BITS_PER_WIDE_INT
7120 - GET_MODE_BITSIZE (GET_MODE (x)) ;
7122 else
7124 nonzero = GET_MODE_MASK (GET_MODE (x));
7125 nonzero >>= INTVAL (XEXP (x, 1));
7128 if ((mask & ~nonzero) == 0
7129 || (i = exact_log2 (mask)) >= 0)
7131 x = simplify_shift_const
7132 (x, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
7133 i < 0 ? INTVAL (XEXP (x, 1))
7134 : GET_MODE_BITSIZE (GET_MODE (x)) - 1 - i);
7136 if (GET_CODE (x) != ASHIFTRT)
7137 return force_to_mode (x, mode, mask, reg, next_select);
7141 /* If MASK is 1, convert this to an LSHIFTRT. This can be done
7142 even if the shift count isn't a constant. */
7143 if (mask == 1)
7144 x = gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0), XEXP (x, 1));
7146 shiftrt:
7148 /* If this is a zero- or sign-extension operation that just affects bits
7149 we don't care about, remove it. Be sure the call above returned
7150 something that is still a shift. */
7152 if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
7153 && GET_CODE (XEXP (x, 1)) == CONST_INT
7154 && INTVAL (XEXP (x, 1)) >= 0
7155 && (INTVAL (XEXP (x, 1))
7156 <= GET_MODE_BITSIZE (GET_MODE (x)) - (floor_log2 (mask) + 1))
7157 && GET_CODE (XEXP (x, 0)) == ASHIFT
7158 && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
7159 return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
7160 reg, next_select);
7162 break;
7164 case ROTATE:
7165 case ROTATERT:
7166 /* If the shift count is constant and we can do computations
7167 in the mode of X, compute where the bits we care about are.
7168 Otherwise, we can't do anything. Don't change the mode of
7169 the shift or propagate MODE into the shift, though. */
7170 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7171 && INTVAL (XEXP (x, 1)) >= 0)
7173 temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
7174 GET_MODE (x), GEN_INT (mask),
7175 XEXP (x, 1));
7176 if (temp && GET_CODE (temp) == CONST_INT)
7177 SUBST (XEXP (x, 0),
7178 force_to_mode (XEXP (x, 0), GET_MODE (x),
7179 INTVAL (temp), reg, next_select));
7181 break;
7183 case NEG:
7184 /* If we just want the low-order bit, the NEG isn't needed since it
7185 won't change the low-order bit. */
7186 if (mask == 1)
7187 return force_to_mode (XEXP (x, 0), mode, mask, reg, just_select);
7189 /* We need any bits less significant than the most significant bit in
7190 MASK since carries from those bits will affect the bits we are
7191 interested in. */
7192 mask = fuller_mask;
7193 goto unop;
7195 case NOT:
7196 /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
7197 same as the XOR case above. Ensure that the constant we form is not
7198 wider than the mode of X. */
7200 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7201 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
7202 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7203 && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
7204 < GET_MODE_BITSIZE (GET_MODE (x)))
7205 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
7207 temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)),
7208 GET_MODE (x));
7209 temp = gen_binary (XOR, GET_MODE (x), XEXP (XEXP (x, 0), 0), temp);
7210 x = gen_binary (LSHIFTRT, GET_MODE (x), temp, XEXP (XEXP (x, 0), 1));
7212 return force_to_mode (x, mode, mask, reg, next_select);
7215 /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
7216 use the full mask inside the NOT. */
7217 mask = fuller_mask;
7219 unop:
7220 op0 = gen_lowpart (op_mode,
7221 force_to_mode (XEXP (x, 0), mode, mask,
7222 reg, next_select));
7223 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
7224 x = simplify_gen_unary (code, op_mode, op0, op_mode);
7225 break;
7227 case NE:
7228 /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
7229 in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
7230 which is equal to STORE_FLAG_VALUE. */
7231 if ((mask & ~STORE_FLAG_VALUE) == 0 && XEXP (x, 1) == const0_rtx
7232 && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
7233 && (nonzero_bits (XEXP (x, 0), mode)
7234 == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
7235 return force_to_mode (XEXP (x, 0), mode, mask, reg, next_select);
7237 break;
7239 case IF_THEN_ELSE:
7240 /* We have no way of knowing if the IF_THEN_ELSE can itself be
7241 written in a narrower mode. We play it safe and do not do so. */
7243 SUBST (XEXP (x, 1),
7244 gen_lowpart (GET_MODE (x),
7245 force_to_mode (XEXP (x, 1), mode,
7246 mask, reg, next_select)));
7247 SUBST (XEXP (x, 2),
7248 gen_lowpart (GET_MODE (x),
7249 force_to_mode (XEXP (x, 2), mode,
7250 mask, reg, next_select)));
7251 break;
7253 default:
7254 break;
7257 /* Ensure we return a value of the proper mode. */
7258 return gen_lowpart (mode, x);
7261 /* Return nonzero if X is an expression that has one of two values depending on
7262 whether some other value is zero or nonzero. In that case, we return the
7263 value that is being tested, *PTRUE is set to the value if the rtx being
7264 returned has a nonzero value, and *PFALSE is set to the other alternative.
7266 If we return zero, we set *PTRUE and *PFALSE to X. */
7268 static rtx
7269 if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
7271 enum machine_mode mode = GET_MODE (x);
7272 enum rtx_code code = GET_CODE (x);
7273 rtx cond0, cond1, true0, true1, false0, false1;
7274 unsigned HOST_WIDE_INT nz;
7276 /* If we are comparing a value against zero, we are done. */
7277 if ((code == NE || code == EQ)
7278 && XEXP (x, 1) == const0_rtx)
7280 *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
7281 *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
7282 return XEXP (x, 0);
7285 /* If this is a unary operation whose operand has one of two values, apply
7286 our opcode to compute those values. */
7287 else if (UNARY_P (x)
7288 && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
7290 *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
7291 *pfalse = simplify_gen_unary (code, mode, false0,
7292 GET_MODE (XEXP (x, 0)));
7293 return cond0;
7296 /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
7297 make can't possibly match and would suppress other optimizations. */
7298 else if (code == COMPARE)
7301 /* If this is a binary operation, see if either side has only one of two
7302 values. If either one does or if both do and they are conditional on
7303 the same value, compute the new true and false values. */
7304 else if (BINARY_P (x))
7306 cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
7307 cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
7309 if ((cond0 != 0 || cond1 != 0)
7310 && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
7312 /* If if_then_else_cond returned zero, then true/false are the
7313 same rtl. We must copy one of them to prevent invalid rtl
7314 sharing. */
7315 if (cond0 == 0)
7316 true0 = copy_rtx (true0);
7317 else if (cond1 == 0)
7318 true1 = copy_rtx (true1);
7320 *ptrue = gen_binary (code, mode, true0, true1);
7321 *pfalse = gen_binary (code, mode, false0, false1);
7322 return cond0 ? cond0 : cond1;
7325 /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
7326 operands is zero when the other is nonzero, and vice-versa,
7327 and STORE_FLAG_VALUE is 1 or -1. */
7329 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
7330 && (code == PLUS || code == IOR || code == XOR || code == MINUS
7331 || code == UMAX)
7332 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
7334 rtx op0 = XEXP (XEXP (x, 0), 1);
7335 rtx op1 = XEXP (XEXP (x, 1), 1);
7337 cond0 = XEXP (XEXP (x, 0), 0);
7338 cond1 = XEXP (XEXP (x, 1), 0);
7340 if (COMPARISON_P (cond0)
7341 && COMPARISON_P (cond1)
7342 && ((GET_CODE (cond0) == combine_reversed_comparison_code (cond1)
7343 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
7344 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
7345 || ((swap_condition (GET_CODE (cond0))
7346 == combine_reversed_comparison_code (cond1))
7347 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
7348 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
7349 && ! side_effects_p (x))
7351 *ptrue = gen_binary (MULT, mode, op0, const_true_rtx);
7352 *pfalse = gen_binary (MULT, mode,
7353 (code == MINUS
7354 ? simplify_gen_unary (NEG, mode, op1,
7355 mode)
7356 : op1),
7357 const_true_rtx);
7358 return cond0;
7362 /* Similarly for MULT, AND and UMIN, except that for these the result
7363 is always zero. */
7364 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
7365 && (code == MULT || code == AND || code == UMIN)
7366 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
7368 cond0 = XEXP (XEXP (x, 0), 0);
7369 cond1 = XEXP (XEXP (x, 1), 0);
7371 if (COMPARISON_P (cond0)
7372 && COMPARISON_P (cond1)
7373 && ((GET_CODE (cond0) == combine_reversed_comparison_code (cond1)
7374 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
7375 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
7376 || ((swap_condition (GET_CODE (cond0))
7377 == combine_reversed_comparison_code (cond1))
7378 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
7379 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
7380 && ! side_effects_p (x))
7382 *ptrue = *pfalse = const0_rtx;
7383 return cond0;
7388 else if (code == IF_THEN_ELSE)
7390 /* If we have IF_THEN_ELSE already, extract the condition and
7391 canonicalize it if it is NE or EQ. */
7392 cond0 = XEXP (x, 0);
7393 *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
7394 if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
7395 return XEXP (cond0, 0);
7396 else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
7398 *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
7399 return XEXP (cond0, 0);
7401 else
7402 return cond0;
7405 /* If X is a SUBREG, we can narrow both the true and false values
7406 if the inner expression, if there is a condition. */
7407 else if (code == SUBREG
7408 && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
7409 &true0, &false0)))
7411 true0 = simplify_gen_subreg (mode, true0,
7412 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
7413 false0 = simplify_gen_subreg (mode, false0,
7414 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
7415 if (true0 && false0)
7417 *ptrue = true0;
7418 *pfalse = false0;
7419 return cond0;
7423 /* If X is a constant, this isn't special and will cause confusions
7424 if we treat it as such. Likewise if it is equivalent to a constant. */
7425 else if (CONSTANT_P (x)
7426 || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
7429 /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
7430 will be least confusing to the rest of the compiler. */
7431 else if (mode == BImode)
7433 *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
7434 return x;
7437 /* If X is known to be either 0 or -1, those are the true and
7438 false values when testing X. */
7439 else if (x == constm1_rtx || x == const0_rtx
7440 || (mode != VOIDmode
7441 && num_sign_bit_copies (x, mode) == GET_MODE_BITSIZE (mode)))
7443 *ptrue = constm1_rtx, *pfalse = const0_rtx;
7444 return x;
7447 /* Likewise for 0 or a single bit. */
7448 else if (SCALAR_INT_MODE_P (mode)
7449 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
7450 && exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
7452 *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
7453 return x;
7456 /* Otherwise fail; show no condition with true and false values the same. */
7457 *ptrue = *pfalse = x;
7458 return 0;
7461 /* Return the value of expression X given the fact that condition COND
7462 is known to be true when applied to REG as its first operand and VAL
7463 as its second. X is known to not be shared and so can be modified in
7464 place.
7466 We only handle the simplest cases, and specifically those cases that
7467 arise with IF_THEN_ELSE expressions. */
7469 static rtx
7470 known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
7472 enum rtx_code code = GET_CODE (x);
7473 rtx temp;
7474 const char *fmt;
7475 int i, j;
7477 if (side_effects_p (x))
7478 return x;
7480 /* If either operand of the condition is a floating point value,
7481 then we have to avoid collapsing an EQ comparison. */
7482 if (cond == EQ
7483 && rtx_equal_p (x, reg)
7484 && ! FLOAT_MODE_P (GET_MODE (x))
7485 && ! FLOAT_MODE_P (GET_MODE (val)))
7486 return val;
7488 if (cond == UNEQ && rtx_equal_p (x, reg))
7489 return val;
7491 /* If X is (abs REG) and we know something about REG's relationship
7492 with zero, we may be able to simplify this. */
7494 if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
7495 switch (cond)
7497 case GE: case GT: case EQ:
7498 return XEXP (x, 0);
7499 case LT: case LE:
7500 return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
7501 XEXP (x, 0),
7502 GET_MODE (XEXP (x, 0)));
7503 default:
7504 break;
7507 /* The only other cases we handle are MIN, MAX, and comparisons if the
7508 operands are the same as REG and VAL. */
7510 else if (COMPARISON_P (x) || COMMUTATIVE_ARITH_P (x))
7512 if (rtx_equal_p (XEXP (x, 0), val))
7513 cond = swap_condition (cond), temp = val, val = reg, reg = temp;
7515 if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
7517 if (COMPARISON_P (x))
7519 if (comparison_dominates_p (cond, code))
7520 return const_true_rtx;
7522 code = combine_reversed_comparison_code (x);
7523 if (code != UNKNOWN
7524 && comparison_dominates_p (cond, code))
7525 return const0_rtx;
7526 else
7527 return x;
7529 else if (code == SMAX || code == SMIN
7530 || code == UMIN || code == UMAX)
7532 int unsignedp = (code == UMIN || code == UMAX);
7534 /* Do not reverse the condition when it is NE or EQ.
7535 This is because we cannot conclude anything about
7536 the value of 'SMAX (x, y)' when x is not equal to y,
7537 but we can when x equals y. */
7538 if ((code == SMAX || code == UMAX)
7539 && ! (cond == EQ || cond == NE))
7540 cond = reverse_condition (cond);
7542 switch (cond)
7544 case GE: case GT:
7545 return unsignedp ? x : XEXP (x, 1);
7546 case LE: case LT:
7547 return unsignedp ? x : XEXP (x, 0);
7548 case GEU: case GTU:
7549 return unsignedp ? XEXP (x, 1) : x;
7550 case LEU: case LTU:
7551 return unsignedp ? XEXP (x, 0) : x;
7552 default:
7553 break;
7558 else if (code == SUBREG)
7560 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
7561 rtx new, r = known_cond (SUBREG_REG (x), cond, reg, val);
7563 if (SUBREG_REG (x) != r)
7565 /* We must simplify subreg here, before we lose track of the
7566 original inner_mode. */
7567 new = simplify_subreg (GET_MODE (x), r,
7568 inner_mode, SUBREG_BYTE (x));
7569 if (new)
7570 return new;
7571 else
7572 SUBST (SUBREG_REG (x), r);
7575 return x;
7577 /* We don't have to handle SIGN_EXTEND here, because even in the
7578 case of replacing something with a modeless CONST_INT, a
7579 CONST_INT is already (supposed to be) a valid sign extension for
7580 its narrower mode, which implies it's already properly
7581 sign-extended for the wider mode. Now, for ZERO_EXTEND, the
7582 story is different. */
7583 else if (code == ZERO_EXTEND)
7585 enum machine_mode inner_mode = GET_MODE (XEXP (x, 0));
7586 rtx new, r = known_cond (XEXP (x, 0), cond, reg, val);
7588 if (XEXP (x, 0) != r)
7590 /* We must simplify the zero_extend here, before we lose
7591 track of the original inner_mode. */
7592 new = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
7593 r, inner_mode);
7594 if (new)
7595 return new;
7596 else
7597 SUBST (XEXP (x, 0), r);
7600 return x;
7603 fmt = GET_RTX_FORMAT (code);
7604 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7606 if (fmt[i] == 'e')
7607 SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
7608 else if (fmt[i] == 'E')
7609 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
7610 SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
7611 cond, reg, val));
7614 return x;
7617 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
7618 assignment as a field assignment. */
7620 static int
7621 rtx_equal_for_field_assignment_p (rtx x, rtx y)
7623 if (x == y || rtx_equal_p (x, y))
7624 return 1;
7626 if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
7627 return 0;
7629 /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
7630 Note that all SUBREGs of MEM are paradoxical; otherwise they
7631 would have been rewritten. */
7632 if (GET_CODE (x) == MEM && GET_CODE (y) == SUBREG
7633 && GET_CODE (SUBREG_REG (y)) == MEM
7634 && rtx_equal_p (SUBREG_REG (y),
7635 gen_lowpart (GET_MODE (SUBREG_REG (y)), x)))
7636 return 1;
7638 if (GET_CODE (y) == MEM && GET_CODE (x) == SUBREG
7639 && GET_CODE (SUBREG_REG (x)) == MEM
7640 && rtx_equal_p (SUBREG_REG (x),
7641 gen_lowpart (GET_MODE (SUBREG_REG (x)), y)))
7642 return 1;
7644 /* We used to see if get_last_value of X and Y were the same but that's
7645 not correct. In one direction, we'll cause the assignment to have
7646 the wrong destination and in the case, we'll import a register into this
7647 insn that might have already have been dead. So fail if none of the
7648 above cases are true. */
7649 return 0;
7652 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
7653 Return that assignment if so.
7655 We only handle the most common cases. */
7657 static rtx
7658 make_field_assignment (rtx x)
7660 rtx dest = SET_DEST (x);
7661 rtx src = SET_SRC (x);
7662 rtx assign;
7663 rtx rhs, lhs;
7664 HOST_WIDE_INT c1;
7665 HOST_WIDE_INT pos;
7666 unsigned HOST_WIDE_INT len;
7667 rtx other;
7668 enum machine_mode mode;
7670 /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
7671 a clear of a one-bit field. We will have changed it to
7672 (and (rotate (const_int -2) POS) DEST), so check for that. Also check
7673 for a SUBREG. */
7675 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
7676 && GET_CODE (XEXP (XEXP (src, 0), 0)) == CONST_INT
7677 && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
7678 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7680 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
7681 1, 1, 1, 0);
7682 if (assign != 0)
7683 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
7684 return x;
7687 else if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
7688 && subreg_lowpart_p (XEXP (src, 0))
7689 && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
7690 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
7691 && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
7692 && GET_CODE (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == CONST_INT
7693 && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
7694 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7696 assign = make_extraction (VOIDmode, dest, 0,
7697 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
7698 1, 1, 1, 0);
7699 if (assign != 0)
7700 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
7701 return x;
7704 /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
7705 one-bit field. */
7706 else if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
7707 && XEXP (XEXP (src, 0), 0) == const1_rtx
7708 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
7710 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
7711 1, 1, 1, 0);
7712 if (assign != 0)
7713 return gen_rtx_SET (VOIDmode, assign, const1_rtx);
7714 return x;
7717 /* The other case we handle is assignments into a constant-position
7718 field. They look like (ior/xor (and DEST C1) OTHER). If C1 represents
7719 a mask that has all one bits except for a group of zero bits and
7720 OTHER is known to have zeros where C1 has ones, this is such an
7721 assignment. Compute the position and length from C1. Shift OTHER
7722 to the appropriate position, force it to the required mode, and
7723 make the extraction. Check for the AND in both operands. */
7725 if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
7726 return x;
7728 rhs = expand_compound_operation (XEXP (src, 0));
7729 lhs = expand_compound_operation (XEXP (src, 1));
7731 if (GET_CODE (rhs) == AND
7732 && GET_CODE (XEXP (rhs, 1)) == CONST_INT
7733 && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
7734 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
7735 else if (GET_CODE (lhs) == AND
7736 && GET_CODE (XEXP (lhs, 1)) == CONST_INT
7737 && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
7738 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
7739 else
7740 return x;
7742 pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
7743 if (pos < 0 || pos + len > GET_MODE_BITSIZE (GET_MODE (dest))
7744 || GET_MODE_BITSIZE (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
7745 || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
7746 return x;
7748 assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
7749 if (assign == 0)
7750 return x;
7752 /* The mode to use for the source is the mode of the assignment, or of
7753 what is inside a possible STRICT_LOW_PART. */
7754 mode = (GET_CODE (assign) == STRICT_LOW_PART
7755 ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
7757 /* Shift OTHER right POS places and make it the source, restricting it
7758 to the proper length and mode. */
7760 src = force_to_mode (simplify_shift_const (NULL_RTX, LSHIFTRT,
7761 GET_MODE (src), other, pos),
7762 mode,
7763 GET_MODE_BITSIZE (mode) >= HOST_BITS_PER_WIDE_INT
7764 ? ~(unsigned HOST_WIDE_INT) 0
7765 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
7766 dest, 0);
7768 /* If SRC is masked by an AND that does not make a difference in
7769 the value being stored, strip it. */
7770 if (GET_CODE (assign) == ZERO_EXTRACT
7771 && GET_CODE (XEXP (assign, 1)) == CONST_INT
7772 && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
7773 && GET_CODE (src) == AND
7774 && GET_CODE (XEXP (src, 1)) == CONST_INT
7775 && ((unsigned HOST_WIDE_INT) INTVAL (XEXP (src, 1))
7776 == ((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (assign, 1))) - 1))
7777 src = XEXP (src, 0);
7779 return gen_rtx_SET (VOIDmode, assign, src);
7782 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
7783 if so. */
7785 static rtx
7786 apply_distributive_law (rtx x)
7788 enum rtx_code code = GET_CODE (x);
7789 enum rtx_code inner_code;
7790 rtx lhs, rhs, other;
7791 rtx tem;
7793 /* Distributivity is not true for floating point as it can change the
7794 value. So we don't do it unless -funsafe-math-optimizations. */
7795 if (FLOAT_MODE_P (GET_MODE (x))
7796 && ! flag_unsafe_math_optimizations)
7797 return x;
7799 /* The outer operation can only be one of the following: */
7800 if (code != IOR && code != AND && code != XOR
7801 && code != PLUS && code != MINUS)
7802 return x;
7804 lhs = XEXP (x, 0);
7805 rhs = XEXP (x, 1);
7807 /* If either operand is a primitive we can't do anything, so get out
7808 fast. */
7809 if (OBJECT_P (lhs) || OBJECT_P (rhs))
7810 return x;
7812 lhs = expand_compound_operation (lhs);
7813 rhs = expand_compound_operation (rhs);
7814 inner_code = GET_CODE (lhs);
7815 if (inner_code != GET_CODE (rhs))
7816 return x;
7818 /* See if the inner and outer operations distribute. */
7819 switch (inner_code)
7821 case LSHIFTRT:
7822 case ASHIFTRT:
7823 case AND:
7824 case IOR:
7825 /* These all distribute except over PLUS. */
7826 if (code == PLUS || code == MINUS)
7827 return x;
7828 break;
7830 case MULT:
7831 if (code != PLUS && code != MINUS)
7832 return x;
7833 break;
7835 case ASHIFT:
7836 /* This is also a multiply, so it distributes over everything. */
7837 break;
7839 case SUBREG:
7840 /* Non-paradoxical SUBREGs distributes over all operations, provided
7841 the inner modes and byte offsets are the same, this is an extraction
7842 of a low-order part, we don't convert an fp operation to int or
7843 vice versa, and we would not be converting a single-word
7844 operation into a multi-word operation. The latter test is not
7845 required, but it prevents generating unneeded multi-word operations.
7846 Some of the previous tests are redundant given the latter test, but
7847 are retained because they are required for correctness.
7849 We produce the result slightly differently in this case. */
7851 if (GET_MODE (SUBREG_REG (lhs)) != GET_MODE (SUBREG_REG (rhs))
7852 || SUBREG_BYTE (lhs) != SUBREG_BYTE (rhs)
7853 || ! subreg_lowpart_p (lhs)
7854 || (GET_MODE_CLASS (GET_MODE (lhs))
7855 != GET_MODE_CLASS (GET_MODE (SUBREG_REG (lhs))))
7856 || (GET_MODE_SIZE (GET_MODE (lhs))
7857 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))))
7858 || GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))) > UNITS_PER_WORD)
7859 return x;
7861 tem = gen_binary (code, GET_MODE (SUBREG_REG (lhs)),
7862 SUBREG_REG (lhs), SUBREG_REG (rhs));
7863 return gen_lowpart (GET_MODE (x), tem);
7865 default:
7866 return x;
7869 /* Set LHS and RHS to the inner operands (A and B in the example
7870 above) and set OTHER to the common operand (C in the example).
7871 There is only one way to do this unless the inner operation is
7872 commutative. */
7873 if (COMMUTATIVE_ARITH_P (lhs)
7874 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
7875 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
7876 else if (COMMUTATIVE_ARITH_P (lhs)
7877 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
7878 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
7879 else if (COMMUTATIVE_ARITH_P (lhs)
7880 && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
7881 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
7882 else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
7883 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
7884 else
7885 return x;
7887 /* Form the new inner operation, seeing if it simplifies first. */
7888 tem = gen_binary (code, GET_MODE (x), lhs, rhs);
7890 /* There is one exception to the general way of distributing:
7891 (a | c) ^ (b | c) -> (a ^ b) & ~c */
7892 if (code == XOR && inner_code == IOR)
7894 inner_code = AND;
7895 other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
7898 /* We may be able to continuing distributing the result, so call
7899 ourselves recursively on the inner operation before forming the
7900 outer operation, which we return. */
7901 return gen_binary (inner_code, GET_MODE (x),
7902 apply_distributive_law (tem), other);
7905 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
7906 in MODE.
7908 Return an equivalent form, if different from X. Otherwise, return X. If
7909 X is zero, we are to always construct the equivalent form. */
7911 static rtx
7912 simplify_and_const_int (rtx x, enum machine_mode mode, rtx varop,
7913 unsigned HOST_WIDE_INT constop)
7915 unsigned HOST_WIDE_INT nonzero;
7916 int i;
7918 /* Simplify VAROP knowing that we will be only looking at some of the
7919 bits in it.
7921 Note by passing in CONSTOP, we guarantee that the bits not set in
7922 CONSTOP are not significant and will never be examined. We must
7923 ensure that is the case by explicitly masking out those bits
7924 before returning. */
7925 varop = force_to_mode (varop, mode, constop, NULL_RTX, 0);
7927 /* If VAROP is a CLOBBER, we will fail so return it. */
7928 if (GET_CODE (varop) == CLOBBER)
7929 return varop;
7931 /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
7932 to VAROP and return the new constant. */
7933 if (GET_CODE (varop) == CONST_INT)
7934 return GEN_INT (trunc_int_for_mode (INTVAL (varop) & constop, mode));
7936 /* See what bits may be nonzero in VAROP. Unlike the general case of
7937 a call to nonzero_bits, here we don't care about bits outside
7938 MODE. */
7940 nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
7942 /* Turn off all bits in the constant that are known to already be zero.
7943 Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
7944 which is tested below. */
7946 constop &= nonzero;
7948 /* If we don't have any bits left, return zero. */
7949 if (constop == 0)
7950 return const0_rtx;
7952 /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
7953 a power of two, we can replace this with an ASHIFT. */
7954 if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
7955 && (i = exact_log2 (constop)) >= 0)
7956 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
7958 /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
7959 or XOR, then try to apply the distributive law. This may eliminate
7960 operations if either branch can be simplified because of the AND.
7961 It may also make some cases more complex, but those cases probably
7962 won't match a pattern either with or without this. */
7964 if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
7965 return
7966 gen_lowpart
7967 (mode,
7968 apply_distributive_law
7969 (gen_binary (GET_CODE (varop), GET_MODE (varop),
7970 simplify_and_const_int (NULL_RTX, GET_MODE (varop),
7971 XEXP (varop, 0), constop),
7972 simplify_and_const_int (NULL_RTX, GET_MODE (varop),
7973 XEXP (varop, 1), constop))));
7975 /* If VAROP is PLUS, and the constant is a mask of low bite, distribute
7976 the AND and see if one of the operands simplifies to zero. If so, we
7977 may eliminate it. */
7979 if (GET_CODE (varop) == PLUS
7980 && exact_log2 (constop + 1) >= 0)
7982 rtx o0, o1;
7984 o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
7985 o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
7986 if (o0 == const0_rtx)
7987 return o1;
7988 if (o1 == const0_rtx)
7989 return o0;
7992 /* Get VAROP in MODE. Try to get a SUBREG if not. Don't make a new SUBREG
7993 if we already had one (just check for the simplest cases). */
7994 if (x && GET_CODE (XEXP (x, 0)) == SUBREG
7995 && GET_MODE (XEXP (x, 0)) == mode
7996 && SUBREG_REG (XEXP (x, 0)) == varop)
7997 varop = XEXP (x, 0);
7998 else
7999 varop = gen_lowpart (mode, varop);
8001 /* If we can't make the SUBREG, try to return what we were given. */
8002 if (GET_CODE (varop) == CLOBBER)
8003 return x ? x : varop;
8005 /* If we are only masking insignificant bits, return VAROP. */
8006 if (constop == nonzero)
8007 x = varop;
8008 else
8010 /* Otherwise, return an AND. */
8011 constop = trunc_int_for_mode (constop, mode);
8012 /* See how much, if any, of X we can use. */
8013 if (x == 0 || GET_CODE (x) != AND || GET_MODE (x) != mode)
8014 x = gen_binary (AND, mode, varop, GEN_INT (constop));
8016 else
8018 if (GET_CODE (XEXP (x, 1)) != CONST_INT
8019 || (unsigned HOST_WIDE_INT) INTVAL (XEXP (x, 1)) != constop)
8020 SUBST (XEXP (x, 1), GEN_INT (constop));
8022 SUBST (XEXP (x, 0), varop);
8026 return x;
8029 #define nonzero_bits_with_known(X, MODE) \
8030 cached_nonzero_bits (X, MODE, known_x, known_mode, known_ret)
8032 /* The function cached_nonzero_bits is a wrapper around nonzero_bits1.
8033 It avoids exponential behavior in nonzero_bits1 when X has
8034 identical subexpressions on the first or the second level. */
8036 static unsigned HOST_WIDE_INT
8037 cached_nonzero_bits (rtx x, enum machine_mode mode, rtx known_x,
8038 enum machine_mode known_mode,
8039 unsigned HOST_WIDE_INT known_ret)
8041 if (x == known_x && mode == known_mode)
8042 return known_ret;
8044 /* Try to find identical subexpressions. If found call
8045 nonzero_bits1 on X with the subexpressions as KNOWN_X and the
8046 precomputed value for the subexpression as KNOWN_RET. */
8048 if (ARITHMETIC_P (x))
8050 rtx x0 = XEXP (x, 0);
8051 rtx x1 = XEXP (x, 1);
8053 /* Check the first level. */
8054 if (x0 == x1)
8055 return nonzero_bits1 (x, mode, x0, mode,
8056 nonzero_bits_with_known (x0, mode));
8058 /* Check the second level. */
8059 if (ARITHMETIC_P (x0)
8060 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
8061 return nonzero_bits1 (x, mode, x1, mode,
8062 nonzero_bits_with_known (x1, mode));
8064 if (ARITHMETIC_P (x1)
8065 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
8066 return nonzero_bits1 (x, mode, x0, mode,
8067 nonzero_bits_with_known (x0, mode));
8070 return nonzero_bits1 (x, mode, known_x, known_mode, known_ret);
8073 /* We let num_sign_bit_copies recur into nonzero_bits as that is useful.
8074 We don't let nonzero_bits recur into num_sign_bit_copies, because that
8075 is less useful. We can't allow both, because that results in exponential
8076 run time recursion. There is a nullstone testcase that triggered
8077 this. This macro avoids accidental uses of num_sign_bit_copies. */
8078 #define cached_num_sign_bit_copies()
8080 /* Given an expression, X, compute which bits in X can be nonzero.
8081 We don't care about bits outside of those defined in MODE.
8083 For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
8084 a shift, AND, or zero_extract, we can do better. */
8086 static unsigned HOST_WIDE_INT
8087 nonzero_bits1 (rtx x, enum machine_mode mode, rtx known_x,
8088 enum machine_mode known_mode,
8089 unsigned HOST_WIDE_INT known_ret)
8091 unsigned HOST_WIDE_INT nonzero = GET_MODE_MASK (mode);
8092 unsigned HOST_WIDE_INT inner_nz;
8093 enum rtx_code code;
8094 unsigned int mode_width = GET_MODE_BITSIZE (mode);
8095 rtx tem;
8097 /* For floating-point values, assume all bits are needed. */
8098 if (FLOAT_MODE_P (GET_MODE (x)) || FLOAT_MODE_P (mode))
8099 return nonzero;
8101 /* If X is wider than MODE, use its mode instead. */
8102 if (GET_MODE_BITSIZE (GET_MODE (x)) > mode_width)
8104 mode = GET_MODE (x);
8105 nonzero = GET_MODE_MASK (mode);
8106 mode_width = GET_MODE_BITSIZE (mode);
8109 if (mode_width > HOST_BITS_PER_WIDE_INT)
8110 /* Our only callers in this case look for single bit values. So
8111 just return the mode mask. Those tests will then be false. */
8112 return nonzero;
8114 #ifndef WORD_REGISTER_OPERATIONS
8115 /* If MODE is wider than X, but both are a single word for both the host
8116 and target machines, we can compute this from which bits of the
8117 object might be nonzero in its own mode, taking into account the fact
8118 that on many CISC machines, accessing an object in a wider mode
8119 causes the high-order bits to become undefined. So they are
8120 not known to be zero. */
8122 if (GET_MODE (x) != VOIDmode && GET_MODE (x) != mode
8123 && GET_MODE_BITSIZE (GET_MODE (x)) <= BITS_PER_WORD
8124 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
8125 && GET_MODE_BITSIZE (mode) > GET_MODE_BITSIZE (GET_MODE (x)))
8127 nonzero &= nonzero_bits_with_known (x, GET_MODE (x));
8128 nonzero |= GET_MODE_MASK (mode) & ~GET_MODE_MASK (GET_MODE (x));
8129 return nonzero;
8131 #endif
8133 code = GET_CODE (x);
8134 switch (code)
8136 case REG:
8137 #if defined(POINTERS_EXTEND_UNSIGNED) && !defined(HAVE_ptr_extend)
8138 /* If pointers extend unsigned and this is a pointer in Pmode, say that
8139 all the bits above ptr_mode are known to be zero. */
8140 if (POINTERS_EXTEND_UNSIGNED && GET_MODE (x) == Pmode
8141 && REG_POINTER (x))
8142 nonzero &= GET_MODE_MASK (ptr_mode);
8143 #endif
8145 /* Include declared information about alignment of pointers. */
8146 /* ??? We don't properly preserve REG_POINTER changes across
8147 pointer-to-integer casts, so we can't trust it except for
8148 things that we know must be pointers. See execute/960116-1.c. */
8149 if ((x == stack_pointer_rtx
8150 || x == frame_pointer_rtx
8151 || x == arg_pointer_rtx)
8152 && REGNO_POINTER_ALIGN (REGNO (x)))
8154 unsigned HOST_WIDE_INT alignment
8155 = REGNO_POINTER_ALIGN (REGNO (x)) / BITS_PER_UNIT;
8157 #ifdef PUSH_ROUNDING
8158 /* If PUSH_ROUNDING is defined, it is possible for the
8159 stack to be momentarily aligned only to that amount,
8160 so we pick the least alignment. */
8161 if (x == stack_pointer_rtx && PUSH_ARGS)
8162 alignment = MIN ((unsigned HOST_WIDE_INT) PUSH_ROUNDING (1),
8163 alignment);
8164 #endif
8166 nonzero &= ~(alignment - 1);
8169 /* If X is a register whose nonzero bits value is current, use it.
8170 Otherwise, if X is a register whose value we can find, use that
8171 value. Otherwise, use the previously-computed global nonzero bits
8172 for this register. */
8174 if (reg_last_set_value[REGNO (x)] != 0
8175 && (reg_last_set_mode[REGNO (x)] == mode
8176 || (GET_MODE_CLASS (reg_last_set_mode[REGNO (x)]) == MODE_INT
8177 && GET_MODE_CLASS (mode) == MODE_INT))
8178 && (reg_last_set_label[REGNO (x)] == label_tick
8179 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
8180 && REG_N_SETS (REGNO (x)) == 1
8181 && ! REGNO_REG_SET_P (ENTRY_BLOCK_PTR->next_bb->global_live_at_start,
8182 REGNO (x))))
8183 && INSN_CUID (reg_last_set[REGNO (x)]) < subst_low_cuid)
8184 return reg_last_set_nonzero_bits[REGNO (x)] & nonzero;
8186 tem = get_last_value (x);
8188 if (tem)
8190 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
8191 /* If X is narrower than MODE and TEM is a non-negative
8192 constant that would appear negative in the mode of X,
8193 sign-extend it for use in reg_nonzero_bits because some
8194 machines (maybe most) will actually do the sign-extension
8195 and this is the conservative approach.
8197 ??? For 2.5, try to tighten up the MD files in this regard
8198 instead of this kludge. */
8200 if (GET_MODE_BITSIZE (GET_MODE (x)) < mode_width
8201 && GET_CODE (tem) == CONST_INT
8202 && INTVAL (tem) > 0
8203 && 0 != (INTVAL (tem)
8204 & ((HOST_WIDE_INT) 1
8205 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
8206 tem = GEN_INT (INTVAL (tem)
8207 | ((HOST_WIDE_INT) (-1)
8208 << GET_MODE_BITSIZE (GET_MODE (x))));
8209 #endif
8210 return nonzero_bits_with_known (tem, mode) & nonzero;
8212 else if (nonzero_sign_valid && reg_nonzero_bits[REGNO (x)])
8214 unsigned HOST_WIDE_INT mask = reg_nonzero_bits[REGNO (x)];
8216 if (GET_MODE_BITSIZE (GET_MODE (x)) < mode_width)
8217 /* We don't know anything about the upper bits. */
8218 mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (GET_MODE (x));
8219 return nonzero & mask;
8221 else
8222 return nonzero;
8224 case CONST_INT:
8225 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
8226 /* If X is negative in MODE, sign-extend the value. */
8227 if (INTVAL (x) > 0 && mode_width < BITS_PER_WORD
8228 && 0 != (INTVAL (x) & ((HOST_WIDE_INT) 1 << (mode_width - 1))))
8229 return (INTVAL (x) | ((HOST_WIDE_INT) (-1) << mode_width));
8230 #endif
8232 return INTVAL (x);
8234 case MEM:
8235 #ifdef LOAD_EXTEND_OP
8236 /* In many, if not most, RISC machines, reading a byte from memory
8237 zeros the rest of the register. Noticing that fact saves a lot
8238 of extra zero-extends. */
8239 if (LOAD_EXTEND_OP (GET_MODE (x)) == ZERO_EXTEND)
8240 nonzero &= GET_MODE_MASK (GET_MODE (x));
8241 #endif
8242 break;
8244 case EQ: case NE:
8245 case UNEQ: case LTGT:
8246 case GT: case GTU: case UNGT:
8247 case LT: case LTU: case UNLT:
8248 case GE: case GEU: case UNGE:
8249 case LE: case LEU: case UNLE:
8250 case UNORDERED: case ORDERED:
8252 /* If this produces an integer result, we know which bits are set.
8253 Code here used to clear bits outside the mode of X, but that is
8254 now done above. */
8256 if (GET_MODE_CLASS (mode) == MODE_INT
8257 && mode_width <= HOST_BITS_PER_WIDE_INT)
8258 nonzero = STORE_FLAG_VALUE;
8259 break;
8261 case NEG:
8262 #if 0
8263 /* Disabled to avoid exponential mutual recursion between nonzero_bits
8264 and num_sign_bit_copies. */
8265 if (num_sign_bit_copies (XEXP (x, 0), GET_MODE (x))
8266 == GET_MODE_BITSIZE (GET_MODE (x)))
8267 nonzero = 1;
8268 #endif
8270 if (GET_MODE_SIZE (GET_MODE (x)) < mode_width)
8271 nonzero |= (GET_MODE_MASK (mode) & ~GET_MODE_MASK (GET_MODE (x)));
8272 break;
8274 case ABS:
8275 #if 0
8276 /* Disabled to avoid exponential mutual recursion between nonzero_bits
8277 and num_sign_bit_copies. */
8278 if (num_sign_bit_copies (XEXP (x, 0), GET_MODE (x))
8279 == GET_MODE_BITSIZE (GET_MODE (x)))
8280 nonzero = 1;
8281 #endif
8282 break;
8284 case TRUNCATE:
8285 nonzero &= (nonzero_bits_with_known (XEXP (x, 0), mode)
8286 & GET_MODE_MASK (mode));
8287 break;
8289 case ZERO_EXTEND:
8290 nonzero &= nonzero_bits_with_known (XEXP (x, 0), mode);
8291 if (GET_MODE (XEXP (x, 0)) != VOIDmode)
8292 nonzero &= GET_MODE_MASK (GET_MODE (XEXP (x, 0)));
8293 break;
8295 case SIGN_EXTEND:
8296 /* If the sign bit is known clear, this is the same as ZERO_EXTEND.
8297 Otherwise, show all the bits in the outer mode but not the inner
8298 may be nonzero. */
8299 inner_nz = nonzero_bits_with_known (XEXP (x, 0), mode);
8300 if (GET_MODE (XEXP (x, 0)) != VOIDmode)
8302 inner_nz &= GET_MODE_MASK (GET_MODE (XEXP (x, 0)));
8303 if (inner_nz
8304 & (((HOST_WIDE_INT) 1
8305 << (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - 1))))
8306 inner_nz |= (GET_MODE_MASK (mode)
8307 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0))));
8310 nonzero &= inner_nz;
8311 break;
8313 case AND:
8314 nonzero &= (nonzero_bits_with_known (XEXP (x, 0), mode)
8315 & nonzero_bits_with_known (XEXP (x, 1), mode));
8316 break;
8318 case XOR: case IOR:
8319 case UMIN: case UMAX: case SMIN: case SMAX:
8321 unsigned HOST_WIDE_INT nonzero0 =
8322 nonzero_bits_with_known (XEXP (x, 0), mode);
8324 /* Don't call nonzero_bits for the second time if it cannot change
8325 anything. */
8326 if ((nonzero & nonzero0) != nonzero)
8327 nonzero &= (nonzero0
8328 | nonzero_bits_with_known (XEXP (x, 1), mode));
8330 break;
8332 case PLUS: case MINUS:
8333 case MULT:
8334 case DIV: case UDIV:
8335 case MOD: case UMOD:
8336 /* We can apply the rules of arithmetic to compute the number of
8337 high- and low-order zero bits of these operations. We start by
8338 computing the width (position of the highest-order nonzero bit)
8339 and the number of low-order zero bits for each value. */
8341 unsigned HOST_WIDE_INT nz0 =
8342 nonzero_bits_with_known (XEXP (x, 0), mode);
8343 unsigned HOST_WIDE_INT nz1 =
8344 nonzero_bits_with_known (XEXP (x, 1), mode);
8345 int sign_index = GET_MODE_BITSIZE (GET_MODE (x)) - 1;
8346 int width0 = floor_log2 (nz0) + 1;
8347 int width1 = floor_log2 (nz1) + 1;
8348 int low0 = floor_log2 (nz0 & -nz0);
8349 int low1 = floor_log2 (nz1 & -nz1);
8350 HOST_WIDE_INT op0_maybe_minusp
8351 = (nz0 & ((HOST_WIDE_INT) 1 << sign_index));
8352 HOST_WIDE_INT op1_maybe_minusp
8353 = (nz1 & ((HOST_WIDE_INT) 1 << sign_index));
8354 unsigned int result_width = mode_width;
8355 int result_low = 0;
8357 switch (code)
8359 case PLUS:
8360 result_width = MAX (width0, width1) + 1;
8361 result_low = MIN (low0, low1);
8362 break;
8363 case MINUS:
8364 result_low = MIN (low0, low1);
8365 break;
8366 case MULT:
8367 result_width = width0 + width1;
8368 result_low = low0 + low1;
8369 break;
8370 case DIV:
8371 if (width1 == 0)
8372 break;
8373 if (! op0_maybe_minusp && ! op1_maybe_minusp)
8374 result_width = width0;
8375 break;
8376 case UDIV:
8377 if (width1 == 0)
8378 break;
8379 result_width = width0;
8380 break;
8381 case MOD:
8382 if (width1 == 0)
8383 break;
8384 if (! op0_maybe_minusp && ! op1_maybe_minusp)
8385 result_width = MIN (width0, width1);
8386 result_low = MIN (low0, low1);
8387 break;
8388 case UMOD:
8389 if (width1 == 0)
8390 break;
8391 result_width = MIN (width0, width1);
8392 result_low = MIN (low0, low1);
8393 break;
8394 default:
8395 abort ();
8398 if (result_width < mode_width)
8399 nonzero &= ((HOST_WIDE_INT) 1 << result_width) - 1;
8401 if (result_low > 0)
8402 nonzero &= ~(((HOST_WIDE_INT) 1 << result_low) - 1);
8404 #ifdef POINTERS_EXTEND_UNSIGNED
8405 /* If pointers extend unsigned and this is an addition or subtraction
8406 to a pointer in Pmode, all the bits above ptr_mode are known to be
8407 zero. */
8408 if (POINTERS_EXTEND_UNSIGNED > 0 && GET_MODE (x) == Pmode
8409 && (code == PLUS || code == MINUS)
8410 && GET_CODE (XEXP (x, 0)) == REG && REG_POINTER (XEXP (x, 0)))
8411 nonzero &= GET_MODE_MASK (ptr_mode);
8412 #endif
8414 break;
8416 case ZERO_EXTRACT:
8417 if (GET_CODE (XEXP (x, 1)) == CONST_INT
8418 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
8419 nonzero &= ((HOST_WIDE_INT) 1 << INTVAL (XEXP (x, 1))) - 1;
8420 break;
8422 case SUBREG:
8423 /* If this is a SUBREG formed for a promoted variable that has
8424 been zero-extended, we know that at least the high-order bits
8425 are zero, though others might be too. */
8427 if (SUBREG_PROMOTED_VAR_P (x) && SUBREG_PROMOTED_UNSIGNED_P (x) > 0)
8428 nonzero = (GET_MODE_MASK (GET_MODE (x))
8429 & nonzero_bits_with_known (SUBREG_REG (x), GET_MODE (x)));
8431 /* If the inner mode is a single word for both the host and target
8432 machines, we can compute this from which bits of the inner
8433 object might be nonzero. */
8434 if (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))) <= BITS_PER_WORD
8435 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x)))
8436 <= HOST_BITS_PER_WIDE_INT))
8438 nonzero &= nonzero_bits_with_known (SUBREG_REG (x), mode);
8440 #if defined (WORD_REGISTER_OPERATIONS) && defined (LOAD_EXTEND_OP)
8441 /* If this is a typical RISC machine, we only have to worry
8442 about the way loads are extended. */
8443 if ((LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (x))) == SIGN_EXTEND
8444 ? (((nonzero
8445 & (((unsigned HOST_WIDE_INT) 1
8446 << (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))) - 1))))
8447 != 0))
8448 : LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (x))) != ZERO_EXTEND)
8449 || GET_CODE (SUBREG_REG (x)) != MEM)
8450 #endif
8452 /* On many CISC machines, accessing an object in a wider mode
8453 causes the high-order bits to become undefined. So they are
8454 not known to be zero. */
8455 if (GET_MODE_SIZE (GET_MODE (x))
8456 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
8457 nonzero |= (GET_MODE_MASK (GET_MODE (x))
8458 & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x))));
8461 break;
8463 case ASHIFTRT:
8464 case LSHIFTRT:
8465 case ASHIFT:
8466 case ROTATE:
8467 /* The nonzero bits are in two classes: any bits within MODE
8468 that aren't in GET_MODE (x) are always significant. The rest of the
8469 nonzero bits are those that are significant in the operand of
8470 the shift when shifted the appropriate number of bits. This
8471 shows that high-order bits are cleared by the right shift and
8472 low-order bits by left shifts. */
8473 if (GET_CODE (XEXP (x, 1)) == CONST_INT
8474 && INTVAL (XEXP (x, 1)) >= 0
8475 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
8477 enum machine_mode inner_mode = GET_MODE (x);
8478 unsigned int width = GET_MODE_BITSIZE (inner_mode);
8479 int count = INTVAL (XEXP (x, 1));
8480 unsigned HOST_WIDE_INT mode_mask = GET_MODE_MASK (inner_mode);
8481 unsigned HOST_WIDE_INT op_nonzero =
8482 nonzero_bits_with_known (XEXP (x, 0), mode);
8483 unsigned HOST_WIDE_INT inner = op_nonzero & mode_mask;
8484 unsigned HOST_WIDE_INT outer = 0;
8486 if (mode_width > width)
8487 outer = (op_nonzero & nonzero & ~mode_mask);
8489 if (code == LSHIFTRT)
8490 inner >>= count;
8491 else if (code == ASHIFTRT)
8493 inner >>= count;
8495 /* If the sign bit may have been nonzero before the shift, we
8496 need to mark all the places it could have been copied to
8497 by the shift as possibly nonzero. */
8498 if (inner & ((HOST_WIDE_INT) 1 << (width - 1 - count)))
8499 inner |= (((HOST_WIDE_INT) 1 << count) - 1) << (width - count);
8501 else if (code == ASHIFT)
8502 inner <<= count;
8503 else
8504 inner = ((inner << (count % width)
8505 | (inner >> (width - (count % width)))) & mode_mask);
8507 nonzero &= (outer | inner);
8509 break;
8511 case FFS:
8512 case POPCOUNT:
8513 /* This is at most the number of bits in the mode. */
8514 nonzero = ((HOST_WIDE_INT) 2 << (floor_log2 (mode_width))) - 1;
8515 break;
8517 case CLZ:
8518 /* If CLZ has a known value at zero, then the nonzero bits are
8519 that value, plus the number of bits in the mode minus one. */
8520 if (CLZ_DEFINED_VALUE_AT_ZERO (mode, nonzero))
8521 nonzero |= ((HOST_WIDE_INT) 1 << (floor_log2 (mode_width))) - 1;
8522 else
8523 nonzero = -1;
8524 break;
8526 case CTZ:
8527 /* If CTZ has a known value at zero, then the nonzero bits are
8528 that value, plus the number of bits in the mode minus one. */
8529 if (CTZ_DEFINED_VALUE_AT_ZERO (mode, nonzero))
8530 nonzero |= ((HOST_WIDE_INT) 1 << (floor_log2 (mode_width))) - 1;
8531 else
8532 nonzero = -1;
8533 break;
8535 case PARITY:
8536 nonzero = 1;
8537 break;
8539 case IF_THEN_ELSE:
8540 nonzero &= (nonzero_bits_with_known (XEXP (x, 1), mode)
8541 | nonzero_bits_with_known (XEXP (x, 2), mode));
8542 break;
8544 default:
8545 break;
8548 return nonzero;
8551 /* See the macro definition above. */
8552 #undef cached_num_sign_bit_copies
8554 #define num_sign_bit_copies_with_known(X, M) \
8555 cached_num_sign_bit_copies (X, M, known_x, known_mode, known_ret)
8557 /* The function cached_num_sign_bit_copies is a wrapper around
8558 num_sign_bit_copies1. It avoids exponential behavior in
8559 num_sign_bit_copies1 when X has identical subexpressions on the
8560 first or the second level. */
8562 static unsigned int
8563 cached_num_sign_bit_copies (rtx x, enum machine_mode mode, rtx known_x,
8564 enum machine_mode known_mode,
8565 unsigned int known_ret)
8567 if (x == known_x && mode == known_mode)
8568 return known_ret;
8570 /* Try to find identical subexpressions. If found call
8571 num_sign_bit_copies1 on X with the subexpressions as KNOWN_X and
8572 the precomputed value for the subexpression as KNOWN_RET. */
8574 if (ARITHMETIC_P (x))
8576 rtx x0 = XEXP (x, 0);
8577 rtx x1 = XEXP (x, 1);
8579 /* Check the first level. */
8580 if (x0 == x1)
8581 return
8582 num_sign_bit_copies1 (x, mode, x0, mode,
8583 num_sign_bit_copies_with_known (x0, mode));
8585 /* Check the second level. */
8586 if (ARITHMETIC_P (x0)
8587 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
8588 return
8589 num_sign_bit_copies1 (x, mode, x1, mode,
8590 num_sign_bit_copies_with_known (x1, mode));
8592 if (ARITHMETIC_P (x1)
8593 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
8594 return
8595 num_sign_bit_copies1 (x, mode, x0, mode,
8596 num_sign_bit_copies_with_known (x0, mode));
8599 return num_sign_bit_copies1 (x, mode, known_x, known_mode, known_ret);
8602 /* Return the number of bits at the high-order end of X that are known to
8603 be equal to the sign bit. X will be used in mode MODE; if MODE is
8604 VOIDmode, X will be used in its own mode. The returned value will always
8605 be between 1 and the number of bits in MODE. */
8607 static unsigned int
8608 num_sign_bit_copies1 (rtx x, enum machine_mode mode, rtx known_x,
8609 enum machine_mode known_mode,
8610 unsigned int known_ret)
8612 enum rtx_code code = GET_CODE (x);
8613 unsigned int bitwidth;
8614 int num0, num1, result;
8615 unsigned HOST_WIDE_INT nonzero;
8616 rtx tem;
8618 /* If we weren't given a mode, use the mode of X. If the mode is still
8619 VOIDmode, we don't know anything. Likewise if one of the modes is
8620 floating-point. */
8622 if (mode == VOIDmode)
8623 mode = GET_MODE (x);
8625 if (mode == VOIDmode || FLOAT_MODE_P (mode) || FLOAT_MODE_P (GET_MODE (x)))
8626 return 1;
8628 bitwidth = GET_MODE_BITSIZE (mode);
8630 /* For a smaller object, just ignore the high bits. */
8631 if (bitwidth < GET_MODE_BITSIZE (GET_MODE (x)))
8633 num0 = num_sign_bit_copies_with_known (x, GET_MODE (x));
8634 return MAX (1,
8635 num0 - (int) (GET_MODE_BITSIZE (GET_MODE (x)) - bitwidth));
8638 if (GET_MODE (x) != VOIDmode && bitwidth > GET_MODE_BITSIZE (GET_MODE (x)))
8640 #ifndef WORD_REGISTER_OPERATIONS
8641 /* If this machine does not do all register operations on the entire
8642 register and MODE is wider than the mode of X, we can say nothing
8643 at all about the high-order bits. */
8644 return 1;
8645 #else
8646 /* Likewise on machines that do, if the mode of the object is smaller
8647 than a word and loads of that size don't sign extend, we can say
8648 nothing about the high order bits. */
8649 if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
8650 #ifdef LOAD_EXTEND_OP
8651 && LOAD_EXTEND_OP (GET_MODE (x)) != SIGN_EXTEND
8652 #endif
8654 return 1;
8655 #endif
8658 switch (code)
8660 case REG:
8662 #if defined(POINTERS_EXTEND_UNSIGNED) && !defined(HAVE_ptr_extend)
8663 /* If pointers extend signed and this is a pointer in Pmode, say that
8664 all the bits above ptr_mode are known to be sign bit copies. */
8665 if (! POINTERS_EXTEND_UNSIGNED && GET_MODE (x) == Pmode && mode == Pmode
8666 && REG_POINTER (x))
8667 return GET_MODE_BITSIZE (Pmode) - GET_MODE_BITSIZE (ptr_mode) + 1;
8668 #endif
8670 if (reg_last_set_value[REGNO (x)] != 0
8671 && reg_last_set_mode[REGNO (x)] == mode
8672 && (reg_last_set_label[REGNO (x)] == label_tick
8673 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
8674 && REG_N_SETS (REGNO (x)) == 1
8675 && ! REGNO_REG_SET_P (ENTRY_BLOCK_PTR->next_bb->global_live_at_start,
8676 REGNO (x))))
8677 && INSN_CUID (reg_last_set[REGNO (x)]) < subst_low_cuid)
8678 return reg_last_set_sign_bit_copies[REGNO (x)];
8680 tem = get_last_value (x);
8681 if (tem != 0)
8682 return num_sign_bit_copies_with_known (tem, mode);
8684 if (nonzero_sign_valid && reg_sign_bit_copies[REGNO (x)] != 0
8685 && GET_MODE_BITSIZE (GET_MODE (x)) == bitwidth)
8686 return reg_sign_bit_copies[REGNO (x)];
8687 break;
8689 case MEM:
8690 #ifdef LOAD_EXTEND_OP
8691 /* Some RISC machines sign-extend all loads of smaller than a word. */
8692 if (LOAD_EXTEND_OP (GET_MODE (x)) == SIGN_EXTEND)
8693 return MAX (1, ((int) bitwidth
8694 - (int) GET_MODE_BITSIZE (GET_MODE (x)) + 1));
8695 #endif
8696 break;
8698 case CONST_INT:
8699 /* If the constant is negative, take its 1's complement and remask.
8700 Then see how many zero bits we have. */
8701 nonzero = INTVAL (x) & GET_MODE_MASK (mode);
8702 if (bitwidth <= HOST_BITS_PER_WIDE_INT
8703 && (nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
8704 nonzero = (~nonzero) & GET_MODE_MASK (mode);
8706 return (nonzero == 0 ? bitwidth : bitwidth - floor_log2 (nonzero) - 1);
8708 case SUBREG:
8709 /* If this is a SUBREG for a promoted object that is sign-extended
8710 and we are looking at it in a wider mode, we know that at least the
8711 high-order bits are known to be sign bit copies. */
8713 if (SUBREG_PROMOTED_VAR_P (x) && ! SUBREG_PROMOTED_UNSIGNED_P (x))
8715 num0 = num_sign_bit_copies_with_known (SUBREG_REG (x), mode);
8716 return MAX ((int) bitwidth
8717 - (int) GET_MODE_BITSIZE (GET_MODE (x)) + 1,
8718 num0);
8721 /* For a smaller object, just ignore the high bits. */
8722 if (bitwidth <= GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))))
8724 num0 = num_sign_bit_copies_with_known (SUBREG_REG (x), VOIDmode);
8725 return MAX (1, (num0
8726 - (int) (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x)))
8727 - bitwidth)));
8730 #ifdef WORD_REGISTER_OPERATIONS
8731 #ifdef LOAD_EXTEND_OP
8732 /* For paradoxical SUBREGs on machines where all register operations
8733 affect the entire register, just look inside. Note that we are
8734 passing MODE to the recursive call, so the number of sign bit copies
8735 will remain relative to that mode, not the inner mode. */
8737 /* This works only if loads sign extend. Otherwise, if we get a
8738 reload for the inner part, it may be loaded from the stack, and
8739 then we lose all sign bit copies that existed before the store
8740 to the stack. */
8742 if ((GET_MODE_SIZE (GET_MODE (x))
8743 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
8744 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (x))) == SIGN_EXTEND
8745 && GET_CODE (SUBREG_REG (x)) == MEM)
8746 return num_sign_bit_copies_with_known (SUBREG_REG (x), mode);
8747 #endif
8748 #endif
8749 break;
8751 case SIGN_EXTRACT:
8752 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
8753 return MAX (1, (int) bitwidth - INTVAL (XEXP (x, 1)));
8754 break;
8756 case SIGN_EXTEND:
8757 return (bitwidth - GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
8758 + num_sign_bit_copies_with_known (XEXP (x, 0), VOIDmode));
8760 case TRUNCATE:
8761 /* For a smaller object, just ignore the high bits. */
8762 num0 = num_sign_bit_copies_with_known (XEXP (x, 0), VOIDmode);
8763 return MAX (1, (num0 - (int) (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
8764 - bitwidth)));
8766 case NOT:
8767 return num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8769 case ROTATE: case ROTATERT:
8770 /* If we are rotating left by a number of bits less than the number
8771 of sign bit copies, we can just subtract that amount from the
8772 number. */
8773 if (GET_CODE (XEXP (x, 1)) == CONST_INT
8774 && INTVAL (XEXP (x, 1)) >= 0
8775 && INTVAL (XEXP (x, 1)) < (int) bitwidth)
8777 num0 = num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8778 return MAX (1, num0 - (code == ROTATE ? INTVAL (XEXP (x, 1))
8779 : (int) bitwidth - INTVAL (XEXP (x, 1))));
8781 break;
8783 case NEG:
8784 /* In general, this subtracts one sign bit copy. But if the value
8785 is known to be positive, the number of sign bit copies is the
8786 same as that of the input. Finally, if the input has just one bit
8787 that might be nonzero, all the bits are copies of the sign bit. */
8788 num0 = num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8789 if (bitwidth > HOST_BITS_PER_WIDE_INT)
8790 return num0 > 1 ? num0 - 1 : 1;
8792 nonzero = nonzero_bits (XEXP (x, 0), mode);
8793 if (nonzero == 1)
8794 return bitwidth;
8796 if (num0 > 1
8797 && (((HOST_WIDE_INT) 1 << (bitwidth - 1)) & nonzero))
8798 num0--;
8800 return num0;
8802 case IOR: case AND: case XOR:
8803 case SMIN: case SMAX: case UMIN: case UMAX:
8804 /* Logical operations will preserve the number of sign-bit copies.
8805 MIN and MAX operations always return one of the operands. */
8806 num0 = num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8807 num1 = num_sign_bit_copies_with_known (XEXP (x, 1), mode);
8808 return MIN (num0, num1);
8810 case PLUS: case MINUS:
8811 /* For addition and subtraction, we can have a 1-bit carry. However,
8812 if we are subtracting 1 from a positive number, there will not
8813 be such a carry. Furthermore, if the positive number is known to
8814 be 0 or 1, we know the result is either -1 or 0. */
8816 if (code == PLUS && XEXP (x, 1) == constm1_rtx
8817 && bitwidth <= HOST_BITS_PER_WIDE_INT)
8819 nonzero = nonzero_bits (XEXP (x, 0), mode);
8820 if ((((HOST_WIDE_INT) 1 << (bitwidth - 1)) & nonzero) == 0)
8821 return (nonzero == 1 || nonzero == 0 ? bitwidth
8822 : bitwidth - floor_log2 (nonzero) - 1);
8825 num0 = num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8826 num1 = num_sign_bit_copies_with_known (XEXP (x, 1), mode);
8827 result = MAX (1, MIN (num0, num1) - 1);
8829 #ifdef POINTERS_EXTEND_UNSIGNED
8830 /* If pointers extend signed and this is an addition or subtraction
8831 to a pointer in Pmode, all the bits above ptr_mode are known to be
8832 sign bit copies. */
8833 if (! POINTERS_EXTEND_UNSIGNED && GET_MODE (x) == Pmode
8834 && (code == PLUS || code == MINUS)
8835 && GET_CODE (XEXP (x, 0)) == REG && REG_POINTER (XEXP (x, 0)))
8836 result = MAX ((int) (GET_MODE_BITSIZE (Pmode)
8837 - GET_MODE_BITSIZE (ptr_mode) + 1),
8838 result);
8839 #endif
8840 return result;
8842 case MULT:
8843 /* The number of bits of the product is the sum of the number of
8844 bits of both terms. However, unless one of the terms if known
8845 to be positive, we must allow for an additional bit since negating
8846 a negative number can remove one sign bit copy. */
8848 num0 = num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8849 num1 = num_sign_bit_copies_with_known (XEXP (x, 1), mode);
8851 result = bitwidth - (bitwidth - num0) - (bitwidth - num1);
8852 if (result > 0
8853 && (bitwidth > HOST_BITS_PER_WIDE_INT
8854 || (((nonzero_bits (XEXP (x, 0), mode)
8855 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
8856 && ((nonzero_bits (XEXP (x, 1), mode)
8857 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0))))
8858 result--;
8860 return MAX (1, result);
8862 case UDIV:
8863 /* The result must be <= the first operand. If the first operand
8864 has the high bit set, we know nothing about the number of sign
8865 bit copies. */
8866 if (bitwidth > HOST_BITS_PER_WIDE_INT)
8867 return 1;
8868 else if ((nonzero_bits (XEXP (x, 0), mode)
8869 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
8870 return 1;
8871 else
8872 return num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8874 case UMOD:
8875 /* The result must be <= the second operand. */
8876 return num_sign_bit_copies_with_known (XEXP (x, 1), mode);
8878 case DIV:
8879 /* Similar to unsigned division, except that we have to worry about
8880 the case where the divisor is negative, in which case we have
8881 to add 1. */
8882 result = num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8883 if (result > 1
8884 && (bitwidth > HOST_BITS_PER_WIDE_INT
8885 || (nonzero_bits (XEXP (x, 1), mode)
8886 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0))
8887 result--;
8889 return result;
8891 case MOD:
8892 result = num_sign_bit_copies_with_known (XEXP (x, 1), mode);
8893 if (result > 1
8894 && (bitwidth > HOST_BITS_PER_WIDE_INT
8895 || (nonzero_bits (XEXP (x, 1), mode)
8896 & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0))
8897 result--;
8899 return result;
8901 case ASHIFTRT:
8902 /* Shifts by a constant add to the number of bits equal to the
8903 sign bit. */
8904 num0 = num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8905 if (GET_CODE (XEXP (x, 1)) == CONST_INT
8906 && INTVAL (XEXP (x, 1)) > 0)
8907 num0 = MIN ((int) bitwidth, num0 + INTVAL (XEXP (x, 1)));
8909 return num0;
8911 case ASHIFT:
8912 /* Left shifts destroy copies. */
8913 if (GET_CODE (XEXP (x, 1)) != CONST_INT
8914 || INTVAL (XEXP (x, 1)) < 0
8915 || INTVAL (XEXP (x, 1)) >= (int) bitwidth)
8916 return 1;
8918 num0 = num_sign_bit_copies_with_known (XEXP (x, 0), mode);
8919 return MAX (1, num0 - INTVAL (XEXP (x, 1)));
8921 case IF_THEN_ELSE:
8922 num0 = num_sign_bit_copies_with_known (XEXP (x, 1), mode);
8923 num1 = num_sign_bit_copies_with_known (XEXP (x, 2), mode);
8924 return MIN (num0, num1);
8926 case EQ: case NE: case GE: case GT: case LE: case LT:
8927 case UNEQ: case LTGT: case UNGE: case UNGT: case UNLE: case UNLT:
8928 case GEU: case GTU: case LEU: case LTU:
8929 case UNORDERED: case ORDERED:
8930 /* If the constant is negative, take its 1's complement and remask.
8931 Then see how many zero bits we have. */
8932 nonzero = STORE_FLAG_VALUE;
8933 if (bitwidth <= HOST_BITS_PER_WIDE_INT
8934 && (nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))) != 0)
8935 nonzero = (~nonzero) & GET_MODE_MASK (mode);
8937 return (nonzero == 0 ? bitwidth : bitwidth - floor_log2 (nonzero) - 1);
8938 break;
8940 default:
8941 break;
8944 /* If we haven't been able to figure it out by one of the above rules,
8945 see if some of the high-order bits are known to be zero. If so,
8946 count those bits and return one less than that amount. If we can't
8947 safely compute the mask for this mode, always return BITWIDTH. */
8949 if (bitwidth > HOST_BITS_PER_WIDE_INT)
8950 return 1;
8952 nonzero = nonzero_bits (x, mode);
8953 return (nonzero & ((HOST_WIDE_INT) 1 << (bitwidth - 1))
8954 ? 1 : bitwidth - floor_log2 (nonzero) - 1);
8957 /* Return the number of "extended" bits there are in X, when interpreted
8958 as a quantity in MODE whose signedness is indicated by UNSIGNEDP. For
8959 unsigned quantities, this is the number of high-order zero bits.
8960 For signed quantities, this is the number of copies of the sign bit
8961 minus 1. In both case, this function returns the number of "spare"
8962 bits. For example, if two quantities for which this function returns
8963 at least 1 are added, the addition is known not to overflow.
8965 This function will always return 0 unless called during combine, which
8966 implies that it must be called from a define_split. */
8968 unsigned int
8969 extended_count (rtx x, enum machine_mode mode, int unsignedp)
8971 if (nonzero_sign_valid == 0)
8972 return 0;
8974 return (unsignedp
8975 ? (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
8976 ? (unsigned int) (GET_MODE_BITSIZE (mode) - 1
8977 - floor_log2 (nonzero_bits (x, mode)))
8978 : 0)
8979 : num_sign_bit_copies (x, mode) - 1);
8982 /* This function is called from `simplify_shift_const' to merge two
8983 outer operations. Specifically, we have already found that we need
8984 to perform operation *POP0 with constant *PCONST0 at the outermost
8985 position. We would now like to also perform OP1 with constant CONST1
8986 (with *POP0 being done last).
8988 Return 1 if we can do the operation and update *POP0 and *PCONST0 with
8989 the resulting operation. *PCOMP_P is set to 1 if we would need to
8990 complement the innermost operand, otherwise it is unchanged.
8992 MODE is the mode in which the operation will be done. No bits outside
8993 the width of this mode matter. It is assumed that the width of this mode
8994 is smaller than or equal to HOST_BITS_PER_WIDE_INT.
8996 If *POP0 or OP1 are NIL, it means no operation is required. Only NEG, PLUS,
8997 IOR, XOR, and AND are supported. We may set *POP0 to SET if the proper
8998 result is simply *PCONST0.
9000 If the resulting operation cannot be expressed as one operation, we
9001 return 0 and do not change *POP0, *PCONST0, and *PCOMP_P. */
9003 static int
9004 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)
9006 enum rtx_code op0 = *pop0;
9007 HOST_WIDE_INT const0 = *pconst0;
9009 const0 &= GET_MODE_MASK (mode);
9010 const1 &= GET_MODE_MASK (mode);
9012 /* If OP0 is an AND, clear unimportant bits in CONST1. */
9013 if (op0 == AND)
9014 const1 &= const0;
9016 /* If OP0 or OP1 is NIL, this is easy. Similarly if they are the same or
9017 if OP0 is SET. */
9019 if (op1 == NIL || op0 == SET)
9020 return 1;
9022 else if (op0 == NIL)
9023 op0 = op1, const0 = const1;
9025 else if (op0 == op1)
9027 switch (op0)
9029 case AND:
9030 const0 &= const1;
9031 break;
9032 case IOR:
9033 const0 |= const1;
9034 break;
9035 case XOR:
9036 const0 ^= const1;
9037 break;
9038 case PLUS:
9039 const0 += const1;
9040 break;
9041 case NEG:
9042 op0 = NIL;
9043 break;
9044 default:
9045 break;
9049 /* Otherwise, if either is a PLUS or NEG, we can't do anything. */
9050 else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
9051 return 0;
9053 /* If the two constants aren't the same, we can't do anything. The
9054 remaining six cases can all be done. */
9055 else if (const0 != const1)
9056 return 0;
9058 else
9059 switch (op0)
9061 case IOR:
9062 if (op1 == AND)
9063 /* (a & b) | b == b */
9064 op0 = SET;
9065 else /* op1 == XOR */
9066 /* (a ^ b) | b == a | b */
9068 break;
9070 case XOR:
9071 if (op1 == AND)
9072 /* (a & b) ^ b == (~a) & b */
9073 op0 = AND, *pcomp_p = 1;
9074 else /* op1 == IOR */
9075 /* (a | b) ^ b == a & ~b */
9076 op0 = AND, const0 = ~const0;
9077 break;
9079 case AND:
9080 if (op1 == IOR)
9081 /* (a | b) & b == b */
9082 op0 = SET;
9083 else /* op1 == XOR */
9084 /* (a ^ b) & b) == (~a) & b */
9085 *pcomp_p = 1;
9086 break;
9087 default:
9088 break;
9091 /* Check for NO-OP cases. */
9092 const0 &= GET_MODE_MASK (mode);
9093 if (const0 == 0
9094 && (op0 == IOR || op0 == XOR || op0 == PLUS))
9095 op0 = NIL;
9096 else if (const0 == 0 && op0 == AND)
9097 op0 = SET;
9098 else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
9099 && op0 == AND)
9100 op0 = NIL;
9102 /* ??? Slightly redundant with the above mask, but not entirely.
9103 Moving this above means we'd have to sign-extend the mode mask
9104 for the final test. */
9105 const0 = trunc_int_for_mode (const0, mode);
9107 *pop0 = op0;
9108 *pconst0 = const0;
9110 return 1;
9113 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
9114 The result of the shift is RESULT_MODE. X, if nonzero, is an expression
9115 that we started with.
9117 The shift is normally computed in the widest mode we find in VAROP, as
9118 long as it isn't a different number of words than RESULT_MODE. Exceptions
9119 are ASHIFTRT and ROTATE, which are always done in their original mode, */
9121 static rtx
9122 simplify_shift_const (rtx x, enum rtx_code code,
9123 enum machine_mode result_mode, rtx varop,
9124 int orig_count)
9126 enum rtx_code orig_code = code;
9127 unsigned int count;
9128 int signed_count;
9129 enum machine_mode mode = result_mode;
9130 enum machine_mode shift_mode, tmode;
9131 unsigned int mode_words
9132 = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
9133 /* We form (outer_op (code varop count) (outer_const)). */
9134 enum rtx_code outer_op = NIL;
9135 HOST_WIDE_INT outer_const = 0;
9136 rtx const_rtx;
9137 int complement_p = 0;
9138 rtx new;
9140 /* Make sure and truncate the "natural" shift on the way in. We don't
9141 want to do this inside the loop as it makes it more difficult to
9142 combine shifts. */
9143 if (SHIFT_COUNT_TRUNCATED)
9144 orig_count &= GET_MODE_BITSIZE (mode) - 1;
9146 /* If we were given an invalid count, don't do anything except exactly
9147 what was requested. */
9149 if (orig_count < 0 || orig_count >= (int) GET_MODE_BITSIZE (mode))
9151 if (x)
9152 return x;
9154 return gen_rtx_fmt_ee (code, mode, varop, GEN_INT (orig_count));
9157 count = orig_count;
9159 /* Unless one of the branches of the `if' in this loop does a `continue',
9160 we will `break' the loop after the `if'. */
9162 while (count != 0)
9164 /* If we have an operand of (clobber (const_int 0)), just return that
9165 value. */
9166 if (GET_CODE (varop) == CLOBBER)
9167 return varop;
9169 /* If we discovered we had to complement VAROP, leave. Making a NOT
9170 here would cause an infinite loop. */
9171 if (complement_p)
9172 break;
9174 /* Convert ROTATERT to ROTATE. */
9175 if (code == ROTATERT)
9177 unsigned int bitsize = GET_MODE_BITSIZE (result_mode);;
9178 code = ROTATE;
9179 if (VECTOR_MODE_P (result_mode))
9180 count = bitsize / GET_MODE_NUNITS (result_mode) - count;
9181 else
9182 count = bitsize - count;
9185 /* We need to determine what mode we will do the shift in. If the
9186 shift is a right shift or a ROTATE, we must always do it in the mode
9187 it was originally done in. Otherwise, we can do it in MODE, the
9188 widest mode encountered. */
9189 shift_mode
9190 = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
9191 ? result_mode : mode);
9193 /* Handle cases where the count is greater than the size of the mode
9194 minus 1. For ASHIFT, use the size minus one as the count (this can
9195 occur when simplifying (lshiftrt (ashiftrt ..))). For rotates,
9196 take the count modulo the size. For other shifts, the result is
9197 zero.
9199 Since these shifts are being produced by the compiler by combining
9200 multiple operations, each of which are defined, we know what the
9201 result is supposed to be. */
9203 if (count > (unsigned int) (GET_MODE_BITSIZE (shift_mode) - 1))
9205 if (code == ASHIFTRT)
9206 count = GET_MODE_BITSIZE (shift_mode) - 1;
9207 else if (code == ROTATE || code == ROTATERT)
9208 count %= GET_MODE_BITSIZE (shift_mode);
9209 else
9211 /* We can't simply return zero because there may be an
9212 outer op. */
9213 varop = const0_rtx;
9214 count = 0;
9215 break;
9219 /* An arithmetic right shift of a quantity known to be -1 or 0
9220 is a no-op. */
9221 if (code == ASHIFTRT
9222 && (num_sign_bit_copies (varop, shift_mode)
9223 == GET_MODE_BITSIZE (shift_mode)))
9225 count = 0;
9226 break;
9229 /* If we are doing an arithmetic right shift and discarding all but
9230 the sign bit copies, this is equivalent to doing a shift by the
9231 bitsize minus one. Convert it into that shift because it will often
9232 allow other simplifications. */
9234 if (code == ASHIFTRT
9235 && (count + num_sign_bit_copies (varop, shift_mode)
9236 >= GET_MODE_BITSIZE (shift_mode)))
9237 count = GET_MODE_BITSIZE (shift_mode) - 1;
9239 /* We simplify the tests below and elsewhere by converting
9240 ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
9241 `make_compound_operation' will convert it to an ASHIFTRT for
9242 those machines (such as VAX) that don't have an LSHIFTRT. */
9243 if (GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9244 && code == ASHIFTRT
9245 && ((nonzero_bits (varop, shift_mode)
9246 & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (shift_mode) - 1)))
9247 == 0))
9248 code = LSHIFTRT;
9250 if (code == LSHIFTRT
9251 && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9252 && !(nonzero_bits (varop, shift_mode) >> count))
9253 varop = const0_rtx;
9254 if (code == ASHIFT
9255 && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9256 && !((nonzero_bits (varop, shift_mode) << count)
9257 & GET_MODE_MASK (shift_mode)))
9258 varop = const0_rtx;
9260 switch (GET_CODE (varop))
9262 case SIGN_EXTEND:
9263 case ZERO_EXTEND:
9264 case SIGN_EXTRACT:
9265 case ZERO_EXTRACT:
9266 new = expand_compound_operation (varop);
9267 if (new != varop)
9269 varop = new;
9270 continue;
9272 break;
9274 case MEM:
9275 /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
9276 minus the width of a smaller mode, we can do this with a
9277 SIGN_EXTEND or ZERO_EXTEND from the narrower memory location. */
9278 if ((code == ASHIFTRT || code == LSHIFTRT)
9279 && ! mode_dependent_address_p (XEXP (varop, 0))
9280 && ! MEM_VOLATILE_P (varop)
9281 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
9282 MODE_INT, 1)) != BLKmode)
9284 new = adjust_address_nv (varop, tmode,
9285 BYTES_BIG_ENDIAN ? 0
9286 : count / BITS_PER_UNIT);
9288 varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
9289 : ZERO_EXTEND, mode, new);
9290 count = 0;
9291 continue;
9293 break;
9295 case USE:
9296 /* Similar to the case above, except that we can only do this if
9297 the resulting mode is the same as that of the underlying
9298 MEM and adjust the address depending on the *bits* endianness
9299 because of the way that bit-field extract insns are defined. */
9300 if ((code == ASHIFTRT || code == LSHIFTRT)
9301 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
9302 MODE_INT, 1)) != BLKmode
9303 && tmode == GET_MODE (XEXP (varop, 0)))
9305 if (BITS_BIG_ENDIAN)
9306 new = XEXP (varop, 0);
9307 else
9309 new = copy_rtx (XEXP (varop, 0));
9310 SUBST (XEXP (new, 0),
9311 plus_constant (XEXP (new, 0),
9312 count / BITS_PER_UNIT));
9315 varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
9316 : ZERO_EXTEND, mode, new);
9317 count = 0;
9318 continue;
9320 break;
9322 case SUBREG:
9323 /* If VAROP is a SUBREG, strip it as long as the inner operand has
9324 the same number of words as what we've seen so far. Then store
9325 the widest mode in MODE. */
9326 if (subreg_lowpart_p (varop)
9327 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
9328 > GET_MODE_SIZE (GET_MODE (varop)))
9329 && (unsigned int) ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
9330 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
9331 == mode_words)
9333 varop = SUBREG_REG (varop);
9334 if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
9335 mode = GET_MODE (varop);
9336 continue;
9338 break;
9340 case MULT:
9341 /* Some machines use MULT instead of ASHIFT because MULT
9342 is cheaper. But it is still better on those machines to
9343 merge two shifts into one. */
9344 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9345 && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
9347 varop
9348 = gen_binary (ASHIFT, GET_MODE (varop), XEXP (varop, 0),
9349 GEN_INT (exact_log2 (INTVAL (XEXP (varop, 1)))));
9350 continue;
9352 break;
9354 case UDIV:
9355 /* Similar, for when divides are cheaper. */
9356 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9357 && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
9359 varop
9360 = gen_binary (LSHIFTRT, GET_MODE (varop), XEXP (varop, 0),
9361 GEN_INT (exact_log2 (INTVAL (XEXP (varop, 1)))));
9362 continue;
9364 break;
9366 case ASHIFTRT:
9367 /* If we are extracting just the sign bit of an arithmetic
9368 right shift, that shift is not needed. However, the sign
9369 bit of a wider mode may be different from what would be
9370 interpreted as the sign bit in a narrower mode, so, if
9371 the result is narrower, don't discard the shift. */
9372 if (code == LSHIFTRT
9373 && count == (unsigned int) (GET_MODE_BITSIZE (result_mode) - 1)
9374 && (GET_MODE_BITSIZE (result_mode)
9375 >= GET_MODE_BITSIZE (GET_MODE (varop))))
9377 varop = XEXP (varop, 0);
9378 continue;
9381 /* ... fall through ... */
9383 case LSHIFTRT:
9384 case ASHIFT:
9385 case ROTATE:
9386 /* Here we have two nested shifts. The result is usually the
9387 AND of a new shift with a mask. We compute the result below. */
9388 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9389 && INTVAL (XEXP (varop, 1)) >= 0
9390 && INTVAL (XEXP (varop, 1)) < GET_MODE_BITSIZE (GET_MODE (varop))
9391 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9392 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
9394 enum rtx_code first_code = GET_CODE (varop);
9395 unsigned int first_count = INTVAL (XEXP (varop, 1));
9396 unsigned HOST_WIDE_INT mask;
9397 rtx mask_rtx;
9399 /* We have one common special case. We can't do any merging if
9400 the inner code is an ASHIFTRT of a smaller mode. However, if
9401 we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
9402 with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
9403 we can convert it to
9404 (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0 C2) C3) C1).
9405 This simplifies certain SIGN_EXTEND operations. */
9406 if (code == ASHIFT && first_code == ASHIFTRT
9407 && count == (unsigned int)
9408 (GET_MODE_BITSIZE (result_mode)
9409 - GET_MODE_BITSIZE (GET_MODE (varop))))
9411 /* C3 has the low-order C1 bits zero. */
9413 mask = (GET_MODE_MASK (mode)
9414 & ~(((HOST_WIDE_INT) 1 << first_count) - 1));
9416 varop = simplify_and_const_int (NULL_RTX, result_mode,
9417 XEXP (varop, 0), mask);
9418 varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
9419 varop, count);
9420 count = first_count;
9421 code = ASHIFTRT;
9422 continue;
9425 /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
9426 than C1 high-order bits equal to the sign bit, we can convert
9427 this to either an ASHIFT or an ASHIFTRT depending on the
9428 two counts.
9430 We cannot do this if VAROP's mode is not SHIFT_MODE. */
9432 if (code == ASHIFTRT && first_code == ASHIFT
9433 && GET_MODE (varop) == shift_mode
9434 && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
9435 > first_count))
9437 varop = XEXP (varop, 0);
9439 signed_count = count - first_count;
9440 if (signed_count < 0)
9441 count = -signed_count, code = ASHIFT;
9442 else
9443 count = signed_count;
9445 continue;
9448 /* There are some cases we can't do. If CODE is ASHIFTRT,
9449 we can only do this if FIRST_CODE is also ASHIFTRT.
9451 We can't do the case when CODE is ROTATE and FIRST_CODE is
9452 ASHIFTRT.
9454 If the mode of this shift is not the mode of the outer shift,
9455 we can't do this if either shift is a right shift or ROTATE.
9457 Finally, we can't do any of these if the mode is too wide
9458 unless the codes are the same.
9460 Handle the case where the shift codes are the same
9461 first. */
9463 if (code == first_code)
9465 if (GET_MODE (varop) != result_mode
9466 && (code == ASHIFTRT || code == LSHIFTRT
9467 || code == ROTATE))
9468 break;
9470 count += first_count;
9471 varop = XEXP (varop, 0);
9472 continue;
9475 if (code == ASHIFTRT
9476 || (code == ROTATE && first_code == ASHIFTRT)
9477 || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT
9478 || (GET_MODE (varop) != result_mode
9479 && (first_code == ASHIFTRT || first_code == LSHIFTRT
9480 || first_code == ROTATE
9481 || code == ROTATE)))
9482 break;
9484 /* To compute the mask to apply after the shift, shift the
9485 nonzero bits of the inner shift the same way the
9486 outer shift will. */
9488 mask_rtx = GEN_INT (nonzero_bits (varop, GET_MODE (varop)));
9490 mask_rtx
9491 = simplify_binary_operation (code, result_mode, mask_rtx,
9492 GEN_INT (count));
9494 /* Give up if we can't compute an outer operation to use. */
9495 if (mask_rtx == 0
9496 || GET_CODE (mask_rtx) != CONST_INT
9497 || ! merge_outer_ops (&outer_op, &outer_const, AND,
9498 INTVAL (mask_rtx),
9499 result_mode, &complement_p))
9500 break;
9502 /* If the shifts are in the same direction, we add the
9503 counts. Otherwise, we subtract them. */
9504 signed_count = count;
9505 if ((code == ASHIFTRT || code == LSHIFTRT)
9506 == (first_code == ASHIFTRT || first_code == LSHIFTRT))
9507 signed_count += first_count;
9508 else
9509 signed_count -= first_count;
9511 /* If COUNT is positive, the new shift is usually CODE,
9512 except for the two exceptions below, in which case it is
9513 FIRST_CODE. If the count is negative, FIRST_CODE should
9514 always be used */
9515 if (signed_count > 0
9516 && ((first_code == ROTATE && code == ASHIFT)
9517 || (first_code == ASHIFTRT && code == LSHIFTRT)))
9518 code = first_code, count = signed_count;
9519 else if (signed_count < 0)
9520 code = first_code, count = -signed_count;
9521 else
9522 count = signed_count;
9524 varop = XEXP (varop, 0);
9525 continue;
9528 /* If we have (A << B << C) for any shift, we can convert this to
9529 (A << C << B). This wins if A is a constant. Only try this if
9530 B is not a constant. */
9532 else if (GET_CODE (varop) == code
9533 && GET_CODE (XEXP (varop, 1)) != CONST_INT
9534 && 0 != (new
9535 = simplify_binary_operation (code, mode,
9536 XEXP (varop, 0),
9537 GEN_INT (count))))
9539 varop = gen_rtx_fmt_ee (code, mode, new, XEXP (varop, 1));
9540 count = 0;
9541 continue;
9543 break;
9545 case NOT:
9546 /* Make this fit the case below. */
9547 varop = gen_rtx_XOR (mode, XEXP (varop, 0),
9548 GEN_INT (GET_MODE_MASK (mode)));
9549 continue;
9551 case IOR:
9552 case AND:
9553 case XOR:
9554 /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
9555 with C the size of VAROP - 1 and the shift is logical if
9556 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
9557 we have an (le X 0) operation. If we have an arithmetic shift
9558 and STORE_FLAG_VALUE is 1 or we have a logical shift with
9559 STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation. */
9561 if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
9562 && XEXP (XEXP (varop, 0), 1) == constm1_rtx
9563 && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9564 && (code == LSHIFTRT || code == ASHIFTRT)
9565 && count == (unsigned int)
9566 (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
9567 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
9569 count = 0;
9570 varop = gen_rtx_LE (GET_MODE (varop), XEXP (varop, 1),
9571 const0_rtx);
9573 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
9574 varop = gen_rtx_NEG (GET_MODE (varop), varop);
9576 continue;
9579 /* If we have (shift (logical)), move the logical to the outside
9580 to allow it to possibly combine with another logical and the
9581 shift to combine with another shift. This also canonicalizes to
9582 what a ZERO_EXTRACT looks like. Also, some machines have
9583 (and (shift)) insns. */
9585 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9586 /* We can't do this if we have (ashiftrt (xor)) and the
9587 constant has its sign bit set in shift_mode. */
9588 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
9589 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
9590 shift_mode))
9591 && (new = simplify_binary_operation (code, result_mode,
9592 XEXP (varop, 1),
9593 GEN_INT (count))) != 0
9594 && GET_CODE (new) == CONST_INT
9595 && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
9596 INTVAL (new), result_mode, &complement_p))
9598 varop = XEXP (varop, 0);
9599 continue;
9602 /* If we can't do that, try to simplify the shift in each arm of the
9603 logical expression, make a new logical expression, and apply
9604 the inverse distributive law. This also can't be done
9605 for some (ashiftrt (xor)). */
9606 if (code != ASHIFTRT || GET_CODE (varop)!= XOR
9607 || 0 <= trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
9608 shift_mode))
9610 rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
9611 XEXP (varop, 0), count);
9612 rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
9613 XEXP (varop, 1), count);
9615 varop = gen_binary (GET_CODE (varop), shift_mode, lhs, rhs);
9616 varop = apply_distributive_law (varop);
9618 count = 0;
9620 break;
9622 case EQ:
9623 /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
9624 says that the sign bit can be tested, FOO has mode MODE, C is
9625 GET_MODE_BITSIZE (MODE) - 1, and FOO has only its low-order bit
9626 that may be nonzero. */
9627 if (code == LSHIFTRT
9628 && XEXP (varop, 1) == const0_rtx
9629 && GET_MODE (XEXP (varop, 0)) == result_mode
9630 && count == (unsigned int) (GET_MODE_BITSIZE (result_mode) - 1)
9631 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9632 && ((STORE_FLAG_VALUE
9633 & ((HOST_WIDE_INT) 1
9634 < (GET_MODE_BITSIZE (result_mode) - 1))))
9635 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
9636 && merge_outer_ops (&outer_op, &outer_const, XOR,
9637 (HOST_WIDE_INT) 1, result_mode,
9638 &complement_p))
9640 varop = XEXP (varop, 0);
9641 count = 0;
9642 continue;
9644 break;
9646 case NEG:
9647 /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
9648 than the number of bits in the mode is equivalent to A. */
9649 if (code == LSHIFTRT
9650 && count == (unsigned int) (GET_MODE_BITSIZE (result_mode) - 1)
9651 && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
9653 varop = XEXP (varop, 0);
9654 count = 0;
9655 continue;
9658 /* NEG commutes with ASHIFT since it is multiplication. Move the
9659 NEG outside to allow shifts to combine. */
9660 if (code == ASHIFT
9661 && merge_outer_ops (&outer_op, &outer_const, NEG,
9662 (HOST_WIDE_INT) 0, result_mode,
9663 &complement_p))
9665 varop = XEXP (varop, 0);
9666 continue;
9668 break;
9670 case PLUS:
9671 /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
9672 is one less than the number of bits in the mode is
9673 equivalent to (xor A 1). */
9674 if (code == LSHIFTRT
9675 && count == (unsigned int) (GET_MODE_BITSIZE (result_mode) - 1)
9676 && XEXP (varop, 1) == constm1_rtx
9677 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
9678 && merge_outer_ops (&outer_op, &outer_const, XOR,
9679 (HOST_WIDE_INT) 1, result_mode,
9680 &complement_p))
9682 count = 0;
9683 varop = XEXP (varop, 0);
9684 continue;
9687 /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
9688 that might be nonzero in BAR are those being shifted out and those
9689 bits are known zero in FOO, we can replace the PLUS with FOO.
9690 Similarly in the other operand order. This code occurs when
9691 we are computing the size of a variable-size array. */
9693 if ((code == ASHIFTRT || code == LSHIFTRT)
9694 && count < HOST_BITS_PER_WIDE_INT
9695 && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
9696 && (nonzero_bits (XEXP (varop, 1), result_mode)
9697 & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
9699 varop = XEXP (varop, 0);
9700 continue;
9702 else if ((code == ASHIFTRT || code == LSHIFTRT)
9703 && count < HOST_BITS_PER_WIDE_INT
9704 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9705 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
9706 >> count)
9707 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
9708 & nonzero_bits (XEXP (varop, 1),
9709 result_mode)))
9711 varop = XEXP (varop, 1);
9712 continue;
9715 /* (ashift (plus foo C) N) is (plus (ashift foo N) C'). */
9716 if (code == ASHIFT
9717 && GET_CODE (XEXP (varop, 1)) == CONST_INT
9718 && (new = simplify_binary_operation (ASHIFT, result_mode,
9719 XEXP (varop, 1),
9720 GEN_INT (count))) != 0
9721 && GET_CODE (new) == CONST_INT
9722 && merge_outer_ops (&outer_op, &outer_const, PLUS,
9723 INTVAL (new), result_mode, &complement_p))
9725 varop = XEXP (varop, 0);
9726 continue;
9728 break;
9730 case MINUS:
9731 /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
9732 with C the size of VAROP - 1 and the shift is logical if
9733 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
9734 we have a (gt X 0) operation. If the shift is arithmetic with
9735 STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
9736 we have a (neg (gt X 0)) operation. */
9738 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9739 && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
9740 && count == (unsigned int)
9741 (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
9742 && (code == LSHIFTRT || code == ASHIFTRT)
9743 && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
9744 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (varop, 0), 1))
9745 == count
9746 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
9748 count = 0;
9749 varop = gen_rtx_GT (GET_MODE (varop), XEXP (varop, 1),
9750 const0_rtx);
9752 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
9753 varop = gen_rtx_NEG (GET_MODE (varop), varop);
9755 continue;
9757 break;
9759 case TRUNCATE:
9760 /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
9761 if the truncate does not affect the value. */
9762 if (code == LSHIFTRT
9763 && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
9764 && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
9765 && (INTVAL (XEXP (XEXP (varop, 0), 1))
9766 >= (GET_MODE_BITSIZE (GET_MODE (XEXP (varop, 0)))
9767 - GET_MODE_BITSIZE (GET_MODE (varop)))))
9769 rtx varop_inner = XEXP (varop, 0);
9771 varop_inner
9772 = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
9773 XEXP (varop_inner, 0),
9774 GEN_INT
9775 (count + INTVAL (XEXP (varop_inner, 1))));
9776 varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
9777 count = 0;
9778 continue;
9780 break;
9782 default:
9783 break;
9786 break;
9789 /* We need to determine what mode to do the shift in. If the shift is
9790 a right shift or ROTATE, we must always do it in the mode it was
9791 originally done in. Otherwise, we can do it in MODE, the widest mode
9792 encountered. The code we care about is that of the shift that will
9793 actually be done, not the shift that was originally requested. */
9794 shift_mode
9795 = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
9796 ? result_mode : mode);
9798 /* We have now finished analyzing the shift. The result should be
9799 a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places. If
9800 OUTER_OP is non-NIL, it is an operation that needs to be applied
9801 to the result of the shift. OUTER_CONST is the relevant constant,
9802 but we must turn off all bits turned off in the shift.
9804 If we were passed a value for X, see if we can use any pieces of
9805 it. If not, make new rtx. */
9807 if (x && GET_RTX_CLASS (GET_CODE (x)) == RTX_BIN_ARITH
9808 && GET_CODE (XEXP (x, 1)) == CONST_INT
9809 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (x, 1)) == count)
9810 const_rtx = XEXP (x, 1);
9811 else
9812 const_rtx = GEN_INT (count);
9814 if (x && GET_CODE (XEXP (x, 0)) == SUBREG
9815 && GET_MODE (XEXP (x, 0)) == shift_mode
9816 && SUBREG_REG (XEXP (x, 0)) == varop)
9817 varop = XEXP (x, 0);
9818 else if (GET_MODE (varop) != shift_mode)
9819 varop = gen_lowpart (shift_mode, varop);
9821 /* If we can't make the SUBREG, try to return what we were given. */
9822 if (GET_CODE (varop) == CLOBBER)
9823 return x ? x : varop;
9825 new = simplify_binary_operation (code, shift_mode, varop, const_rtx);
9826 if (new != 0)
9827 x = new;
9828 else
9829 x = gen_rtx_fmt_ee (code, shift_mode, varop, const_rtx);
9831 /* If we have an outer operation and we just made a shift, it is
9832 possible that we could have simplified the shift were it not
9833 for the outer operation. So try to do the simplification
9834 recursively. */
9836 if (outer_op != NIL && GET_CODE (x) == code
9837 && GET_CODE (XEXP (x, 1)) == CONST_INT)
9838 x = simplify_shift_const (x, code, shift_mode, XEXP (x, 0),
9839 INTVAL (XEXP (x, 1)));
9841 /* If we were doing an LSHIFTRT in a wider mode than it was originally,
9842 turn off all the bits that the shift would have turned off. */
9843 if (orig_code == LSHIFTRT && result_mode != shift_mode)
9844 x = simplify_and_const_int (NULL_RTX, shift_mode, x,
9845 GET_MODE_MASK (result_mode) >> orig_count);
9847 /* Do the remainder of the processing in RESULT_MODE. */
9848 x = gen_lowpart (result_mode, x);
9850 /* If COMPLEMENT_P is set, we have to complement X before doing the outer
9851 operation. */
9852 if (complement_p)
9853 x = simplify_gen_unary (NOT, result_mode, x, result_mode);
9855 if (outer_op != NIL)
9857 if (GET_MODE_BITSIZE (result_mode) < HOST_BITS_PER_WIDE_INT)
9858 outer_const = trunc_int_for_mode (outer_const, result_mode);
9860 if (outer_op == AND)
9861 x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
9862 else if (outer_op == SET)
9863 /* This means that we have determined that the result is
9864 equivalent to a constant. This should be rare. */
9865 x = GEN_INT (outer_const);
9866 else if (GET_RTX_CLASS (outer_op) == RTX_UNARY)
9867 x = simplify_gen_unary (outer_op, result_mode, x, result_mode);
9868 else
9869 x = gen_binary (outer_op, result_mode, x, GEN_INT (outer_const));
9872 return x;
9875 /* Like recog, but we receive the address of a pointer to a new pattern.
9876 We try to match the rtx that the pointer points to.
9877 If that fails, we may try to modify or replace the pattern,
9878 storing the replacement into the same pointer object.
9880 Modifications include deletion or addition of CLOBBERs.
9882 PNOTES is a pointer to a location where any REG_UNUSED notes added for
9883 the CLOBBERs are placed.
9885 The value is the final insn code from the pattern ultimately matched,
9886 or -1. */
9888 static int
9889 recog_for_combine (rtx *pnewpat, rtx insn, rtx *pnotes)
9891 rtx pat = *pnewpat;
9892 int insn_code_number;
9893 int num_clobbers_to_add = 0;
9894 int i;
9895 rtx notes = 0;
9896 rtx old_notes, old_pat;
9898 /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
9899 we use to indicate that something didn't match. If we find such a
9900 thing, force rejection. */
9901 if (GET_CODE (pat) == PARALLEL)
9902 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
9903 if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
9904 && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
9905 return -1;
9907 old_pat = PATTERN (insn);
9908 old_notes = REG_NOTES (insn);
9909 PATTERN (insn) = pat;
9910 REG_NOTES (insn) = 0;
9912 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
9914 /* If it isn't, there is the possibility that we previously had an insn
9915 that clobbered some register as a side effect, but the combined
9916 insn doesn't need to do that. So try once more without the clobbers
9917 unless this represents an ASM insn. */
9919 if (insn_code_number < 0 && ! check_asm_operands (pat)
9920 && GET_CODE (pat) == PARALLEL)
9922 int pos;
9924 for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
9925 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
9927 if (i != pos)
9928 SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
9929 pos++;
9932 SUBST_INT (XVECLEN (pat, 0), pos);
9934 if (pos == 1)
9935 pat = XVECEXP (pat, 0, 0);
9937 PATTERN (insn) = pat;
9938 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
9940 PATTERN (insn) = old_pat;
9941 REG_NOTES (insn) = old_notes;
9943 /* Recognize all noop sets, these will be killed by followup pass. */
9944 if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
9945 insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
9947 /* If we had any clobbers to add, make a new pattern than contains
9948 them. Then check to make sure that all of them are dead. */
9949 if (num_clobbers_to_add)
9951 rtx newpat = gen_rtx_PARALLEL (VOIDmode,
9952 rtvec_alloc (GET_CODE (pat) == PARALLEL
9953 ? (XVECLEN (pat, 0)
9954 + num_clobbers_to_add)
9955 : num_clobbers_to_add + 1));
9957 if (GET_CODE (pat) == PARALLEL)
9958 for (i = 0; i < XVECLEN (pat, 0); i++)
9959 XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
9960 else
9961 XVECEXP (newpat, 0, 0) = pat;
9963 add_clobbers (newpat, insn_code_number);
9965 for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
9966 i < XVECLEN (newpat, 0); i++)
9968 if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) == REG
9969 && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
9970 return -1;
9971 notes = gen_rtx_EXPR_LIST (REG_UNUSED,
9972 XEXP (XVECEXP (newpat, 0, i), 0), notes);
9974 pat = newpat;
9977 *pnewpat = pat;
9978 *pnotes = notes;
9980 return insn_code_number;
9983 /* Like gen_lowpart_general but for use by combine. In combine it
9984 is not possible to create any new pseudoregs. However, it is
9985 safe to create invalid memory addresses, because combine will
9986 try to recognize them and all they will do is make the combine
9987 attempt fail.
9989 If for some reason this cannot do its job, an rtx
9990 (clobber (const_int 0)) is returned.
9991 An insn containing that will not be recognized. */
9993 static rtx
9994 gen_lowpart_for_combine (enum machine_mode mode, rtx x)
9996 rtx result;
9998 if (GET_MODE (x) == mode)
9999 return x;
10001 /* Return identity if this is a CONST or symbolic
10002 reference. */
10003 if (mode == Pmode
10004 && (GET_CODE (x) == CONST
10005 || GET_CODE (x) == SYMBOL_REF
10006 || GET_CODE (x) == LABEL_REF))
10007 return x;
10009 /* We can only support MODE being wider than a word if X is a
10010 constant integer or has a mode the same size. */
10012 if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
10013 && ! ((GET_MODE (x) == VOIDmode
10014 && (GET_CODE (x) == CONST_INT
10015 || GET_CODE (x) == CONST_DOUBLE))
10016 || GET_MODE_SIZE (GET_MODE (x)) == GET_MODE_SIZE (mode)))
10017 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
10019 /* X might be a paradoxical (subreg (mem)). In that case, gen_lowpart
10020 won't know what to do. So we will strip off the SUBREG here and
10021 process normally. */
10022 if (GET_CODE (x) == SUBREG && GET_CODE (SUBREG_REG (x)) == MEM)
10024 x = SUBREG_REG (x);
10025 if (GET_MODE (x) == mode)
10026 return x;
10029 result = gen_lowpart_common (mode, x);
10030 #ifdef CANNOT_CHANGE_MODE_CLASS
10031 if (result != 0
10032 && GET_CODE (result) == SUBREG
10033 && GET_CODE (SUBREG_REG (result)) == REG
10034 && REGNO (SUBREG_REG (result)) >= FIRST_PSEUDO_REGISTER)
10035 bitmap_set_bit (&subregs_of_mode, REGNO (SUBREG_REG (result))
10036 * MAX_MACHINE_MODE
10037 + GET_MODE (result));
10038 #endif
10040 if (result)
10041 return result;
10043 if (GET_CODE (x) == MEM)
10045 int offset = 0;
10047 /* Refuse to work on a volatile memory ref or one with a mode-dependent
10048 address. */
10049 if (MEM_VOLATILE_P (x) || mode_dependent_address_p (XEXP (x, 0)))
10050 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
10052 /* If we want to refer to something bigger than the original memref,
10053 generate a paradoxical subreg instead. That will force a reload
10054 of the original memref X. */
10055 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode))
10056 return gen_rtx_SUBREG (mode, x, 0);
10058 if (WORDS_BIG_ENDIAN)
10059 offset = (MAX (GET_MODE_SIZE (GET_MODE (x)), UNITS_PER_WORD)
10060 - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD));
10062 if (BYTES_BIG_ENDIAN)
10064 /* Adjust the address so that the address-after-the-data is
10065 unchanged. */
10066 offset -= (MIN (UNITS_PER_WORD, GET_MODE_SIZE (mode))
10067 - MIN (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (x))));
10070 return adjust_address_nv (x, mode, offset);
10073 /* If X is a comparison operator, rewrite it in a new mode. This
10074 probably won't match, but may allow further simplifications. */
10075 else if (COMPARISON_P (x))
10076 return gen_rtx_fmt_ee (GET_CODE (x), mode, XEXP (x, 0), XEXP (x, 1));
10078 /* If we couldn't simplify X any other way, just enclose it in a
10079 SUBREG. Normally, this SUBREG won't match, but some patterns may
10080 include an explicit SUBREG or we may simplify it further in combine. */
10081 else
10083 int offset = 0;
10084 rtx res;
10085 enum machine_mode sub_mode = GET_MODE (x);
10087 offset = subreg_lowpart_offset (mode, sub_mode);
10088 if (sub_mode == VOIDmode)
10090 sub_mode = int_mode_for_mode (mode);
10091 x = gen_lowpart_common (sub_mode, x);
10092 if (x == 0)
10093 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
10095 res = simplify_gen_subreg (mode, x, sub_mode, offset);
10096 if (res)
10097 return res;
10098 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
10102 /* These routines make binary and unary operations by first seeing if they
10103 fold; if not, a new expression is allocated. */
10105 static rtx
10106 gen_binary (enum rtx_code code, enum machine_mode mode, rtx op0, rtx op1)
10108 rtx result;
10109 rtx tem;
10111 if (GET_CODE (op0) == CLOBBER)
10112 return op0;
10113 else if (GET_CODE (op1) == CLOBBER)
10114 return op1;
10116 if (GET_RTX_CLASS (code) == RTX_COMM_ARITH
10117 && swap_commutative_operands_p (op0, op1))
10118 tem = op0, op0 = op1, op1 = tem;
10120 if (GET_RTX_CLASS (code) == RTX_COMPARE
10121 || GET_RTX_CLASS (code) == RTX_COMM_COMPARE)
10123 enum machine_mode op_mode = GET_MODE (op0);
10125 /* Strip the COMPARE from (REL_OP (compare X Y) 0) to get
10126 just (REL_OP X Y). */
10127 if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
10129 op1 = XEXP (op0, 1);
10130 op0 = XEXP (op0, 0);
10131 op_mode = GET_MODE (op0);
10134 if (op_mode == VOIDmode)
10135 op_mode = GET_MODE (op1);
10136 result = simplify_relational_operation (code, mode, op_mode, op0, op1);
10138 else
10139 result = simplify_binary_operation (code, mode, op0, op1);
10141 if (result)
10142 return result;
10144 /* Put complex operands first and constants second. */
10145 if (GET_RTX_CLASS (code) == RTX_COMM_ARITH
10146 && swap_commutative_operands_p (op0, op1))
10147 return gen_rtx_fmt_ee (code, mode, op1, op0);
10149 /* If we are turning off bits already known off in OP0, we need not do
10150 an AND. */
10151 else if (code == AND && GET_CODE (op1) == CONST_INT
10152 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
10153 && (nonzero_bits (op0, mode) & ~INTVAL (op1)) == 0)
10154 return op0;
10156 return gen_rtx_fmt_ee (code, mode, op0, op1);
10159 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
10160 comparison code that will be tested.
10162 The result is a possibly different comparison code to use. *POP0 and
10163 *POP1 may be updated.
10165 It is possible that we might detect that a comparison is either always
10166 true or always false. However, we do not perform general constant
10167 folding in combine, so this knowledge isn't useful. Such tautologies
10168 should have been detected earlier. Hence we ignore all such cases. */
10170 static enum rtx_code
10171 simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
10173 rtx op0 = *pop0;
10174 rtx op1 = *pop1;
10175 rtx tem, tem1;
10176 int i;
10177 enum machine_mode mode, tmode;
10179 /* Try a few ways of applying the same transformation to both operands. */
10180 while (1)
10182 #ifndef WORD_REGISTER_OPERATIONS
10183 /* The test below this one won't handle SIGN_EXTENDs on these machines,
10184 so check specially. */
10185 if (code != GTU && code != GEU && code != LTU && code != LEU
10186 && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
10187 && GET_CODE (XEXP (op0, 0)) == ASHIFT
10188 && GET_CODE (XEXP (op1, 0)) == ASHIFT
10189 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
10190 && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
10191 && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
10192 == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
10193 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10194 && XEXP (op0, 1) == XEXP (op1, 1)
10195 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
10196 && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
10197 && (INTVAL (XEXP (op0, 1))
10198 == (GET_MODE_BITSIZE (GET_MODE (op0))
10199 - (GET_MODE_BITSIZE
10200 (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
10202 op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
10203 op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
10205 #endif
10207 /* If both operands are the same constant shift, see if we can ignore the
10208 shift. We can if the shift is a rotate or if the bits shifted out of
10209 this shift are known to be zero for both inputs and if the type of
10210 comparison is compatible with the shift. */
10211 if (GET_CODE (op0) == GET_CODE (op1)
10212 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
10213 && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
10214 || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
10215 && (code != GT && code != LT && code != GE && code != LE))
10216 || (GET_CODE (op0) == ASHIFTRT
10217 && (code != GTU && code != LTU
10218 && code != GEU && code != LEU)))
10219 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10220 && INTVAL (XEXP (op0, 1)) >= 0
10221 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
10222 && XEXP (op0, 1) == XEXP (op1, 1))
10224 enum machine_mode mode = GET_MODE (op0);
10225 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
10226 int shift_count = INTVAL (XEXP (op0, 1));
10228 if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
10229 mask &= (mask >> shift_count) << shift_count;
10230 else if (GET_CODE (op0) == ASHIFT)
10231 mask = (mask & (mask << shift_count)) >> shift_count;
10233 if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
10234 && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
10235 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
10236 else
10237 break;
10240 /* If both operands are AND's of a paradoxical SUBREG by constant, the
10241 SUBREGs are of the same mode, and, in both cases, the AND would
10242 be redundant if the comparison was done in the narrower mode,
10243 do the comparison in the narrower mode (e.g., we are AND'ing with 1
10244 and the operand's possibly nonzero bits are 0xffffff01; in that case
10245 if we only care about QImode, we don't need the AND). This case
10246 occurs if the output mode of an scc insn is not SImode and
10247 STORE_FLAG_VALUE == 1 (e.g., the 386).
10249 Similarly, check for a case where the AND's are ZERO_EXTEND
10250 operations from some narrower mode even though a SUBREG is not
10251 present. */
10253 else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
10254 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10255 && GET_CODE (XEXP (op1, 1)) == CONST_INT)
10257 rtx inner_op0 = XEXP (op0, 0);
10258 rtx inner_op1 = XEXP (op1, 0);
10259 HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
10260 HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
10261 int changed = 0;
10263 if (GET_CODE (inner_op0) == SUBREG && GET_CODE (inner_op1) == SUBREG
10264 && (GET_MODE_SIZE (GET_MODE (inner_op0))
10265 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (inner_op0))))
10266 && (GET_MODE (SUBREG_REG (inner_op0))
10267 == GET_MODE (SUBREG_REG (inner_op1)))
10268 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (inner_op0)))
10269 <= HOST_BITS_PER_WIDE_INT)
10270 && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
10271 GET_MODE (SUBREG_REG (inner_op0)))))
10272 && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
10273 GET_MODE (SUBREG_REG (inner_op1))))))
10275 op0 = SUBREG_REG (inner_op0);
10276 op1 = SUBREG_REG (inner_op1);
10278 /* The resulting comparison is always unsigned since we masked
10279 off the original sign bit. */
10280 code = unsigned_condition (code);
10282 changed = 1;
10285 else if (c0 == c1)
10286 for (tmode = GET_CLASS_NARROWEST_MODE
10287 (GET_MODE_CLASS (GET_MODE (op0)));
10288 tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
10289 if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
10291 op0 = gen_lowpart (tmode, inner_op0);
10292 op1 = gen_lowpart (tmode, inner_op1);
10293 code = unsigned_condition (code);
10294 changed = 1;
10295 break;
10298 if (! changed)
10299 break;
10302 /* If both operands are NOT, we can strip off the outer operation
10303 and adjust the comparison code for swapped operands; similarly for
10304 NEG, except that this must be an equality comparison. */
10305 else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
10306 || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
10307 && (code == EQ || code == NE)))
10308 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
10310 else
10311 break;
10314 /* If the first operand is a constant, swap the operands and adjust the
10315 comparison code appropriately, but don't do this if the second operand
10316 is already a constant integer. */
10317 if (swap_commutative_operands_p (op0, op1))
10319 tem = op0, op0 = op1, op1 = tem;
10320 code = swap_condition (code);
10323 /* We now enter a loop during which we will try to simplify the comparison.
10324 For the most part, we only are concerned with comparisons with zero,
10325 but some things may really be comparisons with zero but not start
10326 out looking that way. */
10328 while (GET_CODE (op1) == CONST_INT)
10330 enum machine_mode mode = GET_MODE (op0);
10331 unsigned int mode_width = GET_MODE_BITSIZE (mode);
10332 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
10333 int equality_comparison_p;
10334 int sign_bit_comparison_p;
10335 int unsigned_comparison_p;
10336 HOST_WIDE_INT const_op;
10338 /* We only want to handle integral modes. This catches VOIDmode,
10339 CCmode, and the floating-point modes. An exception is that we
10340 can handle VOIDmode if OP0 is a COMPARE or a comparison
10341 operation. */
10343 if (GET_MODE_CLASS (mode) != MODE_INT
10344 && ! (mode == VOIDmode
10345 && (GET_CODE (op0) == COMPARE || COMPARISON_P (op0))))
10346 break;
10348 /* Get the constant we are comparing against and turn off all bits
10349 not on in our mode. */
10350 const_op = INTVAL (op1);
10351 if (mode != VOIDmode)
10352 const_op = trunc_int_for_mode (const_op, mode);
10353 op1 = GEN_INT (const_op);
10355 /* If we are comparing against a constant power of two and the value
10356 being compared can only have that single bit nonzero (e.g., it was
10357 `and'ed with that bit), we can replace this with a comparison
10358 with zero. */
10359 if (const_op
10360 && (code == EQ || code == NE || code == GE || code == GEU
10361 || code == LT || code == LTU)
10362 && mode_width <= HOST_BITS_PER_WIDE_INT
10363 && exact_log2 (const_op) >= 0
10364 && nonzero_bits (op0, mode) == (unsigned HOST_WIDE_INT) const_op)
10366 code = (code == EQ || code == GE || code == GEU ? NE : EQ);
10367 op1 = const0_rtx, const_op = 0;
10370 /* Similarly, if we are comparing a value known to be either -1 or
10371 0 with -1, change it to the opposite comparison against zero. */
10373 if (const_op == -1
10374 && (code == EQ || code == NE || code == GT || code == LE
10375 || code == GEU || code == LTU)
10376 && num_sign_bit_copies (op0, mode) == mode_width)
10378 code = (code == EQ || code == LE || code == GEU ? NE : EQ);
10379 op1 = const0_rtx, const_op = 0;
10382 /* Do some canonicalizations based on the comparison code. We prefer
10383 comparisons against zero and then prefer equality comparisons.
10384 If we can reduce the size of a constant, we will do that too. */
10386 switch (code)
10388 case LT:
10389 /* < C is equivalent to <= (C - 1) */
10390 if (const_op > 0)
10392 const_op -= 1;
10393 op1 = GEN_INT (const_op);
10394 code = LE;
10395 /* ... fall through to LE case below. */
10397 else
10398 break;
10400 case LE:
10401 /* <= C is equivalent to < (C + 1); we do this for C < 0 */
10402 if (const_op < 0)
10404 const_op += 1;
10405 op1 = GEN_INT (const_op);
10406 code = LT;
10409 /* If we are doing a <= 0 comparison on a value known to have
10410 a zero sign bit, we can replace this with == 0. */
10411 else if (const_op == 0
10412 && mode_width <= HOST_BITS_PER_WIDE_INT
10413 && (nonzero_bits (op0, mode)
10414 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
10415 code = EQ;
10416 break;
10418 case GE:
10419 /* >= C is equivalent to > (C - 1). */
10420 if (const_op > 0)
10422 const_op -= 1;
10423 op1 = GEN_INT (const_op);
10424 code = GT;
10425 /* ... fall through to GT below. */
10427 else
10428 break;
10430 case GT:
10431 /* > C is equivalent to >= (C + 1); we do this for C < 0. */
10432 if (const_op < 0)
10434 const_op += 1;
10435 op1 = GEN_INT (const_op);
10436 code = GE;
10439 /* If we are doing a > 0 comparison on a value known to have
10440 a zero sign bit, we can replace this with != 0. */
10441 else if (const_op == 0
10442 && mode_width <= HOST_BITS_PER_WIDE_INT
10443 && (nonzero_bits (op0, mode)
10444 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
10445 code = NE;
10446 break;
10448 case LTU:
10449 /* < C is equivalent to <= (C - 1). */
10450 if (const_op > 0)
10452 const_op -= 1;
10453 op1 = GEN_INT (const_op);
10454 code = LEU;
10455 /* ... fall through ... */
10458 /* (unsigned) < 0x80000000 is equivalent to >= 0. */
10459 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10460 && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
10462 const_op = 0, op1 = const0_rtx;
10463 code = GE;
10464 break;
10466 else
10467 break;
10469 case LEU:
10470 /* unsigned <= 0 is equivalent to == 0 */
10471 if (const_op == 0)
10472 code = EQ;
10474 /* (unsigned) <= 0x7fffffff is equivalent to >= 0. */
10475 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10476 && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
10478 const_op = 0, op1 = const0_rtx;
10479 code = GE;
10481 break;
10483 case GEU:
10484 /* >= C is equivalent to < (C - 1). */
10485 if (const_op > 1)
10487 const_op -= 1;
10488 op1 = GEN_INT (const_op);
10489 code = GTU;
10490 /* ... fall through ... */
10493 /* (unsigned) >= 0x80000000 is equivalent to < 0. */
10494 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10495 && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
10497 const_op = 0, op1 = const0_rtx;
10498 code = LT;
10499 break;
10501 else
10502 break;
10504 case GTU:
10505 /* unsigned > 0 is equivalent to != 0 */
10506 if (const_op == 0)
10507 code = NE;
10509 /* (unsigned) > 0x7fffffff is equivalent to < 0. */
10510 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10511 && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
10513 const_op = 0, op1 = const0_rtx;
10514 code = LT;
10516 break;
10518 default:
10519 break;
10522 /* Compute some predicates to simplify code below. */
10524 equality_comparison_p = (code == EQ || code == NE);
10525 sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
10526 unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
10527 || code == GEU);
10529 /* If this is a sign bit comparison and we can do arithmetic in
10530 MODE, say that we will only be needing the sign bit of OP0. */
10531 if (sign_bit_comparison_p
10532 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10533 op0 = force_to_mode (op0, mode,
10534 ((HOST_WIDE_INT) 1
10535 << (GET_MODE_BITSIZE (mode) - 1)),
10536 NULL_RTX, 0);
10538 /* Now try cases based on the opcode of OP0. If none of the cases
10539 does a "continue", we exit this loop immediately after the
10540 switch. */
10542 switch (GET_CODE (op0))
10544 case ZERO_EXTRACT:
10545 /* If we are extracting a single bit from a variable position in
10546 a constant that has only a single bit set and are comparing it
10547 with zero, we can convert this into an equality comparison
10548 between the position and the location of the single bit. */
10549 /* Except we can't if SHIFT_COUNT_TRUNCATED is set, since we might
10550 have already reduced the shift count modulo the word size. */
10551 if (!SHIFT_COUNT_TRUNCATED
10552 && GET_CODE (XEXP (op0, 0)) == CONST_INT
10553 && XEXP (op0, 1) == const1_rtx
10554 && equality_comparison_p && const_op == 0
10555 && (i = exact_log2 (INTVAL (XEXP (op0, 0)))) >= 0)
10557 if (BITS_BIG_ENDIAN)
10559 enum machine_mode new_mode
10560 = mode_for_extraction (EP_extzv, 1);
10561 if (new_mode == MAX_MACHINE_MODE)
10562 i = BITS_PER_WORD - 1 - i;
10563 else
10565 mode = new_mode;
10566 i = (GET_MODE_BITSIZE (mode) - 1 - i);
10570 op0 = XEXP (op0, 2);
10571 op1 = GEN_INT (i);
10572 const_op = i;
10574 /* Result is nonzero iff shift count is equal to I. */
10575 code = reverse_condition (code);
10576 continue;
10579 /* ... fall through ... */
10581 case SIGN_EXTRACT:
10582 tem = expand_compound_operation (op0);
10583 if (tem != op0)
10585 op0 = tem;
10586 continue;
10588 break;
10590 case NOT:
10591 /* If testing for equality, we can take the NOT of the constant. */
10592 if (equality_comparison_p
10593 && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
10595 op0 = XEXP (op0, 0);
10596 op1 = tem;
10597 continue;
10600 /* If just looking at the sign bit, reverse the sense of the
10601 comparison. */
10602 if (sign_bit_comparison_p)
10604 op0 = XEXP (op0, 0);
10605 code = (code == GE ? LT : GE);
10606 continue;
10608 break;
10610 case NEG:
10611 /* If testing for equality, we can take the NEG of the constant. */
10612 if (equality_comparison_p
10613 && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
10615 op0 = XEXP (op0, 0);
10616 op1 = tem;
10617 continue;
10620 /* The remaining cases only apply to comparisons with zero. */
10621 if (const_op != 0)
10622 break;
10624 /* When X is ABS or is known positive,
10625 (neg X) is < 0 if and only if X != 0. */
10627 if (sign_bit_comparison_p
10628 && (GET_CODE (XEXP (op0, 0)) == ABS
10629 || (mode_width <= HOST_BITS_PER_WIDE_INT
10630 && (nonzero_bits (XEXP (op0, 0), mode)
10631 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)))
10633 op0 = XEXP (op0, 0);
10634 code = (code == LT ? NE : EQ);
10635 continue;
10638 /* If we have NEG of something whose two high-order bits are the
10639 same, we know that "(-a) < 0" is equivalent to "a > 0". */
10640 if (num_sign_bit_copies (op0, mode) >= 2)
10642 op0 = XEXP (op0, 0);
10643 code = swap_condition (code);
10644 continue;
10646 break;
10648 case ROTATE:
10649 /* If we are testing equality and our count is a constant, we
10650 can perform the inverse operation on our RHS. */
10651 if (equality_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
10652 && (tem = simplify_binary_operation (ROTATERT, mode,
10653 op1, XEXP (op0, 1))) != 0)
10655 op0 = XEXP (op0, 0);
10656 op1 = tem;
10657 continue;
10660 /* If we are doing a < 0 or >= 0 comparison, it means we are testing
10661 a particular bit. Convert it to an AND of a constant of that
10662 bit. This will be converted into a ZERO_EXTRACT. */
10663 if (const_op == 0 && sign_bit_comparison_p
10664 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10665 && mode_width <= HOST_BITS_PER_WIDE_INT)
10667 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10668 ((HOST_WIDE_INT) 1
10669 << (mode_width - 1
10670 - INTVAL (XEXP (op0, 1)))));
10671 code = (code == LT ? NE : EQ);
10672 continue;
10675 /* Fall through. */
10677 case ABS:
10678 /* ABS is ignorable inside an equality comparison with zero. */
10679 if (const_op == 0 && equality_comparison_p)
10681 op0 = XEXP (op0, 0);
10682 continue;
10684 break;
10686 case SIGN_EXTEND:
10687 /* Can simplify (compare (zero/sign_extend FOO) CONST)
10688 to (compare FOO CONST) if CONST fits in FOO's mode and we
10689 are either testing inequality or have an unsigned comparison
10690 with ZERO_EXTEND or a signed comparison with SIGN_EXTEND. */
10691 if (! unsigned_comparison_p
10692 && (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10693 <= HOST_BITS_PER_WIDE_INT)
10694 && ((unsigned HOST_WIDE_INT) const_op
10695 < (((unsigned HOST_WIDE_INT) 1
10696 << (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0))) - 1)))))
10698 op0 = XEXP (op0, 0);
10699 continue;
10701 break;
10703 case SUBREG:
10704 /* Check for the case where we are comparing A - C1 with C2,
10705 both constants are smaller than 1/2 the maximum positive
10706 value in MODE, and the comparison is equality or unsigned.
10707 In that case, if A is either zero-extended to MODE or has
10708 sufficient sign bits so that the high-order bit in MODE
10709 is a copy of the sign in the inner mode, we can prove that it is
10710 safe to do the operation in the wider mode. This simplifies
10711 many range checks. */
10713 if (mode_width <= HOST_BITS_PER_WIDE_INT
10714 && subreg_lowpart_p (op0)
10715 && GET_CODE (SUBREG_REG (op0)) == PLUS
10716 && GET_CODE (XEXP (SUBREG_REG (op0), 1)) == CONST_INT
10717 && INTVAL (XEXP (SUBREG_REG (op0), 1)) < 0
10718 && (-INTVAL (XEXP (SUBREG_REG (op0), 1))
10719 < (HOST_WIDE_INT) (GET_MODE_MASK (mode) / 2))
10720 && (unsigned HOST_WIDE_INT) const_op < GET_MODE_MASK (mode) / 2
10721 && (0 == (nonzero_bits (XEXP (SUBREG_REG (op0), 0),
10722 GET_MODE (SUBREG_REG (op0)))
10723 & ~GET_MODE_MASK (mode))
10724 || (num_sign_bit_copies (XEXP (SUBREG_REG (op0), 0),
10725 GET_MODE (SUBREG_REG (op0)))
10726 > (unsigned int)
10727 (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
10728 - GET_MODE_BITSIZE (mode)))))
10730 op0 = SUBREG_REG (op0);
10731 continue;
10734 /* If the inner mode is narrower and we are extracting the low part,
10735 we can treat the SUBREG as if it were a ZERO_EXTEND. */
10736 if (subreg_lowpart_p (op0)
10737 && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) < mode_width)
10738 /* Fall through */ ;
10739 else
10740 break;
10742 /* ... fall through ... */
10744 case ZERO_EXTEND:
10745 if ((unsigned_comparison_p || equality_comparison_p)
10746 && (GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
10747 <= HOST_BITS_PER_WIDE_INT)
10748 && ((unsigned HOST_WIDE_INT) const_op
10749 < GET_MODE_MASK (GET_MODE (XEXP (op0, 0)))))
10751 op0 = XEXP (op0, 0);
10752 continue;
10754 break;
10756 case PLUS:
10757 /* (eq (plus X A) B) -> (eq X (minus B A)). We can only do
10758 this for equality comparisons due to pathological cases involving
10759 overflows. */
10760 if (equality_comparison_p
10761 && 0 != (tem = simplify_binary_operation (MINUS, mode,
10762 op1, XEXP (op0, 1))))
10764 op0 = XEXP (op0, 0);
10765 op1 = tem;
10766 continue;
10769 /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0. */
10770 if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
10771 && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
10773 op0 = XEXP (XEXP (op0, 0), 0);
10774 code = (code == LT ? EQ : NE);
10775 continue;
10777 break;
10779 case MINUS:
10780 /* We used to optimize signed comparisons against zero, but that
10781 was incorrect. Unsigned comparisons against zero (GTU, LEU)
10782 arrive here as equality comparisons, or (GEU, LTU) are
10783 optimized away. No need to special-case them. */
10785 /* (eq (minus A B) C) -> (eq A (plus B C)) or
10786 (eq B (minus A C)), whichever simplifies. We can only do
10787 this for equality comparisons due to pathological cases involving
10788 overflows. */
10789 if (equality_comparison_p
10790 && 0 != (tem = simplify_binary_operation (PLUS, mode,
10791 XEXP (op0, 1), op1)))
10793 op0 = XEXP (op0, 0);
10794 op1 = tem;
10795 continue;
10798 if (equality_comparison_p
10799 && 0 != (tem = simplify_binary_operation (MINUS, mode,
10800 XEXP (op0, 0), op1)))
10802 op0 = XEXP (op0, 1);
10803 op1 = tem;
10804 continue;
10807 /* The sign bit of (minus (ashiftrt X C) X), where C is the number
10808 of bits in X minus 1, is one iff X > 0. */
10809 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
10810 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10811 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (op0, 0), 1))
10812 == mode_width - 1
10813 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10815 op0 = XEXP (op0, 1);
10816 code = (code == GE ? LE : GT);
10817 continue;
10819 break;
10821 case XOR:
10822 /* (eq (xor A B) C) -> (eq A (xor B C)). This is a simplification
10823 if C is zero or B is a constant. */
10824 if (equality_comparison_p
10825 && 0 != (tem = simplify_binary_operation (XOR, mode,
10826 XEXP (op0, 1), op1)))
10828 op0 = XEXP (op0, 0);
10829 op1 = tem;
10830 continue;
10832 break;
10834 case EQ: case NE:
10835 case UNEQ: case LTGT:
10836 case LT: case LTU: case UNLT: case LE: case LEU: case UNLE:
10837 case GT: case GTU: case UNGT: case GE: case GEU: case UNGE:
10838 case UNORDERED: case ORDERED:
10839 /* We can't do anything if OP0 is a condition code value, rather
10840 than an actual data value. */
10841 if (const_op != 0
10842 || CC0_P (XEXP (op0, 0))
10843 || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
10844 break;
10846 /* Get the two operands being compared. */
10847 if (GET_CODE (XEXP (op0, 0)) == COMPARE)
10848 tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
10849 else
10850 tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
10852 /* Check for the cases where we simply want the result of the
10853 earlier test or the opposite of that result. */
10854 if (code == NE || code == EQ
10855 || (GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
10856 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10857 && (STORE_FLAG_VALUE
10858 & (((HOST_WIDE_INT) 1
10859 << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
10860 && (code == LT || code == GE)))
10862 enum rtx_code new_code;
10863 if (code == LT || code == NE)
10864 new_code = GET_CODE (op0);
10865 else
10866 new_code = combine_reversed_comparison_code (op0);
10868 if (new_code != UNKNOWN)
10870 code = new_code;
10871 op0 = tem;
10872 op1 = tem1;
10873 continue;
10876 break;
10878 case IOR:
10879 /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
10880 iff X <= 0. */
10881 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
10882 && XEXP (XEXP (op0, 0), 1) == constm1_rtx
10883 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10885 op0 = XEXP (op0, 1);
10886 code = (code == GE ? GT : LE);
10887 continue;
10889 break;
10891 case AND:
10892 /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1). This
10893 will be converted to a ZERO_EXTRACT later. */
10894 if (const_op == 0 && equality_comparison_p
10895 && GET_CODE (XEXP (op0, 0)) == ASHIFT
10896 && XEXP (XEXP (op0, 0), 0) == const1_rtx)
10898 op0 = simplify_and_const_int
10899 (op0, mode, gen_rtx_LSHIFTRT (mode,
10900 XEXP (op0, 1),
10901 XEXP (XEXP (op0, 0), 1)),
10902 (HOST_WIDE_INT) 1);
10903 continue;
10906 /* If we are comparing (and (lshiftrt X C1) C2) for equality with
10907 zero and X is a comparison and C1 and C2 describe only bits set
10908 in STORE_FLAG_VALUE, we can compare with X. */
10909 if (const_op == 0 && equality_comparison_p
10910 && mode_width <= HOST_BITS_PER_WIDE_INT
10911 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10912 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
10913 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10914 && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
10915 && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
10917 mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
10918 << INTVAL (XEXP (XEXP (op0, 0), 1)));
10919 if ((~STORE_FLAG_VALUE & mask) == 0
10920 && (COMPARISON_P (XEXP (XEXP (op0, 0), 0))
10921 || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
10922 && COMPARISON_P (tem))))
10924 op0 = XEXP (XEXP (op0, 0), 0);
10925 continue;
10929 /* If we are doing an equality comparison of an AND of a bit equal
10930 to the sign bit, replace this with a LT or GE comparison of
10931 the underlying value. */
10932 if (equality_comparison_p
10933 && const_op == 0
10934 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10935 && mode_width <= HOST_BITS_PER_WIDE_INT
10936 && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
10937 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
10939 op0 = XEXP (op0, 0);
10940 code = (code == EQ ? GE : LT);
10941 continue;
10944 /* If this AND operation is really a ZERO_EXTEND from a narrower
10945 mode, the constant fits within that mode, and this is either an
10946 equality or unsigned comparison, try to do this comparison in
10947 the narrower mode. */
10948 if ((equality_comparison_p || unsigned_comparison_p)
10949 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10950 && (i = exact_log2 ((INTVAL (XEXP (op0, 1))
10951 & GET_MODE_MASK (mode))
10952 + 1)) >= 0
10953 && const_op >> i == 0
10954 && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode)
10956 op0 = gen_lowpart (tmode, XEXP (op0, 0));
10957 continue;
10960 /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1
10961 fits in both M1 and M2 and the SUBREG is either paradoxical
10962 or represents the low part, permute the SUBREG and the AND
10963 and try again. */
10964 if (GET_CODE (XEXP (op0, 0)) == SUBREG)
10966 unsigned HOST_WIDE_INT c1;
10967 tmode = GET_MODE (SUBREG_REG (XEXP (op0, 0)));
10968 /* Require an integral mode, to avoid creating something like
10969 (AND:SF ...). */
10970 if (SCALAR_INT_MODE_P (tmode)
10971 /* It is unsafe to commute the AND into the SUBREG if the
10972 SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
10973 not defined. As originally written the upper bits
10974 have a defined value due to the AND operation.
10975 However, if we commute the AND inside the SUBREG then
10976 they no longer have defined values and the meaning of
10977 the code has been changed. */
10978 && (0
10979 #ifdef WORD_REGISTER_OPERATIONS
10980 || (mode_width > GET_MODE_BITSIZE (tmode)
10981 && mode_width <= BITS_PER_WORD)
10982 #endif
10983 || (mode_width <= GET_MODE_BITSIZE (tmode)
10984 && subreg_lowpart_p (XEXP (op0, 0))))
10985 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10986 && mode_width <= HOST_BITS_PER_WIDE_INT
10987 && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
10988 && ((c1 = INTVAL (XEXP (op0, 1))) & ~mask) == 0
10989 && (c1 & ~GET_MODE_MASK (tmode)) == 0
10990 && c1 != mask
10991 && c1 != GET_MODE_MASK (tmode))
10993 op0 = gen_binary (AND, tmode,
10994 SUBREG_REG (XEXP (op0, 0)),
10995 gen_int_mode (c1, tmode));
10996 op0 = gen_lowpart (mode, op0);
10997 continue;
11001 /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0). */
11002 if (const_op == 0 && equality_comparison_p
11003 && XEXP (op0, 1) == const1_rtx
11004 && GET_CODE (XEXP (op0, 0)) == NOT)
11006 op0 = simplify_and_const_int
11007 (NULL_RTX, mode, XEXP (XEXP (op0, 0), 0), (HOST_WIDE_INT) 1);
11008 code = (code == NE ? EQ : NE);
11009 continue;
11012 /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
11013 (eq (and (lshiftrt X) 1) 0).
11014 Also handle the case where (not X) is expressed using xor. */
11015 if (const_op == 0 && equality_comparison_p
11016 && XEXP (op0, 1) == const1_rtx
11017 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT)
11019 rtx shift_op = XEXP (XEXP (op0, 0), 0);
11020 rtx shift_count = XEXP (XEXP (op0, 0), 1);
11022 if (GET_CODE (shift_op) == NOT
11023 || (GET_CODE (shift_op) == XOR
11024 && GET_CODE (XEXP (shift_op, 1)) == CONST_INT
11025 && GET_CODE (shift_count) == CONST_INT
11026 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
11027 && (INTVAL (XEXP (shift_op, 1))
11028 == (HOST_WIDE_INT) 1 << INTVAL (shift_count))))
11030 op0 = simplify_and_const_int
11031 (NULL_RTX, mode,
11032 gen_rtx_LSHIFTRT (mode, XEXP (shift_op, 0), shift_count),
11033 (HOST_WIDE_INT) 1);
11034 code = (code == NE ? EQ : NE);
11035 continue;
11038 break;
11040 case ASHIFT:
11041 /* If we have (compare (ashift FOO N) (const_int C)) and
11042 the high order N bits of FOO (N+1 if an inequality comparison)
11043 are known to be zero, we can do this by comparing FOO with C
11044 shifted right N bits so long as the low-order N bits of C are
11045 zero. */
11046 if (GET_CODE (XEXP (op0, 1)) == CONST_INT
11047 && INTVAL (XEXP (op0, 1)) >= 0
11048 && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
11049 < HOST_BITS_PER_WIDE_INT)
11050 && ((const_op
11051 & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0)
11052 && mode_width <= HOST_BITS_PER_WIDE_INT
11053 && (nonzero_bits (XEXP (op0, 0), mode)
11054 & ~(mask >> (INTVAL (XEXP (op0, 1))
11055 + ! equality_comparison_p))) == 0)
11057 /* We must perform a logical shift, not an arithmetic one,
11058 as we want the top N bits of C to be zero. */
11059 unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
11061 temp >>= INTVAL (XEXP (op0, 1));
11062 op1 = gen_int_mode (temp, mode);
11063 op0 = XEXP (op0, 0);
11064 continue;
11067 /* If we are doing a sign bit comparison, it means we are testing
11068 a particular bit. Convert it to the appropriate AND. */
11069 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
11070 && mode_width <= HOST_BITS_PER_WIDE_INT)
11072 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11073 ((HOST_WIDE_INT) 1
11074 << (mode_width - 1
11075 - INTVAL (XEXP (op0, 1)))));
11076 code = (code == LT ? NE : EQ);
11077 continue;
11080 /* If this an equality comparison with zero and we are shifting
11081 the low bit to the sign bit, we can convert this to an AND of the
11082 low-order bit. */
11083 if (const_op == 0 && equality_comparison_p
11084 && GET_CODE (XEXP (op0, 1)) == CONST_INT
11085 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1))
11086 == mode_width - 1)
11088 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11089 (HOST_WIDE_INT) 1);
11090 continue;
11092 break;
11094 case ASHIFTRT:
11095 /* If this is an equality comparison with zero, we can do this
11096 as a logical shift, which might be much simpler. */
11097 if (equality_comparison_p && const_op == 0
11098 && GET_CODE (XEXP (op0, 1)) == CONST_INT)
11100 op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
11101 XEXP (op0, 0),
11102 INTVAL (XEXP (op0, 1)));
11103 continue;
11106 /* If OP0 is a sign extension and CODE is not an unsigned comparison,
11107 do the comparison in a narrower mode. */
11108 if (! unsigned_comparison_p
11109 && GET_CODE (XEXP (op0, 1)) == CONST_INT
11110 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11111 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
11112 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
11113 MODE_INT, 1)) != BLKmode
11114 && (((unsigned HOST_WIDE_INT) const_op
11115 + (GET_MODE_MASK (tmode) >> 1) + 1)
11116 <= GET_MODE_MASK (tmode)))
11118 op0 = gen_lowpart (tmode, XEXP (XEXP (op0, 0), 0));
11119 continue;
11122 /* Likewise if OP0 is a PLUS of a sign extension with a
11123 constant, which is usually represented with the PLUS
11124 between the shifts. */
11125 if (! unsigned_comparison_p
11126 && GET_CODE (XEXP (op0, 1)) == CONST_INT
11127 && GET_CODE (XEXP (op0, 0)) == PLUS
11128 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
11129 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
11130 && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
11131 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
11132 MODE_INT, 1)) != BLKmode
11133 && (((unsigned HOST_WIDE_INT) const_op
11134 + (GET_MODE_MASK (tmode) >> 1) + 1)
11135 <= GET_MODE_MASK (tmode)))
11137 rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
11138 rtx add_const = XEXP (XEXP (op0, 0), 1);
11139 rtx new_const = gen_binary (ASHIFTRT, GET_MODE (op0), add_const,
11140 XEXP (op0, 1));
11142 op0 = gen_binary (PLUS, tmode,
11143 gen_lowpart (tmode, inner),
11144 new_const);
11145 continue;
11148 /* ... fall through ... */
11149 case LSHIFTRT:
11150 /* If we have (compare (xshiftrt FOO N) (const_int C)) and
11151 the low order N bits of FOO are known to be zero, we can do this
11152 by comparing FOO with C shifted left N bits so long as no
11153 overflow occurs. */
11154 if (GET_CODE (XEXP (op0, 1)) == CONST_INT
11155 && INTVAL (XEXP (op0, 1)) >= 0
11156 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
11157 && mode_width <= HOST_BITS_PER_WIDE_INT
11158 && (nonzero_bits (XEXP (op0, 0), mode)
11159 & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0
11160 && (((unsigned HOST_WIDE_INT) const_op
11161 + (GET_CODE (op0) != LSHIFTRT
11162 ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
11163 + 1)
11164 : 0))
11165 <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
11167 /* If the shift was logical, then we must make the condition
11168 unsigned. */
11169 if (GET_CODE (op0) == LSHIFTRT)
11170 code = unsigned_condition (code);
11172 const_op <<= INTVAL (XEXP (op0, 1));
11173 op1 = GEN_INT (const_op);
11174 op0 = XEXP (op0, 0);
11175 continue;
11178 /* If we are using this shift to extract just the sign bit, we
11179 can replace this with an LT or GE comparison. */
11180 if (const_op == 0
11181 && (equality_comparison_p || sign_bit_comparison_p)
11182 && GET_CODE (XEXP (op0, 1)) == CONST_INT
11183 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1))
11184 == mode_width - 1)
11186 op0 = XEXP (op0, 0);
11187 code = (code == NE || code == GT ? LT : GE);
11188 continue;
11190 break;
11192 default:
11193 break;
11196 break;
11199 /* Now make any compound operations involved in this comparison. Then,
11200 check for an outmost SUBREG on OP0 that is not doing anything or is
11201 paradoxical. The latter transformation must only be performed when
11202 it is known that the "extra" bits will be the same in op0 and op1 or
11203 that they don't matter. There are three cases to consider:
11205 1. SUBREG_REG (op0) is a register. In this case the bits are don't
11206 care bits and we can assume they have any convenient value. So
11207 making the transformation is safe.
11209 2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not defined.
11210 In this case the upper bits of op0 are undefined. We should not make
11211 the simplification in that case as we do not know the contents of
11212 those bits.
11214 3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is defined and not
11215 NIL. In that case we know those bits are zeros or ones. We must
11216 also be sure that they are the same as the upper bits of op1.
11218 We can never remove a SUBREG for a non-equality comparison because
11219 the sign bit is in a different place in the underlying object. */
11221 op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
11222 op1 = make_compound_operation (op1, SET);
11224 if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
11225 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
11226 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (op0))) == MODE_INT
11227 && (code == NE || code == EQ))
11229 if (GET_MODE_SIZE (GET_MODE (op0))
11230 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0))))
11232 /* For paradoxical subregs, allow case 1 as above. Case 3 isn't
11233 implemented. */
11234 if (GET_CODE (SUBREG_REG (op0)) == REG)
11236 op0 = SUBREG_REG (op0);
11237 op1 = gen_lowpart (GET_MODE (op0), op1);
11240 else if ((GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
11241 <= HOST_BITS_PER_WIDE_INT)
11242 && (nonzero_bits (SUBREG_REG (op0),
11243 GET_MODE (SUBREG_REG (op0)))
11244 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
11246 tem = gen_lowpart (GET_MODE (SUBREG_REG (op0)), op1);
11248 if ((nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
11249 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
11250 op0 = SUBREG_REG (op0), op1 = tem;
11254 /* We now do the opposite procedure: Some machines don't have compare
11255 insns in all modes. If OP0's mode is an integer mode smaller than a
11256 word and we can't do a compare in that mode, see if there is a larger
11257 mode for which we can do the compare. There are a number of cases in
11258 which we can use the wider mode. */
11260 mode = GET_MODE (op0);
11261 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
11262 && GET_MODE_SIZE (mode) < UNITS_PER_WORD
11263 && ! have_insn_for (COMPARE, mode))
11264 for (tmode = GET_MODE_WIDER_MODE (mode);
11265 (tmode != VOIDmode
11266 && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT);
11267 tmode = GET_MODE_WIDER_MODE (tmode))
11268 if (have_insn_for (COMPARE, tmode))
11270 int zero_extended;
11272 /* If the only nonzero bits in OP0 and OP1 are those in the
11273 narrower mode and this is an equality or unsigned comparison,
11274 we can use the wider mode. Similarly for sign-extended
11275 values, in which case it is true for all comparisons. */
11276 zero_extended = ((code == EQ || code == NE
11277 || code == GEU || code == GTU
11278 || code == LEU || code == LTU)
11279 && (nonzero_bits (op0, tmode)
11280 & ~GET_MODE_MASK (mode)) == 0
11281 && ((GET_CODE (op1) == CONST_INT
11282 || (nonzero_bits (op1, tmode)
11283 & ~GET_MODE_MASK (mode)) == 0)));
11285 if (zero_extended
11286 || ((num_sign_bit_copies (op0, tmode)
11287 > (unsigned int) (GET_MODE_BITSIZE (tmode)
11288 - GET_MODE_BITSIZE (mode)))
11289 && (num_sign_bit_copies (op1, tmode)
11290 > (unsigned int) (GET_MODE_BITSIZE (tmode)
11291 - GET_MODE_BITSIZE (mode)))))
11293 /* If OP0 is an AND and we don't have an AND in MODE either,
11294 make a new AND in the proper mode. */
11295 if (GET_CODE (op0) == AND
11296 && !have_insn_for (AND, mode))
11297 op0 = gen_binary (AND, tmode,
11298 gen_lowpart (tmode,
11299 XEXP (op0, 0)),
11300 gen_lowpart (tmode,
11301 XEXP (op0, 1)));
11303 op0 = gen_lowpart (tmode, op0);
11304 if (zero_extended && GET_CODE (op1) == CONST_INT)
11305 op1 = GEN_INT (INTVAL (op1) & GET_MODE_MASK (mode));
11306 op1 = gen_lowpart (tmode, op1);
11307 break;
11310 /* If this is a test for negative, we can make an explicit
11311 test of the sign bit. */
11313 if (op1 == const0_rtx && (code == LT || code == GE)
11314 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11316 op0 = gen_binary (AND, tmode,
11317 gen_lowpart (tmode, op0),
11318 GEN_INT ((HOST_WIDE_INT) 1
11319 << (GET_MODE_BITSIZE (mode) - 1)));
11320 code = (code == LT) ? NE : EQ;
11321 break;
11325 #ifdef CANONICALIZE_COMPARISON
11326 /* If this machine only supports a subset of valid comparisons, see if we
11327 can convert an unsupported one into a supported one. */
11328 CANONICALIZE_COMPARISON (code, op0, op1);
11329 #endif
11331 *pop0 = op0;
11332 *pop1 = op1;
11334 return code;
11337 /* Like jump.c' reversed_comparison_code, but use combine infrastructure for
11338 searching backward. */
11339 static enum rtx_code
11340 combine_reversed_comparison_code (rtx exp)
11342 enum rtx_code code1 = reversed_comparison_code (exp, NULL);
11343 rtx x;
11345 if (code1 != UNKNOWN
11346 || GET_MODE_CLASS (GET_MODE (XEXP (exp, 0))) != MODE_CC)
11347 return code1;
11348 /* Otherwise try and find where the condition codes were last set and
11349 use that. */
11350 x = get_last_value (XEXP (exp, 0));
11351 if (!x || GET_CODE (x) != COMPARE)
11352 return UNKNOWN;
11353 return reversed_comparison_code_parts (GET_CODE (exp),
11354 XEXP (x, 0), XEXP (x, 1), NULL);
11357 /* Return comparison with reversed code of EXP and operands OP0 and OP1.
11358 Return NULL_RTX in case we fail to do the reversal. */
11359 static rtx
11360 reversed_comparison (rtx exp, enum machine_mode mode, rtx op0, rtx op1)
11362 enum rtx_code reversed_code = combine_reversed_comparison_code (exp);
11363 if (reversed_code == UNKNOWN)
11364 return NULL_RTX;
11365 else
11366 return gen_binary (reversed_code, mode, op0, op1);
11369 /* Utility function for following routine. Called when X is part of a value
11370 being stored into reg_last_set_value. Sets reg_last_set_table_tick
11371 for each register mentioned. Similar to mention_regs in cse.c */
11373 static void
11374 update_table_tick (rtx x)
11376 enum rtx_code code = GET_CODE (x);
11377 const char *fmt = GET_RTX_FORMAT (code);
11378 int i;
11380 if (code == REG)
11382 unsigned int regno = REGNO (x);
11383 unsigned int endregno
11384 = regno + (regno < FIRST_PSEUDO_REGISTER
11385 ? hard_regno_nregs[regno][GET_MODE (x)] : 1);
11386 unsigned int r;
11388 for (r = regno; r < endregno; r++)
11389 reg_last_set_table_tick[r] = label_tick;
11391 return;
11394 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11395 /* Note that we can't have an "E" in values stored; see
11396 get_last_value_validate. */
11397 if (fmt[i] == 'e')
11399 /* Check for identical subexpressions. If x contains
11400 identical subexpression we only have to traverse one of
11401 them. */
11402 if (i == 0 && ARITHMETIC_P (x))
11404 /* Note that at this point x1 has already been
11405 processed. */
11406 rtx x0 = XEXP (x, 0);
11407 rtx x1 = XEXP (x, 1);
11409 /* If x0 and x1 are identical then there is no need to
11410 process x0. */
11411 if (x0 == x1)
11412 break;
11414 /* If x0 is identical to a subexpression of x1 then while
11415 processing x1, x0 has already been processed. Thus we
11416 are done with x. */
11417 if (ARITHMETIC_P (x1)
11418 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
11419 break;
11421 /* If x1 is identical to a subexpression of x0 then we
11422 still have to process the rest of x0. */
11423 if (ARITHMETIC_P (x0)
11424 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
11426 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
11427 break;
11431 update_table_tick (XEXP (x, i));
11435 /* Record that REG is set to VALUE in insn INSN. If VALUE is zero, we
11436 are saying that the register is clobbered and we no longer know its
11437 value. If INSN is zero, don't update reg_last_set; this is only permitted
11438 with VALUE also zero and is used to invalidate the register. */
11440 static void
11441 record_value_for_reg (rtx reg, rtx insn, rtx value)
11443 unsigned int regno = REGNO (reg);
11444 unsigned int endregno
11445 = regno + (regno < FIRST_PSEUDO_REGISTER
11446 ? hard_regno_nregs[regno][GET_MODE (reg)] : 1);
11447 unsigned int i;
11449 /* If VALUE contains REG and we have a previous value for REG, substitute
11450 the previous value. */
11451 if (value && insn && reg_overlap_mentioned_p (reg, value))
11453 rtx tem;
11455 /* Set things up so get_last_value is allowed to see anything set up to
11456 our insn. */
11457 subst_low_cuid = INSN_CUID (insn);
11458 tem = get_last_value (reg);
11460 /* If TEM is simply a binary operation with two CLOBBERs as operands,
11461 it isn't going to be useful and will take a lot of time to process,
11462 so just use the CLOBBER. */
11464 if (tem)
11466 if (ARITHMETIC_P (tem)
11467 && GET_CODE (XEXP (tem, 0)) == CLOBBER
11468 && GET_CODE (XEXP (tem, 1)) == CLOBBER)
11469 tem = XEXP (tem, 0);
11471 value = replace_rtx (copy_rtx (value), reg, tem);
11475 /* For each register modified, show we don't know its value, that
11476 we don't know about its bitwise content, that its value has been
11477 updated, and that we don't know the location of the death of the
11478 register. */
11479 for (i = regno; i < endregno; i++)
11481 if (insn)
11482 reg_last_set[i] = insn;
11484 reg_last_set_value[i] = 0;
11485 reg_last_set_mode[i] = 0;
11486 reg_last_set_nonzero_bits[i] = 0;
11487 reg_last_set_sign_bit_copies[i] = 0;
11488 reg_last_death[i] = 0;
11491 /* Mark registers that are being referenced in this value. */
11492 if (value)
11493 update_table_tick (value);
11495 /* Now update the status of each register being set.
11496 If someone is using this register in this block, set this register
11497 to invalid since we will get confused between the two lives in this
11498 basic block. This makes using this register always invalid. In cse, we
11499 scan the table to invalidate all entries using this register, but this
11500 is too much work for us. */
11502 for (i = regno; i < endregno; i++)
11504 reg_last_set_label[i] = label_tick;
11505 if (value && reg_last_set_table_tick[i] == label_tick)
11506 reg_last_set_invalid[i] = 1;
11507 else
11508 reg_last_set_invalid[i] = 0;
11511 /* The value being assigned might refer to X (like in "x++;"). In that
11512 case, we must replace it with (clobber (const_int 0)) to prevent
11513 infinite loops. */
11514 if (value && ! get_last_value_validate (&value, insn,
11515 reg_last_set_label[regno], 0))
11517 value = copy_rtx (value);
11518 if (! get_last_value_validate (&value, insn,
11519 reg_last_set_label[regno], 1))
11520 value = 0;
11523 /* For the main register being modified, update the value, the mode, the
11524 nonzero bits, and the number of sign bit copies. */
11526 reg_last_set_value[regno] = value;
11528 if (value)
11530 enum machine_mode mode = GET_MODE (reg);
11531 subst_low_cuid = INSN_CUID (insn);
11532 reg_last_set_mode[regno] = mode;
11533 if (GET_MODE_CLASS (mode) == MODE_INT
11534 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11535 mode = nonzero_bits_mode;
11536 reg_last_set_nonzero_bits[regno] = nonzero_bits (value, mode);
11537 reg_last_set_sign_bit_copies[regno]
11538 = num_sign_bit_copies (value, GET_MODE (reg));
11542 /* Called via note_stores from record_dead_and_set_regs to handle one
11543 SET or CLOBBER in an insn. DATA is the instruction in which the
11544 set is occurring. */
11546 static void
11547 record_dead_and_set_regs_1 (rtx dest, rtx setter, void *data)
11549 rtx record_dead_insn = (rtx) data;
11551 if (GET_CODE (dest) == SUBREG)
11552 dest = SUBREG_REG (dest);
11554 if (GET_CODE (dest) == REG)
11556 /* If we are setting the whole register, we know its value. Otherwise
11557 show that we don't know the value. We can handle SUBREG in
11558 some cases. */
11559 if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
11560 record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
11561 else if (GET_CODE (setter) == SET
11562 && GET_CODE (SET_DEST (setter)) == SUBREG
11563 && SUBREG_REG (SET_DEST (setter)) == dest
11564 && GET_MODE_BITSIZE (GET_MODE (dest)) <= BITS_PER_WORD
11565 && subreg_lowpart_p (SET_DEST (setter)))
11566 record_value_for_reg (dest, record_dead_insn,
11567 gen_lowpart (GET_MODE (dest),
11568 SET_SRC (setter)));
11569 else
11570 record_value_for_reg (dest, record_dead_insn, NULL_RTX);
11572 else if (GET_CODE (dest) == MEM
11573 /* Ignore pushes, they clobber nothing. */
11574 && ! push_operand (dest, GET_MODE (dest)))
11575 mem_last_set = INSN_CUID (record_dead_insn);
11578 /* Update the records of when each REG was most recently set or killed
11579 for the things done by INSN. This is the last thing done in processing
11580 INSN in the combiner loop.
11582 We update reg_last_set, reg_last_set_value, reg_last_set_mode,
11583 reg_last_set_nonzero_bits, reg_last_set_sign_bit_copies, reg_last_death,
11584 and also the similar information mem_last_set (which insn most recently
11585 modified memory) and last_call_cuid (which insn was the most recent
11586 subroutine call). */
11588 static void
11589 record_dead_and_set_regs (rtx insn)
11591 rtx link;
11592 unsigned int i;
11594 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
11596 if (REG_NOTE_KIND (link) == REG_DEAD
11597 && GET_CODE (XEXP (link, 0)) == REG)
11599 unsigned int regno = REGNO (XEXP (link, 0));
11600 unsigned int endregno
11601 = regno + (regno < FIRST_PSEUDO_REGISTER
11602 ? hard_regno_nregs[regno][GET_MODE (XEXP (link, 0))]
11603 : 1);
11605 for (i = regno; i < endregno; i++)
11606 reg_last_death[i] = insn;
11608 else if (REG_NOTE_KIND (link) == REG_INC)
11609 record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
11612 if (GET_CODE (insn) == CALL_INSN)
11614 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
11615 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
11617 reg_last_set_value[i] = 0;
11618 reg_last_set_mode[i] = 0;
11619 reg_last_set_nonzero_bits[i] = 0;
11620 reg_last_set_sign_bit_copies[i] = 0;
11621 reg_last_death[i] = 0;
11624 last_call_cuid = mem_last_set = INSN_CUID (insn);
11626 /* Don't bother recording what this insn does. It might set the
11627 return value register, but we can't combine into a call
11628 pattern anyway, so there's no point trying (and it may cause
11629 a crash, if e.g. we wind up asking for last_set_value of a
11630 SUBREG of the return value register). */
11631 return;
11634 note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
11637 /* If a SUBREG has the promoted bit set, it is in fact a property of the
11638 register present in the SUBREG, so for each such SUBREG go back and
11639 adjust nonzero and sign bit information of the registers that are
11640 known to have some zero/sign bits set.
11642 This is needed because when combine blows the SUBREGs away, the
11643 information on zero/sign bits is lost and further combines can be
11644 missed because of that. */
11646 static void
11647 record_promoted_value (rtx insn, rtx subreg)
11649 rtx links, set;
11650 unsigned int regno = REGNO (SUBREG_REG (subreg));
11651 enum machine_mode mode = GET_MODE (subreg);
11653 if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT)
11654 return;
11656 for (links = LOG_LINKS (insn); links;)
11658 insn = XEXP (links, 0);
11659 set = single_set (insn);
11661 if (! set || GET_CODE (SET_DEST (set)) != REG
11662 || REGNO (SET_DEST (set)) != regno
11663 || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
11665 links = XEXP (links, 1);
11666 continue;
11669 if (reg_last_set[regno] == insn)
11671 if (SUBREG_PROMOTED_UNSIGNED_P (subreg) > 0)
11672 reg_last_set_nonzero_bits[regno] &= GET_MODE_MASK (mode);
11675 if (GET_CODE (SET_SRC (set)) == REG)
11677 regno = REGNO (SET_SRC (set));
11678 links = LOG_LINKS (insn);
11680 else
11681 break;
11685 /* Scan X for promoted SUBREGs. For each one found,
11686 note what it implies to the registers used in it. */
11688 static void
11689 check_promoted_subreg (rtx insn, rtx x)
11691 if (GET_CODE (x) == SUBREG && SUBREG_PROMOTED_VAR_P (x)
11692 && GET_CODE (SUBREG_REG (x)) == REG)
11693 record_promoted_value (insn, x);
11694 else
11696 const char *format = GET_RTX_FORMAT (GET_CODE (x));
11697 int i, j;
11699 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
11700 switch (format[i])
11702 case 'e':
11703 check_promoted_subreg (insn, XEXP (x, i));
11704 break;
11705 case 'V':
11706 case 'E':
11707 if (XVEC (x, i) != 0)
11708 for (j = 0; j < XVECLEN (x, i); j++)
11709 check_promoted_subreg (insn, XVECEXP (x, i, j));
11710 break;
11715 /* Utility routine for the following function. Verify that all the registers
11716 mentioned in *LOC are valid when *LOC was part of a value set when
11717 label_tick == TICK. Return 0 if some are not.
11719 If REPLACE is nonzero, replace the invalid reference with
11720 (clobber (const_int 0)) and return 1. This replacement is useful because
11721 we often can get useful information about the form of a value (e.g., if
11722 it was produced by a shift that always produces -1 or 0) even though
11723 we don't know exactly what registers it was produced from. */
11725 static int
11726 get_last_value_validate (rtx *loc, rtx insn, int tick, int replace)
11728 rtx x = *loc;
11729 const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
11730 int len = GET_RTX_LENGTH (GET_CODE (x));
11731 int i;
11733 if (GET_CODE (x) == REG)
11735 unsigned int regno = REGNO (x);
11736 unsigned int endregno
11737 = regno + (regno < FIRST_PSEUDO_REGISTER
11738 ? hard_regno_nregs[regno][GET_MODE (x)] : 1);
11739 unsigned int j;
11741 for (j = regno; j < endregno; j++)
11742 if (reg_last_set_invalid[j]
11743 /* If this is a pseudo-register that was only set once and not
11744 live at the beginning of the function, it is always valid. */
11745 || (! (regno >= FIRST_PSEUDO_REGISTER
11746 && REG_N_SETS (regno) == 1
11747 && (! REGNO_REG_SET_P
11748 (ENTRY_BLOCK_PTR->next_bb->global_live_at_start, regno)))
11749 && reg_last_set_label[j] > tick))
11751 if (replace)
11752 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
11753 return replace;
11756 return 1;
11758 /* If this is a memory reference, make sure that there were
11759 no stores after it that might have clobbered the value. We don't
11760 have alias info, so we assume any store invalidates it. */
11761 else if (GET_CODE (x) == MEM && ! RTX_UNCHANGING_P (x)
11762 && INSN_CUID (insn) <= mem_last_set)
11764 if (replace)
11765 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
11766 return replace;
11769 for (i = 0; i < len; i++)
11771 if (fmt[i] == 'e')
11773 /* Check for identical subexpressions. If x contains
11774 identical subexpression we only have to traverse one of
11775 them. */
11776 if (i == 1 && ARITHMETIC_P (x))
11778 /* Note that at this point x0 has already been checked
11779 and found valid. */
11780 rtx x0 = XEXP (x, 0);
11781 rtx x1 = XEXP (x, 1);
11783 /* If x0 and x1 are identical then x is also valid. */
11784 if (x0 == x1)
11785 return 1;
11787 /* If x1 is identical to a subexpression of x0 then
11788 while checking x0, x1 has already been checked. Thus
11789 it is valid and so as x. */
11790 if (ARITHMETIC_P (x0)
11791 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
11792 return 1;
11794 /* If x0 is identical to a subexpression of x1 then x is
11795 valid iff the rest of x1 is valid. */
11796 if (ARITHMETIC_P (x1)
11797 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
11798 return
11799 get_last_value_validate (&XEXP (x1,
11800 x0 == XEXP (x1, 0) ? 1 : 0),
11801 insn, tick, replace);
11804 if (get_last_value_validate (&XEXP (x, i), insn, tick,
11805 replace) == 0)
11806 return 0;
11808 /* Don't bother with these. They shouldn't occur anyway. */
11809 else if (fmt[i] == 'E')
11810 return 0;
11813 /* If we haven't found a reason for it to be invalid, it is valid. */
11814 return 1;
11817 /* Get the last value assigned to X, if known. Some registers
11818 in the value may be replaced with (clobber (const_int 0)) if their value
11819 is known longer known reliably. */
11821 static rtx
11822 get_last_value (rtx x)
11824 unsigned int regno;
11825 rtx value;
11827 /* If this is a non-paradoxical SUBREG, get the value of its operand and
11828 then convert it to the desired mode. If this is a paradoxical SUBREG,
11829 we cannot predict what values the "extra" bits might have. */
11830 if (GET_CODE (x) == SUBREG
11831 && subreg_lowpart_p (x)
11832 && (GET_MODE_SIZE (GET_MODE (x))
11833 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
11834 && (value = get_last_value (SUBREG_REG (x))) != 0)
11835 return gen_lowpart (GET_MODE (x), value);
11837 if (GET_CODE (x) != REG)
11838 return 0;
11840 regno = REGNO (x);
11841 value = reg_last_set_value[regno];
11843 /* If we don't have a value, or if it isn't for this basic block and
11844 it's either a hard register, set more than once, or it's a live
11845 at the beginning of the function, return 0.
11847 Because if it's not live at the beginning of the function then the reg
11848 is always set before being used (is never used without being set).
11849 And, if it's set only once, and it's always set before use, then all
11850 uses must have the same last value, even if it's not from this basic
11851 block. */
11853 if (value == 0
11854 || (reg_last_set_label[regno] != label_tick
11855 && (regno < FIRST_PSEUDO_REGISTER
11856 || REG_N_SETS (regno) != 1
11857 || (REGNO_REG_SET_P
11858 (ENTRY_BLOCK_PTR->next_bb->global_live_at_start, regno)))))
11859 return 0;
11861 /* If the value was set in a later insn than the ones we are processing,
11862 we can't use it even if the register was only set once. */
11863 if (INSN_CUID (reg_last_set[regno]) >= subst_low_cuid)
11864 return 0;
11866 /* If the value has all its registers valid, return it. */
11867 if (get_last_value_validate (&value, reg_last_set[regno],
11868 reg_last_set_label[regno], 0))
11869 return value;
11871 /* Otherwise, make a copy and replace any invalid register with
11872 (clobber (const_int 0)). If that fails for some reason, return 0. */
11874 value = copy_rtx (value);
11875 if (get_last_value_validate (&value, reg_last_set[regno],
11876 reg_last_set_label[regno], 1))
11877 return value;
11879 return 0;
11882 /* Return nonzero if expression X refers to a REG or to memory
11883 that is set in an instruction more recent than FROM_CUID. */
11885 static int
11886 use_crosses_set_p (rtx x, int from_cuid)
11888 const char *fmt;
11889 int i;
11890 enum rtx_code code = GET_CODE (x);
11892 if (code == REG)
11894 unsigned int regno = REGNO (x);
11895 unsigned endreg = regno + (regno < FIRST_PSEUDO_REGISTER
11896 ? hard_regno_nregs[regno][GET_MODE (x)] : 1);
11898 #ifdef PUSH_ROUNDING
11899 /* Don't allow uses of the stack pointer to be moved,
11900 because we don't know whether the move crosses a push insn. */
11901 if (regno == STACK_POINTER_REGNUM && PUSH_ARGS)
11902 return 1;
11903 #endif
11904 for (; regno < endreg; regno++)
11905 if (reg_last_set[regno]
11906 && INSN_CUID (reg_last_set[regno]) > from_cuid)
11907 return 1;
11908 return 0;
11911 if (code == MEM && mem_last_set > from_cuid)
11912 return 1;
11914 fmt = GET_RTX_FORMAT (code);
11916 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11918 if (fmt[i] == 'E')
11920 int j;
11921 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
11922 if (use_crosses_set_p (XVECEXP (x, i, j), from_cuid))
11923 return 1;
11925 else if (fmt[i] == 'e'
11926 && use_crosses_set_p (XEXP (x, i), from_cuid))
11927 return 1;
11929 return 0;
11932 /* Define three variables used for communication between the following
11933 routines. */
11935 static unsigned int reg_dead_regno, reg_dead_endregno;
11936 static int reg_dead_flag;
11938 /* Function called via note_stores from reg_dead_at_p.
11940 If DEST is within [reg_dead_regno, reg_dead_endregno), set
11941 reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET. */
11943 static void
11944 reg_dead_at_p_1 (rtx dest, rtx x, void *data ATTRIBUTE_UNUSED)
11946 unsigned int regno, endregno;
11948 if (GET_CODE (dest) != REG)
11949 return;
11951 regno = REGNO (dest);
11952 endregno = regno + (regno < FIRST_PSEUDO_REGISTER
11953 ? hard_regno_nregs[regno][GET_MODE (dest)] : 1);
11955 if (reg_dead_endregno > regno && reg_dead_regno < endregno)
11956 reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
11959 /* Return nonzero if REG is known to be dead at INSN.
11961 We scan backwards from INSN. If we hit a REG_DEAD note or a CLOBBER
11962 referencing REG, it is dead. If we hit a SET referencing REG, it is
11963 live. Otherwise, see if it is live or dead at the start of the basic
11964 block we are in. Hard regs marked as being live in NEWPAT_USED_REGS
11965 must be assumed to be always live. */
11967 static int
11968 reg_dead_at_p (rtx reg, rtx insn)
11970 basic_block block;
11971 unsigned int i;
11973 /* Set variables for reg_dead_at_p_1. */
11974 reg_dead_regno = REGNO (reg);
11975 reg_dead_endregno = reg_dead_regno + (reg_dead_regno < FIRST_PSEUDO_REGISTER
11976 ? hard_regno_nregs[reg_dead_regno]
11977 [GET_MODE (reg)]
11978 : 1);
11980 reg_dead_flag = 0;
11982 /* Check that reg isn't mentioned in NEWPAT_USED_REGS. */
11983 if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
11985 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
11986 if (TEST_HARD_REG_BIT (newpat_used_regs, i))
11987 return 0;
11990 /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, label, or
11991 beginning of function. */
11992 for (; insn && GET_CODE (insn) != CODE_LABEL && GET_CODE (insn) != BARRIER;
11993 insn = prev_nonnote_insn (insn))
11995 note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
11996 if (reg_dead_flag)
11997 return reg_dead_flag == 1 ? 1 : 0;
11999 if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
12000 return 1;
12003 /* Get the basic block that we were in. */
12004 if (insn == 0)
12005 block = ENTRY_BLOCK_PTR->next_bb;
12006 else
12008 FOR_EACH_BB (block)
12009 if (insn == BB_HEAD (block))
12010 break;
12012 if (block == EXIT_BLOCK_PTR)
12013 return 0;
12016 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
12017 if (REGNO_REG_SET_P (block->global_live_at_start, i))
12018 return 0;
12020 return 1;
12023 /* Note hard registers in X that are used. This code is similar to
12024 that in flow.c, but much simpler since we don't care about pseudos. */
12026 static void
12027 mark_used_regs_combine (rtx x)
12029 RTX_CODE code = GET_CODE (x);
12030 unsigned int regno;
12031 int i;
12033 switch (code)
12035 case LABEL_REF:
12036 case SYMBOL_REF:
12037 case CONST_INT:
12038 case CONST:
12039 case CONST_DOUBLE:
12040 case CONST_VECTOR:
12041 case PC:
12042 case ADDR_VEC:
12043 case ADDR_DIFF_VEC:
12044 case ASM_INPUT:
12045 #ifdef HAVE_cc0
12046 /* CC0 must die in the insn after it is set, so we don't need to take
12047 special note of it here. */
12048 case CC0:
12049 #endif
12050 return;
12052 case CLOBBER:
12053 /* If we are clobbering a MEM, mark any hard registers inside the
12054 address as used. */
12055 if (GET_CODE (XEXP (x, 0)) == MEM)
12056 mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
12057 return;
12059 case REG:
12060 regno = REGNO (x);
12061 /* A hard reg in a wide mode may really be multiple registers.
12062 If so, mark all of them just like the first. */
12063 if (regno < FIRST_PSEUDO_REGISTER)
12065 unsigned int endregno, r;
12067 /* None of this applies to the stack, frame or arg pointers. */
12068 if (regno == STACK_POINTER_REGNUM
12069 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
12070 || regno == HARD_FRAME_POINTER_REGNUM
12071 #endif
12072 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
12073 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
12074 #endif
12075 || regno == FRAME_POINTER_REGNUM)
12076 return;
12078 endregno = regno + hard_regno_nregs[regno][GET_MODE (x)];
12079 for (r = regno; r < endregno; r++)
12080 SET_HARD_REG_BIT (newpat_used_regs, r);
12082 return;
12084 case SET:
12086 /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
12087 the address. */
12088 rtx testreg = SET_DEST (x);
12090 while (GET_CODE (testreg) == SUBREG
12091 || GET_CODE (testreg) == ZERO_EXTRACT
12092 || GET_CODE (testreg) == SIGN_EXTRACT
12093 || GET_CODE (testreg) == STRICT_LOW_PART)
12094 testreg = XEXP (testreg, 0);
12096 if (GET_CODE (testreg) == MEM)
12097 mark_used_regs_combine (XEXP (testreg, 0));
12099 mark_used_regs_combine (SET_SRC (x));
12101 return;
12103 default:
12104 break;
12107 /* Recursively scan the operands of this expression. */
12110 const char *fmt = GET_RTX_FORMAT (code);
12112 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12114 if (fmt[i] == 'e')
12115 mark_used_regs_combine (XEXP (x, i));
12116 else if (fmt[i] == 'E')
12118 int j;
12120 for (j = 0; j < XVECLEN (x, i); j++)
12121 mark_used_regs_combine (XVECEXP (x, i, j));
12127 /* Remove register number REGNO from the dead registers list of INSN.
12129 Return the note used to record the death, if there was one. */
12132 remove_death (unsigned int regno, rtx insn)
12134 rtx note = find_regno_note (insn, REG_DEAD, regno);
12136 if (note)
12138 REG_N_DEATHS (regno)--;
12139 remove_note (insn, note);
12142 return note;
12145 /* For each register (hardware or pseudo) used within expression X, if its
12146 death is in an instruction with cuid between FROM_CUID (inclusive) and
12147 TO_INSN (exclusive), put a REG_DEAD note for that register in the
12148 list headed by PNOTES.
12150 That said, don't move registers killed by maybe_kill_insn.
12152 This is done when X is being merged by combination into TO_INSN. These
12153 notes will then be distributed as needed. */
12155 static void
12156 move_deaths (rtx x, rtx maybe_kill_insn, int from_cuid, rtx to_insn,
12157 rtx *pnotes)
12159 const char *fmt;
12160 int len, i;
12161 enum rtx_code code = GET_CODE (x);
12163 if (code == REG)
12165 unsigned int regno = REGNO (x);
12166 rtx where_dead = reg_last_death[regno];
12167 rtx before_dead, after_dead;
12169 /* Don't move the register if it gets killed in between from and to. */
12170 if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
12171 && ! reg_referenced_p (x, maybe_kill_insn))
12172 return;
12174 /* WHERE_DEAD could be a USE insn made by combine, so first we
12175 make sure that we have insns with valid INSN_CUID values. */
12176 before_dead = where_dead;
12177 while (before_dead && INSN_UID (before_dead) > max_uid_cuid)
12178 before_dead = PREV_INSN (before_dead);
12180 after_dead = where_dead;
12181 while (after_dead && INSN_UID (after_dead) > max_uid_cuid)
12182 after_dead = NEXT_INSN (after_dead);
12184 if (before_dead && after_dead
12185 && INSN_CUID (before_dead) >= from_cuid
12186 && (INSN_CUID (after_dead) < INSN_CUID (to_insn)
12187 || (where_dead != after_dead
12188 && INSN_CUID (after_dead) == INSN_CUID (to_insn))))
12190 rtx note = remove_death (regno, where_dead);
12192 /* It is possible for the call above to return 0. This can occur
12193 when reg_last_death points to I2 or I1 that we combined with.
12194 In that case make a new note.
12196 We must also check for the case where X is a hard register
12197 and NOTE is a death note for a range of hard registers
12198 including X. In that case, we must put REG_DEAD notes for
12199 the remaining registers in place of NOTE. */
12201 if (note != 0 && regno < FIRST_PSEUDO_REGISTER
12202 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
12203 > GET_MODE_SIZE (GET_MODE (x))))
12205 unsigned int deadregno = REGNO (XEXP (note, 0));
12206 unsigned int deadend
12207 = (deadregno + hard_regno_nregs[deadregno]
12208 [GET_MODE (XEXP (note, 0))]);
12209 unsigned int ourend
12210 = regno + hard_regno_nregs[regno][GET_MODE (x)];
12211 unsigned int i;
12213 for (i = deadregno; i < deadend; i++)
12214 if (i < regno || i >= ourend)
12215 REG_NOTES (where_dead)
12216 = gen_rtx_EXPR_LIST (REG_DEAD,
12217 regno_reg_rtx[i],
12218 REG_NOTES (where_dead));
12221 /* If we didn't find any note, or if we found a REG_DEAD note that
12222 covers only part of the given reg, and we have a multi-reg hard
12223 register, then to be safe we must check for REG_DEAD notes
12224 for each register other than the first. They could have
12225 their own REG_DEAD notes lying around. */
12226 else if ((note == 0
12227 || (note != 0
12228 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
12229 < GET_MODE_SIZE (GET_MODE (x)))))
12230 && regno < FIRST_PSEUDO_REGISTER
12231 && hard_regno_nregs[regno][GET_MODE (x)] > 1)
12233 unsigned int ourend
12234 = regno + hard_regno_nregs[regno][GET_MODE (x)];
12235 unsigned int i, offset;
12236 rtx oldnotes = 0;
12238 if (note)
12239 offset = hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))];
12240 else
12241 offset = 1;
12243 for (i = regno + offset; i < ourend; i++)
12244 move_deaths (regno_reg_rtx[i],
12245 maybe_kill_insn, from_cuid, to_insn, &oldnotes);
12248 if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
12250 XEXP (note, 1) = *pnotes;
12251 *pnotes = note;
12253 else
12254 *pnotes = gen_rtx_EXPR_LIST (REG_DEAD, x, *pnotes);
12256 REG_N_DEATHS (regno)++;
12259 return;
12262 else if (GET_CODE (x) == SET)
12264 rtx dest = SET_DEST (x);
12266 move_deaths (SET_SRC (x), maybe_kill_insn, from_cuid, to_insn, pnotes);
12268 /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
12269 that accesses one word of a multi-word item, some
12270 piece of everything register in the expression is used by
12271 this insn, so remove any old death. */
12272 /* ??? So why do we test for equality of the sizes? */
12274 if (GET_CODE (dest) == ZERO_EXTRACT
12275 || GET_CODE (dest) == STRICT_LOW_PART
12276 || (GET_CODE (dest) == SUBREG
12277 && (((GET_MODE_SIZE (GET_MODE (dest))
12278 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
12279 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
12280 + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
12282 move_deaths (dest, maybe_kill_insn, from_cuid, to_insn, pnotes);
12283 return;
12286 /* If this is some other SUBREG, we know it replaces the entire
12287 value, so use that as the destination. */
12288 if (GET_CODE (dest) == SUBREG)
12289 dest = SUBREG_REG (dest);
12291 /* If this is a MEM, adjust deaths of anything used in the address.
12292 For a REG (the only other possibility), the entire value is
12293 being replaced so the old value is not used in this insn. */
12295 if (GET_CODE (dest) == MEM)
12296 move_deaths (XEXP (dest, 0), maybe_kill_insn, from_cuid,
12297 to_insn, pnotes);
12298 return;
12301 else if (GET_CODE (x) == CLOBBER)
12302 return;
12304 len = GET_RTX_LENGTH (code);
12305 fmt = GET_RTX_FORMAT (code);
12307 for (i = 0; i < len; i++)
12309 if (fmt[i] == 'E')
12311 int j;
12312 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
12313 move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_cuid,
12314 to_insn, pnotes);
12316 else if (fmt[i] == 'e')
12317 move_deaths (XEXP (x, i), maybe_kill_insn, from_cuid, to_insn, pnotes);
12321 /* Return 1 if X is the target of a bit-field assignment in BODY, the
12322 pattern of an insn. X must be a REG. */
12324 static int
12325 reg_bitfield_target_p (rtx x, rtx body)
12327 int i;
12329 if (GET_CODE (body) == SET)
12331 rtx dest = SET_DEST (body);
12332 rtx target;
12333 unsigned int regno, tregno, endregno, endtregno;
12335 if (GET_CODE (dest) == ZERO_EXTRACT)
12336 target = XEXP (dest, 0);
12337 else if (GET_CODE (dest) == STRICT_LOW_PART)
12338 target = SUBREG_REG (XEXP (dest, 0));
12339 else
12340 return 0;
12342 if (GET_CODE (target) == SUBREG)
12343 target = SUBREG_REG (target);
12345 if (GET_CODE (target) != REG)
12346 return 0;
12348 tregno = REGNO (target), regno = REGNO (x);
12349 if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
12350 return target == x;
12352 endtregno = tregno + hard_regno_nregs[tregno][GET_MODE (target)];
12353 endregno = regno + hard_regno_nregs[regno][GET_MODE (x)];
12355 return endregno > tregno && regno < endtregno;
12358 else if (GET_CODE (body) == PARALLEL)
12359 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
12360 if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
12361 return 1;
12363 return 0;
12366 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
12367 as appropriate. I3 and I2 are the insns resulting from the combination
12368 insns including FROM (I2 may be zero).
12370 Each note in the list is either ignored or placed on some insns, depending
12371 on the type of note. */
12373 static void
12374 distribute_notes (rtx notes, rtx from_insn, rtx i3, rtx i2)
12376 rtx note, next_note;
12377 rtx tem;
12379 for (note = notes; note; note = next_note)
12381 rtx place = 0, place2 = 0;
12383 /* If this NOTE references a pseudo register, ensure it references
12384 the latest copy of that register. */
12385 if (XEXP (note, 0) && GET_CODE (XEXP (note, 0)) == REG
12386 && REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER)
12387 XEXP (note, 0) = regno_reg_rtx[REGNO (XEXP (note, 0))];
12389 next_note = XEXP (note, 1);
12390 switch (REG_NOTE_KIND (note))
12392 case REG_BR_PROB:
12393 case REG_BR_PRED:
12394 /* Doesn't matter much where we put this, as long as it's somewhere.
12395 It is preferable to keep these notes on branches, which is most
12396 likely to be i3. */
12397 place = i3;
12398 break;
12400 case REG_VALUE_PROFILE:
12401 /* Just get rid of this note, as it is unused later anyway. */
12402 break;
12404 case REG_VTABLE_REF:
12405 /* ??? Should remain with *a particular* memory load. Given the
12406 nature of vtable data, the last insn seems relatively safe. */
12407 place = i3;
12408 break;
12410 case REG_NON_LOCAL_GOTO:
12411 if (GET_CODE (i3) == JUMP_INSN)
12412 place = i3;
12413 else if (i2 && GET_CODE (i2) == JUMP_INSN)
12414 place = i2;
12415 else
12416 abort ();
12417 break;
12419 case REG_EH_REGION:
12420 /* These notes must remain with the call or trapping instruction. */
12421 if (GET_CODE (i3) == CALL_INSN)
12422 place = i3;
12423 else if (i2 && GET_CODE (i2) == CALL_INSN)
12424 place = i2;
12425 else if (flag_non_call_exceptions)
12427 if (may_trap_p (i3))
12428 place = i3;
12429 else if (i2 && may_trap_p (i2))
12430 place = i2;
12431 /* ??? Otherwise assume we've combined things such that we
12432 can now prove that the instructions can't trap. Drop the
12433 note in this case. */
12435 else
12436 abort ();
12437 break;
12439 case REG_ALWAYS_RETURN:
12440 case REG_NORETURN:
12441 case REG_SETJMP:
12442 /* These notes must remain with the call. It should not be
12443 possible for both I2 and I3 to be a call. */
12444 if (GET_CODE (i3) == CALL_INSN)
12445 place = i3;
12446 else if (i2 && GET_CODE (i2) == CALL_INSN)
12447 place = i2;
12448 else
12449 abort ();
12450 break;
12452 case REG_UNUSED:
12453 /* Any clobbers for i3 may still exist, and so we must process
12454 REG_UNUSED notes from that insn.
12456 Any clobbers from i2 or i1 can only exist if they were added by
12457 recog_for_combine. In that case, recog_for_combine created the
12458 necessary REG_UNUSED notes. Trying to keep any original
12459 REG_UNUSED notes from these insns can cause incorrect output
12460 if it is for the same register as the original i3 dest.
12461 In that case, we will notice that the register is set in i3,
12462 and then add a REG_UNUSED note for the destination of i3, which
12463 is wrong. However, it is possible to have REG_UNUSED notes from
12464 i2 or i1 for register which were both used and clobbered, so
12465 we keep notes from i2 or i1 if they will turn into REG_DEAD
12466 notes. */
12468 /* If this register is set or clobbered in I3, put the note there
12469 unless there is one already. */
12470 if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
12472 if (from_insn != i3)
12473 break;
12475 if (! (GET_CODE (XEXP (note, 0)) == REG
12476 ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
12477 : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
12478 place = i3;
12480 /* Otherwise, if this register is used by I3, then this register
12481 now dies here, so we must put a REG_DEAD note here unless there
12482 is one already. */
12483 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
12484 && ! (GET_CODE (XEXP (note, 0)) == REG
12485 ? find_regno_note (i3, REG_DEAD,
12486 REGNO (XEXP (note, 0)))
12487 : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
12489 PUT_REG_NOTE_KIND (note, REG_DEAD);
12490 place = i3;
12492 break;
12494 case REG_EQUAL:
12495 case REG_EQUIV:
12496 case REG_NOALIAS:
12497 /* These notes say something about results of an insn. We can
12498 only support them if they used to be on I3 in which case they
12499 remain on I3. Otherwise they are ignored.
12501 If the note refers to an expression that is not a constant, we
12502 must also ignore the note since we cannot tell whether the
12503 equivalence is still true. It might be possible to do
12504 slightly better than this (we only have a problem if I2DEST
12505 or I1DEST is present in the expression), but it doesn't
12506 seem worth the trouble. */
12508 if (from_insn == i3
12509 && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
12510 place = i3;
12511 break;
12513 case REG_INC:
12514 case REG_NO_CONFLICT:
12515 /* These notes say something about how a register is used. They must
12516 be present on any use of the register in I2 or I3. */
12517 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
12518 place = i3;
12520 if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
12522 if (place)
12523 place2 = i2;
12524 else
12525 place = i2;
12527 break;
12529 case REG_LABEL:
12530 /* This can show up in several ways -- either directly in the
12531 pattern, or hidden off in the constant pool with (or without?)
12532 a REG_EQUAL note. */
12533 /* ??? Ignore the without-reg_equal-note problem for now. */
12534 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
12535 || ((tem = find_reg_note (i3, REG_EQUAL, NULL_RTX))
12536 && GET_CODE (XEXP (tem, 0)) == LABEL_REF
12537 && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0)))
12538 place = i3;
12540 if (i2
12541 && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
12542 || ((tem = find_reg_note (i2, REG_EQUAL, NULL_RTX))
12543 && GET_CODE (XEXP (tem, 0)) == LABEL_REF
12544 && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0))))
12546 if (place)
12547 place2 = i2;
12548 else
12549 place = i2;
12552 /* Don't attach REG_LABEL note to a JUMP_INSN which has
12553 JUMP_LABEL already. Instead, decrement LABEL_NUSES. */
12554 if (place && GET_CODE (place) == JUMP_INSN && JUMP_LABEL (place))
12556 if (JUMP_LABEL (place) != XEXP (note, 0))
12557 abort ();
12558 if (GET_CODE (JUMP_LABEL (place)) == CODE_LABEL)
12559 LABEL_NUSES (JUMP_LABEL (place))--;
12560 place = 0;
12562 if (place2 && GET_CODE (place2) == JUMP_INSN && JUMP_LABEL (place2))
12564 if (JUMP_LABEL (place2) != XEXP (note, 0))
12565 abort ();
12566 if (GET_CODE (JUMP_LABEL (place2)) == CODE_LABEL)
12567 LABEL_NUSES (JUMP_LABEL (place2))--;
12568 place2 = 0;
12570 break;
12572 case REG_NONNEG:
12573 /* This note says something about the value of a register prior
12574 to the execution of an insn. It is too much trouble to see
12575 if the note is still correct in all situations. It is better
12576 to simply delete it. */
12577 break;
12579 case REG_RETVAL:
12580 /* If the insn previously containing this note still exists,
12581 put it back where it was. Otherwise move it to the previous
12582 insn. Adjust the corresponding REG_LIBCALL note. */
12583 if (GET_CODE (from_insn) != NOTE)
12584 place = from_insn;
12585 else
12587 tem = find_reg_note (XEXP (note, 0), REG_LIBCALL, NULL_RTX);
12588 place = prev_real_insn (from_insn);
12589 if (tem && place)
12590 XEXP (tem, 0) = place;
12591 /* If we're deleting the last remaining instruction of a
12592 libcall sequence, don't add the notes. */
12593 else if (XEXP (note, 0) == from_insn)
12594 tem = place = 0;
12596 break;
12598 case REG_LIBCALL:
12599 /* This is handled similarly to REG_RETVAL. */
12600 if (GET_CODE (from_insn) != NOTE)
12601 place = from_insn;
12602 else
12604 tem = find_reg_note (XEXP (note, 0), REG_RETVAL, NULL_RTX);
12605 place = next_real_insn (from_insn);
12606 if (tem && place)
12607 XEXP (tem, 0) = place;
12608 /* If we're deleting the last remaining instruction of a
12609 libcall sequence, don't add the notes. */
12610 else if (XEXP (note, 0) == from_insn)
12611 tem = place = 0;
12613 break;
12615 case REG_DEAD:
12616 /* If the register is used as an input in I3, it dies there.
12617 Similarly for I2, if it is nonzero and adjacent to I3.
12619 If the register is not used as an input in either I3 or I2
12620 and it is not one of the registers we were supposed to eliminate,
12621 there are two possibilities. We might have a non-adjacent I2
12622 or we might have somehow eliminated an additional register
12623 from a computation. For example, we might have had A & B where
12624 we discover that B will always be zero. In this case we will
12625 eliminate the reference to A.
12627 In both cases, we must search to see if we can find a previous
12628 use of A and put the death note there. */
12630 if (from_insn
12631 && GET_CODE (from_insn) == CALL_INSN
12632 && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
12633 place = from_insn;
12634 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
12635 place = i3;
12636 else if (i2 != 0 && next_nonnote_insn (i2) == i3
12637 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
12638 place = i2;
12640 if (place == 0)
12642 basic_block bb = this_basic_block;
12644 for (tem = PREV_INSN (i3); place == 0; tem = PREV_INSN (tem))
12646 if (! INSN_P (tem))
12648 if (tem == BB_HEAD (bb))
12649 break;
12650 continue;
12653 /* If the register is being set at TEM, see if that is all
12654 TEM is doing. If so, delete TEM. Otherwise, make this
12655 into a REG_UNUSED note instead. */
12656 if (reg_set_p (XEXP (note, 0), PATTERN (tem)))
12658 rtx set = single_set (tem);
12659 rtx inner_dest = 0;
12660 #ifdef HAVE_cc0
12661 rtx cc0_setter = NULL_RTX;
12662 #endif
12664 if (set != 0)
12665 for (inner_dest = SET_DEST (set);
12666 (GET_CODE (inner_dest) == STRICT_LOW_PART
12667 || GET_CODE (inner_dest) == SUBREG
12668 || GET_CODE (inner_dest) == ZERO_EXTRACT);
12669 inner_dest = XEXP (inner_dest, 0))
12672 /* Verify that it was the set, and not a clobber that
12673 modified the register.
12675 CC0 targets must be careful to maintain setter/user
12676 pairs. If we cannot delete the setter due to side
12677 effects, mark the user with an UNUSED note instead
12678 of deleting it. */
12680 if (set != 0 && ! side_effects_p (SET_SRC (set))
12681 && rtx_equal_p (XEXP (note, 0), inner_dest)
12682 #ifdef HAVE_cc0
12683 && (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
12684 || ((cc0_setter = prev_cc0_setter (tem)) != NULL
12685 && sets_cc0_p (PATTERN (cc0_setter)) > 0))
12686 #endif
12689 /* Move the notes and links of TEM elsewhere.
12690 This might delete other dead insns recursively.
12691 First set the pattern to something that won't use
12692 any register. */
12693 rtx old_notes = REG_NOTES (tem);
12695 PATTERN (tem) = pc_rtx;
12696 REG_NOTES (tem) = NULL;
12698 distribute_notes (old_notes, tem, tem, NULL_RTX);
12699 distribute_links (LOG_LINKS (tem));
12701 PUT_CODE (tem, NOTE);
12702 NOTE_LINE_NUMBER (tem) = NOTE_INSN_DELETED;
12703 NOTE_SOURCE_FILE (tem) = 0;
12705 #ifdef HAVE_cc0
12706 /* Delete the setter too. */
12707 if (cc0_setter)
12709 PATTERN (cc0_setter) = pc_rtx;
12710 old_notes = REG_NOTES (cc0_setter);
12711 REG_NOTES (cc0_setter) = NULL;
12713 distribute_notes (old_notes, cc0_setter,
12714 cc0_setter, NULL_RTX);
12715 distribute_links (LOG_LINKS (cc0_setter));
12717 PUT_CODE (cc0_setter, NOTE);
12718 NOTE_LINE_NUMBER (cc0_setter)
12719 = NOTE_INSN_DELETED;
12720 NOTE_SOURCE_FILE (cc0_setter) = 0;
12722 #endif
12724 else
12726 PUT_REG_NOTE_KIND (note, REG_UNUSED);
12728 /* If there isn't already a REG_UNUSED note, put one
12729 here. Do not place a REG_DEAD note, even if
12730 the register is also used here; that would not
12731 match the algorithm used in lifetime analysis
12732 and can cause the consistency check in the
12733 scheduler to fail. */
12734 if (! find_regno_note (tem, REG_UNUSED,
12735 REGNO (XEXP (note, 0))))
12736 place = tem;
12737 break;
12740 else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem))
12741 || (GET_CODE (tem) == CALL_INSN
12742 && find_reg_fusage (tem, USE, XEXP (note, 0))))
12744 place = tem;
12746 /* If we are doing a 3->2 combination, and we have a
12747 register which formerly died in i3 and was not used
12748 by i2, which now no longer dies in i3 and is used in
12749 i2 but does not die in i2, and place is between i2
12750 and i3, then we may need to move a link from place to
12751 i2. */
12752 if (i2 && INSN_UID (place) <= max_uid_cuid
12753 && INSN_CUID (place) > INSN_CUID (i2)
12754 && from_insn
12755 && INSN_CUID (from_insn) > INSN_CUID (i2)
12756 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
12758 rtx links = LOG_LINKS (place);
12759 LOG_LINKS (place) = 0;
12760 distribute_links (links);
12762 break;
12765 if (tem == BB_HEAD (bb))
12766 break;
12769 /* We haven't found an insn for the death note and it
12770 is still a REG_DEAD note, but we have hit the beginning
12771 of the block. If the existing life info says the reg
12772 was dead, there's nothing left to do. Otherwise, we'll
12773 need to do a global life update after combine. */
12774 if (REG_NOTE_KIND (note) == REG_DEAD && place == 0
12775 && REGNO_REG_SET_P (bb->global_live_at_start,
12776 REGNO (XEXP (note, 0))))
12777 SET_BIT (refresh_blocks, this_basic_block->index);
12780 /* If the register is set or already dead at PLACE, we needn't do
12781 anything with this note if it is still a REG_DEAD note.
12782 We can here if it is set at all, not if is it totally replace,
12783 which is what `dead_or_set_p' checks, so also check for it being
12784 set partially. */
12786 if (place && REG_NOTE_KIND (note) == REG_DEAD)
12788 unsigned int regno = REGNO (XEXP (note, 0));
12790 /* Similarly, if the instruction on which we want to place
12791 the note is a noop, we'll need do a global live update
12792 after we remove them in delete_noop_moves. */
12793 if (noop_move_p (place))
12794 SET_BIT (refresh_blocks, this_basic_block->index);
12796 if (dead_or_set_p (place, XEXP (note, 0))
12797 || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
12799 /* Unless the register previously died in PLACE, clear
12800 reg_last_death. [I no longer understand why this is
12801 being done.] */
12802 if (reg_last_death[regno] != place)
12803 reg_last_death[regno] = 0;
12804 place = 0;
12806 else
12807 reg_last_death[regno] = place;
12809 /* If this is a death note for a hard reg that is occupying
12810 multiple registers, ensure that we are still using all
12811 parts of the object. If we find a piece of the object
12812 that is unused, we must arrange for an appropriate REG_DEAD
12813 note to be added for it. However, we can't just emit a USE
12814 and tag the note to it, since the register might actually
12815 be dead; so we recourse, and the recursive call then finds
12816 the previous insn that used this register. */
12818 if (place && regno < FIRST_PSEUDO_REGISTER
12819 && hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))] > 1)
12821 unsigned int endregno
12822 = regno + hard_regno_nregs[regno]
12823 [GET_MODE (XEXP (note, 0))];
12824 int all_used = 1;
12825 unsigned int i;
12827 for (i = regno; i < endregno; i++)
12828 if ((! refers_to_regno_p (i, i + 1, PATTERN (place), 0)
12829 && ! find_regno_fusage (place, USE, i))
12830 || dead_or_set_regno_p (place, i))
12831 all_used = 0;
12833 if (! all_used)
12835 /* Put only REG_DEAD notes for pieces that are
12836 not already dead or set. */
12838 for (i = regno; i < endregno;
12839 i += hard_regno_nregs[i][reg_raw_mode[i]])
12841 rtx piece = regno_reg_rtx[i];
12842 basic_block bb = this_basic_block;
12844 if (! dead_or_set_p (place, piece)
12845 && ! reg_bitfield_target_p (piece,
12846 PATTERN (place)))
12848 rtx new_note
12849 = gen_rtx_EXPR_LIST (REG_DEAD, piece, NULL_RTX);
12851 distribute_notes (new_note, place, place,
12852 NULL_RTX);
12854 else if (! refers_to_regno_p (i, i + 1,
12855 PATTERN (place), 0)
12856 && ! find_regno_fusage (place, USE, i))
12857 for (tem = PREV_INSN (place); ;
12858 tem = PREV_INSN (tem))
12860 if (! INSN_P (tem))
12862 if (tem == BB_HEAD (bb))
12864 SET_BIT (refresh_blocks,
12865 this_basic_block->index);
12866 break;
12868 continue;
12870 if (dead_or_set_p (tem, piece)
12871 || reg_bitfield_target_p (piece,
12872 PATTERN (tem)))
12874 REG_NOTES (tem)
12875 = gen_rtx_EXPR_LIST (REG_UNUSED, piece,
12876 REG_NOTES (tem));
12877 break;
12883 place = 0;
12887 break;
12889 default:
12890 /* Any other notes should not be present at this point in the
12891 compilation. */
12892 abort ();
12895 if (place)
12897 XEXP (note, 1) = REG_NOTES (place);
12898 REG_NOTES (place) = note;
12900 else if ((REG_NOTE_KIND (note) == REG_DEAD
12901 || REG_NOTE_KIND (note) == REG_UNUSED)
12902 && GET_CODE (XEXP (note, 0)) == REG)
12903 REG_N_DEATHS (REGNO (XEXP (note, 0)))--;
12905 if (place2)
12907 if ((REG_NOTE_KIND (note) == REG_DEAD
12908 || REG_NOTE_KIND (note) == REG_UNUSED)
12909 && GET_CODE (XEXP (note, 0)) == REG)
12910 REG_N_DEATHS (REGNO (XEXP (note, 0)))++;
12912 REG_NOTES (place2) = gen_rtx_fmt_ee (GET_CODE (note),
12913 REG_NOTE_KIND (note),
12914 XEXP (note, 0),
12915 REG_NOTES (place2));
12920 /* Similarly to above, distribute the LOG_LINKS that used to be present on
12921 I3, I2, and I1 to new locations. This is also called to add a link
12922 pointing at I3 when I3's destination is changed. */
12924 static void
12925 distribute_links (rtx links)
12927 rtx link, next_link;
12929 for (link = links; link; link = next_link)
12931 rtx place = 0;
12932 rtx insn;
12933 rtx set, reg;
12935 next_link = XEXP (link, 1);
12937 /* If the insn that this link points to is a NOTE or isn't a single
12938 set, ignore it. In the latter case, it isn't clear what we
12939 can do other than ignore the link, since we can't tell which
12940 register it was for. Such links wouldn't be used by combine
12941 anyway.
12943 It is not possible for the destination of the target of the link to
12944 have been changed by combine. The only potential of this is if we
12945 replace I3, I2, and I1 by I3 and I2. But in that case the
12946 destination of I2 also remains unchanged. */
12948 if (GET_CODE (XEXP (link, 0)) == NOTE
12949 || (set = single_set (XEXP (link, 0))) == 0)
12950 continue;
12952 reg = SET_DEST (set);
12953 while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
12954 || GET_CODE (reg) == SIGN_EXTRACT
12955 || GET_CODE (reg) == STRICT_LOW_PART)
12956 reg = XEXP (reg, 0);
12958 /* A LOG_LINK is defined as being placed on the first insn that uses
12959 a register and points to the insn that sets the register. Start
12960 searching at the next insn after the target of the link and stop
12961 when we reach a set of the register or the end of the basic block.
12963 Note that this correctly handles the link that used to point from
12964 I3 to I2. Also note that not much searching is typically done here
12965 since most links don't point very far away. */
12967 for (insn = NEXT_INSN (XEXP (link, 0));
12968 (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
12969 || BB_HEAD (this_basic_block->next_bb) != insn));
12970 insn = NEXT_INSN (insn))
12971 if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
12973 if (reg_referenced_p (reg, PATTERN (insn)))
12974 place = insn;
12975 break;
12977 else if (GET_CODE (insn) == CALL_INSN
12978 && find_reg_fusage (insn, USE, reg))
12980 place = insn;
12981 break;
12983 else if (INSN_P (insn) && reg_set_p (reg, insn))
12984 break;
12986 /* If we found a place to put the link, place it there unless there
12987 is already a link to the same insn as LINK at that point. */
12989 if (place)
12991 rtx link2;
12993 for (link2 = LOG_LINKS (place); link2; link2 = XEXP (link2, 1))
12994 if (XEXP (link2, 0) == XEXP (link, 0))
12995 break;
12997 if (link2 == 0)
12999 XEXP (link, 1) = LOG_LINKS (place);
13000 LOG_LINKS (place) = link;
13002 /* Set added_links_insn to the earliest insn we added a
13003 link to. */
13004 if (added_links_insn == 0
13005 || INSN_CUID (added_links_insn) > INSN_CUID (place))
13006 added_links_insn = place;
13012 /* Subroutine of unmentioned_reg_p and callback from for_each_rtx.
13013 Check whether the expression pointer to by LOC is a register or
13014 memory, and if so return 1 if it isn't mentioned in the rtx EXPR.
13015 Otherwise return zero. */
13017 static int
13018 unmentioned_reg_p_1 (rtx *loc, void *expr)
13020 rtx x = *loc;
13022 if (x != NULL_RTX
13023 && (GET_CODE (x) == REG || GET_CODE (x) == MEM)
13024 && ! reg_mentioned_p (x, (rtx) expr))
13025 return 1;
13026 return 0;
13029 /* Check for any register or memory mentioned in EQUIV that is not
13030 mentioned in EXPR. This is used to restrict EQUIV to "specializations"
13031 of EXPR where some registers may have been replaced by constants. */
13033 static bool
13034 unmentioned_reg_p (rtx equiv, rtx expr)
13036 return for_each_rtx (&equiv, unmentioned_reg_p_1, expr);
13039 /* Compute INSN_CUID for INSN, which is an insn made by combine. */
13041 static int
13042 insn_cuid (rtx insn)
13044 while (insn != 0 && INSN_UID (insn) > max_uid_cuid
13045 && GET_CODE (insn) == INSN && GET_CODE (PATTERN (insn)) == USE)
13046 insn = NEXT_INSN (insn);
13048 if (INSN_UID (insn) > max_uid_cuid)
13049 abort ();
13051 return INSN_CUID (insn);
13054 void
13055 dump_combine_stats (FILE *file)
13057 fnotice
13058 (file,
13059 ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
13060 combine_attempts, combine_merges, combine_extras, combine_successes);
13063 void
13064 dump_combine_total_stats (FILE *file)
13066 fnotice
13067 (file,
13068 "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
13069 total_attempts, total_merges, total_extras, total_successes);