gcc:
[official-gcc.git] / gcc / combine.c
bloba2649b6d5a1cfe2a6192a2a896e38852eb72320a
1 /* Optimize by combining instructions for GNU compiler.
2 Copyright (C) 1987-2018 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 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 modified_between_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 "backend.h"
82 #include "target.h"
83 #include "rtl.h"
84 #include "tree.h"
85 #include "cfghooks.h"
86 #include "predict.h"
87 #include "df.h"
88 #include "memmodel.h"
89 #include "tm_p.h"
90 #include "optabs.h"
91 #include "regs.h"
92 #include "emit-rtl.h"
93 #include "recog.h"
94 #include "cgraph.h"
95 #include "stor-layout.h"
96 #include "cfgrtl.h"
97 #include "cfgcleanup.h"
98 /* Include expr.h after insn-config.h so we get HAVE_conditional_move. */
99 #include "explow.h"
100 #include "insn-attr.h"
101 #include "rtlhooks-def.h"
102 #include "params.h"
103 #include "tree-pass.h"
104 #include "valtrack.h"
105 #include "rtl-iter.h"
106 #include "print-rtl.h"
108 /* Number of attempts to combine instructions in this function. */
110 static int combine_attempts;
112 /* Number of attempts that got as far as substitution in this function. */
114 static int combine_merges;
116 /* Number of instructions combined with added SETs in this function. */
118 static int combine_extras;
120 /* Number of instructions combined in this function. */
122 static int combine_successes;
124 /* Totals over entire compilation. */
126 static int total_attempts, total_merges, total_extras, total_successes;
128 /* combine_instructions may try to replace the right hand side of the
129 second instruction with the value of an associated REG_EQUAL note
130 before throwing it at try_combine. That is problematic when there
131 is a REG_DEAD note for a register used in the old right hand side
132 and can cause distribute_notes to do wrong things. This is the
133 second instruction if it has been so modified, null otherwise. */
135 static rtx_insn *i2mod;
137 /* When I2MOD is nonnull, this is a copy of the old right hand side. */
139 static rtx i2mod_old_rhs;
141 /* When I2MOD is nonnull, this is a copy of the new right hand side. */
143 static rtx i2mod_new_rhs;
145 struct reg_stat_type {
146 /* Record last point of death of (hard or pseudo) register n. */
147 rtx_insn *last_death;
149 /* Record last point of modification of (hard or pseudo) register n. */
150 rtx_insn *last_set;
152 /* The next group of fields allows the recording of the last value assigned
153 to (hard or pseudo) register n. We use this information to see if an
154 operation being processed is redundant given a prior operation performed
155 on the register. For example, an `and' with a constant is redundant if
156 all the zero bits are already known to be turned off.
158 We use an approach similar to that used by cse, but change it in the
159 following ways:
161 (1) We do not want to reinitialize at each label.
162 (2) It is useful, but not critical, to know the actual value assigned
163 to a register. Often just its form is helpful.
165 Therefore, we maintain the following fields:
167 last_set_value the last value assigned
168 last_set_label records the value of label_tick when the
169 register was assigned
170 last_set_table_tick records the value of label_tick when a
171 value using the register is assigned
172 last_set_invalid set to nonzero when it is not valid
173 to use the value of this register in some
174 register's value
176 To understand the usage of these tables, it is important to understand
177 the distinction between the value in last_set_value being valid and
178 the register being validly contained in some other expression in the
179 table.
181 (The next two parameters are out of date).
183 reg_stat[i].last_set_value is valid if it is nonzero, and either
184 reg_n_sets[i] is 1 or reg_stat[i].last_set_label == label_tick.
186 Register I may validly appear in any expression returned for the value
187 of another register if reg_n_sets[i] is 1. It may also appear in the
188 value for register J if reg_stat[j].last_set_invalid is zero, or
189 reg_stat[i].last_set_label < reg_stat[j].last_set_label.
191 If an expression is found in the table containing a register which may
192 not validly appear in an expression, the register is replaced by
193 something that won't match, (clobber (const_int 0)). */
195 /* Record last value assigned to (hard or pseudo) register n. */
197 rtx last_set_value;
199 /* Record the value of label_tick when an expression involving register n
200 is placed in last_set_value. */
202 int last_set_table_tick;
204 /* Record the value of label_tick when the value for register n is placed in
205 last_set_value. */
207 int last_set_label;
209 /* These fields are maintained in parallel with last_set_value and are
210 used to store the mode in which the register was last set, the bits
211 that were known to be zero when it was last set, and the number of
212 sign bits copies it was known to have when it was last set. */
214 unsigned HOST_WIDE_INT last_set_nonzero_bits;
215 char last_set_sign_bit_copies;
216 ENUM_BITFIELD(machine_mode) last_set_mode : 8;
218 /* Set nonzero if references to register n in expressions should not be
219 used. last_set_invalid is set nonzero when this register is being
220 assigned to and last_set_table_tick == label_tick. */
222 char last_set_invalid;
224 /* Some registers that are set more than once and used in more than one
225 basic block are nevertheless always set in similar ways. For example,
226 a QImode register may be loaded from memory in two places on a machine
227 where byte loads zero extend.
229 We record in the following fields if a register has some leading bits
230 that are always equal to the sign bit, and what we know about the
231 nonzero bits of a register, specifically which bits are known to be
232 zero.
234 If an entry is zero, it means that we don't know anything special. */
236 unsigned char sign_bit_copies;
238 unsigned HOST_WIDE_INT nonzero_bits;
240 /* Record the value of the label_tick when the last truncation
241 happened. The field truncated_to_mode is only valid if
242 truncation_label == label_tick. */
244 int truncation_label;
246 /* Record the last truncation seen for this register. If truncation
247 is not a nop to this mode we might be able to save an explicit
248 truncation if we know that value already contains a truncated
249 value. */
251 ENUM_BITFIELD(machine_mode) truncated_to_mode : 8;
255 static vec<reg_stat_type> reg_stat;
257 /* One plus the highest pseudo for which we track REG_N_SETS.
258 regstat_init_n_sets_and_refs allocates the array for REG_N_SETS just once,
259 but during combine_split_insns new pseudos can be created. As we don't have
260 updated DF information in that case, it is hard to initialize the array
261 after growing. The combiner only cares about REG_N_SETS (regno) == 1,
262 so instead of growing the arrays, just assume all newly created pseudos
263 during combine might be set multiple times. */
265 static unsigned int reg_n_sets_max;
267 /* Record the luid of the last insn that invalidated memory
268 (anything that writes memory, and subroutine calls, but not pushes). */
270 static int mem_last_set;
272 /* Record the luid of the last CALL_INSN
273 so we can tell whether a potential combination crosses any calls. */
275 static int last_call_luid;
277 /* When `subst' is called, this is the insn that is being modified
278 (by combining in a previous insn). The PATTERN of this insn
279 is still the old pattern partially modified and it should not be
280 looked at, but this may be used to examine the successors of the insn
281 to judge whether a simplification is valid. */
283 static rtx_insn *subst_insn;
285 /* This is the lowest LUID that `subst' is currently dealing with.
286 get_last_value will not return a value if the register was set at or
287 after this LUID. If not for this mechanism, we could get confused if
288 I2 or I1 in try_combine were an insn that used the old value of a register
289 to obtain a new value. In that case, we might erroneously get the
290 new value of the register when we wanted the old one. */
292 static int subst_low_luid;
294 /* This contains any hard registers that are used in newpat; reg_dead_at_p
295 must consider all these registers to be always live. */
297 static HARD_REG_SET newpat_used_regs;
299 /* This is an insn to which a LOG_LINKS entry has been added. If this
300 insn is the earlier than I2 or I3, combine should rescan starting at
301 that location. */
303 static rtx_insn *added_links_insn;
305 /* And similarly, for notes. */
307 static rtx_insn *added_notes_insn;
309 /* Basic block in which we are performing combines. */
310 static basic_block this_basic_block;
311 static bool optimize_this_for_speed_p;
314 /* Length of the currently allocated uid_insn_cost array. */
316 static int max_uid_known;
318 /* The following array records the insn_cost for every insn
319 in the instruction stream. */
321 static int *uid_insn_cost;
323 /* The following array records the LOG_LINKS for every insn in the
324 instruction stream as struct insn_link pointers. */
326 struct insn_link {
327 rtx_insn *insn;
328 unsigned int regno;
329 struct insn_link *next;
332 static struct insn_link **uid_log_links;
334 static inline int
335 insn_uid_check (const_rtx insn)
337 int uid = INSN_UID (insn);
338 gcc_checking_assert (uid <= max_uid_known);
339 return uid;
342 #define INSN_COST(INSN) (uid_insn_cost[insn_uid_check (INSN)])
343 #define LOG_LINKS(INSN) (uid_log_links[insn_uid_check (INSN)])
345 #define FOR_EACH_LOG_LINK(L, INSN) \
346 for ((L) = LOG_LINKS (INSN); (L); (L) = (L)->next)
348 /* Links for LOG_LINKS are allocated from this obstack. */
350 static struct obstack insn_link_obstack;
352 /* Allocate a link. */
354 static inline struct insn_link *
355 alloc_insn_link (rtx_insn *insn, unsigned int regno, struct insn_link *next)
357 struct insn_link *l
358 = (struct insn_link *) obstack_alloc (&insn_link_obstack,
359 sizeof (struct insn_link));
360 l->insn = insn;
361 l->regno = regno;
362 l->next = next;
363 return l;
366 /* Incremented for each basic block. */
368 static int label_tick;
370 /* Reset to label_tick for each extended basic block in scanning order. */
372 static int label_tick_ebb_start;
374 /* Mode used to compute significance in reg_stat[].nonzero_bits. It is the
375 largest integer mode that can fit in HOST_BITS_PER_WIDE_INT. */
377 static scalar_int_mode nonzero_bits_mode;
379 /* Nonzero when reg_stat[].nonzero_bits and reg_stat[].sign_bit_copies can
380 be safely used. It is zero while computing them and after combine has
381 completed. This former test prevents propagating values based on
382 previously set values, which can be incorrect if a variable is modified
383 in a loop. */
385 static int nonzero_sign_valid;
388 /* Record one modification to rtl structure
389 to be undone by storing old_contents into *where. */
391 enum undo_kind { UNDO_RTX, UNDO_INT, UNDO_MODE, UNDO_LINKS };
393 struct undo
395 struct undo *next;
396 enum undo_kind kind;
397 union { rtx r; int i; machine_mode m; struct insn_link *l; } old_contents;
398 union { rtx *r; int *i; struct insn_link **l; } where;
401 /* Record a bunch of changes to be undone, up to MAX_UNDO of them.
402 num_undo says how many are currently recorded.
404 other_insn is nonzero if we have modified some other insn in the process
405 of working on subst_insn. It must be verified too. */
407 struct undobuf
409 struct undo *undos;
410 struct undo *frees;
411 rtx_insn *other_insn;
414 static struct undobuf undobuf;
416 /* Number of times the pseudo being substituted for
417 was found and replaced. */
419 static int n_occurrences;
421 static rtx reg_nonzero_bits_for_combine (const_rtx, scalar_int_mode,
422 scalar_int_mode,
423 unsigned HOST_WIDE_INT *);
424 static rtx reg_num_sign_bit_copies_for_combine (const_rtx, scalar_int_mode,
425 scalar_int_mode,
426 unsigned int *);
427 static void do_SUBST (rtx *, rtx);
428 static void do_SUBST_INT (int *, int);
429 static void init_reg_last (void);
430 static void setup_incoming_promotions (rtx_insn *);
431 static void set_nonzero_bits_and_sign_copies (rtx, const_rtx, void *);
432 static int cant_combine_insn_p (rtx_insn *);
433 static int can_combine_p (rtx_insn *, rtx_insn *, rtx_insn *, rtx_insn *,
434 rtx_insn *, rtx_insn *, rtx *, rtx *);
435 static int combinable_i3pat (rtx_insn *, rtx *, rtx, rtx, rtx, int, int, rtx *);
436 static int contains_muldiv (rtx);
437 static rtx_insn *try_combine (rtx_insn *, rtx_insn *, rtx_insn *, rtx_insn *,
438 int *, rtx_insn *);
439 static void undo_all (void);
440 static void undo_commit (void);
441 static rtx *find_split_point (rtx *, rtx_insn *, bool);
442 static rtx subst (rtx, rtx, rtx, int, int, int);
443 static rtx combine_simplify_rtx (rtx, machine_mode, int, int);
444 static rtx simplify_if_then_else (rtx);
445 static rtx simplify_set (rtx);
446 static rtx simplify_logical (rtx);
447 static rtx expand_compound_operation (rtx);
448 static const_rtx expand_field_assignment (const_rtx);
449 static rtx make_extraction (machine_mode, rtx, HOST_WIDE_INT,
450 rtx, unsigned HOST_WIDE_INT, int, int, int);
451 static int get_pos_from_mask (unsigned HOST_WIDE_INT,
452 unsigned HOST_WIDE_INT *);
453 static rtx canon_reg_for_combine (rtx, rtx);
454 static rtx force_int_to_mode (rtx, scalar_int_mode, scalar_int_mode,
455 scalar_int_mode, unsigned HOST_WIDE_INT, int);
456 static rtx force_to_mode (rtx, machine_mode,
457 unsigned HOST_WIDE_INT, int);
458 static rtx if_then_else_cond (rtx, rtx *, rtx *);
459 static rtx known_cond (rtx, enum rtx_code, rtx, rtx);
460 static int rtx_equal_for_field_assignment_p (rtx, rtx, bool = false);
461 static rtx make_field_assignment (rtx);
462 static rtx apply_distributive_law (rtx);
463 static rtx distribute_and_simplify_rtx (rtx, int);
464 static rtx simplify_and_const_int_1 (scalar_int_mode, rtx,
465 unsigned HOST_WIDE_INT);
466 static rtx simplify_and_const_int (rtx, scalar_int_mode, rtx,
467 unsigned HOST_WIDE_INT);
468 static int merge_outer_ops (enum rtx_code *, HOST_WIDE_INT *, enum rtx_code,
469 HOST_WIDE_INT, machine_mode, int *);
470 static rtx simplify_shift_const_1 (enum rtx_code, machine_mode, rtx, int);
471 static rtx simplify_shift_const (rtx, enum rtx_code, machine_mode, rtx,
472 int);
473 static int recog_for_combine (rtx *, rtx_insn *, rtx *);
474 static rtx gen_lowpart_for_combine (machine_mode, rtx);
475 static enum rtx_code simplify_compare_const (enum rtx_code, machine_mode,
476 rtx, rtx *);
477 static enum rtx_code simplify_comparison (enum rtx_code, rtx *, rtx *);
478 static void update_table_tick (rtx);
479 static void record_value_for_reg (rtx, rtx_insn *, rtx);
480 static void check_promoted_subreg (rtx_insn *, rtx);
481 static void record_dead_and_set_regs_1 (rtx, const_rtx, void *);
482 static void record_dead_and_set_regs (rtx_insn *);
483 static int get_last_value_validate (rtx *, rtx_insn *, int, int);
484 static rtx get_last_value (const_rtx);
485 static void reg_dead_at_p_1 (rtx, const_rtx, void *);
486 static int reg_dead_at_p (rtx, rtx_insn *);
487 static void move_deaths (rtx, rtx, int, rtx_insn *, rtx *);
488 static int reg_bitfield_target_p (rtx, rtx);
489 static void distribute_notes (rtx, rtx_insn *, rtx_insn *, rtx_insn *, rtx, rtx, rtx);
490 static void distribute_links (struct insn_link *);
491 static void mark_used_regs_combine (rtx);
492 static void record_promoted_value (rtx_insn *, rtx);
493 static bool unmentioned_reg_p (rtx, rtx);
494 static void record_truncated_values (rtx *, void *);
495 static bool reg_truncated_to_mode (machine_mode, const_rtx);
496 static rtx gen_lowpart_or_truncate (machine_mode, rtx);
499 /* It is not safe to use ordinary gen_lowpart in combine.
500 See comments in gen_lowpart_for_combine. */
501 #undef RTL_HOOKS_GEN_LOWPART
502 #define RTL_HOOKS_GEN_LOWPART gen_lowpart_for_combine
504 /* Our implementation of gen_lowpart never emits a new pseudo. */
505 #undef RTL_HOOKS_GEN_LOWPART_NO_EMIT
506 #define RTL_HOOKS_GEN_LOWPART_NO_EMIT gen_lowpart_for_combine
508 #undef RTL_HOOKS_REG_NONZERO_REG_BITS
509 #define RTL_HOOKS_REG_NONZERO_REG_BITS reg_nonzero_bits_for_combine
511 #undef RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES
512 #define RTL_HOOKS_REG_NUM_SIGN_BIT_COPIES reg_num_sign_bit_copies_for_combine
514 #undef RTL_HOOKS_REG_TRUNCATED_TO_MODE
515 #define RTL_HOOKS_REG_TRUNCATED_TO_MODE reg_truncated_to_mode
517 static const struct rtl_hooks combine_rtl_hooks = RTL_HOOKS_INITIALIZER;
520 /* Convenience wrapper for the canonicalize_comparison target hook.
521 Target hooks cannot use enum rtx_code. */
522 static inline void
523 target_canonicalize_comparison (enum rtx_code *code, rtx *op0, rtx *op1,
524 bool op0_preserve_value)
526 int code_int = (int)*code;
527 targetm.canonicalize_comparison (&code_int, op0, op1, op0_preserve_value);
528 *code = (enum rtx_code)code_int;
531 /* Try to split PATTERN found in INSN. This returns NULL_RTX if
532 PATTERN can not be split. Otherwise, it returns an insn sequence.
533 This is a wrapper around split_insns which ensures that the
534 reg_stat vector is made larger if the splitter creates a new
535 register. */
537 static rtx_insn *
538 combine_split_insns (rtx pattern, rtx_insn *insn)
540 rtx_insn *ret;
541 unsigned int nregs;
543 ret = split_insns (pattern, insn);
544 nregs = max_reg_num ();
545 if (nregs > reg_stat.length ())
546 reg_stat.safe_grow_cleared (nregs);
547 return ret;
550 /* This is used by find_single_use to locate an rtx in LOC that
551 contains exactly one use of DEST, which is typically either a REG
552 or CC0. It returns a pointer to the innermost rtx expression
553 containing DEST. Appearances of DEST that are being used to
554 totally replace it are not counted. */
556 static rtx *
557 find_single_use_1 (rtx dest, rtx *loc)
559 rtx x = *loc;
560 enum rtx_code code = GET_CODE (x);
561 rtx *result = NULL;
562 rtx *this_result;
563 int i;
564 const char *fmt;
566 switch (code)
568 case CONST:
569 case LABEL_REF:
570 case SYMBOL_REF:
571 CASE_CONST_ANY:
572 case CLOBBER:
573 case CLOBBER_HIGH:
574 return 0;
576 case SET:
577 /* If the destination is anything other than CC0, PC, a REG or a SUBREG
578 of a REG that occupies all of the REG, the insn uses DEST if
579 it is mentioned in the destination or the source. Otherwise, we
580 need just check the source. */
581 if (GET_CODE (SET_DEST (x)) != CC0
582 && GET_CODE (SET_DEST (x)) != PC
583 && !REG_P (SET_DEST (x))
584 && ! (GET_CODE (SET_DEST (x)) == SUBREG
585 && REG_P (SUBREG_REG (SET_DEST (x)))
586 && !read_modify_subreg_p (SET_DEST (x))))
587 break;
589 return find_single_use_1 (dest, &SET_SRC (x));
591 case MEM:
592 case SUBREG:
593 return find_single_use_1 (dest, &XEXP (x, 0));
595 default:
596 break;
599 /* If it wasn't one of the common cases above, check each expression and
600 vector of this code. Look for a unique usage of DEST. */
602 fmt = GET_RTX_FORMAT (code);
603 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
605 if (fmt[i] == 'e')
607 if (dest == XEXP (x, i)
608 || (REG_P (dest) && REG_P (XEXP (x, i))
609 && REGNO (dest) == REGNO (XEXP (x, i))))
610 this_result = loc;
611 else
612 this_result = find_single_use_1 (dest, &XEXP (x, i));
614 if (result == NULL)
615 result = this_result;
616 else if (this_result)
617 /* Duplicate usage. */
618 return NULL;
620 else if (fmt[i] == 'E')
622 int j;
624 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
626 if (XVECEXP (x, i, j) == dest
627 || (REG_P (dest)
628 && REG_P (XVECEXP (x, i, j))
629 && REGNO (XVECEXP (x, i, j)) == REGNO (dest)))
630 this_result = loc;
631 else
632 this_result = find_single_use_1 (dest, &XVECEXP (x, i, j));
634 if (result == NULL)
635 result = this_result;
636 else if (this_result)
637 return NULL;
642 return result;
646 /* See if DEST, produced in INSN, is used only a single time in the
647 sequel. If so, return a pointer to the innermost rtx expression in which
648 it is used.
650 If PLOC is nonzero, *PLOC is set to the insn containing the single use.
652 If DEST is cc0_rtx, we look only at the next insn. In that case, we don't
653 care about REG_DEAD notes or LOG_LINKS.
655 Otherwise, we find the single use by finding an insn that has a
656 LOG_LINKS pointing at INSN and has a REG_DEAD note for DEST. If DEST is
657 only referenced once in that insn, we know that it must be the first
658 and last insn referencing DEST. */
660 static rtx *
661 find_single_use (rtx dest, rtx_insn *insn, rtx_insn **ploc)
663 basic_block bb;
664 rtx_insn *next;
665 rtx *result;
666 struct insn_link *link;
668 if (dest == cc0_rtx)
670 next = NEXT_INSN (insn);
671 if (next == 0
672 || (!NONJUMP_INSN_P (next) && !JUMP_P (next)))
673 return 0;
675 result = find_single_use_1 (dest, &PATTERN (next));
676 if (result && ploc)
677 *ploc = next;
678 return result;
681 if (!REG_P (dest))
682 return 0;
684 bb = BLOCK_FOR_INSN (insn);
685 for (next = NEXT_INSN (insn);
686 next && BLOCK_FOR_INSN (next) == bb;
687 next = NEXT_INSN (next))
688 if (NONDEBUG_INSN_P (next) && dead_or_set_p (next, dest))
690 FOR_EACH_LOG_LINK (link, next)
691 if (link->insn == insn && link->regno == REGNO (dest))
692 break;
694 if (link)
696 result = find_single_use_1 (dest, &PATTERN (next));
697 if (ploc)
698 *ploc = next;
699 return result;
703 return 0;
706 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
707 insn. The substitution can be undone by undo_all. If INTO is already
708 set to NEWVAL, do not record this change. Because computing NEWVAL might
709 also call SUBST, we have to compute it before we put anything into
710 the undo table. */
712 static void
713 do_SUBST (rtx *into, rtx newval)
715 struct undo *buf;
716 rtx oldval = *into;
718 if (oldval == newval)
719 return;
721 /* We'd like to catch as many invalid transformations here as
722 possible. Unfortunately, there are way too many mode changes
723 that are perfectly valid, so we'd waste too much effort for
724 little gain doing the checks here. Focus on catching invalid
725 transformations involving integer constants. */
726 if (GET_MODE_CLASS (GET_MODE (oldval)) == MODE_INT
727 && CONST_INT_P (newval))
729 /* Sanity check that we're replacing oldval with a CONST_INT
730 that is a valid sign-extension for the original mode. */
731 gcc_assert (INTVAL (newval)
732 == trunc_int_for_mode (INTVAL (newval), GET_MODE (oldval)));
734 /* Replacing the operand of a SUBREG or a ZERO_EXTEND with a
735 CONST_INT is not valid, because after the replacement, the
736 original mode would be gone. Unfortunately, we can't tell
737 when do_SUBST is called to replace the operand thereof, so we
738 perform this test on oldval instead, checking whether an
739 invalid replacement took place before we got here. */
740 gcc_assert (!(GET_CODE (oldval) == SUBREG
741 && CONST_INT_P (SUBREG_REG (oldval))));
742 gcc_assert (!(GET_CODE (oldval) == ZERO_EXTEND
743 && CONST_INT_P (XEXP (oldval, 0))));
746 if (undobuf.frees)
747 buf = undobuf.frees, undobuf.frees = buf->next;
748 else
749 buf = XNEW (struct undo);
751 buf->kind = UNDO_RTX;
752 buf->where.r = into;
753 buf->old_contents.r = oldval;
754 *into = newval;
756 buf->next = undobuf.undos, undobuf.undos = buf;
759 #define SUBST(INTO, NEWVAL) do_SUBST (&(INTO), (NEWVAL))
761 /* Similar to SUBST, but NEWVAL is an int expression. Note that substitution
762 for the value of a HOST_WIDE_INT value (including CONST_INT) is
763 not safe. */
765 static void
766 do_SUBST_INT (int *into, int newval)
768 struct undo *buf;
769 int oldval = *into;
771 if (oldval == newval)
772 return;
774 if (undobuf.frees)
775 buf = undobuf.frees, undobuf.frees = buf->next;
776 else
777 buf = XNEW (struct undo);
779 buf->kind = UNDO_INT;
780 buf->where.i = into;
781 buf->old_contents.i = oldval;
782 *into = newval;
784 buf->next = undobuf.undos, undobuf.undos = buf;
787 #define SUBST_INT(INTO, NEWVAL) do_SUBST_INT (&(INTO), (NEWVAL))
789 /* Similar to SUBST, but just substitute the mode. This is used when
790 changing the mode of a pseudo-register, so that any other
791 references to the entry in the regno_reg_rtx array will change as
792 well. */
794 static void
795 do_SUBST_MODE (rtx *into, machine_mode newval)
797 struct undo *buf;
798 machine_mode oldval = GET_MODE (*into);
800 if (oldval == newval)
801 return;
803 if (undobuf.frees)
804 buf = undobuf.frees, undobuf.frees = buf->next;
805 else
806 buf = XNEW (struct undo);
808 buf->kind = UNDO_MODE;
809 buf->where.r = into;
810 buf->old_contents.m = oldval;
811 adjust_reg_mode (*into, newval);
813 buf->next = undobuf.undos, undobuf.undos = buf;
816 #define SUBST_MODE(INTO, NEWVAL) do_SUBST_MODE (&(INTO), (NEWVAL))
818 /* Similar to SUBST, but NEWVAL is a LOG_LINKS expression. */
820 static void
821 do_SUBST_LINK (struct insn_link **into, struct insn_link *newval)
823 struct undo *buf;
824 struct insn_link * oldval = *into;
826 if (oldval == newval)
827 return;
829 if (undobuf.frees)
830 buf = undobuf.frees, undobuf.frees = buf->next;
831 else
832 buf = XNEW (struct undo);
834 buf->kind = UNDO_LINKS;
835 buf->where.l = into;
836 buf->old_contents.l = oldval;
837 *into = newval;
839 buf->next = undobuf.undos, undobuf.undos = buf;
842 #define SUBST_LINK(oldval, newval) do_SUBST_LINK (&oldval, newval)
844 /* Subroutine of try_combine. Determine whether the replacement patterns
845 NEWPAT, NEWI2PAT and NEWOTHERPAT are cheaper according to insn_cost
846 than the original sequence I0, I1, I2, I3 and undobuf.other_insn. Note
847 that I0, I1 and/or NEWI2PAT may be NULL_RTX. Similarly, NEWOTHERPAT and
848 undobuf.other_insn may also both be NULL_RTX. Return false if the cost
849 of all the instructions can be estimated and the replacements are more
850 expensive than the original sequence. */
852 static bool
853 combine_validate_cost (rtx_insn *i0, rtx_insn *i1, rtx_insn *i2, rtx_insn *i3,
854 rtx newpat, rtx newi2pat, rtx newotherpat)
856 int i0_cost, i1_cost, i2_cost, i3_cost;
857 int new_i2_cost, new_i3_cost;
858 int old_cost, new_cost;
860 /* Lookup the original insn_costs. */
861 i2_cost = INSN_COST (i2);
862 i3_cost = INSN_COST (i3);
864 if (i1)
866 i1_cost = INSN_COST (i1);
867 if (i0)
869 i0_cost = INSN_COST (i0);
870 old_cost = (i0_cost > 0 && i1_cost > 0 && i2_cost > 0 && i3_cost > 0
871 ? i0_cost + i1_cost + i2_cost + i3_cost : 0);
873 else
875 old_cost = (i1_cost > 0 && i2_cost > 0 && i3_cost > 0
876 ? i1_cost + i2_cost + i3_cost : 0);
877 i0_cost = 0;
880 else
882 old_cost = (i2_cost > 0 && i3_cost > 0) ? i2_cost + i3_cost : 0;
883 i1_cost = i0_cost = 0;
886 /* If we have split a PARALLEL I2 to I1,I2, we have counted its cost twice;
887 correct that. */
888 if (old_cost && i1 && INSN_UID (i1) == INSN_UID (i2))
889 old_cost -= i1_cost;
892 /* Calculate the replacement insn_costs. */
893 rtx tmp = PATTERN (i3);
894 PATTERN (i3) = newpat;
895 int tmpi = INSN_CODE (i3);
896 INSN_CODE (i3) = -1;
897 new_i3_cost = insn_cost (i3, optimize_this_for_speed_p);
898 PATTERN (i3) = tmp;
899 INSN_CODE (i3) = tmpi;
900 if (newi2pat)
902 tmp = PATTERN (i2);
903 PATTERN (i2) = newi2pat;
904 tmpi = INSN_CODE (i2);
905 INSN_CODE (i2) = -1;
906 new_i2_cost = insn_cost (i2, optimize_this_for_speed_p);
907 PATTERN (i2) = tmp;
908 INSN_CODE (i2) = tmpi;
909 new_cost = (new_i2_cost > 0 && new_i3_cost > 0)
910 ? new_i2_cost + new_i3_cost : 0;
912 else
914 new_cost = new_i3_cost;
915 new_i2_cost = 0;
918 if (undobuf.other_insn)
920 int old_other_cost, new_other_cost;
922 old_other_cost = INSN_COST (undobuf.other_insn);
923 tmp = PATTERN (undobuf.other_insn);
924 PATTERN (undobuf.other_insn) = newotherpat;
925 tmpi = INSN_CODE (undobuf.other_insn);
926 INSN_CODE (undobuf.other_insn) = -1;
927 new_other_cost = insn_cost (undobuf.other_insn,
928 optimize_this_for_speed_p);
929 PATTERN (undobuf.other_insn) = tmp;
930 INSN_CODE (undobuf.other_insn) = tmpi;
931 if (old_other_cost > 0 && new_other_cost > 0)
933 old_cost += old_other_cost;
934 new_cost += new_other_cost;
936 else
937 old_cost = 0;
940 /* Disallow this combination if both new_cost and old_cost are greater than
941 zero, and new_cost is greater than old cost. */
942 int reject = old_cost > 0 && new_cost > old_cost;
944 if (dump_file)
946 fprintf (dump_file, "%s combination of insns ",
947 reject ? "rejecting" : "allowing");
948 if (i0)
949 fprintf (dump_file, "%d, ", INSN_UID (i0));
950 if (i1 && INSN_UID (i1) != INSN_UID (i2))
951 fprintf (dump_file, "%d, ", INSN_UID (i1));
952 fprintf (dump_file, "%d and %d\n", INSN_UID (i2), INSN_UID (i3));
954 fprintf (dump_file, "original costs ");
955 if (i0)
956 fprintf (dump_file, "%d + ", i0_cost);
957 if (i1 && INSN_UID (i1) != INSN_UID (i2))
958 fprintf (dump_file, "%d + ", i1_cost);
959 fprintf (dump_file, "%d + %d = %d\n", i2_cost, i3_cost, old_cost);
961 if (newi2pat)
962 fprintf (dump_file, "replacement costs %d + %d = %d\n",
963 new_i2_cost, new_i3_cost, new_cost);
964 else
965 fprintf (dump_file, "replacement cost %d\n", new_cost);
968 if (reject)
969 return false;
971 /* Update the uid_insn_cost array with the replacement costs. */
972 INSN_COST (i2) = new_i2_cost;
973 INSN_COST (i3) = new_i3_cost;
974 if (i1)
976 INSN_COST (i1) = 0;
977 if (i0)
978 INSN_COST (i0) = 0;
981 return true;
985 /* Delete any insns that copy a register to itself. */
987 static void
988 delete_noop_moves (void)
990 rtx_insn *insn, *next;
991 basic_block bb;
993 FOR_EACH_BB_FN (bb, cfun)
995 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb)); insn = next)
997 next = NEXT_INSN (insn);
998 if (INSN_P (insn) && noop_move_p (insn))
1000 if (dump_file)
1001 fprintf (dump_file, "deleting noop move %d\n", INSN_UID (insn));
1003 delete_insn_and_edges (insn);
1010 /* Return false if we do not want to (or cannot) combine DEF. */
1011 static bool
1012 can_combine_def_p (df_ref def)
1014 /* Do not consider if it is pre/post modification in MEM. */
1015 if (DF_REF_FLAGS (def) & DF_REF_PRE_POST_MODIFY)
1016 return false;
1018 unsigned int regno = DF_REF_REGNO (def);
1020 /* Do not combine frame pointer adjustments. */
1021 if ((regno == FRAME_POINTER_REGNUM
1022 && (!reload_completed || frame_pointer_needed))
1023 || (!HARD_FRAME_POINTER_IS_FRAME_POINTER
1024 && regno == HARD_FRAME_POINTER_REGNUM
1025 && (!reload_completed || frame_pointer_needed))
1026 || (FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1027 && regno == ARG_POINTER_REGNUM && fixed_regs[regno]))
1028 return false;
1030 return true;
1033 /* Return false if we do not want to (or cannot) combine USE. */
1034 static bool
1035 can_combine_use_p (df_ref use)
1037 /* Do not consider the usage of the stack pointer by function call. */
1038 if (DF_REF_FLAGS (use) & DF_REF_CALL_STACK_USAGE)
1039 return false;
1041 return true;
1044 /* Fill in log links field for all insns. */
1046 static void
1047 create_log_links (void)
1049 basic_block bb;
1050 rtx_insn **next_use;
1051 rtx_insn *insn;
1052 df_ref def, use;
1054 next_use = XCNEWVEC (rtx_insn *, max_reg_num ());
1056 /* Pass through each block from the end, recording the uses of each
1057 register and establishing log links when def is encountered.
1058 Note that we do not clear next_use array in order to save time,
1059 so we have to test whether the use is in the same basic block as def.
1061 There are a few cases below when we do not consider the definition or
1062 usage -- these are taken from original flow.c did. Don't ask me why it is
1063 done this way; I don't know and if it works, I don't want to know. */
1065 FOR_EACH_BB_FN (bb, cfun)
1067 FOR_BB_INSNS_REVERSE (bb, insn)
1069 if (!NONDEBUG_INSN_P (insn))
1070 continue;
1072 /* Log links are created only once. */
1073 gcc_assert (!LOG_LINKS (insn));
1075 FOR_EACH_INSN_DEF (def, insn)
1077 unsigned int regno = DF_REF_REGNO (def);
1078 rtx_insn *use_insn;
1080 if (!next_use[regno])
1081 continue;
1083 if (!can_combine_def_p (def))
1084 continue;
1086 use_insn = next_use[regno];
1087 next_use[regno] = NULL;
1089 if (BLOCK_FOR_INSN (use_insn) != bb)
1090 continue;
1092 /* flow.c claimed:
1094 We don't build a LOG_LINK for hard registers contained
1095 in ASM_OPERANDs. If these registers get replaced,
1096 we might wind up changing the semantics of the insn,
1097 even if reload can make what appear to be valid
1098 assignments later. */
1099 if (regno < FIRST_PSEUDO_REGISTER
1100 && asm_noperands (PATTERN (use_insn)) >= 0)
1101 continue;
1103 /* Don't add duplicate links between instructions. */
1104 struct insn_link *links;
1105 FOR_EACH_LOG_LINK (links, use_insn)
1106 if (insn == links->insn && regno == links->regno)
1107 break;
1109 if (!links)
1110 LOG_LINKS (use_insn)
1111 = alloc_insn_link (insn, regno, LOG_LINKS (use_insn));
1114 FOR_EACH_INSN_USE (use, insn)
1115 if (can_combine_use_p (use))
1116 next_use[DF_REF_REGNO (use)] = insn;
1120 free (next_use);
1123 /* Walk the LOG_LINKS of insn B to see if we find a reference to A. Return
1124 true if we found a LOG_LINK that proves that A feeds B. This only works
1125 if there are no instructions between A and B which could have a link
1126 depending on A, since in that case we would not record a link for B.
1127 We also check the implicit dependency created by a cc0 setter/user
1128 pair. */
1130 static bool
1131 insn_a_feeds_b (rtx_insn *a, rtx_insn *b)
1133 struct insn_link *links;
1134 FOR_EACH_LOG_LINK (links, b)
1135 if (links->insn == a)
1136 return true;
1137 if (HAVE_cc0 && sets_cc0_p (a))
1138 return true;
1139 return false;
1142 /* Main entry point for combiner. F is the first insn of the function.
1143 NREGS is the first unused pseudo-reg number.
1145 Return nonzero if the combiner has turned an indirect jump
1146 instruction into a direct jump. */
1147 static int
1148 combine_instructions (rtx_insn *f, unsigned int nregs)
1150 rtx_insn *insn, *next;
1151 rtx_insn *prev;
1152 struct insn_link *links, *nextlinks;
1153 rtx_insn *first;
1154 basic_block last_bb;
1156 int new_direct_jump_p = 0;
1158 for (first = f; first && !NONDEBUG_INSN_P (first); )
1159 first = NEXT_INSN (first);
1160 if (!first)
1161 return 0;
1163 combine_attempts = 0;
1164 combine_merges = 0;
1165 combine_extras = 0;
1166 combine_successes = 0;
1168 rtl_hooks = combine_rtl_hooks;
1170 reg_stat.safe_grow_cleared (nregs);
1172 init_recog_no_volatile ();
1174 /* Allocate array for insn info. */
1175 max_uid_known = get_max_uid ();
1176 uid_log_links = XCNEWVEC (struct insn_link *, max_uid_known + 1);
1177 uid_insn_cost = XCNEWVEC (int, max_uid_known + 1);
1178 gcc_obstack_init (&insn_link_obstack);
1180 nonzero_bits_mode = int_mode_for_size (HOST_BITS_PER_WIDE_INT, 0).require ();
1182 /* Don't use reg_stat[].nonzero_bits when computing it. This can cause
1183 problems when, for example, we have j <<= 1 in a loop. */
1185 nonzero_sign_valid = 0;
1186 label_tick = label_tick_ebb_start = 1;
1188 /* Scan all SETs and see if we can deduce anything about what
1189 bits are known to be zero for some registers and how many copies
1190 of the sign bit are known to exist for those registers.
1192 Also set any known values so that we can use it while searching
1193 for what bits are known to be set. */
1195 setup_incoming_promotions (first);
1196 /* Allow the entry block and the first block to fall into the same EBB.
1197 Conceptually the incoming promotions are assigned to the entry block. */
1198 last_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1200 create_log_links ();
1201 FOR_EACH_BB_FN (this_basic_block, cfun)
1203 optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1204 last_call_luid = 0;
1205 mem_last_set = -1;
1207 label_tick++;
1208 if (!single_pred_p (this_basic_block)
1209 || single_pred (this_basic_block) != last_bb)
1210 label_tick_ebb_start = label_tick;
1211 last_bb = this_basic_block;
1213 FOR_BB_INSNS (this_basic_block, insn)
1214 if (INSN_P (insn) && BLOCK_FOR_INSN (insn))
1216 rtx links;
1218 subst_low_luid = DF_INSN_LUID (insn);
1219 subst_insn = insn;
1221 note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies,
1222 insn);
1223 record_dead_and_set_regs (insn);
1225 if (AUTO_INC_DEC)
1226 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
1227 if (REG_NOTE_KIND (links) == REG_INC)
1228 set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX,
1229 insn);
1231 /* Record the current insn_cost of this instruction. */
1232 if (NONJUMP_INSN_P (insn))
1233 INSN_COST (insn) = insn_cost (insn, optimize_this_for_speed_p);
1234 if (dump_file)
1236 fprintf (dump_file, "insn_cost %d for ", INSN_COST (insn));
1237 dump_insn_slim (dump_file, insn);
1242 nonzero_sign_valid = 1;
1244 /* Now scan all the insns in forward order. */
1245 label_tick = label_tick_ebb_start = 1;
1246 init_reg_last ();
1247 setup_incoming_promotions (first);
1248 last_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1249 int max_combine = PARAM_VALUE (PARAM_MAX_COMBINE_INSNS);
1251 FOR_EACH_BB_FN (this_basic_block, cfun)
1253 rtx_insn *last_combined_insn = NULL;
1255 /* Ignore instruction combination in basic blocks that are going to
1256 be removed as unreachable anyway. See PR82386. */
1257 if (EDGE_COUNT (this_basic_block->preds) == 0)
1258 continue;
1260 optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1261 last_call_luid = 0;
1262 mem_last_set = -1;
1264 label_tick++;
1265 if (!single_pred_p (this_basic_block)
1266 || single_pred (this_basic_block) != last_bb)
1267 label_tick_ebb_start = label_tick;
1268 last_bb = this_basic_block;
1270 rtl_profile_for_bb (this_basic_block);
1271 for (insn = BB_HEAD (this_basic_block);
1272 insn != NEXT_INSN (BB_END (this_basic_block));
1273 insn = next ? next : NEXT_INSN (insn))
1275 next = 0;
1276 if (!NONDEBUG_INSN_P (insn))
1277 continue;
1279 while (last_combined_insn
1280 && (!NONDEBUG_INSN_P (last_combined_insn)
1281 || last_combined_insn->deleted ()))
1282 last_combined_insn = PREV_INSN (last_combined_insn);
1283 if (last_combined_insn == NULL_RTX
1284 || BLOCK_FOR_INSN (last_combined_insn) != this_basic_block
1285 || DF_INSN_LUID (last_combined_insn) <= DF_INSN_LUID (insn))
1286 last_combined_insn = insn;
1288 /* See if we know about function return values before this
1289 insn based upon SUBREG flags. */
1290 check_promoted_subreg (insn, PATTERN (insn));
1292 /* See if we can find hardregs and subreg of pseudos in
1293 narrower modes. This could help turning TRUNCATEs
1294 into SUBREGs. */
1295 note_uses (&PATTERN (insn), record_truncated_values, NULL);
1297 /* Try this insn with each insn it links back to. */
1299 FOR_EACH_LOG_LINK (links, insn)
1300 if ((next = try_combine (insn, links->insn, NULL,
1301 NULL, &new_direct_jump_p,
1302 last_combined_insn)) != 0)
1304 statistics_counter_event (cfun, "two-insn combine", 1);
1305 goto retry;
1308 /* Try each sequence of three linked insns ending with this one. */
1310 if (max_combine >= 3)
1311 FOR_EACH_LOG_LINK (links, insn)
1313 rtx_insn *link = links->insn;
1315 /* If the linked insn has been replaced by a note, then there
1316 is no point in pursuing this chain any further. */
1317 if (NOTE_P (link))
1318 continue;
1320 FOR_EACH_LOG_LINK (nextlinks, link)
1321 if ((next = try_combine (insn, link, nextlinks->insn,
1322 NULL, &new_direct_jump_p,
1323 last_combined_insn)) != 0)
1325 statistics_counter_event (cfun, "three-insn combine", 1);
1326 goto retry;
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 (HAVE_cc0
1338 && JUMP_P (insn)
1339 && (prev = prev_nonnote_insn (insn)) != 0
1340 && NONJUMP_INSN_P (prev)
1341 && sets_cc0_p (PATTERN (prev)))
1343 if ((next = try_combine (insn, prev, NULL, NULL,
1344 &new_direct_jump_p,
1345 last_combined_insn)) != 0)
1346 goto retry;
1348 FOR_EACH_LOG_LINK (nextlinks, prev)
1349 if ((next = try_combine (insn, prev, nextlinks->insn,
1350 NULL, &new_direct_jump_p,
1351 last_combined_insn)) != 0)
1352 goto retry;
1355 /* Do the same for an insn that explicitly references CC0. */
1356 if (HAVE_cc0 && NONJUMP_INSN_P (insn)
1357 && (prev = prev_nonnote_insn (insn)) != 0
1358 && NONJUMP_INSN_P (prev)
1359 && sets_cc0_p (PATTERN (prev))
1360 && GET_CODE (PATTERN (insn)) == SET
1361 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
1363 if ((next = try_combine (insn, prev, NULL, NULL,
1364 &new_direct_jump_p,
1365 last_combined_insn)) != 0)
1366 goto retry;
1368 FOR_EACH_LOG_LINK (nextlinks, prev)
1369 if ((next = try_combine (insn, prev, nextlinks->insn,
1370 NULL, &new_direct_jump_p,
1371 last_combined_insn)) != 0)
1372 goto retry;
1375 /* Finally, see if any of the insns that this insn links to
1376 explicitly references CC0. If so, try this insn, that insn,
1377 and its predecessor if it sets CC0. */
1378 if (HAVE_cc0)
1380 FOR_EACH_LOG_LINK (links, insn)
1381 if (NONJUMP_INSN_P (links->insn)
1382 && GET_CODE (PATTERN (links->insn)) == SET
1383 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (links->insn)))
1384 && (prev = prev_nonnote_insn (links->insn)) != 0
1385 && NONJUMP_INSN_P (prev)
1386 && sets_cc0_p (PATTERN (prev))
1387 && (next = try_combine (insn, links->insn,
1388 prev, NULL, &new_direct_jump_p,
1389 last_combined_insn)) != 0)
1390 goto retry;
1393 /* Try combining an insn with two different insns whose results it
1394 uses. */
1395 if (max_combine >= 3)
1396 FOR_EACH_LOG_LINK (links, insn)
1397 for (nextlinks = links->next; nextlinks;
1398 nextlinks = nextlinks->next)
1399 if ((next = try_combine (insn, links->insn,
1400 nextlinks->insn, NULL,
1401 &new_direct_jump_p,
1402 last_combined_insn)) != 0)
1405 statistics_counter_event (cfun, "three-insn combine", 1);
1406 goto retry;
1409 /* Try four-instruction combinations. */
1410 if (max_combine >= 4)
1411 FOR_EACH_LOG_LINK (links, insn)
1413 struct insn_link *next1;
1414 rtx_insn *link = links->insn;
1416 /* If the linked insn has been replaced by a note, then there
1417 is no point in pursuing this chain any further. */
1418 if (NOTE_P (link))
1419 continue;
1421 FOR_EACH_LOG_LINK (next1, link)
1423 rtx_insn *link1 = next1->insn;
1424 if (NOTE_P (link1))
1425 continue;
1426 /* I0 -> I1 -> I2 -> I3. */
1427 FOR_EACH_LOG_LINK (nextlinks, link1)
1428 if ((next = try_combine (insn, link, link1,
1429 nextlinks->insn,
1430 &new_direct_jump_p,
1431 last_combined_insn)) != 0)
1433 statistics_counter_event (cfun, "four-insn combine", 1);
1434 goto retry;
1436 /* I0, I1 -> I2, I2 -> I3. */
1437 for (nextlinks = next1->next; nextlinks;
1438 nextlinks = nextlinks->next)
1439 if ((next = try_combine (insn, link, link1,
1440 nextlinks->insn,
1441 &new_direct_jump_p,
1442 last_combined_insn)) != 0)
1444 statistics_counter_event (cfun, "four-insn combine", 1);
1445 goto retry;
1449 for (next1 = links->next; next1; next1 = next1->next)
1451 rtx_insn *link1 = next1->insn;
1452 if (NOTE_P (link1))
1453 continue;
1454 /* I0 -> I2; I1, I2 -> I3. */
1455 FOR_EACH_LOG_LINK (nextlinks, link)
1456 if ((next = try_combine (insn, link, link1,
1457 nextlinks->insn,
1458 &new_direct_jump_p,
1459 last_combined_insn)) != 0)
1461 statistics_counter_event (cfun, "four-insn combine", 1);
1462 goto retry;
1464 /* I0 -> I1; I1, I2 -> I3. */
1465 FOR_EACH_LOG_LINK (nextlinks, link1)
1466 if ((next = try_combine (insn, link, link1,
1467 nextlinks->insn,
1468 &new_direct_jump_p,
1469 last_combined_insn)) != 0)
1471 statistics_counter_event (cfun, "four-insn combine", 1);
1472 goto retry;
1477 /* Try this insn with each REG_EQUAL note it links back to. */
1478 FOR_EACH_LOG_LINK (links, insn)
1480 rtx set, note;
1481 rtx_insn *temp = links->insn;
1482 if ((set = single_set (temp)) != 0
1483 && (note = find_reg_equal_equiv_note (temp)) != 0
1484 && (note = XEXP (note, 0), GET_CODE (note)) != EXPR_LIST
1485 /* Avoid using a register that may already been marked
1486 dead by an earlier instruction. */
1487 && ! unmentioned_reg_p (note, SET_SRC (set))
1488 && (GET_MODE (note) == VOIDmode
1489 ? SCALAR_INT_MODE_P (GET_MODE (SET_DEST (set)))
1490 : (GET_MODE (SET_DEST (set)) == GET_MODE (note)
1491 && (GET_CODE (SET_DEST (set)) != ZERO_EXTRACT
1492 || (GET_MODE (XEXP (SET_DEST (set), 0))
1493 == GET_MODE (note))))))
1495 /* Temporarily replace the set's source with the
1496 contents of the REG_EQUAL note. The insn will
1497 be deleted or recognized by try_combine. */
1498 rtx orig_src = SET_SRC (set);
1499 rtx orig_dest = SET_DEST (set);
1500 if (GET_CODE (SET_DEST (set)) == ZERO_EXTRACT)
1501 SET_DEST (set) = XEXP (SET_DEST (set), 0);
1502 SET_SRC (set) = note;
1503 i2mod = temp;
1504 i2mod_old_rhs = copy_rtx (orig_src);
1505 i2mod_new_rhs = copy_rtx (note);
1506 next = try_combine (insn, i2mod, NULL, NULL,
1507 &new_direct_jump_p,
1508 last_combined_insn);
1509 i2mod = NULL;
1510 if (next)
1512 statistics_counter_event (cfun, "insn-with-note combine", 1);
1513 goto retry;
1515 SET_SRC (set) = orig_src;
1516 SET_DEST (set) = orig_dest;
1520 if (!NOTE_P (insn))
1521 record_dead_and_set_regs (insn);
1523 retry:
1528 default_rtl_profile ();
1529 clear_bb_flags ();
1530 new_direct_jump_p |= purge_all_dead_edges ();
1531 delete_noop_moves ();
1533 /* Clean up. */
1534 obstack_free (&insn_link_obstack, NULL);
1535 free (uid_log_links);
1536 free (uid_insn_cost);
1537 reg_stat.release ();
1540 struct undo *undo, *next;
1541 for (undo = undobuf.frees; undo; undo = next)
1543 next = undo->next;
1544 free (undo);
1546 undobuf.frees = 0;
1549 total_attempts += combine_attempts;
1550 total_merges += combine_merges;
1551 total_extras += combine_extras;
1552 total_successes += combine_successes;
1554 nonzero_sign_valid = 0;
1555 rtl_hooks = general_rtl_hooks;
1557 /* Make recognizer allow volatile MEMs again. */
1558 init_recog ();
1560 return new_direct_jump_p;
1563 /* Wipe the last_xxx fields of reg_stat in preparation for another pass. */
1565 static void
1566 init_reg_last (void)
1568 unsigned int i;
1569 reg_stat_type *p;
1571 FOR_EACH_VEC_ELT (reg_stat, i, p)
1572 memset (p, 0, offsetof (reg_stat_type, sign_bit_copies));
1575 /* Set up any promoted values for incoming argument registers. */
1577 static void
1578 setup_incoming_promotions (rtx_insn *first)
1580 tree arg;
1581 bool strictly_local = false;
1583 for (arg = DECL_ARGUMENTS (current_function_decl); arg;
1584 arg = DECL_CHAIN (arg))
1586 rtx x, reg = DECL_INCOMING_RTL (arg);
1587 int uns1, uns3;
1588 machine_mode mode1, mode2, mode3, mode4;
1590 /* Only continue if the incoming argument is in a register. */
1591 if (!REG_P (reg))
1592 continue;
1594 /* Determine, if possible, whether all call sites of the current
1595 function lie within the current compilation unit. (This does
1596 take into account the exporting of a function via taking its
1597 address, and so forth.) */
1598 strictly_local = cgraph_node::local_info (current_function_decl)->local;
1600 /* The mode and signedness of the argument before any promotions happen
1601 (equal to the mode of the pseudo holding it at that stage). */
1602 mode1 = TYPE_MODE (TREE_TYPE (arg));
1603 uns1 = TYPE_UNSIGNED (TREE_TYPE (arg));
1605 /* The mode and signedness of the argument after any source language and
1606 TARGET_PROMOTE_PROTOTYPES-driven promotions. */
1607 mode2 = TYPE_MODE (DECL_ARG_TYPE (arg));
1608 uns3 = TYPE_UNSIGNED (DECL_ARG_TYPE (arg));
1610 /* The mode and signedness of the argument as it is actually passed,
1611 see assign_parm_setup_reg in function.c. */
1612 mode3 = promote_function_mode (TREE_TYPE (arg), mode1, &uns3,
1613 TREE_TYPE (cfun->decl), 0);
1615 /* The mode of the register in which the argument is being passed. */
1616 mode4 = GET_MODE (reg);
1618 /* Eliminate sign extensions in the callee when:
1619 (a) A mode promotion has occurred; */
1620 if (mode1 == mode3)
1621 continue;
1622 /* (b) The mode of the register is the same as the mode of
1623 the argument as it is passed; */
1624 if (mode3 != mode4)
1625 continue;
1626 /* (c) There's no language level extension; */
1627 if (mode1 == mode2)
1629 /* (c.1) All callers are from the current compilation unit. If that's
1630 the case we don't have to rely on an ABI, we only have to know
1631 what we're generating right now, and we know that we will do the
1632 mode1 to mode2 promotion with the given sign. */
1633 else if (!strictly_local)
1634 continue;
1635 /* (c.2) The combination of the two promotions is useful. This is
1636 true when the signs match, or if the first promotion is unsigned.
1637 In the later case, (sign_extend (zero_extend x)) is the same as
1638 (zero_extend (zero_extend x)), so make sure to force UNS3 true. */
1639 else if (uns1)
1640 uns3 = true;
1641 else if (uns3)
1642 continue;
1644 /* Record that the value was promoted from mode1 to mode3,
1645 so that any sign extension at the head of the current
1646 function may be eliminated. */
1647 x = gen_rtx_CLOBBER (mode1, const0_rtx);
1648 x = gen_rtx_fmt_e ((uns3 ? ZERO_EXTEND : SIGN_EXTEND), mode3, x);
1649 record_value_for_reg (reg, first, x);
1653 /* If MODE has a precision lower than PREC and SRC is a non-negative constant
1654 that would appear negative in MODE, sign-extend SRC for use in nonzero_bits
1655 because some machines (maybe most) will actually do the sign-extension and
1656 this is the conservative approach.
1658 ??? For 2.5, try to tighten up the MD files in this regard instead of this
1659 kludge. */
1661 static rtx
1662 sign_extend_short_imm (rtx src, machine_mode mode, unsigned int prec)
1664 scalar_int_mode int_mode;
1665 if (CONST_INT_P (src)
1666 && is_a <scalar_int_mode> (mode, &int_mode)
1667 && GET_MODE_PRECISION (int_mode) < prec
1668 && INTVAL (src) > 0
1669 && val_signbit_known_set_p (int_mode, INTVAL (src)))
1670 src = GEN_INT (INTVAL (src) | ~GET_MODE_MASK (int_mode));
1672 return src;
1675 /* Update RSP for pseudo-register X from INSN's REG_EQUAL note (if one exists)
1676 and SET. */
1678 static void
1679 update_rsp_from_reg_equal (reg_stat_type *rsp, rtx_insn *insn, const_rtx set,
1680 rtx x)
1682 rtx reg_equal_note = insn ? find_reg_equal_equiv_note (insn) : NULL_RTX;
1683 unsigned HOST_WIDE_INT bits = 0;
1684 rtx reg_equal = NULL, src = SET_SRC (set);
1685 unsigned int num = 0;
1687 if (reg_equal_note)
1688 reg_equal = XEXP (reg_equal_note, 0);
1690 if (SHORT_IMMEDIATES_SIGN_EXTEND)
1692 src = sign_extend_short_imm (src, GET_MODE (x), BITS_PER_WORD);
1693 if (reg_equal)
1694 reg_equal = sign_extend_short_imm (reg_equal, GET_MODE (x), BITS_PER_WORD);
1697 /* Don't call nonzero_bits if it cannot change anything. */
1698 if (rsp->nonzero_bits != HOST_WIDE_INT_M1U)
1700 bits = nonzero_bits (src, nonzero_bits_mode);
1701 if (reg_equal && bits)
1702 bits &= nonzero_bits (reg_equal, nonzero_bits_mode);
1703 rsp->nonzero_bits |= bits;
1706 /* Don't call num_sign_bit_copies if it cannot change anything. */
1707 if (rsp->sign_bit_copies != 1)
1709 num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
1710 if (reg_equal && maybe_ne (num, GET_MODE_PRECISION (GET_MODE (x))))
1712 unsigned int numeq = num_sign_bit_copies (reg_equal, GET_MODE (x));
1713 if (num == 0 || numeq > num)
1714 num = numeq;
1716 if (rsp->sign_bit_copies == 0 || num < rsp->sign_bit_copies)
1717 rsp->sign_bit_copies = num;
1721 /* Called via note_stores. If X is a pseudo that is narrower than
1722 HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
1724 If we are setting only a portion of X and we can't figure out what
1725 portion, assume all bits will be used since we don't know what will
1726 be happening.
1728 Similarly, set how many bits of X are known to be copies of the sign bit
1729 at all locations in the function. This is the smallest number implied
1730 by any set of X. */
1732 static void
1733 set_nonzero_bits_and_sign_copies (rtx x, const_rtx set, void *data)
1735 rtx_insn *insn = (rtx_insn *) data;
1736 scalar_int_mode mode;
1738 if (REG_P (x)
1739 && REGNO (x) >= FIRST_PSEUDO_REGISTER
1740 /* If this register is undefined at the start of the file, we can't
1741 say what its contents were. */
1742 && ! REGNO_REG_SET_P
1743 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb), REGNO (x))
1744 && is_a <scalar_int_mode> (GET_MODE (x), &mode)
1745 && HWI_COMPUTABLE_MODE_P (mode))
1747 reg_stat_type *rsp = &reg_stat[REGNO (x)];
1749 if (set == 0 || GET_CODE (set) == CLOBBER)
1751 rsp->nonzero_bits = GET_MODE_MASK (mode);
1752 rsp->sign_bit_copies = 1;
1753 return;
1756 /* Should not happen as we only using pseduo registers. */
1757 gcc_assert (GET_CODE (set) != CLOBBER_HIGH);
1759 /* If this register is being initialized using itself, and the
1760 register is uninitialized in this basic block, and there are
1761 no LOG_LINKS which set the register, then part of the
1762 register is uninitialized. In that case we can't assume
1763 anything about the number of nonzero bits.
1765 ??? We could do better if we checked this in
1766 reg_{nonzero_bits,num_sign_bit_copies}_for_combine. Then we
1767 could avoid making assumptions about the insn which initially
1768 sets the register, while still using the information in other
1769 insns. We would have to be careful to check every insn
1770 involved in the combination. */
1772 if (insn
1773 && reg_referenced_p (x, PATTERN (insn))
1774 && !REGNO_REG_SET_P (DF_LR_IN (BLOCK_FOR_INSN (insn)),
1775 REGNO (x)))
1777 struct insn_link *link;
1779 FOR_EACH_LOG_LINK (link, insn)
1780 if (dead_or_set_p (link->insn, x))
1781 break;
1782 if (!link)
1784 rsp->nonzero_bits = GET_MODE_MASK (mode);
1785 rsp->sign_bit_copies = 1;
1786 return;
1790 /* If this is a complex assignment, see if we can convert it into a
1791 simple assignment. */
1792 set = expand_field_assignment (set);
1794 /* If this is a simple assignment, or we have a paradoxical SUBREG,
1795 set what we know about X. */
1797 if (SET_DEST (set) == x
1798 || (paradoxical_subreg_p (SET_DEST (set))
1799 && SUBREG_REG (SET_DEST (set)) == x))
1800 update_rsp_from_reg_equal (rsp, insn, set, x);
1801 else
1803 rsp->nonzero_bits = GET_MODE_MASK (mode);
1804 rsp->sign_bit_copies = 1;
1809 /* See if INSN can be combined into I3. PRED, PRED2, SUCC and SUCC2 are
1810 optionally insns that were previously combined into I3 or that will be
1811 combined into the merger of INSN and I3. The order is PRED, PRED2,
1812 INSN, SUCC, SUCC2, I3.
1814 Return 0 if the combination is not allowed for any reason.
1816 If the combination is allowed, *PDEST will be set to the single
1817 destination of INSN and *PSRC to the single source, and this function
1818 will return 1. */
1820 static int
1821 can_combine_p (rtx_insn *insn, rtx_insn *i3, rtx_insn *pred ATTRIBUTE_UNUSED,
1822 rtx_insn *pred2 ATTRIBUTE_UNUSED, rtx_insn *succ, rtx_insn *succ2,
1823 rtx *pdest, rtx *psrc)
1825 int i;
1826 const_rtx set = 0;
1827 rtx src, dest;
1828 rtx_insn *p;
1829 rtx link;
1830 bool all_adjacent = true;
1831 int (*is_volatile_p) (const_rtx);
1833 if (succ)
1835 if (succ2)
1837 if (next_active_insn (succ2) != i3)
1838 all_adjacent = false;
1839 if (next_active_insn (succ) != succ2)
1840 all_adjacent = false;
1842 else if (next_active_insn (succ) != i3)
1843 all_adjacent = false;
1844 if (next_active_insn (insn) != succ)
1845 all_adjacent = false;
1847 else if (next_active_insn (insn) != i3)
1848 all_adjacent = false;
1850 /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
1851 or a PARALLEL consisting of such a SET and CLOBBERs.
1853 If INSN has CLOBBER parallel parts, ignore them for our processing.
1854 By definition, these happen during the execution of the insn. When it
1855 is merged with another insn, all bets are off. If they are, in fact,
1856 needed and aren't also supplied in I3, they may be added by
1857 recog_for_combine. Otherwise, it won't match.
1859 We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
1860 note.
1862 Get the source and destination of INSN. If more than one, can't
1863 combine. */
1865 if (GET_CODE (PATTERN (insn)) == SET)
1866 set = PATTERN (insn);
1867 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1868 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1870 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1872 rtx elt = XVECEXP (PATTERN (insn), 0, i);
1874 switch (GET_CODE (elt))
1876 /* This is important to combine floating point insns
1877 for the SH4 port. */
1878 case USE:
1879 /* Combining an isolated USE doesn't make sense.
1880 We depend here on combinable_i3pat to reject them. */
1881 /* The code below this loop only verifies that the inputs of
1882 the SET in INSN do not change. We call reg_set_between_p
1883 to verify that the REG in the USE does not change between
1884 I3 and INSN.
1885 If the USE in INSN was for a pseudo register, the matching
1886 insn pattern will likely match any register; combining this
1887 with any other USE would only be safe if we knew that the
1888 used registers have identical values, or if there was
1889 something to tell them apart, e.g. different modes. For
1890 now, we forgo such complicated tests and simply disallow
1891 combining of USES of pseudo registers with any other USE. */
1892 if (REG_P (XEXP (elt, 0))
1893 && GET_CODE (PATTERN (i3)) == PARALLEL)
1895 rtx i3pat = PATTERN (i3);
1896 int i = XVECLEN (i3pat, 0) - 1;
1897 unsigned int regno = REGNO (XEXP (elt, 0));
1901 rtx i3elt = XVECEXP (i3pat, 0, i);
1903 if (GET_CODE (i3elt) == USE
1904 && REG_P (XEXP (i3elt, 0))
1905 && (REGNO (XEXP (i3elt, 0)) == regno
1906 ? reg_set_between_p (XEXP (elt, 0),
1907 PREV_INSN (insn), i3)
1908 : regno >= FIRST_PSEUDO_REGISTER))
1909 return 0;
1911 while (--i >= 0);
1913 break;
1915 /* We can ignore CLOBBERs. */
1916 case CLOBBER:
1917 case CLOBBER_HIGH:
1918 break;
1920 case SET:
1921 /* Ignore SETs whose result isn't used but not those that
1922 have side-effects. */
1923 if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
1924 && insn_nothrow_p (insn)
1925 && !side_effects_p (elt))
1926 break;
1928 /* If we have already found a SET, this is a second one and
1929 so we cannot combine with this insn. */
1930 if (set)
1931 return 0;
1933 set = elt;
1934 break;
1936 default:
1937 /* Anything else means we can't combine. */
1938 return 0;
1942 if (set == 0
1943 /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
1944 so don't do anything with it. */
1945 || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
1946 return 0;
1948 else
1949 return 0;
1951 if (set == 0)
1952 return 0;
1954 /* The simplification in expand_field_assignment may call back to
1955 get_last_value, so set safe guard here. */
1956 subst_low_luid = DF_INSN_LUID (insn);
1958 set = expand_field_assignment (set);
1959 src = SET_SRC (set), dest = SET_DEST (set);
1961 /* Do not eliminate user-specified register if it is in an
1962 asm input because we may break the register asm usage defined
1963 in GCC manual if allow to do so.
1964 Be aware that this may cover more cases than we expect but this
1965 should be harmless. */
1966 if (REG_P (dest) && REG_USERVAR_P (dest) && HARD_REGISTER_P (dest)
1967 && extract_asm_operands (PATTERN (i3)))
1968 return 0;
1970 /* Don't eliminate a store in the stack pointer. */
1971 if (dest == stack_pointer_rtx
1972 /* Don't combine with an insn that sets a register to itself if it has
1973 a REG_EQUAL note. This may be part of a LIBCALL sequence. */
1974 || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1975 /* Can't merge an ASM_OPERANDS. */
1976 || GET_CODE (src) == ASM_OPERANDS
1977 /* Can't merge a function call. */
1978 || GET_CODE (src) == CALL
1979 /* Don't eliminate a function call argument. */
1980 || (CALL_P (i3)
1981 && (find_reg_fusage (i3, USE, dest)
1982 || (REG_P (dest)
1983 && REGNO (dest) < FIRST_PSEUDO_REGISTER
1984 && global_regs[REGNO (dest)])))
1985 /* Don't substitute into an incremented register. */
1986 || FIND_REG_INC_NOTE (i3, dest)
1987 || (succ && FIND_REG_INC_NOTE (succ, dest))
1988 || (succ2 && FIND_REG_INC_NOTE (succ2, dest))
1989 /* Don't substitute into a non-local goto, this confuses CFG. */
1990 || (JUMP_P (i3) && find_reg_note (i3, REG_NON_LOCAL_GOTO, NULL_RTX))
1991 /* Make sure that DEST is not used after INSN but before SUCC, or
1992 after SUCC and before SUCC2, or after SUCC2 but before I3. */
1993 || (!all_adjacent
1994 && ((succ2
1995 && (reg_used_between_p (dest, succ2, i3)
1996 || reg_used_between_p (dest, succ, succ2)))
1997 || (!succ2 && succ && reg_used_between_p (dest, succ, i3))
1998 || (!succ2 && !succ && reg_used_between_p (dest, insn, i3))
1999 || (succ
2000 /* SUCC and SUCC2 can be split halves from a PARALLEL; in
2001 that case SUCC is not in the insn stream, so use SUCC2
2002 instead for this test. */
2003 && reg_used_between_p (dest, insn,
2004 succ2
2005 && INSN_UID (succ) == INSN_UID (succ2)
2006 ? succ2 : succ))))
2007 /* Make sure that the value that is to be substituted for the register
2008 does not use any registers whose values alter in between. However,
2009 If the insns are adjacent, a use can't cross a set even though we
2010 think it might (this can happen for a sequence of insns each setting
2011 the same destination; last_set of that register might point to
2012 a NOTE). If INSN has a REG_EQUIV note, the register is always
2013 equivalent to the memory so the substitution is valid even if there
2014 are intervening stores. Also, don't move a volatile asm or
2015 UNSPEC_VOLATILE across any other insns. */
2016 || (! all_adjacent
2017 && (((!MEM_P (src)
2018 || ! find_reg_note (insn, REG_EQUIV, src))
2019 && modified_between_p (src, insn, i3))
2020 || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
2021 || GET_CODE (src) == UNSPEC_VOLATILE))
2022 /* Don't combine across a CALL_INSN, because that would possibly
2023 change whether the life span of some REGs crosses calls or not,
2024 and it is a pain to update that information.
2025 Exception: if source is a constant, moving it later can't hurt.
2026 Accept that as a special case. */
2027 || (DF_INSN_LUID (insn) < last_call_luid && ! CONSTANT_P (src)))
2028 return 0;
2030 /* DEST must either be a REG or CC0. */
2031 if (REG_P (dest))
2033 /* If register alignment is being enforced for multi-word items in all
2034 cases except for parameters, it is possible to have a register copy
2035 insn referencing a hard register that is not allowed to contain the
2036 mode being copied and which would not be valid as an operand of most
2037 insns. Eliminate this problem by not combining with such an insn.
2039 Also, on some machines we don't want to extend the life of a hard
2040 register. */
2042 if (REG_P (src)
2043 && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
2044 && !targetm.hard_regno_mode_ok (REGNO (dest), GET_MODE (dest)))
2045 /* Don't extend the life of a hard register unless it is
2046 user variable (if we have few registers) or it can't
2047 fit into the desired register (meaning something special
2048 is going on).
2049 Also avoid substituting a return register into I3, because
2050 reload can't handle a conflict with constraints of other
2051 inputs. */
2052 || (REGNO (src) < FIRST_PSEUDO_REGISTER
2053 && !targetm.hard_regno_mode_ok (REGNO (src),
2054 GET_MODE (src)))))
2055 return 0;
2057 else if (GET_CODE (dest) != CC0)
2058 return 0;
2061 if (GET_CODE (PATTERN (i3)) == PARALLEL)
2062 for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
2063 if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER)
2065 rtx reg = XEXP (XVECEXP (PATTERN (i3), 0, i), 0);
2067 /* If the clobber represents an earlyclobber operand, we must not
2068 substitute an expression containing the clobbered register.
2069 As we do not analyze the constraint strings here, we have to
2070 make the conservative assumption. However, if the register is
2071 a fixed hard reg, the clobber cannot represent any operand;
2072 we leave it up to the machine description to either accept or
2073 reject use-and-clobber patterns. */
2074 if (!REG_P (reg)
2075 || REGNO (reg) >= FIRST_PSEUDO_REGISTER
2076 || !fixed_regs[REGNO (reg)])
2077 if (reg_overlap_mentioned_p (reg, src))
2078 return 0;
2081 /* If INSN contains anything volatile, or is an `asm' (whether volatile
2082 or not), reject, unless nothing volatile comes between it and I3 */
2084 if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
2086 /* Make sure neither succ nor succ2 contains a volatile reference. */
2087 if (succ2 != 0 && volatile_refs_p (PATTERN (succ2)))
2088 return 0;
2089 if (succ != 0 && volatile_refs_p (PATTERN (succ)))
2090 return 0;
2091 /* We'll check insns between INSN and I3 below. */
2094 /* If INSN is an asm, and DEST is a hard register, reject, since it has
2095 to be an explicit register variable, and was chosen for a reason. */
2097 if (GET_CODE (src) == ASM_OPERANDS
2098 && REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER)
2099 return 0;
2101 /* If INSN contains volatile references (specifically volatile MEMs),
2102 we cannot combine across any other volatile references.
2103 Even if INSN doesn't contain volatile references, any intervening
2104 volatile insn might affect machine state. */
2106 is_volatile_p = volatile_refs_p (PATTERN (insn))
2107 ? volatile_refs_p
2108 : volatile_insn_p;
2110 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
2111 if (INSN_P (p) && p != succ && p != succ2 && is_volatile_p (PATTERN (p)))
2112 return 0;
2114 /* If INSN contains an autoincrement or autodecrement, make sure that
2115 register is not used between there and I3, and not already used in
2116 I3 either. Neither must it be used in PRED or SUCC, if they exist.
2117 Also insist that I3 not be a jump; if it were one
2118 and the incremented register were spilled, we would lose. */
2120 if (AUTO_INC_DEC)
2121 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
2122 if (REG_NOTE_KIND (link) == REG_INC
2123 && (JUMP_P (i3)
2124 || reg_used_between_p (XEXP (link, 0), insn, i3)
2125 || (pred != NULL_RTX
2126 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred)))
2127 || (pred2 != NULL_RTX
2128 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred2)))
2129 || (succ != NULL_RTX
2130 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ)))
2131 || (succ2 != NULL_RTX
2132 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ2)))
2133 || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
2134 return 0;
2136 /* Don't combine an insn that follows a CC0-setting insn.
2137 An insn that uses CC0 must not be separated from the one that sets it.
2138 We do, however, allow I2 to follow a CC0-setting insn if that insn
2139 is passed as I1; in that case it will be deleted also.
2140 We also allow combining in this case if all the insns are adjacent
2141 because that would leave the two CC0 insns adjacent as well.
2142 It would be more logical to test whether CC0 occurs inside I1 or I2,
2143 but that would be much slower, and this ought to be equivalent. */
2145 if (HAVE_cc0)
2147 p = prev_nonnote_insn (insn);
2148 if (p && p != pred && NONJUMP_INSN_P (p) && sets_cc0_p (PATTERN (p))
2149 && ! all_adjacent)
2150 return 0;
2153 /* If we get here, we have passed all the tests and the combination is
2154 to be allowed. */
2156 *pdest = dest;
2157 *psrc = src;
2159 return 1;
2162 /* LOC is the location within I3 that contains its pattern or the component
2163 of a PARALLEL of the pattern. We validate that it is valid for combining.
2165 One problem is if I3 modifies its output, as opposed to replacing it
2166 entirely, we can't allow the output to contain I2DEST, I1DEST or I0DEST as
2167 doing so would produce an insn that is not equivalent to the original insns.
2169 Consider:
2171 (set (reg:DI 101) (reg:DI 100))
2172 (set (subreg:SI (reg:DI 101) 0) <foo>)
2174 This is NOT equivalent to:
2176 (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
2177 (set (reg:DI 101) (reg:DI 100))])
2179 Not only does this modify 100 (in which case it might still be valid
2180 if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
2182 We can also run into a problem if I2 sets a register that I1
2183 uses and I1 gets directly substituted into I3 (not via I2). In that
2184 case, we would be getting the wrong value of I2DEST into I3, so we
2185 must reject the combination. This case occurs when I2 and I1 both
2186 feed into I3, rather than when I1 feeds into I2, which feeds into I3.
2187 If I1_NOT_IN_SRC is nonzero, it means that finding I1 in the source
2188 of a SET must prevent combination from occurring. The same situation
2189 can occur for I0, in which case I0_NOT_IN_SRC is set.
2191 Before doing the above check, we first try to expand a field assignment
2192 into a set of logical operations.
2194 If PI3_DEST_KILLED is nonzero, it is a pointer to a location in which
2195 we place a register that is both set and used within I3. If more than one
2196 such register is detected, we fail.
2198 Return 1 if the combination is valid, zero otherwise. */
2200 static int
2201 combinable_i3pat (rtx_insn *i3, rtx *loc, rtx i2dest, rtx i1dest, rtx i0dest,
2202 int i1_not_in_src, int i0_not_in_src, rtx *pi3dest_killed)
2204 rtx x = *loc;
2206 if (GET_CODE (x) == SET)
2208 rtx set = x ;
2209 rtx dest = SET_DEST (set);
2210 rtx src = SET_SRC (set);
2211 rtx inner_dest = dest;
2212 rtx subdest;
2214 while (GET_CODE (inner_dest) == STRICT_LOW_PART
2215 || GET_CODE (inner_dest) == SUBREG
2216 || GET_CODE (inner_dest) == ZERO_EXTRACT)
2217 inner_dest = XEXP (inner_dest, 0);
2219 /* Check for the case where I3 modifies its output, as discussed
2220 above. We don't want to prevent pseudos from being combined
2221 into the address of a MEM, so only prevent the combination if
2222 i1 or i2 set the same MEM. */
2223 if ((inner_dest != dest &&
2224 (!MEM_P (inner_dest)
2225 || rtx_equal_p (i2dest, inner_dest)
2226 || (i1dest && rtx_equal_p (i1dest, inner_dest))
2227 || (i0dest && rtx_equal_p (i0dest, inner_dest)))
2228 && (reg_overlap_mentioned_p (i2dest, inner_dest)
2229 || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))
2230 || (i0dest && reg_overlap_mentioned_p (i0dest, inner_dest))))
2232 /* This is the same test done in can_combine_p except we can't test
2233 all_adjacent; we don't have to, since this instruction will stay
2234 in place, thus we are not considering increasing the lifetime of
2235 INNER_DEST.
2237 Also, if this insn sets a function argument, combining it with
2238 something that might need a spill could clobber a previous
2239 function argument; the all_adjacent test in can_combine_p also
2240 checks this; here, we do a more specific test for this case. */
2242 || (REG_P (inner_dest)
2243 && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
2244 && !targetm.hard_regno_mode_ok (REGNO (inner_dest),
2245 GET_MODE (inner_dest)))
2246 || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src))
2247 || (i0_not_in_src && reg_overlap_mentioned_p (i0dest, src)))
2248 return 0;
2250 /* If DEST is used in I3, it is being killed in this insn, so
2251 record that for later. We have to consider paradoxical
2252 subregs here, since they kill the whole register, but we
2253 ignore partial subregs, STRICT_LOW_PART, etc.
2254 Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
2255 STACK_POINTER_REGNUM, since these are always considered to be
2256 live. Similarly for ARG_POINTER_REGNUM if it is fixed. */
2257 subdest = dest;
2258 if (GET_CODE (subdest) == SUBREG && !partial_subreg_p (subdest))
2259 subdest = SUBREG_REG (subdest);
2260 if (pi3dest_killed
2261 && REG_P (subdest)
2262 && reg_referenced_p (subdest, PATTERN (i3))
2263 && REGNO (subdest) != FRAME_POINTER_REGNUM
2264 && (HARD_FRAME_POINTER_IS_FRAME_POINTER
2265 || REGNO (subdest) != HARD_FRAME_POINTER_REGNUM)
2266 && (FRAME_POINTER_REGNUM == ARG_POINTER_REGNUM
2267 || (REGNO (subdest) != ARG_POINTER_REGNUM
2268 || ! fixed_regs [REGNO (subdest)]))
2269 && REGNO (subdest) != STACK_POINTER_REGNUM)
2271 if (*pi3dest_killed)
2272 return 0;
2274 *pi3dest_killed = subdest;
2278 else if (GET_CODE (x) == PARALLEL)
2280 int i;
2282 for (i = 0; i < XVECLEN (x, 0); i++)
2283 if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest, i0dest,
2284 i1_not_in_src, i0_not_in_src, pi3dest_killed))
2285 return 0;
2288 return 1;
2291 /* Return 1 if X is an arithmetic expression that contains a multiplication
2292 and division. We don't count multiplications by powers of two here. */
2294 static int
2295 contains_muldiv (rtx x)
2297 switch (GET_CODE (x))
2299 case MOD: case DIV: case UMOD: case UDIV:
2300 return 1;
2302 case MULT:
2303 return ! (CONST_INT_P (XEXP (x, 1))
2304 && pow2p_hwi (UINTVAL (XEXP (x, 1))));
2305 default:
2306 if (BINARY_P (x))
2307 return contains_muldiv (XEXP (x, 0))
2308 || contains_muldiv (XEXP (x, 1));
2310 if (UNARY_P (x))
2311 return contains_muldiv (XEXP (x, 0));
2313 return 0;
2317 /* Determine whether INSN can be used in a combination. Return nonzero if
2318 not. This is used in try_combine to detect early some cases where we
2319 can't perform combinations. */
2321 static int
2322 cant_combine_insn_p (rtx_insn *insn)
2324 rtx set;
2325 rtx src, dest;
2327 /* If this isn't really an insn, we can't do anything.
2328 This can occur when flow deletes an insn that it has merged into an
2329 auto-increment address. */
2330 if (!NONDEBUG_INSN_P (insn))
2331 return 1;
2333 /* Never combine loads and stores involving hard regs that are likely
2334 to be spilled. The register allocator can usually handle such
2335 reg-reg moves by tying. If we allow the combiner to make
2336 substitutions of likely-spilled regs, reload might die.
2337 As an exception, we allow combinations involving fixed regs; these are
2338 not available to the register allocator so there's no risk involved. */
2340 set = single_set (insn);
2341 if (! set)
2342 return 0;
2343 src = SET_SRC (set);
2344 dest = SET_DEST (set);
2345 if (GET_CODE (src) == SUBREG)
2346 src = SUBREG_REG (src);
2347 if (GET_CODE (dest) == SUBREG)
2348 dest = SUBREG_REG (dest);
2349 if (REG_P (src) && REG_P (dest)
2350 && ((HARD_REGISTER_P (src)
2351 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (src))
2352 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (src))))
2353 || (HARD_REGISTER_P (dest)
2354 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (dest))
2355 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (dest))))))
2356 return 1;
2358 return 0;
2361 struct likely_spilled_retval_info
2363 unsigned regno, nregs;
2364 unsigned mask;
2367 /* Called via note_stores by likely_spilled_retval_p. Remove from info->mask
2368 hard registers that are known to be written to / clobbered in full. */
2369 static void
2370 likely_spilled_retval_1 (rtx x, const_rtx set, void *data)
2372 struct likely_spilled_retval_info *const info =
2373 (struct likely_spilled_retval_info *) data;
2374 unsigned regno, nregs;
2375 unsigned new_mask;
2377 if (!REG_P (XEXP (set, 0)))
2378 return;
2379 regno = REGNO (x);
2380 if (regno >= info->regno + info->nregs)
2381 return;
2382 nregs = REG_NREGS (x);
2383 if (regno + nregs <= info->regno)
2384 return;
2385 new_mask = (2U << (nregs - 1)) - 1;
2386 if (regno < info->regno)
2387 new_mask >>= info->regno - regno;
2388 else
2389 new_mask <<= regno - info->regno;
2390 info->mask &= ~new_mask;
2393 /* Return nonzero iff part of the return value is live during INSN, and
2394 it is likely spilled. This can happen when more than one insn is needed
2395 to copy the return value, e.g. when we consider to combine into the
2396 second copy insn for a complex value. */
2398 static int
2399 likely_spilled_retval_p (rtx_insn *insn)
2401 rtx_insn *use = BB_END (this_basic_block);
2402 rtx reg;
2403 rtx_insn *p;
2404 unsigned regno, nregs;
2405 /* We assume here that no machine mode needs more than
2406 32 hard registers when the value overlaps with a register
2407 for which TARGET_FUNCTION_VALUE_REGNO_P is true. */
2408 unsigned mask;
2409 struct likely_spilled_retval_info info;
2411 if (!NONJUMP_INSN_P (use) || GET_CODE (PATTERN (use)) != USE || insn == use)
2412 return 0;
2413 reg = XEXP (PATTERN (use), 0);
2414 if (!REG_P (reg) || !targetm.calls.function_value_regno_p (REGNO (reg)))
2415 return 0;
2416 regno = REGNO (reg);
2417 nregs = REG_NREGS (reg);
2418 if (nregs == 1)
2419 return 0;
2420 mask = (2U << (nregs - 1)) - 1;
2422 /* Disregard parts of the return value that are set later. */
2423 info.regno = regno;
2424 info.nregs = nregs;
2425 info.mask = mask;
2426 for (p = PREV_INSN (use); info.mask && p != insn; p = PREV_INSN (p))
2427 if (INSN_P (p))
2428 note_stores (PATTERN (p), likely_spilled_retval_1, &info);
2429 mask = info.mask;
2431 /* Check if any of the (probably) live return value registers is
2432 likely spilled. */
2433 nregs --;
2436 if ((mask & 1 << nregs)
2437 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (regno + nregs)))
2438 return 1;
2439 } while (nregs--);
2440 return 0;
2443 /* Adjust INSN after we made a change to its destination.
2445 Changing the destination can invalidate notes that say something about
2446 the results of the insn and a LOG_LINK pointing to the insn. */
2448 static void
2449 adjust_for_new_dest (rtx_insn *insn)
2451 /* For notes, be conservative and simply remove them. */
2452 remove_reg_equal_equiv_notes (insn);
2454 /* The new insn will have a destination that was previously the destination
2455 of an insn just above it. Call distribute_links to make a LOG_LINK from
2456 the next use of that destination. */
2458 rtx set = single_set (insn);
2459 gcc_assert (set);
2461 rtx reg = SET_DEST (set);
2463 while (GET_CODE (reg) == ZERO_EXTRACT
2464 || GET_CODE (reg) == STRICT_LOW_PART
2465 || GET_CODE (reg) == SUBREG)
2466 reg = XEXP (reg, 0);
2467 gcc_assert (REG_P (reg));
2469 distribute_links (alloc_insn_link (insn, REGNO (reg), NULL));
2471 df_insn_rescan (insn);
2474 /* Return TRUE if combine can reuse reg X in mode MODE.
2475 ADDED_SETS is nonzero if the original set is still required. */
2476 static bool
2477 can_change_dest_mode (rtx x, int added_sets, machine_mode mode)
2479 unsigned int regno;
2481 if (!REG_P (x))
2482 return false;
2484 /* Don't change between modes with different underlying register sizes,
2485 since this could lead to invalid subregs. */
2486 if (maybe_ne (REGMODE_NATURAL_SIZE (mode),
2487 REGMODE_NATURAL_SIZE (GET_MODE (x))))
2488 return false;
2490 regno = REGNO (x);
2491 /* Allow hard registers if the new mode is legal, and occupies no more
2492 registers than the old mode. */
2493 if (regno < FIRST_PSEUDO_REGISTER)
2494 return (targetm.hard_regno_mode_ok (regno, mode)
2495 && REG_NREGS (x) >= hard_regno_nregs (regno, mode));
2497 /* Or a pseudo that is only used once. */
2498 return (regno < reg_n_sets_max
2499 && REG_N_SETS (regno) == 1
2500 && !added_sets
2501 && !REG_USERVAR_P (x));
2505 /* Check whether X, the destination of a set, refers to part of
2506 the register specified by REG. */
2508 static bool
2509 reg_subword_p (rtx x, rtx reg)
2511 /* Check that reg is an integer mode register. */
2512 if (!REG_P (reg) || GET_MODE_CLASS (GET_MODE (reg)) != MODE_INT)
2513 return false;
2515 if (GET_CODE (x) == STRICT_LOW_PART
2516 || GET_CODE (x) == ZERO_EXTRACT)
2517 x = XEXP (x, 0);
2519 return GET_CODE (x) == SUBREG
2520 && SUBREG_REG (x) == reg
2521 && GET_MODE_CLASS (GET_MODE (x)) == MODE_INT;
2524 /* Delete the unconditional jump INSN and adjust the CFG correspondingly.
2525 Note that the INSN should be deleted *after* removing dead edges, so
2526 that the kept edge is the fallthrough edge for a (set (pc) (pc))
2527 but not for a (set (pc) (label_ref FOO)). */
2529 static void
2530 update_cfg_for_uncondjump (rtx_insn *insn)
2532 basic_block bb = BLOCK_FOR_INSN (insn);
2533 gcc_assert (BB_END (bb) == insn);
2535 purge_dead_edges (bb);
2537 delete_insn (insn);
2538 if (EDGE_COUNT (bb->succs) == 1)
2540 rtx_insn *insn;
2542 single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
2544 /* Remove barriers from the footer if there are any. */
2545 for (insn = BB_FOOTER (bb); insn; insn = NEXT_INSN (insn))
2546 if (BARRIER_P (insn))
2548 if (PREV_INSN (insn))
2549 SET_NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
2550 else
2551 BB_FOOTER (bb) = NEXT_INSN (insn);
2552 if (NEXT_INSN (insn))
2553 SET_PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
2555 else if (LABEL_P (insn))
2556 break;
2560 /* Return whether PAT is a PARALLEL of exactly N register SETs followed
2561 by an arbitrary number of CLOBBERs. */
2562 static bool
2563 is_parallel_of_n_reg_sets (rtx pat, int n)
2565 if (GET_CODE (pat) != PARALLEL)
2566 return false;
2568 int len = XVECLEN (pat, 0);
2569 if (len < n)
2570 return false;
2572 int i;
2573 for (i = 0; i < n; i++)
2574 if (GET_CODE (XVECEXP (pat, 0, i)) != SET
2575 || !REG_P (SET_DEST (XVECEXP (pat, 0, i))))
2576 return false;
2577 for ( ; i < len; i++)
2578 switch (GET_CODE (XVECEXP (pat, 0, i)))
2580 case CLOBBER:
2581 if (XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
2582 return false;
2583 break;
2584 case CLOBBER_HIGH:
2585 break;
2586 default:
2587 return false;
2589 return true;
2592 /* Return whether INSN, a PARALLEL of N register SETs (and maybe some
2593 CLOBBERs), can be split into individual SETs in that order, without
2594 changing semantics. */
2595 static bool
2596 can_split_parallel_of_n_reg_sets (rtx_insn *insn, int n)
2598 if (!insn_nothrow_p (insn))
2599 return false;
2601 rtx pat = PATTERN (insn);
2603 int i, j;
2604 for (i = 0; i < n; i++)
2606 if (side_effects_p (SET_SRC (XVECEXP (pat, 0, i))))
2607 return false;
2609 rtx reg = SET_DEST (XVECEXP (pat, 0, i));
2611 for (j = i + 1; j < n; j++)
2612 if (reg_referenced_p (reg, XVECEXP (pat, 0, j)))
2613 return false;
2616 return true;
2619 /* Return whether X is just a single set, with the source
2620 a general_operand. */
2621 static bool
2622 is_just_move (rtx x)
2624 if (INSN_P (x))
2625 x = PATTERN (x);
2627 return (GET_CODE (x) == SET && general_operand (SET_SRC (x), VOIDmode));
2630 /* Try to combine the insns I0, I1 and I2 into I3.
2631 Here I0, I1 and I2 appear earlier than I3.
2632 I0 and I1 can be zero; then we combine just I2 into I3, or I1 and I2 into
2635 If we are combining more than two insns and the resulting insn is not
2636 recognized, try splitting it into two insns. If that happens, I2 and I3
2637 are retained and I1/I0 are pseudo-deleted by turning them into a NOTE.
2638 Otherwise, I0, I1 and I2 are pseudo-deleted.
2640 Return 0 if the combination does not work. Then nothing is changed.
2641 If we did the combination, return the insn at which combine should
2642 resume scanning.
2644 Set NEW_DIRECT_JUMP_P to a nonzero value if try_combine creates a
2645 new direct jump instruction.
2647 LAST_COMBINED_INSN is either I3, or some insn after I3 that has
2648 been I3 passed to an earlier try_combine within the same basic
2649 block. */
2651 static rtx_insn *
2652 try_combine (rtx_insn *i3, rtx_insn *i2, rtx_insn *i1, rtx_insn *i0,
2653 int *new_direct_jump_p, rtx_insn *last_combined_insn)
2655 /* New patterns for I3 and I2, respectively. */
2656 rtx newpat, newi2pat = 0;
2657 rtvec newpat_vec_with_clobbers = 0;
2658 int substed_i2 = 0, substed_i1 = 0, substed_i0 = 0;
2659 /* Indicates need to preserve SET in I0, I1 or I2 in I3 if it is not
2660 dead. */
2661 int added_sets_0, added_sets_1, added_sets_2;
2662 /* Total number of SETs to put into I3. */
2663 int total_sets;
2664 /* Nonzero if I2's or I1's body now appears in I3. */
2665 int i2_is_used = 0, i1_is_used = 0;
2666 /* INSN_CODEs for new I3, new I2, and user of condition code. */
2667 int insn_code_number, i2_code_number = 0, other_code_number = 0;
2668 /* Contains I3 if the destination of I3 is used in its source, which means
2669 that the old life of I3 is being killed. If that usage is placed into
2670 I2 and not in I3, a REG_DEAD note must be made. */
2671 rtx i3dest_killed = 0;
2672 /* SET_DEST and SET_SRC of I2, I1 and I0. */
2673 rtx i2dest = 0, i2src = 0, i1dest = 0, i1src = 0, i0dest = 0, i0src = 0;
2674 /* Copy of SET_SRC of I1 and I0, if needed. */
2675 rtx i1src_copy = 0, i0src_copy = 0, i0src_copy2 = 0;
2676 /* Set if I2DEST was reused as a scratch register. */
2677 bool i2scratch = false;
2678 /* The PATTERNs of I0, I1, and I2, or a copy of them in certain cases. */
2679 rtx i0pat = 0, i1pat = 0, i2pat = 0;
2680 /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC. */
2681 int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
2682 int i0dest_in_i0src = 0, i1dest_in_i0src = 0, i2dest_in_i0src = 0;
2683 int i2dest_killed = 0, i1dest_killed = 0, i0dest_killed = 0;
2684 int i1_feeds_i2_n = 0, i0_feeds_i2_n = 0, i0_feeds_i1_n = 0;
2685 /* Notes that must be added to REG_NOTES in I3 and I2. */
2686 rtx new_i3_notes, new_i2_notes;
2687 /* Notes that we substituted I3 into I2 instead of the normal case. */
2688 int i3_subst_into_i2 = 0;
2689 /* Notes that I1, I2 or I3 is a MULT operation. */
2690 int have_mult = 0;
2691 int swap_i2i3 = 0;
2692 int split_i2i3 = 0;
2693 int changed_i3_dest = 0;
2694 bool i2_was_move = false, i3_was_move = false;
2696 int maxreg;
2697 rtx_insn *temp_insn;
2698 rtx temp_expr;
2699 struct insn_link *link;
2700 rtx other_pat = 0;
2701 rtx new_other_notes;
2702 int i;
2703 scalar_int_mode dest_mode, temp_mode;
2705 /* Immediately return if any of I0,I1,I2 are the same insn (I3 can
2706 never be). */
2707 if (i1 == i2 || i0 == i2 || (i0 && i0 == i1))
2708 return 0;
2710 /* Only try four-insn combinations when there's high likelihood of
2711 success. Look for simple insns, such as loads of constants or
2712 binary operations involving a constant. */
2713 if (i0)
2715 int i;
2716 int ngood = 0;
2717 int nshift = 0;
2718 rtx set0, set3;
2720 if (!flag_expensive_optimizations)
2721 return 0;
2723 for (i = 0; i < 4; i++)
2725 rtx_insn *insn = i == 0 ? i0 : i == 1 ? i1 : i == 2 ? i2 : i3;
2726 rtx set = single_set (insn);
2727 rtx src;
2728 if (!set)
2729 continue;
2730 src = SET_SRC (set);
2731 if (CONSTANT_P (src))
2733 ngood += 2;
2734 break;
2736 else if (BINARY_P (src) && CONSTANT_P (XEXP (src, 1)))
2737 ngood++;
2738 else if (GET_CODE (src) == ASHIFT || GET_CODE (src) == ASHIFTRT
2739 || GET_CODE (src) == LSHIFTRT)
2740 nshift++;
2743 /* If I0 loads a memory and I3 sets the same memory, then I1 and I2
2744 are likely manipulating its value. Ideally we'll be able to combine
2745 all four insns into a bitfield insertion of some kind.
2747 Note the source in I0 might be inside a sign/zero extension and the
2748 memory modes in I0 and I3 might be different. So extract the address
2749 from the destination of I3 and search for it in the source of I0.
2751 In the event that there's a match but the source/dest do not actually
2752 refer to the same memory, the worst that happens is we try some
2753 combinations that we wouldn't have otherwise. */
2754 if ((set0 = single_set (i0))
2755 /* Ensure the source of SET0 is a MEM, possibly buried inside
2756 an extension. */
2757 && (GET_CODE (SET_SRC (set0)) == MEM
2758 || ((GET_CODE (SET_SRC (set0)) == ZERO_EXTEND
2759 || GET_CODE (SET_SRC (set0)) == SIGN_EXTEND)
2760 && GET_CODE (XEXP (SET_SRC (set0), 0)) == MEM))
2761 && (set3 = single_set (i3))
2762 /* Ensure the destination of SET3 is a MEM. */
2763 && GET_CODE (SET_DEST (set3)) == MEM
2764 /* Would it be better to extract the base address for the MEM
2765 in SET3 and look for that? I don't have cases where it matters
2766 but I could envision such cases. */
2767 && rtx_referenced_p (XEXP (SET_DEST (set3), 0), SET_SRC (set0)))
2768 ngood += 2;
2770 if (ngood < 2 && nshift < 2)
2771 return 0;
2774 /* Exit early if one of the insns involved can't be used for
2775 combinations. */
2776 if (CALL_P (i2)
2777 || (i1 && CALL_P (i1))
2778 || (i0 && CALL_P (i0))
2779 || cant_combine_insn_p (i3)
2780 || cant_combine_insn_p (i2)
2781 || (i1 && cant_combine_insn_p (i1))
2782 || (i0 && cant_combine_insn_p (i0))
2783 || likely_spilled_retval_p (i3))
2784 return 0;
2786 combine_attempts++;
2787 undobuf.other_insn = 0;
2789 /* Reset the hard register usage information. */
2790 CLEAR_HARD_REG_SET (newpat_used_regs);
2792 if (dump_file && (dump_flags & TDF_DETAILS))
2794 if (i0)
2795 fprintf (dump_file, "\nTrying %d, %d, %d -> %d:\n",
2796 INSN_UID (i0), INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2797 else if (i1)
2798 fprintf (dump_file, "\nTrying %d, %d -> %d:\n",
2799 INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2800 else
2801 fprintf (dump_file, "\nTrying %d -> %d:\n",
2802 INSN_UID (i2), INSN_UID (i3));
2804 if (i0)
2805 dump_insn_slim (dump_file, i0);
2806 if (i1)
2807 dump_insn_slim (dump_file, i1);
2808 dump_insn_slim (dump_file, i2);
2809 dump_insn_slim (dump_file, i3);
2812 /* If multiple insns feed into one of I2 or I3, they can be in any
2813 order. To simplify the code below, reorder them in sequence. */
2814 if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i2))
2815 std::swap (i0, i2);
2816 if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i1))
2817 std::swap (i0, i1);
2818 if (i1 && DF_INSN_LUID (i1) > DF_INSN_LUID (i2))
2819 std::swap (i1, i2);
2821 added_links_insn = 0;
2822 added_notes_insn = 0;
2824 /* First check for one important special case that the code below will
2825 not handle. Namely, the case where I1 is zero, I2 is a PARALLEL
2826 and I3 is a SET whose SET_SRC is a SET_DEST in I2. In that case,
2827 we may be able to replace that destination with the destination of I3.
2828 This occurs in the common code where we compute both a quotient and
2829 remainder into a structure, in which case we want to do the computation
2830 directly into the structure to avoid register-register copies.
2832 Note that this case handles both multiple sets in I2 and also cases
2833 where I2 has a number of CLOBBERs inside the PARALLEL.
2835 We make very conservative checks below and only try to handle the
2836 most common cases of this. For example, we only handle the case
2837 where I2 and I3 are adjacent to avoid making difficult register
2838 usage tests. */
2840 if (i1 == 0 && NONJUMP_INSN_P (i3) && GET_CODE (PATTERN (i3)) == SET
2841 && REG_P (SET_SRC (PATTERN (i3)))
2842 && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
2843 && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
2844 && GET_CODE (PATTERN (i2)) == PARALLEL
2845 && ! side_effects_p (SET_DEST (PATTERN (i3)))
2846 /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
2847 below would need to check what is inside (and reg_overlap_mentioned_p
2848 doesn't support those codes anyway). Don't allow those destinations;
2849 the resulting insn isn't likely to be recognized anyway. */
2850 && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
2851 && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
2852 && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
2853 SET_DEST (PATTERN (i3)))
2854 && next_active_insn (i2) == i3)
2856 rtx p2 = PATTERN (i2);
2858 /* Make sure that the destination of I3,
2859 which we are going to substitute into one output of I2,
2860 is not used within another output of I2. We must avoid making this:
2861 (parallel [(set (mem (reg 69)) ...)
2862 (set (reg 69) ...)])
2863 which is not well-defined as to order of actions.
2864 (Besides, reload can't handle output reloads for this.)
2866 The problem can also happen if the dest of I3 is a memory ref,
2867 if another dest in I2 is an indirect memory ref.
2869 Neither can this PARALLEL be an asm. We do not allow combining
2870 that usually (see can_combine_p), so do not here either. */
2871 bool ok = true;
2872 for (i = 0; ok && i < XVECLEN (p2, 0); i++)
2874 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
2875 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER
2876 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER_HIGH)
2877 && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
2878 SET_DEST (XVECEXP (p2, 0, i))))
2879 ok = false;
2880 else if (GET_CODE (XVECEXP (p2, 0, i)) == SET
2881 && GET_CODE (SET_SRC (XVECEXP (p2, 0, i))) == ASM_OPERANDS)
2882 ok = false;
2885 if (ok)
2886 for (i = 0; i < XVECLEN (p2, 0); i++)
2887 if (GET_CODE (XVECEXP (p2, 0, i)) == SET
2888 && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
2890 combine_merges++;
2892 subst_insn = i3;
2893 subst_low_luid = DF_INSN_LUID (i2);
2895 added_sets_2 = added_sets_1 = added_sets_0 = 0;
2896 i2src = SET_SRC (XVECEXP (p2, 0, i));
2897 i2dest = SET_DEST (XVECEXP (p2, 0, i));
2898 i2dest_killed = dead_or_set_p (i2, i2dest);
2900 /* Replace the dest in I2 with our dest and make the resulting
2901 insn the new pattern for I3. Then skip to where we validate
2902 the pattern. Everything was set up above. */
2903 SUBST (SET_DEST (XVECEXP (p2, 0, i)), SET_DEST (PATTERN (i3)));
2904 newpat = p2;
2905 i3_subst_into_i2 = 1;
2906 goto validate_replacement;
2910 /* If I2 is setting a pseudo to a constant and I3 is setting some
2911 sub-part of it to another constant, merge them by making a new
2912 constant. */
2913 if (i1 == 0
2914 && (temp_expr = single_set (i2)) != 0
2915 && is_a <scalar_int_mode> (GET_MODE (SET_DEST (temp_expr)), &temp_mode)
2916 && CONST_SCALAR_INT_P (SET_SRC (temp_expr))
2917 && GET_CODE (PATTERN (i3)) == SET
2918 && CONST_SCALAR_INT_P (SET_SRC (PATTERN (i3)))
2919 && reg_subword_p (SET_DEST (PATTERN (i3)), SET_DEST (temp_expr)))
2921 rtx dest = SET_DEST (PATTERN (i3));
2922 rtx temp_dest = SET_DEST (temp_expr);
2923 int offset = -1;
2924 int width = 0;
2926 if (GET_CODE (dest) == ZERO_EXTRACT)
2928 if (CONST_INT_P (XEXP (dest, 1))
2929 && CONST_INT_P (XEXP (dest, 2))
2930 && is_a <scalar_int_mode> (GET_MODE (XEXP (dest, 0)),
2931 &dest_mode))
2933 width = INTVAL (XEXP (dest, 1));
2934 offset = INTVAL (XEXP (dest, 2));
2935 dest = XEXP (dest, 0);
2936 if (BITS_BIG_ENDIAN)
2937 offset = GET_MODE_PRECISION (dest_mode) - width - offset;
2940 else
2942 if (GET_CODE (dest) == STRICT_LOW_PART)
2943 dest = XEXP (dest, 0);
2944 if (is_a <scalar_int_mode> (GET_MODE (dest), &dest_mode))
2946 width = GET_MODE_PRECISION (dest_mode);
2947 offset = 0;
2951 if (offset >= 0)
2953 /* If this is the low part, we're done. */
2954 if (subreg_lowpart_p (dest))
2956 /* Handle the case where inner is twice the size of outer. */
2957 else if (GET_MODE_PRECISION (temp_mode)
2958 == 2 * GET_MODE_PRECISION (dest_mode))
2959 offset += GET_MODE_PRECISION (dest_mode);
2960 /* Otherwise give up for now. */
2961 else
2962 offset = -1;
2965 if (offset >= 0)
2967 rtx inner = SET_SRC (PATTERN (i3));
2968 rtx outer = SET_SRC (temp_expr);
2970 wide_int o = wi::insert (rtx_mode_t (outer, temp_mode),
2971 rtx_mode_t (inner, dest_mode),
2972 offset, width);
2974 combine_merges++;
2975 subst_insn = i3;
2976 subst_low_luid = DF_INSN_LUID (i2);
2977 added_sets_2 = added_sets_1 = added_sets_0 = 0;
2978 i2dest = temp_dest;
2979 i2dest_killed = dead_or_set_p (i2, i2dest);
2981 /* Replace the source in I2 with the new constant and make the
2982 resulting insn the new pattern for I3. Then skip to where we
2983 validate the pattern. Everything was set up above. */
2984 SUBST (SET_SRC (temp_expr),
2985 immed_wide_int_const (o, temp_mode));
2987 newpat = PATTERN (i2);
2989 /* The dest of I3 has been replaced with the dest of I2. */
2990 changed_i3_dest = 1;
2991 goto validate_replacement;
2995 /* If we have no I1 and I2 looks like:
2996 (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
2997 (set Y OP)])
2998 make up a dummy I1 that is
2999 (set Y OP)
3000 and change I2 to be
3001 (set (reg:CC X) (compare:CC Y (const_int 0)))
3003 (We can ignore any trailing CLOBBERs.)
3005 This undoes a previous combination and allows us to match a branch-and-
3006 decrement insn. */
3008 if (!HAVE_cc0 && i1 == 0
3009 && is_parallel_of_n_reg_sets (PATTERN (i2), 2)
3010 && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
3011 == MODE_CC)
3012 && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
3013 && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
3014 && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
3015 SET_SRC (XVECEXP (PATTERN (i2), 0, 1)))
3016 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 0)), i2, i3)
3017 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)), i2, i3))
3019 /* We make I1 with the same INSN_UID as I2. This gives it
3020 the same DF_INSN_LUID for value tracking. Our fake I1 will
3021 never appear in the insn stream so giving it the same INSN_UID
3022 as I2 will not cause a problem. */
3024 i1 = gen_rtx_INSN (VOIDmode, NULL, i2, BLOCK_FOR_INSN (i2),
3025 XVECEXP (PATTERN (i2), 0, 1), INSN_LOCATION (i2),
3026 -1, NULL_RTX);
3027 INSN_UID (i1) = INSN_UID (i2);
3029 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
3030 SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
3031 SET_DEST (PATTERN (i1)));
3032 unsigned int regno = REGNO (SET_DEST (PATTERN (i1)));
3033 SUBST_LINK (LOG_LINKS (i2),
3034 alloc_insn_link (i1, regno, LOG_LINKS (i2)));
3037 /* If I2 is a PARALLEL of two SETs of REGs (and perhaps some CLOBBERs),
3038 make those two SETs separate I1 and I2 insns, and make an I0 that is
3039 the original I1. */
3040 if (!HAVE_cc0 && i0 == 0
3041 && is_parallel_of_n_reg_sets (PATTERN (i2), 2)
3042 && can_split_parallel_of_n_reg_sets (i2, 2)
3043 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 0)), i2, i3)
3044 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)), i2, i3)
3045 && !reg_set_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 0)), i2, i3)
3046 && !reg_set_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)), i2, i3))
3048 /* If there is no I1, there is no I0 either. */
3049 i0 = i1;
3051 /* We make I1 with the same INSN_UID as I2. This gives it
3052 the same DF_INSN_LUID for value tracking. Our fake I1 will
3053 never appear in the insn stream so giving it the same INSN_UID
3054 as I2 will not cause a problem. */
3056 i1 = gen_rtx_INSN (VOIDmode, NULL, i2, BLOCK_FOR_INSN (i2),
3057 XVECEXP (PATTERN (i2), 0, 0), INSN_LOCATION (i2),
3058 -1, NULL_RTX);
3059 INSN_UID (i1) = INSN_UID (i2);
3061 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 1));
3064 /* Verify that I2 and maybe I1 and I0 can be combined into I3. */
3065 if (!can_combine_p (i2, i3, i0, i1, NULL, NULL, &i2dest, &i2src))
3067 if (dump_file)
3068 fprintf (dump_file, "Can't combine i2 into i3\n");
3069 undo_all ();
3070 return 0;
3072 if (i1 && !can_combine_p (i1, i3, i0, NULL, i2, NULL, &i1dest, &i1src))
3074 if (dump_file)
3075 fprintf (dump_file, "Can't combine i1 into i3\n");
3076 undo_all ();
3077 return 0;
3079 if (i0 && !can_combine_p (i0, i3, NULL, NULL, i1, i2, &i0dest, &i0src))
3081 if (dump_file)
3082 fprintf (dump_file, "Can't combine i0 into i3\n");
3083 undo_all ();
3084 return 0;
3087 /* Record whether i2 and i3 are trivial moves. */
3088 i2_was_move = is_just_move (i2);
3089 i3_was_move = is_just_move (i3);
3091 /* Record whether I2DEST is used in I2SRC and similarly for the other
3092 cases. Knowing this will help in register status updating below. */
3093 i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
3094 i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
3095 i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
3096 i0dest_in_i0src = i0 && reg_overlap_mentioned_p (i0dest, i0src);
3097 i1dest_in_i0src = i0 && reg_overlap_mentioned_p (i1dest, i0src);
3098 i2dest_in_i0src = i0 && reg_overlap_mentioned_p (i2dest, i0src);
3099 i2dest_killed = dead_or_set_p (i2, i2dest);
3100 i1dest_killed = i1 && dead_or_set_p (i1, i1dest);
3101 i0dest_killed = i0 && dead_or_set_p (i0, i0dest);
3103 /* For the earlier insns, determine which of the subsequent ones they
3104 feed. */
3105 i1_feeds_i2_n = i1 && insn_a_feeds_b (i1, i2);
3106 i0_feeds_i1_n = i0 && insn_a_feeds_b (i0, i1);
3107 i0_feeds_i2_n = (i0 && (!i0_feeds_i1_n ? insn_a_feeds_b (i0, i2)
3108 : (!reg_overlap_mentioned_p (i1dest, i0dest)
3109 && reg_overlap_mentioned_p (i0dest, i2src))));
3111 /* Ensure that I3's pattern can be the destination of combines. */
3112 if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest, i0dest,
3113 i1 && i2dest_in_i1src && !i1_feeds_i2_n,
3114 i0 && ((i2dest_in_i0src && !i0_feeds_i2_n)
3115 || (i1dest_in_i0src && !i0_feeds_i1_n)),
3116 &i3dest_killed))
3118 undo_all ();
3119 return 0;
3122 /* See if any of the insns is a MULT operation. Unless one is, we will
3123 reject a combination that is, since it must be slower. Be conservative
3124 here. */
3125 if (GET_CODE (i2src) == MULT
3126 || (i1 != 0 && GET_CODE (i1src) == MULT)
3127 || (i0 != 0 && GET_CODE (i0src) == MULT)
3128 || (GET_CODE (PATTERN (i3)) == SET
3129 && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
3130 have_mult = 1;
3132 /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
3133 We used to do this EXCEPT in one case: I3 has a post-inc in an
3134 output operand. However, that exception can give rise to insns like
3135 mov r3,(r3)+
3136 which is a famous insn on the PDP-11 where the value of r3 used as the
3137 source was model-dependent. Avoid this sort of thing. */
3139 #if 0
3140 if (!(GET_CODE (PATTERN (i3)) == SET
3141 && REG_P (SET_SRC (PATTERN (i3)))
3142 && MEM_P (SET_DEST (PATTERN (i3)))
3143 && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
3144 || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
3145 /* It's not the exception. */
3146 #endif
3147 if (AUTO_INC_DEC)
3149 rtx link;
3150 for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
3151 if (REG_NOTE_KIND (link) == REG_INC
3152 && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
3153 || (i1 != 0
3154 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
3156 undo_all ();
3157 return 0;
3161 /* See if the SETs in I1 or I2 need to be kept around in the merged
3162 instruction: whenever the value set there is still needed past I3.
3163 For the SET in I2, this is easy: we see if I2DEST dies or is set in I3.
3165 For the SET in I1, we have two cases: if I1 and I2 independently feed
3166 into I3, the set in I1 needs to be kept around unless I1DEST dies
3167 or is set in I3. Otherwise (if I1 feeds I2 which feeds I3), the set
3168 in I1 needs to be kept around unless I1DEST dies or is set in either
3169 I2 or I3. The same considerations apply to I0. */
3171 added_sets_2 = !dead_or_set_p (i3, i2dest);
3173 if (i1)
3174 added_sets_1 = !(dead_or_set_p (i3, i1dest)
3175 || (i1_feeds_i2_n && dead_or_set_p (i2, i1dest)));
3176 else
3177 added_sets_1 = 0;
3179 if (i0)
3180 added_sets_0 = !(dead_or_set_p (i3, i0dest)
3181 || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest))
3182 || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
3183 && dead_or_set_p (i2, i0dest)));
3184 else
3185 added_sets_0 = 0;
3187 /* We are about to copy insns for the case where they need to be kept
3188 around. Check that they can be copied in the merged instruction. */
3190 if (targetm.cannot_copy_insn_p
3191 && ((added_sets_2 && targetm.cannot_copy_insn_p (i2))
3192 || (i1 && added_sets_1 && targetm.cannot_copy_insn_p (i1))
3193 || (i0 && added_sets_0 && targetm.cannot_copy_insn_p (i0))))
3195 undo_all ();
3196 return 0;
3199 /* If the set in I2 needs to be kept around, we must make a copy of
3200 PATTERN (I2), so that when we substitute I1SRC for I1DEST in
3201 PATTERN (I2), we are only substituting for the original I1DEST, not into
3202 an already-substituted copy. This also prevents making self-referential
3203 rtx. If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
3204 I2DEST. */
3206 if (added_sets_2)
3208 if (GET_CODE (PATTERN (i2)) == PARALLEL)
3209 i2pat = gen_rtx_SET (i2dest, copy_rtx (i2src));
3210 else
3211 i2pat = copy_rtx (PATTERN (i2));
3214 if (added_sets_1)
3216 if (GET_CODE (PATTERN (i1)) == PARALLEL)
3217 i1pat = gen_rtx_SET (i1dest, copy_rtx (i1src));
3218 else
3219 i1pat = copy_rtx (PATTERN (i1));
3222 if (added_sets_0)
3224 if (GET_CODE (PATTERN (i0)) == PARALLEL)
3225 i0pat = gen_rtx_SET (i0dest, copy_rtx (i0src));
3226 else
3227 i0pat = copy_rtx (PATTERN (i0));
3230 combine_merges++;
3232 /* Substitute in the latest insn for the regs set by the earlier ones. */
3234 maxreg = max_reg_num ();
3236 subst_insn = i3;
3238 /* Many machines that don't use CC0 have insns that can both perform an
3239 arithmetic operation and set the condition code. These operations will
3240 be represented as a PARALLEL with the first element of the vector
3241 being a COMPARE of an arithmetic operation with the constant zero.
3242 The second element of the vector will set some pseudo to the result
3243 of the same arithmetic operation. If we simplify the COMPARE, we won't
3244 match such a pattern and so will generate an extra insn. Here we test
3245 for this case, where both the comparison and the operation result are
3246 needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
3247 I2SRC. Later we will make the PARALLEL that contains I2. */
3249 if (!HAVE_cc0 && i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
3250 && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
3251 && CONST_INT_P (XEXP (SET_SRC (PATTERN (i3)), 1))
3252 && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
3254 rtx newpat_dest;
3255 rtx *cc_use_loc = NULL;
3256 rtx_insn *cc_use_insn = NULL;
3257 rtx op0 = i2src, op1 = XEXP (SET_SRC (PATTERN (i3)), 1);
3258 machine_mode compare_mode, orig_compare_mode;
3259 enum rtx_code compare_code = UNKNOWN, orig_compare_code = UNKNOWN;
3260 scalar_int_mode mode;
3262 newpat = PATTERN (i3);
3263 newpat_dest = SET_DEST (newpat);
3264 compare_mode = orig_compare_mode = GET_MODE (newpat_dest);
3266 if (undobuf.other_insn == 0
3267 && (cc_use_loc = find_single_use (SET_DEST (newpat), i3,
3268 &cc_use_insn)))
3270 compare_code = orig_compare_code = GET_CODE (*cc_use_loc);
3271 if (is_a <scalar_int_mode> (GET_MODE (i2dest), &mode))
3272 compare_code = simplify_compare_const (compare_code, mode,
3273 op0, &op1);
3274 target_canonicalize_comparison (&compare_code, &op0, &op1, 1);
3277 /* Do the rest only if op1 is const0_rtx, which may be the
3278 result of simplification. */
3279 if (op1 == const0_rtx)
3281 /* If a single use of the CC is found, prepare to modify it
3282 when SELECT_CC_MODE returns a new CC-class mode, or when
3283 the above simplify_compare_const() returned a new comparison
3284 operator. undobuf.other_insn is assigned the CC use insn
3285 when modifying it. */
3286 if (cc_use_loc)
3288 #ifdef SELECT_CC_MODE
3289 machine_mode new_mode
3290 = SELECT_CC_MODE (compare_code, op0, op1);
3291 if (new_mode != orig_compare_mode
3292 && can_change_dest_mode (SET_DEST (newpat),
3293 added_sets_2, new_mode))
3295 unsigned int regno = REGNO (newpat_dest);
3296 compare_mode = new_mode;
3297 if (regno < FIRST_PSEUDO_REGISTER)
3298 newpat_dest = gen_rtx_REG (compare_mode, regno);
3299 else
3301 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
3302 newpat_dest = regno_reg_rtx[regno];
3305 #endif
3306 /* Cases for modifying the CC-using comparison. */
3307 if (compare_code != orig_compare_code
3308 /* ??? Do we need to verify the zero rtx? */
3309 && XEXP (*cc_use_loc, 1) == const0_rtx)
3311 /* Replace cc_use_loc with entire new RTX. */
3312 SUBST (*cc_use_loc,
3313 gen_rtx_fmt_ee (compare_code, compare_mode,
3314 newpat_dest, const0_rtx));
3315 undobuf.other_insn = cc_use_insn;
3317 else if (compare_mode != orig_compare_mode)
3319 /* Just replace the CC reg with a new mode. */
3320 SUBST (XEXP (*cc_use_loc, 0), newpat_dest);
3321 undobuf.other_insn = cc_use_insn;
3325 /* Now we modify the current newpat:
3326 First, SET_DEST(newpat) is updated if the CC mode has been
3327 altered. For targets without SELECT_CC_MODE, this should be
3328 optimized away. */
3329 if (compare_mode != orig_compare_mode)
3330 SUBST (SET_DEST (newpat), newpat_dest);
3331 /* This is always done to propagate i2src into newpat. */
3332 SUBST (SET_SRC (newpat),
3333 gen_rtx_COMPARE (compare_mode, op0, op1));
3334 /* Create new version of i2pat if needed; the below PARALLEL
3335 creation needs this to work correctly. */
3336 if (! rtx_equal_p (i2src, op0))
3337 i2pat = gen_rtx_SET (i2dest, op0);
3338 i2_is_used = 1;
3342 if (i2_is_used == 0)
3344 /* It is possible that the source of I2 or I1 may be performing
3345 an unneeded operation, such as a ZERO_EXTEND of something
3346 that is known to have the high part zero. Handle that case
3347 by letting subst look at the inner insns.
3349 Another way to do this would be to have a function that tries
3350 to simplify a single insn instead of merging two or more
3351 insns. We don't do this because of the potential of infinite
3352 loops and because of the potential extra memory required.
3353 However, doing it the way we are is a bit of a kludge and
3354 doesn't catch all cases.
3356 But only do this if -fexpensive-optimizations since it slows
3357 things down and doesn't usually win.
3359 This is not done in the COMPARE case above because the
3360 unmodified I2PAT is used in the PARALLEL and so a pattern
3361 with a modified I2SRC would not match. */
3363 if (flag_expensive_optimizations)
3365 /* Pass pc_rtx so no substitutions are done, just
3366 simplifications. */
3367 if (i1)
3369 subst_low_luid = DF_INSN_LUID (i1);
3370 i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0, 0);
3373 subst_low_luid = DF_INSN_LUID (i2);
3374 i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0, 0);
3377 n_occurrences = 0; /* `subst' counts here */
3378 subst_low_luid = DF_INSN_LUID (i2);
3380 /* If I1 feeds into I2 and I1DEST is in I1SRC, we need to make a unique
3381 copy of I2SRC each time we substitute it, in order to avoid creating
3382 self-referential RTL when we will be substituting I1SRC for I1DEST
3383 later. Likewise if I0 feeds into I2, either directly or indirectly
3384 through I1, and I0DEST is in I0SRC. */
3385 newpat = subst (PATTERN (i3), i2dest, i2src, 0, 0,
3386 (i1_feeds_i2_n && i1dest_in_i1src)
3387 || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
3388 && i0dest_in_i0src));
3389 substed_i2 = 1;
3391 /* Record whether I2's body now appears within I3's body. */
3392 i2_is_used = n_occurrences;
3395 /* If we already got a failure, don't try to do more. Otherwise, try to
3396 substitute I1 if we have it. */
3398 if (i1 && GET_CODE (newpat) != CLOBBER)
3400 /* Check that an autoincrement side-effect on I1 has not been lost.
3401 This happens if I1DEST is mentioned in I2 and dies there, and
3402 has disappeared from the new pattern. */
3403 if ((FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3404 && i1_feeds_i2_n
3405 && dead_or_set_p (i2, i1dest)
3406 && !reg_overlap_mentioned_p (i1dest, newpat))
3407 /* Before we can do this substitution, we must redo the test done
3408 above (see detailed comments there) that ensures I1DEST isn't
3409 mentioned in any SETs in NEWPAT that are field assignments. */
3410 || !combinable_i3pat (NULL, &newpat, i1dest, NULL_RTX, NULL_RTX,
3411 0, 0, 0))
3413 undo_all ();
3414 return 0;
3417 n_occurrences = 0;
3418 subst_low_luid = DF_INSN_LUID (i1);
3420 /* If the following substitution will modify I1SRC, make a copy of it
3421 for the case where it is substituted for I1DEST in I2PAT later. */
3422 if (added_sets_2 && i1_feeds_i2_n)
3423 i1src_copy = copy_rtx (i1src);
3425 /* If I0 feeds into I1 and I0DEST is in I0SRC, we need to make a unique
3426 copy of I1SRC each time we substitute it, in order to avoid creating
3427 self-referential RTL when we will be substituting I0SRC for I0DEST
3428 later. */
3429 newpat = subst (newpat, i1dest, i1src, 0, 0,
3430 i0_feeds_i1_n && i0dest_in_i0src);
3431 substed_i1 = 1;
3433 /* Record whether I1's body now appears within I3's body. */
3434 i1_is_used = n_occurrences;
3437 /* Likewise for I0 if we have it. */
3439 if (i0 && GET_CODE (newpat) != CLOBBER)
3441 if ((FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3442 && ((i0_feeds_i2_n && dead_or_set_p (i2, i0dest))
3443 || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest)))
3444 && !reg_overlap_mentioned_p (i0dest, newpat))
3445 || !combinable_i3pat (NULL, &newpat, i0dest, NULL_RTX, NULL_RTX,
3446 0, 0, 0))
3448 undo_all ();
3449 return 0;
3452 /* If the following substitution will modify I0SRC, make a copy of it
3453 for the case where it is substituted for I0DEST in I1PAT later. */
3454 if (added_sets_1 && i0_feeds_i1_n)
3455 i0src_copy = copy_rtx (i0src);
3456 /* And a copy for I0DEST in I2PAT substitution. */
3457 if (added_sets_2 && ((i0_feeds_i1_n && i1_feeds_i2_n)
3458 || (i0_feeds_i2_n)))
3459 i0src_copy2 = copy_rtx (i0src);
3461 n_occurrences = 0;
3462 subst_low_luid = DF_INSN_LUID (i0);
3463 newpat = subst (newpat, i0dest, i0src, 0, 0, 0);
3464 substed_i0 = 1;
3467 /* Fail if an autoincrement side-effect has been duplicated. Be careful
3468 to count all the ways that I2SRC and I1SRC can be used. */
3469 if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
3470 && i2_is_used + added_sets_2 > 1)
3471 || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3472 && (i1_is_used + added_sets_1 + (added_sets_2 && i1_feeds_i2_n)
3473 > 1))
3474 || (i0 != 0 && FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3475 && (n_occurrences + added_sets_0
3476 + (added_sets_1 && i0_feeds_i1_n)
3477 + (added_sets_2 && i0_feeds_i2_n)
3478 > 1))
3479 /* Fail if we tried to make a new register. */
3480 || max_reg_num () != maxreg
3481 /* Fail if we couldn't do something and have a CLOBBER. */
3482 || GET_CODE (newpat) == CLOBBER
3483 /* Fail if this new pattern is a MULT and we didn't have one before
3484 at the outer level. */
3485 || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
3486 && ! have_mult))
3488 undo_all ();
3489 return 0;
3492 /* If the actions of the earlier insns must be kept
3493 in addition to substituting them into the latest one,
3494 we must make a new PARALLEL for the latest insn
3495 to hold additional the SETs. */
3497 if (added_sets_0 || added_sets_1 || added_sets_2)
3499 int extra_sets = added_sets_0 + added_sets_1 + added_sets_2;
3500 combine_extras++;
3502 if (GET_CODE (newpat) == PARALLEL)
3504 rtvec old = XVEC (newpat, 0);
3505 total_sets = XVECLEN (newpat, 0) + extra_sets;
3506 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3507 memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
3508 sizeof (old->elem[0]) * old->num_elem);
3510 else
3512 rtx old = newpat;
3513 total_sets = 1 + extra_sets;
3514 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3515 XVECEXP (newpat, 0, 0) = old;
3518 if (added_sets_0)
3519 XVECEXP (newpat, 0, --total_sets) = i0pat;
3521 if (added_sets_1)
3523 rtx t = i1pat;
3524 if (i0_feeds_i1_n)
3525 t = subst (t, i0dest, i0src_copy ? i0src_copy : i0src, 0, 0, 0);
3527 XVECEXP (newpat, 0, --total_sets) = t;
3529 if (added_sets_2)
3531 rtx t = i2pat;
3532 if (i1_feeds_i2_n)
3533 t = subst (t, i1dest, i1src_copy ? i1src_copy : i1src, 0, 0,
3534 i0_feeds_i1_n && i0dest_in_i0src);
3535 if ((i0_feeds_i1_n && i1_feeds_i2_n) || i0_feeds_i2_n)
3536 t = subst (t, i0dest, i0src_copy2 ? i0src_copy2 : i0src, 0, 0, 0);
3538 XVECEXP (newpat, 0, --total_sets) = t;
3542 validate_replacement:
3544 /* Note which hard regs this insn has as inputs. */
3545 mark_used_regs_combine (newpat);
3547 /* If recog_for_combine fails, it strips existing clobbers. If we'll
3548 consider splitting this pattern, we might need these clobbers. */
3549 if (i1 && GET_CODE (newpat) == PARALLEL
3550 && GET_CODE (XVECEXP (newpat, 0, XVECLEN (newpat, 0) - 1)) == CLOBBER)
3552 int len = XVECLEN (newpat, 0);
3554 newpat_vec_with_clobbers = rtvec_alloc (len);
3555 for (i = 0; i < len; i++)
3556 RTVEC_ELT (newpat_vec_with_clobbers, i) = XVECEXP (newpat, 0, i);
3559 /* We have recognized nothing yet. */
3560 insn_code_number = -1;
3562 /* See if this is a PARALLEL of two SETs where one SET's destination is
3563 a register that is unused and this isn't marked as an instruction that
3564 might trap in an EH region. In that case, we just need the other SET.
3565 We prefer this over the PARALLEL.
3567 This can occur when simplifying a divmod insn. We *must* test for this
3568 case here because the code below that splits two independent SETs doesn't
3569 handle this case correctly when it updates the register status.
3571 It's pointless doing this if we originally had two sets, one from
3572 i3, and one from i2. Combining then splitting the parallel results
3573 in the original i2 again plus an invalid insn (which we delete).
3574 The net effect is only to move instructions around, which makes
3575 debug info less accurate.
3577 If the remaining SET came from I2 its destination should not be used
3578 between I2 and I3. See PR82024. */
3580 if (!(added_sets_2 && i1 == 0)
3581 && is_parallel_of_n_reg_sets (newpat, 2)
3582 && asm_noperands (newpat) < 0)
3584 rtx set0 = XVECEXP (newpat, 0, 0);
3585 rtx set1 = XVECEXP (newpat, 0, 1);
3586 rtx oldpat = newpat;
3588 if (((REG_P (SET_DEST (set1))
3589 && find_reg_note (i3, REG_UNUSED, SET_DEST (set1)))
3590 || (GET_CODE (SET_DEST (set1)) == SUBREG
3591 && find_reg_note (i3, REG_UNUSED, SUBREG_REG (SET_DEST (set1)))))
3592 && insn_nothrow_p (i3)
3593 && !side_effects_p (SET_SRC (set1)))
3595 newpat = set0;
3596 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3599 else if (((REG_P (SET_DEST (set0))
3600 && find_reg_note (i3, REG_UNUSED, SET_DEST (set0)))
3601 || (GET_CODE (SET_DEST (set0)) == SUBREG
3602 && find_reg_note (i3, REG_UNUSED,
3603 SUBREG_REG (SET_DEST (set0)))))
3604 && insn_nothrow_p (i3)
3605 && !side_effects_p (SET_SRC (set0)))
3607 rtx dest = SET_DEST (set1);
3608 if (GET_CODE (dest) == SUBREG)
3609 dest = SUBREG_REG (dest);
3610 if (!reg_used_between_p (dest, i2, i3))
3612 newpat = set1;
3613 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3615 if (insn_code_number >= 0)
3616 changed_i3_dest = 1;
3620 if (insn_code_number < 0)
3621 newpat = oldpat;
3624 /* Is the result of combination a valid instruction? */
3625 if (insn_code_number < 0)
3626 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3628 /* If we were combining three insns and the result is a simple SET
3629 with no ASM_OPERANDS that wasn't recognized, try to split it into two
3630 insns. There are two ways to do this. It can be split using a
3631 machine-specific method (like when you have an addition of a large
3632 constant) or by combine in the function find_split_point. */
3634 if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
3635 && asm_noperands (newpat) < 0)
3637 rtx parallel, *split;
3638 rtx_insn *m_split_insn;
3640 /* See if the MD file can split NEWPAT. If it can't, see if letting it
3641 use I2DEST as a scratch register will help. In the latter case,
3642 convert I2DEST to the mode of the source of NEWPAT if we can. */
3644 m_split_insn = combine_split_insns (newpat, i3);
3646 /* We can only use I2DEST as a scratch reg if it doesn't overlap any
3647 inputs of NEWPAT. */
3649 /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
3650 possible to try that as a scratch reg. This would require adding
3651 more code to make it work though. */
3653 if (m_split_insn == 0 && ! reg_overlap_mentioned_p (i2dest, newpat))
3655 machine_mode new_mode = GET_MODE (SET_DEST (newpat));
3657 /* ??? Reusing i2dest without resetting the reg_stat entry for it
3658 (temporarily, until we are committed to this instruction
3659 combination) does not work: for example, any call to nonzero_bits
3660 on the register (from a splitter in the MD file, for example)
3661 will get the old information, which is invalid.
3663 Since nowadays we can create registers during combine just fine,
3664 we should just create a new one here, not reuse i2dest. */
3666 /* First try to split using the original register as a
3667 scratch register. */
3668 parallel = gen_rtx_PARALLEL (VOIDmode,
3669 gen_rtvec (2, newpat,
3670 gen_rtx_CLOBBER (VOIDmode,
3671 i2dest)));
3672 m_split_insn = combine_split_insns (parallel, i3);
3674 /* If that didn't work, try changing the mode of I2DEST if
3675 we can. */
3676 if (m_split_insn == 0
3677 && new_mode != GET_MODE (i2dest)
3678 && new_mode != VOIDmode
3679 && can_change_dest_mode (i2dest, added_sets_2, new_mode))
3681 machine_mode old_mode = GET_MODE (i2dest);
3682 rtx ni2dest;
3684 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3685 ni2dest = gen_rtx_REG (new_mode, REGNO (i2dest));
3686 else
3688 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], new_mode);
3689 ni2dest = regno_reg_rtx[REGNO (i2dest)];
3692 parallel = (gen_rtx_PARALLEL
3693 (VOIDmode,
3694 gen_rtvec (2, newpat,
3695 gen_rtx_CLOBBER (VOIDmode,
3696 ni2dest))));
3697 m_split_insn = combine_split_insns (parallel, i3);
3699 if (m_split_insn == 0
3700 && REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
3702 struct undo *buf;
3704 adjust_reg_mode (regno_reg_rtx[REGNO (i2dest)], old_mode);
3705 buf = undobuf.undos;
3706 undobuf.undos = buf->next;
3707 buf->next = undobuf.frees;
3708 undobuf.frees = buf;
3712 i2scratch = m_split_insn != 0;
3715 /* If recog_for_combine has discarded clobbers, try to use them
3716 again for the split. */
3717 if (m_split_insn == 0 && newpat_vec_with_clobbers)
3719 parallel = gen_rtx_PARALLEL (VOIDmode, newpat_vec_with_clobbers);
3720 m_split_insn = combine_split_insns (parallel, i3);
3723 if (m_split_insn && NEXT_INSN (m_split_insn) == NULL_RTX)
3725 rtx m_split_pat = PATTERN (m_split_insn);
3726 insn_code_number = recog_for_combine (&m_split_pat, i3, &new_i3_notes);
3727 if (insn_code_number >= 0)
3728 newpat = m_split_pat;
3730 else if (m_split_insn && NEXT_INSN (NEXT_INSN (m_split_insn)) == NULL_RTX
3731 && (next_nonnote_nondebug_insn (i2) == i3
3732 || !modified_between_p (PATTERN (m_split_insn), i2, i3)))
3734 rtx i2set, i3set;
3735 rtx newi3pat = PATTERN (NEXT_INSN (m_split_insn));
3736 newi2pat = PATTERN (m_split_insn);
3738 i3set = single_set (NEXT_INSN (m_split_insn));
3739 i2set = single_set (m_split_insn);
3741 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3743 /* If I2 or I3 has multiple SETs, we won't know how to track
3744 register status, so don't use these insns. If I2's destination
3745 is used between I2 and I3, we also can't use these insns. */
3747 if (i2_code_number >= 0 && i2set && i3set
3748 && (next_nonnote_nondebug_insn (i2) == i3
3749 || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
3750 insn_code_number = recog_for_combine (&newi3pat, i3,
3751 &new_i3_notes);
3752 if (insn_code_number >= 0)
3753 newpat = newi3pat;
3755 /* It is possible that both insns now set the destination of I3.
3756 If so, we must show an extra use of it. */
3758 if (insn_code_number >= 0)
3760 rtx new_i3_dest = SET_DEST (i3set);
3761 rtx new_i2_dest = SET_DEST (i2set);
3763 while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
3764 || GET_CODE (new_i3_dest) == STRICT_LOW_PART
3765 || GET_CODE (new_i3_dest) == SUBREG)
3766 new_i3_dest = XEXP (new_i3_dest, 0);
3768 while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
3769 || GET_CODE (new_i2_dest) == STRICT_LOW_PART
3770 || GET_CODE (new_i2_dest) == SUBREG)
3771 new_i2_dest = XEXP (new_i2_dest, 0);
3773 if (REG_P (new_i3_dest)
3774 && REG_P (new_i2_dest)
3775 && REGNO (new_i3_dest) == REGNO (new_i2_dest)
3776 && REGNO (new_i2_dest) < reg_n_sets_max)
3777 INC_REG_N_SETS (REGNO (new_i2_dest), 1);
3781 /* If we can split it and use I2DEST, go ahead and see if that
3782 helps things be recognized. Verify that none of the registers
3783 are set between I2 and I3. */
3784 if (insn_code_number < 0
3785 && (split = find_split_point (&newpat, i3, false)) != 0
3786 && (!HAVE_cc0 || REG_P (i2dest))
3787 /* We need I2DEST in the proper mode. If it is a hard register
3788 or the only use of a pseudo, we can change its mode.
3789 Make sure we don't change a hard register to have a mode that
3790 isn't valid for it, or change the number of registers. */
3791 && (GET_MODE (*split) == GET_MODE (i2dest)
3792 || GET_MODE (*split) == VOIDmode
3793 || can_change_dest_mode (i2dest, added_sets_2,
3794 GET_MODE (*split)))
3795 && (next_nonnote_nondebug_insn (i2) == i3
3796 || !modified_between_p (*split, i2, i3))
3797 /* We can't overwrite I2DEST if its value is still used by
3798 NEWPAT. */
3799 && ! reg_referenced_p (i2dest, newpat))
3801 rtx newdest = i2dest;
3802 enum rtx_code split_code = GET_CODE (*split);
3803 machine_mode split_mode = GET_MODE (*split);
3804 bool subst_done = false;
3805 newi2pat = NULL_RTX;
3807 i2scratch = true;
3809 /* *SPLIT may be part of I2SRC, so make sure we have the
3810 original expression around for later debug processing.
3811 We should not need I2SRC any more in other cases. */
3812 if (MAY_HAVE_DEBUG_BIND_INSNS)
3813 i2src = copy_rtx (i2src);
3814 else
3815 i2src = NULL;
3817 /* Get NEWDEST as a register in the proper mode. We have already
3818 validated that we can do this. */
3819 if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
3821 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3822 newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
3823 else
3825 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], split_mode);
3826 newdest = regno_reg_rtx[REGNO (i2dest)];
3830 /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
3831 an ASHIFT. This can occur if it was inside a PLUS and hence
3832 appeared to be a memory address. This is a kludge. */
3833 if (split_code == MULT
3834 && CONST_INT_P (XEXP (*split, 1))
3835 && INTVAL (XEXP (*split, 1)) > 0
3836 && (i = exact_log2 (UINTVAL (XEXP (*split, 1)))) >= 0)
3838 rtx i_rtx = gen_int_shift_amount (split_mode, i);
3839 SUBST (*split, gen_rtx_ASHIFT (split_mode,
3840 XEXP (*split, 0), i_rtx));
3841 /* Update split_code because we may not have a multiply
3842 anymore. */
3843 split_code = GET_CODE (*split);
3846 /* Similarly for (plus (mult FOO (const_int pow2))). */
3847 if (split_code == PLUS
3848 && GET_CODE (XEXP (*split, 0)) == MULT
3849 && CONST_INT_P (XEXP (XEXP (*split, 0), 1))
3850 && INTVAL (XEXP (XEXP (*split, 0), 1)) > 0
3851 && (i = exact_log2 (UINTVAL (XEXP (XEXP (*split, 0), 1)))) >= 0)
3853 rtx nsplit = XEXP (*split, 0);
3854 rtx i_rtx = gen_int_shift_amount (GET_MODE (nsplit), i);
3855 SUBST (XEXP (*split, 0), gen_rtx_ASHIFT (GET_MODE (nsplit),
3856 XEXP (nsplit, 0),
3857 i_rtx));
3858 /* Update split_code because we may not have a multiply
3859 anymore. */
3860 split_code = GET_CODE (*split);
3863 #ifdef INSN_SCHEDULING
3864 /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
3865 be written as a ZERO_EXTEND. */
3866 if (split_code == SUBREG && MEM_P (SUBREG_REG (*split)))
3868 /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
3869 what it really is. */
3870 if (load_extend_op (GET_MODE (SUBREG_REG (*split)))
3871 == SIGN_EXTEND)
3872 SUBST (*split, gen_rtx_SIGN_EXTEND (split_mode,
3873 SUBREG_REG (*split)));
3874 else
3875 SUBST (*split, gen_rtx_ZERO_EXTEND (split_mode,
3876 SUBREG_REG (*split)));
3878 #endif
3880 /* Attempt to split binary operators using arithmetic identities. */
3881 if (BINARY_P (SET_SRC (newpat))
3882 && split_mode == GET_MODE (SET_SRC (newpat))
3883 && ! side_effects_p (SET_SRC (newpat)))
3885 rtx setsrc = SET_SRC (newpat);
3886 machine_mode mode = GET_MODE (setsrc);
3887 enum rtx_code code = GET_CODE (setsrc);
3888 rtx src_op0 = XEXP (setsrc, 0);
3889 rtx src_op1 = XEXP (setsrc, 1);
3891 /* Split "X = Y op Y" as "Z = Y; X = Z op Z". */
3892 if (rtx_equal_p (src_op0, src_op1))
3894 newi2pat = gen_rtx_SET (newdest, src_op0);
3895 SUBST (XEXP (setsrc, 0), newdest);
3896 SUBST (XEXP (setsrc, 1), newdest);
3897 subst_done = true;
3899 /* Split "((P op Q) op R) op S" where op is PLUS or MULT. */
3900 else if ((code == PLUS || code == MULT)
3901 && GET_CODE (src_op0) == code
3902 && GET_CODE (XEXP (src_op0, 0)) == code
3903 && (INTEGRAL_MODE_P (mode)
3904 || (FLOAT_MODE_P (mode)
3905 && flag_unsafe_math_optimizations)))
3907 rtx p = XEXP (XEXP (src_op0, 0), 0);
3908 rtx q = XEXP (XEXP (src_op0, 0), 1);
3909 rtx r = XEXP (src_op0, 1);
3910 rtx s = src_op1;
3912 /* Split both "((X op Y) op X) op Y" and
3913 "((X op Y) op Y) op X" as "T op T" where T is
3914 "X op Y". */
3915 if ((rtx_equal_p (p,r) && rtx_equal_p (q,s))
3916 || (rtx_equal_p (p,s) && rtx_equal_p (q,r)))
3918 newi2pat = gen_rtx_SET (newdest, XEXP (src_op0, 0));
3919 SUBST (XEXP (setsrc, 0), newdest);
3920 SUBST (XEXP (setsrc, 1), newdest);
3921 subst_done = true;
3923 /* Split "((X op X) op Y) op Y)" as "T op T" where
3924 T is "X op Y". */
3925 else if (rtx_equal_p (p,q) && rtx_equal_p (r,s))
3927 rtx tmp = simplify_gen_binary (code, mode, p, r);
3928 newi2pat = gen_rtx_SET (newdest, tmp);
3929 SUBST (XEXP (setsrc, 0), newdest);
3930 SUBST (XEXP (setsrc, 1), newdest);
3931 subst_done = true;
3936 if (!subst_done)
3938 newi2pat = gen_rtx_SET (newdest, *split);
3939 SUBST (*split, newdest);
3942 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3944 /* recog_for_combine might have added CLOBBERs to newi2pat.
3945 Make sure NEWPAT does not depend on the clobbered regs. */
3946 if (GET_CODE (newi2pat) == PARALLEL)
3947 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3948 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3950 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3951 if (reg_overlap_mentioned_p (reg, newpat))
3953 undo_all ();
3954 return 0;
3958 /* If the split point was a MULT and we didn't have one before,
3959 don't use one now. */
3960 if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
3961 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3965 /* Check for a case where we loaded from memory in a narrow mode and
3966 then sign extended it, but we need both registers. In that case,
3967 we have a PARALLEL with both loads from the same memory location.
3968 We can split this into a load from memory followed by a register-register
3969 copy. This saves at least one insn, more if register allocation can
3970 eliminate the copy.
3972 We cannot do this if the destination of the first assignment is a
3973 condition code register or cc0. We eliminate this case by making sure
3974 the SET_DEST and SET_SRC have the same mode.
3976 We cannot do this if the destination of the second assignment is
3977 a register that we have already assumed is zero-extended. Similarly
3978 for a SUBREG of such a register. */
3980 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3981 && GET_CODE (newpat) == PARALLEL
3982 && XVECLEN (newpat, 0) == 2
3983 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3984 && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
3985 && (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0)))
3986 == GET_MODE (SET_SRC (XVECEXP (newpat, 0, 0))))
3987 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3988 && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3989 XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
3990 && !modified_between_p (SET_SRC (XVECEXP (newpat, 0, 1)), i2, i3)
3991 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3992 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3993 && ! (temp_expr = SET_DEST (XVECEXP (newpat, 0, 1)),
3994 (REG_P (temp_expr)
3995 && reg_stat[REGNO (temp_expr)].nonzero_bits != 0
3996 && known_lt (GET_MODE_PRECISION (GET_MODE (temp_expr)),
3997 BITS_PER_WORD)
3998 && known_lt (GET_MODE_PRECISION (GET_MODE (temp_expr)),
3999 HOST_BITS_PER_INT)
4000 && (reg_stat[REGNO (temp_expr)].nonzero_bits
4001 != GET_MODE_MASK (word_mode))))
4002 && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
4003 && (temp_expr = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
4004 (REG_P (temp_expr)
4005 && reg_stat[REGNO (temp_expr)].nonzero_bits != 0
4006 && known_lt (GET_MODE_PRECISION (GET_MODE (temp_expr)),
4007 BITS_PER_WORD)
4008 && known_lt (GET_MODE_PRECISION (GET_MODE (temp_expr)),
4009 HOST_BITS_PER_INT)
4010 && (reg_stat[REGNO (temp_expr)].nonzero_bits
4011 != GET_MODE_MASK (word_mode)))))
4012 && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
4013 SET_SRC (XVECEXP (newpat, 0, 1)))
4014 && ! find_reg_note (i3, REG_UNUSED,
4015 SET_DEST (XVECEXP (newpat, 0, 0))))
4017 rtx ni2dest;
4019 newi2pat = XVECEXP (newpat, 0, 0);
4020 ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
4021 newpat = XVECEXP (newpat, 0, 1);
4022 SUBST (SET_SRC (newpat),
4023 gen_lowpart (GET_MODE (SET_SRC (newpat)), ni2dest));
4024 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
4026 if (i2_code_number >= 0)
4027 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
4029 if (insn_code_number >= 0)
4030 swap_i2i3 = 1;
4033 /* Similarly, check for a case where we have a PARALLEL of two independent
4034 SETs but we started with three insns. In this case, we can do the sets
4035 as two separate insns. This case occurs when some SET allows two
4036 other insns to combine, but the destination of that SET is still live.
4038 Also do this if we started with two insns and (at least) one of the
4039 resulting sets is a noop; this noop will be deleted later.
4041 Also do this if we started with two insns neither of which was a simple
4042 move. */
4044 else if (insn_code_number < 0 && asm_noperands (newpat) < 0
4045 && GET_CODE (newpat) == PARALLEL
4046 && XVECLEN (newpat, 0) == 2
4047 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
4048 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
4049 && (i1
4050 || set_noop_p (XVECEXP (newpat, 0, 0))
4051 || set_noop_p (XVECEXP (newpat, 0, 1))
4052 || (!i2_was_move && !i3_was_move))
4053 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
4054 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
4055 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
4056 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
4057 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
4058 XVECEXP (newpat, 0, 0))
4059 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
4060 XVECEXP (newpat, 0, 1))
4061 && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
4062 && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1)))))
4064 rtx set0 = XVECEXP (newpat, 0, 0);
4065 rtx set1 = XVECEXP (newpat, 0, 1);
4067 /* Normally, it doesn't matter which of the two is done first,
4068 but the one that references cc0 can't be the second, and
4069 one which uses any regs/memory set in between i2 and i3 can't
4070 be first. The PARALLEL might also have been pre-existing in i3,
4071 so we need to make sure that we won't wrongly hoist a SET to i2
4072 that would conflict with a death note present in there, or would
4073 have its dest modified between i2 and i3. */
4074 if (!modified_between_p (SET_SRC (set1), i2, i3)
4075 && !(REG_P (SET_DEST (set1))
4076 && find_reg_note (i2, REG_DEAD, SET_DEST (set1)))
4077 && !(GET_CODE (SET_DEST (set1)) == SUBREG
4078 && find_reg_note (i2, REG_DEAD,
4079 SUBREG_REG (SET_DEST (set1))))
4080 && !modified_between_p (SET_DEST (set1), i2, i3)
4081 && (!HAVE_cc0 || !reg_referenced_p (cc0_rtx, set0))
4082 /* If I3 is a jump, ensure that set0 is a jump so that
4083 we do not create invalid RTL. */
4084 && (!JUMP_P (i3) || SET_DEST (set0) == pc_rtx)
4087 newi2pat = set1;
4088 newpat = set0;
4090 else if (!modified_between_p (SET_SRC (set0), i2, i3)
4091 && !(REG_P (SET_DEST (set0))
4092 && find_reg_note (i2, REG_DEAD, SET_DEST (set0)))
4093 && !(GET_CODE (SET_DEST (set0)) == SUBREG
4094 && find_reg_note (i2, REG_DEAD,
4095 SUBREG_REG (SET_DEST (set0))))
4096 && !modified_between_p (SET_DEST (set0), i2, i3)
4097 && (!HAVE_cc0 || !reg_referenced_p (cc0_rtx, set1))
4098 /* If I3 is a jump, ensure that set1 is a jump so that
4099 we do not create invalid RTL. */
4100 && (!JUMP_P (i3) || SET_DEST (set1) == pc_rtx)
4103 newi2pat = set0;
4104 newpat = set1;
4106 else
4108 undo_all ();
4109 return 0;
4112 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
4114 if (i2_code_number >= 0)
4116 /* recog_for_combine might have added CLOBBERs to newi2pat.
4117 Make sure NEWPAT does not depend on the clobbered regs. */
4118 if (GET_CODE (newi2pat) == PARALLEL)
4120 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
4121 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
4123 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
4124 if (reg_overlap_mentioned_p (reg, newpat))
4126 undo_all ();
4127 return 0;
4132 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
4134 if (insn_code_number >= 0)
4135 split_i2i3 = 1;
4139 /* If it still isn't recognized, fail and change things back the way they
4140 were. */
4141 if ((insn_code_number < 0
4142 /* Is the result a reasonable ASM_OPERANDS? */
4143 && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
4145 undo_all ();
4146 return 0;
4149 /* If we had to change another insn, make sure it is valid also. */
4150 if (undobuf.other_insn)
4152 CLEAR_HARD_REG_SET (newpat_used_regs);
4154 other_pat = PATTERN (undobuf.other_insn);
4155 other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
4156 &new_other_notes);
4158 if (other_code_number < 0 && ! check_asm_operands (other_pat))
4160 undo_all ();
4161 return 0;
4165 /* If I2 is the CC0 setter and I3 is the CC0 user then check whether
4166 they are adjacent to each other or not. */
4167 if (HAVE_cc0)
4169 rtx_insn *p = prev_nonnote_insn (i3);
4170 if (p && p != i2 && NONJUMP_INSN_P (p) && newi2pat
4171 && sets_cc0_p (newi2pat))
4173 undo_all ();
4174 return 0;
4178 /* Only allow this combination if insn_cost reports that the
4179 replacement instructions are cheaper than the originals. */
4180 if (!combine_validate_cost (i0, i1, i2, i3, newpat, newi2pat, other_pat))
4182 undo_all ();
4183 return 0;
4186 if (MAY_HAVE_DEBUG_BIND_INSNS)
4188 struct undo *undo;
4190 for (undo = undobuf.undos; undo; undo = undo->next)
4191 if (undo->kind == UNDO_MODE)
4193 rtx reg = *undo->where.r;
4194 machine_mode new_mode = GET_MODE (reg);
4195 machine_mode old_mode = undo->old_contents.m;
4197 /* Temporarily revert mode back. */
4198 adjust_reg_mode (reg, old_mode);
4200 if (reg == i2dest && i2scratch)
4202 /* If we used i2dest as a scratch register with a
4203 different mode, substitute it for the original
4204 i2src while its original mode is temporarily
4205 restored, and then clear i2scratch so that we don't
4206 do it again later. */
4207 propagate_for_debug (i2, last_combined_insn, reg, i2src,
4208 this_basic_block);
4209 i2scratch = false;
4210 /* Put back the new mode. */
4211 adjust_reg_mode (reg, new_mode);
4213 else
4215 rtx tempreg = gen_raw_REG (old_mode, REGNO (reg));
4216 rtx_insn *first, *last;
4218 if (reg == i2dest)
4220 first = i2;
4221 last = last_combined_insn;
4223 else
4225 first = i3;
4226 last = undobuf.other_insn;
4227 gcc_assert (last);
4228 if (DF_INSN_LUID (last)
4229 < DF_INSN_LUID (last_combined_insn))
4230 last = last_combined_insn;
4233 /* We're dealing with a reg that changed mode but not
4234 meaning, so we want to turn it into a subreg for
4235 the new mode. However, because of REG sharing and
4236 because its mode had already changed, we have to do
4237 it in two steps. First, replace any debug uses of
4238 reg, with its original mode temporarily restored,
4239 with this copy we have created; then, replace the
4240 copy with the SUBREG of the original shared reg,
4241 once again changed to the new mode. */
4242 propagate_for_debug (first, last, reg, tempreg,
4243 this_basic_block);
4244 adjust_reg_mode (reg, new_mode);
4245 propagate_for_debug (first, last, tempreg,
4246 lowpart_subreg (old_mode, reg, new_mode),
4247 this_basic_block);
4252 /* If we will be able to accept this, we have made a
4253 change to the destination of I3. This requires us to
4254 do a few adjustments. */
4256 if (changed_i3_dest)
4258 PATTERN (i3) = newpat;
4259 adjust_for_new_dest (i3);
4262 /* We now know that we can do this combination. Merge the insns and
4263 update the status of registers and LOG_LINKS. */
4265 if (undobuf.other_insn)
4267 rtx note, next;
4269 PATTERN (undobuf.other_insn) = other_pat;
4271 /* If any of the notes in OTHER_INSN were REG_DEAD or REG_UNUSED,
4272 ensure that they are still valid. Then add any non-duplicate
4273 notes added by recog_for_combine. */
4274 for (note = REG_NOTES (undobuf.other_insn); note; note = next)
4276 next = XEXP (note, 1);
4278 if ((REG_NOTE_KIND (note) == REG_DEAD
4279 && !reg_referenced_p (XEXP (note, 0),
4280 PATTERN (undobuf.other_insn)))
4281 ||(REG_NOTE_KIND (note) == REG_UNUSED
4282 && !reg_set_p (XEXP (note, 0),
4283 PATTERN (undobuf.other_insn)))
4284 /* Simply drop equal note since it may be no longer valid
4285 for other_insn. It may be possible to record that CC
4286 register is changed and only discard those notes, but
4287 in practice it's unnecessary complication and doesn't
4288 give any meaningful improvement.
4290 See PR78559. */
4291 || REG_NOTE_KIND (note) == REG_EQUAL
4292 || REG_NOTE_KIND (note) == REG_EQUIV)
4293 remove_note (undobuf.other_insn, note);
4296 distribute_notes (new_other_notes, undobuf.other_insn,
4297 undobuf.other_insn, NULL, NULL_RTX, NULL_RTX,
4298 NULL_RTX);
4301 if (swap_i2i3)
4303 /* I3 now uses what used to be its destination and which is now
4304 I2's destination. This requires us to do a few adjustments. */
4305 PATTERN (i3) = newpat;
4306 adjust_for_new_dest (i3);
4309 if (swap_i2i3 || split_i2i3)
4311 /* We might need a LOG_LINK from I3 to I2. But then we used to
4312 have one, so we still will.
4314 However, some later insn might be using I2's dest and have
4315 a LOG_LINK pointing at I3. We should change it to point at
4316 I2 instead. */
4318 /* newi2pat is usually a SET here; however, recog_for_combine might
4319 have added some clobbers. */
4320 rtx x = newi2pat;
4321 if (GET_CODE (x) == PARALLEL)
4322 x = XVECEXP (newi2pat, 0, 0);
4324 /* It can only be a SET of a REG or of a SUBREG of a REG. */
4325 unsigned int regno = reg_or_subregno (SET_DEST (x));
4327 bool done = false;
4328 for (rtx_insn *insn = NEXT_INSN (i3);
4329 !done
4330 && insn
4331 && NONDEBUG_INSN_P (insn)
4332 && BLOCK_FOR_INSN (insn) == this_basic_block;
4333 insn = NEXT_INSN (insn))
4335 struct insn_link *link;
4336 FOR_EACH_LOG_LINK (link, insn)
4337 if (link->insn == i3 && link->regno == regno)
4339 link->insn = i2;
4340 done = true;
4341 break;
4347 rtx i3notes, i2notes, i1notes = 0, i0notes = 0;
4348 struct insn_link *i3links, *i2links, *i1links = 0, *i0links = 0;
4349 rtx midnotes = 0;
4350 int from_luid;
4351 /* Compute which registers we expect to eliminate. newi2pat may be setting
4352 either i3dest or i2dest, so we must check it. */
4353 rtx elim_i2 = ((newi2pat && reg_set_p (i2dest, newi2pat))
4354 || i2dest_in_i2src || i2dest_in_i1src || i2dest_in_i0src
4355 || !i2dest_killed
4356 ? 0 : i2dest);
4357 /* For i1, we need to compute both local elimination and global
4358 elimination information with respect to newi2pat because i1dest
4359 may be the same as i3dest, in which case newi2pat may be setting
4360 i1dest. Global information is used when distributing REG_DEAD
4361 note for i2 and i3, in which case it does matter if newi2pat sets
4362 i1dest or not.
4364 Local information is used when distributing REG_DEAD note for i1,
4365 in which case it doesn't matter if newi2pat sets i1dest or not.
4366 See PR62151, if we have four insns combination:
4367 i0: r0 <- i0src
4368 i1: r1 <- i1src (using r0)
4369 REG_DEAD (r0)
4370 i2: r0 <- i2src (using r1)
4371 i3: r3 <- i3src (using r0)
4372 ix: using r0
4373 From i1's point of view, r0 is eliminated, no matter if it is set
4374 by newi2pat or not. In other words, REG_DEAD info for r0 in i1
4375 should be discarded.
4377 Note local information only affects cases in forms like "I1->I2->I3",
4378 "I0->I1->I2->I3" or "I0&I1->I2, I2->I3". For other cases like
4379 "I0->I1, I1&I2->I3" or "I1&I2->I3", newi2pat won't set i1dest or
4380 i0dest anyway. */
4381 rtx local_elim_i1 = (i1 == 0 || i1dest_in_i1src || i1dest_in_i0src
4382 || !i1dest_killed
4383 ? 0 : i1dest);
4384 rtx elim_i1 = (local_elim_i1 == 0
4385 || (newi2pat && reg_set_p (i1dest, newi2pat))
4386 ? 0 : i1dest);
4387 /* Same case as i1. */
4388 rtx local_elim_i0 = (i0 == 0 || i0dest_in_i0src || !i0dest_killed
4389 ? 0 : i0dest);
4390 rtx elim_i0 = (local_elim_i0 == 0
4391 || (newi2pat && reg_set_p (i0dest, newi2pat))
4392 ? 0 : i0dest);
4394 /* Get the old REG_NOTES and LOG_LINKS from all our insns and
4395 clear them. */
4396 i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
4397 i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
4398 if (i1)
4399 i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
4400 if (i0)
4401 i0notes = REG_NOTES (i0), i0links = LOG_LINKS (i0);
4403 /* Ensure that we do not have something that should not be shared but
4404 occurs multiple times in the new insns. Check this by first
4405 resetting all the `used' flags and then copying anything is shared. */
4407 reset_used_flags (i3notes);
4408 reset_used_flags (i2notes);
4409 reset_used_flags (i1notes);
4410 reset_used_flags (i0notes);
4411 reset_used_flags (newpat);
4412 reset_used_flags (newi2pat);
4413 if (undobuf.other_insn)
4414 reset_used_flags (PATTERN (undobuf.other_insn));
4416 i3notes = copy_rtx_if_shared (i3notes);
4417 i2notes = copy_rtx_if_shared (i2notes);
4418 i1notes = copy_rtx_if_shared (i1notes);
4419 i0notes = copy_rtx_if_shared (i0notes);
4420 newpat = copy_rtx_if_shared (newpat);
4421 newi2pat = copy_rtx_if_shared (newi2pat);
4422 if (undobuf.other_insn)
4423 reset_used_flags (PATTERN (undobuf.other_insn));
4425 INSN_CODE (i3) = insn_code_number;
4426 PATTERN (i3) = newpat;
4428 if (CALL_P (i3) && CALL_INSN_FUNCTION_USAGE (i3))
4430 for (rtx link = CALL_INSN_FUNCTION_USAGE (i3); link;
4431 link = XEXP (link, 1))
4433 if (substed_i2)
4435 /* I2SRC must still be meaningful at this point. Some
4436 splitting operations can invalidate I2SRC, but those
4437 operations do not apply to calls. */
4438 gcc_assert (i2src);
4439 XEXP (link, 0) = simplify_replace_rtx (XEXP (link, 0),
4440 i2dest, i2src);
4442 if (substed_i1)
4443 XEXP (link, 0) = simplify_replace_rtx (XEXP (link, 0),
4444 i1dest, i1src);
4445 if (substed_i0)
4446 XEXP (link, 0) = simplify_replace_rtx (XEXP (link, 0),
4447 i0dest, i0src);
4451 if (undobuf.other_insn)
4452 INSN_CODE (undobuf.other_insn) = other_code_number;
4454 /* We had one special case above where I2 had more than one set and
4455 we replaced a destination of one of those sets with the destination
4456 of I3. In that case, we have to update LOG_LINKS of insns later
4457 in this basic block. Note that this (expensive) case is rare.
4459 Also, in this case, we must pretend that all REG_NOTEs for I2
4460 actually came from I3, so that REG_UNUSED notes from I2 will be
4461 properly handled. */
4463 if (i3_subst_into_i2)
4465 for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
4466 if ((GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == SET
4467 || GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == CLOBBER)
4468 && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, i)))
4469 && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
4470 && ! find_reg_note (i2, REG_UNUSED,
4471 SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
4472 for (temp_insn = NEXT_INSN (i2);
4473 temp_insn
4474 && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
4475 || BB_HEAD (this_basic_block) != temp_insn);
4476 temp_insn = NEXT_INSN (temp_insn))
4477 if (temp_insn != i3 && NONDEBUG_INSN_P (temp_insn))
4478 FOR_EACH_LOG_LINK (link, temp_insn)
4479 if (link->insn == i2)
4480 link->insn = i3;
4482 if (i3notes)
4484 rtx link = i3notes;
4485 while (XEXP (link, 1))
4486 link = XEXP (link, 1);
4487 XEXP (link, 1) = i2notes;
4489 else
4490 i3notes = i2notes;
4491 i2notes = 0;
4494 LOG_LINKS (i3) = NULL;
4495 REG_NOTES (i3) = 0;
4496 LOG_LINKS (i2) = NULL;
4497 REG_NOTES (i2) = 0;
4499 if (newi2pat)
4501 if (MAY_HAVE_DEBUG_BIND_INSNS && i2scratch)
4502 propagate_for_debug (i2, last_combined_insn, i2dest, i2src,
4503 this_basic_block);
4504 INSN_CODE (i2) = i2_code_number;
4505 PATTERN (i2) = newi2pat;
4507 else
4509 if (MAY_HAVE_DEBUG_BIND_INSNS && i2src)
4510 propagate_for_debug (i2, last_combined_insn, i2dest, i2src,
4511 this_basic_block);
4512 SET_INSN_DELETED (i2);
4515 if (i1)
4517 LOG_LINKS (i1) = NULL;
4518 REG_NOTES (i1) = 0;
4519 if (MAY_HAVE_DEBUG_BIND_INSNS)
4520 propagate_for_debug (i1, last_combined_insn, i1dest, i1src,
4521 this_basic_block);
4522 SET_INSN_DELETED (i1);
4525 if (i0)
4527 LOG_LINKS (i0) = NULL;
4528 REG_NOTES (i0) = 0;
4529 if (MAY_HAVE_DEBUG_BIND_INSNS)
4530 propagate_for_debug (i0, last_combined_insn, i0dest, i0src,
4531 this_basic_block);
4532 SET_INSN_DELETED (i0);
4535 /* Get death notes for everything that is now used in either I3 or
4536 I2 and used to die in a previous insn. If we built two new
4537 patterns, move from I1 to I2 then I2 to I3 so that we get the
4538 proper movement on registers that I2 modifies. */
4540 if (i0)
4541 from_luid = DF_INSN_LUID (i0);
4542 else if (i1)
4543 from_luid = DF_INSN_LUID (i1);
4544 else
4545 from_luid = DF_INSN_LUID (i2);
4546 if (newi2pat)
4547 move_deaths (newi2pat, NULL_RTX, from_luid, i2, &midnotes);
4548 move_deaths (newpat, newi2pat, from_luid, i3, &midnotes);
4550 /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3. */
4551 if (i3notes)
4552 distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL,
4553 elim_i2, elim_i1, elim_i0);
4554 if (i2notes)
4555 distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL,
4556 elim_i2, elim_i1, elim_i0);
4557 if (i1notes)
4558 distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL,
4559 elim_i2, local_elim_i1, local_elim_i0);
4560 if (i0notes)
4561 distribute_notes (i0notes, i0, i3, newi2pat ? i2 : NULL,
4562 elim_i2, elim_i1, local_elim_i0);
4563 if (midnotes)
4564 distribute_notes (midnotes, NULL, i3, newi2pat ? i2 : NULL,
4565 elim_i2, elim_i1, elim_i0);
4567 /* Distribute any notes added to I2 or I3 by recog_for_combine. We
4568 know these are REG_UNUSED and want them to go to the desired insn,
4569 so we always pass it as i3. */
4571 if (newi2pat && new_i2_notes)
4572 distribute_notes (new_i2_notes, i2, i2, NULL, NULL_RTX, NULL_RTX,
4573 NULL_RTX);
4575 if (new_i3_notes)
4576 distribute_notes (new_i3_notes, i3, i3, NULL, NULL_RTX, NULL_RTX,
4577 NULL_RTX);
4579 /* If I3DEST was used in I3SRC, it really died in I3. We may need to
4580 put a REG_DEAD note for it somewhere. If NEWI2PAT exists and sets
4581 I3DEST, the death must be somewhere before I2, not I3. If we passed I3
4582 in that case, it might delete I2. Similarly for I2 and I1.
4583 Show an additional death due to the REG_DEAD note we make here. If
4584 we discard it in distribute_notes, we will decrement it again. */
4586 if (i3dest_killed)
4588 rtx new_note = alloc_reg_note (REG_DEAD, i3dest_killed, NULL_RTX);
4589 if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
4590 distribute_notes (new_note, NULL, i2, NULL, elim_i2,
4591 elim_i1, elim_i0);
4592 else
4593 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4594 elim_i2, elim_i1, elim_i0);
4597 if (i2dest_in_i2src)
4599 rtx new_note = alloc_reg_note (REG_DEAD, i2dest, NULL_RTX);
4600 if (newi2pat && reg_set_p (i2dest, newi2pat))
4601 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4602 NULL_RTX, NULL_RTX);
4603 else
4604 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4605 NULL_RTX, NULL_RTX, NULL_RTX);
4608 if (i1dest_in_i1src)
4610 rtx new_note = alloc_reg_note (REG_DEAD, i1dest, NULL_RTX);
4611 if (newi2pat && reg_set_p (i1dest, newi2pat))
4612 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4613 NULL_RTX, NULL_RTX);
4614 else
4615 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4616 NULL_RTX, NULL_RTX, NULL_RTX);
4619 if (i0dest_in_i0src)
4621 rtx new_note = alloc_reg_note (REG_DEAD, i0dest, NULL_RTX);
4622 if (newi2pat && reg_set_p (i0dest, newi2pat))
4623 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4624 NULL_RTX, NULL_RTX);
4625 else
4626 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4627 NULL_RTX, NULL_RTX, NULL_RTX);
4630 distribute_links (i3links);
4631 distribute_links (i2links);
4632 distribute_links (i1links);
4633 distribute_links (i0links);
4635 if (REG_P (i2dest))
4637 struct insn_link *link;
4638 rtx_insn *i2_insn = 0;
4639 rtx i2_val = 0, set;
4641 /* The insn that used to set this register doesn't exist, and
4642 this life of the register may not exist either. See if one of
4643 I3's links points to an insn that sets I2DEST. If it does,
4644 that is now the last known value for I2DEST. If we don't update
4645 this and I2 set the register to a value that depended on its old
4646 contents, we will get confused. If this insn is used, thing
4647 will be set correctly in combine_instructions. */
4648 FOR_EACH_LOG_LINK (link, i3)
4649 if ((set = single_set (link->insn)) != 0
4650 && rtx_equal_p (i2dest, SET_DEST (set)))
4651 i2_insn = link->insn, i2_val = SET_SRC (set);
4653 record_value_for_reg (i2dest, i2_insn, i2_val);
4655 /* If the reg formerly set in I2 died only once and that was in I3,
4656 zero its use count so it won't make `reload' do any work. */
4657 if (! added_sets_2
4658 && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
4659 && ! i2dest_in_i2src
4660 && REGNO (i2dest) < reg_n_sets_max)
4661 INC_REG_N_SETS (REGNO (i2dest), -1);
4664 if (i1 && REG_P (i1dest))
4666 struct insn_link *link;
4667 rtx_insn *i1_insn = 0;
4668 rtx i1_val = 0, set;
4670 FOR_EACH_LOG_LINK (link, i3)
4671 if ((set = single_set (link->insn)) != 0
4672 && rtx_equal_p (i1dest, SET_DEST (set)))
4673 i1_insn = link->insn, i1_val = SET_SRC (set);
4675 record_value_for_reg (i1dest, i1_insn, i1_val);
4677 if (! added_sets_1
4678 && ! i1dest_in_i1src
4679 && REGNO (i1dest) < reg_n_sets_max)
4680 INC_REG_N_SETS (REGNO (i1dest), -1);
4683 if (i0 && REG_P (i0dest))
4685 struct insn_link *link;
4686 rtx_insn *i0_insn = 0;
4687 rtx i0_val = 0, set;
4689 FOR_EACH_LOG_LINK (link, i3)
4690 if ((set = single_set (link->insn)) != 0
4691 && rtx_equal_p (i0dest, SET_DEST (set)))
4692 i0_insn = link->insn, i0_val = SET_SRC (set);
4694 record_value_for_reg (i0dest, i0_insn, i0_val);
4696 if (! added_sets_0
4697 && ! i0dest_in_i0src
4698 && REGNO (i0dest) < reg_n_sets_max)
4699 INC_REG_N_SETS (REGNO (i0dest), -1);
4702 /* Update reg_stat[].nonzero_bits et al for any changes that may have
4703 been made to this insn. The order is important, because newi2pat
4704 can affect nonzero_bits of newpat. */
4705 if (newi2pat)
4706 note_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
4707 note_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
4710 if (undobuf.other_insn != NULL_RTX)
4712 if (dump_file)
4714 fprintf (dump_file, "modifying other_insn ");
4715 dump_insn_slim (dump_file, undobuf.other_insn);
4717 df_insn_rescan (undobuf.other_insn);
4720 if (i0 && !(NOTE_P (i0) && (NOTE_KIND (i0) == NOTE_INSN_DELETED)))
4722 if (dump_file)
4724 fprintf (dump_file, "modifying insn i0 ");
4725 dump_insn_slim (dump_file, i0);
4727 df_insn_rescan (i0);
4730 if (i1 && !(NOTE_P (i1) && (NOTE_KIND (i1) == NOTE_INSN_DELETED)))
4732 if (dump_file)
4734 fprintf (dump_file, "modifying insn i1 ");
4735 dump_insn_slim (dump_file, i1);
4737 df_insn_rescan (i1);
4740 if (i2 && !(NOTE_P (i2) && (NOTE_KIND (i2) == NOTE_INSN_DELETED)))
4742 if (dump_file)
4744 fprintf (dump_file, "modifying insn i2 ");
4745 dump_insn_slim (dump_file, i2);
4747 df_insn_rescan (i2);
4750 if (i3 && !(NOTE_P (i3) && (NOTE_KIND (i3) == NOTE_INSN_DELETED)))
4752 if (dump_file)
4754 fprintf (dump_file, "modifying insn i3 ");
4755 dump_insn_slim (dump_file, i3);
4757 df_insn_rescan (i3);
4760 /* Set new_direct_jump_p if a new return or simple jump instruction
4761 has been created. Adjust the CFG accordingly. */
4762 if (returnjump_p (i3) || any_uncondjump_p (i3))
4764 *new_direct_jump_p = 1;
4765 mark_jump_label (PATTERN (i3), i3, 0);
4766 update_cfg_for_uncondjump (i3);
4769 if (undobuf.other_insn != NULL_RTX
4770 && (returnjump_p (undobuf.other_insn)
4771 || any_uncondjump_p (undobuf.other_insn)))
4773 *new_direct_jump_p = 1;
4774 update_cfg_for_uncondjump (undobuf.other_insn);
4777 if (GET_CODE (PATTERN (i3)) == TRAP_IF
4778 && XEXP (PATTERN (i3), 0) == const1_rtx)
4780 basic_block bb = BLOCK_FOR_INSN (i3);
4781 gcc_assert (bb);
4782 remove_edge (split_block (bb, i3));
4783 emit_barrier_after_bb (bb);
4784 *new_direct_jump_p = 1;
4787 if (undobuf.other_insn
4788 && GET_CODE (PATTERN (undobuf.other_insn)) == TRAP_IF
4789 && XEXP (PATTERN (undobuf.other_insn), 0) == const1_rtx)
4791 basic_block bb = BLOCK_FOR_INSN (undobuf.other_insn);
4792 gcc_assert (bb);
4793 remove_edge (split_block (bb, undobuf.other_insn));
4794 emit_barrier_after_bb (bb);
4795 *new_direct_jump_p = 1;
4798 /* A noop might also need cleaning up of CFG, if it comes from the
4799 simplification of a jump. */
4800 if (JUMP_P (i3)
4801 && GET_CODE (newpat) == SET
4802 && SET_SRC (newpat) == pc_rtx
4803 && SET_DEST (newpat) == pc_rtx)
4805 *new_direct_jump_p = 1;
4806 update_cfg_for_uncondjump (i3);
4809 if (undobuf.other_insn != NULL_RTX
4810 && JUMP_P (undobuf.other_insn)
4811 && GET_CODE (PATTERN (undobuf.other_insn)) == SET
4812 && SET_SRC (PATTERN (undobuf.other_insn)) == pc_rtx
4813 && SET_DEST (PATTERN (undobuf.other_insn)) == pc_rtx)
4815 *new_direct_jump_p = 1;
4816 update_cfg_for_uncondjump (undobuf.other_insn);
4819 combine_successes++;
4820 undo_commit ();
4822 rtx_insn *ret = newi2pat ? i2 : i3;
4823 if (added_links_insn && DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (ret))
4824 ret = added_links_insn;
4825 if (added_notes_insn && DF_INSN_LUID (added_notes_insn) < DF_INSN_LUID (ret))
4826 ret = added_notes_insn;
4828 return ret;
4831 /* Get a marker for undoing to the current state. */
4833 static void *
4834 get_undo_marker (void)
4836 return undobuf.undos;
4839 /* Undo the modifications up to the marker. */
4841 static void
4842 undo_to_marker (void *marker)
4844 struct undo *undo, *next;
4846 for (undo = undobuf.undos; undo != marker; undo = next)
4848 gcc_assert (undo);
4850 next = undo->next;
4851 switch (undo->kind)
4853 case UNDO_RTX:
4854 *undo->where.r = undo->old_contents.r;
4855 break;
4856 case UNDO_INT:
4857 *undo->where.i = undo->old_contents.i;
4858 break;
4859 case UNDO_MODE:
4860 adjust_reg_mode (*undo->where.r, undo->old_contents.m);
4861 break;
4862 case UNDO_LINKS:
4863 *undo->where.l = undo->old_contents.l;
4864 break;
4865 default:
4866 gcc_unreachable ();
4869 undo->next = undobuf.frees;
4870 undobuf.frees = undo;
4873 undobuf.undos = (struct undo *) marker;
4876 /* Undo all the modifications recorded in undobuf. */
4878 static void
4879 undo_all (void)
4881 undo_to_marker (0);
4884 /* We've committed to accepting the changes we made. Move all
4885 of the undos to the free list. */
4887 static void
4888 undo_commit (void)
4890 struct undo *undo, *next;
4892 for (undo = undobuf.undos; undo; undo = next)
4894 next = undo->next;
4895 undo->next = undobuf.frees;
4896 undobuf.frees = undo;
4898 undobuf.undos = 0;
4901 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
4902 where we have an arithmetic expression and return that point. LOC will
4903 be inside INSN.
4905 try_combine will call this function to see if an insn can be split into
4906 two insns. */
4908 static rtx *
4909 find_split_point (rtx *loc, rtx_insn *insn, bool set_src)
4911 rtx x = *loc;
4912 enum rtx_code code = GET_CODE (x);
4913 rtx *split;
4914 unsigned HOST_WIDE_INT len = 0;
4915 HOST_WIDE_INT pos = 0;
4916 int unsignedp = 0;
4917 rtx inner = NULL_RTX;
4918 scalar_int_mode mode, inner_mode;
4920 /* First special-case some codes. */
4921 switch (code)
4923 case SUBREG:
4924 #ifdef INSN_SCHEDULING
4925 /* If we are making a paradoxical SUBREG invalid, it becomes a split
4926 point. */
4927 if (MEM_P (SUBREG_REG (x)))
4928 return loc;
4929 #endif
4930 return find_split_point (&SUBREG_REG (x), insn, false);
4932 case MEM:
4933 /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
4934 using LO_SUM and HIGH. */
4935 if (HAVE_lo_sum && (GET_CODE (XEXP (x, 0)) == CONST
4936 || GET_CODE (XEXP (x, 0)) == SYMBOL_REF))
4938 machine_mode address_mode = get_address_mode (x);
4940 SUBST (XEXP (x, 0),
4941 gen_rtx_LO_SUM (address_mode,
4942 gen_rtx_HIGH (address_mode, XEXP (x, 0)),
4943 XEXP (x, 0)));
4944 return &XEXP (XEXP (x, 0), 0);
4947 /* If we have a PLUS whose second operand is a constant and the
4948 address is not valid, perhaps will can split it up using
4949 the machine-specific way to split large constants. We use
4950 the first pseudo-reg (one of the virtual regs) as a placeholder;
4951 it will not remain in the result. */
4952 if (GET_CODE (XEXP (x, 0)) == PLUS
4953 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
4954 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4955 MEM_ADDR_SPACE (x)))
4957 rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
4958 rtx_insn *seq = combine_split_insns (gen_rtx_SET (reg, XEXP (x, 0)),
4959 subst_insn);
4961 /* This should have produced two insns, each of which sets our
4962 placeholder. If the source of the second is a valid address,
4963 we can make put both sources together and make a split point
4964 in the middle. */
4966 if (seq
4967 && NEXT_INSN (seq) != NULL_RTX
4968 && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
4969 && NONJUMP_INSN_P (seq)
4970 && GET_CODE (PATTERN (seq)) == SET
4971 && SET_DEST (PATTERN (seq)) == reg
4972 && ! reg_mentioned_p (reg,
4973 SET_SRC (PATTERN (seq)))
4974 && NONJUMP_INSN_P (NEXT_INSN (seq))
4975 && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
4976 && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
4977 && memory_address_addr_space_p
4978 (GET_MODE (x), SET_SRC (PATTERN (NEXT_INSN (seq))),
4979 MEM_ADDR_SPACE (x)))
4981 rtx src1 = SET_SRC (PATTERN (seq));
4982 rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
4984 /* Replace the placeholder in SRC2 with SRC1. If we can
4985 find where in SRC2 it was placed, that can become our
4986 split point and we can replace this address with SRC2.
4987 Just try two obvious places. */
4989 src2 = replace_rtx (src2, reg, src1);
4990 split = 0;
4991 if (XEXP (src2, 0) == src1)
4992 split = &XEXP (src2, 0);
4993 else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
4994 && XEXP (XEXP (src2, 0), 0) == src1)
4995 split = &XEXP (XEXP (src2, 0), 0);
4997 if (split)
4999 SUBST (XEXP (x, 0), src2);
5000 return split;
5004 /* If that didn't work, perhaps the first operand is complex and
5005 needs to be computed separately, so make a split point there.
5006 This will occur on machines that just support REG + CONST
5007 and have a constant moved through some previous computation. */
5009 else if (!OBJECT_P (XEXP (XEXP (x, 0), 0))
5010 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
5011 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
5012 return &XEXP (XEXP (x, 0), 0);
5015 /* If we have a PLUS whose first operand is complex, try computing it
5016 separately by making a split there. */
5017 if (GET_CODE (XEXP (x, 0)) == PLUS
5018 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
5019 MEM_ADDR_SPACE (x))
5020 && ! OBJECT_P (XEXP (XEXP (x, 0), 0))
5021 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
5022 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
5023 return &XEXP (XEXP (x, 0), 0);
5024 break;
5026 case SET:
5027 /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
5028 ZERO_EXTRACT, the most likely reason why this doesn't match is that
5029 we need to put the operand into a register. So split at that
5030 point. */
5032 if (SET_DEST (x) == cc0_rtx
5033 && GET_CODE (SET_SRC (x)) != COMPARE
5034 && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
5035 && !OBJECT_P (SET_SRC (x))
5036 && ! (GET_CODE (SET_SRC (x)) == SUBREG
5037 && OBJECT_P (SUBREG_REG (SET_SRC (x)))))
5038 return &SET_SRC (x);
5040 /* See if we can split SET_SRC as it stands. */
5041 split = find_split_point (&SET_SRC (x), insn, true);
5042 if (split && split != &SET_SRC (x))
5043 return split;
5045 /* See if we can split SET_DEST as it stands. */
5046 split = find_split_point (&SET_DEST (x), insn, false);
5047 if (split && split != &SET_DEST (x))
5048 return split;
5050 /* See if this is a bitfield assignment with everything constant. If
5051 so, this is an IOR of an AND, so split it into that. */
5052 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
5053 && is_a <scalar_int_mode> (GET_MODE (XEXP (SET_DEST (x), 0)),
5054 &inner_mode)
5055 && HWI_COMPUTABLE_MODE_P (inner_mode)
5056 && CONST_INT_P (XEXP (SET_DEST (x), 1))
5057 && CONST_INT_P (XEXP (SET_DEST (x), 2))
5058 && CONST_INT_P (SET_SRC (x))
5059 && ((INTVAL (XEXP (SET_DEST (x), 1))
5060 + INTVAL (XEXP (SET_DEST (x), 2)))
5061 <= GET_MODE_PRECISION (inner_mode))
5062 && ! side_effects_p (XEXP (SET_DEST (x), 0)))
5064 HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
5065 unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
5066 unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x));
5067 rtx dest = XEXP (SET_DEST (x), 0);
5068 unsigned HOST_WIDE_INT mask
5069 = (HOST_WIDE_INT_1U << len) - 1;
5070 rtx or_mask;
5072 if (BITS_BIG_ENDIAN)
5073 pos = GET_MODE_PRECISION (inner_mode) - len - pos;
5075 or_mask = gen_int_mode (src << pos, inner_mode);
5076 if (src == mask)
5077 SUBST (SET_SRC (x),
5078 simplify_gen_binary (IOR, inner_mode, dest, or_mask));
5079 else
5081 rtx negmask = gen_int_mode (~(mask << pos), inner_mode);
5082 SUBST (SET_SRC (x),
5083 simplify_gen_binary (IOR, inner_mode,
5084 simplify_gen_binary (AND, inner_mode,
5085 dest, negmask),
5086 or_mask));
5089 SUBST (SET_DEST (x), dest);
5091 split = find_split_point (&SET_SRC (x), insn, true);
5092 if (split && split != &SET_SRC (x))
5093 return split;
5096 /* Otherwise, see if this is an operation that we can split into two.
5097 If so, try to split that. */
5098 code = GET_CODE (SET_SRC (x));
5100 switch (code)
5102 case AND:
5103 /* If we are AND'ing with a large constant that is only a single
5104 bit and the result is only being used in a context where we
5105 need to know if it is zero or nonzero, replace it with a bit
5106 extraction. This will avoid the large constant, which might
5107 have taken more than one insn to make. If the constant were
5108 not a valid argument to the AND but took only one insn to make,
5109 this is no worse, but if it took more than one insn, it will
5110 be better. */
5112 if (CONST_INT_P (XEXP (SET_SRC (x), 1))
5113 && REG_P (XEXP (SET_SRC (x), 0))
5114 && (pos = exact_log2 (UINTVAL (XEXP (SET_SRC (x), 1)))) >= 7
5115 && REG_P (SET_DEST (x))
5116 && (split = find_single_use (SET_DEST (x), insn, NULL)) != 0
5117 && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
5118 && XEXP (*split, 0) == SET_DEST (x)
5119 && XEXP (*split, 1) == const0_rtx)
5121 rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
5122 XEXP (SET_SRC (x), 0),
5123 pos, NULL_RTX, 1, 1, 0, 0);
5124 if (extraction != 0)
5126 SUBST (SET_SRC (x), extraction);
5127 return find_split_point (loc, insn, false);
5130 break;
5132 case NE:
5133 /* If STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
5134 is known to be on, this can be converted into a NEG of a shift. */
5135 if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
5136 && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
5137 && ((pos = exact_log2 (nonzero_bits (XEXP (SET_SRC (x), 0),
5138 GET_MODE (XEXP (SET_SRC (x),
5139 0))))) >= 1))
5141 machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
5142 rtx pos_rtx = gen_int_shift_amount (mode, pos);
5143 SUBST (SET_SRC (x),
5144 gen_rtx_NEG (mode,
5145 gen_rtx_LSHIFTRT (mode,
5146 XEXP (SET_SRC (x), 0),
5147 pos_rtx)));
5149 split = find_split_point (&SET_SRC (x), insn, true);
5150 if (split && split != &SET_SRC (x))
5151 return split;
5153 break;
5155 case SIGN_EXTEND:
5156 inner = XEXP (SET_SRC (x), 0);
5158 /* We can't optimize if either mode is a partial integer
5159 mode as we don't know how many bits are significant
5160 in those modes. */
5161 if (!is_int_mode (GET_MODE (inner), &inner_mode)
5162 || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
5163 break;
5165 pos = 0;
5166 len = GET_MODE_PRECISION (inner_mode);
5167 unsignedp = 0;
5168 break;
5170 case SIGN_EXTRACT:
5171 case ZERO_EXTRACT:
5172 if (is_a <scalar_int_mode> (GET_MODE (XEXP (SET_SRC (x), 0)),
5173 &inner_mode)
5174 && CONST_INT_P (XEXP (SET_SRC (x), 1))
5175 && CONST_INT_P (XEXP (SET_SRC (x), 2)))
5177 inner = XEXP (SET_SRC (x), 0);
5178 len = INTVAL (XEXP (SET_SRC (x), 1));
5179 pos = INTVAL (XEXP (SET_SRC (x), 2));
5181 if (BITS_BIG_ENDIAN)
5182 pos = GET_MODE_PRECISION (inner_mode) - len - pos;
5183 unsignedp = (code == ZERO_EXTRACT);
5185 break;
5187 default:
5188 break;
5191 if (len
5192 && known_subrange_p (pos, len,
5193 0, GET_MODE_PRECISION (GET_MODE (inner)))
5194 && is_a <scalar_int_mode> (GET_MODE (SET_SRC (x)), &mode))
5196 /* For unsigned, we have a choice of a shift followed by an
5197 AND or two shifts. Use two shifts for field sizes where the
5198 constant might be too large. We assume here that we can
5199 always at least get 8-bit constants in an AND insn, which is
5200 true for every current RISC. */
5202 if (unsignedp && len <= 8)
5204 unsigned HOST_WIDE_INT mask
5205 = (HOST_WIDE_INT_1U << len) - 1;
5206 rtx pos_rtx = gen_int_shift_amount (mode, pos);
5207 SUBST (SET_SRC (x),
5208 gen_rtx_AND (mode,
5209 gen_rtx_LSHIFTRT
5210 (mode, gen_lowpart (mode, inner), pos_rtx),
5211 gen_int_mode (mask, mode)));
5213 split = find_split_point (&SET_SRC (x), insn, true);
5214 if (split && split != &SET_SRC (x))
5215 return split;
5217 else
5219 int left_bits = GET_MODE_PRECISION (mode) - len - pos;
5220 int right_bits = GET_MODE_PRECISION (mode) - len;
5221 SUBST (SET_SRC (x),
5222 gen_rtx_fmt_ee
5223 (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
5224 gen_rtx_ASHIFT (mode,
5225 gen_lowpart (mode, inner),
5226 gen_int_shift_amount (mode, left_bits)),
5227 gen_int_shift_amount (mode, right_bits)));
5229 split = find_split_point (&SET_SRC (x), insn, true);
5230 if (split && split != &SET_SRC (x))
5231 return split;
5235 /* See if this is a simple operation with a constant as the second
5236 operand. It might be that this constant is out of range and hence
5237 could be used as a split point. */
5238 if (BINARY_P (SET_SRC (x))
5239 && CONSTANT_P (XEXP (SET_SRC (x), 1))
5240 && (OBJECT_P (XEXP (SET_SRC (x), 0))
5241 || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
5242 && OBJECT_P (SUBREG_REG (XEXP (SET_SRC (x), 0))))))
5243 return &XEXP (SET_SRC (x), 1);
5245 /* Finally, see if this is a simple operation with its first operand
5246 not in a register. The operation might require this operand in a
5247 register, so return it as a split point. We can always do this
5248 because if the first operand were another operation, we would have
5249 already found it as a split point. */
5250 if ((BINARY_P (SET_SRC (x)) || UNARY_P (SET_SRC (x)))
5251 && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
5252 return &XEXP (SET_SRC (x), 0);
5254 return 0;
5256 case AND:
5257 case IOR:
5258 /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
5259 it is better to write this as (not (ior A B)) so we can split it.
5260 Similarly for IOR. */
5261 if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
5263 SUBST (*loc,
5264 gen_rtx_NOT (GET_MODE (x),
5265 gen_rtx_fmt_ee (code == IOR ? AND : IOR,
5266 GET_MODE (x),
5267 XEXP (XEXP (x, 0), 0),
5268 XEXP (XEXP (x, 1), 0))));
5269 return find_split_point (loc, insn, set_src);
5272 /* Many RISC machines have a large set of logical insns. If the
5273 second operand is a NOT, put it first so we will try to split the
5274 other operand first. */
5275 if (GET_CODE (XEXP (x, 1)) == NOT)
5277 rtx tem = XEXP (x, 0);
5278 SUBST (XEXP (x, 0), XEXP (x, 1));
5279 SUBST (XEXP (x, 1), tem);
5281 break;
5283 case PLUS:
5284 case MINUS:
5285 /* Canonicalization can produce (minus A (mult B C)), where C is a
5286 constant. It may be better to try splitting (plus (mult B -C) A)
5287 instead if this isn't a multiply by a power of two. */
5288 if (set_src && code == MINUS && GET_CODE (XEXP (x, 1)) == MULT
5289 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
5290 && !pow2p_hwi (INTVAL (XEXP (XEXP (x, 1), 1))))
5292 machine_mode mode = GET_MODE (x);
5293 unsigned HOST_WIDE_INT this_int = INTVAL (XEXP (XEXP (x, 1), 1));
5294 HOST_WIDE_INT other_int = trunc_int_for_mode (-this_int, mode);
5295 SUBST (*loc, gen_rtx_PLUS (mode,
5296 gen_rtx_MULT (mode,
5297 XEXP (XEXP (x, 1), 0),
5298 gen_int_mode (other_int,
5299 mode)),
5300 XEXP (x, 0)));
5301 return find_split_point (loc, insn, set_src);
5304 /* Split at a multiply-accumulate instruction. However if this is
5305 the SET_SRC, we likely do not have such an instruction and it's
5306 worthless to try this split. */
5307 if (!set_src
5308 && (GET_CODE (XEXP (x, 0)) == MULT
5309 || (GET_CODE (XEXP (x, 0)) == ASHIFT
5310 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
5311 return loc;
5313 default:
5314 break;
5317 /* Otherwise, select our actions depending on our rtx class. */
5318 switch (GET_RTX_CLASS (code))
5320 case RTX_BITFIELD_OPS: /* This is ZERO_EXTRACT and SIGN_EXTRACT. */
5321 case RTX_TERNARY:
5322 split = find_split_point (&XEXP (x, 2), insn, false);
5323 if (split)
5324 return split;
5325 /* fall through */
5326 case RTX_BIN_ARITH:
5327 case RTX_COMM_ARITH:
5328 case RTX_COMPARE:
5329 case RTX_COMM_COMPARE:
5330 split = find_split_point (&XEXP (x, 1), insn, false);
5331 if (split)
5332 return split;
5333 /* fall through */
5334 case RTX_UNARY:
5335 /* Some machines have (and (shift ...) ...) insns. If X is not
5336 an AND, but XEXP (X, 0) is, use it as our split point. */
5337 if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
5338 return &XEXP (x, 0);
5340 split = find_split_point (&XEXP (x, 0), insn, false);
5341 if (split)
5342 return split;
5343 return loc;
5345 default:
5346 /* Otherwise, we don't have a split point. */
5347 return 0;
5351 /* Throughout X, replace FROM with TO, and return the result.
5352 The result is TO if X is FROM;
5353 otherwise the result is X, but its contents may have been modified.
5354 If they were modified, a record was made in undobuf so that
5355 undo_all will (among other things) return X to its original state.
5357 If the number of changes necessary is too much to record to undo,
5358 the excess changes are not made, so the result is invalid.
5359 The changes already made can still be undone.
5360 undobuf.num_undo is incremented for such changes, so by testing that
5361 the caller can tell whether the result is valid.
5363 `n_occurrences' is incremented each time FROM is replaced.
5365 IN_DEST is nonzero if we are processing the SET_DEST of a SET.
5367 IN_COND is nonzero if we are at the top level of a condition.
5369 UNIQUE_COPY is nonzero if each substitution must be unique. We do this
5370 by copying if `n_occurrences' is nonzero. */
5372 static rtx
5373 subst (rtx x, rtx from, rtx to, int in_dest, int in_cond, int unique_copy)
5375 enum rtx_code code = GET_CODE (x);
5376 machine_mode op0_mode = VOIDmode;
5377 const char *fmt;
5378 int len, i;
5379 rtx new_rtx;
5381 /* Two expressions are equal if they are identical copies of a shared
5382 RTX or if they are both registers with the same register number
5383 and mode. */
5385 #define COMBINE_RTX_EQUAL_P(X,Y) \
5386 ((X) == (Y) \
5387 || (REG_P (X) && REG_P (Y) \
5388 && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
5390 /* Do not substitute into clobbers of regs -- this will never result in
5391 valid RTL. */
5392 if (GET_CODE (x) == CLOBBER && REG_P (XEXP (x, 0)))
5393 return x;
5395 if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
5397 n_occurrences++;
5398 return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
5401 /* If X and FROM are the same register but different modes, they
5402 will not have been seen as equal above. However, the log links code
5403 will make a LOG_LINKS entry for that case. If we do nothing, we
5404 will try to rerecognize our original insn and, when it succeeds,
5405 we will delete the feeding insn, which is incorrect.
5407 So force this insn not to match in this (rare) case. */
5408 if (! in_dest && code == REG && REG_P (from)
5409 && reg_overlap_mentioned_p (x, from))
5410 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
5412 /* If this is an object, we are done unless it is a MEM or LO_SUM, both
5413 of which may contain things that can be combined. */
5414 if (code != MEM && code != LO_SUM && OBJECT_P (x))
5415 return x;
5417 /* It is possible to have a subexpression appear twice in the insn.
5418 Suppose that FROM is a register that appears within TO.
5419 Then, after that subexpression has been scanned once by `subst',
5420 the second time it is scanned, TO may be found. If we were
5421 to scan TO here, we would find FROM within it and create a
5422 self-referent rtl structure which is completely wrong. */
5423 if (COMBINE_RTX_EQUAL_P (x, to))
5424 return to;
5426 /* Parallel asm_operands need special attention because all of the
5427 inputs are shared across the arms. Furthermore, unsharing the
5428 rtl results in recognition failures. Failure to handle this case
5429 specially can result in circular rtl.
5431 Solve this by doing a normal pass across the first entry of the
5432 parallel, and only processing the SET_DESTs of the subsequent
5433 entries. Ug. */
5435 if (code == PARALLEL
5436 && GET_CODE (XVECEXP (x, 0, 0)) == SET
5437 && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
5439 new_rtx = subst (XVECEXP (x, 0, 0), from, to, 0, 0, unique_copy);
5441 /* If this substitution failed, this whole thing fails. */
5442 if (GET_CODE (new_rtx) == CLOBBER
5443 && XEXP (new_rtx, 0) == const0_rtx)
5444 return new_rtx;
5446 SUBST (XVECEXP (x, 0, 0), new_rtx);
5448 for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
5450 rtx dest = SET_DEST (XVECEXP (x, 0, i));
5452 if (!REG_P (dest)
5453 && GET_CODE (dest) != CC0
5454 && GET_CODE (dest) != PC)
5456 new_rtx = subst (dest, from, to, 0, 0, unique_copy);
5458 /* If this substitution failed, this whole thing fails. */
5459 if (GET_CODE (new_rtx) == CLOBBER
5460 && XEXP (new_rtx, 0) == const0_rtx)
5461 return new_rtx;
5463 SUBST (SET_DEST (XVECEXP (x, 0, i)), new_rtx);
5467 else
5469 len = GET_RTX_LENGTH (code);
5470 fmt = GET_RTX_FORMAT (code);
5472 /* We don't need to process a SET_DEST that is a register, CC0,
5473 or PC, so set up to skip this common case. All other cases
5474 where we want to suppress replacing something inside a
5475 SET_SRC are handled via the IN_DEST operand. */
5476 if (code == SET
5477 && (REG_P (SET_DEST (x))
5478 || GET_CODE (SET_DEST (x)) == CC0
5479 || GET_CODE (SET_DEST (x)) == PC))
5480 fmt = "ie";
5482 /* Trying to simplify the operands of a widening MULT is not likely
5483 to create RTL matching a machine insn. */
5484 if (code == MULT
5485 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
5486 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND)
5487 && (GET_CODE (XEXP (x, 1)) == ZERO_EXTEND
5488 || GET_CODE (XEXP (x, 1)) == SIGN_EXTEND)
5489 && REG_P (XEXP (XEXP (x, 0), 0))
5490 && REG_P (XEXP (XEXP (x, 1), 0))
5491 && from == to)
5492 return x;
5495 /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
5496 constant. */
5497 if (fmt[0] == 'e')
5498 op0_mode = GET_MODE (XEXP (x, 0));
5500 for (i = 0; i < len; i++)
5502 if (fmt[i] == 'E')
5504 int j;
5505 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5507 if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
5509 new_rtx = (unique_copy && n_occurrences
5510 ? copy_rtx (to) : to);
5511 n_occurrences++;
5513 else
5515 new_rtx = subst (XVECEXP (x, i, j), from, to, 0, 0,
5516 unique_copy);
5518 /* If this substitution failed, this whole thing
5519 fails. */
5520 if (GET_CODE (new_rtx) == CLOBBER
5521 && XEXP (new_rtx, 0) == const0_rtx)
5522 return new_rtx;
5525 SUBST (XVECEXP (x, i, j), new_rtx);
5528 else if (fmt[i] == 'e')
5530 /* If this is a register being set, ignore it. */
5531 new_rtx = XEXP (x, i);
5532 if (in_dest
5533 && i == 0
5534 && (((code == SUBREG || code == ZERO_EXTRACT)
5535 && REG_P (new_rtx))
5536 || code == STRICT_LOW_PART))
5539 else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
5541 /* In general, don't install a subreg involving two
5542 modes not tieable. It can worsen register
5543 allocation, and can even make invalid reload
5544 insns, since the reg inside may need to be copied
5545 from in the outside mode, and that may be invalid
5546 if it is an fp reg copied in integer mode.
5548 We allow two exceptions to this: It is valid if
5549 it is inside another SUBREG and the mode of that
5550 SUBREG and the mode of the inside of TO is
5551 tieable and it is valid if X is a SET that copies
5552 FROM to CC0. */
5554 if (GET_CODE (to) == SUBREG
5555 && !targetm.modes_tieable_p (GET_MODE (to),
5556 GET_MODE (SUBREG_REG (to)))
5557 && ! (code == SUBREG
5558 && (targetm.modes_tieable_p
5559 (GET_MODE (x), GET_MODE (SUBREG_REG (to)))))
5560 && (!HAVE_cc0
5561 || (! (code == SET
5562 && i == 1
5563 && XEXP (x, 0) == cc0_rtx))))
5564 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5566 if (code == SUBREG
5567 && REG_P (to)
5568 && REGNO (to) < FIRST_PSEUDO_REGISTER
5569 && simplify_subreg_regno (REGNO (to), GET_MODE (to),
5570 SUBREG_BYTE (x),
5571 GET_MODE (x)) < 0)
5572 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5574 new_rtx = (unique_copy && n_occurrences ? copy_rtx (to) : to);
5575 n_occurrences++;
5577 else
5578 /* If we are in a SET_DEST, suppress most cases unless we
5579 have gone inside a MEM, in which case we want to
5580 simplify the address. We assume here that things that
5581 are actually part of the destination have their inner
5582 parts in the first expression. This is true for SUBREG,
5583 STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
5584 things aside from REG and MEM that should appear in a
5585 SET_DEST. */
5586 new_rtx = subst (XEXP (x, i), from, to,
5587 (((in_dest
5588 && (code == SUBREG || code == STRICT_LOW_PART
5589 || code == ZERO_EXTRACT))
5590 || code == SET)
5591 && i == 0),
5592 code == IF_THEN_ELSE && i == 0,
5593 unique_copy);
5595 /* If we found that we will have to reject this combination,
5596 indicate that by returning the CLOBBER ourselves, rather than
5597 an expression containing it. This will speed things up as
5598 well as prevent accidents where two CLOBBERs are considered
5599 to be equal, thus producing an incorrect simplification. */
5601 if (GET_CODE (new_rtx) == CLOBBER && XEXP (new_rtx, 0) == const0_rtx)
5602 return new_rtx;
5604 if (GET_CODE (x) == SUBREG && CONST_SCALAR_INT_P (new_rtx))
5606 machine_mode mode = GET_MODE (x);
5608 x = simplify_subreg (GET_MODE (x), new_rtx,
5609 GET_MODE (SUBREG_REG (x)),
5610 SUBREG_BYTE (x));
5611 if (! x)
5612 x = gen_rtx_CLOBBER (mode, const0_rtx);
5614 else if (CONST_SCALAR_INT_P (new_rtx)
5615 && (GET_CODE (x) == ZERO_EXTEND
5616 || GET_CODE (x) == FLOAT
5617 || GET_CODE (x) == UNSIGNED_FLOAT))
5619 x = simplify_unary_operation (GET_CODE (x), GET_MODE (x),
5620 new_rtx,
5621 GET_MODE (XEXP (x, 0)));
5622 if (!x)
5623 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5625 else
5626 SUBST (XEXP (x, i), new_rtx);
5631 /* Check if we are loading something from the constant pool via float
5632 extension; in this case we would undo compress_float_constant
5633 optimization and degenerate constant load to an immediate value. */
5634 if (GET_CODE (x) == FLOAT_EXTEND
5635 && MEM_P (XEXP (x, 0))
5636 && MEM_READONLY_P (XEXP (x, 0)))
5638 rtx tmp = avoid_constant_pool_reference (x);
5639 if (x != tmp)
5640 return x;
5643 /* Try to simplify X. If the simplification changed the code, it is likely
5644 that further simplification will help, so loop, but limit the number
5645 of repetitions that will be performed. */
5647 for (i = 0; i < 4; i++)
5649 /* If X is sufficiently simple, don't bother trying to do anything
5650 with it. */
5651 if (code != CONST_INT && code != REG && code != CLOBBER)
5652 x = combine_simplify_rtx (x, op0_mode, in_dest, in_cond);
5654 if (GET_CODE (x) == code)
5655 break;
5657 code = GET_CODE (x);
5659 /* We no longer know the original mode of operand 0 since we
5660 have changed the form of X) */
5661 op0_mode = VOIDmode;
5664 return x;
5667 /* If X is a commutative operation whose operands are not in the canonical
5668 order, use substitutions to swap them. */
5670 static void
5671 maybe_swap_commutative_operands (rtx x)
5673 if (COMMUTATIVE_ARITH_P (x)
5674 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
5676 rtx temp = XEXP (x, 0);
5677 SUBST (XEXP (x, 0), XEXP (x, 1));
5678 SUBST (XEXP (x, 1), temp);
5682 /* Simplify X, a piece of RTL. We just operate on the expression at the
5683 outer level; call `subst' to simplify recursively. Return the new
5684 expression.
5686 OP0_MODE is the original mode of XEXP (x, 0). IN_DEST is nonzero
5687 if we are inside a SET_DEST. IN_COND is nonzero if we are at the top level
5688 of a condition. */
5690 static rtx
5691 combine_simplify_rtx (rtx x, machine_mode op0_mode, int in_dest,
5692 int in_cond)
5694 enum rtx_code code = GET_CODE (x);
5695 machine_mode mode = GET_MODE (x);
5696 scalar_int_mode int_mode;
5697 rtx temp;
5698 int i;
5700 /* If this is a commutative operation, put a constant last and a complex
5701 expression first. We don't need to do this for comparisons here. */
5702 maybe_swap_commutative_operands (x);
5704 /* Try to fold this expression in case we have constants that weren't
5705 present before. */
5706 temp = 0;
5707 switch (GET_RTX_CLASS (code))
5709 case RTX_UNARY:
5710 if (op0_mode == VOIDmode)
5711 op0_mode = GET_MODE (XEXP (x, 0));
5712 temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
5713 break;
5714 case RTX_COMPARE:
5715 case RTX_COMM_COMPARE:
5717 machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
5718 if (cmp_mode == VOIDmode)
5720 cmp_mode = GET_MODE (XEXP (x, 1));
5721 if (cmp_mode == VOIDmode)
5722 cmp_mode = op0_mode;
5724 temp = simplify_relational_operation (code, mode, cmp_mode,
5725 XEXP (x, 0), XEXP (x, 1));
5727 break;
5728 case RTX_COMM_ARITH:
5729 case RTX_BIN_ARITH:
5730 temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
5731 break;
5732 case RTX_BITFIELD_OPS:
5733 case RTX_TERNARY:
5734 temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
5735 XEXP (x, 1), XEXP (x, 2));
5736 break;
5737 default:
5738 break;
5741 if (temp)
5743 x = temp;
5744 code = GET_CODE (temp);
5745 op0_mode = VOIDmode;
5746 mode = GET_MODE (temp);
5749 /* If this is a simple operation applied to an IF_THEN_ELSE, try
5750 applying it to the arms of the IF_THEN_ELSE. This often simplifies
5751 things. Check for cases where both arms are testing the same
5752 condition.
5754 Don't do anything if all operands are very simple. */
5756 if ((BINARY_P (x)
5757 && ((!OBJECT_P (XEXP (x, 0))
5758 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5759 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))
5760 || (!OBJECT_P (XEXP (x, 1))
5761 && ! (GET_CODE (XEXP (x, 1)) == SUBREG
5762 && OBJECT_P (SUBREG_REG (XEXP (x, 1)))))))
5763 || (UNARY_P (x)
5764 && (!OBJECT_P (XEXP (x, 0))
5765 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5766 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))))
5768 rtx cond, true_rtx, false_rtx;
5770 cond = if_then_else_cond (x, &true_rtx, &false_rtx);
5771 if (cond != 0
5772 /* If everything is a comparison, what we have is highly unlikely
5773 to be simpler, so don't use it. */
5774 && ! (COMPARISON_P (x)
5775 && (COMPARISON_P (true_rtx) || COMPARISON_P (false_rtx)))
5776 /* Similarly, if we end up with one of the expressions the same
5777 as the original, it is certainly not simpler. */
5778 && ! rtx_equal_p (x, true_rtx)
5779 && ! rtx_equal_p (x, false_rtx))
5781 rtx cop1 = const0_rtx;
5782 enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
5784 if (cond_code == NE && COMPARISON_P (cond))
5785 return x;
5787 /* Simplify the alternative arms; this may collapse the true and
5788 false arms to store-flag values. Be careful to use copy_rtx
5789 here since true_rtx or false_rtx might share RTL with x as a
5790 result of the if_then_else_cond call above. */
5791 true_rtx = subst (copy_rtx (true_rtx), pc_rtx, pc_rtx, 0, 0, 0);
5792 false_rtx = subst (copy_rtx (false_rtx), pc_rtx, pc_rtx, 0, 0, 0);
5794 /* If true_rtx and false_rtx are not general_operands, an if_then_else
5795 is unlikely to be simpler. */
5796 if (general_operand (true_rtx, VOIDmode)
5797 && general_operand (false_rtx, VOIDmode))
5799 enum rtx_code reversed;
5801 /* Restarting if we generate a store-flag expression will cause
5802 us to loop. Just drop through in this case. */
5804 /* If the result values are STORE_FLAG_VALUE and zero, we can
5805 just make the comparison operation. */
5806 if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
5807 x = simplify_gen_relational (cond_code, mode, VOIDmode,
5808 cond, cop1);
5809 else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
5810 && ((reversed = reversed_comparison_code_parts
5811 (cond_code, cond, cop1, NULL))
5812 != UNKNOWN))
5813 x = simplify_gen_relational (reversed, mode, VOIDmode,
5814 cond, cop1);
5816 /* Likewise, we can make the negate of a comparison operation
5817 if the result values are - STORE_FLAG_VALUE and zero. */
5818 else if (CONST_INT_P (true_rtx)
5819 && INTVAL (true_rtx) == - STORE_FLAG_VALUE
5820 && false_rtx == const0_rtx)
5821 x = simplify_gen_unary (NEG, mode,
5822 simplify_gen_relational (cond_code,
5823 mode, VOIDmode,
5824 cond, cop1),
5825 mode);
5826 else if (CONST_INT_P (false_rtx)
5827 && INTVAL (false_rtx) == - STORE_FLAG_VALUE
5828 && true_rtx == const0_rtx
5829 && ((reversed = reversed_comparison_code_parts
5830 (cond_code, cond, cop1, NULL))
5831 != UNKNOWN))
5832 x = simplify_gen_unary (NEG, mode,
5833 simplify_gen_relational (reversed,
5834 mode, VOIDmode,
5835 cond, cop1),
5836 mode);
5837 else
5838 return gen_rtx_IF_THEN_ELSE (mode,
5839 simplify_gen_relational (cond_code,
5840 mode,
5841 VOIDmode,
5842 cond,
5843 cop1),
5844 true_rtx, false_rtx);
5846 code = GET_CODE (x);
5847 op0_mode = VOIDmode;
5852 /* First see if we can apply the inverse distributive law. */
5853 if (code == PLUS || code == MINUS
5854 || code == AND || code == IOR || code == XOR)
5856 x = apply_distributive_law (x);
5857 code = GET_CODE (x);
5858 op0_mode = VOIDmode;
5861 /* If CODE is an associative operation not otherwise handled, see if we
5862 can associate some operands. This can win if they are constants or
5863 if they are logically related (i.e. (a & b) & a). */
5864 if ((code == PLUS || code == MINUS || code == MULT || code == DIV
5865 || code == AND || code == IOR || code == XOR
5866 || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
5867 && ((INTEGRAL_MODE_P (mode) && code != DIV)
5868 || (flag_associative_math && FLOAT_MODE_P (mode))))
5870 if (GET_CODE (XEXP (x, 0)) == code)
5872 rtx other = XEXP (XEXP (x, 0), 0);
5873 rtx inner_op0 = XEXP (XEXP (x, 0), 1);
5874 rtx inner_op1 = XEXP (x, 1);
5875 rtx inner;
5877 /* Make sure we pass the constant operand if any as the second
5878 one if this is a commutative operation. */
5879 if (CONSTANT_P (inner_op0) && COMMUTATIVE_ARITH_P (x))
5880 std::swap (inner_op0, inner_op1);
5881 inner = simplify_binary_operation (code == MINUS ? PLUS
5882 : code == DIV ? MULT
5883 : code,
5884 mode, inner_op0, inner_op1);
5886 /* For commutative operations, try the other pair if that one
5887 didn't simplify. */
5888 if (inner == 0 && COMMUTATIVE_ARITH_P (x))
5890 other = XEXP (XEXP (x, 0), 1);
5891 inner = simplify_binary_operation (code, mode,
5892 XEXP (XEXP (x, 0), 0),
5893 XEXP (x, 1));
5896 if (inner)
5897 return simplify_gen_binary (code, mode, other, inner);
5901 /* A little bit of algebraic simplification here. */
5902 switch (code)
5904 case MEM:
5905 /* Ensure that our address has any ASHIFTs converted to MULT in case
5906 address-recognizing predicates are called later. */
5907 temp = make_compound_operation (XEXP (x, 0), MEM);
5908 SUBST (XEXP (x, 0), temp);
5909 break;
5911 case SUBREG:
5912 if (op0_mode == VOIDmode)
5913 op0_mode = GET_MODE (SUBREG_REG (x));
5915 /* See if this can be moved to simplify_subreg. */
5916 if (CONSTANT_P (SUBREG_REG (x))
5917 && known_eq (subreg_lowpart_offset (mode, op0_mode), SUBREG_BYTE (x))
5918 /* Don't call gen_lowpart if the inner mode
5919 is VOIDmode and we cannot simplify it, as SUBREG without
5920 inner mode is invalid. */
5921 && (GET_MODE (SUBREG_REG (x)) != VOIDmode
5922 || gen_lowpart_common (mode, SUBREG_REG (x))))
5923 return gen_lowpart (mode, SUBREG_REG (x));
5925 if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
5926 break;
5928 rtx temp;
5929 temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
5930 SUBREG_BYTE (x));
5931 if (temp)
5932 return temp;
5934 /* If op is known to have all lower bits zero, the result is zero. */
5935 scalar_int_mode int_mode, int_op0_mode;
5936 if (!in_dest
5937 && is_a <scalar_int_mode> (mode, &int_mode)
5938 && is_a <scalar_int_mode> (op0_mode, &int_op0_mode)
5939 && (GET_MODE_PRECISION (int_mode)
5940 < GET_MODE_PRECISION (int_op0_mode))
5941 && known_eq (subreg_lowpart_offset (int_mode, int_op0_mode),
5942 SUBREG_BYTE (x))
5943 && HWI_COMPUTABLE_MODE_P (int_op0_mode)
5944 && (nonzero_bits (SUBREG_REG (x), int_op0_mode)
5945 & GET_MODE_MASK (int_mode)) == 0)
5946 return CONST0_RTX (int_mode);
5949 /* Don't change the mode of the MEM if that would change the meaning
5950 of the address. */
5951 if (MEM_P (SUBREG_REG (x))
5952 && (MEM_VOLATILE_P (SUBREG_REG (x))
5953 || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0),
5954 MEM_ADDR_SPACE (SUBREG_REG (x)))))
5955 return gen_rtx_CLOBBER (mode, const0_rtx);
5957 /* Note that we cannot do any narrowing for non-constants since
5958 we might have been counting on using the fact that some bits were
5959 zero. We now do this in the SET. */
5961 break;
5963 case NEG:
5964 temp = expand_compound_operation (XEXP (x, 0));
5966 /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
5967 replaced by (lshiftrt X C). This will convert
5968 (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y). */
5970 if (GET_CODE (temp) == ASHIFTRT
5971 && CONST_INT_P (XEXP (temp, 1))
5972 && INTVAL (XEXP (temp, 1)) == GET_MODE_UNIT_PRECISION (mode) - 1)
5973 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (temp, 0),
5974 INTVAL (XEXP (temp, 1)));
5976 /* If X has only a single bit that might be nonzero, say, bit I, convert
5977 (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
5978 MODE minus 1. This will convert (neg (zero_extract X 1 Y)) to
5979 (sign_extract X 1 Y). But only do this if TEMP isn't a register
5980 or a SUBREG of one since we'd be making the expression more
5981 complex if it was just a register. */
5983 if (!REG_P (temp)
5984 && ! (GET_CODE (temp) == SUBREG
5985 && REG_P (SUBREG_REG (temp)))
5986 && is_a <scalar_int_mode> (mode, &int_mode)
5987 && (i = exact_log2 (nonzero_bits (temp, int_mode))) >= 0)
5989 rtx temp1 = simplify_shift_const
5990 (NULL_RTX, ASHIFTRT, int_mode,
5991 simplify_shift_const (NULL_RTX, ASHIFT, int_mode, temp,
5992 GET_MODE_PRECISION (int_mode) - 1 - i),
5993 GET_MODE_PRECISION (int_mode) - 1 - i);
5995 /* If all we did was surround TEMP with the two shifts, we
5996 haven't improved anything, so don't use it. Otherwise,
5997 we are better off with TEMP1. */
5998 if (GET_CODE (temp1) != ASHIFTRT
5999 || GET_CODE (XEXP (temp1, 0)) != ASHIFT
6000 || XEXP (XEXP (temp1, 0), 0) != temp)
6001 return temp1;
6003 break;
6005 case TRUNCATE:
6006 /* We can't handle truncation to a partial integer mode here
6007 because we don't know the real bitsize of the partial
6008 integer mode. */
6009 if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
6010 break;
6012 if (HWI_COMPUTABLE_MODE_P (mode))
6013 SUBST (XEXP (x, 0),
6014 force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
6015 GET_MODE_MASK (mode), 0));
6017 /* We can truncate a constant value and return it. */
6019 poly_int64 c;
6020 if (poly_int_rtx_p (XEXP (x, 0), &c))
6021 return gen_int_mode (c, mode);
6024 /* Similarly to what we do in simplify-rtx.c, a truncate of a register
6025 whose value is a comparison can be replaced with a subreg if
6026 STORE_FLAG_VALUE permits. */
6027 if (HWI_COMPUTABLE_MODE_P (mode)
6028 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
6029 && (temp = get_last_value (XEXP (x, 0)))
6030 && COMPARISON_P (temp))
6031 return gen_lowpart (mode, XEXP (x, 0));
6032 break;
6034 case CONST:
6035 /* (const (const X)) can become (const X). Do it this way rather than
6036 returning the inner CONST since CONST can be shared with a
6037 REG_EQUAL note. */
6038 if (GET_CODE (XEXP (x, 0)) == CONST)
6039 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
6040 break;
6042 case LO_SUM:
6043 /* Convert (lo_sum (high FOO) FOO) to FOO. This is necessary so we
6044 can add in an offset. find_split_point will split this address up
6045 again if it doesn't match. */
6046 if (HAVE_lo_sum && GET_CODE (XEXP (x, 0)) == HIGH
6047 && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
6048 return XEXP (x, 1);
6049 break;
6051 case PLUS:
6052 /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
6053 when c is (const_int (pow2 + 1) / 2) is a sign extension of a
6054 bit-field and can be replaced by either a sign_extend or a
6055 sign_extract. The `and' may be a zero_extend and the two
6056 <c>, -<c> constants may be reversed. */
6057 if (GET_CODE (XEXP (x, 0)) == XOR
6058 && is_a <scalar_int_mode> (mode, &int_mode)
6059 && CONST_INT_P (XEXP (x, 1))
6060 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
6061 && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
6062 && ((i = exact_log2 (UINTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
6063 || (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
6064 && HWI_COMPUTABLE_MODE_P (int_mode)
6065 && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
6066 && CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
6067 && (UINTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
6068 == (HOST_WIDE_INT_1U << (i + 1)) - 1))
6069 || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
6070 && known_eq ((GET_MODE_PRECISION
6071 (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))),
6072 (unsigned int) i + 1))))
6073 return simplify_shift_const
6074 (NULL_RTX, ASHIFTRT, int_mode,
6075 simplify_shift_const (NULL_RTX, ASHIFT, int_mode,
6076 XEXP (XEXP (XEXP (x, 0), 0), 0),
6077 GET_MODE_PRECISION (int_mode) - (i + 1)),
6078 GET_MODE_PRECISION (int_mode) - (i + 1));
6080 /* If only the low-order bit of X is possibly nonzero, (plus x -1)
6081 can become (ashiftrt (ashift (xor x 1) C) C) where C is
6082 the bitsize of the mode - 1. This allows simplification of
6083 "a = (b & 8) == 0;" */
6084 if (XEXP (x, 1) == constm1_rtx
6085 && !REG_P (XEXP (x, 0))
6086 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
6087 && REG_P (SUBREG_REG (XEXP (x, 0))))
6088 && is_a <scalar_int_mode> (mode, &int_mode)
6089 && nonzero_bits (XEXP (x, 0), int_mode) == 1)
6090 return simplify_shift_const
6091 (NULL_RTX, ASHIFTRT, int_mode,
6092 simplify_shift_const (NULL_RTX, ASHIFT, int_mode,
6093 gen_rtx_XOR (int_mode, XEXP (x, 0),
6094 const1_rtx),
6095 GET_MODE_PRECISION (int_mode) - 1),
6096 GET_MODE_PRECISION (int_mode) - 1);
6098 /* If we are adding two things that have no bits in common, convert
6099 the addition into an IOR. This will often be further simplified,
6100 for example in cases like ((a & 1) + (a & 2)), which can
6101 become a & 3. */
6103 if (HWI_COMPUTABLE_MODE_P (mode)
6104 && (nonzero_bits (XEXP (x, 0), mode)
6105 & nonzero_bits (XEXP (x, 1), mode)) == 0)
6107 /* Try to simplify the expression further. */
6108 rtx tor = simplify_gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
6109 temp = combine_simplify_rtx (tor, VOIDmode, in_dest, 0);
6111 /* If we could, great. If not, do not go ahead with the IOR
6112 replacement, since PLUS appears in many special purpose
6113 address arithmetic instructions. */
6114 if (GET_CODE (temp) != CLOBBER
6115 && (GET_CODE (temp) != IOR
6116 || ((XEXP (temp, 0) != XEXP (x, 0)
6117 || XEXP (temp, 1) != XEXP (x, 1))
6118 && (XEXP (temp, 0) != XEXP (x, 1)
6119 || XEXP (temp, 1) != XEXP (x, 0)))))
6120 return temp;
6123 /* Canonicalize x + x into x << 1. */
6124 if (GET_MODE_CLASS (mode) == MODE_INT
6125 && rtx_equal_p (XEXP (x, 0), XEXP (x, 1))
6126 && !side_effects_p (XEXP (x, 0)))
6127 return simplify_gen_binary (ASHIFT, mode, XEXP (x, 0), const1_rtx);
6129 break;
6131 case MINUS:
6132 /* (minus <foo> (and <foo> (const_int -pow2))) becomes
6133 (and <foo> (const_int pow2-1)) */
6134 if (is_a <scalar_int_mode> (mode, &int_mode)
6135 && GET_CODE (XEXP (x, 1)) == AND
6136 && CONST_INT_P (XEXP (XEXP (x, 1), 1))
6137 && pow2p_hwi (-UINTVAL (XEXP (XEXP (x, 1), 1)))
6138 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
6139 return simplify_and_const_int (NULL_RTX, int_mode, XEXP (x, 0),
6140 -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
6141 break;
6143 case MULT:
6144 /* If we have (mult (plus A B) C), apply the distributive law and then
6145 the inverse distributive law to see if things simplify. This
6146 occurs mostly in addresses, often when unrolling loops. */
6148 if (GET_CODE (XEXP (x, 0)) == PLUS)
6150 rtx result = distribute_and_simplify_rtx (x, 0);
6151 if (result)
6152 return result;
6155 /* Try simplify a*(b/c) as (a*b)/c. */
6156 if (FLOAT_MODE_P (mode) && flag_associative_math
6157 && GET_CODE (XEXP (x, 0)) == DIV)
6159 rtx tem = simplify_binary_operation (MULT, mode,
6160 XEXP (XEXP (x, 0), 0),
6161 XEXP (x, 1));
6162 if (tem)
6163 return simplify_gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
6165 break;
6167 case UDIV:
6168 /* If this is a divide by a power of two, treat it as a shift if
6169 its first operand is a shift. */
6170 if (is_a <scalar_int_mode> (mode, &int_mode)
6171 && CONST_INT_P (XEXP (x, 1))
6172 && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0
6173 && (GET_CODE (XEXP (x, 0)) == ASHIFT
6174 || GET_CODE (XEXP (x, 0)) == LSHIFTRT
6175 || GET_CODE (XEXP (x, 0)) == ASHIFTRT
6176 || GET_CODE (XEXP (x, 0)) == ROTATE
6177 || GET_CODE (XEXP (x, 0)) == ROTATERT))
6178 return simplify_shift_const (NULL_RTX, LSHIFTRT, int_mode,
6179 XEXP (x, 0), i);
6180 break;
6182 case EQ: case NE:
6183 case GT: case GTU: case GE: case GEU:
6184 case LT: case LTU: case LE: case LEU:
6185 case UNEQ: case LTGT:
6186 case UNGT: case UNGE:
6187 case UNLT: case UNLE:
6188 case UNORDERED: case ORDERED:
6189 /* If the first operand is a condition code, we can't do anything
6190 with it. */
6191 if (GET_CODE (XEXP (x, 0)) == COMPARE
6192 || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
6193 && ! CC0_P (XEXP (x, 0))))
6195 rtx op0 = XEXP (x, 0);
6196 rtx op1 = XEXP (x, 1);
6197 enum rtx_code new_code;
6199 if (GET_CODE (op0) == COMPARE)
6200 op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
6202 /* Simplify our comparison, if possible. */
6203 new_code = simplify_comparison (code, &op0, &op1);
6205 /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
6206 if only the low-order bit is possibly nonzero in X (such as when
6207 X is a ZERO_EXTRACT of one bit). Similarly, we can convert EQ to
6208 (xor X 1) or (minus 1 X); we use the former. Finally, if X is
6209 known to be either 0 or -1, NE becomes a NEG and EQ becomes
6210 (plus X 1).
6212 Remove any ZERO_EXTRACT we made when thinking this was a
6213 comparison. It may now be simpler to use, e.g., an AND. If a
6214 ZERO_EXTRACT is indeed appropriate, it will be placed back by
6215 the call to make_compound_operation in the SET case.
6217 Don't apply these optimizations if the caller would
6218 prefer a comparison rather than a value.
6219 E.g., for the condition in an IF_THEN_ELSE most targets need
6220 an explicit comparison. */
6222 if (in_cond)
6225 else if (STORE_FLAG_VALUE == 1
6226 && new_code == NE
6227 && is_int_mode (mode, &int_mode)
6228 && op1 == const0_rtx
6229 && int_mode == GET_MODE (op0)
6230 && nonzero_bits (op0, int_mode) == 1)
6231 return gen_lowpart (int_mode,
6232 expand_compound_operation (op0));
6234 else if (STORE_FLAG_VALUE == 1
6235 && new_code == NE
6236 && is_int_mode (mode, &int_mode)
6237 && op1 == const0_rtx
6238 && int_mode == GET_MODE (op0)
6239 && (num_sign_bit_copies (op0, int_mode)
6240 == GET_MODE_PRECISION (int_mode)))
6242 op0 = expand_compound_operation (op0);
6243 return simplify_gen_unary (NEG, int_mode,
6244 gen_lowpart (int_mode, op0),
6245 int_mode);
6248 else if (STORE_FLAG_VALUE == 1
6249 && new_code == EQ
6250 && is_int_mode (mode, &int_mode)
6251 && op1 == const0_rtx
6252 && int_mode == GET_MODE (op0)
6253 && nonzero_bits (op0, int_mode) == 1)
6255 op0 = expand_compound_operation (op0);
6256 return simplify_gen_binary (XOR, int_mode,
6257 gen_lowpart (int_mode, op0),
6258 const1_rtx);
6261 else if (STORE_FLAG_VALUE == 1
6262 && new_code == EQ
6263 && is_int_mode (mode, &int_mode)
6264 && op1 == const0_rtx
6265 && int_mode == GET_MODE (op0)
6266 && (num_sign_bit_copies (op0, int_mode)
6267 == GET_MODE_PRECISION (int_mode)))
6269 op0 = expand_compound_operation (op0);
6270 return plus_constant (int_mode, gen_lowpart (int_mode, op0), 1);
6273 /* If STORE_FLAG_VALUE is -1, we have cases similar to
6274 those above. */
6275 if (in_cond)
6278 else if (STORE_FLAG_VALUE == -1
6279 && new_code == NE
6280 && is_int_mode (mode, &int_mode)
6281 && op1 == const0_rtx
6282 && int_mode == GET_MODE (op0)
6283 && (num_sign_bit_copies (op0, int_mode)
6284 == GET_MODE_PRECISION (int_mode)))
6285 return gen_lowpart (int_mode, expand_compound_operation (op0));
6287 else if (STORE_FLAG_VALUE == -1
6288 && new_code == NE
6289 && is_int_mode (mode, &int_mode)
6290 && op1 == const0_rtx
6291 && int_mode == GET_MODE (op0)
6292 && nonzero_bits (op0, int_mode) == 1)
6294 op0 = expand_compound_operation (op0);
6295 return simplify_gen_unary (NEG, int_mode,
6296 gen_lowpart (int_mode, op0),
6297 int_mode);
6300 else if (STORE_FLAG_VALUE == -1
6301 && new_code == EQ
6302 && is_int_mode (mode, &int_mode)
6303 && op1 == const0_rtx
6304 && int_mode == GET_MODE (op0)
6305 && (num_sign_bit_copies (op0, int_mode)
6306 == GET_MODE_PRECISION (int_mode)))
6308 op0 = expand_compound_operation (op0);
6309 return simplify_gen_unary (NOT, int_mode,
6310 gen_lowpart (int_mode, op0),
6311 int_mode);
6314 /* If X is 0/1, (eq X 0) is X-1. */
6315 else if (STORE_FLAG_VALUE == -1
6316 && new_code == EQ
6317 && is_int_mode (mode, &int_mode)
6318 && op1 == const0_rtx
6319 && int_mode == GET_MODE (op0)
6320 && nonzero_bits (op0, int_mode) == 1)
6322 op0 = expand_compound_operation (op0);
6323 return plus_constant (int_mode, gen_lowpart (int_mode, op0), -1);
6326 /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
6327 one bit that might be nonzero, we can convert (ne x 0) to
6328 (ashift x c) where C puts the bit in the sign bit. Remove any
6329 AND with STORE_FLAG_VALUE when we are done, since we are only
6330 going to test the sign bit. */
6331 if (new_code == NE
6332 && is_int_mode (mode, &int_mode)
6333 && HWI_COMPUTABLE_MODE_P (int_mode)
6334 && val_signbit_p (int_mode, STORE_FLAG_VALUE)
6335 && op1 == const0_rtx
6336 && int_mode == GET_MODE (op0)
6337 && (i = exact_log2 (nonzero_bits (op0, int_mode))) >= 0)
6339 x = simplify_shift_const (NULL_RTX, ASHIFT, int_mode,
6340 expand_compound_operation (op0),
6341 GET_MODE_PRECISION (int_mode) - 1 - i);
6342 if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
6343 return XEXP (x, 0);
6344 else
6345 return x;
6348 /* If the code changed, return a whole new comparison.
6349 We also need to avoid using SUBST in cases where
6350 simplify_comparison has widened a comparison with a CONST_INT,
6351 since in that case the wider CONST_INT may fail the sanity
6352 checks in do_SUBST. */
6353 if (new_code != code
6354 || (CONST_INT_P (op1)
6355 && GET_MODE (op0) != GET_MODE (XEXP (x, 0))
6356 && GET_MODE (op0) != GET_MODE (XEXP (x, 1))))
6357 return gen_rtx_fmt_ee (new_code, mode, op0, op1);
6359 /* Otherwise, keep this operation, but maybe change its operands.
6360 This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR). */
6361 SUBST (XEXP (x, 0), op0);
6362 SUBST (XEXP (x, 1), op1);
6364 break;
6366 case IF_THEN_ELSE:
6367 return simplify_if_then_else (x);
6369 case ZERO_EXTRACT:
6370 case SIGN_EXTRACT:
6371 case ZERO_EXTEND:
6372 case SIGN_EXTEND:
6373 /* If we are processing SET_DEST, we are done. */
6374 if (in_dest)
6375 return x;
6377 return expand_compound_operation (x);
6379 case SET:
6380 return simplify_set (x);
6382 case AND:
6383 case IOR:
6384 return simplify_logical (x);
6386 case ASHIFT:
6387 case LSHIFTRT:
6388 case ASHIFTRT:
6389 case ROTATE:
6390 case ROTATERT:
6391 /* If this is a shift by a constant amount, simplify it. */
6392 if (CONST_INT_P (XEXP (x, 1)))
6393 return simplify_shift_const (x, code, mode, XEXP (x, 0),
6394 INTVAL (XEXP (x, 1)));
6396 else if (SHIFT_COUNT_TRUNCATED && !REG_P (XEXP (x, 1)))
6397 SUBST (XEXP (x, 1),
6398 force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
6399 (HOST_WIDE_INT_1U
6400 << exact_log2 (GET_MODE_UNIT_BITSIZE
6401 (GET_MODE (x))))
6402 - 1,
6403 0));
6404 break;
6406 default:
6407 break;
6410 return x;
6413 /* Simplify X, an IF_THEN_ELSE expression. Return the new expression. */
6415 static rtx
6416 simplify_if_then_else (rtx x)
6418 machine_mode mode = GET_MODE (x);
6419 rtx cond = XEXP (x, 0);
6420 rtx true_rtx = XEXP (x, 1);
6421 rtx false_rtx = XEXP (x, 2);
6422 enum rtx_code true_code = GET_CODE (cond);
6423 int comparison_p = COMPARISON_P (cond);
6424 rtx temp;
6425 int i;
6426 enum rtx_code false_code;
6427 rtx reversed;
6428 scalar_int_mode int_mode, inner_mode;
6430 /* Simplify storing of the truth value. */
6431 if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
6432 return simplify_gen_relational (true_code, mode, VOIDmode,
6433 XEXP (cond, 0), XEXP (cond, 1));
6435 /* Also when the truth value has to be reversed. */
6436 if (comparison_p
6437 && true_rtx == const0_rtx && false_rtx == const_true_rtx
6438 && (reversed = reversed_comparison (cond, mode)))
6439 return reversed;
6441 /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
6442 in it is being compared against certain values. Get the true and false
6443 comparisons and see if that says anything about the value of each arm. */
6445 if (comparison_p
6446 && ((false_code = reversed_comparison_code (cond, NULL))
6447 != UNKNOWN)
6448 && REG_P (XEXP (cond, 0)))
6450 HOST_WIDE_INT nzb;
6451 rtx from = XEXP (cond, 0);
6452 rtx true_val = XEXP (cond, 1);
6453 rtx false_val = true_val;
6454 int swapped = 0;
6456 /* If FALSE_CODE is EQ, swap the codes and arms. */
6458 if (false_code == EQ)
6460 swapped = 1, true_code = EQ, false_code = NE;
6461 std::swap (true_rtx, false_rtx);
6464 scalar_int_mode from_mode;
6465 if (is_a <scalar_int_mode> (GET_MODE (from), &from_mode))
6467 /* If we are comparing against zero and the expression being
6468 tested has only a single bit that might be nonzero, that is
6469 its value when it is not equal to zero. Similarly if it is
6470 known to be -1 or 0. */
6471 if (true_code == EQ
6472 && true_val == const0_rtx
6473 && pow2p_hwi (nzb = nonzero_bits (from, from_mode)))
6475 false_code = EQ;
6476 false_val = gen_int_mode (nzb, from_mode);
6478 else if (true_code == EQ
6479 && true_val == const0_rtx
6480 && (num_sign_bit_copies (from, from_mode)
6481 == GET_MODE_PRECISION (from_mode)))
6483 false_code = EQ;
6484 false_val = constm1_rtx;
6488 /* Now simplify an arm if we know the value of the register in the
6489 branch and it is used in the arm. Be careful due to the potential
6490 of locally-shared RTL. */
6492 if (reg_mentioned_p (from, true_rtx))
6493 true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
6494 from, true_val),
6495 pc_rtx, pc_rtx, 0, 0, 0);
6496 if (reg_mentioned_p (from, false_rtx))
6497 false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
6498 from, false_val),
6499 pc_rtx, pc_rtx, 0, 0, 0);
6501 SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
6502 SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
6504 true_rtx = XEXP (x, 1);
6505 false_rtx = XEXP (x, 2);
6506 true_code = GET_CODE (cond);
6509 /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
6510 reversed, do so to avoid needing two sets of patterns for
6511 subtract-and-branch insns. Similarly if we have a constant in the true
6512 arm, the false arm is the same as the first operand of the comparison, or
6513 the false arm is more complicated than the true arm. */
6515 if (comparison_p
6516 && reversed_comparison_code (cond, NULL) != UNKNOWN
6517 && (true_rtx == pc_rtx
6518 || (CONSTANT_P (true_rtx)
6519 && !CONST_INT_P (false_rtx) && false_rtx != pc_rtx)
6520 || true_rtx == const0_rtx
6521 || (OBJECT_P (true_rtx) && !OBJECT_P (false_rtx))
6522 || (GET_CODE (true_rtx) == SUBREG && OBJECT_P (SUBREG_REG (true_rtx))
6523 && !OBJECT_P (false_rtx))
6524 || reg_mentioned_p (true_rtx, false_rtx)
6525 || rtx_equal_p (false_rtx, XEXP (cond, 0))))
6527 true_code = reversed_comparison_code (cond, NULL);
6528 SUBST (XEXP (x, 0), reversed_comparison (cond, GET_MODE (cond)));
6529 SUBST (XEXP (x, 1), false_rtx);
6530 SUBST (XEXP (x, 2), true_rtx);
6532 std::swap (true_rtx, false_rtx);
6533 cond = XEXP (x, 0);
6535 /* It is possible that the conditional has been simplified out. */
6536 true_code = GET_CODE (cond);
6537 comparison_p = COMPARISON_P (cond);
6540 /* If the two arms are identical, we don't need the comparison. */
6542 if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
6543 return true_rtx;
6545 /* Convert a == b ? b : a to "a". */
6546 if (true_code == EQ && ! side_effects_p (cond)
6547 && !HONOR_NANS (mode)
6548 && rtx_equal_p (XEXP (cond, 0), false_rtx)
6549 && rtx_equal_p (XEXP (cond, 1), true_rtx))
6550 return false_rtx;
6551 else if (true_code == NE && ! side_effects_p (cond)
6552 && !HONOR_NANS (mode)
6553 && rtx_equal_p (XEXP (cond, 0), true_rtx)
6554 && rtx_equal_p (XEXP (cond, 1), false_rtx))
6555 return true_rtx;
6557 /* Look for cases where we have (abs x) or (neg (abs X)). */
6559 if (GET_MODE_CLASS (mode) == MODE_INT
6560 && comparison_p
6561 && XEXP (cond, 1) == const0_rtx
6562 && GET_CODE (false_rtx) == NEG
6563 && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
6564 && rtx_equal_p (true_rtx, XEXP (cond, 0))
6565 && ! side_effects_p (true_rtx))
6566 switch (true_code)
6568 case GT:
6569 case GE:
6570 return simplify_gen_unary (ABS, mode, true_rtx, mode);
6571 case LT:
6572 case LE:
6573 return
6574 simplify_gen_unary (NEG, mode,
6575 simplify_gen_unary (ABS, mode, true_rtx, mode),
6576 mode);
6577 default:
6578 break;
6581 /* Look for MIN or MAX. */
6583 if ((! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
6584 && comparison_p
6585 && rtx_equal_p (XEXP (cond, 0), true_rtx)
6586 && rtx_equal_p (XEXP (cond, 1), false_rtx)
6587 && ! side_effects_p (cond))
6588 switch (true_code)
6590 case GE:
6591 case GT:
6592 return simplify_gen_binary (SMAX, mode, true_rtx, false_rtx);
6593 case LE:
6594 case LT:
6595 return simplify_gen_binary (SMIN, mode, true_rtx, false_rtx);
6596 case GEU:
6597 case GTU:
6598 return simplify_gen_binary (UMAX, mode, true_rtx, false_rtx);
6599 case LEU:
6600 case LTU:
6601 return simplify_gen_binary (UMIN, mode, true_rtx, false_rtx);
6602 default:
6603 break;
6606 /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
6607 second operand is zero, this can be done as (OP Z (mult COND C2)) where
6608 C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
6609 SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
6610 We can do this kind of thing in some cases when STORE_FLAG_VALUE is
6611 neither 1 or -1, but it isn't worth checking for. */
6613 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
6614 && comparison_p
6615 && is_int_mode (mode, &int_mode)
6616 && ! side_effects_p (x))
6618 rtx t = make_compound_operation (true_rtx, SET);
6619 rtx f = make_compound_operation (false_rtx, SET);
6620 rtx cond_op0 = XEXP (cond, 0);
6621 rtx cond_op1 = XEXP (cond, 1);
6622 enum rtx_code op = UNKNOWN, extend_op = UNKNOWN;
6623 scalar_int_mode m = int_mode;
6624 rtx z = 0, c1 = NULL_RTX;
6626 if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
6627 || GET_CODE (t) == IOR || GET_CODE (t) == XOR
6628 || GET_CODE (t) == ASHIFT
6629 || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
6630 && rtx_equal_p (XEXP (t, 0), f))
6631 c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
6633 /* If an identity-zero op is commutative, check whether there
6634 would be a match if we swapped the operands. */
6635 else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
6636 || GET_CODE (t) == XOR)
6637 && rtx_equal_p (XEXP (t, 1), f))
6638 c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
6639 else if (GET_CODE (t) == SIGN_EXTEND
6640 && is_a <scalar_int_mode> (GET_MODE (XEXP (t, 0)), &inner_mode)
6641 && (GET_CODE (XEXP (t, 0)) == PLUS
6642 || GET_CODE (XEXP (t, 0)) == MINUS
6643 || GET_CODE (XEXP (t, 0)) == IOR
6644 || GET_CODE (XEXP (t, 0)) == XOR
6645 || GET_CODE (XEXP (t, 0)) == ASHIFT
6646 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6647 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6648 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6649 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6650 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6651 && (num_sign_bit_copies (f, GET_MODE (f))
6652 > (unsigned int)
6653 (GET_MODE_PRECISION (int_mode)
6654 - GET_MODE_PRECISION (inner_mode))))
6656 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6657 extend_op = SIGN_EXTEND;
6658 m = inner_mode;
6660 else if (GET_CODE (t) == SIGN_EXTEND
6661 && is_a <scalar_int_mode> (GET_MODE (XEXP (t, 0)), &inner_mode)
6662 && (GET_CODE (XEXP (t, 0)) == PLUS
6663 || GET_CODE (XEXP (t, 0)) == IOR
6664 || GET_CODE (XEXP (t, 0)) == XOR)
6665 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6666 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6667 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6668 && (num_sign_bit_copies (f, GET_MODE (f))
6669 > (unsigned int)
6670 (GET_MODE_PRECISION (int_mode)
6671 - GET_MODE_PRECISION (inner_mode))))
6673 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6674 extend_op = SIGN_EXTEND;
6675 m = inner_mode;
6677 else if (GET_CODE (t) == ZERO_EXTEND
6678 && is_a <scalar_int_mode> (GET_MODE (XEXP (t, 0)), &inner_mode)
6679 && (GET_CODE (XEXP (t, 0)) == PLUS
6680 || GET_CODE (XEXP (t, 0)) == MINUS
6681 || GET_CODE (XEXP (t, 0)) == IOR
6682 || GET_CODE (XEXP (t, 0)) == XOR
6683 || GET_CODE (XEXP (t, 0)) == ASHIFT
6684 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6685 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6686 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6687 && HWI_COMPUTABLE_MODE_P (int_mode)
6688 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6689 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6690 && ((nonzero_bits (f, GET_MODE (f))
6691 & ~GET_MODE_MASK (inner_mode))
6692 == 0))
6694 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6695 extend_op = ZERO_EXTEND;
6696 m = inner_mode;
6698 else if (GET_CODE (t) == ZERO_EXTEND
6699 && is_a <scalar_int_mode> (GET_MODE (XEXP (t, 0)), &inner_mode)
6700 && (GET_CODE (XEXP (t, 0)) == PLUS
6701 || GET_CODE (XEXP (t, 0)) == IOR
6702 || GET_CODE (XEXP (t, 0)) == XOR)
6703 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6704 && HWI_COMPUTABLE_MODE_P (int_mode)
6705 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6706 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6707 && ((nonzero_bits (f, GET_MODE (f))
6708 & ~GET_MODE_MASK (inner_mode))
6709 == 0))
6711 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6712 extend_op = ZERO_EXTEND;
6713 m = inner_mode;
6716 if (z)
6718 machine_mode cm = m;
6719 if ((op == ASHIFT || op == LSHIFTRT || op == ASHIFTRT)
6720 && GET_MODE (c1) != VOIDmode)
6721 cm = GET_MODE (c1);
6722 temp = subst (simplify_gen_relational (true_code, cm, VOIDmode,
6723 cond_op0, cond_op1),
6724 pc_rtx, pc_rtx, 0, 0, 0);
6725 temp = simplify_gen_binary (MULT, cm, temp,
6726 simplify_gen_binary (MULT, cm, c1,
6727 const_true_rtx));
6728 temp = subst (temp, pc_rtx, pc_rtx, 0, 0, 0);
6729 temp = simplify_gen_binary (op, m, gen_lowpart (m, z), temp);
6731 if (extend_op != UNKNOWN)
6732 temp = simplify_gen_unary (extend_op, int_mode, temp, m);
6734 return temp;
6738 /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
6739 1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
6740 negation of a single bit, we can convert this operation to a shift. We
6741 can actually do this more generally, but it doesn't seem worth it. */
6743 if (true_code == NE
6744 && is_a <scalar_int_mode> (mode, &int_mode)
6745 && XEXP (cond, 1) == const0_rtx
6746 && false_rtx == const0_rtx
6747 && CONST_INT_P (true_rtx)
6748 && ((nonzero_bits (XEXP (cond, 0), int_mode) == 1
6749 && (i = exact_log2 (UINTVAL (true_rtx))) >= 0)
6750 || ((num_sign_bit_copies (XEXP (cond, 0), int_mode)
6751 == GET_MODE_PRECISION (int_mode))
6752 && (i = exact_log2 (-UINTVAL (true_rtx))) >= 0)))
6753 return
6754 simplify_shift_const (NULL_RTX, ASHIFT, int_mode,
6755 gen_lowpart (int_mode, XEXP (cond, 0)), i);
6757 /* (IF_THEN_ELSE (NE A 0) C1 0) is A or a zero-extend of A if the only
6758 non-zero bit in A is C1. */
6759 if (true_code == NE && XEXP (cond, 1) == const0_rtx
6760 && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
6761 && is_a <scalar_int_mode> (mode, &int_mode)
6762 && is_a <scalar_int_mode> (GET_MODE (XEXP (cond, 0)), &inner_mode)
6763 && (UINTVAL (true_rtx) & GET_MODE_MASK (int_mode))
6764 == nonzero_bits (XEXP (cond, 0), inner_mode)
6765 && (i = exact_log2 (UINTVAL (true_rtx) & GET_MODE_MASK (int_mode))) >= 0)
6767 rtx val = XEXP (cond, 0);
6768 if (inner_mode == int_mode)
6769 return val;
6770 else if (GET_MODE_PRECISION (inner_mode) < GET_MODE_PRECISION (int_mode))
6771 return simplify_gen_unary (ZERO_EXTEND, int_mode, val, inner_mode);
6774 return x;
6777 /* Simplify X, a SET expression. Return the new expression. */
6779 static rtx
6780 simplify_set (rtx x)
6782 rtx src = SET_SRC (x);
6783 rtx dest = SET_DEST (x);
6784 machine_mode mode
6785 = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
6786 rtx_insn *other_insn;
6787 rtx *cc_use;
6788 scalar_int_mode int_mode;
6790 /* (set (pc) (return)) gets written as (return). */
6791 if (GET_CODE (dest) == PC && ANY_RETURN_P (src))
6792 return src;
6794 /* Now that we know for sure which bits of SRC we are using, see if we can
6795 simplify the expression for the object knowing that we only need the
6796 low-order bits. */
6798 if (GET_MODE_CLASS (mode) == MODE_INT && HWI_COMPUTABLE_MODE_P (mode))
6800 src = force_to_mode (src, mode, HOST_WIDE_INT_M1U, 0);
6801 SUBST (SET_SRC (x), src);
6804 /* If we are setting CC0 or if the source is a COMPARE, look for the use of
6805 the comparison result and try to simplify it unless we already have used
6806 undobuf.other_insn. */
6807 if ((GET_MODE_CLASS (mode) == MODE_CC
6808 || GET_CODE (src) == COMPARE
6809 || CC0_P (dest))
6810 && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
6811 && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
6812 && COMPARISON_P (*cc_use)
6813 && rtx_equal_p (XEXP (*cc_use, 0), dest))
6815 enum rtx_code old_code = GET_CODE (*cc_use);
6816 enum rtx_code new_code;
6817 rtx op0, op1, tmp;
6818 int other_changed = 0;
6819 rtx inner_compare = NULL_RTX;
6820 machine_mode compare_mode = GET_MODE (dest);
6822 if (GET_CODE (src) == COMPARE)
6824 op0 = XEXP (src, 0), op1 = XEXP (src, 1);
6825 if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
6827 inner_compare = op0;
6828 op0 = XEXP (inner_compare, 0), op1 = XEXP (inner_compare, 1);
6831 else
6832 op0 = src, op1 = CONST0_RTX (GET_MODE (src));
6834 tmp = simplify_relational_operation (old_code, compare_mode, VOIDmode,
6835 op0, op1);
6836 if (!tmp)
6837 new_code = old_code;
6838 else if (!CONSTANT_P (tmp))
6840 new_code = GET_CODE (tmp);
6841 op0 = XEXP (tmp, 0);
6842 op1 = XEXP (tmp, 1);
6844 else
6846 rtx pat = PATTERN (other_insn);
6847 undobuf.other_insn = other_insn;
6848 SUBST (*cc_use, tmp);
6850 /* Attempt to simplify CC user. */
6851 if (GET_CODE (pat) == SET)
6853 rtx new_rtx = simplify_rtx (SET_SRC (pat));
6854 if (new_rtx != NULL_RTX)
6855 SUBST (SET_SRC (pat), new_rtx);
6858 /* Convert X into a no-op move. */
6859 SUBST (SET_DEST (x), pc_rtx);
6860 SUBST (SET_SRC (x), pc_rtx);
6861 return x;
6864 /* Simplify our comparison, if possible. */
6865 new_code = simplify_comparison (new_code, &op0, &op1);
6867 #ifdef SELECT_CC_MODE
6868 /* If this machine has CC modes other than CCmode, check to see if we
6869 need to use a different CC mode here. */
6870 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
6871 compare_mode = GET_MODE (op0);
6872 else if (inner_compare
6873 && GET_MODE_CLASS (GET_MODE (inner_compare)) == MODE_CC
6874 && new_code == old_code
6875 && op0 == XEXP (inner_compare, 0)
6876 && op1 == XEXP (inner_compare, 1))
6877 compare_mode = GET_MODE (inner_compare);
6878 else
6879 compare_mode = SELECT_CC_MODE (new_code, op0, op1);
6881 /* If the mode changed, we have to change SET_DEST, the mode in the
6882 compare, and the mode in the place SET_DEST is used. If SET_DEST is
6883 a hard register, just build new versions with the proper mode. If it
6884 is a pseudo, we lose unless it is only time we set the pseudo, in
6885 which case we can safely change its mode. */
6886 if (!HAVE_cc0 && compare_mode != GET_MODE (dest))
6888 if (can_change_dest_mode (dest, 0, compare_mode))
6890 unsigned int regno = REGNO (dest);
6891 rtx new_dest;
6893 if (regno < FIRST_PSEUDO_REGISTER)
6894 new_dest = gen_rtx_REG (compare_mode, regno);
6895 else
6897 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
6898 new_dest = regno_reg_rtx[regno];
6901 SUBST (SET_DEST (x), new_dest);
6902 SUBST (XEXP (*cc_use, 0), new_dest);
6903 other_changed = 1;
6905 dest = new_dest;
6908 #endif /* SELECT_CC_MODE */
6910 /* If the code changed, we have to build a new comparison in
6911 undobuf.other_insn. */
6912 if (new_code != old_code)
6914 int other_changed_previously = other_changed;
6915 unsigned HOST_WIDE_INT mask;
6916 rtx old_cc_use = *cc_use;
6918 SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
6919 dest, const0_rtx));
6920 other_changed = 1;
6922 /* If the only change we made was to change an EQ into an NE or
6923 vice versa, OP0 has only one bit that might be nonzero, and OP1
6924 is zero, check if changing the user of the condition code will
6925 produce a valid insn. If it won't, we can keep the original code
6926 in that insn by surrounding our operation with an XOR. */
6928 if (((old_code == NE && new_code == EQ)
6929 || (old_code == EQ && new_code == NE))
6930 && ! other_changed_previously && op1 == const0_rtx
6931 && HWI_COMPUTABLE_MODE_P (GET_MODE (op0))
6932 && pow2p_hwi (mask = nonzero_bits (op0, GET_MODE (op0))))
6934 rtx pat = PATTERN (other_insn), note = 0;
6936 if ((recog_for_combine (&pat, other_insn, &note) < 0
6937 && ! check_asm_operands (pat)))
6939 *cc_use = old_cc_use;
6940 other_changed = 0;
6942 op0 = simplify_gen_binary (XOR, GET_MODE (op0), op0,
6943 gen_int_mode (mask,
6944 GET_MODE (op0)));
6949 if (other_changed)
6950 undobuf.other_insn = other_insn;
6952 /* Don't generate a compare of a CC with 0, just use that CC. */
6953 if (GET_MODE (op0) == compare_mode && op1 == const0_rtx)
6955 SUBST (SET_SRC (x), op0);
6956 src = SET_SRC (x);
6958 /* Otherwise, if we didn't previously have the same COMPARE we
6959 want, create it from scratch. */
6960 else if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode
6961 || XEXP (src, 0) != op0 || XEXP (src, 1) != op1)
6963 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
6964 src = SET_SRC (x);
6967 else
6969 /* Get SET_SRC in a form where we have placed back any
6970 compound expressions. Then do the checks below. */
6971 src = make_compound_operation (src, SET);
6972 SUBST (SET_SRC (x), src);
6975 /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
6976 and X being a REG or (subreg (reg)), we may be able to convert this to
6977 (set (subreg:m2 x) (op)).
6979 We can always do this if M1 is narrower than M2 because that means that
6980 we only care about the low bits of the result.
6982 However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
6983 perform a narrower operation than requested since the high-order bits will
6984 be undefined. On machine where it is defined, this transformation is safe
6985 as long as M1 and M2 have the same number of words. */
6987 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6988 && !OBJECT_P (SUBREG_REG (src))
6989 && (known_equal_after_align_up
6990 (GET_MODE_SIZE (GET_MODE (src)),
6991 GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))),
6992 UNITS_PER_WORD))
6993 && (WORD_REGISTER_OPERATIONS || !paradoxical_subreg_p (src))
6994 && ! (REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER
6995 && !REG_CAN_CHANGE_MODE_P (REGNO (dest),
6996 GET_MODE (SUBREG_REG (src)),
6997 GET_MODE (src)))
6998 && (REG_P (dest)
6999 || (GET_CODE (dest) == SUBREG
7000 && REG_P (SUBREG_REG (dest)))))
7002 SUBST (SET_DEST (x),
7003 gen_lowpart (GET_MODE (SUBREG_REG (src)),
7004 dest));
7005 SUBST (SET_SRC (x), SUBREG_REG (src));
7007 src = SET_SRC (x), dest = SET_DEST (x);
7010 /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
7011 in SRC. */
7012 if (dest == cc0_rtx
7013 && partial_subreg_p (src)
7014 && subreg_lowpart_p (src))
7016 rtx inner = SUBREG_REG (src);
7017 machine_mode inner_mode = GET_MODE (inner);
7019 /* Here we make sure that we don't have a sign bit on. */
7020 if (val_signbit_known_clear_p (GET_MODE (src),
7021 nonzero_bits (inner, inner_mode)))
7023 SUBST (SET_SRC (x), inner);
7024 src = SET_SRC (x);
7028 /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
7029 would require a paradoxical subreg. Replace the subreg with a
7030 zero_extend to avoid the reload that would otherwise be required.
7031 Don't do this unless we have a scalar integer mode, otherwise the
7032 transformation is incorrect. */
7034 enum rtx_code extend_op;
7035 if (paradoxical_subreg_p (src)
7036 && MEM_P (SUBREG_REG (src))
7037 && SCALAR_INT_MODE_P (GET_MODE (src))
7038 && (extend_op = load_extend_op (GET_MODE (SUBREG_REG (src)))) != UNKNOWN)
7040 SUBST (SET_SRC (x),
7041 gen_rtx_fmt_e (extend_op, GET_MODE (src), SUBREG_REG (src)));
7043 src = SET_SRC (x);
7046 /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
7047 are comparing an item known to be 0 or -1 against 0, use a logical
7048 operation instead. Check for one of the arms being an IOR of the other
7049 arm with some value. We compute three terms to be IOR'ed together. In
7050 practice, at most two will be nonzero. Then we do the IOR's. */
7052 if (GET_CODE (dest) != PC
7053 && GET_CODE (src) == IF_THEN_ELSE
7054 && is_int_mode (GET_MODE (src), &int_mode)
7055 && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
7056 && XEXP (XEXP (src, 0), 1) == const0_rtx
7057 && int_mode == GET_MODE (XEXP (XEXP (src, 0), 0))
7058 && (!HAVE_conditional_move
7059 || ! can_conditionally_move_p (int_mode))
7060 && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0), int_mode)
7061 == GET_MODE_PRECISION (int_mode))
7062 && ! side_effects_p (src))
7064 rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
7065 ? XEXP (src, 1) : XEXP (src, 2));
7066 rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
7067 ? XEXP (src, 2) : XEXP (src, 1));
7068 rtx term1 = const0_rtx, term2, term3;
7070 if (GET_CODE (true_rtx) == IOR
7071 && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
7072 term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
7073 else if (GET_CODE (true_rtx) == IOR
7074 && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
7075 term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
7076 else if (GET_CODE (false_rtx) == IOR
7077 && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
7078 term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
7079 else if (GET_CODE (false_rtx) == IOR
7080 && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
7081 term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
7083 term2 = simplify_gen_binary (AND, int_mode,
7084 XEXP (XEXP (src, 0), 0), true_rtx);
7085 term3 = simplify_gen_binary (AND, int_mode,
7086 simplify_gen_unary (NOT, int_mode,
7087 XEXP (XEXP (src, 0), 0),
7088 int_mode),
7089 false_rtx);
7091 SUBST (SET_SRC (x),
7092 simplify_gen_binary (IOR, int_mode,
7093 simplify_gen_binary (IOR, int_mode,
7094 term1, term2),
7095 term3));
7097 src = SET_SRC (x);
7100 /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
7101 whole thing fail. */
7102 if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
7103 return src;
7104 else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
7105 return dest;
7106 else
7107 /* Convert this into a field assignment operation, if possible. */
7108 return make_field_assignment (x);
7111 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
7112 result. */
7114 static rtx
7115 simplify_logical (rtx x)
7117 rtx op0 = XEXP (x, 0);
7118 rtx op1 = XEXP (x, 1);
7119 scalar_int_mode mode;
7121 switch (GET_CODE (x))
7123 case AND:
7124 /* We can call simplify_and_const_int only if we don't lose
7125 any (sign) bits when converting INTVAL (op1) to
7126 "unsigned HOST_WIDE_INT". */
7127 if (is_a <scalar_int_mode> (GET_MODE (x), &mode)
7128 && CONST_INT_P (op1)
7129 && (HWI_COMPUTABLE_MODE_P (mode)
7130 || INTVAL (op1) > 0))
7132 x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
7133 if (GET_CODE (x) != AND)
7134 return x;
7136 op0 = XEXP (x, 0);
7137 op1 = XEXP (x, 1);
7140 /* If we have any of (and (ior A B) C) or (and (xor A B) C),
7141 apply the distributive law and then the inverse distributive
7142 law to see if things simplify. */
7143 if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
7145 rtx result = distribute_and_simplify_rtx (x, 0);
7146 if (result)
7147 return result;
7149 if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
7151 rtx result = distribute_and_simplify_rtx (x, 1);
7152 if (result)
7153 return result;
7155 break;
7157 case IOR:
7158 /* If we have (ior (and A B) C), apply the distributive law and then
7159 the inverse distributive law to see if things simplify. */
7161 if (GET_CODE (op0) == AND)
7163 rtx result = distribute_and_simplify_rtx (x, 0);
7164 if (result)
7165 return result;
7168 if (GET_CODE (op1) == AND)
7170 rtx result = distribute_and_simplify_rtx (x, 1);
7171 if (result)
7172 return result;
7174 break;
7176 default:
7177 gcc_unreachable ();
7180 return x;
7183 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
7184 operations" because they can be replaced with two more basic operations.
7185 ZERO_EXTEND is also considered "compound" because it can be replaced with
7186 an AND operation, which is simpler, though only one operation.
7188 The function expand_compound_operation is called with an rtx expression
7189 and will convert it to the appropriate shifts and AND operations,
7190 simplifying at each stage.
7192 The function make_compound_operation is called to convert an expression
7193 consisting of shifts and ANDs into the equivalent compound expression.
7194 It is the inverse of this function, loosely speaking. */
7196 static rtx
7197 expand_compound_operation (rtx x)
7199 unsigned HOST_WIDE_INT pos = 0, len;
7200 int unsignedp = 0;
7201 unsigned int modewidth;
7202 rtx tem;
7203 scalar_int_mode inner_mode;
7205 switch (GET_CODE (x))
7207 case ZERO_EXTEND:
7208 unsignedp = 1;
7209 /* FALLTHRU */
7210 case SIGN_EXTEND:
7211 /* We can't necessarily use a const_int for a multiword mode;
7212 it depends on implicitly extending the value.
7213 Since we don't know the right way to extend it,
7214 we can't tell whether the implicit way is right.
7216 Even for a mode that is no wider than a const_int,
7217 we can't win, because we need to sign extend one of its bits through
7218 the rest of it, and we don't know which bit. */
7219 if (CONST_INT_P (XEXP (x, 0)))
7220 return x;
7222 /* Reject modes that aren't scalar integers because turning vector
7223 or complex modes into shifts causes problems. */
7224 if (!is_a <scalar_int_mode> (GET_MODE (XEXP (x, 0)), &inner_mode))
7225 return x;
7227 /* Return if (subreg:MODE FROM 0) is not a safe replacement for
7228 (zero_extend:MODE FROM) or (sign_extend:MODE FROM). It is for any MEM
7229 because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
7230 reloaded. If not for that, MEM's would very rarely be safe.
7232 Reject modes bigger than a word, because we might not be able
7233 to reference a two-register group starting with an arbitrary register
7234 (and currently gen_lowpart might crash for a SUBREG). */
7236 if (GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
7237 return x;
7239 len = GET_MODE_PRECISION (inner_mode);
7240 /* If the inner object has VOIDmode (the only way this can happen
7241 is if it is an ASM_OPERANDS), we can't do anything since we don't
7242 know how much masking to do. */
7243 if (len == 0)
7244 return x;
7246 break;
7248 case ZERO_EXTRACT:
7249 unsignedp = 1;
7251 /* fall through */
7253 case SIGN_EXTRACT:
7254 /* If the operand is a CLOBBER, just return it. */
7255 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
7256 return XEXP (x, 0);
7258 if (!CONST_INT_P (XEXP (x, 1))
7259 || !CONST_INT_P (XEXP (x, 2)))
7260 return x;
7262 /* Reject modes that aren't scalar integers because turning vector
7263 or complex modes into shifts causes problems. */
7264 if (!is_a <scalar_int_mode> (GET_MODE (XEXP (x, 0)), &inner_mode))
7265 return x;
7267 len = INTVAL (XEXP (x, 1));
7268 pos = INTVAL (XEXP (x, 2));
7270 /* This should stay within the object being extracted, fail otherwise. */
7271 if (len + pos > GET_MODE_PRECISION (inner_mode))
7272 return x;
7274 if (BITS_BIG_ENDIAN)
7275 pos = GET_MODE_PRECISION (inner_mode) - len - pos;
7277 break;
7279 default:
7280 return x;
7283 /* We've rejected non-scalar operations by now. */
7284 scalar_int_mode mode = as_a <scalar_int_mode> (GET_MODE (x));
7286 /* Convert sign extension to zero extension, if we know that the high
7287 bit is not set, as this is easier to optimize. It will be converted
7288 back to cheaper alternative in make_extraction. */
7289 if (GET_CODE (x) == SIGN_EXTEND
7290 && HWI_COMPUTABLE_MODE_P (mode)
7291 && ((nonzero_bits (XEXP (x, 0), inner_mode)
7292 & ~(((unsigned HOST_WIDE_INT) GET_MODE_MASK (inner_mode)) >> 1))
7293 == 0))
7295 rtx temp = gen_rtx_ZERO_EXTEND (mode, XEXP (x, 0));
7296 rtx temp2 = expand_compound_operation (temp);
7298 /* Make sure this is a profitable operation. */
7299 if (set_src_cost (x, mode, optimize_this_for_speed_p)
7300 > set_src_cost (temp2, mode, optimize_this_for_speed_p))
7301 return temp2;
7302 else if (set_src_cost (x, mode, optimize_this_for_speed_p)
7303 > set_src_cost (temp, mode, optimize_this_for_speed_p))
7304 return temp;
7305 else
7306 return x;
7309 /* We can optimize some special cases of ZERO_EXTEND. */
7310 if (GET_CODE (x) == ZERO_EXTEND)
7312 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
7313 know that the last value didn't have any inappropriate bits
7314 set. */
7315 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
7316 && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode
7317 && HWI_COMPUTABLE_MODE_P (mode)
7318 && (nonzero_bits (XEXP (XEXP (x, 0), 0), mode)
7319 & ~GET_MODE_MASK (inner_mode)) == 0)
7320 return XEXP (XEXP (x, 0), 0);
7322 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
7323 if (GET_CODE (XEXP (x, 0)) == SUBREG
7324 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == mode
7325 && subreg_lowpart_p (XEXP (x, 0))
7326 && HWI_COMPUTABLE_MODE_P (mode)
7327 && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), mode)
7328 & ~GET_MODE_MASK (inner_mode)) == 0)
7329 return SUBREG_REG (XEXP (x, 0));
7331 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
7332 is a comparison and STORE_FLAG_VALUE permits. This is like
7333 the first case, but it works even when MODE is larger
7334 than HOST_WIDE_INT. */
7335 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
7336 && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode
7337 && COMPARISON_P (XEXP (XEXP (x, 0), 0))
7338 && GET_MODE_PRECISION (inner_mode) <= HOST_BITS_PER_WIDE_INT
7339 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (inner_mode)) == 0)
7340 return XEXP (XEXP (x, 0), 0);
7342 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
7343 if (GET_CODE (XEXP (x, 0)) == SUBREG
7344 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == mode
7345 && subreg_lowpart_p (XEXP (x, 0))
7346 && COMPARISON_P (SUBREG_REG (XEXP (x, 0)))
7347 && GET_MODE_PRECISION (inner_mode) <= HOST_BITS_PER_WIDE_INT
7348 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (inner_mode)) == 0)
7349 return SUBREG_REG (XEXP (x, 0));
7353 /* If we reach here, we want to return a pair of shifts. The inner
7354 shift is a left shift of BITSIZE - POS - LEN bits. The outer
7355 shift is a right shift of BITSIZE - LEN bits. It is arithmetic or
7356 logical depending on the value of UNSIGNEDP.
7358 If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
7359 converted into an AND of a shift.
7361 We must check for the case where the left shift would have a negative
7362 count. This can happen in a case like (x >> 31) & 255 on machines
7363 that can't shift by a constant. On those machines, we would first
7364 combine the shift with the AND to produce a variable-position
7365 extraction. Then the constant of 31 would be substituted in
7366 to produce such a position. */
7368 modewidth = GET_MODE_PRECISION (mode);
7369 if (modewidth >= pos + len)
7371 tem = gen_lowpart (mode, XEXP (x, 0));
7372 if (!tem || GET_CODE (tem) == CLOBBER)
7373 return x;
7374 tem = simplify_shift_const (NULL_RTX, ASHIFT, mode,
7375 tem, modewidth - pos - len);
7376 tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
7377 mode, tem, modewidth - len);
7379 else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
7380 tem = simplify_and_const_int (NULL_RTX, mode,
7381 simplify_shift_const (NULL_RTX, LSHIFTRT,
7382 mode, XEXP (x, 0),
7383 pos),
7384 (HOST_WIDE_INT_1U << len) - 1);
7385 else
7386 /* Any other cases we can't handle. */
7387 return x;
7389 /* If we couldn't do this for some reason, return the original
7390 expression. */
7391 if (GET_CODE (tem) == CLOBBER)
7392 return x;
7394 return tem;
7397 /* X is a SET which contains an assignment of one object into
7398 a part of another (such as a bit-field assignment, STRICT_LOW_PART,
7399 or certain SUBREGS). If possible, convert it into a series of
7400 logical operations.
7402 We half-heartedly support variable positions, but do not at all
7403 support variable lengths. */
7405 static const_rtx
7406 expand_field_assignment (const_rtx x)
7408 rtx inner;
7409 rtx pos; /* Always counts from low bit. */
7410 int len, inner_len;
7411 rtx mask, cleared, masked;
7412 scalar_int_mode compute_mode;
7414 /* Loop until we find something we can't simplify. */
7415 while (1)
7417 if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
7418 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
7420 rtx x0 = XEXP (SET_DEST (x), 0);
7421 if (!GET_MODE_PRECISION (GET_MODE (x0)).is_constant (&len))
7422 break;
7423 inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
7424 pos = gen_int_mode (subreg_lsb (XEXP (SET_DEST (x), 0)),
7425 MAX_MODE_INT);
7427 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
7428 && CONST_INT_P (XEXP (SET_DEST (x), 1)))
7430 inner = XEXP (SET_DEST (x), 0);
7431 if (!GET_MODE_PRECISION (GET_MODE (inner)).is_constant (&inner_len))
7432 break;
7434 len = INTVAL (XEXP (SET_DEST (x), 1));
7435 pos = XEXP (SET_DEST (x), 2);
7437 /* A constant position should stay within the width of INNER. */
7438 if (CONST_INT_P (pos) && INTVAL (pos) + len > inner_len)
7439 break;
7441 if (BITS_BIG_ENDIAN)
7443 if (CONST_INT_P (pos))
7444 pos = GEN_INT (inner_len - len - INTVAL (pos));
7445 else if (GET_CODE (pos) == MINUS
7446 && CONST_INT_P (XEXP (pos, 1))
7447 && INTVAL (XEXP (pos, 1)) == inner_len - len)
7448 /* If position is ADJUST - X, new position is X. */
7449 pos = XEXP (pos, 0);
7450 else
7451 pos = simplify_gen_binary (MINUS, GET_MODE (pos),
7452 gen_int_mode (inner_len - len,
7453 GET_MODE (pos)),
7454 pos);
7458 /* If the destination is a subreg that overwrites the whole of the inner
7459 register, we can move the subreg to the source. */
7460 else if (GET_CODE (SET_DEST (x)) == SUBREG
7461 /* We need SUBREGs to compute nonzero_bits properly. */
7462 && nonzero_sign_valid
7463 && !read_modify_subreg_p (SET_DEST (x)))
7465 x = gen_rtx_SET (SUBREG_REG (SET_DEST (x)),
7466 gen_lowpart
7467 (GET_MODE (SUBREG_REG (SET_DEST (x))),
7468 SET_SRC (x)));
7469 continue;
7471 else
7472 break;
7474 while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
7475 inner = SUBREG_REG (inner);
7477 /* Don't attempt bitwise arithmetic on non scalar integer modes. */
7478 if (!is_a <scalar_int_mode> (GET_MODE (inner), &compute_mode))
7480 /* Don't do anything for vector or complex integral types. */
7481 if (! FLOAT_MODE_P (GET_MODE (inner)))
7482 break;
7484 /* Try to find an integral mode to pun with. */
7485 if (!int_mode_for_size (GET_MODE_BITSIZE (GET_MODE (inner)), 0)
7486 .exists (&compute_mode))
7487 break;
7489 inner = gen_lowpart (compute_mode, inner);
7492 /* Compute a mask of LEN bits, if we can do this on the host machine. */
7493 if (len >= HOST_BITS_PER_WIDE_INT)
7494 break;
7496 /* Don't try to compute in too wide unsupported modes. */
7497 if (!targetm.scalar_mode_supported_p (compute_mode))
7498 break;
7500 /* Now compute the equivalent expression. Make a copy of INNER
7501 for the SET_DEST in case it is a MEM into which we will substitute;
7502 we don't want shared RTL in that case. */
7503 mask = gen_int_mode ((HOST_WIDE_INT_1U << len) - 1,
7504 compute_mode);
7505 cleared = simplify_gen_binary (AND, compute_mode,
7506 simplify_gen_unary (NOT, compute_mode,
7507 simplify_gen_binary (ASHIFT,
7508 compute_mode,
7509 mask, pos),
7510 compute_mode),
7511 inner);
7512 masked = simplify_gen_binary (ASHIFT, compute_mode,
7513 simplify_gen_binary (
7514 AND, compute_mode,
7515 gen_lowpart (compute_mode, SET_SRC (x)),
7516 mask),
7517 pos);
7519 x = gen_rtx_SET (copy_rtx (inner),
7520 simplify_gen_binary (IOR, compute_mode,
7521 cleared, masked));
7524 return x;
7527 /* Return an RTX for a reference to LEN bits of INNER. If POS_RTX is nonzero,
7528 it is an RTX that represents the (variable) starting position; otherwise,
7529 POS is the (constant) starting bit position. Both are counted from the LSB.
7531 UNSIGNEDP is nonzero for an unsigned reference and zero for a signed one.
7533 IN_DEST is nonzero if this is a reference in the destination of a SET.
7534 This is used when a ZERO_ or SIGN_EXTRACT isn't needed. If nonzero,
7535 a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
7536 be used.
7538 IN_COMPARE is nonzero if we are in a COMPARE. This means that a
7539 ZERO_EXTRACT should be built even for bits starting at bit 0.
7541 MODE is the desired mode of the result (if IN_DEST == 0).
7543 The result is an RTX for the extraction or NULL_RTX if the target
7544 can't handle it. */
7546 static rtx
7547 make_extraction (machine_mode mode, rtx inner, HOST_WIDE_INT pos,
7548 rtx pos_rtx, unsigned HOST_WIDE_INT len, int unsignedp,
7549 int in_dest, int in_compare)
7551 /* This mode describes the size of the storage area
7552 to fetch the overall value from. Within that, we
7553 ignore the POS lowest bits, etc. */
7554 machine_mode is_mode = GET_MODE (inner);
7555 machine_mode inner_mode;
7556 scalar_int_mode wanted_inner_mode;
7557 scalar_int_mode wanted_inner_reg_mode = word_mode;
7558 scalar_int_mode pos_mode = word_mode;
7559 machine_mode extraction_mode = word_mode;
7560 rtx new_rtx = 0;
7561 rtx orig_pos_rtx = pos_rtx;
7562 HOST_WIDE_INT orig_pos;
7564 if (pos_rtx && CONST_INT_P (pos_rtx))
7565 pos = INTVAL (pos_rtx), pos_rtx = 0;
7567 if (GET_CODE (inner) == SUBREG
7568 && subreg_lowpart_p (inner)
7569 && (paradoxical_subreg_p (inner)
7570 /* If trying or potentionally trying to extract
7571 bits outside of is_mode, don't look through
7572 non-paradoxical SUBREGs. See PR82192. */
7573 || (pos_rtx == NULL_RTX
7574 && known_le (pos + len, GET_MODE_PRECISION (is_mode)))))
7576 /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
7577 consider just the QI as the memory to extract from.
7578 The subreg adds or removes high bits; its mode is
7579 irrelevant to the meaning of this extraction,
7580 since POS and LEN count from the lsb. */
7581 if (MEM_P (SUBREG_REG (inner)))
7582 is_mode = GET_MODE (SUBREG_REG (inner));
7583 inner = SUBREG_REG (inner);
7585 else if (GET_CODE (inner) == ASHIFT
7586 && CONST_INT_P (XEXP (inner, 1))
7587 && pos_rtx == 0 && pos == 0
7588 && len > UINTVAL (XEXP (inner, 1)))
7590 /* We're extracting the least significant bits of an rtx
7591 (ashift X (const_int C)), where LEN > C. Extract the
7592 least significant (LEN - C) bits of X, giving an rtx
7593 whose mode is MODE, then shift it left C times. */
7594 new_rtx = make_extraction (mode, XEXP (inner, 0),
7595 0, 0, len - INTVAL (XEXP (inner, 1)),
7596 unsignedp, in_dest, in_compare);
7597 if (new_rtx != 0)
7598 return gen_rtx_ASHIFT (mode, new_rtx, XEXP (inner, 1));
7600 else if (GET_CODE (inner) == TRUNCATE
7601 /* If trying or potentionally trying to extract
7602 bits outside of is_mode, don't look through
7603 TRUNCATE. See PR82192. */
7604 && pos_rtx == NULL_RTX
7605 && known_le (pos + len, GET_MODE_PRECISION (is_mode)))
7606 inner = XEXP (inner, 0);
7608 inner_mode = GET_MODE (inner);
7610 /* See if this can be done without an extraction. We never can if the
7611 width of the field is not the same as that of some integer mode. For
7612 registers, we can only avoid the extraction if the position is at the
7613 low-order bit and this is either not in the destination or we have the
7614 appropriate STRICT_LOW_PART operation available.
7616 For MEM, we can avoid an extract if the field starts on an appropriate
7617 boundary and we can change the mode of the memory reference. */
7619 scalar_int_mode tmode;
7620 if (int_mode_for_size (len, 1).exists (&tmode)
7621 && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
7622 && !MEM_P (inner)
7623 && (pos == 0 || REG_P (inner))
7624 && (inner_mode == tmode
7625 || !REG_P (inner)
7626 || TRULY_NOOP_TRUNCATION_MODES_P (tmode, inner_mode)
7627 || reg_truncated_to_mode (tmode, inner))
7628 && (! in_dest
7629 || (REG_P (inner)
7630 && have_insn_for (STRICT_LOW_PART, tmode))))
7631 || (MEM_P (inner) && pos_rtx == 0
7632 && (pos
7633 % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
7634 : BITS_PER_UNIT)) == 0
7635 /* We can't do this if we are widening INNER_MODE (it
7636 may not be aligned, for one thing). */
7637 && !paradoxical_subreg_p (tmode, inner_mode)
7638 && (inner_mode == tmode
7639 || (! mode_dependent_address_p (XEXP (inner, 0),
7640 MEM_ADDR_SPACE (inner))
7641 && ! MEM_VOLATILE_P (inner))))))
7643 /* If INNER is a MEM, make a new MEM that encompasses just the desired
7644 field. If the original and current mode are the same, we need not
7645 adjust the offset. Otherwise, we do if bytes big endian.
7647 If INNER is not a MEM, get a piece consisting of just the field
7648 of interest (in this case POS % BITS_PER_WORD must be 0). */
7650 if (MEM_P (inner))
7652 poly_int64 offset;
7654 /* POS counts from lsb, but make OFFSET count in memory order. */
7655 if (BYTES_BIG_ENDIAN)
7656 offset = bits_to_bytes_round_down (GET_MODE_PRECISION (is_mode)
7657 - len - pos);
7658 else
7659 offset = pos / BITS_PER_UNIT;
7661 new_rtx = adjust_address_nv (inner, tmode, offset);
7663 else if (REG_P (inner))
7665 if (tmode != inner_mode)
7667 /* We can't call gen_lowpart in a DEST since we
7668 always want a SUBREG (see below) and it would sometimes
7669 return a new hard register. */
7670 if (pos || in_dest)
7672 poly_uint64 offset
7673 = subreg_offset_from_lsb (tmode, inner_mode, pos);
7675 /* Avoid creating invalid subregs, for example when
7676 simplifying (x>>32)&255. */
7677 if (!validate_subreg (tmode, inner_mode, inner, offset))
7678 return NULL_RTX;
7680 new_rtx = gen_rtx_SUBREG (tmode, inner, offset);
7682 else
7683 new_rtx = gen_lowpart (tmode, inner);
7685 else
7686 new_rtx = inner;
7688 else
7689 new_rtx = force_to_mode (inner, tmode,
7690 len >= HOST_BITS_PER_WIDE_INT
7691 ? HOST_WIDE_INT_M1U
7692 : (HOST_WIDE_INT_1U << len) - 1, 0);
7694 /* If this extraction is going into the destination of a SET,
7695 make a STRICT_LOW_PART unless we made a MEM. */
7697 if (in_dest)
7698 return (MEM_P (new_rtx) ? new_rtx
7699 : (GET_CODE (new_rtx) != SUBREG
7700 ? gen_rtx_CLOBBER (tmode, const0_rtx)
7701 : gen_rtx_STRICT_LOW_PART (VOIDmode, new_rtx)));
7703 if (mode == tmode)
7704 return new_rtx;
7706 if (CONST_SCALAR_INT_P (new_rtx))
7707 return simplify_unary_operation (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7708 mode, new_rtx, tmode);
7710 /* If we know that no extraneous bits are set, and that the high
7711 bit is not set, convert the extraction to the cheaper of
7712 sign and zero extension, that are equivalent in these cases. */
7713 if (flag_expensive_optimizations
7714 && (HWI_COMPUTABLE_MODE_P (tmode)
7715 && ((nonzero_bits (new_rtx, tmode)
7716 & ~(((unsigned HOST_WIDE_INT)GET_MODE_MASK (tmode)) >> 1))
7717 == 0)))
7719 rtx temp = gen_rtx_ZERO_EXTEND (mode, new_rtx);
7720 rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new_rtx);
7722 /* Prefer ZERO_EXTENSION, since it gives more information to
7723 backends. */
7724 if (set_src_cost (temp, mode, optimize_this_for_speed_p)
7725 <= set_src_cost (temp1, mode, optimize_this_for_speed_p))
7726 return temp;
7727 return temp1;
7730 /* Otherwise, sign- or zero-extend unless we already are in the
7731 proper mode. */
7733 return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7734 mode, new_rtx));
7737 /* Unless this is a COMPARE or we have a funny memory reference,
7738 don't do anything with zero-extending field extracts starting at
7739 the low-order bit since they are simple AND operations. */
7740 if (pos_rtx == 0 && pos == 0 && ! in_dest
7741 && ! in_compare && unsignedp)
7742 return 0;
7744 /* Unless INNER is not MEM, reject this if we would be spanning bytes or
7745 if the position is not a constant and the length is not 1. In all
7746 other cases, we would only be going outside our object in cases when
7747 an original shift would have been undefined. */
7748 if (MEM_P (inner)
7749 && ((pos_rtx == 0 && maybe_gt (pos + len, GET_MODE_PRECISION (is_mode)))
7750 || (pos_rtx != 0 && len != 1)))
7751 return 0;
7753 enum extraction_pattern pattern = (in_dest ? EP_insv
7754 : unsignedp ? EP_extzv : EP_extv);
7756 /* If INNER is not from memory, we want it to have the mode of a register
7757 extraction pattern's structure operand, or word_mode if there is no
7758 such pattern. The same applies to extraction_mode and pos_mode
7759 and their respective operands.
7761 For memory, assume that the desired extraction_mode and pos_mode
7762 are the same as for a register operation, since at present we don't
7763 have named patterns for aligned memory structures. */
7764 struct extraction_insn insn;
7765 unsigned int inner_size;
7766 if (GET_MODE_BITSIZE (inner_mode).is_constant (&inner_size)
7767 && get_best_reg_extraction_insn (&insn, pattern, inner_size, mode))
7769 wanted_inner_reg_mode = insn.struct_mode.require ();
7770 pos_mode = insn.pos_mode;
7771 extraction_mode = insn.field_mode;
7774 /* Never narrow an object, since that might not be safe. */
7776 if (mode != VOIDmode
7777 && partial_subreg_p (extraction_mode, mode))
7778 extraction_mode = mode;
7780 if (!MEM_P (inner))
7781 wanted_inner_mode = wanted_inner_reg_mode;
7782 else
7784 /* Be careful not to go beyond the extracted object and maintain the
7785 natural alignment of the memory. */
7786 wanted_inner_mode = smallest_int_mode_for_size (len);
7787 while (pos % GET_MODE_BITSIZE (wanted_inner_mode) + len
7788 > GET_MODE_BITSIZE (wanted_inner_mode))
7789 wanted_inner_mode = GET_MODE_WIDER_MODE (wanted_inner_mode).require ();
7792 orig_pos = pos;
7794 if (BITS_BIG_ENDIAN)
7796 /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
7797 BITS_BIG_ENDIAN style. If position is constant, compute new
7798 position. Otherwise, build subtraction.
7799 Note that POS is relative to the mode of the original argument.
7800 If it's a MEM we need to recompute POS relative to that.
7801 However, if we're extracting from (or inserting into) a register,
7802 we want to recompute POS relative to wanted_inner_mode. */
7803 int width;
7804 if (!MEM_P (inner))
7805 width = GET_MODE_BITSIZE (wanted_inner_mode);
7806 else if (!GET_MODE_BITSIZE (is_mode).is_constant (&width))
7807 return NULL_RTX;
7809 if (pos_rtx == 0)
7810 pos = width - len - pos;
7811 else
7812 pos_rtx
7813 = gen_rtx_MINUS (GET_MODE (pos_rtx),
7814 gen_int_mode (width - len, GET_MODE (pos_rtx)),
7815 pos_rtx);
7816 /* POS may be less than 0 now, but we check for that below.
7817 Note that it can only be less than 0 if !MEM_P (inner). */
7820 /* If INNER has a wider mode, and this is a constant extraction, try to
7821 make it smaller and adjust the byte to point to the byte containing
7822 the value. */
7823 if (wanted_inner_mode != VOIDmode
7824 && inner_mode != wanted_inner_mode
7825 && ! pos_rtx
7826 && partial_subreg_p (wanted_inner_mode, is_mode)
7827 && MEM_P (inner)
7828 && ! mode_dependent_address_p (XEXP (inner, 0), MEM_ADDR_SPACE (inner))
7829 && ! MEM_VOLATILE_P (inner))
7831 poly_int64 offset = 0;
7833 /* The computations below will be correct if the machine is big
7834 endian in both bits and bytes or little endian in bits and bytes.
7835 If it is mixed, we must adjust. */
7837 /* If bytes are big endian and we had a paradoxical SUBREG, we must
7838 adjust OFFSET to compensate. */
7839 if (BYTES_BIG_ENDIAN
7840 && paradoxical_subreg_p (is_mode, inner_mode))
7841 offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
7843 /* We can now move to the desired byte. */
7844 offset += (pos / GET_MODE_BITSIZE (wanted_inner_mode))
7845 * GET_MODE_SIZE (wanted_inner_mode);
7846 pos %= GET_MODE_BITSIZE (wanted_inner_mode);
7848 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
7849 && is_mode != wanted_inner_mode)
7850 offset = (GET_MODE_SIZE (is_mode)
7851 - GET_MODE_SIZE (wanted_inner_mode) - offset);
7853 inner = adjust_address_nv (inner, wanted_inner_mode, offset);
7856 /* If INNER is not memory, get it into the proper mode. If we are changing
7857 its mode, POS must be a constant and smaller than the size of the new
7858 mode. */
7859 else if (!MEM_P (inner))
7861 /* On the LHS, don't create paradoxical subregs implicitely truncating
7862 the register unless TARGET_TRULY_NOOP_TRUNCATION. */
7863 if (in_dest
7864 && !TRULY_NOOP_TRUNCATION_MODES_P (GET_MODE (inner),
7865 wanted_inner_mode))
7866 return NULL_RTX;
7868 if (GET_MODE (inner) != wanted_inner_mode
7869 && (pos_rtx != 0
7870 || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
7871 return NULL_RTX;
7873 if (orig_pos < 0)
7874 return NULL_RTX;
7876 inner = force_to_mode (inner, wanted_inner_mode,
7877 pos_rtx
7878 || len + orig_pos >= HOST_BITS_PER_WIDE_INT
7879 ? HOST_WIDE_INT_M1U
7880 : (((HOST_WIDE_INT_1U << len) - 1)
7881 << orig_pos),
7885 /* Adjust mode of POS_RTX, if needed. If we want a wider mode, we
7886 have to zero extend. Otherwise, we can just use a SUBREG.
7888 We dealt with constant rtxes earlier, so pos_rtx cannot
7889 have VOIDmode at this point. */
7890 if (pos_rtx != 0
7891 && (GET_MODE_SIZE (pos_mode)
7892 > GET_MODE_SIZE (as_a <scalar_int_mode> (GET_MODE (pos_rtx)))))
7894 rtx temp = simplify_gen_unary (ZERO_EXTEND, pos_mode, pos_rtx,
7895 GET_MODE (pos_rtx));
7897 /* If we know that no extraneous bits are set, and that the high
7898 bit is not set, convert extraction to cheaper one - either
7899 SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
7900 cases. */
7901 if (flag_expensive_optimizations
7902 && (HWI_COMPUTABLE_MODE_P (GET_MODE (pos_rtx))
7903 && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
7904 & ~(((unsigned HOST_WIDE_INT)
7905 GET_MODE_MASK (GET_MODE (pos_rtx)))
7906 >> 1))
7907 == 0)))
7909 rtx temp1 = simplify_gen_unary (SIGN_EXTEND, pos_mode, pos_rtx,
7910 GET_MODE (pos_rtx));
7912 /* Prefer ZERO_EXTENSION, since it gives more information to
7913 backends. */
7914 if (set_src_cost (temp1, pos_mode, optimize_this_for_speed_p)
7915 < set_src_cost (temp, pos_mode, optimize_this_for_speed_p))
7916 temp = temp1;
7918 pos_rtx = temp;
7921 /* Make POS_RTX unless we already have it and it is correct. If we don't
7922 have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
7923 be a CONST_INT. */
7924 if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
7925 pos_rtx = orig_pos_rtx;
7927 else if (pos_rtx == 0)
7928 pos_rtx = GEN_INT (pos);
7930 /* Make the required operation. See if we can use existing rtx. */
7931 new_rtx = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
7932 extraction_mode, inner, GEN_INT (len), pos_rtx);
7933 if (! in_dest)
7934 new_rtx = gen_lowpart (mode, new_rtx);
7936 return new_rtx;
7939 /* See if X (of mode MODE) contains an ASHIFT of COUNT or more bits that
7940 can be commuted with any other operations in X. Return X without
7941 that shift if so. */
7943 static rtx
7944 extract_left_shift (scalar_int_mode mode, rtx x, int count)
7946 enum rtx_code code = GET_CODE (x);
7947 rtx tem;
7949 switch (code)
7951 case ASHIFT:
7952 /* This is the shift itself. If it is wide enough, we will return
7953 either the value being shifted if the shift count is equal to
7954 COUNT or a shift for the difference. */
7955 if (CONST_INT_P (XEXP (x, 1))
7956 && INTVAL (XEXP (x, 1)) >= count)
7957 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
7958 INTVAL (XEXP (x, 1)) - count);
7959 break;
7961 case NEG: case NOT:
7962 if ((tem = extract_left_shift (mode, XEXP (x, 0), count)) != 0)
7963 return simplify_gen_unary (code, mode, tem, mode);
7965 break;
7967 case PLUS: case IOR: case XOR: case AND:
7968 /* If we can safely shift this constant and we find the inner shift,
7969 make a new operation. */
7970 if (CONST_INT_P (XEXP (x, 1))
7971 && (UINTVAL (XEXP (x, 1))
7972 & (((HOST_WIDE_INT_1U << count)) - 1)) == 0
7973 && (tem = extract_left_shift (mode, XEXP (x, 0), count)) != 0)
7975 HOST_WIDE_INT val = INTVAL (XEXP (x, 1)) >> count;
7976 return simplify_gen_binary (code, mode, tem,
7977 gen_int_mode (val, mode));
7979 break;
7981 default:
7982 break;
7985 return 0;
7988 /* Subroutine of make_compound_operation. *X_PTR is the rtx at the current
7989 level of the expression and MODE is its mode. IN_CODE is as for
7990 make_compound_operation. *NEXT_CODE_PTR is the value of IN_CODE
7991 that should be used when recursing on operands of *X_PTR.
7993 There are two possible actions:
7995 - Return null. This tells the caller to recurse on *X_PTR with IN_CODE
7996 equal to *NEXT_CODE_PTR, after which *X_PTR holds the final value.
7998 - Return a new rtx, which the caller returns directly. */
8000 static rtx
8001 make_compound_operation_int (scalar_int_mode mode, rtx *x_ptr,
8002 enum rtx_code in_code,
8003 enum rtx_code *next_code_ptr)
8005 rtx x = *x_ptr;
8006 enum rtx_code next_code = *next_code_ptr;
8007 enum rtx_code code = GET_CODE (x);
8008 int mode_width = GET_MODE_PRECISION (mode);
8009 rtx rhs, lhs;
8010 rtx new_rtx = 0;
8011 int i;
8012 rtx tem;
8013 scalar_int_mode inner_mode;
8014 bool equality_comparison = false;
8016 if (in_code == EQ)
8018 equality_comparison = true;
8019 in_code = COMPARE;
8022 /* Process depending on the code of this operation. If NEW is set
8023 nonzero, it will be returned. */
8025 switch (code)
8027 case ASHIFT:
8028 /* Convert shifts by constants into multiplications if inside
8029 an address. */
8030 if (in_code == MEM && CONST_INT_P (XEXP (x, 1))
8031 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
8032 && INTVAL (XEXP (x, 1)) >= 0)
8034 HOST_WIDE_INT count = INTVAL (XEXP (x, 1));
8035 HOST_WIDE_INT multval = HOST_WIDE_INT_1 << count;
8037 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
8038 if (GET_CODE (new_rtx) == NEG)
8040 new_rtx = XEXP (new_rtx, 0);
8041 multval = -multval;
8043 multval = trunc_int_for_mode (multval, mode);
8044 new_rtx = gen_rtx_MULT (mode, new_rtx, gen_int_mode (multval, mode));
8046 break;
8048 case PLUS:
8049 lhs = XEXP (x, 0);
8050 rhs = XEXP (x, 1);
8051 lhs = make_compound_operation (lhs, next_code);
8052 rhs = make_compound_operation (rhs, next_code);
8053 if (GET_CODE (lhs) == MULT && GET_CODE (XEXP (lhs, 0)) == NEG)
8055 tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (lhs, 0), 0),
8056 XEXP (lhs, 1));
8057 new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
8059 else if (GET_CODE (lhs) == MULT
8060 && (CONST_INT_P (XEXP (lhs, 1)) && INTVAL (XEXP (lhs, 1)) < 0))
8062 tem = simplify_gen_binary (MULT, mode, XEXP (lhs, 0),
8063 simplify_gen_unary (NEG, mode,
8064 XEXP (lhs, 1),
8065 mode));
8066 new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
8068 else
8070 SUBST (XEXP (x, 0), lhs);
8071 SUBST (XEXP (x, 1), rhs);
8073 maybe_swap_commutative_operands (x);
8074 return x;
8076 case MINUS:
8077 lhs = XEXP (x, 0);
8078 rhs = XEXP (x, 1);
8079 lhs = make_compound_operation (lhs, next_code);
8080 rhs = make_compound_operation (rhs, next_code);
8081 if (GET_CODE (rhs) == MULT && GET_CODE (XEXP (rhs, 0)) == NEG)
8083 tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (rhs, 0), 0),
8084 XEXP (rhs, 1));
8085 return simplify_gen_binary (PLUS, mode, tem, lhs);
8087 else if (GET_CODE (rhs) == MULT
8088 && (CONST_INT_P (XEXP (rhs, 1)) && INTVAL (XEXP (rhs, 1)) < 0))
8090 tem = simplify_gen_binary (MULT, mode, XEXP (rhs, 0),
8091 simplify_gen_unary (NEG, mode,
8092 XEXP (rhs, 1),
8093 mode));
8094 return simplify_gen_binary (PLUS, mode, tem, lhs);
8096 else
8098 SUBST (XEXP (x, 0), lhs);
8099 SUBST (XEXP (x, 1), rhs);
8100 return x;
8103 case AND:
8104 /* If the second operand is not a constant, we can't do anything
8105 with it. */
8106 if (!CONST_INT_P (XEXP (x, 1)))
8107 break;
8109 /* If the constant is a power of two minus one and the first operand
8110 is a logical right shift, make an extraction. */
8111 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8112 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
8114 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
8115 new_rtx = make_extraction (mode, new_rtx, 0, XEXP (XEXP (x, 0), 1),
8116 i, 1, 0, in_code == COMPARE);
8119 /* Same as previous, but for (subreg (lshiftrt ...)) in first op. */
8120 else if (GET_CODE (XEXP (x, 0)) == SUBREG
8121 && subreg_lowpart_p (XEXP (x, 0))
8122 && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (XEXP (x, 0))),
8123 &inner_mode)
8124 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
8125 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
8127 rtx inner_x0 = SUBREG_REG (XEXP (x, 0));
8128 new_rtx = make_compound_operation (XEXP (inner_x0, 0), next_code);
8129 new_rtx = make_extraction (inner_mode, new_rtx, 0,
8130 XEXP (inner_x0, 1),
8131 i, 1, 0, in_code == COMPARE);
8133 /* If we narrowed the mode when dropping the subreg, then we lose. */
8134 if (GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (mode))
8135 new_rtx = NULL;
8137 /* If that didn't give anything, see if the AND simplifies on
8138 its own. */
8139 if (!new_rtx && i >= 0)
8141 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
8142 new_rtx = make_extraction (mode, new_rtx, 0, NULL_RTX, i, 1,
8143 0, in_code == COMPARE);
8146 /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)). */
8147 else if ((GET_CODE (XEXP (x, 0)) == XOR
8148 || GET_CODE (XEXP (x, 0)) == IOR)
8149 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
8150 && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
8151 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
8153 /* Apply the distributive law, and then try to make extractions. */
8154 new_rtx = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
8155 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
8156 XEXP (x, 1)),
8157 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
8158 XEXP (x, 1)));
8159 new_rtx = make_compound_operation (new_rtx, in_code);
8162 /* If we are have (and (rotate X C) M) and C is larger than the number
8163 of bits in M, this is an extraction. */
8165 else if (GET_CODE (XEXP (x, 0)) == ROTATE
8166 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8167 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0
8168 && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
8170 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
8171 new_rtx = make_extraction (mode, new_rtx,
8172 (GET_MODE_PRECISION (mode)
8173 - INTVAL (XEXP (XEXP (x, 0), 1))),
8174 NULL_RTX, i, 1, 0, in_code == COMPARE);
8177 /* On machines without logical shifts, if the operand of the AND is
8178 a logical shift and our mask turns off all the propagated sign
8179 bits, we can replace the logical shift with an arithmetic shift. */
8180 else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8181 && !have_insn_for (LSHIFTRT, mode)
8182 && have_insn_for (ASHIFTRT, mode)
8183 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8184 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8185 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
8186 && mode_width <= HOST_BITS_PER_WIDE_INT)
8188 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
8190 mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
8191 if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
8192 SUBST (XEXP (x, 0),
8193 gen_rtx_ASHIFTRT (mode,
8194 make_compound_operation (XEXP (XEXP (x,
8197 next_code),
8198 XEXP (XEXP (x, 0), 1)));
8201 /* If the constant is one less than a power of two, this might be
8202 representable by an extraction even if no shift is present.
8203 If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
8204 we are in a COMPARE. */
8205 else if ((i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
8206 new_rtx = make_extraction (mode,
8207 make_compound_operation (XEXP (x, 0),
8208 next_code),
8209 0, NULL_RTX, i, 1, 0, in_code == COMPARE);
8211 /* If we are in a comparison and this is an AND with a power of two,
8212 convert this into the appropriate bit extract. */
8213 else if (in_code == COMPARE
8214 && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0
8215 && (equality_comparison || i < GET_MODE_PRECISION (mode) - 1))
8216 new_rtx = make_extraction (mode,
8217 make_compound_operation (XEXP (x, 0),
8218 next_code),
8219 i, NULL_RTX, 1, 1, 0, 1);
8221 /* If the one operand is a paradoxical subreg of a register or memory and
8222 the constant (limited to the smaller mode) has only zero bits where
8223 the sub expression has known zero bits, this can be expressed as
8224 a zero_extend. */
8225 else if (GET_CODE (XEXP (x, 0)) == SUBREG)
8227 rtx sub;
8229 sub = XEXP (XEXP (x, 0), 0);
8230 machine_mode sub_mode = GET_MODE (sub);
8231 int sub_width;
8232 if ((REG_P (sub) || MEM_P (sub))
8233 && GET_MODE_PRECISION (sub_mode).is_constant (&sub_width)
8234 && sub_width < mode_width)
8236 unsigned HOST_WIDE_INT mode_mask = GET_MODE_MASK (sub_mode);
8237 unsigned HOST_WIDE_INT mask;
8239 /* original AND constant with all the known zero bits set */
8240 mask = UINTVAL (XEXP (x, 1)) | (~nonzero_bits (sub, sub_mode));
8241 if ((mask & mode_mask) == mode_mask)
8243 new_rtx = make_compound_operation (sub, next_code);
8244 new_rtx = make_extraction (mode, new_rtx, 0, 0, sub_width,
8245 1, 0, in_code == COMPARE);
8250 break;
8252 case LSHIFTRT:
8253 /* If the sign bit is known to be zero, replace this with an
8254 arithmetic shift. */
8255 if (have_insn_for (ASHIFTRT, mode)
8256 && ! have_insn_for (LSHIFTRT, mode)
8257 && mode_width <= HOST_BITS_PER_WIDE_INT
8258 && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
8260 new_rtx = gen_rtx_ASHIFTRT (mode,
8261 make_compound_operation (XEXP (x, 0),
8262 next_code),
8263 XEXP (x, 1));
8264 break;
8267 /* fall through */
8269 case ASHIFTRT:
8270 lhs = XEXP (x, 0);
8271 rhs = XEXP (x, 1);
8273 /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
8274 this is a SIGN_EXTRACT. */
8275 if (CONST_INT_P (rhs)
8276 && GET_CODE (lhs) == ASHIFT
8277 && CONST_INT_P (XEXP (lhs, 1))
8278 && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1))
8279 && INTVAL (XEXP (lhs, 1)) >= 0
8280 && INTVAL (rhs) < mode_width)
8282 new_rtx = make_compound_operation (XEXP (lhs, 0), next_code);
8283 new_rtx = make_extraction (mode, new_rtx,
8284 INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
8285 NULL_RTX, mode_width - INTVAL (rhs),
8286 code == LSHIFTRT, 0, in_code == COMPARE);
8287 break;
8290 /* See if we have operations between an ASHIFTRT and an ASHIFT.
8291 If so, try to merge the shifts into a SIGN_EXTEND. We could
8292 also do this for some cases of SIGN_EXTRACT, but it doesn't
8293 seem worth the effort; the case checked for occurs on Alpha. */
8295 if (!OBJECT_P (lhs)
8296 && ! (GET_CODE (lhs) == SUBREG
8297 && (OBJECT_P (SUBREG_REG (lhs))))
8298 && CONST_INT_P (rhs)
8299 && INTVAL (rhs) >= 0
8300 && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
8301 && INTVAL (rhs) < mode_width
8302 && (new_rtx = extract_left_shift (mode, lhs, INTVAL (rhs))) != 0)
8303 new_rtx = make_extraction (mode, make_compound_operation (new_rtx,
8304 next_code),
8305 0, NULL_RTX, mode_width - INTVAL (rhs),
8306 code == LSHIFTRT, 0, in_code == COMPARE);
8308 break;
8310 case SUBREG:
8311 /* Call ourselves recursively on the inner expression. If we are
8312 narrowing the object and it has a different RTL code from
8313 what it originally did, do this SUBREG as a force_to_mode. */
8315 rtx inner = SUBREG_REG (x), simplified;
8316 enum rtx_code subreg_code = in_code;
8318 /* If the SUBREG is masking of a logical right shift,
8319 make an extraction. */
8320 if (GET_CODE (inner) == LSHIFTRT
8321 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
8322 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (inner_mode)
8323 && CONST_INT_P (XEXP (inner, 1))
8324 && UINTVAL (XEXP (inner, 1)) < GET_MODE_PRECISION (inner_mode)
8325 && subreg_lowpart_p (x))
8327 new_rtx = make_compound_operation (XEXP (inner, 0), next_code);
8328 int width = GET_MODE_PRECISION (inner_mode)
8329 - INTVAL (XEXP (inner, 1));
8330 if (width > mode_width)
8331 width = mode_width;
8332 new_rtx = make_extraction (mode, new_rtx, 0, XEXP (inner, 1),
8333 width, 1, 0, in_code == COMPARE);
8334 break;
8337 /* If in_code is COMPARE, it isn't always safe to pass it through
8338 to the recursive make_compound_operation call. */
8339 if (subreg_code == COMPARE
8340 && (!subreg_lowpart_p (x)
8341 || GET_CODE (inner) == SUBREG
8342 /* (subreg:SI (and:DI (reg:DI) (const_int 0x800000000)) 0)
8343 is (const_int 0), rather than
8344 (subreg:SI (lshiftrt:DI (reg:DI) (const_int 35)) 0).
8345 Similarly (subreg:QI (and:SI (reg:SI) (const_int 0x80)) 0)
8346 for non-equality comparisons against 0 is not equivalent
8347 to (subreg:QI (lshiftrt:SI (reg:SI) (const_int 7)) 0). */
8348 || (GET_CODE (inner) == AND
8349 && CONST_INT_P (XEXP (inner, 1))
8350 && partial_subreg_p (x)
8351 && exact_log2 (UINTVAL (XEXP (inner, 1)))
8352 >= GET_MODE_BITSIZE (mode) - 1)))
8353 subreg_code = SET;
8355 tem = make_compound_operation (inner, subreg_code);
8357 simplified
8358 = simplify_subreg (mode, tem, GET_MODE (inner), SUBREG_BYTE (x));
8359 if (simplified)
8360 tem = simplified;
8362 if (GET_CODE (tem) != GET_CODE (inner)
8363 && partial_subreg_p (x)
8364 && subreg_lowpart_p (x))
8366 rtx newer
8367 = force_to_mode (tem, mode, HOST_WIDE_INT_M1U, 0);
8369 /* If we have something other than a SUBREG, we might have
8370 done an expansion, so rerun ourselves. */
8371 if (GET_CODE (newer) != SUBREG)
8372 newer = make_compound_operation (newer, in_code);
8374 /* force_to_mode can expand compounds. If it just re-expanded
8375 the compound, use gen_lowpart to convert to the desired
8376 mode. */
8377 if (rtx_equal_p (newer, x)
8378 /* Likewise if it re-expanded the compound only partially.
8379 This happens for SUBREG of ZERO_EXTRACT if they extract
8380 the same number of bits. */
8381 || (GET_CODE (newer) == SUBREG
8382 && (GET_CODE (SUBREG_REG (newer)) == LSHIFTRT
8383 || GET_CODE (SUBREG_REG (newer)) == ASHIFTRT)
8384 && GET_CODE (inner) == AND
8385 && rtx_equal_p (SUBREG_REG (newer), XEXP (inner, 0))))
8386 return gen_lowpart (GET_MODE (x), tem);
8388 return newer;
8391 if (simplified)
8392 return tem;
8394 break;
8396 default:
8397 break;
8400 if (new_rtx)
8401 *x_ptr = gen_lowpart (mode, new_rtx);
8402 *next_code_ptr = next_code;
8403 return NULL_RTX;
8406 /* Look at the expression rooted at X. Look for expressions
8407 equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
8408 Form these expressions.
8410 Return the new rtx, usually just X.
8412 Also, for machines like the VAX that don't have logical shift insns,
8413 try to convert logical to arithmetic shift operations in cases where
8414 they are equivalent. This undoes the canonicalizations to logical
8415 shifts done elsewhere.
8417 We try, as much as possible, to re-use rtl expressions to save memory.
8419 IN_CODE says what kind of expression we are processing. Normally, it is
8420 SET. In a memory address it is MEM. When processing the arguments of
8421 a comparison or a COMPARE against zero, it is COMPARE, or EQ if more
8422 precisely it is an equality comparison against zero. */
8425 make_compound_operation (rtx x, enum rtx_code in_code)
8427 enum rtx_code code = GET_CODE (x);
8428 const char *fmt;
8429 int i, j;
8430 enum rtx_code next_code;
8431 rtx new_rtx, tem;
8433 /* Select the code to be used in recursive calls. Once we are inside an
8434 address, we stay there. If we have a comparison, set to COMPARE,
8435 but once inside, go back to our default of SET. */
8437 next_code = (code == MEM ? MEM
8438 : ((code == COMPARE || COMPARISON_P (x))
8439 && XEXP (x, 1) == const0_rtx) ? COMPARE
8440 : in_code == COMPARE || in_code == EQ ? SET : in_code);
8442 scalar_int_mode mode;
8443 if (is_a <scalar_int_mode> (GET_MODE (x), &mode))
8445 rtx new_rtx = make_compound_operation_int (mode, &x, in_code,
8446 &next_code);
8447 if (new_rtx)
8448 return new_rtx;
8449 code = GET_CODE (x);
8452 /* Now recursively process each operand of this operation. We need to
8453 handle ZERO_EXTEND specially so that we don't lose track of the
8454 inner mode. */
8455 if (code == ZERO_EXTEND)
8457 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
8458 tem = simplify_const_unary_operation (ZERO_EXTEND, GET_MODE (x),
8459 new_rtx, GET_MODE (XEXP (x, 0)));
8460 if (tem)
8461 return tem;
8462 SUBST (XEXP (x, 0), new_rtx);
8463 return x;
8466 fmt = GET_RTX_FORMAT (code);
8467 for (i = 0; i < GET_RTX_LENGTH (code); i++)
8468 if (fmt[i] == 'e')
8470 new_rtx = make_compound_operation (XEXP (x, i), next_code);
8471 SUBST (XEXP (x, i), new_rtx);
8473 else if (fmt[i] == 'E')
8474 for (j = 0; j < XVECLEN (x, i); j++)
8476 new_rtx = make_compound_operation (XVECEXP (x, i, j), next_code);
8477 SUBST (XVECEXP (x, i, j), new_rtx);
8480 maybe_swap_commutative_operands (x);
8481 return x;
8484 /* Given M see if it is a value that would select a field of bits
8485 within an item, but not the entire word. Return -1 if not.
8486 Otherwise, return the starting position of the field, where 0 is the
8487 low-order bit.
8489 *PLEN is set to the length of the field. */
8491 static int
8492 get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
8494 /* Get the bit number of the first 1 bit from the right, -1 if none. */
8495 int pos = m ? ctz_hwi (m) : -1;
8496 int len = 0;
8498 if (pos >= 0)
8499 /* Now shift off the low-order zero bits and see if we have a
8500 power of two minus 1. */
8501 len = exact_log2 ((m >> pos) + 1);
8503 if (len <= 0)
8504 pos = -1;
8506 *plen = len;
8507 return pos;
8510 /* If X refers to a register that equals REG in value, replace these
8511 references with REG. */
8512 static rtx
8513 canon_reg_for_combine (rtx x, rtx reg)
8515 rtx op0, op1, op2;
8516 const char *fmt;
8517 int i;
8518 bool copied;
8520 enum rtx_code code = GET_CODE (x);
8521 switch (GET_RTX_CLASS (code))
8523 case RTX_UNARY:
8524 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8525 if (op0 != XEXP (x, 0))
8526 return simplify_gen_unary (GET_CODE (x), GET_MODE (x), op0,
8527 GET_MODE (reg));
8528 break;
8530 case RTX_BIN_ARITH:
8531 case RTX_COMM_ARITH:
8532 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8533 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8534 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8535 return simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
8536 break;
8538 case RTX_COMPARE:
8539 case RTX_COMM_COMPARE:
8540 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8541 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8542 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8543 return simplify_gen_relational (GET_CODE (x), GET_MODE (x),
8544 GET_MODE (op0), op0, op1);
8545 break;
8547 case RTX_TERNARY:
8548 case RTX_BITFIELD_OPS:
8549 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8550 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8551 op2 = canon_reg_for_combine (XEXP (x, 2), reg);
8552 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1) || op2 != XEXP (x, 2))
8553 return simplify_gen_ternary (GET_CODE (x), GET_MODE (x),
8554 GET_MODE (op0), op0, op1, op2);
8555 /* FALLTHRU */
8557 case RTX_OBJ:
8558 if (REG_P (x))
8560 if (rtx_equal_p (get_last_value (reg), x)
8561 || rtx_equal_p (reg, get_last_value (x)))
8562 return reg;
8563 else
8564 break;
8567 /* fall through */
8569 default:
8570 fmt = GET_RTX_FORMAT (code);
8571 copied = false;
8572 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8573 if (fmt[i] == 'e')
8575 rtx op = canon_reg_for_combine (XEXP (x, i), reg);
8576 if (op != XEXP (x, i))
8578 if (!copied)
8580 copied = true;
8581 x = copy_rtx (x);
8583 XEXP (x, i) = op;
8586 else if (fmt[i] == 'E')
8588 int j;
8589 for (j = 0; j < XVECLEN (x, i); j++)
8591 rtx op = canon_reg_for_combine (XVECEXP (x, i, j), reg);
8592 if (op != XVECEXP (x, i, j))
8594 if (!copied)
8596 copied = true;
8597 x = copy_rtx (x);
8599 XVECEXP (x, i, j) = op;
8604 break;
8607 return x;
8610 /* Return X converted to MODE. If the value is already truncated to
8611 MODE we can just return a subreg even though in the general case we
8612 would need an explicit truncation. */
8614 static rtx
8615 gen_lowpart_or_truncate (machine_mode mode, rtx x)
8617 if (!CONST_INT_P (x)
8618 && partial_subreg_p (mode, GET_MODE (x))
8619 && !TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (x))
8620 && !(REG_P (x) && reg_truncated_to_mode (mode, x)))
8622 /* Bit-cast X into an integer mode. */
8623 if (!SCALAR_INT_MODE_P (GET_MODE (x)))
8624 x = gen_lowpart (int_mode_for_mode (GET_MODE (x)).require (), x);
8625 x = simplify_gen_unary (TRUNCATE, int_mode_for_mode (mode).require (),
8626 x, GET_MODE (x));
8629 return gen_lowpart (mode, x);
8632 /* See if X can be simplified knowing that we will only refer to it in
8633 MODE and will only refer to those bits that are nonzero in MASK.
8634 If other bits are being computed or if masking operations are done
8635 that select a superset of the bits in MASK, they can sometimes be
8636 ignored.
8638 Return a possibly simplified expression, but always convert X to
8639 MODE. If X is a CONST_INT, AND the CONST_INT with MASK.
8641 If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
8642 are all off in X. This is used when X will be complemented, by either
8643 NOT, NEG, or XOR. */
8645 static rtx
8646 force_to_mode (rtx x, machine_mode mode, unsigned HOST_WIDE_INT mask,
8647 int just_select)
8649 enum rtx_code code = GET_CODE (x);
8650 int next_select = just_select || code == XOR || code == NOT || code == NEG;
8651 machine_mode op_mode;
8652 unsigned HOST_WIDE_INT nonzero;
8654 /* If this is a CALL or ASM_OPERANDS, don't do anything. Some of the
8655 code below will do the wrong thing since the mode of such an
8656 expression is VOIDmode.
8658 Also do nothing if X is a CLOBBER; this can happen if X was
8659 the return value from a call to gen_lowpart. */
8660 if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
8661 return x;
8663 /* We want to perform the operation in its present mode unless we know
8664 that the operation is valid in MODE, in which case we do the operation
8665 in MODE. */
8666 op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
8667 && have_insn_for (code, mode))
8668 ? mode : GET_MODE (x));
8670 /* It is not valid to do a right-shift in a narrower mode
8671 than the one it came in with. */
8672 if ((code == LSHIFTRT || code == ASHIFTRT)
8673 && partial_subreg_p (mode, GET_MODE (x)))
8674 op_mode = GET_MODE (x);
8676 /* Truncate MASK to fit OP_MODE. */
8677 if (op_mode)
8678 mask &= GET_MODE_MASK (op_mode);
8680 /* Determine what bits of X are guaranteed to be (non)zero. */
8681 nonzero = nonzero_bits (x, mode);
8683 /* If none of the bits in X are needed, return a zero. */
8684 if (!just_select && (nonzero & mask) == 0 && !side_effects_p (x))
8685 x = const0_rtx;
8687 /* If X is a CONST_INT, return a new one. Do this here since the
8688 test below will fail. */
8689 if (CONST_INT_P (x))
8691 if (SCALAR_INT_MODE_P (mode))
8692 return gen_int_mode (INTVAL (x) & mask, mode);
8693 else
8695 x = GEN_INT (INTVAL (x) & mask);
8696 return gen_lowpart_common (mode, x);
8700 /* If X is narrower than MODE and we want all the bits in X's mode, just
8701 get X in the proper mode. */
8702 if (paradoxical_subreg_p (mode, GET_MODE (x))
8703 && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
8704 return gen_lowpart (mode, x);
8706 /* We can ignore the effect of a SUBREG if it narrows the mode or
8707 if the constant masks to zero all the bits the mode doesn't have. */
8708 if (GET_CODE (x) == SUBREG
8709 && subreg_lowpart_p (x)
8710 && (partial_subreg_p (x)
8711 || (mask
8712 & GET_MODE_MASK (GET_MODE (x))
8713 & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))) == 0))
8714 return force_to_mode (SUBREG_REG (x), mode, mask, next_select);
8716 scalar_int_mode int_mode, xmode;
8717 if (is_a <scalar_int_mode> (mode, &int_mode)
8718 && is_a <scalar_int_mode> (GET_MODE (x), &xmode))
8719 /* OP_MODE is either MODE or XMODE, so it must be a scalar
8720 integer too. */
8721 return force_int_to_mode (x, int_mode, xmode,
8722 as_a <scalar_int_mode> (op_mode),
8723 mask, just_select);
8725 return gen_lowpart_or_truncate (mode, x);
8728 /* Subroutine of force_to_mode that handles cases in which both X and
8729 the result are scalar integers. MODE is the mode of the result,
8730 XMODE is the mode of X, and OP_MODE says which of MODE or XMODE
8731 is preferred for simplified versions of X. The other arguments
8732 are as for force_to_mode. */
8734 static rtx
8735 force_int_to_mode (rtx x, scalar_int_mode mode, scalar_int_mode xmode,
8736 scalar_int_mode op_mode, unsigned HOST_WIDE_INT mask,
8737 int just_select)
8739 enum rtx_code code = GET_CODE (x);
8740 int next_select = just_select || code == XOR || code == NOT || code == NEG;
8741 unsigned HOST_WIDE_INT fuller_mask;
8742 rtx op0, op1, temp;
8743 poly_int64 const_op0;
8745 /* When we have an arithmetic operation, or a shift whose count we
8746 do not know, we need to assume that all bits up to the highest-order
8747 bit in MASK will be needed. This is how we form such a mask. */
8748 if (mask & (HOST_WIDE_INT_1U << (HOST_BITS_PER_WIDE_INT - 1)))
8749 fuller_mask = HOST_WIDE_INT_M1U;
8750 else
8751 fuller_mask = ((HOST_WIDE_INT_1U << (floor_log2 (mask) + 1))
8752 - 1);
8754 switch (code)
8756 case CLOBBER:
8757 /* If X is a (clobber (const_int)), return it since we know we are
8758 generating something that won't match. */
8759 return x;
8761 case SIGN_EXTEND:
8762 case ZERO_EXTEND:
8763 case ZERO_EXTRACT:
8764 case SIGN_EXTRACT:
8765 x = expand_compound_operation (x);
8766 if (GET_CODE (x) != code)
8767 return force_to_mode (x, mode, mask, next_select);
8768 break;
8770 case TRUNCATE:
8771 /* Similarly for a truncate. */
8772 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8774 case AND:
8775 /* If this is an AND with a constant, convert it into an AND
8776 whose constant is the AND of that constant with MASK. If it
8777 remains an AND of MASK, delete it since it is redundant. */
8779 if (CONST_INT_P (XEXP (x, 1)))
8781 x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
8782 mask & INTVAL (XEXP (x, 1)));
8783 xmode = op_mode;
8785 /* If X is still an AND, see if it is an AND with a mask that
8786 is just some low-order bits. If so, and it is MASK, we don't
8787 need it. */
8789 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8790 && (INTVAL (XEXP (x, 1)) & GET_MODE_MASK (xmode)) == mask)
8791 x = XEXP (x, 0);
8793 /* If it remains an AND, try making another AND with the bits
8794 in the mode mask that aren't in MASK turned on. If the
8795 constant in the AND is wide enough, this might make a
8796 cheaper constant. */
8798 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8799 && GET_MODE_MASK (xmode) != mask
8800 && HWI_COMPUTABLE_MODE_P (xmode))
8802 unsigned HOST_WIDE_INT cval
8803 = UINTVAL (XEXP (x, 1)) | (GET_MODE_MASK (xmode) & ~mask);
8804 rtx y;
8806 y = simplify_gen_binary (AND, xmode, XEXP (x, 0),
8807 gen_int_mode (cval, xmode));
8808 if (set_src_cost (y, xmode, optimize_this_for_speed_p)
8809 < set_src_cost (x, xmode, optimize_this_for_speed_p))
8810 x = y;
8813 break;
8816 goto binop;
8818 case PLUS:
8819 /* In (and (plus FOO C1) M), if M is a mask that just turns off
8820 low-order bits (as in an alignment operation) and FOO is already
8821 aligned to that boundary, mask C1 to that boundary as well.
8822 This may eliminate that PLUS and, later, the AND. */
8825 unsigned int width = GET_MODE_PRECISION (mode);
8826 unsigned HOST_WIDE_INT smask = mask;
8828 /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
8829 number, sign extend it. */
8831 if (width < HOST_BITS_PER_WIDE_INT
8832 && (smask & (HOST_WIDE_INT_1U << (width - 1))) != 0)
8833 smask |= HOST_WIDE_INT_M1U << width;
8835 if (CONST_INT_P (XEXP (x, 1))
8836 && pow2p_hwi (- smask)
8837 && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
8838 && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
8839 return force_to_mode (plus_constant (xmode, XEXP (x, 0),
8840 (INTVAL (XEXP (x, 1)) & smask)),
8841 mode, smask, next_select);
8844 /* fall through */
8846 case MULT:
8847 /* Substituting into the operands of a widening MULT is not likely to
8848 create RTL matching a machine insn. */
8849 if (code == MULT
8850 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
8851 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND)
8852 && (GET_CODE (XEXP (x, 1)) == ZERO_EXTEND
8853 || GET_CODE (XEXP (x, 1)) == SIGN_EXTEND)
8854 && REG_P (XEXP (XEXP (x, 0), 0))
8855 && REG_P (XEXP (XEXP (x, 1), 0)))
8856 return gen_lowpart_or_truncate (mode, x);
8858 /* For PLUS, MINUS and MULT, we need any bits less significant than the
8859 most significant bit in MASK since carries from those bits will
8860 affect the bits we are interested in. */
8861 mask = fuller_mask;
8862 goto binop;
8864 case MINUS:
8865 /* If X is (minus C Y) where C's least set bit is larger than any bit
8866 in the mask, then we may replace with (neg Y). */
8867 if (poly_int_rtx_p (XEXP (x, 0), &const_op0)
8868 && (unsigned HOST_WIDE_INT) known_alignment (const_op0) > mask)
8870 x = simplify_gen_unary (NEG, xmode, XEXP (x, 1), xmode);
8871 return force_to_mode (x, mode, mask, next_select);
8874 /* Similarly, if C contains every bit in the fuller_mask, then we may
8875 replace with (not Y). */
8876 if (CONST_INT_P (XEXP (x, 0))
8877 && ((UINTVAL (XEXP (x, 0)) | fuller_mask) == UINTVAL (XEXP (x, 0))))
8879 x = simplify_gen_unary (NOT, xmode, XEXP (x, 1), xmode);
8880 return force_to_mode (x, mode, mask, next_select);
8883 mask = fuller_mask;
8884 goto binop;
8886 case IOR:
8887 case XOR:
8888 /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
8889 LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
8890 operation which may be a bitfield extraction. Ensure that the
8891 constant we form is not wider than the mode of X. */
8893 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8894 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8895 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8896 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
8897 && CONST_INT_P (XEXP (x, 1))
8898 && ((INTVAL (XEXP (XEXP (x, 0), 1))
8899 + floor_log2 (INTVAL (XEXP (x, 1))))
8900 < GET_MODE_PRECISION (xmode))
8901 && (UINTVAL (XEXP (x, 1))
8902 & ~nonzero_bits (XEXP (x, 0), xmode)) == 0)
8904 temp = gen_int_mode ((INTVAL (XEXP (x, 1)) & mask)
8905 << INTVAL (XEXP (XEXP (x, 0), 1)),
8906 xmode);
8907 temp = simplify_gen_binary (GET_CODE (x), xmode,
8908 XEXP (XEXP (x, 0), 0), temp);
8909 x = simplify_gen_binary (LSHIFTRT, xmode, temp,
8910 XEXP (XEXP (x, 0), 1));
8911 return force_to_mode (x, mode, mask, next_select);
8914 binop:
8915 /* For most binary operations, just propagate into the operation and
8916 change the mode if we have an operation of that mode. */
8918 op0 = force_to_mode (XEXP (x, 0), mode, mask, next_select);
8919 op1 = force_to_mode (XEXP (x, 1), mode, mask, next_select);
8921 /* If we ended up truncating both operands, truncate the result of the
8922 operation instead. */
8923 if (GET_CODE (op0) == TRUNCATE
8924 && GET_CODE (op1) == TRUNCATE)
8926 op0 = XEXP (op0, 0);
8927 op1 = XEXP (op1, 0);
8930 op0 = gen_lowpart_or_truncate (op_mode, op0);
8931 op1 = gen_lowpart_or_truncate (op_mode, op1);
8933 if (op_mode != xmode || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8935 x = simplify_gen_binary (code, op_mode, op0, op1);
8936 xmode = op_mode;
8938 break;
8940 case ASHIFT:
8941 /* For left shifts, do the same, but just for the first operand.
8942 However, we cannot do anything with shifts where we cannot
8943 guarantee that the counts are smaller than the size of the mode
8944 because such a count will have a different meaning in a
8945 wider mode. */
8947 if (! (CONST_INT_P (XEXP (x, 1))
8948 && INTVAL (XEXP (x, 1)) >= 0
8949 && INTVAL (XEXP (x, 1)) < GET_MODE_PRECISION (mode))
8950 && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
8951 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
8952 < (unsigned HOST_WIDE_INT) GET_MODE_PRECISION (mode))))
8953 break;
8955 /* If the shift count is a constant and we can do arithmetic in
8956 the mode of the shift, refine which bits we need. Otherwise, use the
8957 conservative form of the mask. */
8958 if (CONST_INT_P (XEXP (x, 1))
8959 && INTVAL (XEXP (x, 1)) >= 0
8960 && INTVAL (XEXP (x, 1)) < GET_MODE_PRECISION (op_mode)
8961 && HWI_COMPUTABLE_MODE_P (op_mode))
8962 mask >>= INTVAL (XEXP (x, 1));
8963 else
8964 mask = fuller_mask;
8966 op0 = gen_lowpart_or_truncate (op_mode,
8967 force_to_mode (XEXP (x, 0), mode,
8968 mask, next_select));
8970 if (op_mode != xmode || op0 != XEXP (x, 0))
8972 x = simplify_gen_binary (code, op_mode, op0, XEXP (x, 1));
8973 xmode = op_mode;
8975 break;
8977 case LSHIFTRT:
8978 /* Here we can only do something if the shift count is a constant,
8979 this shift constant is valid for the host, and we can do arithmetic
8980 in OP_MODE. */
8982 if (CONST_INT_P (XEXP (x, 1))
8983 && INTVAL (XEXP (x, 1)) >= 0
8984 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
8985 && HWI_COMPUTABLE_MODE_P (op_mode))
8987 rtx inner = XEXP (x, 0);
8988 unsigned HOST_WIDE_INT inner_mask;
8990 /* Select the mask of the bits we need for the shift operand. */
8991 inner_mask = mask << INTVAL (XEXP (x, 1));
8993 /* We can only change the mode of the shift if we can do arithmetic
8994 in the mode of the shift and INNER_MASK is no wider than the
8995 width of X's mode. */
8996 if ((inner_mask & ~GET_MODE_MASK (xmode)) != 0)
8997 op_mode = xmode;
8999 inner = force_to_mode (inner, op_mode, inner_mask, next_select);
9001 if (xmode != op_mode || inner != XEXP (x, 0))
9003 x = simplify_gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
9004 xmode = op_mode;
9008 /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
9009 shift and AND produces only copies of the sign bit (C2 is one less
9010 than a power of two), we can do this with just a shift. */
9012 if (GET_CODE (x) == LSHIFTRT
9013 && CONST_INT_P (XEXP (x, 1))
9014 /* The shift puts one of the sign bit copies in the least significant
9015 bit. */
9016 && ((INTVAL (XEXP (x, 1))
9017 + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
9018 >= GET_MODE_PRECISION (xmode))
9019 && pow2p_hwi (mask + 1)
9020 /* Number of bits left after the shift must be more than the mask
9021 needs. */
9022 && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
9023 <= GET_MODE_PRECISION (xmode))
9024 /* Must be more sign bit copies than the mask needs. */
9025 && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
9026 >= exact_log2 (mask + 1)))
9028 int nbits = GET_MODE_PRECISION (xmode) - exact_log2 (mask + 1);
9029 x = simplify_gen_binary (LSHIFTRT, xmode, XEXP (x, 0),
9030 gen_int_shift_amount (xmode, nbits));
9032 goto shiftrt;
9034 case ASHIFTRT:
9035 /* If we are just looking for the sign bit, we don't need this shift at
9036 all, even if it has a variable count. */
9037 if (val_signbit_p (xmode, mask))
9038 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
9040 /* If this is a shift by a constant, get a mask that contains those bits
9041 that are not copies of the sign bit. We then have two cases: If
9042 MASK only includes those bits, this can be a logical shift, which may
9043 allow simplifications. If MASK is a single-bit field not within
9044 those bits, we are requesting a copy of the sign bit and hence can
9045 shift the sign bit to the appropriate location. */
9047 if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) >= 0
9048 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
9050 unsigned HOST_WIDE_INT nonzero;
9051 int i;
9053 /* If the considered data is wider than HOST_WIDE_INT, we can't
9054 represent a mask for all its bits in a single scalar.
9055 But we only care about the lower bits, so calculate these. */
9057 if (GET_MODE_PRECISION (xmode) > HOST_BITS_PER_WIDE_INT)
9059 nonzero = HOST_WIDE_INT_M1U;
9061 /* GET_MODE_PRECISION (GET_MODE (x)) - INTVAL (XEXP (x, 1))
9062 is the number of bits a full-width mask would have set.
9063 We need only shift if these are fewer than nonzero can
9064 hold. If not, we must keep all bits set in nonzero. */
9066 if (GET_MODE_PRECISION (xmode) - INTVAL (XEXP (x, 1))
9067 < HOST_BITS_PER_WIDE_INT)
9068 nonzero >>= INTVAL (XEXP (x, 1))
9069 + HOST_BITS_PER_WIDE_INT
9070 - GET_MODE_PRECISION (xmode);
9072 else
9074 nonzero = GET_MODE_MASK (xmode);
9075 nonzero >>= INTVAL (XEXP (x, 1));
9078 if ((mask & ~nonzero) == 0)
9080 x = simplify_shift_const (NULL_RTX, LSHIFTRT, xmode,
9081 XEXP (x, 0), INTVAL (XEXP (x, 1)));
9082 if (GET_CODE (x) != ASHIFTRT)
9083 return force_to_mode (x, mode, mask, next_select);
9086 else if ((i = exact_log2 (mask)) >= 0)
9088 x = simplify_shift_const
9089 (NULL_RTX, LSHIFTRT, xmode, XEXP (x, 0),
9090 GET_MODE_PRECISION (xmode) - 1 - i);
9092 if (GET_CODE (x) != ASHIFTRT)
9093 return force_to_mode (x, mode, mask, next_select);
9097 /* If MASK is 1, convert this to an LSHIFTRT. This can be done
9098 even if the shift count isn't a constant. */
9099 if (mask == 1)
9100 x = simplify_gen_binary (LSHIFTRT, xmode, XEXP (x, 0), XEXP (x, 1));
9102 shiftrt:
9104 /* If this is a zero- or sign-extension operation that just affects bits
9105 we don't care about, remove it. Be sure the call above returned
9106 something that is still a shift. */
9108 if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
9109 && CONST_INT_P (XEXP (x, 1))
9110 && INTVAL (XEXP (x, 1)) >= 0
9111 && (INTVAL (XEXP (x, 1))
9112 <= GET_MODE_PRECISION (xmode) - (floor_log2 (mask) + 1))
9113 && GET_CODE (XEXP (x, 0)) == ASHIFT
9114 && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
9115 return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
9116 next_select);
9118 break;
9120 case ROTATE:
9121 case ROTATERT:
9122 /* If the shift count is constant and we can do computations
9123 in the mode of X, compute where the bits we care about are.
9124 Otherwise, we can't do anything. Don't change the mode of
9125 the shift or propagate MODE into the shift, though. */
9126 if (CONST_INT_P (XEXP (x, 1))
9127 && INTVAL (XEXP (x, 1)) >= 0)
9129 temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
9130 xmode, gen_int_mode (mask, xmode),
9131 XEXP (x, 1));
9132 if (temp && CONST_INT_P (temp))
9133 x = simplify_gen_binary (code, xmode,
9134 force_to_mode (XEXP (x, 0), xmode,
9135 INTVAL (temp), next_select),
9136 XEXP (x, 1));
9138 break;
9140 case NEG:
9141 /* If we just want the low-order bit, the NEG isn't needed since it
9142 won't change the low-order bit. */
9143 if (mask == 1)
9144 return force_to_mode (XEXP (x, 0), mode, mask, just_select);
9146 /* We need any bits less significant than the most significant bit in
9147 MASK since carries from those bits will affect the bits we are
9148 interested in. */
9149 mask = fuller_mask;
9150 goto unop;
9152 case NOT:
9153 /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
9154 same as the XOR case above. Ensure that the constant we form is not
9155 wider than the mode of X. */
9157 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
9158 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
9159 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
9160 && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
9161 < GET_MODE_PRECISION (xmode))
9162 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
9164 temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)), xmode);
9165 temp = simplify_gen_binary (XOR, xmode, XEXP (XEXP (x, 0), 0), temp);
9166 x = simplify_gen_binary (LSHIFTRT, xmode,
9167 temp, XEXP (XEXP (x, 0), 1));
9169 return force_to_mode (x, mode, mask, next_select);
9172 /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
9173 use the full mask inside the NOT. */
9174 mask = fuller_mask;
9176 unop:
9177 op0 = gen_lowpart_or_truncate (op_mode,
9178 force_to_mode (XEXP (x, 0), mode, mask,
9179 next_select));
9180 if (op_mode != xmode || op0 != XEXP (x, 0))
9182 x = simplify_gen_unary (code, op_mode, op0, op_mode);
9183 xmode = op_mode;
9185 break;
9187 case NE:
9188 /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
9189 in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
9190 which is equal to STORE_FLAG_VALUE. */
9191 if ((mask & ~STORE_FLAG_VALUE) == 0
9192 && XEXP (x, 1) == const0_rtx
9193 && GET_MODE (XEXP (x, 0)) == mode
9194 && pow2p_hwi (nonzero_bits (XEXP (x, 0), mode))
9195 && (nonzero_bits (XEXP (x, 0), mode)
9196 == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
9197 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
9199 break;
9201 case IF_THEN_ELSE:
9202 /* We have no way of knowing if the IF_THEN_ELSE can itself be
9203 written in a narrower mode. We play it safe and do not do so. */
9205 op0 = gen_lowpart_or_truncate (xmode,
9206 force_to_mode (XEXP (x, 1), mode,
9207 mask, next_select));
9208 op1 = gen_lowpart_or_truncate (xmode,
9209 force_to_mode (XEXP (x, 2), mode,
9210 mask, next_select));
9211 if (op0 != XEXP (x, 1) || op1 != XEXP (x, 2))
9212 x = simplify_gen_ternary (IF_THEN_ELSE, xmode,
9213 GET_MODE (XEXP (x, 0)), XEXP (x, 0),
9214 op0, op1);
9215 break;
9217 default:
9218 break;
9221 /* Ensure we return a value of the proper mode. */
9222 return gen_lowpart_or_truncate (mode, x);
9225 /* Return nonzero if X is an expression that has one of two values depending on
9226 whether some other value is zero or nonzero. In that case, we return the
9227 value that is being tested, *PTRUE is set to the value if the rtx being
9228 returned has a nonzero value, and *PFALSE is set to the other alternative.
9230 If we return zero, we set *PTRUE and *PFALSE to X. */
9232 static rtx
9233 if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
9235 machine_mode mode = GET_MODE (x);
9236 enum rtx_code code = GET_CODE (x);
9237 rtx cond0, cond1, true0, true1, false0, false1;
9238 unsigned HOST_WIDE_INT nz;
9239 scalar_int_mode int_mode;
9241 /* If we are comparing a value against zero, we are done. */
9242 if ((code == NE || code == EQ)
9243 && XEXP (x, 1) == const0_rtx)
9245 *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
9246 *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
9247 return XEXP (x, 0);
9250 /* If this is a unary operation whose operand has one of two values, apply
9251 our opcode to compute those values. */
9252 else if (UNARY_P (x)
9253 && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
9255 *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
9256 *pfalse = simplify_gen_unary (code, mode, false0,
9257 GET_MODE (XEXP (x, 0)));
9258 return cond0;
9261 /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
9262 make can't possibly match and would suppress other optimizations. */
9263 else if (code == COMPARE)
9266 /* If this is a binary operation, see if either side has only one of two
9267 values. If either one does or if both do and they are conditional on
9268 the same value, compute the new true and false values. */
9269 else if (BINARY_P (x))
9271 rtx op0 = XEXP (x, 0);
9272 rtx op1 = XEXP (x, 1);
9273 cond0 = if_then_else_cond (op0, &true0, &false0);
9274 cond1 = if_then_else_cond (op1, &true1, &false1);
9276 if ((cond0 != 0 && cond1 != 0 && !rtx_equal_p (cond0, cond1))
9277 && (REG_P (op0) || REG_P (op1)))
9279 /* Try to enable a simplification by undoing work done by
9280 if_then_else_cond if it converted a REG into something more
9281 complex. */
9282 if (REG_P (op0))
9284 cond0 = 0;
9285 true0 = false0 = op0;
9287 else
9289 cond1 = 0;
9290 true1 = false1 = op1;
9294 if ((cond0 != 0 || cond1 != 0)
9295 && ! (cond0 != 0 && cond1 != 0 && !rtx_equal_p (cond0, cond1)))
9297 /* If if_then_else_cond returned zero, then true/false are the
9298 same rtl. We must copy one of them to prevent invalid rtl
9299 sharing. */
9300 if (cond0 == 0)
9301 true0 = copy_rtx (true0);
9302 else if (cond1 == 0)
9303 true1 = copy_rtx (true1);
9305 if (COMPARISON_P (x))
9307 *ptrue = simplify_gen_relational (code, mode, VOIDmode,
9308 true0, true1);
9309 *pfalse = simplify_gen_relational (code, mode, VOIDmode,
9310 false0, false1);
9312 else
9314 *ptrue = simplify_gen_binary (code, mode, true0, true1);
9315 *pfalse = simplify_gen_binary (code, mode, false0, false1);
9318 return cond0 ? cond0 : cond1;
9321 /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
9322 operands is zero when the other is nonzero, and vice-versa,
9323 and STORE_FLAG_VALUE is 1 or -1. */
9325 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9326 && (code == PLUS || code == IOR || code == XOR || code == MINUS
9327 || code == UMAX)
9328 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
9330 rtx op0 = XEXP (XEXP (x, 0), 1);
9331 rtx op1 = XEXP (XEXP (x, 1), 1);
9333 cond0 = XEXP (XEXP (x, 0), 0);
9334 cond1 = XEXP (XEXP (x, 1), 0);
9336 if (COMPARISON_P (cond0)
9337 && COMPARISON_P (cond1)
9338 && SCALAR_INT_MODE_P (mode)
9339 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
9340 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
9341 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
9342 || ((swap_condition (GET_CODE (cond0))
9343 == reversed_comparison_code (cond1, NULL))
9344 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
9345 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
9346 && ! side_effects_p (x))
9348 *ptrue = simplify_gen_binary (MULT, mode, op0, const_true_rtx);
9349 *pfalse = simplify_gen_binary (MULT, mode,
9350 (code == MINUS
9351 ? simplify_gen_unary (NEG, mode,
9352 op1, mode)
9353 : op1),
9354 const_true_rtx);
9355 return cond0;
9359 /* Similarly for MULT, AND and UMIN, except that for these the result
9360 is always zero. */
9361 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9362 && (code == MULT || code == AND || code == UMIN)
9363 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
9365 cond0 = XEXP (XEXP (x, 0), 0);
9366 cond1 = XEXP (XEXP (x, 1), 0);
9368 if (COMPARISON_P (cond0)
9369 && COMPARISON_P (cond1)
9370 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
9371 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
9372 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
9373 || ((swap_condition (GET_CODE (cond0))
9374 == reversed_comparison_code (cond1, NULL))
9375 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
9376 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
9377 && ! side_effects_p (x))
9379 *ptrue = *pfalse = const0_rtx;
9380 return cond0;
9385 else if (code == IF_THEN_ELSE)
9387 /* If we have IF_THEN_ELSE already, extract the condition and
9388 canonicalize it if it is NE or EQ. */
9389 cond0 = XEXP (x, 0);
9390 *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
9391 if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
9392 return XEXP (cond0, 0);
9393 else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
9395 *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
9396 return XEXP (cond0, 0);
9398 else
9399 return cond0;
9402 /* If X is a SUBREG, we can narrow both the true and false values
9403 if the inner expression, if there is a condition. */
9404 else if (code == SUBREG
9405 && (cond0 = if_then_else_cond (SUBREG_REG (x), &true0,
9406 &false0)) != 0)
9408 true0 = simplify_gen_subreg (mode, true0,
9409 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
9410 false0 = simplify_gen_subreg (mode, false0,
9411 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
9412 if (true0 && false0)
9414 *ptrue = true0;
9415 *pfalse = false0;
9416 return cond0;
9420 /* If X is a constant, this isn't special and will cause confusions
9421 if we treat it as such. Likewise if it is equivalent to a constant. */
9422 else if (CONSTANT_P (x)
9423 || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
9426 /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
9427 will be least confusing to the rest of the compiler. */
9428 else if (mode == BImode)
9430 *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
9431 return x;
9434 /* If X is known to be either 0 or -1, those are the true and
9435 false values when testing X. */
9436 else if (x == constm1_rtx || x == const0_rtx
9437 || (is_a <scalar_int_mode> (mode, &int_mode)
9438 && (num_sign_bit_copies (x, int_mode)
9439 == GET_MODE_PRECISION (int_mode))))
9441 *ptrue = constm1_rtx, *pfalse = const0_rtx;
9442 return x;
9445 /* Likewise for 0 or a single bit. */
9446 else if (HWI_COMPUTABLE_MODE_P (mode)
9447 && pow2p_hwi (nz = nonzero_bits (x, mode)))
9449 *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
9450 return x;
9453 /* Otherwise fail; show no condition with true and false values the same. */
9454 *ptrue = *pfalse = x;
9455 return 0;
9458 /* Return the value of expression X given the fact that condition COND
9459 is known to be true when applied to REG as its first operand and VAL
9460 as its second. X is known to not be shared and so can be modified in
9461 place.
9463 We only handle the simplest cases, and specifically those cases that
9464 arise with IF_THEN_ELSE expressions. */
9466 static rtx
9467 known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
9469 enum rtx_code code = GET_CODE (x);
9470 const char *fmt;
9471 int i, j;
9473 if (side_effects_p (x))
9474 return x;
9476 /* If either operand of the condition is a floating point value,
9477 then we have to avoid collapsing an EQ comparison. */
9478 if (cond == EQ
9479 && rtx_equal_p (x, reg)
9480 && ! FLOAT_MODE_P (GET_MODE (x))
9481 && ! FLOAT_MODE_P (GET_MODE (val)))
9482 return val;
9484 if (cond == UNEQ && rtx_equal_p (x, reg))
9485 return val;
9487 /* If X is (abs REG) and we know something about REG's relationship
9488 with zero, we may be able to simplify this. */
9490 if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
9491 switch (cond)
9493 case GE: case GT: case EQ:
9494 return XEXP (x, 0);
9495 case LT: case LE:
9496 return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
9497 XEXP (x, 0),
9498 GET_MODE (XEXP (x, 0)));
9499 default:
9500 break;
9503 /* The only other cases we handle are MIN, MAX, and comparisons if the
9504 operands are the same as REG and VAL. */
9506 else if (COMPARISON_P (x) || COMMUTATIVE_ARITH_P (x))
9508 if (rtx_equal_p (XEXP (x, 0), val))
9510 std::swap (val, reg);
9511 cond = swap_condition (cond);
9514 if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
9516 if (COMPARISON_P (x))
9518 if (comparison_dominates_p (cond, code))
9519 return VECTOR_MODE_P (GET_MODE (x)) ? x : const_true_rtx;
9521 code = reversed_comparison_code (x, NULL);
9522 if (code != UNKNOWN
9523 && comparison_dominates_p (cond, code))
9524 return CONST0_RTX (GET_MODE (x));
9525 else
9526 return x;
9528 else if (code == SMAX || code == SMIN
9529 || code == UMIN || code == UMAX)
9531 int unsignedp = (code == UMIN || code == UMAX);
9533 /* Do not reverse the condition when it is NE or EQ.
9534 This is because we cannot conclude anything about
9535 the value of 'SMAX (x, y)' when x is not equal to y,
9536 but we can when x equals y. */
9537 if ((code == SMAX || code == UMAX)
9538 && ! (cond == EQ || cond == NE))
9539 cond = reverse_condition (cond);
9541 switch (cond)
9543 case GE: case GT:
9544 return unsignedp ? x : XEXP (x, 1);
9545 case LE: case LT:
9546 return unsignedp ? x : XEXP (x, 0);
9547 case GEU: case GTU:
9548 return unsignedp ? XEXP (x, 1) : x;
9549 case LEU: case LTU:
9550 return unsignedp ? XEXP (x, 0) : x;
9551 default:
9552 break;
9557 else if (code == SUBREG)
9559 machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
9560 rtx new_rtx, r = known_cond (SUBREG_REG (x), cond, reg, val);
9562 if (SUBREG_REG (x) != r)
9564 /* We must simplify subreg here, before we lose track of the
9565 original inner_mode. */
9566 new_rtx = simplify_subreg (GET_MODE (x), r,
9567 inner_mode, SUBREG_BYTE (x));
9568 if (new_rtx)
9569 return new_rtx;
9570 else
9571 SUBST (SUBREG_REG (x), r);
9574 return x;
9576 /* We don't have to handle SIGN_EXTEND here, because even in the
9577 case of replacing something with a modeless CONST_INT, a
9578 CONST_INT is already (supposed to be) a valid sign extension for
9579 its narrower mode, which implies it's already properly
9580 sign-extended for the wider mode. Now, for ZERO_EXTEND, the
9581 story is different. */
9582 else if (code == ZERO_EXTEND)
9584 machine_mode inner_mode = GET_MODE (XEXP (x, 0));
9585 rtx new_rtx, r = known_cond (XEXP (x, 0), cond, reg, val);
9587 if (XEXP (x, 0) != r)
9589 /* We must simplify the zero_extend here, before we lose
9590 track of the original inner_mode. */
9591 new_rtx = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
9592 r, inner_mode);
9593 if (new_rtx)
9594 return new_rtx;
9595 else
9596 SUBST (XEXP (x, 0), r);
9599 return x;
9602 fmt = GET_RTX_FORMAT (code);
9603 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
9605 if (fmt[i] == 'e')
9606 SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
9607 else if (fmt[i] == 'E')
9608 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9609 SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
9610 cond, reg, val));
9613 return x;
9616 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
9617 assignment as a field assignment. */
9619 static int
9620 rtx_equal_for_field_assignment_p (rtx x, rtx y, bool widen_x)
9622 if (widen_x && GET_MODE (x) != GET_MODE (y))
9624 if (paradoxical_subreg_p (GET_MODE (x), GET_MODE (y)))
9625 return 0;
9626 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
9627 return 0;
9628 x = adjust_address_nv (x, GET_MODE (y),
9629 byte_lowpart_offset (GET_MODE (y),
9630 GET_MODE (x)));
9633 if (x == y || rtx_equal_p (x, y))
9634 return 1;
9636 if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
9637 return 0;
9639 /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
9640 Note that all SUBREGs of MEM are paradoxical; otherwise they
9641 would have been rewritten. */
9642 if (MEM_P (x) && GET_CODE (y) == SUBREG
9643 && MEM_P (SUBREG_REG (y))
9644 && rtx_equal_p (SUBREG_REG (y),
9645 gen_lowpart (GET_MODE (SUBREG_REG (y)), x)))
9646 return 1;
9648 if (MEM_P (y) && GET_CODE (x) == SUBREG
9649 && MEM_P (SUBREG_REG (x))
9650 && rtx_equal_p (SUBREG_REG (x),
9651 gen_lowpart (GET_MODE (SUBREG_REG (x)), y)))
9652 return 1;
9654 /* We used to see if get_last_value of X and Y were the same but that's
9655 not correct. In one direction, we'll cause the assignment to have
9656 the wrong destination and in the case, we'll import a register into this
9657 insn that might have already have been dead. So fail if none of the
9658 above cases are true. */
9659 return 0;
9662 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
9663 Return that assignment if so.
9665 We only handle the most common cases. */
9667 static rtx
9668 make_field_assignment (rtx x)
9670 rtx dest = SET_DEST (x);
9671 rtx src = SET_SRC (x);
9672 rtx assign;
9673 rtx rhs, lhs;
9674 HOST_WIDE_INT c1;
9675 HOST_WIDE_INT pos;
9676 unsigned HOST_WIDE_INT len;
9677 rtx other;
9679 /* All the rules in this function are specific to scalar integers. */
9680 scalar_int_mode mode;
9681 if (!is_a <scalar_int_mode> (GET_MODE (dest), &mode))
9682 return x;
9684 /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
9685 a clear of a one-bit field. We will have changed it to
9686 (and (rotate (const_int -2) POS) DEST), so check for that. Also check
9687 for a SUBREG. */
9689 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
9690 && CONST_INT_P (XEXP (XEXP (src, 0), 0))
9691 && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
9692 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9694 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
9695 1, 1, 1, 0);
9696 if (assign != 0)
9697 return gen_rtx_SET (assign, const0_rtx);
9698 return x;
9701 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
9702 && subreg_lowpart_p (XEXP (src, 0))
9703 && partial_subreg_p (XEXP (src, 0))
9704 && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
9705 && CONST_INT_P (XEXP (SUBREG_REG (XEXP (src, 0)), 0))
9706 && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
9707 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9709 assign = make_extraction (VOIDmode, dest, 0,
9710 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
9711 1, 1, 1, 0);
9712 if (assign != 0)
9713 return gen_rtx_SET (assign, const0_rtx);
9714 return x;
9717 /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
9718 one-bit field. */
9719 if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
9720 && XEXP (XEXP (src, 0), 0) == const1_rtx
9721 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9723 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
9724 1, 1, 1, 0);
9725 if (assign != 0)
9726 return gen_rtx_SET (assign, const1_rtx);
9727 return x;
9730 /* If DEST is already a field assignment, i.e. ZERO_EXTRACT, and the
9731 SRC is an AND with all bits of that field set, then we can discard
9732 the AND. */
9733 if (GET_CODE (dest) == ZERO_EXTRACT
9734 && CONST_INT_P (XEXP (dest, 1))
9735 && GET_CODE (src) == AND
9736 && CONST_INT_P (XEXP (src, 1)))
9738 HOST_WIDE_INT width = INTVAL (XEXP (dest, 1));
9739 unsigned HOST_WIDE_INT and_mask = INTVAL (XEXP (src, 1));
9740 unsigned HOST_WIDE_INT ze_mask;
9742 if (width >= HOST_BITS_PER_WIDE_INT)
9743 ze_mask = -1;
9744 else
9745 ze_mask = ((unsigned HOST_WIDE_INT)1 << width) - 1;
9747 /* Complete overlap. We can remove the source AND. */
9748 if ((and_mask & ze_mask) == ze_mask)
9749 return gen_rtx_SET (dest, XEXP (src, 0));
9751 /* Partial overlap. We can reduce the source AND. */
9752 if ((and_mask & ze_mask) != and_mask)
9754 src = gen_rtx_AND (mode, XEXP (src, 0),
9755 gen_int_mode (and_mask & ze_mask, mode));
9756 return gen_rtx_SET (dest, src);
9760 /* The other case we handle is assignments into a constant-position
9761 field. They look like (ior/xor (and DEST C1) OTHER). If C1 represents
9762 a mask that has all one bits except for a group of zero bits and
9763 OTHER is known to have zeros where C1 has ones, this is such an
9764 assignment. Compute the position and length from C1. Shift OTHER
9765 to the appropriate position, force it to the required mode, and
9766 make the extraction. Check for the AND in both operands. */
9768 /* One or more SUBREGs might obscure the constant-position field
9769 assignment. The first one we are likely to encounter is an outer
9770 narrowing SUBREG, which we can just strip for the purposes of
9771 identifying the constant-field assignment. */
9772 scalar_int_mode src_mode = mode;
9773 if (GET_CODE (src) == SUBREG
9774 && subreg_lowpart_p (src)
9775 && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (src)), &src_mode))
9776 src = SUBREG_REG (src);
9778 if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
9779 return x;
9781 rhs = expand_compound_operation (XEXP (src, 0));
9782 lhs = expand_compound_operation (XEXP (src, 1));
9784 if (GET_CODE (rhs) == AND
9785 && CONST_INT_P (XEXP (rhs, 1))
9786 && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
9787 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
9788 /* The second SUBREG that might get in the way is a paradoxical
9789 SUBREG around the first operand of the AND. We want to
9790 pretend the operand is as wide as the destination here. We
9791 do this by adjusting the MEM to wider mode for the sole
9792 purpose of the call to rtx_equal_for_field_assignment_p. Also
9793 note this trick only works for MEMs. */
9794 else if (GET_CODE (rhs) == AND
9795 && paradoxical_subreg_p (XEXP (rhs, 0))
9796 && MEM_P (SUBREG_REG (XEXP (rhs, 0)))
9797 && CONST_INT_P (XEXP (rhs, 1))
9798 && rtx_equal_for_field_assignment_p (SUBREG_REG (XEXP (rhs, 0)),
9799 dest, true))
9800 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
9801 else if (GET_CODE (lhs) == AND
9802 && CONST_INT_P (XEXP (lhs, 1))
9803 && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
9804 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
9805 /* The second SUBREG that might get in the way is a paradoxical
9806 SUBREG around the first operand of the AND. We want to
9807 pretend the operand is as wide as the destination here. We
9808 do this by adjusting the MEM to wider mode for the sole
9809 purpose of the call to rtx_equal_for_field_assignment_p. Also
9810 note this trick only works for MEMs. */
9811 else if (GET_CODE (lhs) == AND
9812 && paradoxical_subreg_p (XEXP (lhs, 0))
9813 && MEM_P (SUBREG_REG (XEXP (lhs, 0)))
9814 && CONST_INT_P (XEXP (lhs, 1))
9815 && rtx_equal_for_field_assignment_p (SUBREG_REG (XEXP (lhs, 0)),
9816 dest, true))
9817 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
9818 else
9819 return x;
9821 pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (mode), &len);
9822 if (pos < 0
9823 || pos + len > GET_MODE_PRECISION (mode)
9824 || GET_MODE_PRECISION (mode) > HOST_BITS_PER_WIDE_INT
9825 || (c1 & nonzero_bits (other, mode)) != 0)
9826 return x;
9828 assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
9829 if (assign == 0)
9830 return x;
9832 /* The mode to use for the source is the mode of the assignment, or of
9833 what is inside a possible STRICT_LOW_PART. */
9834 machine_mode new_mode = (GET_CODE (assign) == STRICT_LOW_PART
9835 ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
9837 /* Shift OTHER right POS places and make it the source, restricting it
9838 to the proper length and mode. */
9840 src = canon_reg_for_combine (simplify_shift_const (NULL_RTX, LSHIFTRT,
9841 src_mode, other, pos),
9842 dest);
9843 src = force_to_mode (src, new_mode,
9844 len >= HOST_BITS_PER_WIDE_INT
9845 ? HOST_WIDE_INT_M1U
9846 : (HOST_WIDE_INT_1U << len) - 1,
9849 /* If SRC is masked by an AND that does not make a difference in
9850 the value being stored, strip it. */
9851 if (GET_CODE (assign) == ZERO_EXTRACT
9852 && CONST_INT_P (XEXP (assign, 1))
9853 && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
9854 && GET_CODE (src) == AND
9855 && CONST_INT_P (XEXP (src, 1))
9856 && UINTVAL (XEXP (src, 1))
9857 == (HOST_WIDE_INT_1U << INTVAL (XEXP (assign, 1))) - 1)
9858 src = XEXP (src, 0);
9860 return gen_rtx_SET (assign, src);
9863 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
9864 if so. */
9866 static rtx
9867 apply_distributive_law (rtx x)
9869 enum rtx_code code = GET_CODE (x);
9870 enum rtx_code inner_code;
9871 rtx lhs, rhs, other;
9872 rtx tem;
9874 /* Distributivity is not true for floating point as it can change the
9875 value. So we don't do it unless -funsafe-math-optimizations. */
9876 if (FLOAT_MODE_P (GET_MODE (x))
9877 && ! flag_unsafe_math_optimizations)
9878 return x;
9880 /* The outer operation can only be one of the following: */
9881 if (code != IOR && code != AND && code != XOR
9882 && code != PLUS && code != MINUS)
9883 return x;
9885 lhs = XEXP (x, 0);
9886 rhs = XEXP (x, 1);
9888 /* If either operand is a primitive we can't do anything, so get out
9889 fast. */
9890 if (OBJECT_P (lhs) || OBJECT_P (rhs))
9891 return x;
9893 lhs = expand_compound_operation (lhs);
9894 rhs = expand_compound_operation (rhs);
9895 inner_code = GET_CODE (lhs);
9896 if (inner_code != GET_CODE (rhs))
9897 return x;
9899 /* See if the inner and outer operations distribute. */
9900 switch (inner_code)
9902 case LSHIFTRT:
9903 case ASHIFTRT:
9904 case AND:
9905 case IOR:
9906 /* These all distribute except over PLUS. */
9907 if (code == PLUS || code == MINUS)
9908 return x;
9909 break;
9911 case MULT:
9912 if (code != PLUS && code != MINUS)
9913 return x;
9914 break;
9916 case ASHIFT:
9917 /* This is also a multiply, so it distributes over everything. */
9918 break;
9920 /* This used to handle SUBREG, but this turned out to be counter-
9921 productive, since (subreg (op ...)) usually is not handled by
9922 insn patterns, and this "optimization" therefore transformed
9923 recognizable patterns into unrecognizable ones. Therefore the
9924 SUBREG case was removed from here.
9926 It is possible that distributing SUBREG over arithmetic operations
9927 leads to an intermediate result than can then be optimized further,
9928 e.g. by moving the outer SUBREG to the other side of a SET as done
9929 in simplify_set. This seems to have been the original intent of
9930 handling SUBREGs here.
9932 However, with current GCC this does not appear to actually happen,
9933 at least on major platforms. If some case is found where removing
9934 the SUBREG case here prevents follow-on optimizations, distributing
9935 SUBREGs ought to be re-added at that place, e.g. in simplify_set. */
9937 default:
9938 return x;
9941 /* Set LHS and RHS to the inner operands (A and B in the example
9942 above) and set OTHER to the common operand (C in the example).
9943 There is only one way to do this unless the inner operation is
9944 commutative. */
9945 if (COMMUTATIVE_ARITH_P (lhs)
9946 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
9947 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
9948 else if (COMMUTATIVE_ARITH_P (lhs)
9949 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
9950 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
9951 else if (COMMUTATIVE_ARITH_P (lhs)
9952 && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
9953 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
9954 else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
9955 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
9956 else
9957 return x;
9959 /* Form the new inner operation, seeing if it simplifies first. */
9960 tem = simplify_gen_binary (code, GET_MODE (x), lhs, rhs);
9962 /* There is one exception to the general way of distributing:
9963 (a | c) ^ (b | c) -> (a ^ b) & ~c */
9964 if (code == XOR && inner_code == IOR)
9966 inner_code = AND;
9967 other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
9970 /* We may be able to continuing distributing the result, so call
9971 ourselves recursively on the inner operation before forming the
9972 outer operation, which we return. */
9973 return simplify_gen_binary (inner_code, GET_MODE (x),
9974 apply_distributive_law (tem), other);
9977 /* See if X is of the form (* (+ A B) C), and if so convert to
9978 (+ (* A C) (* B C)) and try to simplify.
9980 Most of the time, this results in no change. However, if some of
9981 the operands are the same or inverses of each other, simplifications
9982 will result.
9984 For example, (and (ior A B) (not B)) can occur as the result of
9985 expanding a bit field assignment. When we apply the distributive
9986 law to this, we get (ior (and (A (not B))) (and (B (not B)))),
9987 which then simplifies to (and (A (not B))).
9989 Note that no checks happen on the validity of applying the inverse
9990 distributive law. This is pointless since we can do it in the
9991 few places where this routine is called.
9993 N is the index of the term that is decomposed (the arithmetic operation,
9994 i.e. (+ A B) in the first example above). !N is the index of the term that
9995 is distributed, i.e. of C in the first example above. */
9996 static rtx
9997 distribute_and_simplify_rtx (rtx x, int n)
9999 machine_mode mode;
10000 enum rtx_code outer_code, inner_code;
10001 rtx decomposed, distributed, inner_op0, inner_op1, new_op0, new_op1, tmp;
10003 /* Distributivity is not true for floating point as it can change the
10004 value. So we don't do it unless -funsafe-math-optimizations. */
10005 if (FLOAT_MODE_P (GET_MODE (x))
10006 && ! flag_unsafe_math_optimizations)
10007 return NULL_RTX;
10009 decomposed = XEXP (x, n);
10010 if (!ARITHMETIC_P (decomposed))
10011 return NULL_RTX;
10013 mode = GET_MODE (x);
10014 outer_code = GET_CODE (x);
10015 distributed = XEXP (x, !n);
10017 inner_code = GET_CODE (decomposed);
10018 inner_op0 = XEXP (decomposed, 0);
10019 inner_op1 = XEXP (decomposed, 1);
10021 /* Special case (and (xor B C) (not A)), which is equivalent to
10022 (xor (ior A B) (ior A C)) */
10023 if (outer_code == AND && inner_code == XOR && GET_CODE (distributed) == NOT)
10025 distributed = XEXP (distributed, 0);
10026 outer_code = IOR;
10029 if (n == 0)
10031 /* Distribute the second term. */
10032 new_op0 = simplify_gen_binary (outer_code, mode, inner_op0, distributed);
10033 new_op1 = simplify_gen_binary (outer_code, mode, inner_op1, distributed);
10035 else
10037 /* Distribute the first term. */
10038 new_op0 = simplify_gen_binary (outer_code, mode, distributed, inner_op0);
10039 new_op1 = simplify_gen_binary (outer_code, mode, distributed, inner_op1);
10042 tmp = apply_distributive_law (simplify_gen_binary (inner_code, mode,
10043 new_op0, new_op1));
10044 if (GET_CODE (tmp) != outer_code
10045 && (set_src_cost (tmp, mode, optimize_this_for_speed_p)
10046 < set_src_cost (x, mode, optimize_this_for_speed_p)))
10047 return tmp;
10049 return NULL_RTX;
10052 /* Simplify a logical `and' of VAROP with the constant CONSTOP, to be done
10053 in MODE. Return an equivalent form, if different from (and VAROP
10054 (const_int CONSTOP)). Otherwise, return NULL_RTX. */
10056 static rtx
10057 simplify_and_const_int_1 (scalar_int_mode mode, rtx varop,
10058 unsigned HOST_WIDE_INT constop)
10060 unsigned HOST_WIDE_INT nonzero;
10061 unsigned HOST_WIDE_INT orig_constop;
10062 rtx orig_varop;
10063 int i;
10065 orig_varop = varop;
10066 orig_constop = constop;
10067 if (GET_CODE (varop) == CLOBBER)
10068 return NULL_RTX;
10070 /* Simplify VAROP knowing that we will be only looking at some of the
10071 bits in it.
10073 Note by passing in CONSTOP, we guarantee that the bits not set in
10074 CONSTOP are not significant and will never be examined. We must
10075 ensure that is the case by explicitly masking out those bits
10076 before returning. */
10077 varop = force_to_mode (varop, mode, constop, 0);
10079 /* If VAROP is a CLOBBER, we will fail so return it. */
10080 if (GET_CODE (varop) == CLOBBER)
10081 return varop;
10083 /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
10084 to VAROP and return the new constant. */
10085 if (CONST_INT_P (varop))
10086 return gen_int_mode (INTVAL (varop) & constop, mode);
10088 /* See what bits may be nonzero in VAROP. Unlike the general case of
10089 a call to nonzero_bits, here we don't care about bits outside
10090 MODE. */
10092 nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
10094 /* Turn off all bits in the constant that are known to already be zero.
10095 Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
10096 which is tested below. */
10098 constop &= nonzero;
10100 /* If we don't have any bits left, return zero. */
10101 if (constop == 0)
10102 return const0_rtx;
10104 /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
10105 a power of two, we can replace this with an ASHIFT. */
10106 if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
10107 && (i = exact_log2 (constop)) >= 0)
10108 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
10110 /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
10111 or XOR, then try to apply the distributive law. This may eliminate
10112 operations if either branch can be simplified because of the AND.
10113 It may also make some cases more complex, but those cases probably
10114 won't match a pattern either with or without this. */
10116 if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
10118 scalar_int_mode varop_mode = as_a <scalar_int_mode> (GET_MODE (varop));
10119 return
10120 gen_lowpart
10121 (mode,
10122 apply_distributive_law
10123 (simplify_gen_binary (GET_CODE (varop), varop_mode,
10124 simplify_and_const_int (NULL_RTX, varop_mode,
10125 XEXP (varop, 0),
10126 constop),
10127 simplify_and_const_int (NULL_RTX, varop_mode,
10128 XEXP (varop, 1),
10129 constop))));
10132 /* If VAROP is PLUS, and the constant is a mask of low bits, distribute
10133 the AND and see if one of the operands simplifies to zero. If so, we
10134 may eliminate it. */
10136 if (GET_CODE (varop) == PLUS
10137 && pow2p_hwi (constop + 1))
10139 rtx o0, o1;
10141 o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
10142 o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
10143 if (o0 == const0_rtx)
10144 return o1;
10145 if (o1 == const0_rtx)
10146 return o0;
10149 /* Make a SUBREG if necessary. If we can't make it, fail. */
10150 varop = gen_lowpart (mode, varop);
10151 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
10152 return NULL_RTX;
10154 /* If we are only masking insignificant bits, return VAROP. */
10155 if (constop == nonzero)
10156 return varop;
10158 if (varop == orig_varop && constop == orig_constop)
10159 return NULL_RTX;
10161 /* Otherwise, return an AND. */
10162 return simplify_gen_binary (AND, mode, varop, gen_int_mode (constop, mode));
10166 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
10167 in MODE.
10169 Return an equivalent form, if different from X. Otherwise, return X. If
10170 X is zero, we are to always construct the equivalent form. */
10172 static rtx
10173 simplify_and_const_int (rtx x, scalar_int_mode mode, rtx varop,
10174 unsigned HOST_WIDE_INT constop)
10176 rtx tem = simplify_and_const_int_1 (mode, varop, constop);
10177 if (tem)
10178 return tem;
10180 if (!x)
10181 x = simplify_gen_binary (AND, GET_MODE (varop), varop,
10182 gen_int_mode (constop, mode));
10183 if (GET_MODE (x) != mode)
10184 x = gen_lowpart (mode, x);
10185 return x;
10188 /* Given a REG X of mode XMODE, compute which bits in X can be nonzero.
10189 We don't care about bits outside of those defined in MODE.
10191 For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
10192 a shift, AND, or zero_extract, we can do better. */
10194 static rtx
10195 reg_nonzero_bits_for_combine (const_rtx x, scalar_int_mode xmode,
10196 scalar_int_mode mode,
10197 unsigned HOST_WIDE_INT *nonzero)
10199 rtx tem;
10200 reg_stat_type *rsp;
10202 /* If X is a register whose nonzero bits value is current, use it.
10203 Otherwise, if X is a register whose value we can find, use that
10204 value. Otherwise, use the previously-computed global nonzero bits
10205 for this register. */
10207 rsp = &reg_stat[REGNO (x)];
10208 if (rsp->last_set_value != 0
10209 && (rsp->last_set_mode == mode
10210 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
10211 && GET_MODE_CLASS (rsp->last_set_mode) == MODE_INT
10212 && GET_MODE_CLASS (mode) == MODE_INT))
10213 && ((rsp->last_set_label >= label_tick_ebb_start
10214 && rsp->last_set_label < label_tick)
10215 || (rsp->last_set_label == label_tick
10216 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
10217 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
10218 && REGNO (x) < reg_n_sets_max
10219 && REG_N_SETS (REGNO (x)) == 1
10220 && !REGNO_REG_SET_P
10221 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
10222 REGNO (x)))))
10224 /* Note that, even if the precision of last_set_mode is lower than that
10225 of mode, record_value_for_reg invoked nonzero_bits on the register
10226 with nonzero_bits_mode (because last_set_mode is necessarily integral
10227 and HWI_COMPUTABLE_MODE_P in this case) so bits in nonzero_bits_mode
10228 are all valid, hence in mode too since nonzero_bits_mode is defined
10229 to the largest HWI_COMPUTABLE_MODE_P mode. */
10230 *nonzero &= rsp->last_set_nonzero_bits;
10231 return NULL;
10234 tem = get_last_value (x);
10235 if (tem)
10237 if (SHORT_IMMEDIATES_SIGN_EXTEND)
10238 tem = sign_extend_short_imm (tem, xmode, GET_MODE_PRECISION (mode));
10240 return tem;
10243 if (nonzero_sign_valid && rsp->nonzero_bits)
10245 unsigned HOST_WIDE_INT mask = rsp->nonzero_bits;
10247 if (GET_MODE_PRECISION (xmode) < GET_MODE_PRECISION (mode))
10248 /* We don't know anything about the upper bits. */
10249 mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (xmode);
10251 *nonzero &= mask;
10254 return NULL;
10257 /* Given a reg X of mode XMODE, return the number of bits at the high-order
10258 end of X that are known to be equal to the sign bit. X will be used
10259 in mode MODE; the returned value will always be between 1 and the
10260 number of bits in MODE. */
10262 static rtx
10263 reg_num_sign_bit_copies_for_combine (const_rtx x, scalar_int_mode xmode,
10264 scalar_int_mode mode,
10265 unsigned int *result)
10267 rtx tem;
10268 reg_stat_type *rsp;
10270 rsp = &reg_stat[REGNO (x)];
10271 if (rsp->last_set_value != 0
10272 && rsp->last_set_mode == mode
10273 && ((rsp->last_set_label >= label_tick_ebb_start
10274 && rsp->last_set_label < label_tick)
10275 || (rsp->last_set_label == label_tick
10276 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
10277 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
10278 && REGNO (x) < reg_n_sets_max
10279 && REG_N_SETS (REGNO (x)) == 1
10280 && !REGNO_REG_SET_P
10281 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
10282 REGNO (x)))))
10284 *result = rsp->last_set_sign_bit_copies;
10285 return NULL;
10288 tem = get_last_value (x);
10289 if (tem != 0)
10290 return tem;
10292 if (nonzero_sign_valid && rsp->sign_bit_copies != 0
10293 && GET_MODE_PRECISION (xmode) == GET_MODE_PRECISION (mode))
10294 *result = rsp->sign_bit_copies;
10296 return NULL;
10299 /* Return the number of "extended" bits there are in X, when interpreted
10300 as a quantity in MODE whose signedness is indicated by UNSIGNEDP. For
10301 unsigned quantities, this is the number of high-order zero bits.
10302 For signed quantities, this is the number of copies of the sign bit
10303 minus 1. In both case, this function returns the number of "spare"
10304 bits. For example, if two quantities for which this function returns
10305 at least 1 are added, the addition is known not to overflow.
10307 This function will always return 0 unless called during combine, which
10308 implies that it must be called from a define_split. */
10310 unsigned int
10311 extended_count (const_rtx x, machine_mode mode, int unsignedp)
10313 if (nonzero_sign_valid == 0)
10314 return 0;
10316 scalar_int_mode int_mode;
10317 return (unsignedp
10318 ? (is_a <scalar_int_mode> (mode, &int_mode)
10319 && HWI_COMPUTABLE_MODE_P (int_mode)
10320 ? (unsigned int) (GET_MODE_PRECISION (int_mode) - 1
10321 - floor_log2 (nonzero_bits (x, int_mode)))
10322 : 0)
10323 : num_sign_bit_copies (x, mode) - 1);
10326 /* This function is called from `simplify_shift_const' to merge two
10327 outer operations. Specifically, we have already found that we need
10328 to perform operation *POP0 with constant *PCONST0 at the outermost
10329 position. We would now like to also perform OP1 with constant CONST1
10330 (with *POP0 being done last).
10332 Return 1 if we can do the operation and update *POP0 and *PCONST0 with
10333 the resulting operation. *PCOMP_P is set to 1 if we would need to
10334 complement the innermost operand, otherwise it is unchanged.
10336 MODE is the mode in which the operation will be done. No bits outside
10337 the width of this mode matter. It is assumed that the width of this mode
10338 is smaller than or equal to HOST_BITS_PER_WIDE_INT.
10340 If *POP0 or OP1 are UNKNOWN, it means no operation is required. Only NEG, PLUS,
10341 IOR, XOR, and AND are supported. We may set *POP0 to SET if the proper
10342 result is simply *PCONST0.
10344 If the resulting operation cannot be expressed as one operation, we
10345 return 0 and do not change *POP0, *PCONST0, and *PCOMP_P. */
10347 static int
10348 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)
10350 enum rtx_code op0 = *pop0;
10351 HOST_WIDE_INT const0 = *pconst0;
10353 const0 &= GET_MODE_MASK (mode);
10354 const1 &= GET_MODE_MASK (mode);
10356 /* If OP0 is an AND, clear unimportant bits in CONST1. */
10357 if (op0 == AND)
10358 const1 &= const0;
10360 /* If OP0 or OP1 is UNKNOWN, this is easy. Similarly if they are the same or
10361 if OP0 is SET. */
10363 if (op1 == UNKNOWN || op0 == SET)
10364 return 1;
10366 else if (op0 == UNKNOWN)
10367 op0 = op1, const0 = const1;
10369 else if (op0 == op1)
10371 switch (op0)
10373 case AND:
10374 const0 &= const1;
10375 break;
10376 case IOR:
10377 const0 |= const1;
10378 break;
10379 case XOR:
10380 const0 ^= const1;
10381 break;
10382 case PLUS:
10383 const0 += const1;
10384 break;
10385 case NEG:
10386 op0 = UNKNOWN;
10387 break;
10388 default:
10389 break;
10393 /* Otherwise, if either is a PLUS or NEG, we can't do anything. */
10394 else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
10395 return 0;
10397 /* If the two constants aren't the same, we can't do anything. The
10398 remaining six cases can all be done. */
10399 else if (const0 != const1)
10400 return 0;
10402 else
10403 switch (op0)
10405 case IOR:
10406 if (op1 == AND)
10407 /* (a & b) | b == b */
10408 op0 = SET;
10409 else /* op1 == XOR */
10410 /* (a ^ b) | b == a | b */
10412 break;
10414 case XOR:
10415 if (op1 == AND)
10416 /* (a & b) ^ b == (~a) & b */
10417 op0 = AND, *pcomp_p = 1;
10418 else /* op1 == IOR */
10419 /* (a | b) ^ b == a & ~b */
10420 op0 = AND, const0 = ~const0;
10421 break;
10423 case AND:
10424 if (op1 == IOR)
10425 /* (a | b) & b == b */
10426 op0 = SET;
10427 else /* op1 == XOR */
10428 /* (a ^ b) & b) == (~a) & b */
10429 *pcomp_p = 1;
10430 break;
10431 default:
10432 break;
10435 /* Check for NO-OP cases. */
10436 const0 &= GET_MODE_MASK (mode);
10437 if (const0 == 0
10438 && (op0 == IOR || op0 == XOR || op0 == PLUS))
10439 op0 = UNKNOWN;
10440 else if (const0 == 0 && op0 == AND)
10441 op0 = SET;
10442 else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
10443 && op0 == AND)
10444 op0 = UNKNOWN;
10446 *pop0 = op0;
10448 /* ??? Slightly redundant with the above mask, but not entirely.
10449 Moving this above means we'd have to sign-extend the mode mask
10450 for the final test. */
10451 if (op0 != UNKNOWN && op0 != NEG)
10452 *pconst0 = trunc_int_for_mode (const0, mode);
10454 return 1;
10457 /* A helper to simplify_shift_const_1 to determine the mode we can perform
10458 the shift in. The original shift operation CODE is performed on OP in
10459 ORIG_MODE. Return the wider mode MODE if we can perform the operation
10460 in that mode. Return ORIG_MODE otherwise. We can also assume that the
10461 result of the shift is subject to operation OUTER_CODE with operand
10462 OUTER_CONST. */
10464 static scalar_int_mode
10465 try_widen_shift_mode (enum rtx_code code, rtx op, int count,
10466 scalar_int_mode orig_mode, scalar_int_mode mode,
10467 enum rtx_code outer_code, HOST_WIDE_INT outer_const)
10469 gcc_assert (GET_MODE_PRECISION (mode) > GET_MODE_PRECISION (orig_mode));
10471 /* In general we can't perform in wider mode for right shift and rotate. */
10472 switch (code)
10474 case ASHIFTRT:
10475 /* We can still widen if the bits brought in from the left are identical
10476 to the sign bit of ORIG_MODE. */
10477 if (num_sign_bit_copies (op, mode)
10478 > (unsigned) (GET_MODE_PRECISION (mode)
10479 - GET_MODE_PRECISION (orig_mode)))
10480 return mode;
10481 return orig_mode;
10483 case LSHIFTRT:
10484 /* Similarly here but with zero bits. */
10485 if (HWI_COMPUTABLE_MODE_P (mode)
10486 && (nonzero_bits (op, mode) & ~GET_MODE_MASK (orig_mode)) == 0)
10487 return mode;
10489 /* We can also widen if the bits brought in will be masked off. This
10490 operation is performed in ORIG_MODE. */
10491 if (outer_code == AND)
10493 int care_bits = low_bitmask_len (orig_mode, outer_const);
10495 if (care_bits >= 0
10496 && GET_MODE_PRECISION (orig_mode) - care_bits >= count)
10497 return mode;
10499 /* fall through */
10501 case ROTATE:
10502 return orig_mode;
10504 case ROTATERT:
10505 gcc_unreachable ();
10507 default:
10508 return mode;
10512 /* Simplify a shift of VAROP by ORIG_COUNT bits. CODE says what kind
10513 of shift. The result of the shift is RESULT_MODE. Return NULL_RTX
10514 if we cannot simplify it. Otherwise, return a simplified value.
10516 The shift is normally computed in the widest mode we find in VAROP, as
10517 long as it isn't a different number of words than RESULT_MODE. Exceptions
10518 are ASHIFTRT and ROTATE, which are always done in their original mode. */
10520 static rtx
10521 simplify_shift_const_1 (enum rtx_code code, machine_mode result_mode,
10522 rtx varop, int orig_count)
10524 enum rtx_code orig_code = code;
10525 rtx orig_varop = varop;
10526 int count, log2;
10527 machine_mode mode = result_mode;
10528 machine_mode shift_mode;
10529 scalar_int_mode tmode, inner_mode, int_mode, int_varop_mode, int_result_mode;
10530 /* We form (outer_op (code varop count) (outer_const)). */
10531 enum rtx_code outer_op = UNKNOWN;
10532 HOST_WIDE_INT outer_const = 0;
10533 int complement_p = 0;
10534 rtx new_rtx, x;
10536 /* Make sure and truncate the "natural" shift on the way in. We don't
10537 want to do this inside the loop as it makes it more difficult to
10538 combine shifts. */
10539 if (SHIFT_COUNT_TRUNCATED)
10540 orig_count &= GET_MODE_UNIT_BITSIZE (mode) - 1;
10542 /* If we were given an invalid count, don't do anything except exactly
10543 what was requested. */
10545 if (orig_count < 0 || orig_count >= (int) GET_MODE_UNIT_PRECISION (mode))
10546 return NULL_RTX;
10548 count = orig_count;
10550 /* Unless one of the branches of the `if' in this loop does a `continue',
10551 we will `break' the loop after the `if'. */
10553 while (count != 0)
10555 /* If we have an operand of (clobber (const_int 0)), fail. */
10556 if (GET_CODE (varop) == CLOBBER)
10557 return NULL_RTX;
10559 /* Convert ROTATERT to ROTATE. */
10560 if (code == ROTATERT)
10562 unsigned int bitsize = GET_MODE_UNIT_PRECISION (result_mode);
10563 code = ROTATE;
10564 count = bitsize - count;
10567 shift_mode = result_mode;
10568 if (shift_mode != mode)
10570 /* We only change the modes of scalar shifts. */
10571 int_mode = as_a <scalar_int_mode> (mode);
10572 int_result_mode = as_a <scalar_int_mode> (result_mode);
10573 shift_mode = try_widen_shift_mode (code, varop, count,
10574 int_result_mode, int_mode,
10575 outer_op, outer_const);
10578 scalar_int_mode shift_unit_mode
10579 = as_a <scalar_int_mode> (GET_MODE_INNER (shift_mode));
10581 /* Handle cases where the count is greater than the size of the mode
10582 minus 1. For ASHIFT, use the size minus one as the count (this can
10583 occur when simplifying (lshiftrt (ashiftrt ..))). For rotates,
10584 take the count modulo the size. For other shifts, the result is
10585 zero.
10587 Since these shifts are being produced by the compiler by combining
10588 multiple operations, each of which are defined, we know what the
10589 result is supposed to be. */
10591 if (count > (GET_MODE_PRECISION (shift_unit_mode) - 1))
10593 if (code == ASHIFTRT)
10594 count = GET_MODE_PRECISION (shift_unit_mode) - 1;
10595 else if (code == ROTATE || code == ROTATERT)
10596 count %= GET_MODE_PRECISION (shift_unit_mode);
10597 else
10599 /* We can't simply return zero because there may be an
10600 outer op. */
10601 varop = const0_rtx;
10602 count = 0;
10603 break;
10607 /* If we discovered we had to complement VAROP, leave. Making a NOT
10608 here would cause an infinite loop. */
10609 if (complement_p)
10610 break;
10612 if (shift_mode == shift_unit_mode)
10614 /* An arithmetic right shift of a quantity known to be -1 or 0
10615 is a no-op. */
10616 if (code == ASHIFTRT
10617 && (num_sign_bit_copies (varop, shift_unit_mode)
10618 == GET_MODE_PRECISION (shift_unit_mode)))
10620 count = 0;
10621 break;
10624 /* If we are doing an arithmetic right shift and discarding all but
10625 the sign bit copies, this is equivalent to doing a shift by the
10626 bitsize minus one. Convert it into that shift because it will
10627 often allow other simplifications. */
10629 if (code == ASHIFTRT
10630 && (count + num_sign_bit_copies (varop, shift_unit_mode)
10631 >= GET_MODE_PRECISION (shift_unit_mode)))
10632 count = GET_MODE_PRECISION (shift_unit_mode) - 1;
10634 /* We simplify the tests below and elsewhere by converting
10635 ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
10636 `make_compound_operation' will convert it to an ASHIFTRT for
10637 those machines (such as VAX) that don't have an LSHIFTRT. */
10638 if (code == ASHIFTRT
10639 && HWI_COMPUTABLE_MODE_P (shift_unit_mode)
10640 && val_signbit_known_clear_p (shift_unit_mode,
10641 nonzero_bits (varop,
10642 shift_unit_mode)))
10643 code = LSHIFTRT;
10645 if (((code == LSHIFTRT
10646 && HWI_COMPUTABLE_MODE_P (shift_unit_mode)
10647 && !(nonzero_bits (varop, shift_unit_mode) >> count))
10648 || (code == ASHIFT
10649 && HWI_COMPUTABLE_MODE_P (shift_unit_mode)
10650 && !((nonzero_bits (varop, shift_unit_mode) << count)
10651 & GET_MODE_MASK (shift_unit_mode))))
10652 && !side_effects_p (varop))
10653 varop = const0_rtx;
10656 switch (GET_CODE (varop))
10658 case SIGN_EXTEND:
10659 case ZERO_EXTEND:
10660 case SIGN_EXTRACT:
10661 case ZERO_EXTRACT:
10662 new_rtx = expand_compound_operation (varop);
10663 if (new_rtx != varop)
10665 varop = new_rtx;
10666 continue;
10668 break;
10670 case MEM:
10671 /* The following rules apply only to scalars. */
10672 if (shift_mode != shift_unit_mode)
10673 break;
10674 int_mode = as_a <scalar_int_mode> (mode);
10676 /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
10677 minus the width of a smaller mode, we can do this with a
10678 SIGN_EXTEND or ZERO_EXTEND from the narrower memory location. */
10679 if ((code == ASHIFTRT || code == LSHIFTRT)
10680 && ! mode_dependent_address_p (XEXP (varop, 0),
10681 MEM_ADDR_SPACE (varop))
10682 && ! MEM_VOLATILE_P (varop)
10683 && (int_mode_for_size (GET_MODE_BITSIZE (int_mode) - count, 1)
10684 .exists (&tmode)))
10686 new_rtx = adjust_address_nv (varop, tmode,
10687 BYTES_BIG_ENDIAN ? 0
10688 : count / BITS_PER_UNIT);
10690 varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
10691 : ZERO_EXTEND, int_mode, new_rtx);
10692 count = 0;
10693 continue;
10695 break;
10697 case SUBREG:
10698 /* The following rules apply only to scalars. */
10699 if (shift_mode != shift_unit_mode)
10700 break;
10701 int_mode = as_a <scalar_int_mode> (mode);
10702 int_varop_mode = as_a <scalar_int_mode> (GET_MODE (varop));
10704 /* If VAROP is a SUBREG, strip it as long as the inner operand has
10705 the same number of words as what we've seen so far. Then store
10706 the widest mode in MODE. */
10707 if (subreg_lowpart_p (varop)
10708 && is_int_mode (GET_MODE (SUBREG_REG (varop)), &inner_mode)
10709 && GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (int_varop_mode)
10710 && (CEIL (GET_MODE_SIZE (inner_mode), UNITS_PER_WORD)
10711 == CEIL (GET_MODE_SIZE (int_mode), UNITS_PER_WORD))
10712 && GET_MODE_CLASS (int_varop_mode) == MODE_INT)
10714 varop = SUBREG_REG (varop);
10715 if (GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (int_mode))
10716 mode = inner_mode;
10717 continue;
10719 break;
10721 case MULT:
10722 /* Some machines use MULT instead of ASHIFT because MULT
10723 is cheaper. But it is still better on those machines to
10724 merge two shifts into one. */
10725 if (CONST_INT_P (XEXP (varop, 1))
10726 && (log2 = exact_log2 (UINTVAL (XEXP (varop, 1)))) >= 0)
10728 rtx log2_rtx = gen_int_shift_amount (GET_MODE (varop), log2);
10729 varop = simplify_gen_binary (ASHIFT, GET_MODE (varop),
10730 XEXP (varop, 0), log2_rtx);
10731 continue;
10733 break;
10735 case UDIV:
10736 /* Similar, for when divides are cheaper. */
10737 if (CONST_INT_P (XEXP (varop, 1))
10738 && (log2 = exact_log2 (UINTVAL (XEXP (varop, 1)))) >= 0)
10740 rtx log2_rtx = gen_int_shift_amount (GET_MODE (varop), log2);
10741 varop = simplify_gen_binary (LSHIFTRT, GET_MODE (varop),
10742 XEXP (varop, 0), log2_rtx);
10743 continue;
10745 break;
10747 case ASHIFTRT:
10748 /* If we are extracting just the sign bit of an arithmetic
10749 right shift, that shift is not needed. However, the sign
10750 bit of a wider mode may be different from what would be
10751 interpreted as the sign bit in a narrower mode, so, if
10752 the result is narrower, don't discard the shift. */
10753 if (code == LSHIFTRT
10754 && count == (GET_MODE_UNIT_BITSIZE (result_mode) - 1)
10755 && (GET_MODE_UNIT_BITSIZE (result_mode)
10756 >= GET_MODE_UNIT_BITSIZE (GET_MODE (varop))))
10758 varop = XEXP (varop, 0);
10759 continue;
10762 /* fall through */
10764 case LSHIFTRT:
10765 case ASHIFT:
10766 case ROTATE:
10767 /* The following rules apply only to scalars. */
10768 if (shift_mode != shift_unit_mode)
10769 break;
10770 int_mode = as_a <scalar_int_mode> (mode);
10771 int_varop_mode = as_a <scalar_int_mode> (GET_MODE (varop));
10772 int_result_mode = as_a <scalar_int_mode> (result_mode);
10774 /* Here we have two nested shifts. The result is usually the
10775 AND of a new shift with a mask. We compute the result below. */
10776 if (CONST_INT_P (XEXP (varop, 1))
10777 && INTVAL (XEXP (varop, 1)) >= 0
10778 && INTVAL (XEXP (varop, 1)) < GET_MODE_PRECISION (int_varop_mode)
10779 && HWI_COMPUTABLE_MODE_P (int_result_mode)
10780 && HWI_COMPUTABLE_MODE_P (int_mode))
10782 enum rtx_code first_code = GET_CODE (varop);
10783 unsigned int first_count = INTVAL (XEXP (varop, 1));
10784 unsigned HOST_WIDE_INT mask;
10785 rtx mask_rtx;
10787 /* We have one common special case. We can't do any merging if
10788 the inner code is an ASHIFTRT of a smaller mode. However, if
10789 we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
10790 with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
10791 we can convert it to
10792 (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0) C3) C2) C1).
10793 This simplifies certain SIGN_EXTEND operations. */
10794 if (code == ASHIFT && first_code == ASHIFTRT
10795 && count == (GET_MODE_PRECISION (int_result_mode)
10796 - GET_MODE_PRECISION (int_varop_mode)))
10798 /* C3 has the low-order C1 bits zero. */
10800 mask = GET_MODE_MASK (int_mode)
10801 & ~((HOST_WIDE_INT_1U << first_count) - 1);
10803 varop = simplify_and_const_int (NULL_RTX, int_result_mode,
10804 XEXP (varop, 0), mask);
10805 varop = simplify_shift_const (NULL_RTX, ASHIFT,
10806 int_result_mode, varop, count);
10807 count = first_count;
10808 code = ASHIFTRT;
10809 continue;
10812 /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
10813 than C1 high-order bits equal to the sign bit, we can convert
10814 this to either an ASHIFT or an ASHIFTRT depending on the
10815 two counts.
10817 We cannot do this if VAROP's mode is not SHIFT_UNIT_MODE. */
10819 if (code == ASHIFTRT && first_code == ASHIFT
10820 && int_varop_mode == shift_unit_mode
10821 && (num_sign_bit_copies (XEXP (varop, 0), shift_unit_mode)
10822 > first_count))
10824 varop = XEXP (varop, 0);
10825 count -= first_count;
10826 if (count < 0)
10828 count = -count;
10829 code = ASHIFT;
10832 continue;
10835 /* There are some cases we can't do. If CODE is ASHIFTRT,
10836 we can only do this if FIRST_CODE is also ASHIFTRT.
10838 We can't do the case when CODE is ROTATE and FIRST_CODE is
10839 ASHIFTRT.
10841 If the mode of this shift is not the mode of the outer shift,
10842 we can't do this if either shift is a right shift or ROTATE.
10844 Finally, we can't do any of these if the mode is too wide
10845 unless the codes are the same.
10847 Handle the case where the shift codes are the same
10848 first. */
10850 if (code == first_code)
10852 if (int_varop_mode != int_result_mode
10853 && (code == ASHIFTRT || code == LSHIFTRT
10854 || code == ROTATE))
10855 break;
10857 count += first_count;
10858 varop = XEXP (varop, 0);
10859 continue;
10862 if (code == ASHIFTRT
10863 || (code == ROTATE && first_code == ASHIFTRT)
10864 || GET_MODE_PRECISION (int_mode) > HOST_BITS_PER_WIDE_INT
10865 || (int_varop_mode != int_result_mode
10866 && (first_code == ASHIFTRT || first_code == LSHIFTRT
10867 || first_code == ROTATE
10868 || code == ROTATE)))
10869 break;
10871 /* To compute the mask to apply after the shift, shift the
10872 nonzero bits of the inner shift the same way the
10873 outer shift will. */
10875 mask_rtx = gen_int_mode (nonzero_bits (varop, int_varop_mode),
10876 int_result_mode);
10877 rtx count_rtx = gen_int_shift_amount (int_result_mode, count);
10878 mask_rtx
10879 = simplify_const_binary_operation (code, int_result_mode,
10880 mask_rtx, count_rtx);
10882 /* Give up if we can't compute an outer operation to use. */
10883 if (mask_rtx == 0
10884 || !CONST_INT_P (mask_rtx)
10885 || ! merge_outer_ops (&outer_op, &outer_const, AND,
10886 INTVAL (mask_rtx),
10887 int_result_mode, &complement_p))
10888 break;
10890 /* If the shifts are in the same direction, we add the
10891 counts. Otherwise, we subtract them. */
10892 if ((code == ASHIFTRT || code == LSHIFTRT)
10893 == (first_code == ASHIFTRT || first_code == LSHIFTRT))
10894 count += first_count;
10895 else
10896 count -= first_count;
10898 /* If COUNT is positive, the new shift is usually CODE,
10899 except for the two exceptions below, in which case it is
10900 FIRST_CODE. If the count is negative, FIRST_CODE should
10901 always be used */
10902 if (count > 0
10903 && ((first_code == ROTATE && code == ASHIFT)
10904 || (first_code == ASHIFTRT && code == LSHIFTRT)))
10905 code = first_code;
10906 else if (count < 0)
10907 code = first_code, count = -count;
10909 varop = XEXP (varop, 0);
10910 continue;
10913 /* If we have (A << B << C) for any shift, we can convert this to
10914 (A << C << B). This wins if A is a constant. Only try this if
10915 B is not a constant. */
10917 else if (GET_CODE (varop) == code
10918 && CONST_INT_P (XEXP (varop, 0))
10919 && !CONST_INT_P (XEXP (varop, 1)))
10921 /* For ((unsigned) (cstULL >> count)) >> cst2 we have to make
10922 sure the result will be masked. See PR70222. */
10923 if (code == LSHIFTRT
10924 && int_mode != int_result_mode
10925 && !merge_outer_ops (&outer_op, &outer_const, AND,
10926 GET_MODE_MASK (int_result_mode)
10927 >> orig_count, int_result_mode,
10928 &complement_p))
10929 break;
10930 /* For ((int) (cstLL >> count)) >> cst2 just give up. Queuing
10931 up outer sign extension (often left and right shift) is
10932 hardly more efficient than the original. See PR70429. */
10933 if (code == ASHIFTRT && int_mode != int_result_mode)
10934 break;
10936 rtx count_rtx = gen_int_shift_amount (int_result_mode, count);
10937 rtx new_rtx = simplify_const_binary_operation (code, int_mode,
10938 XEXP (varop, 0),
10939 count_rtx);
10940 varop = gen_rtx_fmt_ee (code, int_mode, new_rtx, XEXP (varop, 1));
10941 count = 0;
10942 continue;
10944 break;
10946 case NOT:
10947 /* The following rules apply only to scalars. */
10948 if (shift_mode != shift_unit_mode)
10949 break;
10951 /* Make this fit the case below. */
10952 varop = gen_rtx_XOR (mode, XEXP (varop, 0), constm1_rtx);
10953 continue;
10955 case IOR:
10956 case AND:
10957 case XOR:
10958 /* The following rules apply only to scalars. */
10959 if (shift_mode != shift_unit_mode)
10960 break;
10961 int_varop_mode = as_a <scalar_int_mode> (GET_MODE (varop));
10962 int_result_mode = as_a <scalar_int_mode> (result_mode);
10964 /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
10965 with C the size of VAROP - 1 and the shift is logical if
10966 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
10967 we have an (le X 0) operation. If we have an arithmetic shift
10968 and STORE_FLAG_VALUE is 1 or we have a logical shift with
10969 STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation. */
10971 if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
10972 && XEXP (XEXP (varop, 0), 1) == constm1_rtx
10973 && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
10974 && (code == LSHIFTRT || code == ASHIFTRT)
10975 && count == (GET_MODE_PRECISION (int_varop_mode) - 1)
10976 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
10978 count = 0;
10979 varop = gen_rtx_LE (int_varop_mode, XEXP (varop, 1),
10980 const0_rtx);
10982 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
10983 varop = gen_rtx_NEG (int_varop_mode, varop);
10985 continue;
10988 /* If we have (shift (logical)), move the logical to the outside
10989 to allow it to possibly combine with another logical and the
10990 shift to combine with another shift. This also canonicalizes to
10991 what a ZERO_EXTRACT looks like. Also, some machines have
10992 (and (shift)) insns. */
10994 if (CONST_INT_P (XEXP (varop, 1))
10995 /* We can't do this if we have (ashiftrt (xor)) and the
10996 constant has its sign bit set in shift_unit_mode with
10997 shift_unit_mode wider than result_mode. */
10998 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
10999 && int_result_mode != shift_unit_mode
11000 && trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
11001 shift_unit_mode) < 0)
11002 && (new_rtx = simplify_const_binary_operation
11003 (code, int_result_mode,
11004 gen_int_mode (INTVAL (XEXP (varop, 1)), int_result_mode),
11005 gen_int_shift_amount (int_result_mode, count))) != 0
11006 && CONST_INT_P (new_rtx)
11007 && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
11008 INTVAL (new_rtx), int_result_mode,
11009 &complement_p))
11011 varop = XEXP (varop, 0);
11012 continue;
11015 /* If we can't do that, try to simplify the shift in each arm of the
11016 logical expression, make a new logical expression, and apply
11017 the inverse distributive law. This also can't be done for
11018 (ashiftrt (xor)) where we've widened the shift and the constant
11019 changes the sign bit. */
11020 if (CONST_INT_P (XEXP (varop, 1))
11021 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
11022 && int_result_mode != shift_unit_mode
11023 && trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
11024 shift_unit_mode) < 0))
11026 rtx lhs = simplify_shift_const (NULL_RTX, code, shift_unit_mode,
11027 XEXP (varop, 0), count);
11028 rtx rhs = simplify_shift_const (NULL_RTX, code, shift_unit_mode,
11029 XEXP (varop, 1), count);
11031 varop = simplify_gen_binary (GET_CODE (varop), shift_unit_mode,
11032 lhs, rhs);
11033 varop = apply_distributive_law (varop);
11035 count = 0;
11036 continue;
11038 break;
11040 case EQ:
11041 /* The following rules apply only to scalars. */
11042 if (shift_mode != shift_unit_mode)
11043 break;
11044 int_result_mode = as_a <scalar_int_mode> (result_mode);
11046 /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
11047 says that the sign bit can be tested, FOO has mode MODE, C is
11048 GET_MODE_PRECISION (MODE) - 1, and FOO has only its low-order bit
11049 that may be nonzero. */
11050 if (code == LSHIFTRT
11051 && XEXP (varop, 1) == const0_rtx
11052 && GET_MODE (XEXP (varop, 0)) == int_result_mode
11053 && count == (GET_MODE_PRECISION (int_result_mode) - 1)
11054 && HWI_COMPUTABLE_MODE_P (int_result_mode)
11055 && STORE_FLAG_VALUE == -1
11056 && nonzero_bits (XEXP (varop, 0), int_result_mode) == 1
11057 && merge_outer_ops (&outer_op, &outer_const, XOR, 1,
11058 int_result_mode, &complement_p))
11060 varop = XEXP (varop, 0);
11061 count = 0;
11062 continue;
11064 break;
11066 case NEG:
11067 /* The following rules apply only to scalars. */
11068 if (shift_mode != shift_unit_mode)
11069 break;
11070 int_result_mode = as_a <scalar_int_mode> (result_mode);
11072 /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
11073 than the number of bits in the mode is equivalent to A. */
11074 if (code == LSHIFTRT
11075 && count == (GET_MODE_PRECISION (int_result_mode) - 1)
11076 && nonzero_bits (XEXP (varop, 0), int_result_mode) == 1)
11078 varop = XEXP (varop, 0);
11079 count = 0;
11080 continue;
11083 /* NEG commutes with ASHIFT since it is multiplication. Move the
11084 NEG outside to allow shifts to combine. */
11085 if (code == ASHIFT
11086 && merge_outer_ops (&outer_op, &outer_const, NEG, 0,
11087 int_result_mode, &complement_p))
11089 varop = XEXP (varop, 0);
11090 continue;
11092 break;
11094 case PLUS:
11095 /* The following rules apply only to scalars. */
11096 if (shift_mode != shift_unit_mode)
11097 break;
11098 int_result_mode = as_a <scalar_int_mode> (result_mode);
11100 /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
11101 is one less than the number of bits in the mode is
11102 equivalent to (xor A 1). */
11103 if (code == LSHIFTRT
11104 && count == (GET_MODE_PRECISION (int_result_mode) - 1)
11105 && XEXP (varop, 1) == constm1_rtx
11106 && nonzero_bits (XEXP (varop, 0), int_result_mode) == 1
11107 && merge_outer_ops (&outer_op, &outer_const, XOR, 1,
11108 int_result_mode, &complement_p))
11110 count = 0;
11111 varop = XEXP (varop, 0);
11112 continue;
11115 /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
11116 that might be nonzero in BAR are those being shifted out and those
11117 bits are known zero in FOO, we can replace the PLUS with FOO.
11118 Similarly in the other operand order. This code occurs when
11119 we are computing the size of a variable-size array. */
11121 if ((code == ASHIFTRT || code == LSHIFTRT)
11122 && count < HOST_BITS_PER_WIDE_INT
11123 && nonzero_bits (XEXP (varop, 1), int_result_mode) >> count == 0
11124 && (nonzero_bits (XEXP (varop, 1), int_result_mode)
11125 & nonzero_bits (XEXP (varop, 0), int_result_mode)) == 0)
11127 varop = XEXP (varop, 0);
11128 continue;
11130 else if ((code == ASHIFTRT || code == LSHIFTRT)
11131 && count < HOST_BITS_PER_WIDE_INT
11132 && HWI_COMPUTABLE_MODE_P (int_result_mode)
11133 && (nonzero_bits (XEXP (varop, 0), int_result_mode)
11134 >> count) == 0
11135 && (nonzero_bits (XEXP (varop, 0), int_result_mode)
11136 & nonzero_bits (XEXP (varop, 1), int_result_mode)) == 0)
11138 varop = XEXP (varop, 1);
11139 continue;
11142 /* (ashift (plus foo C) N) is (plus (ashift foo N) C'). */
11143 if (code == ASHIFT
11144 && CONST_INT_P (XEXP (varop, 1))
11145 && (new_rtx = simplify_const_binary_operation
11146 (ASHIFT, int_result_mode,
11147 gen_int_mode (INTVAL (XEXP (varop, 1)), int_result_mode),
11148 gen_int_shift_amount (int_result_mode, count))) != 0
11149 && CONST_INT_P (new_rtx)
11150 && merge_outer_ops (&outer_op, &outer_const, PLUS,
11151 INTVAL (new_rtx), int_result_mode,
11152 &complement_p))
11154 varop = XEXP (varop, 0);
11155 continue;
11158 /* Check for 'PLUS signbit', which is the canonical form of 'XOR
11159 signbit', and attempt to change the PLUS to an XOR and move it to
11160 the outer operation as is done above in the AND/IOR/XOR case
11161 leg for shift(logical). See details in logical handling above
11162 for reasoning in doing so. */
11163 if (code == LSHIFTRT
11164 && CONST_INT_P (XEXP (varop, 1))
11165 && mode_signbit_p (int_result_mode, XEXP (varop, 1))
11166 && (new_rtx = simplify_const_binary_operation
11167 (code, int_result_mode,
11168 gen_int_mode (INTVAL (XEXP (varop, 1)), int_result_mode),
11169 gen_int_shift_amount (int_result_mode, count))) != 0
11170 && CONST_INT_P (new_rtx)
11171 && merge_outer_ops (&outer_op, &outer_const, XOR,
11172 INTVAL (new_rtx), int_result_mode,
11173 &complement_p))
11175 varop = XEXP (varop, 0);
11176 continue;
11179 break;
11181 case MINUS:
11182 /* The following rules apply only to scalars. */
11183 if (shift_mode != shift_unit_mode)
11184 break;
11185 int_varop_mode = as_a <scalar_int_mode> (GET_MODE (varop));
11187 /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
11188 with C the size of VAROP - 1 and the shift is logical if
11189 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
11190 we have a (gt X 0) operation. If the shift is arithmetic with
11191 STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
11192 we have a (neg (gt X 0)) operation. */
11194 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
11195 && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
11196 && count == (GET_MODE_PRECISION (int_varop_mode) - 1)
11197 && (code == LSHIFTRT || code == ASHIFTRT)
11198 && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
11199 && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
11200 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
11202 count = 0;
11203 varop = gen_rtx_GT (int_varop_mode, XEXP (varop, 1),
11204 const0_rtx);
11206 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
11207 varop = gen_rtx_NEG (int_varop_mode, varop);
11209 continue;
11211 break;
11213 case TRUNCATE:
11214 /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
11215 if the truncate does not affect the value. */
11216 if (code == LSHIFTRT
11217 && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
11218 && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
11219 && (INTVAL (XEXP (XEXP (varop, 0), 1))
11220 >= (GET_MODE_UNIT_PRECISION (GET_MODE (XEXP (varop, 0)))
11221 - GET_MODE_UNIT_PRECISION (GET_MODE (varop)))))
11223 rtx varop_inner = XEXP (varop, 0);
11224 int new_count = count + INTVAL (XEXP (varop_inner, 1));
11225 rtx new_count_rtx = gen_int_shift_amount (GET_MODE (varop_inner),
11226 new_count);
11227 varop_inner = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
11228 XEXP (varop_inner, 0),
11229 new_count_rtx);
11230 varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
11231 count = 0;
11232 continue;
11234 break;
11236 default:
11237 break;
11240 break;
11243 shift_mode = result_mode;
11244 if (shift_mode != mode)
11246 /* We only change the modes of scalar shifts. */
11247 int_mode = as_a <scalar_int_mode> (mode);
11248 int_result_mode = as_a <scalar_int_mode> (result_mode);
11249 shift_mode = try_widen_shift_mode (code, varop, count, int_result_mode,
11250 int_mode, outer_op, outer_const);
11253 /* We have now finished analyzing the shift. The result should be
11254 a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places. If
11255 OUTER_OP is non-UNKNOWN, it is an operation that needs to be applied
11256 to the result of the shift. OUTER_CONST is the relevant constant,
11257 but we must turn off all bits turned off in the shift. */
11259 if (outer_op == UNKNOWN
11260 && orig_code == code && orig_count == count
11261 && varop == orig_varop
11262 && shift_mode == GET_MODE (varop))
11263 return NULL_RTX;
11265 /* Make a SUBREG if necessary. If we can't make it, fail. */
11266 varop = gen_lowpart (shift_mode, varop);
11267 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
11268 return NULL_RTX;
11270 /* If we have an outer operation and we just made a shift, it is
11271 possible that we could have simplified the shift were it not
11272 for the outer operation. So try to do the simplification
11273 recursively. */
11275 if (outer_op != UNKNOWN)
11276 x = simplify_shift_const_1 (code, shift_mode, varop, count);
11277 else
11278 x = NULL_RTX;
11280 if (x == NULL_RTX)
11281 x = simplify_gen_binary (code, shift_mode, varop,
11282 gen_int_shift_amount (shift_mode, count));
11284 /* If we were doing an LSHIFTRT in a wider mode than it was originally,
11285 turn off all the bits that the shift would have turned off. */
11286 if (orig_code == LSHIFTRT && result_mode != shift_mode)
11287 /* We only change the modes of scalar shifts. */
11288 x = simplify_and_const_int (NULL_RTX, as_a <scalar_int_mode> (shift_mode),
11289 x, GET_MODE_MASK (result_mode) >> orig_count);
11291 /* Do the remainder of the processing in RESULT_MODE. */
11292 x = gen_lowpart_or_truncate (result_mode, x);
11294 /* If COMPLEMENT_P is set, we have to complement X before doing the outer
11295 operation. */
11296 if (complement_p)
11297 x = simplify_gen_unary (NOT, result_mode, x, result_mode);
11299 if (outer_op != UNKNOWN)
11301 int_result_mode = as_a <scalar_int_mode> (result_mode);
11303 if (GET_RTX_CLASS (outer_op) != RTX_UNARY
11304 && GET_MODE_PRECISION (int_result_mode) < HOST_BITS_PER_WIDE_INT)
11305 outer_const = trunc_int_for_mode (outer_const, int_result_mode);
11307 if (outer_op == AND)
11308 x = simplify_and_const_int (NULL_RTX, int_result_mode, x, outer_const);
11309 else if (outer_op == SET)
11311 /* This means that we have determined that the result is
11312 equivalent to a constant. This should be rare. */
11313 if (!side_effects_p (x))
11314 x = GEN_INT (outer_const);
11316 else if (GET_RTX_CLASS (outer_op) == RTX_UNARY)
11317 x = simplify_gen_unary (outer_op, int_result_mode, x, int_result_mode);
11318 else
11319 x = simplify_gen_binary (outer_op, int_result_mode, x,
11320 GEN_INT (outer_const));
11323 return x;
11326 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
11327 The result of the shift is RESULT_MODE. If we cannot simplify it,
11328 return X or, if it is NULL, synthesize the expression with
11329 simplify_gen_binary. Otherwise, return a simplified value.
11331 The shift is normally computed in the widest mode we find in VAROP, as
11332 long as it isn't a different number of words than RESULT_MODE. Exceptions
11333 are ASHIFTRT and ROTATE, which are always done in their original mode. */
11335 static rtx
11336 simplify_shift_const (rtx x, enum rtx_code code, machine_mode result_mode,
11337 rtx varop, int count)
11339 rtx tem = simplify_shift_const_1 (code, result_mode, varop, count);
11340 if (tem)
11341 return tem;
11343 if (!x)
11344 x = simplify_gen_binary (code, GET_MODE (varop), varop,
11345 gen_int_shift_amount (GET_MODE (varop), count));
11346 if (GET_MODE (x) != result_mode)
11347 x = gen_lowpart (result_mode, x);
11348 return x;
11352 /* A subroutine of recog_for_combine. See there for arguments and
11353 return value. */
11355 static int
11356 recog_for_combine_1 (rtx *pnewpat, rtx_insn *insn, rtx *pnotes)
11358 rtx pat = *pnewpat;
11359 rtx pat_without_clobbers;
11360 int insn_code_number;
11361 int num_clobbers_to_add = 0;
11362 int i;
11363 rtx notes = NULL_RTX;
11364 rtx old_notes, old_pat;
11365 int old_icode;
11367 /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
11368 we use to indicate that something didn't match. If we find such a
11369 thing, force rejection. */
11370 if (GET_CODE (pat) == PARALLEL)
11371 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
11372 if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
11373 && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
11374 return -1;
11376 old_pat = PATTERN (insn);
11377 old_notes = REG_NOTES (insn);
11378 PATTERN (insn) = pat;
11379 REG_NOTES (insn) = NULL_RTX;
11381 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
11382 if (dump_file && (dump_flags & TDF_DETAILS))
11384 if (insn_code_number < 0)
11385 fputs ("Failed to match this instruction:\n", dump_file);
11386 else
11387 fputs ("Successfully matched this instruction:\n", dump_file);
11388 print_rtl_single (dump_file, pat);
11391 /* If it isn't, there is the possibility that we previously had an insn
11392 that clobbered some register as a side effect, but the combined
11393 insn doesn't need to do that. So try once more without the clobbers
11394 unless this represents an ASM insn. */
11396 if (insn_code_number < 0 && ! check_asm_operands (pat)
11397 && GET_CODE (pat) == PARALLEL)
11399 int pos;
11401 for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
11402 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
11404 if (i != pos)
11405 SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
11406 pos++;
11409 SUBST_INT (XVECLEN (pat, 0), pos);
11411 if (pos == 1)
11412 pat = XVECEXP (pat, 0, 0);
11414 PATTERN (insn) = pat;
11415 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
11416 if (dump_file && (dump_flags & TDF_DETAILS))
11418 if (insn_code_number < 0)
11419 fputs ("Failed to match this instruction:\n", dump_file);
11420 else
11421 fputs ("Successfully matched this instruction:\n", dump_file);
11422 print_rtl_single (dump_file, pat);
11426 pat_without_clobbers = pat;
11428 PATTERN (insn) = old_pat;
11429 REG_NOTES (insn) = old_notes;
11431 /* Recognize all noop sets, these will be killed by followup pass. */
11432 if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
11433 insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
11435 /* If we had any clobbers to add, make a new pattern than contains
11436 them. Then check to make sure that all of them are dead. */
11437 if (num_clobbers_to_add)
11439 rtx newpat = gen_rtx_PARALLEL (VOIDmode,
11440 rtvec_alloc (GET_CODE (pat) == PARALLEL
11441 ? (XVECLEN (pat, 0)
11442 + num_clobbers_to_add)
11443 : num_clobbers_to_add + 1));
11445 if (GET_CODE (pat) == PARALLEL)
11446 for (i = 0; i < XVECLEN (pat, 0); i++)
11447 XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
11448 else
11449 XVECEXP (newpat, 0, 0) = pat;
11451 add_clobbers (newpat, insn_code_number);
11453 for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
11454 i < XVECLEN (newpat, 0); i++)
11456 if (REG_P (XEXP (XVECEXP (newpat, 0, i), 0))
11457 && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
11458 return -1;
11459 if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) != SCRATCH)
11461 gcc_assert (REG_P (XEXP (XVECEXP (newpat, 0, i), 0)));
11462 notes = alloc_reg_note (REG_UNUSED,
11463 XEXP (XVECEXP (newpat, 0, i), 0), notes);
11466 pat = newpat;
11469 if (insn_code_number >= 0
11470 && insn_code_number != NOOP_MOVE_INSN_CODE)
11472 old_pat = PATTERN (insn);
11473 old_notes = REG_NOTES (insn);
11474 old_icode = INSN_CODE (insn);
11475 PATTERN (insn) = pat;
11476 REG_NOTES (insn) = notes;
11477 INSN_CODE (insn) = insn_code_number;
11479 /* Allow targets to reject combined insn. */
11480 if (!targetm.legitimate_combined_insn (insn))
11482 if (dump_file && (dump_flags & TDF_DETAILS))
11483 fputs ("Instruction not appropriate for target.",
11484 dump_file);
11486 /* Callers expect recog_for_combine to strip
11487 clobbers from the pattern on failure. */
11488 pat = pat_without_clobbers;
11489 notes = NULL_RTX;
11491 insn_code_number = -1;
11494 PATTERN (insn) = old_pat;
11495 REG_NOTES (insn) = old_notes;
11496 INSN_CODE (insn) = old_icode;
11499 *pnewpat = pat;
11500 *pnotes = notes;
11502 return insn_code_number;
11505 /* Change every ZERO_EXTRACT and ZERO_EXTEND of a SUBREG that can be
11506 expressed as an AND and maybe an LSHIFTRT, to that formulation.
11507 Return whether anything was so changed. */
11509 static bool
11510 change_zero_ext (rtx pat)
11512 bool changed = false;
11513 rtx *src = &SET_SRC (pat);
11515 subrtx_ptr_iterator::array_type array;
11516 FOR_EACH_SUBRTX_PTR (iter, array, src, NONCONST)
11518 rtx x = **iter;
11519 scalar_int_mode mode, inner_mode;
11520 if (!is_a <scalar_int_mode> (GET_MODE (x), &mode))
11521 continue;
11522 int size;
11524 if (GET_CODE (x) == ZERO_EXTRACT
11525 && CONST_INT_P (XEXP (x, 1))
11526 && CONST_INT_P (XEXP (x, 2))
11527 && is_a <scalar_int_mode> (GET_MODE (XEXP (x, 0)), &inner_mode)
11528 && GET_MODE_PRECISION (inner_mode) <= GET_MODE_PRECISION (mode))
11530 size = INTVAL (XEXP (x, 1));
11532 int start = INTVAL (XEXP (x, 2));
11533 if (BITS_BIG_ENDIAN)
11534 start = GET_MODE_PRECISION (inner_mode) - size - start;
11536 if (start != 0)
11537 x = gen_rtx_LSHIFTRT (inner_mode, XEXP (x, 0),
11538 gen_int_shift_amount (inner_mode, start));
11539 else
11540 x = XEXP (x, 0);
11542 if (mode != inner_mode)
11544 if (REG_P (x) && HARD_REGISTER_P (x)
11545 && !can_change_dest_mode (x, 0, mode))
11546 continue;
11548 x = gen_lowpart_SUBREG (mode, x);
11551 else if (GET_CODE (x) == ZERO_EXTEND
11552 && GET_CODE (XEXP (x, 0)) == SUBREG
11553 && SCALAR_INT_MODE_P (GET_MODE (SUBREG_REG (XEXP (x, 0))))
11554 && !paradoxical_subreg_p (XEXP (x, 0))
11555 && subreg_lowpart_p (XEXP (x, 0)))
11557 inner_mode = as_a <scalar_int_mode> (GET_MODE (XEXP (x, 0)));
11558 size = GET_MODE_PRECISION (inner_mode);
11559 x = SUBREG_REG (XEXP (x, 0));
11560 if (GET_MODE (x) != mode)
11562 if (REG_P (x) && HARD_REGISTER_P (x)
11563 && !can_change_dest_mode (x, 0, mode))
11564 continue;
11566 x = gen_lowpart_SUBREG (mode, x);
11569 else if (GET_CODE (x) == ZERO_EXTEND
11570 && REG_P (XEXP (x, 0))
11571 && HARD_REGISTER_P (XEXP (x, 0))
11572 && can_change_dest_mode (XEXP (x, 0), 0, mode))
11574 inner_mode = as_a <scalar_int_mode> (GET_MODE (XEXP (x, 0)));
11575 size = GET_MODE_PRECISION (inner_mode);
11576 x = gen_rtx_REG (mode, REGNO (XEXP (x, 0)));
11578 else
11579 continue;
11581 if (!(GET_CODE (x) == LSHIFTRT
11582 && CONST_INT_P (XEXP (x, 1))
11583 && size + INTVAL (XEXP (x, 1)) == GET_MODE_PRECISION (mode)))
11585 wide_int mask = wi::mask (size, false, GET_MODE_PRECISION (mode));
11586 x = gen_rtx_AND (mode, x, immed_wide_int_const (mask, mode));
11589 SUBST (**iter, x);
11590 changed = true;
11593 if (changed)
11594 FOR_EACH_SUBRTX_PTR (iter, array, src, NONCONST)
11595 maybe_swap_commutative_operands (**iter);
11597 rtx *dst = &SET_DEST (pat);
11598 scalar_int_mode mode;
11599 if (GET_CODE (*dst) == ZERO_EXTRACT
11600 && REG_P (XEXP (*dst, 0))
11601 && is_a <scalar_int_mode> (GET_MODE (XEXP (*dst, 0)), &mode)
11602 && CONST_INT_P (XEXP (*dst, 1))
11603 && CONST_INT_P (XEXP (*dst, 2)))
11605 rtx reg = XEXP (*dst, 0);
11606 int width = INTVAL (XEXP (*dst, 1));
11607 int offset = INTVAL (XEXP (*dst, 2));
11608 int reg_width = GET_MODE_PRECISION (mode);
11609 if (BITS_BIG_ENDIAN)
11610 offset = reg_width - width - offset;
11612 rtx x, y, z, w;
11613 wide_int mask = wi::shifted_mask (offset, width, true, reg_width);
11614 wide_int mask2 = wi::shifted_mask (offset, width, false, reg_width);
11615 x = gen_rtx_AND (mode, reg, immed_wide_int_const (mask, mode));
11616 if (offset)
11617 y = gen_rtx_ASHIFT (mode, SET_SRC (pat), GEN_INT (offset));
11618 else
11619 y = SET_SRC (pat);
11620 z = gen_rtx_AND (mode, y, immed_wide_int_const (mask2, mode));
11621 w = gen_rtx_IOR (mode, x, z);
11622 SUBST (SET_DEST (pat), reg);
11623 SUBST (SET_SRC (pat), w);
11625 changed = true;
11628 return changed;
11631 /* Like recog, but we receive the address of a pointer to a new pattern.
11632 We try to match the rtx that the pointer points to.
11633 If that fails, we may try to modify or replace the pattern,
11634 storing the replacement into the same pointer object.
11636 Modifications include deletion or addition of CLOBBERs. If the
11637 instruction will still not match, we change ZERO_EXTEND and ZERO_EXTRACT
11638 to the equivalent AND and perhaps LSHIFTRT patterns, and try with that
11639 (and undo if that fails).
11641 PNOTES is a pointer to a location where any REG_UNUSED notes added for
11642 the CLOBBERs are placed.
11644 The value is the final insn code from the pattern ultimately matched,
11645 or -1. */
11647 static int
11648 recog_for_combine (rtx *pnewpat, rtx_insn *insn, rtx *pnotes)
11650 rtx pat = *pnewpat;
11651 int insn_code_number = recog_for_combine_1 (pnewpat, insn, pnotes);
11652 if (insn_code_number >= 0 || check_asm_operands (pat))
11653 return insn_code_number;
11655 void *marker = get_undo_marker ();
11656 bool changed = false;
11658 if (GET_CODE (pat) == SET)
11659 changed = change_zero_ext (pat);
11660 else if (GET_CODE (pat) == PARALLEL)
11662 int i;
11663 for (i = 0; i < XVECLEN (pat, 0); i++)
11665 rtx set = XVECEXP (pat, 0, i);
11666 if (GET_CODE (set) == SET)
11667 changed |= change_zero_ext (set);
11671 if (changed)
11673 insn_code_number = recog_for_combine_1 (pnewpat, insn, pnotes);
11675 if (insn_code_number < 0)
11676 undo_to_marker (marker);
11679 return insn_code_number;
11682 /* Like gen_lowpart_general but for use by combine. In combine it
11683 is not possible to create any new pseudoregs. However, it is
11684 safe to create invalid memory addresses, because combine will
11685 try to recognize them and all they will do is make the combine
11686 attempt fail.
11688 If for some reason this cannot do its job, an rtx
11689 (clobber (const_int 0)) is returned.
11690 An insn containing that will not be recognized. */
11692 static rtx
11693 gen_lowpart_for_combine (machine_mode omode, rtx x)
11695 machine_mode imode = GET_MODE (x);
11696 rtx result;
11698 if (omode == imode)
11699 return x;
11701 /* We can only support MODE being wider than a word if X is a
11702 constant integer or has a mode the same size. */
11703 if (maybe_gt (GET_MODE_SIZE (omode), UNITS_PER_WORD)
11704 && ! (CONST_SCALAR_INT_P (x)
11705 || known_eq (GET_MODE_SIZE (imode), GET_MODE_SIZE (omode))))
11706 goto fail;
11708 /* X might be a paradoxical (subreg (mem)). In that case, gen_lowpart
11709 won't know what to do. So we will strip off the SUBREG here and
11710 process normally. */
11711 if (GET_CODE (x) == SUBREG && MEM_P (SUBREG_REG (x)))
11713 x = SUBREG_REG (x);
11715 /* For use in case we fall down into the address adjustments
11716 further below, we need to adjust the known mode and size of
11717 x; imode and isize, since we just adjusted x. */
11718 imode = GET_MODE (x);
11720 if (imode == omode)
11721 return x;
11724 result = gen_lowpart_common (omode, x);
11726 if (result)
11727 return result;
11729 if (MEM_P (x))
11731 /* Refuse to work on a volatile memory ref or one with a mode-dependent
11732 address. */
11733 if (MEM_VOLATILE_P (x)
11734 || mode_dependent_address_p (XEXP (x, 0), MEM_ADDR_SPACE (x)))
11735 goto fail;
11737 /* If we want to refer to something bigger than the original memref,
11738 generate a paradoxical subreg instead. That will force a reload
11739 of the original memref X. */
11740 if (paradoxical_subreg_p (omode, imode))
11741 return gen_rtx_SUBREG (omode, x, 0);
11743 poly_int64 offset = byte_lowpart_offset (omode, imode);
11744 return adjust_address_nv (x, omode, offset);
11747 /* If X is a comparison operator, rewrite it in a new mode. This
11748 probably won't match, but may allow further simplifications. */
11749 else if (COMPARISON_P (x))
11750 return gen_rtx_fmt_ee (GET_CODE (x), omode, XEXP (x, 0), XEXP (x, 1));
11752 /* If we couldn't simplify X any other way, just enclose it in a
11753 SUBREG. Normally, this SUBREG won't match, but some patterns may
11754 include an explicit SUBREG or we may simplify it further in combine. */
11755 else
11757 rtx res;
11759 if (imode == VOIDmode)
11761 imode = int_mode_for_mode (omode).require ();
11762 x = gen_lowpart_common (imode, x);
11763 if (x == NULL)
11764 goto fail;
11766 res = lowpart_subreg (omode, x, imode);
11767 if (res)
11768 return res;
11771 fail:
11772 return gen_rtx_CLOBBER (omode, const0_rtx);
11775 /* Try to simplify a comparison between OP0 and a constant OP1,
11776 where CODE is the comparison code that will be tested, into a
11777 (CODE OP0 const0_rtx) form.
11779 The result is a possibly different comparison code to use.
11780 *POP1 may be updated. */
11782 static enum rtx_code
11783 simplify_compare_const (enum rtx_code code, machine_mode mode,
11784 rtx op0, rtx *pop1)
11786 scalar_int_mode int_mode;
11787 HOST_WIDE_INT const_op = INTVAL (*pop1);
11789 /* Get the constant we are comparing against and turn off all bits
11790 not on in our mode. */
11791 if (mode != VOIDmode)
11792 const_op = trunc_int_for_mode (const_op, mode);
11794 /* If we are comparing against a constant power of two and the value
11795 being compared can only have that single bit nonzero (e.g., it was
11796 `and'ed with that bit), we can replace this with a comparison
11797 with zero. */
11798 if (const_op
11799 && (code == EQ || code == NE || code == GE || code == GEU
11800 || code == LT || code == LTU)
11801 && is_a <scalar_int_mode> (mode, &int_mode)
11802 && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11803 && pow2p_hwi (const_op & GET_MODE_MASK (int_mode))
11804 && (nonzero_bits (op0, int_mode)
11805 == (unsigned HOST_WIDE_INT) (const_op & GET_MODE_MASK (int_mode))))
11807 code = (code == EQ || code == GE || code == GEU ? NE : EQ);
11808 const_op = 0;
11811 /* Similarly, if we are comparing a value known to be either -1 or
11812 0 with -1, change it to the opposite comparison against zero. */
11813 if (const_op == -1
11814 && (code == EQ || code == NE || code == GT || code == LE
11815 || code == GEU || code == LTU)
11816 && is_a <scalar_int_mode> (mode, &int_mode)
11817 && num_sign_bit_copies (op0, int_mode) == GET_MODE_PRECISION (int_mode))
11819 code = (code == EQ || code == LE || code == GEU ? NE : EQ);
11820 const_op = 0;
11823 /* Do some canonicalizations based on the comparison code. We prefer
11824 comparisons against zero and then prefer equality comparisons.
11825 If we can reduce the size of a constant, we will do that too. */
11826 switch (code)
11828 case LT:
11829 /* < C is equivalent to <= (C - 1) */
11830 if (const_op > 0)
11832 const_op -= 1;
11833 code = LE;
11834 /* ... fall through to LE case below. */
11835 gcc_fallthrough ();
11837 else
11838 break;
11840 case LE:
11841 /* <= C is equivalent to < (C + 1); we do this for C < 0 */
11842 if (const_op < 0)
11844 const_op += 1;
11845 code = LT;
11848 /* If we are doing a <= 0 comparison on a value known to have
11849 a zero sign bit, we can replace this with == 0. */
11850 else if (const_op == 0
11851 && is_a <scalar_int_mode> (mode, &int_mode)
11852 && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11853 && (nonzero_bits (op0, int_mode)
11854 & (HOST_WIDE_INT_1U << (GET_MODE_PRECISION (int_mode) - 1)))
11855 == 0)
11856 code = EQ;
11857 break;
11859 case GE:
11860 /* >= C is equivalent to > (C - 1). */
11861 if (const_op > 0)
11863 const_op -= 1;
11864 code = GT;
11865 /* ... fall through to GT below. */
11866 gcc_fallthrough ();
11868 else
11869 break;
11871 case GT:
11872 /* > C is equivalent to >= (C + 1); we do this for C < 0. */
11873 if (const_op < 0)
11875 const_op += 1;
11876 code = GE;
11879 /* If we are doing a > 0 comparison on a value known to have
11880 a zero sign bit, we can replace this with != 0. */
11881 else if (const_op == 0
11882 && is_a <scalar_int_mode> (mode, &int_mode)
11883 && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11884 && (nonzero_bits (op0, int_mode)
11885 & (HOST_WIDE_INT_1U << (GET_MODE_PRECISION (int_mode) - 1)))
11886 == 0)
11887 code = NE;
11888 break;
11890 case LTU:
11891 /* < C is equivalent to <= (C - 1). */
11892 if (const_op > 0)
11894 const_op -= 1;
11895 code = LEU;
11896 /* ... fall through ... */
11897 gcc_fallthrough ();
11899 /* (unsigned) < 0x80000000 is equivalent to >= 0. */
11900 else if (is_a <scalar_int_mode> (mode, &int_mode)
11901 && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11902 && ((unsigned HOST_WIDE_INT) const_op
11903 == HOST_WIDE_INT_1U << (GET_MODE_PRECISION (int_mode) - 1)))
11905 const_op = 0;
11906 code = GE;
11907 break;
11909 else
11910 break;
11912 case LEU:
11913 /* unsigned <= 0 is equivalent to == 0 */
11914 if (const_op == 0)
11915 code = EQ;
11916 /* (unsigned) <= 0x7fffffff is equivalent to >= 0. */
11917 else if (is_a <scalar_int_mode> (mode, &int_mode)
11918 && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11919 && ((unsigned HOST_WIDE_INT) const_op
11920 == ((HOST_WIDE_INT_1U
11921 << (GET_MODE_PRECISION (int_mode) - 1)) - 1)))
11923 const_op = 0;
11924 code = GE;
11926 break;
11928 case GEU:
11929 /* >= C is equivalent to > (C - 1). */
11930 if (const_op > 1)
11932 const_op -= 1;
11933 code = GTU;
11934 /* ... fall through ... */
11935 gcc_fallthrough ();
11938 /* (unsigned) >= 0x80000000 is equivalent to < 0. */
11939 else if (is_a <scalar_int_mode> (mode, &int_mode)
11940 && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11941 && ((unsigned HOST_WIDE_INT) const_op
11942 == HOST_WIDE_INT_1U << (GET_MODE_PRECISION (int_mode) - 1)))
11944 const_op = 0;
11945 code = LT;
11946 break;
11948 else
11949 break;
11951 case GTU:
11952 /* unsigned > 0 is equivalent to != 0 */
11953 if (const_op == 0)
11954 code = NE;
11955 /* (unsigned) > 0x7fffffff is equivalent to < 0. */
11956 else if (is_a <scalar_int_mode> (mode, &int_mode)
11957 && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11958 && ((unsigned HOST_WIDE_INT) const_op
11959 == (HOST_WIDE_INT_1U
11960 << (GET_MODE_PRECISION (int_mode) - 1)) - 1))
11962 const_op = 0;
11963 code = LT;
11965 break;
11967 default:
11968 break;
11971 *pop1 = GEN_INT (const_op);
11972 return code;
11975 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
11976 comparison code that will be tested.
11978 The result is a possibly different comparison code to use. *POP0 and
11979 *POP1 may be updated.
11981 It is possible that we might detect that a comparison is either always
11982 true or always false. However, we do not perform general constant
11983 folding in combine, so this knowledge isn't useful. Such tautologies
11984 should have been detected earlier. Hence we ignore all such cases. */
11986 static enum rtx_code
11987 simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
11989 rtx op0 = *pop0;
11990 rtx op1 = *pop1;
11991 rtx tem, tem1;
11992 int i;
11993 scalar_int_mode mode, inner_mode, tmode;
11994 opt_scalar_int_mode tmode_iter;
11996 /* Try a few ways of applying the same transformation to both operands. */
11997 while (1)
11999 /* The test below this one won't handle SIGN_EXTENDs on these machines,
12000 so check specially. */
12001 if (!WORD_REGISTER_OPERATIONS
12002 && code != GTU && code != GEU && code != LTU && code != LEU
12003 && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
12004 && GET_CODE (XEXP (op0, 0)) == ASHIFT
12005 && GET_CODE (XEXP (op1, 0)) == ASHIFT
12006 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
12007 && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
12008 && is_a <scalar_int_mode> (GET_MODE (op0), &mode)
12009 && (is_a <scalar_int_mode>
12010 (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))), &inner_mode))
12011 && inner_mode == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0)))
12012 && CONST_INT_P (XEXP (op0, 1))
12013 && XEXP (op0, 1) == XEXP (op1, 1)
12014 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
12015 && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
12016 && (INTVAL (XEXP (op0, 1))
12017 == (GET_MODE_PRECISION (mode)
12018 - GET_MODE_PRECISION (inner_mode))))
12020 op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
12021 op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
12024 /* If both operands are the same constant shift, see if we can ignore the
12025 shift. We can if the shift is a rotate or if the bits shifted out of
12026 this shift are known to be zero for both inputs and if the type of
12027 comparison is compatible with the shift. */
12028 if (GET_CODE (op0) == GET_CODE (op1)
12029 && HWI_COMPUTABLE_MODE_P (GET_MODE (op0))
12030 && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
12031 || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
12032 && (code != GT && code != LT && code != GE && code != LE))
12033 || (GET_CODE (op0) == ASHIFTRT
12034 && (code != GTU && code != LTU
12035 && code != GEU && code != LEU)))
12036 && CONST_INT_P (XEXP (op0, 1))
12037 && INTVAL (XEXP (op0, 1)) >= 0
12038 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
12039 && XEXP (op0, 1) == XEXP (op1, 1))
12041 machine_mode mode = GET_MODE (op0);
12042 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
12043 int shift_count = INTVAL (XEXP (op0, 1));
12045 if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
12046 mask &= (mask >> shift_count) << shift_count;
12047 else if (GET_CODE (op0) == ASHIFT)
12048 mask = (mask & (mask << shift_count)) >> shift_count;
12050 if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
12051 && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
12052 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
12053 else
12054 break;
12057 /* If both operands are AND's of a paradoxical SUBREG by constant, the
12058 SUBREGs are of the same mode, and, in both cases, the AND would
12059 be redundant if the comparison was done in the narrower mode,
12060 do the comparison in the narrower mode (e.g., we are AND'ing with 1
12061 and the operand's possibly nonzero bits are 0xffffff01; in that case
12062 if we only care about QImode, we don't need the AND). This case
12063 occurs if the output mode of an scc insn is not SImode and
12064 STORE_FLAG_VALUE == 1 (e.g., the 386).
12066 Similarly, check for a case where the AND's are ZERO_EXTEND
12067 operations from some narrower mode even though a SUBREG is not
12068 present. */
12070 else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
12071 && CONST_INT_P (XEXP (op0, 1))
12072 && CONST_INT_P (XEXP (op1, 1)))
12074 rtx inner_op0 = XEXP (op0, 0);
12075 rtx inner_op1 = XEXP (op1, 0);
12076 HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
12077 HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
12078 int changed = 0;
12080 if (paradoxical_subreg_p (inner_op0)
12081 && GET_CODE (inner_op1) == SUBREG
12082 && HWI_COMPUTABLE_MODE_P (GET_MODE (SUBREG_REG (inner_op0)))
12083 && (GET_MODE (SUBREG_REG (inner_op0))
12084 == GET_MODE (SUBREG_REG (inner_op1)))
12085 && ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
12086 GET_MODE (SUBREG_REG (inner_op0)))) == 0
12087 && ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
12088 GET_MODE (SUBREG_REG (inner_op1)))) == 0)
12090 op0 = SUBREG_REG (inner_op0);
12091 op1 = SUBREG_REG (inner_op1);
12093 /* The resulting comparison is always unsigned since we masked
12094 off the original sign bit. */
12095 code = unsigned_condition (code);
12097 changed = 1;
12100 else if (c0 == c1)
12101 FOR_EACH_MODE_UNTIL (tmode,
12102 as_a <scalar_int_mode> (GET_MODE (op0)))
12103 if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
12105 op0 = gen_lowpart_or_truncate (tmode, inner_op0);
12106 op1 = gen_lowpart_or_truncate (tmode, inner_op1);
12107 code = unsigned_condition (code);
12108 changed = 1;
12109 break;
12112 if (! changed)
12113 break;
12116 /* If both operands are NOT, we can strip off the outer operation
12117 and adjust the comparison code for swapped operands; similarly for
12118 NEG, except that this must be an equality comparison. */
12119 else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
12120 || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
12121 && (code == EQ || code == NE)))
12122 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
12124 else
12125 break;
12128 /* If the first operand is a constant, swap the operands and adjust the
12129 comparison code appropriately, but don't do this if the second operand
12130 is already a constant integer. */
12131 if (swap_commutative_operands_p (op0, op1))
12133 std::swap (op0, op1);
12134 code = swap_condition (code);
12137 /* We now enter a loop during which we will try to simplify the comparison.
12138 For the most part, we only are concerned with comparisons with zero,
12139 but some things may really be comparisons with zero but not start
12140 out looking that way. */
12142 while (CONST_INT_P (op1))
12144 machine_mode raw_mode = GET_MODE (op0);
12145 scalar_int_mode int_mode;
12146 int equality_comparison_p;
12147 int sign_bit_comparison_p;
12148 int unsigned_comparison_p;
12149 HOST_WIDE_INT const_op;
12151 /* We only want to handle integral modes. This catches VOIDmode,
12152 CCmode, and the floating-point modes. An exception is that we
12153 can handle VOIDmode if OP0 is a COMPARE or a comparison
12154 operation. */
12156 if (GET_MODE_CLASS (raw_mode) != MODE_INT
12157 && ! (raw_mode == VOIDmode
12158 && (GET_CODE (op0) == COMPARE || COMPARISON_P (op0))))
12159 break;
12161 /* Try to simplify the compare to constant, possibly changing the
12162 comparison op, and/or changing op1 to zero. */
12163 code = simplify_compare_const (code, raw_mode, op0, &op1);
12164 const_op = INTVAL (op1);
12166 /* Compute some predicates to simplify code below. */
12168 equality_comparison_p = (code == EQ || code == NE);
12169 sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
12170 unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
12171 || code == GEU);
12173 /* If this is a sign bit comparison and we can do arithmetic in
12174 MODE, say that we will only be needing the sign bit of OP0. */
12175 if (sign_bit_comparison_p
12176 && is_a <scalar_int_mode> (raw_mode, &int_mode)
12177 && HWI_COMPUTABLE_MODE_P (int_mode))
12178 op0 = force_to_mode (op0, int_mode,
12179 HOST_WIDE_INT_1U
12180 << (GET_MODE_PRECISION (int_mode) - 1),
12183 if (COMPARISON_P (op0))
12185 /* We can't do anything if OP0 is a condition code value, rather
12186 than an actual data value. */
12187 if (const_op != 0
12188 || CC0_P (XEXP (op0, 0))
12189 || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
12190 break;
12192 /* Get the two operands being compared. */
12193 if (GET_CODE (XEXP (op0, 0)) == COMPARE)
12194 tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
12195 else
12196 tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
12198 /* Check for the cases where we simply want the result of the
12199 earlier test or the opposite of that result. */
12200 if (code == NE || code == EQ
12201 || (val_signbit_known_set_p (raw_mode, STORE_FLAG_VALUE)
12202 && (code == LT || code == GE)))
12204 enum rtx_code new_code;
12205 if (code == LT || code == NE)
12206 new_code = GET_CODE (op0);
12207 else
12208 new_code = reversed_comparison_code (op0, NULL);
12210 if (new_code != UNKNOWN)
12212 code = new_code;
12213 op0 = tem;
12214 op1 = tem1;
12215 continue;
12218 break;
12221 if (raw_mode == VOIDmode)
12222 break;
12223 scalar_int_mode mode = as_a <scalar_int_mode> (raw_mode);
12225 /* Now try cases based on the opcode of OP0. If none of the cases
12226 does a "continue", we exit this loop immediately after the
12227 switch. */
12229 unsigned int mode_width = GET_MODE_PRECISION (mode);
12230 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
12231 switch (GET_CODE (op0))
12233 case ZERO_EXTRACT:
12234 /* If we are extracting a single bit from a variable position in
12235 a constant that has only a single bit set and are comparing it
12236 with zero, we can convert this into an equality comparison
12237 between the position and the location of the single bit. */
12238 /* Except we can't if SHIFT_COUNT_TRUNCATED is set, since we might
12239 have already reduced the shift count modulo the word size. */
12240 if (!SHIFT_COUNT_TRUNCATED
12241 && CONST_INT_P (XEXP (op0, 0))
12242 && XEXP (op0, 1) == const1_rtx
12243 && equality_comparison_p && const_op == 0
12244 && (i = exact_log2 (UINTVAL (XEXP (op0, 0)))) >= 0)
12246 if (BITS_BIG_ENDIAN)
12247 i = BITS_PER_WORD - 1 - i;
12249 op0 = XEXP (op0, 2);
12250 op1 = GEN_INT (i);
12251 const_op = i;
12253 /* Result is nonzero iff shift count is equal to I. */
12254 code = reverse_condition (code);
12255 continue;
12258 /* fall through */
12260 case SIGN_EXTRACT:
12261 tem = expand_compound_operation (op0);
12262 if (tem != op0)
12264 op0 = tem;
12265 continue;
12267 break;
12269 case NOT:
12270 /* If testing for equality, we can take the NOT of the constant. */
12271 if (equality_comparison_p
12272 && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
12274 op0 = XEXP (op0, 0);
12275 op1 = tem;
12276 continue;
12279 /* If just looking at the sign bit, reverse the sense of the
12280 comparison. */
12281 if (sign_bit_comparison_p)
12283 op0 = XEXP (op0, 0);
12284 code = (code == GE ? LT : GE);
12285 continue;
12287 break;
12289 case NEG:
12290 /* If testing for equality, we can take the NEG of the constant. */
12291 if (equality_comparison_p
12292 && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
12294 op0 = XEXP (op0, 0);
12295 op1 = tem;
12296 continue;
12299 /* The remaining cases only apply to comparisons with zero. */
12300 if (const_op != 0)
12301 break;
12303 /* When X is ABS or is known positive,
12304 (neg X) is < 0 if and only if X != 0. */
12306 if (sign_bit_comparison_p
12307 && (GET_CODE (XEXP (op0, 0)) == ABS
12308 || (mode_width <= HOST_BITS_PER_WIDE_INT
12309 && (nonzero_bits (XEXP (op0, 0), mode)
12310 & (HOST_WIDE_INT_1U << (mode_width - 1)))
12311 == 0)))
12313 op0 = XEXP (op0, 0);
12314 code = (code == LT ? NE : EQ);
12315 continue;
12318 /* If we have NEG of something whose two high-order bits are the
12319 same, we know that "(-a) < 0" is equivalent to "a > 0". */
12320 if (num_sign_bit_copies (op0, mode) >= 2)
12322 op0 = XEXP (op0, 0);
12323 code = swap_condition (code);
12324 continue;
12326 break;
12328 case ROTATE:
12329 /* If we are testing equality and our count is a constant, we
12330 can perform the inverse operation on our RHS. */
12331 if (equality_comparison_p && CONST_INT_P (XEXP (op0, 1))
12332 && (tem = simplify_binary_operation (ROTATERT, mode,
12333 op1, XEXP (op0, 1))) != 0)
12335 op0 = XEXP (op0, 0);
12336 op1 = tem;
12337 continue;
12340 /* If we are doing a < 0 or >= 0 comparison, it means we are testing
12341 a particular bit. Convert it to an AND of a constant of that
12342 bit. This will be converted into a ZERO_EXTRACT. */
12343 if (const_op == 0 && sign_bit_comparison_p
12344 && CONST_INT_P (XEXP (op0, 1))
12345 && mode_width <= HOST_BITS_PER_WIDE_INT)
12347 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
12348 (HOST_WIDE_INT_1U
12349 << (mode_width - 1
12350 - INTVAL (XEXP (op0, 1)))));
12351 code = (code == LT ? NE : EQ);
12352 continue;
12355 /* Fall through. */
12357 case ABS:
12358 /* ABS is ignorable inside an equality comparison with zero. */
12359 if (const_op == 0 && equality_comparison_p)
12361 op0 = XEXP (op0, 0);
12362 continue;
12364 break;
12366 case SIGN_EXTEND:
12367 /* Can simplify (compare (zero/sign_extend FOO) CONST) to
12368 (compare FOO CONST) if CONST fits in FOO's mode and we
12369 are either testing inequality or have an unsigned
12370 comparison with ZERO_EXTEND or a signed comparison with
12371 SIGN_EXTEND. But don't do it if we don't have a compare
12372 insn of the given mode, since we'd have to revert it
12373 later on, and then we wouldn't know whether to sign- or
12374 zero-extend. */
12375 if (is_int_mode (GET_MODE (XEXP (op0, 0)), &mode)
12376 && ! unsigned_comparison_p
12377 && HWI_COMPUTABLE_MODE_P (mode)
12378 && trunc_int_for_mode (const_op, mode) == const_op
12379 && have_insn_for (COMPARE, mode))
12381 op0 = XEXP (op0, 0);
12382 continue;
12384 break;
12386 case SUBREG:
12387 /* Check for the case where we are comparing A - C1 with C2, that is
12389 (subreg:MODE (plus (A) (-C1))) op (C2)
12391 with C1 a constant, and try to lift the SUBREG, i.e. to do the
12392 comparison in the wider mode. One of the following two conditions
12393 must be true in order for this to be valid:
12395 1. The mode extension results in the same bit pattern being added
12396 on both sides and the comparison is equality or unsigned. As
12397 C2 has been truncated to fit in MODE, the pattern can only be
12398 all 0s or all 1s.
12400 2. The mode extension results in the sign bit being copied on
12401 each side.
12403 The difficulty here is that we have predicates for A but not for
12404 (A - C1) so we need to check that C1 is within proper bounds so
12405 as to perturbate A as little as possible. */
12407 if (mode_width <= HOST_BITS_PER_WIDE_INT
12408 && subreg_lowpart_p (op0)
12409 && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (op0)),
12410 &inner_mode)
12411 && GET_MODE_PRECISION (inner_mode) > mode_width
12412 && GET_CODE (SUBREG_REG (op0)) == PLUS
12413 && CONST_INT_P (XEXP (SUBREG_REG (op0), 1)))
12415 rtx a = XEXP (SUBREG_REG (op0), 0);
12416 HOST_WIDE_INT c1 = -INTVAL (XEXP (SUBREG_REG (op0), 1));
12418 if ((c1 > 0
12419 && (unsigned HOST_WIDE_INT) c1
12420 < HOST_WIDE_INT_1U << (mode_width - 1)
12421 && (equality_comparison_p || unsigned_comparison_p)
12422 /* (A - C1) zero-extends if it is positive and sign-extends
12423 if it is negative, C2 both zero- and sign-extends. */
12424 && (((nonzero_bits (a, inner_mode)
12425 & ~GET_MODE_MASK (mode)) == 0
12426 && const_op >= 0)
12427 /* (A - C1) sign-extends if it is positive and 1-extends
12428 if it is negative, C2 both sign- and 1-extends. */
12429 || (num_sign_bit_copies (a, inner_mode)
12430 > (unsigned int) (GET_MODE_PRECISION (inner_mode)
12431 - mode_width)
12432 && const_op < 0)))
12433 || ((unsigned HOST_WIDE_INT) c1
12434 < HOST_WIDE_INT_1U << (mode_width - 2)
12435 /* (A - C1) always sign-extends, like C2. */
12436 && num_sign_bit_copies (a, inner_mode)
12437 > (unsigned int) (GET_MODE_PRECISION (inner_mode)
12438 - (mode_width - 1))))
12440 op0 = SUBREG_REG (op0);
12441 continue;
12445 /* If the inner mode is narrower and we are extracting the low part,
12446 we can treat the SUBREG as if it were a ZERO_EXTEND. */
12447 if (paradoxical_subreg_p (op0))
12449 else if (subreg_lowpart_p (op0)
12450 && GET_MODE_CLASS (mode) == MODE_INT
12451 && is_int_mode (GET_MODE (SUBREG_REG (op0)), &inner_mode)
12452 && (code == NE || code == EQ)
12453 && GET_MODE_PRECISION (inner_mode) <= HOST_BITS_PER_WIDE_INT
12454 && !paradoxical_subreg_p (op0)
12455 && (nonzero_bits (SUBREG_REG (op0), inner_mode)
12456 & ~GET_MODE_MASK (mode)) == 0)
12458 /* Remove outer subregs that don't do anything. */
12459 tem = gen_lowpart (inner_mode, op1);
12461 if ((nonzero_bits (tem, inner_mode)
12462 & ~GET_MODE_MASK (mode)) == 0)
12464 op0 = SUBREG_REG (op0);
12465 op1 = tem;
12466 continue;
12468 break;
12470 else
12471 break;
12473 /* FALLTHROUGH */
12475 case ZERO_EXTEND:
12476 if (is_int_mode (GET_MODE (XEXP (op0, 0)), &mode)
12477 && (unsigned_comparison_p || equality_comparison_p)
12478 && HWI_COMPUTABLE_MODE_P (mode)
12479 && (unsigned HOST_WIDE_INT) const_op <= GET_MODE_MASK (mode)
12480 && const_op >= 0
12481 && have_insn_for (COMPARE, mode))
12483 op0 = XEXP (op0, 0);
12484 continue;
12486 break;
12488 case PLUS:
12489 /* (eq (plus X A) B) -> (eq X (minus B A)). We can only do
12490 this for equality comparisons due to pathological cases involving
12491 overflows. */
12492 if (equality_comparison_p
12493 && (tem = simplify_binary_operation (MINUS, mode,
12494 op1, XEXP (op0, 1))) != 0)
12496 op0 = XEXP (op0, 0);
12497 op1 = tem;
12498 continue;
12501 /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0. */
12502 if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
12503 && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
12505 op0 = XEXP (XEXP (op0, 0), 0);
12506 code = (code == LT ? EQ : NE);
12507 continue;
12509 break;
12511 case MINUS:
12512 /* We used to optimize signed comparisons against zero, but that
12513 was incorrect. Unsigned comparisons against zero (GTU, LEU)
12514 arrive here as equality comparisons, or (GEU, LTU) are
12515 optimized away. No need to special-case them. */
12517 /* (eq (minus A B) C) -> (eq A (plus B C)) or
12518 (eq B (minus A C)), whichever simplifies. We can only do
12519 this for equality comparisons due to pathological cases involving
12520 overflows. */
12521 if (equality_comparison_p
12522 && (tem = simplify_binary_operation (PLUS, mode,
12523 XEXP (op0, 1), op1)) != 0)
12525 op0 = XEXP (op0, 0);
12526 op1 = tem;
12527 continue;
12530 if (equality_comparison_p
12531 && (tem = simplify_binary_operation (MINUS, mode,
12532 XEXP (op0, 0), op1)) != 0)
12534 op0 = XEXP (op0, 1);
12535 op1 = tem;
12536 continue;
12539 /* The sign bit of (minus (ashiftrt X C) X), where C is the number
12540 of bits in X minus 1, is one iff X > 0. */
12541 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
12542 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
12543 && UINTVAL (XEXP (XEXP (op0, 0), 1)) == mode_width - 1
12544 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
12546 op0 = XEXP (op0, 1);
12547 code = (code == GE ? LE : GT);
12548 continue;
12550 break;
12552 case XOR:
12553 /* (eq (xor A B) C) -> (eq A (xor B C)). This is a simplification
12554 if C is zero or B is a constant. */
12555 if (equality_comparison_p
12556 && (tem = simplify_binary_operation (XOR, mode,
12557 XEXP (op0, 1), op1)) != 0)
12559 op0 = XEXP (op0, 0);
12560 op1 = tem;
12561 continue;
12563 break;
12566 case IOR:
12567 /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
12568 iff X <= 0. */
12569 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
12570 && XEXP (XEXP (op0, 0), 1) == constm1_rtx
12571 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
12573 op0 = XEXP (op0, 1);
12574 code = (code == GE ? GT : LE);
12575 continue;
12577 break;
12579 case AND:
12580 /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1). This
12581 will be converted to a ZERO_EXTRACT later. */
12582 if (const_op == 0 && equality_comparison_p
12583 && GET_CODE (XEXP (op0, 0)) == ASHIFT
12584 && XEXP (XEXP (op0, 0), 0) == const1_rtx)
12586 op0 = gen_rtx_LSHIFTRT (mode, XEXP (op0, 1),
12587 XEXP (XEXP (op0, 0), 1));
12588 op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
12589 continue;
12592 /* If we are comparing (and (lshiftrt X C1) C2) for equality with
12593 zero and X is a comparison and C1 and C2 describe only bits set
12594 in STORE_FLAG_VALUE, we can compare with X. */
12595 if (const_op == 0 && equality_comparison_p
12596 && mode_width <= HOST_BITS_PER_WIDE_INT
12597 && CONST_INT_P (XEXP (op0, 1))
12598 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
12599 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
12600 && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
12601 && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
12603 mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
12604 << INTVAL (XEXP (XEXP (op0, 0), 1)));
12605 if ((~STORE_FLAG_VALUE & mask) == 0
12606 && (COMPARISON_P (XEXP (XEXP (op0, 0), 0))
12607 || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
12608 && COMPARISON_P (tem))))
12610 op0 = XEXP (XEXP (op0, 0), 0);
12611 continue;
12615 /* If we are doing an equality comparison of an AND of a bit equal
12616 to the sign bit, replace this with a LT or GE comparison of
12617 the underlying value. */
12618 if (equality_comparison_p
12619 && const_op == 0
12620 && CONST_INT_P (XEXP (op0, 1))
12621 && mode_width <= HOST_BITS_PER_WIDE_INT
12622 && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
12623 == HOST_WIDE_INT_1U << (mode_width - 1)))
12625 op0 = XEXP (op0, 0);
12626 code = (code == EQ ? GE : LT);
12627 continue;
12630 /* If this AND operation is really a ZERO_EXTEND from a narrower
12631 mode, the constant fits within that mode, and this is either an
12632 equality or unsigned comparison, try to do this comparison in
12633 the narrower mode.
12635 Note that in:
12637 (ne:DI (and:DI (reg:DI 4) (const_int 0xffffffff)) (const_int 0))
12638 -> (ne:DI (reg:SI 4) (const_int 0))
12640 unless TARGET_TRULY_NOOP_TRUNCATION allows it or the register is
12641 known to hold a value of the required mode the
12642 transformation is invalid. */
12643 if ((equality_comparison_p || unsigned_comparison_p)
12644 && CONST_INT_P (XEXP (op0, 1))
12645 && (i = exact_log2 ((UINTVAL (XEXP (op0, 1))
12646 & GET_MODE_MASK (mode))
12647 + 1)) >= 0
12648 && const_op >> i == 0
12649 && int_mode_for_size (i, 1).exists (&tmode))
12651 op0 = gen_lowpart_or_truncate (tmode, XEXP (op0, 0));
12652 continue;
12655 /* If this is (and:M1 (subreg:M1 X:M2 0) (const_int C1)) where C1
12656 fits in both M1 and M2 and the SUBREG is either paradoxical
12657 or represents the low part, permute the SUBREG and the AND
12658 and try again. */
12659 if (GET_CODE (XEXP (op0, 0)) == SUBREG
12660 && CONST_INT_P (XEXP (op0, 1)))
12662 unsigned HOST_WIDE_INT c1 = INTVAL (XEXP (op0, 1));
12663 /* Require an integral mode, to avoid creating something like
12664 (AND:SF ...). */
12665 if ((is_a <scalar_int_mode>
12666 (GET_MODE (SUBREG_REG (XEXP (op0, 0))), &tmode))
12667 /* It is unsafe to commute the AND into the SUBREG if the
12668 SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
12669 not defined. As originally written the upper bits
12670 have a defined value due to the AND operation.
12671 However, if we commute the AND inside the SUBREG then
12672 they no longer have defined values and the meaning of
12673 the code has been changed.
12674 Also C1 should not change value in the smaller mode,
12675 see PR67028 (a positive C1 can become negative in the
12676 smaller mode, so that the AND does no longer mask the
12677 upper bits). */
12678 && ((WORD_REGISTER_OPERATIONS
12679 && mode_width > GET_MODE_PRECISION (tmode)
12680 && mode_width <= BITS_PER_WORD
12681 && trunc_int_for_mode (c1, tmode) == (HOST_WIDE_INT) c1)
12682 || (mode_width <= GET_MODE_PRECISION (tmode)
12683 && subreg_lowpart_p (XEXP (op0, 0))))
12684 && mode_width <= HOST_BITS_PER_WIDE_INT
12685 && HWI_COMPUTABLE_MODE_P (tmode)
12686 && (c1 & ~mask) == 0
12687 && (c1 & ~GET_MODE_MASK (tmode)) == 0
12688 && c1 != mask
12689 && c1 != GET_MODE_MASK (tmode))
12691 op0 = simplify_gen_binary (AND, tmode,
12692 SUBREG_REG (XEXP (op0, 0)),
12693 gen_int_mode (c1, tmode));
12694 op0 = gen_lowpart (mode, op0);
12695 continue;
12699 /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0). */
12700 if (const_op == 0 && equality_comparison_p
12701 && XEXP (op0, 1) == const1_rtx
12702 && GET_CODE (XEXP (op0, 0)) == NOT)
12704 op0 = simplify_and_const_int (NULL_RTX, mode,
12705 XEXP (XEXP (op0, 0), 0), 1);
12706 code = (code == NE ? EQ : NE);
12707 continue;
12710 /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
12711 (eq (and (lshiftrt X) 1) 0).
12712 Also handle the case where (not X) is expressed using xor. */
12713 if (const_op == 0 && equality_comparison_p
12714 && XEXP (op0, 1) == const1_rtx
12715 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT)
12717 rtx shift_op = XEXP (XEXP (op0, 0), 0);
12718 rtx shift_count = XEXP (XEXP (op0, 0), 1);
12720 if (GET_CODE (shift_op) == NOT
12721 || (GET_CODE (shift_op) == XOR
12722 && CONST_INT_P (XEXP (shift_op, 1))
12723 && CONST_INT_P (shift_count)
12724 && HWI_COMPUTABLE_MODE_P (mode)
12725 && (UINTVAL (XEXP (shift_op, 1))
12726 == HOST_WIDE_INT_1U
12727 << INTVAL (shift_count))))
12730 = gen_rtx_LSHIFTRT (mode, XEXP (shift_op, 0), shift_count);
12731 op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
12732 code = (code == NE ? EQ : NE);
12733 continue;
12736 break;
12738 case ASHIFT:
12739 /* If we have (compare (ashift FOO N) (const_int C)) and
12740 the high order N bits of FOO (N+1 if an inequality comparison)
12741 are known to be zero, we can do this by comparing FOO with C
12742 shifted right N bits so long as the low-order N bits of C are
12743 zero. */
12744 if (CONST_INT_P (XEXP (op0, 1))
12745 && INTVAL (XEXP (op0, 1)) >= 0
12746 && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
12747 < HOST_BITS_PER_WIDE_INT)
12748 && (((unsigned HOST_WIDE_INT) const_op
12749 & ((HOST_WIDE_INT_1U << INTVAL (XEXP (op0, 1)))
12750 - 1)) == 0)
12751 && mode_width <= HOST_BITS_PER_WIDE_INT
12752 && (nonzero_bits (XEXP (op0, 0), mode)
12753 & ~(mask >> (INTVAL (XEXP (op0, 1))
12754 + ! equality_comparison_p))) == 0)
12756 /* We must perform a logical shift, not an arithmetic one,
12757 as we want the top N bits of C to be zero. */
12758 unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
12760 temp >>= INTVAL (XEXP (op0, 1));
12761 op1 = gen_int_mode (temp, mode);
12762 op0 = XEXP (op0, 0);
12763 continue;
12766 /* If we are doing a sign bit comparison, it means we are testing
12767 a particular bit. Convert it to the appropriate AND. */
12768 if (sign_bit_comparison_p && CONST_INT_P (XEXP (op0, 1))
12769 && mode_width <= HOST_BITS_PER_WIDE_INT)
12771 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
12772 (HOST_WIDE_INT_1U
12773 << (mode_width - 1
12774 - INTVAL (XEXP (op0, 1)))));
12775 code = (code == LT ? NE : EQ);
12776 continue;
12779 /* If this an equality comparison with zero and we are shifting
12780 the low bit to the sign bit, we can convert this to an AND of the
12781 low-order bit. */
12782 if (const_op == 0 && equality_comparison_p
12783 && CONST_INT_P (XEXP (op0, 1))
12784 && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
12786 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0), 1);
12787 continue;
12789 break;
12791 case ASHIFTRT:
12792 /* If this is an equality comparison with zero, we can do this
12793 as a logical shift, which might be much simpler. */
12794 if (equality_comparison_p && const_op == 0
12795 && CONST_INT_P (XEXP (op0, 1)))
12797 op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
12798 XEXP (op0, 0),
12799 INTVAL (XEXP (op0, 1)));
12800 continue;
12803 /* If OP0 is a sign extension and CODE is not an unsigned comparison,
12804 do the comparison in a narrower mode. */
12805 if (! unsigned_comparison_p
12806 && CONST_INT_P (XEXP (op0, 1))
12807 && GET_CODE (XEXP (op0, 0)) == ASHIFT
12808 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
12809 && (int_mode_for_size (mode_width - INTVAL (XEXP (op0, 1)), 1)
12810 .exists (&tmode))
12811 && (((unsigned HOST_WIDE_INT) const_op
12812 + (GET_MODE_MASK (tmode) >> 1) + 1)
12813 <= GET_MODE_MASK (tmode)))
12815 op0 = gen_lowpart (tmode, XEXP (XEXP (op0, 0), 0));
12816 continue;
12819 /* Likewise if OP0 is a PLUS of a sign extension with a
12820 constant, which is usually represented with the PLUS
12821 between the shifts. */
12822 if (! unsigned_comparison_p
12823 && CONST_INT_P (XEXP (op0, 1))
12824 && GET_CODE (XEXP (op0, 0)) == PLUS
12825 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
12826 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
12827 && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
12828 && (int_mode_for_size (mode_width - INTVAL (XEXP (op0, 1)), 1)
12829 .exists (&tmode))
12830 && (((unsigned HOST_WIDE_INT) const_op
12831 + (GET_MODE_MASK (tmode) >> 1) + 1)
12832 <= GET_MODE_MASK (tmode)))
12834 rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
12835 rtx add_const = XEXP (XEXP (op0, 0), 1);
12836 rtx new_const = simplify_gen_binary (ASHIFTRT, mode,
12837 add_const, XEXP (op0, 1));
12839 op0 = simplify_gen_binary (PLUS, tmode,
12840 gen_lowpart (tmode, inner),
12841 new_const);
12842 continue;
12845 /* FALLTHROUGH */
12846 case LSHIFTRT:
12847 /* If we have (compare (xshiftrt FOO N) (const_int C)) and
12848 the low order N bits of FOO are known to be zero, we can do this
12849 by comparing FOO with C shifted left N bits so long as no
12850 overflow occurs. Even if the low order N bits of FOO aren't known
12851 to be zero, if the comparison is >= or < we can use the same
12852 optimization and for > or <= by setting all the low
12853 order N bits in the comparison constant. */
12854 if (CONST_INT_P (XEXP (op0, 1))
12855 && INTVAL (XEXP (op0, 1)) > 0
12856 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
12857 && mode_width <= HOST_BITS_PER_WIDE_INT
12858 && (((unsigned HOST_WIDE_INT) const_op
12859 + (GET_CODE (op0) != LSHIFTRT
12860 ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
12861 + 1)
12862 : 0))
12863 <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
12865 unsigned HOST_WIDE_INT low_bits
12866 = (nonzero_bits (XEXP (op0, 0), mode)
12867 & ((HOST_WIDE_INT_1U
12868 << INTVAL (XEXP (op0, 1))) - 1));
12869 if (low_bits == 0 || !equality_comparison_p)
12871 /* If the shift was logical, then we must make the condition
12872 unsigned. */
12873 if (GET_CODE (op0) == LSHIFTRT)
12874 code = unsigned_condition (code);
12876 const_op = (unsigned HOST_WIDE_INT) const_op
12877 << INTVAL (XEXP (op0, 1));
12878 if (low_bits != 0
12879 && (code == GT || code == GTU
12880 || code == LE || code == LEU))
12881 const_op
12882 |= ((HOST_WIDE_INT_1 << INTVAL (XEXP (op0, 1))) - 1);
12883 op1 = GEN_INT (const_op);
12884 op0 = XEXP (op0, 0);
12885 continue;
12889 /* If we are using this shift to extract just the sign bit, we
12890 can replace this with an LT or GE comparison. */
12891 if (const_op == 0
12892 && (equality_comparison_p || sign_bit_comparison_p)
12893 && CONST_INT_P (XEXP (op0, 1))
12894 && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
12896 op0 = XEXP (op0, 0);
12897 code = (code == NE || code == GT ? LT : GE);
12898 continue;
12900 break;
12902 default:
12903 break;
12906 break;
12909 /* Now make any compound operations involved in this comparison. Then,
12910 check for an outmost SUBREG on OP0 that is not doing anything or is
12911 paradoxical. The latter transformation must only be performed when
12912 it is known that the "extra" bits will be the same in op0 and op1 or
12913 that they don't matter. There are three cases to consider:
12915 1. SUBREG_REG (op0) is a register. In this case the bits are don't
12916 care bits and we can assume they have any convenient value. So
12917 making the transformation is safe.
12919 2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is UNKNOWN.
12920 In this case the upper bits of op0 are undefined. We should not make
12921 the simplification in that case as we do not know the contents of
12922 those bits.
12924 3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not UNKNOWN.
12925 In that case we know those bits are zeros or ones. We must also be
12926 sure that they are the same as the upper bits of op1.
12928 We can never remove a SUBREG for a non-equality comparison because
12929 the sign bit is in a different place in the underlying object. */
12931 rtx_code op0_mco_code = SET;
12932 if (op1 == const0_rtx)
12933 op0_mco_code = code == NE || code == EQ ? EQ : COMPARE;
12935 op0 = make_compound_operation (op0, op0_mco_code);
12936 op1 = make_compound_operation (op1, SET);
12938 if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
12939 && is_int_mode (GET_MODE (op0), &mode)
12940 && is_int_mode (GET_MODE (SUBREG_REG (op0)), &inner_mode)
12941 && (code == NE || code == EQ))
12943 if (paradoxical_subreg_p (op0))
12945 /* For paradoxical subregs, allow case 1 as above. Case 3 isn't
12946 implemented. */
12947 if (REG_P (SUBREG_REG (op0)))
12949 op0 = SUBREG_REG (op0);
12950 op1 = gen_lowpart (inner_mode, op1);
12953 else if (GET_MODE_PRECISION (inner_mode) <= HOST_BITS_PER_WIDE_INT
12954 && (nonzero_bits (SUBREG_REG (op0), inner_mode)
12955 & ~GET_MODE_MASK (mode)) == 0)
12957 tem = gen_lowpart (inner_mode, op1);
12959 if ((nonzero_bits (tem, inner_mode) & ~GET_MODE_MASK (mode)) == 0)
12960 op0 = SUBREG_REG (op0), op1 = tem;
12964 /* We now do the opposite procedure: Some machines don't have compare
12965 insns in all modes. If OP0's mode is an integer mode smaller than a
12966 word and we can't do a compare in that mode, see if there is a larger
12967 mode for which we can do the compare. There are a number of cases in
12968 which we can use the wider mode. */
12970 if (is_int_mode (GET_MODE (op0), &mode)
12971 && GET_MODE_SIZE (mode) < UNITS_PER_WORD
12972 && ! have_insn_for (COMPARE, mode))
12973 FOR_EACH_WIDER_MODE (tmode_iter, mode)
12975 tmode = tmode_iter.require ();
12976 if (!HWI_COMPUTABLE_MODE_P (tmode))
12977 break;
12978 if (have_insn_for (COMPARE, tmode))
12980 int zero_extended;
12982 /* If this is a test for negative, we can make an explicit
12983 test of the sign bit. Test this first so we can use
12984 a paradoxical subreg to extend OP0. */
12986 if (op1 == const0_rtx && (code == LT || code == GE)
12987 && HWI_COMPUTABLE_MODE_P (mode))
12989 unsigned HOST_WIDE_INT sign
12990 = HOST_WIDE_INT_1U << (GET_MODE_BITSIZE (mode) - 1);
12991 op0 = simplify_gen_binary (AND, tmode,
12992 gen_lowpart (tmode, op0),
12993 gen_int_mode (sign, tmode));
12994 code = (code == LT) ? NE : EQ;
12995 break;
12998 /* If the only nonzero bits in OP0 and OP1 are those in the
12999 narrower mode and this is an equality or unsigned comparison,
13000 we can use the wider mode. Similarly for sign-extended
13001 values, in which case it is true for all comparisons. */
13002 zero_extended = ((code == EQ || code == NE
13003 || code == GEU || code == GTU
13004 || code == LEU || code == LTU)
13005 && (nonzero_bits (op0, tmode)
13006 & ~GET_MODE_MASK (mode)) == 0
13007 && ((CONST_INT_P (op1)
13008 || (nonzero_bits (op1, tmode)
13009 & ~GET_MODE_MASK (mode)) == 0)));
13011 if (zero_extended
13012 || ((num_sign_bit_copies (op0, tmode)
13013 > (unsigned int) (GET_MODE_PRECISION (tmode)
13014 - GET_MODE_PRECISION (mode)))
13015 && (num_sign_bit_copies (op1, tmode)
13016 > (unsigned int) (GET_MODE_PRECISION (tmode)
13017 - GET_MODE_PRECISION (mode)))))
13019 /* If OP0 is an AND and we don't have an AND in MODE either,
13020 make a new AND in the proper mode. */
13021 if (GET_CODE (op0) == AND
13022 && !have_insn_for (AND, mode))
13023 op0 = simplify_gen_binary (AND, tmode,
13024 gen_lowpart (tmode,
13025 XEXP (op0, 0)),
13026 gen_lowpart (tmode,
13027 XEXP (op0, 1)));
13028 else
13030 if (zero_extended)
13032 op0 = simplify_gen_unary (ZERO_EXTEND, tmode,
13033 op0, mode);
13034 op1 = simplify_gen_unary (ZERO_EXTEND, tmode,
13035 op1, mode);
13037 else
13039 op0 = simplify_gen_unary (SIGN_EXTEND, tmode,
13040 op0, mode);
13041 op1 = simplify_gen_unary (SIGN_EXTEND, tmode,
13042 op1, mode);
13044 break;
13050 /* We may have changed the comparison operands. Re-canonicalize. */
13051 if (swap_commutative_operands_p (op0, op1))
13053 std::swap (op0, op1);
13054 code = swap_condition (code);
13057 /* If this machine only supports a subset of valid comparisons, see if we
13058 can convert an unsupported one into a supported one. */
13059 target_canonicalize_comparison (&code, &op0, &op1, 0);
13061 *pop0 = op0;
13062 *pop1 = op1;
13064 return code;
13067 /* Utility function for record_value_for_reg. Count number of
13068 rtxs in X. */
13069 static int
13070 count_rtxs (rtx x)
13072 enum rtx_code code = GET_CODE (x);
13073 const char *fmt;
13074 int i, j, ret = 1;
13076 if (GET_RTX_CLASS (code) == RTX_BIN_ARITH
13077 || GET_RTX_CLASS (code) == RTX_COMM_ARITH)
13079 rtx x0 = XEXP (x, 0);
13080 rtx x1 = XEXP (x, 1);
13082 if (x0 == x1)
13083 return 1 + 2 * count_rtxs (x0);
13085 if ((GET_RTX_CLASS (GET_CODE (x1)) == RTX_BIN_ARITH
13086 || GET_RTX_CLASS (GET_CODE (x1)) == RTX_COMM_ARITH)
13087 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
13088 return 2 + 2 * count_rtxs (x0)
13089 + count_rtxs (x == XEXP (x1, 0)
13090 ? XEXP (x1, 1) : XEXP (x1, 0));
13092 if ((GET_RTX_CLASS (GET_CODE (x0)) == RTX_BIN_ARITH
13093 || GET_RTX_CLASS (GET_CODE (x0)) == RTX_COMM_ARITH)
13094 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
13095 return 2 + 2 * count_rtxs (x1)
13096 + count_rtxs (x == XEXP (x0, 0)
13097 ? XEXP (x0, 1) : XEXP (x0, 0));
13100 fmt = GET_RTX_FORMAT (code);
13101 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
13102 if (fmt[i] == 'e')
13103 ret += count_rtxs (XEXP (x, i));
13104 else if (fmt[i] == 'E')
13105 for (j = 0; j < XVECLEN (x, i); j++)
13106 ret += count_rtxs (XVECEXP (x, i, j));
13108 return ret;
13111 /* Utility function for following routine. Called when X is part of a value
13112 being stored into last_set_value. Sets last_set_table_tick
13113 for each register mentioned. Similar to mention_regs in cse.c */
13115 static void
13116 update_table_tick (rtx x)
13118 enum rtx_code code = GET_CODE (x);
13119 const char *fmt = GET_RTX_FORMAT (code);
13120 int i, j;
13122 if (code == REG)
13124 unsigned int regno = REGNO (x);
13125 unsigned int endregno = END_REGNO (x);
13126 unsigned int r;
13128 for (r = regno; r < endregno; r++)
13130 reg_stat_type *rsp = &reg_stat[r];
13131 rsp->last_set_table_tick = label_tick;
13134 return;
13137 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
13138 if (fmt[i] == 'e')
13140 /* Check for identical subexpressions. If x contains
13141 identical subexpression we only have to traverse one of
13142 them. */
13143 if (i == 0 && ARITHMETIC_P (x))
13145 /* Note that at this point x1 has already been
13146 processed. */
13147 rtx x0 = XEXP (x, 0);
13148 rtx x1 = XEXP (x, 1);
13150 /* If x0 and x1 are identical then there is no need to
13151 process x0. */
13152 if (x0 == x1)
13153 break;
13155 /* If x0 is identical to a subexpression of x1 then while
13156 processing x1, x0 has already been processed. Thus we
13157 are done with x. */
13158 if (ARITHMETIC_P (x1)
13159 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
13160 break;
13162 /* If x1 is identical to a subexpression of x0 then we
13163 still have to process the rest of x0. */
13164 if (ARITHMETIC_P (x0)
13165 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
13167 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
13168 break;
13172 update_table_tick (XEXP (x, i));
13174 else if (fmt[i] == 'E')
13175 for (j = 0; j < XVECLEN (x, i); j++)
13176 update_table_tick (XVECEXP (x, i, j));
13179 /* Record that REG is set to VALUE in insn INSN. If VALUE is zero, we
13180 are saying that the register is clobbered and we no longer know its
13181 value. If INSN is zero, don't update reg_stat[].last_set; this is
13182 only permitted with VALUE also zero and is used to invalidate the
13183 register. */
13185 static void
13186 record_value_for_reg (rtx reg, rtx_insn *insn, rtx value)
13188 unsigned int regno = REGNO (reg);
13189 unsigned int endregno = END_REGNO (reg);
13190 unsigned int i;
13191 reg_stat_type *rsp;
13193 /* If VALUE contains REG and we have a previous value for REG, substitute
13194 the previous value. */
13195 if (value && insn && reg_overlap_mentioned_p (reg, value))
13197 rtx tem;
13199 /* Set things up so get_last_value is allowed to see anything set up to
13200 our insn. */
13201 subst_low_luid = DF_INSN_LUID (insn);
13202 tem = get_last_value (reg);
13204 /* If TEM is simply a binary operation with two CLOBBERs as operands,
13205 it isn't going to be useful and will take a lot of time to process,
13206 so just use the CLOBBER. */
13208 if (tem)
13210 if (ARITHMETIC_P (tem)
13211 && GET_CODE (XEXP (tem, 0)) == CLOBBER
13212 && GET_CODE (XEXP (tem, 1)) == CLOBBER)
13213 tem = XEXP (tem, 0);
13214 else if (count_occurrences (value, reg, 1) >= 2)
13216 /* If there are two or more occurrences of REG in VALUE,
13217 prevent the value from growing too much. */
13218 if (count_rtxs (tem) > MAX_LAST_VALUE_RTL)
13219 tem = gen_rtx_CLOBBER (GET_MODE (tem), const0_rtx);
13222 value = replace_rtx (copy_rtx (value), reg, tem);
13226 /* For each register modified, show we don't know its value, that
13227 we don't know about its bitwise content, that its value has been
13228 updated, and that we don't know the location of the death of the
13229 register. */
13230 for (i = regno; i < endregno; i++)
13232 rsp = &reg_stat[i];
13234 if (insn)
13235 rsp->last_set = insn;
13237 rsp->last_set_value = 0;
13238 rsp->last_set_mode = VOIDmode;
13239 rsp->last_set_nonzero_bits = 0;
13240 rsp->last_set_sign_bit_copies = 0;
13241 rsp->last_death = 0;
13242 rsp->truncated_to_mode = VOIDmode;
13245 /* Mark registers that are being referenced in this value. */
13246 if (value)
13247 update_table_tick (value);
13249 /* Now update the status of each register being set.
13250 If someone is using this register in this block, set this register
13251 to invalid since we will get confused between the two lives in this
13252 basic block. This makes using this register always invalid. In cse, we
13253 scan the table to invalidate all entries using this register, but this
13254 is too much work for us. */
13256 for (i = regno; i < endregno; i++)
13258 rsp = &reg_stat[i];
13259 rsp->last_set_label = label_tick;
13260 if (!insn
13261 || (value && rsp->last_set_table_tick >= label_tick_ebb_start))
13262 rsp->last_set_invalid = 1;
13263 else
13264 rsp->last_set_invalid = 0;
13267 /* The value being assigned might refer to X (like in "x++;"). In that
13268 case, we must replace it with (clobber (const_int 0)) to prevent
13269 infinite loops. */
13270 rsp = &reg_stat[regno];
13271 if (value && !get_last_value_validate (&value, insn, label_tick, 0))
13273 value = copy_rtx (value);
13274 if (!get_last_value_validate (&value, insn, label_tick, 1))
13275 value = 0;
13278 /* For the main register being modified, update the value, the mode, the
13279 nonzero bits, and the number of sign bit copies. */
13281 rsp->last_set_value = value;
13283 if (value)
13285 machine_mode mode = GET_MODE (reg);
13286 subst_low_luid = DF_INSN_LUID (insn);
13287 rsp->last_set_mode = mode;
13288 if (GET_MODE_CLASS (mode) == MODE_INT
13289 && HWI_COMPUTABLE_MODE_P (mode))
13290 mode = nonzero_bits_mode;
13291 rsp->last_set_nonzero_bits = nonzero_bits (value, mode);
13292 rsp->last_set_sign_bit_copies
13293 = num_sign_bit_copies (value, GET_MODE (reg));
13297 /* Called via note_stores from record_dead_and_set_regs to handle one
13298 SET or CLOBBER in an insn. DATA is the instruction in which the
13299 set is occurring. */
13301 static void
13302 record_dead_and_set_regs_1 (rtx dest, const_rtx setter, void *data)
13304 rtx_insn *record_dead_insn = (rtx_insn *) data;
13306 if (GET_CODE (dest) == SUBREG)
13307 dest = SUBREG_REG (dest);
13309 if (!record_dead_insn)
13311 if (REG_P (dest))
13312 record_value_for_reg (dest, NULL, NULL_RTX);
13313 return;
13316 if (REG_P (dest))
13318 /* If we are setting the whole register, we know its value. Otherwise
13319 show that we don't know the value. We can handle a SUBREG if it's
13320 the low part, but we must be careful with paradoxical SUBREGs on
13321 RISC architectures because we cannot strip e.g. an extension around
13322 a load and record the naked load since the RTL middle-end considers
13323 that the upper bits are defined according to LOAD_EXTEND_OP. */
13324 if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
13325 record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
13326 else if (GET_CODE (setter) == SET
13327 && GET_CODE (SET_DEST (setter)) == SUBREG
13328 && SUBREG_REG (SET_DEST (setter)) == dest
13329 && known_le (GET_MODE_PRECISION (GET_MODE (dest)),
13330 BITS_PER_WORD)
13331 && subreg_lowpart_p (SET_DEST (setter)))
13332 record_value_for_reg (dest, record_dead_insn,
13333 WORD_REGISTER_OPERATIONS
13334 && paradoxical_subreg_p (SET_DEST (setter))
13335 ? SET_SRC (setter)
13336 : gen_lowpart (GET_MODE (dest),
13337 SET_SRC (setter)));
13338 else if (GET_CODE (setter) == CLOBBER_HIGH)
13340 reg_stat_type *rsp = &reg_stat[REGNO (dest)];
13341 if (rsp->last_set_value
13342 && reg_is_clobbered_by_clobber_high
13343 (REGNO (dest), GET_MODE (rsp->last_set_value),
13344 XEXP (setter, 0)))
13345 record_value_for_reg (dest, NULL, NULL_RTX);
13347 else
13348 record_value_for_reg (dest, record_dead_insn, NULL_RTX);
13350 else if (MEM_P (dest)
13351 /* Ignore pushes, they clobber nothing. */
13352 && ! push_operand (dest, GET_MODE (dest)))
13353 mem_last_set = DF_INSN_LUID (record_dead_insn);
13356 /* Update the records of when each REG was most recently set or killed
13357 for the things done by INSN. This is the last thing done in processing
13358 INSN in the combiner loop.
13360 We update reg_stat[], in particular fields last_set, last_set_value,
13361 last_set_mode, last_set_nonzero_bits, last_set_sign_bit_copies,
13362 last_death, and also the similar information mem_last_set (which insn
13363 most recently modified memory) and last_call_luid (which insn was the
13364 most recent subroutine call). */
13366 static void
13367 record_dead_and_set_regs (rtx_insn *insn)
13369 rtx link;
13370 unsigned int i;
13372 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
13374 if (REG_NOTE_KIND (link) == REG_DEAD
13375 && REG_P (XEXP (link, 0)))
13377 unsigned int regno = REGNO (XEXP (link, 0));
13378 unsigned int endregno = END_REGNO (XEXP (link, 0));
13380 for (i = regno; i < endregno; i++)
13382 reg_stat_type *rsp;
13384 rsp = &reg_stat[i];
13385 rsp->last_death = insn;
13388 else if (REG_NOTE_KIND (link) == REG_INC)
13389 record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
13392 if (CALL_P (insn))
13394 hard_reg_set_iterator hrsi;
13395 EXECUTE_IF_SET_IN_HARD_REG_SET (regs_invalidated_by_call, 0, i, hrsi)
13397 reg_stat_type *rsp;
13399 rsp = &reg_stat[i];
13400 rsp->last_set_invalid = 1;
13401 rsp->last_set = insn;
13402 rsp->last_set_value = 0;
13403 rsp->last_set_mode = VOIDmode;
13404 rsp->last_set_nonzero_bits = 0;
13405 rsp->last_set_sign_bit_copies = 0;
13406 rsp->last_death = 0;
13407 rsp->truncated_to_mode = VOIDmode;
13410 last_call_luid = mem_last_set = DF_INSN_LUID (insn);
13412 /* We can't combine into a call pattern. Remember, though, that
13413 the return value register is set at this LUID. We could
13414 still replace a register with the return value from the
13415 wrong subroutine call! */
13416 note_stores (PATTERN (insn), record_dead_and_set_regs_1, NULL_RTX);
13418 else
13419 note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
13422 /* If a SUBREG has the promoted bit set, it is in fact a property of the
13423 register present in the SUBREG, so for each such SUBREG go back and
13424 adjust nonzero and sign bit information of the registers that are
13425 known to have some zero/sign bits set.
13427 This is needed because when combine blows the SUBREGs away, the
13428 information on zero/sign bits is lost and further combines can be
13429 missed because of that. */
13431 static void
13432 record_promoted_value (rtx_insn *insn, rtx subreg)
13434 struct insn_link *links;
13435 rtx set;
13436 unsigned int regno = REGNO (SUBREG_REG (subreg));
13437 machine_mode mode = GET_MODE (subreg);
13439 if (!HWI_COMPUTABLE_MODE_P (mode))
13440 return;
13442 for (links = LOG_LINKS (insn); links;)
13444 reg_stat_type *rsp;
13446 insn = links->insn;
13447 set = single_set (insn);
13449 if (! set || !REG_P (SET_DEST (set))
13450 || REGNO (SET_DEST (set)) != regno
13451 || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
13453 links = links->next;
13454 continue;
13457 rsp = &reg_stat[regno];
13458 if (rsp->last_set == insn)
13460 if (SUBREG_PROMOTED_UNSIGNED_P (subreg))
13461 rsp->last_set_nonzero_bits &= GET_MODE_MASK (mode);
13464 if (REG_P (SET_SRC (set)))
13466 regno = REGNO (SET_SRC (set));
13467 links = LOG_LINKS (insn);
13469 else
13470 break;
13474 /* Check if X, a register, is known to contain a value already
13475 truncated to MODE. In this case we can use a subreg to refer to
13476 the truncated value even though in the generic case we would need
13477 an explicit truncation. */
13479 static bool
13480 reg_truncated_to_mode (machine_mode mode, const_rtx x)
13482 reg_stat_type *rsp = &reg_stat[REGNO (x)];
13483 machine_mode truncated = rsp->truncated_to_mode;
13485 if (truncated == 0
13486 || rsp->truncation_label < label_tick_ebb_start)
13487 return false;
13488 if (!partial_subreg_p (mode, truncated))
13489 return true;
13490 if (TRULY_NOOP_TRUNCATION_MODES_P (mode, truncated))
13491 return true;
13492 return false;
13495 /* If X is a hard reg or a subreg record the mode that the register is
13496 accessed in. For non-TARGET_TRULY_NOOP_TRUNCATION targets we might be
13497 able to turn a truncate into a subreg using this information. Return true
13498 if traversing X is complete. */
13500 static bool
13501 record_truncated_value (rtx x)
13503 machine_mode truncated_mode;
13504 reg_stat_type *rsp;
13506 if (GET_CODE (x) == SUBREG && REG_P (SUBREG_REG (x)))
13508 machine_mode original_mode = GET_MODE (SUBREG_REG (x));
13509 truncated_mode = GET_MODE (x);
13511 if (!partial_subreg_p (truncated_mode, original_mode))
13512 return true;
13514 truncated_mode = GET_MODE (x);
13515 if (TRULY_NOOP_TRUNCATION_MODES_P (truncated_mode, original_mode))
13516 return true;
13518 x = SUBREG_REG (x);
13520 /* ??? For hard-regs we now record everything. We might be able to
13521 optimize this using last_set_mode. */
13522 else if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
13523 truncated_mode = GET_MODE (x);
13524 else
13525 return false;
13527 rsp = &reg_stat[REGNO (x)];
13528 if (rsp->truncated_to_mode == 0
13529 || rsp->truncation_label < label_tick_ebb_start
13530 || partial_subreg_p (truncated_mode, rsp->truncated_to_mode))
13532 rsp->truncated_to_mode = truncated_mode;
13533 rsp->truncation_label = label_tick;
13536 return true;
13539 /* Callback for note_uses. Find hardregs and subregs of pseudos and
13540 the modes they are used in. This can help truning TRUNCATEs into
13541 SUBREGs. */
13543 static void
13544 record_truncated_values (rtx *loc, void *data ATTRIBUTE_UNUSED)
13546 subrtx_var_iterator::array_type array;
13547 FOR_EACH_SUBRTX_VAR (iter, array, *loc, NONCONST)
13548 if (record_truncated_value (*iter))
13549 iter.skip_subrtxes ();
13552 /* Scan X for promoted SUBREGs. For each one found,
13553 note what it implies to the registers used in it. */
13555 static void
13556 check_promoted_subreg (rtx_insn *insn, rtx x)
13558 if (GET_CODE (x) == SUBREG
13559 && SUBREG_PROMOTED_VAR_P (x)
13560 && REG_P (SUBREG_REG (x)))
13561 record_promoted_value (insn, x);
13562 else
13564 const char *format = GET_RTX_FORMAT (GET_CODE (x));
13565 int i, j;
13567 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
13568 switch (format[i])
13570 case 'e':
13571 check_promoted_subreg (insn, XEXP (x, i));
13572 break;
13573 case 'V':
13574 case 'E':
13575 if (XVEC (x, i) != 0)
13576 for (j = 0; j < XVECLEN (x, i); j++)
13577 check_promoted_subreg (insn, XVECEXP (x, i, j));
13578 break;
13583 /* Verify that all the registers and memory references mentioned in *LOC are
13584 still valid. *LOC was part of a value set in INSN when label_tick was
13585 equal to TICK. Return 0 if some are not. If REPLACE is nonzero, replace
13586 the invalid references with (clobber (const_int 0)) and return 1. This
13587 replacement is useful because we often can get useful information about
13588 the form of a value (e.g., if it was produced by a shift that always
13589 produces -1 or 0) even though we don't know exactly what registers it
13590 was produced from. */
13592 static int
13593 get_last_value_validate (rtx *loc, rtx_insn *insn, int tick, int replace)
13595 rtx x = *loc;
13596 const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
13597 int len = GET_RTX_LENGTH (GET_CODE (x));
13598 int i, j;
13600 if (REG_P (x))
13602 unsigned int regno = REGNO (x);
13603 unsigned int endregno = END_REGNO (x);
13604 unsigned int j;
13606 for (j = regno; j < endregno; j++)
13608 reg_stat_type *rsp = &reg_stat[j];
13609 if (rsp->last_set_invalid
13610 /* If this is a pseudo-register that was only set once and not
13611 live at the beginning of the function, it is always valid. */
13612 || (! (regno >= FIRST_PSEUDO_REGISTER
13613 && regno < reg_n_sets_max
13614 && REG_N_SETS (regno) == 1
13615 && (!REGNO_REG_SET_P
13616 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
13617 regno)))
13618 && rsp->last_set_label > tick))
13620 if (replace)
13621 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
13622 return replace;
13626 return 1;
13628 /* If this is a memory reference, make sure that there were no stores after
13629 it that might have clobbered the value. We don't have alias info, so we
13630 assume any store invalidates it. Moreover, we only have local UIDs, so
13631 we also assume that there were stores in the intervening basic blocks. */
13632 else if (MEM_P (x) && !MEM_READONLY_P (x)
13633 && (tick != label_tick || DF_INSN_LUID (insn) <= mem_last_set))
13635 if (replace)
13636 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
13637 return replace;
13640 for (i = 0; i < len; i++)
13642 if (fmt[i] == 'e')
13644 /* Check for identical subexpressions. If x contains
13645 identical subexpression we only have to traverse one of
13646 them. */
13647 if (i == 1 && ARITHMETIC_P (x))
13649 /* Note that at this point x0 has already been checked
13650 and found valid. */
13651 rtx x0 = XEXP (x, 0);
13652 rtx x1 = XEXP (x, 1);
13654 /* If x0 and x1 are identical then x is also valid. */
13655 if (x0 == x1)
13656 return 1;
13658 /* If x1 is identical to a subexpression of x0 then
13659 while checking x0, x1 has already been checked. Thus
13660 it is valid and so as x. */
13661 if (ARITHMETIC_P (x0)
13662 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
13663 return 1;
13665 /* If x0 is identical to a subexpression of x1 then x is
13666 valid iff the rest of x1 is valid. */
13667 if (ARITHMETIC_P (x1)
13668 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
13669 return
13670 get_last_value_validate (&XEXP (x1,
13671 x0 == XEXP (x1, 0) ? 1 : 0),
13672 insn, tick, replace);
13675 if (get_last_value_validate (&XEXP (x, i), insn, tick,
13676 replace) == 0)
13677 return 0;
13679 else if (fmt[i] == 'E')
13680 for (j = 0; j < XVECLEN (x, i); j++)
13681 if (get_last_value_validate (&XVECEXP (x, i, j),
13682 insn, tick, replace) == 0)
13683 return 0;
13686 /* If we haven't found a reason for it to be invalid, it is valid. */
13687 return 1;
13690 /* Get the last value assigned to X, if known. Some registers
13691 in the value may be replaced with (clobber (const_int 0)) if their value
13692 is known longer known reliably. */
13694 static rtx
13695 get_last_value (const_rtx x)
13697 unsigned int regno;
13698 rtx value;
13699 reg_stat_type *rsp;
13701 /* If this is a non-paradoxical SUBREG, get the value of its operand and
13702 then convert it to the desired mode. If this is a paradoxical SUBREG,
13703 we cannot predict what values the "extra" bits might have. */
13704 if (GET_CODE (x) == SUBREG
13705 && subreg_lowpart_p (x)
13706 && !paradoxical_subreg_p (x)
13707 && (value = get_last_value (SUBREG_REG (x))) != 0)
13708 return gen_lowpart (GET_MODE (x), value);
13710 if (!REG_P (x))
13711 return 0;
13713 regno = REGNO (x);
13714 rsp = &reg_stat[regno];
13715 value = rsp->last_set_value;
13717 /* If we don't have a value, or if it isn't for this basic block and
13718 it's either a hard register, set more than once, or it's a live
13719 at the beginning of the function, return 0.
13721 Because if it's not live at the beginning of the function then the reg
13722 is always set before being used (is never used without being set).
13723 And, if it's set only once, and it's always set before use, then all
13724 uses must have the same last value, even if it's not from this basic
13725 block. */
13727 if (value == 0
13728 || (rsp->last_set_label < label_tick_ebb_start
13729 && (regno < FIRST_PSEUDO_REGISTER
13730 || regno >= reg_n_sets_max
13731 || REG_N_SETS (regno) != 1
13732 || REGNO_REG_SET_P
13733 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb), regno))))
13734 return 0;
13736 /* If the value was set in a later insn than the ones we are processing,
13737 we can't use it even if the register was only set once. */
13738 if (rsp->last_set_label == label_tick
13739 && DF_INSN_LUID (rsp->last_set) >= subst_low_luid)
13740 return 0;
13742 /* If fewer bits were set than what we are asked for now, we cannot use
13743 the value. */
13744 if (maybe_lt (GET_MODE_PRECISION (rsp->last_set_mode),
13745 GET_MODE_PRECISION (GET_MODE (x))))
13746 return 0;
13748 /* If the value has all its registers valid, return it. */
13749 if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 0))
13750 return value;
13752 /* Otherwise, make a copy and replace any invalid register with
13753 (clobber (const_int 0)). If that fails for some reason, return 0. */
13755 value = copy_rtx (value);
13756 if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 1))
13757 return value;
13759 return 0;
13762 /* Define three variables used for communication between the following
13763 routines. */
13765 static unsigned int reg_dead_regno, reg_dead_endregno;
13766 static int reg_dead_flag;
13767 rtx reg_dead_reg;
13769 /* Function called via note_stores from reg_dead_at_p.
13771 If DEST is within [reg_dead_regno, reg_dead_endregno), set
13772 reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET. */
13774 static void
13775 reg_dead_at_p_1 (rtx dest, const_rtx x, void *data ATTRIBUTE_UNUSED)
13777 unsigned int regno, endregno;
13779 if (!REG_P (dest))
13780 return;
13782 if (GET_CODE (x) == CLOBBER_HIGH
13783 && !reg_is_clobbered_by_clobber_high (reg_dead_reg, XEXP (x, 0)))
13784 return;
13786 regno = REGNO (dest);
13787 endregno = END_REGNO (dest);
13788 if (reg_dead_endregno > regno && reg_dead_regno < endregno)
13789 reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
13792 /* Return nonzero if REG is known to be dead at INSN.
13794 We scan backwards from INSN. If we hit a REG_DEAD note or a CLOBBER
13795 referencing REG, it is dead. If we hit a SET referencing REG, it is
13796 live. Otherwise, see if it is live or dead at the start of the basic
13797 block we are in. Hard regs marked as being live in NEWPAT_USED_REGS
13798 must be assumed to be always live. */
13800 static int
13801 reg_dead_at_p (rtx reg, rtx_insn *insn)
13803 basic_block block;
13804 unsigned int i;
13806 /* Set variables for reg_dead_at_p_1. */
13807 reg_dead_regno = REGNO (reg);
13808 reg_dead_endregno = END_REGNO (reg);
13809 reg_dead_reg = reg;
13811 reg_dead_flag = 0;
13813 /* Check that reg isn't mentioned in NEWPAT_USED_REGS. For fixed registers
13814 we allow the machine description to decide whether use-and-clobber
13815 patterns are OK. */
13816 if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
13818 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
13819 if (!fixed_regs[i] && TEST_HARD_REG_BIT (newpat_used_regs, i))
13820 return 0;
13823 /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, or
13824 beginning of basic block. */
13825 block = BLOCK_FOR_INSN (insn);
13826 for (;;)
13828 if (INSN_P (insn))
13830 if (find_regno_note (insn, REG_UNUSED, reg_dead_regno))
13831 return 1;
13833 note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
13834 if (reg_dead_flag)
13835 return reg_dead_flag == 1 ? 1 : 0;
13837 if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
13838 return 1;
13841 if (insn == BB_HEAD (block))
13842 break;
13844 insn = PREV_INSN (insn);
13847 /* Look at live-in sets for the basic block that we were in. */
13848 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
13849 if (REGNO_REG_SET_P (df_get_live_in (block), i))
13850 return 0;
13852 return 1;
13855 /* Note hard registers in X that are used. */
13857 static void
13858 mark_used_regs_combine (rtx x)
13860 RTX_CODE code = GET_CODE (x);
13861 unsigned int regno;
13862 int i;
13864 switch (code)
13866 case LABEL_REF:
13867 case SYMBOL_REF:
13868 case CONST:
13869 CASE_CONST_ANY:
13870 case PC:
13871 case ADDR_VEC:
13872 case ADDR_DIFF_VEC:
13873 case ASM_INPUT:
13874 /* CC0 must die in the insn after it is set, so we don't need to take
13875 special note of it here. */
13876 case CC0:
13877 return;
13879 case CLOBBER:
13880 /* If we are clobbering a MEM, mark any hard registers inside the
13881 address as used. */
13882 if (MEM_P (XEXP (x, 0)))
13883 mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
13884 return;
13886 case REG:
13887 regno = REGNO (x);
13888 /* A hard reg in a wide mode may really be multiple registers.
13889 If so, mark all of them just like the first. */
13890 if (regno < FIRST_PSEUDO_REGISTER)
13892 /* None of this applies to the stack, frame or arg pointers. */
13893 if (regno == STACK_POINTER_REGNUM
13894 || (!HARD_FRAME_POINTER_IS_FRAME_POINTER
13895 && regno == HARD_FRAME_POINTER_REGNUM)
13896 || (FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
13897 && regno == ARG_POINTER_REGNUM && fixed_regs[regno])
13898 || regno == FRAME_POINTER_REGNUM)
13899 return;
13901 add_to_hard_reg_set (&newpat_used_regs, GET_MODE (x), regno);
13903 return;
13905 case SET:
13907 /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
13908 the address. */
13909 rtx testreg = SET_DEST (x);
13911 while (GET_CODE (testreg) == SUBREG
13912 || GET_CODE (testreg) == ZERO_EXTRACT
13913 || GET_CODE (testreg) == STRICT_LOW_PART)
13914 testreg = XEXP (testreg, 0);
13916 if (MEM_P (testreg))
13917 mark_used_regs_combine (XEXP (testreg, 0));
13919 mark_used_regs_combine (SET_SRC (x));
13921 return;
13923 default:
13924 break;
13927 /* Recursively scan the operands of this expression. */
13930 const char *fmt = GET_RTX_FORMAT (code);
13932 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
13934 if (fmt[i] == 'e')
13935 mark_used_regs_combine (XEXP (x, i));
13936 else if (fmt[i] == 'E')
13938 int j;
13940 for (j = 0; j < XVECLEN (x, i); j++)
13941 mark_used_regs_combine (XVECEXP (x, i, j));
13947 /* Remove register number REGNO from the dead registers list of INSN.
13949 Return the note used to record the death, if there was one. */
13952 remove_death (unsigned int regno, rtx_insn *insn)
13954 rtx note = find_regno_note (insn, REG_DEAD, regno);
13956 if (note)
13957 remove_note (insn, note);
13959 return note;
13962 /* For each register (hardware or pseudo) used within expression X, if its
13963 death is in an instruction with luid between FROM_LUID (inclusive) and
13964 TO_INSN (exclusive), put a REG_DEAD note for that register in the
13965 list headed by PNOTES.
13967 That said, don't move registers killed by maybe_kill_insn.
13969 This is done when X is being merged by combination into TO_INSN. These
13970 notes will then be distributed as needed. */
13972 static void
13973 move_deaths (rtx x, rtx maybe_kill_insn, int from_luid, rtx_insn *to_insn,
13974 rtx *pnotes)
13976 const char *fmt;
13977 int len, i;
13978 enum rtx_code code = GET_CODE (x);
13980 if (code == REG)
13982 unsigned int regno = REGNO (x);
13983 rtx_insn *where_dead = reg_stat[regno].last_death;
13985 /* If we do not know where the register died, it may still die between
13986 FROM_LUID and TO_INSN. If so, find it. This is PR83304. */
13987 if (!where_dead || DF_INSN_LUID (where_dead) >= DF_INSN_LUID (to_insn))
13989 rtx_insn *insn = prev_real_nondebug_insn (to_insn);
13990 while (insn
13991 && BLOCK_FOR_INSN (insn) == BLOCK_FOR_INSN (to_insn)
13992 && DF_INSN_LUID (insn) >= from_luid)
13994 if (dead_or_set_regno_p (insn, regno))
13996 if (find_regno_note (insn, REG_DEAD, regno))
13997 where_dead = insn;
13998 break;
14001 insn = prev_real_nondebug_insn (insn);
14005 /* Don't move the register if it gets killed in between from and to. */
14006 if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
14007 && ! reg_referenced_p (x, maybe_kill_insn))
14008 return;
14010 if (where_dead
14011 && BLOCK_FOR_INSN (where_dead) == BLOCK_FOR_INSN (to_insn)
14012 && DF_INSN_LUID (where_dead) >= from_luid
14013 && DF_INSN_LUID (where_dead) < DF_INSN_LUID (to_insn))
14015 rtx note = remove_death (regno, where_dead);
14017 /* It is possible for the call above to return 0. This can occur
14018 when last_death points to I2 or I1 that we combined with.
14019 In that case make a new note.
14021 We must also check for the case where X is a hard register
14022 and NOTE is a death note for a range of hard registers
14023 including X. In that case, we must put REG_DEAD notes for
14024 the remaining registers in place of NOTE. */
14026 if (note != 0 && regno < FIRST_PSEUDO_REGISTER
14027 && partial_subreg_p (GET_MODE (x), GET_MODE (XEXP (note, 0))))
14029 unsigned int deadregno = REGNO (XEXP (note, 0));
14030 unsigned int deadend = END_REGNO (XEXP (note, 0));
14031 unsigned int ourend = END_REGNO (x);
14032 unsigned int i;
14034 for (i = deadregno; i < deadend; i++)
14035 if (i < regno || i >= ourend)
14036 add_reg_note (where_dead, REG_DEAD, regno_reg_rtx[i]);
14039 /* If we didn't find any note, or if we found a REG_DEAD note that
14040 covers only part of the given reg, and we have a multi-reg hard
14041 register, then to be safe we must check for REG_DEAD notes
14042 for each register other than the first. They could have
14043 their own REG_DEAD notes lying around. */
14044 else if ((note == 0
14045 || (note != 0
14046 && partial_subreg_p (GET_MODE (XEXP (note, 0)),
14047 GET_MODE (x))))
14048 && regno < FIRST_PSEUDO_REGISTER
14049 && REG_NREGS (x) > 1)
14051 unsigned int ourend = END_REGNO (x);
14052 unsigned int i, offset;
14053 rtx oldnotes = 0;
14055 if (note)
14056 offset = hard_regno_nregs (regno, GET_MODE (XEXP (note, 0)));
14057 else
14058 offset = 1;
14060 for (i = regno + offset; i < ourend; i++)
14061 move_deaths (regno_reg_rtx[i],
14062 maybe_kill_insn, from_luid, to_insn, &oldnotes);
14065 if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
14067 XEXP (note, 1) = *pnotes;
14068 *pnotes = note;
14070 else
14071 *pnotes = alloc_reg_note (REG_DEAD, x, *pnotes);
14074 return;
14077 else if (GET_CODE (x) == SET)
14079 rtx dest = SET_DEST (x);
14081 move_deaths (SET_SRC (x), maybe_kill_insn, from_luid, to_insn, pnotes);
14083 /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
14084 that accesses one word of a multi-word item, some
14085 piece of everything register in the expression is used by
14086 this insn, so remove any old death. */
14087 /* ??? So why do we test for equality of the sizes? */
14089 if (GET_CODE (dest) == ZERO_EXTRACT
14090 || GET_CODE (dest) == STRICT_LOW_PART
14091 || (GET_CODE (dest) == SUBREG
14092 && !read_modify_subreg_p (dest)))
14094 move_deaths (dest, maybe_kill_insn, from_luid, to_insn, pnotes);
14095 return;
14098 /* If this is some other SUBREG, we know it replaces the entire
14099 value, so use that as the destination. */
14100 if (GET_CODE (dest) == SUBREG)
14101 dest = SUBREG_REG (dest);
14103 /* If this is a MEM, adjust deaths of anything used in the address.
14104 For a REG (the only other possibility), the entire value is
14105 being replaced so the old value is not used in this insn. */
14107 if (MEM_P (dest))
14108 move_deaths (XEXP (dest, 0), maybe_kill_insn, from_luid,
14109 to_insn, pnotes);
14110 return;
14113 else if (GET_CODE (x) == CLOBBER)
14114 return;
14116 len = GET_RTX_LENGTH (code);
14117 fmt = GET_RTX_FORMAT (code);
14119 for (i = 0; i < len; i++)
14121 if (fmt[i] == 'E')
14123 int j;
14124 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
14125 move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_luid,
14126 to_insn, pnotes);
14128 else if (fmt[i] == 'e')
14129 move_deaths (XEXP (x, i), maybe_kill_insn, from_luid, to_insn, pnotes);
14133 /* Return 1 if X is the target of a bit-field assignment in BODY, the
14134 pattern of an insn. X must be a REG. */
14136 static int
14137 reg_bitfield_target_p (rtx x, rtx body)
14139 int i;
14141 if (GET_CODE (body) == SET)
14143 rtx dest = SET_DEST (body);
14144 rtx target;
14145 unsigned int regno, tregno, endregno, endtregno;
14147 if (GET_CODE (dest) == ZERO_EXTRACT)
14148 target = XEXP (dest, 0);
14149 else if (GET_CODE (dest) == STRICT_LOW_PART)
14150 target = SUBREG_REG (XEXP (dest, 0));
14151 else
14152 return 0;
14154 if (GET_CODE (target) == SUBREG)
14155 target = SUBREG_REG (target);
14157 if (!REG_P (target))
14158 return 0;
14160 tregno = REGNO (target), regno = REGNO (x);
14161 if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
14162 return target == x;
14164 endtregno = end_hard_regno (GET_MODE (target), tregno);
14165 endregno = end_hard_regno (GET_MODE (x), regno);
14167 return endregno > tregno && regno < endtregno;
14170 else if (GET_CODE (body) == PARALLEL)
14171 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
14172 if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
14173 return 1;
14175 return 0;
14178 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
14179 as appropriate. I3 and I2 are the insns resulting from the combination
14180 insns including FROM (I2 may be zero).
14182 ELIM_I2 and ELIM_I1 are either zero or registers that we know will
14183 not need REG_DEAD notes because they are being substituted for. This
14184 saves searching in the most common cases.
14186 Each note in the list is either ignored or placed on some insns, depending
14187 on the type of note. */
14189 static void
14190 distribute_notes (rtx notes, rtx_insn *from_insn, rtx_insn *i3, rtx_insn *i2,
14191 rtx elim_i2, rtx elim_i1, rtx elim_i0)
14193 rtx note, next_note;
14194 rtx tem_note;
14195 rtx_insn *tem_insn;
14197 for (note = notes; note; note = next_note)
14199 rtx_insn *place = 0, *place2 = 0;
14201 next_note = XEXP (note, 1);
14202 switch (REG_NOTE_KIND (note))
14204 case REG_BR_PROB:
14205 case REG_BR_PRED:
14206 /* Doesn't matter much where we put this, as long as it's somewhere.
14207 It is preferable to keep these notes on branches, which is most
14208 likely to be i3. */
14209 place = i3;
14210 break;
14212 case REG_NON_LOCAL_GOTO:
14213 if (JUMP_P (i3))
14214 place = i3;
14215 else
14217 gcc_assert (i2 && JUMP_P (i2));
14218 place = i2;
14220 break;
14222 case REG_EH_REGION:
14223 /* These notes must remain with the call or trapping instruction. */
14224 if (CALL_P (i3))
14225 place = i3;
14226 else if (i2 && CALL_P (i2))
14227 place = i2;
14228 else
14230 gcc_assert (cfun->can_throw_non_call_exceptions);
14231 if (may_trap_p (i3))
14232 place = i3;
14233 else if (i2 && may_trap_p (i2))
14234 place = i2;
14235 /* ??? Otherwise assume we've combined things such that we
14236 can now prove that the instructions can't trap. Drop the
14237 note in this case. */
14239 break;
14241 case REG_ARGS_SIZE:
14242 /* ??? How to distribute between i3-i1. Assume i3 contains the
14243 entire adjustment. Assert i3 contains at least some adjust. */
14244 if (!noop_move_p (i3))
14246 poly_int64 old_size, args_size = get_args_size (note);
14247 /* fixup_args_size_notes looks at REG_NORETURN note,
14248 so ensure the note is placed there first. */
14249 if (CALL_P (i3))
14251 rtx *np;
14252 for (np = &next_note; *np; np = &XEXP (*np, 1))
14253 if (REG_NOTE_KIND (*np) == REG_NORETURN)
14255 rtx n = *np;
14256 *np = XEXP (n, 1);
14257 XEXP (n, 1) = REG_NOTES (i3);
14258 REG_NOTES (i3) = n;
14259 break;
14262 old_size = fixup_args_size_notes (PREV_INSN (i3), i3, args_size);
14263 /* emit_call_1 adds for !ACCUMULATE_OUTGOING_ARGS
14264 REG_ARGS_SIZE note to all noreturn calls, allow that here. */
14265 gcc_assert (maybe_ne (old_size, args_size)
14266 || (CALL_P (i3)
14267 && !ACCUMULATE_OUTGOING_ARGS
14268 && find_reg_note (i3, REG_NORETURN, NULL_RTX)));
14270 break;
14272 case REG_NORETURN:
14273 case REG_SETJMP:
14274 case REG_TM:
14275 case REG_CALL_DECL:
14276 case REG_CALL_NOCF_CHECK:
14277 /* These notes must remain with the call. It should not be
14278 possible for both I2 and I3 to be a call. */
14279 if (CALL_P (i3))
14280 place = i3;
14281 else
14283 gcc_assert (i2 && CALL_P (i2));
14284 place = i2;
14286 break;
14288 case REG_UNUSED:
14289 /* Any clobbers for i3 may still exist, and so we must process
14290 REG_UNUSED notes from that insn.
14292 Any clobbers from i2 or i1 can only exist if they were added by
14293 recog_for_combine. In that case, recog_for_combine created the
14294 necessary REG_UNUSED notes. Trying to keep any original
14295 REG_UNUSED notes from these insns can cause incorrect output
14296 if it is for the same register as the original i3 dest.
14297 In that case, we will notice that the register is set in i3,
14298 and then add a REG_UNUSED note for the destination of i3, which
14299 is wrong. However, it is possible to have REG_UNUSED notes from
14300 i2 or i1 for register which were both used and clobbered, so
14301 we keep notes from i2 or i1 if they will turn into REG_DEAD
14302 notes. */
14304 /* If this register is set or clobbered in I3, put the note there
14305 unless there is one already. */
14306 if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
14308 if (from_insn != i3)
14309 break;
14311 if (! (REG_P (XEXP (note, 0))
14312 ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
14313 : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
14314 place = i3;
14316 /* Otherwise, if this register is used by I3, then this register
14317 now dies here, so we must put a REG_DEAD note here unless there
14318 is one already. */
14319 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
14320 && ! (REG_P (XEXP (note, 0))
14321 ? find_regno_note (i3, REG_DEAD,
14322 REGNO (XEXP (note, 0)))
14323 : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
14325 PUT_REG_NOTE_KIND (note, REG_DEAD);
14326 place = i3;
14329 /* A SET or CLOBBER of the REG_UNUSED reg has been removed,
14330 but we can't tell which at this point. We must reset any
14331 expectations we had about the value that was previously
14332 stored in the reg. ??? Ideally, we'd adjust REG_N_SETS
14333 and, if appropriate, restore its previous value, but we
14334 don't have enough information for that at this point. */
14335 else
14337 record_value_for_reg (XEXP (note, 0), NULL, NULL_RTX);
14339 /* Otherwise, if this register is now referenced in i2
14340 then the register used to be modified in one of the
14341 original insns. If it was i3 (say, in an unused
14342 parallel), it's now completely gone, so the note can
14343 be discarded. But if it was modified in i2, i1 or i0
14344 and we still reference it in i2, then we're
14345 referencing the previous value, and since the
14346 register was modified and REG_UNUSED, we know that
14347 the previous value is now dead. So, if we only
14348 reference the register in i2, we change the note to
14349 REG_DEAD, to reflect the previous value. However, if
14350 we're also setting or clobbering the register as
14351 scratch, we know (because the register was not
14352 referenced in i3) that it's unused, just as it was
14353 unused before, and we place the note in i2. */
14354 if (from_insn != i3 && i2 && INSN_P (i2)
14355 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
14357 if (!reg_set_p (XEXP (note, 0), PATTERN (i2)))
14358 PUT_REG_NOTE_KIND (note, REG_DEAD);
14359 if (! (REG_P (XEXP (note, 0))
14360 ? find_regno_note (i2, REG_NOTE_KIND (note),
14361 REGNO (XEXP (note, 0)))
14362 : find_reg_note (i2, REG_NOTE_KIND (note),
14363 XEXP (note, 0))))
14364 place = i2;
14368 break;
14370 case REG_EQUAL:
14371 case REG_EQUIV:
14372 case REG_NOALIAS:
14373 /* These notes say something about results of an insn. We can
14374 only support them if they used to be on I3 in which case they
14375 remain on I3. Otherwise they are ignored.
14377 If the note refers to an expression that is not a constant, we
14378 must also ignore the note since we cannot tell whether the
14379 equivalence is still true. It might be possible to do
14380 slightly better than this (we only have a problem if I2DEST
14381 or I1DEST is present in the expression), but it doesn't
14382 seem worth the trouble. */
14384 if (from_insn == i3
14385 && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
14386 place = i3;
14387 break;
14389 case REG_INC:
14390 /* These notes say something about how a register is used. They must
14391 be present on any use of the register in I2 or I3. */
14392 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
14393 place = i3;
14395 if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
14397 if (place)
14398 place2 = i2;
14399 else
14400 place = i2;
14402 break;
14404 case REG_LABEL_TARGET:
14405 case REG_LABEL_OPERAND:
14406 /* This can show up in several ways -- either directly in the
14407 pattern, or hidden off in the constant pool with (or without?)
14408 a REG_EQUAL note. */
14409 /* ??? Ignore the without-reg_equal-note problem for now. */
14410 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
14411 || ((tem_note = find_reg_note (i3, REG_EQUAL, NULL_RTX))
14412 && GET_CODE (XEXP (tem_note, 0)) == LABEL_REF
14413 && label_ref_label (XEXP (tem_note, 0)) == XEXP (note, 0)))
14414 place = i3;
14416 if (i2
14417 && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
14418 || ((tem_note = find_reg_note (i2, REG_EQUAL, NULL_RTX))
14419 && GET_CODE (XEXP (tem_note, 0)) == LABEL_REF
14420 && label_ref_label (XEXP (tem_note, 0)) == XEXP (note, 0))))
14422 if (place)
14423 place2 = i2;
14424 else
14425 place = i2;
14428 /* For REG_LABEL_TARGET on a JUMP_P, we prefer to put the note
14429 as a JUMP_LABEL or decrement LABEL_NUSES if it's already
14430 there. */
14431 if (place && JUMP_P (place)
14432 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
14433 && (JUMP_LABEL (place) == NULL
14434 || JUMP_LABEL (place) == XEXP (note, 0)))
14436 rtx label = JUMP_LABEL (place);
14438 if (!label)
14439 JUMP_LABEL (place) = XEXP (note, 0);
14440 else if (LABEL_P (label))
14441 LABEL_NUSES (label)--;
14444 if (place2 && JUMP_P (place2)
14445 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
14446 && (JUMP_LABEL (place2) == NULL
14447 || JUMP_LABEL (place2) == XEXP (note, 0)))
14449 rtx label = JUMP_LABEL (place2);
14451 if (!label)
14452 JUMP_LABEL (place2) = XEXP (note, 0);
14453 else if (LABEL_P (label))
14454 LABEL_NUSES (label)--;
14455 place2 = 0;
14457 break;
14459 case REG_NONNEG:
14460 /* This note says something about the value of a register prior
14461 to the execution of an insn. It is too much trouble to see
14462 if the note is still correct in all situations. It is better
14463 to simply delete it. */
14464 break;
14466 case REG_DEAD:
14467 /* If we replaced the right hand side of FROM_INSN with a
14468 REG_EQUAL note, the original use of the dying register
14469 will not have been combined into I3 and I2. In such cases,
14470 FROM_INSN is guaranteed to be the first of the combined
14471 instructions, so we simply need to search back before
14472 FROM_INSN for the previous use or set of this register,
14473 then alter the notes there appropriately.
14475 If the register is used as an input in I3, it dies there.
14476 Similarly for I2, if it is nonzero and adjacent to I3.
14478 If the register is not used as an input in either I3 or I2
14479 and it is not one of the registers we were supposed to eliminate,
14480 there are two possibilities. We might have a non-adjacent I2
14481 or we might have somehow eliminated an additional register
14482 from a computation. For example, we might have had A & B where
14483 we discover that B will always be zero. In this case we will
14484 eliminate the reference to A.
14486 In both cases, we must search to see if we can find a previous
14487 use of A and put the death note there. */
14489 if (from_insn
14490 && from_insn == i2mod
14491 && !reg_overlap_mentioned_p (XEXP (note, 0), i2mod_new_rhs))
14492 tem_insn = from_insn;
14493 else
14495 if (from_insn
14496 && CALL_P (from_insn)
14497 && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
14498 place = from_insn;
14499 else if (i2 && reg_set_p (XEXP (note, 0), PATTERN (i2)))
14501 /* If the new I2 sets the same register that is marked
14502 dead in the note, we do not in general know where to
14503 put the note. One important case we _can_ handle is
14504 when the note comes from I3. */
14505 if (from_insn == i3)
14506 place = i3;
14507 else
14508 break;
14510 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
14511 place = i3;
14512 else if (i2 != 0 && next_nonnote_nondebug_insn (i2) == i3
14513 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
14514 place = i2;
14515 else if ((rtx_equal_p (XEXP (note, 0), elim_i2)
14516 && !(i2mod
14517 && reg_overlap_mentioned_p (XEXP (note, 0),
14518 i2mod_old_rhs)))
14519 || rtx_equal_p (XEXP (note, 0), elim_i1)
14520 || rtx_equal_p (XEXP (note, 0), elim_i0))
14521 break;
14522 tem_insn = i3;
14525 if (place == 0)
14527 basic_block bb = this_basic_block;
14529 for (tem_insn = PREV_INSN (tem_insn); place == 0; tem_insn = PREV_INSN (tem_insn))
14531 if (!NONDEBUG_INSN_P (tem_insn))
14533 if (tem_insn == BB_HEAD (bb))
14534 break;
14535 continue;
14538 /* If the register is being set at TEM_INSN, see if that is all
14539 TEM_INSN is doing. If so, delete TEM_INSN. Otherwise, make this
14540 into a REG_UNUSED note instead. Don't delete sets to
14541 global register vars. */
14542 if ((REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER
14543 || !global_regs[REGNO (XEXP (note, 0))])
14544 && reg_set_p (XEXP (note, 0), PATTERN (tem_insn)))
14546 rtx set = single_set (tem_insn);
14547 rtx inner_dest = 0;
14548 rtx_insn *cc0_setter = NULL;
14550 if (set != 0)
14551 for (inner_dest = SET_DEST (set);
14552 (GET_CODE (inner_dest) == STRICT_LOW_PART
14553 || GET_CODE (inner_dest) == SUBREG
14554 || GET_CODE (inner_dest) == ZERO_EXTRACT);
14555 inner_dest = XEXP (inner_dest, 0))
14558 /* Verify that it was the set, and not a clobber that
14559 modified the register.
14561 CC0 targets must be careful to maintain setter/user
14562 pairs. If we cannot delete the setter due to side
14563 effects, mark the user with an UNUSED note instead
14564 of deleting it. */
14566 if (set != 0 && ! side_effects_p (SET_SRC (set))
14567 && rtx_equal_p (XEXP (note, 0), inner_dest)
14568 && (!HAVE_cc0
14569 || (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
14570 || ((cc0_setter = prev_cc0_setter (tem_insn)) != NULL
14571 && sets_cc0_p (PATTERN (cc0_setter)) > 0))))
14573 /* Move the notes and links of TEM_INSN elsewhere.
14574 This might delete other dead insns recursively.
14575 First set the pattern to something that won't use
14576 any register. */
14577 rtx old_notes = REG_NOTES (tem_insn);
14579 PATTERN (tem_insn) = pc_rtx;
14580 REG_NOTES (tem_insn) = NULL;
14582 distribute_notes (old_notes, tem_insn, tem_insn, NULL,
14583 NULL_RTX, NULL_RTX, NULL_RTX);
14584 distribute_links (LOG_LINKS (tem_insn));
14586 unsigned int regno = REGNO (XEXP (note, 0));
14587 reg_stat_type *rsp = &reg_stat[regno];
14588 if (rsp->last_set == tem_insn)
14589 record_value_for_reg (XEXP (note, 0), NULL, NULL_RTX);
14591 SET_INSN_DELETED (tem_insn);
14592 if (tem_insn == i2)
14593 i2 = NULL;
14595 /* Delete the setter too. */
14596 if (cc0_setter)
14598 PATTERN (cc0_setter) = pc_rtx;
14599 old_notes = REG_NOTES (cc0_setter);
14600 REG_NOTES (cc0_setter) = NULL;
14602 distribute_notes (old_notes, cc0_setter,
14603 cc0_setter, NULL,
14604 NULL_RTX, NULL_RTX, NULL_RTX);
14605 distribute_links (LOG_LINKS (cc0_setter));
14607 SET_INSN_DELETED (cc0_setter);
14608 if (cc0_setter == i2)
14609 i2 = NULL;
14612 else
14614 PUT_REG_NOTE_KIND (note, REG_UNUSED);
14616 /* If there isn't already a REG_UNUSED note, put one
14617 here. Do not place a REG_DEAD note, even if
14618 the register is also used here; that would not
14619 match the algorithm used in lifetime analysis
14620 and can cause the consistency check in the
14621 scheduler to fail. */
14622 if (! find_regno_note (tem_insn, REG_UNUSED,
14623 REGNO (XEXP (note, 0))))
14624 place = tem_insn;
14625 break;
14628 else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem_insn))
14629 || (CALL_P (tem_insn)
14630 && find_reg_fusage (tem_insn, USE, XEXP (note, 0))))
14632 place = tem_insn;
14634 /* If we are doing a 3->2 combination, and we have a
14635 register which formerly died in i3 and was not used
14636 by i2, which now no longer dies in i3 and is used in
14637 i2 but does not die in i2, and place is between i2
14638 and i3, then we may need to move a link from place to
14639 i2. */
14640 if (i2 && DF_INSN_LUID (place) > DF_INSN_LUID (i2)
14641 && from_insn
14642 && DF_INSN_LUID (from_insn) > DF_INSN_LUID (i2)
14643 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
14645 struct insn_link *links = LOG_LINKS (place);
14646 LOG_LINKS (place) = NULL;
14647 distribute_links (links);
14649 break;
14652 if (tem_insn == BB_HEAD (bb))
14653 break;
14658 /* If the register is set or already dead at PLACE, we needn't do
14659 anything with this note if it is still a REG_DEAD note.
14660 We check here if it is set at all, not if is it totally replaced,
14661 which is what `dead_or_set_p' checks, so also check for it being
14662 set partially. */
14664 if (place && REG_NOTE_KIND (note) == REG_DEAD)
14666 unsigned int regno = REGNO (XEXP (note, 0));
14667 reg_stat_type *rsp = &reg_stat[regno];
14669 if (dead_or_set_p (place, XEXP (note, 0))
14670 || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
14672 /* Unless the register previously died in PLACE, clear
14673 last_death. [I no longer understand why this is
14674 being done.] */
14675 if (rsp->last_death != place)
14676 rsp->last_death = 0;
14677 place = 0;
14679 else
14680 rsp->last_death = place;
14682 /* If this is a death note for a hard reg that is occupying
14683 multiple registers, ensure that we are still using all
14684 parts of the object. If we find a piece of the object
14685 that is unused, we must arrange for an appropriate REG_DEAD
14686 note to be added for it. However, we can't just emit a USE
14687 and tag the note to it, since the register might actually
14688 be dead; so we recourse, and the recursive call then finds
14689 the previous insn that used this register. */
14691 if (place && REG_NREGS (XEXP (note, 0)) > 1)
14693 unsigned int endregno = END_REGNO (XEXP (note, 0));
14694 bool all_used = true;
14695 unsigned int i;
14697 for (i = regno; i < endregno; i++)
14698 if ((! refers_to_regno_p (i, PATTERN (place))
14699 && ! find_regno_fusage (place, USE, i))
14700 || dead_or_set_regno_p (place, i))
14702 all_used = false;
14703 break;
14706 if (! all_used)
14708 /* Put only REG_DEAD notes for pieces that are
14709 not already dead or set. */
14711 for (i = regno; i < endregno;
14712 i += hard_regno_nregs (i, reg_raw_mode[i]))
14714 rtx piece = regno_reg_rtx[i];
14715 basic_block bb = this_basic_block;
14717 if (! dead_or_set_p (place, piece)
14718 && ! reg_bitfield_target_p (piece,
14719 PATTERN (place)))
14721 rtx new_note = alloc_reg_note (REG_DEAD, piece,
14722 NULL_RTX);
14724 distribute_notes (new_note, place, place,
14725 NULL, NULL_RTX, NULL_RTX,
14726 NULL_RTX);
14728 else if (! refers_to_regno_p (i, PATTERN (place))
14729 && ! find_regno_fusage (place, USE, i))
14730 for (tem_insn = PREV_INSN (place); ;
14731 tem_insn = PREV_INSN (tem_insn))
14733 if (!NONDEBUG_INSN_P (tem_insn))
14735 if (tem_insn == BB_HEAD (bb))
14736 break;
14737 continue;
14739 if (dead_or_set_p (tem_insn, piece)
14740 || reg_bitfield_target_p (piece,
14741 PATTERN (tem_insn)))
14743 add_reg_note (tem_insn, REG_UNUSED, piece);
14744 break;
14749 place = 0;
14753 break;
14755 default:
14756 /* Any other notes should not be present at this point in the
14757 compilation. */
14758 gcc_unreachable ();
14761 if (place)
14763 XEXP (note, 1) = REG_NOTES (place);
14764 REG_NOTES (place) = note;
14766 /* Set added_notes_insn to the earliest insn we added a note to. */
14767 if (added_notes_insn == 0
14768 || DF_INSN_LUID (added_notes_insn) > DF_INSN_LUID (place))
14769 added_notes_insn = place;
14772 if (place2)
14774 add_shallow_copy_of_reg_note (place2, note);
14776 /* Set added_notes_insn to the earliest insn we added a note to. */
14777 if (added_notes_insn == 0
14778 || DF_INSN_LUID (added_notes_insn) > DF_INSN_LUID (place2))
14779 added_notes_insn = place2;
14784 /* Similarly to above, distribute the LOG_LINKS that used to be present on
14785 I3, I2, and I1 to new locations. This is also called to add a link
14786 pointing at I3 when I3's destination is changed. */
14788 static void
14789 distribute_links (struct insn_link *links)
14791 struct insn_link *link, *next_link;
14793 for (link = links; link; link = next_link)
14795 rtx_insn *place = 0;
14796 rtx_insn *insn;
14797 rtx set, reg;
14799 next_link = link->next;
14801 /* If the insn that this link points to is a NOTE, ignore it. */
14802 if (NOTE_P (link->insn))
14803 continue;
14805 set = 0;
14806 rtx pat = PATTERN (link->insn);
14807 if (GET_CODE (pat) == SET)
14808 set = pat;
14809 else if (GET_CODE (pat) == PARALLEL)
14811 int i;
14812 for (i = 0; i < XVECLEN (pat, 0); i++)
14814 set = XVECEXP (pat, 0, i);
14815 if (GET_CODE (set) != SET)
14816 continue;
14818 reg = SET_DEST (set);
14819 while (GET_CODE (reg) == ZERO_EXTRACT
14820 || GET_CODE (reg) == STRICT_LOW_PART
14821 || GET_CODE (reg) == SUBREG)
14822 reg = XEXP (reg, 0);
14824 if (!REG_P (reg))
14825 continue;
14827 if (REGNO (reg) == link->regno)
14828 break;
14830 if (i == XVECLEN (pat, 0))
14831 continue;
14833 else
14834 continue;
14836 reg = SET_DEST (set);
14838 while (GET_CODE (reg) == ZERO_EXTRACT
14839 || GET_CODE (reg) == STRICT_LOW_PART
14840 || GET_CODE (reg) == SUBREG)
14841 reg = XEXP (reg, 0);
14843 if (reg == pc_rtx)
14844 continue;
14846 /* A LOG_LINK is defined as being placed on the first insn that uses
14847 a register and points to the insn that sets the register. Start
14848 searching at the next insn after the target of the link and stop
14849 when we reach a set of the register or the end of the basic block.
14851 Note that this correctly handles the link that used to point from
14852 I3 to I2. Also note that not much searching is typically done here
14853 since most links don't point very far away. */
14855 for (insn = NEXT_INSN (link->insn);
14856 (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
14857 || BB_HEAD (this_basic_block->next_bb) != insn));
14858 insn = NEXT_INSN (insn))
14859 if (DEBUG_INSN_P (insn))
14860 continue;
14861 else if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
14863 if (reg_referenced_p (reg, PATTERN (insn)))
14864 place = insn;
14865 break;
14867 else if (CALL_P (insn)
14868 && find_reg_fusage (insn, USE, reg))
14870 place = insn;
14871 break;
14873 else if (INSN_P (insn) && reg_set_p (reg, insn))
14874 break;
14876 /* If we found a place to put the link, place it there unless there
14877 is already a link to the same insn as LINK at that point. */
14879 if (place)
14881 struct insn_link *link2;
14883 FOR_EACH_LOG_LINK (link2, place)
14884 if (link2->insn == link->insn && link2->regno == link->regno)
14885 break;
14887 if (link2 == NULL)
14889 link->next = LOG_LINKS (place);
14890 LOG_LINKS (place) = link;
14892 /* Set added_links_insn to the earliest insn we added a
14893 link to. */
14894 if (added_links_insn == 0
14895 || DF_INSN_LUID (added_links_insn) > DF_INSN_LUID (place))
14896 added_links_insn = place;
14902 /* Check for any register or memory mentioned in EQUIV that is not
14903 mentioned in EXPR. This is used to restrict EQUIV to "specializations"
14904 of EXPR where some registers may have been replaced by constants. */
14906 static bool
14907 unmentioned_reg_p (rtx equiv, rtx expr)
14909 subrtx_iterator::array_type array;
14910 FOR_EACH_SUBRTX (iter, array, equiv, NONCONST)
14912 const_rtx x = *iter;
14913 if ((REG_P (x) || MEM_P (x))
14914 && !reg_mentioned_p (x, expr))
14915 return true;
14917 return false;
14920 DEBUG_FUNCTION void
14921 dump_combine_stats (FILE *file)
14923 fprintf
14924 (file,
14925 ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
14926 combine_attempts, combine_merges, combine_extras, combine_successes);
14929 void
14930 dump_combine_total_stats (FILE *file)
14932 fprintf
14933 (file,
14934 "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
14935 total_attempts, total_merges, total_extras, total_successes);
14938 /* Try combining insns through substitution. */
14939 static unsigned int
14940 rest_of_handle_combine (void)
14942 int rebuild_jump_labels_after_combine;
14944 df_set_flags (DF_LR_RUN_DCE + DF_DEFER_INSN_RESCAN);
14945 df_note_add_problem ();
14946 df_analyze ();
14948 regstat_init_n_sets_and_refs ();
14949 reg_n_sets_max = max_reg_num ();
14951 rebuild_jump_labels_after_combine
14952 = combine_instructions (get_insns (), max_reg_num ());
14954 /* Combining insns may have turned an indirect jump into a
14955 direct jump. Rebuild the JUMP_LABEL fields of jumping
14956 instructions. */
14957 if (rebuild_jump_labels_after_combine)
14959 if (dom_info_available_p (CDI_DOMINATORS))
14960 free_dominance_info (CDI_DOMINATORS);
14961 timevar_push (TV_JUMP);
14962 rebuild_jump_labels (get_insns ());
14963 cleanup_cfg (0);
14964 timevar_pop (TV_JUMP);
14967 regstat_free_n_sets_and_refs ();
14968 return 0;
14971 namespace {
14973 const pass_data pass_data_combine =
14975 RTL_PASS, /* type */
14976 "combine", /* name */
14977 OPTGROUP_NONE, /* optinfo_flags */
14978 TV_COMBINE, /* tv_id */
14979 PROP_cfglayout, /* properties_required */
14980 0, /* properties_provided */
14981 0, /* properties_destroyed */
14982 0, /* todo_flags_start */
14983 TODO_df_finish, /* todo_flags_finish */
14986 class pass_combine : public rtl_opt_pass
14988 public:
14989 pass_combine (gcc::context *ctxt)
14990 : rtl_opt_pass (pass_data_combine, ctxt)
14993 /* opt_pass methods: */
14994 virtual bool gate (function *) { return (optimize > 0); }
14995 virtual unsigned int execute (function *)
14997 return rest_of_handle_combine ();
15000 }; // class pass_combine
15002 } // anon namespace
15004 rtl_opt_pass *
15005 make_pass_combine (gcc::context *ctxt)
15007 return new pass_combine (ctxt);