Merged with mainline at revision 126347.
[official-gcc.git] / gcc / combine.c
blob01badc35719ba91138df99ba64c34e004cc8642e
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, 2005, 2006, 2007
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
23 /* This module is essentially the "combiner" phase of the U. of Arizona
24 Portable Optimizer, but redone to work on our list-structured
25 representation for RTL instead of their string representation.
27 The LOG_LINKS of each insn identify the most recent assignment
28 to each REG used in the insn. It is a list of previous insns,
29 each of which contains a SET for a REG that is used in this insn
30 and not used or set in between. LOG_LINKs never cross basic blocks.
31 They were set up by the preceding pass (lifetime analysis).
33 We try to combine each pair of insns joined by a logical link.
34 We also try to combine triples of insns A, B and C when
35 C has a link back to B and B has a link back to A.
37 LOG_LINKS does not have links for use of the CC0. They don't
38 need to, because the insn that sets the CC0 is always immediately
39 before the insn that tests it. So we always regard a branch
40 insn as having a logical link to the preceding insn. The same is true
41 for an insn explicitly using CC0.
43 We check (with use_crosses_set_p) to avoid combining in such a way
44 as to move a computation to a place where its value would be different.
46 Combination is done by mathematically substituting the previous
47 insn(s) values for the regs they set into the expressions in
48 the later insns that refer to these regs. If the result is a valid insn
49 for our target machine, according to the machine description,
50 we install it, delete the earlier insns, and update the data flow
51 information (LOG_LINKS and REG_NOTES) for what we did.
53 There are a few exceptions where the dataflow information isn't
54 completely updated (however this is only a local issue since it is
55 regenerated before the next pass that uses it):
57 - reg_live_length is not updated
58 - reg_n_refs is not adjusted in the rare case when a register is
59 no longer required in a computation
60 - there are extremely rare cases (see distribute_notes) when a
61 REG_DEAD note is lost
62 - a LOG_LINKS entry that refers to an insn with multiple SETs may be
63 removed because there is no way to know which register it was
64 linking
66 To simplify substitution, we combine only when the earlier insn(s)
67 consist of only a single assignment. To simplify updating afterward,
68 we never combine when a subroutine call appears in the middle.
70 Since we do not represent assignments to CC0 explicitly except when that
71 is all an insn does, there is no LOG_LINKS entry in an insn that uses
72 the condition code for the insn that set the condition code.
73 Fortunately, these two insns must be consecutive.
74 Therefore, every JUMP_INSN is taken to have an implicit logical link
75 to the preceding insn. This is not quite right, since non-jumps can
76 also use the condition code; but in practice such insns would not
77 combine anyway. */
79 #include "config.h"
80 #include "system.h"
81 #include "coretypes.h"
82 #include "tm.h"
83 #include "rtl.h"
84 #include "tree.h"
85 #include "tm_p.h"
86 #include "flags.h"
87 #include "regs.h"
88 #include "hard-reg-set.h"
89 #include "basic-block.h"
90 #include "insn-config.h"
91 #include "function.h"
92 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
93 #include "expr.h"
94 #include "insn-attr.h"
95 #include "recog.h"
96 #include "real.h"
97 #include "toplev.h"
98 #include "target.h"
99 #include "optabs.h"
100 #include "insn-codes.h"
101 #include "rtlhooks-def.h"
102 /* Include output.h for dump_file. */
103 #include "output.h"
104 #include "params.h"
105 #include "timevar.h"
106 #include "tree-pass.h"
107 #include "df.h"
109 /* Number of attempts to combine instructions in this function. */
111 static int combine_attempts;
113 /* Number of attempts that got as far as substitution in this function. */
115 static int combine_merges;
117 /* Number of instructions combined with added SETs in this function. */
119 static int combine_extras;
121 /* Number of instructions combined in this function. */
123 static int combine_successes;
125 /* Totals over entire compilation. */
127 static int total_attempts, total_merges, total_extras, total_successes;
129 /* combine_instructions may try to replace the right hand side of the
130 second instruction with the value of an associated REG_EQUAL note
131 before throwing it at try_combine. That is problematic when there
132 is a REG_DEAD note for a register used in the old right hand side
133 and can cause distribute_notes to do wrong things. This is the
134 second instruction if it has been so modified, null otherwise. */
136 static rtx i2mod;
138 /* When I2MOD is nonnull, this is a copy of the old right hand side. */
140 static rtx i2mod_old_rhs;
142 /* When I2MOD is nonnull, this is a copy of the new right hand side. */
144 static rtx i2mod_new_rhs;
146 /* Maximum register number, which is the size of the tables below. */
148 static unsigned int combine_max_regno;
150 struct reg_stat {
151 /* Record last point of death of (hard or pseudo) register n. */
152 rtx last_death;
154 /* Record last point of modification of (hard or pseudo) register n. */
155 rtx last_set;
157 /* The next group of fields allows the recording of the last value assigned
158 to (hard or pseudo) register n. We use this information to see if an
159 operation being processed is redundant given a prior operation performed
160 on the register. For example, an `and' with a constant is redundant if
161 all the zero bits are already known to be turned off.
163 We use an approach similar to that used by cse, but change it in the
164 following ways:
166 (1) We do not want to reinitialize at each label.
167 (2) It is useful, but not critical, to know the actual value assigned
168 to a register. Often just its form is helpful.
170 Therefore, we maintain the following fields:
172 last_set_value the last value assigned
173 last_set_label records the value of label_tick when the
174 register was assigned
175 last_set_table_tick records the value of label_tick when a
176 value using the register is assigned
177 last_set_invalid set to nonzero when it is not valid
178 to use the value of this register in some
179 register's value
181 To understand the usage of these tables, it is important to understand
182 the distinction between the value in last_set_value being valid and
183 the register being validly contained in some other expression in the
184 table.
186 (The next two parameters are out of date).
188 reg_stat[i].last_set_value is valid if it is nonzero, and either
189 reg_n_sets[i] is 1 or reg_stat[i].last_set_label == label_tick.
191 Register I may validly appear in any expression returned for the value
192 of another register if reg_n_sets[i] is 1. It may also appear in the
193 value for register J if reg_stat[j].last_set_invalid is zero, or
194 reg_stat[i].last_set_label < reg_stat[j].last_set_label.
196 If an expression is found in the table containing a register which may
197 not validly appear in an expression, the register is replaced by
198 something that won't match, (clobber (const_int 0)). */
200 /* Record last value assigned to (hard or pseudo) register n. */
202 rtx last_set_value;
204 /* Record the value of label_tick when an expression involving register n
205 is placed in last_set_value. */
207 int last_set_table_tick;
209 /* Record the value of label_tick when the value for register n is placed in
210 last_set_value. */
212 int last_set_label;
214 /* These fields are maintained in parallel with last_set_value and are
215 used to store the mode in which the register was last set, the bits
216 that were known to be zero when it was last set, and the number of
217 sign bits copies it was known to have when it was last set. */
219 unsigned HOST_WIDE_INT last_set_nonzero_bits;
220 char last_set_sign_bit_copies;
221 ENUM_BITFIELD(machine_mode) last_set_mode : 8;
223 /* Set nonzero if references to register n in expressions should not be
224 used. last_set_invalid is set nonzero when this register is being
225 assigned to and last_set_table_tick == label_tick. */
227 char last_set_invalid;
229 /* Some registers that are set more than once and used in more than one
230 basic block are nevertheless always set in similar ways. For example,
231 a QImode register may be loaded from memory in two places on a machine
232 where byte loads zero extend.
234 We record in the following fields if a register has some leading bits
235 that are always equal to the sign bit, and what we know about the
236 nonzero bits of a register, specifically which bits are known to be
237 zero.
239 If an entry is zero, it means that we don't know anything special. */
241 unsigned char sign_bit_copies;
243 unsigned HOST_WIDE_INT nonzero_bits;
245 /* Record the value of the label_tick when the last truncation
246 happened. The field truncated_to_mode is only valid if
247 truncation_label == label_tick. */
249 int truncation_label;
251 /* Record the last truncation seen for this register. If truncation
252 is not a nop to this mode we might be able to save an explicit
253 truncation if we know that value already contains a truncated
254 value. */
256 ENUM_BITFIELD(machine_mode) truncated_to_mode : 8;
259 static struct reg_stat *reg_stat;
261 /* Record the luid of the last insn that invalidated memory
262 (anything that writes memory, and subroutine calls, but not pushes). */
264 static int mem_last_set;
266 /* Record the luid of the last CALL_INSN
267 so we can tell whether a potential combination crosses any calls. */
269 static int last_call_luid;
271 /* When `subst' is called, this is the insn that is being modified
272 (by combining in a previous insn). The PATTERN of this insn
273 is still the old pattern partially modified and it should not be
274 looked at, but this may be used to examine the successors of the insn
275 to judge whether a simplification is valid. */
277 static rtx subst_insn;
279 /* This is the lowest LUID that `subst' is currently dealing with.
280 get_last_value will not return a value if the register was set at or
281 after this LUID. If not for this mechanism, we could get confused if
282 I2 or I1 in try_combine were an insn that used the old value of a register
283 to obtain a new value. In that case, we might erroneously get the
284 new value of the register when we wanted the old one. */
286 static int subst_low_luid;
288 /* This contains any hard registers that are used in newpat; reg_dead_at_p
289 must consider all these registers to be always live. */
291 static HARD_REG_SET newpat_used_regs;
293 /* This is an insn to which a LOG_LINKS entry has been added. If this
294 insn is the earlier than I2 or I3, combine should rescan starting at
295 that location. */
297 static rtx added_links_insn;
299 /* Basic block in which we are performing combines. */
300 static basic_block this_basic_block;
303 /* Length of the currently allocated uid_insn_cost array. */
305 static int max_uid_known;
307 /* The following array records the insn_rtx_cost for every insn
308 in the instruction stream. */
310 static int *uid_insn_cost;
312 /* The following array records the LOG_LINKS for every insn in the
313 instruction stream as an INSN_LIST rtx. */
315 static rtx *uid_log_links;
317 #define INSN_COST(INSN) (uid_insn_cost[INSN_UID (INSN)])
318 #define LOG_LINKS(INSN) (uid_log_links[INSN_UID (INSN)])
320 /* Incremented for each basic block. */
322 static int label_tick;
324 /* Reset to label_tick for each label. */
326 static int label_tick_ebb_start;
328 /* Mode used to compute significance in reg_stat[].nonzero_bits. It is the
329 largest integer mode that can fit in HOST_BITS_PER_WIDE_INT. */
331 static enum machine_mode nonzero_bits_mode;
333 /* Nonzero when reg_stat[].nonzero_bits and reg_stat[].sign_bit_copies can
334 be safely used. It is zero while computing them and after combine has
335 completed. This former test prevents propagating values based on
336 previously set values, which can be incorrect if a variable is modified
337 in a loop. */
339 static int nonzero_sign_valid;
342 /* Record one modification to rtl structure
343 to be undone by storing old_contents into *where. */
345 struct undo
347 struct undo *next;
348 enum { UNDO_RTX, UNDO_INT, UNDO_MODE } kind;
349 union { rtx r; int i; enum machine_mode m; } old_contents;
350 union { rtx *r; int *i; } where;
353 /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
354 num_undo says how many are currently recorded.
356 other_insn is nonzero if we have modified some other insn in the process
357 of working on subst_insn. It must be verified too. */
359 struct undobuf
361 struct undo *undos;
362 struct undo *frees;
363 rtx other_insn;
366 static struct undobuf undobuf;
368 /* Number of times the pseudo being substituted for
369 was found and replaced. */
371 static int n_occurrences;
373 static rtx reg_nonzero_bits_for_combine (rtx, enum machine_mode, rtx,
374 enum machine_mode,
375 unsigned HOST_WIDE_INT,
376 unsigned HOST_WIDE_INT *);
377 static rtx reg_num_sign_bit_copies_for_combine (rtx, enum machine_mode, rtx,
378 enum machine_mode,
379 unsigned int, unsigned int *);
380 static void do_SUBST (rtx *, rtx);
381 static void do_SUBST_INT (int *, int);
382 static void init_reg_last (void);
383 static void setup_incoming_promotions (rtx);
384 static void set_nonzero_bits_and_sign_copies (rtx, rtx, void *);
385 static int cant_combine_insn_p (rtx);
386 static int can_combine_p (rtx, rtx, rtx, rtx, rtx *, rtx *);
387 static int combinable_i3pat (rtx, rtx *, rtx, rtx, int, rtx *);
388 static int contains_muldiv (rtx);
389 static rtx try_combine (rtx, rtx, rtx, int *);
390 static void undo_all (void);
391 static void undo_commit (void);
392 static rtx *find_split_point (rtx *, rtx);
393 static rtx subst (rtx, rtx, rtx, int, int);
394 static rtx combine_simplify_rtx (rtx, enum machine_mode, int);
395 static rtx simplify_if_then_else (rtx);
396 static rtx simplify_set (rtx);
397 static rtx simplify_logical (rtx);
398 static rtx expand_compound_operation (rtx);
399 static rtx expand_field_assignment (rtx);
400 static rtx make_extraction (enum machine_mode, rtx, HOST_WIDE_INT,
401 rtx, unsigned HOST_WIDE_INT, int, int, int);
402 static rtx extract_left_shift (rtx, int);
403 static rtx make_compound_operation (rtx, enum rtx_code);
404 static int get_pos_from_mask (unsigned HOST_WIDE_INT,
405 unsigned HOST_WIDE_INT *);
406 static rtx canon_reg_for_combine (rtx, rtx);
407 static rtx force_to_mode (rtx, enum machine_mode,
408 unsigned HOST_WIDE_INT, int);
409 static rtx if_then_else_cond (rtx, rtx *, rtx *);
410 static rtx known_cond (rtx, enum rtx_code, rtx, rtx);
411 static int rtx_equal_for_field_assignment_p (rtx, rtx);
412 static rtx make_field_assignment (rtx);
413 static rtx apply_distributive_law (rtx);
414 static rtx distribute_and_simplify_rtx (rtx, int);
415 static rtx simplify_and_const_int_1 (enum machine_mode, rtx,
416 unsigned HOST_WIDE_INT);
417 static rtx simplify_and_const_int (rtx, enum machine_mode, rtx,
418 unsigned HOST_WIDE_INT);
419 static int merge_outer_ops (enum rtx_code *, HOST_WIDE_INT *, enum rtx_code,
420 HOST_WIDE_INT, enum machine_mode, int *);
421 static rtx simplify_shift_const_1 (enum rtx_code, enum machine_mode, rtx, int);
422 static rtx simplify_shift_const (rtx, enum rtx_code, enum machine_mode, rtx,
423 int);
424 static int recog_for_combine (rtx *, rtx, rtx *);
425 static rtx gen_lowpart_for_combine (enum machine_mode, rtx);
426 static enum rtx_code simplify_comparison (enum rtx_code, rtx *, rtx *);
427 static void update_table_tick (rtx);
428 static void record_value_for_reg (rtx, rtx, rtx);
429 static void check_conversions (rtx, rtx);
430 static void record_dead_and_set_regs_1 (rtx, rtx, void *);
431 static void record_dead_and_set_regs (rtx);
432 static int get_last_value_validate (rtx *, rtx, int, int);
433 static rtx get_last_value (rtx);
434 static int use_crosses_set_p (rtx, int);
435 static void reg_dead_at_p_1 (rtx, rtx, void *);
436 static int reg_dead_at_p (rtx, rtx);
437 static void move_deaths (rtx, rtx, int, rtx, rtx *);
438 static int reg_bitfield_target_p (rtx, rtx);
439 static void distribute_notes (rtx, rtx, rtx, rtx, rtx, rtx);
440 static void distribute_links (rtx);
441 static void mark_used_regs_combine (rtx);
442 static void record_promoted_value (rtx, rtx);
443 static int unmentioned_reg_p_1 (rtx *, void *);
444 static bool unmentioned_reg_p (rtx, rtx);
445 static void record_truncated_value (rtx);
446 static bool reg_truncated_to_mode (enum machine_mode, rtx);
447 static rtx gen_lowpart_or_truncate (enum machine_mode, rtx);
450 /* It is not safe to use ordinary gen_lowpart in combine.
451 See comments in gen_lowpart_for_combine. */
452 #undef RTL_HOOKS_GEN_LOWPART
453 #define RTL_HOOKS_GEN_LOWPART gen_lowpart_for_combine
455 /* Our implementation of gen_lowpart never emits a new pseudo. */
456 #undef RTL_HOOKS_GEN_LOWPART_NO_EMIT
457 #define RTL_HOOKS_GEN_LOWPART_NO_EMIT gen_lowpart_for_combine
459 #undef RTL_HOOKS_REG_NONZERO_REG_BITS
460 #define RTL_HOOKS_REG_NONZERO_REG_BITS reg_nonzero_bits_for_combine
462 #undef RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES
463 #define RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES reg_num_sign_bit_copies_for_combine
465 #undef RTL_HOOKS_REG_TRUNCATED_TO_MODE
466 #define RTL_HOOKS_REG_TRUNCATED_TO_MODE reg_truncated_to_mode
468 static const struct rtl_hooks combine_rtl_hooks = RTL_HOOKS_INITIALIZER;
471 /* This is used by find_single_use to locate an rtx in LOC that
472 contains exactly one use of DEST, which is typically either a REG
473 or CC0. It returns a pointer to the innermost rtx expression
474 containing DEST. Appearances of DEST that are being used to
475 totally replace it are not counted. */
477 static rtx *
478 find_single_use_1 (rtx dest, rtx *loc)
480 rtx x = *loc;
481 enum rtx_code code = GET_CODE (x);
482 rtx *result = NULL;
483 rtx *this_result;
484 int i;
485 const char *fmt;
487 switch (code)
489 case CONST_INT:
490 case CONST:
491 case LABEL_REF:
492 case SYMBOL_REF:
493 case CONST_DOUBLE:
494 case CONST_VECTOR:
495 case CLOBBER:
496 return 0;
498 case SET:
499 /* If the destination is anything other than CC0, PC, a REG or a SUBREG
500 of a REG that occupies all of the REG, the insn uses DEST if
501 it is mentioned in the destination or the source. Otherwise, we
502 need just check the source. */
503 if (GET_CODE (SET_DEST (x)) != CC0
504 && GET_CODE (SET_DEST (x)) != PC
505 && !REG_P (SET_DEST (x))
506 && ! (GET_CODE (SET_DEST (x)) == SUBREG
507 && REG_P (SUBREG_REG (SET_DEST (x)))
508 && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
509 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
510 == ((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
511 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))))
512 break;
514 return find_single_use_1 (dest, &SET_SRC (x));
516 case MEM:
517 case SUBREG:
518 return find_single_use_1 (dest, &XEXP (x, 0));
520 default:
521 break;
524 /* If it wasn't one of the common cases above, check each expression and
525 vector of this code. Look for a unique usage of DEST. */
527 fmt = GET_RTX_FORMAT (code);
528 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
530 if (fmt[i] == 'e')
532 if (dest == XEXP (x, i)
533 || (REG_P (dest) && REG_P (XEXP (x, i))
534 && REGNO (dest) == REGNO (XEXP (x, i))))
535 this_result = loc;
536 else
537 this_result = find_single_use_1 (dest, &XEXP (x, i));
539 if (result == NULL)
540 result = this_result;
541 else if (this_result)
542 /* Duplicate usage. */
543 return NULL;
545 else if (fmt[i] == 'E')
547 int j;
549 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
551 if (XVECEXP (x, i, j) == dest
552 || (REG_P (dest)
553 && REG_P (XVECEXP (x, i, j))
554 && REGNO (XVECEXP (x, i, j)) == REGNO (dest)))
555 this_result = loc;
556 else
557 this_result = find_single_use_1 (dest, &XVECEXP (x, i, j));
559 if (result == NULL)
560 result = this_result;
561 else if (this_result)
562 return NULL;
567 return result;
571 /* See if DEST, produced in INSN, is used only a single time in the
572 sequel. If so, return a pointer to the innermost rtx expression in which
573 it is used.
575 If PLOC is nonzero, *PLOC is set to the insn containing the single use.
577 If DEST is cc0_rtx, we look only at the next insn. In that case, we don't
578 care about REG_DEAD notes or LOG_LINKS.
580 Otherwise, we find the single use by finding an insn that has a
581 LOG_LINKS pointing at INSN and has a REG_DEAD note for DEST. If DEST is
582 only referenced once in that insn, we know that it must be the first
583 and last insn referencing DEST. */
585 static rtx *
586 find_single_use (rtx dest, rtx insn, rtx *ploc)
588 rtx next;
589 rtx *result;
590 rtx link;
592 #ifdef HAVE_cc0
593 if (dest == cc0_rtx)
595 next = NEXT_INSN (insn);
596 if (next == 0
597 || (!NONJUMP_INSN_P (next) && !JUMP_P (next)))
598 return 0;
600 result = find_single_use_1 (dest, &PATTERN (next));
601 if (result && ploc)
602 *ploc = next;
603 return result;
605 #endif
607 if (!REG_P (dest))
608 return 0;
610 for (next = next_nonnote_insn (insn);
611 next != 0 && !LABEL_P (next);
612 next = next_nonnote_insn (next))
613 if (INSN_P (next) && dead_or_set_p (next, dest))
615 for (link = LOG_LINKS (next); link; link = XEXP (link, 1))
616 if (XEXP (link, 0) == insn)
617 break;
619 if (link)
621 result = find_single_use_1 (dest, &PATTERN (next));
622 if (ploc)
623 *ploc = next;
624 return result;
628 return 0;
631 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
632 insn. The substitution can be undone by undo_all. If INTO is already
633 set to NEWVAL, do not record this change. Because computing NEWVAL might
634 also call SUBST, we have to compute it before we put anything into
635 the undo table. */
637 static void
638 do_SUBST (rtx *into, rtx newval)
640 struct undo *buf;
641 rtx oldval = *into;
643 if (oldval == newval)
644 return;
646 /* We'd like to catch as many invalid transformations here as
647 possible. Unfortunately, there are way too many mode changes
648 that are perfectly valid, so we'd waste too much effort for
649 little gain doing the checks here. Focus on catching invalid
650 transformations involving integer constants. */
651 if (GET_MODE_CLASS (GET_MODE (oldval)) == MODE_INT
652 && GET_CODE (newval) == CONST_INT)
654 /* Sanity check that we're replacing oldval with a CONST_INT
655 that is a valid sign-extension for the original mode. */
656 gcc_assert (INTVAL (newval)
657 == trunc_int_for_mode (INTVAL (newval), GET_MODE (oldval)));
659 /* Replacing the operand of a SUBREG or a ZERO_EXTEND with a
660 CONST_INT is not valid, because after the replacement, the
661 original mode would be gone. Unfortunately, we can't tell
662 when do_SUBST is called to replace the operand thereof, so we
663 perform this test on oldval instead, checking whether an
664 invalid replacement took place before we got here. */
665 gcc_assert (!(GET_CODE (oldval) == SUBREG
666 && GET_CODE (SUBREG_REG (oldval)) == CONST_INT));
667 gcc_assert (!(GET_CODE (oldval) == ZERO_EXTEND
668 && GET_CODE (XEXP (oldval, 0)) == CONST_INT));
671 if (undobuf.frees)
672 buf = undobuf.frees, undobuf.frees = buf->next;
673 else
674 buf = XNEW (struct undo);
676 buf->kind = UNDO_RTX;
677 buf->where.r = into;
678 buf->old_contents.r = oldval;
679 *into = newval;
681 buf->next = undobuf.undos, undobuf.undos = buf;
684 #define SUBST(INTO, NEWVAL) do_SUBST(&(INTO), (NEWVAL))
686 /* Similar to SUBST, but NEWVAL is an int expression. Note that substitution
687 for the value of a HOST_WIDE_INT value (including CONST_INT) is
688 not safe. */
690 static void
691 do_SUBST_INT (int *into, int newval)
693 struct undo *buf;
694 int oldval = *into;
696 if (oldval == newval)
697 return;
699 if (undobuf.frees)
700 buf = undobuf.frees, undobuf.frees = buf->next;
701 else
702 buf = XNEW (struct undo);
704 buf->kind = UNDO_INT;
705 buf->where.i = into;
706 buf->old_contents.i = oldval;
707 *into = newval;
709 buf->next = undobuf.undos, undobuf.undos = buf;
712 #define SUBST_INT(INTO, NEWVAL) do_SUBST_INT(&(INTO), (NEWVAL))
714 /* Similar to SUBST, but just substitute the mode. This is used when
715 changing the mode of a pseudo-register, so that any other
716 references to the entry in the regno_reg_rtx array will change as
717 well. */
719 static void
720 do_SUBST_MODE (rtx *into, enum machine_mode newval)
722 struct undo *buf;
723 enum machine_mode oldval = GET_MODE (*into);
725 if (oldval == newval)
726 return;
728 if (undobuf.frees)
729 buf = undobuf.frees, undobuf.frees = buf->next;
730 else
731 buf = XNEW (struct undo);
733 buf->kind = UNDO_MODE;
734 buf->where.r = into;
735 buf->old_contents.m = oldval;
736 PUT_MODE (*into, newval);
738 buf->next = undobuf.undos, undobuf.undos = buf;
741 #define SUBST_MODE(INTO, NEWVAL) do_SUBST_MODE(&(INTO), (NEWVAL))
743 /* Subroutine of try_combine. Determine whether the combine replacement
744 patterns NEWPAT, NEWI2PAT and NEWOTHERPAT are cheaper according to
745 insn_rtx_cost that the original instruction sequence I1, I2, I3 and
746 undobuf.other_insn. Note that I1 and/or NEWI2PAT may be NULL_RTX.
747 NEWOTHERPAT and undobuf.other_insn may also both be NULL_RTX. This
748 function returns false, if the costs of all instructions can be
749 estimated, and the replacements are more expensive than the original
750 sequence. */
752 static bool
753 combine_validate_cost (rtx i1, rtx i2, rtx i3, rtx newpat, rtx newi2pat,
754 rtx newotherpat)
756 int i1_cost, i2_cost, i3_cost;
757 int new_i2_cost, new_i3_cost;
758 int old_cost, new_cost;
760 /* Lookup the original insn_rtx_costs. */
761 i2_cost = INSN_COST (i2);
762 i3_cost = INSN_COST (i3);
764 if (i1)
766 i1_cost = INSN_COST (i1);
767 old_cost = (i1_cost > 0 && i2_cost > 0 && i3_cost > 0)
768 ? i1_cost + i2_cost + i3_cost : 0;
770 else
772 old_cost = (i2_cost > 0 && i3_cost > 0) ? i2_cost + i3_cost : 0;
773 i1_cost = 0;
776 /* Calculate the replacement insn_rtx_costs. */
777 new_i3_cost = insn_rtx_cost (newpat);
778 if (newi2pat)
780 new_i2_cost = insn_rtx_cost (newi2pat);
781 new_cost = (new_i2_cost > 0 && new_i3_cost > 0)
782 ? new_i2_cost + new_i3_cost : 0;
784 else
786 new_cost = new_i3_cost;
787 new_i2_cost = 0;
790 if (undobuf.other_insn)
792 int old_other_cost, new_other_cost;
794 old_other_cost = INSN_COST (undobuf.other_insn);
795 new_other_cost = insn_rtx_cost (newotherpat);
796 if (old_other_cost > 0 && new_other_cost > 0)
798 old_cost += old_other_cost;
799 new_cost += new_other_cost;
801 else
802 old_cost = 0;
805 /* Disallow this recombination if both new_cost and old_cost are
806 greater than zero, and new_cost is greater than old cost. */
807 if (old_cost > 0
808 && new_cost > old_cost)
810 if (dump_file)
812 if (i1)
814 fprintf (dump_file,
815 "rejecting combination of insns %d, %d and %d\n",
816 INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
817 fprintf (dump_file, "original costs %d + %d + %d = %d\n",
818 i1_cost, i2_cost, i3_cost, old_cost);
820 else
822 fprintf (dump_file,
823 "rejecting combination of insns %d and %d\n",
824 INSN_UID (i2), INSN_UID (i3));
825 fprintf (dump_file, "original costs %d + %d = %d\n",
826 i2_cost, i3_cost, old_cost);
829 if (newi2pat)
831 fprintf (dump_file, "replacement costs %d + %d = %d\n",
832 new_i2_cost, new_i3_cost, new_cost);
834 else
835 fprintf (dump_file, "replacement cost %d\n", new_cost);
838 return false;
841 /* Update the uid_insn_cost array with the replacement costs. */
842 INSN_COST (i2) = new_i2_cost;
843 INSN_COST (i3) = new_i3_cost;
844 if (i1)
845 INSN_COST (i1) = 0;
847 return true;
851 /* Delete any insns that copy a register to itself. */
853 static void
854 delete_noop_moves (void)
856 rtx insn, next;
857 basic_block bb;
859 FOR_EACH_BB (bb)
861 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb)); insn = next)
863 next = NEXT_INSN (insn);
864 if (INSN_P (insn) && noop_move_p (insn))
866 rtx note;
868 /* If we're about to remove the first insn of a libcall
869 then move the libcall note to the next real insn and
870 update the retval note. */
871 if ((note = find_reg_note (insn, REG_LIBCALL, NULL_RTX))
872 && XEXP (note, 0) != insn)
874 rtx new_libcall_insn = next_real_insn (insn);
875 rtx retval_note = find_reg_note (XEXP (note, 0),
876 REG_RETVAL, NULL_RTX);
877 REG_NOTES (new_libcall_insn)
878 = gen_rtx_INSN_LIST (REG_LIBCALL, XEXP (note, 0),
879 REG_NOTES (new_libcall_insn));
880 XEXP (retval_note, 0) = new_libcall_insn;
883 if (dump_file)
884 fprintf (dump_file, "deleting noop move %d\n", INSN_UID (insn));
886 delete_insn_and_edges (insn);
893 /* Fill in log links field for all insns. */
895 static void
896 create_log_links (void)
898 basic_block bb;
899 rtx *next_use, insn;
900 struct df_ref **def_vec, **use_vec;
902 next_use = XCNEWVEC (rtx, max_reg_num ());
904 /* Pass through each block from the end, recording the uses of each
905 register and establishing log links when def is encountered.
906 Note that we do not clear next_use array in order to save time,
907 so we have to test whether the use is in the same basic block as def.
909 There are a few cases below when we do not consider the definition or
910 usage -- these are taken from original flow.c did. Don't ask me why it is
911 done this way; I don't know and if it works, I don't want to know. */
913 FOR_EACH_BB (bb)
915 FOR_BB_INSNS_REVERSE (bb, insn)
917 if (!INSN_P (insn))
918 continue;
920 /* Log links are created only once. */
921 gcc_assert (!LOG_LINKS (insn));
923 for (def_vec = DF_INSN_DEFS (insn); *def_vec; def_vec++)
925 struct df_ref *def = *def_vec;
926 int regno = DF_REF_REGNO (def);
927 rtx use_insn;
929 if (!next_use[regno])
930 continue;
932 /* Do not consider if it is pre/post modification in MEM. */
933 if (DF_REF_FLAGS (def) & DF_REF_PRE_POST_MODIFY)
934 continue;
936 /* Do not make the log link for frame pointer. */
937 if ((regno == FRAME_POINTER_REGNUM
938 && (! reload_completed || frame_pointer_needed))
939 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
940 || (regno == HARD_FRAME_POINTER_REGNUM
941 && (! reload_completed || frame_pointer_needed))
942 #endif
943 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
944 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
945 #endif
947 continue;
949 use_insn = next_use[regno];
950 if (BLOCK_FOR_INSN (use_insn) == bb)
952 /* flow.c claimed:
954 We don't build a LOG_LINK for hard registers contained
955 in ASM_OPERANDs. If these registers get replaced,
956 we might wind up changing the semantics of the insn,
957 even if reload can make what appear to be valid
958 assignments later. */
959 if (regno >= FIRST_PSEUDO_REGISTER
960 || asm_noperands (PATTERN (use_insn)) < 0)
961 LOG_LINKS (use_insn) =
962 alloc_INSN_LIST (insn, LOG_LINKS (use_insn));
964 next_use[regno] = NULL_RTX;
967 for (use_vec = DF_INSN_USES (insn); *use_vec; use_vec++)
969 struct df_ref *use = *use_vec;
970 int regno = DF_REF_REGNO (use);
972 /* Do not consider the usage of the stack pointer
973 by function call. */
974 if (DF_REF_FLAGS (use) & DF_REF_CALL_STACK_USAGE)
975 continue;
977 next_use[regno] = insn;
982 free (next_use);
985 /* Clear LOG_LINKS fields of insns. */
987 static void
988 clear_log_links (void)
990 rtx insn;
992 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
993 if (INSN_P (insn))
994 free_INSN_LIST_list (&LOG_LINKS (insn));
1000 /* Main entry point for combiner. F is the first insn of the function.
1001 NREGS is the first unused pseudo-reg number.
1003 Return nonzero if the combiner has turned an indirect jump
1004 instruction into a direct jump. */
1005 static int
1006 combine_instructions (rtx f, unsigned int nregs)
1008 rtx insn, next;
1009 #ifdef HAVE_cc0
1010 rtx prev;
1011 #endif
1012 rtx links, nextlinks;
1013 rtx first;
1015 int new_direct_jump_p = 0;
1017 for (first = f; first && !INSN_P (first); )
1018 first = NEXT_INSN (first);
1019 if (!first)
1020 return 0;
1022 combine_attempts = 0;
1023 combine_merges = 0;
1024 combine_extras = 0;
1025 combine_successes = 0;
1027 combine_max_regno = nregs;
1029 rtl_hooks = combine_rtl_hooks;
1031 reg_stat = XCNEWVEC (struct reg_stat, nregs);
1033 init_recog_no_volatile ();
1035 /* Allocate array for insn info. */
1036 max_uid_known = get_max_uid ();
1037 uid_log_links = XCNEWVEC (rtx, max_uid_known + 1);
1038 uid_insn_cost = XCNEWVEC (int, max_uid_known + 1);
1040 nonzero_bits_mode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
1042 /* Don't use reg_stat[].nonzero_bits when computing it. This can cause
1043 problems when, for example, we have j <<= 1 in a loop. */
1045 nonzero_sign_valid = 0;
1047 /* Scan all SETs and see if we can deduce anything about what
1048 bits are known to be zero for some registers and how many copies
1049 of the sign bit are known to exist for those registers.
1051 Also set any known values so that we can use it while searching
1052 for what bits are known to be set. */
1054 label_tick = label_tick_ebb_start = 1;
1056 setup_incoming_promotions (first);
1058 create_log_links ();
1059 FOR_EACH_BB (this_basic_block)
1061 last_call_luid = 0;
1062 mem_last_set = -1;
1063 label_tick++;
1064 FOR_BB_INSNS (this_basic_block, insn)
1065 if (INSN_P (insn) && BLOCK_FOR_INSN (insn))
1067 subst_low_luid = DF_INSN_LUID (insn);
1068 subst_insn = insn;
1070 note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies,
1071 insn);
1072 record_dead_and_set_regs (insn);
1074 #ifdef AUTO_INC_DEC
1075 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
1076 if (REG_NOTE_KIND (links) == REG_INC)
1077 set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX,
1078 insn);
1079 #endif
1081 /* Record the current insn_rtx_cost of this instruction. */
1082 if (NONJUMP_INSN_P (insn))
1083 INSN_COST (insn) = insn_rtx_cost (PATTERN (insn));
1084 if (dump_file)
1085 fprintf(dump_file, "insn_cost %d: %d\n",
1086 INSN_UID (insn), INSN_COST (insn));
1088 else if (LABEL_P (insn))
1089 label_tick_ebb_start = label_tick;
1092 nonzero_sign_valid = 1;
1094 /* Now scan all the insns in forward order. */
1096 label_tick = label_tick_ebb_start = 1;
1097 init_reg_last ();
1098 setup_incoming_promotions (first);
1100 FOR_EACH_BB (this_basic_block)
1102 last_call_luid = 0;
1103 mem_last_set = -1;
1104 label_tick++;
1105 for (insn = BB_HEAD (this_basic_block);
1106 insn != NEXT_INSN (BB_END (this_basic_block));
1107 insn = next ? next : NEXT_INSN (insn))
1109 next = 0;
1110 if (INSN_P (insn))
1112 /* See if we know about function return values before this
1113 insn based upon SUBREG flags. */
1114 check_conversions (insn, PATTERN (insn));
1116 /* Try this insn with each insn it links back to. */
1118 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
1119 if ((next = try_combine (insn, XEXP (links, 0),
1120 NULL_RTX, &new_direct_jump_p)) != 0)
1121 goto retry;
1123 /* Try each sequence of three linked insns ending with this one. */
1125 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
1127 rtx link = XEXP (links, 0);
1129 /* If the linked insn has been replaced by a note, then there
1130 is no point in pursuing this chain any further. */
1131 if (NOTE_P (link))
1132 continue;
1134 for (nextlinks = LOG_LINKS (link);
1135 nextlinks;
1136 nextlinks = XEXP (nextlinks, 1))
1137 if ((next = try_combine (insn, link,
1138 XEXP (nextlinks, 0),
1139 &new_direct_jump_p)) != 0)
1140 goto retry;
1143 #ifdef HAVE_cc0
1144 /* Try to combine a jump insn that uses CC0
1145 with a preceding insn that sets CC0, and maybe with its
1146 logical predecessor as well.
1147 This is how we make decrement-and-branch insns.
1148 We need this special code because data flow connections
1149 via CC0 do not get entered in LOG_LINKS. */
1151 if (JUMP_P (insn)
1152 && (prev = prev_nonnote_insn (insn)) != 0
1153 && NONJUMP_INSN_P (prev)
1154 && sets_cc0_p (PATTERN (prev)))
1156 if ((next = try_combine (insn, prev,
1157 NULL_RTX, &new_direct_jump_p)) != 0)
1158 goto retry;
1160 for (nextlinks = LOG_LINKS (prev); nextlinks;
1161 nextlinks = XEXP (nextlinks, 1))
1162 if ((next = try_combine (insn, prev,
1163 XEXP (nextlinks, 0),
1164 &new_direct_jump_p)) != 0)
1165 goto retry;
1168 /* Do the same for an insn that explicitly references CC0. */
1169 if (NONJUMP_INSN_P (insn)
1170 && (prev = prev_nonnote_insn (insn)) != 0
1171 && NONJUMP_INSN_P (prev)
1172 && sets_cc0_p (PATTERN (prev))
1173 && GET_CODE (PATTERN (insn)) == SET
1174 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
1176 if ((next = try_combine (insn, prev,
1177 NULL_RTX, &new_direct_jump_p)) != 0)
1178 goto retry;
1180 for (nextlinks = LOG_LINKS (prev); nextlinks;
1181 nextlinks = XEXP (nextlinks, 1))
1182 if ((next = try_combine (insn, prev,
1183 XEXP (nextlinks, 0),
1184 &new_direct_jump_p)) != 0)
1185 goto retry;
1188 /* Finally, see if any of the insns that this insn links to
1189 explicitly references CC0. If so, try this insn, that insn,
1190 and its predecessor if it sets CC0. */
1191 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
1192 if (NONJUMP_INSN_P (XEXP (links, 0))
1193 && GET_CODE (PATTERN (XEXP (links, 0))) == SET
1194 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (XEXP (links, 0))))
1195 && (prev = prev_nonnote_insn (XEXP (links, 0))) != 0
1196 && NONJUMP_INSN_P (prev)
1197 && sets_cc0_p (PATTERN (prev))
1198 && (next = try_combine (insn, XEXP (links, 0),
1199 prev, &new_direct_jump_p)) != 0)
1200 goto retry;
1201 #endif
1203 /* Try combining an insn with two different insns whose results it
1204 uses. */
1205 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
1206 for (nextlinks = XEXP (links, 1); nextlinks;
1207 nextlinks = XEXP (nextlinks, 1))
1208 if ((next = try_combine (insn, XEXP (links, 0),
1209 XEXP (nextlinks, 0),
1210 &new_direct_jump_p)) != 0)
1211 goto retry;
1213 /* Try this insn with each REG_EQUAL note it links back to. */
1214 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
1216 rtx set, note;
1217 rtx temp = XEXP (links, 0);
1218 if ((set = single_set (temp)) != 0
1219 && (note = find_reg_equal_equiv_note (temp)) != 0
1220 && (note = XEXP (note, 0), GET_CODE (note)) != EXPR_LIST
1221 /* Avoid using a register that may already been marked
1222 dead by an earlier instruction. */
1223 && ! unmentioned_reg_p (note, SET_SRC (set))
1224 && (GET_MODE (note) == VOIDmode
1225 ? SCALAR_INT_MODE_P (GET_MODE (SET_DEST (set)))
1226 : GET_MODE (SET_DEST (set)) == GET_MODE (note)))
1228 /* Temporarily replace the set's source with the
1229 contents of the REG_EQUAL note. The insn will
1230 be deleted or recognized by try_combine. */
1231 rtx orig = SET_SRC (set);
1232 SET_SRC (set) = note;
1233 i2mod = temp;
1234 i2mod_old_rhs = copy_rtx (orig);
1235 i2mod_new_rhs = copy_rtx (note);
1236 next = try_combine (insn, i2mod, NULL_RTX,
1237 &new_direct_jump_p);
1238 i2mod = NULL_RTX;
1239 if (next)
1240 goto retry;
1241 SET_SRC (set) = orig;
1245 if (!NOTE_P (insn))
1246 record_dead_and_set_regs (insn);
1248 retry:
1251 else if (LABEL_P (insn))
1252 label_tick_ebb_start = label_tick;
1256 clear_log_links ();
1257 clear_bb_flags ();
1258 new_direct_jump_p |= purge_all_dead_edges ();
1259 delete_noop_moves ();
1261 /* Clean up. */
1262 free (uid_log_links);
1263 free (uid_insn_cost);
1264 free (reg_stat);
1267 struct undo *undo, *next;
1268 for (undo = undobuf.frees; undo; undo = next)
1270 next = undo->next;
1271 free (undo);
1273 undobuf.frees = 0;
1276 total_attempts += combine_attempts;
1277 total_merges += combine_merges;
1278 total_extras += combine_extras;
1279 total_successes += combine_successes;
1281 nonzero_sign_valid = 0;
1282 rtl_hooks = general_rtl_hooks;
1284 /* Make recognizer allow volatile MEMs again. */
1285 init_recog ();
1287 return new_direct_jump_p;
1290 /* Wipe the last_xxx fields of reg_stat in preparation for another pass. */
1292 static void
1293 init_reg_last (void)
1295 unsigned int i;
1296 for (i = 0; i < combine_max_regno; i++)
1297 memset (reg_stat + i, 0, offsetof (struct reg_stat, sign_bit_copies));
1300 /* Set up any promoted values for incoming argument registers. */
1302 static void
1303 setup_incoming_promotions (rtx first)
1305 tree arg;
1307 if (!targetm.calls.promote_function_args (TREE_TYPE (cfun->decl)))
1308 return;
1310 for (arg = DECL_ARGUMENTS (current_function_decl); arg;
1311 arg = TREE_CHAIN (arg))
1313 rtx reg = DECL_INCOMING_RTL (arg);
1315 if (!REG_P (reg))
1316 continue;
1318 if (TYPE_MODE (DECL_ARG_TYPE (arg)) == TYPE_MODE (TREE_TYPE (arg)))
1320 enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg));
1321 int uns = TYPE_UNSIGNED (TREE_TYPE (arg));
1323 mode = promote_mode (TREE_TYPE (arg), mode, &uns, 1);
1324 if (mode == GET_MODE (reg) && mode != DECL_MODE (arg))
1326 rtx x;
1327 x = gen_rtx_CLOBBER (DECL_MODE (arg), const0_rtx);
1328 x = gen_rtx_fmt_e ((uns ? ZERO_EXTEND : SIGN_EXTEND), mode, x);
1329 record_value_for_reg (reg, first, x);
1335 /* Called via note_stores. If X is a pseudo that is narrower than
1336 HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
1338 If we are setting only a portion of X and we can't figure out what
1339 portion, assume all bits will be used since we don't know what will
1340 be happening.
1342 Similarly, set how many bits of X are known to be copies of the sign bit
1343 at all locations in the function. This is the smallest number implied
1344 by any set of X. */
1346 static void
1347 set_nonzero_bits_and_sign_copies (rtx x, rtx set, void *data)
1349 rtx insn = (rtx) data;
1350 unsigned int num;
1352 if (REG_P (x)
1353 && REGNO (x) >= FIRST_PSEUDO_REGISTER
1354 /* If this register is undefined at the start of the file, we can't
1355 say what its contents were. */
1356 && ! REGNO_REG_SET_P
1357 (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), REGNO (x))
1358 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
1360 if (set == 0 || GET_CODE (set) == CLOBBER)
1362 reg_stat[REGNO (x)].nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1363 reg_stat[REGNO (x)].sign_bit_copies = 1;
1364 return;
1367 /* If this register is being initialized using itself, and the
1368 register is uninitialized in this basic block, and there are
1369 no LOG_LINKS which set the register, then part of the
1370 register is uninitialized. In that case we can't assume
1371 anything about the number of nonzero bits.
1373 ??? We could do better if we checked this in
1374 reg_{nonzero_bits,num_sign_bit_copies}_for_combine. Then we
1375 could avoid making assumptions about the insn which initially
1376 sets the register, while still using the information in other
1377 insns. We would have to be careful to check every insn
1378 involved in the combination. */
1380 if (insn
1381 && reg_referenced_p (x, PATTERN (insn))
1382 && !REGNO_REG_SET_P (DF_LR_IN (BLOCK_FOR_INSN (insn)),
1383 REGNO (x)))
1385 rtx link;
1387 for (link = LOG_LINKS (insn); link; link = XEXP (link, 1))
1389 if (dead_or_set_p (XEXP (link, 0), x))
1390 break;
1392 if (!link)
1394 reg_stat[REGNO (x)].nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1395 reg_stat[REGNO (x)].sign_bit_copies = 1;
1396 return;
1400 /* If this is a complex assignment, see if we can convert it into a
1401 simple assignment. */
1402 set = expand_field_assignment (set);
1404 /* If this is a simple assignment, or we have a paradoxical SUBREG,
1405 set what we know about X. */
1407 if (SET_DEST (set) == x
1408 || (GET_CODE (SET_DEST (set)) == SUBREG
1409 && (GET_MODE_SIZE (GET_MODE (SET_DEST (set)))
1410 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (set)))))
1411 && SUBREG_REG (SET_DEST (set)) == x))
1413 rtx src = SET_SRC (set);
1415 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
1416 /* If X is narrower than a word and SRC is a non-negative
1417 constant that would appear negative in the mode of X,
1418 sign-extend it for use in reg_stat[].nonzero_bits because some
1419 machines (maybe most) will actually do the sign-extension
1420 and this is the conservative approach.
1422 ??? For 2.5, try to tighten up the MD files in this regard
1423 instead of this kludge. */
1425 if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
1426 && GET_CODE (src) == CONST_INT
1427 && INTVAL (src) > 0
1428 && 0 != (INTVAL (src)
1429 & ((HOST_WIDE_INT) 1
1430 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
1431 src = GEN_INT (INTVAL (src)
1432 | ((HOST_WIDE_INT) (-1)
1433 << GET_MODE_BITSIZE (GET_MODE (x))));
1434 #endif
1436 /* Don't call nonzero_bits if it cannot change anything. */
1437 if (reg_stat[REGNO (x)].nonzero_bits != ~(unsigned HOST_WIDE_INT) 0)
1438 reg_stat[REGNO (x)].nonzero_bits
1439 |= nonzero_bits (src, nonzero_bits_mode);
1440 num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
1441 if (reg_stat[REGNO (x)].sign_bit_copies == 0
1442 || reg_stat[REGNO (x)].sign_bit_copies > num)
1443 reg_stat[REGNO (x)].sign_bit_copies = num;
1445 else
1447 reg_stat[REGNO (x)].nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1448 reg_stat[REGNO (x)].sign_bit_copies = 1;
1453 /* See if INSN can be combined into I3. PRED and SUCC are optionally
1454 insns that were previously combined into I3 or that will be combined
1455 into the merger of INSN and I3.
1457 Return 0 if the combination is not allowed for any reason.
1459 If the combination is allowed, *PDEST will be set to the single
1460 destination of INSN and *PSRC to the single source, and this function
1461 will return 1. */
1463 static int
1464 can_combine_p (rtx insn, rtx i3, rtx pred ATTRIBUTE_UNUSED, rtx succ,
1465 rtx *pdest, rtx *psrc)
1467 int i;
1468 rtx set = 0, src, dest;
1469 rtx p;
1470 #ifdef AUTO_INC_DEC
1471 rtx link;
1472 #endif
1473 int all_adjacent = (succ ? (next_active_insn (insn) == succ
1474 && next_active_insn (succ) == i3)
1475 : next_active_insn (insn) == i3);
1477 /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
1478 or a PARALLEL consisting of such a SET and CLOBBERs.
1480 If INSN has CLOBBER parallel parts, ignore them for our processing.
1481 By definition, these happen during the execution of the insn. When it
1482 is merged with another insn, all bets are off. If they are, in fact,
1483 needed and aren't also supplied in I3, they may be added by
1484 recog_for_combine. Otherwise, it won't match.
1486 We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
1487 note.
1489 Get the source and destination of INSN. If more than one, can't
1490 combine. */
1492 if (GET_CODE (PATTERN (insn)) == SET)
1493 set = PATTERN (insn);
1494 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1495 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1497 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1499 rtx elt = XVECEXP (PATTERN (insn), 0, i);
1500 rtx note;
1502 switch (GET_CODE (elt))
1504 /* This is important to combine floating point insns
1505 for the SH4 port. */
1506 case USE:
1507 /* Combining an isolated USE doesn't make sense.
1508 We depend here on combinable_i3pat to reject them. */
1509 /* The code below this loop only verifies that the inputs of
1510 the SET in INSN do not change. We call reg_set_between_p
1511 to verify that the REG in the USE does not change between
1512 I3 and INSN.
1513 If the USE in INSN was for a pseudo register, the matching
1514 insn pattern will likely match any register; combining this
1515 with any other USE would only be safe if we knew that the
1516 used registers have identical values, or if there was
1517 something to tell them apart, e.g. different modes. For
1518 now, we forgo such complicated tests and simply disallow
1519 combining of USES of pseudo registers with any other USE. */
1520 if (REG_P (XEXP (elt, 0))
1521 && GET_CODE (PATTERN (i3)) == PARALLEL)
1523 rtx i3pat = PATTERN (i3);
1524 int i = XVECLEN (i3pat, 0) - 1;
1525 unsigned int regno = REGNO (XEXP (elt, 0));
1529 rtx i3elt = XVECEXP (i3pat, 0, i);
1531 if (GET_CODE (i3elt) == USE
1532 && REG_P (XEXP (i3elt, 0))
1533 && (REGNO (XEXP (i3elt, 0)) == regno
1534 ? reg_set_between_p (XEXP (elt, 0),
1535 PREV_INSN (insn), i3)
1536 : regno >= FIRST_PSEUDO_REGISTER))
1537 return 0;
1539 while (--i >= 0);
1541 break;
1543 /* We can ignore CLOBBERs. */
1544 case CLOBBER:
1545 break;
1547 case SET:
1548 /* Ignore SETs whose result isn't used but not those that
1549 have side-effects. */
1550 if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
1551 && (!(note = find_reg_note (insn, REG_EH_REGION, NULL_RTX))
1552 || INTVAL (XEXP (note, 0)) <= 0)
1553 && ! side_effects_p (elt))
1554 break;
1556 /* If we have already found a SET, this is a second one and
1557 so we cannot combine with this insn. */
1558 if (set)
1559 return 0;
1561 set = elt;
1562 break;
1564 default:
1565 /* Anything else means we can't combine. */
1566 return 0;
1570 if (set == 0
1571 /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
1572 so don't do anything with it. */
1573 || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
1574 return 0;
1576 else
1577 return 0;
1579 if (set == 0)
1580 return 0;
1582 set = expand_field_assignment (set);
1583 src = SET_SRC (set), dest = SET_DEST (set);
1585 /* Don't eliminate a store in the stack pointer. */
1586 if (dest == stack_pointer_rtx
1587 /* Don't combine with an insn that sets a register to itself if it has
1588 a REG_EQUAL note. This may be part of a REG_NO_CONFLICT sequence. */
1589 || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1590 /* Can't merge an ASM_OPERANDS. */
1591 || GET_CODE (src) == ASM_OPERANDS
1592 /* Can't merge a function call. */
1593 || GET_CODE (src) == CALL
1594 /* Don't eliminate a function call argument. */
1595 || (CALL_P (i3)
1596 && (find_reg_fusage (i3, USE, dest)
1597 || (REG_P (dest)
1598 && REGNO (dest) < FIRST_PSEUDO_REGISTER
1599 && global_regs[REGNO (dest)])))
1600 /* Don't substitute into an incremented register. */
1601 || FIND_REG_INC_NOTE (i3, dest)
1602 || (succ && FIND_REG_INC_NOTE (succ, dest))
1603 /* Don't substitute into a non-local goto, this confuses CFG. */
1604 || (JUMP_P (i3) && find_reg_note (i3, REG_NON_LOCAL_GOTO, NULL_RTX))
1605 #if 0
1606 /* Don't combine the end of a libcall into anything. */
1607 /* ??? This gives worse code, and appears to be unnecessary, since no
1608 pass after flow uses REG_LIBCALL/REG_RETVAL notes. Local-alloc does
1609 use REG_RETVAL notes for noconflict blocks, but other code here
1610 makes sure that those insns don't disappear. */
1611 || find_reg_note (insn, REG_RETVAL, NULL_RTX)
1612 #endif
1613 /* Make sure that DEST is not used after SUCC but before I3. */
1614 || (succ && ! all_adjacent
1615 && reg_used_between_p (dest, succ, i3))
1616 /* Make sure that the value that is to be substituted for the register
1617 does not use any registers whose values alter in between. However,
1618 If the insns are adjacent, a use can't cross a set even though we
1619 think it might (this can happen for a sequence of insns each setting
1620 the same destination; last_set of that register might point to
1621 a NOTE). If INSN has a REG_EQUIV note, the register is always
1622 equivalent to the memory so the substitution is valid even if there
1623 are intervening stores. Also, don't move a volatile asm or
1624 UNSPEC_VOLATILE across any other insns. */
1625 || (! all_adjacent
1626 && (((!MEM_P (src)
1627 || ! find_reg_note (insn, REG_EQUIV, src))
1628 && use_crosses_set_p (src, DF_INSN_LUID (insn)))
1629 || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
1630 || GET_CODE (src) == UNSPEC_VOLATILE))
1631 /* If there is a REG_NO_CONFLICT note for DEST in I3 or SUCC, we get
1632 better register allocation by not doing the combine. */
1633 || find_reg_note (i3, REG_NO_CONFLICT, dest)
1634 || (succ && find_reg_note (succ, REG_NO_CONFLICT, dest))
1635 /* Don't combine across a CALL_INSN, because that would possibly
1636 change whether the life span of some REGs crosses calls or not,
1637 and it is a pain to update that information.
1638 Exception: if source is a constant, moving it later can't hurt.
1639 Accept that special case, because it helps -fforce-addr a lot. */
1640 || (DF_INSN_LUID (insn) < last_call_luid && ! CONSTANT_P (src)))
1641 return 0;
1643 /* DEST must either be a REG or CC0. */
1644 if (REG_P (dest))
1646 /* If register alignment is being enforced for multi-word items in all
1647 cases except for parameters, it is possible to have a register copy
1648 insn referencing a hard register that is not allowed to contain the
1649 mode being copied and which would not be valid as an operand of most
1650 insns. Eliminate this problem by not combining with such an insn.
1652 Also, on some machines we don't want to extend the life of a hard
1653 register. */
1655 if (REG_P (src)
1656 && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
1657 && ! HARD_REGNO_MODE_OK (REGNO (dest), GET_MODE (dest)))
1658 /* Don't extend the life of a hard register unless it is
1659 user variable (if we have few registers) or it can't
1660 fit into the desired register (meaning something special
1661 is going on).
1662 Also avoid substituting a return register into I3, because
1663 reload can't handle a conflict with constraints of other
1664 inputs. */
1665 || (REGNO (src) < FIRST_PSEUDO_REGISTER
1666 && ! HARD_REGNO_MODE_OK (REGNO (src), GET_MODE (src)))))
1667 return 0;
1669 else if (GET_CODE (dest) != CC0)
1670 return 0;
1673 if (GET_CODE (PATTERN (i3)) == PARALLEL)
1674 for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
1675 if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER)
1677 /* Don't substitute for a register intended as a clobberable
1678 operand. */
1679 rtx reg = XEXP (XVECEXP (PATTERN (i3), 0, i), 0);
1680 if (rtx_equal_p (reg, dest))
1681 return 0;
1683 /* If the clobber represents an earlyclobber operand, we must not
1684 substitute an expression containing the clobbered register.
1685 As we do not analyze the constraint strings here, we have to
1686 make the conservative assumption. However, if the register is
1687 a fixed hard reg, the clobber cannot represent any operand;
1688 we leave it up to the machine description to either accept or
1689 reject use-and-clobber patterns. */
1690 if (!REG_P (reg)
1691 || REGNO (reg) >= FIRST_PSEUDO_REGISTER
1692 || !fixed_regs[REGNO (reg)])
1693 if (reg_overlap_mentioned_p (reg, src))
1694 return 0;
1697 /* If INSN contains anything volatile, or is an `asm' (whether volatile
1698 or not), reject, unless nothing volatile comes between it and I3 */
1700 if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
1702 /* Make sure succ doesn't contain a volatile reference. */
1703 if (succ != 0 && volatile_refs_p (PATTERN (succ)))
1704 return 0;
1706 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1707 if (INSN_P (p) && p != succ && volatile_refs_p (PATTERN (p)))
1708 return 0;
1711 /* If INSN is an asm, and DEST is a hard register, reject, since it has
1712 to be an explicit register variable, and was chosen for a reason. */
1714 if (GET_CODE (src) == ASM_OPERANDS
1715 && REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER)
1716 return 0;
1718 /* If there are any volatile insns between INSN and I3, reject, because
1719 they might affect machine state. */
1721 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1722 if (INSN_P (p) && p != succ && volatile_insn_p (PATTERN (p)))
1723 return 0;
1725 /* If INSN contains an autoincrement or autodecrement, make sure that
1726 register is not used between there and I3, and not already used in
1727 I3 either. Neither must it be used in PRED or SUCC, if they exist.
1728 Also insist that I3 not be a jump; if it were one
1729 and the incremented register were spilled, we would lose. */
1731 #ifdef AUTO_INC_DEC
1732 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1733 if (REG_NOTE_KIND (link) == REG_INC
1734 && (JUMP_P (i3)
1735 || reg_used_between_p (XEXP (link, 0), insn, i3)
1736 || (pred != NULL_RTX
1737 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred)))
1738 || (succ != NULL_RTX
1739 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ)))
1740 || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
1741 return 0;
1742 #endif
1744 #ifdef HAVE_cc0
1745 /* Don't combine an insn that follows a CC0-setting insn.
1746 An insn that uses CC0 must not be separated from the one that sets it.
1747 We do, however, allow I2 to follow a CC0-setting insn if that insn
1748 is passed as I1; in that case it will be deleted also.
1749 We also allow combining in this case if all the insns are adjacent
1750 because that would leave the two CC0 insns adjacent as well.
1751 It would be more logical to test whether CC0 occurs inside I1 or I2,
1752 but that would be much slower, and this ought to be equivalent. */
1754 p = prev_nonnote_insn (insn);
1755 if (p && p != pred && NONJUMP_INSN_P (p) && sets_cc0_p (PATTERN (p))
1756 && ! all_adjacent)
1757 return 0;
1758 #endif
1760 /* If we get here, we have passed all the tests and the combination is
1761 to be allowed. */
1763 *pdest = dest;
1764 *psrc = src;
1766 return 1;
1769 /* LOC is the location within I3 that contains its pattern or the component
1770 of a PARALLEL of the pattern. We validate that it is valid for combining.
1772 One problem is if I3 modifies its output, as opposed to replacing it
1773 entirely, we can't allow the output to contain I2DEST or I1DEST as doing
1774 so would produce an insn that is not equivalent to the original insns.
1776 Consider:
1778 (set (reg:DI 101) (reg:DI 100))
1779 (set (subreg:SI (reg:DI 101) 0) <foo>)
1781 This is NOT equivalent to:
1783 (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
1784 (set (reg:DI 101) (reg:DI 100))])
1786 Not only does this modify 100 (in which case it might still be valid
1787 if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
1789 We can also run into a problem if I2 sets a register that I1
1790 uses and I1 gets directly substituted into I3 (not via I2). In that
1791 case, we would be getting the wrong value of I2DEST into I3, so we
1792 must reject the combination. This case occurs when I2 and I1 both
1793 feed into I3, rather than when I1 feeds into I2, which feeds into I3.
1794 If I1_NOT_IN_SRC is nonzero, it means that finding I1 in the source
1795 of a SET must prevent combination from occurring.
1797 Before doing the above check, we first try to expand a field assignment
1798 into a set of logical operations.
1800 If PI3_DEST_KILLED is nonzero, it is a pointer to a location in which
1801 we place a register that is both set and used within I3. If more than one
1802 such register is detected, we fail.
1804 Return 1 if the combination is valid, zero otherwise. */
1806 static int
1807 combinable_i3pat (rtx i3, rtx *loc, rtx i2dest, rtx i1dest,
1808 int i1_not_in_src, rtx *pi3dest_killed)
1810 rtx x = *loc;
1812 if (GET_CODE (x) == SET)
1814 rtx set = x ;
1815 rtx dest = SET_DEST (set);
1816 rtx src = SET_SRC (set);
1817 rtx inner_dest = dest;
1818 rtx subdest;
1820 while (GET_CODE (inner_dest) == STRICT_LOW_PART
1821 || GET_CODE (inner_dest) == SUBREG
1822 || GET_CODE (inner_dest) == ZERO_EXTRACT)
1823 inner_dest = XEXP (inner_dest, 0);
1825 /* Check for the case where I3 modifies its output, as discussed
1826 above. We don't want to prevent pseudos from being combined
1827 into the address of a MEM, so only prevent the combination if
1828 i1 or i2 set the same MEM. */
1829 if ((inner_dest != dest &&
1830 (!MEM_P (inner_dest)
1831 || rtx_equal_p (i2dest, inner_dest)
1832 || (i1dest && rtx_equal_p (i1dest, inner_dest)))
1833 && (reg_overlap_mentioned_p (i2dest, inner_dest)
1834 || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))))
1836 /* This is the same test done in can_combine_p except we can't test
1837 all_adjacent; we don't have to, since this instruction will stay
1838 in place, thus we are not considering increasing the lifetime of
1839 INNER_DEST.
1841 Also, if this insn sets a function argument, combining it with
1842 something that might need a spill could clobber a previous
1843 function argument; the all_adjacent test in can_combine_p also
1844 checks this; here, we do a more specific test for this case. */
1846 || (REG_P (inner_dest)
1847 && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
1848 && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
1849 GET_MODE (inner_dest))))
1850 || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src)))
1851 return 0;
1853 /* If DEST is used in I3, it is being killed in this insn, so
1854 record that for later. We have to consider paradoxical
1855 subregs here, since they kill the whole register, but we
1856 ignore partial subregs, STRICT_LOW_PART, etc.
1857 Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
1858 STACK_POINTER_REGNUM, since these are always considered to be
1859 live. Similarly for ARG_POINTER_REGNUM if it is fixed. */
1860 subdest = dest;
1861 if (GET_CODE (subdest) == SUBREG
1862 && (GET_MODE_SIZE (GET_MODE (subdest))
1863 >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (subdest)))))
1864 subdest = SUBREG_REG (subdest);
1865 if (pi3dest_killed
1866 && REG_P (subdest)
1867 && reg_referenced_p (subdest, PATTERN (i3))
1868 && REGNO (subdest) != FRAME_POINTER_REGNUM
1869 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
1870 && REGNO (subdest) != HARD_FRAME_POINTER_REGNUM
1871 #endif
1872 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
1873 && (REGNO (subdest) != ARG_POINTER_REGNUM
1874 || ! fixed_regs [REGNO (subdest)])
1875 #endif
1876 && REGNO (subdest) != STACK_POINTER_REGNUM)
1878 if (*pi3dest_killed)
1879 return 0;
1881 *pi3dest_killed = subdest;
1885 else if (GET_CODE (x) == PARALLEL)
1887 int i;
1889 for (i = 0; i < XVECLEN (x, 0); i++)
1890 if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest,
1891 i1_not_in_src, pi3dest_killed))
1892 return 0;
1895 return 1;
1898 /* Return 1 if X is an arithmetic expression that contains a multiplication
1899 and division. We don't count multiplications by powers of two here. */
1901 static int
1902 contains_muldiv (rtx x)
1904 switch (GET_CODE (x))
1906 case MOD: case DIV: case UMOD: case UDIV:
1907 return 1;
1909 case MULT:
1910 return ! (GET_CODE (XEXP (x, 1)) == CONST_INT
1911 && exact_log2 (INTVAL (XEXP (x, 1))) >= 0);
1912 default:
1913 if (BINARY_P (x))
1914 return contains_muldiv (XEXP (x, 0))
1915 || contains_muldiv (XEXP (x, 1));
1917 if (UNARY_P (x))
1918 return contains_muldiv (XEXP (x, 0));
1920 return 0;
1924 /* Determine whether INSN can be used in a combination. Return nonzero if
1925 not. This is used in try_combine to detect early some cases where we
1926 can't perform combinations. */
1928 static int
1929 cant_combine_insn_p (rtx insn)
1931 rtx set;
1932 rtx src, dest;
1934 /* If this isn't really an insn, we can't do anything.
1935 This can occur when flow deletes an insn that it has merged into an
1936 auto-increment address. */
1937 if (! INSN_P (insn))
1938 return 1;
1940 /* Never combine loads and stores involving hard regs that are likely
1941 to be spilled. The register allocator can usually handle such
1942 reg-reg moves by tying. If we allow the combiner to make
1943 substitutions of likely-spilled regs, reload might die.
1944 As an exception, we allow combinations involving fixed regs; these are
1945 not available to the register allocator so there's no risk involved. */
1947 set = single_set (insn);
1948 if (! set)
1949 return 0;
1950 src = SET_SRC (set);
1951 dest = SET_DEST (set);
1952 if (GET_CODE (src) == SUBREG)
1953 src = SUBREG_REG (src);
1954 if (GET_CODE (dest) == SUBREG)
1955 dest = SUBREG_REG (dest);
1956 if (REG_P (src) && REG_P (dest)
1957 && ((REGNO (src) < FIRST_PSEUDO_REGISTER
1958 && ! fixed_regs[REGNO (src)]
1959 && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (REGNO (src))))
1960 || (REGNO (dest) < FIRST_PSEUDO_REGISTER
1961 && ! fixed_regs[REGNO (dest)]
1962 && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (REGNO (dest))))))
1963 return 1;
1965 return 0;
1968 struct likely_spilled_retval_info
1970 unsigned regno, nregs;
1971 unsigned mask;
1974 /* Called via note_stores by likely_spilled_retval_p. Remove from info->mask
1975 hard registers that are known to be written to / clobbered in full. */
1976 static void
1977 likely_spilled_retval_1 (rtx x, rtx set, void *data)
1979 struct likely_spilled_retval_info *info = data;
1980 unsigned regno, nregs;
1981 unsigned new_mask;
1983 if (!REG_P (XEXP (set, 0)))
1984 return;
1985 regno = REGNO (x);
1986 if (regno >= info->regno + info->nregs)
1987 return;
1988 nregs = hard_regno_nregs[regno][GET_MODE (x)];
1989 if (regno + nregs <= info->regno)
1990 return;
1991 new_mask = (2U << (nregs - 1)) - 1;
1992 if (regno < info->regno)
1993 new_mask >>= info->regno - regno;
1994 else
1995 new_mask <<= regno - info->regno;
1996 info->mask &= ~new_mask;
1999 /* Return nonzero iff part of the return value is live during INSN, and
2000 it is likely spilled. This can happen when more than one insn is needed
2001 to copy the return value, e.g. when we consider to combine into the
2002 second copy insn for a complex value. */
2004 static int
2005 likely_spilled_retval_p (rtx insn)
2007 rtx use = BB_END (this_basic_block);
2008 rtx reg, p;
2009 unsigned regno, nregs;
2010 /* We assume here that no machine mode needs more than
2011 32 hard registers when the value overlaps with a register
2012 for which FUNCTION_VALUE_REGNO_P is true. */
2013 unsigned mask;
2014 struct likely_spilled_retval_info info;
2016 if (!NONJUMP_INSN_P (use) || GET_CODE (PATTERN (use)) != USE || insn == use)
2017 return 0;
2018 reg = XEXP (PATTERN (use), 0);
2019 if (!REG_P (reg) || !FUNCTION_VALUE_REGNO_P (REGNO (reg)))
2020 return 0;
2021 regno = REGNO (reg);
2022 nregs = hard_regno_nregs[regno][GET_MODE (reg)];
2023 if (nregs == 1)
2024 return 0;
2025 mask = (2U << (nregs - 1)) - 1;
2027 /* Disregard parts of the return value that are set later. */
2028 info.regno = regno;
2029 info.nregs = nregs;
2030 info.mask = mask;
2031 for (p = PREV_INSN (use); info.mask && p != insn; p = PREV_INSN (p))
2032 if (INSN_P (p))
2033 note_stores (PATTERN (p), likely_spilled_retval_1, &info);
2034 mask = info.mask;
2036 /* Check if any of the (probably) live return value registers is
2037 likely spilled. */
2038 nregs --;
2041 if ((mask & 1 << nregs)
2042 && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (regno + nregs)))
2043 return 1;
2044 } while (nregs--);
2045 return 0;
2048 /* Adjust INSN after we made a change to its destination.
2050 Changing the destination can invalidate notes that say something about
2051 the results of the insn and a LOG_LINK pointing to the insn. */
2053 static void
2054 adjust_for_new_dest (rtx insn)
2056 /* For notes, be conservative and simply remove them. */
2057 remove_reg_equal_equiv_notes (insn);
2059 /* The new insn will have a destination that was previously the destination
2060 of an insn just above it. Call distribute_links to make a LOG_LINK from
2061 the next use of that destination. */
2062 distribute_links (gen_rtx_INSN_LIST (VOIDmode, insn, NULL_RTX));
2064 df_insn_rescan (insn);
2067 /* Return TRUE if combine can reuse reg X in mode MODE.
2068 ADDED_SETS is nonzero if the original set is still required. */
2069 static bool
2070 can_change_dest_mode (rtx x, int added_sets, enum machine_mode mode)
2072 unsigned int regno;
2074 if (!REG_P(x))
2075 return false;
2077 regno = REGNO (x);
2078 /* Allow hard registers if the new mode is legal, and occupies no more
2079 registers than the old mode. */
2080 if (regno < FIRST_PSEUDO_REGISTER)
2081 return (HARD_REGNO_MODE_OK (regno, mode)
2082 && (hard_regno_nregs[regno][GET_MODE (x)]
2083 >= hard_regno_nregs[regno][mode]));
2085 /* Or a pseudo that is only used once. */
2086 return (REG_N_SETS (regno) == 1 && !added_sets
2087 && !REG_USERVAR_P (x));
2091 /* Check whether X, the destination of a set, refers to part of
2092 the register specified by REG. */
2094 static bool
2095 reg_subword_p (rtx x, rtx reg)
2097 /* Check that reg is an integer mode register. */
2098 if (!REG_P (reg) || GET_MODE_CLASS (GET_MODE (reg)) != MODE_INT)
2099 return false;
2101 if (GET_CODE (x) == STRICT_LOW_PART
2102 || GET_CODE (x) == ZERO_EXTRACT)
2103 x = XEXP (x, 0);
2105 return GET_CODE (x) == SUBREG
2106 && SUBREG_REG (x) == reg
2107 && GET_MODE_CLASS (GET_MODE (x)) == MODE_INT;
2111 /* Try to combine the insns I1 and I2 into I3.
2112 Here I1 and I2 appear earlier than I3.
2113 I1 can be zero; then we combine just I2 into I3.
2115 If we are combining three insns and the resulting insn is not recognized,
2116 try splitting it into two insns. If that happens, I2 and I3 are retained
2117 and I1 is pseudo-deleted by turning it into a NOTE. Otherwise, I1 and I2
2118 are pseudo-deleted.
2120 Return 0 if the combination does not work. Then nothing is changed.
2121 If we did the combination, return the insn at which combine should
2122 resume scanning.
2124 Set NEW_DIRECT_JUMP_P to a nonzero value if try_combine creates a
2125 new direct jump instruction. */
2127 static rtx
2128 try_combine (rtx i3, rtx i2, rtx i1, int *new_direct_jump_p)
2130 /* New patterns for I3 and I2, respectively. */
2131 rtx newpat, newi2pat = 0;
2132 rtvec newpat_vec_with_clobbers = 0;
2133 int substed_i2 = 0, substed_i1 = 0;
2134 /* Indicates need to preserve SET in I1 or I2 in I3 if it is not dead. */
2135 int added_sets_1, added_sets_2;
2136 /* Total number of SETs to put into I3. */
2137 int total_sets;
2138 /* Nonzero if I2's body now appears in I3. */
2139 int i2_is_used;
2140 /* INSN_CODEs for new I3, new I2, and user of condition code. */
2141 int insn_code_number, i2_code_number = 0, other_code_number = 0;
2142 /* Contains I3 if the destination of I3 is used in its source, which means
2143 that the old life of I3 is being killed. If that usage is placed into
2144 I2 and not in I3, a REG_DEAD note must be made. */
2145 rtx i3dest_killed = 0;
2146 /* SET_DEST and SET_SRC of I2 and I1. */
2147 rtx i2dest, i2src, i1dest = 0, i1src = 0;
2148 /* PATTERN (I1) and PATTERN (I2), or a copy of it in certain cases. */
2149 rtx i1pat = 0, i2pat = 0;
2150 /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC. */
2151 int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
2152 int i2dest_killed = 0, i1dest_killed = 0;
2153 int i1_feeds_i3 = 0;
2154 /* Notes that must be added to REG_NOTES in I3 and I2. */
2155 rtx new_i3_notes, new_i2_notes;
2156 /* Notes that we substituted I3 into I2 instead of the normal case. */
2157 int i3_subst_into_i2 = 0;
2158 /* Notes that I1, I2 or I3 is a MULT operation. */
2159 int have_mult = 0;
2160 int swap_i2i3 = 0;
2162 int maxreg;
2163 rtx temp;
2164 rtx link;
2165 rtx other_pat = 0;
2166 rtx new_other_notes;
2167 int i;
2169 /* Exit early if one of the insns involved can't be used for
2170 combinations. */
2171 if (cant_combine_insn_p (i3)
2172 || cant_combine_insn_p (i2)
2173 || (i1 && cant_combine_insn_p (i1))
2174 || likely_spilled_retval_p (i3)
2175 /* We also can't do anything if I3 has a
2176 REG_LIBCALL note since we don't want to disrupt the contiguity of a
2177 libcall. */
2178 #if 0
2179 /* ??? This gives worse code, and appears to be unnecessary, since no
2180 pass after flow uses REG_LIBCALL/REG_RETVAL notes. */
2181 || find_reg_note (i3, REG_LIBCALL, NULL_RTX)
2182 #endif
2184 return 0;
2186 combine_attempts++;
2187 undobuf.other_insn = 0;
2189 /* Reset the hard register usage information. */
2190 CLEAR_HARD_REG_SET (newpat_used_regs);
2192 /* If I1 and I2 both feed I3, they can be in any order. To simplify the
2193 code below, set I1 to be the earlier of the two insns. */
2194 if (i1 && DF_INSN_LUID (i1) > DF_INSN_LUID (i2))
2195 temp = i1, i1 = i2, i2 = temp;
2197 added_links_insn = 0;
2199 /* First check for one important special-case that the code below will
2200 not handle. Namely, the case where I1 is zero, I2 is a PARALLEL
2201 and I3 is a SET whose SET_SRC is a SET_DEST in I2. In that case,
2202 we may be able to replace that destination with the destination of I3.
2203 This occurs in the common code where we compute both a quotient and
2204 remainder into a structure, in which case we want to do the computation
2205 directly into the structure to avoid register-register copies.
2207 Note that this case handles both multiple sets in I2 and also
2208 cases where I2 has a number of CLOBBER or PARALLELs.
2210 We make very conservative checks below and only try to handle the
2211 most common cases of this. For example, we only handle the case
2212 where I2 and I3 are adjacent to avoid making difficult register
2213 usage tests. */
2215 if (i1 == 0 && NONJUMP_INSN_P (i3) && GET_CODE (PATTERN (i3)) == SET
2216 && REG_P (SET_SRC (PATTERN (i3)))
2217 && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
2218 && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
2219 && GET_CODE (PATTERN (i2)) == PARALLEL
2220 && ! side_effects_p (SET_DEST (PATTERN (i3)))
2221 /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
2222 below would need to check what is inside (and reg_overlap_mentioned_p
2223 doesn't support those codes anyway). Don't allow those destinations;
2224 the resulting insn isn't likely to be recognized anyway. */
2225 && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
2226 && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
2227 && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
2228 SET_DEST (PATTERN (i3)))
2229 && next_real_insn (i2) == i3)
2231 rtx p2 = PATTERN (i2);
2233 /* Make sure that the destination of I3,
2234 which we are going to substitute into one output of I2,
2235 is not used within another output of I2. We must avoid making this:
2236 (parallel [(set (mem (reg 69)) ...)
2237 (set (reg 69) ...)])
2238 which is not well-defined as to order of actions.
2239 (Besides, reload can't handle output reloads for this.)
2241 The problem can also happen if the dest of I3 is a memory ref,
2242 if another dest in I2 is an indirect memory ref. */
2243 for (i = 0; i < XVECLEN (p2, 0); i++)
2244 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
2245 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
2246 && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
2247 SET_DEST (XVECEXP (p2, 0, i))))
2248 break;
2250 if (i == XVECLEN (p2, 0))
2251 for (i = 0; i < XVECLEN (p2, 0); i++)
2252 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
2253 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
2254 && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
2256 combine_merges++;
2258 subst_insn = i3;
2259 subst_low_luid = DF_INSN_LUID (i2);
2261 added_sets_2 = added_sets_1 = 0;
2262 i2dest = SET_SRC (PATTERN (i3));
2263 i2dest_killed = dead_or_set_p (i2, i2dest);
2265 /* Replace the dest in I2 with our dest and make the resulting
2266 insn the new pattern for I3. Then skip to where we
2267 validate the pattern. Everything was set up above. */
2268 SUBST (SET_DEST (XVECEXP (p2, 0, i)),
2269 SET_DEST (PATTERN (i3)));
2271 newpat = p2;
2272 i3_subst_into_i2 = 1;
2273 goto validate_replacement;
2277 /* If I2 is setting a pseudo to a constant and I3 is setting some
2278 sub-part of it to another constant, merge them by making a new
2279 constant. */
2280 if (i1 == 0
2281 && (temp = single_set (i2)) != 0
2282 && (GET_CODE (SET_SRC (temp)) == CONST_INT
2283 || GET_CODE (SET_SRC (temp)) == CONST_DOUBLE)
2284 && GET_CODE (PATTERN (i3)) == SET
2285 && (GET_CODE (SET_SRC (PATTERN (i3))) == CONST_INT
2286 || GET_CODE (SET_SRC (PATTERN (i3))) == CONST_DOUBLE)
2287 && reg_subword_p (SET_DEST (PATTERN (i3)), SET_DEST (temp)))
2289 rtx dest = SET_DEST (PATTERN (i3));
2290 int offset = -1;
2291 int width = 0;
2293 if (GET_CODE (dest) == ZERO_EXTRACT)
2295 if (GET_CODE (XEXP (dest, 1)) == CONST_INT
2296 && GET_CODE (XEXP (dest, 2)) == CONST_INT)
2298 width = INTVAL (XEXP (dest, 1));
2299 offset = INTVAL (XEXP (dest, 2));
2300 dest = XEXP (dest, 0);
2301 if (BITS_BIG_ENDIAN)
2302 offset = GET_MODE_BITSIZE (GET_MODE (dest)) - width - offset;
2305 else
2307 if (GET_CODE (dest) == STRICT_LOW_PART)
2308 dest = XEXP (dest, 0);
2309 width = GET_MODE_BITSIZE (GET_MODE (dest));
2310 offset = 0;
2313 if (offset >= 0)
2315 /* If this is the low part, we're done. */
2316 if (subreg_lowpart_p (dest))
2318 /* Handle the case where inner is twice the size of outer. */
2319 else if (GET_MODE_BITSIZE (GET_MODE (SET_DEST (temp)))
2320 == 2 * GET_MODE_BITSIZE (GET_MODE (dest)))
2321 offset += GET_MODE_BITSIZE (GET_MODE (dest));
2322 /* Otherwise give up for now. */
2323 else
2324 offset = -1;
2327 if (offset >= 0
2328 && (GET_MODE_BITSIZE (GET_MODE (SET_DEST (temp)))
2329 <= HOST_BITS_PER_WIDE_INT * 2))
2331 HOST_WIDE_INT mhi, ohi, ihi;
2332 HOST_WIDE_INT mlo, olo, ilo;
2333 rtx inner = SET_SRC (PATTERN (i3));
2334 rtx outer = SET_SRC (temp);
2336 if (GET_CODE (outer) == CONST_INT)
2338 olo = INTVAL (outer);
2339 ohi = olo < 0 ? -1 : 0;
2341 else
2343 olo = CONST_DOUBLE_LOW (outer);
2344 ohi = CONST_DOUBLE_HIGH (outer);
2347 if (GET_CODE (inner) == CONST_INT)
2349 ilo = INTVAL (inner);
2350 ihi = ilo < 0 ? -1 : 0;
2352 else
2354 ilo = CONST_DOUBLE_LOW (inner);
2355 ihi = CONST_DOUBLE_HIGH (inner);
2358 if (width < HOST_BITS_PER_WIDE_INT)
2360 mlo = ((unsigned HOST_WIDE_INT) 1 << width) - 1;
2361 mhi = 0;
2363 else if (width < HOST_BITS_PER_WIDE_INT * 2)
2365 mhi = ((unsigned HOST_WIDE_INT) 1
2366 << (width - HOST_BITS_PER_WIDE_INT)) - 1;
2367 mlo = -1;
2369 else
2371 mlo = -1;
2372 mhi = -1;
2375 ilo &= mlo;
2376 ihi &= mhi;
2378 if (offset >= HOST_BITS_PER_WIDE_INT)
2380 mhi = mlo << (offset - HOST_BITS_PER_WIDE_INT);
2381 mlo = 0;
2382 ihi = ilo << (offset - HOST_BITS_PER_WIDE_INT);
2383 ilo = 0;
2385 else if (offset > 0)
2387 mhi = (mhi << offset) | ((unsigned HOST_WIDE_INT) mlo
2388 >> (HOST_BITS_PER_WIDE_INT - offset));
2389 mlo = mlo << offset;
2390 ihi = (ihi << offset) | ((unsigned HOST_WIDE_INT) ilo
2391 >> (HOST_BITS_PER_WIDE_INT - offset));
2392 ilo = ilo << offset;
2395 olo = (olo & ~mlo) | ilo;
2396 ohi = (ohi & ~mhi) | ihi;
2398 combine_merges++;
2399 subst_insn = i3;
2400 subst_low_luid = DF_INSN_LUID (i2);
2401 added_sets_2 = added_sets_1 = 0;
2402 i2dest = SET_DEST (temp);
2403 i2dest_killed = dead_or_set_p (i2, i2dest);
2405 SUBST (SET_SRC (temp),
2406 immed_double_const (olo, ohi, GET_MODE (SET_DEST (temp))));
2408 newpat = PATTERN (i2);
2409 goto validate_replacement;
2413 #ifndef HAVE_cc0
2414 /* If we have no I1 and I2 looks like:
2415 (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
2416 (set Y OP)])
2417 make up a dummy I1 that is
2418 (set Y OP)
2419 and change I2 to be
2420 (set (reg:CC X) (compare:CC Y (const_int 0)))
2422 (We can ignore any trailing CLOBBERs.)
2424 This undoes a previous combination and allows us to match a branch-and-
2425 decrement insn. */
2427 if (i1 == 0 && GET_CODE (PATTERN (i2)) == PARALLEL
2428 && XVECLEN (PATTERN (i2), 0) >= 2
2429 && GET_CODE (XVECEXP (PATTERN (i2), 0, 0)) == SET
2430 && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
2431 == MODE_CC)
2432 && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
2433 && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
2434 && GET_CODE (XVECEXP (PATTERN (i2), 0, 1)) == SET
2435 && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)))
2436 && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
2437 SET_SRC (XVECEXP (PATTERN (i2), 0, 1))))
2439 for (i = XVECLEN (PATTERN (i2), 0) - 1; i >= 2; i--)
2440 if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != CLOBBER)
2441 break;
2443 if (i == 1)
2445 /* We make I1 with the same INSN_UID as I2. This gives it
2446 the same DF_INSN_LUID for value tracking. Our fake I1 will
2447 never appear in the insn stream so giving it the same INSN_UID
2448 as I2 will not cause a problem. */
2450 i1 = gen_rtx_INSN (VOIDmode, INSN_UID (i2), NULL_RTX, i2,
2451 BLOCK_FOR_INSN (i2), INSN_LOCATOR (i2),
2452 XVECEXP (PATTERN (i2), 0, 1), -1, NULL_RTX);
2454 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
2455 SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
2456 SET_DEST (PATTERN (i1)));
2459 #endif
2461 /* Verify that I2 and I1 are valid for combining. */
2462 if (! can_combine_p (i2, i3, i1, NULL_RTX, &i2dest, &i2src)
2463 || (i1 && ! can_combine_p (i1, i3, NULL_RTX, i2, &i1dest, &i1src)))
2465 undo_all ();
2466 return 0;
2469 /* Record whether I2DEST is used in I2SRC and similarly for the other
2470 cases. Knowing this will help in register status updating below. */
2471 i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
2472 i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
2473 i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
2474 i2dest_killed = dead_or_set_p (i2, i2dest);
2475 i1dest_killed = i1 && dead_or_set_p (i1, i1dest);
2477 /* See if I1 directly feeds into I3. It does if I1DEST is not used
2478 in I2SRC. */
2479 i1_feeds_i3 = i1 && ! reg_overlap_mentioned_p (i1dest, i2src);
2481 /* Ensure that I3's pattern can be the destination of combines. */
2482 if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest,
2483 i1 && i2dest_in_i1src && i1_feeds_i3,
2484 &i3dest_killed))
2486 undo_all ();
2487 return 0;
2490 /* See if any of the insns is a MULT operation. Unless one is, we will
2491 reject a combination that is, since it must be slower. Be conservative
2492 here. */
2493 if (GET_CODE (i2src) == MULT
2494 || (i1 != 0 && GET_CODE (i1src) == MULT)
2495 || (GET_CODE (PATTERN (i3)) == SET
2496 && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
2497 have_mult = 1;
2499 /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
2500 We used to do this EXCEPT in one case: I3 has a post-inc in an
2501 output operand. However, that exception can give rise to insns like
2502 mov r3,(r3)+
2503 which is a famous insn on the PDP-11 where the value of r3 used as the
2504 source was model-dependent. Avoid this sort of thing. */
2506 #if 0
2507 if (!(GET_CODE (PATTERN (i3)) == SET
2508 && REG_P (SET_SRC (PATTERN (i3)))
2509 && MEM_P (SET_DEST (PATTERN (i3)))
2510 && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
2511 || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
2512 /* It's not the exception. */
2513 #endif
2514 #ifdef AUTO_INC_DEC
2515 for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
2516 if (REG_NOTE_KIND (link) == REG_INC
2517 && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
2518 || (i1 != 0
2519 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
2521 undo_all ();
2522 return 0;
2524 #endif
2526 /* See if the SETs in I1 or I2 need to be kept around in the merged
2527 instruction: whenever the value set there is still needed past I3.
2528 For the SETs in I2, this is easy: we see if I2DEST dies or is set in I3.
2530 For the SET in I1, we have two cases: If I1 and I2 independently
2531 feed into I3, the set in I1 needs to be kept around if I1DEST dies
2532 or is set in I3. Otherwise (if I1 feeds I2 which feeds I3), the set
2533 in I1 needs to be kept around unless I1DEST dies or is set in either
2534 I2 or I3. We can distinguish these cases by seeing if I2SRC mentions
2535 I1DEST. If so, we know I1 feeds into I2. */
2537 added_sets_2 = ! dead_or_set_p (i3, i2dest);
2539 added_sets_1
2540 = i1 && ! (i1_feeds_i3 ? dead_or_set_p (i3, i1dest)
2541 : (dead_or_set_p (i3, i1dest) || dead_or_set_p (i2, i1dest)));
2543 /* If the set in I2 needs to be kept around, we must make a copy of
2544 PATTERN (I2), so that when we substitute I1SRC for I1DEST in
2545 PATTERN (I2), we are only substituting for the original I1DEST, not into
2546 an already-substituted copy. This also prevents making self-referential
2547 rtx. If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
2548 I2DEST. */
2550 if (added_sets_2)
2552 if (GET_CODE (PATTERN (i2)) == PARALLEL)
2553 i2pat = gen_rtx_SET (VOIDmode, i2dest, copy_rtx (i2src));
2554 else
2555 i2pat = copy_rtx (PATTERN (i2));
2558 if (added_sets_1)
2560 if (GET_CODE (PATTERN (i1)) == PARALLEL)
2561 i1pat = gen_rtx_SET (VOIDmode, i1dest, copy_rtx (i1src));
2562 else
2563 i1pat = copy_rtx (PATTERN (i1));
2566 combine_merges++;
2568 /* Substitute in the latest insn for the regs set by the earlier ones. */
2570 maxreg = max_reg_num ();
2572 subst_insn = i3;
2574 #ifndef HAVE_cc0
2575 /* Many machines that don't use CC0 have insns that can both perform an
2576 arithmetic operation and set the condition code. These operations will
2577 be represented as a PARALLEL with the first element of the vector
2578 being a COMPARE of an arithmetic operation with the constant zero.
2579 The second element of the vector will set some pseudo to the result
2580 of the same arithmetic operation. If we simplify the COMPARE, we won't
2581 match such a pattern and so will generate an extra insn. Here we test
2582 for this case, where both the comparison and the operation result are
2583 needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
2584 I2SRC. Later we will make the PARALLEL that contains I2. */
2586 if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
2587 && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
2588 && XEXP (SET_SRC (PATTERN (i3)), 1) == const0_rtx
2589 && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
2591 #ifdef SELECT_CC_MODE
2592 rtx *cc_use;
2593 enum machine_mode compare_mode;
2594 #endif
2596 newpat = PATTERN (i3);
2597 SUBST (XEXP (SET_SRC (newpat), 0), i2src);
2599 i2_is_used = 1;
2601 #ifdef SELECT_CC_MODE
2602 /* See if a COMPARE with the operand we substituted in should be done
2603 with the mode that is currently being used. If not, do the same
2604 processing we do in `subst' for a SET; namely, if the destination
2605 is used only once, try to replace it with a register of the proper
2606 mode and also replace the COMPARE. */
2607 if (undobuf.other_insn == 0
2608 && (cc_use = find_single_use (SET_DEST (newpat), i3,
2609 &undobuf.other_insn))
2610 && ((compare_mode = SELECT_CC_MODE (GET_CODE (*cc_use),
2611 i2src, const0_rtx))
2612 != GET_MODE (SET_DEST (newpat))))
2614 if (can_change_dest_mode(SET_DEST (newpat), added_sets_2,
2615 compare_mode))
2617 unsigned int regno = REGNO (SET_DEST (newpat));
2618 rtx new_dest;
2620 if (regno < FIRST_PSEUDO_REGISTER)
2621 new_dest = gen_rtx_REG (compare_mode, regno);
2622 else
2624 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
2625 new_dest = regno_reg_rtx[regno];
2628 SUBST (SET_DEST (newpat), new_dest);
2629 SUBST (XEXP (*cc_use, 0), new_dest);
2630 SUBST (SET_SRC (newpat),
2631 gen_rtx_COMPARE (compare_mode, i2src, const0_rtx));
2633 else
2634 undobuf.other_insn = 0;
2636 #endif
2638 else
2639 #endif
2641 /* It is possible that the source of I2 or I1 may be performing
2642 an unneeded operation, such as a ZERO_EXTEND of something
2643 that is known to have the high part zero. Handle that case
2644 by letting subst look at the innermost one of them.
2646 Another way to do this would be to have a function that tries
2647 to simplify a single insn instead of merging two or more
2648 insns. We don't do this because of the potential of infinite
2649 loops and because of the potential extra memory required.
2650 However, doing it the way we are is a bit of a kludge and
2651 doesn't catch all cases.
2653 But only do this if -fexpensive-optimizations since it slows
2654 things down and doesn't usually win.
2656 This is not done in the COMPARE case above because the
2657 unmodified I2PAT is used in the PARALLEL and so a pattern
2658 with a modified I2SRC would not match. */
2660 if (flag_expensive_optimizations)
2662 /* Pass pc_rtx so no substitutions are done, just
2663 simplifications. */
2664 if (i1)
2666 subst_low_luid = DF_INSN_LUID (i1);
2667 i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0);
2669 else
2671 subst_low_luid = DF_INSN_LUID (i2);
2672 i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0);
2676 n_occurrences = 0; /* `subst' counts here */
2678 /* If I1 feeds into I2 (not into I3) and I1DEST is in I1SRC, we
2679 need to make a unique copy of I2SRC each time we substitute it
2680 to avoid self-referential rtl. */
2682 subst_low_luid = DF_INSN_LUID (i2);
2683 newpat = subst (PATTERN (i3), i2dest, i2src, 0,
2684 ! i1_feeds_i3 && i1dest_in_i1src);
2685 substed_i2 = 1;
2687 /* Record whether i2's body now appears within i3's body. */
2688 i2_is_used = n_occurrences;
2691 /* If we already got a failure, don't try to do more. Otherwise,
2692 try to substitute in I1 if we have it. */
2694 if (i1 && GET_CODE (newpat) != CLOBBER)
2696 /* Before we can do this substitution, we must redo the test done
2697 above (see detailed comments there) that ensures that I1DEST
2698 isn't mentioned in any SETs in NEWPAT that are field assignments. */
2700 if (! combinable_i3pat (NULL_RTX, &newpat, i1dest, NULL_RTX,
2701 0, (rtx*) 0))
2703 undo_all ();
2704 return 0;
2707 n_occurrences = 0;
2708 subst_low_luid = DF_INSN_LUID (i1);
2709 newpat = subst (newpat, i1dest, i1src, 0, 0);
2710 substed_i1 = 1;
2713 /* Fail if an autoincrement side-effect has been duplicated. Be careful
2714 to count all the ways that I2SRC and I1SRC can be used. */
2715 if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
2716 && i2_is_used + added_sets_2 > 1)
2717 || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
2718 && (n_occurrences + added_sets_1 + (added_sets_2 && ! i1_feeds_i3)
2719 > 1))
2720 /* Fail if we tried to make a new register. */
2721 || max_reg_num () != maxreg
2722 /* Fail if we couldn't do something and have a CLOBBER. */
2723 || GET_CODE (newpat) == CLOBBER
2724 /* Fail if this new pattern is a MULT and we didn't have one before
2725 at the outer level. */
2726 || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
2727 && ! have_mult))
2729 undo_all ();
2730 return 0;
2733 /* If the actions of the earlier insns must be kept
2734 in addition to substituting them into the latest one,
2735 we must make a new PARALLEL for the latest insn
2736 to hold additional the SETs. */
2738 if (added_sets_1 || added_sets_2)
2740 combine_extras++;
2742 if (GET_CODE (newpat) == PARALLEL)
2744 rtvec old = XVEC (newpat, 0);
2745 total_sets = XVECLEN (newpat, 0) + added_sets_1 + added_sets_2;
2746 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
2747 memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
2748 sizeof (old->elem[0]) * old->num_elem);
2750 else
2752 rtx old = newpat;
2753 total_sets = 1 + added_sets_1 + added_sets_2;
2754 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
2755 XVECEXP (newpat, 0, 0) = old;
2758 if (added_sets_1)
2759 XVECEXP (newpat, 0, --total_sets) = i1pat;
2761 if (added_sets_2)
2763 /* If there is no I1, use I2's body as is. We used to also not do
2764 the subst call below if I2 was substituted into I3,
2765 but that could lose a simplification. */
2766 if (i1 == 0)
2767 XVECEXP (newpat, 0, --total_sets) = i2pat;
2768 else
2769 /* See comment where i2pat is assigned. */
2770 XVECEXP (newpat, 0, --total_sets)
2771 = subst (i2pat, i1dest, i1src, 0, 0);
2775 /* We come here when we are replacing a destination in I2 with the
2776 destination of I3. */
2777 validate_replacement:
2779 /* Note which hard regs this insn has as inputs. */
2780 mark_used_regs_combine (newpat);
2782 /* If recog_for_combine fails, it strips existing clobbers. If we'll
2783 consider splitting this pattern, we might need these clobbers. */
2784 if (i1 && GET_CODE (newpat) == PARALLEL
2785 && GET_CODE (XVECEXP (newpat, 0, XVECLEN (newpat, 0) - 1)) == CLOBBER)
2787 int len = XVECLEN (newpat, 0);
2789 newpat_vec_with_clobbers = rtvec_alloc (len);
2790 for (i = 0; i < len; i++)
2791 RTVEC_ELT (newpat_vec_with_clobbers, i) = XVECEXP (newpat, 0, i);
2794 /* Is the result of combination a valid instruction? */
2795 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2797 /* If the result isn't valid, see if it is a PARALLEL of two SETs where
2798 the second SET's destination is a register that is unused and isn't
2799 marked as an instruction that might trap in an EH region. In that case,
2800 we just need the first SET. This can occur when simplifying a divmod
2801 insn. We *must* test for this case here because the code below that
2802 splits two independent SETs doesn't handle this case correctly when it
2803 updates the register status.
2805 It's pointless doing this if we originally had two sets, one from
2806 i3, and one from i2. Combining then splitting the parallel results
2807 in the original i2 again plus an invalid insn (which we delete).
2808 The net effect is only to move instructions around, which makes
2809 debug info less accurate.
2811 Also check the case where the first SET's destination is unused.
2812 That would not cause incorrect code, but does cause an unneeded
2813 insn to remain. */
2815 if (insn_code_number < 0
2816 && !(added_sets_2 && i1 == 0)
2817 && GET_CODE (newpat) == PARALLEL
2818 && XVECLEN (newpat, 0) == 2
2819 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2820 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2821 && asm_noperands (newpat) < 0)
2823 rtx set0 = XVECEXP (newpat, 0, 0);
2824 rtx set1 = XVECEXP (newpat, 0, 1);
2825 rtx note;
2827 if (((REG_P (SET_DEST (set1))
2828 && find_reg_note (i3, REG_UNUSED, SET_DEST (set1)))
2829 || (GET_CODE (SET_DEST (set1)) == SUBREG
2830 && find_reg_note (i3, REG_UNUSED, SUBREG_REG (SET_DEST (set1)))))
2831 && (!(note = find_reg_note (i3, REG_EH_REGION, NULL_RTX))
2832 || INTVAL (XEXP (note, 0)) <= 0)
2833 && ! side_effects_p (SET_SRC (set1)))
2835 newpat = set0;
2836 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2839 else if (((REG_P (SET_DEST (set0))
2840 && find_reg_note (i3, REG_UNUSED, SET_DEST (set0)))
2841 || (GET_CODE (SET_DEST (set0)) == SUBREG
2842 && find_reg_note (i3, REG_UNUSED,
2843 SUBREG_REG (SET_DEST (set0)))))
2844 && (!(note = find_reg_note (i3, REG_EH_REGION, NULL_RTX))
2845 || INTVAL (XEXP (note, 0)) <= 0)
2846 && ! side_effects_p (SET_SRC (set0)))
2848 newpat = set1;
2849 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2851 if (insn_code_number >= 0)
2853 /* If we will be able to accept this, we have made a
2854 change to the destination of I3. This requires us to
2855 do a few adjustments. */
2857 PATTERN (i3) = newpat;
2858 adjust_for_new_dest (i3);
2863 /* If we were combining three insns and the result is a simple SET
2864 with no ASM_OPERANDS that wasn't recognized, try to split it into two
2865 insns. There are two ways to do this. It can be split using a
2866 machine-specific method (like when you have an addition of a large
2867 constant) or by combine in the function find_split_point. */
2869 if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
2870 && asm_noperands (newpat) < 0)
2872 rtx m_split, *split;
2874 /* See if the MD file can split NEWPAT. If it can't, see if letting it
2875 use I2DEST as a scratch register will help. In the latter case,
2876 convert I2DEST to the mode of the source of NEWPAT if we can. */
2878 m_split = split_insns (newpat, i3);
2880 /* We can only use I2DEST as a scratch reg if it doesn't overlap any
2881 inputs of NEWPAT. */
2883 /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
2884 possible to try that as a scratch reg. This would require adding
2885 more code to make it work though. */
2887 if (m_split == 0 && ! reg_overlap_mentioned_p (i2dest, newpat))
2889 enum machine_mode new_mode = GET_MODE (SET_DEST (newpat));
2891 /* First try to split using the original register as a
2892 scratch register. */
2893 m_split = split_insns (gen_rtx_PARALLEL
2894 (VOIDmode,
2895 gen_rtvec (2, newpat,
2896 gen_rtx_CLOBBER (VOIDmode,
2897 i2dest))),
2898 i3);
2900 /* If that didn't work, try changing the mode of I2DEST if
2901 we can. */
2902 if (m_split == 0
2903 && new_mode != GET_MODE (i2dest)
2904 && new_mode != VOIDmode
2905 && can_change_dest_mode (i2dest, added_sets_2, new_mode))
2907 enum machine_mode old_mode = GET_MODE (i2dest);
2908 rtx ni2dest;
2910 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
2911 ni2dest = gen_rtx_REG (new_mode, REGNO (i2dest));
2912 else
2914 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], new_mode);
2915 ni2dest = regno_reg_rtx[REGNO (i2dest)];
2918 m_split = split_insns (gen_rtx_PARALLEL
2919 (VOIDmode,
2920 gen_rtvec (2, newpat,
2921 gen_rtx_CLOBBER (VOIDmode,
2922 ni2dest))),
2923 i3);
2925 if (m_split == 0
2926 && REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
2928 struct undo *buf;
2930 PUT_MODE (regno_reg_rtx[REGNO (i2dest)], old_mode);
2931 buf = undobuf.undos;
2932 undobuf.undos = buf->next;
2933 buf->next = undobuf.frees;
2934 undobuf.frees = buf;
2939 /* If recog_for_combine has discarded clobbers, try to use them
2940 again for the split. */
2941 if (m_split == 0 && newpat_vec_with_clobbers)
2942 m_split
2943 = split_insns (gen_rtx_PARALLEL (VOIDmode,
2944 newpat_vec_with_clobbers), i3);
2946 if (m_split && NEXT_INSN (m_split) == NULL_RTX)
2948 m_split = PATTERN (m_split);
2949 insn_code_number = recog_for_combine (&m_split, i3, &new_i3_notes);
2950 if (insn_code_number >= 0)
2951 newpat = m_split;
2953 else if (m_split && NEXT_INSN (NEXT_INSN (m_split)) == NULL_RTX
2954 && (next_real_insn (i2) == i3
2955 || ! use_crosses_set_p (PATTERN (m_split), DF_INSN_LUID (i2))))
2957 rtx i2set, i3set;
2958 rtx newi3pat = PATTERN (NEXT_INSN (m_split));
2959 newi2pat = PATTERN (m_split);
2961 i3set = single_set (NEXT_INSN (m_split));
2962 i2set = single_set (m_split);
2964 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
2966 /* If I2 or I3 has multiple SETs, we won't know how to track
2967 register status, so don't use these insns. If I2's destination
2968 is used between I2 and I3, we also can't use these insns. */
2970 if (i2_code_number >= 0 && i2set && i3set
2971 && (next_real_insn (i2) == i3
2972 || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
2973 insn_code_number = recog_for_combine (&newi3pat, i3,
2974 &new_i3_notes);
2975 if (insn_code_number >= 0)
2976 newpat = newi3pat;
2978 /* It is possible that both insns now set the destination of I3.
2979 If so, we must show an extra use of it. */
2981 if (insn_code_number >= 0)
2983 rtx new_i3_dest = SET_DEST (i3set);
2984 rtx new_i2_dest = SET_DEST (i2set);
2986 while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
2987 || GET_CODE (new_i3_dest) == STRICT_LOW_PART
2988 || GET_CODE (new_i3_dest) == SUBREG)
2989 new_i3_dest = XEXP (new_i3_dest, 0);
2991 while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
2992 || GET_CODE (new_i2_dest) == STRICT_LOW_PART
2993 || GET_CODE (new_i2_dest) == SUBREG)
2994 new_i2_dest = XEXP (new_i2_dest, 0);
2996 if (REG_P (new_i3_dest)
2997 && REG_P (new_i2_dest)
2998 && REGNO (new_i3_dest) == REGNO (new_i2_dest))
2999 INC_REG_N_SETS (REGNO (new_i2_dest), 1);
3003 /* If we can split it and use I2DEST, go ahead and see if that
3004 helps things be recognized. Verify that none of the registers
3005 are set between I2 and I3. */
3006 if (insn_code_number < 0 && (split = find_split_point (&newpat, i3)) != 0
3007 #ifdef HAVE_cc0
3008 && REG_P (i2dest)
3009 #endif
3010 /* We need I2DEST in the proper mode. If it is a hard register
3011 or the only use of a pseudo, we can change its mode.
3012 Make sure we don't change a hard register to have a mode that
3013 isn't valid for it, or change the number of registers. */
3014 && (GET_MODE (*split) == GET_MODE (i2dest)
3015 || GET_MODE (*split) == VOIDmode
3016 || can_change_dest_mode (i2dest, added_sets_2,
3017 GET_MODE (*split)))
3018 && (next_real_insn (i2) == i3
3019 || ! use_crosses_set_p (*split, DF_INSN_LUID (i2)))
3020 /* We can't overwrite I2DEST if its value is still used by
3021 NEWPAT. */
3022 && ! reg_referenced_p (i2dest, newpat))
3024 rtx newdest = i2dest;
3025 enum rtx_code split_code = GET_CODE (*split);
3026 enum machine_mode split_mode = GET_MODE (*split);
3027 bool subst_done = false;
3028 newi2pat = NULL_RTX;
3030 /* Get NEWDEST as a register in the proper mode. We have already
3031 validated that we can do this. */
3032 if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
3034 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3035 newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
3036 else
3038 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], split_mode);
3039 newdest = regno_reg_rtx[REGNO (i2dest)];
3043 /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
3044 an ASHIFT. This can occur if it was inside a PLUS and hence
3045 appeared to be a memory address. This is a kludge. */
3046 if (split_code == MULT
3047 && GET_CODE (XEXP (*split, 1)) == CONST_INT
3048 && INTVAL (XEXP (*split, 1)) > 0
3049 && (i = exact_log2 (INTVAL (XEXP (*split, 1)))) >= 0)
3051 SUBST (*split, gen_rtx_ASHIFT (split_mode,
3052 XEXP (*split, 0), GEN_INT (i)));
3053 /* Update split_code because we may not have a multiply
3054 anymore. */
3055 split_code = GET_CODE (*split);
3058 #ifdef INSN_SCHEDULING
3059 /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
3060 be written as a ZERO_EXTEND. */
3061 if (split_code == SUBREG && MEM_P (SUBREG_REG (*split)))
3063 #ifdef LOAD_EXTEND_OP
3064 /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
3065 what it really is. */
3066 if (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (*split)))
3067 == SIGN_EXTEND)
3068 SUBST (*split, gen_rtx_SIGN_EXTEND (split_mode,
3069 SUBREG_REG (*split)));
3070 else
3071 #endif
3072 SUBST (*split, gen_rtx_ZERO_EXTEND (split_mode,
3073 SUBREG_REG (*split)));
3075 #endif
3077 /* Attempt to split binary operators using arithmetic identities. */
3078 if (BINARY_P (SET_SRC (newpat))
3079 && split_mode == GET_MODE (SET_SRC (newpat))
3080 && ! side_effects_p (SET_SRC (newpat)))
3082 rtx setsrc = SET_SRC (newpat);
3083 enum machine_mode mode = GET_MODE (setsrc);
3084 enum rtx_code code = GET_CODE (setsrc);
3085 rtx src_op0 = XEXP (setsrc, 0);
3086 rtx src_op1 = XEXP (setsrc, 1);
3088 /* Split "X = Y op Y" as "Z = Y; X = Z op Z". */
3089 if (rtx_equal_p (src_op0, src_op1))
3091 newi2pat = gen_rtx_SET (VOIDmode, newdest, src_op0);
3092 SUBST (XEXP (setsrc, 0), newdest);
3093 SUBST (XEXP (setsrc, 1), newdest);
3094 subst_done = true;
3096 /* Split "((P op Q) op R) op S" where op is PLUS or MULT. */
3097 else if ((code == PLUS || code == MULT)
3098 && GET_CODE (src_op0) == code
3099 && GET_CODE (XEXP (src_op0, 0)) == code
3100 && (INTEGRAL_MODE_P (mode)
3101 || (FLOAT_MODE_P (mode)
3102 && flag_unsafe_math_optimizations)))
3104 rtx p = XEXP (XEXP (src_op0, 0), 0);
3105 rtx q = XEXP (XEXP (src_op0, 0), 1);
3106 rtx r = XEXP (src_op0, 1);
3107 rtx s = src_op1;
3109 /* Split both "((X op Y) op X) op Y" and
3110 "((X op Y) op Y) op X" as "T op T" where T is
3111 "X op Y". */
3112 if ((rtx_equal_p (p,r) && rtx_equal_p (q,s))
3113 || (rtx_equal_p (p,s) && rtx_equal_p (q,r)))
3115 newi2pat = gen_rtx_SET (VOIDmode, newdest,
3116 XEXP (src_op0, 0));
3117 SUBST (XEXP (setsrc, 0), newdest);
3118 SUBST (XEXP (setsrc, 1), newdest);
3119 subst_done = true;
3121 /* Split "((X op X) op Y) op Y)" as "T op T" where
3122 T is "X op Y". */
3123 else if (rtx_equal_p (p,q) && rtx_equal_p (r,s))
3125 rtx tmp = simplify_gen_binary (code, mode, p, r);
3126 newi2pat = gen_rtx_SET (VOIDmode, newdest, tmp);
3127 SUBST (XEXP (setsrc, 0), newdest);
3128 SUBST (XEXP (setsrc, 1), newdest);
3129 subst_done = true;
3134 if (!subst_done)
3136 newi2pat = gen_rtx_SET (VOIDmode, newdest, *split);
3137 SUBST (*split, newdest);
3140 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3142 /* recog_for_combine might have added CLOBBERs to newi2pat.
3143 Make sure NEWPAT does not depend on the clobbered regs. */
3144 if (GET_CODE (newi2pat) == PARALLEL)
3145 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3146 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3148 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3149 if (reg_overlap_mentioned_p (reg, newpat))
3151 undo_all ();
3152 return 0;
3156 /* If the split point was a MULT and we didn't have one before,
3157 don't use one now. */
3158 if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
3159 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3163 /* Check for a case where we loaded from memory in a narrow mode and
3164 then sign extended it, but we need both registers. In that case,
3165 we have a PARALLEL with both loads from the same memory location.
3166 We can split this into a load from memory followed by a register-register
3167 copy. This saves at least one insn, more if register allocation can
3168 eliminate the copy.
3170 We cannot do this if the destination of the first assignment is a
3171 condition code register or cc0. We eliminate this case by making sure
3172 the SET_DEST and SET_SRC have the same mode.
3174 We cannot do this if the destination of the second assignment is
3175 a register that we have already assumed is zero-extended. Similarly
3176 for a SUBREG of such a register. */
3178 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3179 && GET_CODE (newpat) == PARALLEL
3180 && XVECLEN (newpat, 0) == 2
3181 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3182 && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
3183 && (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0)))
3184 == GET_MODE (SET_SRC (XVECEXP (newpat, 0, 0))))
3185 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3186 && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3187 XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
3188 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3189 DF_INSN_LUID (i2))
3190 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3191 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3192 && ! (temp = SET_DEST (XVECEXP (newpat, 0, 1)),
3193 (REG_P (temp)
3194 && reg_stat[REGNO (temp)].nonzero_bits != 0
3195 && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
3196 && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
3197 && (reg_stat[REGNO (temp)].nonzero_bits
3198 != GET_MODE_MASK (word_mode))))
3199 && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
3200 && (temp = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
3201 (REG_P (temp)
3202 && reg_stat[REGNO (temp)].nonzero_bits != 0
3203 && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
3204 && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
3205 && (reg_stat[REGNO (temp)].nonzero_bits
3206 != GET_MODE_MASK (word_mode)))))
3207 && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3208 SET_SRC (XVECEXP (newpat, 0, 1)))
3209 && ! find_reg_note (i3, REG_UNUSED,
3210 SET_DEST (XVECEXP (newpat, 0, 0))))
3212 rtx ni2dest;
3214 newi2pat = XVECEXP (newpat, 0, 0);
3215 ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
3216 newpat = XVECEXP (newpat, 0, 1);
3217 SUBST (SET_SRC (newpat),
3218 gen_lowpart (GET_MODE (SET_SRC (newpat)), ni2dest));
3219 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3221 if (i2_code_number >= 0)
3222 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3224 if (insn_code_number >= 0)
3225 swap_i2i3 = 1;
3228 /* Similarly, check for a case where we have a PARALLEL of two independent
3229 SETs but we started with three insns. In this case, we can do the sets
3230 as two separate insns. This case occurs when some SET allows two
3231 other insns to combine, but the destination of that SET is still live. */
3233 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3234 && GET_CODE (newpat) == PARALLEL
3235 && XVECLEN (newpat, 0) == 2
3236 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3237 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
3238 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
3239 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3240 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3241 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3242 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3243 DF_INSN_LUID (i2))
3244 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3245 XVECEXP (newpat, 0, 0))
3246 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
3247 XVECEXP (newpat, 0, 1))
3248 && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
3249 && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1))))
3250 #ifdef HAVE_cc0
3251 /* We cannot split the parallel into two sets if both sets
3252 reference cc0. */
3253 && ! (reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 0))
3254 && reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 1)))
3255 #endif
3258 /* Normally, it doesn't matter which of the two is done first,
3259 but it does if one references cc0. In that case, it has to
3260 be first. */
3261 #ifdef HAVE_cc0
3262 if (reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 0)))
3264 newi2pat = XVECEXP (newpat, 0, 0);
3265 newpat = XVECEXP (newpat, 0, 1);
3267 else
3268 #endif
3270 newi2pat = XVECEXP (newpat, 0, 1);
3271 newpat = XVECEXP (newpat, 0, 0);
3274 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3276 if (i2_code_number >= 0)
3277 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3280 /* If it still isn't recognized, fail and change things back the way they
3281 were. */
3282 if ((insn_code_number < 0
3283 /* Is the result a reasonable ASM_OPERANDS? */
3284 && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
3286 undo_all ();
3287 return 0;
3290 /* If we had to change another insn, make sure it is valid also. */
3291 if (undobuf.other_insn)
3293 CLEAR_HARD_REG_SET (newpat_used_regs);
3295 other_pat = PATTERN (undobuf.other_insn);
3296 other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
3297 &new_other_notes);
3299 if (other_code_number < 0 && ! check_asm_operands (other_pat))
3301 undo_all ();
3302 return 0;
3306 #ifdef HAVE_cc0
3307 /* If I2 is the CC0 setter and I3 is the CC0 user then check whether
3308 they are adjacent to each other or not. */
3310 rtx p = prev_nonnote_insn (i3);
3311 if (p && p != i2 && NONJUMP_INSN_P (p) && newi2pat
3312 && sets_cc0_p (newi2pat))
3314 undo_all ();
3315 return 0;
3318 #endif
3320 /* Only allow this combination if insn_rtx_costs reports that the
3321 replacement instructions are cheaper than the originals. */
3322 if (!combine_validate_cost (i1, i2, i3, newpat, newi2pat, other_pat))
3324 undo_all ();
3325 return 0;
3328 /* We now know that we can do this combination. Merge the insns and
3329 update the status of registers and LOG_LINKS. */
3331 if (undobuf.other_insn)
3333 rtx note, next;
3335 PATTERN (undobuf.other_insn) = other_pat;
3337 /* If any of the notes in OTHER_INSN were REG_UNUSED, ensure that they
3338 are still valid. Then add any non-duplicate notes added by
3339 recog_for_combine. */
3340 for (note = REG_NOTES (undobuf.other_insn); note; note = next)
3342 next = XEXP (note, 1);
3344 if (REG_NOTE_KIND (note) == REG_UNUSED
3345 && ! reg_set_p (XEXP (note, 0), PATTERN (undobuf.other_insn)))
3346 remove_note (undobuf.other_insn, note);
3349 distribute_notes (new_other_notes, undobuf.other_insn,
3350 undobuf.other_insn, NULL_RTX, NULL_RTX, NULL_RTX);
3353 if (swap_i2i3)
3355 rtx insn;
3356 rtx link;
3357 rtx ni2dest;
3359 /* I3 now uses what used to be its destination and which is now
3360 I2's destination. This requires us to do a few adjustments. */
3361 PATTERN (i3) = newpat;
3362 adjust_for_new_dest (i3);
3364 /* We need a LOG_LINK from I3 to I2. But we used to have one,
3365 so we still will.
3367 However, some later insn might be using I2's dest and have
3368 a LOG_LINK pointing at I3. We must remove this link.
3369 The simplest way to remove the link is to point it at I1,
3370 which we know will be a NOTE. */
3372 /* newi2pat is usually a SET here; however, recog_for_combine might
3373 have added some clobbers. */
3374 if (GET_CODE (newi2pat) == PARALLEL)
3375 ni2dest = SET_DEST (XVECEXP (newi2pat, 0, 0));
3376 else
3377 ni2dest = SET_DEST (newi2pat);
3379 for (insn = NEXT_INSN (i3);
3380 insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
3381 || insn != BB_HEAD (this_basic_block->next_bb));
3382 insn = NEXT_INSN (insn))
3384 if (INSN_P (insn) && reg_referenced_p (ni2dest, PATTERN (insn)))
3386 for (link = LOG_LINKS (insn); link;
3387 link = XEXP (link, 1))
3388 if (XEXP (link, 0) == i3)
3389 XEXP (link, 0) = i1;
3391 break;
3397 rtx i3notes, i2notes, i1notes = 0;
3398 rtx i3links, i2links, i1links = 0;
3399 rtx midnotes = 0;
3400 unsigned int regno;
3401 /* Compute which registers we expect to eliminate. newi2pat may be setting
3402 either i3dest or i2dest, so we must check it. Also, i1dest may be the
3403 same as i3dest, in which case newi2pat may be setting i1dest. */
3404 rtx elim_i2 = ((newi2pat && reg_set_p (i2dest, newi2pat))
3405 || i2dest_in_i2src || i2dest_in_i1src
3406 || !i2dest_killed
3407 ? 0 : i2dest);
3408 rtx elim_i1 = (i1 == 0 || i1dest_in_i1src
3409 || (newi2pat && reg_set_p (i1dest, newi2pat))
3410 || !i1dest_killed
3411 ? 0 : i1dest);
3413 /* Get the old REG_NOTES and LOG_LINKS from all our insns and
3414 clear them. */
3415 i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
3416 i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
3417 if (i1)
3418 i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
3420 /* Ensure that we do not have something that should not be shared but
3421 occurs multiple times in the new insns. Check this by first
3422 resetting all the `used' flags and then copying anything is shared. */
3424 reset_used_flags (i3notes);
3425 reset_used_flags (i2notes);
3426 reset_used_flags (i1notes);
3427 reset_used_flags (newpat);
3428 reset_used_flags (newi2pat);
3429 if (undobuf.other_insn)
3430 reset_used_flags (PATTERN (undobuf.other_insn));
3432 i3notes = copy_rtx_if_shared (i3notes);
3433 i2notes = copy_rtx_if_shared (i2notes);
3434 i1notes = copy_rtx_if_shared (i1notes);
3435 newpat = copy_rtx_if_shared (newpat);
3436 newi2pat = copy_rtx_if_shared (newi2pat);
3437 if (undobuf.other_insn)
3438 reset_used_flags (PATTERN (undobuf.other_insn));
3440 INSN_CODE (i3) = insn_code_number;
3441 PATTERN (i3) = newpat;
3443 if (CALL_P (i3) && CALL_INSN_FUNCTION_USAGE (i3))
3445 rtx call_usage = CALL_INSN_FUNCTION_USAGE (i3);
3447 reset_used_flags (call_usage);
3448 call_usage = copy_rtx (call_usage);
3450 if (substed_i2)
3451 replace_rtx (call_usage, i2dest, i2src);
3453 if (substed_i1)
3454 replace_rtx (call_usage, i1dest, i1src);
3456 CALL_INSN_FUNCTION_USAGE (i3) = call_usage;
3459 if (undobuf.other_insn)
3460 INSN_CODE (undobuf.other_insn) = other_code_number;
3462 /* We had one special case above where I2 had more than one set and
3463 we replaced a destination of one of those sets with the destination
3464 of I3. In that case, we have to update LOG_LINKS of insns later
3465 in this basic block. Note that this (expensive) case is rare.
3467 Also, in this case, we must pretend that all REG_NOTEs for I2
3468 actually came from I3, so that REG_UNUSED notes from I2 will be
3469 properly handled. */
3471 if (i3_subst_into_i2)
3473 for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
3474 if ((GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == SET
3475 || GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == CLOBBER)
3476 && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, i)))
3477 && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
3478 && ! find_reg_note (i2, REG_UNUSED,
3479 SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
3480 for (temp = NEXT_INSN (i2);
3481 temp && (this_basic_block->next_bb == EXIT_BLOCK_PTR
3482 || BB_HEAD (this_basic_block) != temp);
3483 temp = NEXT_INSN (temp))
3484 if (temp != i3 && INSN_P (temp))
3485 for (link = LOG_LINKS (temp); link; link = XEXP (link, 1))
3486 if (XEXP (link, 0) == i2)
3487 XEXP (link, 0) = i3;
3489 if (i3notes)
3491 rtx link = i3notes;
3492 while (XEXP (link, 1))
3493 link = XEXP (link, 1);
3494 XEXP (link, 1) = i2notes;
3496 else
3497 i3notes = i2notes;
3498 i2notes = 0;
3501 LOG_LINKS (i3) = 0;
3502 REG_NOTES (i3) = 0;
3503 LOG_LINKS (i2) = 0;
3504 REG_NOTES (i2) = 0;
3506 if (newi2pat)
3508 INSN_CODE (i2) = i2_code_number;
3509 PATTERN (i2) = newi2pat;
3511 else
3512 SET_INSN_DELETED (i2);
3514 if (i1)
3516 LOG_LINKS (i1) = 0;
3517 REG_NOTES (i1) = 0;
3518 SET_INSN_DELETED (i1);
3521 /* Get death notes for everything that is now used in either I3 or
3522 I2 and used to die in a previous insn. If we built two new
3523 patterns, move from I1 to I2 then I2 to I3 so that we get the
3524 proper movement on registers that I2 modifies. */
3526 if (newi2pat)
3528 move_deaths (newi2pat, NULL_RTX, DF_INSN_LUID (i1), i2, &midnotes);
3529 move_deaths (newpat, newi2pat, DF_INSN_LUID (i1), i3, &midnotes);
3531 else
3532 move_deaths (newpat, NULL_RTX, i1 ? DF_INSN_LUID (i1) : DF_INSN_LUID (i2),
3533 i3, &midnotes);
3535 /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3. */
3536 if (i3notes)
3537 distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL_RTX,
3538 elim_i2, elim_i1);
3539 if (i2notes)
3540 distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL_RTX,
3541 elim_i2, elim_i1);
3542 if (i1notes)
3543 distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL_RTX,
3544 elim_i2, elim_i1);
3545 if (midnotes)
3546 distribute_notes (midnotes, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
3547 elim_i2, elim_i1);
3549 /* Distribute any notes added to I2 or I3 by recog_for_combine. We
3550 know these are REG_UNUSED and want them to go to the desired insn,
3551 so we always pass it as i3. */
3553 if (newi2pat && new_i2_notes)
3554 distribute_notes (new_i2_notes, i2, i2, NULL_RTX, NULL_RTX, NULL_RTX);
3556 if (new_i3_notes)
3557 distribute_notes (new_i3_notes, i3, i3, NULL_RTX, NULL_RTX, NULL_RTX);
3559 /* If I3DEST was used in I3SRC, it really died in I3. We may need to
3560 put a REG_DEAD note for it somewhere. If NEWI2PAT exists and sets
3561 I3DEST, the death must be somewhere before I2, not I3. If we passed I3
3562 in that case, it might delete I2. Similarly for I2 and I1.
3563 Show an additional death due to the REG_DEAD note we make here. If
3564 we discard it in distribute_notes, we will decrement it again. */
3566 if (i3dest_killed)
3568 if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
3569 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i3dest_killed,
3570 NULL_RTX),
3571 NULL_RTX, i2, NULL_RTX, elim_i2, elim_i1);
3572 else
3573 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i3dest_killed,
3574 NULL_RTX),
3575 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
3576 elim_i2, elim_i1);
3579 if (i2dest_in_i2src)
3581 if (newi2pat && reg_set_p (i2dest, newi2pat))
3582 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i2dest, NULL_RTX),
3583 NULL_RTX, i2, NULL_RTX, NULL_RTX, NULL_RTX);
3584 else
3585 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i2dest, NULL_RTX),
3586 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
3587 NULL_RTX, NULL_RTX);
3590 if (i1dest_in_i1src)
3592 if (newi2pat && reg_set_p (i1dest, newi2pat))
3593 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i1dest, NULL_RTX),
3594 NULL_RTX, i2, NULL_RTX, NULL_RTX, NULL_RTX);
3595 else
3596 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i1dest, NULL_RTX),
3597 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
3598 NULL_RTX, NULL_RTX);
3601 distribute_links (i3links);
3602 distribute_links (i2links);
3603 distribute_links (i1links);
3605 if (REG_P (i2dest))
3607 rtx link;
3608 rtx i2_insn = 0, i2_val = 0, set;
3610 /* The insn that used to set this register doesn't exist, and
3611 this life of the register may not exist either. See if one of
3612 I3's links points to an insn that sets I2DEST. If it does,
3613 that is now the last known value for I2DEST. If we don't update
3614 this and I2 set the register to a value that depended on its old
3615 contents, we will get confused. If this insn is used, thing
3616 will be set correctly in combine_instructions. */
3618 for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
3619 if ((set = single_set (XEXP (link, 0))) != 0
3620 && rtx_equal_p (i2dest, SET_DEST (set)))
3621 i2_insn = XEXP (link, 0), i2_val = SET_SRC (set);
3623 record_value_for_reg (i2dest, i2_insn, i2_val);
3625 /* If the reg formerly set in I2 died only once and that was in I3,
3626 zero its use count so it won't make `reload' do any work. */
3627 if (! added_sets_2
3628 && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
3629 && ! i2dest_in_i2src)
3631 regno = REGNO (i2dest);
3632 INC_REG_N_SETS (regno, -1);
3636 if (i1 && REG_P (i1dest))
3638 rtx link;
3639 rtx i1_insn = 0, i1_val = 0, set;
3641 for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
3642 if ((set = single_set (XEXP (link, 0))) != 0
3643 && rtx_equal_p (i1dest, SET_DEST (set)))
3644 i1_insn = XEXP (link, 0), i1_val = SET_SRC (set);
3646 record_value_for_reg (i1dest, i1_insn, i1_val);
3648 regno = REGNO (i1dest);
3649 if (! added_sets_1 && ! i1dest_in_i1src)
3650 INC_REG_N_SETS (regno, -1);
3653 /* Update reg_stat[].nonzero_bits et al for any changes that may have
3654 been made to this insn. The order of
3655 set_nonzero_bits_and_sign_copies() is important. Because newi2pat
3656 can affect nonzero_bits of newpat */
3657 if (newi2pat)
3658 note_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
3659 note_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
3661 /* Set new_direct_jump_p if a new return or simple jump instruction
3662 has been created.
3664 If I3 is now an unconditional jump, ensure that it has a
3665 BARRIER following it since it may have initially been a
3666 conditional jump. It may also be the last nonnote insn. */
3668 if (returnjump_p (i3) || any_uncondjump_p (i3))
3670 *new_direct_jump_p = 1;
3671 mark_jump_label (PATTERN (i3), i3, 0);
3673 if ((temp = next_nonnote_insn (i3)) == NULL_RTX
3674 || !BARRIER_P (temp))
3675 emit_barrier_after (i3);
3678 if (undobuf.other_insn != NULL_RTX
3679 && (returnjump_p (undobuf.other_insn)
3680 || any_uncondjump_p (undobuf.other_insn)))
3682 *new_direct_jump_p = 1;
3684 if ((temp = next_nonnote_insn (undobuf.other_insn)) == NULL_RTX
3685 || !BARRIER_P (temp))
3686 emit_barrier_after (undobuf.other_insn);
3689 /* An NOOP jump does not need barrier, but it does need cleaning up
3690 of CFG. */
3691 if (GET_CODE (newpat) == SET
3692 && SET_SRC (newpat) == pc_rtx
3693 && SET_DEST (newpat) == pc_rtx)
3694 *new_direct_jump_p = 1;
3697 if (undobuf.other_insn != NULL_RTX)
3699 if (dump_file)
3701 fprintf (dump_file, "modifying other_insn ");
3702 dump_insn_slim (dump_file, undobuf.other_insn);
3704 df_insn_rescan (undobuf.other_insn);
3707 if (i1 && !(NOTE_P(i1) && (NOTE_KIND (i1) == NOTE_INSN_DELETED)))
3709 if (dump_file)
3711 fprintf (dump_file, "modifying insn i1 ");
3712 dump_insn_slim (dump_file, i1);
3714 df_insn_rescan (i1);
3717 if (i2 && !(NOTE_P(i2) && (NOTE_KIND (i2) == NOTE_INSN_DELETED)))
3719 if (dump_file)
3721 fprintf (dump_file, "modifying insn i2 ");
3722 dump_insn_slim (dump_file, i2);
3724 df_insn_rescan (i2);
3727 if (i3 && !(NOTE_P(i3) && (NOTE_KIND (i3) == NOTE_INSN_DELETED)))
3729 if (dump_file)
3731 fprintf (dump_file, "modifying insn i3 ");
3732 dump_insn_slim (dump_file, i3);
3734 df_insn_rescan (i3);
3737 combine_successes++;
3738 undo_commit ();
3740 if (added_links_insn
3741 && (newi2pat == 0 || DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i2))
3742 && DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i3))
3743 return added_links_insn;
3744 else
3745 return newi2pat ? i2 : i3;
3748 /* Undo all the modifications recorded in undobuf. */
3750 static void
3751 undo_all (void)
3753 struct undo *undo, *next;
3755 for (undo = undobuf.undos; undo; undo = next)
3757 next = undo->next;
3758 switch (undo->kind)
3760 case UNDO_RTX:
3761 *undo->where.r = undo->old_contents.r;
3762 break;
3763 case UNDO_INT:
3764 *undo->where.i = undo->old_contents.i;
3765 break;
3766 case UNDO_MODE:
3767 PUT_MODE (*undo->where.r, undo->old_contents.m);
3768 break;
3769 default:
3770 gcc_unreachable ();
3773 undo->next = undobuf.frees;
3774 undobuf.frees = undo;
3777 undobuf.undos = 0;
3780 /* We've committed to accepting the changes we made. Move all
3781 of the undos to the free list. */
3783 static void
3784 undo_commit (void)
3786 struct undo *undo, *next;
3788 for (undo = undobuf.undos; undo; undo = next)
3790 next = undo->next;
3791 undo->next = undobuf.frees;
3792 undobuf.frees = undo;
3794 undobuf.undos = 0;
3797 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
3798 where we have an arithmetic expression and return that point. LOC will
3799 be inside INSN.
3801 try_combine will call this function to see if an insn can be split into
3802 two insns. */
3804 static rtx *
3805 find_split_point (rtx *loc, rtx insn)
3807 rtx x = *loc;
3808 enum rtx_code code = GET_CODE (x);
3809 rtx *split;
3810 unsigned HOST_WIDE_INT len = 0;
3811 HOST_WIDE_INT pos = 0;
3812 int unsignedp = 0;
3813 rtx inner = NULL_RTX;
3815 /* First special-case some codes. */
3816 switch (code)
3818 case SUBREG:
3819 #ifdef INSN_SCHEDULING
3820 /* If we are making a paradoxical SUBREG invalid, it becomes a split
3821 point. */
3822 if (MEM_P (SUBREG_REG (x)))
3823 return loc;
3824 #endif
3825 return find_split_point (&SUBREG_REG (x), insn);
3827 case MEM:
3828 #ifdef HAVE_lo_sum
3829 /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
3830 using LO_SUM and HIGH. */
3831 if (GET_CODE (XEXP (x, 0)) == CONST
3832 || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
3834 SUBST (XEXP (x, 0),
3835 gen_rtx_LO_SUM (Pmode,
3836 gen_rtx_HIGH (Pmode, XEXP (x, 0)),
3837 XEXP (x, 0)));
3838 return &XEXP (XEXP (x, 0), 0);
3840 #endif
3842 /* If we have a PLUS whose second operand is a constant and the
3843 address is not valid, perhaps will can split it up using
3844 the machine-specific way to split large constants. We use
3845 the first pseudo-reg (one of the virtual regs) as a placeholder;
3846 it will not remain in the result. */
3847 if (GET_CODE (XEXP (x, 0)) == PLUS
3848 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3849 && ! memory_address_p (GET_MODE (x), XEXP (x, 0)))
3851 rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
3852 rtx seq = split_insns (gen_rtx_SET (VOIDmode, reg, XEXP (x, 0)),
3853 subst_insn);
3855 /* This should have produced two insns, each of which sets our
3856 placeholder. If the source of the second is a valid address,
3857 we can make put both sources together and make a split point
3858 in the middle. */
3860 if (seq
3861 && NEXT_INSN (seq) != NULL_RTX
3862 && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
3863 && NONJUMP_INSN_P (seq)
3864 && GET_CODE (PATTERN (seq)) == SET
3865 && SET_DEST (PATTERN (seq)) == reg
3866 && ! reg_mentioned_p (reg,
3867 SET_SRC (PATTERN (seq)))
3868 && NONJUMP_INSN_P (NEXT_INSN (seq))
3869 && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
3870 && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
3871 && memory_address_p (GET_MODE (x),
3872 SET_SRC (PATTERN (NEXT_INSN (seq)))))
3874 rtx src1 = SET_SRC (PATTERN (seq));
3875 rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
3877 /* Replace the placeholder in SRC2 with SRC1. If we can
3878 find where in SRC2 it was placed, that can become our
3879 split point and we can replace this address with SRC2.
3880 Just try two obvious places. */
3882 src2 = replace_rtx (src2, reg, src1);
3883 split = 0;
3884 if (XEXP (src2, 0) == src1)
3885 split = &XEXP (src2, 0);
3886 else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
3887 && XEXP (XEXP (src2, 0), 0) == src1)
3888 split = &XEXP (XEXP (src2, 0), 0);
3890 if (split)
3892 SUBST (XEXP (x, 0), src2);
3893 return split;
3897 /* If that didn't work, perhaps the first operand is complex and
3898 needs to be computed separately, so make a split point there.
3899 This will occur on machines that just support REG + CONST
3900 and have a constant moved through some previous computation. */
3902 else if (!OBJECT_P (XEXP (XEXP (x, 0), 0))
3903 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
3904 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
3905 return &XEXP (XEXP (x, 0), 0);
3907 break;
3909 case SET:
3910 #ifdef HAVE_cc0
3911 /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
3912 ZERO_EXTRACT, the most likely reason why this doesn't match is that
3913 we need to put the operand into a register. So split at that
3914 point. */
3916 if (SET_DEST (x) == cc0_rtx
3917 && GET_CODE (SET_SRC (x)) != COMPARE
3918 && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
3919 && !OBJECT_P (SET_SRC (x))
3920 && ! (GET_CODE (SET_SRC (x)) == SUBREG
3921 && OBJECT_P (SUBREG_REG (SET_SRC (x)))))
3922 return &SET_SRC (x);
3923 #endif
3925 /* See if we can split SET_SRC as it stands. */
3926 split = find_split_point (&SET_SRC (x), insn);
3927 if (split && split != &SET_SRC (x))
3928 return split;
3930 /* See if we can split SET_DEST as it stands. */
3931 split = find_split_point (&SET_DEST (x), insn);
3932 if (split && split != &SET_DEST (x))
3933 return split;
3935 /* See if this is a bitfield assignment with everything constant. If
3936 so, this is an IOR of an AND, so split it into that. */
3937 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
3938 && (GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)))
3939 <= HOST_BITS_PER_WIDE_INT)
3940 && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT
3941 && GET_CODE (XEXP (SET_DEST (x), 2)) == CONST_INT
3942 && GET_CODE (SET_SRC (x)) == CONST_INT
3943 && ((INTVAL (XEXP (SET_DEST (x), 1))
3944 + INTVAL (XEXP (SET_DEST (x), 2)))
3945 <= GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0))))
3946 && ! side_effects_p (XEXP (SET_DEST (x), 0)))
3948 HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
3949 unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
3950 unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x));
3951 rtx dest = XEXP (SET_DEST (x), 0);
3952 enum machine_mode mode = GET_MODE (dest);
3953 unsigned HOST_WIDE_INT mask = ((HOST_WIDE_INT) 1 << len) - 1;
3954 rtx or_mask;
3956 if (BITS_BIG_ENDIAN)
3957 pos = GET_MODE_BITSIZE (mode) - len - pos;
3959 or_mask = gen_int_mode (src << pos, mode);
3960 if (src == mask)
3961 SUBST (SET_SRC (x),
3962 simplify_gen_binary (IOR, mode, dest, or_mask));
3963 else
3965 rtx negmask = gen_int_mode (~(mask << pos), mode);
3966 SUBST (SET_SRC (x),
3967 simplify_gen_binary (IOR, mode,
3968 simplify_gen_binary (AND, mode,
3969 dest, negmask),
3970 or_mask));
3973 SUBST (SET_DEST (x), dest);
3975 split = find_split_point (&SET_SRC (x), insn);
3976 if (split && split != &SET_SRC (x))
3977 return split;
3980 /* Otherwise, see if this is an operation that we can split into two.
3981 If so, try to split that. */
3982 code = GET_CODE (SET_SRC (x));
3984 switch (code)
3986 case AND:
3987 /* If we are AND'ing with a large constant that is only a single
3988 bit and the result is only being used in a context where we
3989 need to know if it is zero or nonzero, replace it with a bit
3990 extraction. This will avoid the large constant, which might
3991 have taken more than one insn to make. If the constant were
3992 not a valid argument to the AND but took only one insn to make,
3993 this is no worse, but if it took more than one insn, it will
3994 be better. */
3996 if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
3997 && REG_P (XEXP (SET_SRC (x), 0))
3998 && (pos = exact_log2 (INTVAL (XEXP (SET_SRC (x), 1)))) >= 7
3999 && REG_P (SET_DEST (x))
4000 && (split = find_single_use (SET_DEST (x), insn, (rtx*) 0)) != 0
4001 && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
4002 && XEXP (*split, 0) == SET_DEST (x)
4003 && XEXP (*split, 1) == const0_rtx)
4005 rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
4006 XEXP (SET_SRC (x), 0),
4007 pos, NULL_RTX, 1, 1, 0, 0);
4008 if (extraction != 0)
4010 SUBST (SET_SRC (x), extraction);
4011 return find_split_point (loc, insn);
4014 break;
4016 case NE:
4017 /* If STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
4018 is known to be on, this can be converted into a NEG of a shift. */
4019 if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
4020 && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
4021 && 1 <= (pos = exact_log2
4022 (nonzero_bits (XEXP (SET_SRC (x), 0),
4023 GET_MODE (XEXP (SET_SRC (x), 0))))))
4025 enum machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
4027 SUBST (SET_SRC (x),
4028 gen_rtx_NEG (mode,
4029 gen_rtx_LSHIFTRT (mode,
4030 XEXP (SET_SRC (x), 0),
4031 GEN_INT (pos))));
4033 split = find_split_point (&SET_SRC (x), insn);
4034 if (split && split != &SET_SRC (x))
4035 return split;
4037 break;
4039 case SIGN_EXTEND:
4040 inner = XEXP (SET_SRC (x), 0);
4042 /* We can't optimize if either mode is a partial integer
4043 mode as we don't know how many bits are significant
4044 in those modes. */
4045 if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
4046 || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
4047 break;
4049 pos = 0;
4050 len = GET_MODE_BITSIZE (GET_MODE (inner));
4051 unsignedp = 0;
4052 break;
4054 case SIGN_EXTRACT:
4055 case ZERO_EXTRACT:
4056 if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
4057 && GET_CODE (XEXP (SET_SRC (x), 2)) == CONST_INT)
4059 inner = XEXP (SET_SRC (x), 0);
4060 len = INTVAL (XEXP (SET_SRC (x), 1));
4061 pos = INTVAL (XEXP (SET_SRC (x), 2));
4063 if (BITS_BIG_ENDIAN)
4064 pos = GET_MODE_BITSIZE (GET_MODE (inner)) - len - pos;
4065 unsignedp = (code == ZERO_EXTRACT);
4067 break;
4069 default:
4070 break;
4073 if (len && pos >= 0 && pos + len <= GET_MODE_BITSIZE (GET_MODE (inner)))
4075 enum machine_mode mode = GET_MODE (SET_SRC (x));
4077 /* For unsigned, we have a choice of a shift followed by an
4078 AND or two shifts. Use two shifts for field sizes where the
4079 constant might be too large. We assume here that we can
4080 always at least get 8-bit constants in an AND insn, which is
4081 true for every current RISC. */
4083 if (unsignedp && len <= 8)
4085 SUBST (SET_SRC (x),
4086 gen_rtx_AND (mode,
4087 gen_rtx_LSHIFTRT
4088 (mode, gen_lowpart (mode, inner),
4089 GEN_INT (pos)),
4090 GEN_INT (((HOST_WIDE_INT) 1 << len) - 1)));
4092 split = find_split_point (&SET_SRC (x), insn);
4093 if (split && split != &SET_SRC (x))
4094 return split;
4096 else
4098 SUBST (SET_SRC (x),
4099 gen_rtx_fmt_ee
4100 (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
4101 gen_rtx_ASHIFT (mode,
4102 gen_lowpart (mode, inner),
4103 GEN_INT (GET_MODE_BITSIZE (mode)
4104 - len - pos)),
4105 GEN_INT (GET_MODE_BITSIZE (mode) - len)));
4107 split = find_split_point (&SET_SRC (x), insn);
4108 if (split && split != &SET_SRC (x))
4109 return split;
4113 /* See if this is a simple operation with a constant as the second
4114 operand. It might be that this constant is out of range and hence
4115 could be used as a split point. */
4116 if (BINARY_P (SET_SRC (x))
4117 && CONSTANT_P (XEXP (SET_SRC (x), 1))
4118 && (OBJECT_P (XEXP (SET_SRC (x), 0))
4119 || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
4120 && OBJECT_P (SUBREG_REG (XEXP (SET_SRC (x), 0))))))
4121 return &XEXP (SET_SRC (x), 1);
4123 /* Finally, see if this is a simple operation with its first operand
4124 not in a register. The operation might require this operand in a
4125 register, so return it as a split point. We can always do this
4126 because if the first operand were another operation, we would have
4127 already found it as a split point. */
4128 if ((BINARY_P (SET_SRC (x)) || UNARY_P (SET_SRC (x)))
4129 && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
4130 return &XEXP (SET_SRC (x), 0);
4132 return 0;
4134 case AND:
4135 case IOR:
4136 /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
4137 it is better to write this as (not (ior A B)) so we can split it.
4138 Similarly for IOR. */
4139 if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
4141 SUBST (*loc,
4142 gen_rtx_NOT (GET_MODE (x),
4143 gen_rtx_fmt_ee (code == IOR ? AND : IOR,
4144 GET_MODE (x),
4145 XEXP (XEXP (x, 0), 0),
4146 XEXP (XEXP (x, 1), 0))));
4147 return find_split_point (loc, insn);
4150 /* Many RISC machines have a large set of logical insns. If the
4151 second operand is a NOT, put it first so we will try to split the
4152 other operand first. */
4153 if (GET_CODE (XEXP (x, 1)) == NOT)
4155 rtx tem = XEXP (x, 0);
4156 SUBST (XEXP (x, 0), XEXP (x, 1));
4157 SUBST (XEXP (x, 1), tem);
4159 break;
4161 default:
4162 break;
4165 /* Otherwise, select our actions depending on our rtx class. */
4166 switch (GET_RTX_CLASS (code))
4168 case RTX_BITFIELD_OPS: /* This is ZERO_EXTRACT and SIGN_EXTRACT. */
4169 case RTX_TERNARY:
4170 split = find_split_point (&XEXP (x, 2), insn);
4171 if (split)
4172 return split;
4173 /* ... fall through ... */
4174 case RTX_BIN_ARITH:
4175 case RTX_COMM_ARITH:
4176 case RTX_COMPARE:
4177 case RTX_COMM_COMPARE:
4178 split = find_split_point (&XEXP (x, 1), insn);
4179 if (split)
4180 return split;
4181 /* ... fall through ... */
4182 case RTX_UNARY:
4183 /* Some machines have (and (shift ...) ...) insns. If X is not
4184 an AND, but XEXP (X, 0) is, use it as our split point. */
4185 if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
4186 return &XEXP (x, 0);
4188 split = find_split_point (&XEXP (x, 0), insn);
4189 if (split)
4190 return split;
4191 return loc;
4193 default:
4194 /* Otherwise, we don't have a split point. */
4195 return 0;
4199 /* Throughout X, replace FROM with TO, and return the result.
4200 The result is TO if X is FROM;
4201 otherwise the result is X, but its contents may have been modified.
4202 If they were modified, a record was made in undobuf so that
4203 undo_all will (among other things) return X to its original state.
4205 If the number of changes necessary is too much to record to undo,
4206 the excess changes are not made, so the result is invalid.
4207 The changes already made can still be undone.
4208 undobuf.num_undo is incremented for such changes, so by testing that
4209 the caller can tell whether the result is valid.
4211 `n_occurrences' is incremented each time FROM is replaced.
4213 IN_DEST is nonzero if we are processing the SET_DEST of a SET.
4215 UNIQUE_COPY is nonzero if each substitution must be unique. We do this
4216 by copying if `n_occurrences' is nonzero. */
4218 static rtx
4219 subst (rtx x, rtx from, rtx to, int in_dest, int unique_copy)
4221 enum rtx_code code = GET_CODE (x);
4222 enum machine_mode op0_mode = VOIDmode;
4223 const char *fmt;
4224 int len, i;
4225 rtx new;
4227 /* Two expressions are equal if they are identical copies of a shared
4228 RTX or if they are both registers with the same register number
4229 and mode. */
4231 #define COMBINE_RTX_EQUAL_P(X,Y) \
4232 ((X) == (Y) \
4233 || (REG_P (X) && REG_P (Y) \
4234 && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
4236 if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
4238 n_occurrences++;
4239 return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
4242 /* If X and FROM are the same register but different modes, they
4243 will not have been seen as equal above. However, the log links code
4244 will make a LOG_LINKS entry for that case. If we do nothing, we
4245 will try to rerecognize our original insn and, when it succeeds,
4246 we will delete the feeding insn, which is incorrect.
4248 So force this insn not to match in this (rare) case. */
4249 if (! in_dest && code == REG && REG_P (from)
4250 && reg_overlap_mentioned_p (x, from))
4251 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
4253 /* If this is an object, we are done unless it is a MEM or LO_SUM, both
4254 of which may contain things that can be combined. */
4255 if (code != MEM && code != LO_SUM && OBJECT_P (x))
4256 return x;
4258 /* It is possible to have a subexpression appear twice in the insn.
4259 Suppose that FROM is a register that appears within TO.
4260 Then, after that subexpression has been scanned once by `subst',
4261 the second time it is scanned, TO may be found. If we were
4262 to scan TO here, we would find FROM within it and create a
4263 self-referent rtl structure which is completely wrong. */
4264 if (COMBINE_RTX_EQUAL_P (x, to))
4265 return to;
4267 /* Parallel asm_operands need special attention because all of the
4268 inputs are shared across the arms. Furthermore, unsharing the
4269 rtl results in recognition failures. Failure to handle this case
4270 specially can result in circular rtl.
4272 Solve this by doing a normal pass across the first entry of the
4273 parallel, and only processing the SET_DESTs of the subsequent
4274 entries. Ug. */
4276 if (code == PARALLEL
4277 && GET_CODE (XVECEXP (x, 0, 0)) == SET
4278 && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
4280 new = subst (XVECEXP (x, 0, 0), from, to, 0, unique_copy);
4282 /* If this substitution failed, this whole thing fails. */
4283 if (GET_CODE (new) == CLOBBER
4284 && XEXP (new, 0) == const0_rtx)
4285 return new;
4287 SUBST (XVECEXP (x, 0, 0), new);
4289 for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
4291 rtx dest = SET_DEST (XVECEXP (x, 0, i));
4293 if (!REG_P (dest)
4294 && GET_CODE (dest) != CC0
4295 && GET_CODE (dest) != PC)
4297 new = subst (dest, from, to, 0, unique_copy);
4299 /* If this substitution failed, this whole thing fails. */
4300 if (GET_CODE (new) == CLOBBER
4301 && XEXP (new, 0) == const0_rtx)
4302 return new;
4304 SUBST (SET_DEST (XVECEXP (x, 0, i)), new);
4308 else
4310 len = GET_RTX_LENGTH (code);
4311 fmt = GET_RTX_FORMAT (code);
4313 /* We don't need to process a SET_DEST that is a register, CC0,
4314 or PC, so set up to skip this common case. All other cases
4315 where we want to suppress replacing something inside a
4316 SET_SRC are handled via the IN_DEST operand. */
4317 if (code == SET
4318 && (REG_P (SET_DEST (x))
4319 || GET_CODE (SET_DEST (x)) == CC0
4320 || GET_CODE (SET_DEST (x)) == PC))
4321 fmt = "ie";
4323 /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
4324 constant. */
4325 if (fmt[0] == 'e')
4326 op0_mode = GET_MODE (XEXP (x, 0));
4328 for (i = 0; i < len; i++)
4330 if (fmt[i] == 'E')
4332 int j;
4333 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4335 if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
4337 new = (unique_copy && n_occurrences
4338 ? copy_rtx (to) : to);
4339 n_occurrences++;
4341 else
4343 new = subst (XVECEXP (x, i, j), from, to, 0,
4344 unique_copy);
4346 /* If this substitution failed, this whole thing
4347 fails. */
4348 if (GET_CODE (new) == CLOBBER
4349 && XEXP (new, 0) == const0_rtx)
4350 return new;
4353 SUBST (XVECEXP (x, i, j), new);
4356 else if (fmt[i] == 'e')
4358 /* If this is a register being set, ignore it. */
4359 new = XEXP (x, i);
4360 if (in_dest
4361 && i == 0
4362 && (((code == SUBREG || code == ZERO_EXTRACT)
4363 && REG_P (new))
4364 || code == STRICT_LOW_PART))
4367 else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
4369 /* In general, don't install a subreg involving two
4370 modes not tieable. It can worsen register
4371 allocation, and can even make invalid reload
4372 insns, since the reg inside may need to be copied
4373 from in the outside mode, and that may be invalid
4374 if it is an fp reg copied in integer mode.
4376 We allow two exceptions to this: It is valid if
4377 it is inside another SUBREG and the mode of that
4378 SUBREG and the mode of the inside of TO is
4379 tieable and it is valid if X is a SET that copies
4380 FROM to CC0. */
4382 if (GET_CODE (to) == SUBREG
4383 && ! MODES_TIEABLE_P (GET_MODE (to),
4384 GET_MODE (SUBREG_REG (to)))
4385 && ! (code == SUBREG
4386 && MODES_TIEABLE_P (GET_MODE (x),
4387 GET_MODE (SUBREG_REG (to))))
4388 #ifdef HAVE_cc0
4389 && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
4390 #endif
4392 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
4394 #ifdef CANNOT_CHANGE_MODE_CLASS
4395 if (code == SUBREG
4396 && REG_P (to)
4397 && REGNO (to) < FIRST_PSEUDO_REGISTER
4398 && REG_CANNOT_CHANGE_MODE_P (REGNO (to),
4399 GET_MODE (to),
4400 GET_MODE (x)))
4401 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
4402 #endif
4404 new = (unique_copy && n_occurrences ? copy_rtx (to) : to);
4405 n_occurrences++;
4407 else
4408 /* If we are in a SET_DEST, suppress most cases unless we
4409 have gone inside a MEM, in which case we want to
4410 simplify the address. We assume here that things that
4411 are actually part of the destination have their inner
4412 parts in the first expression. This is true for SUBREG,
4413 STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
4414 things aside from REG and MEM that should appear in a
4415 SET_DEST. */
4416 new = subst (XEXP (x, i), from, to,
4417 (((in_dest
4418 && (code == SUBREG || code == STRICT_LOW_PART
4419 || code == ZERO_EXTRACT))
4420 || code == SET)
4421 && i == 0), unique_copy);
4423 /* If we found that we will have to reject this combination,
4424 indicate that by returning the CLOBBER ourselves, rather than
4425 an expression containing it. This will speed things up as
4426 well as prevent accidents where two CLOBBERs are considered
4427 to be equal, thus producing an incorrect simplification. */
4429 if (GET_CODE (new) == CLOBBER && XEXP (new, 0) == const0_rtx)
4430 return new;
4432 if (GET_CODE (x) == SUBREG
4433 && (GET_CODE (new) == CONST_INT
4434 || GET_CODE (new) == CONST_DOUBLE))
4436 enum machine_mode mode = GET_MODE (x);
4438 x = simplify_subreg (GET_MODE (x), new,
4439 GET_MODE (SUBREG_REG (x)),
4440 SUBREG_BYTE (x));
4441 if (! x)
4442 x = gen_rtx_CLOBBER (mode, const0_rtx);
4444 else if (GET_CODE (new) == CONST_INT
4445 && GET_CODE (x) == ZERO_EXTEND)
4447 x = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
4448 new, GET_MODE (XEXP (x, 0)));
4449 gcc_assert (x);
4451 else
4452 SUBST (XEXP (x, i), new);
4457 /* Try to simplify X. If the simplification changed the code, it is likely
4458 that further simplification will help, so loop, but limit the number
4459 of repetitions that will be performed. */
4461 for (i = 0; i < 4; i++)
4463 /* If X is sufficiently simple, don't bother trying to do anything
4464 with it. */
4465 if (code != CONST_INT && code != REG && code != CLOBBER)
4466 x = combine_simplify_rtx (x, op0_mode, in_dest);
4468 if (GET_CODE (x) == code)
4469 break;
4471 code = GET_CODE (x);
4473 /* We no longer know the original mode of operand 0 since we
4474 have changed the form of X) */
4475 op0_mode = VOIDmode;
4478 return x;
4481 /* Simplify X, a piece of RTL. We just operate on the expression at the
4482 outer level; call `subst' to simplify recursively. Return the new
4483 expression.
4485 OP0_MODE is the original mode of XEXP (x, 0). IN_DEST is nonzero
4486 if we are inside a SET_DEST. */
4488 static rtx
4489 combine_simplify_rtx (rtx x, enum machine_mode op0_mode, int in_dest)
4491 enum rtx_code code = GET_CODE (x);
4492 enum machine_mode mode = GET_MODE (x);
4493 rtx temp;
4494 int i;
4496 /* If this is a commutative operation, put a constant last and a complex
4497 expression first. We don't need to do this for comparisons here. */
4498 if (COMMUTATIVE_ARITH_P (x)
4499 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
4501 temp = XEXP (x, 0);
4502 SUBST (XEXP (x, 0), XEXP (x, 1));
4503 SUBST (XEXP (x, 1), temp);
4506 /* If this is a simple operation applied to an IF_THEN_ELSE, try
4507 applying it to the arms of the IF_THEN_ELSE. This often simplifies
4508 things. Check for cases where both arms are testing the same
4509 condition.
4511 Don't do anything if all operands are very simple. */
4513 if ((BINARY_P (x)
4514 && ((!OBJECT_P (XEXP (x, 0))
4515 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
4516 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))
4517 || (!OBJECT_P (XEXP (x, 1))
4518 && ! (GET_CODE (XEXP (x, 1)) == SUBREG
4519 && OBJECT_P (SUBREG_REG (XEXP (x, 1)))))))
4520 || (UNARY_P (x)
4521 && (!OBJECT_P (XEXP (x, 0))
4522 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
4523 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))))
4525 rtx cond, true_rtx, false_rtx;
4527 cond = if_then_else_cond (x, &true_rtx, &false_rtx);
4528 if (cond != 0
4529 /* If everything is a comparison, what we have is highly unlikely
4530 to be simpler, so don't use it. */
4531 && ! (COMPARISON_P (x)
4532 && (COMPARISON_P (true_rtx) || COMPARISON_P (false_rtx))))
4534 rtx cop1 = const0_rtx;
4535 enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
4537 if (cond_code == NE && COMPARISON_P (cond))
4538 return x;
4540 /* Simplify the alternative arms; this may collapse the true and
4541 false arms to store-flag values. Be careful to use copy_rtx
4542 here since true_rtx or false_rtx might share RTL with x as a
4543 result of the if_then_else_cond call above. */
4544 true_rtx = subst (copy_rtx (true_rtx), pc_rtx, pc_rtx, 0, 0);
4545 false_rtx = subst (copy_rtx (false_rtx), pc_rtx, pc_rtx, 0, 0);
4547 /* If true_rtx and false_rtx are not general_operands, an if_then_else
4548 is unlikely to be simpler. */
4549 if (general_operand (true_rtx, VOIDmode)
4550 && general_operand (false_rtx, VOIDmode))
4552 enum rtx_code reversed;
4554 /* Restarting if we generate a store-flag expression will cause
4555 us to loop. Just drop through in this case. */
4557 /* If the result values are STORE_FLAG_VALUE and zero, we can
4558 just make the comparison operation. */
4559 if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
4560 x = simplify_gen_relational (cond_code, mode, VOIDmode,
4561 cond, cop1);
4562 else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
4563 && ((reversed = reversed_comparison_code_parts
4564 (cond_code, cond, cop1, NULL))
4565 != UNKNOWN))
4566 x = simplify_gen_relational (reversed, mode, VOIDmode,
4567 cond, cop1);
4569 /* Likewise, we can make the negate of a comparison operation
4570 if the result values are - STORE_FLAG_VALUE and zero. */
4571 else if (GET_CODE (true_rtx) == CONST_INT
4572 && INTVAL (true_rtx) == - STORE_FLAG_VALUE
4573 && false_rtx == const0_rtx)
4574 x = simplify_gen_unary (NEG, mode,
4575 simplify_gen_relational (cond_code,
4576 mode, VOIDmode,
4577 cond, cop1),
4578 mode);
4579 else if (GET_CODE (false_rtx) == CONST_INT
4580 && INTVAL (false_rtx) == - STORE_FLAG_VALUE
4581 && true_rtx == const0_rtx
4582 && ((reversed = reversed_comparison_code_parts
4583 (cond_code, cond, cop1, NULL))
4584 != UNKNOWN))
4585 x = simplify_gen_unary (NEG, mode,
4586 simplify_gen_relational (reversed,
4587 mode, VOIDmode,
4588 cond, cop1),
4589 mode);
4590 else
4591 return gen_rtx_IF_THEN_ELSE (mode,
4592 simplify_gen_relational (cond_code,
4593 mode,
4594 VOIDmode,
4595 cond,
4596 cop1),
4597 true_rtx, false_rtx);
4599 code = GET_CODE (x);
4600 op0_mode = VOIDmode;
4605 /* Try to fold this expression in case we have constants that weren't
4606 present before. */
4607 temp = 0;
4608 switch (GET_RTX_CLASS (code))
4610 case RTX_UNARY:
4611 if (op0_mode == VOIDmode)
4612 op0_mode = GET_MODE (XEXP (x, 0));
4613 temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
4614 break;
4615 case RTX_COMPARE:
4616 case RTX_COMM_COMPARE:
4618 enum machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
4619 if (cmp_mode == VOIDmode)
4621 cmp_mode = GET_MODE (XEXP (x, 1));
4622 if (cmp_mode == VOIDmode)
4623 cmp_mode = op0_mode;
4625 temp = simplify_relational_operation (code, mode, cmp_mode,
4626 XEXP (x, 0), XEXP (x, 1));
4628 break;
4629 case RTX_COMM_ARITH:
4630 case RTX_BIN_ARITH:
4631 temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
4632 break;
4633 case RTX_BITFIELD_OPS:
4634 case RTX_TERNARY:
4635 temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
4636 XEXP (x, 1), XEXP (x, 2));
4637 break;
4638 default:
4639 break;
4642 if (temp)
4644 x = temp;
4645 code = GET_CODE (temp);
4646 op0_mode = VOIDmode;
4647 mode = GET_MODE (temp);
4650 /* First see if we can apply the inverse distributive law. */
4651 if (code == PLUS || code == MINUS
4652 || code == AND || code == IOR || code == XOR)
4654 x = apply_distributive_law (x);
4655 code = GET_CODE (x);
4656 op0_mode = VOIDmode;
4659 /* If CODE is an associative operation not otherwise handled, see if we
4660 can associate some operands. This can win if they are constants or
4661 if they are logically related (i.e. (a & b) & a). */
4662 if ((code == PLUS || code == MINUS || code == MULT || code == DIV
4663 || code == AND || code == IOR || code == XOR
4664 || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
4665 && ((INTEGRAL_MODE_P (mode) && code != DIV)
4666 || (flag_unsafe_math_optimizations && FLOAT_MODE_P (mode))))
4668 if (GET_CODE (XEXP (x, 0)) == code)
4670 rtx other = XEXP (XEXP (x, 0), 0);
4671 rtx inner_op0 = XEXP (XEXP (x, 0), 1);
4672 rtx inner_op1 = XEXP (x, 1);
4673 rtx inner;
4675 /* Make sure we pass the constant operand if any as the second
4676 one if this is a commutative operation. */
4677 if (CONSTANT_P (inner_op0) && COMMUTATIVE_ARITH_P (x))
4679 rtx tem = inner_op0;
4680 inner_op0 = inner_op1;
4681 inner_op1 = tem;
4683 inner = simplify_binary_operation (code == MINUS ? PLUS
4684 : code == DIV ? MULT
4685 : code,
4686 mode, inner_op0, inner_op1);
4688 /* For commutative operations, try the other pair if that one
4689 didn't simplify. */
4690 if (inner == 0 && COMMUTATIVE_ARITH_P (x))
4692 other = XEXP (XEXP (x, 0), 1);
4693 inner = simplify_binary_operation (code, mode,
4694 XEXP (XEXP (x, 0), 0),
4695 XEXP (x, 1));
4698 if (inner)
4699 return simplify_gen_binary (code, mode, other, inner);
4703 /* A little bit of algebraic simplification here. */
4704 switch (code)
4706 case MEM:
4707 /* Ensure that our address has any ASHIFTs converted to MULT in case
4708 address-recognizing predicates are called later. */
4709 temp = make_compound_operation (XEXP (x, 0), MEM);
4710 SUBST (XEXP (x, 0), temp);
4711 break;
4713 case SUBREG:
4714 if (op0_mode == VOIDmode)
4715 op0_mode = GET_MODE (SUBREG_REG (x));
4717 /* See if this can be moved to simplify_subreg. */
4718 if (CONSTANT_P (SUBREG_REG (x))
4719 && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
4720 /* Don't call gen_lowpart if the inner mode
4721 is VOIDmode and we cannot simplify it, as SUBREG without
4722 inner mode is invalid. */
4723 && (GET_MODE (SUBREG_REG (x)) != VOIDmode
4724 || gen_lowpart_common (mode, SUBREG_REG (x))))
4725 return gen_lowpart (mode, SUBREG_REG (x));
4727 if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
4728 break;
4730 rtx temp;
4731 temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
4732 SUBREG_BYTE (x));
4733 if (temp)
4734 return temp;
4737 /* Don't change the mode of the MEM if that would change the meaning
4738 of the address. */
4739 if (MEM_P (SUBREG_REG (x))
4740 && (MEM_VOLATILE_P (SUBREG_REG (x))
4741 || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0))))
4742 return gen_rtx_CLOBBER (mode, const0_rtx);
4744 /* Note that we cannot do any narrowing for non-constants since
4745 we might have been counting on using the fact that some bits were
4746 zero. We now do this in the SET. */
4748 break;
4750 case NEG:
4751 temp = expand_compound_operation (XEXP (x, 0));
4753 /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
4754 replaced by (lshiftrt X C). This will convert
4755 (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y). */
4757 if (GET_CODE (temp) == ASHIFTRT
4758 && GET_CODE (XEXP (temp, 1)) == CONST_INT
4759 && INTVAL (XEXP (temp, 1)) == GET_MODE_BITSIZE (mode) - 1)
4760 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (temp, 0),
4761 INTVAL (XEXP (temp, 1)));
4763 /* If X has only a single bit that might be nonzero, say, bit I, convert
4764 (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
4765 MODE minus 1. This will convert (neg (zero_extract X 1 Y)) to
4766 (sign_extract X 1 Y). But only do this if TEMP isn't a register
4767 or a SUBREG of one since we'd be making the expression more
4768 complex if it was just a register. */
4770 if (!REG_P (temp)
4771 && ! (GET_CODE (temp) == SUBREG
4772 && REG_P (SUBREG_REG (temp)))
4773 && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
4775 rtx temp1 = simplify_shift_const
4776 (NULL_RTX, ASHIFTRT, mode,
4777 simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
4778 GET_MODE_BITSIZE (mode) - 1 - i),
4779 GET_MODE_BITSIZE (mode) - 1 - i);
4781 /* If all we did was surround TEMP with the two shifts, we
4782 haven't improved anything, so don't use it. Otherwise,
4783 we are better off with TEMP1. */
4784 if (GET_CODE (temp1) != ASHIFTRT
4785 || GET_CODE (XEXP (temp1, 0)) != ASHIFT
4786 || XEXP (XEXP (temp1, 0), 0) != temp)
4787 return temp1;
4789 break;
4791 case TRUNCATE:
4792 /* We can't handle truncation to a partial integer mode here
4793 because we don't know the real bitsize of the partial
4794 integer mode. */
4795 if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
4796 break;
4798 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4799 && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
4800 GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))))
4801 SUBST (XEXP (x, 0),
4802 force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
4803 GET_MODE_MASK (mode), 0));
4805 /* Similarly to what we do in simplify-rtx.c, a truncate of a register
4806 whose value is a comparison can be replaced with a subreg if
4807 STORE_FLAG_VALUE permits. */
4808 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4809 && ((HOST_WIDE_INT) STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
4810 && (temp = get_last_value (XEXP (x, 0)))
4811 && COMPARISON_P (temp))
4812 return gen_lowpart (mode, XEXP (x, 0));
4813 break;
4815 #ifdef HAVE_cc0
4816 case COMPARE:
4817 /* Convert (compare FOO (const_int 0)) to FOO unless we aren't
4818 using cc0, in which case we want to leave it as a COMPARE
4819 so we can distinguish it from a register-register-copy. */
4820 if (XEXP (x, 1) == const0_rtx)
4821 return XEXP (x, 0);
4823 /* x - 0 is the same as x unless x's mode has signed zeros and
4824 allows rounding towards -infinity. Under those conditions,
4825 0 - 0 is -0. */
4826 if (!(HONOR_SIGNED_ZEROS (GET_MODE (XEXP (x, 0)))
4827 && HONOR_SIGN_DEPENDENT_ROUNDING (GET_MODE (XEXP (x, 0))))
4828 && XEXP (x, 1) == CONST0_RTX (GET_MODE (XEXP (x, 0))))
4829 return XEXP (x, 0);
4830 break;
4831 #endif
4833 case CONST:
4834 /* (const (const X)) can become (const X). Do it this way rather than
4835 returning the inner CONST since CONST can be shared with a
4836 REG_EQUAL note. */
4837 if (GET_CODE (XEXP (x, 0)) == CONST)
4838 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4839 break;
4841 #ifdef HAVE_lo_sum
4842 case LO_SUM:
4843 /* Convert (lo_sum (high FOO) FOO) to FOO. This is necessary so we
4844 can add in an offset. find_split_point will split this address up
4845 again if it doesn't match. */
4846 if (GET_CODE (XEXP (x, 0)) == HIGH
4847 && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
4848 return XEXP (x, 1);
4849 break;
4850 #endif
4852 case PLUS:
4853 /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
4854 when c is (const_int (pow2 + 1) / 2) is a sign extension of a
4855 bit-field and can be replaced by either a sign_extend or a
4856 sign_extract. The `and' may be a zero_extend and the two
4857 <c>, -<c> constants may be reversed. */
4858 if (GET_CODE (XEXP (x, 0)) == XOR
4859 && GET_CODE (XEXP (x, 1)) == CONST_INT
4860 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
4861 && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
4862 && ((i = exact_log2 (INTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
4863 || (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
4864 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4865 && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
4866 && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
4867 && (INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
4868 == ((HOST_WIDE_INT) 1 << (i + 1)) - 1))
4869 || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
4870 && (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
4871 == (unsigned int) i + 1))))
4872 return simplify_shift_const
4873 (NULL_RTX, ASHIFTRT, mode,
4874 simplify_shift_const (NULL_RTX, ASHIFT, mode,
4875 XEXP (XEXP (XEXP (x, 0), 0), 0),
4876 GET_MODE_BITSIZE (mode) - (i + 1)),
4877 GET_MODE_BITSIZE (mode) - (i + 1));
4879 /* If only the low-order bit of X is possibly nonzero, (plus x -1)
4880 can become (ashiftrt (ashift (xor x 1) C) C) where C is
4881 the bitsize of the mode - 1. This allows simplification of
4882 "a = (b & 8) == 0;" */
4883 if (XEXP (x, 1) == constm1_rtx
4884 && !REG_P (XEXP (x, 0))
4885 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
4886 && REG_P (SUBREG_REG (XEXP (x, 0))))
4887 && nonzero_bits (XEXP (x, 0), mode) == 1)
4888 return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
4889 simplify_shift_const (NULL_RTX, ASHIFT, mode,
4890 gen_rtx_XOR (mode, XEXP (x, 0), const1_rtx),
4891 GET_MODE_BITSIZE (mode) - 1),
4892 GET_MODE_BITSIZE (mode) - 1);
4894 /* If we are adding two things that have no bits in common, convert
4895 the addition into an IOR. This will often be further simplified,
4896 for example in cases like ((a & 1) + (a & 2)), which can
4897 become a & 3. */
4899 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4900 && (nonzero_bits (XEXP (x, 0), mode)
4901 & nonzero_bits (XEXP (x, 1), mode)) == 0)
4903 /* Try to simplify the expression further. */
4904 rtx tor = simplify_gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
4905 temp = combine_simplify_rtx (tor, mode, in_dest);
4907 /* If we could, great. If not, do not go ahead with the IOR
4908 replacement, since PLUS appears in many special purpose
4909 address arithmetic instructions. */
4910 if (GET_CODE (temp) != CLOBBER && temp != tor)
4911 return temp;
4913 break;
4915 case MINUS:
4916 /* (minus <foo> (and <foo> (const_int -pow2))) becomes
4917 (and <foo> (const_int pow2-1)) */
4918 if (GET_CODE (XEXP (x, 1)) == AND
4919 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
4920 && exact_log2 (-INTVAL (XEXP (XEXP (x, 1), 1))) >= 0
4921 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
4922 return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
4923 -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
4924 break;
4926 case MULT:
4927 /* If we have (mult (plus A B) C), apply the distributive law and then
4928 the inverse distributive law to see if things simplify. This
4929 occurs mostly in addresses, often when unrolling loops. */
4931 if (GET_CODE (XEXP (x, 0)) == PLUS)
4933 rtx result = distribute_and_simplify_rtx (x, 0);
4934 if (result)
4935 return result;
4938 /* Try simplify a*(b/c) as (a*b)/c. */
4939 if (FLOAT_MODE_P (mode) && flag_unsafe_math_optimizations
4940 && GET_CODE (XEXP (x, 0)) == DIV)
4942 rtx tem = simplify_binary_operation (MULT, mode,
4943 XEXP (XEXP (x, 0), 0),
4944 XEXP (x, 1));
4945 if (tem)
4946 return simplify_gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
4948 break;
4950 case UDIV:
4951 /* If this is a divide by a power of two, treat it as a shift if
4952 its first operand is a shift. */
4953 if (GET_CODE (XEXP (x, 1)) == CONST_INT
4954 && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0
4955 && (GET_CODE (XEXP (x, 0)) == ASHIFT
4956 || GET_CODE (XEXP (x, 0)) == LSHIFTRT
4957 || GET_CODE (XEXP (x, 0)) == ASHIFTRT
4958 || GET_CODE (XEXP (x, 0)) == ROTATE
4959 || GET_CODE (XEXP (x, 0)) == ROTATERT))
4960 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
4961 break;
4963 case EQ: case NE:
4964 case GT: case GTU: case GE: case GEU:
4965 case LT: case LTU: case LE: case LEU:
4966 case UNEQ: case LTGT:
4967 case UNGT: case UNGE:
4968 case UNLT: case UNLE:
4969 case UNORDERED: case ORDERED:
4970 /* If the first operand is a condition code, we can't do anything
4971 with it. */
4972 if (GET_CODE (XEXP (x, 0)) == COMPARE
4973 || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
4974 && ! CC0_P (XEXP (x, 0))))
4976 rtx op0 = XEXP (x, 0);
4977 rtx op1 = XEXP (x, 1);
4978 enum rtx_code new_code;
4980 if (GET_CODE (op0) == COMPARE)
4981 op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
4983 /* Simplify our comparison, if possible. */
4984 new_code = simplify_comparison (code, &op0, &op1);
4986 /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
4987 if only the low-order bit is possibly nonzero in X (such as when
4988 X is a ZERO_EXTRACT of one bit). Similarly, we can convert EQ to
4989 (xor X 1) or (minus 1 X); we use the former. Finally, if X is
4990 known to be either 0 or -1, NE becomes a NEG and EQ becomes
4991 (plus X 1).
4993 Remove any ZERO_EXTRACT we made when thinking this was a
4994 comparison. It may now be simpler to use, e.g., an AND. If a
4995 ZERO_EXTRACT is indeed appropriate, it will be placed back by
4996 the call to make_compound_operation in the SET case. */
4998 if (STORE_FLAG_VALUE == 1
4999 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5000 && op1 == const0_rtx
5001 && mode == GET_MODE (op0)
5002 && nonzero_bits (op0, mode) == 1)
5003 return gen_lowpart (mode,
5004 expand_compound_operation (op0));
5006 else if (STORE_FLAG_VALUE == 1
5007 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5008 && op1 == const0_rtx
5009 && mode == GET_MODE (op0)
5010 && (num_sign_bit_copies (op0, mode)
5011 == GET_MODE_BITSIZE (mode)))
5013 op0 = expand_compound_operation (op0);
5014 return simplify_gen_unary (NEG, mode,
5015 gen_lowpart (mode, op0),
5016 mode);
5019 else if (STORE_FLAG_VALUE == 1
5020 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5021 && op1 == const0_rtx
5022 && mode == GET_MODE (op0)
5023 && nonzero_bits (op0, mode) == 1)
5025 op0 = expand_compound_operation (op0);
5026 return simplify_gen_binary (XOR, mode,
5027 gen_lowpart (mode, op0),
5028 const1_rtx);
5031 else if (STORE_FLAG_VALUE == 1
5032 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5033 && op1 == const0_rtx
5034 && mode == GET_MODE (op0)
5035 && (num_sign_bit_copies (op0, mode)
5036 == GET_MODE_BITSIZE (mode)))
5038 op0 = expand_compound_operation (op0);
5039 return plus_constant (gen_lowpart (mode, op0), 1);
5042 /* If STORE_FLAG_VALUE is -1, we have cases similar to
5043 those above. */
5044 if (STORE_FLAG_VALUE == -1
5045 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5046 && op1 == const0_rtx
5047 && (num_sign_bit_copies (op0, mode)
5048 == GET_MODE_BITSIZE (mode)))
5049 return gen_lowpart (mode,
5050 expand_compound_operation (op0));
5052 else if (STORE_FLAG_VALUE == -1
5053 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5054 && op1 == const0_rtx
5055 && mode == GET_MODE (op0)
5056 && nonzero_bits (op0, mode) == 1)
5058 op0 = expand_compound_operation (op0);
5059 return simplify_gen_unary (NEG, mode,
5060 gen_lowpart (mode, op0),
5061 mode);
5064 else if (STORE_FLAG_VALUE == -1
5065 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5066 && op1 == const0_rtx
5067 && mode == GET_MODE (op0)
5068 && (num_sign_bit_copies (op0, mode)
5069 == GET_MODE_BITSIZE (mode)))
5071 op0 = expand_compound_operation (op0);
5072 return simplify_gen_unary (NOT, mode,
5073 gen_lowpart (mode, op0),
5074 mode);
5077 /* If X is 0/1, (eq X 0) is X-1. */
5078 else if (STORE_FLAG_VALUE == -1
5079 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5080 && op1 == const0_rtx
5081 && mode == GET_MODE (op0)
5082 && nonzero_bits (op0, mode) == 1)
5084 op0 = expand_compound_operation (op0);
5085 return plus_constant (gen_lowpart (mode, op0), -1);
5088 /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
5089 one bit that might be nonzero, we can convert (ne x 0) to
5090 (ashift x c) where C puts the bit in the sign bit. Remove any
5091 AND with STORE_FLAG_VALUE when we are done, since we are only
5092 going to test the sign bit. */
5093 if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5094 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5095 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
5096 == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
5097 && op1 == const0_rtx
5098 && mode == GET_MODE (op0)
5099 && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
5101 x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
5102 expand_compound_operation (op0),
5103 GET_MODE_BITSIZE (mode) - 1 - i);
5104 if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
5105 return XEXP (x, 0);
5106 else
5107 return x;
5110 /* If the code changed, return a whole new comparison. */
5111 if (new_code != code)
5112 return gen_rtx_fmt_ee (new_code, mode, op0, op1);
5114 /* Otherwise, keep this operation, but maybe change its operands.
5115 This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR). */
5116 SUBST (XEXP (x, 0), op0);
5117 SUBST (XEXP (x, 1), op1);
5119 break;
5121 case IF_THEN_ELSE:
5122 return simplify_if_then_else (x);
5124 case ZERO_EXTRACT:
5125 case SIGN_EXTRACT:
5126 case ZERO_EXTEND:
5127 case SIGN_EXTEND:
5128 /* If we are processing SET_DEST, we are done. */
5129 if (in_dest)
5130 return x;
5132 return expand_compound_operation (x);
5134 case SET:
5135 return simplify_set (x);
5137 case AND:
5138 case IOR:
5139 return simplify_logical (x);
5141 case ASHIFT:
5142 case LSHIFTRT:
5143 case ASHIFTRT:
5144 case ROTATE:
5145 case ROTATERT:
5146 /* If this is a shift by a constant amount, simplify it. */
5147 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
5148 return simplify_shift_const (x, code, mode, XEXP (x, 0),
5149 INTVAL (XEXP (x, 1)));
5151 else if (SHIFT_COUNT_TRUNCATED && !REG_P (XEXP (x, 1)))
5152 SUBST (XEXP (x, 1),
5153 force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
5154 ((HOST_WIDE_INT) 1
5155 << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
5156 - 1,
5157 0));
5158 break;
5160 default:
5161 break;
5164 return x;
5167 /* Simplify X, an IF_THEN_ELSE expression. Return the new expression. */
5169 static rtx
5170 simplify_if_then_else (rtx x)
5172 enum machine_mode mode = GET_MODE (x);
5173 rtx cond = XEXP (x, 0);
5174 rtx true_rtx = XEXP (x, 1);
5175 rtx false_rtx = XEXP (x, 2);
5176 enum rtx_code true_code = GET_CODE (cond);
5177 int comparison_p = COMPARISON_P (cond);
5178 rtx temp;
5179 int i;
5180 enum rtx_code false_code;
5181 rtx reversed;
5183 /* Simplify storing of the truth value. */
5184 if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
5185 return simplify_gen_relational (true_code, mode, VOIDmode,
5186 XEXP (cond, 0), XEXP (cond, 1));
5188 /* Also when the truth value has to be reversed. */
5189 if (comparison_p
5190 && true_rtx == const0_rtx && false_rtx == const_true_rtx
5191 && (reversed = reversed_comparison (cond, mode)))
5192 return reversed;
5194 /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
5195 in it is being compared against certain values. Get the true and false
5196 comparisons and see if that says anything about the value of each arm. */
5198 if (comparison_p
5199 && ((false_code = reversed_comparison_code (cond, NULL))
5200 != UNKNOWN)
5201 && REG_P (XEXP (cond, 0)))
5203 HOST_WIDE_INT nzb;
5204 rtx from = XEXP (cond, 0);
5205 rtx true_val = XEXP (cond, 1);
5206 rtx false_val = true_val;
5207 int swapped = 0;
5209 /* If FALSE_CODE is EQ, swap the codes and arms. */
5211 if (false_code == EQ)
5213 swapped = 1, true_code = EQ, false_code = NE;
5214 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
5217 /* If we are comparing against zero and the expression being tested has
5218 only a single bit that might be nonzero, that is its value when it is
5219 not equal to zero. Similarly if it is known to be -1 or 0. */
5221 if (true_code == EQ && true_val == const0_rtx
5222 && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
5224 false_code = EQ;
5225 false_val = GEN_INT (trunc_int_for_mode (nzb, GET_MODE (from)));
5227 else if (true_code == EQ && true_val == const0_rtx
5228 && (num_sign_bit_copies (from, GET_MODE (from))
5229 == GET_MODE_BITSIZE (GET_MODE (from))))
5231 false_code = EQ;
5232 false_val = constm1_rtx;
5235 /* Now simplify an arm if we know the value of the register in the
5236 branch and it is used in the arm. Be careful due to the potential
5237 of locally-shared RTL. */
5239 if (reg_mentioned_p (from, true_rtx))
5240 true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
5241 from, true_val),
5242 pc_rtx, pc_rtx, 0, 0);
5243 if (reg_mentioned_p (from, false_rtx))
5244 false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
5245 from, false_val),
5246 pc_rtx, pc_rtx, 0, 0);
5248 SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
5249 SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
5251 true_rtx = XEXP (x, 1);
5252 false_rtx = XEXP (x, 2);
5253 true_code = GET_CODE (cond);
5256 /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
5257 reversed, do so to avoid needing two sets of patterns for
5258 subtract-and-branch insns. Similarly if we have a constant in the true
5259 arm, the false arm is the same as the first operand of the comparison, or
5260 the false arm is more complicated than the true arm. */
5262 if (comparison_p
5263 && reversed_comparison_code (cond, NULL) != UNKNOWN
5264 && (true_rtx == pc_rtx
5265 || (CONSTANT_P (true_rtx)
5266 && GET_CODE (false_rtx) != CONST_INT && false_rtx != pc_rtx)
5267 || true_rtx == const0_rtx
5268 || (OBJECT_P (true_rtx) && !OBJECT_P (false_rtx))
5269 || (GET_CODE (true_rtx) == SUBREG && OBJECT_P (SUBREG_REG (true_rtx))
5270 && !OBJECT_P (false_rtx))
5271 || reg_mentioned_p (true_rtx, false_rtx)
5272 || rtx_equal_p (false_rtx, XEXP (cond, 0))))
5274 true_code = reversed_comparison_code (cond, NULL);
5275 SUBST (XEXP (x, 0), reversed_comparison (cond, GET_MODE (cond)));
5276 SUBST (XEXP (x, 1), false_rtx);
5277 SUBST (XEXP (x, 2), true_rtx);
5279 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
5280 cond = XEXP (x, 0);
5282 /* It is possible that the conditional has been simplified out. */
5283 true_code = GET_CODE (cond);
5284 comparison_p = COMPARISON_P (cond);
5287 /* If the two arms are identical, we don't need the comparison. */
5289 if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
5290 return true_rtx;
5292 /* Convert a == b ? b : a to "a". */
5293 if (true_code == EQ && ! side_effects_p (cond)
5294 && !HONOR_NANS (mode)
5295 && rtx_equal_p (XEXP (cond, 0), false_rtx)
5296 && rtx_equal_p (XEXP (cond, 1), true_rtx))
5297 return false_rtx;
5298 else if (true_code == NE && ! side_effects_p (cond)
5299 && !HONOR_NANS (mode)
5300 && rtx_equal_p (XEXP (cond, 0), true_rtx)
5301 && rtx_equal_p (XEXP (cond, 1), false_rtx))
5302 return true_rtx;
5304 /* Look for cases where we have (abs x) or (neg (abs X)). */
5306 if (GET_MODE_CLASS (mode) == MODE_INT
5307 && GET_CODE (false_rtx) == NEG
5308 && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
5309 && comparison_p
5310 && rtx_equal_p (true_rtx, XEXP (cond, 0))
5311 && ! side_effects_p (true_rtx))
5312 switch (true_code)
5314 case GT:
5315 case GE:
5316 return simplify_gen_unary (ABS, mode, true_rtx, mode);
5317 case LT:
5318 case LE:
5319 return
5320 simplify_gen_unary (NEG, mode,
5321 simplify_gen_unary (ABS, mode, true_rtx, mode),
5322 mode);
5323 default:
5324 break;
5327 /* Look for MIN or MAX. */
5329 if ((! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
5330 && comparison_p
5331 && rtx_equal_p (XEXP (cond, 0), true_rtx)
5332 && rtx_equal_p (XEXP (cond, 1), false_rtx)
5333 && ! side_effects_p (cond))
5334 switch (true_code)
5336 case GE:
5337 case GT:
5338 return simplify_gen_binary (SMAX, mode, true_rtx, false_rtx);
5339 case LE:
5340 case LT:
5341 return simplify_gen_binary (SMIN, mode, true_rtx, false_rtx);
5342 case GEU:
5343 case GTU:
5344 return simplify_gen_binary (UMAX, mode, true_rtx, false_rtx);
5345 case LEU:
5346 case LTU:
5347 return simplify_gen_binary (UMIN, mode, true_rtx, false_rtx);
5348 default:
5349 break;
5352 /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
5353 second operand is zero, this can be done as (OP Z (mult COND C2)) where
5354 C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
5355 SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
5356 We can do this kind of thing in some cases when STORE_FLAG_VALUE is
5357 neither 1 or -1, but it isn't worth checking for. */
5359 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
5360 && comparison_p
5361 && GET_MODE_CLASS (mode) == MODE_INT
5362 && ! side_effects_p (x))
5364 rtx t = make_compound_operation (true_rtx, SET);
5365 rtx f = make_compound_operation (false_rtx, SET);
5366 rtx cond_op0 = XEXP (cond, 0);
5367 rtx cond_op1 = XEXP (cond, 1);
5368 enum rtx_code op = UNKNOWN, extend_op = UNKNOWN;
5369 enum machine_mode m = mode;
5370 rtx z = 0, c1 = NULL_RTX;
5372 if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
5373 || GET_CODE (t) == IOR || GET_CODE (t) == XOR
5374 || GET_CODE (t) == ASHIFT
5375 || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
5376 && rtx_equal_p (XEXP (t, 0), f))
5377 c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
5379 /* If an identity-zero op is commutative, check whether there
5380 would be a match if we swapped the operands. */
5381 else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
5382 || GET_CODE (t) == XOR)
5383 && rtx_equal_p (XEXP (t, 1), f))
5384 c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
5385 else if (GET_CODE (t) == SIGN_EXTEND
5386 && (GET_CODE (XEXP (t, 0)) == PLUS
5387 || GET_CODE (XEXP (t, 0)) == MINUS
5388 || GET_CODE (XEXP (t, 0)) == IOR
5389 || GET_CODE (XEXP (t, 0)) == XOR
5390 || GET_CODE (XEXP (t, 0)) == ASHIFT
5391 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
5392 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
5393 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
5394 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
5395 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
5396 && (num_sign_bit_copies (f, GET_MODE (f))
5397 > (unsigned int)
5398 (GET_MODE_BITSIZE (mode)
5399 - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 0))))))
5401 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
5402 extend_op = SIGN_EXTEND;
5403 m = GET_MODE (XEXP (t, 0));
5405 else if (GET_CODE (t) == SIGN_EXTEND
5406 && (GET_CODE (XEXP (t, 0)) == PLUS
5407 || GET_CODE (XEXP (t, 0)) == IOR
5408 || GET_CODE (XEXP (t, 0)) == XOR)
5409 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
5410 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
5411 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
5412 && (num_sign_bit_copies (f, GET_MODE (f))
5413 > (unsigned int)
5414 (GET_MODE_BITSIZE (mode)
5415 - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 1))))))
5417 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
5418 extend_op = SIGN_EXTEND;
5419 m = GET_MODE (XEXP (t, 0));
5421 else if (GET_CODE (t) == ZERO_EXTEND
5422 && (GET_CODE (XEXP (t, 0)) == PLUS
5423 || GET_CODE (XEXP (t, 0)) == MINUS
5424 || GET_CODE (XEXP (t, 0)) == IOR
5425 || GET_CODE (XEXP (t, 0)) == XOR
5426 || GET_CODE (XEXP (t, 0)) == ASHIFT
5427 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
5428 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
5429 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
5430 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5431 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
5432 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
5433 && ((nonzero_bits (f, GET_MODE (f))
5434 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
5435 == 0))
5437 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
5438 extend_op = ZERO_EXTEND;
5439 m = GET_MODE (XEXP (t, 0));
5441 else if (GET_CODE (t) == ZERO_EXTEND
5442 && (GET_CODE (XEXP (t, 0)) == PLUS
5443 || GET_CODE (XEXP (t, 0)) == IOR
5444 || GET_CODE (XEXP (t, 0)) == XOR)
5445 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
5446 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5447 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
5448 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
5449 && ((nonzero_bits (f, GET_MODE (f))
5450 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
5451 == 0))
5453 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
5454 extend_op = ZERO_EXTEND;
5455 m = GET_MODE (XEXP (t, 0));
5458 if (z)
5460 temp = subst (simplify_gen_relational (true_code, m, VOIDmode,
5461 cond_op0, cond_op1),
5462 pc_rtx, pc_rtx, 0, 0);
5463 temp = simplify_gen_binary (MULT, m, temp,
5464 simplify_gen_binary (MULT, m, c1,
5465 const_true_rtx));
5466 temp = subst (temp, pc_rtx, pc_rtx, 0, 0);
5467 temp = simplify_gen_binary (op, m, gen_lowpart (m, z), temp);
5469 if (extend_op != UNKNOWN)
5470 temp = simplify_gen_unary (extend_op, mode, temp, m);
5472 return temp;
5476 /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
5477 1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
5478 negation of a single bit, we can convert this operation to a shift. We
5479 can actually do this more generally, but it doesn't seem worth it. */
5481 if (true_code == NE && XEXP (cond, 1) == const0_rtx
5482 && false_rtx == const0_rtx && GET_CODE (true_rtx) == CONST_INT
5483 && ((1 == nonzero_bits (XEXP (cond, 0), mode)
5484 && (i = exact_log2 (INTVAL (true_rtx))) >= 0)
5485 || ((num_sign_bit_copies (XEXP (cond, 0), mode)
5486 == GET_MODE_BITSIZE (mode))
5487 && (i = exact_log2 (-INTVAL (true_rtx))) >= 0)))
5488 return
5489 simplify_shift_const (NULL_RTX, ASHIFT, mode,
5490 gen_lowpart (mode, XEXP (cond, 0)), i);
5492 /* (IF_THEN_ELSE (NE REG 0) (0) (8)) is REG for nonzero_bits (REG) == 8. */
5493 if (true_code == NE && XEXP (cond, 1) == const0_rtx
5494 && false_rtx == const0_rtx && GET_CODE (true_rtx) == CONST_INT
5495 && GET_MODE (XEXP (cond, 0)) == mode
5496 && (INTVAL (true_rtx) & GET_MODE_MASK (mode))
5497 == nonzero_bits (XEXP (cond, 0), mode)
5498 && (i = exact_log2 (INTVAL (true_rtx) & GET_MODE_MASK (mode))) >= 0)
5499 return XEXP (cond, 0);
5501 return x;
5504 /* Simplify X, a SET expression. Return the new expression. */
5506 static rtx
5507 simplify_set (rtx x)
5509 rtx src = SET_SRC (x);
5510 rtx dest = SET_DEST (x);
5511 enum machine_mode mode
5512 = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
5513 rtx other_insn;
5514 rtx *cc_use;
5516 /* (set (pc) (return)) gets written as (return). */
5517 if (GET_CODE (dest) == PC && GET_CODE (src) == RETURN)
5518 return src;
5520 /* Now that we know for sure which bits of SRC we are using, see if we can
5521 simplify the expression for the object knowing that we only need the
5522 low-order bits. */
5524 if (GET_MODE_CLASS (mode) == MODE_INT
5525 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
5527 src = force_to_mode (src, mode, ~(HOST_WIDE_INT) 0, 0);
5528 SUBST (SET_SRC (x), src);
5531 /* If we are setting CC0 or if the source is a COMPARE, look for the use of
5532 the comparison result and try to simplify it unless we already have used
5533 undobuf.other_insn. */
5534 if ((GET_MODE_CLASS (mode) == MODE_CC
5535 || GET_CODE (src) == COMPARE
5536 || CC0_P (dest))
5537 && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
5538 && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
5539 && COMPARISON_P (*cc_use)
5540 && rtx_equal_p (XEXP (*cc_use, 0), dest))
5542 enum rtx_code old_code = GET_CODE (*cc_use);
5543 enum rtx_code new_code;
5544 rtx op0, op1, tmp;
5545 int other_changed = 0;
5546 enum machine_mode compare_mode = GET_MODE (dest);
5548 if (GET_CODE (src) == COMPARE)
5549 op0 = XEXP (src, 0), op1 = XEXP (src, 1);
5550 else
5551 op0 = src, op1 = CONST0_RTX (GET_MODE (src));
5553 tmp = simplify_relational_operation (old_code, compare_mode, VOIDmode,
5554 op0, op1);
5555 if (!tmp)
5556 new_code = old_code;
5557 else if (!CONSTANT_P (tmp))
5559 new_code = GET_CODE (tmp);
5560 op0 = XEXP (tmp, 0);
5561 op1 = XEXP (tmp, 1);
5563 else
5565 rtx pat = PATTERN (other_insn);
5566 undobuf.other_insn = other_insn;
5567 SUBST (*cc_use, tmp);
5569 /* Attempt to simplify CC user. */
5570 if (GET_CODE (pat) == SET)
5572 rtx new = simplify_rtx (SET_SRC (pat));
5573 if (new != NULL_RTX)
5574 SUBST (SET_SRC (pat), new);
5577 /* Convert X into a no-op move. */
5578 SUBST (SET_DEST (x), pc_rtx);
5579 SUBST (SET_SRC (x), pc_rtx);
5580 return x;
5583 /* Simplify our comparison, if possible. */
5584 new_code = simplify_comparison (new_code, &op0, &op1);
5586 #ifdef SELECT_CC_MODE
5587 /* If this machine has CC modes other than CCmode, check to see if we
5588 need to use a different CC mode here. */
5589 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
5590 compare_mode = GET_MODE (op0);
5591 else
5592 compare_mode = SELECT_CC_MODE (new_code, op0, op1);
5594 #ifndef HAVE_cc0
5595 /* If the mode changed, we have to change SET_DEST, the mode in the
5596 compare, and the mode in the place SET_DEST is used. If SET_DEST is
5597 a hard register, just build new versions with the proper mode. If it
5598 is a pseudo, we lose unless it is only time we set the pseudo, in
5599 which case we can safely change its mode. */
5600 if (compare_mode != GET_MODE (dest))
5602 if (can_change_dest_mode (dest, 0, compare_mode))
5604 unsigned int regno = REGNO (dest);
5605 rtx new_dest;
5607 if (regno < FIRST_PSEUDO_REGISTER)
5608 new_dest = gen_rtx_REG (compare_mode, regno);
5609 else
5611 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
5612 new_dest = regno_reg_rtx[regno];
5615 SUBST (SET_DEST (x), new_dest);
5616 SUBST (XEXP (*cc_use, 0), new_dest);
5617 other_changed = 1;
5619 dest = new_dest;
5622 #endif /* cc0 */
5623 #endif /* SELECT_CC_MODE */
5625 /* If the code changed, we have to build a new comparison in
5626 undobuf.other_insn. */
5627 if (new_code != old_code)
5629 int other_changed_previously = other_changed;
5630 unsigned HOST_WIDE_INT mask;
5632 SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
5633 dest, const0_rtx));
5634 other_changed = 1;
5636 /* If the only change we made was to change an EQ into an NE or
5637 vice versa, OP0 has only one bit that might be nonzero, and OP1
5638 is zero, check if changing the user of the condition code will
5639 produce a valid insn. If it won't, we can keep the original code
5640 in that insn by surrounding our operation with an XOR. */
5642 if (((old_code == NE && new_code == EQ)
5643 || (old_code == EQ && new_code == NE))
5644 && ! other_changed_previously && op1 == const0_rtx
5645 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
5646 && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
5648 rtx pat = PATTERN (other_insn), note = 0;
5650 if ((recog_for_combine (&pat, other_insn, &note) < 0
5651 && ! check_asm_operands (pat)))
5653 PUT_CODE (*cc_use, old_code);
5654 other_changed = 0;
5656 op0 = simplify_gen_binary (XOR, GET_MODE (op0),
5657 op0, GEN_INT (mask));
5662 if (other_changed)
5663 undobuf.other_insn = other_insn;
5665 #ifdef HAVE_cc0
5666 /* If we are now comparing against zero, change our source if
5667 needed. If we do not use cc0, we always have a COMPARE. */
5668 if (op1 == const0_rtx && dest == cc0_rtx)
5670 SUBST (SET_SRC (x), op0);
5671 src = op0;
5673 else
5674 #endif
5676 /* Otherwise, if we didn't previously have a COMPARE in the
5677 correct mode, we need one. */
5678 if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
5680 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
5681 src = SET_SRC (x);
5683 else if (GET_MODE (op0) == compare_mode && op1 == const0_rtx)
5685 SUBST (SET_SRC (x), op0);
5686 src = SET_SRC (x);
5688 /* Otherwise, update the COMPARE if needed. */
5689 else if (XEXP (src, 0) != op0 || XEXP (src, 1) != op1)
5691 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
5692 src = SET_SRC (x);
5695 else
5697 /* Get SET_SRC in a form where we have placed back any
5698 compound expressions. Then do the checks below. */
5699 src = make_compound_operation (src, SET);
5700 SUBST (SET_SRC (x), src);
5703 /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
5704 and X being a REG or (subreg (reg)), we may be able to convert this to
5705 (set (subreg:m2 x) (op)).
5707 We can always do this if M1 is narrower than M2 because that means that
5708 we only care about the low bits of the result.
5710 However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
5711 perform a narrower operation than requested since the high-order bits will
5712 be undefined. On machine where it is defined, this transformation is safe
5713 as long as M1 and M2 have the same number of words. */
5715 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
5716 && !OBJECT_P (SUBREG_REG (src))
5717 && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
5718 / UNITS_PER_WORD)
5719 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
5720 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
5721 #ifndef WORD_REGISTER_OPERATIONS
5722 && (GET_MODE_SIZE (GET_MODE (src))
5723 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
5724 #endif
5725 #ifdef CANNOT_CHANGE_MODE_CLASS
5726 && ! (REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER
5727 && REG_CANNOT_CHANGE_MODE_P (REGNO (dest),
5728 GET_MODE (SUBREG_REG (src)),
5729 GET_MODE (src)))
5730 #endif
5731 && (REG_P (dest)
5732 || (GET_CODE (dest) == SUBREG
5733 && REG_P (SUBREG_REG (dest)))))
5735 SUBST (SET_DEST (x),
5736 gen_lowpart (GET_MODE (SUBREG_REG (src)),
5737 dest));
5738 SUBST (SET_SRC (x), SUBREG_REG (src));
5740 src = SET_SRC (x), dest = SET_DEST (x);
5743 #ifdef HAVE_cc0
5744 /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
5745 in SRC. */
5746 if (dest == cc0_rtx
5747 && GET_CODE (src) == SUBREG
5748 && subreg_lowpart_p (src)
5749 && (GET_MODE_BITSIZE (GET_MODE (src))
5750 < GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (src)))))
5752 rtx inner = SUBREG_REG (src);
5753 enum machine_mode inner_mode = GET_MODE (inner);
5755 /* Here we make sure that we don't have a sign bit on. */
5756 if (GET_MODE_BITSIZE (inner_mode) <= HOST_BITS_PER_WIDE_INT
5757 && (nonzero_bits (inner, inner_mode)
5758 < ((unsigned HOST_WIDE_INT) 1
5759 << (GET_MODE_BITSIZE (GET_MODE (src)) - 1))))
5761 SUBST (SET_SRC (x), inner);
5762 src = SET_SRC (x);
5765 #endif
5767 #ifdef LOAD_EXTEND_OP
5768 /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
5769 would require a paradoxical subreg. Replace the subreg with a
5770 zero_extend to avoid the reload that would otherwise be required. */
5772 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
5773 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != UNKNOWN
5774 && SUBREG_BYTE (src) == 0
5775 && (GET_MODE_SIZE (GET_MODE (src))
5776 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
5777 && MEM_P (SUBREG_REG (src)))
5779 SUBST (SET_SRC (x),
5780 gen_rtx_fmt_e (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
5781 GET_MODE (src), SUBREG_REG (src)));
5783 src = SET_SRC (x);
5785 #endif
5787 /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
5788 are comparing an item known to be 0 or -1 against 0, use a logical
5789 operation instead. Check for one of the arms being an IOR of the other
5790 arm with some value. We compute three terms to be IOR'ed together. In
5791 practice, at most two will be nonzero. Then we do the IOR's. */
5793 if (GET_CODE (dest) != PC
5794 && GET_CODE (src) == IF_THEN_ELSE
5795 && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
5796 && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
5797 && XEXP (XEXP (src, 0), 1) == const0_rtx
5798 && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
5799 #ifdef HAVE_conditional_move
5800 && ! can_conditionally_move_p (GET_MODE (src))
5801 #endif
5802 && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
5803 GET_MODE (XEXP (XEXP (src, 0), 0)))
5804 == GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (src, 0), 0))))
5805 && ! side_effects_p (src))
5807 rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
5808 ? XEXP (src, 1) : XEXP (src, 2));
5809 rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
5810 ? XEXP (src, 2) : XEXP (src, 1));
5811 rtx term1 = const0_rtx, term2, term3;
5813 if (GET_CODE (true_rtx) == IOR
5814 && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
5815 term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
5816 else if (GET_CODE (true_rtx) == IOR
5817 && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
5818 term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
5819 else if (GET_CODE (false_rtx) == IOR
5820 && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
5821 term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
5822 else if (GET_CODE (false_rtx) == IOR
5823 && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
5824 term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
5826 term2 = simplify_gen_binary (AND, GET_MODE (src),
5827 XEXP (XEXP (src, 0), 0), true_rtx);
5828 term3 = simplify_gen_binary (AND, GET_MODE (src),
5829 simplify_gen_unary (NOT, GET_MODE (src),
5830 XEXP (XEXP (src, 0), 0),
5831 GET_MODE (src)),
5832 false_rtx);
5834 SUBST (SET_SRC (x),
5835 simplify_gen_binary (IOR, GET_MODE (src),
5836 simplify_gen_binary (IOR, GET_MODE (src),
5837 term1, term2),
5838 term3));
5840 src = SET_SRC (x);
5843 /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
5844 whole thing fail. */
5845 if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
5846 return src;
5847 else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
5848 return dest;
5849 else
5850 /* Convert this into a field assignment operation, if possible. */
5851 return make_field_assignment (x);
5854 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
5855 result. */
5857 static rtx
5858 simplify_logical (rtx x)
5860 enum machine_mode mode = GET_MODE (x);
5861 rtx op0 = XEXP (x, 0);
5862 rtx op1 = XEXP (x, 1);
5864 switch (GET_CODE (x))
5866 case AND:
5867 /* We can call simplify_and_const_int only if we don't lose
5868 any (sign) bits when converting INTVAL (op1) to
5869 "unsigned HOST_WIDE_INT". */
5870 if (GET_CODE (op1) == CONST_INT
5871 && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5872 || INTVAL (op1) > 0))
5874 x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
5875 if (GET_CODE (x) != AND)
5876 return x;
5878 op0 = XEXP (x, 0);
5879 op1 = XEXP (x, 1);
5882 /* If we have any of (and (ior A B) C) or (and (xor A B) C),
5883 apply the distributive law and then the inverse distributive
5884 law to see if things simplify. */
5885 if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
5887 rtx result = distribute_and_simplify_rtx (x, 0);
5888 if (result)
5889 return result;
5891 if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
5893 rtx result = distribute_and_simplify_rtx (x, 1);
5894 if (result)
5895 return result;
5897 break;
5899 case IOR:
5900 /* If we have (ior (and A B) C), apply the distributive law and then
5901 the inverse distributive law to see if things simplify. */
5903 if (GET_CODE (op0) == AND)
5905 rtx result = distribute_and_simplify_rtx (x, 0);
5906 if (result)
5907 return result;
5910 if (GET_CODE (op1) == AND)
5912 rtx result = distribute_and_simplify_rtx (x, 1);
5913 if (result)
5914 return result;
5916 break;
5918 default:
5919 gcc_unreachable ();
5922 return x;
5925 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
5926 operations" because they can be replaced with two more basic operations.
5927 ZERO_EXTEND is also considered "compound" because it can be replaced with
5928 an AND operation, which is simpler, though only one operation.
5930 The function expand_compound_operation is called with an rtx expression
5931 and will convert it to the appropriate shifts and AND operations,
5932 simplifying at each stage.
5934 The function make_compound_operation is called to convert an expression
5935 consisting of shifts and ANDs into the equivalent compound expression.
5936 It is the inverse of this function, loosely speaking. */
5938 static rtx
5939 expand_compound_operation (rtx x)
5941 unsigned HOST_WIDE_INT pos = 0, len;
5942 int unsignedp = 0;
5943 unsigned int modewidth;
5944 rtx tem;
5946 switch (GET_CODE (x))
5948 case ZERO_EXTEND:
5949 unsignedp = 1;
5950 case SIGN_EXTEND:
5951 /* We can't necessarily use a const_int for a multiword mode;
5952 it depends on implicitly extending the value.
5953 Since we don't know the right way to extend it,
5954 we can't tell whether the implicit way is right.
5956 Even for a mode that is no wider than a const_int,
5957 we can't win, because we need to sign extend one of its bits through
5958 the rest of it, and we don't know which bit. */
5959 if (GET_CODE (XEXP (x, 0)) == CONST_INT)
5960 return x;
5962 /* Return if (subreg:MODE FROM 0) is not a safe replacement for
5963 (zero_extend:MODE FROM) or (sign_extend:MODE FROM). It is for any MEM
5964 because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
5965 reloaded. If not for that, MEM's would very rarely be safe.
5967 Reject MODEs bigger than a word, because we might not be able
5968 to reference a two-register group starting with an arbitrary register
5969 (and currently gen_lowpart might crash for a SUBREG). */
5971 if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
5972 return x;
5974 /* Reject MODEs that aren't scalar integers because turning vector
5975 or complex modes into shifts causes problems. */
5977 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
5978 return x;
5980 len = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)));
5981 /* If the inner object has VOIDmode (the only way this can happen
5982 is if it is an ASM_OPERANDS), we can't do anything since we don't
5983 know how much masking to do. */
5984 if (len == 0)
5985 return x;
5987 break;
5989 case ZERO_EXTRACT:
5990 unsignedp = 1;
5992 /* ... fall through ... */
5994 case SIGN_EXTRACT:
5995 /* If the operand is a CLOBBER, just return it. */
5996 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
5997 return XEXP (x, 0);
5999 if (GET_CODE (XEXP (x, 1)) != CONST_INT
6000 || GET_CODE (XEXP (x, 2)) != CONST_INT
6001 || GET_MODE (XEXP (x, 0)) == VOIDmode)
6002 return x;
6004 /* Reject MODEs that aren't scalar integers because turning vector
6005 or complex modes into shifts causes problems. */
6007 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6008 return x;
6010 len = INTVAL (XEXP (x, 1));
6011 pos = INTVAL (XEXP (x, 2));
6013 /* This should stay within the object being extracted, fail otherwise. */
6014 if (len + pos > GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
6015 return x;
6017 if (BITS_BIG_ENDIAN)
6018 pos = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - len - pos;
6020 break;
6022 default:
6023 return x;
6025 /* Convert sign extension to zero extension, if we know that the high
6026 bit is not set, as this is easier to optimize. It will be converted
6027 back to cheaper alternative in make_extraction. */
6028 if (GET_CODE (x) == SIGN_EXTEND
6029 && (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6030 && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
6031 & ~(((unsigned HOST_WIDE_INT)
6032 GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
6033 >> 1))
6034 == 0)))
6036 rtx temp = gen_rtx_ZERO_EXTEND (GET_MODE (x), XEXP (x, 0));
6037 rtx temp2 = expand_compound_operation (temp);
6039 /* Make sure this is a profitable operation. */
6040 if (rtx_cost (x, SET) > rtx_cost (temp2, SET))
6041 return temp2;
6042 else if (rtx_cost (x, SET) > rtx_cost (temp, SET))
6043 return temp;
6044 else
6045 return x;
6048 /* We can optimize some special cases of ZERO_EXTEND. */
6049 if (GET_CODE (x) == ZERO_EXTEND)
6051 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
6052 know that the last value didn't have any inappropriate bits
6053 set. */
6054 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
6055 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
6056 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6057 && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
6058 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6059 return XEXP (XEXP (x, 0), 0);
6061 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
6062 if (GET_CODE (XEXP (x, 0)) == SUBREG
6063 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
6064 && subreg_lowpart_p (XEXP (x, 0))
6065 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6066 && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
6067 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6068 return SUBREG_REG (XEXP (x, 0));
6070 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
6071 is a comparison and STORE_FLAG_VALUE permits. This is like
6072 the first case, but it works even when GET_MODE (x) is larger
6073 than HOST_WIDE_INT. */
6074 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
6075 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
6076 && COMPARISON_P (XEXP (XEXP (x, 0), 0))
6077 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
6078 <= HOST_BITS_PER_WIDE_INT)
6079 && ((HOST_WIDE_INT) STORE_FLAG_VALUE
6080 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6081 return XEXP (XEXP (x, 0), 0);
6083 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
6084 if (GET_CODE (XEXP (x, 0)) == SUBREG
6085 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
6086 && subreg_lowpart_p (XEXP (x, 0))
6087 && COMPARISON_P (SUBREG_REG (XEXP (x, 0)))
6088 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
6089 <= HOST_BITS_PER_WIDE_INT)
6090 && ((HOST_WIDE_INT) STORE_FLAG_VALUE
6091 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6092 return SUBREG_REG (XEXP (x, 0));
6096 /* If we reach here, we want to return a pair of shifts. The inner
6097 shift is a left shift of BITSIZE - POS - LEN bits. The outer
6098 shift is a right shift of BITSIZE - LEN bits. It is arithmetic or
6099 logical depending on the value of UNSIGNEDP.
6101 If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
6102 converted into an AND of a shift.
6104 We must check for the case where the left shift would have a negative
6105 count. This can happen in a case like (x >> 31) & 255 on machines
6106 that can't shift by a constant. On those machines, we would first
6107 combine the shift with the AND to produce a variable-position
6108 extraction. Then the constant of 31 would be substituted in to produce
6109 a such a position. */
6111 modewidth = GET_MODE_BITSIZE (GET_MODE (x));
6112 if (modewidth + len >= pos)
6114 enum machine_mode mode = GET_MODE (x);
6115 tem = gen_lowpart (mode, XEXP (x, 0));
6116 if (!tem || GET_CODE (tem) == CLOBBER)
6117 return x;
6118 tem = simplify_shift_const (NULL_RTX, ASHIFT, mode,
6119 tem, modewidth - pos - len);
6120 tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
6121 mode, tem, modewidth - len);
6123 else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
6124 tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
6125 simplify_shift_const (NULL_RTX, LSHIFTRT,
6126 GET_MODE (x),
6127 XEXP (x, 0), pos),
6128 ((HOST_WIDE_INT) 1 << len) - 1);
6129 else
6130 /* Any other cases we can't handle. */
6131 return x;
6133 /* If we couldn't do this for some reason, return the original
6134 expression. */
6135 if (GET_CODE (tem) == CLOBBER)
6136 return x;
6138 return tem;
6141 /* X is a SET which contains an assignment of one object into
6142 a part of another (such as a bit-field assignment, STRICT_LOW_PART,
6143 or certain SUBREGS). If possible, convert it into a series of
6144 logical operations.
6146 We half-heartedly support variable positions, but do not at all
6147 support variable lengths. */
6149 static rtx
6150 expand_field_assignment (rtx x)
6152 rtx inner;
6153 rtx pos; /* Always counts from low bit. */
6154 int len;
6155 rtx mask, cleared, masked;
6156 enum machine_mode compute_mode;
6158 /* Loop until we find something we can't simplify. */
6159 while (1)
6161 if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
6162 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
6164 inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
6165 len = GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)));
6166 pos = GEN_INT (subreg_lsb (XEXP (SET_DEST (x), 0)));
6168 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
6169 && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT)
6171 inner = XEXP (SET_DEST (x), 0);
6172 len = INTVAL (XEXP (SET_DEST (x), 1));
6173 pos = XEXP (SET_DEST (x), 2);
6175 /* A constant position should stay within the width of INNER. */
6176 if (GET_CODE (pos) == CONST_INT
6177 && INTVAL (pos) + len > GET_MODE_BITSIZE (GET_MODE (inner)))
6178 break;
6180 if (BITS_BIG_ENDIAN)
6182 if (GET_CODE (pos) == CONST_INT)
6183 pos = GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner)) - len
6184 - INTVAL (pos));
6185 else if (GET_CODE (pos) == MINUS
6186 && GET_CODE (XEXP (pos, 1)) == CONST_INT
6187 && (INTVAL (XEXP (pos, 1))
6188 == GET_MODE_BITSIZE (GET_MODE (inner)) - len))
6189 /* If position is ADJUST - X, new position is X. */
6190 pos = XEXP (pos, 0);
6191 else
6192 pos = simplify_gen_binary (MINUS, GET_MODE (pos),
6193 GEN_INT (GET_MODE_BITSIZE (
6194 GET_MODE (inner))
6195 - len),
6196 pos);
6200 /* A SUBREG between two modes that occupy the same numbers of words
6201 can be done by moving the SUBREG to the source. */
6202 else if (GET_CODE (SET_DEST (x)) == SUBREG
6203 /* We need SUBREGs to compute nonzero_bits properly. */
6204 && nonzero_sign_valid
6205 && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
6206 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
6207 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
6208 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
6210 x = gen_rtx_SET (VOIDmode, SUBREG_REG (SET_DEST (x)),
6211 gen_lowpart
6212 (GET_MODE (SUBREG_REG (SET_DEST (x))),
6213 SET_SRC (x)));
6214 continue;
6216 else
6217 break;
6219 while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
6220 inner = SUBREG_REG (inner);
6222 compute_mode = GET_MODE (inner);
6224 /* Don't attempt bitwise arithmetic on non scalar integer modes. */
6225 if (! SCALAR_INT_MODE_P (compute_mode))
6227 enum machine_mode imode;
6229 /* Don't do anything for vector or complex integral types. */
6230 if (! FLOAT_MODE_P (compute_mode))
6231 break;
6233 /* Try to find an integral mode to pun with. */
6234 imode = mode_for_size (GET_MODE_BITSIZE (compute_mode), MODE_INT, 0);
6235 if (imode == BLKmode)
6236 break;
6238 compute_mode = imode;
6239 inner = gen_lowpart (imode, inner);
6242 /* Compute a mask of LEN bits, if we can do this on the host machine. */
6243 if (len >= HOST_BITS_PER_WIDE_INT)
6244 break;
6246 /* Now compute the equivalent expression. Make a copy of INNER
6247 for the SET_DEST in case it is a MEM into which we will substitute;
6248 we don't want shared RTL in that case. */
6249 mask = GEN_INT (((HOST_WIDE_INT) 1 << len) - 1);
6250 cleared = simplify_gen_binary (AND, compute_mode,
6251 simplify_gen_unary (NOT, compute_mode,
6252 simplify_gen_binary (ASHIFT,
6253 compute_mode,
6254 mask, pos),
6255 compute_mode),
6256 inner);
6257 masked = simplify_gen_binary (ASHIFT, compute_mode,
6258 simplify_gen_binary (
6259 AND, compute_mode,
6260 gen_lowpart (compute_mode, SET_SRC (x)),
6261 mask),
6262 pos);
6264 x = gen_rtx_SET (VOIDmode, copy_rtx (inner),
6265 simplify_gen_binary (IOR, compute_mode,
6266 cleared, masked));
6269 return x;
6272 /* Return an RTX for a reference to LEN bits of INNER. If POS_RTX is nonzero,
6273 it is an RTX that represents a variable starting position; otherwise,
6274 POS is the (constant) starting bit position (counted from the LSB).
6276 UNSIGNEDP is nonzero for an unsigned reference and zero for a
6277 signed reference.
6279 IN_DEST is nonzero if this is a reference in the destination of a
6280 SET. This is used when a ZERO_ or SIGN_EXTRACT isn't needed. If nonzero,
6281 a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
6282 be used.
6284 IN_COMPARE is nonzero if we are in a COMPARE. This means that a
6285 ZERO_EXTRACT should be built even for bits starting at bit 0.
6287 MODE is the desired mode of the result (if IN_DEST == 0).
6289 The result is an RTX for the extraction or NULL_RTX if the target
6290 can't handle it. */
6292 static rtx
6293 make_extraction (enum machine_mode mode, rtx inner, HOST_WIDE_INT pos,
6294 rtx pos_rtx, unsigned HOST_WIDE_INT len, int unsignedp,
6295 int in_dest, int in_compare)
6297 /* This mode describes the size of the storage area
6298 to fetch the overall value from. Within that, we
6299 ignore the POS lowest bits, etc. */
6300 enum machine_mode is_mode = GET_MODE (inner);
6301 enum machine_mode inner_mode;
6302 enum machine_mode wanted_inner_mode;
6303 enum machine_mode wanted_inner_reg_mode = word_mode;
6304 enum machine_mode pos_mode = word_mode;
6305 enum machine_mode extraction_mode = word_mode;
6306 enum machine_mode tmode = mode_for_size (len, MODE_INT, 1);
6307 rtx new = 0;
6308 rtx orig_pos_rtx = pos_rtx;
6309 HOST_WIDE_INT orig_pos;
6311 if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
6313 /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
6314 consider just the QI as the memory to extract from.
6315 The subreg adds or removes high bits; its mode is
6316 irrelevant to the meaning of this extraction,
6317 since POS and LEN count from the lsb. */
6318 if (MEM_P (SUBREG_REG (inner)))
6319 is_mode = GET_MODE (SUBREG_REG (inner));
6320 inner = SUBREG_REG (inner);
6322 else if (GET_CODE (inner) == ASHIFT
6323 && GET_CODE (XEXP (inner, 1)) == CONST_INT
6324 && pos_rtx == 0 && pos == 0
6325 && len > (unsigned HOST_WIDE_INT) INTVAL (XEXP (inner, 1)))
6327 /* We're extracting the least significant bits of an rtx
6328 (ashift X (const_int C)), where LEN > C. Extract the
6329 least significant (LEN - C) bits of X, giving an rtx
6330 whose mode is MODE, then shift it left C times. */
6331 new = make_extraction (mode, XEXP (inner, 0),
6332 0, 0, len - INTVAL (XEXP (inner, 1)),
6333 unsignedp, in_dest, in_compare);
6334 if (new != 0)
6335 return gen_rtx_ASHIFT (mode, new, XEXP (inner, 1));
6338 inner_mode = GET_MODE (inner);
6340 if (pos_rtx && GET_CODE (pos_rtx) == CONST_INT)
6341 pos = INTVAL (pos_rtx), pos_rtx = 0;
6343 /* See if this can be done without an extraction. We never can if the
6344 width of the field is not the same as that of some integer mode. For
6345 registers, we can only avoid the extraction if the position is at the
6346 low-order bit and this is either not in the destination or we have the
6347 appropriate STRICT_LOW_PART operation available.
6349 For MEM, we can avoid an extract if the field starts on an appropriate
6350 boundary and we can change the mode of the memory reference. */
6352 if (tmode != BLKmode
6353 && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
6354 && !MEM_P (inner)
6355 && (inner_mode == tmode
6356 || !REG_P (inner)
6357 || TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (tmode),
6358 GET_MODE_BITSIZE (inner_mode))
6359 || reg_truncated_to_mode (tmode, inner))
6360 && (! in_dest
6361 || (REG_P (inner)
6362 && have_insn_for (STRICT_LOW_PART, tmode))))
6363 || (MEM_P (inner) && pos_rtx == 0
6364 && (pos
6365 % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
6366 : BITS_PER_UNIT)) == 0
6367 /* We can't do this if we are widening INNER_MODE (it
6368 may not be aligned, for one thing). */
6369 && GET_MODE_BITSIZE (inner_mode) >= GET_MODE_BITSIZE (tmode)
6370 && (inner_mode == tmode
6371 || (! mode_dependent_address_p (XEXP (inner, 0))
6372 && ! MEM_VOLATILE_P (inner))))))
6374 /* If INNER is a MEM, make a new MEM that encompasses just the desired
6375 field. If the original and current mode are the same, we need not
6376 adjust the offset. Otherwise, we do if bytes big endian.
6378 If INNER is not a MEM, get a piece consisting of just the field
6379 of interest (in this case POS % BITS_PER_WORD must be 0). */
6381 if (MEM_P (inner))
6383 HOST_WIDE_INT offset;
6385 /* POS counts from lsb, but make OFFSET count in memory order. */
6386 if (BYTES_BIG_ENDIAN)
6387 offset = (GET_MODE_BITSIZE (is_mode) - len - pos) / BITS_PER_UNIT;
6388 else
6389 offset = pos / BITS_PER_UNIT;
6391 new = adjust_address_nv (inner, tmode, offset);
6393 else if (REG_P (inner))
6395 if (tmode != inner_mode)
6397 /* We can't call gen_lowpart in a DEST since we
6398 always want a SUBREG (see below) and it would sometimes
6399 return a new hard register. */
6400 if (pos || in_dest)
6402 HOST_WIDE_INT final_word = pos / BITS_PER_WORD;
6404 if (WORDS_BIG_ENDIAN
6405 && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
6406 final_word = ((GET_MODE_SIZE (inner_mode)
6407 - GET_MODE_SIZE (tmode))
6408 / UNITS_PER_WORD) - final_word;
6410 final_word *= UNITS_PER_WORD;
6411 if (BYTES_BIG_ENDIAN &&
6412 GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (tmode))
6413 final_word += (GET_MODE_SIZE (inner_mode)
6414 - GET_MODE_SIZE (tmode)) % UNITS_PER_WORD;
6416 /* Avoid creating invalid subregs, for example when
6417 simplifying (x>>32)&255. */
6418 if (!validate_subreg (tmode, inner_mode, inner, final_word))
6419 return NULL_RTX;
6421 new = gen_rtx_SUBREG (tmode, inner, final_word);
6423 else
6424 new = gen_lowpart (tmode, inner);
6426 else
6427 new = inner;
6429 else
6430 new = force_to_mode (inner, tmode,
6431 len >= HOST_BITS_PER_WIDE_INT
6432 ? ~(unsigned HOST_WIDE_INT) 0
6433 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
6436 /* If this extraction is going into the destination of a SET,
6437 make a STRICT_LOW_PART unless we made a MEM. */
6439 if (in_dest)
6440 return (MEM_P (new) ? new
6441 : (GET_CODE (new) != SUBREG
6442 ? gen_rtx_CLOBBER (tmode, const0_rtx)
6443 : gen_rtx_STRICT_LOW_PART (VOIDmode, new)));
6445 if (mode == tmode)
6446 return new;
6448 if (GET_CODE (new) == CONST_INT)
6449 return gen_int_mode (INTVAL (new), mode);
6451 /* If we know that no extraneous bits are set, and that the high
6452 bit is not set, convert the extraction to the cheaper of
6453 sign and zero extension, that are equivalent in these cases. */
6454 if (flag_expensive_optimizations
6455 && (GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
6456 && ((nonzero_bits (new, tmode)
6457 & ~(((unsigned HOST_WIDE_INT)
6458 GET_MODE_MASK (tmode))
6459 >> 1))
6460 == 0)))
6462 rtx temp = gen_rtx_ZERO_EXTEND (mode, new);
6463 rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new);
6465 /* Prefer ZERO_EXTENSION, since it gives more information to
6466 backends. */
6467 if (rtx_cost (temp, SET) <= rtx_cost (temp1, SET))
6468 return temp;
6469 return temp1;
6472 /* Otherwise, sign- or zero-extend unless we already are in the
6473 proper mode. */
6475 return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
6476 mode, new));
6479 /* Unless this is a COMPARE or we have a funny memory reference,
6480 don't do anything with zero-extending field extracts starting at
6481 the low-order bit since they are simple AND operations. */
6482 if (pos_rtx == 0 && pos == 0 && ! in_dest
6483 && ! in_compare && unsignedp)
6484 return 0;
6486 /* Unless INNER is not MEM, reject this if we would be spanning bytes or
6487 if the position is not a constant and the length is not 1. In all
6488 other cases, we would only be going outside our object in cases when
6489 an original shift would have been undefined. */
6490 if (MEM_P (inner)
6491 && ((pos_rtx == 0 && pos + len > GET_MODE_BITSIZE (is_mode))
6492 || (pos_rtx != 0 && len != 1)))
6493 return 0;
6495 /* Get the mode to use should INNER not be a MEM, the mode for the position,
6496 and the mode for the result. */
6497 if (in_dest && mode_for_extraction (EP_insv, -1) != MAX_MACHINE_MODE)
6499 wanted_inner_reg_mode = mode_for_extraction (EP_insv, 0);
6500 pos_mode = mode_for_extraction (EP_insv, 2);
6501 extraction_mode = mode_for_extraction (EP_insv, 3);
6504 if (! in_dest && unsignedp
6505 && mode_for_extraction (EP_extzv, -1) != MAX_MACHINE_MODE)
6507 wanted_inner_reg_mode = mode_for_extraction (EP_extzv, 1);
6508 pos_mode = mode_for_extraction (EP_extzv, 3);
6509 extraction_mode = mode_for_extraction (EP_extzv, 0);
6512 if (! in_dest && ! unsignedp
6513 && mode_for_extraction (EP_extv, -1) != MAX_MACHINE_MODE)
6515 wanted_inner_reg_mode = mode_for_extraction (EP_extv, 1);
6516 pos_mode = mode_for_extraction (EP_extv, 3);
6517 extraction_mode = mode_for_extraction (EP_extv, 0);
6520 /* Never narrow an object, since that might not be safe. */
6522 if (mode != VOIDmode
6523 && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
6524 extraction_mode = mode;
6526 if (pos_rtx && GET_MODE (pos_rtx) != VOIDmode
6527 && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
6528 pos_mode = GET_MODE (pos_rtx);
6530 /* If this is not from memory, the desired mode is the preferred mode
6531 for an extraction pattern's first input operand, or word_mode if there
6532 is none. */
6533 if (!MEM_P (inner))
6534 wanted_inner_mode = wanted_inner_reg_mode;
6535 else
6537 /* Be careful not to go beyond the extracted object and maintain the
6538 natural alignment of the memory. */
6539 wanted_inner_mode = smallest_mode_for_size (len, MODE_INT);
6540 while (pos % GET_MODE_BITSIZE (wanted_inner_mode) + len
6541 > GET_MODE_BITSIZE (wanted_inner_mode))
6543 wanted_inner_mode = GET_MODE_WIDER_MODE (wanted_inner_mode);
6544 gcc_assert (wanted_inner_mode != VOIDmode);
6547 /* If we have to change the mode of memory and cannot, the desired mode
6548 is EXTRACTION_MODE. */
6549 if (inner_mode != wanted_inner_mode
6550 && (mode_dependent_address_p (XEXP (inner, 0))
6551 || MEM_VOLATILE_P (inner)
6552 || pos_rtx))
6553 wanted_inner_mode = extraction_mode;
6556 orig_pos = pos;
6558 if (BITS_BIG_ENDIAN)
6560 /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
6561 BITS_BIG_ENDIAN style. If position is constant, compute new
6562 position. Otherwise, build subtraction.
6563 Note that POS is relative to the mode of the original argument.
6564 If it's a MEM we need to recompute POS relative to that.
6565 However, if we're extracting from (or inserting into) a register,
6566 we want to recompute POS relative to wanted_inner_mode. */
6567 int width = (MEM_P (inner)
6568 ? GET_MODE_BITSIZE (is_mode)
6569 : GET_MODE_BITSIZE (wanted_inner_mode));
6571 if (pos_rtx == 0)
6572 pos = width - len - pos;
6573 else
6574 pos_rtx
6575 = gen_rtx_MINUS (GET_MODE (pos_rtx), GEN_INT (width - len), pos_rtx);
6576 /* POS may be less than 0 now, but we check for that below.
6577 Note that it can only be less than 0 if !MEM_P (inner). */
6580 /* If INNER has a wider mode, and this is a constant extraction, try to
6581 make it smaller and adjust the byte to point to the byte containing
6582 the value. */
6583 if (wanted_inner_mode != VOIDmode
6584 && inner_mode != wanted_inner_mode
6585 && ! pos_rtx
6586 && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
6587 && MEM_P (inner)
6588 && ! mode_dependent_address_p (XEXP (inner, 0))
6589 && ! MEM_VOLATILE_P (inner))
6591 int offset = 0;
6593 /* The computations below will be correct if the machine is big
6594 endian in both bits and bytes or little endian in bits and bytes.
6595 If it is mixed, we must adjust. */
6597 /* If bytes are big endian and we had a paradoxical SUBREG, we must
6598 adjust OFFSET to compensate. */
6599 if (BYTES_BIG_ENDIAN
6600 && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
6601 offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
6603 /* We can now move to the desired byte. */
6604 offset += (pos / GET_MODE_BITSIZE (wanted_inner_mode))
6605 * GET_MODE_SIZE (wanted_inner_mode);
6606 pos %= GET_MODE_BITSIZE (wanted_inner_mode);
6608 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
6609 && is_mode != wanted_inner_mode)
6610 offset = (GET_MODE_SIZE (is_mode)
6611 - GET_MODE_SIZE (wanted_inner_mode) - offset);
6613 inner = adjust_address_nv (inner, wanted_inner_mode, offset);
6616 /* If INNER is not memory, we can always get it into the proper mode. If we
6617 are changing its mode, POS must be a constant and smaller than the size
6618 of the new mode. */
6619 else if (!MEM_P (inner))
6621 if (GET_MODE (inner) != wanted_inner_mode
6622 && (pos_rtx != 0
6623 || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
6624 return 0;
6626 if (orig_pos < 0)
6627 return 0;
6629 inner = force_to_mode (inner, wanted_inner_mode,
6630 pos_rtx
6631 || len + orig_pos >= HOST_BITS_PER_WIDE_INT
6632 ? ~(unsigned HOST_WIDE_INT) 0
6633 : ((((unsigned HOST_WIDE_INT) 1 << len) - 1)
6634 << orig_pos),
6638 /* Adjust mode of POS_RTX, if needed. If we want a wider mode, we
6639 have to zero extend. Otherwise, we can just use a SUBREG. */
6640 if (pos_rtx != 0
6641 && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
6643 rtx temp = gen_rtx_ZERO_EXTEND (pos_mode, pos_rtx);
6645 /* If we know that no extraneous bits are set, and that the high
6646 bit is not set, convert extraction to cheaper one - either
6647 SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
6648 cases. */
6649 if (flag_expensive_optimizations
6650 && (GET_MODE_BITSIZE (GET_MODE (pos_rtx)) <= HOST_BITS_PER_WIDE_INT
6651 && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
6652 & ~(((unsigned HOST_WIDE_INT)
6653 GET_MODE_MASK (GET_MODE (pos_rtx)))
6654 >> 1))
6655 == 0)))
6657 rtx temp1 = gen_rtx_SIGN_EXTEND (pos_mode, pos_rtx);
6659 /* Prefer ZERO_EXTENSION, since it gives more information to
6660 backends. */
6661 if (rtx_cost (temp1, SET) < rtx_cost (temp, SET))
6662 temp = temp1;
6664 pos_rtx = temp;
6666 else if (pos_rtx != 0
6667 && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
6668 pos_rtx = gen_lowpart (pos_mode, pos_rtx);
6670 /* Make POS_RTX unless we already have it and it is correct. If we don't
6671 have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
6672 be a CONST_INT. */
6673 if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
6674 pos_rtx = orig_pos_rtx;
6676 else if (pos_rtx == 0)
6677 pos_rtx = GEN_INT (pos);
6679 /* Make the required operation. See if we can use existing rtx. */
6680 new = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
6681 extraction_mode, inner, GEN_INT (len), pos_rtx);
6682 if (! in_dest)
6683 new = gen_lowpart (mode, new);
6685 return new;
6688 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
6689 with any other operations in X. Return X without that shift if so. */
6691 static rtx
6692 extract_left_shift (rtx x, int count)
6694 enum rtx_code code = GET_CODE (x);
6695 enum machine_mode mode = GET_MODE (x);
6696 rtx tem;
6698 switch (code)
6700 case ASHIFT:
6701 /* This is the shift itself. If it is wide enough, we will return
6702 either the value being shifted if the shift count is equal to
6703 COUNT or a shift for the difference. */
6704 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6705 && INTVAL (XEXP (x, 1)) >= count)
6706 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
6707 INTVAL (XEXP (x, 1)) - count);
6708 break;
6710 case NEG: case NOT:
6711 if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
6712 return simplify_gen_unary (code, mode, tem, mode);
6714 break;
6716 case PLUS: case IOR: case XOR: case AND:
6717 /* If we can safely shift this constant and we find the inner shift,
6718 make a new operation. */
6719 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6720 && (INTVAL (XEXP (x, 1)) & ((((HOST_WIDE_INT) 1 << count)) - 1)) == 0
6721 && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
6722 return simplify_gen_binary (code, mode, tem,
6723 GEN_INT (INTVAL (XEXP (x, 1)) >> count));
6725 break;
6727 default:
6728 break;
6731 return 0;
6734 /* Look at the expression rooted at X. Look for expressions
6735 equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
6736 Form these expressions.
6738 Return the new rtx, usually just X.
6740 Also, for machines like the VAX that don't have logical shift insns,
6741 try to convert logical to arithmetic shift operations in cases where
6742 they are equivalent. This undoes the canonicalizations to logical
6743 shifts done elsewhere.
6745 We try, as much as possible, to re-use rtl expressions to save memory.
6747 IN_CODE says what kind of expression we are processing. Normally, it is
6748 SET. In a memory address (inside a MEM, PLUS or minus, the latter two
6749 being kludges), it is MEM. When processing the arguments of a comparison
6750 or a COMPARE against zero, it is COMPARE. */
6752 static rtx
6753 make_compound_operation (rtx x, enum rtx_code in_code)
6755 enum rtx_code code = GET_CODE (x);
6756 enum machine_mode mode = GET_MODE (x);
6757 int mode_width = GET_MODE_BITSIZE (mode);
6758 rtx rhs, lhs;
6759 enum rtx_code next_code;
6760 int i;
6761 rtx new = 0;
6762 rtx tem;
6763 const char *fmt;
6765 /* Select the code to be used in recursive calls. Once we are inside an
6766 address, we stay there. If we have a comparison, set to COMPARE,
6767 but once inside, go back to our default of SET. */
6769 next_code = (code == MEM || code == PLUS || code == MINUS ? MEM
6770 : ((code == COMPARE || COMPARISON_P (x))
6771 && XEXP (x, 1) == const0_rtx) ? COMPARE
6772 : in_code == COMPARE ? SET : in_code);
6774 /* Process depending on the code of this operation. If NEW is set
6775 nonzero, it will be returned. */
6777 switch (code)
6779 case ASHIFT:
6780 /* Convert shifts by constants into multiplications if inside
6781 an address. */
6782 if (in_code == MEM && GET_CODE (XEXP (x, 1)) == CONST_INT
6783 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
6784 && INTVAL (XEXP (x, 1)) >= 0)
6786 new = make_compound_operation (XEXP (x, 0), next_code);
6787 new = gen_rtx_MULT (mode, new,
6788 GEN_INT ((HOST_WIDE_INT) 1
6789 << INTVAL (XEXP (x, 1))));
6791 break;
6793 case AND:
6794 /* If the second operand is not a constant, we can't do anything
6795 with it. */
6796 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
6797 break;
6799 /* If the constant is a power of two minus one and the first operand
6800 is a logical right shift, make an extraction. */
6801 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6802 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6804 new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
6805 new = make_extraction (mode, new, 0, XEXP (XEXP (x, 0), 1), i, 1,
6806 0, in_code == COMPARE);
6809 /* Same as previous, but for (subreg (lshiftrt ...)) in first op. */
6810 else if (GET_CODE (XEXP (x, 0)) == SUBREG
6811 && subreg_lowpart_p (XEXP (x, 0))
6812 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
6813 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6815 new = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
6816 next_code);
6817 new = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new, 0,
6818 XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
6819 0, in_code == COMPARE);
6821 /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)). */
6822 else if ((GET_CODE (XEXP (x, 0)) == XOR
6823 || GET_CODE (XEXP (x, 0)) == IOR)
6824 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
6825 && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
6826 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6828 /* Apply the distributive law, and then try to make extractions. */
6829 new = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
6830 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
6831 XEXP (x, 1)),
6832 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
6833 XEXP (x, 1)));
6834 new = make_compound_operation (new, in_code);
6837 /* If we are have (and (rotate X C) M) and C is larger than the number
6838 of bits in M, this is an extraction. */
6840 else if (GET_CODE (XEXP (x, 0)) == ROTATE
6841 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6842 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0
6843 && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
6845 new = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
6846 new = make_extraction (mode, new,
6847 (GET_MODE_BITSIZE (mode)
6848 - INTVAL (XEXP (XEXP (x, 0), 1))),
6849 NULL_RTX, i, 1, 0, in_code == COMPARE);
6852 /* On machines without logical shifts, if the operand of the AND is
6853 a logical shift and our mask turns off all the propagated sign
6854 bits, we can replace the logical shift with an arithmetic shift. */
6855 else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6856 && !have_insn_for (LSHIFTRT, mode)
6857 && have_insn_for (ASHIFTRT, mode)
6858 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6859 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
6860 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
6861 && mode_width <= HOST_BITS_PER_WIDE_INT)
6863 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
6865 mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
6866 if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
6867 SUBST (XEXP (x, 0),
6868 gen_rtx_ASHIFTRT (mode,
6869 make_compound_operation
6870 (XEXP (XEXP (x, 0), 0), next_code),
6871 XEXP (XEXP (x, 0), 1)));
6874 /* If the constant is one less than a power of two, this might be
6875 representable by an extraction even if no shift is present.
6876 If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
6877 we are in a COMPARE. */
6878 else if ((i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6879 new = make_extraction (mode,
6880 make_compound_operation (XEXP (x, 0),
6881 next_code),
6882 0, NULL_RTX, i, 1, 0, in_code == COMPARE);
6884 /* If we are in a comparison and this is an AND with a power of two,
6885 convert this into the appropriate bit extract. */
6886 else if (in_code == COMPARE
6887 && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
6888 new = make_extraction (mode,
6889 make_compound_operation (XEXP (x, 0),
6890 next_code),
6891 i, NULL_RTX, 1, 1, 0, 1);
6893 break;
6895 case LSHIFTRT:
6896 /* If the sign bit is known to be zero, replace this with an
6897 arithmetic shift. */
6898 if (have_insn_for (ASHIFTRT, mode)
6899 && ! have_insn_for (LSHIFTRT, mode)
6900 && mode_width <= HOST_BITS_PER_WIDE_INT
6901 && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
6903 new = gen_rtx_ASHIFTRT (mode,
6904 make_compound_operation (XEXP (x, 0),
6905 next_code),
6906 XEXP (x, 1));
6907 break;
6910 /* ... fall through ... */
6912 case ASHIFTRT:
6913 lhs = XEXP (x, 0);
6914 rhs = XEXP (x, 1);
6916 /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
6917 this is a SIGN_EXTRACT. */
6918 if (GET_CODE (rhs) == CONST_INT
6919 && GET_CODE (lhs) == ASHIFT
6920 && GET_CODE (XEXP (lhs, 1)) == CONST_INT
6921 && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1)))
6923 new = make_compound_operation (XEXP (lhs, 0), next_code);
6924 new = make_extraction (mode, new,
6925 INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
6926 NULL_RTX, mode_width - INTVAL (rhs),
6927 code == LSHIFTRT, 0, in_code == COMPARE);
6928 break;
6931 /* See if we have operations between an ASHIFTRT and an ASHIFT.
6932 If so, try to merge the shifts into a SIGN_EXTEND. We could
6933 also do this for some cases of SIGN_EXTRACT, but it doesn't
6934 seem worth the effort; the case checked for occurs on Alpha. */
6936 if (!OBJECT_P (lhs)
6937 && ! (GET_CODE (lhs) == SUBREG
6938 && (OBJECT_P (SUBREG_REG (lhs))))
6939 && GET_CODE (rhs) == CONST_INT
6940 && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
6941 && (new = extract_left_shift (lhs, INTVAL (rhs))) != 0)
6942 new = make_extraction (mode, make_compound_operation (new, next_code),
6943 0, NULL_RTX, mode_width - INTVAL (rhs),
6944 code == LSHIFTRT, 0, in_code == COMPARE);
6946 break;
6948 case SUBREG:
6949 /* Call ourselves recursively on the inner expression. If we are
6950 narrowing the object and it has a different RTL code from
6951 what it originally did, do this SUBREG as a force_to_mode. */
6953 tem = make_compound_operation (SUBREG_REG (x), in_code);
6956 rtx simplified;
6957 simplified = simplify_subreg (GET_MODE (x), tem, GET_MODE (tem),
6958 SUBREG_BYTE (x));
6960 if (simplified)
6961 tem = simplified;
6963 if (GET_CODE (tem) != GET_CODE (SUBREG_REG (x))
6964 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (tem))
6965 && subreg_lowpart_p (x))
6967 rtx newer = force_to_mode (tem, mode, ~(HOST_WIDE_INT) 0,
6970 /* If we have something other than a SUBREG, we might have
6971 done an expansion, so rerun ourselves. */
6972 if (GET_CODE (newer) != SUBREG)
6973 newer = make_compound_operation (newer, in_code);
6975 return newer;
6978 if (simplified)
6979 return tem;
6981 break;
6983 default:
6984 break;
6987 if (new)
6989 x = gen_lowpart (mode, new);
6990 code = GET_CODE (x);
6993 /* Now recursively process each operand of this operation. */
6994 fmt = GET_RTX_FORMAT (code);
6995 for (i = 0; i < GET_RTX_LENGTH (code); i++)
6996 if (fmt[i] == 'e')
6998 new = make_compound_operation (XEXP (x, i), next_code);
6999 SUBST (XEXP (x, i), new);
7002 /* If this is a commutative operation, the changes to the operands
7003 may have made it noncanonical. */
7004 if (COMMUTATIVE_ARITH_P (x)
7005 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
7007 tem = XEXP (x, 0);
7008 SUBST (XEXP (x, 0), XEXP (x, 1));
7009 SUBST (XEXP (x, 1), tem);
7012 return x;
7015 /* Given M see if it is a value that would select a field of bits
7016 within an item, but not the entire word. Return -1 if not.
7017 Otherwise, return the starting position of the field, where 0 is the
7018 low-order bit.
7020 *PLEN is set to the length of the field. */
7022 static int
7023 get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
7025 /* Get the bit number of the first 1 bit from the right, -1 if none. */
7026 int pos = exact_log2 (m & -m);
7027 int len = 0;
7029 if (pos >= 0)
7030 /* Now shift off the low-order zero bits and see if we have a
7031 power of two minus 1. */
7032 len = exact_log2 ((m >> pos) + 1);
7034 if (len <= 0)
7035 pos = -1;
7037 *plen = len;
7038 return pos;
7041 /* If X refers to a register that equals REG in value, replace these
7042 references with REG. */
7043 static rtx
7044 canon_reg_for_combine (rtx x, rtx reg)
7046 rtx op0, op1, op2;
7047 const char *fmt;
7048 int i;
7049 bool copied;
7051 enum rtx_code code = GET_CODE (x);
7052 switch (GET_RTX_CLASS (code))
7054 case RTX_UNARY:
7055 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7056 if (op0 != XEXP (x, 0))
7057 return simplify_gen_unary (GET_CODE (x), GET_MODE (x), op0,
7058 GET_MODE (reg));
7059 break;
7061 case RTX_BIN_ARITH:
7062 case RTX_COMM_ARITH:
7063 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7064 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7065 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7066 return simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
7067 break;
7069 case RTX_COMPARE:
7070 case RTX_COMM_COMPARE:
7071 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7072 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7073 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7074 return simplify_gen_relational (GET_CODE (x), GET_MODE (x),
7075 GET_MODE (op0), op0, op1);
7076 break;
7078 case RTX_TERNARY:
7079 case RTX_BITFIELD_OPS:
7080 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7081 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7082 op2 = canon_reg_for_combine (XEXP (x, 2), reg);
7083 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1) || op2 != XEXP (x, 2))
7084 return simplify_gen_ternary (GET_CODE (x), GET_MODE (x),
7085 GET_MODE (op0), op0, op1, op2);
7087 case RTX_OBJ:
7088 if (REG_P (x))
7090 if (rtx_equal_p (get_last_value (reg), x)
7091 || rtx_equal_p (reg, get_last_value (x)))
7092 return reg;
7093 else
7094 break;
7097 /* fall through */
7099 default:
7100 fmt = GET_RTX_FORMAT (code);
7101 copied = false;
7102 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7103 if (fmt[i] == 'e')
7105 rtx op = canon_reg_for_combine (XEXP (x, i), reg);
7106 if (op != XEXP (x, i))
7108 if (!copied)
7110 copied = true;
7111 x = copy_rtx (x);
7113 XEXP (x, i) = op;
7116 else if (fmt[i] == 'E')
7118 int j;
7119 for (j = 0; j < XVECLEN (x, i); j++)
7121 rtx op = canon_reg_for_combine (XVECEXP (x, i, j), reg);
7122 if (op != XVECEXP (x, i, j))
7124 if (!copied)
7126 copied = true;
7127 x = copy_rtx (x);
7129 XVECEXP (x, i, j) = op;
7134 break;
7137 return x;
7140 /* Return X converted to MODE. If the value is already truncated to
7141 MODE we can just return a subreg even though in the general case we
7142 would need an explicit truncation. */
7144 static rtx
7145 gen_lowpart_or_truncate (enum machine_mode mode, rtx x)
7147 if (GET_MODE_SIZE (GET_MODE (x)) <= GET_MODE_SIZE (mode)
7148 || TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
7149 GET_MODE_BITSIZE (GET_MODE (x)))
7150 || (REG_P (x) && reg_truncated_to_mode (mode, x)))
7151 return gen_lowpart (mode, x);
7152 else
7153 return simplify_gen_unary (TRUNCATE, mode, x, GET_MODE (x));
7156 /* See if X can be simplified knowing that we will only refer to it in
7157 MODE and will only refer to those bits that are nonzero in MASK.
7158 If other bits are being computed or if masking operations are done
7159 that select a superset of the bits in MASK, they can sometimes be
7160 ignored.
7162 Return a possibly simplified expression, but always convert X to
7163 MODE. If X is a CONST_INT, AND the CONST_INT with MASK.
7165 If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
7166 are all off in X. This is used when X will be complemented, by either
7167 NOT, NEG, or XOR. */
7169 static rtx
7170 force_to_mode (rtx x, enum machine_mode mode, unsigned HOST_WIDE_INT mask,
7171 int just_select)
7173 enum rtx_code code = GET_CODE (x);
7174 int next_select = just_select || code == XOR || code == NOT || code == NEG;
7175 enum machine_mode op_mode;
7176 unsigned HOST_WIDE_INT fuller_mask, nonzero;
7177 rtx op0, op1, temp;
7179 /* If this is a CALL or ASM_OPERANDS, don't do anything. Some of the
7180 code below will do the wrong thing since the mode of such an
7181 expression is VOIDmode.
7183 Also do nothing if X is a CLOBBER; this can happen if X was
7184 the return value from a call to gen_lowpart. */
7185 if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
7186 return x;
7188 /* We want to perform the operation is its present mode unless we know
7189 that the operation is valid in MODE, in which case we do the operation
7190 in MODE. */
7191 op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
7192 && have_insn_for (code, mode))
7193 ? mode : GET_MODE (x));
7195 /* It is not valid to do a right-shift in a narrower mode
7196 than the one it came in with. */
7197 if ((code == LSHIFTRT || code == ASHIFTRT)
7198 && GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (GET_MODE (x)))
7199 op_mode = GET_MODE (x);
7201 /* Truncate MASK to fit OP_MODE. */
7202 if (op_mode)
7203 mask &= GET_MODE_MASK (op_mode);
7205 /* When we have an arithmetic operation, or a shift whose count we
7206 do not know, we need to assume that all bits up to the highest-order
7207 bit in MASK will be needed. This is how we form such a mask. */
7208 if (mask & ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
7209 fuller_mask = ~(unsigned HOST_WIDE_INT) 0;
7210 else
7211 fuller_mask = (((unsigned HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1))
7212 - 1);
7214 /* Determine what bits of X are guaranteed to be (non)zero. */
7215 nonzero = nonzero_bits (x, mode);
7217 /* If none of the bits in X are needed, return a zero. */
7218 if (!just_select && (nonzero & mask) == 0 && !side_effects_p (x))
7219 x = const0_rtx;
7221 /* If X is a CONST_INT, return a new one. Do this here since the
7222 test below will fail. */
7223 if (GET_CODE (x) == CONST_INT)
7225 if (SCALAR_INT_MODE_P (mode))
7226 return gen_int_mode (INTVAL (x) & mask, mode);
7227 else
7229 x = GEN_INT (INTVAL (x) & mask);
7230 return gen_lowpart_common (mode, x);
7234 /* If X is narrower than MODE and we want all the bits in X's mode, just
7235 get X in the proper mode. */
7236 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
7237 && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
7238 return gen_lowpart (mode, x);
7240 switch (code)
7242 case CLOBBER:
7243 /* If X is a (clobber (const_int)), return it since we know we are
7244 generating something that won't match. */
7245 return x;
7247 case SIGN_EXTEND:
7248 case ZERO_EXTEND:
7249 case ZERO_EXTRACT:
7250 case SIGN_EXTRACT:
7251 x = expand_compound_operation (x);
7252 if (GET_CODE (x) != code)
7253 return force_to_mode (x, mode, mask, next_select);
7254 break;
7256 case SUBREG:
7257 if (subreg_lowpart_p (x)
7258 /* We can ignore the effect of this SUBREG if it narrows the mode or
7259 if the constant masks to zero all the bits the mode doesn't
7260 have. */
7261 && ((GET_MODE_SIZE (GET_MODE (x))
7262 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
7263 || (0 == (mask
7264 & GET_MODE_MASK (GET_MODE (x))
7265 & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
7266 return force_to_mode (SUBREG_REG (x), mode, mask, next_select);
7267 break;
7269 case AND:
7270 /* If this is an AND with a constant, convert it into an AND
7271 whose constant is the AND of that constant with MASK. If it
7272 remains an AND of MASK, delete it since it is redundant. */
7274 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
7276 x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
7277 mask & INTVAL (XEXP (x, 1)));
7279 /* If X is still an AND, see if it is an AND with a mask that
7280 is just some low-order bits. If so, and it is MASK, we don't
7281 need it. */
7283 if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
7284 && ((INTVAL (XEXP (x, 1)) & GET_MODE_MASK (GET_MODE (x)))
7285 == mask))
7286 x = XEXP (x, 0);
7288 /* If it remains an AND, try making another AND with the bits
7289 in the mode mask that aren't in MASK turned on. If the
7290 constant in the AND is wide enough, this might make a
7291 cheaper constant. */
7293 if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
7294 && GET_MODE_MASK (GET_MODE (x)) != mask
7295 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
7297 HOST_WIDE_INT cval = (INTVAL (XEXP (x, 1))
7298 | (GET_MODE_MASK (GET_MODE (x)) & ~mask));
7299 int width = GET_MODE_BITSIZE (GET_MODE (x));
7300 rtx y;
7302 /* If MODE is narrower than HOST_WIDE_INT and CVAL is a negative
7303 number, sign extend it. */
7304 if (width > 0 && width < HOST_BITS_PER_WIDE_INT
7305 && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
7306 cval |= (HOST_WIDE_INT) -1 << width;
7308 y = simplify_gen_binary (AND, GET_MODE (x),
7309 XEXP (x, 0), GEN_INT (cval));
7310 if (rtx_cost (y, SET) < rtx_cost (x, SET))
7311 x = y;
7314 break;
7317 goto binop;
7319 case PLUS:
7320 /* In (and (plus FOO C1) M), if M is a mask that just turns off
7321 low-order bits (as in an alignment operation) and FOO is already
7322 aligned to that boundary, mask C1 to that boundary as well.
7323 This may eliminate that PLUS and, later, the AND. */
7326 unsigned int width = GET_MODE_BITSIZE (mode);
7327 unsigned HOST_WIDE_INT smask = mask;
7329 /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
7330 number, sign extend it. */
7332 if (width < HOST_BITS_PER_WIDE_INT
7333 && (smask & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
7334 smask |= (HOST_WIDE_INT) -1 << width;
7336 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7337 && exact_log2 (- smask) >= 0
7338 && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
7339 && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
7340 return force_to_mode (plus_constant (XEXP (x, 0),
7341 (INTVAL (XEXP (x, 1)) & smask)),
7342 mode, smask, next_select);
7345 /* ... fall through ... */
7347 case MULT:
7348 /* For PLUS, MINUS and MULT, we need any bits less significant than the
7349 most significant bit in MASK since carries from those bits will
7350 affect the bits we are interested in. */
7351 mask = fuller_mask;
7352 goto binop;
7354 case MINUS:
7355 /* If X is (minus C Y) where C's least set bit is larger than any bit
7356 in the mask, then we may replace with (neg Y). */
7357 if (GET_CODE (XEXP (x, 0)) == CONST_INT
7358 && (((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 0))
7359 & -INTVAL (XEXP (x, 0))))
7360 > mask))
7362 x = simplify_gen_unary (NEG, GET_MODE (x), XEXP (x, 1),
7363 GET_MODE (x));
7364 return force_to_mode (x, mode, mask, next_select);
7367 /* Similarly, if C contains every bit in the fuller_mask, then we may
7368 replace with (not Y). */
7369 if (GET_CODE (XEXP (x, 0)) == CONST_INT
7370 && ((INTVAL (XEXP (x, 0)) | (HOST_WIDE_INT) fuller_mask)
7371 == INTVAL (XEXP (x, 0))))
7373 x = simplify_gen_unary (NOT, GET_MODE (x),
7374 XEXP (x, 1), GET_MODE (x));
7375 return force_to_mode (x, mode, mask, next_select);
7378 mask = fuller_mask;
7379 goto binop;
7381 case IOR:
7382 case XOR:
7383 /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
7384 LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
7385 operation which may be a bitfield extraction. Ensure that the
7386 constant we form is not wider than the mode of X. */
7388 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7389 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
7390 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7391 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
7392 && GET_CODE (XEXP (x, 1)) == CONST_INT
7393 && ((INTVAL (XEXP (XEXP (x, 0), 1))
7394 + floor_log2 (INTVAL (XEXP (x, 1))))
7395 < GET_MODE_BITSIZE (GET_MODE (x)))
7396 && (INTVAL (XEXP (x, 1))
7397 & ~nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
7399 temp = GEN_INT ((INTVAL (XEXP (x, 1)) & mask)
7400 << INTVAL (XEXP (XEXP (x, 0), 1)));
7401 temp = simplify_gen_binary (GET_CODE (x), GET_MODE (x),
7402 XEXP (XEXP (x, 0), 0), temp);
7403 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), temp,
7404 XEXP (XEXP (x, 0), 1));
7405 return force_to_mode (x, mode, mask, next_select);
7408 binop:
7409 /* For most binary operations, just propagate into the operation and
7410 change the mode if we have an operation of that mode. */
7412 op0 = gen_lowpart_or_truncate (op_mode,
7413 force_to_mode (XEXP (x, 0), mode, mask,
7414 next_select));
7415 op1 = gen_lowpart_or_truncate (op_mode,
7416 force_to_mode (XEXP (x, 1), mode, mask,
7417 next_select));
7419 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7420 x = simplify_gen_binary (code, op_mode, op0, op1);
7421 break;
7423 case ASHIFT:
7424 /* For left shifts, do the same, but just for the first operand.
7425 However, we cannot do anything with shifts where we cannot
7426 guarantee that the counts are smaller than the size of the mode
7427 because such a count will have a different meaning in a
7428 wider mode. */
7430 if (! (GET_CODE (XEXP (x, 1)) == CONST_INT
7431 && INTVAL (XEXP (x, 1)) >= 0
7432 && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (mode))
7433 && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
7434 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
7435 < (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (mode))))
7436 break;
7438 /* If the shift count is a constant and we can do arithmetic in
7439 the mode of the shift, refine which bits we need. Otherwise, use the
7440 conservative form of the mask. */
7441 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7442 && INTVAL (XEXP (x, 1)) >= 0
7443 && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (op_mode)
7444 && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
7445 mask >>= INTVAL (XEXP (x, 1));
7446 else
7447 mask = fuller_mask;
7449 op0 = gen_lowpart_or_truncate (op_mode,
7450 force_to_mode (XEXP (x, 0), op_mode,
7451 mask, next_select));
7453 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
7454 x = simplify_gen_binary (code, op_mode, op0, XEXP (x, 1));
7455 break;
7457 case LSHIFTRT:
7458 /* Here we can only do something if the shift count is a constant,
7459 this shift constant is valid for the host, and we can do arithmetic
7460 in OP_MODE. */
7462 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7463 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
7464 && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
7466 rtx inner = XEXP (x, 0);
7467 unsigned HOST_WIDE_INT inner_mask;
7469 /* Select the mask of the bits we need for the shift operand. */
7470 inner_mask = mask << INTVAL (XEXP (x, 1));
7472 /* We can only change the mode of the shift if we can do arithmetic
7473 in the mode of the shift and INNER_MASK is no wider than the
7474 width of X's mode. */
7475 if ((inner_mask & ~GET_MODE_MASK (GET_MODE (x))) != 0)
7476 op_mode = GET_MODE (x);
7478 inner = force_to_mode (inner, op_mode, inner_mask, next_select);
7480 if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
7481 x = simplify_gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
7484 /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
7485 shift and AND produces only copies of the sign bit (C2 is one less
7486 than a power of two), we can do this with just a shift. */
7488 if (GET_CODE (x) == LSHIFTRT
7489 && GET_CODE (XEXP (x, 1)) == CONST_INT
7490 /* The shift puts one of the sign bit copies in the least significant
7491 bit. */
7492 && ((INTVAL (XEXP (x, 1))
7493 + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
7494 >= GET_MODE_BITSIZE (GET_MODE (x)))
7495 && exact_log2 (mask + 1) >= 0
7496 /* Number of bits left after the shift must be more than the mask
7497 needs. */
7498 && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
7499 <= GET_MODE_BITSIZE (GET_MODE (x)))
7500 /* Must be more sign bit copies than the mask needs. */
7501 && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
7502 >= exact_log2 (mask + 1)))
7503 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
7504 GEN_INT (GET_MODE_BITSIZE (GET_MODE (x))
7505 - exact_log2 (mask + 1)));
7507 goto shiftrt;
7509 case ASHIFTRT:
7510 /* If we are just looking for the sign bit, we don't need this shift at
7511 all, even if it has a variable count. */
7512 if (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
7513 && (mask == ((unsigned HOST_WIDE_INT) 1
7514 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
7515 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
7517 /* If this is a shift by a constant, get a mask that contains those bits
7518 that are not copies of the sign bit. We then have two cases: If
7519 MASK only includes those bits, this can be a logical shift, which may
7520 allow simplifications. If MASK is a single-bit field not within
7521 those bits, we are requesting a copy of the sign bit and hence can
7522 shift the sign bit to the appropriate location. */
7524 if (GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) >= 0
7525 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
7527 int i;
7529 /* If the considered data is wider than HOST_WIDE_INT, we can't
7530 represent a mask for all its bits in a single scalar.
7531 But we only care about the lower bits, so calculate these. */
7533 if (GET_MODE_BITSIZE (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
7535 nonzero = ~(HOST_WIDE_INT) 0;
7537 /* GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
7538 is the number of bits a full-width mask would have set.
7539 We need only shift if these are fewer than nonzero can
7540 hold. If not, we must keep all bits set in nonzero. */
7542 if (GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
7543 < HOST_BITS_PER_WIDE_INT)
7544 nonzero >>= INTVAL (XEXP (x, 1))
7545 + HOST_BITS_PER_WIDE_INT
7546 - GET_MODE_BITSIZE (GET_MODE (x)) ;
7548 else
7550 nonzero = GET_MODE_MASK (GET_MODE (x));
7551 nonzero >>= INTVAL (XEXP (x, 1));
7554 if ((mask & ~nonzero) == 0)
7556 x = simplify_shift_const (NULL_RTX, LSHIFTRT, GET_MODE (x),
7557 XEXP (x, 0), INTVAL (XEXP (x, 1)));
7558 if (GET_CODE (x) != ASHIFTRT)
7559 return force_to_mode (x, mode, mask, next_select);
7562 else if ((i = exact_log2 (mask)) >= 0)
7564 x = simplify_shift_const
7565 (NULL_RTX, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
7566 GET_MODE_BITSIZE (GET_MODE (x)) - 1 - i);
7568 if (GET_CODE (x) != ASHIFTRT)
7569 return force_to_mode (x, mode, mask, next_select);
7573 /* If MASK is 1, convert this to an LSHIFTRT. This can be done
7574 even if the shift count isn't a constant. */
7575 if (mask == 1)
7576 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
7577 XEXP (x, 0), XEXP (x, 1));
7579 shiftrt:
7581 /* If this is a zero- or sign-extension operation that just affects bits
7582 we don't care about, remove it. Be sure the call above returned
7583 something that is still a shift. */
7585 if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
7586 && GET_CODE (XEXP (x, 1)) == CONST_INT
7587 && INTVAL (XEXP (x, 1)) >= 0
7588 && (INTVAL (XEXP (x, 1))
7589 <= GET_MODE_BITSIZE (GET_MODE (x)) - (floor_log2 (mask) + 1))
7590 && GET_CODE (XEXP (x, 0)) == ASHIFT
7591 && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
7592 return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
7593 next_select);
7595 break;
7597 case ROTATE:
7598 case ROTATERT:
7599 /* If the shift count is constant and we can do computations
7600 in the mode of X, compute where the bits we care about are.
7601 Otherwise, we can't do anything. Don't change the mode of
7602 the shift or propagate MODE into the shift, though. */
7603 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7604 && INTVAL (XEXP (x, 1)) >= 0)
7606 temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
7607 GET_MODE (x), GEN_INT (mask),
7608 XEXP (x, 1));
7609 if (temp && GET_CODE (temp) == CONST_INT)
7610 SUBST (XEXP (x, 0),
7611 force_to_mode (XEXP (x, 0), GET_MODE (x),
7612 INTVAL (temp), next_select));
7614 break;
7616 case NEG:
7617 /* If we just want the low-order bit, the NEG isn't needed since it
7618 won't change the low-order bit. */
7619 if (mask == 1)
7620 return force_to_mode (XEXP (x, 0), mode, mask, just_select);
7622 /* We need any bits less significant than the most significant bit in
7623 MASK since carries from those bits will affect the bits we are
7624 interested in. */
7625 mask = fuller_mask;
7626 goto unop;
7628 case NOT:
7629 /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
7630 same as the XOR case above. Ensure that the constant we form is not
7631 wider than the mode of X. */
7633 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7634 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
7635 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7636 && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
7637 < GET_MODE_BITSIZE (GET_MODE (x)))
7638 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
7640 temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)),
7641 GET_MODE (x));
7642 temp = simplify_gen_binary (XOR, GET_MODE (x),
7643 XEXP (XEXP (x, 0), 0), temp);
7644 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
7645 temp, XEXP (XEXP (x, 0), 1));
7647 return force_to_mode (x, mode, mask, next_select);
7650 /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
7651 use the full mask inside the NOT. */
7652 mask = fuller_mask;
7654 unop:
7655 op0 = gen_lowpart_or_truncate (op_mode,
7656 force_to_mode (XEXP (x, 0), mode, mask,
7657 next_select));
7658 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
7659 x = simplify_gen_unary (code, op_mode, op0, op_mode);
7660 break;
7662 case NE:
7663 /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
7664 in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
7665 which is equal to STORE_FLAG_VALUE. */
7666 if ((mask & ~STORE_FLAG_VALUE) == 0 && XEXP (x, 1) == const0_rtx
7667 && GET_MODE (XEXP (x, 0)) == mode
7668 && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
7669 && (nonzero_bits (XEXP (x, 0), mode)
7670 == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
7671 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
7673 break;
7675 case IF_THEN_ELSE:
7676 /* We have no way of knowing if the IF_THEN_ELSE can itself be
7677 written in a narrower mode. We play it safe and do not do so. */
7679 SUBST (XEXP (x, 1),
7680 gen_lowpart_or_truncate (GET_MODE (x),
7681 force_to_mode (XEXP (x, 1), mode,
7682 mask, next_select)));
7683 SUBST (XEXP (x, 2),
7684 gen_lowpart_or_truncate (GET_MODE (x),
7685 force_to_mode (XEXP (x, 2), mode,
7686 mask, next_select)));
7687 break;
7689 default:
7690 break;
7693 /* Ensure we return a value of the proper mode. */
7694 return gen_lowpart_or_truncate (mode, x);
7697 /* Return nonzero if X is an expression that has one of two values depending on
7698 whether some other value is zero or nonzero. In that case, we return the
7699 value that is being tested, *PTRUE is set to the value if the rtx being
7700 returned has a nonzero value, and *PFALSE is set to the other alternative.
7702 If we return zero, we set *PTRUE and *PFALSE to X. */
7704 static rtx
7705 if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
7707 enum machine_mode mode = GET_MODE (x);
7708 enum rtx_code code = GET_CODE (x);
7709 rtx cond0, cond1, true0, true1, false0, false1;
7710 unsigned HOST_WIDE_INT nz;
7712 /* If we are comparing a value against zero, we are done. */
7713 if ((code == NE || code == EQ)
7714 && XEXP (x, 1) == const0_rtx)
7716 *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
7717 *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
7718 return XEXP (x, 0);
7721 /* If this is a unary operation whose operand has one of two values, apply
7722 our opcode to compute those values. */
7723 else if (UNARY_P (x)
7724 && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
7726 *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
7727 *pfalse = simplify_gen_unary (code, mode, false0,
7728 GET_MODE (XEXP (x, 0)));
7729 return cond0;
7732 /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
7733 make can't possibly match and would suppress other optimizations. */
7734 else if (code == COMPARE)
7737 /* If this is a binary operation, see if either side has only one of two
7738 values. If either one does or if both do and they are conditional on
7739 the same value, compute the new true and false values. */
7740 else if (BINARY_P (x))
7742 cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
7743 cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
7745 if ((cond0 != 0 || cond1 != 0)
7746 && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
7748 /* If if_then_else_cond returned zero, then true/false are the
7749 same rtl. We must copy one of them to prevent invalid rtl
7750 sharing. */
7751 if (cond0 == 0)
7752 true0 = copy_rtx (true0);
7753 else if (cond1 == 0)
7754 true1 = copy_rtx (true1);
7756 if (COMPARISON_P (x))
7758 *ptrue = simplify_gen_relational (code, mode, VOIDmode,
7759 true0, true1);
7760 *pfalse = simplify_gen_relational (code, mode, VOIDmode,
7761 false0, false1);
7763 else
7765 *ptrue = simplify_gen_binary (code, mode, true0, true1);
7766 *pfalse = simplify_gen_binary (code, mode, false0, false1);
7769 return cond0 ? cond0 : cond1;
7772 /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
7773 operands is zero when the other is nonzero, and vice-versa,
7774 and STORE_FLAG_VALUE is 1 or -1. */
7776 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
7777 && (code == PLUS || code == IOR || code == XOR || code == MINUS
7778 || code == UMAX)
7779 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
7781 rtx op0 = XEXP (XEXP (x, 0), 1);
7782 rtx op1 = XEXP (XEXP (x, 1), 1);
7784 cond0 = XEXP (XEXP (x, 0), 0);
7785 cond1 = XEXP (XEXP (x, 1), 0);
7787 if (COMPARISON_P (cond0)
7788 && COMPARISON_P (cond1)
7789 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
7790 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
7791 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
7792 || ((swap_condition (GET_CODE (cond0))
7793 == reversed_comparison_code (cond1, NULL))
7794 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
7795 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
7796 && ! side_effects_p (x))
7798 *ptrue = simplify_gen_binary (MULT, mode, op0, const_true_rtx);
7799 *pfalse = simplify_gen_binary (MULT, mode,
7800 (code == MINUS
7801 ? simplify_gen_unary (NEG, mode,
7802 op1, mode)
7803 : op1),
7804 const_true_rtx);
7805 return cond0;
7809 /* Similarly for MULT, AND and UMIN, except that for these the result
7810 is always zero. */
7811 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
7812 && (code == MULT || code == AND || code == UMIN)
7813 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
7815 cond0 = XEXP (XEXP (x, 0), 0);
7816 cond1 = XEXP (XEXP (x, 1), 0);
7818 if (COMPARISON_P (cond0)
7819 && COMPARISON_P (cond1)
7820 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
7821 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
7822 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
7823 || ((swap_condition (GET_CODE (cond0))
7824 == reversed_comparison_code (cond1, NULL))
7825 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
7826 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
7827 && ! side_effects_p (x))
7829 *ptrue = *pfalse = const0_rtx;
7830 return cond0;
7835 else if (code == IF_THEN_ELSE)
7837 /* If we have IF_THEN_ELSE already, extract the condition and
7838 canonicalize it if it is NE or EQ. */
7839 cond0 = XEXP (x, 0);
7840 *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
7841 if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
7842 return XEXP (cond0, 0);
7843 else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
7845 *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
7846 return XEXP (cond0, 0);
7848 else
7849 return cond0;
7852 /* If X is a SUBREG, we can narrow both the true and false values
7853 if the inner expression, if there is a condition. */
7854 else if (code == SUBREG
7855 && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
7856 &true0, &false0)))
7858 true0 = simplify_gen_subreg (mode, true0,
7859 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
7860 false0 = simplify_gen_subreg (mode, false0,
7861 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
7862 if (true0 && false0)
7864 *ptrue = true0;
7865 *pfalse = false0;
7866 return cond0;
7870 /* If X is a constant, this isn't special and will cause confusions
7871 if we treat it as such. Likewise if it is equivalent to a constant. */
7872 else if (CONSTANT_P (x)
7873 || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
7876 /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
7877 will be least confusing to the rest of the compiler. */
7878 else if (mode == BImode)
7880 *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
7881 return x;
7884 /* If X is known to be either 0 or -1, those are the true and
7885 false values when testing X. */
7886 else if (x == constm1_rtx || x == const0_rtx
7887 || (mode != VOIDmode
7888 && num_sign_bit_copies (x, mode) == GET_MODE_BITSIZE (mode)))
7890 *ptrue = constm1_rtx, *pfalse = const0_rtx;
7891 return x;
7894 /* Likewise for 0 or a single bit. */
7895 else if (SCALAR_INT_MODE_P (mode)
7896 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
7897 && exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
7899 *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
7900 return x;
7903 /* Otherwise fail; show no condition with true and false values the same. */
7904 *ptrue = *pfalse = x;
7905 return 0;
7908 /* Return the value of expression X given the fact that condition COND
7909 is known to be true when applied to REG as its first operand and VAL
7910 as its second. X is known to not be shared and so can be modified in
7911 place.
7913 We only handle the simplest cases, and specifically those cases that
7914 arise with IF_THEN_ELSE expressions. */
7916 static rtx
7917 known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
7919 enum rtx_code code = GET_CODE (x);
7920 rtx temp;
7921 const char *fmt;
7922 int i, j;
7924 if (side_effects_p (x))
7925 return x;
7927 /* If either operand of the condition is a floating point value,
7928 then we have to avoid collapsing an EQ comparison. */
7929 if (cond == EQ
7930 && rtx_equal_p (x, reg)
7931 && ! FLOAT_MODE_P (GET_MODE (x))
7932 && ! FLOAT_MODE_P (GET_MODE (val)))
7933 return val;
7935 if (cond == UNEQ && rtx_equal_p (x, reg))
7936 return val;
7938 /* If X is (abs REG) and we know something about REG's relationship
7939 with zero, we may be able to simplify this. */
7941 if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
7942 switch (cond)
7944 case GE: case GT: case EQ:
7945 return XEXP (x, 0);
7946 case LT: case LE:
7947 return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
7948 XEXP (x, 0),
7949 GET_MODE (XEXP (x, 0)));
7950 default:
7951 break;
7954 /* The only other cases we handle are MIN, MAX, and comparisons if the
7955 operands are the same as REG and VAL. */
7957 else if (COMPARISON_P (x) || COMMUTATIVE_ARITH_P (x))
7959 if (rtx_equal_p (XEXP (x, 0), val))
7960 cond = swap_condition (cond), temp = val, val = reg, reg = temp;
7962 if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
7964 if (COMPARISON_P (x))
7966 if (comparison_dominates_p (cond, code))
7967 return const_true_rtx;
7969 code = reversed_comparison_code (x, NULL);
7970 if (code != UNKNOWN
7971 && comparison_dominates_p (cond, code))
7972 return const0_rtx;
7973 else
7974 return x;
7976 else if (code == SMAX || code == SMIN
7977 || code == UMIN || code == UMAX)
7979 int unsignedp = (code == UMIN || code == UMAX);
7981 /* Do not reverse the condition when it is NE or EQ.
7982 This is because we cannot conclude anything about
7983 the value of 'SMAX (x, y)' when x is not equal to y,
7984 but we can when x equals y. */
7985 if ((code == SMAX || code == UMAX)
7986 && ! (cond == EQ || cond == NE))
7987 cond = reverse_condition (cond);
7989 switch (cond)
7991 case GE: case GT:
7992 return unsignedp ? x : XEXP (x, 1);
7993 case LE: case LT:
7994 return unsignedp ? x : XEXP (x, 0);
7995 case GEU: case GTU:
7996 return unsignedp ? XEXP (x, 1) : x;
7997 case LEU: case LTU:
7998 return unsignedp ? XEXP (x, 0) : x;
7999 default:
8000 break;
8005 else if (code == SUBREG)
8007 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
8008 rtx new, r = known_cond (SUBREG_REG (x), cond, reg, val);
8010 if (SUBREG_REG (x) != r)
8012 /* We must simplify subreg here, before we lose track of the
8013 original inner_mode. */
8014 new = simplify_subreg (GET_MODE (x), r,
8015 inner_mode, SUBREG_BYTE (x));
8016 if (new)
8017 return new;
8018 else
8019 SUBST (SUBREG_REG (x), r);
8022 return x;
8024 /* We don't have to handle SIGN_EXTEND here, because even in the
8025 case of replacing something with a modeless CONST_INT, a
8026 CONST_INT is already (supposed to be) a valid sign extension for
8027 its narrower mode, which implies it's already properly
8028 sign-extended for the wider mode. Now, for ZERO_EXTEND, the
8029 story is different. */
8030 else if (code == ZERO_EXTEND)
8032 enum machine_mode inner_mode = GET_MODE (XEXP (x, 0));
8033 rtx new, r = known_cond (XEXP (x, 0), cond, reg, val);
8035 if (XEXP (x, 0) != r)
8037 /* We must simplify the zero_extend here, before we lose
8038 track of the original inner_mode. */
8039 new = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
8040 r, inner_mode);
8041 if (new)
8042 return new;
8043 else
8044 SUBST (XEXP (x, 0), r);
8047 return x;
8050 fmt = GET_RTX_FORMAT (code);
8051 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8053 if (fmt[i] == 'e')
8054 SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
8055 else if (fmt[i] == 'E')
8056 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8057 SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
8058 cond, reg, val));
8061 return x;
8064 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
8065 assignment as a field assignment. */
8067 static int
8068 rtx_equal_for_field_assignment_p (rtx x, rtx y)
8070 if (x == y || rtx_equal_p (x, y))
8071 return 1;
8073 if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
8074 return 0;
8076 /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
8077 Note that all SUBREGs of MEM are paradoxical; otherwise they
8078 would have been rewritten. */
8079 if (MEM_P (x) && GET_CODE (y) == SUBREG
8080 && MEM_P (SUBREG_REG (y))
8081 && rtx_equal_p (SUBREG_REG (y),
8082 gen_lowpart (GET_MODE (SUBREG_REG (y)), x)))
8083 return 1;
8085 if (MEM_P (y) && GET_CODE (x) == SUBREG
8086 && MEM_P (SUBREG_REG (x))
8087 && rtx_equal_p (SUBREG_REG (x),
8088 gen_lowpart (GET_MODE (SUBREG_REG (x)), y)))
8089 return 1;
8091 /* We used to see if get_last_value of X and Y were the same but that's
8092 not correct. In one direction, we'll cause the assignment to have
8093 the wrong destination and in the case, we'll import a register into this
8094 insn that might have already have been dead. So fail if none of the
8095 above cases are true. */
8096 return 0;
8099 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
8100 Return that assignment if so.
8102 We only handle the most common cases. */
8104 static rtx
8105 make_field_assignment (rtx x)
8107 rtx dest = SET_DEST (x);
8108 rtx src = SET_SRC (x);
8109 rtx assign;
8110 rtx rhs, lhs;
8111 HOST_WIDE_INT c1;
8112 HOST_WIDE_INT pos;
8113 unsigned HOST_WIDE_INT len;
8114 rtx other;
8115 enum machine_mode mode;
8117 /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
8118 a clear of a one-bit field. We will have changed it to
8119 (and (rotate (const_int -2) POS) DEST), so check for that. Also check
8120 for a SUBREG. */
8122 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
8123 && GET_CODE (XEXP (XEXP (src, 0), 0)) == CONST_INT
8124 && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
8125 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
8127 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
8128 1, 1, 1, 0);
8129 if (assign != 0)
8130 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
8131 return x;
8134 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
8135 && subreg_lowpart_p (XEXP (src, 0))
8136 && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
8137 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
8138 && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
8139 && GET_CODE (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == CONST_INT
8140 && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
8141 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
8143 assign = make_extraction (VOIDmode, dest, 0,
8144 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
8145 1, 1, 1, 0);
8146 if (assign != 0)
8147 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
8148 return x;
8151 /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
8152 one-bit field. */
8153 if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
8154 && XEXP (XEXP (src, 0), 0) == const1_rtx
8155 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
8157 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
8158 1, 1, 1, 0);
8159 if (assign != 0)
8160 return gen_rtx_SET (VOIDmode, assign, const1_rtx);
8161 return x;
8164 /* If DEST is already a field assignment, i.e. ZERO_EXTRACT, and the
8165 SRC is an AND with all bits of that field set, then we can discard
8166 the AND. */
8167 if (GET_CODE (dest) == ZERO_EXTRACT
8168 && GET_CODE (XEXP (dest, 1)) == CONST_INT
8169 && GET_CODE (src) == AND
8170 && GET_CODE (XEXP (src, 1)) == CONST_INT)
8172 HOST_WIDE_INT width = INTVAL (XEXP (dest, 1));
8173 unsigned HOST_WIDE_INT and_mask = INTVAL (XEXP (src, 1));
8174 unsigned HOST_WIDE_INT ze_mask;
8176 if (width >= HOST_BITS_PER_WIDE_INT)
8177 ze_mask = -1;
8178 else
8179 ze_mask = ((unsigned HOST_WIDE_INT)1 << width) - 1;
8181 /* Complete overlap. We can remove the source AND. */
8182 if ((and_mask & ze_mask) == ze_mask)
8183 return gen_rtx_SET (VOIDmode, dest, XEXP (src, 0));
8185 /* Partial overlap. We can reduce the source AND. */
8186 if ((and_mask & ze_mask) != and_mask)
8188 mode = GET_MODE (src);
8189 src = gen_rtx_AND (mode, XEXP (src, 0),
8190 gen_int_mode (and_mask & ze_mask, mode));
8191 return gen_rtx_SET (VOIDmode, dest, src);
8195 /* The other case we handle is assignments into a constant-position
8196 field. They look like (ior/xor (and DEST C1) OTHER). If C1 represents
8197 a mask that has all one bits except for a group of zero bits and
8198 OTHER is known to have zeros where C1 has ones, this is such an
8199 assignment. Compute the position and length from C1. Shift OTHER
8200 to the appropriate position, force it to the required mode, and
8201 make the extraction. Check for the AND in both operands. */
8203 if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
8204 return x;
8206 rhs = expand_compound_operation (XEXP (src, 0));
8207 lhs = expand_compound_operation (XEXP (src, 1));
8209 if (GET_CODE (rhs) == AND
8210 && GET_CODE (XEXP (rhs, 1)) == CONST_INT
8211 && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
8212 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
8213 else if (GET_CODE (lhs) == AND
8214 && GET_CODE (XEXP (lhs, 1)) == CONST_INT
8215 && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
8216 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
8217 else
8218 return x;
8220 pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
8221 if (pos < 0 || pos + len > GET_MODE_BITSIZE (GET_MODE (dest))
8222 || GET_MODE_BITSIZE (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
8223 || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
8224 return x;
8226 assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
8227 if (assign == 0)
8228 return x;
8230 /* The mode to use for the source is the mode of the assignment, or of
8231 what is inside a possible STRICT_LOW_PART. */
8232 mode = (GET_CODE (assign) == STRICT_LOW_PART
8233 ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
8235 /* Shift OTHER right POS places and make it the source, restricting it
8236 to the proper length and mode. */
8238 src = canon_reg_for_combine (simplify_shift_const (NULL_RTX, LSHIFTRT,
8239 GET_MODE (src),
8240 other, pos),
8241 dest);
8242 src = force_to_mode (src, mode,
8243 GET_MODE_BITSIZE (mode) >= HOST_BITS_PER_WIDE_INT
8244 ? ~(unsigned HOST_WIDE_INT) 0
8245 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
8248 /* If SRC is masked by an AND that does not make a difference in
8249 the value being stored, strip it. */
8250 if (GET_CODE (assign) == ZERO_EXTRACT
8251 && GET_CODE (XEXP (assign, 1)) == CONST_INT
8252 && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
8253 && GET_CODE (src) == AND
8254 && GET_CODE (XEXP (src, 1)) == CONST_INT
8255 && ((unsigned HOST_WIDE_INT) INTVAL (XEXP (src, 1))
8256 == ((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (assign, 1))) - 1))
8257 src = XEXP (src, 0);
8259 return gen_rtx_SET (VOIDmode, assign, src);
8262 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
8263 if so. */
8265 static rtx
8266 apply_distributive_law (rtx x)
8268 enum rtx_code code = GET_CODE (x);
8269 enum rtx_code inner_code;
8270 rtx lhs, rhs, other;
8271 rtx tem;
8273 /* Distributivity is not true for floating point as it can change the
8274 value. So we don't do it unless -funsafe-math-optimizations. */
8275 if (FLOAT_MODE_P (GET_MODE (x))
8276 && ! flag_unsafe_math_optimizations)
8277 return x;
8279 /* The outer operation can only be one of the following: */
8280 if (code != IOR && code != AND && code != XOR
8281 && code != PLUS && code != MINUS)
8282 return x;
8284 lhs = XEXP (x, 0);
8285 rhs = XEXP (x, 1);
8287 /* If either operand is a primitive we can't do anything, so get out
8288 fast. */
8289 if (OBJECT_P (lhs) || OBJECT_P (rhs))
8290 return x;
8292 lhs = expand_compound_operation (lhs);
8293 rhs = expand_compound_operation (rhs);
8294 inner_code = GET_CODE (lhs);
8295 if (inner_code != GET_CODE (rhs))
8296 return x;
8298 /* See if the inner and outer operations distribute. */
8299 switch (inner_code)
8301 case LSHIFTRT:
8302 case ASHIFTRT:
8303 case AND:
8304 case IOR:
8305 /* These all distribute except over PLUS. */
8306 if (code == PLUS || code == MINUS)
8307 return x;
8308 break;
8310 case MULT:
8311 if (code != PLUS && code != MINUS)
8312 return x;
8313 break;
8315 case ASHIFT:
8316 /* This is also a multiply, so it distributes over everything. */
8317 break;
8319 case SUBREG:
8320 /* Non-paradoxical SUBREGs distributes over all operations,
8321 provided the inner modes and byte offsets are the same, this
8322 is an extraction of a low-order part, we don't convert an fp
8323 operation to int or vice versa, this is not a vector mode,
8324 and we would not be converting a single-word operation into a
8325 multi-word operation. The latter test is not required, but
8326 it prevents generating unneeded multi-word operations. Some
8327 of the previous tests are redundant given the latter test,
8328 but are retained because they are required for correctness.
8330 We produce the result slightly differently in this case. */
8332 if (GET_MODE (SUBREG_REG (lhs)) != GET_MODE (SUBREG_REG (rhs))
8333 || SUBREG_BYTE (lhs) != SUBREG_BYTE (rhs)
8334 || ! subreg_lowpart_p (lhs)
8335 || (GET_MODE_CLASS (GET_MODE (lhs))
8336 != GET_MODE_CLASS (GET_MODE (SUBREG_REG (lhs))))
8337 || (GET_MODE_SIZE (GET_MODE (lhs))
8338 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))))
8339 || VECTOR_MODE_P (GET_MODE (lhs))
8340 || GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))) > UNITS_PER_WORD
8341 /* Result might need to be truncated. Don't change mode if
8342 explicit truncation is needed. */
8343 || !TRULY_NOOP_TRUNCATION
8344 (GET_MODE_BITSIZE (GET_MODE (x)),
8345 GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (lhs)))))
8346 return x;
8348 tem = simplify_gen_binary (code, GET_MODE (SUBREG_REG (lhs)),
8349 SUBREG_REG (lhs), SUBREG_REG (rhs));
8350 return gen_lowpart (GET_MODE (x), tem);
8352 default:
8353 return x;
8356 /* Set LHS and RHS to the inner operands (A and B in the example
8357 above) and set OTHER to the common operand (C in the example).
8358 There is only one way to do this unless the inner operation is
8359 commutative. */
8360 if (COMMUTATIVE_ARITH_P (lhs)
8361 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
8362 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
8363 else if (COMMUTATIVE_ARITH_P (lhs)
8364 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
8365 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
8366 else if (COMMUTATIVE_ARITH_P (lhs)
8367 && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
8368 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
8369 else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
8370 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
8371 else
8372 return x;
8374 /* Form the new inner operation, seeing if it simplifies first. */
8375 tem = simplify_gen_binary (code, GET_MODE (x), lhs, rhs);
8377 /* There is one exception to the general way of distributing:
8378 (a | c) ^ (b | c) -> (a ^ b) & ~c */
8379 if (code == XOR && inner_code == IOR)
8381 inner_code = AND;
8382 other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
8385 /* We may be able to continuing distributing the result, so call
8386 ourselves recursively on the inner operation before forming the
8387 outer operation, which we return. */
8388 return simplify_gen_binary (inner_code, GET_MODE (x),
8389 apply_distributive_law (tem), other);
8392 /* See if X is of the form (* (+ A B) C), and if so convert to
8393 (+ (* A C) (* B C)) and try to simplify.
8395 Most of the time, this results in no change. However, if some of
8396 the operands are the same or inverses of each other, simplifications
8397 will result.
8399 For example, (and (ior A B) (not B)) can occur as the result of
8400 expanding a bit field assignment. When we apply the distributive
8401 law to this, we get (ior (and (A (not B))) (and (B (not B)))),
8402 which then simplifies to (and (A (not B))).
8404 Note that no checks happen on the validity of applying the inverse
8405 distributive law. This is pointless since we can do it in the
8406 few places where this routine is called.
8408 N is the index of the term that is decomposed (the arithmetic operation,
8409 i.e. (+ A B) in the first example above). !N is the index of the term that
8410 is distributed, i.e. of C in the first example above. */
8411 static rtx
8412 distribute_and_simplify_rtx (rtx x, int n)
8414 enum machine_mode mode;
8415 enum rtx_code outer_code, inner_code;
8416 rtx decomposed, distributed, inner_op0, inner_op1, new_op0, new_op1, tmp;
8418 decomposed = XEXP (x, n);
8419 if (!ARITHMETIC_P (decomposed))
8420 return NULL_RTX;
8422 mode = GET_MODE (x);
8423 outer_code = GET_CODE (x);
8424 distributed = XEXP (x, !n);
8426 inner_code = GET_CODE (decomposed);
8427 inner_op0 = XEXP (decomposed, 0);
8428 inner_op1 = XEXP (decomposed, 1);
8430 /* Special case (and (xor B C) (not A)), which is equivalent to
8431 (xor (ior A B) (ior A C)) */
8432 if (outer_code == AND && inner_code == XOR && GET_CODE (distributed) == NOT)
8434 distributed = XEXP (distributed, 0);
8435 outer_code = IOR;
8438 if (n == 0)
8440 /* Distribute the second term. */
8441 new_op0 = simplify_gen_binary (outer_code, mode, inner_op0, distributed);
8442 new_op1 = simplify_gen_binary (outer_code, mode, inner_op1, distributed);
8444 else
8446 /* Distribute the first term. */
8447 new_op0 = simplify_gen_binary (outer_code, mode, distributed, inner_op0);
8448 new_op1 = simplify_gen_binary (outer_code, mode, distributed, inner_op1);
8451 tmp = apply_distributive_law (simplify_gen_binary (inner_code, mode,
8452 new_op0, new_op1));
8453 if (GET_CODE (tmp) != outer_code
8454 && rtx_cost (tmp, SET) < rtx_cost (x, SET))
8455 return tmp;
8457 return NULL_RTX;
8460 /* Simplify a logical `and' of VAROP with the constant CONSTOP, to be done
8461 in MODE. Return an equivalent form, if different from (and VAROP
8462 (const_int CONSTOP)). Otherwise, return NULL_RTX. */
8464 static rtx
8465 simplify_and_const_int_1 (enum machine_mode mode, rtx varop,
8466 unsigned HOST_WIDE_INT constop)
8468 unsigned HOST_WIDE_INT nonzero;
8469 unsigned HOST_WIDE_INT orig_constop;
8470 rtx orig_varop;
8471 int i;
8473 orig_varop = varop;
8474 orig_constop = constop;
8475 if (GET_CODE (varop) == CLOBBER)
8476 return NULL_RTX;
8478 /* Simplify VAROP knowing that we will be only looking at some of the
8479 bits in it.
8481 Note by passing in CONSTOP, we guarantee that the bits not set in
8482 CONSTOP are not significant and will never be examined. We must
8483 ensure that is the case by explicitly masking out those bits
8484 before returning. */
8485 varop = force_to_mode (varop, mode, constop, 0);
8487 /* If VAROP is a CLOBBER, we will fail so return it. */
8488 if (GET_CODE (varop) == CLOBBER)
8489 return varop;
8491 /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
8492 to VAROP and return the new constant. */
8493 if (GET_CODE (varop) == CONST_INT)
8494 return gen_int_mode (INTVAL (varop) & constop, mode);
8496 /* See what bits may be nonzero in VAROP. Unlike the general case of
8497 a call to nonzero_bits, here we don't care about bits outside
8498 MODE. */
8500 nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
8502 /* Turn off all bits in the constant that are known to already be zero.
8503 Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
8504 which is tested below. */
8506 constop &= nonzero;
8508 /* If we don't have any bits left, return zero. */
8509 if (constop == 0)
8510 return const0_rtx;
8512 /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
8513 a power of two, we can replace this with an ASHIFT. */
8514 if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
8515 && (i = exact_log2 (constop)) >= 0)
8516 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
8518 /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
8519 or XOR, then try to apply the distributive law. This may eliminate
8520 operations if either branch can be simplified because of the AND.
8521 It may also make some cases more complex, but those cases probably
8522 won't match a pattern either with or without this. */
8524 if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
8525 return
8526 gen_lowpart
8527 (mode,
8528 apply_distributive_law
8529 (simplify_gen_binary (GET_CODE (varop), GET_MODE (varop),
8530 simplify_and_const_int (NULL_RTX,
8531 GET_MODE (varop),
8532 XEXP (varop, 0),
8533 constop),
8534 simplify_and_const_int (NULL_RTX,
8535 GET_MODE (varop),
8536 XEXP (varop, 1),
8537 constop))));
8539 /* If VAROP is PLUS, and the constant is a mask of low bits, distribute
8540 the AND and see if one of the operands simplifies to zero. If so, we
8541 may eliminate it. */
8543 if (GET_CODE (varop) == PLUS
8544 && exact_log2 (constop + 1) >= 0)
8546 rtx o0, o1;
8548 o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
8549 o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
8550 if (o0 == const0_rtx)
8551 return o1;
8552 if (o1 == const0_rtx)
8553 return o0;
8556 /* Make a SUBREG if necessary. If we can't make it, fail. */
8557 varop = gen_lowpart (mode, varop);
8558 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
8559 return NULL_RTX;
8561 /* If we are only masking insignificant bits, return VAROP. */
8562 if (constop == nonzero)
8563 return varop;
8565 if (varop == orig_varop && constop == orig_constop)
8566 return NULL_RTX;
8568 /* Otherwise, return an AND. */
8569 return simplify_gen_binary (AND, mode, varop, gen_int_mode (constop, mode));
8573 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
8574 in MODE.
8576 Return an equivalent form, if different from X. Otherwise, return X. If
8577 X is zero, we are to always construct the equivalent form. */
8579 static rtx
8580 simplify_and_const_int (rtx x, enum machine_mode mode, rtx varop,
8581 unsigned HOST_WIDE_INT constop)
8583 rtx tem = simplify_and_const_int_1 (mode, varop, constop);
8584 if (tem)
8585 return tem;
8587 if (!x)
8588 x = simplify_gen_binary (AND, GET_MODE (varop), varop,
8589 gen_int_mode (constop, mode));
8590 if (GET_MODE (x) != mode)
8591 x = gen_lowpart (mode, x);
8592 return x;
8595 /* Given a REG, X, compute which bits in X can be nonzero.
8596 We don't care about bits outside of those defined in MODE.
8598 For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
8599 a shift, AND, or zero_extract, we can do better. */
8601 static rtx
8602 reg_nonzero_bits_for_combine (rtx x, enum machine_mode mode,
8603 rtx known_x ATTRIBUTE_UNUSED,
8604 enum machine_mode known_mode ATTRIBUTE_UNUSED,
8605 unsigned HOST_WIDE_INT known_ret ATTRIBUTE_UNUSED,
8606 unsigned HOST_WIDE_INT *nonzero)
8608 rtx tem;
8610 /* If X is a register whose nonzero bits value is current, use it.
8611 Otherwise, if X is a register whose value we can find, use that
8612 value. Otherwise, use the previously-computed global nonzero bits
8613 for this register. */
8615 if (reg_stat[REGNO (x)].last_set_value != 0
8616 && (reg_stat[REGNO (x)].last_set_mode == mode
8617 || (GET_MODE_CLASS (reg_stat[REGNO (x)].last_set_mode) == MODE_INT
8618 && GET_MODE_CLASS (mode) == MODE_INT))
8619 && ((reg_stat[REGNO (x)].last_set_label >= label_tick_ebb_start
8620 && reg_stat[REGNO (x)].last_set_label < label_tick)
8621 || (reg_stat[REGNO (x)].last_set_label == label_tick
8622 && DF_INSN_LUID (reg_stat[REGNO (x)].last_set) < subst_low_luid)
8623 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
8624 && REG_N_SETS (REGNO (x)) == 1
8625 && !REGNO_REG_SET_P
8626 (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), REGNO (x)))))
8628 *nonzero &= reg_stat[REGNO (x)].last_set_nonzero_bits;
8629 return NULL;
8632 tem = get_last_value (x);
8634 if (tem)
8636 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
8637 /* If X is narrower than MODE and TEM is a non-negative
8638 constant that would appear negative in the mode of X,
8639 sign-extend it for use in reg_nonzero_bits because some
8640 machines (maybe most) will actually do the sign-extension
8641 and this is the conservative approach.
8643 ??? For 2.5, try to tighten up the MD files in this regard
8644 instead of this kludge. */
8646 if (GET_MODE_BITSIZE (GET_MODE (x)) < GET_MODE_BITSIZE (mode)
8647 && GET_CODE (tem) == CONST_INT
8648 && INTVAL (tem) > 0
8649 && 0 != (INTVAL (tem)
8650 & ((HOST_WIDE_INT) 1
8651 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
8652 tem = GEN_INT (INTVAL (tem)
8653 | ((HOST_WIDE_INT) (-1)
8654 << GET_MODE_BITSIZE (GET_MODE (x))));
8655 #endif
8656 return tem;
8658 else if (nonzero_sign_valid && reg_stat[REGNO (x)].nonzero_bits)
8660 unsigned HOST_WIDE_INT mask = reg_stat[REGNO (x)].nonzero_bits;
8662 if (GET_MODE_BITSIZE (GET_MODE (x)) < GET_MODE_BITSIZE (mode))
8663 /* We don't know anything about the upper bits. */
8664 mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (GET_MODE (x));
8665 *nonzero &= mask;
8668 return NULL;
8671 /* Return the number of bits at the high-order end of X that are known to
8672 be equal to the sign bit. X will be used in mode MODE; if MODE is
8673 VOIDmode, X will be used in its own mode. The returned value will always
8674 be between 1 and the number of bits in MODE. */
8676 static rtx
8677 reg_num_sign_bit_copies_for_combine (rtx x, enum machine_mode mode,
8678 rtx known_x ATTRIBUTE_UNUSED,
8679 enum machine_mode known_mode
8680 ATTRIBUTE_UNUSED,
8681 unsigned int known_ret ATTRIBUTE_UNUSED,
8682 unsigned int *result)
8684 rtx tem;
8686 if (reg_stat[REGNO (x)].last_set_value != 0
8687 && reg_stat[REGNO (x)].last_set_mode == mode
8688 && ((reg_stat[REGNO (x)].last_set_label >= label_tick_ebb_start
8689 && reg_stat[REGNO (x)].last_set_label < label_tick)
8690 || (reg_stat[REGNO (x)].last_set_label == label_tick
8691 && DF_INSN_LUID (reg_stat[REGNO (x)].last_set) < subst_low_luid)
8692 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
8693 && REG_N_SETS (REGNO (x)) == 1
8694 && !REGNO_REG_SET_P
8695 (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), REGNO (x)))))
8697 *result = reg_stat[REGNO (x)].last_set_sign_bit_copies;
8698 return NULL;
8701 tem = get_last_value (x);
8702 if (tem != 0)
8703 return tem;
8705 if (nonzero_sign_valid && reg_stat[REGNO (x)].sign_bit_copies != 0
8706 && GET_MODE_BITSIZE (GET_MODE (x)) == GET_MODE_BITSIZE (mode))
8707 *result = reg_stat[REGNO (x)].sign_bit_copies;
8709 return NULL;
8712 /* Return the number of "extended" bits there are in X, when interpreted
8713 as a quantity in MODE whose signedness is indicated by UNSIGNEDP. For
8714 unsigned quantities, this is the number of high-order zero bits.
8715 For signed quantities, this is the number of copies of the sign bit
8716 minus 1. In both case, this function returns the number of "spare"
8717 bits. For example, if two quantities for which this function returns
8718 at least 1 are added, the addition is known not to overflow.
8720 This function will always return 0 unless called during combine, which
8721 implies that it must be called from a define_split. */
8723 unsigned int
8724 extended_count (rtx x, enum machine_mode mode, int unsignedp)
8726 if (nonzero_sign_valid == 0)
8727 return 0;
8729 return (unsignedp
8730 ? (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
8731 ? (unsigned int) (GET_MODE_BITSIZE (mode) - 1
8732 - floor_log2 (nonzero_bits (x, mode)))
8733 : 0)
8734 : num_sign_bit_copies (x, mode) - 1);
8737 /* This function is called from `simplify_shift_const' to merge two
8738 outer operations. Specifically, we have already found that we need
8739 to perform operation *POP0 with constant *PCONST0 at the outermost
8740 position. We would now like to also perform OP1 with constant CONST1
8741 (with *POP0 being done last).
8743 Return 1 if we can do the operation and update *POP0 and *PCONST0 with
8744 the resulting operation. *PCOMP_P is set to 1 if we would need to
8745 complement the innermost operand, otherwise it is unchanged.
8747 MODE is the mode in which the operation will be done. No bits outside
8748 the width of this mode matter. It is assumed that the width of this mode
8749 is smaller than or equal to HOST_BITS_PER_WIDE_INT.
8751 If *POP0 or OP1 are UNKNOWN, it means no operation is required. Only NEG, PLUS,
8752 IOR, XOR, and AND are supported. We may set *POP0 to SET if the proper
8753 result is simply *PCONST0.
8755 If the resulting operation cannot be expressed as one operation, we
8756 return 0 and do not change *POP0, *PCONST0, and *PCOMP_P. */
8758 static int
8759 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)
8761 enum rtx_code op0 = *pop0;
8762 HOST_WIDE_INT const0 = *pconst0;
8764 const0 &= GET_MODE_MASK (mode);
8765 const1 &= GET_MODE_MASK (mode);
8767 /* If OP0 is an AND, clear unimportant bits in CONST1. */
8768 if (op0 == AND)
8769 const1 &= const0;
8771 /* If OP0 or OP1 is UNKNOWN, this is easy. Similarly if they are the same or
8772 if OP0 is SET. */
8774 if (op1 == UNKNOWN || op0 == SET)
8775 return 1;
8777 else if (op0 == UNKNOWN)
8778 op0 = op1, const0 = const1;
8780 else if (op0 == op1)
8782 switch (op0)
8784 case AND:
8785 const0 &= const1;
8786 break;
8787 case IOR:
8788 const0 |= const1;
8789 break;
8790 case XOR:
8791 const0 ^= const1;
8792 break;
8793 case PLUS:
8794 const0 += const1;
8795 break;
8796 case NEG:
8797 op0 = UNKNOWN;
8798 break;
8799 default:
8800 break;
8804 /* Otherwise, if either is a PLUS or NEG, we can't do anything. */
8805 else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
8806 return 0;
8808 /* If the two constants aren't the same, we can't do anything. The
8809 remaining six cases can all be done. */
8810 else if (const0 != const1)
8811 return 0;
8813 else
8814 switch (op0)
8816 case IOR:
8817 if (op1 == AND)
8818 /* (a & b) | b == b */
8819 op0 = SET;
8820 else /* op1 == XOR */
8821 /* (a ^ b) | b == a | b */
8823 break;
8825 case XOR:
8826 if (op1 == AND)
8827 /* (a & b) ^ b == (~a) & b */
8828 op0 = AND, *pcomp_p = 1;
8829 else /* op1 == IOR */
8830 /* (a | b) ^ b == a & ~b */
8831 op0 = AND, const0 = ~const0;
8832 break;
8834 case AND:
8835 if (op1 == IOR)
8836 /* (a | b) & b == b */
8837 op0 = SET;
8838 else /* op1 == XOR */
8839 /* (a ^ b) & b) == (~a) & b */
8840 *pcomp_p = 1;
8841 break;
8842 default:
8843 break;
8846 /* Check for NO-OP cases. */
8847 const0 &= GET_MODE_MASK (mode);
8848 if (const0 == 0
8849 && (op0 == IOR || op0 == XOR || op0 == PLUS))
8850 op0 = UNKNOWN;
8851 else if (const0 == 0 && op0 == AND)
8852 op0 = SET;
8853 else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
8854 && op0 == AND)
8855 op0 = UNKNOWN;
8857 /* ??? Slightly redundant with the above mask, but not entirely.
8858 Moving this above means we'd have to sign-extend the mode mask
8859 for the final test. */
8860 const0 = trunc_int_for_mode (const0, mode);
8862 *pop0 = op0;
8863 *pconst0 = const0;
8865 return 1;
8868 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
8869 The result of the shift is RESULT_MODE. Return NULL_RTX if we cannot
8870 simplify it. Otherwise, return a simplified value.
8872 The shift is normally computed in the widest mode we find in VAROP, as
8873 long as it isn't a different number of words than RESULT_MODE. Exceptions
8874 are ASHIFTRT and ROTATE, which are always done in their original mode. */
8876 static rtx
8877 simplify_shift_const_1 (enum rtx_code code, enum machine_mode result_mode,
8878 rtx varop, int orig_count)
8880 enum rtx_code orig_code = code;
8881 rtx orig_varop = varop;
8882 int count;
8883 enum machine_mode mode = result_mode;
8884 enum machine_mode shift_mode, tmode;
8885 unsigned int mode_words
8886 = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
8887 /* We form (outer_op (code varop count) (outer_const)). */
8888 enum rtx_code outer_op = UNKNOWN;
8889 HOST_WIDE_INT outer_const = 0;
8890 int complement_p = 0;
8891 rtx new, x;
8893 /* Make sure and truncate the "natural" shift on the way in. We don't
8894 want to do this inside the loop as it makes it more difficult to
8895 combine shifts. */
8896 if (SHIFT_COUNT_TRUNCATED)
8897 orig_count &= GET_MODE_BITSIZE (mode) - 1;
8899 /* If we were given an invalid count, don't do anything except exactly
8900 what was requested. */
8902 if (orig_count < 0 || orig_count >= (int) GET_MODE_BITSIZE (mode))
8903 return NULL_RTX;
8905 count = orig_count;
8907 /* Unless one of the branches of the `if' in this loop does a `continue',
8908 we will `break' the loop after the `if'. */
8910 while (count != 0)
8912 /* If we have an operand of (clobber (const_int 0)), fail. */
8913 if (GET_CODE (varop) == CLOBBER)
8914 return NULL_RTX;
8916 /* If we discovered we had to complement VAROP, leave. Making a NOT
8917 here would cause an infinite loop. */
8918 if (complement_p)
8919 break;
8921 /* Convert ROTATERT to ROTATE. */
8922 if (code == ROTATERT)
8924 unsigned int bitsize = GET_MODE_BITSIZE (result_mode);;
8925 code = ROTATE;
8926 if (VECTOR_MODE_P (result_mode))
8927 count = bitsize / GET_MODE_NUNITS (result_mode) - count;
8928 else
8929 count = bitsize - count;
8932 /* We need to determine what mode we will do the shift in. If the
8933 shift is a right shift or a ROTATE, we must always do it in the mode
8934 it was originally done in. Otherwise, we can do it in MODE, the
8935 widest mode encountered. */
8936 shift_mode
8937 = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
8938 ? result_mode : mode);
8940 /* Handle cases where the count is greater than the size of the mode
8941 minus 1. For ASHIFT, use the size minus one as the count (this can
8942 occur when simplifying (lshiftrt (ashiftrt ..))). For rotates,
8943 take the count modulo the size. For other shifts, the result is
8944 zero.
8946 Since these shifts are being produced by the compiler by combining
8947 multiple operations, each of which are defined, we know what the
8948 result is supposed to be. */
8950 if (count > (GET_MODE_BITSIZE (shift_mode) - 1))
8952 if (code == ASHIFTRT)
8953 count = GET_MODE_BITSIZE (shift_mode) - 1;
8954 else if (code == ROTATE || code == ROTATERT)
8955 count %= GET_MODE_BITSIZE (shift_mode);
8956 else
8958 /* We can't simply return zero because there may be an
8959 outer op. */
8960 varop = const0_rtx;
8961 count = 0;
8962 break;
8966 /* An arithmetic right shift of a quantity known to be -1 or 0
8967 is a no-op. */
8968 if (code == ASHIFTRT
8969 && (num_sign_bit_copies (varop, shift_mode)
8970 == GET_MODE_BITSIZE (shift_mode)))
8972 count = 0;
8973 break;
8976 /* If we are doing an arithmetic right shift and discarding all but
8977 the sign bit copies, this is equivalent to doing a shift by the
8978 bitsize minus one. Convert it into that shift because it will often
8979 allow other simplifications. */
8981 if (code == ASHIFTRT
8982 && (count + num_sign_bit_copies (varop, shift_mode)
8983 >= GET_MODE_BITSIZE (shift_mode)))
8984 count = GET_MODE_BITSIZE (shift_mode) - 1;
8986 /* We simplify the tests below and elsewhere by converting
8987 ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
8988 `make_compound_operation' will convert it to an ASHIFTRT for
8989 those machines (such as VAX) that don't have an LSHIFTRT. */
8990 if (GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
8991 && code == ASHIFTRT
8992 && ((nonzero_bits (varop, shift_mode)
8993 & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (shift_mode) - 1)))
8994 == 0))
8995 code = LSHIFTRT;
8997 if (((code == LSHIFTRT
8998 && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
8999 && !(nonzero_bits (varop, shift_mode) >> count))
9000 || (code == ASHIFT
9001 && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9002 && !((nonzero_bits (varop, shift_mode) << count)
9003 & GET_MODE_MASK (shift_mode))))
9004 && !side_effects_p (varop))
9005 varop = const0_rtx;
9007 switch (GET_CODE (varop))
9009 case SIGN_EXTEND:
9010 case ZERO_EXTEND:
9011 case SIGN_EXTRACT:
9012 case ZERO_EXTRACT:
9013 new = expand_compound_operation (varop);
9014 if (new != varop)
9016 varop = new;
9017 continue;
9019 break;
9021 case MEM:
9022 /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
9023 minus the width of a smaller mode, we can do this with a
9024 SIGN_EXTEND or ZERO_EXTEND from the narrower memory location. */
9025 if ((code == ASHIFTRT || code == LSHIFTRT)
9026 && ! mode_dependent_address_p (XEXP (varop, 0))
9027 && ! MEM_VOLATILE_P (varop)
9028 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
9029 MODE_INT, 1)) != BLKmode)
9031 new = adjust_address_nv (varop, tmode,
9032 BYTES_BIG_ENDIAN ? 0
9033 : count / BITS_PER_UNIT);
9035 varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
9036 : ZERO_EXTEND, mode, new);
9037 count = 0;
9038 continue;
9040 break;
9042 case SUBREG:
9043 /* If VAROP is a SUBREG, strip it as long as the inner operand has
9044 the same number of words as what we've seen so far. Then store
9045 the widest mode in MODE. */
9046 if (subreg_lowpart_p (varop)
9047 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
9048 > GET_MODE_SIZE (GET_MODE (varop)))
9049 && (unsigned int) ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
9050 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
9051 == mode_words)
9053 varop = SUBREG_REG (varop);
9054 if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
9055 mode = GET_MODE (varop);
9056 continue;
9058 break;
9060 case MULT:
9061 /* Some machines use MULT instead of ASHIFT because MULT
9062 is cheaper. But it is still better on those machines to
9063 merge two shifts into one. */
9064 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9065 && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
9067 varop
9068 = simplify_gen_binary (ASHIFT, GET_MODE (varop),
9069 XEXP (varop, 0),
9070 GEN_INT (exact_log2 (
9071 INTVAL (XEXP (varop, 1)))));
9072 continue;
9074 break;
9076 case UDIV:
9077 /* Similar, for when divides are cheaper. */
9078 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9079 && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
9081 varop
9082 = simplify_gen_binary (LSHIFTRT, GET_MODE (varop),
9083 XEXP (varop, 0),
9084 GEN_INT (exact_log2 (
9085 INTVAL (XEXP (varop, 1)))));
9086 continue;
9088 break;
9090 case ASHIFTRT:
9091 /* If we are extracting just the sign bit of an arithmetic
9092 right shift, that shift is not needed. However, the sign
9093 bit of a wider mode may be different from what would be
9094 interpreted as the sign bit in a narrower mode, so, if
9095 the result is narrower, don't discard the shift. */
9096 if (code == LSHIFTRT
9097 && count == (GET_MODE_BITSIZE (result_mode) - 1)
9098 && (GET_MODE_BITSIZE (result_mode)
9099 >= GET_MODE_BITSIZE (GET_MODE (varop))))
9101 varop = XEXP (varop, 0);
9102 continue;
9105 /* ... fall through ... */
9107 case LSHIFTRT:
9108 case ASHIFT:
9109 case ROTATE:
9110 /* Here we have two nested shifts. The result is usually the
9111 AND of a new shift with a mask. We compute the result below. */
9112 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9113 && INTVAL (XEXP (varop, 1)) >= 0
9114 && INTVAL (XEXP (varop, 1)) < GET_MODE_BITSIZE (GET_MODE (varop))
9115 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9116 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
9117 && !VECTOR_MODE_P (result_mode))
9119 enum rtx_code first_code = GET_CODE (varop);
9120 unsigned int first_count = INTVAL (XEXP (varop, 1));
9121 unsigned HOST_WIDE_INT mask;
9122 rtx mask_rtx;
9124 /* We have one common special case. We can't do any merging if
9125 the inner code is an ASHIFTRT of a smaller mode. However, if
9126 we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
9127 with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
9128 we can convert it to
9129 (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0 C2) C3) C1).
9130 This simplifies certain SIGN_EXTEND operations. */
9131 if (code == ASHIFT && first_code == ASHIFTRT
9132 && count == (GET_MODE_BITSIZE (result_mode)
9133 - GET_MODE_BITSIZE (GET_MODE (varop))))
9135 /* C3 has the low-order C1 bits zero. */
9137 mask = (GET_MODE_MASK (mode)
9138 & ~(((HOST_WIDE_INT) 1 << first_count) - 1));
9140 varop = simplify_and_const_int (NULL_RTX, result_mode,
9141 XEXP (varop, 0), mask);
9142 varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
9143 varop, count);
9144 count = first_count;
9145 code = ASHIFTRT;
9146 continue;
9149 /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
9150 than C1 high-order bits equal to the sign bit, we can convert
9151 this to either an ASHIFT or an ASHIFTRT depending on the
9152 two counts.
9154 We cannot do this if VAROP's mode is not SHIFT_MODE. */
9156 if (code == ASHIFTRT && first_code == ASHIFT
9157 && GET_MODE (varop) == shift_mode
9158 && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
9159 > first_count))
9161 varop = XEXP (varop, 0);
9162 count -= first_count;
9163 if (count < 0)
9165 count = -count;
9166 code = ASHIFT;
9169 continue;
9172 /* There are some cases we can't do. If CODE is ASHIFTRT,
9173 we can only do this if FIRST_CODE is also ASHIFTRT.
9175 We can't do the case when CODE is ROTATE and FIRST_CODE is
9176 ASHIFTRT.
9178 If the mode of this shift is not the mode of the outer shift,
9179 we can't do this if either shift is a right shift or ROTATE.
9181 Finally, we can't do any of these if the mode is too wide
9182 unless the codes are the same.
9184 Handle the case where the shift codes are the same
9185 first. */
9187 if (code == first_code)
9189 if (GET_MODE (varop) != result_mode
9190 && (code == ASHIFTRT || code == LSHIFTRT
9191 || code == ROTATE))
9192 break;
9194 count += first_count;
9195 varop = XEXP (varop, 0);
9196 continue;
9199 if (code == ASHIFTRT
9200 || (code == ROTATE && first_code == ASHIFTRT)
9201 || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT
9202 || (GET_MODE (varop) != result_mode
9203 && (first_code == ASHIFTRT || first_code == LSHIFTRT
9204 || first_code == ROTATE
9205 || code == ROTATE)))
9206 break;
9208 /* To compute the mask to apply after the shift, shift the
9209 nonzero bits of the inner shift the same way the
9210 outer shift will. */
9212 mask_rtx = GEN_INT (nonzero_bits (varop, GET_MODE (varop)));
9214 mask_rtx
9215 = simplify_const_binary_operation (code, result_mode, mask_rtx,
9216 GEN_INT (count));
9218 /* Give up if we can't compute an outer operation to use. */
9219 if (mask_rtx == 0
9220 || GET_CODE (mask_rtx) != CONST_INT
9221 || ! merge_outer_ops (&outer_op, &outer_const, AND,
9222 INTVAL (mask_rtx),
9223 result_mode, &complement_p))
9224 break;
9226 /* If the shifts are in the same direction, we add the
9227 counts. Otherwise, we subtract them. */
9228 if ((code == ASHIFTRT || code == LSHIFTRT)
9229 == (first_code == ASHIFTRT || first_code == LSHIFTRT))
9230 count += first_count;
9231 else
9232 count -= first_count;
9234 /* If COUNT is positive, the new shift is usually CODE,
9235 except for the two exceptions below, in which case it is
9236 FIRST_CODE. If the count is negative, FIRST_CODE should
9237 always be used */
9238 if (count > 0
9239 && ((first_code == ROTATE && code == ASHIFT)
9240 || (first_code == ASHIFTRT && code == LSHIFTRT)))
9241 code = first_code;
9242 else if (count < 0)
9243 code = first_code, count = -count;
9245 varop = XEXP (varop, 0);
9246 continue;
9249 /* If we have (A << B << C) for any shift, we can convert this to
9250 (A << C << B). This wins if A is a constant. Only try this if
9251 B is not a constant. */
9253 else if (GET_CODE (varop) == code
9254 && GET_CODE (XEXP (varop, 0)) == CONST_INT
9255 && GET_CODE (XEXP (varop, 1)) != CONST_INT)
9257 rtx new = simplify_const_binary_operation (code, mode,
9258 XEXP (varop, 0),
9259 GEN_INT (count));
9260 varop = gen_rtx_fmt_ee (code, mode, new, XEXP (varop, 1));
9261 count = 0;
9262 continue;
9264 break;
9266 case NOT:
9267 /* Make this fit the case below. */
9268 varop = gen_rtx_XOR (mode, XEXP (varop, 0),
9269 GEN_INT (GET_MODE_MASK (mode)));
9270 continue;
9272 case IOR:
9273 case AND:
9274 case XOR:
9275 /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
9276 with C the size of VAROP - 1 and the shift is logical if
9277 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
9278 we have an (le X 0) operation. If we have an arithmetic shift
9279 and STORE_FLAG_VALUE is 1 or we have a logical shift with
9280 STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation. */
9282 if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
9283 && XEXP (XEXP (varop, 0), 1) == constm1_rtx
9284 && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9285 && (code == LSHIFTRT || code == ASHIFTRT)
9286 && count == (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
9287 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
9289 count = 0;
9290 varop = gen_rtx_LE (GET_MODE (varop), XEXP (varop, 1),
9291 const0_rtx);
9293 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
9294 varop = gen_rtx_NEG (GET_MODE (varop), varop);
9296 continue;
9299 /* If we have (shift (logical)), move the logical to the outside
9300 to allow it to possibly combine with another logical and the
9301 shift to combine with another shift. This also canonicalizes to
9302 what a ZERO_EXTRACT looks like. Also, some machines have
9303 (and (shift)) insns. */
9305 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9306 /* We can't do this if we have (ashiftrt (xor)) and the
9307 constant has its sign bit set in shift_mode. */
9308 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
9309 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
9310 shift_mode))
9311 && (new = simplify_const_binary_operation (code, result_mode,
9312 XEXP (varop, 1),
9313 GEN_INT (count))) != 0
9314 && GET_CODE (new) == CONST_INT
9315 && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
9316 INTVAL (new), result_mode, &complement_p))
9318 varop = XEXP (varop, 0);
9319 continue;
9322 /* If we can't do that, try to simplify the shift in each arm of the
9323 logical expression, make a new logical expression, and apply
9324 the inverse distributive law. This also can't be done
9325 for some (ashiftrt (xor)). */
9326 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9327 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
9328 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
9329 shift_mode)))
9331 rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
9332 XEXP (varop, 0), count);
9333 rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
9334 XEXP (varop, 1), count);
9336 varop = simplify_gen_binary (GET_CODE (varop), shift_mode,
9337 lhs, rhs);
9338 varop = apply_distributive_law (varop);
9340 count = 0;
9341 continue;
9343 break;
9345 case EQ:
9346 /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
9347 says that the sign bit can be tested, FOO has mode MODE, C is
9348 GET_MODE_BITSIZE (MODE) - 1, and FOO has only its low-order bit
9349 that may be nonzero. */
9350 if (code == LSHIFTRT
9351 && XEXP (varop, 1) == const0_rtx
9352 && GET_MODE (XEXP (varop, 0)) == result_mode
9353 && count == (GET_MODE_BITSIZE (result_mode) - 1)
9354 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9355 && STORE_FLAG_VALUE == -1
9356 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
9357 && merge_outer_ops (&outer_op, &outer_const, XOR,
9358 (HOST_WIDE_INT) 1, result_mode,
9359 &complement_p))
9361 varop = XEXP (varop, 0);
9362 count = 0;
9363 continue;
9365 break;
9367 case NEG:
9368 /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
9369 than the number of bits in the mode is equivalent to A. */
9370 if (code == LSHIFTRT
9371 && count == (GET_MODE_BITSIZE (result_mode) - 1)
9372 && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
9374 varop = XEXP (varop, 0);
9375 count = 0;
9376 continue;
9379 /* NEG commutes with ASHIFT since it is multiplication. Move the
9380 NEG outside to allow shifts to combine. */
9381 if (code == ASHIFT
9382 && merge_outer_ops (&outer_op, &outer_const, NEG,
9383 (HOST_WIDE_INT) 0, result_mode,
9384 &complement_p))
9386 varop = XEXP (varop, 0);
9387 continue;
9389 break;
9391 case PLUS:
9392 /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
9393 is one less than the number of bits in the mode is
9394 equivalent to (xor A 1). */
9395 if (code == LSHIFTRT
9396 && count == (GET_MODE_BITSIZE (result_mode) - 1)
9397 && XEXP (varop, 1) == constm1_rtx
9398 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
9399 && merge_outer_ops (&outer_op, &outer_const, XOR,
9400 (HOST_WIDE_INT) 1, result_mode,
9401 &complement_p))
9403 count = 0;
9404 varop = XEXP (varop, 0);
9405 continue;
9408 /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
9409 that might be nonzero in BAR are those being shifted out and those
9410 bits are known zero in FOO, we can replace the PLUS with FOO.
9411 Similarly in the other operand order. This code occurs when
9412 we are computing the size of a variable-size array. */
9414 if ((code == ASHIFTRT || code == LSHIFTRT)
9415 && count < HOST_BITS_PER_WIDE_INT
9416 && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
9417 && (nonzero_bits (XEXP (varop, 1), result_mode)
9418 & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
9420 varop = XEXP (varop, 0);
9421 continue;
9423 else if ((code == ASHIFTRT || code == LSHIFTRT)
9424 && count < HOST_BITS_PER_WIDE_INT
9425 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9426 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
9427 >> count)
9428 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
9429 & nonzero_bits (XEXP (varop, 1),
9430 result_mode)))
9432 varop = XEXP (varop, 1);
9433 continue;
9436 /* (ashift (plus foo C) N) is (plus (ashift foo N) C'). */
9437 if (code == ASHIFT
9438 && GET_CODE (XEXP (varop, 1)) == CONST_INT
9439 && (new = simplify_const_binary_operation (ASHIFT, result_mode,
9440 XEXP (varop, 1),
9441 GEN_INT (count))) != 0
9442 && GET_CODE (new) == CONST_INT
9443 && merge_outer_ops (&outer_op, &outer_const, PLUS,
9444 INTVAL (new), result_mode, &complement_p))
9446 varop = XEXP (varop, 0);
9447 continue;
9450 /* Check for 'PLUS signbit', which is the canonical form of 'XOR
9451 signbit', and attempt to change the PLUS to an XOR and move it to
9452 the outer operation as is done above in the AND/IOR/XOR case
9453 leg for shift(logical). See details in logical handling above
9454 for reasoning in doing so. */
9455 if (code == LSHIFTRT
9456 && GET_CODE (XEXP (varop, 1)) == CONST_INT
9457 && mode_signbit_p (result_mode, XEXP (varop, 1))
9458 && (new = simplify_const_binary_operation (code, result_mode,
9459 XEXP (varop, 1),
9460 GEN_INT (count))) != 0
9461 && GET_CODE (new) == CONST_INT
9462 && merge_outer_ops (&outer_op, &outer_const, XOR,
9463 INTVAL (new), result_mode, &complement_p))
9465 varop = XEXP (varop, 0);
9466 continue;
9469 break;
9471 case MINUS:
9472 /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
9473 with C the size of VAROP - 1 and the shift is logical if
9474 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
9475 we have a (gt X 0) operation. If the shift is arithmetic with
9476 STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
9477 we have a (neg (gt X 0)) operation. */
9479 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9480 && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
9481 && count == (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
9482 && (code == LSHIFTRT || code == ASHIFTRT)
9483 && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
9484 && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
9485 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
9487 count = 0;
9488 varop = gen_rtx_GT (GET_MODE (varop), XEXP (varop, 1),
9489 const0_rtx);
9491 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
9492 varop = gen_rtx_NEG (GET_MODE (varop), varop);
9494 continue;
9496 break;
9498 case TRUNCATE:
9499 /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
9500 if the truncate does not affect the value. */
9501 if (code == LSHIFTRT
9502 && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
9503 && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
9504 && (INTVAL (XEXP (XEXP (varop, 0), 1))
9505 >= (GET_MODE_BITSIZE (GET_MODE (XEXP (varop, 0)))
9506 - GET_MODE_BITSIZE (GET_MODE (varop)))))
9508 rtx varop_inner = XEXP (varop, 0);
9510 varop_inner
9511 = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
9512 XEXP (varop_inner, 0),
9513 GEN_INT
9514 (count + INTVAL (XEXP (varop_inner, 1))));
9515 varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
9516 count = 0;
9517 continue;
9519 break;
9521 default:
9522 break;
9525 break;
9528 /* We need to determine what mode to do the shift in. If the shift is
9529 a right shift or ROTATE, we must always do it in the mode it was
9530 originally done in. Otherwise, we can do it in MODE, the widest mode
9531 encountered. The code we care about is that of the shift that will
9532 actually be done, not the shift that was originally requested. */
9533 shift_mode
9534 = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
9535 ? result_mode : mode);
9537 /* We have now finished analyzing the shift. The result should be
9538 a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places. If
9539 OUTER_OP is non-UNKNOWN, it is an operation that needs to be applied
9540 to the result of the shift. OUTER_CONST is the relevant constant,
9541 but we must turn off all bits turned off in the shift. */
9543 if (outer_op == UNKNOWN
9544 && orig_code == code && orig_count == count
9545 && varop == orig_varop
9546 && shift_mode == GET_MODE (varop))
9547 return NULL_RTX;
9549 /* Make a SUBREG if necessary. If we can't make it, fail. */
9550 varop = gen_lowpart (shift_mode, varop);
9551 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
9552 return NULL_RTX;
9554 /* If we have an outer operation and we just made a shift, it is
9555 possible that we could have simplified the shift were it not
9556 for the outer operation. So try to do the simplification
9557 recursively. */
9559 if (outer_op != UNKNOWN)
9560 x = simplify_shift_const_1 (code, shift_mode, varop, count);
9561 else
9562 x = NULL_RTX;
9564 if (x == NULL_RTX)
9565 x = simplify_gen_binary (code, shift_mode, varop, GEN_INT (count));
9567 /* If we were doing an LSHIFTRT in a wider mode than it was originally,
9568 turn off all the bits that the shift would have turned off. */
9569 if (orig_code == LSHIFTRT && result_mode != shift_mode)
9570 x = simplify_and_const_int (NULL_RTX, shift_mode, x,
9571 GET_MODE_MASK (result_mode) >> orig_count);
9573 /* Do the remainder of the processing in RESULT_MODE. */
9574 x = gen_lowpart_or_truncate (result_mode, x);
9576 /* If COMPLEMENT_P is set, we have to complement X before doing the outer
9577 operation. */
9578 if (complement_p)
9579 x = simplify_gen_unary (NOT, result_mode, x, result_mode);
9581 if (outer_op != UNKNOWN)
9583 if (GET_MODE_BITSIZE (result_mode) < HOST_BITS_PER_WIDE_INT)
9584 outer_const = trunc_int_for_mode (outer_const, result_mode);
9586 if (outer_op == AND)
9587 x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
9588 else if (outer_op == SET)
9590 /* This means that we have determined that the result is
9591 equivalent to a constant. This should be rare. */
9592 if (!side_effects_p (x))
9593 x = GEN_INT (outer_const);
9595 else if (GET_RTX_CLASS (outer_op) == RTX_UNARY)
9596 x = simplify_gen_unary (outer_op, result_mode, x, result_mode);
9597 else
9598 x = simplify_gen_binary (outer_op, result_mode, x,
9599 GEN_INT (outer_const));
9602 return x;
9605 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
9606 The result of the shift is RESULT_MODE. If we cannot simplify it,
9607 return X or, if it is NULL, synthesize the expression with
9608 simplify_gen_binary. Otherwise, return a simplified value.
9610 The shift is normally computed in the widest mode we find in VAROP, as
9611 long as it isn't a different number of words than RESULT_MODE. Exceptions
9612 are ASHIFTRT and ROTATE, which are always done in their original mode. */
9614 static rtx
9615 simplify_shift_const (rtx x, enum rtx_code code, enum machine_mode result_mode,
9616 rtx varop, int count)
9618 rtx tem = simplify_shift_const_1 (code, result_mode, varop, count);
9619 if (tem)
9620 return tem;
9622 if (!x)
9623 x = simplify_gen_binary (code, GET_MODE (varop), varop, GEN_INT (count));
9624 if (GET_MODE (x) != result_mode)
9625 x = gen_lowpart (result_mode, x);
9626 return x;
9630 /* Like recog, but we receive the address of a pointer to a new pattern.
9631 We try to match the rtx that the pointer points to.
9632 If that fails, we may try to modify or replace the pattern,
9633 storing the replacement into the same pointer object.
9635 Modifications include deletion or addition of CLOBBERs.
9637 PNOTES is a pointer to a location where any REG_UNUSED notes added for
9638 the CLOBBERs are placed.
9640 The value is the final insn code from the pattern ultimately matched,
9641 or -1. */
9643 static int
9644 recog_for_combine (rtx *pnewpat, rtx insn, rtx *pnotes)
9646 rtx pat = *pnewpat;
9647 int insn_code_number;
9648 int num_clobbers_to_add = 0;
9649 int i;
9650 rtx notes = 0;
9651 rtx old_notes, old_pat;
9653 /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
9654 we use to indicate that something didn't match. If we find such a
9655 thing, force rejection. */
9656 if (GET_CODE (pat) == PARALLEL)
9657 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
9658 if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
9659 && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
9660 return -1;
9662 old_pat = PATTERN (insn);
9663 old_notes = REG_NOTES (insn);
9664 PATTERN (insn) = pat;
9665 REG_NOTES (insn) = 0;
9667 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
9668 if (dump_file && (dump_flags & TDF_DETAILS))
9670 if (insn_code_number < 0)
9671 fputs ("Failed to match this instruction:\n", dump_file);
9672 else
9673 fputs ("Successfully matched this instruction:\n", dump_file);
9674 print_rtl_single (dump_file, pat);
9677 /* If it isn't, there is the possibility that we previously had an insn
9678 that clobbered some register as a side effect, but the combined
9679 insn doesn't need to do that. So try once more without the clobbers
9680 unless this represents an ASM insn. */
9682 if (insn_code_number < 0 && ! check_asm_operands (pat)
9683 && GET_CODE (pat) == PARALLEL)
9685 int pos;
9687 for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
9688 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
9690 if (i != pos)
9691 SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
9692 pos++;
9695 SUBST_INT (XVECLEN (pat, 0), pos);
9697 if (pos == 1)
9698 pat = XVECEXP (pat, 0, 0);
9700 PATTERN (insn) = pat;
9701 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
9702 if (dump_file && (dump_flags & TDF_DETAILS))
9704 if (insn_code_number < 0)
9705 fputs ("Failed to match this instruction:\n", dump_file);
9706 else
9707 fputs ("Successfully matched this instruction:\n", dump_file);
9708 print_rtl_single (dump_file, pat);
9711 PATTERN (insn) = old_pat;
9712 REG_NOTES (insn) = old_notes;
9714 /* Recognize all noop sets, these will be killed by followup pass. */
9715 if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
9716 insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
9718 /* If we had any clobbers to add, make a new pattern than contains
9719 them. Then check to make sure that all of them are dead. */
9720 if (num_clobbers_to_add)
9722 rtx newpat = gen_rtx_PARALLEL (VOIDmode,
9723 rtvec_alloc (GET_CODE (pat) == PARALLEL
9724 ? (XVECLEN (pat, 0)
9725 + num_clobbers_to_add)
9726 : num_clobbers_to_add + 1));
9728 if (GET_CODE (pat) == PARALLEL)
9729 for (i = 0; i < XVECLEN (pat, 0); i++)
9730 XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
9731 else
9732 XVECEXP (newpat, 0, 0) = pat;
9734 add_clobbers (newpat, insn_code_number);
9736 for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
9737 i < XVECLEN (newpat, 0); i++)
9739 if (REG_P (XEXP (XVECEXP (newpat, 0, i), 0))
9740 && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
9741 return -1;
9742 if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) != SCRATCH)
9744 gcc_assert (REG_P (XEXP (XVECEXP (newpat, 0, i), 0)));
9745 notes = gen_rtx_EXPR_LIST (REG_UNUSED,
9746 XEXP (XVECEXP (newpat, 0, i), 0), notes);
9749 pat = newpat;
9752 *pnewpat = pat;
9753 *pnotes = notes;
9755 return insn_code_number;
9758 /* Like gen_lowpart_general but for use by combine. In combine it
9759 is not possible to create any new pseudoregs. However, it is
9760 safe to create invalid memory addresses, because combine will
9761 try to recognize them and all they will do is make the combine
9762 attempt fail.
9764 If for some reason this cannot do its job, an rtx
9765 (clobber (const_int 0)) is returned.
9766 An insn containing that will not be recognized. */
9768 static rtx
9769 gen_lowpart_for_combine (enum machine_mode omode, rtx x)
9771 enum machine_mode imode = GET_MODE (x);
9772 unsigned int osize = GET_MODE_SIZE (omode);
9773 unsigned int isize = GET_MODE_SIZE (imode);
9774 rtx result;
9776 if (omode == imode)
9777 return x;
9779 /* Return identity if this is a CONST or symbolic reference. */
9780 if (omode == Pmode
9781 && (GET_CODE (x) == CONST
9782 || GET_CODE (x) == SYMBOL_REF
9783 || GET_CODE (x) == LABEL_REF))
9784 return x;
9786 /* We can only support MODE being wider than a word if X is a
9787 constant integer or has a mode the same size. */
9788 if (GET_MODE_SIZE (omode) > UNITS_PER_WORD
9789 && ! ((imode == VOIDmode
9790 && (GET_CODE (x) == CONST_INT
9791 || GET_CODE (x) == CONST_DOUBLE))
9792 || isize == osize))
9793 goto fail;
9795 /* X might be a paradoxical (subreg (mem)). In that case, gen_lowpart
9796 won't know what to do. So we will strip off the SUBREG here and
9797 process normally. */
9798 if (GET_CODE (x) == SUBREG && MEM_P (SUBREG_REG (x)))
9800 x = SUBREG_REG (x);
9802 /* For use in case we fall down into the address adjustments
9803 further below, we need to adjust the known mode and size of
9804 x; imode and isize, since we just adjusted x. */
9805 imode = GET_MODE (x);
9807 if (imode == omode)
9808 return x;
9810 isize = GET_MODE_SIZE (imode);
9813 result = gen_lowpart_common (omode, x);
9815 if (result)
9816 return result;
9818 if (MEM_P (x))
9820 int offset = 0;
9822 /* Refuse to work on a volatile memory ref or one with a mode-dependent
9823 address. */
9824 if (MEM_VOLATILE_P (x) || mode_dependent_address_p (XEXP (x, 0)))
9825 goto fail;
9827 /* If we want to refer to something bigger than the original memref,
9828 generate a paradoxical subreg instead. That will force a reload
9829 of the original memref X. */
9830 if (isize < osize)
9831 return gen_rtx_SUBREG (omode, x, 0);
9833 if (WORDS_BIG_ENDIAN)
9834 offset = MAX (isize, UNITS_PER_WORD) - MAX (osize, UNITS_PER_WORD);
9836 /* Adjust the address so that the address-after-the-data is
9837 unchanged. */
9838 if (BYTES_BIG_ENDIAN)
9839 offset -= MIN (UNITS_PER_WORD, osize) - MIN (UNITS_PER_WORD, isize);
9841 return adjust_address_nv (x, omode, offset);
9844 /* If X is a comparison operator, rewrite it in a new mode. This
9845 probably won't match, but may allow further simplifications. */
9846 else if (COMPARISON_P (x))
9847 return gen_rtx_fmt_ee (GET_CODE (x), omode, XEXP (x, 0), XEXP (x, 1));
9849 /* If we couldn't simplify X any other way, just enclose it in a
9850 SUBREG. Normally, this SUBREG won't match, but some patterns may
9851 include an explicit SUBREG or we may simplify it further in combine. */
9852 else
9854 int offset = 0;
9855 rtx res;
9857 offset = subreg_lowpart_offset (omode, imode);
9858 if (imode == VOIDmode)
9860 imode = int_mode_for_mode (omode);
9861 x = gen_lowpart_common (imode, x);
9862 if (x == NULL)
9863 goto fail;
9865 res = simplify_gen_subreg (omode, x, imode, offset);
9866 if (res)
9867 return res;
9870 fail:
9871 return gen_rtx_CLOBBER (imode, const0_rtx);
9874 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
9875 comparison code that will be tested.
9877 The result is a possibly different comparison code to use. *POP0 and
9878 *POP1 may be updated.
9880 It is possible that we might detect that a comparison is either always
9881 true or always false. However, we do not perform general constant
9882 folding in combine, so this knowledge isn't useful. Such tautologies
9883 should have been detected earlier. Hence we ignore all such cases. */
9885 static enum rtx_code
9886 simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
9888 rtx op0 = *pop0;
9889 rtx op1 = *pop1;
9890 rtx tem, tem1;
9891 int i;
9892 enum machine_mode mode, tmode;
9894 /* Try a few ways of applying the same transformation to both operands. */
9895 while (1)
9897 #ifndef WORD_REGISTER_OPERATIONS
9898 /* The test below this one won't handle SIGN_EXTENDs on these machines,
9899 so check specially. */
9900 if (code != GTU && code != GEU && code != LTU && code != LEU
9901 && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
9902 && GET_CODE (XEXP (op0, 0)) == ASHIFT
9903 && GET_CODE (XEXP (op1, 0)) == ASHIFT
9904 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
9905 && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
9906 && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
9907 == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
9908 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9909 && XEXP (op0, 1) == XEXP (op1, 1)
9910 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
9911 && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
9912 && (INTVAL (XEXP (op0, 1))
9913 == (GET_MODE_BITSIZE (GET_MODE (op0))
9914 - (GET_MODE_BITSIZE
9915 (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
9917 op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
9918 op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
9920 #endif
9922 /* If both operands are the same constant shift, see if we can ignore the
9923 shift. We can if the shift is a rotate or if the bits shifted out of
9924 this shift are known to be zero for both inputs and if the type of
9925 comparison is compatible with the shift. */
9926 if (GET_CODE (op0) == GET_CODE (op1)
9927 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
9928 && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
9929 || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
9930 && (code != GT && code != LT && code != GE && code != LE))
9931 || (GET_CODE (op0) == ASHIFTRT
9932 && (code != GTU && code != LTU
9933 && code != GEU && code != LEU)))
9934 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9935 && INTVAL (XEXP (op0, 1)) >= 0
9936 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
9937 && XEXP (op0, 1) == XEXP (op1, 1))
9939 enum machine_mode mode = GET_MODE (op0);
9940 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
9941 int shift_count = INTVAL (XEXP (op0, 1));
9943 if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
9944 mask &= (mask >> shift_count) << shift_count;
9945 else if (GET_CODE (op0) == ASHIFT)
9946 mask = (mask & (mask << shift_count)) >> shift_count;
9948 if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
9949 && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
9950 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
9951 else
9952 break;
9955 /* If both operands are AND's of a paradoxical SUBREG by constant, the
9956 SUBREGs are of the same mode, and, in both cases, the AND would
9957 be redundant if the comparison was done in the narrower mode,
9958 do the comparison in the narrower mode (e.g., we are AND'ing with 1
9959 and the operand's possibly nonzero bits are 0xffffff01; in that case
9960 if we only care about QImode, we don't need the AND). This case
9961 occurs if the output mode of an scc insn is not SImode and
9962 STORE_FLAG_VALUE == 1 (e.g., the 386).
9964 Similarly, check for a case where the AND's are ZERO_EXTEND
9965 operations from some narrower mode even though a SUBREG is not
9966 present. */
9968 else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
9969 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9970 && GET_CODE (XEXP (op1, 1)) == CONST_INT)
9972 rtx inner_op0 = XEXP (op0, 0);
9973 rtx inner_op1 = XEXP (op1, 0);
9974 HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
9975 HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
9976 int changed = 0;
9978 if (GET_CODE (inner_op0) == SUBREG && GET_CODE (inner_op1) == SUBREG
9979 && (GET_MODE_SIZE (GET_MODE (inner_op0))
9980 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (inner_op0))))
9981 && (GET_MODE (SUBREG_REG (inner_op0))
9982 == GET_MODE (SUBREG_REG (inner_op1)))
9983 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (inner_op0)))
9984 <= HOST_BITS_PER_WIDE_INT)
9985 && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
9986 GET_MODE (SUBREG_REG (inner_op0)))))
9987 && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
9988 GET_MODE (SUBREG_REG (inner_op1))))))
9990 op0 = SUBREG_REG (inner_op0);
9991 op1 = SUBREG_REG (inner_op1);
9993 /* The resulting comparison is always unsigned since we masked
9994 off the original sign bit. */
9995 code = unsigned_condition (code);
9997 changed = 1;
10000 else if (c0 == c1)
10001 for (tmode = GET_CLASS_NARROWEST_MODE
10002 (GET_MODE_CLASS (GET_MODE (op0)));
10003 tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
10004 if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
10006 op0 = gen_lowpart (tmode, inner_op0);
10007 op1 = gen_lowpart (tmode, inner_op1);
10008 code = unsigned_condition (code);
10009 changed = 1;
10010 break;
10013 if (! changed)
10014 break;
10017 /* If both operands are NOT, we can strip off the outer operation
10018 and adjust the comparison code for swapped operands; similarly for
10019 NEG, except that this must be an equality comparison. */
10020 else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
10021 || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
10022 && (code == EQ || code == NE)))
10023 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
10025 else
10026 break;
10029 /* If the first operand is a constant, swap the operands and adjust the
10030 comparison code appropriately, but don't do this if the second operand
10031 is already a constant integer. */
10032 if (swap_commutative_operands_p (op0, op1))
10034 tem = op0, op0 = op1, op1 = tem;
10035 code = swap_condition (code);
10038 /* We now enter a loop during which we will try to simplify the comparison.
10039 For the most part, we only are concerned with comparisons with zero,
10040 but some things may really be comparisons with zero but not start
10041 out looking that way. */
10043 while (GET_CODE (op1) == CONST_INT)
10045 enum machine_mode mode = GET_MODE (op0);
10046 unsigned int mode_width = GET_MODE_BITSIZE (mode);
10047 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
10048 int equality_comparison_p;
10049 int sign_bit_comparison_p;
10050 int unsigned_comparison_p;
10051 HOST_WIDE_INT const_op;
10053 /* We only want to handle integral modes. This catches VOIDmode,
10054 CCmode, and the floating-point modes. An exception is that we
10055 can handle VOIDmode if OP0 is a COMPARE or a comparison
10056 operation. */
10058 if (GET_MODE_CLASS (mode) != MODE_INT
10059 && ! (mode == VOIDmode
10060 && (GET_CODE (op0) == COMPARE || COMPARISON_P (op0))))
10061 break;
10063 /* Get the constant we are comparing against and turn off all bits
10064 not on in our mode. */
10065 const_op = INTVAL (op1);
10066 if (mode != VOIDmode)
10067 const_op = trunc_int_for_mode (const_op, mode);
10068 op1 = GEN_INT (const_op);
10070 /* If we are comparing against a constant power of two and the value
10071 being compared can only have that single bit nonzero (e.g., it was
10072 `and'ed with that bit), we can replace this with a comparison
10073 with zero. */
10074 if (const_op
10075 && (code == EQ || code == NE || code == GE || code == GEU
10076 || code == LT || code == LTU)
10077 && mode_width <= HOST_BITS_PER_WIDE_INT
10078 && exact_log2 (const_op) >= 0
10079 && nonzero_bits (op0, mode) == (unsigned HOST_WIDE_INT) const_op)
10081 code = (code == EQ || code == GE || code == GEU ? NE : EQ);
10082 op1 = const0_rtx, const_op = 0;
10085 /* Similarly, if we are comparing a value known to be either -1 or
10086 0 with -1, change it to the opposite comparison against zero. */
10088 if (const_op == -1
10089 && (code == EQ || code == NE || code == GT || code == LE
10090 || code == GEU || code == LTU)
10091 && num_sign_bit_copies (op0, mode) == mode_width)
10093 code = (code == EQ || code == LE || code == GEU ? NE : EQ);
10094 op1 = const0_rtx, const_op = 0;
10097 /* Do some canonicalizations based on the comparison code. We prefer
10098 comparisons against zero and then prefer equality comparisons.
10099 If we can reduce the size of a constant, we will do that too. */
10101 switch (code)
10103 case LT:
10104 /* < C is equivalent to <= (C - 1) */
10105 if (const_op > 0)
10107 const_op -= 1;
10108 op1 = GEN_INT (const_op);
10109 code = LE;
10110 /* ... fall through to LE case below. */
10112 else
10113 break;
10115 case LE:
10116 /* <= C is equivalent to < (C + 1); we do this for C < 0 */
10117 if (const_op < 0)
10119 const_op += 1;
10120 op1 = GEN_INT (const_op);
10121 code = LT;
10124 /* If we are doing a <= 0 comparison on a value known to have
10125 a zero sign bit, we can replace this with == 0. */
10126 else if (const_op == 0
10127 && mode_width <= HOST_BITS_PER_WIDE_INT
10128 && (nonzero_bits (op0, mode)
10129 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
10130 code = EQ;
10131 break;
10133 case GE:
10134 /* >= C is equivalent to > (C - 1). */
10135 if (const_op > 0)
10137 const_op -= 1;
10138 op1 = GEN_INT (const_op);
10139 code = GT;
10140 /* ... fall through to GT below. */
10142 else
10143 break;
10145 case GT:
10146 /* > C is equivalent to >= (C + 1); we do this for C < 0. */
10147 if (const_op < 0)
10149 const_op += 1;
10150 op1 = GEN_INT (const_op);
10151 code = GE;
10154 /* If we are doing a > 0 comparison on a value known to have
10155 a zero sign bit, we can replace this with != 0. */
10156 else if (const_op == 0
10157 && mode_width <= HOST_BITS_PER_WIDE_INT
10158 && (nonzero_bits (op0, mode)
10159 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
10160 code = NE;
10161 break;
10163 case LTU:
10164 /* < C is equivalent to <= (C - 1). */
10165 if (const_op > 0)
10167 const_op -= 1;
10168 op1 = GEN_INT (const_op);
10169 code = LEU;
10170 /* ... fall through ... */
10173 /* (unsigned) < 0x80000000 is equivalent to >= 0. */
10174 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10175 && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
10177 const_op = 0, op1 = const0_rtx;
10178 code = GE;
10179 break;
10181 else
10182 break;
10184 case LEU:
10185 /* unsigned <= 0 is equivalent to == 0 */
10186 if (const_op == 0)
10187 code = EQ;
10189 /* (unsigned) <= 0x7fffffff is equivalent to >= 0. */
10190 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10191 && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
10193 const_op = 0, op1 = const0_rtx;
10194 code = GE;
10196 break;
10198 case GEU:
10199 /* >= C is equivalent to > (C - 1). */
10200 if (const_op > 1)
10202 const_op -= 1;
10203 op1 = GEN_INT (const_op);
10204 code = GTU;
10205 /* ... fall through ... */
10208 /* (unsigned) >= 0x80000000 is equivalent to < 0. */
10209 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10210 && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
10212 const_op = 0, op1 = const0_rtx;
10213 code = LT;
10214 break;
10216 else
10217 break;
10219 case GTU:
10220 /* unsigned > 0 is equivalent to != 0 */
10221 if (const_op == 0)
10222 code = NE;
10224 /* (unsigned) > 0x7fffffff is equivalent to < 0. */
10225 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10226 && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
10228 const_op = 0, op1 = const0_rtx;
10229 code = LT;
10231 break;
10233 default:
10234 break;
10237 /* Compute some predicates to simplify code below. */
10239 equality_comparison_p = (code == EQ || code == NE);
10240 sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
10241 unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
10242 || code == GEU);
10244 /* If this is a sign bit comparison and we can do arithmetic in
10245 MODE, say that we will only be needing the sign bit of OP0. */
10246 if (sign_bit_comparison_p
10247 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10248 op0 = force_to_mode (op0, mode,
10249 ((HOST_WIDE_INT) 1
10250 << (GET_MODE_BITSIZE (mode) - 1)),
10253 /* Now try cases based on the opcode of OP0. If none of the cases
10254 does a "continue", we exit this loop immediately after the
10255 switch. */
10257 switch (GET_CODE (op0))
10259 case ZERO_EXTRACT:
10260 /* If we are extracting a single bit from a variable position in
10261 a constant that has only a single bit set and are comparing it
10262 with zero, we can convert this into an equality comparison
10263 between the position and the location of the single bit. */
10264 /* Except we can't if SHIFT_COUNT_TRUNCATED is set, since we might
10265 have already reduced the shift count modulo the word size. */
10266 if (!SHIFT_COUNT_TRUNCATED
10267 && GET_CODE (XEXP (op0, 0)) == CONST_INT
10268 && XEXP (op0, 1) == const1_rtx
10269 && equality_comparison_p && const_op == 0
10270 && (i = exact_log2 (INTVAL (XEXP (op0, 0)))) >= 0)
10272 if (BITS_BIG_ENDIAN)
10274 enum machine_mode new_mode
10275 = mode_for_extraction (EP_extzv, 1);
10276 if (new_mode == MAX_MACHINE_MODE)
10277 i = BITS_PER_WORD - 1 - i;
10278 else
10280 mode = new_mode;
10281 i = (GET_MODE_BITSIZE (mode) - 1 - i);
10285 op0 = XEXP (op0, 2);
10286 op1 = GEN_INT (i);
10287 const_op = i;
10289 /* Result is nonzero iff shift count is equal to I. */
10290 code = reverse_condition (code);
10291 continue;
10294 /* ... fall through ... */
10296 case SIGN_EXTRACT:
10297 tem = expand_compound_operation (op0);
10298 if (tem != op0)
10300 op0 = tem;
10301 continue;
10303 break;
10305 case NOT:
10306 /* If testing for equality, we can take the NOT of the constant. */
10307 if (equality_comparison_p
10308 && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
10310 op0 = XEXP (op0, 0);
10311 op1 = tem;
10312 continue;
10315 /* If just looking at the sign bit, reverse the sense of the
10316 comparison. */
10317 if (sign_bit_comparison_p)
10319 op0 = XEXP (op0, 0);
10320 code = (code == GE ? LT : GE);
10321 continue;
10323 break;
10325 case NEG:
10326 /* If testing for equality, we can take the NEG of the constant. */
10327 if (equality_comparison_p
10328 && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
10330 op0 = XEXP (op0, 0);
10331 op1 = tem;
10332 continue;
10335 /* The remaining cases only apply to comparisons with zero. */
10336 if (const_op != 0)
10337 break;
10339 /* When X is ABS or is known positive,
10340 (neg X) is < 0 if and only if X != 0. */
10342 if (sign_bit_comparison_p
10343 && (GET_CODE (XEXP (op0, 0)) == ABS
10344 || (mode_width <= HOST_BITS_PER_WIDE_INT
10345 && (nonzero_bits (XEXP (op0, 0), mode)
10346 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)))
10348 op0 = XEXP (op0, 0);
10349 code = (code == LT ? NE : EQ);
10350 continue;
10353 /* If we have NEG of something whose two high-order bits are the
10354 same, we know that "(-a) < 0" is equivalent to "a > 0". */
10355 if (num_sign_bit_copies (op0, mode) >= 2)
10357 op0 = XEXP (op0, 0);
10358 code = swap_condition (code);
10359 continue;
10361 break;
10363 case ROTATE:
10364 /* If we are testing equality and our count is a constant, we
10365 can perform the inverse operation on our RHS. */
10366 if (equality_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
10367 && (tem = simplify_binary_operation (ROTATERT, mode,
10368 op1, XEXP (op0, 1))) != 0)
10370 op0 = XEXP (op0, 0);
10371 op1 = tem;
10372 continue;
10375 /* If we are doing a < 0 or >= 0 comparison, it means we are testing
10376 a particular bit. Convert it to an AND of a constant of that
10377 bit. This will be converted into a ZERO_EXTRACT. */
10378 if (const_op == 0 && sign_bit_comparison_p
10379 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10380 && mode_width <= HOST_BITS_PER_WIDE_INT)
10382 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10383 ((HOST_WIDE_INT) 1
10384 << (mode_width - 1
10385 - INTVAL (XEXP (op0, 1)))));
10386 code = (code == LT ? NE : EQ);
10387 continue;
10390 /* Fall through. */
10392 case ABS:
10393 /* ABS is ignorable inside an equality comparison with zero. */
10394 if (const_op == 0 && equality_comparison_p)
10396 op0 = XEXP (op0, 0);
10397 continue;
10399 break;
10401 case SIGN_EXTEND:
10402 /* Can simplify (compare (zero/sign_extend FOO) CONST) to
10403 (compare FOO CONST) if CONST fits in FOO's mode and we
10404 are either testing inequality or have an unsigned
10405 comparison with ZERO_EXTEND or a signed comparison with
10406 SIGN_EXTEND. But don't do it if we don't have a compare
10407 insn of the given mode, since we'd have to revert it
10408 later on, and then we wouldn't know whether to sign- or
10409 zero-extend. */
10410 mode = GET_MODE (XEXP (op0, 0));
10411 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
10412 && ! unsigned_comparison_p
10413 && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10414 && ((unsigned HOST_WIDE_INT) const_op
10415 < (((unsigned HOST_WIDE_INT) 1
10416 << (GET_MODE_BITSIZE (mode) - 1))))
10417 && cmp_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
10419 op0 = XEXP (op0, 0);
10420 continue;
10422 break;
10424 case SUBREG:
10425 /* Check for the case where we are comparing A - C1 with C2, that is
10427 (subreg:MODE (plus (A) (-C1))) op (C2)
10429 with C1 a constant, and try to lift the SUBREG, i.e. to do the
10430 comparison in the wider mode. One of the following two conditions
10431 must be true in order for this to be valid:
10433 1. The mode extension results in the same bit pattern being added
10434 on both sides and the comparison is equality or unsigned. As
10435 C2 has been truncated to fit in MODE, the pattern can only be
10436 all 0s or all 1s.
10438 2. The mode extension results in the sign bit being copied on
10439 each side.
10441 The difficulty here is that we have predicates for A but not for
10442 (A - C1) so we need to check that C1 is within proper bounds so
10443 as to perturbate A as little as possible. */
10445 if (mode_width <= HOST_BITS_PER_WIDE_INT
10446 && subreg_lowpart_p (op0)
10447 && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) > mode_width
10448 && GET_CODE (SUBREG_REG (op0)) == PLUS
10449 && GET_CODE (XEXP (SUBREG_REG (op0), 1)) == CONST_INT)
10451 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (op0));
10452 rtx a = XEXP (SUBREG_REG (op0), 0);
10453 HOST_WIDE_INT c1 = -INTVAL (XEXP (SUBREG_REG (op0), 1));
10455 if ((c1 > 0
10456 && (unsigned HOST_WIDE_INT) c1
10457 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)
10458 && (equality_comparison_p || unsigned_comparison_p)
10459 /* (A - C1) zero-extends if it is positive and sign-extends
10460 if it is negative, C2 both zero- and sign-extends. */
10461 && ((0 == (nonzero_bits (a, inner_mode)
10462 & ~GET_MODE_MASK (mode))
10463 && const_op >= 0)
10464 /* (A - C1) sign-extends if it is positive and 1-extends
10465 if it is negative, C2 both sign- and 1-extends. */
10466 || (num_sign_bit_copies (a, inner_mode)
10467 > (unsigned int) (GET_MODE_BITSIZE (inner_mode)
10468 - mode_width)
10469 && const_op < 0)))
10470 || ((unsigned HOST_WIDE_INT) c1
10471 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 2)
10472 /* (A - C1) always sign-extends, like C2. */
10473 && num_sign_bit_copies (a, inner_mode)
10474 > (unsigned int) (GET_MODE_BITSIZE (inner_mode)
10475 - (mode_width - 1))))
10477 op0 = SUBREG_REG (op0);
10478 continue;
10482 /* If the inner mode is narrower and we are extracting the low part,
10483 we can treat the SUBREG as if it were a ZERO_EXTEND. */
10484 if (subreg_lowpart_p (op0)
10485 && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) < mode_width)
10486 /* Fall through */ ;
10487 else
10488 break;
10490 /* ... fall through ... */
10492 case ZERO_EXTEND:
10493 mode = GET_MODE (XEXP (op0, 0));
10494 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
10495 && (unsigned_comparison_p || equality_comparison_p)
10496 && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10497 && ((unsigned HOST_WIDE_INT) const_op < GET_MODE_MASK (mode))
10498 && cmp_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
10500 op0 = XEXP (op0, 0);
10501 continue;
10503 break;
10505 case PLUS:
10506 /* (eq (plus X A) B) -> (eq X (minus B A)). We can only do
10507 this for equality comparisons due to pathological cases involving
10508 overflows. */
10509 if (equality_comparison_p
10510 && 0 != (tem = simplify_binary_operation (MINUS, mode,
10511 op1, XEXP (op0, 1))))
10513 op0 = XEXP (op0, 0);
10514 op1 = tem;
10515 continue;
10518 /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0. */
10519 if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
10520 && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
10522 op0 = XEXP (XEXP (op0, 0), 0);
10523 code = (code == LT ? EQ : NE);
10524 continue;
10526 break;
10528 case MINUS:
10529 /* We used to optimize signed comparisons against zero, but that
10530 was incorrect. Unsigned comparisons against zero (GTU, LEU)
10531 arrive here as equality comparisons, or (GEU, LTU) are
10532 optimized away. No need to special-case them. */
10534 /* (eq (minus A B) C) -> (eq A (plus B C)) or
10535 (eq B (minus A C)), whichever simplifies. We can only do
10536 this for equality comparisons due to pathological cases involving
10537 overflows. */
10538 if (equality_comparison_p
10539 && 0 != (tem = simplify_binary_operation (PLUS, mode,
10540 XEXP (op0, 1), op1)))
10542 op0 = XEXP (op0, 0);
10543 op1 = tem;
10544 continue;
10547 if (equality_comparison_p
10548 && 0 != (tem = simplify_binary_operation (MINUS, mode,
10549 XEXP (op0, 0), op1)))
10551 op0 = XEXP (op0, 1);
10552 op1 = tem;
10553 continue;
10556 /* The sign bit of (minus (ashiftrt X C) X), where C is the number
10557 of bits in X minus 1, is one iff X > 0. */
10558 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
10559 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10560 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (op0, 0), 1))
10561 == mode_width - 1
10562 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10564 op0 = XEXP (op0, 1);
10565 code = (code == GE ? LE : GT);
10566 continue;
10568 break;
10570 case XOR:
10571 /* (eq (xor A B) C) -> (eq A (xor B C)). This is a simplification
10572 if C is zero or B is a constant. */
10573 if (equality_comparison_p
10574 && 0 != (tem = simplify_binary_operation (XOR, mode,
10575 XEXP (op0, 1), op1)))
10577 op0 = XEXP (op0, 0);
10578 op1 = tem;
10579 continue;
10581 break;
10583 case EQ: case NE:
10584 case UNEQ: case LTGT:
10585 case LT: case LTU: case UNLT: case LE: case LEU: case UNLE:
10586 case GT: case GTU: case UNGT: case GE: case GEU: case UNGE:
10587 case UNORDERED: case ORDERED:
10588 /* We can't do anything if OP0 is a condition code value, rather
10589 than an actual data value. */
10590 if (const_op != 0
10591 || CC0_P (XEXP (op0, 0))
10592 || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
10593 break;
10595 /* Get the two operands being compared. */
10596 if (GET_CODE (XEXP (op0, 0)) == COMPARE)
10597 tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
10598 else
10599 tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
10601 /* Check for the cases where we simply want the result of the
10602 earlier test or the opposite of that result. */
10603 if (code == NE || code == EQ
10604 || (GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
10605 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10606 && (STORE_FLAG_VALUE
10607 & (((HOST_WIDE_INT) 1
10608 << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
10609 && (code == LT || code == GE)))
10611 enum rtx_code new_code;
10612 if (code == LT || code == NE)
10613 new_code = GET_CODE (op0);
10614 else
10615 new_code = reversed_comparison_code (op0, NULL);
10617 if (new_code != UNKNOWN)
10619 code = new_code;
10620 op0 = tem;
10621 op1 = tem1;
10622 continue;
10625 break;
10627 case IOR:
10628 /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
10629 iff X <= 0. */
10630 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
10631 && XEXP (XEXP (op0, 0), 1) == constm1_rtx
10632 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10634 op0 = XEXP (op0, 1);
10635 code = (code == GE ? GT : LE);
10636 continue;
10638 break;
10640 case AND:
10641 /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1). This
10642 will be converted to a ZERO_EXTRACT later. */
10643 if (const_op == 0 && equality_comparison_p
10644 && GET_CODE (XEXP (op0, 0)) == ASHIFT
10645 && XEXP (XEXP (op0, 0), 0) == const1_rtx)
10647 op0 = simplify_and_const_int
10648 (NULL_RTX, mode, gen_rtx_LSHIFTRT (mode,
10649 XEXP (op0, 1),
10650 XEXP (XEXP (op0, 0), 1)),
10651 (HOST_WIDE_INT) 1);
10652 continue;
10655 /* If we are comparing (and (lshiftrt X C1) C2) for equality with
10656 zero and X is a comparison and C1 and C2 describe only bits set
10657 in STORE_FLAG_VALUE, we can compare with X. */
10658 if (const_op == 0 && equality_comparison_p
10659 && mode_width <= HOST_BITS_PER_WIDE_INT
10660 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10661 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
10662 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10663 && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
10664 && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
10666 mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
10667 << INTVAL (XEXP (XEXP (op0, 0), 1)));
10668 if ((~STORE_FLAG_VALUE & mask) == 0
10669 && (COMPARISON_P (XEXP (XEXP (op0, 0), 0))
10670 || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
10671 && COMPARISON_P (tem))))
10673 op0 = XEXP (XEXP (op0, 0), 0);
10674 continue;
10678 /* If we are doing an equality comparison of an AND of a bit equal
10679 to the sign bit, replace this with a LT or GE comparison of
10680 the underlying value. */
10681 if (equality_comparison_p
10682 && const_op == 0
10683 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10684 && mode_width <= HOST_BITS_PER_WIDE_INT
10685 && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
10686 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
10688 op0 = XEXP (op0, 0);
10689 code = (code == EQ ? GE : LT);
10690 continue;
10693 /* If this AND operation is really a ZERO_EXTEND from a narrower
10694 mode, the constant fits within that mode, and this is either an
10695 equality or unsigned comparison, try to do this comparison in
10696 the narrower mode.
10698 Note that in:
10700 (ne:DI (and:DI (reg:DI 4) (const_int 0xffffffff)) (const_int 0))
10701 -> (ne:DI (reg:SI 4) (const_int 0))
10703 unless TRULY_NOOP_TRUNCATION allows it or the register is
10704 known to hold a value of the required mode the
10705 transformation is invalid. */
10706 if ((equality_comparison_p || unsigned_comparison_p)
10707 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10708 && (i = exact_log2 ((INTVAL (XEXP (op0, 1))
10709 & GET_MODE_MASK (mode))
10710 + 1)) >= 0
10711 && const_op >> i == 0
10712 && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode
10713 && (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (tmode),
10714 GET_MODE_BITSIZE (GET_MODE (op0)))
10715 || (REG_P (XEXP (op0, 0))
10716 && reg_truncated_to_mode (tmode, XEXP (op0, 0)))))
10718 op0 = gen_lowpart (tmode, XEXP (op0, 0));
10719 continue;
10722 /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1
10723 fits in both M1 and M2 and the SUBREG is either paradoxical
10724 or represents the low part, permute the SUBREG and the AND
10725 and try again. */
10726 if (GET_CODE (XEXP (op0, 0)) == SUBREG)
10728 unsigned HOST_WIDE_INT c1;
10729 tmode = GET_MODE (SUBREG_REG (XEXP (op0, 0)));
10730 /* Require an integral mode, to avoid creating something like
10731 (AND:SF ...). */
10732 if (SCALAR_INT_MODE_P (tmode)
10733 /* It is unsafe to commute the AND into the SUBREG if the
10734 SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
10735 not defined. As originally written the upper bits
10736 have a defined value due to the AND operation.
10737 However, if we commute the AND inside the SUBREG then
10738 they no longer have defined values and the meaning of
10739 the code has been changed. */
10740 && (0
10741 #ifdef WORD_REGISTER_OPERATIONS
10742 || (mode_width > GET_MODE_BITSIZE (tmode)
10743 && mode_width <= BITS_PER_WORD)
10744 #endif
10745 || (mode_width <= GET_MODE_BITSIZE (tmode)
10746 && subreg_lowpart_p (XEXP (op0, 0))))
10747 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10748 && mode_width <= HOST_BITS_PER_WIDE_INT
10749 && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
10750 && ((c1 = INTVAL (XEXP (op0, 1))) & ~mask) == 0
10751 && (c1 & ~GET_MODE_MASK (tmode)) == 0
10752 && c1 != mask
10753 && c1 != GET_MODE_MASK (tmode))
10755 op0 = simplify_gen_binary (AND, tmode,
10756 SUBREG_REG (XEXP (op0, 0)),
10757 gen_int_mode (c1, tmode));
10758 op0 = gen_lowpart (mode, op0);
10759 continue;
10763 /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0). */
10764 if (const_op == 0 && equality_comparison_p
10765 && XEXP (op0, 1) == const1_rtx
10766 && GET_CODE (XEXP (op0, 0)) == NOT)
10768 op0 = simplify_and_const_int
10769 (NULL_RTX, mode, XEXP (XEXP (op0, 0), 0), (HOST_WIDE_INT) 1);
10770 code = (code == NE ? EQ : NE);
10771 continue;
10774 /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
10775 (eq (and (lshiftrt X) 1) 0).
10776 Also handle the case where (not X) is expressed using xor. */
10777 if (const_op == 0 && equality_comparison_p
10778 && XEXP (op0, 1) == const1_rtx
10779 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT)
10781 rtx shift_op = XEXP (XEXP (op0, 0), 0);
10782 rtx shift_count = XEXP (XEXP (op0, 0), 1);
10784 if (GET_CODE (shift_op) == NOT
10785 || (GET_CODE (shift_op) == XOR
10786 && GET_CODE (XEXP (shift_op, 1)) == CONST_INT
10787 && GET_CODE (shift_count) == CONST_INT
10788 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
10789 && (INTVAL (XEXP (shift_op, 1))
10790 == (HOST_WIDE_INT) 1 << INTVAL (shift_count))))
10792 op0 = simplify_and_const_int
10793 (NULL_RTX, mode,
10794 gen_rtx_LSHIFTRT (mode, XEXP (shift_op, 0), shift_count),
10795 (HOST_WIDE_INT) 1);
10796 code = (code == NE ? EQ : NE);
10797 continue;
10800 break;
10802 case ASHIFT:
10803 /* If we have (compare (ashift FOO N) (const_int C)) and
10804 the high order N bits of FOO (N+1 if an inequality comparison)
10805 are known to be zero, we can do this by comparing FOO with C
10806 shifted right N bits so long as the low-order N bits of C are
10807 zero. */
10808 if (GET_CODE (XEXP (op0, 1)) == CONST_INT
10809 && INTVAL (XEXP (op0, 1)) >= 0
10810 && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
10811 < HOST_BITS_PER_WIDE_INT)
10812 && ((const_op
10813 & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0)
10814 && mode_width <= HOST_BITS_PER_WIDE_INT
10815 && (nonzero_bits (XEXP (op0, 0), mode)
10816 & ~(mask >> (INTVAL (XEXP (op0, 1))
10817 + ! equality_comparison_p))) == 0)
10819 /* We must perform a logical shift, not an arithmetic one,
10820 as we want the top N bits of C to be zero. */
10821 unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
10823 temp >>= INTVAL (XEXP (op0, 1));
10824 op1 = gen_int_mode (temp, mode);
10825 op0 = XEXP (op0, 0);
10826 continue;
10829 /* If we are doing a sign bit comparison, it means we are testing
10830 a particular bit. Convert it to the appropriate AND. */
10831 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
10832 && mode_width <= HOST_BITS_PER_WIDE_INT)
10834 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10835 ((HOST_WIDE_INT) 1
10836 << (mode_width - 1
10837 - INTVAL (XEXP (op0, 1)))));
10838 code = (code == LT ? NE : EQ);
10839 continue;
10842 /* If this an equality comparison with zero and we are shifting
10843 the low bit to the sign bit, we can convert this to an AND of the
10844 low-order bit. */
10845 if (const_op == 0 && equality_comparison_p
10846 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10847 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1))
10848 == mode_width - 1)
10850 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10851 (HOST_WIDE_INT) 1);
10852 continue;
10854 break;
10856 case ASHIFTRT:
10857 /* If this is an equality comparison with zero, we can do this
10858 as a logical shift, which might be much simpler. */
10859 if (equality_comparison_p && const_op == 0
10860 && GET_CODE (XEXP (op0, 1)) == CONST_INT)
10862 op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
10863 XEXP (op0, 0),
10864 INTVAL (XEXP (op0, 1)));
10865 continue;
10868 /* If OP0 is a sign extension and CODE is not an unsigned comparison,
10869 do the comparison in a narrower mode. */
10870 if (! unsigned_comparison_p
10871 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10872 && GET_CODE (XEXP (op0, 0)) == ASHIFT
10873 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
10874 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
10875 MODE_INT, 1)) != BLKmode
10876 && (((unsigned HOST_WIDE_INT) const_op
10877 + (GET_MODE_MASK (tmode) >> 1) + 1)
10878 <= GET_MODE_MASK (tmode)))
10880 op0 = gen_lowpart (tmode, XEXP (XEXP (op0, 0), 0));
10881 continue;
10884 /* Likewise if OP0 is a PLUS of a sign extension with a
10885 constant, which is usually represented with the PLUS
10886 between the shifts. */
10887 if (! unsigned_comparison_p
10888 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10889 && GET_CODE (XEXP (op0, 0)) == PLUS
10890 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10891 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
10892 && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
10893 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
10894 MODE_INT, 1)) != BLKmode
10895 && (((unsigned HOST_WIDE_INT) const_op
10896 + (GET_MODE_MASK (tmode) >> 1) + 1)
10897 <= GET_MODE_MASK (tmode)))
10899 rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
10900 rtx add_const = XEXP (XEXP (op0, 0), 1);
10901 rtx new_const = simplify_gen_binary (ASHIFTRT, GET_MODE (op0),
10902 add_const, XEXP (op0, 1));
10904 op0 = simplify_gen_binary (PLUS, tmode,
10905 gen_lowpart (tmode, inner),
10906 new_const);
10907 continue;
10910 /* ... fall through ... */
10911 case LSHIFTRT:
10912 /* If we have (compare (xshiftrt FOO N) (const_int C)) and
10913 the low order N bits of FOO are known to be zero, we can do this
10914 by comparing FOO with C shifted left N bits so long as no
10915 overflow occurs. */
10916 if (GET_CODE (XEXP (op0, 1)) == CONST_INT
10917 && INTVAL (XEXP (op0, 1)) >= 0
10918 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
10919 && mode_width <= HOST_BITS_PER_WIDE_INT
10920 && (nonzero_bits (XEXP (op0, 0), mode)
10921 & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0
10922 && (((unsigned HOST_WIDE_INT) const_op
10923 + (GET_CODE (op0) != LSHIFTRT
10924 ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
10925 + 1)
10926 : 0))
10927 <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
10929 /* If the shift was logical, then we must make the condition
10930 unsigned. */
10931 if (GET_CODE (op0) == LSHIFTRT)
10932 code = unsigned_condition (code);
10934 const_op <<= INTVAL (XEXP (op0, 1));
10935 op1 = GEN_INT (const_op);
10936 op0 = XEXP (op0, 0);
10937 continue;
10940 /* If we are using this shift to extract just the sign bit, we
10941 can replace this with an LT or GE comparison. */
10942 if (const_op == 0
10943 && (equality_comparison_p || sign_bit_comparison_p)
10944 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10945 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1))
10946 == mode_width - 1)
10948 op0 = XEXP (op0, 0);
10949 code = (code == NE || code == GT ? LT : GE);
10950 continue;
10952 break;
10954 default:
10955 break;
10958 break;
10961 /* Now make any compound operations involved in this comparison. Then,
10962 check for an outmost SUBREG on OP0 that is not doing anything or is
10963 paradoxical. The latter transformation must only be performed when
10964 it is known that the "extra" bits will be the same in op0 and op1 or
10965 that they don't matter. There are three cases to consider:
10967 1. SUBREG_REG (op0) is a register. In this case the bits are don't
10968 care bits and we can assume they have any convenient value. So
10969 making the transformation is safe.
10971 2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not defined.
10972 In this case the upper bits of op0 are undefined. We should not make
10973 the simplification in that case as we do not know the contents of
10974 those bits.
10976 3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is defined and not
10977 UNKNOWN. In that case we know those bits are zeros or ones. We must
10978 also be sure that they are the same as the upper bits of op1.
10980 We can never remove a SUBREG for a non-equality comparison because
10981 the sign bit is in a different place in the underlying object. */
10983 op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
10984 op1 = make_compound_operation (op1, SET);
10986 if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
10987 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10988 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (op0))) == MODE_INT
10989 && (code == NE || code == EQ))
10991 if (GET_MODE_SIZE (GET_MODE (op0))
10992 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0))))
10994 /* For paradoxical subregs, allow case 1 as above. Case 3 isn't
10995 implemented. */
10996 if (REG_P (SUBREG_REG (op0)))
10998 op0 = SUBREG_REG (op0);
10999 op1 = gen_lowpart (GET_MODE (op0), op1);
11002 else if ((GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
11003 <= HOST_BITS_PER_WIDE_INT)
11004 && (nonzero_bits (SUBREG_REG (op0),
11005 GET_MODE (SUBREG_REG (op0)))
11006 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
11008 tem = gen_lowpart (GET_MODE (SUBREG_REG (op0)), op1);
11010 if ((nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
11011 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
11012 op0 = SUBREG_REG (op0), op1 = tem;
11016 /* We now do the opposite procedure: Some machines don't have compare
11017 insns in all modes. If OP0's mode is an integer mode smaller than a
11018 word and we can't do a compare in that mode, see if there is a larger
11019 mode for which we can do the compare. There are a number of cases in
11020 which we can use the wider mode. */
11022 mode = GET_MODE (op0);
11023 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
11024 && GET_MODE_SIZE (mode) < UNITS_PER_WORD
11025 && ! have_insn_for (COMPARE, mode))
11026 for (tmode = GET_MODE_WIDER_MODE (mode);
11027 (tmode != VOIDmode
11028 && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT);
11029 tmode = GET_MODE_WIDER_MODE (tmode))
11030 if (have_insn_for (COMPARE, tmode))
11032 int zero_extended;
11034 /* If the only nonzero bits in OP0 and OP1 are those in the
11035 narrower mode and this is an equality or unsigned comparison,
11036 we can use the wider mode. Similarly for sign-extended
11037 values, in which case it is true for all comparisons. */
11038 zero_extended = ((code == EQ || code == NE
11039 || code == GEU || code == GTU
11040 || code == LEU || code == LTU)
11041 && (nonzero_bits (op0, tmode)
11042 & ~GET_MODE_MASK (mode)) == 0
11043 && ((GET_CODE (op1) == CONST_INT
11044 || (nonzero_bits (op1, tmode)
11045 & ~GET_MODE_MASK (mode)) == 0)));
11047 if (zero_extended
11048 || ((num_sign_bit_copies (op0, tmode)
11049 > (unsigned int) (GET_MODE_BITSIZE (tmode)
11050 - GET_MODE_BITSIZE (mode)))
11051 && (num_sign_bit_copies (op1, tmode)
11052 > (unsigned int) (GET_MODE_BITSIZE (tmode)
11053 - GET_MODE_BITSIZE (mode)))))
11055 /* If OP0 is an AND and we don't have an AND in MODE either,
11056 make a new AND in the proper mode. */
11057 if (GET_CODE (op0) == AND
11058 && !have_insn_for (AND, mode))
11059 op0 = simplify_gen_binary (AND, tmode,
11060 gen_lowpart (tmode,
11061 XEXP (op0, 0)),
11062 gen_lowpart (tmode,
11063 XEXP (op0, 1)));
11065 op0 = gen_lowpart (tmode, op0);
11066 if (zero_extended && GET_CODE (op1) == CONST_INT)
11067 op1 = GEN_INT (INTVAL (op1) & GET_MODE_MASK (mode));
11068 op1 = gen_lowpart (tmode, op1);
11069 break;
11072 /* If this is a test for negative, we can make an explicit
11073 test of the sign bit. */
11075 if (op1 == const0_rtx && (code == LT || code == GE)
11076 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11078 op0 = simplify_gen_binary (AND, tmode,
11079 gen_lowpart (tmode, op0),
11080 GEN_INT ((HOST_WIDE_INT) 1
11081 << (GET_MODE_BITSIZE (mode)
11082 - 1)));
11083 code = (code == LT) ? NE : EQ;
11084 break;
11088 #ifdef CANONICALIZE_COMPARISON
11089 /* If this machine only supports a subset of valid comparisons, see if we
11090 can convert an unsupported one into a supported one. */
11091 CANONICALIZE_COMPARISON (code, op0, op1);
11092 #endif
11094 *pop0 = op0;
11095 *pop1 = op1;
11097 return code;
11100 /* Utility function for record_value_for_reg. Count number of
11101 rtxs in X. */
11102 static int
11103 count_rtxs (rtx x)
11105 enum rtx_code code = GET_CODE (x);
11106 const char *fmt;
11107 int i, ret = 1;
11109 if (GET_RTX_CLASS (code) == '2'
11110 || GET_RTX_CLASS (code) == 'c')
11112 rtx x0 = XEXP (x, 0);
11113 rtx x1 = XEXP (x, 1);
11115 if (x0 == x1)
11116 return 1 + 2 * count_rtxs (x0);
11118 if ((GET_RTX_CLASS (GET_CODE (x1)) == '2'
11119 || GET_RTX_CLASS (GET_CODE (x1)) == 'c')
11120 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
11121 return 2 + 2 * count_rtxs (x0)
11122 + count_rtxs (x == XEXP (x1, 0)
11123 ? XEXP (x1, 1) : XEXP (x1, 0));
11125 if ((GET_RTX_CLASS (GET_CODE (x0)) == '2'
11126 || GET_RTX_CLASS (GET_CODE (x0)) == 'c')
11127 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
11128 return 2 + 2 * count_rtxs (x1)
11129 + count_rtxs (x == XEXP (x0, 0)
11130 ? XEXP (x0, 1) : XEXP (x0, 0));
11133 fmt = GET_RTX_FORMAT (code);
11134 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11135 if (fmt[i] == 'e')
11136 ret += count_rtxs (XEXP (x, i));
11138 return ret;
11141 /* Utility function for following routine. Called when X is part of a value
11142 being stored into last_set_value. Sets last_set_table_tick
11143 for each register mentioned. Similar to mention_regs in cse.c */
11145 static void
11146 update_table_tick (rtx x)
11148 enum rtx_code code = GET_CODE (x);
11149 const char *fmt = GET_RTX_FORMAT (code);
11150 int i;
11152 if (code == REG)
11154 unsigned int regno = REGNO (x);
11155 unsigned int endregno = END_REGNO (x);
11156 unsigned int r;
11158 for (r = regno; r < endregno; r++)
11159 reg_stat[r].last_set_table_tick = label_tick;
11161 return;
11164 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11165 /* Note that we can't have an "E" in values stored; see
11166 get_last_value_validate. */
11167 if (fmt[i] == 'e')
11169 /* Check for identical subexpressions. If x contains
11170 identical subexpression we only have to traverse one of
11171 them. */
11172 if (i == 0 && ARITHMETIC_P (x))
11174 /* Note that at this point x1 has already been
11175 processed. */
11176 rtx x0 = XEXP (x, 0);
11177 rtx x1 = XEXP (x, 1);
11179 /* If x0 and x1 are identical then there is no need to
11180 process x0. */
11181 if (x0 == x1)
11182 break;
11184 /* If x0 is identical to a subexpression of x1 then while
11185 processing x1, x0 has already been processed. Thus we
11186 are done with x. */
11187 if (ARITHMETIC_P (x1)
11188 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
11189 break;
11191 /* If x1 is identical to a subexpression of x0 then we
11192 still have to process the rest of x0. */
11193 if (ARITHMETIC_P (x0)
11194 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
11196 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
11197 break;
11201 update_table_tick (XEXP (x, i));
11205 /* Record that REG is set to VALUE in insn INSN. If VALUE is zero, we
11206 are saying that the register is clobbered and we no longer know its
11207 value. If INSN is zero, don't update reg_stat[].last_set; this is
11208 only permitted with VALUE also zero and is used to invalidate the
11209 register. */
11211 static void
11212 record_value_for_reg (rtx reg, rtx insn, rtx value)
11214 unsigned int regno = REGNO (reg);
11215 unsigned int endregno = END_REGNO (reg);
11216 unsigned int i;
11218 /* If VALUE contains REG and we have a previous value for REG, substitute
11219 the previous value. */
11220 if (value && insn && reg_overlap_mentioned_p (reg, value))
11222 rtx tem;
11224 /* Set things up so get_last_value is allowed to see anything set up to
11225 our insn. */
11226 subst_low_luid = DF_INSN_LUID (insn);
11227 tem = get_last_value (reg);
11229 /* If TEM is simply a binary operation with two CLOBBERs as operands,
11230 it isn't going to be useful and will take a lot of time to process,
11231 so just use the CLOBBER. */
11233 if (tem)
11235 if (ARITHMETIC_P (tem)
11236 && GET_CODE (XEXP (tem, 0)) == CLOBBER
11237 && GET_CODE (XEXP (tem, 1)) == CLOBBER)
11238 tem = XEXP (tem, 0);
11239 else if (count_occurrences (value, reg, 1) >= 2)
11241 /* If there are two or more occurrences of REG in VALUE,
11242 prevent the value from growing too much. */
11243 if (count_rtxs (tem) > MAX_LAST_VALUE_RTL)
11244 tem = gen_rtx_CLOBBER (GET_MODE (tem), const0_rtx);
11247 value = replace_rtx (copy_rtx (value), reg, tem);
11251 /* For each register modified, show we don't know its value, that
11252 we don't know about its bitwise content, that its value has been
11253 updated, and that we don't know the location of the death of the
11254 register. */
11255 for (i = regno; i < endregno; i++)
11257 if (insn)
11258 reg_stat[i].last_set = insn;
11260 reg_stat[i].last_set_value = 0;
11261 reg_stat[i].last_set_mode = 0;
11262 reg_stat[i].last_set_nonzero_bits = 0;
11263 reg_stat[i].last_set_sign_bit_copies = 0;
11264 reg_stat[i].last_death = 0;
11265 reg_stat[i].truncated_to_mode = 0;
11268 /* Mark registers that are being referenced in this value. */
11269 if (value)
11270 update_table_tick (value);
11272 /* Now update the status of each register being set.
11273 If someone is using this register in this block, set this register
11274 to invalid since we will get confused between the two lives in this
11275 basic block. This makes using this register always invalid. In cse, we
11276 scan the table to invalidate all entries using this register, but this
11277 is too much work for us. */
11279 for (i = regno; i < endregno; i++)
11281 reg_stat[i].last_set_label = label_tick;
11282 if (!insn
11283 || (value && reg_stat[i].last_set_table_tick >= label_tick_ebb_start))
11284 reg_stat[i].last_set_invalid = 1;
11285 else
11286 reg_stat[i].last_set_invalid = 0;
11289 /* The value being assigned might refer to X (like in "x++;"). In that
11290 case, we must replace it with (clobber (const_int 0)) to prevent
11291 infinite loops. */
11292 if (value && ! get_last_value_validate (&value, insn,
11293 reg_stat[regno].last_set_label, 0))
11295 value = copy_rtx (value);
11296 if (! get_last_value_validate (&value, insn,
11297 reg_stat[regno].last_set_label, 1))
11298 value = 0;
11301 /* For the main register being modified, update the value, the mode, the
11302 nonzero bits, and the number of sign bit copies. */
11304 reg_stat[regno].last_set_value = value;
11306 if (value)
11308 enum machine_mode mode = GET_MODE (reg);
11309 subst_low_luid = DF_INSN_LUID (insn);
11310 reg_stat[regno].last_set_mode = mode;
11311 if (GET_MODE_CLASS (mode) == MODE_INT
11312 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11313 mode = nonzero_bits_mode;
11314 reg_stat[regno].last_set_nonzero_bits = nonzero_bits (value, mode);
11315 reg_stat[regno].last_set_sign_bit_copies
11316 = num_sign_bit_copies (value, GET_MODE (reg));
11320 /* Called via note_stores from record_dead_and_set_regs to handle one
11321 SET or CLOBBER in an insn. DATA is the instruction in which the
11322 set is occurring. */
11324 static void
11325 record_dead_and_set_regs_1 (rtx dest, rtx setter, void *data)
11327 rtx record_dead_insn = (rtx) data;
11329 if (GET_CODE (dest) == SUBREG)
11330 dest = SUBREG_REG (dest);
11332 if (!record_dead_insn)
11334 if (REG_P (dest))
11335 record_value_for_reg (dest, NULL_RTX, NULL_RTX);
11336 return;
11339 if (REG_P (dest))
11341 /* If we are setting the whole register, we know its value. Otherwise
11342 show that we don't know the value. We can handle SUBREG in
11343 some cases. */
11344 if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
11345 record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
11346 else if (GET_CODE (setter) == SET
11347 && GET_CODE (SET_DEST (setter)) == SUBREG
11348 && SUBREG_REG (SET_DEST (setter)) == dest
11349 && GET_MODE_BITSIZE (GET_MODE (dest)) <= BITS_PER_WORD
11350 && subreg_lowpart_p (SET_DEST (setter)))
11351 record_value_for_reg (dest, record_dead_insn,
11352 gen_lowpart (GET_MODE (dest),
11353 SET_SRC (setter)));
11354 else
11355 record_value_for_reg (dest, record_dead_insn, NULL_RTX);
11357 else if (MEM_P (dest)
11358 /* Ignore pushes, they clobber nothing. */
11359 && ! push_operand (dest, GET_MODE (dest)))
11360 mem_last_set = DF_INSN_LUID (record_dead_insn);
11363 /* Update the records of when each REG was most recently set or killed
11364 for the things done by INSN. This is the last thing done in processing
11365 INSN in the combiner loop.
11367 We update reg_stat[], in particular fields last_set, last_set_value,
11368 last_set_mode, last_set_nonzero_bits, last_set_sign_bit_copies,
11369 last_death, and also the similar information mem_last_set (which insn
11370 most recently modified memory) and last_call_luid (which insn was the
11371 most recent subroutine call). */
11373 static void
11374 record_dead_and_set_regs (rtx insn)
11376 rtx link;
11377 unsigned int i;
11379 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
11381 if (REG_NOTE_KIND (link) == REG_DEAD
11382 && REG_P (XEXP (link, 0)))
11384 unsigned int regno = REGNO (XEXP (link, 0));
11385 unsigned int endregno = END_REGNO (XEXP (link, 0));
11387 for (i = regno; i < endregno; i++)
11388 reg_stat[i].last_death = insn;
11390 else if (REG_NOTE_KIND (link) == REG_INC)
11391 record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
11394 if (CALL_P (insn))
11396 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
11397 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
11399 reg_stat[i].last_set_invalid = 1;
11400 reg_stat[i].last_set = insn;
11401 reg_stat[i].last_set_value = 0;
11402 reg_stat[i].last_set_mode = 0;
11403 reg_stat[i].last_set_nonzero_bits = 0;
11404 reg_stat[i].last_set_sign_bit_copies = 0;
11405 reg_stat[i].last_death = 0;
11406 reg_stat[i].truncated_to_mode = 0;
11409 last_call_luid = mem_last_set = DF_INSN_LUID (insn);
11411 /* We can't combine into a call pattern. Remember, though, that
11412 the return value register is set at this LUID. We could
11413 still replace a register with the return value from the
11414 wrong subroutine call! */
11415 note_stores (PATTERN (insn), record_dead_and_set_regs_1, NULL_RTX);
11417 else
11418 note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
11421 /* If a SUBREG has the promoted bit set, it is in fact a property of the
11422 register present in the SUBREG, so for each such SUBREG go back and
11423 adjust nonzero and sign bit information of the registers that are
11424 known to have some zero/sign bits set.
11426 This is needed because when combine blows the SUBREGs away, the
11427 information on zero/sign bits is lost and further combines can be
11428 missed because of that. */
11430 static void
11431 record_promoted_value (rtx insn, rtx subreg)
11433 rtx links, set;
11434 unsigned int regno = REGNO (SUBREG_REG (subreg));
11435 enum machine_mode mode = GET_MODE (subreg);
11437 if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT)
11438 return;
11440 for (links = LOG_LINKS (insn); links;)
11442 insn = XEXP (links, 0);
11443 set = single_set (insn);
11445 if (! set || !REG_P (SET_DEST (set))
11446 || REGNO (SET_DEST (set)) != regno
11447 || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
11449 links = XEXP (links, 1);
11450 continue;
11453 if (reg_stat[regno].last_set == insn)
11455 if (SUBREG_PROMOTED_UNSIGNED_P (subreg) > 0)
11456 reg_stat[regno].last_set_nonzero_bits &= GET_MODE_MASK (mode);
11459 if (REG_P (SET_SRC (set)))
11461 regno = REGNO (SET_SRC (set));
11462 links = LOG_LINKS (insn);
11464 else
11465 break;
11469 /* Check if X, a register, is known to contain a value already
11470 truncated to MODE. In this case we can use a subreg to refer to
11471 the truncated value even though in the generic case we would need
11472 an explicit truncation. */
11474 static bool
11475 reg_truncated_to_mode (enum machine_mode mode, rtx x)
11477 enum machine_mode truncated = reg_stat[REGNO (x)].truncated_to_mode;
11479 if (truncated == 0
11480 || reg_stat[REGNO (x)].truncation_label < label_tick_ebb_start)
11481 return false;
11482 if (GET_MODE_SIZE (truncated) <= GET_MODE_SIZE (mode))
11483 return true;
11484 if (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
11485 GET_MODE_BITSIZE (truncated)))
11486 return true;
11487 return false;
11490 /* X is a REG or a SUBREG. If X is some sort of a truncation record
11491 it. For non-TRULY_NOOP_TRUNCATION targets we might be able to turn
11492 a truncate into a subreg using this information. */
11494 static void
11495 record_truncated_value (rtx x)
11497 enum machine_mode truncated_mode;
11499 if (GET_CODE (x) == SUBREG && REG_P (SUBREG_REG (x)))
11501 enum machine_mode original_mode = GET_MODE (SUBREG_REG (x));
11502 truncated_mode = GET_MODE (x);
11504 if (GET_MODE_SIZE (original_mode) <= GET_MODE_SIZE (truncated_mode))
11505 return;
11507 if (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (truncated_mode),
11508 GET_MODE_BITSIZE (original_mode)))
11509 return;
11511 x = SUBREG_REG (x);
11513 /* ??? For hard-regs we now record everything. We might be able to
11514 optimize this using last_set_mode. */
11515 else if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
11516 truncated_mode = GET_MODE (x);
11517 else
11518 return;
11520 if (reg_stat[REGNO (x)].truncated_to_mode == 0
11521 || reg_stat[REGNO (x)].truncation_label < label_tick_ebb_start
11522 || (GET_MODE_SIZE (truncated_mode)
11523 < GET_MODE_SIZE (reg_stat[REGNO (x)].truncated_to_mode)))
11525 reg_stat[REGNO (x)].truncated_to_mode = truncated_mode;
11526 reg_stat[REGNO (x)].truncation_label = label_tick;
11530 /* Scan X for promoted SUBREGs and truncated REGs. For each one
11531 found, note what it implies to the registers used in it. */
11533 static void
11534 check_conversions (rtx insn, rtx x)
11536 if (GET_CODE (x) == SUBREG || REG_P (x))
11538 if (GET_CODE (x) == SUBREG
11539 && SUBREG_PROMOTED_VAR_P (x)
11540 && REG_P (SUBREG_REG (x)))
11541 record_promoted_value (insn, x);
11543 record_truncated_value (x);
11545 else
11547 const char *format = GET_RTX_FORMAT (GET_CODE (x));
11548 int i, j;
11550 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
11551 switch (format[i])
11553 case 'e':
11554 check_conversions (insn, XEXP (x, i));
11555 break;
11556 case 'V':
11557 case 'E':
11558 if (XVEC (x, i) != 0)
11559 for (j = 0; j < XVECLEN (x, i); j++)
11560 check_conversions (insn, XVECEXP (x, i, j));
11561 break;
11566 /* Utility routine for the following function. Verify that all the registers
11567 mentioned in *LOC are valid when *LOC was part of a value set when
11568 label_tick == TICK. Return 0 if some are not.
11570 If REPLACE is nonzero, replace the invalid reference with
11571 (clobber (const_int 0)) and return 1. This replacement is useful because
11572 we often can get useful information about the form of a value (e.g., if
11573 it was produced by a shift that always produces -1 or 0) even though
11574 we don't know exactly what registers it was produced from. */
11576 static int
11577 get_last_value_validate (rtx *loc, rtx insn, int tick, int replace)
11579 rtx x = *loc;
11580 const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
11581 int len = GET_RTX_LENGTH (GET_CODE (x));
11582 int i;
11584 if (REG_P (x))
11586 unsigned int regno = REGNO (x);
11587 unsigned int endregno = END_REGNO (x);
11588 unsigned int j;
11590 for (j = regno; j < endregno; j++)
11591 if (reg_stat[j].last_set_invalid
11592 /* If this is a pseudo-register that was only set once and not
11593 live at the beginning of the function, it is always valid. */
11594 || (! (regno >= FIRST_PSEUDO_REGISTER
11595 && REG_N_SETS (regno) == 1
11596 && !REGNO_REG_SET_P
11597 (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), regno))
11598 && reg_stat[j].last_set_label > tick))
11600 if (replace)
11601 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
11602 return replace;
11605 return 1;
11607 /* If this is a memory reference, make sure that there were
11608 no stores after it that might have clobbered the value. We don't
11609 have alias info, so we assume any store invalidates it. */
11610 else if (MEM_P (x) && !MEM_READONLY_P (x)
11611 && DF_INSN_LUID (insn) <= mem_last_set)
11613 if (replace)
11614 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
11615 return replace;
11618 for (i = 0; i < len; i++)
11620 if (fmt[i] == 'e')
11622 /* Check for identical subexpressions. If x contains
11623 identical subexpression we only have to traverse one of
11624 them. */
11625 if (i == 1 && ARITHMETIC_P (x))
11627 /* Note that at this point x0 has already been checked
11628 and found valid. */
11629 rtx x0 = XEXP (x, 0);
11630 rtx x1 = XEXP (x, 1);
11632 /* If x0 and x1 are identical then x is also valid. */
11633 if (x0 == x1)
11634 return 1;
11636 /* If x1 is identical to a subexpression of x0 then
11637 while checking x0, x1 has already been checked. Thus
11638 it is valid and so as x. */
11639 if (ARITHMETIC_P (x0)
11640 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
11641 return 1;
11643 /* If x0 is identical to a subexpression of x1 then x is
11644 valid iff the rest of x1 is valid. */
11645 if (ARITHMETIC_P (x1)
11646 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
11647 return
11648 get_last_value_validate (&XEXP (x1,
11649 x0 == XEXP (x1, 0) ? 1 : 0),
11650 insn, tick, replace);
11653 if (get_last_value_validate (&XEXP (x, i), insn, tick,
11654 replace) == 0)
11655 return 0;
11657 /* Don't bother with these. They shouldn't occur anyway. */
11658 else if (fmt[i] == 'E')
11659 return 0;
11662 /* If we haven't found a reason for it to be invalid, it is valid. */
11663 return 1;
11666 /* Get the last value assigned to X, if known. Some registers
11667 in the value may be replaced with (clobber (const_int 0)) if their value
11668 is known longer known reliably. */
11670 static rtx
11671 get_last_value (rtx x)
11673 unsigned int regno;
11674 rtx value;
11676 /* If this is a non-paradoxical SUBREG, get the value of its operand and
11677 then convert it to the desired mode. If this is a paradoxical SUBREG,
11678 we cannot predict what values the "extra" bits might have. */
11679 if (GET_CODE (x) == SUBREG
11680 && subreg_lowpart_p (x)
11681 && (GET_MODE_SIZE (GET_MODE (x))
11682 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
11683 && (value = get_last_value (SUBREG_REG (x))) != 0)
11684 return gen_lowpart (GET_MODE (x), value);
11686 if (!REG_P (x))
11687 return 0;
11689 regno = REGNO (x);
11690 value = reg_stat[regno].last_set_value;
11692 /* If we don't have a value, or if it isn't for this basic block and
11693 it's either a hard register, set more than once, or it's a live
11694 at the beginning of the function, return 0.
11696 Because if it's not live at the beginning of the function then the reg
11697 is always set before being used (is never used without being set).
11698 And, if it's set only once, and it's always set before use, then all
11699 uses must have the same last value, even if it's not from this basic
11700 block. */
11702 if (value == 0
11703 || (reg_stat[regno].last_set_label < label_tick_ebb_start
11704 && (regno < FIRST_PSEUDO_REGISTER
11705 || REG_N_SETS (regno) != 1
11706 || REGNO_REG_SET_P
11707 (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), regno))))
11708 return 0;
11710 /* If the value was set in a later insn than the ones we are processing,
11711 we can't use it even if the register was only set once. */
11712 if (reg_stat[regno].last_set_label == label_tick
11713 && DF_INSN_LUID (reg_stat[regno].last_set) >= subst_low_luid)
11714 return 0;
11716 /* If the value has all its registers valid, return it. */
11717 if (get_last_value_validate (&value, reg_stat[regno].last_set,
11718 reg_stat[regno].last_set_label, 0))
11719 return value;
11721 /* Otherwise, make a copy and replace any invalid register with
11722 (clobber (const_int 0)). If that fails for some reason, return 0. */
11724 value = copy_rtx (value);
11725 if (get_last_value_validate (&value, reg_stat[regno].last_set,
11726 reg_stat[regno].last_set_label, 1))
11727 return value;
11729 return 0;
11732 /* Return nonzero if expression X refers to a REG or to memory
11733 that is set in an instruction more recent than FROM_LUID. */
11735 static int
11736 use_crosses_set_p (rtx x, int from_luid)
11738 const char *fmt;
11739 int i;
11740 enum rtx_code code = GET_CODE (x);
11742 if (code == REG)
11744 unsigned int regno = REGNO (x);
11745 unsigned endreg = END_REGNO (x);
11747 #ifdef PUSH_ROUNDING
11748 /* Don't allow uses of the stack pointer to be moved,
11749 because we don't know whether the move crosses a push insn. */
11750 if (regno == STACK_POINTER_REGNUM && PUSH_ARGS)
11751 return 1;
11752 #endif
11753 for (; regno < endreg; regno++)
11754 if (reg_stat[regno].last_set
11755 && reg_stat[regno].last_set_label == label_tick
11756 && DF_INSN_LUID (reg_stat[regno].last_set) > from_luid)
11757 return 1;
11758 return 0;
11761 if (code == MEM && mem_last_set > from_luid)
11762 return 1;
11764 fmt = GET_RTX_FORMAT (code);
11766 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11768 if (fmt[i] == 'E')
11770 int j;
11771 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
11772 if (use_crosses_set_p (XVECEXP (x, i, j), from_luid))
11773 return 1;
11775 else if (fmt[i] == 'e'
11776 && use_crosses_set_p (XEXP (x, i), from_luid))
11777 return 1;
11779 return 0;
11782 /* Define three variables used for communication between the following
11783 routines. */
11785 static unsigned int reg_dead_regno, reg_dead_endregno;
11786 static int reg_dead_flag;
11788 /* Function called via note_stores from reg_dead_at_p.
11790 If DEST is within [reg_dead_regno, reg_dead_endregno), set
11791 reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET. */
11793 static void
11794 reg_dead_at_p_1 (rtx dest, rtx x, void *data ATTRIBUTE_UNUSED)
11796 unsigned int regno, endregno;
11798 if (!REG_P (dest))
11799 return;
11801 regno = REGNO (dest);
11802 endregno = END_REGNO (dest);
11803 if (reg_dead_endregno > regno && reg_dead_regno < endregno)
11804 reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
11807 /* Return nonzero if REG is known to be dead at INSN.
11809 We scan backwards from INSN. If we hit a REG_DEAD note or a CLOBBER
11810 referencing REG, it is dead. If we hit a SET referencing REG, it is
11811 live. Otherwise, see if it is live or dead at the start of the basic
11812 block we are in. Hard regs marked as being live in NEWPAT_USED_REGS
11813 must be assumed to be always live. */
11815 static int
11816 reg_dead_at_p (rtx reg, rtx insn)
11818 basic_block block;
11819 unsigned int i;
11821 /* Set variables for reg_dead_at_p_1. */
11822 reg_dead_regno = REGNO (reg);
11823 reg_dead_endregno = END_REGNO (reg);
11825 reg_dead_flag = 0;
11827 /* Check that reg isn't mentioned in NEWPAT_USED_REGS. For fixed registers
11828 we allow the machine description to decide whether use-and-clobber
11829 patterns are OK. */
11830 if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
11832 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
11833 if (!fixed_regs[i] && TEST_HARD_REG_BIT (newpat_used_regs, i))
11834 return 0;
11837 /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, label, or
11838 beginning of function. */
11839 for (; insn && !LABEL_P (insn) && !BARRIER_P (insn);
11840 insn = prev_nonnote_insn (insn))
11842 note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
11843 if (reg_dead_flag)
11844 return reg_dead_flag == 1 ? 1 : 0;
11846 if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
11847 return 1;
11850 /* Get the basic block that we were in. */
11851 if (insn == 0)
11852 block = ENTRY_BLOCK_PTR->next_bb;
11853 else
11855 FOR_EACH_BB (block)
11856 if (insn == BB_HEAD (block))
11857 break;
11859 if (block == EXIT_BLOCK_PTR)
11860 return 0;
11863 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
11864 if (REGNO_REG_SET_P (df_get_live_in (block), i))
11865 return 0;
11867 return 1;
11870 /* Note hard registers in X that are used. */
11872 static void
11873 mark_used_regs_combine (rtx x)
11875 RTX_CODE code = GET_CODE (x);
11876 unsigned int regno;
11877 int i;
11879 switch (code)
11881 case LABEL_REF:
11882 case SYMBOL_REF:
11883 case CONST_INT:
11884 case CONST:
11885 case CONST_DOUBLE:
11886 case CONST_VECTOR:
11887 case PC:
11888 case ADDR_VEC:
11889 case ADDR_DIFF_VEC:
11890 case ASM_INPUT:
11891 #ifdef HAVE_cc0
11892 /* CC0 must die in the insn after it is set, so we don't need to take
11893 special note of it here. */
11894 case CC0:
11895 #endif
11896 return;
11898 case CLOBBER:
11899 /* If we are clobbering a MEM, mark any hard registers inside the
11900 address as used. */
11901 if (MEM_P (XEXP (x, 0)))
11902 mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
11903 return;
11905 case REG:
11906 regno = REGNO (x);
11907 /* A hard reg in a wide mode may really be multiple registers.
11908 If so, mark all of them just like the first. */
11909 if (regno < FIRST_PSEUDO_REGISTER)
11911 /* None of this applies to the stack, frame or arg pointers. */
11912 if (regno == STACK_POINTER_REGNUM
11913 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
11914 || regno == HARD_FRAME_POINTER_REGNUM
11915 #endif
11916 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
11917 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
11918 #endif
11919 || regno == FRAME_POINTER_REGNUM)
11920 return;
11922 add_to_hard_reg_set (&newpat_used_regs, GET_MODE (x), regno);
11924 return;
11926 case SET:
11928 /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
11929 the address. */
11930 rtx testreg = SET_DEST (x);
11932 while (GET_CODE (testreg) == SUBREG
11933 || GET_CODE (testreg) == ZERO_EXTRACT
11934 || GET_CODE (testreg) == STRICT_LOW_PART)
11935 testreg = XEXP (testreg, 0);
11937 if (MEM_P (testreg))
11938 mark_used_regs_combine (XEXP (testreg, 0));
11940 mark_used_regs_combine (SET_SRC (x));
11942 return;
11944 default:
11945 break;
11948 /* Recursively scan the operands of this expression. */
11951 const char *fmt = GET_RTX_FORMAT (code);
11953 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11955 if (fmt[i] == 'e')
11956 mark_used_regs_combine (XEXP (x, i));
11957 else if (fmt[i] == 'E')
11959 int j;
11961 for (j = 0; j < XVECLEN (x, i); j++)
11962 mark_used_regs_combine (XVECEXP (x, i, j));
11968 /* Remove register number REGNO from the dead registers list of INSN.
11970 Return the note used to record the death, if there was one. */
11973 remove_death (unsigned int regno, rtx insn)
11975 rtx note = find_regno_note (insn, REG_DEAD, regno);
11977 if (note)
11978 remove_note (insn, note);
11980 return note;
11983 /* For each register (hardware or pseudo) used within expression X, if its
11984 death is in an instruction with luid between FROM_LUID (inclusive) and
11985 TO_INSN (exclusive), put a REG_DEAD note for that register in the
11986 list headed by PNOTES.
11988 That said, don't move registers killed by maybe_kill_insn.
11990 This is done when X is being merged by combination into TO_INSN. These
11991 notes will then be distributed as needed. */
11993 static void
11994 move_deaths (rtx x, rtx maybe_kill_insn, int from_luid, rtx to_insn,
11995 rtx *pnotes)
11997 const char *fmt;
11998 int len, i;
11999 enum rtx_code code = GET_CODE (x);
12001 if (code == REG)
12003 unsigned int regno = REGNO (x);
12004 rtx where_dead = reg_stat[regno].last_death;
12006 /* Don't move the register if it gets killed in between from and to. */
12007 if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
12008 && ! reg_referenced_p (x, maybe_kill_insn))
12009 return;
12011 if (where_dead
12012 && DF_INSN_LUID (where_dead) >= from_luid
12013 && DF_INSN_LUID (where_dead) < DF_INSN_LUID (to_insn))
12015 rtx note = remove_death (regno, where_dead);
12017 /* It is possible for the call above to return 0. This can occur
12018 when last_death points to I2 or I1 that we combined with.
12019 In that case make a new note.
12021 We must also check for the case where X is a hard register
12022 and NOTE is a death note for a range of hard registers
12023 including X. In that case, we must put REG_DEAD notes for
12024 the remaining registers in place of NOTE. */
12026 if (note != 0 && regno < FIRST_PSEUDO_REGISTER
12027 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
12028 > GET_MODE_SIZE (GET_MODE (x))))
12030 unsigned int deadregno = REGNO (XEXP (note, 0));
12031 unsigned int deadend = END_HARD_REGNO (XEXP (note, 0));
12032 unsigned int ourend = END_HARD_REGNO (x);
12033 unsigned int i;
12035 for (i = deadregno; i < deadend; i++)
12036 if (i < regno || i >= ourend)
12037 REG_NOTES (where_dead)
12038 = gen_rtx_EXPR_LIST (REG_DEAD,
12039 regno_reg_rtx[i],
12040 REG_NOTES (where_dead));
12043 /* If we didn't find any note, or if we found a REG_DEAD note that
12044 covers only part of the given reg, and we have a multi-reg hard
12045 register, then to be safe we must check for REG_DEAD notes
12046 for each register other than the first. They could have
12047 their own REG_DEAD notes lying around. */
12048 else if ((note == 0
12049 || (note != 0
12050 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
12051 < GET_MODE_SIZE (GET_MODE (x)))))
12052 && regno < FIRST_PSEUDO_REGISTER
12053 && hard_regno_nregs[regno][GET_MODE (x)] > 1)
12055 unsigned int ourend = END_HARD_REGNO (x);
12056 unsigned int i, offset;
12057 rtx oldnotes = 0;
12059 if (note)
12060 offset = hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))];
12061 else
12062 offset = 1;
12064 for (i = regno + offset; i < ourend; i++)
12065 move_deaths (regno_reg_rtx[i],
12066 maybe_kill_insn, from_luid, to_insn, &oldnotes);
12069 if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
12071 XEXP (note, 1) = *pnotes;
12072 *pnotes = note;
12074 else
12075 *pnotes = gen_rtx_EXPR_LIST (REG_DEAD, x, *pnotes);
12078 return;
12081 else if (GET_CODE (x) == SET)
12083 rtx dest = SET_DEST (x);
12085 move_deaths (SET_SRC (x), maybe_kill_insn, from_luid, to_insn, pnotes);
12087 /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
12088 that accesses one word of a multi-word item, some
12089 piece of everything register in the expression is used by
12090 this insn, so remove any old death. */
12091 /* ??? So why do we test for equality of the sizes? */
12093 if (GET_CODE (dest) == ZERO_EXTRACT
12094 || GET_CODE (dest) == STRICT_LOW_PART
12095 || (GET_CODE (dest) == SUBREG
12096 && (((GET_MODE_SIZE (GET_MODE (dest))
12097 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
12098 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
12099 + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
12101 move_deaths (dest, maybe_kill_insn, from_luid, to_insn, pnotes);
12102 return;
12105 /* If this is some other SUBREG, we know it replaces the entire
12106 value, so use that as the destination. */
12107 if (GET_CODE (dest) == SUBREG)
12108 dest = SUBREG_REG (dest);
12110 /* If this is a MEM, adjust deaths of anything used in the address.
12111 For a REG (the only other possibility), the entire value is
12112 being replaced so the old value is not used in this insn. */
12114 if (MEM_P (dest))
12115 move_deaths (XEXP (dest, 0), maybe_kill_insn, from_luid,
12116 to_insn, pnotes);
12117 return;
12120 else if (GET_CODE (x) == CLOBBER)
12121 return;
12123 len = GET_RTX_LENGTH (code);
12124 fmt = GET_RTX_FORMAT (code);
12126 for (i = 0; i < len; i++)
12128 if (fmt[i] == 'E')
12130 int j;
12131 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
12132 move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_luid,
12133 to_insn, pnotes);
12135 else if (fmt[i] == 'e')
12136 move_deaths (XEXP (x, i), maybe_kill_insn, from_luid, to_insn, pnotes);
12140 /* Return 1 if X is the target of a bit-field assignment in BODY, the
12141 pattern of an insn. X must be a REG. */
12143 static int
12144 reg_bitfield_target_p (rtx x, rtx body)
12146 int i;
12148 if (GET_CODE (body) == SET)
12150 rtx dest = SET_DEST (body);
12151 rtx target;
12152 unsigned int regno, tregno, endregno, endtregno;
12154 if (GET_CODE (dest) == ZERO_EXTRACT)
12155 target = XEXP (dest, 0);
12156 else if (GET_CODE (dest) == STRICT_LOW_PART)
12157 target = SUBREG_REG (XEXP (dest, 0));
12158 else
12159 return 0;
12161 if (GET_CODE (target) == SUBREG)
12162 target = SUBREG_REG (target);
12164 if (!REG_P (target))
12165 return 0;
12167 tregno = REGNO (target), regno = REGNO (x);
12168 if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
12169 return target == x;
12171 endtregno = end_hard_regno (GET_MODE (target), tregno);
12172 endregno = end_hard_regno (GET_MODE (x), regno);
12174 return endregno > tregno && regno < endtregno;
12177 else if (GET_CODE (body) == PARALLEL)
12178 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
12179 if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
12180 return 1;
12182 return 0;
12185 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
12186 as appropriate. I3 and I2 are the insns resulting from the combination
12187 insns including FROM (I2 may be zero).
12189 ELIM_I2 and ELIM_I1 are either zero or registers that we know will
12190 not need REG_DEAD notes because they are being substituted for. This
12191 saves searching in the most common cases.
12193 Each note in the list is either ignored or placed on some insns, depending
12194 on the type of note. */
12196 static void
12197 distribute_notes (rtx notes, rtx from_insn, rtx i3, rtx i2, rtx elim_i2,
12198 rtx elim_i1)
12200 rtx note, next_note;
12201 rtx tem;
12203 for (note = notes; note; note = next_note)
12205 rtx place = 0, place2 = 0;
12207 next_note = XEXP (note, 1);
12208 switch (REG_NOTE_KIND (note))
12210 case REG_BR_PROB:
12211 case REG_BR_PRED:
12212 /* Doesn't matter much where we put this, as long as it's somewhere.
12213 It is preferable to keep these notes on branches, which is most
12214 likely to be i3. */
12215 place = i3;
12216 break;
12218 case REG_VALUE_PROFILE:
12219 /* Just get rid of this note, as it is unused later anyway. */
12220 break;
12222 case REG_NON_LOCAL_GOTO:
12223 if (JUMP_P (i3))
12224 place = i3;
12225 else
12227 gcc_assert (i2 && JUMP_P (i2));
12228 place = i2;
12230 break;
12232 case REG_EH_REGION:
12233 /* These notes must remain with the call or trapping instruction. */
12234 if (CALL_P (i3))
12235 place = i3;
12236 else if (i2 && CALL_P (i2))
12237 place = i2;
12238 else
12240 gcc_assert (flag_non_call_exceptions);
12241 if (may_trap_p (i3))
12242 place = i3;
12243 else if (i2 && may_trap_p (i2))
12244 place = i2;
12245 /* ??? Otherwise assume we've combined things such that we
12246 can now prove that the instructions can't trap. Drop the
12247 note in this case. */
12249 break;
12251 case REG_NORETURN:
12252 case REG_SETJMP:
12253 /* These notes must remain with the call. It should not be
12254 possible for both I2 and I3 to be a call. */
12255 if (CALL_P (i3))
12256 place = i3;
12257 else
12259 gcc_assert (i2 && CALL_P (i2));
12260 place = i2;
12262 break;
12264 case REG_UNUSED:
12265 /* Any clobbers for i3 may still exist, and so we must process
12266 REG_UNUSED notes from that insn.
12268 Any clobbers from i2 or i1 can only exist if they were added by
12269 recog_for_combine. In that case, recog_for_combine created the
12270 necessary REG_UNUSED notes. Trying to keep any original
12271 REG_UNUSED notes from these insns can cause incorrect output
12272 if it is for the same register as the original i3 dest.
12273 In that case, we will notice that the register is set in i3,
12274 and then add a REG_UNUSED note for the destination of i3, which
12275 is wrong. However, it is possible to have REG_UNUSED notes from
12276 i2 or i1 for register which were both used and clobbered, so
12277 we keep notes from i2 or i1 if they will turn into REG_DEAD
12278 notes. */
12280 /* If this register is set or clobbered in I3, put the note there
12281 unless there is one already. */
12282 if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
12284 if (from_insn != i3)
12285 break;
12287 if (! (REG_P (XEXP (note, 0))
12288 ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
12289 : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
12290 place = i3;
12292 /* Otherwise, if this register is used by I3, then this register
12293 now dies here, so we must put a REG_DEAD note here unless there
12294 is one already. */
12295 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
12296 && ! (REG_P (XEXP (note, 0))
12297 ? find_regno_note (i3, REG_DEAD,
12298 REGNO (XEXP (note, 0)))
12299 : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
12301 PUT_REG_NOTE_KIND (note, REG_DEAD);
12302 place = i3;
12304 break;
12306 case REG_EQUAL:
12307 case REG_EQUIV:
12308 case REG_NOALIAS:
12309 /* These notes say something about results of an insn. We can
12310 only support them if they used to be on I3 in which case they
12311 remain on I3. Otherwise they are ignored.
12313 If the note refers to an expression that is not a constant, we
12314 must also ignore the note since we cannot tell whether the
12315 equivalence is still true. It might be possible to do
12316 slightly better than this (we only have a problem if I2DEST
12317 or I1DEST is present in the expression), but it doesn't
12318 seem worth the trouble. */
12320 if (from_insn == i3
12321 && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
12322 place = i3;
12323 break;
12325 case REG_INC:
12326 case REG_NO_CONFLICT:
12327 /* These notes say something about how a register is used. They must
12328 be present on any use of the register in I2 or I3. */
12329 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
12330 place = i3;
12332 if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
12334 if (place)
12335 place2 = i2;
12336 else
12337 place = i2;
12339 break;
12341 case REG_LABEL:
12342 /* This can show up in several ways -- either directly in the
12343 pattern, or hidden off in the constant pool with (or without?)
12344 a REG_EQUAL note. */
12345 /* ??? Ignore the without-reg_equal-note problem for now. */
12346 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
12347 || ((tem = find_reg_note (i3, REG_EQUAL, NULL_RTX))
12348 && GET_CODE (XEXP (tem, 0)) == LABEL_REF
12349 && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0)))
12350 place = i3;
12352 if (i2
12353 && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
12354 || ((tem = find_reg_note (i2, REG_EQUAL, NULL_RTX))
12355 && GET_CODE (XEXP (tem, 0)) == LABEL_REF
12356 && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0))))
12358 if (place)
12359 place2 = i2;
12360 else
12361 place = i2;
12364 /* Don't attach REG_LABEL note to a JUMP_INSN. Add
12365 a JUMP_LABEL instead or decrement LABEL_NUSES. */
12366 if (place && JUMP_P (place))
12368 rtx label = JUMP_LABEL (place);
12370 if (!label)
12371 JUMP_LABEL (place) = XEXP (note, 0);
12372 else
12374 gcc_assert (label == XEXP (note, 0));
12375 if (LABEL_P (label))
12376 LABEL_NUSES (label)--;
12378 place = 0;
12380 if (place2 && JUMP_P (place2))
12382 rtx label = JUMP_LABEL (place2);
12384 if (!label)
12385 JUMP_LABEL (place2) = XEXP (note, 0);
12386 else
12388 gcc_assert (label == XEXP (note, 0));
12389 if (LABEL_P (label))
12390 LABEL_NUSES (label)--;
12392 place2 = 0;
12394 break;
12396 case REG_NONNEG:
12397 /* This note says something about the value of a register prior
12398 to the execution of an insn. It is too much trouble to see
12399 if the note is still correct in all situations. It is better
12400 to simply delete it. */
12401 break;
12403 case REG_LIBCALL_ID:
12404 /* If the insn previously containing this note still exists,
12405 put it back where it was. Otherwise move it to the previous
12406 insn. */
12407 if (!NOTE_P (from_insn))
12408 place = from_insn;
12409 else
12410 place = prev_real_insn (from_insn);
12411 break;
12412 case REG_RETVAL:
12413 /* If the insn previously containing this note still exists,
12414 put it back where it was. Otherwise move it to the previous
12415 insn. Adjust the corresponding REG_LIBCALL note. */
12416 if (!NOTE_P (from_insn))
12417 place = from_insn;
12418 else
12420 tem = find_reg_note (XEXP (note, 0), REG_LIBCALL, NULL_RTX);
12421 place = prev_real_insn (from_insn);
12422 if (tem && place)
12423 XEXP (tem, 0) = place;
12424 /* If we're deleting the last remaining instruction of a
12425 libcall sequence, don't add the notes. */
12426 else if (XEXP (note, 0) == from_insn)
12427 tem = place = 0;
12428 /* Don't add the dangling REG_RETVAL note. */
12429 else if (! tem)
12430 place = 0;
12432 break;
12434 case REG_LIBCALL:
12435 /* This is handled similarly to REG_RETVAL. */
12436 if (!NOTE_P (from_insn))
12437 place = from_insn;
12438 else
12440 tem = find_reg_note (XEXP (note, 0), REG_RETVAL, NULL_RTX);
12441 place = next_real_insn (from_insn);
12442 if (tem && place)
12443 XEXP (tem, 0) = place;
12444 /* If we're deleting the last remaining instruction of a
12445 libcall sequence, don't add the notes. */
12446 else if (XEXP (note, 0) == from_insn)
12447 tem = place = 0;
12448 /* Don't add the dangling REG_LIBCALL note. */
12449 else if (! tem)
12450 place = 0;
12452 break;
12454 case REG_DEAD:
12455 /* If we replaced the right hand side of FROM_INSN with a
12456 REG_EQUAL note, the original use of the dying register
12457 will not have been combined into I3 and I2. In such cases,
12458 FROM_INSN is guaranteed to be the first of the combined
12459 instructions, so we simply need to search back before
12460 FROM_INSN for the previous use or set of this register,
12461 then alter the notes there appropriately.
12463 If the register is used as an input in I3, it dies there.
12464 Similarly for I2, if it is nonzero and adjacent to I3.
12466 If the register is not used as an input in either I3 or I2
12467 and it is not one of the registers we were supposed to eliminate,
12468 there are two possibilities. We might have a non-adjacent I2
12469 or we might have somehow eliminated an additional register
12470 from a computation. For example, we might have had A & B where
12471 we discover that B will always be zero. In this case we will
12472 eliminate the reference to A.
12474 In both cases, we must search to see if we can find a previous
12475 use of A and put the death note there. */
12477 if (from_insn
12478 && from_insn == i2mod
12479 && !reg_overlap_mentioned_p (XEXP (note, 0), i2mod_new_rhs))
12480 tem = from_insn;
12481 else
12483 if (from_insn
12484 && CALL_P (from_insn)
12485 && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
12486 place = from_insn;
12487 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
12488 place = i3;
12489 else if (i2 != 0 && next_nonnote_insn (i2) == i3
12490 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
12491 place = i2;
12492 else if ((rtx_equal_p (XEXP (note, 0), elim_i2)
12493 && !(i2mod
12494 && reg_overlap_mentioned_p (XEXP (note, 0),
12495 i2mod_old_rhs)))
12496 || rtx_equal_p (XEXP (note, 0), elim_i1))
12497 break;
12498 tem = i3;
12501 if (place == 0)
12503 basic_block bb = this_basic_block;
12505 for (tem = PREV_INSN (tem); place == 0; tem = PREV_INSN (tem))
12507 if (! INSN_P (tem))
12509 if (tem == BB_HEAD (bb))
12510 break;
12511 continue;
12514 /* If the register is being set at TEM, see if that is all
12515 TEM is doing. If so, delete TEM. Otherwise, make this
12516 into a REG_UNUSED note instead. Don't delete sets to
12517 global register vars. */
12518 if ((REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER
12519 || !global_regs[REGNO (XEXP (note, 0))])
12520 && reg_set_p (XEXP (note, 0), PATTERN (tem)))
12522 rtx set = single_set (tem);
12523 rtx inner_dest = 0;
12524 #ifdef HAVE_cc0
12525 rtx cc0_setter = NULL_RTX;
12526 #endif
12528 if (set != 0)
12529 for (inner_dest = SET_DEST (set);
12530 (GET_CODE (inner_dest) == STRICT_LOW_PART
12531 || GET_CODE (inner_dest) == SUBREG
12532 || GET_CODE (inner_dest) == ZERO_EXTRACT);
12533 inner_dest = XEXP (inner_dest, 0))
12536 /* Verify that it was the set, and not a clobber that
12537 modified the register.
12539 CC0 targets must be careful to maintain setter/user
12540 pairs. If we cannot delete the setter due to side
12541 effects, mark the user with an UNUSED note instead
12542 of deleting it. */
12544 if (set != 0 && ! side_effects_p (SET_SRC (set))
12545 && rtx_equal_p (XEXP (note, 0), inner_dest)
12546 #ifdef HAVE_cc0
12547 && (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
12548 || ((cc0_setter = prev_cc0_setter (tem)) != NULL
12549 && sets_cc0_p (PATTERN (cc0_setter)) > 0))
12550 #endif
12553 /* Move the notes and links of TEM elsewhere.
12554 This might delete other dead insns recursively.
12555 First set the pattern to something that won't use
12556 any register. */
12557 rtx old_notes = REG_NOTES (tem);
12559 PATTERN (tem) = pc_rtx;
12560 REG_NOTES (tem) = NULL;
12562 distribute_notes (old_notes, tem, tem, NULL_RTX,
12563 NULL_RTX, NULL_RTX);
12564 distribute_links (LOG_LINKS (tem));
12566 SET_INSN_DELETED (tem);
12568 #ifdef HAVE_cc0
12569 /* Delete the setter too. */
12570 if (cc0_setter)
12572 PATTERN (cc0_setter) = pc_rtx;
12573 old_notes = REG_NOTES (cc0_setter);
12574 REG_NOTES (cc0_setter) = NULL;
12576 distribute_notes (old_notes, cc0_setter,
12577 cc0_setter, NULL_RTX,
12578 NULL_RTX, NULL_RTX);
12579 distribute_links (LOG_LINKS (cc0_setter));
12581 SET_INSN_DELETED (cc0_setter);
12583 #endif
12585 else
12587 PUT_REG_NOTE_KIND (note, REG_UNUSED);
12589 /* If there isn't already a REG_UNUSED note, put one
12590 here. Do not place a REG_DEAD note, even if
12591 the register is also used here; that would not
12592 match the algorithm used in lifetime analysis
12593 and can cause the consistency check in the
12594 scheduler to fail. */
12595 if (! find_regno_note (tem, REG_UNUSED,
12596 REGNO (XEXP (note, 0))))
12597 place = tem;
12598 break;
12601 else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem))
12602 || (CALL_P (tem)
12603 && find_reg_fusage (tem, USE, XEXP (note, 0))))
12605 place = tem;
12607 /* If we are doing a 3->2 combination, and we have a
12608 register which formerly died in i3 and was not used
12609 by i2, which now no longer dies in i3 and is used in
12610 i2 but does not die in i2, and place is between i2
12611 and i3, then we may need to move a link from place to
12612 i2. */
12613 if (i2 && DF_INSN_LUID (place) > DF_INSN_LUID (i2)
12614 && from_insn
12615 && DF_INSN_LUID (from_insn) > DF_INSN_LUID (i2)
12616 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
12618 rtx links = LOG_LINKS (place);
12619 LOG_LINKS (place) = 0;
12620 distribute_links (links);
12622 break;
12625 if (tem == BB_HEAD (bb))
12626 break;
12631 /* If the register is set or already dead at PLACE, we needn't do
12632 anything with this note if it is still a REG_DEAD note.
12633 We check here if it is set at all, not if is it totally replaced,
12634 which is what `dead_or_set_p' checks, so also check for it being
12635 set partially. */
12637 if (place && REG_NOTE_KIND (note) == REG_DEAD)
12639 unsigned int regno = REGNO (XEXP (note, 0));
12642 if (dead_or_set_p (place, XEXP (note, 0))
12643 || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
12645 /* Unless the register previously died in PLACE, clear
12646 last_death. [I no longer understand why this is
12647 being done.] */
12648 if (reg_stat[regno].last_death != place)
12649 reg_stat[regno].last_death = 0;
12650 place = 0;
12652 else
12653 reg_stat[regno].last_death = place;
12655 /* If this is a death note for a hard reg that is occupying
12656 multiple registers, ensure that we are still using all
12657 parts of the object. If we find a piece of the object
12658 that is unused, we must arrange for an appropriate REG_DEAD
12659 note to be added for it. However, we can't just emit a USE
12660 and tag the note to it, since the register might actually
12661 be dead; so we recourse, and the recursive call then finds
12662 the previous insn that used this register. */
12664 if (place && regno < FIRST_PSEUDO_REGISTER
12665 && hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))] > 1)
12667 unsigned int endregno = END_HARD_REGNO (XEXP (note, 0));
12668 int all_used = 1;
12669 unsigned int i;
12671 for (i = regno; i < endregno; i++)
12672 if ((! refers_to_regno_p (i, i + 1, PATTERN (place), 0)
12673 && ! find_regno_fusage (place, USE, i))
12674 || dead_or_set_regno_p (place, i))
12675 all_used = 0;
12677 if (! all_used)
12679 /* Put only REG_DEAD notes for pieces that are
12680 not already dead or set. */
12682 for (i = regno; i < endregno;
12683 i += hard_regno_nregs[i][reg_raw_mode[i]])
12685 rtx piece = regno_reg_rtx[i];
12686 basic_block bb = this_basic_block;
12688 if (! dead_or_set_p (place, piece)
12689 && ! reg_bitfield_target_p (piece,
12690 PATTERN (place)))
12692 rtx new_note
12693 = gen_rtx_EXPR_LIST (REG_DEAD, piece, NULL_RTX);
12695 distribute_notes (new_note, place, place,
12696 NULL_RTX, NULL_RTX, NULL_RTX);
12698 else if (! refers_to_regno_p (i, i + 1,
12699 PATTERN (place), 0)
12700 && ! find_regno_fusage (place, USE, i))
12701 for (tem = PREV_INSN (place); ;
12702 tem = PREV_INSN (tem))
12704 if (! INSN_P (tem))
12706 if (tem == BB_HEAD (bb))
12707 break;
12708 continue;
12710 if (dead_or_set_p (tem, piece)
12711 || reg_bitfield_target_p (piece,
12712 PATTERN (tem)))
12714 REG_NOTES (tem)
12715 = gen_rtx_EXPR_LIST (REG_UNUSED, piece,
12716 REG_NOTES (tem));
12717 break;
12723 place = 0;
12727 break;
12729 default:
12730 /* Any other notes should not be present at this point in the
12731 compilation. */
12732 gcc_unreachable ();
12735 if (place)
12737 XEXP (note, 1) = REG_NOTES (place);
12738 REG_NOTES (place) = note;
12741 if (place2)
12742 REG_NOTES (place2)
12743 = gen_rtx_fmt_ee (GET_CODE (note), REG_NOTE_KIND (note),
12744 XEXP (note, 0), REG_NOTES (place2));
12748 /* Similarly to above, distribute the LOG_LINKS that used to be present on
12749 I3, I2, and I1 to new locations. This is also called to add a link
12750 pointing at I3 when I3's destination is changed. */
12752 static void
12753 distribute_links (rtx links)
12755 rtx link, next_link;
12757 for (link = links; link; link = next_link)
12759 rtx place = 0;
12760 rtx insn;
12761 rtx set, reg;
12763 next_link = XEXP (link, 1);
12765 /* If the insn that this link points to is a NOTE or isn't a single
12766 set, ignore it. In the latter case, it isn't clear what we
12767 can do other than ignore the link, since we can't tell which
12768 register it was for. Such links wouldn't be used by combine
12769 anyway.
12771 It is not possible for the destination of the target of the link to
12772 have been changed by combine. The only potential of this is if we
12773 replace I3, I2, and I1 by I3 and I2. But in that case the
12774 destination of I2 also remains unchanged. */
12776 if (NOTE_P (XEXP (link, 0))
12777 || (set = single_set (XEXP (link, 0))) == 0)
12778 continue;
12780 reg = SET_DEST (set);
12781 while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
12782 || GET_CODE (reg) == STRICT_LOW_PART)
12783 reg = XEXP (reg, 0);
12785 /* A LOG_LINK is defined as being placed on the first insn that uses
12786 a register and points to the insn that sets the register. Start
12787 searching at the next insn after the target of the link and stop
12788 when we reach a set of the register or the end of the basic block.
12790 Note that this correctly handles the link that used to point from
12791 I3 to I2. Also note that not much searching is typically done here
12792 since most links don't point very far away. */
12794 for (insn = NEXT_INSN (XEXP (link, 0));
12795 (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
12796 || BB_HEAD (this_basic_block->next_bb) != insn));
12797 insn = NEXT_INSN (insn))
12798 if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
12800 if (reg_referenced_p (reg, PATTERN (insn)))
12801 place = insn;
12802 break;
12804 else if (CALL_P (insn)
12805 && find_reg_fusage (insn, USE, reg))
12807 place = insn;
12808 break;
12810 else if (INSN_P (insn) && reg_set_p (reg, insn))
12811 break;
12813 /* If we found a place to put the link, place it there unless there
12814 is already a link to the same insn as LINK at that point. */
12816 if (place)
12818 rtx link2;
12820 for (link2 = LOG_LINKS (place); link2; link2 = XEXP (link2, 1))
12821 if (XEXP (link2, 0) == XEXP (link, 0))
12822 break;
12824 if (link2 == 0)
12826 XEXP (link, 1) = LOG_LINKS (place);
12827 LOG_LINKS (place) = link;
12829 /* Set added_links_insn to the earliest insn we added a
12830 link to. */
12831 if (added_links_insn == 0
12832 || DF_INSN_LUID (added_links_insn) > DF_INSN_LUID (place))
12833 added_links_insn = place;
12839 /* Subroutine of unmentioned_reg_p and callback from for_each_rtx.
12840 Check whether the expression pointer to by LOC is a register or
12841 memory, and if so return 1 if it isn't mentioned in the rtx EXPR.
12842 Otherwise return zero. */
12844 static int
12845 unmentioned_reg_p_1 (rtx *loc, void *expr)
12847 rtx x = *loc;
12849 if (x != NULL_RTX
12850 && (REG_P (x) || MEM_P (x))
12851 && ! reg_mentioned_p (x, (rtx) expr))
12852 return 1;
12853 return 0;
12856 /* Check for any register or memory mentioned in EQUIV that is not
12857 mentioned in EXPR. This is used to restrict EQUIV to "specializations"
12858 of EXPR where some registers may have been replaced by constants. */
12860 static bool
12861 unmentioned_reg_p (rtx equiv, rtx expr)
12863 return for_each_rtx (&equiv, unmentioned_reg_p_1, expr);
12866 void
12867 dump_combine_stats (FILE *file)
12869 fprintf
12870 (file,
12871 ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
12872 combine_attempts, combine_merges, combine_extras, combine_successes);
12875 void
12876 dump_combine_total_stats (FILE *file)
12878 fprintf
12879 (file,
12880 "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
12881 total_attempts, total_merges, total_extras, total_successes);
12884 static bool
12885 gate_handle_combine (void)
12887 return (optimize > 0);
12890 /* Try combining insns through substitution. */
12891 static unsigned int
12892 rest_of_handle_combine (void)
12894 int rebuild_jump_labels_after_combine;
12896 df_set_flags (DF_LR_RUN_DCE + DF_DEFER_INSN_RESCAN);
12897 df_note_add_problem ();
12898 df_analyze ();
12900 regstat_init_n_sets_and_refs ();
12902 rebuild_jump_labels_after_combine
12903 = combine_instructions (get_insns (), max_reg_num ());
12905 /* Combining insns may have turned an indirect jump into a
12906 direct jump. Rebuild the JUMP_LABEL fields of jumping
12907 instructions. */
12908 if (rebuild_jump_labels_after_combine)
12910 timevar_push (TV_JUMP);
12911 rebuild_jump_labels (get_insns ());
12912 cleanup_cfg (0);
12913 timevar_pop (TV_JUMP);
12916 regstat_free_n_sets_and_refs ();
12917 return 0;
12920 struct tree_opt_pass pass_combine =
12922 "combine", /* name */
12923 gate_handle_combine, /* gate */
12924 rest_of_handle_combine, /* execute */
12925 NULL, /* sub */
12926 NULL, /* next */
12927 0, /* static_pass_number */
12928 TV_COMBINE, /* tv_id */
12929 0, /* properties_required */
12930 0, /* properties_provided */
12931 0, /* properties_destroyed */
12932 0, /* todo_flags_start */
12933 TODO_dump_func |
12934 TODO_df_finish |
12935 TODO_ggc_collect, /* todo_flags_finish */
12936 'c' /* letter */