[NDS32] Add intrinsic function for ffb, ffmism and flmism.
[official-gcc.git] / gcc / combine.c
blobc9105ed02b381ae62bec514a06aef9ab74f0f517
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 return 0;
575 case SET:
576 /* If the destination is anything other than CC0, PC, a REG or a SUBREG
577 of a REG that occupies all of the REG, the insn uses DEST if
578 it is mentioned in the destination or the source. Otherwise, we
579 need just check the source. */
580 if (GET_CODE (SET_DEST (x)) != CC0
581 && GET_CODE (SET_DEST (x)) != PC
582 && !REG_P (SET_DEST (x))
583 && ! (GET_CODE (SET_DEST (x)) == SUBREG
584 && REG_P (SUBREG_REG (SET_DEST (x)))
585 && !read_modify_subreg_p (SET_DEST (x))))
586 break;
588 return find_single_use_1 (dest, &SET_SRC (x));
590 case MEM:
591 case SUBREG:
592 return find_single_use_1 (dest, &XEXP (x, 0));
594 default:
595 break;
598 /* If it wasn't one of the common cases above, check each expression and
599 vector of this code. Look for a unique usage of DEST. */
601 fmt = GET_RTX_FORMAT (code);
602 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
604 if (fmt[i] == 'e')
606 if (dest == XEXP (x, i)
607 || (REG_P (dest) && REG_P (XEXP (x, i))
608 && REGNO (dest) == REGNO (XEXP (x, i))))
609 this_result = loc;
610 else
611 this_result = find_single_use_1 (dest, &XEXP (x, i));
613 if (result == NULL)
614 result = this_result;
615 else if (this_result)
616 /* Duplicate usage. */
617 return NULL;
619 else if (fmt[i] == 'E')
621 int j;
623 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
625 if (XVECEXP (x, i, j) == dest
626 || (REG_P (dest)
627 && REG_P (XVECEXP (x, i, j))
628 && REGNO (XVECEXP (x, i, j)) == REGNO (dest)))
629 this_result = loc;
630 else
631 this_result = find_single_use_1 (dest, &XVECEXP (x, i, j));
633 if (result == NULL)
634 result = this_result;
635 else if (this_result)
636 return NULL;
641 return result;
645 /* See if DEST, produced in INSN, is used only a single time in the
646 sequel. If so, return a pointer to the innermost rtx expression in which
647 it is used.
649 If PLOC is nonzero, *PLOC is set to the insn containing the single use.
651 If DEST is cc0_rtx, we look only at the next insn. In that case, we don't
652 care about REG_DEAD notes or LOG_LINKS.
654 Otherwise, we find the single use by finding an insn that has a
655 LOG_LINKS pointing at INSN and has a REG_DEAD note for DEST. If DEST is
656 only referenced once in that insn, we know that it must be the first
657 and last insn referencing DEST. */
659 static rtx *
660 find_single_use (rtx dest, rtx_insn *insn, rtx_insn **ploc)
662 basic_block bb;
663 rtx_insn *next;
664 rtx *result;
665 struct insn_link *link;
667 if (dest == cc0_rtx)
669 next = NEXT_INSN (insn);
670 if (next == 0
671 || (!NONJUMP_INSN_P (next) && !JUMP_P (next)))
672 return 0;
674 result = find_single_use_1 (dest, &PATTERN (next));
675 if (result && ploc)
676 *ploc = next;
677 return result;
680 if (!REG_P (dest))
681 return 0;
683 bb = BLOCK_FOR_INSN (insn);
684 for (next = NEXT_INSN (insn);
685 next && BLOCK_FOR_INSN (next) == bb;
686 next = NEXT_INSN (next))
687 if (NONDEBUG_INSN_P (next) && dead_or_set_p (next, dest))
689 FOR_EACH_LOG_LINK (link, next)
690 if (link->insn == insn && link->regno == REGNO (dest))
691 break;
693 if (link)
695 result = find_single_use_1 (dest, &PATTERN (next));
696 if (ploc)
697 *ploc = next;
698 return result;
702 return 0;
705 /* Substitute NEWVAL, an rtx expression, into INTO, a place in some
706 insn. The substitution can be undone by undo_all. If INTO is already
707 set to NEWVAL, do not record this change. Because computing NEWVAL might
708 also call SUBST, we have to compute it before we put anything into
709 the undo table. */
711 static void
712 do_SUBST (rtx *into, rtx newval)
714 struct undo *buf;
715 rtx oldval = *into;
717 if (oldval == newval)
718 return;
720 /* We'd like to catch as many invalid transformations here as
721 possible. Unfortunately, there are way too many mode changes
722 that are perfectly valid, so we'd waste too much effort for
723 little gain doing the checks here. Focus on catching invalid
724 transformations involving integer constants. */
725 if (GET_MODE_CLASS (GET_MODE (oldval)) == MODE_INT
726 && CONST_INT_P (newval))
728 /* Sanity check that we're replacing oldval with a CONST_INT
729 that is a valid sign-extension for the original mode. */
730 gcc_assert (INTVAL (newval)
731 == trunc_int_for_mode (INTVAL (newval), GET_MODE (oldval)));
733 /* Replacing the operand of a SUBREG or a ZERO_EXTEND with a
734 CONST_INT is not valid, because after the replacement, the
735 original mode would be gone. Unfortunately, we can't tell
736 when do_SUBST is called to replace the operand thereof, so we
737 perform this test on oldval instead, checking whether an
738 invalid replacement took place before we got here. */
739 gcc_assert (!(GET_CODE (oldval) == SUBREG
740 && CONST_INT_P (SUBREG_REG (oldval))));
741 gcc_assert (!(GET_CODE (oldval) == ZERO_EXTEND
742 && CONST_INT_P (XEXP (oldval, 0))));
745 if (undobuf.frees)
746 buf = undobuf.frees, undobuf.frees = buf->next;
747 else
748 buf = XNEW (struct undo);
750 buf->kind = UNDO_RTX;
751 buf->where.r = into;
752 buf->old_contents.r = oldval;
753 *into = newval;
755 buf->next = undobuf.undos, undobuf.undos = buf;
758 #define SUBST(INTO, NEWVAL) do_SUBST (&(INTO), (NEWVAL))
760 /* Similar to SUBST, but NEWVAL is an int expression. Note that substitution
761 for the value of a HOST_WIDE_INT value (including CONST_INT) is
762 not safe. */
764 static void
765 do_SUBST_INT (int *into, int newval)
767 struct undo *buf;
768 int oldval = *into;
770 if (oldval == newval)
771 return;
773 if (undobuf.frees)
774 buf = undobuf.frees, undobuf.frees = buf->next;
775 else
776 buf = XNEW (struct undo);
778 buf->kind = UNDO_INT;
779 buf->where.i = into;
780 buf->old_contents.i = oldval;
781 *into = newval;
783 buf->next = undobuf.undos, undobuf.undos = buf;
786 #define SUBST_INT(INTO, NEWVAL) do_SUBST_INT (&(INTO), (NEWVAL))
788 /* Similar to SUBST, but just substitute the mode. This is used when
789 changing the mode of a pseudo-register, so that any other
790 references to the entry in the regno_reg_rtx array will change as
791 well. */
793 static void
794 do_SUBST_MODE (rtx *into, machine_mode newval)
796 struct undo *buf;
797 machine_mode oldval = GET_MODE (*into);
799 if (oldval == newval)
800 return;
802 if (undobuf.frees)
803 buf = undobuf.frees, undobuf.frees = buf->next;
804 else
805 buf = XNEW (struct undo);
807 buf->kind = UNDO_MODE;
808 buf->where.r = into;
809 buf->old_contents.m = oldval;
810 adjust_reg_mode (*into, newval);
812 buf->next = undobuf.undos, undobuf.undos = buf;
815 #define SUBST_MODE(INTO, NEWVAL) do_SUBST_MODE (&(INTO), (NEWVAL))
817 /* Similar to SUBST, but NEWVAL is a LOG_LINKS expression. */
819 static void
820 do_SUBST_LINK (struct insn_link **into, struct insn_link *newval)
822 struct undo *buf;
823 struct insn_link * oldval = *into;
825 if (oldval == newval)
826 return;
828 if (undobuf.frees)
829 buf = undobuf.frees, undobuf.frees = buf->next;
830 else
831 buf = XNEW (struct undo);
833 buf->kind = UNDO_LINKS;
834 buf->where.l = into;
835 buf->old_contents.l = oldval;
836 *into = newval;
838 buf->next = undobuf.undos, undobuf.undos = buf;
841 #define SUBST_LINK(oldval, newval) do_SUBST_LINK (&oldval, newval)
843 /* Subroutine of try_combine. Determine whether the replacement patterns
844 NEWPAT, NEWI2PAT and NEWOTHERPAT are cheaper according to insn_cost
845 than the original sequence I0, I1, I2, I3 and undobuf.other_insn. Note
846 that I0, I1 and/or NEWI2PAT may be NULL_RTX. Similarly, NEWOTHERPAT and
847 undobuf.other_insn may also both be NULL_RTX. Return false if the cost
848 of all the instructions can be estimated and the replacements are more
849 expensive than the original sequence. */
851 static bool
852 combine_validate_cost (rtx_insn *i0, rtx_insn *i1, rtx_insn *i2, rtx_insn *i3,
853 rtx newpat, rtx newi2pat, rtx newotherpat)
855 int i0_cost, i1_cost, i2_cost, i3_cost;
856 int new_i2_cost, new_i3_cost;
857 int old_cost, new_cost;
859 /* Lookup the original insn_costs. */
860 i2_cost = INSN_COST (i2);
861 i3_cost = INSN_COST (i3);
863 if (i1)
865 i1_cost = INSN_COST (i1);
866 if (i0)
868 i0_cost = INSN_COST (i0);
869 old_cost = (i0_cost > 0 && i1_cost > 0 && i2_cost > 0 && i3_cost > 0
870 ? i0_cost + i1_cost + i2_cost + i3_cost : 0);
872 else
874 old_cost = (i1_cost > 0 && i2_cost > 0 && i3_cost > 0
875 ? i1_cost + i2_cost + i3_cost : 0);
876 i0_cost = 0;
879 else
881 old_cost = (i2_cost > 0 && i3_cost > 0) ? i2_cost + i3_cost : 0;
882 i1_cost = i0_cost = 0;
885 /* If we have split a PARALLEL I2 to I1,I2, we have counted its cost twice;
886 correct that. */
887 if (old_cost && i1 && INSN_UID (i1) == INSN_UID (i2))
888 old_cost -= i1_cost;
891 /* Calculate the replacement insn_costs. */
892 rtx tmp = PATTERN (i3);
893 PATTERN (i3) = newpat;
894 int tmpi = INSN_CODE (i3);
895 INSN_CODE (i3) = -1;
896 new_i3_cost = insn_cost (i3, optimize_this_for_speed_p);
897 PATTERN (i3) = tmp;
898 INSN_CODE (i3) = tmpi;
899 if (newi2pat)
901 tmp = PATTERN (i2);
902 PATTERN (i2) = newi2pat;
903 tmpi = INSN_CODE (i2);
904 INSN_CODE (i2) = -1;
905 new_i2_cost = insn_cost (i2, optimize_this_for_speed_p);
906 PATTERN (i2) = tmp;
907 INSN_CODE (i2) = tmpi;
908 new_cost = (new_i2_cost > 0 && new_i3_cost > 0)
909 ? new_i2_cost + new_i3_cost : 0;
911 else
913 new_cost = new_i3_cost;
914 new_i2_cost = 0;
917 if (undobuf.other_insn)
919 int old_other_cost, new_other_cost;
921 old_other_cost = INSN_COST (undobuf.other_insn);
922 tmp = PATTERN (undobuf.other_insn);
923 PATTERN (undobuf.other_insn) = newotherpat;
924 tmpi = INSN_CODE (undobuf.other_insn);
925 INSN_CODE (undobuf.other_insn) = -1;
926 new_other_cost = insn_cost (undobuf.other_insn,
927 optimize_this_for_speed_p);
928 PATTERN (undobuf.other_insn) = tmp;
929 INSN_CODE (undobuf.other_insn) = tmpi;
930 if (old_other_cost > 0 && new_other_cost > 0)
932 old_cost += old_other_cost;
933 new_cost += new_other_cost;
935 else
936 old_cost = 0;
939 /* Disallow this combination if both new_cost and old_cost are greater than
940 zero, and new_cost is greater than old cost. */
941 int reject = old_cost > 0 && new_cost > old_cost;
943 if (dump_file)
945 fprintf (dump_file, "%s combination of insns ",
946 reject ? "rejecting" : "allowing");
947 if (i0)
948 fprintf (dump_file, "%d, ", INSN_UID (i0));
949 if (i1 && INSN_UID (i1) != INSN_UID (i2))
950 fprintf (dump_file, "%d, ", INSN_UID (i1));
951 fprintf (dump_file, "%d and %d\n", INSN_UID (i2), INSN_UID (i3));
953 fprintf (dump_file, "original costs ");
954 if (i0)
955 fprintf (dump_file, "%d + ", i0_cost);
956 if (i1 && INSN_UID (i1) != INSN_UID (i2))
957 fprintf (dump_file, "%d + ", i1_cost);
958 fprintf (dump_file, "%d + %d = %d\n", i2_cost, i3_cost, old_cost);
960 if (newi2pat)
961 fprintf (dump_file, "replacement costs %d + %d = %d\n",
962 new_i2_cost, new_i3_cost, new_cost);
963 else
964 fprintf (dump_file, "replacement cost %d\n", new_cost);
967 if (reject)
968 return false;
970 /* Update the uid_insn_cost array with the replacement costs. */
971 INSN_COST (i2) = new_i2_cost;
972 INSN_COST (i3) = new_i3_cost;
973 if (i1)
975 INSN_COST (i1) = 0;
976 if (i0)
977 INSN_COST (i0) = 0;
980 return true;
984 /* Delete any insns that copy a register to itself. */
986 static void
987 delete_noop_moves (void)
989 rtx_insn *insn, *next;
990 basic_block bb;
992 FOR_EACH_BB_FN (bb, cfun)
994 for (insn = BB_HEAD (bb); insn != NEXT_INSN (BB_END (bb)); insn = next)
996 next = NEXT_INSN (insn);
997 if (INSN_P (insn) && noop_move_p (insn))
999 if (dump_file)
1000 fprintf (dump_file, "deleting noop move %d\n", INSN_UID (insn));
1002 delete_insn_and_edges (insn);
1009 /* Return false if we do not want to (or cannot) combine DEF. */
1010 static bool
1011 can_combine_def_p (df_ref def)
1013 /* Do not consider if it is pre/post modification in MEM. */
1014 if (DF_REF_FLAGS (def) & DF_REF_PRE_POST_MODIFY)
1015 return false;
1017 unsigned int regno = DF_REF_REGNO (def);
1019 /* Do not combine frame pointer adjustments. */
1020 if ((regno == FRAME_POINTER_REGNUM
1021 && (!reload_completed || frame_pointer_needed))
1022 || (!HARD_FRAME_POINTER_IS_FRAME_POINTER
1023 && regno == HARD_FRAME_POINTER_REGNUM
1024 && (!reload_completed || frame_pointer_needed))
1025 || (FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1026 && regno == ARG_POINTER_REGNUM && fixed_regs[regno]))
1027 return false;
1029 return true;
1032 /* Return false if we do not want to (or cannot) combine USE. */
1033 static bool
1034 can_combine_use_p (df_ref use)
1036 /* Do not consider the usage of the stack pointer by function call. */
1037 if (DF_REF_FLAGS (use) & DF_REF_CALL_STACK_USAGE)
1038 return false;
1040 return true;
1043 /* Fill in log links field for all insns. */
1045 static void
1046 create_log_links (void)
1048 basic_block bb;
1049 rtx_insn **next_use;
1050 rtx_insn *insn;
1051 df_ref def, use;
1053 next_use = XCNEWVEC (rtx_insn *, max_reg_num ());
1055 /* Pass through each block from the end, recording the uses of each
1056 register and establishing log links when def is encountered.
1057 Note that we do not clear next_use array in order to save time,
1058 so we have to test whether the use is in the same basic block as def.
1060 There are a few cases below when we do not consider the definition or
1061 usage -- these are taken from original flow.c did. Don't ask me why it is
1062 done this way; I don't know and if it works, I don't want to know. */
1064 FOR_EACH_BB_FN (bb, cfun)
1066 FOR_BB_INSNS_REVERSE (bb, insn)
1068 if (!NONDEBUG_INSN_P (insn))
1069 continue;
1071 /* Log links are created only once. */
1072 gcc_assert (!LOG_LINKS (insn));
1074 FOR_EACH_INSN_DEF (def, insn)
1076 unsigned int regno = DF_REF_REGNO (def);
1077 rtx_insn *use_insn;
1079 if (!next_use[regno])
1080 continue;
1082 if (!can_combine_def_p (def))
1083 continue;
1085 use_insn = next_use[regno];
1086 next_use[regno] = NULL;
1088 if (BLOCK_FOR_INSN (use_insn) != bb)
1089 continue;
1091 /* flow.c claimed:
1093 We don't build a LOG_LINK for hard registers contained
1094 in ASM_OPERANDs. If these registers get replaced,
1095 we might wind up changing the semantics of the insn,
1096 even if reload can make what appear to be valid
1097 assignments later. */
1098 if (regno < FIRST_PSEUDO_REGISTER
1099 && asm_noperands (PATTERN (use_insn)) >= 0)
1100 continue;
1102 /* Don't add duplicate links between instructions. */
1103 struct insn_link *links;
1104 FOR_EACH_LOG_LINK (links, use_insn)
1105 if (insn == links->insn && regno == links->regno)
1106 break;
1108 if (!links)
1109 LOG_LINKS (use_insn)
1110 = alloc_insn_link (insn, regno, LOG_LINKS (use_insn));
1113 FOR_EACH_INSN_USE (use, insn)
1114 if (can_combine_use_p (use))
1115 next_use[DF_REF_REGNO (use)] = insn;
1119 free (next_use);
1122 /* Walk the LOG_LINKS of insn B to see if we find a reference to A. Return
1123 true if we found a LOG_LINK that proves that A feeds B. This only works
1124 if there are no instructions between A and B which could have a link
1125 depending on A, since in that case we would not record a link for B.
1126 We also check the implicit dependency created by a cc0 setter/user
1127 pair. */
1129 static bool
1130 insn_a_feeds_b (rtx_insn *a, rtx_insn *b)
1132 struct insn_link *links;
1133 FOR_EACH_LOG_LINK (links, b)
1134 if (links->insn == a)
1135 return true;
1136 if (HAVE_cc0 && sets_cc0_p (a))
1137 return true;
1138 return false;
1141 /* Main entry point for combiner. F is the first insn of the function.
1142 NREGS is the first unused pseudo-reg number.
1144 Return nonzero if the combiner has turned an indirect jump
1145 instruction into a direct jump. */
1146 static int
1147 combine_instructions (rtx_insn *f, unsigned int nregs)
1149 rtx_insn *insn, *next;
1150 rtx_insn *prev;
1151 struct insn_link *links, *nextlinks;
1152 rtx_insn *first;
1153 basic_block last_bb;
1155 int new_direct_jump_p = 0;
1157 for (first = f; first && !NONDEBUG_INSN_P (first); )
1158 first = NEXT_INSN (first);
1159 if (!first)
1160 return 0;
1162 combine_attempts = 0;
1163 combine_merges = 0;
1164 combine_extras = 0;
1165 combine_successes = 0;
1167 rtl_hooks = combine_rtl_hooks;
1169 reg_stat.safe_grow_cleared (nregs);
1171 init_recog_no_volatile ();
1173 /* Allocate array for insn info. */
1174 max_uid_known = get_max_uid ();
1175 uid_log_links = XCNEWVEC (struct insn_link *, max_uid_known + 1);
1176 uid_insn_cost = XCNEWVEC (int, max_uid_known + 1);
1177 gcc_obstack_init (&insn_link_obstack);
1179 nonzero_bits_mode = int_mode_for_size (HOST_BITS_PER_WIDE_INT, 0).require ();
1181 /* Don't use reg_stat[].nonzero_bits when computing it. This can cause
1182 problems when, for example, we have j <<= 1 in a loop. */
1184 nonzero_sign_valid = 0;
1185 label_tick = label_tick_ebb_start = 1;
1187 /* Scan all SETs and see if we can deduce anything about what
1188 bits are known to be zero for some registers and how many copies
1189 of the sign bit are known to exist for those registers.
1191 Also set any known values so that we can use it while searching
1192 for what bits are known to be set. */
1194 setup_incoming_promotions (first);
1195 /* Allow the entry block and the first block to fall into the same EBB.
1196 Conceptually the incoming promotions are assigned to the entry block. */
1197 last_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1199 create_log_links ();
1200 FOR_EACH_BB_FN (this_basic_block, cfun)
1202 optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1203 last_call_luid = 0;
1204 mem_last_set = -1;
1206 label_tick++;
1207 if (!single_pred_p (this_basic_block)
1208 || single_pred (this_basic_block) != last_bb)
1209 label_tick_ebb_start = label_tick;
1210 last_bb = this_basic_block;
1212 FOR_BB_INSNS (this_basic_block, insn)
1213 if (INSN_P (insn) && BLOCK_FOR_INSN (insn))
1215 rtx links;
1217 subst_low_luid = DF_INSN_LUID (insn);
1218 subst_insn = insn;
1220 note_stores (PATTERN (insn), set_nonzero_bits_and_sign_copies,
1221 insn);
1222 record_dead_and_set_regs (insn);
1224 if (AUTO_INC_DEC)
1225 for (links = REG_NOTES (insn); links; links = XEXP (links, 1))
1226 if (REG_NOTE_KIND (links) == REG_INC)
1227 set_nonzero_bits_and_sign_copies (XEXP (links, 0), NULL_RTX,
1228 insn);
1230 /* Record the current insn_cost of this instruction. */
1231 if (NONJUMP_INSN_P (insn))
1232 INSN_COST (insn) = insn_cost (insn, optimize_this_for_speed_p);
1233 if (dump_file)
1235 fprintf (dump_file, "insn_cost %d for ", INSN_COST (insn));
1236 dump_insn_slim (dump_file, insn);
1241 nonzero_sign_valid = 1;
1243 /* Now scan all the insns in forward order. */
1244 label_tick = label_tick_ebb_start = 1;
1245 init_reg_last ();
1246 setup_incoming_promotions (first);
1247 last_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
1248 int max_combine = PARAM_VALUE (PARAM_MAX_COMBINE_INSNS);
1250 FOR_EACH_BB_FN (this_basic_block, cfun)
1252 rtx_insn *last_combined_insn = NULL;
1254 /* Ignore instruction combination in basic blocks that are going to
1255 be removed as unreachable anyway. See PR82386. */
1256 if (EDGE_COUNT (this_basic_block->preds) == 0)
1257 continue;
1259 optimize_this_for_speed_p = optimize_bb_for_speed_p (this_basic_block);
1260 last_call_luid = 0;
1261 mem_last_set = -1;
1263 label_tick++;
1264 if (!single_pred_p (this_basic_block)
1265 || single_pred (this_basic_block) != last_bb)
1266 label_tick_ebb_start = label_tick;
1267 last_bb = this_basic_block;
1269 rtl_profile_for_bb (this_basic_block);
1270 for (insn = BB_HEAD (this_basic_block);
1271 insn != NEXT_INSN (BB_END (this_basic_block));
1272 insn = next ? next : NEXT_INSN (insn))
1274 next = 0;
1275 if (!NONDEBUG_INSN_P (insn))
1276 continue;
1278 while (last_combined_insn
1279 && (!NONDEBUG_INSN_P (last_combined_insn)
1280 || last_combined_insn->deleted ()))
1281 last_combined_insn = PREV_INSN (last_combined_insn);
1282 if (last_combined_insn == NULL_RTX
1283 || BLOCK_FOR_INSN (last_combined_insn) != this_basic_block
1284 || DF_INSN_LUID (last_combined_insn) <= DF_INSN_LUID (insn))
1285 last_combined_insn = insn;
1287 /* See if we know about function return values before this
1288 insn based upon SUBREG flags. */
1289 check_promoted_subreg (insn, PATTERN (insn));
1291 /* See if we can find hardregs and subreg of pseudos in
1292 narrower modes. This could help turning TRUNCATEs
1293 into SUBREGs. */
1294 note_uses (&PATTERN (insn), record_truncated_values, NULL);
1296 /* Try this insn with each insn it links back to. */
1298 FOR_EACH_LOG_LINK (links, insn)
1299 if ((next = try_combine (insn, links->insn, NULL,
1300 NULL, &new_direct_jump_p,
1301 last_combined_insn)) != 0)
1303 statistics_counter_event (cfun, "two-insn combine", 1);
1304 goto retry;
1307 /* Try each sequence of three linked insns ending with this one. */
1309 if (max_combine >= 3)
1310 FOR_EACH_LOG_LINK (links, insn)
1312 rtx_insn *link = links->insn;
1314 /* If the linked insn has been replaced by a note, then there
1315 is no point in pursuing this chain any further. */
1316 if (NOTE_P (link))
1317 continue;
1319 FOR_EACH_LOG_LINK (nextlinks, link)
1320 if ((next = try_combine (insn, link, nextlinks->insn,
1321 NULL, &new_direct_jump_p,
1322 last_combined_insn)) != 0)
1324 statistics_counter_event (cfun, "three-insn combine", 1);
1325 goto retry;
1329 /* Try to combine a jump insn that uses CC0
1330 with a preceding insn that sets CC0, and maybe with its
1331 logical predecessor as well.
1332 This is how we make decrement-and-branch insns.
1333 We need this special code because data flow connections
1334 via CC0 do not get entered in LOG_LINKS. */
1336 if (HAVE_cc0
1337 && JUMP_P (insn)
1338 && (prev = prev_nonnote_insn (insn)) != 0
1339 && NONJUMP_INSN_P (prev)
1340 && sets_cc0_p (PATTERN (prev)))
1342 if ((next = try_combine (insn, prev, NULL, NULL,
1343 &new_direct_jump_p,
1344 last_combined_insn)) != 0)
1345 goto retry;
1347 FOR_EACH_LOG_LINK (nextlinks, prev)
1348 if ((next = try_combine (insn, prev, nextlinks->insn,
1349 NULL, &new_direct_jump_p,
1350 last_combined_insn)) != 0)
1351 goto retry;
1354 /* Do the same for an insn that explicitly references CC0. */
1355 if (HAVE_cc0 && NONJUMP_INSN_P (insn)
1356 && (prev = prev_nonnote_insn (insn)) != 0
1357 && NONJUMP_INSN_P (prev)
1358 && sets_cc0_p (PATTERN (prev))
1359 && GET_CODE (PATTERN (insn)) == SET
1360 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (insn))))
1362 if ((next = try_combine (insn, prev, NULL, NULL,
1363 &new_direct_jump_p,
1364 last_combined_insn)) != 0)
1365 goto retry;
1367 FOR_EACH_LOG_LINK (nextlinks, prev)
1368 if ((next = try_combine (insn, prev, nextlinks->insn,
1369 NULL, &new_direct_jump_p,
1370 last_combined_insn)) != 0)
1371 goto retry;
1374 /* Finally, see if any of the insns that this insn links to
1375 explicitly references CC0. If so, try this insn, that insn,
1376 and its predecessor if it sets CC0. */
1377 if (HAVE_cc0)
1379 FOR_EACH_LOG_LINK (links, insn)
1380 if (NONJUMP_INSN_P (links->insn)
1381 && GET_CODE (PATTERN (links->insn)) == SET
1382 && reg_mentioned_p (cc0_rtx, SET_SRC (PATTERN (links->insn)))
1383 && (prev = prev_nonnote_insn (links->insn)) != 0
1384 && NONJUMP_INSN_P (prev)
1385 && sets_cc0_p (PATTERN (prev))
1386 && (next = try_combine (insn, links->insn,
1387 prev, NULL, &new_direct_jump_p,
1388 last_combined_insn)) != 0)
1389 goto retry;
1392 /* Try combining an insn with two different insns whose results it
1393 uses. */
1394 if (max_combine >= 3)
1395 FOR_EACH_LOG_LINK (links, insn)
1396 for (nextlinks = links->next; nextlinks;
1397 nextlinks = nextlinks->next)
1398 if ((next = try_combine (insn, links->insn,
1399 nextlinks->insn, NULL,
1400 &new_direct_jump_p,
1401 last_combined_insn)) != 0)
1404 statistics_counter_event (cfun, "three-insn combine", 1);
1405 goto retry;
1408 /* Try four-instruction combinations. */
1409 if (max_combine >= 4)
1410 FOR_EACH_LOG_LINK (links, insn)
1412 struct insn_link *next1;
1413 rtx_insn *link = links->insn;
1415 /* If the linked insn has been replaced by a note, then there
1416 is no point in pursuing this chain any further. */
1417 if (NOTE_P (link))
1418 continue;
1420 FOR_EACH_LOG_LINK (next1, link)
1422 rtx_insn *link1 = next1->insn;
1423 if (NOTE_P (link1))
1424 continue;
1425 /* I0 -> I1 -> I2 -> I3. */
1426 FOR_EACH_LOG_LINK (nextlinks, link1)
1427 if ((next = try_combine (insn, link, link1,
1428 nextlinks->insn,
1429 &new_direct_jump_p,
1430 last_combined_insn)) != 0)
1432 statistics_counter_event (cfun, "four-insn combine", 1);
1433 goto retry;
1435 /* I0, I1 -> I2, I2 -> I3. */
1436 for (nextlinks = next1->next; nextlinks;
1437 nextlinks = nextlinks->next)
1438 if ((next = try_combine (insn, link, link1,
1439 nextlinks->insn,
1440 &new_direct_jump_p,
1441 last_combined_insn)) != 0)
1443 statistics_counter_event (cfun, "four-insn combine", 1);
1444 goto retry;
1448 for (next1 = links->next; next1; next1 = next1->next)
1450 rtx_insn *link1 = next1->insn;
1451 if (NOTE_P (link1))
1452 continue;
1453 /* I0 -> I2; I1, I2 -> I3. */
1454 FOR_EACH_LOG_LINK (nextlinks, link)
1455 if ((next = try_combine (insn, link, link1,
1456 nextlinks->insn,
1457 &new_direct_jump_p,
1458 last_combined_insn)) != 0)
1460 statistics_counter_event (cfun, "four-insn combine", 1);
1461 goto retry;
1463 /* I0 -> I1; I1, I2 -> I3. */
1464 FOR_EACH_LOG_LINK (nextlinks, link1)
1465 if ((next = try_combine (insn, link, link1,
1466 nextlinks->insn,
1467 &new_direct_jump_p,
1468 last_combined_insn)) != 0)
1470 statistics_counter_event (cfun, "four-insn combine", 1);
1471 goto retry;
1476 /* Try this insn with each REG_EQUAL note it links back to. */
1477 FOR_EACH_LOG_LINK (links, insn)
1479 rtx set, note;
1480 rtx_insn *temp = links->insn;
1481 if ((set = single_set (temp)) != 0
1482 && (note = find_reg_equal_equiv_note (temp)) != 0
1483 && (note = XEXP (note, 0), GET_CODE (note)) != EXPR_LIST
1484 /* Avoid using a register that may already been marked
1485 dead by an earlier instruction. */
1486 && ! unmentioned_reg_p (note, SET_SRC (set))
1487 && (GET_MODE (note) == VOIDmode
1488 ? SCALAR_INT_MODE_P (GET_MODE (SET_DEST (set)))
1489 : (GET_MODE (SET_DEST (set)) == GET_MODE (note)
1490 && (GET_CODE (SET_DEST (set)) != ZERO_EXTRACT
1491 || (GET_MODE (XEXP (SET_DEST (set), 0))
1492 == GET_MODE (note))))))
1494 /* Temporarily replace the set's source with the
1495 contents of the REG_EQUAL note. The insn will
1496 be deleted or recognized by try_combine. */
1497 rtx orig_src = SET_SRC (set);
1498 rtx orig_dest = SET_DEST (set);
1499 if (GET_CODE (SET_DEST (set)) == ZERO_EXTRACT)
1500 SET_DEST (set) = XEXP (SET_DEST (set), 0);
1501 SET_SRC (set) = note;
1502 i2mod = temp;
1503 i2mod_old_rhs = copy_rtx (orig_src);
1504 i2mod_new_rhs = copy_rtx (note);
1505 next = try_combine (insn, i2mod, NULL, NULL,
1506 &new_direct_jump_p,
1507 last_combined_insn);
1508 i2mod = NULL;
1509 if (next)
1511 statistics_counter_event (cfun, "insn-with-note combine", 1);
1512 goto retry;
1514 SET_SRC (set) = orig_src;
1515 SET_DEST (set) = orig_dest;
1519 if (!NOTE_P (insn))
1520 record_dead_and_set_regs (insn);
1522 retry:
1527 default_rtl_profile ();
1528 clear_bb_flags ();
1529 new_direct_jump_p |= purge_all_dead_edges ();
1530 delete_noop_moves ();
1532 /* Clean up. */
1533 obstack_free (&insn_link_obstack, NULL);
1534 free (uid_log_links);
1535 free (uid_insn_cost);
1536 reg_stat.release ();
1539 struct undo *undo, *next;
1540 for (undo = undobuf.frees; undo; undo = next)
1542 next = undo->next;
1543 free (undo);
1545 undobuf.frees = 0;
1548 total_attempts += combine_attempts;
1549 total_merges += combine_merges;
1550 total_extras += combine_extras;
1551 total_successes += combine_successes;
1553 nonzero_sign_valid = 0;
1554 rtl_hooks = general_rtl_hooks;
1556 /* Make recognizer allow volatile MEMs again. */
1557 init_recog ();
1559 return new_direct_jump_p;
1562 /* Wipe the last_xxx fields of reg_stat in preparation for another pass. */
1564 static void
1565 init_reg_last (void)
1567 unsigned int i;
1568 reg_stat_type *p;
1570 FOR_EACH_VEC_ELT (reg_stat, i, p)
1571 memset (p, 0, offsetof (reg_stat_type, sign_bit_copies));
1574 /* Set up any promoted values for incoming argument registers. */
1576 static void
1577 setup_incoming_promotions (rtx_insn *first)
1579 tree arg;
1580 bool strictly_local = false;
1582 for (arg = DECL_ARGUMENTS (current_function_decl); arg;
1583 arg = DECL_CHAIN (arg))
1585 rtx x, reg = DECL_INCOMING_RTL (arg);
1586 int uns1, uns3;
1587 machine_mode mode1, mode2, mode3, mode4;
1589 /* Only continue if the incoming argument is in a register. */
1590 if (!REG_P (reg))
1591 continue;
1593 /* Determine, if possible, whether all call sites of the current
1594 function lie within the current compilation unit. (This does
1595 take into account the exporting of a function via taking its
1596 address, and so forth.) */
1597 strictly_local = cgraph_node::local_info (current_function_decl)->local;
1599 /* The mode and signedness of the argument before any promotions happen
1600 (equal to the mode of the pseudo holding it at that stage). */
1601 mode1 = TYPE_MODE (TREE_TYPE (arg));
1602 uns1 = TYPE_UNSIGNED (TREE_TYPE (arg));
1604 /* The mode and signedness of the argument after any source language and
1605 TARGET_PROMOTE_PROTOTYPES-driven promotions. */
1606 mode2 = TYPE_MODE (DECL_ARG_TYPE (arg));
1607 uns3 = TYPE_UNSIGNED (DECL_ARG_TYPE (arg));
1609 /* The mode and signedness of the argument as it is actually passed,
1610 see assign_parm_setup_reg in function.c. */
1611 mode3 = promote_function_mode (TREE_TYPE (arg), mode1, &uns3,
1612 TREE_TYPE (cfun->decl), 0);
1614 /* The mode of the register in which the argument is being passed. */
1615 mode4 = GET_MODE (reg);
1617 /* Eliminate sign extensions in the callee when:
1618 (a) A mode promotion has occurred; */
1619 if (mode1 == mode3)
1620 continue;
1621 /* (b) The mode of the register is the same as the mode of
1622 the argument as it is passed; */
1623 if (mode3 != mode4)
1624 continue;
1625 /* (c) There's no language level extension; */
1626 if (mode1 == mode2)
1628 /* (c.1) All callers are from the current compilation unit. If that's
1629 the case we don't have to rely on an ABI, we only have to know
1630 what we're generating right now, and we know that we will do the
1631 mode1 to mode2 promotion with the given sign. */
1632 else if (!strictly_local)
1633 continue;
1634 /* (c.2) The combination of the two promotions is useful. This is
1635 true when the signs match, or if the first promotion is unsigned.
1636 In the later case, (sign_extend (zero_extend x)) is the same as
1637 (zero_extend (zero_extend x)), so make sure to force UNS3 true. */
1638 else if (uns1)
1639 uns3 = true;
1640 else if (uns3)
1641 continue;
1643 /* Record that the value was promoted from mode1 to mode3,
1644 so that any sign extension at the head of the current
1645 function may be eliminated. */
1646 x = gen_rtx_CLOBBER (mode1, const0_rtx);
1647 x = gen_rtx_fmt_e ((uns3 ? ZERO_EXTEND : SIGN_EXTEND), mode3, x);
1648 record_value_for_reg (reg, first, x);
1652 /* If MODE has a precision lower than PREC and SRC is a non-negative constant
1653 that would appear negative in MODE, sign-extend SRC for use in nonzero_bits
1654 because some machines (maybe most) will actually do the sign-extension and
1655 this is the conservative approach.
1657 ??? For 2.5, try to tighten up the MD files in this regard instead of this
1658 kludge. */
1660 static rtx
1661 sign_extend_short_imm (rtx src, machine_mode mode, unsigned int prec)
1663 scalar_int_mode int_mode;
1664 if (CONST_INT_P (src)
1665 && is_a <scalar_int_mode> (mode, &int_mode)
1666 && GET_MODE_PRECISION (int_mode) < prec
1667 && INTVAL (src) > 0
1668 && val_signbit_known_set_p (int_mode, INTVAL (src)))
1669 src = GEN_INT (INTVAL (src) | ~GET_MODE_MASK (int_mode));
1671 return src;
1674 /* Update RSP for pseudo-register X from INSN's REG_EQUAL note (if one exists)
1675 and SET. */
1677 static void
1678 update_rsp_from_reg_equal (reg_stat_type *rsp, rtx_insn *insn, const_rtx set,
1679 rtx x)
1681 rtx reg_equal_note = insn ? find_reg_equal_equiv_note (insn) : NULL_RTX;
1682 unsigned HOST_WIDE_INT bits = 0;
1683 rtx reg_equal = NULL, src = SET_SRC (set);
1684 unsigned int num = 0;
1686 if (reg_equal_note)
1687 reg_equal = XEXP (reg_equal_note, 0);
1689 if (SHORT_IMMEDIATES_SIGN_EXTEND)
1691 src = sign_extend_short_imm (src, GET_MODE (x), BITS_PER_WORD);
1692 if (reg_equal)
1693 reg_equal = sign_extend_short_imm (reg_equal, GET_MODE (x), BITS_PER_WORD);
1696 /* Don't call nonzero_bits if it cannot change anything. */
1697 if (rsp->nonzero_bits != HOST_WIDE_INT_M1U)
1699 bits = nonzero_bits (src, nonzero_bits_mode);
1700 if (reg_equal && bits)
1701 bits &= nonzero_bits (reg_equal, nonzero_bits_mode);
1702 rsp->nonzero_bits |= bits;
1705 /* Don't call num_sign_bit_copies if it cannot change anything. */
1706 if (rsp->sign_bit_copies != 1)
1708 num = num_sign_bit_copies (SET_SRC (set), GET_MODE (x));
1709 if (reg_equal && maybe_ne (num, GET_MODE_PRECISION (GET_MODE (x))))
1711 unsigned int numeq = num_sign_bit_copies (reg_equal, GET_MODE (x));
1712 if (num == 0 || numeq > num)
1713 num = numeq;
1715 if (rsp->sign_bit_copies == 0 || num < rsp->sign_bit_copies)
1716 rsp->sign_bit_copies = num;
1720 /* Called via note_stores. If X is a pseudo that is narrower than
1721 HOST_BITS_PER_WIDE_INT and is being set, record what bits are known zero.
1723 If we are setting only a portion of X and we can't figure out what
1724 portion, assume all bits will be used since we don't know what will
1725 be happening.
1727 Similarly, set how many bits of X are known to be copies of the sign bit
1728 at all locations in the function. This is the smallest number implied
1729 by any set of X. */
1731 static void
1732 set_nonzero_bits_and_sign_copies (rtx x, const_rtx set, void *data)
1734 rtx_insn *insn = (rtx_insn *) data;
1735 scalar_int_mode mode;
1737 if (REG_P (x)
1738 && REGNO (x) >= FIRST_PSEUDO_REGISTER
1739 /* If this register is undefined at the start of the file, we can't
1740 say what its contents were. */
1741 && ! REGNO_REG_SET_P
1742 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb), REGNO (x))
1743 && is_a <scalar_int_mode> (GET_MODE (x), &mode)
1744 && HWI_COMPUTABLE_MODE_P (mode))
1746 reg_stat_type *rsp = &reg_stat[REGNO (x)];
1748 if (set == 0 || GET_CODE (set) == CLOBBER)
1750 rsp->nonzero_bits = GET_MODE_MASK (mode);
1751 rsp->sign_bit_copies = 1;
1752 return;
1755 /* If this register is being initialized using itself, and the
1756 register is uninitialized in this basic block, and there are
1757 no LOG_LINKS which set the register, then part of the
1758 register is uninitialized. In that case we can't assume
1759 anything about the number of nonzero bits.
1761 ??? We could do better if we checked this in
1762 reg_{nonzero_bits,num_sign_bit_copies}_for_combine. Then we
1763 could avoid making assumptions about the insn which initially
1764 sets the register, while still using the information in other
1765 insns. We would have to be careful to check every insn
1766 involved in the combination. */
1768 if (insn
1769 && reg_referenced_p (x, PATTERN (insn))
1770 && !REGNO_REG_SET_P (DF_LR_IN (BLOCK_FOR_INSN (insn)),
1771 REGNO (x)))
1773 struct insn_link *link;
1775 FOR_EACH_LOG_LINK (link, insn)
1776 if (dead_or_set_p (link->insn, x))
1777 break;
1778 if (!link)
1780 rsp->nonzero_bits = GET_MODE_MASK (mode);
1781 rsp->sign_bit_copies = 1;
1782 return;
1786 /* If this is a complex assignment, see if we can convert it into a
1787 simple assignment. */
1788 set = expand_field_assignment (set);
1790 /* If this is a simple assignment, or we have a paradoxical SUBREG,
1791 set what we know about X. */
1793 if (SET_DEST (set) == x
1794 || (paradoxical_subreg_p (SET_DEST (set))
1795 && SUBREG_REG (SET_DEST (set)) == x))
1796 update_rsp_from_reg_equal (rsp, insn, set, x);
1797 else
1799 rsp->nonzero_bits = GET_MODE_MASK (mode);
1800 rsp->sign_bit_copies = 1;
1805 /* See if INSN can be combined into I3. PRED, PRED2, SUCC and SUCC2 are
1806 optionally insns that were previously combined into I3 or that will be
1807 combined into the merger of INSN and I3. The order is PRED, PRED2,
1808 INSN, SUCC, SUCC2, I3.
1810 Return 0 if the combination is not allowed for any reason.
1812 If the combination is allowed, *PDEST will be set to the single
1813 destination of INSN and *PSRC to the single source, and this function
1814 will return 1. */
1816 static int
1817 can_combine_p (rtx_insn *insn, rtx_insn *i3, rtx_insn *pred ATTRIBUTE_UNUSED,
1818 rtx_insn *pred2 ATTRIBUTE_UNUSED, rtx_insn *succ, rtx_insn *succ2,
1819 rtx *pdest, rtx *psrc)
1821 int i;
1822 const_rtx set = 0;
1823 rtx src, dest;
1824 rtx_insn *p;
1825 rtx link;
1826 bool all_adjacent = true;
1827 int (*is_volatile_p) (const_rtx);
1829 if (succ)
1831 if (succ2)
1833 if (next_active_insn (succ2) != i3)
1834 all_adjacent = false;
1835 if (next_active_insn (succ) != succ2)
1836 all_adjacent = false;
1838 else if (next_active_insn (succ) != i3)
1839 all_adjacent = false;
1840 if (next_active_insn (insn) != succ)
1841 all_adjacent = false;
1843 else if (next_active_insn (insn) != i3)
1844 all_adjacent = false;
1846 /* Can combine only if previous insn is a SET of a REG, a SUBREG or CC0.
1847 or a PARALLEL consisting of such a SET and CLOBBERs.
1849 If INSN has CLOBBER parallel parts, ignore them for our processing.
1850 By definition, these happen during the execution of the insn. When it
1851 is merged with another insn, all bets are off. If they are, in fact,
1852 needed and aren't also supplied in I3, they may be added by
1853 recog_for_combine. Otherwise, it won't match.
1855 We can also ignore a SET whose SET_DEST is mentioned in a REG_UNUSED
1856 note.
1858 Get the source and destination of INSN. If more than one, can't
1859 combine. */
1861 if (GET_CODE (PATTERN (insn)) == SET)
1862 set = PATTERN (insn);
1863 else if (GET_CODE (PATTERN (insn)) == PARALLEL
1864 && GET_CODE (XVECEXP (PATTERN (insn), 0, 0)) == SET)
1866 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
1868 rtx elt = XVECEXP (PATTERN (insn), 0, i);
1870 switch (GET_CODE (elt))
1872 /* This is important to combine floating point insns
1873 for the SH4 port. */
1874 case USE:
1875 /* Combining an isolated USE doesn't make sense.
1876 We depend here on combinable_i3pat to reject them. */
1877 /* The code below this loop only verifies that the inputs of
1878 the SET in INSN do not change. We call reg_set_between_p
1879 to verify that the REG in the USE does not change between
1880 I3 and INSN.
1881 If the USE in INSN was for a pseudo register, the matching
1882 insn pattern will likely match any register; combining this
1883 with any other USE would only be safe if we knew that the
1884 used registers have identical values, or if there was
1885 something to tell them apart, e.g. different modes. For
1886 now, we forgo such complicated tests and simply disallow
1887 combining of USES of pseudo registers with any other USE. */
1888 if (REG_P (XEXP (elt, 0))
1889 && GET_CODE (PATTERN (i3)) == PARALLEL)
1891 rtx i3pat = PATTERN (i3);
1892 int i = XVECLEN (i3pat, 0) - 1;
1893 unsigned int regno = REGNO (XEXP (elt, 0));
1897 rtx i3elt = XVECEXP (i3pat, 0, i);
1899 if (GET_CODE (i3elt) == USE
1900 && REG_P (XEXP (i3elt, 0))
1901 && (REGNO (XEXP (i3elt, 0)) == regno
1902 ? reg_set_between_p (XEXP (elt, 0),
1903 PREV_INSN (insn), i3)
1904 : regno >= FIRST_PSEUDO_REGISTER))
1905 return 0;
1907 while (--i >= 0);
1909 break;
1911 /* We can ignore CLOBBERs. */
1912 case CLOBBER:
1913 break;
1915 case SET:
1916 /* Ignore SETs whose result isn't used but not those that
1917 have side-effects. */
1918 if (find_reg_note (insn, REG_UNUSED, SET_DEST (elt))
1919 && insn_nothrow_p (insn)
1920 && !side_effects_p (elt))
1921 break;
1923 /* If we have already found a SET, this is a second one and
1924 so we cannot combine with this insn. */
1925 if (set)
1926 return 0;
1928 set = elt;
1929 break;
1931 default:
1932 /* Anything else means we can't combine. */
1933 return 0;
1937 if (set == 0
1938 /* If SET_SRC is an ASM_OPERANDS we can't throw away these CLOBBERs,
1939 so don't do anything with it. */
1940 || GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
1941 return 0;
1943 else
1944 return 0;
1946 if (set == 0)
1947 return 0;
1949 /* The simplification in expand_field_assignment may call back to
1950 get_last_value, so set safe guard here. */
1951 subst_low_luid = DF_INSN_LUID (insn);
1953 set = expand_field_assignment (set);
1954 src = SET_SRC (set), dest = SET_DEST (set);
1956 /* Do not eliminate user-specified register if it is in an
1957 asm input because we may break the register asm usage defined
1958 in GCC manual if allow to do so.
1959 Be aware that this may cover more cases than we expect but this
1960 should be harmless. */
1961 if (REG_P (dest) && REG_USERVAR_P (dest) && HARD_REGISTER_P (dest)
1962 && extract_asm_operands (PATTERN (i3)))
1963 return 0;
1965 /* Don't eliminate a store in the stack pointer. */
1966 if (dest == stack_pointer_rtx
1967 /* Don't combine with an insn that sets a register to itself if it has
1968 a REG_EQUAL note. This may be part of a LIBCALL sequence. */
1969 || (rtx_equal_p (src, dest) && find_reg_note (insn, REG_EQUAL, NULL_RTX))
1970 /* Can't merge an ASM_OPERANDS. */
1971 || GET_CODE (src) == ASM_OPERANDS
1972 /* Can't merge a function call. */
1973 || GET_CODE (src) == CALL
1974 /* Don't eliminate a function call argument. */
1975 || (CALL_P (i3)
1976 && (find_reg_fusage (i3, USE, dest)
1977 || (REG_P (dest)
1978 && REGNO (dest) < FIRST_PSEUDO_REGISTER
1979 && global_regs[REGNO (dest)])))
1980 /* Don't substitute into an incremented register. */
1981 || FIND_REG_INC_NOTE (i3, dest)
1982 || (succ && FIND_REG_INC_NOTE (succ, dest))
1983 || (succ2 && FIND_REG_INC_NOTE (succ2, dest))
1984 /* Don't substitute into a non-local goto, this confuses CFG. */
1985 || (JUMP_P (i3) && find_reg_note (i3, REG_NON_LOCAL_GOTO, NULL_RTX))
1986 /* Make sure that DEST is not used after INSN but before SUCC, or
1987 after SUCC and before SUCC2, or after SUCC2 but before I3. */
1988 || (!all_adjacent
1989 && ((succ2
1990 && (reg_used_between_p (dest, succ2, i3)
1991 || reg_used_between_p (dest, succ, succ2)))
1992 || (!succ2 && succ && reg_used_between_p (dest, succ, i3))
1993 || (succ
1994 /* SUCC and SUCC2 can be split halves from a PARALLEL; in
1995 that case SUCC is not in the insn stream, so use SUCC2
1996 instead for this test. */
1997 && reg_used_between_p (dest, insn,
1998 succ2
1999 && INSN_UID (succ) == INSN_UID (succ2)
2000 ? succ2 : succ))))
2001 /* Make sure that the value that is to be substituted for the register
2002 does not use any registers whose values alter in between. However,
2003 If the insns are adjacent, a use can't cross a set even though we
2004 think it might (this can happen for a sequence of insns each setting
2005 the same destination; last_set of that register might point to
2006 a NOTE). If INSN has a REG_EQUIV note, the register is always
2007 equivalent to the memory so the substitution is valid even if there
2008 are intervening stores. Also, don't move a volatile asm or
2009 UNSPEC_VOLATILE across any other insns. */
2010 || (! all_adjacent
2011 && (((!MEM_P (src)
2012 || ! find_reg_note (insn, REG_EQUIV, src))
2013 && modified_between_p (src, insn, i3))
2014 || (GET_CODE (src) == ASM_OPERANDS && MEM_VOLATILE_P (src))
2015 || GET_CODE (src) == UNSPEC_VOLATILE))
2016 /* Don't combine across a CALL_INSN, because that would possibly
2017 change whether the life span of some REGs crosses calls or not,
2018 and it is a pain to update that information.
2019 Exception: if source is a constant, moving it later can't hurt.
2020 Accept that as a special case. */
2021 || (DF_INSN_LUID (insn) < last_call_luid && ! CONSTANT_P (src)))
2022 return 0;
2024 /* DEST must either be a REG or CC0. */
2025 if (REG_P (dest))
2027 /* If register alignment is being enforced for multi-word items in all
2028 cases except for parameters, it is possible to have a register copy
2029 insn referencing a hard register that is not allowed to contain the
2030 mode being copied and which would not be valid as an operand of most
2031 insns. Eliminate this problem by not combining with such an insn.
2033 Also, on some machines we don't want to extend the life of a hard
2034 register. */
2036 if (REG_P (src)
2037 && ((REGNO (dest) < FIRST_PSEUDO_REGISTER
2038 && !targetm.hard_regno_mode_ok (REGNO (dest), GET_MODE (dest)))
2039 /* Don't extend the life of a hard register unless it is
2040 user variable (if we have few registers) or it can't
2041 fit into the desired register (meaning something special
2042 is going on).
2043 Also avoid substituting a return register into I3, because
2044 reload can't handle a conflict with constraints of other
2045 inputs. */
2046 || (REGNO (src) < FIRST_PSEUDO_REGISTER
2047 && !targetm.hard_regno_mode_ok (REGNO (src),
2048 GET_MODE (src)))))
2049 return 0;
2051 else if (GET_CODE (dest) != CC0)
2052 return 0;
2055 if (GET_CODE (PATTERN (i3)) == PARALLEL)
2056 for (i = XVECLEN (PATTERN (i3), 0) - 1; i >= 0; i--)
2057 if (GET_CODE (XVECEXP (PATTERN (i3), 0, i)) == CLOBBER)
2059 rtx reg = XEXP (XVECEXP (PATTERN (i3), 0, i), 0);
2061 /* If the clobber represents an earlyclobber operand, we must not
2062 substitute an expression containing the clobbered register.
2063 As we do not analyze the constraint strings here, we have to
2064 make the conservative assumption. However, if the register is
2065 a fixed hard reg, the clobber cannot represent any operand;
2066 we leave it up to the machine description to either accept or
2067 reject use-and-clobber patterns. */
2068 if (!REG_P (reg)
2069 || REGNO (reg) >= FIRST_PSEUDO_REGISTER
2070 || !fixed_regs[REGNO (reg)])
2071 if (reg_overlap_mentioned_p (reg, src))
2072 return 0;
2075 /* If INSN contains anything volatile, or is an `asm' (whether volatile
2076 or not), reject, unless nothing volatile comes between it and I3 */
2078 if (GET_CODE (src) == ASM_OPERANDS || volatile_refs_p (src))
2080 /* Make sure neither succ nor succ2 contains a volatile reference. */
2081 if (succ2 != 0 && volatile_refs_p (PATTERN (succ2)))
2082 return 0;
2083 if (succ != 0 && volatile_refs_p (PATTERN (succ)))
2084 return 0;
2085 /* We'll check insns between INSN and I3 below. */
2088 /* If INSN is an asm, and DEST is a hard register, reject, since it has
2089 to be an explicit register variable, and was chosen for a reason. */
2091 if (GET_CODE (src) == ASM_OPERANDS
2092 && REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER)
2093 return 0;
2095 /* If INSN contains volatile references (specifically volatile MEMs),
2096 we cannot combine across any other volatile references.
2097 Even if INSN doesn't contain volatile references, any intervening
2098 volatile insn might affect machine state. */
2100 is_volatile_p = volatile_refs_p (PATTERN (insn))
2101 ? volatile_refs_p
2102 : volatile_insn_p;
2104 for (p = NEXT_INSN (insn); p != i3; p = NEXT_INSN (p))
2105 if (INSN_P (p) && p != succ && p != succ2 && is_volatile_p (PATTERN (p)))
2106 return 0;
2108 /* If INSN contains an autoincrement or autodecrement, make sure that
2109 register is not used between there and I3, and not already used in
2110 I3 either. Neither must it be used in PRED or SUCC, if they exist.
2111 Also insist that I3 not be a jump; if it were one
2112 and the incremented register were spilled, we would lose. */
2114 if (AUTO_INC_DEC)
2115 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
2116 if (REG_NOTE_KIND (link) == REG_INC
2117 && (JUMP_P (i3)
2118 || reg_used_between_p (XEXP (link, 0), insn, i3)
2119 || (pred != NULL_RTX
2120 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred)))
2121 || (pred2 != NULL_RTX
2122 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (pred2)))
2123 || (succ != NULL_RTX
2124 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ)))
2125 || (succ2 != NULL_RTX
2126 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (succ2)))
2127 || reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i3))))
2128 return 0;
2130 /* Don't combine an insn that follows a CC0-setting insn.
2131 An insn that uses CC0 must not be separated from the one that sets it.
2132 We do, however, allow I2 to follow a CC0-setting insn if that insn
2133 is passed as I1; in that case it will be deleted also.
2134 We also allow combining in this case if all the insns are adjacent
2135 because that would leave the two CC0 insns adjacent as well.
2136 It would be more logical to test whether CC0 occurs inside I1 or I2,
2137 but that would be much slower, and this ought to be equivalent. */
2139 if (HAVE_cc0)
2141 p = prev_nonnote_insn (insn);
2142 if (p && p != pred && NONJUMP_INSN_P (p) && sets_cc0_p (PATTERN (p))
2143 && ! all_adjacent)
2144 return 0;
2147 /* If we get here, we have passed all the tests and the combination is
2148 to be allowed. */
2150 *pdest = dest;
2151 *psrc = src;
2153 return 1;
2156 /* LOC is the location within I3 that contains its pattern or the component
2157 of a PARALLEL of the pattern. We validate that it is valid for combining.
2159 One problem is if I3 modifies its output, as opposed to replacing it
2160 entirely, we can't allow the output to contain I2DEST, I1DEST or I0DEST as
2161 doing so would produce an insn that is not equivalent to the original insns.
2163 Consider:
2165 (set (reg:DI 101) (reg:DI 100))
2166 (set (subreg:SI (reg:DI 101) 0) <foo>)
2168 This is NOT equivalent to:
2170 (parallel [(set (subreg:SI (reg:DI 100) 0) <foo>)
2171 (set (reg:DI 101) (reg:DI 100))])
2173 Not only does this modify 100 (in which case it might still be valid
2174 if 100 were dead in I2), it sets 101 to the ORIGINAL value of 100.
2176 We can also run into a problem if I2 sets a register that I1
2177 uses and I1 gets directly substituted into I3 (not via I2). In that
2178 case, we would be getting the wrong value of I2DEST into I3, so we
2179 must reject the combination. This case occurs when I2 and I1 both
2180 feed into I3, rather than when I1 feeds into I2, which feeds into I3.
2181 If I1_NOT_IN_SRC is nonzero, it means that finding I1 in the source
2182 of a SET must prevent combination from occurring. The same situation
2183 can occur for I0, in which case I0_NOT_IN_SRC is set.
2185 Before doing the above check, we first try to expand a field assignment
2186 into a set of logical operations.
2188 If PI3_DEST_KILLED is nonzero, it is a pointer to a location in which
2189 we place a register that is both set and used within I3. If more than one
2190 such register is detected, we fail.
2192 Return 1 if the combination is valid, zero otherwise. */
2194 static int
2195 combinable_i3pat (rtx_insn *i3, rtx *loc, rtx i2dest, rtx i1dest, rtx i0dest,
2196 int i1_not_in_src, int i0_not_in_src, rtx *pi3dest_killed)
2198 rtx x = *loc;
2200 if (GET_CODE (x) == SET)
2202 rtx set = x ;
2203 rtx dest = SET_DEST (set);
2204 rtx src = SET_SRC (set);
2205 rtx inner_dest = dest;
2206 rtx subdest;
2208 while (GET_CODE (inner_dest) == STRICT_LOW_PART
2209 || GET_CODE (inner_dest) == SUBREG
2210 || GET_CODE (inner_dest) == ZERO_EXTRACT)
2211 inner_dest = XEXP (inner_dest, 0);
2213 /* Check for the case where I3 modifies its output, as discussed
2214 above. We don't want to prevent pseudos from being combined
2215 into the address of a MEM, so only prevent the combination if
2216 i1 or i2 set the same MEM. */
2217 if ((inner_dest != dest &&
2218 (!MEM_P (inner_dest)
2219 || rtx_equal_p (i2dest, inner_dest)
2220 || (i1dest && rtx_equal_p (i1dest, inner_dest))
2221 || (i0dest && rtx_equal_p (i0dest, inner_dest)))
2222 && (reg_overlap_mentioned_p (i2dest, inner_dest)
2223 || (i1dest && reg_overlap_mentioned_p (i1dest, inner_dest))
2224 || (i0dest && reg_overlap_mentioned_p (i0dest, inner_dest))))
2226 /* This is the same test done in can_combine_p except we can't test
2227 all_adjacent; we don't have to, since this instruction will stay
2228 in place, thus we are not considering increasing the lifetime of
2229 INNER_DEST.
2231 Also, if this insn sets a function argument, combining it with
2232 something that might need a spill could clobber a previous
2233 function argument; the all_adjacent test in can_combine_p also
2234 checks this; here, we do a more specific test for this case. */
2236 || (REG_P (inner_dest)
2237 && REGNO (inner_dest) < FIRST_PSEUDO_REGISTER
2238 && !targetm.hard_regno_mode_ok (REGNO (inner_dest),
2239 GET_MODE (inner_dest)))
2240 || (i1_not_in_src && reg_overlap_mentioned_p (i1dest, src))
2241 || (i0_not_in_src && reg_overlap_mentioned_p (i0dest, src)))
2242 return 0;
2244 /* If DEST is used in I3, it is being killed in this insn, so
2245 record that for later. We have to consider paradoxical
2246 subregs here, since they kill the whole register, but we
2247 ignore partial subregs, STRICT_LOW_PART, etc.
2248 Never add REG_DEAD notes for the FRAME_POINTER_REGNUM or the
2249 STACK_POINTER_REGNUM, since these are always considered to be
2250 live. Similarly for ARG_POINTER_REGNUM if it is fixed. */
2251 subdest = dest;
2252 if (GET_CODE (subdest) == SUBREG && !partial_subreg_p (subdest))
2253 subdest = SUBREG_REG (subdest);
2254 if (pi3dest_killed
2255 && REG_P (subdest)
2256 && reg_referenced_p (subdest, PATTERN (i3))
2257 && REGNO (subdest) != FRAME_POINTER_REGNUM
2258 && (HARD_FRAME_POINTER_IS_FRAME_POINTER
2259 || REGNO (subdest) != HARD_FRAME_POINTER_REGNUM)
2260 && (FRAME_POINTER_REGNUM == ARG_POINTER_REGNUM
2261 || (REGNO (subdest) != ARG_POINTER_REGNUM
2262 || ! fixed_regs [REGNO (subdest)]))
2263 && REGNO (subdest) != STACK_POINTER_REGNUM)
2265 if (*pi3dest_killed)
2266 return 0;
2268 *pi3dest_killed = subdest;
2272 else if (GET_CODE (x) == PARALLEL)
2274 int i;
2276 for (i = 0; i < XVECLEN (x, 0); i++)
2277 if (! combinable_i3pat (i3, &XVECEXP (x, 0, i), i2dest, i1dest, i0dest,
2278 i1_not_in_src, i0_not_in_src, pi3dest_killed))
2279 return 0;
2282 return 1;
2285 /* Return 1 if X is an arithmetic expression that contains a multiplication
2286 and division. We don't count multiplications by powers of two here. */
2288 static int
2289 contains_muldiv (rtx x)
2291 switch (GET_CODE (x))
2293 case MOD: case DIV: case UMOD: case UDIV:
2294 return 1;
2296 case MULT:
2297 return ! (CONST_INT_P (XEXP (x, 1))
2298 && pow2p_hwi (UINTVAL (XEXP (x, 1))));
2299 default:
2300 if (BINARY_P (x))
2301 return contains_muldiv (XEXP (x, 0))
2302 || contains_muldiv (XEXP (x, 1));
2304 if (UNARY_P (x))
2305 return contains_muldiv (XEXP (x, 0));
2307 return 0;
2311 /* Determine whether INSN can be used in a combination. Return nonzero if
2312 not. This is used in try_combine to detect early some cases where we
2313 can't perform combinations. */
2315 static int
2316 cant_combine_insn_p (rtx_insn *insn)
2318 rtx set;
2319 rtx src, dest;
2321 /* If this isn't really an insn, we can't do anything.
2322 This can occur when flow deletes an insn that it has merged into an
2323 auto-increment address. */
2324 if (!NONDEBUG_INSN_P (insn))
2325 return 1;
2327 /* Never combine loads and stores involving hard regs that are likely
2328 to be spilled. The register allocator can usually handle such
2329 reg-reg moves by tying. If we allow the combiner to make
2330 substitutions of likely-spilled regs, reload might die.
2331 As an exception, we allow combinations involving fixed regs; these are
2332 not available to the register allocator so there's no risk involved. */
2334 set = single_set (insn);
2335 if (! set)
2336 return 0;
2337 src = SET_SRC (set);
2338 dest = SET_DEST (set);
2339 if (GET_CODE (src) == SUBREG)
2340 src = SUBREG_REG (src);
2341 if (GET_CODE (dest) == SUBREG)
2342 dest = SUBREG_REG (dest);
2343 if (REG_P (src) && REG_P (dest)
2344 && ((HARD_REGISTER_P (src)
2345 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (src))
2346 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (src))))
2347 || (HARD_REGISTER_P (dest)
2348 && ! TEST_HARD_REG_BIT (fixed_reg_set, REGNO (dest))
2349 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (REGNO (dest))))))
2350 return 1;
2352 return 0;
2355 struct likely_spilled_retval_info
2357 unsigned regno, nregs;
2358 unsigned mask;
2361 /* Called via note_stores by likely_spilled_retval_p. Remove from info->mask
2362 hard registers that are known to be written to / clobbered in full. */
2363 static void
2364 likely_spilled_retval_1 (rtx x, const_rtx set, void *data)
2366 struct likely_spilled_retval_info *const info =
2367 (struct likely_spilled_retval_info *) data;
2368 unsigned regno, nregs;
2369 unsigned new_mask;
2371 if (!REG_P (XEXP (set, 0)))
2372 return;
2373 regno = REGNO (x);
2374 if (regno >= info->regno + info->nregs)
2375 return;
2376 nregs = REG_NREGS (x);
2377 if (regno + nregs <= info->regno)
2378 return;
2379 new_mask = (2U << (nregs - 1)) - 1;
2380 if (regno < info->regno)
2381 new_mask >>= info->regno - regno;
2382 else
2383 new_mask <<= regno - info->regno;
2384 info->mask &= ~new_mask;
2387 /* Return nonzero iff part of the return value is live during INSN, and
2388 it is likely spilled. This can happen when more than one insn is needed
2389 to copy the return value, e.g. when we consider to combine into the
2390 second copy insn for a complex value. */
2392 static int
2393 likely_spilled_retval_p (rtx_insn *insn)
2395 rtx_insn *use = BB_END (this_basic_block);
2396 rtx reg;
2397 rtx_insn *p;
2398 unsigned regno, nregs;
2399 /* We assume here that no machine mode needs more than
2400 32 hard registers when the value overlaps with a register
2401 for which TARGET_FUNCTION_VALUE_REGNO_P is true. */
2402 unsigned mask;
2403 struct likely_spilled_retval_info info;
2405 if (!NONJUMP_INSN_P (use) || GET_CODE (PATTERN (use)) != USE || insn == use)
2406 return 0;
2407 reg = XEXP (PATTERN (use), 0);
2408 if (!REG_P (reg) || !targetm.calls.function_value_regno_p (REGNO (reg)))
2409 return 0;
2410 regno = REGNO (reg);
2411 nregs = REG_NREGS (reg);
2412 if (nregs == 1)
2413 return 0;
2414 mask = (2U << (nregs - 1)) - 1;
2416 /* Disregard parts of the return value that are set later. */
2417 info.regno = regno;
2418 info.nregs = nregs;
2419 info.mask = mask;
2420 for (p = PREV_INSN (use); info.mask && p != insn; p = PREV_INSN (p))
2421 if (INSN_P (p))
2422 note_stores (PATTERN (p), likely_spilled_retval_1, &info);
2423 mask = info.mask;
2425 /* Check if any of the (probably) live return value registers is
2426 likely spilled. */
2427 nregs --;
2430 if ((mask & 1 << nregs)
2431 && targetm.class_likely_spilled_p (REGNO_REG_CLASS (regno + nregs)))
2432 return 1;
2433 } while (nregs--);
2434 return 0;
2437 /* Adjust INSN after we made a change to its destination.
2439 Changing the destination can invalidate notes that say something about
2440 the results of the insn and a LOG_LINK pointing to the insn. */
2442 static void
2443 adjust_for_new_dest (rtx_insn *insn)
2445 /* For notes, be conservative and simply remove them. */
2446 remove_reg_equal_equiv_notes (insn);
2448 /* The new insn will have a destination that was previously the destination
2449 of an insn just above it. Call distribute_links to make a LOG_LINK from
2450 the next use of that destination. */
2452 rtx set = single_set (insn);
2453 gcc_assert (set);
2455 rtx reg = SET_DEST (set);
2457 while (GET_CODE (reg) == ZERO_EXTRACT
2458 || GET_CODE (reg) == STRICT_LOW_PART
2459 || GET_CODE (reg) == SUBREG)
2460 reg = XEXP (reg, 0);
2461 gcc_assert (REG_P (reg));
2463 distribute_links (alloc_insn_link (insn, REGNO (reg), NULL));
2465 df_insn_rescan (insn);
2468 /* Return TRUE if combine can reuse reg X in mode MODE.
2469 ADDED_SETS is nonzero if the original set is still required. */
2470 static bool
2471 can_change_dest_mode (rtx x, int added_sets, machine_mode mode)
2473 unsigned int regno;
2475 if (!REG_P (x))
2476 return false;
2478 /* Don't change between modes with different underlying register sizes,
2479 since this could lead to invalid subregs. */
2480 if (maybe_ne (REGMODE_NATURAL_SIZE (mode),
2481 REGMODE_NATURAL_SIZE (GET_MODE (x))))
2482 return false;
2484 regno = REGNO (x);
2485 /* Allow hard registers if the new mode is legal, and occupies no more
2486 registers than the old mode. */
2487 if (regno < FIRST_PSEUDO_REGISTER)
2488 return (targetm.hard_regno_mode_ok (regno, mode)
2489 && REG_NREGS (x) >= hard_regno_nregs (regno, mode));
2491 /* Or a pseudo that is only used once. */
2492 return (regno < reg_n_sets_max
2493 && REG_N_SETS (regno) == 1
2494 && !added_sets
2495 && !REG_USERVAR_P (x));
2499 /* Check whether X, the destination of a set, refers to part of
2500 the register specified by REG. */
2502 static bool
2503 reg_subword_p (rtx x, rtx reg)
2505 /* Check that reg is an integer mode register. */
2506 if (!REG_P (reg) || GET_MODE_CLASS (GET_MODE (reg)) != MODE_INT)
2507 return false;
2509 if (GET_CODE (x) == STRICT_LOW_PART
2510 || GET_CODE (x) == ZERO_EXTRACT)
2511 x = XEXP (x, 0);
2513 return GET_CODE (x) == SUBREG
2514 && SUBREG_REG (x) == reg
2515 && GET_MODE_CLASS (GET_MODE (x)) == MODE_INT;
2518 /* Delete the unconditional jump INSN and adjust the CFG correspondingly.
2519 Note that the INSN should be deleted *after* removing dead edges, so
2520 that the kept edge is the fallthrough edge for a (set (pc) (pc))
2521 but not for a (set (pc) (label_ref FOO)). */
2523 static void
2524 update_cfg_for_uncondjump (rtx_insn *insn)
2526 basic_block bb = BLOCK_FOR_INSN (insn);
2527 gcc_assert (BB_END (bb) == insn);
2529 purge_dead_edges (bb);
2531 delete_insn (insn);
2532 if (EDGE_COUNT (bb->succs) == 1)
2534 rtx_insn *insn;
2536 single_succ_edge (bb)->flags |= EDGE_FALLTHRU;
2538 /* Remove barriers from the footer if there are any. */
2539 for (insn = BB_FOOTER (bb); insn; insn = NEXT_INSN (insn))
2540 if (BARRIER_P (insn))
2542 if (PREV_INSN (insn))
2543 SET_NEXT_INSN (PREV_INSN (insn)) = NEXT_INSN (insn);
2544 else
2545 BB_FOOTER (bb) = NEXT_INSN (insn);
2546 if (NEXT_INSN (insn))
2547 SET_PREV_INSN (NEXT_INSN (insn)) = PREV_INSN (insn);
2549 else if (LABEL_P (insn))
2550 break;
2554 /* Return whether PAT is a PARALLEL of exactly N register SETs followed
2555 by an arbitrary number of CLOBBERs. */
2556 static bool
2557 is_parallel_of_n_reg_sets (rtx pat, int n)
2559 if (GET_CODE (pat) != PARALLEL)
2560 return false;
2562 int len = XVECLEN (pat, 0);
2563 if (len < n)
2564 return false;
2566 int i;
2567 for (i = 0; i < n; i++)
2568 if (GET_CODE (XVECEXP (pat, 0, i)) != SET
2569 || !REG_P (SET_DEST (XVECEXP (pat, 0, i))))
2570 return false;
2571 for ( ; i < len; i++)
2572 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER
2573 || XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
2574 return false;
2576 return true;
2579 /* Return whether INSN, a PARALLEL of N register SETs (and maybe some
2580 CLOBBERs), can be split into individual SETs in that order, without
2581 changing semantics. */
2582 static bool
2583 can_split_parallel_of_n_reg_sets (rtx_insn *insn, int n)
2585 if (!insn_nothrow_p (insn))
2586 return false;
2588 rtx pat = PATTERN (insn);
2590 int i, j;
2591 for (i = 0; i < n; i++)
2593 if (side_effects_p (SET_SRC (XVECEXP (pat, 0, i))))
2594 return false;
2596 rtx reg = SET_DEST (XVECEXP (pat, 0, i));
2598 for (j = i + 1; j < n; j++)
2599 if (reg_referenced_p (reg, XVECEXP (pat, 0, j)))
2600 return false;
2603 return true;
2606 /* Try to combine the insns I0, I1 and I2 into I3.
2607 Here I0, I1 and I2 appear earlier than I3.
2608 I0 and I1 can be zero; then we combine just I2 into I3, or I1 and I2 into
2611 If we are combining more than two insns and the resulting insn is not
2612 recognized, try splitting it into two insns. If that happens, I2 and I3
2613 are retained and I1/I0 are pseudo-deleted by turning them into a NOTE.
2614 Otherwise, I0, I1 and I2 are pseudo-deleted.
2616 Return 0 if the combination does not work. Then nothing is changed.
2617 If we did the combination, return the insn at which combine should
2618 resume scanning.
2620 Set NEW_DIRECT_JUMP_P to a nonzero value if try_combine creates a
2621 new direct jump instruction.
2623 LAST_COMBINED_INSN is either I3, or some insn after I3 that has
2624 been I3 passed to an earlier try_combine within the same basic
2625 block. */
2627 static rtx_insn *
2628 try_combine (rtx_insn *i3, rtx_insn *i2, rtx_insn *i1, rtx_insn *i0,
2629 int *new_direct_jump_p, rtx_insn *last_combined_insn)
2631 /* New patterns for I3 and I2, respectively. */
2632 rtx newpat, newi2pat = 0;
2633 rtvec newpat_vec_with_clobbers = 0;
2634 int substed_i2 = 0, substed_i1 = 0, substed_i0 = 0;
2635 /* Indicates need to preserve SET in I0, I1 or I2 in I3 if it is not
2636 dead. */
2637 int added_sets_0, added_sets_1, added_sets_2;
2638 /* Total number of SETs to put into I3. */
2639 int total_sets;
2640 /* Nonzero if I2's or I1's body now appears in I3. */
2641 int i2_is_used = 0, i1_is_used = 0;
2642 /* INSN_CODEs for new I3, new I2, and user of condition code. */
2643 int insn_code_number, i2_code_number = 0, other_code_number = 0;
2644 /* Contains I3 if the destination of I3 is used in its source, which means
2645 that the old life of I3 is being killed. If that usage is placed into
2646 I2 and not in I3, a REG_DEAD note must be made. */
2647 rtx i3dest_killed = 0;
2648 /* SET_DEST and SET_SRC of I2, I1 and I0. */
2649 rtx i2dest = 0, i2src = 0, i1dest = 0, i1src = 0, i0dest = 0, i0src = 0;
2650 /* Copy of SET_SRC of I1 and I0, if needed. */
2651 rtx i1src_copy = 0, i0src_copy = 0, i0src_copy2 = 0;
2652 /* Set if I2DEST was reused as a scratch register. */
2653 bool i2scratch = false;
2654 /* The PATTERNs of I0, I1, and I2, or a copy of them in certain cases. */
2655 rtx i0pat = 0, i1pat = 0, i2pat = 0;
2656 /* Indicates if I2DEST or I1DEST is in I2SRC or I1_SRC. */
2657 int i2dest_in_i2src = 0, i1dest_in_i1src = 0, i2dest_in_i1src = 0;
2658 int i0dest_in_i0src = 0, i1dest_in_i0src = 0, i2dest_in_i0src = 0;
2659 int i2dest_killed = 0, i1dest_killed = 0, i0dest_killed = 0;
2660 int i1_feeds_i2_n = 0, i0_feeds_i2_n = 0, i0_feeds_i1_n = 0;
2661 /* Notes that must be added to REG_NOTES in I3 and I2. */
2662 rtx new_i3_notes, new_i2_notes;
2663 /* Notes that we substituted I3 into I2 instead of the normal case. */
2664 int i3_subst_into_i2 = 0;
2665 /* Notes that I1, I2 or I3 is a MULT operation. */
2666 int have_mult = 0;
2667 int swap_i2i3 = 0;
2668 int split_i2i3 = 0;
2669 int changed_i3_dest = 0;
2671 int maxreg;
2672 rtx_insn *temp_insn;
2673 rtx temp_expr;
2674 struct insn_link *link;
2675 rtx other_pat = 0;
2676 rtx new_other_notes;
2677 int i;
2678 scalar_int_mode dest_mode, temp_mode;
2680 /* Immediately return if any of I0,I1,I2 are the same insn (I3 can
2681 never be). */
2682 if (i1 == i2 || i0 == i2 || (i0 && i0 == i1))
2683 return 0;
2685 /* Only try four-insn combinations when there's high likelihood of
2686 success. Look for simple insns, such as loads of constants or
2687 binary operations involving a constant. */
2688 if (i0)
2690 int i;
2691 int ngood = 0;
2692 int nshift = 0;
2693 rtx set0, set3;
2695 if (!flag_expensive_optimizations)
2696 return 0;
2698 for (i = 0; i < 4; i++)
2700 rtx_insn *insn = i == 0 ? i0 : i == 1 ? i1 : i == 2 ? i2 : i3;
2701 rtx set = single_set (insn);
2702 rtx src;
2703 if (!set)
2704 continue;
2705 src = SET_SRC (set);
2706 if (CONSTANT_P (src))
2708 ngood += 2;
2709 break;
2711 else if (BINARY_P (src) && CONSTANT_P (XEXP (src, 1)))
2712 ngood++;
2713 else if (GET_CODE (src) == ASHIFT || GET_CODE (src) == ASHIFTRT
2714 || GET_CODE (src) == LSHIFTRT)
2715 nshift++;
2718 /* If I0 loads a memory and I3 sets the same memory, then I1 and I2
2719 are likely manipulating its value. Ideally we'll be able to combine
2720 all four insns into a bitfield insertion of some kind.
2722 Note the source in I0 might be inside a sign/zero extension and the
2723 memory modes in I0 and I3 might be different. So extract the address
2724 from the destination of I3 and search for it in the source of I0.
2726 In the event that there's a match but the source/dest do not actually
2727 refer to the same memory, the worst that happens is we try some
2728 combinations that we wouldn't have otherwise. */
2729 if ((set0 = single_set (i0))
2730 /* Ensure the source of SET0 is a MEM, possibly buried inside
2731 an extension. */
2732 && (GET_CODE (SET_SRC (set0)) == MEM
2733 || ((GET_CODE (SET_SRC (set0)) == ZERO_EXTEND
2734 || GET_CODE (SET_SRC (set0)) == SIGN_EXTEND)
2735 && GET_CODE (XEXP (SET_SRC (set0), 0)) == MEM))
2736 && (set3 = single_set (i3))
2737 /* Ensure the destination of SET3 is a MEM. */
2738 && GET_CODE (SET_DEST (set3)) == MEM
2739 /* Would it be better to extract the base address for the MEM
2740 in SET3 and look for that? I don't have cases where it matters
2741 but I could envision such cases. */
2742 && rtx_referenced_p (XEXP (SET_DEST (set3), 0), SET_SRC (set0)))
2743 ngood += 2;
2745 if (ngood < 2 && nshift < 2)
2746 return 0;
2749 /* Exit early if one of the insns involved can't be used for
2750 combinations. */
2751 if (CALL_P (i2)
2752 || (i1 && CALL_P (i1))
2753 || (i0 && CALL_P (i0))
2754 || cant_combine_insn_p (i3)
2755 || cant_combine_insn_p (i2)
2756 || (i1 && cant_combine_insn_p (i1))
2757 || (i0 && cant_combine_insn_p (i0))
2758 || likely_spilled_retval_p (i3))
2759 return 0;
2761 combine_attempts++;
2762 undobuf.other_insn = 0;
2764 /* Reset the hard register usage information. */
2765 CLEAR_HARD_REG_SET (newpat_used_regs);
2767 if (dump_file && (dump_flags & TDF_DETAILS))
2769 if (i0)
2770 fprintf (dump_file, "\nTrying %d, %d, %d -> %d:\n",
2771 INSN_UID (i0), INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2772 else if (i1)
2773 fprintf (dump_file, "\nTrying %d, %d -> %d:\n",
2774 INSN_UID (i1), INSN_UID (i2), INSN_UID (i3));
2775 else
2776 fprintf (dump_file, "\nTrying %d -> %d:\n",
2777 INSN_UID (i2), INSN_UID (i3));
2779 if (i0)
2780 dump_insn_slim (dump_file, i0);
2781 if (i1)
2782 dump_insn_slim (dump_file, i1);
2783 dump_insn_slim (dump_file, i2);
2784 dump_insn_slim (dump_file, i3);
2787 /* If multiple insns feed into one of I2 or I3, they can be in any
2788 order. To simplify the code below, reorder them in sequence. */
2789 if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i2))
2790 std::swap (i0, i2);
2791 if (i0 && DF_INSN_LUID (i0) > DF_INSN_LUID (i1))
2792 std::swap (i0, i1);
2793 if (i1 && DF_INSN_LUID (i1) > DF_INSN_LUID (i2))
2794 std::swap (i1, i2);
2796 added_links_insn = 0;
2797 added_notes_insn = 0;
2799 /* First check for one important special case that the code below will
2800 not handle. Namely, the case where I1 is zero, I2 is a PARALLEL
2801 and I3 is a SET whose SET_SRC is a SET_DEST in I2. In that case,
2802 we may be able to replace that destination with the destination of I3.
2803 This occurs in the common code where we compute both a quotient and
2804 remainder into a structure, in which case we want to do the computation
2805 directly into the structure to avoid register-register copies.
2807 Note that this case handles both multiple sets in I2 and also cases
2808 where I2 has a number of CLOBBERs inside the PARALLEL.
2810 We make very conservative checks below and only try to handle the
2811 most common cases of this. For example, we only handle the case
2812 where I2 and I3 are adjacent to avoid making difficult register
2813 usage tests. */
2815 if (i1 == 0 && NONJUMP_INSN_P (i3) && GET_CODE (PATTERN (i3)) == SET
2816 && REG_P (SET_SRC (PATTERN (i3)))
2817 && REGNO (SET_SRC (PATTERN (i3))) >= FIRST_PSEUDO_REGISTER
2818 && find_reg_note (i3, REG_DEAD, SET_SRC (PATTERN (i3)))
2819 && GET_CODE (PATTERN (i2)) == PARALLEL
2820 && ! side_effects_p (SET_DEST (PATTERN (i3)))
2821 /* If the dest of I3 is a ZERO_EXTRACT or STRICT_LOW_PART, the code
2822 below would need to check what is inside (and reg_overlap_mentioned_p
2823 doesn't support those codes anyway). Don't allow those destinations;
2824 the resulting insn isn't likely to be recognized anyway. */
2825 && GET_CODE (SET_DEST (PATTERN (i3))) != ZERO_EXTRACT
2826 && GET_CODE (SET_DEST (PATTERN (i3))) != STRICT_LOW_PART
2827 && ! reg_overlap_mentioned_p (SET_SRC (PATTERN (i3)),
2828 SET_DEST (PATTERN (i3)))
2829 && next_active_insn (i2) == i3)
2831 rtx p2 = PATTERN (i2);
2833 /* Make sure that the destination of I3,
2834 which we are going to substitute into one output of I2,
2835 is not used within another output of I2. We must avoid making this:
2836 (parallel [(set (mem (reg 69)) ...)
2837 (set (reg 69) ...)])
2838 which is not well-defined as to order of actions.
2839 (Besides, reload can't handle output reloads for this.)
2841 The problem can also happen if the dest of I3 is a memory ref,
2842 if another dest in I2 is an indirect memory ref.
2844 Neither can this PARALLEL be an asm. We do not allow combining
2845 that usually (see can_combine_p), so do not here either. */
2846 bool ok = true;
2847 for (i = 0; ok && i < XVECLEN (p2, 0); i++)
2849 if ((GET_CODE (XVECEXP (p2, 0, i)) == SET
2850 || GET_CODE (XVECEXP (p2, 0, i)) == CLOBBER)
2851 && reg_overlap_mentioned_p (SET_DEST (PATTERN (i3)),
2852 SET_DEST (XVECEXP (p2, 0, i))))
2853 ok = false;
2854 else if (GET_CODE (XVECEXP (p2, 0, i)) == SET
2855 && GET_CODE (SET_SRC (XVECEXP (p2, 0, i))) == ASM_OPERANDS)
2856 ok = false;
2859 if (ok)
2860 for (i = 0; i < XVECLEN (p2, 0); i++)
2861 if (GET_CODE (XVECEXP (p2, 0, i)) == SET
2862 && SET_DEST (XVECEXP (p2, 0, i)) == SET_SRC (PATTERN (i3)))
2864 combine_merges++;
2866 subst_insn = i3;
2867 subst_low_luid = DF_INSN_LUID (i2);
2869 added_sets_2 = added_sets_1 = added_sets_0 = 0;
2870 i2src = SET_SRC (XVECEXP (p2, 0, i));
2871 i2dest = SET_DEST (XVECEXP (p2, 0, i));
2872 i2dest_killed = dead_or_set_p (i2, i2dest);
2874 /* Replace the dest in I2 with our dest and make the resulting
2875 insn the new pattern for I3. Then skip to where we validate
2876 the pattern. Everything was set up above. */
2877 SUBST (SET_DEST (XVECEXP (p2, 0, i)), SET_DEST (PATTERN (i3)));
2878 newpat = p2;
2879 i3_subst_into_i2 = 1;
2880 goto validate_replacement;
2884 /* If I2 is setting a pseudo to a constant and I3 is setting some
2885 sub-part of it to another constant, merge them by making a new
2886 constant. */
2887 if (i1 == 0
2888 && (temp_expr = single_set (i2)) != 0
2889 && is_a <scalar_int_mode> (GET_MODE (SET_DEST (temp_expr)), &temp_mode)
2890 && CONST_SCALAR_INT_P (SET_SRC (temp_expr))
2891 && GET_CODE (PATTERN (i3)) == SET
2892 && CONST_SCALAR_INT_P (SET_SRC (PATTERN (i3)))
2893 && reg_subword_p (SET_DEST (PATTERN (i3)), SET_DEST (temp_expr)))
2895 rtx dest = SET_DEST (PATTERN (i3));
2896 rtx temp_dest = SET_DEST (temp_expr);
2897 int offset = -1;
2898 int width = 0;
2900 if (GET_CODE (dest) == ZERO_EXTRACT)
2902 if (CONST_INT_P (XEXP (dest, 1))
2903 && CONST_INT_P (XEXP (dest, 2))
2904 && is_a <scalar_int_mode> (GET_MODE (XEXP (dest, 0)),
2905 &dest_mode))
2907 width = INTVAL (XEXP (dest, 1));
2908 offset = INTVAL (XEXP (dest, 2));
2909 dest = XEXP (dest, 0);
2910 if (BITS_BIG_ENDIAN)
2911 offset = GET_MODE_PRECISION (dest_mode) - width - offset;
2914 else
2916 if (GET_CODE (dest) == STRICT_LOW_PART)
2917 dest = XEXP (dest, 0);
2918 if (is_a <scalar_int_mode> (GET_MODE (dest), &dest_mode))
2920 width = GET_MODE_PRECISION (dest_mode);
2921 offset = 0;
2925 if (offset >= 0)
2927 /* If this is the low part, we're done. */
2928 if (subreg_lowpart_p (dest))
2930 /* Handle the case where inner is twice the size of outer. */
2931 else if (GET_MODE_PRECISION (temp_mode)
2932 == 2 * GET_MODE_PRECISION (dest_mode))
2933 offset += GET_MODE_PRECISION (dest_mode);
2934 /* Otherwise give up for now. */
2935 else
2936 offset = -1;
2939 if (offset >= 0)
2941 rtx inner = SET_SRC (PATTERN (i3));
2942 rtx outer = SET_SRC (temp_expr);
2944 wide_int o = wi::insert (rtx_mode_t (outer, temp_mode),
2945 rtx_mode_t (inner, dest_mode),
2946 offset, width);
2948 combine_merges++;
2949 subst_insn = i3;
2950 subst_low_luid = DF_INSN_LUID (i2);
2951 added_sets_2 = added_sets_1 = added_sets_0 = 0;
2952 i2dest = temp_dest;
2953 i2dest_killed = dead_or_set_p (i2, i2dest);
2955 /* Replace the source in I2 with the new constant and make the
2956 resulting insn the new pattern for I3. Then skip to where we
2957 validate the pattern. Everything was set up above. */
2958 SUBST (SET_SRC (temp_expr),
2959 immed_wide_int_const (o, temp_mode));
2961 newpat = PATTERN (i2);
2963 /* The dest of I3 has been replaced with the dest of I2. */
2964 changed_i3_dest = 1;
2965 goto validate_replacement;
2969 /* If we have no I1 and I2 looks like:
2970 (parallel [(set (reg:CC X) (compare:CC OP (const_int 0)))
2971 (set Y OP)])
2972 make up a dummy I1 that is
2973 (set Y OP)
2974 and change I2 to be
2975 (set (reg:CC X) (compare:CC Y (const_int 0)))
2977 (We can ignore any trailing CLOBBERs.)
2979 This undoes a previous combination and allows us to match a branch-and-
2980 decrement insn. */
2982 if (!HAVE_cc0 && i1 == 0
2983 && is_parallel_of_n_reg_sets (PATTERN (i2), 2)
2984 && (GET_MODE_CLASS (GET_MODE (SET_DEST (XVECEXP (PATTERN (i2), 0, 0))))
2985 == MODE_CC)
2986 && GET_CODE (SET_SRC (XVECEXP (PATTERN (i2), 0, 0))) == COMPARE
2987 && XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 1) == const0_rtx
2988 && rtx_equal_p (XEXP (SET_SRC (XVECEXP (PATTERN (i2), 0, 0)), 0),
2989 SET_SRC (XVECEXP (PATTERN (i2), 0, 1)))
2990 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 0)), i2, i3)
2991 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)), i2, i3))
2993 /* We make I1 with the same INSN_UID as I2. This gives it
2994 the same DF_INSN_LUID for value tracking. Our fake I1 will
2995 never appear in the insn stream so giving it the same INSN_UID
2996 as I2 will not cause a problem. */
2998 i1 = gen_rtx_INSN (VOIDmode, NULL, i2, BLOCK_FOR_INSN (i2),
2999 XVECEXP (PATTERN (i2), 0, 1), INSN_LOCATION (i2),
3000 -1, NULL_RTX);
3001 INSN_UID (i1) = INSN_UID (i2);
3003 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 0));
3004 SUBST (XEXP (SET_SRC (PATTERN (i2)), 0),
3005 SET_DEST (PATTERN (i1)));
3006 unsigned int regno = REGNO (SET_DEST (PATTERN (i1)));
3007 SUBST_LINK (LOG_LINKS (i2),
3008 alloc_insn_link (i1, regno, LOG_LINKS (i2)));
3011 /* If I2 is a PARALLEL of two SETs of REGs (and perhaps some CLOBBERs),
3012 make those two SETs separate I1 and I2 insns, and make an I0 that is
3013 the original I1. */
3014 if (!HAVE_cc0 && i0 == 0
3015 && is_parallel_of_n_reg_sets (PATTERN (i2), 2)
3016 && can_split_parallel_of_n_reg_sets (i2, 2)
3017 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 0)), i2, i3)
3018 && !reg_used_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)), i2, i3)
3019 && !reg_set_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 0)), i2, i3)
3020 && !reg_set_between_p (SET_DEST (XVECEXP (PATTERN (i2), 0, 1)), i2, i3))
3022 /* If there is no I1, there is no I0 either. */
3023 i0 = i1;
3025 /* We make I1 with the same INSN_UID as I2. This gives it
3026 the same DF_INSN_LUID for value tracking. Our fake I1 will
3027 never appear in the insn stream so giving it the same INSN_UID
3028 as I2 will not cause a problem. */
3030 i1 = gen_rtx_INSN (VOIDmode, NULL, i2, BLOCK_FOR_INSN (i2),
3031 XVECEXP (PATTERN (i2), 0, 0), INSN_LOCATION (i2),
3032 -1, NULL_RTX);
3033 INSN_UID (i1) = INSN_UID (i2);
3035 SUBST (PATTERN (i2), XVECEXP (PATTERN (i2), 0, 1));
3038 /* Verify that I2 and maybe I1 and I0 can be combined into I3. */
3039 if (!can_combine_p (i2, i3, i0, i1, NULL, NULL, &i2dest, &i2src))
3041 if (dump_file)
3042 fprintf (dump_file, "Can't combine i2 into i3\n");
3043 undo_all ();
3044 return 0;
3046 if (i1 && !can_combine_p (i1, i3, i0, NULL, i2, NULL, &i1dest, &i1src))
3048 if (dump_file)
3049 fprintf (dump_file, "Can't combine i1 into i3\n");
3050 undo_all ();
3051 return 0;
3053 if (i0 && !can_combine_p (i0, i3, NULL, NULL, i1, i2, &i0dest, &i0src))
3055 if (dump_file)
3056 fprintf (dump_file, "Can't combine i0 into i3\n");
3057 undo_all ();
3058 return 0;
3061 /* Record whether I2DEST is used in I2SRC and similarly for the other
3062 cases. Knowing this will help in register status updating below. */
3063 i2dest_in_i2src = reg_overlap_mentioned_p (i2dest, i2src);
3064 i1dest_in_i1src = i1 && reg_overlap_mentioned_p (i1dest, i1src);
3065 i2dest_in_i1src = i1 && reg_overlap_mentioned_p (i2dest, i1src);
3066 i0dest_in_i0src = i0 && reg_overlap_mentioned_p (i0dest, i0src);
3067 i1dest_in_i0src = i0 && reg_overlap_mentioned_p (i1dest, i0src);
3068 i2dest_in_i0src = i0 && reg_overlap_mentioned_p (i2dest, i0src);
3069 i2dest_killed = dead_or_set_p (i2, i2dest);
3070 i1dest_killed = i1 && dead_or_set_p (i1, i1dest);
3071 i0dest_killed = i0 && dead_or_set_p (i0, i0dest);
3073 /* For the earlier insns, determine which of the subsequent ones they
3074 feed. */
3075 i1_feeds_i2_n = i1 && insn_a_feeds_b (i1, i2);
3076 i0_feeds_i1_n = i0 && insn_a_feeds_b (i0, i1);
3077 i0_feeds_i2_n = (i0 && (!i0_feeds_i1_n ? insn_a_feeds_b (i0, i2)
3078 : (!reg_overlap_mentioned_p (i1dest, i0dest)
3079 && reg_overlap_mentioned_p (i0dest, i2src))));
3081 /* Ensure that I3's pattern can be the destination of combines. */
3082 if (! combinable_i3pat (i3, &PATTERN (i3), i2dest, i1dest, i0dest,
3083 i1 && i2dest_in_i1src && !i1_feeds_i2_n,
3084 i0 && ((i2dest_in_i0src && !i0_feeds_i2_n)
3085 || (i1dest_in_i0src && !i0_feeds_i1_n)),
3086 &i3dest_killed))
3088 undo_all ();
3089 return 0;
3092 /* See if any of the insns is a MULT operation. Unless one is, we will
3093 reject a combination that is, since it must be slower. Be conservative
3094 here. */
3095 if (GET_CODE (i2src) == MULT
3096 || (i1 != 0 && GET_CODE (i1src) == MULT)
3097 || (i0 != 0 && GET_CODE (i0src) == MULT)
3098 || (GET_CODE (PATTERN (i3)) == SET
3099 && GET_CODE (SET_SRC (PATTERN (i3))) == MULT))
3100 have_mult = 1;
3102 /* If I3 has an inc, then give up if I1 or I2 uses the reg that is inc'd.
3103 We used to do this EXCEPT in one case: I3 has a post-inc in an
3104 output operand. However, that exception can give rise to insns like
3105 mov r3,(r3)+
3106 which is a famous insn on the PDP-11 where the value of r3 used as the
3107 source was model-dependent. Avoid this sort of thing. */
3109 #if 0
3110 if (!(GET_CODE (PATTERN (i3)) == SET
3111 && REG_P (SET_SRC (PATTERN (i3)))
3112 && MEM_P (SET_DEST (PATTERN (i3)))
3113 && (GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_INC
3114 || GET_CODE (XEXP (SET_DEST (PATTERN (i3)), 0)) == POST_DEC)))
3115 /* It's not the exception. */
3116 #endif
3117 if (AUTO_INC_DEC)
3119 rtx link;
3120 for (link = REG_NOTES (i3); link; link = XEXP (link, 1))
3121 if (REG_NOTE_KIND (link) == REG_INC
3122 && (reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i2))
3123 || (i1 != 0
3124 && reg_overlap_mentioned_p (XEXP (link, 0), PATTERN (i1)))))
3126 undo_all ();
3127 return 0;
3131 /* See if the SETs in I1 or I2 need to be kept around in the merged
3132 instruction: whenever the value set there is still needed past I3.
3133 For the SET in I2, this is easy: we see if I2DEST dies or is set in I3.
3135 For the SET in I1, we have two cases: if I1 and I2 independently feed
3136 into I3, the set in I1 needs to be kept around unless I1DEST dies
3137 or is set in I3. Otherwise (if I1 feeds I2 which feeds I3), the set
3138 in I1 needs to be kept around unless I1DEST dies or is set in either
3139 I2 or I3. The same considerations apply to I0. */
3141 added_sets_2 = !dead_or_set_p (i3, i2dest);
3143 if (i1)
3144 added_sets_1 = !(dead_or_set_p (i3, i1dest)
3145 || (i1_feeds_i2_n && dead_or_set_p (i2, i1dest)));
3146 else
3147 added_sets_1 = 0;
3149 if (i0)
3150 added_sets_0 = !(dead_or_set_p (i3, i0dest)
3151 || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest))
3152 || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
3153 && dead_or_set_p (i2, i0dest)));
3154 else
3155 added_sets_0 = 0;
3157 /* We are about to copy insns for the case where they need to be kept
3158 around. Check that they can be copied in the merged instruction. */
3160 if (targetm.cannot_copy_insn_p
3161 && ((added_sets_2 && targetm.cannot_copy_insn_p (i2))
3162 || (i1 && added_sets_1 && targetm.cannot_copy_insn_p (i1))
3163 || (i0 && added_sets_0 && targetm.cannot_copy_insn_p (i0))))
3165 undo_all ();
3166 return 0;
3169 /* If the set in I2 needs to be kept around, we must make a copy of
3170 PATTERN (I2), so that when we substitute I1SRC for I1DEST in
3171 PATTERN (I2), we are only substituting for the original I1DEST, not into
3172 an already-substituted copy. This also prevents making self-referential
3173 rtx. If I2 is a PARALLEL, we just need the piece that assigns I2SRC to
3174 I2DEST. */
3176 if (added_sets_2)
3178 if (GET_CODE (PATTERN (i2)) == PARALLEL)
3179 i2pat = gen_rtx_SET (i2dest, copy_rtx (i2src));
3180 else
3181 i2pat = copy_rtx (PATTERN (i2));
3184 if (added_sets_1)
3186 if (GET_CODE (PATTERN (i1)) == PARALLEL)
3187 i1pat = gen_rtx_SET (i1dest, copy_rtx (i1src));
3188 else
3189 i1pat = copy_rtx (PATTERN (i1));
3192 if (added_sets_0)
3194 if (GET_CODE (PATTERN (i0)) == PARALLEL)
3195 i0pat = gen_rtx_SET (i0dest, copy_rtx (i0src));
3196 else
3197 i0pat = copy_rtx (PATTERN (i0));
3200 combine_merges++;
3202 /* Substitute in the latest insn for the regs set by the earlier ones. */
3204 maxreg = max_reg_num ();
3206 subst_insn = i3;
3208 /* Many machines that don't use CC0 have insns that can both perform an
3209 arithmetic operation and set the condition code. These operations will
3210 be represented as a PARALLEL with the first element of the vector
3211 being a COMPARE of an arithmetic operation with the constant zero.
3212 The second element of the vector will set some pseudo to the result
3213 of the same arithmetic operation. If we simplify the COMPARE, we won't
3214 match such a pattern and so will generate an extra insn. Here we test
3215 for this case, where both the comparison and the operation result are
3216 needed, and make the PARALLEL by just replacing I2DEST in I3SRC with
3217 I2SRC. Later we will make the PARALLEL that contains I2. */
3219 if (!HAVE_cc0 && i1 == 0 && added_sets_2 && GET_CODE (PATTERN (i3)) == SET
3220 && GET_CODE (SET_SRC (PATTERN (i3))) == COMPARE
3221 && CONST_INT_P (XEXP (SET_SRC (PATTERN (i3)), 1))
3222 && rtx_equal_p (XEXP (SET_SRC (PATTERN (i3)), 0), i2dest))
3224 rtx newpat_dest;
3225 rtx *cc_use_loc = NULL;
3226 rtx_insn *cc_use_insn = NULL;
3227 rtx op0 = i2src, op1 = XEXP (SET_SRC (PATTERN (i3)), 1);
3228 machine_mode compare_mode, orig_compare_mode;
3229 enum rtx_code compare_code = UNKNOWN, orig_compare_code = UNKNOWN;
3230 scalar_int_mode mode;
3232 newpat = PATTERN (i3);
3233 newpat_dest = SET_DEST (newpat);
3234 compare_mode = orig_compare_mode = GET_MODE (newpat_dest);
3236 if (undobuf.other_insn == 0
3237 && (cc_use_loc = find_single_use (SET_DEST (newpat), i3,
3238 &cc_use_insn)))
3240 compare_code = orig_compare_code = GET_CODE (*cc_use_loc);
3241 if (is_a <scalar_int_mode> (GET_MODE (i2dest), &mode))
3242 compare_code = simplify_compare_const (compare_code, mode,
3243 op0, &op1);
3244 target_canonicalize_comparison (&compare_code, &op0, &op1, 1);
3247 /* Do the rest only if op1 is const0_rtx, which may be the
3248 result of simplification. */
3249 if (op1 == const0_rtx)
3251 /* If a single use of the CC is found, prepare to modify it
3252 when SELECT_CC_MODE returns a new CC-class mode, or when
3253 the above simplify_compare_const() returned a new comparison
3254 operator. undobuf.other_insn is assigned the CC use insn
3255 when modifying it. */
3256 if (cc_use_loc)
3258 #ifdef SELECT_CC_MODE
3259 machine_mode new_mode
3260 = SELECT_CC_MODE (compare_code, op0, op1);
3261 if (new_mode != orig_compare_mode
3262 && can_change_dest_mode (SET_DEST (newpat),
3263 added_sets_2, new_mode))
3265 unsigned int regno = REGNO (newpat_dest);
3266 compare_mode = new_mode;
3267 if (regno < FIRST_PSEUDO_REGISTER)
3268 newpat_dest = gen_rtx_REG (compare_mode, regno);
3269 else
3271 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
3272 newpat_dest = regno_reg_rtx[regno];
3275 #endif
3276 /* Cases for modifying the CC-using comparison. */
3277 if (compare_code != orig_compare_code
3278 /* ??? Do we need to verify the zero rtx? */
3279 && XEXP (*cc_use_loc, 1) == const0_rtx)
3281 /* Replace cc_use_loc with entire new RTX. */
3282 SUBST (*cc_use_loc,
3283 gen_rtx_fmt_ee (compare_code, compare_mode,
3284 newpat_dest, const0_rtx));
3285 undobuf.other_insn = cc_use_insn;
3287 else if (compare_mode != orig_compare_mode)
3289 /* Just replace the CC reg with a new mode. */
3290 SUBST (XEXP (*cc_use_loc, 0), newpat_dest);
3291 undobuf.other_insn = cc_use_insn;
3295 /* Now we modify the current newpat:
3296 First, SET_DEST(newpat) is updated if the CC mode has been
3297 altered. For targets without SELECT_CC_MODE, this should be
3298 optimized away. */
3299 if (compare_mode != orig_compare_mode)
3300 SUBST (SET_DEST (newpat), newpat_dest);
3301 /* This is always done to propagate i2src into newpat. */
3302 SUBST (SET_SRC (newpat),
3303 gen_rtx_COMPARE (compare_mode, op0, op1));
3304 /* Create new version of i2pat if needed; the below PARALLEL
3305 creation needs this to work correctly. */
3306 if (! rtx_equal_p (i2src, op0))
3307 i2pat = gen_rtx_SET (i2dest, op0);
3308 i2_is_used = 1;
3312 if (i2_is_used == 0)
3314 /* It is possible that the source of I2 or I1 may be performing
3315 an unneeded operation, such as a ZERO_EXTEND of something
3316 that is known to have the high part zero. Handle that case
3317 by letting subst look at the inner insns.
3319 Another way to do this would be to have a function that tries
3320 to simplify a single insn instead of merging two or more
3321 insns. We don't do this because of the potential of infinite
3322 loops and because of the potential extra memory required.
3323 However, doing it the way we are is a bit of a kludge and
3324 doesn't catch all cases.
3326 But only do this if -fexpensive-optimizations since it slows
3327 things down and doesn't usually win.
3329 This is not done in the COMPARE case above because the
3330 unmodified I2PAT is used in the PARALLEL and so a pattern
3331 with a modified I2SRC would not match. */
3333 if (flag_expensive_optimizations)
3335 /* Pass pc_rtx so no substitutions are done, just
3336 simplifications. */
3337 if (i1)
3339 subst_low_luid = DF_INSN_LUID (i1);
3340 i1src = subst (i1src, pc_rtx, pc_rtx, 0, 0, 0);
3343 subst_low_luid = DF_INSN_LUID (i2);
3344 i2src = subst (i2src, pc_rtx, pc_rtx, 0, 0, 0);
3347 n_occurrences = 0; /* `subst' counts here */
3348 subst_low_luid = DF_INSN_LUID (i2);
3350 /* If I1 feeds into I2 and I1DEST is in I1SRC, we need to make a unique
3351 copy of I2SRC each time we substitute it, in order to avoid creating
3352 self-referential RTL when we will be substituting I1SRC for I1DEST
3353 later. Likewise if I0 feeds into I2, either directly or indirectly
3354 through I1, and I0DEST is in I0SRC. */
3355 newpat = subst (PATTERN (i3), i2dest, i2src, 0, 0,
3356 (i1_feeds_i2_n && i1dest_in_i1src)
3357 || ((i0_feeds_i2_n || (i0_feeds_i1_n && i1_feeds_i2_n))
3358 && i0dest_in_i0src));
3359 substed_i2 = 1;
3361 /* Record whether I2's body now appears within I3's body. */
3362 i2_is_used = n_occurrences;
3365 /* If we already got a failure, don't try to do more. Otherwise, try to
3366 substitute I1 if we have it. */
3368 if (i1 && GET_CODE (newpat) != CLOBBER)
3370 /* Check that an autoincrement side-effect on I1 has not been lost.
3371 This happens if I1DEST is mentioned in I2 and dies there, and
3372 has disappeared from the new pattern. */
3373 if ((FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3374 && i1_feeds_i2_n
3375 && dead_or_set_p (i2, i1dest)
3376 && !reg_overlap_mentioned_p (i1dest, newpat))
3377 /* Before we can do this substitution, we must redo the test done
3378 above (see detailed comments there) that ensures I1DEST isn't
3379 mentioned in any SETs in NEWPAT that are field assignments. */
3380 || !combinable_i3pat (NULL, &newpat, i1dest, NULL_RTX, NULL_RTX,
3381 0, 0, 0))
3383 undo_all ();
3384 return 0;
3387 n_occurrences = 0;
3388 subst_low_luid = DF_INSN_LUID (i1);
3390 /* If the following substitution will modify I1SRC, make a copy of it
3391 for the case where it is substituted for I1DEST in I2PAT later. */
3392 if (added_sets_2 && i1_feeds_i2_n)
3393 i1src_copy = copy_rtx (i1src);
3395 /* If I0 feeds into I1 and I0DEST is in I0SRC, we need to make a unique
3396 copy of I1SRC each time we substitute it, in order to avoid creating
3397 self-referential RTL when we will be substituting I0SRC for I0DEST
3398 later. */
3399 newpat = subst (newpat, i1dest, i1src, 0, 0,
3400 i0_feeds_i1_n && i0dest_in_i0src);
3401 substed_i1 = 1;
3403 /* Record whether I1's body now appears within I3's body. */
3404 i1_is_used = n_occurrences;
3407 /* Likewise for I0 if we have it. */
3409 if (i0 && GET_CODE (newpat) != CLOBBER)
3411 if ((FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3412 && ((i0_feeds_i2_n && dead_or_set_p (i2, i0dest))
3413 || (i0_feeds_i1_n && dead_or_set_p (i1, i0dest)))
3414 && !reg_overlap_mentioned_p (i0dest, newpat))
3415 || !combinable_i3pat (NULL, &newpat, i0dest, NULL_RTX, NULL_RTX,
3416 0, 0, 0))
3418 undo_all ();
3419 return 0;
3422 /* If the following substitution will modify I0SRC, make a copy of it
3423 for the case where it is substituted for I0DEST in I1PAT later. */
3424 if (added_sets_1 && i0_feeds_i1_n)
3425 i0src_copy = copy_rtx (i0src);
3426 /* And a copy for I0DEST in I2PAT substitution. */
3427 if (added_sets_2 && ((i0_feeds_i1_n && i1_feeds_i2_n)
3428 || (i0_feeds_i2_n)))
3429 i0src_copy2 = copy_rtx (i0src);
3431 n_occurrences = 0;
3432 subst_low_luid = DF_INSN_LUID (i0);
3433 newpat = subst (newpat, i0dest, i0src, 0, 0, 0);
3434 substed_i0 = 1;
3437 /* Fail if an autoincrement side-effect has been duplicated. Be careful
3438 to count all the ways that I2SRC and I1SRC can be used. */
3439 if ((FIND_REG_INC_NOTE (i2, NULL_RTX) != 0
3440 && i2_is_used + added_sets_2 > 1)
3441 || (i1 != 0 && FIND_REG_INC_NOTE (i1, NULL_RTX) != 0
3442 && (i1_is_used + added_sets_1 + (added_sets_2 && i1_feeds_i2_n)
3443 > 1))
3444 || (i0 != 0 && FIND_REG_INC_NOTE (i0, NULL_RTX) != 0
3445 && (n_occurrences + added_sets_0
3446 + (added_sets_1 && i0_feeds_i1_n)
3447 + (added_sets_2 && i0_feeds_i2_n)
3448 > 1))
3449 /* Fail if we tried to make a new register. */
3450 || max_reg_num () != maxreg
3451 /* Fail if we couldn't do something and have a CLOBBER. */
3452 || GET_CODE (newpat) == CLOBBER
3453 /* Fail if this new pattern is a MULT and we didn't have one before
3454 at the outer level. */
3455 || (GET_CODE (newpat) == SET && GET_CODE (SET_SRC (newpat)) == MULT
3456 && ! have_mult))
3458 undo_all ();
3459 return 0;
3462 /* If the actions of the earlier insns must be kept
3463 in addition to substituting them into the latest one,
3464 we must make a new PARALLEL for the latest insn
3465 to hold additional the SETs. */
3467 if (added_sets_0 || added_sets_1 || added_sets_2)
3469 int extra_sets = added_sets_0 + added_sets_1 + added_sets_2;
3470 combine_extras++;
3472 if (GET_CODE (newpat) == PARALLEL)
3474 rtvec old = XVEC (newpat, 0);
3475 total_sets = XVECLEN (newpat, 0) + extra_sets;
3476 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3477 memcpy (XVEC (newpat, 0)->elem, &old->elem[0],
3478 sizeof (old->elem[0]) * old->num_elem);
3480 else
3482 rtx old = newpat;
3483 total_sets = 1 + extra_sets;
3484 newpat = gen_rtx_PARALLEL (VOIDmode, rtvec_alloc (total_sets));
3485 XVECEXP (newpat, 0, 0) = old;
3488 if (added_sets_0)
3489 XVECEXP (newpat, 0, --total_sets) = i0pat;
3491 if (added_sets_1)
3493 rtx t = i1pat;
3494 if (i0_feeds_i1_n)
3495 t = subst (t, i0dest, i0src_copy ? i0src_copy : i0src, 0, 0, 0);
3497 XVECEXP (newpat, 0, --total_sets) = t;
3499 if (added_sets_2)
3501 rtx t = i2pat;
3502 if (i1_feeds_i2_n)
3503 t = subst (t, i1dest, i1src_copy ? i1src_copy : i1src, 0, 0,
3504 i0_feeds_i1_n && i0dest_in_i0src);
3505 if ((i0_feeds_i1_n && i1_feeds_i2_n) || i0_feeds_i2_n)
3506 t = subst (t, i0dest, i0src_copy2 ? i0src_copy2 : i0src, 0, 0, 0);
3508 XVECEXP (newpat, 0, --total_sets) = t;
3512 validate_replacement:
3514 /* Note which hard regs this insn has as inputs. */
3515 mark_used_regs_combine (newpat);
3517 /* If recog_for_combine fails, it strips existing clobbers. If we'll
3518 consider splitting this pattern, we might need these clobbers. */
3519 if (i1 && GET_CODE (newpat) == PARALLEL
3520 && GET_CODE (XVECEXP (newpat, 0, XVECLEN (newpat, 0) - 1)) == CLOBBER)
3522 int len = XVECLEN (newpat, 0);
3524 newpat_vec_with_clobbers = rtvec_alloc (len);
3525 for (i = 0; i < len; i++)
3526 RTVEC_ELT (newpat_vec_with_clobbers, i) = XVECEXP (newpat, 0, i);
3529 /* We have recognized nothing yet. */
3530 insn_code_number = -1;
3532 /* See if this is a PARALLEL of two SETs where one SET's destination is
3533 a register that is unused and this isn't marked as an instruction that
3534 might trap in an EH region. In that case, we just need the other SET.
3535 We prefer this over the PARALLEL.
3537 This can occur when simplifying a divmod insn. We *must* test for this
3538 case here because the code below that splits two independent SETs doesn't
3539 handle this case correctly when it updates the register status.
3541 It's pointless doing this if we originally had two sets, one from
3542 i3, and one from i2. Combining then splitting the parallel results
3543 in the original i2 again plus an invalid insn (which we delete).
3544 The net effect is only to move instructions around, which makes
3545 debug info less accurate.
3547 If the remaining SET came from I2 its destination should not be used
3548 between I2 and I3. See PR82024. */
3550 if (!(added_sets_2 && i1 == 0)
3551 && is_parallel_of_n_reg_sets (newpat, 2)
3552 && asm_noperands (newpat) < 0)
3554 rtx set0 = XVECEXP (newpat, 0, 0);
3555 rtx set1 = XVECEXP (newpat, 0, 1);
3556 rtx oldpat = newpat;
3558 if (((REG_P (SET_DEST (set1))
3559 && find_reg_note (i3, REG_UNUSED, SET_DEST (set1)))
3560 || (GET_CODE (SET_DEST (set1)) == SUBREG
3561 && find_reg_note (i3, REG_UNUSED, SUBREG_REG (SET_DEST (set1)))))
3562 && insn_nothrow_p (i3)
3563 && !side_effects_p (SET_SRC (set1)))
3565 newpat = set0;
3566 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3569 else if (((REG_P (SET_DEST (set0))
3570 && find_reg_note (i3, REG_UNUSED, SET_DEST (set0)))
3571 || (GET_CODE (SET_DEST (set0)) == SUBREG
3572 && find_reg_note (i3, REG_UNUSED,
3573 SUBREG_REG (SET_DEST (set0)))))
3574 && insn_nothrow_p (i3)
3575 && !side_effects_p (SET_SRC (set0)))
3577 rtx dest = SET_DEST (set1);
3578 if (GET_CODE (dest) == SUBREG)
3579 dest = SUBREG_REG (dest);
3580 if (!reg_used_between_p (dest, i2, i3))
3582 newpat = set1;
3583 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3585 if (insn_code_number >= 0)
3586 changed_i3_dest = 1;
3590 if (insn_code_number < 0)
3591 newpat = oldpat;
3594 /* Is the result of combination a valid instruction? */
3595 if (insn_code_number < 0)
3596 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3598 /* If we were combining three insns and the result is a simple SET
3599 with no ASM_OPERANDS that wasn't recognized, try to split it into two
3600 insns. There are two ways to do this. It can be split using a
3601 machine-specific method (like when you have an addition of a large
3602 constant) or by combine in the function find_split_point. */
3604 if (i1 && insn_code_number < 0 && GET_CODE (newpat) == SET
3605 && asm_noperands (newpat) < 0)
3607 rtx parallel, *split;
3608 rtx_insn *m_split_insn;
3610 /* See if the MD file can split NEWPAT. If it can't, see if letting it
3611 use I2DEST as a scratch register will help. In the latter case,
3612 convert I2DEST to the mode of the source of NEWPAT if we can. */
3614 m_split_insn = combine_split_insns (newpat, i3);
3616 /* We can only use I2DEST as a scratch reg if it doesn't overlap any
3617 inputs of NEWPAT. */
3619 /* ??? If I2DEST is not safe, and I1DEST exists, then it would be
3620 possible to try that as a scratch reg. This would require adding
3621 more code to make it work though. */
3623 if (m_split_insn == 0 && ! reg_overlap_mentioned_p (i2dest, newpat))
3625 machine_mode new_mode = GET_MODE (SET_DEST (newpat));
3627 /* ??? Reusing i2dest without resetting the reg_stat entry for it
3628 (temporarily, until we are committed to this instruction
3629 combination) does not work: for example, any call to nonzero_bits
3630 on the register (from a splitter in the MD file, for example)
3631 will get the old information, which is invalid.
3633 Since nowadays we can create registers during combine just fine,
3634 we should just create a new one here, not reuse i2dest. */
3636 /* First try to split using the original register as a
3637 scratch register. */
3638 parallel = gen_rtx_PARALLEL (VOIDmode,
3639 gen_rtvec (2, newpat,
3640 gen_rtx_CLOBBER (VOIDmode,
3641 i2dest)));
3642 m_split_insn = combine_split_insns (parallel, i3);
3644 /* If that didn't work, try changing the mode of I2DEST if
3645 we can. */
3646 if (m_split_insn == 0
3647 && new_mode != GET_MODE (i2dest)
3648 && new_mode != VOIDmode
3649 && can_change_dest_mode (i2dest, added_sets_2, new_mode))
3651 machine_mode old_mode = GET_MODE (i2dest);
3652 rtx ni2dest;
3654 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3655 ni2dest = gen_rtx_REG (new_mode, REGNO (i2dest));
3656 else
3658 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], new_mode);
3659 ni2dest = regno_reg_rtx[REGNO (i2dest)];
3662 parallel = (gen_rtx_PARALLEL
3663 (VOIDmode,
3664 gen_rtvec (2, newpat,
3665 gen_rtx_CLOBBER (VOIDmode,
3666 ni2dest))));
3667 m_split_insn = combine_split_insns (parallel, i3);
3669 if (m_split_insn == 0
3670 && REGNO (i2dest) >= FIRST_PSEUDO_REGISTER)
3672 struct undo *buf;
3674 adjust_reg_mode (regno_reg_rtx[REGNO (i2dest)], old_mode);
3675 buf = undobuf.undos;
3676 undobuf.undos = buf->next;
3677 buf->next = undobuf.frees;
3678 undobuf.frees = buf;
3682 i2scratch = m_split_insn != 0;
3685 /* If recog_for_combine has discarded clobbers, try to use them
3686 again for the split. */
3687 if (m_split_insn == 0 && newpat_vec_with_clobbers)
3689 parallel = gen_rtx_PARALLEL (VOIDmode, newpat_vec_with_clobbers);
3690 m_split_insn = combine_split_insns (parallel, i3);
3693 if (m_split_insn && NEXT_INSN (m_split_insn) == NULL_RTX)
3695 rtx m_split_pat = PATTERN (m_split_insn);
3696 insn_code_number = recog_for_combine (&m_split_pat, i3, &new_i3_notes);
3697 if (insn_code_number >= 0)
3698 newpat = m_split_pat;
3700 else if (m_split_insn && NEXT_INSN (NEXT_INSN (m_split_insn)) == NULL_RTX
3701 && (next_nonnote_nondebug_insn (i2) == i3
3702 || !modified_between_p (PATTERN (m_split_insn), i2, i3)))
3704 rtx i2set, i3set;
3705 rtx newi3pat = PATTERN (NEXT_INSN (m_split_insn));
3706 newi2pat = PATTERN (m_split_insn);
3708 i3set = single_set (NEXT_INSN (m_split_insn));
3709 i2set = single_set (m_split_insn);
3711 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3713 /* If I2 or I3 has multiple SETs, we won't know how to track
3714 register status, so don't use these insns. If I2's destination
3715 is used between I2 and I3, we also can't use these insns. */
3717 if (i2_code_number >= 0 && i2set && i3set
3718 && (next_nonnote_nondebug_insn (i2) == i3
3719 || ! reg_used_between_p (SET_DEST (i2set), i2, i3)))
3720 insn_code_number = recog_for_combine (&newi3pat, i3,
3721 &new_i3_notes);
3722 if (insn_code_number >= 0)
3723 newpat = newi3pat;
3725 /* It is possible that both insns now set the destination of I3.
3726 If so, we must show an extra use of it. */
3728 if (insn_code_number >= 0)
3730 rtx new_i3_dest = SET_DEST (i3set);
3731 rtx new_i2_dest = SET_DEST (i2set);
3733 while (GET_CODE (new_i3_dest) == ZERO_EXTRACT
3734 || GET_CODE (new_i3_dest) == STRICT_LOW_PART
3735 || GET_CODE (new_i3_dest) == SUBREG)
3736 new_i3_dest = XEXP (new_i3_dest, 0);
3738 while (GET_CODE (new_i2_dest) == ZERO_EXTRACT
3739 || GET_CODE (new_i2_dest) == STRICT_LOW_PART
3740 || GET_CODE (new_i2_dest) == SUBREG)
3741 new_i2_dest = XEXP (new_i2_dest, 0);
3743 if (REG_P (new_i3_dest)
3744 && REG_P (new_i2_dest)
3745 && REGNO (new_i3_dest) == REGNO (new_i2_dest)
3746 && REGNO (new_i2_dest) < reg_n_sets_max)
3747 INC_REG_N_SETS (REGNO (new_i2_dest), 1);
3751 /* If we can split it and use I2DEST, go ahead and see if that
3752 helps things be recognized. Verify that none of the registers
3753 are set between I2 and I3. */
3754 if (insn_code_number < 0
3755 && (split = find_split_point (&newpat, i3, false)) != 0
3756 && (!HAVE_cc0 || REG_P (i2dest))
3757 /* We need I2DEST in the proper mode. If it is a hard register
3758 or the only use of a pseudo, we can change its mode.
3759 Make sure we don't change a hard register to have a mode that
3760 isn't valid for it, or change the number of registers. */
3761 && (GET_MODE (*split) == GET_MODE (i2dest)
3762 || GET_MODE (*split) == VOIDmode
3763 || can_change_dest_mode (i2dest, added_sets_2,
3764 GET_MODE (*split)))
3765 && (next_nonnote_nondebug_insn (i2) == i3
3766 || !modified_between_p (*split, i2, i3))
3767 /* We can't overwrite I2DEST if its value is still used by
3768 NEWPAT. */
3769 && ! reg_referenced_p (i2dest, newpat))
3771 rtx newdest = i2dest;
3772 enum rtx_code split_code = GET_CODE (*split);
3773 machine_mode split_mode = GET_MODE (*split);
3774 bool subst_done = false;
3775 newi2pat = NULL_RTX;
3777 i2scratch = true;
3779 /* *SPLIT may be part of I2SRC, so make sure we have the
3780 original expression around for later debug processing.
3781 We should not need I2SRC any more in other cases. */
3782 if (MAY_HAVE_DEBUG_BIND_INSNS)
3783 i2src = copy_rtx (i2src);
3784 else
3785 i2src = NULL;
3787 /* Get NEWDEST as a register in the proper mode. We have already
3788 validated that we can do this. */
3789 if (GET_MODE (i2dest) != split_mode && split_mode != VOIDmode)
3791 if (REGNO (i2dest) < FIRST_PSEUDO_REGISTER)
3792 newdest = gen_rtx_REG (split_mode, REGNO (i2dest));
3793 else
3795 SUBST_MODE (regno_reg_rtx[REGNO (i2dest)], split_mode);
3796 newdest = regno_reg_rtx[REGNO (i2dest)];
3800 /* If *SPLIT is a (mult FOO (const_int pow2)), convert it to
3801 an ASHIFT. This can occur if it was inside a PLUS and hence
3802 appeared to be a memory address. This is a kludge. */
3803 if (split_code == MULT
3804 && CONST_INT_P (XEXP (*split, 1))
3805 && INTVAL (XEXP (*split, 1)) > 0
3806 && (i = exact_log2 (UINTVAL (XEXP (*split, 1)))) >= 0)
3808 rtx i_rtx = gen_int_shift_amount (split_mode, i);
3809 SUBST (*split, gen_rtx_ASHIFT (split_mode,
3810 XEXP (*split, 0), i_rtx));
3811 /* Update split_code because we may not have a multiply
3812 anymore. */
3813 split_code = GET_CODE (*split);
3816 /* Similarly for (plus (mult FOO (const_int pow2))). */
3817 if (split_code == PLUS
3818 && GET_CODE (XEXP (*split, 0)) == MULT
3819 && CONST_INT_P (XEXP (XEXP (*split, 0), 1))
3820 && INTVAL (XEXP (XEXP (*split, 0), 1)) > 0
3821 && (i = exact_log2 (UINTVAL (XEXP (XEXP (*split, 0), 1)))) >= 0)
3823 rtx nsplit = XEXP (*split, 0);
3824 rtx i_rtx = gen_int_shift_amount (GET_MODE (nsplit), i);
3825 SUBST (XEXP (*split, 0), gen_rtx_ASHIFT (GET_MODE (nsplit),
3826 XEXP (nsplit, 0),
3827 i_rtx));
3828 /* Update split_code because we may not have a multiply
3829 anymore. */
3830 split_code = GET_CODE (*split);
3833 #ifdef INSN_SCHEDULING
3834 /* If *SPLIT is a paradoxical SUBREG, when we split it, it should
3835 be written as a ZERO_EXTEND. */
3836 if (split_code == SUBREG && MEM_P (SUBREG_REG (*split)))
3838 /* Or as a SIGN_EXTEND if LOAD_EXTEND_OP says that that's
3839 what it really is. */
3840 if (load_extend_op (GET_MODE (SUBREG_REG (*split)))
3841 == SIGN_EXTEND)
3842 SUBST (*split, gen_rtx_SIGN_EXTEND (split_mode,
3843 SUBREG_REG (*split)));
3844 else
3845 SUBST (*split, gen_rtx_ZERO_EXTEND (split_mode,
3846 SUBREG_REG (*split)));
3848 #endif
3850 /* Attempt to split binary operators using arithmetic identities. */
3851 if (BINARY_P (SET_SRC (newpat))
3852 && split_mode == GET_MODE (SET_SRC (newpat))
3853 && ! side_effects_p (SET_SRC (newpat)))
3855 rtx setsrc = SET_SRC (newpat);
3856 machine_mode mode = GET_MODE (setsrc);
3857 enum rtx_code code = GET_CODE (setsrc);
3858 rtx src_op0 = XEXP (setsrc, 0);
3859 rtx src_op1 = XEXP (setsrc, 1);
3861 /* Split "X = Y op Y" as "Z = Y; X = Z op Z". */
3862 if (rtx_equal_p (src_op0, src_op1))
3864 newi2pat = gen_rtx_SET (newdest, src_op0);
3865 SUBST (XEXP (setsrc, 0), newdest);
3866 SUBST (XEXP (setsrc, 1), newdest);
3867 subst_done = true;
3869 /* Split "((P op Q) op R) op S" where op is PLUS or MULT. */
3870 else if ((code == PLUS || code == MULT)
3871 && GET_CODE (src_op0) == code
3872 && GET_CODE (XEXP (src_op0, 0)) == code
3873 && (INTEGRAL_MODE_P (mode)
3874 || (FLOAT_MODE_P (mode)
3875 && flag_unsafe_math_optimizations)))
3877 rtx p = XEXP (XEXP (src_op0, 0), 0);
3878 rtx q = XEXP (XEXP (src_op0, 0), 1);
3879 rtx r = XEXP (src_op0, 1);
3880 rtx s = src_op1;
3882 /* Split both "((X op Y) op X) op Y" and
3883 "((X op Y) op Y) op X" as "T op T" where T is
3884 "X op Y". */
3885 if ((rtx_equal_p (p,r) && rtx_equal_p (q,s))
3886 || (rtx_equal_p (p,s) && rtx_equal_p (q,r)))
3888 newi2pat = gen_rtx_SET (newdest, XEXP (src_op0, 0));
3889 SUBST (XEXP (setsrc, 0), newdest);
3890 SUBST (XEXP (setsrc, 1), newdest);
3891 subst_done = true;
3893 /* Split "((X op X) op Y) op Y)" as "T op T" where
3894 T is "X op Y". */
3895 else if (rtx_equal_p (p,q) && rtx_equal_p (r,s))
3897 rtx tmp = simplify_gen_binary (code, mode, p, r);
3898 newi2pat = gen_rtx_SET (newdest, tmp);
3899 SUBST (XEXP (setsrc, 0), newdest);
3900 SUBST (XEXP (setsrc, 1), newdest);
3901 subst_done = true;
3906 if (!subst_done)
3908 newi2pat = gen_rtx_SET (newdest, *split);
3909 SUBST (*split, newdest);
3912 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3914 /* recog_for_combine might have added CLOBBERs to newi2pat.
3915 Make sure NEWPAT does not depend on the clobbered regs. */
3916 if (GET_CODE (newi2pat) == PARALLEL)
3917 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
3918 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
3920 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
3921 if (reg_overlap_mentioned_p (reg, newpat))
3923 undo_all ();
3924 return 0;
3928 /* If the split point was a MULT and we didn't have one before,
3929 don't use one now. */
3930 if (i2_code_number >= 0 && ! (split_code == MULT && ! have_mult))
3931 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3935 /* Check for a case where we loaded from memory in a narrow mode and
3936 then sign extended it, but we need both registers. In that case,
3937 we have a PARALLEL with both loads from the same memory location.
3938 We can split this into a load from memory followed by a register-register
3939 copy. This saves at least one insn, more if register allocation can
3940 eliminate the copy.
3942 We cannot do this if the destination of the first assignment is a
3943 condition code register or cc0. We eliminate this case by making sure
3944 the SET_DEST and SET_SRC have the same mode.
3946 We cannot do this if the destination of the second assignment is
3947 a register that we have already assumed is zero-extended. Similarly
3948 for a SUBREG of such a register. */
3950 else if (i1 && insn_code_number < 0 && asm_noperands (newpat) < 0
3951 && GET_CODE (newpat) == PARALLEL
3952 && XVECLEN (newpat, 0) == 2
3953 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
3954 && GET_CODE (SET_SRC (XVECEXP (newpat, 0, 0))) == SIGN_EXTEND
3955 && (GET_MODE (SET_DEST (XVECEXP (newpat, 0, 0)))
3956 == GET_MODE (SET_SRC (XVECEXP (newpat, 0, 0))))
3957 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
3958 && rtx_equal_p (SET_SRC (XVECEXP (newpat, 0, 1)),
3959 XEXP (SET_SRC (XVECEXP (newpat, 0, 0)), 0))
3960 && !modified_between_p (SET_SRC (XVECEXP (newpat, 0, 1)), i2, i3)
3961 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
3962 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
3963 && ! (temp_expr = SET_DEST (XVECEXP (newpat, 0, 1)),
3964 (REG_P (temp_expr)
3965 && reg_stat[REGNO (temp_expr)].nonzero_bits != 0
3966 && known_lt (GET_MODE_PRECISION (GET_MODE (temp_expr)),
3967 BITS_PER_WORD)
3968 && known_lt (GET_MODE_PRECISION (GET_MODE (temp_expr)),
3969 HOST_BITS_PER_INT)
3970 && (reg_stat[REGNO (temp_expr)].nonzero_bits
3971 != GET_MODE_MASK (word_mode))))
3972 && ! (GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) == SUBREG
3973 && (temp_expr = SUBREG_REG (SET_DEST (XVECEXP (newpat, 0, 1))),
3974 (REG_P (temp_expr)
3975 && reg_stat[REGNO (temp_expr)].nonzero_bits != 0
3976 && known_lt (GET_MODE_PRECISION (GET_MODE (temp_expr)),
3977 BITS_PER_WORD)
3978 && known_lt (GET_MODE_PRECISION (GET_MODE (temp_expr)),
3979 HOST_BITS_PER_INT)
3980 && (reg_stat[REGNO (temp_expr)].nonzero_bits
3981 != GET_MODE_MASK (word_mode)))))
3982 && ! reg_overlap_mentioned_p (SET_DEST (XVECEXP (newpat, 0, 1)),
3983 SET_SRC (XVECEXP (newpat, 0, 1)))
3984 && ! find_reg_note (i3, REG_UNUSED,
3985 SET_DEST (XVECEXP (newpat, 0, 0))))
3987 rtx ni2dest;
3989 newi2pat = XVECEXP (newpat, 0, 0);
3990 ni2dest = SET_DEST (XVECEXP (newpat, 0, 0));
3991 newpat = XVECEXP (newpat, 0, 1);
3992 SUBST (SET_SRC (newpat),
3993 gen_lowpart (GET_MODE (SET_SRC (newpat)), ni2dest));
3994 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
3996 if (i2_code_number >= 0)
3997 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
3999 if (insn_code_number >= 0)
4000 swap_i2i3 = 1;
4003 /* Similarly, check for a case where we have a PARALLEL of two independent
4004 SETs but we started with three insns. In this case, we can do the sets
4005 as two separate insns. This case occurs when some SET allows two
4006 other insns to combine, but the destination of that SET is still live.
4008 Also do this if we started with two insns and (at least) one of the
4009 resulting sets is a noop; this noop will be deleted later. */
4011 else if (insn_code_number < 0 && asm_noperands (newpat) < 0
4012 && GET_CODE (newpat) == PARALLEL
4013 && XVECLEN (newpat, 0) == 2
4014 && GET_CODE (XVECEXP (newpat, 0, 0)) == SET
4015 && GET_CODE (XVECEXP (newpat, 0, 1)) == SET
4016 && (i1 || set_noop_p (XVECEXP (newpat, 0, 0))
4017 || set_noop_p (XVECEXP (newpat, 0, 1)))
4018 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != ZERO_EXTRACT
4019 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 0))) != STRICT_LOW_PART
4020 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != ZERO_EXTRACT
4021 && GET_CODE (SET_DEST (XVECEXP (newpat, 0, 1))) != STRICT_LOW_PART
4022 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 1)),
4023 XVECEXP (newpat, 0, 0))
4024 && ! reg_referenced_p (SET_DEST (XVECEXP (newpat, 0, 0)),
4025 XVECEXP (newpat, 0, 1))
4026 && ! (contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 0)))
4027 && contains_muldiv (SET_SRC (XVECEXP (newpat, 0, 1)))))
4029 rtx set0 = XVECEXP (newpat, 0, 0);
4030 rtx set1 = XVECEXP (newpat, 0, 1);
4032 /* Normally, it doesn't matter which of the two is done first,
4033 but the one that references cc0 can't be the second, and
4034 one which uses any regs/memory set in between i2 and i3 can't
4035 be first. The PARALLEL might also have been pre-existing in i3,
4036 so we need to make sure that we won't wrongly hoist a SET to i2
4037 that would conflict with a death note present in there. */
4038 if (!modified_between_p (SET_SRC (set1), i2, i3)
4039 && !(REG_P (SET_DEST (set1))
4040 && find_reg_note (i2, REG_DEAD, SET_DEST (set1)))
4041 && !(GET_CODE (SET_DEST (set1)) == SUBREG
4042 && find_reg_note (i2, REG_DEAD,
4043 SUBREG_REG (SET_DEST (set1))))
4044 && (!HAVE_cc0 || !reg_referenced_p (cc0_rtx, set0))
4045 /* If I3 is a jump, ensure that set0 is a jump so that
4046 we do not create invalid RTL. */
4047 && (!JUMP_P (i3) || SET_DEST (set0) == pc_rtx)
4050 newi2pat = set1;
4051 newpat = set0;
4053 else if (!modified_between_p (SET_SRC (set0), i2, i3)
4054 && !(REG_P (SET_DEST (set0))
4055 && find_reg_note (i2, REG_DEAD, SET_DEST (set0)))
4056 && !(GET_CODE (SET_DEST (set0)) == SUBREG
4057 && find_reg_note (i2, REG_DEAD,
4058 SUBREG_REG (SET_DEST (set0))))
4059 && (!HAVE_cc0 || !reg_referenced_p (cc0_rtx, set1))
4060 /* If I3 is a jump, ensure that set1 is a jump so that
4061 we do not create invalid RTL. */
4062 && (!JUMP_P (i3) || SET_DEST (set1) == pc_rtx)
4065 newi2pat = set0;
4066 newpat = set1;
4068 else
4070 undo_all ();
4071 return 0;
4074 i2_code_number = recog_for_combine (&newi2pat, i2, &new_i2_notes);
4076 if (i2_code_number >= 0)
4078 /* recog_for_combine might have added CLOBBERs to newi2pat.
4079 Make sure NEWPAT does not depend on the clobbered regs. */
4080 if (GET_CODE (newi2pat) == PARALLEL)
4082 for (i = XVECLEN (newi2pat, 0) - 1; i >= 0; i--)
4083 if (GET_CODE (XVECEXP (newi2pat, 0, i)) == CLOBBER)
4085 rtx reg = XEXP (XVECEXP (newi2pat, 0, i), 0);
4086 if (reg_overlap_mentioned_p (reg, newpat))
4088 undo_all ();
4089 return 0;
4094 insn_code_number = recog_for_combine (&newpat, i3, &new_i3_notes);
4096 if (insn_code_number >= 0)
4097 split_i2i3 = 1;
4101 /* If it still isn't recognized, fail and change things back the way they
4102 were. */
4103 if ((insn_code_number < 0
4104 /* Is the result a reasonable ASM_OPERANDS? */
4105 && (! check_asm_operands (newpat) || added_sets_1 || added_sets_2)))
4107 undo_all ();
4108 return 0;
4111 /* If we had to change another insn, make sure it is valid also. */
4112 if (undobuf.other_insn)
4114 CLEAR_HARD_REG_SET (newpat_used_regs);
4116 other_pat = PATTERN (undobuf.other_insn);
4117 other_code_number = recog_for_combine (&other_pat, undobuf.other_insn,
4118 &new_other_notes);
4120 if (other_code_number < 0 && ! check_asm_operands (other_pat))
4122 undo_all ();
4123 return 0;
4127 /* If I2 is the CC0 setter and I3 is the CC0 user then check whether
4128 they are adjacent to each other or not. */
4129 if (HAVE_cc0)
4131 rtx_insn *p = prev_nonnote_insn (i3);
4132 if (p && p != i2 && NONJUMP_INSN_P (p) && newi2pat
4133 && sets_cc0_p (newi2pat))
4135 undo_all ();
4136 return 0;
4140 /* Only allow this combination if insn_cost reports that the
4141 replacement instructions are cheaper than the originals. */
4142 if (!combine_validate_cost (i0, i1, i2, i3, newpat, newi2pat, other_pat))
4144 undo_all ();
4145 return 0;
4148 if (MAY_HAVE_DEBUG_BIND_INSNS)
4150 struct undo *undo;
4152 for (undo = undobuf.undos; undo; undo = undo->next)
4153 if (undo->kind == UNDO_MODE)
4155 rtx reg = *undo->where.r;
4156 machine_mode new_mode = GET_MODE (reg);
4157 machine_mode old_mode = undo->old_contents.m;
4159 /* Temporarily revert mode back. */
4160 adjust_reg_mode (reg, old_mode);
4162 if (reg == i2dest && i2scratch)
4164 /* If we used i2dest as a scratch register with a
4165 different mode, substitute it for the original
4166 i2src while its original mode is temporarily
4167 restored, and then clear i2scratch so that we don't
4168 do it again later. */
4169 propagate_for_debug (i2, last_combined_insn, reg, i2src,
4170 this_basic_block);
4171 i2scratch = false;
4172 /* Put back the new mode. */
4173 adjust_reg_mode (reg, new_mode);
4175 else
4177 rtx tempreg = gen_raw_REG (old_mode, REGNO (reg));
4178 rtx_insn *first, *last;
4180 if (reg == i2dest)
4182 first = i2;
4183 last = last_combined_insn;
4185 else
4187 first = i3;
4188 last = undobuf.other_insn;
4189 gcc_assert (last);
4190 if (DF_INSN_LUID (last)
4191 < DF_INSN_LUID (last_combined_insn))
4192 last = last_combined_insn;
4195 /* We're dealing with a reg that changed mode but not
4196 meaning, so we want to turn it into a subreg for
4197 the new mode. However, because of REG sharing and
4198 because its mode had already changed, we have to do
4199 it in two steps. First, replace any debug uses of
4200 reg, with its original mode temporarily restored,
4201 with this copy we have created; then, replace the
4202 copy with the SUBREG of the original shared reg,
4203 once again changed to the new mode. */
4204 propagate_for_debug (first, last, reg, tempreg,
4205 this_basic_block);
4206 adjust_reg_mode (reg, new_mode);
4207 propagate_for_debug (first, last, tempreg,
4208 lowpart_subreg (old_mode, reg, new_mode),
4209 this_basic_block);
4214 /* If we will be able to accept this, we have made a
4215 change to the destination of I3. This requires us to
4216 do a few adjustments. */
4218 if (changed_i3_dest)
4220 PATTERN (i3) = newpat;
4221 adjust_for_new_dest (i3);
4224 /* We now know that we can do this combination. Merge the insns and
4225 update the status of registers and LOG_LINKS. */
4227 if (undobuf.other_insn)
4229 rtx note, next;
4231 PATTERN (undobuf.other_insn) = other_pat;
4233 /* If any of the notes in OTHER_INSN were REG_DEAD or REG_UNUSED,
4234 ensure that they are still valid. Then add any non-duplicate
4235 notes added by recog_for_combine. */
4236 for (note = REG_NOTES (undobuf.other_insn); note; note = next)
4238 next = XEXP (note, 1);
4240 if ((REG_NOTE_KIND (note) == REG_DEAD
4241 && !reg_referenced_p (XEXP (note, 0),
4242 PATTERN (undobuf.other_insn)))
4243 ||(REG_NOTE_KIND (note) == REG_UNUSED
4244 && !reg_set_p (XEXP (note, 0),
4245 PATTERN (undobuf.other_insn)))
4246 /* Simply drop equal note since it may be no longer valid
4247 for other_insn. It may be possible to record that CC
4248 register is changed and only discard those notes, but
4249 in practice it's unnecessary complication and doesn't
4250 give any meaningful improvement.
4252 See PR78559. */
4253 || REG_NOTE_KIND (note) == REG_EQUAL
4254 || REG_NOTE_KIND (note) == REG_EQUIV)
4255 remove_note (undobuf.other_insn, note);
4258 distribute_notes (new_other_notes, undobuf.other_insn,
4259 undobuf.other_insn, NULL, NULL_RTX, NULL_RTX,
4260 NULL_RTX);
4263 if (swap_i2i3)
4265 /* I3 now uses what used to be its destination and which is now
4266 I2's destination. This requires us to do a few adjustments. */
4267 PATTERN (i3) = newpat;
4268 adjust_for_new_dest (i3);
4271 if (swap_i2i3 || split_i2i3)
4273 /* We might need a LOG_LINK from I3 to I2. But then we used to
4274 have one, so we still will.
4276 However, some later insn might be using I2's dest and have
4277 a LOG_LINK pointing at I3. We should change it to point at
4278 I2 instead. */
4280 /* newi2pat is usually a SET here; however, recog_for_combine might
4281 have added some clobbers. */
4282 rtx x = newi2pat;
4283 if (GET_CODE (x) == PARALLEL)
4284 x = XVECEXP (newi2pat, 0, 0);
4286 /* It can only be a SET of a REG or of a SUBREG of a REG. */
4287 unsigned int regno = reg_or_subregno (SET_DEST (x));
4289 bool done = false;
4290 for (rtx_insn *insn = NEXT_INSN (i3);
4291 !done
4292 && insn
4293 && NONDEBUG_INSN_P (insn)
4294 && BLOCK_FOR_INSN (insn) == this_basic_block;
4295 insn = NEXT_INSN (insn))
4297 struct insn_link *link;
4298 FOR_EACH_LOG_LINK (link, insn)
4299 if (link->insn == i3 && link->regno == regno)
4301 link->insn = i2;
4302 done = true;
4303 break;
4309 rtx i3notes, i2notes, i1notes = 0, i0notes = 0;
4310 struct insn_link *i3links, *i2links, *i1links = 0, *i0links = 0;
4311 rtx midnotes = 0;
4312 int from_luid;
4313 /* Compute which registers we expect to eliminate. newi2pat may be setting
4314 either i3dest or i2dest, so we must check it. */
4315 rtx elim_i2 = ((newi2pat && reg_set_p (i2dest, newi2pat))
4316 || i2dest_in_i2src || i2dest_in_i1src || i2dest_in_i0src
4317 || !i2dest_killed
4318 ? 0 : i2dest);
4319 /* For i1, we need to compute both local elimination and global
4320 elimination information with respect to newi2pat because i1dest
4321 may be the same as i3dest, in which case newi2pat may be setting
4322 i1dest. Global information is used when distributing REG_DEAD
4323 note for i2 and i3, in which case it does matter if newi2pat sets
4324 i1dest or not.
4326 Local information is used when distributing REG_DEAD note for i1,
4327 in which case it doesn't matter if newi2pat sets i1dest or not.
4328 See PR62151, if we have four insns combination:
4329 i0: r0 <- i0src
4330 i1: r1 <- i1src (using r0)
4331 REG_DEAD (r0)
4332 i2: r0 <- i2src (using r1)
4333 i3: r3 <- i3src (using r0)
4334 ix: using r0
4335 From i1's point of view, r0 is eliminated, no matter if it is set
4336 by newi2pat or not. In other words, REG_DEAD info for r0 in i1
4337 should be discarded.
4339 Note local information only affects cases in forms like "I1->I2->I3",
4340 "I0->I1->I2->I3" or "I0&I1->I2, I2->I3". For other cases like
4341 "I0->I1, I1&I2->I3" or "I1&I2->I3", newi2pat won't set i1dest or
4342 i0dest anyway. */
4343 rtx local_elim_i1 = (i1 == 0 || i1dest_in_i1src || i1dest_in_i0src
4344 || !i1dest_killed
4345 ? 0 : i1dest);
4346 rtx elim_i1 = (local_elim_i1 == 0
4347 || (newi2pat && reg_set_p (i1dest, newi2pat))
4348 ? 0 : i1dest);
4349 /* Same case as i1. */
4350 rtx local_elim_i0 = (i0 == 0 || i0dest_in_i0src || !i0dest_killed
4351 ? 0 : i0dest);
4352 rtx elim_i0 = (local_elim_i0 == 0
4353 || (newi2pat && reg_set_p (i0dest, newi2pat))
4354 ? 0 : i0dest);
4356 /* Get the old REG_NOTES and LOG_LINKS from all our insns and
4357 clear them. */
4358 i3notes = REG_NOTES (i3), i3links = LOG_LINKS (i3);
4359 i2notes = REG_NOTES (i2), i2links = LOG_LINKS (i2);
4360 if (i1)
4361 i1notes = REG_NOTES (i1), i1links = LOG_LINKS (i1);
4362 if (i0)
4363 i0notes = REG_NOTES (i0), i0links = LOG_LINKS (i0);
4365 /* Ensure that we do not have something that should not be shared but
4366 occurs multiple times in the new insns. Check this by first
4367 resetting all the `used' flags and then copying anything is shared. */
4369 reset_used_flags (i3notes);
4370 reset_used_flags (i2notes);
4371 reset_used_flags (i1notes);
4372 reset_used_flags (i0notes);
4373 reset_used_flags (newpat);
4374 reset_used_flags (newi2pat);
4375 if (undobuf.other_insn)
4376 reset_used_flags (PATTERN (undobuf.other_insn));
4378 i3notes = copy_rtx_if_shared (i3notes);
4379 i2notes = copy_rtx_if_shared (i2notes);
4380 i1notes = copy_rtx_if_shared (i1notes);
4381 i0notes = copy_rtx_if_shared (i0notes);
4382 newpat = copy_rtx_if_shared (newpat);
4383 newi2pat = copy_rtx_if_shared (newi2pat);
4384 if (undobuf.other_insn)
4385 reset_used_flags (PATTERN (undobuf.other_insn));
4387 INSN_CODE (i3) = insn_code_number;
4388 PATTERN (i3) = newpat;
4390 if (CALL_P (i3) && CALL_INSN_FUNCTION_USAGE (i3))
4392 for (rtx link = CALL_INSN_FUNCTION_USAGE (i3); link;
4393 link = XEXP (link, 1))
4395 if (substed_i2)
4397 /* I2SRC must still be meaningful at this point. Some
4398 splitting operations can invalidate I2SRC, but those
4399 operations do not apply to calls. */
4400 gcc_assert (i2src);
4401 XEXP (link, 0) = simplify_replace_rtx (XEXP (link, 0),
4402 i2dest, i2src);
4404 if (substed_i1)
4405 XEXP (link, 0) = simplify_replace_rtx (XEXP (link, 0),
4406 i1dest, i1src);
4407 if (substed_i0)
4408 XEXP (link, 0) = simplify_replace_rtx (XEXP (link, 0),
4409 i0dest, i0src);
4413 if (undobuf.other_insn)
4414 INSN_CODE (undobuf.other_insn) = other_code_number;
4416 /* We had one special case above where I2 had more than one set and
4417 we replaced a destination of one of those sets with the destination
4418 of I3. In that case, we have to update LOG_LINKS of insns later
4419 in this basic block. Note that this (expensive) case is rare.
4421 Also, in this case, we must pretend that all REG_NOTEs for I2
4422 actually came from I3, so that REG_UNUSED notes from I2 will be
4423 properly handled. */
4425 if (i3_subst_into_i2)
4427 for (i = 0; i < XVECLEN (PATTERN (i2), 0); i++)
4428 if ((GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == SET
4429 || GET_CODE (XVECEXP (PATTERN (i2), 0, i)) == CLOBBER)
4430 && REG_P (SET_DEST (XVECEXP (PATTERN (i2), 0, i)))
4431 && SET_DEST (XVECEXP (PATTERN (i2), 0, i)) != i2dest
4432 && ! find_reg_note (i2, REG_UNUSED,
4433 SET_DEST (XVECEXP (PATTERN (i2), 0, i))))
4434 for (temp_insn = NEXT_INSN (i2);
4435 temp_insn
4436 && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
4437 || BB_HEAD (this_basic_block) != temp_insn);
4438 temp_insn = NEXT_INSN (temp_insn))
4439 if (temp_insn != i3 && NONDEBUG_INSN_P (temp_insn))
4440 FOR_EACH_LOG_LINK (link, temp_insn)
4441 if (link->insn == i2)
4442 link->insn = i3;
4444 if (i3notes)
4446 rtx link = i3notes;
4447 while (XEXP (link, 1))
4448 link = XEXP (link, 1);
4449 XEXP (link, 1) = i2notes;
4451 else
4452 i3notes = i2notes;
4453 i2notes = 0;
4456 LOG_LINKS (i3) = NULL;
4457 REG_NOTES (i3) = 0;
4458 LOG_LINKS (i2) = NULL;
4459 REG_NOTES (i2) = 0;
4461 if (newi2pat)
4463 if (MAY_HAVE_DEBUG_BIND_INSNS && i2scratch)
4464 propagate_for_debug (i2, last_combined_insn, i2dest, i2src,
4465 this_basic_block);
4466 INSN_CODE (i2) = i2_code_number;
4467 PATTERN (i2) = newi2pat;
4469 else
4471 if (MAY_HAVE_DEBUG_BIND_INSNS && i2src)
4472 propagate_for_debug (i2, last_combined_insn, i2dest, i2src,
4473 this_basic_block);
4474 SET_INSN_DELETED (i2);
4477 if (i1)
4479 LOG_LINKS (i1) = NULL;
4480 REG_NOTES (i1) = 0;
4481 if (MAY_HAVE_DEBUG_BIND_INSNS)
4482 propagate_for_debug (i1, last_combined_insn, i1dest, i1src,
4483 this_basic_block);
4484 SET_INSN_DELETED (i1);
4487 if (i0)
4489 LOG_LINKS (i0) = NULL;
4490 REG_NOTES (i0) = 0;
4491 if (MAY_HAVE_DEBUG_BIND_INSNS)
4492 propagate_for_debug (i0, last_combined_insn, i0dest, i0src,
4493 this_basic_block);
4494 SET_INSN_DELETED (i0);
4497 /* Get death notes for everything that is now used in either I3 or
4498 I2 and used to die in a previous insn. If we built two new
4499 patterns, move from I1 to I2 then I2 to I3 so that we get the
4500 proper movement on registers that I2 modifies. */
4502 if (i0)
4503 from_luid = DF_INSN_LUID (i0);
4504 else if (i1)
4505 from_luid = DF_INSN_LUID (i1);
4506 else
4507 from_luid = DF_INSN_LUID (i2);
4508 if (newi2pat)
4509 move_deaths (newi2pat, NULL_RTX, from_luid, i2, &midnotes);
4510 move_deaths (newpat, newi2pat, from_luid, i3, &midnotes);
4512 /* Distribute all the LOG_LINKS and REG_NOTES from I1, I2, and I3. */
4513 if (i3notes)
4514 distribute_notes (i3notes, i3, i3, newi2pat ? i2 : NULL,
4515 elim_i2, elim_i1, elim_i0);
4516 if (i2notes)
4517 distribute_notes (i2notes, i2, i3, newi2pat ? i2 : NULL,
4518 elim_i2, elim_i1, elim_i0);
4519 if (i1notes)
4520 distribute_notes (i1notes, i1, i3, newi2pat ? i2 : NULL,
4521 elim_i2, local_elim_i1, local_elim_i0);
4522 if (i0notes)
4523 distribute_notes (i0notes, i0, i3, newi2pat ? i2 : NULL,
4524 elim_i2, elim_i1, local_elim_i0);
4525 if (midnotes)
4526 distribute_notes (midnotes, NULL, i3, newi2pat ? i2 : NULL,
4527 elim_i2, elim_i1, elim_i0);
4529 /* Distribute any notes added to I2 or I3 by recog_for_combine. We
4530 know these are REG_UNUSED and want them to go to the desired insn,
4531 so we always pass it as i3. */
4533 if (newi2pat && new_i2_notes)
4534 distribute_notes (new_i2_notes, i2, i2, NULL, NULL_RTX, NULL_RTX,
4535 NULL_RTX);
4537 if (new_i3_notes)
4538 distribute_notes (new_i3_notes, i3, i3, NULL, NULL_RTX, NULL_RTX,
4539 NULL_RTX);
4541 /* If I3DEST was used in I3SRC, it really died in I3. We may need to
4542 put a REG_DEAD note for it somewhere. If NEWI2PAT exists and sets
4543 I3DEST, the death must be somewhere before I2, not I3. If we passed I3
4544 in that case, it might delete I2. Similarly for I2 and I1.
4545 Show an additional death due to the REG_DEAD note we make here. If
4546 we discard it in distribute_notes, we will decrement it again. */
4548 if (i3dest_killed)
4550 rtx new_note = alloc_reg_note (REG_DEAD, i3dest_killed, NULL_RTX);
4551 if (newi2pat && reg_set_p (i3dest_killed, newi2pat))
4552 distribute_notes (new_note, NULL, i2, NULL, elim_i2,
4553 elim_i1, elim_i0);
4554 else
4555 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4556 elim_i2, elim_i1, elim_i0);
4559 if (i2dest_in_i2src)
4561 rtx new_note = alloc_reg_note (REG_DEAD, i2dest, NULL_RTX);
4562 if (newi2pat && reg_set_p (i2dest, newi2pat))
4563 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4564 NULL_RTX, NULL_RTX);
4565 else
4566 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4567 NULL_RTX, NULL_RTX, NULL_RTX);
4570 if (i1dest_in_i1src)
4572 rtx new_note = alloc_reg_note (REG_DEAD, i1dest, NULL_RTX);
4573 if (newi2pat && reg_set_p (i1dest, newi2pat))
4574 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4575 NULL_RTX, NULL_RTX);
4576 else
4577 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4578 NULL_RTX, NULL_RTX, NULL_RTX);
4581 if (i0dest_in_i0src)
4583 rtx new_note = alloc_reg_note (REG_DEAD, i0dest, NULL_RTX);
4584 if (newi2pat && reg_set_p (i0dest, newi2pat))
4585 distribute_notes (new_note, NULL, i2, NULL, NULL_RTX,
4586 NULL_RTX, NULL_RTX);
4587 else
4588 distribute_notes (new_note, NULL, i3, newi2pat ? i2 : NULL,
4589 NULL_RTX, NULL_RTX, NULL_RTX);
4592 distribute_links (i3links);
4593 distribute_links (i2links);
4594 distribute_links (i1links);
4595 distribute_links (i0links);
4597 if (REG_P (i2dest))
4599 struct insn_link *link;
4600 rtx_insn *i2_insn = 0;
4601 rtx i2_val = 0, set;
4603 /* The insn that used to set this register doesn't exist, and
4604 this life of the register may not exist either. See if one of
4605 I3's links points to an insn that sets I2DEST. If it does,
4606 that is now the last known value for I2DEST. If we don't update
4607 this and I2 set the register to a value that depended on its old
4608 contents, we will get confused. If this insn is used, thing
4609 will be set correctly in combine_instructions. */
4610 FOR_EACH_LOG_LINK (link, i3)
4611 if ((set = single_set (link->insn)) != 0
4612 && rtx_equal_p (i2dest, SET_DEST (set)))
4613 i2_insn = link->insn, i2_val = SET_SRC (set);
4615 record_value_for_reg (i2dest, i2_insn, i2_val);
4617 /* If the reg formerly set in I2 died only once and that was in I3,
4618 zero its use count so it won't make `reload' do any work. */
4619 if (! added_sets_2
4620 && (newi2pat == 0 || ! reg_mentioned_p (i2dest, newi2pat))
4621 && ! i2dest_in_i2src
4622 && REGNO (i2dest) < reg_n_sets_max)
4623 INC_REG_N_SETS (REGNO (i2dest), -1);
4626 if (i1 && REG_P (i1dest))
4628 struct insn_link *link;
4629 rtx_insn *i1_insn = 0;
4630 rtx i1_val = 0, set;
4632 FOR_EACH_LOG_LINK (link, i3)
4633 if ((set = single_set (link->insn)) != 0
4634 && rtx_equal_p (i1dest, SET_DEST (set)))
4635 i1_insn = link->insn, i1_val = SET_SRC (set);
4637 record_value_for_reg (i1dest, i1_insn, i1_val);
4639 if (! added_sets_1
4640 && ! i1dest_in_i1src
4641 && REGNO (i1dest) < reg_n_sets_max)
4642 INC_REG_N_SETS (REGNO (i1dest), -1);
4645 if (i0 && REG_P (i0dest))
4647 struct insn_link *link;
4648 rtx_insn *i0_insn = 0;
4649 rtx i0_val = 0, set;
4651 FOR_EACH_LOG_LINK (link, i3)
4652 if ((set = single_set (link->insn)) != 0
4653 && rtx_equal_p (i0dest, SET_DEST (set)))
4654 i0_insn = link->insn, i0_val = SET_SRC (set);
4656 record_value_for_reg (i0dest, i0_insn, i0_val);
4658 if (! added_sets_0
4659 && ! i0dest_in_i0src
4660 && REGNO (i0dest) < reg_n_sets_max)
4661 INC_REG_N_SETS (REGNO (i0dest), -1);
4664 /* Update reg_stat[].nonzero_bits et al for any changes that may have
4665 been made to this insn. The order is important, because newi2pat
4666 can affect nonzero_bits of newpat. */
4667 if (newi2pat)
4668 note_stores (newi2pat, set_nonzero_bits_and_sign_copies, NULL);
4669 note_stores (newpat, set_nonzero_bits_and_sign_copies, NULL);
4672 if (undobuf.other_insn != NULL_RTX)
4674 if (dump_file)
4676 fprintf (dump_file, "modifying other_insn ");
4677 dump_insn_slim (dump_file, undobuf.other_insn);
4679 df_insn_rescan (undobuf.other_insn);
4682 if (i0 && !(NOTE_P (i0) && (NOTE_KIND (i0) == NOTE_INSN_DELETED)))
4684 if (dump_file)
4686 fprintf (dump_file, "modifying insn i0 ");
4687 dump_insn_slim (dump_file, i0);
4689 df_insn_rescan (i0);
4692 if (i1 && !(NOTE_P (i1) && (NOTE_KIND (i1) == NOTE_INSN_DELETED)))
4694 if (dump_file)
4696 fprintf (dump_file, "modifying insn i1 ");
4697 dump_insn_slim (dump_file, i1);
4699 df_insn_rescan (i1);
4702 if (i2 && !(NOTE_P (i2) && (NOTE_KIND (i2) == NOTE_INSN_DELETED)))
4704 if (dump_file)
4706 fprintf (dump_file, "modifying insn i2 ");
4707 dump_insn_slim (dump_file, i2);
4709 df_insn_rescan (i2);
4712 if (i3 && !(NOTE_P (i3) && (NOTE_KIND (i3) == NOTE_INSN_DELETED)))
4714 if (dump_file)
4716 fprintf (dump_file, "modifying insn i3 ");
4717 dump_insn_slim (dump_file, i3);
4719 df_insn_rescan (i3);
4722 /* Set new_direct_jump_p if a new return or simple jump instruction
4723 has been created. Adjust the CFG accordingly. */
4724 if (returnjump_p (i3) || any_uncondjump_p (i3))
4726 *new_direct_jump_p = 1;
4727 mark_jump_label (PATTERN (i3), i3, 0);
4728 update_cfg_for_uncondjump (i3);
4731 if (undobuf.other_insn != NULL_RTX
4732 && (returnjump_p (undobuf.other_insn)
4733 || any_uncondjump_p (undobuf.other_insn)))
4735 *new_direct_jump_p = 1;
4736 update_cfg_for_uncondjump (undobuf.other_insn);
4739 if (GET_CODE (PATTERN (i3)) == TRAP_IF
4740 && XEXP (PATTERN (i3), 0) == const1_rtx)
4742 basic_block bb = BLOCK_FOR_INSN (i3);
4743 gcc_assert (bb);
4744 remove_edge (split_block (bb, i3));
4745 emit_barrier_after_bb (bb);
4746 *new_direct_jump_p = 1;
4749 if (undobuf.other_insn
4750 && GET_CODE (PATTERN (undobuf.other_insn)) == TRAP_IF
4751 && XEXP (PATTERN (undobuf.other_insn), 0) == const1_rtx)
4753 basic_block bb = BLOCK_FOR_INSN (undobuf.other_insn);
4754 gcc_assert (bb);
4755 remove_edge (split_block (bb, undobuf.other_insn));
4756 emit_barrier_after_bb (bb);
4757 *new_direct_jump_p = 1;
4760 /* A noop might also need cleaning up of CFG, if it comes from the
4761 simplification of a jump. */
4762 if (JUMP_P (i3)
4763 && GET_CODE (newpat) == SET
4764 && SET_SRC (newpat) == pc_rtx
4765 && SET_DEST (newpat) == pc_rtx)
4767 *new_direct_jump_p = 1;
4768 update_cfg_for_uncondjump (i3);
4771 if (undobuf.other_insn != NULL_RTX
4772 && JUMP_P (undobuf.other_insn)
4773 && GET_CODE (PATTERN (undobuf.other_insn)) == SET
4774 && SET_SRC (PATTERN (undobuf.other_insn)) == pc_rtx
4775 && SET_DEST (PATTERN (undobuf.other_insn)) == pc_rtx)
4777 *new_direct_jump_p = 1;
4778 update_cfg_for_uncondjump (undobuf.other_insn);
4781 combine_successes++;
4782 undo_commit ();
4784 rtx_insn *ret = newi2pat ? i2 : i3;
4785 if (added_links_insn && DF_INSN_LUID (added_links_insn) < DF_INSN_LUID (ret))
4786 ret = added_links_insn;
4787 if (added_notes_insn && DF_INSN_LUID (added_notes_insn) < DF_INSN_LUID (ret))
4788 ret = added_notes_insn;
4790 return ret;
4793 /* Get a marker for undoing to the current state. */
4795 static void *
4796 get_undo_marker (void)
4798 return undobuf.undos;
4801 /* Undo the modifications up to the marker. */
4803 static void
4804 undo_to_marker (void *marker)
4806 struct undo *undo, *next;
4808 for (undo = undobuf.undos; undo != marker; undo = next)
4810 gcc_assert (undo);
4812 next = undo->next;
4813 switch (undo->kind)
4815 case UNDO_RTX:
4816 *undo->where.r = undo->old_contents.r;
4817 break;
4818 case UNDO_INT:
4819 *undo->where.i = undo->old_contents.i;
4820 break;
4821 case UNDO_MODE:
4822 adjust_reg_mode (*undo->where.r, undo->old_contents.m);
4823 break;
4824 case UNDO_LINKS:
4825 *undo->where.l = undo->old_contents.l;
4826 break;
4827 default:
4828 gcc_unreachable ();
4831 undo->next = undobuf.frees;
4832 undobuf.frees = undo;
4835 undobuf.undos = (struct undo *) marker;
4838 /* Undo all the modifications recorded in undobuf. */
4840 static void
4841 undo_all (void)
4843 undo_to_marker (0);
4846 /* We've committed to accepting the changes we made. Move all
4847 of the undos to the free list. */
4849 static void
4850 undo_commit (void)
4852 struct undo *undo, *next;
4854 for (undo = undobuf.undos; undo; undo = next)
4856 next = undo->next;
4857 undo->next = undobuf.frees;
4858 undobuf.frees = undo;
4860 undobuf.undos = 0;
4863 /* Find the innermost point within the rtx at LOC, possibly LOC itself,
4864 where we have an arithmetic expression and return that point. LOC will
4865 be inside INSN.
4867 try_combine will call this function to see if an insn can be split into
4868 two insns. */
4870 static rtx *
4871 find_split_point (rtx *loc, rtx_insn *insn, bool set_src)
4873 rtx x = *loc;
4874 enum rtx_code code = GET_CODE (x);
4875 rtx *split;
4876 unsigned HOST_WIDE_INT len = 0;
4877 HOST_WIDE_INT pos = 0;
4878 int unsignedp = 0;
4879 rtx inner = NULL_RTX;
4880 scalar_int_mode mode, inner_mode;
4882 /* First special-case some codes. */
4883 switch (code)
4885 case SUBREG:
4886 #ifdef INSN_SCHEDULING
4887 /* If we are making a paradoxical SUBREG invalid, it becomes a split
4888 point. */
4889 if (MEM_P (SUBREG_REG (x)))
4890 return loc;
4891 #endif
4892 return find_split_point (&SUBREG_REG (x), insn, false);
4894 case MEM:
4895 /* If we have (mem (const ..)) or (mem (symbol_ref ...)), split it
4896 using LO_SUM and HIGH. */
4897 if (HAVE_lo_sum && (GET_CODE (XEXP (x, 0)) == CONST
4898 || GET_CODE (XEXP (x, 0)) == SYMBOL_REF))
4900 machine_mode address_mode = get_address_mode (x);
4902 SUBST (XEXP (x, 0),
4903 gen_rtx_LO_SUM (address_mode,
4904 gen_rtx_HIGH (address_mode, XEXP (x, 0)),
4905 XEXP (x, 0)));
4906 return &XEXP (XEXP (x, 0), 0);
4909 /* If we have a PLUS whose second operand is a constant and the
4910 address is not valid, perhaps will can split it up using
4911 the machine-specific way to split large constants. We use
4912 the first pseudo-reg (one of the virtual regs) as a placeholder;
4913 it will not remain in the result. */
4914 if (GET_CODE (XEXP (x, 0)) == PLUS
4915 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
4916 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4917 MEM_ADDR_SPACE (x)))
4919 rtx reg = regno_reg_rtx[FIRST_PSEUDO_REGISTER];
4920 rtx_insn *seq = combine_split_insns (gen_rtx_SET (reg, XEXP (x, 0)),
4921 subst_insn);
4923 /* This should have produced two insns, each of which sets our
4924 placeholder. If the source of the second is a valid address,
4925 we can make put both sources together and make a split point
4926 in the middle. */
4928 if (seq
4929 && NEXT_INSN (seq) != NULL_RTX
4930 && NEXT_INSN (NEXT_INSN (seq)) == NULL_RTX
4931 && NONJUMP_INSN_P (seq)
4932 && GET_CODE (PATTERN (seq)) == SET
4933 && SET_DEST (PATTERN (seq)) == reg
4934 && ! reg_mentioned_p (reg,
4935 SET_SRC (PATTERN (seq)))
4936 && NONJUMP_INSN_P (NEXT_INSN (seq))
4937 && GET_CODE (PATTERN (NEXT_INSN (seq))) == SET
4938 && SET_DEST (PATTERN (NEXT_INSN (seq))) == reg
4939 && memory_address_addr_space_p
4940 (GET_MODE (x), SET_SRC (PATTERN (NEXT_INSN (seq))),
4941 MEM_ADDR_SPACE (x)))
4943 rtx src1 = SET_SRC (PATTERN (seq));
4944 rtx src2 = SET_SRC (PATTERN (NEXT_INSN (seq)));
4946 /* Replace the placeholder in SRC2 with SRC1. If we can
4947 find where in SRC2 it was placed, that can become our
4948 split point and we can replace this address with SRC2.
4949 Just try two obvious places. */
4951 src2 = replace_rtx (src2, reg, src1);
4952 split = 0;
4953 if (XEXP (src2, 0) == src1)
4954 split = &XEXP (src2, 0);
4955 else if (GET_RTX_FORMAT (GET_CODE (XEXP (src2, 0)))[0] == 'e'
4956 && XEXP (XEXP (src2, 0), 0) == src1)
4957 split = &XEXP (XEXP (src2, 0), 0);
4959 if (split)
4961 SUBST (XEXP (x, 0), src2);
4962 return split;
4966 /* If that didn't work, perhaps the first operand is complex and
4967 needs to be computed separately, so make a split point there.
4968 This will occur on machines that just support REG + CONST
4969 and have a constant moved through some previous computation. */
4971 else if (!OBJECT_P (XEXP (XEXP (x, 0), 0))
4972 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4973 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4974 return &XEXP (XEXP (x, 0), 0);
4977 /* If we have a PLUS whose first operand is complex, try computing it
4978 separately by making a split there. */
4979 if (GET_CODE (XEXP (x, 0)) == PLUS
4980 && ! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
4981 MEM_ADDR_SPACE (x))
4982 && ! OBJECT_P (XEXP (XEXP (x, 0), 0))
4983 && ! (GET_CODE (XEXP (XEXP (x, 0), 0)) == SUBREG
4984 && OBJECT_P (SUBREG_REG (XEXP (XEXP (x, 0), 0)))))
4985 return &XEXP (XEXP (x, 0), 0);
4986 break;
4988 case SET:
4989 /* If SET_DEST is CC0 and SET_SRC is not an operand, a COMPARE, or a
4990 ZERO_EXTRACT, the most likely reason why this doesn't match is that
4991 we need to put the operand into a register. So split at that
4992 point. */
4994 if (SET_DEST (x) == cc0_rtx
4995 && GET_CODE (SET_SRC (x)) != COMPARE
4996 && GET_CODE (SET_SRC (x)) != ZERO_EXTRACT
4997 && !OBJECT_P (SET_SRC (x))
4998 && ! (GET_CODE (SET_SRC (x)) == SUBREG
4999 && OBJECT_P (SUBREG_REG (SET_SRC (x)))))
5000 return &SET_SRC (x);
5002 /* See if we can split SET_SRC as it stands. */
5003 split = find_split_point (&SET_SRC (x), insn, true);
5004 if (split && split != &SET_SRC (x))
5005 return split;
5007 /* See if we can split SET_DEST as it stands. */
5008 split = find_split_point (&SET_DEST (x), insn, false);
5009 if (split && split != &SET_DEST (x))
5010 return split;
5012 /* See if this is a bitfield assignment with everything constant. If
5013 so, this is an IOR of an AND, so split it into that. */
5014 if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
5015 && is_a <scalar_int_mode> (GET_MODE (XEXP (SET_DEST (x), 0)),
5016 &inner_mode)
5017 && HWI_COMPUTABLE_MODE_P (inner_mode)
5018 && CONST_INT_P (XEXP (SET_DEST (x), 1))
5019 && CONST_INT_P (XEXP (SET_DEST (x), 2))
5020 && CONST_INT_P (SET_SRC (x))
5021 && ((INTVAL (XEXP (SET_DEST (x), 1))
5022 + INTVAL (XEXP (SET_DEST (x), 2)))
5023 <= GET_MODE_PRECISION (inner_mode))
5024 && ! side_effects_p (XEXP (SET_DEST (x), 0)))
5026 HOST_WIDE_INT pos = INTVAL (XEXP (SET_DEST (x), 2));
5027 unsigned HOST_WIDE_INT len = INTVAL (XEXP (SET_DEST (x), 1));
5028 unsigned HOST_WIDE_INT src = INTVAL (SET_SRC (x));
5029 rtx dest = XEXP (SET_DEST (x), 0);
5030 unsigned HOST_WIDE_INT mask
5031 = (HOST_WIDE_INT_1U << len) - 1;
5032 rtx or_mask;
5034 if (BITS_BIG_ENDIAN)
5035 pos = GET_MODE_PRECISION (inner_mode) - len - pos;
5037 or_mask = gen_int_mode (src << pos, inner_mode);
5038 if (src == mask)
5039 SUBST (SET_SRC (x),
5040 simplify_gen_binary (IOR, inner_mode, dest, or_mask));
5041 else
5043 rtx negmask = gen_int_mode (~(mask << pos), inner_mode);
5044 SUBST (SET_SRC (x),
5045 simplify_gen_binary (IOR, inner_mode,
5046 simplify_gen_binary (AND, inner_mode,
5047 dest, negmask),
5048 or_mask));
5051 SUBST (SET_DEST (x), dest);
5053 split = find_split_point (&SET_SRC (x), insn, true);
5054 if (split && split != &SET_SRC (x))
5055 return split;
5058 /* Otherwise, see if this is an operation that we can split into two.
5059 If so, try to split that. */
5060 code = GET_CODE (SET_SRC (x));
5062 switch (code)
5064 case AND:
5065 /* If we are AND'ing with a large constant that is only a single
5066 bit and the result is only being used in a context where we
5067 need to know if it is zero or nonzero, replace it with a bit
5068 extraction. This will avoid the large constant, which might
5069 have taken more than one insn to make. If the constant were
5070 not a valid argument to the AND but took only one insn to make,
5071 this is no worse, but if it took more than one insn, it will
5072 be better. */
5074 if (CONST_INT_P (XEXP (SET_SRC (x), 1))
5075 && REG_P (XEXP (SET_SRC (x), 0))
5076 && (pos = exact_log2 (UINTVAL (XEXP (SET_SRC (x), 1)))) >= 7
5077 && REG_P (SET_DEST (x))
5078 && (split = find_single_use (SET_DEST (x), insn, NULL)) != 0
5079 && (GET_CODE (*split) == EQ || GET_CODE (*split) == NE)
5080 && XEXP (*split, 0) == SET_DEST (x)
5081 && XEXP (*split, 1) == const0_rtx)
5083 rtx extraction = make_extraction (GET_MODE (SET_DEST (x)),
5084 XEXP (SET_SRC (x), 0),
5085 pos, NULL_RTX, 1, 1, 0, 0);
5086 if (extraction != 0)
5088 SUBST (SET_SRC (x), extraction);
5089 return find_split_point (loc, insn, false);
5092 break;
5094 case NE:
5095 /* If STORE_FLAG_VALUE is -1, this is (NE X 0) and only one bit of X
5096 is known to be on, this can be converted into a NEG of a shift. */
5097 if (STORE_FLAG_VALUE == -1 && XEXP (SET_SRC (x), 1) == const0_rtx
5098 && GET_MODE (SET_SRC (x)) == GET_MODE (XEXP (SET_SRC (x), 0))
5099 && ((pos = exact_log2 (nonzero_bits (XEXP (SET_SRC (x), 0),
5100 GET_MODE (XEXP (SET_SRC (x),
5101 0))))) >= 1))
5103 machine_mode mode = GET_MODE (XEXP (SET_SRC (x), 0));
5104 rtx pos_rtx = gen_int_shift_amount (mode, pos);
5105 SUBST (SET_SRC (x),
5106 gen_rtx_NEG (mode,
5107 gen_rtx_LSHIFTRT (mode,
5108 XEXP (SET_SRC (x), 0),
5109 pos_rtx)));
5111 split = find_split_point (&SET_SRC (x), insn, true);
5112 if (split && split != &SET_SRC (x))
5113 return split;
5115 break;
5117 case SIGN_EXTEND:
5118 inner = XEXP (SET_SRC (x), 0);
5120 /* We can't optimize if either mode is a partial integer
5121 mode as we don't know how many bits are significant
5122 in those modes. */
5123 if (!is_int_mode (GET_MODE (inner), &inner_mode)
5124 || GET_MODE_CLASS (GET_MODE (SET_SRC (x))) == MODE_PARTIAL_INT)
5125 break;
5127 pos = 0;
5128 len = GET_MODE_PRECISION (inner_mode);
5129 unsignedp = 0;
5130 break;
5132 case SIGN_EXTRACT:
5133 case ZERO_EXTRACT:
5134 if (is_a <scalar_int_mode> (GET_MODE (XEXP (SET_SRC (x), 0)),
5135 &inner_mode)
5136 && CONST_INT_P (XEXP (SET_SRC (x), 1))
5137 && CONST_INT_P (XEXP (SET_SRC (x), 2)))
5139 inner = XEXP (SET_SRC (x), 0);
5140 len = INTVAL (XEXP (SET_SRC (x), 1));
5141 pos = INTVAL (XEXP (SET_SRC (x), 2));
5143 if (BITS_BIG_ENDIAN)
5144 pos = GET_MODE_PRECISION (inner_mode) - len - pos;
5145 unsignedp = (code == ZERO_EXTRACT);
5147 break;
5149 default:
5150 break;
5153 if (len
5154 && known_subrange_p (pos, len,
5155 0, GET_MODE_PRECISION (GET_MODE (inner)))
5156 && is_a <scalar_int_mode> (GET_MODE (SET_SRC (x)), &mode))
5158 /* For unsigned, we have a choice of a shift followed by an
5159 AND or two shifts. Use two shifts for field sizes where the
5160 constant might be too large. We assume here that we can
5161 always at least get 8-bit constants in an AND insn, which is
5162 true for every current RISC. */
5164 if (unsignedp && len <= 8)
5166 unsigned HOST_WIDE_INT mask
5167 = (HOST_WIDE_INT_1U << len) - 1;
5168 rtx pos_rtx = gen_int_shift_amount (mode, pos);
5169 SUBST (SET_SRC (x),
5170 gen_rtx_AND (mode,
5171 gen_rtx_LSHIFTRT
5172 (mode, gen_lowpart (mode, inner), pos_rtx),
5173 gen_int_mode (mask, mode)));
5175 split = find_split_point (&SET_SRC (x), insn, true);
5176 if (split && split != &SET_SRC (x))
5177 return split;
5179 else
5181 int left_bits = GET_MODE_PRECISION (mode) - len - pos;
5182 int right_bits = GET_MODE_PRECISION (mode) - len;
5183 SUBST (SET_SRC (x),
5184 gen_rtx_fmt_ee
5185 (unsignedp ? LSHIFTRT : ASHIFTRT, mode,
5186 gen_rtx_ASHIFT (mode,
5187 gen_lowpart (mode, inner),
5188 gen_int_shift_amount (mode, left_bits)),
5189 gen_int_shift_amount (mode, right_bits)));
5191 split = find_split_point (&SET_SRC (x), insn, true);
5192 if (split && split != &SET_SRC (x))
5193 return split;
5197 /* See if this is a simple operation with a constant as the second
5198 operand. It might be that this constant is out of range and hence
5199 could be used as a split point. */
5200 if (BINARY_P (SET_SRC (x))
5201 && CONSTANT_P (XEXP (SET_SRC (x), 1))
5202 && (OBJECT_P (XEXP (SET_SRC (x), 0))
5203 || (GET_CODE (XEXP (SET_SRC (x), 0)) == SUBREG
5204 && OBJECT_P (SUBREG_REG (XEXP (SET_SRC (x), 0))))))
5205 return &XEXP (SET_SRC (x), 1);
5207 /* Finally, see if this is a simple operation with its first operand
5208 not in a register. The operation might require this operand in a
5209 register, so return it as a split point. We can always do this
5210 because if the first operand were another operation, we would have
5211 already found it as a split point. */
5212 if ((BINARY_P (SET_SRC (x)) || UNARY_P (SET_SRC (x)))
5213 && ! register_operand (XEXP (SET_SRC (x), 0), VOIDmode))
5214 return &XEXP (SET_SRC (x), 0);
5216 return 0;
5218 case AND:
5219 case IOR:
5220 /* We write NOR as (and (not A) (not B)), but if we don't have a NOR,
5221 it is better to write this as (not (ior A B)) so we can split it.
5222 Similarly for IOR. */
5223 if (GET_CODE (XEXP (x, 0)) == NOT && GET_CODE (XEXP (x, 1)) == NOT)
5225 SUBST (*loc,
5226 gen_rtx_NOT (GET_MODE (x),
5227 gen_rtx_fmt_ee (code == IOR ? AND : IOR,
5228 GET_MODE (x),
5229 XEXP (XEXP (x, 0), 0),
5230 XEXP (XEXP (x, 1), 0))));
5231 return find_split_point (loc, insn, set_src);
5234 /* Many RISC machines have a large set of logical insns. If the
5235 second operand is a NOT, put it first so we will try to split the
5236 other operand first. */
5237 if (GET_CODE (XEXP (x, 1)) == NOT)
5239 rtx tem = XEXP (x, 0);
5240 SUBST (XEXP (x, 0), XEXP (x, 1));
5241 SUBST (XEXP (x, 1), tem);
5243 break;
5245 case PLUS:
5246 case MINUS:
5247 /* Canonicalization can produce (minus A (mult B C)), where C is a
5248 constant. It may be better to try splitting (plus (mult B -C) A)
5249 instead if this isn't a multiply by a power of two. */
5250 if (set_src && code == MINUS && GET_CODE (XEXP (x, 1)) == MULT
5251 && GET_CODE (XEXP (XEXP (x, 1), 1)) == CONST_INT
5252 && !pow2p_hwi (INTVAL (XEXP (XEXP (x, 1), 1))))
5254 machine_mode mode = GET_MODE (x);
5255 unsigned HOST_WIDE_INT this_int = INTVAL (XEXP (XEXP (x, 1), 1));
5256 HOST_WIDE_INT other_int = trunc_int_for_mode (-this_int, mode);
5257 SUBST (*loc, gen_rtx_PLUS (mode,
5258 gen_rtx_MULT (mode,
5259 XEXP (XEXP (x, 1), 0),
5260 gen_int_mode (other_int,
5261 mode)),
5262 XEXP (x, 0)));
5263 return find_split_point (loc, insn, set_src);
5266 /* Split at a multiply-accumulate instruction. However if this is
5267 the SET_SRC, we likely do not have such an instruction and it's
5268 worthless to try this split. */
5269 if (!set_src
5270 && (GET_CODE (XEXP (x, 0)) == MULT
5271 || (GET_CODE (XEXP (x, 0)) == ASHIFT
5272 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)))
5273 return loc;
5275 default:
5276 break;
5279 /* Otherwise, select our actions depending on our rtx class. */
5280 switch (GET_RTX_CLASS (code))
5282 case RTX_BITFIELD_OPS: /* This is ZERO_EXTRACT and SIGN_EXTRACT. */
5283 case RTX_TERNARY:
5284 split = find_split_point (&XEXP (x, 2), insn, false);
5285 if (split)
5286 return split;
5287 /* fall through */
5288 case RTX_BIN_ARITH:
5289 case RTX_COMM_ARITH:
5290 case RTX_COMPARE:
5291 case RTX_COMM_COMPARE:
5292 split = find_split_point (&XEXP (x, 1), insn, false);
5293 if (split)
5294 return split;
5295 /* fall through */
5296 case RTX_UNARY:
5297 /* Some machines have (and (shift ...) ...) insns. If X is not
5298 an AND, but XEXP (X, 0) is, use it as our split point. */
5299 if (GET_CODE (x) != AND && GET_CODE (XEXP (x, 0)) == AND)
5300 return &XEXP (x, 0);
5302 split = find_split_point (&XEXP (x, 0), insn, false);
5303 if (split)
5304 return split;
5305 return loc;
5307 default:
5308 /* Otherwise, we don't have a split point. */
5309 return 0;
5313 /* Throughout X, replace FROM with TO, and return the result.
5314 The result is TO if X is FROM;
5315 otherwise the result is X, but its contents may have been modified.
5316 If they were modified, a record was made in undobuf so that
5317 undo_all will (among other things) return X to its original state.
5319 If the number of changes necessary is too much to record to undo,
5320 the excess changes are not made, so the result is invalid.
5321 The changes already made can still be undone.
5322 undobuf.num_undo is incremented for such changes, so by testing that
5323 the caller can tell whether the result is valid.
5325 `n_occurrences' is incremented each time FROM is replaced.
5327 IN_DEST is nonzero if we are processing the SET_DEST of a SET.
5329 IN_COND is nonzero if we are at the top level of a condition.
5331 UNIQUE_COPY is nonzero if each substitution must be unique. We do this
5332 by copying if `n_occurrences' is nonzero. */
5334 static rtx
5335 subst (rtx x, rtx from, rtx to, int in_dest, int in_cond, int unique_copy)
5337 enum rtx_code code = GET_CODE (x);
5338 machine_mode op0_mode = VOIDmode;
5339 const char *fmt;
5340 int len, i;
5341 rtx new_rtx;
5343 /* Two expressions are equal if they are identical copies of a shared
5344 RTX or if they are both registers with the same register number
5345 and mode. */
5347 #define COMBINE_RTX_EQUAL_P(X,Y) \
5348 ((X) == (Y) \
5349 || (REG_P (X) && REG_P (Y) \
5350 && REGNO (X) == REGNO (Y) && GET_MODE (X) == GET_MODE (Y)))
5352 /* Do not substitute into clobbers of regs -- this will never result in
5353 valid RTL. */
5354 if (GET_CODE (x) == CLOBBER && REG_P (XEXP (x, 0)))
5355 return x;
5357 if (! in_dest && COMBINE_RTX_EQUAL_P (x, from))
5359 n_occurrences++;
5360 return (unique_copy && n_occurrences > 1 ? copy_rtx (to) : to);
5363 /* If X and FROM are the same register but different modes, they
5364 will not have been seen as equal above. However, the log links code
5365 will make a LOG_LINKS entry for that case. If we do nothing, we
5366 will try to rerecognize our original insn and, when it succeeds,
5367 we will delete the feeding insn, which is incorrect.
5369 So force this insn not to match in this (rare) case. */
5370 if (! in_dest && code == REG && REG_P (from)
5371 && reg_overlap_mentioned_p (x, from))
5372 return gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
5374 /* If this is an object, we are done unless it is a MEM or LO_SUM, both
5375 of which may contain things that can be combined. */
5376 if (code != MEM && code != LO_SUM && OBJECT_P (x))
5377 return x;
5379 /* It is possible to have a subexpression appear twice in the insn.
5380 Suppose that FROM is a register that appears within TO.
5381 Then, after that subexpression has been scanned once by `subst',
5382 the second time it is scanned, TO may be found. If we were
5383 to scan TO here, we would find FROM within it and create a
5384 self-referent rtl structure which is completely wrong. */
5385 if (COMBINE_RTX_EQUAL_P (x, to))
5386 return to;
5388 /* Parallel asm_operands need special attention because all of the
5389 inputs are shared across the arms. Furthermore, unsharing the
5390 rtl results in recognition failures. Failure to handle this case
5391 specially can result in circular rtl.
5393 Solve this by doing a normal pass across the first entry of the
5394 parallel, and only processing the SET_DESTs of the subsequent
5395 entries. Ug. */
5397 if (code == PARALLEL
5398 && GET_CODE (XVECEXP (x, 0, 0)) == SET
5399 && GET_CODE (SET_SRC (XVECEXP (x, 0, 0))) == ASM_OPERANDS)
5401 new_rtx = subst (XVECEXP (x, 0, 0), from, to, 0, 0, unique_copy);
5403 /* If this substitution failed, this whole thing fails. */
5404 if (GET_CODE (new_rtx) == CLOBBER
5405 && XEXP (new_rtx, 0) == const0_rtx)
5406 return new_rtx;
5408 SUBST (XVECEXP (x, 0, 0), new_rtx);
5410 for (i = XVECLEN (x, 0) - 1; i >= 1; i--)
5412 rtx dest = SET_DEST (XVECEXP (x, 0, i));
5414 if (!REG_P (dest)
5415 && GET_CODE (dest) != CC0
5416 && GET_CODE (dest) != PC)
5418 new_rtx = subst (dest, from, to, 0, 0, unique_copy);
5420 /* If this substitution failed, this whole thing fails. */
5421 if (GET_CODE (new_rtx) == CLOBBER
5422 && XEXP (new_rtx, 0) == const0_rtx)
5423 return new_rtx;
5425 SUBST (SET_DEST (XVECEXP (x, 0, i)), new_rtx);
5429 else
5431 len = GET_RTX_LENGTH (code);
5432 fmt = GET_RTX_FORMAT (code);
5434 /* We don't need to process a SET_DEST that is a register, CC0,
5435 or PC, so set up to skip this common case. All other cases
5436 where we want to suppress replacing something inside a
5437 SET_SRC are handled via the IN_DEST operand. */
5438 if (code == SET
5439 && (REG_P (SET_DEST (x))
5440 || GET_CODE (SET_DEST (x)) == CC0
5441 || GET_CODE (SET_DEST (x)) == PC))
5442 fmt = "ie";
5444 /* Trying to simplify the operands of a widening MULT is not likely
5445 to create RTL matching a machine insn. */
5446 if (code == MULT
5447 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
5448 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND)
5449 && (GET_CODE (XEXP (x, 1)) == ZERO_EXTEND
5450 || GET_CODE (XEXP (x, 1)) == SIGN_EXTEND)
5451 && REG_P (XEXP (XEXP (x, 0), 0))
5452 && REG_P (XEXP (XEXP (x, 1), 0))
5453 && from == to)
5454 return x;
5457 /* Get the mode of operand 0 in case X is now a SIGN_EXTEND of a
5458 constant. */
5459 if (fmt[0] == 'e')
5460 op0_mode = GET_MODE (XEXP (x, 0));
5462 for (i = 0; i < len; i++)
5464 if (fmt[i] == 'E')
5466 int j;
5467 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5469 if (COMBINE_RTX_EQUAL_P (XVECEXP (x, i, j), from))
5471 new_rtx = (unique_copy && n_occurrences
5472 ? copy_rtx (to) : to);
5473 n_occurrences++;
5475 else
5477 new_rtx = subst (XVECEXP (x, i, j), from, to, 0, 0,
5478 unique_copy);
5480 /* If this substitution failed, this whole thing
5481 fails. */
5482 if (GET_CODE (new_rtx) == CLOBBER
5483 && XEXP (new_rtx, 0) == const0_rtx)
5484 return new_rtx;
5487 SUBST (XVECEXP (x, i, j), new_rtx);
5490 else if (fmt[i] == 'e')
5492 /* If this is a register being set, ignore it. */
5493 new_rtx = XEXP (x, i);
5494 if (in_dest
5495 && i == 0
5496 && (((code == SUBREG || code == ZERO_EXTRACT)
5497 && REG_P (new_rtx))
5498 || code == STRICT_LOW_PART))
5501 else if (COMBINE_RTX_EQUAL_P (XEXP (x, i), from))
5503 /* In general, don't install a subreg involving two
5504 modes not tieable. It can worsen register
5505 allocation, and can even make invalid reload
5506 insns, since the reg inside may need to be copied
5507 from in the outside mode, and that may be invalid
5508 if it is an fp reg copied in integer mode.
5510 We allow two exceptions to this: It is valid if
5511 it is inside another SUBREG and the mode of that
5512 SUBREG and the mode of the inside of TO is
5513 tieable and it is valid if X is a SET that copies
5514 FROM to CC0. */
5516 if (GET_CODE (to) == SUBREG
5517 && !targetm.modes_tieable_p (GET_MODE (to),
5518 GET_MODE (SUBREG_REG (to)))
5519 && ! (code == SUBREG
5520 && (targetm.modes_tieable_p
5521 (GET_MODE (x), GET_MODE (SUBREG_REG (to)))))
5522 && (!HAVE_cc0
5523 || (! (code == SET
5524 && i == 1
5525 && XEXP (x, 0) == cc0_rtx))))
5526 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5528 if (code == SUBREG
5529 && REG_P (to)
5530 && REGNO (to) < FIRST_PSEUDO_REGISTER
5531 && simplify_subreg_regno (REGNO (to), GET_MODE (to),
5532 SUBREG_BYTE (x),
5533 GET_MODE (x)) < 0)
5534 return gen_rtx_CLOBBER (VOIDmode, const0_rtx);
5536 new_rtx = (unique_copy && n_occurrences ? copy_rtx (to) : to);
5537 n_occurrences++;
5539 else
5540 /* If we are in a SET_DEST, suppress most cases unless we
5541 have gone inside a MEM, in which case we want to
5542 simplify the address. We assume here that things that
5543 are actually part of the destination have their inner
5544 parts in the first expression. This is true for SUBREG,
5545 STRICT_LOW_PART, and ZERO_EXTRACT, which are the only
5546 things aside from REG and MEM that should appear in a
5547 SET_DEST. */
5548 new_rtx = subst (XEXP (x, i), from, to,
5549 (((in_dest
5550 && (code == SUBREG || code == STRICT_LOW_PART
5551 || code == ZERO_EXTRACT))
5552 || code == SET)
5553 && i == 0),
5554 code == IF_THEN_ELSE && i == 0,
5555 unique_copy);
5557 /* If we found that we will have to reject this combination,
5558 indicate that by returning the CLOBBER ourselves, rather than
5559 an expression containing it. This will speed things up as
5560 well as prevent accidents where two CLOBBERs are considered
5561 to be equal, thus producing an incorrect simplification. */
5563 if (GET_CODE (new_rtx) == CLOBBER && XEXP (new_rtx, 0) == const0_rtx)
5564 return new_rtx;
5566 if (GET_CODE (x) == SUBREG && CONST_SCALAR_INT_P (new_rtx))
5568 machine_mode mode = GET_MODE (x);
5570 x = simplify_subreg (GET_MODE (x), new_rtx,
5571 GET_MODE (SUBREG_REG (x)),
5572 SUBREG_BYTE (x));
5573 if (! x)
5574 x = gen_rtx_CLOBBER (mode, const0_rtx);
5576 else if (CONST_SCALAR_INT_P (new_rtx)
5577 && GET_CODE (x) == ZERO_EXTEND)
5579 x = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
5580 new_rtx, GET_MODE (XEXP (x, 0)));
5581 gcc_assert (x);
5583 else
5584 SUBST (XEXP (x, i), new_rtx);
5589 /* Check if we are loading something from the constant pool via float
5590 extension; in this case we would undo compress_float_constant
5591 optimization and degenerate constant load to an immediate value. */
5592 if (GET_CODE (x) == FLOAT_EXTEND
5593 && MEM_P (XEXP (x, 0))
5594 && MEM_READONLY_P (XEXP (x, 0)))
5596 rtx tmp = avoid_constant_pool_reference (x);
5597 if (x != tmp)
5598 return x;
5601 /* Try to simplify X. If the simplification changed the code, it is likely
5602 that further simplification will help, so loop, but limit the number
5603 of repetitions that will be performed. */
5605 for (i = 0; i < 4; i++)
5607 /* If X is sufficiently simple, don't bother trying to do anything
5608 with it. */
5609 if (code != CONST_INT && code != REG && code != CLOBBER)
5610 x = combine_simplify_rtx (x, op0_mode, in_dest, in_cond);
5612 if (GET_CODE (x) == code)
5613 break;
5615 code = GET_CODE (x);
5617 /* We no longer know the original mode of operand 0 since we
5618 have changed the form of X) */
5619 op0_mode = VOIDmode;
5622 return x;
5625 /* If X is a commutative operation whose operands are not in the canonical
5626 order, use substitutions to swap them. */
5628 static void
5629 maybe_swap_commutative_operands (rtx x)
5631 if (COMMUTATIVE_ARITH_P (x)
5632 && swap_commutative_operands_p (XEXP (x, 0), XEXP (x, 1)))
5634 rtx temp = XEXP (x, 0);
5635 SUBST (XEXP (x, 0), XEXP (x, 1));
5636 SUBST (XEXP (x, 1), temp);
5640 /* Simplify X, a piece of RTL. We just operate on the expression at the
5641 outer level; call `subst' to simplify recursively. Return the new
5642 expression.
5644 OP0_MODE is the original mode of XEXP (x, 0). IN_DEST is nonzero
5645 if we are inside a SET_DEST. IN_COND is nonzero if we are at the top level
5646 of a condition. */
5648 static rtx
5649 combine_simplify_rtx (rtx x, machine_mode op0_mode, int in_dest,
5650 int in_cond)
5652 enum rtx_code code = GET_CODE (x);
5653 machine_mode mode = GET_MODE (x);
5654 scalar_int_mode int_mode;
5655 rtx temp;
5656 int i;
5658 /* If this is a commutative operation, put a constant last and a complex
5659 expression first. We don't need to do this for comparisons here. */
5660 maybe_swap_commutative_operands (x);
5662 /* Try to fold this expression in case we have constants that weren't
5663 present before. */
5664 temp = 0;
5665 switch (GET_RTX_CLASS (code))
5667 case RTX_UNARY:
5668 if (op0_mode == VOIDmode)
5669 op0_mode = GET_MODE (XEXP (x, 0));
5670 temp = simplify_unary_operation (code, mode, XEXP (x, 0), op0_mode);
5671 break;
5672 case RTX_COMPARE:
5673 case RTX_COMM_COMPARE:
5675 machine_mode cmp_mode = GET_MODE (XEXP (x, 0));
5676 if (cmp_mode == VOIDmode)
5678 cmp_mode = GET_MODE (XEXP (x, 1));
5679 if (cmp_mode == VOIDmode)
5680 cmp_mode = op0_mode;
5682 temp = simplify_relational_operation (code, mode, cmp_mode,
5683 XEXP (x, 0), XEXP (x, 1));
5685 break;
5686 case RTX_COMM_ARITH:
5687 case RTX_BIN_ARITH:
5688 temp = simplify_binary_operation (code, mode, XEXP (x, 0), XEXP (x, 1));
5689 break;
5690 case RTX_BITFIELD_OPS:
5691 case RTX_TERNARY:
5692 temp = simplify_ternary_operation (code, mode, op0_mode, XEXP (x, 0),
5693 XEXP (x, 1), XEXP (x, 2));
5694 break;
5695 default:
5696 break;
5699 if (temp)
5701 x = temp;
5702 code = GET_CODE (temp);
5703 op0_mode = VOIDmode;
5704 mode = GET_MODE (temp);
5707 /* If this is a simple operation applied to an IF_THEN_ELSE, try
5708 applying it to the arms of the IF_THEN_ELSE. This often simplifies
5709 things. Check for cases where both arms are testing the same
5710 condition.
5712 Don't do anything if all operands are very simple. */
5714 if ((BINARY_P (x)
5715 && ((!OBJECT_P (XEXP (x, 0))
5716 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5717 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))
5718 || (!OBJECT_P (XEXP (x, 1))
5719 && ! (GET_CODE (XEXP (x, 1)) == SUBREG
5720 && OBJECT_P (SUBREG_REG (XEXP (x, 1)))))))
5721 || (UNARY_P (x)
5722 && (!OBJECT_P (XEXP (x, 0))
5723 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
5724 && OBJECT_P (SUBREG_REG (XEXP (x, 0)))))))
5726 rtx cond, true_rtx, false_rtx;
5728 cond = if_then_else_cond (x, &true_rtx, &false_rtx);
5729 if (cond != 0
5730 /* If everything is a comparison, what we have is highly unlikely
5731 to be simpler, so don't use it. */
5732 && ! (COMPARISON_P (x)
5733 && (COMPARISON_P (true_rtx) || COMPARISON_P (false_rtx)))
5734 /* Similarly, if we end up with one of the expressions the same
5735 as the original, it is certainly not simpler. */
5736 && ! rtx_equal_p (x, true_rtx)
5737 && ! rtx_equal_p (x, false_rtx))
5739 rtx cop1 = const0_rtx;
5740 enum rtx_code cond_code = simplify_comparison (NE, &cond, &cop1);
5742 if (cond_code == NE && COMPARISON_P (cond))
5743 return x;
5745 /* Simplify the alternative arms; this may collapse the true and
5746 false arms to store-flag values. Be careful to use copy_rtx
5747 here since true_rtx or false_rtx might share RTL with x as a
5748 result of the if_then_else_cond call above. */
5749 true_rtx = subst (copy_rtx (true_rtx), pc_rtx, pc_rtx, 0, 0, 0);
5750 false_rtx = subst (copy_rtx (false_rtx), pc_rtx, pc_rtx, 0, 0, 0);
5752 /* If true_rtx and false_rtx are not general_operands, an if_then_else
5753 is unlikely to be simpler. */
5754 if (general_operand (true_rtx, VOIDmode)
5755 && general_operand (false_rtx, VOIDmode))
5757 enum rtx_code reversed;
5759 /* Restarting if we generate a store-flag expression will cause
5760 us to loop. Just drop through in this case. */
5762 /* If the result values are STORE_FLAG_VALUE and zero, we can
5763 just make the comparison operation. */
5764 if (true_rtx == const_true_rtx && false_rtx == const0_rtx)
5765 x = simplify_gen_relational (cond_code, mode, VOIDmode,
5766 cond, cop1);
5767 else if (true_rtx == const0_rtx && false_rtx == const_true_rtx
5768 && ((reversed = reversed_comparison_code_parts
5769 (cond_code, cond, cop1, NULL))
5770 != UNKNOWN))
5771 x = simplify_gen_relational (reversed, mode, VOIDmode,
5772 cond, cop1);
5774 /* Likewise, we can make the negate of a comparison operation
5775 if the result values are - STORE_FLAG_VALUE and zero. */
5776 else if (CONST_INT_P (true_rtx)
5777 && INTVAL (true_rtx) == - STORE_FLAG_VALUE
5778 && false_rtx == const0_rtx)
5779 x = simplify_gen_unary (NEG, mode,
5780 simplify_gen_relational (cond_code,
5781 mode, VOIDmode,
5782 cond, cop1),
5783 mode);
5784 else if (CONST_INT_P (false_rtx)
5785 && INTVAL (false_rtx) == - STORE_FLAG_VALUE
5786 && true_rtx == const0_rtx
5787 && ((reversed = reversed_comparison_code_parts
5788 (cond_code, cond, cop1, NULL))
5789 != UNKNOWN))
5790 x = simplify_gen_unary (NEG, mode,
5791 simplify_gen_relational (reversed,
5792 mode, VOIDmode,
5793 cond, cop1),
5794 mode);
5795 else
5796 return gen_rtx_IF_THEN_ELSE (mode,
5797 simplify_gen_relational (cond_code,
5798 mode,
5799 VOIDmode,
5800 cond,
5801 cop1),
5802 true_rtx, false_rtx);
5804 code = GET_CODE (x);
5805 op0_mode = VOIDmode;
5810 /* First see if we can apply the inverse distributive law. */
5811 if (code == PLUS || code == MINUS
5812 || code == AND || code == IOR || code == XOR)
5814 x = apply_distributive_law (x);
5815 code = GET_CODE (x);
5816 op0_mode = VOIDmode;
5819 /* If CODE is an associative operation not otherwise handled, see if we
5820 can associate some operands. This can win if they are constants or
5821 if they are logically related (i.e. (a & b) & a). */
5822 if ((code == PLUS || code == MINUS || code == MULT || code == DIV
5823 || code == AND || code == IOR || code == XOR
5824 || code == SMAX || code == SMIN || code == UMAX || code == UMIN)
5825 && ((INTEGRAL_MODE_P (mode) && code != DIV)
5826 || (flag_associative_math && FLOAT_MODE_P (mode))))
5828 if (GET_CODE (XEXP (x, 0)) == code)
5830 rtx other = XEXP (XEXP (x, 0), 0);
5831 rtx inner_op0 = XEXP (XEXP (x, 0), 1);
5832 rtx inner_op1 = XEXP (x, 1);
5833 rtx inner;
5835 /* Make sure we pass the constant operand if any as the second
5836 one if this is a commutative operation. */
5837 if (CONSTANT_P (inner_op0) && COMMUTATIVE_ARITH_P (x))
5838 std::swap (inner_op0, inner_op1);
5839 inner = simplify_binary_operation (code == MINUS ? PLUS
5840 : code == DIV ? MULT
5841 : code,
5842 mode, inner_op0, inner_op1);
5844 /* For commutative operations, try the other pair if that one
5845 didn't simplify. */
5846 if (inner == 0 && COMMUTATIVE_ARITH_P (x))
5848 other = XEXP (XEXP (x, 0), 1);
5849 inner = simplify_binary_operation (code, mode,
5850 XEXP (XEXP (x, 0), 0),
5851 XEXP (x, 1));
5854 if (inner)
5855 return simplify_gen_binary (code, mode, other, inner);
5859 /* A little bit of algebraic simplification here. */
5860 switch (code)
5862 case MEM:
5863 /* Ensure that our address has any ASHIFTs converted to MULT in case
5864 address-recognizing predicates are called later. */
5865 temp = make_compound_operation (XEXP (x, 0), MEM);
5866 SUBST (XEXP (x, 0), temp);
5867 break;
5869 case SUBREG:
5870 if (op0_mode == VOIDmode)
5871 op0_mode = GET_MODE (SUBREG_REG (x));
5873 /* See if this can be moved to simplify_subreg. */
5874 if (CONSTANT_P (SUBREG_REG (x))
5875 && known_eq (subreg_lowpart_offset (mode, op0_mode), SUBREG_BYTE (x))
5876 /* Don't call gen_lowpart if the inner mode
5877 is VOIDmode and we cannot simplify it, as SUBREG without
5878 inner mode is invalid. */
5879 && (GET_MODE (SUBREG_REG (x)) != VOIDmode
5880 || gen_lowpart_common (mode, SUBREG_REG (x))))
5881 return gen_lowpart (mode, SUBREG_REG (x));
5883 if (GET_MODE_CLASS (GET_MODE (SUBREG_REG (x))) == MODE_CC)
5884 break;
5886 rtx temp;
5887 temp = simplify_subreg (mode, SUBREG_REG (x), op0_mode,
5888 SUBREG_BYTE (x));
5889 if (temp)
5890 return temp;
5892 /* If op is known to have all lower bits zero, the result is zero. */
5893 scalar_int_mode int_mode, int_op0_mode;
5894 if (!in_dest
5895 && is_a <scalar_int_mode> (mode, &int_mode)
5896 && is_a <scalar_int_mode> (op0_mode, &int_op0_mode)
5897 && (GET_MODE_PRECISION (int_mode)
5898 < GET_MODE_PRECISION (int_op0_mode))
5899 && known_eq (subreg_lowpart_offset (int_mode, int_op0_mode),
5900 SUBREG_BYTE (x))
5901 && HWI_COMPUTABLE_MODE_P (int_op0_mode)
5902 && (nonzero_bits (SUBREG_REG (x), int_op0_mode)
5903 & GET_MODE_MASK (int_mode)) == 0)
5904 return CONST0_RTX (int_mode);
5907 /* Don't change the mode of the MEM if that would change the meaning
5908 of the address. */
5909 if (MEM_P (SUBREG_REG (x))
5910 && (MEM_VOLATILE_P (SUBREG_REG (x))
5911 || mode_dependent_address_p (XEXP (SUBREG_REG (x), 0),
5912 MEM_ADDR_SPACE (SUBREG_REG (x)))))
5913 return gen_rtx_CLOBBER (mode, const0_rtx);
5915 /* Note that we cannot do any narrowing for non-constants since
5916 we might have been counting on using the fact that some bits were
5917 zero. We now do this in the SET. */
5919 break;
5921 case NEG:
5922 temp = expand_compound_operation (XEXP (x, 0));
5924 /* For C equal to the width of MODE minus 1, (neg (ashiftrt X C)) can be
5925 replaced by (lshiftrt X C). This will convert
5926 (neg (sign_extract X 1 Y)) to (zero_extract X 1 Y). */
5928 if (GET_CODE (temp) == ASHIFTRT
5929 && CONST_INT_P (XEXP (temp, 1))
5930 && INTVAL (XEXP (temp, 1)) == GET_MODE_UNIT_PRECISION (mode) - 1)
5931 return simplify_shift_const (NULL_RTX, LSHIFTRT, mode, XEXP (temp, 0),
5932 INTVAL (XEXP (temp, 1)));
5934 /* If X has only a single bit that might be nonzero, say, bit I, convert
5935 (neg X) to (ashiftrt (ashift X C-I) C-I) where C is the bitsize of
5936 MODE minus 1. This will convert (neg (zero_extract X 1 Y)) to
5937 (sign_extract X 1 Y). But only do this if TEMP isn't a register
5938 or a SUBREG of one since we'd be making the expression more
5939 complex if it was just a register. */
5941 if (!REG_P (temp)
5942 && ! (GET_CODE (temp) == SUBREG
5943 && REG_P (SUBREG_REG (temp)))
5944 && is_a <scalar_int_mode> (mode, &int_mode)
5945 && (i = exact_log2 (nonzero_bits (temp, int_mode))) >= 0)
5947 rtx temp1 = simplify_shift_const
5948 (NULL_RTX, ASHIFTRT, int_mode,
5949 simplify_shift_const (NULL_RTX, ASHIFT, int_mode, temp,
5950 GET_MODE_PRECISION (int_mode) - 1 - i),
5951 GET_MODE_PRECISION (int_mode) - 1 - i);
5953 /* If all we did was surround TEMP with the two shifts, we
5954 haven't improved anything, so don't use it. Otherwise,
5955 we are better off with TEMP1. */
5956 if (GET_CODE (temp1) != ASHIFTRT
5957 || GET_CODE (XEXP (temp1, 0)) != ASHIFT
5958 || XEXP (XEXP (temp1, 0), 0) != temp)
5959 return temp1;
5961 break;
5963 case TRUNCATE:
5964 /* We can't handle truncation to a partial integer mode here
5965 because we don't know the real bitsize of the partial
5966 integer mode. */
5967 if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
5968 break;
5970 if (HWI_COMPUTABLE_MODE_P (mode))
5971 SUBST (XEXP (x, 0),
5972 force_to_mode (XEXP (x, 0), GET_MODE (XEXP (x, 0)),
5973 GET_MODE_MASK (mode), 0));
5975 /* We can truncate a constant value and return it. */
5976 if (CONST_INT_P (XEXP (x, 0)))
5977 return gen_int_mode (INTVAL (XEXP (x, 0)), mode);
5979 /* Similarly to what we do in simplify-rtx.c, a truncate of a register
5980 whose value is a comparison can be replaced with a subreg if
5981 STORE_FLAG_VALUE permits. */
5982 if (HWI_COMPUTABLE_MODE_P (mode)
5983 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (mode)) == 0
5984 && (temp = get_last_value (XEXP (x, 0)))
5985 && COMPARISON_P (temp))
5986 return gen_lowpart (mode, XEXP (x, 0));
5987 break;
5989 case CONST:
5990 /* (const (const X)) can become (const X). Do it this way rather than
5991 returning the inner CONST since CONST can be shared with a
5992 REG_EQUAL note. */
5993 if (GET_CODE (XEXP (x, 0)) == CONST)
5994 SUBST (XEXP (x, 0), XEXP (XEXP (x, 0), 0));
5995 break;
5997 case LO_SUM:
5998 /* Convert (lo_sum (high FOO) FOO) to FOO. This is necessary so we
5999 can add in an offset. find_split_point will split this address up
6000 again if it doesn't match. */
6001 if (HAVE_lo_sum && GET_CODE (XEXP (x, 0)) == HIGH
6002 && rtx_equal_p (XEXP (XEXP (x, 0), 0), XEXP (x, 1)))
6003 return XEXP (x, 1);
6004 break;
6006 case PLUS:
6007 /* (plus (xor (and <foo> (const_int pow2 - 1)) <c>) <-c>)
6008 when c is (const_int (pow2 + 1) / 2) is a sign extension of a
6009 bit-field and can be replaced by either a sign_extend or a
6010 sign_extract. The `and' may be a zero_extend and the two
6011 <c>, -<c> constants may be reversed. */
6012 if (GET_CODE (XEXP (x, 0)) == XOR
6013 && is_a <scalar_int_mode> (mode, &int_mode)
6014 && CONST_INT_P (XEXP (x, 1))
6015 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
6016 && INTVAL (XEXP (x, 1)) == -INTVAL (XEXP (XEXP (x, 0), 1))
6017 && ((i = exact_log2 (UINTVAL (XEXP (XEXP (x, 0), 1)))) >= 0
6018 || (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0)
6019 && HWI_COMPUTABLE_MODE_P (int_mode)
6020 && ((GET_CODE (XEXP (XEXP (x, 0), 0)) == AND
6021 && CONST_INT_P (XEXP (XEXP (XEXP (x, 0), 0), 1))
6022 && (UINTVAL (XEXP (XEXP (XEXP (x, 0), 0), 1))
6023 == (HOST_WIDE_INT_1U << (i + 1)) - 1))
6024 || (GET_CODE (XEXP (XEXP (x, 0), 0)) == ZERO_EXTEND
6025 && known_eq ((GET_MODE_PRECISION
6026 (GET_MODE (XEXP (XEXP (XEXP (x, 0), 0), 0)))),
6027 (unsigned int) i + 1))))
6028 return simplify_shift_const
6029 (NULL_RTX, ASHIFTRT, int_mode,
6030 simplify_shift_const (NULL_RTX, ASHIFT, int_mode,
6031 XEXP (XEXP (XEXP (x, 0), 0), 0),
6032 GET_MODE_PRECISION (int_mode) - (i + 1)),
6033 GET_MODE_PRECISION (int_mode) - (i + 1));
6035 /* If only the low-order bit of X is possibly nonzero, (plus x -1)
6036 can become (ashiftrt (ashift (xor x 1) C) C) where C is
6037 the bitsize of the mode - 1. This allows simplification of
6038 "a = (b & 8) == 0;" */
6039 if (XEXP (x, 1) == constm1_rtx
6040 && !REG_P (XEXP (x, 0))
6041 && ! (GET_CODE (XEXP (x, 0)) == SUBREG
6042 && REG_P (SUBREG_REG (XEXP (x, 0))))
6043 && is_a <scalar_int_mode> (mode, &int_mode)
6044 && nonzero_bits (XEXP (x, 0), int_mode) == 1)
6045 return simplify_shift_const
6046 (NULL_RTX, ASHIFTRT, int_mode,
6047 simplify_shift_const (NULL_RTX, ASHIFT, int_mode,
6048 gen_rtx_XOR (int_mode, XEXP (x, 0),
6049 const1_rtx),
6050 GET_MODE_PRECISION (int_mode) - 1),
6051 GET_MODE_PRECISION (int_mode) - 1);
6053 /* If we are adding two things that have no bits in common, convert
6054 the addition into an IOR. This will often be further simplified,
6055 for example in cases like ((a & 1) + (a & 2)), which can
6056 become a & 3. */
6058 if (HWI_COMPUTABLE_MODE_P (mode)
6059 && (nonzero_bits (XEXP (x, 0), mode)
6060 & nonzero_bits (XEXP (x, 1), mode)) == 0)
6062 /* Try to simplify the expression further. */
6063 rtx tor = simplify_gen_binary (IOR, mode, XEXP (x, 0), XEXP (x, 1));
6064 temp = combine_simplify_rtx (tor, VOIDmode, in_dest, 0);
6066 /* If we could, great. If not, do not go ahead with the IOR
6067 replacement, since PLUS appears in many special purpose
6068 address arithmetic instructions. */
6069 if (GET_CODE (temp) != CLOBBER
6070 && (GET_CODE (temp) != IOR
6071 || ((XEXP (temp, 0) != XEXP (x, 0)
6072 || XEXP (temp, 1) != XEXP (x, 1))
6073 && (XEXP (temp, 0) != XEXP (x, 1)
6074 || XEXP (temp, 1) != XEXP (x, 0)))))
6075 return temp;
6078 /* Canonicalize x + x into x << 1. */
6079 if (GET_MODE_CLASS (mode) == MODE_INT
6080 && rtx_equal_p (XEXP (x, 0), XEXP (x, 1))
6081 && !side_effects_p (XEXP (x, 0)))
6082 return simplify_gen_binary (ASHIFT, mode, XEXP (x, 0), const1_rtx);
6084 break;
6086 case MINUS:
6087 /* (minus <foo> (and <foo> (const_int -pow2))) becomes
6088 (and <foo> (const_int pow2-1)) */
6089 if (is_a <scalar_int_mode> (mode, &int_mode)
6090 && GET_CODE (XEXP (x, 1)) == AND
6091 && CONST_INT_P (XEXP (XEXP (x, 1), 1))
6092 && pow2p_hwi (-UINTVAL (XEXP (XEXP (x, 1), 1)))
6093 && rtx_equal_p (XEXP (XEXP (x, 1), 0), XEXP (x, 0)))
6094 return simplify_and_const_int (NULL_RTX, int_mode, XEXP (x, 0),
6095 -INTVAL (XEXP (XEXP (x, 1), 1)) - 1);
6096 break;
6098 case MULT:
6099 /* If we have (mult (plus A B) C), apply the distributive law and then
6100 the inverse distributive law to see if things simplify. This
6101 occurs mostly in addresses, often when unrolling loops. */
6103 if (GET_CODE (XEXP (x, 0)) == PLUS)
6105 rtx result = distribute_and_simplify_rtx (x, 0);
6106 if (result)
6107 return result;
6110 /* Try simplify a*(b/c) as (a*b)/c. */
6111 if (FLOAT_MODE_P (mode) && flag_associative_math
6112 && GET_CODE (XEXP (x, 0)) == DIV)
6114 rtx tem = simplify_binary_operation (MULT, mode,
6115 XEXP (XEXP (x, 0), 0),
6116 XEXP (x, 1));
6117 if (tem)
6118 return simplify_gen_binary (DIV, mode, tem, XEXP (XEXP (x, 0), 1));
6120 break;
6122 case UDIV:
6123 /* If this is a divide by a power of two, treat it as a shift if
6124 its first operand is a shift. */
6125 if (is_a <scalar_int_mode> (mode, &int_mode)
6126 && CONST_INT_P (XEXP (x, 1))
6127 && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0
6128 && (GET_CODE (XEXP (x, 0)) == ASHIFT
6129 || GET_CODE (XEXP (x, 0)) == LSHIFTRT
6130 || GET_CODE (XEXP (x, 0)) == ASHIFTRT
6131 || GET_CODE (XEXP (x, 0)) == ROTATE
6132 || GET_CODE (XEXP (x, 0)) == ROTATERT))
6133 return simplify_shift_const (NULL_RTX, LSHIFTRT, int_mode,
6134 XEXP (x, 0), i);
6135 break;
6137 case EQ: case NE:
6138 case GT: case GTU: case GE: case GEU:
6139 case LT: case LTU: case LE: case LEU:
6140 case UNEQ: case LTGT:
6141 case UNGT: case UNGE:
6142 case UNLT: case UNLE:
6143 case UNORDERED: case ORDERED:
6144 /* If the first operand is a condition code, we can't do anything
6145 with it. */
6146 if (GET_CODE (XEXP (x, 0)) == COMPARE
6147 || (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) != MODE_CC
6148 && ! CC0_P (XEXP (x, 0))))
6150 rtx op0 = XEXP (x, 0);
6151 rtx op1 = XEXP (x, 1);
6152 enum rtx_code new_code;
6154 if (GET_CODE (op0) == COMPARE)
6155 op1 = XEXP (op0, 1), op0 = XEXP (op0, 0);
6157 /* Simplify our comparison, if possible. */
6158 new_code = simplify_comparison (code, &op0, &op1);
6160 /* If STORE_FLAG_VALUE is 1, we can convert (ne x 0) to simply X
6161 if only the low-order bit is possibly nonzero in X (such as when
6162 X is a ZERO_EXTRACT of one bit). Similarly, we can convert EQ to
6163 (xor X 1) or (minus 1 X); we use the former. Finally, if X is
6164 known to be either 0 or -1, NE becomes a NEG and EQ becomes
6165 (plus X 1).
6167 Remove any ZERO_EXTRACT we made when thinking this was a
6168 comparison. It may now be simpler to use, e.g., an AND. If a
6169 ZERO_EXTRACT is indeed appropriate, it will be placed back by
6170 the call to make_compound_operation in the SET case.
6172 Don't apply these optimizations if the caller would
6173 prefer a comparison rather than a value.
6174 E.g., for the condition in an IF_THEN_ELSE most targets need
6175 an explicit comparison. */
6177 if (in_cond)
6180 else if (STORE_FLAG_VALUE == 1
6181 && new_code == NE
6182 && is_int_mode (mode, &int_mode)
6183 && op1 == const0_rtx
6184 && int_mode == GET_MODE (op0)
6185 && nonzero_bits (op0, int_mode) == 1)
6186 return gen_lowpart (int_mode,
6187 expand_compound_operation (op0));
6189 else if (STORE_FLAG_VALUE == 1
6190 && new_code == NE
6191 && is_int_mode (mode, &int_mode)
6192 && op1 == const0_rtx
6193 && int_mode == GET_MODE (op0)
6194 && (num_sign_bit_copies (op0, int_mode)
6195 == GET_MODE_PRECISION (int_mode)))
6197 op0 = expand_compound_operation (op0);
6198 return simplify_gen_unary (NEG, int_mode,
6199 gen_lowpart (int_mode, op0),
6200 int_mode);
6203 else if (STORE_FLAG_VALUE == 1
6204 && new_code == EQ
6205 && is_int_mode (mode, &int_mode)
6206 && op1 == const0_rtx
6207 && int_mode == GET_MODE (op0)
6208 && nonzero_bits (op0, int_mode) == 1)
6210 op0 = expand_compound_operation (op0);
6211 return simplify_gen_binary (XOR, int_mode,
6212 gen_lowpart (int_mode, op0),
6213 const1_rtx);
6216 else if (STORE_FLAG_VALUE == 1
6217 && new_code == EQ
6218 && is_int_mode (mode, &int_mode)
6219 && op1 == const0_rtx
6220 && int_mode == GET_MODE (op0)
6221 && (num_sign_bit_copies (op0, int_mode)
6222 == GET_MODE_PRECISION (int_mode)))
6224 op0 = expand_compound_operation (op0);
6225 return plus_constant (int_mode, gen_lowpart (int_mode, op0), 1);
6228 /* If STORE_FLAG_VALUE is -1, we have cases similar to
6229 those above. */
6230 if (in_cond)
6233 else if (STORE_FLAG_VALUE == -1
6234 && new_code == NE
6235 && is_int_mode (mode, &int_mode)
6236 && op1 == const0_rtx
6237 && int_mode == GET_MODE (op0)
6238 && (num_sign_bit_copies (op0, int_mode)
6239 == GET_MODE_PRECISION (int_mode)))
6240 return gen_lowpart (int_mode, expand_compound_operation (op0));
6242 else if (STORE_FLAG_VALUE == -1
6243 && new_code == NE
6244 && is_int_mode (mode, &int_mode)
6245 && op1 == const0_rtx
6246 && int_mode == GET_MODE (op0)
6247 && nonzero_bits (op0, int_mode) == 1)
6249 op0 = expand_compound_operation (op0);
6250 return simplify_gen_unary (NEG, int_mode,
6251 gen_lowpart (int_mode, op0),
6252 int_mode);
6255 else if (STORE_FLAG_VALUE == -1
6256 && new_code == EQ
6257 && is_int_mode (mode, &int_mode)
6258 && op1 == const0_rtx
6259 && int_mode == GET_MODE (op0)
6260 && (num_sign_bit_copies (op0, int_mode)
6261 == GET_MODE_PRECISION (int_mode)))
6263 op0 = expand_compound_operation (op0);
6264 return simplify_gen_unary (NOT, int_mode,
6265 gen_lowpart (int_mode, op0),
6266 int_mode);
6269 /* If X is 0/1, (eq X 0) is X-1. */
6270 else if (STORE_FLAG_VALUE == -1
6271 && new_code == EQ
6272 && is_int_mode (mode, &int_mode)
6273 && op1 == const0_rtx
6274 && int_mode == GET_MODE (op0)
6275 && nonzero_bits (op0, int_mode) == 1)
6277 op0 = expand_compound_operation (op0);
6278 return plus_constant (int_mode, gen_lowpart (int_mode, op0), -1);
6281 /* If STORE_FLAG_VALUE says to just test the sign bit and X has just
6282 one bit that might be nonzero, we can convert (ne x 0) to
6283 (ashift x c) where C puts the bit in the sign bit. Remove any
6284 AND with STORE_FLAG_VALUE when we are done, since we are only
6285 going to test the sign bit. */
6286 if (new_code == NE
6287 && is_int_mode (mode, &int_mode)
6288 && HWI_COMPUTABLE_MODE_P (int_mode)
6289 && val_signbit_p (int_mode, STORE_FLAG_VALUE)
6290 && op1 == const0_rtx
6291 && int_mode == GET_MODE (op0)
6292 && (i = exact_log2 (nonzero_bits (op0, int_mode))) >= 0)
6294 x = simplify_shift_const (NULL_RTX, ASHIFT, int_mode,
6295 expand_compound_operation (op0),
6296 GET_MODE_PRECISION (int_mode) - 1 - i);
6297 if (GET_CODE (x) == AND && XEXP (x, 1) == const_true_rtx)
6298 return XEXP (x, 0);
6299 else
6300 return x;
6303 /* If the code changed, return a whole new comparison.
6304 We also need to avoid using SUBST in cases where
6305 simplify_comparison has widened a comparison with a CONST_INT,
6306 since in that case the wider CONST_INT may fail the sanity
6307 checks in do_SUBST. */
6308 if (new_code != code
6309 || (CONST_INT_P (op1)
6310 && GET_MODE (op0) != GET_MODE (XEXP (x, 0))
6311 && GET_MODE (op0) != GET_MODE (XEXP (x, 1))))
6312 return gen_rtx_fmt_ee (new_code, mode, op0, op1);
6314 /* Otherwise, keep this operation, but maybe change its operands.
6315 This also converts (ne (compare FOO BAR) 0) to (ne FOO BAR). */
6316 SUBST (XEXP (x, 0), op0);
6317 SUBST (XEXP (x, 1), op1);
6319 break;
6321 case IF_THEN_ELSE:
6322 return simplify_if_then_else (x);
6324 case ZERO_EXTRACT:
6325 case SIGN_EXTRACT:
6326 case ZERO_EXTEND:
6327 case SIGN_EXTEND:
6328 /* If we are processing SET_DEST, we are done. */
6329 if (in_dest)
6330 return x;
6332 return expand_compound_operation (x);
6334 case SET:
6335 return simplify_set (x);
6337 case AND:
6338 case IOR:
6339 return simplify_logical (x);
6341 case ASHIFT:
6342 case LSHIFTRT:
6343 case ASHIFTRT:
6344 case ROTATE:
6345 case ROTATERT:
6346 /* If this is a shift by a constant amount, simplify it. */
6347 if (CONST_INT_P (XEXP (x, 1)))
6348 return simplify_shift_const (x, code, mode, XEXP (x, 0),
6349 INTVAL (XEXP (x, 1)));
6351 else if (SHIFT_COUNT_TRUNCATED && !REG_P (XEXP (x, 1)))
6352 SUBST (XEXP (x, 1),
6353 force_to_mode (XEXP (x, 1), GET_MODE (XEXP (x, 1)),
6354 (HOST_WIDE_INT_1U
6355 << exact_log2 (GET_MODE_UNIT_BITSIZE
6356 (GET_MODE (x))))
6357 - 1,
6358 0));
6359 break;
6361 default:
6362 break;
6365 return x;
6368 /* Simplify X, an IF_THEN_ELSE expression. Return the new expression. */
6370 static rtx
6371 simplify_if_then_else (rtx x)
6373 machine_mode mode = GET_MODE (x);
6374 rtx cond = XEXP (x, 0);
6375 rtx true_rtx = XEXP (x, 1);
6376 rtx false_rtx = XEXP (x, 2);
6377 enum rtx_code true_code = GET_CODE (cond);
6378 int comparison_p = COMPARISON_P (cond);
6379 rtx temp;
6380 int i;
6381 enum rtx_code false_code;
6382 rtx reversed;
6383 scalar_int_mode int_mode, inner_mode;
6385 /* Simplify storing of the truth value. */
6386 if (comparison_p && true_rtx == const_true_rtx && false_rtx == const0_rtx)
6387 return simplify_gen_relational (true_code, mode, VOIDmode,
6388 XEXP (cond, 0), XEXP (cond, 1));
6390 /* Also when the truth value has to be reversed. */
6391 if (comparison_p
6392 && true_rtx == const0_rtx && false_rtx == const_true_rtx
6393 && (reversed = reversed_comparison (cond, mode)))
6394 return reversed;
6396 /* Sometimes we can simplify the arm of an IF_THEN_ELSE if a register used
6397 in it is being compared against certain values. Get the true and false
6398 comparisons and see if that says anything about the value of each arm. */
6400 if (comparison_p
6401 && ((false_code = reversed_comparison_code (cond, NULL))
6402 != UNKNOWN)
6403 && REG_P (XEXP (cond, 0)))
6405 HOST_WIDE_INT nzb;
6406 rtx from = XEXP (cond, 0);
6407 rtx true_val = XEXP (cond, 1);
6408 rtx false_val = true_val;
6409 int swapped = 0;
6411 /* If FALSE_CODE is EQ, swap the codes and arms. */
6413 if (false_code == EQ)
6415 swapped = 1, true_code = EQ, false_code = NE;
6416 std::swap (true_rtx, false_rtx);
6419 scalar_int_mode from_mode;
6420 if (is_a <scalar_int_mode> (GET_MODE (from), &from_mode))
6422 /* If we are comparing against zero and the expression being
6423 tested has only a single bit that might be nonzero, that is
6424 its value when it is not equal to zero. Similarly if it is
6425 known to be -1 or 0. */
6426 if (true_code == EQ
6427 && true_val == const0_rtx
6428 && pow2p_hwi (nzb = nonzero_bits (from, from_mode)))
6430 false_code = EQ;
6431 false_val = gen_int_mode (nzb, from_mode);
6433 else if (true_code == EQ
6434 && true_val == const0_rtx
6435 && (num_sign_bit_copies (from, from_mode)
6436 == GET_MODE_PRECISION (from_mode)))
6438 false_code = EQ;
6439 false_val = constm1_rtx;
6443 /* Now simplify an arm if we know the value of the register in the
6444 branch and it is used in the arm. Be careful due to the potential
6445 of locally-shared RTL. */
6447 if (reg_mentioned_p (from, true_rtx))
6448 true_rtx = subst (known_cond (copy_rtx (true_rtx), true_code,
6449 from, true_val),
6450 pc_rtx, pc_rtx, 0, 0, 0);
6451 if (reg_mentioned_p (from, false_rtx))
6452 false_rtx = subst (known_cond (copy_rtx (false_rtx), false_code,
6453 from, false_val),
6454 pc_rtx, pc_rtx, 0, 0, 0);
6456 SUBST (XEXP (x, 1), swapped ? false_rtx : true_rtx);
6457 SUBST (XEXP (x, 2), swapped ? true_rtx : false_rtx);
6459 true_rtx = XEXP (x, 1);
6460 false_rtx = XEXP (x, 2);
6461 true_code = GET_CODE (cond);
6464 /* If we have (if_then_else FOO (pc) (label_ref BAR)) and FOO can be
6465 reversed, do so to avoid needing two sets of patterns for
6466 subtract-and-branch insns. Similarly if we have a constant in the true
6467 arm, the false arm is the same as the first operand of the comparison, or
6468 the false arm is more complicated than the true arm. */
6470 if (comparison_p
6471 && reversed_comparison_code (cond, NULL) != UNKNOWN
6472 && (true_rtx == pc_rtx
6473 || (CONSTANT_P (true_rtx)
6474 && !CONST_INT_P (false_rtx) && false_rtx != pc_rtx)
6475 || true_rtx == const0_rtx
6476 || (OBJECT_P (true_rtx) && !OBJECT_P (false_rtx))
6477 || (GET_CODE (true_rtx) == SUBREG && OBJECT_P (SUBREG_REG (true_rtx))
6478 && !OBJECT_P (false_rtx))
6479 || reg_mentioned_p (true_rtx, false_rtx)
6480 || rtx_equal_p (false_rtx, XEXP (cond, 0))))
6482 true_code = reversed_comparison_code (cond, NULL);
6483 SUBST (XEXP (x, 0), reversed_comparison (cond, GET_MODE (cond)));
6484 SUBST (XEXP (x, 1), false_rtx);
6485 SUBST (XEXP (x, 2), true_rtx);
6487 std::swap (true_rtx, false_rtx);
6488 cond = XEXP (x, 0);
6490 /* It is possible that the conditional has been simplified out. */
6491 true_code = GET_CODE (cond);
6492 comparison_p = COMPARISON_P (cond);
6495 /* If the two arms are identical, we don't need the comparison. */
6497 if (rtx_equal_p (true_rtx, false_rtx) && ! side_effects_p (cond))
6498 return true_rtx;
6500 /* Convert a == b ? b : a to "a". */
6501 if (true_code == EQ && ! side_effects_p (cond)
6502 && !HONOR_NANS (mode)
6503 && rtx_equal_p (XEXP (cond, 0), false_rtx)
6504 && rtx_equal_p (XEXP (cond, 1), true_rtx))
6505 return false_rtx;
6506 else if (true_code == NE && ! side_effects_p (cond)
6507 && !HONOR_NANS (mode)
6508 && rtx_equal_p (XEXP (cond, 0), true_rtx)
6509 && rtx_equal_p (XEXP (cond, 1), false_rtx))
6510 return true_rtx;
6512 /* Look for cases where we have (abs x) or (neg (abs X)). */
6514 if (GET_MODE_CLASS (mode) == MODE_INT
6515 && comparison_p
6516 && XEXP (cond, 1) == const0_rtx
6517 && GET_CODE (false_rtx) == NEG
6518 && rtx_equal_p (true_rtx, XEXP (false_rtx, 0))
6519 && rtx_equal_p (true_rtx, XEXP (cond, 0))
6520 && ! side_effects_p (true_rtx))
6521 switch (true_code)
6523 case GT:
6524 case GE:
6525 return simplify_gen_unary (ABS, mode, true_rtx, mode);
6526 case LT:
6527 case LE:
6528 return
6529 simplify_gen_unary (NEG, mode,
6530 simplify_gen_unary (ABS, mode, true_rtx, mode),
6531 mode);
6532 default:
6533 break;
6536 /* Look for MIN or MAX. */
6538 if ((! FLOAT_MODE_P (mode) || flag_unsafe_math_optimizations)
6539 && comparison_p
6540 && rtx_equal_p (XEXP (cond, 0), true_rtx)
6541 && rtx_equal_p (XEXP (cond, 1), false_rtx)
6542 && ! side_effects_p (cond))
6543 switch (true_code)
6545 case GE:
6546 case GT:
6547 return simplify_gen_binary (SMAX, mode, true_rtx, false_rtx);
6548 case LE:
6549 case LT:
6550 return simplify_gen_binary (SMIN, mode, true_rtx, false_rtx);
6551 case GEU:
6552 case GTU:
6553 return simplify_gen_binary (UMAX, mode, true_rtx, false_rtx);
6554 case LEU:
6555 case LTU:
6556 return simplify_gen_binary (UMIN, mode, true_rtx, false_rtx);
6557 default:
6558 break;
6561 /* If we have (if_then_else COND (OP Z C1) Z) and OP is an identity when its
6562 second operand is zero, this can be done as (OP Z (mult COND C2)) where
6563 C2 = C1 * STORE_FLAG_VALUE. Similarly if OP has an outer ZERO_EXTEND or
6564 SIGN_EXTEND as long as Z is already extended (so we don't destroy it).
6565 We can do this kind of thing in some cases when STORE_FLAG_VALUE is
6566 neither 1 or -1, but it isn't worth checking for. */
6568 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
6569 && comparison_p
6570 && is_int_mode (mode, &int_mode)
6571 && ! side_effects_p (x))
6573 rtx t = make_compound_operation (true_rtx, SET);
6574 rtx f = make_compound_operation (false_rtx, SET);
6575 rtx cond_op0 = XEXP (cond, 0);
6576 rtx cond_op1 = XEXP (cond, 1);
6577 enum rtx_code op = UNKNOWN, extend_op = UNKNOWN;
6578 scalar_int_mode m = int_mode;
6579 rtx z = 0, c1 = NULL_RTX;
6581 if ((GET_CODE (t) == PLUS || GET_CODE (t) == MINUS
6582 || GET_CODE (t) == IOR || GET_CODE (t) == XOR
6583 || GET_CODE (t) == ASHIFT
6584 || GET_CODE (t) == LSHIFTRT || GET_CODE (t) == ASHIFTRT)
6585 && rtx_equal_p (XEXP (t, 0), f))
6586 c1 = XEXP (t, 1), op = GET_CODE (t), z = f;
6588 /* If an identity-zero op is commutative, check whether there
6589 would be a match if we swapped the operands. */
6590 else if ((GET_CODE (t) == PLUS || GET_CODE (t) == IOR
6591 || GET_CODE (t) == XOR)
6592 && rtx_equal_p (XEXP (t, 1), f))
6593 c1 = XEXP (t, 0), op = GET_CODE (t), z = f;
6594 else if (GET_CODE (t) == SIGN_EXTEND
6595 && is_a <scalar_int_mode> (GET_MODE (XEXP (t, 0)), &inner_mode)
6596 && (GET_CODE (XEXP (t, 0)) == PLUS
6597 || GET_CODE (XEXP (t, 0)) == MINUS
6598 || GET_CODE (XEXP (t, 0)) == IOR
6599 || GET_CODE (XEXP (t, 0)) == XOR
6600 || GET_CODE (XEXP (t, 0)) == ASHIFT
6601 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6602 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6603 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6604 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6605 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6606 && (num_sign_bit_copies (f, GET_MODE (f))
6607 > (unsigned int)
6608 (GET_MODE_PRECISION (int_mode)
6609 - GET_MODE_PRECISION (inner_mode))))
6611 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6612 extend_op = SIGN_EXTEND;
6613 m = inner_mode;
6615 else if (GET_CODE (t) == SIGN_EXTEND
6616 && is_a <scalar_int_mode> (GET_MODE (XEXP (t, 0)), &inner_mode)
6617 && (GET_CODE (XEXP (t, 0)) == PLUS
6618 || GET_CODE (XEXP (t, 0)) == IOR
6619 || GET_CODE (XEXP (t, 0)) == XOR)
6620 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6621 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6622 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6623 && (num_sign_bit_copies (f, GET_MODE (f))
6624 > (unsigned int)
6625 (GET_MODE_PRECISION (int_mode)
6626 - GET_MODE_PRECISION (inner_mode))))
6628 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6629 extend_op = SIGN_EXTEND;
6630 m = inner_mode;
6632 else if (GET_CODE (t) == ZERO_EXTEND
6633 && is_a <scalar_int_mode> (GET_MODE (XEXP (t, 0)), &inner_mode)
6634 && (GET_CODE (XEXP (t, 0)) == PLUS
6635 || GET_CODE (XEXP (t, 0)) == MINUS
6636 || GET_CODE (XEXP (t, 0)) == IOR
6637 || GET_CODE (XEXP (t, 0)) == XOR
6638 || GET_CODE (XEXP (t, 0)) == ASHIFT
6639 || GET_CODE (XEXP (t, 0)) == LSHIFTRT
6640 || GET_CODE (XEXP (t, 0)) == ASHIFTRT)
6641 && GET_CODE (XEXP (XEXP (t, 0), 0)) == SUBREG
6642 && HWI_COMPUTABLE_MODE_P (int_mode)
6643 && subreg_lowpart_p (XEXP (XEXP (t, 0), 0))
6644 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 0)), f)
6645 && ((nonzero_bits (f, GET_MODE (f))
6646 & ~GET_MODE_MASK (inner_mode))
6647 == 0))
6649 c1 = XEXP (XEXP (t, 0), 1); z = f; op = GET_CODE (XEXP (t, 0));
6650 extend_op = ZERO_EXTEND;
6651 m = inner_mode;
6653 else if (GET_CODE (t) == ZERO_EXTEND
6654 && is_a <scalar_int_mode> (GET_MODE (XEXP (t, 0)), &inner_mode)
6655 && (GET_CODE (XEXP (t, 0)) == PLUS
6656 || GET_CODE (XEXP (t, 0)) == IOR
6657 || GET_CODE (XEXP (t, 0)) == XOR)
6658 && GET_CODE (XEXP (XEXP (t, 0), 1)) == SUBREG
6659 && HWI_COMPUTABLE_MODE_P (int_mode)
6660 && subreg_lowpart_p (XEXP (XEXP (t, 0), 1))
6661 && rtx_equal_p (SUBREG_REG (XEXP (XEXP (t, 0), 1)), f)
6662 && ((nonzero_bits (f, GET_MODE (f))
6663 & ~GET_MODE_MASK (inner_mode))
6664 == 0))
6666 c1 = XEXP (XEXP (t, 0), 0); z = f; op = GET_CODE (XEXP (t, 0));
6667 extend_op = ZERO_EXTEND;
6668 m = inner_mode;
6671 if (z)
6673 machine_mode cm = m;
6674 if ((op == ASHIFT || op == LSHIFTRT || op == ASHIFTRT)
6675 && GET_MODE (c1) != VOIDmode)
6676 cm = GET_MODE (c1);
6677 temp = subst (simplify_gen_relational (true_code, cm, VOIDmode,
6678 cond_op0, cond_op1),
6679 pc_rtx, pc_rtx, 0, 0, 0);
6680 temp = simplify_gen_binary (MULT, cm, temp,
6681 simplify_gen_binary (MULT, cm, c1,
6682 const_true_rtx));
6683 temp = subst (temp, pc_rtx, pc_rtx, 0, 0, 0);
6684 temp = simplify_gen_binary (op, m, gen_lowpart (m, z), temp);
6686 if (extend_op != UNKNOWN)
6687 temp = simplify_gen_unary (extend_op, int_mode, temp, m);
6689 return temp;
6693 /* If we have (if_then_else (ne A 0) C1 0) and either A is known to be 0 or
6694 1 and C1 is a single bit or A is known to be 0 or -1 and C1 is the
6695 negation of a single bit, we can convert this operation to a shift. We
6696 can actually do this more generally, but it doesn't seem worth it. */
6698 if (true_code == NE
6699 && is_a <scalar_int_mode> (mode, &int_mode)
6700 && XEXP (cond, 1) == const0_rtx
6701 && false_rtx == const0_rtx
6702 && CONST_INT_P (true_rtx)
6703 && ((nonzero_bits (XEXP (cond, 0), int_mode) == 1
6704 && (i = exact_log2 (UINTVAL (true_rtx))) >= 0)
6705 || ((num_sign_bit_copies (XEXP (cond, 0), int_mode)
6706 == GET_MODE_PRECISION (int_mode))
6707 && (i = exact_log2 (-UINTVAL (true_rtx))) >= 0)))
6708 return
6709 simplify_shift_const (NULL_RTX, ASHIFT, int_mode,
6710 gen_lowpart (int_mode, XEXP (cond, 0)), i);
6712 /* (IF_THEN_ELSE (NE A 0) C1 0) is A or a zero-extend of A if the only
6713 non-zero bit in A is C1. */
6714 if (true_code == NE && XEXP (cond, 1) == const0_rtx
6715 && false_rtx == const0_rtx && CONST_INT_P (true_rtx)
6716 && is_a <scalar_int_mode> (mode, &int_mode)
6717 && is_a <scalar_int_mode> (GET_MODE (XEXP (cond, 0)), &inner_mode)
6718 && (UINTVAL (true_rtx) & GET_MODE_MASK (int_mode))
6719 == nonzero_bits (XEXP (cond, 0), inner_mode)
6720 && (i = exact_log2 (UINTVAL (true_rtx) & GET_MODE_MASK (int_mode))) >= 0)
6722 rtx val = XEXP (cond, 0);
6723 if (inner_mode == int_mode)
6724 return val;
6725 else if (GET_MODE_PRECISION (inner_mode) < GET_MODE_PRECISION (int_mode))
6726 return simplify_gen_unary (ZERO_EXTEND, int_mode, val, inner_mode);
6729 return x;
6732 /* Simplify X, a SET expression. Return the new expression. */
6734 static rtx
6735 simplify_set (rtx x)
6737 rtx src = SET_SRC (x);
6738 rtx dest = SET_DEST (x);
6739 machine_mode mode
6740 = GET_MODE (src) != VOIDmode ? GET_MODE (src) : GET_MODE (dest);
6741 rtx_insn *other_insn;
6742 rtx *cc_use;
6743 scalar_int_mode int_mode;
6745 /* (set (pc) (return)) gets written as (return). */
6746 if (GET_CODE (dest) == PC && ANY_RETURN_P (src))
6747 return src;
6749 /* Now that we know for sure which bits of SRC we are using, see if we can
6750 simplify the expression for the object knowing that we only need the
6751 low-order bits. */
6753 if (GET_MODE_CLASS (mode) == MODE_INT && HWI_COMPUTABLE_MODE_P (mode))
6755 src = force_to_mode (src, mode, HOST_WIDE_INT_M1U, 0);
6756 SUBST (SET_SRC (x), src);
6759 /* If we are setting CC0 or if the source is a COMPARE, look for the use of
6760 the comparison result and try to simplify it unless we already have used
6761 undobuf.other_insn. */
6762 if ((GET_MODE_CLASS (mode) == MODE_CC
6763 || GET_CODE (src) == COMPARE
6764 || CC0_P (dest))
6765 && (cc_use = find_single_use (dest, subst_insn, &other_insn)) != 0
6766 && (undobuf.other_insn == 0 || other_insn == undobuf.other_insn)
6767 && COMPARISON_P (*cc_use)
6768 && rtx_equal_p (XEXP (*cc_use, 0), dest))
6770 enum rtx_code old_code = GET_CODE (*cc_use);
6771 enum rtx_code new_code;
6772 rtx op0, op1, tmp;
6773 int other_changed = 0;
6774 rtx inner_compare = NULL_RTX;
6775 machine_mode compare_mode = GET_MODE (dest);
6777 if (GET_CODE (src) == COMPARE)
6779 op0 = XEXP (src, 0), op1 = XEXP (src, 1);
6780 if (GET_CODE (op0) == COMPARE && op1 == const0_rtx)
6782 inner_compare = op0;
6783 op0 = XEXP (inner_compare, 0), op1 = XEXP (inner_compare, 1);
6786 else
6787 op0 = src, op1 = CONST0_RTX (GET_MODE (src));
6789 tmp = simplify_relational_operation (old_code, compare_mode, VOIDmode,
6790 op0, op1);
6791 if (!tmp)
6792 new_code = old_code;
6793 else if (!CONSTANT_P (tmp))
6795 new_code = GET_CODE (tmp);
6796 op0 = XEXP (tmp, 0);
6797 op1 = XEXP (tmp, 1);
6799 else
6801 rtx pat = PATTERN (other_insn);
6802 undobuf.other_insn = other_insn;
6803 SUBST (*cc_use, tmp);
6805 /* Attempt to simplify CC user. */
6806 if (GET_CODE (pat) == SET)
6808 rtx new_rtx = simplify_rtx (SET_SRC (pat));
6809 if (new_rtx != NULL_RTX)
6810 SUBST (SET_SRC (pat), new_rtx);
6813 /* Convert X into a no-op move. */
6814 SUBST (SET_DEST (x), pc_rtx);
6815 SUBST (SET_SRC (x), pc_rtx);
6816 return x;
6819 /* Simplify our comparison, if possible. */
6820 new_code = simplify_comparison (new_code, &op0, &op1);
6822 #ifdef SELECT_CC_MODE
6823 /* If this machine has CC modes other than CCmode, check to see if we
6824 need to use a different CC mode here. */
6825 if (GET_MODE_CLASS (GET_MODE (op0)) == MODE_CC)
6826 compare_mode = GET_MODE (op0);
6827 else if (inner_compare
6828 && GET_MODE_CLASS (GET_MODE (inner_compare)) == MODE_CC
6829 && new_code == old_code
6830 && op0 == XEXP (inner_compare, 0)
6831 && op1 == XEXP (inner_compare, 1))
6832 compare_mode = GET_MODE (inner_compare);
6833 else
6834 compare_mode = SELECT_CC_MODE (new_code, op0, op1);
6836 /* If the mode changed, we have to change SET_DEST, the mode in the
6837 compare, and the mode in the place SET_DEST is used. If SET_DEST is
6838 a hard register, just build new versions with the proper mode. If it
6839 is a pseudo, we lose unless it is only time we set the pseudo, in
6840 which case we can safely change its mode. */
6841 if (!HAVE_cc0 && compare_mode != GET_MODE (dest))
6843 if (can_change_dest_mode (dest, 0, compare_mode))
6845 unsigned int regno = REGNO (dest);
6846 rtx new_dest;
6848 if (regno < FIRST_PSEUDO_REGISTER)
6849 new_dest = gen_rtx_REG (compare_mode, regno);
6850 else
6852 SUBST_MODE (regno_reg_rtx[regno], compare_mode);
6853 new_dest = regno_reg_rtx[regno];
6856 SUBST (SET_DEST (x), new_dest);
6857 SUBST (XEXP (*cc_use, 0), new_dest);
6858 other_changed = 1;
6860 dest = new_dest;
6863 #endif /* SELECT_CC_MODE */
6865 /* If the code changed, we have to build a new comparison in
6866 undobuf.other_insn. */
6867 if (new_code != old_code)
6869 int other_changed_previously = other_changed;
6870 unsigned HOST_WIDE_INT mask;
6871 rtx old_cc_use = *cc_use;
6873 SUBST (*cc_use, gen_rtx_fmt_ee (new_code, GET_MODE (*cc_use),
6874 dest, const0_rtx));
6875 other_changed = 1;
6877 /* If the only change we made was to change an EQ into an NE or
6878 vice versa, OP0 has only one bit that might be nonzero, and OP1
6879 is zero, check if changing the user of the condition code will
6880 produce a valid insn. If it won't, we can keep the original code
6881 in that insn by surrounding our operation with an XOR. */
6883 if (((old_code == NE && new_code == EQ)
6884 || (old_code == EQ && new_code == NE))
6885 && ! other_changed_previously && op1 == const0_rtx
6886 && HWI_COMPUTABLE_MODE_P (GET_MODE (op0))
6887 && pow2p_hwi (mask = nonzero_bits (op0, GET_MODE (op0))))
6889 rtx pat = PATTERN (other_insn), note = 0;
6891 if ((recog_for_combine (&pat, other_insn, &note) < 0
6892 && ! check_asm_operands (pat)))
6894 *cc_use = old_cc_use;
6895 other_changed = 0;
6897 op0 = simplify_gen_binary (XOR, GET_MODE (op0), op0,
6898 gen_int_mode (mask,
6899 GET_MODE (op0)));
6904 if (other_changed)
6905 undobuf.other_insn = other_insn;
6907 /* Don't generate a compare of a CC with 0, just use that CC. */
6908 if (GET_MODE (op0) == compare_mode && op1 == const0_rtx)
6910 SUBST (SET_SRC (x), op0);
6911 src = SET_SRC (x);
6913 /* Otherwise, if we didn't previously have the same COMPARE we
6914 want, create it from scratch. */
6915 else if (GET_CODE (src) != COMPARE || GET_MODE (src) != compare_mode
6916 || XEXP (src, 0) != op0 || XEXP (src, 1) != op1)
6918 SUBST (SET_SRC (x), gen_rtx_COMPARE (compare_mode, op0, op1));
6919 src = SET_SRC (x);
6922 else
6924 /* Get SET_SRC in a form where we have placed back any
6925 compound expressions. Then do the checks below. */
6926 src = make_compound_operation (src, SET);
6927 SUBST (SET_SRC (x), src);
6930 /* If we have (set x (subreg:m1 (op:m2 ...) 0)) with OP being some operation,
6931 and X being a REG or (subreg (reg)), we may be able to convert this to
6932 (set (subreg:m2 x) (op)).
6934 We can always do this if M1 is narrower than M2 because that means that
6935 we only care about the low bits of the result.
6937 However, on machines without WORD_REGISTER_OPERATIONS defined, we cannot
6938 perform a narrower operation than requested since the high-order bits will
6939 be undefined. On machine where it is defined, this transformation is safe
6940 as long as M1 and M2 have the same number of words. */
6942 if (GET_CODE (src) == SUBREG && subreg_lowpart_p (src)
6943 && !OBJECT_P (SUBREG_REG (src))
6944 && (known_equal_after_align_up
6945 (GET_MODE_SIZE (GET_MODE (src)),
6946 GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))),
6947 UNITS_PER_WORD))
6948 && (WORD_REGISTER_OPERATIONS || !paradoxical_subreg_p (src))
6949 && ! (REG_P (dest) && REGNO (dest) < FIRST_PSEUDO_REGISTER
6950 && !REG_CAN_CHANGE_MODE_P (REGNO (dest),
6951 GET_MODE (SUBREG_REG (src)),
6952 GET_MODE (src)))
6953 && (REG_P (dest)
6954 || (GET_CODE (dest) == SUBREG
6955 && REG_P (SUBREG_REG (dest)))))
6957 SUBST (SET_DEST (x),
6958 gen_lowpart (GET_MODE (SUBREG_REG (src)),
6959 dest));
6960 SUBST (SET_SRC (x), SUBREG_REG (src));
6962 src = SET_SRC (x), dest = SET_DEST (x);
6965 /* If we have (set (cc0) (subreg ...)), we try to remove the subreg
6966 in SRC. */
6967 if (dest == cc0_rtx
6968 && partial_subreg_p (src)
6969 && subreg_lowpart_p (src))
6971 rtx inner = SUBREG_REG (src);
6972 machine_mode inner_mode = GET_MODE (inner);
6974 /* Here we make sure that we don't have a sign bit on. */
6975 if (val_signbit_known_clear_p (GET_MODE (src),
6976 nonzero_bits (inner, inner_mode)))
6978 SUBST (SET_SRC (x), inner);
6979 src = SET_SRC (x);
6983 /* If we have (set FOO (subreg:M (mem:N BAR) 0)) with M wider than N, this
6984 would require a paradoxical subreg. Replace the subreg with a
6985 zero_extend to avoid the reload that would otherwise be required.
6986 Don't do this unless we have a scalar integer mode, otherwise the
6987 transformation is incorrect. */
6989 enum rtx_code extend_op;
6990 if (paradoxical_subreg_p (src)
6991 && MEM_P (SUBREG_REG (src))
6992 && SCALAR_INT_MODE_P (GET_MODE (src))
6993 && (extend_op = load_extend_op (GET_MODE (SUBREG_REG (src)))) != UNKNOWN)
6995 SUBST (SET_SRC (x),
6996 gen_rtx_fmt_e (extend_op, GET_MODE (src), SUBREG_REG (src)));
6998 src = SET_SRC (x);
7001 /* If we don't have a conditional move, SET_SRC is an IF_THEN_ELSE, and we
7002 are comparing an item known to be 0 or -1 against 0, use a logical
7003 operation instead. Check for one of the arms being an IOR of the other
7004 arm with some value. We compute three terms to be IOR'ed together. In
7005 practice, at most two will be nonzero. Then we do the IOR's. */
7007 if (GET_CODE (dest) != PC
7008 && GET_CODE (src) == IF_THEN_ELSE
7009 && is_int_mode (GET_MODE (src), &int_mode)
7010 && (GET_CODE (XEXP (src, 0)) == EQ || GET_CODE (XEXP (src, 0)) == NE)
7011 && XEXP (XEXP (src, 0), 1) == const0_rtx
7012 && int_mode == GET_MODE (XEXP (XEXP (src, 0), 0))
7013 && (!HAVE_conditional_move
7014 || ! can_conditionally_move_p (int_mode))
7015 && (num_sign_bit_copies (XEXP (XEXP (src, 0), 0), int_mode)
7016 == GET_MODE_PRECISION (int_mode))
7017 && ! side_effects_p (src))
7019 rtx true_rtx = (GET_CODE (XEXP (src, 0)) == NE
7020 ? XEXP (src, 1) : XEXP (src, 2));
7021 rtx false_rtx = (GET_CODE (XEXP (src, 0)) == NE
7022 ? XEXP (src, 2) : XEXP (src, 1));
7023 rtx term1 = const0_rtx, term2, term3;
7025 if (GET_CODE (true_rtx) == IOR
7026 && rtx_equal_p (XEXP (true_rtx, 0), false_rtx))
7027 term1 = false_rtx, true_rtx = XEXP (true_rtx, 1), false_rtx = const0_rtx;
7028 else if (GET_CODE (true_rtx) == IOR
7029 && rtx_equal_p (XEXP (true_rtx, 1), false_rtx))
7030 term1 = false_rtx, true_rtx = XEXP (true_rtx, 0), false_rtx = const0_rtx;
7031 else if (GET_CODE (false_rtx) == IOR
7032 && rtx_equal_p (XEXP (false_rtx, 0), true_rtx))
7033 term1 = true_rtx, false_rtx = XEXP (false_rtx, 1), true_rtx = const0_rtx;
7034 else if (GET_CODE (false_rtx) == IOR
7035 && rtx_equal_p (XEXP (false_rtx, 1), true_rtx))
7036 term1 = true_rtx, false_rtx = XEXP (false_rtx, 0), true_rtx = const0_rtx;
7038 term2 = simplify_gen_binary (AND, int_mode,
7039 XEXP (XEXP (src, 0), 0), true_rtx);
7040 term3 = simplify_gen_binary (AND, int_mode,
7041 simplify_gen_unary (NOT, int_mode,
7042 XEXP (XEXP (src, 0), 0),
7043 int_mode),
7044 false_rtx);
7046 SUBST (SET_SRC (x),
7047 simplify_gen_binary (IOR, int_mode,
7048 simplify_gen_binary (IOR, int_mode,
7049 term1, term2),
7050 term3));
7052 src = SET_SRC (x);
7055 /* If either SRC or DEST is a CLOBBER of (const_int 0), make this
7056 whole thing fail. */
7057 if (GET_CODE (src) == CLOBBER && XEXP (src, 0) == const0_rtx)
7058 return src;
7059 else if (GET_CODE (dest) == CLOBBER && XEXP (dest, 0) == const0_rtx)
7060 return dest;
7061 else
7062 /* Convert this into a field assignment operation, if possible. */
7063 return make_field_assignment (x);
7066 /* Simplify, X, and AND, IOR, or XOR operation, and return the simplified
7067 result. */
7069 static rtx
7070 simplify_logical (rtx x)
7072 rtx op0 = XEXP (x, 0);
7073 rtx op1 = XEXP (x, 1);
7074 scalar_int_mode mode;
7076 switch (GET_CODE (x))
7078 case AND:
7079 /* We can call simplify_and_const_int only if we don't lose
7080 any (sign) bits when converting INTVAL (op1) to
7081 "unsigned HOST_WIDE_INT". */
7082 if (is_a <scalar_int_mode> (GET_MODE (x), &mode)
7083 && CONST_INT_P (op1)
7084 && (HWI_COMPUTABLE_MODE_P (mode)
7085 || INTVAL (op1) > 0))
7087 x = simplify_and_const_int (x, mode, op0, INTVAL (op1));
7088 if (GET_CODE (x) != AND)
7089 return x;
7091 op0 = XEXP (x, 0);
7092 op1 = XEXP (x, 1);
7095 /* If we have any of (and (ior A B) C) or (and (xor A B) C),
7096 apply the distributive law and then the inverse distributive
7097 law to see if things simplify. */
7098 if (GET_CODE (op0) == IOR || GET_CODE (op0) == XOR)
7100 rtx result = distribute_and_simplify_rtx (x, 0);
7101 if (result)
7102 return result;
7104 if (GET_CODE (op1) == IOR || GET_CODE (op1) == XOR)
7106 rtx result = distribute_and_simplify_rtx (x, 1);
7107 if (result)
7108 return result;
7110 break;
7112 case IOR:
7113 /* If we have (ior (and A B) C), apply the distributive law and then
7114 the inverse distributive law to see if things simplify. */
7116 if (GET_CODE (op0) == AND)
7118 rtx result = distribute_and_simplify_rtx (x, 0);
7119 if (result)
7120 return result;
7123 if (GET_CODE (op1) == AND)
7125 rtx result = distribute_and_simplify_rtx (x, 1);
7126 if (result)
7127 return result;
7129 break;
7131 default:
7132 gcc_unreachable ();
7135 return x;
7138 /* We consider ZERO_EXTRACT, SIGN_EXTRACT, and SIGN_EXTEND as "compound
7139 operations" because they can be replaced with two more basic operations.
7140 ZERO_EXTEND is also considered "compound" because it can be replaced with
7141 an AND operation, which is simpler, though only one operation.
7143 The function expand_compound_operation is called with an rtx expression
7144 and will convert it to the appropriate shifts and AND operations,
7145 simplifying at each stage.
7147 The function make_compound_operation is called to convert an expression
7148 consisting of shifts and ANDs into the equivalent compound expression.
7149 It is the inverse of this function, loosely speaking. */
7151 static rtx
7152 expand_compound_operation (rtx x)
7154 unsigned HOST_WIDE_INT pos = 0, len;
7155 int unsignedp = 0;
7156 unsigned int modewidth;
7157 rtx tem;
7158 scalar_int_mode inner_mode;
7160 switch (GET_CODE (x))
7162 case ZERO_EXTEND:
7163 unsignedp = 1;
7164 /* FALLTHRU */
7165 case SIGN_EXTEND:
7166 /* We can't necessarily use a const_int for a multiword mode;
7167 it depends on implicitly extending the value.
7168 Since we don't know the right way to extend it,
7169 we can't tell whether the implicit way is right.
7171 Even for a mode that is no wider than a const_int,
7172 we can't win, because we need to sign extend one of its bits through
7173 the rest of it, and we don't know which bit. */
7174 if (CONST_INT_P (XEXP (x, 0)))
7175 return x;
7177 /* Reject modes that aren't scalar integers because turning vector
7178 or complex modes into shifts causes problems. */
7179 if (!is_a <scalar_int_mode> (GET_MODE (XEXP (x, 0)), &inner_mode))
7180 return x;
7182 /* Return if (subreg:MODE FROM 0) is not a safe replacement for
7183 (zero_extend:MODE FROM) or (sign_extend:MODE FROM). It is for any MEM
7184 because (SUBREG (MEM...)) is guaranteed to cause the MEM to be
7185 reloaded. If not for that, MEM's would very rarely be safe.
7187 Reject modes bigger than a word, because we might not be able
7188 to reference a two-register group starting with an arbitrary register
7189 (and currently gen_lowpart might crash for a SUBREG). */
7191 if (GET_MODE_SIZE (inner_mode) > UNITS_PER_WORD)
7192 return x;
7194 len = GET_MODE_PRECISION (inner_mode);
7195 /* If the inner object has VOIDmode (the only way this can happen
7196 is if it is an ASM_OPERANDS), we can't do anything since we don't
7197 know how much masking to do. */
7198 if (len == 0)
7199 return x;
7201 break;
7203 case ZERO_EXTRACT:
7204 unsignedp = 1;
7206 /* fall through */
7208 case SIGN_EXTRACT:
7209 /* If the operand is a CLOBBER, just return it. */
7210 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
7211 return XEXP (x, 0);
7213 if (!CONST_INT_P (XEXP (x, 1))
7214 || !CONST_INT_P (XEXP (x, 2)))
7215 return x;
7217 /* Reject modes that aren't scalar integers because turning vector
7218 or complex modes into shifts causes problems. */
7219 if (!is_a <scalar_int_mode> (GET_MODE (XEXP (x, 0)), &inner_mode))
7220 return x;
7222 len = INTVAL (XEXP (x, 1));
7223 pos = INTVAL (XEXP (x, 2));
7225 /* This should stay within the object being extracted, fail otherwise. */
7226 if (len + pos > GET_MODE_PRECISION (inner_mode))
7227 return x;
7229 if (BITS_BIG_ENDIAN)
7230 pos = GET_MODE_PRECISION (inner_mode) - len - pos;
7232 break;
7234 default:
7235 return x;
7238 /* We've rejected non-scalar operations by now. */
7239 scalar_int_mode mode = as_a <scalar_int_mode> (GET_MODE (x));
7241 /* Convert sign extension to zero extension, if we know that the high
7242 bit is not set, as this is easier to optimize. It will be converted
7243 back to cheaper alternative in make_extraction. */
7244 if (GET_CODE (x) == SIGN_EXTEND
7245 && HWI_COMPUTABLE_MODE_P (mode)
7246 && ((nonzero_bits (XEXP (x, 0), inner_mode)
7247 & ~(((unsigned HOST_WIDE_INT) GET_MODE_MASK (inner_mode)) >> 1))
7248 == 0))
7250 rtx temp = gen_rtx_ZERO_EXTEND (mode, XEXP (x, 0));
7251 rtx temp2 = expand_compound_operation (temp);
7253 /* Make sure this is a profitable operation. */
7254 if (set_src_cost (x, mode, optimize_this_for_speed_p)
7255 > set_src_cost (temp2, mode, optimize_this_for_speed_p))
7256 return temp2;
7257 else if (set_src_cost (x, mode, optimize_this_for_speed_p)
7258 > set_src_cost (temp, mode, optimize_this_for_speed_p))
7259 return temp;
7260 else
7261 return x;
7264 /* We can optimize some special cases of ZERO_EXTEND. */
7265 if (GET_CODE (x) == ZERO_EXTEND)
7267 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI if we
7268 know that the last value didn't have any inappropriate bits
7269 set. */
7270 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
7271 && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode
7272 && HWI_COMPUTABLE_MODE_P (mode)
7273 && (nonzero_bits (XEXP (XEXP (x, 0), 0), mode)
7274 & ~GET_MODE_MASK (inner_mode)) == 0)
7275 return XEXP (XEXP (x, 0), 0);
7277 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
7278 if (GET_CODE (XEXP (x, 0)) == SUBREG
7279 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == mode
7280 && subreg_lowpart_p (XEXP (x, 0))
7281 && HWI_COMPUTABLE_MODE_P (mode)
7282 && (nonzero_bits (SUBREG_REG (XEXP (x, 0)), mode)
7283 & ~GET_MODE_MASK (inner_mode)) == 0)
7284 return SUBREG_REG (XEXP (x, 0));
7286 /* (zero_extend:DI (truncate:SI foo:DI)) is just foo:DI when foo
7287 is a comparison and STORE_FLAG_VALUE permits. This is like
7288 the first case, but it works even when MODE is larger
7289 than HOST_WIDE_INT. */
7290 if (GET_CODE (XEXP (x, 0)) == TRUNCATE
7291 && GET_MODE (XEXP (XEXP (x, 0), 0)) == mode
7292 && COMPARISON_P (XEXP (XEXP (x, 0), 0))
7293 && GET_MODE_PRECISION (inner_mode) <= HOST_BITS_PER_WIDE_INT
7294 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (inner_mode)) == 0)
7295 return XEXP (XEXP (x, 0), 0);
7297 /* Likewise for (zero_extend:DI (subreg:SI foo:DI 0)). */
7298 if (GET_CODE (XEXP (x, 0)) == SUBREG
7299 && GET_MODE (SUBREG_REG (XEXP (x, 0))) == mode
7300 && subreg_lowpart_p (XEXP (x, 0))
7301 && COMPARISON_P (SUBREG_REG (XEXP (x, 0)))
7302 && GET_MODE_PRECISION (inner_mode) <= HOST_BITS_PER_WIDE_INT
7303 && (STORE_FLAG_VALUE & ~GET_MODE_MASK (inner_mode)) == 0)
7304 return SUBREG_REG (XEXP (x, 0));
7308 /* If we reach here, we want to return a pair of shifts. The inner
7309 shift is a left shift of BITSIZE - POS - LEN bits. The outer
7310 shift is a right shift of BITSIZE - LEN bits. It is arithmetic or
7311 logical depending on the value of UNSIGNEDP.
7313 If this was a ZERO_EXTEND or ZERO_EXTRACT, this pair of shifts will be
7314 converted into an AND of a shift.
7316 We must check for the case where the left shift would have a negative
7317 count. This can happen in a case like (x >> 31) & 255 on machines
7318 that can't shift by a constant. On those machines, we would first
7319 combine the shift with the AND to produce a variable-position
7320 extraction. Then the constant of 31 would be substituted in
7321 to produce such a position. */
7323 modewidth = GET_MODE_PRECISION (mode);
7324 if (modewidth >= pos + len)
7326 tem = gen_lowpart (mode, XEXP (x, 0));
7327 if (!tem || GET_CODE (tem) == CLOBBER)
7328 return x;
7329 tem = simplify_shift_const (NULL_RTX, ASHIFT, mode,
7330 tem, modewidth - pos - len);
7331 tem = simplify_shift_const (NULL_RTX, unsignedp ? LSHIFTRT : ASHIFTRT,
7332 mode, tem, modewidth - len);
7334 else if (unsignedp && len < HOST_BITS_PER_WIDE_INT)
7335 tem = simplify_and_const_int (NULL_RTX, mode,
7336 simplify_shift_const (NULL_RTX, LSHIFTRT,
7337 mode, XEXP (x, 0),
7338 pos),
7339 (HOST_WIDE_INT_1U << len) - 1);
7340 else
7341 /* Any other cases we can't handle. */
7342 return x;
7344 /* If we couldn't do this for some reason, return the original
7345 expression. */
7346 if (GET_CODE (tem) == CLOBBER)
7347 return x;
7349 return tem;
7352 /* X is a SET which contains an assignment of one object into
7353 a part of another (such as a bit-field assignment, STRICT_LOW_PART,
7354 or certain SUBREGS). If possible, convert it into a series of
7355 logical operations.
7357 We half-heartedly support variable positions, but do not at all
7358 support variable lengths. */
7360 static const_rtx
7361 expand_field_assignment (const_rtx x)
7363 rtx inner;
7364 rtx pos; /* Always counts from low bit. */
7365 int len, inner_len;
7366 rtx mask, cleared, masked;
7367 scalar_int_mode compute_mode;
7369 /* Loop until we find something we can't simplify. */
7370 while (1)
7372 if (GET_CODE (SET_DEST (x)) == STRICT_LOW_PART
7373 && GET_CODE (XEXP (SET_DEST (x), 0)) == SUBREG)
7375 rtx x0 = XEXP (SET_DEST (x), 0);
7376 if (!GET_MODE_PRECISION (GET_MODE (x0)).is_constant (&len))
7377 break;
7378 inner = SUBREG_REG (XEXP (SET_DEST (x), 0));
7379 pos = gen_int_mode (subreg_lsb (XEXP (SET_DEST (x), 0)),
7380 MAX_MODE_INT);
7382 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT
7383 && CONST_INT_P (XEXP (SET_DEST (x), 1)))
7385 inner = XEXP (SET_DEST (x), 0);
7386 if (!GET_MODE_PRECISION (GET_MODE (inner)).is_constant (&inner_len))
7387 break;
7389 len = INTVAL (XEXP (SET_DEST (x), 1));
7390 pos = XEXP (SET_DEST (x), 2);
7392 /* A constant position should stay within the width of INNER. */
7393 if (CONST_INT_P (pos) && INTVAL (pos) + len > inner_len)
7394 break;
7396 if (BITS_BIG_ENDIAN)
7398 if (CONST_INT_P (pos))
7399 pos = GEN_INT (inner_len - len - INTVAL (pos));
7400 else if (GET_CODE (pos) == MINUS
7401 && CONST_INT_P (XEXP (pos, 1))
7402 && INTVAL (XEXP (pos, 1)) == inner_len - len)
7403 /* If position is ADJUST - X, new position is X. */
7404 pos = XEXP (pos, 0);
7405 else
7406 pos = simplify_gen_binary (MINUS, GET_MODE (pos),
7407 gen_int_mode (inner_len - len,
7408 GET_MODE (pos)),
7409 pos);
7413 /* If the destination is a subreg that overwrites the whole of the inner
7414 register, we can move the subreg to the source. */
7415 else if (GET_CODE (SET_DEST (x)) == SUBREG
7416 /* We need SUBREGs to compute nonzero_bits properly. */
7417 && nonzero_sign_valid
7418 && !read_modify_subreg_p (SET_DEST (x)))
7420 x = gen_rtx_SET (SUBREG_REG (SET_DEST (x)),
7421 gen_lowpart
7422 (GET_MODE (SUBREG_REG (SET_DEST (x))),
7423 SET_SRC (x)));
7424 continue;
7426 else
7427 break;
7429 while (GET_CODE (inner) == SUBREG && subreg_lowpart_p (inner))
7430 inner = SUBREG_REG (inner);
7432 /* Don't attempt bitwise arithmetic on non scalar integer modes. */
7433 if (!is_a <scalar_int_mode> (GET_MODE (inner), &compute_mode))
7435 /* Don't do anything for vector or complex integral types. */
7436 if (! FLOAT_MODE_P (GET_MODE (inner)))
7437 break;
7439 /* Try to find an integral mode to pun with. */
7440 if (!int_mode_for_size (GET_MODE_BITSIZE (GET_MODE (inner)), 0)
7441 .exists (&compute_mode))
7442 break;
7444 inner = gen_lowpart (compute_mode, inner);
7447 /* Compute a mask of LEN bits, if we can do this on the host machine. */
7448 if (len >= HOST_BITS_PER_WIDE_INT)
7449 break;
7451 /* Don't try to compute in too wide unsupported modes. */
7452 if (!targetm.scalar_mode_supported_p (compute_mode))
7453 break;
7455 /* Now compute the equivalent expression. Make a copy of INNER
7456 for the SET_DEST in case it is a MEM into which we will substitute;
7457 we don't want shared RTL in that case. */
7458 mask = gen_int_mode ((HOST_WIDE_INT_1U << len) - 1,
7459 compute_mode);
7460 cleared = simplify_gen_binary (AND, compute_mode,
7461 simplify_gen_unary (NOT, compute_mode,
7462 simplify_gen_binary (ASHIFT,
7463 compute_mode,
7464 mask, pos),
7465 compute_mode),
7466 inner);
7467 masked = simplify_gen_binary (ASHIFT, compute_mode,
7468 simplify_gen_binary (
7469 AND, compute_mode,
7470 gen_lowpart (compute_mode, SET_SRC (x)),
7471 mask),
7472 pos);
7474 x = gen_rtx_SET (copy_rtx (inner),
7475 simplify_gen_binary (IOR, compute_mode,
7476 cleared, masked));
7479 return x;
7482 /* Return an RTX for a reference to LEN bits of INNER. If POS_RTX is nonzero,
7483 it is an RTX that represents the (variable) starting position; otherwise,
7484 POS is the (constant) starting bit position. Both are counted from the LSB.
7486 UNSIGNEDP is nonzero for an unsigned reference and zero for a signed one.
7488 IN_DEST is nonzero if this is a reference in the destination of a SET.
7489 This is used when a ZERO_ or SIGN_EXTRACT isn't needed. If nonzero,
7490 a STRICT_LOW_PART will be used, if zero, ZERO_EXTEND or SIGN_EXTEND will
7491 be used.
7493 IN_COMPARE is nonzero if we are in a COMPARE. This means that a
7494 ZERO_EXTRACT should be built even for bits starting at bit 0.
7496 MODE is the desired mode of the result (if IN_DEST == 0).
7498 The result is an RTX for the extraction or NULL_RTX if the target
7499 can't handle it. */
7501 static rtx
7502 make_extraction (machine_mode mode, rtx inner, HOST_WIDE_INT pos,
7503 rtx pos_rtx, unsigned HOST_WIDE_INT len, int unsignedp,
7504 int in_dest, int in_compare)
7506 /* This mode describes the size of the storage area
7507 to fetch the overall value from. Within that, we
7508 ignore the POS lowest bits, etc. */
7509 machine_mode is_mode = GET_MODE (inner);
7510 machine_mode inner_mode;
7511 scalar_int_mode wanted_inner_mode;
7512 scalar_int_mode wanted_inner_reg_mode = word_mode;
7513 scalar_int_mode pos_mode = word_mode;
7514 machine_mode extraction_mode = word_mode;
7515 rtx new_rtx = 0;
7516 rtx orig_pos_rtx = pos_rtx;
7517 HOST_WIDE_INT orig_pos;
7519 if (pos_rtx && CONST_INT_P (pos_rtx))
7520 pos = INTVAL (pos_rtx), pos_rtx = 0;
7522 if (GET_CODE (inner) == SUBREG
7523 && subreg_lowpart_p (inner)
7524 && (paradoxical_subreg_p (inner)
7525 /* If trying or potentionally trying to extract
7526 bits outside of is_mode, don't look through
7527 non-paradoxical SUBREGs. See PR82192. */
7528 || (pos_rtx == NULL_RTX
7529 && known_le (pos + len, GET_MODE_PRECISION (is_mode)))))
7531 /* If going from (subreg:SI (mem:QI ...)) to (mem:QI ...),
7532 consider just the QI as the memory to extract from.
7533 The subreg adds or removes high bits; its mode is
7534 irrelevant to the meaning of this extraction,
7535 since POS and LEN count from the lsb. */
7536 if (MEM_P (SUBREG_REG (inner)))
7537 is_mode = GET_MODE (SUBREG_REG (inner));
7538 inner = SUBREG_REG (inner);
7540 else if (GET_CODE (inner) == ASHIFT
7541 && CONST_INT_P (XEXP (inner, 1))
7542 && pos_rtx == 0 && pos == 0
7543 && len > UINTVAL (XEXP (inner, 1)))
7545 /* We're extracting the least significant bits of an rtx
7546 (ashift X (const_int C)), where LEN > C. Extract the
7547 least significant (LEN - C) bits of X, giving an rtx
7548 whose mode is MODE, then shift it left C times. */
7549 new_rtx = make_extraction (mode, XEXP (inner, 0),
7550 0, 0, len - INTVAL (XEXP (inner, 1)),
7551 unsignedp, in_dest, in_compare);
7552 if (new_rtx != 0)
7553 return gen_rtx_ASHIFT (mode, new_rtx, XEXP (inner, 1));
7555 else if (GET_CODE (inner) == TRUNCATE
7556 /* If trying or potentionally trying to extract
7557 bits outside of is_mode, don't look through
7558 TRUNCATE. See PR82192. */
7559 && pos_rtx == NULL_RTX
7560 && known_le (pos + len, GET_MODE_PRECISION (is_mode)))
7561 inner = XEXP (inner, 0);
7563 inner_mode = GET_MODE (inner);
7565 /* See if this can be done without an extraction. We never can if the
7566 width of the field is not the same as that of some integer mode. For
7567 registers, we can only avoid the extraction if the position is at the
7568 low-order bit and this is either not in the destination or we have the
7569 appropriate STRICT_LOW_PART operation available.
7571 For MEM, we can avoid an extract if the field starts on an appropriate
7572 boundary and we can change the mode of the memory reference. */
7574 scalar_int_mode tmode;
7575 if (int_mode_for_size (len, 1).exists (&tmode)
7576 && ((pos_rtx == 0 && (pos % BITS_PER_WORD) == 0
7577 && !MEM_P (inner)
7578 && (pos == 0 || REG_P (inner))
7579 && (inner_mode == tmode
7580 || !REG_P (inner)
7581 || TRULY_NOOP_TRUNCATION_MODES_P (tmode, inner_mode)
7582 || reg_truncated_to_mode (tmode, inner))
7583 && (! in_dest
7584 || (REG_P (inner)
7585 && have_insn_for (STRICT_LOW_PART, tmode))))
7586 || (MEM_P (inner) && pos_rtx == 0
7587 && (pos
7588 % (STRICT_ALIGNMENT ? GET_MODE_ALIGNMENT (tmode)
7589 : BITS_PER_UNIT)) == 0
7590 /* We can't do this if we are widening INNER_MODE (it
7591 may not be aligned, for one thing). */
7592 && !paradoxical_subreg_p (tmode, inner_mode)
7593 && (inner_mode == tmode
7594 || (! mode_dependent_address_p (XEXP (inner, 0),
7595 MEM_ADDR_SPACE (inner))
7596 && ! MEM_VOLATILE_P (inner))))))
7598 /* If INNER is a MEM, make a new MEM that encompasses just the desired
7599 field. If the original and current mode are the same, we need not
7600 adjust the offset. Otherwise, we do if bytes big endian.
7602 If INNER is not a MEM, get a piece consisting of just the field
7603 of interest (in this case POS % BITS_PER_WORD must be 0). */
7605 if (MEM_P (inner))
7607 poly_int64 offset;
7609 /* POS counts from lsb, but make OFFSET count in memory order. */
7610 if (BYTES_BIG_ENDIAN)
7611 offset = bits_to_bytes_round_down (GET_MODE_PRECISION (is_mode)
7612 - len - pos);
7613 else
7614 offset = pos / BITS_PER_UNIT;
7616 new_rtx = adjust_address_nv (inner, tmode, offset);
7618 else if (REG_P (inner))
7620 if (tmode != inner_mode)
7622 /* We can't call gen_lowpart in a DEST since we
7623 always want a SUBREG (see below) and it would sometimes
7624 return a new hard register. */
7625 if (pos || in_dest)
7627 poly_uint64 offset
7628 = subreg_offset_from_lsb (tmode, inner_mode, pos);
7630 /* Avoid creating invalid subregs, for example when
7631 simplifying (x>>32)&255. */
7632 if (!validate_subreg (tmode, inner_mode, inner, offset))
7633 return NULL_RTX;
7635 new_rtx = gen_rtx_SUBREG (tmode, inner, offset);
7637 else
7638 new_rtx = gen_lowpart (tmode, inner);
7640 else
7641 new_rtx = inner;
7643 else
7644 new_rtx = force_to_mode (inner, tmode,
7645 len >= HOST_BITS_PER_WIDE_INT
7646 ? HOST_WIDE_INT_M1U
7647 : (HOST_WIDE_INT_1U << len) - 1, 0);
7649 /* If this extraction is going into the destination of a SET,
7650 make a STRICT_LOW_PART unless we made a MEM. */
7652 if (in_dest)
7653 return (MEM_P (new_rtx) ? new_rtx
7654 : (GET_CODE (new_rtx) != SUBREG
7655 ? gen_rtx_CLOBBER (tmode, const0_rtx)
7656 : gen_rtx_STRICT_LOW_PART (VOIDmode, new_rtx)));
7658 if (mode == tmode)
7659 return new_rtx;
7661 if (CONST_SCALAR_INT_P (new_rtx))
7662 return simplify_unary_operation (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7663 mode, new_rtx, tmode);
7665 /* If we know that no extraneous bits are set, and that the high
7666 bit is not set, convert the extraction to the cheaper of
7667 sign and zero extension, that are equivalent in these cases. */
7668 if (flag_expensive_optimizations
7669 && (HWI_COMPUTABLE_MODE_P (tmode)
7670 && ((nonzero_bits (new_rtx, tmode)
7671 & ~(((unsigned HOST_WIDE_INT)GET_MODE_MASK (tmode)) >> 1))
7672 == 0)))
7674 rtx temp = gen_rtx_ZERO_EXTEND (mode, new_rtx);
7675 rtx temp1 = gen_rtx_SIGN_EXTEND (mode, new_rtx);
7677 /* Prefer ZERO_EXTENSION, since it gives more information to
7678 backends. */
7679 if (set_src_cost (temp, mode, optimize_this_for_speed_p)
7680 <= set_src_cost (temp1, mode, optimize_this_for_speed_p))
7681 return temp;
7682 return temp1;
7685 /* Otherwise, sign- or zero-extend unless we already are in the
7686 proper mode. */
7688 return (gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND,
7689 mode, new_rtx));
7692 /* Unless this is a COMPARE or we have a funny memory reference,
7693 don't do anything with zero-extending field extracts starting at
7694 the low-order bit since they are simple AND operations. */
7695 if (pos_rtx == 0 && pos == 0 && ! in_dest
7696 && ! in_compare && unsignedp)
7697 return 0;
7699 /* Unless INNER is not MEM, reject this if we would be spanning bytes or
7700 if the position is not a constant and the length is not 1. In all
7701 other cases, we would only be going outside our object in cases when
7702 an original shift would have been undefined. */
7703 if (MEM_P (inner)
7704 && ((pos_rtx == 0 && maybe_gt (pos + len, GET_MODE_PRECISION (is_mode)))
7705 || (pos_rtx != 0 && len != 1)))
7706 return 0;
7708 enum extraction_pattern pattern = (in_dest ? EP_insv
7709 : unsignedp ? EP_extzv : EP_extv);
7711 /* If INNER is not from memory, we want it to have the mode of a register
7712 extraction pattern's structure operand, or word_mode if there is no
7713 such pattern. The same applies to extraction_mode and pos_mode
7714 and their respective operands.
7716 For memory, assume that the desired extraction_mode and pos_mode
7717 are the same as for a register operation, since at present we don't
7718 have named patterns for aligned memory structures. */
7719 struct extraction_insn insn;
7720 unsigned int inner_size;
7721 if (GET_MODE_BITSIZE (inner_mode).is_constant (&inner_size)
7722 && get_best_reg_extraction_insn (&insn, pattern, inner_size, mode))
7724 wanted_inner_reg_mode = insn.struct_mode.require ();
7725 pos_mode = insn.pos_mode;
7726 extraction_mode = insn.field_mode;
7729 /* Never narrow an object, since that might not be safe. */
7731 if (mode != VOIDmode
7732 && partial_subreg_p (extraction_mode, mode))
7733 extraction_mode = mode;
7735 if (!MEM_P (inner))
7736 wanted_inner_mode = wanted_inner_reg_mode;
7737 else
7739 /* Be careful not to go beyond the extracted object and maintain the
7740 natural alignment of the memory. */
7741 wanted_inner_mode = smallest_int_mode_for_size (len);
7742 while (pos % GET_MODE_BITSIZE (wanted_inner_mode) + len
7743 > GET_MODE_BITSIZE (wanted_inner_mode))
7744 wanted_inner_mode = GET_MODE_WIDER_MODE (wanted_inner_mode).require ();
7747 orig_pos = pos;
7749 if (BITS_BIG_ENDIAN)
7751 /* POS is passed as if BITS_BIG_ENDIAN == 0, so we need to convert it to
7752 BITS_BIG_ENDIAN style. If position is constant, compute new
7753 position. Otherwise, build subtraction.
7754 Note that POS is relative to the mode of the original argument.
7755 If it's a MEM we need to recompute POS relative to that.
7756 However, if we're extracting from (or inserting into) a register,
7757 we want to recompute POS relative to wanted_inner_mode. */
7758 int width;
7759 if (!MEM_P (inner))
7760 width = GET_MODE_BITSIZE (wanted_inner_mode);
7761 else if (!GET_MODE_BITSIZE (is_mode).is_constant (&width))
7762 return NULL_RTX;
7764 if (pos_rtx == 0)
7765 pos = width - len - pos;
7766 else
7767 pos_rtx
7768 = gen_rtx_MINUS (GET_MODE (pos_rtx),
7769 gen_int_mode (width - len, GET_MODE (pos_rtx)),
7770 pos_rtx);
7771 /* POS may be less than 0 now, but we check for that below.
7772 Note that it can only be less than 0 if !MEM_P (inner). */
7775 /* If INNER has a wider mode, and this is a constant extraction, try to
7776 make it smaller and adjust the byte to point to the byte containing
7777 the value. */
7778 if (wanted_inner_mode != VOIDmode
7779 && inner_mode != wanted_inner_mode
7780 && ! pos_rtx
7781 && partial_subreg_p (wanted_inner_mode, is_mode)
7782 && MEM_P (inner)
7783 && ! mode_dependent_address_p (XEXP (inner, 0), MEM_ADDR_SPACE (inner))
7784 && ! MEM_VOLATILE_P (inner))
7786 poly_int64 offset = 0;
7788 /* The computations below will be correct if the machine is big
7789 endian in both bits and bytes or little endian in bits and bytes.
7790 If it is mixed, we must adjust. */
7792 /* If bytes are big endian and we had a paradoxical SUBREG, we must
7793 adjust OFFSET to compensate. */
7794 if (BYTES_BIG_ENDIAN
7795 && paradoxical_subreg_p (is_mode, inner_mode))
7796 offset -= GET_MODE_SIZE (is_mode) - GET_MODE_SIZE (inner_mode);
7798 /* We can now move to the desired byte. */
7799 offset += (pos / GET_MODE_BITSIZE (wanted_inner_mode))
7800 * GET_MODE_SIZE (wanted_inner_mode);
7801 pos %= GET_MODE_BITSIZE (wanted_inner_mode);
7803 if (BYTES_BIG_ENDIAN != BITS_BIG_ENDIAN
7804 && is_mode != wanted_inner_mode)
7805 offset = (GET_MODE_SIZE (is_mode)
7806 - GET_MODE_SIZE (wanted_inner_mode) - offset);
7808 inner = adjust_address_nv (inner, wanted_inner_mode, offset);
7811 /* If INNER is not memory, get it into the proper mode. If we are changing
7812 its mode, POS must be a constant and smaller than the size of the new
7813 mode. */
7814 else if (!MEM_P (inner))
7816 /* On the LHS, don't create paradoxical subregs implicitely truncating
7817 the register unless TARGET_TRULY_NOOP_TRUNCATION. */
7818 if (in_dest
7819 && !TRULY_NOOP_TRUNCATION_MODES_P (GET_MODE (inner),
7820 wanted_inner_mode))
7821 return NULL_RTX;
7823 if (GET_MODE (inner) != wanted_inner_mode
7824 && (pos_rtx != 0
7825 || orig_pos + len > GET_MODE_BITSIZE (wanted_inner_mode)))
7826 return NULL_RTX;
7828 if (orig_pos < 0)
7829 return NULL_RTX;
7831 inner = force_to_mode (inner, wanted_inner_mode,
7832 pos_rtx
7833 || len + orig_pos >= HOST_BITS_PER_WIDE_INT
7834 ? HOST_WIDE_INT_M1U
7835 : (((HOST_WIDE_INT_1U << len) - 1)
7836 << orig_pos),
7840 /* Adjust mode of POS_RTX, if needed. If we want a wider mode, we
7841 have to zero extend. Otherwise, we can just use a SUBREG.
7843 We dealt with constant rtxes earlier, so pos_rtx cannot
7844 have VOIDmode at this point. */
7845 if (pos_rtx != 0
7846 && (GET_MODE_SIZE (pos_mode)
7847 > GET_MODE_SIZE (as_a <scalar_int_mode> (GET_MODE (pos_rtx)))))
7849 rtx temp = simplify_gen_unary (ZERO_EXTEND, pos_mode, pos_rtx,
7850 GET_MODE (pos_rtx));
7852 /* If we know that no extraneous bits are set, and that the high
7853 bit is not set, convert extraction to cheaper one - either
7854 SIGN_EXTENSION or ZERO_EXTENSION, that are equivalent in these
7855 cases. */
7856 if (flag_expensive_optimizations
7857 && (HWI_COMPUTABLE_MODE_P (GET_MODE (pos_rtx))
7858 && ((nonzero_bits (pos_rtx, GET_MODE (pos_rtx))
7859 & ~(((unsigned HOST_WIDE_INT)
7860 GET_MODE_MASK (GET_MODE (pos_rtx)))
7861 >> 1))
7862 == 0)))
7864 rtx temp1 = simplify_gen_unary (SIGN_EXTEND, pos_mode, pos_rtx,
7865 GET_MODE (pos_rtx));
7867 /* Prefer ZERO_EXTENSION, since it gives more information to
7868 backends. */
7869 if (set_src_cost (temp1, pos_mode, optimize_this_for_speed_p)
7870 < set_src_cost (temp, pos_mode, optimize_this_for_speed_p))
7871 temp = temp1;
7873 pos_rtx = temp;
7876 /* Make POS_RTX unless we already have it and it is correct. If we don't
7877 have a POS_RTX but we do have an ORIG_POS_RTX, the latter must
7878 be a CONST_INT. */
7879 if (pos_rtx == 0 && orig_pos_rtx != 0 && INTVAL (orig_pos_rtx) == pos)
7880 pos_rtx = orig_pos_rtx;
7882 else if (pos_rtx == 0)
7883 pos_rtx = GEN_INT (pos);
7885 /* Make the required operation. See if we can use existing rtx. */
7886 new_rtx = gen_rtx_fmt_eee (unsignedp ? ZERO_EXTRACT : SIGN_EXTRACT,
7887 extraction_mode, inner, GEN_INT (len), pos_rtx);
7888 if (! in_dest)
7889 new_rtx = gen_lowpart (mode, new_rtx);
7891 return new_rtx;
7894 /* See if X (of mode MODE) contains an ASHIFT of COUNT or more bits that
7895 can be commuted with any other operations in X. Return X without
7896 that shift if so. */
7898 static rtx
7899 extract_left_shift (scalar_int_mode mode, rtx x, int count)
7901 enum rtx_code code = GET_CODE (x);
7902 rtx tem;
7904 switch (code)
7906 case ASHIFT:
7907 /* This is the shift itself. If it is wide enough, we will return
7908 either the value being shifted if the shift count is equal to
7909 COUNT or a shift for the difference. */
7910 if (CONST_INT_P (XEXP (x, 1))
7911 && INTVAL (XEXP (x, 1)) >= count)
7912 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (x, 0),
7913 INTVAL (XEXP (x, 1)) - count);
7914 break;
7916 case NEG: case NOT:
7917 if ((tem = extract_left_shift (mode, XEXP (x, 0), count)) != 0)
7918 return simplify_gen_unary (code, mode, tem, mode);
7920 break;
7922 case PLUS: case IOR: case XOR: case AND:
7923 /* If we can safely shift this constant and we find the inner shift,
7924 make a new operation. */
7925 if (CONST_INT_P (XEXP (x, 1))
7926 && (UINTVAL (XEXP (x, 1))
7927 & (((HOST_WIDE_INT_1U << count)) - 1)) == 0
7928 && (tem = extract_left_shift (mode, XEXP (x, 0), count)) != 0)
7930 HOST_WIDE_INT val = INTVAL (XEXP (x, 1)) >> count;
7931 return simplify_gen_binary (code, mode, tem,
7932 gen_int_mode (val, mode));
7934 break;
7936 default:
7937 break;
7940 return 0;
7943 /* Subroutine of make_compound_operation. *X_PTR is the rtx at the current
7944 level of the expression and MODE is its mode. IN_CODE is as for
7945 make_compound_operation. *NEXT_CODE_PTR is the value of IN_CODE
7946 that should be used when recursing on operands of *X_PTR.
7948 There are two possible actions:
7950 - Return null. This tells the caller to recurse on *X_PTR with IN_CODE
7951 equal to *NEXT_CODE_PTR, after which *X_PTR holds the final value.
7953 - Return a new rtx, which the caller returns directly. */
7955 static rtx
7956 make_compound_operation_int (scalar_int_mode mode, rtx *x_ptr,
7957 enum rtx_code in_code,
7958 enum rtx_code *next_code_ptr)
7960 rtx x = *x_ptr;
7961 enum rtx_code next_code = *next_code_ptr;
7962 enum rtx_code code = GET_CODE (x);
7963 int mode_width = GET_MODE_PRECISION (mode);
7964 rtx rhs, lhs;
7965 rtx new_rtx = 0;
7966 int i;
7967 rtx tem;
7968 scalar_int_mode inner_mode;
7969 bool equality_comparison = false;
7971 if (in_code == EQ)
7973 equality_comparison = true;
7974 in_code = COMPARE;
7977 /* Process depending on the code of this operation. If NEW is set
7978 nonzero, it will be returned. */
7980 switch (code)
7982 case ASHIFT:
7983 /* Convert shifts by constants into multiplications if inside
7984 an address. */
7985 if (in_code == MEM && CONST_INT_P (XEXP (x, 1))
7986 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
7987 && INTVAL (XEXP (x, 1)) >= 0)
7989 HOST_WIDE_INT count = INTVAL (XEXP (x, 1));
7990 HOST_WIDE_INT multval = HOST_WIDE_INT_1 << count;
7992 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
7993 if (GET_CODE (new_rtx) == NEG)
7995 new_rtx = XEXP (new_rtx, 0);
7996 multval = -multval;
7998 multval = trunc_int_for_mode (multval, mode);
7999 new_rtx = gen_rtx_MULT (mode, new_rtx, gen_int_mode (multval, mode));
8001 break;
8003 case PLUS:
8004 lhs = XEXP (x, 0);
8005 rhs = XEXP (x, 1);
8006 lhs = make_compound_operation (lhs, next_code);
8007 rhs = make_compound_operation (rhs, next_code);
8008 if (GET_CODE (lhs) == MULT && GET_CODE (XEXP (lhs, 0)) == NEG)
8010 tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (lhs, 0), 0),
8011 XEXP (lhs, 1));
8012 new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
8014 else if (GET_CODE (lhs) == MULT
8015 && (CONST_INT_P (XEXP (lhs, 1)) && INTVAL (XEXP (lhs, 1)) < 0))
8017 tem = simplify_gen_binary (MULT, mode, XEXP (lhs, 0),
8018 simplify_gen_unary (NEG, mode,
8019 XEXP (lhs, 1),
8020 mode));
8021 new_rtx = simplify_gen_binary (MINUS, mode, rhs, tem);
8023 else
8025 SUBST (XEXP (x, 0), lhs);
8026 SUBST (XEXP (x, 1), rhs);
8028 maybe_swap_commutative_operands (x);
8029 return x;
8031 case MINUS:
8032 lhs = XEXP (x, 0);
8033 rhs = XEXP (x, 1);
8034 lhs = make_compound_operation (lhs, next_code);
8035 rhs = make_compound_operation (rhs, next_code);
8036 if (GET_CODE (rhs) == MULT && GET_CODE (XEXP (rhs, 0)) == NEG)
8038 tem = simplify_gen_binary (MULT, mode, XEXP (XEXP (rhs, 0), 0),
8039 XEXP (rhs, 1));
8040 return simplify_gen_binary (PLUS, mode, tem, lhs);
8042 else if (GET_CODE (rhs) == MULT
8043 && (CONST_INT_P (XEXP (rhs, 1)) && INTVAL (XEXP (rhs, 1)) < 0))
8045 tem = simplify_gen_binary (MULT, mode, XEXP (rhs, 0),
8046 simplify_gen_unary (NEG, mode,
8047 XEXP (rhs, 1),
8048 mode));
8049 return simplify_gen_binary (PLUS, mode, tem, lhs);
8051 else
8053 SUBST (XEXP (x, 0), lhs);
8054 SUBST (XEXP (x, 1), rhs);
8055 return x;
8058 case AND:
8059 /* If the second operand is not a constant, we can't do anything
8060 with it. */
8061 if (!CONST_INT_P (XEXP (x, 1)))
8062 break;
8064 /* If the constant is a power of two minus one and the first operand
8065 is a logical right shift, make an extraction. */
8066 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8067 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
8069 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
8070 new_rtx = make_extraction (mode, new_rtx, 0, XEXP (XEXP (x, 0), 1),
8071 i, 1, 0, in_code == COMPARE);
8074 /* Same as previous, but for (subreg (lshiftrt ...)) in first op. */
8075 else if (GET_CODE (XEXP (x, 0)) == SUBREG
8076 && subreg_lowpart_p (XEXP (x, 0))
8077 && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (XEXP (x, 0))),
8078 &inner_mode)
8079 && GET_CODE (SUBREG_REG (XEXP (x, 0))) == LSHIFTRT
8080 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
8082 rtx inner_x0 = SUBREG_REG (XEXP (x, 0));
8083 new_rtx = make_compound_operation (XEXP (inner_x0, 0), next_code);
8084 new_rtx = make_extraction (inner_mode, new_rtx, 0,
8085 XEXP (inner_x0, 1),
8086 i, 1, 0, in_code == COMPARE);
8088 /* If we narrowed the mode when dropping the subreg, then we lose. */
8089 if (GET_MODE_SIZE (inner_mode) < GET_MODE_SIZE (mode))
8090 new_rtx = NULL;
8092 /* If that didn't give anything, see if the AND simplifies on
8093 its own. */
8094 if (!new_rtx && i >= 0)
8096 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
8097 new_rtx = make_extraction (mode, new_rtx, 0, NULL_RTX, i, 1,
8098 0, in_code == COMPARE);
8101 /* Same as previous, but for (xor/ior (lshiftrt...) (lshiftrt...)). */
8102 else if ((GET_CODE (XEXP (x, 0)) == XOR
8103 || GET_CODE (XEXP (x, 0)) == IOR)
8104 && GET_CODE (XEXP (XEXP (x, 0), 0)) == LSHIFTRT
8105 && GET_CODE (XEXP (XEXP (x, 0), 1)) == LSHIFTRT
8106 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
8108 /* Apply the distributive law, and then try to make extractions. */
8109 new_rtx = gen_rtx_fmt_ee (GET_CODE (XEXP (x, 0)), mode,
8110 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 0),
8111 XEXP (x, 1)),
8112 gen_rtx_AND (mode, XEXP (XEXP (x, 0), 1),
8113 XEXP (x, 1)));
8114 new_rtx = make_compound_operation (new_rtx, in_code);
8117 /* If we are have (and (rotate X C) M) and C is larger than the number
8118 of bits in M, this is an extraction. */
8120 else if (GET_CODE (XEXP (x, 0)) == ROTATE
8121 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8122 && (i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0
8123 && i <= INTVAL (XEXP (XEXP (x, 0), 1)))
8125 new_rtx = make_compound_operation (XEXP (XEXP (x, 0), 0), next_code);
8126 new_rtx = make_extraction (mode, new_rtx,
8127 (GET_MODE_PRECISION (mode)
8128 - INTVAL (XEXP (XEXP (x, 0), 1))),
8129 NULL_RTX, i, 1, 0, in_code == COMPARE);
8132 /* On machines without logical shifts, if the operand of the AND is
8133 a logical shift and our mask turns off all the propagated sign
8134 bits, we can replace the logical shift with an arithmetic shift. */
8135 else if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8136 && !have_insn_for (LSHIFTRT, mode)
8137 && have_insn_for (ASHIFTRT, mode)
8138 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8139 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8140 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
8141 && mode_width <= HOST_BITS_PER_WIDE_INT)
8143 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
8145 mask >>= INTVAL (XEXP (XEXP (x, 0), 1));
8146 if ((INTVAL (XEXP (x, 1)) & ~mask) == 0)
8147 SUBST (XEXP (x, 0),
8148 gen_rtx_ASHIFTRT (mode,
8149 make_compound_operation (XEXP (XEXP (x,
8152 next_code),
8153 XEXP (XEXP (x, 0), 1)));
8156 /* If the constant is one less than a power of two, this might be
8157 representable by an extraction even if no shift is present.
8158 If it doesn't end up being a ZERO_EXTEND, we will ignore it unless
8159 we are in a COMPARE. */
8160 else if ((i = exact_log2 (UINTVAL (XEXP (x, 1)) + 1)) >= 0)
8161 new_rtx = make_extraction (mode,
8162 make_compound_operation (XEXP (x, 0),
8163 next_code),
8164 0, NULL_RTX, i, 1, 0, in_code == COMPARE);
8166 /* If we are in a comparison and this is an AND with a power of two,
8167 convert this into the appropriate bit extract. */
8168 else if (in_code == COMPARE
8169 && (i = exact_log2 (UINTVAL (XEXP (x, 1)))) >= 0
8170 && (equality_comparison || i < GET_MODE_PRECISION (mode) - 1))
8171 new_rtx = make_extraction (mode,
8172 make_compound_operation (XEXP (x, 0),
8173 next_code),
8174 i, NULL_RTX, 1, 1, 0, 1);
8176 /* If the one operand is a paradoxical subreg of a register or memory and
8177 the constant (limited to the smaller mode) has only zero bits where
8178 the sub expression has known zero bits, this can be expressed as
8179 a zero_extend. */
8180 else if (GET_CODE (XEXP (x, 0)) == SUBREG)
8182 rtx sub;
8184 sub = XEXP (XEXP (x, 0), 0);
8185 machine_mode sub_mode = GET_MODE (sub);
8186 int sub_width;
8187 if ((REG_P (sub) || MEM_P (sub))
8188 && GET_MODE_PRECISION (sub_mode).is_constant (&sub_width)
8189 && sub_width < mode_width)
8191 unsigned HOST_WIDE_INT mode_mask = GET_MODE_MASK (sub_mode);
8192 unsigned HOST_WIDE_INT mask;
8194 /* original AND constant with all the known zero bits set */
8195 mask = UINTVAL (XEXP (x, 1)) | (~nonzero_bits (sub, sub_mode));
8196 if ((mask & mode_mask) == mode_mask)
8198 new_rtx = make_compound_operation (sub, next_code);
8199 new_rtx = make_extraction (mode, new_rtx, 0, 0, sub_width,
8200 1, 0, in_code == COMPARE);
8205 break;
8207 case LSHIFTRT:
8208 /* If the sign bit is known to be zero, replace this with an
8209 arithmetic shift. */
8210 if (have_insn_for (ASHIFTRT, mode)
8211 && ! have_insn_for (LSHIFTRT, mode)
8212 && mode_width <= HOST_BITS_PER_WIDE_INT
8213 && (nonzero_bits (XEXP (x, 0), mode) & (1 << (mode_width - 1))) == 0)
8215 new_rtx = gen_rtx_ASHIFTRT (mode,
8216 make_compound_operation (XEXP (x, 0),
8217 next_code),
8218 XEXP (x, 1));
8219 break;
8222 /* fall through */
8224 case ASHIFTRT:
8225 lhs = XEXP (x, 0);
8226 rhs = XEXP (x, 1);
8228 /* If we have (ashiftrt (ashift foo C1) C2) with C2 >= C1,
8229 this is a SIGN_EXTRACT. */
8230 if (CONST_INT_P (rhs)
8231 && GET_CODE (lhs) == ASHIFT
8232 && CONST_INT_P (XEXP (lhs, 1))
8233 && INTVAL (rhs) >= INTVAL (XEXP (lhs, 1))
8234 && INTVAL (XEXP (lhs, 1)) >= 0
8235 && INTVAL (rhs) < mode_width)
8237 new_rtx = make_compound_operation (XEXP (lhs, 0), next_code);
8238 new_rtx = make_extraction (mode, new_rtx,
8239 INTVAL (rhs) - INTVAL (XEXP (lhs, 1)),
8240 NULL_RTX, mode_width - INTVAL (rhs),
8241 code == LSHIFTRT, 0, in_code == COMPARE);
8242 break;
8245 /* See if we have operations between an ASHIFTRT and an ASHIFT.
8246 If so, try to merge the shifts into a SIGN_EXTEND. We could
8247 also do this for some cases of SIGN_EXTRACT, but it doesn't
8248 seem worth the effort; the case checked for occurs on Alpha. */
8250 if (!OBJECT_P (lhs)
8251 && ! (GET_CODE (lhs) == SUBREG
8252 && (OBJECT_P (SUBREG_REG (lhs))))
8253 && CONST_INT_P (rhs)
8254 && INTVAL (rhs) >= 0
8255 && INTVAL (rhs) < HOST_BITS_PER_WIDE_INT
8256 && INTVAL (rhs) < mode_width
8257 && (new_rtx = extract_left_shift (mode, lhs, INTVAL (rhs))) != 0)
8258 new_rtx = make_extraction (mode, make_compound_operation (new_rtx,
8259 next_code),
8260 0, NULL_RTX, mode_width - INTVAL (rhs),
8261 code == LSHIFTRT, 0, in_code == COMPARE);
8263 break;
8265 case SUBREG:
8266 /* Call ourselves recursively on the inner expression. If we are
8267 narrowing the object and it has a different RTL code from
8268 what it originally did, do this SUBREG as a force_to_mode. */
8270 rtx inner = SUBREG_REG (x), simplified;
8271 enum rtx_code subreg_code = in_code;
8273 /* If the SUBREG is masking of a logical right shift,
8274 make an extraction. */
8275 if (GET_CODE (inner) == LSHIFTRT
8276 && is_a <scalar_int_mode> (GET_MODE (inner), &inner_mode)
8277 && GET_MODE_SIZE (mode) < GET_MODE_SIZE (inner_mode)
8278 && CONST_INT_P (XEXP (inner, 1))
8279 && UINTVAL (XEXP (inner, 1)) < GET_MODE_PRECISION (inner_mode)
8280 && subreg_lowpart_p (x))
8282 new_rtx = make_compound_operation (XEXP (inner, 0), next_code);
8283 int width = GET_MODE_PRECISION (inner_mode)
8284 - INTVAL (XEXP (inner, 1));
8285 if (width > mode_width)
8286 width = mode_width;
8287 new_rtx = make_extraction (mode, new_rtx, 0, XEXP (inner, 1),
8288 width, 1, 0, in_code == COMPARE);
8289 break;
8292 /* If in_code is COMPARE, it isn't always safe to pass it through
8293 to the recursive make_compound_operation call. */
8294 if (subreg_code == COMPARE
8295 && (!subreg_lowpart_p (x)
8296 || GET_CODE (inner) == SUBREG
8297 /* (subreg:SI (and:DI (reg:DI) (const_int 0x800000000)) 0)
8298 is (const_int 0), rather than
8299 (subreg:SI (lshiftrt:DI (reg:DI) (const_int 35)) 0).
8300 Similarly (subreg:QI (and:SI (reg:SI) (const_int 0x80)) 0)
8301 for non-equality comparisons against 0 is not equivalent
8302 to (subreg:QI (lshiftrt:SI (reg:SI) (const_int 7)) 0). */
8303 || (GET_CODE (inner) == AND
8304 && CONST_INT_P (XEXP (inner, 1))
8305 && partial_subreg_p (x)
8306 && exact_log2 (UINTVAL (XEXP (inner, 1)))
8307 >= GET_MODE_BITSIZE (mode) - 1)))
8308 subreg_code = SET;
8310 tem = make_compound_operation (inner, subreg_code);
8312 simplified
8313 = simplify_subreg (mode, tem, GET_MODE (inner), SUBREG_BYTE (x));
8314 if (simplified)
8315 tem = simplified;
8317 if (GET_CODE (tem) != GET_CODE (inner)
8318 && partial_subreg_p (x)
8319 && subreg_lowpart_p (x))
8321 rtx newer
8322 = force_to_mode (tem, mode, HOST_WIDE_INT_M1U, 0);
8324 /* If we have something other than a SUBREG, we might have
8325 done an expansion, so rerun ourselves. */
8326 if (GET_CODE (newer) != SUBREG)
8327 newer = make_compound_operation (newer, in_code);
8329 /* force_to_mode can expand compounds. If it just re-expanded
8330 the compound, use gen_lowpart to convert to the desired
8331 mode. */
8332 if (rtx_equal_p (newer, x)
8333 /* Likewise if it re-expanded the compound only partially.
8334 This happens for SUBREG of ZERO_EXTRACT if they extract
8335 the same number of bits. */
8336 || (GET_CODE (newer) == SUBREG
8337 && (GET_CODE (SUBREG_REG (newer)) == LSHIFTRT
8338 || GET_CODE (SUBREG_REG (newer)) == ASHIFTRT)
8339 && GET_CODE (inner) == AND
8340 && rtx_equal_p (SUBREG_REG (newer), XEXP (inner, 0))))
8341 return gen_lowpart (GET_MODE (x), tem);
8343 return newer;
8346 if (simplified)
8347 return tem;
8349 break;
8351 default:
8352 break;
8355 if (new_rtx)
8356 *x_ptr = gen_lowpart (mode, new_rtx);
8357 *next_code_ptr = next_code;
8358 return NULL_RTX;
8361 /* Look at the expression rooted at X. Look for expressions
8362 equivalent to ZERO_EXTRACT, SIGN_EXTRACT, ZERO_EXTEND, SIGN_EXTEND.
8363 Form these expressions.
8365 Return the new rtx, usually just X.
8367 Also, for machines like the VAX that don't have logical shift insns,
8368 try to convert logical to arithmetic shift operations in cases where
8369 they are equivalent. This undoes the canonicalizations to logical
8370 shifts done elsewhere.
8372 We try, as much as possible, to re-use rtl expressions to save memory.
8374 IN_CODE says what kind of expression we are processing. Normally, it is
8375 SET. In a memory address it is MEM. When processing the arguments of
8376 a comparison or a COMPARE against zero, it is COMPARE, or EQ if more
8377 precisely it is an equality comparison against zero. */
8380 make_compound_operation (rtx x, enum rtx_code in_code)
8382 enum rtx_code code = GET_CODE (x);
8383 const char *fmt;
8384 int i, j;
8385 enum rtx_code next_code;
8386 rtx new_rtx, tem;
8388 /* Select the code to be used in recursive calls. Once we are inside an
8389 address, we stay there. If we have a comparison, set to COMPARE,
8390 but once inside, go back to our default of SET. */
8392 next_code = (code == MEM ? MEM
8393 : ((code == COMPARE || COMPARISON_P (x))
8394 && XEXP (x, 1) == const0_rtx) ? COMPARE
8395 : in_code == COMPARE || in_code == EQ ? SET : in_code);
8397 scalar_int_mode mode;
8398 if (is_a <scalar_int_mode> (GET_MODE (x), &mode))
8400 rtx new_rtx = make_compound_operation_int (mode, &x, in_code,
8401 &next_code);
8402 if (new_rtx)
8403 return new_rtx;
8404 code = GET_CODE (x);
8407 /* Now recursively process each operand of this operation. We need to
8408 handle ZERO_EXTEND specially so that we don't lose track of the
8409 inner mode. */
8410 if (code == ZERO_EXTEND)
8412 new_rtx = make_compound_operation (XEXP (x, 0), next_code);
8413 tem = simplify_const_unary_operation (ZERO_EXTEND, GET_MODE (x),
8414 new_rtx, GET_MODE (XEXP (x, 0)));
8415 if (tem)
8416 return tem;
8417 SUBST (XEXP (x, 0), new_rtx);
8418 return x;
8421 fmt = GET_RTX_FORMAT (code);
8422 for (i = 0; i < GET_RTX_LENGTH (code); i++)
8423 if (fmt[i] == 'e')
8425 new_rtx = make_compound_operation (XEXP (x, i), next_code);
8426 SUBST (XEXP (x, i), new_rtx);
8428 else if (fmt[i] == 'E')
8429 for (j = 0; j < XVECLEN (x, i); j++)
8431 new_rtx = make_compound_operation (XVECEXP (x, i, j), next_code);
8432 SUBST (XVECEXP (x, i, j), new_rtx);
8435 maybe_swap_commutative_operands (x);
8436 return x;
8439 /* Given M see if it is a value that would select a field of bits
8440 within an item, but not the entire word. Return -1 if not.
8441 Otherwise, return the starting position of the field, where 0 is the
8442 low-order bit.
8444 *PLEN is set to the length of the field. */
8446 static int
8447 get_pos_from_mask (unsigned HOST_WIDE_INT m, unsigned HOST_WIDE_INT *plen)
8449 /* Get the bit number of the first 1 bit from the right, -1 if none. */
8450 int pos = m ? ctz_hwi (m) : -1;
8451 int len = 0;
8453 if (pos >= 0)
8454 /* Now shift off the low-order zero bits and see if we have a
8455 power of two minus 1. */
8456 len = exact_log2 ((m >> pos) + 1);
8458 if (len <= 0)
8459 pos = -1;
8461 *plen = len;
8462 return pos;
8465 /* If X refers to a register that equals REG in value, replace these
8466 references with REG. */
8467 static rtx
8468 canon_reg_for_combine (rtx x, rtx reg)
8470 rtx op0, op1, op2;
8471 const char *fmt;
8472 int i;
8473 bool copied;
8475 enum rtx_code code = GET_CODE (x);
8476 switch (GET_RTX_CLASS (code))
8478 case RTX_UNARY:
8479 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8480 if (op0 != XEXP (x, 0))
8481 return simplify_gen_unary (GET_CODE (x), GET_MODE (x), op0,
8482 GET_MODE (reg));
8483 break;
8485 case RTX_BIN_ARITH:
8486 case RTX_COMM_ARITH:
8487 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8488 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8489 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8490 return simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
8491 break;
8493 case RTX_COMPARE:
8494 case RTX_COMM_COMPARE:
8495 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8496 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8497 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8498 return simplify_gen_relational (GET_CODE (x), GET_MODE (x),
8499 GET_MODE (op0), op0, op1);
8500 break;
8502 case RTX_TERNARY:
8503 case RTX_BITFIELD_OPS:
8504 op0 = canon_reg_for_combine (XEXP (x, 0), reg);
8505 op1 = canon_reg_for_combine (XEXP (x, 1), reg);
8506 op2 = canon_reg_for_combine (XEXP (x, 2), reg);
8507 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1) || op2 != XEXP (x, 2))
8508 return simplify_gen_ternary (GET_CODE (x), GET_MODE (x),
8509 GET_MODE (op0), op0, op1, op2);
8510 /* FALLTHRU */
8512 case RTX_OBJ:
8513 if (REG_P (x))
8515 if (rtx_equal_p (get_last_value (reg), x)
8516 || rtx_equal_p (reg, get_last_value (x)))
8517 return reg;
8518 else
8519 break;
8522 /* fall through */
8524 default:
8525 fmt = GET_RTX_FORMAT (code);
8526 copied = false;
8527 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
8528 if (fmt[i] == 'e')
8530 rtx op = canon_reg_for_combine (XEXP (x, i), reg);
8531 if (op != XEXP (x, i))
8533 if (!copied)
8535 copied = true;
8536 x = copy_rtx (x);
8538 XEXP (x, i) = op;
8541 else if (fmt[i] == 'E')
8543 int j;
8544 for (j = 0; j < XVECLEN (x, i); j++)
8546 rtx op = canon_reg_for_combine (XVECEXP (x, i, j), reg);
8547 if (op != XVECEXP (x, i, j))
8549 if (!copied)
8551 copied = true;
8552 x = copy_rtx (x);
8554 XVECEXP (x, i, j) = op;
8559 break;
8562 return x;
8565 /* Return X converted to MODE. If the value is already truncated to
8566 MODE we can just return a subreg even though in the general case we
8567 would need an explicit truncation. */
8569 static rtx
8570 gen_lowpart_or_truncate (machine_mode mode, rtx x)
8572 if (!CONST_INT_P (x)
8573 && partial_subreg_p (mode, GET_MODE (x))
8574 && !TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (x))
8575 && !(REG_P (x) && reg_truncated_to_mode (mode, x)))
8577 /* Bit-cast X into an integer mode. */
8578 if (!SCALAR_INT_MODE_P (GET_MODE (x)))
8579 x = gen_lowpart (int_mode_for_mode (GET_MODE (x)).require (), x);
8580 x = simplify_gen_unary (TRUNCATE, int_mode_for_mode (mode).require (),
8581 x, GET_MODE (x));
8584 return gen_lowpart (mode, x);
8587 /* See if X can be simplified knowing that we will only refer to it in
8588 MODE and will only refer to those bits that are nonzero in MASK.
8589 If other bits are being computed or if masking operations are done
8590 that select a superset of the bits in MASK, they can sometimes be
8591 ignored.
8593 Return a possibly simplified expression, but always convert X to
8594 MODE. If X is a CONST_INT, AND the CONST_INT with MASK.
8596 If JUST_SELECT is nonzero, don't optimize by noticing that bits in MASK
8597 are all off in X. This is used when X will be complemented, by either
8598 NOT, NEG, or XOR. */
8600 static rtx
8601 force_to_mode (rtx x, machine_mode mode, unsigned HOST_WIDE_INT mask,
8602 int just_select)
8604 enum rtx_code code = GET_CODE (x);
8605 int next_select = just_select || code == XOR || code == NOT || code == NEG;
8606 machine_mode op_mode;
8607 unsigned HOST_WIDE_INT nonzero;
8609 /* If this is a CALL or ASM_OPERANDS, don't do anything. Some of the
8610 code below will do the wrong thing since the mode of such an
8611 expression is VOIDmode.
8613 Also do nothing if X is a CLOBBER; this can happen if X was
8614 the return value from a call to gen_lowpart. */
8615 if (code == CALL || code == ASM_OPERANDS || code == CLOBBER)
8616 return x;
8618 /* We want to perform the operation in its present mode unless we know
8619 that the operation is valid in MODE, in which case we do the operation
8620 in MODE. */
8621 op_mode = ((GET_MODE_CLASS (mode) == GET_MODE_CLASS (GET_MODE (x))
8622 && have_insn_for (code, mode))
8623 ? mode : GET_MODE (x));
8625 /* It is not valid to do a right-shift in a narrower mode
8626 than the one it came in with. */
8627 if ((code == LSHIFTRT || code == ASHIFTRT)
8628 && partial_subreg_p (mode, GET_MODE (x)))
8629 op_mode = GET_MODE (x);
8631 /* Truncate MASK to fit OP_MODE. */
8632 if (op_mode)
8633 mask &= GET_MODE_MASK (op_mode);
8635 /* Determine what bits of X are guaranteed to be (non)zero. */
8636 nonzero = nonzero_bits (x, mode);
8638 /* If none of the bits in X are needed, return a zero. */
8639 if (!just_select && (nonzero & mask) == 0 && !side_effects_p (x))
8640 x = const0_rtx;
8642 /* If X is a CONST_INT, return a new one. Do this here since the
8643 test below will fail. */
8644 if (CONST_INT_P (x))
8646 if (SCALAR_INT_MODE_P (mode))
8647 return gen_int_mode (INTVAL (x) & mask, mode);
8648 else
8650 x = GEN_INT (INTVAL (x) & mask);
8651 return gen_lowpart_common (mode, x);
8655 /* If X is narrower than MODE and we want all the bits in X's mode, just
8656 get X in the proper mode. */
8657 if (paradoxical_subreg_p (mode, GET_MODE (x))
8658 && (GET_MODE_MASK (GET_MODE (x)) & ~mask) == 0)
8659 return gen_lowpart (mode, x);
8661 /* We can ignore the effect of a SUBREG if it narrows the mode or
8662 if the constant masks to zero all the bits the mode doesn't have. */
8663 if (GET_CODE (x) == SUBREG
8664 && subreg_lowpart_p (x)
8665 && (partial_subreg_p (x)
8666 || (mask
8667 & GET_MODE_MASK (GET_MODE (x))
8668 & ~GET_MODE_MASK (GET_MODE (SUBREG_REG (x)))) == 0))
8669 return force_to_mode (SUBREG_REG (x), mode, mask, next_select);
8671 scalar_int_mode int_mode, xmode;
8672 if (is_a <scalar_int_mode> (mode, &int_mode)
8673 && is_a <scalar_int_mode> (GET_MODE (x), &xmode))
8674 /* OP_MODE is either MODE or XMODE, so it must be a scalar
8675 integer too. */
8676 return force_int_to_mode (x, int_mode, xmode,
8677 as_a <scalar_int_mode> (op_mode),
8678 mask, just_select);
8680 return gen_lowpart_or_truncate (mode, x);
8683 /* Subroutine of force_to_mode that handles cases in which both X and
8684 the result are scalar integers. MODE is the mode of the result,
8685 XMODE is the mode of X, and OP_MODE says which of MODE or XMODE
8686 is preferred for simplified versions of X. The other arguments
8687 are as for force_to_mode. */
8689 static rtx
8690 force_int_to_mode (rtx x, scalar_int_mode mode, scalar_int_mode xmode,
8691 scalar_int_mode op_mode, unsigned HOST_WIDE_INT mask,
8692 int just_select)
8694 enum rtx_code code = GET_CODE (x);
8695 int next_select = just_select || code == XOR || code == NOT || code == NEG;
8696 unsigned HOST_WIDE_INT fuller_mask;
8697 rtx op0, op1, temp;
8699 /* When we have an arithmetic operation, or a shift whose count we
8700 do not know, we need to assume that all bits up to the highest-order
8701 bit in MASK will be needed. This is how we form such a mask. */
8702 if (mask & (HOST_WIDE_INT_1U << (HOST_BITS_PER_WIDE_INT - 1)))
8703 fuller_mask = HOST_WIDE_INT_M1U;
8704 else
8705 fuller_mask = ((HOST_WIDE_INT_1U << (floor_log2 (mask) + 1))
8706 - 1);
8708 switch (code)
8710 case CLOBBER:
8711 /* If X is a (clobber (const_int)), return it since we know we are
8712 generating something that won't match. */
8713 return x;
8715 case SIGN_EXTEND:
8716 case ZERO_EXTEND:
8717 case ZERO_EXTRACT:
8718 case SIGN_EXTRACT:
8719 x = expand_compound_operation (x);
8720 if (GET_CODE (x) != code)
8721 return force_to_mode (x, mode, mask, next_select);
8722 break;
8724 case TRUNCATE:
8725 /* Similarly for a truncate. */
8726 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8728 case AND:
8729 /* If this is an AND with a constant, convert it into an AND
8730 whose constant is the AND of that constant with MASK. If it
8731 remains an AND of MASK, delete it since it is redundant. */
8733 if (CONST_INT_P (XEXP (x, 1)))
8735 x = simplify_and_const_int (x, op_mode, XEXP (x, 0),
8736 mask & INTVAL (XEXP (x, 1)));
8737 xmode = op_mode;
8739 /* If X is still an AND, see if it is an AND with a mask that
8740 is just some low-order bits. If so, and it is MASK, we don't
8741 need it. */
8743 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8744 && (INTVAL (XEXP (x, 1)) & GET_MODE_MASK (xmode)) == mask)
8745 x = XEXP (x, 0);
8747 /* If it remains an AND, try making another AND with the bits
8748 in the mode mask that aren't in MASK turned on. If the
8749 constant in the AND is wide enough, this might make a
8750 cheaper constant. */
8752 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1))
8753 && GET_MODE_MASK (xmode) != mask
8754 && HWI_COMPUTABLE_MODE_P (xmode))
8756 unsigned HOST_WIDE_INT cval
8757 = UINTVAL (XEXP (x, 1)) | (GET_MODE_MASK (xmode) & ~mask);
8758 rtx y;
8760 y = simplify_gen_binary (AND, xmode, XEXP (x, 0),
8761 gen_int_mode (cval, xmode));
8762 if (set_src_cost (y, xmode, optimize_this_for_speed_p)
8763 < set_src_cost (x, xmode, optimize_this_for_speed_p))
8764 x = y;
8767 break;
8770 goto binop;
8772 case PLUS:
8773 /* In (and (plus FOO C1) M), if M is a mask that just turns off
8774 low-order bits (as in an alignment operation) and FOO is already
8775 aligned to that boundary, mask C1 to that boundary as well.
8776 This may eliminate that PLUS and, later, the AND. */
8779 unsigned int width = GET_MODE_PRECISION (mode);
8780 unsigned HOST_WIDE_INT smask = mask;
8782 /* If MODE is narrower than HOST_WIDE_INT and mask is a negative
8783 number, sign extend it. */
8785 if (width < HOST_BITS_PER_WIDE_INT
8786 && (smask & (HOST_WIDE_INT_1U << (width - 1))) != 0)
8787 smask |= HOST_WIDE_INT_M1U << width;
8789 if (CONST_INT_P (XEXP (x, 1))
8790 && pow2p_hwi (- smask)
8791 && (nonzero_bits (XEXP (x, 0), mode) & ~smask) == 0
8792 && (INTVAL (XEXP (x, 1)) & ~smask) != 0)
8793 return force_to_mode (plus_constant (xmode, XEXP (x, 0),
8794 (INTVAL (XEXP (x, 1)) & smask)),
8795 mode, smask, next_select);
8798 /* fall through */
8800 case MULT:
8801 /* Substituting into the operands of a widening MULT is not likely to
8802 create RTL matching a machine insn. */
8803 if (code == MULT
8804 && (GET_CODE (XEXP (x, 0)) == ZERO_EXTEND
8805 || GET_CODE (XEXP (x, 0)) == SIGN_EXTEND)
8806 && (GET_CODE (XEXP (x, 1)) == ZERO_EXTEND
8807 || GET_CODE (XEXP (x, 1)) == SIGN_EXTEND)
8808 && REG_P (XEXP (XEXP (x, 0), 0))
8809 && REG_P (XEXP (XEXP (x, 1), 0)))
8810 return gen_lowpart_or_truncate (mode, x);
8812 /* For PLUS, MINUS and MULT, we need any bits less significant than the
8813 most significant bit in MASK since carries from those bits will
8814 affect the bits we are interested in. */
8815 mask = fuller_mask;
8816 goto binop;
8818 case MINUS:
8819 /* If X is (minus C Y) where C's least set bit is larger than any bit
8820 in the mask, then we may replace with (neg Y). */
8821 if (CONST_INT_P (XEXP (x, 0))
8822 && least_bit_hwi (UINTVAL (XEXP (x, 0))) > mask)
8824 x = simplify_gen_unary (NEG, xmode, XEXP (x, 1), xmode);
8825 return force_to_mode (x, mode, mask, next_select);
8828 /* Similarly, if C contains every bit in the fuller_mask, then we may
8829 replace with (not Y). */
8830 if (CONST_INT_P (XEXP (x, 0))
8831 && ((UINTVAL (XEXP (x, 0)) | fuller_mask) == UINTVAL (XEXP (x, 0))))
8833 x = simplify_gen_unary (NOT, xmode, XEXP (x, 1), xmode);
8834 return force_to_mode (x, mode, mask, next_select);
8837 mask = fuller_mask;
8838 goto binop;
8840 case IOR:
8841 case XOR:
8842 /* If X is (ior (lshiftrt FOO C1) C2), try to commute the IOR and
8843 LSHIFTRT so we end up with an (and (lshiftrt (ior ...) ...) ...)
8844 operation which may be a bitfield extraction. Ensure that the
8845 constant we form is not wider than the mode of X. */
8847 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
8848 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
8849 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
8850 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT
8851 && CONST_INT_P (XEXP (x, 1))
8852 && ((INTVAL (XEXP (XEXP (x, 0), 1))
8853 + floor_log2 (INTVAL (XEXP (x, 1))))
8854 < GET_MODE_PRECISION (xmode))
8855 && (UINTVAL (XEXP (x, 1))
8856 & ~nonzero_bits (XEXP (x, 0), xmode)) == 0)
8858 temp = gen_int_mode ((INTVAL (XEXP (x, 1)) & mask)
8859 << INTVAL (XEXP (XEXP (x, 0), 1)),
8860 xmode);
8861 temp = simplify_gen_binary (GET_CODE (x), xmode,
8862 XEXP (XEXP (x, 0), 0), temp);
8863 x = simplify_gen_binary (LSHIFTRT, xmode, temp,
8864 XEXP (XEXP (x, 0), 1));
8865 return force_to_mode (x, mode, mask, next_select);
8868 binop:
8869 /* For most binary operations, just propagate into the operation and
8870 change the mode if we have an operation of that mode. */
8872 op0 = force_to_mode (XEXP (x, 0), mode, mask, next_select);
8873 op1 = force_to_mode (XEXP (x, 1), mode, mask, next_select);
8875 /* If we ended up truncating both operands, truncate the result of the
8876 operation instead. */
8877 if (GET_CODE (op0) == TRUNCATE
8878 && GET_CODE (op1) == TRUNCATE)
8880 op0 = XEXP (op0, 0);
8881 op1 = XEXP (op1, 0);
8884 op0 = gen_lowpart_or_truncate (op_mode, op0);
8885 op1 = gen_lowpart_or_truncate (op_mode, op1);
8887 if (op_mode != xmode || op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
8889 x = simplify_gen_binary (code, op_mode, op0, op1);
8890 xmode = op_mode;
8892 break;
8894 case ASHIFT:
8895 /* For left shifts, do the same, but just for the first operand.
8896 However, we cannot do anything with shifts where we cannot
8897 guarantee that the counts are smaller than the size of the mode
8898 because such a count will have a different meaning in a
8899 wider mode. */
8901 if (! (CONST_INT_P (XEXP (x, 1))
8902 && INTVAL (XEXP (x, 1)) >= 0
8903 && INTVAL (XEXP (x, 1)) < GET_MODE_PRECISION (mode))
8904 && ! (GET_MODE (XEXP (x, 1)) != VOIDmode
8905 && (nonzero_bits (XEXP (x, 1), GET_MODE (XEXP (x, 1)))
8906 < (unsigned HOST_WIDE_INT) GET_MODE_PRECISION (mode))))
8907 break;
8909 /* If the shift count is a constant and we can do arithmetic in
8910 the mode of the shift, refine which bits we need. Otherwise, use the
8911 conservative form of the mask. */
8912 if (CONST_INT_P (XEXP (x, 1))
8913 && INTVAL (XEXP (x, 1)) >= 0
8914 && INTVAL (XEXP (x, 1)) < GET_MODE_PRECISION (op_mode)
8915 && HWI_COMPUTABLE_MODE_P (op_mode))
8916 mask >>= INTVAL (XEXP (x, 1));
8917 else
8918 mask = fuller_mask;
8920 op0 = gen_lowpart_or_truncate (op_mode,
8921 force_to_mode (XEXP (x, 0), mode,
8922 mask, next_select));
8924 if (op_mode != xmode || op0 != XEXP (x, 0))
8926 x = simplify_gen_binary (code, op_mode, op0, XEXP (x, 1));
8927 xmode = op_mode;
8929 break;
8931 case LSHIFTRT:
8932 /* Here we can only do something if the shift count is a constant,
8933 this shift constant is valid for the host, and we can do arithmetic
8934 in OP_MODE. */
8936 if (CONST_INT_P (XEXP (x, 1))
8937 && INTVAL (XEXP (x, 1)) >= 0
8938 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT
8939 && HWI_COMPUTABLE_MODE_P (op_mode))
8941 rtx inner = XEXP (x, 0);
8942 unsigned HOST_WIDE_INT inner_mask;
8944 /* Select the mask of the bits we need for the shift operand. */
8945 inner_mask = mask << INTVAL (XEXP (x, 1));
8947 /* We can only change the mode of the shift if we can do arithmetic
8948 in the mode of the shift and INNER_MASK is no wider than the
8949 width of X's mode. */
8950 if ((inner_mask & ~GET_MODE_MASK (xmode)) != 0)
8951 op_mode = xmode;
8953 inner = force_to_mode (inner, op_mode, inner_mask, next_select);
8955 if (xmode != op_mode || inner != XEXP (x, 0))
8957 x = simplify_gen_binary (LSHIFTRT, op_mode, inner, XEXP (x, 1));
8958 xmode = op_mode;
8962 /* If we have (and (lshiftrt FOO C1) C2) where the combination of the
8963 shift and AND produces only copies of the sign bit (C2 is one less
8964 than a power of two), we can do this with just a shift. */
8966 if (GET_CODE (x) == LSHIFTRT
8967 && CONST_INT_P (XEXP (x, 1))
8968 /* The shift puts one of the sign bit copies in the least significant
8969 bit. */
8970 && ((INTVAL (XEXP (x, 1))
8971 + num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0))))
8972 >= GET_MODE_PRECISION (xmode))
8973 && pow2p_hwi (mask + 1)
8974 /* Number of bits left after the shift must be more than the mask
8975 needs. */
8976 && ((INTVAL (XEXP (x, 1)) + exact_log2 (mask + 1))
8977 <= GET_MODE_PRECISION (xmode))
8978 /* Must be more sign bit copies than the mask needs. */
8979 && ((int) num_sign_bit_copies (XEXP (x, 0), GET_MODE (XEXP (x, 0)))
8980 >= exact_log2 (mask + 1)))
8982 int nbits = GET_MODE_PRECISION (xmode) - exact_log2 (mask + 1);
8983 x = simplify_gen_binary (LSHIFTRT, xmode, XEXP (x, 0),
8984 gen_int_shift_amount (xmode, nbits));
8986 goto shiftrt;
8988 case ASHIFTRT:
8989 /* If we are just looking for the sign bit, we don't need this shift at
8990 all, even if it has a variable count. */
8991 if (val_signbit_p (xmode, mask))
8992 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
8994 /* If this is a shift by a constant, get a mask that contains those bits
8995 that are not copies of the sign bit. We then have two cases: If
8996 MASK only includes those bits, this can be a logical shift, which may
8997 allow simplifications. If MASK is a single-bit field not within
8998 those bits, we are requesting a copy of the sign bit and hence can
8999 shift the sign bit to the appropriate location. */
9001 if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) >= 0
9002 && INTVAL (XEXP (x, 1)) < HOST_BITS_PER_WIDE_INT)
9004 unsigned HOST_WIDE_INT nonzero;
9005 int i;
9007 /* If the considered data is wider than HOST_WIDE_INT, we can't
9008 represent a mask for all its bits in a single scalar.
9009 But we only care about the lower bits, so calculate these. */
9011 if (GET_MODE_PRECISION (xmode) > HOST_BITS_PER_WIDE_INT)
9013 nonzero = HOST_WIDE_INT_M1U;
9015 /* GET_MODE_PRECISION (GET_MODE (x)) - INTVAL (XEXP (x, 1))
9016 is the number of bits a full-width mask would have set.
9017 We need only shift if these are fewer than nonzero can
9018 hold. If not, we must keep all bits set in nonzero. */
9020 if (GET_MODE_PRECISION (xmode) - INTVAL (XEXP (x, 1))
9021 < HOST_BITS_PER_WIDE_INT)
9022 nonzero >>= INTVAL (XEXP (x, 1))
9023 + HOST_BITS_PER_WIDE_INT
9024 - GET_MODE_PRECISION (xmode);
9026 else
9028 nonzero = GET_MODE_MASK (xmode);
9029 nonzero >>= INTVAL (XEXP (x, 1));
9032 if ((mask & ~nonzero) == 0)
9034 x = simplify_shift_const (NULL_RTX, LSHIFTRT, xmode,
9035 XEXP (x, 0), INTVAL (XEXP (x, 1)));
9036 if (GET_CODE (x) != ASHIFTRT)
9037 return force_to_mode (x, mode, mask, next_select);
9040 else if ((i = exact_log2 (mask)) >= 0)
9042 x = simplify_shift_const
9043 (NULL_RTX, LSHIFTRT, xmode, XEXP (x, 0),
9044 GET_MODE_PRECISION (xmode) - 1 - i);
9046 if (GET_CODE (x) != ASHIFTRT)
9047 return force_to_mode (x, mode, mask, next_select);
9051 /* If MASK is 1, convert this to an LSHIFTRT. This can be done
9052 even if the shift count isn't a constant. */
9053 if (mask == 1)
9054 x = simplify_gen_binary (LSHIFTRT, xmode, XEXP (x, 0), XEXP (x, 1));
9056 shiftrt:
9058 /* If this is a zero- or sign-extension operation that just affects bits
9059 we don't care about, remove it. Be sure the call above returned
9060 something that is still a shift. */
9062 if ((GET_CODE (x) == LSHIFTRT || GET_CODE (x) == ASHIFTRT)
9063 && CONST_INT_P (XEXP (x, 1))
9064 && INTVAL (XEXP (x, 1)) >= 0
9065 && (INTVAL (XEXP (x, 1))
9066 <= GET_MODE_PRECISION (xmode) - (floor_log2 (mask) + 1))
9067 && GET_CODE (XEXP (x, 0)) == ASHIFT
9068 && XEXP (XEXP (x, 0), 1) == XEXP (x, 1))
9069 return force_to_mode (XEXP (XEXP (x, 0), 0), mode, mask,
9070 next_select);
9072 break;
9074 case ROTATE:
9075 case ROTATERT:
9076 /* If the shift count is constant and we can do computations
9077 in the mode of X, compute where the bits we care about are.
9078 Otherwise, we can't do anything. Don't change the mode of
9079 the shift or propagate MODE into the shift, though. */
9080 if (CONST_INT_P (XEXP (x, 1))
9081 && INTVAL (XEXP (x, 1)) >= 0)
9083 temp = simplify_binary_operation (code == ROTATE ? ROTATERT : ROTATE,
9084 xmode, gen_int_mode (mask, xmode),
9085 XEXP (x, 1));
9086 if (temp && CONST_INT_P (temp))
9087 x = simplify_gen_binary (code, xmode,
9088 force_to_mode (XEXP (x, 0), xmode,
9089 INTVAL (temp), next_select),
9090 XEXP (x, 1));
9092 break;
9094 case NEG:
9095 /* If we just want the low-order bit, the NEG isn't needed since it
9096 won't change the low-order bit. */
9097 if (mask == 1)
9098 return force_to_mode (XEXP (x, 0), mode, mask, just_select);
9100 /* We need any bits less significant than the most significant bit in
9101 MASK since carries from those bits will affect the bits we are
9102 interested in. */
9103 mask = fuller_mask;
9104 goto unop;
9106 case NOT:
9107 /* (not FOO) is (xor FOO CONST), so if FOO is an LSHIFTRT, we can do the
9108 same as the XOR case above. Ensure that the constant we form is not
9109 wider than the mode of X. */
9111 if (GET_CODE (XEXP (x, 0)) == LSHIFTRT
9112 && CONST_INT_P (XEXP (XEXP (x, 0), 1))
9113 && INTVAL (XEXP (XEXP (x, 0), 1)) >= 0
9114 && (INTVAL (XEXP (XEXP (x, 0), 1)) + floor_log2 (mask)
9115 < GET_MODE_PRECISION (xmode))
9116 && INTVAL (XEXP (XEXP (x, 0), 1)) < HOST_BITS_PER_WIDE_INT)
9118 temp = gen_int_mode (mask << INTVAL (XEXP (XEXP (x, 0), 1)), xmode);
9119 temp = simplify_gen_binary (XOR, xmode, XEXP (XEXP (x, 0), 0), temp);
9120 x = simplify_gen_binary (LSHIFTRT, xmode,
9121 temp, XEXP (XEXP (x, 0), 1));
9123 return force_to_mode (x, mode, mask, next_select);
9126 /* (and (not FOO) CONST) is (not (or FOO (not CONST))), so we must
9127 use the full mask inside the NOT. */
9128 mask = fuller_mask;
9130 unop:
9131 op0 = gen_lowpart_or_truncate (op_mode,
9132 force_to_mode (XEXP (x, 0), mode, mask,
9133 next_select));
9134 if (op_mode != xmode || op0 != XEXP (x, 0))
9136 x = simplify_gen_unary (code, op_mode, op0, op_mode);
9137 xmode = op_mode;
9139 break;
9141 case NE:
9142 /* (and (ne FOO 0) CONST) can be (and FOO CONST) if CONST is included
9143 in STORE_FLAG_VALUE and FOO has a single bit that might be nonzero,
9144 which is equal to STORE_FLAG_VALUE. */
9145 if ((mask & ~STORE_FLAG_VALUE) == 0
9146 && XEXP (x, 1) == const0_rtx
9147 && GET_MODE (XEXP (x, 0)) == mode
9148 && pow2p_hwi (nonzero_bits (XEXP (x, 0), mode))
9149 && (nonzero_bits (XEXP (x, 0), mode)
9150 == (unsigned HOST_WIDE_INT) STORE_FLAG_VALUE))
9151 return force_to_mode (XEXP (x, 0), mode, mask, next_select);
9153 break;
9155 case IF_THEN_ELSE:
9156 /* We have no way of knowing if the IF_THEN_ELSE can itself be
9157 written in a narrower mode. We play it safe and do not do so. */
9159 op0 = gen_lowpart_or_truncate (xmode,
9160 force_to_mode (XEXP (x, 1), mode,
9161 mask, next_select));
9162 op1 = gen_lowpart_or_truncate (xmode,
9163 force_to_mode (XEXP (x, 2), mode,
9164 mask, next_select));
9165 if (op0 != XEXP (x, 1) || op1 != XEXP (x, 2))
9166 x = simplify_gen_ternary (IF_THEN_ELSE, xmode,
9167 GET_MODE (XEXP (x, 0)), XEXP (x, 0),
9168 op0, op1);
9169 break;
9171 default:
9172 break;
9175 /* Ensure we return a value of the proper mode. */
9176 return gen_lowpart_or_truncate (mode, x);
9179 /* Return nonzero if X is an expression that has one of two values depending on
9180 whether some other value is zero or nonzero. In that case, we return the
9181 value that is being tested, *PTRUE is set to the value if the rtx being
9182 returned has a nonzero value, and *PFALSE is set to the other alternative.
9184 If we return zero, we set *PTRUE and *PFALSE to X. */
9186 static rtx
9187 if_then_else_cond (rtx x, rtx *ptrue, rtx *pfalse)
9189 machine_mode mode = GET_MODE (x);
9190 enum rtx_code code = GET_CODE (x);
9191 rtx cond0, cond1, true0, true1, false0, false1;
9192 unsigned HOST_WIDE_INT nz;
9193 scalar_int_mode int_mode;
9195 /* If we are comparing a value against zero, we are done. */
9196 if ((code == NE || code == EQ)
9197 && XEXP (x, 1) == const0_rtx)
9199 *ptrue = (code == NE) ? const_true_rtx : const0_rtx;
9200 *pfalse = (code == NE) ? const0_rtx : const_true_rtx;
9201 return XEXP (x, 0);
9204 /* If this is a unary operation whose operand has one of two values, apply
9205 our opcode to compute those values. */
9206 else if (UNARY_P (x)
9207 && (cond0 = if_then_else_cond (XEXP (x, 0), &true0, &false0)) != 0)
9209 *ptrue = simplify_gen_unary (code, mode, true0, GET_MODE (XEXP (x, 0)));
9210 *pfalse = simplify_gen_unary (code, mode, false0,
9211 GET_MODE (XEXP (x, 0)));
9212 return cond0;
9215 /* If this is a COMPARE, do nothing, since the IF_THEN_ELSE we would
9216 make can't possibly match and would suppress other optimizations. */
9217 else if (code == COMPARE)
9220 /* If this is a binary operation, see if either side has only one of two
9221 values. If either one does or if both do and they are conditional on
9222 the same value, compute the new true and false values. */
9223 else if (BINARY_P (x))
9225 rtx op0 = XEXP (x, 0);
9226 rtx op1 = XEXP (x, 1);
9227 cond0 = if_then_else_cond (op0, &true0, &false0);
9228 cond1 = if_then_else_cond (op1, &true1, &false1);
9230 if ((cond0 != 0 && cond1 != 0 && !rtx_equal_p (cond0, cond1))
9231 && (REG_P (op0) || REG_P (op1)))
9233 /* Try to enable a simplification by undoing work done by
9234 if_then_else_cond if it converted a REG into something more
9235 complex. */
9236 if (REG_P (op0))
9238 cond0 = 0;
9239 true0 = false0 = op0;
9241 else
9243 cond1 = 0;
9244 true1 = false1 = op1;
9248 if ((cond0 != 0 || cond1 != 0)
9249 && ! (cond0 != 0 && cond1 != 0 && !rtx_equal_p (cond0, cond1)))
9251 /* If if_then_else_cond returned zero, then true/false are the
9252 same rtl. We must copy one of them to prevent invalid rtl
9253 sharing. */
9254 if (cond0 == 0)
9255 true0 = copy_rtx (true0);
9256 else if (cond1 == 0)
9257 true1 = copy_rtx (true1);
9259 if (COMPARISON_P (x))
9261 *ptrue = simplify_gen_relational (code, mode, VOIDmode,
9262 true0, true1);
9263 *pfalse = simplify_gen_relational (code, mode, VOIDmode,
9264 false0, false1);
9266 else
9268 *ptrue = simplify_gen_binary (code, mode, true0, true1);
9269 *pfalse = simplify_gen_binary (code, mode, false0, false1);
9272 return cond0 ? cond0 : cond1;
9275 /* See if we have PLUS, IOR, XOR, MINUS or UMAX, where one of the
9276 operands is zero when the other is nonzero, and vice-versa,
9277 and STORE_FLAG_VALUE is 1 or -1. */
9279 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9280 && (code == PLUS || code == IOR || code == XOR || code == MINUS
9281 || code == UMAX)
9282 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
9284 rtx op0 = XEXP (XEXP (x, 0), 1);
9285 rtx op1 = XEXP (XEXP (x, 1), 1);
9287 cond0 = XEXP (XEXP (x, 0), 0);
9288 cond1 = XEXP (XEXP (x, 1), 0);
9290 if (COMPARISON_P (cond0)
9291 && COMPARISON_P (cond1)
9292 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
9293 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
9294 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
9295 || ((swap_condition (GET_CODE (cond0))
9296 == reversed_comparison_code (cond1, NULL))
9297 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
9298 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
9299 && ! side_effects_p (x))
9301 *ptrue = simplify_gen_binary (MULT, mode, op0, const_true_rtx);
9302 *pfalse = simplify_gen_binary (MULT, mode,
9303 (code == MINUS
9304 ? simplify_gen_unary (NEG, mode,
9305 op1, mode)
9306 : op1),
9307 const_true_rtx);
9308 return cond0;
9312 /* Similarly for MULT, AND and UMIN, except that for these the result
9313 is always zero. */
9314 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
9315 && (code == MULT || code == AND || code == UMIN)
9316 && GET_CODE (XEXP (x, 0)) == MULT && GET_CODE (XEXP (x, 1)) == MULT)
9318 cond0 = XEXP (XEXP (x, 0), 0);
9319 cond1 = XEXP (XEXP (x, 1), 0);
9321 if (COMPARISON_P (cond0)
9322 && COMPARISON_P (cond1)
9323 && ((GET_CODE (cond0) == reversed_comparison_code (cond1, NULL)
9324 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 0))
9325 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 1)))
9326 || ((swap_condition (GET_CODE (cond0))
9327 == reversed_comparison_code (cond1, NULL))
9328 && rtx_equal_p (XEXP (cond0, 0), XEXP (cond1, 1))
9329 && rtx_equal_p (XEXP (cond0, 1), XEXP (cond1, 0))))
9330 && ! side_effects_p (x))
9332 *ptrue = *pfalse = const0_rtx;
9333 return cond0;
9338 else if (code == IF_THEN_ELSE)
9340 /* If we have IF_THEN_ELSE already, extract the condition and
9341 canonicalize it if it is NE or EQ. */
9342 cond0 = XEXP (x, 0);
9343 *ptrue = XEXP (x, 1), *pfalse = XEXP (x, 2);
9344 if (GET_CODE (cond0) == NE && XEXP (cond0, 1) == const0_rtx)
9345 return XEXP (cond0, 0);
9346 else if (GET_CODE (cond0) == EQ && XEXP (cond0, 1) == const0_rtx)
9348 *ptrue = XEXP (x, 2), *pfalse = XEXP (x, 1);
9349 return XEXP (cond0, 0);
9351 else
9352 return cond0;
9355 /* If X is a SUBREG, we can narrow both the true and false values
9356 if the inner expression, if there is a condition. */
9357 else if (code == SUBREG
9358 && (cond0 = if_then_else_cond (SUBREG_REG (x), &true0,
9359 &false0)) != 0)
9361 true0 = simplify_gen_subreg (mode, true0,
9362 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
9363 false0 = simplify_gen_subreg (mode, false0,
9364 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
9365 if (true0 && false0)
9367 *ptrue = true0;
9368 *pfalse = false0;
9369 return cond0;
9373 /* If X is a constant, this isn't special and will cause confusions
9374 if we treat it as such. Likewise if it is equivalent to a constant. */
9375 else if (CONSTANT_P (x)
9376 || ((cond0 = get_last_value (x)) != 0 && CONSTANT_P (cond0)))
9379 /* If we're in BImode, canonicalize on 0 and STORE_FLAG_VALUE, as that
9380 will be least confusing to the rest of the compiler. */
9381 else if (mode == BImode)
9383 *ptrue = GEN_INT (STORE_FLAG_VALUE), *pfalse = const0_rtx;
9384 return x;
9387 /* If X is known to be either 0 or -1, those are the true and
9388 false values when testing X. */
9389 else if (x == constm1_rtx || x == const0_rtx
9390 || (is_a <scalar_int_mode> (mode, &int_mode)
9391 && (num_sign_bit_copies (x, int_mode)
9392 == GET_MODE_PRECISION (int_mode))))
9394 *ptrue = constm1_rtx, *pfalse = const0_rtx;
9395 return x;
9398 /* Likewise for 0 or a single bit. */
9399 else if (HWI_COMPUTABLE_MODE_P (mode)
9400 && pow2p_hwi (nz = nonzero_bits (x, mode)))
9402 *ptrue = gen_int_mode (nz, mode), *pfalse = const0_rtx;
9403 return x;
9406 /* Otherwise fail; show no condition with true and false values the same. */
9407 *ptrue = *pfalse = x;
9408 return 0;
9411 /* Return the value of expression X given the fact that condition COND
9412 is known to be true when applied to REG as its first operand and VAL
9413 as its second. X is known to not be shared and so can be modified in
9414 place.
9416 We only handle the simplest cases, and specifically those cases that
9417 arise with IF_THEN_ELSE expressions. */
9419 static rtx
9420 known_cond (rtx x, enum rtx_code cond, rtx reg, rtx val)
9422 enum rtx_code code = GET_CODE (x);
9423 const char *fmt;
9424 int i, j;
9426 if (side_effects_p (x))
9427 return x;
9429 /* If either operand of the condition is a floating point value,
9430 then we have to avoid collapsing an EQ comparison. */
9431 if (cond == EQ
9432 && rtx_equal_p (x, reg)
9433 && ! FLOAT_MODE_P (GET_MODE (x))
9434 && ! FLOAT_MODE_P (GET_MODE (val)))
9435 return val;
9437 if (cond == UNEQ && rtx_equal_p (x, reg))
9438 return val;
9440 /* If X is (abs REG) and we know something about REG's relationship
9441 with zero, we may be able to simplify this. */
9443 if (code == ABS && rtx_equal_p (XEXP (x, 0), reg) && val == const0_rtx)
9444 switch (cond)
9446 case GE: case GT: case EQ:
9447 return XEXP (x, 0);
9448 case LT: case LE:
9449 return simplify_gen_unary (NEG, GET_MODE (XEXP (x, 0)),
9450 XEXP (x, 0),
9451 GET_MODE (XEXP (x, 0)));
9452 default:
9453 break;
9456 /* The only other cases we handle are MIN, MAX, and comparisons if the
9457 operands are the same as REG and VAL. */
9459 else if (COMPARISON_P (x) || COMMUTATIVE_ARITH_P (x))
9461 if (rtx_equal_p (XEXP (x, 0), val))
9463 std::swap (val, reg);
9464 cond = swap_condition (cond);
9467 if (rtx_equal_p (XEXP (x, 0), reg) && rtx_equal_p (XEXP (x, 1), val))
9469 if (COMPARISON_P (x))
9471 if (comparison_dominates_p (cond, code))
9472 return const_true_rtx;
9474 code = reversed_comparison_code (x, NULL);
9475 if (code != UNKNOWN
9476 && comparison_dominates_p (cond, code))
9477 return const0_rtx;
9478 else
9479 return x;
9481 else if (code == SMAX || code == SMIN
9482 || code == UMIN || code == UMAX)
9484 int unsignedp = (code == UMIN || code == UMAX);
9486 /* Do not reverse the condition when it is NE or EQ.
9487 This is because we cannot conclude anything about
9488 the value of 'SMAX (x, y)' when x is not equal to y,
9489 but we can when x equals y. */
9490 if ((code == SMAX || code == UMAX)
9491 && ! (cond == EQ || cond == NE))
9492 cond = reverse_condition (cond);
9494 switch (cond)
9496 case GE: case GT:
9497 return unsignedp ? x : XEXP (x, 1);
9498 case LE: case LT:
9499 return unsignedp ? x : XEXP (x, 0);
9500 case GEU: case GTU:
9501 return unsignedp ? XEXP (x, 1) : x;
9502 case LEU: case LTU:
9503 return unsignedp ? XEXP (x, 0) : x;
9504 default:
9505 break;
9510 else if (code == SUBREG)
9512 machine_mode inner_mode = GET_MODE (SUBREG_REG (x));
9513 rtx new_rtx, r = known_cond (SUBREG_REG (x), cond, reg, val);
9515 if (SUBREG_REG (x) != r)
9517 /* We must simplify subreg here, before we lose track of the
9518 original inner_mode. */
9519 new_rtx = simplify_subreg (GET_MODE (x), r,
9520 inner_mode, SUBREG_BYTE (x));
9521 if (new_rtx)
9522 return new_rtx;
9523 else
9524 SUBST (SUBREG_REG (x), r);
9527 return x;
9529 /* We don't have to handle SIGN_EXTEND here, because even in the
9530 case of replacing something with a modeless CONST_INT, a
9531 CONST_INT is already (supposed to be) a valid sign extension for
9532 its narrower mode, which implies it's already properly
9533 sign-extended for the wider mode. Now, for ZERO_EXTEND, the
9534 story is different. */
9535 else if (code == ZERO_EXTEND)
9537 machine_mode inner_mode = GET_MODE (XEXP (x, 0));
9538 rtx new_rtx, r = known_cond (XEXP (x, 0), cond, reg, val);
9540 if (XEXP (x, 0) != r)
9542 /* We must simplify the zero_extend here, before we lose
9543 track of the original inner_mode. */
9544 new_rtx = simplify_unary_operation (ZERO_EXTEND, GET_MODE (x),
9545 r, inner_mode);
9546 if (new_rtx)
9547 return new_rtx;
9548 else
9549 SUBST (XEXP (x, 0), r);
9552 return x;
9555 fmt = GET_RTX_FORMAT (code);
9556 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
9558 if (fmt[i] == 'e')
9559 SUBST (XEXP (x, i), known_cond (XEXP (x, i), cond, reg, val));
9560 else if (fmt[i] == 'E')
9561 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
9562 SUBST (XVECEXP (x, i, j), known_cond (XVECEXP (x, i, j),
9563 cond, reg, val));
9566 return x;
9569 /* See if X and Y are equal for the purposes of seeing if we can rewrite an
9570 assignment as a field assignment. */
9572 static int
9573 rtx_equal_for_field_assignment_p (rtx x, rtx y, bool widen_x)
9575 if (widen_x && GET_MODE (x) != GET_MODE (y))
9577 if (paradoxical_subreg_p (GET_MODE (x), GET_MODE (y)))
9578 return 0;
9579 if (BYTES_BIG_ENDIAN != WORDS_BIG_ENDIAN)
9580 return 0;
9581 x = adjust_address_nv (x, GET_MODE (y),
9582 byte_lowpart_offset (GET_MODE (y),
9583 GET_MODE (x)));
9586 if (x == y || rtx_equal_p (x, y))
9587 return 1;
9589 if (x == 0 || y == 0 || GET_MODE (x) != GET_MODE (y))
9590 return 0;
9592 /* Check for a paradoxical SUBREG of a MEM compared with the MEM.
9593 Note that all SUBREGs of MEM are paradoxical; otherwise they
9594 would have been rewritten. */
9595 if (MEM_P (x) && GET_CODE (y) == SUBREG
9596 && MEM_P (SUBREG_REG (y))
9597 && rtx_equal_p (SUBREG_REG (y),
9598 gen_lowpart (GET_MODE (SUBREG_REG (y)), x)))
9599 return 1;
9601 if (MEM_P (y) && GET_CODE (x) == SUBREG
9602 && MEM_P (SUBREG_REG (x))
9603 && rtx_equal_p (SUBREG_REG (x),
9604 gen_lowpart (GET_MODE (SUBREG_REG (x)), y)))
9605 return 1;
9607 /* We used to see if get_last_value of X and Y were the same but that's
9608 not correct. In one direction, we'll cause the assignment to have
9609 the wrong destination and in the case, we'll import a register into this
9610 insn that might have already have been dead. So fail if none of the
9611 above cases are true. */
9612 return 0;
9615 /* See if X, a SET operation, can be rewritten as a bit-field assignment.
9616 Return that assignment if so.
9618 We only handle the most common cases. */
9620 static rtx
9621 make_field_assignment (rtx x)
9623 rtx dest = SET_DEST (x);
9624 rtx src = SET_SRC (x);
9625 rtx assign;
9626 rtx rhs, lhs;
9627 HOST_WIDE_INT c1;
9628 HOST_WIDE_INT pos;
9629 unsigned HOST_WIDE_INT len;
9630 rtx other;
9632 /* All the rules in this function are specific to scalar integers. */
9633 scalar_int_mode mode;
9634 if (!is_a <scalar_int_mode> (GET_MODE (dest), &mode))
9635 return x;
9637 /* If SRC was (and (not (ashift (const_int 1) POS)) DEST), this is
9638 a clear of a one-bit field. We will have changed it to
9639 (and (rotate (const_int -2) POS) DEST), so check for that. Also check
9640 for a SUBREG. */
9642 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == ROTATE
9643 && CONST_INT_P (XEXP (XEXP (src, 0), 0))
9644 && INTVAL (XEXP (XEXP (src, 0), 0)) == -2
9645 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9647 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
9648 1, 1, 1, 0);
9649 if (assign != 0)
9650 return gen_rtx_SET (assign, const0_rtx);
9651 return x;
9654 if (GET_CODE (src) == AND && GET_CODE (XEXP (src, 0)) == SUBREG
9655 && subreg_lowpart_p (XEXP (src, 0))
9656 && partial_subreg_p (XEXP (src, 0))
9657 && GET_CODE (SUBREG_REG (XEXP (src, 0))) == ROTATE
9658 && CONST_INT_P (XEXP (SUBREG_REG (XEXP (src, 0)), 0))
9659 && INTVAL (XEXP (SUBREG_REG (XEXP (src, 0)), 0)) == -2
9660 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9662 assign = make_extraction (VOIDmode, dest, 0,
9663 XEXP (SUBREG_REG (XEXP (src, 0)), 1),
9664 1, 1, 1, 0);
9665 if (assign != 0)
9666 return gen_rtx_SET (assign, const0_rtx);
9667 return x;
9670 /* If SRC is (ior (ashift (const_int 1) POS) DEST), this is a set of a
9671 one-bit field. */
9672 if (GET_CODE (src) == IOR && GET_CODE (XEXP (src, 0)) == ASHIFT
9673 && XEXP (XEXP (src, 0), 0) == const1_rtx
9674 && rtx_equal_for_field_assignment_p (dest, XEXP (src, 1)))
9676 assign = make_extraction (VOIDmode, dest, 0, XEXP (XEXP (src, 0), 1),
9677 1, 1, 1, 0);
9678 if (assign != 0)
9679 return gen_rtx_SET (assign, const1_rtx);
9680 return x;
9683 /* If DEST is already a field assignment, i.e. ZERO_EXTRACT, and the
9684 SRC is an AND with all bits of that field set, then we can discard
9685 the AND. */
9686 if (GET_CODE (dest) == ZERO_EXTRACT
9687 && CONST_INT_P (XEXP (dest, 1))
9688 && GET_CODE (src) == AND
9689 && CONST_INT_P (XEXP (src, 1)))
9691 HOST_WIDE_INT width = INTVAL (XEXP (dest, 1));
9692 unsigned HOST_WIDE_INT and_mask = INTVAL (XEXP (src, 1));
9693 unsigned HOST_WIDE_INT ze_mask;
9695 if (width >= HOST_BITS_PER_WIDE_INT)
9696 ze_mask = -1;
9697 else
9698 ze_mask = ((unsigned HOST_WIDE_INT)1 << width) - 1;
9700 /* Complete overlap. We can remove the source AND. */
9701 if ((and_mask & ze_mask) == ze_mask)
9702 return gen_rtx_SET (dest, XEXP (src, 0));
9704 /* Partial overlap. We can reduce the source AND. */
9705 if ((and_mask & ze_mask) != and_mask)
9707 src = gen_rtx_AND (mode, XEXP (src, 0),
9708 gen_int_mode (and_mask & ze_mask, mode));
9709 return gen_rtx_SET (dest, src);
9713 /* The other case we handle is assignments into a constant-position
9714 field. They look like (ior/xor (and DEST C1) OTHER). If C1 represents
9715 a mask that has all one bits except for a group of zero bits and
9716 OTHER is known to have zeros where C1 has ones, this is such an
9717 assignment. Compute the position and length from C1. Shift OTHER
9718 to the appropriate position, force it to the required mode, and
9719 make the extraction. Check for the AND in both operands. */
9721 /* One or more SUBREGs might obscure the constant-position field
9722 assignment. The first one we are likely to encounter is an outer
9723 narrowing SUBREG, which we can just strip for the purposes of
9724 identifying the constant-field assignment. */
9725 scalar_int_mode src_mode = mode;
9726 if (GET_CODE (src) == SUBREG
9727 && subreg_lowpart_p (src)
9728 && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (src)), &src_mode))
9729 src = SUBREG_REG (src);
9731 if (GET_CODE (src) != IOR && GET_CODE (src) != XOR)
9732 return x;
9734 rhs = expand_compound_operation (XEXP (src, 0));
9735 lhs = expand_compound_operation (XEXP (src, 1));
9737 if (GET_CODE (rhs) == AND
9738 && CONST_INT_P (XEXP (rhs, 1))
9739 && rtx_equal_for_field_assignment_p (XEXP (rhs, 0), dest))
9740 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
9741 /* The second SUBREG that might get in the way is a paradoxical
9742 SUBREG around the first operand of the AND. We want to
9743 pretend the operand is as wide as the destination here. We
9744 do this by adjusting the MEM to wider mode for the sole
9745 purpose of the call to rtx_equal_for_field_assignment_p. Also
9746 note this trick only works for MEMs. */
9747 else if (GET_CODE (rhs) == AND
9748 && paradoxical_subreg_p (XEXP (rhs, 0))
9749 && MEM_P (SUBREG_REG (XEXP (rhs, 0)))
9750 && CONST_INT_P (XEXP (rhs, 1))
9751 && rtx_equal_for_field_assignment_p (SUBREG_REG (XEXP (rhs, 0)),
9752 dest, true))
9753 c1 = INTVAL (XEXP (rhs, 1)), other = lhs;
9754 else if (GET_CODE (lhs) == AND
9755 && CONST_INT_P (XEXP (lhs, 1))
9756 && rtx_equal_for_field_assignment_p (XEXP (lhs, 0), dest))
9757 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
9758 /* The second SUBREG that might get in the way is a paradoxical
9759 SUBREG around the first operand of the AND. We want to
9760 pretend the operand is as wide as the destination here. We
9761 do this by adjusting the MEM to wider mode for the sole
9762 purpose of the call to rtx_equal_for_field_assignment_p. Also
9763 note this trick only works for MEMs. */
9764 else if (GET_CODE (lhs) == AND
9765 && paradoxical_subreg_p (XEXP (lhs, 0))
9766 && MEM_P (SUBREG_REG (XEXP (lhs, 0)))
9767 && CONST_INT_P (XEXP (lhs, 1))
9768 && rtx_equal_for_field_assignment_p (SUBREG_REG (XEXP (lhs, 0)),
9769 dest, true))
9770 c1 = INTVAL (XEXP (lhs, 1)), other = rhs;
9771 else
9772 return x;
9774 pos = get_pos_from_mask ((~c1) & GET_MODE_MASK (mode), &len);
9775 if (pos < 0
9776 || pos + len > GET_MODE_PRECISION (mode)
9777 || GET_MODE_PRECISION (mode) > HOST_BITS_PER_WIDE_INT
9778 || (c1 & nonzero_bits (other, mode)) != 0)
9779 return x;
9781 assign = make_extraction (VOIDmode, dest, pos, NULL_RTX, len, 1, 1, 0);
9782 if (assign == 0)
9783 return x;
9785 /* The mode to use for the source is the mode of the assignment, or of
9786 what is inside a possible STRICT_LOW_PART. */
9787 machine_mode new_mode = (GET_CODE (assign) == STRICT_LOW_PART
9788 ? GET_MODE (XEXP (assign, 0)) : GET_MODE (assign));
9790 /* Shift OTHER right POS places and make it the source, restricting it
9791 to the proper length and mode. */
9793 src = canon_reg_for_combine (simplify_shift_const (NULL_RTX, LSHIFTRT,
9794 src_mode, other, pos),
9795 dest);
9796 src = force_to_mode (src, new_mode,
9797 len >= HOST_BITS_PER_WIDE_INT
9798 ? HOST_WIDE_INT_M1U
9799 : (HOST_WIDE_INT_1U << len) - 1,
9802 /* If SRC is masked by an AND that does not make a difference in
9803 the value being stored, strip it. */
9804 if (GET_CODE (assign) == ZERO_EXTRACT
9805 && CONST_INT_P (XEXP (assign, 1))
9806 && INTVAL (XEXP (assign, 1)) < HOST_BITS_PER_WIDE_INT
9807 && GET_CODE (src) == AND
9808 && CONST_INT_P (XEXP (src, 1))
9809 && UINTVAL (XEXP (src, 1))
9810 == (HOST_WIDE_INT_1U << INTVAL (XEXP (assign, 1))) - 1)
9811 src = XEXP (src, 0);
9813 return gen_rtx_SET (assign, src);
9816 /* See if X is of the form (+ (* a c) (* b c)) and convert to (* (+ a b) c)
9817 if so. */
9819 static rtx
9820 apply_distributive_law (rtx x)
9822 enum rtx_code code = GET_CODE (x);
9823 enum rtx_code inner_code;
9824 rtx lhs, rhs, other;
9825 rtx tem;
9827 /* Distributivity is not true for floating point as it can change the
9828 value. So we don't do it unless -funsafe-math-optimizations. */
9829 if (FLOAT_MODE_P (GET_MODE (x))
9830 && ! flag_unsafe_math_optimizations)
9831 return x;
9833 /* The outer operation can only be one of the following: */
9834 if (code != IOR && code != AND && code != XOR
9835 && code != PLUS && code != MINUS)
9836 return x;
9838 lhs = XEXP (x, 0);
9839 rhs = XEXP (x, 1);
9841 /* If either operand is a primitive we can't do anything, so get out
9842 fast. */
9843 if (OBJECT_P (lhs) || OBJECT_P (rhs))
9844 return x;
9846 lhs = expand_compound_operation (lhs);
9847 rhs = expand_compound_operation (rhs);
9848 inner_code = GET_CODE (lhs);
9849 if (inner_code != GET_CODE (rhs))
9850 return x;
9852 /* See if the inner and outer operations distribute. */
9853 switch (inner_code)
9855 case LSHIFTRT:
9856 case ASHIFTRT:
9857 case AND:
9858 case IOR:
9859 /* These all distribute except over PLUS. */
9860 if (code == PLUS || code == MINUS)
9861 return x;
9862 break;
9864 case MULT:
9865 if (code != PLUS && code != MINUS)
9866 return x;
9867 break;
9869 case ASHIFT:
9870 /* This is also a multiply, so it distributes over everything. */
9871 break;
9873 /* This used to handle SUBREG, but this turned out to be counter-
9874 productive, since (subreg (op ...)) usually is not handled by
9875 insn patterns, and this "optimization" therefore transformed
9876 recognizable patterns into unrecognizable ones. Therefore the
9877 SUBREG case was removed from here.
9879 It is possible that distributing SUBREG over arithmetic operations
9880 leads to an intermediate result than can then be optimized further,
9881 e.g. by moving the outer SUBREG to the other side of a SET as done
9882 in simplify_set. This seems to have been the original intent of
9883 handling SUBREGs here.
9885 However, with current GCC this does not appear to actually happen,
9886 at least on major platforms. If some case is found where removing
9887 the SUBREG case here prevents follow-on optimizations, distributing
9888 SUBREGs ought to be re-added at that place, e.g. in simplify_set. */
9890 default:
9891 return x;
9894 /* Set LHS and RHS to the inner operands (A and B in the example
9895 above) and set OTHER to the common operand (C in the example).
9896 There is only one way to do this unless the inner operation is
9897 commutative. */
9898 if (COMMUTATIVE_ARITH_P (lhs)
9899 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 0)))
9900 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 1);
9901 else if (COMMUTATIVE_ARITH_P (lhs)
9902 && rtx_equal_p (XEXP (lhs, 0), XEXP (rhs, 1)))
9903 other = XEXP (lhs, 0), lhs = XEXP (lhs, 1), rhs = XEXP (rhs, 0);
9904 else if (COMMUTATIVE_ARITH_P (lhs)
9905 && rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 0)))
9906 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 1);
9907 else if (rtx_equal_p (XEXP (lhs, 1), XEXP (rhs, 1)))
9908 other = XEXP (lhs, 1), lhs = XEXP (lhs, 0), rhs = XEXP (rhs, 0);
9909 else
9910 return x;
9912 /* Form the new inner operation, seeing if it simplifies first. */
9913 tem = simplify_gen_binary (code, GET_MODE (x), lhs, rhs);
9915 /* There is one exception to the general way of distributing:
9916 (a | c) ^ (b | c) -> (a ^ b) & ~c */
9917 if (code == XOR && inner_code == IOR)
9919 inner_code = AND;
9920 other = simplify_gen_unary (NOT, GET_MODE (x), other, GET_MODE (x));
9923 /* We may be able to continuing distributing the result, so call
9924 ourselves recursively on the inner operation before forming the
9925 outer operation, which we return. */
9926 return simplify_gen_binary (inner_code, GET_MODE (x),
9927 apply_distributive_law (tem), other);
9930 /* See if X is of the form (* (+ A B) C), and if so convert to
9931 (+ (* A C) (* B C)) and try to simplify.
9933 Most of the time, this results in no change. However, if some of
9934 the operands are the same or inverses of each other, simplifications
9935 will result.
9937 For example, (and (ior A B) (not B)) can occur as the result of
9938 expanding a bit field assignment. When we apply the distributive
9939 law to this, we get (ior (and (A (not B))) (and (B (not B)))),
9940 which then simplifies to (and (A (not B))).
9942 Note that no checks happen on the validity of applying the inverse
9943 distributive law. This is pointless since we can do it in the
9944 few places where this routine is called.
9946 N is the index of the term that is decomposed (the arithmetic operation,
9947 i.e. (+ A B) in the first example above). !N is the index of the term that
9948 is distributed, i.e. of C in the first example above. */
9949 static rtx
9950 distribute_and_simplify_rtx (rtx x, int n)
9952 machine_mode mode;
9953 enum rtx_code outer_code, inner_code;
9954 rtx decomposed, distributed, inner_op0, inner_op1, new_op0, new_op1, tmp;
9956 /* Distributivity is not true for floating point as it can change the
9957 value. So we don't do it unless -funsafe-math-optimizations. */
9958 if (FLOAT_MODE_P (GET_MODE (x))
9959 && ! flag_unsafe_math_optimizations)
9960 return NULL_RTX;
9962 decomposed = XEXP (x, n);
9963 if (!ARITHMETIC_P (decomposed))
9964 return NULL_RTX;
9966 mode = GET_MODE (x);
9967 outer_code = GET_CODE (x);
9968 distributed = XEXP (x, !n);
9970 inner_code = GET_CODE (decomposed);
9971 inner_op0 = XEXP (decomposed, 0);
9972 inner_op1 = XEXP (decomposed, 1);
9974 /* Special case (and (xor B C) (not A)), which is equivalent to
9975 (xor (ior A B) (ior A C)) */
9976 if (outer_code == AND && inner_code == XOR && GET_CODE (distributed) == NOT)
9978 distributed = XEXP (distributed, 0);
9979 outer_code = IOR;
9982 if (n == 0)
9984 /* Distribute the second term. */
9985 new_op0 = simplify_gen_binary (outer_code, mode, inner_op0, distributed);
9986 new_op1 = simplify_gen_binary (outer_code, mode, inner_op1, distributed);
9988 else
9990 /* Distribute the first term. */
9991 new_op0 = simplify_gen_binary (outer_code, mode, distributed, inner_op0);
9992 new_op1 = simplify_gen_binary (outer_code, mode, distributed, inner_op1);
9995 tmp = apply_distributive_law (simplify_gen_binary (inner_code, mode,
9996 new_op0, new_op1));
9997 if (GET_CODE (tmp) != outer_code
9998 && (set_src_cost (tmp, mode, optimize_this_for_speed_p)
9999 < set_src_cost (x, mode, optimize_this_for_speed_p)))
10000 return tmp;
10002 return NULL_RTX;
10005 /* Simplify a logical `and' of VAROP with the constant CONSTOP, to be done
10006 in MODE. Return an equivalent form, if different from (and VAROP
10007 (const_int CONSTOP)). Otherwise, return NULL_RTX. */
10009 static rtx
10010 simplify_and_const_int_1 (scalar_int_mode mode, rtx varop,
10011 unsigned HOST_WIDE_INT constop)
10013 unsigned HOST_WIDE_INT nonzero;
10014 unsigned HOST_WIDE_INT orig_constop;
10015 rtx orig_varop;
10016 int i;
10018 orig_varop = varop;
10019 orig_constop = constop;
10020 if (GET_CODE (varop) == CLOBBER)
10021 return NULL_RTX;
10023 /* Simplify VAROP knowing that we will be only looking at some of the
10024 bits in it.
10026 Note by passing in CONSTOP, we guarantee that the bits not set in
10027 CONSTOP are not significant and will never be examined. We must
10028 ensure that is the case by explicitly masking out those bits
10029 before returning. */
10030 varop = force_to_mode (varop, mode, constop, 0);
10032 /* If VAROP is a CLOBBER, we will fail so return it. */
10033 if (GET_CODE (varop) == CLOBBER)
10034 return varop;
10036 /* If VAROP is a CONST_INT, then we need to apply the mask in CONSTOP
10037 to VAROP and return the new constant. */
10038 if (CONST_INT_P (varop))
10039 return gen_int_mode (INTVAL (varop) & constop, mode);
10041 /* See what bits may be nonzero in VAROP. Unlike the general case of
10042 a call to nonzero_bits, here we don't care about bits outside
10043 MODE. */
10045 nonzero = nonzero_bits (varop, mode) & GET_MODE_MASK (mode);
10047 /* Turn off all bits in the constant that are known to already be zero.
10048 Thus, if the AND isn't needed at all, we will have CONSTOP == NONZERO_BITS
10049 which is tested below. */
10051 constop &= nonzero;
10053 /* If we don't have any bits left, return zero. */
10054 if (constop == 0)
10055 return const0_rtx;
10057 /* If VAROP is a NEG of something known to be zero or 1 and CONSTOP is
10058 a power of two, we can replace this with an ASHIFT. */
10059 if (GET_CODE (varop) == NEG && nonzero_bits (XEXP (varop, 0), mode) == 1
10060 && (i = exact_log2 (constop)) >= 0)
10061 return simplify_shift_const (NULL_RTX, ASHIFT, mode, XEXP (varop, 0), i);
10063 /* If VAROP is an IOR or XOR, apply the AND to both branches of the IOR
10064 or XOR, then try to apply the distributive law. This may eliminate
10065 operations if either branch can be simplified because of the AND.
10066 It may also make some cases more complex, but those cases probably
10067 won't match a pattern either with or without this. */
10069 if (GET_CODE (varop) == IOR || GET_CODE (varop) == XOR)
10071 scalar_int_mode varop_mode = as_a <scalar_int_mode> (GET_MODE (varop));
10072 return
10073 gen_lowpart
10074 (mode,
10075 apply_distributive_law
10076 (simplify_gen_binary (GET_CODE (varop), varop_mode,
10077 simplify_and_const_int (NULL_RTX, varop_mode,
10078 XEXP (varop, 0),
10079 constop),
10080 simplify_and_const_int (NULL_RTX, varop_mode,
10081 XEXP (varop, 1),
10082 constop))));
10085 /* If VAROP is PLUS, and the constant is a mask of low bits, distribute
10086 the AND and see if one of the operands simplifies to zero. If so, we
10087 may eliminate it. */
10089 if (GET_CODE (varop) == PLUS
10090 && pow2p_hwi (constop + 1))
10092 rtx o0, o1;
10094 o0 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 0), constop);
10095 o1 = simplify_and_const_int (NULL_RTX, mode, XEXP (varop, 1), constop);
10096 if (o0 == const0_rtx)
10097 return o1;
10098 if (o1 == const0_rtx)
10099 return o0;
10102 /* Make a SUBREG if necessary. If we can't make it, fail. */
10103 varop = gen_lowpart (mode, varop);
10104 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
10105 return NULL_RTX;
10107 /* If we are only masking insignificant bits, return VAROP. */
10108 if (constop == nonzero)
10109 return varop;
10111 if (varop == orig_varop && constop == orig_constop)
10112 return NULL_RTX;
10114 /* Otherwise, return an AND. */
10115 return simplify_gen_binary (AND, mode, varop, gen_int_mode (constop, mode));
10119 /* We have X, a logical `and' of VAROP with the constant CONSTOP, to be done
10120 in MODE.
10122 Return an equivalent form, if different from X. Otherwise, return X. If
10123 X is zero, we are to always construct the equivalent form. */
10125 static rtx
10126 simplify_and_const_int (rtx x, scalar_int_mode mode, rtx varop,
10127 unsigned HOST_WIDE_INT constop)
10129 rtx tem = simplify_and_const_int_1 (mode, varop, constop);
10130 if (tem)
10131 return tem;
10133 if (!x)
10134 x = simplify_gen_binary (AND, GET_MODE (varop), varop,
10135 gen_int_mode (constop, mode));
10136 if (GET_MODE (x) != mode)
10137 x = gen_lowpart (mode, x);
10138 return x;
10141 /* Given a REG X of mode XMODE, compute which bits in X can be nonzero.
10142 We don't care about bits outside of those defined in MODE.
10144 For most X this is simply GET_MODE_MASK (GET_MODE (MODE)), but if X is
10145 a shift, AND, or zero_extract, we can do better. */
10147 static rtx
10148 reg_nonzero_bits_for_combine (const_rtx x, scalar_int_mode xmode,
10149 scalar_int_mode mode,
10150 unsigned HOST_WIDE_INT *nonzero)
10152 rtx tem;
10153 reg_stat_type *rsp;
10155 /* If X is a register whose nonzero bits value is current, use it.
10156 Otherwise, if X is a register whose value we can find, use that
10157 value. Otherwise, use the previously-computed global nonzero bits
10158 for this register. */
10160 rsp = &reg_stat[REGNO (x)];
10161 if (rsp->last_set_value != 0
10162 && (rsp->last_set_mode == mode
10163 || (GET_MODE_CLASS (rsp->last_set_mode) == MODE_INT
10164 && GET_MODE_CLASS (mode) == MODE_INT))
10165 && ((rsp->last_set_label >= label_tick_ebb_start
10166 && rsp->last_set_label < label_tick)
10167 || (rsp->last_set_label == label_tick
10168 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
10169 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
10170 && REGNO (x) < reg_n_sets_max
10171 && REG_N_SETS (REGNO (x)) == 1
10172 && !REGNO_REG_SET_P
10173 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
10174 REGNO (x)))))
10176 /* Note that, even if the precision of last_set_mode is lower than that
10177 of mode, record_value_for_reg invoked nonzero_bits on the register
10178 with nonzero_bits_mode (because last_set_mode is necessarily integral
10179 and HWI_COMPUTABLE_MODE_P in this case) so bits in nonzero_bits_mode
10180 are all valid, hence in mode too since nonzero_bits_mode is defined
10181 to the largest HWI_COMPUTABLE_MODE_P mode. */
10182 *nonzero &= rsp->last_set_nonzero_bits;
10183 return NULL;
10186 tem = get_last_value (x);
10187 if (tem)
10189 if (SHORT_IMMEDIATES_SIGN_EXTEND)
10190 tem = sign_extend_short_imm (tem, xmode, GET_MODE_PRECISION (mode));
10192 return tem;
10195 if (nonzero_sign_valid && rsp->nonzero_bits)
10197 unsigned HOST_WIDE_INT mask = rsp->nonzero_bits;
10199 if (GET_MODE_PRECISION (xmode) < GET_MODE_PRECISION (mode))
10200 /* We don't know anything about the upper bits. */
10201 mask |= GET_MODE_MASK (mode) ^ GET_MODE_MASK (xmode);
10203 *nonzero &= mask;
10206 return NULL;
10209 /* Given a reg X of mode XMODE, return the number of bits at the high-order
10210 end of X that are known to be equal to the sign bit. X will be used
10211 in mode MODE; the returned value will always be between 1 and the
10212 number of bits in MODE. */
10214 static rtx
10215 reg_num_sign_bit_copies_for_combine (const_rtx x, scalar_int_mode xmode,
10216 scalar_int_mode mode,
10217 unsigned int *result)
10219 rtx tem;
10220 reg_stat_type *rsp;
10222 rsp = &reg_stat[REGNO (x)];
10223 if (rsp->last_set_value != 0
10224 && rsp->last_set_mode == mode
10225 && ((rsp->last_set_label >= label_tick_ebb_start
10226 && rsp->last_set_label < label_tick)
10227 || (rsp->last_set_label == label_tick
10228 && DF_INSN_LUID (rsp->last_set) < subst_low_luid)
10229 || (REGNO (x) >= FIRST_PSEUDO_REGISTER
10230 && REGNO (x) < reg_n_sets_max
10231 && REG_N_SETS (REGNO (x)) == 1
10232 && !REGNO_REG_SET_P
10233 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
10234 REGNO (x)))))
10236 *result = rsp->last_set_sign_bit_copies;
10237 return NULL;
10240 tem = get_last_value (x);
10241 if (tem != 0)
10242 return tem;
10244 if (nonzero_sign_valid && rsp->sign_bit_copies != 0
10245 && GET_MODE_PRECISION (xmode) == GET_MODE_PRECISION (mode))
10246 *result = rsp->sign_bit_copies;
10248 return NULL;
10251 /* Return the number of "extended" bits there are in X, when interpreted
10252 as a quantity in MODE whose signedness is indicated by UNSIGNEDP. For
10253 unsigned quantities, this is the number of high-order zero bits.
10254 For signed quantities, this is the number of copies of the sign bit
10255 minus 1. In both case, this function returns the number of "spare"
10256 bits. For example, if two quantities for which this function returns
10257 at least 1 are added, the addition is known not to overflow.
10259 This function will always return 0 unless called during combine, which
10260 implies that it must be called from a define_split. */
10262 unsigned int
10263 extended_count (const_rtx x, machine_mode mode, int unsignedp)
10265 if (nonzero_sign_valid == 0)
10266 return 0;
10268 scalar_int_mode int_mode;
10269 return (unsignedp
10270 ? (is_a <scalar_int_mode> (mode, &int_mode)
10271 && HWI_COMPUTABLE_MODE_P (int_mode)
10272 ? (unsigned int) (GET_MODE_PRECISION (int_mode) - 1
10273 - floor_log2 (nonzero_bits (x, int_mode)))
10274 : 0)
10275 : num_sign_bit_copies (x, mode) - 1);
10278 /* This function is called from `simplify_shift_const' to merge two
10279 outer operations. Specifically, we have already found that we need
10280 to perform operation *POP0 with constant *PCONST0 at the outermost
10281 position. We would now like to also perform OP1 with constant CONST1
10282 (with *POP0 being done last).
10284 Return 1 if we can do the operation and update *POP0 and *PCONST0 with
10285 the resulting operation. *PCOMP_P is set to 1 if we would need to
10286 complement the innermost operand, otherwise it is unchanged.
10288 MODE is the mode in which the operation will be done. No bits outside
10289 the width of this mode matter. It is assumed that the width of this mode
10290 is smaller than or equal to HOST_BITS_PER_WIDE_INT.
10292 If *POP0 or OP1 are UNKNOWN, it means no operation is required. Only NEG, PLUS,
10293 IOR, XOR, and AND are supported. We may set *POP0 to SET if the proper
10294 result is simply *PCONST0.
10296 If the resulting operation cannot be expressed as one operation, we
10297 return 0 and do not change *POP0, *PCONST0, and *PCOMP_P. */
10299 static int
10300 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)
10302 enum rtx_code op0 = *pop0;
10303 HOST_WIDE_INT const0 = *pconst0;
10305 const0 &= GET_MODE_MASK (mode);
10306 const1 &= GET_MODE_MASK (mode);
10308 /* If OP0 is an AND, clear unimportant bits in CONST1. */
10309 if (op0 == AND)
10310 const1 &= const0;
10312 /* If OP0 or OP1 is UNKNOWN, this is easy. Similarly if they are the same or
10313 if OP0 is SET. */
10315 if (op1 == UNKNOWN || op0 == SET)
10316 return 1;
10318 else if (op0 == UNKNOWN)
10319 op0 = op1, const0 = const1;
10321 else if (op0 == op1)
10323 switch (op0)
10325 case AND:
10326 const0 &= const1;
10327 break;
10328 case IOR:
10329 const0 |= const1;
10330 break;
10331 case XOR:
10332 const0 ^= const1;
10333 break;
10334 case PLUS:
10335 const0 += const1;
10336 break;
10337 case NEG:
10338 op0 = UNKNOWN;
10339 break;
10340 default:
10341 break;
10345 /* Otherwise, if either is a PLUS or NEG, we can't do anything. */
10346 else if (op0 == PLUS || op1 == PLUS || op0 == NEG || op1 == NEG)
10347 return 0;
10349 /* If the two constants aren't the same, we can't do anything. The
10350 remaining six cases can all be done. */
10351 else if (const0 != const1)
10352 return 0;
10354 else
10355 switch (op0)
10357 case IOR:
10358 if (op1 == AND)
10359 /* (a & b) | b == b */
10360 op0 = SET;
10361 else /* op1 == XOR */
10362 /* (a ^ b) | b == a | b */
10364 break;
10366 case XOR:
10367 if (op1 == AND)
10368 /* (a & b) ^ b == (~a) & b */
10369 op0 = AND, *pcomp_p = 1;
10370 else /* op1 == IOR */
10371 /* (a | b) ^ b == a & ~b */
10372 op0 = AND, const0 = ~const0;
10373 break;
10375 case AND:
10376 if (op1 == IOR)
10377 /* (a | b) & b == b */
10378 op0 = SET;
10379 else /* op1 == XOR */
10380 /* (a ^ b) & b) == (~a) & b */
10381 *pcomp_p = 1;
10382 break;
10383 default:
10384 break;
10387 /* Check for NO-OP cases. */
10388 const0 &= GET_MODE_MASK (mode);
10389 if (const0 == 0
10390 && (op0 == IOR || op0 == XOR || op0 == PLUS))
10391 op0 = UNKNOWN;
10392 else if (const0 == 0 && op0 == AND)
10393 op0 = SET;
10394 else if ((unsigned HOST_WIDE_INT) const0 == GET_MODE_MASK (mode)
10395 && op0 == AND)
10396 op0 = UNKNOWN;
10398 *pop0 = op0;
10400 /* ??? Slightly redundant with the above mask, but not entirely.
10401 Moving this above means we'd have to sign-extend the mode mask
10402 for the final test. */
10403 if (op0 != UNKNOWN && op0 != NEG)
10404 *pconst0 = trunc_int_for_mode (const0, mode);
10406 return 1;
10409 /* A helper to simplify_shift_const_1 to determine the mode we can perform
10410 the shift in. The original shift operation CODE is performed on OP in
10411 ORIG_MODE. Return the wider mode MODE if we can perform the operation
10412 in that mode. Return ORIG_MODE otherwise. We can also assume that the
10413 result of the shift is subject to operation OUTER_CODE with operand
10414 OUTER_CONST. */
10416 static scalar_int_mode
10417 try_widen_shift_mode (enum rtx_code code, rtx op, int count,
10418 scalar_int_mode orig_mode, scalar_int_mode mode,
10419 enum rtx_code outer_code, HOST_WIDE_INT outer_const)
10421 gcc_assert (GET_MODE_PRECISION (mode) > GET_MODE_PRECISION (orig_mode));
10423 /* In general we can't perform in wider mode for right shift and rotate. */
10424 switch (code)
10426 case ASHIFTRT:
10427 /* We can still widen if the bits brought in from the left are identical
10428 to the sign bit of ORIG_MODE. */
10429 if (num_sign_bit_copies (op, mode)
10430 > (unsigned) (GET_MODE_PRECISION (mode)
10431 - GET_MODE_PRECISION (orig_mode)))
10432 return mode;
10433 return orig_mode;
10435 case LSHIFTRT:
10436 /* Similarly here but with zero bits. */
10437 if (HWI_COMPUTABLE_MODE_P (mode)
10438 && (nonzero_bits (op, mode) & ~GET_MODE_MASK (orig_mode)) == 0)
10439 return mode;
10441 /* We can also widen if the bits brought in will be masked off. This
10442 operation is performed in ORIG_MODE. */
10443 if (outer_code == AND)
10445 int care_bits = low_bitmask_len (orig_mode, outer_const);
10447 if (care_bits >= 0
10448 && GET_MODE_PRECISION (orig_mode) - care_bits >= count)
10449 return mode;
10451 /* fall through */
10453 case ROTATE:
10454 return orig_mode;
10456 case ROTATERT:
10457 gcc_unreachable ();
10459 default:
10460 return mode;
10464 /* Simplify a shift of VAROP by ORIG_COUNT bits. CODE says what kind
10465 of shift. The result of the shift is RESULT_MODE. Return NULL_RTX
10466 if we cannot simplify it. Otherwise, return a simplified value.
10468 The shift is normally computed in the widest mode we find in VAROP, as
10469 long as it isn't a different number of words than RESULT_MODE. Exceptions
10470 are ASHIFTRT and ROTATE, which are always done in their original mode. */
10472 static rtx
10473 simplify_shift_const_1 (enum rtx_code code, machine_mode result_mode,
10474 rtx varop, int orig_count)
10476 enum rtx_code orig_code = code;
10477 rtx orig_varop = varop;
10478 int count, log2;
10479 machine_mode mode = result_mode;
10480 machine_mode shift_mode;
10481 scalar_int_mode tmode, inner_mode, int_mode, int_varop_mode, int_result_mode;
10482 /* We form (outer_op (code varop count) (outer_const)). */
10483 enum rtx_code outer_op = UNKNOWN;
10484 HOST_WIDE_INT outer_const = 0;
10485 int complement_p = 0;
10486 rtx new_rtx, x;
10488 /* Make sure and truncate the "natural" shift on the way in. We don't
10489 want to do this inside the loop as it makes it more difficult to
10490 combine shifts. */
10491 if (SHIFT_COUNT_TRUNCATED)
10492 orig_count &= GET_MODE_UNIT_BITSIZE (mode) - 1;
10494 /* If we were given an invalid count, don't do anything except exactly
10495 what was requested. */
10497 if (orig_count < 0 || orig_count >= (int) GET_MODE_UNIT_PRECISION (mode))
10498 return NULL_RTX;
10500 count = orig_count;
10502 /* Unless one of the branches of the `if' in this loop does a `continue',
10503 we will `break' the loop after the `if'. */
10505 while (count != 0)
10507 /* If we have an operand of (clobber (const_int 0)), fail. */
10508 if (GET_CODE (varop) == CLOBBER)
10509 return NULL_RTX;
10511 /* Convert ROTATERT to ROTATE. */
10512 if (code == ROTATERT)
10514 unsigned int bitsize = GET_MODE_UNIT_PRECISION (result_mode);
10515 code = ROTATE;
10516 count = bitsize - count;
10519 shift_mode = result_mode;
10520 if (shift_mode != mode)
10522 /* We only change the modes of scalar shifts. */
10523 int_mode = as_a <scalar_int_mode> (mode);
10524 int_result_mode = as_a <scalar_int_mode> (result_mode);
10525 shift_mode = try_widen_shift_mode (code, varop, count,
10526 int_result_mode, int_mode,
10527 outer_op, outer_const);
10530 scalar_int_mode shift_unit_mode
10531 = as_a <scalar_int_mode> (GET_MODE_INNER (shift_mode));
10533 /* Handle cases where the count is greater than the size of the mode
10534 minus 1. For ASHIFT, use the size minus one as the count (this can
10535 occur when simplifying (lshiftrt (ashiftrt ..))). For rotates,
10536 take the count modulo the size. For other shifts, the result is
10537 zero.
10539 Since these shifts are being produced by the compiler by combining
10540 multiple operations, each of which are defined, we know what the
10541 result is supposed to be. */
10543 if (count > (GET_MODE_PRECISION (shift_unit_mode) - 1))
10545 if (code == ASHIFTRT)
10546 count = GET_MODE_PRECISION (shift_unit_mode) - 1;
10547 else if (code == ROTATE || code == ROTATERT)
10548 count %= GET_MODE_PRECISION (shift_unit_mode);
10549 else
10551 /* We can't simply return zero because there may be an
10552 outer op. */
10553 varop = const0_rtx;
10554 count = 0;
10555 break;
10559 /* If we discovered we had to complement VAROP, leave. Making a NOT
10560 here would cause an infinite loop. */
10561 if (complement_p)
10562 break;
10564 if (shift_mode == shift_unit_mode)
10566 /* An arithmetic right shift of a quantity known to be -1 or 0
10567 is a no-op. */
10568 if (code == ASHIFTRT
10569 && (num_sign_bit_copies (varop, shift_unit_mode)
10570 == GET_MODE_PRECISION (shift_unit_mode)))
10572 count = 0;
10573 break;
10576 /* If we are doing an arithmetic right shift and discarding all but
10577 the sign bit copies, this is equivalent to doing a shift by the
10578 bitsize minus one. Convert it into that shift because it will
10579 often allow other simplifications. */
10581 if (code == ASHIFTRT
10582 && (count + num_sign_bit_copies (varop, shift_unit_mode)
10583 >= GET_MODE_PRECISION (shift_unit_mode)))
10584 count = GET_MODE_PRECISION (shift_unit_mode) - 1;
10586 /* We simplify the tests below and elsewhere by converting
10587 ASHIFTRT to LSHIFTRT if we know the sign bit is clear.
10588 `make_compound_operation' will convert it to an ASHIFTRT for
10589 those machines (such as VAX) that don't have an LSHIFTRT. */
10590 if (code == ASHIFTRT
10591 && HWI_COMPUTABLE_MODE_P (shift_unit_mode)
10592 && val_signbit_known_clear_p (shift_unit_mode,
10593 nonzero_bits (varop,
10594 shift_unit_mode)))
10595 code = LSHIFTRT;
10597 if (((code == LSHIFTRT
10598 && HWI_COMPUTABLE_MODE_P (shift_unit_mode)
10599 && !(nonzero_bits (varop, shift_unit_mode) >> count))
10600 || (code == ASHIFT
10601 && HWI_COMPUTABLE_MODE_P (shift_unit_mode)
10602 && !((nonzero_bits (varop, shift_unit_mode) << count)
10603 & GET_MODE_MASK (shift_unit_mode))))
10604 && !side_effects_p (varop))
10605 varop = const0_rtx;
10608 switch (GET_CODE (varop))
10610 case SIGN_EXTEND:
10611 case ZERO_EXTEND:
10612 case SIGN_EXTRACT:
10613 case ZERO_EXTRACT:
10614 new_rtx = expand_compound_operation (varop);
10615 if (new_rtx != varop)
10617 varop = new_rtx;
10618 continue;
10620 break;
10622 case MEM:
10623 /* The following rules apply only to scalars. */
10624 if (shift_mode != shift_unit_mode)
10625 break;
10626 int_mode = as_a <scalar_int_mode> (mode);
10628 /* If we have (xshiftrt (mem ...) C) and C is MODE_WIDTH
10629 minus the width of a smaller mode, we can do this with a
10630 SIGN_EXTEND or ZERO_EXTEND from the narrower memory location. */
10631 if ((code == ASHIFTRT || code == LSHIFTRT)
10632 && ! mode_dependent_address_p (XEXP (varop, 0),
10633 MEM_ADDR_SPACE (varop))
10634 && ! MEM_VOLATILE_P (varop)
10635 && (int_mode_for_size (GET_MODE_BITSIZE (int_mode) - count, 1)
10636 .exists (&tmode)))
10638 new_rtx = adjust_address_nv (varop, tmode,
10639 BYTES_BIG_ENDIAN ? 0
10640 : count / BITS_PER_UNIT);
10642 varop = gen_rtx_fmt_e (code == ASHIFTRT ? SIGN_EXTEND
10643 : ZERO_EXTEND, int_mode, new_rtx);
10644 count = 0;
10645 continue;
10647 break;
10649 case SUBREG:
10650 /* The following rules apply only to scalars. */
10651 if (shift_mode != shift_unit_mode)
10652 break;
10653 int_mode = as_a <scalar_int_mode> (mode);
10654 int_varop_mode = as_a <scalar_int_mode> (GET_MODE (varop));
10656 /* If VAROP is a SUBREG, strip it as long as the inner operand has
10657 the same number of words as what we've seen so far. Then store
10658 the widest mode in MODE. */
10659 if (subreg_lowpart_p (varop)
10660 && is_int_mode (GET_MODE (SUBREG_REG (varop)), &inner_mode)
10661 && GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (int_varop_mode)
10662 && (CEIL (GET_MODE_SIZE (inner_mode), UNITS_PER_WORD)
10663 == CEIL (GET_MODE_SIZE (int_mode), UNITS_PER_WORD))
10664 && GET_MODE_CLASS (int_varop_mode) == MODE_INT)
10666 varop = SUBREG_REG (varop);
10667 if (GET_MODE_SIZE (inner_mode) > GET_MODE_SIZE (int_mode))
10668 mode = inner_mode;
10669 continue;
10671 break;
10673 case MULT:
10674 /* Some machines use MULT instead of ASHIFT because MULT
10675 is cheaper. But it is still better on those machines to
10676 merge two shifts into one. */
10677 if (CONST_INT_P (XEXP (varop, 1))
10678 && (log2 = exact_log2 (UINTVAL (XEXP (varop, 1)))) >= 0)
10680 rtx log2_rtx = gen_int_shift_amount (GET_MODE (varop), log2);
10681 varop = simplify_gen_binary (ASHIFT, GET_MODE (varop),
10682 XEXP (varop, 0), log2_rtx);
10683 continue;
10685 break;
10687 case UDIV:
10688 /* Similar, for when divides are cheaper. */
10689 if (CONST_INT_P (XEXP (varop, 1))
10690 && (log2 = exact_log2 (UINTVAL (XEXP (varop, 1)))) >= 0)
10692 rtx log2_rtx = gen_int_shift_amount (GET_MODE (varop), log2);
10693 varop = simplify_gen_binary (LSHIFTRT, GET_MODE (varop),
10694 XEXP (varop, 0), log2_rtx);
10695 continue;
10697 break;
10699 case ASHIFTRT:
10700 /* If we are extracting just the sign bit of an arithmetic
10701 right shift, that shift is not needed. However, the sign
10702 bit of a wider mode may be different from what would be
10703 interpreted as the sign bit in a narrower mode, so, if
10704 the result is narrower, don't discard the shift. */
10705 if (code == LSHIFTRT
10706 && count == (GET_MODE_UNIT_BITSIZE (result_mode) - 1)
10707 && (GET_MODE_UNIT_BITSIZE (result_mode)
10708 >= GET_MODE_UNIT_BITSIZE (GET_MODE (varop))))
10710 varop = XEXP (varop, 0);
10711 continue;
10714 /* fall through */
10716 case LSHIFTRT:
10717 case ASHIFT:
10718 case ROTATE:
10719 /* The following rules apply only to scalars. */
10720 if (shift_mode != shift_unit_mode)
10721 break;
10722 int_mode = as_a <scalar_int_mode> (mode);
10723 int_varop_mode = as_a <scalar_int_mode> (GET_MODE (varop));
10724 int_result_mode = as_a <scalar_int_mode> (result_mode);
10726 /* Here we have two nested shifts. The result is usually the
10727 AND of a new shift with a mask. We compute the result below. */
10728 if (CONST_INT_P (XEXP (varop, 1))
10729 && INTVAL (XEXP (varop, 1)) >= 0
10730 && INTVAL (XEXP (varop, 1)) < GET_MODE_PRECISION (int_varop_mode)
10731 && HWI_COMPUTABLE_MODE_P (int_result_mode)
10732 && HWI_COMPUTABLE_MODE_P (int_mode))
10734 enum rtx_code first_code = GET_CODE (varop);
10735 unsigned int first_count = INTVAL (XEXP (varop, 1));
10736 unsigned HOST_WIDE_INT mask;
10737 rtx mask_rtx;
10739 /* We have one common special case. We can't do any merging if
10740 the inner code is an ASHIFTRT of a smaller mode. However, if
10741 we have (ashift:M1 (subreg:M1 (ashiftrt:M2 FOO C1) 0) C2)
10742 with C2 == GET_MODE_BITSIZE (M1) - GET_MODE_BITSIZE (M2),
10743 we can convert it to
10744 (ashiftrt:M1 (ashift:M1 (and:M1 (subreg:M1 FOO 0) C3) C2) C1).
10745 This simplifies certain SIGN_EXTEND operations. */
10746 if (code == ASHIFT && first_code == ASHIFTRT
10747 && count == (GET_MODE_PRECISION (int_result_mode)
10748 - GET_MODE_PRECISION (int_varop_mode)))
10750 /* C3 has the low-order C1 bits zero. */
10752 mask = GET_MODE_MASK (int_mode)
10753 & ~((HOST_WIDE_INT_1U << first_count) - 1);
10755 varop = simplify_and_const_int (NULL_RTX, int_result_mode,
10756 XEXP (varop, 0), mask);
10757 varop = simplify_shift_const (NULL_RTX, ASHIFT,
10758 int_result_mode, varop, count);
10759 count = first_count;
10760 code = ASHIFTRT;
10761 continue;
10764 /* If this was (ashiftrt (ashift foo C1) C2) and FOO has more
10765 than C1 high-order bits equal to the sign bit, we can convert
10766 this to either an ASHIFT or an ASHIFTRT depending on the
10767 two counts.
10769 We cannot do this if VAROP's mode is not SHIFT_UNIT_MODE. */
10771 if (code == ASHIFTRT && first_code == ASHIFT
10772 && int_varop_mode == shift_unit_mode
10773 && (num_sign_bit_copies (XEXP (varop, 0), shift_unit_mode)
10774 > first_count))
10776 varop = XEXP (varop, 0);
10777 count -= first_count;
10778 if (count < 0)
10780 count = -count;
10781 code = ASHIFT;
10784 continue;
10787 /* There are some cases we can't do. If CODE is ASHIFTRT,
10788 we can only do this if FIRST_CODE is also ASHIFTRT.
10790 We can't do the case when CODE is ROTATE and FIRST_CODE is
10791 ASHIFTRT.
10793 If the mode of this shift is not the mode of the outer shift,
10794 we can't do this if either shift is a right shift or ROTATE.
10796 Finally, we can't do any of these if the mode is too wide
10797 unless the codes are the same.
10799 Handle the case where the shift codes are the same
10800 first. */
10802 if (code == first_code)
10804 if (int_varop_mode != int_result_mode
10805 && (code == ASHIFTRT || code == LSHIFTRT
10806 || code == ROTATE))
10807 break;
10809 count += first_count;
10810 varop = XEXP (varop, 0);
10811 continue;
10814 if (code == ASHIFTRT
10815 || (code == ROTATE && first_code == ASHIFTRT)
10816 || GET_MODE_PRECISION (int_mode) > HOST_BITS_PER_WIDE_INT
10817 || (int_varop_mode != int_result_mode
10818 && (first_code == ASHIFTRT || first_code == LSHIFTRT
10819 || first_code == ROTATE
10820 || code == ROTATE)))
10821 break;
10823 /* To compute the mask to apply after the shift, shift the
10824 nonzero bits of the inner shift the same way the
10825 outer shift will. */
10827 mask_rtx = gen_int_mode (nonzero_bits (varop, int_varop_mode),
10828 int_result_mode);
10829 rtx count_rtx = gen_int_shift_amount (int_result_mode, count);
10830 mask_rtx
10831 = simplify_const_binary_operation (code, int_result_mode,
10832 mask_rtx, count_rtx);
10834 /* Give up if we can't compute an outer operation to use. */
10835 if (mask_rtx == 0
10836 || !CONST_INT_P (mask_rtx)
10837 || ! merge_outer_ops (&outer_op, &outer_const, AND,
10838 INTVAL (mask_rtx),
10839 int_result_mode, &complement_p))
10840 break;
10842 /* If the shifts are in the same direction, we add the
10843 counts. Otherwise, we subtract them. */
10844 if ((code == ASHIFTRT || code == LSHIFTRT)
10845 == (first_code == ASHIFTRT || first_code == LSHIFTRT))
10846 count += first_count;
10847 else
10848 count -= first_count;
10850 /* If COUNT is positive, the new shift is usually CODE,
10851 except for the two exceptions below, in which case it is
10852 FIRST_CODE. If the count is negative, FIRST_CODE should
10853 always be used */
10854 if (count > 0
10855 && ((first_code == ROTATE && code == ASHIFT)
10856 || (first_code == ASHIFTRT && code == LSHIFTRT)))
10857 code = first_code;
10858 else if (count < 0)
10859 code = first_code, count = -count;
10861 varop = XEXP (varop, 0);
10862 continue;
10865 /* If we have (A << B << C) for any shift, we can convert this to
10866 (A << C << B). This wins if A is a constant. Only try this if
10867 B is not a constant. */
10869 else if (GET_CODE (varop) == code
10870 && CONST_INT_P (XEXP (varop, 0))
10871 && !CONST_INT_P (XEXP (varop, 1)))
10873 /* For ((unsigned) (cstULL >> count)) >> cst2 we have to make
10874 sure the result will be masked. See PR70222. */
10875 if (code == LSHIFTRT
10876 && int_mode != int_result_mode
10877 && !merge_outer_ops (&outer_op, &outer_const, AND,
10878 GET_MODE_MASK (int_result_mode)
10879 >> orig_count, int_result_mode,
10880 &complement_p))
10881 break;
10882 /* For ((int) (cstLL >> count)) >> cst2 just give up. Queuing
10883 up outer sign extension (often left and right shift) is
10884 hardly more efficient than the original. See PR70429. */
10885 if (code == ASHIFTRT && int_mode != int_result_mode)
10886 break;
10888 rtx count_rtx = gen_int_shift_amount (int_result_mode, count);
10889 rtx new_rtx = simplify_const_binary_operation (code, int_mode,
10890 XEXP (varop, 0),
10891 count_rtx);
10892 varop = gen_rtx_fmt_ee (code, int_mode, new_rtx, XEXP (varop, 1));
10893 count = 0;
10894 continue;
10896 break;
10898 case NOT:
10899 /* The following rules apply only to scalars. */
10900 if (shift_mode != shift_unit_mode)
10901 break;
10903 /* Make this fit the case below. */
10904 varop = gen_rtx_XOR (mode, XEXP (varop, 0), constm1_rtx);
10905 continue;
10907 case IOR:
10908 case AND:
10909 case XOR:
10910 /* The following rules apply only to scalars. */
10911 if (shift_mode != shift_unit_mode)
10912 break;
10913 int_varop_mode = as_a <scalar_int_mode> (GET_MODE (varop));
10914 int_result_mode = as_a <scalar_int_mode> (result_mode);
10916 /* If we have (xshiftrt (ior (plus X (const_int -1)) X) C)
10917 with C the size of VAROP - 1 and the shift is logical if
10918 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
10919 we have an (le X 0) operation. If we have an arithmetic shift
10920 and STORE_FLAG_VALUE is 1 or we have a logical shift with
10921 STORE_FLAG_VALUE of -1, we have a (neg (le X 0)) operation. */
10923 if (GET_CODE (varop) == IOR && GET_CODE (XEXP (varop, 0)) == PLUS
10924 && XEXP (XEXP (varop, 0), 1) == constm1_rtx
10925 && (STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
10926 && (code == LSHIFTRT || code == ASHIFTRT)
10927 && count == (GET_MODE_PRECISION (int_varop_mode) - 1)
10928 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
10930 count = 0;
10931 varop = gen_rtx_LE (int_varop_mode, XEXP (varop, 1),
10932 const0_rtx);
10934 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
10935 varop = gen_rtx_NEG (int_varop_mode, varop);
10937 continue;
10940 /* If we have (shift (logical)), move the logical to the outside
10941 to allow it to possibly combine with another logical and the
10942 shift to combine with another shift. This also canonicalizes to
10943 what a ZERO_EXTRACT looks like. Also, some machines have
10944 (and (shift)) insns. */
10946 if (CONST_INT_P (XEXP (varop, 1))
10947 /* We can't do this if we have (ashiftrt (xor)) and the
10948 constant has its sign bit set in shift_unit_mode with
10949 shift_unit_mode wider than result_mode. */
10950 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
10951 && int_result_mode != shift_unit_mode
10952 && trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
10953 shift_unit_mode) < 0)
10954 && (new_rtx = simplify_const_binary_operation
10955 (code, int_result_mode,
10956 gen_int_mode (INTVAL (XEXP (varop, 1)), int_result_mode),
10957 gen_int_shift_amount (int_result_mode, count))) != 0
10958 && CONST_INT_P (new_rtx)
10959 && merge_outer_ops (&outer_op, &outer_const, GET_CODE (varop),
10960 INTVAL (new_rtx), int_result_mode,
10961 &complement_p))
10963 varop = XEXP (varop, 0);
10964 continue;
10967 /* If we can't do that, try to simplify the shift in each arm of the
10968 logical expression, make a new logical expression, and apply
10969 the inverse distributive law. This also can't be done for
10970 (ashiftrt (xor)) where we've widened the shift and the constant
10971 changes the sign bit. */
10972 if (CONST_INT_P (XEXP (varop, 1))
10973 && !(code == ASHIFTRT && GET_CODE (varop) == XOR
10974 && int_result_mode != shift_unit_mode
10975 && trunc_int_for_mode (INTVAL (XEXP (varop, 1)),
10976 shift_unit_mode) < 0))
10978 rtx lhs = simplify_shift_const (NULL_RTX, code, shift_unit_mode,
10979 XEXP (varop, 0), count);
10980 rtx rhs = simplify_shift_const (NULL_RTX, code, shift_unit_mode,
10981 XEXP (varop, 1), count);
10983 varop = simplify_gen_binary (GET_CODE (varop), shift_unit_mode,
10984 lhs, rhs);
10985 varop = apply_distributive_law (varop);
10987 count = 0;
10988 continue;
10990 break;
10992 case EQ:
10993 /* The following rules apply only to scalars. */
10994 if (shift_mode != shift_unit_mode)
10995 break;
10996 int_result_mode = as_a <scalar_int_mode> (result_mode);
10998 /* Convert (lshiftrt (eq FOO 0) C) to (xor FOO 1) if STORE_FLAG_VALUE
10999 says that the sign bit can be tested, FOO has mode MODE, C is
11000 GET_MODE_PRECISION (MODE) - 1, and FOO has only its low-order bit
11001 that may be nonzero. */
11002 if (code == LSHIFTRT
11003 && XEXP (varop, 1) == const0_rtx
11004 && GET_MODE (XEXP (varop, 0)) == int_result_mode
11005 && count == (GET_MODE_PRECISION (int_result_mode) - 1)
11006 && HWI_COMPUTABLE_MODE_P (int_result_mode)
11007 && STORE_FLAG_VALUE == -1
11008 && nonzero_bits (XEXP (varop, 0), int_result_mode) == 1
11009 && merge_outer_ops (&outer_op, &outer_const, XOR, 1,
11010 int_result_mode, &complement_p))
11012 varop = XEXP (varop, 0);
11013 count = 0;
11014 continue;
11016 break;
11018 case NEG:
11019 /* The following rules apply only to scalars. */
11020 if (shift_mode != shift_unit_mode)
11021 break;
11022 int_result_mode = as_a <scalar_int_mode> (result_mode);
11024 /* (lshiftrt (neg A) C) where A is either 0 or 1 and C is one less
11025 than the number of bits in the mode is equivalent to A. */
11026 if (code == LSHIFTRT
11027 && count == (GET_MODE_PRECISION (int_result_mode) - 1)
11028 && nonzero_bits (XEXP (varop, 0), int_result_mode) == 1)
11030 varop = XEXP (varop, 0);
11031 count = 0;
11032 continue;
11035 /* NEG commutes with ASHIFT since it is multiplication. Move the
11036 NEG outside to allow shifts to combine. */
11037 if (code == ASHIFT
11038 && merge_outer_ops (&outer_op, &outer_const, NEG, 0,
11039 int_result_mode, &complement_p))
11041 varop = XEXP (varop, 0);
11042 continue;
11044 break;
11046 case PLUS:
11047 /* The following rules apply only to scalars. */
11048 if (shift_mode != shift_unit_mode)
11049 break;
11050 int_result_mode = as_a <scalar_int_mode> (result_mode);
11052 /* (lshiftrt (plus A -1) C) where A is either 0 or 1 and C
11053 is one less than the number of bits in the mode is
11054 equivalent to (xor A 1). */
11055 if (code == LSHIFTRT
11056 && count == (GET_MODE_PRECISION (int_result_mode) - 1)
11057 && XEXP (varop, 1) == constm1_rtx
11058 && nonzero_bits (XEXP (varop, 0), int_result_mode) == 1
11059 && merge_outer_ops (&outer_op, &outer_const, XOR, 1,
11060 int_result_mode, &complement_p))
11062 count = 0;
11063 varop = XEXP (varop, 0);
11064 continue;
11067 /* If we have (xshiftrt (plus FOO BAR) C), and the only bits
11068 that might be nonzero in BAR are those being shifted out and those
11069 bits are known zero in FOO, we can replace the PLUS with FOO.
11070 Similarly in the other operand order. This code occurs when
11071 we are computing the size of a variable-size array. */
11073 if ((code == ASHIFTRT || code == LSHIFTRT)
11074 && count < HOST_BITS_PER_WIDE_INT
11075 && nonzero_bits (XEXP (varop, 1), int_result_mode) >> count == 0
11076 && (nonzero_bits (XEXP (varop, 1), int_result_mode)
11077 & nonzero_bits (XEXP (varop, 0), int_result_mode)) == 0)
11079 varop = XEXP (varop, 0);
11080 continue;
11082 else if ((code == ASHIFTRT || code == LSHIFTRT)
11083 && count < HOST_BITS_PER_WIDE_INT
11084 && HWI_COMPUTABLE_MODE_P (int_result_mode)
11085 && (nonzero_bits (XEXP (varop, 0), int_result_mode)
11086 >> count) == 0
11087 && (nonzero_bits (XEXP (varop, 0), int_result_mode)
11088 & nonzero_bits (XEXP (varop, 1), int_result_mode)) == 0)
11090 varop = XEXP (varop, 1);
11091 continue;
11094 /* (ashift (plus foo C) N) is (plus (ashift foo N) C'). */
11095 if (code == ASHIFT
11096 && CONST_INT_P (XEXP (varop, 1))
11097 && (new_rtx = simplify_const_binary_operation
11098 (ASHIFT, int_result_mode,
11099 gen_int_mode (INTVAL (XEXP (varop, 1)), int_result_mode),
11100 gen_int_shift_amount (int_result_mode, count))) != 0
11101 && CONST_INT_P (new_rtx)
11102 && merge_outer_ops (&outer_op, &outer_const, PLUS,
11103 INTVAL (new_rtx), int_result_mode,
11104 &complement_p))
11106 varop = XEXP (varop, 0);
11107 continue;
11110 /* Check for 'PLUS signbit', which is the canonical form of 'XOR
11111 signbit', and attempt to change the PLUS to an XOR and move it to
11112 the outer operation as is done above in the AND/IOR/XOR case
11113 leg for shift(logical). See details in logical handling above
11114 for reasoning in doing so. */
11115 if (code == LSHIFTRT
11116 && CONST_INT_P (XEXP (varop, 1))
11117 && mode_signbit_p (int_result_mode, XEXP (varop, 1))
11118 && (new_rtx = simplify_const_binary_operation
11119 (code, int_result_mode,
11120 gen_int_mode (INTVAL (XEXP (varop, 1)), int_result_mode),
11121 gen_int_shift_amount (int_result_mode, count))) != 0
11122 && CONST_INT_P (new_rtx)
11123 && merge_outer_ops (&outer_op, &outer_const, XOR,
11124 INTVAL (new_rtx), int_result_mode,
11125 &complement_p))
11127 varop = XEXP (varop, 0);
11128 continue;
11131 break;
11133 case MINUS:
11134 /* The following rules apply only to scalars. */
11135 if (shift_mode != shift_unit_mode)
11136 break;
11137 int_varop_mode = as_a <scalar_int_mode> (GET_MODE (varop));
11139 /* If we have (xshiftrt (minus (ashiftrt X C)) X) C)
11140 with C the size of VAROP - 1 and the shift is logical if
11141 STORE_FLAG_VALUE is 1 and arithmetic if STORE_FLAG_VALUE is -1,
11142 we have a (gt X 0) operation. If the shift is arithmetic with
11143 STORE_FLAG_VALUE of 1 or logical with STORE_FLAG_VALUE == -1,
11144 we have a (neg (gt X 0)) operation. */
11146 if ((STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1)
11147 && GET_CODE (XEXP (varop, 0)) == ASHIFTRT
11148 && count == (GET_MODE_PRECISION (int_varop_mode) - 1)
11149 && (code == LSHIFTRT || code == ASHIFTRT)
11150 && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
11151 && INTVAL (XEXP (XEXP (varop, 0), 1)) == count
11152 && rtx_equal_p (XEXP (XEXP (varop, 0), 0), XEXP (varop, 1)))
11154 count = 0;
11155 varop = gen_rtx_GT (int_varop_mode, XEXP (varop, 1),
11156 const0_rtx);
11158 if (STORE_FLAG_VALUE == 1 ? code == ASHIFTRT : code == LSHIFTRT)
11159 varop = gen_rtx_NEG (int_varop_mode, varop);
11161 continue;
11163 break;
11165 case TRUNCATE:
11166 /* Change (lshiftrt (truncate (lshiftrt))) to (truncate (lshiftrt))
11167 if the truncate does not affect the value. */
11168 if (code == LSHIFTRT
11169 && GET_CODE (XEXP (varop, 0)) == LSHIFTRT
11170 && CONST_INT_P (XEXP (XEXP (varop, 0), 1))
11171 && (INTVAL (XEXP (XEXP (varop, 0), 1))
11172 >= (GET_MODE_UNIT_PRECISION (GET_MODE (XEXP (varop, 0)))
11173 - GET_MODE_UNIT_PRECISION (GET_MODE (varop)))))
11175 rtx varop_inner = XEXP (varop, 0);
11176 int new_count = count + INTVAL (XEXP (varop_inner, 1));
11177 rtx new_count_rtx = gen_int_shift_amount (GET_MODE (varop_inner),
11178 new_count);
11179 varop_inner = gen_rtx_LSHIFTRT (GET_MODE (varop_inner),
11180 XEXP (varop_inner, 0),
11181 new_count_rtx);
11182 varop = gen_rtx_TRUNCATE (GET_MODE (varop), varop_inner);
11183 count = 0;
11184 continue;
11186 break;
11188 default:
11189 break;
11192 break;
11195 shift_mode = result_mode;
11196 if (shift_mode != mode)
11198 /* We only change the modes of scalar shifts. */
11199 int_mode = as_a <scalar_int_mode> (mode);
11200 int_result_mode = as_a <scalar_int_mode> (result_mode);
11201 shift_mode = try_widen_shift_mode (code, varop, count, int_result_mode,
11202 int_mode, outer_op, outer_const);
11205 /* We have now finished analyzing the shift. The result should be
11206 a shift of type CODE with SHIFT_MODE shifting VAROP COUNT places. If
11207 OUTER_OP is non-UNKNOWN, it is an operation that needs to be applied
11208 to the result of the shift. OUTER_CONST is the relevant constant,
11209 but we must turn off all bits turned off in the shift. */
11211 if (outer_op == UNKNOWN
11212 && orig_code == code && orig_count == count
11213 && varop == orig_varop
11214 && shift_mode == GET_MODE (varop))
11215 return NULL_RTX;
11217 /* Make a SUBREG if necessary. If we can't make it, fail. */
11218 varop = gen_lowpart (shift_mode, varop);
11219 if (varop == NULL_RTX || GET_CODE (varop) == CLOBBER)
11220 return NULL_RTX;
11222 /* If we have an outer operation and we just made a shift, it is
11223 possible that we could have simplified the shift were it not
11224 for the outer operation. So try to do the simplification
11225 recursively. */
11227 if (outer_op != UNKNOWN)
11228 x = simplify_shift_const_1 (code, shift_mode, varop, count);
11229 else
11230 x = NULL_RTX;
11232 if (x == NULL_RTX)
11233 x = simplify_gen_binary (code, shift_mode, varop,
11234 gen_int_shift_amount (shift_mode, count));
11236 /* If we were doing an LSHIFTRT in a wider mode than it was originally,
11237 turn off all the bits that the shift would have turned off. */
11238 if (orig_code == LSHIFTRT && result_mode != shift_mode)
11239 /* We only change the modes of scalar shifts. */
11240 x = simplify_and_const_int (NULL_RTX, as_a <scalar_int_mode> (shift_mode),
11241 x, GET_MODE_MASK (result_mode) >> orig_count);
11243 /* Do the remainder of the processing in RESULT_MODE. */
11244 x = gen_lowpart_or_truncate (result_mode, x);
11246 /* If COMPLEMENT_P is set, we have to complement X before doing the outer
11247 operation. */
11248 if (complement_p)
11249 x = simplify_gen_unary (NOT, result_mode, x, result_mode);
11251 if (outer_op != UNKNOWN)
11253 int_result_mode = as_a <scalar_int_mode> (result_mode);
11255 if (GET_RTX_CLASS (outer_op) != RTX_UNARY
11256 && GET_MODE_PRECISION (int_result_mode) < HOST_BITS_PER_WIDE_INT)
11257 outer_const = trunc_int_for_mode (outer_const, int_result_mode);
11259 if (outer_op == AND)
11260 x = simplify_and_const_int (NULL_RTX, int_result_mode, x, outer_const);
11261 else if (outer_op == SET)
11263 /* This means that we have determined that the result is
11264 equivalent to a constant. This should be rare. */
11265 if (!side_effects_p (x))
11266 x = GEN_INT (outer_const);
11268 else if (GET_RTX_CLASS (outer_op) == RTX_UNARY)
11269 x = simplify_gen_unary (outer_op, int_result_mode, x, int_result_mode);
11270 else
11271 x = simplify_gen_binary (outer_op, int_result_mode, x,
11272 GEN_INT (outer_const));
11275 return x;
11278 /* Simplify a shift of VAROP by COUNT bits. CODE says what kind of shift.
11279 The result of the shift is RESULT_MODE. If we cannot simplify it,
11280 return X or, if it is NULL, synthesize the expression with
11281 simplify_gen_binary. Otherwise, return a simplified value.
11283 The shift is normally computed in the widest mode we find in VAROP, as
11284 long as it isn't a different number of words than RESULT_MODE. Exceptions
11285 are ASHIFTRT and ROTATE, which are always done in their original mode. */
11287 static rtx
11288 simplify_shift_const (rtx x, enum rtx_code code, machine_mode result_mode,
11289 rtx varop, int count)
11291 rtx tem = simplify_shift_const_1 (code, result_mode, varop, count);
11292 if (tem)
11293 return tem;
11295 if (!x)
11296 x = simplify_gen_binary (code, GET_MODE (varop), varop,
11297 gen_int_shift_amount (GET_MODE (varop), count));
11298 if (GET_MODE (x) != result_mode)
11299 x = gen_lowpart (result_mode, x);
11300 return x;
11304 /* A subroutine of recog_for_combine. See there for arguments and
11305 return value. */
11307 static int
11308 recog_for_combine_1 (rtx *pnewpat, rtx_insn *insn, rtx *pnotes)
11310 rtx pat = *pnewpat;
11311 rtx pat_without_clobbers;
11312 int insn_code_number;
11313 int num_clobbers_to_add = 0;
11314 int i;
11315 rtx notes = NULL_RTX;
11316 rtx old_notes, old_pat;
11317 int old_icode;
11319 /* If PAT is a PARALLEL, check to see if it contains the CLOBBER
11320 we use to indicate that something didn't match. If we find such a
11321 thing, force rejection. */
11322 if (GET_CODE (pat) == PARALLEL)
11323 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
11324 if (GET_CODE (XVECEXP (pat, 0, i)) == CLOBBER
11325 && XEXP (XVECEXP (pat, 0, i), 0) == const0_rtx)
11326 return -1;
11328 old_pat = PATTERN (insn);
11329 old_notes = REG_NOTES (insn);
11330 PATTERN (insn) = pat;
11331 REG_NOTES (insn) = NULL_RTX;
11333 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
11334 if (dump_file && (dump_flags & TDF_DETAILS))
11336 if (insn_code_number < 0)
11337 fputs ("Failed to match this instruction:\n", dump_file);
11338 else
11339 fputs ("Successfully matched this instruction:\n", dump_file);
11340 print_rtl_single (dump_file, pat);
11343 /* If it isn't, there is the possibility that we previously had an insn
11344 that clobbered some register as a side effect, but the combined
11345 insn doesn't need to do that. So try once more without the clobbers
11346 unless this represents an ASM insn. */
11348 if (insn_code_number < 0 && ! check_asm_operands (pat)
11349 && GET_CODE (pat) == PARALLEL)
11351 int pos;
11353 for (pos = 0, i = 0; i < XVECLEN (pat, 0); i++)
11354 if (GET_CODE (XVECEXP (pat, 0, i)) != CLOBBER)
11356 if (i != pos)
11357 SUBST (XVECEXP (pat, 0, pos), XVECEXP (pat, 0, i));
11358 pos++;
11361 SUBST_INT (XVECLEN (pat, 0), pos);
11363 if (pos == 1)
11364 pat = XVECEXP (pat, 0, 0);
11366 PATTERN (insn) = pat;
11367 insn_code_number = recog (pat, insn, &num_clobbers_to_add);
11368 if (dump_file && (dump_flags & TDF_DETAILS))
11370 if (insn_code_number < 0)
11371 fputs ("Failed to match this instruction:\n", dump_file);
11372 else
11373 fputs ("Successfully matched this instruction:\n", dump_file);
11374 print_rtl_single (dump_file, pat);
11378 pat_without_clobbers = pat;
11380 PATTERN (insn) = old_pat;
11381 REG_NOTES (insn) = old_notes;
11383 /* Recognize all noop sets, these will be killed by followup pass. */
11384 if (insn_code_number < 0 && GET_CODE (pat) == SET && set_noop_p (pat))
11385 insn_code_number = NOOP_MOVE_INSN_CODE, num_clobbers_to_add = 0;
11387 /* If we had any clobbers to add, make a new pattern than contains
11388 them. Then check to make sure that all of them are dead. */
11389 if (num_clobbers_to_add)
11391 rtx newpat = gen_rtx_PARALLEL (VOIDmode,
11392 rtvec_alloc (GET_CODE (pat) == PARALLEL
11393 ? (XVECLEN (pat, 0)
11394 + num_clobbers_to_add)
11395 : num_clobbers_to_add + 1));
11397 if (GET_CODE (pat) == PARALLEL)
11398 for (i = 0; i < XVECLEN (pat, 0); i++)
11399 XVECEXP (newpat, 0, i) = XVECEXP (pat, 0, i);
11400 else
11401 XVECEXP (newpat, 0, 0) = pat;
11403 add_clobbers (newpat, insn_code_number);
11405 for (i = XVECLEN (newpat, 0) - num_clobbers_to_add;
11406 i < XVECLEN (newpat, 0); i++)
11408 if (REG_P (XEXP (XVECEXP (newpat, 0, i), 0))
11409 && ! reg_dead_at_p (XEXP (XVECEXP (newpat, 0, i), 0), insn))
11410 return -1;
11411 if (GET_CODE (XEXP (XVECEXP (newpat, 0, i), 0)) != SCRATCH)
11413 gcc_assert (REG_P (XEXP (XVECEXP (newpat, 0, i), 0)));
11414 notes = alloc_reg_note (REG_UNUSED,
11415 XEXP (XVECEXP (newpat, 0, i), 0), notes);
11418 pat = newpat;
11421 if (insn_code_number >= 0
11422 && insn_code_number != NOOP_MOVE_INSN_CODE)
11424 old_pat = PATTERN (insn);
11425 old_notes = REG_NOTES (insn);
11426 old_icode = INSN_CODE (insn);
11427 PATTERN (insn) = pat;
11428 REG_NOTES (insn) = notes;
11429 INSN_CODE (insn) = insn_code_number;
11431 /* Allow targets to reject combined insn. */
11432 if (!targetm.legitimate_combined_insn (insn))
11434 if (dump_file && (dump_flags & TDF_DETAILS))
11435 fputs ("Instruction not appropriate for target.",
11436 dump_file);
11438 /* Callers expect recog_for_combine to strip
11439 clobbers from the pattern on failure. */
11440 pat = pat_without_clobbers;
11441 notes = NULL_RTX;
11443 insn_code_number = -1;
11446 PATTERN (insn) = old_pat;
11447 REG_NOTES (insn) = old_notes;
11448 INSN_CODE (insn) = old_icode;
11451 *pnewpat = pat;
11452 *pnotes = notes;
11454 return insn_code_number;
11457 /* Change every ZERO_EXTRACT and ZERO_EXTEND of a SUBREG that can be
11458 expressed as an AND and maybe an LSHIFTRT, to that formulation.
11459 Return whether anything was so changed. */
11461 static bool
11462 change_zero_ext (rtx pat)
11464 bool changed = false;
11465 rtx *src = &SET_SRC (pat);
11467 subrtx_ptr_iterator::array_type array;
11468 FOR_EACH_SUBRTX_PTR (iter, array, src, NONCONST)
11470 rtx x = **iter;
11471 scalar_int_mode mode, inner_mode;
11472 if (!is_a <scalar_int_mode> (GET_MODE (x), &mode))
11473 continue;
11474 int size;
11476 if (GET_CODE (x) == ZERO_EXTRACT
11477 && CONST_INT_P (XEXP (x, 1))
11478 && CONST_INT_P (XEXP (x, 2))
11479 && is_a <scalar_int_mode> (GET_MODE (XEXP (x, 0)), &inner_mode)
11480 && GET_MODE_PRECISION (inner_mode) <= GET_MODE_PRECISION (mode))
11482 size = INTVAL (XEXP (x, 1));
11484 int start = INTVAL (XEXP (x, 2));
11485 if (BITS_BIG_ENDIAN)
11486 start = GET_MODE_PRECISION (inner_mode) - size - start;
11488 if (start != 0)
11489 x = gen_rtx_LSHIFTRT (inner_mode, XEXP (x, 0),
11490 gen_int_shift_amount (inner_mode, start));
11491 else
11492 x = XEXP (x, 0);
11494 if (mode != inner_mode)
11496 if (REG_P (x) && HARD_REGISTER_P (x)
11497 && !can_change_dest_mode (x, 0, mode))
11498 continue;
11500 x = gen_lowpart_SUBREG (mode, x);
11503 else if (GET_CODE (x) == ZERO_EXTEND
11504 && GET_CODE (XEXP (x, 0)) == SUBREG
11505 && SCALAR_INT_MODE_P (GET_MODE (SUBREG_REG (XEXP (x, 0))))
11506 && !paradoxical_subreg_p (XEXP (x, 0))
11507 && subreg_lowpart_p (XEXP (x, 0)))
11509 inner_mode = as_a <scalar_int_mode> (GET_MODE (XEXP (x, 0)));
11510 size = GET_MODE_PRECISION (inner_mode);
11511 x = SUBREG_REG (XEXP (x, 0));
11512 if (GET_MODE (x) != mode)
11514 if (REG_P (x) && HARD_REGISTER_P (x)
11515 && !can_change_dest_mode (x, 0, mode))
11516 continue;
11518 x = gen_lowpart_SUBREG (mode, x);
11521 else if (GET_CODE (x) == ZERO_EXTEND
11522 && REG_P (XEXP (x, 0))
11523 && HARD_REGISTER_P (XEXP (x, 0))
11524 && can_change_dest_mode (XEXP (x, 0), 0, mode))
11526 inner_mode = as_a <scalar_int_mode> (GET_MODE (XEXP (x, 0)));
11527 size = GET_MODE_PRECISION (inner_mode);
11528 x = gen_rtx_REG (mode, REGNO (XEXP (x, 0)));
11530 else
11531 continue;
11533 if (!(GET_CODE (x) == LSHIFTRT
11534 && CONST_INT_P (XEXP (x, 1))
11535 && size + INTVAL (XEXP (x, 1)) == GET_MODE_PRECISION (mode)))
11537 wide_int mask = wi::mask (size, false, GET_MODE_PRECISION (mode));
11538 x = gen_rtx_AND (mode, x, immed_wide_int_const (mask, mode));
11541 SUBST (**iter, x);
11542 changed = true;
11545 if (changed)
11546 FOR_EACH_SUBRTX_PTR (iter, array, src, NONCONST)
11547 maybe_swap_commutative_operands (**iter);
11549 rtx *dst = &SET_DEST (pat);
11550 scalar_int_mode mode;
11551 if (GET_CODE (*dst) == ZERO_EXTRACT
11552 && REG_P (XEXP (*dst, 0))
11553 && is_a <scalar_int_mode> (GET_MODE (XEXP (*dst, 0)), &mode)
11554 && CONST_INT_P (XEXP (*dst, 1))
11555 && CONST_INT_P (XEXP (*dst, 2)))
11557 rtx reg = XEXP (*dst, 0);
11558 int width = INTVAL (XEXP (*dst, 1));
11559 int offset = INTVAL (XEXP (*dst, 2));
11560 int reg_width = GET_MODE_PRECISION (mode);
11561 if (BITS_BIG_ENDIAN)
11562 offset = reg_width - width - offset;
11564 rtx x, y, z, w;
11565 wide_int mask = wi::shifted_mask (offset, width, true, reg_width);
11566 wide_int mask2 = wi::shifted_mask (offset, width, false, reg_width);
11567 x = gen_rtx_AND (mode, reg, immed_wide_int_const (mask, mode));
11568 if (offset)
11569 y = gen_rtx_ASHIFT (mode, SET_SRC (pat), GEN_INT (offset));
11570 else
11571 y = SET_SRC (pat);
11572 z = gen_rtx_AND (mode, y, immed_wide_int_const (mask2, mode));
11573 w = gen_rtx_IOR (mode, x, z);
11574 SUBST (SET_DEST (pat), reg);
11575 SUBST (SET_SRC (pat), w);
11577 changed = true;
11580 return changed;
11583 /* Like recog, but we receive the address of a pointer to a new pattern.
11584 We try to match the rtx that the pointer points to.
11585 If that fails, we may try to modify or replace the pattern,
11586 storing the replacement into the same pointer object.
11588 Modifications include deletion or addition of CLOBBERs. If the
11589 instruction will still not match, we change ZERO_EXTEND and ZERO_EXTRACT
11590 to the equivalent AND and perhaps LSHIFTRT patterns, and try with that
11591 (and undo if that fails).
11593 PNOTES is a pointer to a location where any REG_UNUSED notes added for
11594 the CLOBBERs are placed.
11596 The value is the final insn code from the pattern ultimately matched,
11597 or -1. */
11599 static int
11600 recog_for_combine (rtx *pnewpat, rtx_insn *insn, rtx *pnotes)
11602 rtx pat = *pnewpat;
11603 int insn_code_number = recog_for_combine_1 (pnewpat, insn, pnotes);
11604 if (insn_code_number >= 0 || check_asm_operands (pat))
11605 return insn_code_number;
11607 void *marker = get_undo_marker ();
11608 bool changed = false;
11610 if (GET_CODE (pat) == SET)
11611 changed = change_zero_ext (pat);
11612 else if (GET_CODE (pat) == PARALLEL)
11614 int i;
11615 for (i = 0; i < XVECLEN (pat, 0); i++)
11617 rtx set = XVECEXP (pat, 0, i);
11618 if (GET_CODE (set) == SET)
11619 changed |= change_zero_ext (set);
11623 if (changed)
11625 insn_code_number = recog_for_combine_1 (pnewpat, insn, pnotes);
11627 if (insn_code_number < 0)
11628 undo_to_marker (marker);
11631 return insn_code_number;
11634 /* Like gen_lowpart_general but for use by combine. In combine it
11635 is not possible to create any new pseudoregs. However, it is
11636 safe to create invalid memory addresses, because combine will
11637 try to recognize them and all they will do is make the combine
11638 attempt fail.
11640 If for some reason this cannot do its job, an rtx
11641 (clobber (const_int 0)) is returned.
11642 An insn containing that will not be recognized. */
11644 static rtx
11645 gen_lowpart_for_combine (machine_mode omode, rtx x)
11647 machine_mode imode = GET_MODE (x);
11648 rtx result;
11650 if (omode == imode)
11651 return x;
11653 /* We can only support MODE being wider than a word if X is a
11654 constant integer or has a mode the same size. */
11655 if (maybe_gt (GET_MODE_SIZE (omode), UNITS_PER_WORD)
11656 && ! (CONST_SCALAR_INT_P (x)
11657 || known_eq (GET_MODE_SIZE (imode), GET_MODE_SIZE (omode))))
11658 goto fail;
11660 /* X might be a paradoxical (subreg (mem)). In that case, gen_lowpart
11661 won't know what to do. So we will strip off the SUBREG here and
11662 process normally. */
11663 if (GET_CODE (x) == SUBREG && MEM_P (SUBREG_REG (x)))
11665 x = SUBREG_REG (x);
11667 /* For use in case we fall down into the address adjustments
11668 further below, we need to adjust the known mode and size of
11669 x; imode and isize, since we just adjusted x. */
11670 imode = GET_MODE (x);
11672 if (imode == omode)
11673 return x;
11676 result = gen_lowpart_common (omode, x);
11678 if (result)
11679 return result;
11681 if (MEM_P (x))
11683 /* Refuse to work on a volatile memory ref or one with a mode-dependent
11684 address. */
11685 if (MEM_VOLATILE_P (x)
11686 || mode_dependent_address_p (XEXP (x, 0), MEM_ADDR_SPACE (x)))
11687 goto fail;
11689 /* If we want to refer to something bigger than the original memref,
11690 generate a paradoxical subreg instead. That will force a reload
11691 of the original memref X. */
11692 if (paradoxical_subreg_p (omode, imode))
11693 return gen_rtx_SUBREG (omode, x, 0);
11695 poly_int64 offset = byte_lowpart_offset (omode, imode);
11696 return adjust_address_nv (x, omode, offset);
11699 /* If X is a comparison operator, rewrite it in a new mode. This
11700 probably won't match, but may allow further simplifications. */
11701 else if (COMPARISON_P (x))
11702 return gen_rtx_fmt_ee (GET_CODE (x), omode, XEXP (x, 0), XEXP (x, 1));
11704 /* If we couldn't simplify X any other way, just enclose it in a
11705 SUBREG. Normally, this SUBREG won't match, but some patterns may
11706 include an explicit SUBREG or we may simplify it further in combine. */
11707 else
11709 rtx res;
11711 if (imode == VOIDmode)
11713 imode = int_mode_for_mode (omode).require ();
11714 x = gen_lowpart_common (imode, x);
11715 if (x == NULL)
11716 goto fail;
11718 res = lowpart_subreg (omode, x, imode);
11719 if (res)
11720 return res;
11723 fail:
11724 return gen_rtx_CLOBBER (omode, const0_rtx);
11727 /* Try to simplify a comparison between OP0 and a constant OP1,
11728 where CODE is the comparison code that will be tested, into a
11729 (CODE OP0 const0_rtx) form.
11731 The result is a possibly different comparison code to use.
11732 *POP1 may be updated. */
11734 static enum rtx_code
11735 simplify_compare_const (enum rtx_code code, machine_mode mode,
11736 rtx op0, rtx *pop1)
11738 scalar_int_mode int_mode;
11739 HOST_WIDE_INT const_op = INTVAL (*pop1);
11741 /* Get the constant we are comparing against and turn off all bits
11742 not on in our mode. */
11743 if (mode != VOIDmode)
11744 const_op = trunc_int_for_mode (const_op, mode);
11746 /* If we are comparing against a constant power of two and the value
11747 being compared can only have that single bit nonzero (e.g., it was
11748 `and'ed with that bit), we can replace this with a comparison
11749 with zero. */
11750 if (const_op
11751 && (code == EQ || code == NE || code == GE || code == GEU
11752 || code == LT || code == LTU)
11753 && is_a <scalar_int_mode> (mode, &int_mode)
11754 && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11755 && pow2p_hwi (const_op & GET_MODE_MASK (int_mode))
11756 && (nonzero_bits (op0, int_mode)
11757 == (unsigned HOST_WIDE_INT) (const_op & GET_MODE_MASK (int_mode))))
11759 code = (code == EQ || code == GE || code == GEU ? NE : EQ);
11760 const_op = 0;
11763 /* Similarly, if we are comparing a value known to be either -1 or
11764 0 with -1, change it to the opposite comparison against zero. */
11765 if (const_op == -1
11766 && (code == EQ || code == NE || code == GT || code == LE
11767 || code == GEU || code == LTU)
11768 && is_a <scalar_int_mode> (mode, &int_mode)
11769 && num_sign_bit_copies (op0, int_mode) == GET_MODE_PRECISION (int_mode))
11771 code = (code == EQ || code == LE || code == GEU ? NE : EQ);
11772 const_op = 0;
11775 /* Do some canonicalizations based on the comparison code. We prefer
11776 comparisons against zero and then prefer equality comparisons.
11777 If we can reduce the size of a constant, we will do that too. */
11778 switch (code)
11780 case LT:
11781 /* < C is equivalent to <= (C - 1) */
11782 if (const_op > 0)
11784 const_op -= 1;
11785 code = LE;
11786 /* ... fall through to LE case below. */
11787 gcc_fallthrough ();
11789 else
11790 break;
11792 case LE:
11793 /* <= C is equivalent to < (C + 1); we do this for C < 0 */
11794 if (const_op < 0)
11796 const_op += 1;
11797 code = LT;
11800 /* If we are doing a <= 0 comparison on a value known to have
11801 a zero sign bit, we can replace this with == 0. */
11802 else if (const_op == 0
11803 && is_a <scalar_int_mode> (mode, &int_mode)
11804 && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11805 && (nonzero_bits (op0, int_mode)
11806 & (HOST_WIDE_INT_1U << (GET_MODE_PRECISION (int_mode) - 1)))
11807 == 0)
11808 code = EQ;
11809 break;
11811 case GE:
11812 /* >= C is equivalent to > (C - 1). */
11813 if (const_op > 0)
11815 const_op -= 1;
11816 code = GT;
11817 /* ... fall through to GT below. */
11818 gcc_fallthrough ();
11820 else
11821 break;
11823 case GT:
11824 /* > C is equivalent to >= (C + 1); we do this for C < 0. */
11825 if (const_op < 0)
11827 const_op += 1;
11828 code = GE;
11831 /* If we are doing a > 0 comparison on a value known to have
11832 a zero sign bit, we can replace this with != 0. */
11833 else if (const_op == 0
11834 && is_a <scalar_int_mode> (mode, &int_mode)
11835 && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11836 && (nonzero_bits (op0, int_mode)
11837 & (HOST_WIDE_INT_1U << (GET_MODE_PRECISION (int_mode) - 1)))
11838 == 0)
11839 code = NE;
11840 break;
11842 case LTU:
11843 /* < C is equivalent to <= (C - 1). */
11844 if (const_op > 0)
11846 const_op -= 1;
11847 code = LEU;
11848 /* ... fall through ... */
11849 gcc_fallthrough ();
11851 /* (unsigned) < 0x80000000 is equivalent to >= 0. */
11852 else if (is_a <scalar_int_mode> (mode, &int_mode)
11853 && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11854 && ((unsigned HOST_WIDE_INT) const_op
11855 == HOST_WIDE_INT_1U << (GET_MODE_PRECISION (int_mode) - 1)))
11857 const_op = 0;
11858 code = GE;
11859 break;
11861 else
11862 break;
11864 case LEU:
11865 /* unsigned <= 0 is equivalent to == 0 */
11866 if (const_op == 0)
11867 code = EQ;
11868 /* (unsigned) <= 0x7fffffff is equivalent to >= 0. */
11869 else if (is_a <scalar_int_mode> (mode, &int_mode)
11870 && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11871 && ((unsigned HOST_WIDE_INT) const_op
11872 == ((HOST_WIDE_INT_1U
11873 << (GET_MODE_PRECISION (int_mode) - 1)) - 1)))
11875 const_op = 0;
11876 code = GE;
11878 break;
11880 case GEU:
11881 /* >= C is equivalent to > (C - 1). */
11882 if (const_op > 1)
11884 const_op -= 1;
11885 code = GTU;
11886 /* ... fall through ... */
11887 gcc_fallthrough ();
11890 /* (unsigned) >= 0x80000000 is equivalent to < 0. */
11891 else if (is_a <scalar_int_mode> (mode, &int_mode)
11892 && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11893 && ((unsigned HOST_WIDE_INT) const_op
11894 == HOST_WIDE_INT_1U << (GET_MODE_PRECISION (int_mode) - 1)))
11896 const_op = 0;
11897 code = LT;
11898 break;
11900 else
11901 break;
11903 case GTU:
11904 /* unsigned > 0 is equivalent to != 0 */
11905 if (const_op == 0)
11906 code = NE;
11907 /* (unsigned) > 0x7fffffff is equivalent to < 0. */
11908 else if (is_a <scalar_int_mode> (mode, &int_mode)
11909 && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT
11910 && ((unsigned HOST_WIDE_INT) const_op
11911 == (HOST_WIDE_INT_1U
11912 << (GET_MODE_PRECISION (int_mode) - 1)) - 1))
11914 const_op = 0;
11915 code = LT;
11917 break;
11919 default:
11920 break;
11923 *pop1 = GEN_INT (const_op);
11924 return code;
11927 /* Simplify a comparison between *POP0 and *POP1 where CODE is the
11928 comparison code that will be tested.
11930 The result is a possibly different comparison code to use. *POP0 and
11931 *POP1 may be updated.
11933 It is possible that we might detect that a comparison is either always
11934 true or always false. However, we do not perform general constant
11935 folding in combine, so this knowledge isn't useful. Such tautologies
11936 should have been detected earlier. Hence we ignore all such cases. */
11938 static enum rtx_code
11939 simplify_comparison (enum rtx_code code, rtx *pop0, rtx *pop1)
11941 rtx op0 = *pop0;
11942 rtx op1 = *pop1;
11943 rtx tem, tem1;
11944 int i;
11945 scalar_int_mode mode, inner_mode, tmode;
11946 opt_scalar_int_mode tmode_iter;
11948 /* Try a few ways of applying the same transformation to both operands. */
11949 while (1)
11951 /* The test below this one won't handle SIGN_EXTENDs on these machines,
11952 so check specially. */
11953 if (!WORD_REGISTER_OPERATIONS
11954 && code != GTU && code != GEU && code != LTU && code != LEU
11955 && GET_CODE (op0) == ASHIFTRT && GET_CODE (op1) == ASHIFTRT
11956 && GET_CODE (XEXP (op0, 0)) == ASHIFT
11957 && GET_CODE (XEXP (op1, 0)) == ASHIFT
11958 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == SUBREG
11959 && GET_CODE (XEXP (XEXP (op1, 0), 0)) == SUBREG
11960 && is_a <scalar_int_mode> (GET_MODE (op0), &mode)
11961 && (is_a <scalar_int_mode>
11962 (GET_MODE (SUBREG_REG (XEXP (XEXP (op0, 0), 0))), &inner_mode))
11963 && inner_mode == GET_MODE (SUBREG_REG (XEXP (XEXP (op1, 0), 0)))
11964 && CONST_INT_P (XEXP (op0, 1))
11965 && XEXP (op0, 1) == XEXP (op1, 1)
11966 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
11967 && XEXP (op0, 1) == XEXP (XEXP (op1, 0), 1)
11968 && (INTVAL (XEXP (op0, 1))
11969 == (GET_MODE_PRECISION (mode)
11970 - GET_MODE_PRECISION (inner_mode))))
11972 op0 = SUBREG_REG (XEXP (XEXP (op0, 0), 0));
11973 op1 = SUBREG_REG (XEXP (XEXP (op1, 0), 0));
11976 /* If both operands are the same constant shift, see if we can ignore the
11977 shift. We can if the shift is a rotate or if the bits shifted out of
11978 this shift are known to be zero for both inputs and if the type of
11979 comparison is compatible with the shift. */
11980 if (GET_CODE (op0) == GET_CODE (op1)
11981 && HWI_COMPUTABLE_MODE_P (GET_MODE (op0))
11982 && ((GET_CODE (op0) == ROTATE && (code == NE || code == EQ))
11983 || ((GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFT)
11984 && (code != GT && code != LT && code != GE && code != LE))
11985 || (GET_CODE (op0) == ASHIFTRT
11986 && (code != GTU && code != LTU
11987 && code != GEU && code != LEU)))
11988 && CONST_INT_P (XEXP (op0, 1))
11989 && INTVAL (XEXP (op0, 1)) >= 0
11990 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
11991 && XEXP (op0, 1) == XEXP (op1, 1))
11993 machine_mode mode = GET_MODE (op0);
11994 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
11995 int shift_count = INTVAL (XEXP (op0, 1));
11997 if (GET_CODE (op0) == LSHIFTRT || GET_CODE (op0) == ASHIFTRT)
11998 mask &= (mask >> shift_count) << shift_count;
11999 else if (GET_CODE (op0) == ASHIFT)
12000 mask = (mask & (mask << shift_count)) >> shift_count;
12002 if ((nonzero_bits (XEXP (op0, 0), mode) & ~mask) == 0
12003 && (nonzero_bits (XEXP (op1, 0), mode) & ~mask) == 0)
12004 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0);
12005 else
12006 break;
12009 /* If both operands are AND's of a paradoxical SUBREG by constant, the
12010 SUBREGs are of the same mode, and, in both cases, the AND would
12011 be redundant if the comparison was done in the narrower mode,
12012 do the comparison in the narrower mode (e.g., we are AND'ing with 1
12013 and the operand's possibly nonzero bits are 0xffffff01; in that case
12014 if we only care about QImode, we don't need the AND). This case
12015 occurs if the output mode of an scc insn is not SImode and
12016 STORE_FLAG_VALUE == 1 (e.g., the 386).
12018 Similarly, check for a case where the AND's are ZERO_EXTEND
12019 operations from some narrower mode even though a SUBREG is not
12020 present. */
12022 else if (GET_CODE (op0) == AND && GET_CODE (op1) == AND
12023 && CONST_INT_P (XEXP (op0, 1))
12024 && CONST_INT_P (XEXP (op1, 1)))
12026 rtx inner_op0 = XEXP (op0, 0);
12027 rtx inner_op1 = XEXP (op1, 0);
12028 HOST_WIDE_INT c0 = INTVAL (XEXP (op0, 1));
12029 HOST_WIDE_INT c1 = INTVAL (XEXP (op1, 1));
12030 int changed = 0;
12032 if (paradoxical_subreg_p (inner_op0)
12033 && GET_CODE (inner_op1) == SUBREG
12034 && HWI_COMPUTABLE_MODE_P (GET_MODE (SUBREG_REG (inner_op0)))
12035 && (GET_MODE (SUBREG_REG (inner_op0))
12036 == GET_MODE (SUBREG_REG (inner_op1)))
12037 && ((~c0) & nonzero_bits (SUBREG_REG (inner_op0),
12038 GET_MODE (SUBREG_REG (inner_op0)))) == 0
12039 && ((~c1) & nonzero_bits (SUBREG_REG (inner_op1),
12040 GET_MODE (SUBREG_REG (inner_op1)))) == 0)
12042 op0 = SUBREG_REG (inner_op0);
12043 op1 = SUBREG_REG (inner_op1);
12045 /* The resulting comparison is always unsigned since we masked
12046 off the original sign bit. */
12047 code = unsigned_condition (code);
12049 changed = 1;
12052 else if (c0 == c1)
12053 FOR_EACH_MODE_UNTIL (tmode,
12054 as_a <scalar_int_mode> (GET_MODE (op0)))
12055 if ((unsigned HOST_WIDE_INT) c0 == GET_MODE_MASK (tmode))
12057 op0 = gen_lowpart_or_truncate (tmode, inner_op0);
12058 op1 = gen_lowpart_or_truncate (tmode, inner_op1);
12059 code = unsigned_condition (code);
12060 changed = 1;
12061 break;
12064 if (! changed)
12065 break;
12068 /* If both operands are NOT, we can strip off the outer operation
12069 and adjust the comparison code for swapped operands; similarly for
12070 NEG, except that this must be an equality comparison. */
12071 else if ((GET_CODE (op0) == NOT && GET_CODE (op1) == NOT)
12072 || (GET_CODE (op0) == NEG && GET_CODE (op1) == NEG
12073 && (code == EQ || code == NE)))
12074 op0 = XEXP (op0, 0), op1 = XEXP (op1, 0), code = swap_condition (code);
12076 else
12077 break;
12080 /* If the first operand is a constant, swap the operands and adjust the
12081 comparison code appropriately, but don't do this if the second operand
12082 is already a constant integer. */
12083 if (swap_commutative_operands_p (op0, op1))
12085 std::swap (op0, op1);
12086 code = swap_condition (code);
12089 /* We now enter a loop during which we will try to simplify the comparison.
12090 For the most part, we only are concerned with comparisons with zero,
12091 but some things may really be comparisons with zero but not start
12092 out looking that way. */
12094 while (CONST_INT_P (op1))
12096 machine_mode raw_mode = GET_MODE (op0);
12097 scalar_int_mode int_mode;
12098 int equality_comparison_p;
12099 int sign_bit_comparison_p;
12100 int unsigned_comparison_p;
12101 HOST_WIDE_INT const_op;
12103 /* We only want to handle integral modes. This catches VOIDmode,
12104 CCmode, and the floating-point modes. An exception is that we
12105 can handle VOIDmode if OP0 is a COMPARE or a comparison
12106 operation. */
12108 if (GET_MODE_CLASS (raw_mode) != MODE_INT
12109 && ! (raw_mode == VOIDmode
12110 && (GET_CODE (op0) == COMPARE || COMPARISON_P (op0))))
12111 break;
12113 /* Try to simplify the compare to constant, possibly changing the
12114 comparison op, and/or changing op1 to zero. */
12115 code = simplify_compare_const (code, raw_mode, op0, &op1);
12116 const_op = INTVAL (op1);
12118 /* Compute some predicates to simplify code below. */
12120 equality_comparison_p = (code == EQ || code == NE);
12121 sign_bit_comparison_p = ((code == LT || code == GE) && const_op == 0);
12122 unsigned_comparison_p = (code == LTU || code == LEU || code == GTU
12123 || code == GEU);
12125 /* If this is a sign bit comparison and we can do arithmetic in
12126 MODE, say that we will only be needing the sign bit of OP0. */
12127 if (sign_bit_comparison_p
12128 && is_a <scalar_int_mode> (raw_mode, &int_mode)
12129 && HWI_COMPUTABLE_MODE_P (int_mode))
12130 op0 = force_to_mode (op0, int_mode,
12131 HOST_WIDE_INT_1U
12132 << (GET_MODE_PRECISION (int_mode) - 1),
12135 if (COMPARISON_P (op0))
12137 /* We can't do anything if OP0 is a condition code value, rather
12138 than an actual data value. */
12139 if (const_op != 0
12140 || CC0_P (XEXP (op0, 0))
12141 || GET_MODE_CLASS (GET_MODE (XEXP (op0, 0))) == MODE_CC)
12142 break;
12144 /* Get the two operands being compared. */
12145 if (GET_CODE (XEXP (op0, 0)) == COMPARE)
12146 tem = XEXP (XEXP (op0, 0), 0), tem1 = XEXP (XEXP (op0, 0), 1);
12147 else
12148 tem = XEXP (op0, 0), tem1 = XEXP (op0, 1);
12150 /* Check for the cases where we simply want the result of the
12151 earlier test or the opposite of that result. */
12152 if (code == NE || code == EQ
12153 || (val_signbit_known_set_p (raw_mode, STORE_FLAG_VALUE)
12154 && (code == LT || code == GE)))
12156 enum rtx_code new_code;
12157 if (code == LT || code == NE)
12158 new_code = GET_CODE (op0);
12159 else
12160 new_code = reversed_comparison_code (op0, NULL);
12162 if (new_code != UNKNOWN)
12164 code = new_code;
12165 op0 = tem;
12166 op1 = tem1;
12167 continue;
12170 break;
12173 if (raw_mode == VOIDmode)
12174 break;
12175 scalar_int_mode mode = as_a <scalar_int_mode> (raw_mode);
12177 /* Now try cases based on the opcode of OP0. If none of the cases
12178 does a "continue", we exit this loop immediately after the
12179 switch. */
12181 unsigned int mode_width = GET_MODE_PRECISION (mode);
12182 unsigned HOST_WIDE_INT mask = GET_MODE_MASK (mode);
12183 switch (GET_CODE (op0))
12185 case ZERO_EXTRACT:
12186 /* If we are extracting a single bit from a variable position in
12187 a constant that has only a single bit set and are comparing it
12188 with zero, we can convert this into an equality comparison
12189 between the position and the location of the single bit. */
12190 /* Except we can't if SHIFT_COUNT_TRUNCATED is set, since we might
12191 have already reduced the shift count modulo the word size. */
12192 if (!SHIFT_COUNT_TRUNCATED
12193 && CONST_INT_P (XEXP (op0, 0))
12194 && XEXP (op0, 1) == const1_rtx
12195 && equality_comparison_p && const_op == 0
12196 && (i = exact_log2 (UINTVAL (XEXP (op0, 0)))) >= 0)
12198 if (BITS_BIG_ENDIAN)
12199 i = BITS_PER_WORD - 1 - i;
12201 op0 = XEXP (op0, 2);
12202 op1 = GEN_INT (i);
12203 const_op = i;
12205 /* Result is nonzero iff shift count is equal to I. */
12206 code = reverse_condition (code);
12207 continue;
12210 /* fall through */
12212 case SIGN_EXTRACT:
12213 tem = expand_compound_operation (op0);
12214 if (tem != op0)
12216 op0 = tem;
12217 continue;
12219 break;
12221 case NOT:
12222 /* If testing for equality, we can take the NOT of the constant. */
12223 if (equality_comparison_p
12224 && (tem = simplify_unary_operation (NOT, mode, op1, mode)) != 0)
12226 op0 = XEXP (op0, 0);
12227 op1 = tem;
12228 continue;
12231 /* If just looking at the sign bit, reverse the sense of the
12232 comparison. */
12233 if (sign_bit_comparison_p)
12235 op0 = XEXP (op0, 0);
12236 code = (code == GE ? LT : GE);
12237 continue;
12239 break;
12241 case NEG:
12242 /* If testing for equality, we can take the NEG of the constant. */
12243 if (equality_comparison_p
12244 && (tem = simplify_unary_operation (NEG, mode, op1, mode)) != 0)
12246 op0 = XEXP (op0, 0);
12247 op1 = tem;
12248 continue;
12251 /* The remaining cases only apply to comparisons with zero. */
12252 if (const_op != 0)
12253 break;
12255 /* When X is ABS or is known positive,
12256 (neg X) is < 0 if and only if X != 0. */
12258 if (sign_bit_comparison_p
12259 && (GET_CODE (XEXP (op0, 0)) == ABS
12260 || (mode_width <= HOST_BITS_PER_WIDE_INT
12261 && (nonzero_bits (XEXP (op0, 0), mode)
12262 & (HOST_WIDE_INT_1U << (mode_width - 1)))
12263 == 0)))
12265 op0 = XEXP (op0, 0);
12266 code = (code == LT ? NE : EQ);
12267 continue;
12270 /* If we have NEG of something whose two high-order bits are the
12271 same, we know that "(-a) < 0" is equivalent to "a > 0". */
12272 if (num_sign_bit_copies (op0, mode) >= 2)
12274 op0 = XEXP (op0, 0);
12275 code = swap_condition (code);
12276 continue;
12278 break;
12280 case ROTATE:
12281 /* If we are testing equality and our count is a constant, we
12282 can perform the inverse operation on our RHS. */
12283 if (equality_comparison_p && CONST_INT_P (XEXP (op0, 1))
12284 && (tem = simplify_binary_operation (ROTATERT, mode,
12285 op1, XEXP (op0, 1))) != 0)
12287 op0 = XEXP (op0, 0);
12288 op1 = tem;
12289 continue;
12292 /* If we are doing a < 0 or >= 0 comparison, it means we are testing
12293 a particular bit. Convert it to an AND of a constant of that
12294 bit. This will be converted into a ZERO_EXTRACT. */
12295 if (const_op == 0 && sign_bit_comparison_p
12296 && CONST_INT_P (XEXP (op0, 1))
12297 && mode_width <= HOST_BITS_PER_WIDE_INT)
12299 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
12300 (HOST_WIDE_INT_1U
12301 << (mode_width - 1
12302 - INTVAL (XEXP (op0, 1)))));
12303 code = (code == LT ? NE : EQ);
12304 continue;
12307 /* Fall through. */
12309 case ABS:
12310 /* ABS is ignorable inside an equality comparison with zero. */
12311 if (const_op == 0 && equality_comparison_p)
12313 op0 = XEXP (op0, 0);
12314 continue;
12316 break;
12318 case SIGN_EXTEND:
12319 /* Can simplify (compare (zero/sign_extend FOO) CONST) to
12320 (compare FOO CONST) if CONST fits in FOO's mode and we
12321 are either testing inequality or have an unsigned
12322 comparison with ZERO_EXTEND or a signed comparison with
12323 SIGN_EXTEND. But don't do it if we don't have a compare
12324 insn of the given mode, since we'd have to revert it
12325 later on, and then we wouldn't know whether to sign- or
12326 zero-extend. */
12327 if (is_int_mode (GET_MODE (XEXP (op0, 0)), &mode)
12328 && ! unsigned_comparison_p
12329 && HWI_COMPUTABLE_MODE_P (mode)
12330 && trunc_int_for_mode (const_op, mode) == const_op
12331 && have_insn_for (COMPARE, mode))
12333 op0 = XEXP (op0, 0);
12334 continue;
12336 break;
12338 case SUBREG:
12339 /* Check for the case where we are comparing A - C1 with C2, that is
12341 (subreg:MODE (plus (A) (-C1))) op (C2)
12343 with C1 a constant, and try to lift the SUBREG, i.e. to do the
12344 comparison in the wider mode. One of the following two conditions
12345 must be true in order for this to be valid:
12347 1. The mode extension results in the same bit pattern being added
12348 on both sides and the comparison is equality or unsigned. As
12349 C2 has been truncated to fit in MODE, the pattern can only be
12350 all 0s or all 1s.
12352 2. The mode extension results in the sign bit being copied on
12353 each side.
12355 The difficulty here is that we have predicates for A but not for
12356 (A - C1) so we need to check that C1 is within proper bounds so
12357 as to perturbate A as little as possible. */
12359 if (mode_width <= HOST_BITS_PER_WIDE_INT
12360 && subreg_lowpart_p (op0)
12361 && is_a <scalar_int_mode> (GET_MODE (SUBREG_REG (op0)),
12362 &inner_mode)
12363 && GET_MODE_PRECISION (inner_mode) > mode_width
12364 && GET_CODE (SUBREG_REG (op0)) == PLUS
12365 && CONST_INT_P (XEXP (SUBREG_REG (op0), 1)))
12367 rtx a = XEXP (SUBREG_REG (op0), 0);
12368 HOST_WIDE_INT c1 = -INTVAL (XEXP (SUBREG_REG (op0), 1));
12370 if ((c1 > 0
12371 && (unsigned HOST_WIDE_INT) c1
12372 < HOST_WIDE_INT_1U << (mode_width - 1)
12373 && (equality_comparison_p || unsigned_comparison_p)
12374 /* (A - C1) zero-extends if it is positive and sign-extends
12375 if it is negative, C2 both zero- and sign-extends. */
12376 && (((nonzero_bits (a, inner_mode)
12377 & ~GET_MODE_MASK (mode)) == 0
12378 && const_op >= 0)
12379 /* (A - C1) sign-extends if it is positive and 1-extends
12380 if it is negative, C2 both sign- and 1-extends. */
12381 || (num_sign_bit_copies (a, inner_mode)
12382 > (unsigned int) (GET_MODE_PRECISION (inner_mode)
12383 - mode_width)
12384 && const_op < 0)))
12385 || ((unsigned HOST_WIDE_INT) c1
12386 < HOST_WIDE_INT_1U << (mode_width - 2)
12387 /* (A - C1) always sign-extends, like C2. */
12388 && num_sign_bit_copies (a, inner_mode)
12389 > (unsigned int) (GET_MODE_PRECISION (inner_mode)
12390 - (mode_width - 1))))
12392 op0 = SUBREG_REG (op0);
12393 continue;
12397 /* If the inner mode is narrower and we are extracting the low part,
12398 we can treat the SUBREG as if it were a ZERO_EXTEND. */
12399 if (paradoxical_subreg_p (op0))
12401 else if (subreg_lowpart_p (op0)
12402 && GET_MODE_CLASS (mode) == MODE_INT
12403 && is_int_mode (GET_MODE (SUBREG_REG (op0)), &inner_mode)
12404 && (code == NE || code == EQ)
12405 && GET_MODE_PRECISION (inner_mode) <= HOST_BITS_PER_WIDE_INT
12406 && !paradoxical_subreg_p (op0)
12407 && (nonzero_bits (SUBREG_REG (op0), inner_mode)
12408 & ~GET_MODE_MASK (mode)) == 0)
12410 /* Remove outer subregs that don't do anything. */
12411 tem = gen_lowpart (inner_mode, op1);
12413 if ((nonzero_bits (tem, inner_mode)
12414 & ~GET_MODE_MASK (mode)) == 0)
12416 op0 = SUBREG_REG (op0);
12417 op1 = tem;
12418 continue;
12420 break;
12422 else
12423 break;
12425 /* FALLTHROUGH */
12427 case ZERO_EXTEND:
12428 if (is_int_mode (GET_MODE (XEXP (op0, 0)), &mode)
12429 && (unsigned_comparison_p || equality_comparison_p)
12430 && HWI_COMPUTABLE_MODE_P (mode)
12431 && (unsigned HOST_WIDE_INT) const_op <= GET_MODE_MASK (mode)
12432 && const_op >= 0
12433 && have_insn_for (COMPARE, mode))
12435 op0 = XEXP (op0, 0);
12436 continue;
12438 break;
12440 case PLUS:
12441 /* (eq (plus X A) B) -> (eq X (minus B A)). We can only do
12442 this for equality comparisons due to pathological cases involving
12443 overflows. */
12444 if (equality_comparison_p
12445 && (tem = simplify_binary_operation (MINUS, mode,
12446 op1, XEXP (op0, 1))) != 0)
12448 op0 = XEXP (op0, 0);
12449 op1 = tem;
12450 continue;
12453 /* (plus (abs X) (const_int -1)) is < 0 if and only if X == 0. */
12454 if (const_op == 0 && XEXP (op0, 1) == constm1_rtx
12455 && GET_CODE (XEXP (op0, 0)) == ABS && sign_bit_comparison_p)
12457 op0 = XEXP (XEXP (op0, 0), 0);
12458 code = (code == LT ? EQ : NE);
12459 continue;
12461 break;
12463 case MINUS:
12464 /* We used to optimize signed comparisons against zero, but that
12465 was incorrect. Unsigned comparisons against zero (GTU, LEU)
12466 arrive here as equality comparisons, or (GEU, LTU) are
12467 optimized away. No need to special-case them. */
12469 /* (eq (minus A B) C) -> (eq A (plus B C)) or
12470 (eq B (minus A C)), whichever simplifies. We can only do
12471 this for equality comparisons due to pathological cases involving
12472 overflows. */
12473 if (equality_comparison_p
12474 && (tem = simplify_binary_operation (PLUS, mode,
12475 XEXP (op0, 1), op1)) != 0)
12477 op0 = XEXP (op0, 0);
12478 op1 = tem;
12479 continue;
12482 if (equality_comparison_p
12483 && (tem = simplify_binary_operation (MINUS, mode,
12484 XEXP (op0, 0), op1)) != 0)
12486 op0 = XEXP (op0, 1);
12487 op1 = tem;
12488 continue;
12491 /* The sign bit of (minus (ashiftrt X C) X), where C is the number
12492 of bits in X minus 1, is one iff X > 0. */
12493 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == ASHIFTRT
12494 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
12495 && UINTVAL (XEXP (XEXP (op0, 0), 1)) == mode_width - 1
12496 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
12498 op0 = XEXP (op0, 1);
12499 code = (code == GE ? LE : GT);
12500 continue;
12502 break;
12504 case XOR:
12505 /* (eq (xor A B) C) -> (eq A (xor B C)). This is a simplification
12506 if C is zero or B is a constant. */
12507 if (equality_comparison_p
12508 && (tem = simplify_binary_operation (XOR, mode,
12509 XEXP (op0, 1), op1)) != 0)
12511 op0 = XEXP (op0, 0);
12512 op1 = tem;
12513 continue;
12515 break;
12518 case IOR:
12519 /* The sign bit of (ior (plus X (const_int -1)) X) is nonzero
12520 iff X <= 0. */
12521 if (sign_bit_comparison_p && GET_CODE (XEXP (op0, 0)) == PLUS
12522 && XEXP (XEXP (op0, 0), 1) == constm1_rtx
12523 && rtx_equal_p (XEXP (XEXP (op0, 0), 0), XEXP (op0, 1)))
12525 op0 = XEXP (op0, 1);
12526 code = (code == GE ? GT : LE);
12527 continue;
12529 break;
12531 case AND:
12532 /* Convert (and (xshift 1 X) Y) to (and (lshiftrt Y X) 1). This
12533 will be converted to a ZERO_EXTRACT later. */
12534 if (const_op == 0 && equality_comparison_p
12535 && GET_CODE (XEXP (op0, 0)) == ASHIFT
12536 && XEXP (XEXP (op0, 0), 0) == const1_rtx)
12538 op0 = gen_rtx_LSHIFTRT (mode, XEXP (op0, 1),
12539 XEXP (XEXP (op0, 0), 1));
12540 op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
12541 continue;
12544 /* If we are comparing (and (lshiftrt X C1) C2) for equality with
12545 zero and X is a comparison and C1 and C2 describe only bits set
12546 in STORE_FLAG_VALUE, we can compare with X. */
12547 if (const_op == 0 && equality_comparison_p
12548 && mode_width <= HOST_BITS_PER_WIDE_INT
12549 && CONST_INT_P (XEXP (op0, 1))
12550 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT
12551 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
12552 && INTVAL (XEXP (XEXP (op0, 0), 1)) >= 0
12553 && INTVAL (XEXP (XEXP (op0, 0), 1)) < HOST_BITS_PER_WIDE_INT)
12555 mask = ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
12556 << INTVAL (XEXP (XEXP (op0, 0), 1)));
12557 if ((~STORE_FLAG_VALUE & mask) == 0
12558 && (COMPARISON_P (XEXP (XEXP (op0, 0), 0))
12559 || ((tem = get_last_value (XEXP (XEXP (op0, 0), 0))) != 0
12560 && COMPARISON_P (tem))))
12562 op0 = XEXP (XEXP (op0, 0), 0);
12563 continue;
12567 /* If we are doing an equality comparison of an AND of a bit equal
12568 to the sign bit, replace this with a LT or GE comparison of
12569 the underlying value. */
12570 if (equality_comparison_p
12571 && const_op == 0
12572 && CONST_INT_P (XEXP (op0, 1))
12573 && mode_width <= HOST_BITS_PER_WIDE_INT
12574 && ((INTVAL (XEXP (op0, 1)) & GET_MODE_MASK (mode))
12575 == HOST_WIDE_INT_1U << (mode_width - 1)))
12577 op0 = XEXP (op0, 0);
12578 code = (code == EQ ? GE : LT);
12579 continue;
12582 /* If this AND operation is really a ZERO_EXTEND from a narrower
12583 mode, the constant fits within that mode, and this is either an
12584 equality or unsigned comparison, try to do this comparison in
12585 the narrower mode.
12587 Note that in:
12589 (ne:DI (and:DI (reg:DI 4) (const_int 0xffffffff)) (const_int 0))
12590 -> (ne:DI (reg:SI 4) (const_int 0))
12592 unless TARGET_TRULY_NOOP_TRUNCATION allows it or the register is
12593 known to hold a value of the required mode the
12594 transformation is invalid. */
12595 if ((equality_comparison_p || unsigned_comparison_p)
12596 && CONST_INT_P (XEXP (op0, 1))
12597 && (i = exact_log2 ((UINTVAL (XEXP (op0, 1))
12598 & GET_MODE_MASK (mode))
12599 + 1)) >= 0
12600 && const_op >> i == 0
12601 && int_mode_for_size (i, 1).exists (&tmode))
12603 op0 = gen_lowpart_or_truncate (tmode, XEXP (op0, 0));
12604 continue;
12607 /* If this is (and:M1 (subreg:M1 X:M2 0) (const_int C1)) where C1
12608 fits in both M1 and M2 and the SUBREG is either paradoxical
12609 or represents the low part, permute the SUBREG and the AND
12610 and try again. */
12611 if (GET_CODE (XEXP (op0, 0)) == SUBREG
12612 && CONST_INT_P (XEXP (op0, 1)))
12614 unsigned HOST_WIDE_INT c1 = INTVAL (XEXP (op0, 1));
12615 /* Require an integral mode, to avoid creating something like
12616 (AND:SF ...). */
12617 if ((is_a <scalar_int_mode>
12618 (GET_MODE (SUBREG_REG (XEXP (op0, 0))), &tmode))
12619 /* It is unsafe to commute the AND into the SUBREG if the
12620 SUBREG is paradoxical and WORD_REGISTER_OPERATIONS is
12621 not defined. As originally written the upper bits
12622 have a defined value due to the AND operation.
12623 However, if we commute the AND inside the SUBREG then
12624 they no longer have defined values and the meaning of
12625 the code has been changed.
12626 Also C1 should not change value in the smaller mode,
12627 see PR67028 (a positive C1 can become negative in the
12628 smaller mode, so that the AND does no longer mask the
12629 upper bits). */
12630 && ((WORD_REGISTER_OPERATIONS
12631 && mode_width > GET_MODE_PRECISION (tmode)
12632 && mode_width <= BITS_PER_WORD
12633 && trunc_int_for_mode (c1, tmode) == (HOST_WIDE_INT) c1)
12634 || (mode_width <= GET_MODE_PRECISION (tmode)
12635 && subreg_lowpart_p (XEXP (op0, 0))))
12636 && mode_width <= HOST_BITS_PER_WIDE_INT
12637 && HWI_COMPUTABLE_MODE_P (tmode)
12638 && (c1 & ~mask) == 0
12639 && (c1 & ~GET_MODE_MASK (tmode)) == 0
12640 && c1 != mask
12641 && c1 != GET_MODE_MASK (tmode))
12643 op0 = simplify_gen_binary (AND, tmode,
12644 SUBREG_REG (XEXP (op0, 0)),
12645 gen_int_mode (c1, tmode));
12646 op0 = gen_lowpart (mode, op0);
12647 continue;
12651 /* Convert (ne (and (not X) 1) 0) to (eq (and X 1) 0). */
12652 if (const_op == 0 && equality_comparison_p
12653 && XEXP (op0, 1) == const1_rtx
12654 && GET_CODE (XEXP (op0, 0)) == NOT)
12656 op0 = simplify_and_const_int (NULL_RTX, mode,
12657 XEXP (XEXP (op0, 0), 0), 1);
12658 code = (code == NE ? EQ : NE);
12659 continue;
12662 /* Convert (ne (and (lshiftrt (not X)) 1) 0) to
12663 (eq (and (lshiftrt X) 1) 0).
12664 Also handle the case where (not X) is expressed using xor. */
12665 if (const_op == 0 && equality_comparison_p
12666 && XEXP (op0, 1) == const1_rtx
12667 && GET_CODE (XEXP (op0, 0)) == LSHIFTRT)
12669 rtx shift_op = XEXP (XEXP (op0, 0), 0);
12670 rtx shift_count = XEXP (XEXP (op0, 0), 1);
12672 if (GET_CODE (shift_op) == NOT
12673 || (GET_CODE (shift_op) == XOR
12674 && CONST_INT_P (XEXP (shift_op, 1))
12675 && CONST_INT_P (shift_count)
12676 && HWI_COMPUTABLE_MODE_P (mode)
12677 && (UINTVAL (XEXP (shift_op, 1))
12678 == HOST_WIDE_INT_1U
12679 << INTVAL (shift_count))))
12682 = gen_rtx_LSHIFTRT (mode, XEXP (shift_op, 0), shift_count);
12683 op0 = simplify_and_const_int (NULL_RTX, mode, op0, 1);
12684 code = (code == NE ? EQ : NE);
12685 continue;
12688 break;
12690 case ASHIFT:
12691 /* If we have (compare (ashift FOO N) (const_int C)) and
12692 the high order N bits of FOO (N+1 if an inequality comparison)
12693 are known to be zero, we can do this by comparing FOO with C
12694 shifted right N bits so long as the low-order N bits of C are
12695 zero. */
12696 if (CONST_INT_P (XEXP (op0, 1))
12697 && INTVAL (XEXP (op0, 1)) >= 0
12698 && ((INTVAL (XEXP (op0, 1)) + ! equality_comparison_p)
12699 < HOST_BITS_PER_WIDE_INT)
12700 && (((unsigned HOST_WIDE_INT) const_op
12701 & ((HOST_WIDE_INT_1U << INTVAL (XEXP (op0, 1)))
12702 - 1)) == 0)
12703 && mode_width <= HOST_BITS_PER_WIDE_INT
12704 && (nonzero_bits (XEXP (op0, 0), mode)
12705 & ~(mask >> (INTVAL (XEXP (op0, 1))
12706 + ! equality_comparison_p))) == 0)
12708 /* We must perform a logical shift, not an arithmetic one,
12709 as we want the top N bits of C to be zero. */
12710 unsigned HOST_WIDE_INT temp = const_op & GET_MODE_MASK (mode);
12712 temp >>= INTVAL (XEXP (op0, 1));
12713 op1 = gen_int_mode (temp, mode);
12714 op0 = XEXP (op0, 0);
12715 continue;
12718 /* If we are doing a sign bit comparison, it means we are testing
12719 a particular bit. Convert it to the appropriate AND. */
12720 if (sign_bit_comparison_p && CONST_INT_P (XEXP (op0, 1))
12721 && mode_width <= HOST_BITS_PER_WIDE_INT)
12723 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0),
12724 (HOST_WIDE_INT_1U
12725 << (mode_width - 1
12726 - INTVAL (XEXP (op0, 1)))));
12727 code = (code == LT ? NE : EQ);
12728 continue;
12731 /* If this an equality comparison with zero and we are shifting
12732 the low bit to the sign bit, we can convert this to an AND of the
12733 low-order bit. */
12734 if (const_op == 0 && equality_comparison_p
12735 && CONST_INT_P (XEXP (op0, 1))
12736 && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
12738 op0 = simplify_and_const_int (NULL_RTX, mode, XEXP (op0, 0), 1);
12739 continue;
12741 break;
12743 case ASHIFTRT:
12744 /* If this is an equality comparison with zero, we can do this
12745 as a logical shift, which might be much simpler. */
12746 if (equality_comparison_p && const_op == 0
12747 && CONST_INT_P (XEXP (op0, 1)))
12749 op0 = simplify_shift_const (NULL_RTX, LSHIFTRT, mode,
12750 XEXP (op0, 0),
12751 INTVAL (XEXP (op0, 1)));
12752 continue;
12755 /* If OP0 is a sign extension and CODE is not an unsigned comparison,
12756 do the comparison in a narrower mode. */
12757 if (! unsigned_comparison_p
12758 && CONST_INT_P (XEXP (op0, 1))
12759 && GET_CODE (XEXP (op0, 0)) == ASHIFT
12760 && XEXP (op0, 1) == XEXP (XEXP (op0, 0), 1)
12761 && (int_mode_for_size (mode_width - INTVAL (XEXP (op0, 1)), 1)
12762 .exists (&tmode))
12763 && (((unsigned HOST_WIDE_INT) const_op
12764 + (GET_MODE_MASK (tmode) >> 1) + 1)
12765 <= GET_MODE_MASK (tmode)))
12767 op0 = gen_lowpart (tmode, XEXP (XEXP (op0, 0), 0));
12768 continue;
12771 /* Likewise if OP0 is a PLUS of a sign extension with a
12772 constant, which is usually represented with the PLUS
12773 between the shifts. */
12774 if (! unsigned_comparison_p
12775 && CONST_INT_P (XEXP (op0, 1))
12776 && GET_CODE (XEXP (op0, 0)) == PLUS
12777 && CONST_INT_P (XEXP (XEXP (op0, 0), 1))
12778 && GET_CODE (XEXP (XEXP (op0, 0), 0)) == ASHIFT
12779 && XEXP (op0, 1) == XEXP (XEXP (XEXP (op0, 0), 0), 1)
12780 && (int_mode_for_size (mode_width - INTVAL (XEXP (op0, 1)), 1)
12781 .exists (&tmode))
12782 && (((unsigned HOST_WIDE_INT) const_op
12783 + (GET_MODE_MASK (tmode) >> 1) + 1)
12784 <= GET_MODE_MASK (tmode)))
12786 rtx inner = XEXP (XEXP (XEXP (op0, 0), 0), 0);
12787 rtx add_const = XEXP (XEXP (op0, 0), 1);
12788 rtx new_const = simplify_gen_binary (ASHIFTRT, mode,
12789 add_const, XEXP (op0, 1));
12791 op0 = simplify_gen_binary (PLUS, tmode,
12792 gen_lowpart (tmode, inner),
12793 new_const);
12794 continue;
12797 /* FALLTHROUGH */
12798 case LSHIFTRT:
12799 /* If we have (compare (xshiftrt FOO N) (const_int C)) and
12800 the low order N bits of FOO are known to be zero, we can do this
12801 by comparing FOO with C shifted left N bits so long as no
12802 overflow occurs. Even if the low order N bits of FOO aren't known
12803 to be zero, if the comparison is >= or < we can use the same
12804 optimization and for > or <= by setting all the low
12805 order N bits in the comparison constant. */
12806 if (CONST_INT_P (XEXP (op0, 1))
12807 && INTVAL (XEXP (op0, 1)) > 0
12808 && INTVAL (XEXP (op0, 1)) < HOST_BITS_PER_WIDE_INT
12809 && mode_width <= HOST_BITS_PER_WIDE_INT
12810 && (((unsigned HOST_WIDE_INT) const_op
12811 + (GET_CODE (op0) != LSHIFTRT
12812 ? ((GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1)) >> 1)
12813 + 1)
12814 : 0))
12815 <= GET_MODE_MASK (mode) >> INTVAL (XEXP (op0, 1))))
12817 unsigned HOST_WIDE_INT low_bits
12818 = (nonzero_bits (XEXP (op0, 0), mode)
12819 & ((HOST_WIDE_INT_1U
12820 << INTVAL (XEXP (op0, 1))) - 1));
12821 if (low_bits == 0 || !equality_comparison_p)
12823 /* If the shift was logical, then we must make the condition
12824 unsigned. */
12825 if (GET_CODE (op0) == LSHIFTRT)
12826 code = unsigned_condition (code);
12828 const_op = (unsigned HOST_WIDE_INT) const_op
12829 << INTVAL (XEXP (op0, 1));
12830 if (low_bits != 0
12831 && (code == GT || code == GTU
12832 || code == LE || code == LEU))
12833 const_op
12834 |= ((HOST_WIDE_INT_1 << INTVAL (XEXP (op0, 1))) - 1);
12835 op1 = GEN_INT (const_op);
12836 op0 = XEXP (op0, 0);
12837 continue;
12841 /* If we are using this shift to extract just the sign bit, we
12842 can replace this with an LT or GE comparison. */
12843 if (const_op == 0
12844 && (equality_comparison_p || sign_bit_comparison_p)
12845 && CONST_INT_P (XEXP (op0, 1))
12846 && UINTVAL (XEXP (op0, 1)) == mode_width - 1)
12848 op0 = XEXP (op0, 0);
12849 code = (code == NE || code == GT ? LT : GE);
12850 continue;
12852 break;
12854 default:
12855 break;
12858 break;
12861 /* Now make any compound operations involved in this comparison. Then,
12862 check for an outmost SUBREG on OP0 that is not doing anything or is
12863 paradoxical. The latter transformation must only be performed when
12864 it is known that the "extra" bits will be the same in op0 and op1 or
12865 that they don't matter. There are three cases to consider:
12867 1. SUBREG_REG (op0) is a register. In this case the bits are don't
12868 care bits and we can assume they have any convenient value. So
12869 making the transformation is safe.
12871 2. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is UNKNOWN.
12872 In this case the upper bits of op0 are undefined. We should not make
12873 the simplification in that case as we do not know the contents of
12874 those bits.
12876 3. SUBREG_REG (op0) is a memory and LOAD_EXTEND_OP is not UNKNOWN.
12877 In that case we know those bits are zeros or ones. We must also be
12878 sure that they are the same as the upper bits of op1.
12880 We can never remove a SUBREG for a non-equality comparison because
12881 the sign bit is in a different place in the underlying object. */
12883 rtx_code op0_mco_code = SET;
12884 if (op1 == const0_rtx)
12885 op0_mco_code = code == NE || code == EQ ? EQ : COMPARE;
12887 op0 = make_compound_operation (op0, op0_mco_code);
12888 op1 = make_compound_operation (op1, SET);
12890 if (GET_CODE (op0) == SUBREG && subreg_lowpart_p (op0)
12891 && is_int_mode (GET_MODE (op0), &mode)
12892 && is_int_mode (GET_MODE (SUBREG_REG (op0)), &inner_mode)
12893 && (code == NE || code == EQ))
12895 if (paradoxical_subreg_p (op0))
12897 /* For paradoxical subregs, allow case 1 as above. Case 3 isn't
12898 implemented. */
12899 if (REG_P (SUBREG_REG (op0)))
12901 op0 = SUBREG_REG (op0);
12902 op1 = gen_lowpart (inner_mode, op1);
12905 else if (GET_MODE_PRECISION (inner_mode) <= HOST_BITS_PER_WIDE_INT
12906 && (nonzero_bits (SUBREG_REG (op0), inner_mode)
12907 & ~GET_MODE_MASK (mode)) == 0)
12909 tem = gen_lowpart (inner_mode, op1);
12911 if ((nonzero_bits (tem, inner_mode) & ~GET_MODE_MASK (mode)) == 0)
12912 op0 = SUBREG_REG (op0), op1 = tem;
12916 /* We now do the opposite procedure: Some machines don't have compare
12917 insns in all modes. If OP0's mode is an integer mode smaller than a
12918 word and we can't do a compare in that mode, see if there is a larger
12919 mode for which we can do the compare. There are a number of cases in
12920 which we can use the wider mode. */
12922 if (is_int_mode (GET_MODE (op0), &mode)
12923 && GET_MODE_SIZE (mode) < UNITS_PER_WORD
12924 && ! have_insn_for (COMPARE, mode))
12925 FOR_EACH_WIDER_MODE (tmode_iter, mode)
12927 tmode = tmode_iter.require ();
12928 if (!HWI_COMPUTABLE_MODE_P (tmode))
12929 break;
12930 if (have_insn_for (COMPARE, tmode))
12932 int zero_extended;
12934 /* If this is a test for negative, we can make an explicit
12935 test of the sign bit. Test this first so we can use
12936 a paradoxical subreg to extend OP0. */
12938 if (op1 == const0_rtx && (code == LT || code == GE)
12939 && HWI_COMPUTABLE_MODE_P (mode))
12941 unsigned HOST_WIDE_INT sign
12942 = HOST_WIDE_INT_1U << (GET_MODE_BITSIZE (mode) - 1);
12943 op0 = simplify_gen_binary (AND, tmode,
12944 gen_lowpart (tmode, op0),
12945 gen_int_mode (sign, tmode));
12946 code = (code == LT) ? NE : EQ;
12947 break;
12950 /* If the only nonzero bits in OP0 and OP1 are those in the
12951 narrower mode and this is an equality or unsigned comparison,
12952 we can use the wider mode. Similarly for sign-extended
12953 values, in which case it is true for all comparisons. */
12954 zero_extended = ((code == EQ || code == NE
12955 || code == GEU || code == GTU
12956 || code == LEU || code == LTU)
12957 && (nonzero_bits (op0, tmode)
12958 & ~GET_MODE_MASK (mode)) == 0
12959 && ((CONST_INT_P (op1)
12960 || (nonzero_bits (op1, tmode)
12961 & ~GET_MODE_MASK (mode)) == 0)));
12963 if (zero_extended
12964 || ((num_sign_bit_copies (op0, tmode)
12965 > (unsigned int) (GET_MODE_PRECISION (tmode)
12966 - GET_MODE_PRECISION (mode)))
12967 && (num_sign_bit_copies (op1, tmode)
12968 > (unsigned int) (GET_MODE_PRECISION (tmode)
12969 - GET_MODE_PRECISION (mode)))))
12971 /* If OP0 is an AND and we don't have an AND in MODE either,
12972 make a new AND in the proper mode. */
12973 if (GET_CODE (op0) == AND
12974 && !have_insn_for (AND, mode))
12975 op0 = simplify_gen_binary (AND, tmode,
12976 gen_lowpart (tmode,
12977 XEXP (op0, 0)),
12978 gen_lowpart (tmode,
12979 XEXP (op0, 1)));
12980 else
12982 if (zero_extended)
12984 op0 = simplify_gen_unary (ZERO_EXTEND, tmode,
12985 op0, mode);
12986 op1 = simplify_gen_unary (ZERO_EXTEND, tmode,
12987 op1, mode);
12989 else
12991 op0 = simplify_gen_unary (SIGN_EXTEND, tmode,
12992 op0, mode);
12993 op1 = simplify_gen_unary (SIGN_EXTEND, tmode,
12994 op1, mode);
12996 break;
13002 /* We may have changed the comparison operands. Re-canonicalize. */
13003 if (swap_commutative_operands_p (op0, op1))
13005 std::swap (op0, op1);
13006 code = swap_condition (code);
13009 /* If this machine only supports a subset of valid comparisons, see if we
13010 can convert an unsupported one into a supported one. */
13011 target_canonicalize_comparison (&code, &op0, &op1, 0);
13013 *pop0 = op0;
13014 *pop1 = op1;
13016 return code;
13019 /* Utility function for record_value_for_reg. Count number of
13020 rtxs in X. */
13021 static int
13022 count_rtxs (rtx x)
13024 enum rtx_code code = GET_CODE (x);
13025 const char *fmt;
13026 int i, j, ret = 1;
13028 if (GET_RTX_CLASS (code) == RTX_BIN_ARITH
13029 || GET_RTX_CLASS (code) == RTX_COMM_ARITH)
13031 rtx x0 = XEXP (x, 0);
13032 rtx x1 = XEXP (x, 1);
13034 if (x0 == x1)
13035 return 1 + 2 * count_rtxs (x0);
13037 if ((GET_RTX_CLASS (GET_CODE (x1)) == RTX_BIN_ARITH
13038 || GET_RTX_CLASS (GET_CODE (x1)) == RTX_COMM_ARITH)
13039 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
13040 return 2 + 2 * count_rtxs (x0)
13041 + count_rtxs (x == XEXP (x1, 0)
13042 ? XEXP (x1, 1) : XEXP (x1, 0));
13044 if ((GET_RTX_CLASS (GET_CODE (x0)) == RTX_BIN_ARITH
13045 || GET_RTX_CLASS (GET_CODE (x0)) == RTX_COMM_ARITH)
13046 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
13047 return 2 + 2 * count_rtxs (x1)
13048 + count_rtxs (x == XEXP (x0, 0)
13049 ? XEXP (x0, 1) : XEXP (x0, 0));
13052 fmt = GET_RTX_FORMAT (code);
13053 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
13054 if (fmt[i] == 'e')
13055 ret += count_rtxs (XEXP (x, i));
13056 else if (fmt[i] == 'E')
13057 for (j = 0; j < XVECLEN (x, i); j++)
13058 ret += count_rtxs (XVECEXP (x, i, j));
13060 return ret;
13063 /* Utility function for following routine. Called when X is part of a value
13064 being stored into last_set_value. Sets last_set_table_tick
13065 for each register mentioned. Similar to mention_regs in cse.c */
13067 static void
13068 update_table_tick (rtx x)
13070 enum rtx_code code = GET_CODE (x);
13071 const char *fmt = GET_RTX_FORMAT (code);
13072 int i, j;
13074 if (code == REG)
13076 unsigned int regno = REGNO (x);
13077 unsigned int endregno = END_REGNO (x);
13078 unsigned int r;
13080 for (r = regno; r < endregno; r++)
13082 reg_stat_type *rsp = &reg_stat[r];
13083 rsp->last_set_table_tick = label_tick;
13086 return;
13089 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
13090 if (fmt[i] == 'e')
13092 /* Check for identical subexpressions. If x contains
13093 identical subexpression we only have to traverse one of
13094 them. */
13095 if (i == 0 && ARITHMETIC_P (x))
13097 /* Note that at this point x1 has already been
13098 processed. */
13099 rtx x0 = XEXP (x, 0);
13100 rtx x1 = XEXP (x, 1);
13102 /* If x0 and x1 are identical then there is no need to
13103 process x0. */
13104 if (x0 == x1)
13105 break;
13107 /* If x0 is identical to a subexpression of x1 then while
13108 processing x1, x0 has already been processed. Thus we
13109 are done with x. */
13110 if (ARITHMETIC_P (x1)
13111 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
13112 break;
13114 /* If x1 is identical to a subexpression of x0 then we
13115 still have to process the rest of x0. */
13116 if (ARITHMETIC_P (x0)
13117 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
13119 update_table_tick (XEXP (x0, x1 == XEXP (x0, 0) ? 1 : 0));
13120 break;
13124 update_table_tick (XEXP (x, i));
13126 else if (fmt[i] == 'E')
13127 for (j = 0; j < XVECLEN (x, i); j++)
13128 update_table_tick (XVECEXP (x, i, j));
13131 /* Record that REG is set to VALUE in insn INSN. If VALUE is zero, we
13132 are saying that the register is clobbered and we no longer know its
13133 value. If INSN is zero, don't update reg_stat[].last_set; this is
13134 only permitted with VALUE also zero and is used to invalidate the
13135 register. */
13137 static void
13138 record_value_for_reg (rtx reg, rtx_insn *insn, rtx value)
13140 unsigned int regno = REGNO (reg);
13141 unsigned int endregno = END_REGNO (reg);
13142 unsigned int i;
13143 reg_stat_type *rsp;
13145 /* If VALUE contains REG and we have a previous value for REG, substitute
13146 the previous value. */
13147 if (value && insn && reg_overlap_mentioned_p (reg, value))
13149 rtx tem;
13151 /* Set things up so get_last_value is allowed to see anything set up to
13152 our insn. */
13153 subst_low_luid = DF_INSN_LUID (insn);
13154 tem = get_last_value (reg);
13156 /* If TEM is simply a binary operation with two CLOBBERs as operands,
13157 it isn't going to be useful and will take a lot of time to process,
13158 so just use the CLOBBER. */
13160 if (tem)
13162 if (ARITHMETIC_P (tem)
13163 && GET_CODE (XEXP (tem, 0)) == CLOBBER
13164 && GET_CODE (XEXP (tem, 1)) == CLOBBER)
13165 tem = XEXP (tem, 0);
13166 else if (count_occurrences (value, reg, 1) >= 2)
13168 /* If there are two or more occurrences of REG in VALUE,
13169 prevent the value from growing too much. */
13170 if (count_rtxs (tem) > MAX_LAST_VALUE_RTL)
13171 tem = gen_rtx_CLOBBER (GET_MODE (tem), const0_rtx);
13174 value = replace_rtx (copy_rtx (value), reg, tem);
13178 /* For each register modified, show we don't know its value, that
13179 we don't know about its bitwise content, that its value has been
13180 updated, and that we don't know the location of the death of the
13181 register. */
13182 for (i = regno; i < endregno; i++)
13184 rsp = &reg_stat[i];
13186 if (insn)
13187 rsp->last_set = insn;
13189 rsp->last_set_value = 0;
13190 rsp->last_set_mode = VOIDmode;
13191 rsp->last_set_nonzero_bits = 0;
13192 rsp->last_set_sign_bit_copies = 0;
13193 rsp->last_death = 0;
13194 rsp->truncated_to_mode = VOIDmode;
13197 /* Mark registers that are being referenced in this value. */
13198 if (value)
13199 update_table_tick (value);
13201 /* Now update the status of each register being set.
13202 If someone is using this register in this block, set this register
13203 to invalid since we will get confused between the two lives in this
13204 basic block. This makes using this register always invalid. In cse, we
13205 scan the table to invalidate all entries using this register, but this
13206 is too much work for us. */
13208 for (i = regno; i < endregno; i++)
13210 rsp = &reg_stat[i];
13211 rsp->last_set_label = label_tick;
13212 if (!insn
13213 || (value && rsp->last_set_table_tick >= label_tick_ebb_start))
13214 rsp->last_set_invalid = 1;
13215 else
13216 rsp->last_set_invalid = 0;
13219 /* The value being assigned might refer to X (like in "x++;"). In that
13220 case, we must replace it with (clobber (const_int 0)) to prevent
13221 infinite loops. */
13222 rsp = &reg_stat[regno];
13223 if (value && !get_last_value_validate (&value, insn, label_tick, 0))
13225 value = copy_rtx (value);
13226 if (!get_last_value_validate (&value, insn, label_tick, 1))
13227 value = 0;
13230 /* For the main register being modified, update the value, the mode, the
13231 nonzero bits, and the number of sign bit copies. */
13233 rsp->last_set_value = value;
13235 if (value)
13237 machine_mode mode = GET_MODE (reg);
13238 subst_low_luid = DF_INSN_LUID (insn);
13239 rsp->last_set_mode = mode;
13240 if (GET_MODE_CLASS (mode) == MODE_INT
13241 && HWI_COMPUTABLE_MODE_P (mode))
13242 mode = nonzero_bits_mode;
13243 rsp->last_set_nonzero_bits = nonzero_bits (value, mode);
13244 rsp->last_set_sign_bit_copies
13245 = num_sign_bit_copies (value, GET_MODE (reg));
13249 /* Called via note_stores from record_dead_and_set_regs to handle one
13250 SET or CLOBBER in an insn. DATA is the instruction in which the
13251 set is occurring. */
13253 static void
13254 record_dead_and_set_regs_1 (rtx dest, const_rtx setter, void *data)
13256 rtx_insn *record_dead_insn = (rtx_insn *) data;
13258 if (GET_CODE (dest) == SUBREG)
13259 dest = SUBREG_REG (dest);
13261 if (!record_dead_insn)
13263 if (REG_P (dest))
13264 record_value_for_reg (dest, NULL, NULL_RTX);
13265 return;
13268 if (REG_P (dest))
13270 /* If we are setting the whole register, we know its value. Otherwise
13271 show that we don't know the value. We can handle a SUBREG if it's
13272 the low part, but we must be careful with paradoxical SUBREGs on
13273 RISC architectures because we cannot strip e.g. an extension around
13274 a load and record the naked load since the RTL middle-end considers
13275 that the upper bits are defined according to LOAD_EXTEND_OP. */
13276 if (GET_CODE (setter) == SET && dest == SET_DEST (setter))
13277 record_value_for_reg (dest, record_dead_insn, SET_SRC (setter));
13278 else if (GET_CODE (setter) == SET
13279 && GET_CODE (SET_DEST (setter)) == SUBREG
13280 && SUBREG_REG (SET_DEST (setter)) == dest
13281 && known_le (GET_MODE_PRECISION (GET_MODE (dest)),
13282 BITS_PER_WORD)
13283 && subreg_lowpart_p (SET_DEST (setter)))
13284 record_value_for_reg (dest, record_dead_insn,
13285 WORD_REGISTER_OPERATIONS
13286 && paradoxical_subreg_p (SET_DEST (setter))
13287 ? SET_SRC (setter)
13288 : gen_lowpart (GET_MODE (dest),
13289 SET_SRC (setter)));
13290 else
13291 record_value_for_reg (dest, record_dead_insn, NULL_RTX);
13293 else if (MEM_P (dest)
13294 /* Ignore pushes, they clobber nothing. */
13295 && ! push_operand (dest, GET_MODE (dest)))
13296 mem_last_set = DF_INSN_LUID (record_dead_insn);
13299 /* Update the records of when each REG was most recently set or killed
13300 for the things done by INSN. This is the last thing done in processing
13301 INSN in the combiner loop.
13303 We update reg_stat[], in particular fields last_set, last_set_value,
13304 last_set_mode, last_set_nonzero_bits, last_set_sign_bit_copies,
13305 last_death, and also the similar information mem_last_set (which insn
13306 most recently modified memory) and last_call_luid (which insn was the
13307 most recent subroutine call). */
13309 static void
13310 record_dead_and_set_regs (rtx_insn *insn)
13312 rtx link;
13313 unsigned int i;
13315 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
13317 if (REG_NOTE_KIND (link) == REG_DEAD
13318 && REG_P (XEXP (link, 0)))
13320 unsigned int regno = REGNO (XEXP (link, 0));
13321 unsigned int endregno = END_REGNO (XEXP (link, 0));
13323 for (i = regno; i < endregno; i++)
13325 reg_stat_type *rsp;
13327 rsp = &reg_stat[i];
13328 rsp->last_death = insn;
13331 else if (REG_NOTE_KIND (link) == REG_INC)
13332 record_value_for_reg (XEXP (link, 0), insn, NULL_RTX);
13335 if (CALL_P (insn))
13337 hard_reg_set_iterator hrsi;
13338 EXECUTE_IF_SET_IN_HARD_REG_SET (regs_invalidated_by_call, 0, i, hrsi)
13340 reg_stat_type *rsp;
13342 rsp = &reg_stat[i];
13343 rsp->last_set_invalid = 1;
13344 rsp->last_set = insn;
13345 rsp->last_set_value = 0;
13346 rsp->last_set_mode = VOIDmode;
13347 rsp->last_set_nonzero_bits = 0;
13348 rsp->last_set_sign_bit_copies = 0;
13349 rsp->last_death = 0;
13350 rsp->truncated_to_mode = VOIDmode;
13353 last_call_luid = mem_last_set = DF_INSN_LUID (insn);
13355 /* We can't combine into a call pattern. Remember, though, that
13356 the return value register is set at this LUID. We could
13357 still replace a register with the return value from the
13358 wrong subroutine call! */
13359 note_stores (PATTERN (insn), record_dead_and_set_regs_1, NULL_RTX);
13361 else
13362 note_stores (PATTERN (insn), record_dead_and_set_regs_1, insn);
13365 /* If a SUBREG has the promoted bit set, it is in fact a property of the
13366 register present in the SUBREG, so for each such SUBREG go back and
13367 adjust nonzero and sign bit information of the registers that are
13368 known to have some zero/sign bits set.
13370 This is needed because when combine blows the SUBREGs away, the
13371 information on zero/sign bits is lost and further combines can be
13372 missed because of that. */
13374 static void
13375 record_promoted_value (rtx_insn *insn, rtx subreg)
13377 struct insn_link *links;
13378 rtx set;
13379 unsigned int regno = REGNO (SUBREG_REG (subreg));
13380 machine_mode mode = GET_MODE (subreg);
13382 if (!HWI_COMPUTABLE_MODE_P (mode))
13383 return;
13385 for (links = LOG_LINKS (insn); links;)
13387 reg_stat_type *rsp;
13389 insn = links->insn;
13390 set = single_set (insn);
13392 if (! set || !REG_P (SET_DEST (set))
13393 || REGNO (SET_DEST (set)) != regno
13394 || GET_MODE (SET_DEST (set)) != GET_MODE (SUBREG_REG (subreg)))
13396 links = links->next;
13397 continue;
13400 rsp = &reg_stat[regno];
13401 if (rsp->last_set == insn)
13403 if (SUBREG_PROMOTED_UNSIGNED_P (subreg))
13404 rsp->last_set_nonzero_bits &= GET_MODE_MASK (mode);
13407 if (REG_P (SET_SRC (set)))
13409 regno = REGNO (SET_SRC (set));
13410 links = LOG_LINKS (insn);
13412 else
13413 break;
13417 /* Check if X, a register, is known to contain a value already
13418 truncated to MODE. In this case we can use a subreg to refer to
13419 the truncated value even though in the generic case we would need
13420 an explicit truncation. */
13422 static bool
13423 reg_truncated_to_mode (machine_mode mode, const_rtx x)
13425 reg_stat_type *rsp = &reg_stat[REGNO (x)];
13426 machine_mode truncated = rsp->truncated_to_mode;
13428 if (truncated == 0
13429 || rsp->truncation_label < label_tick_ebb_start)
13430 return false;
13431 if (!partial_subreg_p (mode, truncated))
13432 return true;
13433 if (TRULY_NOOP_TRUNCATION_MODES_P (mode, truncated))
13434 return true;
13435 return false;
13438 /* If X is a hard reg or a subreg record the mode that the register is
13439 accessed in. For non-TARGET_TRULY_NOOP_TRUNCATION targets we might be
13440 able to turn a truncate into a subreg using this information. Return true
13441 if traversing X is complete. */
13443 static bool
13444 record_truncated_value (rtx x)
13446 machine_mode truncated_mode;
13447 reg_stat_type *rsp;
13449 if (GET_CODE (x) == SUBREG && REG_P (SUBREG_REG (x)))
13451 machine_mode original_mode = GET_MODE (SUBREG_REG (x));
13452 truncated_mode = GET_MODE (x);
13454 if (!partial_subreg_p (truncated_mode, original_mode))
13455 return true;
13457 truncated_mode = GET_MODE (x);
13458 if (TRULY_NOOP_TRUNCATION_MODES_P (truncated_mode, original_mode))
13459 return true;
13461 x = SUBREG_REG (x);
13463 /* ??? For hard-regs we now record everything. We might be able to
13464 optimize this using last_set_mode. */
13465 else if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
13466 truncated_mode = GET_MODE (x);
13467 else
13468 return false;
13470 rsp = &reg_stat[REGNO (x)];
13471 if (rsp->truncated_to_mode == 0
13472 || rsp->truncation_label < label_tick_ebb_start
13473 || partial_subreg_p (truncated_mode, rsp->truncated_to_mode))
13475 rsp->truncated_to_mode = truncated_mode;
13476 rsp->truncation_label = label_tick;
13479 return true;
13482 /* Callback for note_uses. Find hardregs and subregs of pseudos and
13483 the modes they are used in. This can help truning TRUNCATEs into
13484 SUBREGs. */
13486 static void
13487 record_truncated_values (rtx *loc, void *data ATTRIBUTE_UNUSED)
13489 subrtx_var_iterator::array_type array;
13490 FOR_EACH_SUBRTX_VAR (iter, array, *loc, NONCONST)
13491 if (record_truncated_value (*iter))
13492 iter.skip_subrtxes ();
13495 /* Scan X for promoted SUBREGs. For each one found,
13496 note what it implies to the registers used in it. */
13498 static void
13499 check_promoted_subreg (rtx_insn *insn, rtx x)
13501 if (GET_CODE (x) == SUBREG
13502 && SUBREG_PROMOTED_VAR_P (x)
13503 && REG_P (SUBREG_REG (x)))
13504 record_promoted_value (insn, x);
13505 else
13507 const char *format = GET_RTX_FORMAT (GET_CODE (x));
13508 int i, j;
13510 for (i = 0; i < GET_RTX_LENGTH (GET_CODE (x)); i++)
13511 switch (format[i])
13513 case 'e':
13514 check_promoted_subreg (insn, XEXP (x, i));
13515 break;
13516 case 'V':
13517 case 'E':
13518 if (XVEC (x, i) != 0)
13519 for (j = 0; j < XVECLEN (x, i); j++)
13520 check_promoted_subreg (insn, XVECEXP (x, i, j));
13521 break;
13526 /* Verify that all the registers and memory references mentioned in *LOC are
13527 still valid. *LOC was part of a value set in INSN when label_tick was
13528 equal to TICK. Return 0 if some are not. If REPLACE is nonzero, replace
13529 the invalid references with (clobber (const_int 0)) and return 1. This
13530 replacement is useful because we often can get useful information about
13531 the form of a value (e.g., if it was produced by a shift that always
13532 produces -1 or 0) even though we don't know exactly what registers it
13533 was produced from. */
13535 static int
13536 get_last_value_validate (rtx *loc, rtx_insn *insn, int tick, int replace)
13538 rtx x = *loc;
13539 const char *fmt = GET_RTX_FORMAT (GET_CODE (x));
13540 int len = GET_RTX_LENGTH (GET_CODE (x));
13541 int i, j;
13543 if (REG_P (x))
13545 unsigned int regno = REGNO (x);
13546 unsigned int endregno = END_REGNO (x);
13547 unsigned int j;
13549 for (j = regno; j < endregno; j++)
13551 reg_stat_type *rsp = &reg_stat[j];
13552 if (rsp->last_set_invalid
13553 /* If this is a pseudo-register that was only set once and not
13554 live at the beginning of the function, it is always valid. */
13555 || (! (regno >= FIRST_PSEUDO_REGISTER
13556 && regno < reg_n_sets_max
13557 && REG_N_SETS (regno) == 1
13558 && (!REGNO_REG_SET_P
13559 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb),
13560 regno)))
13561 && rsp->last_set_label > tick))
13563 if (replace)
13564 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
13565 return replace;
13569 return 1;
13571 /* If this is a memory reference, make sure that there were no stores after
13572 it that might have clobbered the value. We don't have alias info, so we
13573 assume any store invalidates it. Moreover, we only have local UIDs, so
13574 we also assume that there were stores in the intervening basic blocks. */
13575 else if (MEM_P (x) && !MEM_READONLY_P (x)
13576 && (tick != label_tick || DF_INSN_LUID (insn) <= mem_last_set))
13578 if (replace)
13579 *loc = gen_rtx_CLOBBER (GET_MODE (x), const0_rtx);
13580 return replace;
13583 for (i = 0; i < len; i++)
13585 if (fmt[i] == 'e')
13587 /* Check for identical subexpressions. If x contains
13588 identical subexpression we only have to traverse one of
13589 them. */
13590 if (i == 1 && ARITHMETIC_P (x))
13592 /* Note that at this point x0 has already been checked
13593 and found valid. */
13594 rtx x0 = XEXP (x, 0);
13595 rtx x1 = XEXP (x, 1);
13597 /* If x0 and x1 are identical then x is also valid. */
13598 if (x0 == x1)
13599 return 1;
13601 /* If x1 is identical to a subexpression of x0 then
13602 while checking x0, x1 has already been checked. Thus
13603 it is valid and so as x. */
13604 if (ARITHMETIC_P (x0)
13605 && (x1 == XEXP (x0, 0) || x1 == XEXP (x0, 1)))
13606 return 1;
13608 /* If x0 is identical to a subexpression of x1 then x is
13609 valid iff the rest of x1 is valid. */
13610 if (ARITHMETIC_P (x1)
13611 && (x0 == XEXP (x1, 0) || x0 == XEXP (x1, 1)))
13612 return
13613 get_last_value_validate (&XEXP (x1,
13614 x0 == XEXP (x1, 0) ? 1 : 0),
13615 insn, tick, replace);
13618 if (get_last_value_validate (&XEXP (x, i), insn, tick,
13619 replace) == 0)
13620 return 0;
13622 else if (fmt[i] == 'E')
13623 for (j = 0; j < XVECLEN (x, i); j++)
13624 if (get_last_value_validate (&XVECEXP (x, i, j),
13625 insn, tick, replace) == 0)
13626 return 0;
13629 /* If we haven't found a reason for it to be invalid, it is valid. */
13630 return 1;
13633 /* Get the last value assigned to X, if known. Some registers
13634 in the value may be replaced with (clobber (const_int 0)) if their value
13635 is known longer known reliably. */
13637 static rtx
13638 get_last_value (const_rtx x)
13640 unsigned int regno;
13641 rtx value;
13642 reg_stat_type *rsp;
13644 /* If this is a non-paradoxical SUBREG, get the value of its operand and
13645 then convert it to the desired mode. If this is a paradoxical SUBREG,
13646 we cannot predict what values the "extra" bits might have. */
13647 if (GET_CODE (x) == SUBREG
13648 && subreg_lowpart_p (x)
13649 && !paradoxical_subreg_p (x)
13650 && (value = get_last_value (SUBREG_REG (x))) != 0)
13651 return gen_lowpart (GET_MODE (x), value);
13653 if (!REG_P (x))
13654 return 0;
13656 regno = REGNO (x);
13657 rsp = &reg_stat[regno];
13658 value = rsp->last_set_value;
13660 /* If we don't have a value, or if it isn't for this basic block and
13661 it's either a hard register, set more than once, or it's a live
13662 at the beginning of the function, return 0.
13664 Because if it's not live at the beginning of the function then the reg
13665 is always set before being used (is never used without being set).
13666 And, if it's set only once, and it's always set before use, then all
13667 uses must have the same last value, even if it's not from this basic
13668 block. */
13670 if (value == 0
13671 || (rsp->last_set_label < label_tick_ebb_start
13672 && (regno < FIRST_PSEUDO_REGISTER
13673 || regno >= reg_n_sets_max
13674 || REG_N_SETS (regno) != 1
13675 || REGNO_REG_SET_P
13676 (DF_LR_IN (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb), regno))))
13677 return 0;
13679 /* If the value was set in a later insn than the ones we are processing,
13680 we can't use it even if the register was only set once. */
13681 if (rsp->last_set_label == label_tick
13682 && DF_INSN_LUID (rsp->last_set) >= subst_low_luid)
13683 return 0;
13685 /* If fewer bits were set than what we are asked for now, we cannot use
13686 the value. */
13687 if (maybe_lt (GET_MODE_PRECISION (rsp->last_set_mode),
13688 GET_MODE_PRECISION (GET_MODE (x))))
13689 return 0;
13691 /* If the value has all its registers valid, return it. */
13692 if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 0))
13693 return value;
13695 /* Otherwise, make a copy and replace any invalid register with
13696 (clobber (const_int 0)). If that fails for some reason, return 0. */
13698 value = copy_rtx (value);
13699 if (get_last_value_validate (&value, rsp->last_set, rsp->last_set_label, 1))
13700 return value;
13702 return 0;
13705 /* Define three variables used for communication between the following
13706 routines. */
13708 static unsigned int reg_dead_regno, reg_dead_endregno;
13709 static int reg_dead_flag;
13711 /* Function called via note_stores from reg_dead_at_p.
13713 If DEST is within [reg_dead_regno, reg_dead_endregno), set
13714 reg_dead_flag to 1 if X is a CLOBBER and to -1 it is a SET. */
13716 static void
13717 reg_dead_at_p_1 (rtx dest, const_rtx x, void *data ATTRIBUTE_UNUSED)
13719 unsigned int regno, endregno;
13721 if (!REG_P (dest))
13722 return;
13724 regno = REGNO (dest);
13725 endregno = END_REGNO (dest);
13726 if (reg_dead_endregno > regno && reg_dead_regno < endregno)
13727 reg_dead_flag = (GET_CODE (x) == CLOBBER) ? 1 : -1;
13730 /* Return nonzero if REG is known to be dead at INSN.
13732 We scan backwards from INSN. If we hit a REG_DEAD note or a CLOBBER
13733 referencing REG, it is dead. If we hit a SET referencing REG, it is
13734 live. Otherwise, see if it is live or dead at the start of the basic
13735 block we are in. Hard regs marked as being live in NEWPAT_USED_REGS
13736 must be assumed to be always live. */
13738 static int
13739 reg_dead_at_p (rtx reg, rtx_insn *insn)
13741 basic_block block;
13742 unsigned int i;
13744 /* Set variables for reg_dead_at_p_1. */
13745 reg_dead_regno = REGNO (reg);
13746 reg_dead_endregno = END_REGNO (reg);
13748 reg_dead_flag = 0;
13750 /* Check that reg isn't mentioned in NEWPAT_USED_REGS. For fixed registers
13751 we allow the machine description to decide whether use-and-clobber
13752 patterns are OK. */
13753 if (reg_dead_regno < FIRST_PSEUDO_REGISTER)
13755 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
13756 if (!fixed_regs[i] && TEST_HARD_REG_BIT (newpat_used_regs, i))
13757 return 0;
13760 /* Scan backwards until we find a REG_DEAD note, SET, CLOBBER, or
13761 beginning of basic block. */
13762 block = BLOCK_FOR_INSN (insn);
13763 for (;;)
13765 if (INSN_P (insn))
13767 if (find_regno_note (insn, REG_UNUSED, reg_dead_regno))
13768 return 1;
13770 note_stores (PATTERN (insn), reg_dead_at_p_1, NULL);
13771 if (reg_dead_flag)
13772 return reg_dead_flag == 1 ? 1 : 0;
13774 if (find_regno_note (insn, REG_DEAD, reg_dead_regno))
13775 return 1;
13778 if (insn == BB_HEAD (block))
13779 break;
13781 insn = PREV_INSN (insn);
13784 /* Look at live-in sets for the basic block that we were in. */
13785 for (i = reg_dead_regno; i < reg_dead_endregno; i++)
13786 if (REGNO_REG_SET_P (df_get_live_in (block), i))
13787 return 0;
13789 return 1;
13792 /* Note hard registers in X that are used. */
13794 static void
13795 mark_used_regs_combine (rtx x)
13797 RTX_CODE code = GET_CODE (x);
13798 unsigned int regno;
13799 int i;
13801 switch (code)
13803 case LABEL_REF:
13804 case SYMBOL_REF:
13805 case CONST:
13806 CASE_CONST_ANY:
13807 case PC:
13808 case ADDR_VEC:
13809 case ADDR_DIFF_VEC:
13810 case ASM_INPUT:
13811 /* CC0 must die in the insn after it is set, so we don't need to take
13812 special note of it here. */
13813 case CC0:
13814 return;
13816 case CLOBBER:
13817 /* If we are clobbering a MEM, mark any hard registers inside the
13818 address as used. */
13819 if (MEM_P (XEXP (x, 0)))
13820 mark_used_regs_combine (XEXP (XEXP (x, 0), 0));
13821 return;
13823 case REG:
13824 regno = REGNO (x);
13825 /* A hard reg in a wide mode may really be multiple registers.
13826 If so, mark all of them just like the first. */
13827 if (regno < FIRST_PSEUDO_REGISTER)
13829 /* None of this applies to the stack, frame or arg pointers. */
13830 if (regno == STACK_POINTER_REGNUM
13831 || (!HARD_FRAME_POINTER_IS_FRAME_POINTER
13832 && regno == HARD_FRAME_POINTER_REGNUM)
13833 || (FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
13834 && regno == ARG_POINTER_REGNUM && fixed_regs[regno])
13835 || regno == FRAME_POINTER_REGNUM)
13836 return;
13838 add_to_hard_reg_set (&newpat_used_regs, GET_MODE (x), regno);
13840 return;
13842 case SET:
13844 /* If setting a MEM, or a SUBREG of a MEM, then note any hard regs in
13845 the address. */
13846 rtx testreg = SET_DEST (x);
13848 while (GET_CODE (testreg) == SUBREG
13849 || GET_CODE (testreg) == ZERO_EXTRACT
13850 || GET_CODE (testreg) == STRICT_LOW_PART)
13851 testreg = XEXP (testreg, 0);
13853 if (MEM_P (testreg))
13854 mark_used_regs_combine (XEXP (testreg, 0));
13856 mark_used_regs_combine (SET_SRC (x));
13858 return;
13860 default:
13861 break;
13864 /* Recursively scan the operands of this expression. */
13867 const char *fmt = GET_RTX_FORMAT (code);
13869 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
13871 if (fmt[i] == 'e')
13872 mark_used_regs_combine (XEXP (x, i));
13873 else if (fmt[i] == 'E')
13875 int j;
13877 for (j = 0; j < XVECLEN (x, i); j++)
13878 mark_used_regs_combine (XVECEXP (x, i, j));
13884 /* Remove register number REGNO from the dead registers list of INSN.
13886 Return the note used to record the death, if there was one. */
13889 remove_death (unsigned int regno, rtx_insn *insn)
13891 rtx note = find_regno_note (insn, REG_DEAD, regno);
13893 if (note)
13894 remove_note (insn, note);
13896 return note;
13899 /* For each register (hardware or pseudo) used within expression X, if its
13900 death is in an instruction with luid between FROM_LUID (inclusive) and
13901 TO_INSN (exclusive), put a REG_DEAD note for that register in the
13902 list headed by PNOTES.
13904 That said, don't move registers killed by maybe_kill_insn.
13906 This is done when X is being merged by combination into TO_INSN. These
13907 notes will then be distributed as needed. */
13909 static void
13910 move_deaths (rtx x, rtx maybe_kill_insn, int from_luid, rtx_insn *to_insn,
13911 rtx *pnotes)
13913 const char *fmt;
13914 int len, i;
13915 enum rtx_code code = GET_CODE (x);
13917 if (code == REG)
13919 unsigned int regno = REGNO (x);
13920 rtx_insn *where_dead = reg_stat[regno].last_death;
13922 /* If we do not know where the register died, it may still die between
13923 FROM_LUID and TO_INSN. If so, find it. This is PR83304. */
13924 if (!where_dead || DF_INSN_LUID (where_dead) >= DF_INSN_LUID (to_insn))
13926 rtx_insn *insn = prev_real_nondebug_insn (to_insn);
13927 while (insn
13928 && BLOCK_FOR_INSN (insn) == BLOCK_FOR_INSN (to_insn)
13929 && DF_INSN_LUID (insn) >= from_luid)
13931 if (dead_or_set_regno_p (insn, regno))
13933 if (find_regno_note (insn, REG_DEAD, regno))
13934 where_dead = insn;
13935 break;
13938 insn = prev_real_nondebug_insn (insn);
13942 /* Don't move the register if it gets killed in between from and to. */
13943 if (maybe_kill_insn && reg_set_p (x, maybe_kill_insn)
13944 && ! reg_referenced_p (x, maybe_kill_insn))
13945 return;
13947 if (where_dead
13948 && BLOCK_FOR_INSN (where_dead) == BLOCK_FOR_INSN (to_insn)
13949 && DF_INSN_LUID (where_dead) >= from_luid
13950 && DF_INSN_LUID (where_dead) < DF_INSN_LUID (to_insn))
13952 rtx note = remove_death (regno, where_dead);
13954 /* It is possible for the call above to return 0. This can occur
13955 when last_death points to I2 or I1 that we combined with.
13956 In that case make a new note.
13958 We must also check for the case where X is a hard register
13959 and NOTE is a death note for a range of hard registers
13960 including X. In that case, we must put REG_DEAD notes for
13961 the remaining registers in place of NOTE. */
13963 if (note != 0 && regno < FIRST_PSEUDO_REGISTER
13964 && partial_subreg_p (GET_MODE (x), GET_MODE (XEXP (note, 0))))
13966 unsigned int deadregno = REGNO (XEXP (note, 0));
13967 unsigned int deadend = END_REGNO (XEXP (note, 0));
13968 unsigned int ourend = END_REGNO (x);
13969 unsigned int i;
13971 for (i = deadregno; i < deadend; i++)
13972 if (i < regno || i >= ourend)
13973 add_reg_note (where_dead, REG_DEAD, regno_reg_rtx[i]);
13976 /* If we didn't find any note, or if we found a REG_DEAD note that
13977 covers only part of the given reg, and we have a multi-reg hard
13978 register, then to be safe we must check for REG_DEAD notes
13979 for each register other than the first. They could have
13980 their own REG_DEAD notes lying around. */
13981 else if ((note == 0
13982 || (note != 0
13983 && partial_subreg_p (GET_MODE (XEXP (note, 0)),
13984 GET_MODE (x))))
13985 && regno < FIRST_PSEUDO_REGISTER
13986 && REG_NREGS (x) > 1)
13988 unsigned int ourend = END_REGNO (x);
13989 unsigned int i, offset;
13990 rtx oldnotes = 0;
13992 if (note)
13993 offset = hard_regno_nregs (regno, GET_MODE (XEXP (note, 0)));
13994 else
13995 offset = 1;
13997 for (i = regno + offset; i < ourend; i++)
13998 move_deaths (regno_reg_rtx[i],
13999 maybe_kill_insn, from_luid, to_insn, &oldnotes);
14002 if (note != 0 && GET_MODE (XEXP (note, 0)) == GET_MODE (x))
14004 XEXP (note, 1) = *pnotes;
14005 *pnotes = note;
14007 else
14008 *pnotes = alloc_reg_note (REG_DEAD, x, *pnotes);
14011 return;
14014 else if (GET_CODE (x) == SET)
14016 rtx dest = SET_DEST (x);
14018 move_deaths (SET_SRC (x), maybe_kill_insn, from_luid, to_insn, pnotes);
14020 /* In the case of a ZERO_EXTRACT, a STRICT_LOW_PART, or a SUBREG
14021 that accesses one word of a multi-word item, some
14022 piece of everything register in the expression is used by
14023 this insn, so remove any old death. */
14024 /* ??? So why do we test for equality of the sizes? */
14026 if (GET_CODE (dest) == ZERO_EXTRACT
14027 || GET_CODE (dest) == STRICT_LOW_PART
14028 || (GET_CODE (dest) == SUBREG
14029 && !read_modify_subreg_p (dest)))
14031 move_deaths (dest, maybe_kill_insn, from_luid, to_insn, pnotes);
14032 return;
14035 /* If this is some other SUBREG, we know it replaces the entire
14036 value, so use that as the destination. */
14037 if (GET_CODE (dest) == SUBREG)
14038 dest = SUBREG_REG (dest);
14040 /* If this is a MEM, adjust deaths of anything used in the address.
14041 For a REG (the only other possibility), the entire value is
14042 being replaced so the old value is not used in this insn. */
14044 if (MEM_P (dest))
14045 move_deaths (XEXP (dest, 0), maybe_kill_insn, from_luid,
14046 to_insn, pnotes);
14047 return;
14050 else if (GET_CODE (x) == CLOBBER)
14051 return;
14053 len = GET_RTX_LENGTH (code);
14054 fmt = GET_RTX_FORMAT (code);
14056 for (i = 0; i < len; i++)
14058 if (fmt[i] == 'E')
14060 int j;
14061 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
14062 move_deaths (XVECEXP (x, i, j), maybe_kill_insn, from_luid,
14063 to_insn, pnotes);
14065 else if (fmt[i] == 'e')
14066 move_deaths (XEXP (x, i), maybe_kill_insn, from_luid, to_insn, pnotes);
14070 /* Return 1 if X is the target of a bit-field assignment in BODY, the
14071 pattern of an insn. X must be a REG. */
14073 static int
14074 reg_bitfield_target_p (rtx x, rtx body)
14076 int i;
14078 if (GET_CODE (body) == SET)
14080 rtx dest = SET_DEST (body);
14081 rtx target;
14082 unsigned int regno, tregno, endregno, endtregno;
14084 if (GET_CODE (dest) == ZERO_EXTRACT)
14085 target = XEXP (dest, 0);
14086 else if (GET_CODE (dest) == STRICT_LOW_PART)
14087 target = SUBREG_REG (XEXP (dest, 0));
14088 else
14089 return 0;
14091 if (GET_CODE (target) == SUBREG)
14092 target = SUBREG_REG (target);
14094 if (!REG_P (target))
14095 return 0;
14097 tregno = REGNO (target), regno = REGNO (x);
14098 if (tregno >= FIRST_PSEUDO_REGISTER || regno >= FIRST_PSEUDO_REGISTER)
14099 return target == x;
14101 endtregno = end_hard_regno (GET_MODE (target), tregno);
14102 endregno = end_hard_regno (GET_MODE (x), regno);
14104 return endregno > tregno && regno < endtregno;
14107 else if (GET_CODE (body) == PARALLEL)
14108 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
14109 if (reg_bitfield_target_p (x, XVECEXP (body, 0, i)))
14110 return 1;
14112 return 0;
14115 /* Given a chain of REG_NOTES originally from FROM_INSN, try to place them
14116 as appropriate. I3 and I2 are the insns resulting from the combination
14117 insns including FROM (I2 may be zero).
14119 ELIM_I2 and ELIM_I1 are either zero or registers that we know will
14120 not need REG_DEAD notes because they are being substituted for. This
14121 saves searching in the most common cases.
14123 Each note in the list is either ignored or placed on some insns, depending
14124 on the type of note. */
14126 static void
14127 distribute_notes (rtx notes, rtx_insn *from_insn, rtx_insn *i3, rtx_insn *i2,
14128 rtx elim_i2, rtx elim_i1, rtx elim_i0)
14130 rtx note, next_note;
14131 rtx tem_note;
14132 rtx_insn *tem_insn;
14134 for (note = notes; note; note = next_note)
14136 rtx_insn *place = 0, *place2 = 0;
14138 next_note = XEXP (note, 1);
14139 switch (REG_NOTE_KIND (note))
14141 case REG_BR_PROB:
14142 case REG_BR_PRED:
14143 /* Doesn't matter much where we put this, as long as it's somewhere.
14144 It is preferable to keep these notes on branches, which is most
14145 likely to be i3. */
14146 place = i3;
14147 break;
14149 case REG_NON_LOCAL_GOTO:
14150 if (JUMP_P (i3))
14151 place = i3;
14152 else
14154 gcc_assert (i2 && JUMP_P (i2));
14155 place = i2;
14157 break;
14159 case REG_EH_REGION:
14160 /* These notes must remain with the call or trapping instruction. */
14161 if (CALL_P (i3))
14162 place = i3;
14163 else if (i2 && CALL_P (i2))
14164 place = i2;
14165 else
14167 gcc_assert (cfun->can_throw_non_call_exceptions);
14168 if (may_trap_p (i3))
14169 place = i3;
14170 else if (i2 && may_trap_p (i2))
14171 place = i2;
14172 /* ??? Otherwise assume we've combined things such that we
14173 can now prove that the instructions can't trap. Drop the
14174 note in this case. */
14176 break;
14178 case REG_ARGS_SIZE:
14179 /* ??? How to distribute between i3-i1. Assume i3 contains the
14180 entire adjustment. Assert i3 contains at least some adjust. */
14181 if (!noop_move_p (i3))
14183 poly_int64 old_size, args_size = get_args_size (note);
14184 /* fixup_args_size_notes looks at REG_NORETURN note,
14185 so ensure the note is placed there first. */
14186 if (CALL_P (i3))
14188 rtx *np;
14189 for (np = &next_note; *np; np = &XEXP (*np, 1))
14190 if (REG_NOTE_KIND (*np) == REG_NORETURN)
14192 rtx n = *np;
14193 *np = XEXP (n, 1);
14194 XEXP (n, 1) = REG_NOTES (i3);
14195 REG_NOTES (i3) = n;
14196 break;
14199 old_size = fixup_args_size_notes (PREV_INSN (i3), i3, args_size);
14200 /* emit_call_1 adds for !ACCUMULATE_OUTGOING_ARGS
14201 REG_ARGS_SIZE note to all noreturn calls, allow that here. */
14202 gcc_assert (maybe_ne (old_size, args_size)
14203 || (CALL_P (i3)
14204 && !ACCUMULATE_OUTGOING_ARGS
14205 && find_reg_note (i3, REG_NORETURN, NULL_RTX)));
14207 break;
14209 case REG_NORETURN:
14210 case REG_SETJMP:
14211 case REG_TM:
14212 case REG_CALL_DECL:
14213 case REG_CALL_NOCF_CHECK:
14214 /* These notes must remain with the call. It should not be
14215 possible for both I2 and I3 to be a call. */
14216 if (CALL_P (i3))
14217 place = i3;
14218 else
14220 gcc_assert (i2 && CALL_P (i2));
14221 place = i2;
14223 break;
14225 case REG_UNUSED:
14226 /* Any clobbers for i3 may still exist, and so we must process
14227 REG_UNUSED notes from that insn.
14229 Any clobbers from i2 or i1 can only exist if they were added by
14230 recog_for_combine. In that case, recog_for_combine created the
14231 necessary REG_UNUSED notes. Trying to keep any original
14232 REG_UNUSED notes from these insns can cause incorrect output
14233 if it is for the same register as the original i3 dest.
14234 In that case, we will notice that the register is set in i3,
14235 and then add a REG_UNUSED note for the destination of i3, which
14236 is wrong. However, it is possible to have REG_UNUSED notes from
14237 i2 or i1 for register which were both used and clobbered, so
14238 we keep notes from i2 or i1 if they will turn into REG_DEAD
14239 notes. */
14241 /* If this register is set or clobbered in I3, put the note there
14242 unless there is one already. */
14243 if (reg_set_p (XEXP (note, 0), PATTERN (i3)))
14245 if (from_insn != i3)
14246 break;
14248 if (! (REG_P (XEXP (note, 0))
14249 ? find_regno_note (i3, REG_UNUSED, REGNO (XEXP (note, 0)))
14250 : find_reg_note (i3, REG_UNUSED, XEXP (note, 0))))
14251 place = i3;
14253 /* Otherwise, if this register is used by I3, then this register
14254 now dies here, so we must put a REG_DEAD note here unless there
14255 is one already. */
14256 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3))
14257 && ! (REG_P (XEXP (note, 0))
14258 ? find_regno_note (i3, REG_DEAD,
14259 REGNO (XEXP (note, 0)))
14260 : find_reg_note (i3, REG_DEAD, XEXP (note, 0))))
14262 PUT_REG_NOTE_KIND (note, REG_DEAD);
14263 place = i3;
14266 /* A SET or CLOBBER of the REG_UNUSED reg has been removed,
14267 but we can't tell which at this point. We must reset any
14268 expectations we had about the value that was previously
14269 stored in the reg. ??? Ideally, we'd adjust REG_N_SETS
14270 and, if appropriate, restore its previous value, but we
14271 don't have enough information for that at this point. */
14272 else
14274 record_value_for_reg (XEXP (note, 0), NULL, NULL_RTX);
14276 /* Otherwise, if this register is now referenced in i2
14277 then the register used to be modified in one of the
14278 original insns. If it was i3 (say, in an unused
14279 parallel), it's now completely gone, so the note can
14280 be discarded. But if it was modified in i2, i1 or i0
14281 and we still reference it in i2, then we're
14282 referencing the previous value, and since the
14283 register was modified and REG_UNUSED, we know that
14284 the previous value is now dead. So, if we only
14285 reference the register in i2, we change the note to
14286 REG_DEAD, to reflect the previous value. However, if
14287 we're also setting or clobbering the register as
14288 scratch, we know (because the register was not
14289 referenced in i3) that it's unused, just as it was
14290 unused before, and we place the note in i2. */
14291 if (from_insn != i3 && i2 && INSN_P (i2)
14292 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
14294 if (!reg_set_p (XEXP (note, 0), PATTERN (i2)))
14295 PUT_REG_NOTE_KIND (note, REG_DEAD);
14296 if (! (REG_P (XEXP (note, 0))
14297 ? find_regno_note (i2, REG_NOTE_KIND (note),
14298 REGNO (XEXP (note, 0)))
14299 : find_reg_note (i2, REG_NOTE_KIND (note),
14300 XEXP (note, 0))))
14301 place = i2;
14305 break;
14307 case REG_EQUAL:
14308 case REG_EQUIV:
14309 case REG_NOALIAS:
14310 /* These notes say something about results of an insn. We can
14311 only support them if they used to be on I3 in which case they
14312 remain on I3. Otherwise they are ignored.
14314 If the note refers to an expression that is not a constant, we
14315 must also ignore the note since we cannot tell whether the
14316 equivalence is still true. It might be possible to do
14317 slightly better than this (we only have a problem if I2DEST
14318 or I1DEST is present in the expression), but it doesn't
14319 seem worth the trouble. */
14321 if (from_insn == i3
14322 && (XEXP (note, 0) == 0 || CONSTANT_P (XEXP (note, 0))))
14323 place = i3;
14324 break;
14326 case REG_INC:
14327 /* These notes say something about how a register is used. They must
14328 be present on any use of the register in I2 or I3. */
14329 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3)))
14330 place = i3;
14332 if (i2 && reg_mentioned_p (XEXP (note, 0), PATTERN (i2)))
14334 if (place)
14335 place2 = i2;
14336 else
14337 place = i2;
14339 break;
14341 case REG_LABEL_TARGET:
14342 case REG_LABEL_OPERAND:
14343 /* This can show up in several ways -- either directly in the
14344 pattern, or hidden off in the constant pool with (or without?)
14345 a REG_EQUAL note. */
14346 /* ??? Ignore the without-reg_equal-note problem for now. */
14347 if (reg_mentioned_p (XEXP (note, 0), PATTERN (i3))
14348 || ((tem_note = find_reg_note (i3, REG_EQUAL, NULL_RTX))
14349 && GET_CODE (XEXP (tem_note, 0)) == LABEL_REF
14350 && label_ref_label (XEXP (tem_note, 0)) == XEXP (note, 0)))
14351 place = i3;
14353 if (i2
14354 && (reg_mentioned_p (XEXP (note, 0), PATTERN (i2))
14355 || ((tem_note = find_reg_note (i2, REG_EQUAL, NULL_RTX))
14356 && GET_CODE (XEXP (tem_note, 0)) == LABEL_REF
14357 && label_ref_label (XEXP (tem_note, 0)) == XEXP (note, 0))))
14359 if (place)
14360 place2 = i2;
14361 else
14362 place = i2;
14365 /* For REG_LABEL_TARGET on a JUMP_P, we prefer to put the note
14366 as a JUMP_LABEL or decrement LABEL_NUSES if it's already
14367 there. */
14368 if (place && JUMP_P (place)
14369 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
14370 && (JUMP_LABEL (place) == NULL
14371 || JUMP_LABEL (place) == XEXP (note, 0)))
14373 rtx label = JUMP_LABEL (place);
14375 if (!label)
14376 JUMP_LABEL (place) = XEXP (note, 0);
14377 else if (LABEL_P (label))
14378 LABEL_NUSES (label)--;
14381 if (place2 && JUMP_P (place2)
14382 && REG_NOTE_KIND (note) == REG_LABEL_TARGET
14383 && (JUMP_LABEL (place2) == NULL
14384 || JUMP_LABEL (place2) == XEXP (note, 0)))
14386 rtx label = JUMP_LABEL (place2);
14388 if (!label)
14389 JUMP_LABEL (place2) = XEXP (note, 0);
14390 else if (LABEL_P (label))
14391 LABEL_NUSES (label)--;
14392 place2 = 0;
14394 break;
14396 case REG_NONNEG:
14397 /* This note says something about the value of a register prior
14398 to the execution of an insn. It is too much trouble to see
14399 if the note is still correct in all situations. It is better
14400 to simply delete it. */
14401 break;
14403 case REG_DEAD:
14404 /* If we replaced the right hand side of FROM_INSN with a
14405 REG_EQUAL note, the original use of the dying register
14406 will not have been combined into I3 and I2. In such cases,
14407 FROM_INSN is guaranteed to be the first of the combined
14408 instructions, so we simply need to search back before
14409 FROM_INSN for the previous use or set of this register,
14410 then alter the notes there appropriately.
14412 If the register is used as an input in I3, it dies there.
14413 Similarly for I2, if it is nonzero and adjacent to I3.
14415 If the register is not used as an input in either I3 or I2
14416 and it is not one of the registers we were supposed to eliminate,
14417 there are two possibilities. We might have a non-adjacent I2
14418 or we might have somehow eliminated an additional register
14419 from a computation. For example, we might have had A & B where
14420 we discover that B will always be zero. In this case we will
14421 eliminate the reference to A.
14423 In both cases, we must search to see if we can find a previous
14424 use of A and put the death note there. */
14426 if (from_insn
14427 && from_insn == i2mod
14428 && !reg_overlap_mentioned_p (XEXP (note, 0), i2mod_new_rhs))
14429 tem_insn = from_insn;
14430 else
14432 if (from_insn
14433 && CALL_P (from_insn)
14434 && find_reg_fusage (from_insn, USE, XEXP (note, 0)))
14435 place = from_insn;
14436 else if (i2 && reg_set_p (XEXP (note, 0), PATTERN (i2)))
14438 /* If the new I2 sets the same register that is marked
14439 dead in the note, we do not in general know where to
14440 put the note. One important case we _can_ handle is
14441 when the note comes from I3. */
14442 if (from_insn == i3)
14443 place = i3;
14444 else
14445 break;
14447 else if (reg_referenced_p (XEXP (note, 0), PATTERN (i3)))
14448 place = i3;
14449 else if (i2 != 0 && next_nonnote_nondebug_insn (i2) == i3
14450 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
14451 place = i2;
14452 else if ((rtx_equal_p (XEXP (note, 0), elim_i2)
14453 && !(i2mod
14454 && reg_overlap_mentioned_p (XEXP (note, 0),
14455 i2mod_old_rhs)))
14456 || rtx_equal_p (XEXP (note, 0), elim_i1)
14457 || rtx_equal_p (XEXP (note, 0), elim_i0))
14458 break;
14459 tem_insn = i3;
14462 if (place == 0)
14464 basic_block bb = this_basic_block;
14466 for (tem_insn = PREV_INSN (tem_insn); place == 0; tem_insn = PREV_INSN (tem_insn))
14468 if (!NONDEBUG_INSN_P (tem_insn))
14470 if (tem_insn == BB_HEAD (bb))
14471 break;
14472 continue;
14475 /* If the register is being set at TEM_INSN, see if that is all
14476 TEM_INSN is doing. If so, delete TEM_INSN. Otherwise, make this
14477 into a REG_UNUSED note instead. Don't delete sets to
14478 global register vars. */
14479 if ((REGNO (XEXP (note, 0)) >= FIRST_PSEUDO_REGISTER
14480 || !global_regs[REGNO (XEXP (note, 0))])
14481 && reg_set_p (XEXP (note, 0), PATTERN (tem_insn)))
14483 rtx set = single_set (tem_insn);
14484 rtx inner_dest = 0;
14485 rtx_insn *cc0_setter = NULL;
14487 if (set != 0)
14488 for (inner_dest = SET_DEST (set);
14489 (GET_CODE (inner_dest) == STRICT_LOW_PART
14490 || GET_CODE (inner_dest) == SUBREG
14491 || GET_CODE (inner_dest) == ZERO_EXTRACT);
14492 inner_dest = XEXP (inner_dest, 0))
14495 /* Verify that it was the set, and not a clobber that
14496 modified the register.
14498 CC0 targets must be careful to maintain setter/user
14499 pairs. If we cannot delete the setter due to side
14500 effects, mark the user with an UNUSED note instead
14501 of deleting it. */
14503 if (set != 0 && ! side_effects_p (SET_SRC (set))
14504 && rtx_equal_p (XEXP (note, 0), inner_dest)
14505 && (!HAVE_cc0
14506 || (! reg_mentioned_p (cc0_rtx, SET_SRC (set))
14507 || ((cc0_setter = prev_cc0_setter (tem_insn)) != NULL
14508 && sets_cc0_p (PATTERN (cc0_setter)) > 0))))
14510 /* Move the notes and links of TEM_INSN elsewhere.
14511 This might delete other dead insns recursively.
14512 First set the pattern to something that won't use
14513 any register. */
14514 rtx old_notes = REG_NOTES (tem_insn);
14516 PATTERN (tem_insn) = pc_rtx;
14517 REG_NOTES (tem_insn) = NULL;
14519 distribute_notes (old_notes, tem_insn, tem_insn, NULL,
14520 NULL_RTX, NULL_RTX, NULL_RTX);
14521 distribute_links (LOG_LINKS (tem_insn));
14523 unsigned int regno = REGNO (XEXP (note, 0));
14524 reg_stat_type *rsp = &reg_stat[regno];
14525 if (rsp->last_set == tem_insn)
14526 record_value_for_reg (XEXP (note, 0), NULL, NULL_RTX);
14528 SET_INSN_DELETED (tem_insn);
14529 if (tem_insn == i2)
14530 i2 = NULL;
14532 /* Delete the setter too. */
14533 if (cc0_setter)
14535 PATTERN (cc0_setter) = pc_rtx;
14536 old_notes = REG_NOTES (cc0_setter);
14537 REG_NOTES (cc0_setter) = NULL;
14539 distribute_notes (old_notes, cc0_setter,
14540 cc0_setter, NULL,
14541 NULL_RTX, NULL_RTX, NULL_RTX);
14542 distribute_links (LOG_LINKS (cc0_setter));
14544 SET_INSN_DELETED (cc0_setter);
14545 if (cc0_setter == i2)
14546 i2 = NULL;
14549 else
14551 PUT_REG_NOTE_KIND (note, REG_UNUSED);
14553 /* If there isn't already a REG_UNUSED note, put one
14554 here. Do not place a REG_DEAD note, even if
14555 the register is also used here; that would not
14556 match the algorithm used in lifetime analysis
14557 and can cause the consistency check in the
14558 scheduler to fail. */
14559 if (! find_regno_note (tem_insn, REG_UNUSED,
14560 REGNO (XEXP (note, 0))))
14561 place = tem_insn;
14562 break;
14565 else if (reg_referenced_p (XEXP (note, 0), PATTERN (tem_insn))
14566 || (CALL_P (tem_insn)
14567 && find_reg_fusage (tem_insn, USE, XEXP (note, 0))))
14569 place = tem_insn;
14571 /* If we are doing a 3->2 combination, and we have a
14572 register which formerly died in i3 and was not used
14573 by i2, which now no longer dies in i3 and is used in
14574 i2 but does not die in i2, and place is between i2
14575 and i3, then we may need to move a link from place to
14576 i2. */
14577 if (i2 && DF_INSN_LUID (place) > DF_INSN_LUID (i2)
14578 && from_insn
14579 && DF_INSN_LUID (from_insn) > DF_INSN_LUID (i2)
14580 && reg_referenced_p (XEXP (note, 0), PATTERN (i2)))
14582 struct insn_link *links = LOG_LINKS (place);
14583 LOG_LINKS (place) = NULL;
14584 distribute_links (links);
14586 break;
14589 if (tem_insn == BB_HEAD (bb))
14590 break;
14595 /* If the register is set or already dead at PLACE, we needn't do
14596 anything with this note if it is still a REG_DEAD note.
14597 We check here if it is set at all, not if is it totally replaced,
14598 which is what `dead_or_set_p' checks, so also check for it being
14599 set partially. */
14601 if (place && REG_NOTE_KIND (note) == REG_DEAD)
14603 unsigned int regno = REGNO (XEXP (note, 0));
14604 reg_stat_type *rsp = &reg_stat[regno];
14606 if (dead_or_set_p (place, XEXP (note, 0))
14607 || reg_bitfield_target_p (XEXP (note, 0), PATTERN (place)))
14609 /* Unless the register previously died in PLACE, clear
14610 last_death. [I no longer understand why this is
14611 being done.] */
14612 if (rsp->last_death != place)
14613 rsp->last_death = 0;
14614 place = 0;
14616 else
14617 rsp->last_death = place;
14619 /* If this is a death note for a hard reg that is occupying
14620 multiple registers, ensure that we are still using all
14621 parts of the object. If we find a piece of the object
14622 that is unused, we must arrange for an appropriate REG_DEAD
14623 note to be added for it. However, we can't just emit a USE
14624 and tag the note to it, since the register might actually
14625 be dead; so we recourse, and the recursive call then finds
14626 the previous insn that used this register. */
14628 if (place && REG_NREGS (XEXP (note, 0)) > 1)
14630 unsigned int endregno = END_REGNO (XEXP (note, 0));
14631 bool all_used = true;
14632 unsigned int i;
14634 for (i = regno; i < endregno; i++)
14635 if ((! refers_to_regno_p (i, PATTERN (place))
14636 && ! find_regno_fusage (place, USE, i))
14637 || dead_or_set_regno_p (place, i))
14639 all_used = false;
14640 break;
14643 if (! all_used)
14645 /* Put only REG_DEAD notes for pieces that are
14646 not already dead or set. */
14648 for (i = regno; i < endregno;
14649 i += hard_regno_nregs (i, reg_raw_mode[i]))
14651 rtx piece = regno_reg_rtx[i];
14652 basic_block bb = this_basic_block;
14654 if (! dead_or_set_p (place, piece)
14655 && ! reg_bitfield_target_p (piece,
14656 PATTERN (place)))
14658 rtx new_note = alloc_reg_note (REG_DEAD, piece,
14659 NULL_RTX);
14661 distribute_notes (new_note, place, place,
14662 NULL, NULL_RTX, NULL_RTX,
14663 NULL_RTX);
14665 else if (! refers_to_regno_p (i, PATTERN (place))
14666 && ! find_regno_fusage (place, USE, i))
14667 for (tem_insn = PREV_INSN (place); ;
14668 tem_insn = PREV_INSN (tem_insn))
14670 if (!NONDEBUG_INSN_P (tem_insn))
14672 if (tem_insn == BB_HEAD (bb))
14673 break;
14674 continue;
14676 if (dead_or_set_p (tem_insn, piece)
14677 || reg_bitfield_target_p (piece,
14678 PATTERN (tem_insn)))
14680 add_reg_note (tem_insn, REG_UNUSED, piece);
14681 break;
14686 place = 0;
14690 break;
14692 default:
14693 /* Any other notes should not be present at this point in the
14694 compilation. */
14695 gcc_unreachable ();
14698 if (place)
14700 XEXP (note, 1) = REG_NOTES (place);
14701 REG_NOTES (place) = note;
14703 /* Set added_notes_insn to the earliest insn we added a note to. */
14704 if (added_notes_insn == 0
14705 || DF_INSN_LUID (added_notes_insn) > DF_INSN_LUID (place))
14706 added_notes_insn = place;
14709 if (place2)
14711 add_shallow_copy_of_reg_note (place2, note);
14713 /* Set added_notes_insn to the earliest insn we added a note to. */
14714 if (added_notes_insn == 0
14715 || DF_INSN_LUID (added_notes_insn) > DF_INSN_LUID (place2))
14716 added_notes_insn = place2;
14721 /* Similarly to above, distribute the LOG_LINKS that used to be present on
14722 I3, I2, and I1 to new locations. This is also called to add a link
14723 pointing at I3 when I3's destination is changed. */
14725 static void
14726 distribute_links (struct insn_link *links)
14728 struct insn_link *link, *next_link;
14730 for (link = links; link; link = next_link)
14732 rtx_insn *place = 0;
14733 rtx_insn *insn;
14734 rtx set, reg;
14736 next_link = link->next;
14738 /* If the insn that this link points to is a NOTE, ignore it. */
14739 if (NOTE_P (link->insn))
14740 continue;
14742 set = 0;
14743 rtx pat = PATTERN (link->insn);
14744 if (GET_CODE (pat) == SET)
14745 set = pat;
14746 else if (GET_CODE (pat) == PARALLEL)
14748 int i;
14749 for (i = 0; i < XVECLEN (pat, 0); i++)
14751 set = XVECEXP (pat, 0, i);
14752 if (GET_CODE (set) != SET)
14753 continue;
14755 reg = SET_DEST (set);
14756 while (GET_CODE (reg) == ZERO_EXTRACT
14757 || GET_CODE (reg) == STRICT_LOW_PART
14758 || GET_CODE (reg) == SUBREG)
14759 reg = XEXP (reg, 0);
14761 if (!REG_P (reg))
14762 continue;
14764 if (REGNO (reg) == link->regno)
14765 break;
14767 if (i == XVECLEN (pat, 0))
14768 continue;
14770 else
14771 continue;
14773 reg = SET_DEST (set);
14775 while (GET_CODE (reg) == ZERO_EXTRACT
14776 || GET_CODE (reg) == STRICT_LOW_PART
14777 || GET_CODE (reg) == SUBREG)
14778 reg = XEXP (reg, 0);
14780 /* A LOG_LINK is defined as being placed on the first insn that uses
14781 a register and points to the insn that sets the register. Start
14782 searching at the next insn after the target of the link and stop
14783 when we reach a set of the register or the end of the basic block.
14785 Note that this correctly handles the link that used to point from
14786 I3 to I2. Also note that not much searching is typically done here
14787 since most links don't point very far away. */
14789 for (insn = NEXT_INSN (link->insn);
14790 (insn && (this_basic_block->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)
14791 || BB_HEAD (this_basic_block->next_bb) != insn));
14792 insn = NEXT_INSN (insn))
14793 if (DEBUG_INSN_P (insn))
14794 continue;
14795 else if (INSN_P (insn) && reg_overlap_mentioned_p (reg, PATTERN (insn)))
14797 if (reg_referenced_p (reg, PATTERN (insn)))
14798 place = insn;
14799 break;
14801 else if (CALL_P (insn)
14802 && find_reg_fusage (insn, USE, reg))
14804 place = insn;
14805 break;
14807 else if (INSN_P (insn) && reg_set_p (reg, insn))
14808 break;
14810 /* If we found a place to put the link, place it there unless there
14811 is already a link to the same insn as LINK at that point. */
14813 if (place)
14815 struct insn_link *link2;
14817 FOR_EACH_LOG_LINK (link2, place)
14818 if (link2->insn == link->insn && link2->regno == link->regno)
14819 break;
14821 if (link2 == NULL)
14823 link->next = LOG_LINKS (place);
14824 LOG_LINKS (place) = link;
14826 /* Set added_links_insn to the earliest insn we added a
14827 link to. */
14828 if (added_links_insn == 0
14829 || DF_INSN_LUID (added_links_insn) > DF_INSN_LUID (place))
14830 added_links_insn = place;
14836 /* Check for any register or memory mentioned in EQUIV that is not
14837 mentioned in EXPR. This is used to restrict EQUIV to "specializations"
14838 of EXPR where some registers may have been replaced by constants. */
14840 static bool
14841 unmentioned_reg_p (rtx equiv, rtx expr)
14843 subrtx_iterator::array_type array;
14844 FOR_EACH_SUBRTX (iter, array, equiv, NONCONST)
14846 const_rtx x = *iter;
14847 if ((REG_P (x) || MEM_P (x))
14848 && !reg_mentioned_p (x, expr))
14849 return true;
14851 return false;
14854 DEBUG_FUNCTION void
14855 dump_combine_stats (FILE *file)
14857 fprintf
14858 (file,
14859 ";; Combiner statistics: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n\n",
14860 combine_attempts, combine_merges, combine_extras, combine_successes);
14863 void
14864 dump_combine_total_stats (FILE *file)
14866 fprintf
14867 (file,
14868 "\n;; Combiner totals: %d attempts, %d substitutions (%d requiring new space),\n;; %d successes.\n",
14869 total_attempts, total_merges, total_extras, total_successes);
14872 /* Try combining insns through substitution. */
14873 static unsigned int
14874 rest_of_handle_combine (void)
14876 int rebuild_jump_labels_after_combine;
14878 df_set_flags (DF_LR_RUN_DCE + DF_DEFER_INSN_RESCAN);
14879 df_note_add_problem ();
14880 df_analyze ();
14882 regstat_init_n_sets_and_refs ();
14883 reg_n_sets_max = max_reg_num ();
14885 rebuild_jump_labels_after_combine
14886 = combine_instructions (get_insns (), max_reg_num ());
14888 /* Combining insns may have turned an indirect jump into a
14889 direct jump. Rebuild the JUMP_LABEL fields of jumping
14890 instructions. */
14891 if (rebuild_jump_labels_after_combine)
14893 if (dom_info_available_p (CDI_DOMINATORS))
14894 free_dominance_info (CDI_DOMINATORS);
14895 timevar_push (TV_JUMP);
14896 rebuild_jump_labels (get_insns ());
14897 cleanup_cfg (0);
14898 timevar_pop (TV_JUMP);
14901 regstat_free_n_sets_and_refs ();
14902 return 0;
14905 namespace {
14907 const pass_data pass_data_combine =
14909 RTL_PASS, /* type */
14910 "combine", /* name */
14911 OPTGROUP_NONE, /* optinfo_flags */
14912 TV_COMBINE, /* tv_id */
14913 PROP_cfglayout, /* properties_required */
14914 0, /* properties_provided */
14915 0, /* properties_destroyed */
14916 0, /* todo_flags_start */
14917 TODO_df_finish, /* todo_flags_finish */
14920 class pass_combine : public rtl_opt_pass
14922 public:
14923 pass_combine (gcc::context *ctxt)
14924 : rtl_opt_pass (pass_data_combine, ctxt)
14927 /* opt_pass methods: */
14928 virtual bool gate (function *) { return (optimize > 0); }
14929 virtual unsigned int execute (function *)
14931 return rest_of_handle_combine ();
14934 }; // class pass_combine
14936 } // anon namespace
14938 rtl_opt_pass *
14939 make_pass_combine (gcc::context *ctxt)
14941 return new pass_combine (ctxt);