* configure.ac (HAVE_GAS_CFI_DIRECTIVE): Always test for assembler
[official-gcc.git] / gcc / combine.c
bloba39649d7c8eb1bfcef454f1319f39e23f1525c40
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, 2008
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 3, 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 COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* This module is essentially the "combiner" phase of the U. of Arizona
23 Portable Optimizer, but redone to work on our list-structured
24 representation for RTL instead of their string representation.
26 The LOG_LINKS of each insn identify the most recent assignment
27 to each REG used in the insn. It is a list of previous insns,
28 each of which contains a SET for a REG that is used in this insn
29 and not used or set in between. LOG_LINKs never cross basic blocks.
30 They were set up by the preceding pass (lifetime analysis).
32 We try to combine each pair of insns joined by a logical link.
33 We also try to combine triples of insns A, B and C when
34 C has a link back to B and B has a link back to A.
36 LOG_LINKS does not have links for use of the CC0. They don't
37 need to, because the insn that sets the CC0 is always immediately
38 before the insn that tests it. So we always regard a branch
39 insn as having a logical link to the preceding insn. The same is true
40 for an insn explicitly using CC0.
42 We check (with use_crosses_set_p) to avoid combining in such a way
43 as to move a computation to a place where its value would be different.
45 Combination is done by mathematically substituting the previous
46 insn(s) values for the regs they set into the expressions in
47 the later insns that refer to these regs. If the result is a valid insn
48 for our target machine, according to the machine description,
49 we install it, delete the earlier insns, and update the data flow
50 information (LOG_LINKS and REG_NOTES) for what we did.
52 There are a few exceptions where the dataflow information isn't
53 completely updated (however this is only a local issue since it is
54 regenerated before the next pass that uses it):
56 - reg_live_length is not updated
57 - reg_n_refs is not adjusted in the rare case when a register is
58 no longer required in a computation
59 - there are extremely rare cases (see distribute_notes) when a
60 REG_DEAD note is lost
61 - a LOG_LINKS entry that refers to an insn with multiple SETs may be
62 removed because there is no way to know which register it was
63 linking
65 To simplify substitution, we combine only when the earlier insn(s)
66 consist of only a single assignment. To simplify updating afterward,
67 we never combine when a subroutine call appears in the middle.
69 Since we do not represent assignments to CC0 explicitly except when that
70 is all an insn does, there is no LOG_LINKS entry in an insn that uses
71 the condition code for the insn that set the condition code.
72 Fortunately, these two insns must be consecutive.
73 Therefore, every JUMP_INSN is taken to have an implicit logical link
74 to the preceding insn. This is not quite right, since non-jumps can
75 also use the condition code; but in practice such insns would not
76 combine anyway. */
78 #include "config.h"
79 #include "system.h"
80 #include "coretypes.h"
81 #include "tm.h"
82 #include "rtl.h"
83 #include "tree.h"
84 #include "tm_p.h"
85 #include "flags.h"
86 #include "regs.h"
87 #include "hard-reg-set.h"
88 #include "basic-block.h"
89 #include "insn-config.h"
90 #include "function.h"
91 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
92 #include "expr.h"
93 #include "insn-attr.h"
94 #include "recog.h"
95 #include "real.h"
96 #include "toplev.h"
97 #include "target.h"
98 #include "optabs.h"
99 #include "insn-codes.h"
100 #include "rtlhooks-def.h"
101 /* Include output.h for dump_file. */
102 #include "output.h"
103 #include "params.h"
104 #include "timevar.h"
105 #include "tree-pass.h"
106 #include "df.h"
107 #include "cgraph.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 typedef struct reg_stat_struct {
147 /* Record last point of death of (hard or pseudo) register n. */
148 rtx last_death;
150 /* Record last point of modification of (hard or pseudo) register n. */
151 rtx last_set;
153 /* The next group of fields allows the recording of the last value assigned
154 to (hard or pseudo) register n. We use this information to see if an
155 operation being processed is redundant given a prior operation performed
156 on the register. For example, an `and' with a constant is redundant if
157 all the zero bits are already known to be turned off.
159 We use an approach similar to that used by cse, but change it in the
160 following ways:
162 (1) We do not want to reinitialize at each label.
163 (2) It is useful, but not critical, to know the actual value assigned
164 to a register. Often just its form is helpful.
166 Therefore, we maintain the following fields:
168 last_set_value the last value assigned
169 last_set_label records the value of label_tick when the
170 register was assigned
171 last_set_table_tick records the value of label_tick when a
172 value using the register is assigned
173 last_set_invalid set to nonzero when it is not valid
174 to use the value of this register in some
175 register's value
177 To understand the usage of these tables, it is important to understand
178 the distinction between the value in last_set_value being valid and
179 the register being validly contained in some other expression in the
180 table.
182 (The next two parameters are out of date).
184 reg_stat[i].last_set_value is valid if it is nonzero, and either
185 reg_n_sets[i] is 1 or reg_stat[i].last_set_label == label_tick.
187 Register I may validly appear in any expression returned for the value
188 of another register if reg_n_sets[i] is 1. It may also appear in the
189 value for register J if reg_stat[j].last_set_invalid is zero, or
190 reg_stat[i].last_set_label < reg_stat[j].last_set_label.
192 If an expression is found in the table containing a register which may
193 not validly appear in an expression, the register is replaced by
194 something that won't match, (clobber (const_int 0)). */
196 /* Record last value assigned to (hard or pseudo) register n. */
198 rtx last_set_value;
200 /* Record the value of label_tick when an expression involving register n
201 is placed in last_set_value. */
203 int last_set_table_tick;
205 /* Record the value of label_tick when the value for register n is placed in
206 last_set_value. */
208 int last_set_label;
210 /* These fields are maintained in parallel with last_set_value and are
211 used to store the mode in which the register was last set, the bits
212 that were known to be zero when it was last set, and the number of
213 sign bits copies it was known to have when it was last set. */
215 unsigned HOST_WIDE_INT last_set_nonzero_bits;
216 char last_set_sign_bit_copies;
217 ENUM_BITFIELD(machine_mode) last_set_mode : 8;
219 /* Set nonzero if references to register n in expressions should not be
220 used. last_set_invalid is set nonzero when this register is being
221 assigned to and last_set_table_tick == label_tick. */
223 char last_set_invalid;
225 /* Some registers that are set more than once and used in more than one
226 basic block are nevertheless always set in similar ways. For example,
227 a QImode register may be loaded from memory in two places on a machine
228 where byte loads zero extend.
230 We record in the following fields if a register has some leading bits
231 that are always equal to the sign bit, and what we know about the
232 nonzero bits of a register, specifically which bits are known to be
233 zero.
235 If an entry is zero, it means that we don't know anything special. */
237 unsigned char sign_bit_copies;
239 unsigned HOST_WIDE_INT nonzero_bits;
241 /* Record the value of the label_tick when the last truncation
242 happened. The field truncated_to_mode is only valid if
243 truncation_label == label_tick. */
245 int truncation_label;
247 /* Record the last truncation seen for this register. If truncation
248 is not a nop to this mode we might be able to save an explicit
249 truncation if we know that value already contains a truncated
250 value. */
252 ENUM_BITFIELD(machine_mode) truncated_to_mode : 8;
253 } reg_stat_type;
255 DEF_VEC_O(reg_stat_type);
256 DEF_VEC_ALLOC_O(reg_stat_type,heap);
258 static VEC(reg_stat_type,heap) *reg_stat;
260 /* Record the luid of the last insn that invalidated memory
261 (anything that writes memory, and subroutine calls, but not pushes). */
263 static int mem_last_set;
265 /* Record the luid of the last CALL_INSN
266 so we can tell whether a potential combination crosses any calls. */
268 static int last_call_luid;
270 /* When `subst' is called, this is the insn that is being modified
271 (by combining in a previous insn). The PATTERN of this insn
272 is still the old pattern partially modified and it should not be
273 looked at, but this may be used to examine the successors of the insn
274 to judge whether a simplification is valid. */
276 static rtx subst_insn;
278 /* This is the lowest LUID that `subst' is currently dealing with.
279 get_last_value will not return a value if the register was set at or
280 after this LUID. If not for this mechanism, we could get confused if
281 I2 or I1 in try_combine were an insn that used the old value of a register
282 to obtain a new value. In that case, we might erroneously get the
283 new value of the register when we wanted the old one. */
285 static int subst_low_luid;
287 /* This contains any hard registers that are used in newpat; reg_dead_at_p
288 must consider all these registers to be always live. */
290 static HARD_REG_SET newpat_used_regs;
292 /* This is an insn to which a LOG_LINKS entry has been added. If this
293 insn is the earlier than I2 or I3, combine should rescan starting at
294 that location. */
296 static rtx added_links_insn;
298 /* Basic block in which we are performing combines. */
299 static basic_block this_basic_block;
302 /* Length of the currently allocated uid_insn_cost array. */
304 static int max_uid_known;
306 /* The following array records the insn_rtx_cost for every insn
307 in the instruction stream. */
309 static int *uid_insn_cost;
311 /* The following array records the LOG_LINKS for every insn in the
312 instruction stream as an INSN_LIST rtx. */
314 static rtx *uid_log_links;
316 #define INSN_COST(INSN) (uid_insn_cost[INSN_UID (INSN)])
317 #define LOG_LINKS(INSN) (uid_log_links[INSN_UID (INSN)])
319 /* Incremented for each basic block. */
321 static int label_tick;
323 /* Reset to label_tick for each label. */
325 static int label_tick_ebb_start;
327 /* Mode used to compute significance in reg_stat[].nonzero_bits. It is the
328 largest integer mode that can fit in HOST_BITS_PER_WIDE_INT. */
330 static enum machine_mode nonzero_bits_mode;
332 /* Nonzero when reg_stat[].nonzero_bits and reg_stat[].sign_bit_copies can
333 be safely used. It is zero while computing them and after combine has
334 completed. This former test prevents propagating values based on
335 previously set values, which can be incorrect if a variable is modified
336 in a loop. */
338 static int nonzero_sign_valid;
341 /* Record one modification to rtl structure
342 to be undone by storing old_contents into *where. */
344 struct undo
346 struct undo *next;
347 enum { UNDO_RTX, UNDO_INT, UNDO_MODE } kind;
348 union { rtx r; int i; enum machine_mode m; } old_contents;
349 union { rtx *r; int *i; } where;
352 /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
353 num_undo says how many are currently recorded.
355 other_insn is nonzero if we have modified some other insn in the process
356 of working on subst_insn. It must be verified too. */
358 struct undobuf
360 struct undo *undos;
361 struct undo *frees;
362 rtx other_insn;
365 static struct undobuf undobuf;
367 /* Number of times the pseudo being substituted for
368 was found and replaced. */
370 static int n_occurrences;
372 static rtx reg_nonzero_bits_for_combine (const_rtx, enum machine_mode, const_rtx,
373 enum machine_mode,
374 unsigned HOST_WIDE_INT,
375 unsigned HOST_WIDE_INT *);
376 static rtx reg_num_sign_bit_copies_for_combine (const_rtx, enum machine_mode, const_rtx,
377 enum machine_mode,
378 unsigned int, unsigned int *);
379 static void do_SUBST (rtx *, rtx);
380 static void do_SUBST_INT (int *, int);
381 static void init_reg_last (void);
382 static void setup_incoming_promotions (rtx);
383 static void set_nonzero_bits_and_sign_copies (rtx, const_rtx, void *);
384 static int cant_combine_insn_p (rtx);
385 static int can_combine_p (rtx, rtx, rtx, rtx, rtx *, rtx *);
386 static int combinable_i3pat (rtx, rtx *, rtx, rtx, int, rtx *);
387 static int contains_muldiv (rtx);
388 static rtx try_combine (rtx, rtx, rtx, int *);
389 static void undo_all (void);
390 static void undo_commit (void);
391 static rtx *find_split_point (rtx *, rtx);
392 static rtx subst (rtx, rtx, rtx, int, int);
393 static rtx combine_simplify_rtx (rtx, enum machine_mode, int);
394 static rtx simplify_if_then_else (rtx);
395 static rtx simplify_set (rtx);
396 static rtx simplify_logical (rtx);
397 static rtx expand_compound_operation (rtx);
398 static const_rtx expand_field_assignment (const_rtx);
399 static rtx make_extraction (enum machine_mode, rtx, HOST_WIDE_INT,
400 rtx, unsigned HOST_WIDE_INT, int, int, int);
401 static rtx extract_left_shift (rtx, int);
402 static rtx make_compound_operation (rtx, enum rtx_code);
403 static int get_pos_from_mask (unsigned HOST_WIDE_INT,
404 unsigned HOST_WIDE_INT *);
405 static rtx canon_reg_for_combine (rtx, rtx);
406 static rtx force_to_mode (rtx, enum machine_mode,
407 unsigned HOST_WIDE_INT, int);
408 static rtx if_then_else_cond (rtx, rtx *, rtx *);
409 static rtx known_cond (rtx, enum rtx_code, rtx, rtx);
410 static int rtx_equal_for_field_assignment_p (rtx, rtx);
411 static rtx make_field_assignment (rtx);
412 static rtx apply_distributive_law (rtx);
413 static rtx distribute_and_simplify_rtx (rtx, int);
414 static rtx simplify_and_const_int_1 (enum machine_mode, rtx,
415 unsigned HOST_WIDE_INT);
416 static rtx simplify_and_const_int (rtx, enum machine_mode, rtx,
417 unsigned HOST_WIDE_INT);
418 static int merge_outer_ops (enum rtx_code *, HOST_WIDE_INT *, enum rtx_code,
419 HOST_WIDE_INT, enum machine_mode, int *);
420 static rtx simplify_shift_const_1 (enum rtx_code, enum machine_mode, rtx, int);
421 static rtx simplify_shift_const (rtx, enum rtx_code, enum machine_mode, rtx,
422 int);
423 static int recog_for_combine (rtx *, rtx, rtx *);
424 static rtx gen_lowpart_for_combine (enum machine_mode, rtx);
425 static enum rtx_code simplify_comparison (enum rtx_code, rtx *, rtx *);
426 static void update_table_tick (rtx);
427 static void record_value_for_reg (rtx, rtx, rtx);
428 static void check_promoted_subreg (rtx, rtx);
429 static void record_dead_and_set_regs_1 (rtx, const_rtx, void *);
430 static void record_dead_and_set_regs (rtx);
431 static int get_last_value_validate (rtx *, rtx, int, int);
432 static rtx get_last_value (const_rtx);
433 static int use_crosses_set_p (const_rtx, int);
434 static void reg_dead_at_p_1 (rtx, const_rtx, void *);
435 static int reg_dead_at_p (rtx, rtx);
436 static void move_deaths (rtx, rtx, int, rtx, rtx *);
437 static int reg_bitfield_target_p (rtx, rtx);
438 static void distribute_notes (rtx, rtx, rtx, rtx, rtx, rtx);
439 static void distribute_links (rtx);
440 static void mark_used_regs_combine (rtx);
441 static void record_promoted_value (rtx, rtx);
442 static int unmentioned_reg_p_1 (rtx *, void *);
443 static bool unmentioned_reg_p (rtx, rtx);
444 static int record_truncated_value (rtx *, void *);
445 static void record_truncated_values (rtx *, void *);
446 static bool reg_truncated_to_mode (enum machine_mode, const_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 /* Try to split PATTERN found in INSN. This returns NULL_RTX if
472 PATTERN can not be split. Otherwise, it returns an insn sequence.
473 This is a wrapper around split_insns which ensures that the
474 reg_stat vector is made larger if the splitter creates a new
475 register. */
477 static rtx
478 combine_split_insns (rtx pattern, rtx insn)
480 rtx ret;
481 unsigned int nregs;
483 ret = split_insns (pattern, insn);
484 nregs = max_reg_num ();
485 if (nregs > VEC_length (reg_stat_type, reg_stat))
486 VEC_safe_grow_cleared (reg_stat_type, heap, reg_stat, nregs);
487 return ret;
490 /* This is used by find_single_use to locate an rtx in LOC that
491 contains exactly one use of DEST, which is typically either a REG
492 or CC0. It returns a pointer to the innermost rtx expression
493 containing DEST. Appearances of DEST that are being used to
494 totally replace it are not counted. */
496 static rtx *
497 find_single_use_1 (rtx dest, rtx *loc)
499 rtx x = *loc;
500 enum rtx_code code = GET_CODE (x);
501 rtx *result = NULL;
502 rtx *this_result;
503 int i;
504 const char *fmt;
506 switch (code)
508 case CONST_INT:
509 case CONST:
510 case LABEL_REF:
511 case SYMBOL_REF:
512 case CONST_DOUBLE:
513 case CONST_VECTOR:
514 case CLOBBER:
515 return 0;
517 case SET:
518 /* If the destination is anything other than CC0, PC, a REG or a SUBREG
519 of a REG that occupies all of the REG, the insn uses DEST if
520 it is mentioned in the destination or the source. Otherwise, we
521 need just check the source. */
522 if (GET_CODE (SET_DEST (x)) != CC0
523 && GET_CODE (SET_DEST (x)) != PC
524 && !REG_P (SET_DEST (x))
525 && ! (GET_CODE (SET_DEST (x)) == SUBREG
526 && REG_P (SUBREG_REG (SET_DEST (x)))
527 && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
528 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
529 == ((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
530 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))))
531 break;
533 return find_single_use_1 (dest, &SET_SRC (x));
535 case MEM:
536 case SUBREG:
537 return find_single_use_1 (dest, &XEXP (x, 0));
539 default:
540 break;
543 /* If it wasn't one of the common cases above, check each expression and
544 vector of this code. Look for a unique usage of DEST. */
546 fmt = GET_RTX_FORMAT (code);
547 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
549 if (fmt[i] == 'e')
551 if (dest == XEXP (x, i)
552 || (REG_P (dest) && REG_P (XEXP (x, i))
553 && REGNO (dest) == REGNO (XEXP (x, i))))
554 this_result = loc;
555 else
556 this_result = find_single_use_1 (dest, &XEXP (x, i));
558 if (result == NULL)
559 result = this_result;
560 else if (this_result)
561 /* Duplicate usage. */
562 return NULL;
564 else if (fmt[i] == 'E')
566 int j;
568 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
570 if (XVECEXP (x, i, j) == dest
571 || (REG_P (dest)
572 && REG_P (XVECEXP (x, i, j))
573 && REGNO (XVECEXP (x, i, j)) == REGNO (dest)))
574 this_result = loc;
575 else
576 this_result = find_single_use_1 (dest, &XVECEXP (x, i, j));
578 if (result == NULL)
579 result = this_result;
580 else if (this_result)
581 return NULL;
586 return result;
590 /* See if DEST, produced in INSN, is used only a single time in the
591 sequel. If so, return a pointer to the innermost rtx expression in which
592 it is used.
594 If PLOC is nonzero, *PLOC is set to the insn containing the single use.
596 If DEST is cc0_rtx, we look only at the next insn. In that case, we don't
597 care about REG_DEAD notes or LOG_LINKS.
599 Otherwise, we find the single use by finding an insn that has a
600 LOG_LINKS pointing at INSN and has a REG_DEAD note for DEST. If DEST is
601 only referenced once in that insn, we know that it must be the first
602 and last insn referencing DEST. */
604 static rtx *
605 find_single_use (rtx dest, rtx insn, rtx *ploc)
607 rtx next;
608 rtx *result;
609 rtx link;
611 #ifdef HAVE_cc0
612 if (dest == cc0_rtx)
614 next = NEXT_INSN (insn);
615 if (next == 0
616 || (!NONJUMP_INSN_P (next) && !JUMP_P (next)))
617 return 0;
619 result = find_single_use_1 (dest, &PATTERN (next));
620 if (result && ploc)
621 *ploc = next;
622 return result;
624 #endif
626 if (!REG_P (dest))
627 return 0;
629 for (next = next_nonnote_insn (insn);
630 next != 0 && !LABEL_P (next);
631 next = next_nonnote_insn (next))
632 if (INSN_P (next) && dead_or_set_p (next, dest))
634 for (link = LOG_LINKS (next); link; link = XEXP (link, 1))
635 if (XEXP (link, 0) == insn)
636 break;
638 if (link)
640 result = find_single_use_1 (dest, &PATTERN (next));
641 if (ploc)
642 *ploc = next;
643 return result;
647 return 0;
650 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
651 insn. The substitution can be undone by undo_all. If INTO is already
652 set to NEWVAL, do not record this change. Because computing NEWVAL might
653 also call SUBST, we have to compute it before we put anything into
654 the undo table. */
656 static void
657 do_SUBST (rtx *into, rtx newval)
659 struct undo *buf;
660 rtx oldval = *into;
662 if (oldval == newval)
663 return;
665 /* We'd like to catch as many invalid transformations here as
666 possible. Unfortunately, there are way too many mode changes
667 that are perfectly valid, so we'd waste too much effort for
668 little gain doing the checks here. Focus on catching invalid
669 transformations involving integer constants. */
670 if (GET_MODE_CLASS (GET_MODE (oldval)) == MODE_INT
671 && GET_CODE (newval) == CONST_INT)
673 /* Sanity check that we're replacing oldval with a CONST_INT
674 that is a valid sign-extension for the original mode. */
675 gcc_assert (INTVAL (newval)
676 == trunc_int_for_mode (INTVAL (newval), GET_MODE (oldval)));
678 /* Replacing the operand of a SUBREG or a ZERO_EXTEND with a
679 CONST_INT is not valid, because after the replacement, the
680 original mode would be gone. Unfortunately, we can't tell
681 when do_SUBST is called to replace the operand thereof, so we
682 perform this test on oldval instead, checking whether an
683 invalid replacement took place before we got here. */
684 gcc_assert (!(GET_CODE (oldval) == SUBREG
685 && GET_CODE (SUBREG_REG (oldval)) == CONST_INT));
686 gcc_assert (!(GET_CODE (oldval) == ZERO_EXTEND
687 && GET_CODE (XEXP (oldval, 0)) == CONST_INT));
690 if (undobuf.frees)
691 buf = undobuf.frees, undobuf.frees = buf->next;
692 else
693 buf = XNEW (struct undo);
695 buf->kind = UNDO_RTX;
696 buf->where.r = into;
697 buf->old_contents.r = oldval;
698 *into = newval;
700 buf->next = undobuf.undos, undobuf.undos = buf;
703 #define SUBST(INTO, NEWVAL) do_SUBST(&(INTO), (NEWVAL))
705 /* Similar to SUBST, but NEWVAL is an int expression. Note that substitution
706 for the value of a HOST_WIDE_INT value (including CONST_INT) is
707 not safe. */
709 static void
710 do_SUBST_INT (int *into, int newval)
712 struct undo *buf;
713 int oldval = *into;
715 if (oldval == newval)
716 return;
718 if (undobuf.frees)
719 buf = undobuf.frees, undobuf.frees = buf->next;
720 else
721 buf = XNEW (struct undo);
723 buf->kind = UNDO_INT;
724 buf->where.i = into;
725 buf->old_contents.i = oldval;
726 *into = newval;
728 buf->next = undobuf.undos, undobuf.undos = buf;
731 #define SUBST_INT(INTO, NEWVAL) do_SUBST_INT(&(INTO), (NEWVAL))
733 /* Similar to SUBST, but just substitute the mode. This is used when
734 changing the mode of a pseudo-register, so that any other
735 references to the entry in the regno_reg_rtx array will change as
736 well. */
738 static void
739 do_SUBST_MODE (rtx *into, enum machine_mode newval)
741 struct undo *buf;
742 enum machine_mode oldval = GET_MODE (*into);
744 if (oldval == newval)
745 return;
747 if (undobuf.frees)
748 buf = undobuf.frees, undobuf.frees = buf->next;
749 else
750 buf = XNEW (struct undo);
752 buf->kind = UNDO_MODE;
753 buf->where.r = into;
754 buf->old_contents.m = oldval;
755 adjust_reg_mode (*into, newval);
757 buf->next = undobuf.undos, undobuf.undos = buf;
760 #define SUBST_MODE(INTO, NEWVAL) do_SUBST_MODE(&(INTO), (NEWVAL))
762 /* Subroutine of try_combine. Determine whether the combine replacement
763 patterns NEWPAT, NEWI2PAT and NEWOTHERPAT are cheaper according to
764 insn_rtx_cost that the original instruction sequence I1, I2, I3 and
765 undobuf.other_insn. Note that I1 and/or NEWI2PAT may be NULL_RTX.
766 NEWOTHERPAT and undobuf.other_insn may also both be NULL_RTX. This
767 function returns false, if the costs of all instructions can be
768 estimated, and the replacements are more expensive than the original
769 sequence. */
771 static bool
772 combine_validate_cost (rtx i1, rtx i2, rtx i3, rtx newpat, rtx newi2pat,
773 rtx newotherpat)
775 int i1_cost, i2_cost, i3_cost;
776 int new_i2_cost, new_i3_cost;
777 int old_cost, new_cost;
779 /* Lookup the original insn_rtx_costs. */
780 i2_cost = INSN_COST (i2);
781 i3_cost = INSN_COST (i3);
783 if (i1)
785 i1_cost = INSN_COST (i1);
786 old_cost = (i1_cost > 0 && i2_cost > 0 && i3_cost > 0)
787 ? i1_cost + i2_cost + i3_cost : 0;
789 else
791 old_cost = (i2_cost > 0 && i3_cost > 0) ? i2_cost + i3_cost : 0;
792 i1_cost = 0;
795 /* Calculate the replacement insn_rtx_costs. */
796 new_i3_cost = insn_rtx_cost (newpat);
797 if (newi2pat)
799 new_i2_cost = insn_rtx_cost (newi2pat);
800 new_cost = (new_i2_cost > 0 && new_i3_cost > 0)
801 ? new_i2_cost + new_i3_cost : 0;
803 else
805 new_cost = new_i3_cost;
806 new_i2_cost = 0;
809 if (undobuf.other_insn)
811 int old_other_cost, new_other_cost;
813 old_other_cost = INSN_COST (undobuf.other_insn);
814 new_other_cost = insn_rtx_cost (newotherpat);
815 if (old_other_cost > 0 && new_other_cost > 0)
817 old_cost += old_other_cost;
818 new_cost += new_other_cost;
820 else
821 old_cost = 0;
824 /* Disallow this recombination if both new_cost and old_cost are
825 greater than zero, and new_cost is greater than old cost. */
826 if (old_cost > 0
827 && new_cost > old_cost)
829 if (dump_file)
831 if (i1)
833 fprintf (dump_file,
834 "rejecting combination of insns %d, %d and %d\n",
835 INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
836 fprintf (dump_file, "original costs %d + %d + %d = %d\n",
837 i1_cost, i2_cost, i3_cost, old_cost);
839 else
841 fprintf (dump_file,
842 "rejecting combination of insns %d and %d\n",
843 INSN_UID (i2), INSN_UID (i3));
844 fprintf (dump_file, "original costs %d + %d = %d\n",
845 i2_cost, i3_cost, old_cost);
848 if (newi2pat)
850 fprintf (dump_file, "replacement costs %d + %d = %d\n",
851 new_i2_cost, new_i3_cost, new_cost);
853 else
854 fprintf (dump_file, "replacement cost %d\n", new_cost);
857 return false;
860 /* Update the uid_insn_cost array with the replacement costs. */
861 INSN_COST (i2) = new_i2_cost;
862 INSN_COST (i3) = new_i3_cost;
863 if (i1)
864 INSN_COST (i1) = 0;
866 return true;
870 /* Delete any insns that copy a register to itself. */
872 static void
873 delete_noop_moves (void)
875 rtx insn, next;
876 basic_block bb;
878 FOR_EACH_BB (bb)
880 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb)); insn = next)
882 next = NEXT_INSN (insn);
883 if (INSN_P (insn) && noop_move_p (insn))
885 if (dump_file)
886 fprintf (dump_file, "deleting noop move %d\n", INSN_UID (insn));
888 delete_insn_and_edges (insn);
895 /* Fill in log links field for all insns. */
897 static void
898 create_log_links (void)
900 basic_block bb;
901 rtx *next_use, insn;
902 struct df_ref **def_vec, **use_vec;
904 next_use = XCNEWVEC (rtx, max_reg_num ());
906 /* Pass through each block from the end, recording the uses of each
907 register and establishing log links when def is encountered.
908 Note that we do not clear next_use array in order to save time,
909 so we have to test whether the use is in the same basic block as def.
911 There are a few cases below when we do not consider the definition or
912 usage -- these are taken from original flow.c did. Don't ask me why it is
913 done this way; I don't know and if it works, I don't want to know. */
915 FOR_EACH_BB (bb)
917 FOR_BB_INSNS_REVERSE (bb, insn)
919 if (!INSN_P (insn))
920 continue;
922 /* Log links are created only once. */
923 gcc_assert (!LOG_LINKS (insn));
925 for (def_vec = DF_INSN_DEFS (insn); *def_vec; def_vec++)
927 struct df_ref *def = *def_vec;
928 int regno = DF_REF_REGNO (def);
929 rtx use_insn;
931 if (!next_use[regno])
932 continue;
934 /* Do not consider if it is pre/post modification in MEM. */
935 if (DF_REF_FLAGS (def) & DF_REF_PRE_POST_MODIFY)
936 continue;
938 /* Do not make the log link for frame pointer. */
939 if ((regno == FRAME_POINTER_REGNUM
940 && (! reload_completed || frame_pointer_needed))
941 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
942 || (regno == HARD_FRAME_POINTER_REGNUM
943 && (! reload_completed || frame_pointer_needed))
944 #endif
945 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
946 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
947 #endif
949 continue;
951 use_insn = next_use[regno];
952 if (BLOCK_FOR_INSN (use_insn) == bb)
954 /* flow.c claimed:
956 We don't build a LOG_LINK for hard registers contained
957 in ASM_OPERANDs. If these registers get replaced,
958 we might wind up changing the semantics of the insn,
959 even if reload can make what appear to be valid
960 assignments later. */
961 if (regno >= FIRST_PSEUDO_REGISTER
962 || asm_noperands (PATTERN (use_insn)) < 0)
964 /* Don't add duplicate links between instructions. */
965 rtx links;
966 for (links = LOG_LINKS (use_insn); links;
967 links = XEXP (links, 1))
968 if (insn == XEXP (links, 0))
969 break;
971 if (!links)
972 LOG_LINKS (use_insn) =
973 alloc_INSN_LIST (insn, LOG_LINKS (use_insn));
976 next_use[regno] = NULL_RTX;
979 for (use_vec = DF_INSN_USES (insn); *use_vec; use_vec++)
981 struct df_ref *use = *use_vec;
982 int regno = DF_REF_REGNO (use);
984 /* Do not consider the usage of the stack pointer
985 by function call. */
986 if (DF_REF_FLAGS (use) & DF_REF_CALL_STACK_USAGE)
987 continue;
989 next_use[regno] = insn;
994 free (next_use);
997 /* Clear LOG_LINKS fields of insns. */
999 static void
1000 clear_log_links (void)
1002 rtx insn;
1004 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1005 if (INSN_P (insn))
1006 free_INSN_LIST_list (&LOG_LINKS (insn));
1012 /* Main entry point for combiner. F is the first insn of the function.
1013 NREGS is the first unused pseudo-reg number.
1015 Return nonzero if the combiner has turned an indirect jump
1016 instruction into a direct jump. */
1017 static int
1018 combine_instructions (rtx f, unsigned int nregs)
1020 rtx insn, next;
1021 #ifdef HAVE_cc0
1022 rtx prev;
1023 #endif
1024 rtx links, nextlinks;
1025 rtx first;
1027 int new_direct_jump_p = 0;
1029 for (first = f; first && !INSN_P (first); )
1030 first = NEXT_INSN (first);
1031 if (!first)
1032 return 0;
1034 combine_attempts = 0;
1035 combine_merges = 0;
1036 combine_extras = 0;
1037 combine_successes = 0;
1039 rtl_hooks = combine_rtl_hooks;
1041 VEC_safe_grow_cleared (reg_stat_type, heap, reg_stat, nregs);
1043 init_recog_no_volatile ();
1045 /* Allocate array for insn info. */
1046 max_uid_known = get_max_uid ();
1047 uid_log_links = XCNEWVEC (rtx, max_uid_known + 1);
1048 uid_insn_cost = XCNEWVEC (int, max_uid_known + 1);
1050 nonzero_bits_mode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
1052 /* Don't use reg_stat[].nonzero_bits when computing it. This can cause
1053 problems when, for example, we have j <<= 1 in a loop. */
1055 nonzero_sign_valid = 0;
1057 /* Scan all SETs and see if we can deduce anything about what
1058 bits are known to be zero for some registers and how many copies
1059 of the sign bit are known to exist for those registers.
1061 Also set any known values so that we can use it while searching
1062 for what bits are known to be set. */
1064 label_tick = label_tick_ebb_start = 1;
1066 setup_incoming_promotions (first);
1068 create_log_links ();
1069 FOR_EACH_BB (this_basic_block)
1071 last_call_luid = 0;
1072 mem_last_set = -1;
1073 label_tick++;
1074 FOR_BB_INSNS (this_basic_block, insn)
1075 if (INSN_P (insn) && BLOCK_FOR_INSN (insn))
1077 subst_low_luid = DF_INSN_LUID (insn);
1078 subst_insn = insn;
1080 note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies,
1081 insn);
1082 record_dead_and_set_regs (insn);
1084 #ifdef AUTO_INC_DEC
1085 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
1086 if (REG_NOTE_KIND (links) == REG_INC)
1087 set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX,
1088 insn);
1089 #endif
1091 /* Record the current insn_rtx_cost of this instruction. */
1092 if (NONJUMP_INSN_P (insn))
1093 INSN_COST (insn) = insn_rtx_cost (PATTERN (insn));
1094 if (dump_file)
1095 fprintf(dump_file, "insn_cost %d: %d\n",
1096 INSN_UID (insn), INSN_COST (insn));
1098 else if (LABEL_P (insn))
1099 label_tick_ebb_start = label_tick;
1102 nonzero_sign_valid = 1;
1104 /* Now scan all the insns in forward order. */
1106 label_tick = label_tick_ebb_start = 1;
1107 init_reg_last ();
1108 setup_incoming_promotions (first);
1110 FOR_EACH_BB (this_basic_block)
1112 last_call_luid = 0;
1113 mem_last_set = -1;
1114 label_tick++;
1115 rtl_profile_for_bb (this_basic_block);
1116 for (insn = BB_HEAD (this_basic_block);
1117 insn != NEXT_INSN (BB_END (this_basic_block));
1118 insn = next ? next : NEXT_INSN (insn))
1120 next = 0;
1121 if (INSN_P (insn))
1123 /* See if we know about function return values before this
1124 insn based upon SUBREG flags. */
1125 check_promoted_subreg (insn, PATTERN (insn));
1127 /* See if we can find hardregs and subreg of pseudos in
1128 narrower modes. This could help turning TRUNCATEs
1129 into SUBREGs. */
1130 note_uses (&PATTERN (insn), record_truncated_values, NULL);
1132 /* Try this insn with each insn it links back to. */
1134 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
1135 if ((next = try_combine (insn, XEXP (links, 0),
1136 NULL_RTX, &new_direct_jump_p)) != 0)
1137 goto retry;
1139 /* Try each sequence of three linked insns ending with this one. */
1141 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
1143 rtx link = XEXP (links, 0);
1145 /* If the linked insn has been replaced by a note, then there
1146 is no point in pursuing this chain any further. */
1147 if (NOTE_P (link))
1148 continue;
1150 for (nextlinks = LOG_LINKS (link);
1151 nextlinks;
1152 nextlinks = XEXP (nextlinks, 1))
1153 if ((next = try_combine (insn, link,
1154 XEXP (nextlinks, 0),
1155 &new_direct_jump_p)) != 0)
1156 goto retry;
1159 #ifdef HAVE_cc0
1160 /* Try to combine a jump insn that uses CC0
1161 with a preceding insn that sets CC0, and maybe with its
1162 logical predecessor as well.
1163 This is how we make decrement-and-branch insns.
1164 We need this special code because data flow connections
1165 via CC0 do not get entered in LOG_LINKS. */
1167 if (JUMP_P (insn)
1168 && (prev = prev_nonnote_insn (insn)) != 0
1169 && NONJUMP_INSN_P (prev)
1170 && sets_cc0_p (PATTERN (prev)))
1172 if ((next = try_combine (insn, prev,
1173 NULL_RTX, &new_direct_jump_p)) != 0)
1174 goto retry;
1176 for (nextlinks = LOG_LINKS (prev); nextlinks;
1177 nextlinks = XEXP (nextlinks, 1))
1178 if ((next = try_combine (insn, prev,
1179 XEXP (nextlinks, 0),
1180 &new_direct_jump_p)) != 0)
1181 goto retry;
1184 /* Do the same for an insn that explicitly references CC0. */
1185 if (NONJUMP_INSN_P (insn)
1186 && (prev = prev_nonnote_insn (insn)) != 0
1187 && NONJUMP_INSN_P (prev)
1188 && sets_cc0_p (PATTERN (prev))
1189 && GET_CODE (PATTERN (insn)) == SET
1190 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
1192 if ((next = try_combine (insn, prev,
1193 NULL_RTX, &new_direct_jump_p)) != 0)
1194 goto retry;
1196 for (nextlinks = LOG_LINKS (prev); nextlinks;
1197 nextlinks = XEXP (nextlinks, 1))
1198 if ((next = try_combine (insn, prev,
1199 XEXP (nextlinks, 0),
1200 &new_direct_jump_p)) != 0)
1201 goto retry;
1204 /* Finally, see if any of the insns that this insn links to
1205 explicitly references CC0. If so, try this insn, that insn,
1206 and its predecessor if it sets CC0. */
1207 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
1208 if (NONJUMP_INSN_P (XEXP (links, 0))
1209 && GET_CODE (PATTERN (XEXP (links, 0))) == SET
1210 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (XEXP (links, 0))))
1211 && (prev = prev_nonnote_insn (XEXP (links, 0))) != 0
1212 && NONJUMP_INSN_P (prev)
1213 && sets_cc0_p (PATTERN (prev))
1214 && (next = try_combine (insn, XEXP (links, 0),
1215 prev, &new_direct_jump_p)) != 0)
1216 goto retry;
1217 #endif
1219 /* Try combining an insn with two different insns whose results it
1220 uses. */
1221 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
1222 for (nextlinks = XEXP (links, 1); nextlinks;
1223 nextlinks = XEXP (nextlinks, 1))
1224 if ((next = try_combine (insn, XEXP (links, 0),
1225 XEXP (nextlinks, 0),
1226 &new_direct_jump_p)) != 0)
1227 goto retry;
1229 /* Try this insn with each REG_EQUAL note it links back to. */
1230 for (links = LOG_LINKS (insn); links; links = XEXP (links, 1))
1232 rtx set, note;
1233 rtx temp = XEXP (links, 0);
1234 if ((set = single_set (temp)) != 0
1235 && (note = find_reg_equal_equiv_note (temp)) != 0
1236 && (note = XEXP (note, 0), GET_CODE (note)) != EXPR_LIST
1237 /* Avoid using a register that may already been marked
1238 dead by an earlier instruction. */
1239 && ! unmentioned_reg_p (note, SET_SRC (set))
1240 && (GET_MODE (note) == VOIDmode
1241 ? SCALAR_INT_MODE_P (GET_MODE (SET_DEST (set)))
1242 : GET_MODE (SET_DEST (set)) == GET_MODE (note)))
1244 /* Temporarily replace the set's source with the
1245 contents of the REG_EQUAL note. The insn will
1246 be deleted or recognized by try_combine. */
1247 rtx orig = SET_SRC (set);
1248 SET_SRC (set) = note;
1249 i2mod = temp;
1250 i2mod_old_rhs = copy_rtx (orig);
1251 i2mod_new_rhs = copy_rtx (note);
1252 next = try_combine (insn, i2mod, NULL_RTX,
1253 &new_direct_jump_p);
1254 i2mod = NULL_RTX;
1255 if (next)
1256 goto retry;
1257 SET_SRC (set) = orig;
1261 if (!NOTE_P (insn))
1262 record_dead_and_set_regs (insn);
1264 retry:
1267 else if (LABEL_P (insn))
1268 label_tick_ebb_start = label_tick;
1272 default_rtl_profile ();
1273 clear_log_links ();
1274 clear_bb_flags ();
1275 new_direct_jump_p |= purge_all_dead_edges ();
1276 delete_noop_moves ();
1278 /* Clean up. */
1279 free (uid_log_links);
1280 free (uid_insn_cost);
1281 VEC_free (reg_stat_type, heap, reg_stat);
1284 struct undo *undo, *next;
1285 for (undo = undobuf.frees; undo; undo = next)
1287 next = undo->next;
1288 free (undo);
1290 undobuf.frees = 0;
1293 total_attempts += combine_attempts;
1294 total_merges += combine_merges;
1295 total_extras += combine_extras;
1296 total_successes += combine_successes;
1298 nonzero_sign_valid = 0;
1299 rtl_hooks = general_rtl_hooks;
1301 /* Make recognizer allow volatile MEMs again. */
1302 init_recog ();
1304 return new_direct_jump_p;
1307 /* Wipe the last_xxx fields of reg_stat in preparation for another pass. */
1309 static void
1310 init_reg_last (void)
1312 unsigned int i;
1313 reg_stat_type *p;
1315 for (i = 0; VEC_iterate (reg_stat_type, reg_stat, i, p); ++i)
1316 memset (p, 0, offsetof (reg_stat_type, sign_bit_copies));
1319 /* Set up any promoted values for incoming argument registers. */
1321 static void
1322 setup_incoming_promotions (rtx first)
1324 tree arg;
1325 bool strictly_local = false;
1327 if (!targetm.calls.promote_function_args (TREE_TYPE (cfun->decl)))
1328 return;
1330 for (arg = DECL_ARGUMENTS (current_function_decl); arg;
1331 arg = TREE_CHAIN (arg))
1333 rtx reg = DECL_INCOMING_RTL (arg);
1334 int uns1, uns3;
1335 enum machine_mode mode1, mode2, mode3, mode4;
1337 /* Only continue if the incoming argument is in a register. */
1338 if (!REG_P (reg))
1339 continue;
1341 /* Determine, if possible, whether all call sites of the current
1342 function lie within the current compilation unit. (This does
1343 take into account the exporting of a function via taking its
1344 address, and so forth.) */
1345 strictly_local = cgraph_local_info (current_function_decl)->local;
1347 /* The mode and signedness of the argument before any promotions happen
1348 (equal to the mode of the pseudo holding it at that stage). */
1349 mode1 = TYPE_MODE (TREE_TYPE (arg));
1350 uns1 = TYPE_UNSIGNED (TREE_TYPE (arg));
1352 /* The mode and signedness of the argument after any source language and
1353 TARGET_PROMOTE_PROTOTYPES-driven promotions. */
1354 mode2 = TYPE_MODE (DECL_ARG_TYPE (arg));
1355 uns3 = TYPE_UNSIGNED (DECL_ARG_TYPE (arg));
1357 /* The mode and signedness of the argument as it is actually passed,
1358 after any TARGET_PROMOTE_FUNCTION_ARGS-driven ABI promotions. */
1359 mode3 = promote_mode (DECL_ARG_TYPE (arg), mode2, &uns3, 1);
1361 /* The mode of the register in which the argument is being passed. */
1362 mode4 = GET_MODE (reg);
1364 /* Eliminate sign extensions in the callee when possible. Only
1365 do this when:
1366 (a) a mode promotion has occurred;
1367 (b) the mode of the register is the same as the mode of
1368 the argument as it is passed; and
1369 (c) the signedness does not change across any of the promotions; and
1370 (d) when no language-level promotions (which we cannot guarantee
1371 will have been done by an external caller) are necessary,
1372 unless we know that this function is only ever called from
1373 the current compilation unit -- all of whose call sites will
1374 do the mode1 --> mode2 promotion. */
1375 if (mode1 != mode3
1376 && mode3 == mode4
1377 && uns1 == uns3
1378 && (mode1 == mode2 || strictly_local))
1380 /* Record that the value was promoted from mode1 to mode3,
1381 so that any sign extension at the head of the current
1382 function may be eliminated. */
1383 rtx x;
1384 x = gen_rtx_CLOBBER (mode1, const0_rtx);
1385 x = gen_rtx_fmt_e ((uns3 ? ZERO_EXTEND : SIGN_EXTEND), mode3, x);
1386 record_value_for_reg (reg, first, x);
1391 /* Called via note_stores. If X is a pseudo that is narrower than
1392 HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
1394 If we are setting only a portion of X and we can't figure out what
1395 portion, assume all bits will be used since we don't know what will
1396 be happening.
1398 Similarly, set how many bits of X are known to be copies of the sign bit
1399 at all locations in the function. This is the smallest number implied
1400 by any set of X. */
1402 static void
1403 set_nonzero_bits_and_sign_copies (rtx x, const_rtx set, void *data)
1405 rtx insn = (rtx) data;
1406 unsigned int num;
1408 if (REG_P (x)
1409 && REGNO (x) >= FIRST_PSEUDO_REGISTER
1410 /* If this register is undefined at the start of the file, we can't
1411 say what its contents were. */
1412 && ! REGNO_REG_SET_P
1413 (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), REGNO (x))
1414 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
1416 reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
1418 if (set == 0 || GET_CODE (set) == CLOBBER)
1420 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1421 rsp->sign_bit_copies = 1;
1422 return;
1425 /* If this register is being initialized using itself, and the
1426 register is uninitialized in this basic block, and there are
1427 no LOG_LINKS which set the register, then part of the
1428 register is uninitialized. In that case we can't assume
1429 anything about the number of nonzero bits.
1431 ??? We could do better if we checked this in
1432 reg_{nonzero_bits,num_sign_bit_copies}_for_combine. Then we
1433 could avoid making assumptions about the insn which initially
1434 sets the register, while still using the information in other
1435 insns. We would have to be careful to check every insn
1436 involved in the combination. */
1438 if (insn
1439 && reg_referenced_p (x, PATTERN (insn))
1440 && !REGNO_REG_SET_P (DF_LR_IN (BLOCK_FOR_INSN (insn)),
1441 REGNO (x)))
1443 rtx link;
1445 for (link = LOG_LINKS (insn); link; link = XEXP (link, 1))
1447 if (dead_or_set_p (XEXP (link, 0), x))
1448 break;
1450 if (!link)
1452 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1453 rsp->sign_bit_copies = 1;
1454 return;
1458 /* If this is a complex assignment, see if we can convert it into a
1459 simple assignment. */
1460 set = expand_field_assignment (set);
1462 /* If this is a simple assignment, or we have a paradoxical SUBREG,
1463 set what we know about X. */
1465 if (SET_DEST (set) == x
1466 || (GET_CODE (SET_DEST (set)) == SUBREG
1467 && (GET_MODE_SIZE (GET_MODE (SET_DEST (set)))
1468 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (set)))))
1469 && SUBREG_REG (SET_DEST (set)) == x))
1471 rtx src = SET_SRC (set);
1473 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
1474 /* If X is narrower than a word and SRC is a non-negative
1475 constant that would appear negative in the mode of X,
1476 sign-extend it for use in reg_stat[].nonzero_bits because some
1477 machines (maybe most) will actually do the sign-extension
1478 and this is the conservative approach.
1480 ??? For 2.5, try to tighten up the MD files in this regard
1481 instead of this kludge. */
1483 if (GET_MODE_BITSIZE (GET_MODE (x)) < BITS_PER_WORD
1484 && GET_CODE (src) == CONST_INT
1485 && INTVAL (src) > 0
1486 && 0 != (INTVAL (src)
1487 & ((HOST_WIDE_INT) 1
1488 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
1489 src = GEN_INT (INTVAL (src)
1490 | ((HOST_WIDE_INT) (-1)
1491 << GET_MODE_BITSIZE (GET_MODE (x))));
1492 #endif
1494 /* Don't call nonzero_bits if it cannot change anything. */
1495 if (rsp->nonzero_bits != ~(unsigned HOST_WIDE_INT) 0)
1496 rsp->nonzero_bits |= nonzero_bits (src, nonzero_bits_mode);
1497 num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
1498 if (rsp->sign_bit_copies == 0
1499 || rsp->sign_bit_copies > num)
1500 rsp->sign_bit_copies = num;
1502 else
1504 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1505 rsp->sign_bit_copies = 1;
1510 /* See if INSN can be combined into I3. PRED and SUCC are optionally
1511 insns that were previously combined into I3 or that will be combined
1512 into the merger of INSN and I3.
1514 Return 0 if the combination is not allowed for any reason.
1516 If the combination is allowed, *PDEST will be set to the single
1517 destination of INSN and *PSRC to the single source, and this function
1518 will return 1. */
1520 static int
1521 can_combine_p (rtx insn, rtx i3, rtx pred ATTRIBUTE_UNUSED, rtx succ,
1522 rtx *pdest, rtx *psrc)
1524 int i;
1525 const_rtx set = 0;
1526 rtx src, dest;
1527 rtx p;
1528 #ifdef AUTO_INC_DEC
1529 rtx link;
1530 #endif
1531 int all_adjacent = (succ ? (next_active_insn (insn) == succ
1532 && next_active_insn (succ) == i3)
1533 : next_active_insn (insn) == i3);
1535 /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
1536 or a PARALLEL consisting of such a SET and CLOBBERs.
1538 If INSN has CLOBBER parallel parts, ignore them for our processing.
1539 By definition, these happen during the execution of the insn. When it
1540 is merged with another insn, all bets are off. If they are, in fact,
1541 needed and aren't also supplied in I3, they may be added by
1542 recog_for_combine. Otherwise, it won't match.
1544 We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
1545 note.
1547 Get the source and destination of INSN. If more than one, can't
1548 combine. */
1550 if (GET_CODE (PATTERN (insn)) == SET)
1551 set = PATTERN (insn);
1552 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1553 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1555 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1557 rtx elt = XVECEXP (PATTERN (insn), 0, i);
1558 rtx note;
1560 switch (GET_CODE (elt))
1562 /* This is important to combine floating point insns
1563 for the SH4 port. */
1564 case USE:
1565 /* Combining an isolated USE doesn't make sense.
1566 We depend here on combinable_i3pat to reject them. */
1567 /* The code below this loop only verifies that the inputs of
1568 the SET in INSN do not change. We call reg_set_between_p
1569 to verify that the REG in the USE does not change between
1570 I3 and INSN.
1571 If the USE in INSN was for a pseudo register, the matching
1572 insn pattern will likely match any register; combining this
1573 with any other USE would only be safe if we knew that the
1574 used registers have identical values, or if there was
1575 something to tell them apart, e.g. different modes. For
1576 now, we forgo such complicated tests and simply disallow
1577 combining of USES of pseudo registers with any other USE. */
1578 if (REG_P (XEXP (elt, 0))
1579 && GET_CODE (PATTERN (i3)) == PARALLEL)
1581 rtx i3pat = PATTERN (i3);
1582 int i = XVECLEN (i3pat, 0) - 1;
1583 unsigned int regno = REGNO (XEXP (elt, 0));
1587 rtx i3elt = XVECEXP (i3pat, 0, i);
1589 if (GET_CODE (i3elt) == USE
1590 && REG_P (XEXP (i3elt, 0))
1591 && (REGNO (XEXP (i3elt, 0)) == regno
1592 ? reg_set_between_p (XEXP (elt, 0),
1593 PREV_INSN (insn), i3)
1594 : regno >= FIRST_PSEUDO_REGISTER))
1595 return 0;
1597 while (--i >= 0);
1599 break;
1601 /* We can ignore CLOBBERs. */
1602 case CLOBBER:
1603 break;
1605 case SET:
1606 /* Ignore SETs whose result isn't used but not those that
1607 have side-effects. */
1608 if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
1609 && (!(note = find_reg_note (insn, REG_EH_REGION, NULL_RTX))
1610 || INTVAL (XEXP (note, 0)) <= 0)
1611 && ! side_effects_p (elt))
1612 break;
1614 /* If we have already found a SET, this is a second one and
1615 so we cannot combine with this insn. */
1616 if (set)
1617 return 0;
1619 set = elt;
1620 break;
1622 default:
1623 /* Anything else means we can't combine. */
1624 return 0;
1628 if (set == 0
1629 /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
1630 so don't do anything with it. */
1631 || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
1632 return 0;
1634 else
1635 return 0;
1637 if (set == 0)
1638 return 0;
1640 set = expand_field_assignment (set);
1641 src = SET_SRC (set), dest = SET_DEST (set);
1643 /* Don't eliminate a store in the stack pointer. */
1644 if (dest == stack_pointer_rtx
1645 /* Don't combine with an insn that sets a register to itself if it has
1646 a REG_EQUAL note. This may be part of a LIBCALL sequence. */
1647 || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1648 /* Can't merge an ASM_OPERANDS. */
1649 || GET_CODE (src) == ASM_OPERANDS
1650 /* Can't merge a function call. */
1651 || GET_CODE (src) == CALL
1652 /* Don't eliminate a function call argument. */
1653 || (CALL_P (i3)
1654 && (find_reg_fusage (i3, USE, dest)
1655 || (REG_P (dest)
1656 && REGNO (dest) < FIRST_PSEUDO_REGISTER
1657 && global_regs[REGNO (dest)])))
1658 /* Don't substitute into an incremented register. */
1659 || FIND_REG_INC_NOTE (i3, dest)
1660 || (succ && FIND_REG_INC_NOTE (succ, dest))
1661 /* Don't substitute into a non-local goto, this confuses CFG. */
1662 || (JUMP_P (i3) && find_reg_note (i3, REG_NON_LOCAL_GOTO, NULL_RTX))
1663 /* Make sure that DEST is not used after SUCC but before I3. */
1664 || (succ && ! all_adjacent
1665 && reg_used_between_p (dest, succ, i3))
1666 /* Make sure that the value that is to be substituted for the register
1667 does not use any registers whose values alter in between. However,
1668 If the insns are adjacent, a use can't cross a set even though we
1669 think it might (this can happen for a sequence of insns each setting
1670 the same destination; last_set of that register might point to
1671 a NOTE). If INSN has a REG_EQUIV note, the register is always
1672 equivalent to the memory so the substitution is valid even if there
1673 are intervening stores. Also, don't move a volatile asm or
1674 UNSPEC_VOLATILE across any other insns. */
1675 || (! all_adjacent
1676 && (((!MEM_P (src)
1677 || ! find_reg_note (insn, REG_EQUIV, src))
1678 && use_crosses_set_p (src, DF_INSN_LUID (insn)))
1679 || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
1680 || GET_CODE (src) == UNSPEC_VOLATILE))
1681 /* Don't combine across a CALL_INSN, because that would possibly
1682 change whether the life span of some REGs crosses calls or not,
1683 and it is a pain to update that information.
1684 Exception: if source is a constant, moving it later can't hurt.
1685 Accept that as a special case. */
1686 || (DF_INSN_LUID (insn) < last_call_luid && ! CONSTANT_P (src)))
1687 return 0;
1689 /* DEST must either be a REG or CC0. */
1690 if (REG_P (dest))
1692 /* If register alignment is being enforced for multi-word items in all
1693 cases except for parameters, it is possible to have a register copy
1694 insn referencing a hard register that is not allowed to contain the
1695 mode being copied and which would not be valid as an operand of most
1696 insns. Eliminate this problem by not combining with such an insn.
1698 Also, on some machines we don't want to extend the life of a hard
1699 register. */
1701 if (REG_P (src)
1702 && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
1703 && ! HARD_REGNO_MODE_OK (REGNO (dest), GET_MODE (dest)))
1704 /* Don't extend the life of a hard register unless it is
1705 user variable (if we have few registers) or it can't
1706 fit into the desired register (meaning something special
1707 is going on).
1708 Also avoid substituting a return register into I3, because
1709 reload can't handle a conflict with constraints of other
1710 inputs. */
1711 || (REGNO (src) < FIRST_PSEUDO_REGISTER
1712 && ! HARD_REGNO_MODE_OK (REGNO (src), GET_MODE (src)))))
1713 return 0;
1715 else if (GET_CODE (dest) != CC0)
1716 return 0;
1719 if (GET_CODE (PATTERN (i3)) == PARALLEL)
1720 for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
1721 if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER)
1723 /* Don't substitute for a register intended as a clobberable
1724 operand. */
1725 rtx reg = XEXP (XVECEXP (PATTERN (i3), 0, i), 0);
1726 if (rtx_equal_p (reg, dest))
1727 return 0;
1729 /* If the clobber represents an earlyclobber operand, we must not
1730 substitute an expression containing the clobbered register.
1731 As we do not analyze the constraint strings here, we have to
1732 make the conservative assumption. However, if the register is
1733 a fixed hard reg, the clobber cannot represent any operand;
1734 we leave it up to the machine description to either accept or
1735 reject use-and-clobber patterns. */
1736 if (!REG_P (reg)
1737 || REGNO (reg) >= FIRST_PSEUDO_REGISTER
1738 || !fixed_regs[REGNO (reg)])
1739 if (reg_overlap_mentioned_p (reg, src))
1740 return 0;
1743 /* If INSN contains anything volatile, or is an `asm' (whether volatile
1744 or not), reject, unless nothing volatile comes between it and I3 */
1746 if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
1748 /* Make sure succ doesn't contain a volatile reference. */
1749 if (succ != 0 && volatile_refs_p (PATTERN (succ)))
1750 return 0;
1752 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1753 if (INSN_P (p) && p != succ && volatile_refs_p (PATTERN (p)))
1754 return 0;
1757 /* If INSN is an asm, and DEST is a hard register, reject, since it has
1758 to be an explicit register variable, and was chosen for a reason. */
1760 if (GET_CODE (src) == ASM_OPERANDS
1761 && REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER)
1762 return 0;
1764 /* If there are any volatile insns between INSN and I3, reject, because
1765 they might affect machine state. */
1767 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
1768 if (INSN_P (p) && p != succ && volatile_insn_p (PATTERN (p)))
1769 return 0;
1771 /* If INSN contains an autoincrement or autodecrement, make sure that
1772 register is not used between there and I3, and not already used in
1773 I3 either. Neither must it be used in PRED or SUCC, if they exist.
1774 Also insist that I3 not be a jump; if it were one
1775 and the incremented register were spilled, we would lose. */
1777 #ifdef AUTO_INC_DEC
1778 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1779 if (REG_NOTE_KIND (link) == REG_INC
1780 && (JUMP_P (i3)
1781 || reg_used_between_p (XEXP (link, 0), insn, i3)
1782 || (pred != NULL_RTX
1783 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred)))
1784 || (succ != NULL_RTX
1785 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ)))
1786 || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
1787 return 0;
1788 #endif
1790 #ifdef HAVE_cc0
1791 /* Don't combine an insn that follows a CC0-setting insn.
1792 An insn that uses CC0 must not be separated from the one that sets it.
1793 We do, however, allow I2 to follow a CC0-setting insn if that insn
1794 is passed as I1; in that case it will be deleted also.
1795 We also allow combining in this case if all the insns are adjacent
1796 because that would leave the two CC0 insns adjacent as well.
1797 It would be more logical to test whether CC0 occurs inside I1 or I2,
1798 but that would be much slower, and this ought to be equivalent. */
1800 p = prev_nonnote_insn (insn);
1801 if (p && p != pred && NONJUMP_INSN_P (p) && sets_cc0_p (PATTERN (p))
1802 && ! all_adjacent)
1803 return 0;
1804 #endif
1806 /* If we get here, we have passed all the tests and the combination is
1807 to be allowed. */
1809 *pdest = dest;
1810 *psrc = src;
1812 return 1;
1815 /* LOC is the location within I3 that contains its pattern or the component
1816 of a PARALLEL of the pattern. We validate that it is valid for combining.
1818 One problem is if I3 modifies its output, as opposed to replacing it
1819 entirely, we can't allow the output to contain I2DEST or I1DEST as doing
1820 so would produce an insn that is not equivalent to the original insns.
1822 Consider:
1824 (set (reg:DI 101) (reg:DI 100))
1825 (set (subreg:SI (reg:DI 101) 0) <foo>)
1827 This is NOT equivalent to:
1829 (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
1830 (set (reg:DI 101) (reg:DI 100))])
1832 Not only does this modify 100 (in which case it might still be valid
1833 if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
1835 We can also run into a problem if I2 sets a register that I1
1836 uses and I1 gets directly substituted into I3 (not via I2). In that
1837 case, we would be getting the wrong value of I2DEST into I3, so we
1838 must reject the combination. This case occurs when I2 and I1 both
1839 feed into I3, rather than when I1 feeds into I2, which feeds into I3.
1840 If I1_NOT_IN_SRC is nonzero, it means that finding I1 in the source
1841 of a SET must prevent combination from occurring.
1843 Before doing the above check, we first try to expand a field assignment
1844 into a set of logical operations.
1846 If PI3_DEST_KILLED is nonzero, it is a pointer to a location in which
1847 we place a register that is both set and used within I3. If more than one
1848 such register is detected, we fail.
1850 Return 1 if the combination is valid, zero otherwise. */
1852 static int
1853 combinable_i3pat (rtx i3, rtx *loc, rtx i2dest, rtx i1dest,
1854 int i1_not_in_src, rtx *pi3dest_killed)
1856 rtx x = *loc;
1858 if (GET_CODE (x) == SET)
1860 rtx set = x ;
1861 rtx dest = SET_DEST (set);
1862 rtx src = SET_SRC (set);
1863 rtx inner_dest = dest;
1864 rtx subdest;
1866 while (GET_CODE (inner_dest) == STRICT_LOW_PART
1867 || GET_CODE (inner_dest) == SUBREG
1868 || GET_CODE (inner_dest) == ZERO_EXTRACT)
1869 inner_dest = XEXP (inner_dest, 0);
1871 /* Check for the case where I3 modifies its output, as discussed
1872 above. We don't want to prevent pseudos from being combined
1873 into the address of a MEM, so only prevent the combination if
1874 i1 or i2 set the same MEM. */
1875 if ((inner_dest != dest &&
1876 (!MEM_P (inner_dest)
1877 || rtx_equal_p (i2dest, inner_dest)
1878 || (i1dest && rtx_equal_p (i1dest, inner_dest)))
1879 && (reg_overlap_mentioned_p (i2dest, inner_dest)
1880 || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))))
1882 /* This is the same test done in can_combine_p except we can't test
1883 all_adjacent; we don't have to, since this instruction will stay
1884 in place, thus we are not considering increasing the lifetime of
1885 INNER_DEST.
1887 Also, if this insn sets a function argument, combining it with
1888 something that might need a spill could clobber a previous
1889 function argument; the all_adjacent test in can_combine_p also
1890 checks this; here, we do a more specific test for this case. */
1892 || (REG_P (inner_dest)
1893 && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
1894 && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
1895 GET_MODE (inner_dest))))
1896 || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src)))
1897 return 0;
1899 /* If DEST is used in I3, it is being killed in this insn, so
1900 record that for later. We have to consider paradoxical
1901 subregs here, since they kill the whole register, but we
1902 ignore partial subregs, STRICT_LOW_PART, etc.
1903 Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
1904 STACK_POINTER_REGNUM, since these are always considered to be
1905 live. Similarly for ARG_POINTER_REGNUM if it is fixed. */
1906 subdest = dest;
1907 if (GET_CODE (subdest) == SUBREG
1908 && (GET_MODE_SIZE (GET_MODE (subdest))
1909 >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (subdest)))))
1910 subdest = SUBREG_REG (subdest);
1911 if (pi3dest_killed
1912 && REG_P (subdest)
1913 && reg_referenced_p (subdest, PATTERN (i3))
1914 && REGNO (subdest) != FRAME_POINTER_REGNUM
1915 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
1916 && REGNO (subdest) != HARD_FRAME_POINTER_REGNUM
1917 #endif
1918 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
1919 && (REGNO (subdest) != ARG_POINTER_REGNUM
1920 || ! fixed_regs [REGNO (subdest)])
1921 #endif
1922 && REGNO (subdest) != STACK_POINTER_REGNUM)
1924 if (*pi3dest_killed)
1925 return 0;
1927 *pi3dest_killed = subdest;
1931 else if (GET_CODE (x) == PARALLEL)
1933 int i;
1935 for (i = 0; i < XVECLEN (x, 0); i++)
1936 if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest,
1937 i1_not_in_src, pi3dest_killed))
1938 return 0;
1941 return 1;
1944 /* Return 1 if X is an arithmetic expression that contains a multiplication
1945 and division. We don't count multiplications by powers of two here. */
1947 static int
1948 contains_muldiv (rtx x)
1950 switch (GET_CODE (x))
1952 case MOD: case DIV: case UMOD: case UDIV:
1953 return 1;
1955 case MULT:
1956 return ! (GET_CODE (XEXP (x, 1)) == CONST_INT
1957 && exact_log2 (INTVAL (XEXP (x, 1))) >= 0);
1958 default:
1959 if (BINARY_P (x))
1960 return contains_muldiv (XEXP (x, 0))
1961 || contains_muldiv (XEXP (x, 1));
1963 if (UNARY_P (x))
1964 return contains_muldiv (XEXP (x, 0));
1966 return 0;
1970 /* Determine whether INSN can be used in a combination. Return nonzero if
1971 not. This is used in try_combine to detect early some cases where we
1972 can't perform combinations. */
1974 static int
1975 cant_combine_insn_p (rtx insn)
1977 rtx set;
1978 rtx src, dest;
1980 /* If this isn't really an insn, we can't do anything.
1981 This can occur when flow deletes an insn that it has merged into an
1982 auto-increment address. */
1983 if (! INSN_P (insn))
1984 return 1;
1986 /* Never combine loads and stores involving hard regs that are likely
1987 to be spilled. The register allocator can usually handle such
1988 reg-reg moves by tying. If we allow the combiner to make
1989 substitutions of likely-spilled regs, reload might die.
1990 As an exception, we allow combinations involving fixed regs; these are
1991 not available to the register allocator so there's no risk involved. */
1993 set = single_set (insn);
1994 if (! set)
1995 return 0;
1996 src = SET_SRC (set);
1997 dest = SET_DEST (set);
1998 if (GET_CODE (src) == SUBREG)
1999 src = SUBREG_REG (src);
2000 if (GET_CODE (dest) == SUBREG)
2001 dest = SUBREG_REG (dest);
2002 if (REG_P (src) && REG_P (dest)
2003 && ((REGNO (src) < FIRST_PSEUDO_REGISTER
2004 && ! fixed_regs[REGNO (src)]
2005 && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (REGNO (src))))
2006 || (REGNO (dest) < FIRST_PSEUDO_REGISTER
2007 && ! fixed_regs[REGNO (dest)]
2008 && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (REGNO (dest))))))
2009 return 1;
2011 return 0;
2014 struct likely_spilled_retval_info
2016 unsigned regno, nregs;
2017 unsigned mask;
2020 /* Called via note_stores by likely_spilled_retval_p. Remove from info->mask
2021 hard registers that are known to be written to / clobbered in full. */
2022 static void
2023 likely_spilled_retval_1 (rtx x, const_rtx set, void *data)
2025 struct likely_spilled_retval_info *const info =
2026 (struct likely_spilled_retval_info *) data;
2027 unsigned regno, nregs;
2028 unsigned new_mask;
2030 if (!REG_P (XEXP (set, 0)))
2031 return;
2032 regno = REGNO (x);
2033 if (regno >= info->regno + info->nregs)
2034 return;
2035 nregs = hard_regno_nregs[regno][GET_MODE (x)];
2036 if (regno + nregs <= info->regno)
2037 return;
2038 new_mask = (2U << (nregs - 1)) - 1;
2039 if (regno < info->regno)
2040 new_mask >>= info->regno - regno;
2041 else
2042 new_mask <<= regno - info->regno;
2043 info->mask &= ~new_mask;
2046 /* Return nonzero iff part of the return value is live during INSN, and
2047 it is likely spilled. This can happen when more than one insn is needed
2048 to copy the return value, e.g. when we consider to combine into the
2049 second copy insn for a complex value. */
2051 static int
2052 likely_spilled_retval_p (rtx insn)
2054 rtx use = BB_END (this_basic_block);
2055 rtx reg, p;
2056 unsigned regno, nregs;
2057 /* We assume here that no machine mode needs more than
2058 32 hard registers when the value overlaps with a register
2059 for which FUNCTION_VALUE_REGNO_P is true. */
2060 unsigned mask;
2061 struct likely_spilled_retval_info info;
2063 if (!NONJUMP_INSN_P (use) || GET_CODE (PATTERN (use)) != USE || insn == use)
2064 return 0;
2065 reg = XEXP (PATTERN (use), 0);
2066 if (!REG_P (reg) || !FUNCTION_VALUE_REGNO_P (REGNO (reg)))
2067 return 0;
2068 regno = REGNO (reg);
2069 nregs = hard_regno_nregs[regno][GET_MODE (reg)];
2070 if (nregs == 1)
2071 return 0;
2072 mask = (2U << (nregs - 1)) - 1;
2074 /* Disregard parts of the return value that are set later. */
2075 info.regno = regno;
2076 info.nregs = nregs;
2077 info.mask = mask;
2078 for (p = PREV_INSN (use); info.mask && p != insn; p = PREV_INSN (p))
2079 if (INSN_P (p))
2080 note_stores (PATTERN (p), likely_spilled_retval_1, &info);
2081 mask = info.mask;
2083 /* Check if any of the (probably) live return value registers is
2084 likely spilled. */
2085 nregs --;
2088 if ((mask & 1 << nregs)
2089 && CLASS_LIKELY_SPILLED_P (REGNO_REG_CLASS (regno + nregs)))
2090 return 1;
2091 } while (nregs--);
2092 return 0;
2095 /* Adjust INSN after we made a change to its destination.
2097 Changing the destination can invalidate notes that say something about
2098 the results of the insn and a LOG_LINK pointing to the insn. */
2100 static void
2101 adjust_for_new_dest (rtx insn)
2103 /* For notes, be conservative and simply remove them. */
2104 remove_reg_equal_equiv_notes (insn);
2106 /* The new insn will have a destination that was previously the destination
2107 of an insn just above it. Call distribute_links to make a LOG_LINK from
2108 the next use of that destination. */
2109 distribute_links (gen_rtx_INSN_LIST (VOIDmode, insn, NULL_RTX));
2111 df_insn_rescan (insn);
2114 /* Return TRUE if combine can reuse reg X in mode MODE.
2115 ADDED_SETS is nonzero if the original set is still required. */
2116 static bool
2117 can_change_dest_mode (rtx x, int added_sets, enum machine_mode mode)
2119 unsigned int regno;
2121 if (!REG_P(x))
2122 return false;
2124 regno = REGNO (x);
2125 /* Allow hard registers if the new mode is legal, and occupies no more
2126 registers than the old mode. */
2127 if (regno < FIRST_PSEUDO_REGISTER)
2128 return (HARD_REGNO_MODE_OK (regno, mode)
2129 && (hard_regno_nregs[regno][GET_MODE (x)]
2130 >= hard_regno_nregs[regno][mode]));
2132 /* Or a pseudo that is only used once. */
2133 return (REG_N_SETS (regno) == 1 && !added_sets
2134 && !REG_USERVAR_P (x));
2138 /* Check whether X, the destination of a set, refers to part of
2139 the register specified by REG. */
2141 static bool
2142 reg_subword_p (rtx x, rtx reg)
2144 /* Check that reg is an integer mode register. */
2145 if (!REG_P (reg) || GET_MODE_CLASS (GET_MODE (reg)) != MODE_INT)
2146 return false;
2148 if (GET_CODE (x) == STRICT_LOW_PART
2149 || GET_CODE (x) == ZERO_EXTRACT)
2150 x = XEXP (x, 0);
2152 return GET_CODE (x) == SUBREG
2153 && SUBREG_REG (x) == reg
2154 && GET_MODE_CLASS (GET_MODE (x)) == MODE_INT;
2158 /* Try to combine the insns I1 and I2 into I3.
2159 Here I1 and I2 appear earlier than I3.
2160 I1 can be zero; then we combine just I2 into I3.
2162 If we are combining three insns and the resulting insn is not recognized,
2163 try splitting it into two insns. If that happens, I2 and I3 are retained
2164 and I1 is pseudo-deleted by turning it into a NOTE. Otherwise, I1 and I2
2165 are pseudo-deleted.
2167 Return 0 if the combination does not work. Then nothing is changed.
2168 If we did the combination, return the insn at which combine should
2169 resume scanning.
2171 Set NEW_DIRECT_JUMP_P to a nonzero value if try_combine creates a
2172 new direct jump instruction. */
2174 static rtx
2175 try_combine (rtx i3, rtx i2, rtx i1, int *new_direct_jump_p)
2177 /* New patterns for I3 and I2, respectively. */
2178 rtx newpat, newi2pat = 0;
2179 rtvec newpat_vec_with_clobbers = 0;
2180 int substed_i2 = 0, substed_i1 = 0;
2181 /* Indicates need to preserve SET in I1 or I2 in I3 if it is not dead. */
2182 int added_sets_1, added_sets_2;
2183 /* Total number of SETs to put into I3. */
2184 int total_sets;
2185 /* Nonzero if I2's body now appears in I3. */
2186 int i2_is_used;
2187 /* INSN_CODEs for new I3, new I2, and user of condition code. */
2188 int insn_code_number, i2_code_number = 0, other_code_number = 0;
2189 /* Contains I3 if the destination of I3 is used in its source, which means
2190 that the old life of I3 is being killed. If that usage is placed into
2191 I2 and not in I3, a REG_DEAD note must be made. */
2192 rtx i3dest_killed = 0;
2193 /* SET_DEST and SET_SRC of I2 and I1. */
2194 rtx i2dest, i2src, i1dest = 0, i1src = 0;
2195 /* PATTERN (I1) and PATTERN (I2), or a copy of it in certain cases. */
2196 rtx i1pat = 0, i2pat = 0;
2197 /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC. */
2198 int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
2199 int i2dest_killed = 0, i1dest_killed = 0;
2200 int i1_feeds_i3 = 0;
2201 /* Notes that must be added to REG_NOTES in I3 and I2. */
2202 rtx new_i3_notes, new_i2_notes;
2203 /* Notes that we substituted I3 into I2 instead of the normal case. */
2204 int i3_subst_into_i2 = 0;
2205 /* Notes that I1, I2 or I3 is a MULT operation. */
2206 int have_mult = 0;
2207 int swap_i2i3 = 0;
2209 int maxreg;
2210 rtx temp;
2211 rtx link;
2212 rtx other_pat = 0;
2213 rtx new_other_notes;
2214 int i;
2216 /* Exit early if one of the insns involved can't be used for
2217 combinations. */
2218 if (cant_combine_insn_p (i3)
2219 || cant_combine_insn_p (i2)
2220 || (i1 && cant_combine_insn_p (i1))
2221 || likely_spilled_retval_p (i3))
2222 return 0;
2224 combine_attempts++;
2225 undobuf.other_insn = 0;
2227 /* Reset the hard register usage information. */
2228 CLEAR_HARD_REG_SET (newpat_used_regs);
2230 /* If I1 and I2 both feed I3, they can be in any order. To simplify the
2231 code below, set I1 to be the earlier of the two insns. */
2232 if (i1 && DF_INSN_LUID (i1) > DF_INSN_LUID (i2))
2233 temp = i1, i1 = i2, i2 = temp;
2235 added_links_insn = 0;
2237 /* First check for one important special-case that the code below will
2238 not handle. Namely, the case where I1 is zero, I2 is a PARALLEL
2239 and I3 is a SET whose SET_SRC is a SET_DEST in I2. In that case,
2240 we may be able to replace that destination with the destination of I3.
2241 This occurs in the common code where we compute both a quotient and
2242 remainder into a structure, in which case we want to do the computation
2243 directly into the structure to avoid register-register copies.
2245 Note that this case handles both multiple sets in I2 and also
2246 cases where I2 has a number of CLOBBER or PARALLELs.
2248 We make very conservative checks below and only try to handle the
2249 most common cases of this. For example, we only handle the case
2250 where I2 and I3 are adjacent to avoid making difficult register
2251 usage tests. */
2253 if (i1 == 0 && NONJUMP_INSN_P (i3) && GET_CODE (PATTERN (i3)) == SET
2254 && REG_P (SET_SRC (PATTERN (i3)))
2255 && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
2256 && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
2257 && GET_CODE (PATTERN (i2)) == PARALLEL
2258 && ! side_effects_p (SET_DEST (PATTERN (i3)))
2259 /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
2260 below would need to check what is inside (and reg_overlap_mentioned_p
2261 doesn't support those codes anyway). Don't allow those destinations;
2262 the resulting insn isn't likely to be recognized anyway. */
2263 && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
2264 && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
2265 && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
2266 SET_DEST (PATTERN (i3)))
2267 && next_real_insn (i2) == i3)
2269 rtx p2 = PATTERN (i2);
2271 /* Make sure that the destination of I3,
2272 which we are going to substitute into one output of I2,
2273 is not used within another output of I2. We must avoid making this:
2274 (parallel [(set (mem (reg 69)) ...)
2275 (set (reg 69) ...)])
2276 which is not well-defined as to order of actions.
2277 (Besides, reload can't handle output reloads for this.)
2279 The problem can also happen if the dest of I3 is a memory ref,
2280 if another dest in I2 is an indirect memory ref. */
2281 for (i = 0; i < XVECLEN (p2, 0); i++)
2282 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
2283 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
2284 && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
2285 SET_DEST (XVECEXP (p2, 0, i))))
2286 break;
2288 if (i == XVECLEN (p2, 0))
2289 for (i = 0; i < XVECLEN (p2, 0); i++)
2290 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
2291 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
2292 && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
2294 combine_merges++;
2296 subst_insn = i3;
2297 subst_low_luid = DF_INSN_LUID (i2);
2299 added_sets_2 = added_sets_1 = 0;
2300 i2dest = SET_SRC (PATTERN (i3));
2301 i2dest_killed = dead_or_set_p (i2, i2dest);
2303 /* Replace the dest in I2 with our dest and make the resulting
2304 insn the new pattern for I3. Then skip to where we
2305 validate the pattern. Everything was set up above. */
2306 SUBST (SET_DEST (XVECEXP (p2, 0, i)),
2307 SET_DEST (PATTERN (i3)));
2309 newpat = p2;
2310 i3_subst_into_i2 = 1;
2311 goto validate_replacement;
2315 /* If I2 is setting a pseudo to a constant and I3 is setting some
2316 sub-part of it to another constant, merge them by making a new
2317 constant. */
2318 if (i1 == 0
2319 && (temp = single_set (i2)) != 0
2320 && (GET_CODE (SET_SRC (temp)) == CONST_INT
2321 || GET_CODE (SET_SRC (temp)) == CONST_DOUBLE)
2322 && GET_CODE (PATTERN (i3)) == SET
2323 && (GET_CODE (SET_SRC (PATTERN (i3))) == CONST_INT
2324 || GET_CODE (SET_SRC (PATTERN (i3))) == CONST_DOUBLE)
2325 && reg_subword_p (SET_DEST (PATTERN (i3)), SET_DEST (temp)))
2327 rtx dest = SET_DEST (PATTERN (i3));
2328 int offset = -1;
2329 int width = 0;
2331 if (GET_CODE (dest) == ZERO_EXTRACT)
2333 if (GET_CODE (XEXP (dest, 1)) == CONST_INT
2334 && GET_CODE (XEXP (dest, 2)) == CONST_INT)
2336 width = INTVAL (XEXP (dest, 1));
2337 offset = INTVAL (XEXP (dest, 2));
2338 dest = XEXP (dest, 0);
2339 if (BITS_BIG_ENDIAN)
2340 offset = GET_MODE_BITSIZE (GET_MODE (dest)) - width - offset;
2343 else
2345 if (GET_CODE (dest) == STRICT_LOW_PART)
2346 dest = XEXP (dest, 0);
2347 width = GET_MODE_BITSIZE (GET_MODE (dest));
2348 offset = 0;
2351 if (offset >= 0)
2353 /* If this is the low part, we're done. */
2354 if (subreg_lowpart_p (dest))
2356 /* Handle the case where inner is twice the size of outer. */
2357 else if (GET_MODE_BITSIZE (GET_MODE (SET_DEST (temp)))
2358 == 2 * GET_MODE_BITSIZE (GET_MODE (dest)))
2359 offset += GET_MODE_BITSIZE (GET_MODE (dest));
2360 /* Otherwise give up for now. */
2361 else
2362 offset = -1;
2365 if (offset >= 0
2366 && (GET_MODE_BITSIZE (GET_MODE (SET_DEST (temp)))
2367 <= HOST_BITS_PER_WIDE_INT * 2))
2369 HOST_WIDE_INT mhi, ohi, ihi;
2370 HOST_WIDE_INT mlo, olo, ilo;
2371 rtx inner = SET_SRC (PATTERN (i3));
2372 rtx outer = SET_SRC (temp);
2374 if (GET_CODE (outer) == CONST_INT)
2376 olo = INTVAL (outer);
2377 ohi = olo < 0 ? -1 : 0;
2379 else
2381 olo = CONST_DOUBLE_LOW (outer);
2382 ohi = CONST_DOUBLE_HIGH (outer);
2385 if (GET_CODE (inner) == CONST_INT)
2387 ilo = INTVAL (inner);
2388 ihi = ilo < 0 ? -1 : 0;
2390 else
2392 ilo = CONST_DOUBLE_LOW (inner);
2393 ihi = CONST_DOUBLE_HIGH (inner);
2396 if (width < HOST_BITS_PER_WIDE_INT)
2398 mlo = ((unsigned HOST_WIDE_INT) 1 << width) - 1;
2399 mhi = 0;
2401 else if (width < HOST_BITS_PER_WIDE_INT * 2)
2403 mhi = ((unsigned HOST_WIDE_INT) 1
2404 << (width - HOST_BITS_PER_WIDE_INT)) - 1;
2405 mlo = -1;
2407 else
2409 mlo = -1;
2410 mhi = -1;
2413 ilo &= mlo;
2414 ihi &= mhi;
2416 if (offset >= HOST_BITS_PER_WIDE_INT)
2418 mhi = mlo << (offset - HOST_BITS_PER_WIDE_INT);
2419 mlo = 0;
2420 ihi = ilo << (offset - HOST_BITS_PER_WIDE_INT);
2421 ilo = 0;
2423 else if (offset > 0)
2425 mhi = (mhi << offset) | ((unsigned HOST_WIDE_INT) mlo
2426 >> (HOST_BITS_PER_WIDE_INT - offset));
2427 mlo = mlo << offset;
2428 ihi = (ihi << offset) | ((unsigned HOST_WIDE_INT) ilo
2429 >> (HOST_BITS_PER_WIDE_INT - offset));
2430 ilo = ilo << offset;
2433 olo = (olo & ~mlo) | ilo;
2434 ohi = (ohi & ~mhi) | ihi;
2436 combine_merges++;
2437 subst_insn = i3;
2438 subst_low_luid = DF_INSN_LUID (i2);
2439 added_sets_2 = added_sets_1 = 0;
2440 i2dest = SET_DEST (temp);
2441 i2dest_killed = dead_or_set_p (i2, i2dest);
2443 SUBST (SET_SRC (temp),
2444 immed_double_const (olo, ohi, GET_MODE (SET_DEST (temp))));
2446 newpat = PATTERN (i2);
2447 goto validate_replacement;
2451 #ifndef HAVE_cc0
2452 /* If we have no I1 and I2 looks like:
2453 (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
2454 (set Y OP)])
2455 make up a dummy I1 that is
2456 (set Y OP)
2457 and change I2 to be
2458 (set (reg:CC X) (compare:CC Y (const_int 0)))
2460 (We can ignore any trailing CLOBBERs.)
2462 This undoes a previous combination and allows us to match a branch-and-
2463 decrement insn. */
2465 if (i1 == 0 && GET_CODE (PATTERN (i2)) == PARALLEL
2466 && XVECLEN (PATTERN (i2), 0) >= 2
2467 && GET_CODE (XVECEXP (PATTERN (i2), 0, 0)) == SET
2468 && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
2469 == MODE_CC)
2470 && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
2471 && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
2472 && GET_CODE (XVECEXP (PATTERN (i2), 0, 1)) == SET
2473 && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)))
2474 && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
2475 SET_SRC (XVECEXP (PATTERN (i2), 0, 1))))
2477 for (i = XVECLEN (PATTERN (i2), 0) - 1; i >= 2; i--)
2478 if (GET_CODE (XVECEXP (PATTERN (i2), 0, i)) != CLOBBER)
2479 break;
2481 if (i == 1)
2483 /* We make I1 with the same INSN_UID as I2. This gives it
2484 the same DF_INSN_LUID for value tracking. Our fake I1 will
2485 never appear in the insn stream so giving it the same INSN_UID
2486 as I2 will not cause a problem. */
2488 i1 = gen_rtx_INSN (VOIDmode, INSN_UID (i2), NULL_RTX, i2,
2489 BLOCK_FOR_INSN (i2), INSN_LOCATOR (i2),
2490 XVECEXP (PATTERN (i2), 0, 1), -1, NULL_RTX);
2492 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
2493 SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
2494 SET_DEST (PATTERN (i1)));
2497 #endif
2499 /* Verify that I2 and I1 are valid for combining. */
2500 if (! can_combine_p (i2, i3, i1, NULL_RTX, &i2dest, &i2src)
2501 || (i1 && ! can_combine_p (i1, i3, NULL_RTX, i2, &i1dest, &i1src)))
2503 undo_all ();
2504 return 0;
2507 /* Record whether I2DEST is used in I2SRC and similarly for the other
2508 cases. Knowing this will help in register status updating below. */
2509 i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
2510 i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
2511 i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
2512 i2dest_killed = dead_or_set_p (i2, i2dest);
2513 i1dest_killed = i1 && dead_or_set_p (i1, i1dest);
2515 /* See if I1 directly feeds into I3. It does if I1DEST is not used
2516 in I2SRC. */
2517 i1_feeds_i3 = i1 && ! reg_overlap_mentioned_p (i1dest, i2src);
2519 /* Ensure that I3's pattern can be the destination of combines. */
2520 if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest,
2521 i1 && i2dest_in_i1src && i1_feeds_i3,
2522 &i3dest_killed))
2524 undo_all ();
2525 return 0;
2528 /* See if any of the insns is a MULT operation. Unless one is, we will
2529 reject a combination that is, since it must be slower. Be conservative
2530 here. */
2531 if (GET_CODE (i2src) == MULT
2532 || (i1 != 0 && GET_CODE (i1src) == MULT)
2533 || (GET_CODE (PATTERN (i3)) == SET
2534 && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
2535 have_mult = 1;
2537 /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
2538 We used to do this EXCEPT in one case: I3 has a post-inc in an
2539 output operand. However, that exception can give rise to insns like
2540 mov r3,(r3)+
2541 which is a famous insn on the PDP-11 where the value of r3 used as the
2542 source was model-dependent. Avoid this sort of thing. */
2544 #if 0
2545 if (!(GET_CODE (PATTERN (i3)) == SET
2546 && REG_P (SET_SRC (PATTERN (i3)))
2547 && MEM_P (SET_DEST (PATTERN (i3)))
2548 && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
2549 || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
2550 /* It's not the exception. */
2551 #endif
2552 #ifdef AUTO_INC_DEC
2553 for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
2554 if (REG_NOTE_KIND (link) == REG_INC
2555 && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
2556 || (i1 != 0
2557 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
2559 undo_all ();
2560 return 0;
2562 #endif
2564 /* See if the SETs in I1 or I2 need to be kept around in the merged
2565 instruction: whenever the value set there is still needed past I3.
2566 For the SETs in I2, this is easy: we see if I2DEST dies or is set in I3.
2568 For the SET in I1, we have two cases: If I1 and I2 independently
2569 feed into I3, the set in I1 needs to be kept around if I1DEST dies
2570 or is set in I3. Otherwise (if I1 feeds I2 which feeds I3), the set
2571 in I1 needs to be kept around unless I1DEST dies or is set in either
2572 I2 or I3. We can distinguish these cases by seeing if I2SRC mentions
2573 I1DEST. If so, we know I1 feeds into I2. */
2575 added_sets_2 = ! dead_or_set_p (i3, i2dest);
2577 added_sets_1
2578 = i1 && ! (i1_feeds_i3 ? dead_or_set_p (i3, i1dest)
2579 : (dead_or_set_p (i3, i1dest) || dead_or_set_p (i2, i1dest)));
2581 /* If the set in I2 needs to be kept around, we must make a copy of
2582 PATTERN (I2), so that when we substitute I1SRC for I1DEST in
2583 PATTERN (I2), we are only substituting for the original I1DEST, not into
2584 an already-substituted copy. This also prevents making self-referential
2585 rtx. If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
2586 I2DEST. */
2588 if (added_sets_2)
2590 if (GET_CODE (PATTERN (i2)) == PARALLEL)
2591 i2pat = gen_rtx_SET (VOIDmode, i2dest, copy_rtx (i2src));
2592 else
2593 i2pat = copy_rtx (PATTERN (i2));
2596 if (added_sets_1)
2598 if (GET_CODE (PATTERN (i1)) == PARALLEL)
2599 i1pat = gen_rtx_SET (VOIDmode, i1dest, copy_rtx (i1src));
2600 else
2601 i1pat = copy_rtx (PATTERN (i1));
2604 combine_merges++;
2606 /* Substitute in the latest insn for the regs set by the earlier ones. */
2608 maxreg = max_reg_num ();
2610 subst_insn = i3;
2612 #ifndef HAVE_cc0
2613 /* Many machines that don't use CC0 have insns that can both perform an
2614 arithmetic operation and set the condition code. These operations will
2615 be represented as a PARALLEL with the first element of the vector
2616 being a COMPARE of an arithmetic operation with the constant zero.
2617 The second element of the vector will set some pseudo to the result
2618 of the same arithmetic operation. If we simplify the COMPARE, we won't
2619 match such a pattern and so will generate an extra insn. Here we test
2620 for this case, where both the comparison and the operation result are
2621 needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
2622 I2SRC. Later we will make the PARALLEL that contains I2. */
2624 if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
2625 && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
2626 && XEXP (SET_SRC (PATTERN (i3)), 1) == const0_rtx
2627 && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
2629 #ifdef SELECT_CC_MODE
2630 rtx *cc_use;
2631 enum machine_mode compare_mode;
2632 #endif
2634 newpat = PATTERN (i3);
2635 SUBST (XEXP (SET_SRC (newpat), 0), i2src);
2637 i2_is_used = 1;
2639 #ifdef SELECT_CC_MODE
2640 /* See if a COMPARE with the operand we substituted in should be done
2641 with the mode that is currently being used. If not, do the same
2642 processing we do in `subst' for a SET; namely, if the destination
2643 is used only once, try to replace it with a register of the proper
2644 mode and also replace the COMPARE. */
2645 if (undobuf.other_insn == 0
2646 && (cc_use = find_single_use (SET_DEST (newpat), i3,
2647 &undobuf.other_insn))
2648 && ((compare_mode = SELECT_CC_MODE (GET_CODE (*cc_use),
2649 i2src, const0_rtx))
2650 != GET_MODE (SET_DEST (newpat))))
2652 if (can_change_dest_mode(SET_DEST (newpat), added_sets_2,
2653 compare_mode))
2655 unsigned int regno = REGNO (SET_DEST (newpat));
2656 rtx new_dest;
2658 if (regno < FIRST_PSEUDO_REGISTER)
2659 new_dest = gen_rtx_REG (compare_mode, regno);
2660 else
2662 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
2663 new_dest = regno_reg_rtx[regno];
2666 SUBST (SET_DEST (newpat), new_dest);
2667 SUBST (XEXP (*cc_use, 0), new_dest);
2668 SUBST (SET_SRC (newpat),
2669 gen_rtx_COMPARE (compare_mode, i2src, const0_rtx));
2671 else
2672 undobuf.other_insn = 0;
2674 #endif
2676 else
2677 #endif
2679 /* It is possible that the source of I2 or I1 may be performing
2680 an unneeded operation, such as a ZERO_EXTEND of something
2681 that is known to have the high part zero. Handle that case
2682 by letting subst look at the innermost one of them.
2684 Another way to do this would be to have a function that tries
2685 to simplify a single insn instead of merging two or more
2686 insns. We don't do this because of the potential of infinite
2687 loops and because of the potential extra memory required.
2688 However, doing it the way we are is a bit of a kludge and
2689 doesn't catch all cases.
2691 But only do this if -fexpensive-optimizations since it slows
2692 things down and doesn't usually win.
2694 This is not done in the COMPARE case above because the
2695 unmodified I2PAT is used in the PARALLEL and so a pattern
2696 with a modified I2SRC would not match. */
2698 if (flag_expensive_optimizations)
2700 /* Pass pc_rtx so no substitutions are done, just
2701 simplifications. */
2702 if (i1)
2704 subst_low_luid = DF_INSN_LUID (i1);
2705 i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0);
2707 else
2709 subst_low_luid = DF_INSN_LUID (i2);
2710 i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0);
2714 n_occurrences = 0; /* `subst' counts here */
2716 /* If I1 feeds into I2 (not into I3) and I1DEST is in I1SRC, we
2717 need to make a unique copy of I2SRC each time we substitute it
2718 to avoid self-referential rtl. */
2720 subst_low_luid = DF_INSN_LUID (i2);
2721 newpat = subst (PATTERN (i3), i2dest, i2src, 0,
2722 ! i1_feeds_i3 && i1dest_in_i1src);
2723 substed_i2 = 1;
2725 /* Record whether i2's body now appears within i3's body. */
2726 i2_is_used = n_occurrences;
2729 /* If we already got a failure, don't try to do more. Otherwise,
2730 try to substitute in I1 if we have it. */
2732 if (i1 && GET_CODE (newpat) != CLOBBER)
2734 /* Check that an autoincrement side-effect on I1 has not been lost.
2735 This happens if I1DEST is mentioned in I2 and dies there, and
2736 has disappeared from the new pattern. */
2737 if ((FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
2738 && !i1_feeds_i3
2739 && dead_or_set_p (i2, i1dest)
2740 && !reg_overlap_mentioned_p (i1dest, newpat))
2741 /* Before we can do this substitution, we must redo the test done
2742 above (see detailed comments there) that ensures that I1DEST
2743 isn't mentioned in any SETs in NEWPAT that are field assignments. */
2744 || !combinable_i3pat (NULL_RTX, &newpat, i1dest, NULL_RTX, 0, 0))
2746 undo_all ();
2747 return 0;
2750 n_occurrences = 0;
2751 subst_low_luid = DF_INSN_LUID (i1);
2752 newpat = subst (newpat, i1dest, i1src, 0, 0);
2753 substed_i1 = 1;
2756 /* Fail if an autoincrement side-effect has been duplicated. Be careful
2757 to count all the ways that I2SRC and I1SRC can be used. */
2758 if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
2759 && i2_is_used + added_sets_2 > 1)
2760 || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
2761 && (n_occurrences + added_sets_1 + (added_sets_2 && ! i1_feeds_i3)
2762 > 1))
2763 /* Fail if we tried to make a new register. */
2764 || max_reg_num () != maxreg
2765 /* Fail if we couldn't do something and have a CLOBBER. */
2766 || GET_CODE (newpat) == CLOBBER
2767 /* Fail if this new pattern is a MULT and we didn't have one before
2768 at the outer level. */
2769 || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
2770 && ! have_mult))
2772 undo_all ();
2773 return 0;
2776 /* If the actions of the earlier insns must be kept
2777 in addition to substituting them into the latest one,
2778 we must make a new PARALLEL for the latest insn
2779 to hold additional the SETs. */
2781 if (added_sets_1 || added_sets_2)
2783 combine_extras++;
2785 if (GET_CODE (newpat) == PARALLEL)
2787 rtvec old = XVEC (newpat, 0);
2788 total_sets = XVECLEN (newpat, 0) + added_sets_1 + added_sets_2;
2789 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
2790 memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
2791 sizeof (old->elem[0]) * old->num_elem);
2793 else
2795 rtx old = newpat;
2796 total_sets = 1 + added_sets_1 + added_sets_2;
2797 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
2798 XVECEXP (newpat, 0, 0) = old;
2801 if (added_sets_1)
2802 XVECEXP (newpat, 0, --total_sets) = i1pat;
2804 if (added_sets_2)
2806 /* If there is no I1, use I2's body as is. We used to also not do
2807 the subst call below if I2 was substituted into I3,
2808 but that could lose a simplification. */
2809 if (i1 == 0)
2810 XVECEXP (newpat, 0, --total_sets) = i2pat;
2811 else
2812 /* See comment where i2pat is assigned. */
2813 XVECEXP (newpat, 0, --total_sets)
2814 = subst (i2pat, i1dest, i1src, 0, 0);
2818 /* We come here when we are replacing a destination in I2 with the
2819 destination of I3. */
2820 validate_replacement:
2822 /* Note which hard regs this insn has as inputs. */
2823 mark_used_regs_combine (newpat);
2825 /* If recog_for_combine fails, it strips existing clobbers. If we'll
2826 consider splitting this pattern, we might need these clobbers. */
2827 if (i1 && GET_CODE (newpat) == PARALLEL
2828 && GET_CODE (XVECEXP (newpat, 0, XVECLEN (newpat, 0) - 1)) == CLOBBER)
2830 int len = XVECLEN (newpat, 0);
2832 newpat_vec_with_clobbers = rtvec_alloc (len);
2833 for (i = 0; i < len; i++)
2834 RTVEC_ELT (newpat_vec_with_clobbers, i) = XVECEXP (newpat, 0, i);
2837 /* Is the result of combination a valid instruction? */
2838 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2840 /* If the result isn't valid, see if it is a PARALLEL of two SETs where
2841 the second SET's destination is a register that is unused and isn't
2842 marked as an instruction that might trap in an EH region. In that case,
2843 we just need the first SET. This can occur when simplifying a divmod
2844 insn. We *must* test for this case here because the code below that
2845 splits two independent SETs doesn't handle this case correctly when it
2846 updates the register status.
2848 It's pointless doing this if we originally had two sets, one from
2849 i3, and one from i2. Combining then splitting the parallel results
2850 in the original i2 again plus an invalid insn (which we delete).
2851 The net effect is only to move instructions around, which makes
2852 debug info less accurate.
2854 Also check the case where the first SET's destination is unused.
2855 That would not cause incorrect code, but does cause an unneeded
2856 insn to remain. */
2858 if (insn_code_number < 0
2859 && !(added_sets_2 && i1 == 0)
2860 && GET_CODE (newpat) == PARALLEL
2861 && XVECLEN (newpat, 0) == 2
2862 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
2863 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
2864 && asm_noperands (newpat) < 0)
2866 rtx set0 = XVECEXP (newpat, 0, 0);
2867 rtx set1 = XVECEXP (newpat, 0, 1);
2868 rtx note;
2870 if (((REG_P (SET_DEST (set1))
2871 && find_reg_note (i3, REG_UNUSED, SET_DEST (set1)))
2872 || (GET_CODE (SET_DEST (set1)) == SUBREG
2873 && find_reg_note (i3, REG_UNUSED, SUBREG_REG (SET_DEST (set1)))))
2874 && (!(note = find_reg_note (i3, REG_EH_REGION, NULL_RTX))
2875 || INTVAL (XEXP (note, 0)) <= 0)
2876 && ! side_effects_p (SET_SRC (set1)))
2878 newpat = set0;
2879 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2882 else if (((REG_P (SET_DEST (set0))
2883 && find_reg_note (i3, REG_UNUSED, SET_DEST (set0)))
2884 || (GET_CODE (SET_DEST (set0)) == SUBREG
2885 && find_reg_note (i3, REG_UNUSED,
2886 SUBREG_REG (SET_DEST (set0)))))
2887 && (!(note = find_reg_note (i3, REG_EH_REGION, NULL_RTX))
2888 || INTVAL (XEXP (note, 0)) <= 0)
2889 && ! side_effects_p (SET_SRC (set0)))
2891 newpat = set1;
2892 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
2894 if (insn_code_number >= 0)
2896 /* If we will be able to accept this, we have made a
2897 change to the destination of I3. This requires us to
2898 do a few adjustments. */
2900 PATTERN (i3) = newpat;
2901 adjust_for_new_dest (i3);
2906 /* If we were combining three insns and the result is a simple SET
2907 with no ASM_OPERANDS that wasn't recognized, try to split it into two
2908 insns. There are two ways to do this. It can be split using a
2909 machine-specific method (like when you have an addition of a large
2910 constant) or by combine in the function find_split_point. */
2912 if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
2913 && asm_noperands (newpat) < 0)
2915 rtx parallel, m_split, *split;
2917 /* See if the MD file can split NEWPAT. If it can't, see if letting it
2918 use I2DEST as a scratch register will help. In the latter case,
2919 convert I2DEST to the mode of the source of NEWPAT if we can. */
2921 m_split = combine_split_insns (newpat, i3);
2923 /* We can only use I2DEST as a scratch reg if it doesn't overlap any
2924 inputs of NEWPAT. */
2926 /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
2927 possible to try that as a scratch reg. This would require adding
2928 more code to make it work though. */
2930 if (m_split == 0 && ! reg_overlap_mentioned_p (i2dest, newpat))
2932 enum machine_mode new_mode = GET_MODE (SET_DEST (newpat));
2934 /* First try to split using the original register as a
2935 scratch register. */
2936 parallel = gen_rtx_PARALLEL (VOIDmode,
2937 gen_rtvec (2, newpat,
2938 gen_rtx_CLOBBER (VOIDmode,
2939 i2dest)));
2940 m_split = combine_split_insns (parallel, i3);
2942 /* If that didn't work, try changing the mode of I2DEST if
2943 we can. */
2944 if (m_split == 0
2945 && new_mode != GET_MODE (i2dest)
2946 && new_mode != VOIDmode
2947 && can_change_dest_mode (i2dest, added_sets_2, new_mode))
2949 enum machine_mode old_mode = GET_MODE (i2dest);
2950 rtx ni2dest;
2952 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
2953 ni2dest = gen_rtx_REG (new_mode, REGNO (i2dest));
2954 else
2956 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], new_mode);
2957 ni2dest = regno_reg_rtx[REGNO (i2dest)];
2960 parallel = (gen_rtx_PARALLEL
2961 (VOIDmode,
2962 gen_rtvec (2, newpat,
2963 gen_rtx_CLOBBER (VOIDmode,
2964 ni2dest))));
2965 m_split = combine_split_insns (parallel, i3);
2967 if (m_split == 0
2968 && REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
2970 struct undo *buf;
2972 adjust_reg_mode (regno_reg_rtx[REGNO (i2dest)], old_mode);
2973 buf = undobuf.undos;
2974 undobuf.undos = buf->next;
2975 buf->next = undobuf.frees;
2976 undobuf.frees = buf;
2981 /* If recog_for_combine has discarded clobbers, try to use them
2982 again for the split. */
2983 if (m_split == 0 && newpat_vec_with_clobbers)
2985 parallel = gen_rtx_PARALLEL (VOIDmode, newpat_vec_with_clobbers);
2986 m_split = combine_split_insns (parallel, i3);
2989 if (m_split && NEXT_INSN (m_split) == NULL_RTX)
2991 m_split = PATTERN (m_split);
2992 insn_code_number = recog_for_combine (&m_split, i3, &new_i3_notes);
2993 if (insn_code_number >= 0)
2994 newpat = m_split;
2996 else if (m_split && NEXT_INSN (NEXT_INSN (m_split)) == NULL_RTX
2997 && (next_real_insn (i2) == i3
2998 || ! use_crosses_set_p (PATTERN (m_split), DF_INSN_LUID (i2))))
3000 rtx i2set, i3set;
3001 rtx newi3pat = PATTERN (NEXT_INSN (m_split));
3002 newi2pat = PATTERN (m_split);
3004 i3set = single_set (NEXT_INSN (m_split));
3005 i2set = single_set (m_split);
3007 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3009 /* If I2 or I3 has multiple SETs, we won't know how to track
3010 register status, so don't use these insns. If I2's destination
3011 is used between I2 and I3, we also can't use these insns. */
3013 if (i2_code_number >= 0 && i2set && i3set
3014 && (next_real_insn (i2) == i3
3015 || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
3016 insn_code_number = recog_for_combine (&newi3pat, i3,
3017 &new_i3_notes);
3018 if (insn_code_number >= 0)
3019 newpat = newi3pat;
3021 /* It is possible that both insns now set the destination of I3.
3022 If so, we must show an extra use of it. */
3024 if (insn_code_number >= 0)
3026 rtx new_i3_dest = SET_DEST (i3set);
3027 rtx new_i2_dest = SET_DEST (i2set);
3029 while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
3030 || GET_CODE (new_i3_dest) == STRICT_LOW_PART
3031 || GET_CODE (new_i3_dest) == SUBREG)
3032 new_i3_dest = XEXP (new_i3_dest, 0);
3034 while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
3035 || GET_CODE (new_i2_dest) == STRICT_LOW_PART
3036 || GET_CODE (new_i2_dest) == SUBREG)
3037 new_i2_dest = XEXP (new_i2_dest, 0);
3039 if (REG_P (new_i3_dest)
3040 && REG_P (new_i2_dest)
3041 && REGNO (new_i3_dest) == REGNO (new_i2_dest))
3042 INC_REG_N_SETS (REGNO (new_i2_dest), 1);
3046 /* If we can split it and use I2DEST, go ahead and see if that
3047 helps things be recognized. Verify that none of the registers
3048 are set between I2 and I3. */
3049 if (insn_code_number < 0 && (split = find_split_point (&newpat, i3)) != 0
3050 #ifdef HAVE_cc0
3051 && REG_P (i2dest)
3052 #endif
3053 /* We need I2DEST in the proper mode. If it is a hard register
3054 or the only use of a pseudo, we can change its mode.
3055 Make sure we don't change a hard register to have a mode that
3056 isn't valid for it, or change the number of registers. */
3057 && (GET_MODE (*split) == GET_MODE (i2dest)
3058 || GET_MODE (*split) == VOIDmode
3059 || can_change_dest_mode (i2dest, added_sets_2,
3060 GET_MODE (*split)))
3061 && (next_real_insn (i2) == i3
3062 || ! use_crosses_set_p (*split, DF_INSN_LUID (i2)))
3063 /* We can't overwrite I2DEST if its value is still used by
3064 NEWPAT. */
3065 && ! reg_referenced_p (i2dest, newpat))
3067 rtx newdest = i2dest;
3068 enum rtx_code split_code = GET_CODE (*split);
3069 enum machine_mode split_mode = GET_MODE (*split);
3070 bool subst_done = false;
3071 newi2pat = NULL_RTX;
3073 /* Get NEWDEST as a register in the proper mode. We have already
3074 validated that we can do this. */
3075 if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
3077 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3078 newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
3079 else
3081 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], split_mode);
3082 newdest = regno_reg_rtx[REGNO (i2dest)];
3086 /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
3087 an ASHIFT. This can occur if it was inside a PLUS and hence
3088 appeared to be a memory address. This is a kludge. */
3089 if (split_code == MULT
3090 && GET_CODE (XEXP (*split, 1)) == CONST_INT
3091 && INTVAL (XEXP (*split, 1)) > 0
3092 && (i = exact_log2 (INTVAL (XEXP (*split, 1)))) >= 0)
3094 SUBST (*split, gen_rtx_ASHIFT (split_mode,
3095 XEXP (*split, 0), GEN_INT (i)));
3096 /* Update split_code because we may not have a multiply
3097 anymore. */
3098 split_code = GET_CODE (*split);
3101 #ifdef INSN_SCHEDULING
3102 /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
3103 be written as a ZERO_EXTEND. */
3104 if (split_code == SUBREG && MEM_P (SUBREG_REG (*split)))
3106 #ifdef LOAD_EXTEND_OP
3107 /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
3108 what it really is. */
3109 if (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (*split)))
3110 == SIGN_EXTEND)
3111 SUBST (*split, gen_rtx_SIGN_EXTEND (split_mode,
3112 SUBREG_REG (*split)));
3113 else
3114 #endif
3115 SUBST (*split, gen_rtx_ZERO_EXTEND (split_mode,
3116 SUBREG_REG (*split)));
3118 #endif
3120 /* Attempt to split binary operators using arithmetic identities. */
3121 if (BINARY_P (SET_SRC (newpat))
3122 && split_mode == GET_MODE (SET_SRC (newpat))
3123 && ! side_effects_p (SET_SRC (newpat)))
3125 rtx setsrc = SET_SRC (newpat);
3126 enum machine_mode mode = GET_MODE (setsrc);
3127 enum rtx_code code = GET_CODE (setsrc);
3128 rtx src_op0 = XEXP (setsrc, 0);
3129 rtx src_op1 = XEXP (setsrc, 1);
3131 /* Split "X = Y op Y" as "Z = Y; X = Z op Z". */
3132 if (rtx_equal_p (src_op0, src_op1))
3134 newi2pat = gen_rtx_SET (VOIDmode, newdest, src_op0);
3135 SUBST (XEXP (setsrc, 0), newdest);
3136 SUBST (XEXP (setsrc, 1), newdest);
3137 subst_done = true;
3139 /* Split "((P op Q) op R) op S" where op is PLUS or MULT. */
3140 else if ((code == PLUS || code == MULT)
3141 && GET_CODE (src_op0) == code
3142 && GET_CODE (XEXP (src_op0, 0)) == code
3143 && (INTEGRAL_MODE_P (mode)
3144 || (FLOAT_MODE_P (mode)
3145 && flag_unsafe_math_optimizations)))
3147 rtx p = XEXP (XEXP (src_op0, 0), 0);
3148 rtx q = XEXP (XEXP (src_op0, 0), 1);
3149 rtx r = XEXP (src_op0, 1);
3150 rtx s = src_op1;
3152 /* Split both "((X op Y) op X) op Y" and
3153 "((X op Y) op Y) op X" as "T op T" where T is
3154 "X op Y". */
3155 if ((rtx_equal_p (p,r) && rtx_equal_p (q,s))
3156 || (rtx_equal_p (p,s) && rtx_equal_p (q,r)))
3158 newi2pat = gen_rtx_SET (VOIDmode, newdest,
3159 XEXP (src_op0, 0));
3160 SUBST (XEXP (setsrc, 0), newdest);
3161 SUBST (XEXP (setsrc, 1), newdest);
3162 subst_done = true;
3164 /* Split "((X op X) op Y) op Y)" as "T op T" where
3165 T is "X op Y". */
3166 else if (rtx_equal_p (p,q) && rtx_equal_p (r,s))
3168 rtx tmp = simplify_gen_binary (code, mode, p, r);
3169 newi2pat = gen_rtx_SET (VOIDmode, newdest, tmp);
3170 SUBST (XEXP (setsrc, 0), newdest);
3171 SUBST (XEXP (setsrc, 1), newdest);
3172 subst_done = true;
3177 if (!subst_done)
3179 newi2pat = gen_rtx_SET (VOIDmode, newdest, *split);
3180 SUBST (*split, newdest);
3183 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3185 /* recog_for_combine might have added CLOBBERs to newi2pat.
3186 Make sure NEWPAT does not depend on the clobbered regs. */
3187 if (GET_CODE (newi2pat) == PARALLEL)
3188 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3189 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3191 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3192 if (reg_overlap_mentioned_p (reg, newpat))
3194 undo_all ();
3195 return 0;
3199 /* If the split point was a MULT and we didn't have one before,
3200 don't use one now. */
3201 if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
3202 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3206 /* Check for a case where we loaded from memory in a narrow mode and
3207 then sign extended it, but we need both registers. In that case,
3208 we have a PARALLEL with both loads from the same memory location.
3209 We can split this into a load from memory followed by a register-register
3210 copy. This saves at least one insn, more if register allocation can
3211 eliminate the copy.
3213 We cannot do this if the destination of the first assignment is a
3214 condition code register or cc0. We eliminate this case by making sure
3215 the SET_DEST and SET_SRC have the same mode.
3217 We cannot do this if the destination of the second assignment is
3218 a register that we have already assumed is zero-extended. Similarly
3219 for a SUBREG of such a register. */
3221 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3222 && GET_CODE (newpat) == PARALLEL
3223 && XVECLEN (newpat, 0) == 2
3224 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3225 && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
3226 && (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0)))
3227 == GET_MODE (SET_SRC (XVECEXP (newpat, 0, 0))))
3228 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3229 && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3230 XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
3231 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3232 DF_INSN_LUID (i2))
3233 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3234 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3235 && ! (temp = SET_DEST (XVECEXP (newpat, 0, 1)),
3236 (REG_P (temp)
3237 && VEC_index (reg_stat_type, reg_stat,
3238 REGNO (temp))->nonzero_bits != 0
3239 && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
3240 && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
3241 && (VEC_index (reg_stat_type, reg_stat,
3242 REGNO (temp))->nonzero_bits
3243 != GET_MODE_MASK (word_mode))))
3244 && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
3245 && (temp = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
3246 (REG_P (temp)
3247 && VEC_index (reg_stat_type, reg_stat,
3248 REGNO (temp))->nonzero_bits != 0
3249 && GET_MODE_BITSIZE (GET_MODE (temp)) < BITS_PER_WORD
3250 && GET_MODE_BITSIZE (GET_MODE (temp)) < HOST_BITS_PER_INT
3251 && (VEC_index (reg_stat_type, reg_stat,
3252 REGNO (temp))->nonzero_bits
3253 != GET_MODE_MASK (word_mode)))))
3254 && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3255 SET_SRC (XVECEXP (newpat, 0, 1)))
3256 && ! find_reg_note (i3, REG_UNUSED,
3257 SET_DEST (XVECEXP (newpat, 0, 0))))
3259 rtx ni2dest;
3261 newi2pat = XVECEXP (newpat, 0, 0);
3262 ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
3263 newpat = XVECEXP (newpat, 0, 1);
3264 SUBST (SET_SRC (newpat),
3265 gen_lowpart (GET_MODE (SET_SRC (newpat)), ni2dest));
3266 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3268 if (i2_code_number >= 0)
3269 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3271 if (insn_code_number >= 0)
3272 swap_i2i3 = 1;
3275 /* Similarly, check for a case where we have a PARALLEL of two independent
3276 SETs but we started with three insns. In this case, we can do the sets
3277 as two separate insns. This case occurs when some SET allows two
3278 other insns to combine, but the destination of that SET is still live. */
3280 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3281 && GET_CODE (newpat) == PARALLEL
3282 && XVECLEN (newpat, 0) == 2
3283 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3284 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
3285 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
3286 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3287 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3288 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3289 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3290 DF_INSN_LUID (i2))
3291 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3292 XVECEXP (newpat, 0, 0))
3293 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
3294 XVECEXP (newpat, 0, 1))
3295 && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
3296 && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1))))
3297 #ifdef HAVE_cc0
3298 /* We cannot split the parallel into two sets if both sets
3299 reference cc0. */
3300 && ! (reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 0))
3301 && reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 1)))
3302 #endif
3305 /* Normally, it doesn't matter which of the two is done first,
3306 but it does if one references cc0. In that case, it has to
3307 be first. */
3308 #ifdef HAVE_cc0
3309 if (reg_referenced_p (cc0_rtx, XVECEXP (newpat, 0, 0)))
3311 newi2pat = XVECEXP (newpat, 0, 0);
3312 newpat = XVECEXP (newpat, 0, 1);
3314 else
3315 #endif
3317 newi2pat = XVECEXP (newpat, 0, 1);
3318 newpat = XVECEXP (newpat, 0, 0);
3321 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3323 if (i2_code_number >= 0)
3324 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3327 /* If it still isn't recognized, fail and change things back the way they
3328 were. */
3329 if ((insn_code_number < 0
3330 /* Is the result a reasonable ASM_OPERANDS? */
3331 && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
3333 undo_all ();
3334 return 0;
3337 /* If we had to change another insn, make sure it is valid also. */
3338 if (undobuf.other_insn)
3340 CLEAR_HARD_REG_SET (newpat_used_regs);
3342 other_pat = PATTERN (undobuf.other_insn);
3343 other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
3344 &new_other_notes);
3346 if (other_code_number < 0 && ! check_asm_operands (other_pat))
3348 undo_all ();
3349 return 0;
3353 #ifdef HAVE_cc0
3354 /* If I2 is the CC0 setter and I3 is the CC0 user then check whether
3355 they are adjacent to each other or not. */
3357 rtx p = prev_nonnote_insn (i3);
3358 if (p && p != i2 && NONJUMP_INSN_P (p) && newi2pat
3359 && sets_cc0_p (newi2pat))
3361 undo_all ();
3362 return 0;
3365 #endif
3367 /* Only allow this combination if insn_rtx_costs reports that the
3368 replacement instructions are cheaper than the originals. */
3369 if (!combine_validate_cost (i1, i2, i3, newpat, newi2pat, other_pat))
3371 undo_all ();
3372 return 0;
3375 /* We now know that we can do this combination. Merge the insns and
3376 update the status of registers and LOG_LINKS. */
3378 if (undobuf.other_insn)
3380 rtx note, next;
3382 PATTERN (undobuf.other_insn) = other_pat;
3384 /* If any of the notes in OTHER_INSN were REG_UNUSED, ensure that they
3385 are still valid. Then add any non-duplicate notes added by
3386 recog_for_combine. */
3387 for (note = REG_NOTES (undobuf.other_insn); note; note = next)
3389 next = XEXP (note, 1);
3391 if (REG_NOTE_KIND (note) == REG_UNUSED
3392 && ! reg_set_p (XEXP (note, 0), PATTERN (undobuf.other_insn)))
3393 remove_note (undobuf.other_insn, note);
3396 distribute_notes (new_other_notes, undobuf.other_insn,
3397 undobuf.other_insn, NULL_RTX, NULL_RTX, NULL_RTX);
3400 if (swap_i2i3)
3402 rtx insn;
3403 rtx link;
3404 rtx ni2dest;
3406 /* I3 now uses what used to be its destination and which is now
3407 I2's destination. This requires us to do a few adjustments. */
3408 PATTERN (i3) = newpat;
3409 adjust_for_new_dest (i3);
3411 /* We need a LOG_LINK from I3 to I2. But we used to have one,
3412 so we still will.
3414 However, some later insn might be using I2's dest and have
3415 a LOG_LINK pointing at I3. We must remove this link.
3416 The simplest way to remove the link is to point it at I1,
3417 which we know will be a NOTE. */
3419 /* newi2pat is usually a SET here; however, recog_for_combine might
3420 have added some clobbers. */
3421 if (GET_CODE (newi2pat) == PARALLEL)
3422 ni2dest = SET_DEST (XVECEXP (newi2pat, 0, 0));
3423 else
3424 ni2dest = SET_DEST (newi2pat);
3426 for (insn = NEXT_INSN (i3);
3427 insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
3428 || insn != BB_HEAD (this_basic_block->next_bb));
3429 insn = NEXT_INSN (insn))
3431 if (INSN_P (insn) && reg_referenced_p (ni2dest, PATTERN (insn)))
3433 for (link = LOG_LINKS (insn); link;
3434 link = XEXP (link, 1))
3435 if (XEXP (link, 0) == i3)
3436 XEXP (link, 0) = i1;
3438 break;
3444 rtx i3notes, i2notes, i1notes = 0;
3445 rtx i3links, i2links, i1links = 0;
3446 rtx midnotes = 0;
3447 unsigned int regno;
3448 /* Compute which registers we expect to eliminate. newi2pat may be setting
3449 either i3dest or i2dest, so we must check it. Also, i1dest may be the
3450 same as i3dest, in which case newi2pat may be setting i1dest. */
3451 rtx elim_i2 = ((newi2pat && reg_set_p (i2dest, newi2pat))
3452 || i2dest_in_i2src || i2dest_in_i1src
3453 || !i2dest_killed
3454 ? 0 : i2dest);
3455 rtx elim_i1 = (i1 == 0 || i1dest_in_i1src
3456 || (newi2pat && reg_set_p (i1dest, newi2pat))
3457 || !i1dest_killed
3458 ? 0 : i1dest);
3460 /* Get the old REG_NOTES and LOG_LINKS from all our insns and
3461 clear them. */
3462 i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
3463 i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
3464 if (i1)
3465 i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
3467 /* Ensure that we do not have something that should not be shared but
3468 occurs multiple times in the new insns. Check this by first
3469 resetting all the `used' flags and then copying anything is shared. */
3471 reset_used_flags (i3notes);
3472 reset_used_flags (i2notes);
3473 reset_used_flags (i1notes);
3474 reset_used_flags (newpat);
3475 reset_used_flags (newi2pat);
3476 if (undobuf.other_insn)
3477 reset_used_flags (PATTERN (undobuf.other_insn));
3479 i3notes = copy_rtx_if_shared (i3notes);
3480 i2notes = copy_rtx_if_shared (i2notes);
3481 i1notes = copy_rtx_if_shared (i1notes);
3482 newpat = copy_rtx_if_shared (newpat);
3483 newi2pat = copy_rtx_if_shared (newi2pat);
3484 if (undobuf.other_insn)
3485 reset_used_flags (PATTERN (undobuf.other_insn));
3487 INSN_CODE (i3) = insn_code_number;
3488 PATTERN (i3) = newpat;
3490 if (CALL_P (i3) && CALL_INSN_FUNCTION_USAGE (i3))
3492 rtx call_usage = CALL_INSN_FUNCTION_USAGE (i3);
3494 reset_used_flags (call_usage);
3495 call_usage = copy_rtx (call_usage);
3497 if (substed_i2)
3498 replace_rtx (call_usage, i2dest, i2src);
3500 if (substed_i1)
3501 replace_rtx (call_usage, i1dest, i1src);
3503 CALL_INSN_FUNCTION_USAGE (i3) = call_usage;
3506 if (undobuf.other_insn)
3507 INSN_CODE (undobuf.other_insn) = other_code_number;
3509 /* We had one special case above where I2 had more than one set and
3510 we replaced a destination of one of those sets with the destination
3511 of I3. In that case, we have to update LOG_LINKS of insns later
3512 in this basic block. Note that this (expensive) case is rare.
3514 Also, in this case, we must pretend that all REG_NOTEs for I2
3515 actually came from I3, so that REG_UNUSED notes from I2 will be
3516 properly handled. */
3518 if (i3_subst_into_i2)
3520 for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
3521 if ((GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == SET
3522 || GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == CLOBBER)
3523 && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, i)))
3524 && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
3525 && ! find_reg_note (i2, REG_UNUSED,
3526 SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
3527 for (temp = NEXT_INSN (i2);
3528 temp && (this_basic_block->next_bb == EXIT_BLOCK_PTR
3529 || BB_HEAD (this_basic_block) != temp);
3530 temp = NEXT_INSN (temp))
3531 if (temp != i3 && INSN_P (temp))
3532 for (link = LOG_LINKS (temp); link; link = XEXP (link, 1))
3533 if (XEXP (link, 0) == i2)
3534 XEXP (link, 0) = i3;
3536 if (i3notes)
3538 rtx link = i3notes;
3539 while (XEXP (link, 1))
3540 link = XEXP (link, 1);
3541 XEXP (link, 1) = i2notes;
3543 else
3544 i3notes = i2notes;
3545 i2notes = 0;
3548 LOG_LINKS (i3) = 0;
3549 REG_NOTES (i3) = 0;
3550 LOG_LINKS (i2) = 0;
3551 REG_NOTES (i2) = 0;
3553 if (newi2pat)
3555 INSN_CODE (i2) = i2_code_number;
3556 PATTERN (i2) = newi2pat;
3558 else
3559 SET_INSN_DELETED (i2);
3561 if (i1)
3563 LOG_LINKS (i1) = 0;
3564 REG_NOTES (i1) = 0;
3565 SET_INSN_DELETED (i1);
3568 /* Get death notes for everything that is now used in either I3 or
3569 I2 and used to die in a previous insn. If we built two new
3570 patterns, move from I1 to I2 then I2 to I3 so that we get the
3571 proper movement on registers that I2 modifies. */
3573 if (newi2pat)
3575 move_deaths (newi2pat, NULL_RTX, DF_INSN_LUID (i1), i2, &midnotes);
3576 move_deaths (newpat, newi2pat, DF_INSN_LUID (i1), i3, &midnotes);
3578 else
3579 move_deaths (newpat, NULL_RTX, i1 ? DF_INSN_LUID (i1) : DF_INSN_LUID (i2),
3580 i3, &midnotes);
3582 /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3. */
3583 if (i3notes)
3584 distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL_RTX,
3585 elim_i2, elim_i1);
3586 if (i2notes)
3587 distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL_RTX,
3588 elim_i2, elim_i1);
3589 if (i1notes)
3590 distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL_RTX,
3591 elim_i2, elim_i1);
3592 if (midnotes)
3593 distribute_notes (midnotes, NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
3594 elim_i2, elim_i1);
3596 /* Distribute any notes added to I2 or I3 by recog_for_combine. We
3597 know these are REG_UNUSED and want them to go to the desired insn,
3598 so we always pass it as i3. */
3600 if (newi2pat && new_i2_notes)
3601 distribute_notes (new_i2_notes, i2, i2, NULL_RTX, NULL_RTX, NULL_RTX);
3603 if (new_i3_notes)
3604 distribute_notes (new_i3_notes, i3, i3, NULL_RTX, NULL_RTX, NULL_RTX);
3606 /* If I3DEST was used in I3SRC, it really died in I3. We may need to
3607 put a REG_DEAD note for it somewhere. If NEWI2PAT exists and sets
3608 I3DEST, the death must be somewhere before I2, not I3. If we passed I3
3609 in that case, it might delete I2. Similarly for I2 and I1.
3610 Show an additional death due to the REG_DEAD note we make here. If
3611 we discard it in distribute_notes, we will decrement it again. */
3613 if (i3dest_killed)
3615 if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
3616 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i3dest_killed,
3617 NULL_RTX),
3618 NULL_RTX, i2, NULL_RTX, elim_i2, elim_i1);
3619 else
3620 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i3dest_killed,
3621 NULL_RTX),
3622 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
3623 elim_i2, elim_i1);
3626 if (i2dest_in_i2src)
3628 if (newi2pat && reg_set_p (i2dest, newi2pat))
3629 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i2dest, NULL_RTX),
3630 NULL_RTX, i2, NULL_RTX, NULL_RTX, NULL_RTX);
3631 else
3632 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i2dest, NULL_RTX),
3633 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
3634 NULL_RTX, NULL_RTX);
3637 if (i1dest_in_i1src)
3639 if (newi2pat && reg_set_p (i1dest, newi2pat))
3640 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i1dest, NULL_RTX),
3641 NULL_RTX, i2, NULL_RTX, NULL_RTX, NULL_RTX);
3642 else
3643 distribute_notes (gen_rtx_EXPR_LIST (REG_DEAD, i1dest, NULL_RTX),
3644 NULL_RTX, i3, newi2pat ? i2 : NULL_RTX,
3645 NULL_RTX, NULL_RTX);
3648 distribute_links (i3links);
3649 distribute_links (i2links);
3650 distribute_links (i1links);
3652 if (REG_P (i2dest))
3654 rtx link;
3655 rtx i2_insn = 0, i2_val = 0, set;
3657 /* The insn that used to set this register doesn't exist, and
3658 this life of the register may not exist either. See if one of
3659 I3's links points to an insn that sets I2DEST. If it does,
3660 that is now the last known value for I2DEST. If we don't update
3661 this and I2 set the register to a value that depended on its old
3662 contents, we will get confused. If this insn is used, thing
3663 will be set correctly in combine_instructions. */
3665 for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
3666 if ((set = single_set (XEXP (link, 0))) != 0
3667 && rtx_equal_p (i2dest, SET_DEST (set)))
3668 i2_insn = XEXP (link, 0), i2_val = SET_SRC (set);
3670 record_value_for_reg (i2dest, i2_insn, i2_val);
3672 /* If the reg formerly set in I2 died only once and that was in I3,
3673 zero its use count so it won't make `reload' do any work. */
3674 if (! added_sets_2
3675 && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
3676 && ! i2dest_in_i2src)
3678 regno = REGNO (i2dest);
3679 INC_REG_N_SETS (regno, -1);
3683 if (i1 && REG_P (i1dest))
3685 rtx link;
3686 rtx i1_insn = 0, i1_val = 0, set;
3688 for (link = LOG_LINKS (i3); link; link = XEXP (link, 1))
3689 if ((set = single_set (XEXP (link, 0))) != 0
3690 && rtx_equal_p (i1dest, SET_DEST (set)))
3691 i1_insn = XEXP (link, 0), i1_val = SET_SRC (set);
3693 record_value_for_reg (i1dest, i1_insn, i1_val);
3695 regno = REGNO (i1dest);
3696 if (! added_sets_1 && ! i1dest_in_i1src)
3697 INC_REG_N_SETS (regno, -1);
3700 /* Update reg_stat[].nonzero_bits et al for any changes that may have
3701 been made to this insn. The order of
3702 set_nonzero_bits_and_sign_copies() is important. Because newi2pat
3703 can affect nonzero_bits of newpat */
3704 if (newi2pat)
3705 note_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
3706 note_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
3708 /* Set new_direct_jump_p if a new return or simple jump instruction
3709 has been created.
3711 If I3 is now an unconditional jump, ensure that it has a
3712 BARRIER following it since it may have initially been a
3713 conditional jump. It may also be the last nonnote insn. */
3715 if (returnjump_p (i3) || any_uncondjump_p (i3))
3717 *new_direct_jump_p = 1;
3718 mark_jump_label (PATTERN (i3), i3, 0);
3720 if ((temp = next_nonnote_insn (i3)) == NULL_RTX
3721 || !BARRIER_P (temp))
3722 emit_barrier_after (i3);
3725 if (undobuf.other_insn != NULL_RTX
3726 && (returnjump_p (undobuf.other_insn)
3727 || any_uncondjump_p (undobuf.other_insn)))
3729 *new_direct_jump_p = 1;
3731 if ((temp = next_nonnote_insn (undobuf.other_insn)) == NULL_RTX
3732 || !BARRIER_P (temp))
3733 emit_barrier_after (undobuf.other_insn);
3736 /* An NOOP jump does not need barrier, but it does need cleaning up
3737 of CFG. */
3738 if (GET_CODE (newpat) == SET
3739 && SET_SRC (newpat) == pc_rtx
3740 && SET_DEST (newpat) == pc_rtx)
3741 *new_direct_jump_p = 1;
3744 if (undobuf.other_insn != NULL_RTX)
3746 if (dump_file)
3748 fprintf (dump_file, "modifying other_insn ");
3749 dump_insn_slim (dump_file, undobuf.other_insn);
3751 df_insn_rescan (undobuf.other_insn);
3754 if (i1 && !(NOTE_P(i1) && (NOTE_KIND (i1) == NOTE_INSN_DELETED)))
3756 if (dump_file)
3758 fprintf (dump_file, "modifying insn i1 ");
3759 dump_insn_slim (dump_file, i1);
3761 df_insn_rescan (i1);
3764 if (i2 && !(NOTE_P(i2) && (NOTE_KIND (i2) == NOTE_INSN_DELETED)))
3766 if (dump_file)
3768 fprintf (dump_file, "modifying insn i2 ");
3769 dump_insn_slim (dump_file, i2);
3771 df_insn_rescan (i2);
3774 if (i3 && !(NOTE_P(i3) && (NOTE_KIND (i3) == NOTE_INSN_DELETED)))
3776 if (dump_file)
3778 fprintf (dump_file, "modifying insn i3 ");
3779 dump_insn_slim (dump_file, i3);
3781 df_insn_rescan (i3);
3784 combine_successes++;
3785 undo_commit ();
3787 if (added_links_insn
3788 && (newi2pat == 0 || DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i2))
3789 && DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i3))
3790 return added_links_insn;
3791 else
3792 return newi2pat ? i2 : i3;
3795 /* Undo all the modifications recorded in undobuf. */
3797 static void
3798 undo_all (void)
3800 struct undo *undo, *next;
3802 for (undo = undobuf.undos; undo; undo = next)
3804 next = undo->next;
3805 switch (undo->kind)
3807 case UNDO_RTX:
3808 *undo->where.r = undo->old_contents.r;
3809 break;
3810 case UNDO_INT:
3811 *undo->where.i = undo->old_contents.i;
3812 break;
3813 case UNDO_MODE:
3814 adjust_reg_mode (*undo->where.r, undo->old_contents.m);
3815 break;
3816 default:
3817 gcc_unreachable ();
3820 undo->next = undobuf.frees;
3821 undobuf.frees = undo;
3824 undobuf.undos = 0;
3827 /* We've committed to accepting the changes we made. Move all
3828 of the undos to the free list. */
3830 static void
3831 undo_commit (void)
3833 struct undo *undo, *next;
3835 for (undo = undobuf.undos; undo; undo = next)
3837 next = undo->next;
3838 undo->next = undobuf.frees;
3839 undobuf.frees = undo;
3841 undobuf.undos = 0;
3844 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
3845 where we have an arithmetic expression and return that point. LOC will
3846 be inside INSN.
3848 try_combine will call this function to see if an insn can be split into
3849 two insns. */
3851 static rtx *
3852 find_split_point (rtx *loc, rtx insn)
3854 rtx x = *loc;
3855 enum rtx_code code = GET_CODE (x);
3856 rtx *split;
3857 unsigned HOST_WIDE_INT len = 0;
3858 HOST_WIDE_INT pos = 0;
3859 int unsignedp = 0;
3860 rtx inner = NULL_RTX;
3862 /* First special-case some codes. */
3863 switch (code)
3865 case SUBREG:
3866 #ifdef INSN_SCHEDULING
3867 /* If we are making a paradoxical SUBREG invalid, it becomes a split
3868 point. */
3869 if (MEM_P (SUBREG_REG (x)))
3870 return loc;
3871 #endif
3872 return find_split_point (&SUBREG_REG (x), insn);
3874 case MEM:
3875 #ifdef HAVE_lo_sum
3876 /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
3877 using LO_SUM and HIGH. */
3878 if (GET_CODE (XEXP (x, 0)) == CONST
3879 || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
3881 SUBST (XEXP (x, 0),
3882 gen_rtx_LO_SUM (Pmode,
3883 gen_rtx_HIGH (Pmode, XEXP (x, 0)),
3884 XEXP (x, 0)));
3885 return &XEXP (XEXP (x, 0), 0);
3887 #endif
3889 /* If we have a PLUS whose second operand is a constant and the
3890 address is not valid, perhaps will can split it up using
3891 the machine-specific way to split large constants. We use
3892 the first pseudo-reg (one of the virtual regs) as a placeholder;
3893 it will not remain in the result. */
3894 if (GET_CODE (XEXP (x, 0)) == PLUS
3895 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
3896 && ! memory_address_p (GET_MODE (x), XEXP (x, 0)))
3898 rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
3899 rtx seq = combine_split_insns (gen_rtx_SET (VOIDmode, reg,
3900 XEXP (x, 0)),
3901 subst_insn);
3903 /* This should have produced two insns, each of which sets our
3904 placeholder. If the source of the second is a valid address,
3905 we can make put both sources together and make a split point
3906 in the middle. */
3908 if (seq
3909 && NEXT_INSN (seq) != NULL_RTX
3910 && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
3911 && NONJUMP_INSN_P (seq)
3912 && GET_CODE (PATTERN (seq)) == SET
3913 && SET_DEST (PATTERN (seq)) == reg
3914 && ! reg_mentioned_p (reg,
3915 SET_SRC (PATTERN (seq)))
3916 && NONJUMP_INSN_P (NEXT_INSN (seq))
3917 && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
3918 && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
3919 && memory_address_p (GET_MODE (x),
3920 SET_SRC (PATTERN (NEXT_INSN (seq)))))
3922 rtx src1 = SET_SRC (PATTERN (seq));
3923 rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
3925 /* Replace the placeholder in SRC2 with SRC1. If we can
3926 find where in SRC2 it was placed, that can become our
3927 split point and we can replace this address with SRC2.
3928 Just try two obvious places. */
3930 src2 = replace_rtx (src2, reg, src1);
3931 split = 0;
3932 if (XEXP (src2, 0) == src1)
3933 split = &XEXP (src2, 0);
3934 else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
3935 && XEXP (XEXP (src2, 0), 0) == src1)
3936 split = &XEXP (XEXP (src2, 0), 0);
3938 if (split)
3940 SUBST (XEXP (x, 0), src2);
3941 return split;
3945 /* If that didn't work, perhaps the first operand is complex and
3946 needs to be computed separately, so make a split point there.
3947 This will occur on machines that just support REG + CONST
3948 and have a constant moved through some previous computation. */
3950 else if (!OBJECT_P (XEXP (XEXP (x, 0), 0))
3951 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
3952 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
3953 return &XEXP (XEXP (x, 0), 0);
3956 /* If we have a PLUS whose first operand is complex, try computing it
3957 separately by making a split there. */
3958 if (GET_CODE (XEXP (x, 0)) == PLUS
3959 && ! memory_address_p (GET_MODE (x), XEXP (x, 0))
3960 && ! OBJECT_P (XEXP (XEXP (x, 0), 0))
3961 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
3962 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
3963 return &XEXP (XEXP (x, 0), 0);
3964 break;
3966 case SET:
3967 #ifdef HAVE_cc0
3968 /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
3969 ZERO_EXTRACT, the most likely reason why this doesn't match is that
3970 we need to put the operand into a register. So split at that
3971 point. */
3973 if (SET_DEST (x) == cc0_rtx
3974 && GET_CODE (SET_SRC (x)) != COMPARE
3975 && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
3976 && !OBJECT_P (SET_SRC (x))
3977 && ! (GET_CODE (SET_SRC (x)) == SUBREG
3978 && OBJECT_P (SUBREG_REG (SET_SRC (x)))))
3979 return &SET_SRC (x);
3980 #endif
3982 /* See if we can split SET_SRC as it stands. */
3983 split = find_split_point (&SET_SRC (x), insn);
3984 if (split && split != &SET_SRC (x))
3985 return split;
3987 /* See if we can split SET_DEST as it stands. */
3988 split = find_split_point (&SET_DEST (x), insn);
3989 if (split && split != &SET_DEST (x))
3990 return split;
3992 /* See if this is a bitfield assignment with everything constant. If
3993 so, this is an IOR of an AND, so split it into that. */
3994 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
3995 && (GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)))
3996 <= HOST_BITS_PER_WIDE_INT)
3997 && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT
3998 && GET_CODE (XEXP (SET_DEST (x), 2)) == CONST_INT
3999 && GET_CODE (SET_SRC (x)) == CONST_INT
4000 && ((INTVAL (XEXP (SET_DEST (x), 1))
4001 + INTVAL (XEXP (SET_DEST (x), 2)))
4002 <= GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0))))
4003 && ! side_effects_p (XEXP (SET_DEST (x), 0)))
4005 HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
4006 unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
4007 unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x));
4008 rtx dest = XEXP (SET_DEST (x), 0);
4009 enum machine_mode mode = GET_MODE (dest);
4010 unsigned HOST_WIDE_INT mask = ((HOST_WIDE_INT) 1 << len) - 1;
4011 rtx or_mask;
4013 if (BITS_BIG_ENDIAN)
4014 pos = GET_MODE_BITSIZE (mode) - len - pos;
4016 or_mask = gen_int_mode (src << pos, mode);
4017 if (src == mask)
4018 SUBST (SET_SRC (x),
4019 simplify_gen_binary (IOR, mode, dest, or_mask));
4020 else
4022 rtx negmask = gen_int_mode (~(mask << pos), mode);
4023 SUBST (SET_SRC (x),
4024 simplify_gen_binary (IOR, mode,
4025 simplify_gen_binary (AND, mode,
4026 dest, negmask),
4027 or_mask));
4030 SUBST (SET_DEST (x), dest);
4032 split = find_split_point (&SET_SRC (x), insn);
4033 if (split && split != &SET_SRC (x))
4034 return split;
4037 /* Otherwise, see if this is an operation that we can split into two.
4038 If so, try to split that. */
4039 code = GET_CODE (SET_SRC (x));
4041 switch (code)
4043 case AND:
4044 /* If we are AND'ing with a large constant that is only a single
4045 bit and the result is only being used in a context where we
4046 need to know if it is zero or nonzero, replace it with a bit
4047 extraction. This will avoid the large constant, which might
4048 have taken more than one insn to make. If the constant were
4049 not a valid argument to the AND but took only one insn to make,
4050 this is no worse, but if it took more than one insn, it will
4051 be better. */
4053 if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
4054 && REG_P (XEXP (SET_SRC (x), 0))
4055 && (pos = exact_log2 (INTVAL (XEXP (SET_SRC (x), 1)))) >= 7
4056 && REG_P (SET_DEST (x))
4057 && (split = find_single_use (SET_DEST (x), insn, (rtx*) 0)) != 0
4058 && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
4059 && XEXP (*split, 0) == SET_DEST (x)
4060 && XEXP (*split, 1) == const0_rtx)
4062 rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
4063 XEXP (SET_SRC (x), 0),
4064 pos, NULL_RTX, 1, 1, 0, 0);
4065 if (extraction != 0)
4067 SUBST (SET_SRC (x), extraction);
4068 return find_split_point (loc, insn);
4071 break;
4073 case NE:
4074 /* If STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
4075 is known to be on, this can be converted into a NEG of a shift. */
4076 if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
4077 && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
4078 && 1 <= (pos = exact_log2
4079 (nonzero_bits (XEXP (SET_SRC (x), 0),
4080 GET_MODE (XEXP (SET_SRC (x), 0))))))
4082 enum machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
4084 SUBST (SET_SRC (x),
4085 gen_rtx_NEG (mode,
4086 gen_rtx_LSHIFTRT (mode,
4087 XEXP (SET_SRC (x), 0),
4088 GEN_INT (pos))));
4090 split = find_split_point (&SET_SRC (x), insn);
4091 if (split && split != &SET_SRC (x))
4092 return split;
4094 break;
4096 case SIGN_EXTEND:
4097 inner = XEXP (SET_SRC (x), 0);
4099 /* We can't optimize if either mode is a partial integer
4100 mode as we don't know how many bits are significant
4101 in those modes. */
4102 if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
4103 || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
4104 break;
4106 pos = 0;
4107 len = GET_MODE_BITSIZE (GET_MODE (inner));
4108 unsignedp = 0;
4109 break;
4111 case SIGN_EXTRACT:
4112 case ZERO_EXTRACT:
4113 if (GET_CODE (XEXP (SET_SRC (x), 1)) == CONST_INT
4114 && GET_CODE (XEXP (SET_SRC (x), 2)) == CONST_INT)
4116 inner = XEXP (SET_SRC (x), 0);
4117 len = INTVAL (XEXP (SET_SRC (x), 1));
4118 pos = INTVAL (XEXP (SET_SRC (x), 2));
4120 if (BITS_BIG_ENDIAN)
4121 pos = GET_MODE_BITSIZE (GET_MODE (inner)) - len - pos;
4122 unsignedp = (code == ZERO_EXTRACT);
4124 break;
4126 default:
4127 break;
4130 if (len && pos >= 0 && pos + len <= GET_MODE_BITSIZE (GET_MODE (inner)))
4132 enum machine_mode mode = GET_MODE (SET_SRC (x));
4134 /* For unsigned, we have a choice of a shift followed by an
4135 AND or two shifts. Use two shifts for field sizes where the
4136 constant might be too large. We assume here that we can
4137 always at least get 8-bit constants in an AND insn, which is
4138 true for every current RISC. */
4140 if (unsignedp && len <= 8)
4142 SUBST (SET_SRC (x),
4143 gen_rtx_AND (mode,
4144 gen_rtx_LSHIFTRT
4145 (mode, gen_lowpart (mode, inner),
4146 GEN_INT (pos)),
4147 GEN_INT (((HOST_WIDE_INT) 1 << len) - 1)));
4149 split = find_split_point (&SET_SRC (x), insn);
4150 if (split && split != &SET_SRC (x))
4151 return split;
4153 else
4155 SUBST (SET_SRC (x),
4156 gen_rtx_fmt_ee
4157 (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
4158 gen_rtx_ASHIFT (mode,
4159 gen_lowpart (mode, inner),
4160 GEN_INT (GET_MODE_BITSIZE (mode)
4161 - len - pos)),
4162 GEN_INT (GET_MODE_BITSIZE (mode) - len)));
4164 split = find_split_point (&SET_SRC (x), insn);
4165 if (split && split != &SET_SRC (x))
4166 return split;
4170 /* See if this is a simple operation with a constant as the second
4171 operand. It might be that this constant is out of range and hence
4172 could be used as a split point. */
4173 if (BINARY_P (SET_SRC (x))
4174 && CONSTANT_P (XEXP (SET_SRC (x), 1))
4175 && (OBJECT_P (XEXP (SET_SRC (x), 0))
4176 || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
4177 && OBJECT_P (SUBREG_REG (XEXP (SET_SRC (x), 0))))))
4178 return &XEXP (SET_SRC (x), 1);
4180 /* Finally, see if this is a simple operation with its first operand
4181 not in a register. The operation might require this operand in a
4182 register, so return it as a split point. We can always do this
4183 because if the first operand were another operation, we would have
4184 already found it as a split point. */
4185 if ((BINARY_P (SET_SRC (x)) || UNARY_P (SET_SRC (x)))
4186 && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
4187 return &XEXP (SET_SRC (x), 0);
4189 return 0;
4191 case AND:
4192 case IOR:
4193 /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
4194 it is better to write this as (not (ior A B)) so we can split it.
4195 Similarly for IOR. */
4196 if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
4198 SUBST (*loc,
4199 gen_rtx_NOT (GET_MODE (x),
4200 gen_rtx_fmt_ee (code == IOR ? AND : IOR,
4201 GET_MODE (x),
4202 XEXP (XEXP (x, 0), 0),
4203 XEXP (XEXP (x, 1), 0))));
4204 return find_split_point (loc, insn);
4207 /* Many RISC machines have a large set of logical insns. If the
4208 second operand is a NOT, put it first so we will try to split the
4209 other operand first. */
4210 if (GET_CODE (XEXP (x, 1)) == NOT)
4212 rtx tem = XEXP (x, 0);
4213 SUBST (XEXP (x, 0), XEXP (x, 1));
4214 SUBST (XEXP (x, 1), tem);
4216 break;
4218 default:
4219 break;
4222 /* Otherwise, select our actions depending on our rtx class. */
4223 switch (GET_RTX_CLASS (code))
4225 case RTX_BITFIELD_OPS: /* This is ZERO_EXTRACT and SIGN_EXTRACT. */
4226 case RTX_TERNARY:
4227 split = find_split_point (&XEXP (x, 2), insn);
4228 if (split)
4229 return split;
4230 /* ... fall through ... */
4231 case RTX_BIN_ARITH:
4232 case RTX_COMM_ARITH:
4233 case RTX_COMPARE:
4234 case RTX_COMM_COMPARE:
4235 split = find_split_point (&XEXP (x, 1), insn);
4236 if (split)
4237 return split;
4238 /* ... fall through ... */
4239 case RTX_UNARY:
4240 /* Some machines have (and (shift ...) ...) insns. If X is not
4241 an AND, but XEXP (X, 0) is, use it as our split point. */
4242 if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
4243 return &XEXP (x, 0);
4245 split = find_split_point (&XEXP (x, 0), insn);
4246 if (split)
4247 return split;
4248 return loc;
4250 default:
4251 /* Otherwise, we don't have a split point. */
4252 return 0;
4256 /* Throughout X, replace FROM with TO, and return the result.
4257 The result is TO if X is FROM;
4258 otherwise the result is X, but its contents may have been modified.
4259 If they were modified, a record was made in undobuf so that
4260 undo_all will (among other things) return X to its original state.
4262 If the number of changes necessary is too much to record to undo,
4263 the excess changes are not made, so the result is invalid.
4264 The changes already made can still be undone.
4265 undobuf.num_undo is incremented for such changes, so by testing that
4266 the caller can tell whether the result is valid.
4268 `n_occurrences' is incremented each time FROM is replaced.
4270 IN_DEST is nonzero if we are processing the SET_DEST of a SET.
4272 UNIQUE_COPY is nonzero if each substitution must be unique. We do this
4273 by copying if `n_occurrences' is nonzero. */
4275 static rtx
4276 subst (rtx x, rtx from, rtx to, int in_dest, int unique_copy)
4278 enum rtx_code code = GET_CODE (x);
4279 enum machine_mode op0_mode = VOIDmode;
4280 const char *fmt;
4281 int len, i;
4282 rtx new_rtx;
4284 /* Two expressions are equal if they are identical copies of a shared
4285 RTX or if they are both registers with the same register number
4286 and mode. */
4288 #define COMBINE_RTX_EQUAL_P(X,Y) \
4289 ((X) == (Y) \
4290 || (REG_P (X) && REG_P (Y) \
4291 && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
4293 if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
4295 n_occurrences++;
4296 return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
4299 /* If X and FROM are the same register but different modes, they
4300 will not have been seen as equal above. However, the log links code
4301 will make a LOG_LINKS entry for that case. If we do nothing, we
4302 will try to rerecognize our original insn and, when it succeeds,
4303 we will delete the feeding insn, which is incorrect.
4305 So force this insn not to match in this (rare) case. */
4306 if (! in_dest && code == REG && REG_P (from)
4307 && reg_overlap_mentioned_p (x, from))
4308 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
4310 /* If this is an object, we are done unless it is a MEM or LO_SUM, both
4311 of which may contain things that can be combined. */
4312 if (code != MEM && code != LO_SUM && OBJECT_P (x))
4313 return x;
4315 /* It is possible to have a subexpression appear twice in the insn.
4316 Suppose that FROM is a register that appears within TO.
4317 Then, after that subexpression has been scanned once by `subst',
4318 the second time it is scanned, TO may be found. If we were
4319 to scan TO here, we would find FROM within it and create a
4320 self-referent rtl structure which is completely wrong. */
4321 if (COMBINE_RTX_EQUAL_P (x, to))
4322 return to;
4324 /* Parallel asm_operands need special attention because all of the
4325 inputs are shared across the arms. Furthermore, unsharing the
4326 rtl results in recognition failures. Failure to handle this case
4327 specially can result in circular rtl.
4329 Solve this by doing a normal pass across the first entry of the
4330 parallel, and only processing the SET_DESTs of the subsequent
4331 entries. Ug. */
4333 if (code == PARALLEL
4334 && GET_CODE (XVECEXP (x, 0, 0)) == SET
4335 && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
4337 new_rtx = subst (XVECEXP (x, 0, 0), from, to, 0, unique_copy);
4339 /* If this substitution failed, this whole thing fails. */
4340 if (GET_CODE (new_rtx) == CLOBBER
4341 && XEXP (new_rtx, 0) == const0_rtx)
4342 return new_rtx;
4344 SUBST (XVECEXP (x, 0, 0), new_rtx);
4346 for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
4348 rtx dest = SET_DEST (XVECEXP (x, 0, i));
4350 if (!REG_P (dest)
4351 && GET_CODE (dest) != CC0
4352 && GET_CODE (dest) != PC)
4354 new_rtx = subst (dest, from, to, 0, unique_copy);
4356 /* If this substitution failed, this whole thing fails. */
4357 if (GET_CODE (new_rtx) == CLOBBER
4358 && XEXP (new_rtx, 0) == const0_rtx)
4359 return new_rtx;
4361 SUBST (SET_DEST (XVECEXP (x, 0, i)), new_rtx);
4365 else
4367 len = GET_RTX_LENGTH (code);
4368 fmt = GET_RTX_FORMAT (code);
4370 /* We don't need to process a SET_DEST that is a register, CC0,
4371 or PC, so set up to skip this common case. All other cases
4372 where we want to suppress replacing something inside a
4373 SET_SRC are handled via the IN_DEST operand. */
4374 if (code == SET
4375 && (REG_P (SET_DEST (x))
4376 || GET_CODE (SET_DEST (x)) == CC0
4377 || GET_CODE (SET_DEST (x)) == PC))
4378 fmt = "ie";
4380 /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
4381 constant. */
4382 if (fmt[0] == 'e')
4383 op0_mode = GET_MODE (XEXP (x, 0));
4385 for (i = 0; i < len; i++)
4387 if (fmt[i] == 'E')
4389 int j;
4390 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4392 if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
4394 new_rtx = (unique_copy && n_occurrences
4395 ? copy_rtx (to) : to);
4396 n_occurrences++;
4398 else
4400 new_rtx = subst (XVECEXP (x, i, j), from, to, 0,
4401 unique_copy);
4403 /* If this substitution failed, this whole thing
4404 fails. */
4405 if (GET_CODE (new_rtx) == CLOBBER
4406 && XEXP (new_rtx, 0) == const0_rtx)
4407 return new_rtx;
4410 SUBST (XVECEXP (x, i, j), new_rtx);
4413 else if (fmt[i] == 'e')
4415 /* If this is a register being set, ignore it. */
4416 new_rtx = XEXP (x, i);
4417 if (in_dest
4418 && i == 0
4419 && (((code == SUBREG || code == ZERO_EXTRACT)
4420 && REG_P (new_rtx))
4421 || code == STRICT_LOW_PART))
4424 else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
4426 /* In general, don't install a subreg involving two
4427 modes not tieable. It can worsen register
4428 allocation, and can even make invalid reload
4429 insns, since the reg inside may need to be copied
4430 from in the outside mode, and that may be invalid
4431 if it is an fp reg copied in integer mode.
4433 We allow two exceptions to this: It is valid if
4434 it is inside another SUBREG and the mode of that
4435 SUBREG and the mode of the inside of TO is
4436 tieable and it is valid if X is a SET that copies
4437 FROM to CC0. */
4439 if (GET_CODE (to) == SUBREG
4440 && ! MODES_TIEABLE_P (GET_MODE (to),
4441 GET_MODE (SUBREG_REG (to)))
4442 && ! (code == SUBREG
4443 && MODES_TIEABLE_P (GET_MODE (x),
4444 GET_MODE (SUBREG_REG (to))))
4445 #ifdef HAVE_cc0
4446 && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
4447 #endif
4449 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
4451 #ifdef CANNOT_CHANGE_MODE_CLASS
4452 if (code == SUBREG
4453 && REG_P (to)
4454 && REGNO (to) < FIRST_PSEUDO_REGISTER
4455 && REG_CANNOT_CHANGE_MODE_P (REGNO (to),
4456 GET_MODE (to),
4457 GET_MODE (x)))
4458 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
4459 #endif
4461 new_rtx = (unique_copy && n_occurrences ? copy_rtx (to) : to);
4462 n_occurrences++;
4464 else
4465 /* If we are in a SET_DEST, suppress most cases unless we
4466 have gone inside a MEM, in which case we want to
4467 simplify the address. We assume here that things that
4468 are actually part of the destination have their inner
4469 parts in the first expression. This is true for SUBREG,
4470 STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
4471 things aside from REG and MEM that should appear in a
4472 SET_DEST. */
4473 new_rtx = subst (XEXP (x, i), from, to,
4474 (((in_dest
4475 && (code == SUBREG || code == STRICT_LOW_PART
4476 || code == ZERO_EXTRACT))
4477 || code == SET)
4478 && i == 0), unique_copy);
4480 /* If we found that we will have to reject this combination,
4481 indicate that by returning the CLOBBER ourselves, rather than
4482 an expression containing it. This will speed things up as
4483 well as prevent accidents where two CLOBBERs are considered
4484 to be equal, thus producing an incorrect simplification. */
4486 if (GET_CODE (new_rtx) == CLOBBER && XEXP (new_rtx, 0) == const0_rtx)
4487 return new_rtx;
4489 if (GET_CODE (x) == SUBREG
4490 && (GET_CODE (new_rtx) == CONST_INT
4491 || GET_CODE (new_rtx) == CONST_DOUBLE))
4493 enum machine_mode mode = GET_MODE (x);
4495 x = simplify_subreg (GET_MODE (x), new_rtx,
4496 GET_MODE (SUBREG_REG (x)),
4497 SUBREG_BYTE (x));
4498 if (! x)
4499 x = gen_rtx_CLOBBER (mode, const0_rtx);
4501 else if (GET_CODE (new_rtx) == CONST_INT
4502 && GET_CODE (x) == ZERO_EXTEND)
4504 x = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
4505 new_rtx, GET_MODE (XEXP (x, 0)));
4506 gcc_assert (x);
4508 else
4509 SUBST (XEXP (x, i), new_rtx);
4514 /* Check if we are loading something from the constant pool via float
4515 extension; in this case we would undo compress_float_constant
4516 optimization and degenerate constant load to an immediate value. */
4517 if (GET_CODE (x) == FLOAT_EXTEND
4518 && MEM_P (XEXP (x, 0))
4519 && MEM_READONLY_P (XEXP (x, 0)))
4521 rtx tmp = avoid_constant_pool_reference (x);
4522 if (x != tmp)
4523 return x;
4526 /* Try to simplify X. If the simplification changed the code, it is likely
4527 that further simplification will help, so loop, but limit the number
4528 of repetitions that will be performed. */
4530 for (i = 0; i < 4; i++)
4532 /* If X is sufficiently simple, don't bother trying to do anything
4533 with it. */
4534 if (code != CONST_INT && code != REG && code != CLOBBER)
4535 x = combine_simplify_rtx (x, op0_mode, in_dest);
4537 if (GET_CODE (x) == code)
4538 break;
4540 code = GET_CODE (x);
4542 /* We no longer know the original mode of operand 0 since we
4543 have changed the form of X) */
4544 op0_mode = VOIDmode;
4547 return x;
4550 /* Simplify X, a piece of RTL. We just operate on the expression at the
4551 outer level; call `subst' to simplify recursively. Return the new
4552 expression.
4554 OP0_MODE is the original mode of XEXP (x, 0). IN_DEST is nonzero
4555 if we are inside a SET_DEST. */
4557 static rtx
4558 combine_simplify_rtx (rtx x, enum machine_mode op0_mode, int in_dest)
4560 enum rtx_code code = GET_CODE (x);
4561 enum machine_mode mode = GET_MODE (x);
4562 rtx temp;
4563 int i;
4565 /* If this is a commutative operation, put a constant last and a complex
4566 expression first. We don't need to do this for comparisons here. */
4567 if (COMMUTATIVE_ARITH_P (x)
4568 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
4570 temp = XEXP (x, 0);
4571 SUBST (XEXP (x, 0), XEXP (x, 1));
4572 SUBST (XEXP (x, 1), temp);
4575 /* If this is a simple operation applied to an IF_THEN_ELSE, try
4576 applying it to the arms of the IF_THEN_ELSE. This often simplifies
4577 things. Check for cases where both arms are testing the same
4578 condition.
4580 Don't do anything if all operands are very simple. */
4582 if ((BINARY_P (x)
4583 && ((!OBJECT_P (XEXP (x, 0))
4584 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
4585 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))
4586 || (!OBJECT_P (XEXP (x, 1))
4587 && ! (GET_CODE (XEXP (x, 1)) == SUBREG
4588 && OBJECT_P (SUBREG_REG (XEXP (x, 1)))))))
4589 || (UNARY_P (x)
4590 && (!OBJECT_P (XEXP (x, 0))
4591 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
4592 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))))
4594 rtx cond, true_rtx, false_rtx;
4596 cond = if_then_else_cond (x, &true_rtx, &false_rtx);
4597 if (cond != 0
4598 /* If everything is a comparison, what we have is highly unlikely
4599 to be simpler, so don't use it. */
4600 && ! (COMPARISON_P (x)
4601 && (COMPARISON_P (true_rtx) || COMPARISON_P (false_rtx))))
4603 rtx cop1 = const0_rtx;
4604 enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
4606 if (cond_code == NE && COMPARISON_P (cond))
4607 return x;
4609 /* Simplify the alternative arms; this may collapse the true and
4610 false arms to store-flag values. Be careful to use copy_rtx
4611 here since true_rtx or false_rtx might share RTL with x as a
4612 result of the if_then_else_cond call above. */
4613 true_rtx = subst (copy_rtx (true_rtx), pc_rtx, pc_rtx, 0, 0);
4614 false_rtx = subst (copy_rtx (false_rtx), pc_rtx, pc_rtx, 0, 0);
4616 /* If true_rtx and false_rtx are not general_operands, an if_then_else
4617 is unlikely to be simpler. */
4618 if (general_operand (true_rtx, VOIDmode)
4619 && general_operand (false_rtx, VOIDmode))
4621 enum rtx_code reversed;
4623 /* Restarting if we generate a store-flag expression will cause
4624 us to loop. Just drop through in this case. */
4626 /* If the result values are STORE_FLAG_VALUE and zero, we can
4627 just make the comparison operation. */
4628 if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
4629 x = simplify_gen_relational (cond_code, mode, VOIDmode,
4630 cond, cop1);
4631 else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
4632 && ((reversed = reversed_comparison_code_parts
4633 (cond_code, cond, cop1, NULL))
4634 != UNKNOWN))
4635 x = simplify_gen_relational (reversed, mode, VOIDmode,
4636 cond, cop1);
4638 /* Likewise, we can make the negate of a comparison operation
4639 if the result values are - STORE_FLAG_VALUE and zero. */
4640 else if (GET_CODE (true_rtx) == CONST_INT
4641 && INTVAL (true_rtx) == - STORE_FLAG_VALUE
4642 && false_rtx == const0_rtx)
4643 x = simplify_gen_unary (NEG, mode,
4644 simplify_gen_relational (cond_code,
4645 mode, VOIDmode,
4646 cond, cop1),
4647 mode);
4648 else if (GET_CODE (false_rtx) == CONST_INT
4649 && INTVAL (false_rtx) == - STORE_FLAG_VALUE
4650 && true_rtx == const0_rtx
4651 && ((reversed = reversed_comparison_code_parts
4652 (cond_code, cond, cop1, NULL))
4653 != UNKNOWN))
4654 x = simplify_gen_unary (NEG, mode,
4655 simplify_gen_relational (reversed,
4656 mode, VOIDmode,
4657 cond, cop1),
4658 mode);
4659 else
4660 return gen_rtx_IF_THEN_ELSE (mode,
4661 simplify_gen_relational (cond_code,
4662 mode,
4663 VOIDmode,
4664 cond,
4665 cop1),
4666 true_rtx, false_rtx);
4668 code = GET_CODE (x);
4669 op0_mode = VOIDmode;
4674 /* Try to fold this expression in case we have constants that weren't
4675 present before. */
4676 temp = 0;
4677 switch (GET_RTX_CLASS (code))
4679 case RTX_UNARY:
4680 if (op0_mode == VOIDmode)
4681 op0_mode = GET_MODE (XEXP (x, 0));
4682 temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
4683 break;
4684 case RTX_COMPARE:
4685 case RTX_COMM_COMPARE:
4687 enum machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
4688 if (cmp_mode == VOIDmode)
4690 cmp_mode = GET_MODE (XEXP (x, 1));
4691 if (cmp_mode == VOIDmode)
4692 cmp_mode = op0_mode;
4694 temp = simplify_relational_operation (code, mode, cmp_mode,
4695 XEXP (x, 0), XEXP (x, 1));
4697 break;
4698 case RTX_COMM_ARITH:
4699 case RTX_BIN_ARITH:
4700 temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
4701 break;
4702 case RTX_BITFIELD_OPS:
4703 case RTX_TERNARY:
4704 temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
4705 XEXP (x, 1), XEXP (x, 2));
4706 break;
4707 default:
4708 break;
4711 if (temp)
4713 x = temp;
4714 code = GET_CODE (temp);
4715 op0_mode = VOIDmode;
4716 mode = GET_MODE (temp);
4719 /* First see if we can apply the inverse distributive law. */
4720 if (code == PLUS || code == MINUS
4721 || code == AND || code == IOR || code == XOR)
4723 x = apply_distributive_law (x);
4724 code = GET_CODE (x);
4725 op0_mode = VOIDmode;
4728 /* If CODE is an associative operation not otherwise handled, see if we
4729 can associate some operands. This can win if they are constants or
4730 if they are logically related (i.e. (a & b) & a). */
4731 if ((code == PLUS || code == MINUS || code == MULT || code == DIV
4732 || code == AND || code == IOR || code == XOR
4733 || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
4734 && ((INTEGRAL_MODE_P (mode) && code != DIV)
4735 || (flag_associative_math && FLOAT_MODE_P (mode))))
4737 if (GET_CODE (XEXP (x, 0)) == code)
4739 rtx other = XEXP (XEXP (x, 0), 0);
4740 rtx inner_op0 = XEXP (XEXP (x, 0), 1);
4741 rtx inner_op1 = XEXP (x, 1);
4742 rtx inner;
4744 /* Make sure we pass the constant operand if any as the second
4745 one if this is a commutative operation. */
4746 if (CONSTANT_P (inner_op0) && COMMUTATIVE_ARITH_P (x))
4748 rtx tem = inner_op0;
4749 inner_op0 = inner_op1;
4750 inner_op1 = tem;
4752 inner = simplify_binary_operation (code == MINUS ? PLUS
4753 : code == DIV ? MULT
4754 : code,
4755 mode, inner_op0, inner_op1);
4757 /* For commutative operations, try the other pair if that one
4758 didn't simplify. */
4759 if (inner == 0 && COMMUTATIVE_ARITH_P (x))
4761 other = XEXP (XEXP (x, 0), 1);
4762 inner = simplify_binary_operation (code, mode,
4763 XEXP (XEXP (x, 0), 0),
4764 XEXP (x, 1));
4767 if (inner)
4768 return simplify_gen_binary (code, mode, other, inner);
4772 /* A little bit of algebraic simplification here. */
4773 switch (code)
4775 case MEM:
4776 /* Ensure that our address has any ASHIFTs converted to MULT in case
4777 address-recognizing predicates are called later. */
4778 temp = make_compound_operation (XEXP (x, 0), MEM);
4779 SUBST (XEXP (x, 0), temp);
4780 break;
4782 case SUBREG:
4783 if (op0_mode == VOIDmode)
4784 op0_mode = GET_MODE (SUBREG_REG (x));
4786 /* See if this can be moved to simplify_subreg. */
4787 if (CONSTANT_P (SUBREG_REG (x))
4788 && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
4789 /* Don't call gen_lowpart if the inner mode
4790 is VOIDmode and we cannot simplify it, as SUBREG without
4791 inner mode is invalid. */
4792 && (GET_MODE (SUBREG_REG (x)) != VOIDmode
4793 || gen_lowpart_common (mode, SUBREG_REG (x))))
4794 return gen_lowpart (mode, SUBREG_REG (x));
4796 if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
4797 break;
4799 rtx temp;
4800 temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
4801 SUBREG_BYTE (x));
4802 if (temp)
4803 return temp;
4806 /* Don't change the mode of the MEM if that would change the meaning
4807 of the address. */
4808 if (MEM_P (SUBREG_REG (x))
4809 && (MEM_VOLATILE_P (SUBREG_REG (x))
4810 || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0))))
4811 return gen_rtx_CLOBBER (mode, const0_rtx);
4813 /* Note that we cannot do any narrowing for non-constants since
4814 we might have been counting on using the fact that some bits were
4815 zero. We now do this in the SET. */
4817 break;
4819 case NEG:
4820 temp = expand_compound_operation (XEXP (x, 0));
4822 /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
4823 replaced by (lshiftrt X C). This will convert
4824 (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y). */
4826 if (GET_CODE (temp) == ASHIFTRT
4827 && GET_CODE (XEXP (temp, 1)) == CONST_INT
4828 && INTVAL (XEXP (temp, 1)) == GET_MODE_BITSIZE (mode) - 1)
4829 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (temp, 0),
4830 INTVAL (XEXP (temp, 1)));
4832 /* If X has only a single bit that might be nonzero, say, bit I, convert
4833 (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
4834 MODE minus 1. This will convert (neg (zero_extract X 1 Y)) to
4835 (sign_extract X 1 Y). But only do this if TEMP isn't a register
4836 or a SUBREG of one since we'd be making the expression more
4837 complex if it was just a register. */
4839 if (!REG_P (temp)
4840 && ! (GET_CODE (temp) == SUBREG
4841 && REG_P (SUBREG_REG (temp)))
4842 && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
4844 rtx temp1 = simplify_shift_const
4845 (NULL_RTX, ASHIFTRT, mode,
4846 simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
4847 GET_MODE_BITSIZE (mode) - 1 - i),
4848 GET_MODE_BITSIZE (mode) - 1 - i);
4850 /* If all we did was surround TEMP with the two shifts, we
4851 haven't improved anything, so don't use it. Otherwise,
4852 we are better off with TEMP1. */
4853 if (GET_CODE (temp1) != ASHIFTRT
4854 || GET_CODE (XEXP (temp1, 0)) != ASHIFT
4855 || XEXP (XEXP (temp1, 0), 0) != temp)
4856 return temp1;
4858 break;
4860 case TRUNCATE:
4861 /* We can't handle truncation to a partial integer mode here
4862 because we don't know the real bitsize of the partial
4863 integer mode. */
4864 if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
4865 break;
4867 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4868 && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
4869 GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))))
4870 SUBST (XEXP (x, 0),
4871 force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
4872 GET_MODE_MASK (mode), 0));
4874 /* Similarly to what we do in simplify-rtx.c, a truncate of a register
4875 whose value is a comparison can be replaced with a subreg if
4876 STORE_FLAG_VALUE permits. */
4877 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4878 && ((HOST_WIDE_INT) STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
4879 && (temp = get_last_value (XEXP (x, 0)))
4880 && COMPARISON_P (temp))
4881 return gen_lowpart (mode, XEXP (x, 0));
4882 break;
4884 #ifdef HAVE_cc0
4885 case COMPARE:
4886 /* Convert (compare FOO (const_int 0)) to FOO unless we aren't
4887 using cc0, in which case we want to leave it as a COMPARE
4888 so we can distinguish it from a register-register-copy. */
4889 if (XEXP (x, 1) == const0_rtx)
4890 return XEXP (x, 0);
4892 /* x - 0 is the same as x unless x's mode has signed zeros and
4893 allows rounding towards -infinity. Under those conditions,
4894 0 - 0 is -0. */
4895 if (!(HONOR_SIGNED_ZEROS (GET_MODE (XEXP (x, 0)))
4896 && HONOR_SIGN_DEPENDENT_ROUNDING (GET_MODE (XEXP (x, 0))))
4897 && XEXP (x, 1) == CONST0_RTX (GET_MODE (XEXP (x, 0))))
4898 return XEXP (x, 0);
4899 break;
4900 #endif
4902 case CONST:
4903 /* (const (const X)) can become (const X). Do it this way rather than
4904 returning the inner CONST since CONST can be shared with a
4905 REG_EQUAL note. */
4906 if (GET_CODE (XEXP (x, 0)) == CONST)
4907 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
4908 break;
4910 #ifdef HAVE_lo_sum
4911 case LO_SUM:
4912 /* Convert (lo_sum (high FOO) FOO) to FOO. This is necessary so we
4913 can add in an offset. find_split_point will split this address up
4914 again if it doesn't match. */
4915 if (GET_CODE (XEXP (x, 0)) == HIGH
4916 && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
4917 return XEXP (x, 1);
4918 break;
4919 #endif
4921 case PLUS:
4922 /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
4923 when c is (const_int (pow2 + 1) / 2) is a sign extension of a
4924 bit-field and can be replaced by either a sign_extend or a
4925 sign_extract. The `and' may be a zero_extend and the two
4926 <c>, -<c> constants may be reversed. */
4927 if (GET_CODE (XEXP (x, 0)) == XOR
4928 && GET_CODE (XEXP (x, 1)) == CONST_INT
4929 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
4930 && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
4931 && ((i = exact_log2 (INTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
4932 || (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
4933 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4934 && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
4935 && GET_CODE (XEXP (XEXP (XEXP (x, 0), 0), 1)) == CONST_INT
4936 && (INTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
4937 == ((HOST_WIDE_INT) 1 << (i + 1)) - 1))
4938 || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
4939 && (GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
4940 == (unsigned int) i + 1))))
4941 return simplify_shift_const
4942 (NULL_RTX, ASHIFTRT, mode,
4943 simplify_shift_const (NULL_RTX, ASHIFT, mode,
4944 XEXP (XEXP (XEXP (x, 0), 0), 0),
4945 GET_MODE_BITSIZE (mode) - (i + 1)),
4946 GET_MODE_BITSIZE (mode) - (i + 1));
4948 /* If only the low-order bit of X is possibly nonzero, (plus x -1)
4949 can become (ashiftrt (ashift (xor x 1) C) C) where C is
4950 the bitsize of the mode - 1. This allows simplification of
4951 "a = (b & 8) == 0;" */
4952 if (XEXP (x, 1) == constm1_rtx
4953 && !REG_P (XEXP (x, 0))
4954 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
4955 && REG_P (SUBREG_REG (XEXP (x, 0))))
4956 && nonzero_bits (XEXP (x, 0), mode) == 1)
4957 return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
4958 simplify_shift_const (NULL_RTX, ASHIFT, mode,
4959 gen_rtx_XOR (mode, XEXP (x, 0), const1_rtx),
4960 GET_MODE_BITSIZE (mode) - 1),
4961 GET_MODE_BITSIZE (mode) - 1);
4963 /* If we are adding two things that have no bits in common, convert
4964 the addition into an IOR. This will often be further simplified,
4965 for example in cases like ((a & 1) + (a & 2)), which can
4966 become a & 3. */
4968 if (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
4969 && (nonzero_bits (XEXP (x, 0), mode)
4970 & nonzero_bits (XEXP (x, 1), mode)) == 0)
4972 /* Try to simplify the expression further. */
4973 rtx tor = simplify_gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
4974 temp = combine_simplify_rtx (tor, mode, in_dest);
4976 /* If we could, great. If not, do not go ahead with the IOR
4977 replacement, since PLUS appears in many special purpose
4978 address arithmetic instructions. */
4979 if (GET_CODE (temp) != CLOBBER && temp != tor)
4980 return temp;
4982 break;
4984 case MINUS:
4985 /* (minus <foo> (and <foo> (const_int -pow2))) becomes
4986 (and <foo> (const_int pow2-1)) */
4987 if (GET_CODE (XEXP (x, 1)) == AND
4988 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
4989 && exact_log2 (-INTVAL (XEXP (XEXP (x, 1), 1))) >= 0
4990 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
4991 return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
4992 -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
4993 break;
4995 case MULT:
4996 /* If we have (mult (plus A B) C), apply the distributive law and then
4997 the inverse distributive law to see if things simplify. This
4998 occurs mostly in addresses, often when unrolling loops. */
5000 if (GET_CODE (XEXP (x, 0)) == PLUS)
5002 rtx result = distribute_and_simplify_rtx (x, 0);
5003 if (result)
5004 return result;
5007 /* Try simplify a*(b/c) as (a*b)/c. */
5008 if (FLOAT_MODE_P (mode) && flag_associative_math
5009 && GET_CODE (XEXP (x, 0)) == DIV)
5011 rtx tem = simplify_binary_operation (MULT, mode,
5012 XEXP (XEXP (x, 0), 0),
5013 XEXP (x, 1));
5014 if (tem)
5015 return simplify_gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
5017 break;
5019 case UDIV:
5020 /* If this is a divide by a power of two, treat it as a shift if
5021 its first operand is a shift. */
5022 if (GET_CODE (XEXP (x, 1)) == CONST_INT
5023 && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0
5024 && (GET_CODE (XEXP (x, 0)) == ASHIFT
5025 || GET_CODE (XEXP (x, 0)) == LSHIFTRT
5026 || GET_CODE (XEXP (x, 0)) == ASHIFTRT
5027 || GET_CODE (XEXP (x, 0)) == ROTATE
5028 || GET_CODE (XEXP (x, 0)) == ROTATERT))
5029 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
5030 break;
5032 case EQ: case NE:
5033 case GT: case GTU: case GE: case GEU:
5034 case LT: case LTU: case LE: case LEU:
5035 case UNEQ: case LTGT:
5036 case UNGT: case UNGE:
5037 case UNLT: case UNLE:
5038 case UNORDERED: case ORDERED:
5039 /* If the first operand is a condition code, we can't do anything
5040 with it. */
5041 if (GET_CODE (XEXP (x, 0)) == COMPARE
5042 || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
5043 && ! CC0_P (XEXP (x, 0))))
5045 rtx op0 = XEXP (x, 0);
5046 rtx op1 = XEXP (x, 1);
5047 enum rtx_code new_code;
5049 if (GET_CODE (op0) == COMPARE)
5050 op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
5052 /* Simplify our comparison, if possible. */
5053 new_code = simplify_comparison (code, &op0, &op1);
5055 /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
5056 if only the low-order bit is possibly nonzero in X (such as when
5057 X is a ZERO_EXTRACT of one bit). Similarly, we can convert EQ to
5058 (xor X 1) or (minus 1 X); we use the former. Finally, if X is
5059 known to be either 0 or -1, NE becomes a NEG and EQ becomes
5060 (plus X 1).
5062 Remove any ZERO_EXTRACT we made when thinking this was a
5063 comparison. It may now be simpler to use, e.g., an AND. If a
5064 ZERO_EXTRACT is indeed appropriate, it will be placed back by
5065 the call to make_compound_operation in the SET case. */
5067 if (STORE_FLAG_VALUE == 1
5068 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5069 && op1 == const0_rtx
5070 && mode == GET_MODE (op0)
5071 && nonzero_bits (op0, mode) == 1)
5072 return gen_lowpart (mode,
5073 expand_compound_operation (op0));
5075 else if (STORE_FLAG_VALUE == 1
5076 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5077 && op1 == const0_rtx
5078 && mode == GET_MODE (op0)
5079 && (num_sign_bit_copies (op0, mode)
5080 == GET_MODE_BITSIZE (mode)))
5082 op0 = expand_compound_operation (op0);
5083 return simplify_gen_unary (NEG, mode,
5084 gen_lowpart (mode, op0),
5085 mode);
5088 else if (STORE_FLAG_VALUE == 1
5089 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5090 && op1 == const0_rtx
5091 && mode == GET_MODE (op0)
5092 && nonzero_bits (op0, mode) == 1)
5094 op0 = expand_compound_operation (op0);
5095 return simplify_gen_binary (XOR, mode,
5096 gen_lowpart (mode, op0),
5097 const1_rtx);
5100 else if (STORE_FLAG_VALUE == 1
5101 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5102 && op1 == const0_rtx
5103 && mode == GET_MODE (op0)
5104 && (num_sign_bit_copies (op0, mode)
5105 == GET_MODE_BITSIZE (mode)))
5107 op0 = expand_compound_operation (op0);
5108 return plus_constant (gen_lowpart (mode, op0), 1);
5111 /* If STORE_FLAG_VALUE is -1, we have cases similar to
5112 those above. */
5113 if (STORE_FLAG_VALUE == -1
5114 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5115 && op1 == const0_rtx
5116 && (num_sign_bit_copies (op0, mode)
5117 == GET_MODE_BITSIZE (mode)))
5118 return gen_lowpart (mode,
5119 expand_compound_operation (op0));
5121 else if (STORE_FLAG_VALUE == -1
5122 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5123 && op1 == const0_rtx
5124 && mode == GET_MODE (op0)
5125 && nonzero_bits (op0, mode) == 1)
5127 op0 = expand_compound_operation (op0);
5128 return simplify_gen_unary (NEG, mode,
5129 gen_lowpart (mode, op0),
5130 mode);
5133 else if (STORE_FLAG_VALUE == -1
5134 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5135 && op1 == const0_rtx
5136 && mode == GET_MODE (op0)
5137 && (num_sign_bit_copies (op0, mode)
5138 == GET_MODE_BITSIZE (mode)))
5140 op0 = expand_compound_operation (op0);
5141 return simplify_gen_unary (NOT, mode,
5142 gen_lowpart (mode, op0),
5143 mode);
5146 /* If X is 0/1, (eq X 0) is X-1. */
5147 else if (STORE_FLAG_VALUE == -1
5148 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5149 && op1 == const0_rtx
5150 && mode == GET_MODE (op0)
5151 && nonzero_bits (op0, mode) == 1)
5153 op0 = expand_compound_operation (op0);
5154 return plus_constant (gen_lowpart (mode, op0), -1);
5157 /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
5158 one bit that might be nonzero, we can convert (ne x 0) to
5159 (ashift x c) where C puts the bit in the sign bit. Remove any
5160 AND with STORE_FLAG_VALUE when we are done, since we are only
5161 going to test the sign bit. */
5162 if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5163 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5164 && ((STORE_FLAG_VALUE & GET_MODE_MASK (mode))
5165 == (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1))
5166 && op1 == const0_rtx
5167 && mode == GET_MODE (op0)
5168 && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
5170 x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
5171 expand_compound_operation (op0),
5172 GET_MODE_BITSIZE (mode) - 1 - i);
5173 if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
5174 return XEXP (x, 0);
5175 else
5176 return x;
5179 /* If the code changed, return a whole new comparison. */
5180 if (new_code != code)
5181 return gen_rtx_fmt_ee (new_code, mode, op0, op1);
5183 /* Otherwise, keep this operation, but maybe change its operands.
5184 This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR). */
5185 SUBST (XEXP (x, 0), op0);
5186 SUBST (XEXP (x, 1), op1);
5188 break;
5190 case IF_THEN_ELSE:
5191 return simplify_if_then_else (x);
5193 case ZERO_EXTRACT:
5194 case SIGN_EXTRACT:
5195 case ZERO_EXTEND:
5196 case SIGN_EXTEND:
5197 /* If we are processing SET_DEST, we are done. */
5198 if (in_dest)
5199 return x;
5201 return expand_compound_operation (x);
5203 case SET:
5204 return simplify_set (x);
5206 case AND:
5207 case IOR:
5208 return simplify_logical (x);
5210 case ASHIFT:
5211 case LSHIFTRT:
5212 case ASHIFTRT:
5213 case ROTATE:
5214 case ROTATERT:
5215 /* If this is a shift by a constant amount, simplify it. */
5216 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
5217 return simplify_shift_const (x, code, mode, XEXP (x, 0),
5218 INTVAL (XEXP (x, 1)));
5220 else if (SHIFT_COUNT_TRUNCATED && !REG_P (XEXP (x, 1)))
5221 SUBST (XEXP (x, 1),
5222 force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
5223 ((HOST_WIDE_INT) 1
5224 << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
5225 - 1,
5226 0));
5227 break;
5229 default:
5230 break;
5233 return x;
5236 /* Simplify X, an IF_THEN_ELSE expression. Return the new expression. */
5238 static rtx
5239 simplify_if_then_else (rtx x)
5241 enum machine_mode mode = GET_MODE (x);
5242 rtx cond = XEXP (x, 0);
5243 rtx true_rtx = XEXP (x, 1);
5244 rtx false_rtx = XEXP (x, 2);
5245 enum rtx_code true_code = GET_CODE (cond);
5246 int comparison_p = COMPARISON_P (cond);
5247 rtx temp;
5248 int i;
5249 enum rtx_code false_code;
5250 rtx reversed;
5252 /* Simplify storing of the truth value. */
5253 if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
5254 return simplify_gen_relational (true_code, mode, VOIDmode,
5255 XEXP (cond, 0), XEXP (cond, 1));
5257 /* Also when the truth value has to be reversed. */
5258 if (comparison_p
5259 && true_rtx == const0_rtx && false_rtx == const_true_rtx
5260 && (reversed = reversed_comparison (cond, mode)))
5261 return reversed;
5263 /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
5264 in it is being compared against certain values. Get the true and false
5265 comparisons and see if that says anything about the value of each arm. */
5267 if (comparison_p
5268 && ((false_code = reversed_comparison_code (cond, NULL))
5269 != UNKNOWN)
5270 && REG_P (XEXP (cond, 0)))
5272 HOST_WIDE_INT nzb;
5273 rtx from = XEXP (cond, 0);
5274 rtx true_val = XEXP (cond, 1);
5275 rtx false_val = true_val;
5276 int swapped = 0;
5278 /* If FALSE_CODE is EQ, swap the codes and arms. */
5280 if (false_code == EQ)
5282 swapped = 1, true_code = EQ, false_code = NE;
5283 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
5286 /* If we are comparing against zero and the expression being tested has
5287 only a single bit that might be nonzero, that is its value when it is
5288 not equal to zero. Similarly if it is known to be -1 or 0. */
5290 if (true_code == EQ && true_val == const0_rtx
5291 && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
5293 false_code = EQ;
5294 false_val = GEN_INT (trunc_int_for_mode (nzb, GET_MODE (from)));
5296 else if (true_code == EQ && true_val == const0_rtx
5297 && (num_sign_bit_copies (from, GET_MODE (from))
5298 == GET_MODE_BITSIZE (GET_MODE (from))))
5300 false_code = EQ;
5301 false_val = constm1_rtx;
5304 /* Now simplify an arm if we know the value of the register in the
5305 branch and it is used in the arm. Be careful due to the potential
5306 of locally-shared RTL. */
5308 if (reg_mentioned_p (from, true_rtx))
5309 true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
5310 from, true_val),
5311 pc_rtx, pc_rtx, 0, 0);
5312 if (reg_mentioned_p (from, false_rtx))
5313 false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
5314 from, false_val),
5315 pc_rtx, pc_rtx, 0, 0);
5317 SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
5318 SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
5320 true_rtx = XEXP (x, 1);
5321 false_rtx = XEXP (x, 2);
5322 true_code = GET_CODE (cond);
5325 /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
5326 reversed, do so to avoid needing two sets of patterns for
5327 subtract-and-branch insns. Similarly if we have a constant in the true
5328 arm, the false arm is the same as the first operand of the comparison, or
5329 the false arm is more complicated than the true arm. */
5331 if (comparison_p
5332 && reversed_comparison_code (cond, NULL) != UNKNOWN
5333 && (true_rtx == pc_rtx
5334 || (CONSTANT_P (true_rtx)
5335 && GET_CODE (false_rtx) != CONST_INT && false_rtx != pc_rtx)
5336 || true_rtx == const0_rtx
5337 || (OBJECT_P (true_rtx) && !OBJECT_P (false_rtx))
5338 || (GET_CODE (true_rtx) == SUBREG && OBJECT_P (SUBREG_REG (true_rtx))
5339 && !OBJECT_P (false_rtx))
5340 || reg_mentioned_p (true_rtx, false_rtx)
5341 || rtx_equal_p (false_rtx, XEXP (cond, 0))))
5343 true_code = reversed_comparison_code (cond, NULL);
5344 SUBST (XEXP (x, 0), reversed_comparison (cond, GET_MODE (cond)));
5345 SUBST (XEXP (x, 1), false_rtx);
5346 SUBST (XEXP (x, 2), true_rtx);
5348 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
5349 cond = XEXP (x, 0);
5351 /* It is possible that the conditional has been simplified out. */
5352 true_code = GET_CODE (cond);
5353 comparison_p = COMPARISON_P (cond);
5356 /* If the two arms are identical, we don't need the comparison. */
5358 if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
5359 return true_rtx;
5361 /* Convert a == b ? b : a to "a". */
5362 if (true_code == EQ && ! side_effects_p (cond)
5363 && !HONOR_NANS (mode)
5364 && rtx_equal_p (XEXP (cond, 0), false_rtx)
5365 && rtx_equal_p (XEXP (cond, 1), true_rtx))
5366 return false_rtx;
5367 else if (true_code == NE && ! side_effects_p (cond)
5368 && !HONOR_NANS (mode)
5369 && rtx_equal_p (XEXP (cond, 0), true_rtx)
5370 && rtx_equal_p (XEXP (cond, 1), false_rtx))
5371 return true_rtx;
5373 /* Look for cases where we have (abs x) or (neg (abs X)). */
5375 if (GET_MODE_CLASS (mode) == MODE_INT
5376 && comparison_p
5377 && XEXP (cond, 1) == const0_rtx
5378 && GET_CODE (false_rtx) == NEG
5379 && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
5380 && rtx_equal_p (true_rtx, XEXP (cond, 0))
5381 && ! side_effects_p (true_rtx))
5382 switch (true_code)
5384 case GT:
5385 case GE:
5386 return simplify_gen_unary (ABS, mode, true_rtx, mode);
5387 case LT:
5388 case LE:
5389 return
5390 simplify_gen_unary (NEG, mode,
5391 simplify_gen_unary (ABS, mode, true_rtx, mode),
5392 mode);
5393 default:
5394 break;
5397 /* Look for MIN or MAX. */
5399 if ((! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
5400 && comparison_p
5401 && rtx_equal_p (XEXP (cond, 0), true_rtx)
5402 && rtx_equal_p (XEXP (cond, 1), false_rtx)
5403 && ! side_effects_p (cond))
5404 switch (true_code)
5406 case GE:
5407 case GT:
5408 return simplify_gen_binary (SMAX, mode, true_rtx, false_rtx);
5409 case LE:
5410 case LT:
5411 return simplify_gen_binary (SMIN, mode, true_rtx, false_rtx);
5412 case GEU:
5413 case GTU:
5414 return simplify_gen_binary (UMAX, mode, true_rtx, false_rtx);
5415 case LEU:
5416 case LTU:
5417 return simplify_gen_binary (UMIN, mode, true_rtx, false_rtx);
5418 default:
5419 break;
5422 /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
5423 second operand is zero, this can be done as (OP Z (mult COND C2)) where
5424 C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
5425 SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
5426 We can do this kind of thing in some cases when STORE_FLAG_VALUE is
5427 neither 1 or -1, but it isn't worth checking for. */
5429 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
5430 && comparison_p
5431 && GET_MODE_CLASS (mode) == MODE_INT
5432 && ! side_effects_p (x))
5434 rtx t = make_compound_operation (true_rtx, SET);
5435 rtx f = make_compound_operation (false_rtx, SET);
5436 rtx cond_op0 = XEXP (cond, 0);
5437 rtx cond_op1 = XEXP (cond, 1);
5438 enum rtx_code op = UNKNOWN, extend_op = UNKNOWN;
5439 enum machine_mode m = mode;
5440 rtx z = 0, c1 = NULL_RTX;
5442 if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
5443 || GET_CODE (t) == IOR || GET_CODE (t) == XOR
5444 || GET_CODE (t) == ASHIFT
5445 || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
5446 && rtx_equal_p (XEXP (t, 0), f))
5447 c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
5449 /* If an identity-zero op is commutative, check whether there
5450 would be a match if we swapped the operands. */
5451 else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
5452 || GET_CODE (t) == XOR)
5453 && rtx_equal_p (XEXP (t, 1), f))
5454 c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
5455 else if (GET_CODE (t) == SIGN_EXTEND
5456 && (GET_CODE (XEXP (t, 0)) == PLUS
5457 || GET_CODE (XEXP (t, 0)) == MINUS
5458 || GET_CODE (XEXP (t, 0)) == IOR
5459 || GET_CODE (XEXP (t, 0)) == XOR
5460 || GET_CODE (XEXP (t, 0)) == ASHIFT
5461 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
5462 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
5463 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
5464 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
5465 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
5466 && (num_sign_bit_copies (f, GET_MODE (f))
5467 > (unsigned int)
5468 (GET_MODE_BITSIZE (mode)
5469 - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 0))))))
5471 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
5472 extend_op = SIGN_EXTEND;
5473 m = GET_MODE (XEXP (t, 0));
5475 else if (GET_CODE (t) == SIGN_EXTEND
5476 && (GET_CODE (XEXP (t, 0)) == PLUS
5477 || GET_CODE (XEXP (t, 0)) == IOR
5478 || GET_CODE (XEXP (t, 0)) == XOR)
5479 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
5480 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
5481 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
5482 && (num_sign_bit_copies (f, GET_MODE (f))
5483 > (unsigned int)
5484 (GET_MODE_BITSIZE (mode)
5485 - GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (t, 0), 1))))))
5487 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
5488 extend_op = SIGN_EXTEND;
5489 m = GET_MODE (XEXP (t, 0));
5491 else if (GET_CODE (t) == ZERO_EXTEND
5492 && (GET_CODE (XEXP (t, 0)) == PLUS
5493 || GET_CODE (XEXP (t, 0)) == MINUS
5494 || GET_CODE (XEXP (t, 0)) == IOR
5495 || GET_CODE (XEXP (t, 0)) == XOR
5496 || GET_CODE (XEXP (t, 0)) == ASHIFT
5497 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
5498 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
5499 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
5500 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5501 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
5502 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
5503 && ((nonzero_bits (f, GET_MODE (f))
5504 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
5505 == 0))
5507 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
5508 extend_op = ZERO_EXTEND;
5509 m = GET_MODE (XEXP (t, 0));
5511 else if (GET_CODE (t) == ZERO_EXTEND
5512 && (GET_CODE (XEXP (t, 0)) == PLUS
5513 || GET_CODE (XEXP (t, 0)) == IOR
5514 || GET_CODE (XEXP (t, 0)) == XOR)
5515 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
5516 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5517 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
5518 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
5519 && ((nonzero_bits (f, GET_MODE (f))
5520 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
5521 == 0))
5523 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
5524 extend_op = ZERO_EXTEND;
5525 m = GET_MODE (XEXP (t, 0));
5528 if (z)
5530 temp = subst (simplify_gen_relational (true_code, m, VOIDmode,
5531 cond_op0, cond_op1),
5532 pc_rtx, pc_rtx, 0, 0);
5533 temp = simplify_gen_binary (MULT, m, temp,
5534 simplify_gen_binary (MULT, m, c1,
5535 const_true_rtx));
5536 temp = subst (temp, pc_rtx, pc_rtx, 0, 0);
5537 temp = simplify_gen_binary (op, m, gen_lowpart (m, z), temp);
5539 if (extend_op != UNKNOWN)
5540 temp = simplify_gen_unary (extend_op, mode, temp, m);
5542 return temp;
5546 /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
5547 1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
5548 negation of a single bit, we can convert this operation to a shift. We
5549 can actually do this more generally, but it doesn't seem worth it. */
5551 if (true_code == NE && XEXP (cond, 1) == const0_rtx
5552 && false_rtx == const0_rtx && GET_CODE (true_rtx) == CONST_INT
5553 && ((1 == nonzero_bits (XEXP (cond, 0), mode)
5554 && (i = exact_log2 (INTVAL (true_rtx))) >= 0)
5555 || ((num_sign_bit_copies (XEXP (cond, 0), mode)
5556 == GET_MODE_BITSIZE (mode))
5557 && (i = exact_log2 (-INTVAL (true_rtx))) >= 0)))
5558 return
5559 simplify_shift_const (NULL_RTX, ASHIFT, mode,
5560 gen_lowpart (mode, XEXP (cond, 0)), i);
5562 /* (IF_THEN_ELSE (NE REG 0) (0) (8)) is REG for nonzero_bits (REG) == 8. */
5563 if (true_code == NE && XEXP (cond, 1) == const0_rtx
5564 && false_rtx == const0_rtx && GET_CODE (true_rtx) == CONST_INT
5565 && GET_MODE (XEXP (cond, 0)) == mode
5566 && (INTVAL (true_rtx) & GET_MODE_MASK (mode))
5567 == nonzero_bits (XEXP (cond, 0), mode)
5568 && (i = exact_log2 (INTVAL (true_rtx) & GET_MODE_MASK (mode))) >= 0)
5569 return XEXP (cond, 0);
5571 return x;
5574 /* Simplify X, a SET expression. Return the new expression. */
5576 static rtx
5577 simplify_set (rtx x)
5579 rtx src = SET_SRC (x);
5580 rtx dest = SET_DEST (x);
5581 enum machine_mode mode
5582 = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
5583 rtx other_insn;
5584 rtx *cc_use;
5586 /* (set (pc) (return)) gets written as (return). */
5587 if (GET_CODE (dest) == PC && GET_CODE (src) == RETURN)
5588 return src;
5590 /* Now that we know for sure which bits of SRC we are using, see if we can
5591 simplify the expression for the object knowing that we only need the
5592 low-order bits. */
5594 if (GET_MODE_CLASS (mode) == MODE_INT
5595 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
5597 src = force_to_mode (src, mode, ~(HOST_WIDE_INT) 0, 0);
5598 SUBST (SET_SRC (x), src);
5601 /* If we are setting CC0 or if the source is a COMPARE, look for the use of
5602 the comparison result and try to simplify it unless we already have used
5603 undobuf.other_insn. */
5604 if ((GET_MODE_CLASS (mode) == MODE_CC
5605 || GET_CODE (src) == COMPARE
5606 || CC0_P (dest))
5607 && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
5608 && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
5609 && COMPARISON_P (*cc_use)
5610 && rtx_equal_p (XEXP (*cc_use, 0), dest))
5612 enum rtx_code old_code = GET_CODE (*cc_use);
5613 enum rtx_code new_code;
5614 rtx op0, op1, tmp;
5615 int other_changed = 0;
5616 enum machine_mode compare_mode = GET_MODE (dest);
5618 if (GET_CODE (src) == COMPARE)
5619 op0 = XEXP (src, 0), op1 = XEXP (src, 1);
5620 else
5621 op0 = src, op1 = CONST0_RTX (GET_MODE (src));
5623 tmp = simplify_relational_operation (old_code, compare_mode, VOIDmode,
5624 op0, op1);
5625 if (!tmp)
5626 new_code = old_code;
5627 else if (!CONSTANT_P (tmp))
5629 new_code = GET_CODE (tmp);
5630 op0 = XEXP (tmp, 0);
5631 op1 = XEXP (tmp, 1);
5633 else
5635 rtx pat = PATTERN (other_insn);
5636 undobuf.other_insn = other_insn;
5637 SUBST (*cc_use, tmp);
5639 /* Attempt to simplify CC user. */
5640 if (GET_CODE (pat) == SET)
5642 rtx new_rtx = simplify_rtx (SET_SRC (pat));
5643 if (new_rtx != NULL_RTX)
5644 SUBST (SET_SRC (pat), new_rtx);
5647 /* Convert X into a no-op move. */
5648 SUBST (SET_DEST (x), pc_rtx);
5649 SUBST (SET_SRC (x), pc_rtx);
5650 return x;
5653 /* Simplify our comparison, if possible. */
5654 new_code = simplify_comparison (new_code, &op0, &op1);
5656 #ifdef SELECT_CC_MODE
5657 /* If this machine has CC modes other than CCmode, check to see if we
5658 need to use a different CC mode here. */
5659 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
5660 compare_mode = GET_MODE (op0);
5661 else
5662 compare_mode = SELECT_CC_MODE (new_code, op0, op1);
5664 #ifndef HAVE_cc0
5665 /* If the mode changed, we have to change SET_DEST, the mode in the
5666 compare, and the mode in the place SET_DEST is used. If SET_DEST is
5667 a hard register, just build new versions with the proper mode. If it
5668 is a pseudo, we lose unless it is only time we set the pseudo, in
5669 which case we can safely change its mode. */
5670 if (compare_mode != GET_MODE (dest))
5672 if (can_change_dest_mode (dest, 0, compare_mode))
5674 unsigned int regno = REGNO (dest);
5675 rtx new_dest;
5677 if (regno < FIRST_PSEUDO_REGISTER)
5678 new_dest = gen_rtx_REG (compare_mode, regno);
5679 else
5681 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
5682 new_dest = regno_reg_rtx[regno];
5685 SUBST (SET_DEST (x), new_dest);
5686 SUBST (XEXP (*cc_use, 0), new_dest);
5687 other_changed = 1;
5689 dest = new_dest;
5692 #endif /* cc0 */
5693 #endif /* SELECT_CC_MODE */
5695 /* If the code changed, we have to build a new comparison in
5696 undobuf.other_insn. */
5697 if (new_code != old_code)
5699 int other_changed_previously = other_changed;
5700 unsigned HOST_WIDE_INT mask;
5702 SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
5703 dest, const0_rtx));
5704 other_changed = 1;
5706 /* If the only change we made was to change an EQ into an NE or
5707 vice versa, OP0 has only one bit that might be nonzero, and OP1
5708 is zero, check if changing the user of the condition code will
5709 produce a valid insn. If it won't, we can keep the original code
5710 in that insn by surrounding our operation with an XOR. */
5712 if (((old_code == NE && new_code == EQ)
5713 || (old_code == EQ && new_code == NE))
5714 && ! other_changed_previously && op1 == const0_rtx
5715 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
5716 && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
5718 rtx pat = PATTERN (other_insn), note = 0;
5720 if ((recog_for_combine (&pat, other_insn, &note) < 0
5721 && ! check_asm_operands (pat)))
5723 PUT_CODE (*cc_use, old_code);
5724 other_changed = 0;
5726 op0 = simplify_gen_binary (XOR, GET_MODE (op0),
5727 op0, GEN_INT (mask));
5732 if (other_changed)
5733 undobuf.other_insn = other_insn;
5735 #ifdef HAVE_cc0
5736 /* If we are now comparing against zero, change our source if
5737 needed. If we do not use cc0, we always have a COMPARE. */
5738 if (op1 == const0_rtx && dest == cc0_rtx)
5740 SUBST (SET_SRC (x), op0);
5741 src = op0;
5743 else
5744 #endif
5746 /* Otherwise, if we didn't previously have a COMPARE in the
5747 correct mode, we need one. */
5748 if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
5750 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
5751 src = SET_SRC (x);
5753 else if (GET_MODE (op0) == compare_mode && op1 == const0_rtx)
5755 SUBST (SET_SRC (x), op0);
5756 src = SET_SRC (x);
5758 /* Otherwise, update the COMPARE if needed. */
5759 else if (XEXP (src, 0) != op0 || XEXP (src, 1) != op1)
5761 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
5762 src = SET_SRC (x);
5765 else
5767 /* Get SET_SRC in a form where we have placed back any
5768 compound expressions. Then do the checks below. */
5769 src = make_compound_operation (src, SET);
5770 SUBST (SET_SRC (x), src);
5773 /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
5774 and X being a REG or (subreg (reg)), we may be able to convert this to
5775 (set (subreg:m2 x) (op)).
5777 We can always do this if M1 is narrower than M2 because that means that
5778 we only care about the low bits of the result.
5780 However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
5781 perform a narrower operation than requested since the high-order bits will
5782 be undefined. On machine where it is defined, this transformation is safe
5783 as long as M1 and M2 have the same number of words. */
5785 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
5786 && !OBJECT_P (SUBREG_REG (src))
5787 && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
5788 / UNITS_PER_WORD)
5789 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
5790 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
5791 #ifndef WORD_REGISTER_OPERATIONS
5792 && (GET_MODE_SIZE (GET_MODE (src))
5793 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
5794 #endif
5795 #ifdef CANNOT_CHANGE_MODE_CLASS
5796 && ! (REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER
5797 && REG_CANNOT_CHANGE_MODE_P (REGNO (dest),
5798 GET_MODE (SUBREG_REG (src)),
5799 GET_MODE (src)))
5800 #endif
5801 && (REG_P (dest)
5802 || (GET_CODE (dest) == SUBREG
5803 && REG_P (SUBREG_REG (dest)))))
5805 SUBST (SET_DEST (x),
5806 gen_lowpart (GET_MODE (SUBREG_REG (src)),
5807 dest));
5808 SUBST (SET_SRC (x), SUBREG_REG (src));
5810 src = SET_SRC (x), dest = SET_DEST (x);
5813 #ifdef HAVE_cc0
5814 /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
5815 in SRC. */
5816 if (dest == cc0_rtx
5817 && GET_CODE (src) == SUBREG
5818 && subreg_lowpart_p (src)
5819 && (GET_MODE_BITSIZE (GET_MODE (src))
5820 < GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (src)))))
5822 rtx inner = SUBREG_REG (src);
5823 enum machine_mode inner_mode = GET_MODE (inner);
5825 /* Here we make sure that we don't have a sign bit on. */
5826 if (GET_MODE_BITSIZE (inner_mode) <= HOST_BITS_PER_WIDE_INT
5827 && (nonzero_bits (inner, inner_mode)
5828 < ((unsigned HOST_WIDE_INT) 1
5829 << (GET_MODE_BITSIZE (GET_MODE (src)) - 1))))
5831 SUBST (SET_SRC (x), inner);
5832 src = SET_SRC (x);
5835 #endif
5837 #ifdef LOAD_EXTEND_OP
5838 /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
5839 would require a paradoxical subreg. Replace the subreg with a
5840 zero_extend to avoid the reload that would otherwise be required. */
5842 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
5843 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != UNKNOWN
5844 && SUBREG_BYTE (src) == 0
5845 && (GET_MODE_SIZE (GET_MODE (src))
5846 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
5847 && MEM_P (SUBREG_REG (src)))
5849 SUBST (SET_SRC (x),
5850 gen_rtx_fmt_e (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
5851 GET_MODE (src), SUBREG_REG (src)));
5853 src = SET_SRC (x);
5855 #endif
5857 /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
5858 are comparing an item known to be 0 or -1 against 0, use a logical
5859 operation instead. Check for one of the arms being an IOR of the other
5860 arm with some value. We compute three terms to be IOR'ed together. In
5861 practice, at most two will be nonzero. Then we do the IOR's. */
5863 if (GET_CODE (dest) != PC
5864 && GET_CODE (src) == IF_THEN_ELSE
5865 && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
5866 && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
5867 && XEXP (XEXP (src, 0), 1) == const0_rtx
5868 && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
5869 #ifdef HAVE_conditional_move
5870 && ! can_conditionally_move_p (GET_MODE (src))
5871 #endif
5872 && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
5873 GET_MODE (XEXP (XEXP (src, 0), 0)))
5874 == GET_MODE_BITSIZE (GET_MODE (XEXP (XEXP (src, 0), 0))))
5875 && ! side_effects_p (src))
5877 rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
5878 ? XEXP (src, 1) : XEXP (src, 2));
5879 rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
5880 ? XEXP (src, 2) : XEXP (src, 1));
5881 rtx term1 = const0_rtx, term2, term3;
5883 if (GET_CODE (true_rtx) == IOR
5884 && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
5885 term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
5886 else if (GET_CODE (true_rtx) == IOR
5887 && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
5888 term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
5889 else if (GET_CODE (false_rtx) == IOR
5890 && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
5891 term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
5892 else if (GET_CODE (false_rtx) == IOR
5893 && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
5894 term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
5896 term2 = simplify_gen_binary (AND, GET_MODE (src),
5897 XEXP (XEXP (src, 0), 0), true_rtx);
5898 term3 = simplify_gen_binary (AND, GET_MODE (src),
5899 simplify_gen_unary (NOT, GET_MODE (src),
5900 XEXP (XEXP (src, 0), 0),
5901 GET_MODE (src)),
5902 false_rtx);
5904 SUBST (SET_SRC (x),
5905 simplify_gen_binary (IOR, GET_MODE (src),
5906 simplify_gen_binary (IOR, GET_MODE (src),
5907 term1, term2),
5908 term3));
5910 src = SET_SRC (x);
5913 /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
5914 whole thing fail. */
5915 if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
5916 return src;
5917 else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
5918 return dest;
5919 else
5920 /* Convert this into a field assignment operation, if possible. */
5921 return make_field_assignment (x);
5924 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
5925 result. */
5927 static rtx
5928 simplify_logical (rtx x)
5930 enum machine_mode mode = GET_MODE (x);
5931 rtx op0 = XEXP (x, 0);
5932 rtx op1 = XEXP (x, 1);
5934 switch (GET_CODE (x))
5936 case AND:
5937 /* We can call simplify_and_const_int only if we don't lose
5938 any (sign) bits when converting INTVAL (op1) to
5939 "unsigned HOST_WIDE_INT". */
5940 if (GET_CODE (op1) == CONST_INT
5941 && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
5942 || INTVAL (op1) > 0))
5944 x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
5945 if (GET_CODE (x) != AND)
5946 return x;
5948 op0 = XEXP (x, 0);
5949 op1 = XEXP (x, 1);
5952 /* If we have any of (and (ior A B) C) or (and (xor A B) C),
5953 apply the distributive law and then the inverse distributive
5954 law to see if things simplify. */
5955 if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
5957 rtx result = distribute_and_simplify_rtx (x, 0);
5958 if (result)
5959 return result;
5961 if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
5963 rtx result = distribute_and_simplify_rtx (x, 1);
5964 if (result)
5965 return result;
5967 break;
5969 case IOR:
5970 /* If we have (ior (and A B) C), apply the distributive law and then
5971 the inverse distributive law to see if things simplify. */
5973 if (GET_CODE (op0) == AND)
5975 rtx result = distribute_and_simplify_rtx (x, 0);
5976 if (result)
5977 return result;
5980 if (GET_CODE (op1) == AND)
5982 rtx result = distribute_and_simplify_rtx (x, 1);
5983 if (result)
5984 return result;
5986 break;
5988 default:
5989 gcc_unreachable ();
5992 return x;
5995 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
5996 operations" because they can be replaced with two more basic operations.
5997 ZERO_EXTEND is also considered "compound" because it can be replaced with
5998 an AND operation, which is simpler, though only one operation.
6000 The function expand_compound_operation is called with an rtx expression
6001 and will convert it to the appropriate shifts and AND operations,
6002 simplifying at each stage.
6004 The function make_compound_operation is called to convert an expression
6005 consisting of shifts and ANDs into the equivalent compound expression.
6006 It is the inverse of this function, loosely speaking. */
6008 static rtx
6009 expand_compound_operation (rtx x)
6011 unsigned HOST_WIDE_INT pos = 0, len;
6012 int unsignedp = 0;
6013 unsigned int modewidth;
6014 rtx tem;
6016 switch (GET_CODE (x))
6018 case ZERO_EXTEND:
6019 unsignedp = 1;
6020 case SIGN_EXTEND:
6021 /* We can't necessarily use a const_int for a multiword mode;
6022 it depends on implicitly extending the value.
6023 Since we don't know the right way to extend it,
6024 we can't tell whether the implicit way is right.
6026 Even for a mode that is no wider than a const_int,
6027 we can't win, because we need to sign extend one of its bits through
6028 the rest of it, and we don't know which bit. */
6029 if (GET_CODE (XEXP (x, 0)) == CONST_INT)
6030 return x;
6032 /* Return if (subreg:MODE FROM 0) is not a safe replacement for
6033 (zero_extend:MODE FROM) or (sign_extend:MODE FROM). It is for any MEM
6034 because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
6035 reloaded. If not for that, MEM's would very rarely be safe.
6037 Reject MODEs bigger than a word, because we might not be able
6038 to reference a two-register group starting with an arbitrary register
6039 (and currently gen_lowpart might crash for a SUBREG). */
6041 if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
6042 return x;
6044 /* Reject MODEs that aren't scalar integers because turning vector
6045 or complex modes into shifts causes problems. */
6047 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6048 return x;
6050 len = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)));
6051 /* If the inner object has VOIDmode (the only way this can happen
6052 is if it is an ASM_OPERANDS), we can't do anything since we don't
6053 know how much masking to do. */
6054 if (len == 0)
6055 return x;
6057 break;
6059 case ZERO_EXTRACT:
6060 unsignedp = 1;
6062 /* ... fall through ... */
6064 case SIGN_EXTRACT:
6065 /* If the operand is a CLOBBER, just return it. */
6066 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
6067 return XEXP (x, 0);
6069 if (GET_CODE (XEXP (x, 1)) != CONST_INT
6070 || GET_CODE (XEXP (x, 2)) != CONST_INT
6071 || GET_MODE (XEXP (x, 0)) == VOIDmode)
6072 return x;
6074 /* Reject MODEs that aren't scalar integers because turning vector
6075 or complex modes into shifts causes problems. */
6077 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6078 return x;
6080 len = INTVAL (XEXP (x, 1));
6081 pos = INTVAL (XEXP (x, 2));
6083 /* This should stay within the object being extracted, fail otherwise. */
6084 if (len + pos > GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))))
6085 return x;
6087 if (BITS_BIG_ENDIAN)
6088 pos = GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0))) - len - pos;
6090 break;
6092 default:
6093 return x;
6095 /* Convert sign extension to zero extension, if we know that the high
6096 bit is not set, as this is easier to optimize. It will be converted
6097 back to cheaper alternative in make_extraction. */
6098 if (GET_CODE (x) == SIGN_EXTEND
6099 && (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6100 && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
6101 & ~(((unsigned HOST_WIDE_INT)
6102 GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
6103 >> 1))
6104 == 0)))
6106 rtx temp = gen_rtx_ZERO_EXTEND (GET_MODE (x), XEXP (x, 0));
6107 rtx temp2 = expand_compound_operation (temp);
6109 /* Make sure this is a profitable operation. */
6110 if (rtx_cost (x, SET) > rtx_cost (temp2, SET))
6111 return temp2;
6112 else if (rtx_cost (x, SET) > rtx_cost (temp, SET))
6113 return temp;
6114 else
6115 return x;
6118 /* We can optimize some special cases of ZERO_EXTEND. */
6119 if (GET_CODE (x) == ZERO_EXTEND)
6121 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
6122 know that the last value didn't have any inappropriate bits
6123 set. */
6124 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
6125 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
6126 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6127 && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
6128 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6129 return XEXP (XEXP (x, 0), 0);
6131 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
6132 if (GET_CODE (XEXP (x, 0)) == SUBREG
6133 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
6134 && subreg_lowpart_p (XEXP (x, 0))
6135 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
6136 && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
6137 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6138 return SUBREG_REG (XEXP (x, 0));
6140 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
6141 is a comparison and STORE_FLAG_VALUE permits. This is like
6142 the first case, but it works even when GET_MODE (x) is larger
6143 than HOST_WIDE_INT. */
6144 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
6145 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
6146 && COMPARISON_P (XEXP (XEXP (x, 0), 0))
6147 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
6148 <= HOST_BITS_PER_WIDE_INT)
6149 && ((HOST_WIDE_INT) STORE_FLAG_VALUE
6150 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6151 return XEXP (XEXP (x, 0), 0);
6153 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
6154 if (GET_CODE (XEXP (x, 0)) == SUBREG
6155 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
6156 && subreg_lowpart_p (XEXP (x, 0))
6157 && COMPARISON_P (SUBREG_REG (XEXP (x, 0)))
6158 && (GET_MODE_BITSIZE (GET_MODE (XEXP (x, 0)))
6159 <= HOST_BITS_PER_WIDE_INT)
6160 && ((HOST_WIDE_INT) STORE_FLAG_VALUE
6161 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
6162 return SUBREG_REG (XEXP (x, 0));
6166 /* If we reach here, we want to return a pair of shifts. The inner
6167 shift is a left shift of BITSIZE - POS - LEN bits. The outer
6168 shift is a right shift of BITSIZE - LEN bits. It is arithmetic or
6169 logical depending on the value of UNSIGNEDP.
6171 If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
6172 converted into an AND of a shift.
6174 We must check for the case where the left shift would have a negative
6175 count. This can happen in a case like (x >> 31) & 255 on machines
6176 that can't shift by a constant. On those machines, we would first
6177 combine the shift with the AND to produce a variable-position
6178 extraction. Then the constant of 31 would be substituted in to produce
6179 a such a position. */
6181 modewidth = GET_MODE_BITSIZE (GET_MODE (x));
6182 if (modewidth + len >= pos)
6184 enum machine_mode mode = GET_MODE (x);
6185 tem = gen_lowpart (mode, XEXP (x, 0));
6186 if (!tem || GET_CODE (tem) == CLOBBER)
6187 return x;
6188 tem = simplify_shift_const (NULL_RTX, ASHIFT, mode,
6189 tem, modewidth - pos - len);
6190 tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
6191 mode, tem, modewidth - len);
6193 else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
6194 tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
6195 simplify_shift_const (NULL_RTX, LSHIFTRT,
6196 GET_MODE (x),
6197 XEXP (x, 0), pos),
6198 ((HOST_WIDE_INT) 1 << len) - 1);
6199 else
6200 /* Any other cases we can't handle. */
6201 return x;
6203 /* If we couldn't do this for some reason, return the original
6204 expression. */
6205 if (GET_CODE (tem) == CLOBBER)
6206 return x;
6208 return tem;
6211 /* X is a SET which contains an assignment of one object into
6212 a part of another (such as a bit-field assignment, STRICT_LOW_PART,
6213 or certain SUBREGS). If possible, convert it into a series of
6214 logical operations.
6216 We half-heartedly support variable positions, but do not at all
6217 support variable lengths. */
6219 static const_rtx
6220 expand_field_assignment (const_rtx x)
6222 rtx inner;
6223 rtx pos; /* Always counts from low bit. */
6224 int len;
6225 rtx mask, cleared, masked;
6226 enum machine_mode compute_mode;
6228 /* Loop until we find something we can't simplify. */
6229 while (1)
6231 if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
6232 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
6234 inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
6235 len = GET_MODE_BITSIZE (GET_MODE (XEXP (SET_DEST (x), 0)));
6236 pos = GEN_INT (subreg_lsb (XEXP (SET_DEST (x), 0)));
6238 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
6239 && GET_CODE (XEXP (SET_DEST (x), 1)) == CONST_INT)
6241 inner = XEXP (SET_DEST (x), 0);
6242 len = INTVAL (XEXP (SET_DEST (x), 1));
6243 pos = XEXP (SET_DEST (x), 2);
6245 /* A constant position should stay within the width of INNER. */
6246 if (GET_CODE (pos) == CONST_INT
6247 && INTVAL (pos) + len > GET_MODE_BITSIZE (GET_MODE (inner)))
6248 break;
6250 if (BITS_BIG_ENDIAN)
6252 if (GET_CODE (pos) == CONST_INT)
6253 pos = GEN_INT (GET_MODE_BITSIZE (GET_MODE (inner)) - len
6254 - INTVAL (pos));
6255 else if (GET_CODE (pos) == MINUS
6256 && GET_CODE (XEXP (pos, 1)) == CONST_INT
6257 && (INTVAL (XEXP (pos, 1))
6258 == GET_MODE_BITSIZE (GET_MODE (inner)) - len))
6259 /* If position is ADJUST - X, new position is X. */
6260 pos = XEXP (pos, 0);
6261 else
6262 pos = simplify_gen_binary (MINUS, GET_MODE (pos),
6263 GEN_INT (GET_MODE_BITSIZE (
6264 GET_MODE (inner))
6265 - len),
6266 pos);
6270 /* A SUBREG between two modes that occupy the same numbers of words
6271 can be done by moving the SUBREG to the source. */
6272 else if (GET_CODE (SET_DEST (x)) == SUBREG
6273 /* We need SUBREGs to compute nonzero_bits properly. */
6274 && nonzero_sign_valid
6275 && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
6276 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
6277 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
6278 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
6280 x = gen_rtx_SET (VOIDmode, SUBREG_REG (SET_DEST (x)),
6281 gen_lowpart
6282 (GET_MODE (SUBREG_REG (SET_DEST (x))),
6283 SET_SRC (x)));
6284 continue;
6286 else
6287 break;
6289 while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
6290 inner = SUBREG_REG (inner);
6292 compute_mode = GET_MODE (inner);
6294 /* Don't attempt bitwise arithmetic on non scalar integer modes. */
6295 if (! SCALAR_INT_MODE_P (compute_mode))
6297 enum machine_mode imode;
6299 /* Don't do anything for vector or complex integral types. */
6300 if (! FLOAT_MODE_P (compute_mode))
6301 break;
6303 /* Try to find an integral mode to pun with. */
6304 imode = mode_for_size (GET_MODE_BITSIZE (compute_mode), MODE_INT, 0);
6305 if (imode == BLKmode)
6306 break;
6308 compute_mode = imode;
6309 inner = gen_lowpart (imode, inner);
6312 /* Compute a mask of LEN bits, if we can do this on the host machine. */
6313 if (len >= HOST_BITS_PER_WIDE_INT)
6314 break;
6316 /* Now compute the equivalent expression. Make a copy of INNER
6317 for the SET_DEST in case it is a MEM into which we will substitute;
6318 we don't want shared RTL in that case. */
6319 mask = GEN_INT (((HOST_WIDE_INT) 1 << len) - 1);
6320 cleared = simplify_gen_binary (AND, compute_mode,
6321 simplify_gen_unary (NOT, compute_mode,
6322 simplify_gen_binary (ASHIFT,
6323 compute_mode,
6324 mask, pos),
6325 compute_mode),
6326 inner);
6327 masked = simplify_gen_binary (ASHIFT, compute_mode,
6328 simplify_gen_binary (
6329 AND, compute_mode,
6330 gen_lowpart (compute_mode, SET_SRC (x)),
6331 mask),
6332 pos);
6334 x = gen_rtx_SET (VOIDmode, copy_rtx (inner),
6335 simplify_gen_binary (IOR, compute_mode,
6336 cleared, masked));
6339 return x;
6342 /* Return an RTX for a reference to LEN bits of INNER. If POS_RTX is nonzero,
6343 it is an RTX that represents a variable starting position; otherwise,
6344 POS is the (constant) starting bit position (counted from the LSB).
6346 UNSIGNEDP is nonzero for an unsigned reference and zero for a
6347 signed reference.
6349 IN_DEST is nonzero if this is a reference in the destination of a
6350 SET. This is used when a ZERO_ or SIGN_EXTRACT isn't needed. If nonzero,
6351 a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
6352 be used.
6354 IN_COMPARE is nonzero if we are in a COMPARE. This means that a
6355 ZERO_EXTRACT should be built even for bits starting at bit 0.
6357 MODE is the desired mode of the result (if IN_DEST == 0).
6359 The result is an RTX for the extraction or NULL_RTX if the target
6360 can't handle it. */
6362 static rtx
6363 make_extraction (enum machine_mode mode, rtx inner, HOST_WIDE_INT pos,
6364 rtx pos_rtx, unsigned HOST_WIDE_INT len, int unsignedp,
6365 int in_dest, int in_compare)
6367 /* This mode describes the size of the storage area
6368 to fetch the overall value from. Within that, we
6369 ignore the POS lowest bits, etc. */
6370 enum machine_mode is_mode = GET_MODE (inner);
6371 enum machine_mode inner_mode;
6372 enum machine_mode wanted_inner_mode;
6373 enum machine_mode wanted_inner_reg_mode = word_mode;
6374 enum machine_mode pos_mode = word_mode;
6375 enum machine_mode extraction_mode = word_mode;
6376 enum machine_mode tmode = mode_for_size (len, MODE_INT, 1);
6377 rtx new_rtx = 0;
6378 rtx orig_pos_rtx = pos_rtx;
6379 HOST_WIDE_INT orig_pos;
6381 if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
6383 /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
6384 consider just the QI as the memory to extract from.
6385 The subreg adds or removes high bits; its mode is
6386 irrelevant to the meaning of this extraction,
6387 since POS and LEN count from the lsb. */
6388 if (MEM_P (SUBREG_REG (inner)))
6389 is_mode = GET_MODE (SUBREG_REG (inner));
6390 inner = SUBREG_REG (inner);
6392 else if (GET_CODE (inner) == ASHIFT
6393 && GET_CODE (XEXP (inner, 1)) == CONST_INT
6394 && pos_rtx == 0 && pos == 0
6395 && len > (unsigned HOST_WIDE_INT) INTVAL (XEXP (inner, 1)))
6397 /* We're extracting the least significant bits of an rtx
6398 (ashift X (const_int C)), where LEN > C. Extract the
6399 least significant (LEN - C) bits of X, giving an rtx
6400 whose mode is MODE, then shift it left C times. */
6401 new_rtx = make_extraction (mode, XEXP (inner, 0),
6402 0, 0, len - INTVAL (XEXP (inner, 1)),
6403 unsignedp, in_dest, in_compare);
6404 if (new_rtx != 0)
6405 return gen_rtx_ASHIFT (mode, new_rtx, XEXP (inner, 1));
6408 inner_mode = GET_MODE (inner);
6410 if (pos_rtx && GET_CODE (pos_rtx) == CONST_INT)
6411 pos = INTVAL (pos_rtx), pos_rtx = 0;
6413 /* See if this can be done without an extraction. We never can if the
6414 width of the field is not the same as that of some integer mode. For
6415 registers, we can only avoid the extraction if the position is at the
6416 low-order bit and this is either not in the destination or we have the
6417 appropriate STRICT_LOW_PART operation available.
6419 For MEM, we can avoid an extract if the field starts on an appropriate
6420 boundary and we can change the mode of the memory reference. */
6422 if (tmode != BLKmode
6423 && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
6424 && !MEM_P (inner)
6425 && (inner_mode == tmode
6426 || !REG_P (inner)
6427 || TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (tmode),
6428 GET_MODE_BITSIZE (inner_mode))
6429 || reg_truncated_to_mode (tmode, inner))
6430 && (! in_dest
6431 || (REG_P (inner)
6432 && have_insn_for (STRICT_LOW_PART, tmode))))
6433 || (MEM_P (inner) && pos_rtx == 0
6434 && (pos
6435 % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
6436 : BITS_PER_UNIT)) == 0
6437 /* We can't do this if we are widening INNER_MODE (it
6438 may not be aligned, for one thing). */
6439 && GET_MODE_BITSIZE (inner_mode) >= GET_MODE_BITSIZE (tmode)
6440 && (inner_mode == tmode
6441 || (! mode_dependent_address_p (XEXP (inner, 0))
6442 && ! MEM_VOLATILE_P (inner))))))
6444 /* If INNER is a MEM, make a new MEM that encompasses just the desired
6445 field. If the original and current mode are the same, we need not
6446 adjust the offset. Otherwise, we do if bytes big endian.
6448 If INNER is not a MEM, get a piece consisting of just the field
6449 of interest (in this case POS % BITS_PER_WORD must be 0). */
6451 if (MEM_P (inner))
6453 HOST_WIDE_INT offset;
6455 /* POS counts from lsb, but make OFFSET count in memory order. */
6456 if (BYTES_BIG_ENDIAN)
6457 offset = (GET_MODE_BITSIZE (is_mode) - len - pos) / BITS_PER_UNIT;
6458 else
6459 offset = pos / BITS_PER_UNIT;
6461 new_rtx = adjust_address_nv (inner, tmode, offset);
6463 else if (REG_P (inner))
6465 if (tmode != inner_mode)
6467 /* We can't call gen_lowpart in a DEST since we
6468 always want a SUBREG (see below) and it would sometimes
6469 return a new hard register. */
6470 if (pos || in_dest)
6472 HOST_WIDE_INT final_word = pos / BITS_PER_WORD;
6474 if (WORDS_BIG_ENDIAN
6475 && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
6476 final_word = ((GET_MODE_SIZE (inner_mode)
6477 - GET_MODE_SIZE (tmode))
6478 / UNITS_PER_WORD) - final_word;
6480 final_word *= UNITS_PER_WORD;
6481 if (BYTES_BIG_ENDIAN &&
6482 GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (tmode))
6483 final_word += (GET_MODE_SIZE (inner_mode)
6484 - GET_MODE_SIZE (tmode)) % UNITS_PER_WORD;
6486 /* Avoid creating invalid subregs, for example when
6487 simplifying (x>>32)&255. */
6488 if (!validate_subreg (tmode, inner_mode, inner, final_word))
6489 return NULL_RTX;
6491 new_rtx = gen_rtx_SUBREG (tmode, inner, final_word);
6493 else
6494 new_rtx = gen_lowpart (tmode, inner);
6496 else
6497 new_rtx = inner;
6499 else
6500 new_rtx = force_to_mode (inner, tmode,
6501 len >= HOST_BITS_PER_WIDE_INT
6502 ? ~(unsigned HOST_WIDE_INT) 0
6503 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
6506 /* If this extraction is going into the destination of a SET,
6507 make a STRICT_LOW_PART unless we made a MEM. */
6509 if (in_dest)
6510 return (MEM_P (new_rtx) ? new_rtx
6511 : (GET_CODE (new_rtx) != SUBREG
6512 ? gen_rtx_CLOBBER (tmode, const0_rtx)
6513 : gen_rtx_STRICT_LOW_PART (VOIDmode, new_rtx)));
6515 if (mode == tmode)
6516 return new_rtx;
6518 if (GET_CODE (new_rtx) == CONST_INT)
6519 return gen_int_mode (INTVAL (new_rtx), mode);
6521 /* If we know that no extraneous bits are set, and that the high
6522 bit is not set, convert the extraction to the cheaper of
6523 sign and zero extension, that are equivalent in these cases. */
6524 if (flag_expensive_optimizations
6525 && (GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
6526 && ((nonzero_bits (new_rtx, tmode)
6527 & ~(((unsigned HOST_WIDE_INT)
6528 GET_MODE_MASK (tmode))
6529 >> 1))
6530 == 0)))
6532 rtx temp = gen_rtx_ZERO_EXTEND (mode, new_rtx);
6533 rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new_rtx);
6535 /* Prefer ZERO_EXTENSION, since it gives more information to
6536 backends. */
6537 if (rtx_cost (temp, SET) <= rtx_cost (temp1, SET))
6538 return temp;
6539 return temp1;
6542 /* Otherwise, sign- or zero-extend unless we already are in the
6543 proper mode. */
6545 return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
6546 mode, new_rtx));
6549 /* Unless this is a COMPARE or we have a funny memory reference,
6550 don't do anything with zero-extending field extracts starting at
6551 the low-order bit since they are simple AND operations. */
6552 if (pos_rtx == 0 && pos == 0 && ! in_dest
6553 && ! in_compare && unsignedp)
6554 return 0;
6556 /* Unless INNER is not MEM, reject this if we would be spanning bytes or
6557 if the position is not a constant and the length is not 1. In all
6558 other cases, we would only be going outside our object in cases when
6559 an original shift would have been undefined. */
6560 if (MEM_P (inner)
6561 && ((pos_rtx == 0 && pos + len > GET_MODE_BITSIZE (is_mode))
6562 || (pos_rtx != 0 && len != 1)))
6563 return 0;
6565 /* Get the mode to use should INNER not be a MEM, the mode for the position,
6566 and the mode for the result. */
6567 if (in_dest && mode_for_extraction (EP_insv, -1) != MAX_MACHINE_MODE)
6569 wanted_inner_reg_mode = mode_for_extraction (EP_insv, 0);
6570 pos_mode = mode_for_extraction (EP_insv, 2);
6571 extraction_mode = mode_for_extraction (EP_insv, 3);
6574 if (! in_dest && unsignedp
6575 && mode_for_extraction (EP_extzv, -1) != MAX_MACHINE_MODE)
6577 wanted_inner_reg_mode = mode_for_extraction (EP_extzv, 1);
6578 pos_mode = mode_for_extraction (EP_extzv, 3);
6579 extraction_mode = mode_for_extraction (EP_extzv, 0);
6582 if (! in_dest && ! unsignedp
6583 && mode_for_extraction (EP_extv, -1) != MAX_MACHINE_MODE)
6585 wanted_inner_reg_mode = mode_for_extraction (EP_extv, 1);
6586 pos_mode = mode_for_extraction (EP_extv, 3);
6587 extraction_mode = mode_for_extraction (EP_extv, 0);
6590 /* Never narrow an object, since that might not be safe. */
6592 if (mode != VOIDmode
6593 && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
6594 extraction_mode = mode;
6596 if (pos_rtx && GET_MODE (pos_rtx) != VOIDmode
6597 && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
6598 pos_mode = GET_MODE (pos_rtx);
6600 /* If this is not from memory, the desired mode is the preferred mode
6601 for an extraction pattern's first input operand, or word_mode if there
6602 is none. */
6603 if (!MEM_P (inner))
6604 wanted_inner_mode = wanted_inner_reg_mode;
6605 else
6607 /* Be careful not to go beyond the extracted object and maintain the
6608 natural alignment of the memory. */
6609 wanted_inner_mode = smallest_mode_for_size (len, MODE_INT);
6610 while (pos % GET_MODE_BITSIZE (wanted_inner_mode) + len
6611 > GET_MODE_BITSIZE (wanted_inner_mode))
6613 wanted_inner_mode = GET_MODE_WIDER_MODE (wanted_inner_mode);
6614 gcc_assert (wanted_inner_mode != VOIDmode);
6617 /* If we have to change the mode of memory and cannot, the desired mode
6618 is EXTRACTION_MODE. */
6619 if (inner_mode != wanted_inner_mode
6620 && (mode_dependent_address_p (XEXP (inner, 0))
6621 || MEM_VOLATILE_P (inner)
6622 || pos_rtx))
6623 wanted_inner_mode = extraction_mode;
6626 orig_pos = pos;
6628 if (BITS_BIG_ENDIAN)
6630 /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
6631 BITS_BIG_ENDIAN style. If position is constant, compute new
6632 position. Otherwise, build subtraction.
6633 Note that POS is relative to the mode of the original argument.
6634 If it's a MEM we need to recompute POS relative to that.
6635 However, if we're extracting from (or inserting into) a register,
6636 we want to recompute POS relative to wanted_inner_mode. */
6637 int width = (MEM_P (inner)
6638 ? GET_MODE_BITSIZE (is_mode)
6639 : GET_MODE_BITSIZE (wanted_inner_mode));
6641 if (pos_rtx == 0)
6642 pos = width - len - pos;
6643 else
6644 pos_rtx
6645 = gen_rtx_MINUS (GET_MODE (pos_rtx), GEN_INT (width - len), pos_rtx);
6646 /* POS may be less than 0 now, but we check for that below.
6647 Note that it can only be less than 0 if !MEM_P (inner). */
6650 /* If INNER has a wider mode, and this is a constant extraction, try to
6651 make it smaller and adjust the byte to point to the byte containing
6652 the value. */
6653 if (wanted_inner_mode != VOIDmode
6654 && inner_mode != wanted_inner_mode
6655 && ! pos_rtx
6656 && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
6657 && MEM_P (inner)
6658 && ! mode_dependent_address_p (XEXP (inner, 0))
6659 && ! MEM_VOLATILE_P (inner))
6661 int offset = 0;
6663 /* The computations below will be correct if the machine is big
6664 endian in both bits and bytes or little endian in bits and bytes.
6665 If it is mixed, we must adjust. */
6667 /* If bytes are big endian and we had a paradoxical SUBREG, we must
6668 adjust OFFSET to compensate. */
6669 if (BYTES_BIG_ENDIAN
6670 && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
6671 offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
6673 /* We can now move to the desired byte. */
6674 offset += (pos / GET_MODE_BITSIZE (wanted_inner_mode))
6675 * GET_MODE_SIZE (wanted_inner_mode);
6676 pos %= GET_MODE_BITSIZE (wanted_inner_mode);
6678 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
6679 && is_mode != wanted_inner_mode)
6680 offset = (GET_MODE_SIZE (is_mode)
6681 - GET_MODE_SIZE (wanted_inner_mode) - offset);
6683 inner = adjust_address_nv (inner, wanted_inner_mode, offset);
6686 /* If INNER is not memory, we can always get it into the proper mode. If we
6687 are changing its mode, POS must be a constant and smaller than the size
6688 of the new mode. */
6689 else if (!MEM_P (inner))
6691 if (GET_MODE (inner) != wanted_inner_mode
6692 && (pos_rtx != 0
6693 || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
6694 return 0;
6696 if (orig_pos < 0)
6697 return 0;
6699 inner = force_to_mode (inner, wanted_inner_mode,
6700 pos_rtx
6701 || len + orig_pos >= HOST_BITS_PER_WIDE_INT
6702 ? ~(unsigned HOST_WIDE_INT) 0
6703 : ((((unsigned HOST_WIDE_INT) 1 << len) - 1)
6704 << orig_pos),
6708 /* Adjust mode of POS_RTX, if needed. If we want a wider mode, we
6709 have to zero extend. Otherwise, we can just use a SUBREG. */
6710 if (pos_rtx != 0
6711 && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
6713 rtx temp = gen_rtx_ZERO_EXTEND (pos_mode, pos_rtx);
6715 /* If we know that no extraneous bits are set, and that the high
6716 bit is not set, convert extraction to cheaper one - either
6717 SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
6718 cases. */
6719 if (flag_expensive_optimizations
6720 && (GET_MODE_BITSIZE (GET_MODE (pos_rtx)) <= HOST_BITS_PER_WIDE_INT
6721 && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
6722 & ~(((unsigned HOST_WIDE_INT)
6723 GET_MODE_MASK (GET_MODE (pos_rtx)))
6724 >> 1))
6725 == 0)))
6727 rtx temp1 = gen_rtx_SIGN_EXTEND (pos_mode, pos_rtx);
6729 /* Prefer ZERO_EXTENSION, since it gives more information to
6730 backends. */
6731 if (rtx_cost (temp1, SET) < rtx_cost (temp, SET))
6732 temp = temp1;
6734 pos_rtx = temp;
6736 else if (pos_rtx != 0
6737 && GET_MODE_SIZE (pos_mode) < GET_MODE_SIZE (GET_MODE (pos_rtx)))
6738 pos_rtx = gen_lowpart (pos_mode, pos_rtx);
6740 /* Make POS_RTX unless we already have it and it is correct. If we don't
6741 have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
6742 be a CONST_INT. */
6743 if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
6744 pos_rtx = orig_pos_rtx;
6746 else if (pos_rtx == 0)
6747 pos_rtx = GEN_INT (pos);
6749 /* Make the required operation. See if we can use existing rtx. */
6750 new_rtx = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
6751 extraction_mode, inner, GEN_INT (len), pos_rtx);
6752 if (! in_dest)
6753 new_rtx = gen_lowpart (mode, new_rtx);
6755 return new_rtx;
6758 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
6759 with any other operations in X. Return X without that shift if so. */
6761 static rtx
6762 extract_left_shift (rtx x, int count)
6764 enum rtx_code code = GET_CODE (x);
6765 enum machine_mode mode = GET_MODE (x);
6766 rtx tem;
6768 switch (code)
6770 case ASHIFT:
6771 /* This is the shift itself. If it is wide enough, we will return
6772 either the value being shifted if the shift count is equal to
6773 COUNT or a shift for the difference. */
6774 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6775 && INTVAL (XEXP (x, 1)) >= count)
6776 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
6777 INTVAL (XEXP (x, 1)) - count);
6778 break;
6780 case NEG: case NOT:
6781 if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
6782 return simplify_gen_unary (code, mode, tem, mode);
6784 break;
6786 case PLUS: case IOR: case XOR: case AND:
6787 /* If we can safely shift this constant and we find the inner shift,
6788 make a new operation. */
6789 if (GET_CODE (XEXP (x, 1)) == CONST_INT
6790 && (INTVAL (XEXP (x, 1)) & ((((HOST_WIDE_INT) 1 << count)) - 1)) == 0
6791 && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
6792 return simplify_gen_binary (code, mode, tem,
6793 GEN_INT (INTVAL (XEXP (x, 1)) >> count));
6795 break;
6797 default:
6798 break;
6801 return 0;
6804 /* Look at the expression rooted at X. Look for expressions
6805 equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
6806 Form these expressions.
6808 Return the new rtx, usually just X.
6810 Also, for machines like the VAX that don't have logical shift insns,
6811 try to convert logical to arithmetic shift operations in cases where
6812 they are equivalent. This undoes the canonicalizations to logical
6813 shifts done elsewhere.
6815 We try, as much as possible, to re-use rtl expressions to save memory.
6817 IN_CODE says what kind of expression we are processing. Normally, it is
6818 SET. In a memory address (inside a MEM, PLUS or minus, the latter two
6819 being kludges), it is MEM. When processing the arguments of a comparison
6820 or a COMPARE against zero, it is COMPARE. */
6822 static rtx
6823 make_compound_operation (rtx x, enum rtx_code in_code)
6825 enum rtx_code code = GET_CODE (x);
6826 enum machine_mode mode = GET_MODE (x);
6827 int mode_width = GET_MODE_BITSIZE (mode);
6828 rtx rhs, lhs;
6829 enum rtx_code next_code;
6830 int i;
6831 rtx new_rtx = 0;
6832 rtx tem;
6833 const char *fmt;
6835 /* Select the code to be used in recursive calls. Once we are inside an
6836 address, we stay there. If we have a comparison, set to COMPARE,
6837 but once inside, go back to our default of SET. */
6839 next_code = (code == MEM || code == PLUS || code == MINUS ? MEM
6840 : ((code == COMPARE || COMPARISON_P (x))
6841 && XEXP (x, 1) == const0_rtx) ? COMPARE
6842 : in_code == COMPARE ? SET : in_code);
6844 /* Process depending on the code of this operation. If NEW is set
6845 nonzero, it will be returned. */
6847 switch (code)
6849 case ASHIFT:
6850 /* Convert shifts by constants into multiplications if inside
6851 an address. */
6852 if (in_code == MEM && GET_CODE (XEXP (x, 1)) == CONST_INT
6853 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
6854 && INTVAL (XEXP (x, 1)) >= 0)
6856 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
6857 new_rtx = gen_rtx_MULT (mode, new_rtx,
6858 GEN_INT ((HOST_WIDE_INT) 1
6859 << INTVAL (XEXP (x, 1))));
6861 break;
6863 case AND:
6864 /* If the second operand is not a constant, we can't do anything
6865 with it. */
6866 if (GET_CODE (XEXP (x, 1)) != CONST_INT)
6867 break;
6869 /* If the constant is a power of two minus one and the first operand
6870 is a logical right shift, make an extraction. */
6871 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6872 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6874 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
6875 new_rtx = make_extraction (mode, new_rtx, 0, XEXP (XEXP (x, 0), 1), i, 1,
6876 0, in_code == COMPARE);
6879 /* Same as previous, but for (subreg (lshiftrt ...)) in first op. */
6880 else if (GET_CODE (XEXP (x, 0)) == SUBREG
6881 && subreg_lowpart_p (XEXP (x, 0))
6882 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
6883 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6885 new_rtx = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
6886 next_code);
6887 new_rtx = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new_rtx, 0,
6888 XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
6889 0, in_code == COMPARE);
6891 /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)). */
6892 else if ((GET_CODE (XEXP (x, 0)) == XOR
6893 || GET_CODE (XEXP (x, 0)) == IOR)
6894 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
6895 && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
6896 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6898 /* Apply the distributive law, and then try to make extractions. */
6899 new_rtx = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
6900 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
6901 XEXP (x, 1)),
6902 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
6903 XEXP (x, 1)));
6904 new_rtx = make_compound_operation (new_rtx, in_code);
6907 /* If we are have (and (rotate X C) M) and C is larger than the number
6908 of bits in M, this is an extraction. */
6910 else if (GET_CODE (XEXP (x, 0)) == ROTATE
6911 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6912 && (i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0
6913 && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
6915 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
6916 new_rtx = make_extraction (mode, new_rtx,
6917 (GET_MODE_BITSIZE (mode)
6918 - INTVAL (XEXP (XEXP (x, 0), 1))),
6919 NULL_RTX, i, 1, 0, in_code == COMPARE);
6922 /* On machines without logical shifts, if the operand of the AND is
6923 a logical shift and our mask turns off all the propagated sign
6924 bits, we can replace the logical shift with an arithmetic shift. */
6925 else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
6926 && !have_insn_for (LSHIFTRT, mode)
6927 && have_insn_for (ASHIFTRT, mode)
6928 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
6929 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
6930 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
6931 && mode_width <= HOST_BITS_PER_WIDE_INT)
6933 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
6935 mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
6936 if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
6937 SUBST (XEXP (x, 0),
6938 gen_rtx_ASHIFTRT (mode,
6939 make_compound_operation
6940 (XEXP (XEXP (x, 0), 0), next_code),
6941 XEXP (XEXP (x, 0), 1)));
6944 /* If the constant is one less than a power of two, this might be
6945 representable by an extraction even if no shift is present.
6946 If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
6947 we are in a COMPARE. */
6948 else if ((i = exact_log2 (INTVAL (XEXP (x, 1)) + 1)) >= 0)
6949 new_rtx = make_extraction (mode,
6950 make_compound_operation (XEXP (x, 0),
6951 next_code),
6952 0, NULL_RTX, i, 1, 0, in_code == COMPARE);
6954 /* If we are in a comparison and this is an AND with a power of two,
6955 convert this into the appropriate bit extract. */
6956 else if (in_code == COMPARE
6957 && (i = exact_log2 (INTVAL (XEXP (x, 1)))) >= 0)
6958 new_rtx = make_extraction (mode,
6959 make_compound_operation (XEXP (x, 0),
6960 next_code),
6961 i, NULL_RTX, 1, 1, 0, 1);
6963 break;
6965 case LSHIFTRT:
6966 /* If the sign bit is known to be zero, replace this with an
6967 arithmetic shift. */
6968 if (have_insn_for (ASHIFTRT, mode)
6969 && ! have_insn_for (LSHIFTRT, mode)
6970 && mode_width <= HOST_BITS_PER_WIDE_INT
6971 && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
6973 new_rtx = gen_rtx_ASHIFTRT (mode,
6974 make_compound_operation (XEXP (x, 0),
6975 next_code),
6976 XEXP (x, 1));
6977 break;
6980 /* ... fall through ... */
6982 case ASHIFTRT:
6983 lhs = XEXP (x, 0);
6984 rhs = XEXP (x, 1);
6986 /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
6987 this is a SIGN_EXTRACT. */
6988 if (GET_CODE (rhs) == CONST_INT
6989 && GET_CODE (lhs) == ASHIFT
6990 && GET_CODE (XEXP (lhs, 1)) == CONST_INT
6991 && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1)))
6993 new_rtx = make_compound_operation (XEXP (lhs, 0), next_code);
6994 new_rtx = make_extraction (mode, new_rtx,
6995 INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
6996 NULL_RTX, mode_width - INTVAL (rhs),
6997 code == LSHIFTRT, 0, in_code == COMPARE);
6998 break;
7001 /* See if we have operations between an ASHIFTRT and an ASHIFT.
7002 If so, try to merge the shifts into a SIGN_EXTEND. We could
7003 also do this for some cases of SIGN_EXTRACT, but it doesn't
7004 seem worth the effort; the case checked for occurs on Alpha. */
7006 if (!OBJECT_P (lhs)
7007 && ! (GET_CODE (lhs) == SUBREG
7008 && (OBJECT_P (SUBREG_REG (lhs))))
7009 && GET_CODE (rhs) == CONST_INT
7010 && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
7011 && (new_rtx = extract_left_shift (lhs, INTVAL (rhs))) != 0)
7012 new_rtx = make_extraction (mode, make_compound_operation (new_rtx, next_code),
7013 0, NULL_RTX, mode_width - INTVAL (rhs),
7014 code == LSHIFTRT, 0, in_code == COMPARE);
7016 break;
7018 case SUBREG:
7019 /* Call ourselves recursively on the inner expression. If we are
7020 narrowing the object and it has a different RTL code from
7021 what it originally did, do this SUBREG as a force_to_mode. */
7023 tem = make_compound_operation (SUBREG_REG (x), in_code);
7026 rtx simplified;
7027 simplified = simplify_subreg (GET_MODE (x), tem, GET_MODE (tem),
7028 SUBREG_BYTE (x));
7030 if (simplified)
7031 tem = simplified;
7033 if (GET_CODE (tem) != GET_CODE (SUBREG_REG (x))
7034 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (tem))
7035 && subreg_lowpart_p (x))
7037 rtx newer = force_to_mode (tem, mode, ~(HOST_WIDE_INT) 0,
7040 /* If we have something other than a SUBREG, we might have
7041 done an expansion, so rerun ourselves. */
7042 if (GET_CODE (newer) != SUBREG)
7043 newer = make_compound_operation (newer, in_code);
7045 return newer;
7048 if (simplified)
7049 return tem;
7051 break;
7053 default:
7054 break;
7057 if (new_rtx)
7059 x = gen_lowpart (mode, new_rtx);
7060 code = GET_CODE (x);
7063 /* Now recursively process each operand of this operation. */
7064 fmt = GET_RTX_FORMAT (code);
7065 for (i = 0; i < GET_RTX_LENGTH (code); i++)
7066 if (fmt[i] == 'e')
7068 new_rtx = make_compound_operation (XEXP (x, i), next_code);
7069 SUBST (XEXP (x, i), new_rtx);
7072 /* If this is a commutative operation, the changes to the operands
7073 may have made it noncanonical. */
7074 if (COMMUTATIVE_ARITH_P (x)
7075 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
7077 tem = XEXP (x, 0);
7078 SUBST (XEXP (x, 0), XEXP (x, 1));
7079 SUBST (XEXP (x, 1), tem);
7082 return x;
7085 /* Given M see if it is a value that would select a field of bits
7086 within an item, but not the entire word. Return -1 if not.
7087 Otherwise, return the starting position of the field, where 0 is the
7088 low-order bit.
7090 *PLEN is set to the length of the field. */
7092 static int
7093 get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
7095 /* Get the bit number of the first 1 bit from the right, -1 if none. */
7096 int pos = exact_log2 (m & -m);
7097 int len = 0;
7099 if (pos >= 0)
7100 /* Now shift off the low-order zero bits and see if we have a
7101 power of two minus 1. */
7102 len = exact_log2 ((m >> pos) + 1);
7104 if (len <= 0)
7105 pos = -1;
7107 *plen = len;
7108 return pos;
7111 /* If X refers to a register that equals REG in value, replace these
7112 references with REG. */
7113 static rtx
7114 canon_reg_for_combine (rtx x, rtx reg)
7116 rtx op0, op1, op2;
7117 const char *fmt;
7118 int i;
7119 bool copied;
7121 enum rtx_code code = GET_CODE (x);
7122 switch (GET_RTX_CLASS (code))
7124 case RTX_UNARY:
7125 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7126 if (op0 != XEXP (x, 0))
7127 return simplify_gen_unary (GET_CODE (x), GET_MODE (x), op0,
7128 GET_MODE (reg));
7129 break;
7131 case RTX_BIN_ARITH:
7132 case RTX_COMM_ARITH:
7133 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7134 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7135 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7136 return simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
7137 break;
7139 case RTX_COMPARE:
7140 case RTX_COMM_COMPARE:
7141 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7142 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7143 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7144 return simplify_gen_relational (GET_CODE (x), GET_MODE (x),
7145 GET_MODE (op0), op0, op1);
7146 break;
7148 case RTX_TERNARY:
7149 case RTX_BITFIELD_OPS:
7150 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
7151 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
7152 op2 = canon_reg_for_combine (XEXP (x, 2), reg);
7153 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1) || op2 != XEXP (x, 2))
7154 return simplify_gen_ternary (GET_CODE (x), GET_MODE (x),
7155 GET_MODE (op0), op0, op1, op2);
7157 case RTX_OBJ:
7158 if (REG_P (x))
7160 if (rtx_equal_p (get_last_value (reg), x)
7161 || rtx_equal_p (reg, get_last_value (x)))
7162 return reg;
7163 else
7164 break;
7167 /* fall through */
7169 default:
7170 fmt = GET_RTX_FORMAT (code);
7171 copied = false;
7172 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
7173 if (fmt[i] == 'e')
7175 rtx op = canon_reg_for_combine (XEXP (x, i), reg);
7176 if (op != XEXP (x, i))
7178 if (!copied)
7180 copied = true;
7181 x = copy_rtx (x);
7183 XEXP (x, i) = op;
7186 else if (fmt[i] == 'E')
7188 int j;
7189 for (j = 0; j < XVECLEN (x, i); j++)
7191 rtx op = canon_reg_for_combine (XVECEXP (x, i, j), reg);
7192 if (op != XVECEXP (x, i, j))
7194 if (!copied)
7196 copied = true;
7197 x = copy_rtx (x);
7199 XVECEXP (x, i, j) = op;
7204 break;
7207 return x;
7210 /* Return X converted to MODE. If the value is already truncated to
7211 MODE we can just return a subreg even though in the general case we
7212 would need an explicit truncation. */
7214 static rtx
7215 gen_lowpart_or_truncate (enum machine_mode mode, rtx x)
7217 if (GET_MODE_SIZE (GET_MODE (x)) <= GET_MODE_SIZE (mode)
7218 || TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
7219 GET_MODE_BITSIZE (GET_MODE (x)))
7220 || (REG_P (x) && reg_truncated_to_mode (mode, x)))
7221 return gen_lowpart (mode, x);
7222 else
7223 return simplify_gen_unary (TRUNCATE, mode, x, GET_MODE (x));
7226 /* See if X can be simplified knowing that we will only refer to it in
7227 MODE and will only refer to those bits that are nonzero in MASK.
7228 If other bits are being computed or if masking operations are done
7229 that select a superset of the bits in MASK, they can sometimes be
7230 ignored.
7232 Return a possibly simplified expression, but always convert X to
7233 MODE. If X is a CONST_INT, AND the CONST_INT with MASK.
7235 If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
7236 are all off in X. This is used when X will be complemented, by either
7237 NOT, NEG, or XOR. */
7239 static rtx
7240 force_to_mode (rtx x, enum machine_mode mode, unsigned HOST_WIDE_INT mask,
7241 int just_select)
7243 enum rtx_code code = GET_CODE (x);
7244 int next_select = just_select || code == XOR || code == NOT || code == NEG;
7245 enum machine_mode op_mode;
7246 unsigned HOST_WIDE_INT fuller_mask, nonzero;
7247 rtx op0, op1, temp;
7249 /* If this is a CALL or ASM_OPERANDS, don't do anything. Some of the
7250 code below will do the wrong thing since the mode of such an
7251 expression is VOIDmode.
7253 Also do nothing if X is a CLOBBER; this can happen if X was
7254 the return value from a call to gen_lowpart. */
7255 if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
7256 return x;
7258 /* We want to perform the operation is its present mode unless we know
7259 that the operation is valid in MODE, in which case we do the operation
7260 in MODE. */
7261 op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
7262 && have_insn_for (code, mode))
7263 ? mode : GET_MODE (x));
7265 /* It is not valid to do a right-shift in a narrower mode
7266 than the one it came in with. */
7267 if ((code == LSHIFTRT || code == ASHIFTRT)
7268 && GET_MODE_BITSIZE (mode) < GET_MODE_BITSIZE (GET_MODE (x)))
7269 op_mode = GET_MODE (x);
7271 /* Truncate MASK to fit OP_MODE. */
7272 if (op_mode)
7273 mask &= GET_MODE_MASK (op_mode);
7275 /* When we have an arithmetic operation, or a shift whose count we
7276 do not know, we need to assume that all bits up to the highest-order
7277 bit in MASK will be needed. This is how we form such a mask. */
7278 if (mask & ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
7279 fuller_mask = ~(unsigned HOST_WIDE_INT) 0;
7280 else
7281 fuller_mask = (((unsigned HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1))
7282 - 1);
7284 /* Determine what bits of X are guaranteed to be (non)zero. */
7285 nonzero = nonzero_bits (x, mode);
7287 /* If none of the bits in X are needed, return a zero. */
7288 if (!just_select && (nonzero & mask) == 0 && !side_effects_p (x))
7289 x = const0_rtx;
7291 /* If X is a CONST_INT, return a new one. Do this here since the
7292 test below will fail. */
7293 if (GET_CODE (x) == CONST_INT)
7295 if (SCALAR_INT_MODE_P (mode))
7296 return gen_int_mode (INTVAL (x) & mask, mode);
7297 else
7299 x = GEN_INT (INTVAL (x) & mask);
7300 return gen_lowpart_common (mode, x);
7304 /* If X is narrower than MODE and we want all the bits in X's mode, just
7305 get X in the proper mode. */
7306 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
7307 && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
7308 return gen_lowpart (mode, x);
7310 switch (code)
7312 case CLOBBER:
7313 /* If X is a (clobber (const_int)), return it since we know we are
7314 generating something that won't match. */
7315 return x;
7317 case SIGN_EXTEND:
7318 case ZERO_EXTEND:
7319 case ZERO_EXTRACT:
7320 case SIGN_EXTRACT:
7321 x = expand_compound_operation (x);
7322 if (GET_CODE (x) != code)
7323 return force_to_mode (x, mode, mask, next_select);
7324 break;
7326 case SUBREG:
7327 if (subreg_lowpart_p (x)
7328 /* We can ignore the effect of this SUBREG if it narrows the mode or
7329 if the constant masks to zero all the bits the mode doesn't
7330 have. */
7331 && ((GET_MODE_SIZE (GET_MODE (x))
7332 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
7333 || (0 == (mask
7334 & GET_MODE_MASK (GET_MODE (x))
7335 & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
7336 return force_to_mode (SUBREG_REG (x), mode, mask, next_select);
7337 break;
7339 case AND:
7340 /* If this is an AND with a constant, convert it into an AND
7341 whose constant is the AND of that constant with MASK. If it
7342 remains an AND of MASK, delete it since it is redundant. */
7344 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
7346 x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
7347 mask & INTVAL (XEXP (x, 1)));
7349 /* If X is still an AND, see if it is an AND with a mask that
7350 is just some low-order bits. If so, and it is MASK, we don't
7351 need it. */
7353 if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
7354 && ((INTVAL (XEXP (x, 1)) & GET_MODE_MASK (GET_MODE (x)))
7355 == mask))
7356 x = XEXP (x, 0);
7358 /* If it remains an AND, try making another AND with the bits
7359 in the mode mask that aren't in MASK turned on. If the
7360 constant in the AND is wide enough, this might make a
7361 cheaper constant. */
7363 if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT
7364 && GET_MODE_MASK (GET_MODE (x)) != mask
7365 && GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT)
7367 HOST_WIDE_INT cval = (INTVAL (XEXP (x, 1))
7368 | (GET_MODE_MASK (GET_MODE (x)) & ~mask));
7369 int width = GET_MODE_BITSIZE (GET_MODE (x));
7370 rtx y;
7372 /* If MODE is narrower than HOST_WIDE_INT and CVAL is a negative
7373 number, sign extend it. */
7374 if (width > 0 && width < HOST_BITS_PER_WIDE_INT
7375 && (cval & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
7376 cval |= (HOST_WIDE_INT) -1 << width;
7378 y = simplify_gen_binary (AND, GET_MODE (x),
7379 XEXP (x, 0), GEN_INT (cval));
7380 if (rtx_cost (y, SET) < rtx_cost (x, SET))
7381 x = y;
7384 break;
7387 goto binop;
7389 case PLUS:
7390 /* In (and (plus FOO C1) M), if M is a mask that just turns off
7391 low-order bits (as in an alignment operation) and FOO is already
7392 aligned to that boundary, mask C1 to that boundary as well.
7393 This may eliminate that PLUS and, later, the AND. */
7396 unsigned int width = GET_MODE_BITSIZE (mode);
7397 unsigned HOST_WIDE_INT smask = mask;
7399 /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
7400 number, sign extend it. */
7402 if (width < HOST_BITS_PER_WIDE_INT
7403 && (smask & ((HOST_WIDE_INT) 1 << (width - 1))) != 0)
7404 smask |= (HOST_WIDE_INT) -1 << width;
7406 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7407 && exact_log2 (- smask) >= 0
7408 && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
7409 && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
7410 return force_to_mode (plus_constant (XEXP (x, 0),
7411 (INTVAL (XEXP (x, 1)) & smask)),
7412 mode, smask, next_select);
7415 /* ... fall through ... */
7417 case MULT:
7418 /* For PLUS, MINUS and MULT, we need any bits less significant than the
7419 most significant bit in MASK since carries from those bits will
7420 affect the bits we are interested in. */
7421 mask = fuller_mask;
7422 goto binop;
7424 case MINUS:
7425 /* If X is (minus C Y) where C's least set bit is larger than any bit
7426 in the mask, then we may replace with (neg Y). */
7427 if (GET_CODE (XEXP (x, 0)) == CONST_INT
7428 && (((unsigned HOST_WIDE_INT) (INTVAL (XEXP (x, 0))
7429 & -INTVAL (XEXP (x, 0))))
7430 > mask))
7432 x = simplify_gen_unary (NEG, GET_MODE (x), XEXP (x, 1),
7433 GET_MODE (x));
7434 return force_to_mode (x, mode, mask, next_select);
7437 /* Similarly, if C contains every bit in the fuller_mask, then we may
7438 replace with (not Y). */
7439 if (GET_CODE (XEXP (x, 0)) == CONST_INT
7440 && ((INTVAL (XEXP (x, 0)) | (HOST_WIDE_INT) fuller_mask)
7441 == INTVAL (XEXP (x, 0))))
7443 x = simplify_gen_unary (NOT, GET_MODE (x),
7444 XEXP (x, 1), GET_MODE (x));
7445 return force_to_mode (x, mode, mask, next_select);
7448 mask = fuller_mask;
7449 goto binop;
7451 case IOR:
7452 case XOR:
7453 /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
7454 LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
7455 operation which may be a bitfield extraction. Ensure that the
7456 constant we form is not wider than the mode of X. */
7458 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7459 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
7460 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7461 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
7462 && GET_CODE (XEXP (x, 1)) == CONST_INT
7463 && ((INTVAL (XEXP (XEXP (x, 0), 1))
7464 + floor_log2 (INTVAL (XEXP (x, 1))))
7465 < GET_MODE_BITSIZE (GET_MODE (x)))
7466 && (INTVAL (XEXP (x, 1))
7467 & ~nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
7469 temp = GEN_INT ((INTVAL (XEXP (x, 1)) & mask)
7470 << INTVAL (XEXP (XEXP (x, 0), 1)));
7471 temp = simplify_gen_binary (GET_CODE (x), GET_MODE (x),
7472 XEXP (XEXP (x, 0), 0), temp);
7473 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), temp,
7474 XEXP (XEXP (x, 0), 1));
7475 return force_to_mode (x, mode, mask, next_select);
7478 binop:
7479 /* For most binary operations, just propagate into the operation and
7480 change the mode if we have an operation of that mode. */
7482 op0 = gen_lowpart_or_truncate (op_mode,
7483 force_to_mode (XEXP (x, 0), mode, mask,
7484 next_select));
7485 op1 = gen_lowpart_or_truncate (op_mode,
7486 force_to_mode (XEXP (x, 1), mode, mask,
7487 next_select));
7489 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
7490 x = simplify_gen_binary (code, op_mode, op0, op1);
7491 break;
7493 case ASHIFT:
7494 /* For left shifts, do the same, but just for the first operand.
7495 However, we cannot do anything with shifts where we cannot
7496 guarantee that the counts are smaller than the size of the mode
7497 because such a count will have a different meaning in a
7498 wider mode. */
7500 if (! (GET_CODE (XEXP (x, 1)) == CONST_INT
7501 && INTVAL (XEXP (x, 1)) >= 0
7502 && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (mode))
7503 && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
7504 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
7505 < (unsigned HOST_WIDE_INT) GET_MODE_BITSIZE (mode))))
7506 break;
7508 /* If the shift count is a constant and we can do arithmetic in
7509 the mode of the shift, refine which bits we need. Otherwise, use the
7510 conservative form of the mask. */
7511 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7512 && INTVAL (XEXP (x, 1)) >= 0
7513 && INTVAL (XEXP (x, 1)) < GET_MODE_BITSIZE (op_mode)
7514 && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
7515 mask >>= INTVAL (XEXP (x, 1));
7516 else
7517 mask = fuller_mask;
7519 op0 = gen_lowpart_or_truncate (op_mode,
7520 force_to_mode (XEXP (x, 0), op_mode,
7521 mask, next_select));
7523 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
7524 x = simplify_gen_binary (code, op_mode, op0, XEXP (x, 1));
7525 break;
7527 case LSHIFTRT:
7528 /* Here we can only do something if the shift count is a constant,
7529 this shift constant is valid for the host, and we can do arithmetic
7530 in OP_MODE. */
7532 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7533 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
7534 && GET_MODE_BITSIZE (op_mode) <= HOST_BITS_PER_WIDE_INT)
7536 rtx inner = XEXP (x, 0);
7537 unsigned HOST_WIDE_INT inner_mask;
7539 /* Select the mask of the bits we need for the shift operand. */
7540 inner_mask = mask << INTVAL (XEXP (x, 1));
7542 /* We can only change the mode of the shift if we can do arithmetic
7543 in the mode of the shift and INNER_MASK is no wider than the
7544 width of X's mode. */
7545 if ((inner_mask & ~GET_MODE_MASK (GET_MODE (x))) != 0)
7546 op_mode = GET_MODE (x);
7548 inner = force_to_mode (inner, op_mode, inner_mask, next_select);
7550 if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
7551 x = simplify_gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
7554 /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
7555 shift and AND produces only copies of the sign bit (C2 is one less
7556 than a power of two), we can do this with just a shift. */
7558 if (GET_CODE (x) == LSHIFTRT
7559 && GET_CODE (XEXP (x, 1)) == CONST_INT
7560 /* The shift puts one of the sign bit copies in the least significant
7561 bit. */
7562 && ((INTVAL (XEXP (x, 1))
7563 + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
7564 >= GET_MODE_BITSIZE (GET_MODE (x)))
7565 && exact_log2 (mask + 1) >= 0
7566 /* Number of bits left after the shift must be more than the mask
7567 needs. */
7568 && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
7569 <= GET_MODE_BITSIZE (GET_MODE (x)))
7570 /* Must be more sign bit copies than the mask needs. */
7571 && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
7572 >= exact_log2 (mask + 1)))
7573 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
7574 GEN_INT (GET_MODE_BITSIZE (GET_MODE (x))
7575 - exact_log2 (mask + 1)));
7577 goto shiftrt;
7579 case ASHIFTRT:
7580 /* If we are just looking for the sign bit, we don't need this shift at
7581 all, even if it has a variable count. */
7582 if (GET_MODE_BITSIZE (GET_MODE (x)) <= HOST_BITS_PER_WIDE_INT
7583 && (mask == ((unsigned HOST_WIDE_INT) 1
7584 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
7585 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
7587 /* If this is a shift by a constant, get a mask that contains those bits
7588 that are not copies of the sign bit. We then have two cases: If
7589 MASK only includes those bits, this can be a logical shift, which may
7590 allow simplifications. If MASK is a single-bit field not within
7591 those bits, we are requesting a copy of the sign bit and hence can
7592 shift the sign bit to the appropriate location. */
7594 if (GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) >= 0
7595 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
7597 int i;
7599 /* If the considered data is wider than HOST_WIDE_INT, we can't
7600 represent a mask for all its bits in a single scalar.
7601 But we only care about the lower bits, so calculate these. */
7603 if (GET_MODE_BITSIZE (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
7605 nonzero = ~(HOST_WIDE_INT) 0;
7607 /* GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
7608 is the number of bits a full-width mask would have set.
7609 We need only shift if these are fewer than nonzero can
7610 hold. If not, we must keep all bits set in nonzero. */
7612 if (GET_MODE_BITSIZE (GET_MODE (x)) - INTVAL (XEXP (x, 1))
7613 < HOST_BITS_PER_WIDE_INT)
7614 nonzero >>= INTVAL (XEXP (x, 1))
7615 + HOST_BITS_PER_WIDE_INT
7616 - GET_MODE_BITSIZE (GET_MODE (x)) ;
7618 else
7620 nonzero = GET_MODE_MASK (GET_MODE (x));
7621 nonzero >>= INTVAL (XEXP (x, 1));
7624 if ((mask & ~nonzero) == 0)
7626 x = simplify_shift_const (NULL_RTX, LSHIFTRT, GET_MODE (x),
7627 XEXP (x, 0), INTVAL (XEXP (x, 1)));
7628 if (GET_CODE (x) != ASHIFTRT)
7629 return force_to_mode (x, mode, mask, next_select);
7632 else if ((i = exact_log2 (mask)) >= 0)
7634 x = simplify_shift_const
7635 (NULL_RTX, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
7636 GET_MODE_BITSIZE (GET_MODE (x)) - 1 - i);
7638 if (GET_CODE (x) != ASHIFTRT)
7639 return force_to_mode (x, mode, mask, next_select);
7643 /* If MASK is 1, convert this to an LSHIFTRT. This can be done
7644 even if the shift count isn't a constant. */
7645 if (mask == 1)
7646 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
7647 XEXP (x, 0), XEXP (x, 1));
7649 shiftrt:
7651 /* If this is a zero- or sign-extension operation that just affects bits
7652 we don't care about, remove it. Be sure the call above returned
7653 something that is still a shift. */
7655 if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
7656 && GET_CODE (XEXP (x, 1)) == CONST_INT
7657 && INTVAL (XEXP (x, 1)) >= 0
7658 && (INTVAL (XEXP (x, 1))
7659 <= GET_MODE_BITSIZE (GET_MODE (x)) - (floor_log2 (mask) + 1))
7660 && GET_CODE (XEXP (x, 0)) == ASHIFT
7661 && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
7662 return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
7663 next_select);
7665 break;
7667 case ROTATE:
7668 case ROTATERT:
7669 /* If the shift count is constant and we can do computations
7670 in the mode of X, compute where the bits we care about are.
7671 Otherwise, we can't do anything. Don't change the mode of
7672 the shift or propagate MODE into the shift, though. */
7673 if (GET_CODE (XEXP (x, 1)) == CONST_INT
7674 && INTVAL (XEXP (x, 1)) >= 0)
7676 temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
7677 GET_MODE (x), GEN_INT (mask),
7678 XEXP (x, 1));
7679 if (temp && GET_CODE (temp) == CONST_INT)
7680 SUBST (XEXP (x, 0),
7681 force_to_mode (XEXP (x, 0), GET_MODE (x),
7682 INTVAL (temp), next_select));
7684 break;
7686 case NEG:
7687 /* If we just want the low-order bit, the NEG isn't needed since it
7688 won't change the low-order bit. */
7689 if (mask == 1)
7690 return force_to_mode (XEXP (x, 0), mode, mask, just_select);
7692 /* We need any bits less significant than the most significant bit in
7693 MASK since carries from those bits will affect the bits we are
7694 interested in. */
7695 mask = fuller_mask;
7696 goto unop;
7698 case NOT:
7699 /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
7700 same as the XOR case above. Ensure that the constant we form is not
7701 wider than the mode of X. */
7703 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7704 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT
7705 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7706 && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
7707 < GET_MODE_BITSIZE (GET_MODE (x)))
7708 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
7710 temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)),
7711 GET_MODE (x));
7712 temp = simplify_gen_binary (XOR, GET_MODE (x),
7713 XEXP (XEXP (x, 0), 0), temp);
7714 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
7715 temp, XEXP (XEXP (x, 0), 1));
7717 return force_to_mode (x, mode, mask, next_select);
7720 /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
7721 use the full mask inside the NOT. */
7722 mask = fuller_mask;
7724 unop:
7725 op0 = gen_lowpart_or_truncate (op_mode,
7726 force_to_mode (XEXP (x, 0), mode, mask,
7727 next_select));
7728 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
7729 x = simplify_gen_unary (code, op_mode, op0, op_mode);
7730 break;
7732 case NE:
7733 /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
7734 in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
7735 which is equal to STORE_FLAG_VALUE. */
7736 if ((mask & ~STORE_FLAG_VALUE) == 0 && XEXP (x, 1) == const0_rtx
7737 && GET_MODE (XEXP (x, 0)) == mode
7738 && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
7739 && (nonzero_bits (XEXP (x, 0), mode)
7740 == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
7741 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
7743 break;
7745 case IF_THEN_ELSE:
7746 /* We have no way of knowing if the IF_THEN_ELSE can itself be
7747 written in a narrower mode. We play it safe and do not do so. */
7749 SUBST (XEXP (x, 1),
7750 gen_lowpart_or_truncate (GET_MODE (x),
7751 force_to_mode (XEXP (x, 1), mode,
7752 mask, next_select)));
7753 SUBST (XEXP (x, 2),
7754 gen_lowpart_or_truncate (GET_MODE (x),
7755 force_to_mode (XEXP (x, 2), mode,
7756 mask, next_select)));
7757 break;
7759 default:
7760 break;
7763 /* Ensure we return a value of the proper mode. */
7764 return gen_lowpart_or_truncate (mode, x);
7767 /* Return nonzero if X is an expression that has one of two values depending on
7768 whether some other value is zero or nonzero. In that case, we return the
7769 value that is being tested, *PTRUE is set to the value if the rtx being
7770 returned has a nonzero value, and *PFALSE is set to the other alternative.
7772 If we return zero, we set *PTRUE and *PFALSE to X. */
7774 static rtx
7775 if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
7777 enum machine_mode mode = GET_MODE (x);
7778 enum rtx_code code = GET_CODE (x);
7779 rtx cond0, cond1, true0, true1, false0, false1;
7780 unsigned HOST_WIDE_INT nz;
7782 /* If we are comparing a value against zero, we are done. */
7783 if ((code == NE || code == EQ)
7784 && XEXP (x, 1) == const0_rtx)
7786 *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
7787 *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
7788 return XEXP (x, 0);
7791 /* If this is a unary operation whose operand has one of two values, apply
7792 our opcode to compute those values. */
7793 else if (UNARY_P (x)
7794 && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
7796 *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
7797 *pfalse = simplify_gen_unary (code, mode, false0,
7798 GET_MODE (XEXP (x, 0)));
7799 return cond0;
7802 /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
7803 make can't possibly match and would suppress other optimizations. */
7804 else if (code == COMPARE)
7807 /* If this is a binary operation, see if either side has only one of two
7808 values. If either one does or if both do and they are conditional on
7809 the same value, compute the new true and false values. */
7810 else if (BINARY_P (x))
7812 cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
7813 cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
7815 if ((cond0 != 0 || cond1 != 0)
7816 && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
7818 /* If if_then_else_cond returned zero, then true/false are the
7819 same rtl. We must copy one of them to prevent invalid rtl
7820 sharing. */
7821 if (cond0 == 0)
7822 true0 = copy_rtx (true0);
7823 else if (cond1 == 0)
7824 true1 = copy_rtx (true1);
7826 if (COMPARISON_P (x))
7828 *ptrue = simplify_gen_relational (code, mode, VOIDmode,
7829 true0, true1);
7830 *pfalse = simplify_gen_relational (code, mode, VOIDmode,
7831 false0, false1);
7833 else
7835 *ptrue = simplify_gen_binary (code, mode, true0, true1);
7836 *pfalse = simplify_gen_binary (code, mode, false0, false1);
7839 return cond0 ? cond0 : cond1;
7842 /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
7843 operands is zero when the other is nonzero, and vice-versa,
7844 and STORE_FLAG_VALUE is 1 or -1. */
7846 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
7847 && (code == PLUS || code == IOR || code == XOR || code == MINUS
7848 || code == UMAX)
7849 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
7851 rtx op0 = XEXP (XEXP (x, 0), 1);
7852 rtx op1 = XEXP (XEXP (x, 1), 1);
7854 cond0 = XEXP (XEXP (x, 0), 0);
7855 cond1 = XEXP (XEXP (x, 1), 0);
7857 if (COMPARISON_P (cond0)
7858 && COMPARISON_P (cond1)
7859 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
7860 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
7861 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
7862 || ((swap_condition (GET_CODE (cond0))
7863 == reversed_comparison_code (cond1, NULL))
7864 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
7865 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
7866 && ! side_effects_p (x))
7868 *ptrue = simplify_gen_binary (MULT, mode, op0, const_true_rtx);
7869 *pfalse = simplify_gen_binary (MULT, mode,
7870 (code == MINUS
7871 ? simplify_gen_unary (NEG, mode,
7872 op1, mode)
7873 : op1),
7874 const_true_rtx);
7875 return cond0;
7879 /* Similarly for MULT, AND and UMIN, except that for these the result
7880 is always zero. */
7881 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
7882 && (code == MULT || code == AND || code == UMIN)
7883 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
7885 cond0 = XEXP (XEXP (x, 0), 0);
7886 cond1 = XEXP (XEXP (x, 1), 0);
7888 if (COMPARISON_P (cond0)
7889 && COMPARISON_P (cond1)
7890 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
7891 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
7892 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
7893 || ((swap_condition (GET_CODE (cond0))
7894 == reversed_comparison_code (cond1, NULL))
7895 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
7896 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
7897 && ! side_effects_p (x))
7899 *ptrue = *pfalse = const0_rtx;
7900 return cond0;
7905 else if (code == IF_THEN_ELSE)
7907 /* If we have IF_THEN_ELSE already, extract the condition and
7908 canonicalize it if it is NE or EQ. */
7909 cond0 = XEXP (x, 0);
7910 *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
7911 if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
7912 return XEXP (cond0, 0);
7913 else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
7915 *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
7916 return XEXP (cond0, 0);
7918 else
7919 return cond0;
7922 /* If X is a SUBREG, we can narrow both the true and false values
7923 if the inner expression, if there is a condition. */
7924 else if (code == SUBREG
7925 && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
7926 &true0, &false0)))
7928 true0 = simplify_gen_subreg (mode, true0,
7929 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
7930 false0 = simplify_gen_subreg (mode, false0,
7931 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
7932 if (true0 && false0)
7934 *ptrue = true0;
7935 *pfalse = false0;
7936 return cond0;
7940 /* If X is a constant, this isn't special and will cause confusions
7941 if we treat it as such. Likewise if it is equivalent to a constant. */
7942 else if (CONSTANT_P (x)
7943 || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
7946 /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
7947 will be least confusing to the rest of the compiler. */
7948 else if (mode == BImode)
7950 *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
7951 return x;
7954 /* If X is known to be either 0 or -1, those are the true and
7955 false values when testing X. */
7956 else if (x == constm1_rtx || x == const0_rtx
7957 || (mode != VOIDmode
7958 && num_sign_bit_copies (x, mode) == GET_MODE_BITSIZE (mode)))
7960 *ptrue = constm1_rtx, *pfalse = const0_rtx;
7961 return x;
7964 /* Likewise for 0 or a single bit. */
7965 else if (SCALAR_INT_MODE_P (mode)
7966 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
7967 && exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
7969 *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
7970 return x;
7973 /* Otherwise fail; show no condition with true and false values the same. */
7974 *ptrue = *pfalse = x;
7975 return 0;
7978 /* Return the value of expression X given the fact that condition COND
7979 is known to be true when applied to REG as its first operand and VAL
7980 as its second. X is known to not be shared and so can be modified in
7981 place.
7983 We only handle the simplest cases, and specifically those cases that
7984 arise with IF_THEN_ELSE expressions. */
7986 static rtx
7987 known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
7989 enum rtx_code code = GET_CODE (x);
7990 rtx temp;
7991 const char *fmt;
7992 int i, j;
7994 if (side_effects_p (x))
7995 return x;
7997 /* If either operand of the condition is a floating point value,
7998 then we have to avoid collapsing an EQ comparison. */
7999 if (cond == EQ
8000 && rtx_equal_p (x, reg)
8001 && ! FLOAT_MODE_P (GET_MODE (x))
8002 && ! FLOAT_MODE_P (GET_MODE (val)))
8003 return val;
8005 if (cond == UNEQ && rtx_equal_p (x, reg))
8006 return val;
8008 /* If X is (abs REG) and we know something about REG's relationship
8009 with zero, we may be able to simplify this. */
8011 if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
8012 switch (cond)
8014 case GE: case GT: case EQ:
8015 return XEXP (x, 0);
8016 case LT: case LE:
8017 return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
8018 XEXP (x, 0),
8019 GET_MODE (XEXP (x, 0)));
8020 default:
8021 break;
8024 /* The only other cases we handle are MIN, MAX, and comparisons if the
8025 operands are the same as REG and VAL. */
8027 else if (COMPARISON_P (x) || COMMUTATIVE_ARITH_P (x))
8029 if (rtx_equal_p (XEXP (x, 0), val))
8030 cond = swap_condition (cond), temp = val, val = reg, reg = temp;
8032 if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
8034 if (COMPARISON_P (x))
8036 if (comparison_dominates_p (cond, code))
8037 return const_true_rtx;
8039 code = reversed_comparison_code (x, NULL);
8040 if (code != UNKNOWN
8041 && comparison_dominates_p (cond, code))
8042 return const0_rtx;
8043 else
8044 return x;
8046 else if (code == SMAX || code == SMIN
8047 || code == UMIN || code == UMAX)
8049 int unsignedp = (code == UMIN || code == UMAX);
8051 /* Do not reverse the condition when it is NE or EQ.
8052 This is because we cannot conclude anything about
8053 the value of 'SMAX (x, y)' when x is not equal to y,
8054 but we can when x equals y. */
8055 if ((code == SMAX || code == UMAX)
8056 && ! (cond == EQ || cond == NE))
8057 cond = reverse_condition (cond);
8059 switch (cond)
8061 case GE: case GT:
8062 return unsignedp ? x : XEXP (x, 1);
8063 case LE: case LT:
8064 return unsignedp ? x : XEXP (x, 0);
8065 case GEU: case GTU:
8066 return unsignedp ? XEXP (x, 1) : x;
8067 case LEU: case LTU:
8068 return unsignedp ? XEXP (x, 0) : x;
8069 default:
8070 break;
8075 else if (code == SUBREG)
8077 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
8078 rtx new_rtx, r = known_cond (SUBREG_REG (x), cond, reg, val);
8080 if (SUBREG_REG (x) != r)
8082 /* We must simplify subreg here, before we lose track of the
8083 original inner_mode. */
8084 new_rtx = simplify_subreg (GET_MODE (x), r,
8085 inner_mode, SUBREG_BYTE (x));
8086 if (new_rtx)
8087 return new_rtx;
8088 else
8089 SUBST (SUBREG_REG (x), r);
8092 return x;
8094 /* We don't have to handle SIGN_EXTEND here, because even in the
8095 case of replacing something with a modeless CONST_INT, a
8096 CONST_INT is already (supposed to be) a valid sign extension for
8097 its narrower mode, which implies it's already properly
8098 sign-extended for the wider mode. Now, for ZERO_EXTEND, the
8099 story is different. */
8100 else if (code == ZERO_EXTEND)
8102 enum machine_mode inner_mode = GET_MODE (XEXP (x, 0));
8103 rtx new_rtx, r = known_cond (XEXP (x, 0), cond, reg, val);
8105 if (XEXP (x, 0) != r)
8107 /* We must simplify the zero_extend here, before we lose
8108 track of the original inner_mode. */
8109 new_rtx = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
8110 r, inner_mode);
8111 if (new_rtx)
8112 return new_rtx;
8113 else
8114 SUBST (XEXP (x, 0), r);
8117 return x;
8120 fmt = GET_RTX_FORMAT (code);
8121 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8123 if (fmt[i] == 'e')
8124 SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
8125 else if (fmt[i] == 'E')
8126 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
8127 SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
8128 cond, reg, val));
8131 return x;
8134 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
8135 assignment as a field assignment. */
8137 static int
8138 rtx_equal_for_field_assignment_p (rtx x, rtx y)
8140 if (x == y || rtx_equal_p (x, y))
8141 return 1;
8143 if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
8144 return 0;
8146 /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
8147 Note that all SUBREGs of MEM are paradoxical; otherwise they
8148 would have been rewritten. */
8149 if (MEM_P (x) && GET_CODE (y) == SUBREG
8150 && MEM_P (SUBREG_REG (y))
8151 && rtx_equal_p (SUBREG_REG (y),
8152 gen_lowpart (GET_MODE (SUBREG_REG (y)), x)))
8153 return 1;
8155 if (MEM_P (y) && GET_CODE (x) == SUBREG
8156 && MEM_P (SUBREG_REG (x))
8157 && rtx_equal_p (SUBREG_REG (x),
8158 gen_lowpart (GET_MODE (SUBREG_REG (x)), y)))
8159 return 1;
8161 /* We used to see if get_last_value of X and Y were the same but that's
8162 not correct. In one direction, we'll cause the assignment to have
8163 the wrong destination and in the case, we'll import a register into this
8164 insn that might have already have been dead. So fail if none of the
8165 above cases are true. */
8166 return 0;
8169 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
8170 Return that assignment if so.
8172 We only handle the most common cases. */
8174 static rtx
8175 make_field_assignment (rtx x)
8177 rtx dest = SET_DEST (x);
8178 rtx src = SET_SRC (x);
8179 rtx assign;
8180 rtx rhs, lhs;
8181 HOST_WIDE_INT c1;
8182 HOST_WIDE_INT pos;
8183 unsigned HOST_WIDE_INT len;
8184 rtx other;
8185 enum machine_mode mode;
8187 /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
8188 a clear of a one-bit field. We will have changed it to
8189 (and (rotate (const_int -2) POS) DEST), so check for that. Also check
8190 for a SUBREG. */
8192 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
8193 && GET_CODE (XEXP (XEXP (src, 0), 0)) == CONST_INT
8194 && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
8195 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
8197 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
8198 1, 1, 1, 0);
8199 if (assign != 0)
8200 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
8201 return x;
8204 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
8205 && subreg_lowpart_p (XEXP (src, 0))
8206 && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
8207 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
8208 && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
8209 && GET_CODE (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == CONST_INT
8210 && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
8211 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
8213 assign = make_extraction (VOIDmode, dest, 0,
8214 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
8215 1, 1, 1, 0);
8216 if (assign != 0)
8217 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
8218 return x;
8221 /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
8222 one-bit field. */
8223 if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
8224 && XEXP (XEXP (src, 0), 0) == const1_rtx
8225 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
8227 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
8228 1, 1, 1, 0);
8229 if (assign != 0)
8230 return gen_rtx_SET (VOIDmode, assign, const1_rtx);
8231 return x;
8234 /* If DEST is already a field assignment, i.e. ZERO_EXTRACT, and the
8235 SRC is an AND with all bits of that field set, then we can discard
8236 the AND. */
8237 if (GET_CODE (dest) == ZERO_EXTRACT
8238 && GET_CODE (XEXP (dest, 1)) == CONST_INT
8239 && GET_CODE (src) == AND
8240 && GET_CODE (XEXP (src, 1)) == CONST_INT)
8242 HOST_WIDE_INT width = INTVAL (XEXP (dest, 1));
8243 unsigned HOST_WIDE_INT and_mask = INTVAL (XEXP (src, 1));
8244 unsigned HOST_WIDE_INT ze_mask;
8246 if (width >= HOST_BITS_PER_WIDE_INT)
8247 ze_mask = -1;
8248 else
8249 ze_mask = ((unsigned HOST_WIDE_INT)1 << width) - 1;
8251 /* Complete overlap. We can remove the source AND. */
8252 if ((and_mask & ze_mask) == ze_mask)
8253 return gen_rtx_SET (VOIDmode, dest, XEXP (src, 0));
8255 /* Partial overlap. We can reduce the source AND. */
8256 if ((and_mask & ze_mask) != and_mask)
8258 mode = GET_MODE (src);
8259 src = gen_rtx_AND (mode, XEXP (src, 0),
8260 gen_int_mode (and_mask & ze_mask, mode));
8261 return gen_rtx_SET (VOIDmode, dest, src);
8265 /* The other case we handle is assignments into a constant-position
8266 field. They look like (ior/xor (and DEST C1) OTHER). If C1 represents
8267 a mask that has all one bits except for a group of zero bits and
8268 OTHER is known to have zeros where C1 has ones, this is such an
8269 assignment. Compute the position and length from C1. Shift OTHER
8270 to the appropriate position, force it to the required mode, and
8271 make the extraction. Check for the AND in both operands. */
8273 if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
8274 return x;
8276 rhs = expand_compound_operation (XEXP (src, 0));
8277 lhs = expand_compound_operation (XEXP (src, 1));
8279 if (GET_CODE (rhs) == AND
8280 && GET_CODE (XEXP (rhs, 1)) == CONST_INT
8281 && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
8282 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
8283 else if (GET_CODE (lhs) == AND
8284 && GET_CODE (XEXP (lhs, 1)) == CONST_INT
8285 && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
8286 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
8287 else
8288 return x;
8290 pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
8291 if (pos < 0 || pos + len > GET_MODE_BITSIZE (GET_MODE (dest))
8292 || GET_MODE_BITSIZE (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
8293 || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
8294 return x;
8296 assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
8297 if (assign == 0)
8298 return x;
8300 /* The mode to use for the source is the mode of the assignment, or of
8301 what is inside a possible STRICT_LOW_PART. */
8302 mode = (GET_CODE (assign) == STRICT_LOW_PART
8303 ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
8305 /* Shift OTHER right POS places and make it the source, restricting it
8306 to the proper length and mode. */
8308 src = canon_reg_for_combine (simplify_shift_const (NULL_RTX, LSHIFTRT,
8309 GET_MODE (src),
8310 other, pos),
8311 dest);
8312 src = force_to_mode (src, mode,
8313 GET_MODE_BITSIZE (mode) >= HOST_BITS_PER_WIDE_INT
8314 ? ~(unsigned HOST_WIDE_INT) 0
8315 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
8318 /* If SRC is masked by an AND that does not make a difference in
8319 the value being stored, strip it. */
8320 if (GET_CODE (assign) == ZERO_EXTRACT
8321 && GET_CODE (XEXP (assign, 1)) == CONST_INT
8322 && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
8323 && GET_CODE (src) == AND
8324 && GET_CODE (XEXP (src, 1)) == CONST_INT
8325 && ((unsigned HOST_WIDE_INT) INTVAL (XEXP (src, 1))
8326 == ((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (assign, 1))) - 1))
8327 src = XEXP (src, 0);
8329 return gen_rtx_SET (VOIDmode, assign, src);
8332 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
8333 if so. */
8335 static rtx
8336 apply_distributive_law (rtx x)
8338 enum rtx_code code = GET_CODE (x);
8339 enum rtx_code inner_code;
8340 rtx lhs, rhs, other;
8341 rtx tem;
8343 /* Distributivity is not true for floating point as it can change the
8344 value. So we don't do it unless -funsafe-math-optimizations. */
8345 if (FLOAT_MODE_P (GET_MODE (x))
8346 && ! flag_unsafe_math_optimizations)
8347 return x;
8349 /* The outer operation can only be one of the following: */
8350 if (code != IOR && code != AND && code != XOR
8351 && code != PLUS && code != MINUS)
8352 return x;
8354 lhs = XEXP (x, 0);
8355 rhs = XEXP (x, 1);
8357 /* If either operand is a primitive we can't do anything, so get out
8358 fast. */
8359 if (OBJECT_P (lhs) || OBJECT_P (rhs))
8360 return x;
8362 lhs = expand_compound_operation (lhs);
8363 rhs = expand_compound_operation (rhs);
8364 inner_code = GET_CODE (lhs);
8365 if (inner_code != GET_CODE (rhs))
8366 return x;
8368 /* See if the inner and outer operations distribute. */
8369 switch (inner_code)
8371 case LSHIFTRT:
8372 case ASHIFTRT:
8373 case AND:
8374 case IOR:
8375 /* These all distribute except over PLUS. */
8376 if (code == PLUS || code == MINUS)
8377 return x;
8378 break;
8380 case MULT:
8381 if (code != PLUS && code != MINUS)
8382 return x;
8383 break;
8385 case ASHIFT:
8386 /* This is also a multiply, so it distributes over everything. */
8387 break;
8389 case SUBREG:
8390 /* Non-paradoxical SUBREGs distributes over all operations,
8391 provided the inner modes and byte offsets are the same, this
8392 is an extraction of a low-order part, we don't convert an fp
8393 operation to int or vice versa, this is not a vector mode,
8394 and we would not be converting a single-word operation into a
8395 multi-word operation. The latter test is not required, but
8396 it prevents generating unneeded multi-word operations. Some
8397 of the previous tests are redundant given the latter test,
8398 but are retained because they are required for correctness.
8400 We produce the result slightly differently in this case. */
8402 if (GET_MODE (SUBREG_REG (lhs)) != GET_MODE (SUBREG_REG (rhs))
8403 || SUBREG_BYTE (lhs) != SUBREG_BYTE (rhs)
8404 || ! subreg_lowpart_p (lhs)
8405 || (GET_MODE_CLASS (GET_MODE (lhs))
8406 != GET_MODE_CLASS (GET_MODE (SUBREG_REG (lhs))))
8407 || (GET_MODE_SIZE (GET_MODE (lhs))
8408 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))))
8409 || VECTOR_MODE_P (GET_MODE (lhs))
8410 || GET_MODE_SIZE (GET_MODE (SUBREG_REG (lhs))) > UNITS_PER_WORD
8411 /* Result might need to be truncated. Don't change mode if
8412 explicit truncation is needed. */
8413 || !TRULY_NOOP_TRUNCATION
8414 (GET_MODE_BITSIZE (GET_MODE (x)),
8415 GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (lhs)))))
8416 return x;
8418 tem = simplify_gen_binary (code, GET_MODE (SUBREG_REG (lhs)),
8419 SUBREG_REG (lhs), SUBREG_REG (rhs));
8420 return gen_lowpart (GET_MODE (x), tem);
8422 default:
8423 return x;
8426 /* Set LHS and RHS to the inner operands (A and B in the example
8427 above) and set OTHER to the common operand (C in the example).
8428 There is only one way to do this unless the inner operation is
8429 commutative. */
8430 if (COMMUTATIVE_ARITH_P (lhs)
8431 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
8432 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
8433 else if (COMMUTATIVE_ARITH_P (lhs)
8434 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
8435 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
8436 else if (COMMUTATIVE_ARITH_P (lhs)
8437 && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
8438 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
8439 else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
8440 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
8441 else
8442 return x;
8444 /* Form the new inner operation, seeing if it simplifies first. */
8445 tem = simplify_gen_binary (code, GET_MODE (x), lhs, rhs);
8447 /* There is one exception to the general way of distributing:
8448 (a | c) ^ (b | c) -> (a ^ b) & ~c */
8449 if (code == XOR && inner_code == IOR)
8451 inner_code = AND;
8452 other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
8455 /* We may be able to continuing distributing the result, so call
8456 ourselves recursively on the inner operation before forming the
8457 outer operation, which we return. */
8458 return simplify_gen_binary (inner_code, GET_MODE (x),
8459 apply_distributive_law (tem), other);
8462 /* See if X is of the form (* (+ A B) C), and if so convert to
8463 (+ (* A C) (* B C)) and try to simplify.
8465 Most of the time, this results in no change. However, if some of
8466 the operands are the same or inverses of each other, simplifications
8467 will result.
8469 For example, (and (ior A B) (not B)) can occur as the result of
8470 expanding a bit field assignment. When we apply the distributive
8471 law to this, we get (ior (and (A (not B))) (and (B (not B)))),
8472 which then simplifies to (and (A (not B))).
8474 Note that no checks happen on the validity of applying the inverse
8475 distributive law. This is pointless since we can do it in the
8476 few places where this routine is called.
8478 N is the index of the term that is decomposed (the arithmetic operation,
8479 i.e. (+ A B) in the first example above). !N is the index of the term that
8480 is distributed, i.e. of C in the first example above. */
8481 static rtx
8482 distribute_and_simplify_rtx (rtx x, int n)
8484 enum machine_mode mode;
8485 enum rtx_code outer_code, inner_code;
8486 rtx decomposed, distributed, inner_op0, inner_op1, new_op0, new_op1, tmp;
8488 decomposed = XEXP (x, n);
8489 if (!ARITHMETIC_P (decomposed))
8490 return NULL_RTX;
8492 mode = GET_MODE (x);
8493 outer_code = GET_CODE (x);
8494 distributed = XEXP (x, !n);
8496 inner_code = GET_CODE (decomposed);
8497 inner_op0 = XEXP (decomposed, 0);
8498 inner_op1 = XEXP (decomposed, 1);
8500 /* Special case (and (xor B C) (not A)), which is equivalent to
8501 (xor (ior A B) (ior A C)) */
8502 if (outer_code == AND && inner_code == XOR && GET_CODE (distributed) == NOT)
8504 distributed = XEXP (distributed, 0);
8505 outer_code = IOR;
8508 if (n == 0)
8510 /* Distribute the second term. */
8511 new_op0 = simplify_gen_binary (outer_code, mode, inner_op0, distributed);
8512 new_op1 = simplify_gen_binary (outer_code, mode, inner_op1, distributed);
8514 else
8516 /* Distribute the first term. */
8517 new_op0 = simplify_gen_binary (outer_code, mode, distributed, inner_op0);
8518 new_op1 = simplify_gen_binary (outer_code, mode, distributed, inner_op1);
8521 tmp = apply_distributive_law (simplify_gen_binary (inner_code, mode,
8522 new_op0, new_op1));
8523 if (GET_CODE (tmp) != outer_code
8524 && rtx_cost (tmp, SET) < rtx_cost (x, SET))
8525 return tmp;
8527 return NULL_RTX;
8530 /* Simplify a logical `and' of VAROP with the constant CONSTOP, to be done
8531 in MODE. Return an equivalent form, if different from (and VAROP
8532 (const_int CONSTOP)). Otherwise, return NULL_RTX. */
8534 static rtx
8535 simplify_and_const_int_1 (enum machine_mode mode, rtx varop,
8536 unsigned HOST_WIDE_INT constop)
8538 unsigned HOST_WIDE_INT nonzero;
8539 unsigned HOST_WIDE_INT orig_constop;
8540 rtx orig_varop;
8541 int i;
8543 orig_varop = varop;
8544 orig_constop = constop;
8545 if (GET_CODE (varop) == CLOBBER)
8546 return NULL_RTX;
8548 /* Simplify VAROP knowing that we will be only looking at some of the
8549 bits in it.
8551 Note by passing in CONSTOP, we guarantee that the bits not set in
8552 CONSTOP are not significant and will never be examined. We must
8553 ensure that is the case by explicitly masking out those bits
8554 before returning. */
8555 varop = force_to_mode (varop, mode, constop, 0);
8557 /* If VAROP is a CLOBBER, we will fail so return it. */
8558 if (GET_CODE (varop) == CLOBBER)
8559 return varop;
8561 /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
8562 to VAROP and return the new constant. */
8563 if (GET_CODE (varop) == CONST_INT)
8564 return gen_int_mode (INTVAL (varop) & constop, mode);
8566 /* See what bits may be nonzero in VAROP. Unlike the general case of
8567 a call to nonzero_bits, here we don't care about bits outside
8568 MODE. */
8570 nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
8572 /* Turn off all bits in the constant that are known to already be zero.
8573 Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
8574 which is tested below. */
8576 constop &= nonzero;
8578 /* If we don't have any bits left, return zero. */
8579 if (constop == 0)
8580 return const0_rtx;
8582 /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
8583 a power of two, we can replace this with an ASHIFT. */
8584 if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
8585 && (i = exact_log2 (constop)) >= 0)
8586 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
8588 /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
8589 or XOR, then try to apply the distributive law. This may eliminate
8590 operations if either branch can be simplified because of the AND.
8591 It may also make some cases more complex, but those cases probably
8592 won't match a pattern either with or without this. */
8594 if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
8595 return
8596 gen_lowpart
8597 (mode,
8598 apply_distributive_law
8599 (simplify_gen_binary (GET_CODE (varop), GET_MODE (varop),
8600 simplify_and_const_int (NULL_RTX,
8601 GET_MODE (varop),
8602 XEXP (varop, 0),
8603 constop),
8604 simplify_and_const_int (NULL_RTX,
8605 GET_MODE (varop),
8606 XEXP (varop, 1),
8607 constop))));
8609 /* If VAROP is PLUS, and the constant is a mask of low bits, distribute
8610 the AND and see if one of the operands simplifies to zero. If so, we
8611 may eliminate it. */
8613 if (GET_CODE (varop) == PLUS
8614 && exact_log2 (constop + 1) >= 0)
8616 rtx o0, o1;
8618 o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
8619 o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
8620 if (o0 == const0_rtx)
8621 return o1;
8622 if (o1 == const0_rtx)
8623 return o0;
8626 /* Make a SUBREG if necessary. If we can't make it, fail. */
8627 varop = gen_lowpart (mode, varop);
8628 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
8629 return NULL_RTX;
8631 /* If we are only masking insignificant bits, return VAROP. */
8632 if (constop == nonzero)
8633 return varop;
8635 if (varop == orig_varop && constop == orig_constop)
8636 return NULL_RTX;
8638 /* Otherwise, return an AND. */
8639 return simplify_gen_binary (AND, mode, varop, gen_int_mode (constop, mode));
8643 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
8644 in MODE.
8646 Return an equivalent form, if different from X. Otherwise, return X. If
8647 X is zero, we are to always construct the equivalent form. */
8649 static rtx
8650 simplify_and_const_int (rtx x, enum machine_mode mode, rtx varop,
8651 unsigned HOST_WIDE_INT constop)
8653 rtx tem = simplify_and_const_int_1 (mode, varop, constop);
8654 if (tem)
8655 return tem;
8657 if (!x)
8658 x = simplify_gen_binary (AND, GET_MODE (varop), varop,
8659 gen_int_mode (constop, mode));
8660 if (GET_MODE (x) != mode)
8661 x = gen_lowpart (mode, x);
8662 return x;
8665 /* Given a REG, X, compute which bits in X can be nonzero.
8666 We don't care about bits outside of those defined in MODE.
8668 For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
8669 a shift, AND, or zero_extract, we can do better. */
8671 static rtx
8672 reg_nonzero_bits_for_combine (const_rtx x, enum machine_mode mode,
8673 const_rtx known_x ATTRIBUTE_UNUSED,
8674 enum machine_mode known_mode ATTRIBUTE_UNUSED,
8675 unsigned HOST_WIDE_INT known_ret ATTRIBUTE_UNUSED,
8676 unsigned HOST_WIDE_INT *nonzero)
8678 rtx tem;
8679 reg_stat_type *rsp;
8681 /* If X is a register whose nonzero bits value is current, use it.
8682 Otherwise, if X is a register whose value we can find, use that
8683 value. Otherwise, use the previously-computed global nonzero bits
8684 for this register. */
8686 rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
8687 if (rsp->last_set_value != 0
8688 && (rsp->last_set_mode == mode
8689 || (GET_MODE_CLASS (rsp->last_set_mode) == MODE_INT
8690 && GET_MODE_CLASS (mode) == MODE_INT))
8691 && ((rsp->last_set_label >= label_tick_ebb_start
8692 && rsp->last_set_label < label_tick)
8693 || (rsp->last_set_label == label_tick
8694 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
8695 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
8696 && REG_N_SETS (REGNO (x)) == 1
8697 && !REGNO_REG_SET_P
8698 (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), REGNO (x)))))
8700 *nonzero &= rsp->last_set_nonzero_bits;
8701 return NULL;
8704 tem = get_last_value (x);
8706 if (tem)
8708 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
8709 /* If X is narrower than MODE and TEM is a non-negative
8710 constant that would appear negative in the mode of X,
8711 sign-extend it for use in reg_nonzero_bits because some
8712 machines (maybe most) will actually do the sign-extension
8713 and this is the conservative approach.
8715 ??? For 2.5, try to tighten up the MD files in this regard
8716 instead of this kludge. */
8718 if (GET_MODE_BITSIZE (GET_MODE (x)) < GET_MODE_BITSIZE (mode)
8719 && GET_CODE (tem) == CONST_INT
8720 && INTVAL (tem) > 0
8721 && 0 != (INTVAL (tem)
8722 & ((HOST_WIDE_INT) 1
8723 << (GET_MODE_BITSIZE (GET_MODE (x)) - 1))))
8724 tem = GEN_INT (INTVAL (tem)
8725 | ((HOST_WIDE_INT) (-1)
8726 << GET_MODE_BITSIZE (GET_MODE (x))));
8727 #endif
8728 return tem;
8730 else if (nonzero_sign_valid && rsp->nonzero_bits)
8732 unsigned HOST_WIDE_INT mask = rsp->nonzero_bits;
8734 if (GET_MODE_BITSIZE (GET_MODE (x)) < GET_MODE_BITSIZE (mode))
8735 /* We don't know anything about the upper bits. */
8736 mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (GET_MODE (x));
8737 *nonzero &= mask;
8740 return NULL;
8743 /* Return the number of bits at the high-order end of X that are known to
8744 be equal to the sign bit. X will be used in mode MODE; if MODE is
8745 VOIDmode, X will be used in its own mode. The returned value will always
8746 be between 1 and the number of bits in MODE. */
8748 static rtx
8749 reg_num_sign_bit_copies_for_combine (const_rtx x, enum machine_mode mode,
8750 const_rtx known_x ATTRIBUTE_UNUSED,
8751 enum machine_mode known_mode
8752 ATTRIBUTE_UNUSED,
8753 unsigned int known_ret ATTRIBUTE_UNUSED,
8754 unsigned int *result)
8756 rtx tem;
8757 reg_stat_type *rsp;
8759 rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
8760 if (rsp->last_set_value != 0
8761 && rsp->last_set_mode == mode
8762 && ((rsp->last_set_label >= label_tick_ebb_start
8763 && rsp->last_set_label < label_tick)
8764 || (rsp->last_set_label == label_tick
8765 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
8766 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
8767 && REG_N_SETS (REGNO (x)) == 1
8768 && !REGNO_REG_SET_P
8769 (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), REGNO (x)))))
8771 *result = rsp->last_set_sign_bit_copies;
8772 return NULL;
8775 tem = get_last_value (x);
8776 if (tem != 0)
8777 return tem;
8779 if (nonzero_sign_valid && rsp->sign_bit_copies != 0
8780 && GET_MODE_BITSIZE (GET_MODE (x)) == GET_MODE_BITSIZE (mode))
8781 *result = rsp->sign_bit_copies;
8783 return NULL;
8786 /* Return the number of "extended" bits there are in X, when interpreted
8787 as a quantity in MODE whose signedness is indicated by UNSIGNEDP. For
8788 unsigned quantities, this is the number of high-order zero bits.
8789 For signed quantities, this is the number of copies of the sign bit
8790 minus 1. In both case, this function returns the number of "spare"
8791 bits. For example, if two quantities for which this function returns
8792 at least 1 are added, the addition is known not to overflow.
8794 This function will always return 0 unless called during combine, which
8795 implies that it must be called from a define_split. */
8797 unsigned int
8798 extended_count (const_rtx x, enum machine_mode mode, int unsignedp)
8800 if (nonzero_sign_valid == 0)
8801 return 0;
8803 return (unsignedp
8804 ? (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
8805 ? (unsigned int) (GET_MODE_BITSIZE (mode) - 1
8806 - floor_log2 (nonzero_bits (x, mode)))
8807 : 0)
8808 : num_sign_bit_copies (x, mode) - 1);
8811 /* This function is called from `simplify_shift_const' to merge two
8812 outer operations. Specifically, we have already found that we need
8813 to perform operation *POP0 with constant *PCONST0 at the outermost
8814 position. We would now like to also perform OP1 with constant CONST1
8815 (with *POP0 being done last).
8817 Return 1 if we can do the operation and update *POP0 and *PCONST0 with
8818 the resulting operation. *PCOMP_P is set to 1 if we would need to
8819 complement the innermost operand, otherwise it is unchanged.
8821 MODE is the mode in which the operation will be done. No bits outside
8822 the width of this mode matter. It is assumed that the width of this mode
8823 is smaller than or equal to HOST_BITS_PER_WIDE_INT.
8825 If *POP0 or OP1 are UNKNOWN, it means no operation is required. Only NEG, PLUS,
8826 IOR, XOR, and AND are supported. We may set *POP0 to SET if the proper
8827 result is simply *PCONST0.
8829 If the resulting operation cannot be expressed as one operation, we
8830 return 0 and do not change *POP0, *PCONST0, and *PCOMP_P. */
8832 static int
8833 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)
8835 enum rtx_code op0 = *pop0;
8836 HOST_WIDE_INT const0 = *pconst0;
8838 const0 &= GET_MODE_MASK (mode);
8839 const1 &= GET_MODE_MASK (mode);
8841 /* If OP0 is an AND, clear unimportant bits in CONST1. */
8842 if (op0 == AND)
8843 const1 &= const0;
8845 /* If OP0 or OP1 is UNKNOWN, this is easy. Similarly if they are the same or
8846 if OP0 is SET. */
8848 if (op1 == UNKNOWN || op0 == SET)
8849 return 1;
8851 else if (op0 == UNKNOWN)
8852 op0 = op1, const0 = const1;
8854 else if (op0 == op1)
8856 switch (op0)
8858 case AND:
8859 const0 &= const1;
8860 break;
8861 case IOR:
8862 const0 |= const1;
8863 break;
8864 case XOR:
8865 const0 ^= const1;
8866 break;
8867 case PLUS:
8868 const0 += const1;
8869 break;
8870 case NEG:
8871 op0 = UNKNOWN;
8872 break;
8873 default:
8874 break;
8878 /* Otherwise, if either is a PLUS or NEG, we can't do anything. */
8879 else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
8880 return 0;
8882 /* If the two constants aren't the same, we can't do anything. The
8883 remaining six cases can all be done. */
8884 else if (const0 != const1)
8885 return 0;
8887 else
8888 switch (op0)
8890 case IOR:
8891 if (op1 == AND)
8892 /* (a & b) | b == b */
8893 op0 = SET;
8894 else /* op1 == XOR */
8895 /* (a ^ b) | b == a | b */
8897 break;
8899 case XOR:
8900 if (op1 == AND)
8901 /* (a & b) ^ b == (~a) & b */
8902 op0 = AND, *pcomp_p = 1;
8903 else /* op1 == IOR */
8904 /* (a | b) ^ b == a & ~b */
8905 op0 = AND, const0 = ~const0;
8906 break;
8908 case AND:
8909 if (op1 == IOR)
8910 /* (a | b) & b == b */
8911 op0 = SET;
8912 else /* op1 == XOR */
8913 /* (a ^ b) & b) == (~a) & b */
8914 *pcomp_p = 1;
8915 break;
8916 default:
8917 break;
8920 /* Check for NO-OP cases. */
8921 const0 &= GET_MODE_MASK (mode);
8922 if (const0 == 0
8923 && (op0 == IOR || op0 == XOR || op0 == PLUS))
8924 op0 = UNKNOWN;
8925 else if (const0 == 0 && op0 == AND)
8926 op0 = SET;
8927 else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
8928 && op0 == AND)
8929 op0 = UNKNOWN;
8931 /* ??? Slightly redundant with the above mask, but not entirely.
8932 Moving this above means we'd have to sign-extend the mode mask
8933 for the final test. */
8934 const0 = trunc_int_for_mode (const0, mode);
8936 *pop0 = op0;
8937 *pconst0 = const0;
8939 return 1;
8942 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
8943 The result of the shift is RESULT_MODE. Return NULL_RTX if we cannot
8944 simplify it. Otherwise, return a simplified value.
8946 The shift is normally computed in the widest mode we find in VAROP, as
8947 long as it isn't a different number of words than RESULT_MODE. Exceptions
8948 are ASHIFTRT and ROTATE, which are always done in their original mode. */
8950 static rtx
8951 simplify_shift_const_1 (enum rtx_code code, enum machine_mode result_mode,
8952 rtx varop, int orig_count)
8954 enum rtx_code orig_code = code;
8955 rtx orig_varop = varop;
8956 int count;
8957 enum machine_mode mode = result_mode;
8958 enum machine_mode shift_mode, tmode;
8959 unsigned int mode_words
8960 = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
8961 /* We form (outer_op (code varop count) (outer_const)). */
8962 enum rtx_code outer_op = UNKNOWN;
8963 HOST_WIDE_INT outer_const = 0;
8964 int complement_p = 0;
8965 rtx new_rtx, x;
8967 /* Make sure and truncate the "natural" shift on the way in. We don't
8968 want to do this inside the loop as it makes it more difficult to
8969 combine shifts. */
8970 if (SHIFT_COUNT_TRUNCATED)
8971 orig_count &= GET_MODE_BITSIZE (mode) - 1;
8973 /* If we were given an invalid count, don't do anything except exactly
8974 what was requested. */
8976 if (orig_count < 0 || orig_count >= (int) GET_MODE_BITSIZE (mode))
8977 return NULL_RTX;
8979 count = orig_count;
8981 /* Unless one of the branches of the `if' in this loop does a `continue',
8982 we will `break' the loop after the `if'. */
8984 while (count != 0)
8986 /* If we have an operand of (clobber (const_int 0)), fail. */
8987 if (GET_CODE (varop) == CLOBBER)
8988 return NULL_RTX;
8990 /* If we discovered we had to complement VAROP, leave. Making a NOT
8991 here would cause an infinite loop. */
8992 if (complement_p)
8993 break;
8995 /* Convert ROTATERT to ROTATE. */
8996 if (code == ROTATERT)
8998 unsigned int bitsize = GET_MODE_BITSIZE (result_mode);;
8999 code = ROTATE;
9000 if (VECTOR_MODE_P (result_mode))
9001 count = bitsize / GET_MODE_NUNITS (result_mode) - count;
9002 else
9003 count = bitsize - count;
9006 /* We need to determine what mode we will do the shift in. If the
9007 shift is a right shift or a ROTATE, we must always do it in the mode
9008 it was originally done in. Otherwise, we can do it in MODE, the
9009 widest mode encountered. */
9010 shift_mode
9011 = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
9012 ? result_mode : mode);
9014 /* Handle cases where the count is greater than the size of the mode
9015 minus 1. For ASHIFT, use the size minus one as the count (this can
9016 occur when simplifying (lshiftrt (ashiftrt ..))). For rotates,
9017 take the count modulo the size. For other shifts, the result is
9018 zero.
9020 Since these shifts are being produced by the compiler by combining
9021 multiple operations, each of which are defined, we know what the
9022 result is supposed to be. */
9024 if (count > (GET_MODE_BITSIZE (shift_mode) - 1))
9026 if (code == ASHIFTRT)
9027 count = GET_MODE_BITSIZE (shift_mode) - 1;
9028 else if (code == ROTATE || code == ROTATERT)
9029 count %= GET_MODE_BITSIZE (shift_mode);
9030 else
9032 /* We can't simply return zero because there may be an
9033 outer op. */
9034 varop = const0_rtx;
9035 count = 0;
9036 break;
9040 /* An arithmetic right shift of a quantity known to be -1 or 0
9041 is a no-op. */
9042 if (code == ASHIFTRT
9043 && (num_sign_bit_copies (varop, shift_mode)
9044 == GET_MODE_BITSIZE (shift_mode)))
9046 count = 0;
9047 break;
9050 /* If we are doing an arithmetic right shift and discarding all but
9051 the sign bit copies, this is equivalent to doing a shift by the
9052 bitsize minus one. Convert it into that shift because it will often
9053 allow other simplifications. */
9055 if (code == ASHIFTRT
9056 && (count + num_sign_bit_copies (varop, shift_mode)
9057 >= GET_MODE_BITSIZE (shift_mode)))
9058 count = GET_MODE_BITSIZE (shift_mode) - 1;
9060 /* We simplify the tests below and elsewhere by converting
9061 ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
9062 `make_compound_operation' will convert it to an ASHIFTRT for
9063 those machines (such as VAX) that don't have an LSHIFTRT. */
9064 if (GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9065 && code == ASHIFTRT
9066 && ((nonzero_bits (varop, shift_mode)
9067 & ((HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (shift_mode) - 1)))
9068 == 0))
9069 code = LSHIFTRT;
9071 if (((code == LSHIFTRT
9072 && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9073 && !(nonzero_bits (varop, shift_mode) >> count))
9074 || (code == ASHIFT
9075 && GET_MODE_BITSIZE (shift_mode) <= HOST_BITS_PER_WIDE_INT
9076 && !((nonzero_bits (varop, shift_mode) << count)
9077 & GET_MODE_MASK (shift_mode))))
9078 && !side_effects_p (varop))
9079 varop = const0_rtx;
9081 switch (GET_CODE (varop))
9083 case SIGN_EXTEND:
9084 case ZERO_EXTEND:
9085 case SIGN_EXTRACT:
9086 case ZERO_EXTRACT:
9087 new_rtx = expand_compound_operation (varop);
9088 if (new_rtx != varop)
9090 varop = new_rtx;
9091 continue;
9093 break;
9095 case MEM:
9096 /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
9097 minus the width of a smaller mode, we can do this with a
9098 SIGN_EXTEND or ZERO_EXTEND from the narrower memory location. */
9099 if ((code == ASHIFTRT || code == LSHIFTRT)
9100 && ! mode_dependent_address_p (XEXP (varop, 0))
9101 && ! MEM_VOLATILE_P (varop)
9102 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
9103 MODE_INT, 1)) != BLKmode)
9105 new_rtx = adjust_address_nv (varop, tmode,
9106 BYTES_BIG_ENDIAN ? 0
9107 : count / BITS_PER_UNIT);
9109 varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
9110 : ZERO_EXTEND, mode, new_rtx);
9111 count = 0;
9112 continue;
9114 break;
9116 case SUBREG:
9117 /* If VAROP is a SUBREG, strip it as long as the inner operand has
9118 the same number of words as what we've seen so far. Then store
9119 the widest mode in MODE. */
9120 if (subreg_lowpart_p (varop)
9121 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
9122 > GET_MODE_SIZE (GET_MODE (varop)))
9123 && (unsigned int) ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
9124 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
9125 == mode_words)
9127 varop = SUBREG_REG (varop);
9128 if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
9129 mode = GET_MODE (varop);
9130 continue;
9132 break;
9134 case MULT:
9135 /* Some machines use MULT instead of ASHIFT because MULT
9136 is cheaper. But it is still better on those machines to
9137 merge two shifts into one. */
9138 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9139 && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
9141 varop
9142 = simplify_gen_binary (ASHIFT, GET_MODE (varop),
9143 XEXP (varop, 0),
9144 GEN_INT (exact_log2 (
9145 INTVAL (XEXP (varop, 1)))));
9146 continue;
9148 break;
9150 case UDIV:
9151 /* Similar, for when divides are cheaper. */
9152 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9153 && exact_log2 (INTVAL (XEXP (varop, 1))) >= 0)
9155 varop
9156 = simplify_gen_binary (LSHIFTRT, GET_MODE (varop),
9157 XEXP (varop, 0),
9158 GEN_INT (exact_log2 (
9159 INTVAL (XEXP (varop, 1)))));
9160 continue;
9162 break;
9164 case ASHIFTRT:
9165 /* If we are extracting just the sign bit of an arithmetic
9166 right shift, that shift is not needed. However, the sign
9167 bit of a wider mode may be different from what would be
9168 interpreted as the sign bit in a narrower mode, so, if
9169 the result is narrower, don't discard the shift. */
9170 if (code == LSHIFTRT
9171 && count == (GET_MODE_BITSIZE (result_mode) - 1)
9172 && (GET_MODE_BITSIZE (result_mode)
9173 >= GET_MODE_BITSIZE (GET_MODE (varop))))
9175 varop = XEXP (varop, 0);
9176 continue;
9179 /* ... fall through ... */
9181 case LSHIFTRT:
9182 case ASHIFT:
9183 case ROTATE:
9184 /* Here we have two nested shifts. The result is usually the
9185 AND of a new shift with a mask. We compute the result below. */
9186 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9187 && INTVAL (XEXP (varop, 1)) >= 0
9188 && INTVAL (XEXP (varop, 1)) < GET_MODE_BITSIZE (GET_MODE (varop))
9189 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9190 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
9191 && !VECTOR_MODE_P (result_mode))
9193 enum rtx_code first_code = GET_CODE (varop);
9194 unsigned int first_count = INTVAL (XEXP (varop, 1));
9195 unsigned HOST_WIDE_INT mask;
9196 rtx mask_rtx;
9198 /* We have one common special case. We can't do any merging if
9199 the inner code is an ASHIFTRT of a smaller mode. However, if
9200 we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
9201 with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
9202 we can convert it to
9203 (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0 C2) C3) C1).
9204 This simplifies certain SIGN_EXTEND operations. */
9205 if (code == ASHIFT && first_code == ASHIFTRT
9206 && count == (GET_MODE_BITSIZE (result_mode)
9207 - GET_MODE_BITSIZE (GET_MODE (varop))))
9209 /* C3 has the low-order C1 bits zero. */
9211 mask = (GET_MODE_MASK (mode)
9212 & ~(((HOST_WIDE_INT) 1 << first_count) - 1));
9214 varop = simplify_and_const_int (NULL_RTX, result_mode,
9215 XEXP (varop, 0), mask);
9216 varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
9217 varop, count);
9218 count = first_count;
9219 code = ASHIFTRT;
9220 continue;
9223 /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
9224 than C1 high-order bits equal to the sign bit, we can convert
9225 this to either an ASHIFT or an ASHIFTRT depending on the
9226 two counts.
9228 We cannot do this if VAROP's mode is not SHIFT_MODE. */
9230 if (code == ASHIFTRT && first_code == ASHIFT
9231 && GET_MODE (varop) == shift_mode
9232 && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
9233 > first_count))
9235 varop = XEXP (varop, 0);
9236 count -= first_count;
9237 if (count < 0)
9239 count = -count;
9240 code = ASHIFT;
9243 continue;
9246 /* There are some cases we can't do. If CODE is ASHIFTRT,
9247 we can only do this if FIRST_CODE is also ASHIFTRT.
9249 We can't do the case when CODE is ROTATE and FIRST_CODE is
9250 ASHIFTRT.
9252 If the mode of this shift is not the mode of the outer shift,
9253 we can't do this if either shift is a right shift or ROTATE.
9255 Finally, we can't do any of these if the mode is too wide
9256 unless the codes are the same.
9258 Handle the case where the shift codes are the same
9259 first. */
9261 if (code == first_code)
9263 if (GET_MODE (varop) != result_mode
9264 && (code == ASHIFTRT || code == LSHIFTRT
9265 || code == ROTATE))
9266 break;
9268 count += first_count;
9269 varop = XEXP (varop, 0);
9270 continue;
9273 if (code == ASHIFTRT
9274 || (code == ROTATE && first_code == ASHIFTRT)
9275 || GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT
9276 || (GET_MODE (varop) != result_mode
9277 && (first_code == ASHIFTRT || first_code == LSHIFTRT
9278 || first_code == ROTATE
9279 || code == ROTATE)))
9280 break;
9282 /* To compute the mask to apply after the shift, shift the
9283 nonzero bits of the inner shift the same way the
9284 outer shift will. */
9286 mask_rtx = GEN_INT (nonzero_bits (varop, GET_MODE (varop)));
9288 mask_rtx
9289 = simplify_const_binary_operation (code, result_mode, mask_rtx,
9290 GEN_INT (count));
9292 /* Give up if we can't compute an outer operation to use. */
9293 if (mask_rtx == 0
9294 || GET_CODE (mask_rtx) != CONST_INT
9295 || ! merge_outer_ops (&outer_op, &outer_const, AND,
9296 INTVAL (mask_rtx),
9297 result_mode, &complement_p))
9298 break;
9300 /* If the shifts are in the same direction, we add the
9301 counts. Otherwise, we subtract them. */
9302 if ((code == ASHIFTRT || code == LSHIFTRT)
9303 == (first_code == ASHIFTRT || first_code == LSHIFTRT))
9304 count += first_count;
9305 else
9306 count -= first_count;
9308 /* If COUNT is positive, the new shift is usually CODE,
9309 except for the two exceptions below, in which case it is
9310 FIRST_CODE. If the count is negative, FIRST_CODE should
9311 always be used */
9312 if (count > 0
9313 && ((first_code == ROTATE && code == ASHIFT)
9314 || (first_code == ASHIFTRT && code == LSHIFTRT)))
9315 code = first_code;
9316 else if (count < 0)
9317 code = first_code, count = -count;
9319 varop = XEXP (varop, 0);
9320 continue;
9323 /* If we have (A << B << C) for any shift, we can convert this to
9324 (A << C << B). This wins if A is a constant. Only try this if
9325 B is not a constant. */
9327 else if (GET_CODE (varop) == code
9328 && GET_CODE (XEXP (varop, 0)) == CONST_INT
9329 && GET_CODE (XEXP (varop, 1)) != CONST_INT)
9331 rtx new_rtx = simplify_const_binary_operation (code, mode,
9332 XEXP (varop, 0),
9333 GEN_INT (count));
9334 varop = gen_rtx_fmt_ee (code, mode, new_rtx, XEXP (varop, 1));
9335 count = 0;
9336 continue;
9338 break;
9340 case NOT:
9341 if (VECTOR_MODE_P (mode))
9342 break;
9344 /* Make this fit the case below. */
9345 varop = gen_rtx_XOR (mode, XEXP (varop, 0),
9346 GEN_INT (GET_MODE_MASK (mode)));
9347 continue;
9349 case IOR:
9350 case AND:
9351 case XOR:
9352 /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
9353 with C the size of VAROP - 1 and the shift is logical if
9354 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
9355 we have an (le X 0) operation. If we have an arithmetic shift
9356 and STORE_FLAG_VALUE is 1 or we have a logical shift with
9357 STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation. */
9359 if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
9360 && XEXP (XEXP (varop, 0), 1) == constm1_rtx
9361 && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9362 && (code == LSHIFTRT || code == ASHIFTRT)
9363 && count == (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
9364 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
9366 count = 0;
9367 varop = gen_rtx_LE (GET_MODE (varop), XEXP (varop, 1),
9368 const0_rtx);
9370 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
9371 varop = gen_rtx_NEG (GET_MODE (varop), varop);
9373 continue;
9376 /* If we have (shift (logical)), move the logical to the outside
9377 to allow it to possibly combine with another logical and the
9378 shift to combine with another shift. This also canonicalizes to
9379 what a ZERO_EXTRACT looks like. Also, some machines have
9380 (and (shift)) insns. */
9382 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9383 /* We can't do this if we have (ashiftrt (xor)) and the
9384 constant has its sign bit set in shift_mode. */
9385 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
9386 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
9387 shift_mode))
9388 && (new_rtx = simplify_const_binary_operation (code, result_mode,
9389 XEXP (varop, 1),
9390 GEN_INT (count))) != 0
9391 && GET_CODE (new_rtx) == CONST_INT
9392 && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
9393 INTVAL (new_rtx), result_mode, &complement_p))
9395 varop = XEXP (varop, 0);
9396 continue;
9399 /* If we can't do that, try to simplify the shift in each arm of the
9400 logical expression, make a new logical expression, and apply
9401 the inverse distributive law. This also can't be done
9402 for some (ashiftrt (xor)). */
9403 if (GET_CODE (XEXP (varop, 1)) == CONST_INT
9404 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
9405 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
9406 shift_mode)))
9408 rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
9409 XEXP (varop, 0), count);
9410 rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
9411 XEXP (varop, 1), count);
9413 varop = simplify_gen_binary (GET_CODE (varop), shift_mode,
9414 lhs, rhs);
9415 varop = apply_distributive_law (varop);
9417 count = 0;
9418 continue;
9420 break;
9422 case EQ:
9423 /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
9424 says that the sign bit can be tested, FOO has mode MODE, C is
9425 GET_MODE_BITSIZE (MODE) - 1, and FOO has only its low-order bit
9426 that may be nonzero. */
9427 if (code == LSHIFTRT
9428 && XEXP (varop, 1) == const0_rtx
9429 && GET_MODE (XEXP (varop, 0)) == result_mode
9430 && count == (GET_MODE_BITSIZE (result_mode) - 1)
9431 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9432 && STORE_FLAG_VALUE == -1
9433 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
9434 && merge_outer_ops (&outer_op, &outer_const, XOR,
9435 (HOST_WIDE_INT) 1, result_mode,
9436 &complement_p))
9438 varop = XEXP (varop, 0);
9439 count = 0;
9440 continue;
9442 break;
9444 case NEG:
9445 /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
9446 than the number of bits in the mode is equivalent to A. */
9447 if (code == LSHIFTRT
9448 && count == (GET_MODE_BITSIZE (result_mode) - 1)
9449 && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
9451 varop = XEXP (varop, 0);
9452 count = 0;
9453 continue;
9456 /* NEG commutes with ASHIFT since it is multiplication. Move the
9457 NEG outside to allow shifts to combine. */
9458 if (code == ASHIFT
9459 && merge_outer_ops (&outer_op, &outer_const, NEG,
9460 (HOST_WIDE_INT) 0, result_mode,
9461 &complement_p))
9463 varop = XEXP (varop, 0);
9464 continue;
9466 break;
9468 case PLUS:
9469 /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
9470 is one less than the number of bits in the mode is
9471 equivalent to (xor A 1). */
9472 if (code == LSHIFTRT
9473 && count == (GET_MODE_BITSIZE (result_mode) - 1)
9474 && XEXP (varop, 1) == constm1_rtx
9475 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
9476 && merge_outer_ops (&outer_op, &outer_const, XOR,
9477 (HOST_WIDE_INT) 1, result_mode,
9478 &complement_p))
9480 count = 0;
9481 varop = XEXP (varop, 0);
9482 continue;
9485 /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
9486 that might be nonzero in BAR are those being shifted out and those
9487 bits are known zero in FOO, we can replace the PLUS with FOO.
9488 Similarly in the other operand order. This code occurs when
9489 we are computing the size of a variable-size array. */
9491 if ((code == ASHIFTRT || code == LSHIFTRT)
9492 && count < HOST_BITS_PER_WIDE_INT
9493 && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
9494 && (nonzero_bits (XEXP (varop, 1), result_mode)
9495 & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
9497 varop = XEXP (varop, 0);
9498 continue;
9500 else if ((code == ASHIFTRT || code == LSHIFTRT)
9501 && count < HOST_BITS_PER_WIDE_INT
9502 && GET_MODE_BITSIZE (result_mode) <= HOST_BITS_PER_WIDE_INT
9503 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
9504 >> count)
9505 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
9506 & nonzero_bits (XEXP (varop, 1),
9507 result_mode)))
9509 varop = XEXP (varop, 1);
9510 continue;
9513 /* (ashift (plus foo C) N) is (plus (ashift foo N) C'). */
9514 if (code == ASHIFT
9515 && GET_CODE (XEXP (varop, 1)) == CONST_INT
9516 && (new_rtx = simplify_const_binary_operation (ASHIFT, result_mode,
9517 XEXP (varop, 1),
9518 GEN_INT (count))) != 0
9519 && GET_CODE (new_rtx) == CONST_INT
9520 && merge_outer_ops (&outer_op, &outer_const, PLUS,
9521 INTVAL (new_rtx), result_mode, &complement_p))
9523 varop = XEXP (varop, 0);
9524 continue;
9527 /* Check for 'PLUS signbit', which is the canonical form of 'XOR
9528 signbit', and attempt to change the PLUS to an XOR and move it to
9529 the outer operation as is done above in the AND/IOR/XOR case
9530 leg for shift(logical). See details in logical handling above
9531 for reasoning in doing so. */
9532 if (code == LSHIFTRT
9533 && GET_CODE (XEXP (varop, 1)) == CONST_INT
9534 && mode_signbit_p (result_mode, XEXP (varop, 1))
9535 && (new_rtx = simplify_const_binary_operation (code, result_mode,
9536 XEXP (varop, 1),
9537 GEN_INT (count))) != 0
9538 && GET_CODE (new_rtx) == CONST_INT
9539 && merge_outer_ops (&outer_op, &outer_const, XOR,
9540 INTVAL (new_rtx), result_mode, &complement_p))
9542 varop = XEXP (varop, 0);
9543 continue;
9546 break;
9548 case MINUS:
9549 /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
9550 with C the size of VAROP - 1 and the shift is logical if
9551 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
9552 we have a (gt X 0) operation. If the shift is arithmetic with
9553 STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
9554 we have a (neg (gt X 0)) operation. */
9556 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9557 && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
9558 && count == (GET_MODE_BITSIZE (GET_MODE (varop)) - 1)
9559 && (code == LSHIFTRT || code == ASHIFTRT)
9560 && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
9561 && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
9562 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
9564 count = 0;
9565 varop = gen_rtx_GT (GET_MODE (varop), XEXP (varop, 1),
9566 const0_rtx);
9568 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
9569 varop = gen_rtx_NEG (GET_MODE (varop), varop);
9571 continue;
9573 break;
9575 case TRUNCATE:
9576 /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
9577 if the truncate does not affect the value. */
9578 if (code == LSHIFTRT
9579 && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
9580 && GET_CODE (XEXP (XEXP (varop, 0), 1)) == CONST_INT
9581 && (INTVAL (XEXP (XEXP (varop, 0), 1))
9582 >= (GET_MODE_BITSIZE (GET_MODE (XEXP (varop, 0)))
9583 - GET_MODE_BITSIZE (GET_MODE (varop)))))
9585 rtx varop_inner = XEXP (varop, 0);
9587 varop_inner
9588 = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
9589 XEXP (varop_inner, 0),
9590 GEN_INT
9591 (count + INTVAL (XEXP (varop_inner, 1))));
9592 varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
9593 count = 0;
9594 continue;
9596 break;
9598 default:
9599 break;
9602 break;
9605 /* We need to determine what mode to do the shift in. If the shift is
9606 a right shift or ROTATE, we must always do it in the mode it was
9607 originally done in. Otherwise, we can do it in MODE, the widest mode
9608 encountered. The code we care about is that of the shift that will
9609 actually be done, not the shift that was originally requested. */
9610 shift_mode
9611 = (code == ASHIFTRT || code == LSHIFTRT || code == ROTATE
9612 ? result_mode : mode);
9614 /* We have now finished analyzing the shift. The result should be
9615 a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places. If
9616 OUTER_OP is non-UNKNOWN, it is an operation that needs to be applied
9617 to the result of the shift. OUTER_CONST is the relevant constant,
9618 but we must turn off all bits turned off in the shift. */
9620 if (outer_op == UNKNOWN
9621 && orig_code == code && orig_count == count
9622 && varop == orig_varop
9623 && shift_mode == GET_MODE (varop))
9624 return NULL_RTX;
9626 /* Make a SUBREG if necessary. If we can't make it, fail. */
9627 varop = gen_lowpart (shift_mode, varop);
9628 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
9629 return NULL_RTX;
9631 /* If we have an outer operation and we just made a shift, it is
9632 possible that we could have simplified the shift were it not
9633 for the outer operation. So try to do the simplification
9634 recursively. */
9636 if (outer_op != UNKNOWN)
9637 x = simplify_shift_const_1 (code, shift_mode, varop, count);
9638 else
9639 x = NULL_RTX;
9641 if (x == NULL_RTX)
9642 x = simplify_gen_binary (code, shift_mode, varop, GEN_INT (count));
9644 /* If we were doing an LSHIFTRT in a wider mode than it was originally,
9645 turn off all the bits that the shift would have turned off. */
9646 if (orig_code == LSHIFTRT && result_mode != shift_mode)
9647 x = simplify_and_const_int (NULL_RTX, shift_mode, x,
9648 GET_MODE_MASK (result_mode) >> orig_count);
9650 /* Do the remainder of the processing in RESULT_MODE. */
9651 x = gen_lowpart_or_truncate (result_mode, x);
9653 /* If COMPLEMENT_P is set, we have to complement X before doing the outer
9654 operation. */
9655 if (complement_p)
9656 x = simplify_gen_unary (NOT, result_mode, x, result_mode);
9658 if (outer_op != UNKNOWN)
9660 if (GET_MODE_BITSIZE (result_mode) < HOST_BITS_PER_WIDE_INT)
9661 outer_const = trunc_int_for_mode (outer_const, result_mode);
9663 if (outer_op == AND)
9664 x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
9665 else if (outer_op == SET)
9667 /* This means that we have determined that the result is
9668 equivalent to a constant. This should be rare. */
9669 if (!side_effects_p (x))
9670 x = GEN_INT (outer_const);
9672 else if (GET_RTX_CLASS (outer_op) == RTX_UNARY)
9673 x = simplify_gen_unary (outer_op, result_mode, x, result_mode);
9674 else
9675 x = simplify_gen_binary (outer_op, result_mode, x,
9676 GEN_INT (outer_const));
9679 return x;
9682 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
9683 The result of the shift is RESULT_MODE. If we cannot simplify it,
9684 return X or, if it is NULL, synthesize the expression with
9685 simplify_gen_binary. Otherwise, return a simplified value.
9687 The shift is normally computed in the widest mode we find in VAROP, as
9688 long as it isn't a different number of words than RESULT_MODE. Exceptions
9689 are ASHIFTRT and ROTATE, which are always done in their original mode. */
9691 static rtx
9692 simplify_shift_const (rtx x, enum rtx_code code, enum machine_mode result_mode,
9693 rtx varop, int count)
9695 rtx tem = simplify_shift_const_1 (code, result_mode, varop, count);
9696 if (tem)
9697 return tem;
9699 if (!x)
9700 x = simplify_gen_binary (code, GET_MODE (varop), varop, GEN_INT (count));
9701 if (GET_MODE (x) != result_mode)
9702 x = gen_lowpart (result_mode, x);
9703 return x;
9707 /* Like recog, but we receive the address of a pointer to a new pattern.
9708 We try to match the rtx that the pointer points to.
9709 If that fails, we may try to modify or replace the pattern,
9710 storing the replacement into the same pointer object.
9712 Modifications include deletion or addition of CLOBBERs.
9714 PNOTES is a pointer to a location where any REG_UNUSED notes added for
9715 the CLOBBERs are placed.
9717 The value is the final insn code from the pattern ultimately matched,
9718 or -1. */
9720 static int
9721 recog_for_combine (rtx *pnewpat, rtx insn, rtx *pnotes)
9723 rtx pat = *pnewpat;
9724 int insn_code_number;
9725 int num_clobbers_to_add = 0;
9726 int i;
9727 rtx notes = 0;
9728 rtx old_notes, old_pat;
9730 /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
9731 we use to indicate that something didn't match. If we find such a
9732 thing, force rejection. */
9733 if (GET_CODE (pat) == PARALLEL)
9734 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
9735 if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
9736 && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
9737 return -1;
9739 old_pat = PATTERN (insn);
9740 old_notes = REG_NOTES (insn);
9741 PATTERN (insn) = pat;
9742 REG_NOTES (insn) = 0;
9744 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
9745 if (dump_file && (dump_flags & TDF_DETAILS))
9747 if (insn_code_number < 0)
9748 fputs ("Failed to match this instruction:\n", dump_file);
9749 else
9750 fputs ("Successfully matched this instruction:\n", dump_file);
9751 print_rtl_single (dump_file, pat);
9754 /* If it isn't, there is the possibility that we previously had an insn
9755 that clobbered some register as a side effect, but the combined
9756 insn doesn't need to do that. So try once more without the clobbers
9757 unless this represents an ASM insn. */
9759 if (insn_code_number < 0 && ! check_asm_operands (pat)
9760 && GET_CODE (pat) == PARALLEL)
9762 int pos;
9764 for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
9765 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
9767 if (i != pos)
9768 SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
9769 pos++;
9772 SUBST_INT (XVECLEN (pat, 0), pos);
9774 if (pos == 1)
9775 pat = XVECEXP (pat, 0, 0);
9777 PATTERN (insn) = pat;
9778 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
9779 if (dump_file && (dump_flags & TDF_DETAILS))
9781 if (insn_code_number < 0)
9782 fputs ("Failed to match this instruction:\n", dump_file);
9783 else
9784 fputs ("Successfully matched this instruction:\n", dump_file);
9785 print_rtl_single (dump_file, pat);
9788 PATTERN (insn) = old_pat;
9789 REG_NOTES (insn) = old_notes;
9791 /* Recognize all noop sets, these will be killed by followup pass. */
9792 if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
9793 insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
9795 /* If we had any clobbers to add, make a new pattern than contains
9796 them. Then check to make sure that all of them are dead. */
9797 if (num_clobbers_to_add)
9799 rtx newpat = gen_rtx_PARALLEL (VOIDmode,
9800 rtvec_alloc (GET_CODE (pat) == PARALLEL
9801 ? (XVECLEN (pat, 0)
9802 + num_clobbers_to_add)
9803 : num_clobbers_to_add + 1));
9805 if (GET_CODE (pat) == PARALLEL)
9806 for (i = 0; i < XVECLEN (pat, 0); i++)
9807 XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
9808 else
9809 XVECEXP (newpat, 0, 0) = pat;
9811 add_clobbers (newpat, insn_code_number);
9813 for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
9814 i < XVECLEN (newpat, 0); i++)
9816 if (REG_P (XEXP (XVECEXP (newpat, 0, i), 0))
9817 && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
9818 return -1;
9819 if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) != SCRATCH)
9821 gcc_assert (REG_P (XEXP (XVECEXP (newpat, 0, i), 0)));
9822 notes = gen_rtx_EXPR_LIST (REG_UNUSED,
9823 XEXP (XVECEXP (newpat, 0, i), 0), notes);
9826 pat = newpat;
9829 *pnewpat = pat;
9830 *pnotes = notes;
9832 return insn_code_number;
9835 /* Like gen_lowpart_general but for use by combine. In combine it
9836 is not possible to create any new pseudoregs. However, it is
9837 safe to create invalid memory addresses, because combine will
9838 try to recognize them and all they will do is make the combine
9839 attempt fail.
9841 If for some reason this cannot do its job, an rtx
9842 (clobber (const_int 0)) is returned.
9843 An insn containing that will not be recognized. */
9845 static rtx
9846 gen_lowpart_for_combine (enum machine_mode omode, rtx x)
9848 enum machine_mode imode = GET_MODE (x);
9849 unsigned int osize = GET_MODE_SIZE (omode);
9850 unsigned int isize = GET_MODE_SIZE (imode);
9851 rtx result;
9853 if (omode == imode)
9854 return x;
9856 /* Return identity if this is a CONST or symbolic reference. */
9857 if (omode == Pmode
9858 && (GET_CODE (x) == CONST
9859 || GET_CODE (x) == SYMBOL_REF
9860 || GET_CODE (x) == LABEL_REF))
9861 return x;
9863 /* We can only support MODE being wider than a word if X is a
9864 constant integer or has a mode the same size. */
9865 if (GET_MODE_SIZE (omode) > UNITS_PER_WORD
9866 && ! ((imode == VOIDmode
9867 && (GET_CODE (x) == CONST_INT
9868 || GET_CODE (x) == CONST_DOUBLE))
9869 || isize == osize))
9870 goto fail;
9872 /* X might be a paradoxical (subreg (mem)). In that case, gen_lowpart
9873 won't know what to do. So we will strip off the SUBREG here and
9874 process normally. */
9875 if (GET_CODE (x) == SUBREG && MEM_P (SUBREG_REG (x)))
9877 x = SUBREG_REG (x);
9879 /* For use in case we fall down into the address adjustments
9880 further below, we need to adjust the known mode and size of
9881 x; imode and isize, since we just adjusted x. */
9882 imode = GET_MODE (x);
9884 if (imode == omode)
9885 return x;
9887 isize = GET_MODE_SIZE (imode);
9890 result = gen_lowpart_common (omode, x);
9892 if (result)
9893 return result;
9895 if (MEM_P (x))
9897 int offset = 0;
9899 /* Refuse to work on a volatile memory ref or one with a mode-dependent
9900 address. */
9901 if (MEM_VOLATILE_P (x) || mode_dependent_address_p (XEXP (x, 0)))
9902 goto fail;
9904 /* If we want to refer to something bigger than the original memref,
9905 generate a paradoxical subreg instead. That will force a reload
9906 of the original memref X. */
9907 if (isize < osize)
9908 return gen_rtx_SUBREG (omode, x, 0);
9910 if (WORDS_BIG_ENDIAN)
9911 offset = MAX (isize, UNITS_PER_WORD) - MAX (osize, UNITS_PER_WORD);
9913 /* Adjust the address so that the address-after-the-data is
9914 unchanged. */
9915 if (BYTES_BIG_ENDIAN)
9916 offset -= MIN (UNITS_PER_WORD, osize) - MIN (UNITS_PER_WORD, isize);
9918 return adjust_address_nv (x, omode, offset);
9921 /* If X is a comparison operator, rewrite it in a new mode. This
9922 probably won't match, but may allow further simplifications. */
9923 else if (COMPARISON_P (x))
9924 return gen_rtx_fmt_ee (GET_CODE (x), omode, XEXP (x, 0), XEXP (x, 1));
9926 /* If we couldn't simplify X any other way, just enclose it in a
9927 SUBREG. Normally, this SUBREG won't match, but some patterns may
9928 include an explicit SUBREG or we may simplify it further in combine. */
9929 else
9931 int offset = 0;
9932 rtx res;
9934 offset = subreg_lowpart_offset (omode, imode);
9935 if (imode == VOIDmode)
9937 imode = int_mode_for_mode (omode);
9938 x = gen_lowpart_common (imode, x);
9939 if (x == NULL)
9940 goto fail;
9942 res = simplify_gen_subreg (omode, x, imode, offset);
9943 if (res)
9944 return res;
9947 fail:
9948 return gen_rtx_CLOBBER (imode, const0_rtx);
9951 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
9952 comparison code that will be tested.
9954 The result is a possibly different comparison code to use. *POP0 and
9955 *POP1 may be updated.
9957 It is possible that we might detect that a comparison is either always
9958 true or always false. However, we do not perform general constant
9959 folding in combine, so this knowledge isn't useful. Such tautologies
9960 should have been detected earlier. Hence we ignore all such cases. */
9962 static enum rtx_code
9963 simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
9965 rtx op0 = *pop0;
9966 rtx op1 = *pop1;
9967 rtx tem, tem1;
9968 int i;
9969 enum machine_mode mode, tmode;
9971 /* Try a few ways of applying the same transformation to both operands. */
9972 while (1)
9974 #ifndef WORD_REGISTER_OPERATIONS
9975 /* The test below this one won't handle SIGN_EXTENDs on these machines,
9976 so check specially. */
9977 if (code != GTU && code != GEU && code != LTU && code != LEU
9978 && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
9979 && GET_CODE (XEXP (op0, 0)) == ASHIFT
9980 && GET_CODE (XEXP (op1, 0)) == ASHIFT
9981 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
9982 && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
9983 && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
9984 == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
9985 && GET_CODE (XEXP (op0, 1)) == CONST_INT
9986 && XEXP (op0, 1) == XEXP (op1, 1)
9987 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
9988 && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
9989 && (INTVAL (XEXP (op0, 1))
9990 == (GET_MODE_BITSIZE (GET_MODE (op0))
9991 - (GET_MODE_BITSIZE
9992 (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
9994 op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
9995 op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
9997 #endif
9999 /* If both operands are the same constant shift, see if we can ignore the
10000 shift. We can if the shift is a rotate or if the bits shifted out of
10001 this shift are known to be zero for both inputs and if the type of
10002 comparison is compatible with the shift. */
10003 if (GET_CODE (op0) == GET_CODE (op1)
10004 && GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
10005 && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
10006 || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
10007 && (code != GT && code != LT && code != GE && code != LE))
10008 || (GET_CODE (op0) == ASHIFTRT
10009 && (code != GTU && code != LTU
10010 && code != GEU && code != LEU)))
10011 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10012 && INTVAL (XEXP (op0, 1)) >= 0
10013 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
10014 && XEXP (op0, 1) == XEXP (op1, 1))
10016 enum machine_mode mode = GET_MODE (op0);
10017 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
10018 int shift_count = INTVAL (XEXP (op0, 1));
10020 if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
10021 mask &= (mask >> shift_count) << shift_count;
10022 else if (GET_CODE (op0) == ASHIFT)
10023 mask = (mask & (mask << shift_count)) >> shift_count;
10025 if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
10026 && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
10027 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
10028 else
10029 break;
10032 /* If both operands are AND's of a paradoxical SUBREG by constant, the
10033 SUBREGs are of the same mode, and, in both cases, the AND would
10034 be redundant if the comparison was done in the narrower mode,
10035 do the comparison in the narrower mode (e.g., we are AND'ing with 1
10036 and the operand's possibly nonzero bits are 0xffffff01; in that case
10037 if we only care about QImode, we don't need the AND). This case
10038 occurs if the output mode of an scc insn is not SImode and
10039 STORE_FLAG_VALUE == 1 (e.g., the 386).
10041 Similarly, check for a case where the AND's are ZERO_EXTEND
10042 operations from some narrower mode even though a SUBREG is not
10043 present. */
10045 else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
10046 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10047 && GET_CODE (XEXP (op1, 1)) == CONST_INT)
10049 rtx inner_op0 = XEXP (op0, 0);
10050 rtx inner_op1 = XEXP (op1, 0);
10051 HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
10052 HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
10053 int changed = 0;
10055 if (GET_CODE (inner_op0) == SUBREG && GET_CODE (inner_op1) == SUBREG
10056 && (GET_MODE_SIZE (GET_MODE (inner_op0))
10057 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (inner_op0))))
10058 && (GET_MODE (SUBREG_REG (inner_op0))
10059 == GET_MODE (SUBREG_REG (inner_op1)))
10060 && (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (inner_op0)))
10061 <= HOST_BITS_PER_WIDE_INT)
10062 && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
10063 GET_MODE (SUBREG_REG (inner_op0)))))
10064 && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
10065 GET_MODE (SUBREG_REG (inner_op1))))))
10067 op0 = SUBREG_REG (inner_op0);
10068 op1 = SUBREG_REG (inner_op1);
10070 /* The resulting comparison is always unsigned since we masked
10071 off the original sign bit. */
10072 code = unsigned_condition (code);
10074 changed = 1;
10077 else if (c0 == c1)
10078 for (tmode = GET_CLASS_NARROWEST_MODE
10079 (GET_MODE_CLASS (GET_MODE (op0)));
10080 tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
10081 if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
10083 op0 = gen_lowpart (tmode, inner_op0);
10084 op1 = gen_lowpart (tmode, inner_op1);
10085 code = unsigned_condition (code);
10086 changed = 1;
10087 break;
10090 if (! changed)
10091 break;
10094 /* If both operands are NOT, we can strip off the outer operation
10095 and adjust the comparison code for swapped operands; similarly for
10096 NEG, except that this must be an equality comparison. */
10097 else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
10098 || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
10099 && (code == EQ || code == NE)))
10100 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
10102 else
10103 break;
10106 /* If the first operand is a constant, swap the operands and adjust the
10107 comparison code appropriately, but don't do this if the second operand
10108 is already a constant integer. */
10109 if (swap_commutative_operands_p (op0, op1))
10111 tem = op0, op0 = op1, op1 = tem;
10112 code = swap_condition (code);
10115 /* We now enter a loop during which we will try to simplify the comparison.
10116 For the most part, we only are concerned with comparisons with zero,
10117 but some things may really be comparisons with zero but not start
10118 out looking that way. */
10120 while (GET_CODE (op1) == CONST_INT)
10122 enum machine_mode mode = GET_MODE (op0);
10123 unsigned int mode_width = GET_MODE_BITSIZE (mode);
10124 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
10125 int equality_comparison_p;
10126 int sign_bit_comparison_p;
10127 int unsigned_comparison_p;
10128 HOST_WIDE_INT const_op;
10130 /* We only want to handle integral modes. This catches VOIDmode,
10131 CCmode, and the floating-point modes. An exception is that we
10132 can handle VOIDmode if OP0 is a COMPARE or a comparison
10133 operation. */
10135 if (GET_MODE_CLASS (mode) != MODE_INT
10136 && ! (mode == VOIDmode
10137 && (GET_CODE (op0) == COMPARE || COMPARISON_P (op0))))
10138 break;
10140 /* Get the constant we are comparing against and turn off all bits
10141 not on in our mode. */
10142 const_op = INTVAL (op1);
10143 if (mode != VOIDmode)
10144 const_op = trunc_int_for_mode (const_op, mode);
10145 op1 = GEN_INT (const_op);
10147 /* If we are comparing against a constant power of two and the value
10148 being compared can only have that single bit nonzero (e.g., it was
10149 `and'ed with that bit), we can replace this with a comparison
10150 with zero. */
10151 if (const_op
10152 && (code == EQ || code == NE || code == GE || code == GEU
10153 || code == LT || code == LTU)
10154 && mode_width <= HOST_BITS_PER_WIDE_INT
10155 && exact_log2 (const_op) >= 0
10156 && nonzero_bits (op0, mode) == (unsigned HOST_WIDE_INT) const_op)
10158 code = (code == EQ || code == GE || code == GEU ? NE : EQ);
10159 op1 = const0_rtx, const_op = 0;
10162 /* Similarly, if we are comparing a value known to be either -1 or
10163 0 with -1, change it to the opposite comparison against zero. */
10165 if (const_op == -1
10166 && (code == EQ || code == NE || code == GT || code == LE
10167 || code == GEU || code == LTU)
10168 && num_sign_bit_copies (op0, mode) == mode_width)
10170 code = (code == EQ || code == LE || code == GEU ? NE : EQ);
10171 op1 = const0_rtx, const_op = 0;
10174 /* Do some canonicalizations based on the comparison code. We prefer
10175 comparisons against zero and then prefer equality comparisons.
10176 If we can reduce the size of a constant, we will do that too. */
10178 switch (code)
10180 case LT:
10181 /* < C is equivalent to <= (C - 1) */
10182 if (const_op > 0)
10184 const_op -= 1;
10185 op1 = GEN_INT (const_op);
10186 code = LE;
10187 /* ... fall through to LE case below. */
10189 else
10190 break;
10192 case LE:
10193 /* <= C is equivalent to < (C + 1); we do this for C < 0 */
10194 if (const_op < 0)
10196 const_op += 1;
10197 op1 = GEN_INT (const_op);
10198 code = LT;
10201 /* If we are doing a <= 0 comparison on a value known to have
10202 a zero sign bit, we can replace this with == 0. */
10203 else if (const_op == 0
10204 && mode_width <= HOST_BITS_PER_WIDE_INT
10205 && (nonzero_bits (op0, mode)
10206 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
10207 code = EQ;
10208 break;
10210 case GE:
10211 /* >= C is equivalent to > (C - 1). */
10212 if (const_op > 0)
10214 const_op -= 1;
10215 op1 = GEN_INT (const_op);
10216 code = GT;
10217 /* ... fall through to GT below. */
10219 else
10220 break;
10222 case GT:
10223 /* > C is equivalent to >= (C + 1); we do this for C < 0. */
10224 if (const_op < 0)
10226 const_op += 1;
10227 op1 = GEN_INT (const_op);
10228 code = GE;
10231 /* If we are doing a > 0 comparison on a value known to have
10232 a zero sign bit, we can replace this with != 0. */
10233 else if (const_op == 0
10234 && mode_width <= HOST_BITS_PER_WIDE_INT
10235 && (nonzero_bits (op0, mode)
10236 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)
10237 code = NE;
10238 break;
10240 case LTU:
10241 /* < C is equivalent to <= (C - 1). */
10242 if (const_op > 0)
10244 const_op -= 1;
10245 op1 = GEN_INT (const_op);
10246 code = LEU;
10247 /* ... fall through ... */
10250 /* (unsigned) < 0x80000000 is equivalent to >= 0. */
10251 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10252 && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
10254 const_op = 0, op1 = const0_rtx;
10255 code = GE;
10256 break;
10258 else
10259 break;
10261 case LEU:
10262 /* unsigned <= 0 is equivalent to == 0 */
10263 if (const_op == 0)
10264 code = EQ;
10266 /* (unsigned) <= 0x7fffffff is equivalent to >= 0. */
10267 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10268 && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
10270 const_op = 0, op1 = const0_rtx;
10271 code = GE;
10273 break;
10275 case GEU:
10276 /* >= C is equivalent to > (C - 1). */
10277 if (const_op > 1)
10279 const_op -= 1;
10280 op1 = GEN_INT (const_op);
10281 code = GTU;
10282 /* ... fall through ... */
10285 /* (unsigned) >= 0x80000000 is equivalent to < 0. */
10286 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10287 && (const_op == (HOST_WIDE_INT) 1 << (mode_width - 1)))
10289 const_op = 0, op1 = const0_rtx;
10290 code = LT;
10291 break;
10293 else
10294 break;
10296 case GTU:
10297 /* unsigned > 0 is equivalent to != 0 */
10298 if (const_op == 0)
10299 code = NE;
10301 /* (unsigned) > 0x7fffffff is equivalent to < 0. */
10302 else if ((mode_width <= HOST_BITS_PER_WIDE_INT)
10303 && (const_op == ((HOST_WIDE_INT) 1 << (mode_width - 1)) - 1))
10305 const_op = 0, op1 = const0_rtx;
10306 code = LT;
10308 break;
10310 default:
10311 break;
10314 /* Compute some predicates to simplify code below. */
10316 equality_comparison_p = (code == EQ || code == NE);
10317 sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
10318 unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
10319 || code == GEU);
10321 /* If this is a sign bit comparison and we can do arithmetic in
10322 MODE, say that we will only be needing the sign bit of OP0. */
10323 if (sign_bit_comparison_p
10324 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10325 op0 = force_to_mode (op0, mode,
10326 ((HOST_WIDE_INT) 1
10327 << (GET_MODE_BITSIZE (mode) - 1)),
10330 /* Now try cases based on the opcode of OP0. If none of the cases
10331 does a "continue", we exit this loop immediately after the
10332 switch. */
10334 switch (GET_CODE (op0))
10336 case ZERO_EXTRACT:
10337 /* If we are extracting a single bit from a variable position in
10338 a constant that has only a single bit set and are comparing it
10339 with zero, we can convert this into an equality comparison
10340 between the position and the location of the single bit. */
10341 /* Except we can't if SHIFT_COUNT_TRUNCATED is set, since we might
10342 have already reduced the shift count modulo the word size. */
10343 if (!SHIFT_COUNT_TRUNCATED
10344 && GET_CODE (XEXP (op0, 0)) == CONST_INT
10345 && XEXP (op0, 1) == const1_rtx
10346 && equality_comparison_p && const_op == 0
10347 && (i = exact_log2 (INTVAL (XEXP (op0, 0)))) >= 0)
10349 if (BITS_BIG_ENDIAN)
10351 enum machine_mode new_mode
10352 = mode_for_extraction (EP_extzv, 1);
10353 if (new_mode == MAX_MACHINE_MODE)
10354 i = BITS_PER_WORD - 1 - i;
10355 else
10357 mode = new_mode;
10358 i = (GET_MODE_BITSIZE (mode) - 1 - i);
10362 op0 = XEXP (op0, 2);
10363 op1 = GEN_INT (i);
10364 const_op = i;
10366 /* Result is nonzero iff shift count is equal to I. */
10367 code = reverse_condition (code);
10368 continue;
10371 /* ... fall through ... */
10373 case SIGN_EXTRACT:
10374 tem = expand_compound_operation (op0);
10375 if (tem != op0)
10377 op0 = tem;
10378 continue;
10380 break;
10382 case NOT:
10383 /* If testing for equality, we can take the NOT of the constant. */
10384 if (equality_comparison_p
10385 && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
10387 op0 = XEXP (op0, 0);
10388 op1 = tem;
10389 continue;
10392 /* If just looking at the sign bit, reverse the sense of the
10393 comparison. */
10394 if (sign_bit_comparison_p)
10396 op0 = XEXP (op0, 0);
10397 code = (code == GE ? LT : GE);
10398 continue;
10400 break;
10402 case NEG:
10403 /* If testing for equality, we can take the NEG of the constant. */
10404 if (equality_comparison_p
10405 && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
10407 op0 = XEXP (op0, 0);
10408 op1 = tem;
10409 continue;
10412 /* The remaining cases only apply to comparisons with zero. */
10413 if (const_op != 0)
10414 break;
10416 /* When X is ABS or is known positive,
10417 (neg X) is < 0 if and only if X != 0. */
10419 if (sign_bit_comparison_p
10420 && (GET_CODE (XEXP (op0, 0)) == ABS
10421 || (mode_width <= HOST_BITS_PER_WIDE_INT
10422 && (nonzero_bits (XEXP (op0, 0), mode)
10423 & ((HOST_WIDE_INT) 1 << (mode_width - 1))) == 0)))
10425 op0 = XEXP (op0, 0);
10426 code = (code == LT ? NE : EQ);
10427 continue;
10430 /* If we have NEG of something whose two high-order bits are the
10431 same, we know that "(-a) < 0" is equivalent to "a > 0". */
10432 if (num_sign_bit_copies (op0, mode) >= 2)
10434 op0 = XEXP (op0, 0);
10435 code = swap_condition (code);
10436 continue;
10438 break;
10440 case ROTATE:
10441 /* If we are testing equality and our count is a constant, we
10442 can perform the inverse operation on our RHS. */
10443 if (equality_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
10444 && (tem = simplify_binary_operation (ROTATERT, mode,
10445 op1, XEXP (op0, 1))) != 0)
10447 op0 = XEXP (op0, 0);
10448 op1 = tem;
10449 continue;
10452 /* If we are doing a < 0 or >= 0 comparison, it means we are testing
10453 a particular bit. Convert it to an AND of a constant of that
10454 bit. This will be converted into a ZERO_EXTRACT. */
10455 if (const_op == 0 && sign_bit_comparison_p
10456 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10457 && mode_width <= HOST_BITS_PER_WIDE_INT)
10459 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10460 ((HOST_WIDE_INT) 1
10461 << (mode_width - 1
10462 - INTVAL (XEXP (op0, 1)))));
10463 code = (code == LT ? NE : EQ);
10464 continue;
10467 /* Fall through. */
10469 case ABS:
10470 /* ABS is ignorable inside an equality comparison with zero. */
10471 if (const_op == 0 && equality_comparison_p)
10473 op0 = XEXP (op0, 0);
10474 continue;
10476 break;
10478 case SIGN_EXTEND:
10479 /* Can simplify (compare (zero/sign_extend FOO) CONST) to
10480 (compare FOO CONST) if CONST fits in FOO's mode and we
10481 are either testing inequality or have an unsigned
10482 comparison with ZERO_EXTEND or a signed comparison with
10483 SIGN_EXTEND. But don't do it if we don't have a compare
10484 insn of the given mode, since we'd have to revert it
10485 later on, and then we wouldn't know whether to sign- or
10486 zero-extend. */
10487 mode = GET_MODE (XEXP (op0, 0));
10488 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
10489 && ! unsigned_comparison_p
10490 && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10491 && ((unsigned HOST_WIDE_INT) const_op
10492 < (((unsigned HOST_WIDE_INT) 1
10493 << (GET_MODE_BITSIZE (mode) - 1))))
10494 && optab_handler (cmp_optab, mode)->insn_code != CODE_FOR_nothing)
10496 op0 = XEXP (op0, 0);
10497 continue;
10499 break;
10501 case SUBREG:
10502 /* Check for the case where we are comparing A - C1 with C2, that is
10504 (subreg:MODE (plus (A) (-C1))) op (C2)
10506 with C1 a constant, and try to lift the SUBREG, i.e. to do the
10507 comparison in the wider mode. One of the following two conditions
10508 must be true in order for this to be valid:
10510 1. The mode extension results in the same bit pattern being added
10511 on both sides and the comparison is equality or unsigned. As
10512 C2 has been truncated to fit in MODE, the pattern can only be
10513 all 0s or all 1s.
10515 2. The mode extension results in the sign bit being copied on
10516 each side.
10518 The difficulty here is that we have predicates for A but not for
10519 (A - C1) so we need to check that C1 is within proper bounds so
10520 as to perturbate A as little as possible. */
10522 if (mode_width <= HOST_BITS_PER_WIDE_INT
10523 && subreg_lowpart_p (op0)
10524 && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) > mode_width
10525 && GET_CODE (SUBREG_REG (op0)) == PLUS
10526 && GET_CODE (XEXP (SUBREG_REG (op0), 1)) == CONST_INT)
10528 enum machine_mode inner_mode = GET_MODE (SUBREG_REG (op0));
10529 rtx a = XEXP (SUBREG_REG (op0), 0);
10530 HOST_WIDE_INT c1 = -INTVAL (XEXP (SUBREG_REG (op0), 1));
10532 if ((c1 > 0
10533 && (unsigned HOST_WIDE_INT) c1
10534 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)
10535 && (equality_comparison_p || unsigned_comparison_p)
10536 /* (A - C1) zero-extends if it is positive and sign-extends
10537 if it is negative, C2 both zero- and sign-extends. */
10538 && ((0 == (nonzero_bits (a, inner_mode)
10539 & ~GET_MODE_MASK (mode))
10540 && const_op >= 0)
10541 /* (A - C1) sign-extends if it is positive and 1-extends
10542 if it is negative, C2 both sign- and 1-extends. */
10543 || (num_sign_bit_copies (a, inner_mode)
10544 > (unsigned int) (GET_MODE_BITSIZE (inner_mode)
10545 - mode_width)
10546 && const_op < 0)))
10547 || ((unsigned HOST_WIDE_INT) c1
10548 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 2)
10549 /* (A - C1) always sign-extends, like C2. */
10550 && num_sign_bit_copies (a, inner_mode)
10551 > (unsigned int) (GET_MODE_BITSIZE (inner_mode)
10552 - (mode_width - 1))))
10554 op0 = SUBREG_REG (op0);
10555 continue;
10559 /* If the inner mode is narrower and we are extracting the low part,
10560 we can treat the SUBREG as if it were a ZERO_EXTEND. */
10561 if (subreg_lowpart_p (op0)
10562 && GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0))) < mode_width)
10563 /* Fall through */ ;
10564 else
10565 break;
10567 /* ... fall through ... */
10569 case ZERO_EXTEND:
10570 mode = GET_MODE (XEXP (op0, 0));
10571 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
10572 && (unsigned_comparison_p || equality_comparison_p)
10573 && (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
10574 && ((unsigned HOST_WIDE_INT) const_op < GET_MODE_MASK (mode))
10575 && optab_handler (cmp_optab, mode)->insn_code != CODE_FOR_nothing)
10577 op0 = XEXP (op0, 0);
10578 continue;
10580 break;
10582 case PLUS:
10583 /* (eq (plus X A) B) -> (eq X (minus B A)). We can only do
10584 this for equality comparisons due to pathological cases involving
10585 overflows. */
10586 if (equality_comparison_p
10587 && 0 != (tem = simplify_binary_operation (MINUS, mode,
10588 op1, XEXP (op0, 1))))
10590 op0 = XEXP (op0, 0);
10591 op1 = tem;
10592 continue;
10595 /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0. */
10596 if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
10597 && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
10599 op0 = XEXP (XEXP (op0, 0), 0);
10600 code = (code == LT ? EQ : NE);
10601 continue;
10603 break;
10605 case MINUS:
10606 /* We used to optimize signed comparisons against zero, but that
10607 was incorrect. Unsigned comparisons against zero (GTU, LEU)
10608 arrive here as equality comparisons, or (GEU, LTU) are
10609 optimized away. No need to special-case them. */
10611 /* (eq (minus A B) C) -> (eq A (plus B C)) or
10612 (eq B (minus A C)), whichever simplifies. We can only do
10613 this for equality comparisons due to pathological cases involving
10614 overflows. */
10615 if (equality_comparison_p
10616 && 0 != (tem = simplify_binary_operation (PLUS, mode,
10617 XEXP (op0, 1), op1)))
10619 op0 = XEXP (op0, 0);
10620 op1 = tem;
10621 continue;
10624 if (equality_comparison_p
10625 && 0 != (tem = simplify_binary_operation (MINUS, mode,
10626 XEXP (op0, 0), op1)))
10628 op0 = XEXP (op0, 1);
10629 op1 = tem;
10630 continue;
10633 /* The sign bit of (minus (ashiftrt X C) X), where C is the number
10634 of bits in X minus 1, is one iff X > 0. */
10635 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
10636 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10637 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (XEXP (op0, 0), 1))
10638 == mode_width - 1
10639 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10641 op0 = XEXP (op0, 1);
10642 code = (code == GE ? LE : GT);
10643 continue;
10645 break;
10647 case XOR:
10648 /* (eq (xor A B) C) -> (eq A (xor B C)). This is a simplification
10649 if C is zero or B is a constant. */
10650 if (equality_comparison_p
10651 && 0 != (tem = simplify_binary_operation (XOR, mode,
10652 XEXP (op0, 1), op1)))
10654 op0 = XEXP (op0, 0);
10655 op1 = tem;
10656 continue;
10658 break;
10660 case EQ: case NE:
10661 case UNEQ: case LTGT:
10662 case LT: case LTU: case UNLT: case LE: case LEU: case UNLE:
10663 case GT: case GTU: case UNGT: case GE: case GEU: case UNGE:
10664 case UNORDERED: case ORDERED:
10665 /* We can't do anything if OP0 is a condition code value, rather
10666 than an actual data value. */
10667 if (const_op != 0
10668 || CC0_P (XEXP (op0, 0))
10669 || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
10670 break;
10672 /* Get the two operands being compared. */
10673 if (GET_CODE (XEXP (op0, 0)) == COMPARE)
10674 tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
10675 else
10676 tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
10678 /* Check for the cases where we simply want the result of the
10679 earlier test or the opposite of that result. */
10680 if (code == NE || code == EQ
10681 || (GET_MODE_BITSIZE (GET_MODE (op0)) <= HOST_BITS_PER_WIDE_INT
10682 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10683 && (STORE_FLAG_VALUE
10684 & (((HOST_WIDE_INT) 1
10685 << (GET_MODE_BITSIZE (GET_MODE (op0)) - 1))))
10686 && (code == LT || code == GE)))
10688 enum rtx_code new_code;
10689 if (code == LT || code == NE)
10690 new_code = GET_CODE (op0);
10691 else
10692 new_code = reversed_comparison_code (op0, NULL);
10694 if (new_code != UNKNOWN)
10696 code = new_code;
10697 op0 = tem;
10698 op1 = tem1;
10699 continue;
10702 break;
10704 case IOR:
10705 /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
10706 iff X <= 0. */
10707 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
10708 && XEXP (XEXP (op0, 0), 1) == constm1_rtx
10709 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
10711 op0 = XEXP (op0, 1);
10712 code = (code == GE ? GT : LE);
10713 continue;
10715 break;
10717 case AND:
10718 /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1). This
10719 will be converted to a ZERO_EXTRACT later. */
10720 if (const_op == 0 && equality_comparison_p
10721 && GET_CODE (XEXP (op0, 0)) == ASHIFT
10722 && XEXP (XEXP (op0, 0), 0) == const1_rtx)
10724 op0 = simplify_and_const_int
10725 (NULL_RTX, mode, gen_rtx_LSHIFTRT (mode,
10726 XEXP (op0, 1),
10727 XEXP (XEXP (op0, 0), 1)),
10728 (HOST_WIDE_INT) 1);
10729 continue;
10732 /* If we are comparing (and (lshiftrt X C1) C2) for equality with
10733 zero and X is a comparison and C1 and C2 describe only bits set
10734 in STORE_FLAG_VALUE, we can compare with X. */
10735 if (const_op == 0 && equality_comparison_p
10736 && mode_width <= HOST_BITS_PER_WIDE_INT
10737 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10738 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
10739 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10740 && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
10741 && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
10743 mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
10744 << INTVAL (XEXP (XEXP (op0, 0), 1)));
10745 if ((~STORE_FLAG_VALUE & mask) == 0
10746 && (COMPARISON_P (XEXP (XEXP (op0, 0), 0))
10747 || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
10748 && COMPARISON_P (tem))))
10750 op0 = XEXP (XEXP (op0, 0), 0);
10751 continue;
10755 /* If we are doing an equality comparison of an AND of a bit equal
10756 to the sign bit, replace this with a LT or GE comparison of
10757 the underlying value. */
10758 if (equality_comparison_p
10759 && const_op == 0
10760 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10761 && mode_width <= HOST_BITS_PER_WIDE_INT
10762 && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
10763 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
10765 op0 = XEXP (op0, 0);
10766 code = (code == EQ ? GE : LT);
10767 continue;
10770 /* If this AND operation is really a ZERO_EXTEND from a narrower
10771 mode, the constant fits within that mode, and this is either an
10772 equality or unsigned comparison, try to do this comparison in
10773 the narrower mode.
10775 Note that in:
10777 (ne:DI (and:DI (reg:DI 4) (const_int 0xffffffff)) (const_int 0))
10778 -> (ne:DI (reg:SI 4) (const_int 0))
10780 unless TRULY_NOOP_TRUNCATION allows it or the register is
10781 known to hold a value of the required mode the
10782 transformation is invalid. */
10783 if ((equality_comparison_p || unsigned_comparison_p)
10784 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10785 && (i = exact_log2 ((INTVAL (XEXP (op0, 1))
10786 & GET_MODE_MASK (mode))
10787 + 1)) >= 0
10788 && const_op >> i == 0
10789 && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode
10790 && (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (tmode),
10791 GET_MODE_BITSIZE (GET_MODE (op0)))
10792 || (REG_P (XEXP (op0, 0))
10793 && reg_truncated_to_mode (tmode, XEXP (op0, 0)))))
10795 op0 = gen_lowpart (tmode, XEXP (op0, 0));
10796 continue;
10799 /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1
10800 fits in both M1 and M2 and the SUBREG is either paradoxical
10801 or represents the low part, permute the SUBREG and the AND
10802 and try again. */
10803 if (GET_CODE (XEXP (op0, 0)) == SUBREG)
10805 unsigned HOST_WIDE_INT c1;
10806 tmode = GET_MODE (SUBREG_REG (XEXP (op0, 0)));
10807 /* Require an integral mode, to avoid creating something like
10808 (AND:SF ...). */
10809 if (SCALAR_INT_MODE_P (tmode)
10810 /* It is unsafe to commute the AND into the SUBREG if the
10811 SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
10812 not defined. As originally written the upper bits
10813 have a defined value due to the AND operation.
10814 However, if we commute the AND inside the SUBREG then
10815 they no longer have defined values and the meaning of
10816 the code has been changed. */
10817 && (0
10818 #ifdef WORD_REGISTER_OPERATIONS
10819 || (mode_width > GET_MODE_BITSIZE (tmode)
10820 && mode_width <= BITS_PER_WORD)
10821 #endif
10822 || (mode_width <= GET_MODE_BITSIZE (tmode)
10823 && subreg_lowpart_p (XEXP (op0, 0))))
10824 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10825 && mode_width <= HOST_BITS_PER_WIDE_INT
10826 && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT
10827 && ((c1 = INTVAL (XEXP (op0, 1))) & ~mask) == 0
10828 && (c1 & ~GET_MODE_MASK (tmode)) == 0
10829 && c1 != mask
10830 && c1 != GET_MODE_MASK (tmode))
10832 op0 = simplify_gen_binary (AND, tmode,
10833 SUBREG_REG (XEXP (op0, 0)),
10834 gen_int_mode (c1, tmode));
10835 op0 = gen_lowpart (mode, op0);
10836 continue;
10840 /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0). */
10841 if (const_op == 0 && equality_comparison_p
10842 && XEXP (op0, 1) == const1_rtx
10843 && GET_CODE (XEXP (op0, 0)) == NOT)
10845 op0 = simplify_and_const_int
10846 (NULL_RTX, mode, XEXP (XEXP (op0, 0), 0), (HOST_WIDE_INT) 1);
10847 code = (code == NE ? EQ : NE);
10848 continue;
10851 /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
10852 (eq (and (lshiftrt X) 1) 0).
10853 Also handle the case where (not X) is expressed using xor. */
10854 if (const_op == 0 && equality_comparison_p
10855 && XEXP (op0, 1) == const1_rtx
10856 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT)
10858 rtx shift_op = XEXP (XEXP (op0, 0), 0);
10859 rtx shift_count = XEXP (XEXP (op0, 0), 1);
10861 if (GET_CODE (shift_op) == NOT
10862 || (GET_CODE (shift_op) == XOR
10863 && GET_CODE (XEXP (shift_op, 1)) == CONST_INT
10864 && GET_CODE (shift_count) == CONST_INT
10865 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT
10866 && (INTVAL (XEXP (shift_op, 1))
10867 == (HOST_WIDE_INT) 1 << INTVAL (shift_count))))
10869 op0 = simplify_and_const_int
10870 (NULL_RTX, mode,
10871 gen_rtx_LSHIFTRT (mode, XEXP (shift_op, 0), shift_count),
10872 (HOST_WIDE_INT) 1);
10873 code = (code == NE ? EQ : NE);
10874 continue;
10877 break;
10879 case ASHIFT:
10880 /* If we have (compare (ashift FOO N) (const_int C)) and
10881 the high order N bits of FOO (N+1 if an inequality comparison)
10882 are known to be zero, we can do this by comparing FOO with C
10883 shifted right N bits so long as the low-order N bits of C are
10884 zero. */
10885 if (GET_CODE (XEXP (op0, 1)) == CONST_INT
10886 && INTVAL (XEXP (op0, 1)) >= 0
10887 && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
10888 < HOST_BITS_PER_WIDE_INT)
10889 && ((const_op
10890 & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0)
10891 && mode_width <= HOST_BITS_PER_WIDE_INT
10892 && (nonzero_bits (XEXP (op0, 0), mode)
10893 & ~(mask >> (INTVAL (XEXP (op0, 1))
10894 + ! equality_comparison_p))) == 0)
10896 /* We must perform a logical shift, not an arithmetic one,
10897 as we want the top N bits of C to be zero. */
10898 unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
10900 temp >>= INTVAL (XEXP (op0, 1));
10901 op1 = gen_int_mode (temp, mode);
10902 op0 = XEXP (op0, 0);
10903 continue;
10906 /* If we are doing a sign bit comparison, it means we are testing
10907 a particular bit. Convert it to the appropriate AND. */
10908 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 1)) == CONST_INT
10909 && mode_width <= HOST_BITS_PER_WIDE_INT)
10911 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10912 ((HOST_WIDE_INT) 1
10913 << (mode_width - 1
10914 - INTVAL (XEXP (op0, 1)))));
10915 code = (code == LT ? NE : EQ);
10916 continue;
10919 /* If this an equality comparison with zero and we are shifting
10920 the low bit to the sign bit, we can convert this to an AND of the
10921 low-order bit. */
10922 if (const_op == 0 && equality_comparison_p
10923 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10924 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1))
10925 == mode_width - 1)
10927 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
10928 (HOST_WIDE_INT) 1);
10929 continue;
10931 break;
10933 case ASHIFTRT:
10934 /* If this is an equality comparison with zero, we can do this
10935 as a logical shift, which might be much simpler. */
10936 if (equality_comparison_p && const_op == 0
10937 && GET_CODE (XEXP (op0, 1)) == CONST_INT)
10939 op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
10940 XEXP (op0, 0),
10941 INTVAL (XEXP (op0, 1)));
10942 continue;
10945 /* If OP0 is a sign extension and CODE is not an unsigned comparison,
10946 do the comparison in a narrower mode. */
10947 if (! unsigned_comparison_p
10948 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10949 && GET_CODE (XEXP (op0, 0)) == ASHIFT
10950 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
10951 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
10952 MODE_INT, 1)) != BLKmode
10953 && (((unsigned HOST_WIDE_INT) const_op
10954 + (GET_MODE_MASK (tmode) >> 1) + 1)
10955 <= GET_MODE_MASK (tmode)))
10957 op0 = gen_lowpart (tmode, XEXP (XEXP (op0, 0), 0));
10958 continue;
10961 /* Likewise if OP0 is a PLUS of a sign extension with a
10962 constant, which is usually represented with the PLUS
10963 between the shifts. */
10964 if (! unsigned_comparison_p
10965 && GET_CODE (XEXP (op0, 1)) == CONST_INT
10966 && GET_CODE (XEXP (op0, 0)) == PLUS
10967 && GET_CODE (XEXP (XEXP (op0, 0), 1)) == CONST_INT
10968 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
10969 && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
10970 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
10971 MODE_INT, 1)) != BLKmode
10972 && (((unsigned HOST_WIDE_INT) const_op
10973 + (GET_MODE_MASK (tmode) >> 1) + 1)
10974 <= GET_MODE_MASK (tmode)))
10976 rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
10977 rtx add_const = XEXP (XEXP (op0, 0), 1);
10978 rtx new_const = simplify_gen_binary (ASHIFTRT, GET_MODE (op0),
10979 add_const, XEXP (op0, 1));
10981 op0 = simplify_gen_binary (PLUS, tmode,
10982 gen_lowpart (tmode, inner),
10983 new_const);
10984 continue;
10987 /* ... fall through ... */
10988 case LSHIFTRT:
10989 /* If we have (compare (xshiftrt FOO N) (const_int C)) and
10990 the low order N bits of FOO are known to be zero, we can do this
10991 by comparing FOO with C shifted left N bits so long as no
10992 overflow occurs. */
10993 if (GET_CODE (XEXP (op0, 1)) == CONST_INT
10994 && INTVAL (XEXP (op0, 1)) >= 0
10995 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
10996 && mode_width <= HOST_BITS_PER_WIDE_INT
10997 && (nonzero_bits (XEXP (op0, 0), mode)
10998 & (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1)) == 0
10999 && (((unsigned HOST_WIDE_INT) const_op
11000 + (GET_CODE (op0) != LSHIFTRT
11001 ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
11002 + 1)
11003 : 0))
11004 <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
11006 /* If the shift was logical, then we must make the condition
11007 unsigned. */
11008 if (GET_CODE (op0) == LSHIFTRT)
11009 code = unsigned_condition (code);
11011 const_op <<= INTVAL (XEXP (op0, 1));
11012 op1 = GEN_INT (const_op);
11013 op0 = XEXP (op0, 0);
11014 continue;
11017 /* If we are using this shift to extract just the sign bit, we
11018 can replace this with an LT or GE comparison. */
11019 if (const_op == 0
11020 && (equality_comparison_p || sign_bit_comparison_p)
11021 && GET_CODE (XEXP (op0, 1)) == CONST_INT
11022 && (unsigned HOST_WIDE_INT) INTVAL (XEXP (op0, 1))
11023 == mode_width - 1)
11025 op0 = XEXP (op0, 0);
11026 code = (code == NE || code == GT ? LT : GE);
11027 continue;
11029 break;
11031 default:
11032 break;
11035 break;
11038 /* Now make any compound operations involved in this comparison. Then,
11039 check for an outmost SUBREG on OP0 that is not doing anything or is
11040 paradoxical. The latter transformation must only be performed when
11041 it is known that the "extra" bits will be the same in op0 and op1 or
11042 that they don't matter. There are three cases to consider:
11044 1. SUBREG_REG (op0) is a register. In this case the bits are don't
11045 care bits and we can assume they have any convenient value. So
11046 making the transformation is safe.
11048 2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not defined.
11049 In this case the upper bits of op0 are undefined. We should not make
11050 the simplification in that case as we do not know the contents of
11051 those bits.
11053 3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is defined and not
11054 UNKNOWN. In that case we know those bits are zeros or ones. We must
11055 also be sure that they are the same as the upper bits of op1.
11057 We can never remove a SUBREG for a non-equality comparison because
11058 the sign bit is in a different place in the underlying object. */
11060 op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
11061 op1 = make_compound_operation (op1, SET);
11063 if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
11064 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
11065 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (op0))) == MODE_INT
11066 && (code == NE || code == EQ))
11068 if (GET_MODE_SIZE (GET_MODE (op0))
11069 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op0))))
11071 /* For paradoxical subregs, allow case 1 as above. Case 3 isn't
11072 implemented. */
11073 if (REG_P (SUBREG_REG (op0)))
11075 op0 = SUBREG_REG (op0);
11076 op1 = gen_lowpart (GET_MODE (op0), op1);
11079 else if ((GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (op0)))
11080 <= HOST_BITS_PER_WIDE_INT)
11081 && (nonzero_bits (SUBREG_REG (op0),
11082 GET_MODE (SUBREG_REG (op0)))
11083 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
11085 tem = gen_lowpart (GET_MODE (SUBREG_REG (op0)), op1);
11087 if ((nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
11088 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
11089 op0 = SUBREG_REG (op0), op1 = tem;
11093 /* We now do the opposite procedure: Some machines don't have compare
11094 insns in all modes. If OP0's mode is an integer mode smaller than a
11095 word and we can't do a compare in that mode, see if there is a larger
11096 mode for which we can do the compare. There are a number of cases in
11097 which we can use the wider mode. */
11099 mode = GET_MODE (op0);
11100 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
11101 && GET_MODE_SIZE (mode) < UNITS_PER_WORD
11102 && ! have_insn_for (COMPARE, mode))
11103 for (tmode = GET_MODE_WIDER_MODE (mode);
11104 (tmode != VOIDmode
11105 && GET_MODE_BITSIZE (tmode) <= HOST_BITS_PER_WIDE_INT);
11106 tmode = GET_MODE_WIDER_MODE (tmode))
11107 if (have_insn_for (COMPARE, tmode))
11109 int zero_extended;
11111 /* If the only nonzero bits in OP0 and OP1 are those in the
11112 narrower mode and this is an equality or unsigned comparison,
11113 we can use the wider mode. Similarly for sign-extended
11114 values, in which case it is true for all comparisons. */
11115 zero_extended = ((code == EQ || code == NE
11116 || code == GEU || code == GTU
11117 || code == LEU || code == LTU)
11118 && (nonzero_bits (op0, tmode)
11119 & ~GET_MODE_MASK (mode)) == 0
11120 && ((GET_CODE (op1) == CONST_INT
11121 || (nonzero_bits (op1, tmode)
11122 & ~GET_MODE_MASK (mode)) == 0)));
11124 if (zero_extended
11125 || ((num_sign_bit_copies (op0, tmode)
11126 > (unsigned int) (GET_MODE_BITSIZE (tmode)
11127 - GET_MODE_BITSIZE (mode)))
11128 && (num_sign_bit_copies (op1, tmode)
11129 > (unsigned int) (GET_MODE_BITSIZE (tmode)
11130 - GET_MODE_BITSIZE (mode)))))
11132 /* If OP0 is an AND and we don't have an AND in MODE either,
11133 make a new AND in the proper mode. */
11134 if (GET_CODE (op0) == AND
11135 && !have_insn_for (AND, mode))
11136 op0 = simplify_gen_binary (AND, tmode,
11137 gen_lowpart (tmode,
11138 XEXP (op0, 0)),
11139 gen_lowpart (tmode,
11140 XEXP (op0, 1)));
11142 op0 = gen_lowpart (tmode, op0);
11143 if (zero_extended && GET_CODE (op1) == CONST_INT)
11144 op1 = GEN_INT (INTVAL (op1) & GET_MODE_MASK (mode));
11145 op1 = gen_lowpart (tmode, op1);
11146 break;
11149 /* If this is a test for negative, we can make an explicit
11150 test of the sign bit. */
11152 if (op1 == const0_rtx && (code == LT || code == GE)
11153 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11155 op0 = simplify_gen_binary (AND, tmode,
11156 gen_lowpart (tmode, op0),
11157 GEN_INT ((HOST_WIDE_INT) 1
11158 << (GET_MODE_BITSIZE (mode)
11159 - 1)));
11160 code = (code == LT) ? NE : EQ;
11161 break;
11165 #ifdef CANONICALIZE_COMPARISON
11166 /* If this machine only supports a subset of valid comparisons, see if we
11167 can convert an unsupported one into a supported one. */
11168 CANONICALIZE_COMPARISON (code, op0, op1);
11169 #endif
11171 *pop0 = op0;
11172 *pop1 = op1;
11174 return code;
11177 /* Utility function for record_value_for_reg. Count number of
11178 rtxs in X. */
11179 static int
11180 count_rtxs (rtx x)
11182 enum rtx_code code = GET_CODE (x);
11183 const char *fmt;
11184 int i, ret = 1;
11186 if (GET_RTX_CLASS (code) == '2'
11187 || GET_RTX_CLASS (code) == 'c')
11189 rtx x0 = XEXP (x, 0);
11190 rtx x1 = XEXP (x, 1);
11192 if (x0 == x1)
11193 return 1 + 2 * count_rtxs (x0);
11195 if ((GET_RTX_CLASS (GET_CODE (x1)) == '2'
11196 || GET_RTX_CLASS (GET_CODE (x1)) == 'c')
11197 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
11198 return 2 + 2 * count_rtxs (x0)
11199 + count_rtxs (x == XEXP (x1, 0)
11200 ? XEXP (x1, 1) : XEXP (x1, 0));
11202 if ((GET_RTX_CLASS (GET_CODE (x0)) == '2'
11203 || GET_RTX_CLASS (GET_CODE (x0)) == 'c')
11204 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
11205 return 2 + 2 * count_rtxs (x1)
11206 + count_rtxs (x == XEXP (x0, 0)
11207 ? XEXP (x0, 1) : XEXP (x0, 0));
11210 fmt = GET_RTX_FORMAT (code);
11211 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11212 if (fmt[i] == 'e')
11213 ret += count_rtxs (XEXP (x, i));
11215 return ret;
11218 /* Utility function for following routine. Called when X is part of a value
11219 being stored into last_set_value. Sets last_set_table_tick
11220 for each register mentioned. Similar to mention_regs in cse.c */
11222 static void
11223 update_table_tick (rtx x)
11225 enum rtx_code code = GET_CODE (x);
11226 const char *fmt = GET_RTX_FORMAT (code);
11227 int i;
11229 if (code == REG)
11231 unsigned int regno = REGNO (x);
11232 unsigned int endregno = END_REGNO (x);
11233 unsigned int r;
11235 for (r = regno; r < endregno; r++)
11237 reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, r);
11238 rsp->last_set_table_tick = label_tick;
11241 return;
11244 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11245 /* Note that we can't have an "E" in values stored; see
11246 get_last_value_validate. */
11247 if (fmt[i] == 'e')
11249 /* Check for identical subexpressions. If x contains
11250 identical subexpression we only have to traverse one of
11251 them. */
11252 if (i == 0 && ARITHMETIC_P (x))
11254 /* Note that at this point x1 has already been
11255 processed. */
11256 rtx x0 = XEXP (x, 0);
11257 rtx x1 = XEXP (x, 1);
11259 /* If x0 and x1 are identical then there is no need to
11260 process x0. */
11261 if (x0 == x1)
11262 break;
11264 /* If x0 is identical to a subexpression of x1 then while
11265 processing x1, x0 has already been processed. Thus we
11266 are done with x. */
11267 if (ARITHMETIC_P (x1)
11268 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
11269 break;
11271 /* If x1 is identical to a subexpression of x0 then we
11272 still have to process the rest of x0. */
11273 if (ARITHMETIC_P (x0)
11274 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
11276 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
11277 break;
11281 update_table_tick (XEXP (x, i));
11285 /* Record that REG is set to VALUE in insn INSN. If VALUE is zero, we
11286 are saying that the register is clobbered and we no longer know its
11287 value. If INSN is zero, don't update reg_stat[].last_set; this is
11288 only permitted with VALUE also zero and is used to invalidate the
11289 register. */
11291 static void
11292 record_value_for_reg (rtx reg, rtx insn, rtx value)
11294 unsigned int regno = REGNO (reg);
11295 unsigned int endregno = END_REGNO (reg);
11296 unsigned int i;
11297 reg_stat_type *rsp;
11299 /* If VALUE contains REG and we have a previous value for REG, substitute
11300 the previous value. */
11301 if (value && insn && reg_overlap_mentioned_p (reg, value))
11303 rtx tem;
11305 /* Set things up so get_last_value is allowed to see anything set up to
11306 our insn. */
11307 subst_low_luid = DF_INSN_LUID (insn);
11308 tem = get_last_value (reg);
11310 /* If TEM is simply a binary operation with two CLOBBERs as operands,
11311 it isn't going to be useful and will take a lot of time to process,
11312 so just use the CLOBBER. */
11314 if (tem)
11316 if (ARITHMETIC_P (tem)
11317 && GET_CODE (XEXP (tem, 0)) == CLOBBER
11318 && GET_CODE (XEXP (tem, 1)) == CLOBBER)
11319 tem = XEXP (tem, 0);
11320 else if (count_occurrences (value, reg, 1) >= 2)
11322 /* If there are two or more occurrences of REG in VALUE,
11323 prevent the value from growing too much. */
11324 if (count_rtxs (tem) > MAX_LAST_VALUE_RTL)
11325 tem = gen_rtx_CLOBBER (GET_MODE (tem), const0_rtx);
11328 value = replace_rtx (copy_rtx (value), reg, tem);
11332 /* For each register modified, show we don't know its value, that
11333 we don't know about its bitwise content, that its value has been
11334 updated, and that we don't know the location of the death of the
11335 register. */
11336 for (i = regno; i < endregno; i++)
11338 rsp = VEC_index (reg_stat_type, reg_stat, i);
11340 if (insn)
11341 rsp->last_set = insn;
11343 rsp->last_set_value = 0;
11344 rsp->last_set_mode = 0;
11345 rsp->last_set_nonzero_bits = 0;
11346 rsp->last_set_sign_bit_copies = 0;
11347 rsp->last_death = 0;
11348 rsp->truncated_to_mode = 0;
11351 /* Mark registers that are being referenced in this value. */
11352 if (value)
11353 update_table_tick (value);
11355 /* Now update the status of each register being set.
11356 If someone is using this register in this block, set this register
11357 to invalid since we will get confused between the two lives in this
11358 basic block. This makes using this register always invalid. In cse, we
11359 scan the table to invalidate all entries using this register, but this
11360 is too much work for us. */
11362 for (i = regno; i < endregno; i++)
11364 rsp = VEC_index (reg_stat_type, reg_stat, i);
11365 rsp->last_set_label = label_tick;
11366 if (!insn
11367 || (value && rsp->last_set_table_tick >= label_tick_ebb_start))
11368 rsp->last_set_invalid = 1;
11369 else
11370 rsp->last_set_invalid = 0;
11373 /* The value being assigned might refer to X (like in "x++;"). In that
11374 case, we must replace it with (clobber (const_int 0)) to prevent
11375 infinite loops. */
11376 rsp = VEC_index (reg_stat_type, reg_stat, regno);
11377 if (value && ! get_last_value_validate (&value, insn,
11378 rsp->last_set_label, 0))
11380 value = copy_rtx (value);
11381 if (! get_last_value_validate (&value, insn,
11382 rsp->last_set_label, 1))
11383 value = 0;
11386 /* For the main register being modified, update the value, the mode, the
11387 nonzero bits, and the number of sign bit copies. */
11389 rsp->last_set_value = value;
11391 if (value)
11393 enum machine_mode mode = GET_MODE (reg);
11394 subst_low_luid = DF_INSN_LUID (insn);
11395 rsp->last_set_mode = mode;
11396 if (GET_MODE_CLASS (mode) == MODE_INT
11397 && GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
11398 mode = nonzero_bits_mode;
11399 rsp->last_set_nonzero_bits = nonzero_bits (value, mode);
11400 rsp->last_set_sign_bit_copies
11401 = num_sign_bit_copies (value, GET_MODE (reg));
11405 /* Called via note_stores from record_dead_and_set_regs to handle one
11406 SET or CLOBBER in an insn. DATA is the instruction in which the
11407 set is occurring. */
11409 static void
11410 record_dead_and_set_regs_1 (rtx dest, const_rtx setter, void *data)
11412 rtx record_dead_insn = (rtx) data;
11414 if (GET_CODE (dest) == SUBREG)
11415 dest = SUBREG_REG (dest);
11417 if (!record_dead_insn)
11419 if (REG_P (dest))
11420 record_value_for_reg (dest, NULL_RTX, NULL_RTX);
11421 return;
11424 if (REG_P (dest))
11426 /* If we are setting the whole register, we know its value. Otherwise
11427 show that we don't know the value. We can handle SUBREG in
11428 some cases. */
11429 if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
11430 record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
11431 else if (GET_CODE (setter) == SET
11432 && GET_CODE (SET_DEST (setter)) == SUBREG
11433 && SUBREG_REG (SET_DEST (setter)) == dest
11434 && GET_MODE_BITSIZE (GET_MODE (dest)) <= BITS_PER_WORD
11435 && subreg_lowpart_p (SET_DEST (setter)))
11436 record_value_for_reg (dest, record_dead_insn,
11437 gen_lowpart (GET_MODE (dest),
11438 SET_SRC (setter)));
11439 else
11440 record_value_for_reg (dest, record_dead_insn, NULL_RTX);
11442 else if (MEM_P (dest)
11443 /* Ignore pushes, they clobber nothing. */
11444 && ! push_operand (dest, GET_MODE (dest)))
11445 mem_last_set = DF_INSN_LUID (record_dead_insn);
11448 /* Update the records of when each REG was most recently set or killed
11449 for the things done by INSN. This is the last thing done in processing
11450 INSN in the combiner loop.
11452 We update reg_stat[], in particular fields last_set, last_set_value,
11453 last_set_mode, last_set_nonzero_bits, last_set_sign_bit_copies,
11454 last_death, and also the similar information mem_last_set (which insn
11455 most recently modified memory) and last_call_luid (which insn was the
11456 most recent subroutine call). */
11458 static void
11459 record_dead_and_set_regs (rtx insn)
11461 rtx link;
11462 unsigned int i;
11464 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
11466 if (REG_NOTE_KIND (link) == REG_DEAD
11467 && REG_P (XEXP (link, 0)))
11469 unsigned int regno = REGNO (XEXP (link, 0));
11470 unsigned int endregno = END_REGNO (XEXP (link, 0));
11472 for (i = regno; i < endregno; i++)
11474 reg_stat_type *rsp;
11476 rsp = VEC_index (reg_stat_type, reg_stat, i);
11477 rsp->last_death = insn;
11480 else if (REG_NOTE_KIND (link) == REG_INC)
11481 record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
11484 if (CALL_P (insn))
11486 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
11487 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
11489 reg_stat_type *rsp;
11491 rsp = VEC_index (reg_stat_type, reg_stat, i);
11492 rsp->last_set_invalid = 1;
11493 rsp->last_set = insn;
11494 rsp->last_set_value = 0;
11495 rsp->last_set_mode = 0;
11496 rsp->last_set_nonzero_bits = 0;
11497 rsp->last_set_sign_bit_copies = 0;
11498 rsp->last_death = 0;
11499 rsp->truncated_to_mode = 0;
11502 last_call_luid = mem_last_set = DF_INSN_LUID (insn);
11504 /* We can't combine into a call pattern. Remember, though, that
11505 the return value register is set at this LUID. We could
11506 still replace a register with the return value from the
11507 wrong subroutine call! */
11508 note_stores (PATTERN (insn), record_dead_and_set_regs_1, NULL_RTX);
11510 else
11511 note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
11514 /* If a SUBREG has the promoted bit set, it is in fact a property of the
11515 register present in the SUBREG, so for each such SUBREG go back and
11516 adjust nonzero and sign bit information of the registers that are
11517 known to have some zero/sign bits set.
11519 This is needed because when combine blows the SUBREGs away, the
11520 information on zero/sign bits is lost and further combines can be
11521 missed because of that. */
11523 static void
11524 record_promoted_value (rtx insn, rtx subreg)
11526 rtx links, set;
11527 unsigned int regno = REGNO (SUBREG_REG (subreg));
11528 enum machine_mode mode = GET_MODE (subreg);
11530 if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT)
11531 return;
11533 for (links = LOG_LINKS (insn); links;)
11535 reg_stat_type *rsp;
11537 insn = XEXP (links, 0);
11538 set = single_set (insn);
11540 if (! set || !REG_P (SET_DEST (set))
11541 || REGNO (SET_DEST (set)) != regno
11542 || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
11544 links = XEXP (links, 1);
11545 continue;
11548 rsp = VEC_index (reg_stat_type, reg_stat, regno);
11549 if (rsp->last_set == insn)
11551 if (SUBREG_PROMOTED_UNSIGNED_P (subreg) > 0)
11552 rsp->last_set_nonzero_bits &= GET_MODE_MASK (mode);
11555 if (REG_P (SET_SRC (set)))
11557 regno = REGNO (SET_SRC (set));
11558 links = LOG_LINKS (insn);
11560 else
11561 break;
11565 /* Check if X, a register, is known to contain a value already
11566 truncated to MODE. In this case we can use a subreg to refer to
11567 the truncated value even though in the generic case we would need
11568 an explicit truncation. */
11570 static bool
11571 reg_truncated_to_mode (enum machine_mode mode, const_rtx x)
11573 reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
11574 enum machine_mode truncated = rsp->truncated_to_mode;
11576 if (truncated == 0
11577 || rsp->truncation_label < label_tick_ebb_start)
11578 return false;
11579 if (GET_MODE_SIZE (truncated) <= GET_MODE_SIZE (mode))
11580 return true;
11581 if (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
11582 GET_MODE_BITSIZE (truncated)))
11583 return true;
11584 return false;
11587 /* Callback for for_each_rtx. If *P is a hard reg or a subreg record the mode
11588 that the register is accessed in. For non-TRULY_NOOP_TRUNCATION targets we
11589 might be able to turn a truncate into a subreg using this information.
11590 Return -1 if traversing *P is complete or 0 otherwise. */
11592 static int
11593 record_truncated_value (rtx *p, void *data ATTRIBUTE_UNUSED)
11595 rtx x = *p;
11596 enum machine_mode truncated_mode;
11597 reg_stat_type *rsp;
11599 if (GET_CODE (x) == SUBREG && REG_P (SUBREG_REG (x)))
11601 enum machine_mode original_mode = GET_MODE (SUBREG_REG (x));
11602 truncated_mode = GET_MODE (x);
11604 if (GET_MODE_SIZE (original_mode) <= GET_MODE_SIZE (truncated_mode))
11605 return -1;
11607 if (TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (truncated_mode),
11608 GET_MODE_BITSIZE (original_mode)))
11609 return -1;
11611 x = SUBREG_REG (x);
11613 /* ??? For hard-regs we now record everything. We might be able to
11614 optimize this using last_set_mode. */
11615 else if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
11616 truncated_mode = GET_MODE (x);
11617 else
11618 return 0;
11620 rsp = VEC_index (reg_stat_type, reg_stat, REGNO (x));
11621 if (rsp->truncated_to_mode == 0
11622 || rsp->truncation_label < label_tick_ebb_start
11623 || (GET_MODE_SIZE (truncated_mode)
11624 < GET_MODE_SIZE (rsp->truncated_to_mode)))
11626 rsp->truncated_to_mode = truncated_mode;
11627 rsp->truncation_label = label_tick;
11630 return -1;
11633 /* Callback for note_uses. Find hardregs and subregs of pseudos and
11634 the modes they are used in. This can help truning TRUNCATEs into
11635 SUBREGs. */
11637 static void
11638 record_truncated_values (rtx *x, void *data ATTRIBUTE_UNUSED)
11640 for_each_rtx (x, record_truncated_value, NULL);
11643 /* Scan X for promoted SUBREGs. For each one found,
11644 note what it implies to the registers used in it. */
11646 static void
11647 check_promoted_subreg (rtx insn, rtx x)
11649 if (GET_CODE (x) == SUBREG
11650 && SUBREG_PROMOTED_VAR_P (x)
11651 && REG_P (SUBREG_REG (x)))
11652 record_promoted_value (insn, x);
11653 else
11655 const char *format = GET_RTX_FORMAT (GET_CODE (x));
11656 int i, j;
11658 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
11659 switch (format[i])
11661 case 'e':
11662 check_promoted_subreg (insn, XEXP (x, i));
11663 break;
11664 case 'V':
11665 case 'E':
11666 if (XVEC (x, i) != 0)
11667 for (j = 0; j < XVECLEN (x, i); j++)
11668 check_promoted_subreg (insn, XVECEXP (x, i, j));
11669 break;
11674 /* Utility routine for the following function. Verify that all the registers
11675 mentioned in *LOC are valid when *LOC was part of a value set when
11676 label_tick == TICK. Return 0 if some are not.
11678 If REPLACE is nonzero, replace the invalid reference with
11679 (clobber (const_int 0)) and return 1. This replacement is useful because
11680 we often can get useful information about the form of a value (e.g., if
11681 it was produced by a shift that always produces -1 or 0) even though
11682 we don't know exactly what registers it was produced from. */
11684 static int
11685 get_last_value_validate (rtx *loc, rtx insn, int tick, int replace)
11687 rtx x = *loc;
11688 const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
11689 int len = GET_RTX_LENGTH (GET_CODE (x));
11690 int i;
11692 if (REG_P (x))
11694 unsigned int regno = REGNO (x);
11695 unsigned int endregno = END_REGNO (x);
11696 unsigned int j;
11698 for (j = regno; j < endregno; j++)
11700 reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, j);
11701 if (rsp->last_set_invalid
11702 /* If this is a pseudo-register that was only set once and not
11703 live at the beginning of the function, it is always valid. */
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 && rsp->last_set_label > tick))
11710 if (replace)
11711 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
11712 return replace;
11716 return 1;
11718 /* If this is a memory reference, make sure that there were
11719 no stores after it that might have clobbered the value. We don't
11720 have alias info, so we assume any store invalidates it. */
11721 else if (MEM_P (x) && !MEM_READONLY_P (x)
11722 && DF_INSN_LUID (insn) <= mem_last_set)
11724 if (replace)
11725 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
11726 return replace;
11729 for (i = 0; i < len; i++)
11731 if (fmt[i] == 'e')
11733 /* Check for identical subexpressions. If x contains
11734 identical subexpression we only have to traverse one of
11735 them. */
11736 if (i == 1 && ARITHMETIC_P (x))
11738 /* Note that at this point x0 has already been checked
11739 and found valid. */
11740 rtx x0 = XEXP (x, 0);
11741 rtx x1 = XEXP (x, 1);
11743 /* If x0 and x1 are identical then x is also valid. */
11744 if (x0 == x1)
11745 return 1;
11747 /* If x1 is identical to a subexpression of x0 then
11748 while checking x0, x1 has already been checked. Thus
11749 it is valid and so as x. */
11750 if (ARITHMETIC_P (x0)
11751 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
11752 return 1;
11754 /* If x0 is identical to a subexpression of x1 then x is
11755 valid iff the rest of x1 is valid. */
11756 if (ARITHMETIC_P (x1)
11757 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
11758 return
11759 get_last_value_validate (&XEXP (x1,
11760 x0 == XEXP (x1, 0) ? 1 : 0),
11761 insn, tick, replace);
11764 if (get_last_value_validate (&XEXP (x, i), insn, tick,
11765 replace) == 0)
11766 return 0;
11768 /* Don't bother with these. They shouldn't occur anyway. */
11769 else if (fmt[i] == 'E')
11770 return 0;
11773 /* If we haven't found a reason for it to be invalid, it is valid. */
11774 return 1;
11777 /* Get the last value assigned to X, if known. Some registers
11778 in the value may be replaced with (clobber (const_int 0)) if their value
11779 is known longer known reliably. */
11781 static rtx
11782 get_last_value (const_rtx x)
11784 unsigned int regno;
11785 rtx value;
11786 reg_stat_type *rsp;
11788 /* If this is a non-paradoxical SUBREG, get the value of its operand and
11789 then convert it to the desired mode. If this is a paradoxical SUBREG,
11790 we cannot predict what values the "extra" bits might have. */
11791 if (GET_CODE (x) == SUBREG
11792 && subreg_lowpart_p (x)
11793 && (GET_MODE_SIZE (GET_MODE (x))
11794 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
11795 && (value = get_last_value (SUBREG_REG (x))) != 0)
11796 return gen_lowpart (GET_MODE (x), value);
11798 if (!REG_P (x))
11799 return 0;
11801 regno = REGNO (x);
11802 rsp = VEC_index (reg_stat_type, reg_stat, regno);
11803 value = rsp->last_set_value;
11805 /* If we don't have a value, or if it isn't for this basic block and
11806 it's either a hard register, set more than once, or it's a live
11807 at the beginning of the function, return 0.
11809 Because if it's not live at the beginning of the function then the reg
11810 is always set before being used (is never used without being set).
11811 And, if it's set only once, and it's always set before use, then all
11812 uses must have the same last value, even if it's not from this basic
11813 block. */
11815 if (value == 0
11816 || (rsp->last_set_label < label_tick_ebb_start
11817 && (regno < FIRST_PSEUDO_REGISTER
11818 || REG_N_SETS (regno) != 1
11819 || REGNO_REG_SET_P
11820 (DF_LR_IN (ENTRY_BLOCK_PTR->next_bb), regno))))
11821 return 0;
11823 /* If the value was set in a later insn than the ones we are processing,
11824 we can't use it even if the register was only set once. */
11825 if (rsp->last_set_label == label_tick
11826 && DF_INSN_LUID (rsp->last_set) >= subst_low_luid)
11827 return 0;
11829 /* If the value has all its registers valid, return it. */
11830 if (get_last_value_validate (&value, rsp->last_set,
11831 rsp->last_set_label, 0))
11832 return value;
11834 /* Otherwise, make a copy and replace any invalid register with
11835 (clobber (const_int 0)). If that fails for some reason, return 0. */
11837 value = copy_rtx (value);
11838 if (get_last_value_validate (&value, rsp->last_set,
11839 rsp->last_set_label, 1))
11840 return value;
11842 return 0;
11845 /* Return nonzero if expression X refers to a REG or to memory
11846 that is set in an instruction more recent than FROM_LUID. */
11848 static int
11849 use_crosses_set_p (const_rtx x, int from_luid)
11851 const char *fmt;
11852 int i;
11853 enum rtx_code code = GET_CODE (x);
11855 if (code == REG)
11857 unsigned int regno = REGNO (x);
11858 unsigned endreg = END_REGNO (x);
11860 #ifdef PUSH_ROUNDING
11861 /* Don't allow uses of the stack pointer to be moved,
11862 because we don't know whether the move crosses a push insn. */
11863 if (regno == STACK_POINTER_REGNUM && PUSH_ARGS)
11864 return 1;
11865 #endif
11866 for (; regno < endreg; regno++)
11868 reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, regno);
11869 if (rsp->last_set
11870 && rsp->last_set_label == label_tick
11871 && DF_INSN_LUID (rsp->last_set) > from_luid)
11872 return 1;
11874 return 0;
11877 if (code == MEM && mem_last_set > from_luid)
11878 return 1;
11880 fmt = GET_RTX_FORMAT (code);
11882 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
11884 if (fmt[i] == 'E')
11886 int j;
11887 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
11888 if (use_crosses_set_p (XVECEXP (x, i, j), from_luid))
11889 return 1;
11891 else if (fmt[i] == 'e'
11892 && use_crosses_set_p (XEXP (x, i), from_luid))
11893 return 1;
11895 return 0;
11898 /* Define three variables used for communication between the following
11899 routines. */
11901 static unsigned int reg_dead_regno, reg_dead_endregno;
11902 static int reg_dead_flag;
11904 /* Function called via note_stores from reg_dead_at_p.
11906 If DEST is within [reg_dead_regno, reg_dead_endregno), set
11907 reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET. */
11909 static void
11910 reg_dead_at_p_1 (rtx dest, const_rtx x, void *data ATTRIBUTE_UNUSED)
11912 unsigned int regno, endregno;
11914 if (!REG_P (dest))
11915 return;
11917 regno = REGNO (dest);
11918 endregno = END_REGNO (dest);
11919 if (reg_dead_endregno > regno && reg_dead_regno < endregno)
11920 reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
11923 /* Return nonzero if REG is known to be dead at INSN.
11925 We scan backwards from INSN. If we hit a REG_DEAD note or a CLOBBER
11926 referencing REG, it is dead. If we hit a SET referencing REG, it is
11927 live. Otherwise, see if it is live or dead at the start of the basic
11928 block we are in. Hard regs marked as being live in NEWPAT_USED_REGS
11929 must be assumed to be always live. */
11931 static int
11932 reg_dead_at_p (rtx reg, rtx insn)
11934 basic_block block;
11935 unsigned int i;
11937 /* Set variables for reg_dead_at_p_1. */
11938 reg_dead_regno = REGNO (reg);
11939 reg_dead_endregno = END_REGNO (reg);
11941 reg_dead_flag = 0;
11943 /* Check that reg isn't mentioned in NEWPAT_USED_REGS. For fixed registers
11944 we allow the machine description to decide whether use-and-clobber
11945 patterns are OK. */
11946 if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
11948 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
11949 if (!fixed_regs[i] && TEST_HARD_REG_BIT (newpat_used_regs, i))
11950 return 0;
11953 /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, label, or
11954 beginning of function. */
11955 for (; insn && !LABEL_P (insn) && !BARRIER_P (insn);
11956 insn = prev_nonnote_insn (insn))
11958 note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
11959 if (reg_dead_flag)
11960 return reg_dead_flag == 1 ? 1 : 0;
11962 if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
11963 return 1;
11966 /* Get the basic block that we were in. */
11967 if (insn == 0)
11968 block = ENTRY_BLOCK_PTR->next_bb;
11969 else
11971 FOR_EACH_BB (block)
11972 if (insn == BB_HEAD (block))
11973 break;
11975 if (block == EXIT_BLOCK_PTR)
11976 return 0;
11979 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
11980 if (REGNO_REG_SET_P (df_get_live_in (block), i))
11981 return 0;
11983 return 1;
11986 /* Note hard registers in X that are used. */
11988 static void
11989 mark_used_regs_combine (rtx x)
11991 RTX_CODE code = GET_CODE (x);
11992 unsigned int regno;
11993 int i;
11995 switch (code)
11997 case LABEL_REF:
11998 case SYMBOL_REF:
11999 case CONST_INT:
12000 case CONST:
12001 case CONST_DOUBLE:
12002 case CONST_VECTOR:
12003 case PC:
12004 case ADDR_VEC:
12005 case ADDR_DIFF_VEC:
12006 case ASM_INPUT:
12007 #ifdef HAVE_cc0
12008 /* CC0 must die in the insn after it is set, so we don't need to take
12009 special note of it here. */
12010 case CC0:
12011 #endif
12012 return;
12014 case CLOBBER:
12015 /* If we are clobbering a MEM, mark any hard registers inside the
12016 address as used. */
12017 if (MEM_P (XEXP (x, 0)))
12018 mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
12019 return;
12021 case REG:
12022 regno = REGNO (x);
12023 /* A hard reg in a wide mode may really be multiple registers.
12024 If so, mark all of them just like the first. */
12025 if (regno < FIRST_PSEUDO_REGISTER)
12027 /* None of this applies to the stack, frame or arg pointers. */
12028 if (regno == STACK_POINTER_REGNUM
12029 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
12030 || regno == HARD_FRAME_POINTER_REGNUM
12031 #endif
12032 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
12033 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
12034 #endif
12035 || regno == FRAME_POINTER_REGNUM)
12036 return;
12038 add_to_hard_reg_set (&newpat_used_regs, GET_MODE (x), regno);
12040 return;
12042 case SET:
12044 /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
12045 the address. */
12046 rtx testreg = SET_DEST (x);
12048 while (GET_CODE (testreg) == SUBREG
12049 || GET_CODE (testreg) == ZERO_EXTRACT
12050 || GET_CODE (testreg) == STRICT_LOW_PART)
12051 testreg = XEXP (testreg, 0);
12053 if (MEM_P (testreg))
12054 mark_used_regs_combine (XEXP (testreg, 0));
12056 mark_used_regs_combine (SET_SRC (x));
12058 return;
12060 default:
12061 break;
12064 /* Recursively scan the operands of this expression. */
12067 const char *fmt = GET_RTX_FORMAT (code);
12069 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12071 if (fmt[i] == 'e')
12072 mark_used_regs_combine (XEXP (x, i));
12073 else if (fmt[i] == 'E')
12075 int j;
12077 for (j = 0; j < XVECLEN (x, i); j++)
12078 mark_used_regs_combine (XVECEXP (x, i, j));
12084 /* Remove register number REGNO from the dead registers list of INSN.
12086 Return the note used to record the death, if there was one. */
12089 remove_death (unsigned int regno, rtx insn)
12091 rtx note = find_regno_note (insn, REG_DEAD, regno);
12093 if (note)
12094 remove_note (insn, note);
12096 return note;
12099 /* For each register (hardware or pseudo) used within expression X, if its
12100 death is in an instruction with luid between FROM_LUID (inclusive) and
12101 TO_INSN (exclusive), put a REG_DEAD note for that register in the
12102 list headed by PNOTES.
12104 That said, don't move registers killed by maybe_kill_insn.
12106 This is done when X is being merged by combination into TO_INSN. These
12107 notes will then be distributed as needed. */
12109 static void
12110 move_deaths (rtx x, rtx maybe_kill_insn, int from_luid, rtx to_insn,
12111 rtx *pnotes)
12113 const char *fmt;
12114 int len, i;
12115 enum rtx_code code = GET_CODE (x);
12117 if (code == REG)
12119 unsigned int regno = REGNO (x);
12120 rtx where_dead = VEC_index (reg_stat_type, reg_stat, regno)->last_death;
12122 /* Don't move the register if it gets killed in between from and to. */
12123 if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
12124 && ! reg_referenced_p (x, maybe_kill_insn))
12125 return;
12127 if (where_dead
12128 && DF_INSN_LUID (where_dead) >= from_luid
12129 && DF_INSN_LUID (where_dead) < DF_INSN_LUID (to_insn))
12131 rtx note = remove_death (regno, where_dead);
12133 /* It is possible for the call above to return 0. This can occur
12134 when last_death points to I2 or I1 that we combined with.
12135 In that case make a new note.
12137 We must also check for the case where X is a hard register
12138 and NOTE is a death note for a range of hard registers
12139 including X. In that case, we must put REG_DEAD notes for
12140 the remaining registers in place of NOTE. */
12142 if (note != 0 && regno < FIRST_PSEUDO_REGISTER
12143 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
12144 > GET_MODE_SIZE (GET_MODE (x))))
12146 unsigned int deadregno = REGNO (XEXP (note, 0));
12147 unsigned int deadend = END_HARD_REGNO (XEXP (note, 0));
12148 unsigned int ourend = END_HARD_REGNO (x);
12149 unsigned int i;
12151 for (i = deadregno; i < deadend; i++)
12152 if (i < regno || i >= ourend)
12153 add_reg_note (where_dead, REG_DEAD, regno_reg_rtx[i]);
12156 /* If we didn't find any note, or if we found a REG_DEAD note that
12157 covers only part of the given reg, and we have a multi-reg hard
12158 register, then to be safe we must check for REG_DEAD notes
12159 for each register other than the first. They could have
12160 their own REG_DEAD notes lying around. */
12161 else if ((note == 0
12162 || (note != 0
12163 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
12164 < GET_MODE_SIZE (GET_MODE (x)))))
12165 && regno < FIRST_PSEUDO_REGISTER
12166 && hard_regno_nregs[regno][GET_MODE (x)] > 1)
12168 unsigned int ourend = END_HARD_REGNO (x);
12169 unsigned int i, offset;
12170 rtx oldnotes = 0;
12172 if (note)
12173 offset = hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))];
12174 else
12175 offset = 1;
12177 for (i = regno + offset; i < ourend; i++)
12178 move_deaths (regno_reg_rtx[i],
12179 maybe_kill_insn, from_luid, to_insn, &oldnotes);
12182 if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
12184 XEXP (note, 1) = *pnotes;
12185 *pnotes = note;
12187 else
12188 *pnotes = gen_rtx_EXPR_LIST (REG_DEAD, x, *pnotes);
12191 return;
12194 else if (GET_CODE (x) == SET)
12196 rtx dest = SET_DEST (x);
12198 move_deaths (SET_SRC (x), maybe_kill_insn, from_luid, to_insn, pnotes);
12200 /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
12201 that accesses one word of a multi-word item, some
12202 piece of everything register in the expression is used by
12203 this insn, so remove any old death. */
12204 /* ??? So why do we test for equality of the sizes? */
12206 if (GET_CODE (dest) == ZERO_EXTRACT
12207 || GET_CODE (dest) == STRICT_LOW_PART
12208 || (GET_CODE (dest) == SUBREG
12209 && (((GET_MODE_SIZE (GET_MODE (dest))
12210 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
12211 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
12212 + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
12214 move_deaths (dest, maybe_kill_insn, from_luid, to_insn, pnotes);
12215 return;
12218 /* If this is some other SUBREG, we know it replaces the entire
12219 value, so use that as the destination. */
12220 if (GET_CODE (dest) == SUBREG)
12221 dest = SUBREG_REG (dest);
12223 /* If this is a MEM, adjust deaths of anything used in the address.
12224 For a REG (the only other possibility), the entire value is
12225 being replaced so the old value is not used in this insn. */
12227 if (MEM_P (dest))
12228 move_deaths (XEXP (dest, 0), maybe_kill_insn, from_luid,
12229 to_insn, pnotes);
12230 return;
12233 else if (GET_CODE (x) == CLOBBER)
12234 return;
12236 len = GET_RTX_LENGTH (code);
12237 fmt = GET_RTX_FORMAT (code);
12239 for (i = 0; i < len; i++)
12241 if (fmt[i] == 'E')
12243 int j;
12244 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
12245 move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_luid,
12246 to_insn, pnotes);
12248 else if (fmt[i] == 'e')
12249 move_deaths (XEXP (x, i), maybe_kill_insn, from_luid, to_insn, pnotes);
12253 /* Return 1 if X is the target of a bit-field assignment in BODY, the
12254 pattern of an insn. X must be a REG. */
12256 static int
12257 reg_bitfield_target_p (rtx x, rtx body)
12259 int i;
12261 if (GET_CODE (body) == SET)
12263 rtx dest = SET_DEST (body);
12264 rtx target;
12265 unsigned int regno, tregno, endregno, endtregno;
12267 if (GET_CODE (dest) == ZERO_EXTRACT)
12268 target = XEXP (dest, 0);
12269 else if (GET_CODE (dest) == STRICT_LOW_PART)
12270 target = SUBREG_REG (XEXP (dest, 0));
12271 else
12272 return 0;
12274 if (GET_CODE (target) == SUBREG)
12275 target = SUBREG_REG (target);
12277 if (!REG_P (target))
12278 return 0;
12280 tregno = REGNO (target), regno = REGNO (x);
12281 if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
12282 return target == x;
12284 endtregno = end_hard_regno (GET_MODE (target), tregno);
12285 endregno = end_hard_regno (GET_MODE (x), regno);
12287 return endregno > tregno && regno < endtregno;
12290 else if (GET_CODE (body) == PARALLEL)
12291 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
12292 if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
12293 return 1;
12295 return 0;
12298 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
12299 as appropriate. I3 and I2 are the insns resulting from the combination
12300 insns including FROM (I2 may be zero).
12302 ELIM_I2 and ELIM_I1 are either zero or registers that we know will
12303 not need REG_DEAD notes because they are being substituted for. This
12304 saves searching in the most common cases.
12306 Each note in the list is either ignored or placed on some insns, depending
12307 on the type of note. */
12309 static void
12310 distribute_notes (rtx notes, rtx from_insn, rtx i3, rtx i2, rtx elim_i2,
12311 rtx elim_i1)
12313 rtx note, next_note;
12314 rtx tem;
12316 for (note = notes; note; note = next_note)
12318 rtx place = 0, place2 = 0;
12320 next_note = XEXP (note, 1);
12321 switch (REG_NOTE_KIND (note))
12323 case REG_BR_PROB:
12324 case REG_BR_PRED:
12325 /* Doesn't matter much where we put this, as long as it's somewhere.
12326 It is preferable to keep these notes on branches, which is most
12327 likely to be i3. */
12328 place = i3;
12329 break;
12331 case REG_VALUE_PROFILE:
12332 /* Just get rid of this note, as it is unused later anyway. */
12333 break;
12335 case REG_NON_LOCAL_GOTO:
12336 if (JUMP_P (i3))
12337 place = i3;
12338 else
12340 gcc_assert (i2 && JUMP_P (i2));
12341 place = i2;
12343 break;
12345 case REG_EH_REGION:
12346 /* These notes must remain with the call or trapping instruction. */
12347 if (CALL_P (i3))
12348 place = i3;
12349 else if (i2 && CALL_P (i2))
12350 place = i2;
12351 else
12353 gcc_assert (flag_non_call_exceptions);
12354 if (may_trap_p (i3))
12355 place = i3;
12356 else if (i2 && may_trap_p (i2))
12357 place = i2;
12358 /* ??? Otherwise assume we've combined things such that we
12359 can now prove that the instructions can't trap. Drop the
12360 note in this case. */
12362 break;
12364 case REG_NORETURN:
12365 case REG_SETJMP:
12366 /* These notes must remain with the call. It should not be
12367 possible for both I2 and I3 to be a call. */
12368 if (CALL_P (i3))
12369 place = i3;
12370 else
12372 gcc_assert (i2 && CALL_P (i2));
12373 place = i2;
12375 break;
12377 case REG_UNUSED:
12378 /* Any clobbers for i3 may still exist, and so we must process
12379 REG_UNUSED notes from that insn.
12381 Any clobbers from i2 or i1 can only exist if they were added by
12382 recog_for_combine. In that case, recog_for_combine created the
12383 necessary REG_UNUSED notes. Trying to keep any original
12384 REG_UNUSED notes from these insns can cause incorrect output
12385 if it is for the same register as the original i3 dest.
12386 In that case, we will notice that the register is set in i3,
12387 and then add a REG_UNUSED note for the destination of i3, which
12388 is wrong. However, it is possible to have REG_UNUSED notes from
12389 i2 or i1 for register which were both used and clobbered, so
12390 we keep notes from i2 or i1 if they will turn into REG_DEAD
12391 notes. */
12393 /* If this register is set or clobbered in I3, put the note there
12394 unless there is one already. */
12395 if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
12397 if (from_insn != i3)
12398 break;
12400 if (! (REG_P (XEXP (note, 0))
12401 ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
12402 : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
12403 place = i3;
12405 /* Otherwise, if this register is used by I3, then this register
12406 now dies here, so we must put a REG_DEAD note here unless there
12407 is one already. */
12408 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
12409 && ! (REG_P (XEXP (note, 0))
12410 ? find_regno_note (i3, REG_DEAD,
12411 REGNO (XEXP (note, 0)))
12412 : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
12414 PUT_REG_NOTE_KIND (note, REG_DEAD);
12415 place = i3;
12417 break;
12419 case REG_EQUAL:
12420 case REG_EQUIV:
12421 case REG_NOALIAS:
12422 /* These notes say something about results of an insn. We can
12423 only support them if they used to be on I3 in which case they
12424 remain on I3. Otherwise they are ignored.
12426 If the note refers to an expression that is not a constant, we
12427 must also ignore the note since we cannot tell whether the
12428 equivalence is still true. It might be possible to do
12429 slightly better than this (we only have a problem if I2DEST
12430 or I1DEST is present in the expression), but it doesn't
12431 seem worth the trouble. */
12433 if (from_insn == i3
12434 && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
12435 place = i3;
12436 break;
12438 case REG_INC:
12439 /* These notes say something about how a register is used. They must
12440 be present on any use of the register in I2 or I3. */
12441 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
12442 place = i3;
12444 if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
12446 if (place)
12447 place2 = i2;
12448 else
12449 place = i2;
12451 break;
12453 case REG_LABEL_TARGET:
12454 case REG_LABEL_OPERAND:
12455 /* This can show up in several ways -- either directly in the
12456 pattern, or hidden off in the constant pool with (or without?)
12457 a REG_EQUAL note. */
12458 /* ??? Ignore the without-reg_equal-note problem for now. */
12459 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
12460 || ((tem = find_reg_note (i3, REG_EQUAL, NULL_RTX))
12461 && GET_CODE (XEXP (tem, 0)) == LABEL_REF
12462 && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0)))
12463 place = i3;
12465 if (i2
12466 && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
12467 || ((tem = find_reg_note (i2, REG_EQUAL, NULL_RTX))
12468 && GET_CODE (XEXP (tem, 0)) == LABEL_REF
12469 && XEXP (XEXP (tem, 0), 0) == XEXP (note, 0))))
12471 if (place)
12472 place2 = i2;
12473 else
12474 place = i2;
12477 /* For REG_LABEL_TARGET on a JUMP_P, we prefer to put the note
12478 as a JUMP_LABEL or decrement LABEL_NUSES if it's already
12479 there. */
12480 if (place && JUMP_P (place)
12481 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
12482 && (JUMP_LABEL (place) == NULL
12483 || JUMP_LABEL (place) == XEXP (note, 0)))
12485 rtx label = JUMP_LABEL (place);
12487 if (!label)
12488 JUMP_LABEL (place) = XEXP (note, 0);
12489 else if (LABEL_P (label))
12490 LABEL_NUSES (label)--;
12493 if (place2 && JUMP_P (place2)
12494 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
12495 && (JUMP_LABEL (place2) == NULL
12496 || JUMP_LABEL (place2) == XEXP (note, 0)))
12498 rtx label = JUMP_LABEL (place2);
12500 if (!label)
12501 JUMP_LABEL (place2) = XEXP (note, 0);
12502 else if (LABEL_P (label))
12503 LABEL_NUSES (label)--;
12504 place2 = 0;
12506 break;
12508 case REG_NONNEG:
12509 /* This note says something about the value of a register prior
12510 to the execution of an insn. It is too much trouble to see
12511 if the note is still correct in all situations. It is better
12512 to simply delete it. */
12513 break;
12515 case REG_DEAD:
12516 /* If we replaced the right hand side of FROM_INSN with a
12517 REG_EQUAL note, the original use of the dying register
12518 will not have been combined into I3 and I2. In such cases,
12519 FROM_INSN is guaranteed to be the first of the combined
12520 instructions, so we simply need to search back before
12521 FROM_INSN for the previous use or set of this register,
12522 then alter the notes there appropriately.
12524 If the register is used as an input in I3, it dies there.
12525 Similarly for I2, if it is nonzero and adjacent to I3.
12527 If the register is not used as an input in either I3 or I2
12528 and it is not one of the registers we were supposed to eliminate,
12529 there are two possibilities. We might have a non-adjacent I2
12530 or we might have somehow eliminated an additional register
12531 from a computation. For example, we might have had A & B where
12532 we discover that B will always be zero. In this case we will
12533 eliminate the reference to A.
12535 In both cases, we must search to see if we can find a previous
12536 use of A and put the death note there. */
12538 if (from_insn
12539 && from_insn == i2mod
12540 && !reg_overlap_mentioned_p (XEXP (note, 0), i2mod_new_rhs))
12541 tem = from_insn;
12542 else
12544 if (from_insn
12545 && CALL_P (from_insn)
12546 && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
12547 place = from_insn;
12548 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
12549 place = i3;
12550 else if (i2 != 0 && next_nonnote_insn (i2) == i3
12551 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
12552 place = i2;
12553 else if ((rtx_equal_p (XEXP (note, 0), elim_i2)
12554 && !(i2mod
12555 && reg_overlap_mentioned_p (XEXP (note, 0),
12556 i2mod_old_rhs)))
12557 || rtx_equal_p (XEXP (note, 0), elim_i1))
12558 break;
12559 tem = i3;
12562 if (place == 0)
12564 basic_block bb = this_basic_block;
12566 for (tem = PREV_INSN (tem); place == 0; tem = PREV_INSN (tem))
12568 if (! INSN_P (tem))
12570 if (tem == BB_HEAD (bb))
12571 break;
12572 continue;
12575 /* If the register is being set at TEM, see if that is all
12576 TEM is doing. If so, delete TEM. Otherwise, make this
12577 into a REG_UNUSED note instead. Don't delete sets to
12578 global register vars. */
12579 if ((REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER
12580 || !global_regs[REGNO (XEXP (note, 0))])
12581 && reg_set_p (XEXP (note, 0), PATTERN (tem)))
12583 rtx set = single_set (tem);
12584 rtx inner_dest = 0;
12585 #ifdef HAVE_cc0
12586 rtx cc0_setter = NULL_RTX;
12587 #endif
12589 if (set != 0)
12590 for (inner_dest = SET_DEST (set);
12591 (GET_CODE (inner_dest) == STRICT_LOW_PART
12592 || GET_CODE (inner_dest) == SUBREG
12593 || GET_CODE (inner_dest) == ZERO_EXTRACT);
12594 inner_dest = XEXP (inner_dest, 0))
12597 /* Verify that it was the set, and not a clobber that
12598 modified the register.
12600 CC0 targets must be careful to maintain setter/user
12601 pairs. If we cannot delete the setter due to side
12602 effects, mark the user with an UNUSED note instead
12603 of deleting it. */
12605 if (set != 0 && ! side_effects_p (SET_SRC (set))
12606 && rtx_equal_p (XEXP (note, 0), inner_dest)
12607 #ifdef HAVE_cc0
12608 && (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
12609 || ((cc0_setter = prev_cc0_setter (tem)) != NULL
12610 && sets_cc0_p (PATTERN (cc0_setter)) > 0))
12611 #endif
12614 /* Move the notes and links of TEM elsewhere.
12615 This might delete other dead insns recursively.
12616 First set the pattern to something that won't use
12617 any register. */
12618 rtx old_notes = REG_NOTES (tem);
12620 PATTERN (tem) = pc_rtx;
12621 REG_NOTES (tem) = NULL;
12623 distribute_notes (old_notes, tem, tem, NULL_RTX,
12624 NULL_RTX, NULL_RTX);
12625 distribute_links (LOG_LINKS (tem));
12627 SET_INSN_DELETED (tem);
12629 #ifdef HAVE_cc0
12630 /* Delete the setter too. */
12631 if (cc0_setter)
12633 PATTERN (cc0_setter) = pc_rtx;
12634 old_notes = REG_NOTES (cc0_setter);
12635 REG_NOTES (cc0_setter) = NULL;
12637 distribute_notes (old_notes, cc0_setter,
12638 cc0_setter, NULL_RTX,
12639 NULL_RTX, NULL_RTX);
12640 distribute_links (LOG_LINKS (cc0_setter));
12642 SET_INSN_DELETED (cc0_setter);
12644 #endif
12646 else
12648 PUT_REG_NOTE_KIND (note, REG_UNUSED);
12650 /* If there isn't already a REG_UNUSED note, put one
12651 here. Do not place a REG_DEAD note, even if
12652 the register is also used here; that would not
12653 match the algorithm used in lifetime analysis
12654 and can cause the consistency check in the
12655 scheduler to fail. */
12656 if (! find_regno_note (tem, REG_UNUSED,
12657 REGNO (XEXP (note, 0))))
12658 place = tem;
12659 break;
12662 else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem))
12663 || (CALL_P (tem)
12664 && find_reg_fusage (tem, USE, XEXP (note, 0))))
12666 place = tem;
12668 /* If we are doing a 3->2 combination, and we have a
12669 register which formerly died in i3 and was not used
12670 by i2, which now no longer dies in i3 and is used in
12671 i2 but does not die in i2, and place is between i2
12672 and i3, then we may need to move a link from place to
12673 i2. */
12674 if (i2 && DF_INSN_LUID (place) > DF_INSN_LUID (i2)
12675 && from_insn
12676 && DF_INSN_LUID (from_insn) > DF_INSN_LUID (i2)
12677 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
12679 rtx links = LOG_LINKS (place);
12680 LOG_LINKS (place) = 0;
12681 distribute_links (links);
12683 break;
12686 if (tem == BB_HEAD (bb))
12687 break;
12692 /* If the register is set or already dead at PLACE, we needn't do
12693 anything with this note if it is still a REG_DEAD note.
12694 We check here if it is set at all, not if is it totally replaced,
12695 which is what `dead_or_set_p' checks, so also check for it being
12696 set partially. */
12698 if (place && REG_NOTE_KIND (note) == REG_DEAD)
12700 unsigned int regno = REGNO (XEXP (note, 0));
12701 reg_stat_type *rsp = VEC_index (reg_stat_type, reg_stat, regno);
12703 if (dead_or_set_p (place, XEXP (note, 0))
12704 || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
12706 /* Unless the register previously died in PLACE, clear
12707 last_death. [I no longer understand why this is
12708 being done.] */
12709 if (rsp->last_death != place)
12710 rsp->last_death = 0;
12711 place = 0;
12713 else
12714 rsp->last_death = place;
12716 /* If this is a death note for a hard reg that is occupying
12717 multiple registers, ensure that we are still using all
12718 parts of the object. If we find a piece of the object
12719 that is unused, we must arrange for an appropriate REG_DEAD
12720 note to be added for it. However, we can't just emit a USE
12721 and tag the note to it, since the register might actually
12722 be dead; so we recourse, and the recursive call then finds
12723 the previous insn that used this register. */
12725 if (place && regno < FIRST_PSEUDO_REGISTER
12726 && hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))] > 1)
12728 unsigned int endregno = END_HARD_REGNO (XEXP (note, 0));
12729 int all_used = 1;
12730 unsigned int i;
12732 for (i = regno; i < endregno; i++)
12733 if ((! refers_to_regno_p (i, i + 1, PATTERN (place), 0)
12734 && ! find_regno_fusage (place, USE, i))
12735 || dead_or_set_regno_p (place, i))
12736 all_used = 0;
12738 if (! all_used)
12740 /* Put only REG_DEAD notes for pieces that are
12741 not already dead or set. */
12743 for (i = regno; i < endregno;
12744 i += hard_regno_nregs[i][reg_raw_mode[i]])
12746 rtx piece = regno_reg_rtx[i];
12747 basic_block bb = this_basic_block;
12749 if (! dead_or_set_p (place, piece)
12750 && ! reg_bitfield_target_p (piece,
12751 PATTERN (place)))
12753 rtx new_note
12754 = gen_rtx_EXPR_LIST (REG_DEAD, piece, NULL_RTX);
12756 distribute_notes (new_note, place, place,
12757 NULL_RTX, NULL_RTX, NULL_RTX);
12759 else if (! refers_to_regno_p (i, i + 1,
12760 PATTERN (place), 0)
12761 && ! find_regno_fusage (place, USE, i))
12762 for (tem = PREV_INSN (place); ;
12763 tem = PREV_INSN (tem))
12765 if (! INSN_P (tem))
12767 if (tem == BB_HEAD (bb))
12768 break;
12769 continue;
12771 if (dead_or_set_p (tem, piece)
12772 || reg_bitfield_target_p (piece,
12773 PATTERN (tem)))
12775 add_reg_note (tem, REG_UNUSED, piece);
12776 break;
12782 place = 0;
12786 break;
12788 default:
12789 /* Any other notes should not be present at this point in the
12790 compilation. */
12791 gcc_unreachable ();
12794 if (place)
12796 XEXP (note, 1) = REG_NOTES (place);
12797 REG_NOTES (place) = note;
12800 if (place2)
12801 REG_NOTES (place2)
12802 = gen_rtx_fmt_ee (GET_CODE (note), REG_NOTE_KIND (note),
12803 XEXP (note, 0), REG_NOTES (place2));
12807 /* Similarly to above, distribute the LOG_LINKS that used to be present on
12808 I3, I2, and I1 to new locations. This is also called to add a link
12809 pointing at I3 when I3's destination is changed. */
12811 static void
12812 distribute_links (rtx links)
12814 rtx link, next_link;
12816 for (link = links; link; link = next_link)
12818 rtx place = 0;
12819 rtx insn;
12820 rtx set, reg;
12822 next_link = XEXP (link, 1);
12824 /* If the insn that this link points to is a NOTE or isn't a single
12825 set, ignore it. In the latter case, it isn't clear what we
12826 can do other than ignore the link, since we can't tell which
12827 register it was for. Such links wouldn't be used by combine
12828 anyway.
12830 It is not possible for the destination of the target of the link to
12831 have been changed by combine. The only potential of this is if we
12832 replace I3, I2, and I1 by I3 and I2. But in that case the
12833 destination of I2 also remains unchanged. */
12835 if (NOTE_P (XEXP (link, 0))
12836 || (set = single_set (XEXP (link, 0))) == 0)
12837 continue;
12839 reg = SET_DEST (set);
12840 while (GET_CODE (reg) == SUBREG || GET_CODE (reg) == ZERO_EXTRACT
12841 || GET_CODE (reg) == STRICT_LOW_PART)
12842 reg = XEXP (reg, 0);
12844 /* A LOG_LINK is defined as being placed on the first insn that uses
12845 a register and points to the insn that sets the register. Start
12846 searching at the next insn after the target of the link and stop
12847 when we reach a set of the register or the end of the basic block.
12849 Note that this correctly handles the link that used to point from
12850 I3 to I2. Also note that not much searching is typically done here
12851 since most links don't point very far away. */
12853 for (insn = NEXT_INSN (XEXP (link, 0));
12854 (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR
12855 || BB_HEAD (this_basic_block->next_bb) != insn));
12856 insn = NEXT_INSN (insn))
12857 if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
12859 if (reg_referenced_p (reg, PATTERN (insn)))
12860 place = insn;
12861 break;
12863 else if (CALL_P (insn)
12864 && find_reg_fusage (insn, USE, reg))
12866 place = insn;
12867 break;
12869 else if (INSN_P (insn) && reg_set_p (reg, insn))
12870 break;
12872 /* If we found a place to put the link, place it there unless there
12873 is already a link to the same insn as LINK at that point. */
12875 if (place)
12877 rtx link2;
12879 for (link2 = LOG_LINKS (place); link2; link2 = XEXP (link2, 1))
12880 if (XEXP (link2, 0) == XEXP (link, 0))
12881 break;
12883 if (link2 == 0)
12885 XEXP (link, 1) = LOG_LINKS (place);
12886 LOG_LINKS (place) = link;
12888 /* Set added_links_insn to the earliest insn we added a
12889 link to. */
12890 if (added_links_insn == 0
12891 || DF_INSN_LUID (added_links_insn) > DF_INSN_LUID (place))
12892 added_links_insn = place;
12898 /* Subroutine of unmentioned_reg_p and callback from for_each_rtx.
12899 Check whether the expression pointer to by LOC is a register or
12900 memory, and if so return 1 if it isn't mentioned in the rtx EXPR.
12901 Otherwise return zero. */
12903 static int
12904 unmentioned_reg_p_1 (rtx *loc, void *expr)
12906 rtx x = *loc;
12908 if (x != NULL_RTX
12909 && (REG_P (x) || MEM_P (x))
12910 && ! reg_mentioned_p (x, (rtx) expr))
12911 return 1;
12912 return 0;
12915 /* Check for any register or memory mentioned in EQUIV that is not
12916 mentioned in EXPR. This is used to restrict EQUIV to "specializations"
12917 of EXPR where some registers may have been replaced by constants. */
12919 static bool
12920 unmentioned_reg_p (rtx equiv, rtx expr)
12922 return for_each_rtx (&equiv, unmentioned_reg_p_1, expr);
12925 void
12926 dump_combine_stats (FILE *file)
12928 fprintf
12929 (file,
12930 ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
12931 combine_attempts, combine_merges, combine_extras, combine_successes);
12934 void
12935 dump_combine_total_stats (FILE *file)
12937 fprintf
12938 (file,
12939 "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
12940 total_attempts, total_merges, total_extras, total_successes);
12943 static bool
12944 gate_handle_combine (void)
12946 return (optimize > 0);
12949 /* Try combining insns through substitution. */
12950 static unsigned int
12951 rest_of_handle_combine (void)
12953 int rebuild_jump_labels_after_combine;
12955 df_set_flags (DF_LR_RUN_DCE + DF_DEFER_INSN_RESCAN);
12956 df_note_add_problem ();
12957 df_analyze ();
12959 regstat_init_n_sets_and_refs ();
12961 rebuild_jump_labels_after_combine
12962 = combine_instructions (get_insns (), max_reg_num ());
12964 /* Combining insns may have turned an indirect jump into a
12965 direct jump. Rebuild the JUMP_LABEL fields of jumping
12966 instructions. */
12967 if (rebuild_jump_labels_after_combine)
12969 timevar_push (TV_JUMP);
12970 rebuild_jump_labels (get_insns ());
12971 cleanup_cfg (0);
12972 timevar_pop (TV_JUMP);
12975 regstat_free_n_sets_and_refs ();
12976 return 0;
12979 struct rtl_opt_pass pass_combine =
12982 RTL_PASS,
12983 "combine", /* name */
12984 gate_handle_combine, /* gate */
12985 rest_of_handle_combine, /* execute */
12986 NULL, /* sub */
12987 NULL, /* next */
12988 0, /* static_pass_number */
12989 TV_COMBINE, /* tv_id */
12990 0, /* properties_required */
12991 0, /* properties_provided */
12992 0, /* properties_destroyed */
12993 0, /* todo_flags_start */
12994 TODO_dump_func |
12995 TODO_df_finish | TODO_verify_rtl_sharing |
12996 TODO_ggc_collect, /* todo_flags_finish */