[ARM/AArch64][testsuite] Add vmax, vmin, vhadd, vhsub and vrhadd tests.
[official-gcc.git] / gcc / combine.c
blobad3bed0f2caf1ac9a9f6aa27da1c897f4dcdc70c
1 /* Optimize by combining instructions for GNU compiler.
2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* This module is essentially the "combiner" phase of the U. of Arizona
21 Portable Optimizer, but redone to work on our list-structured
22 representation for RTL instead of their string representation.
24 The LOG_LINKS of each insn identify the most recent assignment
25 to each REG used in the insn. It is a list of previous insns,
26 each of which contains a SET for a REG that is used in this insn
27 and not used or set in between. LOG_LINKs never cross basic blocks.
28 They were set up by the preceding pass (lifetime analysis).
30 We try to combine each pair of insns joined by a logical link.
31 We also try to combine triplets of insns A, B and C when C has
32 a link back to B and B has a link back to A. Likewise for a
33 small number of quadruplets of insns A, B, C and D for which
34 there's high likelihood of of success.
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 "hash-set.h"
84 #include "machmode.h"
85 #include "vec.h"
86 #include "double-int.h"
87 #include "input.h"
88 #include "alias.h"
89 #include "symtab.h"
90 #include "wide-int.h"
91 #include "inchash.h"
92 #include "tree.h"
93 #include "stor-layout.h"
94 #include "tm_p.h"
95 #include "flags.h"
96 #include "regs.h"
97 #include "hard-reg-set.h"
98 #include "predict.h"
99 #include "function.h"
100 #include "dominance.h"
101 #include "cfg.h"
102 #include "cfgrtl.h"
103 #include "cfgcleanup.h"
104 #include "basic-block.h"
105 #include "insn-config.h"
106 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
107 #include "hashtab.h"
108 #include "statistics.h"
109 #include "real.h"
110 #include "fixed-value.h"
111 #include "expmed.h"
112 #include "dojump.h"
113 #include "explow.h"
114 #include "calls.h"
115 #include "emit-rtl.h"
116 #include "varasm.h"
117 #include "stmt.h"
118 #include "expr.h"
119 #include "insn-attr.h"
120 #include "recog.h"
121 #include "diagnostic-core.h"
122 #include "target.h"
123 #include "insn-codes.h"
124 #include "optabs.h"
125 #include "rtlhooks-def.h"
126 #include "params.h"
127 #include "tree-pass.h"
128 #include "df.h"
129 #include "valtrack.h"
130 #include "hash-map.h"
131 #include "is-a.h"
132 #include "plugin-api.h"
133 #include "ipa-ref.h"
134 #include "cgraph.h"
135 #include "obstack.h"
136 #include "rtl-iter.h"
138 /* Number of attempts to combine instructions in this function. */
140 static int combine_attempts;
142 /* Number of attempts that got as far as substitution in this function. */
144 static int combine_merges;
146 /* Number of instructions combined with added SETs in this function. */
148 static int combine_extras;
150 /* Number of instructions combined in this function. */
152 static int combine_successes;
154 /* Totals over entire compilation. */
156 static int total_attempts, total_merges, total_extras, total_successes;
158 /* combine_instructions may try to replace the right hand side of the
159 second instruction with the value of an associated REG_EQUAL note
160 before throwing it at try_combine. That is problematic when there
161 is a REG_DEAD note for a register used in the old right hand side
162 and can cause distribute_notes to do wrong things. This is the
163 second instruction if it has been so modified, null otherwise. */
165 static rtx_insn *i2mod;
167 /* When I2MOD is nonnull, this is a copy of the old right hand side. */
169 static rtx i2mod_old_rhs;
171 /* When I2MOD is nonnull, this is a copy of the new right hand side. */
173 static rtx i2mod_new_rhs;
175 typedef struct reg_stat_struct {
176 /* Record last point of death of (hard or pseudo) register n. */
177 rtx_insn *last_death;
179 /* Record last point of modification of (hard or pseudo) register n. */
180 rtx_insn *last_set;
182 /* The next group of fields allows the recording of the last value assigned
183 to (hard or pseudo) register n. We use this information to see if an
184 operation being processed is redundant given a prior operation performed
185 on the register. For example, an `and' with a constant is redundant if
186 all the zero bits are already known to be turned off.
188 We use an approach similar to that used by cse, but change it in the
189 following ways:
191 (1) We do not want to reinitialize at each label.
192 (2) It is useful, but not critical, to know the actual value assigned
193 to a register. Often just its form is helpful.
195 Therefore, we maintain the following fields:
197 last_set_value the last value assigned
198 last_set_label records the value of label_tick when the
199 register was assigned
200 last_set_table_tick records the value of label_tick when a
201 value using the register is assigned
202 last_set_invalid set to nonzero when it is not valid
203 to use the value of this register in some
204 register's value
206 To understand the usage of these tables, it is important to understand
207 the distinction between the value in last_set_value being valid and
208 the register being validly contained in some other expression in the
209 table.
211 (The next two parameters are out of date).
213 reg_stat[i].last_set_value is valid if it is nonzero, and either
214 reg_n_sets[i] is 1 or reg_stat[i].last_set_label == label_tick.
216 Register I may validly appear in any expression returned for the value
217 of another register if reg_n_sets[i] is 1. It may also appear in the
218 value for register J if reg_stat[j].last_set_invalid is zero, or
219 reg_stat[i].last_set_label < reg_stat[j].last_set_label.
221 If an expression is found in the table containing a register which may
222 not validly appear in an expression, the register is replaced by
223 something that won't match, (clobber (const_int 0)). */
225 /* Record last value assigned to (hard or pseudo) register n. */
227 rtx last_set_value;
229 /* Record the value of label_tick when an expression involving register n
230 is placed in last_set_value. */
232 int last_set_table_tick;
234 /* Record the value of label_tick when the value for register n is placed in
235 last_set_value. */
237 int last_set_label;
239 /* These fields are maintained in parallel with last_set_value and are
240 used to store the mode in which the register was last set, the bits
241 that were known to be zero when it was last set, and the number of
242 sign bits copies it was known to have when it was last set. */
244 unsigned HOST_WIDE_INT last_set_nonzero_bits;
245 char last_set_sign_bit_copies;
246 ENUM_BITFIELD(machine_mode) last_set_mode : 8;
248 /* Set nonzero if references to register n in expressions should not be
249 used. last_set_invalid is set nonzero when this register is being
250 assigned to and last_set_table_tick == label_tick. */
252 char last_set_invalid;
254 /* Some registers that are set more than once and used in more than one
255 basic block are nevertheless always set in similar ways. For example,
256 a QImode register may be loaded from memory in two places on a machine
257 where byte loads zero extend.
259 We record in the following fields if a register has some leading bits
260 that are always equal to the sign bit, and what we know about the
261 nonzero bits of a register, specifically which bits are known to be
262 zero.
264 If an entry is zero, it means that we don't know anything special. */
266 unsigned char sign_bit_copies;
268 unsigned HOST_WIDE_INT nonzero_bits;
270 /* Record the value of the label_tick when the last truncation
271 happened. The field truncated_to_mode is only valid if
272 truncation_label == label_tick. */
274 int truncation_label;
276 /* Record the last truncation seen for this register. If truncation
277 is not a nop to this mode we might be able to save an explicit
278 truncation if we know that value already contains a truncated
279 value. */
281 ENUM_BITFIELD(machine_mode) truncated_to_mode : 8;
282 } reg_stat_type;
285 static vec<reg_stat_type> reg_stat;
287 /* Record the luid of the last insn that invalidated memory
288 (anything that writes memory, and subroutine calls, but not pushes). */
290 static int mem_last_set;
292 /* Record the luid of the last CALL_INSN
293 so we can tell whether a potential combination crosses any calls. */
295 static int last_call_luid;
297 /* When `subst' is called, this is the insn that is being modified
298 (by combining in a previous insn). The PATTERN of this insn
299 is still the old pattern partially modified and it should not be
300 looked at, but this may be used to examine the successors of the insn
301 to judge whether a simplification is valid. */
303 static rtx_insn *subst_insn;
305 /* This is the lowest LUID that `subst' is currently dealing with.
306 get_last_value will not return a value if the register was set at or
307 after this LUID. If not for this mechanism, we could get confused if
308 I2 or I1 in try_combine were an insn that used the old value of a register
309 to obtain a new value. In that case, we might erroneously get the
310 new value of the register when we wanted the old one. */
312 static int subst_low_luid;
314 /* This contains any hard registers that are used in newpat; reg_dead_at_p
315 must consider all these registers to be always live. */
317 static HARD_REG_SET newpat_used_regs;
319 /* This is an insn to which a LOG_LINKS entry has been added. If this
320 insn is the earlier than I2 or I3, combine should rescan starting at
321 that location. */
323 static rtx_insn *added_links_insn;
325 /* Basic block in which we are performing combines. */
326 static basic_block this_basic_block;
327 static bool optimize_this_for_speed_p;
330 /* Length of the currently allocated uid_insn_cost array. */
332 static int max_uid_known;
334 /* The following array records the insn_rtx_cost for every insn
335 in the instruction stream. */
337 static int *uid_insn_cost;
339 /* The following array records the LOG_LINKS for every insn in the
340 instruction stream as struct insn_link pointers. */
342 struct insn_link {
343 rtx_insn *insn;
344 unsigned int regno;
345 struct insn_link *next;
348 static struct insn_link **uid_log_links;
350 #define INSN_COST(INSN) (uid_insn_cost[INSN_UID (INSN)])
351 #define LOG_LINKS(INSN) (uid_log_links[INSN_UID (INSN)])
353 #define FOR_EACH_LOG_LINK(L, INSN) \
354 for ((L) = LOG_LINKS (INSN); (L); (L) = (L)->next)
356 /* Links for LOG_LINKS are allocated from this obstack. */
358 static struct obstack insn_link_obstack;
360 /* Allocate a link. */
362 static inline struct insn_link *
363 alloc_insn_link (rtx_insn *insn, unsigned int regno, struct insn_link *next)
365 struct insn_link *l
366 = (struct insn_link *) obstack_alloc (&insn_link_obstack,
367 sizeof (struct insn_link));
368 l->insn = insn;
369 l->regno = regno;
370 l->next = next;
371 return l;
374 /* Incremented for each basic block. */
376 static int label_tick;
378 /* Reset to label_tick for each extended basic block in scanning order. */
380 static int label_tick_ebb_start;
382 /* Mode used to compute significance in reg_stat[].nonzero_bits. It is the
383 largest integer mode that can fit in HOST_BITS_PER_WIDE_INT. */
385 static machine_mode nonzero_bits_mode;
387 /* Nonzero when reg_stat[].nonzero_bits and reg_stat[].sign_bit_copies can
388 be safely used. It is zero while computing them and after combine has
389 completed. This former test prevents propagating values based on
390 previously set values, which can be incorrect if a variable is modified
391 in a loop. */
393 static int nonzero_sign_valid;
396 /* Record one modification to rtl structure
397 to be undone by storing old_contents into *where. */
399 enum undo_kind { UNDO_RTX, UNDO_INT, UNDO_MODE, UNDO_LINKS };
401 struct undo
403 struct undo *next;
404 enum undo_kind kind;
405 union { rtx r; int i; machine_mode m; struct insn_link *l; } old_contents;
406 union { rtx *r; int *i; struct insn_link **l; } where;
409 /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
410 num_undo says how many are currently recorded.
412 other_insn is nonzero if we have modified some other insn in the process
413 of working on subst_insn. It must be verified too. */
415 struct undobuf
417 struct undo *undos;
418 struct undo *frees;
419 rtx_insn *other_insn;
422 static struct undobuf undobuf;
424 /* Number of times the pseudo being substituted for
425 was found and replaced. */
427 static int n_occurrences;
429 static rtx reg_nonzero_bits_for_combine (const_rtx, machine_mode, const_rtx,
430 machine_mode,
431 unsigned HOST_WIDE_INT,
432 unsigned HOST_WIDE_INT *);
433 static rtx reg_num_sign_bit_copies_for_combine (const_rtx, machine_mode, const_rtx,
434 machine_mode,
435 unsigned int, unsigned int *);
436 static void do_SUBST (rtx *, rtx);
437 static void do_SUBST_INT (int *, int);
438 static void init_reg_last (void);
439 static void setup_incoming_promotions (rtx_insn *);
440 static void set_nonzero_bits_and_sign_copies (rtx, const_rtx, void *);
441 static int cant_combine_insn_p (rtx_insn *);
442 static int can_combine_p (rtx_insn *, rtx_insn *, rtx_insn *, rtx_insn *,
443 rtx_insn *, rtx_insn *, rtx *, rtx *);
444 static int combinable_i3pat (rtx_insn *, rtx *, rtx, rtx, rtx, int, int, rtx *);
445 static int contains_muldiv (rtx);
446 static rtx_insn *try_combine (rtx_insn *, rtx_insn *, rtx_insn *, rtx_insn *,
447 int *, rtx_insn *);
448 static void undo_all (void);
449 static void undo_commit (void);
450 static rtx *find_split_point (rtx *, rtx_insn *, bool);
451 static rtx subst (rtx, rtx, rtx, int, int, int);
452 static rtx combine_simplify_rtx (rtx, machine_mode, int, int);
453 static rtx simplify_if_then_else (rtx);
454 static rtx simplify_set (rtx);
455 static rtx simplify_logical (rtx);
456 static rtx expand_compound_operation (rtx);
457 static const_rtx expand_field_assignment (const_rtx);
458 static rtx make_extraction (machine_mode, rtx, HOST_WIDE_INT,
459 rtx, unsigned HOST_WIDE_INT, int, int, int);
460 static rtx extract_left_shift (rtx, int);
461 static int get_pos_from_mask (unsigned HOST_WIDE_INT,
462 unsigned HOST_WIDE_INT *);
463 static rtx canon_reg_for_combine (rtx, rtx);
464 static rtx force_to_mode (rtx, machine_mode,
465 unsigned HOST_WIDE_INT, int);
466 static rtx if_then_else_cond (rtx, rtx *, rtx *);
467 static rtx known_cond (rtx, enum rtx_code, rtx, rtx);
468 static int rtx_equal_for_field_assignment_p (rtx, rtx);
469 static rtx make_field_assignment (rtx);
470 static rtx apply_distributive_law (rtx);
471 static rtx distribute_and_simplify_rtx (rtx, int);
472 static rtx simplify_and_const_int_1 (machine_mode, rtx,
473 unsigned HOST_WIDE_INT);
474 static rtx simplify_and_const_int (rtx, machine_mode, rtx,
475 unsigned HOST_WIDE_INT);
476 static int merge_outer_ops (enum rtx_code *, HOST_WIDE_INT *, enum rtx_code,
477 HOST_WIDE_INT, machine_mode, int *);
478 static rtx simplify_shift_const_1 (enum rtx_code, machine_mode, rtx, int);
479 static rtx simplify_shift_const (rtx, enum rtx_code, machine_mode, rtx,
480 int);
481 static int recog_for_combine (rtx *, rtx_insn *, rtx *);
482 static rtx gen_lowpart_for_combine (machine_mode, rtx);
483 static enum rtx_code simplify_compare_const (enum rtx_code, machine_mode,
484 rtx, rtx *);
485 static enum rtx_code simplify_comparison (enum rtx_code, rtx *, rtx *);
486 static void update_table_tick (rtx);
487 static void record_value_for_reg (rtx, rtx_insn *, rtx);
488 static void check_promoted_subreg (rtx_insn *, rtx);
489 static void record_dead_and_set_regs_1 (rtx, const_rtx, void *);
490 static void record_dead_and_set_regs (rtx_insn *);
491 static int get_last_value_validate (rtx *, rtx_insn *, int, int);
492 static rtx get_last_value (const_rtx);
493 static int use_crosses_set_p (const_rtx, int);
494 static void reg_dead_at_p_1 (rtx, const_rtx, void *);
495 static int reg_dead_at_p (rtx, rtx_insn *);
496 static void move_deaths (rtx, rtx, int, rtx_insn *, rtx *);
497 static int reg_bitfield_target_p (rtx, rtx);
498 static void distribute_notes (rtx, rtx_insn *, rtx_insn *, rtx_insn *, rtx, rtx, rtx);
499 static void distribute_links (struct insn_link *);
500 static void mark_used_regs_combine (rtx);
501 static void record_promoted_value (rtx_insn *, rtx);
502 static bool unmentioned_reg_p (rtx, rtx);
503 static void record_truncated_values (rtx *, void *);
504 static bool reg_truncated_to_mode (machine_mode, const_rtx);
505 static rtx gen_lowpart_or_truncate (machine_mode, rtx);
508 /* It is not safe to use ordinary gen_lowpart in combine.
509 See comments in gen_lowpart_for_combine. */
510 #undef RTL_HOOKS_GEN_LOWPART
511 #define RTL_HOOKS_GEN_LOWPART gen_lowpart_for_combine
513 /* Our implementation of gen_lowpart never emits a new pseudo. */
514 #undef RTL_HOOKS_GEN_LOWPART_NO_EMIT
515 #define RTL_HOOKS_GEN_LOWPART_NO_EMIT gen_lowpart_for_combine
517 #undef RTL_HOOKS_REG_NONZERO_REG_BITS
518 #define RTL_HOOKS_REG_NONZERO_REG_BITS reg_nonzero_bits_for_combine
520 #undef RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES
521 #define RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES reg_num_sign_bit_copies_for_combine
523 #undef RTL_HOOKS_REG_TRUNCATED_TO_MODE
524 #define RTL_HOOKS_REG_TRUNCATED_TO_MODE reg_truncated_to_mode
526 static const struct rtl_hooks combine_rtl_hooks = RTL_HOOKS_INITIALIZER;
529 /* Convenience wrapper for the canonicalize_comparison target hook.
530 Target hooks cannot use enum rtx_code. */
531 static inline void
532 target_canonicalize_comparison (enum rtx_code *code, rtx *op0, rtx *op1,
533 bool op0_preserve_value)
535 int code_int = (int)*code;
536 targetm.canonicalize_comparison (&code_int, op0, op1, op0_preserve_value);
537 *code = (enum rtx_code)code_int;
540 /* Try to split PATTERN found in INSN. This returns NULL_RTX if
541 PATTERN can not be split. Otherwise, it returns an insn sequence.
542 This is a wrapper around split_insns which ensures that the
543 reg_stat vector is made larger if the splitter creates a new
544 register. */
546 static rtx_insn *
547 combine_split_insns (rtx pattern, rtx insn)
549 rtx_insn *ret;
550 unsigned int nregs;
552 ret = safe_as_a <rtx_insn *> (split_insns (pattern, insn));
553 nregs = max_reg_num ();
554 if (nregs > reg_stat.length ())
555 reg_stat.safe_grow_cleared (nregs);
556 return ret;
559 /* This is used by find_single_use to locate an rtx in LOC that
560 contains exactly one use of DEST, which is typically either a REG
561 or CC0. It returns a pointer to the innermost rtx expression
562 containing DEST. Appearances of DEST that are being used to
563 totally replace it are not counted. */
565 static rtx *
566 find_single_use_1 (rtx dest, rtx *loc)
568 rtx x = *loc;
569 enum rtx_code code = GET_CODE (x);
570 rtx *result = NULL;
571 rtx *this_result;
572 int i;
573 const char *fmt;
575 switch (code)
577 case CONST:
578 case LABEL_REF:
579 case SYMBOL_REF:
580 CASE_CONST_ANY:
581 case CLOBBER:
582 return 0;
584 case SET:
585 /* If the destination is anything other than CC0, PC, a REG or a SUBREG
586 of a REG that occupies all of the REG, the insn uses DEST if
587 it is mentioned in the destination or the source. Otherwise, we
588 need just check the source. */
589 if (GET_CODE (SET_DEST (x)) != CC0
590 && GET_CODE (SET_DEST (x)) != PC
591 && !REG_P (SET_DEST (x))
592 && ! (GET_CODE (SET_DEST (x)) == SUBREG
593 && REG_P (SUBREG_REG (SET_DEST (x)))
594 && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
595 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
596 == ((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
597 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))))
598 break;
600 return find_single_use_1 (dest, &SET_SRC (x));
602 case MEM:
603 case SUBREG:
604 return find_single_use_1 (dest, &XEXP (x, 0));
606 default:
607 break;
610 /* If it wasn't one of the common cases above, check each expression and
611 vector of this code. Look for a unique usage of DEST. */
613 fmt = GET_RTX_FORMAT (code);
614 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
616 if (fmt[i] == 'e')
618 if (dest == XEXP (x, i)
619 || (REG_P (dest) && REG_P (XEXP (x, i))
620 && REGNO (dest) == REGNO (XEXP (x, i))))
621 this_result = loc;
622 else
623 this_result = find_single_use_1 (dest, &XEXP (x, i));
625 if (result == NULL)
626 result = this_result;
627 else if (this_result)
628 /* Duplicate usage. */
629 return NULL;
631 else if (fmt[i] == 'E')
633 int j;
635 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
637 if (XVECEXP (x, i, j) == dest
638 || (REG_P (dest)
639 && REG_P (XVECEXP (x, i, j))
640 && REGNO (XVECEXP (x, i, j)) == REGNO (dest)))
641 this_result = loc;
642 else
643 this_result = find_single_use_1 (dest, &XVECEXP (x, i, j));
645 if (result == NULL)
646 result = this_result;
647 else if (this_result)
648 return NULL;
653 return result;
657 /* See if DEST, produced in INSN, is used only a single time in the
658 sequel. If so, return a pointer to the innermost rtx expression in which
659 it is used.
661 If PLOC is nonzero, *PLOC is set to the insn containing the single use.
663 If DEST is cc0_rtx, we look only at the next insn. In that case, we don't
664 care about REG_DEAD notes or LOG_LINKS.
666 Otherwise, we find the single use by finding an insn that has a
667 LOG_LINKS pointing at INSN and has a REG_DEAD note for DEST. If DEST is
668 only referenced once in that insn, we know that it must be the first
669 and last insn referencing DEST. */
671 static rtx *
672 find_single_use (rtx dest, rtx_insn *insn, rtx_insn **ploc)
674 basic_block bb;
675 rtx_insn *next;
676 rtx *result;
677 struct insn_link *link;
679 #ifdef HAVE_cc0
680 if (dest == cc0_rtx)
682 next = NEXT_INSN (insn);
683 if (next == 0
684 || (!NONJUMP_INSN_P (next) && !JUMP_P (next)))
685 return 0;
687 result = find_single_use_1 (dest, &PATTERN (next));
688 if (result && ploc)
689 *ploc = next;
690 return result;
692 #endif
694 if (!REG_P (dest))
695 return 0;
697 bb = BLOCK_FOR_INSN (insn);
698 for (next = NEXT_INSN (insn);
699 next && BLOCK_FOR_INSN (next) == bb;
700 next = NEXT_INSN (next))
701 if (INSN_P (next) && dead_or_set_p (next, dest))
703 FOR_EACH_LOG_LINK (link, next)
704 if (link->insn == insn && link->regno == REGNO (dest))
705 break;
707 if (link)
709 result = find_single_use_1 (dest, &PATTERN (next));
710 if (ploc)
711 *ploc = next;
712 return result;
716 return 0;
719 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
720 insn. The substitution can be undone by undo_all. If INTO is already
721 set to NEWVAL, do not record this change. Because computing NEWVAL might
722 also call SUBST, we have to compute it before we put anything into
723 the undo table. */
725 static void
726 do_SUBST (rtx *into, rtx newval)
728 struct undo *buf;
729 rtx oldval = *into;
731 if (oldval == newval)
732 return;
734 /* We'd like to catch as many invalid transformations here as
735 possible. Unfortunately, there are way too many mode changes
736 that are perfectly valid, so we'd waste too much effort for
737 little gain doing the checks here. Focus on catching invalid
738 transformations involving integer constants. */
739 if (GET_MODE_CLASS (GET_MODE (oldval)) == MODE_INT
740 && CONST_INT_P (newval))
742 /* Sanity check that we're replacing oldval with a CONST_INT
743 that is a valid sign-extension for the original mode. */
744 gcc_assert (INTVAL (newval)
745 == trunc_int_for_mode (INTVAL (newval), GET_MODE (oldval)));
747 /* Replacing the operand of a SUBREG or a ZERO_EXTEND with a
748 CONST_INT is not valid, because after the replacement, the
749 original mode would be gone. Unfortunately, we can't tell
750 when do_SUBST is called to replace the operand thereof, so we
751 perform this test on oldval instead, checking whether an
752 invalid replacement took place before we got here. */
753 gcc_assert (!(GET_CODE (oldval) == SUBREG
754 && CONST_INT_P (SUBREG_REG (oldval))));
755 gcc_assert (!(GET_CODE (oldval) == ZERO_EXTEND
756 && CONST_INT_P (XEXP (oldval, 0))));
759 if (undobuf.frees)
760 buf = undobuf.frees, undobuf.frees = buf->next;
761 else
762 buf = XNEW (struct undo);
764 buf->kind = UNDO_RTX;
765 buf->where.r = into;
766 buf->old_contents.r = oldval;
767 *into = newval;
769 buf->next = undobuf.undos, undobuf.undos = buf;
772 #define SUBST(INTO, NEWVAL) do_SUBST (&(INTO), (NEWVAL))
774 /* Similar to SUBST, but NEWVAL is an int expression. Note that substitution
775 for the value of a HOST_WIDE_INT value (including CONST_INT) is
776 not safe. */
778 static void
779 do_SUBST_INT (int *into, int newval)
781 struct undo *buf;
782 int oldval = *into;
784 if (oldval == newval)
785 return;
787 if (undobuf.frees)
788 buf = undobuf.frees, undobuf.frees = buf->next;
789 else
790 buf = XNEW (struct undo);
792 buf->kind = UNDO_INT;
793 buf->where.i = into;
794 buf->old_contents.i = oldval;
795 *into = newval;
797 buf->next = undobuf.undos, undobuf.undos = buf;
800 #define SUBST_INT(INTO, NEWVAL) do_SUBST_INT (&(INTO), (NEWVAL))
802 /* Similar to SUBST, but just substitute the mode. This is used when
803 changing the mode of a pseudo-register, so that any other
804 references to the entry in the regno_reg_rtx array will change as
805 well. */
807 static void
808 do_SUBST_MODE (rtx *into, machine_mode newval)
810 struct undo *buf;
811 machine_mode oldval = GET_MODE (*into);
813 if (oldval == newval)
814 return;
816 if (undobuf.frees)
817 buf = undobuf.frees, undobuf.frees = buf->next;
818 else
819 buf = XNEW (struct undo);
821 buf->kind = UNDO_MODE;
822 buf->where.r = into;
823 buf->old_contents.m = oldval;
824 adjust_reg_mode (*into, newval);
826 buf->next = undobuf.undos, undobuf.undos = buf;
829 #define SUBST_MODE(INTO, NEWVAL) do_SUBST_MODE (&(INTO), (NEWVAL))
831 #ifndef HAVE_cc0
832 /* Similar to SUBST, but NEWVAL is a LOG_LINKS expression. */
834 static void
835 do_SUBST_LINK (struct insn_link **into, struct insn_link *newval)
837 struct undo *buf;
838 struct insn_link * oldval = *into;
840 if (oldval == newval)
841 return;
843 if (undobuf.frees)
844 buf = undobuf.frees, undobuf.frees = buf->next;
845 else
846 buf = XNEW (struct undo);
848 buf->kind = UNDO_LINKS;
849 buf->where.l = into;
850 buf->old_contents.l = oldval;
851 *into = newval;
853 buf->next = undobuf.undos, undobuf.undos = buf;
856 #define SUBST_LINK(oldval, newval) do_SUBST_LINK (&oldval, newval)
857 #endif
859 /* Subroutine of try_combine. Determine whether the replacement patterns
860 NEWPAT, NEWI2PAT and NEWOTHERPAT are cheaper according to insn_rtx_cost
861 than the original sequence I0, I1, I2, I3 and undobuf.other_insn. Note
862 that I0, I1 and/or NEWI2PAT may be NULL_RTX. Similarly, NEWOTHERPAT and
863 undobuf.other_insn may also both be NULL_RTX. Return false if the cost
864 of all the instructions can be estimated and the replacements are more
865 expensive than the original sequence. */
867 static bool
868 combine_validate_cost (rtx_insn *i0, rtx_insn *i1, rtx_insn *i2, rtx_insn *i3,
869 rtx newpat, rtx newi2pat, rtx newotherpat)
871 int i0_cost, i1_cost, i2_cost, i3_cost;
872 int new_i2_cost, new_i3_cost;
873 int old_cost, new_cost;
875 /* Lookup the original insn_rtx_costs. */
876 i2_cost = INSN_COST (i2);
877 i3_cost = INSN_COST (i3);
879 if (i1)
881 i1_cost = INSN_COST (i1);
882 if (i0)
884 i0_cost = INSN_COST (i0);
885 old_cost = (i0_cost > 0 && i1_cost > 0 && i2_cost > 0 && i3_cost > 0
886 ? i0_cost + i1_cost + i2_cost + i3_cost : 0);
888 else
890 old_cost = (i1_cost > 0 && i2_cost > 0 && i3_cost > 0
891 ? i1_cost + i2_cost + i3_cost : 0);
892 i0_cost = 0;
895 else
897 old_cost = (i2_cost > 0 && i3_cost > 0) ? i2_cost + i3_cost : 0;
898 i1_cost = i0_cost = 0;
901 /* If we have split a PARALLEL I2 to I1,I2, we have counted its cost twice;
902 correct that. */
903 if (old_cost && i1 && INSN_UID (i1) == INSN_UID (i2))
904 old_cost -= i1_cost;
907 /* Calculate the replacement insn_rtx_costs. */
908 new_i3_cost = insn_rtx_cost (newpat, optimize_this_for_speed_p);
909 if (newi2pat)
911 new_i2_cost = insn_rtx_cost (newi2pat, optimize_this_for_speed_p);
912 new_cost = (new_i2_cost > 0 && new_i3_cost > 0)
913 ? new_i2_cost + new_i3_cost : 0;
915 else
917 new_cost = new_i3_cost;
918 new_i2_cost = 0;
921 if (undobuf.other_insn)
923 int old_other_cost, new_other_cost;
925 old_other_cost = INSN_COST (undobuf.other_insn);
926 new_other_cost = insn_rtx_cost (newotherpat, optimize_this_for_speed_p);
927 if (old_other_cost > 0 && new_other_cost > 0)
929 old_cost += old_other_cost;
930 new_cost += new_other_cost;
932 else
933 old_cost = 0;
936 /* Disallow this combination if both new_cost and old_cost are greater than
937 zero, and new_cost is greater than old cost. */
938 int reject = old_cost > 0 && new_cost > old_cost;
940 if (dump_file)
942 fprintf (dump_file, "%s combination of insns ",
943 reject ? "rejecting" : "allowing");
944 if (i0)
945 fprintf (dump_file, "%d, ", INSN_UID (i0));
946 if (i1 && INSN_UID (i1) != INSN_UID (i2))
947 fprintf (dump_file, "%d, ", INSN_UID (i1));
948 fprintf (dump_file, "%d and %d\n", INSN_UID (i2), INSN_UID (i3));
950 fprintf (dump_file, "original costs ");
951 if (i0)
952 fprintf (dump_file, "%d + ", i0_cost);
953 if (i1 && INSN_UID (i1) != INSN_UID (i2))
954 fprintf (dump_file, "%d + ", i1_cost);
955 fprintf (dump_file, "%d + %d = %d\n", i2_cost, i3_cost, old_cost);
957 if (newi2pat)
958 fprintf (dump_file, "replacement costs %d + %d = %d\n",
959 new_i2_cost, new_i3_cost, new_cost);
960 else
961 fprintf (dump_file, "replacement cost %d\n", new_cost);
964 if (reject)
965 return false;
967 /* Update the uid_insn_cost array with the replacement costs. */
968 INSN_COST (i2) = new_i2_cost;
969 INSN_COST (i3) = new_i3_cost;
970 if (i1)
972 INSN_COST (i1) = 0;
973 if (i0)
974 INSN_COST (i0) = 0;
977 return true;
981 /* Delete any insns that copy a register to itself. */
983 static void
984 delete_noop_moves (void)
986 rtx_insn *insn, *next;
987 basic_block bb;
989 FOR_EACH_BB_FN (bb, cfun)
991 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb)); insn = next)
993 next = NEXT_INSN (insn);
994 if (INSN_P (insn) && noop_move_p (insn))
996 if (dump_file)
997 fprintf (dump_file, "deleting noop move %d\n", INSN_UID (insn));
999 delete_insn_and_edges (insn);
1006 /* Return false if we do not want to (or cannot) combine DEF. */
1007 static bool
1008 can_combine_def_p (df_ref def)
1010 /* Do not consider if it is pre/post modification in MEM. */
1011 if (DF_REF_FLAGS (def) & DF_REF_PRE_POST_MODIFY)
1012 return false;
1014 unsigned int regno = DF_REF_REGNO (def);
1016 /* Do not combine frame pointer adjustments. */
1017 if ((regno == FRAME_POINTER_REGNUM
1018 && (!reload_completed || frame_pointer_needed))
1019 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
1020 || (regno == HARD_FRAME_POINTER_REGNUM
1021 && (!reload_completed || frame_pointer_needed))
1022 #endif
1023 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1024 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
1025 #endif
1027 return false;
1029 return true;
1032 /* Return false if we do not want to (or cannot) combine USE. */
1033 static bool
1034 can_combine_use_p (df_ref use)
1036 /* Do not consider the usage of the stack pointer by function call. */
1037 if (DF_REF_FLAGS (use) & DF_REF_CALL_STACK_USAGE)
1038 return false;
1040 return true;
1043 /* Fill in log links field for all insns. */
1045 static void
1046 create_log_links (void)
1048 basic_block bb;
1049 rtx_insn **next_use;
1050 rtx_insn *insn;
1051 df_ref def, use;
1053 next_use = XCNEWVEC (rtx_insn *, max_reg_num ());
1055 /* Pass through each block from the end, recording the uses of each
1056 register and establishing log links when def is encountered.
1057 Note that we do not clear next_use array in order to save time,
1058 so we have to test whether the use is in the same basic block as def.
1060 There are a few cases below when we do not consider the definition or
1061 usage -- these are taken from original flow.c did. Don't ask me why it is
1062 done this way; I don't know and if it works, I don't want to know. */
1064 FOR_EACH_BB_FN (bb, cfun)
1066 FOR_BB_INSNS_REVERSE (bb, insn)
1068 if (!NONDEBUG_INSN_P (insn))
1069 continue;
1071 /* Log links are created only once. */
1072 gcc_assert (!LOG_LINKS (insn));
1074 FOR_EACH_INSN_DEF (def, insn)
1076 unsigned int regno = DF_REF_REGNO (def);
1077 rtx_insn *use_insn;
1079 if (!next_use[regno])
1080 continue;
1082 if (!can_combine_def_p (def))
1083 continue;
1085 use_insn = next_use[regno];
1086 next_use[regno] = NULL;
1088 if (BLOCK_FOR_INSN (use_insn) != bb)
1089 continue;
1091 /* flow.c claimed:
1093 We don't build a LOG_LINK for hard registers contained
1094 in ASM_OPERANDs. If these registers get replaced,
1095 we might wind up changing the semantics of the insn,
1096 even if reload can make what appear to be valid
1097 assignments later. */
1098 if (regno < FIRST_PSEUDO_REGISTER
1099 && asm_noperands (PATTERN (use_insn)) >= 0)
1100 continue;
1102 /* Don't add duplicate links between instructions. */
1103 struct insn_link *links;
1104 FOR_EACH_LOG_LINK (links, use_insn)
1105 if (insn == links->insn && regno == links->regno)
1106 break;
1108 if (!links)
1109 LOG_LINKS (use_insn)
1110 = alloc_insn_link (insn, regno, LOG_LINKS (use_insn));
1113 FOR_EACH_INSN_USE (use, insn)
1114 if (can_combine_use_p (use))
1115 next_use[DF_REF_REGNO (use)] = insn;
1119 free (next_use);
1122 /* Walk the LOG_LINKS of insn B to see if we find a reference to A. Return
1123 true if we found a LOG_LINK that proves that A feeds B. This only works
1124 if there are no instructions between A and B which could have a link
1125 depending on A, since in that case we would not record a link for B.
1126 We also check the implicit dependency created by a cc0 setter/user
1127 pair. */
1129 static bool
1130 insn_a_feeds_b (rtx_insn *a, rtx_insn *b)
1132 struct insn_link *links;
1133 FOR_EACH_LOG_LINK (links, b)
1134 if (links->insn == a)
1135 return true;
1136 #ifdef HAVE_cc0
1137 if (sets_cc0_p (a))
1138 return true;
1139 #endif
1140 return false;
1143 /* Main entry point for combiner. F is the first insn of the function.
1144 NREGS is the first unused pseudo-reg number.
1146 Return nonzero if the combiner has turned an indirect jump
1147 instruction into a direct jump. */
1148 static int
1149 combine_instructions (rtx_insn *f, unsigned int nregs)
1151 rtx_insn *insn, *next;
1152 #ifdef HAVE_cc0
1153 rtx_insn *prev;
1154 #endif
1155 struct insn_link *links, *nextlinks;
1156 rtx_insn *first;
1157 basic_block last_bb;
1159 int new_direct_jump_p = 0;
1161 for (first = f; first && !INSN_P (first); )
1162 first = NEXT_INSN (first);
1163 if (!first)
1164 return 0;
1166 combine_attempts = 0;
1167 combine_merges = 0;
1168 combine_extras = 0;
1169 combine_successes = 0;
1171 rtl_hooks = combine_rtl_hooks;
1173 reg_stat.safe_grow_cleared (nregs);
1175 init_recog_no_volatile ();
1177 /* Allocate array for insn info. */
1178 max_uid_known = get_max_uid ();
1179 uid_log_links = XCNEWVEC (struct insn_link *, max_uid_known + 1);
1180 uid_insn_cost = XCNEWVEC (int, max_uid_known + 1);
1181 gcc_obstack_init (&insn_link_obstack);
1183 nonzero_bits_mode = mode_for_size (HOST_BITS_PER_WIDE_INT, MODE_INT, 0);
1185 /* Don't use reg_stat[].nonzero_bits when computing it. This can cause
1186 problems when, for example, we have j <<= 1 in a loop. */
1188 nonzero_sign_valid = 0;
1189 label_tick = label_tick_ebb_start = 1;
1191 /* Scan all SETs and see if we can deduce anything about what
1192 bits are known to be zero for some registers and how many copies
1193 of the sign bit are known to exist for those registers.
1195 Also set any known values so that we can use it while searching
1196 for what bits are known to be set. */
1198 setup_incoming_promotions (first);
1199 /* Allow the entry block and the first block to fall into the same EBB.
1200 Conceptually the incoming promotions are assigned to the entry block. */
1201 last_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1203 create_log_links ();
1204 FOR_EACH_BB_FN (this_basic_block, cfun)
1206 optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1207 last_call_luid = 0;
1208 mem_last_set = -1;
1210 label_tick++;
1211 if (!single_pred_p (this_basic_block)
1212 || single_pred (this_basic_block) != last_bb)
1213 label_tick_ebb_start = label_tick;
1214 last_bb = this_basic_block;
1216 FOR_BB_INSNS (this_basic_block, insn)
1217 if (INSN_P (insn) && BLOCK_FOR_INSN (insn))
1219 #ifdef AUTO_INC_DEC
1220 rtx links;
1221 #endif
1223 subst_low_luid = DF_INSN_LUID (insn);
1224 subst_insn = insn;
1226 note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies,
1227 insn);
1228 record_dead_and_set_regs (insn);
1230 #ifdef AUTO_INC_DEC
1231 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
1232 if (REG_NOTE_KIND (links) == REG_INC)
1233 set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX,
1234 insn);
1235 #endif
1237 /* Record the current insn_rtx_cost of this instruction. */
1238 if (NONJUMP_INSN_P (insn))
1239 INSN_COST (insn) = insn_rtx_cost (PATTERN (insn),
1240 optimize_this_for_speed_p);
1241 if (dump_file)
1242 fprintf (dump_file, "insn_cost %d: %d\n",
1243 INSN_UID (insn), INSN_COST (insn));
1247 nonzero_sign_valid = 1;
1249 /* Now scan all the insns in forward order. */
1250 label_tick = label_tick_ebb_start = 1;
1251 init_reg_last ();
1252 setup_incoming_promotions (first);
1253 last_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1254 int max_combine = PARAM_VALUE (PARAM_MAX_COMBINE_INSNS);
1256 FOR_EACH_BB_FN (this_basic_block, cfun)
1258 rtx_insn *last_combined_insn = NULL;
1259 optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1260 last_call_luid = 0;
1261 mem_last_set = -1;
1263 label_tick++;
1264 if (!single_pred_p (this_basic_block)
1265 || single_pred (this_basic_block) != last_bb)
1266 label_tick_ebb_start = label_tick;
1267 last_bb = this_basic_block;
1269 rtl_profile_for_bb (this_basic_block);
1270 for (insn = BB_HEAD (this_basic_block);
1271 insn != NEXT_INSN (BB_END (this_basic_block));
1272 insn = next ? next : NEXT_INSN (insn))
1274 next = 0;
1275 if (!NONDEBUG_INSN_P (insn))
1276 continue;
1278 while (last_combined_insn
1279 && last_combined_insn->deleted ())
1280 last_combined_insn = PREV_INSN (last_combined_insn);
1281 if (last_combined_insn == NULL_RTX
1282 || BARRIER_P (last_combined_insn)
1283 || BLOCK_FOR_INSN (last_combined_insn) != this_basic_block
1284 || DF_INSN_LUID (last_combined_insn) <= DF_INSN_LUID (insn))
1285 last_combined_insn = insn;
1287 /* See if we know about function return values before this
1288 insn based upon SUBREG flags. */
1289 check_promoted_subreg (insn, PATTERN (insn));
1291 /* See if we can find hardregs and subreg of pseudos in
1292 narrower modes. This could help turning TRUNCATEs
1293 into SUBREGs. */
1294 note_uses (&PATTERN (insn), record_truncated_values, NULL);
1296 /* Try this insn with each insn it links back to. */
1298 FOR_EACH_LOG_LINK (links, insn)
1299 if ((next = try_combine (insn, links->insn, NULL,
1300 NULL, &new_direct_jump_p,
1301 last_combined_insn)) != 0)
1303 statistics_counter_event (cfun, "two-insn combine", 1);
1304 goto retry;
1307 /* Try each sequence of three linked insns ending with this one. */
1309 if (max_combine >= 3)
1310 FOR_EACH_LOG_LINK (links, insn)
1312 rtx_insn *link = links->insn;
1314 /* If the linked insn has been replaced by a note, then there
1315 is no point in pursuing this chain any further. */
1316 if (NOTE_P (link))
1317 continue;
1319 FOR_EACH_LOG_LINK (nextlinks, link)
1320 if ((next = try_combine (insn, link, nextlinks->insn,
1321 NULL, &new_direct_jump_p,
1322 last_combined_insn)) != 0)
1324 statistics_counter_event (cfun, "three-insn combine", 1);
1325 goto retry;
1329 #ifdef HAVE_cc0
1330 /* Try to combine a jump insn that uses CC0
1331 with a preceding insn that sets CC0, and maybe with its
1332 logical predecessor as well.
1333 This is how we make decrement-and-branch insns.
1334 We need this special code because data flow connections
1335 via CC0 do not get entered in LOG_LINKS. */
1337 if (JUMP_P (insn)
1338 && (prev = prev_nonnote_insn (insn)) != 0
1339 && NONJUMP_INSN_P (prev)
1340 && sets_cc0_p (PATTERN (prev)))
1342 if ((next = try_combine (insn, prev, NULL, NULL,
1343 &new_direct_jump_p,
1344 last_combined_insn)) != 0)
1345 goto retry;
1347 FOR_EACH_LOG_LINK (nextlinks, prev)
1348 if ((next = try_combine (insn, prev, nextlinks->insn,
1349 NULL, &new_direct_jump_p,
1350 last_combined_insn)) != 0)
1351 goto retry;
1354 /* Do the same for an insn that explicitly references CC0. */
1355 if (NONJUMP_INSN_P (insn)
1356 && (prev = prev_nonnote_insn (insn)) != 0
1357 && NONJUMP_INSN_P (prev)
1358 && sets_cc0_p (PATTERN (prev))
1359 && GET_CODE (PATTERN (insn)) == SET
1360 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
1362 if ((next = try_combine (insn, prev, NULL, NULL,
1363 &new_direct_jump_p,
1364 last_combined_insn)) != 0)
1365 goto retry;
1367 FOR_EACH_LOG_LINK (nextlinks, prev)
1368 if ((next = try_combine (insn, prev, nextlinks->insn,
1369 NULL, &new_direct_jump_p,
1370 last_combined_insn)) != 0)
1371 goto retry;
1374 /* Finally, see if any of the insns that this insn links to
1375 explicitly references CC0. If so, try this insn, that insn,
1376 and its predecessor if it sets CC0. */
1377 FOR_EACH_LOG_LINK (links, insn)
1378 if (NONJUMP_INSN_P (links->insn)
1379 && GET_CODE (PATTERN (links->insn)) == SET
1380 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (links->insn)))
1381 && (prev = prev_nonnote_insn (links->insn)) != 0
1382 && NONJUMP_INSN_P (prev)
1383 && sets_cc0_p (PATTERN (prev))
1384 && (next = try_combine (insn, links->insn,
1385 prev, NULL, &new_direct_jump_p,
1386 last_combined_insn)) != 0)
1387 goto retry;
1388 #endif
1390 /* Try combining an insn with two different insns whose results it
1391 uses. */
1392 if (max_combine >= 3)
1393 FOR_EACH_LOG_LINK (links, insn)
1394 for (nextlinks = links->next; nextlinks;
1395 nextlinks = nextlinks->next)
1396 if ((next = try_combine (insn, links->insn,
1397 nextlinks->insn, NULL,
1398 &new_direct_jump_p,
1399 last_combined_insn)) != 0)
1402 statistics_counter_event (cfun, "three-insn combine", 1);
1403 goto retry;
1406 /* Try four-instruction combinations. */
1407 if (max_combine >= 4)
1408 FOR_EACH_LOG_LINK (links, insn)
1410 struct insn_link *next1;
1411 rtx_insn *link = links->insn;
1413 /* If the linked insn has been replaced by a note, then there
1414 is no point in pursuing this chain any further. */
1415 if (NOTE_P (link))
1416 continue;
1418 FOR_EACH_LOG_LINK (next1, link)
1420 rtx_insn *link1 = next1->insn;
1421 if (NOTE_P (link1))
1422 continue;
1423 /* I0 -> I1 -> I2 -> I3. */
1424 FOR_EACH_LOG_LINK (nextlinks, link1)
1425 if ((next = try_combine (insn, link, link1,
1426 nextlinks->insn,
1427 &new_direct_jump_p,
1428 last_combined_insn)) != 0)
1430 statistics_counter_event (cfun, "four-insn combine", 1);
1431 goto retry;
1433 /* I0, I1 -> I2, I2 -> I3. */
1434 for (nextlinks = next1->next; nextlinks;
1435 nextlinks = nextlinks->next)
1436 if ((next = try_combine (insn, link, link1,
1437 nextlinks->insn,
1438 &new_direct_jump_p,
1439 last_combined_insn)) != 0)
1441 statistics_counter_event (cfun, "four-insn combine", 1);
1442 goto retry;
1446 for (next1 = links->next; next1; next1 = next1->next)
1448 rtx_insn *link1 = next1->insn;
1449 if (NOTE_P (link1))
1450 continue;
1451 /* I0 -> I2; I1, I2 -> I3. */
1452 FOR_EACH_LOG_LINK (nextlinks, link)
1453 if ((next = try_combine (insn, link, link1,
1454 nextlinks->insn,
1455 &new_direct_jump_p,
1456 last_combined_insn)) != 0)
1458 statistics_counter_event (cfun, "four-insn combine", 1);
1459 goto retry;
1461 /* I0 -> I1; I1, I2 -> I3. */
1462 FOR_EACH_LOG_LINK (nextlinks, link1)
1463 if ((next = try_combine (insn, link, link1,
1464 nextlinks->insn,
1465 &new_direct_jump_p,
1466 last_combined_insn)) != 0)
1468 statistics_counter_event (cfun, "four-insn combine", 1);
1469 goto retry;
1474 /* Try this insn with each REG_EQUAL note it links back to. */
1475 FOR_EACH_LOG_LINK (links, insn)
1477 rtx set, note;
1478 rtx_insn *temp = links->insn;
1479 if ((set = single_set (temp)) != 0
1480 && (note = find_reg_equal_equiv_note (temp)) != 0
1481 && (note = XEXP (note, 0), GET_CODE (note)) != EXPR_LIST
1482 /* Avoid using a register that may already been marked
1483 dead by an earlier instruction. */
1484 && ! unmentioned_reg_p (note, SET_SRC (set))
1485 && (GET_MODE (note) == VOIDmode
1486 ? SCALAR_INT_MODE_P (GET_MODE (SET_DEST (set)))
1487 : GET_MODE (SET_DEST (set)) == GET_MODE (note)))
1489 /* Temporarily replace the set's source with the
1490 contents of the REG_EQUAL note. The insn will
1491 be deleted or recognized by try_combine. */
1492 rtx orig = SET_SRC (set);
1493 SET_SRC (set) = note;
1494 i2mod = temp;
1495 i2mod_old_rhs = copy_rtx (orig);
1496 i2mod_new_rhs = copy_rtx (note);
1497 next = try_combine (insn, i2mod, NULL, NULL,
1498 &new_direct_jump_p,
1499 last_combined_insn);
1500 i2mod = NULL;
1501 if (next)
1503 statistics_counter_event (cfun, "insn-with-note combine", 1);
1504 goto retry;
1506 SET_SRC (set) = orig;
1510 if (!NOTE_P (insn))
1511 record_dead_and_set_regs (insn);
1513 retry:
1518 default_rtl_profile ();
1519 clear_bb_flags ();
1520 new_direct_jump_p |= purge_all_dead_edges ();
1521 delete_noop_moves ();
1523 /* Clean up. */
1524 obstack_free (&insn_link_obstack, NULL);
1525 free (uid_log_links);
1526 free (uid_insn_cost);
1527 reg_stat.release ();
1530 struct undo *undo, *next;
1531 for (undo = undobuf.frees; undo; undo = next)
1533 next = undo->next;
1534 free (undo);
1536 undobuf.frees = 0;
1539 total_attempts += combine_attempts;
1540 total_merges += combine_merges;
1541 total_extras += combine_extras;
1542 total_successes += combine_successes;
1544 nonzero_sign_valid = 0;
1545 rtl_hooks = general_rtl_hooks;
1547 /* Make recognizer allow volatile MEMs again. */
1548 init_recog ();
1550 return new_direct_jump_p;
1553 /* Wipe the last_xxx fields of reg_stat in preparation for another pass. */
1555 static void
1556 init_reg_last (void)
1558 unsigned int i;
1559 reg_stat_type *p;
1561 FOR_EACH_VEC_ELT (reg_stat, i, p)
1562 memset (p, 0, offsetof (reg_stat_type, sign_bit_copies));
1565 /* Set up any promoted values for incoming argument registers. */
1567 static void
1568 setup_incoming_promotions (rtx_insn *first)
1570 tree arg;
1571 bool strictly_local = false;
1573 for (arg = DECL_ARGUMENTS (current_function_decl); arg;
1574 arg = DECL_CHAIN (arg))
1576 rtx x, reg = DECL_INCOMING_RTL (arg);
1577 int uns1, uns3;
1578 machine_mode mode1, mode2, mode3, mode4;
1580 /* Only continue if the incoming argument is in a register. */
1581 if (!REG_P (reg))
1582 continue;
1584 /* Determine, if possible, whether all call sites of the current
1585 function lie within the current compilation unit. (This does
1586 take into account the exporting of a function via taking its
1587 address, and so forth.) */
1588 strictly_local = cgraph_node::local_info (current_function_decl)->local;
1590 /* The mode and signedness of the argument before any promotions happen
1591 (equal to the mode of the pseudo holding it at that stage). */
1592 mode1 = TYPE_MODE (TREE_TYPE (arg));
1593 uns1 = TYPE_UNSIGNED (TREE_TYPE (arg));
1595 /* The mode and signedness of the argument after any source language and
1596 TARGET_PROMOTE_PROTOTYPES-driven promotions. */
1597 mode2 = TYPE_MODE (DECL_ARG_TYPE (arg));
1598 uns3 = TYPE_UNSIGNED (DECL_ARG_TYPE (arg));
1600 /* The mode and signedness of the argument as it is actually passed,
1601 see assign_parm_setup_reg in function.c. */
1602 mode3 = promote_function_mode (TREE_TYPE (arg), mode1, &uns3,
1603 TREE_TYPE (cfun->decl), 0);
1605 /* The mode of the register in which the argument is being passed. */
1606 mode4 = GET_MODE (reg);
1608 /* Eliminate sign extensions in the callee when:
1609 (a) A mode promotion has occurred; */
1610 if (mode1 == mode3)
1611 continue;
1612 /* (b) The mode of the register is the same as the mode of
1613 the argument as it is passed; */
1614 if (mode3 != mode4)
1615 continue;
1616 /* (c) There's no language level extension; */
1617 if (mode1 == mode2)
1619 /* (c.1) All callers are from the current compilation unit. If that's
1620 the case we don't have to rely on an ABI, we only have to know
1621 what we're generating right now, and we know that we will do the
1622 mode1 to mode2 promotion with the given sign. */
1623 else if (!strictly_local)
1624 continue;
1625 /* (c.2) The combination of the two promotions is useful. This is
1626 true when the signs match, or if the first promotion is unsigned.
1627 In the later case, (sign_extend (zero_extend x)) is the same as
1628 (zero_extend (zero_extend x)), so make sure to force UNS3 true. */
1629 else if (uns1)
1630 uns3 = true;
1631 else if (uns3)
1632 continue;
1634 /* Record that the value was promoted from mode1 to mode3,
1635 so that any sign extension at the head of the current
1636 function may be eliminated. */
1637 x = gen_rtx_CLOBBER (mode1, const0_rtx);
1638 x = gen_rtx_fmt_e ((uns3 ? ZERO_EXTEND : SIGN_EXTEND), mode3, x);
1639 record_value_for_reg (reg, first, x);
1643 /* Called via note_stores. If X is a pseudo that is narrower than
1644 HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
1646 If we are setting only a portion of X and we can't figure out what
1647 portion, assume all bits will be used since we don't know what will
1648 be happening.
1650 Similarly, set how many bits of X are known to be copies of the sign bit
1651 at all locations in the function. This is the smallest number implied
1652 by any set of X. */
1654 static void
1655 set_nonzero_bits_and_sign_copies (rtx x, const_rtx set, void *data)
1657 rtx_insn *insn = (rtx_insn *) data;
1658 unsigned int num;
1660 if (REG_P (x)
1661 && REGNO (x) >= FIRST_PSEUDO_REGISTER
1662 /* If this register is undefined at the start of the file, we can't
1663 say what its contents were. */
1664 && ! REGNO_REG_SET_P
1665 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb), REGNO (x))
1666 && HWI_COMPUTABLE_MODE_P (GET_MODE (x)))
1668 reg_stat_type *rsp = &reg_stat[REGNO (x)];
1670 if (set == 0 || GET_CODE (set) == CLOBBER)
1672 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1673 rsp->sign_bit_copies = 1;
1674 return;
1677 /* If this register is being initialized using itself, and the
1678 register is uninitialized in this basic block, and there are
1679 no LOG_LINKS which set the register, then part of the
1680 register is uninitialized. In that case we can't assume
1681 anything about the number of nonzero bits.
1683 ??? We could do better if we checked this in
1684 reg_{nonzero_bits,num_sign_bit_copies}_for_combine. Then we
1685 could avoid making assumptions about the insn which initially
1686 sets the register, while still using the information in other
1687 insns. We would have to be careful to check every insn
1688 involved in the combination. */
1690 if (insn
1691 && reg_referenced_p (x, PATTERN (insn))
1692 && !REGNO_REG_SET_P (DF_LR_IN (BLOCK_FOR_INSN (insn)),
1693 REGNO (x)))
1695 struct insn_link *link;
1697 FOR_EACH_LOG_LINK (link, insn)
1698 if (dead_or_set_p (link->insn, x))
1699 break;
1700 if (!link)
1702 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1703 rsp->sign_bit_copies = 1;
1704 return;
1708 /* If this is a complex assignment, see if we can convert it into a
1709 simple assignment. */
1710 set = expand_field_assignment (set);
1712 /* If this is a simple assignment, or we have a paradoxical SUBREG,
1713 set what we know about X. */
1715 if (SET_DEST (set) == x
1716 || (paradoxical_subreg_p (SET_DEST (set))
1717 && SUBREG_REG (SET_DEST (set)) == x))
1719 rtx src = SET_SRC (set);
1721 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
1722 /* If X is narrower than a word and SRC is a non-negative
1723 constant that would appear negative in the mode of X,
1724 sign-extend it for use in reg_stat[].nonzero_bits because some
1725 machines (maybe most) will actually do the sign-extension
1726 and this is the conservative approach.
1728 ??? For 2.5, try to tighten up the MD files in this regard
1729 instead of this kludge. */
1731 if (GET_MODE_PRECISION (GET_MODE (x)) < BITS_PER_WORD
1732 && CONST_INT_P (src)
1733 && INTVAL (src) > 0
1734 && val_signbit_known_set_p (GET_MODE (x), INTVAL (src)))
1735 src = GEN_INT (INTVAL (src) | ~GET_MODE_MASK (GET_MODE (x)));
1736 #endif
1738 /* Don't call nonzero_bits if it cannot change anything. */
1739 if (rsp->nonzero_bits != ~(unsigned HOST_WIDE_INT) 0)
1740 rsp->nonzero_bits |= nonzero_bits (src, nonzero_bits_mode);
1741 num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
1742 if (rsp->sign_bit_copies == 0
1743 || rsp->sign_bit_copies > num)
1744 rsp->sign_bit_copies = num;
1746 else
1748 rsp->nonzero_bits = GET_MODE_MASK (GET_MODE (x));
1749 rsp->sign_bit_copies = 1;
1754 /* See if INSN can be combined into I3. PRED, PRED2, SUCC and SUCC2 are
1755 optionally insns that were previously combined into I3 or that will be
1756 combined into the merger of INSN and I3. The order is PRED, PRED2,
1757 INSN, SUCC, SUCC2, I3.
1759 Return 0 if the combination is not allowed for any reason.
1761 If the combination is allowed, *PDEST will be set to the single
1762 destination of INSN and *PSRC to the single source, and this function
1763 will return 1. */
1765 static int
1766 can_combine_p (rtx_insn *insn, rtx_insn *i3, rtx_insn *pred ATTRIBUTE_UNUSED,
1767 rtx_insn *pred2 ATTRIBUTE_UNUSED, rtx_insn *succ, rtx_insn *succ2,
1768 rtx *pdest, rtx *psrc)
1770 int i;
1771 const_rtx set = 0;
1772 rtx src, dest;
1773 rtx_insn *p;
1774 #ifdef AUTO_INC_DEC
1775 rtx link;
1776 #endif
1777 bool all_adjacent = true;
1778 int (*is_volatile_p) (const_rtx);
1780 if (succ)
1782 if (succ2)
1784 if (next_active_insn (succ2) != i3)
1785 all_adjacent = false;
1786 if (next_active_insn (succ) != succ2)
1787 all_adjacent = false;
1789 else if (next_active_insn (succ) != i3)
1790 all_adjacent = false;
1791 if (next_active_insn (insn) != succ)
1792 all_adjacent = false;
1794 else if (next_active_insn (insn) != i3)
1795 all_adjacent = false;
1797 /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
1798 or a PARALLEL consisting of such a SET and CLOBBERs.
1800 If INSN has CLOBBER parallel parts, ignore them for our processing.
1801 By definition, these happen during the execution of the insn. When it
1802 is merged with another insn, all bets are off. If they are, in fact,
1803 needed and aren't also supplied in I3, they may be added by
1804 recog_for_combine. Otherwise, it won't match.
1806 We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
1807 note.
1809 Get the source and destination of INSN. If more than one, can't
1810 combine. */
1812 if (GET_CODE (PATTERN (insn)) == SET)
1813 set = PATTERN (insn);
1814 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1815 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1817 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1819 rtx elt = XVECEXP (PATTERN (insn), 0, i);
1821 switch (GET_CODE (elt))
1823 /* This is important to combine floating point insns
1824 for the SH4 port. */
1825 case USE:
1826 /* Combining an isolated USE doesn't make sense.
1827 We depend here on combinable_i3pat to reject them. */
1828 /* The code below this loop only verifies that the inputs of
1829 the SET in INSN do not change. We call reg_set_between_p
1830 to verify that the REG in the USE does not change between
1831 I3 and INSN.
1832 If the USE in INSN was for a pseudo register, the matching
1833 insn pattern will likely match any register; combining this
1834 with any other USE would only be safe if we knew that the
1835 used registers have identical values, or if there was
1836 something to tell them apart, e.g. different modes. For
1837 now, we forgo such complicated tests and simply disallow
1838 combining of USES of pseudo registers with any other USE. */
1839 if (REG_P (XEXP (elt, 0))
1840 && GET_CODE (PATTERN (i3)) == PARALLEL)
1842 rtx i3pat = PATTERN (i3);
1843 int i = XVECLEN (i3pat, 0) - 1;
1844 unsigned int regno = REGNO (XEXP (elt, 0));
1848 rtx i3elt = XVECEXP (i3pat, 0, i);
1850 if (GET_CODE (i3elt) == USE
1851 && REG_P (XEXP (i3elt, 0))
1852 && (REGNO (XEXP (i3elt, 0)) == regno
1853 ? reg_set_between_p (XEXP (elt, 0),
1854 PREV_INSN (insn), i3)
1855 : regno >= FIRST_PSEUDO_REGISTER))
1856 return 0;
1858 while (--i >= 0);
1860 break;
1862 /* We can ignore CLOBBERs. */
1863 case CLOBBER:
1864 break;
1866 case SET:
1867 /* Ignore SETs whose result isn't used but not those that
1868 have side-effects. */
1869 if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
1870 && insn_nothrow_p (insn)
1871 && !side_effects_p (elt))
1872 break;
1874 /* If we have already found a SET, this is a second one and
1875 so we cannot combine with this insn. */
1876 if (set)
1877 return 0;
1879 set = elt;
1880 break;
1882 default:
1883 /* Anything else means we can't combine. */
1884 return 0;
1888 if (set == 0
1889 /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
1890 so don't do anything with it. */
1891 || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
1892 return 0;
1894 else
1895 return 0;
1897 if (set == 0)
1898 return 0;
1900 /* The simplification in expand_field_assignment may call back to
1901 get_last_value, so set safe guard here. */
1902 subst_low_luid = DF_INSN_LUID (insn);
1904 set = expand_field_assignment (set);
1905 src = SET_SRC (set), dest = SET_DEST (set);
1907 /* Don't eliminate a store in the stack pointer. */
1908 if (dest == stack_pointer_rtx
1909 /* Don't combine with an insn that sets a register to itself if it has
1910 a REG_EQUAL note. This may be part of a LIBCALL sequence. */
1911 || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1912 /* Can't merge an ASM_OPERANDS. */
1913 || GET_CODE (src) == ASM_OPERANDS
1914 /* Can't merge a function call. */
1915 || GET_CODE (src) == CALL
1916 /* Don't eliminate a function call argument. */
1917 || (CALL_P (i3)
1918 && (find_reg_fusage (i3, USE, dest)
1919 || (REG_P (dest)
1920 && REGNO (dest) < FIRST_PSEUDO_REGISTER
1921 && global_regs[REGNO (dest)])))
1922 /* Don't substitute into an incremented register. */
1923 || FIND_REG_INC_NOTE (i3, dest)
1924 || (succ && FIND_REG_INC_NOTE (succ, dest))
1925 || (succ2 && FIND_REG_INC_NOTE (succ2, dest))
1926 /* Don't substitute into a non-local goto, this confuses CFG. */
1927 || (JUMP_P (i3) && find_reg_note (i3, REG_NON_LOCAL_GOTO, NULL_RTX))
1928 /* Make sure that DEST is not used after SUCC but before I3. */
1929 || (!all_adjacent
1930 && ((succ2
1931 && (reg_used_between_p (dest, succ2, i3)
1932 || reg_used_between_p (dest, succ, succ2)))
1933 || (!succ2 && succ && reg_used_between_p (dest, succ, i3))))
1934 /* Make sure that the value that is to be substituted for the register
1935 does not use any registers whose values alter in between. However,
1936 If the insns are adjacent, a use can't cross a set even though we
1937 think it might (this can happen for a sequence of insns each setting
1938 the same destination; last_set of that register might point to
1939 a NOTE). If INSN has a REG_EQUIV note, the register is always
1940 equivalent to the memory so the substitution is valid even if there
1941 are intervening stores. Also, don't move a volatile asm or
1942 UNSPEC_VOLATILE across any other insns. */
1943 || (! all_adjacent
1944 && (((!MEM_P (src)
1945 || ! find_reg_note (insn, REG_EQUIV, src))
1946 && use_crosses_set_p (src, DF_INSN_LUID (insn)))
1947 || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
1948 || GET_CODE (src) == UNSPEC_VOLATILE))
1949 /* Don't combine across a CALL_INSN, because that would possibly
1950 change whether the life span of some REGs crosses calls or not,
1951 and it is a pain to update that information.
1952 Exception: if source is a constant, moving it later can't hurt.
1953 Accept that as a special case. */
1954 || (DF_INSN_LUID (insn) < last_call_luid && ! CONSTANT_P (src)))
1955 return 0;
1957 /* DEST must either be a REG or CC0. */
1958 if (REG_P (dest))
1960 /* If register alignment is being enforced for multi-word items in all
1961 cases except for parameters, it is possible to have a register copy
1962 insn referencing a hard register that is not allowed to contain the
1963 mode being copied and which would not be valid as an operand of most
1964 insns. Eliminate this problem by not combining with such an insn.
1966 Also, on some machines we don't want to extend the life of a hard
1967 register. */
1969 if (REG_P (src)
1970 && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
1971 && ! HARD_REGNO_MODE_OK (REGNO (dest), GET_MODE (dest)))
1972 /* Don't extend the life of a hard register unless it is
1973 user variable (if we have few registers) or it can't
1974 fit into the desired register (meaning something special
1975 is going on).
1976 Also avoid substituting a return register into I3, because
1977 reload can't handle a conflict with constraints of other
1978 inputs. */
1979 || (REGNO (src) < FIRST_PSEUDO_REGISTER
1980 && ! HARD_REGNO_MODE_OK (REGNO (src), GET_MODE (src)))))
1981 return 0;
1983 else if (GET_CODE (dest) != CC0)
1984 return 0;
1987 if (GET_CODE (PATTERN (i3)) == PARALLEL)
1988 for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
1989 if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER)
1991 rtx reg = XEXP (XVECEXP (PATTERN (i3), 0, i), 0);
1993 /* If the clobber represents an earlyclobber operand, we must not
1994 substitute an expression containing the clobbered register.
1995 As we do not analyze the constraint strings here, we have to
1996 make the conservative assumption. However, if the register is
1997 a fixed hard reg, the clobber cannot represent any operand;
1998 we leave it up to the machine description to either accept or
1999 reject use-and-clobber patterns. */
2000 if (!REG_P (reg)
2001 || REGNO (reg) >= FIRST_PSEUDO_REGISTER
2002 || !fixed_regs[REGNO (reg)])
2003 if (reg_overlap_mentioned_p (reg, src))
2004 return 0;
2007 /* If INSN contains anything volatile, or is an `asm' (whether volatile
2008 or not), reject, unless nothing volatile comes between it and I3 */
2010 if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
2012 /* Make sure neither succ nor succ2 contains a volatile reference. */
2013 if (succ2 != 0 && volatile_refs_p (PATTERN (succ2)))
2014 return 0;
2015 if (succ != 0 && volatile_refs_p (PATTERN (succ)))
2016 return 0;
2017 /* We'll check insns between INSN and I3 below. */
2020 /* If INSN is an asm, and DEST is a hard register, reject, since it has
2021 to be an explicit register variable, and was chosen for a reason. */
2023 if (GET_CODE (src) == ASM_OPERANDS
2024 && REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER)
2025 return 0;
2027 /* If INSN contains volatile references (specifically volatile MEMs),
2028 we cannot combine across any other volatile references.
2029 Even if INSN doesn't contain volatile references, any intervening
2030 volatile insn might affect machine state. */
2032 is_volatile_p = volatile_refs_p (PATTERN (insn))
2033 ? volatile_refs_p
2034 : volatile_insn_p;
2036 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
2037 if (INSN_P (p) && p != succ && p != succ2 && is_volatile_p (PATTERN (p)))
2038 return 0;
2040 /* If INSN contains an autoincrement or autodecrement, make sure that
2041 register is not used between there and I3, and not already used in
2042 I3 either. Neither must it be used in PRED or SUCC, if they exist.
2043 Also insist that I3 not be a jump; if it were one
2044 and the incremented register were spilled, we would lose. */
2046 #ifdef AUTO_INC_DEC
2047 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
2048 if (REG_NOTE_KIND (link) == REG_INC
2049 && (JUMP_P (i3)
2050 || reg_used_between_p (XEXP (link, 0), insn, i3)
2051 || (pred != NULL_RTX
2052 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred)))
2053 || (pred2 != NULL_RTX
2054 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred2)))
2055 || (succ != NULL_RTX
2056 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ)))
2057 || (succ2 != NULL_RTX
2058 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ2)))
2059 || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
2060 return 0;
2061 #endif
2063 #ifdef HAVE_cc0
2064 /* Don't combine an insn that follows a CC0-setting insn.
2065 An insn that uses CC0 must not be separated from the one that sets it.
2066 We do, however, allow I2 to follow a CC0-setting insn if that insn
2067 is passed as I1; in that case it will be deleted also.
2068 We also allow combining in this case if all the insns are adjacent
2069 because that would leave the two CC0 insns adjacent as well.
2070 It would be more logical to test whether CC0 occurs inside I1 or I2,
2071 but that would be much slower, and this ought to be equivalent. */
2073 p = prev_nonnote_insn (insn);
2074 if (p && p != pred && NONJUMP_INSN_P (p) && sets_cc0_p (PATTERN (p))
2075 && ! all_adjacent)
2076 return 0;
2077 #endif
2079 /* If we get here, we have passed all the tests and the combination is
2080 to be allowed. */
2082 *pdest = dest;
2083 *psrc = src;
2085 return 1;
2088 /* LOC is the location within I3 that contains its pattern or the component
2089 of a PARALLEL of the pattern. We validate that it is valid for combining.
2091 One problem is if I3 modifies its output, as opposed to replacing it
2092 entirely, we can't allow the output to contain I2DEST, I1DEST or I0DEST as
2093 doing so would produce an insn that is not equivalent to the original insns.
2095 Consider:
2097 (set (reg:DI 101) (reg:DI 100))
2098 (set (subreg:SI (reg:DI 101) 0) <foo>)
2100 This is NOT equivalent to:
2102 (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
2103 (set (reg:DI 101) (reg:DI 100))])
2105 Not only does this modify 100 (in which case it might still be valid
2106 if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
2108 We can also run into a problem if I2 sets a register that I1
2109 uses and I1 gets directly substituted into I3 (not via I2). In that
2110 case, we would be getting the wrong value of I2DEST into I3, so we
2111 must reject the combination. This case occurs when I2 and I1 both
2112 feed into I3, rather than when I1 feeds into I2, which feeds into I3.
2113 If I1_NOT_IN_SRC is nonzero, it means that finding I1 in the source
2114 of a SET must prevent combination from occurring. The same situation
2115 can occur for I0, in which case I0_NOT_IN_SRC is set.
2117 Before doing the above check, we first try to expand a field assignment
2118 into a set of logical operations.
2120 If PI3_DEST_KILLED is nonzero, it is a pointer to a location in which
2121 we place a register that is both set and used within I3. If more than one
2122 such register is detected, we fail.
2124 Return 1 if the combination is valid, zero otherwise. */
2126 static int
2127 combinable_i3pat (rtx_insn *i3, rtx *loc, rtx i2dest, rtx i1dest, rtx i0dest,
2128 int i1_not_in_src, int i0_not_in_src, rtx *pi3dest_killed)
2130 rtx x = *loc;
2132 if (GET_CODE (x) == SET)
2134 rtx set = x ;
2135 rtx dest = SET_DEST (set);
2136 rtx src = SET_SRC (set);
2137 rtx inner_dest = dest;
2138 rtx subdest;
2140 while (GET_CODE (inner_dest) == STRICT_LOW_PART
2141 || GET_CODE (inner_dest) == SUBREG
2142 || GET_CODE (inner_dest) == ZERO_EXTRACT)
2143 inner_dest = XEXP (inner_dest, 0);
2145 /* Check for the case where I3 modifies its output, as discussed
2146 above. We don't want to prevent pseudos from being combined
2147 into the address of a MEM, so only prevent the combination if
2148 i1 or i2 set the same MEM. */
2149 if ((inner_dest != dest &&
2150 (!MEM_P (inner_dest)
2151 || rtx_equal_p (i2dest, inner_dest)
2152 || (i1dest && rtx_equal_p (i1dest, inner_dest))
2153 || (i0dest && rtx_equal_p (i0dest, inner_dest)))
2154 && (reg_overlap_mentioned_p (i2dest, inner_dest)
2155 || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))
2156 || (i0dest && reg_overlap_mentioned_p (i0dest, inner_dest))))
2158 /* This is the same test done in can_combine_p except we can't test
2159 all_adjacent; we don't have to, since this instruction will stay
2160 in place, thus we are not considering increasing the lifetime of
2161 INNER_DEST.
2163 Also, if this insn sets a function argument, combining it with
2164 something that might need a spill could clobber a previous
2165 function argument; the all_adjacent test in can_combine_p also
2166 checks this; here, we do a more specific test for this case. */
2168 || (REG_P (inner_dest)
2169 && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
2170 && (! HARD_REGNO_MODE_OK (REGNO (inner_dest),
2171 GET_MODE (inner_dest))))
2172 || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src))
2173 || (i0_not_in_src && reg_overlap_mentioned_p (i0dest, src)))
2174 return 0;
2176 /* If DEST is used in I3, it is being killed in this insn, so
2177 record that for later. We have to consider paradoxical
2178 subregs here, since they kill the whole register, but we
2179 ignore partial subregs, STRICT_LOW_PART, etc.
2180 Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
2181 STACK_POINTER_REGNUM, since these are always considered to be
2182 live. Similarly for ARG_POINTER_REGNUM if it is fixed. */
2183 subdest = dest;
2184 if (GET_CODE (subdest) == SUBREG
2185 && (GET_MODE_SIZE (GET_MODE (subdest))
2186 >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (subdest)))))
2187 subdest = SUBREG_REG (subdest);
2188 if (pi3dest_killed
2189 && REG_P (subdest)
2190 && reg_referenced_p (subdest, PATTERN (i3))
2191 && REGNO (subdest) != FRAME_POINTER_REGNUM
2192 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
2193 && REGNO (subdest) != HARD_FRAME_POINTER_REGNUM
2194 #endif
2195 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
2196 && (REGNO (subdest) != ARG_POINTER_REGNUM
2197 || ! fixed_regs [REGNO (subdest)])
2198 #endif
2199 && REGNO (subdest) != STACK_POINTER_REGNUM)
2201 if (*pi3dest_killed)
2202 return 0;
2204 *pi3dest_killed = subdest;
2208 else if (GET_CODE (x) == PARALLEL)
2210 int i;
2212 for (i = 0; i < XVECLEN (x, 0); i++)
2213 if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest, i0dest,
2214 i1_not_in_src, i0_not_in_src, pi3dest_killed))
2215 return 0;
2218 return 1;
2221 /* Return 1 if X is an arithmetic expression that contains a multiplication
2222 and division. We don't count multiplications by powers of two here. */
2224 static int
2225 contains_muldiv (rtx x)
2227 switch (GET_CODE (x))
2229 case MOD: case DIV: case UMOD: case UDIV:
2230 return 1;
2232 case MULT:
2233 return ! (CONST_INT_P (XEXP (x, 1))
2234 && exact_log2 (UINTVAL (XEXP (x, 1))) >= 0);
2235 default:
2236 if (BINARY_P (x))
2237 return contains_muldiv (XEXP (x, 0))
2238 || contains_muldiv (XEXP (x, 1));
2240 if (UNARY_P (x))
2241 return contains_muldiv (XEXP (x, 0));
2243 return 0;
2247 /* Determine whether INSN can be used in a combination. Return nonzero if
2248 not. This is used in try_combine to detect early some cases where we
2249 can't perform combinations. */
2251 static int
2252 cant_combine_insn_p (rtx_insn *insn)
2254 rtx set;
2255 rtx src, dest;
2257 /* If this isn't really an insn, we can't do anything.
2258 This can occur when flow deletes an insn that it has merged into an
2259 auto-increment address. */
2260 if (! INSN_P (insn))
2261 return 1;
2263 /* Never combine loads and stores involving hard regs that are likely
2264 to be spilled. The register allocator can usually handle such
2265 reg-reg moves by tying. If we allow the combiner to make
2266 substitutions of likely-spilled regs, reload might die.
2267 As an exception, we allow combinations involving fixed regs; these are
2268 not available to the register allocator so there's no risk involved. */
2270 set = single_set (insn);
2271 if (! set)
2272 return 0;
2273 src = SET_SRC (set);
2274 dest = SET_DEST (set);
2275 if (GET_CODE (src) == SUBREG)
2276 src = SUBREG_REG (src);
2277 if (GET_CODE (dest) == SUBREG)
2278 dest = SUBREG_REG (dest);
2279 if (REG_P (src) && REG_P (dest)
2280 && ((HARD_REGISTER_P (src)
2281 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (src))
2282 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (src))))
2283 || (HARD_REGISTER_P (dest)
2284 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (dest))
2285 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (dest))))))
2286 return 1;
2288 return 0;
2291 struct likely_spilled_retval_info
2293 unsigned regno, nregs;
2294 unsigned mask;
2297 /* Called via note_stores by likely_spilled_retval_p. Remove from info->mask
2298 hard registers that are known to be written to / clobbered in full. */
2299 static void
2300 likely_spilled_retval_1 (rtx x, const_rtx set, void *data)
2302 struct likely_spilled_retval_info *const info =
2303 (struct likely_spilled_retval_info *) data;
2304 unsigned regno, nregs;
2305 unsigned new_mask;
2307 if (!REG_P (XEXP (set, 0)))
2308 return;
2309 regno = REGNO (x);
2310 if (regno >= info->regno + info->nregs)
2311 return;
2312 nregs = hard_regno_nregs[regno][GET_MODE (x)];
2313 if (regno + nregs <= info->regno)
2314 return;
2315 new_mask = (2U << (nregs - 1)) - 1;
2316 if (regno < info->regno)
2317 new_mask >>= info->regno - regno;
2318 else
2319 new_mask <<= regno - info->regno;
2320 info->mask &= ~new_mask;
2323 /* Return nonzero iff part of the return value is live during INSN, and
2324 it is likely spilled. This can happen when more than one insn is needed
2325 to copy the return value, e.g. when we consider to combine into the
2326 second copy insn for a complex value. */
2328 static int
2329 likely_spilled_retval_p (rtx_insn *insn)
2331 rtx_insn *use = BB_END (this_basic_block);
2332 rtx reg;
2333 rtx_insn *p;
2334 unsigned regno, nregs;
2335 /* We assume here that no machine mode needs more than
2336 32 hard registers when the value overlaps with a register
2337 for which TARGET_FUNCTION_VALUE_REGNO_P is true. */
2338 unsigned mask;
2339 struct likely_spilled_retval_info info;
2341 if (!NONJUMP_INSN_P (use) || GET_CODE (PATTERN (use)) != USE || insn == use)
2342 return 0;
2343 reg = XEXP (PATTERN (use), 0);
2344 if (!REG_P (reg) || !targetm.calls.function_value_regno_p (REGNO (reg)))
2345 return 0;
2346 regno = REGNO (reg);
2347 nregs = hard_regno_nregs[regno][GET_MODE (reg)];
2348 if (nregs == 1)
2349 return 0;
2350 mask = (2U << (nregs - 1)) - 1;
2352 /* Disregard parts of the return value that are set later. */
2353 info.regno = regno;
2354 info.nregs = nregs;
2355 info.mask = mask;
2356 for (p = PREV_INSN (use); info.mask && p != insn; p = PREV_INSN (p))
2357 if (INSN_P (p))
2358 note_stores (PATTERN (p), likely_spilled_retval_1, &info);
2359 mask = info.mask;
2361 /* Check if any of the (probably) live return value registers is
2362 likely spilled. */
2363 nregs --;
2366 if ((mask & 1 << nregs)
2367 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (regno + nregs)))
2368 return 1;
2369 } while (nregs--);
2370 return 0;
2373 /* Adjust INSN after we made a change to its destination.
2375 Changing the destination can invalidate notes that say something about
2376 the results of the insn and a LOG_LINK pointing to the insn. */
2378 static void
2379 adjust_for_new_dest (rtx_insn *insn)
2381 /* For notes, be conservative and simply remove them. */
2382 remove_reg_equal_equiv_notes (insn);
2384 /* The new insn will have a destination that was previously the destination
2385 of an insn just above it. Call distribute_links to make a LOG_LINK from
2386 the next use of that destination. */
2388 rtx set = single_set (insn);
2389 gcc_assert (set);
2391 rtx reg = SET_DEST (set);
2393 while (GET_CODE (reg) == ZERO_EXTRACT
2394 || GET_CODE (reg) == STRICT_LOW_PART
2395 || GET_CODE (reg) == SUBREG)
2396 reg = XEXP (reg, 0);
2397 gcc_assert (REG_P (reg));
2399 distribute_links (alloc_insn_link (insn, REGNO (reg), NULL));
2401 df_insn_rescan (insn);
2404 /* Return TRUE if combine can reuse reg X in mode MODE.
2405 ADDED_SETS is nonzero if the original set is still required. */
2406 static bool
2407 can_change_dest_mode (rtx x, int added_sets, machine_mode mode)
2409 unsigned int regno;
2411 if (!REG_P (x))
2412 return false;
2414 regno = REGNO (x);
2415 /* Allow hard registers if the new mode is legal, and occupies no more
2416 registers than the old mode. */
2417 if (regno < FIRST_PSEUDO_REGISTER)
2418 return (HARD_REGNO_MODE_OK (regno, mode)
2419 && (hard_regno_nregs[regno][GET_MODE (x)]
2420 >= hard_regno_nregs[regno][mode]));
2422 /* Or a pseudo that is only used once. */
2423 return (REG_N_SETS (regno) == 1 && !added_sets
2424 && !REG_USERVAR_P (x));
2428 /* Check whether X, the destination of a set, refers to part of
2429 the register specified by REG. */
2431 static bool
2432 reg_subword_p (rtx x, rtx reg)
2434 /* Check that reg is an integer mode register. */
2435 if (!REG_P (reg) || GET_MODE_CLASS (GET_MODE (reg)) != MODE_INT)
2436 return false;
2438 if (GET_CODE (x) == STRICT_LOW_PART
2439 || GET_CODE (x) == ZERO_EXTRACT)
2440 x = XEXP (x, 0);
2442 return GET_CODE (x) == SUBREG
2443 && SUBREG_REG (x) == reg
2444 && GET_MODE_CLASS (GET_MODE (x)) == MODE_INT;
2447 /* Delete the unconditional jump INSN and adjust the CFG correspondingly.
2448 Note that the INSN should be deleted *after* removing dead edges, so
2449 that the kept edge is the fallthrough edge for a (set (pc) (pc))
2450 but not for a (set (pc) (label_ref FOO)). */
2452 static void
2453 update_cfg_for_uncondjump (rtx_insn *insn)
2455 basic_block bb = BLOCK_FOR_INSN (insn);
2456 gcc_assert (BB_END (bb) == insn);
2458 purge_dead_edges (bb);
2460 delete_insn (insn);
2461 if (EDGE_COUNT (bb->succs) == 1)
2463 rtx_insn *insn;
2465 single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
2467 /* Remove barriers from the footer if there are any. */
2468 for (insn = BB_FOOTER (bb); insn; insn = NEXT_INSN (insn))
2469 if (BARRIER_P (insn))
2471 if (PREV_INSN (insn))
2472 SET_NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
2473 else
2474 BB_FOOTER (bb) = NEXT_INSN (insn);
2475 if (NEXT_INSN (insn))
2476 SET_PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
2478 else if (LABEL_P (insn))
2479 break;
2483 #ifndef HAVE_cc0
2484 /* Return whether INSN is a PARALLEL of exactly N register SETs followed
2485 by an arbitrary number of CLOBBERs. */
2486 static bool
2487 is_parallel_of_n_reg_sets (rtx_insn *insn, int n)
2489 rtx pat = PATTERN (insn);
2491 if (GET_CODE (pat) != PARALLEL)
2492 return false;
2494 int len = XVECLEN (pat, 0);
2495 if (len < n)
2496 return false;
2498 int i;
2499 for (i = 0; i < n; i++)
2500 if (GET_CODE (XVECEXP (pat, 0, i)) != SET
2501 || !REG_P (SET_DEST (XVECEXP (pat, 0, i))))
2502 return false;
2503 for ( ; i < len; i++)
2504 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
2505 return false;
2507 return true;
2510 /* Return whether INSN, a PARALLEL of N register SETs (and maybe some
2511 CLOBBERs), can be split into individual SETs in that order, without
2512 changing semantics. */
2513 static bool
2514 can_split_parallel_of_n_reg_sets (rtx_insn *insn, int n)
2516 if (!insn_nothrow_p (insn))
2517 return false;
2519 rtx pat = PATTERN (insn);
2521 int i, j;
2522 for (i = 0; i < n; i++)
2524 if (side_effects_p (SET_SRC (XVECEXP (pat, 0, i))))
2525 return false;
2527 rtx reg = SET_DEST (XVECEXP (pat, 0, i));
2529 for (j = i + 1; j < n; j++)
2530 if (reg_referenced_p (reg, XVECEXP (pat, 0, j)))
2531 return false;
2534 return true;
2536 #endif
2538 /* Try to combine the insns I0, I1 and I2 into I3.
2539 Here I0, I1 and I2 appear earlier than I3.
2540 I0 and I1 can be zero; then we combine just I2 into I3, or I1 and I2 into
2543 If we are combining more than two insns and the resulting insn is not
2544 recognized, try splitting it into two insns. If that happens, I2 and I3
2545 are retained and I1/I0 are pseudo-deleted by turning them into a NOTE.
2546 Otherwise, I0, I1 and I2 are pseudo-deleted.
2548 Return 0 if the combination does not work. Then nothing is changed.
2549 If we did the combination, return the insn at which combine should
2550 resume scanning.
2552 Set NEW_DIRECT_JUMP_P to a nonzero value if try_combine creates a
2553 new direct jump instruction.
2555 LAST_COMBINED_INSN is either I3, or some insn after I3 that has
2556 been I3 passed to an earlier try_combine within the same basic
2557 block. */
2559 static rtx_insn *
2560 try_combine (rtx_insn *i3, rtx_insn *i2, rtx_insn *i1, rtx_insn *i0,
2561 int *new_direct_jump_p, rtx_insn *last_combined_insn)
2563 /* New patterns for I3 and I2, respectively. */
2564 rtx newpat, newi2pat = 0;
2565 rtvec newpat_vec_with_clobbers = 0;
2566 int substed_i2 = 0, substed_i1 = 0, substed_i0 = 0;
2567 /* Indicates need to preserve SET in I0, I1 or I2 in I3 if it is not
2568 dead. */
2569 int added_sets_0, added_sets_1, added_sets_2;
2570 /* Total number of SETs to put into I3. */
2571 int total_sets;
2572 /* Nonzero if I2's or I1's body now appears in I3. */
2573 int i2_is_used = 0, i1_is_used = 0;
2574 /* INSN_CODEs for new I3, new I2, and user of condition code. */
2575 int insn_code_number, i2_code_number = 0, other_code_number = 0;
2576 /* Contains I3 if the destination of I3 is used in its source, which means
2577 that the old life of I3 is being killed. If that usage is placed into
2578 I2 and not in I3, a REG_DEAD note must be made. */
2579 rtx i3dest_killed = 0;
2580 /* SET_DEST and SET_SRC of I2, I1 and I0. */
2581 rtx i2dest = 0, i2src = 0, i1dest = 0, i1src = 0, i0dest = 0, i0src = 0;
2582 /* Copy of SET_SRC of I1 and I0, if needed. */
2583 rtx i1src_copy = 0, i0src_copy = 0, i0src_copy2 = 0;
2584 /* Set if I2DEST was reused as a scratch register. */
2585 bool i2scratch = false;
2586 /* The PATTERNs of I0, I1, and I2, or a copy of them in certain cases. */
2587 rtx i0pat = 0, i1pat = 0, i2pat = 0;
2588 /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC. */
2589 int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
2590 int i0dest_in_i0src = 0, i1dest_in_i0src = 0, i2dest_in_i0src = 0;
2591 int i2dest_killed = 0, i1dest_killed = 0, i0dest_killed = 0;
2592 int i1_feeds_i2_n = 0, i0_feeds_i2_n = 0, i0_feeds_i1_n = 0;
2593 /* Notes that must be added to REG_NOTES in I3 and I2. */
2594 rtx new_i3_notes, new_i2_notes;
2595 /* Notes that we substituted I3 into I2 instead of the normal case. */
2596 int i3_subst_into_i2 = 0;
2597 /* Notes that I1, I2 or I3 is a MULT operation. */
2598 int have_mult = 0;
2599 int swap_i2i3 = 0;
2600 int changed_i3_dest = 0;
2602 int maxreg;
2603 rtx_insn *temp_insn;
2604 rtx temp_expr;
2605 struct insn_link *link;
2606 rtx other_pat = 0;
2607 rtx new_other_notes;
2608 int i;
2610 /* Immediately return if any of I0,I1,I2 are the same insn (I3 can
2611 never be). */
2612 if (i1 == i2 || i0 == i2 || (i0 && i0 == i1))
2613 return 0;
2615 /* Only try four-insn combinations when there's high likelihood of
2616 success. Look for simple insns, such as loads of constants or
2617 binary operations involving a constant. */
2618 if (i0)
2620 int i;
2621 int ngood = 0;
2622 int nshift = 0;
2623 rtx set0, set3;
2625 if (!flag_expensive_optimizations)
2626 return 0;
2628 for (i = 0; i < 4; i++)
2630 rtx_insn *insn = i == 0 ? i0 : i == 1 ? i1 : i == 2 ? i2 : i3;
2631 rtx set = single_set (insn);
2632 rtx src;
2633 if (!set)
2634 continue;
2635 src = SET_SRC (set);
2636 if (CONSTANT_P (src))
2638 ngood += 2;
2639 break;
2641 else if (BINARY_P (src) && CONSTANT_P (XEXP (src, 1)))
2642 ngood++;
2643 else if (GET_CODE (src) == ASHIFT || GET_CODE (src) == ASHIFTRT
2644 || GET_CODE (src) == LSHIFTRT)
2645 nshift++;
2648 /* If I0 loads a memory and I3 sets the same memory, then I1 and I2
2649 are likely manipulating its value. Ideally we'll be able to combine
2650 all four insns into a bitfield insertion of some kind.
2652 Note the source in I0 might be inside a sign/zero extension and the
2653 memory modes in I0 and I3 might be different. So extract the address
2654 from the destination of I3 and search for it in the source of I0.
2656 In the event that there's a match but the source/dest do not actually
2657 refer to the same memory, the worst that happens is we try some
2658 combinations that we wouldn't have otherwise. */
2659 if ((set0 = single_set (i0))
2660 /* Ensure the source of SET0 is a MEM, possibly buried inside
2661 an extension. */
2662 && (GET_CODE (SET_SRC (set0)) == MEM
2663 || ((GET_CODE (SET_SRC (set0)) == ZERO_EXTEND
2664 || GET_CODE (SET_SRC (set0)) == SIGN_EXTEND)
2665 && GET_CODE (XEXP (SET_SRC (set0), 0)) == MEM))
2666 && (set3 = single_set (i3))
2667 /* Ensure the destination of SET3 is a MEM. */
2668 && GET_CODE (SET_DEST (set3)) == MEM
2669 /* Would it be better to extract the base address for the MEM
2670 in SET3 and look for that? I don't have cases where it matters
2671 but I could envision such cases. */
2672 && rtx_referenced_p (XEXP (SET_DEST (set3), 0), SET_SRC (set0)))
2673 ngood += 2;
2675 if (ngood < 2 && nshift < 2)
2676 return 0;
2679 /* Exit early if one of the insns involved can't be used for
2680 combinations. */
2681 if (CALL_P (i2)
2682 || (i1 && CALL_P (i1))
2683 || (i0 && CALL_P (i0))
2684 || cant_combine_insn_p (i3)
2685 || cant_combine_insn_p (i2)
2686 || (i1 && cant_combine_insn_p (i1))
2687 || (i0 && cant_combine_insn_p (i0))
2688 || likely_spilled_retval_p (i3))
2689 return 0;
2691 combine_attempts++;
2692 undobuf.other_insn = 0;
2694 /* Reset the hard register usage information. */
2695 CLEAR_HARD_REG_SET (newpat_used_regs);
2697 if (dump_file && (dump_flags & TDF_DETAILS))
2699 if (i0)
2700 fprintf (dump_file, "\nTrying %d, %d, %d -> %d:\n",
2701 INSN_UID (i0), INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2702 else if (i1)
2703 fprintf (dump_file, "\nTrying %d, %d -> %d:\n",
2704 INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2705 else
2706 fprintf (dump_file, "\nTrying %d -> %d:\n",
2707 INSN_UID (i2), INSN_UID (i3));
2710 /* If multiple insns feed into one of I2 or I3, they can be in any
2711 order. To simplify the code below, reorder them in sequence. */
2712 if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i2))
2713 temp_insn = i2, i2 = i0, i0 = temp_insn;
2714 if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i1))
2715 temp_insn = i1, i1 = i0, i0 = temp_insn;
2716 if (i1 && DF_INSN_LUID (i1) > DF_INSN_LUID (i2))
2717 temp_insn = i1, i1 = i2, i2 = temp_insn;
2719 added_links_insn = 0;
2721 /* First check for one important special case that the code below will
2722 not handle. Namely, the case where I1 is zero, I2 is a PARALLEL
2723 and I3 is a SET whose SET_SRC is a SET_DEST in I2. In that case,
2724 we may be able to replace that destination with the destination of I3.
2725 This occurs in the common code where we compute both a quotient and
2726 remainder into a structure, in which case we want to do the computation
2727 directly into the structure to avoid register-register copies.
2729 Note that this case handles both multiple sets in I2 and also cases
2730 where I2 has a number of CLOBBERs inside the PARALLEL.
2732 We make very conservative checks below and only try to handle the
2733 most common cases of this. For example, we only handle the case
2734 where I2 and I3 are adjacent to avoid making difficult register
2735 usage tests. */
2737 if (i1 == 0 && NONJUMP_INSN_P (i3) && GET_CODE (PATTERN (i3)) == SET
2738 && REG_P (SET_SRC (PATTERN (i3)))
2739 && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
2740 && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
2741 && GET_CODE (PATTERN (i2)) == PARALLEL
2742 && ! side_effects_p (SET_DEST (PATTERN (i3)))
2743 /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
2744 below would need to check what is inside (and reg_overlap_mentioned_p
2745 doesn't support those codes anyway). Don't allow those destinations;
2746 the resulting insn isn't likely to be recognized anyway. */
2747 && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
2748 && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
2749 && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
2750 SET_DEST (PATTERN (i3)))
2751 && next_active_insn (i2) == i3)
2753 rtx p2 = PATTERN (i2);
2755 /* Make sure that the destination of I3,
2756 which we are going to substitute into one output of I2,
2757 is not used within another output of I2. We must avoid making this:
2758 (parallel [(set (mem (reg 69)) ...)
2759 (set (reg 69) ...)])
2760 which is not well-defined as to order of actions.
2761 (Besides, reload can't handle output reloads for this.)
2763 The problem can also happen if the dest of I3 is a memory ref,
2764 if another dest in I2 is an indirect memory ref. */
2765 for (i = 0; i < XVECLEN (p2, 0); i++)
2766 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
2767 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
2768 && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
2769 SET_DEST (XVECEXP (p2, 0, i))))
2770 break;
2772 /* Make sure this PARALLEL is not an asm. We do not allow combining
2773 that usually (see can_combine_p), so do not here either. */
2774 for (i = 0; i < XVECLEN (p2, 0); i++)
2775 if (GET_CODE (XVECEXP (p2, 0, i)) == SET
2776 && GET_CODE (SET_SRC (XVECEXP (p2, 0, i))) == ASM_OPERANDS)
2777 break;
2779 if (i == XVECLEN (p2, 0))
2780 for (i = 0; i < XVECLEN (p2, 0); i++)
2781 if (GET_CODE (XVECEXP (p2, 0, i)) == SET
2782 && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
2784 combine_merges++;
2786 subst_insn = i3;
2787 subst_low_luid = DF_INSN_LUID (i2);
2789 added_sets_2 = added_sets_1 = added_sets_0 = 0;
2790 i2src = SET_SRC (XVECEXP (p2, 0, i));
2791 i2dest = SET_DEST (XVECEXP (p2, 0, i));
2792 i2dest_killed = dead_or_set_p (i2, i2dest);
2794 /* Replace the dest in I2 with our dest and make the resulting
2795 insn the new pattern for I3. Then skip to where we validate
2796 the pattern. Everything was set up above. */
2797 SUBST (SET_DEST (XVECEXP (p2, 0, i)), SET_DEST (PATTERN (i3)));
2798 newpat = p2;
2799 i3_subst_into_i2 = 1;
2800 goto validate_replacement;
2804 /* If I2 is setting a pseudo to a constant and I3 is setting some
2805 sub-part of it to another constant, merge them by making a new
2806 constant. */
2807 if (i1 == 0
2808 && (temp_expr = single_set (i2)) != 0
2809 && CONST_SCALAR_INT_P (SET_SRC (temp_expr))
2810 && GET_CODE (PATTERN (i3)) == SET
2811 && CONST_SCALAR_INT_P (SET_SRC (PATTERN (i3)))
2812 && reg_subword_p (SET_DEST (PATTERN (i3)), SET_DEST (temp_expr)))
2814 rtx dest = SET_DEST (PATTERN (i3));
2815 int offset = -1;
2816 int width = 0;
2818 if (GET_CODE (dest) == ZERO_EXTRACT)
2820 if (CONST_INT_P (XEXP (dest, 1))
2821 && CONST_INT_P (XEXP (dest, 2)))
2823 width = INTVAL (XEXP (dest, 1));
2824 offset = INTVAL (XEXP (dest, 2));
2825 dest = XEXP (dest, 0);
2826 if (BITS_BIG_ENDIAN)
2827 offset = GET_MODE_PRECISION (GET_MODE (dest)) - width - offset;
2830 else
2832 if (GET_CODE (dest) == STRICT_LOW_PART)
2833 dest = XEXP (dest, 0);
2834 width = GET_MODE_PRECISION (GET_MODE (dest));
2835 offset = 0;
2838 if (offset >= 0)
2840 /* If this is the low part, we're done. */
2841 if (subreg_lowpart_p (dest))
2843 /* Handle the case where inner is twice the size of outer. */
2844 else if (GET_MODE_PRECISION (GET_MODE (SET_DEST (temp_expr)))
2845 == 2 * GET_MODE_PRECISION (GET_MODE (dest)))
2846 offset += GET_MODE_PRECISION (GET_MODE (dest));
2847 /* Otherwise give up for now. */
2848 else
2849 offset = -1;
2852 if (offset >= 0)
2854 rtx inner = SET_SRC (PATTERN (i3));
2855 rtx outer = SET_SRC (temp_expr);
2857 wide_int o
2858 = wi::insert (std::make_pair (outer, GET_MODE (SET_DEST (temp_expr))),
2859 std::make_pair (inner, GET_MODE (dest)),
2860 offset, width);
2862 combine_merges++;
2863 subst_insn = i3;
2864 subst_low_luid = DF_INSN_LUID (i2);
2865 added_sets_2 = added_sets_1 = added_sets_0 = 0;
2866 i2dest = SET_DEST (temp_expr);
2867 i2dest_killed = dead_or_set_p (i2, i2dest);
2869 /* Replace the source in I2 with the new constant and make the
2870 resulting insn the new pattern for I3. Then skip to where we
2871 validate the pattern. Everything was set up above. */
2872 SUBST (SET_SRC (temp_expr),
2873 immed_wide_int_const (o, GET_MODE (SET_DEST (temp_expr))));
2875 newpat = PATTERN (i2);
2877 /* The dest of I3 has been replaced with the dest of I2. */
2878 changed_i3_dest = 1;
2879 goto validate_replacement;
2883 #ifndef HAVE_cc0
2884 /* If we have no I1 and I2 looks like:
2885 (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
2886 (set Y OP)])
2887 make up a dummy I1 that is
2888 (set Y OP)
2889 and change I2 to be
2890 (set (reg:CC X) (compare:CC Y (const_int 0)))
2892 (We can ignore any trailing CLOBBERs.)
2894 This undoes a previous combination and allows us to match a branch-and-
2895 decrement insn. */
2897 if (i1 == 0
2898 && is_parallel_of_n_reg_sets (i2, 2)
2899 && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
2900 == MODE_CC)
2901 && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
2902 && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
2903 && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
2904 SET_SRC (XVECEXP (PATTERN (i2), 0, 1)))
2905 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 0)), i2, i3)
2906 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)), i2, i3))
2908 /* We make I1 with the same INSN_UID as I2. This gives it
2909 the same DF_INSN_LUID for value tracking. Our fake I1 will
2910 never appear in the insn stream so giving it the same INSN_UID
2911 as I2 will not cause a problem. */
2913 i1 = gen_rtx_INSN (VOIDmode, NULL, i2, BLOCK_FOR_INSN (i2),
2914 XVECEXP (PATTERN (i2), 0, 1), INSN_LOCATION (i2),
2915 -1, NULL_RTX);
2916 INSN_UID (i1) = INSN_UID (i2);
2918 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
2919 SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
2920 SET_DEST (PATTERN (i1)));
2921 unsigned int regno = REGNO (SET_DEST (PATTERN (i1)));
2922 SUBST_LINK (LOG_LINKS (i2),
2923 alloc_insn_link (i1, regno, LOG_LINKS (i2)));
2926 /* If I2 is a PARALLEL of two SETs of REGs (and perhaps some CLOBBERs),
2927 make those two SETs separate I1 and I2 insns, and make an I0 that is
2928 the original I1. */
2929 if (i0 == 0
2930 && is_parallel_of_n_reg_sets (i2, 2)
2931 && can_split_parallel_of_n_reg_sets (i2, 2)
2932 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 0)), i2, i3)
2933 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)), i2, i3))
2935 /* If there is no I1, there is no I0 either. */
2936 i0 = i1;
2938 /* We make I1 with the same INSN_UID as I2. This gives it
2939 the same DF_INSN_LUID for value tracking. Our fake I1 will
2940 never appear in the insn stream so giving it the same INSN_UID
2941 as I2 will not cause a problem. */
2943 i1 = gen_rtx_INSN (VOIDmode, NULL, i2, BLOCK_FOR_INSN (i2),
2944 XVECEXP (PATTERN (i2), 0, 0), INSN_LOCATION (i2),
2945 -1, NULL_RTX);
2946 INSN_UID (i1) = INSN_UID (i2);
2948 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 1));
2950 #endif
2952 /* Verify that I2 and I1 are valid for combining. */
2953 if (! can_combine_p (i2, i3, i0, i1, NULL, NULL, &i2dest, &i2src)
2954 || (i1 && ! can_combine_p (i1, i3, i0, NULL, i2, NULL,
2955 &i1dest, &i1src))
2956 || (i0 && ! can_combine_p (i0, i3, NULL, NULL, i1, i2,
2957 &i0dest, &i0src)))
2959 undo_all ();
2960 return 0;
2963 /* Record whether I2DEST is used in I2SRC and similarly for the other
2964 cases. Knowing this will help in register status updating below. */
2965 i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
2966 i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
2967 i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
2968 i0dest_in_i0src = i0 && reg_overlap_mentioned_p (i0dest, i0src);
2969 i1dest_in_i0src = i0 && reg_overlap_mentioned_p (i1dest, i0src);
2970 i2dest_in_i0src = i0 && reg_overlap_mentioned_p (i2dest, i0src);
2971 i2dest_killed = dead_or_set_p (i2, i2dest);
2972 i1dest_killed = i1 && dead_or_set_p (i1, i1dest);
2973 i0dest_killed = i0 && dead_or_set_p (i0, i0dest);
2975 /* For the earlier insns, determine which of the subsequent ones they
2976 feed. */
2977 i1_feeds_i2_n = i1 && insn_a_feeds_b (i1, i2);
2978 i0_feeds_i1_n = i0 && insn_a_feeds_b (i0, i1);
2979 i0_feeds_i2_n = (i0 && (!i0_feeds_i1_n ? insn_a_feeds_b (i0, i2)
2980 : (!reg_overlap_mentioned_p (i1dest, i0dest)
2981 && reg_overlap_mentioned_p (i0dest, i2src))));
2983 /* Ensure that I3's pattern can be the destination of combines. */
2984 if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest, i0dest,
2985 i1 && i2dest_in_i1src && !i1_feeds_i2_n,
2986 i0 && ((i2dest_in_i0src && !i0_feeds_i2_n)
2987 || (i1dest_in_i0src && !i0_feeds_i1_n)),
2988 &i3dest_killed))
2990 undo_all ();
2991 return 0;
2994 /* See if any of the insns is a MULT operation. Unless one is, we will
2995 reject a combination that is, since it must be slower. Be conservative
2996 here. */
2997 if (GET_CODE (i2src) == MULT
2998 || (i1 != 0 && GET_CODE (i1src) == MULT)
2999 || (i0 != 0 && GET_CODE (i0src) == MULT)
3000 || (GET_CODE (PATTERN (i3)) == SET
3001 && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
3002 have_mult = 1;
3004 /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
3005 We used to do this EXCEPT in one case: I3 has a post-inc in an
3006 output operand. However, that exception can give rise to insns like
3007 mov r3,(r3)+
3008 which is a famous insn on the PDP-11 where the value of r3 used as the
3009 source was model-dependent. Avoid this sort of thing. */
3011 #if 0
3012 if (!(GET_CODE (PATTERN (i3)) == SET
3013 && REG_P (SET_SRC (PATTERN (i3)))
3014 && MEM_P (SET_DEST (PATTERN (i3)))
3015 && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
3016 || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
3017 /* It's not the exception. */
3018 #endif
3019 #ifdef AUTO_INC_DEC
3021 rtx link;
3022 for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
3023 if (REG_NOTE_KIND (link) == REG_INC
3024 && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
3025 || (i1 != 0
3026 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
3028 undo_all ();
3029 return 0;
3032 #endif
3034 /* See if the SETs in I1 or I2 need to be kept around in the merged
3035 instruction: whenever the value set there is still needed past I3.
3036 For the SET in I2, this is easy: we see if I2DEST dies or is set in I3.
3038 For the SET in I1, we have two cases: if I1 and I2 independently feed
3039 into I3, the set in I1 needs to be kept around unless I1DEST dies
3040 or is set in I3. Otherwise (if I1 feeds I2 which feeds I3), the set
3041 in I1 needs to be kept around unless I1DEST dies or is set in either
3042 I2 or I3. The same considerations apply to I0. */
3044 added_sets_2 = !dead_or_set_p (i3, i2dest);
3046 if (i1)
3047 added_sets_1 = !(dead_or_set_p (i3, i1dest)
3048 || (i1_feeds_i2_n && dead_or_set_p (i2, i1dest)));
3049 else
3050 added_sets_1 = 0;
3052 if (i0)
3053 added_sets_0 = !(dead_or_set_p (i3, i0dest)
3054 || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest))
3055 || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
3056 && dead_or_set_p (i2, i0dest)));
3057 else
3058 added_sets_0 = 0;
3060 /* We are about to copy insns for the case where they need to be kept
3061 around. Check that they can be copied in the merged instruction. */
3063 if (targetm.cannot_copy_insn_p
3064 && ((added_sets_2 && targetm.cannot_copy_insn_p (i2))
3065 || (i1 && added_sets_1 && targetm.cannot_copy_insn_p (i1))
3066 || (i0 && added_sets_0 && targetm.cannot_copy_insn_p (i0))))
3068 undo_all ();
3069 return 0;
3072 /* If the set in I2 needs to be kept around, we must make a copy of
3073 PATTERN (I2), so that when we substitute I1SRC for I1DEST in
3074 PATTERN (I2), we are only substituting for the original I1DEST, not into
3075 an already-substituted copy. This also prevents making self-referential
3076 rtx. If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
3077 I2DEST. */
3079 if (added_sets_2)
3081 if (GET_CODE (PATTERN (i2)) == PARALLEL)
3082 i2pat = gen_rtx_SET (VOIDmode, i2dest, copy_rtx (i2src));
3083 else
3084 i2pat = copy_rtx (PATTERN (i2));
3087 if (added_sets_1)
3089 if (GET_CODE (PATTERN (i1)) == PARALLEL)
3090 i1pat = gen_rtx_SET (VOIDmode, i1dest, copy_rtx (i1src));
3091 else
3092 i1pat = copy_rtx (PATTERN (i1));
3095 if (added_sets_0)
3097 if (GET_CODE (PATTERN (i0)) == PARALLEL)
3098 i0pat = gen_rtx_SET (VOIDmode, i0dest, copy_rtx (i0src));
3099 else
3100 i0pat = copy_rtx (PATTERN (i0));
3103 combine_merges++;
3105 /* Substitute in the latest insn for the regs set by the earlier ones. */
3107 maxreg = max_reg_num ();
3109 subst_insn = i3;
3111 #ifndef HAVE_cc0
3112 /* Many machines that don't use CC0 have insns that can both perform an
3113 arithmetic operation and set the condition code. These operations will
3114 be represented as a PARALLEL with the first element of the vector
3115 being a COMPARE of an arithmetic operation with the constant zero.
3116 The second element of the vector will set some pseudo to the result
3117 of the same arithmetic operation. If we simplify the COMPARE, we won't
3118 match such a pattern and so will generate an extra insn. Here we test
3119 for this case, where both the comparison and the operation result are
3120 needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
3121 I2SRC. Later we will make the PARALLEL that contains I2. */
3123 if (i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
3124 && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
3125 && CONST_INT_P (XEXP (SET_SRC (PATTERN (i3)), 1))
3126 && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
3128 rtx newpat_dest;
3129 rtx *cc_use_loc = NULL;
3130 rtx_insn *cc_use_insn = NULL;
3131 rtx op0 = i2src, op1 = XEXP (SET_SRC (PATTERN (i3)), 1);
3132 machine_mode compare_mode, orig_compare_mode;
3133 enum rtx_code compare_code = UNKNOWN, orig_compare_code = UNKNOWN;
3135 newpat = PATTERN (i3);
3136 newpat_dest = SET_DEST (newpat);
3137 compare_mode = orig_compare_mode = GET_MODE (newpat_dest);
3139 if (undobuf.other_insn == 0
3140 && (cc_use_loc = find_single_use (SET_DEST (newpat), i3,
3141 &cc_use_insn)))
3143 compare_code = orig_compare_code = GET_CODE (*cc_use_loc);
3144 compare_code = simplify_compare_const (compare_code,
3145 GET_MODE (i2dest), op0, &op1);
3146 target_canonicalize_comparison (&compare_code, &op0, &op1, 1);
3149 /* Do the rest only if op1 is const0_rtx, which may be the
3150 result of simplification. */
3151 if (op1 == const0_rtx)
3153 /* If a single use of the CC is found, prepare to modify it
3154 when SELECT_CC_MODE returns a new CC-class mode, or when
3155 the above simplify_compare_const() returned a new comparison
3156 operator. undobuf.other_insn is assigned the CC use insn
3157 when modifying it. */
3158 if (cc_use_loc)
3160 #ifdef SELECT_CC_MODE
3161 machine_mode new_mode
3162 = SELECT_CC_MODE (compare_code, op0, op1);
3163 if (new_mode != orig_compare_mode
3164 && can_change_dest_mode (SET_DEST (newpat),
3165 added_sets_2, new_mode))
3167 unsigned int regno = REGNO (newpat_dest);
3168 compare_mode = new_mode;
3169 if (regno < FIRST_PSEUDO_REGISTER)
3170 newpat_dest = gen_rtx_REG (compare_mode, regno);
3171 else
3173 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
3174 newpat_dest = regno_reg_rtx[regno];
3177 #endif
3178 /* Cases for modifying the CC-using comparison. */
3179 if (compare_code != orig_compare_code
3180 /* ??? Do we need to verify the zero rtx? */
3181 && XEXP (*cc_use_loc, 1) == const0_rtx)
3183 /* Replace cc_use_loc with entire new RTX. */
3184 SUBST (*cc_use_loc,
3185 gen_rtx_fmt_ee (compare_code, compare_mode,
3186 newpat_dest, const0_rtx));
3187 undobuf.other_insn = cc_use_insn;
3189 else if (compare_mode != orig_compare_mode)
3191 /* Just replace the CC reg with a new mode. */
3192 SUBST (XEXP (*cc_use_loc, 0), newpat_dest);
3193 undobuf.other_insn = cc_use_insn;
3197 /* Now we modify the current newpat:
3198 First, SET_DEST(newpat) is updated if the CC mode has been
3199 altered. For targets without SELECT_CC_MODE, this should be
3200 optimized away. */
3201 if (compare_mode != orig_compare_mode)
3202 SUBST (SET_DEST (newpat), newpat_dest);
3203 /* This is always done to propagate i2src into newpat. */
3204 SUBST (SET_SRC (newpat),
3205 gen_rtx_COMPARE (compare_mode, op0, op1));
3206 /* Create new version of i2pat if needed; the below PARALLEL
3207 creation needs this to work correctly. */
3208 if (! rtx_equal_p (i2src, op0))
3209 i2pat = gen_rtx_SET (VOIDmode, i2dest, op0);
3210 i2_is_used = 1;
3213 #endif
3215 if (i2_is_used == 0)
3217 /* It is possible that the source of I2 or I1 may be performing
3218 an unneeded operation, such as a ZERO_EXTEND of something
3219 that is known to have the high part zero. Handle that case
3220 by letting subst look at the inner insns.
3222 Another way to do this would be to have a function that tries
3223 to simplify a single insn instead of merging two or more
3224 insns. We don't do this because of the potential of infinite
3225 loops and because of the potential extra memory required.
3226 However, doing it the way we are is a bit of a kludge and
3227 doesn't catch all cases.
3229 But only do this if -fexpensive-optimizations since it slows
3230 things down and doesn't usually win.
3232 This is not done in the COMPARE case above because the
3233 unmodified I2PAT is used in the PARALLEL and so a pattern
3234 with a modified I2SRC would not match. */
3236 if (flag_expensive_optimizations)
3238 /* Pass pc_rtx so no substitutions are done, just
3239 simplifications. */
3240 if (i1)
3242 subst_low_luid = DF_INSN_LUID (i1);
3243 i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0, 0);
3246 subst_low_luid = DF_INSN_LUID (i2);
3247 i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0, 0);
3250 n_occurrences = 0; /* `subst' counts here */
3251 subst_low_luid = DF_INSN_LUID (i2);
3253 /* If I1 feeds into I2 and I1DEST is in I1SRC, we need to make a unique
3254 copy of I2SRC each time we substitute it, in order to avoid creating
3255 self-referential RTL when we will be substituting I1SRC for I1DEST
3256 later. Likewise if I0 feeds into I2, either directly or indirectly
3257 through I1, and I0DEST is in I0SRC. */
3258 newpat = subst (PATTERN (i3), i2dest, i2src, 0, 0,
3259 (i1_feeds_i2_n && i1dest_in_i1src)
3260 || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
3261 && i0dest_in_i0src));
3262 substed_i2 = 1;
3264 /* Record whether I2's body now appears within I3's body. */
3265 i2_is_used = n_occurrences;
3268 /* If we already got a failure, don't try to do more. Otherwise, try to
3269 substitute I1 if we have it. */
3271 if (i1 && GET_CODE (newpat) != CLOBBER)
3273 /* Check that an autoincrement side-effect on I1 has not been lost.
3274 This happens if I1DEST is mentioned in I2 and dies there, and
3275 has disappeared from the new pattern. */
3276 if ((FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3277 && i1_feeds_i2_n
3278 && dead_or_set_p (i2, i1dest)
3279 && !reg_overlap_mentioned_p (i1dest, newpat))
3280 /* Before we can do this substitution, we must redo the test done
3281 above (see detailed comments there) that ensures I1DEST isn't
3282 mentioned in any SETs in NEWPAT that are field assignments. */
3283 || !combinable_i3pat (NULL, &newpat, i1dest, NULL_RTX, NULL_RTX,
3284 0, 0, 0))
3286 undo_all ();
3287 return 0;
3290 n_occurrences = 0;
3291 subst_low_luid = DF_INSN_LUID (i1);
3293 /* If the following substitution will modify I1SRC, make a copy of it
3294 for the case where it is substituted for I1DEST in I2PAT later. */
3295 if (added_sets_2 && i1_feeds_i2_n)
3296 i1src_copy = copy_rtx (i1src);
3298 /* If I0 feeds into I1 and I0DEST is in I0SRC, we need to make a unique
3299 copy of I1SRC each time we substitute it, in order to avoid creating
3300 self-referential RTL when we will be substituting I0SRC for I0DEST
3301 later. */
3302 newpat = subst (newpat, i1dest, i1src, 0, 0,
3303 i0_feeds_i1_n && i0dest_in_i0src);
3304 substed_i1 = 1;
3306 /* Record whether I1's body now appears within I3's body. */
3307 i1_is_used = n_occurrences;
3310 /* Likewise for I0 if we have it. */
3312 if (i0 && GET_CODE (newpat) != CLOBBER)
3314 if ((FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3315 && ((i0_feeds_i2_n && dead_or_set_p (i2, i0dest))
3316 || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest)))
3317 && !reg_overlap_mentioned_p (i0dest, newpat))
3318 || !combinable_i3pat (NULL, &newpat, i0dest, NULL_RTX, NULL_RTX,
3319 0, 0, 0))
3321 undo_all ();
3322 return 0;
3325 /* If the following substitution will modify I0SRC, make a copy of it
3326 for the case where it is substituted for I0DEST in I1PAT later. */
3327 if (added_sets_1 && i0_feeds_i1_n)
3328 i0src_copy = copy_rtx (i0src);
3329 /* And a copy for I0DEST in I2PAT substitution. */
3330 if (added_sets_2 && ((i0_feeds_i1_n && i1_feeds_i2_n)
3331 || (i0_feeds_i2_n)))
3332 i0src_copy2 = copy_rtx (i0src);
3334 n_occurrences = 0;
3335 subst_low_luid = DF_INSN_LUID (i0);
3336 newpat = subst (newpat, i0dest, i0src, 0, 0, 0);
3337 substed_i0 = 1;
3340 /* Fail if an autoincrement side-effect has been duplicated. Be careful
3341 to count all the ways that I2SRC and I1SRC can be used. */
3342 if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
3343 && i2_is_used + added_sets_2 > 1)
3344 || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3345 && (i1_is_used + added_sets_1 + (added_sets_2 && i1_feeds_i2_n)
3346 > 1))
3347 || (i0 != 0 && FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3348 && (n_occurrences + added_sets_0
3349 + (added_sets_1 && i0_feeds_i1_n)
3350 + (added_sets_2 && i0_feeds_i2_n)
3351 > 1))
3352 /* Fail if we tried to make a new register. */
3353 || max_reg_num () != maxreg
3354 /* Fail if we couldn't do something and have a CLOBBER. */
3355 || GET_CODE (newpat) == CLOBBER
3356 /* Fail if this new pattern is a MULT and we didn't have one before
3357 at the outer level. */
3358 || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
3359 && ! have_mult))
3361 undo_all ();
3362 return 0;
3365 /* If the actions of the earlier insns must be kept
3366 in addition to substituting them into the latest one,
3367 we must make a new PARALLEL for the latest insn
3368 to hold additional the SETs. */
3370 if (added_sets_0 || added_sets_1 || added_sets_2)
3372 int extra_sets = added_sets_0 + added_sets_1 + added_sets_2;
3373 combine_extras++;
3375 if (GET_CODE (newpat) == PARALLEL)
3377 rtvec old = XVEC (newpat, 0);
3378 total_sets = XVECLEN (newpat, 0) + extra_sets;
3379 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3380 memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
3381 sizeof (old->elem[0]) * old->num_elem);
3383 else
3385 rtx old = newpat;
3386 total_sets = 1 + extra_sets;
3387 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3388 XVECEXP (newpat, 0, 0) = old;
3391 if (added_sets_0)
3392 XVECEXP (newpat, 0, --total_sets) = i0pat;
3394 if (added_sets_1)
3396 rtx t = i1pat;
3397 if (i0_feeds_i1_n)
3398 t = subst (t, i0dest, i0src_copy ? i0src_copy : i0src, 0, 0, 0);
3400 XVECEXP (newpat, 0, --total_sets) = t;
3402 if (added_sets_2)
3404 rtx t = i2pat;
3405 if (i1_feeds_i2_n)
3406 t = subst (t, i1dest, i1src_copy ? i1src_copy : i1src, 0, 0,
3407 i0_feeds_i1_n && i0dest_in_i0src);
3408 if ((i0_feeds_i1_n && i1_feeds_i2_n) || i0_feeds_i2_n)
3409 t = subst (t, i0dest, i0src_copy2 ? i0src_copy2 : i0src, 0, 0, 0);
3411 XVECEXP (newpat, 0, --total_sets) = t;
3415 validate_replacement:
3417 /* Note which hard regs this insn has as inputs. */
3418 mark_used_regs_combine (newpat);
3420 /* If recog_for_combine fails, it strips existing clobbers. If we'll
3421 consider splitting this pattern, we might need these clobbers. */
3422 if (i1 && GET_CODE (newpat) == PARALLEL
3423 && GET_CODE (XVECEXP (newpat, 0, XVECLEN (newpat, 0) - 1)) == CLOBBER)
3425 int len = XVECLEN (newpat, 0);
3427 newpat_vec_with_clobbers = rtvec_alloc (len);
3428 for (i = 0; i < len; i++)
3429 RTVEC_ELT (newpat_vec_with_clobbers, i) = XVECEXP (newpat, 0, i);
3432 /* We have recognized nothing yet. */
3433 insn_code_number = -1;
3435 /* See if this is a PARALLEL of two SETs where one SET's destination is
3436 a register that is unused and this isn't marked as an instruction that
3437 might trap in an EH region. In that case, we just need the other SET.
3438 We prefer this over the PARALLEL.
3440 This can occur when simplifying a divmod insn. We *must* test for this
3441 case here because the code below that splits two independent SETs doesn't
3442 handle this case correctly when it updates the register status.
3444 It's pointless doing this if we originally had two sets, one from
3445 i3, and one from i2. Combining then splitting the parallel results
3446 in the original i2 again plus an invalid insn (which we delete).
3447 The net effect is only to move instructions around, which makes
3448 debug info less accurate. */
3450 if (!(added_sets_2 && i1 == 0)
3451 && GET_CODE (newpat) == PARALLEL
3452 && XVECLEN (newpat, 0) == 2
3453 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3454 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3455 && asm_noperands (newpat) < 0)
3457 rtx set0 = XVECEXP (newpat, 0, 0);
3458 rtx set1 = XVECEXP (newpat, 0, 1);
3459 rtx oldpat = newpat;
3461 if (((REG_P (SET_DEST (set1))
3462 && find_reg_note (i3, REG_UNUSED, SET_DEST (set1)))
3463 || (GET_CODE (SET_DEST (set1)) == SUBREG
3464 && find_reg_note (i3, REG_UNUSED, SUBREG_REG (SET_DEST (set1)))))
3465 && insn_nothrow_p (i3)
3466 && !side_effects_p (SET_SRC (set1)))
3468 newpat = set0;
3469 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3472 else if (((REG_P (SET_DEST (set0))
3473 && find_reg_note (i3, REG_UNUSED, SET_DEST (set0)))
3474 || (GET_CODE (SET_DEST (set0)) == SUBREG
3475 && find_reg_note (i3, REG_UNUSED,
3476 SUBREG_REG (SET_DEST (set0)))))
3477 && insn_nothrow_p (i3)
3478 && !side_effects_p (SET_SRC (set0)))
3480 newpat = set1;
3481 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3483 if (insn_code_number >= 0)
3484 changed_i3_dest = 1;
3487 if (insn_code_number < 0)
3488 newpat = oldpat;
3491 /* Is the result of combination a valid instruction? */
3492 if (insn_code_number < 0)
3493 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3495 /* If we were combining three insns and the result is a simple SET
3496 with no ASM_OPERANDS that wasn't recognized, try to split it into two
3497 insns. There are two ways to do this. It can be split using a
3498 machine-specific method (like when you have an addition of a large
3499 constant) or by combine in the function find_split_point. */
3501 if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
3502 && asm_noperands (newpat) < 0)
3504 rtx parallel, *split;
3505 rtx_insn *m_split_insn;
3507 /* See if the MD file can split NEWPAT. If it can't, see if letting it
3508 use I2DEST as a scratch register will help. In the latter case,
3509 convert I2DEST to the mode of the source of NEWPAT if we can. */
3511 m_split_insn = combine_split_insns (newpat, i3);
3513 /* We can only use I2DEST as a scratch reg if it doesn't overlap any
3514 inputs of NEWPAT. */
3516 /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
3517 possible to try that as a scratch reg. This would require adding
3518 more code to make it work though. */
3520 if (m_split_insn == 0 && ! reg_overlap_mentioned_p (i2dest, newpat))
3522 machine_mode new_mode = GET_MODE (SET_DEST (newpat));
3524 /* First try to split using the original register as a
3525 scratch register. */
3526 parallel = gen_rtx_PARALLEL (VOIDmode,
3527 gen_rtvec (2, newpat,
3528 gen_rtx_CLOBBER (VOIDmode,
3529 i2dest)));
3530 m_split_insn = combine_split_insns (parallel, i3);
3532 /* If that didn't work, try changing the mode of I2DEST if
3533 we can. */
3534 if (m_split_insn == 0
3535 && new_mode != GET_MODE (i2dest)
3536 && new_mode != VOIDmode
3537 && can_change_dest_mode (i2dest, added_sets_2, new_mode))
3539 machine_mode old_mode = GET_MODE (i2dest);
3540 rtx ni2dest;
3542 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3543 ni2dest = gen_rtx_REG (new_mode, REGNO (i2dest));
3544 else
3546 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], new_mode);
3547 ni2dest = regno_reg_rtx[REGNO (i2dest)];
3550 parallel = (gen_rtx_PARALLEL
3551 (VOIDmode,
3552 gen_rtvec (2, newpat,
3553 gen_rtx_CLOBBER (VOIDmode,
3554 ni2dest))));
3555 m_split_insn = combine_split_insns (parallel, i3);
3557 if (m_split_insn == 0
3558 && REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
3560 struct undo *buf;
3562 adjust_reg_mode (regno_reg_rtx[REGNO (i2dest)], old_mode);
3563 buf = undobuf.undos;
3564 undobuf.undos = buf->next;
3565 buf->next = undobuf.frees;
3566 undobuf.frees = buf;
3570 i2scratch = m_split_insn != 0;
3573 /* If recog_for_combine has discarded clobbers, try to use them
3574 again for the split. */
3575 if (m_split_insn == 0 && newpat_vec_with_clobbers)
3577 parallel = gen_rtx_PARALLEL (VOIDmode, newpat_vec_with_clobbers);
3578 m_split_insn = combine_split_insns (parallel, i3);
3581 if (m_split_insn && NEXT_INSN (m_split_insn) == NULL_RTX)
3583 rtx m_split_pat = PATTERN (m_split_insn);
3584 insn_code_number = recog_for_combine (&m_split_pat, i3, &new_i3_notes);
3585 if (insn_code_number >= 0)
3586 newpat = m_split_pat;
3588 else if (m_split_insn && NEXT_INSN (NEXT_INSN (m_split_insn)) == NULL_RTX
3589 && (next_nonnote_nondebug_insn (i2) == i3
3590 || ! use_crosses_set_p (PATTERN (m_split_insn), DF_INSN_LUID (i2))))
3592 rtx i2set, i3set;
3593 rtx newi3pat = PATTERN (NEXT_INSN (m_split_insn));
3594 newi2pat = PATTERN (m_split_insn);
3596 i3set = single_set (NEXT_INSN (m_split_insn));
3597 i2set = single_set (m_split_insn);
3599 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3601 /* If I2 or I3 has multiple SETs, we won't know how to track
3602 register status, so don't use these insns. If I2's destination
3603 is used between I2 and I3, we also can't use these insns. */
3605 if (i2_code_number >= 0 && i2set && i3set
3606 && (next_nonnote_nondebug_insn (i2) == i3
3607 || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
3608 insn_code_number = recog_for_combine (&newi3pat, i3,
3609 &new_i3_notes);
3610 if (insn_code_number >= 0)
3611 newpat = newi3pat;
3613 /* It is possible that both insns now set the destination of I3.
3614 If so, we must show an extra use of it. */
3616 if (insn_code_number >= 0)
3618 rtx new_i3_dest = SET_DEST (i3set);
3619 rtx new_i2_dest = SET_DEST (i2set);
3621 while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
3622 || GET_CODE (new_i3_dest) == STRICT_LOW_PART
3623 || GET_CODE (new_i3_dest) == SUBREG)
3624 new_i3_dest = XEXP (new_i3_dest, 0);
3626 while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
3627 || GET_CODE (new_i2_dest) == STRICT_LOW_PART
3628 || GET_CODE (new_i2_dest) == SUBREG)
3629 new_i2_dest = XEXP (new_i2_dest, 0);
3631 if (REG_P (new_i3_dest)
3632 && REG_P (new_i2_dest)
3633 && REGNO (new_i3_dest) == REGNO (new_i2_dest))
3634 INC_REG_N_SETS (REGNO (new_i2_dest), 1);
3638 /* If we can split it and use I2DEST, go ahead and see if that
3639 helps things be recognized. Verify that none of the registers
3640 are set between I2 and I3. */
3641 if (insn_code_number < 0
3642 && (split = find_split_point (&newpat, i3, false)) != 0
3643 #ifdef HAVE_cc0
3644 && REG_P (i2dest)
3645 #endif
3646 /* We need I2DEST in the proper mode. If it is a hard register
3647 or the only use of a pseudo, we can change its mode.
3648 Make sure we don't change a hard register to have a mode that
3649 isn't valid for it, or change the number of registers. */
3650 && (GET_MODE (*split) == GET_MODE (i2dest)
3651 || GET_MODE (*split) == VOIDmode
3652 || can_change_dest_mode (i2dest, added_sets_2,
3653 GET_MODE (*split)))
3654 && (next_nonnote_nondebug_insn (i2) == i3
3655 || ! use_crosses_set_p (*split, DF_INSN_LUID (i2)))
3656 /* We can't overwrite I2DEST if its value is still used by
3657 NEWPAT. */
3658 && ! reg_referenced_p (i2dest, newpat))
3660 rtx newdest = i2dest;
3661 enum rtx_code split_code = GET_CODE (*split);
3662 machine_mode split_mode = GET_MODE (*split);
3663 bool subst_done = false;
3664 newi2pat = NULL_RTX;
3666 i2scratch = true;
3668 /* *SPLIT may be part of I2SRC, so make sure we have the
3669 original expression around for later debug processing.
3670 We should not need I2SRC any more in other cases. */
3671 if (MAY_HAVE_DEBUG_INSNS)
3672 i2src = copy_rtx (i2src);
3673 else
3674 i2src = NULL;
3676 /* Get NEWDEST as a register in the proper mode. We have already
3677 validated that we can do this. */
3678 if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
3680 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3681 newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
3682 else
3684 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], split_mode);
3685 newdest = regno_reg_rtx[REGNO (i2dest)];
3689 /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
3690 an ASHIFT. This can occur if it was inside a PLUS and hence
3691 appeared to be a memory address. This is a kludge. */
3692 if (split_code == MULT
3693 && CONST_INT_P (XEXP (*split, 1))
3694 && INTVAL (XEXP (*split, 1)) > 0
3695 && (i = exact_log2 (UINTVAL (XEXP (*split, 1)))) >= 0)
3697 SUBST (*split, gen_rtx_ASHIFT (split_mode,
3698 XEXP (*split, 0), GEN_INT (i)));
3699 /* Update split_code because we may not have a multiply
3700 anymore. */
3701 split_code = GET_CODE (*split);
3704 #ifdef INSN_SCHEDULING
3705 /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
3706 be written as a ZERO_EXTEND. */
3707 if (split_code == SUBREG && MEM_P (SUBREG_REG (*split)))
3709 #ifdef LOAD_EXTEND_OP
3710 /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
3711 what it really is. */
3712 if (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (*split)))
3713 == SIGN_EXTEND)
3714 SUBST (*split, gen_rtx_SIGN_EXTEND (split_mode,
3715 SUBREG_REG (*split)));
3716 else
3717 #endif
3718 SUBST (*split, gen_rtx_ZERO_EXTEND (split_mode,
3719 SUBREG_REG (*split)));
3721 #endif
3723 /* Attempt to split binary operators using arithmetic identities. */
3724 if (BINARY_P (SET_SRC (newpat))
3725 && split_mode == GET_MODE (SET_SRC (newpat))
3726 && ! side_effects_p (SET_SRC (newpat)))
3728 rtx setsrc = SET_SRC (newpat);
3729 machine_mode mode = GET_MODE (setsrc);
3730 enum rtx_code code = GET_CODE (setsrc);
3731 rtx src_op0 = XEXP (setsrc, 0);
3732 rtx src_op1 = XEXP (setsrc, 1);
3734 /* Split "X = Y op Y" as "Z = Y; X = Z op Z". */
3735 if (rtx_equal_p (src_op0, src_op1))
3737 newi2pat = gen_rtx_SET (VOIDmode, newdest, src_op0);
3738 SUBST (XEXP (setsrc, 0), newdest);
3739 SUBST (XEXP (setsrc, 1), newdest);
3740 subst_done = true;
3742 /* Split "((P op Q) op R) op S" where op is PLUS or MULT. */
3743 else if ((code == PLUS || code == MULT)
3744 && GET_CODE (src_op0) == code
3745 && GET_CODE (XEXP (src_op0, 0)) == code
3746 && (INTEGRAL_MODE_P (mode)
3747 || (FLOAT_MODE_P (mode)
3748 && flag_unsafe_math_optimizations)))
3750 rtx p = XEXP (XEXP (src_op0, 0), 0);
3751 rtx q = XEXP (XEXP (src_op0, 0), 1);
3752 rtx r = XEXP (src_op0, 1);
3753 rtx s = src_op1;
3755 /* Split both "((X op Y) op X) op Y" and
3756 "((X op Y) op Y) op X" as "T op T" where T is
3757 "X op Y". */
3758 if ((rtx_equal_p (p,r) && rtx_equal_p (q,s))
3759 || (rtx_equal_p (p,s) && rtx_equal_p (q,r)))
3761 newi2pat = gen_rtx_SET (VOIDmode, newdest,
3762 XEXP (src_op0, 0));
3763 SUBST (XEXP (setsrc, 0), newdest);
3764 SUBST (XEXP (setsrc, 1), newdest);
3765 subst_done = true;
3767 /* Split "((X op X) op Y) op Y)" as "T op T" where
3768 T is "X op Y". */
3769 else if (rtx_equal_p (p,q) && rtx_equal_p (r,s))
3771 rtx tmp = simplify_gen_binary (code, mode, p, r);
3772 newi2pat = gen_rtx_SET (VOIDmode, newdest, tmp);
3773 SUBST (XEXP (setsrc, 0), newdest);
3774 SUBST (XEXP (setsrc, 1), newdest);
3775 subst_done = true;
3780 if (!subst_done)
3782 newi2pat = gen_rtx_SET (VOIDmode, newdest, *split);
3783 SUBST (*split, newdest);
3786 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3788 /* recog_for_combine might have added CLOBBERs to newi2pat.
3789 Make sure NEWPAT does not depend on the clobbered regs. */
3790 if (GET_CODE (newi2pat) == PARALLEL)
3791 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3792 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3794 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3795 if (reg_overlap_mentioned_p (reg, newpat))
3797 undo_all ();
3798 return 0;
3802 /* If the split point was a MULT and we didn't have one before,
3803 don't use one now. */
3804 if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
3805 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3809 /* Check for a case where we loaded from memory in a narrow mode and
3810 then sign extended it, but we need both registers. In that case,
3811 we have a PARALLEL with both loads from the same memory location.
3812 We can split this into a load from memory followed by a register-register
3813 copy. This saves at least one insn, more if register allocation can
3814 eliminate the copy.
3816 We cannot do this if the destination of the first assignment is a
3817 condition code register or cc0. We eliminate this case by making sure
3818 the SET_DEST and SET_SRC have the same mode.
3820 We cannot do this if the destination of the second assignment is
3821 a register that we have already assumed is zero-extended. Similarly
3822 for a SUBREG of such a register. */
3824 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3825 && GET_CODE (newpat) == PARALLEL
3826 && XVECLEN (newpat, 0) == 2
3827 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3828 && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
3829 && (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0)))
3830 == GET_MODE (SET_SRC (XVECEXP (newpat, 0, 0))))
3831 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3832 && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3833 XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
3834 && ! use_crosses_set_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3835 DF_INSN_LUID (i2))
3836 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3837 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3838 && ! (temp_expr = SET_DEST (XVECEXP (newpat, 0, 1)),
3839 (REG_P (temp_expr)
3840 && reg_stat[REGNO (temp_expr)].nonzero_bits != 0
3841 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < BITS_PER_WORD
3842 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < HOST_BITS_PER_INT
3843 && (reg_stat[REGNO (temp_expr)].nonzero_bits
3844 != GET_MODE_MASK (word_mode))))
3845 && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
3846 && (temp_expr = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
3847 (REG_P (temp_expr)
3848 && reg_stat[REGNO (temp_expr)].nonzero_bits != 0
3849 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < BITS_PER_WORD
3850 && GET_MODE_PRECISION (GET_MODE (temp_expr)) < HOST_BITS_PER_INT
3851 && (reg_stat[REGNO (temp_expr)].nonzero_bits
3852 != GET_MODE_MASK (word_mode)))))
3853 && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3854 SET_SRC (XVECEXP (newpat, 0, 1)))
3855 && ! find_reg_note (i3, REG_UNUSED,
3856 SET_DEST (XVECEXP (newpat, 0, 0))))
3858 rtx ni2dest;
3860 newi2pat = XVECEXP (newpat, 0, 0);
3861 ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
3862 newpat = XVECEXP (newpat, 0, 1);
3863 SUBST (SET_SRC (newpat),
3864 gen_lowpart (GET_MODE (SET_SRC (newpat)), ni2dest));
3865 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3867 if (i2_code_number >= 0)
3868 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3870 if (insn_code_number >= 0)
3871 swap_i2i3 = 1;
3874 /* Similarly, check for a case where we have a PARALLEL of two independent
3875 SETs but we started with three insns. In this case, we can do the sets
3876 as two separate insns. This case occurs when some SET allows two
3877 other insns to combine, but the destination of that SET is still live.
3879 Also do this if we started with two insns and (at least) one of the
3880 resulting sets is a noop; this noop will be deleted later. */
3882 else if (insn_code_number < 0 && asm_noperands (newpat) < 0
3883 && GET_CODE (newpat) == PARALLEL
3884 && XVECLEN (newpat, 0) == 2
3885 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3886 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3887 && (i1 || set_noop_p (XVECEXP (newpat, 0, 0))
3888 || set_noop_p (XVECEXP (newpat, 0, 1)))
3889 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
3890 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
3891 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3892 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3893 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3894 XVECEXP (newpat, 0, 0))
3895 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
3896 XVECEXP (newpat, 0, 1))
3897 && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
3898 && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1)))))
3900 rtx set0 = XVECEXP (newpat, 0, 0);
3901 rtx set1 = XVECEXP (newpat, 0, 1);
3903 /* Normally, it doesn't matter which of the two is done first,
3904 but the one that references cc0 can't be the second, and
3905 one which uses any regs/memory set in between i2 and i3 can't
3906 be first. The PARALLEL might also have been pre-existing in i3,
3907 so we need to make sure that we won't wrongly hoist a SET to i2
3908 that would conflict with a death note present in there. */
3909 if (!use_crosses_set_p (SET_SRC (set1), DF_INSN_LUID (i2))
3910 && !(REG_P (SET_DEST (set1))
3911 && find_reg_note (i2, REG_DEAD, SET_DEST (set1)))
3912 && !(GET_CODE (SET_DEST (set1)) == SUBREG
3913 && find_reg_note (i2, REG_DEAD,
3914 SUBREG_REG (SET_DEST (set1))))
3915 #ifdef HAVE_cc0
3916 && !reg_referenced_p (cc0_rtx, set0)
3917 #endif
3918 /* If I3 is a jump, ensure that set0 is a jump so that
3919 we do not create invalid RTL. */
3920 && (!JUMP_P (i3) || SET_DEST (set0) == pc_rtx)
3923 newi2pat = set1;
3924 newpat = set0;
3926 else if (!use_crosses_set_p (SET_SRC (set0), DF_INSN_LUID (i2))
3927 && !(REG_P (SET_DEST (set0))
3928 && find_reg_note (i2, REG_DEAD, SET_DEST (set0)))
3929 && !(GET_CODE (SET_DEST (set0)) == SUBREG
3930 && find_reg_note (i2, REG_DEAD,
3931 SUBREG_REG (SET_DEST (set0))))
3932 #ifdef HAVE_cc0
3933 && !reg_referenced_p (cc0_rtx, set1)
3934 #endif
3935 /* If I3 is a jump, ensure that set1 is a jump so that
3936 we do not create invalid RTL. */
3937 && (!JUMP_P (i3) || SET_DEST (set1) == pc_rtx)
3940 newi2pat = set0;
3941 newpat = set1;
3943 else
3945 undo_all ();
3946 return 0;
3949 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3951 if (i2_code_number >= 0)
3953 /* recog_for_combine might have added CLOBBERs to newi2pat.
3954 Make sure NEWPAT does not depend on the clobbered regs. */
3955 if (GET_CODE (newi2pat) == PARALLEL)
3957 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3958 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3960 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3961 if (reg_overlap_mentioned_p (reg, newpat))
3963 undo_all ();
3964 return 0;
3969 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3973 /* If it still isn't recognized, fail and change things back the way they
3974 were. */
3975 if ((insn_code_number < 0
3976 /* Is the result a reasonable ASM_OPERANDS? */
3977 && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
3979 undo_all ();
3980 return 0;
3983 /* If we had to change another insn, make sure it is valid also. */
3984 if (undobuf.other_insn)
3986 CLEAR_HARD_REG_SET (newpat_used_regs);
3988 other_pat = PATTERN (undobuf.other_insn);
3989 other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
3990 &new_other_notes);
3992 if (other_code_number < 0 && ! check_asm_operands (other_pat))
3994 undo_all ();
3995 return 0;
3999 #ifdef HAVE_cc0
4000 /* If I2 is the CC0 setter and I3 is the CC0 user then check whether
4001 they are adjacent to each other or not. */
4003 rtx_insn *p = prev_nonnote_insn (i3);
4004 if (p && p != i2 && NONJUMP_INSN_P (p) && newi2pat
4005 && sets_cc0_p (newi2pat))
4007 undo_all ();
4008 return 0;
4011 #endif
4013 /* Only allow this combination if insn_rtx_costs reports that the
4014 replacement instructions are cheaper than the originals. */
4015 if (!combine_validate_cost (i0, i1, i2, i3, newpat, newi2pat, other_pat))
4017 undo_all ();
4018 return 0;
4021 if (MAY_HAVE_DEBUG_INSNS)
4023 struct undo *undo;
4025 for (undo = undobuf.undos; undo; undo = undo->next)
4026 if (undo->kind == UNDO_MODE)
4028 rtx reg = *undo->where.r;
4029 machine_mode new_mode = GET_MODE (reg);
4030 machine_mode old_mode = undo->old_contents.m;
4032 /* Temporarily revert mode back. */
4033 adjust_reg_mode (reg, old_mode);
4035 if (reg == i2dest && i2scratch)
4037 /* If we used i2dest as a scratch register with a
4038 different mode, substitute it for the original
4039 i2src while its original mode is temporarily
4040 restored, and then clear i2scratch so that we don't
4041 do it again later. */
4042 propagate_for_debug (i2, last_combined_insn, reg, i2src,
4043 this_basic_block);
4044 i2scratch = false;
4045 /* Put back the new mode. */
4046 adjust_reg_mode (reg, new_mode);
4048 else
4050 rtx tempreg = gen_raw_REG (old_mode, REGNO (reg));
4051 rtx_insn *first, *last;
4053 if (reg == i2dest)
4055 first = i2;
4056 last = last_combined_insn;
4058 else
4060 first = i3;
4061 last = undobuf.other_insn;
4062 gcc_assert (last);
4063 if (DF_INSN_LUID (last)
4064 < DF_INSN_LUID (last_combined_insn))
4065 last = last_combined_insn;
4068 /* We're dealing with a reg that changed mode but not
4069 meaning, so we want to turn it into a subreg for
4070 the new mode. However, because of REG sharing and
4071 because its mode had already changed, we have to do
4072 it in two steps. First, replace any debug uses of
4073 reg, with its original mode temporarily restored,
4074 with this copy we have created; then, replace the
4075 copy with the SUBREG of the original shared reg,
4076 once again changed to the new mode. */
4077 propagate_for_debug (first, last, reg, tempreg,
4078 this_basic_block);
4079 adjust_reg_mode (reg, new_mode);
4080 propagate_for_debug (first, last, tempreg,
4081 lowpart_subreg (old_mode, reg, new_mode),
4082 this_basic_block);
4087 /* If we will be able to accept this, we have made a
4088 change to the destination of I3. This requires us to
4089 do a few adjustments. */
4091 if (changed_i3_dest)
4093 PATTERN (i3) = newpat;
4094 adjust_for_new_dest (i3);
4097 /* We now know that we can do this combination. Merge the insns and
4098 update the status of registers and LOG_LINKS. */
4100 if (undobuf.other_insn)
4102 rtx note, next;
4104 PATTERN (undobuf.other_insn) = other_pat;
4106 /* If any of the notes in OTHER_INSN were REG_DEAD or REG_UNUSED,
4107 ensure that they are still valid. Then add any non-duplicate
4108 notes added by recog_for_combine. */
4109 for (note = REG_NOTES (undobuf.other_insn); note; note = next)
4111 next = XEXP (note, 1);
4113 if ((REG_NOTE_KIND (note) == REG_DEAD
4114 && !reg_referenced_p (XEXP (note, 0),
4115 PATTERN (undobuf.other_insn)))
4116 ||(REG_NOTE_KIND (note) == REG_UNUSED
4117 && !reg_set_p (XEXP (note, 0),
4118 PATTERN (undobuf.other_insn))))
4119 remove_note (undobuf.other_insn, note);
4122 distribute_notes (new_other_notes, undobuf.other_insn,
4123 undobuf.other_insn, NULL, NULL_RTX, NULL_RTX,
4124 NULL_RTX);
4127 if (swap_i2i3)
4129 rtx_insn *insn;
4130 struct insn_link *link;
4131 rtx ni2dest;
4133 /* I3 now uses what used to be its destination and which is now
4134 I2's destination. This requires us to do a few adjustments. */
4135 PATTERN (i3) = newpat;
4136 adjust_for_new_dest (i3);
4138 /* We need a LOG_LINK from I3 to I2. But we used to have one,
4139 so we still will.
4141 However, some later insn might be using I2's dest and have
4142 a LOG_LINK pointing at I3. We must remove this link.
4143 The simplest way to remove the link is to point it at I1,
4144 which we know will be a NOTE. */
4146 /* newi2pat is usually a SET here; however, recog_for_combine might
4147 have added some clobbers. */
4148 if (GET_CODE (newi2pat) == PARALLEL)
4149 ni2dest = SET_DEST (XVECEXP (newi2pat, 0, 0));
4150 else
4151 ni2dest = SET_DEST (newi2pat);
4153 for (insn = NEXT_INSN (i3);
4154 insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
4155 || insn != BB_HEAD (this_basic_block->next_bb));
4156 insn = NEXT_INSN (insn))
4158 if (INSN_P (insn) && reg_referenced_p (ni2dest, PATTERN (insn)))
4160 FOR_EACH_LOG_LINK (link, insn)
4161 if (link->insn == i3)
4162 link->insn = i1;
4164 break;
4170 rtx i3notes, i2notes, i1notes = 0, i0notes = 0;
4171 struct insn_link *i3links, *i2links, *i1links = 0, *i0links = 0;
4172 rtx midnotes = 0;
4173 int from_luid;
4174 /* Compute which registers we expect to eliminate. newi2pat may be setting
4175 either i3dest or i2dest, so we must check it. */
4176 rtx elim_i2 = ((newi2pat && reg_set_p (i2dest, newi2pat))
4177 || i2dest_in_i2src || i2dest_in_i1src || i2dest_in_i0src
4178 || !i2dest_killed
4179 ? 0 : i2dest);
4180 /* For i1, we need to compute both local elimination and global
4181 elimination information with respect to newi2pat because i1dest
4182 may be the same as i3dest, in which case newi2pat may be setting
4183 i1dest. Global information is used when distributing REG_DEAD
4184 note for i2 and i3, in which case it does matter if newi2pat sets
4185 i1dest or not.
4187 Local information is used when distributing REG_DEAD note for i1,
4188 in which case it doesn't matter if newi2pat sets i1dest or not.
4189 See PR62151, if we have four insns combination:
4190 i0: r0 <- i0src
4191 i1: r1 <- i1src (using r0)
4192 REG_DEAD (r0)
4193 i2: r0 <- i2src (using r1)
4194 i3: r3 <- i3src (using r0)
4195 ix: using r0
4196 From i1's point of view, r0 is eliminated, no matter if it is set
4197 by newi2pat or not. In other words, REG_DEAD info for r0 in i1
4198 should be discarded.
4200 Note local information only affects cases in forms like "I1->I2->I3",
4201 "I0->I1->I2->I3" or "I0&I1->I2, I2->I3". For other cases like
4202 "I0->I1, I1&I2->I3" or "I1&I2->I3", newi2pat won't set i1dest or
4203 i0dest anyway. */
4204 rtx local_elim_i1 = (i1 == 0 || i1dest_in_i1src || i1dest_in_i0src
4205 || !i1dest_killed
4206 ? 0 : i1dest);
4207 rtx elim_i1 = (local_elim_i1 == 0
4208 || (newi2pat && reg_set_p (i1dest, newi2pat))
4209 ? 0 : i1dest);
4210 /* Same case as i1. */
4211 rtx local_elim_i0 = (i0 == 0 || i0dest_in_i0src || !i0dest_killed
4212 ? 0 : i0dest);
4213 rtx elim_i0 = (local_elim_i0 == 0
4214 || (newi2pat && reg_set_p (i0dest, newi2pat))
4215 ? 0 : i0dest);
4217 /* Get the old REG_NOTES and LOG_LINKS from all our insns and
4218 clear them. */
4219 i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
4220 i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
4221 if (i1)
4222 i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
4223 if (i0)
4224 i0notes = REG_NOTES (i0), i0links = LOG_LINKS (i0);
4226 /* Ensure that we do not have something that should not be shared but
4227 occurs multiple times in the new insns. Check this by first
4228 resetting all the `used' flags and then copying anything is shared. */
4230 reset_used_flags (i3notes);
4231 reset_used_flags (i2notes);
4232 reset_used_flags (i1notes);
4233 reset_used_flags (i0notes);
4234 reset_used_flags (newpat);
4235 reset_used_flags (newi2pat);
4236 if (undobuf.other_insn)
4237 reset_used_flags (PATTERN (undobuf.other_insn));
4239 i3notes = copy_rtx_if_shared (i3notes);
4240 i2notes = copy_rtx_if_shared (i2notes);
4241 i1notes = copy_rtx_if_shared (i1notes);
4242 i0notes = copy_rtx_if_shared (i0notes);
4243 newpat = copy_rtx_if_shared (newpat);
4244 newi2pat = copy_rtx_if_shared (newi2pat);
4245 if (undobuf.other_insn)
4246 reset_used_flags (PATTERN (undobuf.other_insn));
4248 INSN_CODE (i3) = insn_code_number;
4249 PATTERN (i3) = newpat;
4251 if (CALL_P (i3) && CALL_INSN_FUNCTION_USAGE (i3))
4253 rtx call_usage = CALL_INSN_FUNCTION_USAGE (i3);
4255 reset_used_flags (call_usage);
4256 call_usage = copy_rtx (call_usage);
4258 if (substed_i2)
4260 /* I2SRC must still be meaningful at this point. Some splitting
4261 operations can invalidate I2SRC, but those operations do not
4262 apply to calls. */
4263 gcc_assert (i2src);
4264 replace_rtx (call_usage, i2dest, i2src);
4267 if (substed_i1)
4268 replace_rtx (call_usage, i1dest, i1src);
4269 if (substed_i0)
4270 replace_rtx (call_usage, i0dest, i0src);
4272 CALL_INSN_FUNCTION_USAGE (i3) = call_usage;
4275 if (undobuf.other_insn)
4276 INSN_CODE (undobuf.other_insn) = other_code_number;
4278 /* We had one special case above where I2 had more than one set and
4279 we replaced a destination of one of those sets with the destination
4280 of I3. In that case, we have to update LOG_LINKS of insns later
4281 in this basic block. Note that this (expensive) case is rare.
4283 Also, in this case, we must pretend that all REG_NOTEs for I2
4284 actually came from I3, so that REG_UNUSED notes from I2 will be
4285 properly handled. */
4287 if (i3_subst_into_i2)
4289 for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
4290 if ((GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == SET
4291 || GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == CLOBBER)
4292 && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, i)))
4293 && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
4294 && ! find_reg_note (i2, REG_UNUSED,
4295 SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
4296 for (temp_insn = NEXT_INSN (i2);
4297 temp_insn
4298 && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
4299 || BB_HEAD (this_basic_block) != temp_insn);
4300 temp_insn = NEXT_INSN (temp_insn))
4301 if (temp_insn != i3 && INSN_P (temp_insn))
4302 FOR_EACH_LOG_LINK (link, temp_insn)
4303 if (link->insn == i2)
4304 link->insn = i3;
4306 if (i3notes)
4308 rtx link = i3notes;
4309 while (XEXP (link, 1))
4310 link = XEXP (link, 1);
4311 XEXP (link, 1) = i2notes;
4313 else
4314 i3notes = i2notes;
4315 i2notes = 0;
4318 LOG_LINKS (i3) = NULL;
4319 REG_NOTES (i3) = 0;
4320 LOG_LINKS (i2) = NULL;
4321 REG_NOTES (i2) = 0;
4323 if (newi2pat)
4325 if (MAY_HAVE_DEBUG_INSNS && i2scratch)
4326 propagate_for_debug (i2, last_combined_insn, i2dest, i2src,
4327 this_basic_block);
4328 INSN_CODE (i2) = i2_code_number;
4329 PATTERN (i2) = newi2pat;
4331 else
4333 if (MAY_HAVE_DEBUG_INSNS && i2src)
4334 propagate_for_debug (i2, last_combined_insn, i2dest, i2src,
4335 this_basic_block);
4336 SET_INSN_DELETED (i2);
4339 if (i1)
4341 LOG_LINKS (i1) = NULL;
4342 REG_NOTES (i1) = 0;
4343 if (MAY_HAVE_DEBUG_INSNS)
4344 propagate_for_debug (i1, last_combined_insn, i1dest, i1src,
4345 this_basic_block);
4346 SET_INSN_DELETED (i1);
4349 if (i0)
4351 LOG_LINKS (i0) = NULL;
4352 REG_NOTES (i0) = 0;
4353 if (MAY_HAVE_DEBUG_INSNS)
4354 propagate_for_debug (i0, last_combined_insn, i0dest, i0src,
4355 this_basic_block);
4356 SET_INSN_DELETED (i0);
4359 /* Get death notes for everything that is now used in either I3 or
4360 I2 and used to die in a previous insn. If we built two new
4361 patterns, move from I1 to I2 then I2 to I3 so that we get the
4362 proper movement on registers that I2 modifies. */
4364 if (i0)
4365 from_luid = DF_INSN_LUID (i0);
4366 else if (i1)
4367 from_luid = DF_INSN_LUID (i1);
4368 else
4369 from_luid = DF_INSN_LUID (i2);
4370 if (newi2pat)
4371 move_deaths (newi2pat, NULL_RTX, from_luid, i2, &midnotes);
4372 move_deaths (newpat, newi2pat, from_luid, i3, &midnotes);
4374 /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3. */
4375 if (i3notes)
4376 distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL,
4377 elim_i2, elim_i1, elim_i0);
4378 if (i2notes)
4379 distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL,
4380 elim_i2, elim_i1, elim_i0);
4381 if (i1notes)
4382 distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL,
4383 elim_i2, local_elim_i1, local_elim_i0);
4384 if (i0notes)
4385 distribute_notes (i0notes, i0, i3, newi2pat ? i2 : NULL,
4386 elim_i2, elim_i1, local_elim_i0);
4387 if (midnotes)
4388 distribute_notes (midnotes, NULL, i3, newi2pat ? i2 : NULL,
4389 elim_i2, elim_i1, elim_i0);
4391 /* Distribute any notes added to I2 or I3 by recog_for_combine. We
4392 know these are REG_UNUSED and want them to go to the desired insn,
4393 so we always pass it as i3. */
4395 if (newi2pat && new_i2_notes)
4396 distribute_notes (new_i2_notes, i2, i2, NULL, NULL_RTX, NULL_RTX,
4397 NULL_RTX);
4399 if (new_i3_notes)
4400 distribute_notes (new_i3_notes, i3, i3, NULL, NULL_RTX, NULL_RTX,
4401 NULL_RTX);
4403 /* If I3DEST was used in I3SRC, it really died in I3. We may need to
4404 put a REG_DEAD note for it somewhere. If NEWI2PAT exists and sets
4405 I3DEST, the death must be somewhere before I2, not I3. If we passed I3
4406 in that case, it might delete I2. Similarly for I2 and I1.
4407 Show an additional death due to the REG_DEAD note we make here. If
4408 we discard it in distribute_notes, we will decrement it again. */
4410 if (i3dest_killed)
4412 rtx new_note = alloc_reg_note (REG_DEAD, i3dest_killed, NULL_RTX);
4413 if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
4414 distribute_notes (new_note, NULL, i2, NULL, elim_i2,
4415 elim_i1, elim_i0);
4416 else
4417 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4418 elim_i2, elim_i1, elim_i0);
4421 if (i2dest_in_i2src)
4423 rtx new_note = alloc_reg_note (REG_DEAD, i2dest, NULL_RTX);
4424 if (newi2pat && reg_set_p (i2dest, newi2pat))
4425 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4426 NULL_RTX, NULL_RTX);
4427 else
4428 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4429 NULL_RTX, NULL_RTX, NULL_RTX);
4432 if (i1dest_in_i1src)
4434 rtx new_note = alloc_reg_note (REG_DEAD, i1dest, NULL_RTX);
4435 if (newi2pat && reg_set_p (i1dest, newi2pat))
4436 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4437 NULL_RTX, NULL_RTX);
4438 else
4439 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4440 NULL_RTX, NULL_RTX, NULL_RTX);
4443 if (i0dest_in_i0src)
4445 rtx new_note = alloc_reg_note (REG_DEAD, i0dest, NULL_RTX);
4446 if (newi2pat && reg_set_p (i0dest, newi2pat))
4447 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4448 NULL_RTX, NULL_RTX);
4449 else
4450 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4451 NULL_RTX, NULL_RTX, NULL_RTX);
4454 distribute_links (i3links);
4455 distribute_links (i2links);
4456 distribute_links (i1links);
4457 distribute_links (i0links);
4459 if (REG_P (i2dest))
4461 struct insn_link *link;
4462 rtx_insn *i2_insn = 0;
4463 rtx i2_val = 0, set;
4465 /* The insn that used to set this register doesn't exist, and
4466 this life of the register may not exist either. See if one of
4467 I3's links points to an insn that sets I2DEST. If it does,
4468 that is now the last known value for I2DEST. If we don't update
4469 this and I2 set the register to a value that depended on its old
4470 contents, we will get confused. If this insn is used, thing
4471 will be set correctly in combine_instructions. */
4472 FOR_EACH_LOG_LINK (link, i3)
4473 if ((set = single_set (link->insn)) != 0
4474 && rtx_equal_p (i2dest, SET_DEST (set)))
4475 i2_insn = link->insn, i2_val = SET_SRC (set);
4477 record_value_for_reg (i2dest, i2_insn, i2_val);
4479 /* If the reg formerly set in I2 died only once and that was in I3,
4480 zero its use count so it won't make `reload' do any work. */
4481 if (! added_sets_2
4482 && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
4483 && ! i2dest_in_i2src)
4484 INC_REG_N_SETS (REGNO (i2dest), -1);
4487 if (i1 && REG_P (i1dest))
4489 struct insn_link *link;
4490 rtx_insn *i1_insn = 0;
4491 rtx i1_val = 0, set;
4493 FOR_EACH_LOG_LINK (link, i3)
4494 if ((set = single_set (link->insn)) != 0
4495 && rtx_equal_p (i1dest, SET_DEST (set)))
4496 i1_insn = link->insn, i1_val = SET_SRC (set);
4498 record_value_for_reg (i1dest, i1_insn, i1_val);
4500 if (! added_sets_1 && ! i1dest_in_i1src)
4501 INC_REG_N_SETS (REGNO (i1dest), -1);
4504 if (i0 && REG_P (i0dest))
4506 struct insn_link *link;
4507 rtx_insn *i0_insn = 0;
4508 rtx i0_val = 0, set;
4510 FOR_EACH_LOG_LINK (link, i3)
4511 if ((set = single_set (link->insn)) != 0
4512 && rtx_equal_p (i0dest, SET_DEST (set)))
4513 i0_insn = link->insn, i0_val = SET_SRC (set);
4515 record_value_for_reg (i0dest, i0_insn, i0_val);
4517 if (! added_sets_0 && ! i0dest_in_i0src)
4518 INC_REG_N_SETS (REGNO (i0dest), -1);
4521 /* Update reg_stat[].nonzero_bits et al for any changes that may have
4522 been made to this insn. The order is important, because newi2pat
4523 can affect nonzero_bits of newpat. */
4524 if (newi2pat)
4525 note_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
4526 note_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
4529 if (undobuf.other_insn != NULL_RTX)
4531 if (dump_file)
4533 fprintf (dump_file, "modifying other_insn ");
4534 dump_insn_slim (dump_file, undobuf.other_insn);
4536 df_insn_rescan (undobuf.other_insn);
4539 if (i0 && !(NOTE_P (i0) && (NOTE_KIND (i0) == NOTE_INSN_DELETED)))
4541 if (dump_file)
4543 fprintf (dump_file, "modifying insn i0 ");
4544 dump_insn_slim (dump_file, i0);
4546 df_insn_rescan (i0);
4549 if (i1 && !(NOTE_P (i1) && (NOTE_KIND (i1) == NOTE_INSN_DELETED)))
4551 if (dump_file)
4553 fprintf (dump_file, "modifying insn i1 ");
4554 dump_insn_slim (dump_file, i1);
4556 df_insn_rescan (i1);
4559 if (i2 && !(NOTE_P (i2) && (NOTE_KIND (i2) == NOTE_INSN_DELETED)))
4561 if (dump_file)
4563 fprintf (dump_file, "modifying insn i2 ");
4564 dump_insn_slim (dump_file, i2);
4566 df_insn_rescan (i2);
4569 if (i3 && !(NOTE_P (i3) && (NOTE_KIND (i3) == NOTE_INSN_DELETED)))
4571 if (dump_file)
4573 fprintf (dump_file, "modifying insn i3 ");
4574 dump_insn_slim (dump_file, i3);
4576 df_insn_rescan (i3);
4579 /* Set new_direct_jump_p if a new return or simple jump instruction
4580 has been created. Adjust the CFG accordingly. */
4581 if (returnjump_p (i3) || any_uncondjump_p (i3))
4583 *new_direct_jump_p = 1;
4584 mark_jump_label (PATTERN (i3), i3, 0);
4585 update_cfg_for_uncondjump (i3);
4588 if (undobuf.other_insn != NULL_RTX
4589 && (returnjump_p (undobuf.other_insn)
4590 || any_uncondjump_p (undobuf.other_insn)))
4592 *new_direct_jump_p = 1;
4593 update_cfg_for_uncondjump (undobuf.other_insn);
4596 /* A noop might also need cleaning up of CFG, if it comes from the
4597 simplification of a jump. */
4598 if (JUMP_P (i3)
4599 && GET_CODE (newpat) == SET
4600 && SET_SRC (newpat) == pc_rtx
4601 && SET_DEST (newpat) == pc_rtx)
4603 *new_direct_jump_p = 1;
4604 update_cfg_for_uncondjump (i3);
4607 if (undobuf.other_insn != NULL_RTX
4608 && JUMP_P (undobuf.other_insn)
4609 && GET_CODE (PATTERN (undobuf.other_insn)) == SET
4610 && SET_SRC (PATTERN (undobuf.other_insn)) == pc_rtx
4611 && SET_DEST (PATTERN (undobuf.other_insn)) == pc_rtx)
4613 *new_direct_jump_p = 1;
4614 update_cfg_for_uncondjump (undobuf.other_insn);
4617 combine_successes++;
4618 undo_commit ();
4620 if (added_links_insn
4621 && (newi2pat == 0 || DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i2))
4622 && DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (i3))
4623 return added_links_insn;
4624 else
4625 return newi2pat ? i2 : i3;
4628 /* Undo all the modifications recorded in undobuf. */
4630 static void
4631 undo_all (void)
4633 struct undo *undo, *next;
4635 for (undo = undobuf.undos; undo; undo = next)
4637 next = undo->next;
4638 switch (undo->kind)
4640 case UNDO_RTX:
4641 *undo->where.r = undo->old_contents.r;
4642 break;
4643 case UNDO_INT:
4644 *undo->where.i = undo->old_contents.i;
4645 break;
4646 case UNDO_MODE:
4647 adjust_reg_mode (*undo->where.r, undo->old_contents.m);
4648 break;
4649 case UNDO_LINKS:
4650 *undo->where.l = undo->old_contents.l;
4651 break;
4652 default:
4653 gcc_unreachable ();
4656 undo->next = undobuf.frees;
4657 undobuf.frees = undo;
4660 undobuf.undos = 0;
4663 /* We've committed to accepting the changes we made. Move all
4664 of the undos to the free list. */
4666 static void
4667 undo_commit (void)
4669 struct undo *undo, *next;
4671 for (undo = undobuf.undos; undo; undo = next)
4673 next = undo->next;
4674 undo->next = undobuf.frees;
4675 undobuf.frees = undo;
4677 undobuf.undos = 0;
4680 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
4681 where we have an arithmetic expression and return that point. LOC will
4682 be inside INSN.
4684 try_combine will call this function to see if an insn can be split into
4685 two insns. */
4687 static rtx *
4688 find_split_point (rtx *loc, rtx_insn *insn, bool set_src)
4690 rtx x = *loc;
4691 enum rtx_code code = GET_CODE (x);
4692 rtx *split;
4693 unsigned HOST_WIDE_INT len = 0;
4694 HOST_WIDE_INT pos = 0;
4695 int unsignedp = 0;
4696 rtx inner = NULL_RTX;
4698 /* First special-case some codes. */
4699 switch (code)
4701 case SUBREG:
4702 #ifdef INSN_SCHEDULING
4703 /* If we are making a paradoxical SUBREG invalid, it becomes a split
4704 point. */
4705 if (MEM_P (SUBREG_REG (x)))
4706 return loc;
4707 #endif
4708 return find_split_point (&SUBREG_REG (x), insn, false);
4710 case MEM:
4711 #ifdef HAVE_lo_sum
4712 /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
4713 using LO_SUM and HIGH. */
4714 if (GET_CODE (XEXP (x, 0)) == CONST
4715 || GET_CODE (XEXP (x, 0)) == SYMBOL_REF)
4717 machine_mode address_mode = get_address_mode (x);
4719 SUBST (XEXP (x, 0),
4720 gen_rtx_LO_SUM (address_mode,
4721 gen_rtx_HIGH (address_mode, XEXP (x, 0)),
4722 XEXP (x, 0)));
4723 return &XEXP (XEXP (x, 0), 0);
4725 #endif
4727 /* If we have a PLUS whose second operand is a constant and the
4728 address is not valid, perhaps will can split it up using
4729 the machine-specific way to split large constants. We use
4730 the first pseudo-reg (one of the virtual regs) as a placeholder;
4731 it will not remain in the result. */
4732 if (GET_CODE (XEXP (x, 0)) == PLUS
4733 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
4734 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4735 MEM_ADDR_SPACE (x)))
4737 rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
4738 rtx_insn *seq = combine_split_insns (gen_rtx_SET (VOIDmode, reg,
4739 XEXP (x, 0)),
4740 subst_insn);
4742 /* This should have produced two insns, each of which sets our
4743 placeholder. If the source of the second is a valid address,
4744 we can make put both sources together and make a split point
4745 in the middle. */
4747 if (seq
4748 && NEXT_INSN (seq) != NULL_RTX
4749 && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
4750 && NONJUMP_INSN_P (seq)
4751 && GET_CODE (PATTERN (seq)) == SET
4752 && SET_DEST (PATTERN (seq)) == reg
4753 && ! reg_mentioned_p (reg,
4754 SET_SRC (PATTERN (seq)))
4755 && NONJUMP_INSN_P (NEXT_INSN (seq))
4756 && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
4757 && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
4758 && memory_address_addr_space_p
4759 (GET_MODE (x), SET_SRC (PATTERN (NEXT_INSN (seq))),
4760 MEM_ADDR_SPACE (x)))
4762 rtx src1 = SET_SRC (PATTERN (seq));
4763 rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
4765 /* Replace the placeholder in SRC2 with SRC1. If we can
4766 find where in SRC2 it was placed, that can become our
4767 split point and we can replace this address with SRC2.
4768 Just try two obvious places. */
4770 src2 = replace_rtx (src2, reg, src1);
4771 split = 0;
4772 if (XEXP (src2, 0) == src1)
4773 split = &XEXP (src2, 0);
4774 else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
4775 && XEXP (XEXP (src2, 0), 0) == src1)
4776 split = &XEXP (XEXP (src2, 0), 0);
4778 if (split)
4780 SUBST (XEXP (x, 0), src2);
4781 return split;
4785 /* If that didn't work, perhaps the first operand is complex and
4786 needs to be computed separately, so make a split point there.
4787 This will occur on machines that just support REG + CONST
4788 and have a constant moved through some previous computation. */
4790 else if (!OBJECT_P (XEXP (XEXP (x, 0), 0))
4791 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4792 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4793 return &XEXP (XEXP (x, 0), 0);
4796 /* If we have a PLUS whose first operand is complex, try computing it
4797 separately by making a split there. */
4798 if (GET_CODE (XEXP (x, 0)) == PLUS
4799 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4800 MEM_ADDR_SPACE (x))
4801 && ! OBJECT_P (XEXP (XEXP (x, 0), 0))
4802 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4803 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4804 return &XEXP (XEXP (x, 0), 0);
4805 break;
4807 case SET:
4808 #ifdef HAVE_cc0
4809 /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
4810 ZERO_EXTRACT, the most likely reason why this doesn't match is that
4811 we need to put the operand into a register. So split at that
4812 point. */
4814 if (SET_DEST (x) == cc0_rtx
4815 && GET_CODE (SET_SRC (x)) != COMPARE
4816 && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
4817 && !OBJECT_P (SET_SRC (x))
4818 && ! (GET_CODE (SET_SRC (x)) == SUBREG
4819 && OBJECT_P (SUBREG_REG (SET_SRC (x)))))
4820 return &SET_SRC (x);
4821 #endif
4823 /* See if we can split SET_SRC as it stands. */
4824 split = find_split_point (&SET_SRC (x), insn, true);
4825 if (split && split != &SET_SRC (x))
4826 return split;
4828 /* See if we can split SET_DEST as it stands. */
4829 split = find_split_point (&SET_DEST (x), insn, false);
4830 if (split && split != &SET_DEST (x))
4831 return split;
4833 /* See if this is a bitfield assignment with everything constant. If
4834 so, this is an IOR of an AND, so split it into that. */
4835 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
4836 && HWI_COMPUTABLE_MODE_P (GET_MODE (XEXP (SET_DEST (x), 0)))
4837 && CONST_INT_P (XEXP (SET_DEST (x), 1))
4838 && CONST_INT_P (XEXP (SET_DEST (x), 2))
4839 && CONST_INT_P (SET_SRC (x))
4840 && ((INTVAL (XEXP (SET_DEST (x), 1))
4841 + INTVAL (XEXP (SET_DEST (x), 2)))
4842 <= GET_MODE_PRECISION (GET_MODE (XEXP (SET_DEST (x), 0))))
4843 && ! side_effects_p (XEXP (SET_DEST (x), 0)))
4845 HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
4846 unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
4847 unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x));
4848 rtx dest = XEXP (SET_DEST (x), 0);
4849 machine_mode mode = GET_MODE (dest);
4850 unsigned HOST_WIDE_INT mask
4851 = ((unsigned HOST_WIDE_INT) 1 << len) - 1;
4852 rtx or_mask;
4854 if (BITS_BIG_ENDIAN)
4855 pos = GET_MODE_PRECISION (mode) - len - pos;
4857 or_mask = gen_int_mode (src << pos, mode);
4858 if (src == mask)
4859 SUBST (SET_SRC (x),
4860 simplify_gen_binary (IOR, mode, dest, or_mask));
4861 else
4863 rtx negmask = gen_int_mode (~(mask << pos), mode);
4864 SUBST (SET_SRC (x),
4865 simplify_gen_binary (IOR, mode,
4866 simplify_gen_binary (AND, mode,
4867 dest, negmask),
4868 or_mask));
4871 SUBST (SET_DEST (x), dest);
4873 split = find_split_point (&SET_SRC (x), insn, true);
4874 if (split && split != &SET_SRC (x))
4875 return split;
4878 /* Otherwise, see if this is an operation that we can split into two.
4879 If so, try to split that. */
4880 code = GET_CODE (SET_SRC (x));
4882 switch (code)
4884 case AND:
4885 /* If we are AND'ing with a large constant that is only a single
4886 bit and the result is only being used in a context where we
4887 need to know if it is zero or nonzero, replace it with a bit
4888 extraction. This will avoid the large constant, which might
4889 have taken more than one insn to make. If the constant were
4890 not a valid argument to the AND but took only one insn to make,
4891 this is no worse, but if it took more than one insn, it will
4892 be better. */
4894 if (CONST_INT_P (XEXP (SET_SRC (x), 1))
4895 && REG_P (XEXP (SET_SRC (x), 0))
4896 && (pos = exact_log2 (UINTVAL (XEXP (SET_SRC (x), 1)))) >= 7
4897 && REG_P (SET_DEST (x))
4898 && (split = find_single_use (SET_DEST (x), insn, NULL)) != 0
4899 && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
4900 && XEXP (*split, 0) == SET_DEST (x)
4901 && XEXP (*split, 1) == const0_rtx)
4903 rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
4904 XEXP (SET_SRC (x), 0),
4905 pos, NULL_RTX, 1, 1, 0, 0);
4906 if (extraction != 0)
4908 SUBST (SET_SRC (x), extraction);
4909 return find_split_point (loc, insn, false);
4912 break;
4914 case NE:
4915 /* If STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
4916 is known to be on, this can be converted into a NEG of a shift. */
4917 if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
4918 && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
4919 && 1 <= (pos = exact_log2
4920 (nonzero_bits (XEXP (SET_SRC (x), 0),
4921 GET_MODE (XEXP (SET_SRC (x), 0))))))
4923 machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
4925 SUBST (SET_SRC (x),
4926 gen_rtx_NEG (mode,
4927 gen_rtx_LSHIFTRT (mode,
4928 XEXP (SET_SRC (x), 0),
4929 GEN_INT (pos))));
4931 split = find_split_point (&SET_SRC (x), insn, true);
4932 if (split && split != &SET_SRC (x))
4933 return split;
4935 break;
4937 case SIGN_EXTEND:
4938 inner = XEXP (SET_SRC (x), 0);
4940 /* We can't optimize if either mode is a partial integer
4941 mode as we don't know how many bits are significant
4942 in those modes. */
4943 if (GET_MODE_CLASS (GET_MODE (inner)) == MODE_PARTIAL_INT
4944 || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
4945 break;
4947 pos = 0;
4948 len = GET_MODE_PRECISION (GET_MODE (inner));
4949 unsignedp = 0;
4950 break;
4952 case SIGN_EXTRACT:
4953 case ZERO_EXTRACT:
4954 if (CONST_INT_P (XEXP (SET_SRC (x), 1))
4955 && CONST_INT_P (XEXP (SET_SRC (x), 2)))
4957 inner = XEXP (SET_SRC (x), 0);
4958 len = INTVAL (XEXP (SET_SRC (x), 1));
4959 pos = INTVAL (XEXP (SET_SRC (x), 2));
4961 if (BITS_BIG_ENDIAN)
4962 pos = GET_MODE_PRECISION (GET_MODE (inner)) - len - pos;
4963 unsignedp = (code == ZERO_EXTRACT);
4965 break;
4967 default:
4968 break;
4971 if (len && pos >= 0
4972 && pos + len <= GET_MODE_PRECISION (GET_MODE (inner)))
4974 machine_mode mode = GET_MODE (SET_SRC (x));
4976 /* For unsigned, we have a choice of a shift followed by an
4977 AND or two shifts. Use two shifts for field sizes where the
4978 constant might be too large. We assume here that we can
4979 always at least get 8-bit constants in an AND insn, which is
4980 true for every current RISC. */
4982 if (unsignedp && len <= 8)
4984 unsigned HOST_WIDE_INT mask
4985 = ((unsigned HOST_WIDE_INT) 1 << len) - 1;
4986 SUBST (SET_SRC (x),
4987 gen_rtx_AND (mode,
4988 gen_rtx_LSHIFTRT
4989 (mode, gen_lowpart (mode, inner),
4990 GEN_INT (pos)),
4991 gen_int_mode (mask, mode)));
4993 split = find_split_point (&SET_SRC (x), insn, true);
4994 if (split && split != &SET_SRC (x))
4995 return split;
4997 else
4999 SUBST (SET_SRC (x),
5000 gen_rtx_fmt_ee
5001 (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
5002 gen_rtx_ASHIFT (mode,
5003 gen_lowpart (mode, inner),
5004 GEN_INT (GET_MODE_PRECISION (mode)
5005 - len - pos)),
5006 GEN_INT (GET_MODE_PRECISION (mode) - len)));
5008 split = find_split_point (&SET_SRC (x), insn, true);
5009 if (split && split != &SET_SRC (x))
5010 return split;
5014 /* See if this is a simple operation with a constant as the second
5015 operand. It might be that this constant is out of range and hence
5016 could be used as a split point. */
5017 if (BINARY_P (SET_SRC (x))
5018 && CONSTANT_P (XEXP (SET_SRC (x), 1))
5019 && (OBJECT_P (XEXP (SET_SRC (x), 0))
5020 || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
5021 && OBJECT_P (SUBREG_REG (XEXP (SET_SRC (x), 0))))))
5022 return &XEXP (SET_SRC (x), 1);
5024 /* Finally, see if this is a simple operation with its first operand
5025 not in a register. The operation might require this operand in a
5026 register, so return it as a split point. We can always do this
5027 because if the first operand were another operation, we would have
5028 already found it as a split point. */
5029 if ((BINARY_P (SET_SRC (x)) || UNARY_P (SET_SRC (x)))
5030 && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
5031 return &XEXP (SET_SRC (x), 0);
5033 return 0;
5035 case AND:
5036 case IOR:
5037 /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
5038 it is better to write this as (not (ior A B)) so we can split it.
5039 Similarly for IOR. */
5040 if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
5042 SUBST (*loc,
5043 gen_rtx_NOT (GET_MODE (x),
5044 gen_rtx_fmt_ee (code == IOR ? AND : IOR,
5045 GET_MODE (x),
5046 XEXP (XEXP (x, 0), 0),
5047 XEXP (XEXP (x, 1), 0))));
5048 return find_split_point (loc, insn, set_src);
5051 /* Many RISC machines have a large set of logical insns. If the
5052 second operand is a NOT, put it first so we will try to split the
5053 other operand first. */
5054 if (GET_CODE (XEXP (x, 1)) == NOT)
5056 rtx tem = XEXP (x, 0);
5057 SUBST (XEXP (x, 0), XEXP (x, 1));
5058 SUBST (XEXP (x, 1), tem);
5060 break;
5062 case PLUS:
5063 case MINUS:
5064 /* Canonicalization can produce (minus A (mult B C)), where C is a
5065 constant. It may be better to try splitting (plus (mult B -C) A)
5066 instead if this isn't a multiply by a power of two. */
5067 if (set_src && code == MINUS && GET_CODE (XEXP (x, 1)) == MULT
5068 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
5069 && exact_log2 (INTVAL (XEXP (XEXP (x, 1), 1))) < 0)
5071 machine_mode mode = GET_MODE (x);
5072 unsigned HOST_WIDE_INT this_int = INTVAL (XEXP (XEXP (x, 1), 1));
5073 HOST_WIDE_INT other_int = trunc_int_for_mode (-this_int, mode);
5074 SUBST (*loc, gen_rtx_PLUS (mode,
5075 gen_rtx_MULT (mode,
5076 XEXP (XEXP (x, 1), 0),
5077 gen_int_mode (other_int,
5078 mode)),
5079 XEXP (x, 0)));
5080 return find_split_point (loc, insn, set_src);
5083 /* Split at a multiply-accumulate instruction. However if this is
5084 the SET_SRC, we likely do not have such an instruction and it's
5085 worthless to try this split. */
5086 if (!set_src && GET_CODE (XEXP (x, 0)) == MULT)
5087 return loc;
5089 default:
5090 break;
5093 /* Otherwise, select our actions depending on our rtx class. */
5094 switch (GET_RTX_CLASS (code))
5096 case RTX_BITFIELD_OPS: /* This is ZERO_EXTRACT and SIGN_EXTRACT. */
5097 case RTX_TERNARY:
5098 split = find_split_point (&XEXP (x, 2), insn, false);
5099 if (split)
5100 return split;
5101 /* ... fall through ... */
5102 case RTX_BIN_ARITH:
5103 case RTX_COMM_ARITH:
5104 case RTX_COMPARE:
5105 case RTX_COMM_COMPARE:
5106 split = find_split_point (&XEXP (x, 1), insn, false);
5107 if (split)
5108 return split;
5109 /* ... fall through ... */
5110 case RTX_UNARY:
5111 /* Some machines have (and (shift ...) ...) insns. If X is not
5112 an AND, but XEXP (X, 0) is, use it as our split point. */
5113 if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
5114 return &XEXP (x, 0);
5116 split = find_split_point (&XEXP (x, 0), insn, false);
5117 if (split)
5118 return split;
5119 return loc;
5121 default:
5122 /* Otherwise, we don't have a split point. */
5123 return 0;
5127 /* Throughout X, replace FROM with TO, and return the result.
5128 The result is TO if X is FROM;
5129 otherwise the result is X, but its contents may have been modified.
5130 If they were modified, a record was made in undobuf so that
5131 undo_all will (among other things) return X to its original state.
5133 If the number of changes necessary is too much to record to undo,
5134 the excess changes are not made, so the result is invalid.
5135 The changes already made can still be undone.
5136 undobuf.num_undo is incremented for such changes, so by testing that
5137 the caller can tell whether the result is valid.
5139 `n_occurrences' is incremented each time FROM is replaced.
5141 IN_DEST is nonzero if we are processing the SET_DEST of a SET.
5143 IN_COND is nonzero if we are at the top level of a condition.
5145 UNIQUE_COPY is nonzero if each substitution must be unique. We do this
5146 by copying if `n_occurrences' is nonzero. */
5148 static rtx
5149 subst (rtx x, rtx from, rtx to, int in_dest, int in_cond, int unique_copy)
5151 enum rtx_code code = GET_CODE (x);
5152 machine_mode op0_mode = VOIDmode;
5153 const char *fmt;
5154 int len, i;
5155 rtx new_rtx;
5157 /* Two expressions are equal if they are identical copies of a shared
5158 RTX or if they are both registers with the same register number
5159 and mode. */
5161 #define COMBINE_RTX_EQUAL_P(X,Y) \
5162 ((X) == (Y) \
5163 || (REG_P (X) && REG_P (Y) \
5164 && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
5166 /* Do not substitute into clobbers of regs -- this will never result in
5167 valid RTL. */
5168 if (GET_CODE (x) == CLOBBER && REG_P (XEXP (x, 0)))
5169 return x;
5171 if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
5173 n_occurrences++;
5174 return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
5177 /* If X and FROM are the same register but different modes, they
5178 will not have been seen as equal above. However, the log links code
5179 will make a LOG_LINKS entry for that case. If we do nothing, we
5180 will try to rerecognize our original insn and, when it succeeds,
5181 we will delete the feeding insn, which is incorrect.
5183 So force this insn not to match in this (rare) case. */
5184 if (! in_dest && code == REG && REG_P (from)
5185 && reg_overlap_mentioned_p (x, from))
5186 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
5188 /* If this is an object, we are done unless it is a MEM or LO_SUM, both
5189 of which may contain things that can be combined. */
5190 if (code != MEM && code != LO_SUM && OBJECT_P (x))
5191 return x;
5193 /* It is possible to have a subexpression appear twice in the insn.
5194 Suppose that FROM is a register that appears within TO.
5195 Then, after that subexpression has been scanned once by `subst',
5196 the second time it is scanned, TO may be found. If we were
5197 to scan TO here, we would find FROM within it and create a
5198 self-referent rtl structure which is completely wrong. */
5199 if (COMBINE_RTX_EQUAL_P (x, to))
5200 return to;
5202 /* Parallel asm_operands need special attention because all of the
5203 inputs are shared across the arms. Furthermore, unsharing the
5204 rtl results in recognition failures. Failure to handle this case
5205 specially can result in circular rtl.
5207 Solve this by doing a normal pass across the first entry of the
5208 parallel, and only processing the SET_DESTs of the subsequent
5209 entries. Ug. */
5211 if (code == PARALLEL
5212 && GET_CODE (XVECEXP (x, 0, 0)) == SET
5213 && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
5215 new_rtx = subst (XVECEXP (x, 0, 0), from, to, 0, 0, unique_copy);
5217 /* If this substitution failed, this whole thing fails. */
5218 if (GET_CODE (new_rtx) == CLOBBER
5219 && XEXP (new_rtx, 0) == const0_rtx)
5220 return new_rtx;
5222 SUBST (XVECEXP (x, 0, 0), new_rtx);
5224 for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
5226 rtx dest = SET_DEST (XVECEXP (x, 0, i));
5228 if (!REG_P (dest)
5229 && GET_CODE (dest) != CC0
5230 && GET_CODE (dest) != PC)
5232 new_rtx = subst (dest, from, to, 0, 0, unique_copy);
5234 /* If this substitution failed, this whole thing fails. */
5235 if (GET_CODE (new_rtx) == CLOBBER
5236 && XEXP (new_rtx, 0) == const0_rtx)
5237 return new_rtx;
5239 SUBST (SET_DEST (XVECEXP (x, 0, i)), new_rtx);
5243 else
5245 len = GET_RTX_LENGTH (code);
5246 fmt = GET_RTX_FORMAT (code);
5248 /* We don't need to process a SET_DEST that is a register, CC0,
5249 or PC, so set up to skip this common case. All other cases
5250 where we want to suppress replacing something inside a
5251 SET_SRC are handled via the IN_DEST operand. */
5252 if (code == SET
5253 && (REG_P (SET_DEST (x))
5254 || GET_CODE (SET_DEST (x)) == CC0
5255 || GET_CODE (SET_DEST (x)) == PC))
5256 fmt = "ie";
5258 /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
5259 constant. */
5260 if (fmt[0] == 'e')
5261 op0_mode = GET_MODE (XEXP (x, 0));
5263 for (i = 0; i < len; i++)
5265 if (fmt[i] == 'E')
5267 int j;
5268 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5270 if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
5272 new_rtx = (unique_copy && n_occurrences
5273 ? copy_rtx (to) : to);
5274 n_occurrences++;
5276 else
5278 new_rtx = subst (XVECEXP (x, i, j), from, to, 0, 0,
5279 unique_copy);
5281 /* If this substitution failed, this whole thing
5282 fails. */
5283 if (GET_CODE (new_rtx) == CLOBBER
5284 && XEXP (new_rtx, 0) == const0_rtx)
5285 return new_rtx;
5288 SUBST (XVECEXP (x, i, j), new_rtx);
5291 else if (fmt[i] == 'e')
5293 /* If this is a register being set, ignore it. */
5294 new_rtx = XEXP (x, i);
5295 if (in_dest
5296 && i == 0
5297 && (((code == SUBREG || code == ZERO_EXTRACT)
5298 && REG_P (new_rtx))
5299 || code == STRICT_LOW_PART))
5302 else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
5304 /* In general, don't install a subreg involving two
5305 modes not tieable. It can worsen register
5306 allocation, and can even make invalid reload
5307 insns, since the reg inside may need to be copied
5308 from in the outside mode, and that may be invalid
5309 if it is an fp reg copied in integer mode.
5311 We allow two exceptions to this: It is valid if
5312 it is inside another SUBREG and the mode of that
5313 SUBREG and the mode of the inside of TO is
5314 tieable and it is valid if X is a SET that copies
5315 FROM to CC0. */
5317 if (GET_CODE (to) == SUBREG
5318 && ! MODES_TIEABLE_P (GET_MODE (to),
5319 GET_MODE (SUBREG_REG (to)))
5320 && ! (code == SUBREG
5321 && MODES_TIEABLE_P (GET_MODE (x),
5322 GET_MODE (SUBREG_REG (to))))
5323 #ifdef HAVE_cc0
5324 && ! (code == SET && i == 1 && XEXP (x, 0) == cc0_rtx)
5325 #endif
5327 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5329 if (code == SUBREG
5330 && REG_P (to)
5331 && REGNO (to) < FIRST_PSEUDO_REGISTER
5332 && simplify_subreg_regno (REGNO (to), GET_MODE (to),
5333 SUBREG_BYTE (x),
5334 GET_MODE (x)) < 0)
5335 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5337 new_rtx = (unique_copy && n_occurrences ? copy_rtx (to) : to);
5338 n_occurrences++;
5340 else
5341 /* If we are in a SET_DEST, suppress most cases unless we
5342 have gone inside a MEM, in which case we want to
5343 simplify the address. We assume here that things that
5344 are actually part of the destination have their inner
5345 parts in the first expression. This is true for SUBREG,
5346 STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
5347 things aside from REG and MEM that should appear in a
5348 SET_DEST. */
5349 new_rtx = subst (XEXP (x, i), from, to,
5350 (((in_dest
5351 && (code == SUBREG || code == STRICT_LOW_PART
5352 || code == ZERO_EXTRACT))
5353 || code == SET)
5354 && i == 0),
5355 code == IF_THEN_ELSE && i == 0,
5356 unique_copy);
5358 /* If we found that we will have to reject this combination,
5359 indicate that by returning the CLOBBER ourselves, rather than
5360 an expression containing it. This will speed things up as
5361 well as prevent accidents where two CLOBBERs are considered
5362 to be equal, thus producing an incorrect simplification. */
5364 if (GET_CODE (new_rtx) == CLOBBER && XEXP (new_rtx, 0) == const0_rtx)
5365 return new_rtx;
5367 if (GET_CODE (x) == SUBREG && CONST_SCALAR_INT_P (new_rtx))
5369 machine_mode mode = GET_MODE (x);
5371 x = simplify_subreg (GET_MODE (x), new_rtx,
5372 GET_MODE (SUBREG_REG (x)),
5373 SUBREG_BYTE (x));
5374 if (! x)
5375 x = gen_rtx_CLOBBER (mode, const0_rtx);
5377 else if (CONST_SCALAR_INT_P (new_rtx)
5378 && GET_CODE (x) == ZERO_EXTEND)
5380 x = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
5381 new_rtx, GET_MODE (XEXP (x, 0)));
5382 gcc_assert (x);
5384 else
5385 SUBST (XEXP (x, i), new_rtx);
5390 /* Check if we are loading something from the constant pool via float
5391 extension; in this case we would undo compress_float_constant
5392 optimization and degenerate constant load to an immediate value. */
5393 if (GET_CODE (x) == FLOAT_EXTEND
5394 && MEM_P (XEXP (x, 0))
5395 && MEM_READONLY_P (XEXP (x, 0)))
5397 rtx tmp = avoid_constant_pool_reference (x);
5398 if (x != tmp)
5399 return x;
5402 /* Try to simplify X. If the simplification changed the code, it is likely
5403 that further simplification will help, so loop, but limit the number
5404 of repetitions that will be performed. */
5406 for (i = 0; i < 4; i++)
5408 /* If X is sufficiently simple, don't bother trying to do anything
5409 with it. */
5410 if (code != CONST_INT && code != REG && code != CLOBBER)
5411 x = combine_simplify_rtx (x, op0_mode, in_dest, in_cond);
5413 if (GET_CODE (x) == code)
5414 break;
5416 code = GET_CODE (x);
5418 /* We no longer know the original mode of operand 0 since we
5419 have changed the form of X) */
5420 op0_mode = VOIDmode;
5423 return x;
5426 /* Simplify X, a piece of RTL. We just operate on the expression at the
5427 outer level; call `subst' to simplify recursively. Return the new
5428 expression.
5430 OP0_MODE is the original mode of XEXP (x, 0). IN_DEST is nonzero
5431 if we are inside a SET_DEST. IN_COND is nonzero if we are at the top level
5432 of a condition. */
5434 static rtx
5435 combine_simplify_rtx (rtx x, machine_mode op0_mode, int in_dest,
5436 int in_cond)
5438 enum rtx_code code = GET_CODE (x);
5439 machine_mode mode = GET_MODE (x);
5440 rtx temp;
5441 int i;
5443 /* If this is a commutative operation, put a constant last and a complex
5444 expression first. We don't need to do this for comparisons here. */
5445 if (COMMUTATIVE_ARITH_P (x)
5446 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
5448 temp = XEXP (x, 0);
5449 SUBST (XEXP (x, 0), XEXP (x, 1));
5450 SUBST (XEXP (x, 1), temp);
5453 /* If this is a simple operation applied to an IF_THEN_ELSE, try
5454 applying it to the arms of the IF_THEN_ELSE. This often simplifies
5455 things. Check for cases where both arms are testing the same
5456 condition.
5458 Don't do anything if all operands are very simple. */
5460 if ((BINARY_P (x)
5461 && ((!OBJECT_P (XEXP (x, 0))
5462 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5463 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))
5464 || (!OBJECT_P (XEXP (x, 1))
5465 && ! (GET_CODE (XEXP (x, 1)) == SUBREG
5466 && OBJECT_P (SUBREG_REG (XEXP (x, 1)))))))
5467 || (UNARY_P (x)
5468 && (!OBJECT_P (XEXP (x, 0))
5469 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5470 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))))
5472 rtx cond, true_rtx, false_rtx;
5474 cond = if_then_else_cond (x, &true_rtx, &false_rtx);
5475 if (cond != 0
5476 /* If everything is a comparison, what we have is highly unlikely
5477 to be simpler, so don't use it. */
5478 && ! (COMPARISON_P (x)
5479 && (COMPARISON_P (true_rtx) || COMPARISON_P (false_rtx))))
5481 rtx cop1 = const0_rtx;
5482 enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
5484 if (cond_code == NE && COMPARISON_P (cond))
5485 return x;
5487 /* Simplify the alternative arms; this may collapse the true and
5488 false arms to store-flag values. Be careful to use copy_rtx
5489 here since true_rtx or false_rtx might share RTL with x as a
5490 result of the if_then_else_cond call above. */
5491 true_rtx = subst (copy_rtx (true_rtx), pc_rtx, pc_rtx, 0, 0, 0);
5492 false_rtx = subst (copy_rtx (false_rtx), pc_rtx, pc_rtx, 0, 0, 0);
5494 /* If true_rtx and false_rtx are not general_operands, an if_then_else
5495 is unlikely to be simpler. */
5496 if (general_operand (true_rtx, VOIDmode)
5497 && general_operand (false_rtx, VOIDmode))
5499 enum rtx_code reversed;
5501 /* Restarting if we generate a store-flag expression will cause
5502 us to loop. Just drop through in this case. */
5504 /* If the result values are STORE_FLAG_VALUE and zero, we can
5505 just make the comparison operation. */
5506 if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
5507 x = simplify_gen_relational (cond_code, mode, VOIDmode,
5508 cond, cop1);
5509 else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
5510 && ((reversed = reversed_comparison_code_parts
5511 (cond_code, cond, cop1, NULL))
5512 != UNKNOWN))
5513 x = simplify_gen_relational (reversed, mode, VOIDmode,
5514 cond, cop1);
5516 /* Likewise, we can make the negate of a comparison operation
5517 if the result values are - STORE_FLAG_VALUE and zero. */
5518 else if (CONST_INT_P (true_rtx)
5519 && INTVAL (true_rtx) == - STORE_FLAG_VALUE
5520 && false_rtx == const0_rtx)
5521 x = simplify_gen_unary (NEG, mode,
5522 simplify_gen_relational (cond_code,
5523 mode, VOIDmode,
5524 cond, cop1),
5525 mode);
5526 else if (CONST_INT_P (false_rtx)
5527 && INTVAL (false_rtx) == - STORE_FLAG_VALUE
5528 && true_rtx == const0_rtx
5529 && ((reversed = reversed_comparison_code_parts
5530 (cond_code, cond, cop1, NULL))
5531 != UNKNOWN))
5532 x = simplify_gen_unary (NEG, mode,
5533 simplify_gen_relational (reversed,
5534 mode, VOIDmode,
5535 cond, cop1),
5536 mode);
5537 else
5538 return gen_rtx_IF_THEN_ELSE (mode,
5539 simplify_gen_relational (cond_code,
5540 mode,
5541 VOIDmode,
5542 cond,
5543 cop1),
5544 true_rtx, false_rtx);
5546 code = GET_CODE (x);
5547 op0_mode = VOIDmode;
5552 /* Try to fold this expression in case we have constants that weren't
5553 present before. */
5554 temp = 0;
5555 switch (GET_RTX_CLASS (code))
5557 case RTX_UNARY:
5558 if (op0_mode == VOIDmode)
5559 op0_mode = GET_MODE (XEXP (x, 0));
5560 temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
5561 break;
5562 case RTX_COMPARE:
5563 case RTX_COMM_COMPARE:
5565 machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
5566 if (cmp_mode == VOIDmode)
5568 cmp_mode = GET_MODE (XEXP (x, 1));
5569 if (cmp_mode == VOIDmode)
5570 cmp_mode = op0_mode;
5572 temp = simplify_relational_operation (code, mode, cmp_mode,
5573 XEXP (x, 0), XEXP (x, 1));
5575 break;
5576 case RTX_COMM_ARITH:
5577 case RTX_BIN_ARITH:
5578 temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
5579 break;
5580 case RTX_BITFIELD_OPS:
5581 case RTX_TERNARY:
5582 temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
5583 XEXP (x, 1), XEXP (x, 2));
5584 break;
5585 default:
5586 break;
5589 if (temp)
5591 x = temp;
5592 code = GET_CODE (temp);
5593 op0_mode = VOIDmode;
5594 mode = GET_MODE (temp);
5597 /* First see if we can apply the inverse distributive law. */
5598 if (code == PLUS || code == MINUS
5599 || code == AND || code == IOR || code == XOR)
5601 x = apply_distributive_law (x);
5602 code = GET_CODE (x);
5603 op0_mode = VOIDmode;
5606 /* If CODE is an associative operation not otherwise handled, see if we
5607 can associate some operands. This can win if they are constants or
5608 if they are logically related (i.e. (a & b) & a). */
5609 if ((code == PLUS || code == MINUS || code == MULT || code == DIV
5610 || code == AND || code == IOR || code == XOR
5611 || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
5612 && ((INTEGRAL_MODE_P (mode) && code != DIV)
5613 || (flag_associative_math && FLOAT_MODE_P (mode))))
5615 if (GET_CODE (XEXP (x, 0)) == code)
5617 rtx other = XEXP (XEXP (x, 0), 0);
5618 rtx inner_op0 = XEXP (XEXP (x, 0), 1);
5619 rtx inner_op1 = XEXP (x, 1);
5620 rtx inner;
5622 /* Make sure we pass the constant operand if any as the second
5623 one if this is a commutative operation. */
5624 if (CONSTANT_P (inner_op0) && COMMUTATIVE_ARITH_P (x))
5626 rtx tem = inner_op0;
5627 inner_op0 = inner_op1;
5628 inner_op1 = tem;
5630 inner = simplify_binary_operation (code == MINUS ? PLUS
5631 : code == DIV ? MULT
5632 : code,
5633 mode, inner_op0, inner_op1);
5635 /* For commutative operations, try the other pair if that one
5636 didn't simplify. */
5637 if (inner == 0 && COMMUTATIVE_ARITH_P (x))
5639 other = XEXP (XEXP (x, 0), 1);
5640 inner = simplify_binary_operation (code, mode,
5641 XEXP (XEXP (x, 0), 0),
5642 XEXP (x, 1));
5645 if (inner)
5646 return simplify_gen_binary (code, mode, other, inner);
5650 /* A little bit of algebraic simplification here. */
5651 switch (code)
5653 case MEM:
5654 /* Ensure that our address has any ASHIFTs converted to MULT in case
5655 address-recognizing predicates are called later. */
5656 temp = make_compound_operation (XEXP (x, 0), MEM);
5657 SUBST (XEXP (x, 0), temp);
5658 break;
5660 case SUBREG:
5661 if (op0_mode == VOIDmode)
5662 op0_mode = GET_MODE (SUBREG_REG (x));
5664 /* See if this can be moved to simplify_subreg. */
5665 if (CONSTANT_P (SUBREG_REG (x))
5666 && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
5667 /* Don't call gen_lowpart if the inner mode
5668 is VOIDmode and we cannot simplify it, as SUBREG without
5669 inner mode is invalid. */
5670 && (GET_MODE (SUBREG_REG (x)) != VOIDmode
5671 || gen_lowpart_common (mode, SUBREG_REG (x))))
5672 return gen_lowpart (mode, SUBREG_REG (x));
5674 if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
5675 break;
5677 rtx temp;
5678 temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
5679 SUBREG_BYTE (x));
5680 if (temp)
5681 return temp;
5683 /* If op is known to have all lower bits zero, the result is zero. */
5684 if (!in_dest
5685 && SCALAR_INT_MODE_P (mode)
5686 && SCALAR_INT_MODE_P (op0_mode)
5687 && GET_MODE_PRECISION (mode) < GET_MODE_PRECISION (op0_mode)
5688 && subreg_lowpart_offset (mode, op0_mode) == SUBREG_BYTE (x)
5689 && HWI_COMPUTABLE_MODE_P (op0_mode)
5690 && (nonzero_bits (SUBREG_REG (x), op0_mode)
5691 & GET_MODE_MASK (mode)) == 0)
5692 return CONST0_RTX (mode);
5695 /* Don't change the mode of the MEM if that would change the meaning
5696 of the address. */
5697 if (MEM_P (SUBREG_REG (x))
5698 && (MEM_VOLATILE_P (SUBREG_REG (x))
5699 || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0),
5700 MEM_ADDR_SPACE (SUBREG_REG (x)))))
5701 return gen_rtx_CLOBBER (mode, const0_rtx);
5703 /* Note that we cannot do any narrowing for non-constants since
5704 we might have been counting on using the fact that some bits were
5705 zero. We now do this in the SET. */
5707 break;
5709 case NEG:
5710 temp = expand_compound_operation (XEXP (x, 0));
5712 /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
5713 replaced by (lshiftrt X C). This will convert
5714 (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y). */
5716 if (GET_CODE (temp) == ASHIFTRT
5717 && CONST_INT_P (XEXP (temp, 1))
5718 && INTVAL (XEXP (temp, 1)) == GET_MODE_PRECISION (mode) - 1)
5719 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (temp, 0),
5720 INTVAL (XEXP (temp, 1)));
5722 /* If X has only a single bit that might be nonzero, say, bit I, convert
5723 (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
5724 MODE minus 1. This will convert (neg (zero_extract X 1 Y)) to
5725 (sign_extract X 1 Y). But only do this if TEMP isn't a register
5726 or a SUBREG of one since we'd be making the expression more
5727 complex if it was just a register. */
5729 if (!REG_P (temp)
5730 && ! (GET_CODE (temp) == SUBREG
5731 && REG_P (SUBREG_REG (temp)))
5732 && (i = exact_log2 (nonzero_bits (temp, mode))) >= 0)
5734 rtx temp1 = simplify_shift_const
5735 (NULL_RTX, ASHIFTRT, mode,
5736 simplify_shift_const (NULL_RTX, ASHIFT, mode, temp,
5737 GET_MODE_PRECISION (mode) - 1 - i),
5738 GET_MODE_PRECISION (mode) - 1 - i);
5740 /* If all we did was surround TEMP with the two shifts, we
5741 haven't improved anything, so don't use it. Otherwise,
5742 we are better off with TEMP1. */
5743 if (GET_CODE (temp1) != ASHIFTRT
5744 || GET_CODE (XEXP (temp1, 0)) != ASHIFT
5745 || XEXP (XEXP (temp1, 0), 0) != temp)
5746 return temp1;
5748 break;
5750 case TRUNCATE:
5751 /* We can't handle truncation to a partial integer mode here
5752 because we don't know the real bitsize of the partial
5753 integer mode. */
5754 if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
5755 break;
5757 if (HWI_COMPUTABLE_MODE_P (mode))
5758 SUBST (XEXP (x, 0),
5759 force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
5760 GET_MODE_MASK (mode), 0));
5762 /* We can truncate a constant value and return it. */
5763 if (CONST_INT_P (XEXP (x, 0)))
5764 return gen_int_mode (INTVAL (XEXP (x, 0)), mode);
5766 /* Similarly to what we do in simplify-rtx.c, a truncate of a register
5767 whose value is a comparison can be replaced with a subreg if
5768 STORE_FLAG_VALUE permits. */
5769 if (HWI_COMPUTABLE_MODE_P (mode)
5770 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
5771 && (temp = get_last_value (XEXP (x, 0)))
5772 && COMPARISON_P (temp))
5773 return gen_lowpart (mode, XEXP (x, 0));
5774 break;
5776 case CONST:
5777 /* (const (const X)) can become (const X). Do it this way rather than
5778 returning the inner CONST since CONST can be shared with a
5779 REG_EQUAL note. */
5780 if (GET_CODE (XEXP (x, 0)) == CONST)
5781 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
5782 break;
5784 #ifdef HAVE_lo_sum
5785 case LO_SUM:
5786 /* Convert (lo_sum (high FOO) FOO) to FOO. This is necessary so we
5787 can add in an offset. find_split_point will split this address up
5788 again if it doesn't match. */
5789 if (GET_CODE (XEXP (x, 0)) == HIGH
5790 && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
5791 return XEXP (x, 1);
5792 break;
5793 #endif
5795 case PLUS:
5796 /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
5797 when c is (const_int (pow2 + 1) / 2) is a sign extension of a
5798 bit-field and can be replaced by either a sign_extend or a
5799 sign_extract. The `and' may be a zero_extend and the two
5800 <c>, -<c> constants may be reversed. */
5801 if (GET_CODE (XEXP (x, 0)) == XOR
5802 && CONST_INT_P (XEXP (x, 1))
5803 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
5804 && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
5805 && ((i = exact_log2 (UINTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
5806 || (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
5807 && HWI_COMPUTABLE_MODE_P (mode)
5808 && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
5809 && CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
5810 && (UINTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
5811 == ((unsigned HOST_WIDE_INT) 1 << (i + 1)) - 1))
5812 || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
5813 && (GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))
5814 == (unsigned int) i + 1))))
5815 return simplify_shift_const
5816 (NULL_RTX, ASHIFTRT, mode,
5817 simplify_shift_const (NULL_RTX, ASHIFT, mode,
5818 XEXP (XEXP (XEXP (x, 0), 0), 0),
5819 GET_MODE_PRECISION (mode) - (i + 1)),
5820 GET_MODE_PRECISION (mode) - (i + 1));
5822 /* If only the low-order bit of X is possibly nonzero, (plus x -1)
5823 can become (ashiftrt (ashift (xor x 1) C) C) where C is
5824 the bitsize of the mode - 1. This allows simplification of
5825 "a = (b & 8) == 0;" */
5826 if (XEXP (x, 1) == constm1_rtx
5827 && !REG_P (XEXP (x, 0))
5828 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5829 && REG_P (SUBREG_REG (XEXP (x, 0))))
5830 && nonzero_bits (XEXP (x, 0), mode) == 1)
5831 return simplify_shift_const (NULL_RTX, ASHIFTRT, mode,
5832 simplify_shift_const (NULL_RTX, ASHIFT, mode,
5833 gen_rtx_XOR (mode, XEXP (x, 0), const1_rtx),
5834 GET_MODE_PRECISION (mode) - 1),
5835 GET_MODE_PRECISION (mode) - 1);
5837 /* If we are adding two things that have no bits in common, convert
5838 the addition into an IOR. This will often be further simplified,
5839 for example in cases like ((a & 1) + (a & 2)), which can
5840 become a & 3. */
5842 if (HWI_COMPUTABLE_MODE_P (mode)
5843 && (nonzero_bits (XEXP (x, 0), mode)
5844 & nonzero_bits (XEXP (x, 1), mode)) == 0)
5846 /* Try to simplify the expression further. */
5847 rtx tor = simplify_gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
5848 temp = combine_simplify_rtx (tor, VOIDmode, in_dest, 0);
5850 /* If we could, great. If not, do not go ahead with the IOR
5851 replacement, since PLUS appears in many special purpose
5852 address arithmetic instructions. */
5853 if (GET_CODE (temp) != CLOBBER
5854 && (GET_CODE (temp) != IOR
5855 || ((XEXP (temp, 0) != XEXP (x, 0)
5856 || XEXP (temp, 1) != XEXP (x, 1))
5857 && (XEXP (temp, 0) != XEXP (x, 1)
5858 || XEXP (temp, 1) != XEXP (x, 0)))))
5859 return temp;
5861 break;
5863 case MINUS:
5864 /* (minus <foo> (and <foo> (const_int -pow2))) becomes
5865 (and <foo> (const_int pow2-1)) */
5866 if (GET_CODE (XEXP (x, 1)) == AND
5867 && CONST_INT_P (XEXP (XEXP (x, 1), 1))
5868 && exact_log2 (-UINTVAL (XEXP (XEXP (x, 1), 1))) >= 0
5869 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
5870 return simplify_and_const_int (NULL_RTX, mode, XEXP (x, 0),
5871 -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
5872 break;
5874 case MULT:
5875 /* If we have (mult (plus A B) C), apply the distributive law and then
5876 the inverse distributive law to see if things simplify. This
5877 occurs mostly in addresses, often when unrolling loops. */
5879 if (GET_CODE (XEXP (x, 0)) == PLUS)
5881 rtx result = distribute_and_simplify_rtx (x, 0);
5882 if (result)
5883 return result;
5886 /* Try simplify a*(b/c) as (a*b)/c. */
5887 if (FLOAT_MODE_P (mode) && flag_associative_math
5888 && GET_CODE (XEXP (x, 0)) == DIV)
5890 rtx tem = simplify_binary_operation (MULT, mode,
5891 XEXP (XEXP (x, 0), 0),
5892 XEXP (x, 1));
5893 if (tem)
5894 return simplify_gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
5896 break;
5898 case UDIV:
5899 /* If this is a divide by a power of two, treat it as a shift if
5900 its first operand is a shift. */
5901 if (CONST_INT_P (XEXP (x, 1))
5902 && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0
5903 && (GET_CODE (XEXP (x, 0)) == ASHIFT
5904 || GET_CODE (XEXP (x, 0)) == LSHIFTRT
5905 || GET_CODE (XEXP (x, 0)) == ASHIFTRT
5906 || GET_CODE (XEXP (x, 0)) == ROTATE
5907 || GET_CODE (XEXP (x, 0)) == ROTATERT))
5908 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (x, 0), i);
5909 break;
5911 case EQ: case NE:
5912 case GT: case GTU: case GE: case GEU:
5913 case LT: case LTU: case LE: case LEU:
5914 case UNEQ: case LTGT:
5915 case UNGT: case UNGE:
5916 case UNLT: case UNLE:
5917 case UNORDERED: case ORDERED:
5918 /* If the first operand is a condition code, we can't do anything
5919 with it. */
5920 if (GET_CODE (XEXP (x, 0)) == COMPARE
5921 || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
5922 && ! CC0_P (XEXP (x, 0))))
5924 rtx op0 = XEXP (x, 0);
5925 rtx op1 = XEXP (x, 1);
5926 enum rtx_code new_code;
5928 if (GET_CODE (op0) == COMPARE)
5929 op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
5931 /* Simplify our comparison, if possible. */
5932 new_code = simplify_comparison (code, &op0, &op1);
5934 /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
5935 if only the low-order bit is possibly nonzero in X (such as when
5936 X is a ZERO_EXTRACT of one bit). Similarly, we can convert EQ to
5937 (xor X 1) or (minus 1 X); we use the former. Finally, if X is
5938 known to be either 0 or -1, NE becomes a NEG and EQ becomes
5939 (plus X 1).
5941 Remove any ZERO_EXTRACT we made when thinking this was a
5942 comparison. It may now be simpler to use, e.g., an AND. If a
5943 ZERO_EXTRACT is indeed appropriate, it will be placed back by
5944 the call to make_compound_operation in the SET case.
5946 Don't apply these optimizations if the caller would
5947 prefer a comparison rather than a value.
5948 E.g., for the condition in an IF_THEN_ELSE most targets need
5949 an explicit comparison. */
5951 if (in_cond)
5954 else if (STORE_FLAG_VALUE == 1
5955 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5956 && op1 == const0_rtx
5957 && mode == GET_MODE (op0)
5958 && nonzero_bits (op0, mode) == 1)
5959 return gen_lowpart (mode,
5960 expand_compound_operation (op0));
5962 else if (STORE_FLAG_VALUE == 1
5963 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
5964 && op1 == const0_rtx
5965 && mode == GET_MODE (op0)
5966 && (num_sign_bit_copies (op0, mode)
5967 == GET_MODE_PRECISION (mode)))
5969 op0 = expand_compound_operation (op0);
5970 return simplify_gen_unary (NEG, mode,
5971 gen_lowpart (mode, op0),
5972 mode);
5975 else if (STORE_FLAG_VALUE == 1
5976 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5977 && op1 == const0_rtx
5978 && mode == GET_MODE (op0)
5979 && nonzero_bits (op0, mode) == 1)
5981 op0 = expand_compound_operation (op0);
5982 return simplify_gen_binary (XOR, mode,
5983 gen_lowpart (mode, op0),
5984 const1_rtx);
5987 else if (STORE_FLAG_VALUE == 1
5988 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
5989 && op1 == const0_rtx
5990 && mode == GET_MODE (op0)
5991 && (num_sign_bit_copies (op0, mode)
5992 == GET_MODE_PRECISION (mode)))
5994 op0 = expand_compound_operation (op0);
5995 return plus_constant (mode, gen_lowpart (mode, op0), 1);
5998 /* If STORE_FLAG_VALUE is -1, we have cases similar to
5999 those above. */
6000 if (in_cond)
6003 else if (STORE_FLAG_VALUE == -1
6004 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
6005 && op1 == const0_rtx
6006 && mode == GET_MODE (op0)
6007 && (num_sign_bit_copies (op0, mode)
6008 == GET_MODE_PRECISION (mode)))
6009 return gen_lowpart (mode,
6010 expand_compound_operation (op0));
6012 else if (STORE_FLAG_VALUE == -1
6013 && new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
6014 && op1 == const0_rtx
6015 && mode == GET_MODE (op0)
6016 && nonzero_bits (op0, mode) == 1)
6018 op0 = expand_compound_operation (op0);
6019 return simplify_gen_unary (NEG, mode,
6020 gen_lowpart (mode, op0),
6021 mode);
6024 else if (STORE_FLAG_VALUE == -1
6025 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
6026 && op1 == const0_rtx
6027 && mode == GET_MODE (op0)
6028 && (num_sign_bit_copies (op0, mode)
6029 == GET_MODE_PRECISION (mode)))
6031 op0 = expand_compound_operation (op0);
6032 return simplify_gen_unary (NOT, mode,
6033 gen_lowpart (mode, op0),
6034 mode);
6037 /* If X is 0/1, (eq X 0) is X-1. */
6038 else if (STORE_FLAG_VALUE == -1
6039 && new_code == EQ && GET_MODE_CLASS (mode) == MODE_INT
6040 && op1 == const0_rtx
6041 && mode == GET_MODE (op0)
6042 && nonzero_bits (op0, mode) == 1)
6044 op0 = expand_compound_operation (op0);
6045 return plus_constant (mode, gen_lowpart (mode, op0), -1);
6048 /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
6049 one bit that might be nonzero, we can convert (ne x 0) to
6050 (ashift x c) where C puts the bit in the sign bit. Remove any
6051 AND with STORE_FLAG_VALUE when we are done, since we are only
6052 going to test the sign bit. */
6053 if (new_code == NE && GET_MODE_CLASS (mode) == MODE_INT
6054 && HWI_COMPUTABLE_MODE_P (mode)
6055 && val_signbit_p (mode, STORE_FLAG_VALUE)
6056 && op1 == const0_rtx
6057 && mode == GET_MODE (op0)
6058 && (i = exact_log2 (nonzero_bits (op0, mode))) >= 0)
6060 x = simplify_shift_const (NULL_RTX, ASHIFT, mode,
6061 expand_compound_operation (op0),
6062 GET_MODE_PRECISION (mode) - 1 - i);
6063 if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
6064 return XEXP (x, 0);
6065 else
6066 return x;
6069 /* If the code changed, return a whole new comparison.
6070 We also need to avoid using SUBST in cases where
6071 simplify_comparison has widened a comparison with a CONST_INT,
6072 since in that case the wider CONST_INT may fail the sanity
6073 checks in do_SUBST. */
6074 if (new_code != code
6075 || (CONST_INT_P (op1)
6076 && GET_MODE (op0) != GET_MODE (XEXP (x, 0))
6077 && GET_MODE (op0) != GET_MODE (XEXP (x, 1))))
6078 return gen_rtx_fmt_ee (new_code, mode, op0, op1);
6080 /* Otherwise, keep this operation, but maybe change its operands.
6081 This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR). */
6082 SUBST (XEXP (x, 0), op0);
6083 SUBST (XEXP (x, 1), op1);
6085 break;
6087 case IF_THEN_ELSE:
6088 return simplify_if_then_else (x);
6090 case ZERO_EXTRACT:
6091 case SIGN_EXTRACT:
6092 case ZERO_EXTEND:
6093 case SIGN_EXTEND:
6094 /* If we are processing SET_DEST, we are done. */
6095 if (in_dest)
6096 return x;
6098 return expand_compound_operation (x);
6100 case SET:
6101 return simplify_set (x);
6103 case AND:
6104 case IOR:
6105 return simplify_logical (x);
6107 case ASHIFT:
6108 case LSHIFTRT:
6109 case ASHIFTRT:
6110 case ROTATE:
6111 case ROTATERT:
6112 /* If this is a shift by a constant amount, simplify it. */
6113 if (CONST_INT_P (XEXP (x, 1)))
6114 return simplify_shift_const (x, code, mode, XEXP (x, 0),
6115 INTVAL (XEXP (x, 1)));
6117 else if (SHIFT_COUNT_TRUNCATED && !REG_P (XEXP (x, 1)))
6118 SUBST (XEXP (x, 1),
6119 force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
6120 ((unsigned HOST_WIDE_INT) 1
6121 << exact_log2 (GET_MODE_BITSIZE (GET_MODE (x))))
6122 - 1,
6123 0));
6124 break;
6126 default:
6127 break;
6130 return x;
6133 /* Simplify X, an IF_THEN_ELSE expression. Return the new expression. */
6135 static rtx
6136 simplify_if_then_else (rtx x)
6138 machine_mode mode = GET_MODE (x);
6139 rtx cond = XEXP (x, 0);
6140 rtx true_rtx = XEXP (x, 1);
6141 rtx false_rtx = XEXP (x, 2);
6142 enum rtx_code true_code = GET_CODE (cond);
6143 int comparison_p = COMPARISON_P (cond);
6144 rtx temp;
6145 int i;
6146 enum rtx_code false_code;
6147 rtx reversed;
6149 /* Simplify storing of the truth value. */
6150 if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
6151 return simplify_gen_relational (true_code, mode, VOIDmode,
6152 XEXP (cond, 0), XEXP (cond, 1));
6154 /* Also when the truth value has to be reversed. */
6155 if (comparison_p
6156 && true_rtx == const0_rtx && false_rtx == const_true_rtx
6157 && (reversed = reversed_comparison (cond, mode)))
6158 return reversed;
6160 /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
6161 in it is being compared against certain values. Get the true and false
6162 comparisons and see if that says anything about the value of each arm. */
6164 if (comparison_p
6165 && ((false_code = reversed_comparison_code (cond, NULL))
6166 != UNKNOWN)
6167 && REG_P (XEXP (cond, 0)))
6169 HOST_WIDE_INT nzb;
6170 rtx from = XEXP (cond, 0);
6171 rtx true_val = XEXP (cond, 1);
6172 rtx false_val = true_val;
6173 int swapped = 0;
6175 /* If FALSE_CODE is EQ, swap the codes and arms. */
6177 if (false_code == EQ)
6179 swapped = 1, true_code = EQ, false_code = NE;
6180 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
6183 /* If we are comparing against zero and the expression being tested has
6184 only a single bit that might be nonzero, that is its value when it is
6185 not equal to zero. Similarly if it is known to be -1 or 0. */
6187 if (true_code == EQ && true_val == const0_rtx
6188 && exact_log2 (nzb = nonzero_bits (from, GET_MODE (from))) >= 0)
6190 false_code = EQ;
6191 false_val = gen_int_mode (nzb, GET_MODE (from));
6193 else if (true_code == EQ && true_val == const0_rtx
6194 && (num_sign_bit_copies (from, GET_MODE (from))
6195 == GET_MODE_PRECISION (GET_MODE (from))))
6197 false_code = EQ;
6198 false_val = constm1_rtx;
6201 /* Now simplify an arm if we know the value of the register in the
6202 branch and it is used in the arm. Be careful due to the potential
6203 of locally-shared RTL. */
6205 if (reg_mentioned_p (from, true_rtx))
6206 true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
6207 from, true_val),
6208 pc_rtx, pc_rtx, 0, 0, 0);
6209 if (reg_mentioned_p (from, false_rtx))
6210 false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
6211 from, false_val),
6212 pc_rtx, pc_rtx, 0, 0, 0);
6214 SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
6215 SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
6217 true_rtx = XEXP (x, 1);
6218 false_rtx = XEXP (x, 2);
6219 true_code = GET_CODE (cond);
6222 /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
6223 reversed, do so to avoid needing two sets of patterns for
6224 subtract-and-branch insns. Similarly if we have a constant in the true
6225 arm, the false arm is the same as the first operand of the comparison, or
6226 the false arm is more complicated than the true arm. */
6228 if (comparison_p
6229 && reversed_comparison_code (cond, NULL) != UNKNOWN
6230 && (true_rtx == pc_rtx
6231 || (CONSTANT_P (true_rtx)
6232 && !CONST_INT_P (false_rtx) && false_rtx != pc_rtx)
6233 || true_rtx == const0_rtx
6234 || (OBJECT_P (true_rtx) && !OBJECT_P (false_rtx))
6235 || (GET_CODE (true_rtx) == SUBREG && OBJECT_P (SUBREG_REG (true_rtx))
6236 && !OBJECT_P (false_rtx))
6237 || reg_mentioned_p (true_rtx, false_rtx)
6238 || rtx_equal_p (false_rtx, XEXP (cond, 0))))
6240 true_code = reversed_comparison_code (cond, NULL);
6241 SUBST (XEXP (x, 0), reversed_comparison (cond, GET_MODE (cond)));
6242 SUBST (XEXP (x, 1), false_rtx);
6243 SUBST (XEXP (x, 2), true_rtx);
6245 temp = true_rtx, true_rtx = false_rtx, false_rtx = temp;
6246 cond = XEXP (x, 0);
6248 /* It is possible that the conditional has been simplified out. */
6249 true_code = GET_CODE (cond);
6250 comparison_p = COMPARISON_P (cond);
6253 /* If the two arms are identical, we don't need the comparison. */
6255 if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
6256 return true_rtx;
6258 /* Convert a == b ? b : a to "a". */
6259 if (true_code == EQ && ! side_effects_p (cond)
6260 && !HONOR_NANS (mode)
6261 && rtx_equal_p (XEXP (cond, 0), false_rtx)
6262 && rtx_equal_p (XEXP (cond, 1), true_rtx))
6263 return false_rtx;
6264 else if (true_code == NE && ! side_effects_p (cond)
6265 && !HONOR_NANS (mode)
6266 && rtx_equal_p (XEXP (cond, 0), true_rtx)
6267 && rtx_equal_p (XEXP (cond, 1), false_rtx))
6268 return true_rtx;
6270 /* Look for cases where we have (abs x) or (neg (abs X)). */
6272 if (GET_MODE_CLASS (mode) == MODE_INT
6273 && comparison_p
6274 && XEXP (cond, 1) == const0_rtx
6275 && GET_CODE (false_rtx) == NEG
6276 && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
6277 && rtx_equal_p (true_rtx, XEXP (cond, 0))
6278 && ! side_effects_p (true_rtx))
6279 switch (true_code)
6281 case GT:
6282 case GE:
6283 return simplify_gen_unary (ABS, mode, true_rtx, mode);
6284 case LT:
6285 case LE:
6286 return
6287 simplify_gen_unary (NEG, mode,
6288 simplify_gen_unary (ABS, mode, true_rtx, mode),
6289 mode);
6290 default:
6291 break;
6294 /* Look for MIN or MAX. */
6296 if ((! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
6297 && comparison_p
6298 && rtx_equal_p (XEXP (cond, 0), true_rtx)
6299 && rtx_equal_p (XEXP (cond, 1), false_rtx)
6300 && ! side_effects_p (cond))
6301 switch (true_code)
6303 case GE:
6304 case GT:
6305 return simplify_gen_binary (SMAX, mode, true_rtx, false_rtx);
6306 case LE:
6307 case LT:
6308 return simplify_gen_binary (SMIN, mode, true_rtx, false_rtx);
6309 case GEU:
6310 case GTU:
6311 return simplify_gen_binary (UMAX, mode, true_rtx, false_rtx);
6312 case LEU:
6313 case LTU:
6314 return simplify_gen_binary (UMIN, mode, true_rtx, false_rtx);
6315 default:
6316 break;
6319 /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
6320 second operand is zero, this can be done as (OP Z (mult COND C2)) where
6321 C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
6322 SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
6323 We can do this kind of thing in some cases when STORE_FLAG_VALUE is
6324 neither 1 or -1, but it isn't worth checking for. */
6326 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
6327 && comparison_p
6328 && GET_MODE_CLASS (mode) == MODE_INT
6329 && ! side_effects_p (x))
6331 rtx t = make_compound_operation (true_rtx, SET);
6332 rtx f = make_compound_operation (false_rtx, SET);
6333 rtx cond_op0 = XEXP (cond, 0);
6334 rtx cond_op1 = XEXP (cond, 1);
6335 enum rtx_code op = UNKNOWN, extend_op = UNKNOWN;
6336 machine_mode m = mode;
6337 rtx z = 0, c1 = NULL_RTX;
6339 if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
6340 || GET_CODE (t) == IOR || GET_CODE (t) == XOR
6341 || GET_CODE (t) == ASHIFT
6342 || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
6343 && rtx_equal_p (XEXP (t, 0), f))
6344 c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
6346 /* If an identity-zero op is commutative, check whether there
6347 would be a match if we swapped the operands. */
6348 else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
6349 || GET_CODE (t) == XOR)
6350 && rtx_equal_p (XEXP (t, 1), f))
6351 c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
6352 else if (GET_CODE (t) == SIGN_EXTEND
6353 && (GET_CODE (XEXP (t, 0)) == PLUS
6354 || GET_CODE (XEXP (t, 0)) == MINUS
6355 || GET_CODE (XEXP (t, 0)) == IOR
6356 || GET_CODE (XEXP (t, 0)) == XOR
6357 || GET_CODE (XEXP (t, 0)) == ASHIFT
6358 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6359 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6360 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6361 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6362 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6363 && (num_sign_bit_copies (f, GET_MODE (f))
6364 > (unsigned int)
6365 (GET_MODE_PRECISION (mode)
6366 - GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (t, 0), 0))))))
6368 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6369 extend_op = SIGN_EXTEND;
6370 m = GET_MODE (XEXP (t, 0));
6372 else if (GET_CODE (t) == SIGN_EXTEND
6373 && (GET_CODE (XEXP (t, 0)) == PLUS
6374 || GET_CODE (XEXP (t, 0)) == IOR
6375 || GET_CODE (XEXP (t, 0)) == XOR)
6376 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6377 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6378 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6379 && (num_sign_bit_copies (f, GET_MODE (f))
6380 > (unsigned int)
6381 (GET_MODE_PRECISION (mode)
6382 - GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (t, 0), 1))))))
6384 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6385 extend_op = SIGN_EXTEND;
6386 m = GET_MODE (XEXP (t, 0));
6388 else if (GET_CODE (t) == ZERO_EXTEND
6389 && (GET_CODE (XEXP (t, 0)) == PLUS
6390 || GET_CODE (XEXP (t, 0)) == MINUS
6391 || GET_CODE (XEXP (t, 0)) == IOR
6392 || GET_CODE (XEXP (t, 0)) == XOR
6393 || GET_CODE (XEXP (t, 0)) == ASHIFT
6394 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6395 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6396 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6397 && HWI_COMPUTABLE_MODE_P (mode)
6398 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6399 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6400 && ((nonzero_bits (f, GET_MODE (f))
6401 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 0))))
6402 == 0))
6404 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6405 extend_op = ZERO_EXTEND;
6406 m = GET_MODE (XEXP (t, 0));
6408 else if (GET_CODE (t) == ZERO_EXTEND
6409 && (GET_CODE (XEXP (t, 0)) == PLUS
6410 || GET_CODE (XEXP (t, 0)) == IOR
6411 || GET_CODE (XEXP (t, 0)) == XOR)
6412 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6413 && HWI_COMPUTABLE_MODE_P (mode)
6414 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6415 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6416 && ((nonzero_bits (f, GET_MODE (f))
6417 & ~GET_MODE_MASK (GET_MODE (XEXP (XEXP (t, 0), 1))))
6418 == 0))
6420 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6421 extend_op = ZERO_EXTEND;
6422 m = GET_MODE (XEXP (t, 0));
6425 if (z)
6427 temp = subst (simplify_gen_relational (true_code, m, VOIDmode,
6428 cond_op0, cond_op1),
6429 pc_rtx, pc_rtx, 0, 0, 0);
6430 temp = simplify_gen_binary (MULT, m, temp,
6431 simplify_gen_binary (MULT, m, c1,
6432 const_true_rtx));
6433 temp = subst (temp, pc_rtx, pc_rtx, 0, 0, 0);
6434 temp = simplify_gen_binary (op, m, gen_lowpart (m, z), temp);
6436 if (extend_op != UNKNOWN)
6437 temp = simplify_gen_unary (extend_op, mode, temp, m);
6439 return temp;
6443 /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
6444 1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
6445 negation of a single bit, we can convert this operation to a shift. We
6446 can actually do this more generally, but it doesn't seem worth it. */
6448 if (true_code == NE && XEXP (cond, 1) == const0_rtx
6449 && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
6450 && ((1 == nonzero_bits (XEXP (cond, 0), mode)
6451 && (i = exact_log2 (UINTVAL (true_rtx))) >= 0)
6452 || ((num_sign_bit_copies (XEXP (cond, 0), mode)
6453 == GET_MODE_PRECISION (mode))
6454 && (i = exact_log2 (-UINTVAL (true_rtx))) >= 0)))
6455 return
6456 simplify_shift_const (NULL_RTX, ASHIFT, mode,
6457 gen_lowpart (mode, XEXP (cond, 0)), i);
6459 /* (IF_THEN_ELSE (NE REG 0) (0) (8)) is REG for nonzero_bits (REG) == 8. */
6460 if (true_code == NE && XEXP (cond, 1) == const0_rtx
6461 && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
6462 && GET_MODE (XEXP (cond, 0)) == mode
6463 && (UINTVAL (true_rtx) & GET_MODE_MASK (mode))
6464 == nonzero_bits (XEXP (cond, 0), mode)
6465 && (i = exact_log2 (UINTVAL (true_rtx) & GET_MODE_MASK (mode))) >= 0)
6466 return XEXP (cond, 0);
6468 return x;
6471 /* Simplify X, a SET expression. Return the new expression. */
6473 static rtx
6474 simplify_set (rtx x)
6476 rtx src = SET_SRC (x);
6477 rtx dest = SET_DEST (x);
6478 machine_mode mode
6479 = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
6480 rtx_insn *other_insn;
6481 rtx *cc_use;
6483 /* (set (pc) (return)) gets written as (return). */
6484 if (GET_CODE (dest) == PC && ANY_RETURN_P (src))
6485 return src;
6487 /* Now that we know for sure which bits of SRC we are using, see if we can
6488 simplify the expression for the object knowing that we only need the
6489 low-order bits. */
6491 if (GET_MODE_CLASS (mode) == MODE_INT && HWI_COMPUTABLE_MODE_P (mode))
6493 src = force_to_mode (src, mode, ~(unsigned HOST_WIDE_INT) 0, 0);
6494 SUBST (SET_SRC (x), src);
6497 /* If we are setting CC0 or if the source is a COMPARE, look for the use of
6498 the comparison result and try to simplify it unless we already have used
6499 undobuf.other_insn. */
6500 if ((GET_MODE_CLASS (mode) == MODE_CC
6501 || GET_CODE (src) == COMPARE
6502 || CC0_P (dest))
6503 && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
6504 && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
6505 && COMPARISON_P (*cc_use)
6506 && rtx_equal_p (XEXP (*cc_use, 0), dest))
6508 enum rtx_code old_code = GET_CODE (*cc_use);
6509 enum rtx_code new_code;
6510 rtx op0, op1, tmp;
6511 int other_changed = 0;
6512 rtx inner_compare = NULL_RTX;
6513 machine_mode compare_mode = GET_MODE (dest);
6515 if (GET_CODE (src) == COMPARE)
6517 op0 = XEXP (src, 0), op1 = XEXP (src, 1);
6518 if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
6520 inner_compare = op0;
6521 op0 = XEXP (inner_compare, 0), op1 = XEXP (inner_compare, 1);
6524 else
6525 op0 = src, op1 = CONST0_RTX (GET_MODE (src));
6527 tmp = simplify_relational_operation (old_code, compare_mode, VOIDmode,
6528 op0, op1);
6529 if (!tmp)
6530 new_code = old_code;
6531 else if (!CONSTANT_P (tmp))
6533 new_code = GET_CODE (tmp);
6534 op0 = XEXP (tmp, 0);
6535 op1 = XEXP (tmp, 1);
6537 else
6539 rtx pat = PATTERN (other_insn);
6540 undobuf.other_insn = other_insn;
6541 SUBST (*cc_use, tmp);
6543 /* Attempt to simplify CC user. */
6544 if (GET_CODE (pat) == SET)
6546 rtx new_rtx = simplify_rtx (SET_SRC (pat));
6547 if (new_rtx != NULL_RTX)
6548 SUBST (SET_SRC (pat), new_rtx);
6551 /* Convert X into a no-op move. */
6552 SUBST (SET_DEST (x), pc_rtx);
6553 SUBST (SET_SRC (x), pc_rtx);
6554 return x;
6557 /* Simplify our comparison, if possible. */
6558 new_code = simplify_comparison (new_code, &op0, &op1);
6560 #ifdef SELECT_CC_MODE
6561 /* If this machine has CC modes other than CCmode, check to see if we
6562 need to use a different CC mode here. */
6563 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
6564 compare_mode = GET_MODE (op0);
6565 else if (inner_compare
6566 && GET_MODE_CLASS (GET_MODE (inner_compare)) == MODE_CC
6567 && new_code == old_code
6568 && op0 == XEXP (inner_compare, 0)
6569 && op1 == XEXP (inner_compare, 1))
6570 compare_mode = GET_MODE (inner_compare);
6571 else
6572 compare_mode = SELECT_CC_MODE (new_code, op0, op1);
6574 #ifndef HAVE_cc0
6575 /* If the mode changed, we have to change SET_DEST, the mode in the
6576 compare, and the mode in the place SET_DEST is used. If SET_DEST is
6577 a hard register, just build new versions with the proper mode. If it
6578 is a pseudo, we lose unless it is only time we set the pseudo, in
6579 which case we can safely change its mode. */
6580 if (compare_mode != GET_MODE (dest))
6582 if (can_change_dest_mode (dest, 0, compare_mode))
6584 unsigned int regno = REGNO (dest);
6585 rtx new_dest;
6587 if (regno < FIRST_PSEUDO_REGISTER)
6588 new_dest = gen_rtx_REG (compare_mode, regno);
6589 else
6591 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
6592 new_dest = regno_reg_rtx[regno];
6595 SUBST (SET_DEST (x), new_dest);
6596 SUBST (XEXP (*cc_use, 0), new_dest);
6597 other_changed = 1;
6599 dest = new_dest;
6602 #endif /* cc0 */
6603 #endif /* SELECT_CC_MODE */
6605 /* If the code changed, we have to build a new comparison in
6606 undobuf.other_insn. */
6607 if (new_code != old_code)
6609 int other_changed_previously = other_changed;
6610 unsigned HOST_WIDE_INT mask;
6611 rtx old_cc_use = *cc_use;
6613 SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
6614 dest, const0_rtx));
6615 other_changed = 1;
6617 /* If the only change we made was to change an EQ into an NE or
6618 vice versa, OP0 has only one bit that might be nonzero, and OP1
6619 is zero, check if changing the user of the condition code will
6620 produce a valid insn. If it won't, we can keep the original code
6621 in that insn by surrounding our operation with an XOR. */
6623 if (((old_code == NE && new_code == EQ)
6624 || (old_code == EQ && new_code == NE))
6625 && ! other_changed_previously && op1 == const0_rtx
6626 && HWI_COMPUTABLE_MODE_P (GET_MODE (op0))
6627 && exact_log2 (mask = nonzero_bits (op0, GET_MODE (op0))) >= 0)
6629 rtx pat = PATTERN (other_insn), note = 0;
6631 if ((recog_for_combine (&pat, other_insn, &note) < 0
6632 && ! check_asm_operands (pat)))
6634 *cc_use = old_cc_use;
6635 other_changed = 0;
6637 op0 = simplify_gen_binary (XOR, GET_MODE (op0), op0,
6638 gen_int_mode (mask,
6639 GET_MODE (op0)));
6644 if (other_changed)
6645 undobuf.other_insn = other_insn;
6647 /* Otherwise, if we didn't previously have a COMPARE in the
6648 correct mode, we need one. */
6649 if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode)
6651 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
6652 src = SET_SRC (x);
6654 else if (GET_MODE (op0) == compare_mode && op1 == const0_rtx)
6656 SUBST (SET_SRC (x), op0);
6657 src = SET_SRC (x);
6659 /* Otherwise, update the COMPARE if needed. */
6660 else if (XEXP (src, 0) != op0 || XEXP (src, 1) != op1)
6662 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
6663 src = SET_SRC (x);
6666 else
6668 /* Get SET_SRC in a form where we have placed back any
6669 compound expressions. Then do the checks below. */
6670 src = make_compound_operation (src, SET);
6671 SUBST (SET_SRC (x), src);
6674 /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
6675 and X being a REG or (subreg (reg)), we may be able to convert this to
6676 (set (subreg:m2 x) (op)).
6678 We can always do this if M1 is narrower than M2 because that means that
6679 we only care about the low bits of the result.
6681 However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
6682 perform a narrower operation than requested since the high-order bits will
6683 be undefined. On machine where it is defined, this transformation is safe
6684 as long as M1 and M2 have the same number of words. */
6686 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6687 && !OBJECT_P (SUBREG_REG (src))
6688 && (((GET_MODE_SIZE (GET_MODE (src)) + (UNITS_PER_WORD - 1))
6689 / UNITS_PER_WORD)
6690 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (src)))
6691 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))
6692 #ifndef WORD_REGISTER_OPERATIONS
6693 && (GET_MODE_SIZE (GET_MODE (src))
6694 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))
6695 #endif
6696 #ifdef CANNOT_CHANGE_MODE_CLASS
6697 && ! (REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER
6698 && REG_CANNOT_CHANGE_MODE_P (REGNO (dest),
6699 GET_MODE (SUBREG_REG (src)),
6700 GET_MODE (src)))
6701 #endif
6702 && (REG_P (dest)
6703 || (GET_CODE (dest) == SUBREG
6704 && REG_P (SUBREG_REG (dest)))))
6706 SUBST (SET_DEST (x),
6707 gen_lowpart (GET_MODE (SUBREG_REG (src)),
6708 dest));
6709 SUBST (SET_SRC (x), SUBREG_REG (src));
6711 src = SET_SRC (x), dest = SET_DEST (x);
6714 #ifdef HAVE_cc0
6715 /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
6716 in SRC. */
6717 if (dest == cc0_rtx
6718 && GET_CODE (src) == SUBREG
6719 && subreg_lowpart_p (src)
6720 && (GET_MODE_PRECISION (GET_MODE (src))
6721 < GET_MODE_PRECISION (GET_MODE (SUBREG_REG (src)))))
6723 rtx inner = SUBREG_REG (src);
6724 machine_mode inner_mode = GET_MODE (inner);
6726 /* Here we make sure that we don't have a sign bit on. */
6727 if (val_signbit_known_clear_p (GET_MODE (src),
6728 nonzero_bits (inner, inner_mode)))
6730 SUBST (SET_SRC (x), inner);
6731 src = SET_SRC (x);
6734 #endif
6736 #ifdef LOAD_EXTEND_OP
6737 /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
6738 would require a paradoxical subreg. Replace the subreg with a
6739 zero_extend to avoid the reload that would otherwise be required. */
6741 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6742 && INTEGRAL_MODE_P (GET_MODE (SUBREG_REG (src)))
6743 && LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))) != UNKNOWN
6744 && SUBREG_BYTE (src) == 0
6745 && paradoxical_subreg_p (src)
6746 && MEM_P (SUBREG_REG (src)))
6748 SUBST (SET_SRC (x),
6749 gen_rtx_fmt_e (LOAD_EXTEND_OP (GET_MODE (SUBREG_REG (src))),
6750 GET_MODE (src), SUBREG_REG (src)));
6752 src = SET_SRC (x);
6754 #endif
6756 /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
6757 are comparing an item known to be 0 or -1 against 0, use a logical
6758 operation instead. Check for one of the arms being an IOR of the other
6759 arm with some value. We compute three terms to be IOR'ed together. In
6760 practice, at most two will be nonzero. Then we do the IOR's. */
6762 if (GET_CODE (dest) != PC
6763 && GET_CODE (src) == IF_THEN_ELSE
6764 && GET_MODE_CLASS (GET_MODE (src)) == MODE_INT
6765 && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
6766 && XEXP (XEXP (src, 0), 1) == const0_rtx
6767 && GET_MODE (src) == GET_MODE (XEXP (XEXP (src, 0), 0))
6768 #ifdef HAVE_conditional_move
6769 && ! can_conditionally_move_p (GET_MODE (src))
6770 #endif
6771 && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0),
6772 GET_MODE (XEXP (XEXP (src, 0), 0)))
6773 == GET_MODE_PRECISION (GET_MODE (XEXP (XEXP (src, 0), 0))))
6774 && ! side_effects_p (src))
6776 rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
6777 ? XEXP (src, 1) : XEXP (src, 2));
6778 rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
6779 ? XEXP (src, 2) : XEXP (src, 1));
6780 rtx term1 = const0_rtx, term2, term3;
6782 if (GET_CODE (true_rtx) == IOR
6783 && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
6784 term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
6785 else if (GET_CODE (true_rtx) == IOR
6786 && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
6787 term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
6788 else if (GET_CODE (false_rtx) == IOR
6789 && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
6790 term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
6791 else if (GET_CODE (false_rtx) == IOR
6792 && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
6793 term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
6795 term2 = simplify_gen_binary (AND, GET_MODE (src),
6796 XEXP (XEXP (src, 0), 0), true_rtx);
6797 term3 = simplify_gen_binary (AND, GET_MODE (src),
6798 simplify_gen_unary (NOT, GET_MODE (src),
6799 XEXP (XEXP (src, 0), 0),
6800 GET_MODE (src)),
6801 false_rtx);
6803 SUBST (SET_SRC (x),
6804 simplify_gen_binary (IOR, GET_MODE (src),
6805 simplify_gen_binary (IOR, GET_MODE (src),
6806 term1, term2),
6807 term3));
6809 src = SET_SRC (x);
6812 /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
6813 whole thing fail. */
6814 if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
6815 return src;
6816 else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
6817 return dest;
6818 else
6819 /* Convert this into a field assignment operation, if possible. */
6820 return make_field_assignment (x);
6823 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
6824 result. */
6826 static rtx
6827 simplify_logical (rtx x)
6829 machine_mode mode = GET_MODE (x);
6830 rtx op0 = XEXP (x, 0);
6831 rtx op1 = XEXP (x, 1);
6833 switch (GET_CODE (x))
6835 case AND:
6836 /* We can call simplify_and_const_int only if we don't lose
6837 any (sign) bits when converting INTVAL (op1) to
6838 "unsigned HOST_WIDE_INT". */
6839 if (CONST_INT_P (op1)
6840 && (HWI_COMPUTABLE_MODE_P (mode)
6841 || INTVAL (op1) > 0))
6843 x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
6844 if (GET_CODE (x) != AND)
6845 return x;
6847 op0 = XEXP (x, 0);
6848 op1 = XEXP (x, 1);
6851 /* If we have any of (and (ior A B) C) or (and (xor A B) C),
6852 apply the distributive law and then the inverse distributive
6853 law to see if things simplify. */
6854 if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
6856 rtx result = distribute_and_simplify_rtx (x, 0);
6857 if (result)
6858 return result;
6860 if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
6862 rtx result = distribute_and_simplify_rtx (x, 1);
6863 if (result)
6864 return result;
6866 break;
6868 case IOR:
6869 /* If we have (ior (and A B) C), apply the distributive law and then
6870 the inverse distributive law to see if things simplify. */
6872 if (GET_CODE (op0) == AND)
6874 rtx result = distribute_and_simplify_rtx (x, 0);
6875 if (result)
6876 return result;
6879 if (GET_CODE (op1) == AND)
6881 rtx result = distribute_and_simplify_rtx (x, 1);
6882 if (result)
6883 return result;
6885 break;
6887 default:
6888 gcc_unreachable ();
6891 return x;
6894 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
6895 operations" because they can be replaced with two more basic operations.
6896 ZERO_EXTEND is also considered "compound" because it can be replaced with
6897 an AND operation, which is simpler, though only one operation.
6899 The function expand_compound_operation is called with an rtx expression
6900 and will convert it to the appropriate shifts and AND operations,
6901 simplifying at each stage.
6903 The function make_compound_operation is called to convert an expression
6904 consisting of shifts and ANDs into the equivalent compound expression.
6905 It is the inverse of this function, loosely speaking. */
6907 static rtx
6908 expand_compound_operation (rtx x)
6910 unsigned HOST_WIDE_INT pos = 0, len;
6911 int unsignedp = 0;
6912 unsigned int modewidth;
6913 rtx tem;
6915 switch (GET_CODE (x))
6917 case ZERO_EXTEND:
6918 unsignedp = 1;
6919 case SIGN_EXTEND:
6920 /* We can't necessarily use a const_int for a multiword mode;
6921 it depends on implicitly extending the value.
6922 Since we don't know the right way to extend it,
6923 we can't tell whether the implicit way is right.
6925 Even for a mode that is no wider than a const_int,
6926 we can't win, because we need to sign extend one of its bits through
6927 the rest of it, and we don't know which bit. */
6928 if (CONST_INT_P (XEXP (x, 0)))
6929 return x;
6931 /* Return if (subreg:MODE FROM 0) is not a safe replacement for
6932 (zero_extend:MODE FROM) or (sign_extend:MODE FROM). It is for any MEM
6933 because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
6934 reloaded. If not for that, MEM's would very rarely be safe.
6936 Reject MODEs bigger than a word, because we might not be able
6937 to reference a two-register group starting with an arbitrary register
6938 (and currently gen_lowpart might crash for a SUBREG). */
6940 if (GET_MODE_SIZE (GET_MODE (XEXP (x, 0))) > UNITS_PER_WORD)
6941 return x;
6943 /* Reject MODEs that aren't scalar integers because turning vector
6944 or complex modes into shifts causes problems. */
6946 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6947 return x;
6949 len = GET_MODE_PRECISION (GET_MODE (XEXP (x, 0)));
6950 /* If the inner object has VOIDmode (the only way this can happen
6951 is if it is an ASM_OPERANDS), we can't do anything since we don't
6952 know how much masking to do. */
6953 if (len == 0)
6954 return x;
6956 break;
6958 case ZERO_EXTRACT:
6959 unsignedp = 1;
6961 /* ... fall through ... */
6963 case SIGN_EXTRACT:
6964 /* If the operand is a CLOBBER, just return it. */
6965 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
6966 return XEXP (x, 0);
6968 if (!CONST_INT_P (XEXP (x, 1))
6969 || !CONST_INT_P (XEXP (x, 2))
6970 || GET_MODE (XEXP (x, 0)) == VOIDmode)
6971 return x;
6973 /* Reject MODEs that aren't scalar integers because turning vector
6974 or complex modes into shifts causes problems. */
6976 if (! SCALAR_INT_MODE_P (GET_MODE (XEXP (x, 0))))
6977 return x;
6979 len = INTVAL (XEXP (x, 1));
6980 pos = INTVAL (XEXP (x, 2));
6982 /* This should stay within the object being extracted, fail otherwise. */
6983 if (len + pos > GET_MODE_PRECISION (GET_MODE (XEXP (x, 0))))
6984 return x;
6986 if (BITS_BIG_ENDIAN)
6987 pos = GET_MODE_PRECISION (GET_MODE (XEXP (x, 0))) - len - pos;
6989 break;
6991 default:
6992 return x;
6994 /* Convert sign extension to zero extension, if we know that the high
6995 bit is not set, as this is easier to optimize. It will be converted
6996 back to cheaper alternative in make_extraction. */
6997 if (GET_CODE (x) == SIGN_EXTEND
6998 && (HWI_COMPUTABLE_MODE_P (GET_MODE (x))
6999 && ((nonzero_bits (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
7000 & ~(((unsigned HOST_WIDE_INT)
7001 GET_MODE_MASK (GET_MODE (XEXP (x, 0))))
7002 >> 1))
7003 == 0)))
7005 rtx temp = gen_rtx_ZERO_EXTEND (GET_MODE (x), XEXP (x, 0));
7006 rtx temp2 = expand_compound_operation (temp);
7008 /* Make sure this is a profitable operation. */
7009 if (set_src_cost (x, optimize_this_for_speed_p)
7010 > set_src_cost (temp2, optimize_this_for_speed_p))
7011 return temp2;
7012 else if (set_src_cost (x, optimize_this_for_speed_p)
7013 > set_src_cost (temp, optimize_this_for_speed_p))
7014 return temp;
7015 else
7016 return x;
7019 /* We can optimize some special cases of ZERO_EXTEND. */
7020 if (GET_CODE (x) == ZERO_EXTEND)
7022 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
7023 know that the last value didn't have any inappropriate bits
7024 set. */
7025 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
7026 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
7027 && HWI_COMPUTABLE_MODE_P (GET_MODE (x))
7028 && (nonzero_bits (XEXP (XEXP (x, 0), 0), GET_MODE (x))
7029 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
7030 return XEXP (XEXP (x, 0), 0);
7032 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
7033 if (GET_CODE (XEXP (x, 0)) == SUBREG
7034 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
7035 && subreg_lowpart_p (XEXP (x, 0))
7036 && HWI_COMPUTABLE_MODE_P (GET_MODE (x))
7037 && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), GET_MODE (x))
7038 & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
7039 return SUBREG_REG (XEXP (x, 0));
7041 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
7042 is a comparison and STORE_FLAG_VALUE permits. This is like
7043 the first case, but it works even when GET_MODE (x) is larger
7044 than HOST_WIDE_INT. */
7045 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
7046 && GET_MODE (XEXP (XEXP (x, 0), 0)) == GET_MODE (x)
7047 && COMPARISON_P (XEXP (XEXP (x, 0), 0))
7048 && (GET_MODE_PRECISION (GET_MODE (XEXP (x, 0)))
7049 <= HOST_BITS_PER_WIDE_INT)
7050 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
7051 return XEXP (XEXP (x, 0), 0);
7053 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
7054 if (GET_CODE (XEXP (x, 0)) == SUBREG
7055 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == GET_MODE (x)
7056 && subreg_lowpart_p (XEXP (x, 0))
7057 && COMPARISON_P (SUBREG_REG (XEXP (x, 0)))
7058 && (GET_MODE_PRECISION (GET_MODE (XEXP (x, 0)))
7059 <= HOST_BITS_PER_WIDE_INT)
7060 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (GET_MODE (XEXP (x, 0)))) == 0)
7061 return SUBREG_REG (XEXP (x, 0));
7065 /* If we reach here, we want to return a pair of shifts. The inner
7066 shift is a left shift of BITSIZE - POS - LEN bits. The outer
7067 shift is a right shift of BITSIZE - LEN bits. It is arithmetic or
7068 logical depending on the value of UNSIGNEDP.
7070 If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
7071 converted into an AND of a shift.
7073 We must check for the case where the left shift would have a negative
7074 count. This can happen in a case like (x >> 31) & 255 on machines
7075 that can't shift by a constant. On those machines, we would first
7076 combine the shift with the AND to produce a variable-position
7077 extraction. Then the constant of 31 would be substituted in
7078 to produce such a position. */
7080 modewidth = GET_MODE_PRECISION (GET_MODE (x));
7081 if (modewidth >= pos + len)
7083 machine_mode mode = GET_MODE (x);
7084 tem = gen_lowpart (mode, XEXP (x, 0));
7085 if (!tem || GET_CODE (tem) == CLOBBER)
7086 return x;
7087 tem = simplify_shift_const (NULL_RTX, ASHIFT, mode,
7088 tem, modewidth - pos - len);
7089 tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
7090 mode, tem, modewidth - len);
7092 else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
7093 tem = simplify_and_const_int (NULL_RTX, GET_MODE (x),
7094 simplify_shift_const (NULL_RTX, LSHIFTRT,
7095 GET_MODE (x),
7096 XEXP (x, 0), pos),
7097 ((unsigned HOST_WIDE_INT) 1 << len) - 1);
7098 else
7099 /* Any other cases we can't handle. */
7100 return x;
7102 /* If we couldn't do this for some reason, return the original
7103 expression. */
7104 if (GET_CODE (tem) == CLOBBER)
7105 return x;
7107 return tem;
7110 /* X is a SET which contains an assignment of one object into
7111 a part of another (such as a bit-field assignment, STRICT_LOW_PART,
7112 or certain SUBREGS). If possible, convert it into a series of
7113 logical operations.
7115 We half-heartedly support variable positions, but do not at all
7116 support variable lengths. */
7118 static const_rtx
7119 expand_field_assignment (const_rtx x)
7121 rtx inner;
7122 rtx pos; /* Always counts from low bit. */
7123 int len;
7124 rtx mask, cleared, masked;
7125 machine_mode compute_mode;
7127 /* Loop until we find something we can't simplify. */
7128 while (1)
7130 if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
7131 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
7133 inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
7134 len = GET_MODE_PRECISION (GET_MODE (XEXP (SET_DEST (x), 0)));
7135 pos = GEN_INT (subreg_lsb (XEXP (SET_DEST (x), 0)));
7137 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
7138 && CONST_INT_P (XEXP (SET_DEST (x), 1)))
7140 inner = XEXP (SET_DEST (x), 0);
7141 len = INTVAL (XEXP (SET_DEST (x), 1));
7142 pos = XEXP (SET_DEST (x), 2);
7144 /* A constant position should stay within the width of INNER. */
7145 if (CONST_INT_P (pos)
7146 && INTVAL (pos) + len > GET_MODE_PRECISION (GET_MODE (inner)))
7147 break;
7149 if (BITS_BIG_ENDIAN)
7151 if (CONST_INT_P (pos))
7152 pos = GEN_INT (GET_MODE_PRECISION (GET_MODE (inner)) - len
7153 - INTVAL (pos));
7154 else if (GET_CODE (pos) == MINUS
7155 && CONST_INT_P (XEXP (pos, 1))
7156 && (INTVAL (XEXP (pos, 1))
7157 == GET_MODE_PRECISION (GET_MODE (inner)) - len))
7158 /* If position is ADJUST - X, new position is X. */
7159 pos = XEXP (pos, 0);
7160 else
7162 HOST_WIDE_INT prec = GET_MODE_PRECISION (GET_MODE (inner));
7163 pos = simplify_gen_binary (MINUS, GET_MODE (pos),
7164 gen_int_mode (prec - len,
7165 GET_MODE (pos)),
7166 pos);
7171 /* A SUBREG between two modes that occupy the same numbers of words
7172 can be done by moving the SUBREG to the source. */
7173 else if (GET_CODE (SET_DEST (x)) == SUBREG
7174 /* We need SUBREGs to compute nonzero_bits properly. */
7175 && nonzero_sign_valid
7176 && (((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
7177 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
7178 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
7179 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
7181 x = gen_rtx_SET (VOIDmode, SUBREG_REG (SET_DEST (x)),
7182 gen_lowpart
7183 (GET_MODE (SUBREG_REG (SET_DEST (x))),
7184 SET_SRC (x)));
7185 continue;
7187 else
7188 break;
7190 while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
7191 inner = SUBREG_REG (inner);
7193 compute_mode = GET_MODE (inner);
7195 /* Don't attempt bitwise arithmetic on non scalar integer modes. */
7196 if (! SCALAR_INT_MODE_P (compute_mode))
7198 machine_mode imode;
7200 /* Don't do anything for vector or complex integral types. */
7201 if (! FLOAT_MODE_P (compute_mode))
7202 break;
7204 /* Try to find an integral mode to pun with. */
7205 imode = mode_for_size (GET_MODE_BITSIZE (compute_mode), MODE_INT, 0);
7206 if (imode == BLKmode)
7207 break;
7209 compute_mode = imode;
7210 inner = gen_lowpart (imode, inner);
7213 /* Compute a mask of LEN bits, if we can do this on the host machine. */
7214 if (len >= HOST_BITS_PER_WIDE_INT)
7215 break;
7217 /* Now compute the equivalent expression. Make a copy of INNER
7218 for the SET_DEST in case it is a MEM into which we will substitute;
7219 we don't want shared RTL in that case. */
7220 mask = gen_int_mode (((unsigned HOST_WIDE_INT) 1 << len) - 1,
7221 compute_mode);
7222 cleared = simplify_gen_binary (AND, compute_mode,
7223 simplify_gen_unary (NOT, compute_mode,
7224 simplify_gen_binary (ASHIFT,
7225 compute_mode,
7226 mask, pos),
7227 compute_mode),
7228 inner);
7229 masked = simplify_gen_binary (ASHIFT, compute_mode,
7230 simplify_gen_binary (
7231 AND, compute_mode,
7232 gen_lowpart (compute_mode, SET_SRC (x)),
7233 mask),
7234 pos);
7236 x = gen_rtx_SET (VOIDmode, copy_rtx (inner),
7237 simplify_gen_binary (IOR, compute_mode,
7238 cleared, masked));
7241 return x;
7244 /* Return an RTX for a reference to LEN bits of INNER. If POS_RTX is nonzero,
7245 it is an RTX that represents the (variable) starting position; otherwise,
7246 POS is the (constant) starting bit position. Both are counted from the LSB.
7248 UNSIGNEDP is nonzero for an unsigned reference and zero for a signed one.
7250 IN_DEST is nonzero if this is a reference in the destination of a SET.
7251 This is used when a ZERO_ or SIGN_EXTRACT isn't needed. If nonzero,
7252 a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
7253 be used.
7255 IN_COMPARE is nonzero if we are in a COMPARE. This means that a
7256 ZERO_EXTRACT should be built even for bits starting at bit 0.
7258 MODE is the desired mode of the result (if IN_DEST == 0).
7260 The result is an RTX for the extraction or NULL_RTX if the target
7261 can't handle it. */
7263 static rtx
7264 make_extraction (machine_mode mode, rtx inner, HOST_WIDE_INT pos,
7265 rtx pos_rtx, unsigned HOST_WIDE_INT len, int unsignedp,
7266 int in_dest, int in_compare)
7268 /* This mode describes the size of the storage area
7269 to fetch the overall value from. Within that, we
7270 ignore the POS lowest bits, etc. */
7271 machine_mode is_mode = GET_MODE (inner);
7272 machine_mode inner_mode;
7273 machine_mode wanted_inner_mode;
7274 machine_mode wanted_inner_reg_mode = word_mode;
7275 machine_mode pos_mode = word_mode;
7276 machine_mode extraction_mode = word_mode;
7277 machine_mode tmode = mode_for_size (len, MODE_INT, 1);
7278 rtx new_rtx = 0;
7279 rtx orig_pos_rtx = pos_rtx;
7280 HOST_WIDE_INT orig_pos;
7282 if (pos_rtx && CONST_INT_P (pos_rtx))
7283 pos = INTVAL (pos_rtx), pos_rtx = 0;
7285 if (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
7287 /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
7288 consider just the QI as the memory to extract from.
7289 The subreg adds or removes high bits; its mode is
7290 irrelevant to the meaning of this extraction,
7291 since POS and LEN count from the lsb. */
7292 if (MEM_P (SUBREG_REG (inner)))
7293 is_mode = GET_MODE (SUBREG_REG (inner));
7294 inner = SUBREG_REG (inner);
7296 else if (GET_CODE (inner) == ASHIFT
7297 && CONST_INT_P (XEXP (inner, 1))
7298 && pos_rtx == 0 && pos == 0
7299 && len > UINTVAL (XEXP (inner, 1)))
7301 /* We're extracting the least significant bits of an rtx
7302 (ashift X (const_int C)), where LEN > C. Extract the
7303 least significant (LEN - C) bits of X, giving an rtx
7304 whose mode is MODE, then shift it left C times. */
7305 new_rtx = make_extraction (mode, XEXP (inner, 0),
7306 0, 0, len - INTVAL (XEXP (inner, 1)),
7307 unsignedp, in_dest, in_compare);
7308 if (new_rtx != 0)
7309 return gen_rtx_ASHIFT (mode, new_rtx, XEXP (inner, 1));
7311 else if (GET_CODE (inner) == TRUNCATE)
7312 inner = XEXP (inner, 0);
7314 inner_mode = GET_MODE (inner);
7316 /* See if this can be done without an extraction. We never can if the
7317 width of the field is not the same as that of some integer mode. For
7318 registers, we can only avoid the extraction if the position is at the
7319 low-order bit and this is either not in the destination or we have the
7320 appropriate STRICT_LOW_PART operation available.
7322 For MEM, we can avoid an extract if the field starts on an appropriate
7323 boundary and we can change the mode of the memory reference. */
7325 if (tmode != BLKmode
7326 && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
7327 && !MEM_P (inner)
7328 && (inner_mode == tmode
7329 || !REG_P (inner)
7330 || TRULY_NOOP_TRUNCATION_MODES_P (tmode, inner_mode)
7331 || reg_truncated_to_mode (tmode, inner))
7332 && (! in_dest
7333 || (REG_P (inner)
7334 && have_insn_for (STRICT_LOW_PART, tmode))))
7335 || (MEM_P (inner) && pos_rtx == 0
7336 && (pos
7337 % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
7338 : BITS_PER_UNIT)) == 0
7339 /* We can't do this if we are widening INNER_MODE (it
7340 may not be aligned, for one thing). */
7341 && GET_MODE_PRECISION (inner_mode) >= GET_MODE_PRECISION (tmode)
7342 && (inner_mode == tmode
7343 || (! mode_dependent_address_p (XEXP (inner, 0),
7344 MEM_ADDR_SPACE (inner))
7345 && ! MEM_VOLATILE_P (inner))))))
7347 /* If INNER is a MEM, make a new MEM that encompasses just the desired
7348 field. If the original and current mode are the same, we need not
7349 adjust the offset. Otherwise, we do if bytes big endian.
7351 If INNER is not a MEM, get a piece consisting of just the field
7352 of interest (in this case POS % BITS_PER_WORD must be 0). */
7354 if (MEM_P (inner))
7356 HOST_WIDE_INT offset;
7358 /* POS counts from lsb, but make OFFSET count in memory order. */
7359 if (BYTES_BIG_ENDIAN)
7360 offset = (GET_MODE_PRECISION (is_mode) - len - pos) / BITS_PER_UNIT;
7361 else
7362 offset = pos / BITS_PER_UNIT;
7364 new_rtx = adjust_address_nv (inner, tmode, offset);
7366 else if (REG_P (inner))
7368 if (tmode != inner_mode)
7370 /* We can't call gen_lowpart in a DEST since we
7371 always want a SUBREG (see below) and it would sometimes
7372 return a new hard register. */
7373 if (pos || in_dest)
7375 HOST_WIDE_INT final_word = pos / BITS_PER_WORD;
7377 if (WORDS_BIG_ENDIAN
7378 && GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
7379 final_word = ((GET_MODE_SIZE (inner_mode)
7380 - GET_MODE_SIZE (tmode))
7381 / UNITS_PER_WORD) - final_word;
7383 final_word *= UNITS_PER_WORD;
7384 if (BYTES_BIG_ENDIAN &&
7385 GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (tmode))
7386 final_word += (GET_MODE_SIZE (inner_mode)
7387 - GET_MODE_SIZE (tmode)) % UNITS_PER_WORD;
7389 /* Avoid creating invalid subregs, for example when
7390 simplifying (x>>32)&255. */
7391 if (!validate_subreg (tmode, inner_mode, inner, final_word))
7392 return NULL_RTX;
7394 new_rtx = gen_rtx_SUBREG (tmode, inner, final_word);
7396 else
7397 new_rtx = gen_lowpart (tmode, inner);
7399 else
7400 new_rtx = inner;
7402 else
7403 new_rtx = force_to_mode (inner, tmode,
7404 len >= HOST_BITS_PER_WIDE_INT
7405 ? ~(unsigned HOST_WIDE_INT) 0
7406 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
7409 /* If this extraction is going into the destination of a SET,
7410 make a STRICT_LOW_PART unless we made a MEM. */
7412 if (in_dest)
7413 return (MEM_P (new_rtx) ? new_rtx
7414 : (GET_CODE (new_rtx) != SUBREG
7415 ? gen_rtx_CLOBBER (tmode, const0_rtx)
7416 : gen_rtx_STRICT_LOW_PART (VOIDmode, new_rtx)));
7418 if (mode == tmode)
7419 return new_rtx;
7421 if (CONST_SCALAR_INT_P (new_rtx))
7422 return simplify_unary_operation (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7423 mode, new_rtx, tmode);
7425 /* If we know that no extraneous bits are set, and that the high
7426 bit is not set, convert the extraction to the cheaper of
7427 sign and zero extension, that are equivalent in these cases. */
7428 if (flag_expensive_optimizations
7429 && (HWI_COMPUTABLE_MODE_P (tmode)
7430 && ((nonzero_bits (new_rtx, tmode)
7431 & ~(((unsigned HOST_WIDE_INT)GET_MODE_MASK (tmode)) >> 1))
7432 == 0)))
7434 rtx temp = gen_rtx_ZERO_EXTEND (mode, new_rtx);
7435 rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new_rtx);
7437 /* Prefer ZERO_EXTENSION, since it gives more information to
7438 backends. */
7439 if (set_src_cost (temp, optimize_this_for_speed_p)
7440 <= set_src_cost (temp1, optimize_this_for_speed_p))
7441 return temp;
7442 return temp1;
7445 /* Otherwise, sign- or zero-extend unless we already are in the
7446 proper mode. */
7448 return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7449 mode, new_rtx));
7452 /* Unless this is a COMPARE or we have a funny memory reference,
7453 don't do anything with zero-extending field extracts starting at
7454 the low-order bit since they are simple AND operations. */
7455 if (pos_rtx == 0 && pos == 0 && ! in_dest
7456 && ! in_compare && unsignedp)
7457 return 0;
7459 /* Unless INNER is not MEM, reject this if we would be spanning bytes or
7460 if the position is not a constant and the length is not 1. In all
7461 other cases, we would only be going outside our object in cases when
7462 an original shift would have been undefined. */
7463 if (MEM_P (inner)
7464 && ((pos_rtx == 0 && pos + len > GET_MODE_PRECISION (is_mode))
7465 || (pos_rtx != 0 && len != 1)))
7466 return 0;
7468 enum extraction_pattern pattern = (in_dest ? EP_insv
7469 : unsignedp ? EP_extzv : EP_extv);
7471 /* If INNER is not from memory, we want it to have the mode of a register
7472 extraction pattern's structure operand, or word_mode if there is no
7473 such pattern. The same applies to extraction_mode and pos_mode
7474 and their respective operands.
7476 For memory, assume that the desired extraction_mode and pos_mode
7477 are the same as for a register operation, since at present we don't
7478 have named patterns for aligned memory structures. */
7479 struct extraction_insn insn;
7480 if (get_best_reg_extraction_insn (&insn, pattern,
7481 GET_MODE_BITSIZE (inner_mode), mode))
7483 wanted_inner_reg_mode = insn.struct_mode;
7484 pos_mode = insn.pos_mode;
7485 extraction_mode = insn.field_mode;
7488 /* Never narrow an object, since that might not be safe. */
7490 if (mode != VOIDmode
7491 && GET_MODE_SIZE (extraction_mode) < GET_MODE_SIZE (mode))
7492 extraction_mode = mode;
7494 if (!MEM_P (inner))
7495 wanted_inner_mode = wanted_inner_reg_mode;
7496 else
7498 /* Be careful not to go beyond the extracted object and maintain the
7499 natural alignment of the memory. */
7500 wanted_inner_mode = smallest_mode_for_size (len, MODE_INT);
7501 while (pos % GET_MODE_BITSIZE (wanted_inner_mode) + len
7502 > GET_MODE_BITSIZE (wanted_inner_mode))
7504 wanted_inner_mode = GET_MODE_WIDER_MODE (wanted_inner_mode);
7505 gcc_assert (wanted_inner_mode != VOIDmode);
7509 orig_pos = pos;
7511 if (BITS_BIG_ENDIAN)
7513 /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
7514 BITS_BIG_ENDIAN style. If position is constant, compute new
7515 position. Otherwise, build subtraction.
7516 Note that POS is relative to the mode of the original argument.
7517 If it's a MEM we need to recompute POS relative to that.
7518 However, if we're extracting from (or inserting into) a register,
7519 we want to recompute POS relative to wanted_inner_mode. */
7520 int width = (MEM_P (inner)
7521 ? GET_MODE_BITSIZE (is_mode)
7522 : GET_MODE_BITSIZE (wanted_inner_mode));
7524 if (pos_rtx == 0)
7525 pos = width - len - pos;
7526 else
7527 pos_rtx
7528 = gen_rtx_MINUS (GET_MODE (pos_rtx),
7529 gen_int_mode (width - len, GET_MODE (pos_rtx)),
7530 pos_rtx);
7531 /* POS may be less than 0 now, but we check for that below.
7532 Note that it can only be less than 0 if !MEM_P (inner). */
7535 /* If INNER has a wider mode, and this is a constant extraction, try to
7536 make it smaller and adjust the byte to point to the byte containing
7537 the value. */
7538 if (wanted_inner_mode != VOIDmode
7539 && inner_mode != wanted_inner_mode
7540 && ! pos_rtx
7541 && GET_MODE_SIZE (wanted_inner_mode) < GET_MODE_SIZE (is_mode)
7542 && MEM_P (inner)
7543 && ! mode_dependent_address_p (XEXP (inner, 0), MEM_ADDR_SPACE (inner))
7544 && ! MEM_VOLATILE_P (inner))
7546 int offset = 0;
7548 /* The computations below will be correct if the machine is big
7549 endian in both bits and bytes or little endian in bits and bytes.
7550 If it is mixed, we must adjust. */
7552 /* If bytes are big endian and we had a paradoxical SUBREG, we must
7553 adjust OFFSET to compensate. */
7554 if (BYTES_BIG_ENDIAN
7555 && GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (is_mode))
7556 offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
7558 /* We can now move to the desired byte. */
7559 offset += (pos / GET_MODE_BITSIZE (wanted_inner_mode))
7560 * GET_MODE_SIZE (wanted_inner_mode);
7561 pos %= GET_MODE_BITSIZE (wanted_inner_mode);
7563 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
7564 && is_mode != wanted_inner_mode)
7565 offset = (GET_MODE_SIZE (is_mode)
7566 - GET_MODE_SIZE (wanted_inner_mode) - offset);
7568 inner = adjust_address_nv (inner, wanted_inner_mode, offset);
7571 /* If INNER is not memory, get it into the proper mode. If we are changing
7572 its mode, POS must be a constant and smaller than the size of the new
7573 mode. */
7574 else if (!MEM_P (inner))
7576 /* On the LHS, don't create paradoxical subregs implicitely truncating
7577 the register unless TRULY_NOOP_TRUNCATION. */
7578 if (in_dest
7579 && !TRULY_NOOP_TRUNCATION_MODES_P (GET_MODE (inner),
7580 wanted_inner_mode))
7581 return NULL_RTX;
7583 if (GET_MODE (inner) != wanted_inner_mode
7584 && (pos_rtx != 0
7585 || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
7586 return NULL_RTX;
7588 if (orig_pos < 0)
7589 return NULL_RTX;
7591 inner = force_to_mode (inner, wanted_inner_mode,
7592 pos_rtx
7593 || len + orig_pos >= HOST_BITS_PER_WIDE_INT
7594 ? ~(unsigned HOST_WIDE_INT) 0
7595 : ((((unsigned HOST_WIDE_INT) 1 << len) - 1)
7596 << orig_pos),
7600 /* Adjust mode of POS_RTX, if needed. If we want a wider mode, we
7601 have to zero extend. Otherwise, we can just use a SUBREG. */
7602 if (pos_rtx != 0
7603 && GET_MODE_SIZE (pos_mode) > GET_MODE_SIZE (GET_MODE (pos_rtx)))
7605 rtx temp = simplify_gen_unary (ZERO_EXTEND, pos_mode, pos_rtx,
7606 GET_MODE (pos_rtx));
7608 /* If we know that no extraneous bits are set, and that the high
7609 bit is not set, convert extraction to cheaper one - either
7610 SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
7611 cases. */
7612 if (flag_expensive_optimizations
7613 && (HWI_COMPUTABLE_MODE_P (GET_MODE (pos_rtx))
7614 && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
7615 & ~(((unsigned HOST_WIDE_INT)
7616 GET_MODE_MASK (GET_MODE (pos_rtx)))
7617 >> 1))
7618 == 0)))
7620 rtx temp1 = simplify_gen_unary (SIGN_EXTEND, pos_mode, pos_rtx,
7621 GET_MODE (pos_rtx));
7623 /* Prefer ZERO_EXTENSION, since it gives more information to
7624 backends. */
7625 if (set_src_cost (temp1, optimize_this_for_speed_p)
7626 < set_src_cost (temp, optimize_this_for_speed_p))
7627 temp = temp1;
7629 pos_rtx = temp;
7632 /* Make POS_RTX unless we already have it and it is correct. If we don't
7633 have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
7634 be a CONST_INT. */
7635 if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
7636 pos_rtx = orig_pos_rtx;
7638 else if (pos_rtx == 0)
7639 pos_rtx = GEN_INT (pos);
7641 /* Make the required operation. See if we can use existing rtx. */
7642 new_rtx = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
7643 extraction_mode, inner, GEN_INT (len), pos_rtx);
7644 if (! in_dest)
7645 new_rtx = gen_lowpart (mode, new_rtx);
7647 return new_rtx;
7650 /* See if X contains an ASHIFT of COUNT or more bits that can be commuted
7651 with any other operations in X. Return X without that shift if so. */
7653 static rtx
7654 extract_left_shift (rtx x, int count)
7656 enum rtx_code code = GET_CODE (x);
7657 machine_mode mode = GET_MODE (x);
7658 rtx tem;
7660 switch (code)
7662 case ASHIFT:
7663 /* This is the shift itself. If it is wide enough, we will return
7664 either the value being shifted if the shift count is equal to
7665 COUNT or a shift for the difference. */
7666 if (CONST_INT_P (XEXP (x, 1))
7667 && INTVAL (XEXP (x, 1)) >= count)
7668 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
7669 INTVAL (XEXP (x, 1)) - count);
7670 break;
7672 case NEG: case NOT:
7673 if ((tem = extract_left_shift (XEXP (x, 0), count)) != 0)
7674 return simplify_gen_unary (code, mode, tem, mode);
7676 break;
7678 case PLUS: case IOR: case XOR: case AND:
7679 /* If we can safely shift this constant and we find the inner shift,
7680 make a new operation. */
7681 if (CONST_INT_P (XEXP (x, 1))
7682 && (UINTVAL (XEXP (x, 1))
7683 & ((((unsigned HOST_WIDE_INT) 1 << count)) - 1)) == 0
7684 && (tem = extract_left_shift (XEXP (x, 0), count)) != 0)
7686 HOST_WIDE_INT val = INTVAL (XEXP (x, 1)) >> count;
7687 return simplify_gen_binary (code, mode, tem,
7688 gen_int_mode (val, mode));
7690 break;
7692 default:
7693 break;
7696 return 0;
7699 /* Look at the expression rooted at X. Look for expressions
7700 equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
7701 Form these expressions.
7703 Return the new rtx, usually just X.
7705 Also, for machines like the VAX that don't have logical shift insns,
7706 try to convert logical to arithmetic shift operations in cases where
7707 they are equivalent. This undoes the canonicalizations to logical
7708 shifts done elsewhere.
7710 We try, as much as possible, to re-use rtl expressions to save memory.
7712 IN_CODE says what kind of expression we are processing. Normally, it is
7713 SET. In a memory address (inside a MEM, PLUS or minus, the latter two
7714 being kludges), it is MEM. When processing the arguments of a comparison
7715 or a COMPARE against zero, it is COMPARE. */
7718 make_compound_operation (rtx x, enum rtx_code in_code)
7720 enum rtx_code code = GET_CODE (x);
7721 machine_mode mode = GET_MODE (x);
7722 int mode_width = GET_MODE_PRECISION (mode);
7723 rtx rhs, lhs;
7724 enum rtx_code next_code;
7725 int i, j;
7726 rtx new_rtx = 0;
7727 rtx tem;
7728 const char *fmt;
7730 /* Select the code to be used in recursive calls. Once we are inside an
7731 address, we stay there. If we have a comparison, set to COMPARE,
7732 but once inside, go back to our default of SET. */
7734 next_code = (code == MEM ? MEM
7735 : ((code == PLUS || code == MINUS)
7736 && SCALAR_INT_MODE_P (mode)) ? MEM
7737 : ((code == COMPARE || COMPARISON_P (x))
7738 && XEXP (x, 1) == const0_rtx) ? COMPARE
7739 : in_code == COMPARE ? SET : in_code);
7741 /* Process depending on the code of this operation. If NEW is set
7742 nonzero, it will be returned. */
7744 switch (code)
7746 case ASHIFT:
7747 /* Convert shifts by constants into multiplications if inside
7748 an address. */
7749 if (in_code == MEM && CONST_INT_P (XEXP (x, 1))
7750 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
7751 && INTVAL (XEXP (x, 1)) >= 0
7752 && SCALAR_INT_MODE_P (mode))
7754 HOST_WIDE_INT count = INTVAL (XEXP (x, 1));
7755 HOST_WIDE_INT multval = (HOST_WIDE_INT) 1 << count;
7757 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
7758 if (GET_CODE (new_rtx) == NEG)
7760 new_rtx = XEXP (new_rtx, 0);
7761 multval = -multval;
7763 multval = trunc_int_for_mode (multval, mode);
7764 new_rtx = gen_rtx_MULT (mode, new_rtx, gen_int_mode (multval, mode));
7766 break;
7768 case PLUS:
7769 lhs = XEXP (x, 0);
7770 rhs = XEXP (x, 1);
7771 lhs = make_compound_operation (lhs, next_code);
7772 rhs = make_compound_operation (rhs, next_code);
7773 if (GET_CODE (lhs) == MULT && GET_CODE (XEXP (lhs, 0)) == NEG
7774 && SCALAR_INT_MODE_P (mode))
7776 tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (lhs, 0), 0),
7777 XEXP (lhs, 1));
7778 new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
7780 else if (GET_CODE (lhs) == MULT
7781 && (CONST_INT_P (XEXP (lhs, 1)) && INTVAL (XEXP (lhs, 1)) < 0))
7783 tem = simplify_gen_binary (MULT, mode, XEXP (lhs, 0),
7784 simplify_gen_unary (NEG, mode,
7785 XEXP (lhs, 1),
7786 mode));
7787 new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
7789 else
7791 SUBST (XEXP (x, 0), lhs);
7792 SUBST (XEXP (x, 1), rhs);
7793 goto maybe_swap;
7795 x = gen_lowpart (mode, new_rtx);
7796 goto maybe_swap;
7798 case MINUS:
7799 lhs = XEXP (x, 0);
7800 rhs = XEXP (x, 1);
7801 lhs = make_compound_operation (lhs, next_code);
7802 rhs = make_compound_operation (rhs, next_code);
7803 if (GET_CODE (rhs) == MULT && GET_CODE (XEXP (rhs, 0)) == NEG
7804 && SCALAR_INT_MODE_P (mode))
7806 tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (rhs, 0), 0),
7807 XEXP (rhs, 1));
7808 new_rtx = simplify_gen_binary (PLUS, mode, tem, lhs);
7810 else if (GET_CODE (rhs) == MULT
7811 && (CONST_INT_P (XEXP (rhs, 1)) && INTVAL (XEXP (rhs, 1)) < 0))
7813 tem = simplify_gen_binary (MULT, mode, XEXP (rhs, 0),
7814 simplify_gen_unary (NEG, mode,
7815 XEXP (rhs, 1),
7816 mode));
7817 new_rtx = simplify_gen_binary (PLUS, mode, tem, lhs);
7819 else
7821 SUBST (XEXP (x, 0), lhs);
7822 SUBST (XEXP (x, 1), rhs);
7823 return x;
7825 return gen_lowpart (mode, new_rtx);
7827 case AND:
7828 /* If the second operand is not a constant, we can't do anything
7829 with it. */
7830 if (!CONST_INT_P (XEXP (x, 1)))
7831 break;
7833 /* If the constant is a power of two minus one and the first operand
7834 is a logical right shift, make an extraction. */
7835 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7836 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7838 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
7839 new_rtx = make_extraction (mode, new_rtx, 0, XEXP (XEXP (x, 0), 1), i, 1,
7840 0, in_code == COMPARE);
7843 /* Same as previous, but for (subreg (lshiftrt ...)) in first op. */
7844 else if (GET_CODE (XEXP (x, 0)) == SUBREG
7845 && subreg_lowpart_p (XEXP (x, 0))
7846 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
7847 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7849 new_rtx = make_compound_operation (XEXP (SUBREG_REG (XEXP (x, 0)), 0),
7850 next_code);
7851 new_rtx = make_extraction (GET_MODE (SUBREG_REG (XEXP (x, 0))), new_rtx, 0,
7852 XEXP (SUBREG_REG (XEXP (x, 0)), 1), i, 1,
7853 0, in_code == COMPARE);
7855 /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)). */
7856 else if ((GET_CODE (XEXP (x, 0)) == XOR
7857 || GET_CODE (XEXP (x, 0)) == IOR)
7858 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
7859 && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
7860 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7862 /* Apply the distributive law, and then try to make extractions. */
7863 new_rtx = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
7864 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
7865 XEXP (x, 1)),
7866 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
7867 XEXP (x, 1)));
7868 new_rtx = make_compound_operation (new_rtx, in_code);
7871 /* If we are have (and (rotate X C) M) and C is larger than the number
7872 of bits in M, this is an extraction. */
7874 else if (GET_CODE (XEXP (x, 0)) == ROTATE
7875 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7876 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0
7877 && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
7879 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
7880 new_rtx = make_extraction (mode, new_rtx,
7881 (GET_MODE_PRECISION (mode)
7882 - INTVAL (XEXP (XEXP (x, 0), 1))),
7883 NULL_RTX, i, 1, 0, in_code == COMPARE);
7886 /* On machines without logical shifts, if the operand of the AND is
7887 a logical shift and our mask turns off all the propagated sign
7888 bits, we can replace the logical shift with an arithmetic shift. */
7889 else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
7890 && !have_insn_for (LSHIFTRT, mode)
7891 && have_insn_for (ASHIFTRT, mode)
7892 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
7893 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
7894 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
7895 && mode_width <= HOST_BITS_PER_WIDE_INT)
7897 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
7899 mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
7900 if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
7901 SUBST (XEXP (x, 0),
7902 gen_rtx_ASHIFTRT (mode,
7903 make_compound_operation
7904 (XEXP (XEXP (x, 0), 0), next_code),
7905 XEXP (XEXP (x, 0), 1)));
7908 /* If the constant is one less than a power of two, this might be
7909 representable by an extraction even if no shift is present.
7910 If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
7911 we are in a COMPARE. */
7912 else if ((i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
7913 new_rtx = make_extraction (mode,
7914 make_compound_operation (XEXP (x, 0),
7915 next_code),
7916 0, NULL_RTX, i, 1, 0, in_code == COMPARE);
7918 /* If we are in a comparison and this is an AND with a power of two,
7919 convert this into the appropriate bit extract. */
7920 else if (in_code == COMPARE
7921 && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
7922 new_rtx = make_extraction (mode,
7923 make_compound_operation (XEXP (x, 0),
7924 next_code),
7925 i, NULL_RTX, 1, 1, 0, 1);
7927 break;
7929 case LSHIFTRT:
7930 /* If the sign bit is known to be zero, replace this with an
7931 arithmetic shift. */
7932 if (have_insn_for (ASHIFTRT, mode)
7933 && ! have_insn_for (LSHIFTRT, mode)
7934 && mode_width <= HOST_BITS_PER_WIDE_INT
7935 && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
7937 new_rtx = gen_rtx_ASHIFTRT (mode,
7938 make_compound_operation (XEXP (x, 0),
7939 next_code),
7940 XEXP (x, 1));
7941 break;
7944 /* ... fall through ... */
7946 case ASHIFTRT:
7947 lhs = XEXP (x, 0);
7948 rhs = XEXP (x, 1);
7950 /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
7951 this is a SIGN_EXTRACT. */
7952 if (CONST_INT_P (rhs)
7953 && GET_CODE (lhs) == ASHIFT
7954 && CONST_INT_P (XEXP (lhs, 1))
7955 && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1))
7956 && INTVAL (XEXP (lhs, 1)) >= 0
7957 && INTVAL (rhs) < mode_width)
7959 new_rtx = make_compound_operation (XEXP (lhs, 0), next_code);
7960 new_rtx = make_extraction (mode, new_rtx,
7961 INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
7962 NULL_RTX, mode_width - INTVAL (rhs),
7963 code == LSHIFTRT, 0, in_code == COMPARE);
7964 break;
7967 /* See if we have operations between an ASHIFTRT and an ASHIFT.
7968 If so, try to merge the shifts into a SIGN_EXTEND. We could
7969 also do this for some cases of SIGN_EXTRACT, but it doesn't
7970 seem worth the effort; the case checked for occurs on Alpha. */
7972 if (!OBJECT_P (lhs)
7973 && ! (GET_CODE (lhs) == SUBREG
7974 && (OBJECT_P (SUBREG_REG (lhs))))
7975 && CONST_INT_P (rhs)
7976 && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
7977 && INTVAL (rhs) < mode_width
7978 && (new_rtx = extract_left_shift (lhs, INTVAL (rhs))) != 0)
7979 new_rtx = make_extraction (mode, make_compound_operation (new_rtx, next_code),
7980 0, NULL_RTX, mode_width - INTVAL (rhs),
7981 code == LSHIFTRT, 0, in_code == COMPARE);
7983 break;
7985 case SUBREG:
7986 /* Call ourselves recursively on the inner expression. If we are
7987 narrowing the object and it has a different RTL code from
7988 what it originally did, do this SUBREG as a force_to_mode. */
7990 rtx inner = SUBREG_REG (x), simplified;
7991 enum rtx_code subreg_code = in_code;
7993 /* If in_code is COMPARE, it isn't always safe to pass it through
7994 to the recursive make_compound_operation call. */
7995 if (subreg_code == COMPARE
7996 && (!subreg_lowpart_p (x)
7997 || GET_CODE (inner) == SUBREG
7998 /* (subreg:SI (and:DI (reg:DI) (const_int 0x800000000)) 0)
7999 is (const_int 0), rather than
8000 (subreg:SI (lshiftrt:DI (reg:DI) (const_int 35)) 0). */
8001 || (GET_CODE (inner) == AND
8002 && CONST_INT_P (XEXP (inner, 1))
8003 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (inner))
8004 && exact_log2 (UINTVAL (XEXP (inner, 1)))
8005 >= GET_MODE_BITSIZE (mode))))
8006 subreg_code = SET;
8008 tem = make_compound_operation (inner, subreg_code);
8010 simplified
8011 = simplify_subreg (mode, tem, GET_MODE (inner), SUBREG_BYTE (x));
8012 if (simplified)
8013 tem = simplified;
8015 if (GET_CODE (tem) != GET_CODE (inner)
8016 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (inner))
8017 && subreg_lowpart_p (x))
8019 rtx newer
8020 = force_to_mode (tem, mode, ~(unsigned HOST_WIDE_INT) 0, 0);
8022 /* If we have something other than a SUBREG, we might have
8023 done an expansion, so rerun ourselves. */
8024 if (GET_CODE (newer) != SUBREG)
8025 newer = make_compound_operation (newer, in_code);
8027 /* force_to_mode can expand compounds. If it just re-expanded the
8028 compound, use gen_lowpart to convert to the desired mode. */
8029 if (rtx_equal_p (newer, x)
8030 /* Likewise if it re-expanded the compound only partially.
8031 This happens for SUBREG of ZERO_EXTRACT if they extract
8032 the same number of bits. */
8033 || (GET_CODE (newer) == SUBREG
8034 && (GET_CODE (SUBREG_REG (newer)) == LSHIFTRT
8035 || GET_CODE (SUBREG_REG (newer)) == ASHIFTRT)
8036 && GET_CODE (inner) == AND
8037 && rtx_equal_p (SUBREG_REG (newer), XEXP (inner, 0))))
8038 return gen_lowpart (GET_MODE (x), tem);
8040 return newer;
8043 if (simplified)
8044 return tem;
8046 break;
8048 default:
8049 break;
8052 if (new_rtx)
8054 x = gen_lowpart (mode, new_rtx);
8055 code = GET_CODE (x);
8058 /* Now recursively process each operand of this operation. We need to
8059 handle ZERO_EXTEND specially so that we don't lose track of the
8060 inner mode. */
8061 if (GET_CODE (x) == ZERO_EXTEND)
8063 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
8064 tem = simplify_const_unary_operation (ZERO_EXTEND, GET_MODE (x),
8065 new_rtx, GET_MODE (XEXP (x, 0)));
8066 if (tem)
8067 return tem;
8068 SUBST (XEXP (x, 0), new_rtx);
8069 return x;
8072 fmt = GET_RTX_FORMAT (code);
8073 for (i = 0; i < GET_RTX_LENGTH (code); i++)
8074 if (fmt[i] == 'e')
8076 new_rtx = make_compound_operation (XEXP (x, i), next_code);
8077 SUBST (XEXP (x, i), new_rtx);
8079 else if (fmt[i] == 'E')
8080 for (j = 0; j < XVECLEN (x, i); j++)
8082 new_rtx = make_compound_operation (XVECEXP (x, i, j), next_code);
8083 SUBST (XVECEXP (x, i, j), new_rtx);
8086 maybe_swap:
8087 /* If this is a commutative operation, the changes to the operands
8088 may have made it noncanonical. */
8089 if (COMMUTATIVE_ARITH_P (x)
8090 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
8092 tem = XEXP (x, 0);
8093 SUBST (XEXP (x, 0), XEXP (x, 1));
8094 SUBST (XEXP (x, 1), tem);
8097 return x;
8100 /* Given M see if it is a value that would select a field of bits
8101 within an item, but not the entire word. Return -1 if not.
8102 Otherwise, return the starting position of the field, where 0 is the
8103 low-order bit.
8105 *PLEN is set to the length of the field. */
8107 static int
8108 get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
8110 /* Get the bit number of the first 1 bit from the right, -1 if none. */
8111 int pos = m ? ctz_hwi (m) : -1;
8112 int len = 0;
8114 if (pos >= 0)
8115 /* Now shift off the low-order zero bits and see if we have a
8116 power of two minus 1. */
8117 len = exact_log2 ((m >> pos) + 1);
8119 if (len <= 0)
8120 pos = -1;
8122 *plen = len;
8123 return pos;
8126 /* If X refers to a register that equals REG in value, replace these
8127 references with REG. */
8128 static rtx
8129 canon_reg_for_combine (rtx x, rtx reg)
8131 rtx op0, op1, op2;
8132 const char *fmt;
8133 int i;
8134 bool copied;
8136 enum rtx_code code = GET_CODE (x);
8137 switch (GET_RTX_CLASS (code))
8139 case RTX_UNARY:
8140 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8141 if (op0 != XEXP (x, 0))
8142 return simplify_gen_unary (GET_CODE (x), GET_MODE (x), op0,
8143 GET_MODE (reg));
8144 break;
8146 case RTX_BIN_ARITH:
8147 case RTX_COMM_ARITH:
8148 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8149 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8150 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8151 return simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
8152 break;
8154 case RTX_COMPARE:
8155 case RTX_COMM_COMPARE:
8156 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8157 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8158 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8159 return simplify_gen_relational (GET_CODE (x), GET_MODE (x),
8160 GET_MODE (op0), op0, op1);
8161 break;
8163 case RTX_TERNARY:
8164 case RTX_BITFIELD_OPS:
8165 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8166 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8167 op2 = canon_reg_for_combine (XEXP (x, 2), reg);
8168 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1) || op2 != XEXP (x, 2))
8169 return simplify_gen_ternary (GET_CODE (x), GET_MODE (x),
8170 GET_MODE (op0), op0, op1, op2);
8172 case RTX_OBJ:
8173 if (REG_P (x))
8175 if (rtx_equal_p (get_last_value (reg), x)
8176 || rtx_equal_p (reg, get_last_value (x)))
8177 return reg;
8178 else
8179 break;
8182 /* fall through */
8184 default:
8185 fmt = GET_RTX_FORMAT (code);
8186 copied = false;
8187 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8188 if (fmt[i] == 'e')
8190 rtx op = canon_reg_for_combine (XEXP (x, i), reg);
8191 if (op != XEXP (x, i))
8193 if (!copied)
8195 copied = true;
8196 x = copy_rtx (x);
8198 XEXP (x, i) = op;
8201 else if (fmt[i] == 'E')
8203 int j;
8204 for (j = 0; j < XVECLEN (x, i); j++)
8206 rtx op = canon_reg_for_combine (XVECEXP (x, i, j), reg);
8207 if (op != XVECEXP (x, i, j))
8209 if (!copied)
8211 copied = true;
8212 x = copy_rtx (x);
8214 XVECEXP (x, i, j) = op;
8219 break;
8222 return x;
8225 /* Return X converted to MODE. If the value is already truncated to
8226 MODE we can just return a subreg even though in the general case we
8227 would need an explicit truncation. */
8229 static rtx
8230 gen_lowpart_or_truncate (machine_mode mode, rtx x)
8232 if (!CONST_INT_P (x)
8233 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (x))
8234 && !TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (x))
8235 && !(REG_P (x) && reg_truncated_to_mode (mode, x)))
8237 /* Bit-cast X into an integer mode. */
8238 if (!SCALAR_INT_MODE_P (GET_MODE (x)))
8239 x = gen_lowpart (int_mode_for_mode (GET_MODE (x)), x);
8240 x = simplify_gen_unary (TRUNCATE, int_mode_for_mode (mode),
8241 x, GET_MODE (x));
8244 return gen_lowpart (mode, x);
8247 /* See if X can be simplified knowing that we will only refer to it in
8248 MODE and will only refer to those bits that are nonzero in MASK.
8249 If other bits are being computed or if masking operations are done
8250 that select a superset of the bits in MASK, they can sometimes be
8251 ignored.
8253 Return a possibly simplified expression, but always convert X to
8254 MODE. If X is a CONST_INT, AND the CONST_INT with MASK.
8256 If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
8257 are all off in X. This is used when X will be complemented, by either
8258 NOT, NEG, or XOR. */
8260 static rtx
8261 force_to_mode (rtx x, machine_mode mode, unsigned HOST_WIDE_INT mask,
8262 int just_select)
8264 enum rtx_code code = GET_CODE (x);
8265 int next_select = just_select || code == XOR || code == NOT || code == NEG;
8266 machine_mode op_mode;
8267 unsigned HOST_WIDE_INT fuller_mask, nonzero;
8268 rtx op0, op1, temp;
8270 /* If this is a CALL or ASM_OPERANDS, don't do anything. Some of the
8271 code below will do the wrong thing since the mode of such an
8272 expression is VOIDmode.
8274 Also do nothing if X is a CLOBBER; this can happen if X was
8275 the return value from a call to gen_lowpart. */
8276 if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
8277 return x;
8279 /* We want to perform the operation in its present mode unless we know
8280 that the operation is valid in MODE, in which case we do the operation
8281 in MODE. */
8282 op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
8283 && have_insn_for (code, mode))
8284 ? mode : GET_MODE (x));
8286 /* It is not valid to do a right-shift in a narrower mode
8287 than the one it came in with. */
8288 if ((code == LSHIFTRT || code == ASHIFTRT)
8289 && GET_MODE_PRECISION (mode) < GET_MODE_PRECISION (GET_MODE (x)))
8290 op_mode = GET_MODE (x);
8292 /* Truncate MASK to fit OP_MODE. */
8293 if (op_mode)
8294 mask &= GET_MODE_MASK (op_mode);
8296 /* When we have an arithmetic operation, or a shift whose count we
8297 do not know, we need to assume that all bits up to the highest-order
8298 bit in MASK will be needed. This is how we form such a mask. */
8299 if (mask & ((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT - 1)))
8300 fuller_mask = ~(unsigned HOST_WIDE_INT) 0;
8301 else
8302 fuller_mask = (((unsigned HOST_WIDE_INT) 1 << (floor_log2 (mask) + 1))
8303 - 1);
8305 /* Determine what bits of X are guaranteed to be (non)zero. */
8306 nonzero = nonzero_bits (x, mode);
8308 /* If none of the bits in X are needed, return a zero. */
8309 if (!just_select && (nonzero & mask) == 0 && !side_effects_p (x))
8310 x = const0_rtx;
8312 /* If X is a CONST_INT, return a new one. Do this here since the
8313 test below will fail. */
8314 if (CONST_INT_P (x))
8316 if (SCALAR_INT_MODE_P (mode))
8317 return gen_int_mode (INTVAL (x) & mask, mode);
8318 else
8320 x = GEN_INT (INTVAL (x) & mask);
8321 return gen_lowpart_common (mode, x);
8325 /* If X is narrower than MODE and we want all the bits in X's mode, just
8326 get X in the proper mode. */
8327 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (mode)
8328 && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
8329 return gen_lowpart (mode, x);
8331 /* We can ignore the effect of a SUBREG if it narrows the mode or
8332 if the constant masks to zero all the bits the mode doesn't have. */
8333 if (GET_CODE (x) == SUBREG
8334 && subreg_lowpart_p (x)
8335 && ((GET_MODE_SIZE (GET_MODE (x))
8336 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))))
8337 || (0 == (mask
8338 & GET_MODE_MASK (GET_MODE (x))
8339 & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))))))
8340 return force_to_mode (SUBREG_REG (x), mode, mask, next_select);
8342 /* The arithmetic simplifications here only work for scalar integer modes. */
8343 if (!SCALAR_INT_MODE_P (mode) || !SCALAR_INT_MODE_P (GET_MODE (x)))
8344 return gen_lowpart_or_truncate (mode, x);
8346 switch (code)
8348 case CLOBBER:
8349 /* If X is a (clobber (const_int)), return it since we know we are
8350 generating something that won't match. */
8351 return x;
8353 case SIGN_EXTEND:
8354 case ZERO_EXTEND:
8355 case ZERO_EXTRACT:
8356 case SIGN_EXTRACT:
8357 x = expand_compound_operation (x);
8358 if (GET_CODE (x) != code)
8359 return force_to_mode (x, mode, mask, next_select);
8360 break;
8362 case TRUNCATE:
8363 /* Similarly for a truncate. */
8364 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8366 case AND:
8367 /* If this is an AND with a constant, convert it into an AND
8368 whose constant is the AND of that constant with MASK. If it
8369 remains an AND of MASK, delete it since it is redundant. */
8371 if (CONST_INT_P (XEXP (x, 1)))
8373 x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
8374 mask & INTVAL (XEXP (x, 1)));
8376 /* If X is still an AND, see if it is an AND with a mask that
8377 is just some low-order bits. If so, and it is MASK, we don't
8378 need it. */
8380 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8381 && ((INTVAL (XEXP (x, 1)) & GET_MODE_MASK (GET_MODE (x)))
8382 == mask))
8383 x = XEXP (x, 0);
8385 /* If it remains an AND, try making another AND with the bits
8386 in the mode mask that aren't in MASK turned on. If the
8387 constant in the AND is wide enough, this might make a
8388 cheaper constant. */
8390 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8391 && GET_MODE_MASK (GET_MODE (x)) != mask
8392 && HWI_COMPUTABLE_MODE_P (GET_MODE (x)))
8394 unsigned HOST_WIDE_INT cval
8395 = UINTVAL (XEXP (x, 1))
8396 | (GET_MODE_MASK (GET_MODE (x)) & ~mask);
8397 rtx y;
8399 y = simplify_gen_binary (AND, GET_MODE (x), XEXP (x, 0),
8400 gen_int_mode (cval, GET_MODE (x)));
8401 if (set_src_cost (y, optimize_this_for_speed_p)
8402 < set_src_cost (x, optimize_this_for_speed_p))
8403 x = y;
8406 break;
8409 goto binop;
8411 case PLUS:
8412 /* In (and (plus FOO C1) M), if M is a mask that just turns off
8413 low-order bits (as in an alignment operation) and FOO is already
8414 aligned to that boundary, mask C1 to that boundary as well.
8415 This may eliminate that PLUS and, later, the AND. */
8418 unsigned int width = GET_MODE_PRECISION (mode);
8419 unsigned HOST_WIDE_INT smask = mask;
8421 /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
8422 number, sign extend it. */
8424 if (width < HOST_BITS_PER_WIDE_INT
8425 && (smask & (HOST_WIDE_INT_1U << (width - 1))) != 0)
8426 smask |= HOST_WIDE_INT_M1U << width;
8428 if (CONST_INT_P (XEXP (x, 1))
8429 && exact_log2 (- smask) >= 0
8430 && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
8431 && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
8432 return force_to_mode (plus_constant (GET_MODE (x), XEXP (x, 0),
8433 (INTVAL (XEXP (x, 1)) & smask)),
8434 mode, smask, next_select);
8437 /* ... fall through ... */
8439 case MULT:
8440 /* For PLUS, MINUS and MULT, we need any bits less significant than the
8441 most significant bit in MASK since carries from those bits will
8442 affect the bits we are interested in. */
8443 mask = fuller_mask;
8444 goto binop;
8446 case MINUS:
8447 /* If X is (minus C Y) where C's least set bit is larger than any bit
8448 in the mask, then we may replace with (neg Y). */
8449 if (CONST_INT_P (XEXP (x, 0))
8450 && ((UINTVAL (XEXP (x, 0)) & -UINTVAL (XEXP (x, 0))) > mask))
8452 x = simplify_gen_unary (NEG, GET_MODE (x), XEXP (x, 1),
8453 GET_MODE (x));
8454 return force_to_mode (x, mode, mask, next_select);
8457 /* Similarly, if C contains every bit in the fuller_mask, then we may
8458 replace with (not Y). */
8459 if (CONST_INT_P (XEXP (x, 0))
8460 && ((UINTVAL (XEXP (x, 0)) | fuller_mask) == UINTVAL (XEXP (x, 0))))
8462 x = simplify_gen_unary (NOT, GET_MODE (x),
8463 XEXP (x, 1), GET_MODE (x));
8464 return force_to_mode (x, mode, mask, next_select);
8467 mask = fuller_mask;
8468 goto binop;
8470 case IOR:
8471 case XOR:
8472 /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
8473 LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
8474 operation which may be a bitfield extraction. Ensure that the
8475 constant we form is not wider than the mode of X. */
8477 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8478 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8479 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8480 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
8481 && CONST_INT_P (XEXP (x, 1))
8482 && ((INTVAL (XEXP (XEXP (x, 0), 1))
8483 + floor_log2 (INTVAL (XEXP (x, 1))))
8484 < GET_MODE_PRECISION (GET_MODE (x)))
8485 && (UINTVAL (XEXP (x, 1))
8486 & ~nonzero_bits (XEXP (x, 0), GET_MODE (x))) == 0)
8488 temp = gen_int_mode ((INTVAL (XEXP (x, 1)) & mask)
8489 << INTVAL (XEXP (XEXP (x, 0), 1)),
8490 GET_MODE (x));
8491 temp = simplify_gen_binary (GET_CODE (x), GET_MODE (x),
8492 XEXP (XEXP (x, 0), 0), temp);
8493 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), temp,
8494 XEXP (XEXP (x, 0), 1));
8495 return force_to_mode (x, mode, mask, next_select);
8498 binop:
8499 /* For most binary operations, just propagate into the operation and
8500 change the mode if we have an operation of that mode. */
8502 op0 = force_to_mode (XEXP (x, 0), mode, mask, next_select);
8503 op1 = force_to_mode (XEXP (x, 1), mode, mask, next_select);
8505 /* If we ended up truncating both operands, truncate the result of the
8506 operation instead. */
8507 if (GET_CODE (op0) == TRUNCATE
8508 && GET_CODE (op1) == TRUNCATE)
8510 op0 = XEXP (op0, 0);
8511 op1 = XEXP (op1, 0);
8514 op0 = gen_lowpart_or_truncate (op_mode, op0);
8515 op1 = gen_lowpart_or_truncate (op_mode, op1);
8517 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8518 x = simplify_gen_binary (code, op_mode, op0, op1);
8519 break;
8521 case ASHIFT:
8522 /* For left shifts, do the same, but just for the first operand.
8523 However, we cannot do anything with shifts where we cannot
8524 guarantee that the counts are smaller than the size of the mode
8525 because such a count will have a different meaning in a
8526 wider mode. */
8528 if (! (CONST_INT_P (XEXP (x, 1))
8529 && INTVAL (XEXP (x, 1)) >= 0
8530 && INTVAL (XEXP (x, 1)) < GET_MODE_PRECISION (mode))
8531 && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
8532 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
8533 < (unsigned HOST_WIDE_INT) GET_MODE_PRECISION (mode))))
8534 break;
8536 /* If the shift count is a constant and we can do arithmetic in
8537 the mode of the shift, refine which bits we need. Otherwise, use the
8538 conservative form of the mask. */
8539 if (CONST_INT_P (XEXP (x, 1))
8540 && INTVAL (XEXP (x, 1)) >= 0
8541 && INTVAL (XEXP (x, 1)) < GET_MODE_PRECISION (op_mode)
8542 && HWI_COMPUTABLE_MODE_P (op_mode))
8543 mask >>= INTVAL (XEXP (x, 1));
8544 else
8545 mask = fuller_mask;
8547 op0 = gen_lowpart_or_truncate (op_mode,
8548 force_to_mode (XEXP (x, 0), op_mode,
8549 mask, next_select));
8551 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
8552 x = simplify_gen_binary (code, op_mode, op0, XEXP (x, 1));
8553 break;
8555 case LSHIFTRT:
8556 /* Here we can only do something if the shift count is a constant,
8557 this shift constant is valid for the host, and we can do arithmetic
8558 in OP_MODE. */
8560 if (CONST_INT_P (XEXP (x, 1))
8561 && INTVAL (XEXP (x, 1)) >= 0
8562 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
8563 && HWI_COMPUTABLE_MODE_P (op_mode))
8565 rtx inner = XEXP (x, 0);
8566 unsigned HOST_WIDE_INT inner_mask;
8568 /* Select the mask of the bits we need for the shift operand. */
8569 inner_mask = mask << INTVAL (XEXP (x, 1));
8571 /* We can only change the mode of the shift if we can do arithmetic
8572 in the mode of the shift and INNER_MASK is no wider than the
8573 width of X's mode. */
8574 if ((inner_mask & ~GET_MODE_MASK (GET_MODE (x))) != 0)
8575 op_mode = GET_MODE (x);
8577 inner = force_to_mode (inner, op_mode, inner_mask, next_select);
8579 if (GET_MODE (x) != op_mode || inner != XEXP (x, 0))
8580 x = simplify_gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
8583 /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
8584 shift and AND produces only copies of the sign bit (C2 is one less
8585 than a power of two), we can do this with just a shift. */
8587 if (GET_CODE (x) == LSHIFTRT
8588 && CONST_INT_P (XEXP (x, 1))
8589 /* The shift puts one of the sign bit copies in the least significant
8590 bit. */
8591 && ((INTVAL (XEXP (x, 1))
8592 + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
8593 >= GET_MODE_PRECISION (GET_MODE (x)))
8594 && exact_log2 (mask + 1) >= 0
8595 /* Number of bits left after the shift must be more than the mask
8596 needs. */
8597 && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
8598 <= GET_MODE_PRECISION (GET_MODE (x)))
8599 /* Must be more sign bit copies than the mask needs. */
8600 && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
8601 >= exact_log2 (mask + 1)))
8602 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x), XEXP (x, 0),
8603 GEN_INT (GET_MODE_PRECISION (GET_MODE (x))
8604 - exact_log2 (mask + 1)));
8606 goto shiftrt;
8608 case ASHIFTRT:
8609 /* If we are just looking for the sign bit, we don't need this shift at
8610 all, even if it has a variable count. */
8611 if (val_signbit_p (GET_MODE (x), mask))
8612 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8614 /* If this is a shift by a constant, get a mask that contains those bits
8615 that are not copies of the sign bit. We then have two cases: If
8616 MASK only includes those bits, this can be a logical shift, which may
8617 allow simplifications. If MASK is a single-bit field not within
8618 those bits, we are requesting a copy of the sign bit and hence can
8619 shift the sign bit to the appropriate location. */
8621 if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) >= 0
8622 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
8624 int i;
8626 /* If the considered data is wider than HOST_WIDE_INT, we can't
8627 represent a mask for all its bits in a single scalar.
8628 But we only care about the lower bits, so calculate these. */
8630 if (GET_MODE_PRECISION (GET_MODE (x)) > HOST_BITS_PER_WIDE_INT)
8632 nonzero = ~(unsigned HOST_WIDE_INT) 0;
8634 /* GET_MODE_PRECISION (GET_MODE (x)) - INTVAL (XEXP (x, 1))
8635 is the number of bits a full-width mask would have set.
8636 We need only shift if these are fewer than nonzero can
8637 hold. If not, we must keep all bits set in nonzero. */
8639 if (GET_MODE_PRECISION (GET_MODE (x)) - INTVAL (XEXP (x, 1))
8640 < HOST_BITS_PER_WIDE_INT)
8641 nonzero >>= INTVAL (XEXP (x, 1))
8642 + HOST_BITS_PER_WIDE_INT
8643 - GET_MODE_PRECISION (GET_MODE (x)) ;
8645 else
8647 nonzero = GET_MODE_MASK (GET_MODE (x));
8648 nonzero >>= INTVAL (XEXP (x, 1));
8651 if ((mask & ~nonzero) == 0)
8653 x = simplify_shift_const (NULL_RTX, LSHIFTRT, GET_MODE (x),
8654 XEXP (x, 0), INTVAL (XEXP (x, 1)));
8655 if (GET_CODE (x) != ASHIFTRT)
8656 return force_to_mode (x, mode, mask, next_select);
8659 else if ((i = exact_log2 (mask)) >= 0)
8661 x = simplify_shift_const
8662 (NULL_RTX, LSHIFTRT, GET_MODE (x), XEXP (x, 0),
8663 GET_MODE_PRECISION (GET_MODE (x)) - 1 - i);
8665 if (GET_CODE (x) != ASHIFTRT)
8666 return force_to_mode (x, mode, mask, next_select);
8670 /* If MASK is 1, convert this to an LSHIFTRT. This can be done
8671 even if the shift count isn't a constant. */
8672 if (mask == 1)
8673 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
8674 XEXP (x, 0), XEXP (x, 1));
8676 shiftrt:
8678 /* If this is a zero- or sign-extension operation that just affects bits
8679 we don't care about, remove it. Be sure the call above returned
8680 something that is still a shift. */
8682 if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
8683 && CONST_INT_P (XEXP (x, 1))
8684 && INTVAL (XEXP (x, 1)) >= 0
8685 && (INTVAL (XEXP (x, 1))
8686 <= GET_MODE_PRECISION (GET_MODE (x)) - (floor_log2 (mask) + 1))
8687 && GET_CODE (XEXP (x, 0)) == ASHIFT
8688 && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
8689 return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
8690 next_select);
8692 break;
8694 case ROTATE:
8695 case ROTATERT:
8696 /* If the shift count is constant and we can do computations
8697 in the mode of X, compute where the bits we care about are.
8698 Otherwise, we can't do anything. Don't change the mode of
8699 the shift or propagate MODE into the shift, though. */
8700 if (CONST_INT_P (XEXP (x, 1))
8701 && INTVAL (XEXP (x, 1)) >= 0)
8703 temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
8704 GET_MODE (x),
8705 gen_int_mode (mask, GET_MODE (x)),
8706 XEXP (x, 1));
8707 if (temp && CONST_INT_P (temp))
8708 x = simplify_gen_binary (code, GET_MODE (x),
8709 force_to_mode (XEXP (x, 0), GET_MODE (x),
8710 INTVAL (temp), next_select),
8711 XEXP (x, 1));
8713 break;
8715 case NEG:
8716 /* If we just want the low-order bit, the NEG isn't needed since it
8717 won't change the low-order bit. */
8718 if (mask == 1)
8719 return force_to_mode (XEXP (x, 0), mode, mask, just_select);
8721 /* We need any bits less significant than the most significant bit in
8722 MASK since carries from those bits will affect the bits we are
8723 interested in. */
8724 mask = fuller_mask;
8725 goto unop;
8727 case NOT:
8728 /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
8729 same as the XOR case above. Ensure that the constant we form is not
8730 wider than the mode of X. */
8732 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8733 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8734 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8735 && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
8736 < GET_MODE_PRECISION (GET_MODE (x)))
8737 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
8739 temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)),
8740 GET_MODE (x));
8741 temp = simplify_gen_binary (XOR, GET_MODE (x),
8742 XEXP (XEXP (x, 0), 0), temp);
8743 x = simplify_gen_binary (LSHIFTRT, GET_MODE (x),
8744 temp, XEXP (XEXP (x, 0), 1));
8746 return force_to_mode (x, mode, mask, next_select);
8749 /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
8750 use the full mask inside the NOT. */
8751 mask = fuller_mask;
8753 unop:
8754 op0 = gen_lowpart_or_truncate (op_mode,
8755 force_to_mode (XEXP (x, 0), mode, mask,
8756 next_select));
8757 if (op_mode != GET_MODE (x) || op0 != XEXP (x, 0))
8758 x = simplify_gen_unary (code, op_mode, op0, op_mode);
8759 break;
8761 case NE:
8762 /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
8763 in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
8764 which is equal to STORE_FLAG_VALUE. */
8765 if ((mask & ~STORE_FLAG_VALUE) == 0
8766 && XEXP (x, 1) == const0_rtx
8767 && GET_MODE (XEXP (x, 0)) == mode
8768 && exact_log2 (nonzero_bits (XEXP (x, 0), mode)) >= 0
8769 && (nonzero_bits (XEXP (x, 0), mode)
8770 == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
8771 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8773 break;
8775 case IF_THEN_ELSE:
8776 /* We have no way of knowing if the IF_THEN_ELSE can itself be
8777 written in a narrower mode. We play it safe and do not do so. */
8779 op0 = gen_lowpart_or_truncate (GET_MODE (x),
8780 force_to_mode (XEXP (x, 1), mode,
8781 mask, next_select));
8782 op1 = gen_lowpart_or_truncate (GET_MODE (x),
8783 force_to_mode (XEXP (x, 2), mode,
8784 mask, next_select));
8785 if (op0 != XEXP (x, 1) || op1 != XEXP (x, 2))
8786 x = simplify_gen_ternary (IF_THEN_ELSE, GET_MODE (x),
8787 GET_MODE (XEXP (x, 0)), XEXP (x, 0),
8788 op0, op1);
8789 break;
8791 default:
8792 break;
8795 /* Ensure we return a value of the proper mode. */
8796 return gen_lowpart_or_truncate (mode, x);
8799 /* Return nonzero if X is an expression that has one of two values depending on
8800 whether some other value is zero or nonzero. In that case, we return the
8801 value that is being tested, *PTRUE is set to the value if the rtx being
8802 returned has a nonzero value, and *PFALSE is set to the other alternative.
8804 If we return zero, we set *PTRUE and *PFALSE to X. */
8806 static rtx
8807 if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
8809 machine_mode mode = GET_MODE (x);
8810 enum rtx_code code = GET_CODE (x);
8811 rtx cond0, cond1, true0, true1, false0, false1;
8812 unsigned HOST_WIDE_INT nz;
8814 /* If we are comparing a value against zero, we are done. */
8815 if ((code == NE || code == EQ)
8816 && XEXP (x, 1) == const0_rtx)
8818 *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
8819 *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
8820 return XEXP (x, 0);
8823 /* If this is a unary operation whose operand has one of two values, apply
8824 our opcode to compute those values. */
8825 else if (UNARY_P (x)
8826 && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
8828 *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
8829 *pfalse = simplify_gen_unary (code, mode, false0,
8830 GET_MODE (XEXP (x, 0)));
8831 return cond0;
8834 /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
8835 make can't possibly match and would suppress other optimizations. */
8836 else if (code == COMPARE)
8839 /* If this is a binary operation, see if either side has only one of two
8840 values. If either one does or if both do and they are conditional on
8841 the same value, compute the new true and false values. */
8842 else if (BINARY_P (x))
8844 cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0);
8845 cond1 = if_then_else_cond (XEXP (x, 1), &true1, &false1);
8847 if ((cond0 != 0 || cond1 != 0)
8848 && ! (cond0 != 0 && cond1 != 0 && ! rtx_equal_p (cond0, cond1)))
8850 /* If if_then_else_cond returned zero, then true/false are the
8851 same rtl. We must copy one of them to prevent invalid rtl
8852 sharing. */
8853 if (cond0 == 0)
8854 true0 = copy_rtx (true0);
8855 else if (cond1 == 0)
8856 true1 = copy_rtx (true1);
8858 if (COMPARISON_P (x))
8860 *ptrue = simplify_gen_relational (code, mode, VOIDmode,
8861 true0, true1);
8862 *pfalse = simplify_gen_relational (code, mode, VOIDmode,
8863 false0, false1);
8865 else
8867 *ptrue = simplify_gen_binary (code, mode, true0, true1);
8868 *pfalse = simplify_gen_binary (code, mode, false0, false1);
8871 return cond0 ? cond0 : cond1;
8874 /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
8875 operands is zero when the other is nonzero, and vice-versa,
8876 and STORE_FLAG_VALUE is 1 or -1. */
8878 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8879 && (code == PLUS || code == IOR || code == XOR || code == MINUS
8880 || code == UMAX)
8881 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
8883 rtx op0 = XEXP (XEXP (x, 0), 1);
8884 rtx op1 = XEXP (XEXP (x, 1), 1);
8886 cond0 = XEXP (XEXP (x, 0), 0);
8887 cond1 = XEXP (XEXP (x, 1), 0);
8889 if (COMPARISON_P (cond0)
8890 && COMPARISON_P (cond1)
8891 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
8892 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
8893 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
8894 || ((swap_condition (GET_CODE (cond0))
8895 == reversed_comparison_code (cond1, NULL))
8896 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
8897 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
8898 && ! side_effects_p (x))
8900 *ptrue = simplify_gen_binary (MULT, mode, op0, const_true_rtx);
8901 *pfalse = simplify_gen_binary (MULT, mode,
8902 (code == MINUS
8903 ? simplify_gen_unary (NEG, mode,
8904 op1, mode)
8905 : op1),
8906 const_true_rtx);
8907 return cond0;
8911 /* Similarly for MULT, AND and UMIN, except that for these the result
8912 is always zero. */
8913 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
8914 && (code == MULT || code == AND || code == UMIN)
8915 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
8917 cond0 = XEXP (XEXP (x, 0), 0);
8918 cond1 = XEXP (XEXP (x, 1), 0);
8920 if (COMPARISON_P (cond0)
8921 && COMPARISON_P (cond1)
8922 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
8923 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
8924 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
8925 || ((swap_condition (GET_CODE (cond0))
8926 == reversed_comparison_code (cond1, NULL))
8927 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
8928 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
8929 && ! side_effects_p (x))
8931 *ptrue = *pfalse = const0_rtx;
8932 return cond0;
8937 else if (code == IF_THEN_ELSE)
8939 /* If we have IF_THEN_ELSE already, extract the condition and
8940 canonicalize it if it is NE or EQ. */
8941 cond0 = XEXP (x, 0);
8942 *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
8943 if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
8944 return XEXP (cond0, 0);
8945 else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
8947 *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
8948 return XEXP (cond0, 0);
8950 else
8951 return cond0;
8954 /* If X is a SUBREG, we can narrow both the true and false values
8955 if the inner expression, if there is a condition. */
8956 else if (code == SUBREG
8957 && 0 != (cond0 = if_then_else_cond (SUBREG_REG (x),
8958 &true0, &false0)))
8960 true0 = simplify_gen_subreg (mode, true0,
8961 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
8962 false0 = simplify_gen_subreg (mode, false0,
8963 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
8964 if (true0 && false0)
8966 *ptrue = true0;
8967 *pfalse = false0;
8968 return cond0;
8972 /* If X is a constant, this isn't special and will cause confusions
8973 if we treat it as such. Likewise if it is equivalent to a constant. */
8974 else if (CONSTANT_P (x)
8975 || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
8978 /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
8979 will be least confusing to the rest of the compiler. */
8980 else if (mode == BImode)
8982 *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
8983 return x;
8986 /* If X is known to be either 0 or -1, those are the true and
8987 false values when testing X. */
8988 else if (x == constm1_rtx || x == const0_rtx
8989 || (mode != VOIDmode
8990 && num_sign_bit_copies (x, mode) == GET_MODE_PRECISION (mode)))
8992 *ptrue = constm1_rtx, *pfalse = const0_rtx;
8993 return x;
8996 /* Likewise for 0 or a single bit. */
8997 else if (HWI_COMPUTABLE_MODE_P (mode)
8998 && exact_log2 (nz = nonzero_bits (x, mode)) >= 0)
9000 *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
9001 return x;
9004 /* Otherwise fail; show no condition with true and false values the same. */
9005 *ptrue = *pfalse = x;
9006 return 0;
9009 /* Return the value of expression X given the fact that condition COND
9010 is known to be true when applied to REG as its first operand and VAL
9011 as its second. X is known to not be shared and so can be modified in
9012 place.
9014 We only handle the simplest cases, and specifically those cases that
9015 arise with IF_THEN_ELSE expressions. */
9017 static rtx
9018 known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
9020 enum rtx_code code = GET_CODE (x);
9021 rtx temp;
9022 const char *fmt;
9023 int i, j;
9025 if (side_effects_p (x))
9026 return x;
9028 /* If either operand of the condition is a floating point value,
9029 then we have to avoid collapsing an EQ comparison. */
9030 if (cond == EQ
9031 && rtx_equal_p (x, reg)
9032 && ! FLOAT_MODE_P (GET_MODE (x))
9033 && ! FLOAT_MODE_P (GET_MODE (val)))
9034 return val;
9036 if (cond == UNEQ && rtx_equal_p (x, reg))
9037 return val;
9039 /* If X is (abs REG) and we know something about REG's relationship
9040 with zero, we may be able to simplify this. */
9042 if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
9043 switch (cond)
9045 case GE: case GT: case EQ:
9046 return XEXP (x, 0);
9047 case LT: case LE:
9048 return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
9049 XEXP (x, 0),
9050 GET_MODE (XEXP (x, 0)));
9051 default:
9052 break;
9055 /* The only other cases we handle are MIN, MAX, and comparisons if the
9056 operands are the same as REG and VAL. */
9058 else if (COMPARISON_P (x) || COMMUTATIVE_ARITH_P (x))
9060 if (rtx_equal_p (XEXP (x, 0), val))
9061 cond = swap_condition (cond), temp = val, val = reg, reg = temp;
9063 if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
9065 if (COMPARISON_P (x))
9067 if (comparison_dominates_p (cond, code))
9068 return const_true_rtx;
9070 code = reversed_comparison_code (x, NULL);
9071 if (code != UNKNOWN
9072 && comparison_dominates_p (cond, code))
9073 return const0_rtx;
9074 else
9075 return x;
9077 else if (code == SMAX || code == SMIN
9078 || code == UMIN || code == UMAX)
9080 int unsignedp = (code == UMIN || code == UMAX);
9082 /* Do not reverse the condition when it is NE or EQ.
9083 This is because we cannot conclude anything about
9084 the value of 'SMAX (x, y)' when x is not equal to y,
9085 but we can when x equals y. */
9086 if ((code == SMAX || code == UMAX)
9087 && ! (cond == EQ || cond == NE))
9088 cond = reverse_condition (cond);
9090 switch (cond)
9092 case GE: case GT:
9093 return unsignedp ? x : XEXP (x, 1);
9094 case LE: case LT:
9095 return unsignedp ? x : XEXP (x, 0);
9096 case GEU: case GTU:
9097 return unsignedp ? XEXP (x, 1) : x;
9098 case LEU: case LTU:
9099 return unsignedp ? XEXP (x, 0) : x;
9100 default:
9101 break;
9106 else if (code == SUBREG)
9108 machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
9109 rtx new_rtx, r = known_cond (SUBREG_REG (x), cond, reg, val);
9111 if (SUBREG_REG (x) != r)
9113 /* We must simplify subreg here, before we lose track of the
9114 original inner_mode. */
9115 new_rtx = simplify_subreg (GET_MODE (x), r,
9116 inner_mode, SUBREG_BYTE (x));
9117 if (new_rtx)
9118 return new_rtx;
9119 else
9120 SUBST (SUBREG_REG (x), r);
9123 return x;
9125 /* We don't have to handle SIGN_EXTEND here, because even in the
9126 case of replacing something with a modeless CONST_INT, a
9127 CONST_INT is already (supposed to be) a valid sign extension for
9128 its narrower mode, which implies it's already properly
9129 sign-extended for the wider mode. Now, for ZERO_EXTEND, the
9130 story is different. */
9131 else if (code == ZERO_EXTEND)
9133 machine_mode inner_mode = GET_MODE (XEXP (x, 0));
9134 rtx new_rtx, r = known_cond (XEXP (x, 0), cond, reg, val);
9136 if (XEXP (x, 0) != r)
9138 /* We must simplify the zero_extend here, before we lose
9139 track of the original inner_mode. */
9140 new_rtx = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
9141 r, inner_mode);
9142 if (new_rtx)
9143 return new_rtx;
9144 else
9145 SUBST (XEXP (x, 0), r);
9148 return x;
9151 fmt = GET_RTX_FORMAT (code);
9152 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
9154 if (fmt[i] == 'e')
9155 SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
9156 else if (fmt[i] == 'E')
9157 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9158 SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
9159 cond, reg, val));
9162 return x;
9165 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
9166 assignment as a field assignment. */
9168 static int
9169 rtx_equal_for_field_assignment_p (rtx x, rtx y)
9171 if (x == y || rtx_equal_p (x, y))
9172 return 1;
9174 if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
9175 return 0;
9177 /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
9178 Note that all SUBREGs of MEM are paradoxical; otherwise they
9179 would have been rewritten. */
9180 if (MEM_P (x) && GET_CODE (y) == SUBREG
9181 && MEM_P (SUBREG_REG (y))
9182 && rtx_equal_p (SUBREG_REG (y),
9183 gen_lowpart (GET_MODE (SUBREG_REG (y)), x)))
9184 return 1;
9186 if (MEM_P (y) && GET_CODE (x) == SUBREG
9187 && MEM_P (SUBREG_REG (x))
9188 && rtx_equal_p (SUBREG_REG (x),
9189 gen_lowpart (GET_MODE (SUBREG_REG (x)), y)))
9190 return 1;
9192 /* We used to see if get_last_value of X and Y were the same but that's
9193 not correct. In one direction, we'll cause the assignment to have
9194 the wrong destination and in the case, we'll import a register into this
9195 insn that might have already have been dead. So fail if none of the
9196 above cases are true. */
9197 return 0;
9200 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
9201 Return that assignment if so.
9203 We only handle the most common cases. */
9205 static rtx
9206 make_field_assignment (rtx x)
9208 rtx dest = SET_DEST (x);
9209 rtx src = SET_SRC (x);
9210 rtx assign;
9211 rtx rhs, lhs;
9212 HOST_WIDE_INT c1;
9213 HOST_WIDE_INT pos;
9214 unsigned HOST_WIDE_INT len;
9215 rtx other;
9216 machine_mode mode;
9218 /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
9219 a clear of a one-bit field. We will have changed it to
9220 (and (rotate (const_int -2) POS) DEST), so check for that. Also check
9221 for a SUBREG. */
9223 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
9224 && CONST_INT_P (XEXP (XEXP (src, 0), 0))
9225 && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
9226 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9228 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
9229 1, 1, 1, 0);
9230 if (assign != 0)
9231 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
9232 return x;
9235 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
9236 && subreg_lowpart_p (XEXP (src, 0))
9237 && (GET_MODE_SIZE (GET_MODE (XEXP (src, 0)))
9238 < GET_MODE_SIZE (GET_MODE (SUBREG_REG (XEXP (src, 0)))))
9239 && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
9240 && CONST_INT_P (XEXP (SUBREG_REG (XEXP (src, 0)), 0))
9241 && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
9242 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9244 assign = make_extraction (VOIDmode, dest, 0,
9245 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
9246 1, 1, 1, 0);
9247 if (assign != 0)
9248 return gen_rtx_SET (VOIDmode, assign, const0_rtx);
9249 return x;
9252 /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
9253 one-bit field. */
9254 if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
9255 && XEXP (XEXP (src, 0), 0) == const1_rtx
9256 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9258 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
9259 1, 1, 1, 0);
9260 if (assign != 0)
9261 return gen_rtx_SET (VOIDmode, assign, const1_rtx);
9262 return x;
9265 /* If DEST is already a field assignment, i.e. ZERO_EXTRACT, and the
9266 SRC is an AND with all bits of that field set, then we can discard
9267 the AND. */
9268 if (GET_CODE (dest) == ZERO_EXTRACT
9269 && CONST_INT_P (XEXP (dest, 1))
9270 && GET_CODE (src) == AND
9271 && CONST_INT_P (XEXP (src, 1)))
9273 HOST_WIDE_INT width = INTVAL (XEXP (dest, 1));
9274 unsigned HOST_WIDE_INT and_mask = INTVAL (XEXP (src, 1));
9275 unsigned HOST_WIDE_INT ze_mask;
9277 if (width >= HOST_BITS_PER_WIDE_INT)
9278 ze_mask = -1;
9279 else
9280 ze_mask = ((unsigned HOST_WIDE_INT)1 << width) - 1;
9282 /* Complete overlap. We can remove the source AND. */
9283 if ((and_mask & ze_mask) == ze_mask)
9284 return gen_rtx_SET (VOIDmode, dest, XEXP (src, 0));
9286 /* Partial overlap. We can reduce the source AND. */
9287 if ((and_mask & ze_mask) != and_mask)
9289 mode = GET_MODE (src);
9290 src = gen_rtx_AND (mode, XEXP (src, 0),
9291 gen_int_mode (and_mask & ze_mask, mode));
9292 return gen_rtx_SET (VOIDmode, dest, src);
9296 /* The other case we handle is assignments into a constant-position
9297 field. They look like (ior/xor (and DEST C1) OTHER). If C1 represents
9298 a mask that has all one bits except for a group of zero bits and
9299 OTHER is known to have zeros where C1 has ones, this is such an
9300 assignment. Compute the position and length from C1. Shift OTHER
9301 to the appropriate position, force it to the required mode, and
9302 make the extraction. Check for the AND in both operands. */
9304 /* One or more SUBREGs might obscure the constant-position field
9305 assignment. The first one we are likely to encounter is an outer
9306 narrowing SUBREG, which we can just strip for the purposes of
9307 identifying the constant-field assignment. */
9308 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src))
9309 src = SUBREG_REG (src);
9311 if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
9312 return x;
9314 rhs = expand_compound_operation (XEXP (src, 0));
9315 lhs = expand_compound_operation (XEXP (src, 1));
9317 if (GET_CODE (rhs) == AND
9318 && CONST_INT_P (XEXP (rhs, 1))
9319 && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
9320 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
9321 /* The second SUBREG that might get in the way is a paradoxical
9322 SUBREG around the first operand of the AND. We want to
9323 pretend the operand is as wide as the destination here. We
9324 do this by creating a new MEM in the wider mode for the sole
9325 purpose of the call to rtx_equal_for_field_assignment_p. Also
9326 note this trick only works for MEMs. */
9327 else if (GET_CODE (rhs) == AND
9328 && paradoxical_subreg_p (XEXP (rhs, 0))
9329 && GET_CODE (SUBREG_REG (XEXP (rhs, 0))) == MEM
9330 && CONST_INT_P (XEXP (rhs, 1))
9331 && rtx_equal_for_field_assignment_p (gen_rtx_MEM (GET_MODE (dest),
9332 XEXP (SUBREG_REG (XEXP (rhs, 0)), 0)),
9333 dest))
9334 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
9335 else if (GET_CODE (lhs) == AND
9336 && CONST_INT_P (XEXP (lhs, 1))
9337 && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
9338 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
9339 /* The second SUBREG that might get in the way is a paradoxical
9340 SUBREG around the first operand of the AND. We want to
9341 pretend the operand is as wide as the destination here. We
9342 do this by creating a new MEM in the wider mode for the sole
9343 purpose of the call to rtx_equal_for_field_assignment_p. Also
9344 note this trick only works for MEMs. */
9345 else if (GET_CODE (lhs) == AND
9346 && paradoxical_subreg_p (XEXP (lhs, 0))
9347 && GET_CODE (SUBREG_REG (XEXP (lhs, 0))) == MEM
9348 && CONST_INT_P (XEXP (lhs, 1))
9349 && rtx_equal_for_field_assignment_p (gen_rtx_MEM (GET_MODE (dest),
9350 XEXP (SUBREG_REG (XEXP (lhs, 0)), 0)),
9351 dest))
9352 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
9353 else
9354 return x;
9356 pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (GET_MODE (dest)), &len);
9357 if (pos < 0 || pos + len > GET_MODE_PRECISION (GET_MODE (dest))
9358 || GET_MODE_PRECISION (GET_MODE (dest)) > HOST_BITS_PER_WIDE_INT
9359 || (c1 & nonzero_bits (other, GET_MODE (dest))) != 0)
9360 return x;
9362 assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
9363 if (assign == 0)
9364 return x;
9366 /* The mode to use for the source is the mode of the assignment, or of
9367 what is inside a possible STRICT_LOW_PART. */
9368 mode = (GET_CODE (assign) == STRICT_LOW_PART
9369 ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
9371 /* Shift OTHER right POS places and make it the source, restricting it
9372 to the proper length and mode. */
9374 src = canon_reg_for_combine (simplify_shift_const (NULL_RTX, LSHIFTRT,
9375 GET_MODE (src),
9376 other, pos),
9377 dest);
9378 src = force_to_mode (src, mode,
9379 GET_MODE_PRECISION (mode) >= HOST_BITS_PER_WIDE_INT
9380 ? ~(unsigned HOST_WIDE_INT) 0
9381 : ((unsigned HOST_WIDE_INT) 1 << len) - 1,
9384 /* If SRC is masked by an AND that does not make a difference in
9385 the value being stored, strip it. */
9386 if (GET_CODE (assign) == ZERO_EXTRACT
9387 && CONST_INT_P (XEXP (assign, 1))
9388 && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
9389 && GET_CODE (src) == AND
9390 && CONST_INT_P (XEXP (src, 1))
9391 && UINTVAL (XEXP (src, 1))
9392 == ((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (assign, 1))) - 1)
9393 src = XEXP (src, 0);
9395 return gen_rtx_SET (VOIDmode, assign, src);
9398 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
9399 if so. */
9401 static rtx
9402 apply_distributive_law (rtx x)
9404 enum rtx_code code = GET_CODE (x);
9405 enum rtx_code inner_code;
9406 rtx lhs, rhs, other;
9407 rtx tem;
9409 /* Distributivity is not true for floating point as it can change the
9410 value. So we don't do it unless -funsafe-math-optimizations. */
9411 if (FLOAT_MODE_P (GET_MODE (x))
9412 && ! flag_unsafe_math_optimizations)
9413 return x;
9415 /* The outer operation can only be one of the following: */
9416 if (code != IOR && code != AND && code != XOR
9417 && code != PLUS && code != MINUS)
9418 return x;
9420 lhs = XEXP (x, 0);
9421 rhs = XEXP (x, 1);
9423 /* If either operand is a primitive we can't do anything, so get out
9424 fast. */
9425 if (OBJECT_P (lhs) || OBJECT_P (rhs))
9426 return x;
9428 lhs = expand_compound_operation (lhs);
9429 rhs = expand_compound_operation (rhs);
9430 inner_code = GET_CODE (lhs);
9431 if (inner_code != GET_CODE (rhs))
9432 return x;
9434 /* See if the inner and outer operations distribute. */
9435 switch (inner_code)
9437 case LSHIFTRT:
9438 case ASHIFTRT:
9439 case AND:
9440 case IOR:
9441 /* These all distribute except over PLUS. */
9442 if (code == PLUS || code == MINUS)
9443 return x;
9444 break;
9446 case MULT:
9447 if (code != PLUS && code != MINUS)
9448 return x;
9449 break;
9451 case ASHIFT:
9452 /* This is also a multiply, so it distributes over everything. */
9453 break;
9455 /* This used to handle SUBREG, but this turned out to be counter-
9456 productive, since (subreg (op ...)) usually is not handled by
9457 insn patterns, and this "optimization" therefore transformed
9458 recognizable patterns into unrecognizable ones. Therefore the
9459 SUBREG case was removed from here.
9461 It is possible that distributing SUBREG over arithmetic operations
9462 leads to an intermediate result than can then be optimized further,
9463 e.g. by moving the outer SUBREG to the other side of a SET as done
9464 in simplify_set. This seems to have been the original intent of
9465 handling SUBREGs here.
9467 However, with current GCC this does not appear to actually happen,
9468 at least on major platforms. If some case is found where removing
9469 the SUBREG case here prevents follow-on optimizations, distributing
9470 SUBREGs ought to be re-added at that place, e.g. in simplify_set. */
9472 default:
9473 return x;
9476 /* Set LHS and RHS to the inner operands (A and B in the example
9477 above) and set OTHER to the common operand (C in the example).
9478 There is only one way to do this unless the inner operation is
9479 commutative. */
9480 if (COMMUTATIVE_ARITH_P (lhs)
9481 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
9482 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
9483 else if (COMMUTATIVE_ARITH_P (lhs)
9484 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
9485 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
9486 else if (COMMUTATIVE_ARITH_P (lhs)
9487 && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
9488 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
9489 else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
9490 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
9491 else
9492 return x;
9494 /* Form the new inner operation, seeing if it simplifies first. */
9495 tem = simplify_gen_binary (code, GET_MODE (x), lhs, rhs);
9497 /* There is one exception to the general way of distributing:
9498 (a | c) ^ (b | c) -> (a ^ b) & ~c */
9499 if (code == XOR && inner_code == IOR)
9501 inner_code = AND;
9502 other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
9505 /* We may be able to continuing distributing the result, so call
9506 ourselves recursively on the inner operation before forming the
9507 outer operation, which we return. */
9508 return simplify_gen_binary (inner_code, GET_MODE (x),
9509 apply_distributive_law (tem), other);
9512 /* See if X is of the form (* (+ A B) C), and if so convert to
9513 (+ (* A C) (* B C)) and try to simplify.
9515 Most of the time, this results in no change. However, if some of
9516 the operands are the same or inverses of each other, simplifications
9517 will result.
9519 For example, (and (ior A B) (not B)) can occur as the result of
9520 expanding a bit field assignment. When we apply the distributive
9521 law to this, we get (ior (and (A (not B))) (and (B (not B)))),
9522 which then simplifies to (and (A (not B))).
9524 Note that no checks happen on the validity of applying the inverse
9525 distributive law. This is pointless since we can do it in the
9526 few places where this routine is called.
9528 N is the index of the term that is decomposed (the arithmetic operation,
9529 i.e. (+ A B) in the first example above). !N is the index of the term that
9530 is distributed, i.e. of C in the first example above. */
9531 static rtx
9532 distribute_and_simplify_rtx (rtx x, int n)
9534 machine_mode mode;
9535 enum rtx_code outer_code, inner_code;
9536 rtx decomposed, distributed, inner_op0, inner_op1, new_op0, new_op1, tmp;
9538 /* Distributivity is not true for floating point as it can change the
9539 value. So we don't do it unless -funsafe-math-optimizations. */
9540 if (FLOAT_MODE_P (GET_MODE (x))
9541 && ! flag_unsafe_math_optimizations)
9542 return NULL_RTX;
9544 decomposed = XEXP (x, n);
9545 if (!ARITHMETIC_P (decomposed))
9546 return NULL_RTX;
9548 mode = GET_MODE (x);
9549 outer_code = GET_CODE (x);
9550 distributed = XEXP (x, !n);
9552 inner_code = GET_CODE (decomposed);
9553 inner_op0 = XEXP (decomposed, 0);
9554 inner_op1 = XEXP (decomposed, 1);
9556 /* Special case (and (xor B C) (not A)), which is equivalent to
9557 (xor (ior A B) (ior A C)) */
9558 if (outer_code == AND && inner_code == XOR && GET_CODE (distributed) == NOT)
9560 distributed = XEXP (distributed, 0);
9561 outer_code = IOR;
9564 if (n == 0)
9566 /* Distribute the second term. */
9567 new_op0 = simplify_gen_binary (outer_code, mode, inner_op0, distributed);
9568 new_op1 = simplify_gen_binary (outer_code, mode, inner_op1, distributed);
9570 else
9572 /* Distribute the first term. */
9573 new_op0 = simplify_gen_binary (outer_code, mode, distributed, inner_op0);
9574 new_op1 = simplify_gen_binary (outer_code, mode, distributed, inner_op1);
9577 tmp = apply_distributive_law (simplify_gen_binary (inner_code, mode,
9578 new_op0, new_op1));
9579 if (GET_CODE (tmp) != outer_code
9580 && (set_src_cost (tmp, optimize_this_for_speed_p)
9581 < set_src_cost (x, optimize_this_for_speed_p)))
9582 return tmp;
9584 return NULL_RTX;
9587 /* Simplify a logical `and' of VAROP with the constant CONSTOP, to be done
9588 in MODE. Return an equivalent form, if different from (and VAROP
9589 (const_int CONSTOP)). Otherwise, return NULL_RTX. */
9591 static rtx
9592 simplify_and_const_int_1 (machine_mode mode, rtx varop,
9593 unsigned HOST_WIDE_INT constop)
9595 unsigned HOST_WIDE_INT nonzero;
9596 unsigned HOST_WIDE_INT orig_constop;
9597 rtx orig_varop;
9598 int i;
9600 orig_varop = varop;
9601 orig_constop = constop;
9602 if (GET_CODE (varop) == CLOBBER)
9603 return NULL_RTX;
9605 /* Simplify VAROP knowing that we will be only looking at some of the
9606 bits in it.
9608 Note by passing in CONSTOP, we guarantee that the bits not set in
9609 CONSTOP are not significant and will never be examined. We must
9610 ensure that is the case by explicitly masking out those bits
9611 before returning. */
9612 varop = force_to_mode (varop, mode, constop, 0);
9614 /* If VAROP is a CLOBBER, we will fail so return it. */
9615 if (GET_CODE (varop) == CLOBBER)
9616 return varop;
9618 /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
9619 to VAROP and return the new constant. */
9620 if (CONST_INT_P (varop))
9621 return gen_int_mode (INTVAL (varop) & constop, mode);
9623 /* See what bits may be nonzero in VAROP. Unlike the general case of
9624 a call to nonzero_bits, here we don't care about bits outside
9625 MODE. */
9627 nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
9629 /* Turn off all bits in the constant that are known to already be zero.
9630 Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
9631 which is tested below. */
9633 constop &= nonzero;
9635 /* If we don't have any bits left, return zero. */
9636 if (constop == 0)
9637 return const0_rtx;
9639 /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
9640 a power of two, we can replace this with an ASHIFT. */
9641 if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
9642 && (i = exact_log2 (constop)) >= 0)
9643 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
9645 /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
9646 or XOR, then try to apply the distributive law. This may eliminate
9647 operations if either branch can be simplified because of the AND.
9648 It may also make some cases more complex, but those cases probably
9649 won't match a pattern either with or without this. */
9651 if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
9652 return
9653 gen_lowpart
9654 (mode,
9655 apply_distributive_law
9656 (simplify_gen_binary (GET_CODE (varop), GET_MODE (varop),
9657 simplify_and_const_int (NULL_RTX,
9658 GET_MODE (varop),
9659 XEXP (varop, 0),
9660 constop),
9661 simplify_and_const_int (NULL_RTX,
9662 GET_MODE (varop),
9663 XEXP (varop, 1),
9664 constop))));
9666 /* If VAROP is PLUS, and the constant is a mask of low bits, distribute
9667 the AND and see if one of the operands simplifies to zero. If so, we
9668 may eliminate it. */
9670 if (GET_CODE (varop) == PLUS
9671 && exact_log2 (constop + 1) >= 0)
9673 rtx o0, o1;
9675 o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
9676 o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
9677 if (o0 == const0_rtx)
9678 return o1;
9679 if (o1 == const0_rtx)
9680 return o0;
9683 /* Make a SUBREG if necessary. If we can't make it, fail. */
9684 varop = gen_lowpart (mode, varop);
9685 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
9686 return NULL_RTX;
9688 /* If we are only masking insignificant bits, return VAROP. */
9689 if (constop == nonzero)
9690 return varop;
9692 if (varop == orig_varop && constop == orig_constop)
9693 return NULL_RTX;
9695 /* Otherwise, return an AND. */
9696 return simplify_gen_binary (AND, mode, varop, gen_int_mode (constop, mode));
9700 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
9701 in MODE.
9703 Return an equivalent form, if different from X. Otherwise, return X. If
9704 X is zero, we are to always construct the equivalent form. */
9706 static rtx
9707 simplify_and_const_int (rtx x, machine_mode mode, rtx varop,
9708 unsigned HOST_WIDE_INT constop)
9710 rtx tem = simplify_and_const_int_1 (mode, varop, constop);
9711 if (tem)
9712 return tem;
9714 if (!x)
9715 x = simplify_gen_binary (AND, GET_MODE (varop), varop,
9716 gen_int_mode (constop, mode));
9717 if (GET_MODE (x) != mode)
9718 x = gen_lowpart (mode, x);
9719 return x;
9722 /* Given a REG, X, compute which bits in X can be nonzero.
9723 We don't care about bits outside of those defined in MODE.
9725 For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
9726 a shift, AND, or zero_extract, we can do better. */
9728 static rtx
9729 reg_nonzero_bits_for_combine (const_rtx x, machine_mode mode,
9730 const_rtx known_x ATTRIBUTE_UNUSED,
9731 machine_mode known_mode ATTRIBUTE_UNUSED,
9732 unsigned HOST_WIDE_INT known_ret ATTRIBUTE_UNUSED,
9733 unsigned HOST_WIDE_INT *nonzero)
9735 rtx tem;
9736 reg_stat_type *rsp;
9738 /* If X is a register whose nonzero bits value is current, use it.
9739 Otherwise, if X is a register whose value we can find, use that
9740 value. Otherwise, use the previously-computed global nonzero bits
9741 for this register. */
9743 rsp = &reg_stat[REGNO (x)];
9744 if (rsp->last_set_value != 0
9745 && (rsp->last_set_mode == mode
9746 || (GET_MODE_CLASS (rsp->last_set_mode) == MODE_INT
9747 && GET_MODE_CLASS (mode) == MODE_INT))
9748 && ((rsp->last_set_label >= label_tick_ebb_start
9749 && rsp->last_set_label < label_tick)
9750 || (rsp->last_set_label == label_tick
9751 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
9752 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
9753 && REG_N_SETS (REGNO (x)) == 1
9754 && !REGNO_REG_SET_P
9755 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
9756 REGNO (x)))))
9758 unsigned HOST_WIDE_INT mask = rsp->last_set_nonzero_bits;
9760 if (GET_MODE_PRECISION (rsp->last_set_mode) < GET_MODE_PRECISION (mode))
9761 /* We don't know anything about the upper bits. */
9762 mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (rsp->last_set_mode);
9764 *nonzero &= mask;
9765 return NULL;
9768 tem = get_last_value (x);
9770 if (tem)
9772 #ifdef SHORT_IMMEDIATES_SIGN_EXTEND
9773 /* If X is narrower than MODE and TEM is a non-negative
9774 constant that would appear negative in the mode of X,
9775 sign-extend it for use in reg_nonzero_bits because some
9776 machines (maybe most) will actually do the sign-extension
9777 and this is the conservative approach.
9779 ??? For 2.5, try to tighten up the MD files in this regard
9780 instead of this kludge. */
9782 if (GET_MODE_PRECISION (GET_MODE (x)) < GET_MODE_PRECISION (mode)
9783 && CONST_INT_P (tem)
9784 && INTVAL (tem) > 0
9785 && val_signbit_known_set_p (GET_MODE (x), INTVAL (tem)))
9786 tem = GEN_INT (INTVAL (tem) | ~GET_MODE_MASK (GET_MODE (x)));
9787 #endif
9788 return tem;
9790 else if (nonzero_sign_valid && rsp->nonzero_bits)
9792 unsigned HOST_WIDE_INT mask = rsp->nonzero_bits;
9794 if (GET_MODE_PRECISION (GET_MODE (x)) < GET_MODE_PRECISION (mode))
9795 /* We don't know anything about the upper bits. */
9796 mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (GET_MODE (x));
9798 *nonzero &= mask;
9801 return NULL;
9804 /* Return the number of bits at the high-order end of X that are known to
9805 be equal to the sign bit. X will be used in mode MODE; if MODE is
9806 VOIDmode, X will be used in its own mode. The returned value will always
9807 be between 1 and the number of bits in MODE. */
9809 static rtx
9810 reg_num_sign_bit_copies_for_combine (const_rtx x, machine_mode mode,
9811 const_rtx known_x ATTRIBUTE_UNUSED,
9812 machine_mode known_mode
9813 ATTRIBUTE_UNUSED,
9814 unsigned int known_ret ATTRIBUTE_UNUSED,
9815 unsigned int *result)
9817 rtx tem;
9818 reg_stat_type *rsp;
9820 rsp = &reg_stat[REGNO (x)];
9821 if (rsp->last_set_value != 0
9822 && rsp->last_set_mode == mode
9823 && ((rsp->last_set_label >= label_tick_ebb_start
9824 && rsp->last_set_label < label_tick)
9825 || (rsp->last_set_label == label_tick
9826 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
9827 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
9828 && REG_N_SETS (REGNO (x)) == 1
9829 && !REGNO_REG_SET_P
9830 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
9831 REGNO (x)))))
9833 *result = rsp->last_set_sign_bit_copies;
9834 return NULL;
9837 tem = get_last_value (x);
9838 if (tem != 0)
9839 return tem;
9841 if (nonzero_sign_valid && rsp->sign_bit_copies != 0
9842 && GET_MODE_PRECISION (GET_MODE (x)) == GET_MODE_PRECISION (mode))
9843 *result = rsp->sign_bit_copies;
9845 return NULL;
9848 /* Return the number of "extended" bits there are in X, when interpreted
9849 as a quantity in MODE whose signedness is indicated by UNSIGNEDP. For
9850 unsigned quantities, this is the number of high-order zero bits.
9851 For signed quantities, this is the number of copies of the sign bit
9852 minus 1. In both case, this function returns the number of "spare"
9853 bits. For example, if two quantities for which this function returns
9854 at least 1 are added, the addition is known not to overflow.
9856 This function will always return 0 unless called during combine, which
9857 implies that it must be called from a define_split. */
9859 unsigned int
9860 extended_count (const_rtx x, machine_mode mode, int unsignedp)
9862 if (nonzero_sign_valid == 0)
9863 return 0;
9865 return (unsignedp
9866 ? (HWI_COMPUTABLE_MODE_P (mode)
9867 ? (unsigned int) (GET_MODE_PRECISION (mode) - 1
9868 - floor_log2 (nonzero_bits (x, mode)))
9869 : 0)
9870 : num_sign_bit_copies (x, mode) - 1);
9873 /* This function is called from `simplify_shift_const' to merge two
9874 outer operations. Specifically, we have already found that we need
9875 to perform operation *POP0 with constant *PCONST0 at the outermost
9876 position. We would now like to also perform OP1 with constant CONST1
9877 (with *POP0 being done last).
9879 Return 1 if we can do the operation and update *POP0 and *PCONST0 with
9880 the resulting operation. *PCOMP_P is set to 1 if we would need to
9881 complement the innermost operand, otherwise it is unchanged.
9883 MODE is the mode in which the operation will be done. No bits outside
9884 the width of this mode matter. It is assumed that the width of this mode
9885 is smaller than or equal to HOST_BITS_PER_WIDE_INT.
9887 If *POP0 or OP1 are UNKNOWN, it means no operation is required. Only NEG, PLUS,
9888 IOR, XOR, and AND are supported. We may set *POP0 to SET if the proper
9889 result is simply *PCONST0.
9891 If the resulting operation cannot be expressed as one operation, we
9892 return 0 and do not change *POP0, *PCONST0, and *PCOMP_P. */
9894 static int
9895 merge_outer_ops (enum rtx_code *pop0, HOST_WIDE_INT *pconst0, enum rtx_code op1, HOST_WIDE_INT const1, machine_mode mode, int *pcomp_p)
9897 enum rtx_code op0 = *pop0;
9898 HOST_WIDE_INT const0 = *pconst0;
9900 const0 &= GET_MODE_MASK (mode);
9901 const1 &= GET_MODE_MASK (mode);
9903 /* If OP0 is an AND, clear unimportant bits in CONST1. */
9904 if (op0 == AND)
9905 const1 &= const0;
9907 /* If OP0 or OP1 is UNKNOWN, this is easy. Similarly if they are the same or
9908 if OP0 is SET. */
9910 if (op1 == UNKNOWN || op0 == SET)
9911 return 1;
9913 else if (op0 == UNKNOWN)
9914 op0 = op1, const0 = const1;
9916 else if (op0 == op1)
9918 switch (op0)
9920 case AND:
9921 const0 &= const1;
9922 break;
9923 case IOR:
9924 const0 |= const1;
9925 break;
9926 case XOR:
9927 const0 ^= const1;
9928 break;
9929 case PLUS:
9930 const0 += const1;
9931 break;
9932 case NEG:
9933 op0 = UNKNOWN;
9934 break;
9935 default:
9936 break;
9940 /* Otherwise, if either is a PLUS or NEG, we can't do anything. */
9941 else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
9942 return 0;
9944 /* If the two constants aren't the same, we can't do anything. The
9945 remaining six cases can all be done. */
9946 else if (const0 != const1)
9947 return 0;
9949 else
9950 switch (op0)
9952 case IOR:
9953 if (op1 == AND)
9954 /* (a & b) | b == b */
9955 op0 = SET;
9956 else /* op1 == XOR */
9957 /* (a ^ b) | b == a | b */
9959 break;
9961 case XOR:
9962 if (op1 == AND)
9963 /* (a & b) ^ b == (~a) & b */
9964 op0 = AND, *pcomp_p = 1;
9965 else /* op1 == IOR */
9966 /* (a | b) ^ b == a & ~b */
9967 op0 = AND, const0 = ~const0;
9968 break;
9970 case AND:
9971 if (op1 == IOR)
9972 /* (a | b) & b == b */
9973 op0 = SET;
9974 else /* op1 == XOR */
9975 /* (a ^ b) & b) == (~a) & b */
9976 *pcomp_p = 1;
9977 break;
9978 default:
9979 break;
9982 /* Check for NO-OP cases. */
9983 const0 &= GET_MODE_MASK (mode);
9984 if (const0 == 0
9985 && (op0 == IOR || op0 == XOR || op0 == PLUS))
9986 op0 = UNKNOWN;
9987 else if (const0 == 0 && op0 == AND)
9988 op0 = SET;
9989 else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
9990 && op0 == AND)
9991 op0 = UNKNOWN;
9993 *pop0 = op0;
9995 /* ??? Slightly redundant with the above mask, but not entirely.
9996 Moving this above means we'd have to sign-extend the mode mask
9997 for the final test. */
9998 if (op0 != UNKNOWN && op0 != NEG)
9999 *pconst0 = trunc_int_for_mode (const0, mode);
10001 return 1;
10004 /* A helper to simplify_shift_const_1 to determine the mode we can perform
10005 the shift in. The original shift operation CODE is performed on OP in
10006 ORIG_MODE. Return the wider mode MODE if we can perform the operation
10007 in that mode. Return ORIG_MODE otherwise. We can also assume that the
10008 result of the shift is subject to operation OUTER_CODE with operand
10009 OUTER_CONST. */
10011 static machine_mode
10012 try_widen_shift_mode (enum rtx_code code, rtx op, int count,
10013 machine_mode orig_mode, machine_mode mode,
10014 enum rtx_code outer_code, HOST_WIDE_INT outer_const)
10016 if (orig_mode == mode)
10017 return mode;
10018 gcc_assert (GET_MODE_PRECISION (mode) > GET_MODE_PRECISION (orig_mode));
10020 /* In general we can't perform in wider mode for right shift and rotate. */
10021 switch (code)
10023 case ASHIFTRT:
10024 /* We can still widen if the bits brought in from the left are identical
10025 to the sign bit of ORIG_MODE. */
10026 if (num_sign_bit_copies (op, mode)
10027 > (unsigned) (GET_MODE_PRECISION (mode)
10028 - GET_MODE_PRECISION (orig_mode)))
10029 return mode;
10030 return orig_mode;
10032 case LSHIFTRT:
10033 /* Similarly here but with zero bits. */
10034 if (HWI_COMPUTABLE_MODE_P (mode)
10035 && (nonzero_bits (op, mode) & ~GET_MODE_MASK (orig_mode)) == 0)
10036 return mode;
10038 /* We can also widen if the bits brought in will be masked off. This
10039 operation is performed in ORIG_MODE. */
10040 if (outer_code == AND)
10042 int care_bits = low_bitmask_len (orig_mode, outer_const);
10044 if (care_bits >= 0
10045 && GET_MODE_PRECISION (orig_mode) - care_bits >= count)
10046 return mode;
10048 /* fall through */
10050 case ROTATE:
10051 return orig_mode;
10053 case ROTATERT:
10054 gcc_unreachable ();
10056 default:
10057 return mode;
10061 /* Simplify a shift of VAROP by ORIG_COUNT bits. CODE says what kind
10062 of shift. The result of the shift is RESULT_MODE. Return NULL_RTX
10063 if we cannot simplify it. Otherwise, return a simplified value.
10065 The shift is normally computed in the widest mode we find in VAROP, as
10066 long as it isn't a different number of words than RESULT_MODE. Exceptions
10067 are ASHIFTRT and ROTATE, which are always done in their original mode. */
10069 static rtx
10070 simplify_shift_const_1 (enum rtx_code code, machine_mode result_mode,
10071 rtx varop, int orig_count)
10073 enum rtx_code orig_code = code;
10074 rtx orig_varop = varop;
10075 int count;
10076 machine_mode mode = result_mode;
10077 machine_mode shift_mode, tmode;
10078 unsigned int mode_words
10079 = (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
10080 /* We form (outer_op (code varop count) (outer_const)). */
10081 enum rtx_code outer_op = UNKNOWN;
10082 HOST_WIDE_INT outer_const = 0;
10083 int complement_p = 0;
10084 rtx new_rtx, x;
10086 /* Make sure and truncate the "natural" shift on the way in. We don't
10087 want to do this inside the loop as it makes it more difficult to
10088 combine shifts. */
10089 if (SHIFT_COUNT_TRUNCATED)
10090 orig_count &= GET_MODE_BITSIZE (mode) - 1;
10092 /* If we were given an invalid count, don't do anything except exactly
10093 what was requested. */
10095 if (orig_count < 0 || orig_count >= (int) GET_MODE_PRECISION (mode))
10096 return NULL_RTX;
10098 count = orig_count;
10100 /* Unless one of the branches of the `if' in this loop does a `continue',
10101 we will `break' the loop after the `if'. */
10103 while (count != 0)
10105 /* If we have an operand of (clobber (const_int 0)), fail. */
10106 if (GET_CODE (varop) == CLOBBER)
10107 return NULL_RTX;
10109 /* Convert ROTATERT to ROTATE. */
10110 if (code == ROTATERT)
10112 unsigned int bitsize = GET_MODE_PRECISION (result_mode);
10113 code = ROTATE;
10114 if (VECTOR_MODE_P (result_mode))
10115 count = bitsize / GET_MODE_NUNITS (result_mode) - count;
10116 else
10117 count = bitsize - count;
10120 shift_mode = try_widen_shift_mode (code, varop, count, result_mode,
10121 mode, outer_op, outer_const);
10123 /* Handle cases where the count is greater than the size of the mode
10124 minus 1. For ASHIFT, use the size minus one as the count (this can
10125 occur when simplifying (lshiftrt (ashiftrt ..))). For rotates,
10126 take the count modulo the size. For other shifts, the result is
10127 zero.
10129 Since these shifts are being produced by the compiler by combining
10130 multiple operations, each of which are defined, we know what the
10131 result is supposed to be. */
10133 if (count > (GET_MODE_PRECISION (shift_mode) - 1))
10135 if (code == ASHIFTRT)
10136 count = GET_MODE_PRECISION (shift_mode) - 1;
10137 else if (code == ROTATE || code == ROTATERT)
10138 count %= GET_MODE_PRECISION (shift_mode);
10139 else
10141 /* We can't simply return zero because there may be an
10142 outer op. */
10143 varop = const0_rtx;
10144 count = 0;
10145 break;
10149 /* If we discovered we had to complement VAROP, leave. Making a NOT
10150 here would cause an infinite loop. */
10151 if (complement_p)
10152 break;
10154 /* An arithmetic right shift of a quantity known to be -1 or 0
10155 is a no-op. */
10156 if (code == ASHIFTRT
10157 && (num_sign_bit_copies (varop, shift_mode)
10158 == GET_MODE_PRECISION (shift_mode)))
10160 count = 0;
10161 break;
10164 /* If we are doing an arithmetic right shift and discarding all but
10165 the sign bit copies, this is equivalent to doing a shift by the
10166 bitsize minus one. Convert it into that shift because it will often
10167 allow other simplifications. */
10169 if (code == ASHIFTRT
10170 && (count + num_sign_bit_copies (varop, shift_mode)
10171 >= GET_MODE_PRECISION (shift_mode)))
10172 count = GET_MODE_PRECISION (shift_mode) - 1;
10174 /* We simplify the tests below and elsewhere by converting
10175 ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
10176 `make_compound_operation' will convert it to an ASHIFTRT for
10177 those machines (such as VAX) that don't have an LSHIFTRT. */
10178 if (code == ASHIFTRT
10179 && val_signbit_known_clear_p (shift_mode,
10180 nonzero_bits (varop, shift_mode)))
10181 code = LSHIFTRT;
10183 if (((code == LSHIFTRT
10184 && HWI_COMPUTABLE_MODE_P (shift_mode)
10185 && !(nonzero_bits (varop, shift_mode) >> count))
10186 || (code == ASHIFT
10187 && HWI_COMPUTABLE_MODE_P (shift_mode)
10188 && !((nonzero_bits (varop, shift_mode) << count)
10189 & GET_MODE_MASK (shift_mode))))
10190 && !side_effects_p (varop))
10191 varop = const0_rtx;
10193 switch (GET_CODE (varop))
10195 case SIGN_EXTEND:
10196 case ZERO_EXTEND:
10197 case SIGN_EXTRACT:
10198 case ZERO_EXTRACT:
10199 new_rtx = expand_compound_operation (varop);
10200 if (new_rtx != varop)
10202 varop = new_rtx;
10203 continue;
10205 break;
10207 case MEM:
10208 /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
10209 minus the width of a smaller mode, we can do this with a
10210 SIGN_EXTEND or ZERO_EXTEND from the narrower memory location. */
10211 if ((code == ASHIFTRT || code == LSHIFTRT)
10212 && ! mode_dependent_address_p (XEXP (varop, 0),
10213 MEM_ADDR_SPACE (varop))
10214 && ! MEM_VOLATILE_P (varop)
10215 && (tmode = mode_for_size (GET_MODE_BITSIZE (mode) - count,
10216 MODE_INT, 1)) != BLKmode)
10218 new_rtx = adjust_address_nv (varop, tmode,
10219 BYTES_BIG_ENDIAN ? 0
10220 : count / BITS_PER_UNIT);
10222 varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
10223 : ZERO_EXTEND, mode, new_rtx);
10224 count = 0;
10225 continue;
10227 break;
10229 case SUBREG:
10230 /* If VAROP is a SUBREG, strip it as long as the inner operand has
10231 the same number of words as what we've seen so far. Then store
10232 the widest mode in MODE. */
10233 if (subreg_lowpart_p (varop)
10234 && (GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
10235 > GET_MODE_SIZE (GET_MODE (varop)))
10236 && (unsigned int) ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (varop)))
10237 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
10238 == mode_words
10239 && GET_MODE_CLASS (GET_MODE (varop)) == MODE_INT
10240 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (varop))) == MODE_INT)
10242 varop = SUBREG_REG (varop);
10243 if (GET_MODE_SIZE (GET_MODE (varop)) > GET_MODE_SIZE (mode))
10244 mode = GET_MODE (varop);
10245 continue;
10247 break;
10249 case MULT:
10250 /* Some machines use MULT instead of ASHIFT because MULT
10251 is cheaper. But it is still better on those machines to
10252 merge two shifts into one. */
10253 if (CONST_INT_P (XEXP (varop, 1))
10254 && exact_log2 (UINTVAL (XEXP (varop, 1))) >= 0)
10256 varop
10257 = simplify_gen_binary (ASHIFT, GET_MODE (varop),
10258 XEXP (varop, 0),
10259 GEN_INT (exact_log2 (
10260 UINTVAL (XEXP (varop, 1)))));
10261 continue;
10263 break;
10265 case UDIV:
10266 /* Similar, for when divides are cheaper. */
10267 if (CONST_INT_P (XEXP (varop, 1))
10268 && exact_log2 (UINTVAL (XEXP (varop, 1))) >= 0)
10270 varop
10271 = simplify_gen_binary (LSHIFTRT, GET_MODE (varop),
10272 XEXP (varop, 0),
10273 GEN_INT (exact_log2 (
10274 UINTVAL (XEXP (varop, 1)))));
10275 continue;
10277 break;
10279 case ASHIFTRT:
10280 /* If we are extracting just the sign bit of an arithmetic
10281 right shift, that shift is not needed. However, the sign
10282 bit of a wider mode may be different from what would be
10283 interpreted as the sign bit in a narrower mode, so, if
10284 the result is narrower, don't discard the shift. */
10285 if (code == LSHIFTRT
10286 && count == (GET_MODE_BITSIZE (result_mode) - 1)
10287 && (GET_MODE_BITSIZE (result_mode)
10288 >= GET_MODE_BITSIZE (GET_MODE (varop))))
10290 varop = XEXP (varop, 0);
10291 continue;
10294 /* ... fall through ... */
10296 case LSHIFTRT:
10297 case ASHIFT:
10298 case ROTATE:
10299 /* Here we have two nested shifts. The result is usually the
10300 AND of a new shift with a mask. We compute the result below. */
10301 if (CONST_INT_P (XEXP (varop, 1))
10302 && INTVAL (XEXP (varop, 1)) >= 0
10303 && INTVAL (XEXP (varop, 1)) < GET_MODE_PRECISION (GET_MODE (varop))
10304 && HWI_COMPUTABLE_MODE_P (result_mode)
10305 && HWI_COMPUTABLE_MODE_P (mode)
10306 && !VECTOR_MODE_P (result_mode))
10308 enum rtx_code first_code = GET_CODE (varop);
10309 unsigned int first_count = INTVAL (XEXP (varop, 1));
10310 unsigned HOST_WIDE_INT mask;
10311 rtx mask_rtx;
10313 /* We have one common special case. We can't do any merging if
10314 the inner code is an ASHIFTRT of a smaller mode. However, if
10315 we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
10316 with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
10317 we can convert it to
10318 (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0) C3) C2) C1).
10319 This simplifies certain SIGN_EXTEND operations. */
10320 if (code == ASHIFT && first_code == ASHIFTRT
10321 && count == (GET_MODE_PRECISION (result_mode)
10322 - GET_MODE_PRECISION (GET_MODE (varop))))
10324 /* C3 has the low-order C1 bits zero. */
10326 mask = GET_MODE_MASK (mode)
10327 & ~(((unsigned HOST_WIDE_INT) 1 << first_count) - 1);
10329 varop = simplify_and_const_int (NULL_RTX, result_mode,
10330 XEXP (varop, 0), mask);
10331 varop = simplify_shift_const (NULL_RTX, ASHIFT, result_mode,
10332 varop, count);
10333 count = first_count;
10334 code = ASHIFTRT;
10335 continue;
10338 /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
10339 than C1 high-order bits equal to the sign bit, we can convert
10340 this to either an ASHIFT or an ASHIFTRT depending on the
10341 two counts.
10343 We cannot do this if VAROP's mode is not SHIFT_MODE. */
10345 if (code == ASHIFTRT && first_code == ASHIFT
10346 && GET_MODE (varop) == shift_mode
10347 && (num_sign_bit_copies (XEXP (varop, 0), shift_mode)
10348 > first_count))
10350 varop = XEXP (varop, 0);
10351 count -= first_count;
10352 if (count < 0)
10354 count = -count;
10355 code = ASHIFT;
10358 continue;
10361 /* There are some cases we can't do. If CODE is ASHIFTRT,
10362 we can only do this if FIRST_CODE is also ASHIFTRT.
10364 We can't do the case when CODE is ROTATE and FIRST_CODE is
10365 ASHIFTRT.
10367 If the mode of this shift is not the mode of the outer shift,
10368 we can't do this if either shift is a right shift or ROTATE.
10370 Finally, we can't do any of these if the mode is too wide
10371 unless the codes are the same.
10373 Handle the case where the shift codes are the same
10374 first. */
10376 if (code == first_code)
10378 if (GET_MODE (varop) != result_mode
10379 && (code == ASHIFTRT || code == LSHIFTRT
10380 || code == ROTATE))
10381 break;
10383 count += first_count;
10384 varop = XEXP (varop, 0);
10385 continue;
10388 if (code == ASHIFTRT
10389 || (code == ROTATE && first_code == ASHIFTRT)
10390 || GET_MODE_PRECISION (mode) > HOST_BITS_PER_WIDE_INT
10391 || (GET_MODE (varop) != result_mode
10392 && (first_code == ASHIFTRT || first_code == LSHIFTRT
10393 || first_code == ROTATE
10394 || code == ROTATE)))
10395 break;
10397 /* To compute the mask to apply after the shift, shift the
10398 nonzero bits of the inner shift the same way the
10399 outer shift will. */
10401 mask_rtx = gen_int_mode (nonzero_bits (varop, GET_MODE (varop)),
10402 result_mode);
10404 mask_rtx
10405 = simplify_const_binary_operation (code, result_mode, mask_rtx,
10406 GEN_INT (count));
10408 /* Give up if we can't compute an outer operation to use. */
10409 if (mask_rtx == 0
10410 || !CONST_INT_P (mask_rtx)
10411 || ! merge_outer_ops (&outer_op, &outer_const, AND,
10412 INTVAL (mask_rtx),
10413 result_mode, &complement_p))
10414 break;
10416 /* If the shifts are in the same direction, we add the
10417 counts. Otherwise, we subtract them. */
10418 if ((code == ASHIFTRT || code == LSHIFTRT)
10419 == (first_code == ASHIFTRT || first_code == LSHIFTRT))
10420 count += first_count;
10421 else
10422 count -= first_count;
10424 /* If COUNT is positive, the new shift is usually CODE,
10425 except for the two exceptions below, in which case it is
10426 FIRST_CODE. If the count is negative, FIRST_CODE should
10427 always be used */
10428 if (count > 0
10429 && ((first_code == ROTATE && code == ASHIFT)
10430 || (first_code == ASHIFTRT && code == LSHIFTRT)))
10431 code = first_code;
10432 else if (count < 0)
10433 code = first_code, count = -count;
10435 varop = XEXP (varop, 0);
10436 continue;
10439 /* If we have (A << B << C) for any shift, we can convert this to
10440 (A << C << B). This wins if A is a constant. Only try this if
10441 B is not a constant. */
10443 else if (GET_CODE (varop) == code
10444 && CONST_INT_P (XEXP (varop, 0))
10445 && !CONST_INT_P (XEXP (varop, 1)))
10447 rtx new_rtx = simplify_const_binary_operation (code, mode,
10448 XEXP (varop, 0),
10449 GEN_INT (count));
10450 varop = gen_rtx_fmt_ee (code, mode, new_rtx, XEXP (varop, 1));
10451 count = 0;
10452 continue;
10454 break;
10456 case NOT:
10457 if (VECTOR_MODE_P (mode))
10458 break;
10460 /* Make this fit the case below. */
10461 varop = gen_rtx_XOR (mode, XEXP (varop, 0), constm1_rtx);
10462 continue;
10464 case IOR:
10465 case AND:
10466 case XOR:
10467 /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
10468 with C the size of VAROP - 1 and the shift is logical if
10469 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
10470 we have an (le X 0) operation. If we have an arithmetic shift
10471 and STORE_FLAG_VALUE is 1 or we have a logical shift with
10472 STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation. */
10474 if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
10475 && XEXP (XEXP (varop, 0), 1) == constm1_rtx
10476 && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
10477 && (code == LSHIFTRT || code == ASHIFTRT)
10478 && count == (GET_MODE_PRECISION (GET_MODE (varop)) - 1)
10479 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
10481 count = 0;
10482 varop = gen_rtx_LE (GET_MODE (varop), XEXP (varop, 1),
10483 const0_rtx);
10485 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
10486 varop = gen_rtx_NEG (GET_MODE (varop), varop);
10488 continue;
10491 /* If we have (shift (logical)), move the logical to the outside
10492 to allow it to possibly combine with another logical and the
10493 shift to combine with another shift. This also canonicalizes to
10494 what a ZERO_EXTRACT looks like. Also, some machines have
10495 (and (shift)) insns. */
10497 if (CONST_INT_P (XEXP (varop, 1))
10498 /* We can't do this if we have (ashiftrt (xor)) and the
10499 constant has its sign bit set in shift_mode with shift_mode
10500 wider than result_mode. */
10501 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
10502 && result_mode != shift_mode
10503 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
10504 shift_mode))
10505 && (new_rtx = simplify_const_binary_operation
10506 (code, result_mode,
10507 gen_int_mode (INTVAL (XEXP (varop, 1)), result_mode),
10508 GEN_INT (count))) != 0
10509 && CONST_INT_P (new_rtx)
10510 && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
10511 INTVAL (new_rtx), result_mode, &complement_p))
10513 varop = XEXP (varop, 0);
10514 continue;
10517 /* If we can't do that, try to simplify the shift in each arm of the
10518 logical expression, make a new logical expression, and apply
10519 the inverse distributive law. This also can't be done for
10520 (ashiftrt (xor)) where we've widened the shift and the constant
10521 changes the sign bit. */
10522 if (CONST_INT_P (XEXP (varop, 1))
10523 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
10524 && result_mode != shift_mode
10525 && 0 > trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
10526 shift_mode)))
10528 rtx lhs = simplify_shift_const (NULL_RTX, code, shift_mode,
10529 XEXP (varop, 0), count);
10530 rtx rhs = simplify_shift_const (NULL_RTX, code, shift_mode,
10531 XEXP (varop, 1), count);
10533 varop = simplify_gen_binary (GET_CODE (varop), shift_mode,
10534 lhs, rhs);
10535 varop = apply_distributive_law (varop);
10537 count = 0;
10538 continue;
10540 break;
10542 case EQ:
10543 /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
10544 says that the sign bit can be tested, FOO has mode MODE, C is
10545 GET_MODE_PRECISION (MODE) - 1, and FOO has only its low-order bit
10546 that may be nonzero. */
10547 if (code == LSHIFTRT
10548 && XEXP (varop, 1) == const0_rtx
10549 && GET_MODE (XEXP (varop, 0)) == result_mode
10550 && count == (GET_MODE_PRECISION (result_mode) - 1)
10551 && HWI_COMPUTABLE_MODE_P (result_mode)
10552 && STORE_FLAG_VALUE == -1
10553 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
10554 && merge_outer_ops (&outer_op, &outer_const, XOR, 1, result_mode,
10555 &complement_p))
10557 varop = XEXP (varop, 0);
10558 count = 0;
10559 continue;
10561 break;
10563 case NEG:
10564 /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
10565 than the number of bits in the mode is equivalent to A. */
10566 if (code == LSHIFTRT
10567 && count == (GET_MODE_PRECISION (result_mode) - 1)
10568 && nonzero_bits (XEXP (varop, 0), result_mode) == 1)
10570 varop = XEXP (varop, 0);
10571 count = 0;
10572 continue;
10575 /* NEG commutes with ASHIFT since it is multiplication. Move the
10576 NEG outside to allow shifts to combine. */
10577 if (code == ASHIFT
10578 && merge_outer_ops (&outer_op, &outer_const, NEG, 0, result_mode,
10579 &complement_p))
10581 varop = XEXP (varop, 0);
10582 continue;
10584 break;
10586 case PLUS:
10587 /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
10588 is one less than the number of bits in the mode is
10589 equivalent to (xor A 1). */
10590 if (code == LSHIFTRT
10591 && count == (GET_MODE_PRECISION (result_mode) - 1)
10592 && XEXP (varop, 1) == constm1_rtx
10593 && nonzero_bits (XEXP (varop, 0), result_mode) == 1
10594 && merge_outer_ops (&outer_op, &outer_const, XOR, 1, result_mode,
10595 &complement_p))
10597 count = 0;
10598 varop = XEXP (varop, 0);
10599 continue;
10602 /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
10603 that might be nonzero in BAR are those being shifted out and those
10604 bits are known zero in FOO, we can replace the PLUS with FOO.
10605 Similarly in the other operand order. This code occurs when
10606 we are computing the size of a variable-size array. */
10608 if ((code == ASHIFTRT || code == LSHIFTRT)
10609 && count < HOST_BITS_PER_WIDE_INT
10610 && nonzero_bits (XEXP (varop, 1), result_mode) >> count == 0
10611 && (nonzero_bits (XEXP (varop, 1), result_mode)
10612 & nonzero_bits (XEXP (varop, 0), result_mode)) == 0)
10614 varop = XEXP (varop, 0);
10615 continue;
10617 else if ((code == ASHIFTRT || code == LSHIFTRT)
10618 && count < HOST_BITS_PER_WIDE_INT
10619 && HWI_COMPUTABLE_MODE_P (result_mode)
10620 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
10621 >> count)
10622 && 0 == (nonzero_bits (XEXP (varop, 0), result_mode)
10623 & nonzero_bits (XEXP (varop, 1),
10624 result_mode)))
10626 varop = XEXP (varop, 1);
10627 continue;
10630 /* (ashift (plus foo C) N) is (plus (ashift foo N) C'). */
10631 if (code == ASHIFT
10632 && CONST_INT_P (XEXP (varop, 1))
10633 && (new_rtx = simplify_const_binary_operation
10634 (ASHIFT, result_mode,
10635 gen_int_mode (INTVAL (XEXP (varop, 1)), result_mode),
10636 GEN_INT (count))) != 0
10637 && CONST_INT_P (new_rtx)
10638 && merge_outer_ops (&outer_op, &outer_const, PLUS,
10639 INTVAL (new_rtx), result_mode, &complement_p))
10641 varop = XEXP (varop, 0);
10642 continue;
10645 /* Check for 'PLUS signbit', which is the canonical form of 'XOR
10646 signbit', and attempt to change the PLUS to an XOR and move it to
10647 the outer operation as is done above in the AND/IOR/XOR case
10648 leg for shift(logical). See details in logical handling above
10649 for reasoning in doing so. */
10650 if (code == LSHIFTRT
10651 && CONST_INT_P (XEXP (varop, 1))
10652 && mode_signbit_p (result_mode, XEXP (varop, 1))
10653 && (new_rtx = simplify_const_binary_operation
10654 (code, result_mode,
10655 gen_int_mode (INTVAL (XEXP (varop, 1)), result_mode),
10656 GEN_INT (count))) != 0
10657 && CONST_INT_P (new_rtx)
10658 && merge_outer_ops (&outer_op, &outer_const, XOR,
10659 INTVAL (new_rtx), result_mode, &complement_p))
10661 varop = XEXP (varop, 0);
10662 continue;
10665 break;
10667 case MINUS:
10668 /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
10669 with C the size of VAROP - 1 and the shift is logical if
10670 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
10671 we have a (gt X 0) operation. If the shift is arithmetic with
10672 STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
10673 we have a (neg (gt X 0)) operation. */
10675 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
10676 && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
10677 && count == (GET_MODE_PRECISION (GET_MODE (varop)) - 1)
10678 && (code == LSHIFTRT || code == ASHIFTRT)
10679 && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
10680 && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
10681 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
10683 count = 0;
10684 varop = gen_rtx_GT (GET_MODE (varop), XEXP (varop, 1),
10685 const0_rtx);
10687 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
10688 varop = gen_rtx_NEG (GET_MODE (varop), varop);
10690 continue;
10692 break;
10694 case TRUNCATE:
10695 /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
10696 if the truncate does not affect the value. */
10697 if (code == LSHIFTRT
10698 && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
10699 && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
10700 && (INTVAL (XEXP (XEXP (varop, 0), 1))
10701 >= (GET_MODE_PRECISION (GET_MODE (XEXP (varop, 0)))
10702 - GET_MODE_PRECISION (GET_MODE (varop)))))
10704 rtx varop_inner = XEXP (varop, 0);
10706 varop_inner
10707 = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
10708 XEXP (varop_inner, 0),
10709 GEN_INT
10710 (count + INTVAL (XEXP (varop_inner, 1))));
10711 varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
10712 count = 0;
10713 continue;
10715 break;
10717 default:
10718 break;
10721 break;
10724 shift_mode = try_widen_shift_mode (code, varop, count, result_mode, mode,
10725 outer_op, outer_const);
10727 /* We have now finished analyzing the shift. The result should be
10728 a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places. If
10729 OUTER_OP is non-UNKNOWN, it is an operation that needs to be applied
10730 to the result of the shift. OUTER_CONST is the relevant constant,
10731 but we must turn off all bits turned off in the shift. */
10733 if (outer_op == UNKNOWN
10734 && orig_code == code && orig_count == count
10735 && varop == orig_varop
10736 && shift_mode == GET_MODE (varop))
10737 return NULL_RTX;
10739 /* Make a SUBREG if necessary. If we can't make it, fail. */
10740 varop = gen_lowpart (shift_mode, varop);
10741 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
10742 return NULL_RTX;
10744 /* If we have an outer operation and we just made a shift, it is
10745 possible that we could have simplified the shift were it not
10746 for the outer operation. So try to do the simplification
10747 recursively. */
10749 if (outer_op != UNKNOWN)
10750 x = simplify_shift_const_1 (code, shift_mode, varop, count);
10751 else
10752 x = NULL_RTX;
10754 if (x == NULL_RTX)
10755 x = simplify_gen_binary (code, shift_mode, varop, GEN_INT (count));
10757 /* If we were doing an LSHIFTRT in a wider mode than it was originally,
10758 turn off all the bits that the shift would have turned off. */
10759 if (orig_code == LSHIFTRT && result_mode != shift_mode)
10760 x = simplify_and_const_int (NULL_RTX, shift_mode, x,
10761 GET_MODE_MASK (result_mode) >> orig_count);
10763 /* Do the remainder of the processing in RESULT_MODE. */
10764 x = gen_lowpart_or_truncate (result_mode, x);
10766 /* If COMPLEMENT_P is set, we have to complement X before doing the outer
10767 operation. */
10768 if (complement_p)
10769 x = simplify_gen_unary (NOT, result_mode, x, result_mode);
10771 if (outer_op != UNKNOWN)
10773 if (GET_RTX_CLASS (outer_op) != RTX_UNARY
10774 && GET_MODE_PRECISION (result_mode) < HOST_BITS_PER_WIDE_INT)
10775 outer_const = trunc_int_for_mode (outer_const, result_mode);
10777 if (outer_op == AND)
10778 x = simplify_and_const_int (NULL_RTX, result_mode, x, outer_const);
10779 else if (outer_op == SET)
10781 /* This means that we have determined that the result is
10782 equivalent to a constant. This should be rare. */
10783 if (!side_effects_p (x))
10784 x = GEN_INT (outer_const);
10786 else if (GET_RTX_CLASS (outer_op) == RTX_UNARY)
10787 x = simplify_gen_unary (outer_op, result_mode, x, result_mode);
10788 else
10789 x = simplify_gen_binary (outer_op, result_mode, x,
10790 GEN_INT (outer_const));
10793 return x;
10796 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
10797 The result of the shift is RESULT_MODE. If we cannot simplify it,
10798 return X or, if it is NULL, synthesize the expression with
10799 simplify_gen_binary. Otherwise, return a simplified value.
10801 The shift is normally computed in the widest mode we find in VAROP, as
10802 long as it isn't a different number of words than RESULT_MODE. Exceptions
10803 are ASHIFTRT and ROTATE, which are always done in their original mode. */
10805 static rtx
10806 simplify_shift_const (rtx x, enum rtx_code code, machine_mode result_mode,
10807 rtx varop, int count)
10809 rtx tem = simplify_shift_const_1 (code, result_mode, varop, count);
10810 if (tem)
10811 return tem;
10813 if (!x)
10814 x = simplify_gen_binary (code, GET_MODE (varop), varop, GEN_INT (count));
10815 if (GET_MODE (x) != result_mode)
10816 x = gen_lowpart (result_mode, x);
10817 return x;
10821 /* Like recog, but we receive the address of a pointer to a new pattern.
10822 We try to match the rtx that the pointer points to.
10823 If that fails, we may try to modify or replace the pattern,
10824 storing the replacement into the same pointer object.
10826 Modifications include deletion or addition of CLOBBERs.
10828 PNOTES is a pointer to a location where any REG_UNUSED notes added for
10829 the CLOBBERs are placed.
10831 The value is the final insn code from the pattern ultimately matched,
10832 or -1. */
10834 static int
10835 recog_for_combine (rtx *pnewpat, rtx_insn *insn, rtx *pnotes)
10837 rtx pat = *pnewpat;
10838 rtx pat_without_clobbers;
10839 int insn_code_number;
10840 int num_clobbers_to_add = 0;
10841 int i;
10842 rtx notes = NULL_RTX;
10843 rtx old_notes, old_pat;
10844 int old_icode;
10846 /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
10847 we use to indicate that something didn't match. If we find such a
10848 thing, force rejection. */
10849 if (GET_CODE (pat) == PARALLEL)
10850 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
10851 if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
10852 && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
10853 return -1;
10855 old_pat = PATTERN (insn);
10856 old_notes = REG_NOTES (insn);
10857 PATTERN (insn) = pat;
10858 REG_NOTES (insn) = NULL_RTX;
10860 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
10861 if (dump_file && (dump_flags & TDF_DETAILS))
10863 if (insn_code_number < 0)
10864 fputs ("Failed to match this instruction:\n", dump_file);
10865 else
10866 fputs ("Successfully matched this instruction:\n", dump_file);
10867 print_rtl_single (dump_file, pat);
10870 /* If it isn't, there is the possibility that we previously had an insn
10871 that clobbered some register as a side effect, but the combined
10872 insn doesn't need to do that. So try once more without the clobbers
10873 unless this represents an ASM insn. */
10875 if (insn_code_number < 0 && ! check_asm_operands (pat)
10876 && GET_CODE (pat) == PARALLEL)
10878 int pos;
10880 for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
10881 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
10883 if (i != pos)
10884 SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
10885 pos++;
10888 SUBST_INT (XVECLEN (pat, 0), pos);
10890 if (pos == 1)
10891 pat = XVECEXP (pat, 0, 0);
10893 PATTERN (insn) = pat;
10894 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
10895 if (dump_file && (dump_flags & TDF_DETAILS))
10897 if (insn_code_number < 0)
10898 fputs ("Failed to match this instruction:\n", dump_file);
10899 else
10900 fputs ("Successfully matched this instruction:\n", dump_file);
10901 print_rtl_single (dump_file, pat);
10905 pat_without_clobbers = pat;
10907 PATTERN (insn) = old_pat;
10908 REG_NOTES (insn) = old_notes;
10910 /* Recognize all noop sets, these will be killed by followup pass. */
10911 if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
10912 insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
10914 /* If we had any clobbers to add, make a new pattern than contains
10915 them. Then check to make sure that all of them are dead. */
10916 if (num_clobbers_to_add)
10918 rtx newpat = gen_rtx_PARALLEL (VOIDmode,
10919 rtvec_alloc (GET_CODE (pat) == PARALLEL
10920 ? (XVECLEN (pat, 0)
10921 + num_clobbers_to_add)
10922 : num_clobbers_to_add + 1));
10924 if (GET_CODE (pat) == PARALLEL)
10925 for (i = 0; i < XVECLEN (pat, 0); i++)
10926 XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
10927 else
10928 XVECEXP (newpat, 0, 0) = pat;
10930 add_clobbers (newpat, insn_code_number);
10932 for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
10933 i < XVECLEN (newpat, 0); i++)
10935 if (REG_P (XEXP (XVECEXP (newpat, 0, i), 0))
10936 && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
10937 return -1;
10938 if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) != SCRATCH)
10940 gcc_assert (REG_P (XEXP (XVECEXP (newpat, 0, i), 0)));
10941 notes = alloc_reg_note (REG_UNUSED,
10942 XEXP (XVECEXP (newpat, 0, i), 0), notes);
10945 pat = newpat;
10948 if (insn_code_number >= 0
10949 && insn_code_number != NOOP_MOVE_INSN_CODE)
10951 old_pat = PATTERN (insn);
10952 old_notes = REG_NOTES (insn);
10953 old_icode = INSN_CODE (insn);
10954 PATTERN (insn) = pat;
10955 REG_NOTES (insn) = notes;
10957 /* Allow targets to reject combined insn. */
10958 if (!targetm.legitimate_combined_insn (insn))
10960 if (dump_file && (dump_flags & TDF_DETAILS))
10961 fputs ("Instruction not appropriate for target.",
10962 dump_file);
10964 /* Callers expect recog_for_combine to strip
10965 clobbers from the pattern on failure. */
10966 pat = pat_without_clobbers;
10967 notes = NULL_RTX;
10969 insn_code_number = -1;
10972 PATTERN (insn) = old_pat;
10973 REG_NOTES (insn) = old_notes;
10974 INSN_CODE (insn) = old_icode;
10977 *pnewpat = pat;
10978 *pnotes = notes;
10980 return insn_code_number;
10983 /* Like gen_lowpart_general but for use by combine. In combine it
10984 is not possible to create any new pseudoregs. However, it is
10985 safe to create invalid memory addresses, because combine will
10986 try to recognize them and all they will do is make the combine
10987 attempt fail.
10989 If for some reason this cannot do its job, an rtx
10990 (clobber (const_int 0)) is returned.
10991 An insn containing that will not be recognized. */
10993 static rtx
10994 gen_lowpart_for_combine (machine_mode omode, rtx x)
10996 machine_mode imode = GET_MODE (x);
10997 unsigned int osize = GET_MODE_SIZE (omode);
10998 unsigned int isize = GET_MODE_SIZE (imode);
10999 rtx result;
11001 if (omode == imode)
11002 return x;
11004 /* We can only support MODE being wider than a word if X is a
11005 constant integer or has a mode the same size. */
11006 if (GET_MODE_SIZE (omode) > UNITS_PER_WORD
11007 && ! (CONST_SCALAR_INT_P (x) || isize == osize))
11008 goto fail;
11010 /* X might be a paradoxical (subreg (mem)). In that case, gen_lowpart
11011 won't know what to do. So we will strip off the SUBREG here and
11012 process normally. */
11013 if (GET_CODE (x) == SUBREG && MEM_P (SUBREG_REG (x)))
11015 x = SUBREG_REG (x);
11017 /* For use in case we fall down into the address adjustments
11018 further below, we need to adjust the known mode and size of
11019 x; imode and isize, since we just adjusted x. */
11020 imode = GET_MODE (x);
11022 if (imode == omode)
11023 return x;
11025 isize = GET_MODE_SIZE (imode);
11028 result = gen_lowpart_common (omode, x);
11030 if (result)
11031 return result;
11033 if (MEM_P (x))
11035 int offset = 0;
11037 /* Refuse to work on a volatile memory ref or one with a mode-dependent
11038 address. */
11039 if (MEM_VOLATILE_P (x)
11040 || mode_dependent_address_p (XEXP (x, 0), MEM_ADDR_SPACE (x)))
11041 goto fail;
11043 /* If we want to refer to something bigger than the original memref,
11044 generate a paradoxical subreg instead. That will force a reload
11045 of the original memref X. */
11046 if (isize < osize)
11047 return gen_rtx_SUBREG (omode, x, 0);
11049 if (WORDS_BIG_ENDIAN)
11050 offset = MAX (isize, UNITS_PER_WORD) - MAX (osize, UNITS_PER_WORD);
11052 /* Adjust the address so that the address-after-the-data is
11053 unchanged. */
11054 if (BYTES_BIG_ENDIAN)
11055 offset -= MIN (UNITS_PER_WORD, osize) - MIN (UNITS_PER_WORD, isize);
11057 return adjust_address_nv (x, omode, offset);
11060 /* If X is a comparison operator, rewrite it in a new mode. This
11061 probably won't match, but may allow further simplifications. */
11062 else if (COMPARISON_P (x))
11063 return gen_rtx_fmt_ee (GET_CODE (x), omode, XEXP (x, 0), XEXP (x, 1));
11065 /* If we couldn't simplify X any other way, just enclose it in a
11066 SUBREG. Normally, this SUBREG won't match, but some patterns may
11067 include an explicit SUBREG or we may simplify it further in combine. */
11068 else
11070 int offset = 0;
11071 rtx res;
11073 offset = subreg_lowpart_offset (omode, imode);
11074 if (imode == VOIDmode)
11076 imode = int_mode_for_mode (omode);
11077 x = gen_lowpart_common (imode, x);
11078 if (x == NULL)
11079 goto fail;
11081 res = simplify_gen_subreg (omode, x, imode, offset);
11082 if (res)
11083 return res;
11086 fail:
11087 return gen_rtx_CLOBBER (omode, const0_rtx);
11090 /* Try to simplify a comparison between OP0 and a constant OP1,
11091 where CODE is the comparison code that will be tested, into a
11092 (CODE OP0 const0_rtx) form.
11094 The result is a possibly different comparison code to use.
11095 *POP1 may be updated. */
11097 static enum rtx_code
11098 simplify_compare_const (enum rtx_code code, machine_mode mode,
11099 rtx op0, rtx *pop1)
11101 unsigned int mode_width = GET_MODE_PRECISION (mode);
11102 HOST_WIDE_INT const_op = INTVAL (*pop1);
11104 /* Get the constant we are comparing against and turn off all bits
11105 not on in our mode. */
11106 if (mode != VOIDmode)
11107 const_op = trunc_int_for_mode (const_op, mode);
11109 /* If we are comparing against a constant power of two and the value
11110 being compared can only have that single bit nonzero (e.g., it was
11111 `and'ed with that bit), we can replace this with a comparison
11112 with zero. */
11113 if (const_op
11114 && (code == EQ || code == NE || code == GE || code == GEU
11115 || code == LT || code == LTU)
11116 && mode_width - 1 < HOST_BITS_PER_WIDE_INT
11117 && exact_log2 (const_op & GET_MODE_MASK (mode)) >= 0
11118 && (nonzero_bits (op0, mode)
11119 == (unsigned HOST_WIDE_INT) (const_op & GET_MODE_MASK (mode))))
11121 code = (code == EQ || code == GE || code == GEU ? NE : EQ);
11122 const_op = 0;
11125 /* Similarly, if we are comparing a value known to be either -1 or
11126 0 with -1, change it to the opposite comparison against zero. */
11127 if (const_op == -1
11128 && (code == EQ || code == NE || code == GT || code == LE
11129 || code == GEU || code == LTU)
11130 && num_sign_bit_copies (op0, mode) == mode_width)
11132 code = (code == EQ || code == LE || code == GEU ? NE : EQ);
11133 const_op = 0;
11136 /* Do some canonicalizations based on the comparison code. We prefer
11137 comparisons against zero and then prefer equality comparisons.
11138 If we can reduce the size of a constant, we will do that too. */
11139 switch (code)
11141 case LT:
11142 /* < C is equivalent to <= (C - 1) */
11143 if (const_op > 0)
11145 const_op -= 1;
11146 code = LE;
11147 /* ... fall through to LE case below. */
11149 else
11150 break;
11152 case LE:
11153 /* <= C is equivalent to < (C + 1); we do this for C < 0 */
11154 if (const_op < 0)
11156 const_op += 1;
11157 code = LT;
11160 /* If we are doing a <= 0 comparison on a value known to have
11161 a zero sign bit, we can replace this with == 0. */
11162 else if (const_op == 0
11163 && mode_width - 1 < HOST_BITS_PER_WIDE_INT
11164 && (nonzero_bits (op0, mode)
11165 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11166 == 0)
11167 code = EQ;
11168 break;
11170 case GE:
11171 /* >= C is equivalent to > (C - 1). */
11172 if (const_op > 0)
11174 const_op -= 1;
11175 code = GT;
11176 /* ... fall through to GT below. */
11178 else
11179 break;
11181 case GT:
11182 /* > C is equivalent to >= (C + 1); we do this for C < 0. */
11183 if (const_op < 0)
11185 const_op += 1;
11186 code = GE;
11189 /* If we are doing a > 0 comparison on a value known to have
11190 a zero sign bit, we can replace this with != 0. */
11191 else if (const_op == 0
11192 && mode_width - 1 < HOST_BITS_PER_WIDE_INT
11193 && (nonzero_bits (op0, mode)
11194 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11195 == 0)
11196 code = NE;
11197 break;
11199 case LTU:
11200 /* < C is equivalent to <= (C - 1). */
11201 if (const_op > 0)
11203 const_op -= 1;
11204 code = LEU;
11205 /* ... fall through ... */
11207 /* (unsigned) < 0x80000000 is equivalent to >= 0. */
11208 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
11209 && (unsigned HOST_WIDE_INT) const_op
11210 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1))
11212 const_op = 0;
11213 code = GE;
11214 break;
11216 else
11217 break;
11219 case LEU:
11220 /* unsigned <= 0 is equivalent to == 0 */
11221 if (const_op == 0)
11222 code = EQ;
11223 /* (unsigned) <= 0x7fffffff is equivalent to >= 0. */
11224 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
11225 && (unsigned HOST_WIDE_INT) const_op
11226 == ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)) - 1)
11228 const_op = 0;
11229 code = GE;
11231 break;
11233 case GEU:
11234 /* >= C is equivalent to > (C - 1). */
11235 if (const_op > 1)
11237 const_op -= 1;
11238 code = GTU;
11239 /* ... fall through ... */
11242 /* (unsigned) >= 0x80000000 is equivalent to < 0. */
11243 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
11244 && (unsigned HOST_WIDE_INT) const_op
11245 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1))
11247 const_op = 0;
11248 code = LT;
11249 break;
11251 else
11252 break;
11254 case GTU:
11255 /* unsigned > 0 is equivalent to != 0 */
11256 if (const_op == 0)
11257 code = NE;
11258 /* (unsigned) > 0x7fffffff is equivalent to < 0. */
11259 else if (mode_width - 1 < HOST_BITS_PER_WIDE_INT
11260 && (unsigned HOST_WIDE_INT) const_op
11261 == ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)) - 1)
11263 const_op = 0;
11264 code = LT;
11266 break;
11268 default:
11269 break;
11272 *pop1 = GEN_INT (const_op);
11273 return code;
11276 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
11277 comparison code that will be tested.
11279 The result is a possibly different comparison code to use. *POP0 and
11280 *POP1 may be updated.
11282 It is possible that we might detect that a comparison is either always
11283 true or always false. However, we do not perform general constant
11284 folding in combine, so this knowledge isn't useful. Such tautologies
11285 should have been detected earlier. Hence we ignore all such cases. */
11287 static enum rtx_code
11288 simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
11290 rtx op0 = *pop0;
11291 rtx op1 = *pop1;
11292 rtx tem, tem1;
11293 int i;
11294 machine_mode mode, tmode;
11296 /* Try a few ways of applying the same transformation to both operands. */
11297 while (1)
11299 #ifndef WORD_REGISTER_OPERATIONS
11300 /* The test below this one won't handle SIGN_EXTENDs on these machines,
11301 so check specially. */
11302 if (code != GTU && code != GEU && code != LTU && code != LEU
11303 && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
11304 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11305 && GET_CODE (XEXP (op1, 0)) == ASHIFT
11306 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
11307 && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
11308 && (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0)))
11309 == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0))))
11310 && CONST_INT_P (XEXP (op0, 1))
11311 && XEXP (op0, 1) == XEXP (op1, 1)
11312 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
11313 && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
11314 && (INTVAL (XEXP (op0, 1))
11315 == (GET_MODE_PRECISION (GET_MODE (op0))
11316 - (GET_MODE_PRECISION
11317 (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))))))))
11319 op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
11320 op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
11322 #endif
11324 /* If both operands are the same constant shift, see if we can ignore the
11325 shift. We can if the shift is a rotate or if the bits shifted out of
11326 this shift are known to be zero for both inputs and if the type of
11327 comparison is compatible with the shift. */
11328 if (GET_CODE (op0) == GET_CODE (op1)
11329 && HWI_COMPUTABLE_MODE_P (GET_MODE (op0))
11330 && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
11331 || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
11332 && (code != GT && code != LT && code != GE && code != LE))
11333 || (GET_CODE (op0) == ASHIFTRT
11334 && (code != GTU && code != LTU
11335 && code != GEU && code != LEU)))
11336 && CONST_INT_P (XEXP (op0, 1))
11337 && INTVAL (XEXP (op0, 1)) >= 0
11338 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
11339 && XEXP (op0, 1) == XEXP (op1, 1))
11341 machine_mode mode = GET_MODE (op0);
11342 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11343 int shift_count = INTVAL (XEXP (op0, 1));
11345 if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
11346 mask &= (mask >> shift_count) << shift_count;
11347 else if (GET_CODE (op0) == ASHIFT)
11348 mask = (mask & (mask << shift_count)) >> shift_count;
11350 if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
11351 && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
11352 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
11353 else
11354 break;
11357 /* If both operands are AND's of a paradoxical SUBREG by constant, the
11358 SUBREGs are of the same mode, and, in both cases, the AND would
11359 be redundant if the comparison was done in the narrower mode,
11360 do the comparison in the narrower mode (e.g., we are AND'ing with 1
11361 and the operand's possibly nonzero bits are 0xffffff01; in that case
11362 if we only care about QImode, we don't need the AND). This case
11363 occurs if the output mode of an scc insn is not SImode and
11364 STORE_FLAG_VALUE == 1 (e.g., the 386).
11366 Similarly, check for a case where the AND's are ZERO_EXTEND
11367 operations from some narrower mode even though a SUBREG is not
11368 present. */
11370 else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
11371 && CONST_INT_P (XEXP (op0, 1))
11372 && CONST_INT_P (XEXP (op1, 1)))
11374 rtx inner_op0 = XEXP (op0, 0);
11375 rtx inner_op1 = XEXP (op1, 0);
11376 HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
11377 HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
11378 int changed = 0;
11380 if (paradoxical_subreg_p (inner_op0)
11381 && GET_CODE (inner_op1) == SUBREG
11382 && (GET_MODE (SUBREG_REG (inner_op0))
11383 == GET_MODE (SUBREG_REG (inner_op1)))
11384 && (GET_MODE_PRECISION (GET_MODE (SUBREG_REG (inner_op0)))
11385 <= HOST_BITS_PER_WIDE_INT)
11386 && (0 == ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
11387 GET_MODE (SUBREG_REG (inner_op0)))))
11388 && (0 == ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
11389 GET_MODE (SUBREG_REG (inner_op1))))))
11391 op0 = SUBREG_REG (inner_op0);
11392 op1 = SUBREG_REG (inner_op1);
11394 /* The resulting comparison is always unsigned since we masked
11395 off the original sign bit. */
11396 code = unsigned_condition (code);
11398 changed = 1;
11401 else if (c0 == c1)
11402 for (tmode = GET_CLASS_NARROWEST_MODE
11403 (GET_MODE_CLASS (GET_MODE (op0)));
11404 tmode != GET_MODE (op0); tmode = GET_MODE_WIDER_MODE (tmode))
11405 if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
11407 op0 = gen_lowpart (tmode, inner_op0);
11408 op1 = gen_lowpart (tmode, inner_op1);
11409 code = unsigned_condition (code);
11410 changed = 1;
11411 break;
11414 if (! changed)
11415 break;
11418 /* If both operands are NOT, we can strip off the outer operation
11419 and adjust the comparison code for swapped operands; similarly for
11420 NEG, except that this must be an equality comparison. */
11421 else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
11422 || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
11423 && (code == EQ || code == NE)))
11424 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
11426 else
11427 break;
11430 /* If the first operand is a constant, swap the operands and adjust the
11431 comparison code appropriately, but don't do this if the second operand
11432 is already a constant integer. */
11433 if (swap_commutative_operands_p (op0, op1))
11435 tem = op0, op0 = op1, op1 = tem;
11436 code = swap_condition (code);
11439 /* We now enter a loop during which we will try to simplify the comparison.
11440 For the most part, we only are concerned with comparisons with zero,
11441 but some things may really be comparisons with zero but not start
11442 out looking that way. */
11444 while (CONST_INT_P (op1))
11446 machine_mode mode = GET_MODE (op0);
11447 unsigned int mode_width = GET_MODE_PRECISION (mode);
11448 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11449 int equality_comparison_p;
11450 int sign_bit_comparison_p;
11451 int unsigned_comparison_p;
11452 HOST_WIDE_INT const_op;
11454 /* We only want to handle integral modes. This catches VOIDmode,
11455 CCmode, and the floating-point modes. An exception is that we
11456 can handle VOIDmode if OP0 is a COMPARE or a comparison
11457 operation. */
11459 if (GET_MODE_CLASS (mode) != MODE_INT
11460 && ! (mode == VOIDmode
11461 && (GET_CODE (op0) == COMPARE || COMPARISON_P (op0))))
11462 break;
11464 /* Try to simplify the compare to constant, possibly changing the
11465 comparison op, and/or changing op1 to zero. */
11466 code = simplify_compare_const (code, mode, op0, &op1);
11467 const_op = INTVAL (op1);
11469 /* Compute some predicates to simplify code below. */
11471 equality_comparison_p = (code == EQ || code == NE);
11472 sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
11473 unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
11474 || code == GEU);
11476 /* If this is a sign bit comparison and we can do arithmetic in
11477 MODE, say that we will only be needing the sign bit of OP0. */
11478 if (sign_bit_comparison_p && HWI_COMPUTABLE_MODE_P (mode))
11479 op0 = force_to_mode (op0, mode,
11480 (unsigned HOST_WIDE_INT) 1
11481 << (GET_MODE_PRECISION (mode) - 1),
11484 /* Now try cases based on the opcode of OP0. If none of the cases
11485 does a "continue", we exit this loop immediately after the
11486 switch. */
11488 switch (GET_CODE (op0))
11490 case ZERO_EXTRACT:
11491 /* If we are extracting a single bit from a variable position in
11492 a constant that has only a single bit set and are comparing it
11493 with zero, we can convert this into an equality comparison
11494 between the position and the location of the single bit. */
11495 /* Except we can't if SHIFT_COUNT_TRUNCATED is set, since we might
11496 have already reduced the shift count modulo the word size. */
11497 if (!SHIFT_COUNT_TRUNCATED
11498 && CONST_INT_P (XEXP (op0, 0))
11499 && XEXP (op0, 1) == const1_rtx
11500 && equality_comparison_p && const_op == 0
11501 && (i = exact_log2 (UINTVAL (XEXP (op0, 0)))) >= 0)
11503 if (BITS_BIG_ENDIAN)
11504 i = BITS_PER_WORD - 1 - i;
11506 op0 = XEXP (op0, 2);
11507 op1 = GEN_INT (i);
11508 const_op = i;
11510 /* Result is nonzero iff shift count is equal to I. */
11511 code = reverse_condition (code);
11512 continue;
11515 /* ... fall through ... */
11517 case SIGN_EXTRACT:
11518 tem = expand_compound_operation (op0);
11519 if (tem != op0)
11521 op0 = tem;
11522 continue;
11524 break;
11526 case NOT:
11527 /* If testing for equality, we can take the NOT of the constant. */
11528 if (equality_comparison_p
11529 && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
11531 op0 = XEXP (op0, 0);
11532 op1 = tem;
11533 continue;
11536 /* If just looking at the sign bit, reverse the sense of the
11537 comparison. */
11538 if (sign_bit_comparison_p)
11540 op0 = XEXP (op0, 0);
11541 code = (code == GE ? LT : GE);
11542 continue;
11544 break;
11546 case NEG:
11547 /* If testing for equality, we can take the NEG of the constant. */
11548 if (equality_comparison_p
11549 && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
11551 op0 = XEXP (op0, 0);
11552 op1 = tem;
11553 continue;
11556 /* The remaining cases only apply to comparisons with zero. */
11557 if (const_op != 0)
11558 break;
11560 /* When X is ABS or is known positive,
11561 (neg X) is < 0 if and only if X != 0. */
11563 if (sign_bit_comparison_p
11564 && (GET_CODE (XEXP (op0, 0)) == ABS
11565 || (mode_width <= HOST_BITS_PER_WIDE_INT
11566 && (nonzero_bits (XEXP (op0, 0), mode)
11567 & ((unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11568 == 0)))
11570 op0 = XEXP (op0, 0);
11571 code = (code == LT ? NE : EQ);
11572 continue;
11575 /* If we have NEG of something whose two high-order bits are the
11576 same, we know that "(-a) < 0" is equivalent to "a > 0". */
11577 if (num_sign_bit_copies (op0, mode) >= 2)
11579 op0 = XEXP (op0, 0);
11580 code = swap_condition (code);
11581 continue;
11583 break;
11585 case ROTATE:
11586 /* If we are testing equality and our count is a constant, we
11587 can perform the inverse operation on our RHS. */
11588 if (equality_comparison_p && CONST_INT_P (XEXP (op0, 1))
11589 && (tem = simplify_binary_operation (ROTATERT, mode,
11590 op1, XEXP (op0, 1))) != 0)
11592 op0 = XEXP (op0, 0);
11593 op1 = tem;
11594 continue;
11597 /* If we are doing a < 0 or >= 0 comparison, it means we are testing
11598 a particular bit. Convert it to an AND of a constant of that
11599 bit. This will be converted into a ZERO_EXTRACT. */
11600 if (const_op == 0 && sign_bit_comparison_p
11601 && CONST_INT_P (XEXP (op0, 1))
11602 && mode_width <= HOST_BITS_PER_WIDE_INT)
11604 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
11605 ((unsigned HOST_WIDE_INT) 1
11606 << (mode_width - 1
11607 - INTVAL (XEXP (op0, 1)))));
11608 code = (code == LT ? NE : EQ);
11609 continue;
11612 /* Fall through. */
11614 case ABS:
11615 /* ABS is ignorable inside an equality comparison with zero. */
11616 if (const_op == 0 && equality_comparison_p)
11618 op0 = XEXP (op0, 0);
11619 continue;
11621 break;
11623 case SIGN_EXTEND:
11624 /* Can simplify (compare (zero/sign_extend FOO) CONST) to
11625 (compare FOO CONST) if CONST fits in FOO's mode and we
11626 are either testing inequality or have an unsigned
11627 comparison with ZERO_EXTEND or a signed comparison with
11628 SIGN_EXTEND. But don't do it if we don't have a compare
11629 insn of the given mode, since we'd have to revert it
11630 later on, and then we wouldn't know whether to sign- or
11631 zero-extend. */
11632 mode = GET_MODE (XEXP (op0, 0));
11633 if (GET_MODE_CLASS (mode) == MODE_INT
11634 && ! unsigned_comparison_p
11635 && HWI_COMPUTABLE_MODE_P (mode)
11636 && trunc_int_for_mode (const_op, mode) == const_op
11637 && have_insn_for (COMPARE, mode))
11639 op0 = XEXP (op0, 0);
11640 continue;
11642 break;
11644 case SUBREG:
11645 /* Check for the case where we are comparing A - C1 with C2, that is
11647 (subreg:MODE (plus (A) (-C1))) op (C2)
11649 with C1 a constant, and try to lift the SUBREG, i.e. to do the
11650 comparison in the wider mode. One of the following two conditions
11651 must be true in order for this to be valid:
11653 1. The mode extension results in the same bit pattern being added
11654 on both sides and the comparison is equality or unsigned. As
11655 C2 has been truncated to fit in MODE, the pattern can only be
11656 all 0s or all 1s.
11658 2. The mode extension results in the sign bit being copied on
11659 each side.
11661 The difficulty here is that we have predicates for A but not for
11662 (A - C1) so we need to check that C1 is within proper bounds so
11663 as to perturbate A as little as possible. */
11665 if (mode_width <= HOST_BITS_PER_WIDE_INT
11666 && subreg_lowpart_p (op0)
11667 && GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op0))) > mode_width
11668 && GET_CODE (SUBREG_REG (op0)) == PLUS
11669 && CONST_INT_P (XEXP (SUBREG_REG (op0), 1)))
11671 machine_mode inner_mode = GET_MODE (SUBREG_REG (op0));
11672 rtx a = XEXP (SUBREG_REG (op0), 0);
11673 HOST_WIDE_INT c1 = -INTVAL (XEXP (SUBREG_REG (op0), 1));
11675 if ((c1 > 0
11676 && (unsigned HOST_WIDE_INT) c1
11677 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)
11678 && (equality_comparison_p || unsigned_comparison_p)
11679 /* (A - C1) zero-extends if it is positive and sign-extends
11680 if it is negative, C2 both zero- and sign-extends. */
11681 && ((0 == (nonzero_bits (a, inner_mode)
11682 & ~GET_MODE_MASK (mode))
11683 && const_op >= 0)
11684 /* (A - C1) sign-extends if it is positive and 1-extends
11685 if it is negative, C2 both sign- and 1-extends. */
11686 || (num_sign_bit_copies (a, inner_mode)
11687 > (unsigned int) (GET_MODE_PRECISION (inner_mode)
11688 - mode_width)
11689 && const_op < 0)))
11690 || ((unsigned HOST_WIDE_INT) c1
11691 < (unsigned HOST_WIDE_INT) 1 << (mode_width - 2)
11692 /* (A - C1) always sign-extends, like C2. */
11693 && num_sign_bit_copies (a, inner_mode)
11694 > (unsigned int) (GET_MODE_PRECISION (inner_mode)
11695 - (mode_width - 1))))
11697 op0 = SUBREG_REG (op0);
11698 continue;
11702 /* If the inner mode is narrower and we are extracting the low part,
11703 we can treat the SUBREG as if it were a ZERO_EXTEND. */
11704 if (subreg_lowpart_p (op0)
11705 && GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op0))) < mode_width)
11706 /* Fall through */ ;
11707 else
11708 break;
11710 /* ... fall through ... */
11712 case ZERO_EXTEND:
11713 mode = GET_MODE (XEXP (op0, 0));
11714 if (GET_MODE_CLASS (mode) == MODE_INT
11715 && (unsigned_comparison_p || equality_comparison_p)
11716 && HWI_COMPUTABLE_MODE_P (mode)
11717 && (unsigned HOST_WIDE_INT) const_op <= GET_MODE_MASK (mode)
11718 && const_op >= 0
11719 && have_insn_for (COMPARE, mode))
11721 op0 = XEXP (op0, 0);
11722 continue;
11724 break;
11726 case PLUS:
11727 /* (eq (plus X A) B) -> (eq X (minus B A)). We can only do
11728 this for equality comparisons due to pathological cases involving
11729 overflows. */
11730 if (equality_comparison_p
11731 && 0 != (tem = simplify_binary_operation (MINUS, mode,
11732 op1, XEXP (op0, 1))))
11734 op0 = XEXP (op0, 0);
11735 op1 = tem;
11736 continue;
11739 /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0. */
11740 if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
11741 && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
11743 op0 = XEXP (XEXP (op0, 0), 0);
11744 code = (code == LT ? EQ : NE);
11745 continue;
11747 break;
11749 case MINUS:
11750 /* We used to optimize signed comparisons against zero, but that
11751 was incorrect. Unsigned comparisons against zero (GTU, LEU)
11752 arrive here as equality comparisons, or (GEU, LTU) are
11753 optimized away. No need to special-case them. */
11755 /* (eq (minus A B) C) -> (eq A (plus B C)) or
11756 (eq B (minus A C)), whichever simplifies. We can only do
11757 this for equality comparisons due to pathological cases involving
11758 overflows. */
11759 if (equality_comparison_p
11760 && 0 != (tem = simplify_binary_operation (PLUS, mode,
11761 XEXP (op0, 1), op1)))
11763 op0 = XEXP (op0, 0);
11764 op1 = tem;
11765 continue;
11768 if (equality_comparison_p
11769 && 0 != (tem = simplify_binary_operation (MINUS, mode,
11770 XEXP (op0, 0), op1)))
11772 op0 = XEXP (op0, 1);
11773 op1 = tem;
11774 continue;
11777 /* The sign bit of (minus (ashiftrt X C) X), where C is the number
11778 of bits in X minus 1, is one iff X > 0. */
11779 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
11780 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11781 && UINTVAL (XEXP (XEXP (op0, 0), 1)) == mode_width - 1
11782 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
11784 op0 = XEXP (op0, 1);
11785 code = (code == GE ? LE : GT);
11786 continue;
11788 break;
11790 case XOR:
11791 /* (eq (xor A B) C) -> (eq A (xor B C)). This is a simplification
11792 if C is zero or B is a constant. */
11793 if (equality_comparison_p
11794 && 0 != (tem = simplify_binary_operation (XOR, mode,
11795 XEXP (op0, 1), op1)))
11797 op0 = XEXP (op0, 0);
11798 op1 = tem;
11799 continue;
11801 break;
11803 case EQ: case NE:
11804 case UNEQ: case LTGT:
11805 case LT: case LTU: case UNLT: case LE: case LEU: case UNLE:
11806 case GT: case GTU: case UNGT: case GE: case GEU: case UNGE:
11807 case UNORDERED: case ORDERED:
11808 /* We can't do anything if OP0 is a condition code value, rather
11809 than an actual data value. */
11810 if (const_op != 0
11811 || CC0_P (XEXP (op0, 0))
11812 || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
11813 break;
11815 /* Get the two operands being compared. */
11816 if (GET_CODE (XEXP (op0, 0)) == COMPARE)
11817 tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
11818 else
11819 tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
11821 /* Check for the cases where we simply want the result of the
11822 earlier test or the opposite of that result. */
11823 if (code == NE || code == EQ
11824 || (val_signbit_known_set_p (GET_MODE (op0), STORE_FLAG_VALUE)
11825 && (code == LT || code == GE)))
11827 enum rtx_code new_code;
11828 if (code == LT || code == NE)
11829 new_code = GET_CODE (op0);
11830 else
11831 new_code = reversed_comparison_code (op0, NULL);
11833 if (new_code != UNKNOWN)
11835 code = new_code;
11836 op0 = tem;
11837 op1 = tem1;
11838 continue;
11841 break;
11843 case IOR:
11844 /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
11845 iff X <= 0. */
11846 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
11847 && XEXP (XEXP (op0, 0), 1) == constm1_rtx
11848 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
11850 op0 = XEXP (op0, 1);
11851 code = (code == GE ? GT : LE);
11852 continue;
11854 break;
11856 case AND:
11857 /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1). This
11858 will be converted to a ZERO_EXTRACT later. */
11859 if (const_op == 0 && equality_comparison_p
11860 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11861 && XEXP (XEXP (op0, 0), 0) == const1_rtx)
11863 op0 = gen_rtx_LSHIFTRT (mode, XEXP (op0, 1),
11864 XEXP (XEXP (op0, 0), 1));
11865 op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
11866 continue;
11869 /* If we are comparing (and (lshiftrt X C1) C2) for equality with
11870 zero and X is a comparison and C1 and C2 describe only bits set
11871 in STORE_FLAG_VALUE, we can compare with X. */
11872 if (const_op == 0 && equality_comparison_p
11873 && mode_width <= HOST_BITS_PER_WIDE_INT
11874 && CONST_INT_P (XEXP (op0, 1))
11875 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
11876 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
11877 && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
11878 && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
11880 mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
11881 << INTVAL (XEXP (XEXP (op0, 0), 1)));
11882 if ((~STORE_FLAG_VALUE & mask) == 0
11883 && (COMPARISON_P (XEXP (XEXP (op0, 0), 0))
11884 || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
11885 && COMPARISON_P (tem))))
11887 op0 = XEXP (XEXP (op0, 0), 0);
11888 continue;
11892 /* If we are doing an equality comparison of an AND of a bit equal
11893 to the sign bit, replace this with a LT or GE comparison of
11894 the underlying value. */
11895 if (equality_comparison_p
11896 && const_op == 0
11897 && CONST_INT_P (XEXP (op0, 1))
11898 && mode_width <= HOST_BITS_PER_WIDE_INT
11899 && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
11900 == (unsigned HOST_WIDE_INT) 1 << (mode_width - 1)))
11902 op0 = XEXP (op0, 0);
11903 code = (code == EQ ? GE : LT);
11904 continue;
11907 /* If this AND operation is really a ZERO_EXTEND from a narrower
11908 mode, the constant fits within that mode, and this is either an
11909 equality or unsigned comparison, try to do this comparison in
11910 the narrower mode.
11912 Note that in:
11914 (ne:DI (and:DI (reg:DI 4) (const_int 0xffffffff)) (const_int 0))
11915 -> (ne:DI (reg:SI 4) (const_int 0))
11917 unless TRULY_NOOP_TRUNCATION allows it or the register is
11918 known to hold a value of the required mode the
11919 transformation is invalid. */
11920 if ((equality_comparison_p || unsigned_comparison_p)
11921 && CONST_INT_P (XEXP (op0, 1))
11922 && (i = exact_log2 ((UINTVAL (XEXP (op0, 1))
11923 & GET_MODE_MASK (mode))
11924 + 1)) >= 0
11925 && const_op >> i == 0
11926 && (tmode = mode_for_size (i, MODE_INT, 1)) != BLKmode
11927 && (TRULY_NOOP_TRUNCATION_MODES_P (tmode, GET_MODE (op0))
11928 || (REG_P (XEXP (op0, 0))
11929 && reg_truncated_to_mode (tmode, XEXP (op0, 0)))))
11931 op0 = gen_lowpart (tmode, XEXP (op0, 0));
11932 continue;
11935 /* If this is (and:M1 (subreg:M2 X 0) (const_int C1)) where C1
11936 fits in both M1 and M2 and the SUBREG is either paradoxical
11937 or represents the low part, permute the SUBREG and the AND
11938 and try again. */
11939 if (GET_CODE (XEXP (op0, 0)) == SUBREG)
11941 unsigned HOST_WIDE_INT c1;
11942 tmode = GET_MODE (SUBREG_REG (XEXP (op0, 0)));
11943 /* Require an integral mode, to avoid creating something like
11944 (AND:SF ...). */
11945 if (SCALAR_INT_MODE_P (tmode)
11946 /* It is unsafe to commute the AND into the SUBREG if the
11947 SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
11948 not defined. As originally written the upper bits
11949 have a defined value due to the AND operation.
11950 However, if we commute the AND inside the SUBREG then
11951 they no longer have defined values and the meaning of
11952 the code has been changed. */
11953 && (0
11954 #ifdef WORD_REGISTER_OPERATIONS
11955 || (mode_width > GET_MODE_PRECISION (tmode)
11956 && mode_width <= BITS_PER_WORD)
11957 #endif
11958 || (mode_width <= GET_MODE_PRECISION (tmode)
11959 && subreg_lowpart_p (XEXP (op0, 0))))
11960 && CONST_INT_P (XEXP (op0, 1))
11961 && mode_width <= HOST_BITS_PER_WIDE_INT
11962 && HWI_COMPUTABLE_MODE_P (tmode)
11963 && ((c1 = INTVAL (XEXP (op0, 1))) & ~mask) == 0
11964 && (c1 & ~GET_MODE_MASK (tmode)) == 0
11965 && c1 != mask
11966 && c1 != GET_MODE_MASK (tmode))
11968 op0 = simplify_gen_binary (AND, tmode,
11969 SUBREG_REG (XEXP (op0, 0)),
11970 gen_int_mode (c1, tmode));
11971 op0 = gen_lowpart (mode, op0);
11972 continue;
11976 /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0). */
11977 if (const_op == 0 && equality_comparison_p
11978 && XEXP (op0, 1) == const1_rtx
11979 && GET_CODE (XEXP (op0, 0)) == NOT)
11981 op0 = simplify_and_const_int (NULL_RTX, mode,
11982 XEXP (XEXP (op0, 0), 0), 1);
11983 code = (code == NE ? EQ : NE);
11984 continue;
11987 /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
11988 (eq (and (lshiftrt X) 1) 0).
11989 Also handle the case where (not X) is expressed using xor. */
11990 if (const_op == 0 && equality_comparison_p
11991 && XEXP (op0, 1) == const1_rtx
11992 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT)
11994 rtx shift_op = XEXP (XEXP (op0, 0), 0);
11995 rtx shift_count = XEXP (XEXP (op0, 0), 1);
11997 if (GET_CODE (shift_op) == NOT
11998 || (GET_CODE (shift_op) == XOR
11999 && CONST_INT_P (XEXP (shift_op, 1))
12000 && CONST_INT_P (shift_count)
12001 && HWI_COMPUTABLE_MODE_P (mode)
12002 && (UINTVAL (XEXP (shift_op, 1))
12003 == (unsigned HOST_WIDE_INT) 1
12004 << INTVAL (shift_count))))
12007 = gen_rtx_LSHIFTRT (mode, XEXP (shift_op, 0), shift_count);
12008 op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
12009 code = (code == NE ? EQ : NE);
12010 continue;
12013 break;
12015 case ASHIFT:
12016 /* If we have (compare (ashift FOO N) (const_int C)) and
12017 the high order N bits of FOO (N+1 if an inequality comparison)
12018 are known to be zero, we can do this by comparing FOO with C
12019 shifted right N bits so long as the low-order N bits of C are
12020 zero. */
12021 if (CONST_INT_P (XEXP (op0, 1))
12022 && INTVAL (XEXP (op0, 1)) >= 0
12023 && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
12024 < HOST_BITS_PER_WIDE_INT)
12025 && (((unsigned HOST_WIDE_INT) const_op
12026 & (((unsigned HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1)))
12027 - 1)) == 0)
12028 && mode_width <= HOST_BITS_PER_WIDE_INT
12029 && (nonzero_bits (XEXP (op0, 0), mode)
12030 & ~(mask >> (INTVAL (XEXP (op0, 1))
12031 + ! equality_comparison_p))) == 0)
12033 /* We must perform a logical shift, not an arithmetic one,
12034 as we want the top N bits of C to be zero. */
12035 unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
12037 temp >>= INTVAL (XEXP (op0, 1));
12038 op1 = gen_int_mode (temp, mode);
12039 op0 = XEXP (op0, 0);
12040 continue;
12043 /* If we are doing a sign bit comparison, it means we are testing
12044 a particular bit. Convert it to the appropriate AND. */
12045 if (sign_bit_comparison_p && CONST_INT_P (XEXP (op0, 1))
12046 && mode_width <= HOST_BITS_PER_WIDE_INT)
12048 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
12049 ((unsigned HOST_WIDE_INT) 1
12050 << (mode_width - 1
12051 - INTVAL (XEXP (op0, 1)))));
12052 code = (code == LT ? NE : EQ);
12053 continue;
12056 /* If this an equality comparison with zero and we are shifting
12057 the low bit to the sign bit, we can convert this to an AND of the
12058 low-order bit. */
12059 if (const_op == 0 && equality_comparison_p
12060 && CONST_INT_P (XEXP (op0, 1))
12061 && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
12063 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0), 1);
12064 continue;
12066 break;
12068 case ASHIFTRT:
12069 /* If this is an equality comparison with zero, we can do this
12070 as a logical shift, which might be much simpler. */
12071 if (equality_comparison_p && const_op == 0
12072 && CONST_INT_P (XEXP (op0, 1)))
12074 op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
12075 XEXP (op0, 0),
12076 INTVAL (XEXP (op0, 1)));
12077 continue;
12080 /* If OP0 is a sign extension and CODE is not an unsigned comparison,
12081 do the comparison in a narrower mode. */
12082 if (! unsigned_comparison_p
12083 && CONST_INT_P (XEXP (op0, 1))
12084 && GET_CODE (XEXP (op0, 0)) == ASHIFT
12085 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
12086 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
12087 MODE_INT, 1)) != BLKmode
12088 && (((unsigned HOST_WIDE_INT) const_op
12089 + (GET_MODE_MASK (tmode) >> 1) + 1)
12090 <= GET_MODE_MASK (tmode)))
12092 op0 = gen_lowpart (tmode, XEXP (XEXP (op0, 0), 0));
12093 continue;
12096 /* Likewise if OP0 is a PLUS of a sign extension with a
12097 constant, which is usually represented with the PLUS
12098 between the shifts. */
12099 if (! unsigned_comparison_p
12100 && CONST_INT_P (XEXP (op0, 1))
12101 && GET_CODE (XEXP (op0, 0)) == PLUS
12102 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
12103 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
12104 && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
12105 && (tmode = mode_for_size (mode_width - INTVAL (XEXP (op0, 1)),
12106 MODE_INT, 1)) != BLKmode
12107 && (((unsigned HOST_WIDE_INT) const_op
12108 + (GET_MODE_MASK (tmode) >> 1) + 1)
12109 <= GET_MODE_MASK (tmode)))
12111 rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
12112 rtx add_const = XEXP (XEXP (op0, 0), 1);
12113 rtx new_const = simplify_gen_binary (ASHIFTRT, GET_MODE (op0),
12114 add_const, XEXP (op0, 1));
12116 op0 = simplify_gen_binary (PLUS, tmode,
12117 gen_lowpart (tmode, inner),
12118 new_const);
12119 continue;
12122 /* ... fall through ... */
12123 case LSHIFTRT:
12124 /* If we have (compare (xshiftrt FOO N) (const_int C)) and
12125 the low order N bits of FOO are known to be zero, we can do this
12126 by comparing FOO with C shifted left N bits so long as no
12127 overflow occurs. Even if the low order N bits of FOO aren't known
12128 to be zero, if the comparison is >= or < we can use the same
12129 optimization and for > or <= by setting all the low
12130 order N bits in the comparison constant. */
12131 if (CONST_INT_P (XEXP (op0, 1))
12132 && INTVAL (XEXP (op0, 1)) > 0
12133 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
12134 && mode_width <= HOST_BITS_PER_WIDE_INT
12135 && (((unsigned HOST_WIDE_INT) const_op
12136 + (GET_CODE (op0) != LSHIFTRT
12137 ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
12138 + 1)
12139 : 0))
12140 <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
12142 unsigned HOST_WIDE_INT low_bits
12143 = (nonzero_bits (XEXP (op0, 0), mode)
12144 & (((unsigned HOST_WIDE_INT) 1
12145 << INTVAL (XEXP (op0, 1))) - 1));
12146 if (low_bits == 0 || !equality_comparison_p)
12148 /* If the shift was logical, then we must make the condition
12149 unsigned. */
12150 if (GET_CODE (op0) == LSHIFTRT)
12151 code = unsigned_condition (code);
12153 const_op <<= INTVAL (XEXP (op0, 1));
12154 if (low_bits != 0
12155 && (code == GT || code == GTU
12156 || code == LE || code == LEU))
12157 const_op
12158 |= (((HOST_WIDE_INT) 1 << INTVAL (XEXP (op0, 1))) - 1);
12159 op1 = GEN_INT (const_op);
12160 op0 = XEXP (op0, 0);
12161 continue;
12165 /* If we are using this shift to extract just the sign bit, we
12166 can replace this with an LT or GE comparison. */
12167 if (const_op == 0
12168 && (equality_comparison_p || sign_bit_comparison_p)
12169 && CONST_INT_P (XEXP (op0, 1))
12170 && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
12172 op0 = XEXP (op0, 0);
12173 code = (code == NE || code == GT ? LT : GE);
12174 continue;
12176 break;
12178 default:
12179 break;
12182 break;
12185 /* Now make any compound operations involved in this comparison. Then,
12186 check for an outmost SUBREG on OP0 that is not doing anything or is
12187 paradoxical. The latter transformation must only be performed when
12188 it is known that the "extra" bits will be the same in op0 and op1 or
12189 that they don't matter. There are three cases to consider:
12191 1. SUBREG_REG (op0) is a register. In this case the bits are don't
12192 care bits and we can assume they have any convenient value. So
12193 making the transformation is safe.
12195 2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not defined.
12196 In this case the upper bits of op0 are undefined. We should not make
12197 the simplification in that case as we do not know the contents of
12198 those bits.
12200 3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is defined and not
12201 UNKNOWN. In that case we know those bits are zeros or ones. We must
12202 also be sure that they are the same as the upper bits of op1.
12204 We can never remove a SUBREG for a non-equality comparison because
12205 the sign bit is in a different place in the underlying object. */
12207 op0 = make_compound_operation (op0, op1 == const0_rtx ? COMPARE : SET);
12208 op1 = make_compound_operation (op1, SET);
12210 if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
12211 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
12212 && GET_MODE_CLASS (GET_MODE (SUBREG_REG (op0))) == MODE_INT
12213 && (code == NE || code == EQ))
12215 if (paradoxical_subreg_p (op0))
12217 /* For paradoxical subregs, allow case 1 as above. Case 3 isn't
12218 implemented. */
12219 if (REG_P (SUBREG_REG (op0)))
12221 op0 = SUBREG_REG (op0);
12222 op1 = gen_lowpart (GET_MODE (op0), op1);
12225 else if ((GET_MODE_PRECISION (GET_MODE (SUBREG_REG (op0)))
12226 <= HOST_BITS_PER_WIDE_INT)
12227 && (nonzero_bits (SUBREG_REG (op0),
12228 GET_MODE (SUBREG_REG (op0)))
12229 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
12231 tem = gen_lowpart (GET_MODE (SUBREG_REG (op0)), op1);
12233 if ((nonzero_bits (tem, GET_MODE (SUBREG_REG (op0)))
12234 & ~GET_MODE_MASK (GET_MODE (op0))) == 0)
12235 op0 = SUBREG_REG (op0), op1 = tem;
12239 /* We now do the opposite procedure: Some machines don't have compare
12240 insns in all modes. If OP0's mode is an integer mode smaller than a
12241 word and we can't do a compare in that mode, see if there is a larger
12242 mode for which we can do the compare. There are a number of cases in
12243 which we can use the wider mode. */
12245 mode = GET_MODE (op0);
12246 if (mode != VOIDmode && GET_MODE_CLASS (mode) == MODE_INT
12247 && GET_MODE_SIZE (mode) < UNITS_PER_WORD
12248 && ! have_insn_for (COMPARE, mode))
12249 for (tmode = GET_MODE_WIDER_MODE (mode);
12250 (tmode != VOIDmode && HWI_COMPUTABLE_MODE_P (tmode));
12251 tmode = GET_MODE_WIDER_MODE (tmode))
12252 if (have_insn_for (COMPARE, tmode))
12254 int zero_extended;
12256 /* If this is a test for negative, we can make an explicit
12257 test of the sign bit. Test this first so we can use
12258 a paradoxical subreg to extend OP0. */
12260 if (op1 == const0_rtx && (code == LT || code == GE)
12261 && HWI_COMPUTABLE_MODE_P (mode))
12263 unsigned HOST_WIDE_INT sign
12264 = (unsigned HOST_WIDE_INT) 1 << (GET_MODE_BITSIZE (mode) - 1);
12265 op0 = simplify_gen_binary (AND, tmode,
12266 gen_lowpart (tmode, op0),
12267 gen_int_mode (sign, tmode));
12268 code = (code == LT) ? NE : EQ;
12269 break;
12272 /* If the only nonzero bits in OP0 and OP1 are those in the
12273 narrower mode and this is an equality or unsigned comparison,
12274 we can use the wider mode. Similarly for sign-extended
12275 values, in which case it is true for all comparisons. */
12276 zero_extended = ((code == EQ || code == NE
12277 || code == GEU || code == GTU
12278 || code == LEU || code == LTU)
12279 && (nonzero_bits (op0, tmode)
12280 & ~GET_MODE_MASK (mode)) == 0
12281 && ((CONST_INT_P (op1)
12282 || (nonzero_bits (op1, tmode)
12283 & ~GET_MODE_MASK (mode)) == 0)));
12285 if (zero_extended
12286 || ((num_sign_bit_copies (op0, tmode)
12287 > (unsigned int) (GET_MODE_PRECISION (tmode)
12288 - GET_MODE_PRECISION (mode)))
12289 && (num_sign_bit_copies (op1, tmode)
12290 > (unsigned int) (GET_MODE_PRECISION (tmode)
12291 - GET_MODE_PRECISION (mode)))))
12293 /* If OP0 is an AND and we don't have an AND in MODE either,
12294 make a new AND in the proper mode. */
12295 if (GET_CODE (op0) == AND
12296 && !have_insn_for (AND, mode))
12297 op0 = simplify_gen_binary (AND, tmode,
12298 gen_lowpart (tmode,
12299 XEXP (op0, 0)),
12300 gen_lowpart (tmode,
12301 XEXP (op0, 1)));
12302 else
12304 if (zero_extended)
12306 op0 = simplify_gen_unary (ZERO_EXTEND, tmode, op0, mode);
12307 op1 = simplify_gen_unary (ZERO_EXTEND, tmode, op1, mode);
12309 else
12311 op0 = simplify_gen_unary (SIGN_EXTEND, tmode, op0, mode);
12312 op1 = simplify_gen_unary (SIGN_EXTEND, tmode, op1, mode);
12314 break;
12319 /* We may have changed the comparison operands. Re-canonicalize. */
12320 if (swap_commutative_operands_p (op0, op1))
12322 tem = op0, op0 = op1, op1 = tem;
12323 code = swap_condition (code);
12326 /* If this machine only supports a subset of valid comparisons, see if we
12327 can convert an unsupported one into a supported one. */
12328 target_canonicalize_comparison (&code, &op0, &op1, 0);
12330 *pop0 = op0;
12331 *pop1 = op1;
12333 return code;
12336 /* Utility function for record_value_for_reg. Count number of
12337 rtxs in X. */
12338 static int
12339 count_rtxs (rtx x)
12341 enum rtx_code code = GET_CODE (x);
12342 const char *fmt;
12343 int i, j, ret = 1;
12345 if (GET_RTX_CLASS (code) == RTX_BIN_ARITH
12346 || GET_RTX_CLASS (code) == RTX_COMM_ARITH)
12348 rtx x0 = XEXP (x, 0);
12349 rtx x1 = XEXP (x, 1);
12351 if (x0 == x1)
12352 return 1 + 2 * count_rtxs (x0);
12354 if ((GET_RTX_CLASS (GET_CODE (x1)) == RTX_BIN_ARITH
12355 || GET_RTX_CLASS (GET_CODE (x1)) == RTX_COMM_ARITH)
12356 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12357 return 2 + 2 * count_rtxs (x0)
12358 + count_rtxs (x == XEXP (x1, 0)
12359 ? XEXP (x1, 1) : XEXP (x1, 0));
12361 if ((GET_RTX_CLASS (GET_CODE (x0)) == RTX_BIN_ARITH
12362 || GET_RTX_CLASS (GET_CODE (x0)) == RTX_COMM_ARITH)
12363 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12364 return 2 + 2 * count_rtxs (x1)
12365 + count_rtxs (x == XEXP (x0, 0)
12366 ? XEXP (x0, 1) : XEXP (x0, 0));
12369 fmt = GET_RTX_FORMAT (code);
12370 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12371 if (fmt[i] == 'e')
12372 ret += count_rtxs (XEXP (x, i));
12373 else if (fmt[i] == 'E')
12374 for (j = 0; j < XVECLEN (x, i); j++)
12375 ret += count_rtxs (XVECEXP (x, i, j));
12377 return ret;
12380 /* Utility function for following routine. Called when X is part of a value
12381 being stored into last_set_value. Sets last_set_table_tick
12382 for each register mentioned. Similar to mention_regs in cse.c */
12384 static void
12385 update_table_tick (rtx x)
12387 enum rtx_code code = GET_CODE (x);
12388 const char *fmt = GET_RTX_FORMAT (code);
12389 int i, j;
12391 if (code == REG)
12393 unsigned int regno = REGNO (x);
12394 unsigned int endregno = END_REGNO (x);
12395 unsigned int r;
12397 for (r = regno; r < endregno; r++)
12399 reg_stat_type *rsp = &reg_stat[r];
12400 rsp->last_set_table_tick = label_tick;
12403 return;
12406 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
12407 if (fmt[i] == 'e')
12409 /* Check for identical subexpressions. If x contains
12410 identical subexpression we only have to traverse one of
12411 them. */
12412 if (i == 0 && ARITHMETIC_P (x))
12414 /* Note that at this point x1 has already been
12415 processed. */
12416 rtx x0 = XEXP (x, 0);
12417 rtx x1 = XEXP (x, 1);
12419 /* If x0 and x1 are identical then there is no need to
12420 process x0. */
12421 if (x0 == x1)
12422 break;
12424 /* If x0 is identical to a subexpression of x1 then while
12425 processing x1, x0 has already been processed. Thus we
12426 are done with x. */
12427 if (ARITHMETIC_P (x1)
12428 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12429 break;
12431 /* If x1 is identical to a subexpression of x0 then we
12432 still have to process the rest of x0. */
12433 if (ARITHMETIC_P (x0)
12434 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12436 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
12437 break;
12441 update_table_tick (XEXP (x, i));
12443 else if (fmt[i] == 'E')
12444 for (j = 0; j < XVECLEN (x, i); j++)
12445 update_table_tick (XVECEXP (x, i, j));
12448 /* Record that REG is set to VALUE in insn INSN. If VALUE is zero, we
12449 are saying that the register is clobbered and we no longer know its
12450 value. If INSN is zero, don't update reg_stat[].last_set; this is
12451 only permitted with VALUE also zero and is used to invalidate the
12452 register. */
12454 static void
12455 record_value_for_reg (rtx reg, rtx_insn *insn, rtx value)
12457 unsigned int regno = REGNO (reg);
12458 unsigned int endregno = END_REGNO (reg);
12459 unsigned int i;
12460 reg_stat_type *rsp;
12462 /* If VALUE contains REG and we have a previous value for REG, substitute
12463 the previous value. */
12464 if (value && insn && reg_overlap_mentioned_p (reg, value))
12466 rtx tem;
12468 /* Set things up so get_last_value is allowed to see anything set up to
12469 our insn. */
12470 subst_low_luid = DF_INSN_LUID (insn);
12471 tem = get_last_value (reg);
12473 /* If TEM is simply a binary operation with two CLOBBERs as operands,
12474 it isn't going to be useful and will take a lot of time to process,
12475 so just use the CLOBBER. */
12477 if (tem)
12479 if (ARITHMETIC_P (tem)
12480 && GET_CODE (XEXP (tem, 0)) == CLOBBER
12481 && GET_CODE (XEXP (tem, 1)) == CLOBBER)
12482 tem = XEXP (tem, 0);
12483 else if (count_occurrences (value, reg, 1) >= 2)
12485 /* If there are two or more occurrences of REG in VALUE,
12486 prevent the value from growing too much. */
12487 if (count_rtxs (tem) > MAX_LAST_VALUE_RTL)
12488 tem = gen_rtx_CLOBBER (GET_MODE (tem), const0_rtx);
12491 value = replace_rtx (copy_rtx (value), reg, tem);
12495 /* For each register modified, show we don't know its value, that
12496 we don't know about its bitwise content, that its value has been
12497 updated, and that we don't know the location of the death of the
12498 register. */
12499 for (i = regno; i < endregno; i++)
12501 rsp = &reg_stat[i];
12503 if (insn)
12504 rsp->last_set = insn;
12506 rsp->last_set_value = 0;
12507 rsp->last_set_mode = VOIDmode;
12508 rsp->last_set_nonzero_bits = 0;
12509 rsp->last_set_sign_bit_copies = 0;
12510 rsp->last_death = 0;
12511 rsp->truncated_to_mode = VOIDmode;
12514 /* Mark registers that are being referenced in this value. */
12515 if (value)
12516 update_table_tick (value);
12518 /* Now update the status of each register being set.
12519 If someone is using this register in this block, set this register
12520 to invalid since we will get confused between the two lives in this
12521 basic block. This makes using this register always invalid. In cse, we
12522 scan the table to invalidate all entries using this register, but this
12523 is too much work for us. */
12525 for (i = regno; i < endregno; i++)
12527 rsp = &reg_stat[i];
12528 rsp->last_set_label = label_tick;
12529 if (!insn
12530 || (value && rsp->last_set_table_tick >= label_tick_ebb_start))
12531 rsp->last_set_invalid = 1;
12532 else
12533 rsp->last_set_invalid = 0;
12536 /* The value being assigned might refer to X (like in "x++;"). In that
12537 case, we must replace it with (clobber (const_int 0)) to prevent
12538 infinite loops. */
12539 rsp = &reg_stat[regno];
12540 if (value && !get_last_value_validate (&value, insn, label_tick, 0))
12542 value = copy_rtx (value);
12543 if (!get_last_value_validate (&value, insn, label_tick, 1))
12544 value = 0;
12547 /* For the main register being modified, update the value, the mode, the
12548 nonzero bits, and the number of sign bit copies. */
12550 rsp->last_set_value = value;
12552 if (value)
12554 machine_mode mode = GET_MODE (reg);
12555 subst_low_luid = DF_INSN_LUID (insn);
12556 rsp->last_set_mode = mode;
12557 if (GET_MODE_CLASS (mode) == MODE_INT
12558 && HWI_COMPUTABLE_MODE_P (mode))
12559 mode = nonzero_bits_mode;
12560 rsp->last_set_nonzero_bits = nonzero_bits (value, mode);
12561 rsp->last_set_sign_bit_copies
12562 = num_sign_bit_copies (value, GET_MODE (reg));
12566 /* Called via note_stores from record_dead_and_set_regs to handle one
12567 SET or CLOBBER in an insn. DATA is the instruction in which the
12568 set is occurring. */
12570 static void
12571 record_dead_and_set_regs_1 (rtx dest, const_rtx setter, void *data)
12573 rtx_insn *record_dead_insn = (rtx_insn *) data;
12575 if (GET_CODE (dest) == SUBREG)
12576 dest = SUBREG_REG (dest);
12578 if (!record_dead_insn)
12580 if (REG_P (dest))
12581 record_value_for_reg (dest, NULL, NULL_RTX);
12582 return;
12585 if (REG_P (dest))
12587 /* If we are setting the whole register, we know its value. Otherwise
12588 show that we don't know the value. We can handle SUBREG in
12589 some cases. */
12590 if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
12591 record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
12592 else if (GET_CODE (setter) == SET
12593 && GET_CODE (SET_DEST (setter)) == SUBREG
12594 && SUBREG_REG (SET_DEST (setter)) == dest
12595 && GET_MODE_PRECISION (GET_MODE (dest)) <= BITS_PER_WORD
12596 && subreg_lowpart_p (SET_DEST (setter)))
12597 record_value_for_reg (dest, record_dead_insn,
12598 gen_lowpart (GET_MODE (dest),
12599 SET_SRC (setter)));
12600 else
12601 record_value_for_reg (dest, record_dead_insn, NULL_RTX);
12603 else if (MEM_P (dest)
12604 /* Ignore pushes, they clobber nothing. */
12605 && ! push_operand (dest, GET_MODE (dest)))
12606 mem_last_set = DF_INSN_LUID (record_dead_insn);
12609 /* Update the records of when each REG was most recently set or killed
12610 for the things done by INSN. This is the last thing done in processing
12611 INSN in the combiner loop.
12613 We update reg_stat[], in particular fields last_set, last_set_value,
12614 last_set_mode, last_set_nonzero_bits, last_set_sign_bit_copies,
12615 last_death, and also the similar information mem_last_set (which insn
12616 most recently modified memory) and last_call_luid (which insn was the
12617 most recent subroutine call). */
12619 static void
12620 record_dead_and_set_regs (rtx_insn *insn)
12622 rtx link;
12623 unsigned int i;
12625 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
12627 if (REG_NOTE_KIND (link) == REG_DEAD
12628 && REG_P (XEXP (link, 0)))
12630 unsigned int regno = REGNO (XEXP (link, 0));
12631 unsigned int endregno = END_REGNO (XEXP (link, 0));
12633 for (i = regno; i < endregno; i++)
12635 reg_stat_type *rsp;
12637 rsp = &reg_stat[i];
12638 rsp->last_death = insn;
12641 else if (REG_NOTE_KIND (link) == REG_INC)
12642 record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
12645 if (CALL_P (insn))
12647 hard_reg_set_iterator hrsi;
12648 EXECUTE_IF_SET_IN_HARD_REG_SET (regs_invalidated_by_call, 0, i, hrsi)
12650 reg_stat_type *rsp;
12652 rsp = &reg_stat[i];
12653 rsp->last_set_invalid = 1;
12654 rsp->last_set = insn;
12655 rsp->last_set_value = 0;
12656 rsp->last_set_mode = VOIDmode;
12657 rsp->last_set_nonzero_bits = 0;
12658 rsp->last_set_sign_bit_copies = 0;
12659 rsp->last_death = 0;
12660 rsp->truncated_to_mode = VOIDmode;
12663 last_call_luid = mem_last_set = DF_INSN_LUID (insn);
12665 /* We can't combine into a call pattern. Remember, though, that
12666 the return value register is set at this LUID. We could
12667 still replace a register with the return value from the
12668 wrong subroutine call! */
12669 note_stores (PATTERN (insn), record_dead_and_set_regs_1, NULL_RTX);
12671 else
12672 note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
12675 /* If a SUBREG has the promoted bit set, it is in fact a property of the
12676 register present in the SUBREG, so for each such SUBREG go back and
12677 adjust nonzero and sign bit information of the registers that are
12678 known to have some zero/sign bits set.
12680 This is needed because when combine blows the SUBREGs away, the
12681 information on zero/sign bits is lost and further combines can be
12682 missed because of that. */
12684 static void
12685 record_promoted_value (rtx_insn *insn, rtx subreg)
12687 struct insn_link *links;
12688 rtx set;
12689 unsigned int regno = REGNO (SUBREG_REG (subreg));
12690 machine_mode mode = GET_MODE (subreg);
12692 if (GET_MODE_PRECISION (mode) > HOST_BITS_PER_WIDE_INT)
12693 return;
12695 for (links = LOG_LINKS (insn); links;)
12697 reg_stat_type *rsp;
12699 insn = links->insn;
12700 set = single_set (insn);
12702 if (! set || !REG_P (SET_DEST (set))
12703 || REGNO (SET_DEST (set)) != regno
12704 || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
12706 links = links->next;
12707 continue;
12710 rsp = &reg_stat[regno];
12711 if (rsp->last_set == insn)
12713 if (SUBREG_PROMOTED_UNSIGNED_P (subreg))
12714 rsp->last_set_nonzero_bits &= GET_MODE_MASK (mode);
12717 if (REG_P (SET_SRC (set)))
12719 regno = REGNO (SET_SRC (set));
12720 links = LOG_LINKS (insn);
12722 else
12723 break;
12727 /* Check if X, a register, is known to contain a value already
12728 truncated to MODE. In this case we can use a subreg to refer to
12729 the truncated value even though in the generic case we would need
12730 an explicit truncation. */
12732 static bool
12733 reg_truncated_to_mode (machine_mode mode, const_rtx x)
12735 reg_stat_type *rsp = &reg_stat[REGNO (x)];
12736 machine_mode truncated = rsp->truncated_to_mode;
12738 if (truncated == 0
12739 || rsp->truncation_label < label_tick_ebb_start)
12740 return false;
12741 if (GET_MODE_SIZE (truncated) <= GET_MODE_SIZE (mode))
12742 return true;
12743 if (TRULY_NOOP_TRUNCATION_MODES_P (mode, truncated))
12744 return true;
12745 return false;
12748 /* If X is a hard reg or a subreg record the mode that the register is
12749 accessed in. For non-TRULY_NOOP_TRUNCATION targets we might be able
12750 to turn a truncate into a subreg using this information. Return true
12751 if traversing X is complete. */
12753 static bool
12754 record_truncated_value (rtx x)
12756 machine_mode truncated_mode;
12757 reg_stat_type *rsp;
12759 if (GET_CODE (x) == SUBREG && REG_P (SUBREG_REG (x)))
12761 machine_mode original_mode = GET_MODE (SUBREG_REG (x));
12762 truncated_mode = GET_MODE (x);
12764 if (GET_MODE_SIZE (original_mode) <= GET_MODE_SIZE (truncated_mode))
12765 return true;
12767 if (TRULY_NOOP_TRUNCATION_MODES_P (truncated_mode, original_mode))
12768 return true;
12770 x = SUBREG_REG (x);
12772 /* ??? For hard-regs we now record everything. We might be able to
12773 optimize this using last_set_mode. */
12774 else if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
12775 truncated_mode = GET_MODE (x);
12776 else
12777 return false;
12779 rsp = &reg_stat[REGNO (x)];
12780 if (rsp->truncated_to_mode == 0
12781 || rsp->truncation_label < label_tick_ebb_start
12782 || (GET_MODE_SIZE (truncated_mode)
12783 < GET_MODE_SIZE (rsp->truncated_to_mode)))
12785 rsp->truncated_to_mode = truncated_mode;
12786 rsp->truncation_label = label_tick;
12789 return true;
12792 /* Callback for note_uses. Find hardregs and subregs of pseudos and
12793 the modes they are used in. This can help truning TRUNCATEs into
12794 SUBREGs. */
12796 static void
12797 record_truncated_values (rtx *loc, void *data ATTRIBUTE_UNUSED)
12799 subrtx_var_iterator::array_type array;
12800 FOR_EACH_SUBRTX_VAR (iter, array, *loc, NONCONST)
12801 if (record_truncated_value (*iter))
12802 iter.skip_subrtxes ();
12805 /* Scan X for promoted SUBREGs. For each one found,
12806 note what it implies to the registers used in it. */
12808 static void
12809 check_promoted_subreg (rtx_insn *insn, rtx x)
12811 if (GET_CODE (x) == SUBREG
12812 && SUBREG_PROMOTED_VAR_P (x)
12813 && REG_P (SUBREG_REG (x)))
12814 record_promoted_value (insn, x);
12815 else
12817 const char *format = GET_RTX_FORMAT (GET_CODE (x));
12818 int i, j;
12820 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
12821 switch (format[i])
12823 case 'e':
12824 check_promoted_subreg (insn, XEXP (x, i));
12825 break;
12826 case 'V':
12827 case 'E':
12828 if (XVEC (x, i) != 0)
12829 for (j = 0; j < XVECLEN (x, i); j++)
12830 check_promoted_subreg (insn, XVECEXP (x, i, j));
12831 break;
12836 /* Verify that all the registers and memory references mentioned in *LOC are
12837 still valid. *LOC was part of a value set in INSN when label_tick was
12838 equal to TICK. Return 0 if some are not. If REPLACE is nonzero, replace
12839 the invalid references with (clobber (const_int 0)) and return 1. This
12840 replacement is useful because we often can get useful information about
12841 the form of a value (e.g., if it was produced by a shift that always
12842 produces -1 or 0) even though we don't know exactly what registers it
12843 was produced from. */
12845 static int
12846 get_last_value_validate (rtx *loc, rtx_insn *insn, int tick, int replace)
12848 rtx x = *loc;
12849 const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
12850 int len = GET_RTX_LENGTH (GET_CODE (x));
12851 int i, j;
12853 if (REG_P (x))
12855 unsigned int regno = REGNO (x);
12856 unsigned int endregno = END_REGNO (x);
12857 unsigned int j;
12859 for (j = regno; j < endregno; j++)
12861 reg_stat_type *rsp = &reg_stat[j];
12862 if (rsp->last_set_invalid
12863 /* If this is a pseudo-register that was only set once and not
12864 live at the beginning of the function, it is always valid. */
12865 || (! (regno >= FIRST_PSEUDO_REGISTER
12866 && REG_N_SETS (regno) == 1
12867 && (!REGNO_REG_SET_P
12868 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
12869 regno)))
12870 && rsp->last_set_label > tick))
12872 if (replace)
12873 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
12874 return replace;
12878 return 1;
12880 /* If this is a memory reference, make sure that there were no stores after
12881 it that might have clobbered the value. We don't have alias info, so we
12882 assume any store invalidates it. Moreover, we only have local UIDs, so
12883 we also assume that there were stores in the intervening basic blocks. */
12884 else if (MEM_P (x) && !MEM_READONLY_P (x)
12885 && (tick != label_tick || DF_INSN_LUID (insn) <= mem_last_set))
12887 if (replace)
12888 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
12889 return replace;
12892 for (i = 0; i < len; i++)
12894 if (fmt[i] == 'e')
12896 /* Check for identical subexpressions. If x contains
12897 identical subexpression we only have to traverse one of
12898 them. */
12899 if (i == 1 && ARITHMETIC_P (x))
12901 /* Note that at this point x0 has already been checked
12902 and found valid. */
12903 rtx x0 = XEXP (x, 0);
12904 rtx x1 = XEXP (x, 1);
12906 /* If x0 and x1 are identical then x is also valid. */
12907 if (x0 == x1)
12908 return 1;
12910 /* If x1 is identical to a subexpression of x0 then
12911 while checking x0, x1 has already been checked. Thus
12912 it is valid and so as x. */
12913 if (ARITHMETIC_P (x0)
12914 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
12915 return 1;
12917 /* If x0 is identical to a subexpression of x1 then x is
12918 valid iff the rest of x1 is valid. */
12919 if (ARITHMETIC_P (x1)
12920 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
12921 return
12922 get_last_value_validate (&XEXP (x1,
12923 x0 == XEXP (x1, 0) ? 1 : 0),
12924 insn, tick, replace);
12927 if (get_last_value_validate (&XEXP (x, i), insn, tick,
12928 replace) == 0)
12929 return 0;
12931 else if (fmt[i] == 'E')
12932 for (j = 0; j < XVECLEN (x, i); j++)
12933 if (get_last_value_validate (&XVECEXP (x, i, j),
12934 insn, tick, replace) == 0)
12935 return 0;
12938 /* If we haven't found a reason for it to be invalid, it is valid. */
12939 return 1;
12942 /* Get the last value assigned to X, if known. Some registers
12943 in the value may be replaced with (clobber (const_int 0)) if their value
12944 is known longer known reliably. */
12946 static rtx
12947 get_last_value (const_rtx x)
12949 unsigned int regno;
12950 rtx value;
12951 reg_stat_type *rsp;
12953 /* If this is a non-paradoxical SUBREG, get the value of its operand and
12954 then convert it to the desired mode. If this is a paradoxical SUBREG,
12955 we cannot predict what values the "extra" bits might have. */
12956 if (GET_CODE (x) == SUBREG
12957 && subreg_lowpart_p (x)
12958 && !paradoxical_subreg_p (x)
12959 && (value = get_last_value (SUBREG_REG (x))) != 0)
12960 return gen_lowpart (GET_MODE (x), value);
12962 if (!REG_P (x))
12963 return 0;
12965 regno = REGNO (x);
12966 rsp = &reg_stat[regno];
12967 value = rsp->last_set_value;
12969 /* If we don't have a value, or if it isn't for this basic block and
12970 it's either a hard register, set more than once, or it's a live
12971 at the beginning of the function, return 0.
12973 Because if it's not live at the beginning of the function then the reg
12974 is always set before being used (is never used without being set).
12975 And, if it's set only once, and it's always set before use, then all
12976 uses must have the same last value, even if it's not from this basic
12977 block. */
12979 if (value == 0
12980 || (rsp->last_set_label < label_tick_ebb_start
12981 && (regno < FIRST_PSEUDO_REGISTER
12982 || REG_N_SETS (regno) != 1
12983 || REGNO_REG_SET_P
12984 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb), regno))))
12985 return 0;
12987 /* If the value was set in a later insn than the ones we are processing,
12988 we can't use it even if the register was only set once. */
12989 if (rsp->last_set_label == label_tick
12990 && DF_INSN_LUID (rsp->last_set) >= subst_low_luid)
12991 return 0;
12993 /* If the value has all its registers valid, return it. */
12994 if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 0))
12995 return value;
12997 /* Otherwise, make a copy and replace any invalid register with
12998 (clobber (const_int 0)). If that fails for some reason, return 0. */
13000 value = copy_rtx (value);
13001 if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 1))
13002 return value;
13004 return 0;
13007 /* Return nonzero if expression X refers to a REG or to memory
13008 that is set in an instruction more recent than FROM_LUID. */
13010 static int
13011 use_crosses_set_p (const_rtx x, int from_luid)
13013 const char *fmt;
13014 int i;
13015 enum rtx_code code = GET_CODE (x);
13017 if (code == REG)
13019 unsigned int regno = REGNO (x);
13020 unsigned endreg = END_REGNO (x);
13022 #ifdef PUSH_ROUNDING
13023 /* Don't allow uses of the stack pointer to be moved,
13024 because we don't know whether the move crosses a push insn. */
13025 if (regno == STACK_POINTER_REGNUM && PUSH_ARGS)
13026 return 1;
13027 #endif
13028 for (; regno < endreg; regno++)
13030 reg_stat_type *rsp = &reg_stat[regno];
13031 if (rsp->last_set
13032 && rsp->last_set_label == label_tick
13033 && DF_INSN_LUID (rsp->last_set) > from_luid)
13034 return 1;
13036 return 0;
13039 if (code == MEM && mem_last_set > from_luid)
13040 return 1;
13042 fmt = GET_RTX_FORMAT (code);
13044 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
13046 if (fmt[i] == 'E')
13048 int j;
13049 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
13050 if (use_crosses_set_p (XVECEXP (x, i, j), from_luid))
13051 return 1;
13053 else if (fmt[i] == 'e'
13054 && use_crosses_set_p (XEXP (x, i), from_luid))
13055 return 1;
13057 return 0;
13060 /* Define three variables used for communication between the following
13061 routines. */
13063 static unsigned int reg_dead_regno, reg_dead_endregno;
13064 static int reg_dead_flag;
13066 /* Function called via note_stores from reg_dead_at_p.
13068 If DEST is within [reg_dead_regno, reg_dead_endregno), set
13069 reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET. */
13071 static void
13072 reg_dead_at_p_1 (rtx dest, const_rtx x, void *data ATTRIBUTE_UNUSED)
13074 unsigned int regno, endregno;
13076 if (!REG_P (dest))
13077 return;
13079 regno = REGNO (dest);
13080 endregno = END_REGNO (dest);
13081 if (reg_dead_endregno > regno && reg_dead_regno < endregno)
13082 reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
13085 /* Return nonzero if REG is known to be dead at INSN.
13087 We scan backwards from INSN. If we hit a REG_DEAD note or a CLOBBER
13088 referencing REG, it is dead. If we hit a SET referencing REG, it is
13089 live. Otherwise, see if it is live or dead at the start of the basic
13090 block we are in. Hard regs marked as being live in NEWPAT_USED_REGS
13091 must be assumed to be always live. */
13093 static int
13094 reg_dead_at_p (rtx reg, rtx_insn *insn)
13096 basic_block block;
13097 unsigned int i;
13099 /* Set variables for reg_dead_at_p_1. */
13100 reg_dead_regno = REGNO (reg);
13101 reg_dead_endregno = END_REGNO (reg);
13103 reg_dead_flag = 0;
13105 /* Check that reg isn't mentioned in NEWPAT_USED_REGS. For fixed registers
13106 we allow the machine description to decide whether use-and-clobber
13107 patterns are OK. */
13108 if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
13110 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
13111 if (!fixed_regs[i] && TEST_HARD_REG_BIT (newpat_used_regs, i))
13112 return 0;
13115 /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, or
13116 beginning of basic block. */
13117 block = BLOCK_FOR_INSN (insn);
13118 for (;;)
13120 if (INSN_P (insn))
13122 if (find_regno_note (insn, REG_UNUSED, reg_dead_regno))
13123 return 1;
13125 note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
13126 if (reg_dead_flag)
13127 return reg_dead_flag == 1 ? 1 : 0;
13129 if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
13130 return 1;
13133 if (insn == BB_HEAD (block))
13134 break;
13136 insn = PREV_INSN (insn);
13139 /* Look at live-in sets for the basic block that we were in. */
13140 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
13141 if (REGNO_REG_SET_P (df_get_live_in (block), i))
13142 return 0;
13144 return 1;
13147 /* Note hard registers in X that are used. */
13149 static void
13150 mark_used_regs_combine (rtx x)
13152 RTX_CODE code = GET_CODE (x);
13153 unsigned int regno;
13154 int i;
13156 switch (code)
13158 case LABEL_REF:
13159 case SYMBOL_REF:
13160 case CONST:
13161 CASE_CONST_ANY:
13162 case PC:
13163 case ADDR_VEC:
13164 case ADDR_DIFF_VEC:
13165 case ASM_INPUT:
13166 #ifdef HAVE_cc0
13167 /* CC0 must die in the insn after it is set, so we don't need to take
13168 special note of it here. */
13169 case CC0:
13170 #endif
13171 return;
13173 case CLOBBER:
13174 /* If we are clobbering a MEM, mark any hard registers inside the
13175 address as used. */
13176 if (MEM_P (XEXP (x, 0)))
13177 mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
13178 return;
13180 case REG:
13181 regno = REGNO (x);
13182 /* A hard reg in a wide mode may really be multiple registers.
13183 If so, mark all of them just like the first. */
13184 if (regno < FIRST_PSEUDO_REGISTER)
13186 /* None of this applies to the stack, frame or arg pointers. */
13187 if (regno == STACK_POINTER_REGNUM
13188 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
13189 || regno == HARD_FRAME_POINTER_REGNUM
13190 #endif
13191 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
13192 || (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
13193 #endif
13194 || regno == FRAME_POINTER_REGNUM)
13195 return;
13197 add_to_hard_reg_set (&newpat_used_regs, GET_MODE (x), regno);
13199 return;
13201 case SET:
13203 /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
13204 the address. */
13205 rtx testreg = SET_DEST (x);
13207 while (GET_CODE (testreg) == SUBREG
13208 || GET_CODE (testreg) == ZERO_EXTRACT
13209 || GET_CODE (testreg) == STRICT_LOW_PART)
13210 testreg = XEXP (testreg, 0);
13212 if (MEM_P (testreg))
13213 mark_used_regs_combine (XEXP (testreg, 0));
13215 mark_used_regs_combine (SET_SRC (x));
13217 return;
13219 default:
13220 break;
13223 /* Recursively scan the operands of this expression. */
13226 const char *fmt = GET_RTX_FORMAT (code);
13228 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
13230 if (fmt[i] == 'e')
13231 mark_used_regs_combine (XEXP (x, i));
13232 else if (fmt[i] == 'E')
13234 int j;
13236 for (j = 0; j < XVECLEN (x, i); j++)
13237 mark_used_regs_combine (XVECEXP (x, i, j));
13243 /* Remove register number REGNO from the dead registers list of INSN.
13245 Return the note used to record the death, if there was one. */
13248 remove_death (unsigned int regno, rtx_insn *insn)
13250 rtx note = find_regno_note (insn, REG_DEAD, regno);
13252 if (note)
13253 remove_note (insn, note);
13255 return note;
13258 /* For each register (hardware or pseudo) used within expression X, if its
13259 death is in an instruction with luid between FROM_LUID (inclusive) and
13260 TO_INSN (exclusive), put a REG_DEAD note for that register in the
13261 list headed by PNOTES.
13263 That said, don't move registers killed by maybe_kill_insn.
13265 This is done when X is being merged by combination into TO_INSN. These
13266 notes will then be distributed as needed. */
13268 static void
13269 move_deaths (rtx x, rtx maybe_kill_insn, int from_luid, rtx_insn *to_insn,
13270 rtx *pnotes)
13272 const char *fmt;
13273 int len, i;
13274 enum rtx_code code = GET_CODE (x);
13276 if (code == REG)
13278 unsigned int regno = REGNO (x);
13279 rtx_insn *where_dead = reg_stat[regno].last_death;
13281 /* Don't move the register if it gets killed in between from and to. */
13282 if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
13283 && ! reg_referenced_p (x, maybe_kill_insn))
13284 return;
13286 if (where_dead
13287 && BLOCK_FOR_INSN (where_dead) == BLOCK_FOR_INSN (to_insn)
13288 && DF_INSN_LUID (where_dead) >= from_luid
13289 && DF_INSN_LUID (where_dead) < DF_INSN_LUID (to_insn))
13291 rtx note = remove_death (regno, where_dead);
13293 /* It is possible for the call above to return 0. This can occur
13294 when last_death points to I2 or I1 that we combined with.
13295 In that case make a new note.
13297 We must also check for the case where X is a hard register
13298 and NOTE is a death note for a range of hard registers
13299 including X. In that case, we must put REG_DEAD notes for
13300 the remaining registers in place of NOTE. */
13302 if (note != 0 && regno < FIRST_PSEUDO_REGISTER
13303 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
13304 > GET_MODE_SIZE (GET_MODE (x))))
13306 unsigned int deadregno = REGNO (XEXP (note, 0));
13307 unsigned int deadend = END_HARD_REGNO (XEXP (note, 0));
13308 unsigned int ourend = END_HARD_REGNO (x);
13309 unsigned int i;
13311 for (i = deadregno; i < deadend; i++)
13312 if (i < regno || i >= ourend)
13313 add_reg_note (where_dead, REG_DEAD, regno_reg_rtx[i]);
13316 /* If we didn't find any note, or if we found a REG_DEAD note that
13317 covers only part of the given reg, and we have a multi-reg hard
13318 register, then to be safe we must check for REG_DEAD notes
13319 for each register other than the first. They could have
13320 their own REG_DEAD notes lying around. */
13321 else if ((note == 0
13322 || (note != 0
13323 && (GET_MODE_SIZE (GET_MODE (XEXP (note, 0)))
13324 < GET_MODE_SIZE (GET_MODE (x)))))
13325 && regno < FIRST_PSEUDO_REGISTER
13326 && hard_regno_nregs[regno][GET_MODE (x)] > 1)
13328 unsigned int ourend = END_HARD_REGNO (x);
13329 unsigned int i, offset;
13330 rtx oldnotes = 0;
13332 if (note)
13333 offset = hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))];
13334 else
13335 offset = 1;
13337 for (i = regno + offset; i < ourend; i++)
13338 move_deaths (regno_reg_rtx[i],
13339 maybe_kill_insn, from_luid, to_insn, &oldnotes);
13342 if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
13344 XEXP (note, 1) = *pnotes;
13345 *pnotes = note;
13347 else
13348 *pnotes = alloc_reg_note (REG_DEAD, x, *pnotes);
13351 return;
13354 else if (GET_CODE (x) == SET)
13356 rtx dest = SET_DEST (x);
13358 move_deaths (SET_SRC (x), maybe_kill_insn, from_luid, to_insn, pnotes);
13360 /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
13361 that accesses one word of a multi-word item, some
13362 piece of everything register in the expression is used by
13363 this insn, so remove any old death. */
13364 /* ??? So why do we test for equality of the sizes? */
13366 if (GET_CODE (dest) == ZERO_EXTRACT
13367 || GET_CODE (dest) == STRICT_LOW_PART
13368 || (GET_CODE (dest) == SUBREG
13369 && (((GET_MODE_SIZE (GET_MODE (dest))
13370 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
13371 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
13372 + UNITS_PER_WORD - 1) / UNITS_PER_WORD))))
13374 move_deaths (dest, maybe_kill_insn, from_luid, to_insn, pnotes);
13375 return;
13378 /* If this is some other SUBREG, we know it replaces the entire
13379 value, so use that as the destination. */
13380 if (GET_CODE (dest) == SUBREG)
13381 dest = SUBREG_REG (dest);
13383 /* If this is a MEM, adjust deaths of anything used in the address.
13384 For a REG (the only other possibility), the entire value is
13385 being replaced so the old value is not used in this insn. */
13387 if (MEM_P (dest))
13388 move_deaths (XEXP (dest, 0), maybe_kill_insn, from_luid,
13389 to_insn, pnotes);
13390 return;
13393 else if (GET_CODE (x) == CLOBBER)
13394 return;
13396 len = GET_RTX_LENGTH (code);
13397 fmt = GET_RTX_FORMAT (code);
13399 for (i = 0; i < len; i++)
13401 if (fmt[i] == 'E')
13403 int j;
13404 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
13405 move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_luid,
13406 to_insn, pnotes);
13408 else if (fmt[i] == 'e')
13409 move_deaths (XEXP (x, i), maybe_kill_insn, from_luid, to_insn, pnotes);
13413 /* Return 1 if X is the target of a bit-field assignment in BODY, the
13414 pattern of an insn. X must be a REG. */
13416 static int
13417 reg_bitfield_target_p (rtx x, rtx body)
13419 int i;
13421 if (GET_CODE (body) == SET)
13423 rtx dest = SET_DEST (body);
13424 rtx target;
13425 unsigned int regno, tregno, endregno, endtregno;
13427 if (GET_CODE (dest) == ZERO_EXTRACT)
13428 target = XEXP (dest, 0);
13429 else if (GET_CODE (dest) == STRICT_LOW_PART)
13430 target = SUBREG_REG (XEXP (dest, 0));
13431 else
13432 return 0;
13434 if (GET_CODE (target) == SUBREG)
13435 target = SUBREG_REG (target);
13437 if (!REG_P (target))
13438 return 0;
13440 tregno = REGNO (target), regno = REGNO (x);
13441 if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
13442 return target == x;
13444 endtregno = end_hard_regno (GET_MODE (target), tregno);
13445 endregno = end_hard_regno (GET_MODE (x), regno);
13447 return endregno > tregno && regno < endtregno;
13450 else if (GET_CODE (body) == PARALLEL)
13451 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
13452 if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
13453 return 1;
13455 return 0;
13458 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
13459 as appropriate. I3 and I2 are the insns resulting from the combination
13460 insns including FROM (I2 may be zero).
13462 ELIM_I2 and ELIM_I1 are either zero or registers that we know will
13463 not need REG_DEAD notes because they are being substituted for. This
13464 saves searching in the most common cases.
13466 Each note in the list is either ignored or placed on some insns, depending
13467 on the type of note. */
13469 static void
13470 distribute_notes (rtx notes, rtx_insn *from_insn, rtx_insn *i3, rtx_insn *i2,
13471 rtx elim_i2, rtx elim_i1, rtx elim_i0)
13473 rtx note, next_note;
13474 rtx tem_note;
13475 rtx_insn *tem_insn;
13477 for (note = notes; note; note = next_note)
13479 rtx_insn *place = 0, *place2 = 0;
13481 next_note = XEXP (note, 1);
13482 switch (REG_NOTE_KIND (note))
13484 case REG_BR_PROB:
13485 case REG_BR_PRED:
13486 /* Doesn't matter much where we put this, as long as it's somewhere.
13487 It is preferable to keep these notes on branches, which is most
13488 likely to be i3. */
13489 place = i3;
13490 break;
13492 case REG_NON_LOCAL_GOTO:
13493 if (JUMP_P (i3))
13494 place = i3;
13495 else
13497 gcc_assert (i2 && JUMP_P (i2));
13498 place = i2;
13500 break;
13502 case REG_EH_REGION:
13503 /* These notes must remain with the call or trapping instruction. */
13504 if (CALL_P (i3))
13505 place = i3;
13506 else if (i2 && CALL_P (i2))
13507 place = i2;
13508 else
13510 gcc_assert (cfun->can_throw_non_call_exceptions);
13511 if (may_trap_p (i3))
13512 place = i3;
13513 else if (i2 && may_trap_p (i2))
13514 place = i2;
13515 /* ??? Otherwise assume we've combined things such that we
13516 can now prove that the instructions can't trap. Drop the
13517 note in this case. */
13519 break;
13521 case REG_ARGS_SIZE:
13522 /* ??? How to distribute between i3-i1. Assume i3 contains the
13523 entire adjustment. Assert i3 contains at least some adjust. */
13524 if (!noop_move_p (i3))
13526 int old_size, args_size = INTVAL (XEXP (note, 0));
13527 /* fixup_args_size_notes looks at REG_NORETURN note,
13528 so ensure the note is placed there first. */
13529 if (CALL_P (i3))
13531 rtx *np;
13532 for (np = &next_note; *np; np = &XEXP (*np, 1))
13533 if (REG_NOTE_KIND (*np) == REG_NORETURN)
13535 rtx n = *np;
13536 *np = XEXP (n, 1);
13537 XEXP (n, 1) = REG_NOTES (i3);
13538 REG_NOTES (i3) = n;
13539 break;
13542 old_size = fixup_args_size_notes (PREV_INSN (i3), i3, args_size);
13543 /* emit_call_1 adds for !ACCUMULATE_OUTGOING_ARGS
13544 REG_ARGS_SIZE note to all noreturn calls, allow that here. */
13545 gcc_assert (old_size != args_size
13546 || (CALL_P (i3)
13547 && !ACCUMULATE_OUTGOING_ARGS
13548 && find_reg_note (i3, REG_NORETURN, NULL_RTX)));
13550 break;
13552 case REG_NORETURN:
13553 case REG_SETJMP:
13554 case REG_TM:
13555 case REG_CALL_DECL:
13556 /* These notes must remain with the call. It should not be
13557 possible for both I2 and I3 to be a call. */
13558 if (CALL_P (i3))
13559 place = i3;
13560 else
13562 gcc_assert (i2 && CALL_P (i2));
13563 place = i2;
13565 break;
13567 case REG_UNUSED:
13568 /* Any clobbers for i3 may still exist, and so we must process
13569 REG_UNUSED notes from that insn.
13571 Any clobbers from i2 or i1 can only exist if they were added by
13572 recog_for_combine. In that case, recog_for_combine created the
13573 necessary REG_UNUSED notes. Trying to keep any original
13574 REG_UNUSED notes from these insns can cause incorrect output
13575 if it is for the same register as the original i3 dest.
13576 In that case, we will notice that the register is set in i3,
13577 and then add a REG_UNUSED note for the destination of i3, which
13578 is wrong. However, it is possible to have REG_UNUSED notes from
13579 i2 or i1 for register which were both used and clobbered, so
13580 we keep notes from i2 or i1 if they will turn into REG_DEAD
13581 notes. */
13583 /* If this register is set or clobbered in I3, put the note there
13584 unless there is one already. */
13585 if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
13587 if (from_insn != i3)
13588 break;
13590 if (! (REG_P (XEXP (note, 0))
13591 ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
13592 : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
13593 place = i3;
13595 /* Otherwise, if this register is used by I3, then this register
13596 now dies here, so we must put a REG_DEAD note here unless there
13597 is one already. */
13598 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
13599 && ! (REG_P (XEXP (note, 0))
13600 ? find_regno_note (i3, REG_DEAD,
13601 REGNO (XEXP (note, 0)))
13602 : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
13604 PUT_REG_NOTE_KIND (note, REG_DEAD);
13605 place = i3;
13607 break;
13609 case REG_EQUAL:
13610 case REG_EQUIV:
13611 case REG_NOALIAS:
13612 /* These notes say something about results of an insn. We can
13613 only support them if they used to be on I3 in which case they
13614 remain on I3. Otherwise they are ignored.
13616 If the note refers to an expression that is not a constant, we
13617 must also ignore the note since we cannot tell whether the
13618 equivalence is still true. It might be possible to do
13619 slightly better than this (we only have a problem if I2DEST
13620 or I1DEST is present in the expression), but it doesn't
13621 seem worth the trouble. */
13623 if (from_insn == i3
13624 && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
13625 place = i3;
13626 break;
13628 case REG_INC:
13629 /* These notes say something about how a register is used. They must
13630 be present on any use of the register in I2 or I3. */
13631 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
13632 place = i3;
13634 if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
13636 if (place)
13637 place2 = i2;
13638 else
13639 place = i2;
13641 break;
13643 case REG_LABEL_TARGET:
13644 case REG_LABEL_OPERAND:
13645 /* This can show up in several ways -- either directly in the
13646 pattern, or hidden off in the constant pool with (or without?)
13647 a REG_EQUAL note. */
13648 /* ??? Ignore the without-reg_equal-note problem for now. */
13649 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
13650 || ((tem_note = find_reg_note (i3, REG_EQUAL, NULL_RTX))
13651 && GET_CODE (XEXP (tem_note, 0)) == LABEL_REF
13652 && LABEL_REF_LABEL (XEXP (tem_note, 0)) == XEXP (note, 0)))
13653 place = i3;
13655 if (i2
13656 && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
13657 || ((tem_note = find_reg_note (i2, REG_EQUAL, NULL_RTX))
13658 && GET_CODE (XEXP (tem_note, 0)) == LABEL_REF
13659 && LABEL_REF_LABEL (XEXP (tem_note, 0)) == XEXP (note, 0))))
13661 if (place)
13662 place2 = i2;
13663 else
13664 place = i2;
13667 /* For REG_LABEL_TARGET on a JUMP_P, we prefer to put the note
13668 as a JUMP_LABEL or decrement LABEL_NUSES if it's already
13669 there. */
13670 if (place && JUMP_P (place)
13671 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
13672 && (JUMP_LABEL (place) == NULL
13673 || JUMP_LABEL (place) == XEXP (note, 0)))
13675 rtx label = JUMP_LABEL (place);
13677 if (!label)
13678 JUMP_LABEL (place) = XEXP (note, 0);
13679 else if (LABEL_P (label))
13680 LABEL_NUSES (label)--;
13683 if (place2 && JUMP_P (place2)
13684 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
13685 && (JUMP_LABEL (place2) == NULL
13686 || JUMP_LABEL (place2) == XEXP (note, 0)))
13688 rtx label = JUMP_LABEL (place2);
13690 if (!label)
13691 JUMP_LABEL (place2) = XEXP (note, 0);
13692 else if (LABEL_P (label))
13693 LABEL_NUSES (label)--;
13694 place2 = 0;
13696 break;
13698 case REG_NONNEG:
13699 /* This note says something about the value of a register prior
13700 to the execution of an insn. It is too much trouble to see
13701 if the note is still correct in all situations. It is better
13702 to simply delete it. */
13703 break;
13705 case REG_DEAD:
13706 /* If we replaced the right hand side of FROM_INSN with a
13707 REG_EQUAL note, the original use of the dying register
13708 will not have been combined into I3 and I2. In such cases,
13709 FROM_INSN is guaranteed to be the first of the combined
13710 instructions, so we simply need to search back before
13711 FROM_INSN for the previous use or set of this register,
13712 then alter the notes there appropriately.
13714 If the register is used as an input in I3, it dies there.
13715 Similarly for I2, if it is nonzero and adjacent to I3.
13717 If the register is not used as an input in either I3 or I2
13718 and it is not one of the registers we were supposed to eliminate,
13719 there are two possibilities. We might have a non-adjacent I2
13720 or we might have somehow eliminated an additional register
13721 from a computation. For example, we might have had A & B where
13722 we discover that B will always be zero. In this case we will
13723 eliminate the reference to A.
13725 In both cases, we must search to see if we can find a previous
13726 use of A and put the death note there. */
13728 if (from_insn
13729 && from_insn == i2mod
13730 && !reg_overlap_mentioned_p (XEXP (note, 0), i2mod_new_rhs))
13731 tem_insn = from_insn;
13732 else
13734 if (from_insn
13735 && CALL_P (from_insn)
13736 && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
13737 place = from_insn;
13738 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
13739 place = i3;
13740 else if (i2 != 0 && next_nonnote_nondebug_insn (i2) == i3
13741 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
13742 place = i2;
13743 else if ((rtx_equal_p (XEXP (note, 0), elim_i2)
13744 && !(i2mod
13745 && reg_overlap_mentioned_p (XEXP (note, 0),
13746 i2mod_old_rhs)))
13747 || rtx_equal_p (XEXP (note, 0), elim_i1)
13748 || rtx_equal_p (XEXP (note, 0), elim_i0))
13749 break;
13750 tem_insn = i3;
13751 /* If the new I2 sets the same register that is marked dead
13752 in the note, the note now should not be put on I2, as the
13753 note refers to a previous incarnation of the reg. */
13754 if (i2 != 0 && reg_set_p (XEXP (note, 0), PATTERN (i2)))
13755 tem_insn = i2;
13758 if (place == 0)
13760 basic_block bb = this_basic_block;
13762 for (tem_insn = PREV_INSN (tem_insn); place == 0; tem_insn = PREV_INSN (tem_insn))
13764 if (!NONDEBUG_INSN_P (tem_insn))
13766 if (tem_insn == BB_HEAD (bb))
13767 break;
13768 continue;
13771 /* If the register is being set at TEM_INSN, see if that is all
13772 TEM_INSN is doing. If so, delete TEM_INSN. Otherwise, make this
13773 into a REG_UNUSED note instead. Don't delete sets to
13774 global register vars. */
13775 if ((REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER
13776 || !global_regs[REGNO (XEXP (note, 0))])
13777 && reg_set_p (XEXP (note, 0), PATTERN (tem_insn)))
13779 rtx set = single_set (tem_insn);
13780 rtx inner_dest = 0;
13781 #ifdef HAVE_cc0
13782 rtx_insn *cc0_setter = NULL;
13783 #endif
13785 if (set != 0)
13786 for (inner_dest = SET_DEST (set);
13787 (GET_CODE (inner_dest) == STRICT_LOW_PART
13788 || GET_CODE (inner_dest) == SUBREG
13789 || GET_CODE (inner_dest) == ZERO_EXTRACT);
13790 inner_dest = XEXP (inner_dest, 0))
13793 /* Verify that it was the set, and not a clobber that
13794 modified the register.
13796 CC0 targets must be careful to maintain setter/user
13797 pairs. If we cannot delete the setter due to side
13798 effects, mark the user with an UNUSED note instead
13799 of deleting it. */
13801 if (set != 0 && ! side_effects_p (SET_SRC (set))
13802 && rtx_equal_p (XEXP (note, 0), inner_dest)
13803 #ifdef HAVE_cc0
13804 && (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
13805 || ((cc0_setter = prev_cc0_setter (tem_insn)) != NULL
13806 && sets_cc0_p (PATTERN (cc0_setter)) > 0))
13807 #endif
13810 /* Move the notes and links of TEM_INSN elsewhere.
13811 This might delete other dead insns recursively.
13812 First set the pattern to something that won't use
13813 any register. */
13814 rtx old_notes = REG_NOTES (tem_insn);
13816 PATTERN (tem_insn) = pc_rtx;
13817 REG_NOTES (tem_insn) = NULL;
13819 distribute_notes (old_notes, tem_insn, tem_insn, NULL,
13820 NULL_RTX, NULL_RTX, NULL_RTX);
13821 distribute_links (LOG_LINKS (tem_insn));
13823 SET_INSN_DELETED (tem_insn);
13824 if (tem_insn == i2)
13825 i2 = NULL;
13827 #ifdef HAVE_cc0
13828 /* Delete the setter too. */
13829 if (cc0_setter)
13831 PATTERN (cc0_setter) = pc_rtx;
13832 old_notes = REG_NOTES (cc0_setter);
13833 REG_NOTES (cc0_setter) = NULL;
13835 distribute_notes (old_notes, cc0_setter,
13836 cc0_setter, NULL,
13837 NULL_RTX, NULL_RTX, NULL_RTX);
13838 distribute_links (LOG_LINKS (cc0_setter));
13840 SET_INSN_DELETED (cc0_setter);
13841 if (cc0_setter == i2)
13842 i2 = NULL;
13844 #endif
13846 else
13848 PUT_REG_NOTE_KIND (note, REG_UNUSED);
13850 /* If there isn't already a REG_UNUSED note, put one
13851 here. Do not place a REG_DEAD note, even if
13852 the register is also used here; that would not
13853 match the algorithm used in lifetime analysis
13854 and can cause the consistency check in the
13855 scheduler to fail. */
13856 if (! find_regno_note (tem_insn, REG_UNUSED,
13857 REGNO (XEXP (note, 0))))
13858 place = tem_insn;
13859 break;
13862 else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem_insn))
13863 || (CALL_P (tem_insn)
13864 && find_reg_fusage (tem_insn, USE, XEXP (note, 0))))
13866 place = tem_insn;
13868 /* If we are doing a 3->2 combination, and we have a
13869 register which formerly died in i3 and was not used
13870 by i2, which now no longer dies in i3 and is used in
13871 i2 but does not die in i2, and place is between i2
13872 and i3, then we may need to move a link from place to
13873 i2. */
13874 if (i2 && DF_INSN_LUID (place) > DF_INSN_LUID (i2)
13875 && from_insn
13876 && DF_INSN_LUID (from_insn) > DF_INSN_LUID (i2)
13877 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
13879 struct insn_link *links = LOG_LINKS (place);
13880 LOG_LINKS (place) = NULL;
13881 distribute_links (links);
13883 break;
13886 if (tem_insn == BB_HEAD (bb))
13887 break;
13892 /* If the register is set or already dead at PLACE, we needn't do
13893 anything with this note if it is still a REG_DEAD note.
13894 We check here if it is set at all, not if is it totally replaced,
13895 which is what `dead_or_set_p' checks, so also check for it being
13896 set partially. */
13898 if (place && REG_NOTE_KIND (note) == REG_DEAD)
13900 unsigned int regno = REGNO (XEXP (note, 0));
13901 reg_stat_type *rsp = &reg_stat[regno];
13903 if (dead_or_set_p (place, XEXP (note, 0))
13904 || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
13906 /* Unless the register previously died in PLACE, clear
13907 last_death. [I no longer understand why this is
13908 being done.] */
13909 if (rsp->last_death != place)
13910 rsp->last_death = 0;
13911 place = 0;
13913 else
13914 rsp->last_death = place;
13916 /* If this is a death note for a hard reg that is occupying
13917 multiple registers, ensure that we are still using all
13918 parts of the object. If we find a piece of the object
13919 that is unused, we must arrange for an appropriate REG_DEAD
13920 note to be added for it. However, we can't just emit a USE
13921 and tag the note to it, since the register might actually
13922 be dead; so we recourse, and the recursive call then finds
13923 the previous insn that used this register. */
13925 if (place && regno < FIRST_PSEUDO_REGISTER
13926 && hard_regno_nregs[regno][GET_MODE (XEXP (note, 0))] > 1)
13928 unsigned int endregno = END_HARD_REGNO (XEXP (note, 0));
13929 bool all_used = true;
13930 unsigned int i;
13932 for (i = regno; i < endregno; i++)
13933 if ((! refers_to_regno_p (i, PATTERN (place))
13934 && ! find_regno_fusage (place, USE, i))
13935 || dead_or_set_regno_p (place, i))
13937 all_used = false;
13938 break;
13941 if (! all_used)
13943 /* Put only REG_DEAD notes for pieces that are
13944 not already dead or set. */
13946 for (i = regno; i < endregno;
13947 i += hard_regno_nregs[i][reg_raw_mode[i]])
13949 rtx piece = regno_reg_rtx[i];
13950 basic_block bb = this_basic_block;
13952 if (! dead_or_set_p (place, piece)
13953 && ! reg_bitfield_target_p (piece,
13954 PATTERN (place)))
13956 rtx new_note = alloc_reg_note (REG_DEAD, piece,
13957 NULL_RTX);
13959 distribute_notes (new_note, place, place,
13960 NULL, NULL_RTX, NULL_RTX,
13961 NULL_RTX);
13963 else if (! refers_to_regno_p (i, PATTERN (place))
13964 && ! find_regno_fusage (place, USE, i))
13965 for (tem_insn = PREV_INSN (place); ;
13966 tem_insn = PREV_INSN (tem_insn))
13968 if (!NONDEBUG_INSN_P (tem_insn))
13970 if (tem_insn == BB_HEAD (bb))
13971 break;
13972 continue;
13974 if (dead_or_set_p (tem_insn, piece)
13975 || reg_bitfield_target_p (piece,
13976 PATTERN (tem_insn)))
13978 add_reg_note (tem_insn, REG_UNUSED, piece);
13979 break;
13984 place = 0;
13988 break;
13990 default:
13991 /* Any other notes should not be present at this point in the
13992 compilation. */
13993 gcc_unreachable ();
13996 if (place)
13998 XEXP (note, 1) = REG_NOTES (place);
13999 REG_NOTES (place) = note;
14002 if (place2)
14003 add_shallow_copy_of_reg_note (place2, note);
14007 /* Similarly to above, distribute the LOG_LINKS that used to be present on
14008 I3, I2, and I1 to new locations. This is also called to add a link
14009 pointing at I3 when I3's destination is changed. */
14011 static void
14012 distribute_links (struct insn_link *links)
14014 struct insn_link *link, *next_link;
14016 for (link = links; link; link = next_link)
14018 rtx_insn *place = 0;
14019 rtx_insn *insn;
14020 rtx set, reg;
14022 next_link = link->next;
14024 /* If the insn that this link points to is a NOTE, ignore it. */
14025 if (NOTE_P (link->insn))
14026 continue;
14028 set = 0;
14029 rtx pat = PATTERN (link->insn);
14030 if (GET_CODE (pat) == SET)
14031 set = pat;
14032 else if (GET_CODE (pat) == PARALLEL)
14034 int i;
14035 for (i = 0; i < XVECLEN (pat, 0); i++)
14037 set = XVECEXP (pat, 0, i);
14038 if (GET_CODE (set) != SET)
14039 continue;
14041 reg = SET_DEST (set);
14042 while (GET_CODE (reg) == ZERO_EXTRACT
14043 || GET_CODE (reg) == STRICT_LOW_PART
14044 || GET_CODE (reg) == SUBREG)
14045 reg = XEXP (reg, 0);
14047 if (!REG_P (reg))
14048 continue;
14050 if (REGNO (reg) == link->regno)
14051 break;
14053 if (i == XVECLEN (pat, 0))
14054 continue;
14056 else
14057 continue;
14059 reg = SET_DEST (set);
14061 while (GET_CODE (reg) == ZERO_EXTRACT
14062 || GET_CODE (reg) == STRICT_LOW_PART
14063 || GET_CODE (reg) == SUBREG)
14064 reg = XEXP (reg, 0);
14066 /* A LOG_LINK is defined as being placed on the first insn that uses
14067 a register and points to the insn that sets the register. Start
14068 searching at the next insn after the target of the link and stop
14069 when we reach a set of the register or the end of the basic block.
14071 Note that this correctly handles the link that used to point from
14072 I3 to I2. Also note that not much searching is typically done here
14073 since most links don't point very far away. */
14075 for (insn = NEXT_INSN (link->insn);
14076 (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
14077 || BB_HEAD (this_basic_block->next_bb) != insn));
14078 insn = NEXT_INSN (insn))
14079 if (DEBUG_INSN_P (insn))
14080 continue;
14081 else if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
14083 if (reg_referenced_p (reg, PATTERN (insn)))
14084 place = insn;
14085 break;
14087 else if (CALL_P (insn)
14088 && find_reg_fusage (insn, USE, reg))
14090 place = insn;
14091 break;
14093 else if (INSN_P (insn) && reg_set_p (reg, insn))
14094 break;
14096 /* If we found a place to put the link, place it there unless there
14097 is already a link to the same insn as LINK at that point. */
14099 if (place)
14101 struct insn_link *link2;
14103 FOR_EACH_LOG_LINK (link2, place)
14104 if (link2->insn == link->insn && link2->regno == link->regno)
14105 break;
14107 if (link2 == NULL)
14109 link->next = LOG_LINKS (place);
14110 LOG_LINKS (place) = link;
14112 /* Set added_links_insn to the earliest insn we added a
14113 link to. */
14114 if (added_links_insn == 0
14115 || DF_INSN_LUID (added_links_insn) > DF_INSN_LUID (place))
14116 added_links_insn = place;
14122 /* Check for any register or memory mentioned in EQUIV that is not
14123 mentioned in EXPR. This is used to restrict EQUIV to "specializations"
14124 of EXPR where some registers may have been replaced by constants. */
14126 static bool
14127 unmentioned_reg_p (rtx equiv, rtx expr)
14129 subrtx_iterator::array_type array;
14130 FOR_EACH_SUBRTX (iter, array, equiv, NONCONST)
14132 const_rtx x = *iter;
14133 if ((REG_P (x) || MEM_P (x))
14134 && !reg_mentioned_p (x, expr))
14135 return true;
14137 return false;
14140 DEBUG_FUNCTION void
14141 dump_combine_stats (FILE *file)
14143 fprintf
14144 (file,
14145 ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
14146 combine_attempts, combine_merges, combine_extras, combine_successes);
14149 void
14150 dump_combine_total_stats (FILE *file)
14152 fprintf
14153 (file,
14154 "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
14155 total_attempts, total_merges, total_extras, total_successes);
14158 /* Try combining insns through substitution. */
14159 static unsigned int
14160 rest_of_handle_combine (void)
14162 int rebuild_jump_labels_after_combine;
14164 df_set_flags (DF_LR_RUN_DCE + DF_DEFER_INSN_RESCAN);
14165 df_note_add_problem ();
14166 df_analyze ();
14168 regstat_init_n_sets_and_refs ();
14170 rebuild_jump_labels_after_combine
14171 = combine_instructions (get_insns (), max_reg_num ());
14173 /* Combining insns may have turned an indirect jump into a
14174 direct jump. Rebuild the JUMP_LABEL fields of jumping
14175 instructions. */
14176 if (rebuild_jump_labels_after_combine)
14178 timevar_push (TV_JUMP);
14179 rebuild_jump_labels (get_insns ());
14180 cleanup_cfg (0);
14181 timevar_pop (TV_JUMP);
14184 regstat_free_n_sets_and_refs ();
14185 return 0;
14188 namespace {
14190 const pass_data pass_data_combine =
14192 RTL_PASS, /* type */
14193 "combine", /* name */
14194 OPTGROUP_NONE, /* optinfo_flags */
14195 TV_COMBINE, /* tv_id */
14196 PROP_cfglayout, /* properties_required */
14197 0, /* properties_provided */
14198 0, /* properties_destroyed */
14199 0, /* todo_flags_start */
14200 TODO_df_finish, /* todo_flags_finish */
14203 class pass_combine : public rtl_opt_pass
14205 public:
14206 pass_combine (gcc::context *ctxt)
14207 : rtl_opt_pass (pass_data_combine, ctxt)
14210 /* opt_pass methods: */
14211 virtual bool gate (function *) { return (optimize > 0); }
14212 virtual unsigned int execute (function *)
14214 return rest_of_handle_combine ();
14217 }; // class pass_combine
14219 } // anon namespace
14221 rtl_opt_pass *
14222 make_pass_combine (gcc::context *ctxt)
14224 return new pass_combine (ctxt);